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 */ 28 29 #include "amdgpu.h" 30 #include <drm/drm_debugfs.h> 31 #include <drm/amdgpu_drm.h> 32 #include "amdgpu_sched.h" 33 #include "amdgpu_uvd.h" 34 #include "amdgpu_vce.h" 35 #include "atom.h" 36 37 #include <linux/vga_switcheroo.h> 38 #include <linux/slab.h> 39 #include <linux/uaccess.h> 40 #include <linux/pci.h> 41 #include <linux/pm_runtime.h> 42 #include "amdgpu_amdkfd.h" 43 #include "amdgpu_gem.h" 44 #include "amdgpu_display.h" 45 #include "amdgpu_ras.h" 46 47 void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev) 48 { 49 struct amdgpu_gpu_instance *gpu_instance; 50 int i; 51 52 mutex_lock(&mgpu_info.mutex); 53 54 for (i = 0; i < mgpu_info.num_gpu; i++) { 55 gpu_instance = &(mgpu_info.gpu_ins[i]); 56 if (gpu_instance->adev == adev) { 57 mgpu_info.gpu_ins[i] = 58 mgpu_info.gpu_ins[mgpu_info.num_gpu - 1]; 59 mgpu_info.num_gpu--; 60 if (adev->flags & AMD_IS_APU) 61 mgpu_info.num_apu--; 62 else 63 mgpu_info.num_dgpu--; 64 break; 65 } 66 } 67 68 mutex_unlock(&mgpu_info.mutex); 69 } 70 71 #include <drm/drm_pci.h> 72 #include <drm/drm_drv.h> 73 74 #include "vga.h" 75 76 #if NVGA > 0 77 #include <dev/ic/mc6845reg.h> 78 #include <dev/ic/pcdisplayvar.h> 79 #include <dev/ic/vgareg.h> 80 #include <dev/ic/vgavar.h> 81 82 extern int vga_console_attached; 83 #endif 84 85 #ifdef __amd64__ 86 #include "efifb.h" 87 #include <machine/biosvar.h> 88 #endif 89 90 #if NEFIFB > 0 91 #include <machine/efifbvar.h> 92 #endif 93 94 int amdgpu_probe(struct device *, void *, void *); 95 void amdgpu_attach(struct device *, struct device *, void *); 96 int amdgpu_detach(struct device *, int); 97 int amdgpu_activate(struct device *, int); 98 void amdgpu_attachhook(struct device *); 99 int amdgpu_forcedetach(struct amdgpu_device *); 100 101 bool amdgpu_msi_ok(struct amdgpu_device *); 102 103 extern const struct pci_device_id amdgpu_pciidlist[]; 104 extern struct drm_driver amdgpu_kms_driver; 105 extern int amdgpu_exp_hw_support; 106 107 /* 108 * set if the mountroot hook has a fatal error 109 * such as not being able to find the firmware 110 */ 111 int amdgpu_fatal_error; 112 113 struct cfattach amdgpu_ca = { 114 sizeof (struct amdgpu_device), amdgpu_probe, amdgpu_attach, 115 amdgpu_detach, amdgpu_activate 116 }; 117 118 struct cfdriver amdgpu_cd = { 119 NULL, "amdgpu", DV_DULL 120 }; 121 122 #ifdef __linux__ 123 /** 124 * amdgpu_driver_unload_kms - Main unload function for KMS. 125 * 126 * @dev: drm dev pointer 127 * 128 * This is the main unload function for KMS (all asics). 129 * Returns 0 on success. 130 */ 131 void amdgpu_driver_unload_kms(struct drm_device *dev) 132 { 133 struct amdgpu_device *adev = dev->dev_private; 134 135 if (adev == NULL) 136 return; 137 138 amdgpu_unregister_gpu_instance(adev); 139 140 if (adev->rmmio == NULL) 141 goto done_free; 142 143 if (adev->runpm) { 144 pm_runtime_get_sync(dev->dev); 145 pm_runtime_forbid(dev->dev); 146 } 147 148 amdgpu_acpi_fini(adev); 149 150 amdgpu_device_fini(adev); 151 152 done_free: 153 kfree(adev); 154 dev->dev_private = NULL; 155 } 156 #endif /* __linux__ */ 157 158 void amdgpu_register_gpu_instance(struct amdgpu_device *adev) 159 { 160 struct amdgpu_gpu_instance *gpu_instance; 161 162 mutex_lock(&mgpu_info.mutex); 163 164 if (mgpu_info.num_gpu >= MAX_GPU_INSTANCE) { 165 DRM_ERROR("Cannot register more gpu instance\n"); 166 mutex_unlock(&mgpu_info.mutex); 167 return; 168 } 169 170 gpu_instance = &(mgpu_info.gpu_ins[mgpu_info.num_gpu]); 171 gpu_instance->adev = adev; 172 gpu_instance->mgpu_fan_enabled = 0; 173 174 mgpu_info.num_gpu++; 175 if (adev->flags & AMD_IS_APU) 176 mgpu_info.num_apu++; 177 else 178 mgpu_info.num_dgpu++; 179 180 mutex_unlock(&mgpu_info.mutex); 181 } 182 183 #ifdef __linux__ 184 /** 185 * amdgpu_driver_load_kms - Main load function for KMS. 186 * 187 * @dev: drm dev pointer 188 * @flags: device flags 189 * 190 * This is the main load function for KMS (all asics). 191 * Returns 0 on success, error on failure. 192 */ 193 int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags) 194 { 195 struct amdgpu_device *adev; 196 int r, acpi_status; 197 198 adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL); 199 if (adev == NULL) { 200 return -ENOMEM; 201 } 202 dev->dev_private = (void *)adev; 203 204 if (amdgpu_has_atpx() && 205 (amdgpu_is_atpx_hybrid() || 206 amdgpu_has_atpx_dgpu_power_cntl()) && 207 ((flags & AMD_IS_APU) == 0) && 208 !pci_is_thunderbolt_attached(dev->pdev)) 209 flags |= AMD_IS_PX; 210 211 /* amdgpu_device_init should report only fatal error 212 * like memory allocation failure or iomapping failure, 213 * or memory manager initialization failure, it must 214 * properly initialize the GPU MC controller and permit 215 * VRAM allocation 216 */ 217 r = amdgpu_device_init(adev, dev, dev->pdev, flags); 218 if (r) { 219 dev_err(&dev->pdev->dev, "Fatal error during GPU init\n"); 220 goto out; 221 } 222 223 if (amdgpu_device_supports_boco(dev) && 224 (amdgpu_runtime_pm != 0)) /* enable runpm by default for boco */ 225 adev->runpm = true; 226 else if (amdgpu_device_supports_baco(dev) && 227 (amdgpu_runtime_pm != 0) && 228 (adev->asic_type >= CHIP_TOPAZ) && 229 (adev->asic_type != CHIP_VEGA10) && 230 (adev->asic_type != CHIP_VEGA20) && 231 (adev->asic_type != CHIP_ARCTURUS)) /* enable runpm on VI+ */ 232 adev->runpm = true; 233 else if (amdgpu_device_supports_baco(dev) && 234 (amdgpu_runtime_pm > 0)) /* enable runpm if runpm=1 on CI */ 235 adev->runpm = true; 236 237 /* Call ACPI methods: require modeset init 238 * but failure is not fatal 239 */ 240 if (!r) { 241 acpi_status = amdgpu_acpi_init(adev); 242 if (acpi_status) 243 dev_dbg(&dev->pdev->dev, 244 "Error during ACPI methods call\n"); 245 } 246 247 if (adev->runpm) { 248 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NEVER_SKIP); 249 pm_runtime_use_autosuspend(dev->dev); 250 pm_runtime_set_autosuspend_delay(dev->dev, 5000); 251 pm_runtime_set_active(dev->dev); 252 pm_runtime_allow(dev->dev); 253 pm_runtime_mark_last_busy(dev->dev); 254 pm_runtime_put_autosuspend(dev->dev); 255 } 256 257 out: 258 if (r) { 259 /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */ 260 if (adev->rmmio && adev->runpm) 261 pm_runtime_put_noidle(dev->dev); 262 amdgpu_driver_unload_kms(dev); 263 } 264 265 return r; 266 } 267 #endif /* __linux__ */ 268 269 static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info, 270 struct drm_amdgpu_query_fw *query_fw, 271 struct amdgpu_device *adev) 272 { 273 switch (query_fw->fw_type) { 274 case AMDGPU_INFO_FW_VCE: 275 fw_info->ver = adev->vce.fw_version; 276 fw_info->feature = adev->vce.fb_version; 277 break; 278 case AMDGPU_INFO_FW_UVD: 279 fw_info->ver = adev->uvd.fw_version; 280 fw_info->feature = 0; 281 break; 282 case AMDGPU_INFO_FW_VCN: 283 fw_info->ver = adev->vcn.fw_version; 284 fw_info->feature = 0; 285 break; 286 case AMDGPU_INFO_FW_GMC: 287 fw_info->ver = adev->gmc.fw_version; 288 fw_info->feature = 0; 289 break; 290 case AMDGPU_INFO_FW_GFX_ME: 291 fw_info->ver = adev->gfx.me_fw_version; 292 fw_info->feature = adev->gfx.me_feature_version; 293 break; 294 case AMDGPU_INFO_FW_GFX_PFP: 295 fw_info->ver = adev->gfx.pfp_fw_version; 296 fw_info->feature = adev->gfx.pfp_feature_version; 297 break; 298 case AMDGPU_INFO_FW_GFX_CE: 299 fw_info->ver = adev->gfx.ce_fw_version; 300 fw_info->feature = adev->gfx.ce_feature_version; 301 break; 302 case AMDGPU_INFO_FW_GFX_RLC: 303 fw_info->ver = adev->gfx.rlc_fw_version; 304 fw_info->feature = adev->gfx.rlc_feature_version; 305 break; 306 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL: 307 fw_info->ver = adev->gfx.rlc_srlc_fw_version; 308 fw_info->feature = adev->gfx.rlc_srlc_feature_version; 309 break; 310 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM: 311 fw_info->ver = adev->gfx.rlc_srlg_fw_version; 312 fw_info->feature = adev->gfx.rlc_srlg_feature_version; 313 break; 314 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM: 315 fw_info->ver = adev->gfx.rlc_srls_fw_version; 316 fw_info->feature = adev->gfx.rlc_srls_feature_version; 317 break; 318 case AMDGPU_INFO_FW_GFX_MEC: 319 if (query_fw->index == 0) { 320 fw_info->ver = adev->gfx.mec_fw_version; 321 fw_info->feature = adev->gfx.mec_feature_version; 322 } else if (query_fw->index == 1) { 323 fw_info->ver = adev->gfx.mec2_fw_version; 324 fw_info->feature = adev->gfx.mec2_feature_version; 325 } else 326 return -EINVAL; 327 break; 328 case AMDGPU_INFO_FW_SMC: 329 fw_info->ver = adev->pm.fw_version; 330 fw_info->feature = 0; 331 break; 332 case AMDGPU_INFO_FW_TA: 333 if (query_fw->index > 1) 334 return -EINVAL; 335 if (query_fw->index == 0) { 336 fw_info->ver = adev->psp.ta_fw_version; 337 fw_info->feature = adev->psp.ta_xgmi_ucode_version; 338 } else { 339 fw_info->ver = adev->psp.ta_fw_version; 340 fw_info->feature = adev->psp.ta_ras_ucode_version; 341 } 342 break; 343 case AMDGPU_INFO_FW_SDMA: 344 if (query_fw->index >= adev->sdma.num_instances) 345 return -EINVAL; 346 fw_info->ver = adev->sdma.instance[query_fw->index].fw_version; 347 fw_info->feature = adev->sdma.instance[query_fw->index].feature_version; 348 break; 349 case AMDGPU_INFO_FW_SOS: 350 fw_info->ver = adev->psp.sos_fw_version; 351 fw_info->feature = adev->psp.sos_feature_version; 352 break; 353 case AMDGPU_INFO_FW_ASD: 354 fw_info->ver = adev->psp.asd_fw_version; 355 fw_info->feature = adev->psp.asd_feature_version; 356 break; 357 case AMDGPU_INFO_FW_DMCU: 358 fw_info->ver = adev->dm.dmcu_fw_version; 359 fw_info->feature = 0; 360 break; 361 case AMDGPU_INFO_FW_DMCUB: 362 fw_info->ver = adev->dm.dmcub_fw_version; 363 fw_info->feature = 0; 364 break; 365 default: 366 return -EINVAL; 367 } 368 return 0; 369 } 370 371 static int amdgpu_hw_ip_info(struct amdgpu_device *adev, 372 struct drm_amdgpu_info *info, 373 struct drm_amdgpu_info_hw_ip *result) 374 { 375 uint32_t ib_start_alignment = 0; 376 uint32_t ib_size_alignment = 0; 377 enum amd_ip_block_type type; 378 unsigned int num_rings = 0; 379 unsigned int i, j; 380 381 if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT) 382 return -EINVAL; 383 384 switch (info->query_hw_ip.type) { 385 case AMDGPU_HW_IP_GFX: 386 type = AMD_IP_BLOCK_TYPE_GFX; 387 for (i = 0; i < adev->gfx.num_gfx_rings; i++) 388 if (adev->gfx.gfx_ring[i].sched.ready) 389 ++num_rings; 390 ib_start_alignment = 32; 391 ib_size_alignment = 32; 392 break; 393 case AMDGPU_HW_IP_COMPUTE: 394 type = AMD_IP_BLOCK_TYPE_GFX; 395 for (i = 0; i < adev->gfx.num_compute_rings; i++) 396 if (adev->gfx.compute_ring[i].sched.ready) 397 ++num_rings; 398 ib_start_alignment = 32; 399 ib_size_alignment = 32; 400 break; 401 case AMDGPU_HW_IP_DMA: 402 type = AMD_IP_BLOCK_TYPE_SDMA; 403 for (i = 0; i < adev->sdma.num_instances; i++) 404 if (adev->sdma.instance[i].ring.sched.ready) 405 ++num_rings; 406 ib_start_alignment = 256; 407 ib_size_alignment = 4; 408 break; 409 case AMDGPU_HW_IP_UVD: 410 type = AMD_IP_BLOCK_TYPE_UVD; 411 for (i = 0; i < adev->uvd.num_uvd_inst; i++) { 412 if (adev->uvd.harvest_config & (1 << i)) 413 continue; 414 415 if (adev->uvd.inst[i].ring.sched.ready) 416 ++num_rings; 417 } 418 ib_start_alignment = 64; 419 ib_size_alignment = 64; 420 break; 421 case AMDGPU_HW_IP_VCE: 422 type = AMD_IP_BLOCK_TYPE_VCE; 423 for (i = 0; i < adev->vce.num_rings; i++) 424 if (adev->vce.ring[i].sched.ready) 425 ++num_rings; 426 ib_start_alignment = 4; 427 ib_size_alignment = 1; 428 break; 429 case AMDGPU_HW_IP_UVD_ENC: 430 type = AMD_IP_BLOCK_TYPE_UVD; 431 for (i = 0; i < adev->uvd.num_uvd_inst; i++) { 432 if (adev->uvd.harvest_config & (1 << i)) 433 continue; 434 435 for (j = 0; j < adev->uvd.num_enc_rings; j++) 436 if (adev->uvd.inst[i].ring_enc[j].sched.ready) 437 ++num_rings; 438 } 439 ib_start_alignment = 64; 440 ib_size_alignment = 64; 441 break; 442 case AMDGPU_HW_IP_VCN_DEC: 443 type = AMD_IP_BLOCK_TYPE_VCN; 444 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 445 if (adev->uvd.harvest_config & (1 << i)) 446 continue; 447 448 if (adev->vcn.inst[i].ring_dec.sched.ready) 449 ++num_rings; 450 } 451 ib_start_alignment = 16; 452 ib_size_alignment = 16; 453 break; 454 case AMDGPU_HW_IP_VCN_ENC: 455 type = AMD_IP_BLOCK_TYPE_VCN; 456 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 457 if (adev->uvd.harvest_config & (1 << i)) 458 continue; 459 460 for (j = 0; j < adev->vcn.num_enc_rings; j++) 461 if (adev->vcn.inst[i].ring_enc[j].sched.ready) 462 ++num_rings; 463 } 464 ib_start_alignment = 64; 465 ib_size_alignment = 1; 466 break; 467 case AMDGPU_HW_IP_VCN_JPEG: 468 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ? 469 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN; 470 471 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) { 472 if (adev->jpeg.harvest_config & (1 << i)) 473 continue; 474 475 if (adev->jpeg.inst[i].ring_dec.sched.ready) 476 ++num_rings; 477 } 478 ib_start_alignment = 16; 479 ib_size_alignment = 16; 480 break; 481 default: 482 return -EINVAL; 483 } 484 485 for (i = 0; i < adev->num_ip_blocks; i++) 486 if (adev->ip_blocks[i].version->type == type && 487 adev->ip_blocks[i].status.valid) 488 break; 489 490 if (i == adev->num_ip_blocks) 491 return 0; 492 493 num_rings = min(amdgpu_ctx_num_entities[info->query_hw_ip.type], 494 num_rings); 495 496 result->hw_ip_version_major = adev->ip_blocks[i].version->major; 497 result->hw_ip_version_minor = adev->ip_blocks[i].version->minor; 498 result->capabilities_flags = 0; 499 result->available_rings = (1 << num_rings) - 1; 500 result->ib_start_alignment = ib_start_alignment; 501 result->ib_size_alignment = ib_size_alignment; 502 return 0; 503 } 504 505 /* 506 * Userspace get information ioctl 507 */ 508 /** 509 * amdgpu_info_ioctl - answer a device specific request. 510 * 511 * @adev: amdgpu device pointer 512 * @data: request object 513 * @filp: drm filp 514 * 515 * This function is used to pass device specific parameters to the userspace 516 * drivers. Examples include: pci device id, pipeline parms, tiling params, 517 * etc. (all asics). 518 * Returns 0 on success, -EINVAL on failure. 519 */ 520 static int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 521 { 522 struct amdgpu_device *adev = dev->dev_private; 523 struct drm_amdgpu_info *info = data; 524 struct amdgpu_mode_info *minfo = &adev->mode_info; 525 void __user *out = (void __user *)(uintptr_t)info->return_pointer; 526 uint32_t size = info->return_size; 527 struct drm_crtc *crtc; 528 uint32_t ui32 = 0; 529 uint64_t ui64 = 0; 530 int i, found; 531 int ui32_size = sizeof(ui32); 532 533 if (!info->return_size || !info->return_pointer) 534 return -EINVAL; 535 536 switch (info->query) { 537 case AMDGPU_INFO_ACCEL_WORKING: 538 ui32 = adev->accel_working; 539 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 540 case AMDGPU_INFO_CRTC_FROM_ID: 541 for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) { 542 crtc = (struct drm_crtc *)minfo->crtcs[i]; 543 if (crtc && crtc->base.id == info->mode_crtc.id) { 544 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 545 ui32 = amdgpu_crtc->crtc_id; 546 found = 1; 547 break; 548 } 549 } 550 if (!found) { 551 DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id); 552 return -EINVAL; 553 } 554 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 555 case AMDGPU_INFO_HW_IP_INFO: { 556 struct drm_amdgpu_info_hw_ip ip = {}; 557 int ret; 558 559 ret = amdgpu_hw_ip_info(adev, info, &ip); 560 if (ret) 561 return ret; 562 563 ret = copy_to_user(out, &ip, min((size_t)size, sizeof(ip))); 564 return ret ? -EFAULT : 0; 565 } 566 case AMDGPU_INFO_HW_IP_COUNT: { 567 enum amd_ip_block_type type; 568 uint32_t count = 0; 569 570 switch (info->query_hw_ip.type) { 571 case AMDGPU_HW_IP_GFX: 572 type = AMD_IP_BLOCK_TYPE_GFX; 573 break; 574 case AMDGPU_HW_IP_COMPUTE: 575 type = AMD_IP_BLOCK_TYPE_GFX; 576 break; 577 case AMDGPU_HW_IP_DMA: 578 type = AMD_IP_BLOCK_TYPE_SDMA; 579 break; 580 case AMDGPU_HW_IP_UVD: 581 type = AMD_IP_BLOCK_TYPE_UVD; 582 break; 583 case AMDGPU_HW_IP_VCE: 584 type = AMD_IP_BLOCK_TYPE_VCE; 585 break; 586 case AMDGPU_HW_IP_UVD_ENC: 587 type = AMD_IP_BLOCK_TYPE_UVD; 588 break; 589 case AMDGPU_HW_IP_VCN_DEC: 590 case AMDGPU_HW_IP_VCN_ENC: 591 type = AMD_IP_BLOCK_TYPE_VCN; 592 break; 593 case AMDGPU_HW_IP_VCN_JPEG: 594 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ? 595 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN; 596 break; 597 default: 598 return -EINVAL; 599 } 600 601 for (i = 0; i < adev->num_ip_blocks; i++) 602 if (adev->ip_blocks[i].version->type == type && 603 adev->ip_blocks[i].status.valid && 604 count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT) 605 count++; 606 607 return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0; 608 } 609 case AMDGPU_INFO_TIMESTAMP: 610 ui64 = amdgpu_gfx_get_gpu_clock_counter(adev); 611 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 612 case AMDGPU_INFO_FW_VERSION: { 613 struct drm_amdgpu_info_firmware fw_info; 614 int ret; 615 616 /* We only support one instance of each IP block right now. */ 617 if (info->query_fw.ip_instance != 0) 618 return -EINVAL; 619 620 ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev); 621 if (ret) 622 return ret; 623 624 return copy_to_user(out, &fw_info, 625 min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0; 626 } 627 case AMDGPU_INFO_NUM_BYTES_MOVED: 628 ui64 = atomic64_read(&adev->num_bytes_moved); 629 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 630 case AMDGPU_INFO_NUM_EVICTIONS: 631 ui64 = atomic64_read(&adev->num_evictions); 632 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 633 case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS: 634 ui64 = atomic64_read(&adev->num_vram_cpu_page_faults); 635 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 636 case AMDGPU_INFO_VRAM_USAGE: 637 ui64 = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 638 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 639 case AMDGPU_INFO_VIS_VRAM_USAGE: 640 ui64 = amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 641 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 642 case AMDGPU_INFO_GTT_USAGE: 643 ui64 = amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]); 644 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 645 case AMDGPU_INFO_GDS_CONFIG: { 646 struct drm_amdgpu_info_gds gds_info; 647 648 memset(&gds_info, 0, sizeof(gds_info)); 649 gds_info.compute_partition_size = adev->gds.gds_size; 650 gds_info.gds_total_size = adev->gds.gds_size; 651 gds_info.gws_per_compute_partition = adev->gds.gws_size; 652 gds_info.oa_per_compute_partition = adev->gds.oa_size; 653 return copy_to_user(out, &gds_info, 654 min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0; 655 } 656 case AMDGPU_INFO_VRAM_GTT: { 657 struct drm_amdgpu_info_vram_gtt vram_gtt; 658 659 vram_gtt.vram_size = adev->gmc.real_vram_size - 660 atomic64_read(&adev->vram_pin_size) - 661 AMDGPU_VM_RESERVED_VRAM; 662 vram_gtt.vram_cpu_accessible_size = 663 min(adev->gmc.visible_vram_size - 664 atomic64_read(&adev->visible_pin_size), 665 vram_gtt.vram_size); 666 vram_gtt.gtt_size = adev->mman.bdev.man[TTM_PL_TT].size; 667 vram_gtt.gtt_size *= PAGE_SIZE; 668 vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size); 669 return copy_to_user(out, &vram_gtt, 670 min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0; 671 } 672 case AMDGPU_INFO_MEMORY: { 673 struct drm_amdgpu_memory_info mem; 674 675 memset(&mem, 0, sizeof(mem)); 676 mem.vram.total_heap_size = adev->gmc.real_vram_size; 677 mem.vram.usable_heap_size = adev->gmc.real_vram_size - 678 atomic64_read(&adev->vram_pin_size) - 679 AMDGPU_VM_RESERVED_VRAM; 680 mem.vram.heap_usage = 681 amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 682 mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4; 683 684 mem.cpu_accessible_vram.total_heap_size = 685 adev->gmc.visible_vram_size; 686 mem.cpu_accessible_vram.usable_heap_size = 687 min(adev->gmc.visible_vram_size - 688 atomic64_read(&adev->visible_pin_size), 689 mem.vram.usable_heap_size); 690 mem.cpu_accessible_vram.heap_usage = 691 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 692 mem.cpu_accessible_vram.max_allocation = 693 mem.cpu_accessible_vram.usable_heap_size * 3 / 4; 694 695 mem.gtt.total_heap_size = adev->mman.bdev.man[TTM_PL_TT].size; 696 mem.gtt.total_heap_size *= PAGE_SIZE; 697 mem.gtt.usable_heap_size = mem.gtt.total_heap_size - 698 atomic64_read(&adev->gart_pin_size); 699 mem.gtt.heap_usage = 700 amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]); 701 mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4; 702 703 return copy_to_user(out, &mem, 704 min((size_t)size, sizeof(mem))) 705 ? -EFAULT : 0; 706 } 707 case AMDGPU_INFO_READ_MMR_REG: { 708 unsigned n, alloc_size; 709 uint32_t *regs; 710 unsigned se_num = (info->read_mmr_reg.instance >> 711 AMDGPU_INFO_MMR_SE_INDEX_SHIFT) & 712 AMDGPU_INFO_MMR_SE_INDEX_MASK; 713 unsigned sh_num = (info->read_mmr_reg.instance >> 714 AMDGPU_INFO_MMR_SH_INDEX_SHIFT) & 715 AMDGPU_INFO_MMR_SH_INDEX_MASK; 716 717 /* set full masks if the userspace set all bits 718 * in the bitfields */ 719 if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK) 720 se_num = 0xffffffff; 721 if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK) 722 sh_num = 0xffffffff; 723 724 if (info->read_mmr_reg.count > 128) 725 return -EINVAL; 726 727 regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL); 728 if (!regs) 729 return -ENOMEM; 730 alloc_size = info->read_mmr_reg.count * sizeof(*regs); 731 732 amdgpu_gfx_off_ctrl(adev, false); 733 for (i = 0; i < info->read_mmr_reg.count; i++) { 734 if (amdgpu_asic_read_register(adev, se_num, sh_num, 735 info->read_mmr_reg.dword_offset + i, 736 ®s[i])) { 737 DRM_DEBUG_KMS("unallowed offset %#x\n", 738 info->read_mmr_reg.dword_offset + i); 739 kfree(regs); 740 amdgpu_gfx_off_ctrl(adev, true); 741 return -EFAULT; 742 } 743 } 744 amdgpu_gfx_off_ctrl(adev, true); 745 n = copy_to_user(out, regs, min(size, alloc_size)); 746 kfree(regs); 747 return n ? -EFAULT : 0; 748 } 749 case AMDGPU_INFO_DEV_INFO: { 750 struct drm_amdgpu_info_device dev_info = {}; 751 uint64_t vm_size; 752 753 dev_info.device_id = dev->pdev->device; 754 dev_info.chip_rev = adev->rev_id; 755 dev_info.external_rev = adev->external_rev_id; 756 dev_info.pci_rev = dev->pdev->revision; 757 dev_info.family = adev->family; 758 dev_info.num_shader_engines = adev->gfx.config.max_shader_engines; 759 dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se; 760 /* return all clocks in KHz */ 761 dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10; 762 if (adev->pm.dpm_enabled) { 763 dev_info.max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10; 764 dev_info.max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10; 765 } else { 766 dev_info.max_engine_clock = adev->clock.default_sclk * 10; 767 dev_info.max_memory_clock = adev->clock.default_mclk * 10; 768 } 769 dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask; 770 dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se * 771 adev->gfx.config.max_shader_engines; 772 dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts; 773 dev_info._pad = 0; 774 dev_info.ids_flags = 0; 775 if (adev->flags & AMD_IS_APU) 776 dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION; 777 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) 778 dev_info.ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION; 779 780 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE; 781 vm_size -= AMDGPU_VA_RESERVED_SIZE; 782 783 /* Older VCE FW versions are buggy and can handle only 40bits */ 784 if (adev->vce.fw_version && 785 adev->vce.fw_version < AMDGPU_VCE_FW_53_45) 786 vm_size = min(vm_size, 1ULL << 40); 787 788 dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE; 789 dev_info.virtual_address_max = 790 min(vm_size, AMDGPU_GMC_HOLE_START); 791 792 if (vm_size > AMDGPU_GMC_HOLE_START) { 793 dev_info.high_va_offset = AMDGPU_GMC_HOLE_END; 794 dev_info.high_va_max = AMDGPU_GMC_HOLE_END | vm_size; 795 } 796 dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE); 797 dev_info.pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE; 798 dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE; 799 dev_info.cu_active_number = adev->gfx.cu_info.number; 800 dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask; 801 dev_info.ce_ram_size = adev->gfx.ce_ram_size; 802 memcpy(&dev_info.cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0], 803 sizeof(adev->gfx.cu_info.ao_cu_bitmap)); 804 memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0], 805 sizeof(adev->gfx.cu_info.bitmap)); 806 dev_info.vram_type = adev->gmc.vram_type; 807 dev_info.vram_bit_width = adev->gmc.vram_width; 808 dev_info.vce_harvest_config = adev->vce.harvest_config; 809 dev_info.gc_double_offchip_lds_buf = 810 adev->gfx.config.double_offchip_lds_buf; 811 dev_info.wave_front_size = adev->gfx.cu_info.wave_front_size; 812 dev_info.num_shader_visible_vgprs = adev->gfx.config.max_gprs; 813 dev_info.num_cu_per_sh = adev->gfx.config.max_cu_per_sh; 814 dev_info.num_tcc_blocks = adev->gfx.config.max_texture_channel_caches; 815 dev_info.gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth; 816 dev_info.gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth; 817 dev_info.max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads; 818 819 if (adev->family >= AMDGPU_FAMILY_NV) 820 dev_info.pa_sc_tile_steering_override = 821 adev->gfx.config.pa_sc_tile_steering_override; 822 823 dev_info.tcc_disabled_mask = adev->gfx.config.tcc_disabled_mask; 824 825 return copy_to_user(out, &dev_info, 826 min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0; 827 } 828 case AMDGPU_INFO_VCE_CLOCK_TABLE: { 829 unsigned i; 830 struct drm_amdgpu_info_vce_clock_table vce_clk_table = {}; 831 struct amd_vce_state *vce_state; 832 833 for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) { 834 vce_state = amdgpu_dpm_get_vce_clock_state(adev, i); 835 if (vce_state) { 836 vce_clk_table.entries[i].sclk = vce_state->sclk; 837 vce_clk_table.entries[i].mclk = vce_state->mclk; 838 vce_clk_table.entries[i].eclk = vce_state->evclk; 839 vce_clk_table.num_valid_entries++; 840 } 841 } 842 843 return copy_to_user(out, &vce_clk_table, 844 min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0; 845 } 846 case AMDGPU_INFO_VBIOS: { 847 uint32_t bios_size = adev->bios_size; 848 849 switch (info->vbios_info.type) { 850 case AMDGPU_INFO_VBIOS_SIZE: 851 return copy_to_user(out, &bios_size, 852 min((size_t)size, sizeof(bios_size))) 853 ? -EFAULT : 0; 854 case AMDGPU_INFO_VBIOS_IMAGE: { 855 uint8_t *bios; 856 uint32_t bios_offset = info->vbios_info.offset; 857 858 if (bios_offset >= bios_size) 859 return -EINVAL; 860 861 bios = adev->bios + bios_offset; 862 return copy_to_user(out, bios, 863 min((size_t)size, (size_t)(bios_size - bios_offset))) 864 ? -EFAULT : 0; 865 } 866 default: 867 DRM_DEBUG_KMS("Invalid request %d\n", 868 info->vbios_info.type); 869 return -EINVAL; 870 } 871 } 872 case AMDGPU_INFO_NUM_HANDLES: { 873 struct drm_amdgpu_info_num_handles handle; 874 875 switch (info->query_hw_ip.type) { 876 case AMDGPU_HW_IP_UVD: 877 /* Starting Polaris, we support unlimited UVD handles */ 878 if (adev->asic_type < CHIP_POLARIS10) { 879 handle.uvd_max_handles = adev->uvd.max_handles; 880 handle.uvd_used_handles = amdgpu_uvd_used_handles(adev); 881 882 return copy_to_user(out, &handle, 883 min((size_t)size, sizeof(handle))) ? -EFAULT : 0; 884 } else { 885 return -ENODATA; 886 } 887 888 break; 889 default: 890 return -EINVAL; 891 } 892 } 893 case AMDGPU_INFO_SENSOR: { 894 if (!adev->pm.dpm_enabled) 895 return -ENOENT; 896 897 switch (info->sensor_info.type) { 898 case AMDGPU_INFO_SENSOR_GFX_SCLK: 899 /* get sclk in Mhz */ 900 if (amdgpu_dpm_read_sensor(adev, 901 AMDGPU_PP_SENSOR_GFX_SCLK, 902 (void *)&ui32, &ui32_size)) { 903 return -EINVAL; 904 } 905 ui32 /= 100; 906 break; 907 case AMDGPU_INFO_SENSOR_GFX_MCLK: 908 /* get mclk in Mhz */ 909 if (amdgpu_dpm_read_sensor(adev, 910 AMDGPU_PP_SENSOR_GFX_MCLK, 911 (void *)&ui32, &ui32_size)) { 912 return -EINVAL; 913 } 914 ui32 /= 100; 915 break; 916 case AMDGPU_INFO_SENSOR_GPU_TEMP: 917 /* get temperature in millidegrees C */ 918 if (amdgpu_dpm_read_sensor(adev, 919 AMDGPU_PP_SENSOR_GPU_TEMP, 920 (void *)&ui32, &ui32_size)) { 921 return -EINVAL; 922 } 923 break; 924 case AMDGPU_INFO_SENSOR_GPU_LOAD: 925 /* get GPU load */ 926 if (amdgpu_dpm_read_sensor(adev, 927 AMDGPU_PP_SENSOR_GPU_LOAD, 928 (void *)&ui32, &ui32_size)) { 929 return -EINVAL; 930 } 931 break; 932 case AMDGPU_INFO_SENSOR_GPU_AVG_POWER: 933 /* get average GPU power */ 934 if (amdgpu_dpm_read_sensor(adev, 935 AMDGPU_PP_SENSOR_GPU_POWER, 936 (void *)&ui32, &ui32_size)) { 937 return -EINVAL; 938 } 939 ui32 >>= 8; 940 break; 941 case AMDGPU_INFO_SENSOR_VDDNB: 942 /* get VDDNB in millivolts */ 943 if (amdgpu_dpm_read_sensor(adev, 944 AMDGPU_PP_SENSOR_VDDNB, 945 (void *)&ui32, &ui32_size)) { 946 return -EINVAL; 947 } 948 break; 949 case AMDGPU_INFO_SENSOR_VDDGFX: 950 /* get VDDGFX in millivolts */ 951 if (amdgpu_dpm_read_sensor(adev, 952 AMDGPU_PP_SENSOR_VDDGFX, 953 (void *)&ui32, &ui32_size)) { 954 return -EINVAL; 955 } 956 break; 957 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK: 958 /* get stable pstate sclk in Mhz */ 959 if (amdgpu_dpm_read_sensor(adev, 960 AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, 961 (void *)&ui32, &ui32_size)) { 962 return -EINVAL; 963 } 964 ui32 /= 100; 965 break; 966 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK: 967 /* get stable pstate mclk in Mhz */ 968 if (amdgpu_dpm_read_sensor(adev, 969 AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, 970 (void *)&ui32, &ui32_size)) { 971 return -EINVAL; 972 } 973 ui32 /= 100; 974 break; 975 default: 976 DRM_DEBUG_KMS("Invalid request %d\n", 977 info->sensor_info.type); 978 return -EINVAL; 979 } 980 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 981 } 982 case AMDGPU_INFO_VRAM_LOST_COUNTER: 983 ui32 = atomic_read(&adev->vram_lost_counter); 984 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 985 case AMDGPU_INFO_RAS_ENABLED_FEATURES: { 986 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 987 uint64_t ras_mask; 988 989 if (!ras) 990 return -EINVAL; 991 ras_mask = (uint64_t)ras->supported << 32 | ras->features; 992 993 return copy_to_user(out, &ras_mask, 994 min_t(u64, size, sizeof(ras_mask))) ? 995 -EFAULT : 0; 996 } 997 default: 998 DRM_DEBUG_KMS("Invalid request %d\n", info->query); 999 return -EINVAL; 1000 } 1001 return 0; 1002 } 1003 1004 1005 /* 1006 * Outdated mess for old drm with Xorg being in charge (void function now). 1007 */ 1008 /** 1009 * amdgpu_driver_lastclose_kms - drm callback for last close 1010 * 1011 * @dev: drm dev pointer 1012 * 1013 * Switch vga_switcheroo state after last close (all asics). 1014 */ 1015 void amdgpu_driver_lastclose_kms(struct drm_device *dev) 1016 { 1017 drm_fb_helper_lastclose(dev); 1018 vga_switcheroo_process_delayed_switch(); 1019 } 1020 1021 /** 1022 * amdgpu_driver_open_kms - drm callback for open 1023 * 1024 * @dev: drm dev pointer 1025 * @file_priv: drm file 1026 * 1027 * On device open, init vm on cayman+ (all asics). 1028 * Returns 0 on success, error on failure. 1029 */ 1030 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv) 1031 { 1032 struct amdgpu_device *adev = dev->dev_private; 1033 struct amdgpu_fpriv *fpriv; 1034 int r, pasid; 1035 1036 /* Ensure IB tests are run on ring */ 1037 flush_delayed_work(&adev->delayed_init_work); 1038 1039 1040 if (amdgpu_ras_intr_triggered()) { 1041 DRM_ERROR("RAS Intr triggered, device disabled!!"); 1042 return -EHWPOISON; 1043 } 1044 1045 file_priv->driver_priv = NULL; 1046 1047 r = pm_runtime_get_sync(dev->dev); 1048 if (r < 0) 1049 return r; 1050 1051 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL); 1052 if (unlikely(!fpriv)) { 1053 r = -ENOMEM; 1054 goto out_suspend; 1055 } 1056 1057 pasid = amdgpu_pasid_alloc(16); 1058 if (pasid < 0) { 1059 dev_warn(adev->dev, "No more PASIDs available!"); 1060 pasid = 0; 1061 } 1062 r = amdgpu_vm_init(adev, &fpriv->vm, AMDGPU_VM_CONTEXT_GFX, pasid); 1063 if (r) 1064 goto error_pasid; 1065 1066 fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL); 1067 if (!fpriv->prt_va) { 1068 r = -ENOMEM; 1069 goto error_vm; 1070 } 1071 1072 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 1073 uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK; 1074 1075 r = amdgpu_map_static_csa(adev, &fpriv->vm, adev->virt.csa_obj, 1076 &fpriv->csa_va, csa_addr, AMDGPU_CSA_SIZE); 1077 if (r) 1078 goto error_vm; 1079 } 1080 1081 rw_init(&fpriv->bo_list_lock, "agbo"); 1082 idr_init(&fpriv->bo_list_handles); 1083 1084 amdgpu_ctx_mgr_init(&fpriv->ctx_mgr); 1085 1086 file_priv->driver_priv = fpriv; 1087 goto out_suspend; 1088 1089 error_vm: 1090 amdgpu_vm_fini(adev, &fpriv->vm); 1091 1092 error_pasid: 1093 if (pasid) 1094 amdgpu_pasid_free(pasid); 1095 1096 kfree(fpriv); 1097 1098 out_suspend: 1099 pm_runtime_mark_last_busy(dev->dev); 1100 pm_runtime_put_autosuspend(dev->dev); 1101 1102 return r; 1103 } 1104 1105 /** 1106 * amdgpu_driver_postclose_kms - drm callback for post close 1107 * 1108 * @dev: drm dev pointer 1109 * @file_priv: drm file 1110 * 1111 * On device post close, tear down vm on cayman+ (all asics). 1112 */ 1113 void amdgpu_driver_postclose_kms(struct drm_device *dev, 1114 struct drm_file *file_priv) 1115 { 1116 struct amdgpu_device *adev = dev->dev_private; 1117 struct amdgpu_fpriv *fpriv = file_priv->driver_priv; 1118 struct amdgpu_bo_list *list; 1119 struct amdgpu_bo *pd; 1120 unsigned int pasid; 1121 int handle; 1122 1123 if (!fpriv) 1124 return; 1125 1126 pm_runtime_get_sync(dev->dev); 1127 1128 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL) 1129 amdgpu_uvd_free_handles(adev, file_priv); 1130 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL) 1131 amdgpu_vce_free_handles(adev, file_priv); 1132 1133 amdgpu_vm_bo_rmv(adev, fpriv->prt_va); 1134 1135 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 1136 /* TODO: how to handle reserve failure */ 1137 BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true)); 1138 amdgpu_vm_bo_rmv(adev, fpriv->csa_va); 1139 fpriv->csa_va = NULL; 1140 amdgpu_bo_unreserve(adev->virt.csa_obj); 1141 } 1142 1143 pasid = fpriv->vm.pasid; 1144 pd = amdgpu_bo_ref(fpriv->vm.root.base.bo); 1145 1146 amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr); 1147 amdgpu_vm_fini(adev, &fpriv->vm); 1148 1149 if (pasid) 1150 amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid); 1151 amdgpu_bo_unref(&pd); 1152 1153 idr_for_each_entry(&fpriv->bo_list_handles, list, handle) 1154 amdgpu_bo_list_put(list); 1155 1156 idr_destroy(&fpriv->bo_list_handles); 1157 mutex_destroy(&fpriv->bo_list_lock); 1158 1159 kfree(fpriv); 1160 file_priv->driver_priv = NULL; 1161 1162 pm_runtime_mark_last_busy(dev->dev); 1163 pm_runtime_put_autosuspend(dev->dev); 1164 } 1165 1166 /* 1167 * VBlank related functions. 1168 */ 1169 /** 1170 * amdgpu_get_vblank_counter_kms - get frame count 1171 * 1172 * @crtc: crtc to get the frame count from 1173 * 1174 * Gets the frame count on the requested crtc (all asics). 1175 * Returns frame count on success, -EINVAL on failure. 1176 */ 1177 u32 amdgpu_get_vblank_counter_kms(struct drm_crtc *crtc) 1178 { 1179 struct drm_device *dev = crtc->dev; 1180 unsigned int pipe = crtc->index; 1181 struct amdgpu_device *adev = dev->dev_private; 1182 int vpos, hpos, stat; 1183 u32 count; 1184 1185 if (pipe >= adev->mode_info.num_crtc) { 1186 DRM_ERROR("Invalid crtc %u\n", pipe); 1187 return -EINVAL; 1188 } 1189 1190 /* The hw increments its frame counter at start of vsync, not at start 1191 * of vblank, as is required by DRM core vblank counter handling. 1192 * Cook the hw count here to make it appear to the caller as if it 1193 * incremented at start of vblank. We measure distance to start of 1194 * vblank in vpos. vpos therefore will be >= 0 between start of vblank 1195 * and start of vsync, so vpos >= 0 means to bump the hw frame counter 1196 * result by 1 to give the proper appearance to caller. 1197 */ 1198 if (adev->mode_info.crtcs[pipe]) { 1199 /* Repeat readout if needed to provide stable result if 1200 * we cross start of vsync during the queries. 1201 */ 1202 do { 1203 count = amdgpu_display_vblank_get_counter(adev, pipe); 1204 /* Ask amdgpu_display_get_crtc_scanoutpos to return 1205 * vpos as distance to start of vblank, instead of 1206 * regular vertical scanout pos. 1207 */ 1208 stat = amdgpu_display_get_crtc_scanoutpos( 1209 dev, pipe, GET_DISTANCE_TO_VBLANKSTART, 1210 &vpos, &hpos, NULL, NULL, 1211 &adev->mode_info.crtcs[pipe]->base.hwmode); 1212 } while (count != amdgpu_display_vblank_get_counter(adev, pipe)); 1213 1214 if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) != 1215 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) { 1216 DRM_DEBUG_VBL("Query failed! stat %d\n", stat); 1217 } else { 1218 DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n", 1219 pipe, vpos); 1220 1221 /* Bump counter if we are at >= leading edge of vblank, 1222 * but before vsync where vpos would turn negative and 1223 * the hw counter really increments. 1224 */ 1225 if (vpos >= 0) 1226 count++; 1227 } 1228 } else { 1229 /* Fallback to use value as is. */ 1230 count = amdgpu_display_vblank_get_counter(adev, pipe); 1231 DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n"); 1232 } 1233 1234 return count; 1235 } 1236 1237 /** 1238 * amdgpu_enable_vblank_kms - enable vblank interrupt 1239 * 1240 * @crtc: crtc to enable vblank interrupt for 1241 * 1242 * Enable the interrupt on the requested crtc (all asics). 1243 * Returns 0 on success, -EINVAL on failure. 1244 */ 1245 int amdgpu_enable_vblank_kms(struct drm_crtc *crtc) 1246 { 1247 struct drm_device *dev = crtc->dev; 1248 unsigned int pipe = crtc->index; 1249 struct amdgpu_device *adev = dev->dev_private; 1250 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe); 1251 1252 return amdgpu_irq_get(adev, &adev->crtc_irq, idx); 1253 } 1254 1255 /** 1256 * amdgpu_disable_vblank_kms - disable vblank interrupt 1257 * 1258 * @crtc: crtc to disable vblank interrupt for 1259 * 1260 * Disable the interrupt on the requested crtc (all asics). 1261 */ 1262 void amdgpu_disable_vblank_kms(struct drm_crtc *crtc) 1263 { 1264 struct drm_device *dev = crtc->dev; 1265 unsigned int pipe = crtc->index; 1266 struct amdgpu_device *adev = dev->dev_private; 1267 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe); 1268 1269 amdgpu_irq_put(adev, &adev->crtc_irq, idx); 1270 } 1271 1272 const struct drm_ioctl_desc amdgpu_ioctls_kms[] = { 1273 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1274 DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1275 DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1276 DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER), 1277 DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1278 DRM_IOCTL_DEF_DRV(AMDGPU_FENCE_TO_HANDLE, amdgpu_cs_fence_to_handle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1279 /* KMS */ 1280 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1281 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1282 DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1283 DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1284 DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1285 DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1286 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1287 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1288 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1289 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW) 1290 }; 1291 const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms); 1292 1293 /* 1294 * Debugfs info 1295 */ 1296 #if defined(CONFIG_DEBUG_FS) 1297 1298 static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data) 1299 { 1300 struct drm_info_node *node = (struct drm_info_node *) m->private; 1301 struct drm_device *dev = node->minor->dev; 1302 struct amdgpu_device *adev = dev->dev_private; 1303 struct drm_amdgpu_info_firmware fw_info; 1304 struct drm_amdgpu_query_fw query_fw; 1305 struct atom_context *ctx = adev->mode_info.atom_context; 1306 int ret, i; 1307 1308 /* VCE */ 1309 query_fw.fw_type = AMDGPU_INFO_FW_VCE; 1310 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1311 if (ret) 1312 return ret; 1313 seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n", 1314 fw_info.feature, fw_info.ver); 1315 1316 /* UVD */ 1317 query_fw.fw_type = AMDGPU_INFO_FW_UVD; 1318 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1319 if (ret) 1320 return ret; 1321 seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n", 1322 fw_info.feature, fw_info.ver); 1323 1324 /* GMC */ 1325 query_fw.fw_type = AMDGPU_INFO_FW_GMC; 1326 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1327 if (ret) 1328 return ret; 1329 seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n", 1330 fw_info.feature, fw_info.ver); 1331 1332 /* ME */ 1333 query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME; 1334 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1335 if (ret) 1336 return ret; 1337 seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n", 1338 fw_info.feature, fw_info.ver); 1339 1340 /* PFP */ 1341 query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP; 1342 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1343 if (ret) 1344 return ret; 1345 seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n", 1346 fw_info.feature, fw_info.ver); 1347 1348 /* CE */ 1349 query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE; 1350 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1351 if (ret) 1352 return ret; 1353 seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n", 1354 fw_info.feature, fw_info.ver); 1355 1356 /* RLC */ 1357 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC; 1358 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1359 if (ret) 1360 return ret; 1361 seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n", 1362 fw_info.feature, fw_info.ver); 1363 1364 /* RLC SAVE RESTORE LIST CNTL */ 1365 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL; 1366 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1367 if (ret) 1368 return ret; 1369 seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n", 1370 fw_info.feature, fw_info.ver); 1371 1372 /* RLC SAVE RESTORE LIST GPM MEM */ 1373 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM; 1374 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1375 if (ret) 1376 return ret; 1377 seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n", 1378 fw_info.feature, fw_info.ver); 1379 1380 /* RLC SAVE RESTORE LIST SRM MEM */ 1381 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM; 1382 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1383 if (ret) 1384 return ret; 1385 seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n", 1386 fw_info.feature, fw_info.ver); 1387 1388 /* MEC */ 1389 query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC; 1390 query_fw.index = 0; 1391 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1392 if (ret) 1393 return ret; 1394 seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n", 1395 fw_info.feature, fw_info.ver); 1396 1397 /* MEC2 */ 1398 if (adev->asic_type == CHIP_KAVERI || 1399 (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) { 1400 query_fw.index = 1; 1401 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1402 if (ret) 1403 return ret; 1404 seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n", 1405 fw_info.feature, fw_info.ver); 1406 } 1407 1408 /* PSP SOS */ 1409 query_fw.fw_type = AMDGPU_INFO_FW_SOS; 1410 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1411 if (ret) 1412 return ret; 1413 seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n", 1414 fw_info.feature, fw_info.ver); 1415 1416 1417 /* PSP ASD */ 1418 query_fw.fw_type = AMDGPU_INFO_FW_ASD; 1419 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1420 if (ret) 1421 return ret; 1422 seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n", 1423 fw_info.feature, fw_info.ver); 1424 1425 query_fw.fw_type = AMDGPU_INFO_FW_TA; 1426 for (i = 0; i < 2; i++) { 1427 query_fw.index = i; 1428 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1429 if (ret) 1430 continue; 1431 seq_printf(m, "TA %s feature version: %u, firmware version: 0x%08x\n", 1432 i ? "RAS" : "XGMI", fw_info.feature, fw_info.ver); 1433 } 1434 1435 /* SMC */ 1436 query_fw.fw_type = AMDGPU_INFO_FW_SMC; 1437 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1438 if (ret) 1439 return ret; 1440 seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n", 1441 fw_info.feature, fw_info.ver); 1442 1443 /* SDMA */ 1444 query_fw.fw_type = AMDGPU_INFO_FW_SDMA; 1445 for (i = 0; i < adev->sdma.num_instances; i++) { 1446 query_fw.index = i; 1447 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1448 if (ret) 1449 return ret; 1450 seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n", 1451 i, fw_info.feature, fw_info.ver); 1452 } 1453 1454 /* VCN */ 1455 query_fw.fw_type = AMDGPU_INFO_FW_VCN; 1456 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1457 if (ret) 1458 return ret; 1459 seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n", 1460 fw_info.feature, fw_info.ver); 1461 1462 /* DMCU */ 1463 query_fw.fw_type = AMDGPU_INFO_FW_DMCU; 1464 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1465 if (ret) 1466 return ret; 1467 seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n", 1468 fw_info.feature, fw_info.ver); 1469 1470 /* DMCUB */ 1471 query_fw.fw_type = AMDGPU_INFO_FW_DMCUB; 1472 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1473 if (ret) 1474 return ret; 1475 seq_printf(m, "DMCUB feature version: %u, firmware version: 0x%08x\n", 1476 fw_info.feature, fw_info.ver); 1477 1478 1479 seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version); 1480 1481 return 0; 1482 } 1483 1484 static const struct drm_info_list amdgpu_firmware_info_list[] = { 1485 {"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL}, 1486 }; 1487 #endif 1488 1489 int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev) 1490 { 1491 #if defined(CONFIG_DEBUG_FS) 1492 return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list, 1493 ARRAY_SIZE(amdgpu_firmware_info_list)); 1494 #else 1495 return 0; 1496 #endif 1497 } 1498 1499 int 1500 amdgpu_probe(struct device *parent, void *match, void *aux) 1501 { 1502 struct pci_attach_args *pa = aux; 1503 const struct pci_device_id *id_entry; 1504 unsigned long flags = 0; 1505 1506 if (amdgpu_fatal_error) 1507 return 0; 1508 1509 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id), 1510 PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist); 1511 if (id_entry != NULL) { 1512 flags = id_entry->driver_data; 1513 if (flags & AMD_EXP_HW_SUPPORT) 1514 return 0; 1515 else 1516 return 20; 1517 } 1518 1519 return 0; 1520 } 1521 1522 /* 1523 * some functions are only called once on init regardless of how many times 1524 * amdgpu attaches in linux this is handled via module_init()/module_exit() 1525 */ 1526 int amdgpu_refcnt; 1527 1528 int __init drm_sched_fence_slab_init(void); 1529 void __exit drm_sched_fence_slab_fini(void); 1530 1531 void 1532 amdgpu_attach(struct device *parent, struct device *self, void *aux) 1533 { 1534 struct amdgpu_device *adev = (struct amdgpu_device *)self; 1535 struct drm_device *dev; 1536 struct pci_attach_args *pa = aux; 1537 const struct pci_device_id *id_entry; 1538 pcireg_t type; 1539 int i; 1540 uint8_t rmmio_bar; 1541 paddr_t fb_aper; 1542 pcireg_t addr, mask; 1543 int s; 1544 1545 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id), 1546 PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist); 1547 adev->flags = id_entry->driver_data; 1548 adev->family = adev->flags & AMD_ASIC_MASK; 1549 adev->pc = pa->pa_pc; 1550 adev->pa_tag = pa->pa_tag; 1551 adev->iot = pa->pa_iot; 1552 adev->memt = pa->pa_memt; 1553 adev->dmat = pa->pa_dmat; 1554 1555 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY && 1556 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA && 1557 (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) 1558 & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) 1559 == (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) { 1560 adev->primary = 1; 1561 #if NVGA > 0 1562 adev->console = vga_is_console(pa->pa_iot, -1); 1563 vga_console_attached = 1; 1564 #endif 1565 } 1566 #if NEFIFB > 0 1567 if (efifb_is_primary(pa)) { 1568 adev->primary = 1; 1569 adev->console = efifb_is_console(pa); 1570 efifb_detach(); 1571 } 1572 #endif 1573 1574 #define AMDGPU_PCI_MEM 0x10 1575 1576 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM); 1577 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1578 pci_mapreg_info(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, 1579 type, &adev->fb_aper_offset, &adev->fb_aper_size, NULL)) { 1580 printf(": can't get frambuffer info\n"); 1581 return; 1582 } 1583 1584 if (adev->fb_aper_offset == 0) { 1585 bus_size_t start, end, pci_mem_end; 1586 bus_addr_t base; 1587 1588 start = max(PCI_MEM_START, pa->pa_memex->ex_start); 1589 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT) 1590 pci_mem_end = PCI_MEM64_END; 1591 else 1592 pci_mem_end = PCI_MEM_END; 1593 end = min(pci_mem_end, pa->pa_memex->ex_end); 1594 if (pa->pa_memex == NULL || 1595 extent_alloc_subregion(pa->pa_memex, start, end, 1596 adev->fb_aper_size, adev->fb_aper_size, 0, 0, 0, &base)) { 1597 printf(": can't reserve framebuffer space\n"); 1598 return; 1599 } 1600 pci_conf_write(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, base); 1601 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT) 1602 pci_conf_write(pa->pa_pc, pa->pa_tag, 1603 AMDGPU_PCI_MEM + 4, (uint64_t)base >> 32); 1604 adev->fb_aper_offset = base; 1605 } 1606 1607 for (i = PCI_MAPREG_START; i < PCI_MAPREG_END ;) { 1608 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, i); 1609 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_IO) { 1610 if (type & PCI_MAPREG_MEM_TYPE_64BIT) 1611 i += 8; 1612 else 1613 i += 4; 1614 continue; 1615 } 1616 if (pci_mapreg_map(pa, i, type, 0, 1617 &adev->rio_mem_bst, &adev->rio_mem_bsh, NULL, 1618 &adev->rio_mem_size, 0)) { 1619 printf(": can't map rio space\n"); 1620 return; 1621 } 1622 break; 1623 } 1624 1625 if (adev->family >= CHIP_BONAIRE) { 1626 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x18); 1627 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1628 pci_mapreg_map(pa, 0x18, type, 0, 1629 &adev->doorbell.bst, &adev->doorbell.bsh, 1630 &adev->doorbell.base, &adev->doorbell.size, 0)) { 1631 printf(": can't map doorbell space\n"); 1632 return; 1633 } 1634 } 1635 1636 if (adev->family >= CHIP_BONAIRE) 1637 rmmio_bar = 0x24; 1638 else 1639 rmmio_bar = 0x18; 1640 1641 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, rmmio_bar); 1642 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1643 pci_mapreg_map(pa, rmmio_bar, type, 0, 1644 &adev->rmmio_bst, &adev->rmmio_bsh, &adev->rmmio_base, 1645 &adev->rmmio_size, 0)) { 1646 printf(": can't map rmmio space\n"); 1647 return; 1648 } 1649 1650 /* 1651 * Make sure we have a base address for the ROM such that we 1652 * can map it later. 1653 */ 1654 s = splhigh(); 1655 addr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG); 1656 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, ~PCI_ROM_ENABLE); 1657 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG); 1658 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, addr); 1659 splx(s); 1660 1661 if (addr == 0 && PCI_ROM_SIZE(mask) != 0 && pa->pa_memex) { 1662 bus_size_t size, start, end; 1663 bus_addr_t base; 1664 1665 size = PCI_ROM_SIZE(mask); 1666 start = max(PCI_MEM_START, pa->pa_memex->ex_start); 1667 end = min(PCI_MEM_END, pa->pa_memex->ex_end); 1668 if (extent_alloc_subregion(pa->pa_memex, start, end, size, 1669 size, 0, 0, 0, &base) == 0) 1670 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, base); 1671 } 1672 1673 printf("\n"); 1674 1675 /* from amdgpu_init() */ 1676 if (amdgpu_refcnt == 0) { 1677 drm_sched_fence_slab_init(); 1678 1679 if (amdgpu_sync_init()) { 1680 printf(": amdgpu_sync_init failed\n"); 1681 return; 1682 } 1683 1684 if (amdgpu_fence_slab_init()) { 1685 amdgpu_sync_fini(); 1686 printf(": amdgpu_fence_slab_init failed\n"); 1687 return; 1688 } 1689 1690 amdgpu_kms_driver.num_ioctls = amdgpu_max_kms_ioctl; 1691 amdgpu_register_atpx_handler(); 1692 } 1693 amdgpu_refcnt++; 1694 1695 /* from amdgpu_pci_probe() */ 1696 { 1697 int ret; 1698 bool supports_atomic = false; 1699 1700 if (!amdgpu_virtual_display && 1701 amdgpu_device_asic_has_dc_support(adev->family)) 1702 supports_atomic = true; 1703 1704 if ((adev->flags & AMD_EXP_HW_SUPPORT) && !amdgpu_exp_hw_support) { 1705 DRM_INFO("This hardware requires experimental hardware support.\n"); 1706 } 1707 1708 /* 1709 * Initialize amdkfd before starting radeon. 1710 */ 1711 amdgpu_amdkfd_init(); 1712 1713 /* warn the user if they mix atomic and non-atomic capable GPUs */ 1714 if ((amdgpu_kms_driver.driver_features & DRIVER_ATOMIC) && !supports_atomic) 1715 DRM_ERROR("Mixing atomic and non-atomic capable GPUs!\n"); 1716 /* support atomic early so the atomic debugfs stuff gets created */ 1717 if (supports_atomic) 1718 amdgpu_kms_driver.driver_features |= DRIVER_ATOMIC; 1719 } 1720 1721 dev = drm_attach_pci(&amdgpu_kms_driver, pa, 0, adev->primary, 1722 self, NULL); 1723 adev->ddev = dev; 1724 adev->pdev = dev->pdev; 1725 1726 if (!amdgpu_msi_ok(adev)) 1727 pa->pa_flags &= ~PCI_FLAGS_MSI_ENABLED; 1728 1729 adev->irq.msi_enabled = false; 1730 if (pci_intr_map_msi(pa, &adev->intrh) == 0) 1731 adev->irq.msi_enabled = true; 1732 else if (pci_intr_map(pa, &adev->intrh) != 0) { 1733 printf(": couldn't map interrupt\n"); 1734 return; 1735 } 1736 printf("%s: %s\n", adev->self.dv_xname, 1737 pci_intr_string(pa->pa_pc, adev->intrh)); 1738 1739 adev->irqh = pci_intr_establish(pa->pa_pc, adev->intrh, IPL_TTY, 1740 amdgpu_irq_handler, adev->ddev, adev->self.dv_xname); 1741 if (adev->irqh == NULL) { 1742 printf("%s: couldn't establish interrupt\n", 1743 adev->self.dv_xname); 1744 return; 1745 } 1746 adev->pdev->irq = -1; 1747 1748 fb_aper = bus_space_mmap(adev->memt, adev->fb_aper_offset, 0, 0, 0); 1749 if (fb_aper != -1) 1750 rasops_claim_framebuffer(fb_aper, adev->fb_aper_size, self); 1751 1752 1753 adev->shutdown = true; 1754 config_mountroot(self, amdgpu_attachhook); 1755 } 1756 1757 int 1758 amdgpu_forcedetach(struct amdgpu_device *adev) 1759 { 1760 struct pci_softc *sc = (struct pci_softc *)adev->self.dv_parent; 1761 pcitag_t tag = adev->pa_tag; 1762 1763 #if NVGA > 0 1764 if (adev->primary) 1765 vga_console_attached = 0; 1766 #endif 1767 1768 /* reprobe pci device for non efi systems */ 1769 #if NEFIFB > 0 1770 if (bios_efiinfo == NULL && !efifb_cb_found()) { 1771 #endif 1772 config_detach(&adev->self, 0); 1773 return pci_probe_device(sc, tag, NULL, NULL); 1774 #if NEFIFB > 0 1775 } else if (adev->primary) { 1776 efifb_reattach(); 1777 } 1778 #endif 1779 1780 return 0; 1781 } 1782 1783 void amdgpu_burner(void *, u_int, u_int); 1784 int amdgpu_wsioctl(void *, u_long, caddr_t, int, struct proc *); 1785 paddr_t amdgpu_wsmmap(void *, off_t, int); 1786 int amdgpu_alloc_screen(void *, const struct wsscreen_descr *, 1787 void **, int *, int *, uint32_t *); 1788 void amdgpu_free_screen(void *, void *); 1789 int amdgpu_show_screen(void *, void *, int, 1790 void (*)(void *, int, int), void *); 1791 void amdgpu_doswitch(void *); 1792 void amdgpu_enter_ddb(void *, void *); 1793 1794 struct wsscreen_descr amdgpu_stdscreen = { 1795 "std", 1796 0, 0, 1797 0, 1798 0, 0, 1799 WSSCREEN_UNDERLINE | WSSCREEN_HILIT | 1800 WSSCREEN_REVERSE | WSSCREEN_WSCOLORS 1801 }; 1802 1803 const struct wsscreen_descr *amdgpu_scrlist[] = { 1804 &amdgpu_stdscreen, 1805 }; 1806 1807 struct wsscreen_list amdgpu_screenlist = { 1808 nitems(amdgpu_scrlist), amdgpu_scrlist 1809 }; 1810 1811 struct wsdisplay_accessops amdgpu_accessops = { 1812 .ioctl = amdgpu_wsioctl, 1813 .mmap = amdgpu_wsmmap, 1814 .alloc_screen = amdgpu_alloc_screen, 1815 .free_screen = amdgpu_free_screen, 1816 .show_screen = amdgpu_show_screen, 1817 .enter_ddb = amdgpu_enter_ddb, 1818 .getchar = rasops_getchar, 1819 .load_font = rasops_load_font, 1820 .list_font = rasops_list_font, 1821 .scrollback = rasops_scrollback, 1822 .burn_screen = amdgpu_burner 1823 }; 1824 1825 int 1826 amdgpu_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 1827 { 1828 struct rasops_info *ri = v; 1829 struct amdgpu_device *adev = ri->ri_hw; 1830 struct backlight_device *bd = adev->dm.backlight_dev; 1831 struct wsdisplay_param *dp = (struct wsdisplay_param *)data; 1832 struct wsdisplay_fbinfo *wdf; 1833 1834 switch (cmd) { 1835 case WSDISPLAYIO_GTYPE: 1836 *(u_int *)data = WSDISPLAY_TYPE_RADEONDRM; 1837 return 0; 1838 case WSDISPLAYIO_GINFO: 1839 wdf = (struct wsdisplay_fbinfo *)data; 1840 wdf->width = ri->ri_width; 1841 wdf->height = ri->ri_height; 1842 wdf->depth = ri->ri_depth; 1843 wdf->cmsize = 0; 1844 return 0; 1845 case WSDISPLAYIO_GETPARAM: 1846 if (bd == NULL) 1847 return -1; 1848 1849 switch (dp->param) { 1850 case WSDISPLAYIO_PARAM_BRIGHTNESS: 1851 dp->min = 0; 1852 dp->max = bd->props.max_brightness; 1853 dp->curval = bd->props.brightness; 1854 return (dp->max > dp->min) ? 0 : -1; 1855 } 1856 break; 1857 case WSDISPLAYIO_SETPARAM: 1858 if (bd == NULL || dp->curval > bd->props.max_brightness) 1859 return -1; 1860 1861 switch (dp->param) { 1862 case WSDISPLAYIO_PARAM_BRIGHTNESS: 1863 bd->props.brightness = dp->curval; 1864 backlight_update_status(bd); 1865 return 0; 1866 } 1867 break; 1868 } 1869 1870 return (-1); 1871 } 1872 1873 paddr_t 1874 amdgpu_wsmmap(void *v, off_t off, int prot) 1875 { 1876 return (-1); 1877 } 1878 1879 int 1880 amdgpu_alloc_screen(void *v, const struct wsscreen_descr *type, 1881 void **cookiep, int *curxp, int *curyp, uint32_t *attrp) 1882 { 1883 return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp); 1884 } 1885 1886 void 1887 amdgpu_free_screen(void *v, void *cookie) 1888 { 1889 return rasops_free_screen(v, cookie); 1890 } 1891 1892 int 1893 amdgpu_show_screen(void *v, void *cookie, int waitok, 1894 void (*cb)(void *, int, int), void *cbarg) 1895 { 1896 struct rasops_info *ri = v; 1897 struct amdgpu_device *adev = ri->ri_hw; 1898 1899 if (cookie == ri->ri_active) 1900 return (0); 1901 1902 adev->switchcb = cb; 1903 adev->switchcbarg = cbarg; 1904 adev->switchcookie = cookie; 1905 if (cb) { 1906 task_add(systq, &adev->switchtask); 1907 return (EAGAIN); 1908 } 1909 1910 amdgpu_doswitch(v); 1911 1912 return (0); 1913 } 1914 1915 void 1916 amdgpu_doswitch(void *v) 1917 { 1918 struct rasops_info *ri = v; 1919 struct amdgpu_device *adev = ri->ri_hw; 1920 struct amdgpu_crtc *amdgpu_crtc; 1921 int i, crtc; 1922 1923 rasops_show_screen(ri, adev->switchcookie, 0, NULL, NULL); 1924 drm_fb_helper_restore_fbdev_mode_unlocked((void *)adev->mode_info.rfbdev); 1925 1926 if (adev->switchcb) 1927 (adev->switchcb)(adev->switchcbarg, 0, 0); 1928 } 1929 1930 void 1931 amdgpu_enter_ddb(void *v, void *cookie) 1932 { 1933 struct rasops_info *ri = v; 1934 struct amdgpu_device *adev = ri->ri_hw; 1935 struct drm_fb_helper *fb_helper = (void *)adev->mode_info.rfbdev; 1936 1937 if (cookie == ri->ri_active) 1938 return; 1939 1940 rasops_show_screen(ri, cookie, 0, NULL, NULL); 1941 drm_fb_helper_debug_enter(fb_helper->fbdev); 1942 } 1943 1944 1945 void 1946 amdgpu_attachhook(struct device *self) 1947 { 1948 struct amdgpu_device *adev = (struct amdgpu_device *)self; 1949 struct drm_device *dev = adev->ddev; 1950 int r, acpi_status; 1951 1952 if (amdgpu_has_atpx() && 1953 (amdgpu_is_atpx_hybrid() || 1954 amdgpu_has_atpx_dgpu_power_cntl()) && 1955 ((adev->flags & AMD_IS_APU) == 0) && 1956 !pci_is_thunderbolt_attached(dev->pdev)) 1957 adev->flags |= AMD_IS_PX; 1958 1959 /* amdgpu_device_init should report only fatal error 1960 * like memory allocation failure or iomapping failure, 1961 * or memory manager initialization failure, it must 1962 * properly initialize the GPU MC controller and permit 1963 * VRAM allocation 1964 */ 1965 r = amdgpu_device_init(adev, dev, dev->pdev, adev->flags); 1966 if (r) { 1967 dev_err(&dev->pdev->dev, "Fatal error during GPU init\n"); 1968 goto out; 1969 } 1970 1971 if (amdgpu_device_supports_boco(dev) && 1972 (amdgpu_runtime_pm != 0)) /* enable runpm by default for boco */ 1973 adev->runpm = true; 1974 else if (amdgpu_device_supports_baco(dev) && 1975 (amdgpu_runtime_pm != 0) && 1976 (adev->asic_type >= CHIP_TOPAZ) && 1977 (adev->asic_type != CHIP_VEGA10) && 1978 (adev->asic_type != CHIP_VEGA20) && 1979 (adev->asic_type != CHIP_ARCTURUS)) /* enable runpm on VI+ */ 1980 adev->runpm = true; 1981 else if (amdgpu_device_supports_baco(dev) && 1982 (amdgpu_runtime_pm > 0)) /* enable runpm if runpm=1 on CI */ 1983 adev->runpm = true; 1984 1985 /* Call ACPI methods: require modeset init 1986 * but failure is not fatal 1987 */ 1988 if (!r) { 1989 acpi_status = amdgpu_acpi_init(adev); 1990 if (acpi_status) 1991 dev_dbg(&dev->pdev->dev, 1992 "Error during ACPI methods call\n"); 1993 } 1994 1995 if (adev->runpm) { 1996 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NEVER_SKIP); 1997 pm_runtime_use_autosuspend(dev->dev); 1998 pm_runtime_set_autosuspend_delay(dev->dev, 5000); 1999 pm_runtime_set_active(dev->dev); 2000 pm_runtime_allow(dev->dev); 2001 pm_runtime_mark_last_busy(dev->dev); 2002 pm_runtime_put_autosuspend(dev->dev); 2003 } 2004 { 2005 struct wsemuldisplaydev_attach_args aa; 2006 struct rasops_info *ri = &adev->ro; 2007 2008 task_set(&adev->switchtask, amdgpu_doswitch, ri); 2009 2010 if (ri->ri_bits == NULL) 2011 return; 2012 2013 ri->ri_flg = RI_CENTER | RI_VCONS | RI_WRONLY; 2014 rasops_init(ri, 160, 160); 2015 2016 ri->ri_hw = adev; 2017 2018 amdgpu_stdscreen.capabilities = ri->ri_caps; 2019 amdgpu_stdscreen.nrows = ri->ri_rows; 2020 amdgpu_stdscreen.ncols = ri->ri_cols; 2021 amdgpu_stdscreen.textops = &ri->ri_ops; 2022 amdgpu_stdscreen.fontwidth = ri->ri_font->fontwidth; 2023 amdgpu_stdscreen.fontheight = ri->ri_font->fontheight; 2024 2025 aa.console = adev->console; 2026 aa.primary = adev->primary; 2027 aa.scrdata = &amdgpu_screenlist; 2028 aa.accessops = &amdgpu_accessops; 2029 aa.accesscookie = ri; 2030 aa.defaultscreens = 0; 2031 2032 if (adev->console) { 2033 uint32_t defattr; 2034 2035 ri->ri_ops.pack_attr(ri->ri_active, 0, 0, 0, &defattr); 2036 wsdisplay_cnattach(&amdgpu_stdscreen, ri->ri_active, 2037 ri->ri_ccol, ri->ri_crow, defattr); 2038 } 2039 2040 /* 2041 * Now that we've taken over the console, disable decoding of 2042 * VGA legacy addresses, and opt out of arbitration. 2043 */ 2044 amdgpu_asic_set_vga_state(adev, false); 2045 pci_disable_legacy_vga(&adev->self); 2046 2047 printf("%s: %dx%d, %dbpp\n", adev->self.dv_xname, 2048 ri->ri_width, ri->ri_height, ri->ri_depth); 2049 2050 config_found_sm(&adev->self, &aa, wsemuldisplaydevprint, 2051 wsemuldisplaydevsubmatch); 2052 2053 /* 2054 * in linux via amdgpu_pci_probe -> drm_dev_register 2055 */ 2056 drm_dev_register(dev, adev->flags); 2057 } 2058 2059 out: 2060 if (r) { 2061 /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */ 2062 if (adev->runpm) 2063 pm_runtime_put_noidle(dev->dev); 2064 amdgpu_fatal_error = 1; 2065 amdgpu_forcedetach(adev); 2066 } 2067 } 2068 2069 /* from amdgpu_exit amdgpu_driver_unload_kms */ 2070 int 2071 amdgpu_detach(struct device *self, int flags) 2072 { 2073 struct amdgpu_device *adev = (struct amdgpu_device *)self; 2074 struct drm_device *dev = adev->ddev; 2075 2076 if (adev == NULL) 2077 return 0; 2078 2079 amdgpu_refcnt--; 2080 2081 if (amdgpu_refcnt == 0) 2082 amdgpu_amdkfd_fini(); 2083 2084 pci_intr_disestablish(adev->pc, adev->irqh); 2085 2086 amdgpu_unregister_gpu_instance(adev); 2087 2088 if (adev->runpm) { 2089 pm_runtime_get_sync(dev->dev); 2090 pm_runtime_forbid(dev->dev); 2091 } 2092 2093 amdgpu_acpi_fini(adev); 2094 2095 amdgpu_device_fini(adev); 2096 2097 if (amdgpu_refcnt == 0) { 2098 amdgpu_unregister_atpx_handler(); 2099 amdgpu_sync_fini(); 2100 amdgpu_fence_slab_fini(); 2101 2102 drm_sched_fence_slab_fini(); 2103 } 2104 2105 if (adev->ddev != NULL) { 2106 config_detach(adev->ddev->dev, flags); 2107 adev->ddev = NULL; 2108 } 2109 2110 return 0; 2111 } 2112 2113 int 2114 amdgpu_activate(struct device *self, int act) 2115 { 2116 struct amdgpu_device *adev = (struct amdgpu_device *)self; 2117 int rv = 0; 2118 2119 if (adev->ddev == NULL) 2120 return (0); 2121 2122 switch (act) { 2123 case DVACT_QUIESCE: 2124 rv = config_activate_children(self, act); 2125 amdgpu_device_suspend(adev->ddev, true); 2126 break; 2127 case DVACT_SUSPEND: 2128 break; 2129 case DVACT_RESUME: 2130 break; 2131 case DVACT_WAKEUP: 2132 amdgpu_device_resume(adev->ddev, true); 2133 rv = config_activate_children(self, act); 2134 break; 2135 } 2136 2137 return (rv); 2138 } 2139