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