1 /* 2 * Copyright 2015 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: AMD 23 * 24 */ 25 26 #include "dm_services_types.h" 27 #include "dc.h" 28 #include "dc/inc/core_types.h" 29 30 #include "vid.h" 31 #include "amdgpu.h" 32 #include "amdgpu_display.h" 33 #include "amdgpu_ucode.h" 34 #include "atom.h" 35 #include "amdgpu_dm.h" 36 #include "amdgpu_pm.h" 37 38 #include "amd_shared.h" 39 #include "amdgpu_dm_irq.h" 40 #include "dm_helpers.h" 41 #include "dm_services_types.h" 42 #include "amdgpu_dm_mst_types.h" 43 #if defined(CONFIG_DEBUG_FS) 44 #include "amdgpu_dm_debugfs.h" 45 #endif 46 47 #include "ivsrcid/ivsrcid_vislands30.h" 48 49 #include <linux/module.h> 50 #include <linux/moduleparam.h> 51 #include <linux/version.h> 52 #include <linux/types.h> 53 #include <linux/pm_runtime.h> 54 #include <linux/firmware.h> 55 56 #include <drm/drmP.h> 57 #include <drm/drm_atomic.h> 58 #include <drm/drm_atomic_helper.h> 59 #include <drm/drm_dp_mst_helper.h> 60 #include <drm/drm_fb_helper.h> 61 #include <drm/drm_edid.h> 62 63 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 64 #include "ivsrcid/irqsrcs_dcn_1_0.h" 65 66 #include "dcn/dcn_1_0_offset.h" 67 #include "dcn/dcn_1_0_sh_mask.h" 68 #include "soc15_hw_ip.h" 69 #include "vega10_ip_offset.h" 70 71 #include "soc15_common.h" 72 #endif 73 74 #include "modules/inc/mod_freesync.h" 75 76 #include "i2caux_interface.h" 77 78 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin" 79 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU); 80 81 /* basic init/fini API */ 82 static int amdgpu_dm_init(struct amdgpu_device *adev); 83 static void amdgpu_dm_fini(struct amdgpu_device *adev); 84 85 /* initializes drm_device display related structures, based on the information 86 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector, 87 * drm_encoder, drm_mode_config 88 * 89 * Returns 0 on success 90 */ 91 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev); 92 /* removes and deallocates the drm structures, created by the above function */ 93 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm); 94 95 static void 96 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector); 97 98 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm, 99 struct amdgpu_plane *aplane, 100 unsigned long possible_crtcs); 101 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 102 struct drm_plane *plane, 103 uint32_t link_index); 104 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm, 105 struct amdgpu_dm_connector *amdgpu_dm_connector, 106 uint32_t link_index, 107 struct amdgpu_encoder *amdgpu_encoder); 108 static int amdgpu_dm_encoder_init(struct drm_device *dev, 109 struct amdgpu_encoder *aencoder, 110 uint32_t link_index); 111 112 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector); 113 114 static int amdgpu_dm_atomic_commit(struct drm_device *dev, 115 struct drm_atomic_state *state, 116 bool nonblock); 117 118 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state); 119 120 static int amdgpu_dm_atomic_check(struct drm_device *dev, 121 struct drm_atomic_state *state); 122 123 124 125 126 static const enum drm_plane_type dm_plane_type_default[AMDGPU_MAX_PLANES] = { 127 DRM_PLANE_TYPE_PRIMARY, 128 DRM_PLANE_TYPE_PRIMARY, 129 DRM_PLANE_TYPE_PRIMARY, 130 DRM_PLANE_TYPE_PRIMARY, 131 DRM_PLANE_TYPE_PRIMARY, 132 DRM_PLANE_TYPE_PRIMARY, 133 }; 134 135 static const enum drm_plane_type dm_plane_type_carizzo[AMDGPU_MAX_PLANES] = { 136 DRM_PLANE_TYPE_PRIMARY, 137 DRM_PLANE_TYPE_PRIMARY, 138 DRM_PLANE_TYPE_PRIMARY, 139 DRM_PLANE_TYPE_OVERLAY,/* YUV Capable Underlay */ 140 }; 141 142 static const enum drm_plane_type dm_plane_type_stoney[AMDGPU_MAX_PLANES] = { 143 DRM_PLANE_TYPE_PRIMARY, 144 DRM_PLANE_TYPE_PRIMARY, 145 DRM_PLANE_TYPE_OVERLAY, /* YUV Capable Underlay */ 146 }; 147 148 /* 149 * dm_vblank_get_counter 150 * 151 * @brief 152 * Get counter for number of vertical blanks 153 * 154 * @param 155 * struct amdgpu_device *adev - [in] desired amdgpu device 156 * int disp_idx - [in] which CRTC to get the counter from 157 * 158 * @return 159 * Counter for vertical blanks 160 */ 161 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc) 162 { 163 if (crtc >= adev->mode_info.num_crtc) 164 return 0; 165 else { 166 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc]; 167 struct dm_crtc_state *acrtc_state = to_dm_crtc_state( 168 acrtc->base.state); 169 170 171 if (acrtc_state->stream == NULL) { 172 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n", 173 crtc); 174 return 0; 175 } 176 177 return dc_stream_get_vblank_counter(acrtc_state->stream); 178 } 179 } 180 181 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc, 182 u32 *vbl, u32 *position) 183 { 184 uint32_t v_blank_start, v_blank_end, h_position, v_position; 185 186 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc)) 187 return -EINVAL; 188 else { 189 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc]; 190 struct dm_crtc_state *acrtc_state = to_dm_crtc_state( 191 acrtc->base.state); 192 193 if (acrtc_state->stream == NULL) { 194 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n", 195 crtc); 196 return 0; 197 } 198 199 /* 200 * TODO rework base driver to use values directly. 201 * for now parse it back into reg-format 202 */ 203 dc_stream_get_scanoutpos(acrtc_state->stream, 204 &v_blank_start, 205 &v_blank_end, 206 &h_position, 207 &v_position); 208 209 *position = v_position | (h_position << 16); 210 *vbl = v_blank_start | (v_blank_end << 16); 211 } 212 213 return 0; 214 } 215 216 static bool dm_is_idle(void *handle) 217 { 218 /* XXX todo */ 219 return true; 220 } 221 222 static int dm_wait_for_idle(void *handle) 223 { 224 /* XXX todo */ 225 return 0; 226 } 227 228 static bool dm_check_soft_reset(void *handle) 229 { 230 return false; 231 } 232 233 static int dm_soft_reset(void *handle) 234 { 235 /* XXX todo */ 236 return 0; 237 } 238 239 static struct amdgpu_crtc * 240 get_crtc_by_otg_inst(struct amdgpu_device *adev, 241 int otg_inst) 242 { 243 struct drm_device *dev = adev->ddev; 244 struct drm_crtc *crtc; 245 struct amdgpu_crtc *amdgpu_crtc; 246 247 /* 248 * following if is check inherited from both functions where this one is 249 * used now. Need to be checked why it could happen. 250 */ 251 if (otg_inst == -1) { 252 WARN_ON(1); 253 return adev->mode_info.crtcs[0]; 254 } 255 256 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 257 amdgpu_crtc = to_amdgpu_crtc(crtc); 258 259 if (amdgpu_crtc->otg_inst == otg_inst) 260 return amdgpu_crtc; 261 } 262 263 return NULL; 264 } 265 266 static void dm_pflip_high_irq(void *interrupt_params) 267 { 268 struct amdgpu_crtc *amdgpu_crtc; 269 struct common_irq_params *irq_params = interrupt_params; 270 struct amdgpu_device *adev = irq_params->adev; 271 unsigned long flags; 272 273 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP); 274 275 /* IRQ could occur when in initial stage */ 276 /*TODO work and BO cleanup */ 277 if (amdgpu_crtc == NULL) { 278 DRM_DEBUG_DRIVER("CRTC is null, returning.\n"); 279 return; 280 } 281 282 spin_lock_irqsave(&adev->ddev->event_lock, flags); 283 284 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){ 285 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n", 286 amdgpu_crtc->pflip_status, 287 AMDGPU_FLIP_SUBMITTED, 288 amdgpu_crtc->crtc_id, 289 amdgpu_crtc); 290 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 291 return; 292 } 293 294 295 /* wakeup usersapce */ 296 if (amdgpu_crtc->event) { 297 /* Update to correct count/ts if racing with vblank irq */ 298 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base); 299 300 drm_crtc_send_vblank_event(&amdgpu_crtc->base, amdgpu_crtc->event); 301 302 /* page flip completed. clean up */ 303 amdgpu_crtc->event = NULL; 304 305 } else 306 WARN_ON(1); 307 308 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE; 309 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 310 311 DRM_DEBUG_DRIVER("%s - crtc :%d[%p], pflip_stat:AMDGPU_FLIP_NONE\n", 312 __func__, amdgpu_crtc->crtc_id, amdgpu_crtc); 313 314 drm_crtc_vblank_put(&amdgpu_crtc->base); 315 } 316 317 static void dm_crtc_high_irq(void *interrupt_params) 318 { 319 struct common_irq_params *irq_params = interrupt_params; 320 struct amdgpu_device *adev = irq_params->adev; 321 uint8_t crtc_index = 0; 322 struct amdgpu_crtc *acrtc; 323 324 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK); 325 326 if (acrtc) 327 crtc_index = acrtc->crtc_id; 328 329 drm_handle_vblank(adev->ddev, crtc_index); 330 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base); 331 } 332 333 static int dm_set_clockgating_state(void *handle, 334 enum amd_clockgating_state state) 335 { 336 return 0; 337 } 338 339 static int dm_set_powergating_state(void *handle, 340 enum amd_powergating_state state) 341 { 342 return 0; 343 } 344 345 /* Prototypes of private functions */ 346 static int dm_early_init(void* handle); 347 348 static void hotplug_notify_work_func(struct work_struct *work) 349 { 350 struct amdgpu_display_manager *dm = container_of(work, struct amdgpu_display_manager, mst_hotplug_work); 351 struct drm_device *dev = dm->ddev; 352 353 drm_kms_helper_hotplug_event(dev); 354 } 355 356 /* Allocate memory for FBC compressed data */ 357 static void amdgpu_dm_fbc_init(struct drm_connector *connector) 358 { 359 struct drm_device *dev = connector->dev; 360 struct amdgpu_device *adev = dev->dev_private; 361 struct dm_comressor_info *compressor = &adev->dm.compressor; 362 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector); 363 struct drm_display_mode *mode; 364 unsigned long max_size = 0; 365 366 if (adev->dm.dc->fbc_compressor == NULL) 367 return; 368 369 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP) 370 return; 371 372 if (compressor->bo_ptr) 373 return; 374 375 376 list_for_each_entry(mode, &connector->modes, head) { 377 if (max_size < mode->htotal * mode->vtotal) 378 max_size = mode->htotal * mode->vtotal; 379 } 380 381 if (max_size) { 382 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE, 383 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr, 384 &compressor->gpu_addr, &compressor->cpu_addr); 385 386 if (r) 387 DRM_ERROR("DM: Failed to initialize FBC\n"); 388 else { 389 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr; 390 DRM_INFO("DM: FBC alloc %lu\n", max_size*4); 391 } 392 393 } 394 395 } 396 397 398 /* Init display KMS 399 * 400 * Returns 0 on success 401 */ 402 static int amdgpu_dm_init(struct amdgpu_device *adev) 403 { 404 struct dc_init_data init_data; 405 adev->dm.ddev = adev->ddev; 406 adev->dm.adev = adev; 407 408 /* Zero all the fields */ 409 memset(&init_data, 0, sizeof(init_data)); 410 411 if(amdgpu_dm_irq_init(adev)) { 412 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n"); 413 goto error; 414 } 415 416 init_data.asic_id.chip_family = adev->family; 417 418 init_data.asic_id.pci_revision_id = adev->rev_id; 419 init_data.asic_id.hw_internal_rev = adev->external_rev_id; 420 421 init_data.asic_id.vram_width = adev->gmc.vram_width; 422 /* TODO: initialize init_data.asic_id.vram_type here!!!! */ 423 init_data.asic_id.atombios_base_address = 424 adev->mode_info.atom_context->bios; 425 426 init_data.driver = adev; 427 428 adev->dm.cgs_device = amdgpu_cgs_create_device(adev); 429 430 if (!adev->dm.cgs_device) { 431 DRM_ERROR("amdgpu: failed to create cgs device.\n"); 432 goto error; 433 } 434 435 init_data.cgs_device = adev->dm.cgs_device; 436 437 adev->dm.dal = NULL; 438 439 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV; 440 441 /* 442 * TODO debug why this doesn't work on Raven 443 */ 444 if (adev->flags & AMD_IS_APU && 445 adev->asic_type >= CHIP_CARRIZO && 446 adev->asic_type < CHIP_RAVEN) 447 init_data.flags.gpu_vm_support = true; 448 449 /* Display Core create. */ 450 adev->dm.dc = dc_create(&init_data); 451 452 if (adev->dm.dc) { 453 DRM_INFO("Display Core initialized with v%s!\n", DC_VER); 454 } else { 455 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER); 456 goto error; 457 } 458 459 INIT_WORK(&adev->dm.mst_hotplug_work, hotplug_notify_work_func); 460 461 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc); 462 if (!adev->dm.freesync_module) { 463 DRM_ERROR( 464 "amdgpu: failed to initialize freesync_module.\n"); 465 } else 466 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n", 467 adev->dm.freesync_module); 468 469 amdgpu_dm_init_color_mod(); 470 471 if (amdgpu_dm_initialize_drm_device(adev)) { 472 DRM_ERROR( 473 "amdgpu: failed to initialize sw for display support.\n"); 474 goto error; 475 } 476 477 /* Update the actual used number of crtc */ 478 adev->mode_info.num_crtc = adev->dm.display_indexes_num; 479 480 /* TODO: Add_display_info? */ 481 482 /* TODO use dynamic cursor width */ 483 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size; 484 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size; 485 486 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) { 487 DRM_ERROR( 488 "amdgpu: failed to initialize sw for display support.\n"); 489 goto error; 490 } 491 492 DRM_DEBUG_DRIVER("KMS initialized.\n"); 493 494 return 0; 495 error: 496 amdgpu_dm_fini(adev); 497 498 return -1; 499 } 500 501 static void amdgpu_dm_fini(struct amdgpu_device *adev) 502 { 503 amdgpu_dm_destroy_drm_device(&adev->dm); 504 /* 505 * TODO: pageflip, vlank interrupt 506 * 507 * amdgpu_dm_irq_fini(adev); 508 */ 509 510 if (adev->dm.cgs_device) { 511 amdgpu_cgs_destroy_device(adev->dm.cgs_device); 512 adev->dm.cgs_device = NULL; 513 } 514 if (adev->dm.freesync_module) { 515 mod_freesync_destroy(adev->dm.freesync_module); 516 adev->dm.freesync_module = NULL; 517 } 518 /* DC Destroy TODO: Replace destroy DAL */ 519 if (adev->dm.dc) 520 dc_destroy(&adev->dm.dc); 521 return; 522 } 523 524 static int load_dmcu_fw(struct amdgpu_device *adev) 525 { 526 const char *fw_name_dmcu; 527 int r; 528 const struct dmcu_firmware_header_v1_0 *hdr; 529 530 switch(adev->asic_type) { 531 case CHIP_BONAIRE: 532 case CHIP_HAWAII: 533 case CHIP_KAVERI: 534 case CHIP_KABINI: 535 case CHIP_MULLINS: 536 case CHIP_TONGA: 537 case CHIP_FIJI: 538 case CHIP_CARRIZO: 539 case CHIP_STONEY: 540 case CHIP_POLARIS11: 541 case CHIP_POLARIS10: 542 case CHIP_POLARIS12: 543 case CHIP_VEGAM: 544 case CHIP_VEGA10: 545 case CHIP_VEGA12: 546 case CHIP_VEGA20: 547 return 0; 548 case CHIP_RAVEN: 549 fw_name_dmcu = FIRMWARE_RAVEN_DMCU; 550 break; 551 default: 552 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type); 553 return -1; 554 } 555 556 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 557 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n"); 558 return 0; 559 } 560 561 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev); 562 if (r == -ENOENT) { 563 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */ 564 DRM_DEBUG_KMS("dm: DMCU firmware not found\n"); 565 adev->dm.fw_dmcu = NULL; 566 return 0; 567 } 568 if (r) { 569 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n", 570 fw_name_dmcu); 571 return r; 572 } 573 574 r = amdgpu_ucode_validate(adev->dm.fw_dmcu); 575 if (r) { 576 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n", 577 fw_name_dmcu); 578 release_firmware(adev->dm.fw_dmcu); 579 adev->dm.fw_dmcu = NULL; 580 return r; 581 } 582 583 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data; 584 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM; 585 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu; 586 adev->firmware.fw_size += 587 roundup2(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 588 589 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV; 590 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu; 591 adev->firmware.fw_size += 592 roundup2(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 593 594 DRM_DEBUG_KMS("PSP loading DMCU firmware\n"); 595 596 return 0; 597 } 598 599 static int dm_sw_init(void *handle) 600 { 601 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 602 603 return load_dmcu_fw(adev); 604 } 605 606 static int dm_sw_fini(void *handle) 607 { 608 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 609 610 if(adev->dm.fw_dmcu) { 611 release_firmware(adev->dm.fw_dmcu); 612 adev->dm.fw_dmcu = NULL; 613 } 614 615 return 0; 616 } 617 618 static int detect_mst_link_for_all_connectors(struct drm_device *dev) 619 { 620 struct amdgpu_dm_connector *aconnector; 621 struct drm_connector *connector; 622 int ret = 0; 623 624 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 625 626 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 627 aconnector = to_amdgpu_dm_connector(connector); 628 if (aconnector->dc_link->type == dc_connection_mst_branch && 629 aconnector->mst_mgr.aux) { 630 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n", 631 aconnector, aconnector->base.base.id); 632 633 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true); 634 if (ret < 0) { 635 DRM_ERROR("DM_MST: Failed to start MST\n"); 636 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single; 637 return ret; 638 } 639 } 640 } 641 642 drm_modeset_unlock(&dev->mode_config.connection_mutex); 643 return ret; 644 } 645 646 static int dm_late_init(void *handle) 647 { 648 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 649 650 return detect_mst_link_for_all_connectors(adev->ddev); 651 } 652 653 static void s3_handle_mst(struct drm_device *dev, bool suspend) 654 { 655 struct amdgpu_dm_connector *aconnector; 656 struct drm_connector *connector; 657 struct drm_dp_mst_topology_mgr *mgr; 658 int ret; 659 bool need_hotplug = false; 660 661 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 662 663 list_for_each_entry(connector, &dev->mode_config.connector_list, 664 head) { 665 aconnector = to_amdgpu_dm_connector(connector); 666 if (aconnector->dc_link->type != dc_connection_mst_branch || 667 aconnector->mst_port) 668 continue; 669 670 mgr = &aconnector->mst_mgr; 671 672 if (suspend) { 673 drm_dp_mst_topology_mgr_suspend(mgr); 674 } else { 675 ret = drm_dp_mst_topology_mgr_resume(mgr); 676 if (ret < 0) { 677 drm_dp_mst_topology_mgr_set_mst(mgr, false); 678 need_hotplug = true; 679 } 680 } 681 } 682 683 drm_modeset_unlock(&dev->mode_config.connection_mutex); 684 685 if (need_hotplug) 686 drm_kms_helper_hotplug_event(dev); 687 } 688 689 static int dm_hw_init(void *handle) 690 { 691 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 692 /* Create DAL display manager */ 693 amdgpu_dm_init(adev); 694 amdgpu_dm_hpd_init(adev); 695 696 return 0; 697 } 698 699 static int dm_hw_fini(void *handle) 700 { 701 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 702 703 amdgpu_dm_hpd_fini(adev); 704 705 amdgpu_dm_irq_fini(adev); 706 amdgpu_dm_fini(adev); 707 return 0; 708 } 709 710 static int dm_suspend(void *handle) 711 { 712 struct amdgpu_device *adev = handle; 713 struct amdgpu_display_manager *dm = &adev->dm; 714 int ret = 0; 715 716 WARN_ON(adev->dm.cached_state); 717 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev); 718 719 s3_handle_mst(adev->ddev, true); 720 721 amdgpu_dm_irq_suspend(adev); 722 723 724 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3); 725 726 return ret; 727 } 728 729 static struct amdgpu_dm_connector * 730 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state, 731 struct drm_crtc *crtc) 732 { 733 uint32_t i; 734 struct drm_connector_state *new_con_state; 735 struct drm_connector *connector; 736 struct drm_crtc *crtc_from_state; 737 738 for_each_new_connector_in_state(state, connector, new_con_state, i) { 739 crtc_from_state = new_con_state->crtc; 740 741 if (crtc_from_state == crtc) 742 return to_amdgpu_dm_connector(connector); 743 } 744 745 return NULL; 746 } 747 748 static void emulated_link_detect(struct dc_link *link) 749 { 750 struct dc_sink_init_data sink_init_data = { 0 }; 751 struct display_sink_capability sink_caps = { 0 }; 752 enum dc_edid_status edid_status; 753 struct dc_context *dc_ctx = link->ctx; 754 struct dc_sink *sink = NULL; 755 struct dc_sink *prev_sink = NULL; 756 757 link->type = dc_connection_none; 758 prev_sink = link->local_sink; 759 760 if (prev_sink != NULL) 761 dc_sink_retain(prev_sink); 762 763 switch (link->connector_signal) { 764 case SIGNAL_TYPE_HDMI_TYPE_A: { 765 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 766 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A; 767 break; 768 } 769 770 case SIGNAL_TYPE_DVI_SINGLE_LINK: { 771 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 772 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK; 773 break; 774 } 775 776 case SIGNAL_TYPE_DVI_DUAL_LINK: { 777 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 778 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK; 779 break; 780 } 781 782 case SIGNAL_TYPE_LVDS: { 783 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 784 sink_caps.signal = SIGNAL_TYPE_LVDS; 785 break; 786 } 787 788 case SIGNAL_TYPE_EDP: { 789 sink_caps.transaction_type = 790 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 791 sink_caps.signal = SIGNAL_TYPE_EDP; 792 break; 793 } 794 795 case SIGNAL_TYPE_DISPLAY_PORT: { 796 sink_caps.transaction_type = 797 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 798 sink_caps.signal = SIGNAL_TYPE_VIRTUAL; 799 break; 800 } 801 802 default: 803 DC_ERROR("Invalid connector type! signal:%d\n", 804 link->connector_signal); 805 return; 806 } 807 808 sink_init_data.link = link; 809 sink_init_data.sink_signal = sink_caps.signal; 810 811 sink = dc_sink_create(&sink_init_data); 812 if (!sink) { 813 DC_ERROR("Failed to create sink!\n"); 814 return; 815 } 816 817 link->local_sink = sink; 818 819 edid_status = dm_helpers_read_local_edid( 820 link->ctx, 821 link, 822 sink); 823 824 if (edid_status != EDID_OK) 825 DC_ERROR("Failed to read EDID"); 826 827 } 828 829 static int dm_resume(void *handle) 830 { 831 struct amdgpu_device *adev = handle; 832 struct drm_device *ddev = adev->ddev; 833 struct amdgpu_display_manager *dm = &adev->dm; 834 struct amdgpu_dm_connector *aconnector; 835 struct drm_connector *connector; 836 struct drm_crtc *crtc; 837 struct drm_crtc_state *new_crtc_state; 838 struct dm_crtc_state *dm_new_crtc_state; 839 struct drm_plane *plane; 840 struct drm_plane_state *new_plane_state; 841 struct dm_plane_state *dm_new_plane_state; 842 enum dc_connection_type new_connection_type = dc_connection_none; 843 int i; 844 845 /* power on hardware */ 846 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0); 847 848 /* program HPD filter */ 849 dc_resume(dm->dc); 850 851 /* On resume we need to rewrite the MSTM control bits to enamble MST*/ 852 s3_handle_mst(ddev, false); 853 854 /* 855 * early enable HPD Rx IRQ, should be done before set mode as short 856 * pulse interrupts are used for MST 857 */ 858 amdgpu_dm_irq_resume_early(adev); 859 860 /* Do detection*/ 861 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) { 862 aconnector = to_amdgpu_dm_connector(connector); 863 864 /* 865 * this is the case when traversing through already created 866 * MST connectors, should be skipped 867 */ 868 if (aconnector->mst_port) 869 continue; 870 871 mutex_lock(&aconnector->hpd_lock); 872 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type)) 873 DRM_ERROR("KMS: Failed to detect connector\n"); 874 875 if (aconnector->base.force && new_connection_type == dc_connection_none) 876 emulated_link_detect(aconnector->dc_link); 877 else 878 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD); 879 880 if (aconnector->fake_enable && aconnector->dc_link->local_sink) 881 aconnector->fake_enable = false; 882 883 aconnector->dc_sink = NULL; 884 amdgpu_dm_update_connector_after_detect(aconnector); 885 mutex_unlock(&aconnector->hpd_lock); 886 } 887 888 /* Force mode set in atomic comit */ 889 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) 890 new_crtc_state->active_changed = true; 891 892 /* 893 * atomic_check is expected to create the dc states. We need to release 894 * them here, since they were duplicated as part of the suspend 895 * procedure. 896 */ 897 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) { 898 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 899 if (dm_new_crtc_state->stream) { 900 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1); 901 dc_stream_release(dm_new_crtc_state->stream); 902 dm_new_crtc_state->stream = NULL; 903 } 904 } 905 906 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) { 907 dm_new_plane_state = to_dm_plane_state(new_plane_state); 908 if (dm_new_plane_state->dc_state) { 909 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1); 910 dc_plane_state_release(dm_new_plane_state->dc_state); 911 dm_new_plane_state->dc_state = NULL; 912 } 913 } 914 915 drm_atomic_helper_resume(ddev, dm->cached_state); 916 917 dm->cached_state = NULL; 918 919 amdgpu_dm_irq_resume_late(adev); 920 921 return 0; 922 } 923 924 static const struct amd_ip_funcs amdgpu_dm_funcs = { 925 .name = "dm", 926 .early_init = dm_early_init, 927 .late_init = dm_late_init, 928 .sw_init = dm_sw_init, 929 .sw_fini = dm_sw_fini, 930 .hw_init = dm_hw_init, 931 .hw_fini = dm_hw_fini, 932 .suspend = dm_suspend, 933 .resume = dm_resume, 934 .is_idle = dm_is_idle, 935 .wait_for_idle = dm_wait_for_idle, 936 .check_soft_reset = dm_check_soft_reset, 937 .soft_reset = dm_soft_reset, 938 .set_clockgating_state = dm_set_clockgating_state, 939 .set_powergating_state = dm_set_powergating_state, 940 }; 941 942 const struct amdgpu_ip_block_version dm_ip_block = 943 { 944 .type = AMD_IP_BLOCK_TYPE_DCE, 945 .major = 1, 946 .minor = 0, 947 .rev = 0, 948 .funcs = &amdgpu_dm_funcs, 949 }; 950 951 952 static struct drm_atomic_state * 953 dm_atomic_state_alloc(struct drm_device *dev) 954 { 955 struct dm_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL); 956 957 if (!state) 958 return NULL; 959 960 if (drm_atomic_state_init(dev, &state->base) < 0) 961 goto fail; 962 963 return &state->base; 964 965 fail: 966 kfree(state); 967 return NULL; 968 } 969 970 static void 971 dm_atomic_state_clear(struct drm_atomic_state *state) 972 { 973 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 974 975 if (dm_state->context) { 976 dc_release_state(dm_state->context); 977 dm_state->context = NULL; 978 } 979 980 drm_atomic_state_default_clear(state); 981 } 982 983 static void 984 dm_atomic_state_alloc_free(struct drm_atomic_state *state) 985 { 986 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 987 drm_atomic_state_default_release(state); 988 kfree(dm_state); 989 } 990 991 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = { 992 .fb_create = amdgpu_display_user_framebuffer_create, 993 .output_poll_changed = drm_fb_helper_output_poll_changed, 994 .atomic_check = amdgpu_dm_atomic_check, 995 .atomic_commit = amdgpu_dm_atomic_commit, 996 .atomic_state_alloc = dm_atomic_state_alloc, 997 .atomic_state_clear = dm_atomic_state_clear, 998 .atomic_state_free = dm_atomic_state_alloc_free 999 }; 1000 1001 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = { 1002 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail 1003 }; 1004 1005 static void 1006 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector) 1007 { 1008 struct drm_connector *connector = &aconnector->base; 1009 struct drm_device *dev = connector->dev; 1010 struct dc_sink *sink; 1011 1012 /* MST handled by drm_mst framework */ 1013 if (aconnector->mst_mgr.mst_state == true) 1014 return; 1015 1016 1017 sink = aconnector->dc_link->local_sink; 1018 1019 /* Edid mgmt connector gets first update only in mode_valid hook and then 1020 * the connector sink is set to either fake or physical sink depends on link status. 1021 * don't do it here if u are during boot 1022 */ 1023 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED 1024 && aconnector->dc_em_sink) { 1025 1026 /* For S3 resume with headless use eml_sink to fake stream 1027 * because on resume connecotr->sink is set ti NULL 1028 */ 1029 mutex_lock(&dev->mode_config.mutex); 1030 1031 if (sink) { 1032 if (aconnector->dc_sink) { 1033 amdgpu_dm_remove_sink_from_freesync_module( 1034 connector); 1035 /* retain and release bellow are used for 1036 * bump up refcount for sink because the link don't point 1037 * to it anymore after disconnect so on next crtc to connector 1038 * reshuffle by UMD we will get into unwanted dc_sink release 1039 */ 1040 if (aconnector->dc_sink != aconnector->dc_em_sink) 1041 dc_sink_release(aconnector->dc_sink); 1042 } 1043 aconnector->dc_sink = sink; 1044 amdgpu_dm_add_sink_to_freesync_module( 1045 connector, aconnector->edid); 1046 } else { 1047 amdgpu_dm_remove_sink_from_freesync_module(connector); 1048 if (!aconnector->dc_sink) 1049 aconnector->dc_sink = aconnector->dc_em_sink; 1050 else if (aconnector->dc_sink != aconnector->dc_em_sink) 1051 dc_sink_retain(aconnector->dc_sink); 1052 } 1053 1054 mutex_unlock(&dev->mode_config.mutex); 1055 return; 1056 } 1057 1058 /* 1059 * TODO: temporary guard to look for proper fix 1060 * if this sink is MST sink, we should not do anything 1061 */ 1062 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) 1063 return; 1064 1065 if (aconnector->dc_sink == sink) { 1066 /* We got a DP short pulse (Link Loss, DP CTS, etc...). 1067 * Do nothing!! */ 1068 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n", 1069 aconnector->connector_id); 1070 return; 1071 } 1072 1073 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n", 1074 aconnector->connector_id, aconnector->dc_sink, sink); 1075 1076 mutex_lock(&dev->mode_config.mutex); 1077 1078 /* 1. Update status of the drm connector 1079 * 2. Send an event and let userspace tell us what to do */ 1080 if (sink) { 1081 /* TODO: check if we still need the S3 mode update workaround. 1082 * If yes, put it here. */ 1083 if (aconnector->dc_sink) 1084 amdgpu_dm_remove_sink_from_freesync_module( 1085 connector); 1086 1087 aconnector->dc_sink = sink; 1088 if (sink->dc_edid.length == 0) { 1089 aconnector->edid = NULL; 1090 } else { 1091 aconnector->edid = 1092 (struct edid *) sink->dc_edid.raw_edid; 1093 1094 1095 drm_connector_update_edid_property(connector, 1096 aconnector->edid); 1097 } 1098 amdgpu_dm_add_sink_to_freesync_module(connector, aconnector->edid); 1099 1100 } else { 1101 amdgpu_dm_remove_sink_from_freesync_module(connector); 1102 drm_connector_update_edid_property(connector, NULL); 1103 aconnector->num_modes = 0; 1104 aconnector->dc_sink = NULL; 1105 aconnector->edid = NULL; 1106 } 1107 1108 mutex_unlock(&dev->mode_config.mutex); 1109 } 1110 1111 static void handle_hpd_irq(void *param) 1112 { 1113 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param; 1114 struct drm_connector *connector = &aconnector->base; 1115 struct drm_device *dev = connector->dev; 1116 enum dc_connection_type new_connection_type = dc_connection_none; 1117 1118 /* In case of failure or MST no need to update connector status or notify the OS 1119 * since (for MST case) MST does this in it's own context. 1120 */ 1121 mutex_lock(&aconnector->hpd_lock); 1122 1123 if (aconnector->fake_enable) 1124 aconnector->fake_enable = false; 1125 1126 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type)) 1127 DRM_ERROR("KMS: Failed to detect connector\n"); 1128 1129 if (aconnector->base.force && new_connection_type == dc_connection_none) { 1130 emulated_link_detect(aconnector->dc_link); 1131 1132 1133 drm_modeset_lock_all(dev); 1134 dm_restore_drm_connector_state(dev, connector); 1135 drm_modeset_unlock_all(dev); 1136 1137 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED) 1138 drm_kms_helper_hotplug_event(dev); 1139 1140 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) { 1141 amdgpu_dm_update_connector_after_detect(aconnector); 1142 1143 1144 drm_modeset_lock_all(dev); 1145 dm_restore_drm_connector_state(dev, connector); 1146 drm_modeset_unlock_all(dev); 1147 1148 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED) 1149 drm_kms_helper_hotplug_event(dev); 1150 } 1151 mutex_unlock(&aconnector->hpd_lock); 1152 1153 } 1154 1155 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector) 1156 { 1157 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 }; 1158 uint8_t dret; 1159 bool new_irq_handled = false; 1160 int dpcd_addr; 1161 int dpcd_bytes_to_read; 1162 1163 const int max_process_count = 30; 1164 int process_count = 0; 1165 1166 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link); 1167 1168 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) { 1169 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT; 1170 /* DPCD 0x200 - 0x201 for downstream IRQ */ 1171 dpcd_addr = DP_SINK_COUNT; 1172 } else { 1173 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI; 1174 /* DPCD 0x2002 - 0x2005 for downstream IRQ */ 1175 dpcd_addr = DP_SINK_COUNT_ESI; 1176 } 1177 1178 dret = drm_dp_dpcd_read( 1179 &aconnector->dm_dp_aux.aux, 1180 dpcd_addr, 1181 esi, 1182 dpcd_bytes_to_read); 1183 1184 while (dret == dpcd_bytes_to_read && 1185 process_count < max_process_count) { 1186 uint8_t retry; 1187 dret = 0; 1188 1189 process_count++; 1190 1191 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]); 1192 /* handle HPD short pulse irq */ 1193 if (aconnector->mst_mgr.mst_state) 1194 drm_dp_mst_hpd_irq( 1195 &aconnector->mst_mgr, 1196 esi, 1197 &new_irq_handled); 1198 1199 if (new_irq_handled) { 1200 /* ACK at DPCD to notify down stream */ 1201 const int ack_dpcd_bytes_to_write = 1202 dpcd_bytes_to_read - 1; 1203 1204 for (retry = 0; retry < 3; retry++) { 1205 uint8_t wret; 1206 1207 wret = drm_dp_dpcd_write( 1208 &aconnector->dm_dp_aux.aux, 1209 dpcd_addr + 1, 1210 &esi[1], 1211 ack_dpcd_bytes_to_write); 1212 if (wret == ack_dpcd_bytes_to_write) 1213 break; 1214 } 1215 1216 /* check if there is new irq to be handle */ 1217 dret = drm_dp_dpcd_read( 1218 &aconnector->dm_dp_aux.aux, 1219 dpcd_addr, 1220 esi, 1221 dpcd_bytes_to_read); 1222 1223 new_irq_handled = false; 1224 } else { 1225 break; 1226 } 1227 } 1228 1229 if (process_count == max_process_count) 1230 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n"); 1231 } 1232 1233 static void handle_hpd_rx_irq(void *param) 1234 { 1235 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param; 1236 struct drm_connector *connector = &aconnector->base; 1237 struct drm_device *dev = connector->dev; 1238 struct dc_link *dc_link = aconnector->dc_link; 1239 bool is_mst_root_connector = aconnector->mst_mgr.mst_state; 1240 enum dc_connection_type new_connection_type = dc_connection_none; 1241 1242 /* TODO:Temporary add mutex to protect hpd interrupt not have a gpio 1243 * conflict, after implement i2c helper, this mutex should be 1244 * retired. 1245 */ 1246 if (dc_link->type != dc_connection_mst_branch) 1247 mutex_lock(&aconnector->hpd_lock); 1248 1249 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) && 1250 !is_mst_root_connector) { 1251 /* Downstream Port status changed. */ 1252 if (!dc_link_detect_sink(dc_link, &new_connection_type)) 1253 DRM_ERROR("KMS: Failed to detect connector\n"); 1254 1255 if (aconnector->base.force && new_connection_type == dc_connection_none) { 1256 emulated_link_detect(dc_link); 1257 1258 if (aconnector->fake_enable) 1259 aconnector->fake_enable = false; 1260 1261 amdgpu_dm_update_connector_after_detect(aconnector); 1262 1263 1264 drm_modeset_lock_all(dev); 1265 dm_restore_drm_connector_state(dev, connector); 1266 drm_modeset_unlock_all(dev); 1267 1268 drm_kms_helper_hotplug_event(dev); 1269 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) { 1270 1271 if (aconnector->fake_enable) 1272 aconnector->fake_enable = false; 1273 1274 amdgpu_dm_update_connector_after_detect(aconnector); 1275 1276 1277 drm_modeset_lock_all(dev); 1278 dm_restore_drm_connector_state(dev, connector); 1279 drm_modeset_unlock_all(dev); 1280 1281 drm_kms_helper_hotplug_event(dev); 1282 } 1283 } 1284 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) || 1285 (dc_link->type == dc_connection_mst_branch)) 1286 dm_handle_hpd_rx_irq(aconnector); 1287 1288 if (dc_link->type != dc_connection_mst_branch) 1289 mutex_unlock(&aconnector->hpd_lock); 1290 } 1291 1292 static void register_hpd_handlers(struct amdgpu_device *adev) 1293 { 1294 struct drm_device *dev = adev->ddev; 1295 struct drm_connector *connector; 1296 struct amdgpu_dm_connector *aconnector; 1297 const struct dc_link *dc_link; 1298 struct dc_interrupt_params int_params = {0}; 1299 1300 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 1301 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 1302 1303 list_for_each_entry(connector, 1304 &dev->mode_config.connector_list, head) { 1305 1306 aconnector = to_amdgpu_dm_connector(connector); 1307 dc_link = aconnector->dc_link; 1308 1309 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) { 1310 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT; 1311 int_params.irq_source = dc_link->irq_source_hpd; 1312 1313 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1314 handle_hpd_irq, 1315 (void *) aconnector); 1316 } 1317 1318 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) { 1319 1320 /* Also register for DP short pulse (hpd_rx). */ 1321 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT; 1322 int_params.irq_source = dc_link->irq_source_hpd_rx; 1323 1324 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1325 handle_hpd_rx_irq, 1326 (void *) aconnector); 1327 } 1328 } 1329 } 1330 1331 /* Register IRQ sources and initialize IRQ callbacks */ 1332 static int dce110_register_irq_handlers(struct amdgpu_device *adev) 1333 { 1334 struct dc *dc = adev->dm.dc; 1335 struct common_irq_params *c_irq_params; 1336 struct dc_interrupt_params int_params = {0}; 1337 int r; 1338 int i; 1339 unsigned client_id = AMDGPU_IH_CLIENTID_LEGACY; 1340 1341 if (adev->asic_type == CHIP_VEGA10 || 1342 adev->asic_type == CHIP_VEGA12 || 1343 adev->asic_type == CHIP_VEGA20 || 1344 adev->asic_type == CHIP_RAVEN) 1345 client_id = SOC15_IH_CLIENTID_DCE; 1346 1347 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 1348 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 1349 1350 /* Actions of amdgpu_irq_add_id(): 1351 * 1. Register a set() function with base driver. 1352 * Base driver will call set() function to enable/disable an 1353 * interrupt in DC hardware. 1354 * 2. Register amdgpu_dm_irq_handler(). 1355 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts 1356 * coming from DC hardware. 1357 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC 1358 * for acknowledging and handling. */ 1359 1360 /* Use VBLANK interrupt */ 1361 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) { 1362 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq); 1363 if (r) { 1364 DRM_ERROR("Failed to add crtc irq id!\n"); 1365 return r; 1366 } 1367 1368 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1369 int_params.irq_source = 1370 dc_interrupt_to_irq_source(dc, i, 0); 1371 1372 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1]; 1373 1374 c_irq_params->adev = adev; 1375 c_irq_params->irq_src = int_params.irq_source; 1376 1377 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1378 dm_crtc_high_irq, c_irq_params); 1379 } 1380 1381 /* Use GRPH_PFLIP interrupt */ 1382 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP; 1383 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) { 1384 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq); 1385 if (r) { 1386 DRM_ERROR("Failed to add page flip irq id!\n"); 1387 return r; 1388 } 1389 1390 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1391 int_params.irq_source = 1392 dc_interrupt_to_irq_source(dc, i, 0); 1393 1394 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST]; 1395 1396 c_irq_params->adev = adev; 1397 c_irq_params->irq_src = int_params.irq_source; 1398 1399 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1400 dm_pflip_high_irq, c_irq_params); 1401 1402 } 1403 1404 /* HPD */ 1405 r = amdgpu_irq_add_id(adev, client_id, 1406 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq); 1407 if (r) { 1408 DRM_ERROR("Failed to add hpd irq id!\n"); 1409 return r; 1410 } 1411 1412 register_hpd_handlers(adev); 1413 1414 return 0; 1415 } 1416 1417 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 1418 /* Register IRQ sources and initialize IRQ callbacks */ 1419 static int dcn10_register_irq_handlers(struct amdgpu_device *adev) 1420 { 1421 struct dc *dc = adev->dm.dc; 1422 struct common_irq_params *c_irq_params; 1423 struct dc_interrupt_params int_params = {0}; 1424 int r; 1425 int i; 1426 1427 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 1428 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 1429 1430 /* Actions of amdgpu_irq_add_id(): 1431 * 1. Register a set() function with base driver. 1432 * Base driver will call set() function to enable/disable an 1433 * interrupt in DC hardware. 1434 * 2. Register amdgpu_dm_irq_handler(). 1435 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts 1436 * coming from DC hardware. 1437 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC 1438 * for acknowledging and handling. 1439 * */ 1440 1441 /* Use VSTARTUP interrupt */ 1442 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP; 1443 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1; 1444 i++) { 1445 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq); 1446 1447 if (r) { 1448 DRM_ERROR("Failed to add crtc irq id!\n"); 1449 return r; 1450 } 1451 1452 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1453 int_params.irq_source = 1454 dc_interrupt_to_irq_source(dc, i, 0); 1455 1456 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1]; 1457 1458 c_irq_params->adev = adev; 1459 c_irq_params->irq_src = int_params.irq_source; 1460 1461 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1462 dm_crtc_high_irq, c_irq_params); 1463 } 1464 1465 /* Use GRPH_PFLIP interrupt */ 1466 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT; 1467 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1; 1468 i++) { 1469 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq); 1470 if (r) { 1471 DRM_ERROR("Failed to add page flip irq id!\n"); 1472 return r; 1473 } 1474 1475 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1476 int_params.irq_source = 1477 dc_interrupt_to_irq_source(dc, i, 0); 1478 1479 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST]; 1480 1481 c_irq_params->adev = adev; 1482 c_irq_params->irq_src = int_params.irq_source; 1483 1484 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1485 dm_pflip_high_irq, c_irq_params); 1486 1487 } 1488 1489 /* HPD */ 1490 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT, 1491 &adev->hpd_irq); 1492 if (r) { 1493 DRM_ERROR("Failed to add hpd irq id!\n"); 1494 return r; 1495 } 1496 1497 register_hpd_handlers(adev); 1498 1499 return 0; 1500 } 1501 #endif 1502 1503 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev) 1504 { 1505 int r; 1506 1507 adev->mode_info.mode_config_initialized = true; 1508 1509 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs; 1510 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs; 1511 1512 adev->ddev->mode_config.max_width = 16384; 1513 adev->ddev->mode_config.max_height = 16384; 1514 1515 adev->ddev->mode_config.preferred_depth = 24; 1516 adev->ddev->mode_config.prefer_shadow = 1; 1517 /* indicate support of immediate flip */ 1518 adev->ddev->mode_config.async_page_flip = true; 1519 1520 adev->ddev->mode_config.fb_base = adev->gmc.aper_base; 1521 1522 r = amdgpu_display_modeset_create_props(adev); 1523 if (r) 1524 return r; 1525 1526 return 0; 1527 } 1528 1529 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\ 1530 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 1531 1532 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd) 1533 { 1534 struct amdgpu_display_manager *dm = bl_get_data(bd); 1535 1536 if (dc_link_set_backlight_level(dm->backlight_link, 1537 bd->props.brightness, 0, 0)) 1538 return 0; 1539 else 1540 return 1; 1541 } 1542 1543 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd) 1544 { 1545 struct amdgpu_display_manager *dm = bl_get_data(bd); 1546 int ret = dc_link_get_backlight_level(dm->backlight_link); 1547 1548 if (ret == DC_ERROR_UNEXPECTED) 1549 return bd->props.brightness; 1550 return ret; 1551 } 1552 1553 static const struct backlight_ops amdgpu_dm_backlight_ops = { 1554 .options = BL_CORE_SUSPENDRESUME, 1555 .get_brightness = amdgpu_dm_backlight_get_brightness, 1556 .update_status = amdgpu_dm_backlight_update_status, 1557 }; 1558 1559 static void 1560 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm) 1561 { 1562 char bl_name[16]; 1563 struct backlight_properties props = { 0 }; 1564 1565 props.max_brightness = AMDGPU_MAX_BL_LEVEL; 1566 props.brightness = AMDGPU_MAX_BL_LEVEL; 1567 props.type = BACKLIGHT_RAW; 1568 1569 #ifdef notyet 1570 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d", 1571 dm->adev->ddev->primary->index); 1572 #else 1573 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d", 0); 1574 #endif 1575 1576 dm->backlight_dev = backlight_device_register(bl_name, 1577 dm->adev->ddev->dev, 1578 dm, 1579 &amdgpu_dm_backlight_ops, 1580 &props); 1581 1582 if (IS_ERR(dm->backlight_dev)) 1583 DRM_ERROR("DM: Backlight registration failed!\n"); 1584 else 1585 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name); 1586 } 1587 1588 #endif 1589 1590 static int initialize_plane(struct amdgpu_display_manager *dm, 1591 struct amdgpu_mode_info *mode_info, 1592 int plane_id) 1593 { 1594 struct amdgpu_plane *plane; 1595 unsigned long possible_crtcs; 1596 int ret = 0; 1597 1598 plane = kzalloc(sizeof(struct amdgpu_plane), GFP_KERNEL); 1599 mode_info->planes[plane_id] = plane; 1600 1601 if (!plane) { 1602 DRM_ERROR("KMS: Failed to allocate plane\n"); 1603 return -ENOMEM; 1604 } 1605 plane->base.type = mode_info->plane_type[plane_id]; 1606 1607 /* 1608 * HACK: IGT tests expect that each plane can only have one 1609 * one possible CRTC. For now, set one CRTC for each 1610 * plane that is not an underlay, but still allow multiple 1611 * CRTCs for underlay planes. 1612 */ 1613 possible_crtcs = 1 << plane_id; 1614 if (plane_id >= dm->dc->caps.max_streams) 1615 possible_crtcs = 0xff; 1616 1617 ret = amdgpu_dm_plane_init(dm, mode_info->planes[plane_id], possible_crtcs); 1618 1619 if (ret) { 1620 DRM_ERROR("KMS: Failed to initialize plane\n"); 1621 return ret; 1622 } 1623 1624 return ret; 1625 } 1626 1627 1628 static void register_backlight_device(struct amdgpu_display_manager *dm, 1629 struct dc_link *link) 1630 { 1631 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\ 1632 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 1633 1634 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) && 1635 link->type != dc_connection_none) { 1636 /* Event if registration failed, we should continue with 1637 * DM initialization because not having a backlight control 1638 * is better then a black screen. 1639 */ 1640 amdgpu_dm_register_backlight_device(dm); 1641 1642 if (dm->backlight_dev) 1643 dm->backlight_link = link; 1644 } 1645 #endif 1646 } 1647 1648 1649 /* In this architecture, the association 1650 * connector -> encoder -> crtc 1651 * id not really requried. The crtc and connector will hold the 1652 * display_index as an abstraction to use with DAL component 1653 * 1654 * Returns 0 on success 1655 */ 1656 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev) 1657 { 1658 struct amdgpu_display_manager *dm = &adev->dm; 1659 int32_t i; 1660 struct amdgpu_dm_connector *aconnector = NULL; 1661 struct amdgpu_encoder *aencoder = NULL; 1662 struct amdgpu_mode_info *mode_info = &adev->mode_info; 1663 uint32_t link_cnt; 1664 int32_t total_overlay_planes, total_primary_planes; 1665 enum dc_connection_type new_connection_type = dc_connection_none; 1666 1667 link_cnt = dm->dc->caps.max_links; 1668 if (amdgpu_dm_mode_config_init(dm->adev)) { 1669 DRM_ERROR("DM: Failed to initialize mode config\n"); 1670 return -1; 1671 } 1672 1673 /* Identify the number of planes to be initialized */ 1674 total_overlay_planes = dm->dc->caps.max_slave_planes; 1675 total_primary_planes = dm->dc->caps.max_planes - dm->dc->caps.max_slave_planes; 1676 1677 /* First initialize overlay planes, index starting after primary planes */ 1678 for (i = (total_overlay_planes - 1); i >= 0; i--) { 1679 if (initialize_plane(dm, mode_info, (total_primary_planes + i))) { 1680 DRM_ERROR("KMS: Failed to initialize overlay plane\n"); 1681 goto fail; 1682 } 1683 } 1684 1685 /* Initialize primary planes */ 1686 for (i = (total_primary_planes - 1); i >= 0; i--) { 1687 if (initialize_plane(dm, mode_info, i)) { 1688 DRM_ERROR("KMS: Failed to initialize primary plane\n"); 1689 goto fail; 1690 } 1691 } 1692 1693 for (i = 0; i < dm->dc->caps.max_streams; i++) 1694 if (amdgpu_dm_crtc_init(dm, &mode_info->planes[i]->base, i)) { 1695 DRM_ERROR("KMS: Failed to initialize crtc\n"); 1696 goto fail; 1697 } 1698 1699 dm->display_indexes_num = dm->dc->caps.max_streams; 1700 1701 /* loops over all connectors on the board */ 1702 for (i = 0; i < link_cnt; i++) { 1703 struct dc_link *link = NULL; 1704 1705 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) { 1706 DRM_ERROR( 1707 "KMS: Cannot support more than %d display indexes\n", 1708 AMDGPU_DM_MAX_DISPLAY_INDEX); 1709 continue; 1710 } 1711 1712 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL); 1713 if (!aconnector) 1714 goto fail; 1715 1716 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL); 1717 if (!aencoder) 1718 goto fail; 1719 1720 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) { 1721 DRM_ERROR("KMS: Failed to initialize encoder\n"); 1722 goto fail; 1723 } 1724 1725 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) { 1726 DRM_ERROR("KMS: Failed to initialize connector\n"); 1727 goto fail; 1728 } 1729 1730 link = dc_get_link_at_index(dm->dc, i); 1731 1732 if (!dc_link_detect_sink(link, &new_connection_type)) 1733 DRM_ERROR("KMS: Failed to detect connector\n"); 1734 1735 if (aconnector->base.force && new_connection_type == dc_connection_none) { 1736 emulated_link_detect(link); 1737 amdgpu_dm_update_connector_after_detect(aconnector); 1738 1739 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) { 1740 amdgpu_dm_update_connector_after_detect(aconnector); 1741 register_backlight_device(dm, link); 1742 } 1743 1744 1745 } 1746 1747 /* Software is initialized. Now we can register interrupt handlers. */ 1748 switch (adev->asic_type) { 1749 case CHIP_BONAIRE: 1750 case CHIP_HAWAII: 1751 case CHIP_KAVERI: 1752 case CHIP_KABINI: 1753 case CHIP_MULLINS: 1754 case CHIP_TONGA: 1755 case CHIP_FIJI: 1756 case CHIP_CARRIZO: 1757 case CHIP_STONEY: 1758 case CHIP_POLARIS11: 1759 case CHIP_POLARIS10: 1760 case CHIP_POLARIS12: 1761 case CHIP_VEGAM: 1762 case CHIP_VEGA10: 1763 case CHIP_VEGA12: 1764 case CHIP_VEGA20: 1765 if (dce110_register_irq_handlers(dm->adev)) { 1766 DRM_ERROR("DM: Failed to initialize IRQ\n"); 1767 goto fail; 1768 } 1769 break; 1770 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 1771 case CHIP_RAVEN: 1772 if (dcn10_register_irq_handlers(dm->adev)) { 1773 DRM_ERROR("DM: Failed to initialize IRQ\n"); 1774 goto fail; 1775 } 1776 break; 1777 #endif 1778 default: 1779 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type); 1780 goto fail; 1781 } 1782 1783 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY) 1784 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true; 1785 1786 return 0; 1787 fail: 1788 kfree(aencoder); 1789 kfree(aconnector); 1790 for (i = 0; i < dm->dc->caps.max_planes; i++) 1791 kfree(mode_info->planes[i]); 1792 return -1; 1793 } 1794 1795 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm) 1796 { 1797 drm_mode_config_cleanup(dm->ddev); 1798 return; 1799 } 1800 1801 /****************************************************************************** 1802 * amdgpu_display_funcs functions 1803 *****************************************************************************/ 1804 1805 /** 1806 * dm_bandwidth_update - program display watermarks 1807 * 1808 * @adev: amdgpu_device pointer 1809 * 1810 * Calculate and program the display watermarks and line buffer allocation. 1811 */ 1812 static void dm_bandwidth_update(struct amdgpu_device *adev) 1813 { 1814 /* TODO: implement later */ 1815 } 1816 1817 static int amdgpu_notify_freesync(struct drm_device *dev, void *data, 1818 struct drm_file *filp) 1819 { 1820 struct mod_freesync_params freesync_params; 1821 uint8_t num_streams; 1822 uint8_t i; 1823 1824 struct amdgpu_device *adev = dev->dev_private; 1825 int r = 0; 1826 1827 /* Get freesync enable flag from DRM */ 1828 1829 num_streams = dc_get_current_stream_count(adev->dm.dc); 1830 1831 for (i = 0; i < num_streams; i++) { 1832 struct dc_stream_state *stream; 1833 stream = dc_get_stream_at_index(adev->dm.dc, i); 1834 1835 mod_freesync_update_state(adev->dm.freesync_module, 1836 &stream, 1, &freesync_params); 1837 } 1838 1839 return r; 1840 } 1841 1842 static const struct amdgpu_display_funcs dm_display_funcs = { 1843 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */ 1844 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */ 1845 .backlight_set_level = NULL, /* never called for DC */ 1846 .backlight_get_level = NULL, /* never called for DC */ 1847 .hpd_sense = NULL,/* called unconditionally */ 1848 .hpd_set_polarity = NULL, /* called unconditionally */ 1849 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */ 1850 .page_flip_get_scanoutpos = 1851 dm_crtc_get_scanoutpos,/* called unconditionally */ 1852 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */ 1853 .add_connector = NULL, /* VBIOS parsing. DAL does it. */ 1854 .notify_freesync = amdgpu_notify_freesync, 1855 1856 }; 1857 1858 #if defined(CONFIG_DEBUG_KERNEL_DC) 1859 1860 static ssize_t s3_debug_store(struct device *device, 1861 struct device_attribute *attr, 1862 const char *buf, 1863 size_t count) 1864 { 1865 int ret; 1866 int s3_state; 1867 struct pci_dev *pdev = to_pci_dev(device); 1868 struct drm_device *drm_dev = pci_get_drvdata(pdev); 1869 struct amdgpu_device *adev = drm_dev->dev_private; 1870 1871 ret = kstrtoint(buf, 0, &s3_state); 1872 1873 if (ret == 0) { 1874 if (s3_state) { 1875 dm_resume(adev); 1876 drm_kms_helper_hotplug_event(adev->ddev); 1877 } else 1878 dm_suspend(adev); 1879 } 1880 1881 return ret == 0 ? count : 0; 1882 } 1883 1884 DEVICE_ATTR_WO(s3_debug); 1885 1886 #endif 1887 1888 static int dm_early_init(void *handle) 1889 { 1890 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1891 1892 switch (adev->asic_type) { 1893 case CHIP_BONAIRE: 1894 case CHIP_HAWAII: 1895 adev->mode_info.num_crtc = 6; 1896 adev->mode_info.num_hpd = 6; 1897 adev->mode_info.num_dig = 6; 1898 adev->mode_info.plane_type = dm_plane_type_default; 1899 break; 1900 case CHIP_KAVERI: 1901 adev->mode_info.num_crtc = 4; 1902 adev->mode_info.num_hpd = 6; 1903 adev->mode_info.num_dig = 7; 1904 adev->mode_info.plane_type = dm_plane_type_default; 1905 break; 1906 case CHIP_KABINI: 1907 case CHIP_MULLINS: 1908 adev->mode_info.num_crtc = 2; 1909 adev->mode_info.num_hpd = 6; 1910 adev->mode_info.num_dig = 6; 1911 adev->mode_info.plane_type = dm_plane_type_default; 1912 break; 1913 case CHIP_FIJI: 1914 case CHIP_TONGA: 1915 adev->mode_info.num_crtc = 6; 1916 adev->mode_info.num_hpd = 6; 1917 adev->mode_info.num_dig = 7; 1918 adev->mode_info.plane_type = dm_plane_type_default; 1919 break; 1920 case CHIP_CARRIZO: 1921 adev->mode_info.num_crtc = 3; 1922 adev->mode_info.num_hpd = 6; 1923 adev->mode_info.num_dig = 9; 1924 adev->mode_info.plane_type = dm_plane_type_carizzo; 1925 break; 1926 case CHIP_STONEY: 1927 adev->mode_info.num_crtc = 2; 1928 adev->mode_info.num_hpd = 6; 1929 adev->mode_info.num_dig = 9; 1930 adev->mode_info.plane_type = dm_plane_type_stoney; 1931 break; 1932 case CHIP_POLARIS11: 1933 case CHIP_POLARIS12: 1934 adev->mode_info.num_crtc = 5; 1935 adev->mode_info.num_hpd = 5; 1936 adev->mode_info.num_dig = 5; 1937 adev->mode_info.plane_type = dm_plane_type_default; 1938 break; 1939 case CHIP_POLARIS10: 1940 case CHIP_VEGAM: 1941 adev->mode_info.num_crtc = 6; 1942 adev->mode_info.num_hpd = 6; 1943 adev->mode_info.num_dig = 6; 1944 adev->mode_info.plane_type = dm_plane_type_default; 1945 break; 1946 case CHIP_VEGA10: 1947 case CHIP_VEGA12: 1948 case CHIP_VEGA20: 1949 adev->mode_info.num_crtc = 6; 1950 adev->mode_info.num_hpd = 6; 1951 adev->mode_info.num_dig = 6; 1952 adev->mode_info.plane_type = dm_plane_type_default; 1953 break; 1954 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 1955 case CHIP_RAVEN: 1956 adev->mode_info.num_crtc = 4; 1957 adev->mode_info.num_hpd = 4; 1958 adev->mode_info.num_dig = 4; 1959 adev->mode_info.plane_type = dm_plane_type_default; 1960 break; 1961 #endif 1962 default: 1963 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type); 1964 return -EINVAL; 1965 } 1966 1967 amdgpu_dm_set_irq_funcs(adev); 1968 1969 if (adev->mode_info.funcs == NULL) 1970 adev->mode_info.funcs = &dm_display_funcs; 1971 1972 /* Note: Do NOT change adev->audio_endpt_rreg and 1973 * adev->audio_endpt_wreg because they are initialised in 1974 * amdgpu_device_init() */ 1975 #if defined(CONFIG_DEBUG_KERNEL_DC) 1976 device_create_file( 1977 adev->ddev->dev, 1978 &dev_attr_s3_debug); 1979 #endif 1980 1981 return 0; 1982 } 1983 1984 static bool modeset_required(struct drm_crtc_state *crtc_state, 1985 struct dc_stream_state *new_stream, 1986 struct dc_stream_state *old_stream) 1987 { 1988 if (!drm_atomic_crtc_needs_modeset(crtc_state)) 1989 return false; 1990 1991 if (!crtc_state->enable) 1992 return false; 1993 1994 return crtc_state->active; 1995 } 1996 1997 static bool modereset_required(struct drm_crtc_state *crtc_state) 1998 { 1999 if (!drm_atomic_crtc_needs_modeset(crtc_state)) 2000 return false; 2001 2002 return !crtc_state->enable || !crtc_state->active; 2003 } 2004 2005 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder) 2006 { 2007 drm_encoder_cleanup(encoder); 2008 kfree(encoder); 2009 } 2010 2011 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = { 2012 .destroy = amdgpu_dm_encoder_destroy, 2013 }; 2014 2015 static bool fill_rects_from_plane_state(const struct drm_plane_state *state, 2016 struct dc_plane_state *plane_state) 2017 { 2018 plane_state->src_rect.x = state->src_x >> 16; 2019 plane_state->src_rect.y = state->src_y >> 16; 2020 /*we ignore for now mantissa and do not to deal with floating pixels :(*/ 2021 plane_state->src_rect.width = state->src_w >> 16; 2022 2023 if (plane_state->src_rect.width == 0) 2024 return false; 2025 2026 plane_state->src_rect.height = state->src_h >> 16; 2027 if (plane_state->src_rect.height == 0) 2028 return false; 2029 2030 plane_state->dst_rect.x = state->crtc_x; 2031 plane_state->dst_rect.y = state->crtc_y; 2032 2033 if (state->crtc_w == 0) 2034 return false; 2035 2036 plane_state->dst_rect.width = state->crtc_w; 2037 2038 if (state->crtc_h == 0) 2039 return false; 2040 2041 plane_state->dst_rect.height = state->crtc_h; 2042 2043 plane_state->clip_rect = plane_state->dst_rect; 2044 2045 switch (state->rotation & DRM_MODE_ROTATE_MASK) { 2046 case DRM_MODE_ROTATE_0: 2047 plane_state->rotation = ROTATION_ANGLE_0; 2048 break; 2049 case DRM_MODE_ROTATE_90: 2050 plane_state->rotation = ROTATION_ANGLE_90; 2051 break; 2052 case DRM_MODE_ROTATE_180: 2053 plane_state->rotation = ROTATION_ANGLE_180; 2054 break; 2055 case DRM_MODE_ROTATE_270: 2056 plane_state->rotation = ROTATION_ANGLE_270; 2057 break; 2058 default: 2059 plane_state->rotation = ROTATION_ANGLE_0; 2060 break; 2061 } 2062 2063 return true; 2064 } 2065 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb, 2066 uint64_t *tiling_flags) 2067 { 2068 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]); 2069 int r = amdgpu_bo_reserve(rbo, false); 2070 2071 if (unlikely(r)) { 2072 // Don't show error msg. when return -ERESTARTSYS 2073 if (r != -ERESTARTSYS) 2074 DRM_ERROR("Unable to reserve buffer: %d\n", r); 2075 return r; 2076 } 2077 2078 if (tiling_flags) 2079 amdgpu_bo_get_tiling_flags(rbo, tiling_flags); 2080 2081 amdgpu_bo_unreserve(rbo); 2082 2083 return r; 2084 } 2085 2086 static int fill_plane_attributes_from_fb(struct amdgpu_device *adev, 2087 struct dc_plane_state *plane_state, 2088 const struct amdgpu_framebuffer *amdgpu_fb) 2089 { 2090 uint64_t tiling_flags; 2091 unsigned int awidth; 2092 const struct drm_framebuffer *fb = &amdgpu_fb->base; 2093 int ret = 0; 2094 struct drm_format_name_buf format_name; 2095 2096 ret = get_fb_info( 2097 amdgpu_fb, 2098 &tiling_flags); 2099 2100 if (ret) 2101 return ret; 2102 2103 switch (fb->format->format) { 2104 case DRM_FORMAT_C8: 2105 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS; 2106 break; 2107 case DRM_FORMAT_RGB565: 2108 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565; 2109 break; 2110 case DRM_FORMAT_XRGB8888: 2111 case DRM_FORMAT_ARGB8888: 2112 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888; 2113 break; 2114 case DRM_FORMAT_XRGB2101010: 2115 case DRM_FORMAT_ARGB2101010: 2116 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010; 2117 break; 2118 case DRM_FORMAT_XBGR2101010: 2119 case DRM_FORMAT_ABGR2101010: 2120 plane_state->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010; 2121 break; 2122 case DRM_FORMAT_NV21: 2123 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr; 2124 break; 2125 case DRM_FORMAT_NV12: 2126 plane_state->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb; 2127 break; 2128 default: 2129 DRM_ERROR("Unsupported screen format %s\n", 2130 drm_get_format_name(fb->format->format, &format_name)); 2131 return -EINVAL; 2132 } 2133 2134 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) { 2135 plane_state->address.type = PLN_ADDR_TYPE_GRAPHICS; 2136 plane_state->plane_size.grph.surface_size.x = 0; 2137 plane_state->plane_size.grph.surface_size.y = 0; 2138 plane_state->plane_size.grph.surface_size.width = fb->width; 2139 plane_state->plane_size.grph.surface_size.height = fb->height; 2140 plane_state->plane_size.grph.surface_pitch = 2141 fb->pitches[0] / fb->format->cpp[0]; 2142 /* TODO: unhardcode */ 2143 plane_state->color_space = COLOR_SPACE_SRGB; 2144 2145 } else { 2146 awidth = roundup2(fb->width, 64); 2147 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE; 2148 plane_state->plane_size.video.luma_size.x = 0; 2149 plane_state->plane_size.video.luma_size.y = 0; 2150 plane_state->plane_size.video.luma_size.width = awidth; 2151 plane_state->plane_size.video.luma_size.height = fb->height; 2152 /* TODO: unhardcode */ 2153 plane_state->plane_size.video.luma_pitch = awidth; 2154 2155 plane_state->plane_size.video.chroma_size.x = 0; 2156 plane_state->plane_size.video.chroma_size.y = 0; 2157 plane_state->plane_size.video.chroma_size.width = awidth; 2158 plane_state->plane_size.video.chroma_size.height = fb->height; 2159 plane_state->plane_size.video.chroma_pitch = awidth / 2; 2160 2161 /* TODO: unhardcode */ 2162 plane_state->color_space = COLOR_SPACE_YCBCR709; 2163 } 2164 2165 memset(&plane_state->tiling_info, 0, sizeof(plane_state->tiling_info)); 2166 2167 /* Fill GFX8 params */ 2168 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) { 2169 unsigned int bankw, bankh, mtaspect, tile_split, num_banks; 2170 2171 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH); 2172 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT); 2173 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT); 2174 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT); 2175 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS); 2176 2177 /* XXX fix me for VI */ 2178 plane_state->tiling_info.gfx8.num_banks = num_banks; 2179 plane_state->tiling_info.gfx8.array_mode = 2180 DC_ARRAY_2D_TILED_THIN1; 2181 plane_state->tiling_info.gfx8.tile_split = tile_split; 2182 plane_state->tiling_info.gfx8.bank_width = bankw; 2183 plane_state->tiling_info.gfx8.bank_height = bankh; 2184 plane_state->tiling_info.gfx8.tile_aspect = mtaspect; 2185 plane_state->tiling_info.gfx8.tile_mode = 2186 DC_ADDR_SURF_MICRO_TILING_DISPLAY; 2187 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) 2188 == DC_ARRAY_1D_TILED_THIN1) { 2189 plane_state->tiling_info.gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1; 2190 } 2191 2192 plane_state->tiling_info.gfx8.pipe_config = 2193 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG); 2194 2195 if (adev->asic_type == CHIP_VEGA10 || 2196 adev->asic_type == CHIP_VEGA12 || 2197 adev->asic_type == CHIP_VEGA20 || 2198 adev->asic_type == CHIP_RAVEN) { 2199 /* Fill GFX9 params */ 2200 plane_state->tiling_info.gfx9.num_pipes = 2201 adev->gfx.config.gb_addr_config_fields.num_pipes; 2202 plane_state->tiling_info.gfx9.num_banks = 2203 adev->gfx.config.gb_addr_config_fields.num_banks; 2204 plane_state->tiling_info.gfx9.pipe_interleave = 2205 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size; 2206 plane_state->tiling_info.gfx9.num_shader_engines = 2207 adev->gfx.config.gb_addr_config_fields.num_se; 2208 plane_state->tiling_info.gfx9.max_compressed_frags = 2209 adev->gfx.config.gb_addr_config_fields.max_compress_frags; 2210 plane_state->tiling_info.gfx9.num_rb_per_se = 2211 adev->gfx.config.gb_addr_config_fields.num_rb_per_se; 2212 plane_state->tiling_info.gfx9.swizzle = 2213 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE); 2214 plane_state->tiling_info.gfx9.shaderEnable = 1; 2215 } 2216 2217 plane_state->visible = true; 2218 plane_state->scaling_quality.h_taps_c = 0; 2219 plane_state->scaling_quality.v_taps_c = 0; 2220 2221 /* is this needed? is plane_state zeroed at allocation? */ 2222 plane_state->scaling_quality.h_taps = 0; 2223 plane_state->scaling_quality.v_taps = 0; 2224 plane_state->stereo_format = PLANE_STEREO_FORMAT_NONE; 2225 2226 return ret; 2227 2228 } 2229 2230 static int fill_plane_attributes(struct amdgpu_device *adev, 2231 struct dc_plane_state *dc_plane_state, 2232 struct drm_plane_state *plane_state, 2233 struct drm_crtc_state *crtc_state) 2234 { 2235 const struct amdgpu_framebuffer *amdgpu_fb = 2236 to_amdgpu_framebuffer(plane_state->fb); 2237 const struct drm_crtc *crtc = plane_state->crtc; 2238 int ret = 0; 2239 2240 if (!fill_rects_from_plane_state(plane_state, dc_plane_state)) 2241 return -EINVAL; 2242 2243 ret = fill_plane_attributes_from_fb( 2244 crtc->dev->dev_private, 2245 dc_plane_state, 2246 amdgpu_fb); 2247 2248 if (ret) 2249 return ret; 2250 2251 /* 2252 * Always set input transfer function, since plane state is refreshed 2253 * every time. 2254 */ 2255 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state); 2256 if (ret) { 2257 dc_transfer_func_release(dc_plane_state->in_transfer_func); 2258 dc_plane_state->in_transfer_func = NULL; 2259 } 2260 2261 return ret; 2262 } 2263 2264 /*****************************************************************************/ 2265 2266 static void update_stream_scaling_settings(const struct drm_display_mode *mode, 2267 const struct dm_connector_state *dm_state, 2268 struct dc_stream_state *stream) 2269 { 2270 enum amdgpu_rmx_type rmx_type; 2271 2272 struct rect src = { 0 }; /* viewport in composition space*/ 2273 struct rect dst = { 0 }; /* stream addressable area */ 2274 2275 /* no mode. nothing to be done */ 2276 if (!mode) 2277 return; 2278 2279 /* Full screen scaling by default */ 2280 src.width = mode->hdisplay; 2281 src.height = mode->vdisplay; 2282 dst.width = stream->timing.h_addressable; 2283 dst.height = stream->timing.v_addressable; 2284 2285 if (dm_state) { 2286 rmx_type = dm_state->scaling; 2287 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) { 2288 if (src.width * dst.height < 2289 src.height * dst.width) { 2290 /* height needs less upscaling/more downscaling */ 2291 dst.width = src.width * 2292 dst.height / src.height; 2293 } else { 2294 /* width needs less upscaling/more downscaling */ 2295 dst.height = src.height * 2296 dst.width / src.width; 2297 } 2298 } else if (rmx_type == RMX_CENTER) { 2299 dst = src; 2300 } 2301 2302 dst.x = (stream->timing.h_addressable - dst.width) / 2; 2303 dst.y = (stream->timing.v_addressable - dst.height) / 2; 2304 2305 if (dm_state->underscan_enable) { 2306 dst.x += dm_state->underscan_hborder / 2; 2307 dst.y += dm_state->underscan_vborder / 2; 2308 dst.width -= dm_state->underscan_hborder; 2309 dst.height -= dm_state->underscan_vborder; 2310 } 2311 } 2312 2313 stream->src = src; 2314 stream->dst = dst; 2315 2316 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n", 2317 dst.x, dst.y, dst.width, dst.height); 2318 2319 } 2320 2321 static enum dc_color_depth 2322 convert_color_depth_from_display_info(const struct drm_connector *connector) 2323 { 2324 struct dm_connector_state *dm_conn_state = 2325 to_dm_connector_state(connector->state); 2326 uint32_t bpc = connector->display_info.bpc; 2327 2328 /* TODO: Remove this when there's support for max_bpc in drm */ 2329 if (dm_conn_state && bpc > dm_conn_state->max_bpc) 2330 /* Round down to nearest even number. */ 2331 bpc = dm_conn_state->max_bpc - (dm_conn_state->max_bpc & 1); 2332 2333 switch (bpc) { 2334 case 0: 2335 /* Temporary Work around, DRM don't parse color depth for 2336 * EDID revision before 1.4 2337 * TODO: Fix edid parsing 2338 */ 2339 return COLOR_DEPTH_888; 2340 case 6: 2341 return COLOR_DEPTH_666; 2342 case 8: 2343 return COLOR_DEPTH_888; 2344 case 10: 2345 return COLOR_DEPTH_101010; 2346 case 12: 2347 return COLOR_DEPTH_121212; 2348 case 14: 2349 return COLOR_DEPTH_141414; 2350 case 16: 2351 return COLOR_DEPTH_161616; 2352 default: 2353 return COLOR_DEPTH_UNDEFINED; 2354 } 2355 } 2356 2357 static enum dc_aspect_ratio 2358 get_aspect_ratio(const struct drm_display_mode *mode_in) 2359 { 2360 /* 1-1 mapping, since both enums follow the HDMI spec. */ 2361 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio; 2362 } 2363 2364 static enum dc_color_space 2365 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing) 2366 { 2367 enum dc_color_space color_space = COLOR_SPACE_SRGB; 2368 2369 switch (dc_crtc_timing->pixel_encoding) { 2370 case PIXEL_ENCODING_YCBCR422: 2371 case PIXEL_ENCODING_YCBCR444: 2372 case PIXEL_ENCODING_YCBCR420: 2373 { 2374 /* 2375 * 27030khz is the separation point between HDTV and SDTV 2376 * according to HDMI spec, we use YCbCr709 and YCbCr601 2377 * respectively 2378 */ 2379 if (dc_crtc_timing->pix_clk_khz > 27030) { 2380 if (dc_crtc_timing->flags.Y_ONLY) 2381 color_space = 2382 COLOR_SPACE_YCBCR709_LIMITED; 2383 else 2384 color_space = COLOR_SPACE_YCBCR709; 2385 } else { 2386 if (dc_crtc_timing->flags.Y_ONLY) 2387 color_space = 2388 COLOR_SPACE_YCBCR601_LIMITED; 2389 else 2390 color_space = COLOR_SPACE_YCBCR601; 2391 } 2392 2393 } 2394 break; 2395 case PIXEL_ENCODING_RGB: 2396 color_space = COLOR_SPACE_SRGB; 2397 break; 2398 2399 default: 2400 WARN_ON(1); 2401 break; 2402 } 2403 2404 return color_space; 2405 } 2406 2407 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out) 2408 { 2409 if (timing_out->display_color_depth <= COLOR_DEPTH_888) 2410 return; 2411 2412 timing_out->display_color_depth--; 2413 } 2414 2415 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out, 2416 const struct drm_display_info *info) 2417 { 2418 int normalized_clk; 2419 if (timing_out->display_color_depth <= COLOR_DEPTH_888) 2420 return; 2421 do { 2422 normalized_clk = timing_out->pix_clk_khz; 2423 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */ 2424 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420) 2425 normalized_clk /= 2; 2426 /* Adjusting pix clock following on HDMI spec based on colour depth */ 2427 switch (timing_out->display_color_depth) { 2428 case COLOR_DEPTH_101010: 2429 normalized_clk = (normalized_clk * 30) / 24; 2430 break; 2431 case COLOR_DEPTH_121212: 2432 normalized_clk = (normalized_clk * 36) / 24; 2433 break; 2434 case COLOR_DEPTH_161616: 2435 normalized_clk = (normalized_clk * 48) / 24; 2436 break; 2437 default: 2438 return; 2439 } 2440 if (normalized_clk <= info->max_tmds_clock) 2441 return; 2442 reduce_mode_colour_depth(timing_out); 2443 2444 } while (timing_out->display_color_depth > COLOR_DEPTH_888); 2445 2446 } 2447 /*****************************************************************************/ 2448 2449 static void 2450 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream, 2451 const struct drm_display_mode *mode_in, 2452 const struct drm_connector *connector) 2453 { 2454 struct dc_crtc_timing *timing_out = &stream->timing; 2455 const struct drm_display_info *info = &connector->display_info; 2456 2457 memset(timing_out, 0, sizeof(struct dc_crtc_timing)); 2458 2459 timing_out->h_border_left = 0; 2460 timing_out->h_border_right = 0; 2461 timing_out->v_border_top = 0; 2462 timing_out->v_border_bottom = 0; 2463 /* TODO: un-hardcode */ 2464 if (drm_mode_is_420_only(info, mode_in) 2465 && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) 2466 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420; 2467 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444) 2468 && stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) 2469 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444; 2470 else 2471 timing_out->pixel_encoding = PIXEL_ENCODING_RGB; 2472 2473 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE; 2474 timing_out->display_color_depth = convert_color_depth_from_display_info( 2475 connector); 2476 timing_out->scan_type = SCANNING_TYPE_NODATA; 2477 timing_out->hdmi_vic = 0; 2478 timing_out->vic = drm_match_cea_mode(mode_in); 2479 2480 timing_out->h_addressable = mode_in->crtc_hdisplay; 2481 timing_out->h_total = mode_in->crtc_htotal; 2482 timing_out->h_sync_width = 2483 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start; 2484 timing_out->h_front_porch = 2485 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay; 2486 timing_out->v_total = mode_in->crtc_vtotal; 2487 timing_out->v_addressable = mode_in->crtc_vdisplay; 2488 timing_out->v_front_porch = 2489 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay; 2490 timing_out->v_sync_width = 2491 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start; 2492 timing_out->pix_clk_khz = mode_in->crtc_clock; 2493 timing_out->aspect_ratio = get_aspect_ratio(mode_in); 2494 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC) 2495 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1; 2496 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC) 2497 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1; 2498 2499 stream->output_color_space = get_output_color_space(timing_out); 2500 2501 stream->out_transfer_func->type = TF_TYPE_PREDEFINED; 2502 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB; 2503 if (stream->sink->sink_signal == SIGNAL_TYPE_HDMI_TYPE_A) 2504 adjust_colour_depth_from_display_info(timing_out, info); 2505 } 2506 2507 static void fill_audio_info(struct audio_info *audio_info, 2508 const struct drm_connector *drm_connector, 2509 const struct dc_sink *dc_sink) 2510 { 2511 int i = 0; 2512 int cea_revision = 0; 2513 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps; 2514 2515 audio_info->manufacture_id = edid_caps->manufacturer_id; 2516 audio_info->product_id = edid_caps->product_id; 2517 2518 cea_revision = drm_connector->display_info.cea_rev; 2519 2520 strncpy(audio_info->display_name, 2521 edid_caps->display_name, 2522 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS - 1); 2523 2524 if (cea_revision >= 3) { 2525 audio_info->mode_count = edid_caps->audio_mode_count; 2526 2527 for (i = 0; i < audio_info->mode_count; ++i) { 2528 audio_info->modes[i].format_code = 2529 (enum audio_format_code) 2530 (edid_caps->audio_modes[i].format_code); 2531 audio_info->modes[i].channel_count = 2532 edid_caps->audio_modes[i].channel_count; 2533 audio_info->modes[i].sample_rates.all = 2534 edid_caps->audio_modes[i].sample_rate; 2535 audio_info->modes[i].sample_size = 2536 edid_caps->audio_modes[i].sample_size; 2537 } 2538 } 2539 2540 audio_info->flags.all = edid_caps->speaker_flags; 2541 2542 /* TODO: We only check for the progressive mode, check for interlace mode too */ 2543 if (drm_connector->latency_present[0]) { 2544 audio_info->video_latency = drm_connector->video_latency[0]; 2545 audio_info->audio_latency = drm_connector->audio_latency[0]; 2546 } 2547 2548 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */ 2549 2550 } 2551 2552 static void 2553 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode, 2554 struct drm_display_mode *dst_mode) 2555 { 2556 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay; 2557 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay; 2558 dst_mode->crtc_clock = src_mode->crtc_clock; 2559 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start; 2560 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end; 2561 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start; 2562 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end; 2563 dst_mode->crtc_htotal = src_mode->crtc_htotal; 2564 dst_mode->crtc_hskew = src_mode->crtc_hskew; 2565 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start; 2566 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end; 2567 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start; 2568 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end; 2569 dst_mode->crtc_vtotal = src_mode->crtc_vtotal; 2570 } 2571 2572 static void 2573 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode, 2574 const struct drm_display_mode *native_mode, 2575 bool scale_enabled) 2576 { 2577 if (scale_enabled) { 2578 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode); 2579 } else if (native_mode->clock == drm_mode->clock && 2580 native_mode->htotal == drm_mode->htotal && 2581 native_mode->vtotal == drm_mode->vtotal) { 2582 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode); 2583 } else { 2584 /* no scaling nor amdgpu inserted, no need to patch */ 2585 } 2586 } 2587 2588 static struct dc_sink * 2589 create_fake_sink(struct amdgpu_dm_connector *aconnector) 2590 { 2591 struct dc_sink_init_data sink_init_data = { 0 }; 2592 struct dc_sink *sink = NULL; 2593 sink_init_data.link = aconnector->dc_link; 2594 sink_init_data.sink_signal = aconnector->dc_link->connector_signal; 2595 2596 sink = dc_sink_create(&sink_init_data); 2597 if (!sink) { 2598 DRM_ERROR("Failed to create sink!\n"); 2599 return NULL; 2600 } 2601 sink->sink_signal = SIGNAL_TYPE_VIRTUAL; 2602 2603 return sink; 2604 } 2605 2606 static void set_multisync_trigger_params( 2607 struct dc_stream_state *stream) 2608 { 2609 if (stream->triggered_crtc_reset.enabled) { 2610 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING; 2611 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE; 2612 } 2613 } 2614 2615 static void set_master_stream(struct dc_stream_state *stream_set[], 2616 int stream_count) 2617 { 2618 int j, highest_rfr = 0, master_stream = 0; 2619 2620 for (j = 0; j < stream_count; j++) { 2621 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) { 2622 int refresh_rate = 0; 2623 2624 refresh_rate = (stream_set[j]->timing.pix_clk_khz*1000)/ 2625 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total); 2626 if (refresh_rate > highest_rfr) { 2627 highest_rfr = refresh_rate; 2628 master_stream = j; 2629 } 2630 } 2631 } 2632 for (j = 0; j < stream_count; j++) { 2633 if (stream_set[j]) 2634 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream]; 2635 } 2636 } 2637 2638 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context) 2639 { 2640 int i = 0; 2641 2642 if (context->stream_count < 2) 2643 return; 2644 for (i = 0; i < context->stream_count ; i++) { 2645 if (!context->streams[i]) 2646 continue; 2647 /* TODO: add a function to read AMD VSDB bits and will set 2648 * crtc_sync_master.multi_sync_enabled flag 2649 * For now its set to false 2650 */ 2651 set_multisync_trigger_params(context->streams[i]); 2652 } 2653 set_master_stream(context->streams, context->stream_count); 2654 } 2655 2656 static struct dc_stream_state * 2657 create_stream_for_sink(struct amdgpu_dm_connector *aconnector, 2658 const struct drm_display_mode *drm_mode, 2659 const struct dm_connector_state *dm_state) 2660 { 2661 struct drm_display_mode *preferred_mode = NULL; 2662 struct drm_connector *drm_connector; 2663 struct dc_stream_state *stream = NULL; 2664 struct drm_display_mode mode = *drm_mode; 2665 bool native_mode_found = false; 2666 struct dc_sink *sink = NULL; 2667 if (aconnector == NULL) { 2668 DRM_ERROR("aconnector is NULL!\n"); 2669 return stream; 2670 } 2671 2672 drm_connector = &aconnector->base; 2673 2674 if (!aconnector->dc_sink) { 2675 /* 2676 * Create dc_sink when necessary to MST 2677 * Don't apply fake_sink to MST 2678 */ 2679 if (aconnector->mst_port) { 2680 dm_dp_mst_dc_sink_create(drm_connector); 2681 return stream; 2682 } 2683 2684 sink = create_fake_sink(aconnector); 2685 if (!sink) 2686 return stream; 2687 } else { 2688 sink = aconnector->dc_sink; 2689 } 2690 2691 stream = dc_create_stream_for_sink(sink); 2692 2693 if (stream == NULL) { 2694 DRM_ERROR("Failed to create stream for sink!\n"); 2695 goto finish; 2696 } 2697 2698 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) { 2699 /* Search for preferred mode */ 2700 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) { 2701 native_mode_found = true; 2702 break; 2703 } 2704 } 2705 if (!native_mode_found) 2706 preferred_mode = list_first_entry_or_null( 2707 &aconnector->base.modes, 2708 struct drm_display_mode, 2709 head); 2710 2711 if (preferred_mode == NULL) { 2712 /* This may not be an error, the use case is when we we have no 2713 * usermode calls to reset and set mode upon hotplug. In this 2714 * case, we call set mode ourselves to restore the previous mode 2715 * and the modelist may not be filled in in time. 2716 */ 2717 DRM_DEBUG_DRIVER("No preferred mode found\n"); 2718 } else { 2719 decide_crtc_timing_for_drm_display_mode( 2720 &mode, preferred_mode, 2721 dm_state ? (dm_state->scaling != RMX_OFF) : false); 2722 } 2723 2724 if (!dm_state) 2725 drm_mode_set_crtcinfo(&mode, 0); 2726 2727 fill_stream_properties_from_drm_display_mode(stream, 2728 &mode, &aconnector->base); 2729 update_stream_scaling_settings(&mode, dm_state, stream); 2730 2731 fill_audio_info( 2732 &stream->audio_info, 2733 drm_connector, 2734 sink); 2735 2736 update_stream_signal(stream); 2737 2738 if (dm_state && dm_state->freesync_capable) 2739 stream->ignore_msa_timing_param = true; 2740 finish: 2741 if (sink && sink->sink_signal == SIGNAL_TYPE_VIRTUAL && aconnector->base.force != DRM_FORCE_ON) 2742 dc_sink_release(sink); 2743 2744 return stream; 2745 } 2746 2747 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) 2748 { 2749 drm_crtc_cleanup(crtc); 2750 kfree(crtc); 2751 } 2752 2753 static void dm_crtc_destroy_state(struct drm_crtc *crtc, 2754 struct drm_crtc_state *state) 2755 { 2756 struct dm_crtc_state *cur = to_dm_crtc_state(state); 2757 2758 /* TODO Destroy dc_stream objects are stream object is flattened */ 2759 if (cur->stream) 2760 dc_stream_release(cur->stream); 2761 2762 2763 __drm_atomic_helper_crtc_destroy_state(state); 2764 2765 2766 kfree(state); 2767 } 2768 2769 static void dm_crtc_reset_state(struct drm_crtc *crtc) 2770 { 2771 struct dm_crtc_state *state; 2772 2773 if (crtc->state) 2774 dm_crtc_destroy_state(crtc, crtc->state); 2775 2776 state = kzalloc(sizeof(*state), GFP_KERNEL); 2777 if (WARN_ON(!state)) 2778 return; 2779 2780 crtc->state = &state->base; 2781 crtc->state->crtc = crtc; 2782 2783 } 2784 2785 static struct drm_crtc_state * 2786 dm_crtc_duplicate_state(struct drm_crtc *crtc) 2787 { 2788 struct dm_crtc_state *state, *cur; 2789 2790 cur = to_dm_crtc_state(crtc->state); 2791 2792 if (WARN_ON(!crtc->state)) 2793 return NULL; 2794 2795 state = kzalloc(sizeof(*state), GFP_KERNEL); 2796 if (!state) 2797 return NULL; 2798 2799 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 2800 2801 if (cur->stream) { 2802 state->stream = cur->stream; 2803 dc_stream_retain(state->stream); 2804 } 2805 2806 /* TODO Duplicate dc_stream after objects are stream object is flattened */ 2807 2808 return &state->base; 2809 } 2810 2811 2812 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable) 2813 { 2814 enum dc_irq_source irq_source; 2815 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 2816 struct amdgpu_device *adev = crtc->dev->dev_private; 2817 2818 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst; 2819 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY; 2820 } 2821 2822 static int dm_enable_vblank(struct drm_crtc *crtc) 2823 { 2824 return dm_set_vblank(crtc, true); 2825 } 2826 2827 static void dm_disable_vblank(struct drm_crtc *crtc) 2828 { 2829 dm_set_vblank(crtc, false); 2830 } 2831 2832 /* Implemented only the options currently availible for the driver */ 2833 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = { 2834 .reset = dm_crtc_reset_state, 2835 .destroy = amdgpu_dm_crtc_destroy, 2836 .gamma_set = drm_atomic_helper_legacy_gamma_set, 2837 .set_config = drm_atomic_helper_set_config, 2838 .page_flip = drm_atomic_helper_page_flip, 2839 .atomic_duplicate_state = dm_crtc_duplicate_state, 2840 .atomic_destroy_state = dm_crtc_destroy_state, 2841 .set_crc_source = amdgpu_dm_crtc_set_crc_source, 2842 .enable_vblank = dm_enable_vblank, 2843 .disable_vblank = dm_disable_vblank, 2844 }; 2845 2846 static enum drm_connector_status 2847 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force) 2848 { 2849 bool connected; 2850 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 2851 2852 /* Notes: 2853 * 1. This interface is NOT called in context of HPD irq. 2854 * 2. This interface *is called* in context of user-mode ioctl. Which 2855 * makes it a bad place for *any* MST-related activit. */ 2856 2857 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED && 2858 !aconnector->fake_enable) 2859 connected = (aconnector->dc_sink != NULL); 2860 else 2861 connected = (aconnector->base.force == DRM_FORCE_ON); 2862 2863 return (connected ? connector_status_connected : 2864 connector_status_disconnected); 2865 } 2866 2867 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector, 2868 struct drm_connector_state *connector_state, 2869 struct drm_property *property, 2870 uint64_t val) 2871 { 2872 struct drm_device *dev = connector->dev; 2873 struct amdgpu_device *adev = dev->dev_private; 2874 struct dm_connector_state *dm_old_state = 2875 to_dm_connector_state(connector->state); 2876 struct dm_connector_state *dm_new_state = 2877 to_dm_connector_state(connector_state); 2878 2879 int ret = -EINVAL; 2880 2881 if (property == dev->mode_config.scaling_mode_property) { 2882 enum amdgpu_rmx_type rmx_type; 2883 2884 switch (val) { 2885 case DRM_MODE_SCALE_CENTER: 2886 rmx_type = RMX_CENTER; 2887 break; 2888 case DRM_MODE_SCALE_ASPECT: 2889 rmx_type = RMX_ASPECT; 2890 break; 2891 case DRM_MODE_SCALE_FULLSCREEN: 2892 rmx_type = RMX_FULL; 2893 break; 2894 case DRM_MODE_SCALE_NONE: 2895 default: 2896 rmx_type = RMX_OFF; 2897 break; 2898 } 2899 2900 if (dm_old_state->scaling == rmx_type) 2901 return 0; 2902 2903 dm_new_state->scaling = rmx_type; 2904 ret = 0; 2905 } else if (property == adev->mode_info.underscan_hborder_property) { 2906 dm_new_state->underscan_hborder = val; 2907 ret = 0; 2908 } else if (property == adev->mode_info.underscan_vborder_property) { 2909 dm_new_state->underscan_vborder = val; 2910 ret = 0; 2911 } else if (property == adev->mode_info.underscan_property) { 2912 dm_new_state->underscan_enable = val; 2913 ret = 0; 2914 } else if (property == adev->mode_info.max_bpc_property) { 2915 dm_new_state->max_bpc = val; 2916 ret = 0; 2917 } 2918 2919 return ret; 2920 } 2921 2922 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector, 2923 const struct drm_connector_state *state, 2924 struct drm_property *property, 2925 uint64_t *val) 2926 { 2927 struct drm_device *dev = connector->dev; 2928 struct amdgpu_device *adev = dev->dev_private; 2929 struct dm_connector_state *dm_state = 2930 to_dm_connector_state(state); 2931 int ret = -EINVAL; 2932 2933 if (property == dev->mode_config.scaling_mode_property) { 2934 switch (dm_state->scaling) { 2935 case RMX_CENTER: 2936 *val = DRM_MODE_SCALE_CENTER; 2937 break; 2938 case RMX_ASPECT: 2939 *val = DRM_MODE_SCALE_ASPECT; 2940 break; 2941 case RMX_FULL: 2942 *val = DRM_MODE_SCALE_FULLSCREEN; 2943 break; 2944 case RMX_OFF: 2945 default: 2946 *val = DRM_MODE_SCALE_NONE; 2947 break; 2948 } 2949 ret = 0; 2950 } else if (property == adev->mode_info.underscan_hborder_property) { 2951 *val = dm_state->underscan_hborder; 2952 ret = 0; 2953 } else if (property == adev->mode_info.underscan_vborder_property) { 2954 *val = dm_state->underscan_vborder; 2955 ret = 0; 2956 } else if (property == adev->mode_info.underscan_property) { 2957 *val = dm_state->underscan_enable; 2958 ret = 0; 2959 } else if (property == adev->mode_info.max_bpc_property) { 2960 *val = dm_state->max_bpc; 2961 ret = 0; 2962 } 2963 return ret; 2964 } 2965 2966 static void amdgpu_dm_connector_destroy(struct drm_connector *connector) 2967 { 2968 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 2969 const struct dc_link *link = aconnector->dc_link; 2970 struct amdgpu_device *adev = connector->dev->dev_private; 2971 struct amdgpu_display_manager *dm = &adev->dm; 2972 2973 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\ 2974 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 2975 2976 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) && 2977 link->type != dc_connection_none && 2978 dm->backlight_dev) { 2979 backlight_device_unregister(dm->backlight_dev); 2980 dm->backlight_dev = NULL; 2981 } 2982 #endif 2983 drm_connector_unregister(connector); 2984 drm_connector_cleanup(connector); 2985 kfree(connector); 2986 } 2987 2988 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) 2989 { 2990 struct dm_connector_state *state = 2991 to_dm_connector_state(connector->state); 2992 2993 if (connector->state) 2994 __drm_atomic_helper_connector_destroy_state(connector->state); 2995 2996 kfree(state); 2997 2998 state = kzalloc(sizeof(*state), GFP_KERNEL); 2999 3000 if (state) { 3001 state->scaling = RMX_OFF; 3002 state->underscan_enable = false; 3003 state->underscan_hborder = 0; 3004 state->underscan_vborder = 0; 3005 state->max_bpc = 8; 3006 3007 __drm_atomic_helper_connector_reset(connector, &state->base); 3008 } 3009 } 3010 3011 struct drm_connector_state * 3012 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector) 3013 { 3014 struct dm_connector_state *state = 3015 to_dm_connector_state(connector->state); 3016 3017 struct dm_connector_state *new_state = 3018 kmemdup(state, sizeof(*state), GFP_KERNEL); 3019 3020 if (new_state) { 3021 __drm_atomic_helper_connector_duplicate_state(connector, 3022 &new_state->base); 3023 new_state->max_bpc = state->max_bpc; 3024 return &new_state->base; 3025 } 3026 3027 return NULL; 3028 } 3029 3030 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = { 3031 .reset = amdgpu_dm_connector_funcs_reset, 3032 .detect = amdgpu_dm_connector_detect, 3033 .fill_modes = drm_helper_probe_single_connector_modes, 3034 .destroy = amdgpu_dm_connector_destroy, 3035 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state, 3036 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 3037 .atomic_set_property = amdgpu_dm_connector_atomic_set_property, 3038 .atomic_get_property = amdgpu_dm_connector_atomic_get_property 3039 }; 3040 3041 static struct drm_encoder *best_encoder(struct drm_connector *connector) 3042 { 3043 int enc_id = connector->encoder_ids[0]; 3044 struct drm_mode_object *obj; 3045 struct drm_encoder *encoder; 3046 3047 DRM_DEBUG_DRIVER("Finding the best encoder\n"); 3048 3049 /* pick the encoder ids */ 3050 if (enc_id) { 3051 obj = drm_mode_object_find(connector->dev, NULL, enc_id, DRM_MODE_OBJECT_ENCODER); 3052 if (!obj) { 3053 DRM_ERROR("Couldn't find a matching encoder for our connector\n"); 3054 return NULL; 3055 } 3056 encoder = obj_to_encoder(obj); 3057 return encoder; 3058 } 3059 DRM_ERROR("No encoder id\n"); 3060 return NULL; 3061 } 3062 3063 static int get_modes(struct drm_connector *connector) 3064 { 3065 return amdgpu_dm_connector_get_modes(connector); 3066 } 3067 3068 static void create_eml_sink(struct amdgpu_dm_connector *aconnector) 3069 { 3070 struct dc_sink_init_data init_params = { 3071 .link = aconnector->dc_link, 3072 .sink_signal = SIGNAL_TYPE_VIRTUAL 3073 }; 3074 struct edid *edid; 3075 3076 if (!aconnector->base.edid_blob_ptr) { 3077 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n", 3078 aconnector->base.name); 3079 3080 aconnector->base.force = DRM_FORCE_OFF; 3081 aconnector->base.override_edid = false; 3082 return; 3083 } 3084 3085 edid = (struct edid *) aconnector->base.edid_blob_ptr->data; 3086 3087 aconnector->edid = edid; 3088 3089 aconnector->dc_em_sink = dc_link_add_remote_sink( 3090 aconnector->dc_link, 3091 (uint8_t *)edid, 3092 (edid->extensions + 1) * EDID_LENGTH, 3093 &init_params); 3094 3095 if (aconnector->base.force == DRM_FORCE_ON) 3096 aconnector->dc_sink = aconnector->dc_link->local_sink ? 3097 aconnector->dc_link->local_sink : 3098 aconnector->dc_em_sink; 3099 } 3100 3101 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector) 3102 { 3103 struct dc_link *link = (struct dc_link *)aconnector->dc_link; 3104 3105 /* In case of headless boot with force on for DP managed connector 3106 * Those settings have to be != 0 to get initial modeset 3107 */ 3108 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) { 3109 link->verified_link_cap.lane_count = LANE_COUNT_FOUR; 3110 link->verified_link_cap.link_rate = LINK_RATE_HIGH2; 3111 } 3112 3113 3114 aconnector->base.override_edid = true; 3115 create_eml_sink(aconnector); 3116 } 3117 3118 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector, 3119 struct drm_display_mode *mode) 3120 { 3121 int result = MODE_ERROR; 3122 struct dc_sink *dc_sink; 3123 struct amdgpu_device *adev = connector->dev->dev_private; 3124 /* TODO: Unhardcode stream count */ 3125 struct dc_stream_state *stream; 3126 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 3127 enum dc_status dc_result = DC_OK; 3128 3129 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) || 3130 (mode->flags & DRM_MODE_FLAG_DBLSCAN)) 3131 return result; 3132 3133 /* Only run this the first time mode_valid is called to initilialize 3134 * EDID mgmt 3135 */ 3136 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED && 3137 !aconnector->dc_em_sink) 3138 handle_edid_mgmt(aconnector); 3139 3140 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink; 3141 3142 if (dc_sink == NULL) { 3143 DRM_ERROR("dc_sink is NULL!\n"); 3144 goto fail; 3145 } 3146 3147 stream = create_stream_for_sink(aconnector, mode, NULL); 3148 if (stream == NULL) { 3149 DRM_ERROR("Failed to create stream for sink!\n"); 3150 goto fail; 3151 } 3152 3153 dc_result = dc_validate_stream(adev->dm.dc, stream); 3154 3155 if (dc_result == DC_OK) 3156 result = MODE_OK; 3157 else 3158 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n", 3159 mode->vdisplay, 3160 mode->hdisplay, 3161 mode->clock, 3162 dc_result); 3163 3164 dc_stream_release(stream); 3165 3166 fail: 3167 /* TODO: error handling*/ 3168 return result; 3169 } 3170 3171 static const struct drm_connector_helper_funcs 3172 amdgpu_dm_connector_helper_funcs = { 3173 /* 3174 * If hotplug a second bigger display in FB Con mode, bigger resolution 3175 * modes will be filtered by drm_mode_validate_size(), and those modes 3176 * is missing after user start lightdm. So we need to renew modes list. 3177 * in get_modes call back, not just return the modes count 3178 */ 3179 .get_modes = get_modes, 3180 .mode_valid = amdgpu_dm_connector_mode_valid, 3181 .best_encoder = best_encoder 3182 }; 3183 3184 static void dm_crtc_helper_disable(struct drm_crtc *crtc) 3185 { 3186 } 3187 3188 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc, 3189 struct drm_crtc_state *state) 3190 { 3191 struct amdgpu_device *adev = crtc->dev->dev_private; 3192 struct dc *dc = adev->dm.dc; 3193 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state); 3194 int ret = -EINVAL; 3195 3196 if (unlikely(!dm_crtc_state->stream && 3197 modeset_required(state, NULL, dm_crtc_state->stream))) { 3198 WARN_ON(1); 3199 return ret; 3200 } 3201 3202 /* In some use cases, like reset, no stream is attached */ 3203 if (!dm_crtc_state->stream) 3204 return 0; 3205 3206 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK) 3207 return 0; 3208 3209 return ret; 3210 } 3211 3212 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc, 3213 const struct drm_display_mode *mode, 3214 struct drm_display_mode *adjusted_mode) 3215 { 3216 return true; 3217 } 3218 3219 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = { 3220 .disable = dm_crtc_helper_disable, 3221 .atomic_check = dm_crtc_helper_atomic_check, 3222 .mode_fixup = dm_crtc_helper_mode_fixup 3223 }; 3224 3225 static void dm_encoder_helper_disable(struct drm_encoder *encoder) 3226 { 3227 3228 } 3229 3230 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder, 3231 struct drm_crtc_state *crtc_state, 3232 struct drm_connector_state *conn_state) 3233 { 3234 return 0; 3235 } 3236 3237 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = { 3238 .disable = dm_encoder_helper_disable, 3239 .atomic_check = dm_encoder_helper_atomic_check 3240 }; 3241 3242 static void dm_drm_plane_reset(struct drm_plane *plane) 3243 { 3244 struct dm_plane_state *amdgpu_state = NULL; 3245 3246 if (plane->state) 3247 plane->funcs->atomic_destroy_state(plane, plane->state); 3248 3249 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL); 3250 WARN_ON(amdgpu_state == NULL); 3251 3252 if (amdgpu_state) { 3253 plane->state = &amdgpu_state->base; 3254 plane->state->plane = plane; 3255 plane->state->rotation = DRM_MODE_ROTATE_0; 3256 } 3257 } 3258 3259 static struct drm_plane_state * 3260 dm_drm_plane_duplicate_state(struct drm_plane *plane) 3261 { 3262 struct dm_plane_state *dm_plane_state, *old_dm_plane_state; 3263 3264 old_dm_plane_state = to_dm_plane_state(plane->state); 3265 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL); 3266 if (!dm_plane_state) 3267 return NULL; 3268 3269 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base); 3270 3271 if (old_dm_plane_state->dc_state) { 3272 dm_plane_state->dc_state = old_dm_plane_state->dc_state; 3273 dc_plane_state_retain(dm_plane_state->dc_state); 3274 } 3275 3276 return &dm_plane_state->base; 3277 } 3278 3279 void dm_drm_plane_destroy_state(struct drm_plane *plane, 3280 struct drm_plane_state *state) 3281 { 3282 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state); 3283 3284 if (dm_plane_state->dc_state) 3285 dc_plane_state_release(dm_plane_state->dc_state); 3286 3287 drm_atomic_helper_plane_destroy_state(plane, state); 3288 } 3289 3290 static const struct drm_plane_funcs dm_plane_funcs = { 3291 .update_plane = drm_atomic_helper_update_plane, 3292 .disable_plane = drm_atomic_helper_disable_plane, 3293 .destroy = drm_primary_helper_destroy, 3294 .reset = dm_drm_plane_reset, 3295 .atomic_duplicate_state = dm_drm_plane_duplicate_state, 3296 .atomic_destroy_state = dm_drm_plane_destroy_state, 3297 }; 3298 3299 static int dm_plane_helper_prepare_fb(struct drm_plane *plane, 3300 struct drm_plane_state *new_state) 3301 { 3302 struct amdgpu_framebuffer *afb; 3303 struct drm_gem_object *obj; 3304 struct amdgpu_device *adev; 3305 struct amdgpu_bo *rbo; 3306 uint64_t chroma_addr = 0; 3307 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old; 3308 unsigned int awidth; 3309 uint32_t domain; 3310 int r; 3311 3312 dm_plane_state_old = to_dm_plane_state(plane->state); 3313 dm_plane_state_new = to_dm_plane_state(new_state); 3314 3315 if (!new_state->fb) { 3316 DRM_DEBUG_DRIVER("No FB bound\n"); 3317 return 0; 3318 } 3319 3320 afb = to_amdgpu_framebuffer(new_state->fb); 3321 obj = new_state->fb->obj[0]; 3322 rbo = gem_to_amdgpu_bo(obj); 3323 adev = amdgpu_ttm_adev(rbo->tbo.bdev); 3324 r = amdgpu_bo_reserve(rbo, false); 3325 if (unlikely(r != 0)) 3326 return r; 3327 3328 if (plane->type != DRM_PLANE_TYPE_CURSOR) 3329 domain = amdgpu_display_supported_domains(adev); 3330 else 3331 domain = AMDGPU_GEM_DOMAIN_VRAM; 3332 3333 r = amdgpu_bo_pin(rbo, domain); 3334 if (unlikely(r != 0)) { 3335 if (r != -ERESTARTSYS) 3336 DRM_ERROR("Failed to pin framebuffer with error %d\n", r); 3337 amdgpu_bo_unreserve(rbo); 3338 return r; 3339 } 3340 3341 r = amdgpu_ttm_alloc_gart(&rbo->tbo); 3342 if (unlikely(r != 0)) { 3343 amdgpu_bo_unpin(rbo); 3344 amdgpu_bo_unreserve(rbo); 3345 DRM_ERROR("%p bind failed\n", rbo); 3346 return r; 3347 } 3348 amdgpu_bo_unreserve(rbo); 3349 3350 afb->address = amdgpu_bo_gpu_offset(rbo); 3351 3352 amdgpu_bo_ref(rbo); 3353 3354 if (dm_plane_state_new->dc_state && 3355 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) { 3356 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state; 3357 3358 if (plane_state->format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) { 3359 plane_state->address.grph.addr.low_part = lower_32_bits(afb->address); 3360 plane_state->address.grph.addr.high_part = upper_32_bits(afb->address); 3361 } else { 3362 awidth = roundup2(new_state->fb->width, 64); 3363 plane_state->address.type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE; 3364 plane_state->address.video_progressive.luma_addr.low_part 3365 = lower_32_bits(afb->address); 3366 plane_state->address.video_progressive.luma_addr.high_part 3367 = upper_32_bits(afb->address); 3368 chroma_addr = afb->address + (u64)awidth * new_state->fb->height; 3369 plane_state->address.video_progressive.chroma_addr.low_part 3370 = lower_32_bits(chroma_addr); 3371 plane_state->address.video_progressive.chroma_addr.high_part 3372 = upper_32_bits(chroma_addr); 3373 } 3374 } 3375 3376 return 0; 3377 } 3378 3379 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane, 3380 struct drm_plane_state *old_state) 3381 { 3382 struct amdgpu_bo *rbo; 3383 int r; 3384 3385 if (!old_state->fb) 3386 return; 3387 3388 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]); 3389 r = amdgpu_bo_reserve(rbo, false); 3390 if (unlikely(r)) { 3391 DRM_ERROR("failed to reserve rbo before unpin\n"); 3392 return; 3393 } 3394 3395 amdgpu_bo_unpin(rbo); 3396 amdgpu_bo_unreserve(rbo); 3397 amdgpu_bo_unref(&rbo); 3398 } 3399 3400 static int dm_plane_atomic_check(struct drm_plane *plane, 3401 struct drm_plane_state *state) 3402 { 3403 struct amdgpu_device *adev = plane->dev->dev_private; 3404 struct dc *dc = adev->dm.dc; 3405 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state); 3406 3407 if (!dm_plane_state->dc_state) 3408 return 0; 3409 3410 if (!fill_rects_from_plane_state(state, dm_plane_state->dc_state)) 3411 return -EINVAL; 3412 3413 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK) 3414 return 0; 3415 3416 return -EINVAL; 3417 } 3418 3419 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = { 3420 .prepare_fb = dm_plane_helper_prepare_fb, 3421 .cleanup_fb = dm_plane_helper_cleanup_fb, 3422 .atomic_check = dm_plane_atomic_check, 3423 }; 3424 3425 /* 3426 * TODO: these are currently initialized to rgb formats only. 3427 * For future use cases we should either initialize them dynamically based on 3428 * plane capabilities, or initialize this array to all formats, so internal drm 3429 * check will succeed, and let DC to implement proper check 3430 */ 3431 static const uint32_t rgb_formats[] = { 3432 DRM_FORMAT_RGB888, 3433 DRM_FORMAT_XRGB8888, 3434 DRM_FORMAT_ARGB8888, 3435 DRM_FORMAT_RGBA8888, 3436 DRM_FORMAT_XRGB2101010, 3437 DRM_FORMAT_XBGR2101010, 3438 DRM_FORMAT_ARGB2101010, 3439 DRM_FORMAT_ABGR2101010, 3440 }; 3441 3442 static const uint32_t yuv_formats[] = { 3443 DRM_FORMAT_NV12, 3444 DRM_FORMAT_NV21, 3445 }; 3446 3447 static const u32 cursor_formats[] = { 3448 DRM_FORMAT_ARGB8888 3449 }; 3450 3451 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm, 3452 struct amdgpu_plane *aplane, 3453 unsigned long possible_crtcs) 3454 { 3455 int res = -EPERM; 3456 3457 switch (aplane->base.type) { 3458 case DRM_PLANE_TYPE_PRIMARY: 3459 res = drm_universal_plane_init( 3460 dm->adev->ddev, 3461 &aplane->base, 3462 possible_crtcs, 3463 &dm_plane_funcs, 3464 rgb_formats, 3465 ARRAY_SIZE(rgb_formats), 3466 NULL, aplane->base.type, NULL); 3467 break; 3468 case DRM_PLANE_TYPE_OVERLAY: 3469 res = drm_universal_plane_init( 3470 dm->adev->ddev, 3471 &aplane->base, 3472 possible_crtcs, 3473 &dm_plane_funcs, 3474 yuv_formats, 3475 ARRAY_SIZE(yuv_formats), 3476 NULL, aplane->base.type, NULL); 3477 break; 3478 case DRM_PLANE_TYPE_CURSOR: 3479 res = drm_universal_plane_init( 3480 dm->adev->ddev, 3481 &aplane->base, 3482 possible_crtcs, 3483 &dm_plane_funcs, 3484 cursor_formats, 3485 ARRAY_SIZE(cursor_formats), 3486 NULL, aplane->base.type, NULL); 3487 break; 3488 } 3489 3490 drm_plane_helper_add(&aplane->base, &dm_plane_helper_funcs); 3491 3492 /* Create (reset) the plane state */ 3493 if (aplane->base.funcs->reset) 3494 aplane->base.funcs->reset(&aplane->base); 3495 3496 3497 return res; 3498 } 3499 3500 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 3501 struct drm_plane *plane, 3502 uint32_t crtc_index) 3503 { 3504 struct amdgpu_crtc *acrtc = NULL; 3505 struct amdgpu_plane *cursor_plane; 3506 3507 int res = -ENOMEM; 3508 3509 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL); 3510 if (!cursor_plane) 3511 goto fail; 3512 3513 cursor_plane->base.type = DRM_PLANE_TYPE_CURSOR; 3514 res = amdgpu_dm_plane_init(dm, cursor_plane, 0); 3515 3516 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL); 3517 if (!acrtc) 3518 goto fail; 3519 3520 res = drm_crtc_init_with_planes( 3521 dm->ddev, 3522 &acrtc->base, 3523 plane, 3524 &cursor_plane->base, 3525 &amdgpu_dm_crtc_funcs, NULL); 3526 3527 if (res) 3528 goto fail; 3529 3530 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs); 3531 3532 /* Create (reset) the plane state */ 3533 if (acrtc->base.funcs->reset) 3534 acrtc->base.funcs->reset(&acrtc->base); 3535 3536 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size; 3537 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size; 3538 3539 acrtc->crtc_id = crtc_index; 3540 acrtc->base.enabled = false; 3541 3542 dm->adev->mode_info.crtcs[crtc_index] = acrtc; 3543 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES, 3544 true, MAX_COLOR_LUT_ENTRIES); 3545 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES); 3546 3547 return 0; 3548 3549 fail: 3550 kfree(acrtc); 3551 kfree(cursor_plane); 3552 return res; 3553 } 3554 3555 3556 static int to_drm_connector_type(enum amd_signal_type st) 3557 { 3558 switch (st) { 3559 case SIGNAL_TYPE_HDMI_TYPE_A: 3560 return DRM_MODE_CONNECTOR_HDMIA; 3561 case SIGNAL_TYPE_EDP: 3562 return DRM_MODE_CONNECTOR_eDP; 3563 case SIGNAL_TYPE_RGB: 3564 return DRM_MODE_CONNECTOR_VGA; 3565 case SIGNAL_TYPE_DISPLAY_PORT: 3566 case SIGNAL_TYPE_DISPLAY_PORT_MST: 3567 return DRM_MODE_CONNECTOR_DisplayPort; 3568 case SIGNAL_TYPE_DVI_DUAL_LINK: 3569 case SIGNAL_TYPE_DVI_SINGLE_LINK: 3570 return DRM_MODE_CONNECTOR_DVID; 3571 case SIGNAL_TYPE_VIRTUAL: 3572 return DRM_MODE_CONNECTOR_VIRTUAL; 3573 3574 default: 3575 return DRM_MODE_CONNECTOR_Unknown; 3576 } 3577 } 3578 3579 static void amdgpu_dm_get_native_mode(struct drm_connector *connector) 3580 { 3581 const struct drm_connector_helper_funcs *helper = 3582 connector->helper_private; 3583 struct drm_encoder *encoder; 3584 struct amdgpu_encoder *amdgpu_encoder; 3585 3586 encoder = helper->best_encoder(connector); 3587 3588 if (encoder == NULL) 3589 return; 3590 3591 amdgpu_encoder = to_amdgpu_encoder(encoder); 3592 3593 amdgpu_encoder->native_mode.clock = 0; 3594 3595 if (!list_empty(&connector->probed_modes)) { 3596 struct drm_display_mode *preferred_mode = NULL; 3597 3598 list_for_each_entry(preferred_mode, 3599 &connector->probed_modes, 3600 head) { 3601 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) 3602 amdgpu_encoder->native_mode = *preferred_mode; 3603 3604 break; 3605 } 3606 3607 } 3608 } 3609 3610 static struct drm_display_mode * 3611 amdgpu_dm_create_common_mode(struct drm_encoder *encoder, 3612 char *name, 3613 int hdisplay, int vdisplay) 3614 { 3615 struct drm_device *dev = encoder->dev; 3616 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder); 3617 struct drm_display_mode *mode = NULL; 3618 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode; 3619 3620 mode = drm_mode_duplicate(dev, native_mode); 3621 3622 if (mode == NULL) 3623 return NULL; 3624 3625 mode->hdisplay = hdisplay; 3626 mode->vdisplay = vdisplay; 3627 mode->type &= ~DRM_MODE_TYPE_PREFERRED; 3628 strncpy(mode->name, name, DRM_DISPLAY_MODE_LEN); 3629 3630 return mode; 3631 3632 } 3633 3634 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder, 3635 struct drm_connector *connector) 3636 { 3637 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder); 3638 struct drm_display_mode *mode = NULL; 3639 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode; 3640 struct amdgpu_dm_connector *amdgpu_dm_connector = 3641 to_amdgpu_dm_connector(connector); 3642 int i; 3643 int n; 3644 struct mode_size { 3645 char name[DRM_DISPLAY_MODE_LEN]; 3646 int w; 3647 int h; 3648 } common_modes[] = { 3649 { "640x480", 640, 480}, 3650 { "800x600", 800, 600}, 3651 { "1024x768", 1024, 768}, 3652 { "1280x720", 1280, 720}, 3653 { "1280x800", 1280, 800}, 3654 {"1280x1024", 1280, 1024}, 3655 { "1440x900", 1440, 900}, 3656 {"1680x1050", 1680, 1050}, 3657 {"1600x1200", 1600, 1200}, 3658 {"1920x1080", 1920, 1080}, 3659 {"1920x1200", 1920, 1200} 3660 }; 3661 3662 n = ARRAY_SIZE(common_modes); 3663 3664 for (i = 0; i < n; i++) { 3665 struct drm_display_mode *curmode = NULL; 3666 bool mode_existed = false; 3667 3668 if (common_modes[i].w > native_mode->hdisplay || 3669 common_modes[i].h > native_mode->vdisplay || 3670 (common_modes[i].w == native_mode->hdisplay && 3671 common_modes[i].h == native_mode->vdisplay)) 3672 continue; 3673 3674 list_for_each_entry(curmode, &connector->probed_modes, head) { 3675 if (common_modes[i].w == curmode->hdisplay && 3676 common_modes[i].h == curmode->vdisplay) { 3677 mode_existed = true; 3678 break; 3679 } 3680 } 3681 3682 if (mode_existed) 3683 continue; 3684 3685 mode = amdgpu_dm_create_common_mode(encoder, 3686 common_modes[i].name, common_modes[i].w, 3687 common_modes[i].h); 3688 drm_mode_probed_add(connector, mode); 3689 amdgpu_dm_connector->num_modes++; 3690 } 3691 } 3692 3693 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector, 3694 struct edid *edid) 3695 { 3696 struct amdgpu_dm_connector *amdgpu_dm_connector = 3697 to_amdgpu_dm_connector(connector); 3698 3699 if (edid) { 3700 /* empty probed_modes */ 3701 INIT_LIST_HEAD(&connector->probed_modes); 3702 amdgpu_dm_connector->num_modes = 3703 drm_add_edid_modes(connector, edid); 3704 3705 amdgpu_dm_get_native_mode(connector); 3706 } else { 3707 amdgpu_dm_connector->num_modes = 0; 3708 } 3709 } 3710 3711 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector) 3712 { 3713 const struct drm_connector_helper_funcs *helper = 3714 connector->helper_private; 3715 struct amdgpu_dm_connector *amdgpu_dm_connector = 3716 to_amdgpu_dm_connector(connector); 3717 struct drm_encoder *encoder; 3718 struct edid *edid = amdgpu_dm_connector->edid; 3719 3720 encoder = helper->best_encoder(connector); 3721 3722 if (!edid || !drm_edid_is_valid(edid)) { 3723 drm_add_modes_noedid(connector, 640, 480); 3724 } else { 3725 amdgpu_dm_connector_ddc_get_modes(connector, edid); 3726 amdgpu_dm_connector_add_common_modes(encoder, connector); 3727 } 3728 amdgpu_dm_fbc_init(connector); 3729 3730 return amdgpu_dm_connector->num_modes; 3731 } 3732 3733 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm, 3734 struct amdgpu_dm_connector *aconnector, 3735 int connector_type, 3736 struct dc_link *link, 3737 int link_index) 3738 { 3739 struct amdgpu_device *adev = dm->ddev->dev_private; 3740 3741 /* 3742 * Some of the properties below require access to state, like bpc. 3743 * Allocate some default initial connector state with our reset helper. 3744 */ 3745 if (aconnector->base.funcs->reset) 3746 aconnector->base.funcs->reset(&aconnector->base); 3747 3748 aconnector->connector_id = link_index; 3749 aconnector->dc_link = link; 3750 aconnector->base.interlace_allowed = false; 3751 aconnector->base.doublescan_allowed = false; 3752 aconnector->base.stereo_allowed = false; 3753 aconnector->base.dpms = DRM_MODE_DPMS_OFF; 3754 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */ 3755 rw_init(&aconnector->hpd_lock, "dmhpd"); 3756 3757 /* configure support HPD hot plug connector_>polled default value is 0 3758 * which means HPD hot plug not supported 3759 */ 3760 switch (connector_type) { 3761 case DRM_MODE_CONNECTOR_HDMIA: 3762 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 3763 aconnector->base.ycbcr_420_allowed = 3764 link->link_enc->features.ycbcr420_supported ? true : false; 3765 break; 3766 case DRM_MODE_CONNECTOR_DisplayPort: 3767 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 3768 aconnector->base.ycbcr_420_allowed = 3769 link->link_enc->features.ycbcr420_supported ? true : false; 3770 break; 3771 case DRM_MODE_CONNECTOR_DVID: 3772 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 3773 break; 3774 default: 3775 break; 3776 } 3777 3778 drm_object_attach_property(&aconnector->base.base, 3779 dm->ddev->mode_config.scaling_mode_property, 3780 DRM_MODE_SCALE_NONE); 3781 3782 drm_object_attach_property(&aconnector->base.base, 3783 adev->mode_info.underscan_property, 3784 UNDERSCAN_OFF); 3785 drm_object_attach_property(&aconnector->base.base, 3786 adev->mode_info.underscan_hborder_property, 3787 0); 3788 drm_object_attach_property(&aconnector->base.base, 3789 adev->mode_info.underscan_vborder_property, 3790 0); 3791 drm_object_attach_property(&aconnector->base.base, 3792 adev->mode_info.max_bpc_property, 3793 0); 3794 3795 } 3796 3797 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap, 3798 struct i2c_msg *msgs, int num) 3799 { 3800 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap); 3801 struct ddc_service *ddc_service = i2c->ddc_service; 3802 struct i2c_command cmd; 3803 int i; 3804 int result = -EIO; 3805 3806 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL); 3807 3808 if (!cmd.payloads) 3809 return result; 3810 3811 cmd.number_of_payloads = num; 3812 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT; 3813 cmd.speed = 100; 3814 3815 for (i = 0; i < num; i++) { 3816 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD); 3817 cmd.payloads[i].address = msgs[i].addr; 3818 cmd.payloads[i].length = msgs[i].len; 3819 cmd.payloads[i].data = msgs[i].buf; 3820 } 3821 3822 if (dal_i2caux_submit_i2c_command( 3823 ddc_service->ctx->i2caux, 3824 ddc_service->ddc_pin, 3825 &cmd)) 3826 result = num; 3827 3828 kfree(cmd.payloads); 3829 return result; 3830 } 3831 3832 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap) 3833 { 3834 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 3835 } 3836 3837 static const struct i2c_algorithm amdgpu_dm_i2c_algo = { 3838 .master_xfer = amdgpu_dm_i2c_xfer, 3839 .functionality = amdgpu_dm_i2c_func, 3840 }; 3841 3842 static struct amdgpu_i2c_adapter * 3843 create_i2c(struct ddc_service *ddc_service, 3844 int link_index, 3845 int *res) 3846 { 3847 struct amdgpu_device *adev = ddc_service->ctx->driver_context; 3848 struct amdgpu_i2c_adapter *i2c; 3849 3850 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL); 3851 if (!i2c) 3852 return NULL; 3853 #ifdef __linux__ 3854 i2c->base.owner = THIS_MODULE; 3855 i2c->base.class = I2C_CLASS_DDC; 3856 i2c->base.dev.parent = &adev->pdev->dev; 3857 #endif 3858 i2c->base.algo = &amdgpu_dm_i2c_algo; 3859 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index); 3860 i2c_set_adapdata(&i2c->base, i2c); 3861 i2c->ddc_service = ddc_service; 3862 3863 return i2c; 3864 } 3865 3866 3867 /* Note: this function assumes that dc_link_detect() was called for the 3868 * dc_link which will be represented by this aconnector. 3869 */ 3870 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm, 3871 struct amdgpu_dm_connector *aconnector, 3872 uint32_t link_index, 3873 struct amdgpu_encoder *aencoder) 3874 { 3875 int res = 0; 3876 int connector_type; 3877 struct dc *dc = dm->dc; 3878 struct dc_link *link = dc_get_link_at_index(dc, link_index); 3879 struct amdgpu_i2c_adapter *i2c; 3880 3881 link->priv = aconnector; 3882 3883 DRM_DEBUG_DRIVER("%s()\n", __func__); 3884 3885 i2c = create_i2c(link->ddc, link->link_index, &res); 3886 if (!i2c) { 3887 DRM_ERROR("Failed to create i2c adapter data\n"); 3888 return -ENOMEM; 3889 } 3890 3891 aconnector->i2c = i2c; 3892 res = i2c_add_adapter(&i2c->base); 3893 3894 if (res) { 3895 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index); 3896 goto out_free; 3897 } 3898 3899 connector_type = to_drm_connector_type(link->connector_signal); 3900 3901 res = drm_connector_init( 3902 dm->ddev, 3903 &aconnector->base, 3904 &amdgpu_dm_connector_funcs, 3905 connector_type); 3906 3907 if (res) { 3908 DRM_ERROR("connector_init failed\n"); 3909 aconnector->connector_id = -1; 3910 goto out_free; 3911 } 3912 3913 drm_connector_helper_add( 3914 &aconnector->base, 3915 &amdgpu_dm_connector_helper_funcs); 3916 3917 amdgpu_dm_connector_init_helper( 3918 dm, 3919 aconnector, 3920 connector_type, 3921 link, 3922 link_index); 3923 3924 drm_connector_attach_encoder( 3925 &aconnector->base, &aencoder->base); 3926 3927 drm_connector_register(&aconnector->base); 3928 #if defined(CONFIG_DEBUG_FS) 3929 res = connector_debugfs_init(aconnector); 3930 if (res) { 3931 DRM_ERROR("Failed to create debugfs for connector"); 3932 goto out_free; 3933 } 3934 #endif 3935 3936 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort 3937 || connector_type == DRM_MODE_CONNECTOR_eDP) 3938 amdgpu_dm_initialize_dp_connector(dm, aconnector); 3939 3940 out_free: 3941 if (res) { 3942 kfree(i2c); 3943 aconnector->i2c = NULL; 3944 } 3945 return res; 3946 } 3947 3948 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev) 3949 { 3950 switch (adev->mode_info.num_crtc) { 3951 case 1: 3952 return 0x1; 3953 case 2: 3954 return 0x3; 3955 case 3: 3956 return 0x7; 3957 case 4: 3958 return 0xf; 3959 case 5: 3960 return 0x1f; 3961 case 6: 3962 default: 3963 return 0x3f; 3964 } 3965 } 3966 3967 static int amdgpu_dm_encoder_init(struct drm_device *dev, 3968 struct amdgpu_encoder *aencoder, 3969 uint32_t link_index) 3970 { 3971 struct amdgpu_device *adev = dev->dev_private; 3972 3973 int res = drm_encoder_init(dev, 3974 &aencoder->base, 3975 &amdgpu_dm_encoder_funcs, 3976 DRM_MODE_ENCODER_TMDS, 3977 NULL); 3978 3979 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev); 3980 3981 if (!res) 3982 aencoder->encoder_id = link_index; 3983 else 3984 aencoder->encoder_id = -1; 3985 3986 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs); 3987 3988 return res; 3989 } 3990 3991 static void manage_dm_interrupts(struct amdgpu_device *adev, 3992 struct amdgpu_crtc *acrtc, 3993 bool enable) 3994 { 3995 /* 3996 * this is not correct translation but will work as soon as VBLANK 3997 * constant is the same as PFLIP 3998 */ 3999 int irq_type = 4000 amdgpu_display_crtc_idx_to_irq_type( 4001 adev, 4002 acrtc->crtc_id); 4003 4004 if (enable) { 4005 drm_crtc_vblank_on(&acrtc->base); 4006 amdgpu_irq_get( 4007 adev, 4008 &adev->pageflip_irq, 4009 irq_type); 4010 } else { 4011 4012 amdgpu_irq_put( 4013 adev, 4014 &adev->pageflip_irq, 4015 irq_type); 4016 drm_crtc_vblank_off(&acrtc->base); 4017 } 4018 } 4019 4020 static bool 4021 is_scaling_state_different(const struct dm_connector_state *dm_state, 4022 const struct dm_connector_state *old_dm_state) 4023 { 4024 if (dm_state->scaling != old_dm_state->scaling) 4025 return true; 4026 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) { 4027 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0) 4028 return true; 4029 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) { 4030 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0) 4031 return true; 4032 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder || 4033 dm_state->underscan_vborder != old_dm_state->underscan_vborder) 4034 return true; 4035 return false; 4036 } 4037 4038 static void remove_stream(struct amdgpu_device *adev, 4039 struct amdgpu_crtc *acrtc, 4040 struct dc_stream_state *stream) 4041 { 4042 /* this is the update mode case */ 4043 if (adev->dm.freesync_module) 4044 mod_freesync_remove_stream(adev->dm.freesync_module, stream); 4045 4046 acrtc->otg_inst = -1; 4047 acrtc->enabled = false; 4048 } 4049 4050 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc, 4051 struct dc_cursor_position *position) 4052 { 4053 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 4054 int x, y; 4055 int xorigin = 0, yorigin = 0; 4056 4057 if (!crtc || !plane->state->fb) { 4058 position->enable = false; 4059 position->x = 0; 4060 position->y = 0; 4061 return 0; 4062 } 4063 4064 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) || 4065 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) { 4066 DRM_ERROR("%s: bad cursor width or height %d x %d\n", 4067 __func__, 4068 plane->state->crtc_w, 4069 plane->state->crtc_h); 4070 return -EINVAL; 4071 } 4072 4073 x = plane->state->crtc_x; 4074 y = plane->state->crtc_y; 4075 /* avivo cursor are offset into the total surface */ 4076 x += crtc->primary->state->src_x >> 16; 4077 y += crtc->primary->state->src_y >> 16; 4078 if (x < 0) { 4079 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1); 4080 x = 0; 4081 } 4082 if (y < 0) { 4083 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1); 4084 y = 0; 4085 } 4086 position->enable = true; 4087 position->x = x; 4088 position->y = y; 4089 position->x_hotspot = xorigin; 4090 position->y_hotspot = yorigin; 4091 4092 return 0; 4093 } 4094 4095 static void handle_cursor_update(struct drm_plane *plane, 4096 struct drm_plane_state *old_plane_state) 4097 { 4098 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb); 4099 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc; 4100 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL; 4101 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 4102 uint64_t address = afb ? afb->address : 0; 4103 struct dc_cursor_position position; 4104 struct dc_cursor_attributes attributes; 4105 int ret; 4106 4107 if (!plane->state->fb && !old_plane_state->fb) 4108 return; 4109 4110 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n", 4111 __func__, 4112 amdgpu_crtc->crtc_id, 4113 plane->state->crtc_w, 4114 plane->state->crtc_h); 4115 4116 ret = get_cursor_position(plane, crtc, &position); 4117 if (ret) 4118 return; 4119 4120 if (!position.enable) { 4121 /* turn off cursor */ 4122 if (crtc_state && crtc_state->stream) 4123 dc_stream_set_cursor_position(crtc_state->stream, 4124 &position); 4125 return; 4126 } 4127 4128 amdgpu_crtc->cursor_width = plane->state->crtc_w; 4129 amdgpu_crtc->cursor_height = plane->state->crtc_h; 4130 4131 memset(&attributes, 0, sizeof(attributes)); 4132 attributes.address.high_part = upper_32_bits(address); 4133 attributes.address.low_part = lower_32_bits(address); 4134 attributes.width = plane->state->crtc_w; 4135 attributes.height = plane->state->crtc_h; 4136 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA; 4137 attributes.rotation_angle = 0; 4138 attributes.attribute_flags.value = 0; 4139 4140 attributes.pitch = attributes.width; 4141 4142 if (crtc_state->stream) { 4143 if (!dc_stream_set_cursor_attributes(crtc_state->stream, 4144 &attributes)) 4145 DRM_ERROR("DC failed to set cursor attributes\n"); 4146 4147 if (!dc_stream_set_cursor_position(crtc_state->stream, 4148 &position)) 4149 DRM_ERROR("DC failed to set cursor position\n"); 4150 } 4151 } 4152 4153 static void prepare_flip_isr(struct amdgpu_crtc *acrtc) 4154 { 4155 4156 assert_spin_locked(&acrtc->base.dev->event_lock); 4157 WARN_ON(acrtc->event); 4158 4159 acrtc->event = acrtc->base.state->event; 4160 4161 /* Set the flip status */ 4162 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED; 4163 4164 /* Mark this event as consumed */ 4165 acrtc->base.state->event = NULL; 4166 4167 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n", 4168 acrtc->crtc_id); 4169 } 4170 4171 /* 4172 * Executes flip 4173 * 4174 * Waits on all BO's fences and for proper vblank count 4175 */ 4176 static void amdgpu_dm_do_flip(struct drm_crtc *crtc, 4177 struct drm_framebuffer *fb, 4178 uint32_t target, 4179 struct dc_state *state) 4180 { 4181 unsigned long flags; 4182 uint32_t target_vblank; 4183 int r, vpos, hpos; 4184 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4185 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(fb); 4186 struct amdgpu_bo *abo = gem_to_amdgpu_bo(fb->obj[0]); 4187 struct amdgpu_device *adev = crtc->dev->dev_private; 4188 bool async_flip = (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0; 4189 struct dc_flip_addrs addr = { {0} }; 4190 /* TODO eliminate or rename surface_update */ 4191 struct dc_surface_update surface_updates[1] = { {0} }; 4192 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state); 4193 4194 4195 /* Prepare wait for target vblank early - before the fence-waits */ 4196 target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) + 4197 amdgpu_get_vblank_counter_kms(crtc->dev, acrtc->crtc_id); 4198 4199 /* TODO This might fail and hence better not used, wait 4200 * explicitly on fences instead 4201 * and in general should be called for 4202 * blocking commit to as per framework helpers 4203 */ 4204 r = amdgpu_bo_reserve(abo, true); 4205 if (unlikely(r != 0)) { 4206 DRM_ERROR("failed to reserve buffer before flip\n"); 4207 WARN_ON(1); 4208 } 4209 4210 /* Wait for all fences on this FB */ 4211 WARN_ON(reservation_object_wait_timeout_rcu(abo->tbo.resv, true, false, 4212 MAX_SCHEDULE_TIMEOUT) < 0); 4213 4214 amdgpu_bo_unreserve(abo); 4215 4216 /* Wait until we're out of the vertical blank period before the one 4217 * targeted by the flip 4218 */ 4219 while ((acrtc->enabled && 4220 (amdgpu_display_get_crtc_scanoutpos(adev->ddev, acrtc->crtc_id, 4221 0, &vpos, &hpos, NULL, 4222 NULL, &crtc->hwmode) 4223 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) == 4224 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) && 4225 (int)(target_vblank - 4226 amdgpu_get_vblank_counter_kms(adev->ddev, acrtc->crtc_id)) > 0)) { 4227 usleep_range(1000, 1100); 4228 } 4229 4230 /* Flip */ 4231 spin_lock_irqsave(&crtc->dev->event_lock, flags); 4232 4233 WARN_ON(acrtc->pflip_status != AMDGPU_FLIP_NONE); 4234 WARN_ON(!acrtc_state->stream); 4235 4236 addr.address.grph.addr.low_part = lower_32_bits(afb->address); 4237 addr.address.grph.addr.high_part = upper_32_bits(afb->address); 4238 addr.flip_immediate = async_flip; 4239 4240 4241 if (acrtc->base.state->event) 4242 prepare_flip_isr(acrtc); 4243 4244 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 4245 4246 surface_updates->surface = dc_stream_get_status(acrtc_state->stream)->plane_states[0]; 4247 surface_updates->flip_addr = &addr; 4248 4249 dc_commit_updates_for_stream(adev->dm.dc, 4250 surface_updates, 4251 1, 4252 acrtc_state->stream, 4253 NULL, 4254 &surface_updates->surface, 4255 state); 4256 4257 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x \n", 4258 __func__, 4259 addr.address.grph.addr.high_part, 4260 addr.address.grph.addr.low_part); 4261 } 4262 4263 /* 4264 * TODO this whole function needs to go 4265 * 4266 * dc_surface_update is needlessly complex. See if we can just replace this 4267 * with a dc_plane_state and follow the atomic model a bit more closely here. 4268 */ 4269 static bool commit_planes_to_stream( 4270 struct dc *dc, 4271 struct dc_plane_state **plane_states, 4272 uint8_t new_plane_count, 4273 struct dm_crtc_state *dm_new_crtc_state, 4274 struct dm_crtc_state *dm_old_crtc_state, 4275 struct dc_state *state) 4276 { 4277 /* no need to dynamically allocate this. it's pretty small */ 4278 struct dc_surface_update updates[MAX_SURFACES]; 4279 struct dc_flip_addrs *flip_addr; 4280 struct dc_plane_info *plane_info; 4281 struct dc_scaling_info *scaling_info; 4282 int i; 4283 struct dc_stream_state *dc_stream = dm_new_crtc_state->stream; 4284 struct dc_stream_update *stream_update = 4285 kzalloc(sizeof(struct dc_stream_update), GFP_KERNEL); 4286 4287 if (!stream_update) { 4288 BREAK_TO_DEBUGGER(); 4289 return false; 4290 } 4291 4292 flip_addr = kcalloc(MAX_SURFACES, sizeof(struct dc_flip_addrs), 4293 GFP_KERNEL); 4294 plane_info = kcalloc(MAX_SURFACES, sizeof(struct dc_plane_info), 4295 GFP_KERNEL); 4296 scaling_info = kcalloc(MAX_SURFACES, sizeof(struct dc_scaling_info), 4297 GFP_KERNEL); 4298 4299 if (!flip_addr || !plane_info || !scaling_info) { 4300 kfree(flip_addr); 4301 kfree(plane_info); 4302 kfree(scaling_info); 4303 kfree(stream_update); 4304 return false; 4305 } 4306 4307 memset(updates, 0, sizeof(updates)); 4308 4309 stream_update->src = dc_stream->src; 4310 stream_update->dst = dc_stream->dst; 4311 stream_update->out_transfer_func = dc_stream->out_transfer_func; 4312 4313 for (i = 0; i < new_plane_count; i++) { 4314 updates[i].surface = plane_states[i]; 4315 updates[i].gamma = 4316 (struct dc_gamma *)plane_states[i]->gamma_correction; 4317 updates[i].in_transfer_func = plane_states[i]->in_transfer_func; 4318 flip_addr[i].address = plane_states[i]->address; 4319 flip_addr[i].flip_immediate = plane_states[i]->flip_immediate; 4320 plane_info[i].color_space = plane_states[i]->color_space; 4321 plane_info[i].format = plane_states[i]->format; 4322 plane_info[i].plane_size = plane_states[i]->plane_size; 4323 plane_info[i].rotation = plane_states[i]->rotation; 4324 plane_info[i].horizontal_mirror = plane_states[i]->horizontal_mirror; 4325 plane_info[i].stereo_format = plane_states[i]->stereo_format; 4326 plane_info[i].tiling_info = plane_states[i]->tiling_info; 4327 plane_info[i].visible = plane_states[i]->visible; 4328 plane_info[i].per_pixel_alpha = plane_states[i]->per_pixel_alpha; 4329 plane_info[i].dcc = plane_states[i]->dcc; 4330 scaling_info[i].scaling_quality = plane_states[i]->scaling_quality; 4331 scaling_info[i].src_rect = plane_states[i]->src_rect; 4332 scaling_info[i].dst_rect = plane_states[i]->dst_rect; 4333 scaling_info[i].clip_rect = plane_states[i]->clip_rect; 4334 4335 updates[i].flip_addr = &flip_addr[i]; 4336 updates[i].plane_info = &plane_info[i]; 4337 updates[i].scaling_info = &scaling_info[i]; 4338 } 4339 4340 dc_commit_updates_for_stream( 4341 dc, 4342 updates, 4343 new_plane_count, 4344 dc_stream, stream_update, plane_states, state); 4345 4346 kfree(flip_addr); 4347 kfree(plane_info); 4348 kfree(scaling_info); 4349 kfree(stream_update); 4350 return true; 4351 } 4352 4353 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state, 4354 struct drm_device *dev, 4355 struct amdgpu_display_manager *dm, 4356 struct drm_crtc *pcrtc, 4357 bool *wait_for_vblank) 4358 { 4359 uint32_t i; 4360 struct drm_plane *plane; 4361 struct drm_plane_state *old_plane_state, *new_plane_state; 4362 struct dc_stream_state *dc_stream_attach; 4363 struct dc_plane_state *plane_states_constructed[MAX_SURFACES]; 4364 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc); 4365 struct drm_crtc_state *new_pcrtc_state = 4366 drm_atomic_get_new_crtc_state(state, pcrtc); 4367 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state); 4368 struct dm_crtc_state *dm_old_crtc_state = 4369 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc)); 4370 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 4371 int planes_count = 0; 4372 unsigned long flags; 4373 4374 /* update planes when needed */ 4375 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 4376 struct drm_crtc *crtc = new_plane_state->crtc; 4377 struct drm_crtc_state *new_crtc_state; 4378 struct drm_framebuffer *fb = new_plane_state->fb; 4379 bool pflip_needed; 4380 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state); 4381 4382 if (plane->type == DRM_PLANE_TYPE_CURSOR) { 4383 handle_cursor_update(plane, old_plane_state); 4384 continue; 4385 } 4386 4387 if (!fb || !crtc || pcrtc != crtc) 4388 continue; 4389 4390 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 4391 if (!new_crtc_state->active) 4392 continue; 4393 4394 pflip_needed = !state->allow_modeset; 4395 4396 spin_lock_irqsave(&crtc->dev->event_lock, flags); 4397 if (acrtc_attach->pflip_status != AMDGPU_FLIP_NONE) { 4398 DRM_ERROR("%s: acrtc %d, already busy\n", 4399 __func__, 4400 acrtc_attach->crtc_id); 4401 /* In commit tail framework this cannot happen */ 4402 WARN_ON(1); 4403 } 4404 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 4405 4406 if (!pflip_needed || plane->type == DRM_PLANE_TYPE_OVERLAY) { 4407 WARN_ON(!dm_new_plane_state->dc_state); 4408 4409 plane_states_constructed[planes_count] = dm_new_plane_state->dc_state; 4410 4411 dc_stream_attach = acrtc_state->stream; 4412 planes_count++; 4413 4414 } else if (new_crtc_state->planes_changed) { 4415 /* Assume even ONE crtc with immediate flip means 4416 * entire can't wait for VBLANK 4417 * TODO Check if it's correct 4418 */ 4419 *wait_for_vblank = 4420 new_pcrtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC ? 4421 false : true; 4422 4423 /* TODO: Needs rework for multiplane flip */ 4424 if (plane->type == DRM_PLANE_TYPE_PRIMARY) 4425 drm_crtc_vblank_get(crtc); 4426 4427 amdgpu_dm_do_flip( 4428 crtc, 4429 fb, 4430 (uint32_t)drm_crtc_vblank_count(crtc) + *wait_for_vblank, 4431 dm_state->context); 4432 } 4433 4434 } 4435 4436 if (planes_count) { 4437 unsigned long flags; 4438 4439 if (new_pcrtc_state->event) { 4440 4441 drm_crtc_vblank_get(pcrtc); 4442 4443 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 4444 prepare_flip_isr(acrtc_attach); 4445 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 4446 } 4447 4448 4449 if (false == commit_planes_to_stream(dm->dc, 4450 plane_states_constructed, 4451 planes_count, 4452 acrtc_state, 4453 dm_old_crtc_state, 4454 dm_state->context)) 4455 dm_error("%s: Failed to attach plane!\n", __func__); 4456 } else { 4457 /*TODO BUG Here should go disable planes on CRTC. */ 4458 } 4459 } 4460 4461 /** 4462 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC 4463 * @crtc_state: the DRM CRTC state 4464 * @stream_state: the DC stream state. 4465 * 4466 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring 4467 * a dc_stream_state's flags in sync with a drm_crtc_state's flags. 4468 */ 4469 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state, 4470 struct dc_stream_state *stream_state) 4471 { 4472 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state); 4473 } 4474 4475 static int amdgpu_dm_atomic_commit(struct drm_device *dev, 4476 struct drm_atomic_state *state, 4477 bool nonblock) 4478 { 4479 struct drm_crtc *crtc; 4480 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 4481 struct amdgpu_device *adev = dev->dev_private; 4482 int i; 4483 4484 /* 4485 * We evade vblanks and pflips on crtc that 4486 * should be changed. We do it here to flush & disable 4487 * interrupts before drm_swap_state is called in drm_atomic_helper_commit 4488 * it will update crtc->dm_crtc_state->stream pointer which is used in 4489 * the ISRs. 4490 */ 4491 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 4492 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4493 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4494 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4495 4496 if (drm_atomic_crtc_needs_modeset(new_crtc_state) 4497 && dm_old_crtc_state->stream) { 4498 /* 4499 * CRC capture was enabled but not disabled. 4500 * Release the vblank reference. 4501 */ 4502 if (dm_new_crtc_state->crc_enabled) { 4503 drm_crtc_vblank_put(crtc); 4504 dm_new_crtc_state->crc_enabled = false; 4505 } 4506 4507 manage_dm_interrupts(adev, acrtc, false); 4508 } 4509 } 4510 /* Add check here for SoC's that support hardware cursor plane, to 4511 * unset legacy_cursor_update */ 4512 4513 return drm_atomic_helper_commit(dev, state, nonblock); 4514 4515 /*TODO Handle EINTR, reenable IRQ*/ 4516 } 4517 4518 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state) 4519 { 4520 struct drm_device *dev = state->dev; 4521 struct amdgpu_device *adev = dev->dev_private; 4522 struct amdgpu_display_manager *dm = &adev->dm; 4523 struct dm_atomic_state *dm_state; 4524 uint32_t i, j; 4525 struct drm_crtc *crtc; 4526 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 4527 unsigned long flags; 4528 bool wait_for_vblank = true; 4529 struct drm_connector *connector; 4530 struct drm_connector_state *old_con_state, *new_con_state; 4531 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 4532 int crtc_disable_count = 0; 4533 4534 drm_atomic_helper_update_legacy_modeset_state(dev, state); 4535 4536 dm_state = to_dm_atomic_state(state); 4537 4538 /* update changed items */ 4539 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 4540 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4541 4542 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4543 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4544 4545 DRM_DEBUG_DRIVER( 4546 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, " 4547 "planes_changed:%d, mode_changed:%d,active_changed:%d," 4548 "connectors_changed:%d\n", 4549 acrtc->crtc_id, 4550 new_crtc_state->enable, 4551 new_crtc_state->active, 4552 new_crtc_state->planes_changed, 4553 new_crtc_state->mode_changed, 4554 new_crtc_state->active_changed, 4555 new_crtc_state->connectors_changed); 4556 4557 /* Copy all transient state flags into dc state */ 4558 if (dm_new_crtc_state->stream) { 4559 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base, 4560 dm_new_crtc_state->stream); 4561 } 4562 4563 /* handles headless hotplug case, updating new_state and 4564 * aconnector as needed 4565 */ 4566 4567 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) { 4568 4569 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc); 4570 4571 if (!dm_new_crtc_state->stream) { 4572 /* 4573 * this could happen because of issues with 4574 * userspace notifications delivery. 4575 * In this case userspace tries to set mode on 4576 * display which is disconnect in fact. 4577 * dc_sink in NULL in this case on aconnector. 4578 * We expect reset mode will come soon. 4579 * 4580 * This can also happen when unplug is done 4581 * during resume sequence ended 4582 * 4583 * In this case, we want to pretend we still 4584 * have a sink to keep the pipe running so that 4585 * hw state is consistent with the sw state 4586 */ 4587 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n", 4588 __func__, acrtc->base.base.id); 4589 continue; 4590 } 4591 4592 if (dm_old_crtc_state->stream) 4593 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 4594 4595 pm_runtime_get_noresume(dev->dev); 4596 4597 acrtc->enabled = true; 4598 acrtc->hw_mode = new_crtc_state->mode; 4599 crtc->hwmode = new_crtc_state->mode; 4600 } else if (modereset_required(new_crtc_state)) { 4601 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc); 4602 4603 /* i.e. reset mode */ 4604 if (dm_old_crtc_state->stream) 4605 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 4606 } 4607 } /* for_each_crtc_in_state() */ 4608 4609 /* 4610 * Add streams after required streams from new and replaced streams 4611 * are removed from freesync module 4612 */ 4613 if (adev->dm.freesync_module) { 4614 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, 4615 new_crtc_state, i) { 4616 struct amdgpu_dm_connector *aconnector = NULL; 4617 struct dm_connector_state *dm_new_con_state = NULL; 4618 struct amdgpu_crtc *acrtc = NULL; 4619 bool modeset_needed; 4620 4621 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4622 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4623 modeset_needed = modeset_required( 4624 new_crtc_state, 4625 dm_new_crtc_state->stream, 4626 dm_old_crtc_state->stream); 4627 /* We add stream to freesync if: 4628 * 1. Said stream is not null, and 4629 * 2. A modeset is requested. This means that the 4630 * stream was removed previously, and needs to be 4631 * replaced. 4632 */ 4633 if (dm_new_crtc_state->stream == NULL || 4634 !modeset_needed) 4635 continue; 4636 4637 acrtc = to_amdgpu_crtc(crtc); 4638 4639 aconnector = 4640 amdgpu_dm_find_first_crtc_matching_connector( 4641 state, crtc); 4642 if (!aconnector) { 4643 DRM_DEBUG_DRIVER("Atomic commit: Failed to " 4644 "find connector for acrtc " 4645 "id:%d skipping freesync " 4646 "init\n", 4647 acrtc->crtc_id); 4648 continue; 4649 } 4650 4651 mod_freesync_add_stream(adev->dm.freesync_module, 4652 dm_new_crtc_state->stream, 4653 &aconnector->caps); 4654 new_con_state = drm_atomic_get_new_connector_state( 4655 state, &aconnector->base); 4656 dm_new_con_state = to_dm_connector_state(new_con_state); 4657 4658 mod_freesync_set_user_enable(adev->dm.freesync_module, 4659 &dm_new_crtc_state->stream, 4660 1, 4661 &dm_new_con_state->user_enable); 4662 } 4663 } 4664 4665 if (dm_state->context) { 4666 dm_enable_per_frame_crtc_master_sync(dm_state->context); 4667 WARN_ON(!dc_commit_state(dm->dc, dm_state->context)); 4668 } 4669 4670 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 4671 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4672 4673 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4674 4675 if (dm_new_crtc_state->stream != NULL) { 4676 const struct dc_stream_status *status = 4677 dc_stream_get_status(dm_new_crtc_state->stream); 4678 4679 if (!status) 4680 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc); 4681 else 4682 acrtc->otg_inst = status->primary_otg_inst; 4683 } 4684 } 4685 4686 /* Handle scaling and underscan changes*/ 4687 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 4688 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 4689 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 4690 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 4691 struct dc_stream_status *status = NULL; 4692 4693 if (acrtc) { 4694 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 4695 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 4696 } 4697 4698 /* Skip any modesets/resets */ 4699 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state)) 4700 continue; 4701 4702 /* Skip any thing not scale or underscan changes */ 4703 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state)) 4704 continue; 4705 4706 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4707 4708 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode, 4709 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream); 4710 4711 if (!dm_new_crtc_state->stream) 4712 continue; 4713 4714 status = dc_stream_get_status(dm_new_crtc_state->stream); 4715 WARN_ON(!status); 4716 WARN_ON(!status->plane_count); 4717 4718 /*TODO How it works with MPO ?*/ 4719 if (!commit_planes_to_stream( 4720 dm->dc, 4721 status->plane_states, 4722 status->plane_count, 4723 dm_new_crtc_state, 4724 to_dm_crtc_state(old_crtc_state), 4725 dm_state->context)) 4726 dm_error("%s: Failed to update stream scaling!\n", __func__); 4727 } 4728 4729 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, 4730 new_crtc_state, i) { 4731 /* 4732 * loop to enable interrupts on newly arrived crtc 4733 */ 4734 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 4735 bool modeset_needed; 4736 4737 if (old_crtc_state->active && !new_crtc_state->active) 4738 crtc_disable_count++; 4739 4740 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4741 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4742 modeset_needed = modeset_required( 4743 new_crtc_state, 4744 dm_new_crtc_state->stream, 4745 dm_old_crtc_state->stream); 4746 4747 if (dm_new_crtc_state->stream == NULL || !modeset_needed) 4748 continue; 4749 4750 if (adev->dm.freesync_module) 4751 mod_freesync_notify_mode_change( 4752 adev->dm.freesync_module, 4753 &dm_new_crtc_state->stream, 1); 4754 4755 manage_dm_interrupts(adev, acrtc, true); 4756 } 4757 4758 /* update planes when needed per crtc*/ 4759 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) { 4760 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4761 4762 if (dm_new_crtc_state->stream) 4763 amdgpu_dm_commit_planes(state, dev, dm, crtc, &wait_for_vblank); 4764 } 4765 4766 4767 /* 4768 * send vblank event on all events not handled in flip and 4769 * mark consumed event for drm_atomic_helper_commit_hw_done 4770 */ 4771 spin_lock_irqsave(&adev->ddev->event_lock, flags); 4772 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 4773 4774 if (new_crtc_state->event) 4775 drm_send_event_locked(dev, &new_crtc_state->event->base); 4776 4777 new_crtc_state->event = NULL; 4778 } 4779 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 4780 4781 4782 if (wait_for_vblank) 4783 drm_atomic_helper_wait_for_flip_done(dev, state); 4784 4785 /* 4786 * FIXME: 4787 * Delay hw_done() until flip_done() is signaled. This is to block 4788 * another commit from freeing the CRTC state while we're still 4789 * waiting on flip_done. 4790 */ 4791 drm_atomic_helper_commit_hw_done(state); 4792 4793 drm_atomic_helper_cleanup_planes(dev, state); 4794 4795 /* Finally, drop a runtime PM reference for each newly disabled CRTC, 4796 * so we can put the GPU into runtime suspend if we're not driving any 4797 * displays anymore 4798 */ 4799 for (i = 0; i < crtc_disable_count; i++) 4800 pm_runtime_put_autosuspend(dev->dev); 4801 pm_runtime_mark_last_busy(dev->dev); 4802 } 4803 4804 4805 static int dm_force_atomic_commit(struct drm_connector *connector) 4806 { 4807 int ret = 0; 4808 struct drm_device *ddev = connector->dev; 4809 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev); 4810 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc); 4811 struct drm_plane *plane = disconnected_acrtc->base.primary; 4812 struct drm_connector_state *conn_state; 4813 struct drm_crtc_state *crtc_state; 4814 struct drm_plane_state *plane_state; 4815 4816 if (!state) 4817 return -ENOMEM; 4818 4819 state->acquire_ctx = ddev->mode_config.acquire_ctx; 4820 4821 /* Construct an atomic state to restore previous display setting */ 4822 4823 /* 4824 * Attach connectors to drm_atomic_state 4825 */ 4826 conn_state = drm_atomic_get_connector_state(state, connector); 4827 4828 ret = PTR_ERR_OR_ZERO(conn_state); 4829 if (ret) 4830 goto err; 4831 4832 /* Attach crtc to drm_atomic_state*/ 4833 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base); 4834 4835 ret = PTR_ERR_OR_ZERO(crtc_state); 4836 if (ret) 4837 goto err; 4838 4839 /* force a restore */ 4840 crtc_state->mode_changed = true; 4841 4842 /* Attach plane to drm_atomic_state */ 4843 plane_state = drm_atomic_get_plane_state(state, plane); 4844 4845 ret = PTR_ERR_OR_ZERO(plane_state); 4846 if (ret) 4847 goto err; 4848 4849 4850 /* Call commit internally with the state we just constructed */ 4851 ret = drm_atomic_commit(state); 4852 if (!ret) 4853 return 0; 4854 4855 err: 4856 DRM_ERROR("Restoring old state failed with %i\n", ret); 4857 drm_atomic_state_put(state); 4858 4859 return ret; 4860 } 4861 4862 /* 4863 * This functions handle all cases when set mode does not come upon hotplug. 4864 * This include when the same display is unplugged then plugged back into the 4865 * same port and when we are running without usermode desktop manager supprot 4866 */ 4867 void dm_restore_drm_connector_state(struct drm_device *dev, 4868 struct drm_connector *connector) 4869 { 4870 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 4871 struct amdgpu_crtc *disconnected_acrtc; 4872 struct dm_crtc_state *acrtc_state; 4873 4874 if (!aconnector->dc_sink || !connector->state || !connector->encoder) 4875 return; 4876 4877 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc); 4878 if (!disconnected_acrtc) 4879 return; 4880 4881 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state); 4882 if (!acrtc_state->stream) 4883 return; 4884 4885 /* 4886 * If the previous sink is not released and different from the current, 4887 * we deduce we are in a state where we can not rely on usermode call 4888 * to turn on the display, so we do it here 4889 */ 4890 if (acrtc_state->stream->sink != aconnector->dc_sink) 4891 dm_force_atomic_commit(&aconnector->base); 4892 } 4893 4894 /*` 4895 * Grabs all modesetting locks to serialize against any blocking commits, 4896 * Waits for completion of all non blocking commits. 4897 */ 4898 static int do_aquire_global_lock(struct drm_device *dev, 4899 struct drm_atomic_state *state) 4900 { 4901 struct drm_crtc *crtc; 4902 struct drm_crtc_commit *commit; 4903 long ret; 4904 4905 /* Adding all modeset locks to aquire_ctx will 4906 * ensure that when the framework release it the 4907 * extra locks we are locking here will get released to 4908 */ 4909 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx); 4910 if (ret) 4911 return ret; 4912 4913 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 4914 spin_lock(&crtc->commit_lock); 4915 commit = list_first_entry_or_null(&crtc->commit_list, 4916 struct drm_crtc_commit, commit_entry); 4917 if (commit) 4918 drm_crtc_commit_get(commit); 4919 spin_unlock(&crtc->commit_lock); 4920 4921 if (!commit) 4922 continue; 4923 4924 /* Make sure all pending HW programming completed and 4925 * page flips done 4926 */ 4927 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ); 4928 4929 if (ret > 0) 4930 ret = wait_for_completion_interruptible_timeout( 4931 &commit->flip_done, 10*HZ); 4932 4933 if (ret == 0) 4934 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done " 4935 "timed out\n", crtc->base.id, crtc->name); 4936 4937 drm_crtc_commit_put(commit); 4938 } 4939 4940 return ret < 0 ? ret : 0; 4941 } 4942 4943 static int dm_update_crtcs_state(struct dc *dc, 4944 struct drm_atomic_state *state, 4945 bool enable, 4946 bool *lock_and_validation_needed) 4947 { 4948 struct drm_crtc *crtc; 4949 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 4950 int i; 4951 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 4952 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 4953 struct dc_stream_state *new_stream; 4954 int ret = 0; 4955 4956 /*TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set */ 4957 /* update changed items */ 4958 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 4959 struct amdgpu_crtc *acrtc = NULL; 4960 struct amdgpu_dm_connector *aconnector = NULL; 4961 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL; 4962 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL; 4963 struct drm_plane_state *new_plane_state = NULL; 4964 4965 new_stream = NULL; 4966 4967 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 4968 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 4969 acrtc = to_amdgpu_crtc(crtc); 4970 4971 new_plane_state = drm_atomic_get_new_plane_state(state, new_crtc_state->crtc->primary); 4972 4973 if (new_crtc_state->enable && new_plane_state && !new_plane_state->fb) { 4974 ret = -EINVAL; 4975 goto fail; 4976 } 4977 4978 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc); 4979 4980 /* TODO This hack should go away */ 4981 if (aconnector && enable) { 4982 // Make sure fake sink is created in plug-in scenario 4983 drm_new_conn_state = drm_atomic_get_new_connector_state(state, 4984 &aconnector->base); 4985 drm_old_conn_state = drm_atomic_get_old_connector_state(state, 4986 &aconnector->base); 4987 4988 if (IS_ERR(drm_new_conn_state)) { 4989 ret = PTR_ERR_OR_ZERO(drm_new_conn_state); 4990 break; 4991 } 4992 4993 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state); 4994 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state); 4995 4996 new_stream = create_stream_for_sink(aconnector, 4997 &new_crtc_state->mode, 4998 dm_new_conn_state); 4999 5000 /* 5001 * we can have no stream on ACTION_SET if a display 5002 * was disconnected during S3, in this case it not and 5003 * error, the OS will be updated after detection, and 5004 * do the right thing on next atomic commit 5005 */ 5006 5007 if (!new_stream) { 5008 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n", 5009 __func__, acrtc->base.base.id); 5010 break; 5011 } 5012 5013 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) && 5014 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) { 5015 new_crtc_state->mode_changed = false; 5016 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d", 5017 new_crtc_state->mode_changed); 5018 } 5019 } 5020 5021 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 5022 goto next_crtc; 5023 5024 DRM_DEBUG_DRIVER( 5025 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, " 5026 "planes_changed:%d, mode_changed:%d,active_changed:%d," 5027 "connectors_changed:%d\n", 5028 acrtc->crtc_id, 5029 new_crtc_state->enable, 5030 new_crtc_state->active, 5031 new_crtc_state->planes_changed, 5032 new_crtc_state->mode_changed, 5033 new_crtc_state->active_changed, 5034 new_crtc_state->connectors_changed); 5035 5036 /* Remove stream for any changed/disabled CRTC */ 5037 if (!enable) { 5038 5039 if (!dm_old_crtc_state->stream) 5040 goto next_crtc; 5041 5042 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n", 5043 crtc->base.id); 5044 5045 /* i.e. reset mode */ 5046 if (dc_remove_stream_from_ctx( 5047 dc, 5048 dm_state->context, 5049 dm_old_crtc_state->stream) != DC_OK) { 5050 ret = -EINVAL; 5051 goto fail; 5052 } 5053 5054 dc_stream_release(dm_old_crtc_state->stream); 5055 dm_new_crtc_state->stream = NULL; 5056 5057 *lock_and_validation_needed = true; 5058 5059 } else {/* Add stream for any updated/enabled CRTC */ 5060 /* 5061 * Quick fix to prevent NULL pointer on new_stream when 5062 * added MST connectors not found in existing crtc_state in the chained mode 5063 * TODO: need to dig out the root cause of that 5064 */ 5065 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port)) 5066 goto next_crtc; 5067 5068 if (modereset_required(new_crtc_state)) 5069 goto next_crtc; 5070 5071 if (modeset_required(new_crtc_state, new_stream, 5072 dm_old_crtc_state->stream)) { 5073 5074 WARN_ON(dm_new_crtc_state->stream); 5075 5076 dm_new_crtc_state->stream = new_stream; 5077 5078 dc_stream_retain(new_stream); 5079 5080 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n", 5081 crtc->base.id); 5082 5083 if (dc_add_stream_to_ctx( 5084 dc, 5085 dm_state->context, 5086 dm_new_crtc_state->stream) != DC_OK) { 5087 ret = -EINVAL; 5088 goto fail; 5089 } 5090 5091 *lock_and_validation_needed = true; 5092 } 5093 } 5094 5095 next_crtc: 5096 /* Release extra reference */ 5097 if (new_stream) 5098 dc_stream_release(new_stream); 5099 5100 /* 5101 * We want to do dc stream updates that do not require a 5102 * full modeset below. 5103 */ 5104 if (!(enable && aconnector && new_crtc_state->enable && 5105 new_crtc_state->active)) 5106 continue; 5107 /* 5108 * Given above conditions, the dc state cannot be NULL because: 5109 * 1. We're in the process of enabling CRTCs (just been added 5110 * to the dc context, or already is on the context) 5111 * 2. Has a valid connector attached, and 5112 * 3. Is currently active and enabled. 5113 * => The dc stream state currently exists. 5114 */ 5115 BUG_ON(dm_new_crtc_state->stream == NULL); 5116 5117 /* Scaling or underscan settings */ 5118 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state)) 5119 update_stream_scaling_settings( 5120 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream); 5121 5122 /* 5123 * Color management settings. We also update color properties 5124 * when a modeset is needed, to ensure it gets reprogrammed. 5125 */ 5126 if (dm_new_crtc_state->base.color_mgmt_changed || 5127 drm_atomic_crtc_needs_modeset(new_crtc_state)) { 5128 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state); 5129 if (ret) 5130 goto fail; 5131 amdgpu_dm_set_ctm(dm_new_crtc_state); 5132 } 5133 } 5134 5135 return ret; 5136 5137 fail: 5138 if (new_stream) 5139 dc_stream_release(new_stream); 5140 return ret; 5141 } 5142 5143 static int dm_update_planes_state(struct dc *dc, 5144 struct drm_atomic_state *state, 5145 bool enable, 5146 bool *lock_and_validation_needed) 5147 { 5148 struct drm_crtc *new_plane_crtc, *old_plane_crtc; 5149 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5150 struct drm_plane *plane; 5151 struct drm_plane_state *old_plane_state, *new_plane_state; 5152 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state; 5153 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 5154 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state; 5155 int i ; 5156 /* TODO return page_flip_needed() function */ 5157 bool pflip_needed = !state->allow_modeset; 5158 int ret = 0; 5159 5160 5161 /* Add new planes, in reverse order as DC expectation */ 5162 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) { 5163 new_plane_crtc = new_plane_state->crtc; 5164 old_plane_crtc = old_plane_state->crtc; 5165 dm_new_plane_state = to_dm_plane_state(new_plane_state); 5166 dm_old_plane_state = to_dm_plane_state(old_plane_state); 5167 5168 /*TODO Implement atomic check for cursor plane */ 5169 if (plane->type == DRM_PLANE_TYPE_CURSOR) 5170 continue; 5171 5172 /* Remove any changed/removed planes */ 5173 if (!enable) { 5174 if (pflip_needed && 5175 plane->type != DRM_PLANE_TYPE_OVERLAY) 5176 continue; 5177 5178 if (!old_plane_crtc) 5179 continue; 5180 5181 old_crtc_state = drm_atomic_get_old_crtc_state( 5182 state, old_plane_crtc); 5183 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5184 5185 if (!dm_old_crtc_state->stream) 5186 continue; 5187 5188 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n", 5189 plane->base.id, old_plane_crtc->base.id); 5190 5191 if (!dc_remove_plane_from_context( 5192 dc, 5193 dm_old_crtc_state->stream, 5194 dm_old_plane_state->dc_state, 5195 dm_state->context)) { 5196 5197 ret = EINVAL; 5198 return ret; 5199 } 5200 5201 5202 dc_plane_state_release(dm_old_plane_state->dc_state); 5203 dm_new_plane_state->dc_state = NULL; 5204 5205 *lock_and_validation_needed = true; 5206 5207 } else { /* Add new planes */ 5208 struct dc_plane_state *dc_new_plane_state; 5209 5210 if (drm_atomic_plane_disabling(plane->state, new_plane_state)) 5211 continue; 5212 5213 if (!new_plane_crtc) 5214 continue; 5215 5216 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc); 5217 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5218 5219 if (!dm_new_crtc_state->stream) 5220 continue; 5221 5222 if (pflip_needed && 5223 plane->type != DRM_PLANE_TYPE_OVERLAY) 5224 continue; 5225 5226 WARN_ON(dm_new_plane_state->dc_state); 5227 5228 dc_new_plane_state = dc_create_plane_state(dc); 5229 if (!dc_new_plane_state) 5230 return -ENOMEM; 5231 5232 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n", 5233 plane->base.id, new_plane_crtc->base.id); 5234 5235 ret = fill_plane_attributes( 5236 new_plane_crtc->dev->dev_private, 5237 dc_new_plane_state, 5238 new_plane_state, 5239 new_crtc_state); 5240 if (ret) { 5241 dc_plane_state_release(dc_new_plane_state); 5242 return ret; 5243 } 5244 5245 /* 5246 * Any atomic check errors that occur after this will 5247 * not need a release. The plane state will be attached 5248 * to the stream, and therefore part of the atomic 5249 * state. It'll be released when the atomic state is 5250 * cleaned. 5251 */ 5252 if (!dc_add_plane_to_context( 5253 dc, 5254 dm_new_crtc_state->stream, 5255 dc_new_plane_state, 5256 dm_state->context)) { 5257 5258 dc_plane_state_release(dc_new_plane_state); 5259 return -EINVAL; 5260 } 5261 5262 dm_new_plane_state->dc_state = dc_new_plane_state; 5263 5264 /* Tell DC to do a full surface update every time there 5265 * is a plane change. Inefficient, but works for now. 5266 */ 5267 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1; 5268 5269 *lock_and_validation_needed = true; 5270 } 5271 } 5272 5273 5274 return ret; 5275 } 5276 5277 static int amdgpu_dm_atomic_check(struct drm_device *dev, 5278 struct drm_atomic_state *state) 5279 { 5280 struct amdgpu_device *adev = dev->dev_private; 5281 struct dc *dc = adev->dm.dc; 5282 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 5283 struct drm_connector *connector; 5284 struct drm_connector_state *old_con_state, *new_con_state; 5285 struct drm_crtc *crtc; 5286 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5287 int ret, i; 5288 5289 /* 5290 * This bool will be set for true for any modeset/reset 5291 * or plane update which implies non fast surface update. 5292 */ 5293 bool lock_and_validation_needed = false; 5294 5295 ret = drm_atomic_helper_check_modeset(dev, state); 5296 if (ret) 5297 goto fail; 5298 5299 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 5300 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) && 5301 !new_crtc_state->color_mgmt_changed) 5302 continue; 5303 5304 if (!new_crtc_state->enable) 5305 continue; 5306 5307 ret = drm_atomic_add_affected_connectors(state, crtc); 5308 if (ret) 5309 return ret; 5310 5311 ret = drm_atomic_add_affected_planes(state, crtc); 5312 if (ret) 5313 goto fail; 5314 } 5315 5316 dm_state->context = dc_create_state(); 5317 ASSERT(dm_state->context); 5318 dc_resource_state_copy_construct_current(dc, dm_state->context); 5319 5320 /* Remove exiting planes if they are modified */ 5321 ret = dm_update_planes_state(dc, state, false, &lock_and_validation_needed); 5322 if (ret) { 5323 goto fail; 5324 } 5325 5326 /* Disable all crtcs which require disable */ 5327 ret = dm_update_crtcs_state(dc, state, false, &lock_and_validation_needed); 5328 if (ret) { 5329 goto fail; 5330 } 5331 5332 /* Enable all crtcs which require enable */ 5333 ret = dm_update_crtcs_state(dc, state, true, &lock_and_validation_needed); 5334 if (ret) { 5335 goto fail; 5336 } 5337 5338 /* Add new/modified planes */ 5339 ret = dm_update_planes_state(dc, state, true, &lock_and_validation_needed); 5340 if (ret) { 5341 goto fail; 5342 } 5343 5344 /* Run this here since we want to validate the streams we created */ 5345 ret = drm_atomic_helper_check_planes(dev, state); 5346 if (ret) 5347 goto fail; 5348 5349 /* Check scaling and underscan changes*/ 5350 /*TODO Removed scaling changes validation due to inability to commit 5351 * new stream into context w\o causing full reset. Need to 5352 * decide how to handle. 5353 */ 5354 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 5355 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 5356 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 5357 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 5358 5359 /* Skip any modesets/resets */ 5360 if (!acrtc || drm_atomic_crtc_needs_modeset( 5361 drm_atomic_get_new_crtc_state(state, &acrtc->base))) 5362 continue; 5363 5364 /* Skip any thing not scale or underscan changes */ 5365 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state)) 5366 continue; 5367 5368 lock_and_validation_needed = true; 5369 } 5370 5371 /* 5372 * For full updates case when 5373 * removing/adding/updating streams on once CRTC while flipping 5374 * on another CRTC, 5375 * acquiring global lock will guarantee that any such full 5376 * update commit 5377 * will wait for completion of any outstanding flip using DRMs 5378 * synchronization events. 5379 */ 5380 5381 if (lock_and_validation_needed) { 5382 5383 ret = do_aquire_global_lock(dev, state); 5384 if (ret) 5385 goto fail; 5386 5387 if (dc_validate_global_state(dc, dm_state->context) != DC_OK) { 5388 ret = -EINVAL; 5389 goto fail; 5390 } 5391 } 5392 5393 /* Must be success */ 5394 WARN_ON(ret); 5395 return ret; 5396 5397 fail: 5398 if (ret == -EDEADLK) 5399 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n"); 5400 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS) 5401 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n"); 5402 else 5403 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret); 5404 5405 return ret; 5406 } 5407 5408 static bool is_dp_capable_without_timing_msa(struct dc *dc, 5409 struct amdgpu_dm_connector *amdgpu_dm_connector) 5410 { 5411 uint8_t dpcd_data; 5412 bool capable = false; 5413 5414 if (amdgpu_dm_connector->dc_link && 5415 dm_helpers_dp_read_dpcd( 5416 NULL, 5417 amdgpu_dm_connector->dc_link, 5418 DP_DOWN_STREAM_PORT_COUNT, 5419 &dpcd_data, 5420 sizeof(dpcd_data))) { 5421 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false; 5422 } 5423 5424 return capable; 5425 } 5426 void amdgpu_dm_add_sink_to_freesync_module(struct drm_connector *connector, 5427 struct edid *edid) 5428 { 5429 int i; 5430 bool edid_check_required; 5431 struct detailed_timing *timing; 5432 struct detailed_non_pixel *data; 5433 struct detailed_data_monitor_range *range; 5434 struct amdgpu_dm_connector *amdgpu_dm_connector = 5435 to_amdgpu_dm_connector(connector); 5436 struct dm_connector_state *dm_con_state; 5437 5438 struct drm_device *dev = connector->dev; 5439 struct amdgpu_device *adev = dev->dev_private; 5440 5441 if (!connector->state) { 5442 DRM_ERROR("%s - Connector has no state", __func__); 5443 return; 5444 } 5445 5446 dm_con_state = to_dm_connector_state(connector->state); 5447 5448 edid_check_required = false; 5449 if (!amdgpu_dm_connector->dc_sink) { 5450 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n"); 5451 return; 5452 } 5453 if (!adev->dm.freesync_module) 5454 return; 5455 /* 5456 * if edid non zero restrict freesync only for dp and edp 5457 */ 5458 if (edid) { 5459 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT 5460 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) { 5461 edid_check_required = is_dp_capable_without_timing_msa( 5462 adev->dm.dc, 5463 amdgpu_dm_connector); 5464 } 5465 } 5466 dm_con_state->freesync_capable = false; 5467 if (edid_check_required == true && (edid->version > 1 || 5468 (edid->version == 1 && edid->revision > 1))) { 5469 for (i = 0; i < 4; i++) { 5470 5471 timing = &edid->detailed_timings[i]; 5472 data = &timing->data.other_data; 5473 range = &data->data.range; 5474 /* 5475 * Check if monitor has continuous frequency mode 5476 */ 5477 if (data->type != EDID_DETAIL_MONITOR_RANGE) 5478 continue; 5479 /* 5480 * Check for flag range limits only. If flag == 1 then 5481 * no additional timing information provided. 5482 * Default GTF, GTF Secondary curve and CVT are not 5483 * supported 5484 */ 5485 if (range->flags != 1) 5486 continue; 5487 5488 amdgpu_dm_connector->min_vfreq = range->min_vfreq; 5489 amdgpu_dm_connector->max_vfreq = range->max_vfreq; 5490 amdgpu_dm_connector->pixel_clock_mhz = 5491 range->pixel_clock_mhz * 10; 5492 break; 5493 } 5494 5495 if (amdgpu_dm_connector->max_vfreq - 5496 amdgpu_dm_connector->min_vfreq > 10) { 5497 amdgpu_dm_connector->caps.supported = true; 5498 amdgpu_dm_connector->caps.min_refresh_in_micro_hz = 5499 amdgpu_dm_connector->min_vfreq * 1000000; 5500 amdgpu_dm_connector->caps.max_refresh_in_micro_hz = 5501 amdgpu_dm_connector->max_vfreq * 1000000; 5502 dm_con_state->freesync_capable = true; 5503 } 5504 } 5505 5506 /* 5507 * TODO figure out how to notify user-mode or DRM of freesync caps 5508 * once we figure out how to deal with freesync in an upstreamable 5509 * fashion 5510 */ 5511 5512 } 5513 5514 void amdgpu_dm_remove_sink_from_freesync_module(struct drm_connector *connector) 5515 { 5516 /* 5517 * TODO fill in once we figure out how to deal with freesync in 5518 * an upstreamable fashion 5519 */ 5520 } 5521