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