blob: c9a8cf87934ee4385fb63e1bf25af9779dfc3131 [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 */
Huang Rui60a970a62017-03-15 10:13:32 +0800495 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
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{
533 if (wb < adev->wb.num_wb)
Monk Liu63ae07c2017-10-17 19:18:56 +0800534 __clear_bit(wb >> 3, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535}
536
537/**
Alex Deucher2543e282017-12-14 16:33:36 -0500538 * amdgpu_device_vram_location - try to find VRAM location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 * @adev: amdgpu device structure holding all necessary informations
540 * @mc: memory controller structure holding memory informations
541 * @base: base address at which to put VRAM
542 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400543 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100544 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400545 */
Alex Deucher2543e282017-12-14 16:33:36 -0500546void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100547 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548{
549 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
550
551 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400552 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
553 if (limit && limit < mc->real_vram_size)
554 mc->real_vram_size = limit;
555 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
556 mc->mc_vram_size >> 20, mc->vram_start,
557 mc->vram_end, mc->real_vram_size >> 20);
558}
559
560/**
Alex Deucher2543e282017-12-14 16:33:36 -0500561 * amdgpu_device_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 * @adev: amdgpu device structure holding all necessary informations
563 * @mc: memory controller structure holding memory informations
564 *
565 * Function will place try to place GTT before or after VRAM.
566 *
567 * If GTT size is bigger than space left then we ajust GTT size.
568 * Thus function will never fails.
569 *
570 * FIXME: when reducing GTT size align new size on power of 2.
571 */
Alex Deucher2543e282017-12-14 16:33:36 -0500572void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100573 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574{
575 u64 size_af, size_bf;
576
Christian König770d13b2018-01-12 14:52:22 +0100577 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200578 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200580 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200582 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 }
Christian König6f02a692017-07-07 11:56:59 +0200584 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585 } else {
Christian König6f02a692017-07-07 11:56:59 +0200586 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200588 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589 }
Christian Königb98f1b92017-11-16 20:12:51 +0100590 /* VCE doesn't like it when BOs cross a 4GB segment, so align
591 * the GART base on a 4GB boundary as well.
592 */
593 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 }
Christian König6f02a692017-07-07 11:56:59 +0200595 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400596 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200597 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598}
599
Christian Königd6895ad2017-02-28 10:36:43 +0100600/**
601 * amdgpu_device_resize_fb_bar - try to resize FB BAR
602 *
603 * @adev: amdgpu_device pointer
604 *
605 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
606 * to fail, but if any of the BARs is not accessible after the size we abort
607 * driver loading by returning -ENODEV.
608 */
609int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
610{
Christian König770d13b2018-01-12 14:52:22 +0100611 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100612 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100613 struct pci_bus *root;
614 struct resource *res;
615 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100616 u16 cmd;
617 int r;
618
pding0c03b912017-11-07 11:02:00 +0800619 /* Bypass for VF */
620 if (amdgpu_sriov_vf(adev))
621 return 0;
622
Christian König31b8ada2017-11-15 20:07:38 +0100623 /* Check if the root BUS has 64bit memory resources */
624 root = adev->pdev->bus;
625 while (root->parent)
626 root = root->parent;
627
628 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100629 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100630 res->start > 0x100000000ull)
631 break;
632 }
633
634 /* Trying to resize is pointless without a root hub window above 4GB */
635 if (!res)
636 return 0;
637
Christian Königd6895ad2017-02-28 10:36:43 +0100638 /* Disable memory decoding while we change the BAR addresses and size */
639 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
640 pci_write_config_word(adev->pdev, PCI_COMMAND,
641 cmd & ~PCI_COMMAND_MEMORY);
642
643 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500644 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100645 if (adev->asic_type >= CHIP_BONAIRE)
646 pci_release_resource(adev->pdev, 2);
647
648 pci_release_resource(adev->pdev, 0);
649
650 r = pci_resize_resource(adev->pdev, 0, rbar_size);
651 if (r == -ENOSPC)
652 DRM_INFO("Not enough PCI address space for a large BAR.");
653 else if (r && r != -ENOTSUPP)
654 DRM_ERROR("Problem resizing BAR0 (%d).", r);
655
656 pci_assign_unassigned_bus_resources(adev->pdev->bus);
657
658 /* When the doorbell or fb BAR isn't available we have no chance of
659 * using the device.
660 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500661 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100662 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
663 return -ENODEV;
664
665 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
666
667 return 0;
668}
Horace Chena05502e2017-09-29 14:41:57 +0800669
670/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 * GPU helpers function.
672 */
673/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500674 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675 *
676 * @adev: amdgpu_device pointer
677 *
Jim Quc836fec2017-02-10 15:59:59 +0800678 * Check if the asic has been initialized (all asics) at driver startup
679 * or post is needed if hw reset is performed.
680 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500682bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400683{
684 uint32_t reg;
685
Monk Liubec86372016-09-14 19:38:08 +0800686 if (amdgpu_sriov_vf(adev))
687 return false;
688
689 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800690 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
691 * some old smc fw still need driver do vPost otherwise gpu hang, while
692 * those smc fw version above 22.15 doesn't have this flaw, so we force
693 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800694 */
695 if (adev->asic_type == CHIP_FIJI) {
696 int err;
697 uint32_t fw_ver;
698 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
699 /* force vPost if error occured */
700 if (err)
701 return true;
702
703 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800704 if (fw_ver < 0x00160e00)
705 return true;
Monk Liubec86372016-09-14 19:38:08 +0800706 }
Monk Liubec86372016-09-14 19:38:08 +0800707 }
pding91fe77e2017-10-19 09:38:39 +0800708
709 if (adev->has_hw_reset) {
710 adev->has_hw_reset = false;
711 return true;
712 }
713
714 /* bios scratch used on CIK+ */
715 if (adev->asic_type >= CHIP_BONAIRE)
716 return amdgpu_atombios_scratch_need_asic_init(adev);
717
718 /* check MEM_SIZE for older asics */
719 reg = amdgpu_asic_get_config_memsize(adev);
720
721 if ((reg != 0) && (reg != 0xffffffff))
722 return false;
723
724 return true;
Monk Liubec86372016-09-14 19:38:08 +0800725}
726
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727/* if we get transitioned to only one device, take VGA back */
728/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500729 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 *
731 * @cookie: amdgpu_device pointer
732 * @state: enable/disable vga decode
733 *
734 * Enable/disable vga decode (all asics).
735 * Returns VGA resource flags.
736 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500737static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738{
739 struct amdgpu_device *adev = cookie;
740 amdgpu_asic_set_vga_state(adev, state);
741 if (state)
742 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
743 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
744 else
745 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
746}
747
Alex Deucher06ec9072017-12-14 15:02:39 -0500748static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800749{
750 /* defines number of bits in page table versus page directory,
751 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
752 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800753 if (amdgpu_vm_block_size == -1)
754 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800755
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800756 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800757 dev_warn(adev->dev, "VM page table size (%d) too small\n",
758 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100759 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800760 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800761}
762
Alex Deucher06ec9072017-12-14 15:02:39 -0500763static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800764{
Alex Deucher64dab072017-06-15 18:20:09 -0400765 /* no need to check the default value */
766 if (amdgpu_vm_size == -1)
767 return;
768
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800769 if (amdgpu_vm_size < 1) {
770 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
771 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100772 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800773 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800774}
775
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500777 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400778 *
779 * @adev: amdgpu_device pointer
780 *
781 * Validates certain module parameters and updates
782 * the associated values used by the driver (all asics).
783 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500784static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785{
Chunming Zhou5b011232015-12-10 17:34:33 +0800786 if (amdgpu_sched_jobs < 4) {
787 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
788 amdgpu_sched_jobs);
789 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400790 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800791 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
792 amdgpu_sched_jobs);
793 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
794 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400795
Alex Deucher83e74db2017-08-21 11:58:25 -0400796 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200797 /* gart size must be greater or equal to 32M */
798 dev_warn(adev->dev, "gart size (%d) too small\n",
799 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400800 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801 }
802
Christian König36d38372017-07-07 13:17:45 +0200803 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200805 dev_warn(adev->dev, "gtt size (%d) too small\n",
806 amdgpu_gtt_size);
807 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808 }
809
Roger Hed07f14b2017-08-15 16:05:59 +0800810 /* valid range is between 4 and 9 inclusive */
811 if (amdgpu_vm_fragment_size != -1 &&
812 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
813 dev_warn(adev->dev, "valid range is between 4 and 9\n");
814 amdgpu_vm_fragment_size = -1;
815 }
816
Alex Deucher06ec9072017-12-14 15:02:39 -0500817 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400818
Alex Deucher06ec9072017-12-14 15:02:39 -0500819 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +0200820
jimqu526bae32016-11-07 09:53:10 +0800821 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -0400822 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +0200823 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
824 amdgpu_vram_page_split);
825 amdgpu_vram_page_split = 1024;
826 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -0500827
828 if (amdgpu_lockup_timeout == 0) {
829 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
830 amdgpu_lockup_timeout = 10000;
831 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400832}
833
834/**
835 * amdgpu_switcheroo_set_state - set switcheroo state
836 *
837 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +0200838 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400839 *
840 * Callback for the switcheroo driver. Suspends or resumes the
841 * the asics before or after it is powered up using ACPI methods.
842 */
843static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
844{
845 struct drm_device *dev = pci_get_drvdata(pdev);
846
847 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
848 return;
849
850 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -0800851 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400852 /* don't suspend or resume card normally */
853 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
854
Alex Deucher810ddc32016-08-23 13:25:49 -0400855 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400856
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857 dev->switch_power_state = DRM_SWITCH_POWER_ON;
858 drm_kms_helper_poll_enable(dev);
859 } else {
Joe Perches7ca85292017-02-28 04:55:52 -0800860 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861 drm_kms_helper_poll_disable(dev);
862 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -0400863 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400864 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
865 }
866}
867
868/**
869 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
870 *
871 * @pdev: pci dev pointer
872 *
873 * Callback for the switcheroo driver. Check of the switcheroo
874 * state can be changed.
875 * Returns true if the state can be changed, false if not.
876 */
877static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
878{
879 struct drm_device *dev = pci_get_drvdata(pdev);
880
881 /*
882 * FIXME: open_count is protected by drm_global_mutex but that would lead to
883 * locking inversion with the driver load path. And the access here is
884 * completely racy anyway. So don't bother with locking for now.
885 */
886 return dev->open_count == 0;
887}
888
889static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
890 .set_gpu_state = amdgpu_switcheroo_set_state,
891 .reprobe = NULL,
892 .can_switch = amdgpu_switcheroo_can_switch,
893};
894
Alex Deucher2990a1f2017-12-15 16:18:00 -0500895int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
896 enum amd_ip_block_type block_type,
897 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400898{
899 int i, r = 0;
900
901 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400902 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400903 continue;
Rex Zhuc7228652017-02-22 15:33:46 +0800904 if (adev->ip_blocks[i].version->type != block_type)
905 continue;
906 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
907 continue;
908 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
909 (void *)adev, state);
910 if (r)
911 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
912 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 }
914 return r;
915}
916
Alex Deucher2990a1f2017-12-15 16:18:00 -0500917int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
918 enum amd_ip_block_type block_type,
919 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400920{
921 int i, r = 0;
922
923 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400924 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400925 continue;
Rex Zhuc7228652017-02-22 15:33:46 +0800926 if (adev->ip_blocks[i].version->type != block_type)
927 continue;
928 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
929 continue;
930 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
931 (void *)adev, state);
932 if (r)
933 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
934 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 }
936 return r;
937}
938
Alex Deucher2990a1f2017-12-15 16:18:00 -0500939void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
940 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +0800941{
942 int i;
943
944 for (i = 0; i < adev->num_ip_blocks; i++) {
945 if (!adev->ip_blocks[i].status.valid)
946 continue;
947 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
948 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
949 }
950}
951
Alex Deucher2990a1f2017-12-15 16:18:00 -0500952int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
953 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -0400954{
955 int i, r;
956
957 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400958 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400959 continue;
Alex Deuchera1255102016-10-13 17:41:13 -0400960 if (adev->ip_blocks[i].version->type == block_type) {
961 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -0400962 if (r)
963 return r;
964 break;
965 }
966 }
967 return 0;
968
969}
970
Alex Deucher2990a1f2017-12-15 16:18:00 -0500971bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
972 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -0400973{
974 int i;
975
976 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400977 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400978 continue;
Alex Deuchera1255102016-10-13 17:41:13 -0400979 if (adev->ip_blocks[i].version->type == block_type)
980 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -0400981 }
982 return true;
983
984}
985
Alex Deucher2990a1f2017-12-15 16:18:00 -0500986struct amdgpu_ip_block *
987amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
988 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989{
990 int i;
991
992 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -0400993 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400994 return &adev->ip_blocks[i];
995
996 return NULL;
997}
998
999/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001000 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001 *
1002 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001003 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001004 * @major: major version
1005 * @minor: minor version
1006 *
1007 * return 0 if equal or greater
1008 * return 1 if smaller or the ip_block doesn't exist
1009 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001010int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1011 enum amd_ip_block_type type,
1012 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001013{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001014 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001015
Alex Deuchera1255102016-10-13 17:41:13 -04001016 if (ip_block && ((ip_block->version->major > major) ||
1017 ((ip_block->version->major == major) &&
1018 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001019 return 0;
1020
1021 return 1;
1022}
1023
Alex Deuchera1255102016-10-13 17:41:13 -04001024/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001025 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001026 *
1027 * @adev: amdgpu_device pointer
1028 * @ip_block_version: pointer to the IP to add
1029 *
1030 * Adds the IP block driver information to the collection of IPs
1031 * on the asic.
1032 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001033int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1034 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001035{
1036 if (!ip_block_version)
1037 return -EINVAL;
1038
Shaoyun Liue966a722018-02-01 16:45:26 -05001039 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001040 ip_block_version->funcs->name);
1041
Alex Deuchera1255102016-10-13 17:41:13 -04001042 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1043
1044 return 0;
1045}
1046
Alex Deucher483ef982016-09-30 12:43:04 -04001047static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001048{
1049 adev->enable_virtual_display = false;
1050
1051 if (amdgpu_virtual_display) {
1052 struct drm_device *ddev = adev->ddev;
1053 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001054 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001055
1056 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1057 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001058 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1059 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001060 if (!strcmp("all", pciaddname)
1061 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001062 long num_crtc;
1063 int res = -1;
1064
Emily Deng9accf2f2016-08-10 16:01:25 +08001065 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001066
1067 if (pciaddname_tmp)
1068 res = kstrtol(pciaddname_tmp, 10,
1069 &num_crtc);
1070
1071 if (!res) {
1072 if (num_crtc < 1)
1073 num_crtc = 1;
1074 if (num_crtc > 6)
1075 num_crtc = 6;
1076 adev->mode_info.num_crtc = num_crtc;
1077 } else {
1078 adev->mode_info.num_crtc = 1;
1079 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001080 break;
1081 }
1082 }
1083
Emily Deng0f663562016-09-30 13:02:18 -04001084 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1085 amdgpu_virtual_display, pci_address_name,
1086 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001087
1088 kfree(pciaddstr);
1089 }
1090}
1091
Alex Deuchere2a75f82017-04-27 16:58:01 -04001092static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1093{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001094 const char *chip_name;
1095 char fw_name[30];
1096 int err;
1097 const struct gpu_info_firmware_header_v1_0 *hdr;
1098
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001099 adev->firmware.gpu_info_fw = NULL;
1100
Alex Deuchere2a75f82017-04-27 16:58:01 -04001101 switch (adev->asic_type) {
1102 case CHIP_TOPAZ:
1103 case CHIP_TONGA:
1104 case CHIP_FIJI:
1105 case CHIP_POLARIS11:
1106 case CHIP_POLARIS10:
1107 case CHIP_POLARIS12:
1108 case CHIP_CARRIZO:
1109 case CHIP_STONEY:
1110#ifdef CONFIG_DRM_AMDGPU_SI
1111 case CHIP_VERDE:
1112 case CHIP_TAHITI:
1113 case CHIP_PITCAIRN:
1114 case CHIP_OLAND:
1115 case CHIP_HAINAN:
1116#endif
1117#ifdef CONFIG_DRM_AMDGPU_CIK
1118 case CHIP_BONAIRE:
1119 case CHIP_HAWAII:
1120 case CHIP_KAVERI:
1121 case CHIP_KABINI:
1122 case CHIP_MULLINS:
1123#endif
1124 default:
1125 return 0;
1126 case CHIP_VEGA10:
1127 chip_name = "vega10";
1128 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001129 case CHIP_RAVEN:
1130 chip_name = "raven";
1131 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001132 }
1133
1134 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001135 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001136 if (err) {
1137 dev_err(adev->dev,
1138 "Failed to load gpu_info firmware \"%s\"\n",
1139 fw_name);
1140 goto out;
1141 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001142 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001143 if (err) {
1144 dev_err(adev->dev,
1145 "Failed to validate gpu_info firmware \"%s\"\n",
1146 fw_name);
1147 goto out;
1148 }
1149
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001150 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001151 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1152
1153 switch (hdr->version_major) {
1154 case 1:
1155 {
1156 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001157 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001158 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1159
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001160 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1161 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1162 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1163 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 -04001164 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001165 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1166 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1167 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1168 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1169 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001170 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001171 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1172 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001173 adev->gfx.cu_info.max_waves_per_simd =
1174 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1175 adev->gfx.cu_info.max_scratch_slots_per_cu =
1176 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1177 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001178 break;
1179 }
1180 default:
1181 dev_err(adev->dev,
1182 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1183 err = -EINVAL;
1184 goto out;
1185 }
1186out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001187 return err;
1188}
1189
Alex Deucher06ec9072017-12-14 15:02:39 -05001190static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001191{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001192 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001193
Alex Deucher483ef982016-09-30 12:43:04 -04001194 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001195
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001197 case CHIP_TOPAZ:
1198 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001199 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001200 case CHIP_POLARIS11:
1201 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001202 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001203 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001204 case CHIP_STONEY:
1205 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001206 adev->family = AMDGPU_FAMILY_CZ;
1207 else
1208 adev->family = AMDGPU_FAMILY_VI;
1209
1210 r = vi_set_ip_blocks(adev);
1211 if (r)
1212 return r;
1213 break;
Ken Wang33f34802016-01-21 17:29:41 +08001214#ifdef CONFIG_DRM_AMDGPU_SI
1215 case CHIP_VERDE:
1216 case CHIP_TAHITI:
1217 case CHIP_PITCAIRN:
1218 case CHIP_OLAND:
1219 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001220 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001221 r = si_set_ip_blocks(adev);
1222 if (r)
1223 return r;
1224 break;
1225#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001226#ifdef CONFIG_DRM_AMDGPU_CIK
1227 case CHIP_BONAIRE:
1228 case CHIP_HAWAII:
1229 case CHIP_KAVERI:
1230 case CHIP_KABINI:
1231 case CHIP_MULLINS:
1232 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1233 adev->family = AMDGPU_FAMILY_CI;
1234 else
1235 adev->family = AMDGPU_FAMILY_KV;
1236
1237 r = cik_set_ip_blocks(adev);
1238 if (r)
1239 return r;
1240 break;
1241#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001242 case CHIP_VEGA10:
1243 case CHIP_RAVEN:
1244 if (adev->asic_type == CHIP_RAVEN)
1245 adev->family = AMDGPU_FAMILY_RV;
1246 else
1247 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001248
1249 r = soc15_set_ip_blocks(adev);
1250 if (r)
1251 return r;
1252 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001253 default:
1254 /* FIXME: not supported yet */
1255 return -EINVAL;
1256 }
1257
Alex Deuchere2a75f82017-04-27 16:58:01 -04001258 r = amdgpu_device_parse_gpu_info_fw(adev);
1259 if (r)
1260 return r;
1261
pding18847342017-11-06 10:21:26 +08001262 amdgpu_amdkfd_device_probe(adev);
1263
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001264 if (amdgpu_sriov_vf(adev)) {
1265 r = amdgpu_virt_request_full_gpu(adev, true);
1266 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001267 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001268 }
1269
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270 for (i = 0; i < adev->num_ip_blocks; i++) {
1271 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001272 DRM_ERROR("disabled ip block: %d <%s>\n",
1273 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001274 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001275 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001276 if (adev->ip_blocks[i].version->funcs->early_init) {
1277 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001278 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001279 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001280 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001281 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1282 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001284 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001285 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001286 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001287 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001288 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 }
1291 }
1292
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001293 adev->cg_flags &= amdgpu_cg_mask;
1294 adev->pg_flags &= amdgpu_pg_mask;
1295
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 return 0;
1297}
1298
Alex Deucher06ec9072017-12-14 15:02:39 -05001299static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001300{
1301 int i, r;
1302
1303 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001304 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001305 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001306 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001307 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001308 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1309 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001310 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001311 }
Alex Deuchera1255102016-10-13 17:41:13 -04001312 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001313
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001315 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001316 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001317 if (r) {
1318 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001319 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001320 }
Alex Deuchera1255102016-10-13 17:41:13 -04001321 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001322 if (r) {
1323 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001324 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001325 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001326 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001327 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001328 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001329 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001330 }
Alex Deuchera1255102016-10-13 17:41:13 -04001331 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001332
1333 /* right after GMC hw init, we create CSA */
1334 if (amdgpu_sriov_vf(adev)) {
1335 r = amdgpu_allocate_static_csa(adev);
1336 if (r) {
1337 DRM_ERROR("allocate CSA failed %d\n", r);
1338 return r;
1339 }
1340 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 }
1342 }
1343
1344 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001345 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001346 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001347 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001349 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001350 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001351 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1352 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001353 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001354 }
Alex Deuchera1255102016-10-13 17:41:13 -04001355 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356 }
1357
pding18847342017-11-06 10:21:26 +08001358 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001359
1360 if (amdgpu_sriov_vf(adev))
1361 amdgpu_virt_release_full_gpu(adev, true);
1362
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 return 0;
1364}
1365
Alex Deucher06ec9072017-12-14 15:02:39 -05001366static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001367{
1368 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1369}
1370
Alex Deucher06ec9072017-12-14 15:02:39 -05001371static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001372{
1373 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1374 AMDGPU_RESET_MAGIC_NUM);
1375}
1376
Alex Deucher06ec9072017-12-14 15:02:39 -05001377static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301378{
1379 int i = 0, r;
1380
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001381 if (amdgpu_emu_mode == 1)
1382 return 0;
1383
Shirish S2dc80b02017-05-25 10:05:25 +05301384 for (i = 0; i < adev->num_ip_blocks; i++) {
1385 if (!adev->ip_blocks[i].status.valid)
1386 continue;
1387 /* skip CG for VCE/UVD, it's handled specially */
1388 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1389 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1390 /* enable clockgating to save power */
1391 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1392 AMD_CG_STATE_GATE);
1393 if (r) {
1394 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1395 adev->ip_blocks[i].version->funcs->name, r);
1396 return r;
1397 }
1398 }
1399 }
1400 return 0;
1401}
1402
Alex Deucher06ec9072017-12-14 15:02:39 -05001403static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001404{
1405 int i = 0, r;
1406
1407 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001408 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001409 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001410 if (adev->ip_blocks[i].version->funcs->late_init) {
1411 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001412 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001413 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1414 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001416 }
Alex Deuchera1255102016-10-13 17:41:13 -04001417 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418 }
1419 }
1420
Shirish S2dc80b02017-05-25 10:05:25 +05301421 mod_delayed_work(system_wq, &adev->late_init_work,
1422 msecs_to_jiffies(AMDGPU_RESUME_MS));
1423
Alex Deucher06ec9072017-12-14 15:02:39 -05001424 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001425
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001426 return 0;
1427}
1428
Alex Deucher06ec9072017-12-14 15:02:39 -05001429static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001430{
1431 int i, r;
1432
pding18847342017-11-06 10:21:26 +08001433 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001434 /* need to disable SMC first */
1435 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001436 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001437 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001438 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001439 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001440 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1441 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001442 if (r) {
1443 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001444 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001445 return r;
1446 }
Alex Deuchera1255102016-10-13 17:41:13 -04001447 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001448 /* XXX handle errors */
1449 if (r) {
1450 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001451 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001452 }
Alex Deuchera1255102016-10-13 17:41:13 -04001453 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001454 break;
1455 }
1456 }
1457
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001459 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001460 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001461 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Monk Liu84e5b512017-11-14 16:52:14 +08001462 amdgpu_free_static_csa(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05001463 amdgpu_device_wb_fini(adev);
1464 amdgpu_device_vram_scratch_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001465 }
Rex Zhu8201a672016-11-24 21:44:44 +08001466
1467 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1468 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1469 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1470 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1471 AMD_CG_STATE_UNGATE);
1472 if (r) {
1473 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1474 adev->ip_blocks[i].version->funcs->name, r);
1475 return r;
1476 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001477 }
Rex Zhu8201a672016-11-24 21:44:44 +08001478
Alex Deuchera1255102016-10-13 17:41:13 -04001479 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001481 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001482 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1483 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001484 }
Rex Zhu8201a672016-11-24 21:44:44 +08001485
Alex Deuchera1255102016-10-13 17:41:13 -04001486 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487 }
1488
Alex Deucher9950cda2018-01-18 19:05:36 -05001489 /* disable all interrupts */
1490 amdgpu_irq_disable_all(adev);
1491
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001493 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001495 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001496 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001497 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001498 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1499 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001500 }
Alex Deuchera1255102016-10-13 17:41:13 -04001501 adev->ip_blocks[i].status.sw = false;
1502 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001503 }
1504
Monk Liua6dcfd92016-05-19 14:36:34 +08001505 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001506 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001507 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001508 if (adev->ip_blocks[i].version->funcs->late_fini)
1509 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1510 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001511 }
1512
Monk Liu030308f2017-09-15 15:34:52 +08001513 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001514 if (amdgpu_virt_release_full_gpu(adev, false))
1515 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001516
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517 return 0;
1518}
1519
Alex Deucher06ec9072017-12-14 15:02:39 -05001520static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301521{
1522 struct amdgpu_device *adev =
1523 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001524 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301525}
1526
Alex Deuchercdd61df2017-12-14 16:47:40 -05001527int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001528{
1529 int i, r;
1530
Xiangliang Yue941ea92017-01-18 12:47:55 +08001531 if (amdgpu_sriov_vf(adev))
1532 amdgpu_virt_request_full_gpu(adev, false);
1533
Flora Cuic5a93a22016-02-26 10:45:25 +08001534 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001535 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1536 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001537 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001538 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001539 }
1540
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001542 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001543 continue;
1544 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001545 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001546 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1547 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001548 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001549 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1550 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001551 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001552 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001554 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001556 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001557 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1558 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001559 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 }
1561
Xiangliang Yue941ea92017-01-18 12:47:55 +08001562 if (amdgpu_sriov_vf(adev))
1563 amdgpu_virt_release_full_gpu(adev, false);
1564
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001565 return 0;
1566}
1567
Alex Deucher06ec9072017-12-14 15:02:39 -05001568static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001569{
1570 int i, r;
1571
Monk Liu2cb681b2017-04-26 12:00:49 +08001572 static enum amd_ip_block_type ip_order[] = {
1573 AMD_IP_BLOCK_TYPE_GMC,
1574 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001575 AMD_IP_BLOCK_TYPE_IH,
1576 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001577
Monk Liu2cb681b2017-04-26 12:00:49 +08001578 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1579 int j;
1580 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001581
Monk Liu2cb681b2017-04-26 12:00:49 +08001582 for (j = 0; j < adev->num_ip_blocks; j++) {
1583 block = &adev->ip_blocks[j];
1584
1585 if (block->version->type != ip_order[i] ||
1586 !block->status.valid)
1587 continue;
1588
1589 r = block->version->funcs->hw_init(adev);
1590 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001591 }
1592 }
1593
1594 return 0;
1595}
1596
Alex Deucher06ec9072017-12-14 15:02:39 -05001597static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001598{
1599 int i, r;
1600
Monk Liu2cb681b2017-04-26 12:00:49 +08001601 static enum amd_ip_block_type ip_order[] = {
1602 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001603 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001604 AMD_IP_BLOCK_TYPE_DCE,
1605 AMD_IP_BLOCK_TYPE_GFX,
1606 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001607 AMD_IP_BLOCK_TYPE_UVD,
1608 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001609 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001610
Monk Liu2cb681b2017-04-26 12:00:49 +08001611 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1612 int j;
1613 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001614
Monk Liu2cb681b2017-04-26 12:00:49 +08001615 for (j = 0; j < adev->num_ip_blocks; j++) {
1616 block = &adev->ip_blocks[j];
1617
1618 if (block->version->type != ip_order[i] ||
1619 !block->status.valid)
1620 continue;
1621
1622 r = block->version->funcs->hw_init(adev);
1623 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001624 }
1625 }
1626
1627 return 0;
1628}
1629
Alex Deucher06ec9072017-12-14 15:02:39 -05001630static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001631{
1632 int i, r;
1633
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 for (i = 0; i < adev->num_ip_blocks; i++) {
1635 if (!adev->ip_blocks[i].status.valid)
1636 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001637 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1638 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1639 adev->ip_blocks[i].version->type ==
1640 AMD_IP_BLOCK_TYPE_IH) {
1641 r = adev->ip_blocks[i].version->funcs->resume(adev);
1642 if (r) {
1643 DRM_ERROR("resume of IP block <%s> failed %d\n",
1644 adev->ip_blocks[i].version->funcs->name, r);
1645 return r;
1646 }
1647 }
1648 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649
Chunming Zhoufcf06492017-05-05 10:33:33 +08001650 return 0;
1651}
1652
Alex Deucher06ec9072017-12-14 15:02:39 -05001653static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08001654{
1655 int i, r;
1656
1657 for (i = 0; i < adev->num_ip_blocks; i++) {
1658 if (!adev->ip_blocks[i].status.valid)
1659 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001660 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1661 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1662 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1663 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001664 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001665 if (r) {
1666 DRM_ERROR("resume of IP block <%s> failed %d\n",
1667 adev->ip_blocks[i].version->funcs->name, r);
1668 return r;
1669 }
1670 }
1671
1672 return 0;
1673}
1674
Alex Deucher06ec9072017-12-14 15:02:39 -05001675static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001676{
Chunming Zhoufcf06492017-05-05 10:33:33 +08001677 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678
Alex Deucher06ec9072017-12-14 15:02:39 -05001679 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08001680 if (r)
1681 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05001682 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683
Chunming Zhoufcf06492017-05-05 10:33:33 +08001684 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685}
1686
Monk Liu4e99a442016-03-31 13:26:59 +08001687static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001688{
Monk Liu6867e1b2017-10-16 19:50:44 +08001689 if (amdgpu_sriov_vf(adev)) {
1690 if (adev->is_atom_fw) {
1691 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1692 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1693 } else {
1694 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1695 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1696 }
1697
1698 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
1699 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001700 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001701}
1702
Harry Wentland45622362017-09-12 15:58:20 -04001703bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
1704{
1705 switch (asic_type) {
1706#if defined(CONFIG_DRM_AMD_DC)
1707 case CHIP_BONAIRE:
1708 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04001709 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05001710 case CHIP_KABINI:
1711 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04001712 case CHIP_CARRIZO:
1713 case CHIP_STONEY:
1714 case CHIP_POLARIS11:
1715 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04001716 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04001717 case CHIP_TONGA:
1718 case CHIP_FIJI:
1719#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
1720 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04001721#endif
Harry Wentland42f8ffa2017-09-15 14:07:30 -04001722 case CHIP_VEGA10:
1723#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08001724 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04001725#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08001726 return amdgpu_dc != 0;
1727#endif
Harry Wentland45622362017-09-12 15:58:20 -04001728 default:
1729 return false;
1730 }
1731}
1732
1733/**
1734 * amdgpu_device_has_dc_support - check if dc is supported
1735 *
1736 * @adev: amdgpu_device_pointer
1737 *
1738 * Returns true for supported, false for not supported
1739 */
1740bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
1741{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08001742 if (amdgpu_sriov_vf(adev))
1743 return false;
1744
Harry Wentland45622362017-09-12 15:58:20 -04001745 return amdgpu_device_asic_has_dc_support(adev->asic_type);
1746}
1747
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001748/**
1749 * amdgpu_device_init - initialize the driver
1750 *
1751 * @adev: amdgpu_device pointer
1752 * @pdev: drm dev pointer
1753 * @pdev: pci dev pointer
1754 * @flags: driver flags
1755 *
1756 * Initializes the driver info and hw (all asics).
1757 * Returns 0 for success or an error on failure.
1758 * Called at driver startup.
1759 */
1760int amdgpu_device_init(struct amdgpu_device *adev,
1761 struct drm_device *ddev,
1762 struct pci_dev *pdev,
1763 uint32_t flags)
1764{
1765 int r, i;
1766 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001767 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001768
1769 adev->shutdown = false;
1770 adev->dev = &pdev->dev;
1771 adev->ddev = ddev;
1772 adev->pdev = pdev;
1773 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001774 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05001776 if (amdgpu_emu_mode == 1)
1777 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01001778 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 adev->accel_working = false;
1780 adev->num_rings = 0;
1781 adev->mman.buffer_funcs = NULL;
1782 adev->mman.buffer_funcs_ring = NULL;
1783 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001784 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01001785 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001786 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04001787 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001788
1789 adev->smc_rreg = &amdgpu_invalid_rreg;
1790 adev->smc_wreg = &amdgpu_invalid_wreg;
1791 adev->pcie_rreg = &amdgpu_invalid_rreg;
1792 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001793 adev->pciep_rreg = &amdgpu_invalid_rreg;
1794 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1796 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1797 adev->didt_rreg = &amdgpu_invalid_rreg;
1798 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001799 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1800 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001801 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1802 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1803
Alex Deucher3e39ab92015-06-05 15:04:33 -04001804 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1805 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1806 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001807
1808 /* mutex initialization are all done here so we
1809 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05001811 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001812 mutex_init(&adev->pm.mutex);
1813 mutex_init(&adev->gfx.gpu_clock_mutex);
1814 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04001815 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001816 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001817 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04001818 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08001820 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001821
Alex Deucher06ec9072017-12-14 15:02:39 -05001822 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001823
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001824 spin_lock_init(&adev->mmio_idx_lock);
1825 spin_lock_init(&adev->smc_idx_lock);
1826 spin_lock_init(&adev->pcie_idx_lock);
1827 spin_lock_init(&adev->uvd_ctx_idx_lock);
1828 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001829 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08001830 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001831 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001832 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001833
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001834 INIT_LIST_HEAD(&adev->shadow_list);
1835 mutex_init(&adev->shadow_list_lock);
1836
Andres Rodriguez795f2812017-03-06 16:27:55 -05001837 INIT_LIST_HEAD(&adev->ring_lru_list);
1838 spin_lock_init(&adev->ring_lru_list_lock);
1839
Alex Deucher06ec9072017-12-14 15:02:39 -05001840 INIT_DELAYED_WORK(&adev->late_init_work,
1841 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05301842
Alex Xie0fa49552017-06-08 14:58:05 -04001843 /* Registers mapping */
1844 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08001845 if (adev->asic_type >= CHIP_BONAIRE) {
1846 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1847 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1848 } else {
1849 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1850 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1851 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001852
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001853 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1854 if (adev->rmmio == NULL) {
1855 return -ENOMEM;
1856 }
1857 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1858 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1859
Christian König705e5192017-06-08 11:15:16 +02001860 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05001861 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001862
1863 /* io port mapping */
1864 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1865 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1866 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1867 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1868 break;
1869 }
1870 }
1871 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001872 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001873
1874 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05001875 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001876 if (r)
1877 return r;
1878
1879 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1880 /* this will fail for cards that aren't VGA class devices, just
1881 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05001882 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001883
Alex Deuchere9bef452016-04-25 13:12:18 -04001884 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001885 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01001886 if (!pci_is_thunderbolt_attached(adev->pdev))
1887 vga_switcheroo_register_client(adev->pdev,
1888 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 if (runtime)
1890 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1891
Shaoyun Liu9475a942018-02-01 18:13:23 -05001892 if (amdgpu_emu_mode == 1) {
1893 /* post the asic on emulation mode */
1894 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05001895 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05001896 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05001897
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001898 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001899 if (!amdgpu_get_bios(adev)) {
1900 r = -EINVAL;
1901 goto failed;
1902 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001903
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001904 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001905 if (r) {
1906 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001907 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04001908 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001909 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001910
Monk Liu4e99a442016-03-31 13:26:59 +08001911 /* detect if we are with an SRIOV vbios */
1912 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001913
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001914 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05001915 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001916 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001917 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001918 r = -EINVAL;
1919 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920 }
Monk Liubec86372016-09-14 19:38:08 +08001921 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001922 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1923 if (r) {
1924 dev_err(adev->dev, "gpu post error!\n");
1925 goto failed;
1926 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001927 }
1928
Alex Deucher88b64e92017-07-10 10:43:10 -04001929 if (adev->is_atom_fw) {
1930 /* Initialize clocks */
1931 r = amdgpu_atomfirmware_get_clock_info(adev);
1932 if (r) {
1933 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001934 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04001935 goto failed;
1936 }
1937 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001938 /* Initialize clocks */
1939 r = amdgpu_atombios_get_clock_info(adev);
1940 if (r) {
1941 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001942 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04001943 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001944 }
1945 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04001946 if (!amdgpu_device_has_dc_support(adev))
1947 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001948 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001949
Shaoyun Liubfca0282018-02-01 17:37:50 -05001950fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001951 /* Fence driver */
1952 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001953 if (r) {
1954 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001955 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04001956 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001957 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958
1959 /* init the mode config */
1960 drm_mode_config_init(adev->ddev);
1961
Alex Deucher06ec9072017-12-14 15:02:39 -05001962 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001963 if (r) {
pding8840a382017-10-23 17:22:09 +08001964 /* failed in exclusive mode due to timeout */
1965 if (amdgpu_sriov_vf(adev) &&
1966 !amdgpu_sriov_runtime(adev) &&
1967 amdgpu_virt_mmio_blocked(adev) &&
1968 !amdgpu_virt_wait_reset(adev)) {
1969 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08001970 /* Don't send request since VF is inactive. */
1971 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
1972 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08001973 r = -EAGAIN;
1974 goto failed;
1975 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001976 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001977 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher06ec9072017-12-14 15:02:39 -05001978 amdgpu_device_ip_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001979 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001980 }
1981
1982 adev->accel_working = true;
1983
Alex Xiee59c0202017-06-01 09:42:59 -04001984 amdgpu_vm_check_compute_bug(adev);
1985
Marek Olšák95844d22016-08-17 23:49:27 +02001986 /* Initialize the buffer migration limit. */
1987 if (amdgpu_moverate >= 0)
1988 max_MBps = amdgpu_moverate;
1989 else
1990 max_MBps = 8; /* Allow 8 MB/s. */
1991 /* Get a log2 for easy divisions. */
1992 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1993
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001994 r = amdgpu_ib_pool_init(adev);
1995 if (r) {
1996 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04001997 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001998 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001999 }
2000
2001 r = amdgpu_ib_ring_tests(adev);
2002 if (r)
2003 DRM_ERROR("ib ring test failed (%d).\n", r);
2004
Horace Chen2dc8f812017-10-09 16:17:16 +08002005 if (amdgpu_sriov_vf(adev))
2006 amdgpu_virt_init_data_exchange(adev);
2007
Monk Liu9bc92b92017-02-08 17:38:13 +08002008 amdgpu_fbdev_init(adev);
2009
Rex Zhud2f52ac2017-09-22 17:47:27 +08002010 r = amdgpu_pm_sysfs_init(adev);
2011 if (r)
2012 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2013
Alex Deucher75758252017-12-14 15:23:14 -05002014 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002015 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002016 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002017
2018 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002019 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002020 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002021
Huang Rui50ab2532016-06-12 15:51:09 +08002022 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002023 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002024 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002025
Christian König763efb62017-12-06 15:44:51 +01002026 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002027 if (r)
Christian König763efb62017-12-06 15:44:51 +01002028 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002029
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030 if ((amdgpu_testing & 1)) {
2031 if (adev->accel_working)
2032 amdgpu_test_moves(adev);
2033 else
2034 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2035 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036 if (amdgpu_benchmarking) {
2037 if (adev->accel_working)
2038 amdgpu_benchmark(adev, amdgpu_benchmarking);
2039 else
2040 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2041 }
2042
2043 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2044 * explicit gating rather than handling it automatically.
2045 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002046 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002047 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002048 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002049 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002050 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002051 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002052
2053 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002054
2055failed:
Gavin Wan89041942017-06-23 13:55:15 -04002056 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002057 if (runtime)
2058 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002059
Alex Deucher83ba1262016-06-03 18:21:41 -04002060 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002061}
2062
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063/**
2064 * amdgpu_device_fini - tear down the driver
2065 *
2066 * @adev: amdgpu_device pointer
2067 *
2068 * Tear down the driver info (all asics).
2069 * Called at driver shutdown.
2070 */
2071void amdgpu_device_fini(struct amdgpu_device *adev)
2072{
2073 int r;
2074
2075 DRM_INFO("amdgpu: finishing device.\n");
2076 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002077 if (adev->mode_info.mode_config_initialized)
2078 drm_crtc_force_disable_all(adev->ddev);
Monk Liub9141cd2017-11-22 19:21:43 +08002079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080 amdgpu_ib_pool_fini(adev);
2081 amdgpu_fence_driver_fini(adev);
2082 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002083 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002084 if (adev->firmware.gpu_info_fw) {
2085 release_firmware(adev->firmware.gpu_info_fw);
2086 adev->firmware.gpu_info_fw = NULL;
2087 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002088 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302089 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002091 if (!amdgpu_device_has_dc_support(adev))
2092 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002093
2094 if (amdgpu_emu_mode != 1)
2095 amdgpu_atombios_fini(adev);
2096
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097 kfree(adev->bios);
2098 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002099 if (!pci_is_thunderbolt_attached(adev->pdev))
2100 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002101 if (adev->flags & AMD_IS_PX)
2102 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002103 vga_client_register(adev->pdev, NULL, NULL, NULL);
2104 if (adev->rio_mem)
2105 pci_iounmap(adev->pdev, adev->rio_mem);
2106 adev->rio_mem = NULL;
2107 iounmap(adev->rmmio);
2108 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002109 amdgpu_device_doorbell_fini(adev);
Rex Zhud2f52ac2017-09-22 17:47:27 +08002110 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002112}
2113
2114
2115/*
2116 * Suspend & resume.
2117 */
2118/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002119 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002120 *
2121 * @pdev: drm dev pointer
2122 * @state: suspend state
2123 *
2124 * Puts the hw in the suspend state (all asics).
2125 * Returns 0 for success or an error on failure.
2126 * Called at driver suspend.
2127 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002128int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002129{
2130 struct amdgpu_device *adev;
2131 struct drm_crtc *crtc;
2132 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002133 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002134
2135 if (dev == NULL || dev->dev_private == NULL) {
2136 return -ENODEV;
2137 }
2138
2139 adev = dev->dev_private;
2140
2141 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2142 return 0;
2143
2144 drm_kms_helper_poll_disable(dev);
2145
Harry Wentland45622362017-09-12 15:58:20 -04002146 if (!amdgpu_device_has_dc_support(adev)) {
2147 /* turn off display hw */
2148 drm_modeset_lock_all(dev);
2149 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2150 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2151 }
2152 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002153 }
2154
Yong Zhaoba997702015-11-09 17:21:45 -05002155 amdgpu_amdkfd_suspend(adev);
2156
Alex Deucher756e6882015-10-08 00:03:36 -04002157 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002158 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002159 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002160 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2161 struct amdgpu_bo *robj;
2162
Alex Deucher756e6882015-10-08 00:03:36 -04002163 if (amdgpu_crtc->cursor_bo) {
2164 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002165 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002166 if (r == 0) {
2167 amdgpu_bo_unpin(aobj);
2168 amdgpu_bo_unreserve(aobj);
2169 }
2170 }
2171
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002172 if (rfb == NULL || rfb->obj == NULL) {
2173 continue;
2174 }
2175 robj = gem_to_amdgpu_bo(rfb->obj);
2176 /* don't unpin kernel fb objects */
2177 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002178 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179 if (r == 0) {
2180 amdgpu_bo_unpin(robj);
2181 amdgpu_bo_unreserve(robj);
2182 }
2183 }
2184 }
2185 /* evict vram memory */
2186 amdgpu_bo_evict_vram(adev);
2187
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002188 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002189
Alex Deuchercdd61df2017-12-14 16:47:40 -05002190 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191
Alex Deuchera0a71e42016-10-10 12:41:36 -04002192 /* evict remaining vram memory
2193 * This second call to evict vram is to evict the gart page table
2194 * using the CPU.
2195 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002196 amdgpu_bo_evict_vram(adev);
2197
2198 pci_save_state(dev->pdev);
2199 if (suspend) {
2200 /* Shut down the device */
2201 pci_disable_device(dev->pdev);
2202 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002203 } else {
2204 r = amdgpu_asic_reset(adev);
2205 if (r)
2206 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 }
2208
2209 if (fbcon) {
2210 console_lock();
2211 amdgpu_fbdev_set_suspend(adev, 1);
2212 console_unlock();
2213 }
2214 return 0;
2215}
2216
2217/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002218 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 *
2220 * @pdev: drm dev pointer
2221 *
2222 * Bring the hw back to operating state (all asics).
2223 * Returns 0 for success or an error on failure.
2224 * Called at driver resume.
2225 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002226int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227{
2228 struct drm_connector *connector;
2229 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002230 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002231 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232
2233 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2234 return 0;
2235
jimqu74b0b152016-09-07 17:09:12 +08002236 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002237 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002238
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002239 if (resume) {
2240 pci_set_power_state(dev->pdev, PCI_D0);
2241 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002242 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002243 if (r)
2244 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002245 }
2246
2247 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002248 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002249 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2250 if (r)
2251 DRM_ERROR("amdgpu asic init failed\n");
2252 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002253
Alex Deucher06ec9072017-12-14 15:02:39 -05002254 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002255 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002256 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002257 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002258 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002259 amdgpu_fence_driver_resume(adev);
2260
Flora Cuica198522016-02-04 15:10:08 +08002261 if (resume) {
2262 r = amdgpu_ib_ring_tests(adev);
2263 if (r)
2264 DRM_ERROR("ib ring test failed (%d).\n", r);
2265 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002266
Alex Deucher06ec9072017-12-14 15:02:39 -05002267 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002268 if (r)
2269 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002270
Alex Deucher756e6882015-10-08 00:03:36 -04002271 /* pin cursors */
2272 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2273 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2274
2275 if (amdgpu_crtc->cursor_bo) {
2276 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002277 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002278 if (r == 0) {
2279 r = amdgpu_bo_pin(aobj,
2280 AMDGPU_GEM_DOMAIN_VRAM,
2281 &amdgpu_crtc->cursor_addr);
2282 if (r != 0)
2283 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2284 amdgpu_bo_unreserve(aobj);
2285 }
2286 }
2287 }
Yong Zhaoba997702015-11-09 17:21:45 -05002288 r = amdgpu_amdkfd_resume(adev);
2289 if (r)
2290 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002291
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292 /* blat the mode back in */
2293 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002294 if (!amdgpu_device_has_dc_support(adev)) {
2295 /* pre DCE11 */
2296 drm_helper_resume_force_mode(dev);
2297
2298 /* turn on display hw */
2299 drm_modeset_lock_all(dev);
2300 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2301 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2302 }
2303 drm_modeset_unlock_all(dev);
2304 } else {
2305 /*
2306 * There is no equivalent atomic helper to turn on
2307 * display, so we defined our own function for this,
2308 * once suspend resume is supported by the atomic
2309 * framework this will be reworked
2310 */
2311 amdgpu_dm_display_resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312 }
2313 }
2314
2315 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002316
2317 /*
2318 * Most of the connector probing functions try to acquire runtime pm
2319 * refs to ensure that the GPU is powered on when connector polling is
2320 * performed. Since we're calling this from a runtime PM callback,
2321 * trying to acquire rpm refs will cause us to deadlock.
2322 *
2323 * Since we're guaranteed to be holding the rpm lock, it's safe to
2324 * temporarily disable the rpm helpers so this doesn't deadlock us.
2325 */
2326#ifdef CONFIG_PM
2327 dev->dev->power.disable_depth++;
2328#endif
Harry Wentland45622362017-09-12 15:58:20 -04002329 if (!amdgpu_device_has_dc_support(adev))
2330 drm_helper_hpd_irq_event(dev);
2331 else
2332 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002333#ifdef CONFIG_PM
2334 dev->dev->power.disable_depth--;
2335#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002336
Huang Rui03161a62017-04-13 16:12:26 +08002337 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002338 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002339
Huang Rui03161a62017-04-13 16:12:26 +08002340unlock:
2341 if (fbcon)
2342 console_unlock();
2343
2344 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345}
2346
Alex Deucher06ec9072017-12-14 15:02:39 -05002347static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002348{
2349 int i;
2350 bool asic_hang = false;
2351
Monk Liuf993d622017-10-16 19:46:01 +08002352 if (amdgpu_sriov_vf(adev))
2353 return true;
2354
Chunming Zhou63fbf422016-07-15 11:19:20 +08002355 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002356 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002357 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002358 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2359 adev->ip_blocks[i].status.hang =
2360 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2361 if (adev->ip_blocks[i].status.hang) {
2362 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002363 asic_hang = true;
2364 }
2365 }
2366 return asic_hang;
2367}
2368
Alex Deucher06ec9072017-12-14 15:02:39 -05002369static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002370{
2371 int i, r = 0;
2372
2373 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002374 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002375 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002376 if (adev->ip_blocks[i].status.hang &&
2377 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2378 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002379 if (r)
2380 return r;
2381 }
2382 }
2383
2384 return 0;
2385}
2386
Alex Deucher06ec9072017-12-14 15:02:39 -05002387static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002388{
Alex Deucherda146d32016-10-13 16:07:03 -04002389 int i;
2390
2391 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002392 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002393 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002394 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2395 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2396 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002397 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2398 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002399 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002400 DRM_INFO("Some block need full reset!\n");
2401 return true;
2402 }
2403 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002404 }
2405 return false;
2406}
2407
Alex Deucher06ec9072017-12-14 15:02:39 -05002408static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002409{
2410 int i, r = 0;
2411
2412 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002413 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002414 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002415 if (adev->ip_blocks[i].status.hang &&
2416 adev->ip_blocks[i].version->funcs->soft_reset) {
2417 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002418 if (r)
2419 return r;
2420 }
2421 }
2422
2423 return 0;
2424}
2425
Alex Deucher06ec9072017-12-14 15:02:39 -05002426static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002427{
2428 int i, r = 0;
2429
2430 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002431 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002432 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002433 if (adev->ip_blocks[i].status.hang &&
2434 adev->ip_blocks[i].version->funcs->post_soft_reset)
2435 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002436 if (r)
2437 return r;
2438 }
2439
2440 return 0;
2441}
2442
Alex Deucher06ec9072017-12-14 15:02:39 -05002443static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2444 struct amdgpu_ring *ring,
2445 struct amdgpu_bo *bo,
2446 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002447{
2448 uint32_t domain;
2449 int r;
2450
Roger.He23d2e502017-04-21 14:24:26 +08002451 if (!bo->shadow)
2452 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002453
Alex Xie1d284792017-04-24 13:53:04 -04002454 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002455 if (r)
2456 return r;
2457 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2458 /* if bo has been evicted, then no need to recover */
2459 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002460 r = amdgpu_bo_validate(bo->shadow);
2461 if (r) {
2462 DRM_ERROR("bo validate failed!\n");
2463 goto err;
2464 }
2465
Roger.He23d2e502017-04-21 14:24:26 +08002466 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002467 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002468 if (r) {
2469 DRM_ERROR("recover page table failed!\n");
2470 goto err;
2471 }
2472 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002473err:
Roger.He23d2e502017-04-21 14:24:26 +08002474 amdgpu_bo_unreserve(bo);
2475 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002476}
2477
Monk Liu57406822017-10-25 16:37:02 +08002478/*
Alex Deucher06ec9072017-12-14 15:02:39 -05002479 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002480 *
2481 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08002482 * @reset_flags: output param tells caller the reset result
Monk Liua90ad3c2017-01-23 14:22:08 +08002483 *
Monk Liu57406822017-10-25 16:37:02 +08002484 * attempt to do soft-reset or full-reset and reinitialize Asic
2485 * return 0 means successed otherwise failed
2486*/
Alex Deucher06ec9072017-12-14 15:02:39 -05002487static int amdgpu_device_reset(struct amdgpu_device *adev,
2488 uint64_t* reset_flags)
Monk Liua90ad3c2017-01-23 14:22:08 +08002489{
Monk Liu57406822017-10-25 16:37:02 +08002490 bool need_full_reset, vram_lost = 0;
2491 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002492
Alex Deucher06ec9072017-12-14 15:02:39 -05002493 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002494
2495 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002496 amdgpu_device_ip_pre_soft_reset(adev);
2497 r = amdgpu_device_ip_soft_reset(adev);
2498 amdgpu_device_ip_post_soft_reset(adev);
2499 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002500 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2501 need_full_reset = true;
2502 }
Monk Liu57406822017-10-25 16:37:02 +08002503
Chunming Zhou35d782f2016-07-15 15:57:13 +08002504 }
2505
2506 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05002507 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002508
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002509retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002510 r = amdgpu_asic_reset(adev);
2511 /* post card */
2512 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002513
Chunming Zhou35d782f2016-07-15 15:57:13 +08002514 if (!r) {
2515 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05002516 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002517 if (r)
2518 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002519
Alex Deucher06ec9072017-12-14 15:02:39 -05002520 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002521 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002522 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002523 atomic_inc(&adev->vram_lost_counter);
2524 }
Monk Liu57406822017-10-25 16:37:02 +08002525
Christian Königc1c7ce82017-10-16 16:50:32 +02002526 r = amdgpu_gtt_mgr_recover(
2527 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002528 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002529 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002530
Alex Deucher06ec9072017-12-14 15:02:39 -05002531 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002532 if (r)
2533 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002534
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002535 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05002536 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002537 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002538 }
Monk Liu57406822017-10-25 16:37:02 +08002539
Chunming Zhoufcf06492017-05-05 10:33:33 +08002540out:
2541 if (!r) {
2542 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002543 r = amdgpu_ib_ring_tests(adev);
2544 if (r) {
2545 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05002546 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002547 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002548 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002549 }
Monk Liu57406822017-10-25 16:37:02 +08002550 }
2551
2552 if (reset_flags) {
2553 if (vram_lost)
2554 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2555
2556 if (need_full_reset)
2557 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2558 }
2559
2560 return r;
2561}
2562
2563/*
Alex Deucher06ec9072017-12-14 15:02:39 -05002564 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08002565 *
2566 * @adev: amdgpu device pointer
2567 * @reset_flags: output param tells caller the reset result
2568 *
2569 * do VF FLR and reinitialize Asic
2570 * return 0 means successed otherwise failed
2571*/
Alex Deucher06ec9072017-12-14 15:02:39 -05002572static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
2573 uint64_t *reset_flags,
2574 bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08002575{
2576 int r;
2577
2578 if (from_hypervisor)
2579 r = amdgpu_virt_request_full_gpu(adev, true);
2580 else
2581 r = amdgpu_virt_reset_gpu(adev);
2582 if (r)
2583 return r;
2584
2585 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05002586 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08002587 if (r)
2588 goto error;
2589
2590 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02002591 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08002592
2593 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05002594 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08002595 if (r)
2596 goto error;
2597
2598 amdgpu_irq_gpu_reset_resume_helper(adev);
2599 r = amdgpu_ib_ring_tests(adev);
2600 if (r)
2601 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2602
2603error:
2604 /* release full control of GPU after ib test */
2605 amdgpu_virt_release_full_gpu(adev, true);
2606
2607 if (reset_flags) {
Monk Liu75bc6092017-10-30 20:11:54 +08002608 if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
2609 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2610 atomic_inc(&adev->vram_lost_counter);
2611 }
Monk Liu57406822017-10-25 16:37:02 +08002612
2613 /* VF FLR or hotlink reset is always full-reset */
2614 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2615 }
2616
2617 return r;
2618}
2619
2620/**
Alex Deucher5f152b52017-12-15 16:40:49 -05002621 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08002622 *
2623 * @adev: amdgpu device pointer
2624 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05002625 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08002626 *
2627 * Attempt to reset the GPU if it has hung (all asics).
2628 * Returns 0 for success or an error on failure.
2629 */
Alex Deucher5f152b52017-12-15 16:40:49 -05002630int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
2631 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08002632{
2633 struct drm_atomic_state *state = NULL;
2634 uint64_t reset_flags = 0;
2635 int i, r, resched;
2636
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05002637 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08002638 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2639 return 0;
2640 }
2641
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05002642 if (!force && (amdgpu_gpu_recovery == 0 ||
2643 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
2644 DRM_INFO("GPU recovery disabled.\n");
2645 return 0;
2646 }
2647
Monk Liu57406822017-10-25 16:37:02 +08002648 dev_info(adev->dev, "GPU reset begin!\n");
2649
Monk Liu13a752e2017-10-17 15:11:12 +08002650 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08002651 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08002652 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08002653
2654 /* block TTM */
2655 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2656 /* store modesetting */
2657 if (amdgpu_device_has_dc_support(adev))
2658 state = drm_atomic_helper_suspend(adev->ddev);
2659
2660 /* block scheduler */
2661 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2662 struct amdgpu_ring *ring = adev->rings[i];
2663
2664 if (!ring || !ring->sched.thread)
2665 continue;
2666
2667 /* only focus on the ring hit timeout if &job not NULL */
2668 if (job && job->ring->idx != i)
2669 continue;
2670
2671 kthread_park(ring->sched.thread);
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002672 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08002673
2674 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2675 amdgpu_fence_driver_force_completion(ring);
2676 }
2677
2678 if (amdgpu_sriov_vf(adev))
Alex Deucher06ec9072017-12-14 15:02:39 -05002679 r = amdgpu_device_reset_sriov(adev, &reset_flags, job ? false : true);
Monk Liu57406822017-10-25 16:37:02 +08002680 else
Alex Deucher06ec9072017-12-14 15:02:39 -05002681 r = amdgpu_device_reset(adev, &reset_flags);
Monk Liu57406822017-10-25 16:37:02 +08002682
2683 if (!r) {
2684 if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
2685 (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002686 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2687 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002688 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002689
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002690 DRM_INFO("recover vram bo from shadow\n");
2691 mutex_lock(&adev->shadow_list_lock);
2692 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002693 next = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002694 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002695 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002696 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002697 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002698 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002699 break;
2700 }
2701 }
2702
Chris Wilsonf54d1862016-10-25 13:00:45 +01002703 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002704 fence = next;
2705 }
2706 mutex_unlock(&adev->shadow_list_lock);
2707 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002708 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002709 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002710 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002711 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002712 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002713 }
Monk Liu57406822017-10-25 16:37:02 +08002714
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002715 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2716 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002717
2718 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002719 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002720
Monk Liu57406822017-10-25 16:37:02 +08002721 /* only focus on the ring hit timeout if &job not NULL */
2722 if (job && job->ring->idx != i)
2723 continue;
2724
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002725 drm_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002726 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002727 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002728 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002729 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Monk Liu57406822017-10-25 16:37:02 +08002730 struct amdgpu_ring *ring = adev->rings[i];
2731
2732 if (!ring || !ring->sched.thread)
2733 continue;
2734
2735 /* only focus on the ring hit timeout if &job not NULL */
2736 if (job && job->ring->idx != i)
2737 continue;
2738
2739 kthread_unpark(adev->rings[i]->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002740 }
2741 }
2742
Harry Wentland45622362017-09-12 15:58:20 -04002743 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08002744 if (drm_atomic_helper_resume(adev->ddev, state))
2745 dev_info(adev->dev, "drm resume failed:%d\n", r);
Harry Wentland45622362017-09-12 15:58:20 -04002746 amdgpu_dm_display_resume(adev);
Monk Liu57406822017-10-25 16:37:02 +08002747 } else {
Harry Wentland45622362017-09-12 15:58:20 -04002748 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08002749 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002750
2751 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08002752
Gavin Wan89041942017-06-23 13:55:15 -04002753 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002754 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08002755 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
2756 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2757 } else {
2758 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04002759 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002760
Gavin Wan89041942017-06-23 13:55:15 -04002761 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08002762 adev->in_gpu_reset = 0;
2763 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002764 return r;
2765}
2766
Alex Deucher041d9d92017-12-15 16:49:33 -05002767void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002768{
2769 u32 mask;
2770 int ret;
2771
Alex Deuchercd474ba2016-02-04 10:21:23 -05002772 if (amdgpu_pcie_gen_cap)
2773 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2774
2775 if (amdgpu_pcie_lane_cap)
2776 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2777
2778 /* covers APUs as well */
2779 if (pci_is_root_bus(adev->pdev->bus)) {
2780 if (adev->pm.pcie_gen_mask == 0)
2781 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2782 if (adev->pm.pcie_mlw_mask == 0)
2783 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002784 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002785 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002786
2787 if (adev->pm.pcie_gen_mask == 0) {
2788 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2789 if (!ret) {
2790 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2791 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2792 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2793
2794 if (mask & DRM_PCIE_SPEED_25)
2795 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2796 if (mask & DRM_PCIE_SPEED_50)
2797 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2798 if (mask & DRM_PCIE_SPEED_80)
2799 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2800 } else {
2801 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2802 }
2803 }
2804 if (adev->pm.pcie_mlw_mask == 0) {
2805 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2806 if (!ret) {
2807 switch (mask) {
2808 case 32:
2809 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2810 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2811 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2812 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2813 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2814 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2815 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2816 break;
2817 case 16:
2818 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2819 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2820 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2821 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2822 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2823 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2824 break;
2825 case 12:
2826 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2827 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2828 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2829 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2830 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2831 break;
2832 case 8:
2833 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2834 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2835 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2836 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2837 break;
2838 case 4:
2839 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2840 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2841 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2842 break;
2843 case 2:
2844 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2845 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2846 break;
2847 case 1:
2848 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2849 break;
2850 default:
2851 break;
2852 }
2853 } else {
2854 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002855 }
2856 }
2857}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002858