blob: 47b65f3a19278a3fc768663156a1e49aeefcc3a5 [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",
Leo Liu48ff1082017-11-09 13:18:24 -050086 "VEGAM",
Ken Wangd4196f02016-03-09 09:28:32 +080087 "VEGA10",
Feifei Xu8fab8062017-10-19 17:04:54 +080088 "VEGA12",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080089 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 "LAST",
91};
92
Alex Deucher5494d862018-03-09 15:14:11 -050093static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
94
Alex Deuchere3ecdff2018-03-15 17:39:45 -050095/**
96 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
97 *
98 * @dev: drm_device pointer
99 *
100 * Returns true if the device is a dGPU with HG/PX power control,
101 * otherwise return false.
102 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103bool amdgpu_device_is_px(struct drm_device *dev)
104{
105 struct amdgpu_device *adev = dev->dev_private;
106
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800107 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 return true;
109 return false;
110}
111
112/*
113 * MMIO register access helper functions.
114 */
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500115/**
116 * amdgpu_mm_rreg - read a memory mapped IO register
117 *
118 * @adev: amdgpu_device pointer
119 * @reg: dword aligned register offset
120 * @acc_flags: access flags which require special behavior
121 *
122 * Returns the 32 bit value from the offset specified.
123 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800125 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400127 uint32_t ret;
128
pding43ca8ef2017-10-13 15:38:35 +0800129 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800130 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800131
Monk Liu15d72fd2017-01-25 15:07:40 +0800132 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400133 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 else {
135 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136
137 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
138 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
139 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
140 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400142 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
143 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144}
145
Monk Liu421a2a32018-01-04 18:13:20 +0800146/*
147 * MMIO register read with bytes helper functions
148 * @offset:bytes offset from MMIO start
149 *
150*/
151
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500152/**
153 * amdgpu_mm_rreg8 - read a memory mapped IO register
154 *
155 * @adev: amdgpu_device pointer
156 * @offset: byte aligned register offset
157 *
158 * Returns the 8 bit value from the offset specified.
159 */
Monk Liu421a2a32018-01-04 18:13:20 +0800160uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
161 if (offset < adev->rmmio_size)
162 return (readb(adev->rmmio + offset));
163 BUG();
164}
165
166/*
167 * MMIO register write with bytes helper functions
168 * @offset:bytes offset from MMIO start
169 * @value: the value want to be written to the register
170 *
171*/
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500172/**
173 * amdgpu_mm_wreg8 - read a memory mapped IO register
174 *
175 * @adev: amdgpu_device pointer
176 * @offset: byte aligned register offset
177 * @value: 8 bit value to write
178 *
179 * Writes the value specified to the offset specified.
180 */
Monk Liu421a2a32018-01-04 18:13:20 +0800181void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
182 if (offset < adev->rmmio_size)
183 writeb(value, adev->rmmio + offset);
184 else
185 BUG();
186}
187
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500188/**
189 * amdgpu_mm_wreg - write to a memory mapped IO register
190 *
191 * @adev: amdgpu_device pointer
192 * @reg: dword aligned register offset
193 * @v: 32 bit value to write to the register
194 * @acc_flags: access flags which require special behavior
195 *
196 * Writes the value specified to the offset specified.
197 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800199 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400201 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800202
Ken Wang47ed4e12017-07-04 13:11:52 +0800203 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
204 adev->last_mm_index = v;
205 }
206
pding43ca8ef2017-10-13 15:38:35 +0800207 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800208 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800209
Monk Liu15d72fd2017-01-25 15:07:40 +0800210 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
212 else {
213 unsigned long flags;
214
215 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
216 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
217 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
218 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
219 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800220
221 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
222 udelay(500);
223 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400224}
225
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500226/**
227 * amdgpu_io_rreg - read an IO register
228 *
229 * @adev: amdgpu_device pointer
230 * @reg: dword aligned register offset
231 *
232 * Returns the 32 bit value from the offset specified.
233 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
235{
236 if ((reg * 4) < adev->rio_mem_size)
237 return ioread32(adev->rio_mem + (reg * 4));
238 else {
239 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
240 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
241 }
242}
243
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500244/**
245 * amdgpu_io_wreg - write to an IO register
246 *
247 * @adev: amdgpu_device pointer
248 * @reg: dword aligned register offset
249 * @v: 32 bit value to write to the register
250 *
251 * Writes the value specified to the offset specified.
252 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400253void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
254{
Ken Wang47ed4e12017-07-04 13:11:52 +0800255 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
256 adev->last_mm_index = v;
257 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400258
259 if ((reg * 4) < adev->rio_mem_size)
260 iowrite32(v, adev->rio_mem + (reg * 4));
261 else {
262 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
263 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
264 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800265
266 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
267 udelay(500);
268 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269}
270
271/**
272 * amdgpu_mm_rdoorbell - read a doorbell dword
273 *
274 * @adev: amdgpu_device pointer
275 * @index: doorbell index
276 *
277 * Returns the value in the doorbell aperture at the
278 * requested doorbell index (CIK).
279 */
280u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
281{
282 if (index < adev->doorbell.num_doorbells) {
283 return readl(adev->doorbell.ptr + index);
284 } else {
285 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
286 return 0;
287 }
288}
289
290/**
291 * amdgpu_mm_wdoorbell - write a doorbell dword
292 *
293 * @adev: amdgpu_device pointer
294 * @index: doorbell index
295 * @v: value to write
296 *
297 * Writes @v to the doorbell aperture at the
298 * requested doorbell index (CIK).
299 */
300void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
301{
302 if (index < adev->doorbell.num_doorbells) {
303 writel(v, adev->doorbell.ptr + index);
304 } else {
305 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
306 }
307}
308
309/**
Ken Wang832be402016-03-18 15:23:08 +0800310 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
311 *
312 * @adev: amdgpu_device pointer
313 * @index: doorbell index
314 *
315 * Returns the value in the doorbell aperture at the
316 * requested doorbell index (VEGA10+).
317 */
318u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
319{
320 if (index < adev->doorbell.num_doorbells) {
321 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
322 } else {
323 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
324 return 0;
325 }
326}
327
328/**
329 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
330 *
331 * @adev: amdgpu_device pointer
332 * @index: doorbell index
333 * @v: value to write
334 *
335 * Writes @v to the doorbell aperture at the
336 * requested doorbell index (VEGA10+).
337 */
338void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
339{
340 if (index < adev->doorbell.num_doorbells) {
341 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
342 } else {
343 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
344 }
345}
346
347/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 * amdgpu_invalid_rreg - dummy reg read function
349 *
350 * @adev: amdgpu device pointer
351 * @reg: offset of register
352 *
353 * Dummy register read function. Used for register blocks
354 * that certain asics don't have (all asics).
355 * Returns the value in the register.
356 */
357static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
358{
359 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
360 BUG();
361 return 0;
362}
363
364/**
365 * amdgpu_invalid_wreg - dummy reg write function
366 *
367 * @adev: amdgpu device pointer
368 * @reg: offset of register
369 * @v: value to write to the register
370 *
371 * Dummy register read function. Used for register blocks
372 * that certain asics don't have (all asics).
373 */
374static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
375{
376 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
377 reg, v);
378 BUG();
379}
380
381/**
382 * amdgpu_block_invalid_rreg - dummy reg read function
383 *
384 * @adev: amdgpu device pointer
385 * @block: offset of instance
386 * @reg: offset of register
387 *
388 * Dummy register read function. Used for register blocks
389 * that certain asics don't have (all asics).
390 * Returns the value in the register.
391 */
392static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
393 uint32_t block, uint32_t reg)
394{
395 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
396 reg, block);
397 BUG();
398 return 0;
399}
400
401/**
402 * amdgpu_block_invalid_wreg - dummy reg write function
403 *
404 * @adev: amdgpu device pointer
405 * @block: offset of instance
406 * @reg: offset of register
407 * @v: value to write to the register
408 *
409 * Dummy register read function. Used for register blocks
410 * that certain asics don't have (all asics).
411 */
412static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
413 uint32_t block,
414 uint32_t reg, uint32_t v)
415{
416 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
417 reg, block, v);
418 BUG();
419}
420
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500421/**
422 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
423 *
424 * @adev: amdgpu device pointer
425 *
426 * Allocates a scratch page of VRAM for use by various things in the
427 * driver.
428 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500429static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400430{
Christian Königa4a02772017-07-27 17:24:36 +0200431 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
432 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
433 &adev->vram_scratch.robj,
434 &adev->vram_scratch.gpu_addr,
435 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400436}
437
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500438/**
439 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
440 *
441 * @adev: amdgpu device pointer
442 *
443 * Frees the VRAM scratch page.
444 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500445static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400446{
Christian König078af1a2017-07-27 17:43:00 +0200447 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448}
449
450/**
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500451 * amdgpu_device_program_register_sequence - program an array of registers.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452 *
453 * @adev: amdgpu_device pointer
454 * @registers: pointer to the register array
455 * @array_size: size of the register array
456 *
457 * Programs an array or registers with and and or masks.
458 * This is a helper for setting golden registers.
459 */
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500460void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
461 const u32 *registers,
462 const u32 array_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463{
464 u32 tmp, reg, and_mask, or_mask;
465 int i;
466
467 if (array_size % 3)
468 return;
469
470 for (i = 0; i < array_size; i +=3) {
471 reg = registers[i + 0];
472 and_mask = registers[i + 1];
473 or_mask = registers[i + 2];
474
475 if (and_mask == 0xffffffff) {
476 tmp = or_mask;
477 } else {
478 tmp = RREG32(reg);
479 tmp &= ~and_mask;
480 tmp |= or_mask;
481 }
482 WREG32(reg, tmp);
483 }
484}
485
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500486/**
487 * amdgpu_device_pci_config_reset - reset the GPU
488 *
489 * @adev: amdgpu_device pointer
490 *
491 * Resets the GPU using the pci config reset sequence.
492 * Only applicable to asics prior to vega10.
493 */
Alex Deucher8111c382017-12-14 16:22:53 -0500494void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400495{
496 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
497}
498
499/*
500 * GPU doorbell aperture helpers function.
501 */
502/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500503 * amdgpu_device_doorbell_init - Init doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 *
505 * @adev: amdgpu_device pointer
506 *
507 * Init doorbell driver information (CIK)
508 * Returns 0 on success, error on failure.
509 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500510static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511{
Christian König705e5192017-06-08 11:15:16 +0200512 /* No doorbell on SI hardware generation */
513 if (adev->asic_type < CHIP_BONAIRE) {
514 adev->doorbell.base = 0;
515 adev->doorbell.size = 0;
516 adev->doorbell.num_doorbells = 0;
517 adev->doorbell.ptr = NULL;
518 return 0;
519 }
520
Christian Königd6895ad2017-02-28 10:36:43 +0100521 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
522 return -EINVAL;
523
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524 /* doorbell bar mapping */
525 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
526 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
527
Christian Königedf600d2016-05-03 15:54:54 +0200528 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
530 if (adev->doorbell.num_doorbells == 0)
531 return -EINVAL;
532
Christian König8972e5d2017-03-06 13:34:57 +0100533 adev->doorbell.ptr = ioremap(adev->doorbell.base,
534 adev->doorbell.num_doorbells *
535 sizeof(u32));
536 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400538
539 return 0;
540}
541
542/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500543 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 *
545 * @adev: amdgpu_device pointer
546 *
547 * Tear down doorbell driver information (CIK)
548 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500549static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550{
551 iounmap(adev->doorbell.ptr);
552 adev->doorbell.ptr = NULL;
553}
554
Alex Deucher22cb0162017-12-14 16:27:11 -0500555
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556
557/*
Alex Deucher06ec9072017-12-14 15:02:39 -0500558 * amdgpu_device_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400559 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400560 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 */
562
563/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500564 * amdgpu_device_wb_fini - Disable Writeback and free memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400565 *
566 * @adev: amdgpu_device pointer
567 *
568 * Disables Writeback and frees the Writeback memory (all asics).
569 * Used at driver shutdown.
570 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500571static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572{
573 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400574 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
575 &adev->wb.gpu_addr,
576 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 adev->wb.wb_obj = NULL;
578 }
579}
580
581/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500582 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 *
584 * @adev: amdgpu_device pointer
585 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400586 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 * Used at driver startup.
588 * Returns 0 on success or an -error on failure.
589 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500590static int amdgpu_device_wb_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591{
592 int r;
593
594 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400595 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
596 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400597 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
598 &adev->wb.wb_obj, &adev->wb.gpu_addr,
599 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600 if (r) {
601 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
602 return r;
603 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604
605 adev->wb.num_wb = AMDGPU_MAX_WB;
606 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
607
608 /* clear wb memory */
Monk Liu73469582017-12-29 17:06:41 +0800609 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610 }
611
612 return 0;
613}
614
615/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500616 * amdgpu_device_wb_get - Allocate a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400617 *
618 * @adev: amdgpu_device pointer
619 * @wb: wb index
620 *
621 * Allocate a wb slot for use by the driver (all asics).
622 * Returns 0 on success or -EINVAL on failure.
623 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500624int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625{
626 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400627
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628 if (offset < adev->wb.num_wb) {
629 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800630 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400631 return 0;
632 } else {
633 return -EINVAL;
634 }
635}
636
Ken Wang70142852016-03-18 15:08:49 +0800637/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500638 * amdgpu_device_wb_free - Free a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 *
640 * @adev: amdgpu_device pointer
641 * @wb: wb index
642 *
643 * Free a wb slot allocated for use by the driver (all asics)
644 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500645void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646{
Monk Liu73469582017-12-29 17:06:41 +0800647 wb >>= 3;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 if (wb < adev->wb.num_wb)
Monk Liu73469582017-12-29 17:06:41 +0800649 __clear_bit(wb, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650}
651
652/**
Alex Deucher2543e282017-12-14 16:33:36 -0500653 * amdgpu_device_vram_location - try to find VRAM location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500654 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655 * @adev: amdgpu device structure holding all necessary informations
656 * @mc: memory controller structure holding memory informations
657 * @base: base address at which to put VRAM
658 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400659 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100660 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661 */
Alex Deucher2543e282017-12-14 16:33:36 -0500662void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100663 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664{
665 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
666
667 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
669 if (limit && limit < mc->real_vram_size)
670 mc->real_vram_size = limit;
671 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
672 mc->mc_vram_size >> 20, mc->vram_start,
673 mc->vram_end, mc->real_vram_size >> 20);
674}
675
676/**
Alex Deucher2543e282017-12-14 16:33:36 -0500677 * amdgpu_device_gart_location - try to find GTT location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500678 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400679 * @adev: amdgpu device structure holding all necessary informations
680 * @mc: memory controller structure holding memory informations
681 *
682 * Function will place try to place GTT before or after VRAM.
683 *
684 * If GTT size is bigger than space left then we ajust GTT size.
685 * Thus function will never fails.
686 *
687 * FIXME: when reducing GTT size align new size on power of 2.
688 */
Alex Deucher2543e282017-12-14 16:33:36 -0500689void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100690 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400691{
692 u64 size_af, size_bf;
693
Rex Zhu7951e372018-04-13 16:13:41 +0800694 mc->gart_size += adev->pm.smu_prv_buffer_size;
695
Christian König770d13b2018-01-12 14:52:22 +0100696 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200697 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200699 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200701 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 }
Christian König6f02a692017-07-07 11:56:59 +0200703 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 } else {
Christian König6f02a692017-07-07 11:56:59 +0200705 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200707 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708 }
Christian Königb98f1b92017-11-16 20:12:51 +0100709 /* VCE doesn't like it when BOs cross a 4GB segment, so align
710 * the GART base on a 4GB boundary as well.
711 */
712 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 }
Christian König6f02a692017-07-07 11:56:59 +0200714 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200716 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717}
718
Christian Königd6895ad2017-02-28 10:36:43 +0100719/**
720 * amdgpu_device_resize_fb_bar - try to resize FB BAR
721 *
722 * @adev: amdgpu_device pointer
723 *
724 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
725 * to fail, but if any of the BARs is not accessible after the size we abort
726 * driver loading by returning -ENODEV.
727 */
728int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
729{
Christian König770d13b2018-01-12 14:52:22 +0100730 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100731 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100732 struct pci_bus *root;
733 struct resource *res;
734 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100735 u16 cmd;
736 int r;
737
pding0c03b912017-11-07 11:02:00 +0800738 /* Bypass for VF */
739 if (amdgpu_sriov_vf(adev))
740 return 0;
741
Christian König31b8ada2017-11-15 20:07:38 +0100742 /* Check if the root BUS has 64bit memory resources */
743 root = adev->pdev->bus;
744 while (root->parent)
745 root = root->parent;
746
747 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100748 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100749 res->start > 0x100000000ull)
750 break;
751 }
752
753 /* Trying to resize is pointless without a root hub window above 4GB */
754 if (!res)
755 return 0;
756
Christian Königd6895ad2017-02-28 10:36:43 +0100757 /* Disable memory decoding while we change the BAR addresses and size */
758 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
759 pci_write_config_word(adev->pdev, PCI_COMMAND,
760 cmd & ~PCI_COMMAND_MEMORY);
761
762 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500763 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100764 if (adev->asic_type >= CHIP_BONAIRE)
765 pci_release_resource(adev->pdev, 2);
766
767 pci_release_resource(adev->pdev, 0);
768
769 r = pci_resize_resource(adev->pdev, 0, rbar_size);
770 if (r == -ENOSPC)
771 DRM_INFO("Not enough PCI address space for a large BAR.");
772 else if (r && r != -ENOTSUPP)
773 DRM_ERROR("Problem resizing BAR0 (%d).", r);
774
775 pci_assign_unassigned_bus_resources(adev->pdev->bus);
776
777 /* When the doorbell or fb BAR isn't available we have no chance of
778 * using the device.
779 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500780 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100781 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
782 return -ENODEV;
783
784 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
785
786 return 0;
787}
Horace Chena05502e2017-09-29 14:41:57 +0800788
789/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400790 * GPU helpers function.
791 */
792/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500793 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794 *
795 * @adev: amdgpu_device pointer
796 *
Jim Quc836fec2017-02-10 15:59:59 +0800797 * Check if the asic has been initialized (all asics) at driver startup
798 * or post is needed if hw reset is performed.
799 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400800 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500801bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400802{
803 uint32_t reg;
804
Monk Liubec86372016-09-14 19:38:08 +0800805 if (amdgpu_sriov_vf(adev))
806 return false;
807
808 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800809 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
810 * some old smc fw still need driver do vPost otherwise gpu hang, while
811 * those smc fw version above 22.15 doesn't have this flaw, so we force
812 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800813 */
814 if (adev->asic_type == CHIP_FIJI) {
815 int err;
816 uint32_t fw_ver;
817 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
818 /* force vPost if error occured */
819 if (err)
820 return true;
821
822 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800823 if (fw_ver < 0x00160e00)
824 return true;
Monk Liubec86372016-09-14 19:38:08 +0800825 }
Monk Liubec86372016-09-14 19:38:08 +0800826 }
pding91fe77e2017-10-19 09:38:39 +0800827
828 if (adev->has_hw_reset) {
829 adev->has_hw_reset = false;
830 return true;
831 }
832
833 /* bios scratch used on CIK+ */
834 if (adev->asic_type >= CHIP_BONAIRE)
835 return amdgpu_atombios_scratch_need_asic_init(adev);
836
837 /* check MEM_SIZE for older asics */
838 reg = amdgpu_asic_get_config_memsize(adev);
839
840 if ((reg != 0) && (reg != 0xffffffff))
841 return false;
842
843 return true;
Monk Liubec86372016-09-14 19:38:08 +0800844}
845
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846/* if we get transitioned to only one device, take VGA back */
847/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500848 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400849 *
850 * @cookie: amdgpu_device pointer
851 * @state: enable/disable vga decode
852 *
853 * Enable/disable vga decode (all asics).
854 * Returns VGA resource flags.
855 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500856static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857{
858 struct amdgpu_device *adev = cookie;
859 amdgpu_asic_set_vga_state(adev, state);
860 if (state)
861 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
862 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
863 else
864 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
865}
866
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500867/**
868 * amdgpu_device_check_block_size - validate the vm block size
869 *
870 * @adev: amdgpu_device pointer
871 *
872 * Validates the vm block size specified via module parameter.
873 * The vm block size defines number of bits in page table versus page directory,
874 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
875 * page table and the remaining bits are in the page directory.
876 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500877static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800878{
879 /* defines number of bits in page table versus page directory,
880 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
881 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800882 if (amdgpu_vm_block_size == -1)
883 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800884
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800885 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800886 dev_warn(adev->dev, "VM page table size (%d) too small\n",
887 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100888 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800889 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800890}
891
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500892/**
893 * amdgpu_device_check_vm_size - validate the vm size
894 *
895 * @adev: amdgpu_device pointer
896 *
897 * Validates the vm size in GB specified via module parameter.
898 * The VM size is the size of the GPU virtual memory space in GB.
899 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500900static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800901{
Alex Deucher64dab072017-06-15 18:20:09 -0400902 /* no need to check the default value */
903 if (amdgpu_vm_size == -1)
904 return;
905
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800906 if (amdgpu_vm_size < 1) {
907 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
908 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100909 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800910 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800911}
912
Rex Zhu7951e372018-04-13 16:13:41 +0800913static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
914{
915 struct sysinfo si;
916 bool is_os_64 = (sizeof(void *) == 8) ? true : false;
917 uint64_t total_memory;
918 uint64_t dram_size_seven_GB = 0x1B8000000;
919 uint64_t dram_size_three_GB = 0xB8000000;
920
921 if (amdgpu_smu_memory_pool_size == 0)
922 return;
923
924 if (!is_os_64) {
925 DRM_WARN("Not 64-bit OS, feature not supported\n");
926 goto def_value;
927 }
928 si_meminfo(&si);
929 total_memory = (uint64_t)si.totalram * si.mem_unit;
930
931 if ((amdgpu_smu_memory_pool_size == 1) ||
932 (amdgpu_smu_memory_pool_size == 2)) {
933 if (total_memory < dram_size_three_GB)
934 goto def_value1;
935 } else if ((amdgpu_smu_memory_pool_size == 4) ||
936 (amdgpu_smu_memory_pool_size == 8)) {
937 if (total_memory < dram_size_seven_GB)
938 goto def_value1;
939 } else {
940 DRM_WARN("Smu memory pool size not supported\n");
941 goto def_value;
942 }
943 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
944
945 return;
946
947def_value1:
948 DRM_WARN("No enough system memory\n");
949def_value:
950 adev->pm.smu_prv_buffer_size = 0;
951}
952
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500954 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955 *
956 * @adev: amdgpu_device pointer
957 *
958 * Validates certain module parameters and updates
959 * the associated values used by the driver (all asics).
960 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500961static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962{
Chunming Zhou5b011232015-12-10 17:34:33 +0800963 if (amdgpu_sched_jobs < 4) {
964 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
965 amdgpu_sched_jobs);
966 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400967 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800968 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
969 amdgpu_sched_jobs);
970 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
971 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972
Alex Deucher83e74db2017-08-21 11:58:25 -0400973 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200974 /* gart size must be greater or equal to 32M */
975 dev_warn(adev->dev, "gart size (%d) too small\n",
976 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400977 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978 }
979
Christian König36d38372017-07-07 13:17:45 +0200980 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200982 dev_warn(adev->dev, "gtt size (%d) too small\n",
983 amdgpu_gtt_size);
984 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985 }
986
Roger Hed07f14b2017-08-15 16:05:59 +0800987 /* valid range is between 4 and 9 inclusive */
988 if (amdgpu_vm_fragment_size != -1 &&
989 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
990 dev_warn(adev->dev, "valid range is between 4 and 9\n");
991 amdgpu_vm_fragment_size = -1;
992 }
993
Rex Zhu7951e372018-04-13 16:13:41 +0800994 amdgpu_device_check_smu_prv_buffer_size(adev);
995
Alex Deucher06ec9072017-12-14 15:02:39 -0500996 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400997
Alex Deucher06ec9072017-12-14 15:02:39 -0500998 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +0200999
jimqu526bae32016-11-07 09:53:10 +08001000 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001001 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001002 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1003 amdgpu_vram_page_split);
1004 amdgpu_vram_page_split = 1024;
1005 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -05001006
1007 if (amdgpu_lockup_timeout == 0) {
1008 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
1009 amdgpu_lockup_timeout = 10000;
1010 }
Alex Deucher19aede72018-03-09 15:06:35 -05001011
1012 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001013}
1014
1015/**
1016 * amdgpu_switcheroo_set_state - set switcheroo state
1017 *
1018 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001019 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 *
1021 * Callback for the switcheroo driver. Suspends or resumes the
1022 * the asics before or after it is powered up using ACPI methods.
1023 */
1024static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1025{
1026 struct drm_device *dev = pci_get_drvdata(pdev);
1027
1028 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1029 return;
1030
1031 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001032 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033 /* don't suspend or resume card normally */
1034 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1035
Alex Deucher810ddc32016-08-23 13:25:49 -04001036 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001037
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001038 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1039 drm_kms_helper_poll_enable(dev);
1040 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001041 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001042 drm_kms_helper_poll_disable(dev);
1043 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001044 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1046 }
1047}
1048
1049/**
1050 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1051 *
1052 * @pdev: pci dev pointer
1053 *
1054 * Callback for the switcheroo driver. Check of the switcheroo
1055 * state can be changed.
1056 * Returns true if the state can be changed, false if not.
1057 */
1058static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1059{
1060 struct drm_device *dev = pci_get_drvdata(pdev);
1061
1062 /*
1063 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1064 * locking inversion with the driver load path. And the access here is
1065 * completely racy anyway. So don't bother with locking for now.
1066 */
1067 return dev->open_count == 0;
1068}
1069
1070static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1071 .set_gpu_state = amdgpu_switcheroo_set_state,
1072 .reprobe = NULL,
1073 .can_switch = amdgpu_switcheroo_can_switch,
1074};
1075
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001076/**
1077 * amdgpu_device_ip_set_clockgating_state - set the CG state
1078 *
1079 * @adev: amdgpu_device pointer
1080 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1081 * @state: clockgating state (gate or ungate)
1082 *
1083 * Sets the requested clockgating state for all instances of
1084 * the hardware IP specified.
1085 * Returns the error code from the last instance.
1086 */
Rex Zhu43fa5612018-03-28 13:42:45 -05001087int amdgpu_device_ip_set_clockgating_state(void *dev,
Alex Deucher2990a1f2017-12-15 16:18:00 -05001088 enum amd_ip_block_type block_type,
1089 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090{
Rex Zhu43fa5612018-03-28 13:42:45 -05001091 struct amdgpu_device *adev = dev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001092 int i, r = 0;
1093
1094 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001095 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001096 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001097 if (adev->ip_blocks[i].version->type != block_type)
1098 continue;
1099 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1100 continue;
1101 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1102 (void *)adev, state);
1103 if (r)
1104 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1105 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 }
1107 return r;
1108}
1109
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001110/**
1111 * amdgpu_device_ip_set_powergating_state - set the PG state
1112 *
1113 * @adev: amdgpu_device pointer
1114 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1115 * @state: powergating state (gate or ungate)
1116 *
1117 * Sets the requested powergating state for all instances of
1118 * the hardware IP specified.
1119 * Returns the error code from the last instance.
1120 */
Rex Zhu43fa5612018-03-28 13:42:45 -05001121int amdgpu_device_ip_set_powergating_state(void *dev,
Alex Deucher2990a1f2017-12-15 16:18:00 -05001122 enum amd_ip_block_type block_type,
1123 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124{
Rex Zhu43fa5612018-03-28 13:42:45 -05001125 struct amdgpu_device *adev = dev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001126 int i, r = 0;
1127
1128 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001129 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001130 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001131 if (adev->ip_blocks[i].version->type != block_type)
1132 continue;
1133 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1134 continue;
1135 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1136 (void *)adev, state);
1137 if (r)
1138 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1139 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 }
1141 return r;
1142}
1143
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001144/**
1145 * amdgpu_device_ip_get_clockgating_state - get the CG state
1146 *
1147 * @adev: amdgpu_device pointer
1148 * @flags: clockgating feature flags
1149 *
1150 * Walks the list of IPs on the device and updates the clockgating
1151 * flags for each IP.
1152 * Updates @flags with the feature flags for each hardware IP where
1153 * clockgating is enabled.
1154 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001155void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1156 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001157{
1158 int i;
1159
1160 for (i = 0; i < adev->num_ip_blocks; i++) {
1161 if (!adev->ip_blocks[i].status.valid)
1162 continue;
1163 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1164 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1165 }
1166}
1167
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001168/**
1169 * amdgpu_device_ip_wait_for_idle - wait for idle
1170 *
1171 * @adev: amdgpu_device pointer
1172 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1173 *
1174 * Waits for the request hardware IP to be idle.
1175 * Returns 0 for success or a negative error code on failure.
1176 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001177int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1178 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001179{
1180 int i, r;
1181
1182 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001183 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001184 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001185 if (adev->ip_blocks[i].version->type == block_type) {
1186 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001187 if (r)
1188 return r;
1189 break;
1190 }
1191 }
1192 return 0;
1193
1194}
1195
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001196/**
1197 * amdgpu_device_ip_is_idle - is the hardware IP idle
1198 *
1199 * @adev: amdgpu_device pointer
1200 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1201 *
1202 * Check if the hardware IP is idle or not.
1203 * Returns true if it the IP is idle, false if not.
1204 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001205bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1206 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001207{
1208 int i;
1209
1210 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001211 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001212 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001213 if (adev->ip_blocks[i].version->type == block_type)
1214 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001215 }
1216 return true;
1217
1218}
1219
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001220/**
1221 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1222 *
1223 * @adev: amdgpu_device pointer
1224 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1225 *
1226 * Returns a pointer to the hardware IP block structure
1227 * if it exists for the asic, otherwise NULL.
1228 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001229struct amdgpu_ip_block *
1230amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1231 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232{
1233 int i;
1234
1235 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001236 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001237 return &adev->ip_blocks[i];
1238
1239 return NULL;
1240}
1241
1242/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001243 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 *
1245 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001246 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247 * @major: major version
1248 * @minor: minor version
1249 *
1250 * return 0 if equal or greater
1251 * return 1 if smaller or the ip_block doesn't exist
1252 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001253int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1254 enum amd_ip_block_type type,
1255 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001257 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001258
Alex Deuchera1255102016-10-13 17:41:13 -04001259 if (ip_block && ((ip_block->version->major > major) ||
1260 ((ip_block->version->major == major) &&
1261 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262 return 0;
1263
1264 return 1;
1265}
1266
Alex Deuchera1255102016-10-13 17:41:13 -04001267/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001268 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001269 *
1270 * @adev: amdgpu_device pointer
1271 * @ip_block_version: pointer to the IP to add
1272 *
1273 * Adds the IP block driver information to the collection of IPs
1274 * on the asic.
1275 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001276int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1277 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001278{
1279 if (!ip_block_version)
1280 return -EINVAL;
1281
Shaoyun Liue966a722018-02-01 16:45:26 -05001282 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001283 ip_block_version->funcs->name);
1284
Alex Deuchera1255102016-10-13 17:41:13 -04001285 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1286
1287 return 0;
1288}
1289
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001290/**
1291 * amdgpu_device_enable_virtual_display - enable virtual display feature
1292 *
1293 * @adev: amdgpu_device pointer
1294 *
1295 * Enabled the virtual display feature if the user has enabled it via
1296 * the module parameter virtual_display. This feature provides a virtual
1297 * display hardware on headless boards or in virtualized environments.
1298 * This function parses and validates the configuration string specified by
1299 * the user and configues the virtual display configuration (number of
1300 * virtual connectors, crtcs, etc.) specified.
1301 */
Alex Deucher483ef982016-09-30 12:43:04 -04001302static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001303{
1304 adev->enable_virtual_display = false;
1305
1306 if (amdgpu_virtual_display) {
1307 struct drm_device *ddev = adev->ddev;
1308 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001309 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001310
1311 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1312 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001313 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1314 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001315 if (!strcmp("all", pciaddname)
1316 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001317 long num_crtc;
1318 int res = -1;
1319
Emily Deng9accf2f2016-08-10 16:01:25 +08001320 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001321
1322 if (pciaddname_tmp)
1323 res = kstrtol(pciaddname_tmp, 10,
1324 &num_crtc);
1325
1326 if (!res) {
1327 if (num_crtc < 1)
1328 num_crtc = 1;
1329 if (num_crtc > 6)
1330 num_crtc = 6;
1331 adev->mode_info.num_crtc = num_crtc;
1332 } else {
1333 adev->mode_info.num_crtc = 1;
1334 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001335 break;
1336 }
1337 }
1338
Emily Deng0f663562016-09-30 13:02:18 -04001339 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1340 amdgpu_virtual_display, pci_address_name,
1341 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001342
1343 kfree(pciaddstr);
1344 }
1345}
1346
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001347/**
1348 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1349 *
1350 * @adev: amdgpu_device pointer
1351 *
1352 * Parses the asic configuration parameters specified in the gpu info
1353 * firmware and makes them availale to the driver for use in configuring
1354 * the asic.
1355 * Returns 0 on success, -EINVAL on failure.
1356 */
Alex Deuchere2a75f82017-04-27 16:58:01 -04001357static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1358{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001359 const char *chip_name;
1360 char fw_name[30];
1361 int err;
1362 const struct gpu_info_firmware_header_v1_0 *hdr;
1363
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001364 adev->firmware.gpu_info_fw = NULL;
1365
Alex Deuchere2a75f82017-04-27 16:58:01 -04001366 switch (adev->asic_type) {
1367 case CHIP_TOPAZ:
1368 case CHIP_TONGA:
1369 case CHIP_FIJI:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001370 case CHIP_POLARIS10:
Leo Liucc07f182017-11-09 13:19:58 -05001371 case CHIP_POLARIS11:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001372 case CHIP_POLARIS12:
Leo Liucc07f182017-11-09 13:19:58 -05001373 case CHIP_VEGAM:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001374 case CHIP_CARRIZO:
1375 case CHIP_STONEY:
1376#ifdef CONFIG_DRM_AMDGPU_SI
1377 case CHIP_VERDE:
1378 case CHIP_TAHITI:
1379 case CHIP_PITCAIRN:
1380 case CHIP_OLAND:
1381 case CHIP_HAINAN:
1382#endif
1383#ifdef CONFIG_DRM_AMDGPU_CIK
1384 case CHIP_BONAIRE:
1385 case CHIP_HAWAII:
1386 case CHIP_KAVERI:
1387 case CHIP_KABINI:
1388 case CHIP_MULLINS:
1389#endif
1390 default:
1391 return 0;
1392 case CHIP_VEGA10:
1393 chip_name = "vega10";
1394 break;
Alex Deucher3f76dce2017-09-01 16:20:53 -04001395 case CHIP_VEGA12:
1396 chip_name = "vega12";
1397 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001398 case CHIP_RAVEN:
1399 chip_name = "raven";
1400 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001401 }
1402
1403 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001404 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001405 if (err) {
1406 dev_err(adev->dev,
1407 "Failed to load gpu_info firmware \"%s\"\n",
1408 fw_name);
1409 goto out;
1410 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001411 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001412 if (err) {
1413 dev_err(adev->dev,
1414 "Failed to validate gpu_info firmware \"%s\"\n",
1415 fw_name);
1416 goto out;
1417 }
1418
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001419 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001420 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1421
1422 switch (hdr->version_major) {
1423 case 1:
1424 {
1425 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001426 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001427 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1428
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001429 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1430 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1431 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1432 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 -04001433 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001434 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1435 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1436 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1437 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1438 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001439 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001440 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1441 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001442 adev->gfx.cu_info.max_waves_per_simd =
1443 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1444 adev->gfx.cu_info.max_scratch_slots_per_cu =
1445 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1446 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001447 break;
1448 }
1449 default:
1450 dev_err(adev->dev,
1451 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1452 err = -EINVAL;
1453 goto out;
1454 }
1455out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001456 return err;
1457}
1458
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001459/**
1460 * amdgpu_device_ip_early_init - run early init for hardware IPs
1461 *
1462 * @adev: amdgpu_device pointer
1463 *
1464 * Early initialization pass for hardware IPs. The hardware IPs that make
1465 * up each asic are discovered each IP's early_init callback is run. This
1466 * is the first stage in initializing the asic.
1467 * Returns 0 on success, negative error code on failure.
1468 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001469static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001471 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001472
Alex Deucher483ef982016-09-30 12:43:04 -04001473 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001474
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001475 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001476 case CHIP_TOPAZ:
1477 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001478 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001479 case CHIP_POLARIS11:
1480 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001481 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001482 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001483 case CHIP_STONEY:
1484 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001485 adev->family = AMDGPU_FAMILY_CZ;
1486 else
1487 adev->family = AMDGPU_FAMILY_VI;
1488
1489 r = vi_set_ip_blocks(adev);
1490 if (r)
1491 return r;
1492 break;
Ken Wang33f34802016-01-21 17:29:41 +08001493#ifdef CONFIG_DRM_AMDGPU_SI
1494 case CHIP_VERDE:
1495 case CHIP_TAHITI:
1496 case CHIP_PITCAIRN:
1497 case CHIP_OLAND:
1498 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001499 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001500 r = si_set_ip_blocks(adev);
1501 if (r)
1502 return r;
1503 break;
1504#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001505#ifdef CONFIG_DRM_AMDGPU_CIK
1506 case CHIP_BONAIRE:
1507 case CHIP_HAWAII:
1508 case CHIP_KAVERI:
1509 case CHIP_KABINI:
1510 case CHIP_MULLINS:
1511 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1512 adev->family = AMDGPU_FAMILY_CI;
1513 else
1514 adev->family = AMDGPU_FAMILY_KV;
1515
1516 r = cik_set_ip_blocks(adev);
1517 if (r)
1518 return r;
1519 break;
1520#endif
Alex Deuchere48a3cd2017-09-01 16:22:35 -04001521 case CHIP_VEGA10:
1522 case CHIP_VEGA12:
1523 case CHIP_RAVEN:
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001524 if (adev->asic_type == CHIP_RAVEN)
1525 adev->family = AMDGPU_FAMILY_RV;
1526 else
1527 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001528
1529 r = soc15_set_ip_blocks(adev);
1530 if (r)
1531 return r;
1532 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001533 default:
1534 /* FIXME: not supported yet */
1535 return -EINVAL;
1536 }
1537
Alex Deuchere2a75f82017-04-27 16:58:01 -04001538 r = amdgpu_device_parse_gpu_info_fw(adev);
1539 if (r)
1540 return r;
1541
pding18847342017-11-06 10:21:26 +08001542 amdgpu_amdkfd_device_probe(adev);
1543
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001544 if (amdgpu_sriov_vf(adev)) {
1545 r = amdgpu_virt_request_full_gpu(adev, true);
1546 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001547 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001548 }
1549
Huang Rui00f54b92018-02-27 21:53:00 +08001550 adev->powerplay.pp_feature = amdgpu_pp_feature_mask;
1551
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 for (i = 0; i < adev->num_ip_blocks; i++) {
1553 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001554 DRM_ERROR("disabled ip block: %d <%s>\n",
1555 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001556 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001558 if (adev->ip_blocks[i].version->funcs->early_init) {
1559 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001560 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001561 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001562 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001563 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1564 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001565 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001566 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001567 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001568 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001569 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001570 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001572 }
1573 }
1574
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001575 adev->cg_flags &= amdgpu_cg_mask;
1576 adev->pg_flags &= amdgpu_pg_mask;
1577
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001578 return 0;
1579}
1580
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001581/**
1582 * amdgpu_device_ip_init - run init for hardware IPs
1583 *
1584 * @adev: amdgpu_device pointer
1585 *
1586 * Main initialization pass for hardware IPs. The list of all the hardware
1587 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1588 * are run. sw_init initializes the software state associated with each IP
1589 * and hw_init initializes the hardware associated with each IP.
1590 * Returns 0 on success, negative error code on failure.
1591 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001592static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593{
1594 int i, r;
1595
1596 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001597 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001599 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001600 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001601 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1602 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001603 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001604 }
Alex Deuchera1255102016-10-13 17:41:13 -04001605 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001606
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001608 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001609 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001610 if (r) {
1611 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001613 }
Alex Deuchera1255102016-10-13 17:41:13 -04001614 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001615 if (r) {
1616 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001618 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001619 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001620 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001621 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001622 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001623 }
Alex Deuchera1255102016-10-13 17:41:13 -04001624 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001625
1626 /* right after GMC hw init, we create CSA */
1627 if (amdgpu_sriov_vf(adev)) {
1628 r = amdgpu_allocate_static_csa(adev);
1629 if (r) {
1630 DRM_ERROR("allocate CSA failed %d\n", r);
1631 return r;
1632 }
1633 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 }
1635 }
1636
1637 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001638 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001639 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001640 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001642 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001643 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001644 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1645 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001647 }
Alex Deuchera1255102016-10-13 17:41:13 -04001648 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 }
1650
pding18847342017-11-06 10:21:26 +08001651 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001652
1653 if (amdgpu_sriov_vf(adev))
1654 amdgpu_virt_release_full_gpu(adev, true);
1655
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656 return 0;
1657}
1658
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001659/**
1660 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1661 *
1662 * @adev: amdgpu_device pointer
1663 *
1664 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1665 * this function before a GPU reset. If the value is retained after a
1666 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1667 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001668static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001669{
1670 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1671}
1672
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001673/**
1674 * amdgpu_device_check_vram_lost - check if vram is valid
1675 *
1676 * @adev: amdgpu_device pointer
1677 *
1678 * Checks the reset magic value written to the gart pointer in VRAM.
1679 * The driver calls this after a GPU reset to see if the contents of
1680 * VRAM is lost or now.
1681 * returns true if vram is lost, false if not.
1682 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001683static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001684{
1685 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1686 AMDGPU_RESET_MAGIC_NUM);
1687}
1688
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001689/**
1690 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1691 *
1692 * @adev: amdgpu_device pointer
1693 *
1694 * Late initialization pass enabling clockgating for hardware IPs.
1695 * The list of all the hardware IPs that make up the asic is walked and the
1696 * set_clockgating_state callbacks are run. This stage is run late
1697 * in the init process.
1698 * Returns 0 on success, negative error code on failure.
1699 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001700static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301701{
1702 int i = 0, r;
1703
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001704 if (amdgpu_emu_mode == 1)
1705 return 0;
1706
Shirish S2c773de2018-04-16 12:17:57 +05301707 r = amdgpu_ib_ring_tests(adev);
1708 if (r)
1709 DRM_ERROR("ib ring test failed (%d).\n", r);
1710
Shirish S2dc80b02017-05-25 10:05:25 +05301711 for (i = 0; i < adev->num_ip_blocks; i++) {
1712 if (!adev->ip_blocks[i].status.valid)
1713 continue;
1714 /* skip CG for VCE/UVD, it's handled specially */
1715 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu57716322018-03-12 19:50:38 +08001716 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1717 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Shirish S2dc80b02017-05-25 10:05:25 +05301718 /* enable clockgating to save power */
1719 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1720 AMD_CG_STATE_GATE);
1721 if (r) {
1722 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1723 adev->ip_blocks[i].version->funcs->name, r);
1724 return r;
1725 }
1726 }
1727 }
1728 return 0;
1729}
1730
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001731/**
1732 * amdgpu_device_ip_late_init - run late init for hardware IPs
1733 *
1734 * @adev: amdgpu_device pointer
1735 *
1736 * Late initialization pass for hardware IPs. The list of all the hardware
1737 * IPs that make up the asic is walked and the late_init callbacks are run.
1738 * late_init covers any special initialization that an IP requires
1739 * after all of the have been initialized or something that needs to happen
1740 * late in the init process.
1741 * Returns 0 on success, negative error code on failure.
1742 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001743static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001744{
1745 int i = 0, r;
1746
1747 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001748 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001749 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001750 if (adev->ip_blocks[i].version->funcs->late_init) {
1751 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001752 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001753 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1754 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001755 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001756 }
Alex Deuchera1255102016-10-13 17:41:13 -04001757 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758 }
1759 }
1760
Shirish S2c773de2018-04-16 12:17:57 +05301761 queue_delayed_work(system_wq, &adev->late_init_work,
1762 msecs_to_jiffies(AMDGPU_RESUME_MS));
Shirish S2dc80b02017-05-25 10:05:25 +05301763
Alex Deucher06ec9072017-12-14 15:02:39 -05001764 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001765
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001766 return 0;
1767}
1768
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001769/**
1770 * amdgpu_device_ip_fini - run fini for hardware IPs
1771 *
1772 * @adev: amdgpu_device pointer
1773 *
1774 * Main teardown pass for hardware IPs. The list of all the hardware
1775 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1776 * are run. hw_fini tears down the hardware associated with each IP
1777 * and sw_fini tears down any software state associated with each IP.
1778 * Returns 0 on success, negative error code on failure.
1779 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001780static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001781{
1782 int i, r;
1783
pding18847342017-11-06 10:21:26 +08001784 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001785 /* need to disable SMC first */
1786 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001787 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001788 continue;
Rex Zhu57716322018-03-12 19:50:38 +08001789 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1790 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001791 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001792 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1793 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001794 if (r) {
1795 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001796 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001797 return r;
1798 }
Alex Deuchera1255102016-10-13 17:41:13 -04001799 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001800 /* XXX handle errors */
1801 if (r) {
1802 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001803 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001804 }
Alex Deuchera1255102016-10-13 17:41:13 -04001805 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001806 break;
1807 }
1808 }
1809
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001811 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001812 continue;
Rex Zhu8201a672016-11-24 21:44:44 +08001813
1814 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu81ce8be2018-03-20 16:28:56 +08001815 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1816 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Rex Zhu8201a672016-11-24 21:44:44 +08001817 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1818 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1819 AMD_CG_STATE_UNGATE);
1820 if (r) {
1821 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1822 adev->ip_blocks[i].version->funcs->name, r);
1823 return r;
1824 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001825 }
Rex Zhu8201a672016-11-24 21:44:44 +08001826
Alex Deuchera1255102016-10-13 17:41:13 -04001827 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001828 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001829 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001830 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1831 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001832 }
Rex Zhu8201a672016-11-24 21:44:44 +08001833
Alex Deuchera1255102016-10-13 17:41:13 -04001834 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001835 }
1836
Alex Deucher9950cda2018-01-18 19:05:36 -05001837
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001838 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001839 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001840 continue;
Monk Liuc12aba32018-01-24 12:20:32 +08001841
1842 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1843 amdgpu_free_static_csa(adev);
1844 amdgpu_device_wb_fini(adev);
1845 amdgpu_device_vram_scratch_fini(adev);
1846 }
1847
Alex Deuchera1255102016-10-13 17:41:13 -04001848 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001849 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001850 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001851 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1852 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001853 }
Alex Deuchera1255102016-10-13 17:41:13 -04001854 adev->ip_blocks[i].status.sw = false;
1855 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001856 }
1857
Monk Liua6dcfd92016-05-19 14:36:34 +08001858 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001859 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001860 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001861 if (adev->ip_blocks[i].version->funcs->late_fini)
1862 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1863 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001864 }
1865
Monk Liu030308f2017-09-15 15:34:52 +08001866 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001867 if (amdgpu_virt_release_full_gpu(adev, false))
1868 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001869
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001870 return 0;
1871}
1872
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001873/**
1874 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1875 *
1876 * @work: work_struct
1877 *
1878 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1879 * clockgating setup into a worker thread to speed up driver init and
1880 * resume from suspend.
1881 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001882static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301883{
1884 struct amdgpu_device *adev =
1885 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001886 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301887}
1888
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001889/**
1890 * amdgpu_device_ip_suspend - run suspend for hardware IPs
1891 *
1892 * @adev: amdgpu_device pointer
1893 *
1894 * Main suspend function for hardware IPs. The list of all the hardware
1895 * IPs that make up the asic is walked, clockgating is disabled and the
1896 * suspend callbacks are run. suspend puts the hardware and software state
1897 * in each IP into a state suitable for suspend.
1898 * Returns 0 on success, negative error code on failure.
1899 */
Alex Deuchercdd61df2017-12-14 16:47:40 -05001900int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001901{
1902 int i, r;
1903
Xiangliang Yue941ea92017-01-18 12:47:55 +08001904 if (amdgpu_sriov_vf(adev))
1905 amdgpu_virt_request_full_gpu(adev, false);
1906
Huang Ruib0833692018-03-13 18:39:48 +08001907 /* ungate SMC block powergating */
1908 if (adev->powerplay.pp_feature & PP_GFXOFF_MASK)
1909 amdgpu_device_ip_set_powergating_state(adev,
1910 AMD_IP_BLOCK_TYPE_SMC,
1911 AMD_CG_STATE_UNGATE);
1912
Flora Cuic5a93a22016-02-26 10:45:25 +08001913 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001914 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1915 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001916 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001917 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001918 }
1919
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001921 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001922 continue;
1923 /* ungate blocks so that suspend can properly shut them down */
Rex Zhu5b2a3d22018-03-14 15:38:48 +08001924 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
Rex Zhu57716322018-03-12 19:50:38 +08001925 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deuchera1255102016-10-13 17:41:13 -04001926 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1927 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001928 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001929 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1930 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001931 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001932 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001933 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001934 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001936 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001937 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1938 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001939 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940 }
1941
Xiangliang Yue941ea92017-01-18 12:47:55 +08001942 if (amdgpu_sriov_vf(adev))
1943 amdgpu_virt_release_full_gpu(adev, false);
1944
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 return 0;
1946}
1947
Alex Deucher06ec9072017-12-14 15:02:39 -05001948static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001949{
1950 int i, r;
1951
Monk Liu2cb681b2017-04-26 12:00:49 +08001952 static enum amd_ip_block_type ip_order[] = {
1953 AMD_IP_BLOCK_TYPE_GMC,
1954 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001955 AMD_IP_BLOCK_TYPE_IH,
1956 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001957
Monk Liu2cb681b2017-04-26 12:00:49 +08001958 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1959 int j;
1960 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001961
Monk Liu2cb681b2017-04-26 12:00:49 +08001962 for (j = 0; j < adev->num_ip_blocks; j++) {
1963 block = &adev->ip_blocks[j];
1964
1965 if (block->version->type != ip_order[i] ||
1966 !block->status.valid)
1967 continue;
1968
1969 r = block->version->funcs->hw_init(adev);
1970 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001971 if (r)
1972 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001973 }
1974 }
1975
1976 return 0;
1977}
1978
Alex Deucher06ec9072017-12-14 15:02:39 -05001979static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001980{
1981 int i, r;
1982
Monk Liu2cb681b2017-04-26 12:00:49 +08001983 static enum amd_ip_block_type ip_order[] = {
1984 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001985 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001986 AMD_IP_BLOCK_TYPE_DCE,
1987 AMD_IP_BLOCK_TYPE_GFX,
1988 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001989 AMD_IP_BLOCK_TYPE_UVD,
1990 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001991 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001992
Monk Liu2cb681b2017-04-26 12:00:49 +08001993 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1994 int j;
1995 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001996
Monk Liu2cb681b2017-04-26 12:00:49 +08001997 for (j = 0; j < adev->num_ip_blocks; j++) {
1998 block = &adev->ip_blocks[j];
1999
2000 if (block->version->type != ip_order[i] ||
2001 !block->status.valid)
2002 continue;
2003
2004 r = block->version->funcs->hw_init(adev);
2005 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08002006 if (r)
2007 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08002008 }
2009 }
2010
2011 return 0;
2012}
2013
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002014/**
2015 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2016 *
2017 * @adev: amdgpu_device pointer
2018 *
2019 * First resume function for hardware IPs. The list of all the hardware
2020 * IPs that make up the asic is walked and the resume callbacks are run for
2021 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2022 * after a suspend and updates the software state as necessary. This
2023 * function is also used for restoring the GPU after a GPU reset.
2024 * Returns 0 on success, negative error code on failure.
2025 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002026static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027{
2028 int i, r;
2029
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030 for (i = 0; i < adev->num_ip_blocks; i++) {
2031 if (!adev->ip_blocks[i].status.valid)
2032 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08002033 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002034 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2035 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
Chunming Zhoufcf06492017-05-05 10:33:33 +08002036 r = adev->ip_blocks[i].version->funcs->resume(adev);
2037 if (r) {
2038 DRM_ERROR("resume of IP block <%s> failed %d\n",
2039 adev->ip_blocks[i].version->funcs->name, r);
2040 return r;
2041 }
2042 }
2043 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002044
Chunming Zhoufcf06492017-05-05 10:33:33 +08002045 return 0;
2046}
2047
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002048/**
2049 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2050 *
2051 * @adev: amdgpu_device pointer
2052 *
2053 * First resume function for hardware IPs. The list of all the hardware
2054 * IPs that make up the asic is walked and the resume callbacks are run for
2055 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2056 * functional state after a suspend and updates the software state as
2057 * necessary. This function is also used for restoring the GPU after a GPU
2058 * reset.
2059 * Returns 0 on success, negative error code on failure.
2060 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002061static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002062{
2063 int i, r;
2064
2065 for (i = 0; i < adev->num_ip_blocks; i++) {
2066 if (!adev->ip_blocks[i].status.valid)
2067 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002069 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2070 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002072 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 if (r) {
2074 DRM_ERROR("resume of IP block <%s> failed %d\n",
2075 adev->ip_blocks[i].version->funcs->name, r);
2076 return r;
2077 }
2078 }
2079
2080 return 0;
2081}
2082
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002083/**
2084 * amdgpu_device_ip_resume - run resume for hardware IPs
2085 *
2086 * @adev: amdgpu_device pointer
2087 *
2088 * Main resume function for hardware IPs. The hardware IPs
2089 * are split into two resume functions because they are
2090 * are also used in in recovering from a GPU reset and some additional
2091 * steps need to be take between them. In this case (S3/S4) they are
2092 * run sequentially.
2093 * Returns 0 on success, negative error code on failure.
2094 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002095static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002096{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002097 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002098
Alex Deucher06ec9072017-12-14 15:02:39 -05002099 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002100 if (r)
2101 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05002102 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002103
Chunming Zhoufcf06492017-05-05 10:33:33 +08002104 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002105}
2106
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002107/**
2108 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2109 *
2110 * @adev: amdgpu_device pointer
2111 *
2112 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2113 */
Monk Liu4e99a442016-03-31 13:26:59 +08002114static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002115{
Monk Liu6867e1b2017-10-16 19:50:44 +08002116 if (amdgpu_sriov_vf(adev)) {
2117 if (adev->is_atom_fw) {
2118 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2119 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2120 } else {
2121 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2122 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2123 }
2124
2125 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2126 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002127 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002128}
2129
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002130/**
2131 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2132 *
2133 * @asic_type: AMD asic type
2134 *
2135 * Check if there is DC (new modesetting infrastructre) support for an asic.
2136 * returns true if DC has support, false if not.
2137 */
Harry Wentland45622362017-09-12 15:58:20 -04002138bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2139{
2140 switch (asic_type) {
2141#if defined(CONFIG_DRM_AMD_DC)
2142 case CHIP_BONAIRE:
2143 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002144 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05002145 case CHIP_KABINI:
2146 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04002147 case CHIP_CARRIZO:
2148 case CHIP_STONEY:
2149 case CHIP_POLARIS11:
2150 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002151 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002152 case CHIP_TONGA:
2153 case CHIP_FIJI:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002154 case CHIP_VEGA10:
Alex Deucherdca7b402017-09-02 02:05:29 -04002155 case CHIP_VEGA12:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002156#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002157 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002158#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002159 return amdgpu_dc != 0;
2160#endif
Harry Wentland45622362017-09-12 15:58:20 -04002161 default:
2162 return false;
2163 }
2164}
2165
2166/**
2167 * amdgpu_device_has_dc_support - check if dc is supported
2168 *
2169 * @adev: amdgpu_device_pointer
2170 *
2171 * Returns true for supported, false for not supported
2172 */
2173bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2174{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002175 if (amdgpu_sriov_vf(adev))
2176 return false;
2177
Harry Wentland45622362017-09-12 15:58:20 -04002178 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2179}
2180
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002181/**
2182 * amdgpu_device_init - initialize the driver
2183 *
2184 * @adev: amdgpu_device pointer
2185 * @pdev: drm dev pointer
2186 * @pdev: pci dev pointer
2187 * @flags: driver flags
2188 *
2189 * Initializes the driver info and hw (all asics).
2190 * Returns 0 for success or an error on failure.
2191 * Called at driver startup.
2192 */
2193int amdgpu_device_init(struct amdgpu_device *adev,
2194 struct drm_device *ddev,
2195 struct pci_dev *pdev,
2196 uint32_t flags)
2197{
2198 int r, i;
2199 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002200 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201
2202 adev->shutdown = false;
2203 adev->dev = &pdev->dev;
2204 adev->ddev = ddev;
2205 adev->pdev = pdev;
2206 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002207 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05002209 if (amdgpu_emu_mode == 1)
2210 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01002211 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 adev->accel_working = false;
2213 adev->num_rings = 0;
2214 adev->mman.buffer_funcs = NULL;
2215 adev->mman.buffer_funcs_ring = NULL;
2216 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002217 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01002218 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002219 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002220 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002221
2222 adev->smc_rreg = &amdgpu_invalid_rreg;
2223 adev->smc_wreg = &amdgpu_invalid_wreg;
2224 adev->pcie_rreg = &amdgpu_invalid_rreg;
2225 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002226 adev->pciep_rreg = &amdgpu_invalid_rreg;
2227 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002228 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2229 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2230 adev->didt_rreg = &amdgpu_invalid_rreg;
2231 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002232 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2233 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002234 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2235 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2236
Alex Deucher3e39ab92015-06-05 15:04:33 -04002237 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2238 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2239 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240
2241 /* mutex initialization are all done here so we
2242 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002244 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002245 mutex_init(&adev->pm.mutex);
2246 mutex_init(&adev->gfx.gpu_clock_mutex);
2247 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002248 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002249 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002250 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002251 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002252 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002253 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002254
Alex Deucher06ec9072017-12-14 15:02:39 -05002255 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002257 spin_lock_init(&adev->mmio_idx_lock);
2258 spin_lock_init(&adev->smc_idx_lock);
2259 spin_lock_init(&adev->pcie_idx_lock);
2260 spin_lock_init(&adev->uvd_ctx_idx_lock);
2261 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002262 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002263 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002265 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002266
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002267 INIT_LIST_HEAD(&adev->shadow_list);
2268 mutex_init(&adev->shadow_list_lock);
2269
Andres Rodriguez795f2812017-03-06 16:27:55 -05002270 INIT_LIST_HEAD(&adev->ring_lru_list);
2271 spin_lock_init(&adev->ring_lru_list_lock);
2272
Alex Deucher06ec9072017-12-14 15:02:39 -05002273 INIT_DELAYED_WORK(&adev->late_init_work,
2274 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05302275
Alex Xie0fa49552017-06-08 14:58:05 -04002276 /* Registers mapping */
2277 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002278 if (adev->asic_type >= CHIP_BONAIRE) {
2279 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2280 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2281 } else {
2282 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2283 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2284 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002285
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002286 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2287 if (adev->rmmio == NULL) {
2288 return -ENOMEM;
2289 }
2290 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2291 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2292
Christian König705e5192017-06-08 11:15:16 +02002293 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05002294 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002295
2296 /* io port mapping */
2297 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2298 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2299 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2300 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2301 break;
2302 }
2303 }
2304 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002305 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002306
Alex Deucher5494d862018-03-09 15:14:11 -05002307 amdgpu_device_get_pcie_info(adev);
2308
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002309 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05002310 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002311 if (r)
2312 return r;
2313
2314 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2315 /* this will fail for cards that aren't VGA class devices, just
2316 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05002317 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002318
Alex Deuchere9bef452016-04-25 13:12:18 -04002319 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002320 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002321 if (!pci_is_thunderbolt_attached(adev->pdev))
2322 vga_switcheroo_register_client(adev->pdev,
2323 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324 if (runtime)
2325 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2326
Shaoyun Liu9475a942018-02-01 18:13:23 -05002327 if (amdgpu_emu_mode == 1) {
2328 /* post the asic on emulation mode */
2329 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002330 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05002331 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05002332
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002333 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002334 if (!amdgpu_get_bios(adev)) {
2335 r = -EINVAL;
2336 goto failed;
2337 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002338
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002339 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002340 if (r) {
2341 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002342 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002343 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002344 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345
Monk Liu4e99a442016-03-31 13:26:59 +08002346 /* detect if we are with an SRIOV vbios */
2347 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002348
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002349 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05002350 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002351 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002352 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002353 r = -EINVAL;
2354 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002355 }
Monk Liubec86372016-09-14 19:38:08 +08002356 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002357 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2358 if (r) {
2359 dev_err(adev->dev, "gpu post error!\n");
2360 goto failed;
2361 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002362 }
2363
Alex Deucher88b64e92017-07-10 10:43:10 -04002364 if (adev->is_atom_fw) {
2365 /* Initialize clocks */
2366 r = amdgpu_atomfirmware_get_clock_info(adev);
2367 if (r) {
2368 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002369 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002370 goto failed;
2371 }
2372 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002373 /* Initialize clocks */
2374 r = amdgpu_atombios_get_clock_info(adev);
2375 if (r) {
2376 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002377 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002378 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002379 }
2380 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002381 if (!amdgpu_device_has_dc_support(adev))
2382 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002383 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384
Shaoyun Liubfca0282018-02-01 17:37:50 -05002385fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002386 /* Fence driver */
2387 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002388 if (r) {
2389 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002390 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002391 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002392 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002393
2394 /* init the mode config */
2395 drm_mode_config_init(adev->ddev);
2396
Alex Deucher06ec9072017-12-14 15:02:39 -05002397 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002398 if (r) {
pding8840a382017-10-23 17:22:09 +08002399 /* failed in exclusive mode due to timeout */
2400 if (amdgpu_sriov_vf(adev) &&
2401 !amdgpu_sriov_runtime(adev) &&
2402 amdgpu_virt_mmio_blocked(adev) &&
2403 !amdgpu_virt_wait_reset(adev)) {
2404 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002405 /* Don't send request since VF is inactive. */
2406 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2407 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002408 r = -EAGAIN;
2409 goto failed;
2410 }
Alex Deucher06ec9072017-12-14 15:02:39 -05002411 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002412 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002413 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002414 }
2415
2416 adev->accel_working = true;
2417
Alex Xiee59c0202017-06-01 09:42:59 -04002418 amdgpu_vm_check_compute_bug(adev);
2419
Marek Olšák95844d22016-08-17 23:49:27 +02002420 /* Initialize the buffer migration limit. */
2421 if (amdgpu_moverate >= 0)
2422 max_MBps = amdgpu_moverate;
2423 else
2424 max_MBps = 8; /* Allow 8 MB/s. */
2425 /* Get a log2 for easy divisions. */
2426 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2427
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002428 r = amdgpu_ib_pool_init(adev);
2429 if (r) {
2430 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002431 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002432 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002433 }
2434
Horace Chen2dc8f812017-10-09 16:17:16 +08002435 if (amdgpu_sriov_vf(adev))
2436 amdgpu_virt_init_data_exchange(adev);
2437
Monk Liu9bc92b92017-02-08 17:38:13 +08002438 amdgpu_fbdev_init(adev);
2439
Rex Zhud2f52ac2017-09-22 17:47:27 +08002440 r = amdgpu_pm_sysfs_init(adev);
2441 if (r)
2442 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2443
Alex Deucher75758252017-12-14 15:23:14 -05002444 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002445 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002446 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002447
2448 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002449 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002450 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002451
Huang Rui50ab2532016-06-12 15:51:09 +08002452 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002453 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002454 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002455
Christian König763efb62017-12-06 15:44:51 +01002456 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002457 if (r)
Christian König763efb62017-12-06 15:44:51 +01002458 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002459
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002460 if ((amdgpu_testing & 1)) {
2461 if (adev->accel_working)
2462 amdgpu_test_moves(adev);
2463 else
2464 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2465 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002466 if (amdgpu_benchmarking) {
2467 if (adev->accel_working)
2468 amdgpu_benchmark(adev, amdgpu_benchmarking);
2469 else
2470 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2471 }
2472
2473 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2474 * explicit gating rather than handling it automatically.
2475 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002476 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002477 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002478 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002479 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002480 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002481 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002482
2483 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002484
2485failed:
Gavin Wan89041942017-06-23 13:55:15 -04002486 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002487 if (runtime)
2488 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002489
Alex Deucher83ba1262016-06-03 18:21:41 -04002490 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002491}
2492
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002493/**
2494 * amdgpu_device_fini - tear down the driver
2495 *
2496 * @adev: amdgpu_device pointer
2497 *
2498 * Tear down the driver info (all asics).
2499 * Called at driver shutdown.
2500 */
2501void amdgpu_device_fini(struct amdgpu_device *adev)
2502{
2503 int r;
2504
2505 DRM_INFO("amdgpu: finishing device.\n");
2506 adev->shutdown = true;
Mikita Lipskie5b03032018-03-15 16:53:08 -04002507 /* disable all interrupts */
2508 amdgpu_irq_disable_all(adev);
Mikita Lipskiff97cba2018-03-14 13:41:29 -04002509 if (adev->mode_info.mode_config_initialized){
2510 if (!amdgpu_device_has_dc_support(adev))
2511 drm_crtc_force_disable_all(adev->ddev);
2512 else
2513 drm_atomic_helper_shutdown(adev->ddev);
2514 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002515 amdgpu_ib_pool_fini(adev);
2516 amdgpu_fence_driver_fini(adev);
Emily Deng58e955d2018-03-08 09:35:19 +08002517 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002518 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002519 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002520 if (adev->firmware.gpu_info_fw) {
2521 release_firmware(adev->firmware.gpu_info_fw);
2522 adev->firmware.gpu_info_fw = NULL;
2523 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002524 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302525 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002526 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002527 if (!amdgpu_device_has_dc_support(adev))
2528 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002529
2530 if (amdgpu_emu_mode != 1)
2531 amdgpu_atombios_fini(adev);
2532
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002533 kfree(adev->bios);
2534 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002535 if (!pci_is_thunderbolt_attached(adev->pdev))
2536 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002537 if (adev->flags & AMD_IS_PX)
2538 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002539 vga_client_register(adev->pdev, NULL, NULL, NULL);
2540 if (adev->rio_mem)
2541 pci_iounmap(adev->pdev, adev->rio_mem);
2542 adev->rio_mem = NULL;
2543 iounmap(adev->rmmio);
2544 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002545 amdgpu_device_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002546 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002547}
2548
2549
2550/*
2551 * Suspend & resume.
2552 */
2553/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002554 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002555 *
2556 * @pdev: drm dev pointer
2557 * @state: suspend state
2558 *
2559 * Puts the hw in the suspend state (all asics).
2560 * Returns 0 for success or an error on failure.
2561 * Called at driver suspend.
2562 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002563int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002564{
2565 struct amdgpu_device *adev;
2566 struct drm_crtc *crtc;
2567 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002568 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002569
2570 if (dev == NULL || dev->dev_private == NULL) {
2571 return -ENODEV;
2572 }
2573
2574 adev = dev->dev_private;
2575
2576 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2577 return 0;
2578
2579 drm_kms_helper_poll_disable(dev);
2580
Harry Wentland45622362017-09-12 15:58:20 -04002581 if (!amdgpu_device_has_dc_support(adev)) {
2582 /* turn off display hw */
2583 drm_modeset_lock_all(dev);
2584 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2585 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2586 }
2587 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588 }
2589
Yong Zhaoba997702015-11-09 17:21:45 -05002590 amdgpu_amdkfd_suspend(adev);
2591
Alex Deucher756e6882015-10-08 00:03:36 -04002592 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002593 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002594 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Daniel Stonee68d14d2018-03-30 15:11:38 +01002595 struct drm_framebuffer *fb = crtc->primary->fb;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002596 struct amdgpu_bo *robj;
2597
Alex Deucher756e6882015-10-08 00:03:36 -04002598 if (amdgpu_crtc->cursor_bo) {
2599 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002600 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002601 if (r == 0) {
2602 amdgpu_bo_unpin(aobj);
2603 amdgpu_bo_unreserve(aobj);
2604 }
2605 }
2606
Daniel Stonee68d14d2018-03-30 15:11:38 +01002607 if (fb == NULL || fb->obj[0] == NULL) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608 continue;
2609 }
Daniel Stonee68d14d2018-03-30 15:11:38 +01002610 robj = gem_to_amdgpu_bo(fb->obj[0]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611 /* don't unpin kernel fb objects */
2612 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002613 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002614 if (r == 0) {
2615 amdgpu_bo_unpin(robj);
2616 amdgpu_bo_unreserve(robj);
2617 }
2618 }
2619 }
2620 /* evict vram memory */
2621 amdgpu_bo_evict_vram(adev);
2622
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002623 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002624
Alex Deuchercdd61df2017-12-14 16:47:40 -05002625 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002626
Alex Deuchera0a71e42016-10-10 12:41:36 -04002627 /* evict remaining vram memory
2628 * This second call to evict vram is to evict the gart page table
2629 * using the CPU.
2630 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002631 amdgpu_bo_evict_vram(adev);
2632
2633 pci_save_state(dev->pdev);
2634 if (suspend) {
2635 /* Shut down the device */
2636 pci_disable_device(dev->pdev);
2637 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002638 } else {
2639 r = amdgpu_asic_reset(adev);
2640 if (r)
2641 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002642 }
2643
2644 if (fbcon) {
2645 console_lock();
2646 amdgpu_fbdev_set_suspend(adev, 1);
2647 console_unlock();
2648 }
2649 return 0;
2650}
2651
2652/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002653 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002654 *
2655 * @pdev: drm dev pointer
2656 *
2657 * Bring the hw back to operating state (all asics).
2658 * Returns 0 for success or an error on failure.
2659 * Called at driver resume.
2660 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002661int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002662{
2663 struct drm_connector *connector;
2664 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002665 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002666 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002667
2668 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2669 return 0;
2670
jimqu74b0b152016-09-07 17:09:12 +08002671 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002672 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002673
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002674 if (resume) {
2675 pci_set_power_state(dev->pdev, PCI_D0);
2676 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002677 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002678 if (r)
2679 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002680 }
2681
2682 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002683 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002684 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2685 if (r)
2686 DRM_ERROR("amdgpu asic init failed\n");
2687 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002688
Alex Deucher06ec9072017-12-14 15:02:39 -05002689 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002690 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002691 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002692 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002693 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002694 amdgpu_fence_driver_resume(adev);
2695
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002696
Alex Deucher06ec9072017-12-14 15:02:39 -05002697 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002698 if (r)
2699 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002700
Alex Deucher756e6882015-10-08 00:03:36 -04002701 /* pin cursors */
2702 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2703 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2704
2705 if (amdgpu_crtc->cursor_bo) {
2706 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002707 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002708 if (r == 0) {
2709 r = amdgpu_bo_pin(aobj,
2710 AMDGPU_GEM_DOMAIN_VRAM,
2711 &amdgpu_crtc->cursor_addr);
2712 if (r != 0)
2713 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2714 amdgpu_bo_unreserve(aobj);
2715 }
2716 }
2717 }
Yong Zhaoba997702015-11-09 17:21:45 -05002718 r = amdgpu_amdkfd_resume(adev);
2719 if (r)
2720 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002721
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002722 /* blat the mode back in */
2723 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002724 if (!amdgpu_device_has_dc_support(adev)) {
2725 /* pre DCE11 */
2726 drm_helper_resume_force_mode(dev);
2727
2728 /* turn on display hw */
2729 drm_modeset_lock_all(dev);
2730 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2731 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2732 }
2733 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002734 }
2735 }
2736
2737 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002738
2739 /*
2740 * Most of the connector probing functions try to acquire runtime pm
2741 * refs to ensure that the GPU is powered on when connector polling is
2742 * performed. Since we're calling this from a runtime PM callback,
2743 * trying to acquire rpm refs will cause us to deadlock.
2744 *
2745 * Since we're guaranteed to be holding the rpm lock, it's safe to
2746 * temporarily disable the rpm helpers so this doesn't deadlock us.
2747 */
2748#ifdef CONFIG_PM
2749 dev->dev->power.disable_depth++;
2750#endif
Harry Wentland45622362017-09-12 15:58:20 -04002751 if (!amdgpu_device_has_dc_support(adev))
2752 drm_helper_hpd_irq_event(dev);
2753 else
2754 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002755#ifdef CONFIG_PM
2756 dev->dev->power.disable_depth--;
2757#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002758
Huang Rui03161a62017-04-13 16:12:26 +08002759 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002760 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002761
Huang Rui03161a62017-04-13 16:12:26 +08002762unlock:
2763 if (fbcon)
2764 console_unlock();
2765
2766 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002767}
2768
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002769/**
2770 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2771 *
2772 * @adev: amdgpu_device pointer
2773 *
2774 * The list of all the hardware IPs that make up the asic is walked and
2775 * the check_soft_reset callbacks are run. check_soft_reset determines
2776 * if the asic is still hung or not.
2777 * Returns true if any of the IPs are still in a hung state, false if not.
2778 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002779static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002780{
2781 int i;
2782 bool asic_hang = false;
2783
Monk Liuf993d622017-10-16 19:46:01 +08002784 if (amdgpu_sriov_vf(adev))
2785 return true;
2786
Alex Deucher8bc04c22018-03-29 14:48:37 -05002787 if (amdgpu_asic_need_full_reset(adev))
2788 return true;
2789
Chunming Zhou63fbf422016-07-15 11:19:20 +08002790 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002791 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002792 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002793 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2794 adev->ip_blocks[i].status.hang =
2795 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2796 if (adev->ip_blocks[i].status.hang) {
2797 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002798 asic_hang = true;
2799 }
2800 }
2801 return asic_hang;
2802}
2803
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002804/**
2805 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2806 *
2807 * @adev: amdgpu_device pointer
2808 *
2809 * The list of all the hardware IPs that make up the asic is walked and the
2810 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2811 * handles any IP specific hardware or software state changes that are
2812 * necessary for a soft reset to succeed.
2813 * Returns 0 on success, negative error code on failure.
2814 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002815static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002816{
2817 int i, r = 0;
2818
2819 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002820 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002821 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002822 if (adev->ip_blocks[i].status.hang &&
2823 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2824 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002825 if (r)
2826 return r;
2827 }
2828 }
2829
2830 return 0;
2831}
2832
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002833/**
2834 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2835 *
2836 * @adev: amdgpu_device pointer
2837 *
2838 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2839 * reset is necessary to recover.
2840 * Returns true if a full asic reset is required, false if not.
2841 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002842static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002843{
Alex Deucherda146d32016-10-13 16:07:03 -04002844 int i;
2845
Alex Deucher8bc04c22018-03-29 14:48:37 -05002846 if (amdgpu_asic_need_full_reset(adev))
2847 return true;
2848
Alex Deucherda146d32016-10-13 16:07:03 -04002849 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002850 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002851 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002852 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2853 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2854 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002855 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2856 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002857 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002858 DRM_INFO("Some block need full reset!\n");
2859 return true;
2860 }
2861 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002862 }
2863 return false;
2864}
2865
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002866/**
2867 * amdgpu_device_ip_soft_reset - do a soft reset
2868 *
2869 * @adev: amdgpu_device pointer
2870 *
2871 * The list of all the hardware IPs that make up the asic is walked and the
2872 * soft_reset callbacks are run if the block is hung. soft_reset handles any
2873 * IP specific hardware or software state changes that are necessary to soft
2874 * reset the IP.
2875 * Returns 0 on success, negative error code on failure.
2876 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002877static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002878{
2879 int i, r = 0;
2880
2881 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002882 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002883 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002884 if (adev->ip_blocks[i].status.hang &&
2885 adev->ip_blocks[i].version->funcs->soft_reset) {
2886 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002887 if (r)
2888 return r;
2889 }
2890 }
2891
2892 return 0;
2893}
2894
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002895/**
2896 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
2897 *
2898 * @adev: amdgpu_device pointer
2899 *
2900 * The list of all the hardware IPs that make up the asic is walked and the
2901 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
2902 * handles any IP specific hardware or software state changes that are
2903 * necessary after the IP has been soft reset.
2904 * Returns 0 on success, negative error code on failure.
2905 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002906static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002907{
2908 int i, r = 0;
2909
2910 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002911 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002912 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002913 if (adev->ip_blocks[i].status.hang &&
2914 adev->ip_blocks[i].version->funcs->post_soft_reset)
2915 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002916 if (r)
2917 return r;
2918 }
2919
2920 return 0;
2921}
2922
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002923/**
2924 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
2925 *
2926 * @adev: amdgpu_device pointer
2927 * @ring: amdgpu_ring for the engine handling the buffer operations
2928 * @bo: amdgpu_bo buffer whose shadow is being restored
2929 * @fence: dma_fence associated with the operation
2930 *
2931 * Restores the VRAM buffer contents from the shadow in GTT. Used to
2932 * restore things like GPUVM page tables after a GPU reset where
2933 * the contents of VRAM might be lost.
2934 * Returns 0 on success, negative error code on failure.
2935 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002936static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2937 struct amdgpu_ring *ring,
2938 struct amdgpu_bo *bo,
2939 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002940{
2941 uint32_t domain;
2942 int r;
2943
Roger.He23d2e502017-04-21 14:24:26 +08002944 if (!bo->shadow)
2945 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002946
Alex Xie1d284792017-04-24 13:53:04 -04002947 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002948 if (r)
2949 return r;
2950 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2951 /* if bo has been evicted, then no need to recover */
2952 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002953 r = amdgpu_bo_validate(bo->shadow);
2954 if (r) {
2955 DRM_ERROR("bo validate failed!\n");
2956 goto err;
2957 }
2958
Roger.He23d2e502017-04-21 14:24:26 +08002959 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002960 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002961 if (r) {
2962 DRM_ERROR("recover page table failed!\n");
2963 goto err;
2964 }
2965 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002966err:
Roger.He23d2e502017-04-21 14:24:26 +08002967 amdgpu_bo_unreserve(bo);
2968 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002969}
2970
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002971/**
2972 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
2973 *
2974 * @adev: amdgpu_device pointer
2975 *
2976 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
2977 * restore things like GPUVM page tables after a GPU reset where
2978 * the contents of VRAM might be lost.
2979 * Returns 0 on success, 1 on failure.
2980 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002981static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2982{
2983 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2984 struct amdgpu_bo *bo, *tmp;
2985 struct dma_fence *fence = NULL, *next = NULL;
2986 long r = 1;
2987 int i = 0;
2988 long tmo;
2989
2990 if (amdgpu_sriov_runtime(adev))
2991 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2992 else
2993 tmo = msecs_to_jiffies(100);
2994
2995 DRM_INFO("recover vram bo from shadow start\n");
2996 mutex_lock(&adev->shadow_list_lock);
2997 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2998 next = NULL;
2999 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
3000 if (fence) {
3001 r = dma_fence_wait_timeout(fence, false, tmo);
3002 if (r == 0)
3003 pr_err("wait fence %p[%d] timeout\n", fence, i);
3004 else if (r < 0)
3005 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3006 if (r < 1) {
3007 dma_fence_put(fence);
3008 fence = next;
3009 break;
3010 }
3011 i++;
3012 }
3013
3014 dma_fence_put(fence);
3015 fence = next;
3016 }
3017 mutex_unlock(&adev->shadow_list_lock);
3018
3019 if (fence) {
3020 r = dma_fence_wait_timeout(fence, false, tmo);
3021 if (r == 0)
3022 pr_err("wait fence %p[%d] timeout\n", fence, i);
3023 else if (r < 0)
3024 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3025
3026 }
3027 dma_fence_put(fence);
3028
3029 if (r > 0)
3030 DRM_INFO("recover vram bo from shadow done\n");
3031 else
3032 DRM_ERROR("recover vram bo from shadow failed\n");
3033
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003034 return (r > 0) ? 0 : 1;
Monk Liuc41d1cf2017-12-25 11:59:27 +08003035}
3036
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003037/**
Alex Deucher06ec9072017-12-14 15:02:39 -05003038 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08003039 *
3040 * @adev: amdgpu device pointer
Monk Liua90ad3c2017-01-23 14:22:08 +08003041 *
Monk Liu57406822017-10-25 16:37:02 +08003042 * attempt to do soft-reset or full-reset and reinitialize Asic
3043 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003044 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08003045static int amdgpu_device_reset(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08003046{
Monk Liu57406822017-10-25 16:37:02 +08003047 bool need_full_reset, vram_lost = 0;
3048 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003049
Alex Deucher06ec9072017-12-14 15:02:39 -05003050 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08003051
3052 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05003053 amdgpu_device_ip_pre_soft_reset(adev);
3054 r = amdgpu_device_ip_soft_reset(adev);
3055 amdgpu_device_ip_post_soft_reset(adev);
3056 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08003057 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3058 need_full_reset = true;
3059 }
3060 }
3061
3062 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05003063 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003064
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003065retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08003066 r = amdgpu_asic_reset(adev);
3067 /* post card */
3068 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05003069
Chunming Zhou35d782f2016-07-15 15:57:13 +08003070 if (!r) {
3071 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05003072 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003073 if (r)
3074 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003075
Alex Deucher06ec9072017-12-14 15:02:39 -05003076 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08003077 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003078 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08003079 atomic_inc(&adev->vram_lost_counter);
3080 }
Monk Liu57406822017-10-25 16:37:02 +08003081
Christian Königc1c7ce82017-10-16 16:50:32 +02003082 r = amdgpu_gtt_mgr_recover(
3083 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003084 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08003085 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003086
Alex Deucher06ec9072017-12-14 15:02:39 -05003087 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003088 if (r)
3089 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003090
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003091 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05003092 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003093 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08003094 }
Monk Liu57406822017-10-25 16:37:02 +08003095
Chunming Zhoufcf06492017-05-05 10:33:33 +08003096out:
3097 if (!r) {
3098 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08003099 r = amdgpu_ib_ring_tests(adev);
3100 if (r) {
3101 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05003102 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003103 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08003104 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08003105 }
Monk Liu57406822017-10-25 16:37:02 +08003106 }
3107
Monk Liuc41d1cf2017-12-25 11:59:27 +08003108 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3109 r = amdgpu_device_handle_vram_lost(adev);
Monk Liu57406822017-10-25 16:37:02 +08003110
3111 return r;
3112}
3113
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003114/**
Alex Deucher06ec9072017-12-14 15:02:39 -05003115 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08003116 *
3117 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08003118 *
3119 * do VF FLR and reinitialize Asic
3120 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003121 */
3122static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3123 bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08003124{
3125 int r;
3126
3127 if (from_hypervisor)
3128 r = amdgpu_virt_request_full_gpu(adev, true);
3129 else
3130 r = amdgpu_virt_reset_gpu(adev);
3131 if (r)
3132 return r;
3133
3134 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05003135 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08003136 if (r)
3137 goto error;
3138
3139 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02003140 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08003141
3142 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05003143 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003144 amdgpu_virt_release_full_gpu(adev, true);
Monk Liu57406822017-10-25 16:37:02 +08003145 if (r)
3146 goto error;
3147
3148 amdgpu_irq_gpu_reset_resume_helper(adev);
3149 r = amdgpu_ib_ring_tests(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003150
3151 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3152 atomic_inc(&adev->vram_lost_counter);
3153 r = amdgpu_device_handle_vram_lost(adev);
3154 }
Monk Liu57406822017-10-25 16:37:02 +08003155
3156error:
Monk Liu57406822017-10-25 16:37:02 +08003157
3158 return r;
3159}
3160
3161/**
Alex Deucher5f152b52017-12-15 16:40:49 -05003162 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08003163 *
3164 * @adev: amdgpu device pointer
3165 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003166 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08003167 *
3168 * Attempt to reset the GPU if it has hung (all asics).
3169 * Returns 0 for success or an error on failure.
3170 */
Alex Deucher5f152b52017-12-15 16:40:49 -05003171int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3172 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08003173{
3174 struct drm_atomic_state *state = NULL;
Monk Liu57406822017-10-25 16:37:02 +08003175 int i, r, resched;
3176
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05003177 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003178 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3179 return 0;
3180 }
3181
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003182 if (!force && (amdgpu_gpu_recovery == 0 ||
3183 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3184 DRM_INFO("GPU recovery disabled.\n");
3185 return 0;
3186 }
3187
Monk Liu57406822017-10-25 16:37:02 +08003188 dev_info(adev->dev, "GPU reset begin!\n");
3189
Monk Liu13a752e2017-10-17 15:11:12 +08003190 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003191 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003192 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003193
3194 /* block TTM */
3195 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Monk Liu71182662017-12-25 15:14:58 +08003196
Monk Liu57406822017-10-25 16:37:02 +08003197 /* store modesetting */
3198 if (amdgpu_device_has_dc_support(adev))
3199 state = drm_atomic_helper_suspend(adev->ddev);
3200
Monk Liu71182662017-12-25 15:14:58 +08003201 /* block all schedulers and reset given job's ring */
Monk Liu57406822017-10-25 16:37:02 +08003202 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3203 struct amdgpu_ring *ring = adev->rings[i];
3204
3205 if (!ring || !ring->sched.thread)
3206 continue;
3207
Monk Liu71182662017-12-25 15:14:58 +08003208 kthread_park(ring->sched.thread);
3209
Monk Liu57406822017-10-25 16:37:02 +08003210 if (job && job->ring->idx != i)
3211 continue;
3212
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003213 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08003214
3215 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3216 amdgpu_fence_driver_force_completion(ring);
3217 }
3218
3219 if (amdgpu_sriov_vf(adev))
Monk Liuc41d1cf2017-12-25 11:59:27 +08003220 r = amdgpu_device_reset_sriov(adev, job ? false : true);
Monk Liu57406822017-10-25 16:37:02 +08003221 else
Monk Liuc41d1cf2017-12-25 11:59:27 +08003222 r = amdgpu_device_reset(adev);
Monk Liu57406822017-10-25 16:37:02 +08003223
Monk Liu71182662017-12-25 15:14:58 +08003224 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3225 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003226
Monk Liu71182662017-12-25 15:14:58 +08003227 if (!ring || !ring->sched.thread)
3228 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003229
Monk Liu71182662017-12-25 15:14:58 +08003230 /* only need recovery sched of the given job's ring
3231 * or all rings (in the case @job is NULL)
3232 * after above amdgpu_reset accomplished
3233 */
3234 if ((!job || job->ring->idx == i) && !r)
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003235 drm_sched_job_recovery(&ring->sched);
Monk Liu57406822017-10-25 16:37:02 +08003236
Monk Liu71182662017-12-25 15:14:58 +08003237 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003238 }
3239
Harry Wentland45622362017-09-12 15:58:20 -04003240 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003241 if (drm_atomic_helper_resume(adev->ddev, state))
3242 dev_info(adev->dev, "drm resume failed:%d\n", r);
Monk Liu57406822017-10-25 16:37:02 +08003243 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003244 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003245 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003246
3247 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003248
Gavin Wan89041942017-06-23 13:55:15 -04003249 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003250 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003251 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3252 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3253 } else {
3254 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003255 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003256
Gavin Wan89041942017-06-23 13:55:15 -04003257 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003258 adev->in_gpu_reset = 0;
3259 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003260 return r;
3261}
3262
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003263/**
3264 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3265 *
3266 * @adev: amdgpu_device pointer
3267 *
3268 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3269 * and lanes) of the slot the device is in. Handles APUs and
3270 * virtualized environments where PCIE config space may not be available.
3271 */
Alex Deucher5494d862018-03-09 15:14:11 -05003272static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003273{
3274 u32 mask;
3275 int ret;
3276
Alex Deuchercd474ba2016-02-04 10:21:23 -05003277 if (amdgpu_pcie_gen_cap)
3278 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3279
3280 if (amdgpu_pcie_lane_cap)
3281 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3282
3283 /* covers APUs as well */
3284 if (pci_is_root_bus(adev->pdev->bus)) {
3285 if (adev->pm.pcie_gen_mask == 0)
3286 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3287 if (adev->pm.pcie_mlw_mask == 0)
3288 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003289 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003290 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003291
3292 if (adev->pm.pcie_gen_mask == 0) {
3293 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3294 if (!ret) {
3295 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3296 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3297 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3298
3299 if (mask & DRM_PCIE_SPEED_25)
3300 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3301 if (mask & DRM_PCIE_SPEED_50)
3302 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3303 if (mask & DRM_PCIE_SPEED_80)
3304 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3305 } else {
3306 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3307 }
3308 }
3309 if (adev->pm.pcie_mlw_mask == 0) {
3310 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3311 if (!ret) {
3312 switch (mask) {
3313 case 32:
3314 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3315 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3316 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3317 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3318 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3319 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3320 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3321 break;
3322 case 16:
3323 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3324 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3325 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3326 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3327 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3328 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3329 break;
3330 case 12:
3331 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3332 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3333 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3334 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3335 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3336 break;
3337 case 8:
3338 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3339 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3340 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3341 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3342 break;
3343 case 4:
3344 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3345 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3346 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3347 break;
3348 case 2:
3349 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3350 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3351 break;
3352 case 1:
3353 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3354 break;
3355 default:
3356 break;
3357 }
3358 } else {
3359 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003360 }
3361 }
3362}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003363