1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright 2012-15 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: AMD 24 * 25 */ 26 27 #include <linux/vmalloc.h> 28 #include <drm/display/drm_dp_helper.h> 29 #include <drm/display/drm_dp_mst_helper.h> 30 #include <drm/drm_atomic.h> 31 #include <drm/drm_atomic_helper.h> 32 #include <drm/drm_fixed.h> 33 #include <drm/drm_edid.h> 34 #include "dm_services.h" 35 #include "amdgpu.h" 36 #include "amdgpu_dm.h" 37 #include "amdgpu_dm_mst_types.h" 38 #include "amdgpu_dm_hdcp.h" 39 40 #include "dc.h" 41 #include "dm_helpers.h" 42 43 #include "ddc_service_types.h" 44 #include "dpcd_defs.h" 45 46 #include "dmub_cmd.h" 47 #if defined(CONFIG_DEBUG_FS) 48 #include "amdgpu_dm_debugfs.h" 49 #endif 50 51 #include "dc/resource/dcn20/dcn20_resource.h" 52 53 #define PEAK_FACTOR_X1000 1006 54 55 /* 56 * This function handles both native AUX and I2C-Over-AUX transactions. 57 */ 58 static ssize_t dm_dp_aux_transfer(struct drm_dp_aux *aux, 59 struct drm_dp_aux_msg *msg) 60 { 61 ssize_t result = 0; 62 struct aux_payload payload; 63 enum aux_return_code_type operation_result; 64 struct amdgpu_device *adev; 65 struct ddc_service *ddc; 66 uint8_t copy[16]; 67 68 if (WARN_ON(msg->size > 16)) 69 return -E2BIG; 70 71 payload.address = msg->address; 72 payload.data = msg->buffer; 73 payload.length = msg->size; 74 payload.reply = &msg->reply; 75 payload.i2c_over_aux = (msg->request & DP_AUX_NATIVE_WRITE) == 0; 76 payload.write = (msg->request & DP_AUX_I2C_READ) == 0; 77 payload.mot = (msg->request & DP_AUX_I2C_MOT) != 0; 78 payload.write_status_update = 79 (msg->request & DP_AUX_I2C_WRITE_STATUS_UPDATE) != 0; 80 payload.defer_delay = 0; 81 82 if (payload.write) { 83 memcpy(copy, msg->buffer, msg->size); 84 payload.data = copy; 85 } 86 87 result = dc_link_aux_transfer_raw(TO_DM_AUX(aux)->ddc_service, &payload, 88 &operation_result); 89 90 /* 91 * w/a on certain intel platform where hpd is unexpected to pull low during 92 * 1st sideband message transaction by return AUX_RET_ERROR_HPD_DISCON 93 * aux transaction is succuess in such case, therefore bypass the error 94 */ 95 ddc = TO_DM_AUX(aux)->ddc_service; 96 adev = ddc->ctx->driver_context; 97 if (adev->dm.aux_hpd_discon_quirk) { 98 if (msg->address == DP_SIDEBAND_MSG_DOWN_REQ_BASE && 99 operation_result == AUX_RET_ERROR_HPD_DISCON) { 100 result = msg->size; 101 operation_result = AUX_RET_SUCCESS; 102 } 103 } 104 105 /* 106 * result equals to 0 includes the cases of AUX_DEFER/I2C_DEFER 107 */ 108 if (payload.write && result >= 0) { 109 if (result) { 110 /*one byte indicating partially written bytes*/ 111 drm_dbg_dp(adev_to_drm(adev), "AUX partially written\n"); 112 result = payload.data[0]; 113 } else if (!payload.reply[0]) 114 /*I2C_ACK|AUX_ACK*/ 115 result = msg->size; 116 } 117 118 if (result < 0) { 119 switch (operation_result) { 120 case AUX_RET_SUCCESS: 121 break; 122 case AUX_RET_ERROR_HPD_DISCON: 123 case AUX_RET_ERROR_UNKNOWN: 124 case AUX_RET_ERROR_INVALID_OPERATION: 125 case AUX_RET_ERROR_PROTOCOL_ERROR: 126 result = -EIO; 127 break; 128 case AUX_RET_ERROR_INVALID_REPLY: 129 case AUX_RET_ERROR_ENGINE_ACQUIRE: 130 result = -EBUSY; 131 break; 132 case AUX_RET_ERROR_TIMEOUT: 133 result = -ETIMEDOUT; 134 break; 135 } 136 137 drm_dbg_dp(adev_to_drm(adev), "DP AUX transfer fail:%d\n", operation_result); 138 } 139 140 if (payload.reply[0]) 141 drm_dbg_dp(adev_to_drm(adev), "AUX reply command not ACK: 0x%02x.", 142 payload.reply[0]); 143 144 return result; 145 } 146 147 static void 148 dm_dp_mst_connector_destroy(struct drm_connector *connector) 149 { 150 struct amdgpu_dm_connector *aconnector = 151 to_amdgpu_dm_connector(connector); 152 153 if (aconnector->dc_sink) { 154 dc_link_remove_remote_sink(aconnector->dc_link, 155 aconnector->dc_sink); 156 dc_sink_release(aconnector->dc_sink); 157 } 158 159 drm_edid_free(aconnector->drm_edid); 160 161 drm_connector_cleanup(connector); 162 drm_dp_mst_put_port_malloc(aconnector->mst_output_port); 163 kfree(aconnector); 164 } 165 166 static int 167 amdgpu_dm_mst_connector_late_register(struct drm_connector *connector) 168 { 169 struct amdgpu_dm_connector *amdgpu_dm_connector = 170 to_amdgpu_dm_connector(connector); 171 int r; 172 173 r = drm_dp_mst_connector_late_register(connector, 174 amdgpu_dm_connector->mst_output_port); 175 if (r < 0) 176 return r; 177 178 #if defined(CONFIG_DEBUG_FS) 179 connector_debugfs_init(amdgpu_dm_connector); 180 #endif 181 182 return 0; 183 } 184 185 186 static inline void 187 amdgpu_dm_mst_reset_mst_connector_setting(struct amdgpu_dm_connector *aconnector) 188 { 189 aconnector->drm_edid = NULL; 190 aconnector->dsc_aux = NULL; 191 aconnector->mst_output_port->passthrough_aux = NULL; 192 aconnector->mst_local_bw = 0; 193 aconnector->vc_full_pbn = 0; 194 } 195 196 static void 197 amdgpu_dm_mst_connector_early_unregister(struct drm_connector *connector) 198 { 199 struct amdgpu_dm_connector *aconnector = 200 to_amdgpu_dm_connector(connector); 201 struct drm_dp_mst_port *port = aconnector->mst_output_port; 202 struct amdgpu_dm_connector *root = aconnector->mst_root; 203 struct dc_link *dc_link = aconnector->dc_link; 204 struct dc_sink *dc_sink = aconnector->dc_sink; 205 206 drm_dp_mst_connector_early_unregister(connector, port); 207 208 /* 209 * Release dc_sink for connector which its attached port is 210 * no longer in the mst topology 211 */ 212 drm_modeset_lock(&root->mst_mgr.base.lock, NULL); 213 if (dc_sink) { 214 if (dc_link->sink_count) 215 dc_link_remove_remote_sink(dc_link, dc_sink); 216 217 drm_dbg_dp(connector->dev, 218 "DM_MST: remove remote sink 0x%p, %d remaining\n", 219 dc_sink, dc_link->sink_count); 220 221 dc_sink_release(dc_sink); 222 aconnector->dc_sink = NULL; 223 amdgpu_dm_mst_reset_mst_connector_setting(aconnector); 224 } 225 226 aconnector->mst_status = MST_STATUS_DEFAULT; 227 drm_modeset_unlock(&root->mst_mgr.base.lock); 228 } 229 230 static const struct drm_connector_funcs dm_dp_mst_connector_funcs = { 231 .fill_modes = drm_helper_probe_single_connector_modes, 232 .destroy = dm_dp_mst_connector_destroy, 233 .reset = amdgpu_dm_connector_funcs_reset, 234 .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state, 235 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 236 .atomic_set_property = amdgpu_dm_connector_atomic_set_property, 237 .atomic_get_property = amdgpu_dm_connector_atomic_get_property, 238 .late_register = amdgpu_dm_mst_connector_late_register, 239 .early_unregister = amdgpu_dm_mst_connector_early_unregister, 240 }; 241 242 bool needs_dsc_aux_workaround(struct dc_link *link) 243 { 244 if (link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_90CC24 && 245 (link->dpcd_caps.dpcd_rev.raw == DPCD_REV_14 || link->dpcd_caps.dpcd_rev.raw == DPCD_REV_12) && 246 link->dpcd_caps.sink_count.bits.SINK_COUNT >= 2) 247 return true; 248 249 return false; 250 } 251 252 #if defined(CONFIG_DRM_AMD_DC_FP) 253 static bool is_synaptics_cascaded_panamera(struct dc_link *link, struct drm_dp_mst_port *port) 254 { 255 u8 branch_vendor_data[4] = { 0 }; // Vendor data 0x50C ~ 0x50F 256 257 if (drm_dp_dpcd_read(port->mgr->aux, DP_BRANCH_VENDOR_SPECIFIC_START, &branch_vendor_data, 4) == 4) { 258 if (link->dpcd_caps.branch_dev_id == DP_BRANCH_DEVICE_ID_90CC24 && 259 IS_SYNAPTICS_CASCADED_PANAMERA(link->dpcd_caps.branch_dev_name, branch_vendor_data)) { 260 DRM_INFO("Synaptics Cascaded MST hub\n"); 261 return true; 262 } 263 } 264 265 return false; 266 } 267 268 static bool validate_dsc_caps_on_connector(struct amdgpu_dm_connector *aconnector) 269 { 270 struct dc_sink *dc_sink = aconnector->dc_sink; 271 struct drm_dp_mst_port *port = aconnector->mst_output_port; 272 u8 dsc_caps[16] = { 0 }; 273 u8 dsc_branch_dec_caps_raw[3] = { 0 }; // DSC branch decoder caps 0xA0 ~ 0xA2 274 u8 *dsc_branch_dec_caps = NULL; 275 276 aconnector->dsc_aux = drm_dp_mst_dsc_aux_for_port(port); 277 278 /* 279 * drm_dp_mst_dsc_aux_for_port() will return NULL for certain configs 280 * because it only check the dsc/fec caps of the "port variable" and not the dock 281 * 282 * This case will return NULL: DSC capabe MST dock connected to a non fec/dsc capable display 283 * 284 * Workaround: explicitly check the use case above and use the mst dock's aux as dsc_aux 285 * 286 */ 287 if (!aconnector->dsc_aux && !port->parent->port_parent && 288 needs_dsc_aux_workaround(aconnector->dc_link)) 289 aconnector->dsc_aux = &aconnector->mst_root->dm_dp_aux.aux; 290 291 /* synaptics cascaded MST hub case */ 292 if (is_synaptics_cascaded_panamera(aconnector->dc_link, port)) 293 aconnector->dsc_aux = port->mgr->aux; 294 295 if (!aconnector->dsc_aux) 296 return false; 297 298 if (drm_dp_dpcd_read(aconnector->dsc_aux, DP_DSC_SUPPORT, dsc_caps, 16) < 0) 299 return false; 300 301 if (drm_dp_dpcd_read(aconnector->dsc_aux, 302 DP_DSC_BRANCH_OVERALL_THROUGHPUT_0, dsc_branch_dec_caps_raw, 3) == 3) 303 dsc_branch_dec_caps = dsc_branch_dec_caps_raw; 304 305 if (!dc_dsc_parse_dsc_dpcd(aconnector->dc_link->ctx->dc, 306 dsc_caps, dsc_branch_dec_caps, 307 &dc_sink->dsc_caps.dsc_dec_caps)) 308 return false; 309 310 return true; 311 } 312 #endif 313 314 static bool retrieve_downstream_port_device(struct amdgpu_dm_connector *aconnector) 315 { 316 union dp_downstream_port_present ds_port_present; 317 318 if (!aconnector->dsc_aux) 319 return false; 320 321 if (drm_dp_dpcd_read(aconnector->dsc_aux, DP_DOWNSTREAMPORT_PRESENT, &ds_port_present, 1) < 0) { 322 DRM_INFO("Failed to read downstream_port_present 0x05 from DFP of branch device\n"); 323 return false; 324 } 325 326 aconnector->mst_downstream_port_present = ds_port_present; 327 DRM_INFO("Downstream port present %d, type %d\n", 328 ds_port_present.fields.PORT_PRESENT, ds_port_present.fields.PORT_TYPE); 329 330 return true; 331 } 332 333 static bool retrieve_branch_specific_data(struct amdgpu_dm_connector *aconnector) 334 { 335 struct drm_connector *connector = &aconnector->base; 336 struct drm_dp_mst_port *port = aconnector->mst_output_port; 337 struct drm_dp_mst_port *port_parent; 338 struct drm_dp_aux *immediate_upstream_aux; 339 struct drm_dp_desc branch_desc; 340 341 if (!port->parent) 342 return false; 343 344 port_parent = port->parent->port_parent; 345 346 immediate_upstream_aux = port_parent ? &port_parent->aux : port->mgr->aux; 347 348 if (drm_dp_read_desc(immediate_upstream_aux, &branch_desc, true)) 349 return false; 350 351 aconnector->branch_ieee_oui = (branch_desc.ident.oui[0] << 16) + 352 (branch_desc.ident.oui[1] << 8) + 353 (branch_desc.ident.oui[2]); 354 355 drm_dbg_dp(port->aux.drm_dev, "MST branch oui 0x%x detected at %s\n", 356 aconnector->branch_ieee_oui, connector->name); 357 358 return true; 359 } 360 361 static int dm_dp_mst_get_modes(struct drm_connector *connector) 362 { 363 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 364 int ret = 0; 365 366 if (!aconnector) 367 return drm_add_edid_modes(connector, NULL); 368 369 if (!aconnector->drm_edid) { 370 const struct drm_edid *drm_edid; 371 372 drm_edid = drm_dp_mst_edid_read(connector, 373 &aconnector->mst_root->mst_mgr, 374 aconnector->mst_output_port); 375 376 if (!drm_edid) { 377 amdgpu_dm_set_mst_status(&aconnector->mst_status, 378 MST_REMOTE_EDID, false); 379 380 drm_edid_connector_update( 381 &aconnector->base, 382 NULL); 383 384 DRM_DEBUG_KMS("Can't get EDID of %s. Add default remote sink.", connector->name); 385 if (!aconnector->dc_sink) { 386 struct dc_sink *dc_sink; 387 struct dc_sink_init_data init_params = { 388 .link = aconnector->dc_link, 389 .sink_signal = SIGNAL_TYPE_DISPLAY_PORT_MST }; 390 391 dc_sink = dc_link_add_remote_sink( 392 aconnector->dc_link, 393 NULL, 394 0, 395 &init_params); 396 397 if (!dc_sink) { 398 DRM_ERROR("Unable to add a remote sink\n"); 399 return 0; 400 } 401 402 drm_dbg_dp(connector->dev, 403 "DM_MST: add remote sink 0x%p, %d remaining\n", 404 dc_sink, 405 aconnector->dc_link->sink_count); 406 407 dc_sink->priv = aconnector; 408 aconnector->dc_sink = dc_sink; 409 } 410 411 return ret; 412 } 413 414 aconnector->drm_edid = drm_edid; 415 amdgpu_dm_set_mst_status(&aconnector->mst_status, 416 MST_REMOTE_EDID, true); 417 } 418 419 if (aconnector->dc_sink && aconnector->dc_sink->sink_signal == SIGNAL_TYPE_VIRTUAL) { 420 dc_sink_release(aconnector->dc_sink); 421 aconnector->dc_sink = NULL; 422 } 423 424 if (!aconnector->dc_sink) { 425 struct dc_sink *dc_sink; 426 struct dc_sink_init_data init_params = { 427 .link = aconnector->dc_link, 428 .sink_signal = SIGNAL_TYPE_DISPLAY_PORT_MST }; 429 const struct edid *edid; 430 431 edid = drm_edid_raw(aconnector->drm_edid); // FIXME: Get rid of drm_edid_raw() 432 dc_sink = dc_link_add_remote_sink( 433 aconnector->dc_link, 434 (uint8_t *)edid, 435 (edid->extensions + 1) * EDID_LENGTH, 436 &init_params); 437 438 if (!dc_sink) { 439 DRM_ERROR("Unable to add a remote sink\n"); 440 return 0; 441 } 442 443 drm_dbg_dp(connector->dev, 444 "DM_MST: add remote sink 0x%p, %d remaining\n", 445 dc_sink, aconnector->dc_link->sink_count); 446 447 dc_sink->priv = aconnector; 448 /* dc_link_add_remote_sink returns a new reference */ 449 aconnector->dc_sink = dc_sink; 450 451 /* when display is unplugged from mst hub, connctor will be 452 * destroyed within dm_dp_mst_connector_destroy. connector 453 * hdcp perperties, like type, undesired, desired, enabled, 454 * will be lost. So, save hdcp properties into hdcp_work within 455 * amdgpu_dm_atomic_commit_tail. if the same display is 456 * plugged back with same display index, its hdcp properties 457 * will be retrieved from hdcp_work within dm_dp_mst_get_modes 458 */ 459 if (aconnector->dc_sink && connector->state) { 460 struct drm_device *dev = connector->dev; 461 struct amdgpu_device *adev = drm_to_adev(dev); 462 463 if (adev->dm.hdcp_workqueue) { 464 struct hdcp_workqueue *hdcp_work = adev->dm.hdcp_workqueue; 465 struct hdcp_workqueue *hdcp_w = 466 &hdcp_work[aconnector->dc_link->link_index]; 467 468 connector->state->hdcp_content_type = 469 hdcp_w->hdcp_content_type[connector->index]; 470 connector->state->content_protection = 471 hdcp_w->content_protection[connector->index]; 472 } 473 } 474 475 if (aconnector->dc_sink) { 476 amdgpu_dm_update_freesync_caps( 477 connector, aconnector->drm_edid, true); 478 479 #if defined(CONFIG_DRM_AMD_DC_FP) 480 if (!validate_dsc_caps_on_connector(aconnector)) 481 memset(&aconnector->dc_sink->dsc_caps, 482 0, sizeof(aconnector->dc_sink->dsc_caps)); 483 #endif 484 485 if (!retrieve_downstream_port_device(aconnector)) 486 memset(&aconnector->mst_downstream_port_present, 487 0, sizeof(aconnector->mst_downstream_port_present)); 488 } 489 } 490 491 drm_edid_connector_update(&aconnector->base, aconnector->drm_edid); 492 493 ret = drm_edid_connector_add_modes(connector); 494 495 return ret; 496 } 497 498 static struct drm_encoder * 499 dm_mst_atomic_best_encoder(struct drm_connector *connector, 500 struct drm_atomic_commit *state) 501 { 502 struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state, 503 connector); 504 struct amdgpu_device *adev = drm_to_adev(connector->dev); 505 struct amdgpu_crtc *acrtc = to_amdgpu_crtc(connector_state->crtc); 506 507 return &adev->dm.mst_encoders[acrtc->crtc_id].base; 508 } 509 510 static int 511 dm_dp_mst_detect(struct drm_connector *connector, 512 struct drm_modeset_acquire_ctx *ctx, bool force) 513 { 514 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 515 struct amdgpu_dm_connector *master = aconnector->mst_root; 516 struct drm_dp_mst_port *port = aconnector->mst_output_port; 517 int connection_status; 518 519 if (drm_connector_is_unregistered(connector)) 520 return connector_status_disconnected; 521 522 connection_status = drm_dp_mst_detect_port(connector, ctx, &master->mst_mgr, 523 aconnector->mst_output_port); 524 525 if (port->pdt != DP_PEER_DEVICE_NONE && !port->dpcd_rev) { 526 uint8_t dpcd_rev; 527 int ret; 528 529 ret = drm_dp_dpcd_readb(&port->aux, DP_DP13_DPCD_REV, &dpcd_rev); 530 531 if (ret == 1) { 532 port->dpcd_rev = dpcd_rev; 533 534 /* Could be DP1.2 DP Rx case*/ 535 if (!dpcd_rev) { 536 ret = drm_dp_dpcd_readb(&port->aux, DP_DPCD_REV, &dpcd_rev); 537 538 if (ret == 1) 539 port->dpcd_rev = dpcd_rev; 540 } 541 542 if (!dpcd_rev) 543 DRM_DEBUG_KMS("Can't decide DPCD revision number!"); 544 } 545 546 /* 547 * Could be legacy sink, logical port etc on DP1.2. 548 * Will get Nack under these cases when issue remote 549 * DPCD read. 550 */ 551 if (ret != 1) 552 DRM_DEBUG_KMS("Can't access DPCD"); 553 } else if (port->pdt == DP_PEER_DEVICE_NONE) { 554 port->dpcd_rev = 0; 555 } 556 557 /* 558 * Release dc_sink for connector which unplug event is notified by CSN msg 559 */ 560 if (connection_status == connector_status_disconnected && aconnector->dc_sink) { 561 if (aconnector->dc_link->sink_count) 562 dc_link_remove_remote_sink(aconnector->dc_link, aconnector->dc_sink); 563 564 drm_dbg_dp(connector->dev, 565 "DM_MST: remove remote sink 0x%p, %d remaining\n", 566 aconnector->dc_link, 567 aconnector->dc_link->sink_count); 568 569 dc_sink_release(aconnector->dc_sink); 570 aconnector->dc_sink = NULL; 571 amdgpu_dm_mst_reset_mst_connector_setting(aconnector); 572 573 amdgpu_dm_set_mst_status(&aconnector->mst_status, 574 MST_REMOTE_EDID | MST_ALLOCATE_NEW_PAYLOAD | MST_CLEAR_ALLOCATED_PAYLOAD, 575 false); 576 } 577 578 return connection_status; 579 } 580 581 static int dm_dp_mst_atomic_check(struct drm_connector *connector, 582 struct drm_atomic_commit *state) 583 { 584 struct amdgpu_dm_connector *aconnector = to_amdgpu_dm_connector(connector); 585 struct drm_dp_mst_topology_mgr *mst_mgr = &aconnector->mst_root->mst_mgr; 586 struct drm_dp_mst_port *mst_port = aconnector->mst_output_port; 587 588 return drm_dp_atomic_release_time_slots(state, mst_mgr, mst_port); 589 } 590 591 static const struct drm_connector_helper_funcs dm_dp_mst_connector_helper_funcs = { 592 .get_modes = dm_dp_mst_get_modes, 593 .mode_valid = amdgpu_dm_connector_mode_valid, 594 .atomic_best_encoder = dm_mst_atomic_best_encoder, 595 .detect_ctx = dm_dp_mst_detect, 596 .atomic_check = dm_dp_mst_atomic_check, 597 }; 598 599 static void amdgpu_dm_encoder_destroy(struct drm_encoder *encoder) 600 { 601 drm_encoder_cleanup(encoder); 602 } 603 604 static const struct drm_encoder_funcs amdgpu_dm_encoder_funcs = { 605 .destroy = amdgpu_dm_encoder_destroy, 606 }; 607 608 void 609 dm_dp_create_fake_mst_encoders(struct amdgpu_device *adev) 610 { 611 struct drm_device *dev = adev_to_drm(adev); 612 int i; 613 614 for (i = 0; i < adev->dm.display_indexes_num; i++) { 615 struct amdgpu_encoder *amdgpu_encoder = &adev->dm.mst_encoders[i]; 616 struct drm_encoder *encoder = &amdgpu_encoder->base; 617 618 encoder->possible_crtcs = amdgpu_dm_get_encoder_crtc_mask(adev); 619 620 drm_encoder_init( 621 dev, 622 &amdgpu_encoder->base, 623 &amdgpu_dm_encoder_funcs, 624 DRM_MODE_ENCODER_DPMST, 625 NULL); 626 627 drm_encoder_helper_add(encoder, &amdgpu_dm_encoder_helper_funcs); 628 } 629 } 630 631 static struct drm_connector * 632 dm_dp_add_mst_connector(struct drm_dp_mst_topology_mgr *mgr, 633 struct drm_dp_mst_port *port, 634 const char *pathprop) 635 { 636 struct amdgpu_dm_connector *master = container_of(mgr, struct amdgpu_dm_connector, mst_mgr); 637 struct drm_device *dev = master->base.dev; 638 struct amdgpu_device *adev = drm_to_adev(dev); 639 struct amdgpu_dm_connector *aconnector; 640 struct drm_connector *connector; 641 int i; 642 643 aconnector = kzalloc_obj(*aconnector); 644 if (!aconnector) 645 return NULL; 646 647 DRM_DEBUG_DRIVER("%s: Create aconnector 0x%p for port 0x%p\n", __func__, aconnector, port); 648 649 connector = &aconnector->base; 650 aconnector->mst_output_port = port; 651 aconnector->mst_root = master; 652 amdgpu_dm_set_mst_status(&aconnector->mst_status, 653 MST_PROBE, true); 654 655 if (drm_connector_dynamic_init( 656 dev, 657 connector, 658 &dm_dp_mst_connector_funcs, 659 DRM_MODE_CONNECTOR_DisplayPort, 660 NULL)) { 661 kfree(aconnector); 662 return NULL; 663 } 664 drm_connector_helper_add(connector, &dm_dp_mst_connector_helper_funcs); 665 666 amdgpu_dm_connector_init_helper( 667 &adev->dm, 668 aconnector, 669 DRM_MODE_CONNECTOR_DisplayPort, 670 master->dc_link, 671 master->connector_id); 672 673 for (i = 0; i < adev->dm.display_indexes_num; i++) { 674 drm_connector_attach_encoder(&aconnector->base, 675 &adev->dm.mst_encoders[i].base); 676 } 677 678 connector->max_bpc_property = master->base.max_bpc_property; 679 if (connector->max_bpc_property) 680 drm_connector_attach_max_bpc_property(connector, 8, 16); 681 682 connector->vrr_capable_property = master->base.vrr_capable_property; 683 if (connector->vrr_capable_property) 684 drm_connector_attach_vrr_capable_property(connector); 685 686 drm_object_attach_property( 687 &connector->base, 688 dev->mode_config.path_property, 689 0); 690 drm_object_attach_property( 691 &connector->base, 692 dev->mode_config.tile_property, 693 0); 694 connector->colorspace_property = master->base.colorspace_property; 695 if (connector->colorspace_property) 696 drm_connector_attach_colorspace_property(connector); 697 698 drm_connector_set_path_property(connector, pathprop); 699 700 if (!retrieve_branch_specific_data(aconnector)) 701 aconnector->branch_ieee_oui = 0; 702 703 /* 704 * Initialize connector state before adding the connectror to drm and 705 * framebuffer lists 706 */ 707 amdgpu_dm_connector_funcs_reset(connector); 708 709 drm_dp_mst_get_port_malloc(port); 710 711 return connector; 712 } 713 714 void dm_handle_mst_sideband_msg_ready_event( 715 struct drm_dp_mst_topology_mgr *mgr, 716 enum mst_msg_ready_type msg_rdy_type) 717 { 718 uint8_t esi[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = { 0 }; 719 uint8_t dret; 720 bool new_irq_handled = false; 721 int dpcd_addr; 722 uint8_t dpcd_bytes_to_read; 723 const uint8_t max_process_count = 30; 724 uint8_t process_count = 0; 725 u8 retry; 726 struct amdgpu_dm_connector *aconnector = 727 container_of(mgr, struct amdgpu_dm_connector, mst_mgr); 728 729 730 const struct dc_link_status *link_status = dc_link_get_status(aconnector->dc_link); 731 732 if (link_status->dpcd_caps->dpcd_rev.raw < 0x12) { 733 dpcd_bytes_to_read = DP_LANE0_1_STATUS - DP_SINK_COUNT; 734 /* DPCD 0x200 - 0x201 for downstream IRQ */ 735 dpcd_addr = DP_SINK_COUNT; 736 } else { 737 dpcd_bytes_to_read = DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI; 738 /* DPCD 0x2002 - 0x2005 for downstream IRQ */ 739 dpcd_addr = DP_SINK_COUNT_ESI; 740 } 741 742 mutex_lock(&aconnector->handle_mst_msg_ready); 743 744 while (process_count < max_process_count) { 745 u8 ack[DP_PSR_ERROR_STATUS - DP_SINK_COUNT_ESI] = {}; 746 747 process_count++; 748 749 dret = drm_dp_dpcd_read( 750 &aconnector->dm_dp_aux.aux, 751 dpcd_addr, 752 esi, 753 dpcd_bytes_to_read); 754 755 if (dret != dpcd_bytes_to_read) { 756 DRM_DEBUG_KMS("DPCD read and acked number is not as expected!"); 757 break; 758 } 759 760 DRM_DEBUG_DRIVER("ESI %02x %02x %02x\n", esi[0], esi[1], esi[2]); 761 762 switch (msg_rdy_type) { 763 case DOWN_REP_MSG_RDY_EVENT: 764 /* Only handle DOWN_REP_MSG_RDY case*/ 765 esi[1] &= DP_DOWN_REP_MSG_RDY; 766 break; 767 case UP_REQ_MSG_RDY_EVENT: 768 /* Only handle UP_REQ_MSG_RDY case*/ 769 esi[1] &= DP_UP_REQ_MSG_RDY; 770 break; 771 default: 772 /* Handle both cases*/ 773 esi[1] &= (DP_DOWN_REP_MSG_RDY | DP_UP_REQ_MSG_RDY); 774 break; 775 } 776 777 if (!esi[1]) 778 break; 779 780 /* handle MST irq */ 781 if (aconnector->mst_mgr.mst_state) 782 drm_dp_mst_hpd_irq_handle_event(&aconnector->mst_mgr, 783 esi, 784 ack, 785 &new_irq_handled); 786 787 if (new_irq_handled) { 788 /* ACK at DPCD to notify down stream */ 789 for (retry = 0; retry < 3; retry++) { 790 ssize_t wret; 791 792 wret = drm_dp_dpcd_writeb(&aconnector->dm_dp_aux.aux, 793 dpcd_addr + 1, 794 ack[1]); 795 if (wret == 1) 796 break; 797 } 798 799 if (retry == 3) { 800 DRM_ERROR("Failed to ack MST event.\n"); 801 break; 802 } 803 804 drm_dp_mst_hpd_irq_send_new_request(&aconnector->mst_mgr); 805 806 new_irq_handled = false; 807 } else { 808 break; 809 } 810 } 811 812 mutex_unlock(&aconnector->handle_mst_msg_ready); 813 814 if (process_count == max_process_count) 815 DRM_DEBUG_DRIVER("Loop exceeded max iterations\n"); 816 } 817 818 static void dm_handle_mst_down_rep_msg_ready(struct drm_dp_mst_topology_mgr *mgr) 819 { 820 dm_handle_mst_sideband_msg_ready_event(mgr, DOWN_REP_MSG_RDY_EVENT); 821 } 822 823 static const struct drm_dp_mst_topology_cbs dm_mst_cbs = { 824 .add_connector = dm_dp_add_mst_connector, 825 .poll_hpd_irq = dm_handle_mst_down_rep_msg_ready, 826 }; 827 828 void amdgpu_dm_initialize_dp_connector(struct amdgpu_display_manager *dm, 829 struct amdgpu_dm_connector *aconnector, 830 int link_index) 831 { 832 struct dc_link_settings max_link_enc_cap = {0}; 833 834 aconnector->dm_dp_aux.aux.name = 835 kasprintf(GFP_KERNEL, "AMDGPU DM aux hw bus %d", 836 link_index); 837 aconnector->dm_dp_aux.aux.transfer = dm_dp_aux_transfer; 838 aconnector->dm_dp_aux.aux.drm_dev = dm->ddev; 839 aconnector->dm_dp_aux.ddc_service = aconnector->dc_link->ddc; 840 841 drm_dp_aux_init(&aconnector->dm_dp_aux.aux); 842 drm_dp_cec_register_connector(&aconnector->dm_dp_aux.aux, 843 &aconnector->base); 844 drm_dp_dpcd_set_probe(&aconnector->dm_dp_aux.aux, false); 845 846 if (aconnector->base.connector_type == DRM_MODE_CONNECTOR_eDP) 847 return; 848 849 dc_link_dp_get_max_link_enc_cap(aconnector->dc_link, &max_link_enc_cap); 850 aconnector->mst_mgr.cbs = &dm_mst_cbs; 851 drm_dp_mst_topology_mgr_init(&aconnector->mst_mgr, adev_to_drm(dm->adev), 852 &aconnector->dm_dp_aux.aux, 16, 4, aconnector->connector_id); 853 854 drm_connector_attach_dp_subconnector_property(&aconnector->base); 855 } 856 857 uint32_t dm_mst_get_pbn_divider(struct dc_link *link) 858 { 859 uint32_t pbn_div_x100; 860 uint64_t dividend, divisor; 861 862 if (!link) 863 return 0; 864 865 dividend = (uint64_t)dc_link_bandwidth_kbps(link, dc_link_get_link_cap(link)) * 100; 866 divisor = 8 * 1000 * 54; 867 868 pbn_div_x100 = div64_u64(dividend, divisor); 869 870 return dfixed_const(pbn_div_x100) / 100; 871 } 872 873 struct dsc_mst_fairness_params { 874 struct dc_crtc_timing *timing; 875 struct dc_sink *sink; 876 struct dc_dsc_bw_range bw_range; 877 bool compression_possible; 878 struct drm_dp_mst_port *port; 879 enum dsc_clock_force_state clock_force_enable; 880 uint32_t num_slices_h; 881 uint32_t num_slices_v; 882 uint32_t bpp_overwrite; 883 struct amdgpu_dm_connector *aconnector; 884 }; 885 886 #if defined(CONFIG_DRM_AMD_DC_FP) 887 static uint16_t get_fec_overhead_multiplier(struct dc_link *dc_link) 888 { 889 u8 link_coding_cap; 890 uint16_t fec_overhead_multiplier_x1000 = PBN_FEC_OVERHEAD_MULTIPLIER_8B_10B; 891 892 link_coding_cap = dc_link_dp_mst_decide_link_encoding_format(dc_link); 893 if (link_coding_cap == DP_128b_132b_ENCODING) 894 fec_overhead_multiplier_x1000 = PBN_FEC_OVERHEAD_MULTIPLIER_128B_132B; 895 896 return fec_overhead_multiplier_x1000; 897 } 898 899 static int kbps_to_peak_pbn(int kbps, uint16_t fec_overhead_multiplier_x1000) 900 { 901 u64 peak_kbps = kbps; 902 903 peak_kbps *= 1006; 904 peak_kbps *= fec_overhead_multiplier_x1000; 905 peak_kbps = div_u64(peak_kbps, 1000 * 1000); 906 return (int) DIV64_U64_ROUND_UP(peak_kbps * 64, (54 * 8 * 1000)); 907 } 908 909 static void set_dsc_configs_from_fairness_vars(struct dsc_mst_fairness_params *params, 910 struct dsc_mst_fairness_vars *vars, 911 int count, 912 int k) 913 { 914 struct drm_connector *drm_connector; 915 int i; 916 struct dc_dsc_config_options dsc_options = {0}; 917 918 for (i = 0; i < count; i++) { 919 drm_connector = ¶ms[i].aconnector->base; 920 921 dc_dsc_get_default_config_option(params[i].sink->ctx->dc, &dsc_options); 922 dsc_options.max_target_bpp_limit_override_x16 = drm_connector->display_info.max_dsc_bpp * 16; 923 924 memset(¶ms[i].timing->dsc_cfg, 0, sizeof(params[i].timing->dsc_cfg)); 925 if (vars[i + k].dsc_enabled && dc_dsc_compute_config( 926 params[i].sink->ctx->dc->res_pool->dscs[0], 927 ¶ms[i].sink->dsc_caps.dsc_dec_caps, 928 &dsc_options, 929 0, 930 params[i].timing, 931 dc_link_get_highest_encoding_format(params[i].aconnector->dc_link), 932 ¶ms[i].timing->dsc_cfg)) { 933 params[i].timing->flags.DSC = 1; 934 935 if (params[i].bpp_overwrite) 936 params[i].timing->dsc_cfg.bits_per_pixel = params[i].bpp_overwrite; 937 else 938 params[i].timing->dsc_cfg.bits_per_pixel = vars[i + k].bpp_x16; 939 940 if (params[i].num_slices_h) 941 params[i].timing->dsc_cfg.num_slices_h = params[i].num_slices_h; 942 943 if (params[i].num_slices_v) 944 params[i].timing->dsc_cfg.num_slices_v = params[i].num_slices_v; 945 } else { 946 params[i].timing->flags.DSC = 0; 947 } 948 params[i].timing->dsc_cfg.mst_pbn = vars[i + k].pbn; 949 } 950 951 for (i = 0; i < count; i++) { 952 if (params[i].sink) { 953 if (params[i].sink->sink_signal != SIGNAL_TYPE_VIRTUAL && 954 params[i].sink->sink_signal != SIGNAL_TYPE_NONE) 955 DRM_DEBUG_DRIVER("MST_DSC %s i=%d dispname=%s\n", __func__, i, 956 params[i].sink->edid_caps.display_name); 957 } 958 959 DRM_DEBUG_DRIVER("MST_DSC dsc=%d bits_per_pixel=%d pbn=%d\n", 960 params[i].timing->flags.DSC, 961 params[i].timing->dsc_cfg.bits_per_pixel, 962 vars[i + k].pbn); 963 } 964 } 965 966 static int bpp_x16_from_pbn(struct dsc_mst_fairness_params param, int pbn) 967 { 968 struct dc_dsc_config dsc_config; 969 u64 kbps; 970 971 struct drm_connector *drm_connector = ¶m.aconnector->base; 972 struct dc_dsc_config_options dsc_options = {0}; 973 974 dc_dsc_get_default_config_option(param.sink->ctx->dc, &dsc_options); 975 dsc_options.max_target_bpp_limit_override_x16 = drm_connector->display_info.max_dsc_bpp * 16; 976 977 kbps = div_u64((u64)pbn * 994 * 8 * 54, 64); 978 dc_dsc_compute_config( 979 param.sink->ctx->dc->res_pool->dscs[0], 980 ¶m.sink->dsc_caps.dsc_dec_caps, 981 &dsc_options, 982 (int) kbps, param.timing, 983 dc_link_get_highest_encoding_format(param.aconnector->dc_link), 984 &dsc_config); 985 986 return dsc_config.bits_per_pixel; 987 } 988 989 static int increase_dsc_bpp(struct drm_atomic_commit *state, 990 struct drm_dp_mst_topology_state *mst_state, 991 struct dc_link *dc_link, 992 struct dsc_mst_fairness_params *params, 993 struct dsc_mst_fairness_vars *vars, 994 int count, 995 int k) 996 { 997 int i; 998 bool bpp_increased[MAX_PIPES]; 999 int initial_slack[MAX_PIPES]; 1000 int min_initial_slack; 1001 int next_index; 1002 int remaining_to_increase = 0; 1003 int link_timeslots_used; 1004 int fair_pbn_alloc; 1005 int ret = 0; 1006 uint16_t fec_overhead_multiplier_x1000 = get_fec_overhead_multiplier(dc_link); 1007 1008 for (i = 0; i < count; i++) { 1009 if (vars[i + k].dsc_enabled) { 1010 initial_slack[i] = 1011 kbps_to_peak_pbn(params[i].bw_range.max_kbps, fec_overhead_multiplier_x1000) - vars[i + k].pbn; 1012 bpp_increased[i] = false; 1013 remaining_to_increase += 1; 1014 } else { 1015 initial_slack[i] = 0; 1016 bpp_increased[i] = true; 1017 } 1018 } 1019 1020 while (remaining_to_increase) { 1021 next_index = -1; 1022 min_initial_slack = -1; 1023 for (i = 0; i < count; i++) { 1024 if (!bpp_increased[i]) { 1025 if (min_initial_slack == -1 || min_initial_slack > initial_slack[i]) { 1026 min_initial_slack = initial_slack[i]; 1027 next_index = i; 1028 } 1029 } 1030 } 1031 1032 if (next_index == -1) 1033 break; 1034 1035 link_timeslots_used = 0; 1036 1037 for (i = 0; i < count; i++) 1038 link_timeslots_used += DIV_ROUND_UP(vars[i + k].pbn, dfixed_trunc(mst_state->pbn_div)); 1039 1040 fair_pbn_alloc = 1041 (63 - link_timeslots_used) / remaining_to_increase * dfixed_trunc(mst_state->pbn_div); 1042 1043 if (initial_slack[next_index] > fair_pbn_alloc) { 1044 vars[next_index].pbn += fair_pbn_alloc; 1045 ret = drm_dp_atomic_find_time_slots(state, 1046 params[next_index].port->mgr, 1047 params[next_index].port, 1048 vars[next_index].pbn); 1049 if (ret < 0) 1050 return ret; 1051 1052 ret = drm_dp_mst_atomic_check(state); 1053 if (ret == 0) { 1054 vars[next_index].bpp_x16 = bpp_x16_from_pbn(params[next_index], vars[next_index].pbn); 1055 } else { 1056 vars[next_index].pbn -= fair_pbn_alloc; 1057 ret = drm_dp_atomic_find_time_slots(state, 1058 params[next_index].port->mgr, 1059 params[next_index].port, 1060 vars[next_index].pbn); 1061 if (ret < 0) 1062 return ret; 1063 } 1064 } else { 1065 vars[next_index].pbn += initial_slack[next_index]; 1066 ret = drm_dp_atomic_find_time_slots(state, 1067 params[next_index].port->mgr, 1068 params[next_index].port, 1069 vars[next_index].pbn); 1070 if (ret < 0) 1071 return ret; 1072 1073 ret = drm_dp_mst_atomic_check(state); 1074 if (ret == 0) { 1075 vars[next_index].bpp_x16 = params[next_index].bw_range.max_target_bpp_x16; 1076 } else { 1077 vars[next_index].pbn -= initial_slack[next_index]; 1078 ret = drm_dp_atomic_find_time_slots(state, 1079 params[next_index].port->mgr, 1080 params[next_index].port, 1081 vars[next_index].pbn); 1082 if (ret < 0) 1083 return ret; 1084 } 1085 } 1086 1087 bpp_increased[next_index] = true; 1088 remaining_to_increase--; 1089 } 1090 return 0; 1091 } 1092 1093 static int try_disable_dsc(struct drm_atomic_commit *state, 1094 struct dc_link *dc_link, 1095 struct dsc_mst_fairness_params *params, 1096 struct dsc_mst_fairness_vars *vars, 1097 int count, 1098 int k) 1099 { 1100 int i; 1101 bool tried[MAX_PIPES]; 1102 int kbps_increase[MAX_PIPES]; 1103 int max_kbps_increase; 1104 int next_index; 1105 int remaining_to_try = 0; 1106 int ret; 1107 uint16_t fec_overhead_multiplier_x1000 = get_fec_overhead_multiplier(dc_link); 1108 int var_pbn; 1109 1110 for (i = 0; i < count; i++) { 1111 if (vars[i + k].dsc_enabled 1112 && vars[i + k].bpp_x16 == params[i].bw_range.max_target_bpp_x16 1113 && params[i].clock_force_enable == DSC_CLK_FORCE_DEFAULT) { 1114 kbps_increase[i] = params[i].bw_range.stream_kbps - params[i].bw_range.max_kbps; 1115 tried[i] = false; 1116 remaining_to_try += 1; 1117 } else { 1118 kbps_increase[i] = 0; 1119 tried[i] = true; 1120 } 1121 } 1122 1123 while (remaining_to_try) { 1124 next_index = -1; 1125 max_kbps_increase = -1; 1126 for (i = 0; i < count; i++) { 1127 if (!tried[i]) { 1128 if (max_kbps_increase == -1 || max_kbps_increase < kbps_increase[i]) { 1129 max_kbps_increase = kbps_increase[i]; 1130 next_index = i; 1131 } 1132 } 1133 } 1134 1135 if (next_index == -1) 1136 break; 1137 1138 DRM_DEBUG_DRIVER("MST_DSC index #%d, try no compression\n", next_index); 1139 var_pbn = vars[next_index].pbn; 1140 vars[next_index].pbn = kbps_to_peak_pbn(params[next_index].bw_range.stream_kbps, fec_overhead_multiplier_x1000); 1141 ret = drm_dp_atomic_find_time_slots(state, 1142 params[next_index].port->mgr, 1143 params[next_index].port, 1144 vars[next_index].pbn); 1145 if (ret < 0) { 1146 DRM_DEBUG_DRIVER("%s:%d MST_DSC index #%d, failed to set pbn to the state, %d\n", 1147 __func__, __LINE__, next_index, ret); 1148 vars[next_index].pbn = var_pbn; 1149 return ret; 1150 } 1151 1152 ret = drm_dp_mst_atomic_check(state); 1153 if (ret == 0) { 1154 DRM_DEBUG_DRIVER("MST_DSC index #%d, greedily disable dsc\n", next_index); 1155 vars[next_index].dsc_enabled = false; 1156 vars[next_index].bpp_x16 = 0; 1157 } else { 1158 DRM_DEBUG_DRIVER("MST_DSC index #%d, restore optimized pbn value\n", next_index); 1159 vars[next_index].pbn = var_pbn; 1160 ret = drm_dp_atomic_find_time_slots(state, 1161 params[next_index].port->mgr, 1162 params[next_index].port, 1163 vars[next_index].pbn); 1164 if (ret < 0) { 1165 DRM_DEBUG_DRIVER("%s:%d MST_DSC index #%d, failed to set pbn to the state, %d\n", 1166 __func__, __LINE__, next_index, ret); 1167 return ret; 1168 } 1169 } 1170 1171 tried[next_index] = true; 1172 remaining_to_try--; 1173 } 1174 return 0; 1175 } 1176 1177 static bool get_conv_frl_bw(struct amdgpu_dm_connector *aconnector, 1178 uint32_t *bw_in_kbps, uint32_t *dsc_bw_in_kbps) 1179 { 1180 unsigned int max_conv_bw_in_kbps = 0; 1181 unsigned int max_sink_bw_in_kbps = 0; 1182 unsigned int dsc_max_sink_bw_in_kbps = 0; 1183 1184 if (aconnector->dc_link->dc->caps.dp_hdmi21_pcon_support && 1185 aconnector->mst_downstream_port_caps.bytes.byte0.bits.DWN_STRM_PORTX_TYPE == DOWN_STREAM_DETAILED_HDMI) { 1186 max_conv_bw_in_kbps = dc_link_bw_kbps_from_raw_frl_link_rate_data( 1187 aconnector->dc_link->dc, 1188 aconnector->mst_downstream_port_caps.bytes.byte2.bits.MAX_ENCODED_LINK_BW_SUPPORT); 1189 if (aconnector->dc_sink->edid_caps.max_frl_rate && max_conv_bw_in_kbps) { 1190 max_sink_bw_in_kbps = dc_link_bw_kbps_from_raw_frl_link_rate_data( 1191 aconnector->dc_link->dc, 1192 aconnector->dc_sink->edid_caps.max_frl_rate); 1193 dsc_max_sink_bw_in_kbps = dc_link_bw_kbps_from_raw_frl_link_rate_data( 1194 aconnector->dc_link->dc, 1195 aconnector->dc_sink->edid_caps.frl_dsc_max_frl_rate); 1196 1197 *bw_in_kbps = min(max_conv_bw_in_kbps, max_sink_bw_in_kbps); 1198 *dsc_bw_in_kbps = min(*bw_in_kbps, dsc_max_sink_bw_in_kbps); 1199 } 1200 } 1201 1202 return *bw_in_kbps > 0; // Frl endpoint is detected 1203 } 1204 1205 static void build_frl_mst_dsc_params(struct amdgpu_dm_connector *aconnector, 1206 struct dc_stream_state *stream, 1207 struct dc_dsc_policy *dsc_policy, 1208 struct dsc_mst_fairness_params *params, 1209 uint32_t frl_conv_dsc_bw_in_kbps) 1210 { 1211 uint32_t min_bpp_x16, max_bpp_x16; 1212 struct dc_dsc_config_options dsc_options = {0}; 1213 1214 min_bpp_x16 = dsc_policy->min_target_bpp * 16; 1215 max_bpp_x16 = dsc_policy->max_target_bpp * 16; 1216 1217 dc_dsc_get_default_config_option(stream->sink->ctx->dc, &dsc_options); 1218 dsc_options.max_target_bpp_limit_override_x16 = 1219 stream->sink->edid_caps.panel_patch.max_dsc_target_bpp_limit * 16; 1220 1221 if (dc_dsc_compute_config( 1222 stream->sink->ctx->dc->res_pool->dscs[0], 1223 &stream->sink->dsc_caps.dsc_dec_caps, 1224 &dsc_options, 1225 frl_conv_dsc_bw_in_kbps, 1226 &stream->timing, 1227 dc_link_get_highest_encoding_format(aconnector->dc_link), 1228 &stream->timing.dsc_cfg)) { 1229 // The timing can enable dsc 1230 if (stream->sink->dsc_caps.dsc_dec_caps.is_vic_all_bpp && min_bpp_x16 <= stream->timing.dsc_cfg.bits_per_pixel) { 1231 // with all supported bpp within the range limit 1232 params->bw_range.max_target_bpp_x16 = min(stream->timing.dsc_cfg.bits_per_pixel, dsc_policy->max_target_bpp * 16); 1233 params->bw_range.min_target_bpp_x16 = min_bpp_x16; 1234 params->bw_range.max_kbps = (params->bw_range.max_target_bpp_x16 * stream->timing.pix_clk_100hz + 159) / 160; 1235 params->bw_range.min_kbps = (params->bw_range.min_target_bpp_x16 * stream->timing.pix_clk_100hz + 159) / 160; 1236 } else if (!stream->sink->dsc_caps.dsc_dec_caps.is_vic_all_bpp && 1237 min_bpp_x16 <= stream->timing.dsc_cfg.bits_per_pixel && 1238 max_bpp_x16 >= stream->timing.dsc_cfg.bits_per_pixel) { 1239 // with selected bpp only within the range limit 1240 params->bw_range.max_target_bpp_x16 = stream->timing.dsc_cfg.bits_per_pixel; 1241 params->bw_range.max_kbps = (params->bw_range.max_target_bpp_x16 * stream->timing.pix_clk_100hz + 159) / 160; 1242 params->bw_range.min_target_bpp_x16 = params->bw_range.max_target_bpp_x16; 1243 params->bw_range.min_kbps = params->bw_range.max_kbps; 1244 } 1245 } 1246 } 1247 1248 static void log_dsc_params(int count, struct dsc_mst_fairness_vars *vars, int k) 1249 { 1250 int i; 1251 1252 for (i = 0; i < count; i++) 1253 DRM_DEBUG_DRIVER("MST_DSC DSC params: stream #%d --- dsc_enabled = %d, bpp_x16 = %d, pbn = %d\n", 1254 i, vars[i + k].dsc_enabled, vars[i + k].bpp_x16, vars[i + k].pbn); 1255 } 1256 1257 static int compute_mst_dsc_configs_for_link(struct drm_atomic_commit *state, 1258 struct dc_state *dc_state, 1259 struct dc_link *dc_link, 1260 struct dsc_mst_fairness_vars *vars, 1261 struct drm_dp_mst_topology_mgr *mgr, 1262 int *link_vars_start_index) 1263 { 1264 struct dc_stream_state *stream; 1265 struct dsc_mst_fairness_params params[MAX_PIPES]; 1266 struct amdgpu_dm_connector *aconnector; 1267 struct drm_dp_mst_topology_state *mst_state = drm_atomic_get_mst_topology_state(state, mgr); 1268 int count = 0; 1269 int i, k, ret; 1270 bool debugfs_overwrite = false; 1271 uint16_t fec_overhead_multiplier_x1000 = get_fec_overhead_multiplier(dc_link); 1272 struct drm_connector_state *new_conn_state; 1273 bool is_frl_endpoint_present; 1274 uint32_t frl_conv_bw_in_kbps, frl_conv_dsc_bw_in_kbps; 1275 1276 memset(params, 0, sizeof(params)); 1277 1278 if (IS_ERR(mst_state)) 1279 return PTR_ERR(mst_state); 1280 1281 /* Set up params */ 1282 DRM_DEBUG_DRIVER("%s: MST_DSC Try to set up params from %d streams\n", __func__, dc_state->stream_count); 1283 for (i = 0; i < dc_state->stream_count; i++) { 1284 struct dc_dsc_policy dsc_policy = {0}; 1285 1286 stream = dc_state->streams[i]; 1287 1288 if (stream->link != dc_link) 1289 continue; 1290 1291 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context; 1292 if (!aconnector) 1293 continue; 1294 1295 if (!aconnector->mst_output_port) 1296 continue; 1297 1298 new_conn_state = drm_atomic_get_new_connector_state(state, &aconnector->base); 1299 1300 if (!new_conn_state) { 1301 DRM_DEBUG_DRIVER("%s:%d MST_DSC Skip the stream 0x%p with invalid new_conn_state\n", 1302 __func__, __LINE__, stream); 1303 continue; 1304 } 1305 1306 stream->timing.flags.DSC = 0; 1307 1308 params[count].timing = &stream->timing; 1309 params[count].sink = stream->sink; 1310 params[count].aconnector = aconnector; 1311 params[count].port = aconnector->mst_output_port; 1312 params[count].clock_force_enable = aconnector->dsc_settings.dsc_force_enable; 1313 if (params[count].clock_force_enable == DSC_CLK_FORCE_ENABLE) 1314 debugfs_overwrite = true; 1315 params[count].num_slices_h = aconnector->dsc_settings.dsc_num_slices_h; 1316 params[count].num_slices_v = aconnector->dsc_settings.dsc_num_slices_v; 1317 params[count].bpp_overwrite = aconnector->dsc_settings.dsc_bits_per_pixel; 1318 params[count].compression_possible = stream->sink->dsc_caps.dsc_dec_caps.is_dsc_supported; 1319 dc_dsc_get_policy_for_timing(params[count].timing, 0, &dsc_policy, dc_link_get_highest_encoding_format(stream->link)); 1320 is_frl_endpoint_present = get_conv_frl_bw(aconnector, &frl_conv_bw_in_kbps, &frl_conv_dsc_bw_in_kbps); 1321 if (stream->sink->dsc_caps.dsc_dec_caps.is_dsc_supported && 1322 is_frl_endpoint_present && 1323 frl_conv_dsc_bw_in_kbps && 1324 stream->sink->dsc_caps.dsc_dec_caps.is_frl) 1325 build_frl_mst_dsc_params(aconnector, stream, &dsc_policy, ¶ms[count], frl_conv_dsc_bw_in_kbps); 1326 if (!dc_dsc_compute_bandwidth_range( 1327 stream->sink->ctx->dc->res_pool->dscs[0], 1328 stream->sink->ctx->dc->debug.dsc_min_slice_height_override, 1329 dsc_policy.min_target_bpp * 16, 1330 dsc_policy.max_target_bpp * 16, 1331 &stream->sink->dsc_caps.dsc_dec_caps, 1332 &stream->timing, 1333 dc_link_get_highest_encoding_format(dc_link), 1334 ¶ms[count].bw_range)) 1335 params[count].bw_range.stream_kbps = dc_bandwidth_in_kbps_from_timing(&stream->timing, 1336 dc_link_get_highest_encoding_format(dc_link)); 1337 1338 DRM_DEBUG_DRIVER("MST_DSC #%d stream 0x%p - max_kbps = %u, min_kbps = %u, uncompressed_kbps = %u\n", 1339 count, stream, params[count].bw_range.max_kbps, params[count].bw_range.min_kbps, 1340 params[count].bw_range.stream_kbps); 1341 count++; 1342 } 1343 1344 DRM_DEBUG_DRIVER("%s: MST_DSC Params set up for %d streams\n", __func__, count); 1345 1346 if (count == 0) { 1347 ASSERT(0); 1348 return 0; 1349 } 1350 1351 /* k is start index of vars for current phy link used by mst hub */ 1352 k = *link_vars_start_index; 1353 /* set vars start index for next mst hub phy link */ 1354 *link_vars_start_index += count; 1355 1356 /* Try no compression */ 1357 DRM_DEBUG_DRIVER("MST_DSC Try no compression\n"); 1358 for (i = 0; i < count; i++) { 1359 vars[i + k].aconnector = params[i].aconnector; 1360 vars[i + k].pbn = kbps_to_peak_pbn(params[i].bw_range.stream_kbps, fec_overhead_multiplier_x1000); 1361 vars[i + k].dsc_enabled = false; 1362 vars[i + k].bpp_x16 = 0; 1363 ret = drm_dp_atomic_find_time_slots(state, params[i].port->mgr, params[i].port, 1364 vars[i + k].pbn); 1365 if (ret < 0) 1366 return ret; 1367 } 1368 ret = drm_dp_mst_atomic_check(state); 1369 if (ret == 0 && !debugfs_overwrite) { 1370 set_dsc_configs_from_fairness_vars(params, vars, count, k); 1371 return 0; 1372 } else if (ret != -ENOSPC) { 1373 return ret; 1374 } 1375 1376 log_dsc_params(count, vars, k); 1377 1378 /* Try max compression */ 1379 DRM_DEBUG_DRIVER("MST_DSC Try max compression\n"); 1380 for (i = 0; i < count; i++) { 1381 if (params[i].compression_possible && params[i].clock_force_enable != DSC_CLK_FORCE_DISABLE) { 1382 vars[i + k].pbn = kbps_to_peak_pbn(params[i].bw_range.min_kbps, fec_overhead_multiplier_x1000); 1383 vars[i + k].dsc_enabled = true; 1384 vars[i + k].bpp_x16 = params[i].bw_range.min_target_bpp_x16; 1385 ret = drm_dp_atomic_find_time_slots(state, params[i].port->mgr, 1386 params[i].port, vars[i + k].pbn); 1387 if (ret < 0) 1388 return ret; 1389 } else { 1390 vars[i + k].pbn = kbps_to_peak_pbn(params[i].bw_range.stream_kbps, fec_overhead_multiplier_x1000); 1391 vars[i + k].dsc_enabled = false; 1392 vars[i + k].bpp_x16 = 0; 1393 ret = drm_dp_atomic_find_time_slots(state, params[i].port->mgr, 1394 params[i].port, vars[i + k].pbn); 1395 if (ret < 0) 1396 return ret; 1397 } 1398 } 1399 ret = drm_dp_mst_atomic_check(state); 1400 if (ret != 0) 1401 return ret; 1402 1403 log_dsc_params(count, vars, k); 1404 1405 /* Optimize degree of compression */ 1406 DRM_DEBUG_DRIVER("MST_DSC Try optimize compression\n"); 1407 ret = increase_dsc_bpp(state, mst_state, dc_link, params, vars, count, k); 1408 if (ret < 0) { 1409 DRM_DEBUG_DRIVER("MST_DSC Failed to optimize compression\n"); 1410 return ret; 1411 } 1412 1413 log_dsc_params(count, vars, k); 1414 1415 DRM_DEBUG_DRIVER("MST_DSC Try disable compression\n"); 1416 ret = try_disable_dsc(state, dc_link, params, vars, count, k); 1417 if (ret < 0) { 1418 DRM_DEBUG_DRIVER("MST_DSC Failed to disable compression\n"); 1419 return ret; 1420 } 1421 1422 log_dsc_params(count, vars, k); 1423 1424 set_dsc_configs_from_fairness_vars(params, vars, count, k); 1425 1426 return 0; 1427 } 1428 1429 static bool is_dsc_need_re_compute( 1430 struct drm_atomic_commit *state, 1431 struct dc_state *dc_state, 1432 struct dc_link *dc_link) 1433 { 1434 int i, j; 1435 bool is_dsc_need_re_compute = false; 1436 struct amdgpu_dm_connector *stream_on_link[MAX_PIPES]; 1437 int new_stream_on_link_num = 0; 1438 struct amdgpu_dm_connector *aconnector; 1439 struct dc_stream_state *stream; 1440 const struct dc *dc = dc_link->dc; 1441 1442 /* only check phy used by dsc mst branch */ 1443 if (dc_link->type != dc_connection_mst_branch) 1444 goto out; 1445 1446 /* add a check for older MST DSC with no virtual DPCDs */ 1447 if (needs_dsc_aux_workaround(dc_link) && 1448 (!(dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_SUPPORT || 1449 dc_link->dpcd_caps.dsc_caps.dsc_basic_caps.fields.dsc_support.DSC_PASSTHROUGH_SUPPORT))) 1450 goto out; 1451 1452 for (i = 0; i < MAX_PIPES; i++) 1453 stream_on_link[i] = NULL; 1454 1455 DRM_DEBUG_DRIVER("%s: MST_DSC check on %d streams in new dc_state\n", __func__, dc_state->stream_count); 1456 1457 /* check if there is mode change in new request */ 1458 for (i = 0; i < dc_state->stream_count; i++) { 1459 struct drm_crtc_state *new_crtc_state; 1460 struct drm_connector_state *new_conn_state; 1461 1462 stream = dc_state->streams[i]; 1463 if (!stream) 1464 continue; 1465 1466 DRM_DEBUG_DRIVER("%s:%d MST_DSC checking #%d stream 0x%p\n", __func__, __LINE__, i, stream); 1467 1468 /* check if stream using the same link for mst */ 1469 if (stream->link != dc_link) 1470 continue; 1471 1472 aconnector = (struct amdgpu_dm_connector *) stream->dm_stream_context; 1473 if (!aconnector) 1474 continue; 1475 1476 stream_on_link[new_stream_on_link_num] = aconnector; 1477 new_stream_on_link_num++; 1478 1479 new_conn_state = drm_atomic_get_new_connector_state(state, &aconnector->base); 1480 if (!new_conn_state) { 1481 DRM_DEBUG_DRIVER("%s:%d MST_DSC no new_conn_state for stream 0x%p, aconnector 0x%p\n", 1482 __func__, __LINE__, stream, aconnector); 1483 continue; 1484 } 1485 1486 if (IS_ERR(new_conn_state)) 1487 continue; 1488 1489 if (!new_conn_state->crtc) 1490 continue; 1491 1492 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc); 1493 if (!new_crtc_state) { 1494 DRM_DEBUG_DRIVER("%s:%d MST_DSC no new_crtc_state for crtc of stream 0x%p, aconnector 0x%p\n", 1495 __func__, __LINE__, stream, aconnector); 1496 continue; 1497 } 1498 1499 if (IS_ERR(new_crtc_state)) 1500 continue; 1501 1502 if (new_crtc_state->enable && new_crtc_state->active) { 1503 if (new_crtc_state->mode_changed || new_crtc_state->active_changed || 1504 new_crtc_state->connectors_changed) { 1505 DRM_DEBUG_DRIVER("%s:%d MST_DSC dsc recompute required." 1506 "stream 0x%p in new dc_state\n", 1507 __func__, __LINE__, stream); 1508 is_dsc_need_re_compute = true; 1509 goto out; 1510 } 1511 } 1512 } 1513 1514 if (new_stream_on_link_num == 0) { 1515 DRM_DEBUG_DRIVER("%s:%d MST_DSC no mode change request for streams in new dc_state\n", 1516 __func__, __LINE__); 1517 is_dsc_need_re_compute = false; 1518 goto out; 1519 } 1520 1521 DRM_DEBUG_DRIVER("%s: MST_DSC check on %d streams in current dc_state\n", 1522 __func__, dc->current_state->stream_count); 1523 1524 /* check current_state if there stream on link but it is not in 1525 * new request state 1526 */ 1527 for (i = 0; i < dc->current_state->stream_count; i++) { 1528 stream = dc->current_state->streams[i]; 1529 /* only check stream on the mst hub */ 1530 if (stream->link != dc_link) 1531 continue; 1532 1533 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context; 1534 if (!aconnector) 1535 continue; 1536 1537 for (j = 0; j < new_stream_on_link_num; j++) { 1538 if (stream_on_link[j]) { 1539 if (aconnector == stream_on_link[j]) 1540 break; 1541 } 1542 } 1543 1544 if (j == new_stream_on_link_num) { 1545 /* not in new state */ 1546 DRM_DEBUG_DRIVER("%s:%d MST_DSC dsc recompute required." 1547 "stream 0x%p in current dc_state but not in new dc_state\n", 1548 __func__, __LINE__, stream); 1549 is_dsc_need_re_compute = true; 1550 break; 1551 } 1552 } 1553 1554 out: 1555 DRM_DEBUG_DRIVER("%s: MST_DSC dsc recompute %s\n", 1556 __func__, is_dsc_need_re_compute ? "required" : "not required"); 1557 1558 return is_dsc_need_re_compute; 1559 } 1560 1561 int compute_mst_dsc_configs_for_state(struct drm_atomic_commit *state, 1562 struct dc_state *dc_state, 1563 struct dsc_mst_fairness_vars *vars) 1564 { 1565 int i, j; 1566 struct dc_stream_state *stream; 1567 bool computed_streams[MAX_PIPES]; 1568 struct amdgpu_dm_connector *aconnector; 1569 struct drm_dp_mst_topology_mgr *mst_mgr; 1570 struct resource_pool *res_pool; 1571 int link_vars_start_index = 0; 1572 int ret = 0; 1573 1574 for (i = 0; i < dc_state->stream_count; i++) 1575 computed_streams[i] = false; 1576 1577 for (i = 0; i < dc_state->stream_count; i++) { 1578 stream = dc_state->streams[i]; 1579 res_pool = stream->ctx->dc->res_pool; 1580 1581 if (stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST) 1582 continue; 1583 1584 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context; 1585 1586 DRM_DEBUG_DRIVER("%s: MST_DSC compute mst dsc configs for stream 0x%p, aconnector 0x%p\n", 1587 __func__, stream, aconnector); 1588 1589 if (!aconnector || !aconnector->dc_sink || !aconnector->mst_output_port) 1590 continue; 1591 1592 if (!aconnector->dc_sink->dsc_caps.dsc_dec_caps.is_dsc_supported) 1593 continue; 1594 1595 if (computed_streams[i]) 1596 continue; 1597 1598 if (res_pool->funcs->remove_stream_from_ctx && 1599 res_pool->funcs->remove_stream_from_ctx(stream->ctx->dc, dc_state, stream) != DC_OK) 1600 return -EINVAL; 1601 1602 if (!is_dsc_need_re_compute(state, dc_state, stream->link)) 1603 continue; 1604 1605 mst_mgr = aconnector->mst_output_port->mgr; 1606 ret = compute_mst_dsc_configs_for_link(state, dc_state, stream->link, vars, mst_mgr, 1607 &link_vars_start_index); 1608 if (ret != 0) 1609 return ret; 1610 1611 for (j = 0; j < dc_state->stream_count; j++) { 1612 if (dc_state->streams[j]->link == stream->link) 1613 computed_streams[j] = true; 1614 } 1615 } 1616 1617 for (i = 0; i < dc_state->stream_count; i++) { 1618 stream = dc_state->streams[i]; 1619 1620 if (stream->timing.flags.DSC == 1) 1621 if (dc_stream_add_dsc_to_resource(stream->ctx->dc, dc_state, stream) != DC_OK) { 1622 DRM_DEBUG_DRIVER("%s:%d MST_DSC Failed to request dsc hw resource for stream 0x%p\n", 1623 __func__, __LINE__, stream); 1624 return -EINVAL; 1625 } 1626 } 1627 1628 return ret; 1629 } 1630 1631 static int pre_compute_mst_dsc_configs_for_state(struct drm_atomic_commit *state, 1632 struct dc_state *dc_state, 1633 struct dsc_mst_fairness_vars *vars) 1634 { 1635 int i, j; 1636 struct dc_stream_state *stream; 1637 bool computed_streams[MAX_PIPES]; 1638 struct amdgpu_dm_connector *aconnector; 1639 struct drm_dp_mst_topology_mgr *mst_mgr; 1640 int link_vars_start_index = 0; 1641 int ret = 0; 1642 1643 for (i = 0; i < dc_state->stream_count; i++) 1644 computed_streams[i] = false; 1645 1646 for (i = 0; i < dc_state->stream_count; i++) { 1647 stream = dc_state->streams[i]; 1648 1649 if (stream->signal != SIGNAL_TYPE_DISPLAY_PORT_MST) 1650 continue; 1651 1652 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context; 1653 1654 DRM_DEBUG_DRIVER("MST_DSC pre compute mst dsc configs for #%d stream 0x%p, aconnector 0x%p\n", 1655 i, stream, aconnector); 1656 1657 if (!aconnector || !aconnector->dc_sink || !aconnector->mst_output_port) 1658 continue; 1659 1660 if (!aconnector->dc_sink->dsc_caps.dsc_dec_caps.is_dsc_supported) 1661 continue; 1662 1663 if (computed_streams[i]) 1664 continue; 1665 1666 if (!is_dsc_need_re_compute(state, dc_state, stream->link)) 1667 continue; 1668 1669 mst_mgr = aconnector->mst_output_port->mgr; 1670 ret = compute_mst_dsc_configs_for_link(state, dc_state, stream->link, vars, mst_mgr, 1671 &link_vars_start_index); 1672 if (ret != 0) 1673 return ret; 1674 1675 for (j = 0; j < dc_state->stream_count; j++) { 1676 if (dc_state->streams[j]->link == stream->link) 1677 computed_streams[j] = true; 1678 } 1679 } 1680 1681 return ret; 1682 } 1683 1684 static int find_crtc_index_in_state_by_stream(struct drm_atomic_commit *state, 1685 struct dc_stream_state *stream) 1686 { 1687 int i; 1688 struct drm_crtc *crtc; 1689 struct drm_crtc_state *new_state, *old_state; 1690 1691 for_each_oldnew_crtc_in_state(state, crtc, old_state, new_state, i) { 1692 struct dm_crtc_state *dm_state = to_dm_crtc_state(new_state); 1693 1694 if (dm_state->stream == stream) 1695 return i; 1696 } 1697 return -1; 1698 } 1699 1700 static bool is_link_to_dschub(struct dc_link *dc_link) 1701 { 1702 union dpcd_dsc_basic_capabilities *dsc_caps = 1703 &dc_link->dpcd_caps.dsc_caps.dsc_basic_caps; 1704 1705 /* only check phy used by dsc mst branch */ 1706 if (dc_link->type != dc_connection_mst_branch) 1707 return false; 1708 1709 if (!(dsc_caps->fields.dsc_support.DSC_SUPPORT || 1710 dsc_caps->fields.dsc_support.DSC_PASSTHROUGH_SUPPORT)) 1711 return false; 1712 return true; 1713 } 1714 1715 static bool is_dsc_precompute_needed(struct drm_atomic_commit *state) 1716 { 1717 int i; 1718 struct drm_crtc *crtc; 1719 struct drm_crtc_state *old_crtc_state, *new_crtc_state; 1720 bool ret = false; 1721 1722 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 1723 struct dm_crtc_state *dm_crtc_state = to_dm_crtc_state(new_crtc_state); 1724 1725 if (!amdgpu_dm_find_first_crtc_matching_connector(state, crtc)) { 1726 ret = false; 1727 break; 1728 } 1729 if (dm_crtc_state->stream && dm_crtc_state->stream->link) 1730 if (is_link_to_dschub(dm_crtc_state->stream->link)) 1731 ret = true; 1732 } 1733 return ret; 1734 } 1735 1736 int pre_validate_dsc(struct drm_atomic_commit *state, 1737 struct dm_atomic_state **dm_state_ptr, 1738 struct dsc_mst_fairness_vars *vars) 1739 { 1740 int i; 1741 struct dm_atomic_state *dm_state; 1742 struct dc_state *local_dc_state = NULL; 1743 int ret = 0; 1744 1745 if (!is_dsc_precompute_needed(state)) { 1746 DRM_INFO_ONCE("%s:%d MST_DSC dsc precompute is not needed\n", __func__, __LINE__); 1747 return 0; 1748 } 1749 ret = dm_atomic_get_state(state, dm_state_ptr); 1750 if (ret != 0) { 1751 DRM_INFO_ONCE("%s:%d MST_DSC dm_atomic_get_state() failed\n", __func__, __LINE__); 1752 return ret; 1753 } 1754 dm_state = *dm_state_ptr; 1755 1756 /* 1757 * create local vailable for dc_state. copy content of streams of dm_state->context 1758 * to local variable. make sure stream pointer of local variable not the same as stream 1759 * from dm_state->context. 1760 */ 1761 1762 local_dc_state = vmalloc(sizeof(struct dc_state)); 1763 if (!local_dc_state) 1764 return -ENOMEM; 1765 memcpy(local_dc_state, dm_state->context, sizeof(struct dc_state)); 1766 1767 for (i = 0; i < local_dc_state->stream_count; i++) { 1768 struct dc_stream_state *stream = dm_state->context->streams[i]; 1769 int ind = find_crtc_index_in_state_by_stream(state, stream); 1770 1771 if (ind >= 0) { 1772 struct drm_connector *connector; 1773 struct drm_connector_state *drm_new_conn_state; 1774 struct dm_connector_state *dm_new_conn_state; 1775 struct dm_crtc_state *dm_old_crtc_state; 1776 1777 connector = 1778 amdgpu_dm_find_first_crtc_matching_connector(state, 1779 state->crtcs[ind].ptr); 1780 if (!connector) 1781 continue; 1782 1783 drm_new_conn_state = 1784 drm_atomic_get_new_connector_state(state, 1785 connector); 1786 dm_new_conn_state = to_dm_connector_state(drm_new_conn_state); 1787 dm_old_crtc_state = to_dm_crtc_state(state->crtcs[ind].old_state); 1788 1789 local_dc_state->streams[i] = 1790 create_validate_stream_for_sink(connector, 1791 &state->crtcs[ind].new_state->mode, 1792 dm_new_conn_state, 1793 dm_old_crtc_state->stream); 1794 if (local_dc_state->streams[i] == NULL) { 1795 ret = -EINVAL; 1796 break; 1797 } 1798 } 1799 } 1800 1801 if (ret != 0) 1802 goto clean_exit; 1803 1804 ret = pre_compute_mst_dsc_configs_for_state(state, local_dc_state, vars); 1805 if (ret != 0) { 1806 DRM_INFO_ONCE("%s:%d MST_DSC dsc pre_compute_mst_dsc_configs_for_state() failed\n", 1807 __func__, __LINE__); 1808 ret = -EINVAL; 1809 goto clean_exit; 1810 } 1811 1812 /* 1813 * compare local_streams -> timing with dm_state->context, 1814 * if the same set crtc_state->mode-change = 0; 1815 */ 1816 for (i = 0; i < local_dc_state->stream_count; i++) { 1817 struct dc_stream_state *stream = dm_state->context->streams[i]; 1818 1819 if (local_dc_state->streams[i] && 1820 dc_is_timing_changed(stream, local_dc_state->streams[i])) { 1821 DRM_INFO_ONCE("%s:%d MST_DSC crtc[%d] needs mode_change\n", __func__, __LINE__, i); 1822 } else { 1823 int ind = find_crtc_index_in_state_by_stream(state, stream); 1824 1825 if (ind >= 0) { 1826 struct dm_crtc_state *dm_new_crtc_state = to_dm_crtc_state(state->crtcs[ind].new_state); 1827 1828 DRM_INFO_ONCE("%s:%d MST_DSC no mode changed for stream 0x%p\n", 1829 __func__, __LINE__, stream); 1830 dm_new_crtc_state->base.mode_changed = dm_new_crtc_state->mode_changed_independent_from_dsc; 1831 } 1832 } 1833 } 1834 clean_exit: 1835 for (i = 0; i < local_dc_state->stream_count; i++) { 1836 struct dc_stream_state *stream = dm_state->context->streams[i]; 1837 1838 if (local_dc_state->streams[i] != stream) 1839 dc_stream_release(local_dc_state->streams[i]); 1840 } 1841 1842 vfree(local_dc_state); 1843 1844 return ret; 1845 } 1846 1847 static uint32_t kbps_from_pbn(unsigned int pbn) 1848 { 1849 uint64_t kbps = (uint64_t)pbn; 1850 1851 kbps *= (1000000 / PEAK_FACTOR_X1000); 1852 kbps *= 8; 1853 kbps *= 54; 1854 kbps /= 64; 1855 1856 return (uint32_t)kbps; 1857 } 1858 1859 static bool is_dsc_common_config_possible(struct dc_stream_state *stream, 1860 struct dc_dsc_bw_range *bw_range) 1861 { 1862 struct dc_dsc_policy dsc_policy = {0}; 1863 bool is_dsc_possible; 1864 1865 dc_dsc_get_policy_for_timing(&stream->timing, 0, &dsc_policy, dc_link_get_highest_encoding_format(stream->link)); 1866 is_dsc_possible = dc_dsc_compute_bandwidth_range(stream->sink->ctx->dc->res_pool->dscs[0], 1867 stream->sink->ctx->dc->debug.dsc_min_slice_height_override, 1868 dsc_policy.min_target_bpp * 16, 1869 dsc_policy.max_target_bpp * 16, 1870 &stream->sink->dsc_caps.dsc_dec_caps, 1871 &stream->timing, dc_link_get_highest_encoding_format(stream->link), bw_range); 1872 1873 return is_dsc_possible; 1874 } 1875 #endif 1876 1877 #if defined(CONFIG_DRM_AMD_DC_FP) 1878 static bool dp_get_link_current_set_bw(struct drm_dp_aux *aux, uint32_t *cur_link_bw) 1879 { 1880 uint32_t total_data_bw_efficiency_x10000 = 0; 1881 uint32_t link_rate_per_lane_kbps = 0; 1882 enum dc_link_rate link_rate; 1883 union lane_count_set lane_count; 1884 u8 dp_link_encoding; 1885 u8 link_bw_set = 0; 1886 u8 data[16] = {0}; 1887 1888 *cur_link_bw = 0; 1889 1890 if (drm_dp_dpcd_read(aux, DP_LINK_BW_SET, data, 16) != 16) 1891 return false; 1892 1893 dp_link_encoding = data[DP_MAIN_LINK_CHANNEL_CODING_SET - DP_LINK_BW_SET]; 1894 link_bw_set = data[DP_LINK_BW_SET - DP_LINK_BW_SET]; 1895 lane_count.raw = data[DP_LANE_COUNT_SET - DP_LINK_BW_SET]; 1896 1897 drm_dbg_dp(aux->drm_dev, "MST_DSC downlink setting: %d, 0x%x x %d\n", 1898 dp_link_encoding, link_bw_set, lane_count.bits.LANE_COUNT_SET); 1899 1900 switch (dp_link_encoding) { 1901 case DP_8b_10b_ENCODING: 1902 link_rate = link_bw_set; 1903 link_rate_per_lane_kbps = link_rate * LINK_RATE_REF_FREQ_IN_KHZ * BITS_PER_DP_BYTE; 1904 total_data_bw_efficiency_x10000 = DATA_EFFICIENCY_8b_10b_x10000; 1905 total_data_bw_efficiency_x10000 /= 100; 1906 total_data_bw_efficiency_x10000 *= DATA_EFFICIENCY_8b_10b_FEC_EFFICIENCY_x100; 1907 break; 1908 case DP_128b_132b_ENCODING: 1909 switch (link_bw_set) { 1910 case DP_LINK_BW_10: 1911 link_rate = LINK_RATE_UHBR10; 1912 break; 1913 case DP_LINK_BW_13_5: 1914 link_rate = LINK_RATE_UHBR13_5; 1915 break; 1916 case DP_LINK_BW_20: 1917 link_rate = LINK_RATE_UHBR20; 1918 break; 1919 default: 1920 return false; 1921 } 1922 1923 link_rate_per_lane_kbps = link_rate * 10000; 1924 total_data_bw_efficiency_x10000 = DATA_EFFICIENCY_128b_132b_x10000; 1925 break; 1926 default: 1927 return false; 1928 } 1929 1930 *cur_link_bw = link_rate_per_lane_kbps * lane_count.bits.LANE_COUNT_SET / 10000 * total_data_bw_efficiency_x10000; 1931 return true; 1932 } 1933 #endif 1934 1935 enum dc_status dm_dp_mst_is_port_support_mode( 1936 struct amdgpu_dm_connector *aconnector, 1937 struct dc_stream_state *stream) 1938 { 1939 #if defined(CONFIG_DRM_AMD_DC_FP) 1940 int branch_max_throughput_mps = 0; 1941 struct dc_link_settings cur_link_settings; 1942 uint32_t end_to_end_bw_in_kbps = 0; 1943 uint32_t root_link_bw_in_kbps = 0; 1944 uint32_t virtual_channel_bw_in_kbps = 0; 1945 struct dc_dsc_bw_range bw_range = {0}; 1946 struct dc_dsc_config_options dsc_options = {0}; 1947 uint32_t stream_kbps; 1948 1949 /* DSC unnecessary case 1950 * Check if timing could be supported within end-to-end BW 1951 */ 1952 stream_kbps = 1953 dc_bandwidth_in_kbps_from_timing(&stream->timing, 1954 dc_link_get_highest_encoding_format(stream->link)); 1955 cur_link_settings = stream->link->verified_link_cap; 1956 root_link_bw_in_kbps = dc_link_bandwidth_kbps(aconnector->dc_link, &cur_link_settings); 1957 virtual_channel_bw_in_kbps = kbps_from_pbn(aconnector->mst_output_port->full_pbn); 1958 1959 /* pick the end to end bw bottleneck */ 1960 end_to_end_bw_in_kbps = min(root_link_bw_in_kbps, virtual_channel_bw_in_kbps); 1961 1962 if (stream_kbps <= end_to_end_bw_in_kbps) { 1963 DRM_DEBUG_DRIVER("MST_DSC no dsc required. End-to-end bw sufficient\n"); 1964 return DC_OK; 1965 } 1966 1967 /*DSC necessary case*/ 1968 if (!aconnector->dsc_aux) 1969 return DC_FAIL_BANDWIDTH_VALIDATE; 1970 1971 if (is_dsc_common_config_possible(stream, &bw_range)) { 1972 1973 /*capable of dsc passthough. dsc bitstream along the entire path*/ 1974 if (aconnector->mst_output_port->passthrough_aux) { 1975 if (bw_range.min_kbps > end_to_end_bw_in_kbps) { 1976 DRM_DEBUG_DRIVER("MST_DSC dsc passthrough and decode at endpoint" 1977 "Max dsc compression bw can't fit into end-to-end bw\n"); 1978 return DC_FAIL_BANDWIDTH_VALIDATE; 1979 } 1980 } else { 1981 /*dsc bitstream decoded at the dp last link*/ 1982 struct drm_dp_mst_port *immediate_upstream_port = NULL; 1983 uint32_t end_link_bw = 0; 1984 1985 /*Get last DP link BW capability. Mode shall be supported by Legacy peer*/ 1986 if (aconnector->mst_output_port->pdt != DP_PEER_DEVICE_DP_LEGACY_CONV && 1987 aconnector->mst_output_port->pdt != DP_PEER_DEVICE_NONE) { 1988 if (aconnector->vc_full_pbn != aconnector->mst_output_port->full_pbn) { 1989 dp_get_link_current_set_bw(&aconnector->mst_output_port->aux, &end_link_bw); 1990 aconnector->vc_full_pbn = aconnector->mst_output_port->full_pbn; 1991 aconnector->mst_local_bw = end_link_bw; 1992 } else { 1993 end_link_bw = aconnector->mst_local_bw; 1994 } 1995 1996 if (end_link_bw > 0 && 1997 stream_kbps > end_link_bw && 1998 aconnector->branch_ieee_oui != DP_BRANCH_DEVICE_ID_90CC24) { 1999 DRM_DEBUG_DRIVER("MST_DSC dsc decode at last link. " 2000 "Mode required bw can't fit into last link\n"); 2001 return DC_FAIL_BANDWIDTH_VALIDATE; 2002 } 2003 } 2004 2005 /*Get virtual channel bandwidth between source and the link before the last link*/ 2006 if (aconnector->mst_output_port->parent->port_parent) 2007 immediate_upstream_port = aconnector->mst_output_port->parent->port_parent; 2008 2009 if (immediate_upstream_port) { 2010 virtual_channel_bw_in_kbps = kbps_from_pbn(immediate_upstream_port->full_pbn); 2011 virtual_channel_bw_in_kbps = min(root_link_bw_in_kbps, virtual_channel_bw_in_kbps); 2012 } else { 2013 /* For topology LCT 1 case - only one mstb*/ 2014 virtual_channel_bw_in_kbps = root_link_bw_in_kbps; 2015 } 2016 2017 if (bw_range.min_kbps > virtual_channel_bw_in_kbps) { 2018 DRM_DEBUG_DRIVER("MST_DSC dsc decode at last link." 2019 "Max dsc compression can't fit into MST available bw\n"); 2020 return DC_FAIL_BANDWIDTH_VALIDATE; 2021 } 2022 } 2023 2024 /*Confirm if we can obtain dsc config*/ 2025 dc_dsc_get_default_config_option(stream->link->dc, &dsc_options); 2026 dsc_options.max_target_bpp_limit_override_x16 = aconnector->base.display_info.max_dsc_bpp * 16; 2027 if (dc_dsc_compute_config(stream->sink->ctx->dc->res_pool->dscs[0], 2028 &stream->sink->dsc_caps.dsc_dec_caps, 2029 &dsc_options, 2030 end_to_end_bw_in_kbps, 2031 &stream->timing, 2032 dc_link_get_highest_encoding_format(stream->link), 2033 &stream->timing.dsc_cfg)) { 2034 stream->timing.flags.DSC = 1; 2035 DRM_DEBUG_DRIVER("MST_DSC require dsc and dsc config found\n"); 2036 } else { 2037 DRM_DEBUG_DRIVER("MST_DSC require dsc but can't find appropriate dsc config\n"); 2038 return DC_FAIL_BANDWIDTH_VALIDATE; 2039 } 2040 2041 /* check is mst dsc output bandwidth branch_overall_throughput_0_mps */ 2042 switch (stream->timing.pixel_encoding) { 2043 case PIXEL_ENCODING_RGB: 2044 case PIXEL_ENCODING_YCBCR444: 2045 branch_max_throughput_mps = 2046 aconnector->dc_sink->dsc_caps.dsc_dec_caps.branch_overall_throughput_0_mps; 2047 break; 2048 case PIXEL_ENCODING_YCBCR422: 2049 case PIXEL_ENCODING_YCBCR420: 2050 branch_max_throughput_mps = 2051 aconnector->dc_sink->dsc_caps.dsc_dec_caps.branch_overall_throughput_1_mps; 2052 break; 2053 default: 2054 break; 2055 } 2056 2057 if (branch_max_throughput_mps != 0 && 2058 ((stream->timing.pix_clk_100hz / 10) > branch_max_throughput_mps * 1000)) { 2059 DRM_DEBUG_DRIVER("MST_DSC require dsc but max throughput mps fails\n"); 2060 return DC_FAIL_BANDWIDTH_VALIDATE; 2061 } 2062 } else { 2063 DRM_DEBUG_DRIVER("MST_DSC require dsc but can't find common dsc config\n"); 2064 return DC_FAIL_BANDWIDTH_VALIDATE; 2065 } 2066 #endif 2067 return DC_OK; 2068 } 2069