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