blob: 9a2c2c3f06032fd177f3fb057ef24ccf10746a94 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
Harry Wentland45622362017-09-12 15:58:20 -040033#include <drm/drm_atomic_helper.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040034#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040056#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
Yong Zhaoba997702015-11-09 17:21:45 -050058#include "amdgpu_amdkfd.h"
Rex Zhud2f52ac2017-09-22 17:47:27 +080059#include "amdgpu_pm.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040060
Alex Deuchere2a75f82017-04-27 16:58:01 -040061MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040062MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040063
Shirish S2dc80b02017-05-25 10:05:25 +053064#define AMDGPU_RESUME_MS 2000
65
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080067 "TAHITI",
68 "PITCAIRN",
69 "VERDE",
70 "OLAND",
71 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 "BONAIRE",
73 "KAVERI",
74 "KABINI",
75 "HAWAII",
76 "MULLINS",
77 "TOPAZ",
78 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080079 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040081 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040082 "POLARIS10",
83 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050084 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080085 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080086 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 "LAST",
88};
89
90bool amdgpu_device_is_px(struct drm_device *dev)
91{
92 struct amdgpu_device *adev = dev->dev_private;
93
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080094 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095 return true;
96 return false;
97}
98
99/*
100 * MMIO register access helper functions.
101 */
102uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800103 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400105 uint32_t ret;
106
pding43ca8ef2017-10-13 15:38:35 +0800107 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800108 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800109
Monk Liu15d72fd2017-01-25 15:07:40 +0800110 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400111 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112 else {
113 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114
115 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
116 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
117 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
118 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400120 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
121 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122}
123
124void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800125 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400127 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800128
Ken Wang47ed4e12017-07-04 13:11:52 +0800129 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
130 adev->last_mm_index = v;
131 }
132
pding43ca8ef2017-10-13 15:38:35 +0800133 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800134 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800135
Monk Liu15d72fd2017-01-25 15:07:40 +0800136 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
138 else {
139 unsigned long flags;
140
141 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
142 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
143 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
144 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
145 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800146
147 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
148 udelay(500);
149 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150}
151
152u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
153{
154 if ((reg * 4) < adev->rio_mem_size)
155 return ioread32(adev->rio_mem + (reg * 4));
156 else {
157 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
158 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
159 }
160}
161
162void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
163{
Ken Wang47ed4e12017-07-04 13:11:52 +0800164 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
165 adev->last_mm_index = v;
166 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167
168 if ((reg * 4) < adev->rio_mem_size)
169 iowrite32(v, adev->rio_mem + (reg * 4));
170 else {
171 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
172 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
173 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800174
175 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
176 udelay(500);
177 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178}
179
180/**
181 * amdgpu_mm_rdoorbell - read a doorbell dword
182 *
183 * @adev: amdgpu_device pointer
184 * @index: doorbell index
185 *
186 * Returns the value in the doorbell aperture at the
187 * requested doorbell index (CIK).
188 */
189u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
190{
191 if (index < adev->doorbell.num_doorbells) {
192 return readl(adev->doorbell.ptr + index);
193 } else {
194 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
195 return 0;
196 }
197}
198
199/**
200 * amdgpu_mm_wdoorbell - write a doorbell dword
201 *
202 * @adev: amdgpu_device pointer
203 * @index: doorbell index
204 * @v: value to write
205 *
206 * Writes @v to the doorbell aperture at the
207 * requested doorbell index (CIK).
208 */
209void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
210{
211 if (index < adev->doorbell.num_doorbells) {
212 writel(v, adev->doorbell.ptr + index);
213 } else {
214 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
215 }
216}
217
218/**
Ken Wang832be402016-03-18 15:23:08 +0800219 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
220 *
221 * @adev: amdgpu_device pointer
222 * @index: doorbell index
223 *
224 * Returns the value in the doorbell aperture at the
225 * requested doorbell index (VEGA10+).
226 */
227u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
228{
229 if (index < adev->doorbell.num_doorbells) {
230 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
231 } else {
232 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
233 return 0;
234 }
235}
236
237/**
238 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
239 *
240 * @adev: amdgpu_device pointer
241 * @index: doorbell index
242 * @v: value to write
243 *
244 * Writes @v to the doorbell aperture at the
245 * requested doorbell index (VEGA10+).
246 */
247void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
248{
249 if (index < adev->doorbell.num_doorbells) {
250 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
251 } else {
252 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
253 }
254}
255
256/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257 * amdgpu_invalid_rreg - dummy reg read function
258 *
259 * @adev: amdgpu device pointer
260 * @reg: offset of register
261 *
262 * Dummy register read function. Used for register blocks
263 * that certain asics don't have (all asics).
264 * Returns the value in the register.
265 */
266static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
267{
268 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
269 BUG();
270 return 0;
271}
272
273/**
274 * amdgpu_invalid_wreg - dummy reg write function
275 *
276 * @adev: amdgpu device pointer
277 * @reg: offset of register
278 * @v: value to write to the register
279 *
280 * Dummy register read function. Used for register blocks
281 * that certain asics don't have (all asics).
282 */
283static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
284{
285 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
286 reg, v);
287 BUG();
288}
289
290/**
291 * amdgpu_block_invalid_rreg - dummy reg read function
292 *
293 * @adev: amdgpu device pointer
294 * @block: offset of instance
295 * @reg: offset of register
296 *
297 * Dummy register read function. Used for register blocks
298 * that certain asics don't have (all asics).
299 * Returns the value in the register.
300 */
301static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
302 uint32_t block, uint32_t reg)
303{
304 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
305 reg, block);
306 BUG();
307 return 0;
308}
309
310/**
311 * amdgpu_block_invalid_wreg - dummy reg write function
312 *
313 * @adev: amdgpu device pointer
314 * @block: offset of instance
315 * @reg: offset of register
316 * @v: value to write to the register
317 *
318 * Dummy register read function. Used for register blocks
319 * that certain asics don't have (all asics).
320 */
321static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
322 uint32_t block,
323 uint32_t reg, uint32_t v)
324{
325 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
326 reg, block, v);
327 BUG();
328}
329
Alex Deucher06ec9072017-12-14 15:02:39 -0500330static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331{
Christian Königa4a02772017-07-27 17:24:36 +0200332 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
333 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
334 &adev->vram_scratch.robj,
335 &adev->vram_scratch.gpu_addr,
336 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400337}
338
Alex Deucher06ec9072017-12-14 15:02:39 -0500339static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400340{
Christian König078af1a2017-07-27 17:43:00 +0200341 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342}
343
344/**
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500345 * amdgpu_device_program_register_sequence - program an array of registers.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 *
347 * @adev: amdgpu_device pointer
348 * @registers: pointer to the register array
349 * @array_size: size of the register array
350 *
351 * Programs an array or registers with and and or masks.
352 * This is a helper for setting golden registers.
353 */
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500354void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
355 const u32 *registers,
356 const u32 array_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357{
358 u32 tmp, reg, and_mask, or_mask;
359 int i;
360
361 if (array_size % 3)
362 return;
363
364 for (i = 0; i < array_size; i +=3) {
365 reg = registers[i + 0];
366 and_mask = registers[i + 1];
367 or_mask = registers[i + 2];
368
369 if (and_mask == 0xffffffff) {
370 tmp = or_mask;
371 } else {
372 tmp = RREG32(reg);
373 tmp &= ~and_mask;
374 tmp |= or_mask;
375 }
376 WREG32(reg, tmp);
377 }
378}
379
Alex Deucher8111c382017-12-14 16:22:53 -0500380void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381{
382 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
383}
384
385/*
386 * GPU doorbell aperture helpers function.
387 */
388/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500389 * amdgpu_device_doorbell_init - Init doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400390 *
391 * @adev: amdgpu_device pointer
392 *
393 * Init doorbell driver information (CIK)
394 * Returns 0 on success, error on failure.
395 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500396static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400397{
Christian König705e5192017-06-08 11:15:16 +0200398 /* No doorbell on SI hardware generation */
399 if (adev->asic_type < CHIP_BONAIRE) {
400 adev->doorbell.base = 0;
401 adev->doorbell.size = 0;
402 adev->doorbell.num_doorbells = 0;
403 adev->doorbell.ptr = NULL;
404 return 0;
405 }
406
Christian Königd6895ad2017-02-28 10:36:43 +0100407 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
408 return -EINVAL;
409
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400410 /* doorbell bar mapping */
411 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
412 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
413
Christian Königedf600d2016-05-03 15:54:54 +0200414 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
416 if (adev->doorbell.num_doorbells == 0)
417 return -EINVAL;
418
Christian König8972e5d2017-03-06 13:34:57 +0100419 adev->doorbell.ptr = ioremap(adev->doorbell.base,
420 adev->doorbell.num_doorbells *
421 sizeof(u32));
422 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400423 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424
425 return 0;
426}
427
428/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500429 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400430 *
431 * @adev: amdgpu_device pointer
432 *
433 * Tear down doorbell driver information (CIK)
434 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500435static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400436{
437 iounmap(adev->doorbell.ptr);
438 adev->doorbell.ptr = NULL;
439}
440
Alex Deucher22cb0162017-12-14 16:27:11 -0500441
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400442
443/*
Alex Deucher06ec9072017-12-14 15:02:39 -0500444 * amdgpu_device_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400445 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400446 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400447 */
448
449/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500450 * amdgpu_device_wb_fini - Disable Writeback and free memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 *
452 * @adev: amdgpu_device pointer
453 *
454 * Disables Writeback and frees the Writeback memory (all asics).
455 * Used at driver shutdown.
456 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500457static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400458{
459 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400460 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
461 &adev->wb.gpu_addr,
462 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 adev->wb.wb_obj = NULL;
464 }
465}
466
467/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500468 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469 *
470 * @adev: amdgpu_device pointer
471 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400472 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473 * Used at driver startup.
474 * Returns 0 on success or an -error on failure.
475 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500476static int amdgpu_device_wb_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400477{
478 int r;
479
480 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400481 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
482 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400483 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
484 &adev->wb.wb_obj, &adev->wb.gpu_addr,
485 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 if (r) {
487 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
488 return r;
489 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490
491 adev->wb.num_wb = AMDGPU_MAX_WB;
492 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
493
494 /* clear wb memory */
Monk Liu73469582017-12-29 17:06:41 +0800495 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496 }
497
498 return 0;
499}
500
501/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500502 * amdgpu_device_wb_get - Allocate a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 *
504 * @adev: amdgpu_device pointer
505 * @wb: wb index
506 *
507 * Allocate a wb slot for use by the driver (all asics).
508 * Returns 0 on success or -EINVAL on failure.
509 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500510int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511{
512 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400513
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400514 if (offset < adev->wb.num_wb) {
515 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800516 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400517 return 0;
518 } else {
519 return -EINVAL;
520 }
521}
522
Ken Wang70142852016-03-18 15:08:49 +0800523/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500524 * amdgpu_device_wb_free - Free a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 *
526 * @adev: amdgpu_device pointer
527 * @wb: wb index
528 *
529 * Free a wb slot allocated for use by the driver (all asics)
530 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500531void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400532{
Monk Liu73469582017-12-29 17:06:41 +0800533 wb >>= 3;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 if (wb < adev->wb.num_wb)
Monk Liu73469582017-12-29 17:06:41 +0800535 __clear_bit(wb, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536}
537
538/**
Alex Deucher2543e282017-12-14 16:33:36 -0500539 * amdgpu_device_vram_location - try to find VRAM location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540 * @adev: amdgpu device structure holding all necessary informations
541 * @mc: memory controller structure holding memory informations
542 * @base: base address at which to put VRAM
543 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400544 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100545 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546 */
Alex Deucher2543e282017-12-14 16:33:36 -0500547void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100548 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400549{
550 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
551
552 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
554 if (limit && limit < mc->real_vram_size)
555 mc->real_vram_size = limit;
556 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
557 mc->mc_vram_size >> 20, mc->vram_start,
558 mc->vram_end, mc->real_vram_size >> 20);
559}
560
561/**
Alex Deucher2543e282017-12-14 16:33:36 -0500562 * amdgpu_device_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 * @adev: amdgpu device structure holding all necessary informations
564 * @mc: memory controller structure holding memory informations
565 *
566 * Function will place try to place GTT before or after VRAM.
567 *
568 * If GTT size is bigger than space left then we ajust GTT size.
569 * Thus function will never fails.
570 *
571 * FIXME: when reducing GTT size align new size on power of 2.
572 */
Alex Deucher2543e282017-12-14 16:33:36 -0500573void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100574 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400575{
576 u64 size_af, size_bf;
577
Christian König770d13b2018-01-12 14:52:22 +0100578 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200579 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200581 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200583 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 }
Christian König6f02a692017-07-07 11:56:59 +0200585 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 } else {
Christian König6f02a692017-07-07 11:56:59 +0200587 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200589 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400590 }
Christian Königb98f1b92017-11-16 20:12:51 +0100591 /* VCE doesn't like it when BOs cross a 4GB segment, so align
592 * the GART base on a 4GB boundary as well.
593 */
594 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 }
Christian König6f02a692017-07-07 11:56:59 +0200596 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200598 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599}
600
Christian Königd6895ad2017-02-28 10:36:43 +0100601/**
602 * amdgpu_device_resize_fb_bar - try to resize FB BAR
603 *
604 * @adev: amdgpu_device pointer
605 *
606 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
607 * to fail, but if any of the BARs is not accessible after the size we abort
608 * driver loading by returning -ENODEV.
609 */
610int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
611{
Christian König770d13b2018-01-12 14:52:22 +0100612 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100613 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100614 struct pci_bus *root;
615 struct resource *res;
616 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100617 u16 cmd;
618 int r;
619
pding0c03b912017-11-07 11:02:00 +0800620 /* Bypass for VF */
621 if (amdgpu_sriov_vf(adev))
622 return 0;
623
Christian König31b8ada2017-11-15 20:07:38 +0100624 /* Check if the root BUS has 64bit memory resources */
625 root = adev->pdev->bus;
626 while (root->parent)
627 root = root->parent;
628
629 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100630 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100631 res->start > 0x100000000ull)
632 break;
633 }
634
635 /* Trying to resize is pointless without a root hub window above 4GB */
636 if (!res)
637 return 0;
638
Christian Königd6895ad2017-02-28 10:36:43 +0100639 /* Disable memory decoding while we change the BAR addresses and size */
640 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
641 pci_write_config_word(adev->pdev, PCI_COMMAND,
642 cmd & ~PCI_COMMAND_MEMORY);
643
644 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500645 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100646 if (adev->asic_type >= CHIP_BONAIRE)
647 pci_release_resource(adev->pdev, 2);
648
649 pci_release_resource(adev->pdev, 0);
650
651 r = pci_resize_resource(adev->pdev, 0, rbar_size);
652 if (r == -ENOSPC)
653 DRM_INFO("Not enough PCI address space for a large BAR.");
654 else if (r && r != -ENOTSUPP)
655 DRM_ERROR("Problem resizing BAR0 (%d).", r);
656
657 pci_assign_unassigned_bus_resources(adev->pdev->bus);
658
659 /* When the doorbell or fb BAR isn't available we have no chance of
660 * using the device.
661 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500662 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100663 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
664 return -ENODEV;
665
666 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
667
668 return 0;
669}
Horace Chena05502e2017-09-29 14:41:57 +0800670
671/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 * GPU helpers function.
673 */
674/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500675 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 *
677 * @adev: amdgpu_device pointer
678 *
Jim Quc836fec2017-02-10 15:59:59 +0800679 * Check if the asic has been initialized (all asics) at driver startup
680 * or post is needed if hw reset is performed.
681 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500683bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684{
685 uint32_t reg;
686
Monk Liubec86372016-09-14 19:38:08 +0800687 if (amdgpu_sriov_vf(adev))
688 return false;
689
690 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800691 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
692 * some old smc fw still need driver do vPost otherwise gpu hang, while
693 * those smc fw version above 22.15 doesn't have this flaw, so we force
694 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800695 */
696 if (adev->asic_type == CHIP_FIJI) {
697 int err;
698 uint32_t fw_ver;
699 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
700 /* force vPost if error occured */
701 if (err)
702 return true;
703
704 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800705 if (fw_ver < 0x00160e00)
706 return true;
Monk Liubec86372016-09-14 19:38:08 +0800707 }
Monk Liubec86372016-09-14 19:38:08 +0800708 }
pding91fe77e2017-10-19 09:38:39 +0800709
710 if (adev->has_hw_reset) {
711 adev->has_hw_reset = false;
712 return true;
713 }
714
715 /* bios scratch used on CIK+ */
716 if (adev->asic_type >= CHIP_BONAIRE)
717 return amdgpu_atombios_scratch_need_asic_init(adev);
718
719 /* check MEM_SIZE for older asics */
720 reg = amdgpu_asic_get_config_memsize(adev);
721
722 if ((reg != 0) && (reg != 0xffffffff))
723 return false;
724
725 return true;
Monk Liubec86372016-09-14 19:38:08 +0800726}
727
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728/* if we get transitioned to only one device, take VGA back */
729/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500730 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731 *
732 * @cookie: amdgpu_device pointer
733 * @state: enable/disable vga decode
734 *
735 * Enable/disable vga decode (all asics).
736 * Returns VGA resource flags.
737 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500738static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739{
740 struct amdgpu_device *adev = cookie;
741 amdgpu_asic_set_vga_state(adev, state);
742 if (state)
743 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
744 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
745 else
746 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
747}
748
Alex Deucher06ec9072017-12-14 15:02:39 -0500749static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800750{
751 /* defines number of bits in page table versus page directory,
752 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
753 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800754 if (amdgpu_vm_block_size == -1)
755 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800756
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800757 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800758 dev_warn(adev->dev, "VM page table size (%d) too small\n",
759 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100760 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800761 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800762}
763
Alex Deucher06ec9072017-12-14 15:02:39 -0500764static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800765{
Alex Deucher64dab072017-06-15 18:20:09 -0400766 /* no need to check the default value */
767 if (amdgpu_vm_size == -1)
768 return;
769
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800770 if (amdgpu_vm_size < 1) {
771 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
772 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100773 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800774 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800775}
776
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400777/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500778 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400779 *
780 * @adev: amdgpu_device pointer
781 *
782 * Validates certain module parameters and updates
783 * the associated values used by the driver (all asics).
784 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500785static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786{
Chunming Zhou5b011232015-12-10 17:34:33 +0800787 if (amdgpu_sched_jobs < 4) {
788 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
789 amdgpu_sched_jobs);
790 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400791 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800792 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
793 amdgpu_sched_jobs);
794 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
795 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796
Alex Deucher83e74db2017-08-21 11:58:25 -0400797 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200798 /* gart size must be greater or equal to 32M */
799 dev_warn(adev->dev, "gart size (%d) too small\n",
800 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400801 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400802 }
803
Christian König36d38372017-07-07 13:17:45 +0200804 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200806 dev_warn(adev->dev, "gtt size (%d) too small\n",
807 amdgpu_gtt_size);
808 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400809 }
810
Roger Hed07f14b2017-08-15 16:05:59 +0800811 /* valid range is between 4 and 9 inclusive */
812 if (amdgpu_vm_fragment_size != -1 &&
813 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
814 dev_warn(adev->dev, "valid range is between 4 and 9\n");
815 amdgpu_vm_fragment_size = -1;
816 }
817
Alex Deucher06ec9072017-12-14 15:02:39 -0500818 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819
Alex Deucher06ec9072017-12-14 15:02:39 -0500820 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +0200821
jimqu526bae32016-11-07 09:53:10 +0800822 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -0400823 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +0200824 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
825 amdgpu_vram_page_split);
826 amdgpu_vram_page_split = 1024;
827 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -0500828
829 if (amdgpu_lockup_timeout == 0) {
830 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
831 amdgpu_lockup_timeout = 10000;
832 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400833}
834
835/**
836 * amdgpu_switcheroo_set_state - set switcheroo state
837 *
838 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +0200839 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840 *
841 * Callback for the switcheroo driver. Suspends or resumes the
842 * the asics before or after it is powered up using ACPI methods.
843 */
844static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
845{
846 struct drm_device *dev = pci_get_drvdata(pdev);
847
848 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
849 return;
850
851 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -0800852 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400853 /* don't suspend or resume card normally */
854 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
855
Alex Deucher810ddc32016-08-23 13:25:49 -0400856 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400858 dev->switch_power_state = DRM_SWITCH_POWER_ON;
859 drm_kms_helper_poll_enable(dev);
860 } else {
Joe Perches7ca85292017-02-28 04:55:52 -0800861 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400862 drm_kms_helper_poll_disable(dev);
863 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -0400864 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
866 }
867}
868
869/**
870 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
871 *
872 * @pdev: pci dev pointer
873 *
874 * Callback for the switcheroo driver. Check of the switcheroo
875 * state can be changed.
876 * Returns true if the state can be changed, false if not.
877 */
878static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
879{
880 struct drm_device *dev = pci_get_drvdata(pdev);
881
882 /*
883 * FIXME: open_count is protected by drm_global_mutex but that would lead to
884 * locking inversion with the driver load path. And the access here is
885 * completely racy anyway. So don't bother with locking for now.
886 */
887 return dev->open_count == 0;
888}
889
890static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
891 .set_gpu_state = amdgpu_switcheroo_set_state,
892 .reprobe = NULL,
893 .can_switch = amdgpu_switcheroo_can_switch,
894};
895
Alex Deucher2990a1f2017-12-15 16:18:00 -0500896int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
897 enum amd_ip_block_type block_type,
898 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899{
900 int i, r = 0;
901
902 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400903 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400904 continue;
Rex Zhuc7228652017-02-22 15:33:46 +0800905 if (adev->ip_blocks[i].version->type != block_type)
906 continue;
907 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
908 continue;
909 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
910 (void *)adev, state);
911 if (r)
912 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
913 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400914 }
915 return r;
916}
917
Alex Deucher2990a1f2017-12-15 16:18:00 -0500918int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
919 enum amd_ip_block_type block_type,
920 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400921{
922 int i, r = 0;
923
924 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400925 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400926 continue;
Rex Zhuc7228652017-02-22 15:33:46 +0800927 if (adev->ip_blocks[i].version->type != block_type)
928 continue;
929 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
930 continue;
931 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
932 (void *)adev, state);
933 if (r)
934 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
935 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936 }
937 return r;
938}
939
Alex Deucher2990a1f2017-12-15 16:18:00 -0500940void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
941 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +0800942{
943 int i;
944
945 for (i = 0; i < adev->num_ip_blocks; i++) {
946 if (!adev->ip_blocks[i].status.valid)
947 continue;
948 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
949 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
950 }
951}
952
Alex Deucher2990a1f2017-12-15 16:18:00 -0500953int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
954 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -0400955{
956 int i, r;
957
958 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400959 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400960 continue;
Alex Deuchera1255102016-10-13 17:41:13 -0400961 if (adev->ip_blocks[i].version->type == block_type) {
962 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -0400963 if (r)
964 return r;
965 break;
966 }
967 }
968 return 0;
969
970}
971
Alex Deucher2990a1f2017-12-15 16:18:00 -0500972bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
973 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -0400974{
975 int i;
976
977 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400978 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400979 continue;
Alex Deuchera1255102016-10-13 17:41:13 -0400980 if (adev->ip_blocks[i].version->type == block_type)
981 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -0400982 }
983 return true;
984
985}
986
Alex Deucher2990a1f2017-12-15 16:18:00 -0500987struct amdgpu_ip_block *
988amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
989 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990{
991 int i;
992
993 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -0400994 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 return &adev->ip_blocks[i];
996
997 return NULL;
998}
999
1000/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001001 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001002 *
1003 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001004 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005 * @major: major version
1006 * @minor: minor version
1007 *
1008 * return 0 if equal or greater
1009 * return 1 if smaller or the ip_block doesn't exist
1010 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001011int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1012 enum amd_ip_block_type type,
1013 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001014{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001015 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016
Alex Deuchera1255102016-10-13 17:41:13 -04001017 if (ip_block && ((ip_block->version->major > major) ||
1018 ((ip_block->version->major == major) &&
1019 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 return 0;
1021
1022 return 1;
1023}
1024
Alex Deuchera1255102016-10-13 17:41:13 -04001025/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001026 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001027 *
1028 * @adev: amdgpu_device pointer
1029 * @ip_block_version: pointer to the IP to add
1030 *
1031 * Adds the IP block driver information to the collection of IPs
1032 * on the asic.
1033 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001034int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1035 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001036{
1037 if (!ip_block_version)
1038 return -EINVAL;
1039
Shaoyun Liue966a722018-02-01 16:45:26 -05001040 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001041 ip_block_version->funcs->name);
1042
Alex Deuchera1255102016-10-13 17:41:13 -04001043 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1044
1045 return 0;
1046}
1047
Alex Deucher483ef982016-09-30 12:43:04 -04001048static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001049{
1050 adev->enable_virtual_display = false;
1051
1052 if (amdgpu_virtual_display) {
1053 struct drm_device *ddev = adev->ddev;
1054 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001055 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001056
1057 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1058 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001059 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1060 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001061 if (!strcmp("all", pciaddname)
1062 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001063 long num_crtc;
1064 int res = -1;
1065
Emily Deng9accf2f2016-08-10 16:01:25 +08001066 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001067
1068 if (pciaddname_tmp)
1069 res = kstrtol(pciaddname_tmp, 10,
1070 &num_crtc);
1071
1072 if (!res) {
1073 if (num_crtc < 1)
1074 num_crtc = 1;
1075 if (num_crtc > 6)
1076 num_crtc = 6;
1077 adev->mode_info.num_crtc = num_crtc;
1078 } else {
1079 adev->mode_info.num_crtc = 1;
1080 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001081 break;
1082 }
1083 }
1084
Emily Deng0f663562016-09-30 13:02:18 -04001085 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1086 amdgpu_virtual_display, pci_address_name,
1087 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001088
1089 kfree(pciaddstr);
1090 }
1091}
1092
Alex Deuchere2a75f82017-04-27 16:58:01 -04001093static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1094{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001095 const char *chip_name;
1096 char fw_name[30];
1097 int err;
1098 const struct gpu_info_firmware_header_v1_0 *hdr;
1099
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001100 adev->firmware.gpu_info_fw = NULL;
1101
Alex Deuchere2a75f82017-04-27 16:58:01 -04001102 switch (adev->asic_type) {
1103 case CHIP_TOPAZ:
1104 case CHIP_TONGA:
1105 case CHIP_FIJI:
1106 case CHIP_POLARIS11:
1107 case CHIP_POLARIS10:
1108 case CHIP_POLARIS12:
1109 case CHIP_CARRIZO:
1110 case CHIP_STONEY:
1111#ifdef CONFIG_DRM_AMDGPU_SI
1112 case CHIP_VERDE:
1113 case CHIP_TAHITI:
1114 case CHIP_PITCAIRN:
1115 case CHIP_OLAND:
1116 case CHIP_HAINAN:
1117#endif
1118#ifdef CONFIG_DRM_AMDGPU_CIK
1119 case CHIP_BONAIRE:
1120 case CHIP_HAWAII:
1121 case CHIP_KAVERI:
1122 case CHIP_KABINI:
1123 case CHIP_MULLINS:
1124#endif
1125 default:
1126 return 0;
1127 case CHIP_VEGA10:
1128 chip_name = "vega10";
1129 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001130 case CHIP_RAVEN:
1131 chip_name = "raven";
1132 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001133 }
1134
1135 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001136 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001137 if (err) {
1138 dev_err(adev->dev,
1139 "Failed to load gpu_info firmware \"%s\"\n",
1140 fw_name);
1141 goto out;
1142 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001143 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001144 if (err) {
1145 dev_err(adev->dev,
1146 "Failed to validate gpu_info firmware \"%s\"\n",
1147 fw_name);
1148 goto out;
1149 }
1150
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001151 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001152 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1153
1154 switch (hdr->version_major) {
1155 case 1:
1156 {
1157 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001158 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001159 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1160
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001161 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1162 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1163 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1164 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 -04001165 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001166 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1167 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1168 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1169 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1170 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001171 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001172 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1173 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001174 adev->gfx.cu_info.max_waves_per_simd =
1175 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1176 adev->gfx.cu_info.max_scratch_slots_per_cu =
1177 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1178 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001179 break;
1180 }
1181 default:
1182 dev_err(adev->dev,
1183 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1184 err = -EINVAL;
1185 goto out;
1186 }
1187out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001188 return err;
1189}
1190
Alex Deucher06ec9072017-12-14 15:02:39 -05001191static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001193 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001194
Alex Deucher483ef982016-09-30 12:43:04 -04001195 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001196
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001198 case CHIP_TOPAZ:
1199 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001200 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001201 case CHIP_POLARIS11:
1202 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001203 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001204 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001205 case CHIP_STONEY:
1206 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001207 adev->family = AMDGPU_FAMILY_CZ;
1208 else
1209 adev->family = AMDGPU_FAMILY_VI;
1210
1211 r = vi_set_ip_blocks(adev);
1212 if (r)
1213 return r;
1214 break;
Ken Wang33f34802016-01-21 17:29:41 +08001215#ifdef CONFIG_DRM_AMDGPU_SI
1216 case CHIP_VERDE:
1217 case CHIP_TAHITI:
1218 case CHIP_PITCAIRN:
1219 case CHIP_OLAND:
1220 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001221 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001222 r = si_set_ip_blocks(adev);
1223 if (r)
1224 return r;
1225 break;
1226#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001227#ifdef CONFIG_DRM_AMDGPU_CIK
1228 case CHIP_BONAIRE:
1229 case CHIP_HAWAII:
1230 case CHIP_KAVERI:
1231 case CHIP_KABINI:
1232 case CHIP_MULLINS:
1233 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1234 adev->family = AMDGPU_FAMILY_CI;
1235 else
1236 adev->family = AMDGPU_FAMILY_KV;
1237
1238 r = cik_set_ip_blocks(adev);
1239 if (r)
1240 return r;
1241 break;
1242#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001243 case CHIP_VEGA10:
1244 case CHIP_RAVEN:
1245 if (adev->asic_type == CHIP_RAVEN)
1246 adev->family = AMDGPU_FAMILY_RV;
1247 else
1248 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001249
1250 r = soc15_set_ip_blocks(adev);
1251 if (r)
1252 return r;
1253 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001254 default:
1255 /* FIXME: not supported yet */
1256 return -EINVAL;
1257 }
1258
Alex Deuchere2a75f82017-04-27 16:58:01 -04001259 r = amdgpu_device_parse_gpu_info_fw(adev);
1260 if (r)
1261 return r;
1262
pding18847342017-11-06 10:21:26 +08001263 amdgpu_amdkfd_device_probe(adev);
1264
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001265 if (amdgpu_sriov_vf(adev)) {
1266 r = amdgpu_virt_request_full_gpu(adev, true);
1267 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001268 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001269 }
1270
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001271 for (i = 0; i < adev->num_ip_blocks; i++) {
1272 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001273 DRM_ERROR("disabled ip block: %d <%s>\n",
1274 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001275 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001277 if (adev->ip_blocks[i].version->funcs->early_init) {
1278 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001279 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001280 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001281 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001282 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1283 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001285 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001286 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001287 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001288 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001289 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001291 }
1292 }
1293
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001294 adev->cg_flags &= amdgpu_cg_mask;
1295 adev->pg_flags &= amdgpu_pg_mask;
1296
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297 return 0;
1298}
1299
Alex Deucher06ec9072017-12-14 15:02:39 -05001300static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301{
1302 int i, r;
1303
1304 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001305 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001307 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001308 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001309 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1310 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001311 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001312 }
Alex Deuchera1255102016-10-13 17:41:13 -04001313 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001314
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001316 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001317 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001318 if (r) {
1319 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001321 }
Alex Deuchera1255102016-10-13 17:41:13 -04001322 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001323 if (r) {
1324 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001325 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001326 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001327 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001328 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001329 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001330 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001331 }
Alex Deuchera1255102016-10-13 17:41:13 -04001332 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001333
1334 /* right after GMC hw init, we create CSA */
1335 if (amdgpu_sriov_vf(adev)) {
1336 r = amdgpu_allocate_static_csa(adev);
1337 if (r) {
1338 DRM_ERROR("allocate CSA failed %d\n", r);
1339 return r;
1340 }
1341 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001342 }
1343 }
1344
1345 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001346 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001347 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001348 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001350 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001351 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001352 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1353 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001354 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001355 }
Alex Deuchera1255102016-10-13 17:41:13 -04001356 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001357 }
1358
pding18847342017-11-06 10:21:26 +08001359 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001360
1361 if (amdgpu_sriov_vf(adev))
1362 amdgpu_virt_release_full_gpu(adev, true);
1363
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001364 return 0;
1365}
1366
Alex Deucher06ec9072017-12-14 15:02:39 -05001367static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001368{
1369 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1370}
1371
Alex Deucher06ec9072017-12-14 15:02:39 -05001372static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001373{
1374 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1375 AMDGPU_RESET_MAGIC_NUM);
1376}
1377
Alex Deucher06ec9072017-12-14 15:02:39 -05001378static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301379{
1380 int i = 0, r;
1381
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001382 if (amdgpu_emu_mode == 1)
1383 return 0;
1384
Shirish S2dc80b02017-05-25 10:05:25 +05301385 for (i = 0; i < adev->num_ip_blocks; i++) {
1386 if (!adev->ip_blocks[i].status.valid)
1387 continue;
1388 /* skip CG for VCE/UVD, it's handled specially */
1389 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1390 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1391 /* enable clockgating to save power */
1392 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1393 AMD_CG_STATE_GATE);
1394 if (r) {
1395 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1396 adev->ip_blocks[i].version->funcs->name, r);
1397 return r;
1398 }
1399 }
1400 }
1401 return 0;
1402}
1403
Alex Deucher06ec9072017-12-14 15:02:39 -05001404static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405{
1406 int i = 0, r;
1407
1408 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001409 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001411 if (adev->ip_blocks[i].version->funcs->late_init) {
1412 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001413 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001414 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1415 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001416 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001417 }
Alex Deuchera1255102016-10-13 17:41:13 -04001418 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419 }
1420 }
1421
Shirish S2dc80b02017-05-25 10:05:25 +05301422 mod_delayed_work(system_wq, &adev->late_init_work,
1423 msecs_to_jiffies(AMDGPU_RESUME_MS));
1424
Alex Deucher06ec9072017-12-14 15:02:39 -05001425 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001426
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001427 return 0;
1428}
1429
Alex Deucher06ec9072017-12-14 15:02:39 -05001430static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001431{
1432 int i, r;
1433
pding18847342017-11-06 10:21:26 +08001434 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001435 /* need to disable SMC first */
1436 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001437 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001438 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001439 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001440 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001441 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1442 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001443 if (r) {
1444 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001445 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001446 return r;
1447 }
Alex Deuchera1255102016-10-13 17:41:13 -04001448 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001449 /* XXX handle errors */
1450 if (r) {
1451 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001452 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001453 }
Alex Deuchera1255102016-10-13 17:41:13 -04001454 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001455 break;
1456 }
1457 }
1458
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001459 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001460 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001461 continue;
Rex Zhu8201a672016-11-24 21:44:44 +08001462
1463 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1464 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1465 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1466 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1467 AMD_CG_STATE_UNGATE);
1468 if (r) {
1469 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1470 adev->ip_blocks[i].version->funcs->name, r);
1471 return r;
1472 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001473 }
Rex Zhu8201a672016-11-24 21:44:44 +08001474
Alex Deuchera1255102016-10-13 17:41:13 -04001475 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001477 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001478 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1479 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001480 }
Rex Zhu8201a672016-11-24 21:44:44 +08001481
Alex Deuchera1255102016-10-13 17:41:13 -04001482 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001483 }
1484
Alex Deucher9950cda2018-01-18 19:05:36 -05001485 /* disable all interrupts */
1486 amdgpu_irq_disable_all(adev);
1487
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001489 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001490 continue;
Monk Liuc12aba32018-01-24 12:20:32 +08001491
1492 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1493 amdgpu_free_static_csa(adev);
1494 amdgpu_device_wb_fini(adev);
1495 amdgpu_device_vram_scratch_fini(adev);
1496 }
1497
Alex Deuchera1255102016-10-13 17:41:13 -04001498 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001499 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001500 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001501 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1502 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001503 }
Alex Deuchera1255102016-10-13 17:41:13 -04001504 adev->ip_blocks[i].status.sw = false;
1505 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001506 }
1507
Monk Liua6dcfd92016-05-19 14:36:34 +08001508 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001509 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001510 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001511 if (adev->ip_blocks[i].version->funcs->late_fini)
1512 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1513 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001514 }
1515
Monk Liu030308f2017-09-15 15:34:52 +08001516 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001517 if (amdgpu_virt_release_full_gpu(adev, false))
1518 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001519
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001520 return 0;
1521}
1522
Alex Deucher06ec9072017-12-14 15:02:39 -05001523static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301524{
1525 struct amdgpu_device *adev =
1526 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001527 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301528}
1529
Alex Deuchercdd61df2017-12-14 16:47:40 -05001530int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001531{
1532 int i, r;
1533
Xiangliang Yue941ea92017-01-18 12:47:55 +08001534 if (amdgpu_sriov_vf(adev))
1535 amdgpu_virt_request_full_gpu(adev, false);
1536
Flora Cuic5a93a22016-02-26 10:45:25 +08001537 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001538 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1539 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001540 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001541 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001542 }
1543
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001544 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001545 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546 continue;
1547 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001548 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001549 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1550 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001551 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001552 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1553 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001554 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001555 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001557 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001558 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001559 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001560 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1561 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001562 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001563 }
1564
Xiangliang Yue941ea92017-01-18 12:47:55 +08001565 if (amdgpu_sriov_vf(adev))
1566 amdgpu_virt_release_full_gpu(adev, false);
1567
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001568 return 0;
1569}
1570
Alex Deucher06ec9072017-12-14 15:02:39 -05001571static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001572{
1573 int i, r;
1574
Monk Liu2cb681b2017-04-26 12:00:49 +08001575 static enum amd_ip_block_type ip_order[] = {
1576 AMD_IP_BLOCK_TYPE_GMC,
1577 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001578 AMD_IP_BLOCK_TYPE_IH,
1579 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001580
Monk Liu2cb681b2017-04-26 12:00:49 +08001581 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1582 int j;
1583 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001584
Monk Liu2cb681b2017-04-26 12:00:49 +08001585 for (j = 0; j < adev->num_ip_blocks; j++) {
1586 block = &adev->ip_blocks[j];
1587
1588 if (block->version->type != ip_order[i] ||
1589 !block->status.valid)
1590 continue;
1591
1592 r = block->version->funcs->hw_init(adev);
1593 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001594 if (r)
1595 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001596 }
1597 }
1598
1599 return 0;
1600}
1601
Alex Deucher06ec9072017-12-14 15:02:39 -05001602static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001603{
1604 int i, r;
1605
Monk Liu2cb681b2017-04-26 12:00:49 +08001606 static enum amd_ip_block_type ip_order[] = {
1607 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001608 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001609 AMD_IP_BLOCK_TYPE_DCE,
1610 AMD_IP_BLOCK_TYPE_GFX,
1611 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001612 AMD_IP_BLOCK_TYPE_UVD,
1613 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001614 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001615
Monk Liu2cb681b2017-04-26 12:00:49 +08001616 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1617 int j;
1618 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001619
Monk Liu2cb681b2017-04-26 12:00:49 +08001620 for (j = 0; j < adev->num_ip_blocks; j++) {
1621 block = &adev->ip_blocks[j];
1622
1623 if (block->version->type != ip_order[i] ||
1624 !block->status.valid)
1625 continue;
1626
1627 r = block->version->funcs->hw_init(adev);
1628 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001629 if (r)
1630 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001631 }
1632 }
1633
1634 return 0;
1635}
1636
Alex Deucher06ec9072017-12-14 15:02:39 -05001637static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638{
1639 int i, r;
1640
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641 for (i = 0; i < adev->num_ip_blocks; i++) {
1642 if (!adev->ip_blocks[i].status.valid)
1643 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001644 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1645 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1646 adev->ip_blocks[i].version->type ==
1647 AMD_IP_BLOCK_TYPE_IH) {
1648 r = adev->ip_blocks[i].version->funcs->resume(adev);
1649 if (r) {
1650 DRM_ERROR("resume of IP block <%s> failed %d\n",
1651 adev->ip_blocks[i].version->funcs->name, r);
1652 return r;
1653 }
1654 }
1655 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656
Chunming Zhoufcf06492017-05-05 10:33:33 +08001657 return 0;
1658}
1659
Alex Deucher06ec9072017-12-14 15:02:39 -05001660static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08001661{
1662 int i, r;
1663
1664 for (i = 0; i < adev->num_ip_blocks; i++) {
1665 if (!adev->ip_blocks[i].status.valid)
1666 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1668 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1669 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1670 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001671 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 if (r) {
1673 DRM_ERROR("resume of IP block <%s> failed %d\n",
1674 adev->ip_blocks[i].version->funcs->name, r);
1675 return r;
1676 }
1677 }
1678
1679 return 0;
1680}
1681
Alex Deucher06ec9072017-12-14 15:02:39 -05001682static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683{
Chunming Zhoufcf06492017-05-05 10:33:33 +08001684 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685
Alex Deucher06ec9072017-12-14 15:02:39 -05001686 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08001687 if (r)
1688 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05001689 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001690
Chunming Zhoufcf06492017-05-05 10:33:33 +08001691 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001692}
1693
Monk Liu4e99a442016-03-31 13:26:59 +08001694static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001695{
Monk Liu6867e1b2017-10-16 19:50:44 +08001696 if (amdgpu_sriov_vf(adev)) {
1697 if (adev->is_atom_fw) {
1698 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1699 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1700 } else {
1701 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1702 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1703 }
1704
1705 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
1706 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001707 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001708}
1709
Harry Wentland45622362017-09-12 15:58:20 -04001710bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
1711{
1712 switch (asic_type) {
1713#if defined(CONFIG_DRM_AMD_DC)
1714 case CHIP_BONAIRE:
1715 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04001716 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05001717 case CHIP_KABINI:
1718 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04001719 case CHIP_CARRIZO:
1720 case CHIP_STONEY:
1721 case CHIP_POLARIS11:
1722 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04001723 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04001724 case CHIP_TONGA:
1725 case CHIP_FIJI:
1726#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
1727 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04001728#endif
Harry Wentland42f8ffa2017-09-15 14:07:30 -04001729 case CHIP_VEGA10:
1730#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08001731 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04001732#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08001733 return amdgpu_dc != 0;
1734#endif
Harry Wentland45622362017-09-12 15:58:20 -04001735 default:
1736 return false;
1737 }
1738}
1739
1740/**
1741 * amdgpu_device_has_dc_support - check if dc is supported
1742 *
1743 * @adev: amdgpu_device_pointer
1744 *
1745 * Returns true for supported, false for not supported
1746 */
1747bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
1748{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08001749 if (amdgpu_sriov_vf(adev))
1750 return false;
1751
Harry Wentland45622362017-09-12 15:58:20 -04001752 return amdgpu_device_asic_has_dc_support(adev->asic_type);
1753}
1754
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001755/**
1756 * amdgpu_device_init - initialize the driver
1757 *
1758 * @adev: amdgpu_device pointer
1759 * @pdev: drm dev pointer
1760 * @pdev: pci dev pointer
1761 * @flags: driver flags
1762 *
1763 * Initializes the driver info and hw (all asics).
1764 * Returns 0 for success or an error on failure.
1765 * Called at driver startup.
1766 */
1767int amdgpu_device_init(struct amdgpu_device *adev,
1768 struct drm_device *ddev,
1769 struct pci_dev *pdev,
1770 uint32_t flags)
1771{
1772 int r, i;
1773 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001774 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775
1776 adev->shutdown = false;
1777 adev->dev = &pdev->dev;
1778 adev->ddev = ddev;
1779 adev->pdev = pdev;
1780 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001781 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001782 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05001783 if (amdgpu_emu_mode == 1)
1784 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01001785 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001786 adev->accel_working = false;
1787 adev->num_rings = 0;
1788 adev->mman.buffer_funcs = NULL;
1789 adev->mman.buffer_funcs_ring = NULL;
1790 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001791 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01001792 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001793 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04001794 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795
1796 adev->smc_rreg = &amdgpu_invalid_rreg;
1797 adev->smc_wreg = &amdgpu_invalid_wreg;
1798 adev->pcie_rreg = &amdgpu_invalid_rreg;
1799 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001800 adev->pciep_rreg = &amdgpu_invalid_rreg;
1801 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001802 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1803 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1804 adev->didt_rreg = &amdgpu_invalid_rreg;
1805 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001806 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1807 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1809 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1810
Alex Deucher3e39ab92015-06-05 15:04:33 -04001811 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1812 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1813 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001814
1815 /* mutex initialization are all done here so we
1816 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001817 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05001818 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819 mutex_init(&adev->pm.mutex);
1820 mutex_init(&adev->gfx.gpu_clock_mutex);
1821 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04001822 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001823 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001824 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04001825 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001826 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08001827 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001828
Alex Deucher06ec9072017-12-14 15:02:39 -05001829 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001830
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001831 spin_lock_init(&adev->mmio_idx_lock);
1832 spin_lock_init(&adev->smc_idx_lock);
1833 spin_lock_init(&adev->pcie_idx_lock);
1834 spin_lock_init(&adev->uvd_ctx_idx_lock);
1835 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001836 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08001837 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001838 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001839 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001840
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001841 INIT_LIST_HEAD(&adev->shadow_list);
1842 mutex_init(&adev->shadow_list_lock);
1843
Andres Rodriguez795f2812017-03-06 16:27:55 -05001844 INIT_LIST_HEAD(&adev->ring_lru_list);
1845 spin_lock_init(&adev->ring_lru_list_lock);
1846
Alex Deucher06ec9072017-12-14 15:02:39 -05001847 INIT_DELAYED_WORK(&adev->late_init_work,
1848 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05301849
Alex Xie0fa49552017-06-08 14:58:05 -04001850 /* Registers mapping */
1851 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08001852 if (adev->asic_type >= CHIP_BONAIRE) {
1853 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1854 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1855 } else {
1856 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1857 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1858 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001859
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001860 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1861 if (adev->rmmio == NULL) {
1862 return -ENOMEM;
1863 }
1864 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1865 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1866
Christian König705e5192017-06-08 11:15:16 +02001867 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05001868 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001869
1870 /* io port mapping */
1871 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1872 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1873 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1874 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1875 break;
1876 }
1877 }
1878 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001879 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001880
1881 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05001882 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001883 if (r)
1884 return r;
1885
1886 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1887 /* this will fail for cards that aren't VGA class devices, just
1888 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05001889 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001890
Alex Deuchere9bef452016-04-25 13:12:18 -04001891 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001892 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01001893 if (!pci_is_thunderbolt_attached(adev->pdev))
1894 vga_switcheroo_register_client(adev->pdev,
1895 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896 if (runtime)
1897 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1898
Shaoyun Liu9475a942018-02-01 18:13:23 -05001899 if (amdgpu_emu_mode == 1) {
1900 /* post the asic on emulation mode */
1901 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05001902 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05001903 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05001904
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001906 if (!amdgpu_get_bios(adev)) {
1907 r = -EINVAL;
1908 goto failed;
1909 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001910
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001912 if (r) {
1913 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001914 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04001915 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001916 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001917
Monk Liu4e99a442016-03-31 13:26:59 +08001918 /* detect if we are with an SRIOV vbios */
1919 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001920
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001921 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05001922 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001924 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001925 r = -EINVAL;
1926 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001927 }
Monk Liubec86372016-09-14 19:38:08 +08001928 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001929 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1930 if (r) {
1931 dev_err(adev->dev, "gpu post error!\n");
1932 goto failed;
1933 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001934 }
1935
Alex Deucher88b64e92017-07-10 10:43:10 -04001936 if (adev->is_atom_fw) {
1937 /* Initialize clocks */
1938 r = amdgpu_atomfirmware_get_clock_info(adev);
1939 if (r) {
1940 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001941 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04001942 goto failed;
1943 }
1944 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001945 /* Initialize clocks */
1946 r = amdgpu_atombios_get_clock_info(adev);
1947 if (r) {
1948 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001949 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04001950 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001951 }
1952 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04001953 if (!amdgpu_device_has_dc_support(adev))
1954 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001955 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001956
Shaoyun Liubfca0282018-02-01 17:37:50 -05001957fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958 /* Fence driver */
1959 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001960 if (r) {
1961 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001962 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04001963 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001964 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001965
1966 /* init the mode config */
1967 drm_mode_config_init(adev->ddev);
1968
Alex Deucher06ec9072017-12-14 15:02:39 -05001969 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001970 if (r) {
pding8840a382017-10-23 17:22:09 +08001971 /* failed in exclusive mode due to timeout */
1972 if (amdgpu_sriov_vf(adev) &&
1973 !amdgpu_sriov_runtime(adev) &&
1974 amdgpu_virt_mmio_blocked(adev) &&
1975 !amdgpu_virt_wait_reset(adev)) {
1976 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08001977 /* Don't send request since VF is inactive. */
1978 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
1979 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08001980 r = -EAGAIN;
1981 goto failed;
1982 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001983 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001984 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher06ec9072017-12-14 15:02:39 -05001985 amdgpu_device_ip_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001986 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001987 }
1988
1989 adev->accel_working = true;
1990
Alex Xiee59c0202017-06-01 09:42:59 -04001991 amdgpu_vm_check_compute_bug(adev);
1992
Marek Olšák95844d22016-08-17 23:49:27 +02001993 /* Initialize the buffer migration limit. */
1994 if (amdgpu_moverate >= 0)
1995 max_MBps = amdgpu_moverate;
1996 else
1997 max_MBps = 8; /* Allow 8 MB/s. */
1998 /* Get a log2 for easy divisions. */
1999 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2000
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002001 r = amdgpu_ib_pool_init(adev);
2002 if (r) {
2003 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002004 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002005 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006 }
2007
2008 r = amdgpu_ib_ring_tests(adev);
2009 if (r)
2010 DRM_ERROR("ib ring test failed (%d).\n", r);
2011
Horace Chen2dc8f812017-10-09 16:17:16 +08002012 if (amdgpu_sriov_vf(adev))
2013 amdgpu_virt_init_data_exchange(adev);
2014
Monk Liu9bc92b92017-02-08 17:38:13 +08002015 amdgpu_fbdev_init(adev);
2016
Rex Zhud2f52ac2017-09-22 17:47:27 +08002017 r = amdgpu_pm_sysfs_init(adev);
2018 if (r)
2019 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2020
Alex Deucher75758252017-12-14 15:23:14 -05002021 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002022 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002023 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002024
2025 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002026 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028
Huang Rui50ab2532016-06-12 15:51:09 +08002029 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002030 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002031 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002032
Christian König763efb62017-12-06 15:44:51 +01002033 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002034 if (r)
Christian König763efb62017-12-06 15:44:51 +01002035 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002036
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037 if ((amdgpu_testing & 1)) {
2038 if (adev->accel_working)
2039 amdgpu_test_moves(adev);
2040 else
2041 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2042 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002043 if (amdgpu_benchmarking) {
2044 if (adev->accel_working)
2045 amdgpu_benchmark(adev, amdgpu_benchmarking);
2046 else
2047 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2048 }
2049
2050 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2051 * explicit gating rather than handling it automatically.
2052 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002053 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002054 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002055 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002056 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002057 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002058 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059
2060 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002061
2062failed:
Gavin Wan89041942017-06-23 13:55:15 -04002063 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002064 if (runtime)
2065 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002066
Alex Deucher83ba1262016-06-03 18:21:41 -04002067 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068}
2069
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002070/**
2071 * amdgpu_device_fini - tear down the driver
2072 *
2073 * @adev: amdgpu_device pointer
2074 *
2075 * Tear down the driver info (all asics).
2076 * Called at driver shutdown.
2077 */
2078void amdgpu_device_fini(struct amdgpu_device *adev)
2079{
2080 int r;
2081
2082 DRM_INFO("amdgpu: finishing device.\n");
2083 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002084 if (adev->mode_info.mode_config_initialized)
2085 drm_crtc_force_disable_all(adev->ddev);
Monk Liub9141cd2017-11-22 19:21:43 +08002086
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087 amdgpu_ib_pool_fini(adev);
2088 amdgpu_fence_driver_fini(adev);
Emily Deng58e955d2018-03-08 09:35:19 +08002089 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002091 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002092 if (adev->firmware.gpu_info_fw) {
2093 release_firmware(adev->firmware.gpu_info_fw);
2094 adev->firmware.gpu_info_fw = NULL;
2095 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002096 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302097 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002098 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002099 if (!amdgpu_device_has_dc_support(adev))
2100 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002101
2102 if (amdgpu_emu_mode != 1)
2103 amdgpu_atombios_fini(adev);
2104
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002105 kfree(adev->bios);
2106 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002107 if (!pci_is_thunderbolt_attached(adev->pdev))
2108 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002109 if (adev->flags & AMD_IS_PX)
2110 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 vga_client_register(adev->pdev, NULL, NULL, NULL);
2112 if (adev->rio_mem)
2113 pci_iounmap(adev->pdev, adev->rio_mem);
2114 adev->rio_mem = NULL;
2115 iounmap(adev->rmmio);
2116 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002117 amdgpu_device_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002118 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002119}
2120
2121
2122/*
2123 * Suspend & resume.
2124 */
2125/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002126 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002127 *
2128 * @pdev: drm dev pointer
2129 * @state: suspend state
2130 *
2131 * Puts the hw in the suspend state (all asics).
2132 * Returns 0 for success or an error on failure.
2133 * Called at driver suspend.
2134 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002135int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002136{
2137 struct amdgpu_device *adev;
2138 struct drm_crtc *crtc;
2139 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002140 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002141
2142 if (dev == NULL || dev->dev_private == NULL) {
2143 return -ENODEV;
2144 }
2145
2146 adev = dev->dev_private;
2147
2148 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2149 return 0;
2150
2151 drm_kms_helper_poll_disable(dev);
2152
Harry Wentland45622362017-09-12 15:58:20 -04002153 if (!amdgpu_device_has_dc_support(adev)) {
2154 /* turn off display hw */
2155 drm_modeset_lock_all(dev);
2156 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2157 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2158 }
2159 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002160 }
2161
Yong Zhaoba997702015-11-09 17:21:45 -05002162 amdgpu_amdkfd_suspend(adev);
2163
Alex Deucher756e6882015-10-08 00:03:36 -04002164 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002165 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002166 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002167 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2168 struct amdgpu_bo *robj;
2169
Alex Deucher756e6882015-10-08 00:03:36 -04002170 if (amdgpu_crtc->cursor_bo) {
2171 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002172 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002173 if (r == 0) {
2174 amdgpu_bo_unpin(aobj);
2175 amdgpu_bo_unreserve(aobj);
2176 }
2177 }
2178
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179 if (rfb == NULL || rfb->obj == NULL) {
2180 continue;
2181 }
2182 robj = gem_to_amdgpu_bo(rfb->obj);
2183 /* don't unpin kernel fb objects */
2184 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002185 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 if (r == 0) {
2187 amdgpu_bo_unpin(robj);
2188 amdgpu_bo_unreserve(robj);
2189 }
2190 }
2191 }
2192 /* evict vram memory */
2193 amdgpu_bo_evict_vram(adev);
2194
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002195 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002196
Alex Deuchercdd61df2017-12-14 16:47:40 -05002197 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198
Alex Deuchera0a71e42016-10-10 12:41:36 -04002199 /* evict remaining vram memory
2200 * This second call to evict vram is to evict the gart page table
2201 * using the CPU.
2202 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203 amdgpu_bo_evict_vram(adev);
2204
2205 pci_save_state(dev->pdev);
2206 if (suspend) {
2207 /* Shut down the device */
2208 pci_disable_device(dev->pdev);
2209 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002210 } else {
2211 r = amdgpu_asic_reset(adev);
2212 if (r)
2213 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 }
2215
2216 if (fbcon) {
2217 console_lock();
2218 amdgpu_fbdev_set_suspend(adev, 1);
2219 console_unlock();
2220 }
2221 return 0;
2222}
2223
2224/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002225 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002226 *
2227 * @pdev: drm dev pointer
2228 *
2229 * Bring the hw back to operating state (all asics).
2230 * Returns 0 for success or an error on failure.
2231 * Called at driver resume.
2232 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002233int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002234{
2235 struct drm_connector *connector;
2236 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002237 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002238 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002239
2240 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2241 return 0;
2242
jimqu74b0b152016-09-07 17:09:12 +08002243 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002244 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002245
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002246 if (resume) {
2247 pci_set_power_state(dev->pdev, PCI_D0);
2248 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002249 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002250 if (r)
2251 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002252 }
2253
2254 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002255 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002256 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2257 if (r)
2258 DRM_ERROR("amdgpu asic init failed\n");
2259 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002260
Alex Deucher06ec9072017-12-14 15:02:39 -05002261 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002262 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002263 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002264 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002265 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002266 amdgpu_fence_driver_resume(adev);
2267
Flora Cuica198522016-02-04 15:10:08 +08002268 if (resume) {
2269 r = amdgpu_ib_ring_tests(adev);
2270 if (r)
2271 DRM_ERROR("ib ring test failed (%d).\n", r);
2272 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002273
Alex Deucher06ec9072017-12-14 15:02:39 -05002274 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002275 if (r)
2276 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002277
Alex Deucher756e6882015-10-08 00:03:36 -04002278 /* pin cursors */
2279 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2280 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2281
2282 if (amdgpu_crtc->cursor_bo) {
2283 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002284 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002285 if (r == 0) {
2286 r = amdgpu_bo_pin(aobj,
2287 AMDGPU_GEM_DOMAIN_VRAM,
2288 &amdgpu_crtc->cursor_addr);
2289 if (r != 0)
2290 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2291 amdgpu_bo_unreserve(aobj);
2292 }
2293 }
2294 }
Yong Zhaoba997702015-11-09 17:21:45 -05002295 r = amdgpu_amdkfd_resume(adev);
2296 if (r)
2297 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002298
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002299 /* blat the mode back in */
2300 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002301 if (!amdgpu_device_has_dc_support(adev)) {
2302 /* pre DCE11 */
2303 drm_helper_resume_force_mode(dev);
2304
2305 /* turn on display hw */
2306 drm_modeset_lock_all(dev);
2307 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2308 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2309 }
2310 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002311 }
2312 }
2313
2314 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002315
2316 /*
2317 * Most of the connector probing functions try to acquire runtime pm
2318 * refs to ensure that the GPU is powered on when connector polling is
2319 * performed. Since we're calling this from a runtime PM callback,
2320 * trying to acquire rpm refs will cause us to deadlock.
2321 *
2322 * Since we're guaranteed to be holding the rpm lock, it's safe to
2323 * temporarily disable the rpm helpers so this doesn't deadlock us.
2324 */
2325#ifdef CONFIG_PM
2326 dev->dev->power.disable_depth++;
2327#endif
Harry Wentland45622362017-09-12 15:58:20 -04002328 if (!amdgpu_device_has_dc_support(adev))
2329 drm_helper_hpd_irq_event(dev);
2330 else
2331 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002332#ifdef CONFIG_PM
2333 dev->dev->power.disable_depth--;
2334#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002335
Huang Rui03161a62017-04-13 16:12:26 +08002336 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002337 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002338
Huang Rui03161a62017-04-13 16:12:26 +08002339unlock:
2340 if (fbcon)
2341 console_unlock();
2342
2343 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002344}
2345
Alex Deucher06ec9072017-12-14 15:02:39 -05002346static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002347{
2348 int i;
2349 bool asic_hang = false;
2350
Monk Liuf993d622017-10-16 19:46:01 +08002351 if (amdgpu_sriov_vf(adev))
2352 return true;
2353
Chunming Zhou63fbf422016-07-15 11:19:20 +08002354 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002355 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002356 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002357 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2358 adev->ip_blocks[i].status.hang =
2359 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2360 if (adev->ip_blocks[i].status.hang) {
2361 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002362 asic_hang = true;
2363 }
2364 }
2365 return asic_hang;
2366}
2367
Alex Deucher06ec9072017-12-14 15:02:39 -05002368static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002369{
2370 int i, r = 0;
2371
2372 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002373 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002374 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002375 if (adev->ip_blocks[i].status.hang &&
2376 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2377 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002378 if (r)
2379 return r;
2380 }
2381 }
2382
2383 return 0;
2384}
2385
Alex Deucher06ec9072017-12-14 15:02:39 -05002386static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002387{
Alex Deucherda146d32016-10-13 16:07:03 -04002388 int i;
2389
2390 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002391 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002392 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002393 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2394 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2395 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002396 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2397 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002398 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002399 DRM_INFO("Some block need full reset!\n");
2400 return true;
2401 }
2402 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002403 }
2404 return false;
2405}
2406
Alex Deucher06ec9072017-12-14 15:02:39 -05002407static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002408{
2409 int i, r = 0;
2410
2411 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002412 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002413 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002414 if (adev->ip_blocks[i].status.hang &&
2415 adev->ip_blocks[i].version->funcs->soft_reset) {
2416 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002417 if (r)
2418 return r;
2419 }
2420 }
2421
2422 return 0;
2423}
2424
Alex Deucher06ec9072017-12-14 15:02:39 -05002425static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002426{
2427 int i, r = 0;
2428
2429 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002430 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002431 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002432 if (adev->ip_blocks[i].status.hang &&
2433 adev->ip_blocks[i].version->funcs->post_soft_reset)
2434 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002435 if (r)
2436 return r;
2437 }
2438
2439 return 0;
2440}
2441
Alex Deucher06ec9072017-12-14 15:02:39 -05002442static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2443 struct amdgpu_ring *ring,
2444 struct amdgpu_bo *bo,
2445 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002446{
2447 uint32_t domain;
2448 int r;
2449
Roger.He23d2e502017-04-21 14:24:26 +08002450 if (!bo->shadow)
2451 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002452
Alex Xie1d284792017-04-24 13:53:04 -04002453 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002454 if (r)
2455 return r;
2456 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2457 /* if bo has been evicted, then no need to recover */
2458 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002459 r = amdgpu_bo_validate(bo->shadow);
2460 if (r) {
2461 DRM_ERROR("bo validate failed!\n");
2462 goto err;
2463 }
2464
Roger.He23d2e502017-04-21 14:24:26 +08002465 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002466 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002467 if (r) {
2468 DRM_ERROR("recover page table failed!\n");
2469 goto err;
2470 }
2471 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002472err:
Roger.He23d2e502017-04-21 14:24:26 +08002473 amdgpu_bo_unreserve(bo);
2474 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002475}
2476
Monk Liuc41d1cf2017-12-25 11:59:27 +08002477static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2478{
2479 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2480 struct amdgpu_bo *bo, *tmp;
2481 struct dma_fence *fence = NULL, *next = NULL;
2482 long r = 1;
2483 int i = 0;
2484 long tmo;
2485
2486 if (amdgpu_sriov_runtime(adev))
2487 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2488 else
2489 tmo = msecs_to_jiffies(100);
2490
2491 DRM_INFO("recover vram bo from shadow start\n");
2492 mutex_lock(&adev->shadow_list_lock);
2493 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2494 next = NULL;
2495 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
2496 if (fence) {
2497 r = dma_fence_wait_timeout(fence, false, tmo);
2498 if (r == 0)
2499 pr_err("wait fence %p[%d] timeout\n", fence, i);
2500 else if (r < 0)
2501 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2502 if (r < 1) {
2503 dma_fence_put(fence);
2504 fence = next;
2505 break;
2506 }
2507 i++;
2508 }
2509
2510 dma_fence_put(fence);
2511 fence = next;
2512 }
2513 mutex_unlock(&adev->shadow_list_lock);
2514
2515 if (fence) {
2516 r = dma_fence_wait_timeout(fence, false, tmo);
2517 if (r == 0)
2518 pr_err("wait fence %p[%d] timeout\n", fence, i);
2519 else if (r < 0)
2520 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2521
2522 }
2523 dma_fence_put(fence);
2524
2525 if (r > 0)
2526 DRM_INFO("recover vram bo from shadow done\n");
2527 else
2528 DRM_ERROR("recover vram bo from shadow failed\n");
2529
2530 return (r > 0?0:1);
2531}
2532
Monk Liu57406822017-10-25 16:37:02 +08002533/*
Alex Deucher06ec9072017-12-14 15:02:39 -05002534 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002535 *
2536 * @adev: amdgpu device pointer
Monk Liua90ad3c2017-01-23 14:22:08 +08002537 *
Monk Liu57406822017-10-25 16:37:02 +08002538 * attempt to do soft-reset or full-reset and reinitialize Asic
2539 * return 0 means successed otherwise failed
2540*/
Monk Liuc41d1cf2017-12-25 11:59:27 +08002541static int amdgpu_device_reset(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08002542{
Monk Liu57406822017-10-25 16:37:02 +08002543 bool need_full_reset, vram_lost = 0;
2544 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002545
Alex Deucher06ec9072017-12-14 15:02:39 -05002546 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002547
2548 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002549 amdgpu_device_ip_pre_soft_reset(adev);
2550 r = amdgpu_device_ip_soft_reset(adev);
2551 amdgpu_device_ip_post_soft_reset(adev);
2552 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002553 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2554 need_full_reset = true;
2555 }
2556 }
2557
2558 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05002559 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002560
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002561retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002562 r = amdgpu_asic_reset(adev);
2563 /* post card */
2564 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002565
Chunming Zhou35d782f2016-07-15 15:57:13 +08002566 if (!r) {
2567 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05002568 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002569 if (r)
2570 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002571
Alex Deucher06ec9072017-12-14 15:02:39 -05002572 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002573 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002574 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002575 atomic_inc(&adev->vram_lost_counter);
2576 }
Monk Liu57406822017-10-25 16:37:02 +08002577
Christian Königc1c7ce82017-10-16 16:50:32 +02002578 r = amdgpu_gtt_mgr_recover(
2579 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002580 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002581 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002582
Alex Deucher06ec9072017-12-14 15:02:39 -05002583 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002584 if (r)
2585 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002586
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002587 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05002588 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002589 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002590 }
Monk Liu57406822017-10-25 16:37:02 +08002591
Chunming Zhoufcf06492017-05-05 10:33:33 +08002592out:
2593 if (!r) {
2594 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002595 r = amdgpu_ib_ring_tests(adev);
2596 if (r) {
2597 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05002598 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002599 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002600 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002601 }
Monk Liu57406822017-10-25 16:37:02 +08002602 }
2603
Monk Liuc41d1cf2017-12-25 11:59:27 +08002604 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
2605 r = amdgpu_device_handle_vram_lost(adev);
Monk Liu57406822017-10-25 16:37:02 +08002606
2607 return r;
2608}
2609
2610/*
Alex Deucher06ec9072017-12-14 15:02:39 -05002611 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08002612 *
2613 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08002614 *
2615 * do VF FLR and reinitialize Asic
2616 * return 0 means successed otherwise failed
2617*/
Monk Liuc41d1cf2017-12-25 11:59:27 +08002618static int amdgpu_device_reset_sriov(struct amdgpu_device *adev, bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08002619{
2620 int r;
2621
2622 if (from_hypervisor)
2623 r = amdgpu_virt_request_full_gpu(adev, true);
2624 else
2625 r = amdgpu_virt_reset_gpu(adev);
2626 if (r)
2627 return r;
2628
2629 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05002630 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08002631 if (r)
2632 goto error;
2633
2634 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02002635 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08002636
2637 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05002638 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08002639 amdgpu_virt_release_full_gpu(adev, true);
Monk Liu57406822017-10-25 16:37:02 +08002640 if (r)
2641 goto error;
2642
2643 amdgpu_irq_gpu_reset_resume_helper(adev);
2644 r = amdgpu_ib_ring_tests(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08002645
2646 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
2647 atomic_inc(&adev->vram_lost_counter);
2648 r = amdgpu_device_handle_vram_lost(adev);
2649 }
Monk Liu57406822017-10-25 16:37:02 +08002650
2651error:
Monk Liu57406822017-10-25 16:37:02 +08002652
2653 return r;
2654}
2655
2656/**
Alex Deucher5f152b52017-12-15 16:40:49 -05002657 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08002658 *
2659 * @adev: amdgpu device pointer
2660 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05002661 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08002662 *
2663 * Attempt to reset the GPU if it has hung (all asics).
2664 * Returns 0 for success or an error on failure.
2665 */
Alex Deucher5f152b52017-12-15 16:40:49 -05002666int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
2667 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08002668{
2669 struct drm_atomic_state *state = NULL;
Monk Liu57406822017-10-25 16:37:02 +08002670 int i, r, resched;
2671
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05002672 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08002673 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2674 return 0;
2675 }
2676
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05002677 if (!force && (amdgpu_gpu_recovery == 0 ||
2678 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
2679 DRM_INFO("GPU recovery disabled.\n");
2680 return 0;
2681 }
2682
Monk Liu57406822017-10-25 16:37:02 +08002683 dev_info(adev->dev, "GPU reset begin!\n");
2684
Monk Liu13a752e2017-10-17 15:11:12 +08002685 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08002686 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08002687 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08002688
2689 /* block TTM */
2690 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Monk Liu71182662017-12-25 15:14:58 +08002691
Monk Liu57406822017-10-25 16:37:02 +08002692 /* store modesetting */
2693 if (amdgpu_device_has_dc_support(adev))
2694 state = drm_atomic_helper_suspend(adev->ddev);
2695
Monk Liu71182662017-12-25 15:14:58 +08002696 /* block all schedulers and reset given job's ring */
Monk Liu57406822017-10-25 16:37:02 +08002697 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2698 struct amdgpu_ring *ring = adev->rings[i];
2699
2700 if (!ring || !ring->sched.thread)
2701 continue;
2702
Monk Liu71182662017-12-25 15:14:58 +08002703 kthread_park(ring->sched.thread);
2704
Monk Liu57406822017-10-25 16:37:02 +08002705 if (job && job->ring->idx != i)
2706 continue;
2707
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002708 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08002709
2710 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2711 amdgpu_fence_driver_force_completion(ring);
2712 }
2713
2714 if (amdgpu_sriov_vf(adev))
Monk Liuc41d1cf2017-12-25 11:59:27 +08002715 r = amdgpu_device_reset_sriov(adev, job ? false : true);
Monk Liu57406822017-10-25 16:37:02 +08002716 else
Monk Liuc41d1cf2017-12-25 11:59:27 +08002717 r = amdgpu_device_reset(adev);
Monk Liu57406822017-10-25 16:37:02 +08002718
Monk Liu71182662017-12-25 15:14:58 +08002719 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2720 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002721
Monk Liu71182662017-12-25 15:14:58 +08002722 if (!ring || !ring->sched.thread)
2723 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002724
Monk Liu71182662017-12-25 15:14:58 +08002725 /* only need recovery sched of the given job's ring
2726 * or all rings (in the case @job is NULL)
2727 * after above amdgpu_reset accomplished
2728 */
2729 if ((!job || job->ring->idx == i) && !r)
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002730 drm_sched_job_recovery(&ring->sched);
Monk Liu57406822017-10-25 16:37:02 +08002731
Monk Liu71182662017-12-25 15:14:58 +08002732 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002733 }
2734
Harry Wentland45622362017-09-12 15:58:20 -04002735 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08002736 if (drm_atomic_helper_resume(adev->ddev, state))
2737 dev_info(adev->dev, "drm resume failed:%d\n", r);
Monk Liu57406822017-10-25 16:37:02 +08002738 } else {
Harry Wentland45622362017-09-12 15:58:20 -04002739 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08002740 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002741
2742 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08002743
Gavin Wan89041942017-06-23 13:55:15 -04002744 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002745 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08002746 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
2747 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2748 } else {
2749 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04002750 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002751
Gavin Wan89041942017-06-23 13:55:15 -04002752 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08002753 adev->in_gpu_reset = 0;
2754 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002755 return r;
2756}
2757
Alex Deucher041d9d92017-12-15 16:49:33 -05002758void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002759{
2760 u32 mask;
2761 int ret;
2762
Alex Deuchercd474ba2016-02-04 10:21:23 -05002763 if (amdgpu_pcie_gen_cap)
2764 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2765
2766 if (amdgpu_pcie_lane_cap)
2767 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2768
2769 /* covers APUs as well */
2770 if (pci_is_root_bus(adev->pdev->bus)) {
2771 if (adev->pm.pcie_gen_mask == 0)
2772 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2773 if (adev->pm.pcie_mlw_mask == 0)
2774 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002775 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002776 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002777
2778 if (adev->pm.pcie_gen_mask == 0) {
2779 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2780 if (!ret) {
2781 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2782 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2783 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2784
2785 if (mask & DRM_PCIE_SPEED_25)
2786 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2787 if (mask & DRM_PCIE_SPEED_50)
2788 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2789 if (mask & DRM_PCIE_SPEED_80)
2790 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2791 } else {
2792 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2793 }
2794 }
2795 if (adev->pm.pcie_mlw_mask == 0) {
2796 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2797 if (!ret) {
2798 switch (mask) {
2799 case 32:
2800 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2801 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2802 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2803 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2804 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2805 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2806 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2807 break;
2808 case 16:
2809 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2810 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2811 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2812 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2813 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2814 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2815 break;
2816 case 12:
2817 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2818 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2819 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2820 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2821 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2822 break;
2823 case 8:
2824 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2825 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2826 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2827 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2828 break;
2829 case 4:
2830 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2831 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2832 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2833 break;
2834 case 2:
2835 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2836 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2837 break;
2838 case 1:
2839 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2840 break;
2841 default:
2842 break;
2843 }
2844 } else {
2845 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002846 }
2847 }
2848}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002849