2 * Copyright © 2014 Red Hat
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 #include <linux/bitfield.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/i2c.h>
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/random.h>
30 #include <linux/sched.h>
31 #include <linux/seq_file.h>
32 #include <linux/iopoll.h>
34 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
35 #include <linux/stacktrace.h>
36 #include <linux/sort.h>
37 #include <linux/timekeeping.h>
38 #include <linux/math64.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_atomic_helper.h>
43 #include <drm/drm_dp_mst_helper.h>
44 #include <drm/drm_drv.h>
45 #include <drm/drm_print.h>
46 #include <drm/drm_probe_helper.h>
48 #include "drm_crtc_helper_internal.h"
49 #include "drm_dp_mst_topology_internal.h"
54 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
55 * protocol. The helpers contain a topology manager and bandwidth manager.
56 * The helpers encapsulate the sending and received of sideband msgs.
58 struct drm_dp_pending_up_req
{
59 struct drm_dp_sideband_msg_hdr hdr
;
60 struct drm_dp_sideband_msg_req_body msg
;
61 struct list_head next
;
64 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
67 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port
*port
);
69 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
71 struct drm_dp_payload
*payload
);
73 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr
*mgr
,
74 struct drm_dp_mst_port
*port
,
75 int offset
, int size
, u8
*bytes
);
76 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
77 struct drm_dp_mst_port
*port
,
78 int offset
, int size
, u8
*bytes
);
80 static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
81 struct drm_dp_mst_branch
*mstb
);
84 drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr
*mgr
,
85 struct drm_dp_mst_branch
*mstb
);
87 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
88 struct drm_dp_mst_branch
*mstb
,
89 struct drm_dp_mst_port
*port
);
90 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
93 static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port
*port
);
94 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port
*port
);
95 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
);
97 #define DBG_PREFIX "[dp_mst]"
99 #define DP_STR(x) [DP_ ## x] = #x
101 static const char *drm_dp_mst_req_type_str(u8 req_type
)
103 static const char * const req_type_str
[] = {
104 DP_STR(GET_MSG_TRANSACTION_VERSION
),
105 DP_STR(LINK_ADDRESS
),
106 DP_STR(CONNECTION_STATUS_NOTIFY
),
107 DP_STR(ENUM_PATH_RESOURCES
),
108 DP_STR(ALLOCATE_PAYLOAD
),
109 DP_STR(QUERY_PAYLOAD
),
110 DP_STR(RESOURCE_STATUS_NOTIFY
),
111 DP_STR(CLEAR_PAYLOAD_ID_TABLE
),
112 DP_STR(REMOTE_DPCD_READ
),
113 DP_STR(REMOTE_DPCD_WRITE
),
114 DP_STR(REMOTE_I2C_READ
),
115 DP_STR(REMOTE_I2C_WRITE
),
116 DP_STR(POWER_UP_PHY
),
117 DP_STR(POWER_DOWN_PHY
),
118 DP_STR(SINK_EVENT_NOTIFY
),
119 DP_STR(QUERY_STREAM_ENC_STATUS
),
122 if (req_type
>= ARRAY_SIZE(req_type_str
) ||
123 !req_type_str
[req_type
])
126 return req_type_str
[req_type
];
130 #define DP_STR(x) [DP_NAK_ ## x] = #x
132 static const char *drm_dp_mst_nak_reason_str(u8 nak_reason
)
134 static const char * const nak_reason_str
[] = {
135 DP_STR(WRITE_FAILURE
),
136 DP_STR(INVALID_READ
),
140 DP_STR(LINK_FAILURE
),
141 DP_STR(NO_RESOURCES
),
144 DP_STR(ALLOCATE_FAIL
),
147 if (nak_reason
>= ARRAY_SIZE(nak_reason_str
) ||
148 !nak_reason_str
[nak_reason
])
151 return nak_reason_str
[nak_reason
];
155 #define DP_STR(x) [DRM_DP_SIDEBAND_TX_ ## x] = #x
157 static const char *drm_dp_mst_sideband_tx_state_str(int state
)
159 static const char * const sideband_reason_str
[] = {
167 if (state
>= ARRAY_SIZE(sideband_reason_str
) ||
168 !sideband_reason_str
[state
])
171 return sideband_reason_str
[state
];
175 drm_dp_mst_rad_to_str(const u8 rad
[8], u8 lct
, char *out
, size_t len
)
180 for (i
= 0; i
< lct
; i
++) {
182 unpacked_rad
[i
] = rad
[i
/ 2] >> 4;
184 unpacked_rad
[i
] = rad
[i
/ 2] & BIT_MASK(4);
187 /* TODO: Eventually add something to printk so we can format the rad
190 return snprintf(out
, len
, "%*phC", lct
, unpacked_rad
);
193 /* sideband msg handling */
194 static u8
drm_dp_msg_header_crc4(const uint8_t *data
, size_t num_nibbles
)
199 int number_of_bits
= num_nibbles
* 4;
202 while (number_of_bits
!= 0) {
205 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
213 if ((remainder
& 0x10) == 0x10)
218 while (number_of_bits
!= 0) {
221 if ((remainder
& 0x10) != 0)
228 static u8
drm_dp_msg_data_crc4(const uint8_t *data
, u8 number_of_bytes
)
233 int number_of_bits
= number_of_bytes
* 8;
236 while (number_of_bits
!= 0) {
239 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
247 if ((remainder
& 0x100) == 0x100)
252 while (number_of_bits
!= 0) {
255 if ((remainder
& 0x100) != 0)
259 return remainder
& 0xff;
261 static inline u8
drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr
*hdr
)
265 size
+= (hdr
->lct
/ 2);
269 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
276 buf
[idx
++] = ((hdr
->lct
& 0xf) << 4) | (hdr
->lcr
& 0xf);
277 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
278 buf
[idx
++] = hdr
->rad
[i
];
279 buf
[idx
++] = (hdr
->broadcast
<< 7) | (hdr
->path_msg
<< 6) |
280 (hdr
->msg_len
& 0x3f);
281 buf
[idx
++] = (hdr
->somt
<< 7) | (hdr
->eomt
<< 6) | (hdr
->seqno
<< 4);
283 crc4
= drm_dp_msg_header_crc4(buf
, (idx
* 2) - 1);
284 buf
[idx
- 1] |= (crc4
& 0xf);
289 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
290 u8
*buf
, int buflen
, u8
*hdrlen
)
300 len
+= ((buf
[0] & 0xf0) >> 4) / 2;
303 crc4
= drm_dp_msg_header_crc4(buf
, (len
* 2) - 1);
305 if ((crc4
& 0xf) != (buf
[len
- 1] & 0xf)) {
306 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4
, buf
[len
- 1]);
310 hdr
->lct
= (buf
[0] & 0xf0) >> 4;
311 hdr
->lcr
= (buf
[0] & 0xf);
313 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
314 hdr
->rad
[i
] = buf
[idx
++];
315 hdr
->broadcast
= (buf
[idx
] >> 7) & 0x1;
316 hdr
->path_msg
= (buf
[idx
] >> 6) & 0x1;
317 hdr
->msg_len
= buf
[idx
] & 0x3f;
319 hdr
->somt
= (buf
[idx
] >> 7) & 0x1;
320 hdr
->eomt
= (buf
[idx
] >> 6) & 0x1;
321 hdr
->seqno
= (buf
[idx
] >> 4) & 0x1;
328 drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body
*req
,
329 struct drm_dp_sideband_msg_tx
*raw
)
335 buf
[idx
++] = req
->req_type
& 0x7f;
337 switch (req
->req_type
) {
338 case DP_ENUM_PATH_RESOURCES
:
339 case DP_POWER_DOWN_PHY
:
340 case DP_POWER_UP_PHY
:
341 buf
[idx
] = (req
->u
.port_num
.port_number
& 0xf) << 4;
344 case DP_ALLOCATE_PAYLOAD
:
345 buf
[idx
] = (req
->u
.allocate_payload
.port_number
& 0xf) << 4 |
346 (req
->u
.allocate_payload
.number_sdp_streams
& 0xf);
348 buf
[idx
] = (req
->u
.allocate_payload
.vcpi
& 0x7f);
350 buf
[idx
] = (req
->u
.allocate_payload
.pbn
>> 8);
352 buf
[idx
] = (req
->u
.allocate_payload
.pbn
& 0xff);
354 for (i
= 0; i
< req
->u
.allocate_payload
.number_sdp_streams
/ 2; i
++) {
355 buf
[idx
] = ((req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2] & 0xf) << 4) |
356 (req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2 + 1] & 0xf);
359 if (req
->u
.allocate_payload
.number_sdp_streams
& 1) {
360 i
= req
->u
.allocate_payload
.number_sdp_streams
- 1;
361 buf
[idx
] = (req
->u
.allocate_payload
.sdp_stream_sink
[i
] & 0xf) << 4;
365 case DP_QUERY_PAYLOAD
:
366 buf
[idx
] = (req
->u
.query_payload
.port_number
& 0xf) << 4;
368 buf
[idx
] = (req
->u
.query_payload
.vcpi
& 0x7f);
371 case DP_REMOTE_DPCD_READ
:
372 buf
[idx
] = (req
->u
.dpcd_read
.port_number
& 0xf) << 4;
373 buf
[idx
] |= ((req
->u
.dpcd_read
.dpcd_address
& 0xf0000) >> 16) & 0xf;
375 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff00) >> 8;
377 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff);
379 buf
[idx
] = (req
->u
.dpcd_read
.num_bytes
);
383 case DP_REMOTE_DPCD_WRITE
:
384 buf
[idx
] = (req
->u
.dpcd_write
.port_number
& 0xf) << 4;
385 buf
[idx
] |= ((req
->u
.dpcd_write
.dpcd_address
& 0xf0000) >> 16) & 0xf;
387 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff00) >> 8;
389 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff);
391 buf
[idx
] = (req
->u
.dpcd_write
.num_bytes
);
393 memcpy(&buf
[idx
], req
->u
.dpcd_write
.bytes
, req
->u
.dpcd_write
.num_bytes
);
394 idx
+= req
->u
.dpcd_write
.num_bytes
;
396 case DP_REMOTE_I2C_READ
:
397 buf
[idx
] = (req
->u
.i2c_read
.port_number
& 0xf) << 4;
398 buf
[idx
] |= (req
->u
.i2c_read
.num_transactions
& 0x3);
400 for (i
= 0; i
< (req
->u
.i2c_read
.num_transactions
& 0x3); i
++) {
401 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].i2c_dev_id
& 0x7f;
403 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].num_bytes
;
405 memcpy(&buf
[idx
], req
->u
.i2c_read
.transactions
[i
].bytes
, req
->u
.i2c_read
.transactions
[i
].num_bytes
);
406 idx
+= req
->u
.i2c_read
.transactions
[i
].num_bytes
;
408 buf
[idx
] = (req
->u
.i2c_read
.transactions
[i
].no_stop_bit
& 0x1) << 4;
409 buf
[idx
] |= (req
->u
.i2c_read
.transactions
[i
].i2c_transaction_delay
& 0xf);
412 buf
[idx
] = (req
->u
.i2c_read
.read_i2c_device_id
) & 0x7f;
414 buf
[idx
] = (req
->u
.i2c_read
.num_bytes_read
);
418 case DP_REMOTE_I2C_WRITE
:
419 buf
[idx
] = (req
->u
.i2c_write
.port_number
& 0xf) << 4;
421 buf
[idx
] = (req
->u
.i2c_write
.write_i2c_device_id
) & 0x7f;
423 buf
[idx
] = (req
->u
.i2c_write
.num_bytes
);
425 memcpy(&buf
[idx
], req
->u
.i2c_write
.bytes
, req
->u
.i2c_write
.num_bytes
);
426 idx
+= req
->u
.i2c_write
.num_bytes
;
428 case DP_QUERY_STREAM_ENC_STATUS
: {
429 const struct drm_dp_query_stream_enc_status
*msg
;
431 msg
= &req
->u
.enc_status
;
432 buf
[idx
] = msg
->stream_id
;
434 memcpy(&buf
[idx
], msg
->client_id
, sizeof(msg
->client_id
));
435 idx
+= sizeof(msg
->client_id
);
437 buf
[idx
] |= FIELD_PREP(GENMASK(1, 0), msg
->stream_event
);
438 buf
[idx
] |= msg
->valid_stream_event
? BIT(2) : 0;
439 buf
[idx
] |= FIELD_PREP(GENMASK(4, 3), msg
->stream_behavior
);
440 buf
[idx
] |= msg
->valid_stream_behavior
? BIT(5) : 0;
447 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req
);
449 /* Decode a sideband request we've encoded, mainly used for debugging */
451 drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx
*raw
,
452 struct drm_dp_sideband_msg_req_body
*req
)
454 const u8
*buf
= raw
->msg
;
457 req
->req_type
= buf
[idx
++] & 0x7f;
458 switch (req
->req_type
) {
459 case DP_ENUM_PATH_RESOURCES
:
460 case DP_POWER_DOWN_PHY
:
461 case DP_POWER_UP_PHY
:
462 req
->u
.port_num
.port_number
= (buf
[idx
] >> 4) & 0xf;
464 case DP_ALLOCATE_PAYLOAD
:
466 struct drm_dp_allocate_payload
*a
=
467 &req
->u
.allocate_payload
;
469 a
->number_sdp_streams
= buf
[idx
] & 0xf;
470 a
->port_number
= (buf
[idx
] >> 4) & 0xf;
472 WARN_ON(buf
[++idx
] & 0x80);
473 a
->vcpi
= buf
[idx
] & 0x7f;
475 a
->pbn
= buf
[++idx
] << 8;
476 a
->pbn
|= buf
[++idx
];
479 for (i
= 0; i
< a
->number_sdp_streams
; i
++) {
480 a
->sdp_stream_sink
[i
] =
481 (buf
[idx
+ (i
/ 2)] >> ((i
% 2) ? 0 : 4)) & 0xf;
485 case DP_QUERY_PAYLOAD
:
486 req
->u
.query_payload
.port_number
= (buf
[idx
] >> 4) & 0xf;
487 WARN_ON(buf
[++idx
] & 0x80);
488 req
->u
.query_payload
.vcpi
= buf
[idx
] & 0x7f;
490 case DP_REMOTE_DPCD_READ
:
492 struct drm_dp_remote_dpcd_read
*r
= &req
->u
.dpcd_read
;
494 r
->port_number
= (buf
[idx
] >> 4) & 0xf;
496 r
->dpcd_address
= (buf
[idx
] << 16) & 0xf0000;
497 r
->dpcd_address
|= (buf
[++idx
] << 8) & 0xff00;
498 r
->dpcd_address
|= buf
[++idx
] & 0xff;
500 r
->num_bytes
= buf
[++idx
];
503 case DP_REMOTE_DPCD_WRITE
:
505 struct drm_dp_remote_dpcd_write
*w
=
508 w
->port_number
= (buf
[idx
] >> 4) & 0xf;
510 w
->dpcd_address
= (buf
[idx
] << 16) & 0xf0000;
511 w
->dpcd_address
|= (buf
[++idx
] << 8) & 0xff00;
512 w
->dpcd_address
|= buf
[++idx
] & 0xff;
514 w
->num_bytes
= buf
[++idx
];
516 w
->bytes
= kmemdup(&buf
[++idx
], w
->num_bytes
,
522 case DP_REMOTE_I2C_READ
:
524 struct drm_dp_remote_i2c_read
*r
= &req
->u
.i2c_read
;
525 struct drm_dp_remote_i2c_read_tx
*tx
;
528 r
->num_transactions
= buf
[idx
] & 0x3;
529 r
->port_number
= (buf
[idx
] >> 4) & 0xf;
530 for (i
= 0; i
< r
->num_transactions
; i
++) {
531 tx
= &r
->transactions
[i
];
533 tx
->i2c_dev_id
= buf
[++idx
] & 0x7f;
534 tx
->num_bytes
= buf
[++idx
];
535 tx
->bytes
= kmemdup(&buf
[++idx
],
542 idx
+= tx
->num_bytes
;
543 tx
->no_stop_bit
= (buf
[idx
] >> 5) & 0x1;
544 tx
->i2c_transaction_delay
= buf
[idx
] & 0xf;
548 for (i
= 0; i
< r
->num_transactions
; i
++) {
549 tx
= &r
->transactions
[i
];
555 r
->read_i2c_device_id
= buf
[++idx
] & 0x7f;
556 r
->num_bytes_read
= buf
[++idx
];
559 case DP_REMOTE_I2C_WRITE
:
561 struct drm_dp_remote_i2c_write
*w
= &req
->u
.i2c_write
;
563 w
->port_number
= (buf
[idx
] >> 4) & 0xf;
564 w
->write_i2c_device_id
= buf
[++idx
] & 0x7f;
565 w
->num_bytes
= buf
[++idx
];
566 w
->bytes
= kmemdup(&buf
[++idx
], w
->num_bytes
,
572 case DP_QUERY_STREAM_ENC_STATUS
:
573 req
->u
.enc_status
.stream_id
= buf
[idx
++];
574 for (i
= 0; i
< sizeof(req
->u
.enc_status
.client_id
); i
++)
575 req
->u
.enc_status
.client_id
[i
] = buf
[idx
++];
577 req
->u
.enc_status
.stream_event
= FIELD_GET(GENMASK(1, 0),
579 req
->u
.enc_status
.valid_stream_event
= FIELD_GET(BIT(2),
581 req
->u
.enc_status
.stream_behavior
= FIELD_GET(GENMASK(4, 3),
583 req
->u
.enc_status
.valid_stream_behavior
= FIELD_GET(BIT(5),
590 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req
);
593 drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body
*req
,
594 int indent
, struct drm_printer
*printer
)
598 #define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)
599 if (req
->req_type
== DP_LINK_ADDRESS
) {
600 /* No contents to print */
601 P("type=%s\n", drm_dp_mst_req_type_str(req
->req_type
));
605 P("type=%s contents:\n", drm_dp_mst_req_type_str(req
->req_type
));
608 switch (req
->req_type
) {
609 case DP_ENUM_PATH_RESOURCES
:
610 case DP_POWER_DOWN_PHY
:
611 case DP_POWER_UP_PHY
:
612 P("port=%d\n", req
->u
.port_num
.port_number
);
614 case DP_ALLOCATE_PAYLOAD
:
615 P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
616 req
->u
.allocate_payload
.port_number
,
617 req
->u
.allocate_payload
.vcpi
, req
->u
.allocate_payload
.pbn
,
618 req
->u
.allocate_payload
.number_sdp_streams
,
619 req
->u
.allocate_payload
.number_sdp_streams
,
620 req
->u
.allocate_payload
.sdp_stream_sink
);
622 case DP_QUERY_PAYLOAD
:
623 P("port=%d vcpi=%d\n",
624 req
->u
.query_payload
.port_number
,
625 req
->u
.query_payload
.vcpi
);
627 case DP_REMOTE_DPCD_READ
:
628 P("port=%d dpcd_addr=%05x len=%d\n",
629 req
->u
.dpcd_read
.port_number
, req
->u
.dpcd_read
.dpcd_address
,
630 req
->u
.dpcd_read
.num_bytes
);
632 case DP_REMOTE_DPCD_WRITE
:
633 P("port=%d addr=%05x len=%d: %*ph\n",
634 req
->u
.dpcd_write
.port_number
,
635 req
->u
.dpcd_write
.dpcd_address
,
636 req
->u
.dpcd_write
.num_bytes
, req
->u
.dpcd_write
.num_bytes
,
637 req
->u
.dpcd_write
.bytes
);
639 case DP_REMOTE_I2C_READ
:
640 P("port=%d num_tx=%d id=%d size=%d:\n",
641 req
->u
.i2c_read
.port_number
,
642 req
->u
.i2c_read
.num_transactions
,
643 req
->u
.i2c_read
.read_i2c_device_id
,
644 req
->u
.i2c_read
.num_bytes_read
);
647 for (i
= 0; i
< req
->u
.i2c_read
.num_transactions
; i
++) {
648 const struct drm_dp_remote_i2c_read_tx
*rtx
=
649 &req
->u
.i2c_read
.transactions
[i
];
651 P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
652 i
, rtx
->i2c_dev_id
, rtx
->num_bytes
,
653 rtx
->no_stop_bit
, rtx
->i2c_transaction_delay
,
654 rtx
->num_bytes
, rtx
->bytes
);
657 case DP_REMOTE_I2C_WRITE
:
658 P("port=%d id=%d size=%d: %*ph\n",
659 req
->u
.i2c_write
.port_number
,
660 req
->u
.i2c_write
.write_i2c_device_id
,
661 req
->u
.i2c_write
.num_bytes
, req
->u
.i2c_write
.num_bytes
,
662 req
->u
.i2c_write
.bytes
);
664 case DP_QUERY_STREAM_ENC_STATUS
:
665 P("stream_id=%u client_id=%*ph stream_event=%x "
666 "valid_event=%d stream_behavior=%x valid_behavior=%d",
667 req
->u
.enc_status
.stream_id
,
668 (int)ARRAY_SIZE(req
->u
.enc_status
.client_id
),
669 req
->u
.enc_status
.client_id
, req
->u
.enc_status
.stream_event
,
670 req
->u
.enc_status
.valid_stream_event
,
671 req
->u
.enc_status
.stream_behavior
,
672 req
->u
.enc_status
.valid_stream_behavior
);
680 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body
);
683 drm_dp_mst_dump_sideband_msg_tx(struct drm_printer
*p
,
684 const struct drm_dp_sideband_msg_tx
*txmsg
)
686 struct drm_dp_sideband_msg_req_body req
;
691 drm_dp_mst_rad_to_str(txmsg
->dst
->rad
, txmsg
->dst
->lct
, buf
,
693 drm_printf(p
, "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
694 txmsg
->cur_offset
, txmsg
->cur_len
, txmsg
->seqno
,
695 drm_dp_mst_sideband_tx_state_str(txmsg
->state
),
696 txmsg
->path_msg
, buf
);
698 ret
= drm_dp_decode_sideband_req(txmsg
, &req
);
700 drm_printf(p
, "<failed to decode sideband req: %d>\n", ret
);
703 drm_dp_dump_sideband_msg_req_body(&req
, 1, p
);
705 switch (req
.req_type
) {
706 case DP_REMOTE_DPCD_WRITE
:
707 kfree(req
.u
.dpcd_write
.bytes
);
709 case DP_REMOTE_I2C_READ
:
710 for (i
= 0; i
< req
.u
.i2c_read
.num_transactions
; i
++)
711 kfree(req
.u
.i2c_read
.transactions
[i
].bytes
);
713 case DP_REMOTE_I2C_WRITE
:
714 kfree(req
.u
.i2c_write
.bytes
);
719 static void drm_dp_crc_sideband_chunk_req(u8
*msg
, u8 len
)
723 crc4
= drm_dp_msg_data_crc4(msg
, len
);
727 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body
*rep
,
728 struct drm_dp_sideband_msg_tx
*raw
)
733 buf
[idx
++] = (rep
->reply_type
& 0x1) << 7 | (rep
->req_type
& 0x7f);
738 static int drm_dp_sideband_msg_set_header(struct drm_dp_sideband_msg_rx
*msg
,
739 struct drm_dp_sideband_msg_hdr
*hdr
,
743 * ignore out-of-order messages or messages that are part of a
746 if (!hdr
->somt
&& !msg
->have_somt
)
749 /* get length contained in this portion */
750 msg
->curchunk_idx
= 0;
751 msg
->curchunk_len
= hdr
->msg_len
;
752 msg
->curchunk_hdrlen
= hdrlen
;
754 /* we have already gotten an somt - don't bother parsing */
755 if (hdr
->somt
&& msg
->have_somt
)
759 memcpy(&msg
->initial_hdr
, hdr
,
760 sizeof(struct drm_dp_sideband_msg_hdr
));
761 msg
->have_somt
= true;
764 msg
->have_eomt
= true;
769 /* this adds a chunk of msg to the builder to get the final msg */
770 static bool drm_dp_sideband_append_payload(struct drm_dp_sideband_msg_rx
*msg
,
771 u8
*replybuf
, u8 replybuflen
)
775 memcpy(&msg
->chunk
[msg
->curchunk_idx
], replybuf
, replybuflen
);
776 msg
->curchunk_idx
+= replybuflen
;
778 if (msg
->curchunk_idx
>= msg
->curchunk_len
) {
780 crc4
= drm_dp_msg_data_crc4(msg
->chunk
, msg
->curchunk_len
- 1);
781 if (crc4
!= msg
->chunk
[msg
->curchunk_len
- 1])
782 print_hex_dump(KERN_DEBUG
, "wrong crc",
783 DUMP_PREFIX_NONE
, 16, 1,
784 msg
->chunk
, msg
->curchunk_len
, false);
785 /* copy chunk into bigger msg */
786 memcpy(&msg
->msg
[msg
->curlen
], msg
->chunk
, msg
->curchunk_len
- 1);
787 msg
->curlen
+= msg
->curchunk_len
- 1;
792 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx
*raw
,
793 struct drm_dp_sideband_msg_reply_body
*repmsg
)
798 memcpy(repmsg
->u
.link_addr
.guid
, &raw
->msg
[idx
], 16);
800 repmsg
->u
.link_addr
.nports
= raw
->msg
[idx
] & 0xf;
802 if (idx
> raw
->curlen
)
804 for (i
= 0; i
< repmsg
->u
.link_addr
.nports
; i
++) {
805 if (raw
->msg
[idx
] & 0x80)
806 repmsg
->u
.link_addr
.ports
[i
].input_port
= 1;
808 repmsg
->u
.link_addr
.ports
[i
].peer_device_type
= (raw
->msg
[idx
] >> 4) & 0x7;
809 repmsg
->u
.link_addr
.ports
[i
].port_number
= (raw
->msg
[idx
] & 0xf);
812 if (idx
> raw
->curlen
)
814 repmsg
->u
.link_addr
.ports
[i
].mcs
= (raw
->msg
[idx
] >> 7) & 0x1;
815 repmsg
->u
.link_addr
.ports
[i
].ddps
= (raw
->msg
[idx
] >> 6) & 0x1;
816 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0)
817 repmsg
->u
.link_addr
.ports
[i
].legacy_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
819 if (idx
> raw
->curlen
)
821 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0) {
822 repmsg
->u
.link_addr
.ports
[i
].dpcd_revision
= (raw
->msg
[idx
]);
824 if (idx
> raw
->curlen
)
826 memcpy(repmsg
->u
.link_addr
.ports
[i
].peer_guid
, &raw
->msg
[idx
], 16);
828 if (idx
> raw
->curlen
)
830 repmsg
->u
.link_addr
.ports
[i
].num_sdp_streams
= (raw
->msg
[idx
] >> 4) & 0xf;
831 repmsg
->u
.link_addr
.ports
[i
].num_sdp_stream_sinks
= (raw
->msg
[idx
] & 0xf);
835 if (idx
> raw
->curlen
)
841 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
845 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx
*raw
,
846 struct drm_dp_sideband_msg_reply_body
*repmsg
)
850 repmsg
->u
.remote_dpcd_read_ack
.port_number
= raw
->msg
[idx
] & 0xf;
852 if (idx
> raw
->curlen
)
854 repmsg
->u
.remote_dpcd_read_ack
.num_bytes
= raw
->msg
[idx
];
856 if (idx
> raw
->curlen
)
859 memcpy(repmsg
->u
.remote_dpcd_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_dpcd_read_ack
.num_bytes
);
862 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
866 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx
*raw
,
867 struct drm_dp_sideband_msg_reply_body
*repmsg
)
871 repmsg
->u
.remote_dpcd_write_ack
.port_number
= raw
->msg
[idx
] & 0xf;
873 if (idx
> raw
->curlen
)
877 DRM_DEBUG_KMS("parse length fail %d %d\n", idx
, raw
->curlen
);
881 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx
*raw
,
882 struct drm_dp_sideband_msg_reply_body
*repmsg
)
886 repmsg
->u
.remote_i2c_read_ack
.port_number
= (raw
->msg
[idx
] & 0xf);
888 if (idx
> raw
->curlen
)
890 repmsg
->u
.remote_i2c_read_ack
.num_bytes
= raw
->msg
[idx
];
893 memcpy(repmsg
->u
.remote_i2c_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_i2c_read_ack
.num_bytes
);
896 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx
, raw
->curlen
);
900 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx
*raw
,
901 struct drm_dp_sideband_msg_reply_body
*repmsg
)
905 repmsg
->u
.path_resources
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
906 repmsg
->u
.path_resources
.fec_capable
= raw
->msg
[idx
] & 0x1;
908 if (idx
> raw
->curlen
)
910 repmsg
->u
.path_resources
.full_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
912 if (idx
> raw
->curlen
)
914 repmsg
->u
.path_resources
.avail_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
916 if (idx
> raw
->curlen
)
920 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx
, raw
->curlen
);
924 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
925 struct drm_dp_sideband_msg_reply_body
*repmsg
)
929 repmsg
->u
.allocate_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
931 if (idx
> raw
->curlen
)
933 repmsg
->u
.allocate_payload
.vcpi
= raw
->msg
[idx
];
935 if (idx
> raw
->curlen
)
937 repmsg
->u
.allocate_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
939 if (idx
> raw
->curlen
)
943 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx
, raw
->curlen
);
947 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
948 struct drm_dp_sideband_msg_reply_body
*repmsg
)
952 repmsg
->u
.query_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
954 if (idx
> raw
->curlen
)
956 repmsg
->u
.query_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
958 if (idx
> raw
->curlen
)
962 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx
, raw
->curlen
);
966 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx
*raw
,
967 struct drm_dp_sideband_msg_reply_body
*repmsg
)
971 repmsg
->u
.port_number
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
973 if (idx
> raw
->curlen
) {
974 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
982 drm_dp_sideband_parse_query_stream_enc_status(
983 struct drm_dp_sideband_msg_rx
*raw
,
984 struct drm_dp_sideband_msg_reply_body
*repmsg
)
986 struct drm_dp_query_stream_enc_status_ack_reply
*reply
;
988 reply
= &repmsg
->u
.enc_status
;
990 reply
->stream_id
= raw
->msg
[3];
992 reply
->reply_signed
= raw
->msg
[2] & BIT(0);
995 * NOTE: It's my impression from reading the spec that the below parsing
996 * is correct. However I noticed while testing with an HDCP 1.4 display
997 * through an HDCP 2.2 hub that only bit 3 was set. In that case, I
998 * would expect both bits to be set. So keep the parsing following the
999 * spec, but beware reality might not match the spec (at least for some
1002 reply
->hdcp_1x_device_present
= raw
->msg
[2] & BIT(4);
1003 reply
->hdcp_2x_device_present
= raw
->msg
[2] & BIT(3);
1005 reply
->query_capable_device_present
= raw
->msg
[2] & BIT(5);
1006 reply
->legacy_device_present
= raw
->msg
[2] & BIT(6);
1007 reply
->unauthorizable_device_present
= raw
->msg
[2] & BIT(7);
1009 reply
->auth_completed
= !!(raw
->msg
[1] & BIT(3));
1010 reply
->encryption_enabled
= !!(raw
->msg
[1] & BIT(4));
1011 reply
->repeater_present
= !!(raw
->msg
[1] & BIT(5));
1012 reply
->state
= (raw
->msg
[1] & GENMASK(7, 6)) >> 6;
1017 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx
*raw
,
1018 struct drm_dp_sideband_msg_reply_body
*msg
)
1020 memset(msg
, 0, sizeof(*msg
));
1021 msg
->reply_type
= (raw
->msg
[0] & 0x80) >> 7;
1022 msg
->req_type
= (raw
->msg
[0] & 0x7f);
1024 if (msg
->reply_type
== DP_SIDEBAND_REPLY_NAK
) {
1025 memcpy(msg
->u
.nak
.guid
, &raw
->msg
[1], 16);
1026 msg
->u
.nak
.reason
= raw
->msg
[17];
1027 msg
->u
.nak
.nak_data
= raw
->msg
[18];
1031 switch (msg
->req_type
) {
1032 case DP_LINK_ADDRESS
:
1033 return drm_dp_sideband_parse_link_address(raw
, msg
);
1034 case DP_QUERY_PAYLOAD
:
1035 return drm_dp_sideband_parse_query_payload_ack(raw
, msg
);
1036 case DP_REMOTE_DPCD_READ
:
1037 return drm_dp_sideband_parse_remote_dpcd_read(raw
, msg
);
1038 case DP_REMOTE_DPCD_WRITE
:
1039 return drm_dp_sideband_parse_remote_dpcd_write(raw
, msg
);
1040 case DP_REMOTE_I2C_READ
:
1041 return drm_dp_sideband_parse_remote_i2c_read_ack(raw
, msg
);
1042 case DP_REMOTE_I2C_WRITE
:
1043 return true; /* since there's nothing to parse */
1044 case DP_ENUM_PATH_RESOURCES
:
1045 return drm_dp_sideband_parse_enum_path_resources_ack(raw
, msg
);
1046 case DP_ALLOCATE_PAYLOAD
:
1047 return drm_dp_sideband_parse_allocate_payload_ack(raw
, msg
);
1048 case DP_POWER_DOWN_PHY
:
1049 case DP_POWER_UP_PHY
:
1050 return drm_dp_sideband_parse_power_updown_phy_ack(raw
, msg
);
1051 case DP_CLEAR_PAYLOAD_ID_TABLE
:
1052 return true; /* since there's nothing to parse */
1053 case DP_QUERY_STREAM_ENC_STATUS
:
1054 return drm_dp_sideband_parse_query_stream_enc_status(raw
, msg
);
1056 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg
->req_type
,
1057 drm_dp_mst_req_type_str(msg
->req_type
));
1062 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
1063 struct drm_dp_sideband_msg_req_body
*msg
)
1067 msg
->u
.conn_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
1069 if (idx
> raw
->curlen
)
1072 memcpy(msg
->u
.conn_stat
.guid
, &raw
->msg
[idx
], 16);
1074 if (idx
> raw
->curlen
)
1077 msg
->u
.conn_stat
.legacy_device_plug_status
= (raw
->msg
[idx
] >> 6) & 0x1;
1078 msg
->u
.conn_stat
.displayport_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
1079 msg
->u
.conn_stat
.message_capability_status
= (raw
->msg
[idx
] >> 4) & 0x1;
1080 msg
->u
.conn_stat
.input_port
= (raw
->msg
[idx
] >> 3) & 0x1;
1081 msg
->u
.conn_stat
.peer_device_type
= (raw
->msg
[idx
] & 0x7);
1085 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx
, raw
->curlen
);
1089 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
1090 struct drm_dp_sideband_msg_req_body
*msg
)
1094 msg
->u
.resource_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
1096 if (idx
> raw
->curlen
)
1099 memcpy(msg
->u
.resource_stat
.guid
, &raw
->msg
[idx
], 16);
1101 if (idx
> raw
->curlen
)
1104 msg
->u
.resource_stat
.available_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
1108 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx
, raw
->curlen
);
1112 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx
*raw
,
1113 struct drm_dp_sideband_msg_req_body
*msg
)
1115 memset(msg
, 0, sizeof(*msg
));
1116 msg
->req_type
= (raw
->msg
[0] & 0x7f);
1118 switch (msg
->req_type
) {
1119 case DP_CONNECTION_STATUS_NOTIFY
:
1120 return drm_dp_sideband_parse_connection_status_notify(raw
, msg
);
1121 case DP_RESOURCE_STATUS_NOTIFY
:
1122 return drm_dp_sideband_parse_resource_status_notify(raw
, msg
);
1124 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg
->req_type
,
1125 drm_dp_mst_req_type_str(msg
->req_type
));
1130 static void build_dpcd_write(struct drm_dp_sideband_msg_tx
*msg
,
1131 u8 port_num
, u32 offset
, u8 num_bytes
, u8
*bytes
)
1133 struct drm_dp_sideband_msg_req_body req
;
1135 req
.req_type
= DP_REMOTE_DPCD_WRITE
;
1136 req
.u
.dpcd_write
.port_number
= port_num
;
1137 req
.u
.dpcd_write
.dpcd_address
= offset
;
1138 req
.u
.dpcd_write
.num_bytes
= num_bytes
;
1139 req
.u
.dpcd_write
.bytes
= bytes
;
1140 drm_dp_encode_sideband_req(&req
, msg
);
1143 static void build_link_address(struct drm_dp_sideband_msg_tx
*msg
)
1145 struct drm_dp_sideband_msg_req_body req
;
1147 req
.req_type
= DP_LINK_ADDRESS
;
1148 drm_dp_encode_sideband_req(&req
, msg
);
1151 static void build_clear_payload_id_table(struct drm_dp_sideband_msg_tx
*msg
)
1153 struct drm_dp_sideband_msg_req_body req
;
1155 req
.req_type
= DP_CLEAR_PAYLOAD_ID_TABLE
;
1156 drm_dp_encode_sideband_req(&req
, msg
);
1159 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx
*msg
,
1162 struct drm_dp_sideband_msg_req_body req
;
1164 req
.req_type
= DP_ENUM_PATH_RESOURCES
;
1165 req
.u
.port_num
.port_number
= port_num
;
1166 drm_dp_encode_sideband_req(&req
, msg
);
1167 msg
->path_msg
= true;
1171 static void build_allocate_payload(struct drm_dp_sideband_msg_tx
*msg
,
1173 u8 vcpi
, uint16_t pbn
,
1174 u8 number_sdp_streams
,
1175 u8
*sdp_stream_sink
)
1177 struct drm_dp_sideband_msg_req_body req
;
1179 memset(&req
, 0, sizeof(req
));
1180 req
.req_type
= DP_ALLOCATE_PAYLOAD
;
1181 req
.u
.allocate_payload
.port_number
= port_num
;
1182 req
.u
.allocate_payload
.vcpi
= vcpi
;
1183 req
.u
.allocate_payload
.pbn
= pbn
;
1184 req
.u
.allocate_payload
.number_sdp_streams
= number_sdp_streams
;
1185 memcpy(req
.u
.allocate_payload
.sdp_stream_sink
, sdp_stream_sink
,
1186 number_sdp_streams
);
1187 drm_dp_encode_sideband_req(&req
, msg
);
1188 msg
->path_msg
= true;
1191 static void build_power_updown_phy(struct drm_dp_sideband_msg_tx
*msg
,
1192 int port_num
, bool power_up
)
1194 struct drm_dp_sideband_msg_req_body req
;
1197 req
.req_type
= DP_POWER_UP_PHY
;
1199 req
.req_type
= DP_POWER_DOWN_PHY
;
1201 req
.u
.port_num
.port_number
= port_num
;
1202 drm_dp_encode_sideband_req(&req
, msg
);
1203 msg
->path_msg
= true;
1207 build_query_stream_enc_status(struct drm_dp_sideband_msg_tx
*msg
, u8 stream_id
,
1210 struct drm_dp_sideband_msg_req_body req
;
1212 req
.req_type
= DP_QUERY_STREAM_ENC_STATUS
;
1213 req
.u
.enc_status
.stream_id
= stream_id
;
1214 memcpy(req
.u
.enc_status
.client_id
, q_id
,
1215 sizeof(req
.u
.enc_status
.client_id
));
1216 req
.u
.enc_status
.stream_event
= 0;
1217 req
.u
.enc_status
.valid_stream_event
= false;
1218 req
.u
.enc_status
.stream_behavior
= 0;
1219 req
.u
.enc_status
.valid_stream_behavior
= false;
1221 drm_dp_encode_sideband_req(&req
, msg
);
1225 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
1226 struct drm_dp_vcpi
*vcpi
)
1230 mutex_lock(&mgr
->payload_lock
);
1231 ret
= find_first_zero_bit(&mgr
->payload_mask
, mgr
->max_payloads
+ 1);
1232 if (ret
> mgr
->max_payloads
) {
1234 DRM_DEBUG_KMS("out of payload ids %d\n", ret
);
1238 vcpi_ret
= find_first_zero_bit(&mgr
->vcpi_mask
, mgr
->max_payloads
+ 1);
1239 if (vcpi_ret
> mgr
->max_payloads
) {
1241 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret
);
1245 set_bit(ret
, &mgr
->payload_mask
);
1246 set_bit(vcpi_ret
, &mgr
->vcpi_mask
);
1247 vcpi
->vcpi
= vcpi_ret
+ 1;
1248 mgr
->proposed_vcpis
[ret
- 1] = vcpi
;
1250 mutex_unlock(&mgr
->payload_lock
);
1254 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
1262 mutex_lock(&mgr
->payload_lock
);
1263 DRM_DEBUG_KMS("putting payload %d\n", vcpi
);
1264 clear_bit(vcpi
- 1, &mgr
->vcpi_mask
);
1266 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1267 if (mgr
->proposed_vcpis
[i
] &&
1268 mgr
->proposed_vcpis
[i
]->vcpi
== vcpi
) {
1269 mgr
->proposed_vcpis
[i
] = NULL
;
1270 clear_bit(i
+ 1, &mgr
->payload_mask
);
1273 mutex_unlock(&mgr
->payload_lock
);
1276 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr
*mgr
,
1277 struct drm_dp_sideband_msg_tx
*txmsg
)
1282 * All updates to txmsg->state are protected by mgr->qlock, and the two
1283 * cases we check here are terminal states. For those the barriers
1284 * provided by the wake_up/wait_event pair are enough.
1286 state
= READ_ONCE(txmsg
->state
);
1287 return (state
== DRM_DP_SIDEBAND_TX_RX
||
1288 state
== DRM_DP_SIDEBAND_TX_TIMEOUT
);
1291 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch
*mstb
,
1292 struct drm_dp_sideband_msg_tx
*txmsg
)
1294 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
1295 unsigned long wait_timeout
= msecs_to_jiffies(4000);
1296 unsigned long wait_expires
= jiffies
+ wait_timeout
;
1301 * If the driver provides a way for this, change to
1302 * poll-waiting for the MST reply interrupt if we didn't receive
1303 * it for 50 msec. This would cater for cases where the HPD
1304 * pulse signal got lost somewhere, even though the sink raised
1305 * the corresponding MST interrupt correctly. One example is the
1306 * Club 3D CAC-1557 TypeC -> DP adapter which for some reason
1307 * filters out short pulses with a duration less than ~540 usec.
1309 * The poll period is 50 msec to avoid missing an interrupt
1310 * after the sink has cleared it (after a 110msec timeout
1311 * since it raised the interrupt).
1313 ret
= wait_event_timeout(mgr
->tx_waitq
,
1314 check_txmsg_state(mgr
, txmsg
),
1315 mgr
->cbs
->poll_hpd_irq
?
1316 msecs_to_jiffies(50) :
1319 if (ret
|| !mgr
->cbs
->poll_hpd_irq
||
1320 time_after(jiffies
, wait_expires
))
1323 mgr
->cbs
->poll_hpd_irq(mgr
);
1326 mutex_lock(&mgr
->qlock
);
1328 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
) {
1333 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg
, txmsg
->state
, txmsg
->seqno
);
1335 /* dump some state */
1339 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
||
1340 txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
||
1341 txmsg
->state
== DRM_DP_SIDEBAND_TX_SENT
)
1342 list_del(&txmsg
->next
);
1345 if (unlikely(ret
== -EIO
) && drm_debug_enabled(DRM_UT_DP
)) {
1346 struct drm_printer p
= drm_debug_printer(DBG_PREFIX
);
1348 drm_dp_mst_dump_sideband_msg_tx(&p
, txmsg
);
1350 mutex_unlock(&mgr
->qlock
);
1352 drm_dp_mst_kick_tx(mgr
);
1356 static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct
, u8
*rad
)
1358 struct drm_dp_mst_branch
*mstb
;
1360 mstb
= kzalloc(sizeof(*mstb
), GFP_KERNEL
);
1366 memcpy(mstb
->rad
, rad
, lct
/ 2);
1367 INIT_LIST_HEAD(&mstb
->ports
);
1368 kref_init(&mstb
->topology_kref
);
1369 kref_init(&mstb
->malloc_kref
);
1373 static void drm_dp_free_mst_branch_device(struct kref
*kref
)
1375 struct drm_dp_mst_branch
*mstb
=
1376 container_of(kref
, struct drm_dp_mst_branch
, malloc_kref
);
1378 if (mstb
->port_parent
)
1379 drm_dp_mst_put_port_malloc(mstb
->port_parent
);
1385 * DOC: Branch device and port refcounting
1387 * Topology refcount overview
1388 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
1390 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
1391 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
1392 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
1394 * Topology refcounts are not exposed to drivers, and are handled internally
1395 * by the DP MST helpers. The helpers use them in order to prevent the
1396 * in-memory topology state from being changed in the middle of critical
1397 * operations like changing the internal state of payload allocations. This
1398 * means each branch and port will be considered to be connected to the rest
1399 * of the topology until its topology refcount reaches zero. Additionally,
1400 * for ports this means that their associated &struct drm_connector will stay
1401 * registered with userspace until the port's refcount reaches 0.
1403 * Malloc refcount overview
1404 * ~~~~~~~~~~~~~~~~~~~~~~~~
1406 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
1407 * drm_dp_mst_branch allocated even after all of its topology references have
1408 * been dropped, so that the driver or MST helpers can safely access each
1409 * branch's last known state before it was disconnected from the topology.
1410 * When the malloc refcount of a port or branch reaches 0, the memory
1411 * allocation containing the &struct drm_dp_mst_branch or &struct
1412 * drm_dp_mst_port respectively will be freed.
1414 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
1415 * to drivers. As of writing this documentation, there are no drivers that
1416 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
1417 * helpers. Exposing this API to drivers in a race-free manner would take more
1418 * tweaking of the refcounting scheme, however patches are welcome provided
1419 * there is a legitimate driver usecase for this.
1421 * Refcount relationships in a topology
1422 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1424 * Let's take a look at why the relationship between topology and malloc
1425 * refcounts is designed the way it is.
1427 * .. kernel-figure:: dp-mst/topology-figure-1.dot
1429 * An example of topology and malloc refs in a DP MST topology with two
1430 * active payloads. Topology refcount increments are indicated by solid
1431 * lines, and malloc refcount increments are indicated by dashed lines.
1432 * Each starts from the branch which incremented the refcount, and ends at
1433 * the branch to which the refcount belongs to, i.e. the arrow points the
1434 * same way as the C pointers used to reference a structure.
1436 * As you can see in the above figure, every branch increments the topology
1437 * refcount of its children, and increments the malloc refcount of its
1438 * parent. Additionally, every payload increments the malloc refcount of its
1439 * assigned port by 1.
1441 * So, what would happen if MSTB #3 from the above figure was unplugged from
1442 * the system, but the driver hadn't yet removed payload #2 from port #3? The
1443 * topology would start to look like the figure below.
1445 * .. kernel-figure:: dp-mst/topology-figure-2.dot
1447 * Ports and branch devices which have been released from memory are
1448 * colored grey, and references which have been removed are colored red.
1450 * Whenever a port or branch device's topology refcount reaches zero, it will
1451 * decrement the topology refcounts of all its children, the malloc refcount
1452 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
1453 * #4, this means they both have been disconnected from the topology and freed
1454 * from memory. But, because payload #2 is still holding a reference to port
1455 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1456 * is still accessible from memory. This also means port #3 has not yet
1457 * decremented the malloc refcount of MSTB #3, so its &struct
1458 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1459 * malloc refcount reaches 0.
1461 * This relationship is necessary because in order to release payload #2, we
1462 * need to be able to figure out the last relative of port #3 that's still
1463 * connected to the topology. In this case, we would travel up the topology as
1466 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1468 * And finally, remove payload #2 by communicating with port #2 through
1469 * sideband transactions.
1473 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1475 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1477 * Increments &drm_dp_mst_branch.malloc_kref. When
1478 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1479 * will be released and @mstb may no longer be used.
1481 * See also: drm_dp_mst_put_mstb_malloc()
1484 drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch
*mstb
)
1486 kref_get(&mstb
->malloc_kref
);
1487 DRM_DEBUG("mstb %p (%d)\n", mstb
, kref_read(&mstb
->malloc_kref
));
1491 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1493 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1495 * Decrements &drm_dp_mst_branch.malloc_kref. When
1496 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1497 * will be released and @mstb may no longer be used.
1499 * See also: drm_dp_mst_get_mstb_malloc()
1502 drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch
*mstb
)
1504 DRM_DEBUG("mstb %p (%d)\n", mstb
, kref_read(&mstb
->malloc_kref
) - 1);
1505 kref_put(&mstb
->malloc_kref
, drm_dp_free_mst_branch_device
);
1508 static void drm_dp_free_mst_port(struct kref
*kref
)
1510 struct drm_dp_mst_port
*port
=
1511 container_of(kref
, struct drm_dp_mst_port
, malloc_kref
);
1513 drm_dp_mst_put_mstb_malloc(port
->parent
);
1518 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1519 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1521 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1522 * reaches 0, the memory allocation for @port will be released and @port may
1523 * no longer be used.
1525 * Because @port could potentially be freed at any time by the DP MST helpers
1526 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1527 * function, drivers that which to make use of &struct drm_dp_mst_port should
1528 * ensure that they grab at least one main malloc reference to their MST ports
1529 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1530 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1532 * See also: drm_dp_mst_put_port_malloc()
1535 drm_dp_mst_get_port_malloc(struct drm_dp_mst_port
*port
)
1537 kref_get(&port
->malloc_kref
);
1538 DRM_DEBUG("port %p (%d)\n", port
, kref_read(&port
->malloc_kref
));
1540 EXPORT_SYMBOL(drm_dp_mst_get_port_malloc
);
1543 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1544 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1546 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1547 * reaches 0, the memory allocation for @port will be released and @port may
1548 * no longer be used.
1550 * See also: drm_dp_mst_get_port_malloc()
1553 drm_dp_mst_put_port_malloc(struct drm_dp_mst_port
*port
)
1555 DRM_DEBUG("port %p (%d)\n", port
, kref_read(&port
->malloc_kref
) - 1);
1556 kref_put(&port
->malloc_kref
, drm_dp_free_mst_port
);
1558 EXPORT_SYMBOL(drm_dp_mst_put_port_malloc
);
1560 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
1562 #define STACK_DEPTH 8
1564 static noinline
void
1565 __topology_ref_save(struct drm_dp_mst_topology_mgr
*mgr
,
1566 struct drm_dp_mst_topology_ref_history
*history
,
1567 enum drm_dp_mst_topology_ref_type type
)
1569 struct drm_dp_mst_topology_ref_entry
*entry
= NULL
;
1570 depot_stack_handle_t backtrace
;
1571 ulong stack_entries
[STACK_DEPTH
];
1575 n
= stack_trace_save(stack_entries
, ARRAY_SIZE(stack_entries
), 1);
1576 backtrace
= stack_depot_save(stack_entries
, n
, GFP_KERNEL
);
1580 /* Try to find an existing entry for this backtrace */
1581 for (i
= 0; i
< history
->len
; i
++) {
1582 if (history
->entries
[i
].backtrace
== backtrace
) {
1583 entry
= &history
->entries
[i
];
1588 /* Otherwise add one */
1590 struct drm_dp_mst_topology_ref_entry
*new;
1591 int new_len
= history
->len
+ 1;
1593 new = krealloc(history
->entries
, sizeof(*new) * new_len
,
1598 entry
= &new[history
->len
];
1599 history
->len
= new_len
;
1600 history
->entries
= new;
1602 entry
->backtrace
= backtrace
;
1607 entry
->ts_nsec
= ktime_get_ns();
1611 topology_ref_history_cmp(const void *a
, const void *b
)
1613 const struct drm_dp_mst_topology_ref_entry
*entry_a
= a
, *entry_b
= b
;
1615 if (entry_a
->ts_nsec
> entry_b
->ts_nsec
)
1617 else if (entry_a
->ts_nsec
< entry_b
->ts_nsec
)
1623 static inline const char *
1624 topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type
)
1626 if (type
== DRM_DP_MST_TOPOLOGY_REF_GET
)
1633 __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history
*history
,
1634 void *ptr
, const char *type_str
)
1636 struct drm_printer p
= drm_debug_printer(DBG_PREFIX
);
1637 char *buf
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
1646 /* First, sort the list so that it goes from oldest to newest
1649 sort(history
->entries
, history
->len
, sizeof(*history
->entries
),
1650 topology_ref_history_cmp
, NULL
);
1652 drm_printf(&p
, "%s (%p) topology count reached 0, dumping history:\n",
1655 for (i
= 0; i
< history
->len
; i
++) {
1656 const struct drm_dp_mst_topology_ref_entry
*entry
=
1657 &history
->entries
[i
];
1660 u64 ts_nsec
= entry
->ts_nsec
;
1661 u32 rem_nsec
= do_div(ts_nsec
, 1000000000);
1663 nr_entries
= stack_depot_fetch(entry
->backtrace
, &entries
);
1664 stack_trace_snprint(buf
, PAGE_SIZE
, entries
, nr_entries
, 4);
1666 drm_printf(&p
, " %d %ss (last at %5llu.%06u):\n%s",
1668 topology_ref_type_to_str(entry
->type
),
1669 ts_nsec
, rem_nsec
/ 1000, buf
);
1672 /* Now free the history, since this is the only time we expose it */
1673 kfree(history
->entries
);
1678 static __always_inline
void
1679 drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch
*mstb
)
1681 __dump_topology_ref_history(&mstb
->topology_ref_history
, mstb
,
1685 static __always_inline
void
1686 drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port
*port
)
1688 __dump_topology_ref_history(&port
->topology_ref_history
, port
,
1692 static __always_inline
void
1693 save_mstb_topology_ref(struct drm_dp_mst_branch
*mstb
,
1694 enum drm_dp_mst_topology_ref_type type
)
1696 __topology_ref_save(mstb
->mgr
, &mstb
->topology_ref_history
, type
);
1699 static __always_inline
void
1700 save_port_topology_ref(struct drm_dp_mst_port
*port
,
1701 enum drm_dp_mst_topology_ref_type type
)
1703 __topology_ref_save(port
->mgr
, &port
->topology_ref_history
, type
);
1707 topology_ref_history_lock(struct drm_dp_mst_topology_mgr
*mgr
)
1709 mutex_lock(&mgr
->topology_ref_history_lock
);
1713 topology_ref_history_unlock(struct drm_dp_mst_topology_mgr
*mgr
)
1715 mutex_unlock(&mgr
->topology_ref_history_lock
);
1719 topology_ref_history_lock(struct drm_dp_mst_topology_mgr
*mgr
) {}
1721 topology_ref_history_unlock(struct drm_dp_mst_topology_mgr
*mgr
) {}
1723 drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch
*mstb
) {}
1725 drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port
*port
) {}
1726 #define save_mstb_topology_ref(mstb, type)
1727 #define save_port_topology_ref(port, type)
1730 static void drm_dp_destroy_mst_branch_device(struct kref
*kref
)
1732 struct drm_dp_mst_branch
*mstb
=
1733 container_of(kref
, struct drm_dp_mst_branch
, topology_kref
);
1734 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
1736 drm_dp_mst_dump_mstb_topology_history(mstb
);
1738 INIT_LIST_HEAD(&mstb
->destroy_next
);
1741 * This can get called under mgr->mutex, so we need to perform the
1742 * actual destruction of the mstb in another worker
1744 mutex_lock(&mgr
->delayed_destroy_lock
);
1745 list_add(&mstb
->destroy_next
, &mgr
->destroy_branch_device_list
);
1746 mutex_unlock(&mgr
->delayed_destroy_lock
);
1747 queue_work(mgr
->delayed_destroy_wq
, &mgr
->delayed_destroy_work
);
1751 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1752 * branch device unless it's zero
1753 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1755 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1756 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1757 * reached 0). Holding a topology reference implies that a malloc reference
1758 * will be held to @mstb as long as the user holds the topology reference.
1760 * Care should be taken to ensure that the user has at least one malloc
1761 * reference to @mstb. If you already have a topology reference to @mstb, you
1762 * should use drm_dp_mst_topology_get_mstb() instead.
1765 * drm_dp_mst_topology_get_mstb()
1766 * drm_dp_mst_topology_put_mstb()
1769 * * 1: A topology reference was grabbed successfully
1770 * * 0: @port is no longer in the topology, no reference was grabbed
1772 static int __must_check
1773 drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch
*mstb
)
1777 topology_ref_history_lock(mstb
->mgr
);
1778 ret
= kref_get_unless_zero(&mstb
->topology_kref
);
1780 DRM_DEBUG("mstb %p (%d)\n",
1781 mstb
, kref_read(&mstb
->topology_kref
));
1782 save_mstb_topology_ref(mstb
, DRM_DP_MST_TOPOLOGY_REF_GET
);
1785 topology_ref_history_unlock(mstb
->mgr
);
1791 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1793 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1795 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1796 * not it's already reached 0. This is only valid to use in scenarios where
1797 * you are already guaranteed to have at least one active topology reference
1798 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1801 * drm_dp_mst_topology_try_get_mstb()
1802 * drm_dp_mst_topology_put_mstb()
1804 static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch
*mstb
)
1806 topology_ref_history_lock(mstb
->mgr
);
1808 save_mstb_topology_ref(mstb
, DRM_DP_MST_TOPOLOGY_REF_GET
);
1809 WARN_ON(kref_read(&mstb
->topology_kref
) == 0);
1810 kref_get(&mstb
->topology_kref
);
1811 DRM_DEBUG("mstb %p (%d)\n", mstb
, kref_read(&mstb
->topology_kref
));
1813 topology_ref_history_unlock(mstb
->mgr
);
1817 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1819 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1821 * Releases a topology reference from @mstb by decrementing
1822 * &drm_dp_mst_branch.topology_kref.
1825 * drm_dp_mst_topology_try_get_mstb()
1826 * drm_dp_mst_topology_get_mstb()
1829 drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch
*mstb
)
1831 topology_ref_history_lock(mstb
->mgr
);
1833 DRM_DEBUG("mstb %p (%d)\n",
1834 mstb
, kref_read(&mstb
->topology_kref
) - 1);
1835 save_mstb_topology_ref(mstb
, DRM_DP_MST_TOPOLOGY_REF_PUT
);
1837 topology_ref_history_unlock(mstb
->mgr
);
1838 kref_put(&mstb
->topology_kref
, drm_dp_destroy_mst_branch_device
);
1841 static void drm_dp_destroy_port(struct kref
*kref
)
1843 struct drm_dp_mst_port
*port
=
1844 container_of(kref
, struct drm_dp_mst_port
, topology_kref
);
1845 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
1847 drm_dp_mst_dump_port_topology_history(port
);
1849 /* There's nothing that needs locking to destroy an input port yet */
1851 drm_dp_mst_put_port_malloc(port
);
1855 kfree(port
->cached_edid
);
1858 * we can't destroy the connector here, as we might be holding the
1859 * mode_config.mutex from an EDID retrieval
1861 mutex_lock(&mgr
->delayed_destroy_lock
);
1862 list_add(&port
->next
, &mgr
->destroy_port_list
);
1863 mutex_unlock(&mgr
->delayed_destroy_lock
);
1864 queue_work(mgr
->delayed_destroy_wq
, &mgr
->delayed_destroy_work
);
1868 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1869 * port unless it's zero
1870 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1872 * Attempts to grab a topology reference to @port, if it hasn't yet been
1873 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1874 * 0). Holding a topology reference implies that a malloc reference will be
1875 * held to @port as long as the user holds the topology reference.
1877 * Care should be taken to ensure that the user has at least one malloc
1878 * reference to @port. If you already have a topology reference to @port, you
1879 * should use drm_dp_mst_topology_get_port() instead.
1882 * drm_dp_mst_topology_get_port()
1883 * drm_dp_mst_topology_put_port()
1886 * * 1: A topology reference was grabbed successfully
1887 * * 0: @port is no longer in the topology, no reference was grabbed
1889 static int __must_check
1890 drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port
*port
)
1894 topology_ref_history_lock(port
->mgr
);
1895 ret
= kref_get_unless_zero(&port
->topology_kref
);
1897 DRM_DEBUG("port %p (%d)\n",
1898 port
, kref_read(&port
->topology_kref
));
1899 save_port_topology_ref(port
, DRM_DP_MST_TOPOLOGY_REF_GET
);
1902 topology_ref_history_unlock(port
->mgr
);
1907 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1908 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1910 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1911 * not it's already reached 0. This is only valid to use in scenarios where
1912 * you are already guaranteed to have at least one active topology reference
1913 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1916 * drm_dp_mst_topology_try_get_port()
1917 * drm_dp_mst_topology_put_port()
1919 static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port
*port
)
1921 topology_ref_history_lock(port
->mgr
);
1923 WARN_ON(kref_read(&port
->topology_kref
) == 0);
1924 kref_get(&port
->topology_kref
);
1925 DRM_DEBUG("port %p (%d)\n", port
, kref_read(&port
->topology_kref
));
1926 save_port_topology_ref(port
, DRM_DP_MST_TOPOLOGY_REF_GET
);
1928 topology_ref_history_unlock(port
->mgr
);
1932 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1933 * @port: The &struct drm_dp_mst_port to release the topology reference from
1935 * Releases a topology reference from @port by decrementing
1936 * &drm_dp_mst_port.topology_kref.
1939 * drm_dp_mst_topology_try_get_port()
1940 * drm_dp_mst_topology_get_port()
1942 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port
*port
)
1944 topology_ref_history_lock(port
->mgr
);
1946 DRM_DEBUG("port %p (%d)\n",
1947 port
, kref_read(&port
->topology_kref
) - 1);
1948 save_port_topology_ref(port
, DRM_DP_MST_TOPOLOGY_REF_PUT
);
1950 topology_ref_history_unlock(port
->mgr
);
1951 kref_put(&port
->topology_kref
, drm_dp_destroy_port
);
1954 static struct drm_dp_mst_branch
*
1955 drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch
*mstb
,
1956 struct drm_dp_mst_branch
*to_find
)
1958 struct drm_dp_mst_port
*port
;
1959 struct drm_dp_mst_branch
*rmstb
;
1961 if (to_find
== mstb
)
1964 list_for_each_entry(port
, &mstb
->ports
, next
) {
1966 rmstb
= drm_dp_mst_topology_get_mstb_validated_locked(
1967 port
->mstb
, to_find
);
1975 static struct drm_dp_mst_branch
*
1976 drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr
*mgr
,
1977 struct drm_dp_mst_branch
*mstb
)
1979 struct drm_dp_mst_branch
*rmstb
= NULL
;
1981 mutex_lock(&mgr
->lock
);
1982 if (mgr
->mst_primary
) {
1983 rmstb
= drm_dp_mst_topology_get_mstb_validated_locked(
1984 mgr
->mst_primary
, mstb
);
1986 if (rmstb
&& !drm_dp_mst_topology_try_get_mstb(rmstb
))
1989 mutex_unlock(&mgr
->lock
);
1993 static struct drm_dp_mst_port
*
1994 drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch
*mstb
,
1995 struct drm_dp_mst_port
*to_find
)
1997 struct drm_dp_mst_port
*port
, *mport
;
1999 list_for_each_entry(port
, &mstb
->ports
, next
) {
2000 if (port
== to_find
)
2004 mport
= drm_dp_mst_topology_get_port_validated_locked(
2005 port
->mstb
, to_find
);
2013 static struct drm_dp_mst_port
*
2014 drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr
*mgr
,
2015 struct drm_dp_mst_port
*port
)
2017 struct drm_dp_mst_port
*rport
= NULL
;
2019 mutex_lock(&mgr
->lock
);
2020 if (mgr
->mst_primary
) {
2021 rport
= drm_dp_mst_topology_get_port_validated_locked(
2022 mgr
->mst_primary
, port
);
2024 if (rport
&& !drm_dp_mst_topology_try_get_port(rport
))
2027 mutex_unlock(&mgr
->lock
);
2031 static struct drm_dp_mst_port
*drm_dp_get_port(struct drm_dp_mst_branch
*mstb
, u8 port_num
)
2033 struct drm_dp_mst_port
*port
;
2036 list_for_each_entry(port
, &mstb
->ports
, next
) {
2037 if (port
->port_num
== port_num
) {
2038 ret
= drm_dp_mst_topology_try_get_port(port
);
2039 return ret
? port
: NULL
;
2047 * calculate a new RAD for this MST branch device
2048 * if parent has an LCT of 2 then it has 1 nibble of RAD,
2049 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
2051 static u8
drm_dp_calculate_rad(struct drm_dp_mst_port
*port
,
2054 int parent_lct
= port
->parent
->lct
;
2056 int idx
= (parent_lct
- 1) / 2;
2058 if (parent_lct
> 1) {
2059 memcpy(rad
, port
->parent
->rad
, idx
+ 1);
2060 shift
= (parent_lct
% 2) ? 4 : 0;
2064 rad
[idx
] |= port
->port_num
<< shift
;
2065 return parent_lct
+ 1;
2068 static bool drm_dp_mst_is_end_device(u8 pdt
, bool mcs
)
2071 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
2072 case DP_PEER_DEVICE_SST_SINK
:
2074 case DP_PEER_DEVICE_MST_BRANCHING
:
2075 /* For sst branch device */
2085 drm_dp_port_set_pdt(struct drm_dp_mst_port
*port
, u8 new_pdt
,
2088 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
2089 struct drm_dp_mst_branch
*mstb
;
2093 if (port
->pdt
== new_pdt
&& port
->mcs
== new_mcs
)
2096 /* Teardown the old pdt, if there is one */
2097 if (port
->pdt
!= DP_PEER_DEVICE_NONE
) {
2098 if (drm_dp_mst_is_end_device(port
->pdt
, port
->mcs
)) {
2100 * If the new PDT would also have an i2c bus,
2101 * don't bother with reregistering it
2103 if (new_pdt
!= DP_PEER_DEVICE_NONE
&&
2104 drm_dp_mst_is_end_device(new_pdt
, new_mcs
)) {
2105 port
->pdt
= new_pdt
;
2106 port
->mcs
= new_mcs
;
2110 /* remove i2c over sideband */
2111 drm_dp_mst_unregister_i2c_bus(port
);
2113 mutex_lock(&mgr
->lock
);
2114 drm_dp_mst_topology_put_mstb(port
->mstb
);
2116 mutex_unlock(&mgr
->lock
);
2120 port
->pdt
= new_pdt
;
2121 port
->mcs
= new_mcs
;
2123 if (port
->pdt
!= DP_PEER_DEVICE_NONE
) {
2124 if (drm_dp_mst_is_end_device(port
->pdt
, port
->mcs
)) {
2125 /* add i2c over sideband */
2126 ret
= drm_dp_mst_register_i2c_bus(port
);
2128 lct
= drm_dp_calculate_rad(port
, rad
);
2129 mstb
= drm_dp_add_mst_branch_device(lct
, rad
);
2132 DRM_ERROR("Failed to create MSTB for port %p",
2137 mutex_lock(&mgr
->lock
);
2139 mstb
->mgr
= port
->mgr
;
2140 mstb
->port_parent
= port
;
2143 * Make sure this port's memory allocation stays
2144 * around until its child MSTB releases it
2146 drm_dp_mst_get_port_malloc(port
);
2147 mutex_unlock(&mgr
->lock
);
2149 /* And make sure we send a link address for this */
2156 port
->pdt
= DP_PEER_DEVICE_NONE
;
2161 * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
2162 * @aux: Fake sideband AUX CH
2163 * @offset: address of the (first) register to read
2164 * @buffer: buffer to store the register values
2165 * @size: number of bytes in @buffer
2167 * Performs the same functionality for remote devices via
2168 * sideband messaging as drm_dp_dpcd_read() does for local
2169 * devices via actual AUX CH.
2171 * Return: Number of bytes read, or negative error code on failure.
2173 ssize_t
drm_dp_mst_dpcd_read(struct drm_dp_aux
*aux
,
2174 unsigned int offset
, void *buffer
, size_t size
)
2176 struct drm_dp_mst_port
*port
= container_of(aux
, struct drm_dp_mst_port
,
2179 return drm_dp_send_dpcd_read(port
->mgr
, port
,
2180 offset
, size
, buffer
);
2184 * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
2185 * @aux: Fake sideband AUX CH
2186 * @offset: address of the (first) register to write
2187 * @buffer: buffer containing the values to write
2188 * @size: number of bytes in @buffer
2190 * Performs the same functionality for remote devices via
2191 * sideband messaging as drm_dp_dpcd_write() does for local
2192 * devices via actual AUX CH.
2194 * Return: number of bytes written on success, negative error code on failure.
2196 ssize_t
drm_dp_mst_dpcd_write(struct drm_dp_aux
*aux
,
2197 unsigned int offset
, void *buffer
, size_t size
)
2199 struct drm_dp_mst_port
*port
= container_of(aux
, struct drm_dp_mst_port
,
2202 return drm_dp_send_dpcd_write(port
->mgr
, port
,
2203 offset
, size
, buffer
);
2206 static int drm_dp_check_mstb_guid(struct drm_dp_mst_branch
*mstb
, u8
*guid
)
2210 memcpy(mstb
->guid
, guid
, 16);
2212 if (!drm_dp_validate_guid(mstb
->mgr
, mstb
->guid
)) {
2213 if (mstb
->port_parent
) {
2214 ret
= drm_dp_send_dpcd_write(mstb
->mgr
,
2216 DP_GUID
, 16, mstb
->guid
);
2218 ret
= drm_dp_dpcd_write(mstb
->mgr
->aux
,
2219 DP_GUID
, mstb
->guid
, 16);
2223 if (ret
< 16 && ret
> 0)
2226 return ret
== 16 ? 0 : ret
;
2229 static void build_mst_prop_path(const struct drm_dp_mst_branch
*mstb
,
2232 size_t proppath_size
)
2237 snprintf(proppath
, proppath_size
, "mst:%d", mstb
->mgr
->conn_base_id
);
2238 for (i
= 0; i
< (mstb
->lct
- 1); i
++) {
2239 int shift
= (i
% 2) ? 0 : 4;
2240 int port_num
= (mstb
->rad
[i
/ 2] >> shift
) & 0xf;
2242 snprintf(temp
, sizeof(temp
), "-%d", port_num
);
2243 strlcat(proppath
, temp
, proppath_size
);
2245 snprintf(temp
, sizeof(temp
), "-%d", pnum
);
2246 strlcat(proppath
, temp
, proppath_size
);
2250 * drm_dp_mst_connector_late_register() - Late MST connector registration
2251 * @connector: The MST connector
2252 * @port: The MST port for this connector
2254 * Helper to register the remote aux device for this MST port. Drivers should
2255 * call this from their mst connector's late_register hook to enable MST aux
2258 * Return: 0 on success, negative error code on failure.
2260 int drm_dp_mst_connector_late_register(struct drm_connector
*connector
,
2261 struct drm_dp_mst_port
*port
)
2263 DRM_DEBUG_KMS("registering %s remote bus for %s\n",
2264 port
->aux
.name
, connector
->kdev
->kobj
.name
);
2266 port
->aux
.dev
= connector
->kdev
;
2267 return drm_dp_aux_register_devnode(&port
->aux
);
2269 EXPORT_SYMBOL(drm_dp_mst_connector_late_register
);
2272 * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
2273 * @connector: The MST connector
2274 * @port: The MST port for this connector
2276 * Helper to unregister the remote aux device for this MST port, registered by
2277 * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
2278 * connector's early_unregister hook.
2280 void drm_dp_mst_connector_early_unregister(struct drm_connector
*connector
,
2281 struct drm_dp_mst_port
*port
)
2283 DRM_DEBUG_KMS("unregistering %s remote bus for %s\n",
2284 port
->aux
.name
, connector
->kdev
->kobj
.name
);
2285 drm_dp_aux_unregister_devnode(&port
->aux
);
2287 EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister
);
2290 drm_dp_mst_port_add_connector(struct drm_dp_mst_branch
*mstb
,
2291 struct drm_dp_mst_port
*port
)
2293 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
2297 build_mst_prop_path(mstb
, port
->port_num
, proppath
, sizeof(proppath
));
2298 port
->connector
= mgr
->cbs
->add_connector(mgr
, port
, proppath
);
2299 if (!port
->connector
) {
2304 if (port
->pdt
!= DP_PEER_DEVICE_NONE
&&
2305 drm_dp_mst_is_end_device(port
->pdt
, port
->mcs
)) {
2306 port
->cached_edid
= drm_get_edid(port
->connector
,
2308 drm_connector_set_tile_property(port
->connector
);
2311 drm_connector_register(port
->connector
);
2315 DRM_ERROR("Failed to create connector for port %p: %d\n", port
, ret
);
2319 * Drop a topology reference, and unlink the port from the in-memory topology
2323 drm_dp_mst_topology_unlink_port(struct drm_dp_mst_topology_mgr
*mgr
,
2324 struct drm_dp_mst_port
*port
)
2326 mutex_lock(&mgr
->lock
);
2327 port
->parent
->num_ports
--;
2328 list_del(&port
->next
);
2329 mutex_unlock(&mgr
->lock
);
2330 drm_dp_mst_topology_put_port(port
);
2333 static struct drm_dp_mst_port
*
2334 drm_dp_mst_add_port(struct drm_device
*dev
,
2335 struct drm_dp_mst_topology_mgr
*mgr
,
2336 struct drm_dp_mst_branch
*mstb
, u8 port_number
)
2338 struct drm_dp_mst_port
*port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
2343 kref_init(&port
->topology_kref
);
2344 kref_init(&port
->malloc_kref
);
2345 port
->parent
= mstb
;
2346 port
->port_num
= port_number
;
2348 port
->aux
.name
= "DPMST";
2349 port
->aux
.dev
= dev
->dev
;
2350 port
->aux
.is_remote
= true;
2352 /* initialize the MST downstream port's AUX crc work queue */
2353 drm_dp_remote_aux_init(&port
->aux
);
2356 * Make sure the memory allocation for our parent branch stays
2357 * around until our own memory allocation is released
2359 drm_dp_mst_get_mstb_malloc(mstb
);
2365 drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch
*mstb
,
2366 struct drm_device
*dev
,
2367 struct drm_dp_link_addr_reply_port
*port_msg
)
2369 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
2370 struct drm_dp_mst_port
*port
;
2371 int old_ddps
= 0, ret
;
2372 u8 new_pdt
= DP_PEER_DEVICE_NONE
;
2374 bool created
= false, send_link_addr
= false, changed
= false;
2376 port
= drm_dp_get_port(mstb
, port_msg
->port_number
);
2378 port
= drm_dp_mst_add_port(dev
, mgr
, mstb
,
2379 port_msg
->port_number
);
2384 } else if (!port
->input
&& port_msg
->input_port
&& port
->connector
) {
2385 /* Since port->connector can't be changed here, we create a
2386 * new port if input_port changes from 0 to 1
2388 drm_dp_mst_topology_unlink_port(mgr
, port
);
2389 drm_dp_mst_topology_put_port(port
);
2390 port
= drm_dp_mst_add_port(dev
, mgr
, mstb
,
2391 port_msg
->port_number
);
2396 } else if (port
->input
&& !port_msg
->input_port
) {
2398 } else if (port
->connector
) {
2399 /* We're updating a port that's exposed to userspace, so do it
2402 drm_modeset_lock(&mgr
->base
.lock
, NULL
);
2404 old_ddps
= port
->ddps
;
2405 changed
= port
->ddps
!= port_msg
->ddps
||
2407 (port
->ldps
!= port_msg
->legacy_device_plug_status
||
2408 port
->dpcd_rev
!= port_msg
->dpcd_revision
||
2409 port
->mcs
!= port_msg
->mcs
||
2410 port
->pdt
!= port_msg
->peer_device_type
||
2411 port
->num_sdp_stream_sinks
!=
2412 port_msg
->num_sdp_stream_sinks
));
2415 port
->input
= port_msg
->input_port
;
2417 new_pdt
= port_msg
->peer_device_type
;
2418 new_mcs
= port_msg
->mcs
;
2419 port
->ddps
= port_msg
->ddps
;
2420 port
->ldps
= port_msg
->legacy_device_plug_status
;
2421 port
->dpcd_rev
= port_msg
->dpcd_revision
;
2422 port
->num_sdp_streams
= port_msg
->num_sdp_streams
;
2423 port
->num_sdp_stream_sinks
= port_msg
->num_sdp_stream_sinks
;
2425 /* manage mstb port lists with mgr lock - take a reference
2428 mutex_lock(&mgr
->lock
);
2429 drm_dp_mst_topology_get_port(port
);
2430 list_add(&port
->next
, &mstb
->ports
);
2432 mutex_unlock(&mgr
->lock
);
2436 * Reprobe PBN caps on both hotplug, and when re-probing the link
2437 * for our parent mstb
2439 if (old_ddps
!= port
->ddps
|| !created
) {
2440 if (port
->ddps
&& !port
->input
) {
2441 ret
= drm_dp_send_enum_path_resources(mgr
, mstb
,
2450 ret
= drm_dp_port_set_pdt(port
, new_pdt
, new_mcs
);
2452 send_link_addr
= true;
2453 } else if (ret
< 0) {
2454 DRM_ERROR("Failed to change PDT on port %p: %d\n",
2460 * If this port wasn't just created, then we're reprobing because
2461 * we're coming out of suspend. In this case, always resend the link
2462 * address if there's an MSTB on this port
2464 if (!created
&& port
->pdt
== DP_PEER_DEVICE_MST_BRANCHING
&&
2466 send_link_addr
= true;
2468 if (port
->connector
)
2469 drm_modeset_unlock(&mgr
->base
.lock
);
2470 else if (!port
->input
)
2471 drm_dp_mst_port_add_connector(mstb
, port
);
2473 if (send_link_addr
&& port
->mstb
) {
2474 ret
= drm_dp_send_link_address(mgr
, port
->mstb
);
2475 if (ret
== 1) /* MSTB below us changed */
2481 /* put reference to this port */
2482 drm_dp_mst_topology_put_port(port
);
2486 drm_dp_mst_topology_unlink_port(mgr
, port
);
2487 if (port
->connector
)
2488 drm_modeset_unlock(&mgr
->base
.lock
);
2490 drm_dp_mst_topology_put_port(port
);
2495 drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch
*mstb
,
2496 struct drm_dp_connection_status_notify
*conn_stat
)
2498 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
2499 struct drm_dp_mst_port
*port
;
2500 int old_ddps
, old_input
, ret
, i
;
2503 bool dowork
= false, create_connector
= false;
2505 port
= drm_dp_get_port(mstb
, conn_stat
->port_number
);
2509 if (port
->connector
) {
2510 if (!port
->input
&& conn_stat
->input_port
) {
2512 * We can't remove a connector from an already exposed
2513 * port, so just throw the port out and make sure we
2514 * reprobe the link address of it's parent MSTB
2516 drm_dp_mst_topology_unlink_port(mgr
, port
);
2517 mstb
->link_address_sent
= false;
2522 /* Locking is only needed if the port's exposed to userspace */
2523 drm_modeset_lock(&mgr
->base
.lock
, NULL
);
2524 } else if (port
->input
&& !conn_stat
->input_port
) {
2525 create_connector
= true;
2526 /* Reprobe link address so we get num_sdp_streams */
2527 mstb
->link_address_sent
= false;
2531 old_ddps
= port
->ddps
;
2532 old_input
= port
->input
;
2533 port
->input
= conn_stat
->input_port
;
2534 port
->ldps
= conn_stat
->legacy_device_plug_status
;
2535 port
->ddps
= conn_stat
->displayport_device_plug_status
;
2537 if (old_ddps
!= port
->ddps
) {
2538 if (port
->ddps
&& !port
->input
)
2539 drm_dp_send_enum_path_resources(mgr
, mstb
, port
);
2544 new_pdt
= port
->input
? DP_PEER_DEVICE_NONE
: conn_stat
->peer_device_type
;
2545 new_mcs
= conn_stat
->message_capability_status
;
2546 ret
= drm_dp_port_set_pdt(port
, new_pdt
, new_mcs
);
2549 } else if (ret
< 0) {
2550 DRM_ERROR("Failed to change PDT for port %p: %d\n",
2555 if (!old_input
&& old_ddps
!= port
->ddps
&& !port
->ddps
) {
2556 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
2557 struct drm_dp_vcpi
*vcpi
= mgr
->proposed_vcpis
[i
];
2558 struct drm_dp_mst_port
*port_validated
;
2564 container_of(vcpi
, struct drm_dp_mst_port
, vcpi
);
2566 drm_dp_mst_topology_get_port_validated(mgr
, port_validated
);
2567 if (!port_validated
) {
2568 mutex_lock(&mgr
->payload_lock
);
2569 vcpi
->num_slots
= 0;
2570 mutex_unlock(&mgr
->payload_lock
);
2572 drm_dp_mst_topology_put_port(port_validated
);
2577 if (port
->connector
)
2578 drm_modeset_unlock(&mgr
->base
.lock
);
2579 else if (create_connector
)
2580 drm_dp_mst_port_add_connector(mstb
, port
);
2583 drm_dp_mst_topology_put_port(port
);
2585 queue_work(system_long_wq
, &mstb
->mgr
->work
);
2588 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr
*mgr
,
2591 struct drm_dp_mst_branch
*mstb
;
2592 struct drm_dp_mst_port
*port
;
2594 /* find the port by iterating down */
2596 mutex_lock(&mgr
->lock
);
2597 mstb
= mgr
->mst_primary
;
2602 for (i
= 0; i
< lct
- 1; i
++) {
2603 int shift
= (i
% 2) ? 0 : 4;
2604 int port_num
= (rad
[i
/ 2] >> shift
) & 0xf;
2606 list_for_each_entry(port
, &mstb
->ports
, next
) {
2607 if (port
->port_num
== port_num
) {
2610 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct
, rad
[0]);
2618 ret
= drm_dp_mst_topology_try_get_mstb(mstb
);
2622 mutex_unlock(&mgr
->lock
);
2626 static struct drm_dp_mst_branch
*get_mst_branch_device_by_guid_helper(
2627 struct drm_dp_mst_branch
*mstb
,
2628 const uint8_t *guid
)
2630 struct drm_dp_mst_branch
*found_mstb
;
2631 struct drm_dp_mst_port
*port
;
2633 if (memcmp(mstb
->guid
, guid
, 16) == 0)
2637 list_for_each_entry(port
, &mstb
->ports
, next
) {
2641 found_mstb
= get_mst_branch_device_by_guid_helper(port
->mstb
, guid
);
2650 static struct drm_dp_mst_branch
*
2651 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr
*mgr
,
2652 const uint8_t *guid
)
2654 struct drm_dp_mst_branch
*mstb
;
2657 /* find the port by iterating down */
2658 mutex_lock(&mgr
->lock
);
2660 mstb
= get_mst_branch_device_by_guid_helper(mgr
->mst_primary
, guid
);
2662 ret
= drm_dp_mst_topology_try_get_mstb(mstb
);
2667 mutex_unlock(&mgr
->lock
);
2671 static int drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
2672 struct drm_dp_mst_branch
*mstb
)
2674 struct drm_dp_mst_port
*port
;
2676 bool changed
= false;
2678 if (!mstb
->link_address_sent
) {
2679 ret
= drm_dp_send_link_address(mgr
, mstb
);
2686 list_for_each_entry(port
, &mstb
->ports
, next
) {
2687 struct drm_dp_mst_branch
*mstb_child
= NULL
;
2689 if (port
->input
|| !port
->ddps
)
2693 mstb_child
= drm_dp_mst_topology_get_mstb_validated(
2697 ret
= drm_dp_check_and_send_link_address(mgr
,
2699 drm_dp_mst_topology_put_mstb(mstb_child
);
2710 static void drm_dp_mst_link_probe_work(struct work_struct
*work
)
2712 struct drm_dp_mst_topology_mgr
*mgr
=
2713 container_of(work
, struct drm_dp_mst_topology_mgr
, work
);
2714 struct drm_device
*dev
= mgr
->dev
;
2715 struct drm_dp_mst_branch
*mstb
;
2717 bool clear_payload_id_table
;
2719 mutex_lock(&mgr
->probe_lock
);
2721 mutex_lock(&mgr
->lock
);
2722 clear_payload_id_table
= !mgr
->payload_id_table_cleared
;
2723 mgr
->payload_id_table_cleared
= true;
2725 mstb
= mgr
->mst_primary
;
2727 ret
= drm_dp_mst_topology_try_get_mstb(mstb
);
2731 mutex_unlock(&mgr
->lock
);
2733 mutex_unlock(&mgr
->probe_lock
);
2738 * Certain branch devices seem to incorrectly report an available_pbn
2739 * of 0 on downstream sinks, even after clearing the
2740 * DP_PAYLOAD_ALLOCATE_* registers in
2741 * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C
2742 * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make
2743 * things work again.
2745 if (clear_payload_id_table
) {
2746 DRM_DEBUG_KMS("Clearing payload ID table\n");
2747 drm_dp_send_clear_payload_id_table(mgr
, mstb
);
2750 ret
= drm_dp_check_and_send_link_address(mgr
, mstb
);
2751 drm_dp_mst_topology_put_mstb(mstb
);
2753 mutex_unlock(&mgr
->probe_lock
);
2755 drm_kms_helper_hotplug_event(dev
);
2758 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
2763 if (memchr_inv(guid
, 0, 16))
2766 salt
= get_jiffies_64();
2768 memcpy(&guid
[0], &salt
, sizeof(u64
));
2769 memcpy(&guid
[8], &salt
, sizeof(u64
));
2774 static void build_dpcd_read(struct drm_dp_sideband_msg_tx
*msg
,
2775 u8 port_num
, u32 offset
, u8 num_bytes
)
2777 struct drm_dp_sideband_msg_req_body req
;
2779 req
.req_type
= DP_REMOTE_DPCD_READ
;
2780 req
.u
.dpcd_read
.port_number
= port_num
;
2781 req
.u
.dpcd_read
.dpcd_address
= offset
;
2782 req
.u
.dpcd_read
.num_bytes
= num_bytes
;
2783 drm_dp_encode_sideband_req(&req
, msg
);
2786 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr
*mgr
,
2787 bool up
, u8
*msg
, int len
)
2790 int regbase
= up
? DP_SIDEBAND_MSG_UP_REP_BASE
: DP_SIDEBAND_MSG_DOWN_REQ_BASE
;
2791 int tosend
, total
, offset
;
2798 tosend
= min3(mgr
->max_dpcd_transaction_bytes
, 16, total
);
2800 ret
= drm_dp_dpcd_write(mgr
->aux
, regbase
+ offset
,
2803 if (ret
!= tosend
) {
2804 if (ret
== -EIO
&& retries
< 5) {
2808 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend
, ret
);
2814 } while (total
> 0);
2818 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr
*hdr
,
2819 struct drm_dp_sideband_msg_tx
*txmsg
)
2821 struct drm_dp_mst_branch
*mstb
= txmsg
->dst
;
2824 req_type
= txmsg
->msg
[0] & 0x7f;
2825 if (req_type
== DP_CONNECTION_STATUS_NOTIFY
||
2826 req_type
== DP_RESOURCE_STATUS_NOTIFY
)
2830 hdr
->path_msg
= txmsg
->path_msg
;
2831 hdr
->lct
= mstb
->lct
;
2832 hdr
->lcr
= mstb
->lct
- 1;
2834 memcpy(hdr
->rad
, mstb
->rad
, mstb
->lct
/ 2);
2839 * process a single block of the next message in the sideband queue
2841 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
2842 struct drm_dp_sideband_msg_tx
*txmsg
,
2846 struct drm_dp_sideband_msg_hdr hdr
;
2847 int len
, space
, idx
, tosend
;
2850 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_SENT
)
2853 memset(&hdr
, 0, sizeof(struct drm_dp_sideband_msg_hdr
));
2855 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
)
2856 txmsg
->state
= DRM_DP_SIDEBAND_TX_START_SEND
;
2858 /* make hdr from dst mst */
2859 ret
= set_hdr_from_dst_qlock(&hdr
, txmsg
);
2863 /* amount left to send in this message */
2864 len
= txmsg
->cur_len
- txmsg
->cur_offset
;
2866 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
2867 space
= 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr
);
2869 tosend
= min(len
, space
);
2870 if (len
== txmsg
->cur_len
)
2876 hdr
.msg_len
= tosend
+ 1;
2877 drm_dp_encode_sideband_msg_hdr(&hdr
, chunk
, &idx
);
2878 memcpy(&chunk
[idx
], &txmsg
->msg
[txmsg
->cur_offset
], tosend
);
2879 /* add crc at end */
2880 drm_dp_crc_sideband_chunk_req(&chunk
[idx
], tosend
);
2883 ret
= drm_dp_send_sideband_msg(mgr
, up
, chunk
, idx
);
2884 if (unlikely(ret
) && drm_debug_enabled(DRM_UT_DP
)) {
2885 struct drm_printer p
= drm_debug_printer(DBG_PREFIX
);
2887 drm_printf(&p
, "sideband msg failed to send\n");
2888 drm_dp_mst_dump_sideband_msg_tx(&p
, txmsg
);
2892 txmsg
->cur_offset
+= tosend
;
2893 if (txmsg
->cur_offset
== txmsg
->cur_len
) {
2894 txmsg
->state
= DRM_DP_SIDEBAND_TX_SENT
;
2900 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
)
2902 struct drm_dp_sideband_msg_tx
*txmsg
;
2905 WARN_ON(!mutex_is_locked(&mgr
->qlock
));
2907 /* construct a chunk from the first msg in the tx_msg queue */
2908 if (list_empty(&mgr
->tx_msg_downq
))
2911 txmsg
= list_first_entry(&mgr
->tx_msg_downq
,
2912 struct drm_dp_sideband_msg_tx
, next
);
2913 ret
= process_single_tx_qlock(mgr
, txmsg
, false);
2915 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
2916 list_del(&txmsg
->next
);
2917 txmsg
->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
2918 wake_up_all(&mgr
->tx_waitq
);
2922 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr
*mgr
,
2923 struct drm_dp_sideband_msg_tx
*txmsg
)
2925 mutex_lock(&mgr
->qlock
);
2926 list_add_tail(&txmsg
->next
, &mgr
->tx_msg_downq
);
2928 if (drm_debug_enabled(DRM_UT_DP
)) {
2929 struct drm_printer p
= drm_debug_printer(DBG_PREFIX
);
2931 drm_dp_mst_dump_sideband_msg_tx(&p
, txmsg
);
2934 if (list_is_singular(&mgr
->tx_msg_downq
))
2935 process_single_down_tx_qlock(mgr
);
2936 mutex_unlock(&mgr
->qlock
);
2940 drm_dp_dump_link_address(struct drm_dp_link_address_ack_reply
*reply
)
2942 struct drm_dp_link_addr_reply_port
*port_reply
;
2945 for (i
= 0; i
< reply
->nports
; i
++) {
2946 port_reply
= &reply
->ports
[i
];
2947 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",
2949 port_reply
->input_port
,
2950 port_reply
->peer_device_type
,
2951 port_reply
->port_number
,
2952 port_reply
->dpcd_revision
,
2955 port_reply
->legacy_device_plug_status
,
2956 port_reply
->num_sdp_streams
,
2957 port_reply
->num_sdp_stream_sinks
);
2961 static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
2962 struct drm_dp_mst_branch
*mstb
)
2964 struct drm_dp_sideband_msg_tx
*txmsg
;
2965 struct drm_dp_link_address_ack_reply
*reply
;
2966 struct drm_dp_mst_port
*port
, *tmp
;
2967 int i
, ret
, port_mask
= 0;
2968 bool changed
= false;
2970 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
2975 build_link_address(txmsg
);
2977 mstb
->link_address_sent
= true;
2978 drm_dp_queue_down_tx(mgr
, txmsg
);
2980 /* FIXME: Actually do some real error handling here */
2981 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
2983 DRM_ERROR("Sending link address failed with %d\n", ret
);
2986 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
) {
2987 DRM_ERROR("link address NAK received\n");
2992 reply
= &txmsg
->reply
.u
.link_addr
;
2993 DRM_DEBUG_KMS("link address reply: %d\n", reply
->nports
);
2994 drm_dp_dump_link_address(reply
);
2996 ret
= drm_dp_check_mstb_guid(mstb
, reply
->guid
);
3000 drm_dp_mst_rad_to_str(mstb
->rad
, mstb
->lct
, buf
, sizeof(buf
));
3001 DRM_ERROR("GUID check on %s failed: %d\n",
3006 for (i
= 0; i
< reply
->nports
; i
++) {
3007 port_mask
|= BIT(reply
->ports
[i
].port_number
);
3008 ret
= drm_dp_mst_handle_link_address_port(mstb
, mgr
->dev
,
3016 /* Prune any ports that are currently a part of mstb in our in-memory
3017 * topology, but were not seen in this link address. Usually this
3018 * means that they were removed while the topology was out of sync,
3019 * e.g. during suspend/resume
3021 mutex_lock(&mgr
->lock
);
3022 list_for_each_entry_safe(port
, tmp
, &mstb
->ports
, next
) {
3023 if (port_mask
& BIT(port
->port_num
))
3026 DRM_DEBUG_KMS("port %d was not in link address, removing\n",
3028 list_del(&port
->next
);
3029 drm_dp_mst_topology_put_port(port
);
3032 mutex_unlock(&mgr
->lock
);
3036 mstb
->link_address_sent
= false;
3038 return ret
< 0 ? ret
: changed
;
3042 drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr
*mgr
,
3043 struct drm_dp_mst_branch
*mstb
)
3045 struct drm_dp_sideband_msg_tx
*txmsg
;
3048 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3053 build_clear_payload_id_table(txmsg
);
3055 drm_dp_queue_down_tx(mgr
, txmsg
);
3057 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3058 if (ret
> 0 && txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
)
3059 DRM_DEBUG_KMS("clear payload table id nak received\n");
3065 drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
3066 struct drm_dp_mst_branch
*mstb
,
3067 struct drm_dp_mst_port
*port
)
3069 struct drm_dp_enum_path_resources_ack_reply
*path_res
;
3070 struct drm_dp_sideband_msg_tx
*txmsg
;
3073 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3078 build_enum_path_resources(txmsg
, port
->port_num
);
3080 drm_dp_queue_down_tx(mgr
, txmsg
);
3082 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3085 path_res
= &txmsg
->reply
.u
.path_resources
;
3087 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
) {
3088 DRM_DEBUG_KMS("enum path resources nak received\n");
3090 if (port
->port_num
!= path_res
->port_number
)
3091 DRM_ERROR("got incorrect port in response\n");
3093 DRM_DEBUG_KMS("enum path resources %d: %d %d\n",
3094 path_res
->port_number
,
3095 path_res
->full_payload_bw_number
,
3096 path_res
->avail_payload_bw_number
);
3099 * If something changed, make sure we send a
3102 if (port
->full_pbn
!= path_res
->full_payload_bw_number
||
3103 port
->fec_capable
!= path_res
->fec_capable
)
3106 port
->full_pbn
= path_res
->full_payload_bw_number
;
3107 port
->fec_capable
= path_res
->fec_capable
;
3115 static struct drm_dp_mst_port
*drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch
*mstb
)
3117 if (!mstb
->port_parent
)
3120 if (mstb
->port_parent
->mstb
!= mstb
)
3121 return mstb
->port_parent
;
3123 return drm_dp_get_last_connected_port_to_mstb(mstb
->port_parent
->parent
);
3127 * Searches upwards in the topology starting from mstb to try to find the
3128 * closest available parent of mstb that's still connected to the rest of the
3129 * topology. This can be used in order to perform operations like releasing
3130 * payloads, where the branch device which owned the payload may no longer be
3131 * around and thus would require that the payload on the last living relative
3134 static struct drm_dp_mst_branch
*
3135 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr
*mgr
,
3136 struct drm_dp_mst_branch
*mstb
,
3139 struct drm_dp_mst_branch
*rmstb
= NULL
;
3140 struct drm_dp_mst_port
*found_port
;
3142 mutex_lock(&mgr
->lock
);
3143 if (!mgr
->mst_primary
)
3147 found_port
= drm_dp_get_last_connected_port_to_mstb(mstb
);
3151 if (drm_dp_mst_topology_try_get_mstb(found_port
->parent
)) {
3152 rmstb
= found_port
->parent
;
3153 *port_num
= found_port
->port_num
;
3155 /* Search again, starting from this parent */
3156 mstb
= found_port
->parent
;
3160 mutex_unlock(&mgr
->lock
);
3164 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr
*mgr
,
3165 struct drm_dp_mst_port
*port
,
3169 struct drm_dp_sideband_msg_tx
*txmsg
;
3170 struct drm_dp_mst_branch
*mstb
;
3172 u8 sinks
[DRM_DP_MAX_SDP_STREAMS
];
3175 port_num
= port
->port_num
;
3176 mstb
= drm_dp_mst_topology_get_mstb_validated(mgr
, port
->parent
);
3178 mstb
= drm_dp_get_last_connected_port_and_mstb(mgr
,
3186 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3192 for (i
= 0; i
< port
->num_sdp_streams
; i
++)
3196 build_allocate_payload(txmsg
, port_num
,
3198 pbn
, port
->num_sdp_streams
, sinks
);
3200 drm_dp_queue_down_tx(mgr
, txmsg
);
3203 * FIXME: there is a small chance that between getting the last
3204 * connected mstb and sending the payload message, the last connected
3205 * mstb could also be removed from the topology. In the future, this
3206 * needs to be fixed by restarting the
3207 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
3208 * timeout if the topology is still connected to the system.
3210 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3212 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
)
3219 drm_dp_mst_topology_put_mstb(mstb
);
3223 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr
*mgr
,
3224 struct drm_dp_mst_port
*port
, bool power_up
)
3226 struct drm_dp_sideband_msg_tx
*txmsg
;
3229 port
= drm_dp_mst_topology_get_port_validated(mgr
, port
);
3233 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3235 drm_dp_mst_topology_put_port(port
);
3239 txmsg
->dst
= port
->parent
;
3240 build_power_updown_phy(txmsg
, port
->port_num
, power_up
);
3241 drm_dp_queue_down_tx(mgr
, txmsg
);
3243 ret
= drm_dp_mst_wait_tx_reply(port
->parent
, txmsg
);
3245 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
)
3251 drm_dp_mst_topology_put_port(port
);
3255 EXPORT_SYMBOL(drm_dp_send_power_updown_phy
);
3257 int drm_dp_send_query_stream_enc_status(struct drm_dp_mst_topology_mgr
*mgr
,
3258 struct drm_dp_mst_port
*port
,
3259 struct drm_dp_query_stream_enc_status_ack_reply
*status
)
3261 struct drm_dp_sideband_msg_tx
*txmsg
;
3265 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3269 port
= drm_dp_mst_topology_get_port_validated(mgr
, port
);
3275 get_random_bytes(nonce
, sizeof(nonce
));
3278 * "Source device targets the QUERY_STREAM_ENCRYPTION_STATUS message
3279 * transaction at the MST Branch device directly connected to the
3282 txmsg
->dst
= mgr
->mst_primary
;
3284 build_query_stream_enc_status(txmsg
, port
->vcpi
.vcpi
, nonce
);
3286 drm_dp_queue_down_tx(mgr
, txmsg
);
3288 ret
= drm_dp_mst_wait_tx_reply(mgr
->mst_primary
, txmsg
);
3291 } else if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
) {
3292 drm_dbg_kms(mgr
->dev
, "query encryption status nak received\n");
3298 memcpy(status
, &txmsg
->reply
.u
.enc_status
, sizeof(*status
));
3301 drm_dp_mst_topology_put_port(port
);
3306 EXPORT_SYMBOL(drm_dp_send_query_stream_enc_status
);
3308 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
3310 struct drm_dp_payload
*payload
)
3314 ret
= drm_dp_dpcd_write_payload(mgr
, id
, payload
);
3316 payload
->payload_state
= 0;
3319 payload
->payload_state
= DP_PAYLOAD_LOCAL
;
3323 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
3324 struct drm_dp_mst_port
*port
,
3326 struct drm_dp_payload
*payload
)
3330 ret
= drm_dp_payload_send_msg(mgr
, port
, id
, port
->vcpi
.pbn
);
3333 payload
->payload_state
= DP_PAYLOAD_REMOTE
;
3337 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
3338 struct drm_dp_mst_port
*port
,
3340 struct drm_dp_payload
*payload
)
3342 DRM_DEBUG_KMS("\n");
3343 /* it's okay for these to fail */
3345 drm_dp_payload_send_msg(mgr
, port
, id
, 0);
3348 drm_dp_dpcd_write_payload(mgr
, id
, payload
);
3349 payload
->payload_state
= DP_PAYLOAD_DELETE_LOCAL
;
3353 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
3355 struct drm_dp_payload
*payload
)
3357 payload
->payload_state
= 0;
3362 * drm_dp_update_payload_part1() - Execute payload update part 1
3363 * @mgr: manager to use.
3365 * This iterates over all proposed virtual channels, and tries to
3366 * allocate space in the link for them. For 0->slots transitions,
3367 * this step just writes the VCPI to the MST device. For slots->0
3368 * transitions, this writes the updated VCPIs and removes the
3369 * remote VC payloads.
3371 * after calling this the driver should generate ACT and payload
3374 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr
*mgr
)
3376 struct drm_dp_payload req_payload
;
3377 struct drm_dp_mst_port
*port
;
3381 mutex_lock(&mgr
->payload_lock
);
3382 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
3383 struct drm_dp_vcpi
*vcpi
= mgr
->proposed_vcpis
[i
];
3384 struct drm_dp_payload
*payload
= &mgr
->payloads
[i
];
3385 bool put_port
= false;
3387 /* solve the current payloads - compare to the hw ones
3388 - update the hw view */
3389 req_payload
.start_slot
= cur_slots
;
3391 port
= container_of(vcpi
, struct drm_dp_mst_port
,
3394 /* Validated ports don't matter if we're releasing
3397 if (vcpi
->num_slots
) {
3398 port
= drm_dp_mst_topology_get_port_validated(
3401 mutex_unlock(&mgr
->payload_lock
);
3407 req_payload
.num_slots
= vcpi
->num_slots
;
3408 req_payload
.vcpi
= vcpi
->vcpi
;
3411 req_payload
.num_slots
= 0;
3414 payload
->start_slot
= req_payload
.start_slot
;
3415 /* work out what is required to happen with this payload */
3416 if (payload
->num_slots
!= req_payload
.num_slots
) {
3418 /* need to push an update for this payload */
3419 if (req_payload
.num_slots
) {
3420 drm_dp_create_payload_step1(mgr
, vcpi
->vcpi
,
3422 payload
->num_slots
= req_payload
.num_slots
;
3423 payload
->vcpi
= req_payload
.vcpi
;
3425 } else if (payload
->num_slots
) {
3426 payload
->num_slots
= 0;
3427 drm_dp_destroy_payload_step1(mgr
, port
,
3430 req_payload
.payload_state
=
3431 payload
->payload_state
;
3432 payload
->start_slot
= 0;
3434 payload
->payload_state
= req_payload
.payload_state
;
3436 cur_slots
+= req_payload
.num_slots
;
3439 drm_dp_mst_topology_put_port(port
);
3442 for (i
= 0; i
< mgr
->max_payloads
; /* do nothing */) {
3443 if (mgr
->payloads
[i
].payload_state
!= DP_PAYLOAD_DELETE_LOCAL
) {
3448 DRM_DEBUG_KMS("removing payload %d\n", i
);
3449 for (j
= i
; j
< mgr
->max_payloads
- 1; j
++) {
3450 mgr
->payloads
[j
] = mgr
->payloads
[j
+ 1];
3451 mgr
->proposed_vcpis
[j
] = mgr
->proposed_vcpis
[j
+ 1];
3453 if (mgr
->proposed_vcpis
[j
] &&
3454 mgr
->proposed_vcpis
[j
]->num_slots
) {
3455 set_bit(j
+ 1, &mgr
->payload_mask
);
3457 clear_bit(j
+ 1, &mgr
->payload_mask
);
3461 memset(&mgr
->payloads
[mgr
->max_payloads
- 1], 0,
3462 sizeof(struct drm_dp_payload
));
3463 mgr
->proposed_vcpis
[mgr
->max_payloads
- 1] = NULL
;
3464 clear_bit(mgr
->max_payloads
, &mgr
->payload_mask
);
3466 mutex_unlock(&mgr
->payload_lock
);
3470 EXPORT_SYMBOL(drm_dp_update_payload_part1
);
3473 * drm_dp_update_payload_part2() - Execute payload update part 2
3474 * @mgr: manager to use.
3476 * This iterates over all proposed virtual channels, and tries to
3477 * allocate space in the link for them. For 0->slots transitions,
3478 * this step writes the remote VC payload commands. For slots->0
3479 * this just resets some internal state.
3481 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr
*mgr
)
3483 struct drm_dp_mst_port
*port
;
3487 mutex_lock(&mgr
->payload_lock
);
3488 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
3490 if (!mgr
->proposed_vcpis
[i
])
3493 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
3495 DRM_DEBUG_KMS("payload %d %d\n", i
, mgr
->payloads
[i
].payload_state
);
3496 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_LOCAL
) {
3497 ret
= drm_dp_create_payload_step2(mgr
, port
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
3498 } else if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
3499 ret
= drm_dp_destroy_payload_step2(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
3502 mutex_unlock(&mgr
->payload_lock
);
3506 mutex_unlock(&mgr
->payload_lock
);
3509 EXPORT_SYMBOL(drm_dp_update_payload_part2
);
3511 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr
*mgr
,
3512 struct drm_dp_mst_port
*port
,
3513 int offset
, int size
, u8
*bytes
)
3516 struct drm_dp_sideband_msg_tx
*txmsg
;
3517 struct drm_dp_mst_branch
*mstb
;
3519 mstb
= drm_dp_mst_topology_get_mstb_validated(mgr
, port
->parent
);
3523 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3529 build_dpcd_read(txmsg
, port
->port_num
, offset
, size
);
3530 txmsg
->dst
= port
->parent
;
3532 drm_dp_queue_down_tx(mgr
, txmsg
);
3534 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3538 /* DPCD read should never be NACKed */
3539 if (txmsg
->reply
.reply_type
== 1) {
3540 DRM_ERROR("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
3541 mstb
, port
->port_num
, offset
, size
);
3546 if (txmsg
->reply
.u
.remote_dpcd_read_ack
.num_bytes
!= size
) {
3551 ret
= min_t(size_t, txmsg
->reply
.u
.remote_dpcd_read_ack
.num_bytes
,
3553 memcpy(bytes
, txmsg
->reply
.u
.remote_dpcd_read_ack
.bytes
, ret
);
3558 drm_dp_mst_topology_put_mstb(mstb
);
3563 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
3564 struct drm_dp_mst_port
*port
,
3565 int offset
, int size
, u8
*bytes
)
3568 struct drm_dp_sideband_msg_tx
*txmsg
;
3569 struct drm_dp_mst_branch
*mstb
;
3571 mstb
= drm_dp_mst_topology_get_mstb_validated(mgr
, port
->parent
);
3575 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3581 build_dpcd_write(txmsg
, port
->port_num
, offset
, size
, bytes
);
3584 drm_dp_queue_down_tx(mgr
, txmsg
);
3586 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3588 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
)
3596 drm_dp_mst_topology_put_mstb(mstb
);
3600 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx
*msg
, u8 req_type
)
3602 struct drm_dp_sideband_msg_reply_body reply
;
3604 reply
.reply_type
= DP_SIDEBAND_REPLY_ACK
;
3605 reply
.req_type
= req_type
;
3606 drm_dp_encode_sideband_reply(&reply
, msg
);
3610 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr
*mgr
,
3611 struct drm_dp_mst_branch
*mstb
,
3612 int req_type
, bool broadcast
)
3614 struct drm_dp_sideband_msg_tx
*txmsg
;
3616 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3621 drm_dp_encode_up_ack_reply(txmsg
, req_type
);
3623 mutex_lock(&mgr
->qlock
);
3624 /* construct a chunk from the first msg in the tx_msg queue */
3625 process_single_tx_qlock(mgr
, txmsg
, true);
3626 mutex_unlock(&mgr
->qlock
);
3632 static int drm_dp_get_vc_payload_bw(u8 dp_link_bw
, u8 dp_link_count
)
3634 if (dp_link_bw
== 0 || dp_link_count
== 0)
3635 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
3636 dp_link_bw
, dp_link_count
);
3638 return dp_link_bw
* dp_link_count
/ 2;
3642 * drm_dp_read_mst_cap() - check whether or not a sink supports MST
3643 * @aux: The DP AUX channel to use
3644 * @dpcd: A cached copy of the DPCD capabilities for this sink
3646 * Returns: %True if the sink supports MST, %false otherwise
3648 bool drm_dp_read_mst_cap(struct drm_dp_aux
*aux
,
3649 const u8 dpcd
[DP_RECEIVER_CAP_SIZE
])
3653 if (dpcd
[DP_DPCD_REV
] < DP_DPCD_REV_12
)
3656 if (drm_dp_dpcd_readb(aux
, DP_MSTM_CAP
, &mstm_cap
) != 1)
3659 return mstm_cap
& DP_MST_CAP
;
3661 EXPORT_SYMBOL(drm_dp_read_mst_cap
);
3664 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
3665 * @mgr: manager to set state for
3666 * @mst_state: true to enable MST on this connector - false to disable.
3668 * This is called by the driver when it detects an MST capable device plugged
3669 * into a DP MST capable port, or when a DP MST capable device is unplugged.
3671 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr
*mgr
, bool mst_state
)
3674 struct drm_dp_mst_branch
*mstb
= NULL
;
3676 mutex_lock(&mgr
->payload_lock
);
3677 mutex_lock(&mgr
->lock
);
3678 if (mst_state
== mgr
->mst_state
)
3681 mgr
->mst_state
= mst_state
;
3682 /* set the device into MST mode */
3684 struct drm_dp_payload reset_pay
;
3686 WARN_ON(mgr
->mst_primary
);
3689 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
3690 if (ret
!= DP_RECEIVER_CAP_SIZE
) {
3691 DRM_DEBUG_KMS("failed to read DPCD\n");
3695 mgr
->pbn_div
= drm_dp_get_vc_payload_bw(mgr
->dpcd
[1],
3696 mgr
->dpcd
[2] & DP_MAX_LANE_COUNT_MASK
);
3697 if (mgr
->pbn_div
== 0) {
3702 /* add initial branch device at LCT 1 */
3703 mstb
= drm_dp_add_mst_branch_device(1, NULL
);
3710 /* give this the main reference */
3711 mgr
->mst_primary
= mstb
;
3712 drm_dp_mst_topology_get_mstb(mgr
->mst_primary
);
3714 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
3717 DP_UPSTREAM_IS_SRC
);
3721 reset_pay
.start_slot
= 0;
3722 reset_pay
.num_slots
= 0x3f;
3723 drm_dp_dpcd_write_payload(mgr
, 0, &reset_pay
);
3725 queue_work(system_long_wq
, &mgr
->work
);
3729 /* disable MST on the device */
3730 mstb
= mgr
->mst_primary
;
3731 mgr
->mst_primary
= NULL
;
3732 /* this can fail if the device is gone */
3733 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
, 0);
3735 memset(mgr
->payloads
, 0,
3736 mgr
->max_payloads
* sizeof(mgr
->payloads
[0]));
3737 memset(mgr
->proposed_vcpis
, 0,
3738 mgr
->max_payloads
* sizeof(mgr
->proposed_vcpis
[0]));
3739 mgr
->payload_mask
= 0;
3740 set_bit(0, &mgr
->payload_mask
);
3742 mgr
->payload_id_table_cleared
= false;
3746 mutex_unlock(&mgr
->lock
);
3747 mutex_unlock(&mgr
->payload_lock
);
3749 drm_dp_mst_topology_put_mstb(mstb
);
3753 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst
);
3756 drm_dp_mst_topology_mgr_invalidate_mstb(struct drm_dp_mst_branch
*mstb
)
3758 struct drm_dp_mst_port
*port
;
3760 /* The link address will need to be re-sent on resume */
3761 mstb
->link_address_sent
= false;
3763 list_for_each_entry(port
, &mstb
->ports
, next
)
3765 drm_dp_mst_topology_mgr_invalidate_mstb(port
->mstb
);
3769 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
3770 * @mgr: manager to suspend
3772 * This function tells the MST device that we can't handle UP messages
3773 * anymore. This should stop it from sending any since we are suspended.
3775 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr
*mgr
)
3777 mutex_lock(&mgr
->lock
);
3778 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
3779 DP_MST_EN
| DP_UPSTREAM_IS_SRC
);
3780 mutex_unlock(&mgr
->lock
);
3781 flush_work(&mgr
->up_req_work
);
3782 flush_work(&mgr
->work
);
3783 flush_work(&mgr
->delayed_destroy_work
);
3785 mutex_lock(&mgr
->lock
);
3786 if (mgr
->mst_state
&& mgr
->mst_primary
)
3787 drm_dp_mst_topology_mgr_invalidate_mstb(mgr
->mst_primary
);
3788 mutex_unlock(&mgr
->lock
);
3790 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend
);
3793 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
3794 * @mgr: manager to resume
3795 * @sync: whether or not to perform topology reprobing synchronously
3797 * This will fetch DPCD and see if the device is still there,
3798 * if it is, it will rewrite the MSTM control bits, and return.
3800 * If the device fails this returns -1, and the driver should do
3801 * a full MST reprobe, in case we were undocked.
3803 * During system resume (where it is assumed that the driver will be calling
3804 * drm_atomic_helper_resume()) this function should be called beforehand with
3805 * @sync set to true. In contexts like runtime resume where the driver is not
3806 * expected to be calling drm_atomic_helper_resume(), this function should be
3807 * called with @sync set to false in order to avoid deadlocking.
3809 * Returns: -1 if the MST topology was removed while we were suspended, 0
3812 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr
*mgr
,
3818 mutex_lock(&mgr
->lock
);
3819 if (!mgr
->mst_primary
)
3822 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
,
3823 DP_RECEIVER_CAP_SIZE
);
3824 if (ret
!= DP_RECEIVER_CAP_SIZE
) {
3825 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3829 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
3832 DP_UPSTREAM_IS_SRC
);
3834 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
3838 /* Some hubs forget their guids after they resume */
3839 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_GUID
, guid
, 16);
3841 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3845 ret
= drm_dp_check_mstb_guid(mgr
->mst_primary
, guid
);
3847 DRM_DEBUG_KMS("check mstb failed - undocked during suspend?\n");
3852 * For the final step of resuming the topology, we need to bring the
3853 * state of our in-memory topology back into sync with reality. So,
3854 * restart the probing process as if we're probing a new hub
3856 queue_work(system_long_wq
, &mgr
->work
);
3857 mutex_unlock(&mgr
->lock
);
3860 DRM_DEBUG_KMS("Waiting for link probe work to finish re-syncing topology...\n");
3861 flush_work(&mgr
->work
);
3867 mutex_unlock(&mgr
->lock
);
3870 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume
);
3873 drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr
*mgr
, bool up
,
3874 struct drm_dp_mst_branch
**mstb
)
3878 int replylen
, curreply
;
3881 struct drm_dp_sideband_msg_hdr hdr
;
3882 struct drm_dp_sideband_msg_rx
*msg
=
3883 up
? &mgr
->up_req_recv
: &mgr
->down_rep_recv
;
3884 int basereg
= up
? DP_SIDEBAND_MSG_UP_REQ_BASE
:
3885 DP_SIDEBAND_MSG_DOWN_REP_BASE
;
3890 len
= min(mgr
->max_dpcd_transaction_bytes
, 16);
3891 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
, replyblock
, len
);
3893 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len
, ret
);
3897 ret
= drm_dp_decode_sideband_msg_hdr(&hdr
, replyblock
, len
, &hdrlen
);
3899 print_hex_dump(KERN_DEBUG
, "failed hdr", DUMP_PREFIX_NONE
, 16,
3900 1, replyblock
, len
, false);
3901 DRM_DEBUG_KMS("ERROR: failed header\n");
3906 /* Caller is responsible for giving back this reference */
3907 *mstb
= drm_dp_get_mst_branch_device(mgr
, hdr
.lct
, hdr
.rad
);
3909 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3915 if (!drm_dp_sideband_msg_set_header(msg
, &hdr
, hdrlen
)) {
3916 DRM_DEBUG_KMS("sideband msg set header failed %d\n",
3921 replylen
= min(msg
->curchunk_len
, (u8
)(len
- hdrlen
));
3922 ret
= drm_dp_sideband_append_payload(msg
, replyblock
+ hdrlen
, replylen
);
3924 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock
[0]);
3928 replylen
= msg
->curchunk_len
+ msg
->curchunk_hdrlen
- len
;
3930 while (replylen
> 0) {
3931 len
= min3(replylen
, mgr
->max_dpcd_transaction_bytes
, 16);
3932 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
+ curreply
,
3935 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
3940 ret
= drm_dp_sideband_append_payload(msg
, replyblock
, len
);
3942 DRM_DEBUG_KMS("failed to build sideband msg\n");
3952 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr
*mgr
)
3954 struct drm_dp_sideband_msg_tx
*txmsg
;
3955 struct drm_dp_mst_branch
*mstb
= NULL
;
3956 struct drm_dp_sideband_msg_rx
*msg
= &mgr
->down_rep_recv
;
3958 if (!drm_dp_get_one_sb_msg(mgr
, false, &mstb
))
3961 /* Multi-packet message transmission, don't clear the reply */
3962 if (!msg
->have_eomt
)
3965 /* find the message */
3966 mutex_lock(&mgr
->qlock
);
3967 txmsg
= list_first_entry_or_null(&mgr
->tx_msg_downq
,
3968 struct drm_dp_sideband_msg_tx
, next
);
3969 mutex_unlock(&mgr
->qlock
);
3971 /* Were we actually expecting a response, and from this mstb? */
3972 if (!txmsg
|| txmsg
->dst
!= mstb
) {
3973 struct drm_dp_sideband_msg_hdr
*hdr
;
3975 hdr
= &msg
->initial_hdr
;
3976 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
3977 mstb
, hdr
->seqno
, hdr
->lct
, hdr
->rad
[0],
3979 goto out_clear_reply
;
3982 drm_dp_sideband_parse_reply(msg
, &txmsg
->reply
);
3984 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
) {
3985 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
3986 txmsg
->reply
.req_type
,
3987 drm_dp_mst_req_type_str(txmsg
->reply
.req_type
),
3988 txmsg
->reply
.u
.nak
.reason
,
3989 drm_dp_mst_nak_reason_str(txmsg
->reply
.u
.nak
.reason
),
3990 txmsg
->reply
.u
.nak
.nak_data
);
3993 memset(msg
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
3994 drm_dp_mst_topology_put_mstb(mstb
);
3996 mutex_lock(&mgr
->qlock
);
3997 txmsg
->state
= DRM_DP_SIDEBAND_TX_RX
;
3998 list_del(&txmsg
->next
);
3999 mutex_unlock(&mgr
->qlock
);
4001 wake_up_all(&mgr
->tx_waitq
);
4006 memset(msg
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
4009 drm_dp_mst_topology_put_mstb(mstb
);
4015 drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr
*mgr
,
4016 struct drm_dp_pending_up_req
*up_req
)
4018 struct drm_dp_mst_branch
*mstb
= NULL
;
4019 struct drm_dp_sideband_msg_req_body
*msg
= &up_req
->msg
;
4020 struct drm_dp_sideband_msg_hdr
*hdr
= &up_req
->hdr
;
4021 bool hotplug
= false;
4023 if (hdr
->broadcast
) {
4024 const u8
*guid
= NULL
;
4026 if (msg
->req_type
== DP_CONNECTION_STATUS_NOTIFY
)
4027 guid
= msg
->u
.conn_stat
.guid
;
4028 else if (msg
->req_type
== DP_RESOURCE_STATUS_NOTIFY
)
4029 guid
= msg
->u
.resource_stat
.guid
;
4032 mstb
= drm_dp_get_mst_branch_device_by_guid(mgr
, guid
);
4034 mstb
= drm_dp_get_mst_branch_device(mgr
, hdr
->lct
, hdr
->rad
);
4038 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
4043 /* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */
4044 if (msg
->req_type
== DP_CONNECTION_STATUS_NOTIFY
) {
4045 drm_dp_mst_handle_conn_stat(mstb
, &msg
->u
.conn_stat
);
4049 drm_dp_mst_topology_put_mstb(mstb
);
4053 static void drm_dp_mst_up_req_work(struct work_struct
*work
)
4055 struct drm_dp_mst_topology_mgr
*mgr
=
4056 container_of(work
, struct drm_dp_mst_topology_mgr
,
4058 struct drm_dp_pending_up_req
*up_req
;
4059 bool send_hotplug
= false;
4061 mutex_lock(&mgr
->probe_lock
);
4063 mutex_lock(&mgr
->up_req_lock
);
4064 up_req
= list_first_entry_or_null(&mgr
->up_req_list
,
4065 struct drm_dp_pending_up_req
,
4068 list_del(&up_req
->next
);
4069 mutex_unlock(&mgr
->up_req_lock
);
4074 send_hotplug
|= drm_dp_mst_process_up_req(mgr
, up_req
);
4077 mutex_unlock(&mgr
->probe_lock
);
4080 drm_kms_helper_hotplug_event(mgr
->dev
);
4083 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr
*mgr
)
4085 struct drm_dp_pending_up_req
*up_req
;
4087 if (!drm_dp_get_one_sb_msg(mgr
, true, NULL
))
4090 if (!mgr
->up_req_recv
.have_eomt
)
4093 up_req
= kzalloc(sizeof(*up_req
), GFP_KERNEL
);
4095 DRM_ERROR("Not enough memory to process MST up req\n");
4098 INIT_LIST_HEAD(&up_req
->next
);
4100 drm_dp_sideband_parse_req(&mgr
->up_req_recv
, &up_req
->msg
);
4102 if (up_req
->msg
.req_type
!= DP_CONNECTION_STATUS_NOTIFY
&&
4103 up_req
->msg
.req_type
!= DP_RESOURCE_STATUS_NOTIFY
) {
4104 DRM_DEBUG_KMS("Received unknown up req type, ignoring: %x\n",
4105 up_req
->msg
.req_type
);
4110 drm_dp_send_up_ack_reply(mgr
, mgr
->mst_primary
, up_req
->msg
.req_type
,
4113 if (up_req
->msg
.req_type
== DP_CONNECTION_STATUS_NOTIFY
) {
4114 const struct drm_dp_connection_status_notify
*conn_stat
=
4115 &up_req
->msg
.u
.conn_stat
;
4117 DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",
4118 conn_stat
->port_number
,
4119 conn_stat
->legacy_device_plug_status
,
4120 conn_stat
->displayport_device_plug_status
,
4121 conn_stat
->message_capability_status
,
4122 conn_stat
->input_port
,
4123 conn_stat
->peer_device_type
);
4124 } else if (up_req
->msg
.req_type
== DP_RESOURCE_STATUS_NOTIFY
) {
4125 const struct drm_dp_resource_status_notify
*res_stat
=
4126 &up_req
->msg
.u
.resource_stat
;
4128 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n",
4129 res_stat
->port_number
,
4130 res_stat
->available_pbn
);
4133 up_req
->hdr
= mgr
->up_req_recv
.initial_hdr
;
4134 mutex_lock(&mgr
->up_req_lock
);
4135 list_add_tail(&up_req
->next
, &mgr
->up_req_list
);
4136 mutex_unlock(&mgr
->up_req_lock
);
4137 queue_work(system_long_wq
, &mgr
->up_req_work
);
4140 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
4145 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
4146 * @mgr: manager to notify irq for.
4147 * @esi: 4 bytes from SINK_COUNT_ESI
4148 * @handled: whether the hpd interrupt was consumed or not
4150 * This should be called from the driver when it detects a short IRQ,
4151 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
4152 * topology manager will process the sideband messages received as a result
4155 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr
*mgr
, u8
*esi
, bool *handled
)
4162 if (sc
!= mgr
->sink_count
) {
4163 mgr
->sink_count
= sc
;
4167 if (esi
[1] & DP_DOWN_REP_MSG_RDY
) {
4168 ret
= drm_dp_mst_handle_down_rep(mgr
);
4172 if (esi
[1] & DP_UP_REQ_MSG_RDY
) {
4173 ret
|= drm_dp_mst_handle_up_req(mgr
);
4177 drm_dp_mst_kick_tx(mgr
);
4180 EXPORT_SYMBOL(drm_dp_mst_hpd_irq
);
4183 * drm_dp_mst_detect_port() - get connection status for an MST port
4184 * @connector: DRM connector for this port
4185 * @ctx: The acquisition context to use for grabbing locks
4186 * @mgr: manager for this port
4187 * @port: pointer to a port
4189 * This returns the current connection state for a port.
4192 drm_dp_mst_detect_port(struct drm_connector
*connector
,
4193 struct drm_modeset_acquire_ctx
*ctx
,
4194 struct drm_dp_mst_topology_mgr
*mgr
,
4195 struct drm_dp_mst_port
*port
)
4199 /* we need to search for the port in the mgr in case it's gone */
4200 port
= drm_dp_mst_topology_get_port_validated(mgr
, port
);
4202 return connector_status_disconnected
;
4204 ret
= drm_modeset_lock(&mgr
->base
.lock
, ctx
);
4208 ret
= connector_status_disconnected
;
4213 switch (port
->pdt
) {
4214 case DP_PEER_DEVICE_NONE
:
4215 case DP_PEER_DEVICE_MST_BRANCHING
:
4217 ret
= connector_status_connected
;
4220 case DP_PEER_DEVICE_SST_SINK
:
4221 ret
= connector_status_connected
;
4222 /* for logical ports - cache the EDID */
4223 if (port
->port_num
>= 8 && !port
->cached_edid
) {
4224 port
->cached_edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
4227 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
4229 ret
= connector_status_connected
;
4233 drm_dp_mst_topology_put_port(port
);
4236 EXPORT_SYMBOL(drm_dp_mst_detect_port
);
4239 * drm_dp_mst_get_edid() - get EDID for an MST port
4240 * @connector: toplevel connector to get EDID for
4241 * @mgr: manager for this port
4242 * @port: unverified pointer to a port.
4244 * This returns an EDID for the port connected to a connector,
4245 * It validates the pointer still exists so the caller doesn't require a
4248 struct edid
*drm_dp_mst_get_edid(struct drm_connector
*connector
, struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
4250 struct edid
*edid
= NULL
;
4252 /* we need to search for the port in the mgr in case it's gone */
4253 port
= drm_dp_mst_topology_get_port_validated(mgr
, port
);
4257 if (port
->cached_edid
)
4258 edid
= drm_edid_duplicate(port
->cached_edid
);
4260 edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
4262 port
->has_audio
= drm_detect_monitor_audio(edid
);
4263 drm_dp_mst_topology_put_port(port
);
4266 EXPORT_SYMBOL(drm_dp_mst_get_edid
);
4269 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
4270 * @mgr: manager to use
4271 * @pbn: payload bandwidth to convert into slots.
4273 * Calculate the number of VCPI slots that will be required for the given PBN
4274 * value. This function is deprecated, and should not be used in atomic
4278 * The total slots required for this port, or error.
4280 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
,
4285 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
4287 /* max. time slots - one slot for MTP header */
4292 EXPORT_SYMBOL(drm_dp_find_vcpi_slots
);
4294 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
4295 struct drm_dp_vcpi
*vcpi
, int pbn
, int slots
)
4299 /* max. time slots - one slot for MTP header */
4304 vcpi
->aligned_pbn
= slots
* mgr
->pbn_div
;
4305 vcpi
->num_slots
= slots
;
4307 ret
= drm_dp_mst_assign_payload_id(mgr
, vcpi
);
4314 * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
4315 * @state: global atomic state
4316 * @mgr: MST topology manager for the port
4317 * @port: port to find vcpi slots for
4318 * @pbn: bandwidth required for the mode in PBN
4319 * @pbn_div: divider for DSC mode that takes FEC into account
4321 * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
4322 * may have had. Any atomic drivers which support MST must call this function
4323 * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
4324 * current VCPI allocation for the new state, but only when
4325 * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
4326 * to ensure compatibility with userspace applications that still use the
4327 * legacy modesetting UAPI.
4329 * Allocations set by this function are not checked against the bandwidth
4330 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
4332 * Additionally, it is OK to call this function multiple times on the same
4333 * @port as needed. It is not OK however, to call this function and
4334 * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
4337 * drm_dp_atomic_release_vcpi_slots()
4338 * drm_dp_mst_atomic_check()
4341 * Total slots in the atomic state assigned for this port, or a negative error
4342 * code if the port no longer exists
4344 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state
*state
,
4345 struct drm_dp_mst_topology_mgr
*mgr
,
4346 struct drm_dp_mst_port
*port
, int pbn
,
4349 struct drm_dp_mst_topology_state
*topology_state
;
4350 struct drm_dp_vcpi_allocation
*pos
, *vcpi
= NULL
;
4351 int prev_slots
, prev_bw
, req_slots
;
4353 topology_state
= drm_atomic_get_mst_topology_state(state
, mgr
);
4354 if (IS_ERR(topology_state
))
4355 return PTR_ERR(topology_state
);
4357 /* Find the current allocation for this port, if any */
4358 list_for_each_entry(pos
, &topology_state
->vcpis
, next
) {
4359 if (pos
->port
== port
) {
4361 prev_slots
= vcpi
->vcpi
;
4362 prev_bw
= vcpi
->pbn
;
4365 * This should never happen, unless the driver tries
4366 * releasing and allocating the same VCPI allocation,
4369 if (WARN_ON(!prev_slots
)) {
4370 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
4384 pbn_div
= mgr
->pbn_div
;
4386 req_slots
= DIV_ROUND_UP(pbn
, pbn_div
);
4388 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
4389 port
->connector
->base
.id
, port
->connector
->name
,
4390 port
, prev_slots
, req_slots
);
4391 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n",
4392 port
->connector
->base
.id
, port
->connector
->name
,
4393 port
, prev_bw
, pbn
);
4395 /* Add the new allocation to the state */
4397 vcpi
= kzalloc(sizeof(*vcpi
), GFP_KERNEL
);
4401 drm_dp_mst_get_port_malloc(port
);
4403 list_add(&vcpi
->next
, &topology_state
->vcpis
);
4405 vcpi
->vcpi
= req_slots
;
4410 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots
);
4413 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
4414 * @state: global atomic state
4415 * @mgr: MST topology manager for the port
4416 * @port: The port to release the VCPI slots from
4418 * Releases any VCPI slots that have been allocated to a port in the atomic
4419 * state. Any atomic drivers which support MST must call this function in
4420 * their &drm_connector_helper_funcs.atomic_check() callback when the
4421 * connector will no longer have VCPI allocated (e.g. because its CRTC was
4422 * removed) when it had VCPI allocated in the previous atomic state.
4424 * It is OK to call this even if @port has been removed from the system.
4425 * Additionally, it is OK to call this function multiple times on the same
4426 * @port as needed. It is not OK however, to call this function and
4427 * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
4431 * drm_dp_atomic_find_vcpi_slots()
4432 * drm_dp_mst_atomic_check()
4435 * 0 if all slots for this port were added back to
4436 * &drm_dp_mst_topology_state.avail_slots or negative error code
4438 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state
*state
,
4439 struct drm_dp_mst_topology_mgr
*mgr
,
4440 struct drm_dp_mst_port
*port
)
4442 struct drm_dp_mst_topology_state
*topology_state
;
4443 struct drm_dp_vcpi_allocation
*pos
;
4446 topology_state
= drm_atomic_get_mst_topology_state(state
, mgr
);
4447 if (IS_ERR(topology_state
))
4448 return PTR_ERR(topology_state
);
4450 list_for_each_entry(pos
, &topology_state
->vcpis
, next
) {
4451 if (pos
->port
== port
) {
4456 if (WARN_ON(!found
)) {
4457 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
4458 port
, &topology_state
->base
);
4462 DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port
, pos
->vcpi
);
4464 drm_dp_mst_put_port_malloc(port
);
4471 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots
);
4474 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
4475 * @mgr: manager for this port
4476 * @port: port to allocate a virtual channel for.
4477 * @pbn: payload bandwidth number to request
4478 * @slots: returned number of slots for this PBN.
4480 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
4481 struct drm_dp_mst_port
*port
, int pbn
, int slots
)
4488 port
= drm_dp_mst_topology_get_port_validated(mgr
, port
);
4492 if (port
->vcpi
.vcpi
> 0) {
4493 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
4494 port
->vcpi
.vcpi
, port
->vcpi
.pbn
, pbn
);
4495 if (pbn
== port
->vcpi
.pbn
) {
4496 drm_dp_mst_topology_put_port(port
);
4501 ret
= drm_dp_init_vcpi(mgr
, &port
->vcpi
, pbn
, slots
);
4503 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
4504 DIV_ROUND_UP(pbn
, mgr
->pbn_div
), ret
);
4505 drm_dp_mst_topology_put_port(port
);
4508 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
4509 pbn
, port
->vcpi
.num_slots
);
4511 /* Keep port allocated until its payload has been removed */
4512 drm_dp_mst_get_port_malloc(port
);
4513 drm_dp_mst_topology_put_port(port
);
4518 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi
);
4520 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
4524 port
= drm_dp_mst_topology_get_port_validated(mgr
, port
);
4528 slots
= port
->vcpi
.num_slots
;
4529 drm_dp_mst_topology_put_port(port
);
4532 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots
);
4535 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
4536 * @mgr: manager for this port
4537 * @port: unverified pointer to a port.
4539 * This just resets the number of slots for the ports VCPI for later programming.
4541 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
4544 * A port with VCPI will remain allocated until its VCPI is
4545 * released, no verified ref needed
4548 port
->vcpi
.num_slots
= 0;
4550 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots
);
4553 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
4554 * @mgr: manager for this port
4555 * @port: port to deallocate vcpi for
4557 * This can be called unconditionally, regardless of whether
4558 * drm_dp_mst_allocate_vcpi() succeeded or not.
4560 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
4561 struct drm_dp_mst_port
*port
)
4563 if (!port
->vcpi
.vcpi
)
4566 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
4567 port
->vcpi
.num_slots
= 0;
4569 port
->vcpi
.aligned_pbn
= 0;
4570 port
->vcpi
.vcpi
= 0;
4571 drm_dp_mst_put_port_malloc(port
);
4573 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi
);
4575 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
4576 int id
, struct drm_dp_payload
*payload
)
4578 u8 payload_alloc
[3], status
;
4582 drm_dp_dpcd_writeb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
,
4583 DP_PAYLOAD_TABLE_UPDATED
);
4585 payload_alloc
[0] = id
;
4586 payload_alloc
[1] = payload
->start_slot
;
4587 payload_alloc
[2] = payload
->num_slots
;
4589 ret
= drm_dp_dpcd_write(mgr
->aux
, DP_PAYLOAD_ALLOCATE_SET
, payload_alloc
, 3);
4591 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret
);
4596 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
4598 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
4602 if (!(status
& DP_PAYLOAD_TABLE_UPDATED
)) {
4605 usleep_range(10000, 20000);
4608 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status
);
4617 static int do_get_act_status(struct drm_dp_aux
*aux
)
4622 ret
= drm_dp_dpcd_readb(aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
4630 * drm_dp_check_act_status() - Polls for ACT handled status.
4631 * @mgr: manager to use
4633 * Tries waiting for the MST hub to finish updating it's payload table by
4634 * polling for the ACT handled bit for up to 3 seconds (yes-some hubs really
4638 * 0 if the ACT was handled in time, negative error code on failure.
4640 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr
*mgr
)
4643 * There doesn't seem to be any recommended retry count or timeout in
4644 * the MST specification. Since some hubs have been observed to take
4645 * over 1 second to update their payload allocations under certain
4646 * conditions, we use a rather large timeout value.
4648 const int timeout_ms
= 3000;
4651 ret
= readx_poll_timeout(do_get_act_status
, mgr
->aux
, status
,
4652 status
& DP_PAYLOAD_ACT_HANDLED
|| status
< 0,
4653 200, timeout_ms
* USEC_PER_MSEC
);
4654 if (ret
< 0 && status
>= 0) {
4655 DRM_ERROR("Failed to get ACT after %dms, last status: %02x\n",
4656 timeout_ms
, status
);
4658 } else if (status
< 0) {
4660 * Failure here isn't unexpected - the hub may have
4661 * just been unplugged
4663 DRM_DEBUG_KMS("Failed to read payload table status: %d\n",
4670 EXPORT_SYMBOL(drm_dp_check_act_status
);
4673 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
4674 * @clock: dot clock for the mode
4675 * @bpp: bpp for the mode.
4676 * @dsc: DSC mode. If true, bpp has units of 1/16 of a bit per pixel
4678 * This uses the formula in the spec to calculate the PBN value for a mode.
4680 int drm_dp_calc_pbn_mode(int clock
, int bpp
, bool dsc
)
4683 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
4684 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
4685 * common multiplier to render an integer PBN for all link rate/lane
4686 * counts combinations
4688 * peak_kbps *= (1006/1000)
4689 * peak_kbps *= (64/54)
4690 * peak_kbps *= 8 convert to bytes
4692 * If the bpp is in units of 1/16, further divide by 16. Put this
4693 * factor in the numerator rather than the denominator to avoid
4698 return DIV_ROUND_UP_ULL(mul_u32_u32(clock
* (bpp
/ 16), 64 * 1006),
4699 8 * 54 * 1000 * 1000);
4701 return DIV_ROUND_UP_ULL(mul_u32_u32(clock
* bpp
, 64 * 1006),
4702 8 * 54 * 1000 * 1000);
4704 EXPORT_SYMBOL(drm_dp_calc_pbn_mode
);
4706 /* we want to kick the TX after we've ack the up/down IRQs. */
4707 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
)
4709 queue_work(system_long_wq
, &mgr
->tx_work
);
4712 static void drm_dp_mst_dump_mstb(struct seq_file
*m
,
4713 struct drm_dp_mst_branch
*mstb
)
4715 struct drm_dp_mst_port
*port
;
4716 int tabs
= mstb
->lct
;
4720 for (i
= 0; i
< tabs
; i
++)
4724 seq_printf(m
, "%smst: %p, %d\n", prefix
, mstb
, mstb
->num_ports
);
4725 list_for_each_entry(port
, &mstb
->ports
, next
) {
4726 seq_printf(m
, "%sport: %d: input: %d: pdt: %d, ddps: %d ldps: %d, sdp: %d/%d, %p, conn: %p\n", prefix
, port
->port_num
, port
->input
, port
->pdt
, port
->ddps
, port
->ldps
, port
->num_sdp_streams
, port
->num_sdp_stream_sinks
, port
, port
->connector
);
4728 drm_dp_mst_dump_mstb(m
, port
->mstb
);
4732 #define DP_PAYLOAD_TABLE_SIZE 64
4734 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
4739 for (i
= 0; i
< DP_PAYLOAD_TABLE_SIZE
; i
+= 16) {
4740 if (drm_dp_dpcd_read(mgr
->aux
,
4741 DP_PAYLOAD_TABLE_UPDATE_STATUS
+ i
,
4748 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr
*mgr
,
4749 struct drm_dp_mst_port
*port
, char *name
,
4752 struct edid
*mst_edid
;
4754 mst_edid
= drm_dp_mst_get_edid(port
->connector
, mgr
, port
);
4755 drm_edid_get_monitor_name(mst_edid
, name
, namelen
);
4759 * drm_dp_mst_dump_topology(): dump topology to seq file.
4760 * @m: seq_file to dump output to
4761 * @mgr: manager to dump current topology for.
4763 * helper to dump MST topology to a seq file for debugfs.
4765 void drm_dp_mst_dump_topology(struct seq_file
*m
,
4766 struct drm_dp_mst_topology_mgr
*mgr
)
4769 struct drm_dp_mst_port
*port
;
4771 mutex_lock(&mgr
->lock
);
4772 if (mgr
->mst_primary
)
4773 drm_dp_mst_dump_mstb(m
, mgr
->mst_primary
);
4776 mutex_unlock(&mgr
->lock
);
4778 mutex_lock(&mgr
->payload_lock
);
4779 seq_printf(m
, "vcpi: %lx %lx %d\n", mgr
->payload_mask
, mgr
->vcpi_mask
,
4782 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
4783 if (mgr
->proposed_vcpis
[i
]) {
4786 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
4787 fetch_monitor_name(mgr
, port
, name
, sizeof(name
));
4788 seq_printf(m
, "vcpi %d: %d %d %d sink name: %s\n", i
,
4789 port
->port_num
, port
->vcpi
.vcpi
,
4790 port
->vcpi
.num_slots
,
4791 (*name
!= 0) ? name
: "Unknown");
4793 seq_printf(m
, "vcpi %d:unused\n", i
);
4795 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
4796 seq_printf(m
, "payload %d: %d, %d, %d\n",
4798 mgr
->payloads
[i
].payload_state
,
4799 mgr
->payloads
[i
].start_slot
,
4800 mgr
->payloads
[i
].num_slots
);
4804 mutex_unlock(&mgr
->payload_lock
);
4806 mutex_lock(&mgr
->lock
);
4807 if (mgr
->mst_primary
) {
4808 u8 buf
[DP_PAYLOAD_TABLE_SIZE
];
4811 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, buf
, DP_RECEIVER_CAP_SIZE
);
4813 seq_printf(m
, "dpcd read failed\n");
4816 seq_printf(m
, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE
, buf
);
4818 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_FAUX_CAP
, buf
, 2);
4820 seq_printf(m
, "faux/mst read failed\n");
4823 seq_printf(m
, "faux/mst: %*ph\n", 2, buf
);
4825 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_MSTM_CTRL
, buf
, 1);
4827 seq_printf(m
, "mst ctrl read failed\n");
4830 seq_printf(m
, "mst ctrl: %*ph\n", 1, buf
);
4832 /* dump the standard OUI branch header */
4833 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_BRANCH_OUI
, buf
, DP_BRANCH_OUI_HEADER_SIZE
);
4835 seq_printf(m
, "branch oui read failed\n");
4838 seq_printf(m
, "branch oui: %*phN devid: ", 3, buf
);
4840 for (i
= 0x3; i
< 0x8 && buf
[i
]; i
++)
4841 seq_printf(m
, "%c", buf
[i
]);
4842 seq_printf(m
, " revision: hw: %x.%x sw: %x.%x\n",
4843 buf
[0x9] >> 4, buf
[0x9] & 0xf, buf
[0xa], buf
[0xb]);
4844 if (dump_dp_payload_table(mgr
, buf
))
4845 seq_printf(m
, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE
, buf
);
4849 mutex_unlock(&mgr
->lock
);
4852 EXPORT_SYMBOL(drm_dp_mst_dump_topology
);
4854 static void drm_dp_tx_work(struct work_struct
*work
)
4856 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, tx_work
);
4858 mutex_lock(&mgr
->qlock
);
4859 if (!list_empty(&mgr
->tx_msg_downq
))
4860 process_single_down_tx_qlock(mgr
);
4861 mutex_unlock(&mgr
->qlock
);
4865 drm_dp_delayed_destroy_port(struct drm_dp_mst_port
*port
)
4867 drm_dp_port_set_pdt(port
, DP_PEER_DEVICE_NONE
, port
->mcs
);
4869 if (port
->connector
) {
4870 drm_connector_unregister(port
->connector
);
4871 drm_connector_put(port
->connector
);
4874 drm_dp_mst_put_port_malloc(port
);
4878 drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch
*mstb
)
4880 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
4881 struct drm_dp_mst_port
*port
, *port_tmp
;
4882 struct drm_dp_sideband_msg_tx
*txmsg
, *txmsg_tmp
;
4883 bool wake_tx
= false;
4885 mutex_lock(&mgr
->lock
);
4886 list_for_each_entry_safe(port
, port_tmp
, &mstb
->ports
, next
) {
4887 list_del(&port
->next
);
4888 drm_dp_mst_topology_put_port(port
);
4890 mutex_unlock(&mgr
->lock
);
4892 /* drop any tx slot msg */
4893 mutex_lock(&mstb
->mgr
->qlock
);
4894 list_for_each_entry_safe(txmsg
, txmsg_tmp
, &mgr
->tx_msg_downq
, next
) {
4895 if (txmsg
->dst
!= mstb
)
4898 txmsg
->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
4899 list_del(&txmsg
->next
);
4902 mutex_unlock(&mstb
->mgr
->qlock
);
4905 wake_up_all(&mstb
->mgr
->tx_waitq
);
4907 drm_dp_mst_put_mstb_malloc(mstb
);
4910 static void drm_dp_delayed_destroy_work(struct work_struct
*work
)
4912 struct drm_dp_mst_topology_mgr
*mgr
=
4913 container_of(work
, struct drm_dp_mst_topology_mgr
,
4914 delayed_destroy_work
);
4915 bool send_hotplug
= false, go_again
;
4918 * Not a regular list traverse as we have to drop the destroy
4919 * connector lock before destroying the mstb/port, to avoid AB->BA
4920 * ordering between this lock and the config mutex.
4926 struct drm_dp_mst_branch
*mstb
;
4928 mutex_lock(&mgr
->delayed_destroy_lock
);
4929 mstb
= list_first_entry_or_null(&mgr
->destroy_branch_device_list
,
4930 struct drm_dp_mst_branch
,
4933 list_del(&mstb
->destroy_next
);
4934 mutex_unlock(&mgr
->delayed_destroy_lock
);
4939 drm_dp_delayed_destroy_mstb(mstb
);
4944 struct drm_dp_mst_port
*port
;
4946 mutex_lock(&mgr
->delayed_destroy_lock
);
4947 port
= list_first_entry_or_null(&mgr
->destroy_port_list
,
4948 struct drm_dp_mst_port
,
4951 list_del(&port
->next
);
4952 mutex_unlock(&mgr
->delayed_destroy_lock
);
4957 drm_dp_delayed_destroy_port(port
);
4958 send_hotplug
= true;
4964 drm_kms_helper_hotplug_event(mgr
->dev
);
4967 static struct drm_private_state
*
4968 drm_dp_mst_duplicate_state(struct drm_private_obj
*obj
)
4970 struct drm_dp_mst_topology_state
*state
, *old_state
=
4971 to_dp_mst_topology_state(obj
->state
);
4972 struct drm_dp_vcpi_allocation
*pos
, *vcpi
;
4974 state
= kmemdup(old_state
, sizeof(*state
), GFP_KERNEL
);
4978 __drm_atomic_helper_private_obj_duplicate_state(obj
, &state
->base
);
4980 INIT_LIST_HEAD(&state
->vcpis
);
4982 list_for_each_entry(pos
, &old_state
->vcpis
, next
) {
4983 /* Prune leftover freed VCPI allocations */
4987 vcpi
= kmemdup(pos
, sizeof(*vcpi
), GFP_KERNEL
);
4991 drm_dp_mst_get_port_malloc(vcpi
->port
);
4992 list_add(&vcpi
->next
, &state
->vcpis
);
4995 return &state
->base
;
4998 list_for_each_entry_safe(pos
, vcpi
, &state
->vcpis
, next
) {
4999 drm_dp_mst_put_port_malloc(pos
->port
);
5007 static void drm_dp_mst_destroy_state(struct drm_private_obj
*obj
,
5008 struct drm_private_state
*state
)
5010 struct drm_dp_mst_topology_state
*mst_state
=
5011 to_dp_mst_topology_state(state
);
5012 struct drm_dp_vcpi_allocation
*pos
, *tmp
;
5014 list_for_each_entry_safe(pos
, tmp
, &mst_state
->vcpis
, next
) {
5015 /* We only keep references to ports with non-zero VCPIs */
5017 drm_dp_mst_put_port_malloc(pos
->port
);
5024 static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port
*port
,
5025 struct drm_dp_mst_branch
*branch
)
5027 while (port
->parent
) {
5028 if (port
->parent
== branch
)
5031 if (port
->parent
->port_parent
)
5032 port
= port
->parent
->port_parent
;
5040 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port
*port
,
5041 struct drm_dp_mst_topology_state
*state
);
5044 drm_dp_mst_atomic_check_mstb_bw_limit(struct drm_dp_mst_branch
*mstb
,
5045 struct drm_dp_mst_topology_state
*state
)
5047 struct drm_dp_vcpi_allocation
*vcpi
;
5048 struct drm_dp_mst_port
*port
;
5049 int pbn_used
= 0, ret
;
5052 /* Check that we have at least one port in our state that's downstream
5053 * of this branch, otherwise we can skip this branch
5055 list_for_each_entry(vcpi
, &state
->vcpis
, next
) {
5057 !drm_dp_mst_port_downstream_of_branch(vcpi
->port
, mstb
))
5066 if (mstb
->port_parent
)
5067 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n",
5068 mstb
->port_parent
->parent
, mstb
->port_parent
,
5071 DRM_DEBUG_ATOMIC("[MSTB:%p] Checking bandwidth limits\n",
5074 list_for_each_entry(port
, &mstb
->ports
, next
) {
5075 ret
= drm_dp_mst_atomic_check_port_bw_limit(port
, state
);
5086 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port
*port
,
5087 struct drm_dp_mst_topology_state
*state
)
5089 struct drm_dp_vcpi_allocation
*vcpi
;
5092 if (port
->pdt
== DP_PEER_DEVICE_NONE
)
5095 if (drm_dp_mst_is_end_device(port
->pdt
, port
->mcs
)) {
5098 list_for_each_entry(vcpi
, &state
->vcpis
, next
) {
5099 if (vcpi
->port
!= port
)
5110 /* This should never happen, as it means we tried to
5111 * set a mode before querying the full_pbn
5113 if (WARN_ON(!port
->full_pbn
))
5116 pbn_used
= vcpi
->pbn
;
5118 pbn_used
= drm_dp_mst_atomic_check_mstb_bw_limit(port
->mstb
,
5124 if (pbn_used
> port
->full_pbn
) {
5125 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n",
5126 port
->parent
, port
, pbn_used
,
5131 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n",
5132 port
->parent
, port
, pbn_used
, port
->full_pbn
);
5138 drm_dp_mst_atomic_check_vcpi_alloc_limit(struct drm_dp_mst_topology_mgr
*mgr
,
5139 struct drm_dp_mst_topology_state
*mst_state
)
5141 struct drm_dp_vcpi_allocation
*vcpi
;
5142 int avail_slots
= 63, payload_count
= 0;
5144 list_for_each_entry(vcpi
, &mst_state
->vcpis
, next
) {
5145 /* Releasing VCPI is always OK-even if the port is gone */
5147 DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
5152 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
5153 vcpi
->port
, vcpi
->vcpi
);
5155 avail_slots
-= vcpi
->vcpi
;
5156 if (avail_slots
< 0) {
5157 DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
5158 vcpi
->port
, mst_state
,
5159 avail_slots
+ vcpi
->vcpi
);
5163 if (++payload_count
> mgr
->max_payloads
) {
5164 DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
5165 mgr
, mst_state
, mgr
->max_payloads
);
5169 DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
5170 mgr
, mst_state
, avail_slots
,
5177 * drm_dp_mst_add_affected_dsc_crtcs
5178 * @state: Pointer to the new struct drm_dp_mst_topology_state
5179 * @mgr: MST topology manager
5181 * Whenever there is a change in mst topology
5182 * DSC configuration would have to be recalculated
5183 * therefore we need to trigger modeset on all affected
5184 * CRTCs in that topology
5187 * drm_dp_mst_atomic_enable_dsc()
5189 int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_state
*state
, struct drm_dp_mst_topology_mgr
*mgr
)
5191 struct drm_dp_mst_topology_state
*mst_state
;
5192 struct drm_dp_vcpi_allocation
*pos
;
5193 struct drm_connector
*connector
;
5194 struct drm_connector_state
*conn_state
;
5195 struct drm_crtc
*crtc
;
5196 struct drm_crtc_state
*crtc_state
;
5198 mst_state
= drm_atomic_get_mst_topology_state(state
, mgr
);
5200 if (IS_ERR(mst_state
))
5203 list_for_each_entry(pos
, &mst_state
->vcpis
, next
) {
5205 connector
= pos
->port
->connector
;
5210 conn_state
= drm_atomic_get_connector_state(state
, connector
);
5212 if (IS_ERR(conn_state
))
5213 return PTR_ERR(conn_state
);
5215 crtc
= conn_state
->crtc
;
5220 if (!drm_dp_mst_dsc_aux_for_port(pos
->port
))
5223 crtc_state
= drm_atomic_get_crtc_state(mst_state
->base
.state
, crtc
);
5225 if (IS_ERR(crtc_state
))
5226 return PTR_ERR(crtc_state
);
5228 DRM_DEBUG_ATOMIC("[MST MGR:%p] Setting mode_changed flag on CRTC %p\n",
5231 crtc_state
->mode_changed
= true;
5235 EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs
);
5238 * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off
5239 * @state: Pointer to the new drm_atomic_state
5240 * @port: Pointer to the affected MST Port
5241 * @pbn: Newly recalculated bw required for link with DSC enabled
5242 * @pbn_div: Divider to calculate correct number of pbn per slot
5243 * @enable: Boolean flag to enable or disable DSC on the port
5245 * This function enables DSC on the given Port
5246 * by recalculating its vcpi from pbn provided
5247 * and sets dsc_enable flag to keep track of which
5248 * ports have DSC enabled
5251 int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_state
*state
,
5252 struct drm_dp_mst_port
*port
,
5253 int pbn
, int pbn_div
,
5256 struct drm_dp_mst_topology_state
*mst_state
;
5257 struct drm_dp_vcpi_allocation
*pos
;
5261 mst_state
= drm_atomic_get_mst_topology_state(state
, port
->mgr
);
5263 if (IS_ERR(mst_state
))
5264 return PTR_ERR(mst_state
);
5266 list_for_each_entry(pos
, &mst_state
->vcpis
, next
) {
5267 if (pos
->port
== port
) {
5274 DRM_DEBUG_ATOMIC("[MST PORT:%p] Couldn't find VCPI allocation in mst state %p\n",
5279 if (pos
->dsc_enabled
== enable
) {
5280 DRM_DEBUG_ATOMIC("[MST PORT:%p] DSC flag is already set to %d, returning %d VCPI slots\n",
5281 port
, enable
, pos
->vcpi
);
5286 vcpi
= drm_dp_atomic_find_vcpi_slots(state
, port
->mgr
, port
, pbn
, pbn_div
);
5287 DRM_DEBUG_ATOMIC("[MST PORT:%p] Enabling DSC flag, reallocating %d VCPI slots on the port\n",
5293 pos
->dsc_enabled
= enable
;
5297 EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc
);
5299 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
5300 * atomic update is valid
5301 * @state: Pointer to the new &struct drm_dp_mst_topology_state
5303 * Checks the given topology state for an atomic update to ensure that it's
5304 * valid. This includes checking whether there's enough bandwidth to support
5305 * the new VCPI allocations in the atomic update.
5307 * Any atomic drivers supporting DP MST must make sure to call this after
5308 * checking the rest of their state in their
5309 * &drm_mode_config_funcs.atomic_check() callback.
5312 * drm_dp_atomic_find_vcpi_slots()
5313 * drm_dp_atomic_release_vcpi_slots()
5317 * 0 if the new state is valid, negative error code otherwise.
5319 int drm_dp_mst_atomic_check(struct drm_atomic_state
*state
)
5321 struct drm_dp_mst_topology_mgr
*mgr
;
5322 struct drm_dp_mst_topology_state
*mst_state
;
5325 for_each_new_mst_mgr_in_state(state
, mgr
, mst_state
, i
) {
5326 if (!mgr
->mst_state
)
5329 ret
= drm_dp_mst_atomic_check_vcpi_alloc_limit(mgr
, mst_state
);
5333 mutex_lock(&mgr
->lock
);
5334 ret
= drm_dp_mst_atomic_check_mstb_bw_limit(mgr
->mst_primary
,
5336 mutex_unlock(&mgr
->lock
);
5345 EXPORT_SYMBOL(drm_dp_mst_atomic_check
);
5347 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs
= {
5348 .atomic_duplicate_state
= drm_dp_mst_duplicate_state
,
5349 .atomic_destroy_state
= drm_dp_mst_destroy_state
,
5351 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs
);
5354 * drm_atomic_get_mst_topology_state: get MST topology state
5356 * @state: global atomic state
5357 * @mgr: MST topology manager, also the private object in this case
5359 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
5360 * state vtable so that the private object state returned is that of a MST
5361 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
5362 * to care of the locking, so warn if don't hold the connection_mutex.
5366 * The MST topology state or error pointer.
5368 struct drm_dp_mst_topology_state
*drm_atomic_get_mst_topology_state(struct drm_atomic_state
*state
,
5369 struct drm_dp_mst_topology_mgr
*mgr
)
5371 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state
, &mgr
->base
));
5373 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state
);
5376 * drm_dp_mst_topology_mgr_init - initialise a topology manager
5377 * @mgr: manager struct to initialise
5378 * @dev: device providing this structure - for i2c addition.
5379 * @aux: DP helper aux channel to talk to this device
5380 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
5381 * @max_payloads: maximum number of payloads this GPU can source
5382 * @conn_base_id: the connector object ID the MST device is connected to.
5384 * Return 0 for success, or negative error code on failure
5386 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr
*mgr
,
5387 struct drm_device
*dev
, struct drm_dp_aux
*aux
,
5388 int max_dpcd_transaction_bytes
,
5389 int max_payloads
, int conn_base_id
)
5391 struct drm_dp_mst_topology_state
*mst_state
;
5393 mutex_init(&mgr
->lock
);
5394 mutex_init(&mgr
->qlock
);
5395 mutex_init(&mgr
->payload_lock
);
5396 mutex_init(&mgr
->delayed_destroy_lock
);
5397 mutex_init(&mgr
->up_req_lock
);
5398 mutex_init(&mgr
->probe_lock
);
5399 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
5400 mutex_init(&mgr
->topology_ref_history_lock
);
5402 INIT_LIST_HEAD(&mgr
->tx_msg_downq
);
5403 INIT_LIST_HEAD(&mgr
->destroy_port_list
);
5404 INIT_LIST_HEAD(&mgr
->destroy_branch_device_list
);
5405 INIT_LIST_HEAD(&mgr
->up_req_list
);
5408 * delayed_destroy_work will be queued on a dedicated WQ, so that any
5409 * requeuing will be also flushed when deiniting the topology manager.
5411 mgr
->delayed_destroy_wq
= alloc_ordered_workqueue("drm_dp_mst_wq", 0);
5412 if (mgr
->delayed_destroy_wq
== NULL
)
5415 INIT_WORK(&mgr
->work
, drm_dp_mst_link_probe_work
);
5416 INIT_WORK(&mgr
->tx_work
, drm_dp_tx_work
);
5417 INIT_WORK(&mgr
->delayed_destroy_work
, drm_dp_delayed_destroy_work
);
5418 INIT_WORK(&mgr
->up_req_work
, drm_dp_mst_up_req_work
);
5419 init_waitqueue_head(&mgr
->tx_waitq
);
5422 mgr
->max_dpcd_transaction_bytes
= max_dpcd_transaction_bytes
;
5423 mgr
->max_payloads
= max_payloads
;
5424 mgr
->conn_base_id
= conn_base_id
;
5425 if (max_payloads
+ 1 > sizeof(mgr
->payload_mask
) * 8 ||
5426 max_payloads
+ 1 > sizeof(mgr
->vcpi_mask
) * 8)
5428 mgr
->payloads
= kcalloc(max_payloads
, sizeof(struct drm_dp_payload
), GFP_KERNEL
);
5431 mgr
->proposed_vcpis
= kcalloc(max_payloads
, sizeof(struct drm_dp_vcpi
*), GFP_KERNEL
);
5432 if (!mgr
->proposed_vcpis
)
5434 set_bit(0, &mgr
->payload_mask
);
5436 mst_state
= kzalloc(sizeof(*mst_state
), GFP_KERNEL
);
5437 if (mst_state
== NULL
)
5440 mst_state
->mgr
= mgr
;
5441 INIT_LIST_HEAD(&mst_state
->vcpis
);
5443 drm_atomic_private_obj_init(dev
, &mgr
->base
,
5445 &drm_dp_mst_topology_state_funcs
);
5449 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init
);
5452 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
5453 * @mgr: manager to destroy
5455 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr
*mgr
)
5457 drm_dp_mst_topology_mgr_set_mst(mgr
, false);
5458 flush_work(&mgr
->work
);
5459 /* The following will also drain any requeued work on the WQ. */
5460 if (mgr
->delayed_destroy_wq
) {
5461 destroy_workqueue(mgr
->delayed_destroy_wq
);
5462 mgr
->delayed_destroy_wq
= NULL
;
5464 mutex_lock(&mgr
->payload_lock
);
5465 kfree(mgr
->payloads
);
5466 mgr
->payloads
= NULL
;
5467 kfree(mgr
->proposed_vcpis
);
5468 mgr
->proposed_vcpis
= NULL
;
5469 mutex_unlock(&mgr
->payload_lock
);
5472 drm_atomic_private_obj_fini(&mgr
->base
);
5475 mutex_destroy(&mgr
->delayed_destroy_lock
);
5476 mutex_destroy(&mgr
->payload_lock
);
5477 mutex_destroy(&mgr
->qlock
);
5478 mutex_destroy(&mgr
->lock
);
5479 mutex_destroy(&mgr
->up_req_lock
);
5480 mutex_destroy(&mgr
->probe_lock
);
5481 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
5482 mutex_destroy(&mgr
->topology_ref_history_lock
);
5485 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy
);
5487 static bool remote_i2c_read_ok(const struct i2c_msg msgs
[], int num
)
5491 if (num
- 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS
)
5494 for (i
= 0; i
< num
- 1; i
++) {
5495 if (msgs
[i
].flags
& I2C_M_RD
||
5500 return msgs
[num
- 1].flags
& I2C_M_RD
&&
5501 msgs
[num
- 1].len
<= 0xff;
5504 static bool remote_i2c_write_ok(const struct i2c_msg msgs
[], int num
)
5508 for (i
= 0; i
< num
- 1; i
++) {
5509 if (msgs
[i
].flags
& I2C_M_RD
|| !(msgs
[i
].flags
& I2C_M_STOP
) ||
5514 return !(msgs
[num
- 1].flags
& I2C_M_RD
) && msgs
[num
- 1].len
<= 0xff;
5517 static int drm_dp_mst_i2c_read(struct drm_dp_mst_branch
*mstb
,
5518 struct drm_dp_mst_port
*port
,
5519 struct i2c_msg
*msgs
, int num
)
5521 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
5523 struct drm_dp_sideband_msg_req_body msg
;
5524 struct drm_dp_sideband_msg_tx
*txmsg
= NULL
;
5527 memset(&msg
, 0, sizeof(msg
));
5528 msg
.req_type
= DP_REMOTE_I2C_READ
;
5529 msg
.u
.i2c_read
.num_transactions
= num
- 1;
5530 msg
.u
.i2c_read
.port_number
= port
->port_num
;
5531 for (i
= 0; i
< num
- 1; i
++) {
5532 msg
.u
.i2c_read
.transactions
[i
].i2c_dev_id
= msgs
[i
].addr
;
5533 msg
.u
.i2c_read
.transactions
[i
].num_bytes
= msgs
[i
].len
;
5534 msg
.u
.i2c_read
.transactions
[i
].bytes
= msgs
[i
].buf
;
5535 msg
.u
.i2c_read
.transactions
[i
].no_stop_bit
= !(msgs
[i
].flags
& I2C_M_STOP
);
5537 msg
.u
.i2c_read
.read_i2c_device_id
= msgs
[num
- 1].addr
;
5538 msg
.u
.i2c_read
.num_bytes_read
= msgs
[num
- 1].len
;
5540 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
5547 drm_dp_encode_sideband_req(&msg
, txmsg
);
5549 drm_dp_queue_down_tx(mgr
, txmsg
);
5551 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
5554 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
) {
5558 if (txmsg
->reply
.u
.remote_i2c_read_ack
.num_bytes
!= msgs
[num
- 1].len
) {
5562 memcpy(msgs
[num
- 1].buf
, txmsg
->reply
.u
.remote_i2c_read_ack
.bytes
, msgs
[num
- 1].len
);
5570 static int drm_dp_mst_i2c_write(struct drm_dp_mst_branch
*mstb
,
5571 struct drm_dp_mst_port
*port
,
5572 struct i2c_msg
*msgs
, int num
)
5574 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
5576 struct drm_dp_sideband_msg_req_body msg
;
5577 struct drm_dp_sideband_msg_tx
*txmsg
= NULL
;
5580 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
5585 for (i
= 0; i
< num
; i
++) {
5586 memset(&msg
, 0, sizeof(msg
));
5587 msg
.req_type
= DP_REMOTE_I2C_WRITE
;
5588 msg
.u
.i2c_write
.port_number
= port
->port_num
;
5589 msg
.u
.i2c_write
.write_i2c_device_id
= msgs
[i
].addr
;
5590 msg
.u
.i2c_write
.num_bytes
= msgs
[i
].len
;
5591 msg
.u
.i2c_write
.bytes
= msgs
[i
].buf
;
5593 memset(txmsg
, 0, sizeof(*txmsg
));
5596 drm_dp_encode_sideband_req(&msg
, txmsg
);
5597 drm_dp_queue_down_tx(mgr
, txmsg
);
5599 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
5601 if (txmsg
->reply
.reply_type
== DP_SIDEBAND_REPLY_NAK
) {
5616 static int drm_dp_mst_i2c_xfer(struct i2c_adapter
*adapter
,
5617 struct i2c_msg
*msgs
, int num
)
5619 struct drm_dp_aux
*aux
= adapter
->algo_data
;
5620 struct drm_dp_mst_port
*port
=
5621 container_of(aux
, struct drm_dp_mst_port
, aux
);
5622 struct drm_dp_mst_branch
*mstb
;
5623 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
5626 mstb
= drm_dp_mst_topology_get_mstb_validated(mgr
, port
->parent
);
5630 if (remote_i2c_read_ok(msgs
, num
)) {
5631 ret
= drm_dp_mst_i2c_read(mstb
, port
, msgs
, num
);
5632 } else if (remote_i2c_write_ok(msgs
, num
)) {
5633 ret
= drm_dp_mst_i2c_write(mstb
, port
, msgs
, num
);
5635 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
5639 drm_dp_mst_topology_put_mstb(mstb
);
5643 static u32
drm_dp_mst_i2c_functionality(struct i2c_adapter
*adapter
)
5645 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
5646 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
5647 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
5648 I2C_FUNC_10BIT_ADDR
;
5651 static const struct i2c_algorithm drm_dp_mst_i2c_algo
= {
5652 .functionality
= drm_dp_mst_i2c_functionality
,
5653 .master_xfer
= drm_dp_mst_i2c_xfer
,
5657 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
5658 * @port: The port to add the I2C bus on
5660 * Returns 0 on success or a negative error code on failure.
5662 static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port
*port
)
5664 struct drm_dp_aux
*aux
= &port
->aux
;
5665 struct device
*parent_dev
= port
->mgr
->dev
->dev
;
5667 aux
->ddc
.algo
= &drm_dp_mst_i2c_algo
;
5668 aux
->ddc
.algo_data
= aux
;
5669 aux
->ddc
.retries
= 3;
5671 aux
->ddc
.class = I2C_CLASS_DDC
;
5672 aux
->ddc
.owner
= THIS_MODULE
;
5673 /* FIXME: set the kdev of the port's connector as parent */
5674 aux
->ddc
.dev
.parent
= parent_dev
;
5675 aux
->ddc
.dev
.of_node
= parent_dev
->of_node
;
5677 strlcpy(aux
->ddc
.name
, aux
->name
? aux
->name
: dev_name(parent_dev
),
5678 sizeof(aux
->ddc
.name
));
5680 return i2c_add_adapter(&aux
->ddc
);
5684 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
5685 * @port: The port to remove the I2C bus from
5687 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port
*port
)
5689 i2c_del_adapter(&port
->aux
.ddc
);
5693 * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device
5694 * @port: The port to check
5696 * A single physical MST hub object can be represented in the topology
5697 * by multiple branches, with virtual ports between those branches.
5699 * As of DP1.4, An MST hub with internal (virtual) ports must expose
5700 * certain DPCD registers over those ports. See sections 2.6.1.1.1
5701 * and 2.6.1.1.2 of Display Port specification v1.4 for details.
5703 * May acquire mgr->lock
5706 * true if the port is a virtual DP peer device, false otherwise
5708 static bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port
*port
)
5710 struct drm_dp_mst_port
*downstream_port
;
5712 if (!port
|| port
->dpcd_rev
< DP_DPCD_REV_14
)
5715 /* Virtual DP Sink (Internal Display Panel) */
5716 if (port
->port_num
>= 8)
5719 /* DP-to-HDMI Protocol Converter */
5720 if (port
->pdt
== DP_PEER_DEVICE_DP_LEGACY_CONV
&&
5726 mutex_lock(&port
->mgr
->lock
);
5727 if (port
->pdt
== DP_PEER_DEVICE_MST_BRANCHING
&&
5729 port
->mstb
->num_ports
== 2) {
5730 list_for_each_entry(downstream_port
, &port
->mstb
->ports
, next
) {
5731 if (downstream_port
->pdt
== DP_PEER_DEVICE_SST_SINK
&&
5732 !downstream_port
->input
) {
5733 mutex_unlock(&port
->mgr
->lock
);
5738 mutex_unlock(&port
->mgr
->lock
);
5744 * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC
5745 * @port: The port to check. A leaf of the MST tree with an attached display.
5747 * Depending on the situation, DSC may be enabled via the endpoint aux,
5748 * the immediately upstream aux, or the connector's physical aux.
5750 * This is both the correct aux to read DSC_CAPABILITY and the
5751 * correct aux to write DSC_ENABLED.
5753 * This operation can be expensive (up to four aux reads), so
5754 * the caller should cache the return.
5757 * NULL if DSC cannot be enabled on this port, otherwise the aux device
5759 struct drm_dp_aux
*drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port
*port
)
5761 struct drm_dp_mst_port
*immediate_upstream_port
;
5762 struct drm_dp_mst_port
*fec_port
;
5763 struct drm_dp_desc desc
= {};
5770 if (port
->parent
->port_parent
)
5771 immediate_upstream_port
= port
->parent
->port_parent
;
5773 immediate_upstream_port
= NULL
;
5775 fec_port
= immediate_upstream_port
;
5778 * Each physical link (i.e. not a virtual port) between the
5779 * output and the primary device must support FEC
5781 if (!drm_dp_mst_is_virtual_dpcd(fec_port
) &&
5782 !fec_port
->fec_capable
)
5785 fec_port
= fec_port
->parent
->port_parent
;
5788 /* DP-to-DP peer device */
5789 if (drm_dp_mst_is_virtual_dpcd(immediate_upstream_port
)) {
5792 if (drm_dp_dpcd_read(&port
->aux
,
5793 DP_DSC_SUPPORT
, &endpoint_dsc
, 1) != 1)
5795 if (drm_dp_dpcd_read(&port
->aux
,
5796 DP_FEC_CAPABILITY
, &endpoint_fec
, 1) != 1)
5798 if (drm_dp_dpcd_read(&immediate_upstream_port
->aux
,
5799 DP_DSC_SUPPORT
, &upstream_dsc
, 1) != 1)
5802 /* Enpoint decompression with DP-to-DP peer device */
5803 if ((endpoint_dsc
& DP_DSC_DECOMPRESSION_IS_SUPPORTED
) &&
5804 (endpoint_fec
& DP_FEC_CAPABLE
) &&
5805 (upstream_dsc
& 0x2) /* DSC passthrough */)
5808 /* Virtual DPCD decompression with DP-to-DP peer device */
5809 return &immediate_upstream_port
->aux
;
5812 /* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */
5813 if (drm_dp_mst_is_virtual_dpcd(port
))
5818 * Applies to ports for which:
5819 * - Physical aux has Synaptics OUI
5820 * - DPv1.4 or higher
5821 * - Port is on primary branch device
5822 * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG)
5824 if (drm_dp_read_desc(port
->mgr
->aux
, &desc
, true))
5827 if (drm_dp_has_quirk(&desc
, 0,
5828 DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD
) &&
5829 port
->mgr
->dpcd
[DP_DPCD_REV
] >= DP_DPCD_REV_14
&&
5830 port
->parent
== port
->mgr
->mst_primary
) {
5833 if (drm_dp_dpcd_read(&port
->aux
, DP_DOWNSTREAMPORT_PRESENT
,
5834 &downstreamport
, 1) < 0)
5837 if ((downstreamport
& DP_DWN_STRM_PORT_PRESENT
) &&
5838 ((downstreamport
& DP_DWN_STRM_PORT_TYPE_MASK
)
5839 != DP_DWN_STRM_PORT_TYPE_ANALOG
))
5840 return port
->mgr
->aux
;
5844 * The check below verifies if the MST sink
5845 * connected to the GPU is capable of DSC -
5846 * therefore the endpoint needs to be
5847 * both DSC and FEC capable.
5849 if (drm_dp_dpcd_read(&port
->aux
,
5850 DP_DSC_SUPPORT
, &endpoint_dsc
, 1) != 1)
5852 if (drm_dp_dpcd_read(&port
->aux
,
5853 DP_FEC_CAPABILITY
, &endpoint_fec
, 1) != 1)
5855 if ((endpoint_dsc
& DP_DSC_DECOMPRESSION_IS_SUPPORTED
) &&
5856 (endpoint_fec
& DP_FEC_CAPABLE
))
5861 EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port
);