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