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