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