blob: 7d7d30b8a719ff4486e3b8e6c2fd6b30dbeb07ac [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
Alex Deucher5494d862018-03-09 15:14:11 -050090static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
91
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092bool amdgpu_device_is_px(struct drm_device *dev)
93{
94 struct amdgpu_device *adev = dev->dev_private;
95
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080096 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040097 return true;
98 return false;
99}
100
101/*
102 * MMIO register access helper functions.
103 */
104uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800105 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400107 uint32_t ret;
108
pding43ca8ef2017-10-13 15:38:35 +0800109 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800110 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800111
Monk Liu15d72fd2017-01-25 15:07:40 +0800112 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400113 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114 else {
115 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116
117 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
118 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
119 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
120 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400122 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
123 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124}
125
Monk Liu421a2a32018-01-04 18:13:20 +0800126/*
127 * MMIO register read with bytes helper functions
128 * @offset:bytes offset from MMIO start
129 *
130*/
131
132uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
133 if (offset < adev->rmmio_size)
134 return (readb(adev->rmmio + offset));
135 BUG();
136}
137
138/*
139 * MMIO register write with bytes helper functions
140 * @offset:bytes offset from MMIO start
141 * @value: the value want to be written to the register
142 *
143*/
144void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
145 if (offset < adev->rmmio_size)
146 writeb(value, adev->rmmio + offset);
147 else
148 BUG();
149}
150
151
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400152void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800153 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400155 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800156
Ken Wang47ed4e12017-07-04 13:11:52 +0800157 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
158 adev->last_mm_index = v;
159 }
160
pding43ca8ef2017-10-13 15:38:35 +0800161 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800162 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800163
Monk Liu15d72fd2017-01-25 15:07:40 +0800164 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
166 else {
167 unsigned long flags;
168
169 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
170 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
171 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
172 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
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
180u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
181{
182 if ((reg * 4) < adev->rio_mem_size)
183 return ioread32(adev->rio_mem + (reg * 4));
184 else {
185 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
186 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
187 }
188}
189
190void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
191{
Ken Wang47ed4e12017-07-04 13:11:52 +0800192 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
193 adev->last_mm_index = v;
194 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195
196 if ((reg * 4) < adev->rio_mem_size)
197 iowrite32(v, adev->rio_mem + (reg * 4));
198 else {
199 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
200 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
201 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800202
203 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
204 udelay(500);
205 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206}
207
208/**
209 * amdgpu_mm_rdoorbell - read a doorbell dword
210 *
211 * @adev: amdgpu_device pointer
212 * @index: doorbell index
213 *
214 * Returns the value in the doorbell aperture at the
215 * requested doorbell index (CIK).
216 */
217u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
218{
219 if (index < adev->doorbell.num_doorbells) {
220 return readl(adev->doorbell.ptr + index);
221 } else {
222 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
223 return 0;
224 }
225}
226
227/**
228 * amdgpu_mm_wdoorbell - write a doorbell dword
229 *
230 * @adev: amdgpu_device pointer
231 * @index: doorbell index
232 * @v: value to write
233 *
234 * Writes @v to the doorbell aperture at the
235 * requested doorbell index (CIK).
236 */
237void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
238{
239 if (index < adev->doorbell.num_doorbells) {
240 writel(v, adev->doorbell.ptr + index);
241 } else {
242 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
243 }
244}
245
246/**
Ken Wang832be402016-03-18 15:23:08 +0800247 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
248 *
249 * @adev: amdgpu_device pointer
250 * @index: doorbell index
251 *
252 * Returns the value in the doorbell aperture at the
253 * requested doorbell index (VEGA10+).
254 */
255u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
256{
257 if (index < adev->doorbell.num_doorbells) {
258 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
259 } else {
260 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
261 return 0;
262 }
263}
264
265/**
266 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
267 *
268 * @adev: amdgpu_device pointer
269 * @index: doorbell index
270 * @v: value to write
271 *
272 * Writes @v to the doorbell aperture at the
273 * requested doorbell index (VEGA10+).
274 */
275void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
276{
277 if (index < adev->doorbell.num_doorbells) {
278 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
279 } else {
280 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
281 }
282}
283
284/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285 * amdgpu_invalid_rreg - dummy reg read function
286 *
287 * @adev: amdgpu device pointer
288 * @reg: offset of register
289 *
290 * Dummy register read function. Used for register blocks
291 * that certain asics don't have (all asics).
292 * Returns the value in the register.
293 */
294static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
295{
296 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
297 BUG();
298 return 0;
299}
300
301/**
302 * amdgpu_invalid_wreg - dummy reg write function
303 *
304 * @adev: amdgpu device pointer
305 * @reg: offset of register
306 * @v: value to write to the register
307 *
308 * Dummy register read function. Used for register blocks
309 * that certain asics don't have (all asics).
310 */
311static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
312{
313 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
314 reg, v);
315 BUG();
316}
317
318/**
319 * amdgpu_block_invalid_rreg - dummy reg read function
320 *
321 * @adev: amdgpu device pointer
322 * @block: offset of instance
323 * @reg: offset of register
324 *
325 * Dummy register read function. Used for register blocks
326 * that certain asics don't have (all asics).
327 * Returns the value in the register.
328 */
329static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
330 uint32_t block, uint32_t reg)
331{
332 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
333 reg, block);
334 BUG();
335 return 0;
336}
337
338/**
339 * amdgpu_block_invalid_wreg - dummy reg write function
340 *
341 * @adev: amdgpu device pointer
342 * @block: offset of instance
343 * @reg: offset of register
344 * @v: value to write to the register
345 *
346 * Dummy register read function. Used for register blocks
347 * that certain asics don't have (all asics).
348 */
349static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
350 uint32_t block,
351 uint32_t reg, uint32_t v)
352{
353 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
354 reg, block, v);
355 BUG();
356}
357
Alex Deucher06ec9072017-12-14 15:02:39 -0500358static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400359{
Christian Königa4a02772017-07-27 17:24:36 +0200360 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
361 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
362 &adev->vram_scratch.robj,
363 &adev->vram_scratch.gpu_addr,
364 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400365}
366
Alex Deucher06ec9072017-12-14 15:02:39 -0500367static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400368{
Christian König078af1a2017-07-27 17:43:00 +0200369 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400370}
371
372/**
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500373 * amdgpu_device_program_register_sequence - program an array of registers.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400374 *
375 * @adev: amdgpu_device pointer
376 * @registers: pointer to the register array
377 * @array_size: size of the register array
378 *
379 * Programs an array or registers with and and or masks.
380 * This is a helper for setting golden registers.
381 */
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500382void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
383 const u32 *registers,
384 const u32 array_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400385{
386 u32 tmp, reg, and_mask, or_mask;
387 int i;
388
389 if (array_size % 3)
390 return;
391
392 for (i = 0; i < array_size; i +=3) {
393 reg = registers[i + 0];
394 and_mask = registers[i + 1];
395 or_mask = registers[i + 2];
396
397 if (and_mask == 0xffffffff) {
398 tmp = or_mask;
399 } else {
400 tmp = RREG32(reg);
401 tmp &= ~and_mask;
402 tmp |= or_mask;
403 }
404 WREG32(reg, tmp);
405 }
406}
407
Alex Deucher8111c382017-12-14 16:22:53 -0500408void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409{
410 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
411}
412
413/*
414 * GPU doorbell aperture helpers function.
415 */
416/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500417 * amdgpu_device_doorbell_init - Init doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400418 *
419 * @adev: amdgpu_device pointer
420 *
421 * Init doorbell driver information (CIK)
422 * Returns 0 on success, error on failure.
423 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500424static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425{
Christian König705e5192017-06-08 11:15:16 +0200426 /* No doorbell on SI hardware generation */
427 if (adev->asic_type < CHIP_BONAIRE) {
428 adev->doorbell.base = 0;
429 adev->doorbell.size = 0;
430 adev->doorbell.num_doorbells = 0;
431 adev->doorbell.ptr = NULL;
432 return 0;
433 }
434
Christian Königd6895ad2017-02-28 10:36:43 +0100435 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
436 return -EINVAL;
437
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400438 /* doorbell bar mapping */
439 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
440 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
441
Christian Königedf600d2016-05-03 15:54:54 +0200442 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400443 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
444 if (adev->doorbell.num_doorbells == 0)
445 return -EINVAL;
446
Christian König8972e5d2017-03-06 13:34:57 +0100447 adev->doorbell.ptr = ioremap(adev->doorbell.base,
448 adev->doorbell.num_doorbells *
449 sizeof(u32));
450 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452
453 return 0;
454}
455
456/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500457 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400458 *
459 * @adev: amdgpu_device pointer
460 *
461 * Tear down doorbell driver information (CIK)
462 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500463static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464{
465 iounmap(adev->doorbell.ptr);
466 adev->doorbell.ptr = NULL;
467}
468
Alex Deucher22cb0162017-12-14 16:27:11 -0500469
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470
471/*
Alex Deucher06ec9072017-12-14 15:02:39 -0500472 * amdgpu_device_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400473 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400474 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475 */
476
477/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500478 * amdgpu_device_wb_fini - Disable Writeback and free memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479 *
480 * @adev: amdgpu_device pointer
481 *
482 * Disables Writeback and frees the Writeback memory (all asics).
483 * Used at driver shutdown.
484 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500485static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486{
487 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400488 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
489 &adev->wb.gpu_addr,
490 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491 adev->wb.wb_obj = NULL;
492 }
493}
494
495/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500496 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497 *
498 * @adev: amdgpu_device pointer
499 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400500 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501 * Used at driver startup.
502 * Returns 0 on success or an -error on failure.
503 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500504static int amdgpu_device_wb_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505{
506 int r;
507
508 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400509 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
510 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400511 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
512 &adev->wb.wb_obj, &adev->wb.gpu_addr,
513 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400514 if (r) {
515 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
516 return r;
517 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518
519 adev->wb.num_wb = AMDGPU_MAX_WB;
520 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
521
522 /* clear wb memory */
Monk Liu73469582017-12-29 17:06:41 +0800523 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524 }
525
526 return 0;
527}
528
529/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500530 * amdgpu_device_wb_get - Allocate a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400531 *
532 * @adev: amdgpu_device pointer
533 * @wb: wb index
534 *
535 * Allocate a wb slot for use by the driver (all asics).
536 * Returns 0 on success or -EINVAL on failure.
537 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500538int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539{
540 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400541
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 if (offset < adev->wb.num_wb) {
543 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800544 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400545 return 0;
546 } else {
547 return -EINVAL;
548 }
549}
550
Ken Wang70142852016-03-18 15:08:49 +0800551/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500552 * amdgpu_device_wb_free - Free a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 *
554 * @adev: amdgpu_device pointer
555 * @wb: wb index
556 *
557 * Free a wb slot allocated for use by the driver (all asics)
558 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500559void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560{
Monk Liu73469582017-12-29 17:06:41 +0800561 wb >>= 3;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 if (wb < adev->wb.num_wb)
Monk Liu73469582017-12-29 17:06:41 +0800563 __clear_bit(wb, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564}
565
566/**
Alex Deucher2543e282017-12-14 16:33:36 -0500567 * amdgpu_device_vram_location - try to find VRAM location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400568 * @adev: amdgpu device structure holding all necessary informations
569 * @mc: memory controller structure holding memory informations
570 * @base: base address at which to put VRAM
571 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400572 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100573 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574 */
Alex Deucher2543e282017-12-14 16:33:36 -0500575void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100576 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577{
578 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
579
580 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
582 if (limit && limit < mc->real_vram_size)
583 mc->real_vram_size = limit;
584 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
585 mc->mc_vram_size >> 20, mc->vram_start,
586 mc->vram_end, mc->real_vram_size >> 20);
587}
588
589/**
Alex Deucher2543e282017-12-14 16:33:36 -0500590 * amdgpu_device_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 * @adev: amdgpu device structure holding all necessary informations
592 * @mc: memory controller structure holding memory informations
593 *
594 * Function will place try to place GTT before or after VRAM.
595 *
596 * If GTT size is bigger than space left then we ajust GTT size.
597 * Thus function will never fails.
598 *
599 * FIXME: when reducing GTT size align new size on power of 2.
600 */
Alex Deucher2543e282017-12-14 16:33:36 -0500601void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100602 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603{
604 u64 size_af, size_bf;
605
Christian König770d13b2018-01-12 14:52:22 +0100606 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200607 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200609 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200611 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400612 }
Christian König6f02a692017-07-07 11:56:59 +0200613 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 } else {
Christian König6f02a692017-07-07 11:56:59 +0200615 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400616 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200617 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 }
Christian Königb98f1b92017-11-16 20:12:51 +0100619 /* VCE doesn't like it when BOs cross a 4GB segment, so align
620 * the GART base on a 4GB boundary as well.
621 */
622 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 }
Christian König6f02a692017-07-07 11:56:59 +0200624 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200626 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627}
628
Christian Königd6895ad2017-02-28 10:36:43 +0100629/**
630 * amdgpu_device_resize_fb_bar - try to resize FB BAR
631 *
632 * @adev: amdgpu_device pointer
633 *
634 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
635 * to fail, but if any of the BARs is not accessible after the size we abort
636 * driver loading by returning -ENODEV.
637 */
638int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
639{
Christian König770d13b2018-01-12 14:52:22 +0100640 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100641 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100642 struct pci_bus *root;
643 struct resource *res;
644 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100645 u16 cmd;
646 int r;
647
pding0c03b912017-11-07 11:02:00 +0800648 /* Bypass for VF */
649 if (amdgpu_sriov_vf(adev))
650 return 0;
651
Christian König31b8ada2017-11-15 20:07:38 +0100652 /* Check if the root BUS has 64bit memory resources */
653 root = adev->pdev->bus;
654 while (root->parent)
655 root = root->parent;
656
657 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100658 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100659 res->start > 0x100000000ull)
660 break;
661 }
662
663 /* Trying to resize is pointless without a root hub window above 4GB */
664 if (!res)
665 return 0;
666
Christian Königd6895ad2017-02-28 10:36:43 +0100667 /* Disable memory decoding while we change the BAR addresses and size */
668 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
669 pci_write_config_word(adev->pdev, PCI_COMMAND,
670 cmd & ~PCI_COMMAND_MEMORY);
671
672 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500673 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100674 if (adev->asic_type >= CHIP_BONAIRE)
675 pci_release_resource(adev->pdev, 2);
676
677 pci_release_resource(adev->pdev, 0);
678
679 r = pci_resize_resource(adev->pdev, 0, rbar_size);
680 if (r == -ENOSPC)
681 DRM_INFO("Not enough PCI address space for a large BAR.");
682 else if (r && r != -ENOTSUPP)
683 DRM_ERROR("Problem resizing BAR0 (%d).", r);
684
685 pci_assign_unassigned_bus_resources(adev->pdev->bus);
686
687 /* When the doorbell or fb BAR isn't available we have no chance of
688 * using the device.
689 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500690 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100691 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
692 return -ENODEV;
693
694 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
695
696 return 0;
697}
Horace Chena05502e2017-09-29 14:41:57 +0800698
699/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 * GPU helpers function.
701 */
702/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500703 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 *
705 * @adev: amdgpu_device pointer
706 *
Jim Quc836fec2017-02-10 15:59:59 +0800707 * Check if the asic has been initialized (all asics) at driver startup
708 * or post is needed if hw reset is performed.
709 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500711bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712{
713 uint32_t reg;
714
Monk Liubec86372016-09-14 19:38:08 +0800715 if (amdgpu_sriov_vf(adev))
716 return false;
717
718 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800719 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
720 * some old smc fw still need driver do vPost otherwise gpu hang, while
721 * those smc fw version above 22.15 doesn't have this flaw, so we force
722 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800723 */
724 if (adev->asic_type == CHIP_FIJI) {
725 int err;
726 uint32_t fw_ver;
727 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
728 /* force vPost if error occured */
729 if (err)
730 return true;
731
732 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800733 if (fw_ver < 0x00160e00)
734 return true;
Monk Liubec86372016-09-14 19:38:08 +0800735 }
Monk Liubec86372016-09-14 19:38:08 +0800736 }
pding91fe77e2017-10-19 09:38:39 +0800737
738 if (adev->has_hw_reset) {
739 adev->has_hw_reset = false;
740 return true;
741 }
742
743 /* bios scratch used on CIK+ */
744 if (adev->asic_type >= CHIP_BONAIRE)
745 return amdgpu_atombios_scratch_need_asic_init(adev);
746
747 /* check MEM_SIZE for older asics */
748 reg = amdgpu_asic_get_config_memsize(adev);
749
750 if ((reg != 0) && (reg != 0xffffffff))
751 return false;
752
753 return true;
Monk Liubec86372016-09-14 19:38:08 +0800754}
755
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756/* if we get transitioned to only one device, take VGA back */
757/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500758 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759 *
760 * @cookie: amdgpu_device pointer
761 * @state: enable/disable vga decode
762 *
763 * Enable/disable vga decode (all asics).
764 * Returns VGA resource flags.
765 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500766static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400767{
768 struct amdgpu_device *adev = cookie;
769 amdgpu_asic_set_vga_state(adev, state);
770 if (state)
771 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
772 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
773 else
774 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
775}
776
Alex Deucher06ec9072017-12-14 15:02:39 -0500777static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800778{
779 /* defines number of bits in page table versus page directory,
780 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
781 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800782 if (amdgpu_vm_block_size == -1)
783 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800784
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800785 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800786 dev_warn(adev->dev, "VM page table size (%d) too small\n",
787 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100788 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800789 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800790}
791
Alex Deucher06ec9072017-12-14 15:02:39 -0500792static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800793{
Alex Deucher64dab072017-06-15 18:20:09 -0400794 /* no need to check the default value */
795 if (amdgpu_vm_size == -1)
796 return;
797
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800798 if (amdgpu_vm_size < 1) {
799 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
800 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100801 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800802 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800803}
804
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500806 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400807 *
808 * @adev: amdgpu_device pointer
809 *
810 * Validates certain module parameters and updates
811 * the associated values used by the driver (all asics).
812 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500813static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814{
Chunming Zhou5b011232015-12-10 17:34:33 +0800815 if (amdgpu_sched_jobs < 4) {
816 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
817 amdgpu_sched_jobs);
818 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400819 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800820 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
821 amdgpu_sched_jobs);
822 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
823 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400824
Alex Deucher83e74db2017-08-21 11:58:25 -0400825 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200826 /* gart size must be greater or equal to 32M */
827 dev_warn(adev->dev, "gart size (%d) too small\n",
828 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400829 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400830 }
831
Christian König36d38372017-07-07 13:17:45 +0200832 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400833 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200834 dev_warn(adev->dev, "gtt size (%d) too small\n",
835 amdgpu_gtt_size);
836 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400837 }
838
Roger Hed07f14b2017-08-15 16:05:59 +0800839 /* valid range is between 4 and 9 inclusive */
840 if (amdgpu_vm_fragment_size != -1 &&
841 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
842 dev_warn(adev->dev, "valid range is between 4 and 9\n");
843 amdgpu_vm_fragment_size = -1;
844 }
845
Alex Deucher06ec9072017-12-14 15:02:39 -0500846 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400847
Alex Deucher06ec9072017-12-14 15:02:39 -0500848 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +0200849
jimqu526bae32016-11-07 09:53:10 +0800850 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -0400851 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +0200852 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
853 amdgpu_vram_page_split);
854 amdgpu_vram_page_split = 1024;
855 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -0500856
857 if (amdgpu_lockup_timeout == 0) {
858 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
859 amdgpu_lockup_timeout = 10000;
860 }
Alex Deucher19aede72018-03-09 15:06:35 -0500861
862 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400863}
864
865/**
866 * amdgpu_switcheroo_set_state - set switcheroo state
867 *
868 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +0200869 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400870 *
871 * Callback for the switcheroo driver. Suspends or resumes the
872 * the asics before or after it is powered up using ACPI methods.
873 */
874static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
875{
876 struct drm_device *dev = pci_get_drvdata(pdev);
877
878 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
879 return;
880
881 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -0800882 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400883 /* don't suspend or resume card normally */
884 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
885
Alex Deucher810ddc32016-08-23 13:25:49 -0400886 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400887
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400888 dev->switch_power_state = DRM_SWITCH_POWER_ON;
889 drm_kms_helper_poll_enable(dev);
890 } else {
Joe Perches7ca85292017-02-28 04:55:52 -0800891 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892 drm_kms_helper_poll_disable(dev);
893 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -0400894 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400895 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
896 }
897}
898
899/**
900 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
901 *
902 * @pdev: pci dev pointer
903 *
904 * Callback for the switcheroo driver. Check of the switcheroo
905 * state can be changed.
906 * Returns true if the state can be changed, false if not.
907 */
908static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
909{
910 struct drm_device *dev = pci_get_drvdata(pdev);
911
912 /*
913 * FIXME: open_count is protected by drm_global_mutex but that would lead to
914 * locking inversion with the driver load path. And the access here is
915 * completely racy anyway. So don't bother with locking for now.
916 */
917 return dev->open_count == 0;
918}
919
920static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
921 .set_gpu_state = amdgpu_switcheroo_set_state,
922 .reprobe = NULL,
923 .can_switch = amdgpu_switcheroo_can_switch,
924};
925
Alex Deucher2990a1f2017-12-15 16:18:00 -0500926int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
927 enum amd_ip_block_type block_type,
928 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929{
930 int i, r = 0;
931
932 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400933 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400934 continue;
Rex Zhuc7228652017-02-22 15:33:46 +0800935 if (adev->ip_blocks[i].version->type != block_type)
936 continue;
937 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
938 continue;
939 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
940 (void *)adev, state);
941 if (r)
942 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
943 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400944 }
945 return r;
946}
947
Alex Deucher2990a1f2017-12-15 16:18:00 -0500948int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
949 enum amd_ip_block_type block_type,
950 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951{
952 int i, r = 0;
953
954 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400955 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400956 continue;
Rex Zhuc7228652017-02-22 15:33:46 +0800957 if (adev->ip_blocks[i].version->type != block_type)
958 continue;
959 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
960 continue;
961 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
962 (void *)adev, state);
963 if (r)
964 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
965 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966 }
967 return r;
968}
969
Alex Deucher2990a1f2017-12-15 16:18:00 -0500970void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
971 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +0800972{
973 int i;
974
975 for (i = 0; i < adev->num_ip_blocks; i++) {
976 if (!adev->ip_blocks[i].status.valid)
977 continue;
978 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
979 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
980 }
981}
982
Alex Deucher2990a1f2017-12-15 16:18:00 -0500983int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
984 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -0400985{
986 int i, r;
987
988 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -0400989 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -0400990 continue;
Alex Deuchera1255102016-10-13 17:41:13 -0400991 if (adev->ip_blocks[i].version->type == block_type) {
992 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -0400993 if (r)
994 return r;
995 break;
996 }
997 }
998 return 0;
999
1000}
1001
Alex Deucher2990a1f2017-12-15 16:18:00 -05001002bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1003 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001004{
1005 int i;
1006
1007 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001008 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001009 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001010 if (adev->ip_blocks[i].version->type == block_type)
1011 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001012 }
1013 return true;
1014
1015}
1016
Alex Deucher2990a1f2017-12-15 16:18:00 -05001017struct amdgpu_ip_block *
1018amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1019 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020{
1021 int i;
1022
1023 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001024 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001025 return &adev->ip_blocks[i];
1026
1027 return NULL;
1028}
1029
1030/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001031 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001032 *
1033 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001034 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001035 * @major: major version
1036 * @minor: minor version
1037 *
1038 * return 0 if equal or greater
1039 * return 1 if smaller or the ip_block doesn't exist
1040 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001041int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1042 enum amd_ip_block_type type,
1043 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001044{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001045 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046
Alex Deuchera1255102016-10-13 17:41:13 -04001047 if (ip_block && ((ip_block->version->major > major) ||
1048 ((ip_block->version->major == major) &&
1049 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050 return 0;
1051
1052 return 1;
1053}
1054
Alex Deuchera1255102016-10-13 17:41:13 -04001055/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001056 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001057 *
1058 * @adev: amdgpu_device pointer
1059 * @ip_block_version: pointer to the IP to add
1060 *
1061 * Adds the IP block driver information to the collection of IPs
1062 * on the asic.
1063 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001064int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1065 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001066{
1067 if (!ip_block_version)
1068 return -EINVAL;
1069
Shaoyun Liue966a722018-02-01 16:45:26 -05001070 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001071 ip_block_version->funcs->name);
1072
Alex Deuchera1255102016-10-13 17:41:13 -04001073 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1074
1075 return 0;
1076}
1077
Alex Deucher483ef982016-09-30 12:43:04 -04001078static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001079{
1080 adev->enable_virtual_display = false;
1081
1082 if (amdgpu_virtual_display) {
1083 struct drm_device *ddev = adev->ddev;
1084 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001085 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001086
1087 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1088 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001089 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1090 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001091 if (!strcmp("all", pciaddname)
1092 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001093 long num_crtc;
1094 int res = -1;
1095
Emily Deng9accf2f2016-08-10 16:01:25 +08001096 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001097
1098 if (pciaddname_tmp)
1099 res = kstrtol(pciaddname_tmp, 10,
1100 &num_crtc);
1101
1102 if (!res) {
1103 if (num_crtc < 1)
1104 num_crtc = 1;
1105 if (num_crtc > 6)
1106 num_crtc = 6;
1107 adev->mode_info.num_crtc = num_crtc;
1108 } else {
1109 adev->mode_info.num_crtc = 1;
1110 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001111 break;
1112 }
1113 }
1114
Emily Deng0f663562016-09-30 13:02:18 -04001115 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1116 amdgpu_virtual_display, pci_address_name,
1117 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001118
1119 kfree(pciaddstr);
1120 }
1121}
1122
Alex Deuchere2a75f82017-04-27 16:58:01 -04001123static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1124{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001125 const char *chip_name;
1126 char fw_name[30];
1127 int err;
1128 const struct gpu_info_firmware_header_v1_0 *hdr;
1129
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001130 adev->firmware.gpu_info_fw = NULL;
1131
Alex Deuchere2a75f82017-04-27 16:58:01 -04001132 switch (adev->asic_type) {
1133 case CHIP_TOPAZ:
1134 case CHIP_TONGA:
1135 case CHIP_FIJI:
1136 case CHIP_POLARIS11:
1137 case CHIP_POLARIS10:
1138 case CHIP_POLARIS12:
1139 case CHIP_CARRIZO:
1140 case CHIP_STONEY:
1141#ifdef CONFIG_DRM_AMDGPU_SI
1142 case CHIP_VERDE:
1143 case CHIP_TAHITI:
1144 case CHIP_PITCAIRN:
1145 case CHIP_OLAND:
1146 case CHIP_HAINAN:
1147#endif
1148#ifdef CONFIG_DRM_AMDGPU_CIK
1149 case CHIP_BONAIRE:
1150 case CHIP_HAWAII:
1151 case CHIP_KAVERI:
1152 case CHIP_KABINI:
1153 case CHIP_MULLINS:
1154#endif
1155 default:
1156 return 0;
1157 case CHIP_VEGA10:
1158 chip_name = "vega10";
1159 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001160 case CHIP_RAVEN:
1161 chip_name = "raven";
1162 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001163 }
1164
1165 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001166 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001167 if (err) {
1168 dev_err(adev->dev,
1169 "Failed to load gpu_info firmware \"%s\"\n",
1170 fw_name);
1171 goto out;
1172 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001173 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001174 if (err) {
1175 dev_err(adev->dev,
1176 "Failed to validate gpu_info firmware \"%s\"\n",
1177 fw_name);
1178 goto out;
1179 }
1180
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001181 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001182 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1183
1184 switch (hdr->version_major) {
1185 case 1:
1186 {
1187 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001188 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001189 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1190
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001191 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1192 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1193 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1194 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 -04001195 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001196 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1197 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1198 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1199 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1200 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001201 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001202 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1203 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001204 adev->gfx.cu_info.max_waves_per_simd =
1205 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1206 adev->gfx.cu_info.max_scratch_slots_per_cu =
1207 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1208 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001209 break;
1210 }
1211 default:
1212 dev_err(adev->dev,
1213 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1214 err = -EINVAL;
1215 goto out;
1216 }
1217out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001218 return err;
1219}
1220
Alex Deucher06ec9072017-12-14 15:02:39 -05001221static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001223 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001224
Alex Deucher483ef982016-09-30 12:43:04 -04001225 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001226
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001227 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001228 case CHIP_TOPAZ:
1229 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001230 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001231 case CHIP_POLARIS11:
1232 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001233 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001234 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001235 case CHIP_STONEY:
1236 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001237 adev->family = AMDGPU_FAMILY_CZ;
1238 else
1239 adev->family = AMDGPU_FAMILY_VI;
1240
1241 r = vi_set_ip_blocks(adev);
1242 if (r)
1243 return r;
1244 break;
Ken Wang33f34802016-01-21 17:29:41 +08001245#ifdef CONFIG_DRM_AMDGPU_SI
1246 case CHIP_VERDE:
1247 case CHIP_TAHITI:
1248 case CHIP_PITCAIRN:
1249 case CHIP_OLAND:
1250 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001251 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001252 r = si_set_ip_blocks(adev);
1253 if (r)
1254 return r;
1255 break;
1256#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001257#ifdef CONFIG_DRM_AMDGPU_CIK
1258 case CHIP_BONAIRE:
1259 case CHIP_HAWAII:
1260 case CHIP_KAVERI:
1261 case CHIP_KABINI:
1262 case CHIP_MULLINS:
1263 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1264 adev->family = AMDGPU_FAMILY_CI;
1265 else
1266 adev->family = AMDGPU_FAMILY_KV;
1267
1268 r = cik_set_ip_blocks(adev);
1269 if (r)
1270 return r;
1271 break;
1272#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001273 case CHIP_VEGA10:
1274 case CHIP_RAVEN:
1275 if (adev->asic_type == CHIP_RAVEN)
1276 adev->family = AMDGPU_FAMILY_RV;
1277 else
1278 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001279
1280 r = soc15_set_ip_blocks(adev);
1281 if (r)
1282 return r;
1283 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284 default:
1285 /* FIXME: not supported yet */
1286 return -EINVAL;
1287 }
1288
Alex Deuchere2a75f82017-04-27 16:58:01 -04001289 r = amdgpu_device_parse_gpu_info_fw(adev);
1290 if (r)
1291 return r;
1292
pding18847342017-11-06 10:21:26 +08001293 amdgpu_amdkfd_device_probe(adev);
1294
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001295 if (amdgpu_sriov_vf(adev)) {
1296 r = amdgpu_virt_request_full_gpu(adev, true);
1297 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001298 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001299 }
1300
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301 for (i = 0; i < adev->num_ip_blocks; i++) {
1302 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001303 DRM_ERROR("disabled ip block: %d <%s>\n",
1304 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001305 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001307 if (adev->ip_blocks[i].version->funcs->early_init) {
1308 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001309 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001310 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001311 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001312 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1313 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001315 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001316 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001317 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001318 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001319 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001321 }
1322 }
1323
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001324 adev->cg_flags &= amdgpu_cg_mask;
1325 adev->pg_flags &= amdgpu_pg_mask;
1326
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001327 return 0;
1328}
1329
Alex Deucher06ec9072017-12-14 15:02:39 -05001330static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001331{
1332 int i, r;
1333
1334 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001335 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001337 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001338 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001339 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1340 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001342 }
Alex Deuchera1255102016-10-13 17:41:13 -04001343 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001344
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001345 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001346 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001347 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001348 if (r) {
1349 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001350 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001351 }
Alex Deuchera1255102016-10-13 17:41:13 -04001352 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001353 if (r) {
1354 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001355 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001356 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001357 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001358 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001359 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001360 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001361 }
Alex Deuchera1255102016-10-13 17:41:13 -04001362 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001363
1364 /* right after GMC hw init, we create CSA */
1365 if (amdgpu_sriov_vf(adev)) {
1366 r = amdgpu_allocate_static_csa(adev);
1367 if (r) {
1368 DRM_ERROR("allocate CSA failed %d\n", r);
1369 return r;
1370 }
1371 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372 }
1373 }
1374
1375 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001376 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001378 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001380 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001381 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001382 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1383 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001385 }
Alex Deuchera1255102016-10-13 17:41:13 -04001386 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001387 }
1388
pding18847342017-11-06 10:21:26 +08001389 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001390
1391 if (amdgpu_sriov_vf(adev))
1392 amdgpu_virt_release_full_gpu(adev, true);
1393
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001394 return 0;
1395}
1396
Alex Deucher06ec9072017-12-14 15:02:39 -05001397static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001398{
1399 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1400}
1401
Alex Deucher06ec9072017-12-14 15:02:39 -05001402static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001403{
1404 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1405 AMDGPU_RESET_MAGIC_NUM);
1406}
1407
Alex Deucher06ec9072017-12-14 15:02:39 -05001408static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301409{
1410 int i = 0, r;
1411
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001412 if (amdgpu_emu_mode == 1)
1413 return 0;
1414
Shirish S2dc80b02017-05-25 10:05:25 +05301415 for (i = 0; i < adev->num_ip_blocks; i++) {
1416 if (!adev->ip_blocks[i].status.valid)
1417 continue;
1418 /* skip CG for VCE/UVD, it's handled specially */
1419 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1420 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1421 /* enable clockgating to save power */
1422 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1423 AMD_CG_STATE_GATE);
1424 if (r) {
1425 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1426 adev->ip_blocks[i].version->funcs->name, r);
1427 return r;
1428 }
1429 }
1430 }
1431 return 0;
1432}
1433
Alex Deucher06ec9072017-12-14 15:02:39 -05001434static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001435{
1436 int i = 0, r;
1437
1438 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001439 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001440 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001441 if (adev->ip_blocks[i].version->funcs->late_init) {
1442 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001443 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001444 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1445 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001446 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001447 }
Alex Deuchera1255102016-10-13 17:41:13 -04001448 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001449 }
1450 }
1451
Shirish S2dc80b02017-05-25 10:05:25 +05301452 mod_delayed_work(system_wq, &adev->late_init_work,
1453 msecs_to_jiffies(AMDGPU_RESUME_MS));
1454
Alex Deucher06ec9072017-12-14 15:02:39 -05001455 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001456
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001457 return 0;
1458}
1459
Alex Deucher06ec9072017-12-14 15:02:39 -05001460static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001461{
1462 int i, r;
1463
pding18847342017-11-06 10:21:26 +08001464 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001465 /* need to disable SMC first */
1466 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001467 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001468 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001469 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001470 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001471 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1472 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001473 if (r) {
1474 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001475 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001476 return r;
1477 }
Alex Deuchera1255102016-10-13 17:41:13 -04001478 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001479 /* XXX handle errors */
1480 if (r) {
1481 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001482 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001483 }
Alex Deuchera1255102016-10-13 17:41:13 -04001484 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001485 break;
1486 }
1487 }
1488
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Emily Dengedc3d272018-03-08 10:49:09 +08001490 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1491 adev->firmware.load_type == AMDGPU_FW_LOAD_SMU)
1492 amdgpu_ucode_fini_bo(adev);
Alex Deuchera1255102016-10-13 17:41:13 -04001493 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494 continue;
Rex Zhu8201a672016-11-24 21:44:44 +08001495
1496 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1497 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1498 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1499 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1500 AMD_CG_STATE_UNGATE);
1501 if (r) {
1502 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1503 adev->ip_blocks[i].version->funcs->name, r);
1504 return r;
1505 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001506 }
Rex Zhu8201a672016-11-24 21:44:44 +08001507
Alex Deuchera1255102016-10-13 17:41:13 -04001508 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001510 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001511 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1512 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001513 }
Rex Zhu8201a672016-11-24 21:44:44 +08001514
Alex Deuchera1255102016-10-13 17:41:13 -04001515 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001516 }
1517
Alex Deucher9950cda2018-01-18 19:05:36 -05001518 /* disable all interrupts */
1519 amdgpu_irq_disable_all(adev);
1520
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001521 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001522 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 continue;
Monk Liuc12aba32018-01-24 12:20:32 +08001524
1525 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1526 amdgpu_free_static_csa(adev);
1527 amdgpu_device_wb_fini(adev);
1528 amdgpu_device_vram_scratch_fini(adev);
1529 }
1530
Alex Deuchera1255102016-10-13 17:41:13 -04001531 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001532 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001533 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001534 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1535 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001536 }
Alex Deuchera1255102016-10-13 17:41:13 -04001537 adev->ip_blocks[i].status.sw = false;
1538 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539 }
1540
Monk Liua6dcfd92016-05-19 14:36:34 +08001541 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001542 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001543 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001544 if (adev->ip_blocks[i].version->funcs->late_fini)
1545 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1546 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001547 }
1548
Monk Liu030308f2017-09-15 15:34:52 +08001549 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001550 if (amdgpu_virt_release_full_gpu(adev, false))
1551 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001552
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553 return 0;
1554}
1555
Alex Deucher06ec9072017-12-14 15:02:39 -05001556static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301557{
1558 struct amdgpu_device *adev =
1559 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001560 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301561}
1562
Alex Deuchercdd61df2017-12-14 16:47:40 -05001563int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001564{
1565 int i, r;
1566
Xiangliang Yue941ea92017-01-18 12:47:55 +08001567 if (amdgpu_sriov_vf(adev))
1568 amdgpu_virt_request_full_gpu(adev, false);
1569
Flora Cuic5a93a22016-02-26 10:45:25 +08001570 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001571 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1572 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001573 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001574 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001575 }
1576
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001578 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001579 continue;
1580 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001581 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001582 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1583 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001584 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001585 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1586 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001587 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001588 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001590 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001592 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001593 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1594 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001595 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596 }
1597
Xiangliang Yue941ea92017-01-18 12:47:55 +08001598 if (amdgpu_sriov_vf(adev))
1599 amdgpu_virt_release_full_gpu(adev, false);
1600
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601 return 0;
1602}
1603
Alex Deucher06ec9072017-12-14 15:02:39 -05001604static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001605{
1606 int i, r;
1607
Monk Liu2cb681b2017-04-26 12:00:49 +08001608 static enum amd_ip_block_type ip_order[] = {
1609 AMD_IP_BLOCK_TYPE_GMC,
1610 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001611 AMD_IP_BLOCK_TYPE_IH,
1612 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001613
Monk Liu2cb681b2017-04-26 12:00:49 +08001614 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1615 int j;
1616 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001617
Monk Liu2cb681b2017-04-26 12:00:49 +08001618 for (j = 0; j < adev->num_ip_blocks; j++) {
1619 block = &adev->ip_blocks[j];
1620
1621 if (block->version->type != ip_order[i] ||
1622 !block->status.valid)
1623 continue;
1624
1625 r = block->version->funcs->hw_init(adev);
1626 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001627 if (r)
1628 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001629 }
1630 }
1631
1632 return 0;
1633}
1634
Alex Deucher06ec9072017-12-14 15:02:39 -05001635static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001636{
1637 int i, r;
1638
Monk Liu2cb681b2017-04-26 12:00:49 +08001639 static enum amd_ip_block_type ip_order[] = {
1640 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001641 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001642 AMD_IP_BLOCK_TYPE_DCE,
1643 AMD_IP_BLOCK_TYPE_GFX,
1644 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001645 AMD_IP_BLOCK_TYPE_UVD,
1646 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001647 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001648
Monk Liu2cb681b2017-04-26 12:00:49 +08001649 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1650 int j;
1651 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001652
Monk Liu2cb681b2017-04-26 12:00:49 +08001653 for (j = 0; j < adev->num_ip_blocks; j++) {
1654 block = &adev->ip_blocks[j];
1655
1656 if (block->version->type != ip_order[i] ||
1657 !block->status.valid)
1658 continue;
1659
1660 r = block->version->funcs->hw_init(adev);
1661 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001662 if (r)
1663 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001664 }
1665 }
1666
1667 return 0;
1668}
1669
Alex Deucher06ec9072017-12-14 15:02:39 -05001670static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671{
1672 int i, r;
1673
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674 for (i = 0; i < adev->num_ip_blocks; i++) {
1675 if (!adev->ip_blocks[i].status.valid)
1676 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001677 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1678 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1679 adev->ip_blocks[i].version->type ==
1680 AMD_IP_BLOCK_TYPE_IH) {
1681 r = adev->ip_blocks[i].version->funcs->resume(adev);
1682 if (r) {
1683 DRM_ERROR("resume of IP block <%s> failed %d\n",
1684 adev->ip_blocks[i].version->funcs->name, r);
1685 return r;
1686 }
1687 }
1688 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689
Chunming Zhoufcf06492017-05-05 10:33:33 +08001690 return 0;
1691}
1692
Alex Deucher06ec9072017-12-14 15:02:39 -05001693static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08001694{
1695 int i, r;
1696
1697 for (i = 0; i < adev->num_ip_blocks; i++) {
1698 if (!adev->ip_blocks[i].status.valid)
1699 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1701 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1702 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1703 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001704 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 if (r) {
1706 DRM_ERROR("resume of IP block <%s> failed %d\n",
1707 adev->ip_blocks[i].version->funcs->name, r);
1708 return r;
1709 }
1710 }
1711
1712 return 0;
1713}
1714
Alex Deucher06ec9072017-12-14 15:02:39 -05001715static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001716{
Chunming Zhoufcf06492017-05-05 10:33:33 +08001717 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001718
Alex Deucher06ec9072017-12-14 15:02:39 -05001719 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08001720 if (r)
1721 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05001722 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001723
Chunming Zhoufcf06492017-05-05 10:33:33 +08001724 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001725}
1726
Monk Liu4e99a442016-03-31 13:26:59 +08001727static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001728{
Monk Liu6867e1b2017-10-16 19:50:44 +08001729 if (amdgpu_sriov_vf(adev)) {
1730 if (adev->is_atom_fw) {
1731 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1732 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1733 } else {
1734 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1735 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1736 }
1737
1738 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
1739 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001740 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001741}
1742
Harry Wentland45622362017-09-12 15:58:20 -04001743bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
1744{
1745 switch (asic_type) {
1746#if defined(CONFIG_DRM_AMD_DC)
1747 case CHIP_BONAIRE:
1748 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04001749 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05001750 case CHIP_KABINI:
1751 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04001752 case CHIP_CARRIZO:
1753 case CHIP_STONEY:
1754 case CHIP_POLARIS11:
1755 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04001756 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04001757 case CHIP_TONGA:
1758 case CHIP_FIJI:
1759#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
1760 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04001761#endif
Harry Wentland42f8ffa2017-09-15 14:07:30 -04001762 case CHIP_VEGA10:
1763#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08001764 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04001765#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08001766 return amdgpu_dc != 0;
1767#endif
Harry Wentland45622362017-09-12 15:58:20 -04001768 default:
1769 return false;
1770 }
1771}
1772
1773/**
1774 * amdgpu_device_has_dc_support - check if dc is supported
1775 *
1776 * @adev: amdgpu_device_pointer
1777 *
1778 * Returns true for supported, false for not supported
1779 */
1780bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
1781{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08001782 if (amdgpu_sriov_vf(adev))
1783 return false;
1784
Harry Wentland45622362017-09-12 15:58:20 -04001785 return amdgpu_device_asic_has_dc_support(adev->asic_type);
1786}
1787
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001788/**
1789 * amdgpu_device_init - initialize the driver
1790 *
1791 * @adev: amdgpu_device pointer
1792 * @pdev: drm dev pointer
1793 * @pdev: pci dev pointer
1794 * @flags: driver flags
1795 *
1796 * Initializes the driver info and hw (all asics).
1797 * Returns 0 for success or an error on failure.
1798 * Called at driver startup.
1799 */
1800int amdgpu_device_init(struct amdgpu_device *adev,
1801 struct drm_device *ddev,
1802 struct pci_dev *pdev,
1803 uint32_t flags)
1804{
1805 int r, i;
1806 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001807 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808
1809 adev->shutdown = false;
1810 adev->dev = &pdev->dev;
1811 adev->ddev = ddev;
1812 adev->pdev = pdev;
1813 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001814 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05001816 if (amdgpu_emu_mode == 1)
1817 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01001818 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819 adev->accel_working = false;
1820 adev->num_rings = 0;
1821 adev->mman.buffer_funcs = NULL;
1822 adev->mman.buffer_funcs_ring = NULL;
1823 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001824 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01001825 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001826 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04001827 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001828
1829 adev->smc_rreg = &amdgpu_invalid_rreg;
1830 adev->smc_wreg = &amdgpu_invalid_wreg;
1831 adev->pcie_rreg = &amdgpu_invalid_rreg;
1832 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001833 adev->pciep_rreg = &amdgpu_invalid_rreg;
1834 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001835 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1836 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1837 adev->didt_rreg = &amdgpu_invalid_rreg;
1838 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001839 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1840 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001841 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1842 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1843
Alex Deucher3e39ab92015-06-05 15:04:33 -04001844 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1845 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1846 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001847
1848 /* mutex initialization are all done here so we
1849 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001850 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05001851 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001852 mutex_init(&adev->pm.mutex);
1853 mutex_init(&adev->gfx.gpu_clock_mutex);
1854 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04001855 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001856 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04001858 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001859 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08001860 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001861
Alex Deucher06ec9072017-12-14 15:02:39 -05001862 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001863
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864 spin_lock_init(&adev->mmio_idx_lock);
1865 spin_lock_init(&adev->smc_idx_lock);
1866 spin_lock_init(&adev->pcie_idx_lock);
1867 spin_lock_init(&adev->uvd_ctx_idx_lock);
1868 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001869 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08001870 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001871 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001872 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001873
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001874 INIT_LIST_HEAD(&adev->shadow_list);
1875 mutex_init(&adev->shadow_list_lock);
1876
Andres Rodriguez795f2812017-03-06 16:27:55 -05001877 INIT_LIST_HEAD(&adev->ring_lru_list);
1878 spin_lock_init(&adev->ring_lru_list_lock);
1879
Alex Deucher06ec9072017-12-14 15:02:39 -05001880 INIT_DELAYED_WORK(&adev->late_init_work,
1881 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05301882
Alex Xie0fa49552017-06-08 14:58:05 -04001883 /* Registers mapping */
1884 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08001885 if (adev->asic_type >= CHIP_BONAIRE) {
1886 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1887 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1888 } else {
1889 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1890 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1891 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001892
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001893 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1894 if (adev->rmmio == NULL) {
1895 return -ENOMEM;
1896 }
1897 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1898 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1899
Christian König705e5192017-06-08 11:15:16 +02001900 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05001901 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001902
1903 /* io port mapping */
1904 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1905 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1906 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1907 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1908 break;
1909 }
1910 }
1911 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001912 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001913
Alex Deucher5494d862018-03-09 15:14:11 -05001914 amdgpu_device_get_pcie_info(adev);
1915
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001916 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05001917 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001918 if (r)
1919 return r;
1920
1921 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1922 /* this will fail for cards that aren't VGA class devices, just
1923 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05001924 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001925
Alex Deuchere9bef452016-04-25 13:12:18 -04001926 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001927 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01001928 if (!pci_is_thunderbolt_attached(adev->pdev))
1929 vga_switcheroo_register_client(adev->pdev,
1930 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 if (runtime)
1932 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1933
Shaoyun Liu9475a942018-02-01 18:13:23 -05001934 if (amdgpu_emu_mode == 1) {
1935 /* post the asic on emulation mode */
1936 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05001937 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05001938 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05001939
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001941 if (!amdgpu_get_bios(adev)) {
1942 r = -EINVAL;
1943 goto failed;
1944 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001945
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001946 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001947 if (r) {
1948 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001949 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04001950 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001951 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001952
Monk Liu4e99a442016-03-31 13:26:59 +08001953 /* detect if we are with an SRIOV vbios */
1954 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001955
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001956 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05001957 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001959 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001960 r = -EINVAL;
1961 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962 }
Monk Liubec86372016-09-14 19:38:08 +08001963 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001964 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1965 if (r) {
1966 dev_err(adev->dev, "gpu post error!\n");
1967 goto failed;
1968 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969 }
1970
Alex Deucher88b64e92017-07-10 10:43:10 -04001971 if (adev->is_atom_fw) {
1972 /* Initialize clocks */
1973 r = amdgpu_atomfirmware_get_clock_info(adev);
1974 if (r) {
1975 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001976 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04001977 goto failed;
1978 }
1979 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001980 /* Initialize clocks */
1981 r = amdgpu_atombios_get_clock_info(adev);
1982 if (r) {
1983 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001984 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04001985 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001986 }
1987 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04001988 if (!amdgpu_device_has_dc_support(adev))
1989 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001990 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001991
Shaoyun Liubfca0282018-02-01 17:37:50 -05001992fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001993 /* Fence driver */
1994 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001995 if (r) {
1996 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04001997 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04001998 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001999 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002000
2001 /* init the mode config */
2002 drm_mode_config_init(adev->ddev);
2003
Alex Deucher06ec9072017-12-14 15:02:39 -05002004 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 if (r) {
pding8840a382017-10-23 17:22:09 +08002006 /* failed in exclusive mode due to timeout */
2007 if (amdgpu_sriov_vf(adev) &&
2008 !amdgpu_sriov_runtime(adev) &&
2009 amdgpu_virt_mmio_blocked(adev) &&
2010 !amdgpu_virt_wait_reset(adev)) {
2011 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002012 /* Don't send request since VF is inactive. */
2013 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2014 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002015 r = -EAGAIN;
2016 goto failed;
2017 }
Alex Deucher06ec9072017-12-14 15:02:39 -05002018 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002019 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher06ec9072017-12-14 15:02:39 -05002020 amdgpu_device_ip_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002021 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002022 }
2023
2024 adev->accel_working = true;
2025
Alex Xiee59c0202017-06-01 09:42:59 -04002026 amdgpu_vm_check_compute_bug(adev);
2027
Marek Olšák95844d22016-08-17 23:49:27 +02002028 /* Initialize the buffer migration limit. */
2029 if (amdgpu_moverate >= 0)
2030 max_MBps = amdgpu_moverate;
2031 else
2032 max_MBps = 8; /* Allow 8 MB/s. */
2033 /* Get a log2 for easy divisions. */
2034 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2035
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036 r = amdgpu_ib_pool_init(adev);
2037 if (r) {
2038 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002039 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002040 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002041 }
2042
2043 r = amdgpu_ib_ring_tests(adev);
2044 if (r)
2045 DRM_ERROR("ib ring test failed (%d).\n", r);
2046
Horace Chen2dc8f812017-10-09 16:17:16 +08002047 if (amdgpu_sriov_vf(adev))
2048 amdgpu_virt_init_data_exchange(adev);
2049
Monk Liu9bc92b92017-02-08 17:38:13 +08002050 amdgpu_fbdev_init(adev);
2051
Rex Zhud2f52ac2017-09-22 17:47:27 +08002052 r = amdgpu_pm_sysfs_init(adev);
2053 if (r)
2054 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2055
Alex Deucher75758252017-12-14 15:23:14 -05002056 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002057 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002058 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059
2060 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002061 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002062 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063
Huang Rui50ab2532016-06-12 15:51:09 +08002064 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002065 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002066 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002067
Christian König763efb62017-12-06 15:44:51 +01002068 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002069 if (r)
Christian König763efb62017-12-06 15:44:51 +01002070 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002071
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002072 if ((amdgpu_testing & 1)) {
2073 if (adev->accel_working)
2074 amdgpu_test_moves(adev);
2075 else
2076 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2077 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002078 if (amdgpu_benchmarking) {
2079 if (adev->accel_working)
2080 amdgpu_benchmark(adev, amdgpu_benchmarking);
2081 else
2082 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2083 }
2084
2085 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2086 * explicit gating rather than handling it automatically.
2087 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002088 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002089 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002090 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002091 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002092 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002093 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002094
2095 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002096
2097failed:
Gavin Wan89041942017-06-23 13:55:15 -04002098 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002099 if (runtime)
2100 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002101
Alex Deucher83ba1262016-06-03 18:21:41 -04002102 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002103}
2104
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002105/**
2106 * amdgpu_device_fini - tear down the driver
2107 *
2108 * @adev: amdgpu_device pointer
2109 *
2110 * Tear down the driver info (all asics).
2111 * Called at driver shutdown.
2112 */
2113void amdgpu_device_fini(struct amdgpu_device *adev)
2114{
2115 int r;
2116
2117 DRM_INFO("amdgpu: finishing device.\n");
2118 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002119 if (adev->mode_info.mode_config_initialized)
2120 drm_crtc_force_disable_all(adev->ddev);
Monk Liub9141cd2017-11-22 19:21:43 +08002121
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002122 amdgpu_ib_pool_fini(adev);
2123 amdgpu_fence_driver_fini(adev);
Emily Deng58e955d2018-03-08 09:35:19 +08002124 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002125 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002126 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002127 if (adev->firmware.gpu_info_fw) {
2128 release_firmware(adev->firmware.gpu_info_fw);
2129 adev->firmware.gpu_info_fw = NULL;
2130 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002131 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302132 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002133 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002134 if (!amdgpu_device_has_dc_support(adev))
2135 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002136
2137 if (amdgpu_emu_mode != 1)
2138 amdgpu_atombios_fini(adev);
2139
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002140 kfree(adev->bios);
2141 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002142 if (!pci_is_thunderbolt_attached(adev->pdev))
2143 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002144 if (adev->flags & AMD_IS_PX)
2145 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002146 vga_client_register(adev->pdev, NULL, NULL, NULL);
2147 if (adev->rio_mem)
2148 pci_iounmap(adev->pdev, adev->rio_mem);
2149 adev->rio_mem = NULL;
2150 iounmap(adev->rmmio);
2151 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002152 amdgpu_device_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002153 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002154}
2155
2156
2157/*
2158 * Suspend & resume.
2159 */
2160/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002161 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002162 *
2163 * @pdev: drm dev pointer
2164 * @state: suspend state
2165 *
2166 * Puts the hw in the suspend state (all asics).
2167 * Returns 0 for success or an error on failure.
2168 * Called at driver suspend.
2169 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002170int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171{
2172 struct amdgpu_device *adev;
2173 struct drm_crtc *crtc;
2174 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002175 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002176
2177 if (dev == NULL || dev->dev_private == NULL) {
2178 return -ENODEV;
2179 }
2180
2181 adev = dev->dev_private;
2182
2183 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2184 return 0;
2185
2186 drm_kms_helper_poll_disable(dev);
2187
Harry Wentland45622362017-09-12 15:58:20 -04002188 if (!amdgpu_device_has_dc_support(adev)) {
2189 /* turn off display hw */
2190 drm_modeset_lock_all(dev);
2191 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2192 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2193 }
2194 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002195 }
2196
Yong Zhaoba997702015-11-09 17:21:45 -05002197 amdgpu_amdkfd_suspend(adev);
2198
Alex Deucher756e6882015-10-08 00:03:36 -04002199 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002200 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002201 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2203 struct amdgpu_bo *robj;
2204
Alex Deucher756e6882015-10-08 00:03:36 -04002205 if (amdgpu_crtc->cursor_bo) {
2206 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002207 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002208 if (r == 0) {
2209 amdgpu_bo_unpin(aobj);
2210 amdgpu_bo_unreserve(aobj);
2211 }
2212 }
2213
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 if (rfb == NULL || rfb->obj == NULL) {
2215 continue;
2216 }
2217 robj = gem_to_amdgpu_bo(rfb->obj);
2218 /* don't unpin kernel fb objects */
2219 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002220 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002221 if (r == 0) {
2222 amdgpu_bo_unpin(robj);
2223 amdgpu_bo_unreserve(robj);
2224 }
2225 }
2226 }
2227 /* evict vram memory */
2228 amdgpu_bo_evict_vram(adev);
2229
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002230 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002231
Alex Deuchercdd61df2017-12-14 16:47:40 -05002232 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002233
Alex Deuchera0a71e42016-10-10 12:41:36 -04002234 /* evict remaining vram memory
2235 * This second call to evict vram is to evict the gart page table
2236 * using the CPU.
2237 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002238 amdgpu_bo_evict_vram(adev);
2239
2240 pci_save_state(dev->pdev);
2241 if (suspend) {
2242 /* Shut down the device */
2243 pci_disable_device(dev->pdev);
2244 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002245 } else {
2246 r = amdgpu_asic_reset(adev);
2247 if (r)
2248 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002249 }
2250
2251 if (fbcon) {
2252 console_lock();
2253 amdgpu_fbdev_set_suspend(adev, 1);
2254 console_unlock();
2255 }
2256 return 0;
2257}
2258
2259/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002260 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002261 *
2262 * @pdev: drm dev pointer
2263 *
2264 * Bring the hw back to operating state (all asics).
2265 * Returns 0 for success or an error on failure.
2266 * Called at driver resume.
2267 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002268int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002269{
2270 struct drm_connector *connector;
2271 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002272 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002273 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002274
2275 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2276 return 0;
2277
jimqu74b0b152016-09-07 17:09:12 +08002278 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002279 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002280
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002281 if (resume) {
2282 pci_set_power_state(dev->pdev, PCI_D0);
2283 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002284 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002285 if (r)
2286 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002287 }
2288
2289 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002290 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002291 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2292 if (r)
2293 DRM_ERROR("amdgpu asic init failed\n");
2294 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002295
Alex Deucher06ec9072017-12-14 15:02:39 -05002296 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002297 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002298 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002299 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002300 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002301 amdgpu_fence_driver_resume(adev);
2302
Flora Cuica198522016-02-04 15:10:08 +08002303 if (resume) {
2304 r = amdgpu_ib_ring_tests(adev);
2305 if (r)
2306 DRM_ERROR("ib ring test failed (%d).\n", r);
2307 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002308
Alex Deucher06ec9072017-12-14 15:02:39 -05002309 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002310 if (r)
2311 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312
Alex Deucher756e6882015-10-08 00:03:36 -04002313 /* pin cursors */
2314 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2315 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2316
2317 if (amdgpu_crtc->cursor_bo) {
2318 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002319 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002320 if (r == 0) {
2321 r = amdgpu_bo_pin(aobj,
2322 AMDGPU_GEM_DOMAIN_VRAM,
2323 &amdgpu_crtc->cursor_addr);
2324 if (r != 0)
2325 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2326 amdgpu_bo_unreserve(aobj);
2327 }
2328 }
2329 }
Yong Zhaoba997702015-11-09 17:21:45 -05002330 r = amdgpu_amdkfd_resume(adev);
2331 if (r)
2332 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002333
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002334 /* blat the mode back in */
2335 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002336 if (!amdgpu_device_has_dc_support(adev)) {
2337 /* pre DCE11 */
2338 drm_helper_resume_force_mode(dev);
2339
2340 /* turn on display hw */
2341 drm_modeset_lock_all(dev);
2342 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2343 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2344 }
2345 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002346 }
2347 }
2348
2349 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002350
2351 /*
2352 * Most of the connector probing functions try to acquire runtime pm
2353 * refs to ensure that the GPU is powered on when connector polling is
2354 * performed. Since we're calling this from a runtime PM callback,
2355 * trying to acquire rpm refs will cause us to deadlock.
2356 *
2357 * Since we're guaranteed to be holding the rpm lock, it's safe to
2358 * temporarily disable the rpm helpers so this doesn't deadlock us.
2359 */
2360#ifdef CONFIG_PM
2361 dev->dev->power.disable_depth++;
2362#endif
Harry Wentland45622362017-09-12 15:58:20 -04002363 if (!amdgpu_device_has_dc_support(adev))
2364 drm_helper_hpd_irq_event(dev);
2365 else
2366 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002367#ifdef CONFIG_PM
2368 dev->dev->power.disable_depth--;
2369#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002370
Huang Rui03161a62017-04-13 16:12:26 +08002371 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002372 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002373
Huang Rui03161a62017-04-13 16:12:26 +08002374unlock:
2375 if (fbcon)
2376 console_unlock();
2377
2378 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002379}
2380
Alex Deucher06ec9072017-12-14 15:02:39 -05002381static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002382{
2383 int i;
2384 bool asic_hang = false;
2385
Monk Liuf993d622017-10-16 19:46:01 +08002386 if (amdgpu_sriov_vf(adev))
2387 return true;
2388
Chunming Zhou63fbf422016-07-15 11:19:20 +08002389 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002390 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002391 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002392 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2393 adev->ip_blocks[i].status.hang =
2394 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2395 if (adev->ip_blocks[i].status.hang) {
2396 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002397 asic_hang = true;
2398 }
2399 }
2400 return asic_hang;
2401}
2402
Alex Deucher06ec9072017-12-14 15:02:39 -05002403static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002404{
2405 int i, r = 0;
2406
2407 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002408 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002409 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002410 if (adev->ip_blocks[i].status.hang &&
2411 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2412 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002413 if (r)
2414 return r;
2415 }
2416 }
2417
2418 return 0;
2419}
2420
Alex Deucher06ec9072017-12-14 15:02:39 -05002421static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002422{
Alex Deucherda146d32016-10-13 16:07:03 -04002423 int i;
2424
2425 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002426 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002427 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002428 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2429 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2430 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002431 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2432 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002433 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002434 DRM_INFO("Some block need full reset!\n");
2435 return true;
2436 }
2437 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002438 }
2439 return false;
2440}
2441
Alex Deucher06ec9072017-12-14 15:02:39 -05002442static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002443{
2444 int i, r = 0;
2445
2446 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002447 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002448 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002449 if (adev->ip_blocks[i].status.hang &&
2450 adev->ip_blocks[i].version->funcs->soft_reset) {
2451 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002452 if (r)
2453 return r;
2454 }
2455 }
2456
2457 return 0;
2458}
2459
Alex Deucher06ec9072017-12-14 15:02:39 -05002460static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002461{
2462 int i, r = 0;
2463
2464 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002465 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002466 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002467 if (adev->ip_blocks[i].status.hang &&
2468 adev->ip_blocks[i].version->funcs->post_soft_reset)
2469 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002470 if (r)
2471 return r;
2472 }
2473
2474 return 0;
2475}
2476
Alex Deucher06ec9072017-12-14 15:02:39 -05002477static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2478 struct amdgpu_ring *ring,
2479 struct amdgpu_bo *bo,
2480 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002481{
2482 uint32_t domain;
2483 int r;
2484
Roger.He23d2e502017-04-21 14:24:26 +08002485 if (!bo->shadow)
2486 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002487
Alex Xie1d284792017-04-24 13:53:04 -04002488 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002489 if (r)
2490 return r;
2491 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2492 /* if bo has been evicted, then no need to recover */
2493 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002494 r = amdgpu_bo_validate(bo->shadow);
2495 if (r) {
2496 DRM_ERROR("bo validate failed!\n");
2497 goto err;
2498 }
2499
Roger.He23d2e502017-04-21 14:24:26 +08002500 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002501 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002502 if (r) {
2503 DRM_ERROR("recover page table failed!\n");
2504 goto err;
2505 }
2506 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002507err:
Roger.He23d2e502017-04-21 14:24:26 +08002508 amdgpu_bo_unreserve(bo);
2509 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002510}
2511
Monk Liuc41d1cf2017-12-25 11:59:27 +08002512static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2513{
2514 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2515 struct amdgpu_bo *bo, *tmp;
2516 struct dma_fence *fence = NULL, *next = NULL;
2517 long r = 1;
2518 int i = 0;
2519 long tmo;
2520
2521 if (amdgpu_sriov_runtime(adev))
2522 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2523 else
2524 tmo = msecs_to_jiffies(100);
2525
2526 DRM_INFO("recover vram bo from shadow start\n");
2527 mutex_lock(&adev->shadow_list_lock);
2528 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2529 next = NULL;
2530 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
2531 if (fence) {
2532 r = dma_fence_wait_timeout(fence, false, tmo);
2533 if (r == 0)
2534 pr_err("wait fence %p[%d] timeout\n", fence, i);
2535 else if (r < 0)
2536 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2537 if (r < 1) {
2538 dma_fence_put(fence);
2539 fence = next;
2540 break;
2541 }
2542 i++;
2543 }
2544
2545 dma_fence_put(fence);
2546 fence = next;
2547 }
2548 mutex_unlock(&adev->shadow_list_lock);
2549
2550 if (fence) {
2551 r = dma_fence_wait_timeout(fence, false, tmo);
2552 if (r == 0)
2553 pr_err("wait fence %p[%d] timeout\n", fence, i);
2554 else if (r < 0)
2555 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2556
2557 }
2558 dma_fence_put(fence);
2559
2560 if (r > 0)
2561 DRM_INFO("recover vram bo from shadow done\n");
2562 else
2563 DRM_ERROR("recover vram bo from shadow failed\n");
2564
2565 return (r > 0?0:1);
2566}
2567
Monk Liu57406822017-10-25 16:37:02 +08002568/*
Alex Deucher06ec9072017-12-14 15:02:39 -05002569 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002570 *
2571 * @adev: amdgpu device pointer
Monk Liua90ad3c2017-01-23 14:22:08 +08002572 *
Monk Liu57406822017-10-25 16:37:02 +08002573 * attempt to do soft-reset or full-reset and reinitialize Asic
2574 * return 0 means successed otherwise failed
2575*/
Monk Liuc41d1cf2017-12-25 11:59:27 +08002576static int amdgpu_device_reset(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08002577{
Monk Liu57406822017-10-25 16:37:02 +08002578 bool need_full_reset, vram_lost = 0;
2579 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002580
Alex Deucher06ec9072017-12-14 15:02:39 -05002581 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002582
2583 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002584 amdgpu_device_ip_pre_soft_reset(adev);
2585 r = amdgpu_device_ip_soft_reset(adev);
2586 amdgpu_device_ip_post_soft_reset(adev);
2587 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002588 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2589 need_full_reset = true;
2590 }
2591 }
2592
2593 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05002594 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002595
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002596retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002597 r = amdgpu_asic_reset(adev);
2598 /* post card */
2599 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002600
Chunming Zhou35d782f2016-07-15 15:57:13 +08002601 if (!r) {
2602 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05002603 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002604 if (r)
2605 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002606
Alex Deucher06ec9072017-12-14 15:02:39 -05002607 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002608 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002609 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002610 atomic_inc(&adev->vram_lost_counter);
2611 }
Monk Liu57406822017-10-25 16:37:02 +08002612
Christian Königc1c7ce82017-10-16 16:50:32 +02002613 r = amdgpu_gtt_mgr_recover(
2614 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002615 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002616 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002617
Alex Deucher06ec9072017-12-14 15:02:39 -05002618 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002619 if (r)
2620 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002621
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002622 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05002623 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002624 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002625 }
Monk Liu57406822017-10-25 16:37:02 +08002626
Chunming Zhoufcf06492017-05-05 10:33:33 +08002627out:
2628 if (!r) {
2629 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002630 r = amdgpu_ib_ring_tests(adev);
2631 if (r) {
2632 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05002633 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002634 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002635 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002636 }
Monk Liu57406822017-10-25 16:37:02 +08002637 }
2638
Monk Liuc41d1cf2017-12-25 11:59:27 +08002639 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
2640 r = amdgpu_device_handle_vram_lost(adev);
Monk Liu57406822017-10-25 16:37:02 +08002641
2642 return r;
2643}
2644
2645/*
Alex Deucher06ec9072017-12-14 15:02:39 -05002646 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08002647 *
2648 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08002649 *
2650 * do VF FLR and reinitialize Asic
2651 * return 0 means successed otherwise failed
2652*/
Monk Liuc41d1cf2017-12-25 11:59:27 +08002653static int amdgpu_device_reset_sriov(struct amdgpu_device *adev, bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08002654{
2655 int r;
2656
2657 if (from_hypervisor)
2658 r = amdgpu_virt_request_full_gpu(adev, true);
2659 else
2660 r = amdgpu_virt_reset_gpu(adev);
2661 if (r)
2662 return r;
2663
2664 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05002665 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08002666 if (r)
2667 goto error;
2668
2669 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02002670 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08002671
2672 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05002673 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08002674 amdgpu_virt_release_full_gpu(adev, true);
Monk Liu57406822017-10-25 16:37:02 +08002675 if (r)
2676 goto error;
2677
2678 amdgpu_irq_gpu_reset_resume_helper(adev);
2679 r = amdgpu_ib_ring_tests(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08002680
2681 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
2682 atomic_inc(&adev->vram_lost_counter);
2683 r = amdgpu_device_handle_vram_lost(adev);
2684 }
Monk Liu57406822017-10-25 16:37:02 +08002685
2686error:
Monk Liu57406822017-10-25 16:37:02 +08002687
2688 return r;
2689}
2690
2691/**
Alex Deucher5f152b52017-12-15 16:40:49 -05002692 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08002693 *
2694 * @adev: amdgpu device pointer
2695 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05002696 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08002697 *
2698 * Attempt to reset the GPU if it has hung (all asics).
2699 * Returns 0 for success or an error on failure.
2700 */
Alex Deucher5f152b52017-12-15 16:40:49 -05002701int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
2702 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08002703{
2704 struct drm_atomic_state *state = NULL;
Monk Liu57406822017-10-25 16:37:02 +08002705 int i, r, resched;
2706
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05002707 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08002708 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2709 return 0;
2710 }
2711
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05002712 if (!force && (amdgpu_gpu_recovery == 0 ||
2713 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
2714 DRM_INFO("GPU recovery disabled.\n");
2715 return 0;
2716 }
2717
Monk Liu57406822017-10-25 16:37:02 +08002718 dev_info(adev->dev, "GPU reset begin!\n");
2719
Monk Liu13a752e2017-10-17 15:11:12 +08002720 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08002721 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08002722 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08002723
2724 /* block TTM */
2725 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Monk Liu71182662017-12-25 15:14:58 +08002726
Monk Liu57406822017-10-25 16:37:02 +08002727 /* store modesetting */
2728 if (amdgpu_device_has_dc_support(adev))
2729 state = drm_atomic_helper_suspend(adev->ddev);
2730
Monk Liu71182662017-12-25 15:14:58 +08002731 /* block all schedulers and reset given job's ring */
Monk Liu57406822017-10-25 16:37:02 +08002732 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2733 struct amdgpu_ring *ring = adev->rings[i];
2734
2735 if (!ring || !ring->sched.thread)
2736 continue;
2737
Monk Liu71182662017-12-25 15:14:58 +08002738 kthread_park(ring->sched.thread);
2739
Monk Liu57406822017-10-25 16:37:02 +08002740 if (job && job->ring->idx != i)
2741 continue;
2742
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002743 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08002744
2745 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2746 amdgpu_fence_driver_force_completion(ring);
2747 }
2748
2749 if (amdgpu_sriov_vf(adev))
Monk Liuc41d1cf2017-12-25 11:59:27 +08002750 r = amdgpu_device_reset_sriov(adev, job ? false : true);
Monk Liu57406822017-10-25 16:37:02 +08002751 else
Monk Liuc41d1cf2017-12-25 11:59:27 +08002752 r = amdgpu_device_reset(adev);
Monk Liu57406822017-10-25 16:37:02 +08002753
Monk Liu71182662017-12-25 15:14:58 +08002754 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2755 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002756
Monk Liu71182662017-12-25 15:14:58 +08002757 if (!ring || !ring->sched.thread)
2758 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002759
Monk Liu71182662017-12-25 15:14:58 +08002760 /* only need recovery sched of the given job's ring
2761 * or all rings (in the case @job is NULL)
2762 * after above amdgpu_reset accomplished
2763 */
2764 if ((!job || job->ring->idx == i) && !r)
Lucas Stach1b1f42d2017-12-06 17:49:39 +01002765 drm_sched_job_recovery(&ring->sched);
Monk Liu57406822017-10-25 16:37:02 +08002766
Monk Liu71182662017-12-25 15:14:58 +08002767 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002768 }
2769
Harry Wentland45622362017-09-12 15:58:20 -04002770 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08002771 if (drm_atomic_helper_resume(adev->ddev, state))
2772 dev_info(adev->dev, "drm resume failed:%d\n", r);
Monk Liu57406822017-10-25 16:37:02 +08002773 } else {
Harry Wentland45622362017-09-12 15:58:20 -04002774 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08002775 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002776
2777 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08002778
Gavin Wan89041942017-06-23 13:55:15 -04002779 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002780 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08002781 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
2782 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2783 } else {
2784 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04002785 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002786
Gavin Wan89041942017-06-23 13:55:15 -04002787 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08002788 adev->in_gpu_reset = 0;
2789 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002790 return r;
2791}
2792
Alex Deucher5494d862018-03-09 15:14:11 -05002793static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002794{
2795 u32 mask;
2796 int ret;
2797
Alex Deuchercd474ba2016-02-04 10:21:23 -05002798 if (amdgpu_pcie_gen_cap)
2799 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2800
2801 if (amdgpu_pcie_lane_cap)
2802 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2803
2804 /* covers APUs as well */
2805 if (pci_is_root_bus(adev->pdev->bus)) {
2806 if (adev->pm.pcie_gen_mask == 0)
2807 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2808 if (adev->pm.pcie_mlw_mask == 0)
2809 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002810 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002811 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002812
2813 if (adev->pm.pcie_gen_mask == 0) {
2814 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2815 if (!ret) {
2816 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2817 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2818 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2819
2820 if (mask & DRM_PCIE_SPEED_25)
2821 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2822 if (mask & DRM_PCIE_SPEED_50)
2823 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2824 if (mask & DRM_PCIE_SPEED_80)
2825 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2826 } else {
2827 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2828 }
2829 }
2830 if (adev->pm.pcie_mlw_mask == 0) {
2831 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2832 if (!ret) {
2833 switch (mask) {
2834 case 32:
2835 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2836 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2837 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2838 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2839 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2840 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2841 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2842 break;
2843 case 16:
2844 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2845 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2846 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2847 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2848 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2849 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2850 break;
2851 case 12:
2852 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2853 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2854 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2855 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2856 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2857 break;
2858 case 8:
2859 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2860 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2861 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2862 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2863 break;
2864 case 4:
2865 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2866 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2867 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2868 break;
2869 case 2:
2870 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2871 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2872 break;
2873 case 1:
2874 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2875 break;
2876 default:
2877 break;
2878 }
2879 } else {
2880 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002881 }
2882 }
2883}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002884