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 /* The caprices of the preprocessor require that this be declared right here */ 27 #define CREATE_TRACE_POINTS 28 29 #include "dm_services_types.h" 30 #include "dc.h" 31 #include "dc/inc/core_types.h" 32 #include "dal_asic_id.h" 33 34 #include "vid.h" 35 #include "amdgpu.h" 36 #include "amdgpu_display.h" 37 #include "amdgpu_ucode.h" 38 #include "atom.h" 39 #include "amdgpu_dm.h" 40 #include "amdgpu_pm.h" 41 42 #include "amd_shared.h" 43 #include "amdgpu_dm_irq.h" 44 #include "dm_helpers.h" 45 #include "amdgpu_dm_mst_types.h" 46 #if defined(CONFIG_DEBUG_FS) 47 #include "amdgpu_dm_debugfs.h" 48 #endif 49 50 #include "ivsrcid/ivsrcid_vislands30.h" 51 52 #include <linux/module.h> 53 #include <linux/moduleparam.h> 54 #include <linux/version.h> 55 #include <linux/types.h> 56 #include <linux/pm_runtime.h> 57 #include <linux/firmware.h> 58 59 #include <drm/drmP.h> 60 #include <drm/drm_atomic.h> 61 #include <drm/drm_atomic_uapi.h> 62 #include <drm/drm_atomic_helper.h> 63 #include <drm/drm_dp_mst_helper.h> 64 #include <drm/drm_fb_helper.h> 65 #include <drm/drm_edid.h> 66 67 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 68 #include "ivsrcid/irqsrcs_dcn_1_0.h" 69 70 #include "dcn/dcn_1_0_offset.h" 71 #include "dcn/dcn_1_0_sh_mask.h" 72 #include "soc15_hw_ip.h" 73 #include "vega10_ip_offset.h" 74 75 #include "soc15_common.h" 76 #endif 77 78 #include "modules/inc/mod_freesync.h" 79 #include "modules/power/power_helpers.h" 80 #include "modules/inc/mod_info_packet.h" 81 82 #define FIRMWARE_RAVEN_DMCU "amdgpu/raven_dmcu.bin" 83 MODULE_FIRMWARE(FIRMWARE_RAVEN_DMCU); 84 85 /** 86 * DOC: overview 87 * 88 * The AMDgpu display manager, **amdgpu_dm** (or even simpler, 89 * **dm**) sits between DRM and DC. It acts as a liason, converting DRM 90 * requests into DC requests, and DC responses into DRM responses. 91 * 92 * The root control structure is &struct amdgpu_display_manager. 93 */ 94 95 /* basic init/fini API */ 96 static int amdgpu_dm_init(struct amdgpu_device *adev); 97 static void amdgpu_dm_fini(struct amdgpu_device *adev); 98 99 /* 100 * initializes drm_device display related structures, based on the information 101 * provided by DAL. The drm strcutures are: drm_crtc, drm_connector, 102 * drm_encoder, drm_mode_config 103 * 104 * Returns 0 on success 105 */ 106 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev); 107 /* removes and deallocates the drm structures, created by the above function */ 108 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm); 109 110 static void 111 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector); 112 113 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm, 114 struct drm_plane *plane, 115 unsigned long possible_crtcs, 116 const struct dc_plane_cap *plane_cap); 117 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 118 struct drm_plane *plane, 119 uint32_t link_index); 120 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm, 121 struct amdgpu_dm_connector *amdgpu_dm_connector, 122 uint32_t link_index, 123 struct amdgpu_encoder *amdgpu_encoder); 124 static int amdgpu_dm_encoder_init(struct drm_device *dev, 125 struct amdgpu_encoder *aencoder, 126 uint32_t link_index); 127 128 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector); 129 130 static int amdgpu_dm_atomic_commit(struct drm_device *dev, 131 struct drm_atomic_state *state, 132 bool nonblock); 133 134 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state); 135 136 static int amdgpu_dm_atomic_check(struct drm_device *dev, 137 struct drm_atomic_state *state); 138 139 static void handle_cursor_update(struct drm_plane *plane, 140 struct drm_plane_state *old_plane_state); 141 142 /* 143 * dm_vblank_get_counter 144 * 145 * @brief 146 * Get counter for number of vertical blanks 147 * 148 * @param 149 * struct amdgpu_device *adev - [in] desired amdgpu device 150 * int disp_idx - [in] which CRTC to get the counter from 151 * 152 * @return 153 * Counter for vertical blanks 154 */ 155 static u32 dm_vblank_get_counter(struct amdgpu_device *adev, int crtc) 156 { 157 if (crtc >= adev->mode_info.num_crtc) 158 return 0; 159 else { 160 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc]; 161 struct dm_crtc_state *acrtc_state = to_dm_crtc_state( 162 acrtc->base.state); 163 164 165 if (acrtc_state->stream == NULL) { 166 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n", 167 crtc); 168 return 0; 169 } 170 171 return dc_stream_get_vblank_counter(acrtc_state->stream); 172 } 173 } 174 175 static int dm_crtc_get_scanoutpos(struct amdgpu_device *adev, int crtc, 176 u32 *vbl, u32 *position) 177 { 178 uint32_t v_blank_start, v_blank_end, h_position, v_position; 179 180 if ((crtc < 0) || (crtc >= adev->mode_info.num_crtc)) 181 return -EINVAL; 182 else { 183 struct amdgpu_crtc *acrtc = adev->mode_info.crtcs[crtc]; 184 struct dm_crtc_state *acrtc_state = to_dm_crtc_state( 185 acrtc->base.state); 186 187 if (acrtc_state->stream == NULL) { 188 DRM_ERROR("dc_stream_state is NULL for crtc '%d'!\n", 189 crtc); 190 return 0; 191 } 192 193 /* 194 * TODO rework base driver to use values directly. 195 * for now parse it back into reg-format 196 */ 197 dc_stream_get_scanoutpos(acrtc_state->stream, 198 &v_blank_start, 199 &v_blank_end, 200 &h_position, 201 &v_position); 202 203 *position = v_position | (h_position << 16); 204 *vbl = v_blank_start | (v_blank_end << 16); 205 } 206 207 return 0; 208 } 209 210 static bool dm_is_idle(void *handle) 211 { 212 /* XXX todo */ 213 return true; 214 } 215 216 static int dm_wait_for_idle(void *handle) 217 { 218 /* XXX todo */ 219 return 0; 220 } 221 222 static bool dm_check_soft_reset(void *handle) 223 { 224 return false; 225 } 226 227 static int dm_soft_reset(void *handle) 228 { 229 /* XXX todo */ 230 return 0; 231 } 232 233 static struct amdgpu_crtc * 234 get_crtc_by_otg_inst(struct amdgpu_device *adev, 235 int otg_inst) 236 { 237 struct drm_device *dev = adev->ddev; 238 struct drm_crtc *crtc; 239 struct amdgpu_crtc *amdgpu_crtc; 240 241 if (otg_inst == -1) { 242 WARN_ON(1); 243 return adev->mode_info.crtcs[0]; 244 } 245 246 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 247 amdgpu_crtc = to_amdgpu_crtc(crtc); 248 249 if (amdgpu_crtc->otg_inst == otg_inst) 250 return amdgpu_crtc; 251 } 252 253 return NULL; 254 } 255 256 static inline bool amdgpu_dm_vrr_active(struct dm_crtc_state *dm_state) 257 { 258 return dm_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE || 259 dm_state->freesync_config.state == VRR_STATE_ACTIVE_FIXED; 260 } 261 262 static void dm_pflip_high_irq(void *interrupt_params) 263 { 264 struct amdgpu_crtc *amdgpu_crtc; 265 struct common_irq_params *irq_params = interrupt_params; 266 struct amdgpu_device *adev = irq_params->adev; 267 unsigned long flags; 268 struct drm_pending_vblank_event *e; 269 struct dm_crtc_state *acrtc_state; 270 uint32_t vpos, hpos, v_blank_start, v_blank_end; 271 bool vrr_active; 272 273 amdgpu_crtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_PFLIP); 274 275 /* IRQ could occur when in initial stage */ 276 /* TODO work and BO cleanup */ 277 if (amdgpu_crtc == NULL) { 278 DRM_DEBUG_DRIVER("CRTC is null, returning.\n"); 279 return; 280 } 281 282 spin_lock_irqsave(&adev->ddev->event_lock, flags); 283 284 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_SUBMITTED){ 285 DRM_DEBUG_DRIVER("amdgpu_crtc->pflip_status = %d !=AMDGPU_FLIP_SUBMITTED(%d) on crtc:%d[%p] \n", 286 amdgpu_crtc->pflip_status, 287 AMDGPU_FLIP_SUBMITTED, 288 amdgpu_crtc->crtc_id, 289 amdgpu_crtc); 290 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 291 return; 292 } 293 294 /* page flip completed. */ 295 e = amdgpu_crtc->event; 296 amdgpu_crtc->event = NULL; 297 298 if (!e) 299 WARN_ON(1); 300 301 acrtc_state = to_dm_crtc_state(amdgpu_crtc->base.state); 302 vrr_active = amdgpu_dm_vrr_active(acrtc_state); 303 304 /* Fixed refresh rate, or VRR scanout position outside front-porch? */ 305 if (!vrr_active || 306 !dc_stream_get_scanoutpos(acrtc_state->stream, &v_blank_start, 307 &v_blank_end, &hpos, &vpos) || 308 (vpos < v_blank_start)) { 309 /* Update to correct count and vblank timestamp if racing with 310 * vblank irq. This also updates to the correct vblank timestamp 311 * even in VRR mode, as scanout is past the front-porch atm. 312 */ 313 drm_crtc_accurate_vblank_count(&amdgpu_crtc->base); 314 315 /* Wake up userspace by sending the pageflip event with proper 316 * count and timestamp of vblank of flip completion. 317 */ 318 if (e) { 319 drm_crtc_send_vblank_event(&amdgpu_crtc->base, e); 320 321 /* Event sent, so done with vblank for this flip */ 322 drm_crtc_vblank_put(&amdgpu_crtc->base); 323 } 324 } else if (e) { 325 /* VRR active and inside front-porch: vblank count and 326 * timestamp for pageflip event will only be up to date after 327 * drm_crtc_handle_vblank() has been executed from late vblank 328 * irq handler after start of back-porch (vline 0). We queue the 329 * pageflip event for send-out by drm_crtc_handle_vblank() with 330 * updated timestamp and count, once it runs after us. 331 * 332 * We need to open-code this instead of using the helper 333 * drm_crtc_arm_vblank_event(), as that helper would 334 * call drm_crtc_accurate_vblank_count(), which we must 335 * not call in VRR mode while we are in front-porch! 336 */ 337 338 /* sequence will be replaced by real count during send-out. */ 339 e->sequence = drm_crtc_vblank_count(&amdgpu_crtc->base); 340 e->pipe = amdgpu_crtc->crtc_id; 341 342 list_add_tail(&e->base.link, &adev->ddev->vblank_event_list); 343 e = NULL; 344 } 345 346 /* Keep track of vblank of this flip for flip throttling. We use the 347 * cooked hw counter, as that one incremented at start of this vblank 348 * of pageflip completion, so last_flip_vblank is the forbidden count 349 * for queueing new pageflips if vsync + VRR is enabled. 350 */ 351 amdgpu_crtc->last_flip_vblank = amdgpu_get_vblank_counter_kms(adev->ddev, 352 amdgpu_crtc->crtc_id); 353 354 amdgpu_crtc->pflip_status = AMDGPU_FLIP_NONE; 355 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 356 357 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_NONE, vrr[%d]-fp %d\n", 358 amdgpu_crtc->crtc_id, amdgpu_crtc, 359 vrr_active, (int) !e); 360 } 361 362 static void dm_vupdate_high_irq(void *interrupt_params) 363 { 364 struct common_irq_params *irq_params = interrupt_params; 365 struct amdgpu_device *adev = irq_params->adev; 366 struct amdgpu_crtc *acrtc; 367 struct dm_crtc_state *acrtc_state; 368 unsigned long flags; 369 370 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VUPDATE); 371 372 if (acrtc) { 373 acrtc_state = to_dm_crtc_state(acrtc->base.state); 374 375 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id, 376 amdgpu_dm_vrr_active(acrtc_state)); 377 378 /* Core vblank handling is done here after end of front-porch in 379 * vrr mode, as vblank timestamping will give valid results 380 * while now done after front-porch. This will also deliver 381 * page-flip completion events that have been queued to us 382 * if a pageflip happened inside front-porch. 383 */ 384 if (amdgpu_dm_vrr_active(acrtc_state)) { 385 drm_crtc_handle_vblank(&acrtc->base); 386 387 /* BTR processing for pre-DCE12 ASICs */ 388 if (acrtc_state->stream && 389 adev->family < AMDGPU_FAMILY_AI) { 390 spin_lock_irqsave(&adev->ddev->event_lock, flags); 391 mod_freesync_handle_v_update( 392 adev->dm.freesync_module, 393 acrtc_state->stream, 394 &acrtc_state->vrr_params); 395 396 dc_stream_adjust_vmin_vmax( 397 adev->dm.dc, 398 acrtc_state->stream, 399 &acrtc_state->vrr_params.adjust); 400 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 401 } 402 } 403 } 404 } 405 406 static void dm_crtc_high_irq(void *interrupt_params) 407 { 408 struct common_irq_params *irq_params = interrupt_params; 409 struct amdgpu_device *adev = irq_params->adev; 410 struct amdgpu_crtc *acrtc; 411 struct dm_crtc_state *acrtc_state; 412 unsigned long flags; 413 414 acrtc = get_crtc_by_otg_inst(adev, irq_params->irq_src - IRQ_TYPE_VBLANK); 415 416 if (acrtc) { 417 acrtc_state = to_dm_crtc_state(acrtc->base.state); 418 419 DRM_DEBUG_DRIVER("crtc:%d, vupdate-vrr:%d\n", acrtc->crtc_id, 420 amdgpu_dm_vrr_active(acrtc_state)); 421 422 /* Core vblank handling at start of front-porch is only possible 423 * in non-vrr mode, as only there vblank timestamping will give 424 * valid results while done in front-porch. Otherwise defer it 425 * to dm_vupdate_high_irq after end of front-porch. 426 */ 427 if (!amdgpu_dm_vrr_active(acrtc_state)) 428 drm_crtc_handle_vblank(&acrtc->base); 429 430 /* Following stuff must happen at start of vblank, for crc 431 * computation and below-the-range btr support in vrr mode. 432 */ 433 amdgpu_dm_crtc_handle_crc_irq(&acrtc->base); 434 435 if (acrtc_state->stream && adev->family >= AMDGPU_FAMILY_AI && 436 acrtc_state->vrr_params.supported && 437 acrtc_state->freesync_config.state == VRR_STATE_ACTIVE_VARIABLE) { 438 spin_lock_irqsave(&adev->ddev->event_lock, flags); 439 mod_freesync_handle_v_update( 440 adev->dm.freesync_module, 441 acrtc_state->stream, 442 &acrtc_state->vrr_params); 443 444 dc_stream_adjust_vmin_vmax( 445 adev->dm.dc, 446 acrtc_state->stream, 447 &acrtc_state->vrr_params.adjust); 448 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 449 } 450 } 451 } 452 453 static int dm_set_clockgating_state(void *handle, 454 enum amd_clockgating_state state) 455 { 456 return 0; 457 } 458 459 static int dm_set_powergating_state(void *handle, 460 enum amd_powergating_state state) 461 { 462 return 0; 463 } 464 465 /* Prototypes of private functions */ 466 static int dm_early_init(void* handle); 467 468 /* Allocate memory for FBC compressed data */ 469 static void amdgpu_dm_fbc_init(struct drm_connector *connector) 470 { 471 struct drm_device *dev = connector->dev; 472 struct amdgpu_device *adev = dev->dev_private; 473 struct dm_comressor_info *compressor = &adev->dm.compressor; 474 struct amdgpu_dm_connector *aconn = to_amdgpu_dm_connector(connector); 475 struct drm_display_mode *mode; 476 unsigned long max_size = 0; 477 478 if (adev->dm.dc->fbc_compressor == NULL) 479 return; 480 481 if (aconn->dc_link->connector_signal != SIGNAL_TYPE_EDP) 482 return; 483 484 if (compressor->bo_ptr) 485 return; 486 487 488 list_for_each_entry(mode, &connector->modes, head) { 489 if (max_size < mode->htotal * mode->vtotal) 490 max_size = mode->htotal * mode->vtotal; 491 } 492 493 if (max_size) { 494 int r = amdgpu_bo_create_kernel(adev, max_size * 4, PAGE_SIZE, 495 AMDGPU_GEM_DOMAIN_GTT, &compressor->bo_ptr, 496 &compressor->gpu_addr, &compressor->cpu_addr); 497 498 if (r) 499 DRM_ERROR("DM: Failed to initialize FBC\n"); 500 else { 501 adev->dm.dc->ctx->fbc_gpu_addr = compressor->gpu_addr; 502 DRM_INFO("DM: FBC alloc %lu\n", max_size*4); 503 } 504 505 } 506 507 } 508 509 static int amdgpu_dm_init(struct amdgpu_device *adev) 510 { 511 struct dc_init_data init_data; 512 adev->dm.ddev = adev->ddev; 513 adev->dm.adev = adev; 514 515 /* Zero all the fields */ 516 memset(&init_data, 0, sizeof(init_data)); 517 518 mutex_init(&adev->dm.dc_lock); 519 520 if(amdgpu_dm_irq_init(adev)) { 521 DRM_ERROR("amdgpu: failed to initialize DM IRQ support.\n"); 522 goto error; 523 } 524 525 init_data.asic_id.chip_family = adev->family; 526 527 init_data.asic_id.pci_revision_id = adev->rev_id; 528 init_data.asic_id.hw_internal_rev = adev->external_rev_id; 529 530 init_data.asic_id.vram_width = adev->gmc.vram_width; 531 /* TODO: initialize init_data.asic_id.vram_type here!!!! */ 532 init_data.asic_id.atombios_base_address = 533 adev->mode_info.atom_context->bios; 534 535 init_data.driver = adev; 536 537 adev->dm.cgs_device = amdgpu_cgs_create_device(adev); 538 539 if (!adev->dm.cgs_device) { 540 DRM_ERROR("amdgpu: failed to create cgs device.\n"); 541 goto error; 542 } 543 544 init_data.cgs_device = adev->dm.cgs_device; 545 546 init_data.dce_environment = DCE_ENV_PRODUCTION_DRV; 547 548 /* 549 * TODO debug why this doesn't work on Raven 550 */ 551 if (adev->flags & AMD_IS_APU && 552 adev->asic_type >= CHIP_CARRIZO && 553 adev->asic_type < CHIP_RAVEN) 554 init_data.flags.gpu_vm_support = true; 555 556 if (amdgpu_dc_feature_mask & DC_FBC_MASK) 557 init_data.flags.fbc_support = true; 558 559 init_data.flags.power_down_display_on_boot = true; 560 561 /* Display Core create. */ 562 adev->dm.dc = dc_create(&init_data); 563 564 if (adev->dm.dc) { 565 DRM_INFO("Display Core initialized with v%s!\n", DC_VER); 566 } else { 567 DRM_INFO("Display Core failed to initialize with v%s!\n", DC_VER); 568 goto error; 569 } 570 571 adev->dm.freesync_module = mod_freesync_create(adev->dm.dc); 572 if (!adev->dm.freesync_module) { 573 DRM_ERROR( 574 "amdgpu: failed to initialize freesync_module.\n"); 575 } else 576 DRM_DEBUG_DRIVER("amdgpu: freesync_module init done %p.\n", 577 adev->dm.freesync_module); 578 579 amdgpu_dm_init_color_mod(); 580 581 if (amdgpu_dm_initialize_drm_device(adev)) { 582 DRM_ERROR( 583 "amdgpu: failed to initialize sw for display support.\n"); 584 goto error; 585 } 586 587 /* Update the actual used number of crtc */ 588 adev->mode_info.num_crtc = adev->dm.display_indexes_num; 589 590 /* TODO: Add_display_info? */ 591 592 /* TODO use dynamic cursor width */ 593 adev->ddev->mode_config.cursor_width = adev->dm.dc->caps.max_cursor_size; 594 adev->ddev->mode_config.cursor_height = adev->dm.dc->caps.max_cursor_size; 595 596 if (drm_vblank_init(adev->ddev, adev->dm.display_indexes_num)) { 597 DRM_ERROR( 598 "amdgpu: failed to initialize sw for display support.\n"); 599 goto error; 600 } 601 602 #if defined(CONFIG_DEBUG_FS) 603 if (dtn_debugfs_init(adev)) 604 DRM_ERROR("amdgpu: failed initialize dtn debugfs support.\n"); 605 #endif 606 607 DRM_DEBUG_DRIVER("KMS initialized.\n"); 608 609 return 0; 610 error: 611 amdgpu_dm_fini(adev); 612 613 return -EINVAL; 614 } 615 616 static void amdgpu_dm_fini(struct amdgpu_device *adev) 617 { 618 amdgpu_dm_destroy_drm_device(&adev->dm); 619 /* 620 * TODO: pageflip, vlank interrupt 621 * 622 * amdgpu_dm_irq_fini(adev); 623 */ 624 625 if (adev->dm.cgs_device) { 626 amdgpu_cgs_destroy_device(adev->dm.cgs_device); 627 adev->dm.cgs_device = NULL; 628 } 629 if (adev->dm.freesync_module) { 630 mod_freesync_destroy(adev->dm.freesync_module); 631 adev->dm.freesync_module = NULL; 632 } 633 /* DC Destroy TODO: Replace destroy DAL */ 634 if (adev->dm.dc) 635 dc_destroy(&adev->dm.dc); 636 637 mutex_destroy(&adev->dm.dc_lock); 638 639 return; 640 } 641 642 static int load_dmcu_fw(struct amdgpu_device *adev) 643 { 644 const char *fw_name_dmcu = NULL; 645 int r; 646 const struct dmcu_firmware_header_v1_0 *hdr; 647 648 switch(adev->asic_type) { 649 case CHIP_BONAIRE: 650 case CHIP_HAWAII: 651 case CHIP_KAVERI: 652 case CHIP_KABINI: 653 case CHIP_MULLINS: 654 case CHIP_TONGA: 655 case CHIP_FIJI: 656 case CHIP_CARRIZO: 657 case CHIP_STONEY: 658 case CHIP_POLARIS11: 659 case CHIP_POLARIS10: 660 case CHIP_POLARIS12: 661 case CHIP_VEGAM: 662 case CHIP_VEGA10: 663 case CHIP_VEGA12: 664 case CHIP_VEGA20: 665 return 0; 666 case CHIP_RAVEN: 667 #if defined(CONFIG_DRM_AMD_DC_DCN1_01) 668 if (ASICREV_IS_PICASSO(adev->external_rev_id)) 669 fw_name_dmcu = FIRMWARE_RAVEN_DMCU; 670 else if (ASICREV_IS_RAVEN2(adev->external_rev_id)) 671 fw_name_dmcu = FIRMWARE_RAVEN_DMCU; 672 else 673 #endif 674 return 0; 675 break; 676 default: 677 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type); 678 return -EINVAL; 679 } 680 681 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) { 682 DRM_DEBUG_KMS("dm: DMCU firmware not supported on direct or SMU loading\n"); 683 return 0; 684 } 685 686 r = request_firmware_direct(&adev->dm.fw_dmcu, fw_name_dmcu, adev->dev); 687 if (r == -ENOENT) { 688 /* DMCU firmware is not necessary, so don't raise a fuss if it's missing */ 689 DRM_DEBUG_KMS("dm: DMCU firmware not found\n"); 690 adev->dm.fw_dmcu = NULL; 691 return 0; 692 } 693 if (r) { 694 dev_err(adev->dev, "amdgpu_dm: Can't load firmware \"%s\"\n", 695 fw_name_dmcu); 696 return r; 697 } 698 699 r = amdgpu_ucode_validate(adev->dm.fw_dmcu); 700 if (r) { 701 dev_err(adev->dev, "amdgpu_dm: Can't validate firmware \"%s\"\n", 702 fw_name_dmcu); 703 release_firmware(adev->dm.fw_dmcu); 704 adev->dm.fw_dmcu = NULL; 705 return r; 706 } 707 708 hdr = (const struct dmcu_firmware_header_v1_0 *)adev->dm.fw_dmcu->data; 709 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].ucode_id = AMDGPU_UCODE_ID_DMCU_ERAM; 710 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_ERAM].fw = adev->dm.fw_dmcu; 711 adev->firmware.fw_size += 712 ALIGN(le32_to_cpu(hdr->header.ucode_size_bytes) - le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 713 714 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].ucode_id = AMDGPU_UCODE_ID_DMCU_INTV; 715 adev->firmware.ucode[AMDGPU_UCODE_ID_DMCU_INTV].fw = adev->dm.fw_dmcu; 716 adev->firmware.fw_size += 717 ALIGN(le32_to_cpu(hdr->intv_size_bytes), PAGE_SIZE); 718 719 adev->dm.dmcu_fw_version = le32_to_cpu(hdr->header.ucode_version); 720 721 DRM_DEBUG_KMS("PSP loading DMCU firmware\n"); 722 723 return 0; 724 } 725 726 static int dm_sw_init(void *handle) 727 { 728 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 729 730 return load_dmcu_fw(adev); 731 } 732 733 static int dm_sw_fini(void *handle) 734 { 735 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 736 737 if(adev->dm.fw_dmcu) { 738 release_firmware(adev->dm.fw_dmcu); 739 adev->dm.fw_dmcu = NULL; 740 } 741 742 return 0; 743 } 744 745 static int detect_mst_link_for_all_connectors(struct drm_device *dev) 746 { 747 struct amdgpu_dm_connector *aconnector; 748 struct drm_connector *connector; 749 int ret = 0; 750 751 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 752 753 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 754 aconnector = to_amdgpu_dm_connector(connector); 755 if (aconnector->dc_link->type == dc_connection_mst_branch && 756 aconnector->mst_mgr.aux) { 757 DRM_DEBUG_DRIVER("DM_MST: starting TM on aconnector: %p [id: %d]\n", 758 aconnector, aconnector->base.base.id); 759 760 ret = drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true); 761 if (ret < 0) { 762 DRM_ERROR("DM_MST: Failed to start MST\n"); 763 ((struct dc_link *)aconnector->dc_link)->type = dc_connection_single; 764 return ret; 765 } 766 } 767 } 768 769 drm_modeset_unlock(&dev->mode_config.connection_mutex); 770 return ret; 771 } 772 773 static int dm_late_init(void *handle) 774 { 775 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 776 777 struct dmcu_iram_parameters params; 778 unsigned int linear_lut[16]; 779 int i; 780 struct dmcu *dmcu = adev->dm.dc->res_pool->dmcu; 781 bool ret; 782 783 for (i = 0; i < 16; i++) 784 linear_lut[i] = 0xFFFF * i / 15; 785 786 params.set = 0; 787 params.backlight_ramping_start = 0xCCCC; 788 params.backlight_ramping_reduction = 0xCCCCCCCC; 789 params.backlight_lut_array_size = 16; 790 params.backlight_lut_array = linear_lut; 791 792 ret = dmcu_load_iram(dmcu, params); 793 794 if (!ret) 795 return -EINVAL; 796 797 return detect_mst_link_for_all_connectors(adev->ddev); 798 } 799 800 static void s3_handle_mst(struct drm_device *dev, bool suspend) 801 { 802 struct amdgpu_dm_connector *aconnector; 803 struct drm_connector *connector; 804 struct drm_dp_mst_topology_mgr *mgr; 805 int ret; 806 bool need_hotplug = false; 807 808 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 809 810 list_for_each_entry(connector, &dev->mode_config.connector_list, 811 head) { 812 aconnector = to_amdgpu_dm_connector(connector); 813 if (aconnector->dc_link->type != dc_connection_mst_branch || 814 aconnector->mst_port) 815 continue; 816 817 mgr = &aconnector->mst_mgr; 818 819 if (suspend) { 820 drm_dp_mst_topology_mgr_suspend(mgr); 821 } else { 822 ret = drm_dp_mst_topology_mgr_resume(mgr); 823 if (ret < 0) { 824 drm_dp_mst_topology_mgr_set_mst(mgr, false); 825 need_hotplug = true; 826 } 827 } 828 } 829 830 drm_modeset_unlock(&dev->mode_config.connection_mutex); 831 832 if (need_hotplug) 833 drm_kms_helper_hotplug_event(dev); 834 } 835 836 /** 837 * dm_hw_init() - Initialize DC device 838 * @handle: The base driver device containing the amdpgu_dm device. 839 * 840 * Initialize the &struct amdgpu_display_manager device. This involves calling 841 * the initializers of each DM component, then populating the struct with them. 842 * 843 * Although the function implies hardware initialization, both hardware and 844 * software are initialized here. Splitting them out to their relevant init 845 * hooks is a future TODO item. 846 * 847 * Some notable things that are initialized here: 848 * 849 * - Display Core, both software and hardware 850 * - DC modules that we need (freesync and color management) 851 * - DRM software states 852 * - Interrupt sources and handlers 853 * - Vblank support 854 * - Debug FS entries, if enabled 855 */ 856 static int dm_hw_init(void *handle) 857 { 858 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 859 /* Create DAL display manager */ 860 amdgpu_dm_init(adev); 861 amdgpu_dm_hpd_init(adev); 862 863 return 0; 864 } 865 866 /** 867 * dm_hw_fini() - Teardown DC device 868 * @handle: The base driver device containing the amdpgu_dm device. 869 * 870 * Teardown components within &struct amdgpu_display_manager that require 871 * cleanup. This involves cleaning up the DRM device, DC, and any modules that 872 * were loaded. Also flush IRQ workqueues and disable them. 873 */ 874 static int dm_hw_fini(void *handle) 875 { 876 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 877 878 amdgpu_dm_hpd_fini(adev); 879 880 amdgpu_dm_irq_fini(adev); 881 amdgpu_dm_fini(adev); 882 return 0; 883 } 884 885 static int dm_suspend(void *handle) 886 { 887 struct amdgpu_device *adev = handle; 888 struct amdgpu_display_manager *dm = &adev->dm; 889 int ret = 0; 890 891 WARN_ON(adev->dm.cached_state); 892 adev->dm.cached_state = drm_atomic_helper_suspend(adev->ddev); 893 894 s3_handle_mst(adev->ddev, true); 895 896 amdgpu_dm_irq_suspend(adev); 897 898 899 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D3); 900 901 return ret; 902 } 903 904 static struct amdgpu_dm_connector * 905 amdgpu_dm_find_first_crtc_matching_connector(struct drm_atomic_state *state, 906 struct drm_crtc *crtc) 907 { 908 uint32_t i; 909 struct drm_connector_state *new_con_state; 910 struct drm_connector *connector; 911 struct drm_crtc *crtc_from_state; 912 913 for_each_new_connector_in_state(state, connector, new_con_state, i) { 914 crtc_from_state = new_con_state->crtc; 915 916 if (crtc_from_state == crtc) 917 return to_amdgpu_dm_connector(connector); 918 } 919 920 return NULL; 921 } 922 923 static void emulated_link_detect(struct dc_link *link) 924 { 925 struct dc_sink_init_data sink_init_data = { 0 }; 926 struct display_sink_capability sink_caps = { 0 }; 927 enum dc_edid_status edid_status; 928 struct dc_context *dc_ctx = link->ctx; 929 struct dc_sink *sink = NULL; 930 struct dc_sink *prev_sink = NULL; 931 932 link->type = dc_connection_none; 933 prev_sink = link->local_sink; 934 935 if (prev_sink != NULL) 936 dc_sink_retain(prev_sink); 937 938 switch (link->connector_signal) { 939 case SIGNAL_TYPE_HDMI_TYPE_A: { 940 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 941 sink_caps.signal = SIGNAL_TYPE_HDMI_TYPE_A; 942 break; 943 } 944 945 case SIGNAL_TYPE_DVI_SINGLE_LINK: { 946 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 947 sink_caps.signal = SIGNAL_TYPE_DVI_SINGLE_LINK; 948 break; 949 } 950 951 case SIGNAL_TYPE_DVI_DUAL_LINK: { 952 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 953 sink_caps.signal = SIGNAL_TYPE_DVI_DUAL_LINK; 954 break; 955 } 956 957 case SIGNAL_TYPE_LVDS: { 958 sink_caps.transaction_type = DDC_TRANSACTION_TYPE_I2C; 959 sink_caps.signal = SIGNAL_TYPE_LVDS; 960 break; 961 } 962 963 case SIGNAL_TYPE_EDP: { 964 sink_caps.transaction_type = 965 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 966 sink_caps.signal = SIGNAL_TYPE_EDP; 967 break; 968 } 969 970 case SIGNAL_TYPE_DISPLAY_PORT: { 971 sink_caps.transaction_type = 972 DDC_TRANSACTION_TYPE_I2C_OVER_AUX; 973 sink_caps.signal = SIGNAL_TYPE_VIRTUAL; 974 break; 975 } 976 977 default: 978 DC_ERROR("Invalid connector type! signal:%d\n", 979 link->connector_signal); 980 return; 981 } 982 983 sink_init_data.link = link; 984 sink_init_data.sink_signal = sink_caps.signal; 985 986 sink = dc_sink_create(&sink_init_data); 987 if (!sink) { 988 DC_ERROR("Failed to create sink!\n"); 989 return; 990 } 991 992 /* dc_sink_create returns a new reference */ 993 link->local_sink = sink; 994 995 edid_status = dm_helpers_read_local_edid( 996 link->ctx, 997 link, 998 sink); 999 1000 if (edid_status != EDID_OK) 1001 DC_ERROR("Failed to read EDID"); 1002 1003 } 1004 1005 static int dm_resume(void *handle) 1006 { 1007 struct amdgpu_device *adev = handle; 1008 struct drm_device *ddev = adev->ddev; 1009 struct amdgpu_display_manager *dm = &adev->dm; 1010 struct amdgpu_dm_connector *aconnector; 1011 struct drm_connector *connector; 1012 struct drm_crtc *crtc; 1013 struct drm_crtc_state *new_crtc_state; 1014 struct dm_crtc_state *dm_new_crtc_state; 1015 struct drm_plane *plane; 1016 struct drm_plane_state *new_plane_state; 1017 struct dm_plane_state *dm_new_plane_state; 1018 struct dm_atomic_state *dm_state = to_dm_atomic_state(dm->atomic_obj.state); 1019 enum dc_connection_type new_connection_type = dc_connection_none; 1020 int i; 1021 1022 /* Recreate dc_state - DC invalidates it when setting power state to S3. */ 1023 dc_release_state(dm_state->context); 1024 dm_state->context = dc_create_state(dm->dc); 1025 /* TODO: Remove dc_state->dccg, use dc->dccg directly. */ 1026 dc_resource_state_construct(dm->dc, dm_state->context); 1027 1028 /* power on hardware */ 1029 dc_set_power_state(dm->dc, DC_ACPI_CM_POWER_STATE_D0); 1030 1031 /* program HPD filter */ 1032 dc_resume(dm->dc); 1033 1034 /* On resume we need to rewrite the MSTM control bits to enamble MST*/ 1035 s3_handle_mst(ddev, false); 1036 1037 /* 1038 * early enable HPD Rx IRQ, should be done before set mode as short 1039 * pulse interrupts are used for MST 1040 */ 1041 amdgpu_dm_irq_resume_early(adev); 1042 1043 /* Do detection*/ 1044 list_for_each_entry(connector, &ddev->mode_config.connector_list, head) { 1045 aconnector = to_amdgpu_dm_connector(connector); 1046 1047 /* 1048 * this is the case when traversing through already created 1049 * MST connectors, should be skipped 1050 */ 1051 if (aconnector->mst_port) 1052 continue; 1053 1054 mutex_lock(&aconnector->hpd_lock); 1055 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type)) 1056 DRM_ERROR("KMS: Failed to detect connector\n"); 1057 1058 if (aconnector->base.force && new_connection_type == dc_connection_none) 1059 emulated_link_detect(aconnector->dc_link); 1060 else 1061 dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD); 1062 1063 if (aconnector->fake_enable && aconnector->dc_link->local_sink) 1064 aconnector->fake_enable = false; 1065 1066 if (aconnector->dc_sink) 1067 dc_sink_release(aconnector->dc_sink); 1068 aconnector->dc_sink = NULL; 1069 amdgpu_dm_update_connector_after_detect(aconnector); 1070 mutex_unlock(&aconnector->hpd_lock); 1071 } 1072 1073 /* Force mode set in atomic commit */ 1074 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) 1075 new_crtc_state->active_changed = true; 1076 1077 /* 1078 * atomic_check is expected to create the dc states. We need to release 1079 * them here, since they were duplicated as part of the suspend 1080 * procedure. 1081 */ 1082 for_each_new_crtc_in_state(dm->cached_state, crtc, new_crtc_state, i) { 1083 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 1084 if (dm_new_crtc_state->stream) { 1085 WARN_ON(kref_read(&dm_new_crtc_state->stream->refcount) > 1); 1086 dc_stream_release(dm_new_crtc_state->stream); 1087 dm_new_crtc_state->stream = NULL; 1088 } 1089 } 1090 1091 for_each_new_plane_in_state(dm->cached_state, plane, new_plane_state, i) { 1092 dm_new_plane_state = to_dm_plane_state(new_plane_state); 1093 if (dm_new_plane_state->dc_state) { 1094 WARN_ON(kref_read(&dm_new_plane_state->dc_state->refcount) > 1); 1095 dc_plane_state_release(dm_new_plane_state->dc_state); 1096 dm_new_plane_state->dc_state = NULL; 1097 } 1098 } 1099 1100 drm_atomic_helper_resume(ddev, dm->cached_state); 1101 1102 dm->cached_state = NULL; 1103 1104 amdgpu_dm_irq_resume_late(adev); 1105 1106 return 0; 1107 } 1108 1109 /** 1110 * DOC: DM Lifecycle 1111 * 1112 * DM (and consequently DC) is registered in the amdgpu base driver as a IP 1113 * block. When CONFIG_DRM_AMD_DC is enabled, the DM device IP block is added to 1114 * the base driver's device list to be initialized and torn down accordingly. 1115 * 1116 * The functions to do so are provided as hooks in &struct amd_ip_funcs. 1117 */ 1118 1119 static const struct amd_ip_funcs amdgpu_dm_funcs = { 1120 .name = "dm", 1121 .early_init = dm_early_init, 1122 .late_init = dm_late_init, 1123 .sw_init = dm_sw_init, 1124 .sw_fini = dm_sw_fini, 1125 .hw_init = dm_hw_init, 1126 .hw_fini = dm_hw_fini, 1127 .suspend = dm_suspend, 1128 .resume = dm_resume, 1129 .is_idle = dm_is_idle, 1130 .wait_for_idle = dm_wait_for_idle, 1131 .check_soft_reset = dm_check_soft_reset, 1132 .soft_reset = dm_soft_reset, 1133 .set_clockgating_state = dm_set_clockgating_state, 1134 .set_powergating_state = dm_set_powergating_state, 1135 }; 1136 1137 const struct amdgpu_ip_block_version dm_ip_block = 1138 { 1139 .type = AMD_IP_BLOCK_TYPE_DCE, 1140 .major = 1, 1141 .minor = 0, 1142 .rev = 0, 1143 .funcs = &amdgpu_dm_funcs, 1144 }; 1145 1146 1147 /** 1148 * DOC: atomic 1149 * 1150 * *WIP* 1151 */ 1152 1153 static const struct drm_mode_config_funcs amdgpu_dm_mode_funcs = { 1154 .fb_create = amdgpu_display_user_framebuffer_create, 1155 .output_poll_changed = drm_fb_helper_output_poll_changed, 1156 .atomic_check = amdgpu_dm_atomic_check, 1157 .atomic_commit = amdgpu_dm_atomic_commit, 1158 }; 1159 1160 static struct drm_mode_config_helper_funcs amdgpu_dm_mode_config_helperfuncs = { 1161 .atomic_commit_tail = amdgpu_dm_atomic_commit_tail 1162 }; 1163 1164 static void 1165 amdgpu_dm_update_connector_after_detect(struct amdgpu_dm_connector *aconnector) 1166 { 1167 struct drm_connector *connector = &aconnector->base; 1168 struct drm_device *dev = connector->dev; 1169 struct dc_sink *sink; 1170 1171 /* MST handled by drm_mst framework */ 1172 if (aconnector->mst_mgr.mst_state == true) 1173 return; 1174 1175 1176 sink = aconnector->dc_link->local_sink; 1177 if (sink) 1178 dc_sink_retain(sink); 1179 1180 /* 1181 * Edid mgmt connector gets first update only in mode_valid hook and then 1182 * the connector sink is set to either fake or physical sink depends on link status. 1183 * Skip if already done during boot. 1184 */ 1185 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED 1186 && aconnector->dc_em_sink) { 1187 1188 /* 1189 * For S3 resume with headless use eml_sink to fake stream 1190 * because on resume connector->sink is set to NULL 1191 */ 1192 mutex_lock(&dev->mode_config.mutex); 1193 1194 if (sink) { 1195 if (aconnector->dc_sink) { 1196 amdgpu_dm_update_freesync_caps(connector, NULL); 1197 /* 1198 * retain and release below are used to 1199 * bump up refcount for sink because the link doesn't point 1200 * to it anymore after disconnect, so on next crtc to connector 1201 * reshuffle by UMD we will get into unwanted dc_sink release 1202 */ 1203 dc_sink_release(aconnector->dc_sink); 1204 } 1205 aconnector->dc_sink = sink; 1206 dc_sink_retain(aconnector->dc_sink); 1207 amdgpu_dm_update_freesync_caps(connector, 1208 aconnector->edid); 1209 } else { 1210 amdgpu_dm_update_freesync_caps(connector, NULL); 1211 if (!aconnector->dc_sink) { 1212 aconnector->dc_sink = aconnector->dc_em_sink; 1213 dc_sink_retain(aconnector->dc_sink); 1214 } 1215 } 1216 1217 mutex_unlock(&dev->mode_config.mutex); 1218 1219 if (sink) 1220 dc_sink_release(sink); 1221 return; 1222 } 1223 1224 /* 1225 * TODO: temporary guard to look for proper fix 1226 * if this sink is MST sink, we should not do anything 1227 */ 1228 if (sink && sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT_MST) { 1229 dc_sink_release(sink); 1230 return; 1231 } 1232 1233 if (aconnector->dc_sink == sink) { 1234 /* 1235 * We got a DP short pulse (Link Loss, DP CTS, etc...). 1236 * Do nothing!! 1237 */ 1238 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: dc_sink didn't change.\n", 1239 aconnector->connector_id); 1240 if (sink) 1241 dc_sink_release(sink); 1242 return; 1243 } 1244 1245 DRM_DEBUG_DRIVER("DCHPD: connector_id=%d: Old sink=%p New sink=%p\n", 1246 aconnector->connector_id, aconnector->dc_sink, sink); 1247 1248 mutex_lock(&dev->mode_config.mutex); 1249 1250 /* 1251 * 1. Update status of the drm connector 1252 * 2. Send an event and let userspace tell us what to do 1253 */ 1254 if (sink) { 1255 /* 1256 * TODO: check if we still need the S3 mode update workaround. 1257 * If yes, put it here. 1258 */ 1259 if (aconnector->dc_sink) 1260 amdgpu_dm_update_freesync_caps(connector, NULL); 1261 1262 aconnector->dc_sink = sink; 1263 dc_sink_retain(aconnector->dc_sink); 1264 if (sink->dc_edid.length == 0) { 1265 aconnector->edid = NULL; 1266 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux); 1267 } else { 1268 aconnector->edid = 1269 (struct edid *) sink->dc_edid.raw_edid; 1270 1271 1272 drm_connector_update_edid_property(connector, 1273 aconnector->edid); 1274 drm_dp_cec_set_edid(&aconnector->dm_dp_aux.aux, 1275 aconnector->edid); 1276 } 1277 amdgpu_dm_update_freesync_caps(connector, aconnector->edid); 1278 1279 } else { 1280 drm_dp_cec_unset_edid(&aconnector->dm_dp_aux.aux); 1281 amdgpu_dm_update_freesync_caps(connector, NULL); 1282 drm_connector_update_edid_property(connector, NULL); 1283 aconnector->num_modes = 0; 1284 dc_sink_release(aconnector->dc_sink); 1285 aconnector->dc_sink = NULL; 1286 aconnector->edid = NULL; 1287 } 1288 1289 mutex_unlock(&dev->mode_config.mutex); 1290 1291 if (sink) 1292 dc_sink_release(sink); 1293 } 1294 1295 static void handle_hpd_irq(void *param) 1296 { 1297 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param; 1298 struct drm_connector *connector = &aconnector->base; 1299 struct drm_device *dev = connector->dev; 1300 enum dc_connection_type new_connection_type = dc_connection_none; 1301 1302 /* 1303 * In case of failure or MST no need to update connector status or notify the OS 1304 * since (for MST case) MST does this in its own context. 1305 */ 1306 mutex_lock(&aconnector->hpd_lock); 1307 1308 if (aconnector->fake_enable) 1309 aconnector->fake_enable = false; 1310 1311 if (!dc_link_detect_sink(aconnector->dc_link, &new_connection_type)) 1312 DRM_ERROR("KMS: Failed to detect connector\n"); 1313 1314 if (aconnector->base.force && new_connection_type == dc_connection_none) { 1315 emulated_link_detect(aconnector->dc_link); 1316 1317 1318 drm_modeset_lock_all(dev); 1319 dm_restore_drm_connector_state(dev, connector); 1320 drm_modeset_unlock_all(dev); 1321 1322 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED) 1323 drm_kms_helper_hotplug_event(dev); 1324 1325 } else if (dc_link_detect(aconnector->dc_link, DETECT_REASON_HPD)) { 1326 amdgpu_dm_update_connector_after_detect(aconnector); 1327 1328 1329 drm_modeset_lock_all(dev); 1330 dm_restore_drm_connector_state(dev, connector); 1331 drm_modeset_unlock_all(dev); 1332 1333 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED) 1334 drm_kms_helper_hotplug_event(dev); 1335 } 1336 mutex_unlock(&aconnector->hpd_lock); 1337 1338 } 1339 1340 static void dm_handle_hpd_rx_irq(struct amdgpu_dm_connector *aconnector) 1341 { 1342 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 }; 1343 uint8_t dret; 1344 bool new_irq_handled = false; 1345 int dpcd_addr; 1346 int dpcd_bytes_to_read; 1347 1348 const int max_process_count = 30; 1349 int process_count = 0; 1350 1351 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link); 1352 1353 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) { 1354 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT; 1355 /* DPCD 0x200 - 0x201 for downstream IRQ */ 1356 dpcd_addr = DP_SINK_COUNT; 1357 } else { 1358 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI; 1359 /* DPCD 0x2002 - 0x2005 for downstream IRQ */ 1360 dpcd_addr = DP_SINK_COUNT_ESI; 1361 } 1362 1363 dret = drm_dp_dpcd_read( 1364 &aconnector->dm_dp_aux.aux, 1365 dpcd_addr, 1366 esi, 1367 dpcd_bytes_to_read); 1368 1369 while (dret == dpcd_bytes_to_read && 1370 process_count < max_process_count) { 1371 uint8_t retry; 1372 dret = 0; 1373 1374 process_count++; 1375 1376 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]); 1377 /* handle HPD short pulse irq */ 1378 if (aconnector->mst_mgr.mst_state) 1379 drm_dp_mst_hpd_irq( 1380 &aconnector->mst_mgr, 1381 esi, 1382 &new_irq_handled); 1383 1384 if (new_irq_handled) { 1385 /* ACK at DPCD to notify down stream */ 1386 const int ack_dpcd_bytes_to_write = 1387 dpcd_bytes_to_read - 1; 1388 1389 for (retry = 0; retry < 3; retry++) { 1390 uint8_t wret; 1391 1392 wret = drm_dp_dpcd_write( 1393 &aconnector->dm_dp_aux.aux, 1394 dpcd_addr + 1, 1395 &esi[1], 1396 ack_dpcd_bytes_to_write); 1397 if (wret == ack_dpcd_bytes_to_write) 1398 break; 1399 } 1400 1401 /* check if there is new irq to be handled */ 1402 dret = drm_dp_dpcd_read( 1403 &aconnector->dm_dp_aux.aux, 1404 dpcd_addr, 1405 esi, 1406 dpcd_bytes_to_read); 1407 1408 new_irq_handled = false; 1409 } else { 1410 break; 1411 } 1412 } 1413 1414 if (process_count == max_process_count) 1415 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n"); 1416 } 1417 1418 static void handle_hpd_rx_irq(void *param) 1419 { 1420 struct amdgpu_dm_connector *aconnector = (struct amdgpu_dm_connector *)param; 1421 struct drm_connector *connector = &aconnector->base; 1422 struct drm_device *dev = connector->dev; 1423 struct dc_link *dc_link = aconnector->dc_link; 1424 bool is_mst_root_connector = aconnector->mst_mgr.mst_state; 1425 enum dc_connection_type new_connection_type = dc_connection_none; 1426 1427 /* 1428 * TODO:Temporary add mutex to protect hpd interrupt not have a gpio 1429 * conflict, after implement i2c helper, this mutex should be 1430 * retired. 1431 */ 1432 if (dc_link->type != dc_connection_mst_branch) 1433 mutex_lock(&aconnector->hpd_lock); 1434 1435 if (dc_link_handle_hpd_rx_irq(dc_link, NULL, NULL) && 1436 !is_mst_root_connector) { 1437 /* Downstream Port status changed. */ 1438 if (!dc_link_detect_sink(dc_link, &new_connection_type)) 1439 DRM_ERROR("KMS: Failed to detect connector\n"); 1440 1441 if (aconnector->base.force && new_connection_type == dc_connection_none) { 1442 emulated_link_detect(dc_link); 1443 1444 if (aconnector->fake_enable) 1445 aconnector->fake_enable = false; 1446 1447 amdgpu_dm_update_connector_after_detect(aconnector); 1448 1449 1450 drm_modeset_lock_all(dev); 1451 dm_restore_drm_connector_state(dev, connector); 1452 drm_modeset_unlock_all(dev); 1453 1454 drm_kms_helper_hotplug_event(dev); 1455 } else if (dc_link_detect(dc_link, DETECT_REASON_HPDRX)) { 1456 1457 if (aconnector->fake_enable) 1458 aconnector->fake_enable = false; 1459 1460 amdgpu_dm_update_connector_after_detect(aconnector); 1461 1462 1463 drm_modeset_lock_all(dev); 1464 dm_restore_drm_connector_state(dev, connector); 1465 drm_modeset_unlock_all(dev); 1466 1467 drm_kms_helper_hotplug_event(dev); 1468 } 1469 } 1470 if ((dc_link->cur_link_settings.lane_count != LANE_COUNT_UNKNOWN) || 1471 (dc_link->type == dc_connection_mst_branch)) 1472 dm_handle_hpd_rx_irq(aconnector); 1473 1474 if (dc_link->type != dc_connection_mst_branch) { 1475 drm_dp_cec_irq(&aconnector->dm_dp_aux.aux); 1476 mutex_unlock(&aconnector->hpd_lock); 1477 } 1478 } 1479 1480 static void register_hpd_handlers(struct amdgpu_device *adev) 1481 { 1482 struct drm_device *dev = adev->ddev; 1483 struct drm_connector *connector; 1484 struct amdgpu_dm_connector *aconnector; 1485 const struct dc_link *dc_link; 1486 struct dc_interrupt_params int_params = {0}; 1487 1488 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 1489 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 1490 1491 list_for_each_entry(connector, 1492 &dev->mode_config.connector_list, head) { 1493 1494 aconnector = to_amdgpu_dm_connector(connector); 1495 dc_link = aconnector->dc_link; 1496 1497 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd) { 1498 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT; 1499 int_params.irq_source = dc_link->irq_source_hpd; 1500 1501 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1502 handle_hpd_irq, 1503 (void *) aconnector); 1504 } 1505 1506 if (DC_IRQ_SOURCE_INVALID != dc_link->irq_source_hpd_rx) { 1507 1508 /* Also register for DP short pulse (hpd_rx). */ 1509 int_params.int_context = INTERRUPT_LOW_IRQ_CONTEXT; 1510 int_params.irq_source = dc_link->irq_source_hpd_rx; 1511 1512 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1513 handle_hpd_rx_irq, 1514 (void *) aconnector); 1515 } 1516 } 1517 } 1518 1519 /* Register IRQ sources and initialize IRQ callbacks */ 1520 static int dce110_register_irq_handlers(struct amdgpu_device *adev) 1521 { 1522 struct dc *dc = adev->dm.dc; 1523 struct common_irq_params *c_irq_params; 1524 struct dc_interrupt_params int_params = {0}; 1525 int r; 1526 int i; 1527 unsigned client_id = AMDGPU_IRQ_CLIENTID_LEGACY; 1528 1529 if (adev->asic_type == CHIP_VEGA10 || 1530 adev->asic_type == CHIP_VEGA12 || 1531 adev->asic_type == CHIP_VEGA20 || 1532 adev->asic_type == CHIP_RAVEN) 1533 client_id = SOC15_IH_CLIENTID_DCE; 1534 1535 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 1536 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 1537 1538 /* 1539 * Actions of amdgpu_irq_add_id(): 1540 * 1. Register a set() function with base driver. 1541 * Base driver will call set() function to enable/disable an 1542 * interrupt in DC hardware. 1543 * 2. Register amdgpu_dm_irq_handler(). 1544 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts 1545 * coming from DC hardware. 1546 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC 1547 * for acknowledging and handling. */ 1548 1549 /* Use VBLANK interrupt */ 1550 for (i = VISLANDS30_IV_SRCID_D1_VERTICAL_INTERRUPT0; i <= VISLANDS30_IV_SRCID_D6_VERTICAL_INTERRUPT0; i++) { 1551 r = amdgpu_irq_add_id(adev, client_id, i, &adev->crtc_irq); 1552 if (r) { 1553 DRM_ERROR("Failed to add crtc irq id!\n"); 1554 return r; 1555 } 1556 1557 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1558 int_params.irq_source = 1559 dc_interrupt_to_irq_source(dc, i, 0); 1560 1561 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1]; 1562 1563 c_irq_params->adev = adev; 1564 c_irq_params->irq_src = int_params.irq_source; 1565 1566 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1567 dm_crtc_high_irq, c_irq_params); 1568 } 1569 1570 /* Use VUPDATE interrupt */ 1571 for (i = VISLANDS30_IV_SRCID_D1_V_UPDATE_INT; i <= VISLANDS30_IV_SRCID_D6_V_UPDATE_INT; i += 2) { 1572 r = amdgpu_irq_add_id(adev, client_id, i, &adev->vupdate_irq); 1573 if (r) { 1574 DRM_ERROR("Failed to add vupdate irq id!\n"); 1575 return r; 1576 } 1577 1578 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1579 int_params.irq_source = 1580 dc_interrupt_to_irq_source(dc, i, 0); 1581 1582 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1]; 1583 1584 c_irq_params->adev = adev; 1585 c_irq_params->irq_src = int_params.irq_source; 1586 1587 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1588 dm_vupdate_high_irq, c_irq_params); 1589 } 1590 1591 /* Use GRPH_PFLIP interrupt */ 1592 for (i = VISLANDS30_IV_SRCID_D1_GRPH_PFLIP; 1593 i <= VISLANDS30_IV_SRCID_D6_GRPH_PFLIP; i += 2) { 1594 r = amdgpu_irq_add_id(adev, client_id, i, &adev->pageflip_irq); 1595 if (r) { 1596 DRM_ERROR("Failed to add page flip irq id!\n"); 1597 return r; 1598 } 1599 1600 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1601 int_params.irq_source = 1602 dc_interrupt_to_irq_source(dc, i, 0); 1603 1604 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST]; 1605 1606 c_irq_params->adev = adev; 1607 c_irq_params->irq_src = int_params.irq_source; 1608 1609 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1610 dm_pflip_high_irq, c_irq_params); 1611 1612 } 1613 1614 /* HPD */ 1615 r = amdgpu_irq_add_id(adev, client_id, 1616 VISLANDS30_IV_SRCID_HOTPLUG_DETECT_A, &adev->hpd_irq); 1617 if (r) { 1618 DRM_ERROR("Failed to add hpd irq id!\n"); 1619 return r; 1620 } 1621 1622 register_hpd_handlers(adev); 1623 1624 return 0; 1625 } 1626 1627 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 1628 /* Register IRQ sources and initialize IRQ callbacks */ 1629 static int dcn10_register_irq_handlers(struct amdgpu_device *adev) 1630 { 1631 struct dc *dc = adev->dm.dc; 1632 struct common_irq_params *c_irq_params; 1633 struct dc_interrupt_params int_params = {0}; 1634 int r; 1635 int i; 1636 1637 int_params.requested_polarity = INTERRUPT_POLARITY_DEFAULT; 1638 int_params.current_polarity = INTERRUPT_POLARITY_DEFAULT; 1639 1640 /* 1641 * Actions of amdgpu_irq_add_id(): 1642 * 1. Register a set() function with base driver. 1643 * Base driver will call set() function to enable/disable an 1644 * interrupt in DC hardware. 1645 * 2. Register amdgpu_dm_irq_handler(). 1646 * Base driver will call amdgpu_dm_irq_handler() for ALL interrupts 1647 * coming from DC hardware. 1648 * amdgpu_dm_irq_handler() will re-direct the interrupt to DC 1649 * for acknowledging and handling. 1650 */ 1651 1652 /* Use VSTARTUP interrupt */ 1653 for (i = DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP; 1654 i <= DCN_1_0__SRCID__DC_D1_OTG_VSTARTUP + adev->mode_info.num_crtc - 1; 1655 i++) { 1656 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->crtc_irq); 1657 1658 if (r) { 1659 DRM_ERROR("Failed to add crtc irq id!\n"); 1660 return r; 1661 } 1662 1663 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1664 int_params.irq_source = 1665 dc_interrupt_to_irq_source(dc, i, 0); 1666 1667 c_irq_params = &adev->dm.vblank_params[int_params.irq_source - DC_IRQ_SOURCE_VBLANK1]; 1668 1669 c_irq_params->adev = adev; 1670 c_irq_params->irq_src = int_params.irq_source; 1671 1672 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1673 dm_crtc_high_irq, c_irq_params); 1674 } 1675 1676 /* Use VUPDATE_NO_LOCK interrupt on DCN, which seems to correspond to 1677 * the regular VUPDATE interrupt on DCE. We want DC_IRQ_SOURCE_VUPDATEx 1678 * to trigger at end of each vblank, regardless of state of the lock, 1679 * matching DCE behaviour. 1680 */ 1681 for (i = DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT; 1682 i <= DCN_1_0__SRCID__OTG0_IHC_V_UPDATE_NO_LOCK_INTERRUPT + adev->mode_info.num_crtc - 1; 1683 i++) { 1684 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->vupdate_irq); 1685 1686 if (r) { 1687 DRM_ERROR("Failed to add vupdate irq id!\n"); 1688 return r; 1689 } 1690 1691 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1692 int_params.irq_source = 1693 dc_interrupt_to_irq_source(dc, i, 0); 1694 1695 c_irq_params = &adev->dm.vupdate_params[int_params.irq_source - DC_IRQ_SOURCE_VUPDATE1]; 1696 1697 c_irq_params->adev = adev; 1698 c_irq_params->irq_src = int_params.irq_source; 1699 1700 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1701 dm_vupdate_high_irq, c_irq_params); 1702 } 1703 1704 /* Use GRPH_PFLIP interrupt */ 1705 for (i = DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT; 1706 i <= DCN_1_0__SRCID__HUBP0_FLIP_INTERRUPT + adev->mode_info.num_crtc - 1; 1707 i++) { 1708 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, i, &adev->pageflip_irq); 1709 if (r) { 1710 DRM_ERROR("Failed to add page flip irq id!\n"); 1711 return r; 1712 } 1713 1714 int_params.int_context = INTERRUPT_HIGH_IRQ_CONTEXT; 1715 int_params.irq_source = 1716 dc_interrupt_to_irq_source(dc, i, 0); 1717 1718 c_irq_params = &adev->dm.pflip_params[int_params.irq_source - DC_IRQ_SOURCE_PFLIP_FIRST]; 1719 1720 c_irq_params->adev = adev; 1721 c_irq_params->irq_src = int_params.irq_source; 1722 1723 amdgpu_dm_irq_register_interrupt(adev, &int_params, 1724 dm_pflip_high_irq, c_irq_params); 1725 1726 } 1727 1728 /* HPD */ 1729 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DCE, DCN_1_0__SRCID__DC_HPD1_INT, 1730 &adev->hpd_irq); 1731 if (r) { 1732 DRM_ERROR("Failed to add hpd irq id!\n"); 1733 return r; 1734 } 1735 1736 register_hpd_handlers(adev); 1737 1738 return 0; 1739 } 1740 #endif 1741 1742 /* 1743 * Acquires the lock for the atomic state object and returns 1744 * the new atomic state. 1745 * 1746 * This should only be called during atomic check. 1747 */ 1748 static int dm_atomic_get_state(struct drm_atomic_state *state, 1749 struct dm_atomic_state **dm_state) 1750 { 1751 struct drm_device *dev = state->dev; 1752 struct amdgpu_device *adev = dev->dev_private; 1753 struct amdgpu_display_manager *dm = &adev->dm; 1754 struct drm_private_state *priv_state; 1755 1756 if (*dm_state) 1757 return 0; 1758 1759 priv_state = drm_atomic_get_private_obj_state(state, &dm->atomic_obj); 1760 if (IS_ERR(priv_state)) 1761 return PTR_ERR(priv_state); 1762 1763 *dm_state = to_dm_atomic_state(priv_state); 1764 1765 return 0; 1766 } 1767 1768 struct dm_atomic_state * 1769 dm_atomic_get_new_state(struct drm_atomic_state *state) 1770 { 1771 struct drm_device *dev = state->dev; 1772 struct amdgpu_device *adev = dev->dev_private; 1773 struct amdgpu_display_manager *dm = &adev->dm; 1774 struct drm_private_obj *obj; 1775 struct drm_private_state *new_obj_state; 1776 int i; 1777 1778 for_each_new_private_obj_in_state(state, obj, new_obj_state, i) { 1779 if (obj->funcs == dm->atomic_obj.funcs) 1780 return to_dm_atomic_state(new_obj_state); 1781 } 1782 1783 return NULL; 1784 } 1785 1786 struct dm_atomic_state * 1787 dm_atomic_get_old_state(struct drm_atomic_state *state) 1788 { 1789 struct drm_device *dev = state->dev; 1790 struct amdgpu_device *adev = dev->dev_private; 1791 struct amdgpu_display_manager *dm = &adev->dm; 1792 struct drm_private_obj *obj; 1793 struct drm_private_state *old_obj_state; 1794 int i; 1795 1796 for_each_old_private_obj_in_state(state, obj, old_obj_state, i) { 1797 if (obj->funcs == dm->atomic_obj.funcs) 1798 return to_dm_atomic_state(old_obj_state); 1799 } 1800 1801 return NULL; 1802 } 1803 1804 static struct drm_private_state * 1805 dm_atomic_duplicate_state(struct drm_private_obj *obj) 1806 { 1807 struct dm_atomic_state *old_state, *new_state; 1808 1809 new_state = kzalloc(sizeof(*new_state), GFP_KERNEL); 1810 if (!new_state) 1811 return NULL; 1812 1813 __drm_atomic_helper_private_obj_duplicate_state(obj, &new_state->base); 1814 1815 old_state = to_dm_atomic_state(obj->state); 1816 1817 if (old_state && old_state->context) 1818 new_state->context = dc_copy_state(old_state->context); 1819 1820 if (!new_state->context) { 1821 kfree(new_state); 1822 return NULL; 1823 } 1824 1825 return &new_state->base; 1826 } 1827 1828 static void dm_atomic_destroy_state(struct drm_private_obj *obj, 1829 struct drm_private_state *state) 1830 { 1831 struct dm_atomic_state *dm_state = to_dm_atomic_state(state); 1832 1833 if (dm_state && dm_state->context) 1834 dc_release_state(dm_state->context); 1835 1836 kfree(dm_state); 1837 } 1838 1839 static struct drm_private_state_funcs dm_atomic_state_funcs = { 1840 .atomic_duplicate_state = dm_atomic_duplicate_state, 1841 .atomic_destroy_state = dm_atomic_destroy_state, 1842 }; 1843 1844 static int amdgpu_dm_mode_config_init(struct amdgpu_device *adev) 1845 { 1846 struct dm_atomic_state *state; 1847 int r; 1848 1849 adev->mode_info.mode_config_initialized = true; 1850 1851 adev->ddev->mode_config.funcs = (void *)&amdgpu_dm_mode_funcs; 1852 adev->ddev->mode_config.helper_private = &amdgpu_dm_mode_config_helperfuncs; 1853 1854 adev->ddev->mode_config.max_width = 16384; 1855 adev->ddev->mode_config.max_height = 16384; 1856 1857 adev->ddev->mode_config.preferred_depth = 24; 1858 adev->ddev->mode_config.prefer_shadow = 1; 1859 /* indicates support for immediate flip */ 1860 adev->ddev->mode_config.async_page_flip = true; 1861 1862 adev->ddev->mode_config.fb_base = adev->gmc.aper_base; 1863 1864 state = kzalloc(sizeof(*state), GFP_KERNEL); 1865 if (!state) 1866 return -ENOMEM; 1867 1868 state->context = dc_create_state(adev->dm.dc); 1869 if (!state->context) { 1870 kfree(state); 1871 return -ENOMEM; 1872 } 1873 1874 dc_resource_state_copy_construct_current(adev->dm.dc, state->context); 1875 1876 drm_atomic_private_obj_init(adev->ddev, 1877 &adev->dm.atomic_obj, 1878 &state->base, 1879 &dm_atomic_state_funcs); 1880 1881 r = amdgpu_display_modeset_create_props(adev); 1882 if (r) 1883 return r; 1884 1885 return 0; 1886 } 1887 1888 #define AMDGPU_DM_DEFAULT_MIN_BACKLIGHT 12 1889 #define AMDGPU_DM_DEFAULT_MAX_BACKLIGHT 255 1890 1891 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\ 1892 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 1893 1894 static void amdgpu_dm_update_backlight_caps(struct amdgpu_display_manager *dm) 1895 { 1896 #if defined(CONFIG_ACPI) 1897 struct amdgpu_dm_backlight_caps caps; 1898 1899 if (dm->backlight_caps.caps_valid) 1900 return; 1901 1902 amdgpu_acpi_get_backlight_caps(dm->adev, &caps); 1903 if (caps.caps_valid) { 1904 dm->backlight_caps.min_input_signal = caps.min_input_signal; 1905 dm->backlight_caps.max_input_signal = caps.max_input_signal; 1906 dm->backlight_caps.caps_valid = true; 1907 } else { 1908 dm->backlight_caps.min_input_signal = 1909 AMDGPU_DM_DEFAULT_MIN_BACKLIGHT; 1910 dm->backlight_caps.max_input_signal = 1911 AMDGPU_DM_DEFAULT_MAX_BACKLIGHT; 1912 } 1913 #else 1914 dm->backlight_caps.min_input_signal = AMDGPU_DM_DEFAULT_MIN_BACKLIGHT; 1915 dm->backlight_caps.max_input_signal = AMDGPU_DM_DEFAULT_MAX_BACKLIGHT; 1916 #endif 1917 } 1918 1919 static int amdgpu_dm_backlight_update_status(struct backlight_device *bd) 1920 { 1921 struct amdgpu_display_manager *dm = bl_get_data(bd); 1922 struct amdgpu_dm_backlight_caps caps; 1923 uint32_t brightness = bd->props.brightness; 1924 1925 amdgpu_dm_update_backlight_caps(dm); 1926 caps = dm->backlight_caps; 1927 /* 1928 * The brightness input is in the range 0-255 1929 * It needs to be rescaled to be between the 1930 * requested min and max input signal 1931 * 1932 * It also needs to be scaled up by 0x101 to 1933 * match the DC interface which has a range of 1934 * 0 to 0xffff 1935 */ 1936 brightness = 1937 brightness 1938 * 0x101 1939 * (caps.max_input_signal - caps.min_input_signal) 1940 / AMDGPU_MAX_BL_LEVEL 1941 + caps.min_input_signal * 0x101; 1942 1943 if (dc_link_set_backlight_level(dm->backlight_link, 1944 brightness, 0)) 1945 return 0; 1946 else 1947 return 1; 1948 } 1949 1950 static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd) 1951 { 1952 struct amdgpu_display_manager *dm = bl_get_data(bd); 1953 int ret = dc_link_get_backlight_level(dm->backlight_link); 1954 1955 if (ret == DC_ERROR_UNEXPECTED) 1956 return bd->props.brightness; 1957 return ret; 1958 } 1959 1960 static const struct backlight_ops amdgpu_dm_backlight_ops = { 1961 .get_brightness = amdgpu_dm_backlight_get_brightness, 1962 .update_status = amdgpu_dm_backlight_update_status, 1963 }; 1964 1965 static void 1966 amdgpu_dm_register_backlight_device(struct amdgpu_display_manager *dm) 1967 { 1968 char bl_name[16]; 1969 struct backlight_properties props = { 0 }; 1970 1971 amdgpu_dm_update_backlight_caps(dm); 1972 1973 props.max_brightness = AMDGPU_MAX_BL_LEVEL; 1974 props.brightness = AMDGPU_MAX_BL_LEVEL; 1975 props.type = BACKLIGHT_RAW; 1976 1977 snprintf(bl_name, sizeof(bl_name), "amdgpu_bl%d", 1978 dm->adev->ddev->primary->index); 1979 1980 dm->backlight_dev = backlight_device_register(bl_name, 1981 dm->adev->ddev->dev, 1982 dm, 1983 &amdgpu_dm_backlight_ops, 1984 &props); 1985 1986 if (IS_ERR(dm->backlight_dev)) 1987 DRM_ERROR("DM: Backlight registration failed!\n"); 1988 else 1989 DRM_DEBUG_DRIVER("DM: Registered Backlight device: %s\n", bl_name); 1990 } 1991 1992 #endif 1993 1994 static int initialize_plane(struct amdgpu_display_manager *dm, 1995 struct amdgpu_mode_info *mode_info, int plane_id, 1996 enum drm_plane_type plane_type, 1997 const struct dc_plane_cap *plane_cap) 1998 { 1999 struct drm_plane *plane; 2000 unsigned long possible_crtcs; 2001 int ret = 0; 2002 2003 plane = kzalloc(sizeof(struct drm_plane), GFP_KERNEL); 2004 if (!plane) { 2005 DRM_ERROR("KMS: Failed to allocate plane\n"); 2006 return -ENOMEM; 2007 } 2008 plane->type = plane_type; 2009 2010 /* 2011 * HACK: IGT tests expect that the primary plane for a CRTC 2012 * can only have one possible CRTC. Only expose support for 2013 * any CRTC if they're not going to be used as a primary plane 2014 * for a CRTC - like overlay or underlay planes. 2015 */ 2016 possible_crtcs = 1 << plane_id; 2017 if (plane_id >= dm->dc->caps.max_streams) 2018 possible_crtcs = 0xff; 2019 2020 ret = amdgpu_dm_plane_init(dm, plane, possible_crtcs, plane_cap); 2021 2022 if (ret) { 2023 DRM_ERROR("KMS: Failed to initialize plane\n"); 2024 kfree(plane); 2025 return ret; 2026 } 2027 2028 if (mode_info) 2029 mode_info->planes[plane_id] = plane; 2030 2031 return ret; 2032 } 2033 2034 2035 static void register_backlight_device(struct amdgpu_display_manager *dm, 2036 struct dc_link *link) 2037 { 2038 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\ 2039 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 2040 2041 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) && 2042 link->type != dc_connection_none) { 2043 /* 2044 * Event if registration failed, we should continue with 2045 * DM initialization because not having a backlight control 2046 * is better then a black screen. 2047 */ 2048 amdgpu_dm_register_backlight_device(dm); 2049 2050 if (dm->backlight_dev) 2051 dm->backlight_link = link; 2052 } 2053 #endif 2054 } 2055 2056 2057 /* 2058 * In this architecture, the association 2059 * connector -> encoder -> crtc 2060 * id not really requried. The crtc and connector will hold the 2061 * display_index as an abstraction to use with DAL component 2062 * 2063 * Returns 0 on success 2064 */ 2065 static int amdgpu_dm_initialize_drm_device(struct amdgpu_device *adev) 2066 { 2067 struct amdgpu_display_manager *dm = &adev->dm; 2068 int32_t i; 2069 struct amdgpu_dm_connector *aconnector = NULL; 2070 struct amdgpu_encoder *aencoder = NULL; 2071 struct amdgpu_mode_info *mode_info = &adev->mode_info; 2072 uint32_t link_cnt; 2073 int32_t primary_planes; 2074 enum dc_connection_type new_connection_type = dc_connection_none; 2075 const struct dc_plane_cap *plane; 2076 2077 link_cnt = dm->dc->caps.max_links; 2078 if (amdgpu_dm_mode_config_init(dm->adev)) { 2079 DRM_ERROR("DM: Failed to initialize mode config\n"); 2080 return -EINVAL; 2081 } 2082 2083 /* There is one primary plane per CRTC */ 2084 primary_planes = dm->dc->caps.max_streams; 2085 ASSERT(primary_planes <= AMDGPU_MAX_PLANES); 2086 2087 /* 2088 * Initialize primary planes, implicit planes for legacy IOCTLS. 2089 * Order is reversed to match iteration order in atomic check. 2090 */ 2091 for (i = (primary_planes - 1); i >= 0; i--) { 2092 plane = &dm->dc->caps.planes[i]; 2093 2094 if (initialize_plane(dm, mode_info, i, 2095 DRM_PLANE_TYPE_PRIMARY, plane)) { 2096 DRM_ERROR("KMS: Failed to initialize primary plane\n"); 2097 goto fail; 2098 } 2099 } 2100 2101 /* 2102 * Initialize overlay planes, index starting after primary planes. 2103 * These planes have a higher DRM index than the primary planes since 2104 * they should be considered as having a higher z-order. 2105 * Order is reversed to match iteration order in atomic check. 2106 * 2107 * Only support DCN for now, and only expose one so we don't encourage 2108 * userspace to use up all the pipes. 2109 */ 2110 for (i = 0; i < dm->dc->caps.max_planes; ++i) { 2111 struct dc_plane_cap *plane = &dm->dc->caps.planes[i]; 2112 2113 if (plane->type != DC_PLANE_TYPE_DCN_UNIVERSAL) 2114 continue; 2115 2116 if (!plane->blends_with_above || !plane->blends_with_below) 2117 continue; 2118 2119 if (!plane->pixel_format_support.argb8888) 2120 continue; 2121 2122 if (initialize_plane(dm, NULL, primary_planes + i, 2123 DRM_PLANE_TYPE_OVERLAY, plane)) { 2124 DRM_ERROR("KMS: Failed to initialize overlay plane\n"); 2125 goto fail; 2126 } 2127 2128 /* Only create one overlay plane. */ 2129 break; 2130 } 2131 2132 for (i = 0; i < dm->dc->caps.max_streams; i++) 2133 if (amdgpu_dm_crtc_init(dm, mode_info->planes[i], i)) { 2134 DRM_ERROR("KMS: Failed to initialize crtc\n"); 2135 goto fail; 2136 } 2137 2138 dm->display_indexes_num = dm->dc->caps.max_streams; 2139 2140 /* loops over all connectors on the board */ 2141 for (i = 0; i < link_cnt; i++) { 2142 struct dc_link *link = NULL; 2143 2144 if (i > AMDGPU_DM_MAX_DISPLAY_INDEX) { 2145 DRM_ERROR( 2146 "KMS: Cannot support more than %d display indexes\n", 2147 AMDGPU_DM_MAX_DISPLAY_INDEX); 2148 continue; 2149 } 2150 2151 aconnector = kzalloc(sizeof(*aconnector), GFP_KERNEL); 2152 if (!aconnector) 2153 goto fail; 2154 2155 aencoder = kzalloc(sizeof(*aencoder), GFP_KERNEL); 2156 if (!aencoder) 2157 goto fail; 2158 2159 if (amdgpu_dm_encoder_init(dm->ddev, aencoder, i)) { 2160 DRM_ERROR("KMS: Failed to initialize encoder\n"); 2161 goto fail; 2162 } 2163 2164 if (amdgpu_dm_connector_init(dm, aconnector, i, aencoder)) { 2165 DRM_ERROR("KMS: Failed to initialize connector\n"); 2166 goto fail; 2167 } 2168 2169 link = dc_get_link_at_index(dm->dc, i); 2170 2171 if (!dc_link_detect_sink(link, &new_connection_type)) 2172 DRM_ERROR("KMS: Failed to detect connector\n"); 2173 2174 if (aconnector->base.force && new_connection_type == dc_connection_none) { 2175 emulated_link_detect(link); 2176 amdgpu_dm_update_connector_after_detect(aconnector); 2177 2178 } else if (dc_link_detect(link, DETECT_REASON_BOOT)) { 2179 amdgpu_dm_update_connector_after_detect(aconnector); 2180 register_backlight_device(dm, link); 2181 } 2182 2183 2184 } 2185 2186 /* Software is initialized. Now we can register interrupt handlers. */ 2187 switch (adev->asic_type) { 2188 case CHIP_BONAIRE: 2189 case CHIP_HAWAII: 2190 case CHIP_KAVERI: 2191 case CHIP_KABINI: 2192 case CHIP_MULLINS: 2193 case CHIP_TONGA: 2194 case CHIP_FIJI: 2195 case CHIP_CARRIZO: 2196 case CHIP_STONEY: 2197 case CHIP_POLARIS11: 2198 case CHIP_POLARIS10: 2199 case CHIP_POLARIS12: 2200 case CHIP_VEGAM: 2201 case CHIP_VEGA10: 2202 case CHIP_VEGA12: 2203 case CHIP_VEGA20: 2204 if (dce110_register_irq_handlers(dm->adev)) { 2205 DRM_ERROR("DM: Failed to initialize IRQ\n"); 2206 goto fail; 2207 } 2208 break; 2209 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 2210 case CHIP_RAVEN: 2211 if (dcn10_register_irq_handlers(dm->adev)) { 2212 DRM_ERROR("DM: Failed to initialize IRQ\n"); 2213 goto fail; 2214 } 2215 break; 2216 #endif 2217 default: 2218 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type); 2219 goto fail; 2220 } 2221 2222 if (adev->asic_type != CHIP_CARRIZO && adev->asic_type != CHIP_STONEY) 2223 dm->dc->debug.disable_stutter = amdgpu_pp_feature_mask & PP_STUTTER_MODE ? false : true; 2224 2225 return 0; 2226 fail: 2227 kfree(aencoder); 2228 kfree(aconnector); 2229 2230 return -EINVAL; 2231 } 2232 2233 static void amdgpu_dm_destroy_drm_device(struct amdgpu_display_manager *dm) 2234 { 2235 drm_mode_config_cleanup(dm->ddev); 2236 drm_atomic_private_obj_fini(&dm->atomic_obj); 2237 return; 2238 } 2239 2240 /****************************************************************************** 2241 * amdgpu_display_funcs functions 2242 *****************************************************************************/ 2243 2244 /* 2245 * dm_bandwidth_update - program display watermarks 2246 * 2247 * @adev: amdgpu_device pointer 2248 * 2249 * Calculate and program the display watermarks and line buffer allocation. 2250 */ 2251 static void dm_bandwidth_update(struct amdgpu_device *adev) 2252 { 2253 /* TODO: implement later */ 2254 } 2255 2256 static const struct amdgpu_display_funcs dm_display_funcs = { 2257 .bandwidth_update = dm_bandwidth_update, /* called unconditionally */ 2258 .vblank_get_counter = dm_vblank_get_counter,/* called unconditionally */ 2259 .backlight_set_level = NULL, /* never called for DC */ 2260 .backlight_get_level = NULL, /* never called for DC */ 2261 .hpd_sense = NULL,/* called unconditionally */ 2262 .hpd_set_polarity = NULL, /* called unconditionally */ 2263 .hpd_get_gpio_reg = NULL, /* VBIOS parsing. DAL does it. */ 2264 .page_flip_get_scanoutpos = 2265 dm_crtc_get_scanoutpos,/* called unconditionally */ 2266 .add_encoder = NULL, /* VBIOS parsing. DAL does it. */ 2267 .add_connector = NULL, /* VBIOS parsing. DAL does it. */ 2268 }; 2269 2270 #if defined(CONFIG_DEBUG_KERNEL_DC) 2271 2272 static ssize_t s3_debug_store(struct device *device, 2273 struct device_attribute *attr, 2274 const char *buf, 2275 size_t count) 2276 { 2277 int ret; 2278 int s3_state; 2279 struct pci_dev *pdev = to_pci_dev(device); 2280 struct drm_device *drm_dev = pci_get_drvdata(pdev); 2281 struct amdgpu_device *adev = drm_dev->dev_private; 2282 2283 ret = kstrtoint(buf, 0, &s3_state); 2284 2285 if (ret == 0) { 2286 if (s3_state) { 2287 dm_resume(adev); 2288 drm_kms_helper_hotplug_event(adev->ddev); 2289 } else 2290 dm_suspend(adev); 2291 } 2292 2293 return ret == 0 ? count : 0; 2294 } 2295 2296 DEVICE_ATTR_WO(s3_debug); 2297 2298 #endif 2299 2300 static int dm_early_init(void *handle) 2301 { 2302 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 2303 2304 switch (adev->asic_type) { 2305 case CHIP_BONAIRE: 2306 case CHIP_HAWAII: 2307 adev->mode_info.num_crtc = 6; 2308 adev->mode_info.num_hpd = 6; 2309 adev->mode_info.num_dig = 6; 2310 break; 2311 case CHIP_KAVERI: 2312 adev->mode_info.num_crtc = 4; 2313 adev->mode_info.num_hpd = 6; 2314 adev->mode_info.num_dig = 7; 2315 break; 2316 case CHIP_KABINI: 2317 case CHIP_MULLINS: 2318 adev->mode_info.num_crtc = 2; 2319 adev->mode_info.num_hpd = 6; 2320 adev->mode_info.num_dig = 6; 2321 break; 2322 case CHIP_FIJI: 2323 case CHIP_TONGA: 2324 adev->mode_info.num_crtc = 6; 2325 adev->mode_info.num_hpd = 6; 2326 adev->mode_info.num_dig = 7; 2327 break; 2328 case CHIP_CARRIZO: 2329 adev->mode_info.num_crtc = 3; 2330 adev->mode_info.num_hpd = 6; 2331 adev->mode_info.num_dig = 9; 2332 break; 2333 case CHIP_STONEY: 2334 adev->mode_info.num_crtc = 2; 2335 adev->mode_info.num_hpd = 6; 2336 adev->mode_info.num_dig = 9; 2337 break; 2338 case CHIP_POLARIS11: 2339 case CHIP_POLARIS12: 2340 adev->mode_info.num_crtc = 5; 2341 adev->mode_info.num_hpd = 5; 2342 adev->mode_info.num_dig = 5; 2343 break; 2344 case CHIP_POLARIS10: 2345 case CHIP_VEGAM: 2346 adev->mode_info.num_crtc = 6; 2347 adev->mode_info.num_hpd = 6; 2348 adev->mode_info.num_dig = 6; 2349 break; 2350 case CHIP_VEGA10: 2351 case CHIP_VEGA12: 2352 case CHIP_VEGA20: 2353 adev->mode_info.num_crtc = 6; 2354 adev->mode_info.num_hpd = 6; 2355 adev->mode_info.num_dig = 6; 2356 break; 2357 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 2358 case CHIP_RAVEN: 2359 adev->mode_info.num_crtc = 4; 2360 adev->mode_info.num_hpd = 4; 2361 adev->mode_info.num_dig = 4; 2362 break; 2363 #endif 2364 default: 2365 DRM_ERROR("Unsupported ASIC type: 0x%X\n", adev->asic_type); 2366 return -EINVAL; 2367 } 2368 2369 amdgpu_dm_set_irq_funcs(adev); 2370 2371 if (adev->mode_info.funcs == NULL) 2372 adev->mode_info.funcs = &dm_display_funcs; 2373 2374 /* 2375 * Note: Do NOT change adev->audio_endpt_rreg and 2376 * adev->audio_endpt_wreg because they are initialised in 2377 * amdgpu_device_init() 2378 */ 2379 #if defined(CONFIG_DEBUG_KERNEL_DC) 2380 device_create_file( 2381 adev->ddev->dev, 2382 &dev_attr_s3_debug); 2383 #endif 2384 2385 return 0; 2386 } 2387 2388 static bool modeset_required(struct drm_crtc_state *crtc_state, 2389 struct dc_stream_state *new_stream, 2390 struct dc_stream_state *old_stream) 2391 { 2392 if (!drm_atomic_crtc_needs_modeset(crtc_state)) 2393 return false; 2394 2395 if (!crtc_state->enable) 2396 return false; 2397 2398 return crtc_state->active; 2399 } 2400 2401 static bool modereset_required(struct drm_crtc_state *crtc_state) 2402 { 2403 if (!drm_atomic_crtc_needs_modeset(crtc_state)) 2404 return false; 2405 2406 return !crtc_state->enable || !crtc_state->active; 2407 } 2408 2409 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder) 2410 { 2411 drm_encoder_cleanup(encoder); 2412 kfree(encoder); 2413 } 2414 2415 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = { 2416 .destroy = amdgpu_dm_encoder_destroy, 2417 }; 2418 2419 2420 static int fill_dc_scaling_info(const struct drm_plane_state *state, 2421 struct dc_scaling_info *scaling_info) 2422 { 2423 int scale_w, scale_h; 2424 2425 memset(scaling_info, 0, sizeof(*scaling_info)); 2426 2427 /* Source is fixed 16.16 but we ignore mantissa for now... */ 2428 scaling_info->src_rect.x = state->src_x >> 16; 2429 scaling_info->src_rect.y = state->src_y >> 16; 2430 2431 scaling_info->src_rect.width = state->src_w >> 16; 2432 if (scaling_info->src_rect.width == 0) 2433 return -EINVAL; 2434 2435 scaling_info->src_rect.height = state->src_h >> 16; 2436 if (scaling_info->src_rect.height == 0) 2437 return -EINVAL; 2438 2439 scaling_info->dst_rect.x = state->crtc_x; 2440 scaling_info->dst_rect.y = state->crtc_y; 2441 2442 if (state->crtc_w == 0) 2443 return -EINVAL; 2444 2445 scaling_info->dst_rect.width = state->crtc_w; 2446 2447 if (state->crtc_h == 0) 2448 return -EINVAL; 2449 2450 scaling_info->dst_rect.height = state->crtc_h; 2451 2452 /* DRM doesn't specify clipping on destination output. */ 2453 scaling_info->clip_rect = scaling_info->dst_rect; 2454 2455 /* TODO: Validate scaling per-format with DC plane caps */ 2456 scale_w = scaling_info->dst_rect.width * 1000 / 2457 scaling_info->src_rect.width; 2458 2459 if (scale_w < 250 || scale_w > 16000) 2460 return -EINVAL; 2461 2462 scale_h = scaling_info->dst_rect.height * 1000 / 2463 scaling_info->src_rect.height; 2464 2465 if (scale_h < 250 || scale_h > 16000) 2466 return -EINVAL; 2467 2468 /* 2469 * The "scaling_quality" can be ignored for now, quality = 0 has DC 2470 * assume reasonable defaults based on the format. 2471 */ 2472 2473 return 0; 2474 } 2475 2476 static int get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb, 2477 uint64_t *tiling_flags) 2478 { 2479 struct amdgpu_bo *rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]); 2480 int r = amdgpu_bo_reserve(rbo, false); 2481 2482 if (unlikely(r)) { 2483 /* Don't show error message when returning -ERESTARTSYS */ 2484 if (r != -ERESTARTSYS) 2485 DRM_ERROR("Unable to reserve buffer: %d\n", r); 2486 return r; 2487 } 2488 2489 if (tiling_flags) 2490 amdgpu_bo_get_tiling_flags(rbo, tiling_flags); 2491 2492 amdgpu_bo_unreserve(rbo); 2493 2494 return r; 2495 } 2496 2497 static inline uint64_t get_dcc_address(uint64_t address, uint64_t tiling_flags) 2498 { 2499 uint32_t offset = AMDGPU_TILING_GET(tiling_flags, DCC_OFFSET_256B); 2500 2501 return offset ? (address + offset * 256) : 0; 2502 } 2503 2504 static int 2505 fill_plane_dcc_attributes(struct amdgpu_device *adev, 2506 const struct amdgpu_framebuffer *afb, 2507 const enum surface_pixel_format format, 2508 const enum dc_rotation_angle rotation, 2509 const union plane_size *plane_size, 2510 const union dc_tiling_info *tiling_info, 2511 const uint64_t info, 2512 struct dc_plane_dcc_param *dcc, 2513 struct dc_plane_address *address) 2514 { 2515 struct dc *dc = adev->dm.dc; 2516 struct dc_dcc_surface_param input; 2517 struct dc_surface_dcc_cap output; 2518 uint32_t offset = AMDGPU_TILING_GET(info, DCC_OFFSET_256B); 2519 uint32_t i64b = AMDGPU_TILING_GET(info, DCC_INDEPENDENT_64B) != 0; 2520 uint64_t dcc_address; 2521 2522 memset(&input, 0, sizeof(input)); 2523 memset(&output, 0, sizeof(output)); 2524 2525 if (!offset) 2526 return 0; 2527 2528 if (format >= SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) 2529 return 0; 2530 2531 if (!dc->cap_funcs.get_dcc_compression_cap) 2532 return -EINVAL; 2533 2534 input.format = format; 2535 input.surface_size.width = plane_size->grph.surface_size.width; 2536 input.surface_size.height = plane_size->grph.surface_size.height; 2537 input.swizzle_mode = tiling_info->gfx9.swizzle; 2538 2539 if (rotation == ROTATION_ANGLE_0 || rotation == ROTATION_ANGLE_180) 2540 input.scan = SCAN_DIRECTION_HORIZONTAL; 2541 else if (rotation == ROTATION_ANGLE_90 || rotation == ROTATION_ANGLE_270) 2542 input.scan = SCAN_DIRECTION_VERTICAL; 2543 2544 if (!dc->cap_funcs.get_dcc_compression_cap(dc, &input, &output)) 2545 return -EINVAL; 2546 2547 if (!output.capable) 2548 return -EINVAL; 2549 2550 if (i64b == 0 && output.grph.rgb.independent_64b_blks != 0) 2551 return -EINVAL; 2552 2553 dcc->enable = 1; 2554 dcc->grph.meta_pitch = 2555 AMDGPU_TILING_GET(info, DCC_PITCH_MAX) + 1; 2556 dcc->grph.independent_64b_blks = i64b; 2557 2558 dcc_address = get_dcc_address(afb->address, info); 2559 address->grph.meta_addr.low_part = lower_32_bits(dcc_address); 2560 address->grph.meta_addr.high_part = upper_32_bits(dcc_address); 2561 2562 return 0; 2563 } 2564 2565 static int 2566 fill_plane_buffer_attributes(struct amdgpu_device *adev, 2567 const struct amdgpu_framebuffer *afb, 2568 const enum surface_pixel_format format, 2569 const enum dc_rotation_angle rotation, 2570 const uint64_t tiling_flags, 2571 union dc_tiling_info *tiling_info, 2572 union plane_size *plane_size, 2573 struct dc_plane_dcc_param *dcc, 2574 struct dc_plane_address *address) 2575 { 2576 const struct drm_framebuffer *fb = &afb->base; 2577 int ret; 2578 2579 memset(tiling_info, 0, sizeof(*tiling_info)); 2580 memset(plane_size, 0, sizeof(*plane_size)); 2581 memset(dcc, 0, sizeof(*dcc)); 2582 memset(address, 0, sizeof(*address)); 2583 2584 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) { 2585 plane_size->grph.surface_size.x = 0; 2586 plane_size->grph.surface_size.y = 0; 2587 plane_size->grph.surface_size.width = fb->width; 2588 plane_size->grph.surface_size.height = fb->height; 2589 plane_size->grph.surface_pitch = 2590 fb->pitches[0] / fb->format->cpp[0]; 2591 2592 address->type = PLN_ADDR_TYPE_GRAPHICS; 2593 address->grph.addr.low_part = lower_32_bits(afb->address); 2594 address->grph.addr.high_part = upper_32_bits(afb->address); 2595 } else { 2596 uint64_t chroma_addr = afb->address + fb->offsets[1]; 2597 2598 plane_size->video.luma_size.x = 0; 2599 plane_size->video.luma_size.y = 0; 2600 plane_size->video.luma_size.width = fb->width; 2601 plane_size->video.luma_size.height = fb->height; 2602 plane_size->video.luma_pitch = 2603 fb->pitches[0] / fb->format->cpp[0]; 2604 2605 plane_size->video.chroma_size.x = 0; 2606 plane_size->video.chroma_size.y = 0; 2607 /* TODO: set these based on surface format */ 2608 plane_size->video.chroma_size.width = fb->width / 2; 2609 plane_size->video.chroma_size.height = fb->height / 2; 2610 2611 plane_size->video.chroma_pitch = 2612 fb->pitches[1] / fb->format->cpp[1]; 2613 2614 address->type = PLN_ADDR_TYPE_VIDEO_PROGRESSIVE; 2615 address->video_progressive.luma_addr.low_part = 2616 lower_32_bits(afb->address); 2617 address->video_progressive.luma_addr.high_part = 2618 upper_32_bits(afb->address); 2619 address->video_progressive.chroma_addr.low_part = 2620 lower_32_bits(chroma_addr); 2621 address->video_progressive.chroma_addr.high_part = 2622 upper_32_bits(chroma_addr); 2623 } 2624 2625 /* Fill GFX8 params */ 2626 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == DC_ARRAY_2D_TILED_THIN1) { 2627 unsigned int bankw, bankh, mtaspect, tile_split, num_banks; 2628 2629 bankw = AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH); 2630 bankh = AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT); 2631 mtaspect = AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT); 2632 tile_split = AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT); 2633 num_banks = AMDGPU_TILING_GET(tiling_flags, NUM_BANKS); 2634 2635 /* XXX fix me for VI */ 2636 tiling_info->gfx8.num_banks = num_banks; 2637 tiling_info->gfx8.array_mode = 2638 DC_ARRAY_2D_TILED_THIN1; 2639 tiling_info->gfx8.tile_split = tile_split; 2640 tiling_info->gfx8.bank_width = bankw; 2641 tiling_info->gfx8.bank_height = bankh; 2642 tiling_info->gfx8.tile_aspect = mtaspect; 2643 tiling_info->gfx8.tile_mode = 2644 DC_ADDR_SURF_MICRO_TILING_DISPLAY; 2645 } else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) 2646 == DC_ARRAY_1D_TILED_THIN1) { 2647 tiling_info->gfx8.array_mode = DC_ARRAY_1D_TILED_THIN1; 2648 } 2649 2650 tiling_info->gfx8.pipe_config = 2651 AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG); 2652 2653 if (adev->asic_type == CHIP_VEGA10 || 2654 adev->asic_type == CHIP_VEGA12 || 2655 adev->asic_type == CHIP_VEGA20 || 2656 adev->asic_type == CHIP_RAVEN) { 2657 /* Fill GFX9 params */ 2658 tiling_info->gfx9.num_pipes = 2659 adev->gfx.config.gb_addr_config_fields.num_pipes; 2660 tiling_info->gfx9.num_banks = 2661 adev->gfx.config.gb_addr_config_fields.num_banks; 2662 tiling_info->gfx9.pipe_interleave = 2663 adev->gfx.config.gb_addr_config_fields.pipe_interleave_size; 2664 tiling_info->gfx9.num_shader_engines = 2665 adev->gfx.config.gb_addr_config_fields.num_se; 2666 tiling_info->gfx9.max_compressed_frags = 2667 adev->gfx.config.gb_addr_config_fields.max_compress_frags; 2668 tiling_info->gfx9.num_rb_per_se = 2669 adev->gfx.config.gb_addr_config_fields.num_rb_per_se; 2670 tiling_info->gfx9.swizzle = 2671 AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE); 2672 tiling_info->gfx9.shaderEnable = 1; 2673 2674 ret = fill_plane_dcc_attributes(adev, afb, format, rotation, 2675 plane_size, tiling_info, 2676 tiling_flags, dcc, address); 2677 if (ret) 2678 return ret; 2679 } 2680 2681 return 0; 2682 } 2683 2684 static void 2685 fill_blending_from_plane_state(const struct drm_plane_state *plane_state, 2686 bool *per_pixel_alpha, bool *global_alpha, 2687 int *global_alpha_value) 2688 { 2689 *per_pixel_alpha = false; 2690 *global_alpha = false; 2691 *global_alpha_value = 0xff; 2692 2693 if (plane_state->plane->type != DRM_PLANE_TYPE_OVERLAY) 2694 return; 2695 2696 if (plane_state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI) { 2697 static const uint32_t alpha_formats[] = { 2698 DRM_FORMAT_ARGB8888, 2699 DRM_FORMAT_RGBA8888, 2700 DRM_FORMAT_ABGR8888, 2701 }; 2702 uint32_t format = plane_state->fb->format->format; 2703 unsigned int i; 2704 2705 for (i = 0; i < ARRAY_SIZE(alpha_formats); ++i) { 2706 if (format == alpha_formats[i]) { 2707 *per_pixel_alpha = true; 2708 break; 2709 } 2710 } 2711 } 2712 2713 if (plane_state->alpha < 0xffff) { 2714 *global_alpha = true; 2715 *global_alpha_value = plane_state->alpha >> 8; 2716 } 2717 } 2718 2719 static int 2720 fill_plane_color_attributes(const struct drm_plane_state *plane_state, 2721 const enum surface_pixel_format format, 2722 enum dc_color_space *color_space) 2723 { 2724 bool full_range; 2725 2726 *color_space = COLOR_SPACE_SRGB; 2727 2728 /* DRM color properties only affect non-RGB formats. */ 2729 if (format < SURFACE_PIXEL_FORMAT_VIDEO_BEGIN) 2730 return 0; 2731 2732 full_range = (plane_state->color_range == DRM_COLOR_YCBCR_FULL_RANGE); 2733 2734 switch (plane_state->color_encoding) { 2735 case DRM_COLOR_YCBCR_BT601: 2736 if (full_range) 2737 *color_space = COLOR_SPACE_YCBCR601; 2738 else 2739 *color_space = COLOR_SPACE_YCBCR601_LIMITED; 2740 break; 2741 2742 case DRM_COLOR_YCBCR_BT709: 2743 if (full_range) 2744 *color_space = COLOR_SPACE_YCBCR709; 2745 else 2746 *color_space = COLOR_SPACE_YCBCR709_LIMITED; 2747 break; 2748 2749 case DRM_COLOR_YCBCR_BT2020: 2750 if (full_range) 2751 *color_space = COLOR_SPACE_2020_YCBCR; 2752 else 2753 return -EINVAL; 2754 break; 2755 2756 default: 2757 return -EINVAL; 2758 } 2759 2760 return 0; 2761 } 2762 2763 static int 2764 fill_dc_plane_info_and_addr(struct amdgpu_device *adev, 2765 const struct drm_plane_state *plane_state, 2766 const uint64_t tiling_flags, 2767 struct dc_plane_info *plane_info, 2768 struct dc_plane_address *address) 2769 { 2770 const struct drm_framebuffer *fb = plane_state->fb; 2771 const struct amdgpu_framebuffer *afb = 2772 to_amdgpu_framebuffer(plane_state->fb); 2773 struct drm_format_name_buf format_name; 2774 int ret; 2775 2776 memset(plane_info, 0, sizeof(*plane_info)); 2777 2778 switch (fb->format->format) { 2779 case DRM_FORMAT_C8: 2780 plane_info->format = 2781 SURFACE_PIXEL_FORMAT_GRPH_PALETA_256_COLORS; 2782 break; 2783 case DRM_FORMAT_RGB565: 2784 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_RGB565; 2785 break; 2786 case DRM_FORMAT_XRGB8888: 2787 case DRM_FORMAT_ARGB8888: 2788 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB8888; 2789 break; 2790 case DRM_FORMAT_XRGB2101010: 2791 case DRM_FORMAT_ARGB2101010: 2792 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ARGB2101010; 2793 break; 2794 case DRM_FORMAT_XBGR2101010: 2795 case DRM_FORMAT_ABGR2101010: 2796 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR2101010; 2797 break; 2798 case DRM_FORMAT_XBGR8888: 2799 case DRM_FORMAT_ABGR8888: 2800 plane_info->format = SURFACE_PIXEL_FORMAT_GRPH_ABGR8888; 2801 break; 2802 case DRM_FORMAT_NV21: 2803 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr; 2804 break; 2805 case DRM_FORMAT_NV12: 2806 plane_info->format = SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb; 2807 break; 2808 default: 2809 DRM_ERROR( 2810 "Unsupported screen format %s\n", 2811 drm_get_format_name(fb->format->format, &format_name)); 2812 return -EINVAL; 2813 } 2814 2815 switch (plane_state->rotation & DRM_MODE_ROTATE_MASK) { 2816 case DRM_MODE_ROTATE_0: 2817 plane_info->rotation = ROTATION_ANGLE_0; 2818 break; 2819 case DRM_MODE_ROTATE_90: 2820 plane_info->rotation = ROTATION_ANGLE_90; 2821 break; 2822 case DRM_MODE_ROTATE_180: 2823 plane_info->rotation = ROTATION_ANGLE_180; 2824 break; 2825 case DRM_MODE_ROTATE_270: 2826 plane_info->rotation = ROTATION_ANGLE_270; 2827 break; 2828 default: 2829 plane_info->rotation = ROTATION_ANGLE_0; 2830 break; 2831 } 2832 2833 plane_info->visible = true; 2834 plane_info->stereo_format = PLANE_STEREO_FORMAT_NONE; 2835 2836 ret = fill_plane_color_attributes(plane_state, plane_info->format, 2837 &plane_info->color_space); 2838 if (ret) 2839 return ret; 2840 2841 ret = fill_plane_buffer_attributes(adev, afb, plane_info->format, 2842 plane_info->rotation, tiling_flags, 2843 &plane_info->tiling_info, 2844 &plane_info->plane_size, 2845 &plane_info->dcc, address); 2846 if (ret) 2847 return ret; 2848 2849 fill_blending_from_plane_state( 2850 plane_state, &plane_info->per_pixel_alpha, 2851 &plane_info->global_alpha, &plane_info->global_alpha_value); 2852 2853 return 0; 2854 } 2855 2856 static int fill_dc_plane_attributes(struct amdgpu_device *adev, 2857 struct dc_plane_state *dc_plane_state, 2858 struct drm_plane_state *plane_state, 2859 struct drm_crtc_state *crtc_state) 2860 { 2861 const struct amdgpu_framebuffer *amdgpu_fb = 2862 to_amdgpu_framebuffer(plane_state->fb); 2863 struct dc_scaling_info scaling_info; 2864 struct dc_plane_info plane_info; 2865 uint64_t tiling_flags; 2866 int ret; 2867 2868 ret = fill_dc_scaling_info(plane_state, &scaling_info); 2869 if (ret) 2870 return ret; 2871 2872 dc_plane_state->src_rect = scaling_info.src_rect; 2873 dc_plane_state->dst_rect = scaling_info.dst_rect; 2874 dc_plane_state->clip_rect = scaling_info.clip_rect; 2875 dc_plane_state->scaling_quality = scaling_info.scaling_quality; 2876 2877 ret = get_fb_info(amdgpu_fb, &tiling_flags); 2878 if (ret) 2879 return ret; 2880 2881 ret = fill_dc_plane_info_and_addr(adev, plane_state, tiling_flags, 2882 &plane_info, 2883 &dc_plane_state->address); 2884 if (ret) 2885 return ret; 2886 2887 dc_plane_state->format = plane_info.format; 2888 dc_plane_state->color_space = plane_info.color_space; 2889 dc_plane_state->format = plane_info.format; 2890 dc_plane_state->plane_size = plane_info.plane_size; 2891 dc_plane_state->rotation = plane_info.rotation; 2892 dc_plane_state->horizontal_mirror = plane_info.horizontal_mirror; 2893 dc_plane_state->stereo_format = plane_info.stereo_format; 2894 dc_plane_state->tiling_info = plane_info.tiling_info; 2895 dc_plane_state->visible = plane_info.visible; 2896 dc_plane_state->per_pixel_alpha = plane_info.per_pixel_alpha; 2897 dc_plane_state->global_alpha = plane_info.global_alpha; 2898 dc_plane_state->global_alpha_value = plane_info.global_alpha_value; 2899 dc_plane_state->dcc = plane_info.dcc; 2900 2901 /* 2902 * Always set input transfer function, since plane state is refreshed 2903 * every time. 2904 */ 2905 ret = amdgpu_dm_set_degamma_lut(crtc_state, dc_plane_state); 2906 if (ret) { 2907 dc_transfer_func_release(dc_plane_state->in_transfer_func); 2908 dc_plane_state->in_transfer_func = NULL; 2909 } 2910 2911 return ret; 2912 } 2913 2914 static void update_stream_scaling_settings(const struct drm_display_mode *mode, 2915 const struct dm_connector_state *dm_state, 2916 struct dc_stream_state *stream) 2917 { 2918 enum amdgpu_rmx_type rmx_type; 2919 2920 struct rect src = { 0 }; /* viewport in composition space*/ 2921 struct rect dst = { 0 }; /* stream addressable area */ 2922 2923 /* no mode. nothing to be done */ 2924 if (!mode) 2925 return; 2926 2927 /* Full screen scaling by default */ 2928 src.width = mode->hdisplay; 2929 src.height = mode->vdisplay; 2930 dst.width = stream->timing.h_addressable; 2931 dst.height = stream->timing.v_addressable; 2932 2933 if (dm_state) { 2934 rmx_type = dm_state->scaling; 2935 if (rmx_type == RMX_ASPECT || rmx_type == RMX_OFF) { 2936 if (src.width * dst.height < 2937 src.height * dst.width) { 2938 /* height needs less upscaling/more downscaling */ 2939 dst.width = src.width * 2940 dst.height / src.height; 2941 } else { 2942 /* width needs less upscaling/more downscaling */ 2943 dst.height = src.height * 2944 dst.width / src.width; 2945 } 2946 } else if (rmx_type == RMX_CENTER) { 2947 dst = src; 2948 } 2949 2950 dst.x = (stream->timing.h_addressable - dst.width) / 2; 2951 dst.y = (stream->timing.v_addressable - dst.height) / 2; 2952 2953 if (dm_state->underscan_enable) { 2954 dst.x += dm_state->underscan_hborder / 2; 2955 dst.y += dm_state->underscan_vborder / 2; 2956 dst.width -= dm_state->underscan_hborder; 2957 dst.height -= dm_state->underscan_vborder; 2958 } 2959 } 2960 2961 stream->src = src; 2962 stream->dst = dst; 2963 2964 DRM_DEBUG_DRIVER("Destination Rectangle x:%d y:%d width:%d height:%d\n", 2965 dst.x, dst.y, dst.width, dst.height); 2966 2967 } 2968 2969 static enum dc_color_depth 2970 convert_color_depth_from_display_info(const struct drm_connector *connector) 2971 { 2972 struct dm_connector_state *dm_conn_state = 2973 to_dm_connector_state(connector->state); 2974 uint32_t bpc = connector->display_info.bpc; 2975 2976 /* TODO: Remove this when there's support for max_bpc in drm */ 2977 if (dm_conn_state && bpc > dm_conn_state->max_bpc) 2978 /* Round down to nearest even number. */ 2979 bpc = dm_conn_state->max_bpc - (dm_conn_state->max_bpc & 1); 2980 2981 switch (bpc) { 2982 case 0: 2983 /* 2984 * Temporary Work around, DRM doesn't parse color depth for 2985 * EDID revision before 1.4 2986 * TODO: Fix edid parsing 2987 */ 2988 return COLOR_DEPTH_888; 2989 case 6: 2990 return COLOR_DEPTH_666; 2991 case 8: 2992 return COLOR_DEPTH_888; 2993 case 10: 2994 return COLOR_DEPTH_101010; 2995 case 12: 2996 return COLOR_DEPTH_121212; 2997 case 14: 2998 return COLOR_DEPTH_141414; 2999 case 16: 3000 return COLOR_DEPTH_161616; 3001 default: 3002 return COLOR_DEPTH_UNDEFINED; 3003 } 3004 } 3005 3006 static enum dc_aspect_ratio 3007 get_aspect_ratio(const struct drm_display_mode *mode_in) 3008 { 3009 /* 1-1 mapping, since both enums follow the HDMI spec. */ 3010 return (enum dc_aspect_ratio) mode_in->picture_aspect_ratio; 3011 } 3012 3013 static enum dc_color_space 3014 get_output_color_space(const struct dc_crtc_timing *dc_crtc_timing) 3015 { 3016 enum dc_color_space color_space = COLOR_SPACE_SRGB; 3017 3018 switch (dc_crtc_timing->pixel_encoding) { 3019 case PIXEL_ENCODING_YCBCR422: 3020 case PIXEL_ENCODING_YCBCR444: 3021 case PIXEL_ENCODING_YCBCR420: 3022 { 3023 /* 3024 * 27030khz is the separation point between HDTV and SDTV 3025 * according to HDMI spec, we use YCbCr709 and YCbCr601 3026 * respectively 3027 */ 3028 if (dc_crtc_timing->pix_clk_100hz > 270300) { 3029 if (dc_crtc_timing->flags.Y_ONLY) 3030 color_space = 3031 COLOR_SPACE_YCBCR709_LIMITED; 3032 else 3033 color_space = COLOR_SPACE_YCBCR709; 3034 } else { 3035 if (dc_crtc_timing->flags.Y_ONLY) 3036 color_space = 3037 COLOR_SPACE_YCBCR601_LIMITED; 3038 else 3039 color_space = COLOR_SPACE_YCBCR601; 3040 } 3041 3042 } 3043 break; 3044 case PIXEL_ENCODING_RGB: 3045 color_space = COLOR_SPACE_SRGB; 3046 break; 3047 3048 default: 3049 WARN_ON(1); 3050 break; 3051 } 3052 3053 return color_space; 3054 } 3055 3056 static void reduce_mode_colour_depth(struct dc_crtc_timing *timing_out) 3057 { 3058 if (timing_out->display_color_depth <= COLOR_DEPTH_888) 3059 return; 3060 3061 timing_out->display_color_depth--; 3062 } 3063 3064 static void adjust_colour_depth_from_display_info(struct dc_crtc_timing *timing_out, 3065 const struct drm_display_info *info) 3066 { 3067 int normalized_clk; 3068 if (timing_out->display_color_depth <= COLOR_DEPTH_888) 3069 return; 3070 do { 3071 normalized_clk = timing_out->pix_clk_100hz / 10; 3072 /* YCbCr 4:2:0 requires additional adjustment of 1/2 */ 3073 if (timing_out->pixel_encoding == PIXEL_ENCODING_YCBCR420) 3074 normalized_clk /= 2; 3075 /* Adjusting pix clock following on HDMI spec based on colour depth */ 3076 switch (timing_out->display_color_depth) { 3077 case COLOR_DEPTH_101010: 3078 normalized_clk = (normalized_clk * 30) / 24; 3079 break; 3080 case COLOR_DEPTH_121212: 3081 normalized_clk = (normalized_clk * 36) / 24; 3082 break; 3083 case COLOR_DEPTH_161616: 3084 normalized_clk = (normalized_clk * 48) / 24; 3085 break; 3086 default: 3087 return; 3088 } 3089 if (normalized_clk <= info->max_tmds_clock) 3090 return; 3091 reduce_mode_colour_depth(timing_out); 3092 3093 } while (timing_out->display_color_depth > COLOR_DEPTH_888); 3094 3095 } 3096 3097 static void 3098 fill_stream_properties_from_drm_display_mode(struct dc_stream_state *stream, 3099 const struct drm_display_mode *mode_in, 3100 const struct drm_connector *connector, 3101 const struct dc_stream_state *old_stream) 3102 { 3103 struct dc_crtc_timing *timing_out = &stream->timing; 3104 const struct drm_display_info *info = &connector->display_info; 3105 3106 memset(timing_out, 0, sizeof(struct dc_crtc_timing)); 3107 3108 timing_out->h_border_left = 0; 3109 timing_out->h_border_right = 0; 3110 timing_out->v_border_top = 0; 3111 timing_out->v_border_bottom = 0; 3112 /* TODO: un-hardcode */ 3113 if (drm_mode_is_420_only(info, mode_in) 3114 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) 3115 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR420; 3116 else if ((connector->display_info.color_formats & DRM_COLOR_FORMAT_YCRCB444) 3117 && stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) 3118 timing_out->pixel_encoding = PIXEL_ENCODING_YCBCR444; 3119 else 3120 timing_out->pixel_encoding = PIXEL_ENCODING_RGB; 3121 3122 timing_out->timing_3d_format = TIMING_3D_FORMAT_NONE; 3123 timing_out->display_color_depth = convert_color_depth_from_display_info( 3124 connector); 3125 timing_out->scan_type = SCANNING_TYPE_NODATA; 3126 timing_out->hdmi_vic = 0; 3127 3128 if(old_stream) { 3129 timing_out->vic = old_stream->timing.vic; 3130 timing_out->flags.HSYNC_POSITIVE_POLARITY = old_stream->timing.flags.HSYNC_POSITIVE_POLARITY; 3131 timing_out->flags.VSYNC_POSITIVE_POLARITY = old_stream->timing.flags.VSYNC_POSITIVE_POLARITY; 3132 } else { 3133 timing_out->vic = drm_match_cea_mode(mode_in); 3134 if (mode_in->flags & DRM_MODE_FLAG_PHSYNC) 3135 timing_out->flags.HSYNC_POSITIVE_POLARITY = 1; 3136 if (mode_in->flags & DRM_MODE_FLAG_PVSYNC) 3137 timing_out->flags.VSYNC_POSITIVE_POLARITY = 1; 3138 } 3139 3140 timing_out->h_addressable = mode_in->crtc_hdisplay; 3141 timing_out->h_total = mode_in->crtc_htotal; 3142 timing_out->h_sync_width = 3143 mode_in->crtc_hsync_end - mode_in->crtc_hsync_start; 3144 timing_out->h_front_porch = 3145 mode_in->crtc_hsync_start - mode_in->crtc_hdisplay; 3146 timing_out->v_total = mode_in->crtc_vtotal; 3147 timing_out->v_addressable = mode_in->crtc_vdisplay; 3148 timing_out->v_front_porch = 3149 mode_in->crtc_vsync_start - mode_in->crtc_vdisplay; 3150 timing_out->v_sync_width = 3151 mode_in->crtc_vsync_end - mode_in->crtc_vsync_start; 3152 timing_out->pix_clk_100hz = mode_in->crtc_clock * 10; 3153 timing_out->aspect_ratio = get_aspect_ratio(mode_in); 3154 3155 stream->output_color_space = get_output_color_space(timing_out); 3156 3157 stream->out_transfer_func->type = TF_TYPE_PREDEFINED; 3158 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB; 3159 if (stream->signal == SIGNAL_TYPE_HDMI_TYPE_A) 3160 adjust_colour_depth_from_display_info(timing_out, info); 3161 } 3162 3163 static void fill_audio_info(struct audio_info *audio_info, 3164 const struct drm_connector *drm_connector, 3165 const struct dc_sink *dc_sink) 3166 { 3167 int i = 0; 3168 int cea_revision = 0; 3169 const struct dc_edid_caps *edid_caps = &dc_sink->edid_caps; 3170 3171 audio_info->manufacture_id = edid_caps->manufacturer_id; 3172 audio_info->product_id = edid_caps->product_id; 3173 3174 cea_revision = drm_connector->display_info.cea_rev; 3175 3176 strscpy(audio_info->display_name, 3177 edid_caps->display_name, 3178 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS); 3179 3180 if (cea_revision >= 3) { 3181 audio_info->mode_count = edid_caps->audio_mode_count; 3182 3183 for (i = 0; i < audio_info->mode_count; ++i) { 3184 audio_info->modes[i].format_code = 3185 (enum audio_format_code) 3186 (edid_caps->audio_modes[i].format_code); 3187 audio_info->modes[i].channel_count = 3188 edid_caps->audio_modes[i].channel_count; 3189 audio_info->modes[i].sample_rates.all = 3190 edid_caps->audio_modes[i].sample_rate; 3191 audio_info->modes[i].sample_size = 3192 edid_caps->audio_modes[i].sample_size; 3193 } 3194 } 3195 3196 audio_info->flags.all = edid_caps->speaker_flags; 3197 3198 /* TODO: We only check for the progressive mode, check for interlace mode too */ 3199 if (drm_connector->latency_present[0]) { 3200 audio_info->video_latency = drm_connector->video_latency[0]; 3201 audio_info->audio_latency = drm_connector->audio_latency[0]; 3202 } 3203 3204 /* TODO: For DP, video and audio latency should be calculated from DPCD caps */ 3205 3206 } 3207 3208 static void 3209 copy_crtc_timing_for_drm_display_mode(const struct drm_display_mode *src_mode, 3210 struct drm_display_mode *dst_mode) 3211 { 3212 dst_mode->crtc_hdisplay = src_mode->crtc_hdisplay; 3213 dst_mode->crtc_vdisplay = src_mode->crtc_vdisplay; 3214 dst_mode->crtc_clock = src_mode->crtc_clock; 3215 dst_mode->crtc_hblank_start = src_mode->crtc_hblank_start; 3216 dst_mode->crtc_hblank_end = src_mode->crtc_hblank_end; 3217 dst_mode->crtc_hsync_start = src_mode->crtc_hsync_start; 3218 dst_mode->crtc_hsync_end = src_mode->crtc_hsync_end; 3219 dst_mode->crtc_htotal = src_mode->crtc_htotal; 3220 dst_mode->crtc_hskew = src_mode->crtc_hskew; 3221 dst_mode->crtc_vblank_start = src_mode->crtc_vblank_start; 3222 dst_mode->crtc_vblank_end = src_mode->crtc_vblank_end; 3223 dst_mode->crtc_vsync_start = src_mode->crtc_vsync_start; 3224 dst_mode->crtc_vsync_end = src_mode->crtc_vsync_end; 3225 dst_mode->crtc_vtotal = src_mode->crtc_vtotal; 3226 } 3227 3228 static void 3229 decide_crtc_timing_for_drm_display_mode(struct drm_display_mode *drm_mode, 3230 const struct drm_display_mode *native_mode, 3231 bool scale_enabled) 3232 { 3233 if (scale_enabled) { 3234 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode); 3235 } else if (native_mode->clock == drm_mode->clock && 3236 native_mode->htotal == drm_mode->htotal && 3237 native_mode->vtotal == drm_mode->vtotal) { 3238 copy_crtc_timing_for_drm_display_mode(native_mode, drm_mode); 3239 } else { 3240 /* no scaling nor amdgpu inserted, no need to patch */ 3241 } 3242 } 3243 3244 static struct dc_sink * 3245 create_fake_sink(struct amdgpu_dm_connector *aconnector) 3246 { 3247 struct dc_sink_init_data sink_init_data = { 0 }; 3248 struct dc_sink *sink = NULL; 3249 sink_init_data.link = aconnector->dc_link; 3250 sink_init_data.sink_signal = aconnector->dc_link->connector_signal; 3251 3252 sink = dc_sink_create(&sink_init_data); 3253 if (!sink) { 3254 DRM_ERROR("Failed to create sink!\n"); 3255 return NULL; 3256 } 3257 sink->sink_signal = SIGNAL_TYPE_VIRTUAL; 3258 3259 return sink; 3260 } 3261 3262 static void set_multisync_trigger_params( 3263 struct dc_stream_state *stream) 3264 { 3265 if (stream->triggered_crtc_reset.enabled) { 3266 stream->triggered_crtc_reset.event = CRTC_EVENT_VSYNC_RISING; 3267 stream->triggered_crtc_reset.delay = TRIGGER_DELAY_NEXT_LINE; 3268 } 3269 } 3270 3271 static void set_master_stream(struct dc_stream_state *stream_set[], 3272 int stream_count) 3273 { 3274 int j, highest_rfr = 0, master_stream = 0; 3275 3276 for (j = 0; j < stream_count; j++) { 3277 if (stream_set[j] && stream_set[j]->triggered_crtc_reset.enabled) { 3278 int refresh_rate = 0; 3279 3280 refresh_rate = (stream_set[j]->timing.pix_clk_100hz*100)/ 3281 (stream_set[j]->timing.h_total*stream_set[j]->timing.v_total); 3282 if (refresh_rate > highest_rfr) { 3283 highest_rfr = refresh_rate; 3284 master_stream = j; 3285 } 3286 } 3287 } 3288 for (j = 0; j < stream_count; j++) { 3289 if (stream_set[j]) 3290 stream_set[j]->triggered_crtc_reset.event_source = stream_set[master_stream]; 3291 } 3292 } 3293 3294 static void dm_enable_per_frame_crtc_master_sync(struct dc_state *context) 3295 { 3296 int i = 0; 3297 3298 if (context->stream_count < 2) 3299 return; 3300 for (i = 0; i < context->stream_count ; i++) { 3301 if (!context->streams[i]) 3302 continue; 3303 /* 3304 * TODO: add a function to read AMD VSDB bits and set 3305 * crtc_sync_master.multi_sync_enabled flag 3306 * For now it's set to false 3307 */ 3308 set_multisync_trigger_params(context->streams[i]); 3309 } 3310 set_master_stream(context->streams, context->stream_count); 3311 } 3312 3313 static struct dc_stream_state * 3314 create_stream_for_sink(struct amdgpu_dm_connector *aconnector, 3315 const struct drm_display_mode *drm_mode, 3316 const struct dm_connector_state *dm_state, 3317 const struct dc_stream_state *old_stream) 3318 { 3319 struct drm_display_mode *preferred_mode = NULL; 3320 struct drm_connector *drm_connector; 3321 struct dc_stream_state *stream = NULL; 3322 struct drm_display_mode mode = *drm_mode; 3323 bool native_mode_found = false; 3324 bool scale = dm_state ? (dm_state->scaling != RMX_OFF) : false; 3325 int mode_refresh; 3326 int preferred_refresh = 0; 3327 3328 struct dc_sink *sink = NULL; 3329 if (aconnector == NULL) { 3330 DRM_ERROR("aconnector is NULL!\n"); 3331 return stream; 3332 } 3333 3334 drm_connector = &aconnector->base; 3335 3336 if (!aconnector->dc_sink) { 3337 sink = create_fake_sink(aconnector); 3338 if (!sink) 3339 return stream; 3340 } else { 3341 sink = aconnector->dc_sink; 3342 dc_sink_retain(sink); 3343 } 3344 3345 stream = dc_create_stream_for_sink(sink); 3346 3347 if (stream == NULL) { 3348 DRM_ERROR("Failed to create stream for sink!\n"); 3349 goto finish; 3350 } 3351 3352 stream->dm_stream_context = aconnector; 3353 3354 list_for_each_entry(preferred_mode, &aconnector->base.modes, head) { 3355 /* Search for preferred mode */ 3356 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) { 3357 native_mode_found = true; 3358 break; 3359 } 3360 } 3361 if (!native_mode_found) 3362 preferred_mode = list_first_entry_or_null( 3363 &aconnector->base.modes, 3364 struct drm_display_mode, 3365 head); 3366 3367 mode_refresh = drm_mode_vrefresh(&mode); 3368 3369 if (preferred_mode == NULL) { 3370 /* 3371 * This may not be an error, the use case is when we have no 3372 * usermode calls to reset and set mode upon hotplug. In this 3373 * case, we call set mode ourselves to restore the previous mode 3374 * and the modelist may not be filled in in time. 3375 */ 3376 DRM_DEBUG_DRIVER("No preferred mode found\n"); 3377 } else { 3378 decide_crtc_timing_for_drm_display_mode( 3379 &mode, preferred_mode, 3380 dm_state ? (dm_state->scaling != RMX_OFF) : false); 3381 preferred_refresh = drm_mode_vrefresh(preferred_mode); 3382 } 3383 3384 if (!dm_state) 3385 drm_mode_set_crtcinfo(&mode, 0); 3386 3387 /* 3388 * If scaling is enabled and refresh rate didn't change 3389 * we copy the vic and polarities of the old timings 3390 */ 3391 if (!scale || mode_refresh != preferred_refresh) 3392 fill_stream_properties_from_drm_display_mode(stream, 3393 &mode, &aconnector->base, NULL); 3394 else 3395 fill_stream_properties_from_drm_display_mode(stream, 3396 &mode, &aconnector->base, old_stream); 3397 3398 update_stream_scaling_settings(&mode, dm_state, stream); 3399 3400 fill_audio_info( 3401 &stream->audio_info, 3402 drm_connector, 3403 sink); 3404 3405 update_stream_signal(stream, sink); 3406 3407 finish: 3408 dc_sink_release(sink); 3409 3410 return stream; 3411 } 3412 3413 static void amdgpu_dm_crtc_destroy(struct drm_crtc *crtc) 3414 { 3415 drm_crtc_cleanup(crtc); 3416 kfree(crtc); 3417 } 3418 3419 static void dm_crtc_destroy_state(struct drm_crtc *crtc, 3420 struct drm_crtc_state *state) 3421 { 3422 struct dm_crtc_state *cur = to_dm_crtc_state(state); 3423 3424 /* TODO Destroy dc_stream objects are stream object is flattened */ 3425 if (cur->stream) 3426 dc_stream_release(cur->stream); 3427 3428 3429 __drm_atomic_helper_crtc_destroy_state(state); 3430 3431 3432 kfree(state); 3433 } 3434 3435 static void dm_crtc_reset_state(struct drm_crtc *crtc) 3436 { 3437 struct dm_crtc_state *state; 3438 3439 if (crtc->state) 3440 dm_crtc_destroy_state(crtc, crtc->state); 3441 3442 state = kzalloc(sizeof(*state), GFP_KERNEL); 3443 if (WARN_ON(!state)) 3444 return; 3445 3446 crtc->state = &state->base; 3447 crtc->state->crtc = crtc; 3448 3449 } 3450 3451 static struct drm_crtc_state * 3452 dm_crtc_duplicate_state(struct drm_crtc *crtc) 3453 { 3454 struct dm_crtc_state *state, *cur; 3455 3456 cur = to_dm_crtc_state(crtc->state); 3457 3458 if (WARN_ON(!crtc->state)) 3459 return NULL; 3460 3461 state = kzalloc(sizeof(*state), GFP_KERNEL); 3462 if (!state) 3463 return NULL; 3464 3465 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base); 3466 3467 if (cur->stream) { 3468 state->stream = cur->stream; 3469 dc_stream_retain(state->stream); 3470 } 3471 3472 state->active_planes = cur->active_planes; 3473 state->interrupts_enabled = cur->interrupts_enabled; 3474 state->vrr_params = cur->vrr_params; 3475 state->vrr_infopacket = cur->vrr_infopacket; 3476 state->abm_level = cur->abm_level; 3477 state->vrr_supported = cur->vrr_supported; 3478 state->freesync_config = cur->freesync_config; 3479 state->crc_enabled = cur->crc_enabled; 3480 3481 /* TODO Duplicate dc_stream after objects are stream object is flattened */ 3482 3483 return &state->base; 3484 } 3485 3486 static inline int dm_set_vupdate_irq(struct drm_crtc *crtc, bool enable) 3487 { 3488 enum dc_irq_source irq_source; 3489 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 3490 struct amdgpu_device *adev = crtc->dev->dev_private; 3491 int rc; 3492 3493 irq_source = IRQ_TYPE_VUPDATE + acrtc->otg_inst; 3494 3495 rc = dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY; 3496 3497 DRM_DEBUG_DRIVER("crtc %d - vupdate irq %sabling: r=%d\n", 3498 acrtc->crtc_id, enable ? "en" : "dis", rc); 3499 return rc; 3500 } 3501 3502 static inline int dm_set_vblank(struct drm_crtc *crtc, bool enable) 3503 { 3504 enum dc_irq_source irq_source; 3505 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 3506 struct amdgpu_device *adev = crtc->dev->dev_private; 3507 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(crtc->state); 3508 int rc = 0; 3509 3510 if (enable) { 3511 /* vblank irq on -> Only need vupdate irq in vrr mode */ 3512 if (amdgpu_dm_vrr_active(acrtc_state)) 3513 rc = dm_set_vupdate_irq(crtc, true); 3514 } else { 3515 /* vblank irq off -> vupdate irq off */ 3516 rc = dm_set_vupdate_irq(crtc, false); 3517 } 3518 3519 if (rc) 3520 return rc; 3521 3522 irq_source = IRQ_TYPE_VBLANK + acrtc->otg_inst; 3523 return dc_interrupt_set(adev->dm.dc, irq_source, enable) ? 0 : -EBUSY; 3524 } 3525 3526 static int dm_enable_vblank(struct drm_crtc *crtc) 3527 { 3528 return dm_set_vblank(crtc, true); 3529 } 3530 3531 static void dm_disable_vblank(struct drm_crtc *crtc) 3532 { 3533 dm_set_vblank(crtc, false); 3534 } 3535 3536 /* Implemented only the options currently availible for the driver */ 3537 static const struct drm_crtc_funcs amdgpu_dm_crtc_funcs = { 3538 .reset = dm_crtc_reset_state, 3539 .destroy = amdgpu_dm_crtc_destroy, 3540 .gamma_set = drm_atomic_helper_legacy_gamma_set, 3541 .set_config = drm_atomic_helper_set_config, 3542 .page_flip = drm_atomic_helper_page_flip, 3543 .atomic_duplicate_state = dm_crtc_duplicate_state, 3544 .atomic_destroy_state = dm_crtc_destroy_state, 3545 .set_crc_source = amdgpu_dm_crtc_set_crc_source, 3546 .verify_crc_source = amdgpu_dm_crtc_verify_crc_source, 3547 .enable_vblank = dm_enable_vblank, 3548 .disable_vblank = dm_disable_vblank, 3549 }; 3550 3551 static enum drm_connector_status 3552 amdgpu_dm_connector_detect(struct drm_connector *connector, bool force) 3553 { 3554 bool connected; 3555 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 3556 3557 /* 3558 * Notes: 3559 * 1. This interface is NOT called in context of HPD irq. 3560 * 2. This interface *is called* in context of user-mode ioctl. Which 3561 * makes it a bad place for *any* MST-related activity. 3562 */ 3563 3564 if (aconnector->base.force == DRM_FORCE_UNSPECIFIED && 3565 !aconnector->fake_enable) 3566 connected = (aconnector->dc_sink != NULL); 3567 else 3568 connected = (aconnector->base.force == DRM_FORCE_ON); 3569 3570 return (connected ? connector_status_connected : 3571 connector_status_disconnected); 3572 } 3573 3574 int amdgpu_dm_connector_atomic_set_property(struct drm_connector *connector, 3575 struct drm_connector_state *connector_state, 3576 struct drm_property *property, 3577 uint64_t val) 3578 { 3579 struct drm_device *dev = connector->dev; 3580 struct amdgpu_device *adev = dev->dev_private; 3581 struct dm_connector_state *dm_old_state = 3582 to_dm_connector_state(connector->state); 3583 struct dm_connector_state *dm_new_state = 3584 to_dm_connector_state(connector_state); 3585 3586 int ret = -EINVAL; 3587 3588 if (property == dev->mode_config.scaling_mode_property) { 3589 enum amdgpu_rmx_type rmx_type; 3590 3591 switch (val) { 3592 case DRM_MODE_SCALE_CENTER: 3593 rmx_type = RMX_CENTER; 3594 break; 3595 case DRM_MODE_SCALE_ASPECT: 3596 rmx_type = RMX_ASPECT; 3597 break; 3598 case DRM_MODE_SCALE_FULLSCREEN: 3599 rmx_type = RMX_FULL; 3600 break; 3601 case DRM_MODE_SCALE_NONE: 3602 default: 3603 rmx_type = RMX_OFF; 3604 break; 3605 } 3606 3607 if (dm_old_state->scaling == rmx_type) 3608 return 0; 3609 3610 dm_new_state->scaling = rmx_type; 3611 ret = 0; 3612 } else if (property == adev->mode_info.underscan_hborder_property) { 3613 dm_new_state->underscan_hborder = val; 3614 ret = 0; 3615 } else if (property == adev->mode_info.underscan_vborder_property) { 3616 dm_new_state->underscan_vborder = val; 3617 ret = 0; 3618 } else if (property == adev->mode_info.underscan_property) { 3619 dm_new_state->underscan_enable = val; 3620 ret = 0; 3621 } else if (property == adev->mode_info.max_bpc_property) { 3622 dm_new_state->max_bpc = val; 3623 ret = 0; 3624 } else if (property == adev->mode_info.abm_level_property) { 3625 dm_new_state->abm_level = val; 3626 ret = 0; 3627 } 3628 3629 return ret; 3630 } 3631 3632 int amdgpu_dm_connector_atomic_get_property(struct drm_connector *connector, 3633 const struct drm_connector_state *state, 3634 struct drm_property *property, 3635 uint64_t *val) 3636 { 3637 struct drm_device *dev = connector->dev; 3638 struct amdgpu_device *adev = dev->dev_private; 3639 struct dm_connector_state *dm_state = 3640 to_dm_connector_state(state); 3641 int ret = -EINVAL; 3642 3643 if (property == dev->mode_config.scaling_mode_property) { 3644 switch (dm_state->scaling) { 3645 case RMX_CENTER: 3646 *val = DRM_MODE_SCALE_CENTER; 3647 break; 3648 case RMX_ASPECT: 3649 *val = DRM_MODE_SCALE_ASPECT; 3650 break; 3651 case RMX_FULL: 3652 *val = DRM_MODE_SCALE_FULLSCREEN; 3653 break; 3654 case RMX_OFF: 3655 default: 3656 *val = DRM_MODE_SCALE_NONE; 3657 break; 3658 } 3659 ret = 0; 3660 } else if (property == adev->mode_info.underscan_hborder_property) { 3661 *val = dm_state->underscan_hborder; 3662 ret = 0; 3663 } else if (property == adev->mode_info.underscan_vborder_property) { 3664 *val = dm_state->underscan_vborder; 3665 ret = 0; 3666 } else if (property == adev->mode_info.underscan_property) { 3667 *val = dm_state->underscan_enable; 3668 ret = 0; 3669 } else if (property == adev->mode_info.max_bpc_property) { 3670 *val = dm_state->max_bpc; 3671 ret = 0; 3672 } else if (property == adev->mode_info.abm_level_property) { 3673 *val = dm_state->abm_level; 3674 ret = 0; 3675 } 3676 3677 return ret; 3678 } 3679 3680 static void amdgpu_dm_connector_destroy(struct drm_connector *connector) 3681 { 3682 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 3683 const struct dc_link *link = aconnector->dc_link; 3684 struct amdgpu_device *adev = connector->dev->dev_private; 3685 struct amdgpu_display_manager *dm = &adev->dm; 3686 3687 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) ||\ 3688 defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) 3689 3690 if ((link->connector_signal & (SIGNAL_TYPE_EDP | SIGNAL_TYPE_LVDS)) && 3691 link->type != dc_connection_none && 3692 dm->backlight_dev) { 3693 backlight_device_unregister(dm->backlight_dev); 3694 dm->backlight_dev = NULL; 3695 } 3696 #endif 3697 3698 if (aconnector->dc_em_sink) 3699 dc_sink_release(aconnector->dc_em_sink); 3700 aconnector->dc_em_sink = NULL; 3701 if (aconnector->dc_sink) 3702 dc_sink_release(aconnector->dc_sink); 3703 aconnector->dc_sink = NULL; 3704 3705 drm_dp_cec_unregister_connector(&aconnector->dm_dp_aux.aux); 3706 drm_connector_unregister(connector); 3707 drm_connector_cleanup(connector); 3708 kfree(connector); 3709 } 3710 3711 void amdgpu_dm_connector_funcs_reset(struct drm_connector *connector) 3712 { 3713 struct dm_connector_state *state = 3714 to_dm_connector_state(connector->state); 3715 3716 if (connector->state) 3717 __drm_atomic_helper_connector_destroy_state(connector->state); 3718 3719 kfree(state); 3720 3721 state = kzalloc(sizeof(*state), GFP_KERNEL); 3722 3723 if (state) { 3724 state->scaling = RMX_OFF; 3725 state->underscan_enable = false; 3726 state->underscan_hborder = 0; 3727 state->underscan_vborder = 0; 3728 state->max_bpc = 8; 3729 3730 __drm_atomic_helper_connector_reset(connector, &state->base); 3731 } 3732 } 3733 3734 struct drm_connector_state * 3735 amdgpu_dm_connector_atomic_duplicate_state(struct drm_connector *connector) 3736 { 3737 struct dm_connector_state *state = 3738 to_dm_connector_state(connector->state); 3739 3740 struct dm_connector_state *new_state = 3741 kmemdup(state, sizeof(*state), GFP_KERNEL); 3742 3743 if (!new_state) 3744 return NULL; 3745 3746 __drm_atomic_helper_connector_duplicate_state(connector, &new_state->base); 3747 3748 new_state->freesync_capable = state->freesync_capable; 3749 new_state->abm_level = state->abm_level; 3750 new_state->scaling = state->scaling; 3751 new_state->underscan_enable = state->underscan_enable; 3752 new_state->underscan_hborder = state->underscan_hborder; 3753 new_state->underscan_vborder = state->underscan_vborder; 3754 new_state->max_bpc = state->max_bpc; 3755 3756 return &new_state->base; 3757 } 3758 3759 static const struct drm_connector_funcs amdgpu_dm_connector_funcs = { 3760 .reset = amdgpu_dm_connector_funcs_reset, 3761 .detect = amdgpu_dm_connector_detect, 3762 .fill_modes = drm_helper_probe_single_connector_modes, 3763 .destroy = amdgpu_dm_connector_destroy, 3764 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state, 3765 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 3766 .atomic_set_property = amdgpu_dm_connector_atomic_set_property, 3767 .atomic_get_property = amdgpu_dm_connector_atomic_get_property 3768 }; 3769 3770 static int get_modes(struct drm_connector *connector) 3771 { 3772 return amdgpu_dm_connector_get_modes(connector); 3773 } 3774 3775 static void create_eml_sink(struct amdgpu_dm_connector *aconnector) 3776 { 3777 struct dc_sink_init_data init_params = { 3778 .link = aconnector->dc_link, 3779 .sink_signal = SIGNAL_TYPE_VIRTUAL 3780 }; 3781 struct edid *edid; 3782 3783 if (!aconnector->base.edid_blob_ptr) { 3784 DRM_ERROR("No EDID firmware found on connector: %s ,forcing to OFF!\n", 3785 aconnector->base.name); 3786 3787 aconnector->base.force = DRM_FORCE_OFF; 3788 aconnector->base.override_edid = false; 3789 return; 3790 } 3791 3792 edid = (struct edid *) aconnector->base.edid_blob_ptr->data; 3793 3794 aconnector->edid = edid; 3795 3796 aconnector->dc_em_sink = dc_link_add_remote_sink( 3797 aconnector->dc_link, 3798 (uint8_t *)edid, 3799 (edid->extensions + 1) * EDID_LENGTH, 3800 &init_params); 3801 3802 if (aconnector->base.force == DRM_FORCE_ON) { 3803 aconnector->dc_sink = aconnector->dc_link->local_sink ? 3804 aconnector->dc_link->local_sink : 3805 aconnector->dc_em_sink; 3806 dc_sink_retain(aconnector->dc_sink); 3807 } 3808 } 3809 3810 static void handle_edid_mgmt(struct amdgpu_dm_connector *aconnector) 3811 { 3812 struct dc_link *link = (struct dc_link *)aconnector->dc_link; 3813 3814 /* 3815 * In case of headless boot with force on for DP managed connector 3816 * Those settings have to be != 0 to get initial modeset 3817 */ 3818 if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT) { 3819 link->verified_link_cap.lane_count = LANE_COUNT_FOUR; 3820 link->verified_link_cap.link_rate = LINK_RATE_HIGH2; 3821 } 3822 3823 3824 aconnector->base.override_edid = true; 3825 create_eml_sink(aconnector); 3826 } 3827 3828 enum drm_mode_status amdgpu_dm_connector_mode_valid(struct drm_connector *connector, 3829 struct drm_display_mode *mode) 3830 { 3831 int result = MODE_ERROR; 3832 struct dc_sink *dc_sink; 3833 struct amdgpu_device *adev = connector->dev->dev_private; 3834 /* TODO: Unhardcode stream count */ 3835 struct dc_stream_state *stream; 3836 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 3837 enum dc_status dc_result = DC_OK; 3838 3839 if ((mode->flags & DRM_MODE_FLAG_INTERLACE) || 3840 (mode->flags & DRM_MODE_FLAG_DBLSCAN)) 3841 return result; 3842 3843 /* 3844 * Only run this the first time mode_valid is called to initilialize 3845 * EDID mgmt 3846 */ 3847 if (aconnector->base.force != DRM_FORCE_UNSPECIFIED && 3848 !aconnector->dc_em_sink) 3849 handle_edid_mgmt(aconnector); 3850 3851 dc_sink = to_amdgpu_dm_connector(connector)->dc_sink; 3852 3853 if (dc_sink == NULL) { 3854 DRM_ERROR("dc_sink is NULL!\n"); 3855 goto fail; 3856 } 3857 3858 stream = create_stream_for_sink(aconnector, mode, NULL, NULL); 3859 if (stream == NULL) { 3860 DRM_ERROR("Failed to create stream for sink!\n"); 3861 goto fail; 3862 } 3863 3864 dc_result = dc_validate_stream(adev->dm.dc, stream); 3865 3866 if (dc_result == DC_OK) 3867 result = MODE_OK; 3868 else 3869 DRM_DEBUG_KMS("Mode %dx%d (clk %d) failed DC validation with error %d\n", 3870 mode->vdisplay, 3871 mode->hdisplay, 3872 mode->clock, 3873 dc_result); 3874 3875 dc_stream_release(stream); 3876 3877 fail: 3878 /* TODO: error handling*/ 3879 return result; 3880 } 3881 3882 static const struct drm_connector_helper_funcs 3883 amdgpu_dm_connector_helper_funcs = { 3884 /* 3885 * If hotplugging a second bigger display in FB Con mode, bigger resolution 3886 * modes will be filtered by drm_mode_validate_size(), and those modes 3887 * are missing after user start lightdm. So we need to renew modes list. 3888 * in get_modes call back, not just return the modes count 3889 */ 3890 .get_modes = get_modes, 3891 .mode_valid = amdgpu_dm_connector_mode_valid, 3892 }; 3893 3894 static void dm_crtc_helper_disable(struct drm_crtc *crtc) 3895 { 3896 } 3897 3898 static bool does_crtc_have_active_cursor(struct drm_crtc_state *new_crtc_state) 3899 { 3900 struct drm_device *dev = new_crtc_state->crtc->dev; 3901 struct drm_plane *plane; 3902 3903 drm_for_each_plane_mask(plane, dev, new_crtc_state->plane_mask) { 3904 if (plane->type == DRM_PLANE_TYPE_CURSOR) 3905 return true; 3906 } 3907 3908 return false; 3909 } 3910 3911 static int count_crtc_active_planes(struct drm_crtc_state *new_crtc_state) 3912 { 3913 struct drm_atomic_state *state = new_crtc_state->state; 3914 struct drm_plane *plane; 3915 int num_active = 0; 3916 3917 drm_for_each_plane_mask(plane, state->dev, new_crtc_state->plane_mask) { 3918 struct drm_plane_state *new_plane_state; 3919 3920 /* Cursor planes are "fake". */ 3921 if (plane->type == DRM_PLANE_TYPE_CURSOR) 3922 continue; 3923 3924 new_plane_state = drm_atomic_get_new_plane_state(state, plane); 3925 3926 if (!new_plane_state) { 3927 /* 3928 * The plane is enable on the CRTC and hasn't changed 3929 * state. This means that it previously passed 3930 * validation and is therefore enabled. 3931 */ 3932 num_active += 1; 3933 continue; 3934 } 3935 3936 /* We need a framebuffer to be considered enabled. */ 3937 num_active += (new_plane_state->fb != NULL); 3938 } 3939 3940 return num_active; 3941 } 3942 3943 /* 3944 * Sets whether interrupts should be enabled on a specific CRTC. 3945 * We require that the stream be enabled and that there exist active 3946 * DC planes on the stream. 3947 */ 3948 static void 3949 dm_update_crtc_interrupt_state(struct drm_crtc *crtc, 3950 struct drm_crtc_state *new_crtc_state) 3951 { 3952 struct dm_crtc_state *dm_new_crtc_state = 3953 to_dm_crtc_state(new_crtc_state); 3954 3955 dm_new_crtc_state->active_planes = 0; 3956 dm_new_crtc_state->interrupts_enabled = false; 3957 3958 if (!dm_new_crtc_state->stream) 3959 return; 3960 3961 dm_new_crtc_state->active_planes = 3962 count_crtc_active_planes(new_crtc_state); 3963 3964 dm_new_crtc_state->interrupts_enabled = 3965 dm_new_crtc_state->active_planes > 0; 3966 } 3967 3968 static int dm_crtc_helper_atomic_check(struct drm_crtc *crtc, 3969 struct drm_crtc_state *state) 3970 { 3971 struct amdgpu_device *adev = crtc->dev->dev_private; 3972 struct dc *dc = adev->dm.dc; 3973 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(state); 3974 int ret = -EINVAL; 3975 3976 /* 3977 * Update interrupt state for the CRTC. This needs to happen whenever 3978 * the CRTC has changed or whenever any of its planes have changed. 3979 * Atomic check satisfies both of these requirements since the CRTC 3980 * is added to the state by DRM during drm_atomic_helper_check_planes. 3981 */ 3982 dm_update_crtc_interrupt_state(crtc, state); 3983 3984 if (unlikely(!dm_crtc_state->stream && 3985 modeset_required(state, NULL, dm_crtc_state->stream))) { 3986 WARN_ON(1); 3987 return ret; 3988 } 3989 3990 /* In some use cases, like reset, no stream is attached */ 3991 if (!dm_crtc_state->stream) 3992 return 0; 3993 3994 /* 3995 * We want at least one hardware plane enabled to use 3996 * the stream with a cursor enabled. 3997 */ 3998 if (state->enable && state->active && 3999 does_crtc_have_active_cursor(state) && 4000 dm_crtc_state->active_planes == 0) 4001 return -EINVAL; 4002 4003 if (dc_validate_stream(dc, dm_crtc_state->stream) == DC_OK) 4004 return 0; 4005 4006 return ret; 4007 } 4008 4009 static bool dm_crtc_helper_mode_fixup(struct drm_crtc *crtc, 4010 const struct drm_display_mode *mode, 4011 struct drm_display_mode *adjusted_mode) 4012 { 4013 return true; 4014 } 4015 4016 static const struct drm_crtc_helper_funcs amdgpu_dm_crtc_helper_funcs = { 4017 .disable = dm_crtc_helper_disable, 4018 .atomic_check = dm_crtc_helper_atomic_check, 4019 .mode_fixup = dm_crtc_helper_mode_fixup 4020 }; 4021 4022 static void dm_encoder_helper_disable(struct drm_encoder *encoder) 4023 { 4024 4025 } 4026 4027 static int dm_encoder_helper_atomic_check(struct drm_encoder *encoder, 4028 struct drm_crtc_state *crtc_state, 4029 struct drm_connector_state *conn_state) 4030 { 4031 return 0; 4032 } 4033 4034 const struct drm_encoder_helper_funcs amdgpu_dm_encoder_helper_funcs = { 4035 .disable = dm_encoder_helper_disable, 4036 .atomic_check = dm_encoder_helper_atomic_check 4037 }; 4038 4039 static void dm_drm_plane_reset(struct drm_plane *plane) 4040 { 4041 struct dm_plane_state *amdgpu_state = NULL; 4042 4043 if (plane->state) 4044 plane->funcs->atomic_destroy_state(plane, plane->state); 4045 4046 amdgpu_state = kzalloc(sizeof(*amdgpu_state), GFP_KERNEL); 4047 WARN_ON(amdgpu_state == NULL); 4048 4049 if (amdgpu_state) 4050 __drm_atomic_helper_plane_reset(plane, &amdgpu_state->base); 4051 } 4052 4053 static struct drm_plane_state * 4054 dm_drm_plane_duplicate_state(struct drm_plane *plane) 4055 { 4056 struct dm_plane_state *dm_plane_state, *old_dm_plane_state; 4057 4058 old_dm_plane_state = to_dm_plane_state(plane->state); 4059 dm_plane_state = kzalloc(sizeof(*dm_plane_state), GFP_KERNEL); 4060 if (!dm_plane_state) 4061 return NULL; 4062 4063 __drm_atomic_helper_plane_duplicate_state(plane, &dm_plane_state->base); 4064 4065 if (old_dm_plane_state->dc_state) { 4066 dm_plane_state->dc_state = old_dm_plane_state->dc_state; 4067 dc_plane_state_retain(dm_plane_state->dc_state); 4068 } 4069 4070 return &dm_plane_state->base; 4071 } 4072 4073 void dm_drm_plane_destroy_state(struct drm_plane *plane, 4074 struct drm_plane_state *state) 4075 { 4076 struct dm_plane_state *dm_plane_state = to_dm_plane_state(state); 4077 4078 if (dm_plane_state->dc_state) 4079 dc_plane_state_release(dm_plane_state->dc_state); 4080 4081 drm_atomic_helper_plane_destroy_state(plane, state); 4082 } 4083 4084 static const struct drm_plane_funcs dm_plane_funcs = { 4085 .update_plane = drm_atomic_helper_update_plane, 4086 .disable_plane = drm_atomic_helper_disable_plane, 4087 .destroy = drm_primary_helper_destroy, 4088 .reset = dm_drm_plane_reset, 4089 .atomic_duplicate_state = dm_drm_plane_duplicate_state, 4090 .atomic_destroy_state = dm_drm_plane_destroy_state, 4091 }; 4092 4093 static int dm_plane_helper_prepare_fb(struct drm_plane *plane, 4094 struct drm_plane_state *new_state) 4095 { 4096 struct amdgpu_framebuffer *afb; 4097 struct drm_gem_object *obj; 4098 struct amdgpu_device *adev; 4099 struct amdgpu_bo *rbo; 4100 struct dm_plane_state *dm_plane_state_new, *dm_plane_state_old; 4101 uint64_t tiling_flags; 4102 uint32_t domain; 4103 int r; 4104 4105 dm_plane_state_old = to_dm_plane_state(plane->state); 4106 dm_plane_state_new = to_dm_plane_state(new_state); 4107 4108 if (!new_state->fb) { 4109 DRM_DEBUG_DRIVER("No FB bound\n"); 4110 return 0; 4111 } 4112 4113 afb = to_amdgpu_framebuffer(new_state->fb); 4114 obj = new_state->fb->obj[0]; 4115 rbo = gem_to_amdgpu_bo(obj); 4116 adev = amdgpu_ttm_adev(rbo->tbo.bdev); 4117 r = amdgpu_bo_reserve(rbo, false); 4118 if (unlikely(r != 0)) 4119 return r; 4120 4121 if (plane->type != DRM_PLANE_TYPE_CURSOR) 4122 domain = amdgpu_display_supported_domains(adev); 4123 else 4124 domain = AMDGPU_GEM_DOMAIN_VRAM; 4125 4126 r = amdgpu_bo_pin(rbo, domain); 4127 if (unlikely(r != 0)) { 4128 if (r != -ERESTARTSYS) 4129 DRM_ERROR("Failed to pin framebuffer with error %d\n", r); 4130 amdgpu_bo_unreserve(rbo); 4131 return r; 4132 } 4133 4134 r = amdgpu_ttm_alloc_gart(&rbo->tbo); 4135 if (unlikely(r != 0)) { 4136 amdgpu_bo_unpin(rbo); 4137 amdgpu_bo_unreserve(rbo); 4138 DRM_ERROR("%p bind failed\n", rbo); 4139 return r; 4140 } 4141 4142 amdgpu_bo_get_tiling_flags(rbo, &tiling_flags); 4143 4144 amdgpu_bo_unreserve(rbo); 4145 4146 afb->address = amdgpu_bo_gpu_offset(rbo); 4147 4148 amdgpu_bo_ref(rbo); 4149 4150 if (dm_plane_state_new->dc_state && 4151 dm_plane_state_old->dc_state != dm_plane_state_new->dc_state) { 4152 struct dc_plane_state *plane_state = dm_plane_state_new->dc_state; 4153 4154 fill_plane_buffer_attributes( 4155 adev, afb, plane_state->format, plane_state->rotation, 4156 tiling_flags, &plane_state->tiling_info, 4157 &plane_state->plane_size, &plane_state->dcc, 4158 &plane_state->address); 4159 } 4160 4161 return 0; 4162 } 4163 4164 static void dm_plane_helper_cleanup_fb(struct drm_plane *plane, 4165 struct drm_plane_state *old_state) 4166 { 4167 struct amdgpu_bo *rbo; 4168 int r; 4169 4170 if (!old_state->fb) 4171 return; 4172 4173 rbo = gem_to_amdgpu_bo(old_state->fb->obj[0]); 4174 r = amdgpu_bo_reserve(rbo, false); 4175 if (unlikely(r)) { 4176 DRM_ERROR("failed to reserve rbo before unpin\n"); 4177 return; 4178 } 4179 4180 amdgpu_bo_unpin(rbo); 4181 amdgpu_bo_unreserve(rbo); 4182 amdgpu_bo_unref(&rbo); 4183 } 4184 4185 static int dm_plane_atomic_check(struct drm_plane *plane, 4186 struct drm_plane_state *state) 4187 { 4188 struct amdgpu_device *adev = plane->dev->dev_private; 4189 struct dc *dc = adev->dm.dc; 4190 struct dm_plane_state *dm_plane_state; 4191 struct dc_scaling_info scaling_info; 4192 int ret; 4193 4194 dm_plane_state = to_dm_plane_state(state); 4195 4196 if (!dm_plane_state->dc_state) 4197 return 0; 4198 4199 ret = fill_dc_scaling_info(state, &scaling_info); 4200 if (ret) 4201 return ret; 4202 4203 if (dc_validate_plane(dc, dm_plane_state->dc_state) == DC_OK) 4204 return 0; 4205 4206 return -EINVAL; 4207 } 4208 4209 static int dm_plane_atomic_async_check(struct drm_plane *plane, 4210 struct drm_plane_state *new_plane_state) 4211 { 4212 struct drm_plane_state *old_plane_state = 4213 drm_atomic_get_old_plane_state(new_plane_state->state, plane); 4214 4215 /* Only support async updates on cursor planes. */ 4216 if (plane->type != DRM_PLANE_TYPE_CURSOR) 4217 return -EINVAL; 4218 4219 /* 4220 * DRM calls prepare_fb and cleanup_fb on new_plane_state for 4221 * async commits so don't allow fb changes. 4222 */ 4223 if (old_plane_state->fb != new_plane_state->fb) 4224 return -EINVAL; 4225 4226 return 0; 4227 } 4228 4229 static void dm_plane_atomic_async_update(struct drm_plane *plane, 4230 struct drm_plane_state *new_state) 4231 { 4232 struct drm_plane_state *old_state = 4233 drm_atomic_get_old_plane_state(new_state->state, plane); 4234 4235 if (plane->state->fb != new_state->fb) 4236 drm_atomic_set_fb_for_plane(plane->state, new_state->fb); 4237 4238 plane->state->src_x = new_state->src_x; 4239 plane->state->src_y = new_state->src_y; 4240 plane->state->src_w = new_state->src_w; 4241 plane->state->src_h = new_state->src_h; 4242 plane->state->crtc_x = new_state->crtc_x; 4243 plane->state->crtc_y = new_state->crtc_y; 4244 plane->state->crtc_w = new_state->crtc_w; 4245 plane->state->crtc_h = new_state->crtc_h; 4246 4247 handle_cursor_update(plane, old_state); 4248 } 4249 4250 static const struct drm_plane_helper_funcs dm_plane_helper_funcs = { 4251 .prepare_fb = dm_plane_helper_prepare_fb, 4252 .cleanup_fb = dm_plane_helper_cleanup_fb, 4253 .atomic_check = dm_plane_atomic_check, 4254 .atomic_async_check = dm_plane_atomic_async_check, 4255 .atomic_async_update = dm_plane_atomic_async_update 4256 }; 4257 4258 /* 4259 * TODO: these are currently initialized to rgb formats only. 4260 * For future use cases we should either initialize them dynamically based on 4261 * plane capabilities, or initialize this array to all formats, so internal drm 4262 * check will succeed, and let DC implement proper check 4263 */ 4264 static const uint32_t rgb_formats[] = { 4265 DRM_FORMAT_XRGB8888, 4266 DRM_FORMAT_ARGB8888, 4267 DRM_FORMAT_RGBA8888, 4268 DRM_FORMAT_XRGB2101010, 4269 DRM_FORMAT_XBGR2101010, 4270 DRM_FORMAT_ARGB2101010, 4271 DRM_FORMAT_ABGR2101010, 4272 DRM_FORMAT_XBGR8888, 4273 DRM_FORMAT_ABGR8888, 4274 DRM_FORMAT_RGB565, 4275 }; 4276 4277 static const uint32_t overlay_formats[] = { 4278 DRM_FORMAT_XRGB8888, 4279 DRM_FORMAT_ARGB8888, 4280 DRM_FORMAT_RGBA8888, 4281 DRM_FORMAT_XBGR8888, 4282 DRM_FORMAT_ABGR8888, 4283 DRM_FORMAT_RGB565 4284 }; 4285 4286 static const u32 cursor_formats[] = { 4287 DRM_FORMAT_ARGB8888 4288 }; 4289 4290 static int get_plane_formats(const struct drm_plane *plane, 4291 const struct dc_plane_cap *plane_cap, 4292 uint32_t *formats, int max_formats) 4293 { 4294 int i, num_formats = 0; 4295 4296 /* 4297 * TODO: Query support for each group of formats directly from 4298 * DC plane caps. This will require adding more formats to the 4299 * caps list. 4300 */ 4301 4302 switch (plane->type) { 4303 case DRM_PLANE_TYPE_PRIMARY: 4304 for (i = 0; i < ARRAY_SIZE(rgb_formats); ++i) { 4305 if (num_formats >= max_formats) 4306 break; 4307 4308 formats[num_formats++] = rgb_formats[i]; 4309 } 4310 4311 if (plane_cap && plane_cap->pixel_format_support.nv12) 4312 formats[num_formats++] = DRM_FORMAT_NV12; 4313 break; 4314 4315 case DRM_PLANE_TYPE_OVERLAY: 4316 for (i = 0; i < ARRAY_SIZE(overlay_formats); ++i) { 4317 if (num_formats >= max_formats) 4318 break; 4319 4320 formats[num_formats++] = overlay_formats[i]; 4321 } 4322 break; 4323 4324 case DRM_PLANE_TYPE_CURSOR: 4325 for (i = 0; i < ARRAY_SIZE(cursor_formats); ++i) { 4326 if (num_formats >= max_formats) 4327 break; 4328 4329 formats[num_formats++] = cursor_formats[i]; 4330 } 4331 break; 4332 } 4333 4334 return num_formats; 4335 } 4336 4337 static int amdgpu_dm_plane_init(struct amdgpu_display_manager *dm, 4338 struct drm_plane *plane, 4339 unsigned long possible_crtcs, 4340 const struct dc_plane_cap *plane_cap) 4341 { 4342 uint32_t formats[32]; 4343 int num_formats; 4344 int res = -EPERM; 4345 4346 num_formats = get_plane_formats(plane, plane_cap, formats, 4347 ARRAY_SIZE(formats)); 4348 4349 res = drm_universal_plane_init(dm->adev->ddev, plane, possible_crtcs, 4350 &dm_plane_funcs, formats, num_formats, 4351 NULL, plane->type, NULL); 4352 if (res) 4353 return res; 4354 4355 if (plane->type == DRM_PLANE_TYPE_OVERLAY && 4356 plane_cap && plane_cap->per_pixel_alpha) { 4357 unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) | 4358 BIT(DRM_MODE_BLEND_PREMULTI); 4359 4360 drm_plane_create_alpha_property(plane); 4361 drm_plane_create_blend_mode_property(plane, blend_caps); 4362 } 4363 4364 if (plane->type == DRM_PLANE_TYPE_PRIMARY && 4365 plane_cap && plane_cap->pixel_format_support.nv12) { 4366 /* This only affects YUV formats. */ 4367 drm_plane_create_color_properties( 4368 plane, 4369 BIT(DRM_COLOR_YCBCR_BT601) | 4370 BIT(DRM_COLOR_YCBCR_BT709), 4371 BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) | 4372 BIT(DRM_COLOR_YCBCR_FULL_RANGE), 4373 DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE); 4374 } 4375 4376 drm_plane_helper_add(plane, &dm_plane_helper_funcs); 4377 4378 /* Create (reset) the plane state */ 4379 if (plane->funcs->reset) 4380 plane->funcs->reset(plane); 4381 4382 return 0; 4383 } 4384 4385 static int amdgpu_dm_crtc_init(struct amdgpu_display_manager *dm, 4386 struct drm_plane *plane, 4387 uint32_t crtc_index) 4388 { 4389 struct amdgpu_crtc *acrtc = NULL; 4390 struct drm_plane *cursor_plane; 4391 4392 int res = -ENOMEM; 4393 4394 cursor_plane = kzalloc(sizeof(*cursor_plane), GFP_KERNEL); 4395 if (!cursor_plane) 4396 goto fail; 4397 4398 cursor_plane->type = DRM_PLANE_TYPE_CURSOR; 4399 res = amdgpu_dm_plane_init(dm, cursor_plane, 0, NULL); 4400 4401 acrtc = kzalloc(sizeof(struct amdgpu_crtc), GFP_KERNEL); 4402 if (!acrtc) 4403 goto fail; 4404 4405 res = drm_crtc_init_with_planes( 4406 dm->ddev, 4407 &acrtc->base, 4408 plane, 4409 cursor_plane, 4410 &amdgpu_dm_crtc_funcs, NULL); 4411 4412 if (res) 4413 goto fail; 4414 4415 drm_crtc_helper_add(&acrtc->base, &amdgpu_dm_crtc_helper_funcs); 4416 4417 /* Create (reset) the plane state */ 4418 if (acrtc->base.funcs->reset) 4419 acrtc->base.funcs->reset(&acrtc->base); 4420 4421 acrtc->max_cursor_width = dm->adev->dm.dc->caps.max_cursor_size; 4422 acrtc->max_cursor_height = dm->adev->dm.dc->caps.max_cursor_size; 4423 4424 acrtc->crtc_id = crtc_index; 4425 acrtc->base.enabled = false; 4426 acrtc->otg_inst = -1; 4427 4428 dm->adev->mode_info.crtcs[crtc_index] = acrtc; 4429 drm_crtc_enable_color_mgmt(&acrtc->base, MAX_COLOR_LUT_ENTRIES, 4430 true, MAX_COLOR_LUT_ENTRIES); 4431 drm_mode_crtc_set_gamma_size(&acrtc->base, MAX_COLOR_LEGACY_LUT_ENTRIES); 4432 4433 return 0; 4434 4435 fail: 4436 kfree(acrtc); 4437 kfree(cursor_plane); 4438 return res; 4439 } 4440 4441 4442 static int to_drm_connector_type(enum signal_type st) 4443 { 4444 switch (st) { 4445 case SIGNAL_TYPE_HDMI_TYPE_A: 4446 return DRM_MODE_CONNECTOR_HDMIA; 4447 case SIGNAL_TYPE_EDP: 4448 return DRM_MODE_CONNECTOR_eDP; 4449 case SIGNAL_TYPE_LVDS: 4450 return DRM_MODE_CONNECTOR_LVDS; 4451 case SIGNAL_TYPE_RGB: 4452 return DRM_MODE_CONNECTOR_VGA; 4453 case SIGNAL_TYPE_DISPLAY_PORT: 4454 case SIGNAL_TYPE_DISPLAY_PORT_MST: 4455 return DRM_MODE_CONNECTOR_DisplayPort; 4456 case SIGNAL_TYPE_DVI_DUAL_LINK: 4457 case SIGNAL_TYPE_DVI_SINGLE_LINK: 4458 return DRM_MODE_CONNECTOR_DVID; 4459 case SIGNAL_TYPE_VIRTUAL: 4460 return DRM_MODE_CONNECTOR_VIRTUAL; 4461 4462 default: 4463 return DRM_MODE_CONNECTOR_Unknown; 4464 } 4465 } 4466 4467 static struct drm_encoder *amdgpu_dm_connector_to_encoder(struct drm_connector *connector) 4468 { 4469 return drm_encoder_find(connector->dev, NULL, connector->encoder_ids[0]); 4470 } 4471 4472 static void amdgpu_dm_get_native_mode(struct drm_connector *connector) 4473 { 4474 struct drm_encoder *encoder; 4475 struct amdgpu_encoder *amdgpu_encoder; 4476 4477 encoder = amdgpu_dm_connector_to_encoder(connector); 4478 4479 if (encoder == NULL) 4480 return; 4481 4482 amdgpu_encoder = to_amdgpu_encoder(encoder); 4483 4484 amdgpu_encoder->native_mode.clock = 0; 4485 4486 if (!list_empty(&connector->probed_modes)) { 4487 struct drm_display_mode *preferred_mode = NULL; 4488 4489 list_for_each_entry(preferred_mode, 4490 &connector->probed_modes, 4491 head) { 4492 if (preferred_mode->type & DRM_MODE_TYPE_PREFERRED) 4493 amdgpu_encoder->native_mode = *preferred_mode; 4494 4495 break; 4496 } 4497 4498 } 4499 } 4500 4501 static struct drm_display_mode * 4502 amdgpu_dm_create_common_mode(struct drm_encoder *encoder, 4503 char *name, 4504 int hdisplay, int vdisplay) 4505 { 4506 struct drm_device *dev = encoder->dev; 4507 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder); 4508 struct drm_display_mode *mode = NULL; 4509 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode; 4510 4511 mode = drm_mode_duplicate(dev, native_mode); 4512 4513 if (mode == NULL) 4514 return NULL; 4515 4516 mode->hdisplay = hdisplay; 4517 mode->vdisplay = vdisplay; 4518 mode->type &= ~DRM_MODE_TYPE_PREFERRED; 4519 strscpy(mode->name, name, DRM_DISPLAY_MODE_LEN); 4520 4521 return mode; 4522 4523 } 4524 4525 static void amdgpu_dm_connector_add_common_modes(struct drm_encoder *encoder, 4526 struct drm_connector *connector) 4527 { 4528 struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder); 4529 struct drm_display_mode *mode = NULL; 4530 struct drm_display_mode *native_mode = &amdgpu_encoder->native_mode; 4531 struct amdgpu_dm_connector *amdgpu_dm_connector = 4532 to_amdgpu_dm_connector(connector); 4533 int i; 4534 int n; 4535 struct mode_size { 4536 char name[DRM_DISPLAY_MODE_LEN]; 4537 int w; 4538 int h; 4539 } common_modes[] = { 4540 { "640x480", 640, 480}, 4541 { "800x600", 800, 600}, 4542 { "1024x768", 1024, 768}, 4543 { "1280x720", 1280, 720}, 4544 { "1280x800", 1280, 800}, 4545 {"1280x1024", 1280, 1024}, 4546 { "1440x900", 1440, 900}, 4547 {"1680x1050", 1680, 1050}, 4548 {"1600x1200", 1600, 1200}, 4549 {"1920x1080", 1920, 1080}, 4550 {"1920x1200", 1920, 1200} 4551 }; 4552 4553 n = ARRAY_SIZE(common_modes); 4554 4555 for (i = 0; i < n; i++) { 4556 struct drm_display_mode *curmode = NULL; 4557 bool mode_existed = false; 4558 4559 if (common_modes[i].w > native_mode->hdisplay || 4560 common_modes[i].h > native_mode->vdisplay || 4561 (common_modes[i].w == native_mode->hdisplay && 4562 common_modes[i].h == native_mode->vdisplay)) 4563 continue; 4564 4565 list_for_each_entry(curmode, &connector->probed_modes, head) { 4566 if (common_modes[i].w == curmode->hdisplay && 4567 common_modes[i].h == curmode->vdisplay) { 4568 mode_existed = true; 4569 break; 4570 } 4571 } 4572 4573 if (mode_existed) 4574 continue; 4575 4576 mode = amdgpu_dm_create_common_mode(encoder, 4577 common_modes[i].name, common_modes[i].w, 4578 common_modes[i].h); 4579 drm_mode_probed_add(connector, mode); 4580 amdgpu_dm_connector->num_modes++; 4581 } 4582 } 4583 4584 static void amdgpu_dm_connector_ddc_get_modes(struct drm_connector *connector, 4585 struct edid *edid) 4586 { 4587 struct amdgpu_dm_connector *amdgpu_dm_connector = 4588 to_amdgpu_dm_connector(connector); 4589 4590 if (edid) { 4591 /* empty probed_modes */ 4592 INIT_LIST_HEAD(&connector->probed_modes); 4593 amdgpu_dm_connector->num_modes = 4594 drm_add_edid_modes(connector, edid); 4595 4596 amdgpu_dm_get_native_mode(connector); 4597 } else { 4598 amdgpu_dm_connector->num_modes = 0; 4599 } 4600 } 4601 4602 static int amdgpu_dm_connector_get_modes(struct drm_connector *connector) 4603 { 4604 struct amdgpu_dm_connector *amdgpu_dm_connector = 4605 to_amdgpu_dm_connector(connector); 4606 struct drm_encoder *encoder; 4607 struct edid *edid = amdgpu_dm_connector->edid; 4608 4609 encoder = amdgpu_dm_connector_to_encoder(connector); 4610 4611 if (!edid || !drm_edid_is_valid(edid)) { 4612 amdgpu_dm_connector->num_modes = 4613 drm_add_modes_noedid(connector, 640, 480); 4614 } else { 4615 amdgpu_dm_connector_ddc_get_modes(connector, edid); 4616 amdgpu_dm_connector_add_common_modes(encoder, connector); 4617 } 4618 amdgpu_dm_fbc_init(connector); 4619 4620 return amdgpu_dm_connector->num_modes; 4621 } 4622 4623 void amdgpu_dm_connector_init_helper(struct amdgpu_display_manager *dm, 4624 struct amdgpu_dm_connector *aconnector, 4625 int connector_type, 4626 struct dc_link *link, 4627 int link_index) 4628 { 4629 struct amdgpu_device *adev = dm->ddev->dev_private; 4630 4631 aconnector->connector_id = link_index; 4632 aconnector->dc_link = link; 4633 aconnector->base.interlace_allowed = false; 4634 aconnector->base.doublescan_allowed = false; 4635 aconnector->base.stereo_allowed = false; 4636 aconnector->base.dpms = DRM_MODE_DPMS_OFF; 4637 aconnector->hpd.hpd = AMDGPU_HPD_NONE; /* not used */ 4638 mutex_init(&aconnector->hpd_lock); 4639 4640 /* 4641 * configure support HPD hot plug connector_>polled default value is 0 4642 * which means HPD hot plug not supported 4643 */ 4644 switch (connector_type) { 4645 case DRM_MODE_CONNECTOR_HDMIA: 4646 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 4647 aconnector->base.ycbcr_420_allowed = 4648 link->link_enc->features.hdmi_ycbcr420_supported ? true : false; 4649 break; 4650 case DRM_MODE_CONNECTOR_DisplayPort: 4651 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 4652 aconnector->base.ycbcr_420_allowed = 4653 link->link_enc->features.dp_ycbcr420_supported ? true : false; 4654 break; 4655 case DRM_MODE_CONNECTOR_DVID: 4656 aconnector->base.polled = DRM_CONNECTOR_POLL_HPD; 4657 break; 4658 default: 4659 break; 4660 } 4661 4662 drm_object_attach_property(&aconnector->base.base, 4663 dm->ddev->mode_config.scaling_mode_property, 4664 DRM_MODE_SCALE_NONE); 4665 4666 drm_object_attach_property(&aconnector->base.base, 4667 adev->mode_info.underscan_property, 4668 UNDERSCAN_OFF); 4669 drm_object_attach_property(&aconnector->base.base, 4670 adev->mode_info.underscan_hborder_property, 4671 0); 4672 drm_object_attach_property(&aconnector->base.base, 4673 adev->mode_info.underscan_vborder_property, 4674 0); 4675 drm_object_attach_property(&aconnector->base.base, 4676 adev->mode_info.max_bpc_property, 4677 0); 4678 4679 if (connector_type == DRM_MODE_CONNECTOR_eDP && 4680 dc_is_dmcu_initialized(adev->dm.dc)) { 4681 drm_object_attach_property(&aconnector->base.base, 4682 adev->mode_info.abm_level_property, 0); 4683 } 4684 4685 if (connector_type == DRM_MODE_CONNECTOR_HDMIA || 4686 connector_type == DRM_MODE_CONNECTOR_DisplayPort || 4687 connector_type == DRM_MODE_CONNECTOR_eDP) { 4688 drm_connector_attach_vrr_capable_property( 4689 &aconnector->base); 4690 } 4691 } 4692 4693 static int amdgpu_dm_i2c_xfer(struct i2c_adapter *i2c_adap, 4694 struct i2c_msg *msgs, int num) 4695 { 4696 struct amdgpu_i2c_adapter *i2c = i2c_get_adapdata(i2c_adap); 4697 struct ddc_service *ddc_service = i2c->ddc_service; 4698 struct i2c_command cmd; 4699 int i; 4700 int result = -EIO; 4701 4702 cmd.payloads = kcalloc(num, sizeof(struct i2c_payload), GFP_KERNEL); 4703 4704 if (!cmd.payloads) 4705 return result; 4706 4707 cmd.number_of_payloads = num; 4708 cmd.engine = I2C_COMMAND_ENGINE_DEFAULT; 4709 cmd.speed = 100; 4710 4711 for (i = 0; i < num; i++) { 4712 cmd.payloads[i].write = !(msgs[i].flags & I2C_M_RD); 4713 cmd.payloads[i].address = msgs[i].addr; 4714 cmd.payloads[i].length = msgs[i].len; 4715 cmd.payloads[i].data = msgs[i].buf; 4716 } 4717 4718 if (dc_submit_i2c( 4719 ddc_service->ctx->dc, 4720 ddc_service->ddc_pin->hw_info.ddc_channel, 4721 &cmd)) 4722 result = num; 4723 4724 kfree(cmd.payloads); 4725 return result; 4726 } 4727 4728 static u32 amdgpu_dm_i2c_func(struct i2c_adapter *adap) 4729 { 4730 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 4731 } 4732 4733 static const struct i2c_algorithm amdgpu_dm_i2c_algo = { 4734 .master_xfer = amdgpu_dm_i2c_xfer, 4735 .functionality = amdgpu_dm_i2c_func, 4736 }; 4737 4738 static struct amdgpu_i2c_adapter * 4739 create_i2c(struct ddc_service *ddc_service, 4740 int link_index, 4741 int *res) 4742 { 4743 struct amdgpu_device *adev = ddc_service->ctx->driver_context; 4744 struct amdgpu_i2c_adapter *i2c; 4745 4746 i2c = kzalloc(sizeof(struct amdgpu_i2c_adapter), GFP_KERNEL); 4747 if (!i2c) 4748 return NULL; 4749 i2c->base.owner = THIS_MODULE; 4750 i2c->base.class = I2C_CLASS_DDC; 4751 i2c->base.dev.parent = &adev->pdev->dev; 4752 i2c->base.algo = &amdgpu_dm_i2c_algo; 4753 snprintf(i2c->base.name, sizeof(i2c->base.name), "AMDGPU DM i2c hw bus %d", link_index); 4754 i2c_set_adapdata(&i2c->base, i2c); 4755 i2c->ddc_service = ddc_service; 4756 i2c->ddc_service->ddc_pin->hw_info.ddc_channel = link_index; 4757 4758 return i2c; 4759 } 4760 4761 4762 /* 4763 * Note: this function assumes that dc_link_detect() was called for the 4764 * dc_link which will be represented by this aconnector. 4765 */ 4766 static int amdgpu_dm_connector_init(struct amdgpu_display_manager *dm, 4767 struct amdgpu_dm_connector *aconnector, 4768 uint32_t link_index, 4769 struct amdgpu_encoder *aencoder) 4770 { 4771 int res = 0; 4772 int connector_type; 4773 struct dc *dc = dm->dc; 4774 struct dc_link *link = dc_get_link_at_index(dc, link_index); 4775 struct amdgpu_i2c_adapter *i2c; 4776 4777 link->priv = aconnector; 4778 4779 DRM_DEBUG_DRIVER("%s()\n", __func__); 4780 4781 i2c = create_i2c(link->ddc, link->link_index, &res); 4782 if (!i2c) { 4783 DRM_ERROR("Failed to create i2c adapter data\n"); 4784 return -ENOMEM; 4785 } 4786 4787 aconnector->i2c = i2c; 4788 res = i2c_add_adapter(&i2c->base); 4789 4790 if (res) { 4791 DRM_ERROR("Failed to register hw i2c %d\n", link->link_index); 4792 goto out_free; 4793 } 4794 4795 connector_type = to_drm_connector_type(link->connector_signal); 4796 4797 res = drm_connector_init( 4798 dm->ddev, 4799 &aconnector->base, 4800 &amdgpu_dm_connector_funcs, 4801 connector_type); 4802 4803 if (res) { 4804 DRM_ERROR("connector_init failed\n"); 4805 aconnector->connector_id = -1; 4806 goto out_free; 4807 } 4808 4809 drm_connector_helper_add( 4810 &aconnector->base, 4811 &amdgpu_dm_connector_helper_funcs); 4812 4813 if (aconnector->base.funcs->reset) 4814 aconnector->base.funcs->reset(&aconnector->base); 4815 4816 amdgpu_dm_connector_init_helper( 4817 dm, 4818 aconnector, 4819 connector_type, 4820 link, 4821 link_index); 4822 4823 drm_connector_attach_encoder( 4824 &aconnector->base, &aencoder->base); 4825 4826 drm_connector_register(&aconnector->base); 4827 #if defined(CONFIG_DEBUG_FS) 4828 res = connector_debugfs_init(aconnector); 4829 if (res) { 4830 DRM_ERROR("Failed to create debugfs for connector"); 4831 goto out_free; 4832 } 4833 aconnector->debugfs_dpcd_address = 0; 4834 aconnector->debugfs_dpcd_size = 0; 4835 #endif 4836 4837 if (connector_type == DRM_MODE_CONNECTOR_DisplayPort 4838 || connector_type == DRM_MODE_CONNECTOR_eDP) 4839 amdgpu_dm_initialize_dp_connector(dm, aconnector); 4840 4841 out_free: 4842 if (res) { 4843 kfree(i2c); 4844 aconnector->i2c = NULL; 4845 } 4846 return res; 4847 } 4848 4849 int amdgpu_dm_get_encoder_crtc_mask(struct amdgpu_device *adev) 4850 { 4851 switch (adev->mode_info.num_crtc) { 4852 case 1: 4853 return 0x1; 4854 case 2: 4855 return 0x3; 4856 case 3: 4857 return 0x7; 4858 case 4: 4859 return 0xf; 4860 case 5: 4861 return 0x1f; 4862 case 6: 4863 default: 4864 return 0x3f; 4865 } 4866 } 4867 4868 static int amdgpu_dm_encoder_init(struct drm_device *dev, 4869 struct amdgpu_encoder *aencoder, 4870 uint32_t link_index) 4871 { 4872 struct amdgpu_device *adev = dev->dev_private; 4873 4874 int res = drm_encoder_init(dev, 4875 &aencoder->base, 4876 &amdgpu_dm_encoder_funcs, 4877 DRM_MODE_ENCODER_TMDS, 4878 NULL); 4879 4880 aencoder->base.possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev); 4881 4882 if (!res) 4883 aencoder->encoder_id = link_index; 4884 else 4885 aencoder->encoder_id = -1; 4886 4887 drm_encoder_helper_add(&aencoder->base, &amdgpu_dm_encoder_helper_funcs); 4888 4889 return res; 4890 } 4891 4892 static void manage_dm_interrupts(struct amdgpu_device *adev, 4893 struct amdgpu_crtc *acrtc, 4894 bool enable) 4895 { 4896 /* 4897 * this is not correct translation but will work as soon as VBLANK 4898 * constant is the same as PFLIP 4899 */ 4900 int irq_type = 4901 amdgpu_display_crtc_idx_to_irq_type( 4902 adev, 4903 acrtc->crtc_id); 4904 4905 if (enable) { 4906 drm_crtc_vblank_on(&acrtc->base); 4907 amdgpu_irq_get( 4908 adev, 4909 &adev->pageflip_irq, 4910 irq_type); 4911 } else { 4912 4913 amdgpu_irq_put( 4914 adev, 4915 &adev->pageflip_irq, 4916 irq_type); 4917 drm_crtc_vblank_off(&acrtc->base); 4918 } 4919 } 4920 4921 static bool 4922 is_scaling_state_different(const struct dm_connector_state *dm_state, 4923 const struct dm_connector_state *old_dm_state) 4924 { 4925 if (dm_state->scaling != old_dm_state->scaling) 4926 return true; 4927 if (!dm_state->underscan_enable && old_dm_state->underscan_enable) { 4928 if (old_dm_state->underscan_hborder != 0 && old_dm_state->underscan_vborder != 0) 4929 return true; 4930 } else if (dm_state->underscan_enable && !old_dm_state->underscan_enable) { 4931 if (dm_state->underscan_hborder != 0 && dm_state->underscan_vborder != 0) 4932 return true; 4933 } else if (dm_state->underscan_hborder != old_dm_state->underscan_hborder || 4934 dm_state->underscan_vborder != old_dm_state->underscan_vborder) 4935 return true; 4936 return false; 4937 } 4938 4939 static void remove_stream(struct amdgpu_device *adev, 4940 struct amdgpu_crtc *acrtc, 4941 struct dc_stream_state *stream) 4942 { 4943 /* this is the update mode case */ 4944 4945 acrtc->otg_inst = -1; 4946 acrtc->enabled = false; 4947 } 4948 4949 static int get_cursor_position(struct drm_plane *plane, struct drm_crtc *crtc, 4950 struct dc_cursor_position *position) 4951 { 4952 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 4953 int x, y; 4954 int xorigin = 0, yorigin = 0; 4955 4956 if (!crtc || !plane->state->fb) { 4957 position->enable = false; 4958 position->x = 0; 4959 position->y = 0; 4960 return 0; 4961 } 4962 4963 if ((plane->state->crtc_w > amdgpu_crtc->max_cursor_width) || 4964 (plane->state->crtc_h > amdgpu_crtc->max_cursor_height)) { 4965 DRM_ERROR("%s: bad cursor width or height %d x %d\n", 4966 __func__, 4967 plane->state->crtc_w, 4968 plane->state->crtc_h); 4969 return -EINVAL; 4970 } 4971 4972 x = plane->state->crtc_x; 4973 y = plane->state->crtc_y; 4974 4975 if (crtc->primary->state) { 4976 /* avivo cursor are offset into the total surface */ 4977 x += crtc->primary->state->src_x >> 16; 4978 y += crtc->primary->state->src_y >> 16; 4979 } 4980 4981 if (x < 0) { 4982 xorigin = min(-x, amdgpu_crtc->max_cursor_width - 1); 4983 x = 0; 4984 } 4985 if (y < 0) { 4986 yorigin = min(-y, amdgpu_crtc->max_cursor_height - 1); 4987 y = 0; 4988 } 4989 position->enable = true; 4990 position->x = x; 4991 position->y = y; 4992 position->x_hotspot = xorigin; 4993 position->y_hotspot = yorigin; 4994 4995 return 0; 4996 } 4997 4998 static void handle_cursor_update(struct drm_plane *plane, 4999 struct drm_plane_state *old_plane_state) 5000 { 5001 struct amdgpu_device *adev = plane->dev->dev_private; 5002 struct amdgpu_framebuffer *afb = to_amdgpu_framebuffer(plane->state->fb); 5003 struct drm_crtc *crtc = afb ? plane->state->crtc : old_plane_state->crtc; 5004 struct dm_crtc_state *crtc_state = crtc ? to_dm_crtc_state(crtc->state) : NULL; 5005 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 5006 uint64_t address = afb ? afb->address : 0; 5007 struct dc_cursor_position position; 5008 struct dc_cursor_attributes attributes; 5009 int ret; 5010 5011 if (!plane->state->fb && !old_plane_state->fb) 5012 return; 5013 5014 DRM_DEBUG_DRIVER("%s: crtc_id=%d with size %d to %d\n", 5015 __func__, 5016 amdgpu_crtc->crtc_id, 5017 plane->state->crtc_w, 5018 plane->state->crtc_h); 5019 5020 ret = get_cursor_position(plane, crtc, &position); 5021 if (ret) 5022 return; 5023 5024 if (!position.enable) { 5025 /* turn off cursor */ 5026 if (crtc_state && crtc_state->stream) { 5027 mutex_lock(&adev->dm.dc_lock); 5028 dc_stream_set_cursor_position(crtc_state->stream, 5029 &position); 5030 mutex_unlock(&adev->dm.dc_lock); 5031 } 5032 return; 5033 } 5034 5035 amdgpu_crtc->cursor_width = plane->state->crtc_w; 5036 amdgpu_crtc->cursor_height = plane->state->crtc_h; 5037 5038 memset(&attributes, 0, sizeof(attributes)); 5039 attributes.address.high_part = upper_32_bits(address); 5040 attributes.address.low_part = lower_32_bits(address); 5041 attributes.width = plane->state->crtc_w; 5042 attributes.height = plane->state->crtc_h; 5043 attributes.color_format = CURSOR_MODE_COLOR_PRE_MULTIPLIED_ALPHA; 5044 attributes.rotation_angle = 0; 5045 attributes.attribute_flags.value = 0; 5046 5047 attributes.pitch = attributes.width; 5048 5049 if (crtc_state->stream) { 5050 mutex_lock(&adev->dm.dc_lock); 5051 if (!dc_stream_set_cursor_attributes(crtc_state->stream, 5052 &attributes)) 5053 DRM_ERROR("DC failed to set cursor attributes\n"); 5054 5055 if (!dc_stream_set_cursor_position(crtc_state->stream, 5056 &position)) 5057 DRM_ERROR("DC failed to set cursor position\n"); 5058 mutex_unlock(&adev->dm.dc_lock); 5059 } 5060 } 5061 5062 static void prepare_flip_isr(struct amdgpu_crtc *acrtc) 5063 { 5064 5065 assert_spin_locked(&acrtc->base.dev->event_lock); 5066 WARN_ON(acrtc->event); 5067 5068 acrtc->event = acrtc->base.state->event; 5069 5070 /* Set the flip status */ 5071 acrtc->pflip_status = AMDGPU_FLIP_SUBMITTED; 5072 5073 /* Mark this event as consumed */ 5074 acrtc->base.state->event = NULL; 5075 5076 DRM_DEBUG_DRIVER("crtc:%d, pflip_stat:AMDGPU_FLIP_SUBMITTED\n", 5077 acrtc->crtc_id); 5078 } 5079 5080 static void update_freesync_state_on_stream( 5081 struct amdgpu_display_manager *dm, 5082 struct dm_crtc_state *new_crtc_state, 5083 struct dc_stream_state *new_stream, 5084 struct dc_plane_state *surface, 5085 u32 flip_timestamp_in_us) 5086 { 5087 struct mod_vrr_params vrr_params; 5088 struct dc_info_packet vrr_infopacket = {0}; 5089 struct amdgpu_device *adev = dm->adev; 5090 unsigned long flags; 5091 5092 if (!new_stream) 5093 return; 5094 5095 /* 5096 * TODO: Determine why min/max totals and vrefresh can be 0 here. 5097 * For now it's sufficient to just guard against these conditions. 5098 */ 5099 5100 if (!new_stream->timing.h_total || !new_stream->timing.v_total) 5101 return; 5102 5103 spin_lock_irqsave(&adev->ddev->event_lock, flags); 5104 vrr_params = new_crtc_state->vrr_params; 5105 5106 if (surface) { 5107 mod_freesync_handle_preflip( 5108 dm->freesync_module, 5109 surface, 5110 new_stream, 5111 flip_timestamp_in_us, 5112 &vrr_params); 5113 5114 if (adev->family < AMDGPU_FAMILY_AI && 5115 amdgpu_dm_vrr_active(new_crtc_state)) { 5116 mod_freesync_handle_v_update(dm->freesync_module, 5117 new_stream, &vrr_params); 5118 } 5119 } 5120 5121 mod_freesync_build_vrr_infopacket( 5122 dm->freesync_module, 5123 new_stream, 5124 &vrr_params, 5125 PACKET_TYPE_VRR, 5126 TRANSFER_FUNC_UNKNOWN, 5127 &vrr_infopacket); 5128 5129 new_crtc_state->freesync_timing_changed |= 5130 (memcmp(&new_crtc_state->vrr_params.adjust, 5131 &vrr_params.adjust, 5132 sizeof(vrr_params.adjust)) != 0); 5133 5134 new_crtc_state->freesync_vrr_info_changed |= 5135 (memcmp(&new_crtc_state->vrr_infopacket, 5136 &vrr_infopacket, 5137 sizeof(vrr_infopacket)) != 0); 5138 5139 new_crtc_state->vrr_params = vrr_params; 5140 new_crtc_state->vrr_infopacket = vrr_infopacket; 5141 5142 new_stream->adjust = new_crtc_state->vrr_params.adjust; 5143 new_stream->vrr_infopacket = vrr_infopacket; 5144 5145 if (new_crtc_state->freesync_vrr_info_changed) 5146 DRM_DEBUG_KMS("VRR packet update: crtc=%u enabled=%d state=%d", 5147 new_crtc_state->base.crtc->base.id, 5148 (int)new_crtc_state->base.vrr_enabled, 5149 (int)vrr_params.state); 5150 5151 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 5152 } 5153 5154 static void pre_update_freesync_state_on_stream( 5155 struct amdgpu_display_manager *dm, 5156 struct dm_crtc_state *new_crtc_state) 5157 { 5158 struct dc_stream_state *new_stream = new_crtc_state->stream; 5159 struct mod_vrr_params vrr_params; 5160 struct mod_freesync_config config = new_crtc_state->freesync_config; 5161 struct amdgpu_device *adev = dm->adev; 5162 unsigned long flags; 5163 5164 if (!new_stream) 5165 return; 5166 5167 /* 5168 * TODO: Determine why min/max totals and vrefresh can be 0 here. 5169 * For now it's sufficient to just guard against these conditions. 5170 */ 5171 if (!new_stream->timing.h_total || !new_stream->timing.v_total) 5172 return; 5173 5174 spin_lock_irqsave(&adev->ddev->event_lock, flags); 5175 vrr_params = new_crtc_state->vrr_params; 5176 5177 if (new_crtc_state->vrr_supported && 5178 config.min_refresh_in_uhz && 5179 config.max_refresh_in_uhz) { 5180 config.state = new_crtc_state->base.vrr_enabled ? 5181 VRR_STATE_ACTIVE_VARIABLE : 5182 VRR_STATE_INACTIVE; 5183 } else { 5184 config.state = VRR_STATE_UNSUPPORTED; 5185 } 5186 5187 mod_freesync_build_vrr_params(dm->freesync_module, 5188 new_stream, 5189 &config, &vrr_params); 5190 5191 new_crtc_state->freesync_timing_changed |= 5192 (memcmp(&new_crtc_state->vrr_params.adjust, 5193 &vrr_params.adjust, 5194 sizeof(vrr_params.adjust)) != 0); 5195 5196 new_crtc_state->vrr_params = vrr_params; 5197 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 5198 } 5199 5200 static void amdgpu_dm_handle_vrr_transition(struct dm_crtc_state *old_state, 5201 struct dm_crtc_state *new_state) 5202 { 5203 bool old_vrr_active = amdgpu_dm_vrr_active(old_state); 5204 bool new_vrr_active = amdgpu_dm_vrr_active(new_state); 5205 5206 if (!old_vrr_active && new_vrr_active) { 5207 /* Transition VRR inactive -> active: 5208 * While VRR is active, we must not disable vblank irq, as a 5209 * reenable after disable would compute bogus vblank/pflip 5210 * timestamps if it likely happened inside display front-porch. 5211 * 5212 * We also need vupdate irq for the actual core vblank handling 5213 * at end of vblank. 5214 */ 5215 dm_set_vupdate_irq(new_state->base.crtc, true); 5216 drm_crtc_vblank_get(new_state->base.crtc); 5217 DRM_DEBUG_DRIVER("%s: crtc=%u VRR off->on: Get vblank ref\n", 5218 __func__, new_state->base.crtc->base.id); 5219 } else if (old_vrr_active && !new_vrr_active) { 5220 /* Transition VRR active -> inactive: 5221 * Allow vblank irq disable again for fixed refresh rate. 5222 */ 5223 dm_set_vupdate_irq(new_state->base.crtc, false); 5224 drm_crtc_vblank_put(new_state->base.crtc); 5225 DRM_DEBUG_DRIVER("%s: crtc=%u VRR on->off: Drop vblank ref\n", 5226 __func__, new_state->base.crtc->base.id); 5227 } 5228 } 5229 5230 static void amdgpu_dm_commit_cursors(struct drm_atomic_state *state) 5231 { 5232 struct drm_plane *plane; 5233 struct drm_plane_state *old_plane_state, *new_plane_state; 5234 int i; 5235 5236 /* 5237 * TODO: Make this per-stream so we don't issue redundant updates for 5238 * commits with multiple streams. 5239 */ 5240 for_each_oldnew_plane_in_state(state, plane, old_plane_state, 5241 new_plane_state, i) 5242 if (plane->type == DRM_PLANE_TYPE_CURSOR) 5243 handle_cursor_update(plane, old_plane_state); 5244 } 5245 5246 static void amdgpu_dm_commit_planes(struct drm_atomic_state *state, 5247 struct dc_state *dc_state, 5248 struct drm_device *dev, 5249 struct amdgpu_display_manager *dm, 5250 struct drm_crtc *pcrtc, 5251 bool wait_for_vblank) 5252 { 5253 uint32_t i; 5254 uint64_t timestamp_ns; 5255 struct drm_plane *plane; 5256 struct drm_plane_state *old_plane_state, *new_plane_state; 5257 struct amdgpu_crtc *acrtc_attach = to_amdgpu_crtc(pcrtc); 5258 struct drm_crtc_state *new_pcrtc_state = 5259 drm_atomic_get_new_crtc_state(state, pcrtc); 5260 struct dm_crtc_state *acrtc_state = to_dm_crtc_state(new_pcrtc_state); 5261 struct dm_crtc_state *dm_old_crtc_state = 5262 to_dm_crtc_state(drm_atomic_get_old_crtc_state(state, pcrtc)); 5263 int planes_count = 0, vpos, hpos; 5264 long r; 5265 unsigned long flags; 5266 struct amdgpu_bo *abo; 5267 uint64_t tiling_flags; 5268 uint32_t target_vblank, last_flip_vblank; 5269 bool vrr_active = amdgpu_dm_vrr_active(acrtc_state); 5270 bool pflip_present = false; 5271 struct { 5272 struct dc_surface_update surface_updates[MAX_SURFACES]; 5273 struct dc_plane_info plane_infos[MAX_SURFACES]; 5274 struct dc_scaling_info scaling_infos[MAX_SURFACES]; 5275 struct dc_flip_addrs flip_addrs[MAX_SURFACES]; 5276 struct dc_stream_update stream_update; 5277 } *bundle; 5278 5279 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL); 5280 5281 if (!bundle) { 5282 dm_error("Failed to allocate update bundle\n"); 5283 goto cleanup; 5284 } 5285 5286 /* 5287 * Disable the cursor first if we're disabling all the planes. 5288 * It'll remain on the screen after the planes are re-enabled 5289 * if we don't. 5290 */ 5291 if (acrtc_state->active_planes == 0) 5292 amdgpu_dm_commit_cursors(state); 5293 5294 /* update planes when needed */ 5295 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 5296 struct drm_crtc *crtc = new_plane_state->crtc; 5297 struct drm_crtc_state *new_crtc_state; 5298 struct drm_framebuffer *fb = new_plane_state->fb; 5299 bool plane_needs_flip; 5300 struct dc_plane_state *dc_plane; 5301 struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state); 5302 5303 /* Cursor plane is handled after stream updates */ 5304 if (plane->type == DRM_PLANE_TYPE_CURSOR) 5305 continue; 5306 5307 if (!fb || !crtc || pcrtc != crtc) 5308 continue; 5309 5310 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 5311 if (!new_crtc_state->active) 5312 continue; 5313 5314 dc_plane = dm_new_plane_state->dc_state; 5315 5316 bundle->surface_updates[planes_count].surface = dc_plane; 5317 if (new_pcrtc_state->color_mgmt_changed) { 5318 bundle->surface_updates[planes_count].gamma = dc_plane->gamma_correction; 5319 bundle->surface_updates[planes_count].in_transfer_func = dc_plane->in_transfer_func; 5320 } 5321 5322 fill_dc_scaling_info(new_plane_state, 5323 &bundle->scaling_infos[planes_count]); 5324 5325 bundle->surface_updates[planes_count].scaling_info = 5326 &bundle->scaling_infos[planes_count]; 5327 5328 plane_needs_flip = old_plane_state->fb && new_plane_state->fb; 5329 5330 pflip_present = pflip_present || plane_needs_flip; 5331 5332 if (!plane_needs_flip) { 5333 planes_count += 1; 5334 continue; 5335 } 5336 5337 abo = gem_to_amdgpu_bo(fb->obj[0]); 5338 5339 /* 5340 * Wait for all fences on this FB. Do limited wait to avoid 5341 * deadlock during GPU reset when this fence will not signal 5342 * but we hold reservation lock for the BO. 5343 */ 5344 r = reservation_object_wait_timeout_rcu(abo->tbo.resv, true, 5345 false, 5346 msecs_to_jiffies(5000)); 5347 if (unlikely(r <= 0)) 5348 DRM_ERROR("Waiting for fences timed out or interrupted!"); 5349 5350 /* 5351 * TODO This might fail and hence better not used, wait 5352 * explicitly on fences instead 5353 * and in general should be called for 5354 * blocking commit to as per framework helpers 5355 */ 5356 r = amdgpu_bo_reserve(abo, true); 5357 if (unlikely(r != 0)) 5358 DRM_ERROR("failed to reserve buffer before flip\n"); 5359 5360 amdgpu_bo_get_tiling_flags(abo, &tiling_flags); 5361 5362 amdgpu_bo_unreserve(abo); 5363 5364 fill_dc_plane_info_and_addr( 5365 dm->adev, new_plane_state, tiling_flags, 5366 &bundle->plane_infos[planes_count], 5367 &bundle->flip_addrs[planes_count].address); 5368 5369 bundle->surface_updates[planes_count].plane_info = 5370 &bundle->plane_infos[planes_count]; 5371 5372 bundle->flip_addrs[planes_count].flip_immediate = 5373 (crtc->state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0; 5374 5375 timestamp_ns = ktime_get_ns(); 5376 bundle->flip_addrs[planes_count].flip_timestamp_in_us = div_u64(timestamp_ns, 1000); 5377 bundle->surface_updates[planes_count].flip_addr = &bundle->flip_addrs[planes_count]; 5378 bundle->surface_updates[planes_count].surface = dc_plane; 5379 5380 if (!bundle->surface_updates[planes_count].surface) { 5381 DRM_ERROR("No surface for CRTC: id=%d\n", 5382 acrtc_attach->crtc_id); 5383 continue; 5384 } 5385 5386 if (plane == pcrtc->primary) 5387 update_freesync_state_on_stream( 5388 dm, 5389 acrtc_state, 5390 acrtc_state->stream, 5391 dc_plane, 5392 bundle->flip_addrs[planes_count].flip_timestamp_in_us); 5393 5394 DRM_DEBUG_DRIVER("%s Flipping to hi: 0x%x, low: 0x%x\n", 5395 __func__, 5396 bundle->flip_addrs[planes_count].address.grph.addr.high_part, 5397 bundle->flip_addrs[planes_count].address.grph.addr.low_part); 5398 5399 planes_count += 1; 5400 5401 } 5402 5403 if (pflip_present) { 5404 if (!vrr_active) { 5405 /* Use old throttling in non-vrr fixed refresh rate mode 5406 * to keep flip scheduling based on target vblank counts 5407 * working in a backwards compatible way, e.g., for 5408 * clients using the GLX_OML_sync_control extension or 5409 * DRI3/Present extension with defined target_msc. 5410 */ 5411 last_flip_vblank = amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id); 5412 } 5413 else { 5414 /* For variable refresh rate mode only: 5415 * Get vblank of last completed flip to avoid > 1 vrr 5416 * flips per video frame by use of throttling, but allow 5417 * flip programming anywhere in the possibly large 5418 * variable vrr vblank interval for fine-grained flip 5419 * timing control and more opportunity to avoid stutter 5420 * on late submission of flips. 5421 */ 5422 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 5423 last_flip_vblank = acrtc_attach->last_flip_vblank; 5424 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 5425 } 5426 5427 target_vblank = last_flip_vblank + wait_for_vblank; 5428 5429 /* 5430 * Wait until we're out of the vertical blank period before the one 5431 * targeted by the flip 5432 */ 5433 while ((acrtc_attach->enabled && 5434 (amdgpu_display_get_crtc_scanoutpos(dm->ddev, acrtc_attach->crtc_id, 5435 0, &vpos, &hpos, NULL, 5436 NULL, &pcrtc->hwmode) 5437 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) == 5438 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) && 5439 (int)(target_vblank - 5440 amdgpu_get_vblank_counter_kms(dm->ddev, acrtc_attach->crtc_id)) > 0)) { 5441 usleep_range(1000, 1100); 5442 } 5443 5444 if (acrtc_attach->base.state->event) { 5445 drm_crtc_vblank_get(pcrtc); 5446 5447 spin_lock_irqsave(&pcrtc->dev->event_lock, flags); 5448 5449 WARN_ON(acrtc_attach->pflip_status != AMDGPU_FLIP_NONE); 5450 prepare_flip_isr(acrtc_attach); 5451 5452 spin_unlock_irqrestore(&pcrtc->dev->event_lock, flags); 5453 } 5454 5455 if (acrtc_state->stream) { 5456 5457 if (acrtc_state->freesync_timing_changed) 5458 bundle->stream_update.adjust = 5459 &acrtc_state->stream->adjust; 5460 5461 if (acrtc_state->freesync_vrr_info_changed) 5462 bundle->stream_update.vrr_infopacket = 5463 &acrtc_state->stream->vrr_infopacket; 5464 } 5465 } 5466 5467 /* Update the planes if changed or disable if we don't have any. */ 5468 if (planes_count || acrtc_state->active_planes == 0) { 5469 if (new_pcrtc_state->mode_changed) { 5470 bundle->stream_update.src = acrtc_state->stream->src; 5471 bundle->stream_update.dst = acrtc_state->stream->dst; 5472 } 5473 5474 if (new_pcrtc_state->color_mgmt_changed) 5475 bundle->stream_update.out_transfer_func = acrtc_state->stream->out_transfer_func; 5476 5477 acrtc_state->stream->abm_level = acrtc_state->abm_level; 5478 if (acrtc_state->abm_level != dm_old_crtc_state->abm_level) 5479 bundle->stream_update.abm_level = &acrtc_state->abm_level; 5480 5481 mutex_lock(&dm->dc_lock); 5482 dc_commit_updates_for_stream(dm->dc, 5483 bundle->surface_updates, 5484 planes_count, 5485 acrtc_state->stream, 5486 &bundle->stream_update, 5487 dc_state); 5488 mutex_unlock(&dm->dc_lock); 5489 } 5490 5491 /* 5492 * Update cursor state *after* programming all the planes. 5493 * This avoids redundant programming in the case where we're going 5494 * to be disabling a single plane - those pipes are being disabled. 5495 */ 5496 if (acrtc_state->active_planes) 5497 amdgpu_dm_commit_cursors(state); 5498 5499 cleanup: 5500 kfree(bundle); 5501 } 5502 5503 /* 5504 * Enable interrupts on CRTCs that are newly active, undergone 5505 * a modeset, or have active planes again. 5506 * 5507 * Done in two passes, based on the for_modeset flag: 5508 * Pass 1: For CRTCs going through modeset 5509 * Pass 2: For CRTCs going from 0 to n active planes 5510 * 5511 * Interrupts can only be enabled after the planes are programmed, 5512 * so this requires a two-pass approach since we don't want to 5513 * just defer the interrupts until after commit planes every time. 5514 */ 5515 static void amdgpu_dm_enable_crtc_interrupts(struct drm_device *dev, 5516 struct drm_atomic_state *state, 5517 bool for_modeset) 5518 { 5519 struct amdgpu_device *adev = dev->dev_private; 5520 struct drm_crtc *crtc; 5521 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5522 int i; 5523 5524 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, 5525 new_crtc_state, i) { 5526 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 5527 struct dm_crtc_state *dm_new_crtc_state = 5528 to_dm_crtc_state(new_crtc_state); 5529 struct dm_crtc_state *dm_old_crtc_state = 5530 to_dm_crtc_state(old_crtc_state); 5531 bool modeset = drm_atomic_crtc_needs_modeset(new_crtc_state); 5532 bool run_pass; 5533 5534 run_pass = (for_modeset && modeset) || 5535 (!for_modeset && !modeset && 5536 !dm_old_crtc_state->interrupts_enabled); 5537 5538 if (!run_pass) 5539 continue; 5540 5541 if (!dm_new_crtc_state->interrupts_enabled) 5542 continue; 5543 5544 manage_dm_interrupts(adev, acrtc, true); 5545 5546 #ifdef CONFIG_DEBUG_FS 5547 /* The stream has changed so CRC capture needs to re-enabled. */ 5548 if (dm_new_crtc_state->crc_enabled) { 5549 dm_new_crtc_state->crc_enabled = false; 5550 amdgpu_dm_crtc_set_crc_source(crtc, "auto"); 5551 } 5552 #endif 5553 } 5554 } 5555 5556 /* 5557 * amdgpu_dm_crtc_copy_transient_flags - copy mirrored flags from DRM to DC 5558 * @crtc_state: the DRM CRTC state 5559 * @stream_state: the DC stream state. 5560 * 5561 * Copy the mirrored transient state flags from DRM, to DC. It is used to bring 5562 * a dc_stream_state's flags in sync with a drm_crtc_state's flags. 5563 */ 5564 static void amdgpu_dm_crtc_copy_transient_flags(struct drm_crtc_state *crtc_state, 5565 struct dc_stream_state *stream_state) 5566 { 5567 stream_state->mode_changed = drm_atomic_crtc_needs_modeset(crtc_state); 5568 } 5569 5570 static int amdgpu_dm_atomic_commit(struct drm_device *dev, 5571 struct drm_atomic_state *state, 5572 bool nonblock) 5573 { 5574 struct drm_crtc *crtc; 5575 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5576 struct amdgpu_device *adev = dev->dev_private; 5577 int i; 5578 5579 /* 5580 * We evade vblank and pflip interrupts on CRTCs that are undergoing 5581 * a modeset, being disabled, or have no active planes. 5582 * 5583 * It's done in atomic commit rather than commit tail for now since 5584 * some of these interrupt handlers access the current CRTC state and 5585 * potentially the stream pointer itself. 5586 * 5587 * Since the atomic state is swapped within atomic commit and not within 5588 * commit tail this would leave to new state (that hasn't been committed yet) 5589 * being accesssed from within the handlers. 5590 * 5591 * TODO: Fix this so we can do this in commit tail and not have to block 5592 * in atomic check. 5593 */ 5594 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 5595 struct dm_crtc_state *dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5596 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5597 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 5598 5599 if (dm_old_crtc_state->interrupts_enabled && 5600 (!dm_new_crtc_state->interrupts_enabled || 5601 drm_atomic_crtc_needs_modeset(new_crtc_state))) { 5602 /* 5603 * Drop the extra vblank reference added by CRC 5604 * capture if applicable. 5605 */ 5606 if (dm_new_crtc_state->crc_enabled) 5607 drm_crtc_vblank_put(crtc); 5608 5609 /* 5610 * Only keep CRC capture enabled if there's 5611 * still a stream for the CRTC. 5612 */ 5613 if (!dm_new_crtc_state->stream) 5614 dm_new_crtc_state->crc_enabled = false; 5615 5616 manage_dm_interrupts(adev, acrtc, false); 5617 } 5618 } 5619 /* 5620 * Add check here for SoC's that support hardware cursor plane, to 5621 * unset legacy_cursor_update 5622 */ 5623 5624 return drm_atomic_helper_commit(dev, state, nonblock); 5625 5626 /*TODO Handle EINTR, reenable IRQ*/ 5627 } 5628 5629 /** 5630 * amdgpu_dm_atomic_commit_tail() - AMDgpu DM's commit tail implementation. 5631 * @state: The atomic state to commit 5632 * 5633 * This will tell DC to commit the constructed DC state from atomic_check, 5634 * programming the hardware. Any failures here implies a hardware failure, since 5635 * atomic check should have filtered anything non-kosher. 5636 */ 5637 static void amdgpu_dm_atomic_commit_tail(struct drm_atomic_state *state) 5638 { 5639 struct drm_device *dev = state->dev; 5640 struct amdgpu_device *adev = dev->dev_private; 5641 struct amdgpu_display_manager *dm = &adev->dm; 5642 struct dm_atomic_state *dm_state; 5643 struct dc_state *dc_state = NULL, *dc_state_temp = NULL; 5644 uint32_t i, j; 5645 struct drm_crtc *crtc; 5646 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5647 unsigned long flags; 5648 bool wait_for_vblank = true; 5649 struct drm_connector *connector; 5650 struct drm_connector_state *old_con_state, *new_con_state; 5651 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 5652 int crtc_disable_count = 0; 5653 5654 drm_atomic_helper_update_legacy_modeset_state(dev, state); 5655 5656 dm_state = dm_atomic_get_new_state(state); 5657 if (dm_state && dm_state->context) { 5658 dc_state = dm_state->context; 5659 } else { 5660 /* No state changes, retain current state. */ 5661 dc_state_temp = dc_create_state(dm->dc); 5662 ASSERT(dc_state_temp); 5663 dc_state = dc_state_temp; 5664 dc_resource_state_copy_construct_current(dm->dc, dc_state); 5665 } 5666 5667 /* update changed items */ 5668 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 5669 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 5670 5671 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5672 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5673 5674 DRM_DEBUG_DRIVER( 5675 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, " 5676 "planes_changed:%d, mode_changed:%d,active_changed:%d," 5677 "connectors_changed:%d\n", 5678 acrtc->crtc_id, 5679 new_crtc_state->enable, 5680 new_crtc_state->active, 5681 new_crtc_state->planes_changed, 5682 new_crtc_state->mode_changed, 5683 new_crtc_state->active_changed, 5684 new_crtc_state->connectors_changed); 5685 5686 /* Copy all transient state flags into dc state */ 5687 if (dm_new_crtc_state->stream) { 5688 amdgpu_dm_crtc_copy_transient_flags(&dm_new_crtc_state->base, 5689 dm_new_crtc_state->stream); 5690 } 5691 5692 /* handles headless hotplug case, updating new_state and 5693 * aconnector as needed 5694 */ 5695 5696 if (modeset_required(new_crtc_state, dm_new_crtc_state->stream, dm_old_crtc_state->stream)) { 5697 5698 DRM_DEBUG_DRIVER("Atomic commit: SET crtc id %d: [%p]\n", acrtc->crtc_id, acrtc); 5699 5700 if (!dm_new_crtc_state->stream) { 5701 /* 5702 * this could happen because of issues with 5703 * userspace notifications delivery. 5704 * In this case userspace tries to set mode on 5705 * display which is disconnected in fact. 5706 * dc_sink is NULL in this case on aconnector. 5707 * We expect reset mode will come soon. 5708 * 5709 * This can also happen when unplug is done 5710 * during resume sequence ended 5711 * 5712 * In this case, we want to pretend we still 5713 * have a sink to keep the pipe running so that 5714 * hw state is consistent with the sw state 5715 */ 5716 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n", 5717 __func__, acrtc->base.base.id); 5718 continue; 5719 } 5720 5721 if (dm_old_crtc_state->stream) 5722 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 5723 5724 pm_runtime_get_noresume(dev->dev); 5725 5726 acrtc->enabled = true; 5727 acrtc->hw_mode = new_crtc_state->mode; 5728 crtc->hwmode = new_crtc_state->mode; 5729 } else if (modereset_required(new_crtc_state)) { 5730 DRM_DEBUG_DRIVER("Atomic commit: RESET. crtc id %d:[%p]\n", acrtc->crtc_id, acrtc); 5731 5732 /* i.e. reset mode */ 5733 if (dm_old_crtc_state->stream) 5734 remove_stream(adev, acrtc, dm_old_crtc_state->stream); 5735 } 5736 } /* for_each_crtc_in_state() */ 5737 5738 if (dc_state) { 5739 dm_enable_per_frame_crtc_master_sync(dc_state); 5740 mutex_lock(&dm->dc_lock); 5741 WARN_ON(!dc_commit_state(dm->dc, dc_state)); 5742 mutex_unlock(&dm->dc_lock); 5743 } 5744 5745 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 5746 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(crtc); 5747 5748 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5749 5750 if (dm_new_crtc_state->stream != NULL) { 5751 const struct dc_stream_status *status = 5752 dc_stream_get_status(dm_new_crtc_state->stream); 5753 5754 if (!status) 5755 status = dc_stream_get_status_from_state(dc_state, 5756 dm_new_crtc_state->stream); 5757 5758 if (!status) 5759 DC_ERR("got no status for stream %p on acrtc%p\n", dm_new_crtc_state->stream, acrtc); 5760 else 5761 acrtc->otg_inst = status->primary_otg_inst; 5762 } 5763 } 5764 5765 /* Handle connector state changes */ 5766 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 5767 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 5768 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 5769 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 5770 struct dc_surface_update dummy_updates[MAX_SURFACES]; 5771 struct dc_stream_update stream_update; 5772 struct dc_stream_status *status = NULL; 5773 5774 memset(&dummy_updates, 0, sizeof(dummy_updates)); 5775 memset(&stream_update, 0, sizeof(stream_update)); 5776 5777 if (acrtc) { 5778 new_crtc_state = drm_atomic_get_new_crtc_state(state, &acrtc->base); 5779 old_crtc_state = drm_atomic_get_old_crtc_state(state, &acrtc->base); 5780 } 5781 5782 /* Skip any modesets/resets */ 5783 if (!acrtc || drm_atomic_crtc_needs_modeset(new_crtc_state)) 5784 continue; 5785 5786 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5787 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5788 5789 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state) && 5790 (dm_new_crtc_state->abm_level == dm_old_crtc_state->abm_level)) 5791 continue; 5792 5793 if (is_scaling_state_different(dm_new_con_state, dm_old_con_state)) { 5794 update_stream_scaling_settings(&dm_new_con_state->base.crtc->mode, 5795 dm_new_con_state, (struct dc_stream_state *)dm_new_crtc_state->stream); 5796 5797 stream_update.src = dm_new_crtc_state->stream->src; 5798 stream_update.dst = dm_new_crtc_state->stream->dst; 5799 } 5800 5801 if (dm_new_crtc_state->abm_level != dm_old_crtc_state->abm_level) { 5802 dm_new_crtc_state->stream->abm_level = dm_new_crtc_state->abm_level; 5803 5804 stream_update.abm_level = &dm_new_crtc_state->abm_level; 5805 } 5806 5807 status = dc_stream_get_status(dm_new_crtc_state->stream); 5808 WARN_ON(!status); 5809 WARN_ON(!status->plane_count); 5810 5811 /* 5812 * TODO: DC refuses to perform stream updates without a dc_surface_update. 5813 * Here we create an empty update on each plane. 5814 * To fix this, DC should permit updating only stream properties. 5815 */ 5816 for (j = 0; j < status->plane_count; j++) 5817 dummy_updates[j].surface = status->plane_states[0]; 5818 5819 5820 mutex_lock(&dm->dc_lock); 5821 dc_commit_updates_for_stream(dm->dc, 5822 dummy_updates, 5823 status->plane_count, 5824 dm_new_crtc_state->stream, 5825 &stream_update, 5826 dc_state); 5827 mutex_unlock(&dm->dc_lock); 5828 } 5829 5830 /* Count number of newly disabled CRTCs for dropping PM refs later. */ 5831 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, 5832 new_crtc_state, i) { 5833 if (old_crtc_state->active && !new_crtc_state->active) 5834 crtc_disable_count++; 5835 5836 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5837 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 5838 5839 /* Update freesync active state. */ 5840 pre_update_freesync_state_on_stream(dm, dm_new_crtc_state); 5841 5842 /* Handle vrr on->off / off->on transitions */ 5843 amdgpu_dm_handle_vrr_transition(dm_old_crtc_state, 5844 dm_new_crtc_state); 5845 } 5846 5847 /* Enable interrupts for CRTCs going through a modeset. */ 5848 amdgpu_dm_enable_crtc_interrupts(dev, state, true); 5849 5850 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) 5851 if (new_crtc_state->pageflip_flags & DRM_MODE_PAGE_FLIP_ASYNC) 5852 wait_for_vblank = false; 5853 5854 /* update planes when needed per crtc*/ 5855 for_each_new_crtc_in_state(state, crtc, new_crtc_state, j) { 5856 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 5857 5858 if (dm_new_crtc_state->stream) 5859 amdgpu_dm_commit_planes(state, dc_state, dev, 5860 dm, crtc, wait_for_vblank); 5861 } 5862 5863 /* Enable interrupts for CRTCs going from 0 to n active planes. */ 5864 amdgpu_dm_enable_crtc_interrupts(dev, state, false); 5865 5866 /* 5867 * send vblank event on all events not handled in flip and 5868 * mark consumed event for drm_atomic_helper_commit_hw_done 5869 */ 5870 spin_lock_irqsave(&adev->ddev->event_lock, flags); 5871 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 5872 5873 if (new_crtc_state->event) 5874 drm_send_event_locked(dev, &new_crtc_state->event->base); 5875 5876 new_crtc_state->event = NULL; 5877 } 5878 spin_unlock_irqrestore(&adev->ddev->event_lock, flags); 5879 5880 /* Signal HW programming completion */ 5881 drm_atomic_helper_commit_hw_done(state); 5882 5883 if (wait_for_vblank) 5884 drm_atomic_helper_wait_for_flip_done(dev, state); 5885 5886 drm_atomic_helper_cleanup_planes(dev, state); 5887 5888 /* 5889 * Finally, drop a runtime PM reference for each newly disabled CRTC, 5890 * so we can put the GPU into runtime suspend if we're not driving any 5891 * displays anymore 5892 */ 5893 for (i = 0; i < crtc_disable_count; i++) 5894 pm_runtime_put_autosuspend(dev->dev); 5895 pm_runtime_mark_last_busy(dev->dev); 5896 5897 if (dc_state_temp) 5898 dc_release_state(dc_state_temp); 5899 } 5900 5901 5902 static int dm_force_atomic_commit(struct drm_connector *connector) 5903 { 5904 int ret = 0; 5905 struct drm_device *ddev = connector->dev; 5906 struct drm_atomic_state *state = drm_atomic_state_alloc(ddev); 5907 struct amdgpu_crtc *disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc); 5908 struct drm_plane *plane = disconnected_acrtc->base.primary; 5909 struct drm_connector_state *conn_state; 5910 struct drm_crtc_state *crtc_state; 5911 struct drm_plane_state *plane_state; 5912 5913 if (!state) 5914 return -ENOMEM; 5915 5916 state->acquire_ctx = ddev->mode_config.acquire_ctx; 5917 5918 /* Construct an atomic state to restore previous display setting */ 5919 5920 /* 5921 * Attach connectors to drm_atomic_state 5922 */ 5923 conn_state = drm_atomic_get_connector_state(state, connector); 5924 5925 ret = PTR_ERR_OR_ZERO(conn_state); 5926 if (ret) 5927 goto err; 5928 5929 /* Attach crtc to drm_atomic_state*/ 5930 crtc_state = drm_atomic_get_crtc_state(state, &disconnected_acrtc->base); 5931 5932 ret = PTR_ERR_OR_ZERO(crtc_state); 5933 if (ret) 5934 goto err; 5935 5936 /* force a restore */ 5937 crtc_state->mode_changed = true; 5938 5939 /* Attach plane to drm_atomic_state */ 5940 plane_state = drm_atomic_get_plane_state(state, plane); 5941 5942 ret = PTR_ERR_OR_ZERO(plane_state); 5943 if (ret) 5944 goto err; 5945 5946 5947 /* Call commit internally with the state we just constructed */ 5948 ret = drm_atomic_commit(state); 5949 if (!ret) 5950 return 0; 5951 5952 err: 5953 DRM_ERROR("Restoring old state failed with %i\n", ret); 5954 drm_atomic_state_put(state); 5955 5956 return ret; 5957 } 5958 5959 /* 5960 * This function handles all cases when set mode does not come upon hotplug. 5961 * This includes when a display is unplugged then plugged back into the 5962 * same port and when running without usermode desktop manager supprot 5963 */ 5964 void dm_restore_drm_connector_state(struct drm_device *dev, 5965 struct drm_connector *connector) 5966 { 5967 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 5968 struct amdgpu_crtc *disconnected_acrtc; 5969 struct dm_crtc_state *acrtc_state; 5970 5971 if (!aconnector->dc_sink || !connector->state || !connector->encoder) 5972 return; 5973 5974 disconnected_acrtc = to_amdgpu_crtc(connector->encoder->crtc); 5975 if (!disconnected_acrtc) 5976 return; 5977 5978 acrtc_state = to_dm_crtc_state(disconnected_acrtc->base.state); 5979 if (!acrtc_state->stream) 5980 return; 5981 5982 /* 5983 * If the previous sink is not released and different from the current, 5984 * we deduce we are in a state where we can not rely on usermode call 5985 * to turn on the display, so we do it here 5986 */ 5987 if (acrtc_state->stream->sink != aconnector->dc_sink) 5988 dm_force_atomic_commit(&aconnector->base); 5989 } 5990 5991 /* 5992 * Grabs all modesetting locks to serialize against any blocking commits, 5993 * Waits for completion of all non blocking commits. 5994 */ 5995 static int do_aquire_global_lock(struct drm_device *dev, 5996 struct drm_atomic_state *state) 5997 { 5998 struct drm_crtc *crtc; 5999 struct drm_crtc_commit *commit; 6000 long ret; 6001 6002 /* 6003 * Adding all modeset locks to aquire_ctx will 6004 * ensure that when the framework release it the 6005 * extra locks we are locking here will get released to 6006 */ 6007 ret = drm_modeset_lock_all_ctx(dev, state->acquire_ctx); 6008 if (ret) 6009 return ret; 6010 6011 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 6012 spin_lock(&crtc->commit_lock); 6013 commit = list_first_entry_or_null(&crtc->commit_list, 6014 struct drm_crtc_commit, commit_entry); 6015 if (commit) 6016 drm_crtc_commit_get(commit); 6017 spin_unlock(&crtc->commit_lock); 6018 6019 if (!commit) 6020 continue; 6021 6022 /* 6023 * Make sure all pending HW programming completed and 6024 * page flips done 6025 */ 6026 ret = wait_for_completion_interruptible_timeout(&commit->hw_done, 10*HZ); 6027 6028 if (ret > 0) 6029 ret = wait_for_completion_interruptible_timeout( 6030 &commit->flip_done, 10*HZ); 6031 6032 if (ret == 0) 6033 DRM_ERROR("[CRTC:%d:%s] hw_done or flip_done " 6034 "timed out\n", crtc->base.id, crtc->name); 6035 6036 drm_crtc_commit_put(commit); 6037 } 6038 6039 return ret < 0 ? ret : 0; 6040 } 6041 6042 static void get_freesync_config_for_crtc( 6043 struct dm_crtc_state *new_crtc_state, 6044 struct dm_connector_state *new_con_state) 6045 { 6046 struct mod_freesync_config config = {0}; 6047 struct amdgpu_dm_connector *aconnector = 6048 to_amdgpu_dm_connector(new_con_state->base.connector); 6049 struct drm_display_mode *mode = &new_crtc_state->base.mode; 6050 int vrefresh = drm_mode_vrefresh(mode); 6051 6052 new_crtc_state->vrr_supported = new_con_state->freesync_capable && 6053 vrefresh >= aconnector->min_vfreq && 6054 vrefresh <= aconnector->max_vfreq; 6055 6056 if (new_crtc_state->vrr_supported) { 6057 new_crtc_state->stream->ignore_msa_timing_param = true; 6058 config.state = new_crtc_state->base.vrr_enabled ? 6059 VRR_STATE_ACTIVE_VARIABLE : 6060 VRR_STATE_INACTIVE; 6061 config.min_refresh_in_uhz = 6062 aconnector->min_vfreq * 1000000; 6063 config.max_refresh_in_uhz = 6064 aconnector->max_vfreq * 1000000; 6065 config.vsif_supported = true; 6066 config.btr = true; 6067 } 6068 6069 new_crtc_state->freesync_config = config; 6070 } 6071 6072 static void reset_freesync_config_for_crtc( 6073 struct dm_crtc_state *new_crtc_state) 6074 { 6075 new_crtc_state->vrr_supported = false; 6076 6077 memset(&new_crtc_state->vrr_params, 0, 6078 sizeof(new_crtc_state->vrr_params)); 6079 memset(&new_crtc_state->vrr_infopacket, 0, 6080 sizeof(new_crtc_state->vrr_infopacket)); 6081 } 6082 6083 static int dm_update_crtc_state(struct amdgpu_display_manager *dm, 6084 struct drm_atomic_state *state, 6085 struct drm_crtc *crtc, 6086 struct drm_crtc_state *old_crtc_state, 6087 struct drm_crtc_state *new_crtc_state, 6088 bool enable, 6089 bool *lock_and_validation_needed) 6090 { 6091 struct dm_atomic_state *dm_state = NULL; 6092 struct dm_crtc_state *dm_old_crtc_state, *dm_new_crtc_state; 6093 struct dc_stream_state *new_stream; 6094 int ret = 0; 6095 6096 /* 6097 * TODO Move this code into dm_crtc_atomic_check once we get rid of dc_validation_set 6098 * update changed items 6099 */ 6100 struct amdgpu_crtc *acrtc = NULL; 6101 struct amdgpu_dm_connector *aconnector = NULL; 6102 struct drm_connector_state *drm_new_conn_state = NULL, *drm_old_conn_state = NULL; 6103 struct dm_connector_state *dm_new_conn_state = NULL, *dm_old_conn_state = NULL; 6104 6105 new_stream = NULL; 6106 6107 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 6108 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 6109 acrtc = to_amdgpu_crtc(crtc); 6110 aconnector = amdgpu_dm_find_first_crtc_matching_connector(state, crtc); 6111 6112 /* TODO This hack should go away */ 6113 if (aconnector && enable) { 6114 /* Make sure fake sink is created in plug-in scenario */ 6115 drm_new_conn_state = drm_atomic_get_new_connector_state(state, 6116 &aconnector->base); 6117 drm_old_conn_state = drm_atomic_get_old_connector_state(state, 6118 &aconnector->base); 6119 6120 if (IS_ERR(drm_new_conn_state)) { 6121 ret = PTR_ERR_OR_ZERO(drm_new_conn_state); 6122 goto fail; 6123 } 6124 6125 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state); 6126 dm_old_conn_state = to_dm_connector_state(drm_old_conn_state); 6127 6128 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 6129 goto skip_modeset; 6130 6131 new_stream = create_stream_for_sink(aconnector, 6132 &new_crtc_state->mode, 6133 dm_new_conn_state, 6134 dm_old_crtc_state->stream); 6135 6136 /* 6137 * we can have no stream on ACTION_SET if a display 6138 * was disconnected during S3, in this case it is not an 6139 * error, the OS will be updated after detection, and 6140 * will do the right thing on next atomic commit 6141 */ 6142 6143 if (!new_stream) { 6144 DRM_DEBUG_DRIVER("%s: Failed to create new stream for crtc %d\n", 6145 __func__, acrtc->base.base.id); 6146 ret = -ENOMEM; 6147 goto fail; 6148 } 6149 6150 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level; 6151 6152 if (dc_is_stream_unchanged(new_stream, dm_old_crtc_state->stream) && 6153 dc_is_stream_scaling_unchanged(new_stream, dm_old_crtc_state->stream)) { 6154 new_crtc_state->mode_changed = false; 6155 DRM_DEBUG_DRIVER("Mode change not required, setting mode_changed to %d", 6156 new_crtc_state->mode_changed); 6157 } 6158 } 6159 6160 /* mode_changed flag may get updated above, need to check again */ 6161 if (!drm_atomic_crtc_needs_modeset(new_crtc_state)) 6162 goto skip_modeset; 6163 6164 DRM_DEBUG_DRIVER( 6165 "amdgpu_crtc id:%d crtc_state_flags: enable:%d, active:%d, " 6166 "planes_changed:%d, mode_changed:%d,active_changed:%d," 6167 "connectors_changed:%d\n", 6168 acrtc->crtc_id, 6169 new_crtc_state->enable, 6170 new_crtc_state->active, 6171 new_crtc_state->planes_changed, 6172 new_crtc_state->mode_changed, 6173 new_crtc_state->active_changed, 6174 new_crtc_state->connectors_changed); 6175 6176 /* Remove stream for any changed/disabled CRTC */ 6177 if (!enable) { 6178 6179 if (!dm_old_crtc_state->stream) 6180 goto skip_modeset; 6181 6182 ret = dm_atomic_get_state(state, &dm_state); 6183 if (ret) 6184 goto fail; 6185 6186 DRM_DEBUG_DRIVER("Disabling DRM crtc: %d\n", 6187 crtc->base.id); 6188 6189 /* i.e. reset mode */ 6190 if (dc_remove_stream_from_ctx( 6191 dm->dc, 6192 dm_state->context, 6193 dm_old_crtc_state->stream) != DC_OK) { 6194 ret = -EINVAL; 6195 goto fail; 6196 } 6197 6198 dc_stream_release(dm_old_crtc_state->stream); 6199 dm_new_crtc_state->stream = NULL; 6200 6201 reset_freesync_config_for_crtc(dm_new_crtc_state); 6202 6203 *lock_and_validation_needed = true; 6204 6205 } else {/* Add stream for any updated/enabled CRTC */ 6206 /* 6207 * Quick fix to prevent NULL pointer on new_stream when 6208 * added MST connectors not found in existing crtc_state in the chained mode 6209 * TODO: need to dig out the root cause of that 6210 */ 6211 if (!aconnector || (!aconnector->dc_sink && aconnector->mst_port)) 6212 goto skip_modeset; 6213 6214 if (modereset_required(new_crtc_state)) 6215 goto skip_modeset; 6216 6217 if (modeset_required(new_crtc_state, new_stream, 6218 dm_old_crtc_state->stream)) { 6219 6220 WARN_ON(dm_new_crtc_state->stream); 6221 6222 ret = dm_atomic_get_state(state, &dm_state); 6223 if (ret) 6224 goto fail; 6225 6226 dm_new_crtc_state->stream = new_stream; 6227 6228 dc_stream_retain(new_stream); 6229 6230 DRM_DEBUG_DRIVER("Enabling DRM crtc: %d\n", 6231 crtc->base.id); 6232 6233 if (dc_add_stream_to_ctx( 6234 dm->dc, 6235 dm_state->context, 6236 dm_new_crtc_state->stream) != DC_OK) { 6237 ret = -EINVAL; 6238 goto fail; 6239 } 6240 6241 *lock_and_validation_needed = true; 6242 } 6243 } 6244 6245 skip_modeset: 6246 /* Release extra reference */ 6247 if (new_stream) 6248 dc_stream_release(new_stream); 6249 6250 /* 6251 * We want to do dc stream updates that do not require a 6252 * full modeset below. 6253 */ 6254 if (!(enable && aconnector && new_crtc_state->enable && 6255 new_crtc_state->active)) 6256 return 0; 6257 /* 6258 * Given above conditions, the dc state cannot be NULL because: 6259 * 1. We're in the process of enabling CRTCs (just been added 6260 * to the dc context, or already is on the context) 6261 * 2. Has a valid connector attached, and 6262 * 3. Is currently active and enabled. 6263 * => The dc stream state currently exists. 6264 */ 6265 BUG_ON(dm_new_crtc_state->stream == NULL); 6266 6267 /* Scaling or underscan settings */ 6268 if (is_scaling_state_different(dm_old_conn_state, dm_new_conn_state)) 6269 update_stream_scaling_settings( 6270 &new_crtc_state->mode, dm_new_conn_state, dm_new_crtc_state->stream); 6271 6272 /* ABM settings */ 6273 dm_new_crtc_state->abm_level = dm_new_conn_state->abm_level; 6274 6275 /* 6276 * Color management settings. We also update color properties 6277 * when a modeset is needed, to ensure it gets reprogrammed. 6278 */ 6279 if (dm_new_crtc_state->base.color_mgmt_changed || 6280 drm_atomic_crtc_needs_modeset(new_crtc_state)) { 6281 ret = amdgpu_dm_set_regamma_lut(dm_new_crtc_state); 6282 if (ret) 6283 goto fail; 6284 amdgpu_dm_set_ctm(dm_new_crtc_state); 6285 } 6286 6287 /* Update Freesync settings. */ 6288 get_freesync_config_for_crtc(dm_new_crtc_state, 6289 dm_new_conn_state); 6290 6291 return ret; 6292 6293 fail: 6294 if (new_stream) 6295 dc_stream_release(new_stream); 6296 return ret; 6297 } 6298 6299 static bool should_reset_plane(struct drm_atomic_state *state, 6300 struct drm_plane *plane, 6301 struct drm_plane_state *old_plane_state, 6302 struct drm_plane_state *new_plane_state) 6303 { 6304 struct drm_plane *other; 6305 struct drm_plane_state *old_other_state, *new_other_state; 6306 struct drm_crtc_state *new_crtc_state; 6307 int i; 6308 6309 /* 6310 * TODO: Remove this hack once the checks below are sufficient 6311 * enough to determine when we need to reset all the planes on 6312 * the stream. 6313 */ 6314 if (state->allow_modeset) 6315 return true; 6316 6317 /* Exit early if we know that we're adding or removing the plane. */ 6318 if (old_plane_state->crtc != new_plane_state->crtc) 6319 return true; 6320 6321 /* old crtc == new_crtc == NULL, plane not in context. */ 6322 if (!new_plane_state->crtc) 6323 return false; 6324 6325 new_crtc_state = 6326 drm_atomic_get_new_crtc_state(state, new_plane_state->crtc); 6327 6328 if (!new_crtc_state) 6329 return true; 6330 6331 if (drm_atomic_crtc_needs_modeset(new_crtc_state)) 6332 return true; 6333 6334 /* 6335 * If there are any new primary or overlay planes being added or 6336 * removed then the z-order can potentially change. To ensure 6337 * correct z-order and pipe acquisition the current DC architecture 6338 * requires us to remove and recreate all existing planes. 6339 * 6340 * TODO: Come up with a more elegant solution for this. 6341 */ 6342 for_each_oldnew_plane_in_state(state, other, old_other_state, new_other_state, i) { 6343 if (other->type == DRM_PLANE_TYPE_CURSOR) 6344 continue; 6345 6346 if (old_other_state->crtc != new_plane_state->crtc && 6347 new_other_state->crtc != new_plane_state->crtc) 6348 continue; 6349 6350 if (old_other_state->crtc != new_other_state->crtc) 6351 return true; 6352 6353 /* TODO: Remove this once we can handle fast format changes. */ 6354 if (old_other_state->fb && new_other_state->fb && 6355 old_other_state->fb->format != new_other_state->fb->format) 6356 return true; 6357 } 6358 6359 return false; 6360 } 6361 6362 static int dm_update_plane_state(struct dc *dc, 6363 struct drm_atomic_state *state, 6364 struct drm_plane *plane, 6365 struct drm_plane_state *old_plane_state, 6366 struct drm_plane_state *new_plane_state, 6367 bool enable, 6368 bool *lock_and_validation_needed) 6369 { 6370 6371 struct dm_atomic_state *dm_state = NULL; 6372 struct drm_crtc *new_plane_crtc, *old_plane_crtc; 6373 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 6374 struct dm_crtc_state *dm_new_crtc_state, *dm_old_crtc_state; 6375 struct dm_plane_state *dm_new_plane_state, *dm_old_plane_state; 6376 bool needs_reset; 6377 int ret = 0; 6378 6379 6380 new_plane_crtc = new_plane_state->crtc; 6381 old_plane_crtc = old_plane_state->crtc; 6382 dm_new_plane_state = to_dm_plane_state(new_plane_state); 6383 dm_old_plane_state = to_dm_plane_state(old_plane_state); 6384 6385 /*TODO Implement atomic check for cursor plane */ 6386 if (plane->type == DRM_PLANE_TYPE_CURSOR) 6387 return 0; 6388 6389 needs_reset = should_reset_plane(state, plane, old_plane_state, 6390 new_plane_state); 6391 6392 /* Remove any changed/removed planes */ 6393 if (!enable) { 6394 if (!needs_reset) 6395 return 0; 6396 6397 if (!old_plane_crtc) 6398 return 0; 6399 6400 old_crtc_state = drm_atomic_get_old_crtc_state( 6401 state, old_plane_crtc); 6402 dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); 6403 6404 if (!dm_old_crtc_state->stream) 6405 return 0; 6406 6407 DRM_DEBUG_ATOMIC("Disabling DRM plane: %d on DRM crtc %d\n", 6408 plane->base.id, old_plane_crtc->base.id); 6409 6410 ret = dm_atomic_get_state(state, &dm_state); 6411 if (ret) 6412 return ret; 6413 6414 if (!dc_remove_plane_from_context( 6415 dc, 6416 dm_old_crtc_state->stream, 6417 dm_old_plane_state->dc_state, 6418 dm_state->context)) { 6419 6420 ret = EINVAL; 6421 return ret; 6422 } 6423 6424 6425 dc_plane_state_release(dm_old_plane_state->dc_state); 6426 dm_new_plane_state->dc_state = NULL; 6427 6428 *lock_and_validation_needed = true; 6429 6430 } else { /* Add new planes */ 6431 struct dc_plane_state *dc_new_plane_state; 6432 6433 if (drm_atomic_plane_disabling(plane->state, new_plane_state)) 6434 return 0; 6435 6436 if (!new_plane_crtc) 6437 return 0; 6438 6439 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_crtc); 6440 dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); 6441 6442 if (!dm_new_crtc_state->stream) 6443 return 0; 6444 6445 if (!needs_reset) 6446 return 0; 6447 6448 WARN_ON(dm_new_plane_state->dc_state); 6449 6450 dc_new_plane_state = dc_create_plane_state(dc); 6451 if (!dc_new_plane_state) 6452 return -ENOMEM; 6453 6454 DRM_DEBUG_DRIVER("Enabling DRM plane: %d on DRM crtc %d\n", 6455 plane->base.id, new_plane_crtc->base.id); 6456 6457 ret = fill_dc_plane_attributes( 6458 new_plane_crtc->dev->dev_private, 6459 dc_new_plane_state, 6460 new_plane_state, 6461 new_crtc_state); 6462 if (ret) { 6463 dc_plane_state_release(dc_new_plane_state); 6464 return ret; 6465 } 6466 6467 ret = dm_atomic_get_state(state, &dm_state); 6468 if (ret) { 6469 dc_plane_state_release(dc_new_plane_state); 6470 return ret; 6471 } 6472 6473 /* 6474 * Any atomic check errors that occur after this will 6475 * not need a release. The plane state will be attached 6476 * to the stream, and therefore part of the atomic 6477 * state. It'll be released when the atomic state is 6478 * cleaned. 6479 */ 6480 if (!dc_add_plane_to_context( 6481 dc, 6482 dm_new_crtc_state->stream, 6483 dc_new_plane_state, 6484 dm_state->context)) { 6485 6486 dc_plane_state_release(dc_new_plane_state); 6487 return -EINVAL; 6488 } 6489 6490 dm_new_plane_state->dc_state = dc_new_plane_state; 6491 6492 /* Tell DC to do a full surface update every time there 6493 * is a plane change. Inefficient, but works for now. 6494 */ 6495 dm_new_plane_state->dc_state->update_flags.bits.full_update = 1; 6496 6497 *lock_and_validation_needed = true; 6498 } 6499 6500 6501 return ret; 6502 } 6503 6504 static int 6505 dm_determine_update_type_for_commit(struct amdgpu_display_manager *dm, 6506 struct drm_atomic_state *state, 6507 enum surface_update_type *out_type) 6508 { 6509 struct dc *dc = dm->dc; 6510 struct dm_atomic_state *dm_state = NULL, *old_dm_state = NULL; 6511 int i, j, num_plane, ret = 0; 6512 struct drm_plane_state *old_plane_state, *new_plane_state; 6513 struct dm_plane_state *new_dm_plane_state, *old_dm_plane_state; 6514 struct drm_crtc *new_plane_crtc, *old_plane_crtc; 6515 struct drm_plane *plane; 6516 6517 struct drm_crtc *crtc; 6518 struct drm_crtc_state *new_crtc_state, *old_crtc_state; 6519 struct dm_crtc_state *new_dm_crtc_state, *old_dm_crtc_state; 6520 struct dc_stream_status *status = NULL; 6521 6522 struct dc_surface_update *updates; 6523 enum surface_update_type update_type = UPDATE_TYPE_FAST; 6524 6525 updates = kcalloc(MAX_SURFACES, sizeof(*updates), GFP_KERNEL); 6526 6527 if (!updates) { 6528 DRM_ERROR("Failed to allocate plane updates\n"); 6529 /* Set type to FULL to avoid crashing in DC*/ 6530 update_type = UPDATE_TYPE_FULL; 6531 goto cleanup; 6532 } 6533 6534 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6535 struct dc_scaling_info scaling_info; 6536 struct dc_stream_update stream_update; 6537 6538 memset(&stream_update, 0, sizeof(stream_update)); 6539 6540 new_dm_crtc_state = to_dm_crtc_state(new_crtc_state); 6541 old_dm_crtc_state = to_dm_crtc_state(old_crtc_state); 6542 num_plane = 0; 6543 6544 if (new_dm_crtc_state->stream != old_dm_crtc_state->stream) { 6545 update_type = UPDATE_TYPE_FULL; 6546 goto cleanup; 6547 } 6548 6549 if (!new_dm_crtc_state->stream) 6550 continue; 6551 6552 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, j) { 6553 new_plane_crtc = new_plane_state->crtc; 6554 old_plane_crtc = old_plane_state->crtc; 6555 new_dm_plane_state = to_dm_plane_state(new_plane_state); 6556 old_dm_plane_state = to_dm_plane_state(old_plane_state); 6557 6558 if (plane->type == DRM_PLANE_TYPE_CURSOR) 6559 continue; 6560 6561 if (new_dm_plane_state->dc_state != old_dm_plane_state->dc_state) { 6562 update_type = UPDATE_TYPE_FULL; 6563 goto cleanup; 6564 } 6565 6566 if (crtc != new_plane_crtc) 6567 continue; 6568 6569 updates[num_plane].surface = new_dm_plane_state->dc_state; 6570 6571 if (new_crtc_state->mode_changed) { 6572 stream_update.dst = new_dm_crtc_state->stream->dst; 6573 stream_update.src = new_dm_crtc_state->stream->src; 6574 } 6575 6576 if (new_crtc_state->color_mgmt_changed) { 6577 updates[num_plane].gamma = 6578 new_dm_plane_state->dc_state->gamma_correction; 6579 updates[num_plane].in_transfer_func = 6580 new_dm_plane_state->dc_state->in_transfer_func; 6581 stream_update.gamut_remap = 6582 &new_dm_crtc_state->stream->gamut_remap_matrix; 6583 stream_update.out_transfer_func = 6584 new_dm_crtc_state->stream->out_transfer_func; 6585 } 6586 6587 ret = fill_dc_scaling_info(new_plane_state, 6588 &scaling_info); 6589 if (ret) 6590 goto cleanup; 6591 6592 updates[num_plane].scaling_info = &scaling_info; 6593 6594 num_plane++; 6595 } 6596 6597 if (num_plane == 0) 6598 continue; 6599 6600 ret = dm_atomic_get_state(state, &dm_state); 6601 if (ret) 6602 goto cleanup; 6603 6604 old_dm_state = dm_atomic_get_old_state(state); 6605 if (!old_dm_state) { 6606 ret = -EINVAL; 6607 goto cleanup; 6608 } 6609 6610 status = dc_stream_get_status_from_state(old_dm_state->context, 6611 new_dm_crtc_state->stream); 6612 6613 /* 6614 * TODO: DC modifies the surface during this call so we need 6615 * to lock here - find a way to do this without locking. 6616 */ 6617 mutex_lock(&dm->dc_lock); 6618 update_type = dc_check_update_surfaces_for_stream(dc, updates, num_plane, 6619 &stream_update, status); 6620 mutex_unlock(&dm->dc_lock); 6621 6622 if (update_type > UPDATE_TYPE_MED) { 6623 update_type = UPDATE_TYPE_FULL; 6624 goto cleanup; 6625 } 6626 } 6627 6628 cleanup: 6629 kfree(updates); 6630 6631 *out_type = update_type; 6632 return ret; 6633 } 6634 6635 /** 6636 * amdgpu_dm_atomic_check() - Atomic check implementation for AMDgpu DM. 6637 * @dev: The DRM device 6638 * @state: The atomic state to commit 6639 * 6640 * Validate that the given atomic state is programmable by DC into hardware. 6641 * This involves constructing a &struct dc_state reflecting the new hardware 6642 * state we wish to commit, then querying DC to see if it is programmable. It's 6643 * important not to modify the existing DC state. Otherwise, atomic_check 6644 * may unexpectedly commit hardware changes. 6645 * 6646 * When validating the DC state, it's important that the right locks are 6647 * acquired. For full updates case which removes/adds/updates streams on one 6648 * CRTC while flipping on another CRTC, acquiring global lock will guarantee 6649 * that any such full update commit will wait for completion of any outstanding 6650 * flip using DRMs synchronization events. See 6651 * dm_determine_update_type_for_commit() 6652 * 6653 * Note that DM adds the affected connectors for all CRTCs in state, when that 6654 * might not seem necessary. This is because DC stream creation requires the 6655 * DC sink, which is tied to the DRM connector state. Cleaning this up should 6656 * be possible but non-trivial - a possible TODO item. 6657 * 6658 * Return: -Error code if validation failed. 6659 */ 6660 static int amdgpu_dm_atomic_check(struct drm_device *dev, 6661 struct drm_atomic_state *state) 6662 { 6663 struct amdgpu_device *adev = dev->dev_private; 6664 struct dm_atomic_state *dm_state = NULL; 6665 struct dc *dc = adev->dm.dc; 6666 struct drm_connector *connector; 6667 struct drm_connector_state *old_con_state, *new_con_state; 6668 struct drm_crtc *crtc; 6669 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 6670 struct drm_plane *plane; 6671 struct drm_plane_state *old_plane_state, *new_plane_state; 6672 enum surface_update_type update_type = UPDATE_TYPE_FAST; 6673 enum surface_update_type overall_update_type = UPDATE_TYPE_FAST; 6674 6675 int ret, i; 6676 6677 /* 6678 * This bool will be set for true for any modeset/reset 6679 * or plane update which implies non fast surface update. 6680 */ 6681 bool lock_and_validation_needed = false; 6682 6683 ret = drm_atomic_helper_check_modeset(dev, state); 6684 if (ret) 6685 goto fail; 6686 6687 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6688 if (!drm_atomic_crtc_needs_modeset(new_crtc_state) && 6689 !new_crtc_state->color_mgmt_changed && 6690 old_crtc_state->vrr_enabled == new_crtc_state->vrr_enabled) 6691 continue; 6692 6693 if (!new_crtc_state->enable) 6694 continue; 6695 6696 ret = drm_atomic_add_affected_connectors(state, crtc); 6697 if (ret) 6698 return ret; 6699 6700 ret = drm_atomic_add_affected_planes(state, crtc); 6701 if (ret) 6702 goto fail; 6703 } 6704 6705 /* 6706 * Add all primary and overlay planes on the CRTC to the state 6707 * whenever a plane is enabled to maintain correct z-ordering 6708 * and to enable fast surface updates. 6709 */ 6710 drm_for_each_crtc(crtc, dev) { 6711 bool modified = false; 6712 6713 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { 6714 if (plane->type == DRM_PLANE_TYPE_CURSOR) 6715 continue; 6716 6717 if (new_plane_state->crtc == crtc || 6718 old_plane_state->crtc == crtc) { 6719 modified = true; 6720 break; 6721 } 6722 } 6723 6724 if (!modified) 6725 continue; 6726 6727 drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) { 6728 if (plane->type == DRM_PLANE_TYPE_CURSOR) 6729 continue; 6730 6731 new_plane_state = 6732 drm_atomic_get_plane_state(state, plane); 6733 6734 if (IS_ERR(new_plane_state)) { 6735 ret = PTR_ERR(new_plane_state); 6736 goto fail; 6737 } 6738 } 6739 } 6740 6741 /* Remove exiting planes if they are modified */ 6742 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) { 6743 ret = dm_update_plane_state(dc, state, plane, 6744 old_plane_state, 6745 new_plane_state, 6746 false, 6747 &lock_and_validation_needed); 6748 if (ret) 6749 goto fail; 6750 } 6751 6752 /* Disable all crtcs which require disable */ 6753 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6754 ret = dm_update_crtc_state(&adev->dm, state, crtc, 6755 old_crtc_state, 6756 new_crtc_state, 6757 false, 6758 &lock_and_validation_needed); 6759 if (ret) 6760 goto fail; 6761 } 6762 6763 /* Enable all crtcs which require enable */ 6764 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 6765 ret = dm_update_crtc_state(&adev->dm, state, crtc, 6766 old_crtc_state, 6767 new_crtc_state, 6768 true, 6769 &lock_and_validation_needed); 6770 if (ret) 6771 goto fail; 6772 } 6773 6774 /* Add new/modified planes */ 6775 for_each_oldnew_plane_in_state_reverse(state, plane, old_plane_state, new_plane_state, i) { 6776 ret = dm_update_plane_state(dc, state, plane, 6777 old_plane_state, 6778 new_plane_state, 6779 true, 6780 &lock_and_validation_needed); 6781 if (ret) 6782 goto fail; 6783 } 6784 6785 /* Run this here since we want to validate the streams we created */ 6786 ret = drm_atomic_helper_check_planes(dev, state); 6787 if (ret) 6788 goto fail; 6789 6790 /* Check scaling and underscan changes*/ 6791 /* TODO Removed scaling changes validation due to inability to commit 6792 * new stream into context w\o causing full reset. Need to 6793 * decide how to handle. 6794 */ 6795 for_each_oldnew_connector_in_state(state, connector, old_con_state, new_con_state, i) { 6796 struct dm_connector_state *dm_old_con_state = to_dm_connector_state(old_con_state); 6797 struct dm_connector_state *dm_new_con_state = to_dm_connector_state(new_con_state); 6798 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(dm_new_con_state->base.crtc); 6799 6800 /* Skip any modesets/resets */ 6801 if (!acrtc || drm_atomic_crtc_needs_modeset( 6802 drm_atomic_get_new_crtc_state(state, &acrtc->base))) 6803 continue; 6804 6805 /* Skip any thing not scale or underscan changes */ 6806 if (!is_scaling_state_different(dm_new_con_state, dm_old_con_state)) 6807 continue; 6808 6809 overall_update_type = UPDATE_TYPE_FULL; 6810 lock_and_validation_needed = true; 6811 } 6812 6813 ret = dm_determine_update_type_for_commit(&adev->dm, state, &update_type); 6814 if (ret) 6815 goto fail; 6816 6817 if (overall_update_type < update_type) 6818 overall_update_type = update_type; 6819 6820 /* 6821 * lock_and_validation_needed was an old way to determine if we need to set 6822 * the global lock. Leaving it in to check if we broke any corner cases 6823 * lock_and_validation_needed true = UPDATE_TYPE_FULL or UPDATE_TYPE_MED 6824 * lock_and_validation_needed false = UPDATE_TYPE_FAST 6825 */ 6826 if (lock_and_validation_needed && overall_update_type <= UPDATE_TYPE_FAST) 6827 WARN(1, "Global lock should be Set, overall_update_type should be UPDATE_TYPE_MED or UPDATE_TYPE_FULL"); 6828 6829 if (overall_update_type > UPDATE_TYPE_FAST) { 6830 ret = dm_atomic_get_state(state, &dm_state); 6831 if (ret) 6832 goto fail; 6833 6834 ret = do_aquire_global_lock(dev, state); 6835 if (ret) 6836 goto fail; 6837 6838 if (dc_validate_global_state(dc, dm_state->context, false) != DC_OK) { 6839 ret = -EINVAL; 6840 goto fail; 6841 } 6842 } else if (state->legacy_cursor_update) { 6843 /* 6844 * This is a fast cursor update coming from the plane update 6845 * helper, check if it can be done asynchronously for better 6846 * performance. 6847 */ 6848 state->async_update = !drm_atomic_helper_async_check(dev, state); 6849 } 6850 6851 /* Must be success */ 6852 WARN_ON(ret); 6853 return ret; 6854 6855 fail: 6856 if (ret == -EDEADLK) 6857 DRM_DEBUG_DRIVER("Atomic check stopped to avoid deadlock.\n"); 6858 else if (ret == -EINTR || ret == -EAGAIN || ret == -ERESTARTSYS) 6859 DRM_DEBUG_DRIVER("Atomic check stopped due to signal.\n"); 6860 else 6861 DRM_DEBUG_DRIVER("Atomic check failed with err: %d \n", ret); 6862 6863 return ret; 6864 } 6865 6866 static bool is_dp_capable_without_timing_msa(struct dc *dc, 6867 struct amdgpu_dm_connector *amdgpu_dm_connector) 6868 { 6869 uint8_t dpcd_data; 6870 bool capable = false; 6871 6872 if (amdgpu_dm_connector->dc_link && 6873 dm_helpers_dp_read_dpcd( 6874 NULL, 6875 amdgpu_dm_connector->dc_link, 6876 DP_DOWN_STREAM_PORT_COUNT, 6877 &dpcd_data, 6878 sizeof(dpcd_data))) { 6879 capable = (dpcd_data & DP_MSA_TIMING_PAR_IGNORED) ? true:false; 6880 } 6881 6882 return capable; 6883 } 6884 void amdgpu_dm_update_freesync_caps(struct drm_connector *connector, 6885 struct edid *edid) 6886 { 6887 int i; 6888 bool edid_check_required; 6889 struct detailed_timing *timing; 6890 struct detailed_non_pixel *data; 6891 struct detailed_data_monitor_range *range; 6892 struct amdgpu_dm_connector *amdgpu_dm_connector = 6893 to_amdgpu_dm_connector(connector); 6894 struct dm_connector_state *dm_con_state = NULL; 6895 6896 struct drm_device *dev = connector->dev; 6897 struct amdgpu_device *adev = dev->dev_private; 6898 bool freesync_capable = false; 6899 6900 if (!connector->state) { 6901 DRM_ERROR("%s - Connector has no state", __func__); 6902 goto update; 6903 } 6904 6905 if (!edid) { 6906 dm_con_state = to_dm_connector_state(connector->state); 6907 6908 amdgpu_dm_connector->min_vfreq = 0; 6909 amdgpu_dm_connector->max_vfreq = 0; 6910 amdgpu_dm_connector->pixel_clock_mhz = 0; 6911 6912 goto update; 6913 } 6914 6915 dm_con_state = to_dm_connector_state(connector->state); 6916 6917 edid_check_required = false; 6918 if (!amdgpu_dm_connector->dc_sink) { 6919 DRM_ERROR("dc_sink NULL, could not add free_sync module.\n"); 6920 goto update; 6921 } 6922 if (!adev->dm.freesync_module) 6923 goto update; 6924 /* 6925 * if edid non zero restrict freesync only for dp and edp 6926 */ 6927 if (edid) { 6928 if (amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_DISPLAY_PORT 6929 || amdgpu_dm_connector->dc_sink->sink_signal == SIGNAL_TYPE_EDP) { 6930 edid_check_required = is_dp_capable_without_timing_msa( 6931 adev->dm.dc, 6932 amdgpu_dm_connector); 6933 } 6934 } 6935 if (edid_check_required == true && (edid->version > 1 || 6936 (edid->version == 1 && edid->revision > 1))) { 6937 for (i = 0; i < 4; i++) { 6938 6939 timing = &edid->detailed_timings[i]; 6940 data = &timing->data.other_data; 6941 range = &data->data.range; 6942 /* 6943 * Check if monitor has continuous frequency mode 6944 */ 6945 if (data->type != EDID_DETAIL_MONITOR_RANGE) 6946 continue; 6947 /* 6948 * Check for flag range limits only. If flag == 1 then 6949 * no additional timing information provided. 6950 * Default GTF, GTF Secondary curve and CVT are not 6951 * supported 6952 */ 6953 if (range->flags != 1) 6954 continue; 6955 6956 amdgpu_dm_connector->min_vfreq = range->min_vfreq; 6957 amdgpu_dm_connector->max_vfreq = range->max_vfreq; 6958 amdgpu_dm_connector->pixel_clock_mhz = 6959 range->pixel_clock_mhz * 10; 6960 break; 6961 } 6962 6963 if (amdgpu_dm_connector->max_vfreq - 6964 amdgpu_dm_connector->min_vfreq > 10) { 6965 6966 freesync_capable = true; 6967 } 6968 } 6969 6970 update: 6971 if (dm_con_state) 6972 dm_con_state->freesync_capable = freesync_capable; 6973 6974 if (connector->vrr_capable_property) 6975 drm_connector_set_vrr_capable_property(connector, 6976 freesync_capable); 6977 } 6978 6979