1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2023 Intel Corporation 4 */ 5 6 #include <linux/export.h> 7 #include <linux/ref_tracker.h> 8 #include <linux/types.h> 9 10 #include <drm/drm_atomic_state_helper.h> 11 12 #include <drm/drm_atomic.h> 13 #include <drm/drm_print.h> 14 #include <drm/display/drm_dp.h> 15 #include <drm/display/drm_dp_helper.h> 16 #include <drm/display/drm_dp_tunnel.h> 17 18 #define to_group(__private_obj) \ 19 container_of(__private_obj, struct drm_dp_tunnel_group, base) 20 21 #define to_group_state(__private_state) \ 22 container_of(__private_state, struct drm_dp_tunnel_group_state, base) 23 24 #define is_dp_tunnel_private_obj(__obj) \ 25 ((__obj)->funcs == &tunnel_group_funcs) 26 27 #define for_each_new_group_in_state(__state, __new_group_state, __i) \ 28 for ((__i) = 0; \ 29 (__i) < (__state)->num_private_objs; \ 30 (__i)++) \ 31 for_each_if ((__state)->private_objs[__i].ptr && \ 32 is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \ 33 ((__new_group_state) = \ 34 to_group_state((__state)->private_objs[__i].new_state), 1)) 35 36 #define for_each_old_group_in_state(__state, __old_group_state, __i) \ 37 for ((__i) = 0; \ 38 (__i) < (__state)->num_private_objs; \ 39 (__i)++) \ 40 for_each_if ((__state)->private_objs[__i].ptr && \ 41 is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \ 42 ((__old_group_state) = \ 43 to_group_state((__state)->private_objs[__i].old_state), 1)) 44 45 #define for_each_tunnel_in_group(__group, __tunnel) \ 46 list_for_each_entry(__tunnel, &(__group)->tunnels, node) 47 48 #define for_each_tunnel_state(__group_state, __tunnel_state) \ 49 list_for_each_entry(__tunnel_state, &(__group_state)->tunnel_states, node) 50 51 #define for_each_tunnel_state_safe(__group_state, __tunnel_state, __tunnel_state_tmp) \ 52 list_for_each_entry_safe(__tunnel_state, __tunnel_state_tmp, \ 53 &(__group_state)->tunnel_states, node) 54 55 #define kbytes_to_mbits(__kbytes) \ 56 DIV_ROUND_UP((__kbytes) * 8, 1000) 57 58 #define DPTUN_BW_ARG(__bw) ((__bw) < 0 ? (__bw) : kbytes_to_mbits(__bw)) 59 60 #define __tun_prn(__tunnel, __level, __type, __fmt, ...) \ 61 drm_##__level##__type((__tunnel)->group->mgr->dev, \ 62 "[DPTUN %s][%s] " __fmt, \ 63 drm_dp_tunnel_name(__tunnel), \ 64 (__tunnel)->aux->name, ## \ 65 __VA_ARGS__) 66 67 #define tun_dbg(__tunnel, __fmt, ...) \ 68 __tun_prn(__tunnel, dbg, _kms, __fmt, ## __VA_ARGS__) 69 70 #define tun_dbg_stat(__tunnel, __err, __fmt, ...) do { \ 71 if (__err) \ 72 __tun_prn(__tunnel, dbg, _kms, __fmt " (Failed, err: %pe)\n", \ 73 ## __VA_ARGS__, ERR_PTR(__err)); \ 74 else \ 75 __tun_prn(__tunnel, dbg, _kms, __fmt " (Ok)\n", \ 76 ## __VA_ARGS__); \ 77 } while (0) 78 79 #define tun_dbg_atomic(__tunnel, __fmt, ...) \ 80 __tun_prn(__tunnel, dbg, _atomic, __fmt, ## __VA_ARGS__) 81 82 #define tun_grp_dbg(__group, __fmt, ...) \ 83 drm_dbg_kms((__group)->mgr->dev, \ 84 "[DPTUN %s] " __fmt, \ 85 drm_dp_tunnel_group_name(__group), ## \ 86 __VA_ARGS__) 87 88 #define DP_TUNNELING_BASE DP_TUNNELING_OUI 89 90 #define __DPTUN_REG_RANGE(__start, __size) \ 91 GENMASK_ULL((__start) + (__size) - 1, (__start)) 92 93 #define DPTUN_REG_RANGE(__addr, __size) \ 94 __DPTUN_REG_RANGE((__addr) - DP_TUNNELING_BASE, (__size)) 95 96 #define DPTUN_REG(__addr) DPTUN_REG_RANGE(__addr, 1) 97 98 #define DPTUN_INFO_REG_MASK ( \ 99 DPTUN_REG_RANGE(DP_TUNNELING_OUI, DP_TUNNELING_OUI_BYTES) | \ 100 DPTUN_REG_RANGE(DP_TUNNELING_DEV_ID, DP_TUNNELING_DEV_ID_BYTES) | \ 101 DPTUN_REG(DP_TUNNELING_HW_REV) | \ 102 DPTUN_REG(DP_TUNNELING_SW_REV_MAJOR) | \ 103 DPTUN_REG(DP_TUNNELING_SW_REV_MINOR) | \ 104 DPTUN_REG(DP_TUNNELING_CAPABILITIES) | \ 105 DPTUN_REG(DP_IN_ADAPTER_INFO) | \ 106 DPTUN_REG(DP_USB4_DRIVER_ID) | \ 107 DPTUN_REG(DP_USB4_DRIVER_BW_CAPABILITY) | \ 108 DPTUN_REG(DP_IN_ADAPTER_TUNNEL_INFORMATION) | \ 109 DPTUN_REG(DP_BW_GRANULARITY) | \ 110 DPTUN_REG(DP_ESTIMATED_BW) | \ 111 DPTUN_REG(DP_ALLOCATED_BW) | \ 112 DPTUN_REG(DP_TUNNELING_MAX_LINK_RATE) | \ 113 DPTUN_REG(DP_TUNNELING_MAX_LANE_COUNT) | \ 114 DPTUN_REG(DP_TUNNELING_MAIN_LINK_CHANNEL_CODING) | \ 115 DPTUN_REG(DP_TUNNELING_128B132B_LINK_RATE) | \ 116 DPTUN_REG(DP_DPTX_BW_ALLOCATION_MODE_CONTROL)) 117 118 static const DECLARE_BITMAP(dptun_info_regs, 64) = { 119 DPTUN_INFO_REG_MASK & -1UL, 120 #if BITS_PER_LONG == 32 121 DPTUN_INFO_REG_MASK >> 32, 122 #endif 123 }; 124 125 struct drm_dp_tunnel_regs { 126 u8 buf[HWEIGHT64(DPTUN_INFO_REG_MASK)]; 127 }; 128 129 struct drm_dp_tunnel_group; 130 131 struct drm_dp_tunnel { 132 struct drm_dp_tunnel_group *group; 133 134 struct list_head node; 135 136 struct kref kref; 137 struct ref_tracker *tracker; 138 struct drm_dp_aux *aux; 139 char name[8]; 140 141 int bw_granularity; 142 int estimated_bw; 143 int allocated_bw; 144 145 u8 dprx_128b132b_rates; 146 int max_dprx_rate; 147 u8 max_dprx_lane_count; 148 149 u8 adapter_id; 150 151 bool dprx_128b132b_support:1; 152 bool dprx_128b132b_lane0_mapping_support:1; 153 bool bw_alloc_supported:1; 154 bool bw_alloc_enabled:1; 155 bool has_io_error:1; 156 bool destroyed:1; 157 bool pr_optimization_support:1; 158 }; 159 160 struct drm_dp_tunnel_group_state; 161 162 struct drm_dp_tunnel_state { 163 struct drm_dp_tunnel_group_state *group_state; 164 165 struct drm_dp_tunnel_ref tunnel_ref; 166 167 struct list_head node; 168 169 u32 stream_mask; 170 int *stream_bw; 171 }; 172 173 struct drm_dp_tunnel_group_state { 174 struct drm_private_state base; 175 176 struct list_head tunnel_states; 177 }; 178 179 struct drm_dp_tunnel_group { 180 struct drm_private_obj base; 181 struct drm_dp_tunnel_mgr *mgr; 182 183 struct list_head tunnels; 184 185 /* available BW including the allocated_bw of all tunnels in the group */ 186 int available_bw; 187 188 u8 drv_group_id; 189 char name[8]; 190 191 bool active:1; 192 }; 193 194 struct drm_dp_tunnel_mgr { 195 struct drm_device *dev; 196 197 int group_count; 198 struct drm_dp_tunnel_group *groups; 199 wait_queue_head_t bw_req_queue; 200 201 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG 202 struct ref_tracker_dir ref_tracker; 203 #endif 204 }; 205 206 /* 207 * The following helpers provide a way to read out the tunneling DPCD 208 * registers with a minimal amount of AUX transfers (1 transfer per contiguous 209 * range, as permitted by the 16 byte per transfer AUX limit), not accessing 210 * other registers to avoid any read side-effects. 211 */ 212 static int next_reg_area(int *offset) 213 { 214 *offset = find_next_bit(dptun_info_regs, 64, *offset); 215 216 return find_next_zero_bit(dptun_info_regs, 64, *offset + 1) - *offset; 217 } 218 219 #define tunnel_reg_ptr(__regs, __address) ({ \ 220 WARN_ON(!test_bit((__address) - DP_TUNNELING_BASE, dptun_info_regs)); \ 221 &(__regs)->buf[bitmap_weight(dptun_info_regs, (__address) - DP_TUNNELING_BASE)]; \ 222 }) 223 224 static int read_tunnel_regs(struct drm_dp_aux *aux, struct drm_dp_tunnel_regs *regs) 225 { 226 int offset = 0; 227 int len; 228 229 while ((len = next_reg_area(&offset))) { 230 int address = DP_TUNNELING_BASE + offset; 231 232 if (drm_dp_dpcd_read_data(aux, address, tunnel_reg_ptr(regs, address), len) < 0) 233 return -EIO; 234 235 offset += len; 236 } 237 238 return 0; 239 } 240 241 static u8 tunnel_reg(const struct drm_dp_tunnel_regs *regs, int address) 242 { 243 return *tunnel_reg_ptr(regs, address); 244 } 245 246 static u8 tunnel_reg_drv_group_id(const struct drm_dp_tunnel_regs *regs) 247 { 248 u8 drv_id = tunnel_reg(regs, DP_USB4_DRIVER_ID) & DP_USB4_DRIVER_ID_MASK; 249 u8 group_id = tunnel_reg(regs, DP_IN_ADAPTER_TUNNEL_INFORMATION) & DP_GROUP_ID_MASK; 250 251 if (!group_id) 252 return 0; 253 254 return (drv_id << DP_GROUP_ID_BITS) | group_id; 255 } 256 257 /* Return granularity in kB/s units */ 258 static int tunnel_reg_bw_granularity(const struct drm_dp_tunnel_regs *regs) 259 { 260 int gr = tunnel_reg(regs, DP_BW_GRANULARITY) & DP_BW_GRANULARITY_MASK; 261 262 if (gr > 2) 263 return -1; 264 265 return (250000 << gr) / 8; 266 } 267 268 static bool tunnel_reg_dprx_128b132b_support(const struct drm_dp_tunnel_regs *regs) 269 { 270 return tunnel_reg(regs, DP_TUNNELING_MAIN_LINK_CHANNEL_CODING) & DP_128B132B_DP_SUPPORTED; 271 } 272 273 static bool tunnel_reg_dprx_128b132b_lane0_mapping_support(const struct drm_dp_tunnel_regs *regs) 274 { 275 return tunnel_reg(regs, DP_TUNNELING_128B132B_LINK_RATE) & 276 DP_TUNNELING_128B132B_LL_LANE0_MAPPING_SUPPORT; 277 } 278 279 static u8 tunnel_reg_dprx_128b132b_rates(const struct drm_dp_tunnel_regs *regs) 280 { 281 if (!tunnel_reg_dprx_128b132b_support(regs)) 282 return 0; 283 284 return tunnel_reg(regs, DP_TUNNELING_128B132B_LINK_RATE) & 285 DP_TUNNELING_128B132B_LINK_RATE_MASK; 286 } 287 288 static u8 max_128b132b_rate(u8 rates) 289 { 290 if (rates & DP_TUNNELING_20GBPS_PER_LANE_SUPPORT) 291 return DP_TUNNELING_20GBPS_PER_LANE_SUPPORT; 292 else if (rates & DP_TUNNELING_13_5GBPS_PER_LANE_SUPPORT) 293 return DP_TUNNELING_13_5GBPS_PER_LANE_SUPPORT; 294 else if (rates & DP_TUNNELING_10GBPS_PER_LANE_SUPPORT) 295 return DP_TUNNELING_10GBPS_PER_LANE_SUPPORT; 296 297 WARN_ON(rates); 298 299 return 0; 300 } 301 302 static int tunnel_reg_max_dprx_rate(const struct drm_dp_tunnel_regs *regs) 303 { 304 u8 bw_code = max_128b132b_rate(tunnel_reg_dprx_128b132b_rates(regs)); 305 306 if (!bw_code) 307 bw_code = tunnel_reg(regs, DP_TUNNELING_MAX_LINK_RATE); 308 309 return drm_dp_bw_code_to_link_rate(bw_code); 310 } 311 312 static int tunnel_reg_max_dprx_lane_count(const struct drm_dp_tunnel_regs *regs) 313 { 314 return tunnel_reg(regs, DP_TUNNELING_MAX_LANE_COUNT) & 315 DP_TUNNELING_MAX_LANE_COUNT_MASK; 316 } 317 318 static bool tunnel_reg_bw_alloc_supported(const struct drm_dp_tunnel_regs *regs) 319 { 320 u8 cap_mask = DP_TUNNELING_SUPPORT | DP_IN_BW_ALLOCATION_MODE_SUPPORT; 321 322 if ((tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & cap_mask) != cap_mask) 323 return false; 324 325 return tunnel_reg(regs, DP_USB4_DRIVER_BW_CAPABILITY) & 326 DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT; 327 } 328 329 static bool tunnel_reg_bw_alloc_enabled(const struct drm_dp_tunnel_regs *regs) 330 { 331 return tunnel_reg(regs, DP_DPTX_BW_ALLOCATION_MODE_CONTROL) & 332 DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE; 333 } 334 335 static u8 tunnel_group_drv_id(u8 drv_group_id) 336 { 337 return drv_group_id >> DP_GROUP_ID_BITS; 338 } 339 340 static u8 tunnel_group_id(u8 drv_group_id) 341 { 342 return drv_group_id & DP_GROUP_ID_MASK; 343 } 344 345 const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel) 346 { 347 return tunnel->name; 348 } 349 EXPORT_SYMBOL(drm_dp_tunnel_name); 350 351 static const char *drm_dp_tunnel_group_name(const struct drm_dp_tunnel_group *group) 352 { 353 return group->name; 354 } 355 356 static struct drm_dp_tunnel_group * 357 lookup_or_alloc_group(struct drm_dp_tunnel_mgr *mgr, u8 drv_group_id) 358 { 359 struct drm_dp_tunnel_group *group = NULL; 360 int i; 361 362 for (i = 0; i < mgr->group_count; i++) { 363 /* 364 * A tunnel group with 0 group ID shouldn't have more than one 365 * tunnels. 366 */ 367 if (tunnel_group_id(drv_group_id) && 368 mgr->groups[i].drv_group_id == drv_group_id) 369 return &mgr->groups[i]; 370 371 if (!group && !mgr->groups[i].active) 372 group = &mgr->groups[i]; 373 } 374 375 if (!group) { 376 drm_dbg_kms(mgr->dev, 377 "DPTUN: Can't allocate more tunnel groups\n"); 378 return NULL; 379 } 380 381 group->drv_group_id = drv_group_id; 382 group->active = true; 383 384 /* 385 * The group name format here and elsewhere: Driver-ID:Group-ID:* 386 * (* standing for all DP-Adapters/tunnels in the group). 387 */ 388 snprintf(group->name, sizeof(group->name), "%d:%d:*", 389 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1), 390 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1)); 391 392 return group; 393 } 394 395 static void free_group(struct drm_dp_tunnel_group *group) 396 { 397 struct drm_dp_tunnel_mgr *mgr = group->mgr; 398 399 if (drm_WARN_ON(mgr->dev, !list_empty(&group->tunnels))) 400 return; 401 402 group->drv_group_id = 0; 403 group->available_bw = -1; 404 group->active = false; 405 } 406 407 static struct drm_dp_tunnel * 408 tunnel_get(struct drm_dp_tunnel *tunnel) 409 { 410 kref_get(&tunnel->kref); 411 412 return tunnel; 413 } 414 415 static void free_tunnel(struct kref *kref) 416 { 417 struct drm_dp_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref); 418 struct drm_dp_tunnel_group *group = tunnel->group; 419 420 list_del(&tunnel->node); 421 if (list_empty(&group->tunnels)) 422 free_group(group); 423 424 kfree(tunnel); 425 } 426 427 static void tunnel_put(struct drm_dp_tunnel *tunnel) 428 { 429 kref_put(&tunnel->kref, free_tunnel); 430 } 431 432 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG 433 static void track_tunnel_ref(struct drm_dp_tunnel *tunnel, 434 struct ref_tracker **tracker) 435 { 436 ref_tracker_alloc(&tunnel->group->mgr->ref_tracker, 437 tracker, GFP_KERNEL); 438 } 439 440 static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel, 441 struct ref_tracker **tracker) 442 { 443 ref_tracker_free(&tunnel->group->mgr->ref_tracker, 444 tracker); 445 } 446 #else 447 static void track_tunnel_ref(struct drm_dp_tunnel *tunnel, 448 struct ref_tracker **tracker) 449 { 450 } 451 452 static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel, 453 struct ref_tracker **tracker) 454 { 455 } 456 #endif 457 458 /** 459 * drm_dp_tunnel_get - Get a reference for a DP tunnel 460 * @tunnel: Tunnel object 461 * @tracker: Debug tracker for the reference 462 * 463 * Get a reference for @tunnel, along with a debug tracker to help locating 464 * the source of a reference leak/double reference put etc. issue. 465 * 466 * The reference must be dropped after use calling drm_dp_tunnel_put() 467 * passing @tunnel and *@tracker returned from here. 468 * 469 * Returns @tunnel - as a convenience - along with *@tracker. 470 */ 471 struct drm_dp_tunnel * 472 drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel, 473 struct ref_tracker **tracker) 474 { 475 track_tunnel_ref(tunnel, tracker); 476 477 return tunnel_get(tunnel); 478 } 479 EXPORT_SYMBOL(drm_dp_tunnel_get); 480 481 /** 482 * drm_dp_tunnel_put - Put a reference for a DP tunnel 483 * @tunnel: Tunnel object 484 * @tracker: Debug tracker for the reference 485 * 486 * Put a reference for @tunnel along with its debug *@tracker, which 487 * was obtained with drm_dp_tunnel_get(). 488 */ 489 void drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel, 490 struct ref_tracker **tracker) 491 { 492 untrack_tunnel_ref(tunnel, tracker); 493 494 tunnel_put(tunnel); 495 } 496 EXPORT_SYMBOL(drm_dp_tunnel_put); 497 498 static bool add_tunnel_to_group(struct drm_dp_tunnel_mgr *mgr, 499 u8 drv_group_id, 500 struct drm_dp_tunnel *tunnel) 501 { 502 struct drm_dp_tunnel_group *group; 503 504 group = lookup_or_alloc_group(mgr, drv_group_id); 505 if (!group) 506 return false; 507 508 tunnel->group = group; 509 list_add(&tunnel->node, &group->tunnels); 510 511 return true; 512 } 513 514 static struct drm_dp_tunnel * 515 create_tunnel(struct drm_dp_tunnel_mgr *mgr, 516 struct drm_dp_aux *aux, 517 const struct drm_dp_tunnel_regs *regs) 518 { 519 u8 drv_group_id = tunnel_reg_drv_group_id(regs); 520 struct drm_dp_tunnel *tunnel; 521 522 tunnel = kzalloc_obj(*tunnel); 523 if (!tunnel) 524 return NULL; 525 526 INIT_LIST_HEAD(&tunnel->node); 527 528 kref_init(&tunnel->kref); 529 530 tunnel->aux = aux; 531 532 tunnel->adapter_id = tunnel_reg(regs, DP_IN_ADAPTER_INFO) & DP_IN_ADAPTER_NUMBER_MASK; 533 534 snprintf(tunnel->name, sizeof(tunnel->name), "%d:%d:%d", 535 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1), 536 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1), 537 tunnel->adapter_id & ((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1)); 538 539 tunnel->bw_granularity = tunnel_reg_bw_granularity(regs); 540 tunnel->allocated_bw = tunnel_reg(regs, DP_ALLOCATED_BW) * 541 tunnel->bw_granularity; 542 /* 543 * An initial allocated BW of 0 indicates an undefined state: the 544 * actual allocation is determined by the TBT CM, usually following a 545 * legacy allocation policy (based on the max DPRX caps). From the 546 * driver's POV the state becomes defined only after the first 547 * allocation request. 548 */ 549 if (!tunnel->allocated_bw) 550 tunnel->allocated_bw = -1; 551 552 tunnel->bw_alloc_supported = tunnel_reg_bw_alloc_supported(regs); 553 tunnel->bw_alloc_enabled = tunnel_reg_bw_alloc_enabled(regs); 554 tunnel->pr_optimization_support = tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & 555 DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT; 556 557 if (!add_tunnel_to_group(mgr, drv_group_id, tunnel)) { 558 kfree(tunnel); 559 560 return NULL; 561 } 562 563 track_tunnel_ref(tunnel, &tunnel->tracker); 564 565 return tunnel; 566 } 567 568 static void destroy_tunnel(struct drm_dp_tunnel *tunnel) 569 { 570 untrack_tunnel_ref(tunnel, &tunnel->tracker); 571 tunnel_put(tunnel); 572 } 573 574 /** 575 * drm_dp_tunnel_set_io_error - Set the IO error flag for a DP tunnel 576 * @tunnel: Tunnel object 577 * 578 * Set the IO error flag for @tunnel. Drivers can call this function upon 579 * detecting a failure that affects the tunnel functionality, for instance 580 * after a DP AUX transfer failure on the port @tunnel is connected to. 581 * 582 * This disables further management of @tunnel, including any related 583 * AUX accesses for tunneling DPCD registers, returning error to the 584 * initiators of these. The driver is supposed to drop this tunnel and - 585 * optionally - recreate it. 586 */ 587 void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel) 588 { 589 tunnel->has_io_error = true; 590 } 591 EXPORT_SYMBOL(drm_dp_tunnel_set_io_error); 592 593 #define SKIP_DPRX_CAPS_CHECK BIT(0) 594 #define ALLOW_ALLOCATED_BW_CHANGE BIT(1) 595 static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr, 596 const struct drm_dp_tunnel_regs *regs, 597 unsigned int flags) 598 { 599 u8 drv_group_id = tunnel_reg_drv_group_id(regs); 600 bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK); 601 bool ret = true; 602 603 if (!tunnel_reg_bw_alloc_supported(regs)) { 604 if (tunnel_group_id(drv_group_id)) { 605 drm_dbg_kms(mgr->dev, 606 "DPTUN: A non-zero group ID is only allowed with BWA support\n"); 607 ret = false; 608 } 609 610 if (tunnel_reg(regs, DP_ALLOCATED_BW)) { 611 drm_dbg_kms(mgr->dev, 612 "DPTUN: BW is allocated without BWA support\n"); 613 ret = false; 614 } 615 616 return ret; 617 } 618 619 if (!tunnel_group_id(drv_group_id)) { 620 drm_dbg_kms(mgr->dev, 621 "DPTUN: BWA support requires a non-zero group ID\n"); 622 ret = false; 623 } 624 625 if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) { 626 drm_dbg_kms(mgr->dev, 627 "DPTUN: Invalid DPRX lane count: %d\n", 628 tunnel_reg_max_dprx_lane_count(regs)); 629 630 ret = false; 631 } 632 633 if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) { 634 drm_dbg_kms(mgr->dev, 635 "DPTUN: DPRX rate is 0\n"); 636 637 ret = false; 638 } 639 640 if (tunnel_reg_bw_granularity(regs) < 0) { 641 drm_dbg_kms(mgr->dev, 642 "DPTUN: Invalid BW granularity\n"); 643 644 ret = false; 645 } 646 647 if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs, DP_ESTIMATED_BW)) { 648 drm_dbg_kms(mgr->dev, 649 "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n", 650 DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * 651 tunnel_reg_bw_granularity(regs)), 652 DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) * 653 tunnel_reg_bw_granularity(regs))); 654 655 ret = false; 656 } 657 658 return ret; 659 } 660 661 static int tunnel_allocated_bw(const struct drm_dp_tunnel *tunnel) 662 { 663 return max(tunnel->allocated_bw, 0); 664 } 665 666 static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel *tunnel, 667 const struct drm_dp_tunnel_regs *regs, 668 unsigned int flags) 669 { 670 u8 new_drv_group_id = tunnel_reg_drv_group_id(regs); 671 bool ret = true; 672 673 if (tunnel->bw_alloc_supported != tunnel_reg_bw_alloc_supported(regs)) { 674 tun_dbg(tunnel, 675 "BW alloc support has changed %s -> %s\n", 676 str_yes_no(tunnel->bw_alloc_supported), 677 str_yes_no(tunnel_reg_bw_alloc_supported(regs))); 678 679 ret = false; 680 } 681 682 if (tunnel->group->drv_group_id != new_drv_group_id) { 683 tun_dbg(tunnel, 684 "Driver/group ID has changed %d:%d:* -> %d:%d:*\n", 685 tunnel_group_drv_id(tunnel->group->drv_group_id), 686 tunnel_group_id(tunnel->group->drv_group_id), 687 tunnel_group_drv_id(new_drv_group_id), 688 tunnel_group_id(new_drv_group_id)); 689 690 ret = false; 691 } 692 693 if (!tunnel->bw_alloc_supported) 694 return ret; 695 696 if (tunnel->bw_granularity != tunnel_reg_bw_granularity(regs)) { 697 tun_dbg(tunnel, 698 "BW granularity has changed: %d -> %d Mb/s\n", 699 DPTUN_BW_ARG(tunnel->bw_granularity), 700 DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs))); 701 702 ret = false; 703 } 704 705 /* 706 * On some devices at least the BW alloc mode enabled status is always 707 * reported as 0, so skip checking that here. 708 */ 709 710 if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) && 711 tunnel_allocated_bw(tunnel) != 712 tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity) { 713 tun_dbg(tunnel, 714 "Allocated BW has changed: %d -> %d Mb/s\n", 715 DPTUN_BW_ARG(tunnel->allocated_bw), 716 DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity)); 717 718 ret = false; 719 } 720 721 return ret; 722 } 723 724 static int 725 read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel, 726 struct drm_dp_tunnel_regs *regs, 727 unsigned int flags) 728 { 729 int err; 730 731 err = read_tunnel_regs(tunnel->aux, regs); 732 if (err < 0) { 733 drm_dp_tunnel_set_io_error(tunnel); 734 735 return err; 736 } 737 738 if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags)) 739 return -EINVAL; 740 741 if (!tunnel_info_changes_are_valid(tunnel, regs, flags)) 742 return -EINVAL; 743 744 return 0; 745 } 746 747 static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const struct drm_dp_tunnel_regs *regs) 748 { 749 bool changed = false; 750 751 if (tunnel_reg_dprx_128b132b_support(regs) != tunnel->dprx_128b132b_support) { 752 tunnel->dprx_128b132b_support = tunnel_reg_dprx_128b132b_support(regs); 753 changed = true; 754 } 755 756 if (tunnel_reg_dprx_128b132b_lane0_mapping_support(regs) != 757 tunnel->dprx_128b132b_lane0_mapping_support) { 758 tunnel->dprx_128b132b_lane0_mapping_support = 759 tunnel_reg_dprx_128b132b_lane0_mapping_support(regs); 760 changed = true; 761 } 762 763 if (tunnel_reg_dprx_128b132b_rates(regs) != tunnel->dprx_128b132b_rates) { 764 tunnel->dprx_128b132b_rates = tunnel_reg_dprx_128b132b_rates(regs); 765 changed = true; 766 } 767 768 if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate) { 769 tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs); 770 changed = true; 771 } 772 773 if (tunnel_reg_max_dprx_lane_count(regs) != tunnel->max_dprx_lane_count) { 774 tunnel->max_dprx_lane_count = tunnel_reg_max_dprx_lane_count(regs); 775 changed = true; 776 } 777 778 return changed; 779 } 780 781 static int dev_id_len(const u8 *dev_id, int max_len) 782 { 783 while (max_len && dev_id[max_len - 1] == '\0') 784 max_len--; 785 786 return max_len; 787 } 788 789 static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel) 790 { 791 int max_dprx_bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate, 792 tunnel->max_dprx_lane_count); 793 794 /* 795 * A BW request of roundup(max_dprx_bw, tunnel->bw_granularity) results in 796 * an allocation of max_dprx_bw. A BW request above this rounded-up 797 * value will fail. 798 */ 799 return min(roundup(max_dprx_bw, tunnel->bw_granularity), 800 MAX_DP_REQUEST_BW * tunnel->bw_granularity); 801 } 802 803 static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel) 804 { 805 return min(get_max_dprx_bw(tunnel), tunnel->group->available_bw); 806 } 807 808 /** 809 * drm_dp_tunnel_detect - Detect DP tunnel on the link 810 * @mgr: Tunnel manager 811 * @aux: DP AUX on which the tunnel will be detected 812 * 813 * Detect if there is any DP tunnel on the link and add it to the tunnel 814 * group's tunnel list. 815 * 816 * Returns a pointer to a tunnel on success, or an ERR_PTR() error on 817 * failure. 818 */ 819 struct drm_dp_tunnel * 820 drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr, 821 struct drm_dp_aux *aux) 822 { 823 struct drm_dp_tunnel_regs regs; 824 struct drm_dp_tunnel *tunnel; 825 int err; 826 827 err = read_tunnel_regs(aux, ®s); 828 if (err) 829 return ERR_PTR(err); 830 831 if (!(tunnel_reg(®s, DP_TUNNELING_CAPABILITIES) & 832 DP_TUNNELING_SUPPORT)) 833 return ERR_PTR(-ENODEV); 834 835 /* The DPRX caps are valid only after enabling BW alloc mode. */ 836 if (!tunnel_regs_are_valid(mgr, ®s, SKIP_DPRX_CAPS_CHECK)) 837 return ERR_PTR(-EINVAL); 838 839 tunnel = create_tunnel(mgr, aux, ®s); 840 if (!tunnel) 841 return ERR_PTR(-ENOMEM); 842 843 tun_dbg(tunnel, 844 "OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%s BWA-Sup:%s BWA-En:%s\n", 845 DP_TUNNELING_OUI_BYTES, 846 tunnel_reg_ptr(®s, DP_TUNNELING_OUI), 847 dev_id_len(tunnel_reg_ptr(®s, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES), 848 tunnel_reg_ptr(®s, DP_TUNNELING_DEV_ID), 849 (tunnel_reg(®s, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >> 850 DP_TUNNELING_HW_REV_MAJOR_SHIFT, 851 (tunnel_reg(®s, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >> 852 DP_TUNNELING_HW_REV_MINOR_SHIFT, 853 tunnel_reg(®s, DP_TUNNELING_SW_REV_MAJOR), 854 tunnel_reg(®s, DP_TUNNELING_SW_REV_MINOR), 855 str_yes_no(tunnel_reg(®s, DP_TUNNELING_CAPABILITIES) & 856 DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT), 857 str_yes_no(tunnel->bw_alloc_supported), 858 str_yes_no(tunnel->bw_alloc_enabled)); 859 860 return tunnel; 861 } 862 EXPORT_SYMBOL(drm_dp_tunnel_detect); 863 864 /** 865 * drm_dp_tunnel_destroy - Destroy tunnel object 866 * @tunnel: Tunnel object 867 * 868 * Remove the tunnel from the tunnel topology and destroy it. 869 * 870 * Returns 0 on success, -ENODEV if the tunnel has been destroyed already. 871 */ 872 int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel) 873 { 874 if (!tunnel) 875 return 0; 876 877 if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed)) 878 return -ENODEV; 879 880 tun_dbg(tunnel, "destroying\n"); 881 882 tunnel->destroyed = true; 883 destroy_tunnel(tunnel); 884 885 return 0; 886 } 887 EXPORT_SYMBOL(drm_dp_tunnel_destroy); 888 889 static int check_tunnel(const struct drm_dp_tunnel *tunnel) 890 { 891 if (tunnel->destroyed) 892 return -ENODEV; 893 894 if (tunnel->has_io_error) 895 return -EIO; 896 897 return 0; 898 } 899 900 static int group_allocated_bw(struct drm_dp_tunnel_group *group) 901 { 902 struct drm_dp_tunnel *tunnel; 903 int group_allocated_bw = 0; 904 905 for_each_tunnel_in_group(group, tunnel) { 906 if (check_tunnel(tunnel) == 0 && 907 tunnel->bw_alloc_enabled) 908 group_allocated_bw += tunnel_allocated_bw(tunnel); 909 } 910 911 return group_allocated_bw; 912 } 913 914 /* 915 * The estimated BW reported by the TBT Connection Manager for each tunnel in 916 * a group includes the BW already allocated for the given tunnel and the 917 * unallocated BW which is free to be used by any tunnel in the group. 918 */ 919 static int group_free_bw(const struct drm_dp_tunnel *tunnel) 920 { 921 return tunnel->estimated_bw - tunnel_allocated_bw(tunnel); 922 } 923 924 static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel) 925 { 926 return group_allocated_bw(tunnel->group) + 927 group_free_bw(tunnel); 928 } 929 930 static int update_group_available_bw(struct drm_dp_tunnel *tunnel, 931 const struct drm_dp_tunnel_regs *regs) 932 { 933 struct drm_dp_tunnel *tunnel_iter; 934 int group_available_bw; 935 bool changed; 936 937 tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity; 938 939 if (calc_group_available_bw(tunnel) == tunnel->group->available_bw) 940 return 0; 941 942 for_each_tunnel_in_group(tunnel->group, tunnel_iter) { 943 int err; 944 945 if (tunnel_iter == tunnel) 946 continue; 947 948 if (check_tunnel(tunnel_iter) != 0 || 949 !tunnel_iter->bw_alloc_enabled) 950 continue; 951 952 err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV); 953 if (err) { 954 tun_dbg(tunnel_iter, 955 "Probe failed, assume disconnected (err %pe)\n", 956 ERR_PTR(err)); 957 drm_dp_tunnel_set_io_error(tunnel_iter); 958 } 959 } 960 961 group_available_bw = calc_group_available_bw(tunnel); 962 963 tun_dbg(tunnel, "Updated group available BW: %d->%d\n", 964 DPTUN_BW_ARG(tunnel->group->available_bw), 965 DPTUN_BW_ARG(group_available_bw)); 966 967 changed = tunnel->group->available_bw != group_available_bw; 968 969 tunnel->group->available_bw = group_available_bw; 970 971 return changed ? 1 : 0; 972 } 973 974 static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable) 975 { 976 u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ; 977 u8 val; 978 979 if (drm_dp_dpcd_read_byte(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0) 980 goto out_err; 981 982 if (enable) 983 val |= mask; 984 else 985 val &= ~mask; 986 987 if (drm_dp_dpcd_write_byte(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0) 988 goto out_err; 989 990 tunnel->bw_alloc_enabled = enable; 991 992 return 0; 993 994 out_err: 995 drm_dp_tunnel_set_io_error(tunnel); 996 997 return -EIO; 998 } 999 1000 /** 1001 * drm_dp_tunnel_enable_bw_alloc - Enable DP tunnel BW allocation mode 1002 * @tunnel: Tunnel object 1003 * 1004 * Enable the DP tunnel BW allocation mode on @tunnel if it supports it. 1005 * 1006 * Returns 0 in case of success, negative error code otherwise. 1007 */ 1008 int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel) 1009 { 1010 struct drm_dp_tunnel_regs regs; 1011 int err; 1012 1013 err = check_tunnel(tunnel); 1014 if (err) 1015 return err; 1016 1017 if (!tunnel->bw_alloc_supported) 1018 return -EOPNOTSUPP; 1019 1020 if (!tunnel_group_id(tunnel->group->drv_group_id)) 1021 return -EINVAL; 1022 1023 err = set_bw_alloc_mode(tunnel, true); 1024 if (err) 1025 goto out; 1026 1027 /* 1028 * After a BWA disable/re-enable sequence the allocated BW can either 1029 * stay at its last requested value or, for instance after system 1030 * suspend/resume, TBT CM can reset back the allocation to the amount 1031 * allocated in the legacy/non-BWA mode. Accordingly allow for the 1032 * allocation to change wrt. the last SW state. 1033 */ 1034 err = read_and_verify_tunnel_regs(tunnel, ®s, 1035 ALLOW_ALLOCATED_BW_CHANGE); 1036 if (err) { 1037 set_bw_alloc_mode(tunnel, false); 1038 1039 goto out; 1040 } 1041 1042 if (!tunnel->max_dprx_rate) 1043 update_dprx_caps(tunnel, ®s); 1044 1045 if (tunnel->group->available_bw == -1) { 1046 err = update_group_available_bw(tunnel, ®s); 1047 if (err > 0) 1048 err = 0; 1049 } 1050 out: 1051 tun_dbg_stat(tunnel, err, 1052 "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s", 1053 tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count, 1054 DPTUN_BW_ARG(group_allocated_bw(tunnel->group)), 1055 DPTUN_BW_ARG(tunnel->group->available_bw)); 1056 1057 return err; 1058 } 1059 EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc); 1060 1061 /** 1062 * drm_dp_tunnel_disable_bw_alloc - Disable DP tunnel BW allocation mode 1063 * @tunnel: Tunnel object 1064 * 1065 * Disable the DP tunnel BW allocation mode on @tunnel. 1066 * 1067 * Returns 0 in case of success, negative error code otherwise. 1068 */ 1069 int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel) 1070 { 1071 int err; 1072 1073 err = check_tunnel(tunnel); 1074 if (err) 1075 return err; 1076 1077 tunnel->allocated_bw = -1; 1078 1079 err = set_bw_alloc_mode(tunnel, false); 1080 1081 tun_dbg_stat(tunnel, err, "Disabling BW alloc mode"); 1082 1083 return err; 1084 } 1085 EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc); 1086 1087 /** 1088 * drm_dp_tunnel_bw_alloc_is_enabled - Query the BW allocation mode enabled state 1089 * @tunnel: Tunnel object 1090 * 1091 * Query if the BW allocation mode is enabled for @tunnel. 1092 * 1093 * Returns %true if the BW allocation mode is enabled for @tunnel. 1094 */ 1095 bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel) 1096 { 1097 return tunnel && tunnel->bw_alloc_enabled; 1098 } 1099 EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled); 1100 1101 /** 1102 * drm_dp_tunnel_pr_optimization_supported - Query the PR BW optimization support 1103 * @tunnel: Tunnel object 1104 * 1105 * Query if the PR BW optimization is supported for @tunnel. 1106 * 1107 * Returns %true if the PR BW optimiation is supported for @tunnel. 1108 */ 1109 bool drm_dp_tunnel_pr_optimization_supported(const struct drm_dp_tunnel *tunnel) 1110 { 1111 return tunnel && tunnel->pr_optimization_support; 1112 } 1113 EXPORT_SYMBOL(drm_dp_tunnel_pr_optimization_supported); 1114 1115 static int clear_bw_req_state(struct drm_dp_aux *aux) 1116 { 1117 u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED; 1118 1119 if (drm_dp_dpcd_write_byte(aux, DP_TUNNELING_STATUS, bw_req_mask) < 0) 1120 return -EIO; 1121 1122 return 0; 1123 } 1124 1125 static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed) 1126 { 1127 u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED; 1128 u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED; 1129 u8 val; 1130 int err; 1131 1132 if (drm_dp_dpcd_read_byte(aux, DP_TUNNELING_STATUS, &val) < 0) 1133 return -EIO; 1134 1135 *status_changed = val & status_change_mask; 1136 1137 val &= bw_req_mask; 1138 1139 if (!val) 1140 return -EAGAIN; 1141 1142 err = clear_bw_req_state(aux); 1143 if (err < 0) 1144 return err; 1145 1146 return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC; 1147 } 1148 1149 static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw) 1150 { 1151 struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr; 1152 int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity); 1153 DEFINE_WAIT_FUNC(wait, woken_wake_function); 1154 long timeout; 1155 int err; 1156 1157 if (bw < 0) { 1158 err = -EINVAL; 1159 goto out; 1160 } 1161 1162 if (request_bw * tunnel->bw_granularity == tunnel->allocated_bw) 1163 return 0; 1164 1165 /* Atomic check should prevent the following. */ 1166 if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) { 1167 err = -EINVAL; 1168 goto out; 1169 } 1170 1171 err = clear_bw_req_state(tunnel->aux); 1172 if (err) 1173 goto out; 1174 1175 if (drm_dp_dpcd_write_byte(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) { 1176 err = -EIO; 1177 goto out; 1178 } 1179 1180 timeout = msecs_to_jiffies(3000); 1181 add_wait_queue(&mgr->bw_req_queue, &wait); 1182 1183 for (;;) { 1184 bool status_changed; 1185 1186 err = bw_req_complete(tunnel->aux, &status_changed); 1187 if (err != -EAGAIN) 1188 break; 1189 1190 if (status_changed) { 1191 struct drm_dp_tunnel_regs regs; 1192 1193 err = read_and_verify_tunnel_regs(tunnel, ®s, 1194 ALLOW_ALLOCATED_BW_CHANGE); 1195 if (err) 1196 break; 1197 } 1198 1199 if (!timeout) { 1200 err = -ETIMEDOUT; 1201 break; 1202 } 1203 1204 timeout = wait_woken(&wait, TASK_UNINTERRUPTIBLE, timeout); 1205 }; 1206 1207 remove_wait_queue(&mgr->bw_req_queue, &wait); 1208 1209 if (err) 1210 goto out; 1211 1212 tunnel->allocated_bw = request_bw * tunnel->bw_granularity; 1213 1214 out: 1215 tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s", 1216 DPTUN_BW_ARG(request_bw * tunnel->bw_granularity), 1217 DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)), 1218 DPTUN_BW_ARG(group_allocated_bw(tunnel->group)), 1219 DPTUN_BW_ARG(tunnel->group->available_bw)); 1220 1221 if (err == -EIO) 1222 drm_dp_tunnel_set_io_error(tunnel); 1223 1224 return err; 1225 } 1226 1227 /** 1228 * drm_dp_tunnel_alloc_bw - Allocate BW for a DP tunnel 1229 * @tunnel: Tunnel object 1230 * @bw: BW in kB/s units 1231 * 1232 * Allocate @bw kB/s for @tunnel. The allocated BW must be freed after use by 1233 * calling this function for the same tunnel setting @bw to 0. 1234 * 1235 * Returns 0 in case of success, a negative error code otherwise. 1236 */ 1237 int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw) 1238 { 1239 int err; 1240 1241 err = check_tunnel(tunnel); 1242 if (err) 1243 return err; 1244 1245 return allocate_tunnel_bw(tunnel, bw); 1246 } 1247 EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw); 1248 1249 /** 1250 * drm_dp_tunnel_get_allocated_bw - Get the BW allocated for a DP tunnel 1251 * @tunnel: Tunnel object 1252 * 1253 * Get the current BW allocated for @tunnel. After the tunnel is created / 1254 * resumed and the BW allocation mode is enabled for it, the allocation 1255 * becomes determined only after the first allocation request by the driver 1256 * calling drm_dp_tunnel_alloc_bw(). 1257 * 1258 * Return the BW allocated for the tunnel, or -1 if the allocation is 1259 * undetermined. 1260 */ 1261 int drm_dp_tunnel_get_allocated_bw(struct drm_dp_tunnel *tunnel) 1262 { 1263 return tunnel->allocated_bw; 1264 } 1265 EXPORT_SYMBOL(drm_dp_tunnel_get_allocated_bw); 1266 1267 /* 1268 * Return 0 if the status hasn't changed, 1 if the status has changed, a 1269 * negative error code in case of an I/O failure. 1270 */ 1271 static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel) 1272 { 1273 u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED; 1274 u8 val; 1275 1276 if (drm_dp_dpcd_read_byte(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0) 1277 goto out_err; 1278 1279 val &= mask; 1280 1281 if (val) { 1282 if (drm_dp_dpcd_write_byte(tunnel->aux, DP_TUNNELING_STATUS, val) < 0) 1283 goto out_err; 1284 1285 return 1; 1286 } 1287 1288 if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel)) 1289 return 0; 1290 1291 /* 1292 * Check for estimated BW changes explicitly to account for lost 1293 * BW change notifications. 1294 */ 1295 if (drm_dp_dpcd_read_byte(tunnel->aux, DP_ESTIMATED_BW, &val) < 0) 1296 goto out_err; 1297 1298 if (val * tunnel->bw_granularity != tunnel->estimated_bw) 1299 return 1; 1300 1301 return 0; 1302 1303 out_err: 1304 drm_dp_tunnel_set_io_error(tunnel); 1305 1306 return -EIO; 1307 } 1308 1309 /** 1310 * drm_dp_tunnel_update_state - Update DP tunnel SW state with the HW state 1311 * @tunnel: Tunnel object 1312 * 1313 * Update the SW state of @tunnel with the HW state. 1314 * 1315 * Returns 0 if the state has not changed, 1 if it has changed and got updated 1316 * successfully and a negative error code otherwise. 1317 */ 1318 int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel) 1319 { 1320 struct drm_dp_tunnel_regs regs; 1321 bool changed = false; 1322 int ret; 1323 1324 ret = check_tunnel(tunnel); 1325 if (ret < 0) 1326 return ret; 1327 1328 ret = check_and_clear_status_change(tunnel); 1329 if (ret < 0) 1330 goto out; 1331 1332 if (!ret) 1333 return 0; 1334 1335 ret = read_and_verify_tunnel_regs(tunnel, ®s, 0); 1336 if (ret) 1337 goto out; 1338 1339 if (update_dprx_caps(tunnel, ®s)) 1340 changed = true; 1341 1342 ret = update_group_available_bw(tunnel, ®s); 1343 if (ret == 1) 1344 changed = true; 1345 1346 out: 1347 tun_dbg_stat(tunnel, ret < 0 ? ret : 0, 1348 "State update: Changed:%s DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s", 1349 str_yes_no(changed), 1350 tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count, 1351 DPTUN_BW_ARG(tunnel->allocated_bw), 1352 DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)), 1353 DPTUN_BW_ARG(group_allocated_bw(tunnel->group)), 1354 DPTUN_BW_ARG(tunnel->group->available_bw)); 1355 1356 if (ret < 0) 1357 return ret; 1358 1359 if (changed) 1360 return 1; 1361 1362 return 0; 1363 } 1364 EXPORT_SYMBOL(drm_dp_tunnel_update_state); 1365 1366 /* 1367 * drm_dp_tunnel_handle_irq - Handle DP tunnel IRQs 1368 * 1369 * Handle any pending DP tunnel IRQs, waking up waiters for a completion 1370 * event. 1371 * 1372 * Returns 1 if the state of the tunnel has changed which requires calling 1373 * drm_dp_tunnel_update_state(), a negative error code in case of a failure, 1374 * 0 otherwise. 1375 */ 1376 int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux) 1377 { 1378 u8 val; 1379 1380 if (drm_dp_dpcd_read_byte(aux, DP_TUNNELING_STATUS, &val) < 0) 1381 return -EIO; 1382 1383 if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED)) 1384 wake_up_all(&mgr->bw_req_queue); 1385 1386 if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED)) 1387 return 1; 1388 1389 return 0; 1390 } 1391 EXPORT_SYMBOL(drm_dp_tunnel_handle_irq); 1392 1393 /** 1394 * drm_dp_tunnel_128b132b_supported - Query if 128b132b is supported by the tunnel's DPRX 1395 * @tunnel: Tunnel object 1396 * 1397 * The function is used to query if 128b132b is supported by the DPRX connected 1398 * to @tunnel. 1399 * 1400 * Returns %true if 128b132b is supported by the DPRX. 1401 */ 1402 bool drm_dp_tunnel_128b132b_supported(const struct drm_dp_tunnel *tunnel) 1403 { 1404 return tunnel->dprx_128b132b_support; 1405 } 1406 EXPORT_SYMBOL(drm_dp_tunnel_128b132b_supported); 1407 1408 /** 1409 * drm_dp_tunnel_128b132b_lane0_mapping_supported - Check 128b/132b lane 0 mapping support 1410 * @tunnel: Tunnel object 1411 * 1412 * Check whether the DP-out adapter always maps lane 0 as expected by the 1413 * DPRX on a tunneled 128b/132b link. If the function returns %true, one- and 1414 * two-lane configurations with UHBR link rates can always be used. If it 1415 * returns %false, using one or two lanes with UHBR link rates may cause a 1416 * lane-count conversion failure in the DPRX, requiring corrective action by 1417 * the source during link training. See DP Standard v2.1b, section 1418 * 3.5.2.16.3, 128b/132b DPRX Lane Count Conversion Failure Indication and 1419 * Corrective Action. 1420 * 1421 * A four-lane configuration can always be used, provided that the DPRX 1422 * supports it, regardless of the function's return value. 1423 * 1424 * Returns %true if the DP-out adapter supports the 128b/132b lane 0 mapping. 1425 */ 1426 bool drm_dp_tunnel_128b132b_lane0_mapping_supported(const struct drm_dp_tunnel *tunnel) 1427 { 1428 return tunnel->dprx_128b132b_lane0_mapping_support; 1429 } 1430 EXPORT_SYMBOL(drm_dp_tunnel_128b132b_lane0_mapping_supported); 1431 1432 /** 1433 * drm_dp_tunnel_128b132b_dprx_rates - Query the supported 128b132b rates of the tunnel's DPRX 1434 * @tunnel: Tunnel object 1435 * 1436 * The function is used to query the supported 128b132b rates of the DPRX connected 1437 * to @tunnel. Note that the related DP_128B132B_SUPPROTED_LINK_RATES DPCD 1438 * register will indicate no supported 128B132B rates for a tunneled DPRX. 1439 * 1440 * Returns the mask of supported 128b132b rates. 1441 */ 1442 u8 drm_dp_tunnel_128b132b_dprx_rates(const struct drm_dp_tunnel *tunnel) 1443 { 1444 return tunnel->dprx_128b132b_rates; 1445 } 1446 EXPORT_SYMBOL(drm_dp_tunnel_128b132b_dprx_rates); 1447 1448 /** 1449 * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX 1450 * @tunnel: Tunnel object 1451 * 1452 * The function is used to query the maximum link rate of the DPRX connected 1453 * to @tunnel. Note that this rate will not be limited by the BW limit of the 1454 * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD 1455 * registers. 1456 * 1457 * Returns the maximum link rate in 10 kbit/s units. 1458 */ 1459 int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel) 1460 { 1461 return tunnel->max_dprx_rate; 1462 } 1463 EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate); 1464 1465 /** 1466 * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX 1467 * @tunnel: Tunnel object 1468 * 1469 * The function is used to query the maximum lane count of the DPRX connected 1470 * to @tunnel. Note that this lane count will not be limited by the BW limit of 1471 * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD 1472 * registers. 1473 * 1474 * Returns the maximum lane count. 1475 */ 1476 int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel) 1477 { 1478 return tunnel->max_dprx_lane_count; 1479 } 1480 EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count); 1481 1482 /** 1483 * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel 1484 * @tunnel: Tunnel object 1485 * 1486 * This function is used to query the estimated total available BW of the 1487 * tunnel. This includes the currently allocated and free BW for all the 1488 * tunnels in @tunnel's group. The available BW is valid only after the BW 1489 * allocation mode has been enabled for the tunnel and its state got updated 1490 * calling drm_dp_tunnel_update_state(). 1491 * 1492 * Returns the @tunnel group's estimated total available bandwidth in kB/s 1493 * units, or -1 if the available BW isn't valid (the BW allocation mode is 1494 * not enabled or the tunnel's state hasn't been updated). 1495 */ 1496 int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel) 1497 { 1498 return tunnel->group->available_bw; 1499 } 1500 EXPORT_SYMBOL(drm_dp_tunnel_available_bw); 1501 1502 static struct drm_dp_tunnel_group_state * 1503 drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_commit *state, 1504 const struct drm_dp_tunnel *tunnel) 1505 { 1506 return (struct drm_dp_tunnel_group_state *) 1507 drm_atomic_get_private_obj_state(state, 1508 &tunnel->group->base); 1509 } 1510 1511 static struct drm_dp_tunnel_state * 1512 add_tunnel_state(struct drm_dp_tunnel_group_state *group_state, 1513 struct drm_dp_tunnel *tunnel) 1514 { 1515 struct drm_dp_tunnel_state *tunnel_state; 1516 1517 tun_dbg_atomic(tunnel, 1518 "Adding state for tunnel %p to group state %p\n", 1519 tunnel, group_state); 1520 1521 tunnel_state = kzalloc_obj(*tunnel_state); 1522 if (!tunnel_state) 1523 return NULL; 1524 1525 tunnel_state->group_state = group_state; 1526 1527 drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref); 1528 1529 INIT_LIST_HEAD(&tunnel_state->node); 1530 list_add(&tunnel_state->node, &group_state->tunnel_states); 1531 1532 return tunnel_state; 1533 } 1534 1535 static void free_tunnel_state(struct drm_dp_tunnel_state *tunnel_state) 1536 { 1537 tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel, 1538 "Freeing state for tunnel %p\n", 1539 tunnel_state->tunnel_ref.tunnel); 1540 1541 list_del(&tunnel_state->node); 1542 1543 kfree(tunnel_state->stream_bw); 1544 drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref); 1545 1546 kfree(tunnel_state); 1547 } 1548 1549 static void free_group_state(struct drm_dp_tunnel_group_state *group_state) 1550 { 1551 struct drm_dp_tunnel_state *tunnel_state; 1552 struct drm_dp_tunnel_state *tunnel_state_tmp; 1553 1554 for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp) 1555 free_tunnel_state(tunnel_state); 1556 1557 kfree(group_state); 1558 } 1559 1560 static struct drm_dp_tunnel_state * 1561 get_tunnel_state(struct drm_dp_tunnel_group_state *group_state, 1562 const struct drm_dp_tunnel *tunnel) 1563 { 1564 struct drm_dp_tunnel_state *tunnel_state; 1565 1566 for_each_tunnel_state(group_state, tunnel_state) 1567 if (tunnel_state->tunnel_ref.tunnel == tunnel) 1568 return tunnel_state; 1569 1570 return NULL; 1571 } 1572 1573 static struct drm_dp_tunnel_state * 1574 get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state, 1575 struct drm_dp_tunnel *tunnel) 1576 { 1577 struct drm_dp_tunnel_state *tunnel_state; 1578 1579 tunnel_state = get_tunnel_state(group_state, tunnel); 1580 if (tunnel_state) 1581 return tunnel_state; 1582 1583 return add_tunnel_state(group_state, tunnel); 1584 } 1585 1586 static struct drm_private_state * 1587 tunnel_group_duplicate_state(struct drm_private_obj *obj) 1588 { 1589 struct drm_dp_tunnel_group_state *group_state; 1590 struct drm_dp_tunnel_state *tunnel_state; 1591 1592 group_state = kzalloc_obj(*group_state); 1593 if (!group_state) 1594 return NULL; 1595 1596 INIT_LIST_HEAD(&group_state->tunnel_states); 1597 1598 __drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base); 1599 1600 for_each_tunnel_state(to_group_state(obj->state), tunnel_state) { 1601 struct drm_dp_tunnel_state *new_tunnel_state; 1602 1603 new_tunnel_state = get_or_add_tunnel_state(group_state, 1604 tunnel_state->tunnel_ref.tunnel); 1605 if (!new_tunnel_state) 1606 goto out_free_state; 1607 1608 new_tunnel_state->stream_mask = tunnel_state->stream_mask; 1609 new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw, 1610 sizeof(*tunnel_state->stream_bw) * 1611 hweight32(tunnel_state->stream_mask), 1612 GFP_KERNEL); 1613 1614 if (!new_tunnel_state->stream_bw) 1615 goto out_free_state; 1616 } 1617 1618 return &group_state->base; 1619 1620 out_free_state: 1621 free_group_state(group_state); 1622 1623 return NULL; 1624 } 1625 1626 static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state) 1627 { 1628 free_group_state(to_group_state(state)); 1629 } 1630 1631 static struct drm_private_state *tunnel_group_atomic_create_state(struct drm_private_obj *obj) 1632 { 1633 struct drm_dp_tunnel_group_state *group_state; 1634 1635 group_state = kzalloc_obj(*group_state); 1636 if (!group_state) 1637 return ERR_PTR(-ENOMEM); 1638 1639 __drm_atomic_helper_private_obj_create_state(obj, &group_state->base); 1640 INIT_LIST_HEAD(&group_state->tunnel_states); 1641 1642 return &group_state->base; 1643 } 1644 1645 static const struct drm_private_state_funcs tunnel_group_funcs = { 1646 .atomic_create_state = tunnel_group_atomic_create_state, 1647 .atomic_duplicate_state = tunnel_group_duplicate_state, 1648 .atomic_destroy_state = tunnel_group_destroy_state, 1649 }; 1650 1651 /** 1652 * drm_dp_tunnel_atomic_get_state - get/allocate the new atomic state for a tunnel 1653 * @state: Atomic state 1654 * @tunnel: Tunnel to get the state for 1655 * 1656 * Get the new atomic state for @tunnel, duplicating it from the old tunnel 1657 * state if not yet allocated. 1658 * 1659 * Return the state or an ERR_PTR() error on failure. 1660 */ 1661 struct drm_dp_tunnel_state * 1662 drm_dp_tunnel_atomic_get_state(struct drm_atomic_commit *state, 1663 struct drm_dp_tunnel *tunnel) 1664 { 1665 struct drm_dp_tunnel_group_state *group_state; 1666 struct drm_dp_tunnel_state *tunnel_state; 1667 1668 group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel); 1669 if (IS_ERR(group_state)) 1670 return ERR_CAST(group_state); 1671 1672 tunnel_state = get_or_add_tunnel_state(group_state, tunnel); 1673 if (!tunnel_state) 1674 return ERR_PTR(-ENOMEM); 1675 1676 return tunnel_state; 1677 } 1678 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state); 1679 1680 /** 1681 * drm_dp_tunnel_atomic_get_old_state - get the old atomic state for a tunnel 1682 * @state: Atomic state 1683 * @tunnel: Tunnel to get the state for 1684 * 1685 * Get the old atomic state for @tunnel. 1686 * 1687 * Return the old state or NULL if the tunnel's atomic state is not in @state. 1688 */ 1689 struct drm_dp_tunnel_state * 1690 drm_dp_tunnel_atomic_get_old_state(struct drm_atomic_commit *state, 1691 const struct drm_dp_tunnel *tunnel) 1692 { 1693 struct drm_dp_tunnel_group_state *old_group_state; 1694 int i; 1695 1696 for_each_old_group_in_state(state, old_group_state, i) 1697 if (to_group(old_group_state->base.obj) == tunnel->group) 1698 return get_tunnel_state(old_group_state, tunnel); 1699 1700 return NULL; 1701 } 1702 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_old_state); 1703 1704 /** 1705 * drm_dp_tunnel_atomic_get_new_state - get the new atomic state for a tunnel 1706 * @state: Atomic state 1707 * @tunnel: Tunnel to get the state for 1708 * 1709 * Get the new atomic state for @tunnel. 1710 * 1711 * Return the new state or NULL if the tunnel's atomic state is not in @state. 1712 */ 1713 struct drm_dp_tunnel_state * 1714 drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_commit *state, 1715 const struct drm_dp_tunnel *tunnel) 1716 { 1717 struct drm_dp_tunnel_group_state *new_group_state; 1718 int i; 1719 1720 for_each_new_group_in_state(state, new_group_state, i) 1721 if (to_group(new_group_state->base.obj) == tunnel->group) 1722 return get_tunnel_state(new_group_state, tunnel); 1723 1724 return NULL; 1725 } 1726 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state); 1727 1728 static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group) 1729 { 1730 group->mgr = mgr; 1731 group->available_bw = -1; 1732 INIT_LIST_HEAD(&group->tunnels); 1733 1734 drm_atomic_private_obj_init(mgr->dev, &group->base, 1735 &tunnel_group_funcs); 1736 1737 return true; 1738 } 1739 1740 static void cleanup_group(struct drm_dp_tunnel_group *group) 1741 { 1742 drm_atomic_private_obj_fini(&group->base); 1743 } 1744 1745 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG 1746 static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state) 1747 { 1748 const struct drm_dp_tunnel_state *tunnel_state; 1749 u32 stream_mask = 0; 1750 1751 for_each_tunnel_state(group_state, tunnel_state) { 1752 drm_WARN(to_group(group_state->base.obj)->mgr->dev, 1753 tunnel_state->stream_mask & stream_mask, 1754 "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n", 1755 tunnel_state->tunnel_ref.tunnel->name, 1756 tunnel_state->stream_mask, 1757 stream_mask); 1758 1759 stream_mask |= tunnel_state->stream_mask; 1760 } 1761 } 1762 #else 1763 static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state) 1764 { 1765 } 1766 #endif 1767 1768 static int stream_id_to_idx(u32 stream_mask, u8 stream_id) 1769 { 1770 return hweight32(stream_mask & (BIT(stream_id) - 1)); 1771 } 1772 1773 static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state, 1774 unsigned long old_mask, unsigned long new_mask) 1775 { 1776 unsigned long move_mask = old_mask & new_mask; 1777 int *new_bws = NULL; 1778 int id; 1779 1780 WARN_ON(!new_mask); 1781 1782 if (old_mask == new_mask) 1783 return 0; 1784 1785 new_bws = kzalloc_objs(*new_bws, hweight32(new_mask)); 1786 if (!new_bws) 1787 return -ENOMEM; 1788 1789 for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask)) 1790 new_bws[stream_id_to_idx(new_mask, id)] = 1791 tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)]; 1792 1793 kfree(tunnel_state->stream_bw); 1794 tunnel_state->stream_bw = new_bws; 1795 tunnel_state->stream_mask = new_mask; 1796 1797 return 0; 1798 } 1799 1800 static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state, 1801 u8 stream_id, int bw) 1802 { 1803 int err; 1804 1805 err = resize_bw_array(tunnel_state, 1806 tunnel_state->stream_mask, 1807 tunnel_state->stream_mask | BIT(stream_id)); 1808 if (err) 1809 return err; 1810 1811 tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw; 1812 1813 return 0; 1814 } 1815 1816 static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state, 1817 u8 stream_id) 1818 { 1819 if (!(tunnel_state->stream_mask & ~BIT(stream_id))) { 1820 free_tunnel_state(tunnel_state); 1821 return 0; 1822 } 1823 1824 return resize_bw_array(tunnel_state, 1825 tunnel_state->stream_mask, 1826 tunnel_state->stream_mask & ~BIT(stream_id)); 1827 } 1828 1829 /** 1830 * drm_dp_tunnel_atomic_set_stream_bw - Set the BW for a DP tunnel stream 1831 * @state: Atomic state 1832 * @tunnel: DP tunnel containing the stream 1833 * @stream_id: Stream ID 1834 * @bw: BW of the stream 1835 * 1836 * Set a DP tunnel stream's required BW in the atomic state. 1837 * 1838 * Returns 0 in case of success, a negative error code otherwise. 1839 */ 1840 int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_commit *state, 1841 struct drm_dp_tunnel *tunnel, 1842 u8 stream_id, int bw) 1843 { 1844 struct drm_dp_tunnel_group_state *new_group_state; 1845 struct drm_dp_tunnel_state *tunnel_state; 1846 int err; 1847 1848 if (drm_WARN_ON(tunnel->group->mgr->dev, 1849 stream_id > BITS_PER_TYPE(tunnel_state->stream_mask))) 1850 return -EINVAL; 1851 1852 tun_dbg(tunnel, 1853 "Setting %d Mb/s for stream %d\n", 1854 DPTUN_BW_ARG(bw), stream_id); 1855 1856 new_group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel); 1857 if (IS_ERR(new_group_state)) 1858 return PTR_ERR(new_group_state); 1859 1860 if (bw == 0) { 1861 tunnel_state = get_tunnel_state(new_group_state, tunnel); 1862 if (!tunnel_state) 1863 return 0; 1864 1865 return clear_stream_bw(tunnel_state, stream_id); 1866 } 1867 1868 tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel); 1869 if (drm_WARN_ON(state->dev, !tunnel_state)) 1870 return -EINVAL; 1871 1872 err = set_stream_bw(tunnel_state, stream_id, bw); 1873 if (err) 1874 return err; 1875 1876 check_unique_stream_ids(new_group_state); 1877 1878 return 0; 1879 } 1880 EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw); 1881 1882 /** 1883 * drm_dp_tunnel_atomic_get_required_bw - Get the BW required by a DP tunnel 1884 * @tunnel_state: Atomic state of the queried tunnel 1885 * 1886 * Calculate the BW required by a tunnel adding up the required BW of all 1887 * the streams in the tunnel. 1888 * 1889 * Return the total BW required by the tunnel. 1890 */ 1891 int drm_dp_tunnel_atomic_get_required_bw(const struct drm_dp_tunnel_state *tunnel_state) 1892 { 1893 int tunnel_bw = 0; 1894 int i; 1895 1896 if (!tunnel_state || !tunnel_state->stream_mask) 1897 return 0; 1898 1899 for (i = 0; i < hweight32(tunnel_state->stream_mask); i++) 1900 tunnel_bw += tunnel_state->stream_bw[i]; 1901 1902 return tunnel_bw; 1903 } 1904 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_required_bw); 1905 1906 /** 1907 * drm_dp_tunnel_atomic_get_group_streams_in_state - Get mask of stream IDs in a group 1908 * @state: Atomic state 1909 * @tunnel: Tunnel object 1910 * @stream_mask: Mask of streams in @tunnel's group 1911 * 1912 * Get the mask of all the stream IDs in the tunnel group of @tunnel. 1913 * 1914 * Return 0 in case of success - with the stream IDs in @stream_mask - or a 1915 * negative error code in case of failure. 1916 */ 1917 int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_commit *state, 1918 const struct drm_dp_tunnel *tunnel, 1919 u32 *stream_mask) 1920 { 1921 struct drm_dp_tunnel_group_state *group_state; 1922 struct drm_dp_tunnel_state *tunnel_state; 1923 1924 group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel); 1925 if (IS_ERR(group_state)) 1926 return PTR_ERR(group_state); 1927 1928 *stream_mask = 0; 1929 for_each_tunnel_state(group_state, tunnel_state) 1930 *stream_mask |= tunnel_state->stream_mask; 1931 1932 return 0; 1933 } 1934 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state); 1935 1936 static int 1937 drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state, 1938 u32 *failed_stream_mask) 1939 { 1940 struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj); 1941 struct drm_dp_tunnel_state *new_tunnel_state; 1942 u32 group_stream_mask = 0; 1943 int group_bw = 0; 1944 1945 for_each_tunnel_state(new_group_state, new_tunnel_state) { 1946 struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel; 1947 int max_dprx_bw = get_max_dprx_bw(tunnel); 1948 int tunnel_bw = drm_dp_tunnel_atomic_get_required_bw(new_tunnel_state); 1949 1950 tun_dbg(tunnel, 1951 "%sRequired %d/%d Mb/s total for tunnel.\n", 1952 tunnel_bw > max_dprx_bw ? "Not enough BW: " : "", 1953 DPTUN_BW_ARG(tunnel_bw), 1954 DPTUN_BW_ARG(max_dprx_bw)); 1955 1956 if (tunnel_bw > max_dprx_bw) { 1957 *failed_stream_mask = new_tunnel_state->stream_mask; 1958 return -ENOSPC; 1959 } 1960 1961 group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity), 1962 max_dprx_bw); 1963 group_stream_mask |= new_tunnel_state->stream_mask; 1964 } 1965 1966 tun_grp_dbg(group, 1967 "%sRequired %d/%d Mb/s total for tunnel group.\n", 1968 group_bw > group->available_bw ? "Not enough BW: " : "", 1969 DPTUN_BW_ARG(group_bw), 1970 DPTUN_BW_ARG(group->available_bw)); 1971 1972 if (group_bw > group->available_bw) { 1973 *failed_stream_mask = group_stream_mask; 1974 return -ENOSPC; 1975 } 1976 1977 return 0; 1978 } 1979 1980 /** 1981 * drm_dp_tunnel_atomic_check_stream_bws - Check BW limit for all streams in state 1982 * @state: Atomic state 1983 * @failed_stream_mask: Mask of stream IDs with a BW limit failure 1984 * 1985 * Check the required BW of each DP tunnel in @state against both the DPRX BW 1986 * limit of the tunnel and the BW limit of the tunnel group. Return a mask of 1987 * stream IDs in @failed_stream_mask once a check fails. The mask will contain 1988 * either all the streams in a tunnel (in case a DPRX BW limit check failed) or 1989 * all the streams in a tunnel group (in case a group BW limit check failed). 1990 * 1991 * Return 0 if all the BW limit checks passed, -ENOSPC in case a BW limit 1992 * check failed - with @failed_stream_mask containing the streams failing the 1993 * check - or a negative error code otherwise. 1994 */ 1995 int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_commit *state, 1996 u32 *failed_stream_mask) 1997 { 1998 struct drm_dp_tunnel_group_state *new_group_state; 1999 int i; 2000 2001 for_each_new_group_in_state(state, new_group_state, i) { 2002 int ret; 2003 2004 ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state, 2005 failed_stream_mask); 2006 if (ret) 2007 return ret; 2008 } 2009 2010 return 0; 2011 } 2012 EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws); 2013 2014 static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr) 2015 { 2016 int i; 2017 2018 for (i = 0; i < mgr->group_count; i++) { 2019 cleanup_group(&mgr->groups[i]); 2020 drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels)); 2021 } 2022 2023 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG 2024 ref_tracker_dir_exit(&mgr->ref_tracker); 2025 #endif 2026 2027 kfree(mgr->groups); 2028 kfree(mgr); 2029 } 2030 2031 /** 2032 * drm_dp_tunnel_mgr_create - Create a DP tunnel manager 2033 * @dev: DRM device object 2034 * @max_group_count: Maximum number of tunnel groups 2035 * 2036 * Creates a DP tunnel manager for @dev. 2037 * 2038 * Returns a pointer to the tunnel manager if created successfully or error 2039 * pointer in case of failure. 2040 */ 2041 struct drm_dp_tunnel_mgr * 2042 drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count) 2043 { 2044 struct drm_dp_tunnel_mgr *mgr; 2045 int i; 2046 2047 mgr = kzalloc_obj(*mgr); 2048 if (!mgr) 2049 return ERR_PTR(-ENOMEM); 2050 2051 mgr->dev = dev; 2052 init_waitqueue_head(&mgr->bw_req_queue); 2053 2054 mgr->groups = kzalloc_objs(*mgr->groups, max_group_count); 2055 if (!mgr->groups) { 2056 kfree(mgr); 2057 2058 return ERR_PTR(-ENOMEM); 2059 } 2060 2061 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG 2062 ref_tracker_dir_init(&mgr->ref_tracker, 16, "drm_dptun"); 2063 #endif 2064 2065 for (i = 0; i < max_group_count; i++) { 2066 if (!init_group(mgr, &mgr->groups[i])) { 2067 destroy_mgr(mgr); 2068 2069 return ERR_PTR(-ENOMEM); 2070 } 2071 2072 mgr->group_count++; 2073 } 2074 2075 return mgr; 2076 } 2077 EXPORT_SYMBOL(drm_dp_tunnel_mgr_create); 2078 2079 /** 2080 * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager 2081 * @mgr: Tunnel manager object 2082 * 2083 * Destroy the tunnel manager. 2084 */ 2085 void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr) 2086 { 2087 destroy_mgr(mgr); 2088 } 2089 EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy); 2090