1 // SPDX-License-Identifier: MIT
3 * Copyright © 2023 Intel Corporation
6 #include <linux/ref_tracker.h>
7 #include <linux/types.h>
9 #include <drm/drm_atomic_state_helper.h>
11 #include <drm/drm_atomic.h>
12 #include <drm/drm_print.h>
13 #include <drm/display/drm_dp.h>
14 #include <drm/display/drm_dp_helper.h>
15 #include <drm/display/drm_dp_tunnel.h>
17 #define to_group(__private_obj) \
18 container_of(__private_obj, struct drm_dp_tunnel_group, base)
20 #define to_group_state(__private_state) \
21 container_of(__private_state, struct drm_dp_tunnel_group_state, base)
23 #define is_dp_tunnel_private_obj(__obj) \
24 ((__obj)->funcs == &tunnel_group_funcs)
26 #define for_each_new_group_in_state(__state, __new_group_state, __i) \
28 (__i) < (__state)->num_private_objs; \
30 for_each_if ((__state)->private_objs[__i].ptr && \
31 is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
32 ((__new_group_state) = \
33 to_group_state((__state)->private_objs[__i].new_state), 1))
35 #define for_each_old_group_in_state(__state, __old_group_state, __i) \
37 (__i) < (__state)->num_private_objs; \
39 for_each_if ((__state)->private_objs[__i].ptr && \
40 is_dp_tunnel_private_obj((__state)->private_objs[__i].ptr) && \
41 ((__old_group_state) = \
42 to_group_state((__state)->private_objs[__i].old_state), 1))
44 #define for_each_tunnel_in_group(__group, __tunnel) \
45 list_for_each_entry(__tunnel, &(__group)->tunnels, node)
47 #define for_each_tunnel_state(__group_state, __tunnel_state) \
48 list_for_each_entry(__tunnel_state, &(__group_state)->tunnel_states, node)
50 #define for_each_tunnel_state_safe(__group_state, __tunnel_state, __tunnel_state_tmp) \
51 list_for_each_entry_safe(__tunnel_state, __tunnel_state_tmp, \
52 &(__group_state)->tunnel_states, node)
54 #define kbytes_to_mbits(__kbytes) \
55 DIV_ROUND_UP((__kbytes) * 8, 1000)
57 #define DPTUN_BW_ARG(__bw) ((__bw) < 0 ? (__bw) : kbytes_to_mbits(__bw))
59 #define __tun_prn(__tunnel, __level, __type, __fmt, ...) \
60 drm_##__level##__type((__tunnel)->group->mgr->dev, \
61 "[DPTUN %s][%s] " __fmt, \
62 drm_dp_tunnel_name(__tunnel), \
63 (__tunnel)->aux->name, ## \
66 #define tun_dbg(__tunnel, __fmt, ...) \
67 __tun_prn(__tunnel, dbg, _kms, __fmt, ## __VA_ARGS__)
69 #define tun_dbg_stat(__tunnel, __err, __fmt, ...) do { \
71 __tun_prn(__tunnel, dbg, _kms, __fmt " (Failed, err: %pe)\n", \
72 ## __VA_ARGS__, ERR_PTR(__err)); \
74 __tun_prn(__tunnel, dbg, _kms, __fmt " (Ok)\n", \
78 #define tun_dbg_atomic(__tunnel, __fmt, ...) \
79 __tun_prn(__tunnel, dbg, _atomic, __fmt, ## __VA_ARGS__)
81 #define tun_grp_dbg(__group, __fmt, ...) \
82 drm_dbg_kms((__group)->mgr->dev, \
83 "[DPTUN %s] " __fmt, \
84 drm_dp_tunnel_group_name(__group), ## \
87 #define DP_TUNNELING_BASE DP_TUNNELING_OUI
89 #define __DPTUN_REG_RANGE(__start, __size) \
90 GENMASK_ULL((__start) + (__size) - 1, (__start))
92 #define DPTUN_REG_RANGE(__addr, __size) \
93 __DPTUN_REG_RANGE((__addr) - DP_TUNNELING_BASE, (__size))
95 #define DPTUN_REG(__addr) DPTUN_REG_RANGE(__addr, 1)
97 #define DPTUN_INFO_REG_MASK ( \
98 DPTUN_REG_RANGE(DP_TUNNELING_OUI, DP_TUNNELING_OUI_BYTES) | \
99 DPTUN_REG_RANGE(DP_TUNNELING_DEV_ID, DP_TUNNELING_DEV_ID_BYTES) | \
100 DPTUN_REG(DP_TUNNELING_HW_REV) | \
101 DPTUN_REG(DP_TUNNELING_SW_REV_MAJOR) | \
102 DPTUN_REG(DP_TUNNELING_SW_REV_MINOR) | \
103 DPTUN_REG(DP_TUNNELING_CAPABILITIES) | \
104 DPTUN_REG(DP_IN_ADAPTER_INFO) | \
105 DPTUN_REG(DP_USB4_DRIVER_ID) | \
106 DPTUN_REG(DP_USB4_DRIVER_BW_CAPABILITY) | \
107 DPTUN_REG(DP_IN_ADAPTER_TUNNEL_INFORMATION) | \
108 DPTUN_REG(DP_BW_GRANULARITY) | \
109 DPTUN_REG(DP_ESTIMATED_BW) | \
110 DPTUN_REG(DP_ALLOCATED_BW) | \
111 DPTUN_REG(DP_TUNNELING_MAX_LINK_RATE) | \
112 DPTUN_REG(DP_TUNNELING_MAX_LANE_COUNT) | \
113 DPTUN_REG(DP_DPTX_BW_ALLOCATION_MODE_CONTROL))
115 static const DECLARE_BITMAP(dptun_info_regs, 64) = {
116 DPTUN_INFO_REG_MASK & -1UL,
117 #if BITS_PER_LONG == 32
118 DPTUN_INFO_REG_MASK >> 32,
122 struct drm_dp_tunnel_regs {
123 u8 buf[HWEIGHT64(DPTUN_INFO_REG_MASK)];
126 struct drm_dp_tunnel_group;
128 struct drm_dp_tunnel {
129 struct drm_dp_tunnel_group *group;
131 struct list_head node;
134 struct ref_tracker *tracker;
135 struct drm_dp_aux *aux;
143 u8 max_dprx_lane_count;
147 bool bw_alloc_supported:1;
148 bool bw_alloc_enabled:1;
153 struct drm_dp_tunnel_group_state;
155 struct drm_dp_tunnel_state {
156 struct drm_dp_tunnel_group_state *group_state;
158 struct drm_dp_tunnel_ref tunnel_ref;
160 struct list_head node;
166 struct drm_dp_tunnel_group_state {
167 struct drm_private_state base;
169 struct list_head tunnel_states;
172 struct drm_dp_tunnel_group {
173 struct drm_private_obj base;
174 struct drm_dp_tunnel_mgr *mgr;
176 struct list_head tunnels;
178 /* available BW including the allocated_bw of all tunnels in the group */
187 struct drm_dp_tunnel_mgr {
188 struct drm_device *dev;
191 struct drm_dp_tunnel_group *groups;
192 wait_queue_head_t bw_req_queue;
194 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
195 struct ref_tracker_dir ref_tracker;
200 * The following helpers provide a way to read out the tunneling DPCD
201 * registers with a minimal amount of AUX transfers (1 transfer per contiguous
202 * range, as permitted by the 16 byte per transfer AUX limit), not accessing
203 * other registers to avoid any read side-effects.
205 static int next_reg_area(int *offset)
207 *offset = find_next_bit(dptun_info_regs, 64, *offset);
209 return find_next_zero_bit(dptun_info_regs, 64, *offset + 1) - *offset;
212 #define tunnel_reg_ptr(__regs, __address) ({ \
213 WARN_ON(!test_bit((__address) - DP_TUNNELING_BASE, dptun_info_regs)); \
214 &(__regs)->buf[bitmap_weight(dptun_info_regs, (__address) - DP_TUNNELING_BASE)]; \
217 static int read_tunnel_regs(struct drm_dp_aux *aux, struct drm_dp_tunnel_regs *regs)
222 while ((len = next_reg_area(&offset))) {
223 int address = DP_TUNNELING_BASE + offset;
225 if (drm_dp_dpcd_read(aux, address, tunnel_reg_ptr(regs, address), len) < 0)
234 static u8 tunnel_reg(const struct drm_dp_tunnel_regs *regs, int address)
236 return *tunnel_reg_ptr(regs, address);
239 static u8 tunnel_reg_drv_group_id(const struct drm_dp_tunnel_regs *regs)
241 u8 drv_id = tunnel_reg(regs, DP_USB4_DRIVER_ID) & DP_USB4_DRIVER_ID_MASK;
242 u8 group_id = tunnel_reg(regs, DP_IN_ADAPTER_TUNNEL_INFORMATION) & DP_GROUP_ID_MASK;
247 return (drv_id << DP_GROUP_ID_BITS) | group_id;
250 /* Return granularity in kB/s units */
251 static int tunnel_reg_bw_granularity(const struct drm_dp_tunnel_regs *regs)
253 int gr = tunnel_reg(regs, DP_BW_GRANULARITY) & DP_BW_GRANULARITY_MASK;
258 return (250000 << gr) / 8;
261 static int tunnel_reg_max_dprx_rate(const struct drm_dp_tunnel_regs *regs)
263 u8 bw_code = tunnel_reg(regs, DP_TUNNELING_MAX_LINK_RATE);
265 return drm_dp_bw_code_to_link_rate(bw_code);
268 static int tunnel_reg_max_dprx_lane_count(const struct drm_dp_tunnel_regs *regs)
270 return tunnel_reg(regs, DP_TUNNELING_MAX_LANE_COUNT) &
271 DP_TUNNELING_MAX_LANE_COUNT_MASK;
274 static bool tunnel_reg_bw_alloc_supported(const struct drm_dp_tunnel_regs *regs)
276 u8 cap_mask = DP_TUNNELING_SUPPORT | DP_IN_BW_ALLOCATION_MODE_SUPPORT;
278 if ((tunnel_reg(regs, DP_TUNNELING_CAPABILITIES) & cap_mask) != cap_mask)
281 return tunnel_reg(regs, DP_USB4_DRIVER_BW_CAPABILITY) &
282 DP_USB4_DRIVER_BW_ALLOCATION_MODE_SUPPORT;
285 static bool tunnel_reg_bw_alloc_enabled(const struct drm_dp_tunnel_regs *regs)
287 return tunnel_reg(regs, DP_DPTX_BW_ALLOCATION_MODE_CONTROL) &
288 DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE;
291 static u8 tunnel_group_drv_id(u8 drv_group_id)
293 return drv_group_id >> DP_GROUP_ID_BITS;
296 static u8 tunnel_group_id(u8 drv_group_id)
298 return drv_group_id & DP_GROUP_ID_MASK;
301 const char *drm_dp_tunnel_name(const struct drm_dp_tunnel *tunnel)
305 EXPORT_SYMBOL(drm_dp_tunnel_name);
307 static const char *drm_dp_tunnel_group_name(const struct drm_dp_tunnel_group *group)
312 static struct drm_dp_tunnel_group *
313 lookup_or_alloc_group(struct drm_dp_tunnel_mgr *mgr, u8 drv_group_id)
315 struct drm_dp_tunnel_group *group = NULL;
318 for (i = 0; i < mgr->group_count; i++) {
320 * A tunnel group with 0 group ID shouldn't have more than one
323 if (tunnel_group_id(drv_group_id) &&
324 mgr->groups[i].drv_group_id == drv_group_id)
325 return &mgr->groups[i];
327 if (!group && !mgr->groups[i].active)
328 group = &mgr->groups[i];
332 drm_dbg_kms(mgr->dev,
333 "DPTUN: Can't allocate more tunnel groups\n");
337 group->drv_group_id = drv_group_id;
338 group->active = true;
341 * The group name format here and elsewhere: Driver-ID:Group-ID:*
342 * (* standing for all DP-Adapters/tunnels in the group).
344 snprintf(group->name, sizeof(group->name), "%d:%d:*",
345 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
346 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1));
351 static void free_group(struct drm_dp_tunnel_group *group)
353 struct drm_dp_tunnel_mgr *mgr = group->mgr;
355 if (drm_WARN_ON(mgr->dev, !list_empty(&group->tunnels)))
358 group->drv_group_id = 0;
359 group->available_bw = -1;
360 group->active = false;
363 static struct drm_dp_tunnel *
364 tunnel_get(struct drm_dp_tunnel *tunnel)
366 kref_get(&tunnel->kref);
371 static void free_tunnel(struct kref *kref)
373 struct drm_dp_tunnel *tunnel = container_of(kref, typeof(*tunnel), kref);
374 struct drm_dp_tunnel_group *group = tunnel->group;
376 list_del(&tunnel->node);
377 if (list_empty(&group->tunnels))
383 static void tunnel_put(struct drm_dp_tunnel *tunnel)
385 kref_put(&tunnel->kref, free_tunnel);
388 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
389 static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
390 struct ref_tracker **tracker)
392 ref_tracker_alloc(&tunnel->group->mgr->ref_tracker,
393 tracker, GFP_KERNEL);
396 static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
397 struct ref_tracker **tracker)
399 ref_tracker_free(&tunnel->group->mgr->ref_tracker,
403 static void track_tunnel_ref(struct drm_dp_tunnel *tunnel,
404 struct ref_tracker **tracker)
408 static void untrack_tunnel_ref(struct drm_dp_tunnel *tunnel,
409 struct ref_tracker **tracker)
415 * drm_dp_tunnel_get - Get a reference for a DP tunnel
416 * @tunnel: Tunnel object
417 * @tracker: Debug tracker for the reference
419 * Get a reference for @tunnel, along with a debug tracker to help locating
420 * the source of a reference leak/double reference put etc. issue.
422 * The reference must be dropped after use calling drm_dp_tunnel_put()
423 * passing @tunnel and *@tracker returned from here.
425 * Returns @tunnel - as a convenience - along with *@tracker.
427 struct drm_dp_tunnel *
428 drm_dp_tunnel_get(struct drm_dp_tunnel *tunnel,
429 struct ref_tracker **tracker)
431 track_tunnel_ref(tunnel, tracker);
433 return tunnel_get(tunnel);
435 EXPORT_SYMBOL(drm_dp_tunnel_get);
438 * drm_dp_tunnel_put - Put a reference for a DP tunnel
439 * @tunnel: Tunnel object
440 * @tracker: Debug tracker for the reference
442 * Put a reference for @tunnel along with its debug *@tracker, which
443 * was obtained with drm_dp_tunnel_get().
445 void drm_dp_tunnel_put(struct drm_dp_tunnel *tunnel,
446 struct ref_tracker **tracker)
448 untrack_tunnel_ref(tunnel, tracker);
452 EXPORT_SYMBOL(drm_dp_tunnel_put);
454 static bool add_tunnel_to_group(struct drm_dp_tunnel_mgr *mgr,
456 struct drm_dp_tunnel *tunnel)
458 struct drm_dp_tunnel_group *group;
460 group = lookup_or_alloc_group(mgr, drv_group_id);
464 tunnel->group = group;
465 list_add(&tunnel->node, &group->tunnels);
470 static struct drm_dp_tunnel *
471 create_tunnel(struct drm_dp_tunnel_mgr *mgr,
472 struct drm_dp_aux *aux,
473 const struct drm_dp_tunnel_regs *regs)
475 u8 drv_group_id = tunnel_reg_drv_group_id(regs);
476 struct drm_dp_tunnel *tunnel;
478 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
482 INIT_LIST_HEAD(&tunnel->node);
484 kref_init(&tunnel->kref);
488 tunnel->adapter_id = tunnel_reg(regs, DP_IN_ADAPTER_INFO) & DP_IN_ADAPTER_NUMBER_MASK;
490 snprintf(tunnel->name, sizeof(tunnel->name), "%d:%d:%d",
491 tunnel_group_drv_id(drv_group_id) & ((1 << DP_GROUP_ID_BITS) - 1),
492 tunnel_group_id(drv_group_id) & ((1 << DP_USB4_DRIVER_ID_BITS) - 1),
493 tunnel->adapter_id & ((1 << DP_IN_ADAPTER_NUMBER_BITS) - 1));
495 tunnel->bw_granularity = tunnel_reg_bw_granularity(regs);
496 tunnel->allocated_bw = tunnel_reg(regs, DP_ALLOCATED_BW) *
497 tunnel->bw_granularity;
499 * An initial allocated BW of 0 indicates an undefined state: the
500 * actual allocation is determined by the TBT CM, usually following a
501 * legacy allocation policy (based on the max DPRX caps). From the
502 * driver's POV the state becomes defined only after the first
503 * allocation request.
505 if (!tunnel->allocated_bw)
506 tunnel->allocated_bw = -1;
508 tunnel->bw_alloc_supported = tunnel_reg_bw_alloc_supported(regs);
509 tunnel->bw_alloc_enabled = tunnel_reg_bw_alloc_enabled(regs);
511 if (!add_tunnel_to_group(mgr, drv_group_id, tunnel)) {
517 track_tunnel_ref(tunnel, &tunnel->tracker);
522 static void destroy_tunnel(struct drm_dp_tunnel *tunnel)
524 untrack_tunnel_ref(tunnel, &tunnel->tracker);
529 * drm_dp_tunnel_set_io_error - Set the IO error flag for a DP tunnel
530 * @tunnel: Tunnel object
532 * Set the IO error flag for @tunnel. Drivers can call this function upon
533 * detecting a failure that affects the tunnel functionality, for instance
534 * after a DP AUX transfer failure on the port @tunnel is connected to.
536 * This disables further management of @tunnel, including any related
537 * AUX accesses for tunneling DPCD registers, returning error to the
538 * initiators of these. The driver is supposed to drop this tunnel and -
539 * optionally - recreate it.
541 void drm_dp_tunnel_set_io_error(struct drm_dp_tunnel *tunnel)
543 tunnel->has_io_error = true;
545 EXPORT_SYMBOL(drm_dp_tunnel_set_io_error);
547 #define SKIP_DPRX_CAPS_CHECK BIT(0)
548 #define ALLOW_ALLOCATED_BW_CHANGE BIT(1)
549 static bool tunnel_regs_are_valid(struct drm_dp_tunnel_mgr *mgr,
550 const struct drm_dp_tunnel_regs *regs,
553 u8 drv_group_id = tunnel_reg_drv_group_id(regs);
554 bool check_dprx = !(flags & SKIP_DPRX_CAPS_CHECK);
557 if (!tunnel_reg_bw_alloc_supported(regs)) {
558 if (tunnel_group_id(drv_group_id)) {
559 drm_dbg_kms(mgr->dev,
560 "DPTUN: A non-zero group ID is only allowed with BWA support\n");
564 if (tunnel_reg(regs, DP_ALLOCATED_BW)) {
565 drm_dbg_kms(mgr->dev,
566 "DPTUN: BW is allocated without BWA support\n");
573 if (!tunnel_group_id(drv_group_id)) {
574 drm_dbg_kms(mgr->dev,
575 "DPTUN: BWA support requires a non-zero group ID\n");
579 if (check_dprx && hweight8(tunnel_reg_max_dprx_lane_count(regs)) != 1) {
580 drm_dbg_kms(mgr->dev,
581 "DPTUN: Invalid DPRX lane count: %d\n",
582 tunnel_reg_max_dprx_lane_count(regs));
587 if (check_dprx && !tunnel_reg_max_dprx_rate(regs)) {
588 drm_dbg_kms(mgr->dev,
589 "DPTUN: DPRX rate is 0\n");
594 if (tunnel_reg_bw_granularity(regs) < 0) {
595 drm_dbg_kms(mgr->dev,
596 "DPTUN: Invalid BW granularity\n");
601 if (tunnel_reg(regs, DP_ALLOCATED_BW) > tunnel_reg(regs, DP_ESTIMATED_BW)) {
602 drm_dbg_kms(mgr->dev,
603 "DPTUN: Allocated BW %d > estimated BW %d Mb/s\n",
604 DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) *
605 tunnel_reg_bw_granularity(regs)),
606 DPTUN_BW_ARG(tunnel_reg(regs, DP_ESTIMATED_BW) *
607 tunnel_reg_bw_granularity(regs)));
615 static int tunnel_allocated_bw(const struct drm_dp_tunnel *tunnel)
617 return max(tunnel->allocated_bw, 0);
620 static bool tunnel_info_changes_are_valid(struct drm_dp_tunnel *tunnel,
621 const struct drm_dp_tunnel_regs *regs,
624 u8 new_drv_group_id = tunnel_reg_drv_group_id(regs);
627 if (tunnel->bw_alloc_supported != tunnel_reg_bw_alloc_supported(regs)) {
629 "BW alloc support has changed %s -> %s\n",
630 str_yes_no(tunnel->bw_alloc_supported),
631 str_yes_no(tunnel_reg_bw_alloc_supported(regs)));
636 if (tunnel->group->drv_group_id != new_drv_group_id) {
638 "Driver/group ID has changed %d:%d:* -> %d:%d:*\n",
639 tunnel_group_drv_id(tunnel->group->drv_group_id),
640 tunnel_group_id(tunnel->group->drv_group_id),
641 tunnel_group_drv_id(new_drv_group_id),
642 tunnel_group_id(new_drv_group_id));
647 if (!tunnel->bw_alloc_supported)
650 if (tunnel->bw_granularity != tunnel_reg_bw_granularity(regs)) {
652 "BW granularity has changed: %d -> %d Mb/s\n",
653 DPTUN_BW_ARG(tunnel->bw_granularity),
654 DPTUN_BW_ARG(tunnel_reg_bw_granularity(regs)));
660 * On some devices at least the BW alloc mode enabled status is always
661 * reported as 0, so skip checking that here.
664 if (!(flags & ALLOW_ALLOCATED_BW_CHANGE) &&
665 tunnel_allocated_bw(tunnel) !=
666 tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity) {
668 "Allocated BW has changed: %d -> %d Mb/s\n",
669 DPTUN_BW_ARG(tunnel->allocated_bw),
670 DPTUN_BW_ARG(tunnel_reg(regs, DP_ALLOCATED_BW) * tunnel->bw_granularity));
679 read_and_verify_tunnel_regs(struct drm_dp_tunnel *tunnel,
680 struct drm_dp_tunnel_regs *regs,
685 err = read_tunnel_regs(tunnel->aux, regs);
687 drm_dp_tunnel_set_io_error(tunnel);
692 if (!tunnel_regs_are_valid(tunnel->group->mgr, regs, flags))
695 if (!tunnel_info_changes_are_valid(tunnel, regs, flags))
701 static bool update_dprx_caps(struct drm_dp_tunnel *tunnel, const struct drm_dp_tunnel_regs *regs)
703 bool changed = false;
705 if (tunnel_reg_max_dprx_rate(regs) != tunnel->max_dprx_rate) {
706 tunnel->max_dprx_rate = tunnel_reg_max_dprx_rate(regs);
710 if (tunnel_reg_max_dprx_lane_count(regs) != tunnel->max_dprx_lane_count) {
711 tunnel->max_dprx_lane_count = tunnel_reg_max_dprx_lane_count(regs);
718 static int dev_id_len(const u8 *dev_id, int max_len)
720 while (max_len && dev_id[max_len - 1] == '\0')
726 static int get_max_dprx_bw(const struct drm_dp_tunnel *tunnel)
728 int max_dprx_bw = drm_dp_max_dprx_data_rate(tunnel->max_dprx_rate,
729 tunnel->max_dprx_lane_count);
732 * A BW request of roundup(max_dprx_bw, tunnel->bw_granularity) results in
733 * an allocation of max_dprx_bw. A BW request above this rounded-up
736 return min(roundup(max_dprx_bw, tunnel->bw_granularity),
737 MAX_DP_REQUEST_BW * tunnel->bw_granularity);
740 static int get_max_tunnel_bw(const struct drm_dp_tunnel *tunnel)
742 return min(get_max_dprx_bw(tunnel), tunnel->group->available_bw);
746 * drm_dp_tunnel_detect - Detect DP tunnel on the link
747 * @mgr: Tunnel manager
748 * @aux: DP AUX on which the tunnel will be detected
750 * Detect if there is any DP tunnel on the link and add it to the tunnel
751 * group's tunnel list.
753 * Returns a pointer to a tunnel on success, or an ERR_PTR() error on
756 struct drm_dp_tunnel *
757 drm_dp_tunnel_detect(struct drm_dp_tunnel_mgr *mgr,
758 struct drm_dp_aux *aux)
760 struct drm_dp_tunnel_regs regs;
761 struct drm_dp_tunnel *tunnel;
764 err = read_tunnel_regs(aux, ®s);
768 if (!(tunnel_reg(®s, DP_TUNNELING_CAPABILITIES) &
769 DP_TUNNELING_SUPPORT))
770 return ERR_PTR(-ENODEV);
772 /* The DPRX caps are valid only after enabling BW alloc mode. */
773 if (!tunnel_regs_are_valid(mgr, ®s, SKIP_DPRX_CAPS_CHECK))
774 return ERR_PTR(-EINVAL);
776 tunnel = create_tunnel(mgr, aux, ®s);
778 return ERR_PTR(-ENOMEM);
781 "OUI:%*phD DevID:%*pE Rev-HW:%d.%d SW:%d.%d PR-Sup:%s BWA-Sup:%s BWA-En:%s\n",
782 DP_TUNNELING_OUI_BYTES,
783 tunnel_reg_ptr(®s, DP_TUNNELING_OUI),
784 dev_id_len(tunnel_reg_ptr(®s, DP_TUNNELING_DEV_ID), DP_TUNNELING_DEV_ID_BYTES),
785 tunnel_reg_ptr(®s, DP_TUNNELING_DEV_ID),
786 (tunnel_reg(®s, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MAJOR_MASK) >>
787 DP_TUNNELING_HW_REV_MAJOR_SHIFT,
788 (tunnel_reg(®s, DP_TUNNELING_HW_REV) & DP_TUNNELING_HW_REV_MINOR_MASK) >>
789 DP_TUNNELING_HW_REV_MINOR_SHIFT,
790 tunnel_reg(®s, DP_TUNNELING_SW_REV_MAJOR),
791 tunnel_reg(®s, DP_TUNNELING_SW_REV_MINOR),
792 str_yes_no(tunnel_reg(®s, DP_TUNNELING_CAPABILITIES) &
793 DP_PANEL_REPLAY_OPTIMIZATION_SUPPORT),
794 str_yes_no(tunnel->bw_alloc_supported),
795 str_yes_no(tunnel->bw_alloc_enabled));
799 EXPORT_SYMBOL(drm_dp_tunnel_detect);
802 * drm_dp_tunnel_destroy - Destroy tunnel object
803 * @tunnel: Tunnel object
805 * Remove the tunnel from the tunnel topology and destroy it.
807 * Returns 0 on success, -ENODEV if the tunnel has been destroyed already.
809 int drm_dp_tunnel_destroy(struct drm_dp_tunnel *tunnel)
814 if (drm_WARN_ON(tunnel->group->mgr->dev, tunnel->destroyed))
817 tun_dbg(tunnel, "destroying\n");
819 tunnel->destroyed = true;
820 destroy_tunnel(tunnel);
824 EXPORT_SYMBOL(drm_dp_tunnel_destroy);
826 static int check_tunnel(const struct drm_dp_tunnel *tunnel)
828 if (tunnel->destroyed)
831 if (tunnel->has_io_error)
837 static int group_allocated_bw(struct drm_dp_tunnel_group *group)
839 struct drm_dp_tunnel *tunnel;
840 int group_allocated_bw = 0;
842 for_each_tunnel_in_group(group, tunnel) {
843 if (check_tunnel(tunnel) == 0 &&
844 tunnel->bw_alloc_enabled)
845 group_allocated_bw += tunnel_allocated_bw(tunnel);
848 return group_allocated_bw;
852 * The estimated BW reported by the TBT Connection Manager for each tunnel in
853 * a group includes the BW already allocated for the given tunnel and the
854 * unallocated BW which is free to be used by any tunnel in the group.
856 static int group_free_bw(const struct drm_dp_tunnel *tunnel)
858 return tunnel->estimated_bw - tunnel_allocated_bw(tunnel);
861 static int calc_group_available_bw(const struct drm_dp_tunnel *tunnel)
863 return group_allocated_bw(tunnel->group) +
864 group_free_bw(tunnel);
867 static int update_group_available_bw(struct drm_dp_tunnel *tunnel,
868 const struct drm_dp_tunnel_regs *regs)
870 struct drm_dp_tunnel *tunnel_iter;
871 int group_available_bw;
874 tunnel->estimated_bw = tunnel_reg(regs, DP_ESTIMATED_BW) * tunnel->bw_granularity;
876 if (calc_group_available_bw(tunnel) == tunnel->group->available_bw)
879 for_each_tunnel_in_group(tunnel->group, tunnel_iter) {
882 if (tunnel_iter == tunnel)
885 if (check_tunnel(tunnel_iter) != 0 ||
886 !tunnel_iter->bw_alloc_enabled)
889 err = drm_dp_dpcd_probe(tunnel_iter->aux, DP_DPCD_REV);
892 "Probe failed, assume disconnected (err %pe)\n",
894 drm_dp_tunnel_set_io_error(tunnel_iter);
898 group_available_bw = calc_group_available_bw(tunnel);
900 tun_dbg(tunnel, "Updated group available BW: %d->%d\n",
901 DPTUN_BW_ARG(tunnel->group->available_bw),
902 DPTUN_BW_ARG(group_available_bw));
904 changed = tunnel->group->available_bw != group_available_bw;
906 tunnel->group->available_bw = group_available_bw;
908 return changed ? 1 : 0;
911 static int set_bw_alloc_mode(struct drm_dp_tunnel *tunnel, bool enable)
913 u8 mask = DP_DISPLAY_DRIVER_BW_ALLOCATION_MODE_ENABLE | DP_UNMASK_BW_ALLOCATION_IRQ;
916 if (drm_dp_dpcd_readb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, &val) < 0)
924 if (drm_dp_dpcd_writeb(tunnel->aux, DP_DPTX_BW_ALLOCATION_MODE_CONTROL, val) < 0)
927 tunnel->bw_alloc_enabled = enable;
932 drm_dp_tunnel_set_io_error(tunnel);
938 * drm_dp_tunnel_enable_bw_alloc - Enable DP tunnel BW allocation mode
939 * @tunnel: Tunnel object
941 * Enable the DP tunnel BW allocation mode on @tunnel if it supports it.
943 * Returns 0 in case of success, negative error code otherwise.
945 int drm_dp_tunnel_enable_bw_alloc(struct drm_dp_tunnel *tunnel)
947 struct drm_dp_tunnel_regs regs;
950 err = check_tunnel(tunnel);
954 if (!tunnel->bw_alloc_supported)
957 if (!tunnel_group_id(tunnel->group->drv_group_id))
960 err = set_bw_alloc_mode(tunnel, true);
965 * After a BWA disable/re-enable sequence the allocated BW can either
966 * stay at its last requested value or, for instance after system
967 * suspend/resume, TBT CM can reset back the allocation to the amount
968 * allocated in the legacy/non-BWA mode. Accordingly allow for the
969 * allocation to change wrt. the last SW state.
971 err = read_and_verify_tunnel_regs(tunnel, ®s,
972 ALLOW_ALLOCATED_BW_CHANGE);
974 set_bw_alloc_mode(tunnel, false);
979 if (!tunnel->max_dprx_rate)
980 update_dprx_caps(tunnel, ®s);
982 if (tunnel->group->available_bw == -1) {
983 err = update_group_available_bw(tunnel, ®s);
988 tun_dbg_stat(tunnel, err,
989 "Enabling BW alloc mode: DPRX:%dx%d Group alloc:%d/%d Mb/s",
990 tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
991 DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
992 DPTUN_BW_ARG(tunnel->group->available_bw));
996 EXPORT_SYMBOL(drm_dp_tunnel_enable_bw_alloc);
999 * drm_dp_tunnel_disable_bw_alloc - Disable DP tunnel BW allocation mode
1000 * @tunnel: Tunnel object
1002 * Disable the DP tunnel BW allocation mode on @tunnel.
1004 * Returns 0 in case of success, negative error code otherwise.
1006 int drm_dp_tunnel_disable_bw_alloc(struct drm_dp_tunnel *tunnel)
1010 err = check_tunnel(tunnel);
1014 tunnel->allocated_bw = -1;
1016 err = set_bw_alloc_mode(tunnel, false);
1018 tun_dbg_stat(tunnel, err, "Disabling BW alloc mode");
1022 EXPORT_SYMBOL(drm_dp_tunnel_disable_bw_alloc);
1025 * drm_dp_tunnel_bw_alloc_is_enabled - Query the BW allocation mode enabled state
1026 * @tunnel: Tunnel object
1028 * Query if the BW allocation mode is enabled for @tunnel.
1030 * Returns %true if the BW allocation mode is enabled for @tunnel.
1032 bool drm_dp_tunnel_bw_alloc_is_enabled(const struct drm_dp_tunnel *tunnel)
1034 return tunnel && tunnel->bw_alloc_enabled;
1036 EXPORT_SYMBOL(drm_dp_tunnel_bw_alloc_is_enabled);
1038 static int clear_bw_req_state(struct drm_dp_aux *aux)
1040 u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
1042 if (drm_dp_dpcd_writeb(aux, DP_TUNNELING_STATUS, bw_req_mask) < 0)
1048 static int bw_req_complete(struct drm_dp_aux *aux, bool *status_changed)
1050 u8 bw_req_mask = DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED;
1051 u8 status_change_mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
1055 if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
1058 *status_changed = val & status_change_mask;
1065 err = clear_bw_req_state(aux);
1069 return val == DP_BW_REQUEST_SUCCEEDED ? 0 : -ENOSPC;
1072 static int allocate_tunnel_bw(struct drm_dp_tunnel *tunnel, int bw)
1074 struct drm_dp_tunnel_mgr *mgr = tunnel->group->mgr;
1075 int request_bw = DIV_ROUND_UP(bw, tunnel->bw_granularity);
1076 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1085 if (request_bw * tunnel->bw_granularity == tunnel->allocated_bw)
1088 /* Atomic check should prevent the following. */
1089 if (drm_WARN_ON(mgr->dev, request_bw > MAX_DP_REQUEST_BW)) {
1094 err = clear_bw_req_state(tunnel->aux);
1098 if (drm_dp_dpcd_writeb(tunnel->aux, DP_REQUEST_BW, request_bw) < 0) {
1103 timeout = msecs_to_jiffies(3000);
1104 add_wait_queue(&mgr->bw_req_queue, &wait);
1107 bool status_changed;
1109 err = bw_req_complete(tunnel->aux, &status_changed);
1113 if (status_changed) {
1114 struct drm_dp_tunnel_regs regs;
1116 err = read_and_verify_tunnel_regs(tunnel, ®s,
1117 ALLOW_ALLOCATED_BW_CHANGE);
1127 timeout = wait_woken(&wait, TASK_UNINTERRUPTIBLE, timeout);
1130 remove_wait_queue(&mgr->bw_req_queue, &wait);
1135 tunnel->allocated_bw = request_bw * tunnel->bw_granularity;
1138 tun_dbg_stat(tunnel, err, "Allocating %d/%d Mb/s for tunnel: Group alloc:%d/%d Mb/s",
1139 DPTUN_BW_ARG(request_bw * tunnel->bw_granularity),
1140 DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
1141 DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
1142 DPTUN_BW_ARG(tunnel->group->available_bw));
1145 drm_dp_tunnel_set_io_error(tunnel);
1151 * drm_dp_tunnel_alloc_bw - Allocate BW for a DP tunnel
1152 * @tunnel: Tunnel object
1153 * @bw: BW in kB/s units
1155 * Allocate @bw kB/s for @tunnel. The allocated BW must be freed after use by
1156 * calling this function for the same tunnel setting @bw to 0.
1158 * Returns 0 in case of success, a negative error code otherwise.
1160 int drm_dp_tunnel_alloc_bw(struct drm_dp_tunnel *tunnel, int bw)
1164 err = check_tunnel(tunnel);
1168 return allocate_tunnel_bw(tunnel, bw);
1170 EXPORT_SYMBOL(drm_dp_tunnel_alloc_bw);
1173 * drm_dp_tunnel_get_allocated_bw - Get the BW allocated for a DP tunnel
1174 * @tunnel: Tunnel object
1176 * Get the current BW allocated for @tunnel. After the tunnel is created /
1177 * resumed and the BW allocation mode is enabled for it, the allocation
1178 * becomes determined only after the first allocation request by the driver
1179 * calling drm_dp_tunnel_alloc_bw().
1181 * Return the BW allocated for the tunnel, or -1 if the allocation is
1184 int drm_dp_tunnel_get_allocated_bw(struct drm_dp_tunnel *tunnel)
1186 return tunnel->allocated_bw;
1188 EXPORT_SYMBOL(drm_dp_tunnel_get_allocated_bw);
1191 * Return 0 if the status hasn't changed, 1 if the status has changed, a
1192 * negative error code in case of an I/O failure.
1194 static int check_and_clear_status_change(struct drm_dp_tunnel *tunnel)
1196 u8 mask = DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED;
1199 if (drm_dp_dpcd_readb(tunnel->aux, DP_TUNNELING_STATUS, &val) < 0)
1205 if (drm_dp_dpcd_writeb(tunnel->aux, DP_TUNNELING_STATUS, val) < 0)
1211 if (!drm_dp_tunnel_bw_alloc_is_enabled(tunnel))
1215 * Check for estimated BW changes explicitly to account for lost
1216 * BW change notifications.
1218 if (drm_dp_dpcd_readb(tunnel->aux, DP_ESTIMATED_BW, &val) < 0)
1221 if (val * tunnel->bw_granularity != tunnel->estimated_bw)
1227 drm_dp_tunnel_set_io_error(tunnel);
1233 * drm_dp_tunnel_update_state - Update DP tunnel SW state with the HW state
1234 * @tunnel: Tunnel object
1236 * Update the SW state of @tunnel with the HW state.
1238 * Returns 0 if the state has not changed, 1 if it has changed and got updated
1239 * successfully and a negative error code otherwise.
1241 int drm_dp_tunnel_update_state(struct drm_dp_tunnel *tunnel)
1243 struct drm_dp_tunnel_regs regs;
1244 bool changed = false;
1247 ret = check_tunnel(tunnel);
1251 ret = check_and_clear_status_change(tunnel);
1258 ret = read_and_verify_tunnel_regs(tunnel, ®s, 0);
1262 if (update_dprx_caps(tunnel, ®s))
1265 ret = update_group_available_bw(tunnel, ®s);
1270 tun_dbg_stat(tunnel, ret < 0 ? ret : 0,
1271 "State update: Changed:%s DPRX:%dx%d Tunnel alloc:%d/%d Group alloc:%d/%d Mb/s",
1272 str_yes_no(changed),
1273 tunnel->max_dprx_rate / 100, tunnel->max_dprx_lane_count,
1274 DPTUN_BW_ARG(tunnel->allocated_bw),
1275 DPTUN_BW_ARG(get_max_tunnel_bw(tunnel)),
1276 DPTUN_BW_ARG(group_allocated_bw(tunnel->group)),
1277 DPTUN_BW_ARG(tunnel->group->available_bw));
1287 EXPORT_SYMBOL(drm_dp_tunnel_update_state);
1290 * drm_dp_tunnel_handle_irq - Handle DP tunnel IRQs
1292 * Handle any pending DP tunnel IRQs, waking up waiters for a completion
1295 * Returns 1 if the state of the tunnel has changed which requires calling
1296 * drm_dp_tunnel_update_state(), a negative error code in case of a failure,
1299 int drm_dp_tunnel_handle_irq(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_aux *aux)
1303 if (drm_dp_dpcd_readb(aux, DP_TUNNELING_STATUS, &val) < 0)
1306 if (val & (DP_BW_REQUEST_SUCCEEDED | DP_BW_REQUEST_FAILED))
1307 wake_up_all(&mgr->bw_req_queue);
1309 if (val & (DP_BW_ALLOCATION_CAPABILITY_CHANGED | DP_ESTIMATED_BW_CHANGED))
1314 EXPORT_SYMBOL(drm_dp_tunnel_handle_irq);
1317 * drm_dp_tunnel_max_dprx_rate - Query the maximum rate of the tunnel's DPRX
1318 * @tunnel: Tunnel object
1320 * The function is used to query the maximum link rate of the DPRX connected
1321 * to @tunnel. Note that this rate will not be limited by the BW limit of the
1322 * tunnel, as opposed to the standard and extended DP_MAX_LINK_RATE DPCD
1325 * Returns the maximum link rate in 10 kbit/s units.
1327 int drm_dp_tunnel_max_dprx_rate(const struct drm_dp_tunnel *tunnel)
1329 return tunnel->max_dprx_rate;
1331 EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_rate);
1334 * drm_dp_tunnel_max_dprx_lane_count - Query the maximum lane count of the tunnel's DPRX
1335 * @tunnel: Tunnel object
1337 * The function is used to query the maximum lane count of the DPRX connected
1338 * to @tunnel. Note that this lane count will not be limited by the BW limit of
1339 * the tunnel, as opposed to the standard and extended DP_MAX_LANE_COUNT DPCD
1342 * Returns the maximum lane count.
1344 int drm_dp_tunnel_max_dprx_lane_count(const struct drm_dp_tunnel *tunnel)
1346 return tunnel->max_dprx_lane_count;
1348 EXPORT_SYMBOL(drm_dp_tunnel_max_dprx_lane_count);
1351 * drm_dp_tunnel_available_bw - Query the estimated total available BW of the tunnel
1352 * @tunnel: Tunnel object
1354 * This function is used to query the estimated total available BW of the
1355 * tunnel. This includes the currently allocated and free BW for all the
1356 * tunnels in @tunnel's group. The available BW is valid only after the BW
1357 * allocation mode has been enabled for the tunnel and its state got updated
1358 * calling drm_dp_tunnel_update_state().
1360 * Returns the @tunnel group's estimated total available bandwidth in kB/s
1361 * units, or -1 if the available BW isn't valid (the BW allocation mode is
1362 * not enabled or the tunnel's state hasn't been updated).
1364 int drm_dp_tunnel_available_bw(const struct drm_dp_tunnel *tunnel)
1366 return tunnel->group->available_bw;
1368 EXPORT_SYMBOL(drm_dp_tunnel_available_bw);
1370 static struct drm_dp_tunnel_group_state *
1371 drm_dp_tunnel_atomic_get_group_state(struct drm_atomic_state *state,
1372 const struct drm_dp_tunnel *tunnel)
1374 return (struct drm_dp_tunnel_group_state *)
1375 drm_atomic_get_private_obj_state(state,
1376 &tunnel->group->base);
1379 static struct drm_dp_tunnel_state *
1380 add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
1381 struct drm_dp_tunnel *tunnel)
1383 struct drm_dp_tunnel_state *tunnel_state;
1385 tun_dbg_atomic(tunnel,
1386 "Adding state for tunnel %p to group state %p\n",
1387 tunnel, group_state);
1389 tunnel_state = kzalloc(sizeof(*tunnel_state), GFP_KERNEL);
1393 tunnel_state->group_state = group_state;
1395 drm_dp_tunnel_ref_get(tunnel, &tunnel_state->tunnel_ref);
1397 INIT_LIST_HEAD(&tunnel_state->node);
1398 list_add(&tunnel_state->node, &group_state->tunnel_states);
1400 return tunnel_state;
1403 static void free_tunnel_state(struct drm_dp_tunnel_state *tunnel_state)
1405 tun_dbg_atomic(tunnel_state->tunnel_ref.tunnel,
1406 "Freeing state for tunnel %p\n",
1407 tunnel_state->tunnel_ref.tunnel);
1409 list_del(&tunnel_state->node);
1411 kfree(tunnel_state->stream_bw);
1412 drm_dp_tunnel_ref_put(&tunnel_state->tunnel_ref);
1414 kfree(tunnel_state);
1417 static void free_group_state(struct drm_dp_tunnel_group_state *group_state)
1419 struct drm_dp_tunnel_state *tunnel_state;
1420 struct drm_dp_tunnel_state *tunnel_state_tmp;
1422 for_each_tunnel_state_safe(group_state, tunnel_state, tunnel_state_tmp)
1423 free_tunnel_state(tunnel_state);
1428 static struct drm_dp_tunnel_state *
1429 get_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
1430 const struct drm_dp_tunnel *tunnel)
1432 struct drm_dp_tunnel_state *tunnel_state;
1434 for_each_tunnel_state(group_state, tunnel_state)
1435 if (tunnel_state->tunnel_ref.tunnel == tunnel)
1436 return tunnel_state;
1441 static struct drm_dp_tunnel_state *
1442 get_or_add_tunnel_state(struct drm_dp_tunnel_group_state *group_state,
1443 struct drm_dp_tunnel *tunnel)
1445 struct drm_dp_tunnel_state *tunnel_state;
1447 tunnel_state = get_tunnel_state(group_state, tunnel);
1449 return tunnel_state;
1451 return add_tunnel_state(group_state, tunnel);
1454 static struct drm_private_state *
1455 tunnel_group_duplicate_state(struct drm_private_obj *obj)
1457 struct drm_dp_tunnel_group_state *group_state;
1458 struct drm_dp_tunnel_state *tunnel_state;
1460 group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
1464 INIT_LIST_HEAD(&group_state->tunnel_states);
1466 __drm_atomic_helper_private_obj_duplicate_state(obj, &group_state->base);
1468 for_each_tunnel_state(to_group_state(obj->state), tunnel_state) {
1469 struct drm_dp_tunnel_state *new_tunnel_state;
1471 new_tunnel_state = get_or_add_tunnel_state(group_state,
1472 tunnel_state->tunnel_ref.tunnel);
1473 if (!new_tunnel_state)
1474 goto out_free_state;
1476 new_tunnel_state->stream_mask = tunnel_state->stream_mask;
1477 new_tunnel_state->stream_bw = kmemdup(tunnel_state->stream_bw,
1478 sizeof(*tunnel_state->stream_bw) *
1479 hweight32(tunnel_state->stream_mask),
1482 if (!new_tunnel_state->stream_bw)
1483 goto out_free_state;
1486 return &group_state->base;
1489 free_group_state(group_state);
1494 static void tunnel_group_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state)
1496 free_group_state(to_group_state(state));
1499 static const struct drm_private_state_funcs tunnel_group_funcs = {
1500 .atomic_duplicate_state = tunnel_group_duplicate_state,
1501 .atomic_destroy_state = tunnel_group_destroy_state,
1505 * drm_dp_tunnel_atomic_get_state - get/allocate the new atomic state for a tunnel
1506 * @state: Atomic state
1507 * @tunnel: Tunnel to get the state for
1509 * Get the new atomic state for @tunnel, duplicating it from the old tunnel
1510 * state if not yet allocated.
1512 * Return the state or an ERR_PTR() error on failure.
1514 struct drm_dp_tunnel_state *
1515 drm_dp_tunnel_atomic_get_state(struct drm_atomic_state *state,
1516 struct drm_dp_tunnel *tunnel)
1518 struct drm_dp_tunnel_group_state *group_state;
1519 struct drm_dp_tunnel_state *tunnel_state;
1521 group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel);
1522 if (IS_ERR(group_state))
1523 return ERR_CAST(group_state);
1525 tunnel_state = get_or_add_tunnel_state(group_state, tunnel);
1527 return ERR_PTR(-ENOMEM);
1529 return tunnel_state;
1531 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_state);
1534 * drm_dp_tunnel_atomic_get_old_state - get the old atomic state for a tunnel
1535 * @state: Atomic state
1536 * @tunnel: Tunnel to get the state for
1538 * Get the old atomic state for @tunnel.
1540 * Return the old state or NULL if the tunnel's atomic state is not in @state.
1542 struct drm_dp_tunnel_state *
1543 drm_dp_tunnel_atomic_get_old_state(struct drm_atomic_state *state,
1544 const struct drm_dp_tunnel *tunnel)
1546 struct drm_dp_tunnel_group_state *old_group_state;
1549 for_each_old_group_in_state(state, old_group_state, i)
1550 if (to_group(old_group_state->base.obj) == tunnel->group)
1551 return get_tunnel_state(old_group_state, tunnel);
1555 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_old_state);
1558 * drm_dp_tunnel_atomic_get_new_state - get the new atomic state for a tunnel
1559 * @state: Atomic state
1560 * @tunnel: Tunnel to get the state for
1562 * Get the new atomic state for @tunnel.
1564 * Return the new state or NULL if the tunnel's atomic state is not in @state.
1566 struct drm_dp_tunnel_state *
1567 drm_dp_tunnel_atomic_get_new_state(struct drm_atomic_state *state,
1568 const struct drm_dp_tunnel *tunnel)
1570 struct drm_dp_tunnel_group_state *new_group_state;
1573 for_each_new_group_in_state(state, new_group_state, i)
1574 if (to_group(new_group_state->base.obj) == tunnel->group)
1575 return get_tunnel_state(new_group_state, tunnel);
1579 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_new_state);
1581 static bool init_group(struct drm_dp_tunnel_mgr *mgr, struct drm_dp_tunnel_group *group)
1583 struct drm_dp_tunnel_group_state *group_state;
1585 group_state = kzalloc(sizeof(*group_state), GFP_KERNEL);
1589 INIT_LIST_HEAD(&group_state->tunnel_states);
1592 group->available_bw = -1;
1593 INIT_LIST_HEAD(&group->tunnels);
1595 drm_atomic_private_obj_init(mgr->dev, &group->base, &group_state->base,
1596 &tunnel_group_funcs);
1601 static void cleanup_group(struct drm_dp_tunnel_group *group)
1603 drm_atomic_private_obj_fini(&group->base);
1606 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
1607 static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
1609 const struct drm_dp_tunnel_state *tunnel_state;
1610 u32 stream_mask = 0;
1612 for_each_tunnel_state(group_state, tunnel_state) {
1613 drm_WARN(to_group(group_state->base.obj)->mgr->dev,
1614 tunnel_state->stream_mask & stream_mask,
1615 "[DPTUN %s]: conflicting stream IDs %x (IDs in other tunnels %x)\n",
1616 tunnel_state->tunnel_ref.tunnel->name,
1617 tunnel_state->stream_mask,
1620 stream_mask |= tunnel_state->stream_mask;
1624 static void check_unique_stream_ids(const struct drm_dp_tunnel_group_state *group_state)
1629 static int stream_id_to_idx(u32 stream_mask, u8 stream_id)
1631 return hweight32(stream_mask & (BIT(stream_id) - 1));
1634 static int resize_bw_array(struct drm_dp_tunnel_state *tunnel_state,
1635 unsigned long old_mask, unsigned long new_mask)
1637 unsigned long move_mask = old_mask & new_mask;
1638 int *new_bws = NULL;
1643 if (old_mask == new_mask)
1646 new_bws = kcalloc(hweight32(new_mask), sizeof(*new_bws), GFP_KERNEL);
1650 for_each_set_bit(id, &move_mask, BITS_PER_TYPE(move_mask))
1651 new_bws[stream_id_to_idx(new_mask, id)] =
1652 tunnel_state->stream_bw[stream_id_to_idx(old_mask, id)];
1654 kfree(tunnel_state->stream_bw);
1655 tunnel_state->stream_bw = new_bws;
1656 tunnel_state->stream_mask = new_mask;
1661 static int set_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
1662 u8 stream_id, int bw)
1666 err = resize_bw_array(tunnel_state,
1667 tunnel_state->stream_mask,
1668 tunnel_state->stream_mask | BIT(stream_id));
1672 tunnel_state->stream_bw[stream_id_to_idx(tunnel_state->stream_mask, stream_id)] = bw;
1677 static int clear_stream_bw(struct drm_dp_tunnel_state *tunnel_state,
1680 if (!(tunnel_state->stream_mask & ~BIT(stream_id))) {
1681 free_tunnel_state(tunnel_state);
1685 return resize_bw_array(tunnel_state,
1686 tunnel_state->stream_mask,
1687 tunnel_state->stream_mask & ~BIT(stream_id));
1691 * drm_dp_tunnel_atomic_set_stream_bw - Set the BW for a DP tunnel stream
1692 * @state: Atomic state
1693 * @tunnel: DP tunnel containing the stream
1694 * @stream_id: Stream ID
1695 * @bw: BW of the stream
1697 * Set a DP tunnel stream's required BW in the atomic state.
1699 * Returns 0 in case of success, a negative error code otherwise.
1701 int drm_dp_tunnel_atomic_set_stream_bw(struct drm_atomic_state *state,
1702 struct drm_dp_tunnel *tunnel,
1703 u8 stream_id, int bw)
1705 struct drm_dp_tunnel_group_state *new_group_state;
1706 struct drm_dp_tunnel_state *tunnel_state;
1709 if (drm_WARN_ON(tunnel->group->mgr->dev,
1710 stream_id > BITS_PER_TYPE(tunnel_state->stream_mask)))
1714 "Setting %d Mb/s for stream %d\n",
1715 DPTUN_BW_ARG(bw), stream_id);
1717 new_group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel);
1718 if (IS_ERR(new_group_state))
1719 return PTR_ERR(new_group_state);
1722 tunnel_state = get_tunnel_state(new_group_state, tunnel);
1726 return clear_stream_bw(tunnel_state, stream_id);
1729 tunnel_state = get_or_add_tunnel_state(new_group_state, tunnel);
1730 if (drm_WARN_ON(state->dev, !tunnel_state))
1733 err = set_stream_bw(tunnel_state, stream_id, bw);
1737 check_unique_stream_ids(new_group_state);
1741 EXPORT_SYMBOL(drm_dp_tunnel_atomic_set_stream_bw);
1744 * drm_dp_tunnel_atomic_get_required_bw - Get the BW required by a DP tunnel
1745 * @tunnel_state: Atomic state of the queried tunnel
1747 * Calculate the BW required by a tunnel adding up the required BW of all
1748 * the streams in the tunnel.
1750 * Return the total BW required by the tunnel.
1752 int drm_dp_tunnel_atomic_get_required_bw(const struct drm_dp_tunnel_state *tunnel_state)
1757 if (!tunnel_state || !tunnel_state->stream_mask)
1760 for (i = 0; i < hweight32(tunnel_state->stream_mask); i++)
1761 tunnel_bw += tunnel_state->stream_bw[i];
1765 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_required_bw);
1768 * drm_dp_tunnel_atomic_get_group_streams_in_state - Get mask of stream IDs in a group
1769 * @state: Atomic state
1770 * @tunnel: Tunnel object
1771 * @stream_mask: Mask of streams in @tunnel's group
1773 * Get the mask of all the stream IDs in the tunnel group of @tunnel.
1775 * Return 0 in case of success - with the stream IDs in @stream_mask - or a
1776 * negative error code in case of failure.
1778 int drm_dp_tunnel_atomic_get_group_streams_in_state(struct drm_atomic_state *state,
1779 const struct drm_dp_tunnel *tunnel,
1782 struct drm_dp_tunnel_group_state *group_state;
1783 struct drm_dp_tunnel_state *tunnel_state;
1785 group_state = drm_dp_tunnel_atomic_get_group_state(state, tunnel);
1786 if (IS_ERR(group_state))
1787 return PTR_ERR(group_state);
1790 for_each_tunnel_state(group_state, tunnel_state)
1791 *stream_mask |= tunnel_state->stream_mask;
1795 EXPORT_SYMBOL(drm_dp_tunnel_atomic_get_group_streams_in_state);
1798 drm_dp_tunnel_atomic_check_group_bw(struct drm_dp_tunnel_group_state *new_group_state,
1799 u32 *failed_stream_mask)
1801 struct drm_dp_tunnel_group *group = to_group(new_group_state->base.obj);
1802 struct drm_dp_tunnel_state *new_tunnel_state;
1803 u32 group_stream_mask = 0;
1806 for_each_tunnel_state(new_group_state, new_tunnel_state) {
1807 struct drm_dp_tunnel *tunnel = new_tunnel_state->tunnel_ref.tunnel;
1808 int max_dprx_bw = get_max_dprx_bw(tunnel);
1809 int tunnel_bw = drm_dp_tunnel_atomic_get_required_bw(new_tunnel_state);
1812 "%sRequired %d/%d Mb/s total for tunnel.\n",
1813 tunnel_bw > max_dprx_bw ? "Not enough BW: " : "",
1814 DPTUN_BW_ARG(tunnel_bw),
1815 DPTUN_BW_ARG(max_dprx_bw));
1817 if (tunnel_bw > max_dprx_bw) {
1818 *failed_stream_mask = new_tunnel_state->stream_mask;
1822 group_bw += min(roundup(tunnel_bw, tunnel->bw_granularity),
1824 group_stream_mask |= new_tunnel_state->stream_mask;
1828 "%sRequired %d/%d Mb/s total for tunnel group.\n",
1829 group_bw > group->available_bw ? "Not enough BW: " : "",
1830 DPTUN_BW_ARG(group_bw),
1831 DPTUN_BW_ARG(group->available_bw));
1833 if (group_bw > group->available_bw) {
1834 *failed_stream_mask = group_stream_mask;
1842 * drm_dp_tunnel_atomic_check_stream_bws - Check BW limit for all streams in state
1843 * @state: Atomic state
1844 * @failed_stream_mask: Mask of stream IDs with a BW limit failure
1846 * Check the required BW of each DP tunnel in @state against both the DPRX BW
1847 * limit of the tunnel and the BW limit of the tunnel group. Return a mask of
1848 * stream IDs in @failed_stream_mask once a check fails. The mask will contain
1849 * either all the streams in a tunnel (in case a DPRX BW limit check failed) or
1850 * all the streams in a tunnel group (in case a group BW limit check failed).
1852 * Return 0 if all the BW limit checks passed, -ENOSPC in case a BW limit
1853 * check failed - with @failed_stream_mask containing the streams failing the
1854 * check - or a negative error code otherwise.
1856 int drm_dp_tunnel_atomic_check_stream_bws(struct drm_atomic_state *state,
1857 u32 *failed_stream_mask)
1859 struct drm_dp_tunnel_group_state *new_group_state;
1862 for_each_new_group_in_state(state, new_group_state, i) {
1865 ret = drm_dp_tunnel_atomic_check_group_bw(new_group_state,
1866 failed_stream_mask);
1873 EXPORT_SYMBOL(drm_dp_tunnel_atomic_check_stream_bws);
1875 static void destroy_mgr(struct drm_dp_tunnel_mgr *mgr)
1879 for (i = 0; i < mgr->group_count; i++) {
1880 cleanup_group(&mgr->groups[i]);
1881 drm_WARN_ON(mgr->dev, !list_empty(&mgr->groups[i].tunnels));
1884 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
1885 ref_tracker_dir_exit(&mgr->ref_tracker);
1893 * drm_dp_tunnel_mgr_create - Create a DP tunnel manager
1894 * @dev: DRM device object
1895 * @max_group_count: Maximum number of tunnel groups
1897 * Creates a DP tunnel manager for @dev.
1899 * Returns a pointer to the tunnel manager if created successfully or error
1900 * pointer in case of failure.
1902 struct drm_dp_tunnel_mgr *
1903 drm_dp_tunnel_mgr_create(struct drm_device *dev, int max_group_count)
1905 struct drm_dp_tunnel_mgr *mgr;
1908 mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
1910 return ERR_PTR(-ENOMEM);
1913 init_waitqueue_head(&mgr->bw_req_queue);
1915 mgr->groups = kcalloc(max_group_count, sizeof(*mgr->groups), GFP_KERNEL);
1919 return ERR_PTR(-ENOMEM);
1922 #ifdef CONFIG_DRM_DISPLAY_DP_TUNNEL_STATE_DEBUG
1923 ref_tracker_dir_init(&mgr->ref_tracker, 16, "dptun");
1926 for (i = 0; i < max_group_count; i++) {
1927 if (!init_group(mgr, &mgr->groups[i])) {
1930 return ERR_PTR(-ENOMEM);
1938 EXPORT_SYMBOL(drm_dp_tunnel_mgr_create);
1941 * drm_dp_tunnel_mgr_destroy - Destroy DP tunnel manager
1942 * @mgr: Tunnel manager object
1944 * Destroy the tunnel manager.
1946 void drm_dp_tunnel_mgr_destroy(struct drm_dp_tunnel_mgr *mgr)
1950 EXPORT_SYMBOL(drm_dp_tunnel_mgr_destroy);