1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28 29 #include "amdgpu.h" 30 #include <drm/drm_debugfs.h> 31 #include <drm/amdgpu_drm.h> 32 #include "amdgpu_sched.h" 33 #include "amdgpu_uvd.h" 34 #include "amdgpu_vce.h" 35 #include "atom.h" 36 37 #include <linux/vga_switcheroo.h> 38 #include <linux/slab.h> 39 #include <linux/uaccess.h> 40 #include <linux/pci.h> 41 #include <linux/pm_runtime.h> 42 #include "amdgpu_amdkfd.h" 43 #include "amdgpu_gem.h" 44 #include "amdgpu_display.h" 45 #include "amdgpu_ras.h" 46 47 void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev) 48 { 49 struct amdgpu_gpu_instance *gpu_instance; 50 int i; 51 52 mutex_lock(&mgpu_info.mutex); 53 54 for (i = 0; i < mgpu_info.num_gpu; i++) { 55 gpu_instance = &(mgpu_info.gpu_ins[i]); 56 if (gpu_instance->adev == adev) { 57 mgpu_info.gpu_ins[i] = 58 mgpu_info.gpu_ins[mgpu_info.num_gpu - 1]; 59 mgpu_info.num_gpu--; 60 if (adev->flags & AMD_IS_APU) 61 mgpu_info.num_apu--; 62 else 63 mgpu_info.num_dgpu--; 64 break; 65 } 66 } 67 68 mutex_unlock(&mgpu_info.mutex); 69 } 70 71 #include <drm/drm_pci.h> 72 #include <drm/drm_drv.h> 73 74 #include "vga.h" 75 76 #if NVGA > 0 77 #include <dev/ic/mc6845reg.h> 78 #include <dev/ic/pcdisplayvar.h> 79 #include <dev/ic/vgareg.h> 80 #include <dev/ic/vgavar.h> 81 82 extern int vga_console_attached; 83 #endif 84 85 #ifdef __amd64__ 86 #include "efifb.h" 87 #include <machine/biosvar.h> 88 #endif 89 90 #if NEFIFB > 0 91 #include <machine/efifbvar.h> 92 #endif 93 94 int amdgpu_probe(struct device *, void *, void *); 95 void amdgpu_attach(struct device *, struct device *, void *); 96 int amdgpu_detach(struct device *, int); 97 int amdgpu_activate(struct device *, int); 98 void amdgpu_attachhook(struct device *); 99 int amdgpu_forcedetach(struct amdgpu_device *); 100 101 bool amdgpu_msi_ok(struct amdgpu_device *); 102 103 extern const struct pci_device_id amdgpu_pciidlist[]; 104 extern struct drm_driver amdgpu_kms_driver; 105 extern int amdgpu_exp_hw_support; 106 107 /* 108 * set if the mountroot hook has a fatal error 109 * such as not being able to find the firmware 110 */ 111 int amdgpu_fatal_error; 112 113 struct cfattach amdgpu_ca = { 114 sizeof (struct amdgpu_device), amdgpu_probe, amdgpu_attach, 115 amdgpu_detach, amdgpu_activate 116 }; 117 118 struct cfdriver amdgpu_cd = { 119 NULL, "amdgpu", DV_DULL 120 }; 121 122 #ifdef __linux__ 123 /** 124 * amdgpu_driver_unload_kms - Main unload function for KMS. 125 * 126 * @dev: drm dev pointer 127 * 128 * This is the main unload function for KMS (all asics). 129 * Returns 0 on success. 130 */ 131 void amdgpu_driver_unload_kms(struct drm_device *dev) 132 { 133 struct amdgpu_device *adev = dev->dev_private; 134 135 if (adev == NULL) 136 return; 137 138 amdgpu_unregister_gpu_instance(adev); 139 140 if (adev->rmmio == NULL) 141 goto done_free; 142 143 if (adev->runpm) { 144 pm_runtime_get_sync(dev->dev); 145 pm_runtime_forbid(dev->dev); 146 } 147 148 amdgpu_acpi_fini(adev); 149 150 amdgpu_device_fini(adev); 151 152 done_free: 153 kfree(adev); 154 dev->dev_private = NULL; 155 } 156 #endif /* __linux__ */ 157 158 void amdgpu_register_gpu_instance(struct amdgpu_device *adev) 159 { 160 struct amdgpu_gpu_instance *gpu_instance; 161 162 mutex_lock(&mgpu_info.mutex); 163 164 if (mgpu_info.num_gpu >= MAX_GPU_INSTANCE) { 165 DRM_ERROR("Cannot register more gpu instance\n"); 166 mutex_unlock(&mgpu_info.mutex); 167 return; 168 } 169 170 gpu_instance = &(mgpu_info.gpu_ins[mgpu_info.num_gpu]); 171 gpu_instance->adev = adev; 172 gpu_instance->mgpu_fan_enabled = 0; 173 174 mgpu_info.num_gpu++; 175 if (adev->flags & AMD_IS_APU) 176 mgpu_info.num_apu++; 177 else 178 mgpu_info.num_dgpu++; 179 180 mutex_unlock(&mgpu_info.mutex); 181 } 182 183 #ifdef __linux__ 184 /** 185 * amdgpu_driver_load_kms - Main load function for KMS. 186 * 187 * @dev: drm dev pointer 188 * @flags: device flags 189 * 190 * This is the main load function for KMS (all asics). 191 * Returns 0 on success, error on failure. 192 */ 193 int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags) 194 { 195 struct amdgpu_device *adev; 196 int r, acpi_status; 197 198 adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL); 199 if (adev == NULL) { 200 return -ENOMEM; 201 } 202 dev->dev_private = (void *)adev; 203 204 if (amdgpu_has_atpx() && 205 (amdgpu_is_atpx_hybrid() || 206 amdgpu_has_atpx_dgpu_power_cntl()) && 207 ((flags & AMD_IS_APU) == 0) && 208 !pci_is_thunderbolt_attached(dev->pdev)) 209 flags |= AMD_IS_PX; 210 211 /* amdgpu_device_init should report only fatal error 212 * like memory allocation failure or iomapping failure, 213 * or memory manager initialization failure, it must 214 * properly initialize the GPU MC controller and permit 215 * VRAM allocation 216 */ 217 r = amdgpu_device_init(adev, dev, dev->pdev, flags); 218 if (r) { 219 dev_err(&dev->pdev->dev, "Fatal error during GPU init\n"); 220 goto out; 221 } 222 223 if (amdgpu_device_supports_boco(dev) && 224 (amdgpu_runtime_pm != 0)) /* enable runpm by default for boco */ 225 adev->runpm = true; 226 else if (amdgpu_device_supports_baco(dev) && 227 (amdgpu_runtime_pm != 0) && 228 (adev->asic_type >= CHIP_TOPAZ) && 229 (adev->asic_type != CHIP_VEGA10) && 230 (adev->asic_type != CHIP_VEGA20) && 231 (adev->asic_type != CHIP_ARCTURUS)) /* enable runpm on VI+ */ 232 adev->runpm = true; 233 else if (amdgpu_device_supports_baco(dev) && 234 (amdgpu_runtime_pm > 0)) /* enable runpm if runpm=1 on CI */ 235 adev->runpm = true; 236 237 /* Call ACPI methods: require modeset init 238 * but failure is not fatal 239 */ 240 if (!r) { 241 acpi_status = amdgpu_acpi_init(adev); 242 if (acpi_status) 243 dev_dbg(&dev->pdev->dev, 244 "Error during ACPI methods call\n"); 245 } 246 247 if (adev->runpm) { 248 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NEVER_SKIP); 249 pm_runtime_use_autosuspend(dev->dev); 250 pm_runtime_set_autosuspend_delay(dev->dev, 5000); 251 pm_runtime_set_active(dev->dev); 252 pm_runtime_allow(dev->dev); 253 pm_runtime_mark_last_busy(dev->dev); 254 pm_runtime_put_autosuspend(dev->dev); 255 } 256 257 out: 258 if (r) { 259 /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */ 260 if (adev->rmmio && adev->runpm) 261 pm_runtime_put_noidle(dev->dev); 262 amdgpu_driver_unload_kms(dev); 263 } 264 265 return r; 266 } 267 #endif /* __linux__ */ 268 269 static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info, 270 struct drm_amdgpu_query_fw *query_fw, 271 struct amdgpu_device *adev) 272 { 273 switch (query_fw->fw_type) { 274 case AMDGPU_INFO_FW_VCE: 275 fw_info->ver = adev->vce.fw_version; 276 fw_info->feature = adev->vce.fb_version; 277 break; 278 case AMDGPU_INFO_FW_UVD: 279 fw_info->ver = adev->uvd.fw_version; 280 fw_info->feature = 0; 281 break; 282 case AMDGPU_INFO_FW_VCN: 283 fw_info->ver = adev->vcn.fw_version; 284 fw_info->feature = 0; 285 break; 286 case AMDGPU_INFO_FW_GMC: 287 fw_info->ver = adev->gmc.fw_version; 288 fw_info->feature = 0; 289 break; 290 case AMDGPU_INFO_FW_GFX_ME: 291 fw_info->ver = adev->gfx.me_fw_version; 292 fw_info->feature = adev->gfx.me_feature_version; 293 break; 294 case AMDGPU_INFO_FW_GFX_PFP: 295 fw_info->ver = adev->gfx.pfp_fw_version; 296 fw_info->feature = adev->gfx.pfp_feature_version; 297 break; 298 case AMDGPU_INFO_FW_GFX_CE: 299 fw_info->ver = adev->gfx.ce_fw_version; 300 fw_info->feature = adev->gfx.ce_feature_version; 301 break; 302 case AMDGPU_INFO_FW_GFX_RLC: 303 fw_info->ver = adev->gfx.rlc_fw_version; 304 fw_info->feature = adev->gfx.rlc_feature_version; 305 break; 306 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL: 307 fw_info->ver = adev->gfx.rlc_srlc_fw_version; 308 fw_info->feature = adev->gfx.rlc_srlc_feature_version; 309 break; 310 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM: 311 fw_info->ver = adev->gfx.rlc_srlg_fw_version; 312 fw_info->feature = adev->gfx.rlc_srlg_feature_version; 313 break; 314 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM: 315 fw_info->ver = adev->gfx.rlc_srls_fw_version; 316 fw_info->feature = adev->gfx.rlc_srls_feature_version; 317 break; 318 case AMDGPU_INFO_FW_GFX_MEC: 319 if (query_fw->index == 0) { 320 fw_info->ver = adev->gfx.mec_fw_version; 321 fw_info->feature = adev->gfx.mec_feature_version; 322 } else if (query_fw->index == 1) { 323 fw_info->ver = adev->gfx.mec2_fw_version; 324 fw_info->feature = adev->gfx.mec2_feature_version; 325 } else 326 return -EINVAL; 327 break; 328 case AMDGPU_INFO_FW_SMC: 329 fw_info->ver = adev->pm.fw_version; 330 fw_info->feature = 0; 331 break; 332 case AMDGPU_INFO_FW_TA: 333 if (query_fw->index > 1) 334 return -EINVAL; 335 if (query_fw->index == 0) { 336 fw_info->ver = adev->psp.ta_fw_version; 337 fw_info->feature = adev->psp.ta_xgmi_ucode_version; 338 } else { 339 fw_info->ver = adev->psp.ta_fw_version; 340 fw_info->feature = adev->psp.ta_ras_ucode_version; 341 } 342 break; 343 case AMDGPU_INFO_FW_SDMA: 344 if (query_fw->index >= adev->sdma.num_instances) 345 return -EINVAL; 346 fw_info->ver = adev->sdma.instance[query_fw->index].fw_version; 347 fw_info->feature = adev->sdma.instance[query_fw->index].feature_version; 348 break; 349 case AMDGPU_INFO_FW_SOS: 350 fw_info->ver = adev->psp.sos_fw_version; 351 fw_info->feature = adev->psp.sos_feature_version; 352 break; 353 case AMDGPU_INFO_FW_ASD: 354 fw_info->ver = adev->psp.asd_fw_version; 355 fw_info->feature = adev->psp.asd_feature_version; 356 break; 357 case AMDGPU_INFO_FW_DMCU: 358 fw_info->ver = adev->dm.dmcu_fw_version; 359 fw_info->feature = 0; 360 break; 361 case AMDGPU_INFO_FW_DMCUB: 362 fw_info->ver = adev->dm.dmcub_fw_version; 363 fw_info->feature = 0; 364 break; 365 default: 366 return -EINVAL; 367 } 368 return 0; 369 } 370 371 static int amdgpu_hw_ip_info(struct amdgpu_device *adev, 372 struct drm_amdgpu_info *info, 373 struct drm_amdgpu_info_hw_ip *result) 374 { 375 uint32_t ib_start_alignment = 0; 376 uint32_t ib_size_alignment = 0; 377 enum amd_ip_block_type type; 378 unsigned int num_rings = 0; 379 unsigned int i, j; 380 381 if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT) 382 return -EINVAL; 383 384 switch (info->query_hw_ip.type) { 385 case AMDGPU_HW_IP_GFX: 386 type = AMD_IP_BLOCK_TYPE_GFX; 387 for (i = 0; i < adev->gfx.num_gfx_rings; i++) 388 if (adev->gfx.gfx_ring[i].sched.ready) 389 ++num_rings; 390 ib_start_alignment = 32; 391 ib_size_alignment = 32; 392 break; 393 case AMDGPU_HW_IP_COMPUTE: 394 type = AMD_IP_BLOCK_TYPE_GFX; 395 for (i = 0; i < adev->gfx.num_compute_rings; i++) 396 if (adev->gfx.compute_ring[i].sched.ready) 397 ++num_rings; 398 ib_start_alignment = 32; 399 ib_size_alignment = 32; 400 break; 401 case AMDGPU_HW_IP_DMA: 402 type = AMD_IP_BLOCK_TYPE_SDMA; 403 for (i = 0; i < adev->sdma.num_instances; i++) 404 if (adev->sdma.instance[i].ring.sched.ready) 405 ++num_rings; 406 ib_start_alignment = 256; 407 ib_size_alignment = 4; 408 break; 409 case AMDGPU_HW_IP_UVD: 410 type = AMD_IP_BLOCK_TYPE_UVD; 411 for (i = 0; i < adev->uvd.num_uvd_inst; i++) { 412 if (adev->uvd.harvest_config & (1 << i)) 413 continue; 414 415 if (adev->uvd.inst[i].ring.sched.ready) 416 ++num_rings; 417 } 418 ib_start_alignment = 64; 419 ib_size_alignment = 64; 420 break; 421 case AMDGPU_HW_IP_VCE: 422 type = AMD_IP_BLOCK_TYPE_VCE; 423 for (i = 0; i < adev->vce.num_rings; i++) 424 if (adev->vce.ring[i].sched.ready) 425 ++num_rings; 426 ib_start_alignment = 4; 427 ib_size_alignment = 1; 428 break; 429 case AMDGPU_HW_IP_UVD_ENC: 430 type = AMD_IP_BLOCK_TYPE_UVD; 431 for (i = 0; i < adev->uvd.num_uvd_inst; i++) { 432 if (adev->uvd.harvest_config & (1 << i)) 433 continue; 434 435 for (j = 0; j < adev->uvd.num_enc_rings; j++) 436 if (adev->uvd.inst[i].ring_enc[j].sched.ready) 437 ++num_rings; 438 } 439 ib_start_alignment = 64; 440 ib_size_alignment = 64; 441 break; 442 case AMDGPU_HW_IP_VCN_DEC: 443 type = AMD_IP_BLOCK_TYPE_VCN; 444 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 445 if (adev->uvd.harvest_config & (1 << i)) 446 continue; 447 448 if (adev->vcn.inst[i].ring_dec.sched.ready) 449 ++num_rings; 450 } 451 ib_start_alignment = 16; 452 ib_size_alignment = 16; 453 break; 454 case AMDGPU_HW_IP_VCN_ENC: 455 type = AMD_IP_BLOCK_TYPE_VCN; 456 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 457 if (adev->uvd.harvest_config & (1 << i)) 458 continue; 459 460 for (j = 0; j < adev->vcn.num_enc_rings; j++) 461 if (adev->vcn.inst[i].ring_enc[j].sched.ready) 462 ++num_rings; 463 } 464 ib_start_alignment = 64; 465 ib_size_alignment = 1; 466 break; 467 case AMDGPU_HW_IP_VCN_JPEG: 468 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ? 469 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN; 470 471 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) { 472 if (adev->jpeg.harvest_config & (1 << i)) 473 continue; 474 475 if (adev->jpeg.inst[i].ring_dec.sched.ready) 476 ++num_rings; 477 } 478 ib_start_alignment = 16; 479 ib_size_alignment = 16; 480 break; 481 default: 482 return -EINVAL; 483 } 484 485 for (i = 0; i < adev->num_ip_blocks; i++) 486 if (adev->ip_blocks[i].version->type == type && 487 adev->ip_blocks[i].status.valid) 488 break; 489 490 if (i == adev->num_ip_blocks) 491 return 0; 492 493 num_rings = min(amdgpu_ctx_num_entities[info->query_hw_ip.type], 494 num_rings); 495 496 result->hw_ip_version_major = adev->ip_blocks[i].version->major; 497 result->hw_ip_version_minor = adev->ip_blocks[i].version->minor; 498 result->capabilities_flags = 0; 499 result->available_rings = (1 << num_rings) - 1; 500 result->ib_start_alignment = ib_start_alignment; 501 result->ib_size_alignment = ib_size_alignment; 502 return 0; 503 } 504 505 /* 506 * Userspace get information ioctl 507 */ 508 /** 509 * amdgpu_info_ioctl - answer a device specific request. 510 * 511 * @adev: amdgpu device pointer 512 * @data: request object 513 * @filp: drm filp 514 * 515 * This function is used to pass device specific parameters to the userspace 516 * drivers. Examples include: pci device id, pipeline parms, tiling params, 517 * etc. (all asics). 518 * Returns 0 on success, -EINVAL on failure. 519 */ 520 static int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 521 { 522 struct amdgpu_device *adev = dev->dev_private; 523 struct drm_amdgpu_info *info = data; 524 struct amdgpu_mode_info *minfo = &adev->mode_info; 525 void __user *out = (void __user *)(uintptr_t)info->return_pointer; 526 uint32_t size = info->return_size; 527 struct drm_crtc *crtc; 528 uint32_t ui32 = 0; 529 uint64_t ui64 = 0; 530 int i, found; 531 int ui32_size = sizeof(ui32); 532 533 if (!info->return_size || !info->return_pointer) 534 return -EINVAL; 535 536 switch (info->query) { 537 case AMDGPU_INFO_ACCEL_WORKING: 538 ui32 = adev->accel_working; 539 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 540 case AMDGPU_INFO_CRTC_FROM_ID: 541 for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) { 542 crtc = (struct drm_crtc *)minfo->crtcs[i]; 543 if (crtc && crtc->base.id == info->mode_crtc.id) { 544 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 545 ui32 = amdgpu_crtc->crtc_id; 546 found = 1; 547 break; 548 } 549 } 550 if (!found) { 551 DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id); 552 return -EINVAL; 553 } 554 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 555 case AMDGPU_INFO_HW_IP_INFO: { 556 struct drm_amdgpu_info_hw_ip ip = {}; 557 int ret; 558 559 ret = amdgpu_hw_ip_info(adev, info, &ip); 560 if (ret) 561 return ret; 562 563 ret = copy_to_user(out, &ip, min((size_t)size, sizeof(ip))); 564 return ret ? -EFAULT : 0; 565 } 566 case AMDGPU_INFO_HW_IP_COUNT: { 567 enum amd_ip_block_type type; 568 uint32_t count = 0; 569 570 switch (info->query_hw_ip.type) { 571 case AMDGPU_HW_IP_GFX: 572 type = AMD_IP_BLOCK_TYPE_GFX; 573 break; 574 case AMDGPU_HW_IP_COMPUTE: 575 type = AMD_IP_BLOCK_TYPE_GFX; 576 break; 577 case AMDGPU_HW_IP_DMA: 578 type = AMD_IP_BLOCK_TYPE_SDMA; 579 break; 580 case AMDGPU_HW_IP_UVD: 581 type = AMD_IP_BLOCK_TYPE_UVD; 582 break; 583 case AMDGPU_HW_IP_VCE: 584 type = AMD_IP_BLOCK_TYPE_VCE; 585 break; 586 case AMDGPU_HW_IP_UVD_ENC: 587 type = AMD_IP_BLOCK_TYPE_UVD; 588 break; 589 case AMDGPU_HW_IP_VCN_DEC: 590 case AMDGPU_HW_IP_VCN_ENC: 591 type = AMD_IP_BLOCK_TYPE_VCN; 592 break; 593 case AMDGPU_HW_IP_VCN_JPEG: 594 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ? 595 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN; 596 break; 597 default: 598 return -EINVAL; 599 } 600 601 for (i = 0; i < adev->num_ip_blocks; i++) 602 if (adev->ip_blocks[i].version->type == type && 603 adev->ip_blocks[i].status.valid && 604 count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT) 605 count++; 606 607 return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0; 608 } 609 case AMDGPU_INFO_TIMESTAMP: 610 ui64 = amdgpu_gfx_get_gpu_clock_counter(adev); 611 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 612 case AMDGPU_INFO_FW_VERSION: { 613 struct drm_amdgpu_info_firmware fw_info; 614 int ret; 615 616 /* We only support one instance of each IP block right now. */ 617 if (info->query_fw.ip_instance != 0) 618 return -EINVAL; 619 620 ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev); 621 if (ret) 622 return ret; 623 624 return copy_to_user(out, &fw_info, 625 min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0; 626 } 627 case AMDGPU_INFO_NUM_BYTES_MOVED: 628 ui64 = atomic64_read(&adev->num_bytes_moved); 629 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 630 case AMDGPU_INFO_NUM_EVICTIONS: 631 ui64 = atomic64_read(&adev->num_evictions); 632 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 633 case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS: 634 ui64 = atomic64_read(&adev->num_vram_cpu_page_faults); 635 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 636 case AMDGPU_INFO_VRAM_USAGE: 637 ui64 = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 638 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 639 case AMDGPU_INFO_VIS_VRAM_USAGE: 640 ui64 = amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 641 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 642 case AMDGPU_INFO_GTT_USAGE: 643 ui64 = amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]); 644 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0; 645 case AMDGPU_INFO_GDS_CONFIG: { 646 struct drm_amdgpu_info_gds gds_info; 647 648 memset(&gds_info, 0, sizeof(gds_info)); 649 gds_info.compute_partition_size = adev->gds.gds_size; 650 gds_info.gds_total_size = adev->gds.gds_size; 651 gds_info.gws_per_compute_partition = adev->gds.gws_size; 652 gds_info.oa_per_compute_partition = adev->gds.oa_size; 653 return copy_to_user(out, &gds_info, 654 min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0; 655 } 656 case AMDGPU_INFO_VRAM_GTT: { 657 struct drm_amdgpu_info_vram_gtt vram_gtt; 658 659 vram_gtt.vram_size = adev->gmc.real_vram_size - 660 atomic64_read(&adev->vram_pin_size) - 661 AMDGPU_VM_RESERVED_VRAM; 662 vram_gtt.vram_cpu_accessible_size = 663 min(adev->gmc.visible_vram_size - 664 atomic64_read(&adev->visible_pin_size), 665 vram_gtt.vram_size); 666 vram_gtt.gtt_size = adev->mman.bdev.man[TTM_PL_TT].size; 667 vram_gtt.gtt_size *= PAGE_SIZE; 668 vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size); 669 return copy_to_user(out, &vram_gtt, 670 min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0; 671 } 672 case AMDGPU_INFO_MEMORY: { 673 struct drm_amdgpu_memory_info mem; 674 675 memset(&mem, 0, sizeof(mem)); 676 mem.vram.total_heap_size = adev->gmc.real_vram_size; 677 mem.vram.usable_heap_size = adev->gmc.real_vram_size - 678 atomic64_read(&adev->vram_pin_size) - 679 AMDGPU_VM_RESERVED_VRAM; 680 mem.vram.heap_usage = 681 amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 682 mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4; 683 684 mem.cpu_accessible_vram.total_heap_size = 685 adev->gmc.visible_vram_size; 686 mem.cpu_accessible_vram.usable_heap_size = 687 min(adev->gmc.visible_vram_size - 688 atomic64_read(&adev->visible_pin_size), 689 mem.vram.usable_heap_size); 690 mem.cpu_accessible_vram.heap_usage = 691 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]); 692 mem.cpu_accessible_vram.max_allocation = 693 mem.cpu_accessible_vram.usable_heap_size * 3 / 4; 694 695 mem.gtt.total_heap_size = adev->mman.bdev.man[TTM_PL_TT].size; 696 mem.gtt.total_heap_size *= PAGE_SIZE; 697 mem.gtt.usable_heap_size = mem.gtt.total_heap_size - 698 atomic64_read(&adev->gart_pin_size); 699 mem.gtt.heap_usage = 700 amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]); 701 mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4; 702 703 return copy_to_user(out, &mem, 704 min((size_t)size, sizeof(mem))) 705 ? -EFAULT : 0; 706 } 707 case AMDGPU_INFO_READ_MMR_REG: { 708 unsigned n, alloc_size; 709 uint32_t *regs; 710 unsigned se_num = (info->read_mmr_reg.instance >> 711 AMDGPU_INFO_MMR_SE_INDEX_SHIFT) & 712 AMDGPU_INFO_MMR_SE_INDEX_MASK; 713 unsigned sh_num = (info->read_mmr_reg.instance >> 714 AMDGPU_INFO_MMR_SH_INDEX_SHIFT) & 715 AMDGPU_INFO_MMR_SH_INDEX_MASK; 716 717 /* set full masks if the userspace set all bits 718 * in the bitfields */ 719 if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK) 720 se_num = 0xffffffff; 721 if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK) 722 sh_num = 0xffffffff; 723 724 if (info->read_mmr_reg.count > 128) 725 return -EINVAL; 726 727 regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL); 728 if (!regs) 729 return -ENOMEM; 730 alloc_size = info->read_mmr_reg.count * sizeof(*regs); 731 732 amdgpu_gfx_off_ctrl(adev, false); 733 for (i = 0; i < info->read_mmr_reg.count; i++) { 734 if (amdgpu_asic_read_register(adev, se_num, sh_num, 735 info->read_mmr_reg.dword_offset + i, 736 ®s[i])) { 737 DRM_DEBUG_KMS("unallowed offset %#x\n", 738 info->read_mmr_reg.dword_offset + i); 739 kfree(regs); 740 amdgpu_gfx_off_ctrl(adev, true); 741 return -EFAULT; 742 } 743 } 744 amdgpu_gfx_off_ctrl(adev, true); 745 n = copy_to_user(out, regs, min(size, alloc_size)); 746 kfree(regs); 747 return n ? -EFAULT : 0; 748 } 749 case AMDGPU_INFO_DEV_INFO: { 750 struct drm_amdgpu_info_device dev_info; 751 uint64_t vm_size; 752 753 memset(&dev_info, 0, sizeof(dev_info)); 754 dev_info.device_id = dev->pdev->device; 755 dev_info.chip_rev = adev->rev_id; 756 dev_info.external_rev = adev->external_rev_id; 757 dev_info.pci_rev = dev->pdev->revision; 758 dev_info.family = adev->family; 759 dev_info.num_shader_engines = adev->gfx.config.max_shader_engines; 760 dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se; 761 /* return all clocks in KHz */ 762 dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10; 763 if (adev->pm.dpm_enabled) { 764 dev_info.max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10; 765 dev_info.max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10; 766 } else { 767 dev_info.max_engine_clock = adev->clock.default_sclk * 10; 768 dev_info.max_memory_clock = adev->clock.default_mclk * 10; 769 } 770 dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask; 771 dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se * 772 adev->gfx.config.max_shader_engines; 773 dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts; 774 dev_info._pad = 0; 775 dev_info.ids_flags = 0; 776 if (adev->flags & AMD_IS_APU) 777 dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION; 778 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) 779 dev_info.ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION; 780 781 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE; 782 vm_size -= AMDGPU_VA_RESERVED_SIZE; 783 784 /* Older VCE FW versions are buggy and can handle only 40bits */ 785 if (adev->vce.fw_version && 786 adev->vce.fw_version < AMDGPU_VCE_FW_53_45) 787 vm_size = min(vm_size, 1ULL << 40); 788 789 dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE; 790 dev_info.virtual_address_max = 791 min(vm_size, AMDGPU_GMC_HOLE_START); 792 793 if (vm_size > AMDGPU_GMC_HOLE_START) { 794 dev_info.high_va_offset = AMDGPU_GMC_HOLE_END; 795 dev_info.high_va_max = AMDGPU_GMC_HOLE_END | vm_size; 796 } 797 dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE); 798 dev_info.pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE; 799 dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE; 800 dev_info.cu_active_number = adev->gfx.cu_info.number; 801 dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask; 802 dev_info.ce_ram_size = adev->gfx.ce_ram_size; 803 memcpy(&dev_info.cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0], 804 sizeof(adev->gfx.cu_info.ao_cu_bitmap)); 805 memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0], 806 sizeof(adev->gfx.cu_info.bitmap)); 807 dev_info.vram_type = adev->gmc.vram_type; 808 dev_info.vram_bit_width = adev->gmc.vram_width; 809 dev_info.vce_harvest_config = adev->vce.harvest_config; 810 dev_info.gc_double_offchip_lds_buf = 811 adev->gfx.config.double_offchip_lds_buf; 812 dev_info.wave_front_size = adev->gfx.cu_info.wave_front_size; 813 dev_info.num_shader_visible_vgprs = adev->gfx.config.max_gprs; 814 dev_info.num_cu_per_sh = adev->gfx.config.max_cu_per_sh; 815 dev_info.num_tcc_blocks = adev->gfx.config.max_texture_channel_caches; 816 dev_info.gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth; 817 dev_info.gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth; 818 dev_info.max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads; 819 820 if (adev->family >= AMDGPU_FAMILY_NV) 821 dev_info.pa_sc_tile_steering_override = 822 adev->gfx.config.pa_sc_tile_steering_override; 823 824 dev_info.tcc_disabled_mask = adev->gfx.config.tcc_disabled_mask; 825 826 return copy_to_user(out, &dev_info, 827 min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0; 828 } 829 case AMDGPU_INFO_VCE_CLOCK_TABLE: { 830 unsigned i; 831 struct drm_amdgpu_info_vce_clock_table vce_clk_table = {}; 832 struct amd_vce_state *vce_state; 833 834 for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) { 835 vce_state = amdgpu_dpm_get_vce_clock_state(adev, i); 836 if (vce_state) { 837 vce_clk_table.entries[i].sclk = vce_state->sclk; 838 vce_clk_table.entries[i].mclk = vce_state->mclk; 839 vce_clk_table.entries[i].eclk = vce_state->evclk; 840 vce_clk_table.num_valid_entries++; 841 } 842 } 843 844 return copy_to_user(out, &vce_clk_table, 845 min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0; 846 } 847 case AMDGPU_INFO_VBIOS: { 848 uint32_t bios_size = adev->bios_size; 849 850 switch (info->vbios_info.type) { 851 case AMDGPU_INFO_VBIOS_SIZE: 852 return copy_to_user(out, &bios_size, 853 min((size_t)size, sizeof(bios_size))) 854 ? -EFAULT : 0; 855 case AMDGPU_INFO_VBIOS_IMAGE: { 856 uint8_t *bios; 857 uint32_t bios_offset = info->vbios_info.offset; 858 859 if (bios_offset >= bios_size) 860 return -EINVAL; 861 862 bios = adev->bios + bios_offset; 863 return copy_to_user(out, bios, 864 min((size_t)size, (size_t)(bios_size - bios_offset))) 865 ? -EFAULT : 0; 866 } 867 default: 868 DRM_DEBUG_KMS("Invalid request %d\n", 869 info->vbios_info.type); 870 return -EINVAL; 871 } 872 } 873 case AMDGPU_INFO_NUM_HANDLES: { 874 struct drm_amdgpu_info_num_handles handle; 875 876 switch (info->query_hw_ip.type) { 877 case AMDGPU_HW_IP_UVD: 878 /* Starting Polaris, we support unlimited UVD handles */ 879 if (adev->asic_type < CHIP_POLARIS10) { 880 handle.uvd_max_handles = adev->uvd.max_handles; 881 handle.uvd_used_handles = amdgpu_uvd_used_handles(adev); 882 883 return copy_to_user(out, &handle, 884 min((size_t)size, sizeof(handle))) ? -EFAULT : 0; 885 } else { 886 return -ENODATA; 887 } 888 889 break; 890 default: 891 return -EINVAL; 892 } 893 } 894 case AMDGPU_INFO_SENSOR: { 895 if (!adev->pm.dpm_enabled) 896 return -ENOENT; 897 898 switch (info->sensor_info.type) { 899 case AMDGPU_INFO_SENSOR_GFX_SCLK: 900 /* get sclk in Mhz */ 901 if (amdgpu_dpm_read_sensor(adev, 902 AMDGPU_PP_SENSOR_GFX_SCLK, 903 (void *)&ui32, &ui32_size)) { 904 return -EINVAL; 905 } 906 ui32 /= 100; 907 break; 908 case AMDGPU_INFO_SENSOR_GFX_MCLK: 909 /* get mclk in Mhz */ 910 if (amdgpu_dpm_read_sensor(adev, 911 AMDGPU_PP_SENSOR_GFX_MCLK, 912 (void *)&ui32, &ui32_size)) { 913 return -EINVAL; 914 } 915 ui32 /= 100; 916 break; 917 case AMDGPU_INFO_SENSOR_GPU_TEMP: 918 /* get temperature in millidegrees C */ 919 if (amdgpu_dpm_read_sensor(adev, 920 AMDGPU_PP_SENSOR_GPU_TEMP, 921 (void *)&ui32, &ui32_size)) { 922 return -EINVAL; 923 } 924 break; 925 case AMDGPU_INFO_SENSOR_GPU_LOAD: 926 /* get GPU load */ 927 if (amdgpu_dpm_read_sensor(adev, 928 AMDGPU_PP_SENSOR_GPU_LOAD, 929 (void *)&ui32, &ui32_size)) { 930 return -EINVAL; 931 } 932 break; 933 case AMDGPU_INFO_SENSOR_GPU_AVG_POWER: 934 /* get average GPU power */ 935 if (amdgpu_dpm_read_sensor(adev, 936 AMDGPU_PP_SENSOR_GPU_POWER, 937 (void *)&ui32, &ui32_size)) { 938 return -EINVAL; 939 } 940 ui32 >>= 8; 941 break; 942 case AMDGPU_INFO_SENSOR_VDDNB: 943 /* get VDDNB in millivolts */ 944 if (amdgpu_dpm_read_sensor(adev, 945 AMDGPU_PP_SENSOR_VDDNB, 946 (void *)&ui32, &ui32_size)) { 947 return -EINVAL; 948 } 949 break; 950 case AMDGPU_INFO_SENSOR_VDDGFX: 951 /* get VDDGFX in millivolts */ 952 if (amdgpu_dpm_read_sensor(adev, 953 AMDGPU_PP_SENSOR_VDDGFX, 954 (void *)&ui32, &ui32_size)) { 955 return -EINVAL; 956 } 957 break; 958 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK: 959 /* get stable pstate sclk in Mhz */ 960 if (amdgpu_dpm_read_sensor(adev, 961 AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, 962 (void *)&ui32, &ui32_size)) { 963 return -EINVAL; 964 } 965 ui32 /= 100; 966 break; 967 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK: 968 /* get stable pstate mclk in Mhz */ 969 if (amdgpu_dpm_read_sensor(adev, 970 AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, 971 (void *)&ui32, &ui32_size)) { 972 return -EINVAL; 973 } 974 ui32 /= 100; 975 break; 976 default: 977 DRM_DEBUG_KMS("Invalid request %d\n", 978 info->sensor_info.type); 979 return -EINVAL; 980 } 981 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 982 } 983 case AMDGPU_INFO_VRAM_LOST_COUNTER: 984 ui32 = atomic_read(&adev->vram_lost_counter); 985 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0; 986 case AMDGPU_INFO_RAS_ENABLED_FEATURES: { 987 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 988 uint64_t ras_mask; 989 990 if (!ras) 991 return -EINVAL; 992 ras_mask = (uint64_t)ras->supported << 32 | ras->features; 993 994 return copy_to_user(out, &ras_mask, 995 min_t(u64, size, sizeof(ras_mask))) ? 996 -EFAULT : 0; 997 } 998 default: 999 DRM_DEBUG_KMS("Invalid request %d\n", info->query); 1000 return -EINVAL; 1001 } 1002 return 0; 1003 } 1004 1005 1006 /* 1007 * Outdated mess for old drm with Xorg being in charge (void function now). 1008 */ 1009 /** 1010 * amdgpu_driver_lastclose_kms - drm callback for last close 1011 * 1012 * @dev: drm dev pointer 1013 * 1014 * Switch vga_switcheroo state after last close (all asics). 1015 */ 1016 void amdgpu_driver_lastclose_kms(struct drm_device *dev) 1017 { 1018 drm_fb_helper_lastclose(dev); 1019 vga_switcheroo_process_delayed_switch(); 1020 } 1021 1022 /** 1023 * amdgpu_driver_open_kms - drm callback for open 1024 * 1025 * @dev: drm dev pointer 1026 * @file_priv: drm file 1027 * 1028 * On device open, init vm on cayman+ (all asics). 1029 * Returns 0 on success, error on failure. 1030 */ 1031 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv) 1032 { 1033 struct amdgpu_device *adev = dev->dev_private; 1034 struct amdgpu_fpriv *fpriv; 1035 int r, pasid; 1036 1037 /* Ensure IB tests are run on ring */ 1038 flush_delayed_work(&adev->delayed_init_work); 1039 1040 1041 if (amdgpu_ras_intr_triggered()) { 1042 DRM_ERROR("RAS Intr triggered, device disabled!!"); 1043 return -EHWPOISON; 1044 } 1045 1046 file_priv->driver_priv = NULL; 1047 1048 r = pm_runtime_get_sync(dev->dev); 1049 if (r < 0) 1050 return r; 1051 1052 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL); 1053 if (unlikely(!fpriv)) { 1054 r = -ENOMEM; 1055 goto out_suspend; 1056 } 1057 1058 pasid = amdgpu_pasid_alloc(16); 1059 if (pasid < 0) { 1060 dev_warn(adev->dev, "No more PASIDs available!"); 1061 pasid = 0; 1062 } 1063 r = amdgpu_vm_init(adev, &fpriv->vm, AMDGPU_VM_CONTEXT_GFX, pasid); 1064 if (r) 1065 goto error_pasid; 1066 1067 fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL); 1068 if (!fpriv->prt_va) { 1069 r = -ENOMEM; 1070 goto error_vm; 1071 } 1072 1073 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 1074 uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK; 1075 1076 r = amdgpu_map_static_csa(adev, &fpriv->vm, adev->virt.csa_obj, 1077 &fpriv->csa_va, csa_addr, AMDGPU_CSA_SIZE); 1078 if (r) 1079 goto error_vm; 1080 } 1081 1082 rw_init(&fpriv->bo_list_lock, "agbo"); 1083 idr_init(&fpriv->bo_list_handles); 1084 1085 amdgpu_ctx_mgr_init(&fpriv->ctx_mgr); 1086 1087 file_priv->driver_priv = fpriv; 1088 goto out_suspend; 1089 1090 error_vm: 1091 amdgpu_vm_fini(adev, &fpriv->vm); 1092 1093 error_pasid: 1094 if (pasid) 1095 amdgpu_pasid_free(pasid); 1096 1097 kfree(fpriv); 1098 1099 out_suspend: 1100 pm_runtime_mark_last_busy(dev->dev); 1101 pm_runtime_put_autosuspend(dev->dev); 1102 1103 return r; 1104 } 1105 1106 /** 1107 * amdgpu_driver_postclose_kms - drm callback for post close 1108 * 1109 * @dev: drm dev pointer 1110 * @file_priv: drm file 1111 * 1112 * On device post close, tear down vm on cayman+ (all asics). 1113 */ 1114 void amdgpu_driver_postclose_kms(struct drm_device *dev, 1115 struct drm_file *file_priv) 1116 { 1117 struct amdgpu_device *adev = dev->dev_private; 1118 struct amdgpu_fpriv *fpriv = file_priv->driver_priv; 1119 struct amdgpu_bo_list *list; 1120 struct amdgpu_bo *pd; 1121 unsigned int pasid; 1122 int handle; 1123 1124 if (!fpriv) 1125 return; 1126 1127 pm_runtime_get_sync(dev->dev); 1128 1129 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL) 1130 amdgpu_uvd_free_handles(adev, file_priv); 1131 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL) 1132 amdgpu_vce_free_handles(adev, file_priv); 1133 1134 amdgpu_vm_bo_rmv(adev, fpriv->prt_va); 1135 1136 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 1137 /* TODO: how to handle reserve failure */ 1138 BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true)); 1139 amdgpu_vm_bo_rmv(adev, fpriv->csa_va); 1140 fpriv->csa_va = NULL; 1141 amdgpu_bo_unreserve(adev->virt.csa_obj); 1142 } 1143 1144 pasid = fpriv->vm.pasid; 1145 pd = amdgpu_bo_ref(fpriv->vm.root.base.bo); 1146 1147 amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr); 1148 amdgpu_vm_fini(adev, &fpriv->vm); 1149 1150 if (pasid) 1151 amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid); 1152 amdgpu_bo_unref(&pd); 1153 1154 idr_for_each_entry(&fpriv->bo_list_handles, list, handle) 1155 amdgpu_bo_list_put(list); 1156 1157 idr_destroy(&fpriv->bo_list_handles); 1158 mutex_destroy(&fpriv->bo_list_lock); 1159 1160 kfree(fpriv); 1161 file_priv->driver_priv = NULL; 1162 1163 pm_runtime_mark_last_busy(dev->dev); 1164 pm_runtime_put_autosuspend(dev->dev); 1165 } 1166 1167 /* 1168 * VBlank related functions. 1169 */ 1170 /** 1171 * amdgpu_get_vblank_counter_kms - get frame count 1172 * 1173 * @crtc: crtc to get the frame count from 1174 * 1175 * Gets the frame count on the requested crtc (all asics). 1176 * Returns frame count on success, -EINVAL on failure. 1177 */ 1178 u32 amdgpu_get_vblank_counter_kms(struct drm_crtc *crtc) 1179 { 1180 struct drm_device *dev = crtc->dev; 1181 unsigned int pipe = crtc->index; 1182 struct amdgpu_device *adev = dev->dev_private; 1183 int vpos, hpos, stat; 1184 u32 count; 1185 1186 if (pipe >= adev->mode_info.num_crtc) { 1187 DRM_ERROR("Invalid crtc %u\n", pipe); 1188 return -EINVAL; 1189 } 1190 1191 /* The hw increments its frame counter at start of vsync, not at start 1192 * of vblank, as is required by DRM core vblank counter handling. 1193 * Cook the hw count here to make it appear to the caller as if it 1194 * incremented at start of vblank. We measure distance to start of 1195 * vblank in vpos. vpos therefore will be >= 0 between start of vblank 1196 * and start of vsync, so vpos >= 0 means to bump the hw frame counter 1197 * result by 1 to give the proper appearance to caller. 1198 */ 1199 if (adev->mode_info.crtcs[pipe]) { 1200 /* Repeat readout if needed to provide stable result if 1201 * we cross start of vsync during the queries. 1202 */ 1203 do { 1204 count = amdgpu_display_vblank_get_counter(adev, pipe); 1205 /* Ask amdgpu_display_get_crtc_scanoutpos to return 1206 * vpos as distance to start of vblank, instead of 1207 * regular vertical scanout pos. 1208 */ 1209 stat = amdgpu_display_get_crtc_scanoutpos( 1210 dev, pipe, GET_DISTANCE_TO_VBLANKSTART, 1211 &vpos, &hpos, NULL, NULL, 1212 &adev->mode_info.crtcs[pipe]->base.hwmode); 1213 } while (count != amdgpu_display_vblank_get_counter(adev, pipe)); 1214 1215 if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) != 1216 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) { 1217 DRM_DEBUG_VBL("Query failed! stat %d\n", stat); 1218 } else { 1219 DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n", 1220 pipe, vpos); 1221 1222 /* Bump counter if we are at >= leading edge of vblank, 1223 * but before vsync where vpos would turn negative and 1224 * the hw counter really increments. 1225 */ 1226 if (vpos >= 0) 1227 count++; 1228 } 1229 } else { 1230 /* Fallback to use value as is. */ 1231 count = amdgpu_display_vblank_get_counter(adev, pipe); 1232 DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n"); 1233 } 1234 1235 return count; 1236 } 1237 1238 /** 1239 * amdgpu_enable_vblank_kms - enable vblank interrupt 1240 * 1241 * @crtc: crtc to enable vblank interrupt for 1242 * 1243 * Enable the interrupt on the requested crtc (all asics). 1244 * Returns 0 on success, -EINVAL on failure. 1245 */ 1246 int amdgpu_enable_vblank_kms(struct drm_crtc *crtc) 1247 { 1248 struct drm_device *dev = crtc->dev; 1249 unsigned int pipe = crtc->index; 1250 struct amdgpu_device *adev = dev->dev_private; 1251 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe); 1252 1253 return amdgpu_irq_get(adev, &adev->crtc_irq, idx); 1254 } 1255 1256 /** 1257 * amdgpu_disable_vblank_kms - disable vblank interrupt 1258 * 1259 * @crtc: crtc to disable vblank interrupt for 1260 * 1261 * Disable the interrupt on the requested crtc (all asics). 1262 */ 1263 void amdgpu_disable_vblank_kms(struct drm_crtc *crtc) 1264 { 1265 struct drm_device *dev = crtc->dev; 1266 unsigned int pipe = crtc->index; 1267 struct amdgpu_device *adev = dev->dev_private; 1268 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe); 1269 1270 amdgpu_irq_put(adev, &adev->crtc_irq, idx); 1271 } 1272 1273 const struct drm_ioctl_desc amdgpu_ioctls_kms[] = { 1274 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1275 DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1276 DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1277 DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER), 1278 DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1279 DRM_IOCTL_DEF_DRV(AMDGPU_FENCE_TO_HANDLE, amdgpu_cs_fence_to_handle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1280 /* KMS */ 1281 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1282 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1283 DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1284 DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1285 DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1286 DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1287 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1288 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1289 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW), 1290 DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW) 1291 }; 1292 const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms); 1293 1294 /* 1295 * Debugfs info 1296 */ 1297 #if defined(CONFIG_DEBUG_FS) 1298 1299 static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data) 1300 { 1301 struct drm_info_node *node = (struct drm_info_node *) m->private; 1302 struct drm_device *dev = node->minor->dev; 1303 struct amdgpu_device *adev = dev->dev_private; 1304 struct drm_amdgpu_info_firmware fw_info; 1305 struct drm_amdgpu_query_fw query_fw; 1306 struct atom_context *ctx = adev->mode_info.atom_context; 1307 int ret, i; 1308 1309 /* VCE */ 1310 query_fw.fw_type = AMDGPU_INFO_FW_VCE; 1311 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1312 if (ret) 1313 return ret; 1314 seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n", 1315 fw_info.feature, fw_info.ver); 1316 1317 /* UVD */ 1318 query_fw.fw_type = AMDGPU_INFO_FW_UVD; 1319 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1320 if (ret) 1321 return ret; 1322 seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n", 1323 fw_info.feature, fw_info.ver); 1324 1325 /* GMC */ 1326 query_fw.fw_type = AMDGPU_INFO_FW_GMC; 1327 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1328 if (ret) 1329 return ret; 1330 seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n", 1331 fw_info.feature, fw_info.ver); 1332 1333 /* ME */ 1334 query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME; 1335 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1336 if (ret) 1337 return ret; 1338 seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n", 1339 fw_info.feature, fw_info.ver); 1340 1341 /* PFP */ 1342 query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP; 1343 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1344 if (ret) 1345 return ret; 1346 seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n", 1347 fw_info.feature, fw_info.ver); 1348 1349 /* CE */ 1350 query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE; 1351 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1352 if (ret) 1353 return ret; 1354 seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n", 1355 fw_info.feature, fw_info.ver); 1356 1357 /* RLC */ 1358 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC; 1359 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1360 if (ret) 1361 return ret; 1362 seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n", 1363 fw_info.feature, fw_info.ver); 1364 1365 /* RLC SAVE RESTORE LIST CNTL */ 1366 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL; 1367 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1368 if (ret) 1369 return ret; 1370 seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n", 1371 fw_info.feature, fw_info.ver); 1372 1373 /* RLC SAVE RESTORE LIST GPM MEM */ 1374 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM; 1375 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1376 if (ret) 1377 return ret; 1378 seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n", 1379 fw_info.feature, fw_info.ver); 1380 1381 /* RLC SAVE RESTORE LIST SRM MEM */ 1382 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM; 1383 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1384 if (ret) 1385 return ret; 1386 seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n", 1387 fw_info.feature, fw_info.ver); 1388 1389 /* MEC */ 1390 query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC; 1391 query_fw.index = 0; 1392 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1393 if (ret) 1394 return ret; 1395 seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n", 1396 fw_info.feature, fw_info.ver); 1397 1398 /* MEC2 */ 1399 if (adev->asic_type == CHIP_KAVERI || 1400 (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) { 1401 query_fw.index = 1; 1402 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1403 if (ret) 1404 return ret; 1405 seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n", 1406 fw_info.feature, fw_info.ver); 1407 } 1408 1409 /* PSP SOS */ 1410 query_fw.fw_type = AMDGPU_INFO_FW_SOS; 1411 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1412 if (ret) 1413 return ret; 1414 seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n", 1415 fw_info.feature, fw_info.ver); 1416 1417 1418 /* PSP ASD */ 1419 query_fw.fw_type = AMDGPU_INFO_FW_ASD; 1420 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1421 if (ret) 1422 return ret; 1423 seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n", 1424 fw_info.feature, fw_info.ver); 1425 1426 query_fw.fw_type = AMDGPU_INFO_FW_TA; 1427 for (i = 0; i < 2; i++) { 1428 query_fw.index = i; 1429 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1430 if (ret) 1431 continue; 1432 seq_printf(m, "TA %s feature version: %u, firmware version: 0x%08x\n", 1433 i ? "RAS" : "XGMI", fw_info.feature, fw_info.ver); 1434 } 1435 1436 /* SMC */ 1437 query_fw.fw_type = AMDGPU_INFO_FW_SMC; 1438 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1439 if (ret) 1440 return ret; 1441 seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n", 1442 fw_info.feature, fw_info.ver); 1443 1444 /* SDMA */ 1445 query_fw.fw_type = AMDGPU_INFO_FW_SDMA; 1446 for (i = 0; i < adev->sdma.num_instances; i++) { 1447 query_fw.index = i; 1448 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1449 if (ret) 1450 return ret; 1451 seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n", 1452 i, fw_info.feature, fw_info.ver); 1453 } 1454 1455 /* VCN */ 1456 query_fw.fw_type = AMDGPU_INFO_FW_VCN; 1457 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1458 if (ret) 1459 return ret; 1460 seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n", 1461 fw_info.feature, fw_info.ver); 1462 1463 /* DMCU */ 1464 query_fw.fw_type = AMDGPU_INFO_FW_DMCU; 1465 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1466 if (ret) 1467 return ret; 1468 seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n", 1469 fw_info.feature, fw_info.ver); 1470 1471 /* DMCUB */ 1472 query_fw.fw_type = AMDGPU_INFO_FW_DMCUB; 1473 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev); 1474 if (ret) 1475 return ret; 1476 seq_printf(m, "DMCUB feature version: %u, firmware version: 0x%08x\n", 1477 fw_info.feature, fw_info.ver); 1478 1479 1480 seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version); 1481 1482 return 0; 1483 } 1484 1485 static const struct drm_info_list amdgpu_firmware_info_list[] = { 1486 {"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL}, 1487 }; 1488 #endif 1489 1490 int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev) 1491 { 1492 #if defined(CONFIG_DEBUG_FS) 1493 return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list, 1494 ARRAY_SIZE(amdgpu_firmware_info_list)); 1495 #else 1496 return 0; 1497 #endif 1498 } 1499 1500 int 1501 amdgpu_probe(struct device *parent, void *match, void *aux) 1502 { 1503 struct pci_attach_args *pa = aux; 1504 const struct pci_device_id *id_entry; 1505 unsigned long flags = 0; 1506 1507 if (amdgpu_fatal_error) 1508 return 0; 1509 1510 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id), 1511 PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist); 1512 if (id_entry != NULL) { 1513 flags = id_entry->driver_data; 1514 if (flags & AMD_EXP_HW_SUPPORT) 1515 return 0; 1516 else 1517 return 20; 1518 } 1519 1520 return 0; 1521 } 1522 1523 /* 1524 * some functions are only called once on init regardless of how many times 1525 * amdgpu attaches in linux this is handled via module_init()/module_exit() 1526 */ 1527 int amdgpu_refcnt; 1528 1529 int __init drm_sched_fence_slab_init(void); 1530 void __exit drm_sched_fence_slab_fini(void); 1531 1532 void 1533 amdgpu_attach(struct device *parent, struct device *self, void *aux) 1534 { 1535 struct amdgpu_device *adev = (struct amdgpu_device *)self; 1536 struct drm_device *dev; 1537 struct pci_attach_args *pa = aux; 1538 const struct pci_device_id *id_entry; 1539 pcireg_t type; 1540 int i; 1541 uint8_t rmmio_bar; 1542 paddr_t fb_aper; 1543 pcireg_t addr, mask; 1544 int s; 1545 1546 id_entry = drm_find_description(PCI_VENDOR(pa->pa_id), 1547 PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist); 1548 adev->flags = id_entry->driver_data; 1549 adev->family = adev->flags & AMD_ASIC_MASK; 1550 adev->pc = pa->pa_pc; 1551 adev->pa_tag = pa->pa_tag; 1552 adev->iot = pa->pa_iot; 1553 adev->memt = pa->pa_memt; 1554 adev->dmat = pa->pa_dmat; 1555 1556 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY && 1557 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA && 1558 (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) 1559 & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) 1560 == (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) { 1561 adev->primary = 1; 1562 #if NVGA > 0 1563 adev->console = vga_is_console(pa->pa_iot, -1); 1564 vga_console_attached = 1; 1565 #endif 1566 } 1567 #if NEFIFB > 0 1568 if (efifb_is_primary(pa)) { 1569 adev->primary = 1; 1570 adev->console = efifb_is_console(pa); 1571 efifb_detach(); 1572 } 1573 #endif 1574 1575 #define AMDGPU_PCI_MEM 0x10 1576 1577 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM); 1578 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1579 pci_mapreg_info(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, 1580 type, &adev->fb_aper_offset, &adev->fb_aper_size, NULL)) { 1581 printf(": can't get frambuffer info\n"); 1582 return; 1583 } 1584 1585 if (adev->fb_aper_offset == 0) { 1586 bus_size_t start, end, pci_mem_end; 1587 bus_addr_t base; 1588 1589 start = max(PCI_MEM_START, pa->pa_memex->ex_start); 1590 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT) 1591 pci_mem_end = PCI_MEM64_END; 1592 else 1593 pci_mem_end = PCI_MEM_END; 1594 end = min(pci_mem_end, pa->pa_memex->ex_end); 1595 if (pa->pa_memex == NULL || 1596 extent_alloc_subregion(pa->pa_memex, start, end, 1597 adev->fb_aper_size, adev->fb_aper_size, 0, 0, 0, &base)) { 1598 printf(": can't reserve framebuffer space\n"); 1599 return; 1600 } 1601 pci_conf_write(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, base); 1602 if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT) 1603 pci_conf_write(pa->pa_pc, pa->pa_tag, 1604 AMDGPU_PCI_MEM + 4, (uint64_t)base >> 32); 1605 adev->fb_aper_offset = base; 1606 } 1607 1608 for (i = PCI_MAPREG_START; i < PCI_MAPREG_END ;) { 1609 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, i); 1610 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_IO) { 1611 if (type & PCI_MAPREG_MEM_TYPE_64BIT) 1612 i += 8; 1613 else 1614 i += 4; 1615 continue; 1616 } 1617 if (pci_mapreg_map(pa, i, type, 0, 1618 &adev->rio_mem_bst, &adev->rio_mem_bsh, NULL, 1619 &adev->rio_mem_size, 0)) { 1620 printf(": can't map rio space\n"); 1621 return; 1622 } 1623 break; 1624 } 1625 1626 if (adev->family >= CHIP_BONAIRE) { 1627 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x18); 1628 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1629 pci_mapreg_map(pa, 0x18, type, 0, 1630 &adev->doorbell.bst, &adev->doorbell.bsh, 1631 &adev->doorbell.base, &adev->doorbell.size, 0)) { 1632 printf(": can't map doorbell space\n"); 1633 return; 1634 } 1635 } 1636 1637 if (adev->family >= CHIP_BONAIRE) 1638 rmmio_bar = 0x24; 1639 else 1640 rmmio_bar = 0x18; 1641 1642 type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, rmmio_bar); 1643 if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM || 1644 pci_mapreg_map(pa, rmmio_bar, type, 0, 1645 &adev->rmmio_bst, &adev->rmmio_bsh, &adev->rmmio_base, 1646 &adev->rmmio_size, 0)) { 1647 printf(": can't map rmmio space\n"); 1648 return; 1649 } 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