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 else if (se_num >= AMDGPU_GFX_MAX_SE) 722 return -EINVAL; 723 if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK) 724 sh_num = 0xffffffff; 725 else if (sh_num >= AMDGPU_GFX_MAX_SH_PER_SE) 726 return -EINVAL; 727 728 if (info->read_mmr_reg.count > 128) 729 return -EINVAL; 730 731 regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL); 732 if (!regs) 733 return -ENOMEM; 734 alloc_size = info->read_mmr_reg.count * sizeof(*regs); 735 736 amdgpu_gfx_off_ctrl(adev, false); 737 for (i = 0; i < info->read_mmr_reg.count; i++) { 738 if (amdgpu_asic_read_register(adev, se_num, sh_num, 739 info->read_mmr_reg.dword_offset + i, 740 ®s[i])) { 741 DRM_DEBUG_KMS("unallowed offset %#x\n", 742 info->read_mmr_reg.dword_offset + i); 743 kfree(regs); 744 amdgpu_gfx_off_ctrl(adev, true); 745 return -EFAULT; 746 } 747 } 748 amdgpu_gfx_off_ctrl(adev, true); 749 n = copy_to_user(out, regs, min(size, alloc_size)); 750 kfree(regs); 751 return n ? -EFAULT : 0; 752 } 753 case AMDGPU_INFO_DEV_INFO: { 754 struct drm_amdgpu_info_device dev_info; 755 uint64_t vm_size; 756 757 memset(&dev_info, 0, sizeof(dev_info)); 758 dev_info.device_id = dev->pdev->device; 759 dev_info.chip_rev = adev->rev_id; 760 dev_info.external_rev = adev->external_rev_id; 761 dev_info.pci_rev = dev->pdev->revision; 762 dev_info.family = adev->family; 763 dev_info.num_shader_engines = adev->gfx.config.max_shader_engines; 764 dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se; 765 /* return all clocks in KHz */ 766 dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10; 767 if (adev->pm.dpm_enabled) { 768 dev_info.max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10; 769 dev_info.max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10; 770 } else { 771 dev_info.max_engine_clock = adev->clock.default_sclk * 10; 772 dev_info.max_memory_clock = adev->clock.default_mclk * 10; 773 } 774 dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask; 775 dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se * 776 adev->gfx.config.max_shader_engines; 777 dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts; 778 dev_info._pad = 0; 779 dev_info.ids_flags = 0; 780 if (adev->flags & AMD_IS_APU) 781 dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION; 782 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) 783 dev_info.ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION; 784 785 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE; 786 vm_size -= AMDGPU_VA_RESERVED_SIZE; 787 788 /* Older VCE FW versions are buggy and can handle only 40bits */ 789 if (adev->vce.fw_version && 790 adev->vce.fw_version < AMDGPU_VCE_FW_53_45) 791 vm_size = min(vm_size, 1ULL << 40); 792 793 dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE; 794 dev_info.virtual_address_max = 795 min(vm_size, AMDGPU_GMC_HOLE_START); 796 797 if (vm_size > AMDGPU_GMC_HOLE_START) { 798 dev_info.high_va_offset = AMDGPU_GMC_HOLE_END; 799 dev_info.high_va_max = AMDGPU_GMC_HOLE_END | vm_size; 800 } 801 dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE); 802 dev_info.pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE; 803 dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE; 804 dev_info.cu_active_number = adev->gfx.cu_info.number; 805 dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask; 806 dev_info.ce_ram_size = adev->gfx.ce_ram_size; 807 memcpy(&dev_info.cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0], 808 sizeof(adev->gfx.cu_info.ao_cu_bitmap)); 809 memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0], 810 sizeof(adev->gfx.cu_info.bitmap)); 811 dev_info.vram_type = adev->gmc.vram_type; 812 dev_info.vram_bit_width = adev->gmc.vram_width; 813 dev_info.vce_harvest_config = adev->vce.harvest_config; 814 dev_info.gc_double_offchip_lds_buf = 815 adev->gfx.config.double_offchip_lds_buf; 816 dev_info.wave_front_size = adev->gfx.cu_info.wave_front_size; 817 dev_info.num_shader_visible_vgprs = adev->gfx.config.max_gprs; 818 dev_info.num_cu_per_sh = adev->gfx.config.max_cu_per_sh; 819 dev_info.num_tcc_blocks = adev->gfx.config.max_texture_channel_caches; 820 dev_info.gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth; 821 dev_info.gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth; 822 dev_info.max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads; 823 824 if (adev->family >= AMDGPU_FAMILY_NV) 825 dev_info.pa_sc_tile_steering_override = 826 adev->gfx.config.pa_sc_tile_steering_override; 827 828 dev_info.tcc_disabled_mask = adev->gfx.config.tcc_disabled_mask; 829 830 return copy_to_user(out, &dev_info, 831 min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0; 832 } 833 case AMDGPU_INFO_VCE_CLOCK_TABLE: { 834 unsigned i; 835 struct drm_amdgpu_info_vce_clock_table vce_clk_table = {}; 836 struct amd_vce_state *vce_state; 837 838 for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) { 839 vce_state = amdgpu_dpm_get_vce_clock_state(adev, i); 840 if (vce_state) { 841 vce_clk_table.entries[i].sclk = vce_state->sclk; 842 vce_clk_table.entries[i].mclk = vce_state->mclk; 843 vce_clk_table.entries[i].eclk = vce_state->evclk; 844 vce_clk_table.num_valid_entries++; 845 } 846 } 847 848 return copy_to_user(out, &vce_clk_table, 849 min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0; 850 } 851 case AMDGPU_INFO_VBIOS: { 852 uint32_t bios_size = adev->bios_size; 853 854 switch (info->vbios_info.type) { 855 case AMDGPU_INFO_VBIOS_SIZE: 856 return copy_to_user(out, &bios_size, 857 min((size_t)size, sizeof(bios_size))) 858 ? -EFAULT : 0; 859 case AMDGPU_INFO_VBIOS_IMAGE: { 860 uint8_t *bios; 861 uint32_t bios_offset = info->vbios_info.offset; 862 863 if (bios_offset >= bios_size) 864 return -EINVAL; 865 866 bios = adev->bios + bios_offset; 867 return copy_to_user(out, bios, 868 min((size_t)size, (size_t)(bios_size - bios_offset))) 869 ? -EFAULT : 0; 870 } 871 default: 872 DRM_DEBUG_KMS("Invalid request %d\n", 873 info->vbios_info.type); 874 return -EINVAL; 875 } 876 } 877 case AMDGPU_INFO_NUM_HANDLES: { 878 struct drm_amdgpu_info_num_handles handle; 879 880 switch (info->query_hw_ip.type) { 881 case AMDGPU_HW_IP_UVD: 882 /* Starting Polaris, we support unlimited UVD handles */ 883 if (adev->asic_type < CHIP_POLARIS10) { 884 handle.uvd_max_handles = adev->uvd.max_handles; 885 handle.uvd_used_handles = amdgpu_uvd_used_handles(adev); 886 887 return copy_to_user(out, &handle, 888 min((size_t)size, sizeof(handle))) ? -EFAULT : 0; 889 } else { 890 return -ENODATA; 891 } 892 893 break; 894 default: 895 return -EINVAL; 896 } 897 } 898 case AMDGPU_INFO_SENSOR: { 899 if (!adev->pm.dpm_enabled) 900 return -ENOENT; 901 902 switch (info->sensor_info.type) { 903 case AMDGPU_INFO_SENSOR_GFX_SCLK: 904 /* get sclk in Mhz */ 905 if (amdgpu_dpm_read_sensor(adev, 906 AMDGPU_PP_SENSOR_GFX_SCLK, 907 (void *)&ui32, &ui32_size)) { 908 return -EINVAL; 909 } 910 ui32 /= 100; 911 break; 912 case AMDGPU_INFO_SENSOR_GFX_MCLK: 913 /* get mclk in Mhz */ 914 if (amdgpu_dpm_read_sensor(adev, 915 AMDGPU_PP_SENSOR_GFX_MCLK, 916 (void *)&ui32, &ui32_size)) { 917 return -EINVAL; 918 } 919 ui32 /= 100; 920 break; 921 case AMDGPU_INFO_SENSOR_GPU_TEMP: 922 /* get temperature in millidegrees C */ 923 if (amdgpu_dpm_read_sensor(adev, 924 AMDGPU_PP_SENSOR_GPU_TEMP, 925 (void *)&ui32, &ui32_size)) { 926 return -EINVAL; 927 } 928 break; 929 case AMDGPU_INFO_SENSOR_GPU_LOAD: 930 /* get GPU load */ 931 if (amdgpu_dpm_read_sensor(adev, 932 AMDGPU_PP_SENSOR_GPU_LOAD, 933 (void *)&ui32, &ui32_size)) { 934 return -EINVAL; 935 } 936 break; 937 case AMDGPU_INFO_SENSOR_GPU_AVG_POWER: 938 /* get average GPU power */ 939 if (amdgpu_dpm_read_sensor(adev, 940 AMDGPU_PP_SENSOR_GPU_POWER, 941 (void *)&ui32, &ui32_size)) { 942 return -EINVAL; 943 } 944 ui32 >>= 8; 945 break; 946 case AMDGPU_INFO_SENSOR_VDDNB: 947 /* get VDDNB in millivolts */ 948 if (amdgpu_dpm_read_sensor(adev, 949 AMDGPU_PP_SENSOR_VDDNB, 950 (void *)&ui32, &ui32_size)) { 951 return -EINVAL; 952 } 953 break; 954 case AMDGPU_INFO_SENSOR_VDDGFX: 955 /* get VDDGFX in millivolts */ 956 if (amdgpu_dpm_read_sensor(adev, 957 AMDGPU_PP_SENSOR_VDDGFX, 958 (void *)&ui32, &ui32_size)) { 959 return -EINVAL; 960 } 961 break; 962 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK: 963 /* get stable pstate sclk in Mhz */ 964 if (amdgpu_dpm_read_sensor(adev, 965 AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, 966 (void *)&ui32, &ui32_size)) { 967 return -EINVAL; 968 } 969 ui32 /= 100; 970 break; 971 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK: 972 /* get stable pstate mclk in Mhz */ 973 if (amdgpu_dpm_read_sensor(adev, 974 AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, 975 (void *)&ui32, &ui32_size)) { 976 return -EINVAL; 977 } 978 ui32 /= 100; 979 break; 980 default: 981 DRM_DEBUG_KMS("Invalid request %d\n", 982 info->sensor_info.type); 983 return -EINVAL; 984 } 985 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 986 } 987 case AMDGPU_INFO_VRAM_LOST_COUNTER: 988 ui32 = atomic_read(&adev->vram_lost_counter); 989 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 990 case AMDGPU_INFO_RAS_ENABLED_FEATURES: { 991 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 992 uint64_t ras_mask; 993 994 if (!ras) 995 return -EINVAL; 996 ras_mask = (uint64_t)ras->supported << 32 | ras->features; 997 998 return copy_to_user(out, &ras_mask, 999 min_t(u64, size, sizeof(ras_mask))) ? 1000 -EFAULT : 0; 1001 } 1002 default: 1003 DRM_DEBUG_KMS("Invalid request %d\n", info->query); 1004 return -EINVAL; 1005 } 1006 return 0; 1007 } 1008 1009 1010 /* 1011 * Outdated mess for old drm with Xorg being in charge (void function now). 1012 */ 1013 /** 1014 * amdgpu_driver_lastclose_kms - drm callback for last close 1015 * 1016 * @dev: drm dev pointer 1017 * 1018 * Switch vga_switcheroo state after last close (all asics). 1019 */ 1020 void amdgpu_driver_lastclose_kms(struct drm_device *dev) 1021 { 1022 drm_fb_helper_lastclose(dev); 1023 vga_switcheroo_process_delayed_switch(); 1024 } 1025 1026 /** 1027 * amdgpu_driver_open_kms - drm callback for open 1028 * 1029 * @dev: drm dev pointer 1030 * @file_priv: drm file 1031 * 1032 * On device open, init vm on cayman+ (all asics). 1033 * Returns 0 on success, error on failure. 1034 */ 1035 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv) 1036 { 1037 struct amdgpu_device *adev = dev->dev_private; 1038 struct amdgpu_fpriv *fpriv; 1039 int r, pasid; 1040 1041 /* Ensure IB tests are run on ring */ 1042 flush_delayed_work(&adev->delayed_init_work); 1043 1044 1045 if (amdgpu_ras_intr_triggered()) { 1046 DRM_ERROR("RAS Intr triggered, device disabled!!"); 1047 return -EHWPOISON; 1048 } 1049 1050 file_priv->driver_priv = NULL; 1051 1052 r = pm_runtime_get_sync(dev->dev); 1053 if (r < 0) 1054 return r; 1055 1056 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL); 1057 if (unlikely(!fpriv)) { 1058 r = -ENOMEM; 1059 goto out_suspend; 1060 } 1061 1062 pasid = amdgpu_pasid_alloc(16); 1063 if (pasid < 0) { 1064 dev_warn(adev->dev, "No more PASIDs available!"); 1065 pasid = 0; 1066 } 1067 r = amdgpu_vm_init(adev, &fpriv->vm, AMDGPU_VM_CONTEXT_GFX, pasid); 1068 if (r) 1069 goto error_pasid; 1070 1071 fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL); 1072 if (!fpriv->prt_va) { 1073 r = -ENOMEM; 1074 goto error_vm; 1075 } 1076 1077 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 1078 uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK; 1079 1080 r = amdgpu_map_static_csa(adev, &fpriv->vm, adev->virt.csa_obj, 1081 &fpriv->csa_va, csa_addr, AMDGPU_CSA_SIZE); 1082 if (r) 1083 goto error_vm; 1084 } 1085 1086 rw_init(&fpriv->bo_list_lock, "agbo"); 1087 idr_init(&fpriv->bo_list_handles); 1088 1089 amdgpu_ctx_mgr_init(&fpriv->ctx_mgr); 1090 1091 file_priv->driver_priv = fpriv; 1092 goto out_suspend; 1093 1094 error_vm: 1095 amdgpu_vm_fini(adev, &fpriv->vm); 1096 1097 error_pasid: 1098 if (pasid) 1099 amdgpu_pasid_free(pasid); 1100 1101 kfree(fpriv); 1102 1103 out_suspend: 1104 pm_runtime_mark_last_busy(dev->dev); 1105 pm_runtime_put_autosuspend(dev->dev); 1106 1107 return r; 1108 } 1109 1110 /** 1111 * amdgpu_driver_postclose_kms - drm callback for post close 1112 * 1113 * @dev: drm dev pointer 1114 * @file_priv: drm file 1115 * 1116 * On device post close, tear down vm on cayman+ (all asics). 1117 */ 1118 void amdgpu_driver_postclose_kms(struct drm_device *dev, 1119 struct drm_file *file_priv) 1120 { 1121 struct amdgpu_device *adev = dev->dev_private; 1122 struct amdgpu_fpriv *fpriv = file_priv->driver_priv; 1123 struct amdgpu_bo_list *list; 1124 struct amdgpu_bo *pd; 1125 unsigned int pasid; 1126 int handle; 1127 1128 if (!fpriv) 1129 return; 1130 1131 pm_runtime_get_sync(dev->dev); 1132 1133 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL) 1134 amdgpu_uvd_free_handles(adev, file_priv); 1135 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL) 1136 amdgpu_vce_free_handles(adev, file_priv); 1137 1138 amdgpu_vm_bo_rmv(adev, fpriv->prt_va); 1139 1140 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 1141 /* TODO: how to handle reserve failure */ 1142 BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true)); 1143 amdgpu_vm_bo_rmv(adev, fpriv->csa_va); 1144 fpriv->csa_va = NULL; 1145 amdgpu_bo_unreserve(adev->virt.csa_obj); 1146 } 1147 1148 pasid = fpriv->vm.pasid; 1149 pd = amdgpu_bo_ref(fpriv->vm.root.base.bo); 1150 1151 amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr); 1152 amdgpu_vm_fini(adev, &fpriv->vm); 1153 1154 if (pasid) 1155 amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid); 1156 amdgpu_bo_unref(&pd); 1157 1158 idr_for_each_entry(&fpriv->bo_list_handles, list, handle) 1159 amdgpu_bo_list_put(list); 1160 1161 idr_destroy(&fpriv->bo_list_handles); 1162 mutex_destroy(&fpriv->bo_list_lock); 1163 1164 kfree(fpriv); 1165 file_priv->driver_priv = NULL; 1166 1167 pm_runtime_mark_last_busy(dev->dev); 1168 pm_runtime_put_autosuspend(dev->dev); 1169 } 1170 1171 /* 1172 * VBlank related functions. 1173 */ 1174 /** 1175 * amdgpu_get_vblank_counter_kms - get frame count 1176 * 1177 * @crtc: crtc to get the frame count from 1178 * 1179 * Gets the frame count on the requested crtc (all asics). 1180 * Returns frame count on success, -EINVAL on failure. 1181 */ 1182 u32 amdgpu_get_vblank_counter_kms(struct drm_crtc *crtc) 1183 { 1184 struct drm_device *dev = crtc->dev; 1185 unsigned int pipe = crtc->index; 1186 struct amdgpu_device *adev = dev->dev_private; 1187 int vpos, hpos, stat; 1188 u32 count; 1189 1190 if (pipe >= adev->mode_info.num_crtc) { 1191 DRM_ERROR("Invalid crtc %u\n", pipe); 1192 return -EINVAL; 1193 } 1194 1195 /* The hw increments its frame counter at start of vsync, not at start 1196 * of vblank, as is required by DRM core vblank counter handling. 1197 * Cook the hw count here to make it appear to the caller as if it 1198 * incremented at start of vblank. We measure distance to start of 1199 * vblank in vpos. vpos therefore will be >= 0 between start of vblank 1200 * and start of vsync, so vpos >= 0 means to bump the hw frame counter 1201 * result by 1 to give the proper appearance to caller. 1202 */ 1203 if (adev->mode_info.crtcs[pipe]) { 1204 /* Repeat readout if needed to provide stable result if 1205 * we cross start of vsync during the queries. 1206 */ 1207 do { 1208 count = amdgpu_display_vblank_get_counter(adev, pipe); 1209 /* Ask amdgpu_display_get_crtc_scanoutpos to return 1210 * vpos as distance to start of vblank, instead of 1211 * regular vertical scanout pos. 1212 */ 1213 stat = amdgpu_display_get_crtc_scanoutpos( 1214 dev, pipe, GET_DISTANCE_TO_VBLANKSTART, 1215 &vpos, &hpos, NULL, NULL, 1216 &adev->mode_info.crtcs[pipe]->base.hwmode); 1217 } while (count != amdgpu_display_vblank_get_counter(adev, pipe)); 1218 1219 if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) != 1220 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) { 1221 DRM_DEBUG_VBL("Query failed! stat %d\n", stat); 1222 } else { 1223 DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n", 1224 pipe, vpos); 1225 1226 /* Bump counter if we are at >= leading edge of vblank, 1227 * but before vsync where vpos would turn negative and 1228 * the hw counter really increments. 1229 */ 1230 if (vpos >= 0) 1231 count++; 1232 } 1233 } else { 1234 /* Fallback to use value as is. */ 1235 count = amdgpu_display_vblank_get_counter(adev, pipe); 1236 DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n"); 1237 } 1238 1239 return count; 1240 } 1241 1242 /** 1243 * amdgpu_enable_vblank_kms - enable vblank interrupt 1244 * 1245 * @crtc: crtc to enable vblank interrupt for 1246 * 1247 * Enable the interrupt on the requested crtc (all asics). 1248 * Returns 0 on success, -EINVAL on failure. 1249 */ 1250 int amdgpu_enable_vblank_kms(struct drm_crtc *crtc) 1251 { 1252 struct drm_device *dev = crtc->dev; 1253 unsigned int pipe = crtc->index; 1254 struct amdgpu_device *adev = dev->dev_private; 1255 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe); 1256 1257 return amdgpu_irq_get(adev, &adev->crtc_irq, idx); 1258 } 1259 1260 /** 1261 * amdgpu_disable_vblank_kms - disable vblank interrupt 1262 * 1263 * @crtc: crtc to disable vblank interrupt for 1264 * 1265 * Disable the interrupt on the requested crtc (all asics). 1266 */ 1267 void amdgpu_disable_vblank_kms(struct drm_crtc *crtc) 1268 { 1269 struct drm_device *dev = crtc->dev; 1270 unsigned int pipe = crtc->index; 1271 struct amdgpu_device *adev = dev->dev_private; 1272 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe); 1273 1274 amdgpu_irq_put(adev, &adev->crtc_irq, idx); 1275 } 1276 1277 const struct drm_ioctl_desc amdgpu_ioctls_kms[] = { 1278 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1279 DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1280 DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1281 DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER), 1282 DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1283 DRM_IOCTL_DEF_DRV(AMDGPU_FENCE_TO_HANDLE, amdgpu_cs_fence_to_handle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1284 /* KMS */ 1285 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1286 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1287 DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1288 DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1289 DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1290 DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1291 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1292 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1293 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1294 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW) 1295 }; 1296 const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms); 1297 1298 /* 1299 * Debugfs info 1300 */ 1301 #if defined(CONFIG_DEBUG_FS) 1302 1303 static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data) 1304 { 1305 struct drm_info_node *node = (struct drm_info_node *) m->private; 1306 struct drm_device *dev = node->minor->dev; 1307 struct amdgpu_device *adev = dev->dev_private; 1308 struct drm_amdgpu_info_firmware fw_info; 1309 struct drm_amdgpu_query_fw query_fw; 1310 struct atom_context *ctx = adev->mode_info.atom_context; 1311 int ret, i; 1312 1313 /* VCE */ 1314 query_fw.fw_type = AMDGPU_INFO_FW_VCE; 1315 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1316 if (ret) 1317 return ret; 1318 seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n", 1319 fw_info.feature, fw_info.ver); 1320 1321 /* UVD */ 1322 query_fw.fw_type = AMDGPU_INFO_FW_UVD; 1323 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1324 if (ret) 1325 return ret; 1326 seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n", 1327 fw_info.feature, fw_info.ver); 1328 1329 /* GMC */ 1330 query_fw.fw_type = AMDGPU_INFO_FW_GMC; 1331 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1332 if (ret) 1333 return ret; 1334 seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n", 1335 fw_info.feature, fw_info.ver); 1336 1337 /* ME */ 1338 query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME; 1339 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1340 if (ret) 1341 return ret; 1342 seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n", 1343 fw_info.feature, fw_info.ver); 1344 1345 /* PFP */ 1346 query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP; 1347 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1348 if (ret) 1349 return ret; 1350 seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n", 1351 fw_info.feature, fw_info.ver); 1352 1353 /* CE */ 1354 query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE; 1355 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1356 if (ret) 1357 return ret; 1358 seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n", 1359 fw_info.feature, fw_info.ver); 1360 1361 /* RLC */ 1362 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC; 1363 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1364 if (ret) 1365 return ret; 1366 seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n", 1367 fw_info.feature, fw_info.ver); 1368 1369 /* RLC SAVE RESTORE LIST CNTL */ 1370 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL; 1371 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1372 if (ret) 1373 return ret; 1374 seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n", 1375 fw_info.feature, fw_info.ver); 1376 1377 /* RLC SAVE RESTORE LIST GPM MEM */ 1378 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM; 1379 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1380 if (ret) 1381 return ret; 1382 seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n", 1383 fw_info.feature, fw_info.ver); 1384 1385 /* RLC SAVE RESTORE LIST SRM MEM */ 1386 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM; 1387 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1388 if (ret) 1389 return ret; 1390 seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n", 1391 fw_info.feature, fw_info.ver); 1392 1393 /* MEC */ 1394 query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC; 1395 query_fw.index = 0; 1396 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1397 if (ret) 1398 return ret; 1399 seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n", 1400 fw_info.feature, fw_info.ver); 1401 1402 /* MEC2 */ 1403 if (adev->asic_type == CHIP_KAVERI || 1404 (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) { 1405 query_fw.index = 1; 1406 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1407 if (ret) 1408 return ret; 1409 seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n", 1410 fw_info.feature, fw_info.ver); 1411 } 1412 1413 /* PSP SOS */ 1414 query_fw.fw_type = AMDGPU_INFO_FW_SOS; 1415 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1416 if (ret) 1417 return ret; 1418 seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n", 1419 fw_info.feature, fw_info.ver); 1420 1421 1422 /* PSP ASD */ 1423 query_fw.fw_type = AMDGPU_INFO_FW_ASD; 1424 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1425 if (ret) 1426 return ret; 1427 seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n", 1428 fw_info.feature, fw_info.ver); 1429 1430 query_fw.fw_type = AMDGPU_INFO_FW_TA; 1431 for (i = 0; i < 2; i++) { 1432 query_fw.index = i; 1433 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1434 if (ret) 1435 continue; 1436 seq_printf(m, "TA %s feature version: %u, firmware version: 0x%08x\n", 1437 i ? "RAS" : "XGMI", fw_info.feature, fw_info.ver); 1438 } 1439 1440 /* SMC */ 1441 query_fw.fw_type = AMDGPU_INFO_FW_SMC; 1442 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1443 if (ret) 1444 return ret; 1445 seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n", 1446 fw_info.feature, fw_info.ver); 1447 1448 /* SDMA */ 1449 query_fw.fw_type = AMDGPU_INFO_FW_SDMA; 1450 for (i = 0; i < adev->sdma.num_instances; i++) { 1451 query_fw.index = i; 1452 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1453 if (ret) 1454 return ret; 1455 seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n", 1456 i, fw_info.feature, fw_info.ver); 1457 } 1458 1459 /* VCN */ 1460 query_fw.fw_type = AMDGPU_INFO_FW_VCN; 1461 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1462 if (ret) 1463 return ret; 1464 seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n", 1465 fw_info.feature, fw_info.ver); 1466 1467 /* DMCU */ 1468 query_fw.fw_type = AMDGPU_INFO_FW_DMCU; 1469 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1470 if (ret) 1471 return ret; 1472 seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n", 1473 fw_info.feature, fw_info.ver); 1474 1475 /* DMCUB */ 1476 query_fw.fw_type = AMDGPU_INFO_FW_DMCUB; 1477 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1478 if (ret) 1479 return ret; 1480 seq_printf(m, "DMCUB feature version: %u, firmware version: 0x%08x\n", 1481 fw_info.feature, fw_info.ver); 1482 1483 1484 seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version); 1485 1486 return 0; 1487 } 1488 1489 static const struct drm_info_list amdgpu_firmware_info_list[] = { 1490 {"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL}, 1491 }; 1492 #endif 1493 1494 int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev) 1495 { 1496 #if defined(CONFIG_DEBUG_FS) 1497 return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list, 1498 ARRAY_SIZE(amdgpu_firmware_info_list)); 1499 #else 1500 return 0; 1501 #endif 1502 } 1503 1504 int 1505 amdgpu_probe(struct device *parent, void *match, void *aux) 1506 { 1507 struct pci_attach_args *pa = aux; 1508 const struct pci_device_id *id_entry; 1509 unsigned long flags = 0; 1510 1511 if (amdgpu_fatal_error) 1512 return 0; 1513 1514 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id), 1515 PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist); 1516 if (id_entry != NULL) { 1517 flags = id_entry->driver_data; 1518 if (flags & AMD_EXP_HW_SUPPORT) 1519 return 0; 1520 else 1521 return 20; 1522 } 1523 1524 return 0; 1525 } 1526 1527 /* 1528 * some functions are only called once on init regardless of how many times 1529 * amdgpu attaches in linux this is handled via module_init()/module_exit() 1530 */ 1531 int amdgpu_refcnt; 1532 1533 int __init drm_sched_fence_slab_init(void); 1534 void __exit drm_sched_fence_slab_fini(void); 1535 1536 void 1537 amdgpu_attach(struct device *parent, struct device *self, void *aux) 1538 { 1539 struct amdgpu_device *adev = (struct amdgpu_device *)self; 1540 struct drm_device *dev; 1541 struct pci_attach_args *pa = aux; 1542 const struct pci_device_id *id_entry; 1543 pcireg_t type; 1544 int i; 1545 uint8_t rmmio_bar; 1546 paddr_t fb_aper; 1547 pcireg_t addr, mask; 1548 int s; 1549 1550 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id), 1551 PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist); 1552 adev->flags = id_entry->driver_data; 1553 adev->family = adev->flags & AMD_ASIC_MASK; 1554 adev->pc = pa->pa_pc; 1555 adev->pa_tag = pa->pa_tag; 1556 adev->iot = pa->pa_iot; 1557 adev->memt = pa->pa_memt; 1558 adev->dmat = pa->pa_dmat; 1559 1560 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY && 1561 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA && 1562 (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) 1563 & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) 1564 == (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) { 1565 adev->primary = 1; 1566 #if NVGA > 0 1567 adev->console = vga_is_console(pa->pa_iot, -1); 1568 vga_console_attached = 1; 1569 #endif 1570 } 1571 #if NEFIFB > 0 1572 if (efifb_is_primary(pa)) { 1573 adev->primary = 1; 1574 adev->console = efifb_is_console(pa); 1575 efifb_detach(); 1576 } 1577 #endif 1578 1579 #define AMDGPU_PCI_MEM 0x10 1580 1581 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM); 1582 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1583 pci_mapreg_info(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, 1584 type, &adev->fb_aper_offset, &adev->fb_aper_size, NULL)) { 1585 printf(": can't get frambuffer info\n"); 1586 return; 1587 } 1588 1589 if (adev->fb_aper_offset == 0) { 1590 bus_size_t start, end, pci_mem_end; 1591 bus_addr_t base; 1592 1593 start = max(PCI_MEM_START, pa->pa_memex->ex_start); 1594 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT) 1595 pci_mem_end = PCI_MEM64_END; 1596 else 1597 pci_mem_end = PCI_MEM_END; 1598 end = min(pci_mem_end, pa->pa_memex->ex_end); 1599 if (pa->pa_memex == NULL || 1600 extent_alloc_subregion(pa->pa_memex, start, end, 1601 adev->fb_aper_size, adev->fb_aper_size, 0, 0, 0, &base)) { 1602 printf(": can't reserve framebuffer space\n"); 1603 return; 1604 } 1605 pci_conf_write(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, base); 1606 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT) 1607 pci_conf_write(pa->pa_pc, pa->pa_tag, 1608 AMDGPU_PCI_MEM + 4, (uint64_t)base >> 32); 1609 adev->fb_aper_offset = base; 1610 } 1611 1612 for (i = PCI_MAPREG_START; i < PCI_MAPREG_END; i += 4) { 1613 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, i); 1614 if (type == PCI_MAPREG_TYPE_IO) { 1615 pci_mapreg_map(pa, i, type, 0, &adev->rio_mem_bst, 1616 &adev->rio_mem_bsh, NULL, &adev->rio_mem_size, 0); 1617 break; 1618 } 1619 if (type == PCI_MAPREG_MEM_TYPE_64BIT) 1620 i += 4; 1621 } 1622 1623 if (adev->family >= CHIP_BONAIRE) { 1624 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x18); 1625 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1626 pci_mapreg_map(pa, 0x18, type, BUS_SPACE_MAP_LINEAR, 1627 &adev->doorbell.bst, &adev->doorbell.bsh, 1628 &adev->doorbell.base, &adev->doorbell.size, 0)) { 1629 printf(": can't map doorbell space\n"); 1630 return; 1631 } 1632 adev->doorbell.ptr = bus_space_vaddr(adev->doorbell.bst, 1633 adev->doorbell.bsh); 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, BUS_SPACE_MAP_LINEAR, 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 adev->rmmio = bus_space_vaddr(adev->rmmio_bst, adev->rmmio_bsh); 1650 1651 /* 1652 * Make sure we have a base address for the ROM such that we 1653 * can map it later. 1654 */ 1655 s = splhigh(); 1656 addr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG); 1657 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, ~PCI_ROM_ENABLE); 1658 mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG); 1659 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, addr); 1660 splx(s); 1661 1662 if (addr == 0 && PCI_ROM_SIZE(mask) != 0 && pa->pa_memex) { 1663 bus_size_t size, start, end; 1664 bus_addr_t base; 1665 1666 size = PCI_ROM_SIZE(mask); 1667 start = max(PCI_MEM_START, pa->pa_memex->ex_start); 1668 end = min(PCI_MEM_END, pa->pa_memex->ex_end); 1669 if (extent_alloc_subregion(pa->pa_memex, start, end, size, 1670 size, 0, 0, 0, &base) == 0) 1671 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, base); 1672 } 1673 1674 printf("\n"); 1675 1676 /* from amdgpu_init() */ 1677 if (amdgpu_refcnt == 0) { 1678 drm_sched_fence_slab_init(); 1679 1680 if (amdgpu_sync_init()) { 1681 printf(": amdgpu_sync_init failed\n"); 1682 return; 1683 } 1684 1685 if (amdgpu_fence_slab_init()) { 1686 amdgpu_sync_fini(); 1687 printf(": amdgpu_fence_slab_init failed\n"); 1688 return; 1689 } 1690 1691 amdgpu_kms_driver.num_ioctls = amdgpu_max_kms_ioctl; 1692 amdgpu_register_atpx_handler(); 1693 } 1694 amdgpu_refcnt++; 1695 1696 /* from amdgpu_pci_probe() */ 1697 { 1698 int ret; 1699 bool supports_atomic = false; 1700 1701 if (!amdgpu_virtual_display && 1702 amdgpu_device_asic_has_dc_support(adev->family)) 1703 supports_atomic = true; 1704 1705 if ((adev->flags & AMD_EXP_HW_SUPPORT) && !amdgpu_exp_hw_support) { 1706 DRM_INFO("This hardware requires experimental hardware support.\n"); 1707 } 1708 1709 /* 1710 * Initialize amdkfd before starting radeon. 1711 */ 1712 amdgpu_amdkfd_init(); 1713 1714 /* warn the user if they mix atomic and non-atomic capable GPUs */ 1715 if ((amdgpu_kms_driver.driver_features & DRIVER_ATOMIC) && !supports_atomic) 1716 DRM_ERROR("Mixing atomic and non-atomic capable GPUs!\n"); 1717 /* support atomic early so the atomic debugfs stuff gets created */ 1718 if (supports_atomic) 1719 amdgpu_kms_driver.driver_features |= DRIVER_ATOMIC; 1720 } 1721 1722 dev = drm_attach_pci(&amdgpu_kms_driver, pa, 0, adev->primary, 1723 self, NULL); 1724 adev->ddev = dev; 1725 adev->pdev = dev->pdev; 1726 1727 if (!amdgpu_msi_ok(adev)) 1728 pa->pa_flags &= ~PCI_FLAGS_MSI_ENABLED; 1729 1730 adev->irq.msi_enabled = false; 1731 if (pci_intr_map_msi(pa, &adev->intrh) == 0) 1732 adev->irq.msi_enabled = true; 1733 else if (pci_intr_map(pa, &adev->intrh) != 0) { 1734 printf(": couldn't map interrupt\n"); 1735 return; 1736 } 1737 printf("%s: %s\n", adev->self.dv_xname, 1738 pci_intr_string(pa->pa_pc, adev->intrh)); 1739 1740 adev->irqh = pci_intr_establish(pa->pa_pc, adev->intrh, IPL_TTY, 1741 amdgpu_irq_handler, adev->ddev, adev->self.dv_xname); 1742 if (adev->irqh == NULL) { 1743 printf("%s: couldn't establish interrupt\n", 1744 adev->self.dv_xname); 1745 return; 1746 } 1747 adev->pdev->irq = -1; 1748 1749 fb_aper = bus_space_mmap(adev->memt, adev->fb_aper_offset, 0, 0, 0); 1750 if (fb_aper != -1) 1751 rasops_claim_framebuffer(fb_aper, adev->fb_aper_size, self); 1752 1753 1754 adev->shutdown = true; 1755 config_mountroot(self, amdgpu_attachhook); 1756 } 1757 1758 int 1759 amdgpu_forcedetach(struct amdgpu_device *adev) 1760 { 1761 struct pci_softc *sc = (struct pci_softc *)adev->self.dv_parent; 1762 pcitag_t tag = adev->pa_tag; 1763 1764 #if NVGA > 0 1765 if (adev->primary) 1766 vga_console_attached = 0; 1767 #endif 1768 1769 /* reprobe pci device for non efi systems */ 1770 #if NEFIFB > 0 1771 if (bios_efiinfo == NULL && !efifb_cb_found()) { 1772 #endif 1773 config_detach(&adev->self, 0); 1774 return pci_probe_device(sc, tag, NULL, NULL); 1775 #if NEFIFB > 0 1776 } else if (adev->primary) { 1777 efifb_reattach(); 1778 } 1779 #endif 1780 1781 return 0; 1782 } 1783 1784 void amdgpu_burner(void *, u_int, u_int); 1785 int amdgpu_wsioctl(void *, u_long, caddr_t, int, struct proc *); 1786 paddr_t amdgpu_wsmmap(void *, off_t, int); 1787 int amdgpu_alloc_screen(void *, const struct wsscreen_descr *, 1788 void **, int *, int *, uint32_t *); 1789 void amdgpu_free_screen(void *, void *); 1790 int amdgpu_show_screen(void *, void *, int, 1791 void (*)(void *, int, int), void *); 1792 void amdgpu_doswitch(void *); 1793 void amdgpu_enter_ddb(void *, void *); 1794 1795 struct wsscreen_descr amdgpu_stdscreen = { 1796 "std", 1797 0, 0, 1798 0, 1799 0, 0, 1800 WSSCREEN_UNDERLINE | WSSCREEN_HILIT | 1801 WSSCREEN_REVERSE | WSSCREEN_WSCOLORS 1802 }; 1803 1804 const struct wsscreen_descr *amdgpu_scrlist[] = { 1805 &amdgpu_stdscreen, 1806 }; 1807 1808 struct wsscreen_list amdgpu_screenlist = { 1809 nitems(amdgpu_scrlist), amdgpu_scrlist 1810 }; 1811 1812 struct wsdisplay_accessops amdgpu_accessops = { 1813 .ioctl = amdgpu_wsioctl, 1814 .mmap = amdgpu_wsmmap, 1815 .alloc_screen = amdgpu_alloc_screen, 1816 .free_screen = amdgpu_free_screen, 1817 .show_screen = amdgpu_show_screen, 1818 .enter_ddb = amdgpu_enter_ddb, 1819 .getchar = rasops_getchar, 1820 .load_font = rasops_load_font, 1821 .list_font = rasops_list_font, 1822 .scrollback = rasops_scrollback, 1823 .burn_screen = amdgpu_burner 1824 }; 1825 1826 int 1827 amdgpu_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p) 1828 { 1829 struct rasops_info *ri = v; 1830 struct amdgpu_device *adev = ri->ri_hw; 1831 struct backlight_device *bd = adev->dm.backlight_dev; 1832 struct wsdisplay_param *dp = (struct wsdisplay_param *)data; 1833 struct wsdisplay_fbinfo *wdf; 1834 1835 switch (cmd) { 1836 case WSDISPLAYIO_GTYPE: 1837 *(u_int *)data = WSDISPLAY_TYPE_RADEONDRM; 1838 return 0; 1839 case WSDISPLAYIO_GINFO: 1840 wdf = (struct wsdisplay_fbinfo *)data; 1841 wdf->width = ri->ri_width; 1842 wdf->height = ri->ri_height; 1843 wdf->depth = ri->ri_depth; 1844 wdf->cmsize = 0; 1845 return 0; 1846 case WSDISPLAYIO_GETPARAM: 1847 if (bd == NULL) 1848 return -1; 1849 1850 switch (dp->param) { 1851 case WSDISPLAYIO_PARAM_BRIGHTNESS: 1852 dp->min = 0; 1853 dp->max = bd->props.max_brightness; 1854 dp->curval = bd->props.brightness; 1855 return (dp->max > dp->min) ? 0 : -1; 1856 } 1857 break; 1858 case WSDISPLAYIO_SETPARAM: 1859 if (bd == NULL || dp->curval > bd->props.max_brightness) 1860 return -1; 1861 1862 switch (dp->param) { 1863 case WSDISPLAYIO_PARAM_BRIGHTNESS: 1864 bd->props.brightness = dp->curval; 1865 backlight_update_status(bd); 1866 return 0; 1867 } 1868 break; 1869 } 1870 1871 return (-1); 1872 } 1873 1874 paddr_t 1875 amdgpu_wsmmap(void *v, off_t off, int prot) 1876 { 1877 return (-1); 1878 } 1879 1880 int 1881 amdgpu_alloc_screen(void *v, const struct wsscreen_descr *type, 1882 void **cookiep, int *curxp, int *curyp, uint32_t *attrp) 1883 { 1884 return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp); 1885 } 1886 1887 void 1888 amdgpu_free_screen(void *v, void *cookie) 1889 { 1890 return rasops_free_screen(v, cookie); 1891 } 1892 1893 int 1894 amdgpu_show_screen(void *v, void *cookie, int waitok, 1895 void (*cb)(void *, int, int), void *cbarg) 1896 { 1897 struct rasops_info *ri = v; 1898 struct amdgpu_device *adev = ri->ri_hw; 1899 1900 if (cookie == ri->ri_active) 1901 return (0); 1902 1903 adev->switchcb = cb; 1904 adev->switchcbarg = cbarg; 1905 adev->switchcookie = cookie; 1906 if (cb) { 1907 task_add(systq, &adev->switchtask); 1908 return (EAGAIN); 1909 } 1910 1911 amdgpu_doswitch(v); 1912 1913 return (0); 1914 } 1915 1916 void 1917 amdgpu_doswitch(void *v) 1918 { 1919 struct rasops_info *ri = v; 1920 struct amdgpu_device *adev = ri->ri_hw; 1921 struct amdgpu_crtc *amdgpu_crtc; 1922 int i, crtc; 1923 1924 rasops_show_screen(ri, adev->switchcookie, 0, NULL, NULL); 1925 drm_fb_helper_restore_fbdev_mode_unlocked((void *)adev->mode_info.rfbdev); 1926 1927 if (adev->switchcb) 1928 (adev->switchcb)(adev->switchcbarg, 0, 0); 1929 } 1930 1931 void 1932 amdgpu_enter_ddb(void *v, void *cookie) 1933 { 1934 struct rasops_info *ri = v; 1935 struct amdgpu_device *adev = ri->ri_hw; 1936 struct drm_fb_helper *fb_helper = (void *)adev->mode_info.rfbdev; 1937 1938 if (cookie == ri->ri_active) 1939 return; 1940 1941 rasops_show_screen(ri, cookie, 0, NULL, NULL); 1942 drm_fb_helper_debug_enter(fb_helper->fbdev); 1943 } 1944 1945 1946 void 1947 amdgpu_attachhook(struct device *self) 1948 { 1949 struct amdgpu_device *adev = (struct amdgpu_device *)self; 1950 struct drm_device *dev = adev->ddev; 1951 int r, acpi_status; 1952 1953 if (amdgpu_has_atpx() && 1954 (amdgpu_is_atpx_hybrid() || 1955 amdgpu_has_atpx_dgpu_power_cntl()) && 1956 ((adev->flags & AMD_IS_APU) == 0) && 1957 !pci_is_thunderbolt_attached(dev->pdev)) 1958 adev->flags |= AMD_IS_PX; 1959 1960 /* amdgpu_device_init should report only fatal error 1961 * like memory allocation failure or iomapping failure, 1962 * or memory manager initialization failure, it must 1963 * properly initialize the GPU MC controller and permit 1964 * VRAM allocation 1965 */ 1966 r = amdgpu_device_init(adev, dev, dev->pdev, adev->flags); 1967 if (r) { 1968 dev_err(&dev->pdev->dev, "Fatal error during GPU init\n"); 1969 goto out; 1970 } 1971 1972 if (amdgpu_device_supports_boco(dev) && 1973 (amdgpu_runtime_pm != 0)) /* enable runpm by default for boco */ 1974 adev->runpm = true; 1975 else if (amdgpu_device_supports_baco(dev) && 1976 (amdgpu_runtime_pm != 0) && 1977 (adev->asic_type >= CHIP_TOPAZ) && 1978 (adev->asic_type != CHIP_VEGA10) && 1979 (adev->asic_type != CHIP_VEGA20) && 1980 (adev->asic_type != CHIP_ARCTURUS)) /* enable runpm on VI+ */ 1981 adev->runpm = true; 1982 else if (amdgpu_device_supports_baco(dev) && 1983 (amdgpu_runtime_pm > 0)) /* enable runpm if runpm=1 on CI */ 1984 adev->runpm = true; 1985 1986 /* Call ACPI methods: require modeset init 1987 * but failure is not fatal 1988 */ 1989 if (!r) { 1990 acpi_status = amdgpu_acpi_init(adev); 1991 if (acpi_status) 1992 dev_dbg(&dev->pdev->dev, 1993 "Error during ACPI methods call\n"); 1994 } 1995 1996 if (adev->runpm) { 1997 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NEVER_SKIP); 1998 pm_runtime_use_autosuspend(dev->dev); 1999 pm_runtime_set_autosuspend_delay(dev->dev, 5000); 2000 pm_runtime_set_active(dev->dev); 2001 pm_runtime_allow(dev->dev); 2002 pm_runtime_mark_last_busy(dev->dev); 2003 pm_runtime_put_autosuspend(dev->dev); 2004 } 2005 { 2006 struct wsemuldisplaydev_attach_args aa; 2007 struct rasops_info *ri = &adev->ro; 2008 2009 task_set(&adev->switchtask, amdgpu_doswitch, ri); 2010 2011 if (ri->ri_bits == NULL) 2012 return; 2013 2014 ri->ri_flg = RI_CENTER | RI_VCONS | RI_WRONLY; 2015 rasops_init(ri, 160, 160); 2016 2017 ri->ri_hw = adev; 2018 2019 amdgpu_stdscreen.capabilities = ri->ri_caps; 2020 amdgpu_stdscreen.nrows = ri->ri_rows; 2021 amdgpu_stdscreen.ncols = ri->ri_cols; 2022 amdgpu_stdscreen.textops = &ri->ri_ops; 2023 amdgpu_stdscreen.fontwidth = ri->ri_font->fontwidth; 2024 amdgpu_stdscreen.fontheight = ri->ri_font->fontheight; 2025 2026 aa.console = adev->console; 2027 aa.primary = adev->primary; 2028 aa.scrdata = &amdgpu_screenlist; 2029 aa.accessops = &amdgpu_accessops; 2030 aa.accesscookie = ri; 2031 aa.defaultscreens = 0; 2032 2033 if (adev->console) { 2034 uint32_t defattr; 2035 2036 ri->ri_ops.pack_attr(ri->ri_active, 0, 0, 0, &defattr); 2037 wsdisplay_cnattach(&amdgpu_stdscreen, ri->ri_active, 2038 ri->ri_ccol, ri->ri_crow, defattr); 2039 } 2040 2041 /* 2042 * Now that we've taken over the console, disable decoding of 2043 * VGA legacy addresses, and opt out of arbitration. 2044 */ 2045 amdgpu_asic_set_vga_state(adev, false); 2046 pci_disable_legacy_vga(&adev->self); 2047 2048 printf("%s: %dx%d, %dbpp\n", adev->self.dv_xname, 2049 ri->ri_width, ri->ri_height, ri->ri_depth); 2050 2051 config_found_sm(&adev->self, &aa, wsemuldisplaydevprint, 2052 wsemuldisplaydevsubmatch); 2053 2054 /* 2055 * in linux via amdgpu_pci_probe -> drm_dev_register 2056 */ 2057 drm_dev_register(dev, adev->flags); 2058 } 2059 2060 out: 2061 if (r) { 2062 /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */ 2063 if (adev->runpm) 2064 pm_runtime_put_noidle(dev->dev); 2065 amdgpu_fatal_error = 1; 2066 amdgpu_forcedetach(adev); 2067 } 2068 } 2069 2070 /* from amdgpu_exit amdgpu_driver_unload_kms */ 2071 int 2072 amdgpu_detach(struct device *self, int flags) 2073 { 2074 struct amdgpu_device *adev = (struct amdgpu_device *)self; 2075 struct drm_device *dev = adev->ddev; 2076 2077 if (adev == NULL) 2078 return 0; 2079 2080 amdgpu_refcnt--; 2081 2082 if (amdgpu_refcnt == 0) 2083 amdgpu_amdkfd_fini(); 2084 2085 pci_intr_disestablish(adev->pc, adev->irqh); 2086 2087 amdgpu_unregister_gpu_instance(adev); 2088 2089 if (adev->runpm) { 2090 pm_runtime_get_sync(dev->dev); 2091 pm_runtime_forbid(dev->dev); 2092 } 2093 2094 amdgpu_acpi_fini(adev); 2095 2096 amdgpu_device_fini(adev); 2097 2098 if (amdgpu_refcnt == 0) { 2099 amdgpu_unregister_atpx_handler(); 2100 amdgpu_sync_fini(); 2101 amdgpu_fence_slab_fini(); 2102 2103 drm_sched_fence_slab_fini(); 2104 } 2105 2106 if (adev->ddev != NULL) { 2107 config_detach(adev->ddev->dev, flags); 2108 adev->ddev = NULL; 2109 } 2110 2111 return 0; 2112 } 2113 2114 int 2115 amdgpu_activate(struct device *self, int act) 2116 { 2117 struct amdgpu_device *adev = (struct amdgpu_device *)self; 2118 int rv = 0; 2119 2120 if (adev->ddev == NULL) 2121 return (0); 2122 2123 switch (act) { 2124 case DVACT_QUIESCE: 2125 rv = config_activate_children(self, act); 2126 amdgpu_device_suspend(adev->ddev, true); 2127 break; 2128 case DVACT_SUSPEND: 2129 break; 2130 case DVACT_RESUME: 2131 break; 2132 case DVACT_WAKEUP: 2133 amdgpu_device_resume(adev->ddev, true); 2134 rv = config_activate_children(self, act); 2135 break; 2136 } 2137 2138 return (rv); 2139 } 2140