Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 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 Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 28 | #include <linux/kthread.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 29 | #include <linux/console.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/debugfs.h> |
| 32 | #include <drm/drmP.h> |
| 33 | #include <drm/drm_crtc_helper.h> |
| 34 | #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 Denis | f4b373f | 2016-05-31 08:02:27 -0400 | [diff] [blame] | 39 | #include "amdgpu_trace.h" |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 40 | #include "amdgpu_i2c.h" |
| 41 | #include "atom.h" |
| 42 | #include "amdgpu_atombios.h" |
Alex Deucher | d0dd7f0 | 2015-11-11 19:45:06 -0500 | [diff] [blame] | 43 | #include "amd_pcie.h" |
Ken Wang | 33f3480 | 2016-01-21 17:29:41 +0800 | [diff] [blame] | 44 | #ifdef CONFIG_DRM_AMDGPU_SI |
| 45 | #include "si.h" |
| 46 | #endif |
Alex Deucher | a2e73f5 | 2015-04-20 17:09:27 -0400 | [diff] [blame] | 47 | #ifdef CONFIG_DRM_AMDGPU_CIK |
| 48 | #include "cik.h" |
| 49 | #endif |
Alex Deucher | aaa36a9 | 2015-04-20 17:31:14 -0400 | [diff] [blame] | 50 | #include "vi.h" |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 51 | #include "bif/bif_4_1_d.h" |
Emily Deng | 9accf2f | 2016-08-10 16:01:25 +0800 | [diff] [blame] | 52 | #include <linux/pci.h> |
Monk Liu | bec8637 | 2016-09-14 19:38:08 +0800 | [diff] [blame] | 53 | #include <linux/firmware.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 54 | |
| 55 | static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev); |
| 56 | static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev); |
| 57 | |
| 58 | static const char *amdgpu_asic_name[] = { |
Ken Wang | da69c161 | 2016-01-21 19:08:55 +0800 | [diff] [blame] | 59 | "TAHITI", |
| 60 | "PITCAIRN", |
| 61 | "VERDE", |
| 62 | "OLAND", |
| 63 | "HAINAN", |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 64 | "BONAIRE", |
| 65 | "KAVERI", |
| 66 | "KABINI", |
| 67 | "HAWAII", |
| 68 | "MULLINS", |
| 69 | "TOPAZ", |
| 70 | "TONGA", |
David Zhang | 48299f9 | 2015-07-08 01:05:16 +0800 | [diff] [blame] | 71 | "FIJI", |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 72 | "CARRIZO", |
Samuel Li | 139f491 | 2015-10-08 14:50:27 -0400 | [diff] [blame] | 73 | "STONEY", |
Flora Cui | 2cc0c0b | 2016-03-14 18:33:29 -0400 | [diff] [blame] | 74 | "POLARIS10", |
| 75 | "POLARIS11", |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 76 | "LAST", |
| 77 | }; |
| 78 | |
| 79 | bool amdgpu_device_is_px(struct drm_device *dev) |
| 80 | { |
| 81 | struct amdgpu_device *adev = dev->dev_private; |
| 82 | |
Jammy Zhou | 2f7d10b | 2015-07-22 11:29:01 +0800 | [diff] [blame] | 83 | if (adev->flags & AMD_IS_PX) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 84 | return true; |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * MMIO register access helper functions. |
| 90 | */ |
| 91 | uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg, |
| 92 | bool always_indirect) |
| 93 | { |
Tom St Denis | f4b373f | 2016-05-31 08:02:27 -0400 | [diff] [blame] | 94 | uint32_t ret; |
| 95 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 96 | if ((reg * 4) < adev->rmmio_size && !always_indirect) |
Tom St Denis | f4b373f | 2016-05-31 08:02:27 -0400 | [diff] [blame] | 97 | ret = readl(((void __iomem *)adev->rmmio) + (reg * 4)); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 98 | else { |
| 99 | unsigned long flags; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 100 | |
| 101 | spin_lock_irqsave(&adev->mmio_idx_lock, flags); |
| 102 | writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4)); |
| 103 | ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4)); |
| 104 | spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 105 | } |
Tom St Denis | f4b373f | 2016-05-31 08:02:27 -0400 | [diff] [blame] | 106 | trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret); |
| 107 | return ret; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v, |
| 111 | bool always_indirect) |
| 112 | { |
Tom St Denis | f4b373f | 2016-05-31 08:02:27 -0400 | [diff] [blame] | 113 | trace_amdgpu_mm_wreg(adev->pdev->device, reg, v); |
Monk Liu | 4e99a44 | 2016-03-31 13:26:59 +0800 | [diff] [blame] | 114 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 115 | if ((reg * 4) < adev->rmmio_size && !always_indirect) |
| 116 | writel(v, ((void __iomem *)adev->rmmio) + (reg * 4)); |
| 117 | else { |
| 118 | unsigned long flags; |
| 119 | |
| 120 | spin_lock_irqsave(&adev->mmio_idx_lock, flags); |
| 121 | writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4)); |
| 122 | writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4)); |
| 123 | spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg) |
| 128 | { |
| 129 | if ((reg * 4) < adev->rio_mem_size) |
| 130 | return ioread32(adev->rio_mem + (reg * 4)); |
| 131 | else { |
| 132 | iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4)); |
| 133 | return ioread32(adev->rio_mem + (mmMM_DATA * 4)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v) |
| 138 | { |
| 139 | |
| 140 | if ((reg * 4) < adev->rio_mem_size) |
| 141 | iowrite32(v, adev->rio_mem + (reg * 4)); |
| 142 | else { |
| 143 | iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4)); |
| 144 | iowrite32(v, adev->rio_mem + (mmMM_DATA * 4)); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * amdgpu_mm_rdoorbell - read a doorbell dword |
| 150 | * |
| 151 | * @adev: amdgpu_device pointer |
| 152 | * @index: doorbell index |
| 153 | * |
| 154 | * Returns the value in the doorbell aperture at the |
| 155 | * requested doorbell index (CIK). |
| 156 | */ |
| 157 | u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index) |
| 158 | { |
| 159 | if (index < adev->doorbell.num_doorbells) { |
| 160 | return readl(adev->doorbell.ptr + index); |
| 161 | } else { |
| 162 | DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index); |
| 163 | return 0; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * amdgpu_mm_wdoorbell - write a doorbell dword |
| 169 | * |
| 170 | * @adev: amdgpu_device pointer |
| 171 | * @index: doorbell index |
| 172 | * @v: value to write |
| 173 | * |
| 174 | * Writes @v to the doorbell aperture at the |
| 175 | * requested doorbell index (CIK). |
| 176 | */ |
| 177 | void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v) |
| 178 | { |
| 179 | if (index < adev->doorbell.num_doorbells) { |
| 180 | writel(v, adev->doorbell.ptr + index); |
| 181 | } else { |
| 182 | DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * amdgpu_invalid_rreg - dummy reg read function |
| 188 | * |
| 189 | * @adev: amdgpu device pointer |
| 190 | * @reg: offset of register |
| 191 | * |
| 192 | * Dummy register read function. Used for register blocks |
| 193 | * that certain asics don't have (all asics). |
| 194 | * Returns the value in the register. |
| 195 | */ |
| 196 | static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg) |
| 197 | { |
| 198 | DRM_ERROR("Invalid callback to read register 0x%04X\n", reg); |
| 199 | BUG(); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * amdgpu_invalid_wreg - dummy reg write function |
| 205 | * |
| 206 | * @adev: amdgpu device pointer |
| 207 | * @reg: offset of register |
| 208 | * @v: value to write to the register |
| 209 | * |
| 210 | * Dummy register read function. Used for register blocks |
| 211 | * that certain asics don't have (all asics). |
| 212 | */ |
| 213 | static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v) |
| 214 | { |
| 215 | DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n", |
| 216 | reg, v); |
| 217 | BUG(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * amdgpu_block_invalid_rreg - dummy reg read function |
| 222 | * |
| 223 | * @adev: amdgpu device pointer |
| 224 | * @block: offset of instance |
| 225 | * @reg: offset of register |
| 226 | * |
| 227 | * Dummy register read function. Used for register blocks |
| 228 | * that certain asics don't have (all asics). |
| 229 | * Returns the value in the register. |
| 230 | */ |
| 231 | static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev, |
| 232 | uint32_t block, uint32_t reg) |
| 233 | { |
| 234 | DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n", |
| 235 | reg, block); |
| 236 | BUG(); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * amdgpu_block_invalid_wreg - dummy reg write function |
| 242 | * |
| 243 | * @adev: amdgpu device pointer |
| 244 | * @block: offset of instance |
| 245 | * @reg: offset of register |
| 246 | * @v: value to write to the register |
| 247 | * |
| 248 | * Dummy register read function. Used for register blocks |
| 249 | * that certain asics don't have (all asics). |
| 250 | */ |
| 251 | static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev, |
| 252 | uint32_t block, |
| 253 | uint32_t reg, uint32_t v) |
| 254 | { |
| 255 | DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n", |
| 256 | reg, block, v); |
| 257 | BUG(); |
| 258 | } |
| 259 | |
| 260 | static int amdgpu_vram_scratch_init(struct amdgpu_device *adev) |
| 261 | { |
| 262 | int r; |
| 263 | |
| 264 | if (adev->vram_scratch.robj == NULL) { |
| 265 | r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 266 | PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM, |
| 267 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 268 | NULL, NULL, &adev->vram_scratch.robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 269 | if (r) { |
| 270 | return r; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | r = amdgpu_bo_reserve(adev->vram_scratch.robj, false); |
| 275 | if (unlikely(r != 0)) |
| 276 | return r; |
| 277 | r = amdgpu_bo_pin(adev->vram_scratch.robj, |
| 278 | AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr); |
| 279 | if (r) { |
| 280 | amdgpu_bo_unreserve(adev->vram_scratch.robj); |
| 281 | return r; |
| 282 | } |
| 283 | r = amdgpu_bo_kmap(adev->vram_scratch.robj, |
| 284 | (void **)&adev->vram_scratch.ptr); |
| 285 | if (r) |
| 286 | amdgpu_bo_unpin(adev->vram_scratch.robj); |
| 287 | amdgpu_bo_unreserve(adev->vram_scratch.robj); |
| 288 | |
| 289 | return r; |
| 290 | } |
| 291 | |
| 292 | static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev) |
| 293 | { |
| 294 | int r; |
| 295 | |
| 296 | if (adev->vram_scratch.robj == NULL) { |
| 297 | return; |
| 298 | } |
| 299 | r = amdgpu_bo_reserve(adev->vram_scratch.robj, false); |
| 300 | if (likely(r == 0)) { |
| 301 | amdgpu_bo_kunmap(adev->vram_scratch.robj); |
| 302 | amdgpu_bo_unpin(adev->vram_scratch.robj); |
| 303 | amdgpu_bo_unreserve(adev->vram_scratch.robj); |
| 304 | } |
| 305 | amdgpu_bo_unref(&adev->vram_scratch.robj); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * amdgpu_program_register_sequence - program an array of registers. |
| 310 | * |
| 311 | * @adev: amdgpu_device pointer |
| 312 | * @registers: pointer to the register array |
| 313 | * @array_size: size of the register array |
| 314 | * |
| 315 | * Programs an array or registers with and and or masks. |
| 316 | * This is a helper for setting golden registers. |
| 317 | */ |
| 318 | void amdgpu_program_register_sequence(struct amdgpu_device *adev, |
| 319 | const u32 *registers, |
| 320 | const u32 array_size) |
| 321 | { |
| 322 | u32 tmp, reg, and_mask, or_mask; |
| 323 | int i; |
| 324 | |
| 325 | if (array_size % 3) |
| 326 | return; |
| 327 | |
| 328 | for (i = 0; i < array_size; i +=3) { |
| 329 | reg = registers[i + 0]; |
| 330 | and_mask = registers[i + 1]; |
| 331 | or_mask = registers[i + 2]; |
| 332 | |
| 333 | if (and_mask == 0xffffffff) { |
| 334 | tmp = or_mask; |
| 335 | } else { |
| 336 | tmp = RREG32(reg); |
| 337 | tmp &= ~and_mask; |
| 338 | tmp |= or_mask; |
| 339 | } |
| 340 | WREG32(reg, tmp); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void amdgpu_pci_config_reset(struct amdgpu_device *adev) |
| 345 | { |
| 346 | pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * GPU doorbell aperture helpers function. |
| 351 | */ |
| 352 | /** |
| 353 | * amdgpu_doorbell_init - Init doorbell driver information. |
| 354 | * |
| 355 | * @adev: amdgpu_device pointer |
| 356 | * |
| 357 | * Init doorbell driver information (CIK) |
| 358 | * Returns 0 on success, error on failure. |
| 359 | */ |
| 360 | static int amdgpu_doorbell_init(struct amdgpu_device *adev) |
| 361 | { |
| 362 | /* doorbell bar mapping */ |
| 363 | adev->doorbell.base = pci_resource_start(adev->pdev, 2); |
| 364 | adev->doorbell.size = pci_resource_len(adev->pdev, 2); |
| 365 | |
Christian König | edf600d | 2016-05-03 15:54:54 +0200 | [diff] [blame] | 366 | adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32), |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 367 | AMDGPU_DOORBELL_MAX_ASSIGNMENT+1); |
| 368 | if (adev->doorbell.num_doorbells == 0) |
| 369 | return -EINVAL; |
| 370 | |
| 371 | adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32)); |
| 372 | if (adev->doorbell.ptr == NULL) { |
| 373 | return -ENOMEM; |
| 374 | } |
| 375 | DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base); |
| 376 | DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size); |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * amdgpu_doorbell_fini - Tear down doorbell driver information. |
| 383 | * |
| 384 | * @adev: amdgpu_device pointer |
| 385 | * |
| 386 | * Tear down doorbell driver information (CIK) |
| 387 | */ |
| 388 | static void amdgpu_doorbell_fini(struct amdgpu_device *adev) |
| 389 | { |
| 390 | iounmap(adev->doorbell.ptr); |
| 391 | adev->doorbell.ptr = NULL; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to |
| 396 | * setup amdkfd |
| 397 | * |
| 398 | * @adev: amdgpu_device pointer |
| 399 | * @aperture_base: output returning doorbell aperture base physical address |
| 400 | * @aperture_size: output returning doorbell aperture size in bytes |
| 401 | * @start_offset: output returning # of doorbell bytes reserved for amdgpu. |
| 402 | * |
| 403 | * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up, |
| 404 | * takes doorbells required for its own rings and reports the setup to amdkfd. |
| 405 | * amdgpu reserved doorbells are at the start of the doorbell aperture. |
| 406 | */ |
| 407 | void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev, |
| 408 | phys_addr_t *aperture_base, |
| 409 | size_t *aperture_size, |
| 410 | size_t *start_offset) |
| 411 | { |
| 412 | /* |
| 413 | * The first num_doorbells are used by amdgpu. |
| 414 | * amdkfd takes whatever's left in the aperture. |
| 415 | */ |
| 416 | if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) { |
| 417 | *aperture_base = adev->doorbell.base; |
| 418 | *aperture_size = adev->doorbell.size; |
| 419 | *start_offset = adev->doorbell.num_doorbells * sizeof(u32); |
| 420 | } else { |
| 421 | *aperture_base = 0; |
| 422 | *aperture_size = 0; |
| 423 | *start_offset = 0; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * amdgpu_wb_*() |
| 429 | * Writeback is the the method by which the the GPU updates special pages |
| 430 | * in memory with the status of certain GPU events (fences, ring pointers, |
| 431 | * etc.). |
| 432 | */ |
| 433 | |
| 434 | /** |
| 435 | * amdgpu_wb_fini - Disable Writeback and free memory |
| 436 | * |
| 437 | * @adev: amdgpu_device pointer |
| 438 | * |
| 439 | * Disables Writeback and frees the Writeback memory (all asics). |
| 440 | * Used at driver shutdown. |
| 441 | */ |
| 442 | static void amdgpu_wb_fini(struct amdgpu_device *adev) |
| 443 | { |
| 444 | if (adev->wb.wb_obj) { |
| 445 | if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) { |
| 446 | amdgpu_bo_kunmap(adev->wb.wb_obj); |
| 447 | amdgpu_bo_unpin(adev->wb.wb_obj); |
| 448 | amdgpu_bo_unreserve(adev->wb.wb_obj); |
| 449 | } |
| 450 | amdgpu_bo_unref(&adev->wb.wb_obj); |
| 451 | adev->wb.wb = NULL; |
| 452 | adev->wb.wb_obj = NULL; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * amdgpu_wb_init- Init Writeback driver info and allocate memory |
| 458 | * |
| 459 | * @adev: amdgpu_device pointer |
| 460 | * |
| 461 | * Disables Writeback and frees the Writeback memory (all asics). |
| 462 | * Used at driver startup. |
| 463 | * Returns 0 on success or an -error on failure. |
| 464 | */ |
| 465 | static int amdgpu_wb_init(struct amdgpu_device *adev) |
| 466 | { |
| 467 | int r; |
| 468 | |
| 469 | if (adev->wb.wb_obj == NULL) { |
| 470 | r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 471 | AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL, |
| 472 | &adev->wb.wb_obj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 473 | if (r) { |
| 474 | dev_warn(adev->dev, "(%d) create WB bo failed\n", r); |
| 475 | return r; |
| 476 | } |
| 477 | r = amdgpu_bo_reserve(adev->wb.wb_obj, false); |
| 478 | if (unlikely(r != 0)) { |
| 479 | amdgpu_wb_fini(adev); |
| 480 | return r; |
| 481 | } |
| 482 | r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT, |
| 483 | &adev->wb.gpu_addr); |
| 484 | if (r) { |
| 485 | amdgpu_bo_unreserve(adev->wb.wb_obj); |
| 486 | dev_warn(adev->dev, "(%d) pin WB bo failed\n", r); |
| 487 | amdgpu_wb_fini(adev); |
| 488 | return r; |
| 489 | } |
| 490 | r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb); |
| 491 | amdgpu_bo_unreserve(adev->wb.wb_obj); |
| 492 | if (r) { |
| 493 | dev_warn(adev->dev, "(%d) map WB bo failed\n", r); |
| 494 | amdgpu_wb_fini(adev); |
| 495 | return r; |
| 496 | } |
| 497 | |
| 498 | adev->wb.num_wb = AMDGPU_MAX_WB; |
| 499 | memset(&adev->wb.used, 0, sizeof(adev->wb.used)); |
| 500 | |
| 501 | /* clear wb memory */ |
| 502 | memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE); |
| 503 | } |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * amdgpu_wb_get - Allocate a wb entry |
| 510 | * |
| 511 | * @adev: amdgpu_device pointer |
| 512 | * @wb: wb index |
| 513 | * |
| 514 | * Allocate a wb slot for use by the driver (all asics). |
| 515 | * Returns 0 on success or -EINVAL on failure. |
| 516 | */ |
| 517 | int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb) |
| 518 | { |
| 519 | unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb); |
| 520 | if (offset < adev->wb.num_wb) { |
| 521 | __set_bit(offset, adev->wb.used); |
| 522 | *wb = offset; |
| 523 | return 0; |
| 524 | } else { |
| 525 | return -EINVAL; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * amdgpu_wb_free - Free a wb entry |
| 531 | * |
| 532 | * @adev: amdgpu_device pointer |
| 533 | * @wb: wb index |
| 534 | * |
| 535 | * Free a wb slot allocated for use by the driver (all asics) |
| 536 | */ |
| 537 | void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb) |
| 538 | { |
| 539 | if (wb < adev->wb.num_wb) |
| 540 | __clear_bit(wb, adev->wb.used); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * amdgpu_vram_location - try to find VRAM location |
| 545 | * @adev: amdgpu device structure holding all necessary informations |
| 546 | * @mc: memory controller structure holding memory informations |
| 547 | * @base: base address at which to put VRAM |
| 548 | * |
| 549 | * Function will place try to place VRAM at base address provided |
| 550 | * as parameter (which is so far either PCI aperture address or |
| 551 | * for IGP TOM base address). |
| 552 | * |
| 553 | * If there is not enough space to fit the unvisible VRAM in the 32bits |
| 554 | * address space then we limit the VRAM size to the aperture. |
| 555 | * |
| 556 | * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size, |
| 557 | * this shouldn't be a problem as we are using the PCI aperture as a reference. |
| 558 | * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but |
| 559 | * not IGP. |
| 560 | * |
| 561 | * Note: we use mc_vram_size as on some board we need to program the mc to |
| 562 | * cover the whole aperture even if VRAM size is inferior to aperture size |
| 563 | * Novell bug 204882 + along with lots of ubuntu ones |
| 564 | * |
| 565 | * Note: when limiting vram it's safe to overwritte real_vram_size because |
| 566 | * we are not in case where real_vram_size is inferior to mc_vram_size (ie |
| 567 | * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu |
| 568 | * ones) |
| 569 | * |
| 570 | * Note: IGP TOM addr should be the same as the aperture addr, we don't |
| 571 | * explicitly check for that thought. |
| 572 | * |
| 573 | * FIXME: when reducing VRAM size align new size on power of 2. |
| 574 | */ |
| 575 | void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base) |
| 576 | { |
| 577 | uint64_t limit = (uint64_t)amdgpu_vram_limit << 20; |
| 578 | |
| 579 | mc->vram_start = base; |
| 580 | if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) { |
| 581 | dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n"); |
| 582 | mc->real_vram_size = mc->aper_size; |
| 583 | mc->mc_vram_size = mc->aper_size; |
| 584 | } |
| 585 | mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; |
| 586 | if (limit && limit < mc->real_vram_size) |
| 587 | mc->real_vram_size = limit; |
| 588 | dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n", |
| 589 | mc->mc_vram_size >> 20, mc->vram_start, |
| 590 | mc->vram_end, mc->real_vram_size >> 20); |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * amdgpu_gtt_location - try to find GTT location |
| 595 | * @adev: amdgpu device structure holding all necessary informations |
| 596 | * @mc: memory controller structure holding memory informations |
| 597 | * |
| 598 | * Function will place try to place GTT before or after VRAM. |
| 599 | * |
| 600 | * If GTT size is bigger than space left then we ajust GTT size. |
| 601 | * Thus function will never fails. |
| 602 | * |
| 603 | * FIXME: when reducing GTT size align new size on power of 2. |
| 604 | */ |
| 605 | void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc) |
| 606 | { |
| 607 | u64 size_af, size_bf; |
| 608 | |
| 609 | size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align; |
| 610 | size_bf = mc->vram_start & ~mc->gtt_base_align; |
| 611 | if (size_bf > size_af) { |
| 612 | if (mc->gtt_size > size_bf) { |
| 613 | dev_warn(adev->dev, "limiting GTT\n"); |
| 614 | mc->gtt_size = size_bf; |
| 615 | } |
| 616 | mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size; |
| 617 | } else { |
| 618 | if (mc->gtt_size > size_af) { |
| 619 | dev_warn(adev->dev, "limiting GTT\n"); |
| 620 | mc->gtt_size = size_af; |
| 621 | } |
| 622 | mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align; |
| 623 | } |
| 624 | mc->gtt_end = mc->gtt_start + mc->gtt_size - 1; |
| 625 | dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n", |
| 626 | mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end); |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * GPU helpers function. |
| 631 | */ |
| 632 | /** |
| 633 | * amdgpu_card_posted - check if the hw has already been initialized |
| 634 | * |
| 635 | * @adev: amdgpu_device pointer |
| 636 | * |
| 637 | * Check if the asic has been initialized (all asics). |
| 638 | * Used at driver startup. |
| 639 | * Returns true if initialized or false if not. |
| 640 | */ |
| 641 | bool amdgpu_card_posted(struct amdgpu_device *adev) |
| 642 | { |
| 643 | uint32_t reg; |
| 644 | |
| 645 | /* then check MEM_SIZE, in case the crtcs are off */ |
| 646 | reg = RREG32(mmCONFIG_MEMSIZE); |
| 647 | |
| 648 | if (reg) |
| 649 | return true; |
| 650 | |
| 651 | return false; |
| 652 | |
| 653 | } |
| 654 | |
Monk Liu | bec8637 | 2016-09-14 19:38:08 +0800 | [diff] [blame] | 655 | static bool amdgpu_vpost_needed(struct amdgpu_device *adev) |
| 656 | { |
| 657 | if (amdgpu_sriov_vf(adev)) |
| 658 | return false; |
| 659 | |
| 660 | if (amdgpu_passthrough(adev)) { |
| 661 | /* for FIJI: In whole GPU pass-through virtualization case |
| 662 | * old smc fw won't clear some registers (e.g. MEM_SIZE, BIOS_SCRATCH) |
| 663 | * so amdgpu_card_posted return false and driver will incorrectly skip vPost. |
| 664 | * but if we force vPost do in pass-through case, the driver reload will hang. |
| 665 | * whether doing vPost depends on amdgpu_card_posted if smc version is above |
| 666 | * 00160e00 for FIJI. |
| 667 | */ |
| 668 | if (adev->asic_type == CHIP_FIJI) { |
| 669 | int err; |
| 670 | uint32_t fw_ver; |
| 671 | err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev); |
| 672 | /* force vPost if error occured */ |
| 673 | if (err) |
| 674 | return true; |
| 675 | |
| 676 | fw_ver = *((uint32_t *)adev->pm.fw->data + 69); |
| 677 | if (fw_ver >= 0x00160e00) |
| 678 | return !amdgpu_card_posted(adev); |
| 679 | } |
| 680 | } else { |
| 681 | /* in bare-metal case, amdgpu_card_posted return false |
| 682 | * after system reboot/boot, and return true if driver |
| 683 | * reloaded. |
| 684 | * we shouldn't do vPost after driver reload otherwise GPU |
| 685 | * could hang. |
| 686 | */ |
| 687 | if (amdgpu_card_posted(adev)) |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | /* we assume vPost is neede for all other cases */ |
| 692 | return true; |
| 693 | } |
| 694 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 695 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 696 | * amdgpu_dummy_page_init - init dummy page used by the driver |
| 697 | * |
| 698 | * @adev: amdgpu_device pointer |
| 699 | * |
| 700 | * Allocate the dummy page used by the driver (all asics). |
| 701 | * This dummy page is used by the driver as a filler for gart entries |
| 702 | * when pages are taken out of the GART |
| 703 | * Returns 0 on sucess, -ENOMEM on failure. |
| 704 | */ |
| 705 | int amdgpu_dummy_page_init(struct amdgpu_device *adev) |
| 706 | { |
| 707 | if (adev->dummy_page.page) |
| 708 | return 0; |
| 709 | adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO); |
| 710 | if (adev->dummy_page.page == NULL) |
| 711 | return -ENOMEM; |
| 712 | adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page, |
| 713 | 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 714 | if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) { |
| 715 | dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n"); |
| 716 | __free_page(adev->dummy_page.page); |
| 717 | adev->dummy_page.page = NULL; |
| 718 | return -ENOMEM; |
| 719 | } |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * amdgpu_dummy_page_fini - free dummy page used by the driver |
| 725 | * |
| 726 | * @adev: amdgpu_device pointer |
| 727 | * |
| 728 | * Frees the dummy page used by the driver (all asics). |
| 729 | */ |
| 730 | void amdgpu_dummy_page_fini(struct amdgpu_device *adev) |
| 731 | { |
| 732 | if (adev->dummy_page.page == NULL) |
| 733 | return; |
| 734 | pci_unmap_page(adev->pdev, adev->dummy_page.addr, |
| 735 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 736 | __free_page(adev->dummy_page.page); |
| 737 | adev->dummy_page.page = NULL; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | /* ATOM accessor methods */ |
| 742 | /* |
| 743 | * ATOM is an interpreted byte code stored in tables in the vbios. The |
| 744 | * driver registers callbacks to access registers and the interpreter |
| 745 | * in the driver parses the tables and executes then to program specific |
| 746 | * actions (set display modes, asic init, etc.). See amdgpu_atombios.c, |
| 747 | * atombios.h, and atom.c |
| 748 | */ |
| 749 | |
| 750 | /** |
| 751 | * cail_pll_read - read PLL register |
| 752 | * |
| 753 | * @info: atom card_info pointer |
| 754 | * @reg: PLL register offset |
| 755 | * |
| 756 | * Provides a PLL register accessor for the atom interpreter (r4xx+). |
| 757 | * Returns the value of the PLL register. |
| 758 | */ |
| 759 | static uint32_t cail_pll_read(struct card_info *info, uint32_t reg) |
| 760 | { |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * cail_pll_write - write PLL register |
| 766 | * |
| 767 | * @info: atom card_info pointer |
| 768 | * @reg: PLL register offset |
| 769 | * @val: value to write to the pll register |
| 770 | * |
| 771 | * Provides a PLL register accessor for the atom interpreter (r4xx+). |
| 772 | */ |
| 773 | static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val) |
| 774 | { |
| 775 | |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * cail_mc_read - read MC (Memory Controller) register |
| 780 | * |
| 781 | * @info: atom card_info pointer |
| 782 | * @reg: MC register offset |
| 783 | * |
| 784 | * Provides an MC register accessor for the atom interpreter (r4xx+). |
| 785 | * Returns the value of the MC register. |
| 786 | */ |
| 787 | static uint32_t cail_mc_read(struct card_info *info, uint32_t reg) |
| 788 | { |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * cail_mc_write - write MC (Memory Controller) register |
| 794 | * |
| 795 | * @info: atom card_info pointer |
| 796 | * @reg: MC register offset |
| 797 | * @val: value to write to the pll register |
| 798 | * |
| 799 | * Provides a MC register accessor for the atom interpreter (r4xx+). |
| 800 | */ |
| 801 | static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val) |
| 802 | { |
| 803 | |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * cail_reg_write - write MMIO register |
| 808 | * |
| 809 | * @info: atom card_info pointer |
| 810 | * @reg: MMIO register offset |
| 811 | * @val: value to write to the pll register |
| 812 | * |
| 813 | * Provides a MMIO register accessor for the atom interpreter (r4xx+). |
| 814 | */ |
| 815 | static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val) |
| 816 | { |
| 817 | struct amdgpu_device *adev = info->dev->dev_private; |
| 818 | |
| 819 | WREG32(reg, val); |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * cail_reg_read - read MMIO register |
| 824 | * |
| 825 | * @info: atom card_info pointer |
| 826 | * @reg: MMIO register offset |
| 827 | * |
| 828 | * Provides an MMIO register accessor for the atom interpreter (r4xx+). |
| 829 | * Returns the value of the MMIO register. |
| 830 | */ |
| 831 | static uint32_t cail_reg_read(struct card_info *info, uint32_t reg) |
| 832 | { |
| 833 | struct amdgpu_device *adev = info->dev->dev_private; |
| 834 | uint32_t r; |
| 835 | |
| 836 | r = RREG32(reg); |
| 837 | return r; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * cail_ioreg_write - write IO register |
| 842 | * |
| 843 | * @info: atom card_info pointer |
| 844 | * @reg: IO register offset |
| 845 | * @val: value to write to the pll register |
| 846 | * |
| 847 | * Provides a IO register accessor for the atom interpreter (r4xx+). |
| 848 | */ |
| 849 | static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val) |
| 850 | { |
| 851 | struct amdgpu_device *adev = info->dev->dev_private; |
| 852 | |
| 853 | WREG32_IO(reg, val); |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * cail_ioreg_read - read IO register |
| 858 | * |
| 859 | * @info: atom card_info pointer |
| 860 | * @reg: IO register offset |
| 861 | * |
| 862 | * Provides an IO register accessor for the atom interpreter (r4xx+). |
| 863 | * Returns the value of the IO register. |
| 864 | */ |
| 865 | static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg) |
| 866 | { |
| 867 | struct amdgpu_device *adev = info->dev->dev_private; |
| 868 | uint32_t r; |
| 869 | |
| 870 | r = RREG32_IO(reg); |
| 871 | return r; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * amdgpu_atombios_fini - free the driver info and callbacks for atombios |
| 876 | * |
| 877 | * @adev: amdgpu_device pointer |
| 878 | * |
| 879 | * Frees the driver info and register access callbacks for the ATOM |
| 880 | * interpreter (r4xx+). |
| 881 | * Called at driver shutdown. |
| 882 | */ |
| 883 | static void amdgpu_atombios_fini(struct amdgpu_device *adev) |
| 884 | { |
Monk Liu | 89e0ec9 | 2016-05-27 19:34:11 +0800 | [diff] [blame] | 885 | if (adev->mode_info.atom_context) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 886 | kfree(adev->mode_info.atom_context->scratch); |
Monk Liu | 89e0ec9 | 2016-05-27 19:34:11 +0800 | [diff] [blame] | 887 | kfree(adev->mode_info.atom_context->iio); |
| 888 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 889 | kfree(adev->mode_info.atom_context); |
| 890 | adev->mode_info.atom_context = NULL; |
| 891 | kfree(adev->mode_info.atom_card_info); |
| 892 | adev->mode_info.atom_card_info = NULL; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * amdgpu_atombios_init - init the driver info and callbacks for atombios |
| 897 | * |
| 898 | * @adev: amdgpu_device pointer |
| 899 | * |
| 900 | * Initializes the driver info and register access callbacks for the |
| 901 | * ATOM interpreter (r4xx+). |
| 902 | * Returns 0 on sucess, -ENOMEM on failure. |
| 903 | * Called at driver startup. |
| 904 | */ |
| 905 | static int amdgpu_atombios_init(struct amdgpu_device *adev) |
| 906 | { |
| 907 | struct card_info *atom_card_info = |
| 908 | kzalloc(sizeof(struct card_info), GFP_KERNEL); |
| 909 | |
| 910 | if (!atom_card_info) |
| 911 | return -ENOMEM; |
| 912 | |
| 913 | adev->mode_info.atom_card_info = atom_card_info; |
| 914 | atom_card_info->dev = adev->ddev; |
| 915 | atom_card_info->reg_read = cail_reg_read; |
| 916 | atom_card_info->reg_write = cail_reg_write; |
| 917 | /* needed for iio ops */ |
| 918 | if (adev->rio_mem) { |
| 919 | atom_card_info->ioreg_read = cail_ioreg_read; |
| 920 | atom_card_info->ioreg_write = cail_ioreg_write; |
| 921 | } else { |
| 922 | DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n"); |
| 923 | atom_card_info->ioreg_read = cail_reg_read; |
| 924 | atom_card_info->ioreg_write = cail_reg_write; |
| 925 | } |
| 926 | atom_card_info->mc_read = cail_mc_read; |
| 927 | atom_card_info->mc_write = cail_mc_write; |
| 928 | atom_card_info->pll_read = cail_pll_read; |
| 929 | atom_card_info->pll_write = cail_pll_write; |
| 930 | |
| 931 | adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios); |
| 932 | if (!adev->mode_info.atom_context) { |
| 933 | amdgpu_atombios_fini(adev); |
| 934 | return -ENOMEM; |
| 935 | } |
| 936 | |
| 937 | mutex_init(&adev->mode_info.atom_context->mutex); |
| 938 | amdgpu_atombios_scratch_regs_init(adev); |
| 939 | amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context); |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | /* if we get transitioned to only one device, take VGA back */ |
| 944 | /** |
| 945 | * amdgpu_vga_set_decode - enable/disable vga decode |
| 946 | * |
| 947 | * @cookie: amdgpu_device pointer |
| 948 | * @state: enable/disable vga decode |
| 949 | * |
| 950 | * Enable/disable vga decode (all asics). |
| 951 | * Returns VGA resource flags. |
| 952 | */ |
| 953 | static unsigned int amdgpu_vga_set_decode(void *cookie, bool state) |
| 954 | { |
| 955 | struct amdgpu_device *adev = cookie; |
| 956 | amdgpu_asic_set_vga_state(adev, state); |
| 957 | if (state) |
| 958 | return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | |
| 959 | VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 960 | else |
| 961 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * amdgpu_check_pot_argument - check that argument is a power of two |
| 966 | * |
| 967 | * @arg: value to check |
| 968 | * |
| 969 | * Validates that a certain argument is a power of two (all asics). |
| 970 | * Returns true if argument is valid. |
| 971 | */ |
| 972 | static bool amdgpu_check_pot_argument(int arg) |
| 973 | { |
| 974 | return (arg & (arg - 1)) == 0; |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * amdgpu_check_arguments - validate module params |
| 979 | * |
| 980 | * @adev: amdgpu_device pointer |
| 981 | * |
| 982 | * Validates certain module parameters and updates |
| 983 | * the associated values used by the driver (all asics). |
| 984 | */ |
| 985 | static void amdgpu_check_arguments(struct amdgpu_device *adev) |
| 986 | { |
Chunming Zhou | 5b01123 | 2015-12-10 17:34:33 +0800 | [diff] [blame] | 987 | if (amdgpu_sched_jobs < 4) { |
| 988 | dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n", |
| 989 | amdgpu_sched_jobs); |
| 990 | amdgpu_sched_jobs = 4; |
| 991 | } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){ |
| 992 | dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n", |
| 993 | amdgpu_sched_jobs); |
| 994 | amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs); |
| 995 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 996 | |
| 997 | if (amdgpu_gart_size != -1) { |
Christian König | c4e1a13 | 2016-03-17 16:25:15 +0100 | [diff] [blame] | 998 | /* gtt size must be greater or equal to 32M */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 999 | if (amdgpu_gart_size < 32) { |
| 1000 | dev_warn(adev->dev, "gart size (%d) too small\n", |
| 1001 | amdgpu_gart_size); |
| 1002 | amdgpu_gart_size = -1; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if (!amdgpu_check_pot_argument(amdgpu_vm_size)) { |
| 1007 | dev_warn(adev->dev, "VM size (%d) must be a power of 2\n", |
| 1008 | amdgpu_vm_size); |
Alex Deucher | 8dacc12 | 2015-05-11 16:20:58 -0400 | [diff] [blame] | 1009 | amdgpu_vm_size = 8; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | if (amdgpu_vm_size < 1) { |
| 1013 | dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n", |
| 1014 | amdgpu_vm_size); |
Alex Deucher | 8dacc12 | 2015-05-11 16:20:58 -0400 | [diff] [blame] | 1015 | amdgpu_vm_size = 8; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * Max GPUVM size for Cayman, SI and CI are 40 bits. |
| 1020 | */ |
| 1021 | if (amdgpu_vm_size > 1024) { |
| 1022 | dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n", |
| 1023 | amdgpu_vm_size); |
Alex Deucher | 8dacc12 | 2015-05-11 16:20:58 -0400 | [diff] [blame] | 1024 | amdgpu_vm_size = 8; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* defines number of bits in page table versus page directory, |
| 1028 | * a page is 4KB so we have 12 bits offset, minimum 9 bits in the |
| 1029 | * page table and the remaining bits are in the page directory */ |
| 1030 | if (amdgpu_vm_block_size == -1) { |
| 1031 | |
| 1032 | /* Total bits covered by PD + PTs */ |
| 1033 | unsigned bits = ilog2(amdgpu_vm_size) + 18; |
| 1034 | |
| 1035 | /* Make sure the PD is 4K in size up to 8GB address space. |
| 1036 | Above that split equal between PD and PTs */ |
| 1037 | if (amdgpu_vm_size <= 8) |
| 1038 | amdgpu_vm_block_size = bits - 9; |
| 1039 | else |
| 1040 | amdgpu_vm_block_size = (bits + 3) / 2; |
| 1041 | |
| 1042 | } else if (amdgpu_vm_block_size < 9) { |
| 1043 | dev_warn(adev->dev, "VM page table size (%d) too small\n", |
| 1044 | amdgpu_vm_block_size); |
| 1045 | amdgpu_vm_block_size = 9; |
| 1046 | } |
| 1047 | |
| 1048 | if (amdgpu_vm_block_size > 24 || |
| 1049 | (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) { |
| 1050 | dev_warn(adev->dev, "VM page table size (%d) too large\n", |
| 1051 | amdgpu_vm_block_size); |
| 1052 | amdgpu_vm_block_size = 9; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * amdgpu_switcheroo_set_state - set switcheroo state |
| 1058 | * |
| 1059 | * @pdev: pci dev pointer |
Lukas Wunner | 1694467 | 2015-09-05 11:17:35 +0200 | [diff] [blame] | 1060 | * @state: vga_switcheroo state |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1061 | * |
| 1062 | * Callback for the switcheroo driver. Suspends or resumes the |
| 1063 | * the asics before or after it is powered up using ACPI methods. |
| 1064 | */ |
| 1065 | static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state) |
| 1066 | { |
| 1067 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 1068 | |
| 1069 | if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF) |
| 1070 | return; |
| 1071 | |
| 1072 | if (state == VGA_SWITCHEROO_ON) { |
| 1073 | unsigned d3_delay = dev->pdev->d3_delay; |
| 1074 | |
| 1075 | printk(KERN_INFO "amdgpu: switched on\n"); |
| 1076 | /* don't suspend or resume card normally */ |
| 1077 | dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; |
| 1078 | |
Alex Deucher | 810ddc3 | 2016-08-23 13:25:49 -0400 | [diff] [blame] | 1079 | amdgpu_device_resume(dev, true, true); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1080 | |
| 1081 | dev->pdev->d3_delay = d3_delay; |
| 1082 | |
| 1083 | dev->switch_power_state = DRM_SWITCH_POWER_ON; |
| 1084 | drm_kms_helper_poll_enable(dev); |
| 1085 | } else { |
| 1086 | printk(KERN_INFO "amdgpu: switched off\n"); |
| 1087 | drm_kms_helper_poll_disable(dev); |
| 1088 | dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; |
Alex Deucher | 810ddc3 | 2016-08-23 13:25:49 -0400 | [diff] [blame] | 1089 | amdgpu_device_suspend(dev, true, true); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1090 | dev->switch_power_state = DRM_SWITCH_POWER_OFF; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * amdgpu_switcheroo_can_switch - see if switcheroo state can change |
| 1096 | * |
| 1097 | * @pdev: pci dev pointer |
| 1098 | * |
| 1099 | * Callback for the switcheroo driver. Check of the switcheroo |
| 1100 | * state can be changed. |
| 1101 | * Returns true if the state can be changed, false if not. |
| 1102 | */ |
| 1103 | static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev) |
| 1104 | { |
| 1105 | struct drm_device *dev = pci_get_drvdata(pdev); |
| 1106 | |
| 1107 | /* |
| 1108 | * FIXME: open_count is protected by drm_global_mutex but that would lead to |
| 1109 | * locking inversion with the driver load path. And the access here is |
| 1110 | * completely racy anyway. So don't bother with locking for now. |
| 1111 | */ |
| 1112 | return dev->open_count == 0; |
| 1113 | } |
| 1114 | |
| 1115 | static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = { |
| 1116 | .set_gpu_state = amdgpu_switcheroo_set_state, |
| 1117 | .reprobe = NULL, |
| 1118 | .can_switch = amdgpu_switcheroo_can_switch, |
| 1119 | }; |
| 1120 | |
| 1121 | int amdgpu_set_clockgating_state(struct amdgpu_device *adev, |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1122 | enum amd_ip_block_type block_type, |
| 1123 | enum amd_clockgating_state state) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1124 | { |
| 1125 | int i, r = 0; |
| 1126 | |
| 1127 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 9ecbe7f | 2016-06-23 11:53:12 -0400 | [diff] [blame] | 1128 | if (!adev->ip_block_status[i].valid) |
| 1129 | continue; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1130 | if (adev->ip_blocks[i].type == block_type) { |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1131 | r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1132 | state); |
| 1133 | if (r) |
| 1134 | return r; |
Alex Deucher | a225bf1 | 2016-06-23 11:48:30 -0400 | [diff] [blame] | 1135 | break; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | return r; |
| 1139 | } |
| 1140 | |
| 1141 | int amdgpu_set_powergating_state(struct amdgpu_device *adev, |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1142 | enum amd_ip_block_type block_type, |
| 1143 | enum amd_powergating_state state) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1144 | { |
| 1145 | int i, r = 0; |
| 1146 | |
| 1147 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 9ecbe7f | 2016-06-23 11:53:12 -0400 | [diff] [blame] | 1148 | if (!adev->ip_block_status[i].valid) |
| 1149 | continue; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1150 | if (adev->ip_blocks[i].type == block_type) { |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1151 | r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1152 | state); |
| 1153 | if (r) |
| 1154 | return r; |
Alex Deucher | a225bf1 | 2016-06-23 11:48:30 -0400 | [diff] [blame] | 1155 | break; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | return r; |
| 1159 | } |
| 1160 | |
Alex Deucher | 5dbbb60 | 2016-06-23 11:41:04 -0400 | [diff] [blame] | 1161 | int amdgpu_wait_for_idle(struct amdgpu_device *adev, |
| 1162 | enum amd_ip_block_type block_type) |
| 1163 | { |
| 1164 | int i, r; |
| 1165 | |
| 1166 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 9ecbe7f | 2016-06-23 11:53:12 -0400 | [diff] [blame] | 1167 | if (!adev->ip_block_status[i].valid) |
| 1168 | continue; |
Alex Deucher | 5dbbb60 | 2016-06-23 11:41:04 -0400 | [diff] [blame] | 1169 | if (adev->ip_blocks[i].type == block_type) { |
| 1170 | r = adev->ip_blocks[i].funcs->wait_for_idle((void *)adev); |
| 1171 | if (r) |
| 1172 | return r; |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | return 0; |
| 1177 | |
| 1178 | } |
| 1179 | |
| 1180 | bool amdgpu_is_idle(struct amdgpu_device *adev, |
| 1181 | enum amd_ip_block_type block_type) |
| 1182 | { |
| 1183 | int i; |
| 1184 | |
| 1185 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 9ecbe7f | 2016-06-23 11:53:12 -0400 | [diff] [blame] | 1186 | if (!adev->ip_block_status[i].valid) |
| 1187 | continue; |
Alex Deucher | 5dbbb60 | 2016-06-23 11:41:04 -0400 | [diff] [blame] | 1188 | if (adev->ip_blocks[i].type == block_type) |
| 1189 | return adev->ip_blocks[i].funcs->is_idle((void *)adev); |
| 1190 | } |
| 1191 | return true; |
| 1192 | |
| 1193 | } |
| 1194 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1195 | const struct amdgpu_ip_block_version * amdgpu_get_ip_block( |
| 1196 | struct amdgpu_device *adev, |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1197 | enum amd_ip_block_type type) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1198 | { |
| 1199 | int i; |
| 1200 | |
| 1201 | for (i = 0; i < adev->num_ip_blocks; i++) |
| 1202 | if (adev->ip_blocks[i].type == type) |
| 1203 | return &adev->ip_blocks[i]; |
| 1204 | |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | |
| 1208 | /** |
| 1209 | * amdgpu_ip_block_version_cmp |
| 1210 | * |
| 1211 | * @adev: amdgpu_device pointer |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1212 | * @type: enum amd_ip_block_type |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1213 | * @major: major version |
| 1214 | * @minor: minor version |
| 1215 | * |
| 1216 | * return 0 if equal or greater |
| 1217 | * return 1 if smaller or the ip_block doesn't exist |
| 1218 | */ |
| 1219 | int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev, |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1220 | enum amd_ip_block_type type, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1221 | u32 major, u32 minor) |
| 1222 | { |
| 1223 | const struct amdgpu_ip_block_version *ip_block; |
| 1224 | ip_block = amdgpu_get_ip_block(adev, type); |
| 1225 | |
| 1226 | if (ip_block && ((ip_block->major > major) || |
| 1227 | ((ip_block->major == major) && |
| 1228 | (ip_block->minor >= minor)))) |
| 1229 | return 0; |
| 1230 | |
| 1231 | return 1; |
| 1232 | } |
| 1233 | |
Alex Deucher | 483ef98 | 2016-09-30 12:43:04 -0400 | [diff] [blame] | 1234 | static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev) |
Emily Deng | 9accf2f | 2016-08-10 16:01:25 +0800 | [diff] [blame] | 1235 | { |
| 1236 | adev->enable_virtual_display = false; |
| 1237 | |
| 1238 | if (amdgpu_virtual_display) { |
| 1239 | struct drm_device *ddev = adev->ddev; |
| 1240 | const char *pci_address_name = pci_name(ddev->pdev); |
Emily Deng | 0f66356 | 2016-09-30 13:02:18 -0400 | [diff] [blame] | 1241 | char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname; |
Emily Deng | 9accf2f | 2016-08-10 16:01:25 +0800 | [diff] [blame] | 1242 | |
| 1243 | pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL); |
| 1244 | pciaddstr_tmp = pciaddstr; |
Emily Deng | 0f66356 | 2016-09-30 13:02:18 -0400 | [diff] [blame] | 1245 | while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) { |
| 1246 | pciaddname = strsep(&pciaddname_tmp, ","); |
Emily Deng | 9accf2f | 2016-08-10 16:01:25 +0800 | [diff] [blame] | 1247 | if (!strcmp(pci_address_name, pciaddname)) { |
Emily Deng | 0f66356 | 2016-09-30 13:02:18 -0400 | [diff] [blame] | 1248 | long num_crtc; |
| 1249 | int res = -1; |
| 1250 | |
Emily Deng | 9accf2f | 2016-08-10 16:01:25 +0800 | [diff] [blame] | 1251 | adev->enable_virtual_display = true; |
Emily Deng | 0f66356 | 2016-09-30 13:02:18 -0400 | [diff] [blame] | 1252 | |
| 1253 | if (pciaddname_tmp) |
| 1254 | res = kstrtol(pciaddname_tmp, 10, |
| 1255 | &num_crtc); |
| 1256 | |
| 1257 | if (!res) { |
| 1258 | if (num_crtc < 1) |
| 1259 | num_crtc = 1; |
| 1260 | if (num_crtc > 6) |
| 1261 | num_crtc = 6; |
| 1262 | adev->mode_info.num_crtc = num_crtc; |
| 1263 | } else { |
| 1264 | adev->mode_info.num_crtc = 1; |
| 1265 | } |
Emily Deng | 9accf2f | 2016-08-10 16:01:25 +0800 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | |
Emily Deng | 0f66356 | 2016-09-30 13:02:18 -0400 | [diff] [blame] | 1270 | DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n", |
| 1271 | amdgpu_virtual_display, pci_address_name, |
| 1272 | adev->enable_virtual_display, adev->mode_info.num_crtc); |
Emily Deng | 9accf2f | 2016-08-10 16:01:25 +0800 | [diff] [blame] | 1273 | |
| 1274 | kfree(pciaddstr); |
| 1275 | } |
| 1276 | } |
| 1277 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1278 | static int amdgpu_early_init(struct amdgpu_device *adev) |
| 1279 | { |
Alex Deucher | aaa36a9 | 2015-04-20 17:31:14 -0400 | [diff] [blame] | 1280 | int i, r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1281 | |
Alex Deucher | 483ef98 | 2016-09-30 12:43:04 -0400 | [diff] [blame] | 1282 | amdgpu_device_enable_virtual_display(adev); |
Emily Deng | a6be757 | 2016-08-08 11:37:50 +0800 | [diff] [blame] | 1283 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1284 | switch (adev->asic_type) { |
Alex Deucher | aaa36a9 | 2015-04-20 17:31:14 -0400 | [diff] [blame] | 1285 | case CHIP_TOPAZ: |
| 1286 | case CHIP_TONGA: |
David Zhang | 48299f9 | 2015-07-08 01:05:16 +0800 | [diff] [blame] | 1287 | case CHIP_FIJI: |
Flora Cui | 2cc0c0b | 2016-03-14 18:33:29 -0400 | [diff] [blame] | 1288 | case CHIP_POLARIS11: |
| 1289 | case CHIP_POLARIS10: |
Alex Deucher | aaa36a9 | 2015-04-20 17:31:14 -0400 | [diff] [blame] | 1290 | case CHIP_CARRIZO: |
Samuel Li | 39bb0c9 | 2015-10-08 16:31:43 -0400 | [diff] [blame] | 1291 | case CHIP_STONEY: |
| 1292 | if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY) |
Alex Deucher | aaa36a9 | 2015-04-20 17:31:14 -0400 | [diff] [blame] | 1293 | adev->family = AMDGPU_FAMILY_CZ; |
| 1294 | else |
| 1295 | adev->family = AMDGPU_FAMILY_VI; |
| 1296 | |
| 1297 | r = vi_set_ip_blocks(adev); |
| 1298 | if (r) |
| 1299 | return r; |
| 1300 | break; |
Ken Wang | 33f3480 | 2016-01-21 17:29:41 +0800 | [diff] [blame] | 1301 | #ifdef CONFIG_DRM_AMDGPU_SI |
| 1302 | case CHIP_VERDE: |
| 1303 | case CHIP_TAHITI: |
| 1304 | case CHIP_PITCAIRN: |
| 1305 | case CHIP_OLAND: |
| 1306 | case CHIP_HAINAN: |
Ken Wang | 295d0da | 2016-05-24 21:02:53 +0800 | [diff] [blame] | 1307 | adev->family = AMDGPU_FAMILY_SI; |
Ken Wang | 33f3480 | 2016-01-21 17:29:41 +0800 | [diff] [blame] | 1308 | r = si_set_ip_blocks(adev); |
| 1309 | if (r) |
| 1310 | return r; |
| 1311 | break; |
| 1312 | #endif |
Alex Deucher | a2e73f5 | 2015-04-20 17:09:27 -0400 | [diff] [blame] | 1313 | #ifdef CONFIG_DRM_AMDGPU_CIK |
| 1314 | case CHIP_BONAIRE: |
| 1315 | case CHIP_HAWAII: |
| 1316 | case CHIP_KAVERI: |
| 1317 | case CHIP_KABINI: |
| 1318 | case CHIP_MULLINS: |
| 1319 | if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII)) |
| 1320 | adev->family = AMDGPU_FAMILY_CI; |
| 1321 | else |
| 1322 | adev->family = AMDGPU_FAMILY_KV; |
| 1323 | |
| 1324 | r = cik_set_ip_blocks(adev); |
| 1325 | if (r) |
| 1326 | return r; |
| 1327 | break; |
| 1328 | #endif |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1329 | default: |
| 1330 | /* FIXME: not supported yet */ |
| 1331 | return -EINVAL; |
| 1332 | } |
| 1333 | |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1334 | adev->ip_block_status = kcalloc(adev->num_ip_blocks, |
| 1335 | sizeof(struct amdgpu_ip_block_status), GFP_KERNEL); |
| 1336 | if (adev->ip_block_status == NULL) |
Alex Deucher | d8d090b | 2015-06-26 13:02:57 -0400 | [diff] [blame] | 1337 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1338 | |
| 1339 | if (adev->ip_blocks == NULL) { |
| 1340 | DRM_ERROR("No IP blocks found!\n"); |
| 1341 | return r; |
| 1342 | } |
| 1343 | |
| 1344 | for (i = 0; i < adev->num_ip_blocks; i++) { |
| 1345 | if ((amdgpu_ip_block_mask & (1 << i)) == 0) { |
| 1346 | DRM_ERROR("disabled ip block: %d\n", i); |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1347 | adev->ip_block_status[i].valid = false; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1348 | } else { |
| 1349 | if (adev->ip_blocks[i].funcs->early_init) { |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1350 | r = adev->ip_blocks[i].funcs->early_init((void *)adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1351 | if (r == -ENOENT) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1352 | adev->ip_block_status[i].valid = false; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1353 | } else if (r) { |
Tom St Denis | 88a907d | 2016-05-04 14:28:35 -0400 | [diff] [blame] | 1354 | DRM_ERROR("early_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1355 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1356 | } else { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1357 | adev->ip_block_status[i].valid = true; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1358 | } |
Alex Deucher | 974e6b6 | 2015-07-10 13:59:44 -0400 | [diff] [blame] | 1359 | } else { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1360 | adev->ip_block_status[i].valid = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1361 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | |
Nicolai Hähnle | 395d1fb | 2016-06-02 12:32:07 +0200 | [diff] [blame] | 1365 | adev->cg_flags &= amdgpu_cg_mask; |
| 1366 | adev->pg_flags &= amdgpu_pg_mask; |
| 1367 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | static int amdgpu_init(struct amdgpu_device *adev) |
| 1372 | { |
| 1373 | int i, r; |
| 1374 | |
| 1375 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1376 | if (!adev->ip_block_status[i].valid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1377 | continue; |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1378 | r = adev->ip_blocks[i].funcs->sw_init((void *)adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1379 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1380 | DRM_ERROR("sw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1381 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1382 | } |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1383 | adev->ip_block_status[i].sw = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1384 | /* need to do gmc hw init early so we can allocate gpu mem */ |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1385 | if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1386 | r = amdgpu_vram_scratch_init(adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1387 | if (r) { |
| 1388 | DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1389 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1390 | } |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1391 | r = adev->ip_blocks[i].funcs->hw_init((void *)adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1392 | if (r) { |
| 1393 | DRM_ERROR("hw_init %d failed %d\n", i, r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1394 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1395 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1396 | r = amdgpu_wb_init(adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1397 | if (r) { |
| 1398 | DRM_ERROR("amdgpu_wb_init failed %d\n", r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1399 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1400 | } |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1401 | adev->ip_block_status[i].hw = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1406 | if (!adev->ip_block_status[i].sw) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1407 | continue; |
| 1408 | /* gmc hw init is done early */ |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1409 | if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1410 | continue; |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1411 | r = adev->ip_blocks[i].funcs->hw_init((void *)adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1412 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1413 | DRM_ERROR("hw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1414 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1415 | } |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1416 | adev->ip_block_status[i].hw = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | static int amdgpu_late_init(struct amdgpu_device *adev) |
| 1423 | { |
| 1424 | int i = 0, r; |
| 1425 | |
| 1426 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1427 | if (!adev->ip_block_status[i].valid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1428 | continue; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1429 | if (adev->ip_blocks[i].funcs->late_init) { |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1430 | r = adev->ip_blocks[i].funcs->late_init((void *)adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1431 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1432 | DRM_ERROR("late_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1433 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1434 | } |
Grazvydas Ignotas | 8a2eef1 | 2016-10-03 00:06:44 +0300 | [diff] [blame] | 1435 | adev->ip_block_status[i].late_initialized = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1436 | } |
Alex Deucher | 4a446d5 | 2016-10-07 14:48:18 -0400 | [diff] [blame] | 1437 | /* skip CG for VCE/UVD, it's handled specially */ |
| 1438 | if (adev->ip_blocks[i].type != AMD_IP_BLOCK_TYPE_UVD && |
| 1439 | adev->ip_blocks[i].type != AMD_IP_BLOCK_TYPE_VCE) { |
| 1440 | /* enable clockgating to save power */ |
| 1441 | r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev, |
| 1442 | AMD_CG_STATE_GATE); |
| 1443 | if (r) { |
| 1444 | DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n", |
| 1445 | adev->ip_blocks[i].funcs->name, r); |
| 1446 | return r; |
| 1447 | } |
Arindam Nath | b0b00ff | 2016-10-07 19:01:37 +0530 | [diff] [blame] | 1448 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | static int amdgpu_fini(struct amdgpu_device *adev) |
| 1455 | { |
| 1456 | int i, r; |
| 1457 | |
Alex Deucher | 3e96dbf | 2016-10-13 11:22:17 -0400 | [diff] [blame] | 1458 | /* need to disable SMC first */ |
| 1459 | for (i = 0; i < adev->num_ip_blocks; i++) { |
| 1460 | if (!adev->ip_block_status[i].hw) |
| 1461 | continue; |
| 1462 | if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_SMC) { |
| 1463 | /* ungate blocks before hw fini so that we can shutdown the blocks safely */ |
| 1464 | r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev, |
| 1465 | AMD_CG_STATE_UNGATE); |
| 1466 | if (r) { |
| 1467 | DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", |
| 1468 | adev->ip_blocks[i].funcs->name, r); |
| 1469 | return r; |
| 1470 | } |
| 1471 | r = adev->ip_blocks[i].funcs->hw_fini((void *)adev); |
| 1472 | /* XXX handle errors */ |
| 1473 | if (r) { |
| 1474 | DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", |
| 1475 | adev->ip_blocks[i].funcs->name, r); |
| 1476 | } |
| 1477 | adev->ip_block_status[i].hw = false; |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1482 | for (i = adev->num_ip_blocks - 1; i >= 0; i--) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1483 | if (!adev->ip_block_status[i].hw) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1484 | continue; |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1485 | if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1486 | amdgpu_wb_fini(adev); |
| 1487 | amdgpu_vram_scratch_fini(adev); |
| 1488 | } |
| 1489 | /* ungate blocks before hw fini so that we can shutdown the blocks safely */ |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1490 | r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev, |
| 1491 | AMD_CG_STATE_UNGATE); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1492 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1493 | DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1494 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1495 | } |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1496 | r = adev->ip_blocks[i].funcs->hw_fini((void *)adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1497 | /* XXX handle errors */ |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1498 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1499 | DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1500 | } |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1501 | adev->ip_block_status[i].hw = false; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | for (i = adev->num_ip_blocks - 1; i >= 0; i--) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1505 | if (!adev->ip_block_status[i].sw) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1506 | continue; |
yanyang1 | 5fc3aee | 2015-05-22 14:39:35 -0400 | [diff] [blame] | 1507 | r = adev->ip_blocks[i].funcs->sw_fini((void *)adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1508 | /* XXX handle errors */ |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1509 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1510 | DRM_DEBUG("sw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1511 | } |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1512 | adev->ip_block_status[i].sw = false; |
| 1513 | adev->ip_block_status[i].valid = false; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
Monk Liu | a6dcfd9 | 2016-05-19 14:36:34 +0800 | [diff] [blame] | 1516 | for (i = adev->num_ip_blocks - 1; i >= 0; i--) { |
Grazvydas Ignotas | 8a2eef1 | 2016-10-03 00:06:44 +0300 | [diff] [blame] | 1517 | if (!adev->ip_block_status[i].late_initialized) |
| 1518 | continue; |
Monk Liu | a6dcfd9 | 2016-05-19 14:36:34 +0800 | [diff] [blame] | 1519 | if (adev->ip_blocks[i].funcs->late_fini) |
| 1520 | adev->ip_blocks[i].funcs->late_fini((void *)adev); |
Grazvydas Ignotas | 8a2eef1 | 2016-10-03 00:06:44 +0300 | [diff] [blame] | 1521 | adev->ip_block_status[i].late_initialized = false; |
Monk Liu | a6dcfd9 | 2016-05-19 14:36:34 +0800 | [diff] [blame] | 1522 | } |
| 1523 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | static int amdgpu_suspend(struct amdgpu_device *adev) |
| 1528 | { |
| 1529 | int i, r; |
| 1530 | |
Flora Cui | c5a93a2 | 2016-02-26 10:45:25 +0800 | [diff] [blame] | 1531 | /* ungate SMC block first */ |
| 1532 | r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC, |
| 1533 | AMD_CG_STATE_UNGATE); |
| 1534 | if (r) { |
| 1535 | DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r); |
| 1536 | } |
| 1537 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1538 | for (i = adev->num_ip_blocks - 1; i >= 0; i--) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1539 | if (!adev->ip_block_status[i].valid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1540 | continue; |
| 1541 | /* ungate blocks so that suspend can properly shut them down */ |
Flora Cui | c5a93a2 | 2016-02-26 10:45:25 +0800 | [diff] [blame] | 1542 | if (i != AMD_IP_BLOCK_TYPE_SMC) { |
| 1543 | r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev, |
| 1544 | AMD_CG_STATE_UNGATE); |
| 1545 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1546 | DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Flora Cui | c5a93a2 | 2016-02-26 10:45:25 +0800 | [diff] [blame] | 1547 | } |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1548 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1549 | /* XXX handle errors */ |
| 1550 | r = adev->ip_blocks[i].funcs->suspend(adev); |
| 1551 | /* XXX handle errors */ |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1552 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1553 | DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1554 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | static int amdgpu_resume(struct amdgpu_device *adev) |
| 1561 | { |
| 1562 | int i, r; |
| 1563 | |
| 1564 | for (i = 0; i < adev->num_ip_blocks; i++) { |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1565 | if (!adev->ip_block_status[i].valid) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1566 | continue; |
| 1567 | r = adev->ip_blocks[i].funcs->resume(adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1568 | if (r) { |
Tom St Denis | 822b2ce | 2016-05-05 10:23:40 -0400 | [diff] [blame] | 1569 | DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1570 | return r; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1571 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
Monk Liu | 4e99a44 | 2016-03-31 13:26:59 +0800 | [diff] [blame] | 1577 | static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev) |
Andres Rodriguez | 048765a | 2016-06-11 02:51:32 -0400 | [diff] [blame] | 1578 | { |
Monk Liu | 4e99a44 | 2016-03-31 13:26:59 +0800 | [diff] [blame] | 1579 | if (amdgpu_atombios_has_gpu_virtualization_table(adev)) |
| 1580 | adev->virtualization.virtual_caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS; |
Andres Rodriguez | 048765a | 2016-06-11 02:51:32 -0400 | [diff] [blame] | 1581 | } |
| 1582 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1583 | /** |
| 1584 | * amdgpu_device_init - initialize the driver |
| 1585 | * |
| 1586 | * @adev: amdgpu_device pointer |
| 1587 | * @pdev: drm dev pointer |
| 1588 | * @pdev: pci dev pointer |
| 1589 | * @flags: driver flags |
| 1590 | * |
| 1591 | * Initializes the driver info and hw (all asics). |
| 1592 | * Returns 0 for success or an error on failure. |
| 1593 | * Called at driver startup. |
| 1594 | */ |
| 1595 | int amdgpu_device_init(struct amdgpu_device *adev, |
| 1596 | struct drm_device *ddev, |
| 1597 | struct pci_dev *pdev, |
| 1598 | uint32_t flags) |
| 1599 | { |
| 1600 | int r, i; |
| 1601 | bool runtime = false; |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 1602 | u32 max_MBps; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1603 | |
| 1604 | adev->shutdown = false; |
| 1605 | adev->dev = &pdev->dev; |
| 1606 | adev->ddev = ddev; |
| 1607 | adev->pdev = pdev; |
| 1608 | adev->flags = flags; |
Jammy Zhou | 2f7d10b | 2015-07-22 11:29:01 +0800 | [diff] [blame] | 1609 | adev->asic_type = flags & AMD_ASIC_MASK; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1610 | adev->is_atom_bios = false; |
| 1611 | adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT; |
| 1612 | adev->mc.gtt_size = 512 * 1024 * 1024; |
| 1613 | adev->accel_working = false; |
| 1614 | adev->num_rings = 0; |
| 1615 | adev->mman.buffer_funcs = NULL; |
| 1616 | adev->mman.buffer_funcs_ring = NULL; |
| 1617 | adev->vm_manager.vm_pte_funcs = NULL; |
Christian König | 2d55e45 | 2016-02-08 17:37:38 +0100 | [diff] [blame] | 1618 | adev->vm_manager.vm_pte_num_rings = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1619 | adev->gart.gart_funcs = NULL; |
| 1620 | adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS); |
| 1621 | |
| 1622 | adev->smc_rreg = &amdgpu_invalid_rreg; |
| 1623 | adev->smc_wreg = &amdgpu_invalid_wreg; |
| 1624 | adev->pcie_rreg = &amdgpu_invalid_rreg; |
| 1625 | adev->pcie_wreg = &amdgpu_invalid_wreg; |
Huang Rui | 36b9a95 | 2016-08-31 13:23:25 +0800 | [diff] [blame] | 1626 | adev->pciep_rreg = &amdgpu_invalid_rreg; |
| 1627 | adev->pciep_wreg = &amdgpu_invalid_wreg; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1628 | adev->uvd_ctx_rreg = &amdgpu_invalid_rreg; |
| 1629 | adev->uvd_ctx_wreg = &amdgpu_invalid_wreg; |
| 1630 | adev->didt_rreg = &amdgpu_invalid_rreg; |
| 1631 | adev->didt_wreg = &amdgpu_invalid_wreg; |
Rex Zhu | ccdbb20 | 2016-06-08 12:47:41 +0800 | [diff] [blame] | 1632 | adev->gc_cac_rreg = &amdgpu_invalid_rreg; |
| 1633 | adev->gc_cac_wreg = &amdgpu_invalid_wreg; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1634 | adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg; |
| 1635 | adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg; |
| 1636 | |
Rex Zhu | ccdbb20 | 2016-06-08 12:47:41 +0800 | [diff] [blame] | 1637 | |
Alex Deucher | 3e39ab9 | 2015-06-05 15:04:33 -0400 | [diff] [blame] | 1638 | DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n", |
| 1639 | amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device, |
| 1640 | pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1641 | |
| 1642 | /* mutex initialization are all done here so we |
| 1643 | * can recall function without having locking issues */ |
Christian König | 8d0a7ce | 2015-11-03 20:58:50 +0100 | [diff] [blame] | 1644 | mutex_init(&adev->vm_manager.lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1645 | atomic_set(&adev->irq.ih.lock, 0); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1646 | mutex_init(&adev->pm.mutex); |
| 1647 | mutex_init(&adev->gfx.gpu_clock_mutex); |
| 1648 | mutex_init(&adev->srbm_mutex); |
| 1649 | mutex_init(&adev->grbm_idx_mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1650 | mutex_init(&adev->mn_lock); |
| 1651 | hash_init(adev->mn_hash); |
| 1652 | |
| 1653 | amdgpu_check_arguments(adev); |
| 1654 | |
| 1655 | /* Registers mapping */ |
| 1656 | /* TODO: block userspace mapping of io register */ |
| 1657 | spin_lock_init(&adev->mmio_idx_lock); |
| 1658 | spin_lock_init(&adev->smc_idx_lock); |
| 1659 | spin_lock_init(&adev->pcie_idx_lock); |
| 1660 | spin_lock_init(&adev->uvd_ctx_idx_lock); |
| 1661 | spin_lock_init(&adev->didt_idx_lock); |
Rex Zhu | ccdbb20 | 2016-06-08 12:47:41 +0800 | [diff] [blame] | 1662 | spin_lock_init(&adev->gc_cac_idx_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1663 | spin_lock_init(&adev->audio_endpt_idx_lock); |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 1664 | spin_lock_init(&adev->mm_stats.lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1665 | |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 1666 | INIT_LIST_HEAD(&adev->shadow_list); |
| 1667 | mutex_init(&adev->shadow_list_lock); |
| 1668 | |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 1669 | INIT_LIST_HEAD(&adev->gtt_list); |
| 1670 | spin_lock_init(&adev->gtt_list_lock); |
| 1671 | |
Ken Wang | da69c161 | 2016-01-21 19:08:55 +0800 | [diff] [blame] | 1672 | if (adev->asic_type >= CHIP_BONAIRE) { |
| 1673 | adev->rmmio_base = pci_resource_start(adev->pdev, 5); |
| 1674 | adev->rmmio_size = pci_resource_len(adev->pdev, 5); |
| 1675 | } else { |
| 1676 | adev->rmmio_base = pci_resource_start(adev->pdev, 2); |
| 1677 | adev->rmmio_size = pci_resource_len(adev->pdev, 2); |
| 1678 | } |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 1679 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1680 | adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size); |
| 1681 | if (adev->rmmio == NULL) { |
| 1682 | return -ENOMEM; |
| 1683 | } |
| 1684 | DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base); |
| 1685 | DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size); |
| 1686 | |
Ken Wang | da69c161 | 2016-01-21 19:08:55 +0800 | [diff] [blame] | 1687 | if (adev->asic_type >= CHIP_BONAIRE) |
| 1688 | /* doorbell bar mapping */ |
| 1689 | amdgpu_doorbell_init(adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1690 | |
| 1691 | /* io port mapping */ |
| 1692 | for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { |
| 1693 | if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) { |
| 1694 | adev->rio_mem_size = pci_resource_len(adev->pdev, i); |
| 1695 | adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size); |
| 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | if (adev->rio_mem == NULL) |
| 1700 | DRM_ERROR("Unable to find PCI I/O BAR\n"); |
| 1701 | |
| 1702 | /* early init functions */ |
| 1703 | r = amdgpu_early_init(adev); |
| 1704 | if (r) |
| 1705 | return r; |
| 1706 | |
| 1707 | /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */ |
| 1708 | /* this will fail for cards that aren't VGA class devices, just |
| 1709 | * ignore it */ |
| 1710 | vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode); |
| 1711 | |
| 1712 | if (amdgpu_runtime_pm == 1) |
| 1713 | runtime = true; |
Alex Deucher | e9bef45 | 2016-04-25 13:12:18 -0400 | [diff] [blame] | 1714 | if (amdgpu_device_is_px(ddev)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1715 | runtime = true; |
| 1716 | vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime); |
| 1717 | if (runtime) |
| 1718 | vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain); |
| 1719 | |
| 1720 | /* Read BIOS */ |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1721 | if (!amdgpu_get_bios(adev)) { |
| 1722 | r = -EINVAL; |
| 1723 | goto failed; |
| 1724 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1725 | /* Must be an ATOMBIOS */ |
| 1726 | if (!adev->is_atom_bios) { |
| 1727 | dev_err(adev->dev, "Expecting atombios for GPU\n"); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1728 | r = -EINVAL; |
| 1729 | goto failed; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1730 | } |
| 1731 | r = amdgpu_atombios_init(adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1732 | if (r) { |
| 1733 | dev_err(adev->dev, "amdgpu_atombios_init failed\n"); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1734 | goto failed; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1735 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1736 | |
Monk Liu | 4e99a44 | 2016-03-31 13:26:59 +0800 | [diff] [blame] | 1737 | /* detect if we are with an SRIOV vbios */ |
| 1738 | amdgpu_device_detect_sriov_bios(adev); |
Andres Rodriguez | 048765a | 2016-06-11 02:51:32 -0400 | [diff] [blame] | 1739 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1740 | /* Post card if necessary */ |
Monk Liu | bec8637 | 2016-09-14 19:38:08 +0800 | [diff] [blame] | 1741 | if (amdgpu_vpost_needed(adev)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1742 | if (!adev->bios) { |
Monk Liu | bec8637 | 2016-09-14 19:38:08 +0800 | [diff] [blame] | 1743 | dev_err(adev->dev, "no vBIOS found\n"); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1744 | r = -EINVAL; |
| 1745 | goto failed; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1746 | } |
Monk Liu | bec8637 | 2016-09-14 19:38:08 +0800 | [diff] [blame] | 1747 | DRM_INFO("GPU posting now...\n"); |
Monk Liu | 4e99a44 | 2016-03-31 13:26:59 +0800 | [diff] [blame] | 1748 | r = amdgpu_atom_asic_init(adev->mode_info.atom_context); |
| 1749 | if (r) { |
| 1750 | dev_err(adev->dev, "gpu post error!\n"); |
| 1751 | goto failed; |
| 1752 | } |
| 1753 | } else { |
| 1754 | DRM_INFO("GPU post is not needed\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | /* Initialize clocks */ |
| 1758 | r = amdgpu_atombios_get_clock_info(adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1759 | if (r) { |
| 1760 | dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n"); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1761 | goto failed; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1762 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1763 | /* init i2c buses */ |
| 1764 | amdgpu_atombios_i2c_init(adev); |
| 1765 | |
| 1766 | /* Fence driver */ |
| 1767 | r = amdgpu_fence_driver_init(adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1768 | if (r) { |
| 1769 | dev_err(adev->dev, "amdgpu_fence_driver_init failed\n"); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1770 | goto failed; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1771 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1772 | |
| 1773 | /* init the mode config */ |
| 1774 | drm_mode_config_init(adev->ddev); |
| 1775 | |
| 1776 | r = amdgpu_init(adev); |
| 1777 | if (r) { |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1778 | dev_err(adev->dev, "amdgpu_init failed\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1779 | amdgpu_fini(adev); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1780 | goto failed; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | adev->accel_working = true; |
| 1784 | |
Marek Olšák | 95844d2 | 2016-08-17 23:49:27 +0200 | [diff] [blame] | 1785 | /* Initialize the buffer migration limit. */ |
| 1786 | if (amdgpu_moverate >= 0) |
| 1787 | max_MBps = amdgpu_moverate; |
| 1788 | else |
| 1789 | max_MBps = 8; /* Allow 8 MB/s. */ |
| 1790 | /* Get a log2 for easy divisions. */ |
| 1791 | adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps)); |
| 1792 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1793 | amdgpu_fbdev_init(adev); |
| 1794 | |
| 1795 | r = amdgpu_ib_pool_init(adev); |
| 1796 | if (r) { |
| 1797 | dev_err(adev->dev, "IB initialization failed (%d).\n", r); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1798 | goto failed; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | r = amdgpu_ib_ring_tests(adev); |
| 1802 | if (r) |
| 1803 | DRM_ERROR("ib ring test failed (%d).\n", r); |
| 1804 | |
| 1805 | r = amdgpu_gem_debugfs_init(adev); |
| 1806 | if (r) { |
| 1807 | DRM_ERROR("registering gem debugfs failed (%d).\n", r); |
| 1808 | } |
| 1809 | |
| 1810 | r = amdgpu_debugfs_regs_init(adev); |
| 1811 | if (r) { |
| 1812 | DRM_ERROR("registering register debugfs failed (%d).\n", r); |
| 1813 | } |
| 1814 | |
Huang Rui | 50ab253 | 2016-06-12 15:51:09 +0800 | [diff] [blame] | 1815 | r = amdgpu_debugfs_firmware_init(adev); |
| 1816 | if (r) { |
| 1817 | DRM_ERROR("registering firmware debugfs failed (%d).\n", r); |
| 1818 | return r; |
| 1819 | } |
| 1820 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1821 | if ((amdgpu_testing & 1)) { |
| 1822 | if (adev->accel_working) |
| 1823 | amdgpu_test_moves(adev); |
| 1824 | else |
| 1825 | DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n"); |
| 1826 | } |
| 1827 | if ((amdgpu_testing & 2)) { |
| 1828 | if (adev->accel_working) |
| 1829 | amdgpu_test_syncing(adev); |
| 1830 | else |
| 1831 | DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n"); |
| 1832 | } |
| 1833 | if (amdgpu_benchmarking) { |
| 1834 | if (adev->accel_working) |
| 1835 | amdgpu_benchmark(adev, amdgpu_benchmarking); |
| 1836 | else |
| 1837 | DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n"); |
| 1838 | } |
| 1839 | |
| 1840 | /* enable clockgating, etc. after ib tests, etc. since some blocks require |
| 1841 | * explicit gating rather than handling it automatically. |
| 1842 | */ |
| 1843 | r = amdgpu_late_init(adev); |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1844 | if (r) { |
| 1845 | dev_err(adev->dev, "amdgpu_late_init failed\n"); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1846 | goto failed; |
Alex Deucher | 2c1a278 | 2015-12-07 17:02:53 -0500 | [diff] [blame] | 1847 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1848 | |
| 1849 | return 0; |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1850 | |
| 1851 | failed: |
| 1852 | if (runtime) |
| 1853 | vga_switcheroo_fini_domain_pm_ops(adev->dev); |
| 1854 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev); |
| 1858 | |
| 1859 | /** |
| 1860 | * amdgpu_device_fini - tear down the driver |
| 1861 | * |
| 1862 | * @adev: amdgpu_device pointer |
| 1863 | * |
| 1864 | * Tear down the driver info (all asics). |
| 1865 | * Called at driver shutdown. |
| 1866 | */ |
| 1867 | void amdgpu_device_fini(struct amdgpu_device *adev) |
| 1868 | { |
| 1869 | int r; |
| 1870 | |
| 1871 | DRM_INFO("amdgpu: finishing device.\n"); |
| 1872 | adev->shutdown = true; |
Grazvydas Ignotas | a951ed8 | 2016-09-25 23:34:48 +0300 | [diff] [blame] | 1873 | drm_crtc_force_disable_all(adev->ddev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1874 | /* evict vram memory */ |
| 1875 | amdgpu_bo_evict_vram(adev); |
| 1876 | amdgpu_ib_pool_fini(adev); |
| 1877 | amdgpu_fence_driver_fini(adev); |
| 1878 | amdgpu_fbdev_fini(adev); |
| 1879 | r = amdgpu_fini(adev); |
Alex Deucher | 8faf0e08 | 2015-07-28 11:50:31 -0400 | [diff] [blame] | 1880 | kfree(adev->ip_block_status); |
| 1881 | adev->ip_block_status = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1882 | adev->accel_working = false; |
| 1883 | /* free i2c buses */ |
| 1884 | amdgpu_i2c_fini(adev); |
| 1885 | amdgpu_atombios_fini(adev); |
| 1886 | kfree(adev->bios); |
| 1887 | adev->bios = NULL; |
| 1888 | vga_switcheroo_unregister_client(adev->pdev); |
Alex Deucher | 83ba126 | 2016-06-03 18:21:41 -0400 | [diff] [blame] | 1889 | if (adev->flags & AMD_IS_PX) |
| 1890 | vga_switcheroo_fini_domain_pm_ops(adev->dev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1891 | vga_client_register(adev->pdev, NULL, NULL, NULL); |
| 1892 | if (adev->rio_mem) |
| 1893 | pci_iounmap(adev->pdev, adev->rio_mem); |
| 1894 | adev->rio_mem = NULL; |
| 1895 | iounmap(adev->rmmio); |
| 1896 | adev->rmmio = NULL; |
Ken Wang | da69c161 | 2016-01-21 19:08:55 +0800 | [diff] [blame] | 1897 | if (adev->asic_type >= CHIP_BONAIRE) |
| 1898 | amdgpu_doorbell_fini(adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1899 | amdgpu_debugfs_regs_cleanup(adev); |
| 1900 | amdgpu_debugfs_remove_files(adev); |
| 1901 | } |
| 1902 | |
| 1903 | |
| 1904 | /* |
| 1905 | * Suspend & resume. |
| 1906 | */ |
| 1907 | /** |
Alex Deucher | 810ddc3 | 2016-08-23 13:25:49 -0400 | [diff] [blame] | 1908 | * amdgpu_device_suspend - initiate device suspend |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1909 | * |
| 1910 | * @pdev: drm dev pointer |
| 1911 | * @state: suspend state |
| 1912 | * |
| 1913 | * Puts the hw in the suspend state (all asics). |
| 1914 | * Returns 0 for success or an error on failure. |
| 1915 | * Called at driver suspend. |
| 1916 | */ |
Alex Deucher | 810ddc3 | 2016-08-23 13:25:49 -0400 | [diff] [blame] | 1917 | int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1918 | { |
| 1919 | struct amdgpu_device *adev; |
| 1920 | struct drm_crtc *crtc; |
| 1921 | struct drm_connector *connector; |
Alex Deucher | 5ceb54c | 2015-08-05 12:41:48 -0400 | [diff] [blame] | 1922 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1923 | |
| 1924 | if (dev == NULL || dev->dev_private == NULL) { |
| 1925 | return -ENODEV; |
| 1926 | } |
| 1927 | |
| 1928 | adev = dev->dev_private; |
| 1929 | |
| 1930 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) |
| 1931 | return 0; |
| 1932 | |
| 1933 | drm_kms_helper_poll_disable(dev); |
| 1934 | |
| 1935 | /* turn off display hw */ |
Alex Deucher | 4c7fbc3 | 2015-09-23 14:32:06 -0400 | [diff] [blame] | 1936 | drm_modeset_lock_all(dev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1937 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 1938 | drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF); |
| 1939 | } |
Alex Deucher | 4c7fbc3 | 2015-09-23 14:32:06 -0400 | [diff] [blame] | 1940 | drm_modeset_unlock_all(dev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1941 | |
Alex Deucher | 756e688 | 2015-10-08 00:03:36 -0400 | [diff] [blame] | 1942 | /* unpin the front buffers and cursors */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1943 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
Alex Deucher | 756e688 | 2015-10-08 00:03:36 -0400 | [diff] [blame] | 1944 | struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1945 | struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb); |
| 1946 | struct amdgpu_bo *robj; |
| 1947 | |
Alex Deucher | 756e688 | 2015-10-08 00:03:36 -0400 | [diff] [blame] | 1948 | if (amdgpu_crtc->cursor_bo) { |
| 1949 | struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo); |
| 1950 | r = amdgpu_bo_reserve(aobj, false); |
| 1951 | if (r == 0) { |
| 1952 | amdgpu_bo_unpin(aobj); |
| 1953 | amdgpu_bo_unreserve(aobj); |
| 1954 | } |
| 1955 | } |
| 1956 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1957 | if (rfb == NULL || rfb->obj == NULL) { |
| 1958 | continue; |
| 1959 | } |
| 1960 | robj = gem_to_amdgpu_bo(rfb->obj); |
| 1961 | /* don't unpin kernel fb objects */ |
| 1962 | if (!amdgpu_fbdev_robj_is_fb(adev, robj)) { |
| 1963 | r = amdgpu_bo_reserve(robj, false); |
| 1964 | if (r == 0) { |
| 1965 | amdgpu_bo_unpin(robj); |
| 1966 | amdgpu_bo_unreserve(robj); |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | /* evict vram memory */ |
| 1971 | amdgpu_bo_evict_vram(adev); |
| 1972 | |
Alex Deucher | 5ceb54c | 2015-08-05 12:41:48 -0400 | [diff] [blame] | 1973 | amdgpu_fence_driver_suspend(adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1974 | |
| 1975 | r = amdgpu_suspend(adev); |
| 1976 | |
| 1977 | /* evict remaining vram memory */ |
| 1978 | amdgpu_bo_evict_vram(adev); |
| 1979 | |
| 1980 | pci_save_state(dev->pdev); |
| 1981 | if (suspend) { |
| 1982 | /* Shut down the device */ |
| 1983 | pci_disable_device(dev->pdev); |
| 1984 | pci_set_power_state(dev->pdev, PCI_D3hot); |
jimqu | 74b0b15 | 2016-09-07 17:09:12 +0800 | [diff] [blame] | 1985 | } else { |
| 1986 | r = amdgpu_asic_reset(adev); |
| 1987 | if (r) |
| 1988 | DRM_ERROR("amdgpu asic reset failed\n"); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | if (fbcon) { |
| 1992 | console_lock(); |
| 1993 | amdgpu_fbdev_set_suspend(adev, 1); |
| 1994 | console_unlock(); |
| 1995 | } |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | /** |
Alex Deucher | 810ddc3 | 2016-08-23 13:25:49 -0400 | [diff] [blame] | 2000 | * amdgpu_device_resume - initiate device resume |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2001 | * |
| 2002 | * @pdev: drm dev pointer |
| 2003 | * |
| 2004 | * Bring the hw back to operating state (all asics). |
| 2005 | * Returns 0 for success or an error on failure. |
| 2006 | * Called at driver resume. |
| 2007 | */ |
Alex Deucher | 810ddc3 | 2016-08-23 13:25:49 -0400 | [diff] [blame] | 2008 | int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2009 | { |
| 2010 | struct drm_connector *connector; |
| 2011 | struct amdgpu_device *adev = dev->dev_private; |
Alex Deucher | 756e688 | 2015-10-08 00:03:36 -0400 | [diff] [blame] | 2012 | struct drm_crtc *crtc; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2013 | int r; |
| 2014 | |
| 2015 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) |
| 2016 | return 0; |
| 2017 | |
jimqu | 74b0b15 | 2016-09-07 17:09:12 +0800 | [diff] [blame] | 2018 | if (fbcon) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2019 | console_lock(); |
jimqu | 74b0b15 | 2016-09-07 17:09:12 +0800 | [diff] [blame] | 2020 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2021 | if (resume) { |
| 2022 | pci_set_power_state(dev->pdev, PCI_D0); |
| 2023 | pci_restore_state(dev->pdev); |
jimqu | 74b0b15 | 2016-09-07 17:09:12 +0800 | [diff] [blame] | 2024 | r = pci_enable_device(dev->pdev); |
| 2025 | if (r) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2026 | if (fbcon) |
| 2027 | console_unlock(); |
jimqu | 74b0b15 | 2016-09-07 17:09:12 +0800 | [diff] [blame] | 2028 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | /* post card */ |
jimqu | 74b0b15 | 2016-09-07 17:09:12 +0800 | [diff] [blame] | 2033 | if (!amdgpu_card_posted(adev) || !resume) { |
| 2034 | r = amdgpu_atom_asic_init(adev->mode_info.atom_context); |
| 2035 | if (r) |
| 2036 | DRM_ERROR("amdgpu asic init failed\n"); |
| 2037 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2038 | |
| 2039 | r = amdgpu_resume(adev); |
Flora Cui | ca19852 | 2016-02-04 15:10:08 +0800 | [diff] [blame] | 2040 | if (r) |
| 2041 | DRM_ERROR("amdgpu_resume failed (%d).\n", r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2042 | |
Alex Deucher | 5ceb54c | 2015-08-05 12:41:48 -0400 | [diff] [blame] | 2043 | amdgpu_fence_driver_resume(adev); |
| 2044 | |
Flora Cui | ca19852 | 2016-02-04 15:10:08 +0800 | [diff] [blame] | 2045 | if (resume) { |
| 2046 | r = amdgpu_ib_ring_tests(adev); |
| 2047 | if (r) |
| 2048 | DRM_ERROR("ib ring test failed (%d).\n", r); |
| 2049 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2050 | |
| 2051 | r = amdgpu_late_init(adev); |
| 2052 | if (r) |
| 2053 | return r; |
| 2054 | |
Alex Deucher | 756e688 | 2015-10-08 00:03:36 -0400 | [diff] [blame] | 2055 | /* pin cursors */ |
| 2056 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 2057 | struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); |
| 2058 | |
| 2059 | if (amdgpu_crtc->cursor_bo) { |
| 2060 | struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo); |
| 2061 | r = amdgpu_bo_reserve(aobj, false); |
| 2062 | if (r == 0) { |
| 2063 | r = amdgpu_bo_pin(aobj, |
| 2064 | AMDGPU_GEM_DOMAIN_VRAM, |
| 2065 | &amdgpu_crtc->cursor_addr); |
| 2066 | if (r != 0) |
| 2067 | DRM_ERROR("Failed to pin cursor BO (%d)\n", r); |
| 2068 | amdgpu_bo_unreserve(aobj); |
| 2069 | } |
| 2070 | } |
| 2071 | } |
| 2072 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2073 | /* blat the mode back in */ |
| 2074 | if (fbcon) { |
| 2075 | drm_helper_resume_force_mode(dev); |
| 2076 | /* turn on display hw */ |
Alex Deucher | 4c7fbc3 | 2015-09-23 14:32:06 -0400 | [diff] [blame] | 2077 | drm_modeset_lock_all(dev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2078 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 2079 | drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON); |
| 2080 | } |
Alex Deucher | 4c7fbc3 | 2015-09-23 14:32:06 -0400 | [diff] [blame] | 2081 | drm_modeset_unlock_all(dev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | drm_kms_helper_poll_enable(dev); |
Lyude | 23a1a9e | 2016-07-18 11:41:37 -0400 | [diff] [blame] | 2085 | |
| 2086 | /* |
| 2087 | * Most of the connector probing functions try to acquire runtime pm |
| 2088 | * refs to ensure that the GPU is powered on when connector polling is |
| 2089 | * performed. Since we're calling this from a runtime PM callback, |
| 2090 | * trying to acquire rpm refs will cause us to deadlock. |
| 2091 | * |
| 2092 | * Since we're guaranteed to be holding the rpm lock, it's safe to |
| 2093 | * temporarily disable the rpm helpers so this doesn't deadlock us. |
| 2094 | */ |
| 2095 | #ifdef CONFIG_PM |
| 2096 | dev->dev->power.disable_depth++; |
| 2097 | #endif |
Alex Deucher | 54fb2a5 | 2015-11-24 14:30:56 -0500 | [diff] [blame] | 2098 | drm_helper_hpd_irq_event(dev); |
Lyude | 23a1a9e | 2016-07-18 11:41:37 -0400 | [diff] [blame] | 2099 | #ifdef CONFIG_PM |
| 2100 | dev->dev->power.disable_depth--; |
| 2101 | #endif |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2102 | |
| 2103 | if (fbcon) { |
| 2104 | amdgpu_fbdev_set_suspend(adev, 0); |
| 2105 | console_unlock(); |
| 2106 | } |
| 2107 | |
| 2108 | return 0; |
| 2109 | } |
| 2110 | |
Chunming Zhou | 63fbf42 | 2016-07-15 11:19:20 +0800 | [diff] [blame] | 2111 | static bool amdgpu_check_soft_reset(struct amdgpu_device *adev) |
| 2112 | { |
| 2113 | int i; |
| 2114 | bool asic_hang = false; |
| 2115 | |
| 2116 | for (i = 0; i < adev->num_ip_blocks; i++) { |
| 2117 | if (!adev->ip_block_status[i].valid) |
| 2118 | continue; |
| 2119 | if (adev->ip_blocks[i].funcs->check_soft_reset) |
Alex Deucher | da146d3 | 2016-10-13 16:07:03 -0400 | [diff] [blame] | 2120 | adev->ip_block_status[i].hang = |
| 2121 | adev->ip_blocks[i].funcs->check_soft_reset(adev); |
Chunming Zhou | 63fbf42 | 2016-07-15 11:19:20 +0800 | [diff] [blame] | 2122 | if (adev->ip_block_status[i].hang) { |
| 2123 | DRM_INFO("IP block:%d is hang!\n", i); |
| 2124 | asic_hang = true; |
| 2125 | } |
| 2126 | } |
| 2127 | return asic_hang; |
| 2128 | } |
| 2129 | |
Baoyou Xie | 4d44665 | 2016-09-18 22:09:35 +0800 | [diff] [blame] | 2130 | static int amdgpu_pre_soft_reset(struct amdgpu_device *adev) |
Chunming Zhou | d31a501 | 2016-07-18 10:04:34 +0800 | [diff] [blame] | 2131 | { |
| 2132 | int i, r = 0; |
| 2133 | |
| 2134 | for (i = 0; i < adev->num_ip_blocks; i++) { |
| 2135 | if (!adev->ip_block_status[i].valid) |
| 2136 | continue; |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2137 | if (adev->ip_block_status[i].hang && |
| 2138 | adev->ip_blocks[i].funcs->pre_soft_reset) { |
Chunming Zhou | d31a501 | 2016-07-18 10:04:34 +0800 | [diff] [blame] | 2139 | r = adev->ip_blocks[i].funcs->pre_soft_reset(adev); |
| 2140 | if (r) |
| 2141 | return r; |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | return 0; |
| 2146 | } |
| 2147 | |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2148 | static bool amdgpu_need_full_reset(struct amdgpu_device *adev) |
| 2149 | { |
Alex Deucher | da146d3 | 2016-10-13 16:07:03 -0400 | [diff] [blame] | 2150 | int i; |
| 2151 | |
| 2152 | for (i = 0; i < adev->num_ip_blocks; i++) { |
| 2153 | if (!adev->ip_block_status[i].valid) |
| 2154 | continue; |
| 2155 | if ((adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) || |
| 2156 | (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_SMC) || |
| 2157 | (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_ACP) || |
| 2158 | (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_DCE)) { |
| 2159 | if (adev->ip_block_status[i].hang) { |
| 2160 | DRM_INFO("Some block need full reset!\n"); |
| 2161 | return true; |
| 2162 | } |
| 2163 | } |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2164 | } |
| 2165 | return false; |
| 2166 | } |
| 2167 | |
| 2168 | static int amdgpu_soft_reset(struct amdgpu_device *adev) |
| 2169 | { |
| 2170 | int i, r = 0; |
| 2171 | |
| 2172 | for (i = 0; i < adev->num_ip_blocks; i++) { |
| 2173 | if (!adev->ip_block_status[i].valid) |
| 2174 | continue; |
| 2175 | if (adev->ip_block_status[i].hang && |
| 2176 | adev->ip_blocks[i].funcs->soft_reset) { |
| 2177 | r = adev->ip_blocks[i].funcs->soft_reset(adev); |
| 2178 | if (r) |
| 2179 | return r; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | static int amdgpu_post_soft_reset(struct amdgpu_device *adev) |
| 2187 | { |
| 2188 | int i, r = 0; |
| 2189 | |
| 2190 | for (i = 0; i < adev->num_ip_blocks; i++) { |
| 2191 | if (!adev->ip_block_status[i].valid) |
| 2192 | continue; |
| 2193 | if (adev->ip_block_status[i].hang && |
| 2194 | adev->ip_blocks[i].funcs->post_soft_reset) |
| 2195 | r = adev->ip_blocks[i].funcs->post_soft_reset(adev); |
| 2196 | if (r) |
| 2197 | return r; |
| 2198 | } |
| 2199 | |
| 2200 | return 0; |
| 2201 | } |
| 2202 | |
Chunming Zhou | 3ad81f1 | 2016-08-05 17:30:17 +0800 | [diff] [blame] | 2203 | bool amdgpu_need_backup(struct amdgpu_device *adev) |
| 2204 | { |
| 2205 | if (adev->flags & AMD_IS_APU) |
| 2206 | return false; |
| 2207 | |
| 2208 | return amdgpu_lockup_timeout > 0 ? true : false; |
| 2209 | } |
| 2210 | |
Chunming Zhou | 53cdccd | 2016-07-21 17:20:52 +0800 | [diff] [blame] | 2211 | static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev, |
| 2212 | struct amdgpu_ring *ring, |
| 2213 | struct amdgpu_bo *bo, |
| 2214 | struct fence **fence) |
| 2215 | { |
| 2216 | uint32_t domain; |
| 2217 | int r; |
| 2218 | |
| 2219 | if (!bo->shadow) |
| 2220 | return 0; |
| 2221 | |
| 2222 | r = amdgpu_bo_reserve(bo, false); |
| 2223 | if (r) |
| 2224 | return r; |
| 2225 | domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); |
| 2226 | /* if bo has been evicted, then no need to recover */ |
| 2227 | if (domain == AMDGPU_GEM_DOMAIN_VRAM) { |
| 2228 | r = amdgpu_bo_restore_from_shadow(adev, ring, bo, |
| 2229 | NULL, fence, true); |
| 2230 | if (r) { |
| 2231 | DRM_ERROR("recover page table failed!\n"); |
| 2232 | goto err; |
| 2233 | } |
| 2234 | } |
| 2235 | err: |
| 2236 | amdgpu_bo_unreserve(bo); |
| 2237 | return r; |
| 2238 | } |
| 2239 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2240 | /** |
| 2241 | * amdgpu_gpu_reset - reset the asic |
| 2242 | * |
| 2243 | * @adev: amdgpu device pointer |
| 2244 | * |
| 2245 | * Attempt the reset the GPU if it has hung (all asics). |
| 2246 | * Returns 0 for success or an error on failure. |
| 2247 | */ |
| 2248 | int amdgpu_gpu_reset(struct amdgpu_device *adev) |
| 2249 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2250 | int i, r; |
| 2251 | int resched; |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2252 | bool need_full_reset; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2253 | |
Chunming Zhou | 63fbf42 | 2016-07-15 11:19:20 +0800 | [diff] [blame] | 2254 | if (!amdgpu_check_soft_reset(adev)) { |
| 2255 | DRM_INFO("No hardware hang detected. Did some blocks stall?\n"); |
| 2256 | return 0; |
| 2257 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2258 | |
Marek Olšák | d94aed5 | 2015-05-05 21:13:49 +0200 | [diff] [blame] | 2259 | atomic_inc(&adev->gpu_reset_counter); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2260 | |
Chunming Zhou | a3c47d6 | 2016-06-30 16:44:41 +0800 | [diff] [blame] | 2261 | /* block TTM */ |
| 2262 | resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev); |
| 2263 | |
Chunming Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 2264 | /* block scheduler */ |
| 2265 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
| 2266 | struct amdgpu_ring *ring = adev->rings[i]; |
| 2267 | |
| 2268 | if (!ring) |
| 2269 | continue; |
| 2270 | kthread_park(ring->sched.thread); |
Chunming Zhou | aa1c890 | 2016-06-30 13:56:02 +0800 | [diff] [blame] | 2271 | amd_sched_hw_job_reset(&ring->sched); |
Chunming Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 2272 | } |
Chunming Zhou | 2200eda | 2016-06-30 16:53:02 +0800 | [diff] [blame] | 2273 | /* after all hw jobs are reset, hw fence is meaningless, so force_completion */ |
| 2274 | amdgpu_fence_driver_force_completion(adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2275 | |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2276 | need_full_reset = amdgpu_need_full_reset(adev); |
| 2277 | |
| 2278 | if (!need_full_reset) { |
| 2279 | amdgpu_pre_soft_reset(adev); |
| 2280 | r = amdgpu_soft_reset(adev); |
| 2281 | amdgpu_post_soft_reset(adev); |
| 2282 | if (r || amdgpu_check_soft_reset(adev)) { |
| 2283 | DRM_INFO("soft reset failed, will fallback to full reset!\n"); |
| 2284 | need_full_reset = true; |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | if (need_full_reset) { |
| 2289 | /* save scratch */ |
| 2290 | amdgpu_atombios_scratch_regs_save(adev); |
| 2291 | r = amdgpu_suspend(adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2292 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2293 | retry: |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2294 | /* Disable fb access */ |
| 2295 | if (adev->mode_info.num_crtc) { |
| 2296 | struct amdgpu_mode_mc_save save; |
| 2297 | amdgpu_display_stop_mc_access(adev, &save); |
| 2298 | amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC); |
| 2299 | } |
Chunming Zhou | f1aa7e0 | 2016-06-28 10:38:50 +0800 | [diff] [blame] | 2300 | |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2301 | r = amdgpu_asic_reset(adev); |
| 2302 | /* post card */ |
| 2303 | amdgpu_atom_asic_init(adev->mode_info.atom_context); |
Alex Deucher | bfa9926 | 2016-01-15 11:59:48 -0500 | [diff] [blame] | 2304 | |
Chunming Zhou | 35d782f | 2016-07-15 15:57:13 +0800 | [diff] [blame] | 2305 | if (!r) { |
| 2306 | dev_info(adev->dev, "GPU reset succeeded, trying to resume\n"); |
| 2307 | r = amdgpu_resume(adev); |
| 2308 | } |
| 2309 | /* restore scratch */ |
| 2310 | amdgpu_atombios_scratch_regs_restore(adev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2311 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2312 | if (!r) { |
Chunming Zhou | e72cfd5 | 2016-07-27 13:15:20 +0800 | [diff] [blame] | 2313 | amdgpu_irq_gpu_reset_resume_helper(adev); |
Chunming Zhou | 2c0d731 | 2016-08-30 16:36:25 +0800 | [diff] [blame] | 2314 | if (need_full_reset && amdgpu_need_backup(adev)) { |
| 2315 | r = amdgpu_ttm_recover_gart(adev); |
| 2316 | if (r) |
| 2317 | DRM_ERROR("gart recovery failed!!!\n"); |
| 2318 | } |
Chunming Zhou | 1f46508 | 2016-06-30 15:02:26 +0800 | [diff] [blame] | 2319 | r = amdgpu_ib_ring_tests(adev); |
| 2320 | if (r) { |
| 2321 | dev_err(adev->dev, "ib ring test failed (%d).\n", r); |
Chunming Zhou | 40019dc | 2016-06-29 16:01:49 +0800 | [diff] [blame] | 2322 | r = amdgpu_suspend(adev); |
Chunming Zhou | 53cdccd | 2016-07-21 17:20:52 +0800 | [diff] [blame] | 2323 | need_full_reset = true; |
Chunming Zhou | 40019dc | 2016-06-29 16:01:49 +0800 | [diff] [blame] | 2324 | goto retry; |
Chunming Zhou | 1f46508 | 2016-06-30 15:02:26 +0800 | [diff] [blame] | 2325 | } |
Chunming Zhou | 53cdccd | 2016-07-21 17:20:52 +0800 | [diff] [blame] | 2326 | /** |
| 2327 | * recovery vm page tables, since we cannot depend on VRAM is |
| 2328 | * consistent after gpu full reset. |
| 2329 | */ |
| 2330 | if (need_full_reset && amdgpu_need_backup(adev)) { |
| 2331 | struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; |
| 2332 | struct amdgpu_bo *bo, *tmp; |
| 2333 | struct fence *fence = NULL, *next = NULL; |
Chunming Zhou | 1f46508 | 2016-06-30 15:02:26 +0800 | [diff] [blame] | 2334 | |
Chunming Zhou | 53cdccd | 2016-07-21 17:20:52 +0800 | [diff] [blame] | 2335 | DRM_INFO("recover vram bo from shadow\n"); |
| 2336 | mutex_lock(&adev->shadow_list_lock); |
| 2337 | list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) { |
| 2338 | amdgpu_recover_vram_from_shadow(adev, ring, bo, &next); |
| 2339 | if (fence) { |
| 2340 | r = fence_wait(fence, false); |
| 2341 | if (r) { |
| 2342 | WARN(r, "recovery from shadow isn't comleted\n"); |
| 2343 | break; |
| 2344 | } |
| 2345 | } |
| 2346 | |
| 2347 | fence_put(fence); |
| 2348 | fence = next; |
| 2349 | } |
| 2350 | mutex_unlock(&adev->shadow_list_lock); |
| 2351 | if (fence) { |
| 2352 | r = fence_wait(fence, false); |
| 2353 | if (r) |
| 2354 | WARN(r, "recovery from shadow isn't comleted\n"); |
| 2355 | } |
| 2356 | fence_put(fence); |
| 2357 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2358 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
| 2359 | struct amdgpu_ring *ring = adev->rings[i]; |
| 2360 | if (!ring) |
| 2361 | continue; |
Chunming Zhou | 53cdccd | 2016-07-21 17:20:52 +0800 | [diff] [blame] | 2362 | |
Chunming Zhou | aa1c890 | 2016-06-30 13:56:02 +0800 | [diff] [blame] | 2363 | amd_sched_job_recovery(&ring->sched); |
Chunming Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 2364 | kthread_unpark(ring->sched.thread); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2365 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2366 | } else { |
Chunming Zhou | 2200eda | 2016-06-30 16:53:02 +0800 | [diff] [blame] | 2367 | dev_err(adev->dev, "asic resume failed (%d).\n", r); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2368 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
Chunming Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 2369 | if (adev->rings[i]) { |
| 2370 | kthread_unpark(adev->rings[i]->sched.thread); |
Chunming Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 2371 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | drm_helper_resume_force_mode(adev->ddev); |
| 2376 | |
| 2377 | ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched); |
| 2378 | if (r) { |
| 2379 | /* bad news, how to tell it to userspace ? */ |
| 2380 | dev_info(adev->dev, "GPU reset failed\n"); |
| 2381 | } |
| 2382 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2383 | return r; |
| 2384 | } |
| 2385 | |
Alex Deucher | d0dd7f0 | 2015-11-11 19:45:06 -0500 | [diff] [blame] | 2386 | void amdgpu_get_pcie_info(struct amdgpu_device *adev) |
| 2387 | { |
| 2388 | u32 mask; |
| 2389 | int ret; |
| 2390 | |
Alex Deucher | cd474ba | 2016-02-04 10:21:23 -0500 | [diff] [blame] | 2391 | if (amdgpu_pcie_gen_cap) |
| 2392 | adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap; |
| 2393 | |
| 2394 | if (amdgpu_pcie_lane_cap) |
| 2395 | adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap; |
| 2396 | |
| 2397 | /* covers APUs as well */ |
| 2398 | if (pci_is_root_bus(adev->pdev->bus)) { |
| 2399 | if (adev->pm.pcie_gen_mask == 0) |
| 2400 | adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK; |
| 2401 | if (adev->pm.pcie_mlw_mask == 0) |
| 2402 | adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK; |
Alex Deucher | d0dd7f0 | 2015-11-11 19:45:06 -0500 | [diff] [blame] | 2403 | return; |
Alex Deucher | d0dd7f0 | 2015-11-11 19:45:06 -0500 | [diff] [blame] | 2404 | } |
Alex Deucher | cd474ba | 2016-02-04 10:21:23 -0500 | [diff] [blame] | 2405 | |
| 2406 | if (adev->pm.pcie_gen_mask == 0) { |
| 2407 | ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask); |
| 2408 | if (!ret) { |
| 2409 | adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | |
| 2410 | CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | |
| 2411 | CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3); |
| 2412 | |
| 2413 | if (mask & DRM_PCIE_SPEED_25) |
| 2414 | adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1; |
| 2415 | if (mask & DRM_PCIE_SPEED_50) |
| 2416 | adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2; |
| 2417 | if (mask & DRM_PCIE_SPEED_80) |
| 2418 | adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3; |
| 2419 | } else { |
| 2420 | adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK; |
| 2421 | } |
| 2422 | } |
| 2423 | if (adev->pm.pcie_mlw_mask == 0) { |
| 2424 | ret = drm_pcie_get_max_link_width(adev->ddev, &mask); |
| 2425 | if (!ret) { |
| 2426 | switch (mask) { |
| 2427 | case 32: |
| 2428 | adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 | |
| 2429 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | |
| 2430 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | |
| 2431 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | |
| 2432 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | |
| 2433 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | |
| 2434 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); |
| 2435 | break; |
| 2436 | case 16: |
| 2437 | adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | |
| 2438 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | |
| 2439 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | |
| 2440 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | |
| 2441 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | |
| 2442 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); |
| 2443 | break; |
| 2444 | case 12: |
| 2445 | adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | |
| 2446 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | |
| 2447 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | |
| 2448 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | |
| 2449 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); |
| 2450 | break; |
| 2451 | case 8: |
| 2452 | adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | |
| 2453 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | |
| 2454 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | |
| 2455 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); |
| 2456 | break; |
| 2457 | case 4: |
| 2458 | adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | |
| 2459 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | |
| 2460 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); |
| 2461 | break; |
| 2462 | case 2: |
| 2463 | adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | |
| 2464 | CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); |
| 2465 | break; |
| 2466 | case 1: |
| 2467 | adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1; |
| 2468 | break; |
| 2469 | default: |
| 2470 | break; |
| 2471 | } |
| 2472 | } else { |
| 2473 | adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK; |
Alex Deucher | d0dd7f0 | 2015-11-11 19:45:06 -0500 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2477 | |
| 2478 | /* |
| 2479 | * Debugfs |
| 2480 | */ |
| 2481 | int amdgpu_debugfs_add_files(struct amdgpu_device *adev, |
Nils Wallménius | 06ab683 | 2016-05-02 12:46:15 -0400 | [diff] [blame] | 2482 | const struct drm_info_list *files, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2483 | unsigned nfiles) |
| 2484 | { |
| 2485 | unsigned i; |
| 2486 | |
| 2487 | for (i = 0; i < adev->debugfs_count; i++) { |
| 2488 | if (adev->debugfs[i].files == files) { |
| 2489 | /* Already registered */ |
| 2490 | return 0; |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | i = adev->debugfs_count + 1; |
| 2495 | if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) { |
| 2496 | DRM_ERROR("Reached maximum number of debugfs components.\n"); |
| 2497 | DRM_ERROR("Report so we increase " |
| 2498 | "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n"); |
| 2499 | return -EINVAL; |
| 2500 | } |
| 2501 | adev->debugfs[adev->debugfs_count].files = files; |
| 2502 | adev->debugfs[adev->debugfs_count].num_files = nfiles; |
| 2503 | adev->debugfs_count = i; |
| 2504 | #if defined(CONFIG_DEBUG_FS) |
| 2505 | drm_debugfs_create_files(files, nfiles, |
| 2506 | adev->ddev->control->debugfs_root, |
| 2507 | adev->ddev->control); |
| 2508 | drm_debugfs_create_files(files, nfiles, |
| 2509 | adev->ddev->primary->debugfs_root, |
| 2510 | adev->ddev->primary); |
| 2511 | #endif |
| 2512 | return 0; |
| 2513 | } |
| 2514 | |
| 2515 | static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev) |
| 2516 | { |
| 2517 | #if defined(CONFIG_DEBUG_FS) |
| 2518 | unsigned i; |
| 2519 | |
| 2520 | for (i = 0; i < adev->debugfs_count; i++) { |
| 2521 | drm_debugfs_remove_files(adev->debugfs[i].files, |
| 2522 | adev->debugfs[i].num_files, |
| 2523 | adev->ddev->control); |
| 2524 | drm_debugfs_remove_files(adev->debugfs[i].files, |
| 2525 | adev->debugfs[i].num_files, |
| 2526 | adev->ddev->primary); |
| 2527 | } |
| 2528 | #endif |
| 2529 | } |
| 2530 | |
| 2531 | #if defined(CONFIG_DEBUG_FS) |
| 2532 | |
| 2533 | static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf, |
| 2534 | size_t size, loff_t *pos) |
| 2535 | { |
| 2536 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2537 | ssize_t result = 0; |
| 2538 | int r; |
Tom St Denis | bd12267 | 2016-07-28 09:39:22 -0400 | [diff] [blame] | 2539 | bool pm_pg_lock, use_bank; |
Tom St Denis | 56628159 | 2016-06-27 11:55:07 -0400 | [diff] [blame] | 2540 | unsigned instance_bank, sh_bank, se_bank; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2541 | |
| 2542 | if (size & 0x3 || *pos & 0x3) |
| 2543 | return -EINVAL; |
| 2544 | |
Tom St Denis | bd12267 | 2016-07-28 09:39:22 -0400 | [diff] [blame] | 2545 | /* are we reading registers for which a PG lock is necessary? */ |
| 2546 | pm_pg_lock = (*pos >> 23) & 1; |
| 2547 | |
Tom St Denis | 56628159 | 2016-06-27 11:55:07 -0400 | [diff] [blame] | 2548 | if (*pos & (1ULL << 62)) { |
| 2549 | se_bank = (*pos >> 24) & 0x3FF; |
| 2550 | sh_bank = (*pos >> 34) & 0x3FF; |
| 2551 | instance_bank = (*pos >> 44) & 0x3FF; |
| 2552 | use_bank = 1; |
Tom St Denis | 56628159 | 2016-06-27 11:55:07 -0400 | [diff] [blame] | 2553 | } else { |
| 2554 | use_bank = 0; |
| 2555 | } |
| 2556 | |
Tom St Denis | bd12267 | 2016-07-28 09:39:22 -0400 | [diff] [blame] | 2557 | *pos &= 0x3FFFF; |
| 2558 | |
Tom St Denis | 56628159 | 2016-06-27 11:55:07 -0400 | [diff] [blame] | 2559 | if (use_bank) { |
| 2560 | if (sh_bank >= adev->gfx.config.max_sh_per_se || |
| 2561 | se_bank >= adev->gfx.config.max_shader_engines) |
| 2562 | return -EINVAL; |
| 2563 | mutex_lock(&adev->grbm_idx_mutex); |
| 2564 | amdgpu_gfx_select_se_sh(adev, se_bank, |
| 2565 | sh_bank, instance_bank); |
| 2566 | } |
| 2567 | |
Tom St Denis | bd12267 | 2016-07-28 09:39:22 -0400 | [diff] [blame] | 2568 | if (pm_pg_lock) |
| 2569 | mutex_lock(&adev->pm.mutex); |
| 2570 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2571 | while (size) { |
| 2572 | uint32_t value; |
| 2573 | |
| 2574 | if (*pos > adev->rmmio_size) |
Tom St Denis | 56628159 | 2016-06-27 11:55:07 -0400 | [diff] [blame] | 2575 | goto end; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2576 | |
| 2577 | value = RREG32(*pos >> 2); |
| 2578 | r = put_user(value, (uint32_t *)buf); |
Tom St Denis | 56628159 | 2016-06-27 11:55:07 -0400 | [diff] [blame] | 2579 | if (r) { |
| 2580 | result = r; |
| 2581 | goto end; |
| 2582 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2583 | |
| 2584 | result += 4; |
| 2585 | buf += 4; |
| 2586 | *pos += 4; |
| 2587 | size -= 4; |
| 2588 | } |
| 2589 | |
Tom St Denis | 56628159 | 2016-06-27 11:55:07 -0400 | [diff] [blame] | 2590 | end: |
| 2591 | if (use_bank) { |
| 2592 | amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff); |
| 2593 | mutex_unlock(&adev->grbm_idx_mutex); |
| 2594 | } |
| 2595 | |
Tom St Denis | bd12267 | 2016-07-28 09:39:22 -0400 | [diff] [blame] | 2596 | if (pm_pg_lock) |
| 2597 | mutex_unlock(&adev->pm.mutex); |
| 2598 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2599 | return result; |
| 2600 | } |
| 2601 | |
| 2602 | static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf, |
| 2603 | size_t size, loff_t *pos) |
| 2604 | { |
| 2605 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2606 | ssize_t result = 0; |
| 2607 | int r; |
| 2608 | |
| 2609 | if (size & 0x3 || *pos & 0x3) |
| 2610 | return -EINVAL; |
| 2611 | |
| 2612 | while (size) { |
| 2613 | uint32_t value; |
| 2614 | |
| 2615 | if (*pos > adev->rmmio_size) |
| 2616 | return result; |
| 2617 | |
| 2618 | r = get_user(value, (uint32_t *)buf); |
| 2619 | if (r) |
| 2620 | return r; |
| 2621 | |
| 2622 | WREG32(*pos >> 2, value); |
| 2623 | |
| 2624 | result += 4; |
| 2625 | buf += 4; |
| 2626 | *pos += 4; |
| 2627 | size -= 4; |
| 2628 | } |
| 2629 | |
| 2630 | return result; |
| 2631 | } |
| 2632 | |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2633 | static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf, |
| 2634 | size_t size, loff_t *pos) |
| 2635 | { |
| 2636 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2637 | ssize_t result = 0; |
| 2638 | int r; |
| 2639 | |
| 2640 | if (size & 0x3 || *pos & 0x3) |
| 2641 | return -EINVAL; |
| 2642 | |
| 2643 | while (size) { |
| 2644 | uint32_t value; |
| 2645 | |
| 2646 | value = RREG32_PCIE(*pos >> 2); |
| 2647 | r = put_user(value, (uint32_t *)buf); |
| 2648 | if (r) |
| 2649 | return r; |
| 2650 | |
| 2651 | result += 4; |
| 2652 | buf += 4; |
| 2653 | *pos += 4; |
| 2654 | size -= 4; |
| 2655 | } |
| 2656 | |
| 2657 | return result; |
| 2658 | } |
| 2659 | |
| 2660 | static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf, |
| 2661 | size_t size, loff_t *pos) |
| 2662 | { |
| 2663 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2664 | ssize_t result = 0; |
| 2665 | int r; |
| 2666 | |
| 2667 | if (size & 0x3 || *pos & 0x3) |
| 2668 | return -EINVAL; |
| 2669 | |
| 2670 | while (size) { |
| 2671 | uint32_t value; |
| 2672 | |
| 2673 | r = get_user(value, (uint32_t *)buf); |
| 2674 | if (r) |
| 2675 | return r; |
| 2676 | |
| 2677 | WREG32_PCIE(*pos >> 2, value); |
| 2678 | |
| 2679 | result += 4; |
| 2680 | buf += 4; |
| 2681 | *pos += 4; |
| 2682 | size -= 4; |
| 2683 | } |
| 2684 | |
| 2685 | return result; |
| 2686 | } |
| 2687 | |
| 2688 | static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf, |
| 2689 | size_t size, loff_t *pos) |
| 2690 | { |
| 2691 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2692 | ssize_t result = 0; |
| 2693 | int r; |
| 2694 | |
| 2695 | if (size & 0x3 || *pos & 0x3) |
| 2696 | return -EINVAL; |
| 2697 | |
| 2698 | while (size) { |
| 2699 | uint32_t value; |
| 2700 | |
| 2701 | value = RREG32_DIDT(*pos >> 2); |
| 2702 | r = put_user(value, (uint32_t *)buf); |
| 2703 | if (r) |
| 2704 | return r; |
| 2705 | |
| 2706 | result += 4; |
| 2707 | buf += 4; |
| 2708 | *pos += 4; |
| 2709 | size -= 4; |
| 2710 | } |
| 2711 | |
| 2712 | return result; |
| 2713 | } |
| 2714 | |
| 2715 | static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf, |
| 2716 | size_t size, loff_t *pos) |
| 2717 | { |
| 2718 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2719 | ssize_t result = 0; |
| 2720 | int r; |
| 2721 | |
| 2722 | if (size & 0x3 || *pos & 0x3) |
| 2723 | return -EINVAL; |
| 2724 | |
| 2725 | while (size) { |
| 2726 | uint32_t value; |
| 2727 | |
| 2728 | r = get_user(value, (uint32_t *)buf); |
| 2729 | if (r) |
| 2730 | return r; |
| 2731 | |
| 2732 | WREG32_DIDT(*pos >> 2, value); |
| 2733 | |
| 2734 | result += 4; |
| 2735 | buf += 4; |
| 2736 | *pos += 4; |
| 2737 | size -= 4; |
| 2738 | } |
| 2739 | |
| 2740 | return result; |
| 2741 | } |
| 2742 | |
| 2743 | static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf, |
| 2744 | size_t size, loff_t *pos) |
| 2745 | { |
| 2746 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2747 | ssize_t result = 0; |
| 2748 | int r; |
| 2749 | |
| 2750 | if (size & 0x3 || *pos & 0x3) |
| 2751 | return -EINVAL; |
| 2752 | |
| 2753 | while (size) { |
| 2754 | uint32_t value; |
| 2755 | |
Tom St Denis | 6fc0dea | 2016-08-29 08:39:29 -0400 | [diff] [blame] | 2756 | value = RREG32_SMC(*pos); |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2757 | r = put_user(value, (uint32_t *)buf); |
| 2758 | if (r) |
| 2759 | return r; |
| 2760 | |
| 2761 | result += 4; |
| 2762 | buf += 4; |
| 2763 | *pos += 4; |
| 2764 | size -= 4; |
| 2765 | } |
| 2766 | |
| 2767 | return result; |
| 2768 | } |
| 2769 | |
| 2770 | static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf, |
| 2771 | size_t size, loff_t *pos) |
| 2772 | { |
| 2773 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2774 | ssize_t result = 0; |
| 2775 | int r; |
| 2776 | |
| 2777 | if (size & 0x3 || *pos & 0x3) |
| 2778 | return -EINVAL; |
| 2779 | |
| 2780 | while (size) { |
| 2781 | uint32_t value; |
| 2782 | |
| 2783 | r = get_user(value, (uint32_t *)buf); |
| 2784 | if (r) |
| 2785 | return r; |
| 2786 | |
Tom St Denis | 6fc0dea | 2016-08-29 08:39:29 -0400 | [diff] [blame] | 2787 | WREG32_SMC(*pos, value); |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2788 | |
| 2789 | result += 4; |
| 2790 | buf += 4; |
| 2791 | *pos += 4; |
| 2792 | size -= 4; |
| 2793 | } |
| 2794 | |
| 2795 | return result; |
| 2796 | } |
| 2797 | |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2798 | static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf, |
| 2799 | size_t size, loff_t *pos) |
| 2800 | { |
| 2801 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2802 | ssize_t result = 0; |
| 2803 | int r; |
| 2804 | uint32_t *config, no_regs = 0; |
| 2805 | |
| 2806 | if (size & 0x3 || *pos & 0x3) |
| 2807 | return -EINVAL; |
| 2808 | |
Markus Elfring | ecab766 | 2016-09-18 17:00:52 +0200 | [diff] [blame] | 2809 | config = kmalloc_array(256, sizeof(*config), GFP_KERNEL); |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2810 | if (!config) |
| 2811 | return -ENOMEM; |
| 2812 | |
| 2813 | /* version, increment each time something is added */ |
Tom St Denis | e9f11dc | 2016-08-17 12:00:51 -0400 | [diff] [blame] | 2814 | config[no_regs++] = 2; |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2815 | config[no_regs++] = adev->gfx.config.max_shader_engines; |
| 2816 | config[no_regs++] = adev->gfx.config.max_tile_pipes; |
| 2817 | config[no_regs++] = adev->gfx.config.max_cu_per_sh; |
| 2818 | config[no_regs++] = adev->gfx.config.max_sh_per_se; |
| 2819 | config[no_regs++] = adev->gfx.config.max_backends_per_se; |
| 2820 | config[no_regs++] = adev->gfx.config.max_texture_channel_caches; |
| 2821 | config[no_regs++] = adev->gfx.config.max_gprs; |
| 2822 | config[no_regs++] = adev->gfx.config.max_gs_threads; |
| 2823 | config[no_regs++] = adev->gfx.config.max_hw_contexts; |
| 2824 | config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend; |
| 2825 | config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend; |
| 2826 | config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size; |
| 2827 | config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size; |
| 2828 | config[no_regs++] = adev->gfx.config.num_tile_pipes; |
| 2829 | config[no_regs++] = adev->gfx.config.backend_enable_mask; |
| 2830 | config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes; |
| 2831 | config[no_regs++] = adev->gfx.config.mem_row_size_in_kb; |
| 2832 | config[no_regs++] = adev->gfx.config.shader_engine_tile_size; |
| 2833 | config[no_regs++] = adev->gfx.config.num_gpus; |
| 2834 | config[no_regs++] = adev->gfx.config.multi_gpu_tile_size; |
| 2835 | config[no_regs++] = adev->gfx.config.mc_arb_ramcfg; |
| 2836 | config[no_regs++] = adev->gfx.config.gb_addr_config; |
| 2837 | config[no_regs++] = adev->gfx.config.num_rbs; |
| 2838 | |
Tom St Denis | 89a8f30 | 2016-08-12 15:14:31 -0400 | [diff] [blame] | 2839 | /* rev==1 */ |
| 2840 | config[no_regs++] = adev->rev_id; |
| 2841 | config[no_regs++] = adev->pg_flags; |
| 2842 | config[no_regs++] = adev->cg_flags; |
| 2843 | |
Tom St Denis | e9f11dc | 2016-08-17 12:00:51 -0400 | [diff] [blame] | 2844 | /* rev==2 */ |
| 2845 | config[no_regs++] = adev->family; |
| 2846 | config[no_regs++] = adev->external_rev_id; |
| 2847 | |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2848 | while (size && (*pos < no_regs * 4)) { |
| 2849 | uint32_t value; |
| 2850 | |
| 2851 | value = config[*pos >> 2]; |
| 2852 | r = put_user(value, (uint32_t *)buf); |
| 2853 | if (r) { |
| 2854 | kfree(config); |
| 2855 | return r; |
| 2856 | } |
| 2857 | |
| 2858 | result += 4; |
| 2859 | buf += 4; |
| 2860 | *pos += 4; |
| 2861 | size -= 4; |
| 2862 | } |
| 2863 | |
| 2864 | kfree(config); |
| 2865 | return result; |
| 2866 | } |
| 2867 | |
Tom St Denis | f2cdaf2 | 2016-09-15 10:08:44 -0400 | [diff] [blame] | 2868 | static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf, |
| 2869 | size_t size, loff_t *pos) |
| 2870 | { |
| 2871 | struct amdgpu_device *adev = f->f_inode->i_private; |
| 2872 | int idx, r; |
| 2873 | int32_t value; |
| 2874 | |
| 2875 | if (size != 4 || *pos & 0x3) |
| 2876 | return -EINVAL; |
| 2877 | |
| 2878 | /* convert offset to sensor number */ |
| 2879 | idx = *pos >> 2; |
| 2880 | |
| 2881 | if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor) |
| 2882 | r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value); |
| 2883 | else |
| 2884 | return -EINVAL; |
| 2885 | |
| 2886 | if (!r) |
| 2887 | r = put_user(value, (int32_t *)buf); |
| 2888 | |
| 2889 | return !r ? 4 : r; |
| 2890 | } |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2891 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2892 | static const struct file_operations amdgpu_debugfs_regs_fops = { |
| 2893 | .owner = THIS_MODULE, |
| 2894 | .read = amdgpu_debugfs_regs_read, |
| 2895 | .write = amdgpu_debugfs_regs_write, |
| 2896 | .llseek = default_llseek |
| 2897 | }; |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2898 | static const struct file_operations amdgpu_debugfs_regs_didt_fops = { |
| 2899 | .owner = THIS_MODULE, |
| 2900 | .read = amdgpu_debugfs_regs_didt_read, |
| 2901 | .write = amdgpu_debugfs_regs_didt_write, |
| 2902 | .llseek = default_llseek |
| 2903 | }; |
| 2904 | static const struct file_operations amdgpu_debugfs_regs_pcie_fops = { |
| 2905 | .owner = THIS_MODULE, |
| 2906 | .read = amdgpu_debugfs_regs_pcie_read, |
| 2907 | .write = amdgpu_debugfs_regs_pcie_write, |
| 2908 | .llseek = default_llseek |
| 2909 | }; |
| 2910 | static const struct file_operations amdgpu_debugfs_regs_smc_fops = { |
| 2911 | .owner = THIS_MODULE, |
| 2912 | .read = amdgpu_debugfs_regs_smc_read, |
| 2913 | .write = amdgpu_debugfs_regs_smc_write, |
| 2914 | .llseek = default_llseek |
| 2915 | }; |
| 2916 | |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2917 | static const struct file_operations amdgpu_debugfs_gca_config_fops = { |
| 2918 | .owner = THIS_MODULE, |
| 2919 | .read = amdgpu_debugfs_gca_config_read, |
| 2920 | .llseek = default_llseek |
| 2921 | }; |
| 2922 | |
Tom St Denis | f2cdaf2 | 2016-09-15 10:08:44 -0400 | [diff] [blame] | 2923 | static const struct file_operations amdgpu_debugfs_sensors_fops = { |
| 2924 | .owner = THIS_MODULE, |
| 2925 | .read = amdgpu_debugfs_sensor_read, |
| 2926 | .llseek = default_llseek |
| 2927 | }; |
| 2928 | |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2929 | static const struct file_operations *debugfs_regs[] = { |
| 2930 | &amdgpu_debugfs_regs_fops, |
| 2931 | &amdgpu_debugfs_regs_didt_fops, |
| 2932 | &amdgpu_debugfs_regs_pcie_fops, |
| 2933 | &amdgpu_debugfs_regs_smc_fops, |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2934 | &amdgpu_debugfs_gca_config_fops, |
Tom St Denis | f2cdaf2 | 2016-09-15 10:08:44 -0400 | [diff] [blame] | 2935 | &amdgpu_debugfs_sensors_fops, |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2936 | }; |
| 2937 | |
| 2938 | static const char *debugfs_regs_names[] = { |
| 2939 | "amdgpu_regs", |
| 2940 | "amdgpu_regs_didt", |
| 2941 | "amdgpu_regs_pcie", |
| 2942 | "amdgpu_regs_smc", |
Tom St Denis | 1e05141 | 2016-06-27 09:57:18 -0400 | [diff] [blame] | 2943 | "amdgpu_gca_config", |
Tom St Denis | f2cdaf2 | 2016-09-15 10:08:44 -0400 | [diff] [blame] | 2944 | "amdgpu_sensors", |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2945 | }; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2946 | |
| 2947 | static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) |
| 2948 | { |
| 2949 | struct drm_minor *minor = adev->ddev->primary; |
| 2950 | struct dentry *ent, *root = minor->debugfs_root; |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2951 | unsigned i, j; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2952 | |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2953 | for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) { |
| 2954 | ent = debugfs_create_file(debugfs_regs_names[i], |
| 2955 | S_IFREG | S_IRUGO, root, |
| 2956 | adev, debugfs_regs[i]); |
| 2957 | if (IS_ERR(ent)) { |
| 2958 | for (j = 0; j < i; j++) { |
| 2959 | debugfs_remove(adev->debugfs_regs[i]); |
| 2960 | adev->debugfs_regs[i] = NULL; |
| 2961 | } |
| 2962 | return PTR_ERR(ent); |
| 2963 | } |
| 2964 | |
| 2965 | if (!i) |
| 2966 | i_size_write(ent->d_inode, adev->rmmio_size); |
| 2967 | adev->debugfs_regs[i] = ent; |
| 2968 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2969 | |
| 2970 | return 0; |
| 2971 | } |
| 2972 | |
| 2973 | static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) |
| 2974 | { |
Tom St Denis | adcec28 | 2016-04-15 13:08:44 -0400 | [diff] [blame] | 2975 | unsigned i; |
| 2976 | |
| 2977 | for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) { |
| 2978 | if (adev->debugfs_regs[i]) { |
| 2979 | debugfs_remove(adev->debugfs_regs[i]); |
| 2980 | adev->debugfs_regs[i] = NULL; |
| 2981 | } |
| 2982 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | int amdgpu_debugfs_init(struct drm_minor *minor) |
| 2986 | { |
| 2987 | return 0; |
| 2988 | } |
| 2989 | |
| 2990 | void amdgpu_debugfs_cleanup(struct drm_minor *minor) |
| 2991 | { |
| 2992 | } |
Alexander Kuleshov | 7cebc72 | 2015-06-27 13:16:05 +0600 | [diff] [blame] | 2993 | #else |
| 2994 | static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev) |
| 2995 | { |
| 2996 | return 0; |
| 2997 | } |
| 2998 | static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 2999 | #endif |