blob: adeb48ec4897cff4132c9a9d7c23586e9567980c [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",
Feifei Xu956fcdd2018-04-20 12:27:54 +080089 "VEGA20",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080090 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091 "LAST",
92};
93
Alex Deucher5494d862018-03-09 15:14:11 -050094static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
95
Alex Deuchere3ecdff2018-03-15 17:39:45 -050096/**
97 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
98 *
99 * @dev: drm_device pointer
100 *
101 * Returns true if the device is a dGPU with HG/PX power control,
102 * otherwise return false.
103 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104bool amdgpu_device_is_px(struct drm_device *dev)
105{
106 struct amdgpu_device *adev = dev->dev_private;
107
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800108 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 return true;
110 return false;
111}
112
113/*
114 * MMIO register access helper functions.
115 */
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500116/**
117 * amdgpu_mm_rreg - read a memory mapped IO register
118 *
119 * @adev: amdgpu_device pointer
120 * @reg: dword aligned register offset
121 * @acc_flags: access flags which require special behavior
122 *
123 * Returns the 32 bit value from the offset specified.
124 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800126 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400128 uint32_t ret;
129
pding43ca8ef2017-10-13 15:38:35 +0800130 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800131 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800132
Monk Liu15d72fd2017-01-25 15:07:40 +0800133 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400134 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 else {
136 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137
138 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
139 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
140 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
141 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400143 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
144 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145}
146
Monk Liu421a2a32018-01-04 18:13:20 +0800147/*
148 * MMIO register read with bytes helper functions
149 * @offset:bytes offset from MMIO start
150 *
151*/
152
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500153/**
154 * amdgpu_mm_rreg8 - read a memory mapped IO register
155 *
156 * @adev: amdgpu_device pointer
157 * @offset: byte aligned register offset
158 *
159 * Returns the 8 bit value from the offset specified.
160 */
Monk Liu421a2a32018-01-04 18:13:20 +0800161uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
162 if (offset < adev->rmmio_size)
163 return (readb(adev->rmmio + offset));
164 BUG();
165}
166
167/*
168 * MMIO register write with bytes helper functions
169 * @offset:bytes offset from MMIO start
170 * @value: the value want to be written to the register
171 *
172*/
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500173/**
174 * amdgpu_mm_wreg8 - read a memory mapped IO register
175 *
176 * @adev: amdgpu_device pointer
177 * @offset: byte aligned register offset
178 * @value: 8 bit value to write
179 *
180 * Writes the value specified to the offset specified.
181 */
Monk Liu421a2a32018-01-04 18:13:20 +0800182void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
183 if (offset < adev->rmmio_size)
184 writeb(value, adev->rmmio + offset);
185 else
186 BUG();
187}
188
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500189/**
190 * amdgpu_mm_wreg - write to a memory mapped IO register
191 *
192 * @adev: amdgpu_device pointer
193 * @reg: dword aligned register offset
194 * @v: 32 bit value to write to the register
195 * @acc_flags: access flags which require special behavior
196 *
197 * Writes the value specified to the offset specified.
198 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800200 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400201{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400202 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800203
Ken Wang47ed4e12017-07-04 13:11:52 +0800204 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
205 adev->last_mm_index = v;
206 }
207
pding43ca8ef2017-10-13 15:38:35 +0800208 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800209 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800210
Monk Liu15d72fd2017-01-25 15:07:40 +0800211 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
213 else {
214 unsigned long flags;
215
216 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
217 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
218 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
219 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
220 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800221
222 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
223 udelay(500);
224 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225}
226
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500227/**
228 * amdgpu_io_rreg - read an IO register
229 *
230 * @adev: amdgpu_device pointer
231 * @reg: dword aligned register offset
232 *
233 * Returns the 32 bit value from the offset specified.
234 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
236{
237 if ((reg * 4) < adev->rio_mem_size)
238 return ioread32(adev->rio_mem + (reg * 4));
239 else {
240 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
241 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
242 }
243}
244
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500245/**
246 * amdgpu_io_wreg - write to an IO register
247 *
248 * @adev: amdgpu_device pointer
249 * @reg: dword aligned register offset
250 * @v: 32 bit value to write to the register
251 *
252 * Writes the value specified to the offset specified.
253 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
255{
Ken Wang47ed4e12017-07-04 13:11:52 +0800256 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
257 adev->last_mm_index = v;
258 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400259
260 if ((reg * 4) < adev->rio_mem_size)
261 iowrite32(v, adev->rio_mem + (reg * 4));
262 else {
263 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
264 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
265 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800266
267 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
268 udelay(500);
269 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270}
271
272/**
273 * amdgpu_mm_rdoorbell - read a doorbell dword
274 *
275 * @adev: amdgpu_device pointer
276 * @index: doorbell index
277 *
278 * Returns the value in the doorbell aperture at the
279 * requested doorbell index (CIK).
280 */
281u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
282{
283 if (index < adev->doorbell.num_doorbells) {
284 return readl(adev->doorbell.ptr + index);
285 } else {
286 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
287 return 0;
288 }
289}
290
291/**
292 * amdgpu_mm_wdoorbell - write a doorbell dword
293 *
294 * @adev: amdgpu_device pointer
295 * @index: doorbell index
296 * @v: value to write
297 *
298 * Writes @v to the doorbell aperture at the
299 * requested doorbell index (CIK).
300 */
301void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
302{
303 if (index < adev->doorbell.num_doorbells) {
304 writel(v, adev->doorbell.ptr + index);
305 } else {
306 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
307 }
308}
309
310/**
Ken Wang832be402016-03-18 15:23:08 +0800311 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
312 *
313 * @adev: amdgpu_device pointer
314 * @index: doorbell index
315 *
316 * Returns the value in the doorbell aperture at the
317 * requested doorbell index (VEGA10+).
318 */
319u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
320{
321 if (index < adev->doorbell.num_doorbells) {
322 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
323 } else {
324 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
325 return 0;
326 }
327}
328
329/**
330 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
331 *
332 * @adev: amdgpu_device pointer
333 * @index: doorbell index
334 * @v: value to write
335 *
336 * Writes @v to the doorbell aperture at the
337 * requested doorbell index (VEGA10+).
338 */
339void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
340{
341 if (index < adev->doorbell.num_doorbells) {
342 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
343 } else {
344 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
345 }
346}
347
348/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400349 * amdgpu_invalid_rreg - dummy reg read function
350 *
351 * @adev: amdgpu device pointer
352 * @reg: offset of register
353 *
354 * Dummy register read function. Used for register blocks
355 * that certain asics don't have (all asics).
356 * Returns the value in the register.
357 */
358static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
359{
360 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
361 BUG();
362 return 0;
363}
364
365/**
366 * amdgpu_invalid_wreg - dummy reg write function
367 *
368 * @adev: amdgpu device pointer
369 * @reg: offset of register
370 * @v: value to write to the register
371 *
372 * Dummy register read function. Used for register blocks
373 * that certain asics don't have (all asics).
374 */
375static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
376{
377 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
378 reg, v);
379 BUG();
380}
381
382/**
383 * amdgpu_block_invalid_rreg - dummy reg read function
384 *
385 * @adev: amdgpu device pointer
386 * @block: offset of instance
387 * @reg: offset of register
388 *
389 * Dummy register read function. Used for register blocks
390 * that certain asics don't have (all asics).
391 * Returns the value in the register.
392 */
393static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
394 uint32_t block, uint32_t reg)
395{
396 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
397 reg, block);
398 BUG();
399 return 0;
400}
401
402/**
403 * amdgpu_block_invalid_wreg - dummy reg write function
404 *
405 * @adev: amdgpu device pointer
406 * @block: offset of instance
407 * @reg: offset of register
408 * @v: value to write to the register
409 *
410 * Dummy register read function. Used for register blocks
411 * that certain asics don't have (all asics).
412 */
413static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
414 uint32_t block,
415 uint32_t reg, uint32_t v)
416{
417 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
418 reg, block, v);
419 BUG();
420}
421
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500422/**
423 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
424 *
425 * @adev: amdgpu device pointer
426 *
427 * Allocates a scratch page of VRAM for use by various things in the
428 * driver.
429 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500430static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400431{
Christian Königa4a02772017-07-27 17:24:36 +0200432 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
433 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
434 &adev->vram_scratch.robj,
435 &adev->vram_scratch.gpu_addr,
436 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437}
438
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500439/**
440 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
441 *
442 * @adev: amdgpu device pointer
443 *
444 * Frees the VRAM scratch page.
445 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500446static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400447{
Christian König078af1a2017-07-27 17:43:00 +0200448 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449}
450
451/**
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500452 * amdgpu_device_program_register_sequence - program an array of registers.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 *
454 * @adev: amdgpu_device pointer
455 * @registers: pointer to the register array
456 * @array_size: size of the register array
457 *
458 * Programs an array or registers with and and or masks.
459 * This is a helper for setting golden registers.
460 */
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500461void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
462 const u32 *registers,
463 const u32 array_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464{
465 u32 tmp, reg, and_mask, or_mask;
466 int i;
467
468 if (array_size % 3)
469 return;
470
471 for (i = 0; i < array_size; i +=3) {
472 reg = registers[i + 0];
473 and_mask = registers[i + 1];
474 or_mask = registers[i + 2];
475
476 if (and_mask == 0xffffffff) {
477 tmp = or_mask;
478 } else {
479 tmp = RREG32(reg);
480 tmp &= ~and_mask;
481 tmp |= or_mask;
482 }
483 WREG32(reg, tmp);
484 }
485}
486
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500487/**
488 * amdgpu_device_pci_config_reset - reset the GPU
489 *
490 * @adev: amdgpu_device pointer
491 *
492 * Resets the GPU using the pci config reset sequence.
493 * Only applicable to asics prior to vega10.
494 */
Alex Deucher8111c382017-12-14 16:22:53 -0500495void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496{
497 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
498}
499
500/*
501 * GPU doorbell aperture helpers function.
502 */
503/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500504 * amdgpu_device_doorbell_init - Init doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 *
506 * @adev: amdgpu_device pointer
507 *
508 * Init doorbell driver information (CIK)
509 * Returns 0 on success, error on failure.
510 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500511static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400512{
Christian König705e5192017-06-08 11:15:16 +0200513 /* No doorbell on SI hardware generation */
514 if (adev->asic_type < CHIP_BONAIRE) {
515 adev->doorbell.base = 0;
516 adev->doorbell.size = 0;
517 adev->doorbell.num_doorbells = 0;
518 adev->doorbell.ptr = NULL;
519 return 0;
520 }
521
Christian Königd6895ad2017-02-28 10:36:43 +0100522 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
523 return -EINVAL;
524
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 /* doorbell bar mapping */
526 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
527 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
528
Christian Königedf600d2016-05-03 15:54:54 +0200529 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
531 if (adev->doorbell.num_doorbells == 0)
532 return -EINVAL;
533
Christian König8972e5d2017-03-06 13:34:57 +0100534 adev->doorbell.ptr = ioremap(adev->doorbell.base,
535 adev->doorbell.num_doorbells *
536 sizeof(u32));
537 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400538 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539
540 return 0;
541}
542
543/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500544 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400545 *
546 * @adev: amdgpu_device pointer
547 *
548 * Tear down doorbell driver information (CIK)
549 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500550static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551{
552 iounmap(adev->doorbell.ptr);
553 adev->doorbell.ptr = NULL;
554}
555
Alex Deucher22cb0162017-12-14 16:27:11 -0500556
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557
558/*
Alex Deucher06ec9072017-12-14 15:02:39 -0500559 * amdgpu_device_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400560 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400561 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 */
563
564/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500565 * amdgpu_device_wb_fini - Disable Writeback and free memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400566 *
567 * @adev: amdgpu_device pointer
568 *
569 * Disables Writeback and frees the Writeback memory (all asics).
570 * Used at driver shutdown.
571 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500572static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573{
574 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400575 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
576 &adev->wb.gpu_addr,
577 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578 adev->wb.wb_obj = NULL;
579 }
580}
581
582/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500583 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 *
585 * @adev: amdgpu_device pointer
586 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400587 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 * Used at driver startup.
589 * Returns 0 on success or an -error on failure.
590 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500591static int amdgpu_device_wb_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592{
593 int r;
594
595 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400596 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
597 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400598 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
599 &adev->wb.wb_obj, &adev->wb.gpu_addr,
600 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601 if (r) {
602 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
603 return r;
604 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400605
606 adev->wb.num_wb = AMDGPU_MAX_WB;
607 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
608
609 /* clear wb memory */
Monk Liu73469582017-12-29 17:06:41 +0800610 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611 }
612
613 return 0;
614}
615
616/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500617 * amdgpu_device_wb_get - Allocate a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 *
619 * @adev: amdgpu_device pointer
620 * @wb: wb index
621 *
622 * Allocate a wb slot for use by the driver (all asics).
623 * Returns 0 on success or -EINVAL on failure.
624 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500625int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626{
627 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400628
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629 if (offset < adev->wb.num_wb) {
630 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800631 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400632 return 0;
633 } else {
634 return -EINVAL;
635 }
636}
637
Ken Wang70142852016-03-18 15:08:49 +0800638/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500639 * amdgpu_device_wb_free - Free a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640 *
641 * @adev: amdgpu_device pointer
642 * @wb: wb index
643 *
644 * Free a wb slot allocated for use by the driver (all asics)
645 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500646void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647{
Monk Liu73469582017-12-29 17:06:41 +0800648 wb >>= 3;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 if (wb < adev->wb.num_wb)
Monk Liu73469582017-12-29 17:06:41 +0800650 __clear_bit(wb, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651}
652
653/**
Alex Deucher2543e282017-12-14 16:33:36 -0500654 * amdgpu_device_vram_location - try to find VRAM location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500655 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 * @adev: amdgpu device structure holding all necessary informations
657 * @mc: memory controller structure holding memory informations
658 * @base: base address at which to put VRAM
659 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400660 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100661 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662 */
Alex Deucher2543e282017-12-14 16:33:36 -0500663void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100664 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665{
666 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
667
668 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400669 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
670 if (limit && limit < mc->real_vram_size)
671 mc->real_vram_size = limit;
672 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
673 mc->mc_vram_size >> 20, mc->vram_start,
674 mc->vram_end, mc->real_vram_size >> 20);
675}
676
677/**
Alex Deucher2543e282017-12-14 16:33:36 -0500678 * amdgpu_device_gart_location - try to find GTT location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500679 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 * @adev: amdgpu device structure holding all necessary informations
681 * @mc: memory controller structure holding memory informations
682 *
683 * Function will place try to place GTT before or after VRAM.
684 *
685 * If GTT size is bigger than space left then we ajust GTT size.
686 * Thus function will never fails.
687 *
688 * FIXME: when reducing GTT size align new size on power of 2.
689 */
Alex Deucher2543e282017-12-14 16:33:36 -0500690void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100691 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692{
693 u64 size_af, size_bf;
694
Rex Zhu7951e372018-04-13 16:13:41 +0800695 mc->gart_size += adev->pm.smu_prv_buffer_size;
696
Christian König770d13b2018-01-12 14:52:22 +0100697 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200698 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200700 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200702 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 }
Christian König6f02a692017-07-07 11:56:59 +0200704 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 } else {
Christian König6f02a692017-07-07 11:56:59 +0200706 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200708 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 }
Christian Königb98f1b92017-11-16 20:12:51 +0100710 /* VCE doesn't like it when BOs cross a 4GB segment, so align
711 * the GART base on a 4GB boundary as well.
712 */
713 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400714 }
Christian König6f02a692017-07-07 11:56:59 +0200715 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200717 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400718}
719
Christian Königd6895ad2017-02-28 10:36:43 +0100720/**
721 * amdgpu_device_resize_fb_bar - try to resize FB BAR
722 *
723 * @adev: amdgpu_device pointer
724 *
725 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
726 * to fail, but if any of the BARs is not accessible after the size we abort
727 * driver loading by returning -ENODEV.
728 */
729int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
730{
Christian König770d13b2018-01-12 14:52:22 +0100731 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100732 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100733 struct pci_bus *root;
734 struct resource *res;
735 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100736 u16 cmd;
737 int r;
738
pding0c03b912017-11-07 11:02:00 +0800739 /* Bypass for VF */
740 if (amdgpu_sriov_vf(adev))
741 return 0;
742
Christian König31b8ada2017-11-15 20:07:38 +0100743 /* Check if the root BUS has 64bit memory resources */
744 root = adev->pdev->bus;
745 while (root->parent)
746 root = root->parent;
747
748 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100749 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100750 res->start > 0x100000000ull)
751 break;
752 }
753
754 /* Trying to resize is pointless without a root hub window above 4GB */
755 if (!res)
756 return 0;
757
Christian Königd6895ad2017-02-28 10:36:43 +0100758 /* Disable memory decoding while we change the BAR addresses and size */
759 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
760 pci_write_config_word(adev->pdev, PCI_COMMAND,
761 cmd & ~PCI_COMMAND_MEMORY);
762
763 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500764 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100765 if (adev->asic_type >= CHIP_BONAIRE)
766 pci_release_resource(adev->pdev, 2);
767
768 pci_release_resource(adev->pdev, 0);
769
770 r = pci_resize_resource(adev->pdev, 0, rbar_size);
771 if (r == -ENOSPC)
772 DRM_INFO("Not enough PCI address space for a large BAR.");
773 else if (r && r != -ENOTSUPP)
774 DRM_ERROR("Problem resizing BAR0 (%d).", r);
775
776 pci_assign_unassigned_bus_resources(adev->pdev->bus);
777
778 /* When the doorbell or fb BAR isn't available we have no chance of
779 * using the device.
780 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500781 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100782 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
783 return -ENODEV;
784
785 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
786
787 return 0;
788}
Horace Chena05502e2017-09-29 14:41:57 +0800789
790/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400791 * GPU helpers function.
792 */
793/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500794 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400795 *
796 * @adev: amdgpu_device pointer
797 *
Jim Quc836fec2017-02-10 15:59:59 +0800798 * Check if the asic has been initialized (all asics) at driver startup
799 * or post is needed if hw reset is performed.
800 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500802bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400803{
804 uint32_t reg;
805
Monk Liubec86372016-09-14 19:38:08 +0800806 if (amdgpu_sriov_vf(adev))
807 return false;
808
809 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800810 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
811 * some old smc fw still need driver do vPost otherwise gpu hang, while
812 * those smc fw version above 22.15 doesn't have this flaw, so we force
813 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800814 */
815 if (adev->asic_type == CHIP_FIJI) {
816 int err;
817 uint32_t fw_ver;
818 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
819 /* force vPost if error occured */
820 if (err)
821 return true;
822
823 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800824 if (fw_ver < 0x00160e00)
825 return true;
Monk Liubec86372016-09-14 19:38:08 +0800826 }
Monk Liubec86372016-09-14 19:38:08 +0800827 }
pding91fe77e2017-10-19 09:38:39 +0800828
829 if (adev->has_hw_reset) {
830 adev->has_hw_reset = false;
831 return true;
832 }
833
834 /* bios scratch used on CIK+ */
835 if (adev->asic_type >= CHIP_BONAIRE)
836 return amdgpu_atombios_scratch_need_asic_init(adev);
837
838 /* check MEM_SIZE for older asics */
839 reg = amdgpu_asic_get_config_memsize(adev);
840
841 if ((reg != 0) && (reg != 0xffffffff))
842 return false;
843
844 return true;
Monk Liubec86372016-09-14 19:38:08 +0800845}
846
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400847/* if we get transitioned to only one device, take VGA back */
848/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500849 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400850 *
851 * @cookie: amdgpu_device pointer
852 * @state: enable/disable vga decode
853 *
854 * Enable/disable vga decode (all asics).
855 * Returns VGA resource flags.
856 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500857static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400858{
859 struct amdgpu_device *adev = cookie;
860 amdgpu_asic_set_vga_state(adev, state);
861 if (state)
862 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
863 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
864 else
865 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
866}
867
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500868/**
869 * amdgpu_device_check_block_size - validate the vm block size
870 *
871 * @adev: amdgpu_device pointer
872 *
873 * Validates the vm block size specified via module parameter.
874 * The vm block size defines number of bits in page table versus page directory,
875 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
876 * page table and the remaining bits are in the page directory.
877 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500878static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800879{
880 /* defines number of bits in page table versus page directory,
881 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
882 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800883 if (amdgpu_vm_block_size == -1)
884 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800885
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800886 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800887 dev_warn(adev->dev, "VM page table size (%d) too small\n",
888 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100889 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800890 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800891}
892
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500893/**
894 * amdgpu_device_check_vm_size - validate the vm size
895 *
896 * @adev: amdgpu_device pointer
897 *
898 * Validates the vm size in GB specified via module parameter.
899 * The VM size is the size of the GPU virtual memory space in GB.
900 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500901static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800902{
Alex Deucher64dab072017-06-15 18:20:09 -0400903 /* no need to check the default value */
904 if (amdgpu_vm_size == -1)
905 return;
906
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800907 if (amdgpu_vm_size < 1) {
908 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
909 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100910 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800911 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800912}
913
Rex Zhu7951e372018-04-13 16:13:41 +0800914static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
915{
916 struct sysinfo si;
917 bool is_os_64 = (sizeof(void *) == 8) ? true : false;
918 uint64_t total_memory;
919 uint64_t dram_size_seven_GB = 0x1B8000000;
920 uint64_t dram_size_three_GB = 0xB8000000;
921
922 if (amdgpu_smu_memory_pool_size == 0)
923 return;
924
925 if (!is_os_64) {
926 DRM_WARN("Not 64-bit OS, feature not supported\n");
927 goto def_value;
928 }
929 si_meminfo(&si);
930 total_memory = (uint64_t)si.totalram * si.mem_unit;
931
932 if ((amdgpu_smu_memory_pool_size == 1) ||
933 (amdgpu_smu_memory_pool_size == 2)) {
934 if (total_memory < dram_size_three_GB)
935 goto def_value1;
936 } else if ((amdgpu_smu_memory_pool_size == 4) ||
937 (amdgpu_smu_memory_pool_size == 8)) {
938 if (total_memory < dram_size_seven_GB)
939 goto def_value1;
940 } else {
941 DRM_WARN("Smu memory pool size not supported\n");
942 goto def_value;
943 }
944 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
945
946 return;
947
948def_value1:
949 DRM_WARN("No enough system memory\n");
950def_value:
951 adev->pm.smu_prv_buffer_size = 0;
952}
953
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500955 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956 *
957 * @adev: amdgpu_device pointer
958 *
959 * Validates certain module parameters and updates
960 * the associated values used by the driver (all asics).
961 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500962static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963{
Chunming Zhou5b011232015-12-10 17:34:33 +0800964 if (amdgpu_sched_jobs < 4) {
965 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
966 amdgpu_sched_jobs);
967 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400968 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800969 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
970 amdgpu_sched_jobs);
971 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
972 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973
Alex Deucher83e74db2017-08-21 11:58:25 -0400974 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200975 /* gart size must be greater or equal to 32M */
976 dev_warn(adev->dev, "gart size (%d) too small\n",
977 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400978 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 }
980
Christian König36d38372017-07-07 13:17:45 +0200981 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200983 dev_warn(adev->dev, "gtt size (%d) too small\n",
984 amdgpu_gtt_size);
985 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 }
987
Roger Hed07f14b2017-08-15 16:05:59 +0800988 /* valid range is between 4 and 9 inclusive */
989 if (amdgpu_vm_fragment_size != -1 &&
990 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
991 dev_warn(adev->dev, "valid range is between 4 and 9\n");
992 amdgpu_vm_fragment_size = -1;
993 }
994
Rex Zhu7951e372018-04-13 16:13:41 +0800995 amdgpu_device_check_smu_prv_buffer_size(adev);
996
Alex Deucher06ec9072017-12-14 15:02:39 -0500997 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400998
Alex Deucher06ec9072017-12-14 15:02:39 -0500999 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001000
jimqu526bae32016-11-07 09:53:10 +08001001 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001002 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001003 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1004 amdgpu_vram_page_split);
1005 amdgpu_vram_page_split = 1024;
1006 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -05001007
1008 if (amdgpu_lockup_timeout == 0) {
1009 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
1010 amdgpu_lockup_timeout = 10000;
1011 }
Alex Deucher19aede72018-03-09 15:06:35 -05001012
1013 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001014}
1015
1016/**
1017 * amdgpu_switcheroo_set_state - set switcheroo state
1018 *
1019 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001020 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001021 *
1022 * Callback for the switcheroo driver. Suspends or resumes the
1023 * the asics before or after it is powered up using ACPI methods.
1024 */
1025static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1026{
1027 struct drm_device *dev = pci_get_drvdata(pdev);
1028
1029 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1030 return;
1031
1032 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001033 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001034 /* don't suspend or resume card normally */
1035 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1036
Alex Deucher810ddc32016-08-23 13:25:49 -04001037 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001038
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001039 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1040 drm_kms_helper_poll_enable(dev);
1041 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001042 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001043 drm_kms_helper_poll_disable(dev);
1044 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001045 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1047 }
1048}
1049
1050/**
1051 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1052 *
1053 * @pdev: pci dev pointer
1054 *
1055 * Callback for the switcheroo driver. Check of the switcheroo
1056 * state can be changed.
1057 * Returns true if the state can be changed, false if not.
1058 */
1059static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1060{
1061 struct drm_device *dev = pci_get_drvdata(pdev);
1062
1063 /*
1064 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1065 * locking inversion with the driver load path. And the access here is
1066 * completely racy anyway. So don't bother with locking for now.
1067 */
1068 return dev->open_count == 0;
1069}
1070
1071static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1072 .set_gpu_state = amdgpu_switcheroo_set_state,
1073 .reprobe = NULL,
1074 .can_switch = amdgpu_switcheroo_can_switch,
1075};
1076
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001077/**
1078 * amdgpu_device_ip_set_clockgating_state - set the CG state
1079 *
1080 * @adev: amdgpu_device pointer
1081 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1082 * @state: clockgating state (gate or ungate)
1083 *
1084 * Sets the requested clockgating state for all instances of
1085 * the hardware IP specified.
1086 * Returns the error code from the last instance.
1087 */
Rex Zhu43fa5612018-03-28 13:42:45 -05001088int amdgpu_device_ip_set_clockgating_state(void *dev,
Alex Deucher2990a1f2017-12-15 16:18:00 -05001089 enum amd_ip_block_type block_type,
1090 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091{
Rex Zhu43fa5612018-03-28 13:42:45 -05001092 struct amdgpu_device *adev = dev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001093 int i, r = 0;
1094
1095 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001096 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001097 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001098 if (adev->ip_blocks[i].version->type != block_type)
1099 continue;
1100 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1101 continue;
1102 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1103 (void *)adev, state);
1104 if (r)
1105 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1106 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 }
1108 return r;
1109}
1110
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001111/**
1112 * amdgpu_device_ip_set_powergating_state - set the PG state
1113 *
1114 * @adev: amdgpu_device pointer
1115 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1116 * @state: powergating state (gate or ungate)
1117 *
1118 * Sets the requested powergating state for all instances of
1119 * the hardware IP specified.
1120 * Returns the error code from the last instance.
1121 */
Rex Zhu43fa5612018-03-28 13:42:45 -05001122int amdgpu_device_ip_set_powergating_state(void *dev,
Alex Deucher2990a1f2017-12-15 16:18:00 -05001123 enum amd_ip_block_type block_type,
1124 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125{
Rex Zhu43fa5612018-03-28 13:42:45 -05001126 struct amdgpu_device *adev = dev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001127 int i, r = 0;
1128
1129 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001130 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001131 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001132 if (adev->ip_blocks[i].version->type != block_type)
1133 continue;
1134 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1135 continue;
1136 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1137 (void *)adev, state);
1138 if (r)
1139 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1140 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001141 }
1142 return r;
1143}
1144
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001145/**
1146 * amdgpu_device_ip_get_clockgating_state - get the CG state
1147 *
1148 * @adev: amdgpu_device pointer
1149 * @flags: clockgating feature flags
1150 *
1151 * Walks the list of IPs on the device and updates the clockgating
1152 * flags for each IP.
1153 * Updates @flags with the feature flags for each hardware IP where
1154 * clockgating is enabled.
1155 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001156void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1157 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001158{
1159 int i;
1160
1161 for (i = 0; i < adev->num_ip_blocks; i++) {
1162 if (!adev->ip_blocks[i].status.valid)
1163 continue;
1164 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1165 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1166 }
1167}
1168
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001169/**
1170 * amdgpu_device_ip_wait_for_idle - wait for idle
1171 *
1172 * @adev: amdgpu_device pointer
1173 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1174 *
1175 * Waits for the request hardware IP to be idle.
1176 * Returns 0 for success or a negative error code on failure.
1177 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001178int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1179 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001180{
1181 int i, r;
1182
1183 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001184 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001185 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001186 if (adev->ip_blocks[i].version->type == block_type) {
1187 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001188 if (r)
1189 return r;
1190 break;
1191 }
1192 }
1193 return 0;
1194
1195}
1196
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001197/**
1198 * amdgpu_device_ip_is_idle - is the hardware IP idle
1199 *
1200 * @adev: amdgpu_device pointer
1201 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1202 *
1203 * Check if the hardware IP is idle or not.
1204 * Returns true if it the IP is idle, false if not.
1205 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001206bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1207 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001208{
1209 int i;
1210
1211 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001212 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001213 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001214 if (adev->ip_blocks[i].version->type == block_type)
1215 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001216 }
1217 return true;
1218
1219}
1220
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001221/**
1222 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1223 *
1224 * @adev: amdgpu_device pointer
1225 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1226 *
1227 * Returns a pointer to the hardware IP block structure
1228 * if it exists for the asic, otherwise NULL.
1229 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001230struct amdgpu_ip_block *
1231amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1232 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233{
1234 int i;
1235
1236 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001237 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001238 return &adev->ip_blocks[i];
1239
1240 return NULL;
1241}
1242
1243/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001244 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001245 *
1246 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001247 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001248 * @major: major version
1249 * @minor: minor version
1250 *
1251 * return 0 if equal or greater
1252 * return 1 if smaller or the ip_block doesn't exist
1253 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001254int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1255 enum amd_ip_block_type type,
1256 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001258 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259
Alex Deuchera1255102016-10-13 17:41:13 -04001260 if (ip_block && ((ip_block->version->major > major) ||
1261 ((ip_block->version->major == major) &&
1262 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263 return 0;
1264
1265 return 1;
1266}
1267
Alex Deuchera1255102016-10-13 17:41:13 -04001268/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001269 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001270 *
1271 * @adev: amdgpu_device pointer
1272 * @ip_block_version: pointer to the IP to add
1273 *
1274 * Adds the IP block driver information to the collection of IPs
1275 * on the asic.
1276 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001277int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1278 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001279{
1280 if (!ip_block_version)
1281 return -EINVAL;
1282
Shaoyun Liue966a722018-02-01 16:45:26 -05001283 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001284 ip_block_version->funcs->name);
1285
Alex Deuchera1255102016-10-13 17:41:13 -04001286 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1287
1288 return 0;
1289}
1290
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001291/**
1292 * amdgpu_device_enable_virtual_display - enable virtual display feature
1293 *
1294 * @adev: amdgpu_device pointer
1295 *
1296 * Enabled the virtual display feature if the user has enabled it via
1297 * the module parameter virtual_display. This feature provides a virtual
1298 * display hardware on headless boards or in virtualized environments.
1299 * This function parses and validates the configuration string specified by
1300 * the user and configues the virtual display configuration (number of
1301 * virtual connectors, crtcs, etc.) specified.
1302 */
Alex Deucher483ef982016-09-30 12:43:04 -04001303static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001304{
1305 adev->enable_virtual_display = false;
1306
1307 if (amdgpu_virtual_display) {
1308 struct drm_device *ddev = adev->ddev;
1309 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001310 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001311
1312 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1313 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001314 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1315 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001316 if (!strcmp("all", pciaddname)
1317 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001318 long num_crtc;
1319 int res = -1;
1320
Emily Deng9accf2f2016-08-10 16:01:25 +08001321 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001322
1323 if (pciaddname_tmp)
1324 res = kstrtol(pciaddname_tmp, 10,
1325 &num_crtc);
1326
1327 if (!res) {
1328 if (num_crtc < 1)
1329 num_crtc = 1;
1330 if (num_crtc > 6)
1331 num_crtc = 6;
1332 adev->mode_info.num_crtc = num_crtc;
1333 } else {
1334 adev->mode_info.num_crtc = 1;
1335 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001336 break;
1337 }
1338 }
1339
Emily Deng0f663562016-09-30 13:02:18 -04001340 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1341 amdgpu_virtual_display, pci_address_name,
1342 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001343
1344 kfree(pciaddstr);
1345 }
1346}
1347
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001348/**
1349 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1350 *
1351 * @adev: amdgpu_device pointer
1352 *
1353 * Parses the asic configuration parameters specified in the gpu info
1354 * firmware and makes them availale to the driver for use in configuring
1355 * the asic.
1356 * Returns 0 on success, -EINVAL on failure.
1357 */
Alex Deuchere2a75f82017-04-27 16:58:01 -04001358static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1359{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001360 const char *chip_name;
1361 char fw_name[30];
1362 int err;
1363 const struct gpu_info_firmware_header_v1_0 *hdr;
1364
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001365 adev->firmware.gpu_info_fw = NULL;
1366
Alex Deuchere2a75f82017-04-27 16:58:01 -04001367 switch (adev->asic_type) {
1368 case CHIP_TOPAZ:
1369 case CHIP_TONGA:
1370 case CHIP_FIJI:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001371 case CHIP_POLARIS10:
Leo Liucc07f182017-11-09 13:19:58 -05001372 case CHIP_POLARIS11:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001373 case CHIP_POLARIS12:
Leo Liucc07f182017-11-09 13:19:58 -05001374 case CHIP_VEGAM:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001375 case CHIP_CARRIZO:
1376 case CHIP_STONEY:
1377#ifdef CONFIG_DRM_AMDGPU_SI
1378 case CHIP_VERDE:
1379 case CHIP_TAHITI:
1380 case CHIP_PITCAIRN:
1381 case CHIP_OLAND:
1382 case CHIP_HAINAN:
1383#endif
1384#ifdef CONFIG_DRM_AMDGPU_CIK
1385 case CHIP_BONAIRE:
1386 case CHIP_HAWAII:
1387 case CHIP_KAVERI:
1388 case CHIP_KABINI:
1389 case CHIP_MULLINS:
1390#endif
Feifei Xu27c0bc72018-05-17 10:01:19 -05001391 case CHIP_VEGA20:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001392 default:
1393 return 0;
1394 case CHIP_VEGA10:
1395 chip_name = "vega10";
1396 break;
Alex Deucher3f76dce2017-09-01 16:20:53 -04001397 case CHIP_VEGA12:
1398 chip_name = "vega12";
1399 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001400 case CHIP_RAVEN:
1401 chip_name = "raven";
1402 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001403 }
1404
1405 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001406 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001407 if (err) {
1408 dev_err(adev->dev,
1409 "Failed to load gpu_info firmware \"%s\"\n",
1410 fw_name);
1411 goto out;
1412 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001413 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001414 if (err) {
1415 dev_err(adev->dev,
1416 "Failed to validate gpu_info firmware \"%s\"\n",
1417 fw_name);
1418 goto out;
1419 }
1420
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001421 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001422 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1423
1424 switch (hdr->version_major) {
1425 case 1:
1426 {
1427 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001428 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001429 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1430
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001431 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1432 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1433 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1434 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 -04001435 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001436 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1437 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1438 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1439 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1440 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001441 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001442 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1443 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001444 adev->gfx.cu_info.max_waves_per_simd =
1445 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1446 adev->gfx.cu_info.max_scratch_slots_per_cu =
1447 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1448 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001449 break;
1450 }
1451 default:
1452 dev_err(adev->dev,
1453 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1454 err = -EINVAL;
1455 goto out;
1456 }
1457out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001458 return err;
1459}
1460
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001461/**
1462 * amdgpu_device_ip_early_init - run early init for hardware IPs
1463 *
1464 * @adev: amdgpu_device pointer
1465 *
1466 * Early initialization pass for hardware IPs. The hardware IPs that make
1467 * up each asic are discovered each IP's early_init callback is run. This
1468 * is the first stage in initializing the asic.
1469 * Returns 0 on success, negative error code on failure.
1470 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001471static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001472{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001473 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001474
Alex Deucher483ef982016-09-30 12:43:04 -04001475 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001476
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001477 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001478 case CHIP_TOPAZ:
1479 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001480 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001481 case CHIP_POLARIS10:
Leo Liu32cc7e52017-11-09 13:22:54 -05001482 case CHIP_POLARIS11:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001483 case CHIP_POLARIS12:
Leo Liu32cc7e52017-11-09 13:22:54 -05001484 case CHIP_VEGAM:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001485 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001486 case CHIP_STONEY:
1487 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001488 adev->family = AMDGPU_FAMILY_CZ;
1489 else
1490 adev->family = AMDGPU_FAMILY_VI;
1491
1492 r = vi_set_ip_blocks(adev);
1493 if (r)
1494 return r;
1495 break;
Ken Wang33f34802016-01-21 17:29:41 +08001496#ifdef CONFIG_DRM_AMDGPU_SI
1497 case CHIP_VERDE:
1498 case CHIP_TAHITI:
1499 case CHIP_PITCAIRN:
1500 case CHIP_OLAND:
1501 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001502 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001503 r = si_set_ip_blocks(adev);
1504 if (r)
1505 return r;
1506 break;
1507#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001508#ifdef CONFIG_DRM_AMDGPU_CIK
1509 case CHIP_BONAIRE:
1510 case CHIP_HAWAII:
1511 case CHIP_KAVERI:
1512 case CHIP_KABINI:
1513 case CHIP_MULLINS:
1514 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1515 adev->family = AMDGPU_FAMILY_CI;
1516 else
1517 adev->family = AMDGPU_FAMILY_KV;
1518
1519 r = cik_set_ip_blocks(adev);
1520 if (r)
1521 return r;
1522 break;
1523#endif
Alex Deuchere48a3cd2017-09-01 16:22:35 -04001524 case CHIP_VEGA10:
1525 case CHIP_VEGA12:
Feifei Xue4bd8172018-04-20 12:33:33 +08001526 case CHIP_VEGA20:
Alex Deuchere48a3cd2017-09-01 16:22:35 -04001527 case CHIP_RAVEN:
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001528 if (adev->asic_type == CHIP_RAVEN)
1529 adev->family = AMDGPU_FAMILY_RV;
1530 else
1531 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001532
1533 r = soc15_set_ip_blocks(adev);
1534 if (r)
1535 return r;
1536 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537 default:
1538 /* FIXME: not supported yet */
1539 return -EINVAL;
1540 }
1541
Alex Deuchere2a75f82017-04-27 16:58:01 -04001542 r = amdgpu_device_parse_gpu_info_fw(adev);
1543 if (r)
1544 return r;
1545
pding18847342017-11-06 10:21:26 +08001546 amdgpu_amdkfd_device_probe(adev);
1547
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001548 if (amdgpu_sriov_vf(adev)) {
1549 r = amdgpu_virt_request_full_gpu(adev, true);
1550 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001551 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001552 }
1553
Huang Rui00f54b92018-02-27 21:53:00 +08001554 adev->powerplay.pp_feature = amdgpu_pp_feature_mask;
1555
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 for (i = 0; i < adev->num_ip_blocks; i++) {
1557 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001558 DRM_ERROR("disabled ip block: %d <%s>\n",
1559 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001560 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001561 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001562 if (adev->ip_blocks[i].version->funcs->early_init) {
1563 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001564 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001565 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001566 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001567 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1568 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001570 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001571 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001572 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001573 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001574 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001575 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001576 }
1577 }
1578
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001579 adev->cg_flags &= amdgpu_cg_mask;
1580 adev->pg_flags &= amdgpu_pg_mask;
1581
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001582 return 0;
1583}
1584
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001585/**
1586 * amdgpu_device_ip_init - run init for hardware IPs
1587 *
1588 * @adev: amdgpu_device pointer
1589 *
1590 * Main initialization pass for hardware IPs. The list of all the hardware
1591 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1592 * are run. sw_init initializes the software state associated with each IP
1593 * and hw_init initializes the hardware associated with each IP.
1594 * Returns 0 on success, negative error code on failure.
1595 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001596static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001597{
1598 int i, r;
1599
1600 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001601 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001603 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001604 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001605 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1606 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001608 }
Alex Deuchera1255102016-10-13 17:41:13 -04001609 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001610
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001612 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001613 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001614 if (r) {
1615 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001617 }
Alex Deuchera1255102016-10-13 17:41:13 -04001618 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001619 if (r) {
1620 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001622 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001623 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001624 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001625 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001626 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001627 }
Alex Deuchera1255102016-10-13 17:41:13 -04001628 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001629
1630 /* right after GMC hw init, we create CSA */
1631 if (amdgpu_sriov_vf(adev)) {
1632 r = amdgpu_allocate_static_csa(adev);
1633 if (r) {
1634 DRM_ERROR("allocate CSA failed %d\n", r);
1635 return r;
1636 }
1637 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638 }
1639 }
1640
1641 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001642 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001644 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001646 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001647 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001648 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1649 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001651 }
Alex Deuchera1255102016-10-13 17:41:13 -04001652 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001653 }
1654
pding18847342017-11-06 10:21:26 +08001655 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001656
1657 if (amdgpu_sriov_vf(adev))
1658 amdgpu_virt_release_full_gpu(adev, true);
1659
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001660 return 0;
1661}
1662
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001663/**
1664 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1665 *
1666 * @adev: amdgpu_device pointer
1667 *
1668 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1669 * this function before a GPU reset. If the value is retained after a
1670 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1671 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001672static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001673{
1674 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1675}
1676
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001677/**
1678 * amdgpu_device_check_vram_lost - check if vram is valid
1679 *
1680 * @adev: amdgpu_device pointer
1681 *
1682 * Checks the reset magic value written to the gart pointer in VRAM.
1683 * The driver calls this after a GPU reset to see if the contents of
1684 * VRAM is lost or now.
1685 * returns true if vram is lost, false if not.
1686 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001687static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001688{
1689 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1690 AMDGPU_RESET_MAGIC_NUM);
1691}
1692
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001693/**
1694 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1695 *
1696 * @adev: amdgpu_device pointer
1697 *
1698 * Late initialization pass enabling clockgating for hardware IPs.
1699 * The list of all the hardware IPs that make up the asic is walked and the
1700 * set_clockgating_state callbacks are run. This stage is run late
1701 * in the init process.
1702 * Returns 0 on success, negative error code on failure.
1703 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001704static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301705{
1706 int i = 0, r;
1707
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001708 if (amdgpu_emu_mode == 1)
1709 return 0;
1710
Shirish S2c773de2018-04-16 12:17:57 +05301711 r = amdgpu_ib_ring_tests(adev);
1712 if (r)
1713 DRM_ERROR("ib ring test failed (%d).\n", r);
1714
Shirish S2dc80b02017-05-25 10:05:25 +05301715 for (i = 0; i < adev->num_ip_blocks; i++) {
1716 if (!adev->ip_blocks[i].status.valid)
1717 continue;
1718 /* skip CG for VCE/UVD, it's handled specially */
1719 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu57716322018-03-12 19:50:38 +08001720 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1721 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Shirish S2dc80b02017-05-25 10:05:25 +05301722 /* enable clockgating to save power */
1723 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1724 AMD_CG_STATE_GATE);
1725 if (r) {
1726 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1727 adev->ip_blocks[i].version->funcs->name, r);
1728 return r;
1729 }
1730 }
1731 }
1732 return 0;
1733}
1734
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001735/**
1736 * amdgpu_device_ip_late_init - run late init for hardware IPs
1737 *
1738 * @adev: amdgpu_device pointer
1739 *
1740 * Late initialization pass for hardware IPs. The list of all the hardware
1741 * IPs that make up the asic is walked and the late_init callbacks are run.
1742 * late_init covers any special initialization that an IP requires
1743 * after all of the have been initialized or something that needs to happen
1744 * late in the init process.
1745 * Returns 0 on success, negative error code on failure.
1746 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001747static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001748{
1749 int i = 0, r;
1750
1751 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001752 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001753 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001754 if (adev->ip_blocks[i].version->funcs->late_init) {
1755 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001756 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001757 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1758 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001759 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001760 }
Alex Deuchera1255102016-10-13 17:41:13 -04001761 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001762 }
1763 }
1764
Shirish S2c773de2018-04-16 12:17:57 +05301765 queue_delayed_work(system_wq, &adev->late_init_work,
1766 msecs_to_jiffies(AMDGPU_RESUME_MS));
Shirish S2dc80b02017-05-25 10:05:25 +05301767
Alex Deucher06ec9072017-12-14 15:02:39 -05001768 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001769
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001770 return 0;
1771}
1772
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001773/**
1774 * amdgpu_device_ip_fini - run fini for hardware IPs
1775 *
1776 * @adev: amdgpu_device pointer
1777 *
1778 * Main teardown pass for hardware IPs. The list of all the hardware
1779 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1780 * are run. hw_fini tears down the hardware associated with each IP
1781 * and sw_fini tears down any software state associated with each IP.
1782 * Returns 0 on success, negative error code on failure.
1783 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001784static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785{
1786 int i, r;
1787
pding18847342017-11-06 10:21:26 +08001788 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001789 /* need to disable SMC first */
1790 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001791 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001792 continue;
Rex Zhu57716322018-03-12 19:50:38 +08001793 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1794 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001795 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001796 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1797 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001798 if (r) {
1799 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001800 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001801 return r;
1802 }
Alex Deuchera1255102016-10-13 17:41:13 -04001803 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001804 /* XXX handle errors */
1805 if (r) {
1806 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001807 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001808 }
Alex Deuchera1255102016-10-13 17:41:13 -04001809 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001810 break;
1811 }
1812 }
1813
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001814 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001815 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001816 continue;
Rex Zhu8201a672016-11-24 21:44:44 +08001817
1818 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu81ce8be2018-03-20 16:28:56 +08001819 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1820 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Rex Zhu8201a672016-11-24 21:44:44 +08001821 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1822 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1823 AMD_CG_STATE_UNGATE);
1824 if (r) {
1825 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1826 adev->ip_blocks[i].version->funcs->name, r);
1827 return r;
1828 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001829 }
Rex Zhu8201a672016-11-24 21:44:44 +08001830
Alex Deuchera1255102016-10-13 17:41:13 -04001831 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001832 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001833 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001834 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1835 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001836 }
Rex Zhu8201a672016-11-24 21:44:44 +08001837
Alex Deuchera1255102016-10-13 17:41:13 -04001838 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001839 }
1840
Alex Deucher9950cda2018-01-18 19:05:36 -05001841
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001842 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001843 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001844 continue;
Monk Liuc12aba32018-01-24 12:20:32 +08001845
1846 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1847 amdgpu_free_static_csa(adev);
1848 amdgpu_device_wb_fini(adev);
1849 amdgpu_device_vram_scratch_fini(adev);
1850 }
1851
Alex Deuchera1255102016-10-13 17:41:13 -04001852 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001853 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001854 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001855 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1856 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001857 }
Alex Deuchera1255102016-10-13 17:41:13 -04001858 adev->ip_blocks[i].status.sw = false;
1859 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001860 }
1861
Monk Liua6dcfd92016-05-19 14:36:34 +08001862 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001863 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001864 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001865 if (adev->ip_blocks[i].version->funcs->late_fini)
1866 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1867 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001868 }
1869
Monk Liu030308f2017-09-15 15:34:52 +08001870 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001871 if (amdgpu_virt_release_full_gpu(adev, false))
1872 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001873
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001874 return 0;
1875}
1876
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001877/**
1878 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1879 *
1880 * @work: work_struct
1881 *
1882 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1883 * clockgating setup into a worker thread to speed up driver init and
1884 * resume from suspend.
1885 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001886static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301887{
1888 struct amdgpu_device *adev =
1889 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001890 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301891}
1892
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001893/**
1894 * amdgpu_device_ip_suspend - run suspend for hardware IPs
1895 *
1896 * @adev: amdgpu_device pointer
1897 *
1898 * Main suspend function for hardware IPs. The list of all the hardware
1899 * IPs that make up the asic is walked, clockgating is disabled and the
1900 * suspend callbacks are run. suspend puts the hardware and software state
1901 * in each IP into a state suitable for suspend.
1902 * Returns 0 on success, negative error code on failure.
1903 */
Alex Deuchercdd61df2017-12-14 16:47:40 -05001904int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905{
1906 int i, r;
1907
Xiangliang Yue941ea92017-01-18 12:47:55 +08001908 if (amdgpu_sriov_vf(adev))
1909 amdgpu_virt_request_full_gpu(adev, false);
1910
Huang Ruib0833692018-03-13 18:39:48 +08001911 /* ungate SMC block powergating */
1912 if (adev->powerplay.pp_feature & PP_GFXOFF_MASK)
1913 amdgpu_device_ip_set_powergating_state(adev,
1914 AMD_IP_BLOCK_TYPE_SMC,
1915 AMD_CG_STATE_UNGATE);
1916
Flora Cuic5a93a22016-02-26 10:45:25 +08001917 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001918 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1919 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001920 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001921 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001922 }
1923
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001924 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001925 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001926 continue;
1927 /* ungate blocks so that suspend can properly shut them down */
Rex Zhu5b2a3d22018-03-14 15:38:48 +08001928 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
Rex Zhu57716322018-03-12 19:50:38 +08001929 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deuchera1255102016-10-13 17:41:13 -04001930 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1931 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001932 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001933 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1934 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001935 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001936 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001937 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001938 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001940 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001941 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1942 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001943 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001944 }
1945
Xiangliang Yue941ea92017-01-18 12:47:55 +08001946 if (amdgpu_sriov_vf(adev))
1947 amdgpu_virt_release_full_gpu(adev, false);
1948
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001949 return 0;
1950}
1951
Alex Deucher06ec9072017-12-14 15:02:39 -05001952static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001953{
1954 int i, r;
1955
Monk Liu2cb681b2017-04-26 12:00:49 +08001956 static enum amd_ip_block_type ip_order[] = {
1957 AMD_IP_BLOCK_TYPE_GMC,
1958 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001959 AMD_IP_BLOCK_TYPE_IH,
1960 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001961
Monk Liu2cb681b2017-04-26 12:00:49 +08001962 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1963 int j;
1964 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001965
Monk Liu2cb681b2017-04-26 12:00:49 +08001966 for (j = 0; j < adev->num_ip_blocks; j++) {
1967 block = &adev->ip_blocks[j];
1968
1969 if (block->version->type != ip_order[i] ||
1970 !block->status.valid)
1971 continue;
1972
1973 r = block->version->funcs->hw_init(adev);
1974 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001975 if (r)
1976 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001977 }
1978 }
1979
1980 return 0;
1981}
1982
Alex Deucher06ec9072017-12-14 15:02:39 -05001983static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001984{
1985 int i, r;
1986
Monk Liu2cb681b2017-04-26 12:00:49 +08001987 static enum amd_ip_block_type ip_order[] = {
1988 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001989 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001990 AMD_IP_BLOCK_TYPE_DCE,
1991 AMD_IP_BLOCK_TYPE_GFX,
1992 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001993 AMD_IP_BLOCK_TYPE_UVD,
1994 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001995 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001996
Monk Liu2cb681b2017-04-26 12:00:49 +08001997 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1998 int j;
1999 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08002000
Monk Liu2cb681b2017-04-26 12:00:49 +08002001 for (j = 0; j < adev->num_ip_blocks; j++) {
2002 block = &adev->ip_blocks[j];
2003
2004 if (block->version->type != ip_order[i] ||
2005 !block->status.valid)
2006 continue;
2007
2008 r = block->version->funcs->hw_init(adev);
2009 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08002010 if (r)
2011 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08002012 }
2013 }
2014
2015 return 0;
2016}
2017
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002018/**
2019 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2020 *
2021 * @adev: amdgpu_device pointer
2022 *
2023 * First resume function for hardware IPs. The list of all the hardware
2024 * IPs that make up the asic is walked and the resume callbacks are run for
2025 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2026 * after a suspend and updates the software state as necessary. This
2027 * function is also used for restoring the GPU after a GPU reset.
2028 * Returns 0 on success, negative error code on failure.
2029 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002030static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031{
2032 int i, r;
2033
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002034 for (i = 0; i < adev->num_ip_blocks; i++) {
2035 if (!adev->ip_blocks[i].status.valid)
2036 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08002037 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002038 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2039 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
Chunming Zhoufcf06492017-05-05 10:33:33 +08002040 r = adev->ip_blocks[i].version->funcs->resume(adev);
2041 if (r) {
2042 DRM_ERROR("resume of IP block <%s> failed %d\n",
2043 adev->ip_blocks[i].version->funcs->name, r);
2044 return r;
2045 }
2046 }
2047 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048
Chunming Zhoufcf06492017-05-05 10:33:33 +08002049 return 0;
2050}
2051
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002052/**
2053 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2054 *
2055 * @adev: amdgpu_device pointer
2056 *
2057 * First resume function for hardware IPs. The list of all the hardware
2058 * IPs that make up the asic is walked and the resume callbacks are run for
2059 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2060 * functional state after a suspend and updates the software state as
2061 * necessary. This function is also used for restoring the GPU after a GPU
2062 * reset.
2063 * Returns 0 on success, negative error code on failure.
2064 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002065static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002066{
2067 int i, r;
2068
2069 for (i = 0; i < adev->num_ip_blocks; i++) {
2070 if (!adev->ip_blocks[i].status.valid)
2071 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002072 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002073 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2074 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002076 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077 if (r) {
2078 DRM_ERROR("resume of IP block <%s> failed %d\n",
2079 adev->ip_blocks[i].version->funcs->name, r);
2080 return r;
2081 }
2082 }
2083
2084 return 0;
2085}
2086
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002087/**
2088 * amdgpu_device_ip_resume - run resume for hardware IPs
2089 *
2090 * @adev: amdgpu_device pointer
2091 *
2092 * Main resume function for hardware IPs. The hardware IPs
2093 * are split into two resume functions because they are
2094 * are also used in in recovering from a GPU reset and some additional
2095 * steps need to be take between them. In this case (S3/S4) they are
2096 * run sequentially.
2097 * Returns 0 on success, negative error code on failure.
2098 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002099static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002100{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002101 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002102
Alex Deucher06ec9072017-12-14 15:02:39 -05002103 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002104 if (r)
2105 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05002106 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002107
Chunming Zhoufcf06492017-05-05 10:33:33 +08002108 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002109}
2110
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002111/**
2112 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2113 *
2114 * @adev: amdgpu_device pointer
2115 *
2116 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2117 */
Monk Liu4e99a442016-03-31 13:26:59 +08002118static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002119{
Monk Liu6867e1b2017-10-16 19:50:44 +08002120 if (amdgpu_sriov_vf(adev)) {
2121 if (adev->is_atom_fw) {
2122 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2123 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2124 } else {
2125 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2126 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2127 }
2128
2129 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2130 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002131 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002132}
2133
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002134/**
2135 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2136 *
2137 * @asic_type: AMD asic type
2138 *
2139 * Check if there is DC (new modesetting infrastructre) support for an asic.
2140 * returns true if DC has support, false if not.
2141 */
Harry Wentland45622362017-09-12 15:58:20 -04002142bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2143{
2144 switch (asic_type) {
2145#if defined(CONFIG_DRM_AMD_DC)
2146 case CHIP_BONAIRE:
2147 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002148 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05002149 case CHIP_KABINI:
2150 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04002151 case CHIP_CARRIZO:
2152 case CHIP_STONEY:
Harry Wentland45622362017-09-12 15:58:20 -04002153 case CHIP_POLARIS10:
Leo Liu675fd322017-11-08 18:07:12 -05002154 case CHIP_POLARIS11:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002155 case CHIP_POLARIS12:
Leo Liu675fd322017-11-08 18:07:12 -05002156 case CHIP_VEGAM:
Harry Wentland45622362017-09-12 15:58:20 -04002157 case CHIP_TONGA:
2158 case CHIP_FIJI:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002159 case CHIP_VEGA10:
Alex Deucherdca7b402017-09-02 02:05:29 -04002160 case CHIP_VEGA12:
Feifei Xuc6034aa2018-02-03 12:19:46 +08002161 case CHIP_VEGA20:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002162#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002163 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002164#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002165 return amdgpu_dc != 0;
2166#endif
Harry Wentland45622362017-09-12 15:58:20 -04002167 default:
2168 return false;
2169 }
2170}
2171
2172/**
2173 * amdgpu_device_has_dc_support - check if dc is supported
2174 *
2175 * @adev: amdgpu_device_pointer
2176 *
2177 * Returns true for supported, false for not supported
2178 */
2179bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2180{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002181 if (amdgpu_sriov_vf(adev))
2182 return false;
2183
Harry Wentland45622362017-09-12 15:58:20 -04002184 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2185}
2186
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187/**
2188 * amdgpu_device_init - initialize the driver
2189 *
2190 * @adev: amdgpu_device pointer
2191 * @pdev: drm dev pointer
2192 * @pdev: pci dev pointer
2193 * @flags: driver flags
2194 *
2195 * Initializes the driver info and hw (all asics).
2196 * Returns 0 for success or an error on failure.
2197 * Called at driver startup.
2198 */
2199int amdgpu_device_init(struct amdgpu_device *adev,
2200 struct drm_device *ddev,
2201 struct pci_dev *pdev,
2202 uint32_t flags)
2203{
2204 int r, i;
2205 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002206 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207
2208 adev->shutdown = false;
2209 adev->dev = &pdev->dev;
2210 adev->ddev = ddev;
2211 adev->pdev = pdev;
2212 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002213 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05002215 if (amdgpu_emu_mode == 1)
2216 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01002217 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002218 adev->accel_working = false;
2219 adev->num_rings = 0;
2220 adev->mman.buffer_funcs = NULL;
2221 adev->mman.buffer_funcs_ring = NULL;
2222 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002223 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01002224 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002225 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002226 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227
2228 adev->smc_rreg = &amdgpu_invalid_rreg;
2229 adev->smc_wreg = &amdgpu_invalid_wreg;
2230 adev->pcie_rreg = &amdgpu_invalid_rreg;
2231 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002232 adev->pciep_rreg = &amdgpu_invalid_rreg;
2233 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002234 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2235 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2236 adev->didt_rreg = &amdgpu_invalid_rreg;
2237 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002238 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2239 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2241 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2242
Alex Deucher3e39ab92015-06-05 15:04:33 -04002243 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2244 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2245 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002246
2247 /* mutex initialization are all done here so we
2248 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002249 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002250 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251 mutex_init(&adev->pm.mutex);
2252 mutex_init(&adev->gfx.gpu_clock_mutex);
2253 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002254 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002255 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002257 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002258 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002259 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002260
Alex Deucher06ec9072017-12-14 15:02:39 -05002261 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002262
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002263 spin_lock_init(&adev->mmio_idx_lock);
2264 spin_lock_init(&adev->smc_idx_lock);
2265 spin_lock_init(&adev->pcie_idx_lock);
2266 spin_lock_init(&adev->uvd_ctx_idx_lock);
2267 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002268 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002269 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002270 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002271 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002272
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002273 INIT_LIST_HEAD(&adev->shadow_list);
2274 mutex_init(&adev->shadow_list_lock);
2275
Andres Rodriguez795f2812017-03-06 16:27:55 -05002276 INIT_LIST_HEAD(&adev->ring_lru_list);
2277 spin_lock_init(&adev->ring_lru_list_lock);
2278
Alex Deucher06ec9072017-12-14 15:02:39 -05002279 INIT_DELAYED_WORK(&adev->late_init_work,
2280 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05302281
Alex Xie0fa49552017-06-08 14:58:05 -04002282 /* Registers mapping */
2283 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002284 if (adev->asic_type >= CHIP_BONAIRE) {
2285 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2286 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2287 } else {
2288 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2289 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2290 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002291
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2293 if (adev->rmmio == NULL) {
2294 return -ENOMEM;
2295 }
2296 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2297 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2298
Christian König705e5192017-06-08 11:15:16 +02002299 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05002300 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301
2302 /* io port mapping */
2303 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2304 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2305 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2306 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2307 break;
2308 }
2309 }
2310 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002311 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312
Alex Deucher5494d862018-03-09 15:14:11 -05002313 amdgpu_device_get_pcie_info(adev);
2314
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002315 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05002316 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002317 if (r)
2318 return r;
2319
2320 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2321 /* this will fail for cards that aren't VGA class devices, just
2322 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05002323 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324
Alex Deuchere9bef452016-04-25 13:12:18 -04002325 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002326 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002327 if (!pci_is_thunderbolt_attached(adev->pdev))
2328 vga_switcheroo_register_client(adev->pdev,
2329 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002330 if (runtime)
2331 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2332
Shaoyun Liu9475a942018-02-01 18:13:23 -05002333 if (amdgpu_emu_mode == 1) {
2334 /* post the asic on emulation mode */
2335 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002336 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05002337 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05002338
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002339 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002340 if (!amdgpu_get_bios(adev)) {
2341 r = -EINVAL;
2342 goto failed;
2343 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002344
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002346 if (r) {
2347 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002348 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002349 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002350 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002351
Monk Liu4e99a442016-03-31 13:26:59 +08002352 /* detect if we are with an SRIOV vbios */
2353 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002354
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002355 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05002356 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002357 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002358 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002359 r = -EINVAL;
2360 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002361 }
Monk Liubec86372016-09-14 19:38:08 +08002362 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002363 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2364 if (r) {
2365 dev_err(adev->dev, "gpu post error!\n");
2366 goto failed;
2367 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002368 }
2369
Alex Deucher88b64e92017-07-10 10:43:10 -04002370 if (adev->is_atom_fw) {
2371 /* Initialize clocks */
2372 r = amdgpu_atomfirmware_get_clock_info(adev);
2373 if (r) {
2374 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002375 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002376 goto failed;
2377 }
2378 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002379 /* Initialize clocks */
2380 r = amdgpu_atombios_get_clock_info(adev);
2381 if (r) {
2382 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002383 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002384 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002385 }
2386 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002387 if (!amdgpu_device_has_dc_support(adev))
2388 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002389 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002390
Shaoyun Liubfca0282018-02-01 17:37:50 -05002391fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 /* Fence driver */
2393 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002394 if (r) {
2395 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002396 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002397 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002398 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399
2400 /* init the mode config */
2401 drm_mode_config_init(adev->ddev);
2402
Alex Deucher06ec9072017-12-14 15:02:39 -05002403 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002404 if (r) {
pding8840a382017-10-23 17:22:09 +08002405 /* failed in exclusive mode due to timeout */
2406 if (amdgpu_sriov_vf(adev) &&
2407 !amdgpu_sriov_runtime(adev) &&
2408 amdgpu_virt_mmio_blocked(adev) &&
2409 !amdgpu_virt_wait_reset(adev)) {
2410 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002411 /* Don't send request since VF is inactive. */
2412 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2413 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002414 r = -EAGAIN;
2415 goto failed;
2416 }
Alex Deucher06ec9072017-12-14 15:02:39 -05002417 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002418 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002419 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002420 }
2421
2422 adev->accel_working = true;
2423
Alex Xiee59c0202017-06-01 09:42:59 -04002424 amdgpu_vm_check_compute_bug(adev);
2425
Marek Olšák95844d22016-08-17 23:49:27 +02002426 /* Initialize the buffer migration limit. */
2427 if (amdgpu_moverate >= 0)
2428 max_MBps = amdgpu_moverate;
2429 else
2430 max_MBps = 8; /* Allow 8 MB/s. */
2431 /* Get a log2 for easy divisions. */
2432 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2433
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002434 r = amdgpu_ib_pool_init(adev);
2435 if (r) {
2436 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002437 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002438 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002439 }
2440
Horace Chen2dc8f812017-10-09 16:17:16 +08002441 if (amdgpu_sriov_vf(adev))
2442 amdgpu_virt_init_data_exchange(adev);
2443
Monk Liu9bc92b92017-02-08 17:38:13 +08002444 amdgpu_fbdev_init(adev);
2445
Rex Zhud2f52ac2017-09-22 17:47:27 +08002446 r = amdgpu_pm_sysfs_init(adev);
2447 if (r)
2448 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2449
Alex Deucher75758252017-12-14 15:23:14 -05002450 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002451 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002452 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002453
2454 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002455 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002456 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002457
Huang Rui50ab2532016-06-12 15:51:09 +08002458 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002459 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002460 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002461
Christian König763efb62017-12-06 15:44:51 +01002462 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002463 if (r)
Christian König763efb62017-12-06 15:44:51 +01002464 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002465
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002466 if ((amdgpu_testing & 1)) {
2467 if (adev->accel_working)
2468 amdgpu_test_moves(adev);
2469 else
2470 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2471 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002472 if (amdgpu_benchmarking) {
2473 if (adev->accel_working)
2474 amdgpu_benchmark(adev, amdgpu_benchmarking);
2475 else
2476 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2477 }
2478
2479 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2480 * explicit gating rather than handling it automatically.
2481 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002482 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002483 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002484 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002485 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002486 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002487 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488
2489 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002490
2491failed:
Gavin Wan89041942017-06-23 13:55:15 -04002492 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002493 if (runtime)
2494 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002495
Alex Deucher83ba1262016-06-03 18:21:41 -04002496 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002497}
2498
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499/**
2500 * amdgpu_device_fini - tear down the driver
2501 *
2502 * @adev: amdgpu_device pointer
2503 *
2504 * Tear down the driver info (all asics).
2505 * Called at driver shutdown.
2506 */
2507void amdgpu_device_fini(struct amdgpu_device *adev)
2508{
2509 int r;
2510
2511 DRM_INFO("amdgpu: finishing device.\n");
2512 adev->shutdown = true;
Mikita Lipskie5b03032018-03-15 16:53:08 -04002513 /* disable all interrupts */
2514 amdgpu_irq_disable_all(adev);
Mikita Lipskiff97cba2018-03-14 13:41:29 -04002515 if (adev->mode_info.mode_config_initialized){
2516 if (!amdgpu_device_has_dc_support(adev))
2517 drm_crtc_force_disable_all(adev->ddev);
2518 else
2519 drm_atomic_helper_shutdown(adev->ddev);
2520 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002521 amdgpu_ib_pool_fini(adev);
2522 amdgpu_fence_driver_fini(adev);
Emily Deng58e955d2018-03-08 09:35:19 +08002523 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002524 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002525 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002526 if (adev->firmware.gpu_info_fw) {
2527 release_firmware(adev->firmware.gpu_info_fw);
2528 adev->firmware.gpu_info_fw = NULL;
2529 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002530 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302531 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002532 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002533 if (!amdgpu_device_has_dc_support(adev))
2534 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002535
2536 if (amdgpu_emu_mode != 1)
2537 amdgpu_atombios_fini(adev);
2538
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002539 kfree(adev->bios);
2540 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002541 if (!pci_is_thunderbolt_attached(adev->pdev))
2542 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002543 if (adev->flags & AMD_IS_PX)
2544 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002545 vga_client_register(adev->pdev, NULL, NULL, NULL);
2546 if (adev->rio_mem)
2547 pci_iounmap(adev->pdev, adev->rio_mem);
2548 adev->rio_mem = NULL;
2549 iounmap(adev->rmmio);
2550 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002551 amdgpu_device_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002552 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002553}
2554
2555
2556/*
2557 * Suspend & resume.
2558 */
2559/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002560 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002561 *
2562 * @pdev: drm dev pointer
2563 * @state: suspend state
2564 *
2565 * Puts the hw in the suspend state (all asics).
2566 * Returns 0 for success or an error on failure.
2567 * Called at driver suspend.
2568 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002569int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002570{
2571 struct amdgpu_device *adev;
2572 struct drm_crtc *crtc;
2573 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002574 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002575
2576 if (dev == NULL || dev->dev_private == NULL) {
2577 return -ENODEV;
2578 }
2579
2580 adev = dev->dev_private;
2581
2582 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2583 return 0;
2584
2585 drm_kms_helper_poll_disable(dev);
2586
Harry Wentland45622362017-09-12 15:58:20 -04002587 if (!amdgpu_device_has_dc_support(adev)) {
2588 /* turn off display hw */
2589 drm_modeset_lock_all(dev);
2590 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2591 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2592 }
2593 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002594 }
2595
Yong Zhaoba997702015-11-09 17:21:45 -05002596 amdgpu_amdkfd_suspend(adev);
2597
Alex Deucher756e6882015-10-08 00:03:36 -04002598 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002599 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002600 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Daniel Stonee68d14d2018-03-30 15:11:38 +01002601 struct drm_framebuffer *fb = crtc->primary->fb;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002602 struct amdgpu_bo *robj;
2603
Alex Deucher756e6882015-10-08 00:03:36 -04002604 if (amdgpu_crtc->cursor_bo) {
2605 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002606 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002607 if (r == 0) {
2608 amdgpu_bo_unpin(aobj);
2609 amdgpu_bo_unreserve(aobj);
2610 }
2611 }
2612
Daniel Stonee68d14d2018-03-30 15:11:38 +01002613 if (fb == NULL || fb->obj[0] == NULL) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002614 continue;
2615 }
Daniel Stonee68d14d2018-03-30 15:11:38 +01002616 robj = gem_to_amdgpu_bo(fb->obj[0]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002617 /* don't unpin kernel fb objects */
2618 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002619 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002620 if (r == 0) {
2621 amdgpu_bo_unpin(robj);
2622 amdgpu_bo_unreserve(robj);
2623 }
2624 }
2625 }
2626 /* evict vram memory */
2627 amdgpu_bo_evict_vram(adev);
2628
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002629 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002630
Alex Deuchercdd61df2017-12-14 16:47:40 -05002631 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002632
Alex Deuchera0a71e42016-10-10 12:41:36 -04002633 /* evict remaining vram memory
2634 * This second call to evict vram is to evict the gart page table
2635 * using the CPU.
2636 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002637 amdgpu_bo_evict_vram(adev);
2638
2639 pci_save_state(dev->pdev);
2640 if (suspend) {
2641 /* Shut down the device */
2642 pci_disable_device(dev->pdev);
2643 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002644 } else {
2645 r = amdgpu_asic_reset(adev);
2646 if (r)
2647 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002648 }
2649
2650 if (fbcon) {
2651 console_lock();
2652 amdgpu_fbdev_set_suspend(adev, 1);
2653 console_unlock();
2654 }
2655 return 0;
2656}
2657
2658/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002659 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002660 *
2661 * @pdev: drm dev pointer
2662 *
2663 * Bring the hw back to operating state (all asics).
2664 * Returns 0 for success or an error on failure.
2665 * Called at driver resume.
2666 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002667int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002668{
2669 struct drm_connector *connector;
2670 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002671 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002672 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002673
2674 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2675 return 0;
2676
jimqu74b0b152016-09-07 17:09:12 +08002677 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002678 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002679
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002680 if (resume) {
2681 pci_set_power_state(dev->pdev, PCI_D0);
2682 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002683 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002684 if (r)
2685 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002686 }
2687
2688 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002689 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002690 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2691 if (r)
2692 DRM_ERROR("amdgpu asic init failed\n");
2693 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002694
Alex Deucher06ec9072017-12-14 15:02:39 -05002695 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002696 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002697 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002698 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002699 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002700 amdgpu_fence_driver_resume(adev);
2701
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002702
Alex Deucher06ec9072017-12-14 15:02:39 -05002703 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002704 if (r)
2705 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002706
Alex Deucher756e6882015-10-08 00:03:36 -04002707 /* pin cursors */
2708 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2709 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2710
2711 if (amdgpu_crtc->cursor_bo) {
2712 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002713 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002714 if (r == 0) {
2715 r = amdgpu_bo_pin(aobj,
2716 AMDGPU_GEM_DOMAIN_VRAM,
2717 &amdgpu_crtc->cursor_addr);
2718 if (r != 0)
2719 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2720 amdgpu_bo_unreserve(aobj);
2721 }
2722 }
2723 }
Yong Zhaoba997702015-11-09 17:21:45 -05002724 r = amdgpu_amdkfd_resume(adev);
2725 if (r)
2726 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002727
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002728 /* blat the mode back in */
2729 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002730 if (!amdgpu_device_has_dc_support(adev)) {
2731 /* pre DCE11 */
2732 drm_helper_resume_force_mode(dev);
2733
2734 /* turn on display hw */
2735 drm_modeset_lock_all(dev);
2736 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2737 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2738 }
2739 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002740 }
2741 }
2742
2743 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002744
2745 /*
2746 * Most of the connector probing functions try to acquire runtime pm
2747 * refs to ensure that the GPU is powered on when connector polling is
2748 * performed. Since we're calling this from a runtime PM callback,
2749 * trying to acquire rpm refs will cause us to deadlock.
2750 *
2751 * Since we're guaranteed to be holding the rpm lock, it's safe to
2752 * temporarily disable the rpm helpers so this doesn't deadlock us.
2753 */
2754#ifdef CONFIG_PM
2755 dev->dev->power.disable_depth++;
2756#endif
Harry Wentland45622362017-09-12 15:58:20 -04002757 if (!amdgpu_device_has_dc_support(adev))
2758 drm_helper_hpd_irq_event(dev);
2759 else
2760 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002761#ifdef CONFIG_PM
2762 dev->dev->power.disable_depth--;
2763#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002764
Huang Rui03161a62017-04-13 16:12:26 +08002765 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002766 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002767
Huang Rui03161a62017-04-13 16:12:26 +08002768unlock:
2769 if (fbcon)
2770 console_unlock();
2771
2772 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002773}
2774
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002775/**
2776 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2777 *
2778 * @adev: amdgpu_device pointer
2779 *
2780 * The list of all the hardware IPs that make up the asic is walked and
2781 * the check_soft_reset callbacks are run. check_soft_reset determines
2782 * if the asic is still hung or not.
2783 * Returns true if any of the IPs are still in a hung state, false if not.
2784 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002785static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002786{
2787 int i;
2788 bool asic_hang = false;
2789
Monk Liuf993d622017-10-16 19:46:01 +08002790 if (amdgpu_sriov_vf(adev))
2791 return true;
2792
Alex Deucher8bc04c22018-03-29 14:48:37 -05002793 if (amdgpu_asic_need_full_reset(adev))
2794 return true;
2795
Chunming Zhou63fbf422016-07-15 11:19:20 +08002796 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002797 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002798 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002799 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2800 adev->ip_blocks[i].status.hang =
2801 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2802 if (adev->ip_blocks[i].status.hang) {
2803 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002804 asic_hang = true;
2805 }
2806 }
2807 return asic_hang;
2808}
2809
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002810/**
2811 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2812 *
2813 * @adev: amdgpu_device pointer
2814 *
2815 * The list of all the hardware IPs that make up the asic is walked and the
2816 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2817 * handles any IP specific hardware or software state changes that are
2818 * necessary for a soft reset to succeed.
2819 * Returns 0 on success, negative error code on failure.
2820 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002821static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002822{
2823 int i, r = 0;
2824
2825 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002826 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002827 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002828 if (adev->ip_blocks[i].status.hang &&
2829 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2830 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002831 if (r)
2832 return r;
2833 }
2834 }
2835
2836 return 0;
2837}
2838
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002839/**
2840 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2841 *
2842 * @adev: amdgpu_device pointer
2843 *
2844 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2845 * reset is necessary to recover.
2846 * Returns true if a full asic reset is required, false if not.
2847 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002848static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002849{
Alex Deucherda146d32016-10-13 16:07:03 -04002850 int i;
2851
Alex Deucher8bc04c22018-03-29 14:48:37 -05002852 if (amdgpu_asic_need_full_reset(adev))
2853 return true;
2854
Alex Deucherda146d32016-10-13 16:07:03 -04002855 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002856 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002857 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002858 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2859 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2860 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002861 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2862 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002863 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002864 DRM_INFO("Some block need full reset!\n");
2865 return true;
2866 }
2867 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002868 }
2869 return false;
2870}
2871
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002872/**
2873 * amdgpu_device_ip_soft_reset - do a soft reset
2874 *
2875 * @adev: amdgpu_device pointer
2876 *
2877 * The list of all the hardware IPs that make up the asic is walked and the
2878 * soft_reset callbacks are run if the block is hung. soft_reset handles any
2879 * IP specific hardware or software state changes that are necessary to soft
2880 * reset the IP.
2881 * Returns 0 on success, negative error code on failure.
2882 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002883static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002884{
2885 int i, r = 0;
2886
2887 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002888 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002889 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002890 if (adev->ip_blocks[i].status.hang &&
2891 adev->ip_blocks[i].version->funcs->soft_reset) {
2892 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002893 if (r)
2894 return r;
2895 }
2896 }
2897
2898 return 0;
2899}
2900
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002901/**
2902 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
2903 *
2904 * @adev: amdgpu_device pointer
2905 *
2906 * The list of all the hardware IPs that make up the asic is walked and the
2907 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
2908 * handles any IP specific hardware or software state changes that are
2909 * necessary after the IP has been soft reset.
2910 * Returns 0 on success, negative error code on failure.
2911 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002912static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002913{
2914 int i, r = 0;
2915
2916 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002917 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002918 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002919 if (adev->ip_blocks[i].status.hang &&
2920 adev->ip_blocks[i].version->funcs->post_soft_reset)
2921 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002922 if (r)
2923 return r;
2924 }
2925
2926 return 0;
2927}
2928
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002929/**
2930 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
2931 *
2932 * @adev: amdgpu_device pointer
2933 * @ring: amdgpu_ring for the engine handling the buffer operations
2934 * @bo: amdgpu_bo buffer whose shadow is being restored
2935 * @fence: dma_fence associated with the operation
2936 *
2937 * Restores the VRAM buffer contents from the shadow in GTT. Used to
2938 * restore things like GPUVM page tables after a GPU reset where
2939 * the contents of VRAM might be lost.
2940 * Returns 0 on success, negative error code on failure.
2941 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002942static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2943 struct amdgpu_ring *ring,
2944 struct amdgpu_bo *bo,
2945 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002946{
2947 uint32_t domain;
2948 int r;
2949
Roger.He23d2e502017-04-21 14:24:26 +08002950 if (!bo->shadow)
2951 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002952
Alex Xie1d284792017-04-24 13:53:04 -04002953 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002954 if (r)
2955 return r;
2956 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2957 /* if bo has been evicted, then no need to recover */
2958 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002959 r = amdgpu_bo_validate(bo->shadow);
2960 if (r) {
2961 DRM_ERROR("bo validate failed!\n");
2962 goto err;
2963 }
2964
Roger.He23d2e502017-04-21 14:24:26 +08002965 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002966 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002967 if (r) {
2968 DRM_ERROR("recover page table failed!\n");
2969 goto err;
2970 }
2971 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002972err:
Roger.He23d2e502017-04-21 14:24:26 +08002973 amdgpu_bo_unreserve(bo);
2974 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002975}
2976
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002977/**
2978 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
2979 *
2980 * @adev: amdgpu_device pointer
2981 *
2982 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
2983 * restore things like GPUVM page tables after a GPU reset where
2984 * the contents of VRAM might be lost.
2985 * Returns 0 on success, 1 on failure.
2986 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002987static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2988{
2989 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2990 struct amdgpu_bo *bo, *tmp;
2991 struct dma_fence *fence = NULL, *next = NULL;
2992 long r = 1;
2993 int i = 0;
2994 long tmo;
2995
2996 if (amdgpu_sriov_runtime(adev))
2997 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2998 else
2999 tmo = msecs_to_jiffies(100);
3000
3001 DRM_INFO("recover vram bo from shadow start\n");
3002 mutex_lock(&adev->shadow_list_lock);
3003 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
3004 next = NULL;
3005 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
3006 if (fence) {
3007 r = dma_fence_wait_timeout(fence, false, tmo);
3008 if (r == 0)
3009 pr_err("wait fence %p[%d] timeout\n", fence, i);
3010 else if (r < 0)
3011 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3012 if (r < 1) {
3013 dma_fence_put(fence);
3014 fence = next;
3015 break;
3016 }
3017 i++;
3018 }
3019
3020 dma_fence_put(fence);
3021 fence = next;
3022 }
3023 mutex_unlock(&adev->shadow_list_lock);
3024
3025 if (fence) {
3026 r = dma_fence_wait_timeout(fence, false, tmo);
3027 if (r == 0)
3028 pr_err("wait fence %p[%d] timeout\n", fence, i);
3029 else if (r < 0)
3030 pr_err("wait fence %p[%d] interrupted\n", fence, i);
3031
3032 }
3033 dma_fence_put(fence);
3034
3035 if (r > 0)
3036 DRM_INFO("recover vram bo from shadow done\n");
3037 else
3038 DRM_ERROR("recover vram bo from shadow failed\n");
3039
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003040 return (r > 0) ? 0 : 1;
Monk Liuc41d1cf2017-12-25 11:59:27 +08003041}
3042
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003043/**
Alex Deucher06ec9072017-12-14 15:02:39 -05003044 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08003045 *
3046 * @adev: amdgpu device pointer
Monk Liua90ad3c2017-01-23 14:22:08 +08003047 *
Monk Liu57406822017-10-25 16:37:02 +08003048 * attempt to do soft-reset or full-reset and reinitialize Asic
3049 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003050 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08003051static int amdgpu_device_reset(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08003052{
Monk Liu57406822017-10-25 16:37:02 +08003053 bool need_full_reset, vram_lost = 0;
3054 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003055
Alex Deucher06ec9072017-12-14 15:02:39 -05003056 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08003057
3058 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05003059 amdgpu_device_ip_pre_soft_reset(adev);
3060 r = amdgpu_device_ip_soft_reset(adev);
3061 amdgpu_device_ip_post_soft_reset(adev);
3062 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08003063 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3064 need_full_reset = true;
3065 }
3066 }
3067
3068 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05003069 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003070
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003071retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08003072 r = amdgpu_asic_reset(adev);
3073 /* post card */
3074 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05003075
Chunming Zhou35d782f2016-07-15 15:57:13 +08003076 if (!r) {
3077 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05003078 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003079 if (r)
3080 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003081
Alex Deucher06ec9072017-12-14 15:02:39 -05003082 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08003083 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003084 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08003085 atomic_inc(&adev->vram_lost_counter);
3086 }
Monk Liu57406822017-10-25 16:37:02 +08003087
Christian Königc1c7ce82017-10-16 16:50:32 +02003088 r = amdgpu_gtt_mgr_recover(
3089 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003090 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08003091 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003092
Alex Deucher06ec9072017-12-14 15:02:39 -05003093 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003094 if (r)
3095 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003096
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003097 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05003098 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003099 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08003100 }
Monk Liu57406822017-10-25 16:37:02 +08003101
Chunming Zhoufcf06492017-05-05 10:33:33 +08003102out:
3103 if (!r) {
3104 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08003105 r = amdgpu_ib_ring_tests(adev);
3106 if (r) {
3107 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05003108 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003109 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08003110 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08003111 }
Monk Liu57406822017-10-25 16:37:02 +08003112 }
3113
Monk Liuc41d1cf2017-12-25 11:59:27 +08003114 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3115 r = amdgpu_device_handle_vram_lost(adev);
Monk Liu57406822017-10-25 16:37:02 +08003116
3117 return r;
3118}
3119
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003120/**
Alex Deucher06ec9072017-12-14 15:02:39 -05003121 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08003122 *
3123 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08003124 *
3125 * do VF FLR and reinitialize Asic
3126 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003127 */
3128static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3129 bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08003130{
3131 int r;
3132
3133 if (from_hypervisor)
3134 r = amdgpu_virt_request_full_gpu(adev, true);
3135 else
3136 r = amdgpu_virt_reset_gpu(adev);
3137 if (r)
3138 return r;
3139
3140 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05003141 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08003142 if (r)
3143 goto error;
3144
3145 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02003146 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08003147
3148 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05003149 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08003150 if (r)
3151 goto error;
3152
3153 amdgpu_irq_gpu_reset_resume_helper(adev);
3154 r = amdgpu_ib_ring_tests(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003155
Emily Dengabc34252018-04-26 18:02:55 +08003156error:
3157 amdgpu_virt_release_full_gpu(adev, true);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003158 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3159 atomic_inc(&adev->vram_lost_counter);
3160 r = amdgpu_device_handle_vram_lost(adev);
3161 }
Monk Liu57406822017-10-25 16:37:02 +08003162
Monk Liu57406822017-10-25 16:37:02 +08003163 return r;
3164}
3165
3166/**
Alex Deucher5f152b52017-12-15 16:40:49 -05003167 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08003168 *
3169 * @adev: amdgpu device pointer
3170 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003171 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08003172 *
3173 * Attempt to reset the GPU if it has hung (all asics).
3174 * Returns 0 for success or an error on failure.
3175 */
Alex Deucher5f152b52017-12-15 16:40:49 -05003176int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3177 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08003178{
Monk Liu57406822017-10-25 16:37:02 +08003179 int i, r, resched;
3180
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05003181 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003182 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3183 return 0;
3184 }
3185
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003186 if (!force && (amdgpu_gpu_recovery == 0 ||
3187 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3188 DRM_INFO("GPU recovery disabled.\n");
3189 return 0;
3190 }
3191
Monk Liu57406822017-10-25 16:37:02 +08003192 dev_info(adev->dev, "GPU reset begin!\n");
3193
Monk Liu13a752e2017-10-17 15:11:12 +08003194 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003195 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003196 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003197
3198 /* block TTM */
3199 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Monk Liu71182662017-12-25 15:14:58 +08003200
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
Andrey Grodzovskybf830602018-05-17 11:18:34 -04003240 if (!amdgpu_device_has_dc_support(adev)) {
Harry Wentland45622362017-09-12 15:58:20 -04003241 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003242 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003243
3244 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003245
Gavin Wan89041942017-06-23 13:55:15 -04003246 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003247 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003248 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3249 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3250 } else {
3251 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003252 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003253
Gavin Wan89041942017-06-23 13:55:15 -04003254 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003255 adev->in_gpu_reset = 0;
3256 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003257 return r;
3258}
3259
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003260/**
3261 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3262 *
3263 * @adev: amdgpu_device pointer
3264 *
3265 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3266 * and lanes) of the slot the device is in. Handles APUs and
3267 * virtualized environments where PCIE config space may not be available.
3268 */
Alex Deucher5494d862018-03-09 15:14:11 -05003269static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003270{
3271 u32 mask;
3272 int ret;
3273
Alex Deuchercd474ba2016-02-04 10:21:23 -05003274 if (amdgpu_pcie_gen_cap)
3275 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3276
3277 if (amdgpu_pcie_lane_cap)
3278 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3279
3280 /* covers APUs as well */
3281 if (pci_is_root_bus(adev->pdev->bus)) {
3282 if (adev->pm.pcie_gen_mask == 0)
3283 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3284 if (adev->pm.pcie_mlw_mask == 0)
3285 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003286 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003287 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003288
3289 if (adev->pm.pcie_gen_mask == 0) {
3290 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3291 if (!ret) {
3292 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3293 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3294 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3295
3296 if (mask & DRM_PCIE_SPEED_25)
3297 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3298 if (mask & DRM_PCIE_SPEED_50)
3299 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3300 if (mask & DRM_PCIE_SPEED_80)
3301 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3302 } else {
3303 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3304 }
3305 }
3306 if (adev->pm.pcie_mlw_mask == 0) {
3307 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3308 if (!ret) {
3309 switch (mask) {
3310 case 32:
3311 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3312 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3313 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3314 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3315 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3316 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3317 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3318 break;
3319 case 16:
3320 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3321 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3322 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3323 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3324 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3325 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3326 break;
3327 case 12:
3328 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3329 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3330 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3331 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3332 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3333 break;
3334 case 8:
3335 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3336 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3337 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3338 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3339 break;
3340 case 4:
3341 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3342 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3343 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3344 break;
3345 case 2:
3346 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3347 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3348 break;
3349 case 1:
3350 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3351 break;
3352 default:
3353 break;
3354 }
3355 } else {
3356 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003357 }
3358 }
3359}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003360