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_display *display = to_intel_display(link_caps->dp); 430 struct intel_dp_link_caps_order order = bw_desc_config_order(); 431 struct intel_dp_link_caps_iter iter; 432 struct intel_dp_link_config config; 433 434 iter_start(&iter, link_caps, order, enabled_configs); 435 for_each_dp_link_config(&iter, &config) { 436 int config_idx; 437 438 if (forced_params->rate && 439 forced_params->rate != config.rate) 440 continue; 441 442 if (forced_params->lane_count && 443 forced_params->lane_count != config.lane_count) 444 continue; 445 446 if (config.rate > max_limits->rate) 447 continue; 448 449 if (config.lane_count > max_limits->lane_count) 450 continue; 451 452 config_idx = iter_pos_to_idx(link_caps, order, iter.pos); 453 if (drm_WARN_ON(display->drm, config_idx < 0)) 454 continue; 455 456 allowed_configs.config_mask |= BIT(config_idx); 457 } 458 intel_dp_link_caps_iter_end(&iter); 459 460 return allowed_configs; 461 } 462 463 /* 464 * get_allowed_config_filter - get filter for the currently allowed configs 465 * @link_caps: link capabilities state 466 * 467 * Return: 468 * Filter of link configurations allowed after applying the current 469 * maximum link limits, and further narrowing them by removing any disabled 470 * configuration and limiting to forced link parameters. 471 * 472 * See also: 473 * - intel_dp_link_caps_get_max_limits() 474 * - intel_dp_link_caps_get_forced_params() 475 */ 476 static struct intel_dp_link_caps_filter 477 get_allowed_config_filter(struct intel_dp_link_caps *link_caps) 478 { 479 struct intel_dp_link_config forced_params; 480 481 intel_dp_link_caps_get_forced_params(link_caps, &forced_params); 482 483 return calc_allowed_config_filter(link_caps, link_caps->enabled_configs, 484 &link_caps->max_limits, &forced_params); 485 } 486 487 void intel_dp_link_caps_iter_start(struct intel_dp_link_caps_iter *iter, 488 struct intel_dp_link_caps *link_caps, 489 struct intel_dp_link_caps_order order, 490 struct intel_dp_link_caps_filter filter) 491 { 492 filter.config_mask &= get_allowed_config_filter(link_caps).config_mask; 493 494 iter_start(iter, link_caps, order, filter); 495 } 496 497 void intel_dp_link_caps_iter_end(struct intel_dp_link_caps_iter *iter) 498 { 499 memset(iter, 0, sizeof(*iter)); 500 } 501 502 /** 503 * intel_dp_link_caps_get_max_config - get the maximum config in a given order 504 * @link_caps: link capabilities state 505 * @order_key: ordering key used to rank candidate configurations 506 * @filter: filter for candidate configurations 507 * @max_config: returned maximum link configuration 508 * 509 * Find the last configuration among the currently allowed 510 * configurations filtered by @filter in the iteration order 511 * selected by @order_key, and store it in @max_config. 512 * 513 * See also: 514 * - &enum intel_dp_link_caps_order_key 515 * 516 * Returns: 517 * %true if a maximum config is returned 518 * %false otherwise. 519 */ 520 bool intel_dp_link_caps_get_max_config(struct intel_dp_link_caps *link_caps, 521 enum intel_dp_link_caps_order_key order_key, 522 struct intel_dp_link_caps_filter filter, 523 struct intel_dp_link_config *max_config) 524 { 525 struct intel_dp_link_caps_order order = { 526 .key = order_key, 527 .dir = INTEL_DP_LINK_CAPS_ORDER_DIR_DESC 528 }; 529 struct intel_dp_link_config iter_config; 530 struct intel_dp_link_caps_iter iter; 531 bool found = false; 532 533 intel_dp_link_caps_iter_start(&iter, link_caps, order, filter); 534 for_each_dp_link_config(&iter, &iter_config) { 535 found = true; 536 break; 537 } 538 intel_dp_link_caps_iter_end(&iter); 539 540 if (!found) 541 return false; 542 543 *max_config = iter_config; 544 545 return true; 546 } 547 548 /** 549 * intel_dp_link_caps_get_max_bw_config - get maximum BW link configuration 550 * @link_caps: link capabilities state 551 * @max_config: returned maximum link configuration 552 * 553 * Return the maximum BW link configuration among the currently 554 * allowed configurations. 555 */ 556 void intel_dp_link_caps_get_max_bw_config(struct intel_dp_link_caps *link_caps, 557 struct intel_dp_link_config *max_config) 558 { 559 if (!intel_dp_link_caps_get_max_config(link_caps, 560 bw_desc_config_order().key, INTEL_DP_LINK_CAPS_FILTER_ALL, 561 max_config)) 562 *max_config = INTEL_DP_LINK_CONFIG_NULL; 563 } 564 565 static int find_config_idx(struct intel_dp_link_caps *link_caps, 566 struct intel_dp_link_caps_filter filter, 567 const struct intel_dp_link_config *link_config) 568 { 569 struct intel_dp_link_caps_order order = bw_desc_config_order(); 570 struct intel_dp_link_config iter_config; 571 struct intel_dp_link_caps_iter iter; 572 int pos = -1; 573 574 intel_dp_link_caps_iter_start(&iter, link_caps, order, filter); 575 for_each_dp_link_config(&iter, &iter_config) { 576 if (iter_config.rate == link_config->rate && 577 iter_config.lane_count == link_config->lane_count) { 578 pos = iter.pos; 579 580 break; 581 } 582 } 583 intel_dp_link_caps_iter_end(&iter); 584 585 if (pos < 0) 586 return pos; 587 588 return iter_pos_to_idx(link_caps, order, pos); 589 } 590 591 bool intel_dp_link_caps_filter_add(struct intel_dp_link_caps *link_caps, 592 struct intel_dp_link_caps_filter *filter, 593 const struct intel_dp_link_config *config) 594 { 595 int idx; 596 597 idx = find_config_idx(link_caps, get_allowed_config_filter(link_caps), config); 598 if (idx < 0) 599 return false; 600 601 filter->config_mask |= BIT(idx); 602 603 return true; 604 } 605 606 static bool intel_dp_link_caps_filter_remove(struct intel_dp_link_caps *link_caps, 607 struct intel_dp_link_caps_filter *filter, 608 const struct intel_dp_link_config *config) 609 { 610 int idx; 611 612 idx = find_config_idx(link_caps, get_allowed_config_filter(link_caps), config); 613 if (idx < 0) 614 return false; 615 616 filter->config_mask &= ~BIT(idx); 617 618 return true; 619 } 620 621 static void set_max_link_limits(struct intel_dp_link_caps *link_caps, 622 const struct intel_dp_link_config *max_link_limits) 623 { 624 link_caps->max_limits = *max_link_limits; 625 } 626 627 static void reset_max_link_limits(struct intel_dp_link_caps *link_caps) 628 { 629 struct intel_dp_link_config max_link_limits = { 630 .rate = intel_dp_max_common_rate(link_caps), 631 .lane_count = intel_dp_link_caps_max_common_lane_count(link_caps), 632 }; 633 634 set_max_link_limits(link_caps, &max_link_limits); 635 } 636 637 static void reset_max_link_limits_reenable_all(struct intel_dp_link_caps *link_caps) 638 { 639 link_caps->enabled_configs = INTEL_DP_LINK_CAPS_FILTER_ALL; 640 reset_max_link_limits(link_caps); 641 } 642 643 /** 644 * intel_dp_link_caps_disable_config - disable a configuration 645 * @link_caps: link capabilities state 646 * @config: configuration to disable 647 * 648 * Disable the configuration identified by @config. This removes the 649 * configuration from the set of allowed configurations. The disabling 650 * shouldn't leave the remaining configuration set empty. 651 * 652 * The configuration remains disallowed until intel_dp_link_caps() with 653 * reset=%true or changed sink capabilities is called, or 654 * intel_dp_link_caps_reset() is called. Each of these happens after a 655 * new sink is connected or the currently connected sink changes its 656 * capabilities. 657 * 658 * Return: 659 * - %true if @config was valid and the derived state was updated. 660 * - %false if @config was invalid or the remaining configuration set 661 * would remain empty. 662 */ 663 bool intel_dp_link_caps_disable_config(struct intel_dp_link_caps *link_caps, 664 const struct intel_dp_link_config *config) 665 { 666 struct intel_dp_link_caps_filter enabled_configs = link_caps->enabled_configs; 667 struct intel_dp_link_config forced_params; 668 669 if (!intel_dp_link_caps_filter_remove(link_caps, &enabled_configs, config)) 670 return false; 671 672 intel_dp_link_caps_get_forced_params(link_caps, &forced_params); 673 674 if (!calc_allowed_config_filter(link_caps, enabled_configs, 675 &link_caps->max_limits, &forced_params).config_mask) 676 return false; 677 678 link_caps->enabled_configs = enabled_configs; 679 680 return true; 681 } 682 683 /** 684 * intel_dp_link_caps_get_max_limits - get the current maximum link limits 685 * @link_caps: link capabilities state 686 * @max_link_limits: returned maximum link limits 687 * 688 * Return the current maximum rate and lane count limits in 689 * @max_link_limits. 690 * 691 * These limits constrain the set of allowed configurations. 692 * 693 * The limits are set to the maximum common supported values after 694 * intel_dp_link_caps_reset() is called, and can later be modified by 695 * intel_dp_link_caps_set_max_limits(). The max rate and lane count 696 * parameters are independent limits, so the pair does not necessarily 697 * define a valid configuration. 698 * 699 * This function may be called without serializing against updates to 700 * @link_caps. However, without such serialization the returned value may be 701 * an out-of-sync (link rate, lane count) tuple, i.e. the parameters may 702 * belong to different update snapshots in time. 703 */ 704 void intel_dp_link_caps_get_max_limits(struct intel_dp_link_caps *link_caps, 705 struct intel_dp_link_config *max_link_limits) 706 { 707 *max_link_limits = link_caps->max_limits; 708 } 709 710 static bool max_link_limits_valid(struct intel_dp_link_caps *link_caps, 711 const struct intel_dp_link_config *max_link_limits) 712 { 713 struct intel_dp_link_caps_filter allowed_configs; 714 struct intel_dp_link_config forced_params; 715 716 if (max_link_limits->lane_count > INTEL_DP_MAX_LANE_COUNT || 717 !is_power_of_2(max_link_limits->lane_count)) 718 return false; 719 720 /* TODO: Validate max_link_limits->rate against the source supported rates. */ 721 722 intel_dp_link_caps_get_forced_params(link_caps, &forced_params); 723 allowed_configs = calc_allowed_config_filter(link_caps, link_caps->enabled_configs, 724 max_link_limits, &forced_params); 725 726 return allowed_configs.config_mask != 0; 727 } 728 729 /** 730 * intel_dp_link_caps_set_max_limits - set the current maximum link limits 731 * @link_caps: link capabilities state 732 * @max_link_limits: new maximum link limits 733 * 734 * Set the current maximum rate and lane count limits to @max_link_limits, 735 * constraining the set of allowed configurations. 736 * 737 * The new limits must leave at least one configuration allowed: the limits 738 * must not be below the currently active forced parameters or below all the 739 * configurations that remain after disabled configurations are excluded. 740 * 741 * Unlike intel_dp_link_caps_get_max_limits(), the caller must serialize 742 * this call against concurrent queries and updates to @link_caps, in line 743 * with the rest of the API. 744 * 745 * Return: 746 * - %true if the @link_caps cached max limits value got updated with 747 * @max_link_limits. 748 * - %false if @max_link_limits is invalid. 749 */ 750 bool intel_dp_link_caps_set_max_limits(struct intel_dp_link_caps *link_caps, 751 const struct intel_dp_link_config *max_link_limits) 752 { 753 if (!max_link_limits_valid(link_caps, max_link_limits)) 754 return false; 755 756 set_max_link_limits(link_caps, max_link_limits); 757 758 return true; 759 } 760 761 /** 762 * intel_dp_link_caps_reset_max_limits - reset the current maximum link limits 763 * @link_caps: link capabilities state 764 * 765 * Reset the current maximum link limits to the maximum supported common link 766 * rate and lane count. 767 */ 768 void intel_dp_link_caps_reset_max_limits(struct intel_dp_link_caps *link_caps) 769 { 770 reset_max_link_limits(link_caps); 771 } 772 773 static int intel_dp_link_config_bw(struct intel_dp_link_caps *link_caps, 774 const struct intel_dp_link_config_entry *lce) 775 { 776 return drm_dp_max_dprx_data_rate(intel_dp_link_config_rate(link_caps, lce), 777 intel_dp_link_config_lane_count(lce)); 778 } 779 780 static int link_config_cmp_by_bw(const void *a, const void *b, const void *p) 781 { 782 struct intel_dp *intel_dp = (struct intel_dp *)p; /* remove const */ 783 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 784 785 const struct intel_dp_link_config_entry *lce_a = a; 786 const struct intel_dp_link_config_entry *lce_b = b; 787 int bw_a = intel_dp_link_config_bw(link_caps, lce_a); 788 int bw_b = intel_dp_link_config_bw(link_caps, lce_b); 789 790 if (bw_a != bw_b) 791 return bw_a - bw_b; 792 793 return intel_dp_link_config_rate(link_caps, lce_a) - 794 intel_dp_link_config_rate(link_caps, lce_b); 795 } 796 797 static int link_config_cmp_by_rate_lane(const void *a, const void *b, const void *p) 798 { 799 const struct intel_dp_link_caps *link_caps = p; 800 u8 *lce_a_idx = (u8 *)a; 801 u8 *lce_b_idx = (u8 *)b; 802 const struct intel_dp_link_config_entry *lce_a = &link_caps->configs[*lce_a_idx]; 803 const struct intel_dp_link_config_entry *lce_b = &link_caps->configs[*lce_b_idx]; 804 805 if (lce_a->link_rate_idx != lce_b->link_rate_idx) 806 return lce_a->link_rate_idx - lce_b->link_rate_idx; 807 808 return lce_a->lane_count_exp - lce_b->lane_count_exp; 809 } 810 811 static int link_config_cmp_by_lane_rate(const void *a, const void *b, const void *p) 812 { 813 const struct intel_dp_link_caps *link_caps = p; 814 u8 *lce_a_idx = (u8 *)a; 815 u8 *lce_b_idx = (u8 *)b; 816 const struct intel_dp_link_config_entry *lce_a = &link_caps->configs[*lce_a_idx]; 817 const struct intel_dp_link_config_entry *lce_b = &link_caps->configs[*lce_b_idx]; 818 819 if (lce_a->lane_count_exp != lce_b->lane_count_exp) 820 return lce_a->lane_count_exp - lce_b->lane_count_exp; 821 822 return lce_a->link_rate_idx - lce_b->link_rate_idx; 823 } 824 825 /** 826 * intel_dp_link_caps_update - rebuild the supported link configuration state 827 * @link_caps: link capabilities state 828 * @rates: supported common link rates 829 * @num_rates: number of entries in @rates 830 * @max_lane_count: supported maximum lane count 831 * @reset: reset limits and disabled configs 832 * 833 * Rebuild the supported link configuration state from @rates and 834 * @max_lane_count. 835 * 836 * If @reset is %true, reset the maximum link limits to the maximum 837 * supported rate and lane count, and re-enable all configurations. 838 * 839 * This function is called regularly, at least after a sink is connected, 840 * but it may also be called later whenever the sink capabilities may have 841 * changed, for example in response to HPD IRQ / RX_CAP_CHANGED signaling. 842 * 843 * In the Intel driver this function is currently called whenever the 844 * connector detect handler runs, after reading the sink capabilities. This 845 * may change if those capabilities are cached until the sink is 846 * disconnected, or until RX_CAP_CHANGED is signaled. In any case, this 847 * function should be called whenever the sink capabilities were read out 848 * and may have changed. 849 * 850 * Returns: 851 * - %true if the link capabilities have changed, %false otherwise. 852 */ 853 bool intel_dp_link_caps_update(struct intel_dp_link_caps *link_caps, 854 const int *rates, int num_rates, int max_lane_count, 855 bool reset) 856 { 857 struct intel_dp *intel_dp = link_caps->dp; 858 struct intel_display *display = to_intel_display(intel_dp); 859 struct intel_dp_link_config_entry *lce; 860 bool link_params_changed = reset; 861 int num_common_lane_configs; 862 int i; 863 int j; 864 865 if (drm_WARN_ON(display->drm, !is_power_of_2(max_lane_count))) 866 return false; 867 868 if (drm_WARN_ON(display->drm, num_rates > ARRAY_SIZE(link_caps->rates))) 869 return false; 870 871 num_common_lane_configs = ilog2(max_lane_count) + 1; 872 873 if (drm_WARN_ON(display->drm, num_rates * num_common_lane_configs > 874 ARRAY_SIZE(link_caps->configs))) 875 return false; 876 877 /* TODO: Add a struct containing both rates and number of rates. */ 878 static_assert(__same_type(rates[0], link_caps->rates[0])); 879 if (num_rates != link_caps->num_rates || 880 memcmp(rates, link_caps->rates, num_rates * sizeof(rates[0]))) 881 link_params_changed = true; 882 883 if (max_lane_count != link_caps->max_lane_count) 884 link_params_changed = true; 885 886 memcpy(link_caps->rates, rates, num_rates * sizeof(rates[0])); 887 link_caps->num_rates = num_rates; 888 link_caps->max_lane_count = max_lane_count; 889 890 link_caps->num_configs = num_rates * num_common_lane_configs; 891 892 lce = &link_caps->configs[0]; 893 for (i = 0; i < link_caps->num_rates; i++) { 894 for (j = 0; j < num_common_lane_configs; j++) { 895 lce->lane_count_exp = j; 896 lce->link_rate_idx = i; 897 898 lce++; 899 } 900 } 901 902 sort_r(link_caps->configs, link_caps->num_configs, 903 sizeof(link_caps->configs[0]), 904 link_config_cmp_by_bw, NULL, 905 intel_dp); 906 907 for (i = 0; i < link_caps->num_configs; i++) { 908 link_caps->rate_lane_map[i] = i; 909 link_caps->lane_rate_map[i] = i; 910 } 911 912 sort_r(link_caps->rate_lane_map, link_caps->num_configs, 913 sizeof(link_caps->rate_lane_map[0]), 914 link_config_cmp_by_rate_lane, NULL, 915 link_caps); 916 917 sort_r(link_caps->lane_rate_map, link_caps->num_configs, 918 sizeof(link_caps->lane_rate_map[0]), 919 link_config_cmp_by_lane_rate, NULL, 920 link_caps); 921 922 if (link_params_changed) 923 reset_max_link_limits_reenable_all(link_caps); 924 925 return link_params_changed; 926 } 927 928 /** 929 * intel_dp_link_caps_reset - reset link capability restrictions 930 * @link_caps: link capabilities state 931 * 932 * Reset all current restrictions except for the user requested forced 933 * parameters, thus updating the set of allowed configurations and the 934 * derived maximum link information accordingly. 935 * 936 * This function is regularly called after a sink is connected, either 937 * for the first time to the connector or after a previous sink was 938 * disconnected from it, and intel_dp_link_caps_update() was called. 939 */ 940 void intel_dp_link_caps_reset(struct intel_dp_link_caps *link_caps) 941 { 942 reset_max_link_limits_reenable_all(link_caps); 943 } 944 945 static int i915_dp_force_link_rate_show(struct seq_file *m, void *data) 946 { 947 struct intel_connector *connector = to_intel_connector(m->private); 948 struct intel_display *display = to_intel_display(connector); 949 struct intel_dp *intel_dp = intel_attached_dp(connector); 950 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 951 int current_rate = -1; 952 int force_rate; 953 int err; 954 int i; 955 956 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 957 if (err) 958 return err; 959 960 intel_dp_flush_connector_commits(connector); 961 962 if (intel_dp->link.active) 963 current_rate = intel_dp->link_rate; 964 965 force_rate = link_caps->forced_params.rate; 966 967 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 968 969 seq_printf(m, "%sauto%s", 970 force_rate == 0 ? "[" : "", 971 force_rate == 0 ? "]" : ""); 972 973 for (i = 0; i < intel_dp->num_source_rates; i++) 974 seq_printf(m, " %s%d%s%s", 975 intel_dp->source_rates[i] == force_rate ? "[" : "", 976 intel_dp->source_rates[i], 977 intel_dp->source_rates[i] == current_rate ? "*" : "", 978 intel_dp->source_rates[i] == force_rate ? "]" : ""); 979 980 seq_putc(m, '\n'); 981 982 return 0; 983 } 984 985 static int parse_link_rate(struct intel_dp_link_caps *link_caps, const char __user *ubuf, size_t len) 986 { 987 struct intel_dp *intel_dp = link_caps->dp; 988 char *kbuf; 989 const char *p; 990 int rate; 991 int ret = 0; 992 993 kbuf = memdup_user_nul(ubuf, len); 994 if (IS_ERR(kbuf)) 995 return PTR_ERR(kbuf); 996 997 p = strim(kbuf); 998 999 if (!strcmp(p, "auto")) { 1000 rate = 0; 1001 } else { 1002 ret = kstrtoint(p, 0, &rate); 1003 if (ret < 0) 1004 goto out_free; 1005 1006 if (intel_dp_rate_index(intel_dp->source_rates, 1007 intel_dp->num_source_rates, 1008 rate) < 0) 1009 ret = -EINVAL; 1010 } 1011 1012 out_free: 1013 kfree(kbuf); 1014 1015 return ret < 0 ? ret : rate; 1016 } 1017 1018 static ssize_t i915_dp_force_link_rate_write(struct file *file, 1019 const char __user *ubuf, 1020 size_t len, loff_t *offp) 1021 { 1022 struct seq_file *m = file->private_data; 1023 struct intel_connector *connector = to_intel_connector(m->private); 1024 struct intel_display *display = to_intel_display(connector); 1025 struct intel_dp *intel_dp = intel_attached_dp(connector); 1026 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1027 int rate; 1028 int err; 1029 1030 rate = parse_link_rate(link_caps, ubuf, len); 1031 if (rate < 0) 1032 return rate; 1033 1034 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1035 if (err) 1036 return err; 1037 1038 intel_dp_flush_connector_commits(connector); 1039 1040 intel_dp_reset_link_params(intel_dp); 1041 link_caps->forced_params.rate = rate; 1042 1043 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1044 1045 *offp += len; 1046 1047 return len; 1048 } 1049 DEFINE_SHOW_STORE_ATTRIBUTE(i915_dp_force_link_rate); 1050 1051 static int i915_dp_force_lane_count_show(struct seq_file *m, void *data) 1052 { 1053 struct intel_connector *connector = to_intel_connector(m->private); 1054 struct intel_display *display = to_intel_display(connector); 1055 struct intel_dp *intel_dp = intel_attached_dp(connector); 1056 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1057 int current_lane_count = -1; 1058 int force_lane_count; 1059 int err; 1060 int i; 1061 1062 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1063 if (err) 1064 return err; 1065 1066 intel_dp_flush_connector_commits(connector); 1067 1068 if (intel_dp->link.active) 1069 current_lane_count = intel_dp->lane_count; 1070 force_lane_count = link_caps->forced_params.lane_count; 1071 1072 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1073 1074 seq_printf(m, "%sauto%s", 1075 force_lane_count == 0 ? "[" : "", 1076 force_lane_count == 0 ? "]" : ""); 1077 1078 for (i = 1; i <= 4; i <<= 1) 1079 seq_printf(m, " %s%d%s%s", 1080 i == force_lane_count ? "[" : "", 1081 i, 1082 i == current_lane_count ? "*" : "", 1083 i == force_lane_count ? "]" : ""); 1084 1085 seq_putc(m, '\n'); 1086 1087 return 0; 1088 } 1089 1090 static int parse_lane_count(const char __user *ubuf, size_t len) 1091 { 1092 char *kbuf; 1093 const char *p; 1094 int lane_count; 1095 int ret = 0; 1096 1097 kbuf = memdup_user_nul(ubuf, len); 1098 if (IS_ERR(kbuf)) 1099 return PTR_ERR(kbuf); 1100 1101 p = strim(kbuf); 1102 1103 if (!strcmp(p, "auto")) { 1104 lane_count = 0; 1105 } else { 1106 ret = kstrtoint(p, 0, &lane_count); 1107 if (ret < 0) 1108 goto out_free; 1109 1110 switch (lane_count) { 1111 case 1: 1112 case 2: 1113 case 4: 1114 break; 1115 default: 1116 ret = -EINVAL; 1117 } 1118 } 1119 1120 out_free: 1121 kfree(kbuf); 1122 1123 return ret < 0 ? ret : lane_count; 1124 } 1125 1126 static ssize_t i915_dp_force_lane_count_write(struct file *file, 1127 const char __user *ubuf, 1128 size_t len, loff_t *offp) 1129 { 1130 struct seq_file *m = file->private_data; 1131 struct intel_connector *connector = to_intel_connector(m->private); 1132 struct intel_display *display = to_intel_display(connector); 1133 struct intel_dp *intel_dp = intel_attached_dp(connector); 1134 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1135 int lane_count; 1136 int err; 1137 1138 lane_count = parse_lane_count(ubuf, len); 1139 if (lane_count < 0) 1140 return lane_count; 1141 1142 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1143 if (err) 1144 return err; 1145 1146 intel_dp_flush_connector_commits(connector); 1147 1148 intel_dp_reset_link_params(intel_dp); 1149 link_caps->forced_params.lane_count = lane_count; 1150 1151 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1152 1153 *offp += len; 1154 1155 return len; 1156 } 1157 DEFINE_SHOW_STORE_ATTRIBUTE(i915_dp_force_lane_count); 1158 1159 static int i915_dp_max_link_rate_show(void *data, u64 *val) 1160 { 1161 struct intel_connector *connector = to_intel_connector(data); 1162 struct intel_display *display = to_intel_display(connector); 1163 struct intel_dp *intel_dp = intel_attached_dp(connector); 1164 struct intel_dp_link_config max_link_limits; 1165 int err; 1166 1167 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1168 if (err) 1169 return err; 1170 1171 intel_dp_flush_connector_commits(connector); 1172 1173 intel_dp_link_caps_get_max_limits(intel_dp->link.caps, &max_link_limits); 1174 *val = max_link_limits.rate; 1175 1176 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1177 1178 return 0; 1179 } 1180 DEFINE_DEBUGFS_ATTRIBUTE(i915_dp_max_link_rate_fops, i915_dp_max_link_rate_show, NULL, "%llu\n"); 1181 1182 static int i915_dp_max_lane_count_show(void *data, u64 *val) 1183 { 1184 struct intel_connector *connector = to_intel_connector(data); 1185 struct intel_display *display = to_intel_display(connector); 1186 struct intel_dp *intel_dp = intel_attached_dp(connector); 1187 struct intel_dp_link_config max_link_limits; 1188 int err; 1189 1190 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1191 if (err) 1192 return err; 1193 1194 intel_dp_flush_connector_commits(connector); 1195 1196 intel_dp_link_caps_get_max_limits(intel_dp->link.caps, &max_link_limits); 1197 *val = max_link_limits.lane_count; 1198 1199 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1200 1201 return 0; 1202 } 1203 DEFINE_DEBUGFS_ATTRIBUTE(i915_dp_max_lane_count_fops, i915_dp_max_lane_count_show, NULL, "%llu\n"); 1204 1205 static int intel_dp_allowed_link_configs_show(struct seq_file *m, void *data) 1206 { 1207 struct intel_connector *connector = to_intel_connector(m->private); 1208 struct intel_display *display = to_intel_display(connector); 1209 struct intel_dp *intel_dp = intel_attached_dp(connector); 1210 struct intel_dp_link_caps *link_caps = intel_dp->link.caps; 1211 struct intel_dp_link_config link_config; 1212 struct intel_dp_link_caps_iter iter; 1213 int err; 1214 int i; 1215 1216 err = drm_modeset_lock_single_interruptible(&display->drm->mode_config.connection_mutex); 1217 if (err) 1218 return err; 1219 1220 intel_dp_flush_connector_commits(connector); 1221 1222 i = 0; 1223 intel_dp_link_caps_iter_start(&iter, 1224 link_caps, 1225 intel_dp_link_caps_connector_compute_order(connector), 1226 INTEL_DP_LINK_CAPS_FILTER_ALL); 1227 for_each_dp_link_config(&iter, &link_config) { 1228 seq_printf(m, "%s%dx%d", 1229 i ? " " : "", 1230 link_config.lane_count, link_config.rate); 1231 i++; 1232 } 1233 intel_dp_link_caps_iter_end(&iter); 1234 1235 drm_modeset_unlock(&display->drm->mode_config.connection_mutex); 1236 1237 seq_putc(m, '\n'); 1238 1239 return 0; 1240 } 1241 DEFINE_SHOW_ATTRIBUTE(intel_dp_allowed_link_configs); 1242 1243 /** 1244 * intel_dp_link_caps_debugfs_add - add link caps debugfs files for a connector 1245 * @connector: connector to add the debugfs files for 1246 * 1247 * Add the link-capability debugfs files for a DP @connector. 1248 */ 1249 void intel_dp_link_caps_debugfs_add(struct intel_connector *connector) 1250 { 1251 struct dentry *root = connector->base.debugfs_entry; 1252 1253 if (connector->base.connector_type != DRM_MODE_CONNECTOR_DisplayPort && 1254 connector->base.connector_type != DRM_MODE_CONNECTOR_eDP) 1255 return; 1256 1257 debugfs_create_file("i915_dp_force_link_rate", 0644, root, 1258 connector, &i915_dp_force_link_rate_fops); 1259 1260 debugfs_create_file("i915_dp_force_lane_count", 0644, root, 1261 connector, &i915_dp_force_lane_count_fops); 1262 1263 debugfs_create_file("i915_dp_max_link_rate", 0444, root, 1264 connector, &i915_dp_max_link_rate_fops); 1265 1266 debugfs_create_file("i915_dp_max_lane_count", 0444, root, 1267 connector, &i915_dp_max_lane_count_fops); 1268 1269 debugfs_create_file("intel_dp_allowed_link_configs", 0444, root, 1270 connector, &intel_dp_allowed_link_configs_fops); 1271 } 1272 1273 struct intel_dp_link_caps *intel_dp_link_caps_init(struct intel_dp *intel_dp) 1274 { 1275 struct intel_dp_link_caps *link_caps; 1276 1277 link_caps = kzalloc_obj(*link_caps); 1278 if (!link_caps) 1279 return NULL; 1280 1281 link_caps->dp = intel_dp; 1282 link_caps->enabled_configs = INTEL_DP_LINK_CAPS_FILTER_ALL; 1283 1284 return link_caps; 1285 } 1286 1287 void intel_dp_link_caps_cleanup(struct intel_dp_link_caps *link_caps) 1288 { 1289 kfree(link_caps); 1290 } 1291 1292 #if IS_ENABLED(CONFIG_KUNIT) 1293 1294 #define __INIT_MEMBER(__name, __fn) \ 1295 .__name = __fn, 1296 1297 #define INTEL_DP_LINK_CAPS_TEST_OPS_INIT \ 1298 INTEL_DP_LINK_CAPS_TEST_OPS_MEMBERS(__INIT_MEMBER) 1299 1300 #ifdef I915 1301 1302 const struct intel_dp_link_caps_test_ops i915_display_dp_link_caps_test_ops = { 1303 INTEL_DP_LINK_CAPS_TEST_OPS_INIT 1304 }; 1305 EXPORT_SYMBOL(i915_display_dp_link_caps_test_ops); 1306 1307 #else 1308 1309 const struct intel_dp_link_caps_test_ops intel_display_dp_link_caps_test_ops = { 1310 INTEL_DP_LINK_CAPS_TEST_OPS_INIT 1311 }; 1312 EXPORT_SYMBOL(intel_display_dp_link_caps_test_ops); 1313 1314 #endif /* I915 */ 1315 1316 #undef INTEL_DP_LINK_CAPS_TEST_OPS_INIT 1317 #undef __INIT_MEMBER 1318 1319 #endif /* CONFIG_KUNIT */ 1320