1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2026 Intel Corporation 4 */ 5 6 #include <linux/bitops.h> 7 #include <linux/debugfs.h> 8 #include <linux/log2.h> 9 #include <linux/seq_buf.h> 10 #include <linux/slab.h> 11 #include <linux/sort.h> 12 #include <linux/string.h> 13 #include <linux/types.h> 14 15 #include <drm/drm_print.h> 16 17 #include "intel_display_core.h" 18 #include "intel_display_types.h" 19 #include "intel_display_utils.h" 20 #include "intel_dp.h" 21 #include "intel_dp_link_caps.h" 22 23 /** 24 * DOC: DisplayPort link capabilities 25 * 26 * The Intel DP link caps API tracks the supported and allowed 27 * DisplayPort link configurations for a DP encoder and its attached 28 * connectors, and provides helpers to iterate over the allowed 29 * configurations and constrain them by filtering, disabling, or 30 * limiting them to maximum link parameters. 31 * 32 * Locking 33 * ------- 34 * 35 * All accesses to this API must be serialized. The only exception 36 * is intel_dp_link_caps_get_max_limits(), which allow lockless 37 * lookup. Such lookups may observe an out-of-sync &struct 38 * intel_dp_link_config tuple, i.e. a rate from one state and a lane 39 * count from another. 40 * 41 * The Intel i915/xe drivers ensure the above serialization by holding 42 * &drm_mode_config.connection_mutex and, while holding the lock, 43 * waiting for any pending asynchronous atomic commits. This also allows 44 * use of the API from the tails of asynchronous atomic commits, which 45 * cannot hold the lock. 46 * 47 * Iterating and restricting link configurations 48 * --------------------------------------------- 49 * 50 * The link configuration iterators can iterate the ``allowed 51 * configurations`` during modeset configuration selection or link 52 * training fallback handling in a configurable order. 53 * 54 * The iteration order can depend on connector type (eDP, DP SST, 55 * DP MST) and modeset-specific conditions or driver policies, such 56 * as DSC vs. non-DSC modes, power saving vs. better user experience, 57 * or policy changes after a link training failure. 58 * 59 * The configurations exposed via the iterators can be additionally 60 * constrained in the following ways: 61 * 62 * - Filtered for a given modeset based on modeset-specific conditions. 63 * Examples for such conditions include driver policies preferring 64 * power saving or better user experience, post-link training failure 65 * preference changes, or sink automated test requests limiting the 66 * usable configurations. 67 * 68 * - Disabled permanently for the connected sink. Examples of reasons 69 * to disable a configuration include a link training failure for a 70 * given configuration or a driver workaround preventing the use of 71 * a particular configuration. 72 * 73 * - Limited via a maximum link rate and lane count. For example, after 74 * a link training failure, subsequent modesets may be limited to 75 * configurations at or below the failed parameters. 76 * 77 * This mechanism exists for backward compatibility only. Eventually, 78 * it will be removed in favor of relying solely on individually 79 * disabled configurations, as described above. 80 * 81 * Terminology 82 * ----------- 83 * 84 * ``Common link capabilities`` (or ``common caps``) refer to the link 85 * rates and maximum lane count supported by both the source and the 86 * sink, i.e. the intersection of their respective capabilities. 87 * 88 * ``Supported configurations`` are all configurations defined by the 89 * ``Common link capabilities``' link rates and maximum lane count. 90 * 91 * ``Disabled configurations`` are ``Supported configurations`` disabled 92 * via this API. 93 * 94 * ``Enabled configurations`` are ``Supported configurations`` that are 95 * not disabled. 96 * 97 * ``Forced configurations`` are ``Enabled configurations`` forced via 98 * forced link parameter debugfs entries. 99 * 100 * ``Allowed configurations`` are the ``Enabled configurations``, or if 101 * forcing is in effect the ``Forced configurations``, constrained by a 102 * maximum rate and lane count set via the API. 103 */ 104 struct intel_dp_link_caps { 105 struct intel_dp *dp; 106 107 /* Rate, lane count caps common to source and sink. */ 108 int num_rates; 109 int rates[DP_MAX_SUPPORTED_RATES]; 110 int max_lane_count; 111 112 /* common rate,lane_count configs in bw order */ 113 int num_configs; 114 #define INTEL_DP_MAX_LANE_COUNT 4 115 #define INTEL_DP_MAX_SUPPORTED_LANE_CONFIGS (ilog2(INTEL_DP_MAX_LANE_COUNT) + 1) 116 #define INTEL_DP_LANE_COUNT_EXP_BITS order_base_2(INTEL_DP_MAX_SUPPORTED_LANE_CONFIGS) 117 #define INTEL_DP_LINK_RATE_IDX_BITS (BITS_PER_TYPE(u8) - INTEL_DP_LANE_COUNT_EXP_BITS) 118 #define INTEL_DP_MAX_LINK_CONFIGS (DP_MAX_SUPPORTED_RATES * \ 119 INTEL_DP_MAX_SUPPORTED_LANE_CONFIGS) 120 struct intel_dp_link_config_entry { 121 /* index into rates[] */ 122 u8 link_rate_idx:INTEL_DP_LINK_RATE_IDX_BITS; 123 u8 lane_count_exp:INTEL_DP_LANE_COUNT_EXP_BITS; 124 } configs[INTEL_DP_MAX_LINK_CONFIGS]; 125 126 /* 127 * Indices to intel_dp_link_caps::configs[] in rate/lane count, 128 * lane_count/rate order. 129 */ 130 u8 rate_lane_map[INTEL_DP_MAX_LINK_CONFIGS]; 131 u8 lane_rate_map[INTEL_DP_MAX_LINK_CONFIGS]; 132 133 /* 134 * Filter of configurations enabled for the current sink 135 * connection. 136 * 137 * Each bit in the filter's configuration mask corresponds to a 138 * configuration index in the intel_dp_link_caps::configs[] array. 139 * 140 * All configurations start out enabled in the filter after a 141 * new sink is connected. Users disable configurations afterwards 142 * via the link caps API. All configurations get re-enabled 143 * internally in the following cases: 144 * - when forcing a link rate or lane count 145 * - when intel_dp_link_caps_update(reset=true) is called after 146 * a new sink is connected 147 * - when intel_dp_link_caps_update(reset=false) with changed 148 * link capabilities is called 149 * - when intel_dp_link_caps_reset() is called after a new sink 150 * is connected 151 */ 152 struct intel_dp_link_caps_filter enabled_configs; 153 154 /* 155 * Allowed configurations are the supported configurations defined by 156 * config_table.rates and config_table.max_lane_count, constrained by 157 * config_table.enabled_configs and the forced_params and 158 * max_limits values below. 159 * 160 * See get_allowed_config_filter() for the filter of these 161 * configurations. 162 */ 163 164 /* 165 * Forced parameters requested via debugfs. Remains set across sink 166 * disconnects. 167 */ 168 struct intel_dp_link_config forced_params; 169 170 /* 171 * User set maximum limits. These limits constrain the currently 172 * allowed set of configurations and are not adjusted when sink 173 * capabilities change. 174 * 175 * max_limits.rate/lane_count may come from different allowed 176 * configurations, i.e. the (max_limits.rate, max_limits.lane_count) 177 * tuple itself may not be an allowed configuration. 178 */ 179 struct intel_dp_link_config max_limits; 180 }; 181 static_assert(BITS_PER_TYPE(((struct intel_dp_link_caps_filter *)NULL)->config_mask) >= 182 ARRAY_SIZE(((struct intel_dp_link_caps *)NULL)->configs)); 183 184 static struct intel_dp_link_caps_order bw_desc_config_order(void) 185 { 186 struct intel_dp_link_caps_order order = { 187 .key = INTEL_DP_LINK_CAPS_ORDER_KEY_BW, 188 .dir = INTEL_DP_LINK_CAPS_ORDER_DIR_DESC, 189 }; 190 191 return order; 192 } 193 194 static enum intel_dp_link_caps_order_key 195 connector_compute_order_key(bool is_mst) 196 { 197 if (is_mst) 198 return INTEL_DP_LINK_CAPS_ORDER_KEY_BW; 199 else 200 return INTEL_DP_LINK_CAPS_ORDER_KEY_RATE_LANE; 201 } 202 203 static enum intel_dp_link_caps_order_key 204 connector_fallback_order_key(bool is_mst) 205 { 206 if (is_mst) 207 return INTEL_DP_LINK_CAPS_ORDER_KEY_BW; 208 else 209 return INTEL_DP_LINK_CAPS_ORDER_KEY_LANE_RATE; 210 } 211 212 static enum intel_dp_link_caps_order_direction 213 connector_compute_order_dir(bool is_mst, bool use_max_params) 214 { 215 if (is_mst || use_max_params) 216 return INTEL_DP_LINK_CAPS_ORDER_DIR_DESC; 217 else 218 return INTEL_DP_LINK_CAPS_ORDER_DIR_ASC; 219 } 220 221 struct intel_dp_link_caps_order 222 intel_dp_link_caps_connector_compute_order(struct intel_connector *connector) 223 { 224 struct intel_dp *intel_dp = intel_attached_dp(connector); 225 struct intel_dp_link_caps_order order = { 226 .key = connector_compute_order_key(connector->mst.dp), 227 .dir = connector_compute_order_dir(connector->mst.dp, intel_dp->use_max_params) 228 }; 229 230 return order; 231 } 232 233 struct intel_dp_link_caps_order 234 intel_dp_link_caps_connector_fallback_order(bool is_mst) 235 { 236 struct intel_dp_link_caps_order order = { 237 .key = connector_fallback_order_key(is_mst), 238 .dir = INTEL_DP_LINK_CAPS_ORDER_DIR_DESC, 239 }; 240 241 return order; 242 } 243 244 /* Get length of common rates array potentially limited by max_rate. */ 245 static int intel_dp_common_len_rate_limit(struct intel_dp_link_caps *link_caps, 246 int max_rate) 247 { 248 return intel_dp_rate_limit_len(link_caps->rates, 249 link_caps->num_rates, max_rate); 250 } 251 252 static int intel_dp_common_rate(struct intel_dp_link_caps *link_caps, int index) 253 { 254 struct intel_display *display = to_intel_display(link_caps->dp); 255 256 if (drm_WARN_ON(display->drm, 257 index < 0 || index >= link_caps->num_rates)) 258 return 162000; 259 260 return link_caps->rates[index]; 261 } 262 263 /* Theoretical max between source and sink */ 264 static int intel_dp_max_common_rate(struct intel_dp_link_caps *link_caps) 265 { 266 return intel_dp_common_rate(link_caps, link_caps->num_rates - 1); 267 } 268 269 void intel_dp_link_caps_print_common_rates(struct intel_dp_link_caps *link_caps) 270 { 271 struct intel_display *display = to_intel_display(link_caps->dp); 272 DECLARE_SEQ_BUF(s, 128); 273 int i; 274 275 for (i = 0; i < link_caps->num_rates; i++) 276 seq_buf_printf(&s, "%s%d", i ? ", " : "", link_caps->rates[i]); 277 278 drm_dbg_kms(display->drm, "common rates: %s\n", seq_buf_str(&s)); 279 } 280 281 static int intel_dp_link_caps_max_common_lane_count(struct intel_dp_link_caps *link_caps) 282 { 283 return link_caps->max_lane_count; 284 } 285 286 static int forced_lane_count(struct intel_dp_link_caps *link_caps) 287 { 288 if (!link_caps->forced_params.lane_count) 289 return 0; 290 291 return clamp(link_caps->forced_params.lane_count, 292 1, intel_dp_link_caps_max_common_lane_count(link_caps)); 293 } 294 295 static int forced_link_rate(struct intel_dp_link_caps *link_caps) 296 { 297 int len; 298 299 if (!link_caps->forced_params.rate) 300 return 0; 301 302 len = intel_dp_common_len_rate_limit(link_caps, link_caps->forced_params.rate); 303 if (len == 0) 304 return intel_dp_common_rate(link_caps, 0); 305 306 return intel_dp_common_rate(link_caps, len - 1); 307 } 308 309 void intel_dp_link_caps_get_forced_params(struct intel_dp_link_caps *link_caps, 310 struct intel_dp_link_config *forced_params) 311 { 312 forced_params->rate = forced_link_rate(link_caps); 313 forced_params->lane_count = forced_lane_count(link_caps); 314 } 315 316 static int intel_dp_link_config_rate(struct intel_dp_link_caps *link_caps, 317 const struct intel_dp_link_config_entry *lce) 318 { 319 return intel_dp_common_rate(link_caps, lce->link_rate_idx); 320 } 321 322 static int intel_dp_link_config_lane_count(const struct intel_dp_link_config_entry *lce) 323 { 324 return 1 << lce->lane_count_exp; 325 } 326 327 static void 328 to_intel_dp_link_config(struct intel_dp_link_caps *link_caps, 329 int config_idx, struct intel_dp_link_config *config) 330 { 331 const struct intel_dp_link_config_entry *lce = &link_caps->configs[config_idx]; 332 333 config->rate = intel_dp_link_config_rate(link_caps, lce); 334 config->lane_count = intel_dp_link_config_lane_count(lce); 335 } 336 337 static int 338 iter_pos_to_idx(struct intel_dp_link_caps *link_caps, 339 struct intel_dp_link_caps_order config_order, 340 int iter_pos) 341 { 342 int config_idx; 343 344 if (!in_range(iter_pos, 0, link_caps->num_configs)) 345 return -1; 346 347 switch (config_order.dir) { 348 case INTEL_DP_LINK_CAPS_ORDER_DIR_ASC: 349 break; 350 case INTEL_DP_LINK_CAPS_ORDER_DIR_DESC: 351 iter_pos = link_caps->num_configs - 1 - iter_pos; 352 353 break; 354 default: 355 MISSING_CASE(config_order.dir); 356 357 return -1; 358 } 359 360 switch (config_order.key) { 361 case INTEL_DP_LINK_CAPS_ORDER_KEY_BW: 362 config_idx = iter_pos; 363 364 break; 365 case INTEL_DP_LINK_CAPS_ORDER_KEY_RATE_LANE: 366 config_idx = link_caps->rate_lane_map[iter_pos]; 367 368 break; 369 case INTEL_DP_LINK_CAPS_ORDER_KEY_LANE_RATE: 370 config_idx = link_caps->lane_rate_map[iter_pos]; 371 372 break; 373 default: 374 MISSING_CASE(config_order.key); 375 376 return -1; 377 } 378 379 return config_idx; 380 } 381 382 static bool iter_get_next_config(struct intel_dp_link_caps_iter *iter, 383 struct intel_dp_link_config *config) 384 { 385 while (true) { 386 int config_idx; 387 388 iter->pos++; 389 390 config_idx = iter_pos_to_idx(iter->link_caps, iter->order, iter->pos); 391 if (config_idx < 0) { 392 iter->pos = -1; 393 *config = INTEL_DP_LINK_CONFIG_NULL; 394 395 break; 396 } 397 398 if (!(BIT(config_idx) & iter->filter.config_mask)) 399 continue; 400 401 to_intel_dp_link_config(iter->link_caps, config_idx, config); 402 403 break; 404 } 405 406 return iter->pos >= 0; 407 } 408 409 static void iter_start(struct intel_dp_link_caps_iter *iter, 410 struct intel_dp_link_caps *link_caps, 411 struct intel_dp_link_caps_order order, 412 struct intel_dp_link_caps_filter filter) 413 { 414 iter->link_caps = link_caps; 415 iter->pos = -1; 416 iter->order = order; 417 iter->filter = filter; 418 419 iter->get_next_config = iter_get_next_config; 420 } 421 422 static struct intel_dp_link_caps_filter 423 calc_allowed_config_filter(struct intel_dp_link_caps *link_caps, 424 struct intel_dp_link_caps_filter enabled_configs, 425 const struct intel_dp_link_config *max_limits, 426 const struct intel_dp_link_config *forced_params) 427 { 428 struct intel_dp_link_caps_filter allowed_configs = INTEL_DP_LINK_CAPS_FILTER_NONE; 429 struct intel_dp_link_caps_order order = bw_desc_config_order(); 430 struct intel_dp_link_caps_iter iter; 431 struct intel_dp_link_config config; 432 433 iter_start(&iter, link_caps, order, enabled_configs); 434 for_each_dp_link_config(&iter, &config) { 435 if (forced_params->rate && 436 forced_params->rate != config.rate) 437 continue; 438 439 if (forced_params->lane_count && 440 forced_params->lane_count != config.lane_count) 441 continue; 442 443 if (config.rate > max_limits->rate) 444 continue; 445 446 if (config.lane_count > max_limits->lane_count) 447 continue; 448 449 allowed_configs.config_mask |= BIT(iter_pos_to_idx(link_caps, order, iter.pos)); 450 } 451 intel_dp_link_caps_iter_end(&iter); 452 453 return allowed_configs; 454 } 455 456 /* 457 * get_allowed_config_filter - get filter for the currently allowed configs 458 * @link_caps: link capabilities state 459 * 460 * Return: 461 * Filter of link configurations allowed after applying the current 462 * maximum link limits, and further narrowing them by removing any disabled 463 * configuration and limiting to forced link parameters. 464 * 465 * See also: 466 * - intel_dp_link_caps_get_max_limits() 467 * - intel_dp_link_caps_get_forced_params() 468 */ 469 static struct intel_dp_link_caps_filter 470 get_allowed_config_filter(struct intel_dp_link_caps *link_caps) 471 { 472 struct intel_dp_link_config forced_params; 473 474 intel_dp_link_caps_get_forced_params(link_caps, &forced_params); 475 476 return calc_allowed_config_filter(link_caps, link_caps->enabled_configs, 477 &link_caps->max_limits, &forced_params); 478 } 479 480 void intel_dp_link_caps_iter_start(struct intel_dp_link_caps_iter *iter, 481 struct intel_dp_link_caps *link_caps, 482 struct intel_dp_link_caps_order order, 483 struct intel_dp_link_caps_filter filter) 484 { 485 filter.config_mask &= get_allowed_config_filter(link_caps).config_mask; 486 487 iter_start(iter, link_caps, order, filter); 488 } 489 490 void intel_dp_link_caps_iter_end(struct intel_dp_link_caps_iter *iter) 491 { 492 memset(iter, 0, sizeof(*iter)); 493 } 494 495 /** 496 * intel_dp_link_caps_get_max_config - get the maximum config in a given order 497 * @link_caps: link capabilities state 498 * @order_key: ordering key used to rank candidate configurations 499 * @filter: filter for candidate configurations 500 * @max_config: returned maximum link configuration 501 * 502 * Find the last configuration among the currently allowed 503 * configurations filtered by @filter in the iteration order 504 * selected by @order_key, and store it in @max_config. 505 * 506 * See also: 507 * - &enum intel_dp_link_caps_order_key 508 * 509 * Returns: 510 * %true if a maximum config is returned 511 * %false otherwise. 512 */ 513 bool intel_dp_link_caps_get_max_config(struct intel_dp_link_caps *link_caps, 514 enum intel_dp_link_caps_order_key order_key, 515 struct intel_dp_link_caps_filter filter, 516 struct intel_dp_link_config *max_config) 517 { 518 struct intel_dp_link_caps_order order = { 519 .key = order_key, 520 .dir = INTEL_DP_LINK_CAPS_ORDER_DIR_DESC 521 }; 522 struct intel_dp_link_config iter_config; 523 struct intel_dp_link_caps_iter iter; 524 bool found = false; 525 526 intel_dp_link_caps_iter_start(&iter, link_caps, order, filter); 527 for_each_dp_link_config(&iter, &iter_config) { 528 found = true; 529 break; 530 } 531 intel_dp_link_caps_iter_end(&iter); 532 533 if (!found) 534 return false; 535 536 *max_config = iter_config; 537 538 return true; 539 } 540 541 /** 542 * intel_dp_link_caps_get_max_bw_config - get maximum BW link configuration 543 * @link_caps: link capabilities state 544 * @max_config: returned maximum link configuration 545 * 546 * Return the maximum BW link configuration among the currently 547 * allowed configurations. 548 */ 549 void intel_dp_link_caps_get_max_bw_config(struct intel_dp_link_caps *link_caps, 550 struct intel_dp_link_config *max_config) 551 { 552 if (!intel_dp_link_caps_get_max_config(link_caps, 553 bw_desc_config_order().key, INTEL_DP_LINK_CAPS_FILTER_ALL, 554 max_config)) 555 *max_config = INTEL_DP_LINK_CONFIG_NULL; 556 } 557 558 static int find_config_idx(struct intel_dp_link_caps *link_caps, 559 struct intel_dp_link_caps_filter filter, 560 const struct intel_dp_link_config *link_config) 561 { 562 struct intel_dp_link_caps_order order = bw_desc_config_order(); 563 struct intel_dp_link_config iter_config; 564 struct intel_dp_link_caps_iter iter; 565 int pos = -1; 566 567 intel_dp_link_caps_iter_start(&iter, link_caps, order, filter); 568 for_each_dp_link_config(&iter, &iter_config) { 569 if (iter_config.rate == link_config->rate && 570 iter_config.lane_count == link_config->lane_count) { 571 pos = iter.pos; 572 573 break; 574 } 575 } 576 intel_dp_link_caps_iter_end(&iter); 577 578 if (pos < 0) 579 return pos; 580 581 return iter_pos_to_idx(link_caps, order, pos); 582 } 583 584 bool intel_dp_link_caps_filter_add(struct intel_dp_link_caps *link_caps, 585 struct intel_dp_link_caps_filter *filter, 586 const struct intel_dp_link_config *config) 587 { 588 int idx; 589 590 idx = find_config_idx(link_caps, get_allowed_config_filter(link_caps), config); 591 if (idx < 0) 592 return false; 593 594 filter->config_mask |= BIT(idx); 595 596 return true; 597 } 598 599 static bool intel_dp_link_caps_filter_remove(struct intel_dp_link_caps *link_caps, 600 struct intel_dp_link_caps_filter *filter, 601 const struct intel_dp_link_config *config) 602 { 603 int idx; 604 605 idx = find_config_idx(link_caps, get_allowed_config_filter(link_caps), config); 606 if (idx < 0) 607 return false; 608 609 filter->config_mask &= ~BIT(idx); 610 611 return true; 612 } 613 614 static void set_max_link_limits(struct intel_dp_link_caps *link_caps, 615 const struct intel_dp_link_config *max_link_limits) 616 { 617 link_caps->max_limits = *max_link_limits; 618 } 619 620 static void reset_max_link_limits(struct intel_dp_link_caps *link_caps) 621 { 622 struct intel_dp_link_config max_link_limits = { 623 .rate = intel_dp_max_common_rate(link_caps), 624 .lane_count = intel_dp_link_caps_max_common_lane_count(link_caps), 625 }; 626 627 set_max_link_limits(link_caps, &max_link_limits); 628 } 629 630 static void reset_max_link_limits_reenable_all(struct intel_dp_link_caps *link_caps) 631 { 632 link_caps->enabled_configs = INTEL_DP_LINK_CAPS_FILTER_ALL; 633 reset_max_link_limits(link_caps); 634 } 635 636 /** 637 * intel_dp_link_caps_disable_config - disable a configuration 638 * @link_caps: link capabilities state 639 * @config: configuration to disable 640 * 641 * Disable the configuration identified by @config. This removes the 642 * configuration from the set of allowed configurations. The disabling 643 * shouldn't leave the remaining configuration set empty. 644 * 645 * The configuration remains disallowed until intel_dp_link_caps() with 646 * reset=%true or changed sink capabilities is called, or 647 * intel_dp_link_caps_reset() is called. Each of these happens after a 648 * new sink is connected or the currently connected sink changes its 649 * capabilities. 650 * 651 * Return: 652 * - %true if @config was valid and the derived state was updated. 653 * - %false if @config was invalid or the remaining configuration set 654 * would remain empty. 655 */ 656 bool intel_dp_link_caps_disable_config(struct intel_dp_link_caps *link_caps, 657 const struct intel_dp_link_config *config) 658 { 659 struct intel_dp_link_caps_filter enabled_configs = link_caps->enabled_configs; 660 struct intel_dp_link_config forced_params; 661 662 if (!intel_dp_link_caps_filter_remove(link_caps, &enabled_configs, config)) 663 return false; 664 665 intel_dp_link_caps_get_forced_params(link_caps, &forced_params); 666 667 if (!calc_allowed_config_filter(link_caps, enabled_configs, 668 &link_caps->max_limits, &forced_params).config_mask) 669 return false; 670 671 link_caps->enabled_configs = enabled_configs; 672 673 return true; 674 } 675 676 /** 677 * intel_dp_link_caps_get_max_limits - get the current maximum link limits 678 * @link_caps: link capabilities state 679 * @max_link_limits: returned maximum link limits 680 * 681 * Return the current maximum rate and lane count limits in 682 * @max_link_limits. 683 * 684 * These limits constrain the set of allowed configurations. 685 * 686 * The limits are set to the maximum common supported values after 687 * intel_dp_link_caps_reset() is called, and can later be modified by 688 * intel_dp_link_caps_set_max_limits(). The max rate and lane count 689 * parameters are independent limits, so the pair does not necessarily 690 * define a valid configuration. 691 * 692 * This function may be called without serializing against updates to 693 * @link_caps. However, without such serialization the returned value may be 694 * an out-of-sync (link rate, lane count) tuple, i.e. the parameters may 695 * belong to different update snapshots in time. 696 */ 697 void intel_dp_link_caps_get_max_limits(struct intel_dp_link_caps *link_caps, 698 struct intel_dp_link_config *max_link_limits) 699 { 700 *max_link_limits = link_caps->max_limits; 701 } 702 703 static bool max_link_limits_valid(struct intel_dp_link_caps *link_caps, 704 const struct intel_dp_link_config *max_link_limits) 705 { 706 struct intel_dp_link_caps_filter allowed_configs; 707 struct intel_dp_link_config forced_params; 708 709 if (max_link_limits->lane_count > INTEL_DP_MAX_LANE_COUNT || 710 !is_power_of_2(max_link_limits->lane_count)) 711 return false; 712 713 /* TODO: Validate max_link_limits->rate against the source supported rates. */ 714 715 intel_dp_link_caps_get_forced_params(link_caps, &forced_params); 716 allowed_configs = calc_allowed_config_filter(link_caps, link_caps->enabled_configs, 717 max_link_limits, &forced_params); 718 719 return allowed_configs.config_mask != 0; 720 } 721 722 /** 723 * intel_dp_link_caps_set_max_limits - set the current maximum link limits 724 * @link_caps: link capabilities state 725 * @max_link_limits: new maximum link limits 726 * 727 * Set the current maximum rate and lane count limits to @max_link_limits, 728 * constraining the set of allowed configurations. 729 * 730 * The new limits must leave at least one configuration allowed: the limits 731 * must not be below the currently active forced parameters or below all the 732 * configurations that remain after disabled configurations are excluded. 733 * 734 * Unlike intel_dp_link_caps_get_max_limits(), the caller must serialize 735 * this call against concurrent queries and updates to @link_caps, in line 736 * with the rest of the API. 737 * 738 * Return: 739 * - %true if the @link_caps cached max limits value got updated with 740 * @max_link_limits. 741 * - %false if @max_link_limits is invalid. 742 */ 743 bool intel_dp_link_caps_set_max_limits(struct intel_dp_link_caps *link_caps, 744 const struct intel_dp_link_config *max_link_limits) 745 { 746 if (!max_link_limits_valid(link_caps, max_link_limits)) 747 return false; 748 749 set_max_link_limits(link_caps, max_link_limits); 750 751 return true; 752 } 753 754 /** 755 * intel_dp_link_caps_reset_max_limits - reset the current maximum link limits 756 * @link_caps: link capabilities state 757 * 758 * Reset the current maximum link limits to the maximum supported common link 759 * rate and lane count. 760 */ 761 void intel_dp_link_caps_reset_max_limits(struct intel_dp_link_caps *link_caps) 762 { 763 reset_max_link_limits(link_caps); 764 } 765 766 static int intel_dp_link_config_bw(struct intel_dp_link_caps *link_caps, 767 const struct intel_dp_link_config_entry *lce) 768 { 769 return drm_dp_max_dprx_data_rate(intel_dp_link_config_rate(link_caps, lce), 770 intel_dp_link_config_lane_count(lce)); 771 } 772 773 static int link_config_cmp_by_bw(const void *a, const void *b, const void *p) 774 { 775 struct intel_dp *intel_dp = (struct intel_dp *)p; /* remove const */ 776 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 777 778 const struct intel_dp_link_config_entry *lce_a = a; 779 const struct intel_dp_link_config_entry *lce_b = b; 780 int bw_a = intel_dp_link_config_bw(link_caps, lce_a); 781 int bw_b = intel_dp_link_config_bw(link_caps, lce_b); 782 783 if (bw_a != bw_b) 784 return bw_a - bw_b; 785 786 return intel_dp_link_config_rate(link_caps, lce_a) - 787 intel_dp_link_config_rate(link_caps, lce_b); 788 } 789 790 static int link_config_cmp_by_rate_lane(const void *a, const void *b, const void *p) 791 { 792 const struct intel_dp_link_caps *link_caps = p; 793 u8 *lce_a_idx = (u8 *)a; 794 u8 *lce_b_idx = (u8 *)b; 795 const struct intel_dp_link_config_entry *lce_a = &link_caps->configs[*lce_a_idx]; 796 const struct intel_dp_link_config_entry *lce_b = &link_caps->configs[*lce_b_idx]; 797 798 if (lce_a->link_rate_idx != lce_b->link_rate_idx) 799 return lce_a->link_rate_idx - lce_b->link_rate_idx; 800 801 return lce_a->lane_count_exp - lce_b->lane_count_exp; 802 } 803 804 static int link_config_cmp_by_lane_rate(const void *a, const void *b, const void *p) 805 { 806 const struct intel_dp_link_caps *link_caps = p; 807 u8 *lce_a_idx = (u8 *)a; 808 u8 *lce_b_idx = (u8 *)b; 809 const struct intel_dp_link_config_entry *lce_a = &link_caps->configs[*lce_a_idx]; 810 const struct intel_dp_link_config_entry *lce_b = &link_caps->configs[*lce_b_idx]; 811 812 if (lce_a->lane_count_exp != lce_b->lane_count_exp) 813 return lce_a->lane_count_exp - lce_b->lane_count_exp; 814 815 return lce_a->link_rate_idx - lce_b->link_rate_idx; 816 } 817 818 /** 819 * intel_dp_link_caps_update - rebuild the supported link configuration state 820 * @link_caps: link capabilities state 821 * @rates: supported common link rates 822 * @num_rates: number of entries in @rates 823 * @max_lane_count: supported maximum lane count 824 * @reset: reset limits and disabled configs 825 * 826 * Rebuild the supported link configuration state from @rates and 827 * @max_lane_count. 828 * 829 * If @reset is %true, reset the maximum link limits to the maximum 830 * supported rate and lane count, and re-enable all configurations. 831 * 832 * This function is called regularly, at least after a sink is connected, 833 * but it may also be called later whenever the sink capabilities may have 834 * changed, for example in response to HPD IRQ / RX_CAP_CHANGED signaling. 835 * 836 * In the Intel driver this function is currently called whenever the 837 * connector detect handler runs, after reading the sink capabilities. This 838 * may change if those capabilities are cached until the sink is 839 * disconnected, or until RX_CAP_CHANGED is signaled. In any case, this 840 * function should be called whenever the sink capabilities were read out 841 * and may have changed. 842 * 843 * Returns: 844 * - %true if the link capabilities have changed, %false otherwise. 845 */ 846 bool intel_dp_link_caps_update(struct intel_dp_link_caps *link_caps, 847 const int *rates, int num_rates, int max_lane_count, 848 bool reset) 849 { 850 struct intel_dp *intel_dp = link_caps->dp; 851 struct intel_display *display = to_intel_display(intel_dp); 852 struct intel_dp_link_config_entry *lce; 853 bool link_params_changed = reset; 854 int num_common_lane_configs; 855 int i; 856 int j; 857 858 if (drm_WARN_ON(display->drm, !is_power_of_2(max_lane_count))) 859 return false; 860 861 if (drm_WARN_ON(display->drm, num_rates > ARRAY_SIZE(link_caps->rates))) 862 return false; 863 864 num_common_lane_configs = ilog2(max_lane_count) + 1; 865 866 if (drm_WARN_ON(display->drm, num_rates * num_common_lane_configs > 867 ARRAY_SIZE(link_caps->configs))) 868 return false; 869 870 /* TODO: Add a struct containing both rates and number of rates. */ 871 static_assert(__same_type(rates[0], link_caps->rates[0])); 872 if (num_rates != link_caps->num_rates || 873 memcmp(rates, link_caps->rates, num_rates * sizeof(rates[0]))) 874 link_params_changed = true; 875 876 if (max_lane_count != link_caps->max_lane_count) 877 link_params_changed = true; 878 879 memcpy(link_caps->rates, rates, num_rates * sizeof(rates[0])); 880 link_caps->num_rates = num_rates; 881 link_caps->max_lane_count = max_lane_count; 882 883 link_caps->num_configs = num_rates * num_common_lane_configs; 884 885 lce = &link_caps->configs[0]; 886 for (i = 0; i < link_caps->num_rates; i++) { 887 for (j = 0; j < num_common_lane_configs; j++) { 888 lce->lane_count_exp = j; 889 lce->link_rate_idx = i; 890 891 lce++; 892 } 893 } 894 895 sort_r(link_caps->configs, link_caps->num_configs, 896 sizeof(link_caps->configs[0]), 897 link_config_cmp_by_bw, NULL, 898 intel_dp); 899 900 for (i = 0; i < link_caps->num_configs; i++) { 901 link_caps->rate_lane_map[i] = i; 902 link_caps->lane_rate_map[i] = i; 903 } 904 905 sort_r(link_caps->rate_lane_map, link_caps->num_configs, 906 sizeof(link_caps->rate_lane_map[0]), 907 link_config_cmp_by_rate_lane, NULL, 908 link_caps); 909 910 sort_r(link_caps->lane_rate_map, link_caps->num_configs, 911 sizeof(link_caps->lane_rate_map[0]), 912 link_config_cmp_by_lane_rate, NULL, 913 link_caps); 914 915 if (link_params_changed) 916 reset_max_link_limits_reenable_all(link_caps); 917 918 return link_params_changed; 919 } 920 921 /** 922 * intel_dp_link_caps_reset - reset link capability restrictions 923 * @link_caps: link capabilities state 924 * 925 * Reset all current restrictions except for the user requested forced 926 * parameters, thus updating the set of allowed configurations and the 927 * derived maximum link information accordingly. 928 * 929 * This function is regularly called after a sink is connected, either 930 * for the first time to the connector or after a previous sink was 931 * disconnected from it, and intel_dp_link_caps_update() was called. 932 */ 933 void intel_dp_link_caps_reset(struct intel_dp_link_caps *link_caps) 934 { 935 reset_max_link_limits_reenable_all(link_caps); 936 } 937 938 static int i915_dp_force_link_rate_show(struct seq_file *m, void *data) 939 { 940 struct intel_connector *connector = to_intel_connector(m->private); 941 struct intel_display *display = to_intel_display(connector); 942 struct intel_dp *intel_dp = intel_attached_dp(connector); 943 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 944 int current_rate = -1; 945 int force_rate; 946 int err; 947 int i; 948 949 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 950 if (err) 951 return err; 952 953 intel_dp_flush_connector_commits(connector); 954 955 if (intel_dp->link.active) 956 current_rate = intel_dp->link_rate; 957 958 force_rate = link_caps->forced_params.rate; 959 960 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 961 962 seq_printf(m, "%sauto%s", 963 force_rate == 0 ? "[" : "", 964 force_rate == 0 ? "]" : ""); 965 966 for (i = 0; i < intel_dp->num_source_rates; i++) 967 seq_printf(m, " %s%d%s%s", 968 intel_dp->source_rates[i] == force_rate ? "[" : "", 969 intel_dp->source_rates[i], 970 intel_dp->source_rates[i] == current_rate ? "*" : "", 971 intel_dp->source_rates[i] == force_rate ? "]" : ""); 972 973 seq_putc(m, '\n'); 974 975 return 0; 976 } 977 978 static int parse_link_rate(struct intel_dp_link_caps *link_caps, const char __user *ubuf, size_t len) 979 { 980 struct intel_dp *intel_dp = link_caps->dp; 981 char *kbuf; 982 const char *p; 983 int rate; 984 int ret = 0; 985 986 kbuf = memdup_user_nul(ubuf, len); 987 if (IS_ERR(kbuf)) 988 return PTR_ERR(kbuf); 989 990 p = strim(kbuf); 991 992 if (!strcmp(p, "auto")) { 993 rate = 0; 994 } else { 995 ret = kstrtoint(p, 0, &rate); 996 if (ret < 0) 997 goto out_free; 998 999 if (intel_dp_rate_index(intel_dp->source_rates, 1000 intel_dp->num_source_rates, 1001 rate) < 0) 1002 ret = -EINVAL; 1003 } 1004 1005 out_free: 1006 kfree(kbuf); 1007 1008 return ret < 0 ? ret : rate; 1009 } 1010 1011 static ssize_t i915_dp_force_link_rate_write(struct file *file, 1012 const char __user *ubuf, 1013 size_t len, loff_t *offp) 1014 { 1015 struct seq_file *m = file->private_data; 1016 struct intel_connector *connector = to_intel_connector(m->private); 1017 struct intel_display *display = to_intel_display(connector); 1018 struct intel_dp *intel_dp = intel_attached_dp(connector); 1019 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1020 int rate; 1021 int err; 1022 1023 rate = parse_link_rate(link_caps, ubuf, len); 1024 if (rate < 0) 1025 return rate; 1026 1027 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1028 if (err) 1029 return err; 1030 1031 intel_dp_flush_connector_commits(connector); 1032 1033 intel_dp_reset_link_params(intel_dp); 1034 link_caps->forced_params.rate = rate; 1035 1036 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1037 1038 *offp += len; 1039 1040 return len; 1041 } 1042 DEFINE_SHOW_STORE_ATTRIBUTE(i915_dp_force_link_rate); 1043 1044 static int i915_dp_force_lane_count_show(struct seq_file *m, void *data) 1045 { 1046 struct intel_connector *connector = to_intel_connector(m->private); 1047 struct intel_display *display = to_intel_display(connector); 1048 struct intel_dp *intel_dp = intel_attached_dp(connector); 1049 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1050 int current_lane_count = -1; 1051 int force_lane_count; 1052 int err; 1053 int i; 1054 1055 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1056 if (err) 1057 return err; 1058 1059 intel_dp_flush_connector_commits(connector); 1060 1061 if (intel_dp->link.active) 1062 current_lane_count = intel_dp->lane_count; 1063 force_lane_count = link_caps->forced_params.lane_count; 1064 1065 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1066 1067 seq_printf(m, "%sauto%s", 1068 force_lane_count == 0 ? "[" : "", 1069 force_lane_count == 0 ? "]" : ""); 1070 1071 for (i = 1; i <= 4; i <<= 1) 1072 seq_printf(m, " %s%d%s%s", 1073 i == force_lane_count ? "[" : "", 1074 i, 1075 i == current_lane_count ? "*" : "", 1076 i == force_lane_count ? "]" : ""); 1077 1078 seq_putc(m, '\n'); 1079 1080 return 0; 1081 } 1082 1083 static int parse_lane_count(const char __user *ubuf, size_t len) 1084 { 1085 char *kbuf; 1086 const char *p; 1087 int lane_count; 1088 int ret = 0; 1089 1090 kbuf = memdup_user_nul(ubuf, len); 1091 if (IS_ERR(kbuf)) 1092 return PTR_ERR(kbuf); 1093 1094 p = strim(kbuf); 1095 1096 if (!strcmp(p, "auto")) { 1097 lane_count = 0; 1098 } else { 1099 ret = kstrtoint(p, 0, &lane_count); 1100 if (ret < 0) 1101 goto out_free; 1102 1103 switch (lane_count) { 1104 case 1: 1105 case 2: 1106 case 4: 1107 break; 1108 default: 1109 ret = -EINVAL; 1110 } 1111 } 1112 1113 out_free: 1114 kfree(kbuf); 1115 1116 return ret < 0 ? ret : lane_count; 1117 } 1118 1119 static ssize_t i915_dp_force_lane_count_write(struct file *file, 1120 const char __user *ubuf, 1121 size_t len, loff_t *offp) 1122 { 1123 struct seq_file *m = file->private_data; 1124 struct intel_connector *connector = to_intel_connector(m->private); 1125 struct intel_display *display = to_intel_display(connector); 1126 struct intel_dp *intel_dp = intel_attached_dp(connector); 1127 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1128 int lane_count; 1129 int err; 1130 1131 lane_count = parse_lane_count(ubuf, len); 1132 if (lane_count < 0) 1133 return lane_count; 1134 1135 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1136 if (err) 1137 return err; 1138 1139 intel_dp_flush_connector_commits(connector); 1140 1141 intel_dp_reset_link_params(intel_dp); 1142 link_caps->forced_params.lane_count = lane_count; 1143 1144 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1145 1146 *offp += len; 1147 1148 return len; 1149 } 1150 DEFINE_SHOW_STORE_ATTRIBUTE(i915_dp_force_lane_count); 1151 1152 static int i915_dp_max_link_rate_show(void *data, u64 *val) 1153 { 1154 struct intel_connector *connector = to_intel_connector(data); 1155 struct intel_display *display = to_intel_display(connector); 1156 struct intel_dp *intel_dp = intel_attached_dp(connector); 1157 struct intel_dp_link_config max_link_limits; 1158 int err; 1159 1160 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1161 if (err) 1162 return err; 1163 1164 intel_dp_flush_connector_commits(connector); 1165 1166 intel_dp_link_caps_get_max_limits(intel_dp->link.caps, &max_link_limits); 1167 *val = max_link_limits.rate; 1168 1169 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1170 1171 return 0; 1172 } 1173 DEFINE_DEBUGFS_ATTRIBUTE(i915_dp_max_link_rate_fops, i915_dp_max_link_rate_show, NULL, "%llu\n"); 1174 1175 static int i915_dp_max_lane_count_show(void *data, u64 *val) 1176 { 1177 struct intel_connector *connector = to_intel_connector(data); 1178 struct intel_display *display = to_intel_display(connector); 1179 struct intel_dp *intel_dp = intel_attached_dp(connector); 1180 struct intel_dp_link_config max_link_limits; 1181 int err; 1182 1183 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1184 if (err) 1185 return err; 1186 1187 intel_dp_flush_connector_commits(connector); 1188 1189 intel_dp_link_caps_get_max_limits(intel_dp->link.caps, &max_link_limits); 1190 *val = max_link_limits.lane_count; 1191 1192 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1193 1194 return 0; 1195 } 1196 DEFINE_DEBUGFS_ATTRIBUTE(i915_dp_max_lane_count_fops, i915_dp_max_lane_count_show, NULL, "%llu\n"); 1197 1198 static int intel_dp_allowed_link_configs_show(struct seq_file *m, void *data) 1199 { 1200 struct intel_connector *connector = to_intel_connector(m->private); 1201 struct intel_display *display = to_intel_display(connector); 1202 struct intel_dp *intel_dp = intel_attached_dp(connector); 1203 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1204 struct intel_dp_link_config link_config; 1205 struct intel_dp_link_caps_iter iter; 1206 int err; 1207 int i; 1208 1209 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1210 if (err) 1211 return err; 1212 1213 intel_dp_flush_connector_commits(connector); 1214 1215 i = 0; 1216 intel_dp_link_caps_iter_start(&iter, 1217 link_caps, 1218 intel_dp_link_caps_connector_compute_order(connector), 1219 INTEL_DP_LINK_CAPS_FILTER_ALL); 1220 for_each_dp_link_config(&iter, &link_config) { 1221 seq_printf(m, "%s%dx%d", 1222 i ? " " : "", 1223 link_config.lane_count, link_config.rate); 1224 i++; 1225 } 1226 intel_dp_link_caps_iter_end(&iter); 1227 1228 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1229 1230 seq_putc(m, '\n'); 1231 1232 return 0; 1233 } 1234 DEFINE_SHOW_ATTRIBUTE(intel_dp_allowed_link_configs); 1235 1236 /** 1237 * intel_dp_link_caps_debugfs_add - add link caps debugfs files for a connector 1238 * @connector: connector to add the debugfs files for 1239 * 1240 * Add the link-capability debugfs files for a DP @connector. 1241 */ 1242 void intel_dp_link_caps_debugfs_add(struct intel_connector *connector) 1243 { 1244 struct dentry *root = connector->base.debugfs_entry; 1245 1246 if (connector->base.connector_type != DRM_MODE_CONNECTOR_DisplayPort && 1247 connector->base.connector_type != DRM_MODE_CONNECTOR_eDP) 1248 return; 1249 1250 debugfs_create_file("i915_dp_force_link_rate", 0644, root, 1251 connector, &i915_dp_force_link_rate_fops); 1252 1253 debugfs_create_file("i915_dp_force_lane_count", 0644, root, 1254 connector, &i915_dp_force_lane_count_fops); 1255 1256 debugfs_create_file("i915_dp_max_link_rate", 0444, root, 1257 connector, &i915_dp_max_link_rate_fops); 1258 1259 debugfs_create_file("i915_dp_max_lane_count", 0444, root, 1260 connector, &i915_dp_max_lane_count_fops); 1261 1262 debugfs_create_file("intel_dp_allowed_link_configs", 0444, root, 1263 connector, &intel_dp_allowed_link_configs_fops); 1264 } 1265 1266 struct intel_dp_link_caps *intel_dp_link_caps_init(struct intel_dp *intel_dp) 1267 { 1268 struct intel_dp_link_caps *link_caps; 1269 1270 link_caps = kzalloc_obj(*link_caps); 1271 if (!link_caps) 1272 return NULL; 1273 1274 link_caps->dp = intel_dp; 1275 link_caps->enabled_configs = INTEL_DP_LINK_CAPS_FILTER_ALL; 1276 1277 return link_caps; 1278 } 1279 1280 void intel_dp_link_caps_cleanup(struct intel_dp_link_caps *link_caps) 1281 { 1282 kfree(link_caps); 1283 } 1284 1285 #if IS_ENABLED(CONFIG_KUNIT) 1286 1287 #define __INIT_MEMBER(__name, __fn) \ 1288 .__name = __fn, 1289 1290 #define INTEL_DP_LINK_CAPS_TEST_OPS_INIT \ 1291 INTEL_DP_LINK_CAPS_TEST_OPS_MEMBERS(__INIT_MEMBER) 1292 1293 #ifdef I915 1294 1295 const struct intel_dp_link_caps_test_ops i915_display_dp_link_caps_test_ops = { 1296 INTEL_DP_LINK_CAPS_TEST_OPS_INIT 1297 }; 1298 EXPORT_SYMBOL(i915_display_dp_link_caps_test_ops); 1299 1300 #else 1301 1302 const struct intel_dp_link_caps_test_ops intel_display_dp_link_caps_test_ops = { 1303 INTEL_DP_LINK_CAPS_TEST_OPS_INIT 1304 }; 1305 EXPORT_SYMBOL(intel_display_dp_link_caps_test_ops); 1306 1307 #endif /* I915 */ 1308 1309 #undef INTEL_DP_LINK_CAPS_TEST_OPS_INIT 1310 #undef __INIT_MEMBER 1311 1312 #endif /* CONFIG_KUNIT */ 1313