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/delay.h>
24 #include <linux/errno.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/seq_file.h>
30 #include <linux/iopoll.h>
32 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
33 #include <linux/stacktrace.h>
34 #include <linux/sort.h>
35 #include <linux/timekeeping.h>
36 #include <linux/math64.h>
39 #include <drm/drm_atomic.h>
40 #include <drm/drm_atomic_helper.h>
41 #include <drm/drm_dp_mst_helper.h>
42 #include <drm/drm_drv.h>
43 #include <drm/drm_print.h>
44 #include <drm/drm_probe_helper.h>
46 #include "drm_crtc_helper_internal.h"
47 #include "drm_dp_mst_topology_internal.h"
52 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
53 * protocol. The helpers contain a topology manager and bandwidth manager.
54 * The helpers encapsulate the sending and received of sideband msgs.
56 struct drm_dp_pending_up_req {
57 struct drm_dp_sideband_msg_hdr hdr;
58 struct drm_dp_sideband_msg_req_body msg;
59 struct list_head next;
62 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
65 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
67 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
69 struct drm_dp_payload *payload);
71 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
72 struct drm_dp_mst_port *port,
73 int offset, int size, u8 *bytes);
74 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
75 struct drm_dp_mst_port *port,
76 int offset, int size, u8 *bytes);
78 static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
79 struct drm_dp_mst_branch *mstb);
82 drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
83 struct drm_dp_mst_branch *mstb);
85 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
86 struct drm_dp_mst_branch *mstb,
87 struct drm_dp_mst_port *port);
88 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
91 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
92 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
93 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
95 #define DBG_PREFIX "[dp_mst]"
97 #define DP_STR(x) [DP_ ## x] = #x
99 static const char *drm_dp_mst_req_type_str(u8 req_type)
101 static const char * const req_type_str[] = {
102 DP_STR(GET_MSG_TRANSACTION_VERSION),
103 DP_STR(LINK_ADDRESS),
104 DP_STR(CONNECTION_STATUS_NOTIFY),
105 DP_STR(ENUM_PATH_RESOURCES),
106 DP_STR(ALLOCATE_PAYLOAD),
107 DP_STR(QUERY_PAYLOAD),
108 DP_STR(RESOURCE_STATUS_NOTIFY),
109 DP_STR(CLEAR_PAYLOAD_ID_TABLE),
110 DP_STR(REMOTE_DPCD_READ),
111 DP_STR(REMOTE_DPCD_WRITE),
112 DP_STR(REMOTE_I2C_READ),
113 DP_STR(REMOTE_I2C_WRITE),
114 DP_STR(POWER_UP_PHY),
115 DP_STR(POWER_DOWN_PHY),
116 DP_STR(SINK_EVENT_NOTIFY),
117 DP_STR(QUERY_STREAM_ENC_STATUS),
120 if (req_type >= ARRAY_SIZE(req_type_str) ||
121 !req_type_str[req_type])
124 return req_type_str[req_type];
128 #define DP_STR(x) [DP_NAK_ ## x] = #x
130 static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
132 static const char * const nak_reason_str[] = {
133 DP_STR(WRITE_FAILURE),
134 DP_STR(INVALID_READ),
138 DP_STR(LINK_FAILURE),
139 DP_STR(NO_RESOURCES),
142 DP_STR(ALLOCATE_FAIL),
145 if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
146 !nak_reason_str[nak_reason])
149 return nak_reason_str[nak_reason];
153 #define DP_STR(x) [DRM_DP_SIDEBAND_TX_ ## x] = #x
155 static const char *drm_dp_mst_sideband_tx_state_str(int state)
157 static const char * const sideband_reason_str[] = {
165 if (state >= ARRAY_SIZE(sideband_reason_str) ||
166 !sideband_reason_str[state])
169 return sideband_reason_str[state];
173 drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len)
178 for (i = 0; i < lct; i++) {
180 unpacked_rad[i] = rad[i / 2] >> 4;
182 unpacked_rad[i] = rad[i / 2] & BIT_MASK(4);
185 /* TODO: Eventually add something to printk so we can format the rad
188 return snprintf(out, len, "%*phC", lct, unpacked_rad);
191 /* sideband msg handling */
192 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
197 int number_of_bits = num_nibbles * 4;
200 while (number_of_bits != 0) {
203 remainder |= (data[array_index] & bitmask) >> bitshift;
211 if ((remainder & 0x10) == 0x10)
216 while (number_of_bits != 0) {
219 if ((remainder & 0x10) != 0)
226 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
231 int number_of_bits = number_of_bytes * 8;
234 while (number_of_bits != 0) {
237 remainder |= (data[array_index] & bitmask) >> bitshift;
245 if ((remainder & 0x100) == 0x100)
250 while (number_of_bits != 0) {
253 if ((remainder & 0x100) != 0)
257 return remainder & 0xff;
259 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
262 size += (hdr->lct / 2);
266 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
272 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
273 for (i = 0; i < (hdr->lct / 2); i++)
274 buf[idx++] = hdr->rad[i];
275 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
276 (hdr->msg_len & 0x3f);
277 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
279 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
280 buf[idx - 1] |= (crc4 & 0xf);
285 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
286 u8 *buf, int buflen, u8 *hdrlen)
295 len += ((buf[0] & 0xf0) >> 4) / 2;
298 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
300 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
301 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
305 hdr->lct = (buf[0] & 0xf0) >> 4;
306 hdr->lcr = (buf[0] & 0xf);
308 for (i = 0; i < (hdr->lct / 2); i++)
309 hdr->rad[i] = buf[idx++];
310 hdr->broadcast = (buf[idx] >> 7) & 0x1;
311 hdr->path_msg = (buf[idx] >> 6) & 0x1;
312 hdr->msg_len = buf[idx] & 0x3f;
314 hdr->somt = (buf[idx] >> 7) & 0x1;
315 hdr->eomt = (buf[idx] >> 6) & 0x1;
316 hdr->seqno = (buf[idx] >> 4) & 0x1;
323 drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body *req,
324 struct drm_dp_sideband_msg_tx *raw)
329 buf[idx++] = req->req_type & 0x7f;
331 switch (req->req_type) {
332 case DP_ENUM_PATH_RESOURCES:
333 case DP_POWER_DOWN_PHY:
334 case DP_POWER_UP_PHY:
335 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
338 case DP_ALLOCATE_PAYLOAD:
339 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
340 (req->u.allocate_payload.number_sdp_streams & 0xf);
342 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
344 buf[idx] = (req->u.allocate_payload.pbn >> 8);
346 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
348 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
349 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
350 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
353 if (req->u.allocate_payload.number_sdp_streams & 1) {
354 i = req->u.allocate_payload.number_sdp_streams - 1;
355 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
359 case DP_QUERY_PAYLOAD:
360 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
362 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
365 case DP_REMOTE_DPCD_READ:
366 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
367 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
369 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
371 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
373 buf[idx] = (req->u.dpcd_read.num_bytes);
377 case DP_REMOTE_DPCD_WRITE:
378 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
379 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
381 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
383 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
385 buf[idx] = (req->u.dpcd_write.num_bytes);
387 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
388 idx += req->u.dpcd_write.num_bytes;
390 case DP_REMOTE_I2C_READ:
391 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
392 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
394 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
395 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
397 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
399 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
400 idx += req->u.i2c_read.transactions[i].num_bytes;
402 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 4;
403 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
406 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
408 buf[idx] = (req->u.i2c_read.num_bytes_read);
412 case DP_REMOTE_I2C_WRITE:
413 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
415 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
417 buf[idx] = (req->u.i2c_write.num_bytes);
419 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
420 idx += req->u.i2c_write.num_bytes;
425 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req);
427 /* Decode a sideband request we've encoded, mainly used for debugging */
429 drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx *raw,
430 struct drm_dp_sideband_msg_req_body *req)
432 const u8 *buf = raw->msg;
435 req->req_type = buf[idx++] & 0x7f;
436 switch (req->req_type) {
437 case DP_ENUM_PATH_RESOURCES:
438 case DP_POWER_DOWN_PHY:
439 case DP_POWER_UP_PHY:
440 req->u.port_num.port_number = (buf[idx] >> 4) & 0xf;
442 case DP_ALLOCATE_PAYLOAD:
444 struct drm_dp_allocate_payload *a =
445 &req->u.allocate_payload;
447 a->number_sdp_streams = buf[idx] & 0xf;
448 a->port_number = (buf[idx] >> 4) & 0xf;
450 WARN_ON(buf[++idx] & 0x80);
451 a->vcpi = buf[idx] & 0x7f;
453 a->pbn = buf[++idx] << 8;
454 a->pbn |= buf[++idx];
457 for (i = 0; i < a->number_sdp_streams; i++) {
458 a->sdp_stream_sink[i] =
459 (buf[idx + (i / 2)] >> ((i % 2) ? 0 : 4)) & 0xf;
463 case DP_QUERY_PAYLOAD:
464 req->u.query_payload.port_number = (buf[idx] >> 4) & 0xf;
465 WARN_ON(buf[++idx] & 0x80);
466 req->u.query_payload.vcpi = buf[idx] & 0x7f;
468 case DP_REMOTE_DPCD_READ:
470 struct drm_dp_remote_dpcd_read *r = &req->u.dpcd_read;
472 r->port_number = (buf[idx] >> 4) & 0xf;
474 r->dpcd_address = (buf[idx] << 16) & 0xf0000;
475 r->dpcd_address |= (buf[++idx] << 8) & 0xff00;
476 r->dpcd_address |= buf[++idx] & 0xff;
478 r->num_bytes = buf[++idx];
481 case DP_REMOTE_DPCD_WRITE:
483 struct drm_dp_remote_dpcd_write *w =
486 w->port_number = (buf[idx] >> 4) & 0xf;
488 w->dpcd_address = (buf[idx] << 16) & 0xf0000;
489 w->dpcd_address |= (buf[++idx] << 8) & 0xff00;
490 w->dpcd_address |= buf[++idx] & 0xff;
492 w->num_bytes = buf[++idx];
494 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
500 case DP_REMOTE_I2C_READ:
502 struct drm_dp_remote_i2c_read *r = &req->u.i2c_read;
503 struct drm_dp_remote_i2c_read_tx *tx;
506 r->num_transactions = buf[idx] & 0x3;
507 r->port_number = (buf[idx] >> 4) & 0xf;
508 for (i = 0; i < r->num_transactions; i++) {
509 tx = &r->transactions[i];
511 tx->i2c_dev_id = buf[++idx] & 0x7f;
512 tx->num_bytes = buf[++idx];
513 tx->bytes = kmemdup(&buf[++idx],
520 idx += tx->num_bytes;
521 tx->no_stop_bit = (buf[idx] >> 5) & 0x1;
522 tx->i2c_transaction_delay = buf[idx] & 0xf;
526 for (i = 0; i < r->num_transactions; i++) {
527 tx = &r->transactions[i];
533 r->read_i2c_device_id = buf[++idx] & 0x7f;
534 r->num_bytes_read = buf[++idx];
537 case DP_REMOTE_I2C_WRITE:
539 struct drm_dp_remote_i2c_write *w = &req->u.i2c_write;
541 w->port_number = (buf[idx] >> 4) & 0xf;
542 w->write_i2c_device_id = buf[++idx] & 0x7f;
543 w->num_bytes = buf[++idx];
544 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
554 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req);
557 drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body *req,
558 int indent, struct drm_printer *printer)
562 #define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)
563 if (req->req_type == DP_LINK_ADDRESS) {
564 /* No contents to print */
565 P("type=%s\n", drm_dp_mst_req_type_str(req->req_type));
569 P("type=%s contents:\n", drm_dp_mst_req_type_str(req->req_type));
572 switch (req->req_type) {
573 case DP_ENUM_PATH_RESOURCES:
574 case DP_POWER_DOWN_PHY:
575 case DP_POWER_UP_PHY:
576 P("port=%d\n", req->u.port_num.port_number);
578 case DP_ALLOCATE_PAYLOAD:
579 P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
580 req->u.allocate_payload.port_number,
581 req->u.allocate_payload.vcpi, req->u.allocate_payload.pbn,
582 req->u.allocate_payload.number_sdp_streams,
583 req->u.allocate_payload.number_sdp_streams,
584 req->u.allocate_payload.sdp_stream_sink);
586 case DP_QUERY_PAYLOAD:
587 P("port=%d vcpi=%d\n",
588 req->u.query_payload.port_number,
589 req->u.query_payload.vcpi);
591 case DP_REMOTE_DPCD_READ:
592 P("port=%d dpcd_addr=%05x len=%d\n",
593 req->u.dpcd_read.port_number, req->u.dpcd_read.dpcd_address,
594 req->u.dpcd_read.num_bytes);
596 case DP_REMOTE_DPCD_WRITE:
597 P("port=%d addr=%05x len=%d: %*ph\n",
598 req->u.dpcd_write.port_number,
599 req->u.dpcd_write.dpcd_address,
600 req->u.dpcd_write.num_bytes, req->u.dpcd_write.num_bytes,
601 req->u.dpcd_write.bytes);
603 case DP_REMOTE_I2C_READ:
604 P("port=%d num_tx=%d id=%d size=%d:\n",
605 req->u.i2c_read.port_number,
606 req->u.i2c_read.num_transactions,
607 req->u.i2c_read.read_i2c_device_id,
608 req->u.i2c_read.num_bytes_read);
611 for (i = 0; i < req->u.i2c_read.num_transactions; i++) {
612 const struct drm_dp_remote_i2c_read_tx *rtx =
613 &req->u.i2c_read.transactions[i];
615 P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
616 i, rtx->i2c_dev_id, rtx->num_bytes,
617 rtx->no_stop_bit, rtx->i2c_transaction_delay,
618 rtx->num_bytes, rtx->bytes);
621 case DP_REMOTE_I2C_WRITE:
622 P("port=%d id=%d size=%d: %*ph\n",
623 req->u.i2c_write.port_number,
624 req->u.i2c_write.write_i2c_device_id,
625 req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
626 req->u.i2c_write.bytes);
634 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body);
637 drm_dp_mst_dump_sideband_msg_tx(struct drm_printer *p,
638 const struct drm_dp_sideband_msg_tx *txmsg)
640 struct drm_dp_sideband_msg_req_body req;
645 drm_dp_mst_rad_to_str(txmsg->dst->rad, txmsg->dst->lct, buf,
647 drm_printf(p, "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
648 txmsg->cur_offset, txmsg->cur_len, txmsg->seqno,
649 drm_dp_mst_sideband_tx_state_str(txmsg->state),
650 txmsg->path_msg, buf);
652 ret = drm_dp_decode_sideband_req(txmsg, &req);
654 drm_printf(p, "<failed to decode sideband req: %d>\n", ret);
657 drm_dp_dump_sideband_msg_req_body(&req, 1, p);
659 switch (req.req_type) {
660 case DP_REMOTE_DPCD_WRITE:
661 kfree(req.u.dpcd_write.bytes);
663 case DP_REMOTE_I2C_READ:
664 for (i = 0; i < req.u.i2c_read.num_transactions; i++)
665 kfree(req.u.i2c_read.transactions[i].bytes);
667 case DP_REMOTE_I2C_WRITE:
668 kfree(req.u.i2c_write.bytes);
673 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
676 crc4 = drm_dp_msg_data_crc4(msg, len);
680 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
681 struct drm_dp_sideband_msg_tx *raw)
686 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
691 static int drm_dp_sideband_msg_set_header(struct drm_dp_sideband_msg_rx *msg,
692 struct drm_dp_sideband_msg_hdr *hdr,
696 * ignore out-of-order messages or messages that are part of a
699 if (!hdr->somt && !msg->have_somt)
702 /* get length contained in this portion */
703 msg->curchunk_idx = 0;
704 msg->curchunk_len = hdr->msg_len;
705 msg->curchunk_hdrlen = hdrlen;
707 /* we have already gotten an somt - don't bother parsing */
708 if (hdr->somt && msg->have_somt)
712 memcpy(&msg->initial_hdr, hdr,
713 sizeof(struct drm_dp_sideband_msg_hdr));
714 msg->have_somt = true;
717 msg->have_eomt = true;
722 /* this adds a chunk of msg to the builder to get the final msg */
723 static bool drm_dp_sideband_append_payload(struct drm_dp_sideband_msg_rx *msg,
724 u8 *replybuf, u8 replybuflen)
728 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
729 msg->curchunk_idx += replybuflen;
731 if (msg->curchunk_idx >= msg->curchunk_len) {
733 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
734 if (crc4 != msg->chunk[msg->curchunk_len - 1])
735 print_hex_dump(KERN_DEBUG, "wrong crc",
736 DUMP_PREFIX_NONE, 16, 1,
737 msg->chunk, msg->curchunk_len, false);
738 /* copy chunk into bigger msg */
739 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
740 msg->curlen += msg->curchunk_len - 1;
745 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
746 struct drm_dp_sideband_msg_reply_body *repmsg)
750 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
752 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
754 if (idx > raw->curlen)
756 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
757 if (raw->msg[idx] & 0x80)
758 repmsg->u.link_addr.ports[i].input_port = 1;
760 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
761 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
764 if (idx > raw->curlen)
766 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
767 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
768 if (repmsg->u.link_addr.ports[i].input_port == 0)
769 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
771 if (idx > raw->curlen)
773 if (repmsg->u.link_addr.ports[i].input_port == 0) {
774 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
776 if (idx > raw->curlen)
778 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
780 if (idx > raw->curlen)
782 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
783 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
787 if (idx > raw->curlen)
793 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
797 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
798 struct drm_dp_sideband_msg_reply_body *repmsg)
801 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
803 if (idx > raw->curlen)
805 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
807 if (idx > raw->curlen)
810 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
813 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
817 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
818 struct drm_dp_sideband_msg_reply_body *repmsg)
821 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
823 if (idx > raw->curlen)
827 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
831 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
832 struct drm_dp_sideband_msg_reply_body *repmsg)
836 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
838 if (idx > raw->curlen)
840 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
843 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
846 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
850 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
851 struct drm_dp_sideband_msg_reply_body *repmsg)
854 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
855 repmsg->u.path_resources.fec_capable = raw->msg[idx] & 0x1;
857 if (idx > raw->curlen)
859 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
861 if (idx > raw->curlen)
863 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
865 if (idx > raw->curlen)
869 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
873 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
874 struct drm_dp_sideband_msg_reply_body *repmsg)
877 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
879 if (idx > raw->curlen)
881 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
883 if (idx > raw->curlen)
885 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
887 if (idx > raw->curlen)
891 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
895 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
896 struct drm_dp_sideband_msg_reply_body *repmsg)
899 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
901 if (idx > raw->curlen)
903 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
905 if (idx > raw->curlen)
909 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
913 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
914 struct drm_dp_sideband_msg_reply_body *repmsg)
918 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
920 if (idx > raw->curlen) {
921 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
928 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
929 struct drm_dp_sideband_msg_reply_body *msg)
931 memset(msg, 0, sizeof(*msg));
932 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
933 msg->req_type = (raw->msg[0] & 0x7f);
935 if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
936 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
937 msg->u.nak.reason = raw->msg[17];
938 msg->u.nak.nak_data = raw->msg[18];
942 switch (msg->req_type) {
943 case DP_LINK_ADDRESS:
944 return drm_dp_sideband_parse_link_address(raw, msg);
945 case DP_QUERY_PAYLOAD:
946 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
947 case DP_REMOTE_DPCD_READ:
948 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
949 case DP_REMOTE_DPCD_WRITE:
950 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
951 case DP_REMOTE_I2C_READ:
952 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
953 case DP_ENUM_PATH_RESOURCES:
954 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
955 case DP_ALLOCATE_PAYLOAD:
956 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
957 case DP_POWER_DOWN_PHY:
958 case DP_POWER_UP_PHY:
959 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
960 case DP_CLEAR_PAYLOAD_ID_TABLE:
961 return true; /* since there's nothing to parse */
963 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
964 drm_dp_mst_req_type_str(msg->req_type));
969 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
970 struct drm_dp_sideband_msg_req_body *msg)
974 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
976 if (idx > raw->curlen)
979 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
981 if (idx > raw->curlen)
984 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
985 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
986 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
987 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
988 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
992 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
996 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
997 struct drm_dp_sideband_msg_req_body *msg)
1001 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
1003 if (idx > raw->curlen)
1006 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
1008 if (idx > raw->curlen)
1011 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
1015 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
1019 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
1020 struct drm_dp_sideband_msg_req_body *msg)
1022 memset(msg, 0, sizeof(*msg));
1023 msg->req_type = (raw->msg[0] & 0x7f);
1025 switch (msg->req_type) {
1026 case DP_CONNECTION_STATUS_NOTIFY:
1027 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
1028 case DP_RESOURCE_STATUS_NOTIFY:
1029 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
1031 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,
1032 drm_dp_mst_req_type_str(msg->req_type));
1037 static void build_dpcd_write(struct drm_dp_sideband_msg_tx *msg,
1038 u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
1040 struct drm_dp_sideband_msg_req_body req;
1042 req.req_type = DP_REMOTE_DPCD_WRITE;
1043 req.u.dpcd_write.port_number = port_num;
1044 req.u.dpcd_write.dpcd_address = offset;
1045 req.u.dpcd_write.num_bytes = num_bytes;
1046 req.u.dpcd_write.bytes = bytes;
1047 drm_dp_encode_sideband_req(&req, msg);
1050 static void build_link_address(struct drm_dp_sideband_msg_tx *msg)
1052 struct drm_dp_sideband_msg_req_body req;
1054 req.req_type = DP_LINK_ADDRESS;
1055 drm_dp_encode_sideband_req(&req, msg);
1058 static void build_clear_payload_id_table(struct drm_dp_sideband_msg_tx *msg)
1060 struct drm_dp_sideband_msg_req_body req;
1062 req.req_type = DP_CLEAR_PAYLOAD_ID_TABLE;
1063 drm_dp_encode_sideband_req(&req, msg);
1066 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg,
1069 struct drm_dp_sideband_msg_req_body req;
1071 req.req_type = DP_ENUM_PATH_RESOURCES;
1072 req.u.port_num.port_number = port_num;
1073 drm_dp_encode_sideband_req(&req, msg);
1074 msg->path_msg = true;
1078 static void build_allocate_payload(struct drm_dp_sideband_msg_tx *msg,
1080 u8 vcpi, uint16_t pbn,
1081 u8 number_sdp_streams,
1082 u8 *sdp_stream_sink)
1084 struct drm_dp_sideband_msg_req_body req;
1085 memset(&req, 0, sizeof(req));
1086 req.req_type = DP_ALLOCATE_PAYLOAD;
1087 req.u.allocate_payload.port_number = port_num;
1088 req.u.allocate_payload.vcpi = vcpi;
1089 req.u.allocate_payload.pbn = pbn;
1090 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
1091 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
1092 number_sdp_streams);
1093 drm_dp_encode_sideband_req(&req, msg);
1094 msg->path_msg = true;
1097 static void build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
1098 int port_num, bool power_up)
1100 struct drm_dp_sideband_msg_req_body req;
1103 req.req_type = DP_POWER_UP_PHY;
1105 req.req_type = DP_POWER_DOWN_PHY;
1107 req.u.port_num.port_number = port_num;
1108 drm_dp_encode_sideband_req(&req, msg);
1109 msg->path_msg = true;
1112 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1113 struct drm_dp_vcpi *vcpi)
1117 mutex_lock(&mgr->payload_lock);
1118 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
1119 if (ret > mgr->max_payloads) {
1121 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
1125 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
1126 if (vcpi_ret > mgr->max_payloads) {
1128 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
1132 set_bit(ret, &mgr->payload_mask);
1133 set_bit(vcpi_ret, &mgr->vcpi_mask);
1134 vcpi->vcpi = vcpi_ret + 1;
1135 mgr->proposed_vcpis[ret - 1] = vcpi;
1137 mutex_unlock(&mgr->payload_lock);
1141 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1148 mutex_lock(&mgr->payload_lock);
1149 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
1150 clear_bit(vcpi - 1, &mgr->vcpi_mask);
1152 for (i = 0; i < mgr->max_payloads; i++) {
1153 if (mgr->proposed_vcpis[i] &&
1154 mgr->proposed_vcpis[i]->vcpi == vcpi) {
1155 mgr->proposed_vcpis[i] = NULL;
1156 clear_bit(i + 1, &mgr->payload_mask);
1159 mutex_unlock(&mgr->payload_lock);
1162 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
1163 struct drm_dp_sideband_msg_tx *txmsg)
1168 * All updates to txmsg->state are protected by mgr->qlock, and the two
1169 * cases we check here are terminal states. For those the barriers
1170 * provided by the wake_up/wait_event pair are enough.
1172 state = READ_ONCE(txmsg->state);
1173 return (state == DRM_DP_SIDEBAND_TX_RX ||
1174 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
1177 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
1178 struct drm_dp_sideband_msg_tx *txmsg)
1180 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1183 ret = wait_event_timeout(mgr->tx_waitq,
1184 check_txmsg_state(mgr, txmsg),
1186 mutex_lock(&mstb->mgr->qlock);
1188 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
1193 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
1195 /* dump some state */
1199 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
1200 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND)
1201 list_del(&txmsg->next);
1204 if (unlikely(ret == -EIO) && drm_debug_enabled(DRM_UT_DP)) {
1205 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
1207 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
1209 mutex_unlock(&mgr->qlock);
1211 drm_dp_mst_kick_tx(mgr);
1215 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
1217 struct drm_dp_mst_branch *mstb;
1219 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
1225 memcpy(mstb->rad, rad, lct / 2);
1226 INIT_LIST_HEAD(&mstb->ports);
1227 kref_init(&mstb->topology_kref);
1228 kref_init(&mstb->malloc_kref);
1232 static void drm_dp_free_mst_branch_device(struct kref *kref)
1234 struct drm_dp_mst_branch *mstb =
1235 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
1237 if (mstb->port_parent)
1238 drm_dp_mst_put_port_malloc(mstb->port_parent);
1244 * DOC: Branch device and port refcounting
1246 * Topology refcount overview
1247 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
1249 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
1250 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
1251 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
1253 * Topology refcounts are not exposed to drivers, and are handled internally
1254 * by the DP MST helpers. The helpers use them in order to prevent the
1255 * in-memory topology state from being changed in the middle of critical
1256 * operations like changing the internal state of payload allocations. This
1257 * means each branch and port will be considered to be connected to the rest
1258 * of the topology until its topology refcount reaches zero. Additionally,
1259 * for ports this means that their associated &struct drm_connector will stay
1260 * registered with userspace until the port's refcount reaches 0.
1262 * Malloc refcount overview
1263 * ~~~~~~~~~~~~~~~~~~~~~~~~
1265 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
1266 * drm_dp_mst_branch allocated even after all of its topology references have
1267 * been dropped, so that the driver or MST helpers can safely access each
1268 * branch's last known state before it was disconnected from the topology.
1269 * When the malloc refcount of a port or branch reaches 0, the memory
1270 * allocation containing the &struct drm_dp_mst_branch or &struct
1271 * drm_dp_mst_port respectively will be freed.
1273 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
1274 * to drivers. As of writing this documentation, there are no drivers that
1275 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
1276 * helpers. Exposing this API to drivers in a race-free manner would take more
1277 * tweaking of the refcounting scheme, however patches are welcome provided
1278 * there is a legitimate driver usecase for this.
1280 * Refcount relationships in a topology
1281 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1283 * Let's take a look at why the relationship between topology and malloc
1284 * refcounts is designed the way it is.
1286 * .. kernel-figure:: dp-mst/topology-figure-1.dot
1288 * An example of topology and malloc refs in a DP MST topology with two
1289 * active payloads. Topology refcount increments are indicated by solid
1290 * lines, and malloc refcount increments are indicated by dashed lines.
1291 * Each starts from the branch which incremented the refcount, and ends at
1292 * the branch to which the refcount belongs to, i.e. the arrow points the
1293 * same way as the C pointers used to reference a structure.
1295 * As you can see in the above figure, every branch increments the topology
1296 * refcount of its children, and increments the malloc refcount of its
1297 * parent. Additionally, every payload increments the malloc refcount of its
1298 * assigned port by 1.
1300 * So, what would happen if MSTB #3 from the above figure was unplugged from
1301 * the system, but the driver hadn't yet removed payload #2 from port #3? The
1302 * topology would start to look like the figure below.
1304 * .. kernel-figure:: dp-mst/topology-figure-2.dot
1306 * Ports and branch devices which have been released from memory are
1307 * colored grey, and references which have been removed are colored red.
1309 * Whenever a port or branch device's topology refcount reaches zero, it will
1310 * decrement the topology refcounts of all its children, the malloc refcount
1311 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
1312 * #4, this means they both have been disconnected from the topology and freed
1313 * from memory. But, because payload #2 is still holding a reference to port
1314 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1315 * is still accessible from memory. This also means port #3 has not yet
1316 * decremented the malloc refcount of MSTB #3, so its &struct
1317 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1318 * malloc refcount reaches 0.
1320 * This relationship is necessary because in order to release payload #2, we
1321 * need to be able to figure out the last relative of port #3 that's still
1322 * connected to the topology. In this case, we would travel up the topology as
1325 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1327 * And finally, remove payload #2 by communicating with port #2 through
1328 * sideband transactions.
1332 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1334 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1336 * Increments &drm_dp_mst_branch.malloc_kref. When
1337 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1338 * will be released and @mstb may no longer be used.
1340 * See also: drm_dp_mst_put_mstb_malloc()
1343 drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1345 kref_get(&mstb->malloc_kref);
1346 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
1350 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1352 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1354 * Decrements &drm_dp_mst_branch.malloc_kref. When
1355 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1356 * will be released and @mstb may no longer be used.
1358 * See also: drm_dp_mst_get_mstb_malloc()
1361 drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1363 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
1364 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
1367 static void drm_dp_free_mst_port(struct kref *kref)
1369 struct drm_dp_mst_port *port =
1370 container_of(kref, struct drm_dp_mst_port, malloc_kref);
1372 drm_dp_mst_put_mstb_malloc(port->parent);
1377 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1378 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1380 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1381 * reaches 0, the memory allocation for @port will be released and @port may
1382 * no longer be used.
1384 * Because @port could potentially be freed at any time by the DP MST helpers
1385 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1386 * function, drivers that which to make use of &struct drm_dp_mst_port should
1387 * ensure that they grab at least one main malloc reference to their MST ports
1388 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1389 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1391 * See also: drm_dp_mst_put_port_malloc()
1394 drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1396 kref_get(&port->malloc_kref);
1397 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1399 EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1402 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1403 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1405 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1406 * reaches 0, the memory allocation for @port will be released and @port may
1407 * no longer be used.
1409 * See also: drm_dp_mst_get_port_malloc()
1412 drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1414 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1415 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1417 EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1419 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
1421 #define STACK_DEPTH 8
1423 static noinline void
1424 __topology_ref_save(struct drm_dp_mst_topology_mgr *mgr,
1425 struct drm_dp_mst_topology_ref_history *history,
1426 enum drm_dp_mst_topology_ref_type type)
1428 struct drm_dp_mst_topology_ref_entry *entry = NULL;
1429 depot_stack_handle_t backtrace;
1430 ulong stack_entries[STACK_DEPTH];
1434 n = stack_trace_save(stack_entries, ARRAY_SIZE(stack_entries), 1);
1435 backtrace = stack_depot_save(stack_entries, n, GFP_KERNEL);
1439 /* Try to find an existing entry for this backtrace */
1440 for (i = 0; i < history->len; i++) {
1441 if (history->entries[i].backtrace == backtrace) {
1442 entry = &history->entries[i];
1447 /* Otherwise add one */
1449 struct drm_dp_mst_topology_ref_entry *new;
1450 int new_len = history->len + 1;
1452 new = krealloc(history->entries, sizeof(*new) * new_len,
1457 entry = &new[history->len];
1458 history->len = new_len;
1459 history->entries = new;
1461 entry->backtrace = backtrace;
1466 entry->ts_nsec = ktime_get_ns();
1470 topology_ref_history_cmp(const void *a, const void *b)
1472 const struct drm_dp_mst_topology_ref_entry *entry_a = a, *entry_b = b;
1474 if (entry_a->ts_nsec > entry_b->ts_nsec)
1476 else if (entry_a->ts_nsec < entry_b->ts_nsec)
1482 static inline const char *
1483 topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type)
1485 if (type == DRM_DP_MST_TOPOLOGY_REF_GET)
1492 __dump_topology_ref_history(struct drm_dp_mst_topology_ref_history *history,
1493 void *ptr, const char *type_str)
1495 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
1496 char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
1505 /* First, sort the list so that it goes from oldest to newest
1508 sort(history->entries, history->len, sizeof(*history->entries),
1509 topology_ref_history_cmp, NULL);
1511 drm_printf(&p, "%s (%p) topology count reached 0, dumping history:\n",
1514 for (i = 0; i < history->len; i++) {
1515 const struct drm_dp_mst_topology_ref_entry *entry =
1516 &history->entries[i];
1519 u64 ts_nsec = entry->ts_nsec;
1520 u32 rem_nsec = do_div(ts_nsec, 1000000000);
1522 nr_entries = stack_depot_fetch(entry->backtrace, &entries);
1523 stack_trace_snprint(buf, PAGE_SIZE, entries, nr_entries, 4);
1525 drm_printf(&p, " %d %ss (last at %5llu.%06u):\n%s",
1527 topology_ref_type_to_str(entry->type),
1528 ts_nsec, rem_nsec / 1000, buf);
1531 /* Now free the history, since this is the only time we expose it */
1532 kfree(history->entries);
1537 static __always_inline void
1538 drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)
1540 __dump_topology_ref_history(&mstb->topology_ref_history, mstb,
1544 static __always_inline void
1545 drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port)
1547 __dump_topology_ref_history(&port->topology_ref_history, port,
1551 static __always_inline void
1552 save_mstb_topology_ref(struct drm_dp_mst_branch *mstb,
1553 enum drm_dp_mst_topology_ref_type type)
1555 __topology_ref_save(mstb->mgr, &mstb->topology_ref_history, type);
1558 static __always_inline void
1559 save_port_topology_ref(struct drm_dp_mst_port *port,
1560 enum drm_dp_mst_topology_ref_type type)
1562 __topology_ref_save(port->mgr, &port->topology_ref_history, type);
1566 topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr)
1568 mutex_lock(&mgr->topology_ref_history_lock);
1572 topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr)
1574 mutex_unlock(&mgr->topology_ref_history_lock);
1578 topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {}
1580 topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {}
1582 drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb) {}
1584 drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port) {}
1585 #define save_mstb_topology_ref(mstb, type)
1586 #define save_port_topology_ref(port, type)
1589 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1591 struct drm_dp_mst_branch *mstb =
1592 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1593 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1595 drm_dp_mst_dump_mstb_topology_history(mstb);
1597 INIT_LIST_HEAD(&mstb->destroy_next);
1600 * This can get called under mgr->mutex, so we need to perform the
1601 * actual destruction of the mstb in another worker
1603 mutex_lock(&mgr->delayed_destroy_lock);
1604 list_add(&mstb->destroy_next, &mgr->destroy_branch_device_list);
1605 mutex_unlock(&mgr->delayed_destroy_lock);
1606 schedule_work(&mgr->delayed_destroy_work);
1610 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1611 * branch device unless it's zero
1612 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1614 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1615 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1616 * reached 0). Holding a topology reference implies that a malloc reference
1617 * will be held to @mstb as long as the user holds the topology reference.
1619 * Care should be taken to ensure that the user has at least one malloc
1620 * reference to @mstb. If you already have a topology reference to @mstb, you
1621 * should use drm_dp_mst_topology_get_mstb() instead.
1624 * drm_dp_mst_topology_get_mstb()
1625 * drm_dp_mst_topology_put_mstb()
1628 * * 1: A topology reference was grabbed successfully
1629 * * 0: @port is no longer in the topology, no reference was grabbed
1631 static int __must_check
1632 drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1636 topology_ref_history_lock(mstb->mgr);
1637 ret = kref_get_unless_zero(&mstb->topology_kref);
1639 DRM_DEBUG("mstb %p (%d)\n",
1640 mstb, kref_read(&mstb->topology_kref));
1641 save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
1644 topology_ref_history_unlock(mstb->mgr);
1650 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1652 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1654 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1655 * not it's already reached 0. This is only valid to use in scenarios where
1656 * you are already guaranteed to have at least one active topology reference
1657 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1660 * drm_dp_mst_topology_try_get_mstb()
1661 * drm_dp_mst_topology_put_mstb()
1663 static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1665 topology_ref_history_lock(mstb->mgr);
1667 save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_GET);
1668 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1669 kref_get(&mstb->topology_kref);
1670 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1672 topology_ref_history_unlock(mstb->mgr);
1676 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1678 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1680 * Releases a topology reference from @mstb by decrementing
1681 * &drm_dp_mst_branch.topology_kref.
1684 * drm_dp_mst_topology_try_get_mstb()
1685 * drm_dp_mst_topology_get_mstb()
1688 drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1690 topology_ref_history_lock(mstb->mgr);
1692 DRM_DEBUG("mstb %p (%d)\n",
1693 mstb, kref_read(&mstb->topology_kref) - 1);
1694 save_mstb_topology_ref(mstb, DRM_DP_MST_TOPOLOGY_REF_PUT);
1696 topology_ref_history_unlock(mstb->mgr);
1697 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1700 static void drm_dp_destroy_port(struct kref *kref)
1702 struct drm_dp_mst_port *port =
1703 container_of(kref, struct drm_dp_mst_port, topology_kref);
1704 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1706 drm_dp_mst_dump_port_topology_history(port);
1708 /* There's nothing that needs locking to destroy an input port yet */
1710 drm_dp_mst_put_port_malloc(port);
1714 kfree(port->cached_edid);
1717 * we can't destroy the connector here, as we might be holding the
1718 * mode_config.mutex from an EDID retrieval
1720 mutex_lock(&mgr->delayed_destroy_lock);
1721 list_add(&port->next, &mgr->destroy_port_list);
1722 mutex_unlock(&mgr->delayed_destroy_lock);
1723 schedule_work(&mgr->delayed_destroy_work);
1727 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1728 * port unless it's zero
1729 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1731 * Attempts to grab a topology reference to @port, if it hasn't yet been
1732 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1733 * 0). Holding a topology reference implies that a malloc reference will be
1734 * held to @port as long as the user holds the topology reference.
1736 * Care should be taken to ensure that the user has at least one malloc
1737 * reference to @port. If you already have a topology reference to @port, you
1738 * should use drm_dp_mst_topology_get_port() instead.
1741 * drm_dp_mst_topology_get_port()
1742 * drm_dp_mst_topology_put_port()
1745 * * 1: A topology reference was grabbed successfully
1746 * * 0: @port is no longer in the topology, no reference was grabbed
1748 static int __must_check
1749 drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1753 topology_ref_history_lock(port->mgr);
1754 ret = kref_get_unless_zero(&port->topology_kref);
1756 DRM_DEBUG("port %p (%d)\n",
1757 port, kref_read(&port->topology_kref));
1758 save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
1761 topology_ref_history_unlock(port->mgr);
1766 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1767 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1769 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1770 * not it's already reached 0. This is only valid to use in scenarios where
1771 * you are already guaranteed to have at least one active topology reference
1772 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1775 * drm_dp_mst_topology_try_get_port()
1776 * drm_dp_mst_topology_put_port()
1778 static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1780 topology_ref_history_lock(port->mgr);
1782 WARN_ON(kref_read(&port->topology_kref) == 0);
1783 kref_get(&port->topology_kref);
1784 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1785 save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_GET);
1787 topology_ref_history_unlock(port->mgr);
1791 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1792 * @port: The &struct drm_dp_mst_port to release the topology reference from
1794 * Releases a topology reference from @port by decrementing
1795 * &drm_dp_mst_port.topology_kref.
1798 * drm_dp_mst_topology_try_get_port()
1799 * drm_dp_mst_topology_get_port()
1801 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1803 topology_ref_history_lock(port->mgr);
1805 DRM_DEBUG("port %p (%d)\n",
1806 port, kref_read(&port->topology_kref) - 1);
1807 save_port_topology_ref(port, DRM_DP_MST_TOPOLOGY_REF_PUT);
1809 topology_ref_history_unlock(port->mgr);
1810 kref_put(&port->topology_kref, drm_dp_destroy_port);
1813 static struct drm_dp_mst_branch *
1814 drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1815 struct drm_dp_mst_branch *to_find)
1817 struct drm_dp_mst_port *port;
1818 struct drm_dp_mst_branch *rmstb;
1820 if (to_find == mstb)
1823 list_for_each_entry(port, &mstb->ports, next) {
1825 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1826 port->mstb, to_find);
1834 static struct drm_dp_mst_branch *
1835 drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1836 struct drm_dp_mst_branch *mstb)
1838 struct drm_dp_mst_branch *rmstb = NULL;
1840 mutex_lock(&mgr->lock);
1841 if (mgr->mst_primary) {
1842 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1843 mgr->mst_primary, mstb);
1845 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1848 mutex_unlock(&mgr->lock);
1852 static struct drm_dp_mst_port *
1853 drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1854 struct drm_dp_mst_port *to_find)
1856 struct drm_dp_mst_port *port, *mport;
1858 list_for_each_entry(port, &mstb->ports, next) {
1859 if (port == to_find)
1863 mport = drm_dp_mst_topology_get_port_validated_locked(
1864 port->mstb, to_find);
1872 static struct drm_dp_mst_port *
1873 drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1874 struct drm_dp_mst_port *port)
1876 struct drm_dp_mst_port *rport = NULL;
1878 mutex_lock(&mgr->lock);
1879 if (mgr->mst_primary) {
1880 rport = drm_dp_mst_topology_get_port_validated_locked(
1881 mgr->mst_primary, port);
1883 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1886 mutex_unlock(&mgr->lock);
1890 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1892 struct drm_dp_mst_port *port;
1895 list_for_each_entry(port, &mstb->ports, next) {
1896 if (port->port_num == port_num) {
1897 ret = drm_dp_mst_topology_try_get_port(port);
1898 return ret ? port : NULL;
1906 * calculate a new RAD for this MST branch device
1907 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1908 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1910 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1913 int parent_lct = port->parent->lct;
1915 int idx = (parent_lct - 1) / 2;
1916 if (parent_lct > 1) {
1917 memcpy(rad, port->parent->rad, idx + 1);
1918 shift = (parent_lct % 2) ? 4 : 0;
1922 rad[idx] |= port->port_num << shift;
1923 return parent_lct + 1;
1926 static bool drm_dp_mst_is_end_device(u8 pdt, bool mcs)
1929 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1930 case DP_PEER_DEVICE_SST_SINK:
1932 case DP_PEER_DEVICE_MST_BRANCHING:
1933 /* For sst branch device */
1943 drm_dp_port_set_pdt(struct drm_dp_mst_port *port, u8 new_pdt,
1946 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1947 struct drm_dp_mst_branch *mstb;
1951 if (port->pdt == new_pdt && port->mcs == new_mcs)
1954 /* Teardown the old pdt, if there is one */
1955 if (port->pdt != DP_PEER_DEVICE_NONE) {
1956 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
1958 * If the new PDT would also have an i2c bus,
1959 * don't bother with reregistering it
1961 if (new_pdt != DP_PEER_DEVICE_NONE &&
1962 drm_dp_mst_is_end_device(new_pdt, new_mcs)) {
1963 port->pdt = new_pdt;
1964 port->mcs = new_mcs;
1968 /* remove i2c over sideband */
1969 drm_dp_mst_unregister_i2c_bus(&port->aux);
1971 mutex_lock(&mgr->lock);
1972 drm_dp_mst_topology_put_mstb(port->mstb);
1974 mutex_unlock(&mgr->lock);
1978 port->pdt = new_pdt;
1979 port->mcs = new_mcs;
1981 if (port->pdt != DP_PEER_DEVICE_NONE) {
1982 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
1983 /* add i2c over sideband */
1984 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1986 lct = drm_dp_calculate_rad(port, rad);
1987 mstb = drm_dp_add_mst_branch_device(lct, rad);
1990 DRM_ERROR("Failed to create MSTB for port %p",
1995 mutex_lock(&mgr->lock);
1997 mstb->mgr = port->mgr;
1998 mstb->port_parent = port;
2001 * Make sure this port's memory allocation stays
2002 * around until its child MSTB releases it
2004 drm_dp_mst_get_port_malloc(port);
2005 mutex_unlock(&mgr->lock);
2007 /* And make sure we send a link address for this */
2014 port->pdt = DP_PEER_DEVICE_NONE;
2019 * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
2020 * @aux: Fake sideband AUX CH
2021 * @offset: address of the (first) register to read
2022 * @buffer: buffer to store the register values
2023 * @size: number of bytes in @buffer
2025 * Performs the same functionality for remote devices via
2026 * sideband messaging as drm_dp_dpcd_read() does for local
2027 * devices via actual AUX CH.
2029 * Return: Number of bytes read, or negative error code on failure.
2031 ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
2032 unsigned int offset, void *buffer, size_t size)
2034 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
2037 return drm_dp_send_dpcd_read(port->mgr, port,
2038 offset, size, buffer);
2042 * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
2043 * @aux: Fake sideband AUX CH
2044 * @offset: address of the (first) register to write
2045 * @buffer: buffer containing the values to write
2046 * @size: number of bytes in @buffer
2048 * Performs the same functionality for remote devices via
2049 * sideband messaging as drm_dp_dpcd_write() does for local
2050 * devices via actual AUX CH.
2052 * Return: number of bytes written on success, negative error code on failure.
2054 ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
2055 unsigned int offset, void *buffer, size_t size)
2057 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
2060 return drm_dp_send_dpcd_write(port->mgr, port,
2061 offset, size, buffer);
2064 static int drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
2068 memcpy(mstb->guid, guid, 16);
2070 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
2071 if (mstb->port_parent) {
2072 ret = drm_dp_send_dpcd_write(mstb->mgr,
2074 DP_GUID, 16, mstb->guid);
2076 ret = drm_dp_dpcd_write(mstb->mgr->aux,
2077 DP_GUID, mstb->guid, 16);
2081 if (ret < 16 && ret > 0)
2084 return ret == 16 ? 0 : ret;
2087 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
2090 size_t proppath_size)
2094 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
2095 for (i = 0; i < (mstb->lct - 1); i++) {
2096 int shift = (i % 2) ? 0 : 4;
2097 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
2098 snprintf(temp, sizeof(temp), "-%d", port_num);
2099 strlcat(proppath, temp, proppath_size);
2101 snprintf(temp, sizeof(temp), "-%d", pnum);
2102 strlcat(proppath, temp, proppath_size);
2106 * drm_dp_mst_connector_late_register() - Late MST connector registration
2107 * @connector: The MST connector
2108 * @port: The MST port for this connector
2110 * Helper to register the remote aux device for this MST port. Drivers should
2111 * call this from their mst connector's late_register hook to enable MST aux
2114 * Return: 0 on success, negative error code on failure.
2116 int drm_dp_mst_connector_late_register(struct drm_connector *connector,
2117 struct drm_dp_mst_port *port)
2119 DRM_DEBUG_KMS("registering %s remote bus for %s\n",
2120 port->aux.name, connector->kdev->kobj.name);
2122 port->aux.dev = connector->kdev;
2123 return drm_dp_aux_register_devnode(&port->aux);
2125 EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
2128 * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
2129 * @connector: The MST connector
2130 * @port: The MST port for this connector
2132 * Helper to unregister the remote aux device for this MST port, registered by
2133 * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
2134 * connector's early_unregister hook.
2136 void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
2137 struct drm_dp_mst_port *port)
2139 DRM_DEBUG_KMS("unregistering %s remote bus for %s\n",
2140 port->aux.name, connector->kdev->kobj.name);
2141 drm_dp_aux_unregister_devnode(&port->aux);
2143 EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
2146 drm_dp_mst_port_add_connector(struct drm_dp_mst_branch *mstb,
2147 struct drm_dp_mst_port *port)
2149 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2153 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
2154 port->connector = mgr->cbs->add_connector(mgr, port, proppath);
2155 if (!port->connector) {
2160 if (port->pdt != DP_PEER_DEVICE_NONE &&
2161 drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
2162 port->cached_edid = drm_get_edid(port->connector,
2164 drm_connector_set_tile_property(port->connector);
2167 drm_connector_register(port->connector);
2171 DRM_ERROR("Failed to create connector for port %p: %d\n", port, ret);
2175 * Drop a topology reference, and unlink the port from the in-memory topology
2179 drm_dp_mst_topology_unlink_port(struct drm_dp_mst_topology_mgr *mgr,
2180 struct drm_dp_mst_port *port)
2182 mutex_lock(&mgr->lock);
2183 port->parent->num_ports--;
2184 list_del(&port->next);
2185 mutex_unlock(&mgr->lock);
2186 drm_dp_mst_topology_put_port(port);
2189 static struct drm_dp_mst_port *
2190 drm_dp_mst_add_port(struct drm_device *dev,
2191 struct drm_dp_mst_topology_mgr *mgr,
2192 struct drm_dp_mst_branch *mstb, u8 port_number)
2194 struct drm_dp_mst_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
2199 kref_init(&port->topology_kref);
2200 kref_init(&port->malloc_kref);
2201 port->parent = mstb;
2202 port->port_num = port_number;
2204 port->aux.name = "DPMST";
2205 port->aux.dev = dev->dev;
2206 port->aux.is_remote = true;
2208 /* initialize the MST downstream port's AUX crc work queue */
2209 drm_dp_remote_aux_init(&port->aux);
2212 * Make sure the memory allocation for our parent branch stays
2213 * around until our own memory allocation is released
2215 drm_dp_mst_get_mstb_malloc(mstb);
2221 drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch *mstb,
2222 struct drm_device *dev,
2223 struct drm_dp_link_addr_reply_port *port_msg)
2225 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
2226 struct drm_dp_mst_port *port;
2227 int old_ddps = 0, ret;
2228 u8 new_pdt = DP_PEER_DEVICE_NONE;
2230 bool created = false, send_link_addr = false, changed = false;
2232 port = drm_dp_get_port(mstb, port_msg->port_number);
2234 port = drm_dp_mst_add_port(dev, mgr, mstb,
2235 port_msg->port_number);
2240 } else if (!port->input && port_msg->input_port && port->connector) {
2241 /* Since port->connector can't be changed here, we create a
2242 * new port if input_port changes from 0 to 1
2244 drm_dp_mst_topology_unlink_port(mgr, port);
2245 drm_dp_mst_topology_put_port(port);
2246 port = drm_dp_mst_add_port(dev, mgr, mstb,
2247 port_msg->port_number);
2252 } else if (port->input && !port_msg->input_port) {
2254 } else if (port->connector) {
2255 /* We're updating a port that's exposed to userspace, so do it
2258 drm_modeset_lock(&mgr->base.lock, NULL);
2260 old_ddps = port->ddps;
2261 changed = port->ddps != port_msg->ddps ||
2263 (port->ldps != port_msg->legacy_device_plug_status ||
2264 port->dpcd_rev != port_msg->dpcd_revision ||
2265 port->mcs != port_msg->mcs ||
2266 port->pdt != port_msg->peer_device_type ||
2267 port->num_sdp_stream_sinks !=
2268 port_msg->num_sdp_stream_sinks));
2271 port->input = port_msg->input_port;
2273 new_pdt = port_msg->peer_device_type;
2274 new_mcs = port_msg->mcs;
2275 port->ddps = port_msg->ddps;
2276 port->ldps = port_msg->legacy_device_plug_status;
2277 port->dpcd_rev = port_msg->dpcd_revision;
2278 port->num_sdp_streams = port_msg->num_sdp_streams;
2279 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
2281 /* manage mstb port lists with mgr lock - take a reference
2284 mutex_lock(&mgr->lock);
2285 drm_dp_mst_topology_get_port(port);
2286 list_add(&port->next, &mstb->ports);
2288 mutex_unlock(&mgr->lock);
2292 * Reprobe PBN caps on both hotplug, and when re-probing the link
2293 * for our parent mstb
2295 if (old_ddps != port->ddps || !created) {
2296 if (port->ddps && !port->input) {
2297 ret = drm_dp_send_enum_path_resources(mgr, mstb,
2306 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
2308 send_link_addr = true;
2309 } else if (ret < 0) {
2310 DRM_ERROR("Failed to change PDT on port %p: %d\n",
2316 * If this port wasn't just created, then we're reprobing because
2317 * we're coming out of suspend. In this case, always resend the link
2318 * address if there's an MSTB on this port
2320 if (!created && port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
2322 send_link_addr = true;
2324 if (port->connector)
2325 drm_modeset_unlock(&mgr->base.lock);
2326 else if (!port->input)
2327 drm_dp_mst_port_add_connector(mstb, port);
2329 if (send_link_addr && port->mstb) {
2330 ret = drm_dp_send_link_address(mgr, port->mstb);
2331 if (ret == 1) /* MSTB below us changed */
2337 /* put reference to this port */
2338 drm_dp_mst_topology_put_port(port);
2342 drm_dp_mst_topology_unlink_port(mgr, port);
2343 if (port->connector)
2344 drm_modeset_unlock(&mgr->base.lock);
2346 drm_dp_mst_topology_put_port(port);
2351 drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
2352 struct drm_dp_connection_status_notify *conn_stat)
2354 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
2355 struct drm_dp_mst_port *port;
2356 int old_ddps, old_input, ret, i;
2359 bool dowork = false, create_connector = false;
2361 port = drm_dp_get_port(mstb, conn_stat->port_number);
2365 if (port->connector) {
2366 if (!port->input && conn_stat->input_port) {
2368 * We can't remove a connector from an already exposed
2369 * port, so just throw the port out and make sure we
2370 * reprobe the link address of it's parent MSTB
2372 drm_dp_mst_topology_unlink_port(mgr, port);
2373 mstb->link_address_sent = false;
2378 /* Locking is only needed if the port's exposed to userspace */
2379 drm_modeset_lock(&mgr->base.lock, NULL);
2380 } else if (port->input && !conn_stat->input_port) {
2381 create_connector = true;
2382 /* Reprobe link address so we get num_sdp_streams */
2383 mstb->link_address_sent = false;
2387 old_ddps = port->ddps;
2388 old_input = port->input;
2389 port->input = conn_stat->input_port;
2390 port->ldps = conn_stat->legacy_device_plug_status;
2391 port->ddps = conn_stat->displayport_device_plug_status;
2393 if (old_ddps != port->ddps) {
2394 if (port->ddps && !port->input)
2395 drm_dp_send_enum_path_resources(mgr, mstb, port);
2400 new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat->peer_device_type;
2401 new_mcs = conn_stat->message_capability_status;
2402 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
2405 } else if (ret < 0) {
2406 DRM_ERROR("Failed to change PDT for port %p: %d\n",
2411 if (!old_input && old_ddps != port->ddps && !port->ddps) {
2412 for (i = 0; i < mgr->max_payloads; i++) {
2413 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2414 struct drm_dp_mst_port *port_validated;
2420 container_of(vcpi, struct drm_dp_mst_port, vcpi);
2422 drm_dp_mst_topology_get_port_validated(mgr, port_validated);
2423 if (!port_validated) {
2424 mutex_lock(&mgr->payload_lock);
2425 vcpi->num_slots = 0;
2426 mutex_unlock(&mgr->payload_lock);
2428 drm_dp_mst_topology_put_port(port_validated);
2433 if (port->connector)
2434 drm_modeset_unlock(&mgr->base.lock);
2435 else if (create_connector)
2436 drm_dp_mst_port_add_connector(mstb, port);
2439 drm_dp_mst_topology_put_port(port);
2441 queue_work(system_long_wq, &mstb->mgr->work);
2444 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
2447 struct drm_dp_mst_branch *mstb;
2448 struct drm_dp_mst_port *port;
2450 /* find the port by iterating down */
2452 mutex_lock(&mgr->lock);
2453 mstb = mgr->mst_primary;
2458 for (i = 0; i < lct - 1; i++) {
2459 int shift = (i % 2) ? 0 : 4;
2460 int port_num = (rad[i / 2] >> shift) & 0xf;
2462 list_for_each_entry(port, &mstb->ports, next) {
2463 if (port->port_num == port_num) {
2466 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
2474 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2478 mutex_unlock(&mgr->lock);
2482 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
2483 struct drm_dp_mst_branch *mstb,
2484 const uint8_t *guid)
2486 struct drm_dp_mst_branch *found_mstb;
2487 struct drm_dp_mst_port *port;
2489 if (memcmp(mstb->guid, guid, 16) == 0)
2493 list_for_each_entry(port, &mstb->ports, next) {
2497 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
2506 static struct drm_dp_mst_branch *
2507 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
2508 const uint8_t *guid)
2510 struct drm_dp_mst_branch *mstb;
2513 /* find the port by iterating down */
2514 mutex_lock(&mgr->lock);
2516 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
2518 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2523 mutex_unlock(&mgr->lock);
2527 static int drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2528 struct drm_dp_mst_branch *mstb)
2530 struct drm_dp_mst_port *port;
2532 bool changed = false;
2534 if (!mstb->link_address_sent) {
2535 ret = drm_dp_send_link_address(mgr, mstb);
2542 list_for_each_entry(port, &mstb->ports, next) {
2543 struct drm_dp_mst_branch *mstb_child = NULL;
2545 if (port->input || !port->ddps)
2549 mstb_child = drm_dp_mst_topology_get_mstb_validated(
2553 ret = drm_dp_check_and_send_link_address(mgr,
2555 drm_dp_mst_topology_put_mstb(mstb_child);
2566 static void drm_dp_mst_link_probe_work(struct work_struct *work)
2568 struct drm_dp_mst_topology_mgr *mgr =
2569 container_of(work, struct drm_dp_mst_topology_mgr, work);
2570 struct drm_device *dev = mgr->dev;
2571 struct drm_dp_mst_branch *mstb;
2573 bool clear_payload_id_table;
2575 mutex_lock(&mgr->probe_lock);
2577 mutex_lock(&mgr->lock);
2578 clear_payload_id_table = !mgr->payload_id_table_cleared;
2579 mgr->payload_id_table_cleared = true;
2581 mstb = mgr->mst_primary;
2583 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2587 mutex_unlock(&mgr->lock);
2589 mutex_unlock(&mgr->probe_lock);
2594 * Certain branch devices seem to incorrectly report an available_pbn
2595 * of 0 on downstream sinks, even after clearing the
2596 * DP_PAYLOAD_ALLOCATE_* registers in
2597 * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C
2598 * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make
2599 * things work again.
2601 if (clear_payload_id_table) {
2602 DRM_DEBUG_KMS("Clearing payload ID table\n");
2603 drm_dp_send_clear_payload_id_table(mgr, mstb);
2606 ret = drm_dp_check_and_send_link_address(mgr, mstb);
2607 drm_dp_mst_topology_put_mstb(mstb);
2609 mutex_unlock(&mgr->probe_lock);
2611 drm_kms_helper_hotplug_event(dev);
2614 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
2619 if (memchr_inv(guid, 0, 16))
2622 salt = get_jiffies_64();
2624 memcpy(&guid[0], &salt, sizeof(u64));
2625 memcpy(&guid[8], &salt, sizeof(u64));
2630 static void build_dpcd_read(struct drm_dp_sideband_msg_tx *msg,
2631 u8 port_num, u32 offset, u8 num_bytes)
2633 struct drm_dp_sideband_msg_req_body req;
2635 req.req_type = DP_REMOTE_DPCD_READ;
2636 req.u.dpcd_read.port_number = port_num;
2637 req.u.dpcd_read.dpcd_address = offset;
2638 req.u.dpcd_read.num_bytes = num_bytes;
2639 drm_dp_encode_sideband_req(&req, msg);
2642 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
2643 bool up, u8 *msg, int len)
2646 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
2647 int tosend, total, offset;
2654 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
2656 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
2659 if (ret != tosend) {
2660 if (ret == -EIO && retries < 5) {
2664 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
2670 } while (total > 0);
2674 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
2675 struct drm_dp_sideband_msg_tx *txmsg)
2677 struct drm_dp_mst_branch *mstb = txmsg->dst;
2680 req_type = txmsg->msg[0] & 0x7f;
2681 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
2682 req_type == DP_RESOURCE_STATUS_NOTIFY)
2686 hdr->path_msg = txmsg->path_msg;
2687 hdr->lct = mstb->lct;
2688 hdr->lcr = mstb->lct - 1;
2690 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
2695 * process a single block of the next message in the sideband queue
2697 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2698 struct drm_dp_sideband_msg_tx *txmsg,
2702 struct drm_dp_sideband_msg_hdr hdr;
2703 int len, space, idx, tosend;
2706 if (txmsg->state == DRM_DP_SIDEBAND_TX_SENT)
2709 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
2711 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED)
2712 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
2714 /* make hdr from dst mst */
2715 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
2719 /* amount left to send in this message */
2720 len = txmsg->cur_len - txmsg->cur_offset;
2722 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
2723 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
2725 tosend = min(len, space);
2726 if (len == txmsg->cur_len)
2732 hdr.msg_len = tosend + 1;
2733 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
2734 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
2735 /* add crc at end */
2736 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
2739 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
2740 if (unlikely(ret) && drm_debug_enabled(DRM_UT_DP)) {
2741 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
2743 drm_printf(&p, "sideband msg failed to send\n");
2744 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2748 txmsg->cur_offset += tosend;
2749 if (txmsg->cur_offset == txmsg->cur_len) {
2750 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
2756 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
2758 struct drm_dp_sideband_msg_tx *txmsg;
2761 WARN_ON(!mutex_is_locked(&mgr->qlock));
2763 /* construct a chunk from the first msg in the tx_msg queue */
2764 if (list_empty(&mgr->tx_msg_downq))
2767 txmsg = list_first_entry(&mgr->tx_msg_downq,
2768 struct drm_dp_sideband_msg_tx, next);
2769 ret = process_single_tx_qlock(mgr, txmsg, false);
2771 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
2772 list_del(&txmsg->next);
2773 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
2774 wake_up_all(&mgr->tx_waitq);
2778 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2779 struct drm_dp_sideband_msg_tx *txmsg)
2781 mutex_lock(&mgr->qlock);
2782 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
2784 if (drm_debug_enabled(DRM_UT_DP)) {
2785 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
2787 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2790 if (list_is_singular(&mgr->tx_msg_downq))
2791 process_single_down_tx_qlock(mgr);
2792 mutex_unlock(&mgr->qlock);
2796 drm_dp_dump_link_address(struct drm_dp_link_address_ack_reply *reply)
2798 struct drm_dp_link_addr_reply_port *port_reply;
2801 for (i = 0; i < reply->nports; i++) {
2802 port_reply = &reply->ports[i];
2803 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",
2805 port_reply->input_port,
2806 port_reply->peer_device_type,
2807 port_reply->port_number,
2808 port_reply->dpcd_revision,
2811 port_reply->legacy_device_plug_status,
2812 port_reply->num_sdp_streams,
2813 port_reply->num_sdp_stream_sinks);
2817 static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2818 struct drm_dp_mst_branch *mstb)
2820 struct drm_dp_sideband_msg_tx *txmsg;
2821 struct drm_dp_link_address_ack_reply *reply;
2822 struct drm_dp_mst_port *port, *tmp;
2823 int i, ret, port_mask = 0;
2824 bool changed = false;
2826 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2831 build_link_address(txmsg);
2833 mstb->link_address_sent = true;
2834 drm_dp_queue_down_tx(mgr, txmsg);
2836 /* FIXME: Actually do some real error handling here */
2837 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2839 DRM_ERROR("Sending link address failed with %d\n", ret);
2842 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2843 DRM_ERROR("link address NAK received\n");
2848 reply = &txmsg->reply.u.link_addr;
2849 DRM_DEBUG_KMS("link address reply: %d\n", reply->nports);
2850 drm_dp_dump_link_address(reply);
2852 ret = drm_dp_check_mstb_guid(mstb, reply->guid);
2856 drm_dp_mst_rad_to_str(mstb->rad, mstb->lct, buf, sizeof(buf));
2857 DRM_ERROR("GUID check on %s failed: %d\n",
2862 for (i = 0; i < reply->nports; i++) {
2863 port_mask |= BIT(reply->ports[i].port_number);
2864 ret = drm_dp_mst_handle_link_address_port(mstb, mgr->dev,
2872 /* Prune any ports that are currently a part of mstb in our in-memory
2873 * topology, but were not seen in this link address. Usually this
2874 * means that they were removed while the topology was out of sync,
2875 * e.g. during suspend/resume
2877 mutex_lock(&mgr->lock);
2878 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
2879 if (port_mask & BIT(port->port_num))
2882 DRM_DEBUG_KMS("port %d was not in link address, removing\n",
2884 list_del(&port->next);
2885 drm_dp_mst_topology_put_port(port);
2888 mutex_unlock(&mgr->lock);
2892 mstb->link_address_sent = false;
2894 return ret < 0 ? ret : changed;
2897 void drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
2898 struct drm_dp_mst_branch *mstb)
2900 struct drm_dp_sideband_msg_tx *txmsg;
2903 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2908 build_clear_payload_id_table(txmsg);
2910 drm_dp_queue_down_tx(mgr, txmsg);
2912 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2913 if (ret > 0 && txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2914 DRM_DEBUG_KMS("clear payload table id nak received\n");
2920 drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2921 struct drm_dp_mst_branch *mstb,
2922 struct drm_dp_mst_port *port)
2924 struct drm_dp_enum_path_resources_ack_reply *path_res;
2925 struct drm_dp_sideband_msg_tx *txmsg;
2928 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2933 build_enum_path_resources(txmsg, port->port_num);
2935 drm_dp_queue_down_tx(mgr, txmsg);
2937 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2940 path_res = &txmsg->reply.u.path_resources;
2942 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2943 DRM_DEBUG_KMS("enum path resources nak received\n");
2945 if (port->port_num != path_res->port_number)
2946 DRM_ERROR("got incorrect port in response\n");
2948 DRM_DEBUG_KMS("enum path resources %d: %d %d\n",
2949 path_res->port_number,
2950 path_res->full_payload_bw_number,
2951 path_res->avail_payload_bw_number);
2954 * If something changed, make sure we send a
2957 if (port->full_pbn != path_res->full_payload_bw_number ||
2958 port->fec_capable != path_res->fec_capable)
2961 port->full_pbn = path_res->full_payload_bw_number;
2962 port->fec_capable = path_res->fec_capable;
2970 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2972 if (!mstb->port_parent)
2975 if (mstb->port_parent->mstb != mstb)
2976 return mstb->port_parent;
2978 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2982 * Searches upwards in the topology starting from mstb to try to find the
2983 * closest available parent of mstb that's still connected to the rest of the
2984 * topology. This can be used in order to perform operations like releasing
2985 * payloads, where the branch device which owned the payload may no longer be
2986 * around and thus would require that the payload on the last living relative
2989 static struct drm_dp_mst_branch *
2990 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2991 struct drm_dp_mst_branch *mstb,
2994 struct drm_dp_mst_branch *rmstb = NULL;
2995 struct drm_dp_mst_port *found_port;
2997 mutex_lock(&mgr->lock);
2998 if (!mgr->mst_primary)
3002 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
3006 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
3007 rmstb = found_port->parent;
3008 *port_num = found_port->port_num;
3010 /* Search again, starting from this parent */
3011 mstb = found_port->parent;
3015 mutex_unlock(&mgr->lock);
3019 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
3020 struct drm_dp_mst_port *port,
3024 struct drm_dp_sideband_msg_tx *txmsg;
3025 struct drm_dp_mst_branch *mstb;
3027 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
3030 port_num = port->port_num;
3031 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3033 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
3041 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3047 for (i = 0; i < port->num_sdp_streams; i++)
3051 build_allocate_payload(txmsg, port_num,
3053 pbn, port->num_sdp_streams, sinks);
3055 drm_dp_queue_down_tx(mgr, txmsg);
3058 * FIXME: there is a small chance that between getting the last
3059 * connected mstb and sending the payload message, the last connected
3060 * mstb could also be removed from the topology. In the future, this
3061 * needs to be fixed by restarting the
3062 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
3063 * timeout if the topology is still connected to the system.
3065 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3067 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3074 drm_dp_mst_topology_put_mstb(mstb);
3078 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
3079 struct drm_dp_mst_port *port, bool power_up)
3081 struct drm_dp_sideband_msg_tx *txmsg;
3084 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3088 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3090 drm_dp_mst_topology_put_port(port);
3094 txmsg->dst = port->parent;
3095 build_power_updown_phy(txmsg, port->port_num, power_up);
3096 drm_dp_queue_down_tx(mgr, txmsg);
3098 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
3100 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3106 drm_dp_mst_topology_put_port(port);
3110 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
3112 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
3114 struct drm_dp_payload *payload)
3118 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
3120 payload->payload_state = 0;
3123 payload->payload_state = DP_PAYLOAD_LOCAL;
3127 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
3128 struct drm_dp_mst_port *port,
3130 struct drm_dp_payload *payload)
3133 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
3136 payload->payload_state = DP_PAYLOAD_REMOTE;
3140 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
3141 struct drm_dp_mst_port *port,
3143 struct drm_dp_payload *payload)
3145 DRM_DEBUG_KMS("\n");
3146 /* it's okay for these to fail */
3148 drm_dp_payload_send_msg(mgr, port, id, 0);
3151 drm_dp_dpcd_write_payload(mgr, id, payload);
3152 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
3156 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
3158 struct drm_dp_payload *payload)
3160 payload->payload_state = 0;
3165 * drm_dp_update_payload_part1() - Execute payload update part 1
3166 * @mgr: manager to use.
3168 * This iterates over all proposed virtual channels, and tries to
3169 * allocate space in the link for them. For 0->slots transitions,
3170 * this step just writes the VCPI to the MST device. For slots->0
3171 * transitions, this writes the updated VCPIs and removes the
3172 * remote VC payloads.
3174 * after calling this the driver should generate ACT and payload
3177 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
3179 struct drm_dp_payload req_payload;
3180 struct drm_dp_mst_port *port;
3184 mutex_lock(&mgr->payload_lock);
3185 for (i = 0; i < mgr->max_payloads; i++) {
3186 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
3187 struct drm_dp_payload *payload = &mgr->payloads[i];
3188 bool put_port = false;
3190 /* solve the current payloads - compare to the hw ones
3191 - update the hw view */
3192 req_payload.start_slot = cur_slots;
3194 port = container_of(vcpi, struct drm_dp_mst_port,
3197 /* Validated ports don't matter if we're releasing
3200 if (vcpi->num_slots) {
3201 port = drm_dp_mst_topology_get_port_validated(
3204 mutex_unlock(&mgr->payload_lock);
3210 req_payload.num_slots = vcpi->num_slots;
3211 req_payload.vcpi = vcpi->vcpi;
3214 req_payload.num_slots = 0;
3217 payload->start_slot = req_payload.start_slot;
3218 /* work out what is required to happen with this payload */
3219 if (payload->num_slots != req_payload.num_slots) {
3221 /* need to push an update for this payload */
3222 if (req_payload.num_slots) {
3223 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
3225 payload->num_slots = req_payload.num_slots;
3226 payload->vcpi = req_payload.vcpi;
3228 } else if (payload->num_slots) {
3229 payload->num_slots = 0;
3230 drm_dp_destroy_payload_step1(mgr, port,
3233 req_payload.payload_state =
3234 payload->payload_state;
3235 payload->start_slot = 0;
3237 payload->payload_state = req_payload.payload_state;
3239 cur_slots += req_payload.num_slots;
3242 drm_dp_mst_topology_put_port(port);
3245 for (i = 0; i < mgr->max_payloads; /* do nothing */) {
3246 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL) {
3251 DRM_DEBUG_KMS("removing payload %d\n", i);
3252 for (j = i; j < mgr->max_payloads - 1; j++) {
3253 mgr->payloads[j] = mgr->payloads[j + 1];
3254 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
3256 if (mgr->proposed_vcpis[j] &&
3257 mgr->proposed_vcpis[j]->num_slots) {
3258 set_bit(j + 1, &mgr->payload_mask);
3260 clear_bit(j + 1, &mgr->payload_mask);
3264 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
3265 sizeof(struct drm_dp_payload));
3266 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
3267 clear_bit(mgr->max_payloads, &mgr->payload_mask);
3269 mutex_unlock(&mgr->payload_lock);
3273 EXPORT_SYMBOL(drm_dp_update_payload_part1);
3276 * drm_dp_update_payload_part2() - Execute payload update part 2
3277 * @mgr: manager to use.
3279 * This iterates over all proposed virtual channels, and tries to
3280 * allocate space in the link for them. For 0->slots transitions,
3281 * this step writes the remote VC payload commands. For slots->0
3282 * this just resets some internal state.
3284 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
3286 struct drm_dp_mst_port *port;
3289 mutex_lock(&mgr->payload_lock);
3290 for (i = 0; i < mgr->max_payloads; i++) {
3292 if (!mgr->proposed_vcpis[i])
3295 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
3297 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
3298 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
3299 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
3300 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
3301 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
3304 mutex_unlock(&mgr->payload_lock);
3308 mutex_unlock(&mgr->payload_lock);
3311 EXPORT_SYMBOL(drm_dp_update_payload_part2);
3313 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
3314 struct drm_dp_mst_port *port,
3315 int offset, int size, u8 *bytes)
3318 struct drm_dp_sideband_msg_tx *txmsg;
3319 struct drm_dp_mst_branch *mstb;
3321 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3325 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3331 build_dpcd_read(txmsg, port->port_num, offset, size);
3332 txmsg->dst = port->parent;
3334 drm_dp_queue_down_tx(mgr, txmsg);
3336 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3340 /* DPCD read should never be NACKed */
3341 if (txmsg->reply.reply_type == 1) {
3342 DRM_ERROR("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
3343 mstb, port->port_num, offset, size);
3348 if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
3353 ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,
3355 memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret);
3360 drm_dp_mst_topology_put_mstb(mstb);
3365 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
3366 struct drm_dp_mst_port *port,
3367 int offset, int size, u8 *bytes)
3370 struct drm_dp_sideband_msg_tx *txmsg;
3371 struct drm_dp_mst_branch *mstb;
3373 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3377 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3383 build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
3386 drm_dp_queue_down_tx(mgr, txmsg);
3388 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3390 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3398 drm_dp_mst_topology_put_mstb(mstb);
3402 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
3404 struct drm_dp_sideband_msg_reply_body reply;
3406 reply.reply_type = DP_SIDEBAND_REPLY_ACK;
3407 reply.req_type = req_type;
3408 drm_dp_encode_sideband_reply(&reply, msg);
3412 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
3413 struct drm_dp_mst_branch *mstb,
3414 int req_type, bool broadcast)
3416 struct drm_dp_sideband_msg_tx *txmsg;
3418 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3423 drm_dp_encode_up_ack_reply(txmsg, req_type);
3425 mutex_lock(&mgr->qlock);
3426 /* construct a chunk from the first msg in the tx_msg queue */
3427 process_single_tx_qlock(mgr, txmsg, true);
3428 mutex_unlock(&mgr->qlock);
3434 static int drm_dp_get_vc_payload_bw(u8 dp_link_bw, u8 dp_link_count)
3436 if (dp_link_bw == 0 || dp_link_count == 0)
3437 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
3438 dp_link_bw, dp_link_count);
3440 return dp_link_bw * dp_link_count / 2;
3444 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
3445 * @mgr: manager to set state for
3446 * @mst_state: true to enable MST on this connector - false to disable.
3448 * This is called by the driver when it detects an MST capable device plugged
3449 * into a DP MST capable port, or when a DP MST capable device is unplugged.
3451 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
3454 struct drm_dp_mst_branch *mstb = NULL;
3456 mutex_lock(&mgr->payload_lock);
3457 mutex_lock(&mgr->lock);
3458 if (mst_state == mgr->mst_state)
3461 mgr->mst_state = mst_state;
3462 /* set the device into MST mode */
3464 struct drm_dp_payload reset_pay;
3466 WARN_ON(mgr->mst_primary);
3469 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
3470 if (ret != DP_RECEIVER_CAP_SIZE) {
3471 DRM_DEBUG_KMS("failed to read DPCD\n");
3475 mgr->pbn_div = drm_dp_get_vc_payload_bw(mgr->dpcd[1],
3476 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK);
3477 if (mgr->pbn_div == 0) {
3482 /* add initial branch device at LCT 1 */
3483 mstb = drm_dp_add_mst_branch_device(1, NULL);
3490 /* give this the main reference */
3491 mgr->mst_primary = mstb;
3492 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
3494 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3497 DP_UPSTREAM_IS_SRC);
3501 reset_pay.start_slot = 0;
3502 reset_pay.num_slots = 0x3f;
3503 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
3505 queue_work(system_long_wq, &mgr->work);
3509 /* disable MST on the device */
3510 mstb = mgr->mst_primary;
3511 mgr->mst_primary = NULL;
3512 /* this can fail if the device is gone */
3513 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
3515 memset(mgr->payloads, 0,
3516 mgr->max_payloads * sizeof(mgr->payloads[0]));
3517 memset(mgr->proposed_vcpis, 0,
3518 mgr->max_payloads * sizeof(mgr->proposed_vcpis[0]));
3519 mgr->payload_mask = 0;
3520 set_bit(0, &mgr->payload_mask);
3522 mgr->payload_id_table_cleared = false;
3526 mutex_unlock(&mgr->lock);
3527 mutex_unlock(&mgr->payload_lock);
3529 drm_dp_mst_topology_put_mstb(mstb);
3533 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
3536 drm_dp_mst_topology_mgr_invalidate_mstb(struct drm_dp_mst_branch *mstb)
3538 struct drm_dp_mst_port *port;
3540 /* The link address will need to be re-sent on resume */
3541 mstb->link_address_sent = false;
3543 list_for_each_entry(port, &mstb->ports, next)
3545 drm_dp_mst_topology_mgr_invalidate_mstb(port->mstb);
3549 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
3550 * @mgr: manager to suspend
3552 * This function tells the MST device that we can't handle UP messages
3553 * anymore. This should stop it from sending any since we are suspended.
3555 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
3557 mutex_lock(&mgr->lock);
3558 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3559 DP_MST_EN | DP_UPSTREAM_IS_SRC);
3560 mutex_unlock(&mgr->lock);
3561 flush_work(&mgr->up_req_work);
3562 flush_work(&mgr->work);
3563 flush_work(&mgr->delayed_destroy_work);
3565 mutex_lock(&mgr->lock);
3566 if (mgr->mst_state && mgr->mst_primary)
3567 drm_dp_mst_topology_mgr_invalidate_mstb(mgr->mst_primary);
3568 mutex_unlock(&mgr->lock);
3570 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
3573 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
3574 * @mgr: manager to resume
3575 * @sync: whether or not to perform topology reprobing synchronously
3577 * This will fetch DPCD and see if the device is still there,
3578 * if it is, it will rewrite the MSTM control bits, and return.
3580 * If the device fails this returns -1, and the driver should do
3581 * a full MST reprobe, in case we were undocked.
3583 * During system resume (where it is assumed that the driver will be calling
3584 * drm_atomic_helper_resume()) this function should be called beforehand with
3585 * @sync set to true. In contexts like runtime resume where the driver is not
3586 * expected to be calling drm_atomic_helper_resume(), this function should be
3587 * called with @sync set to false in order to avoid deadlocking.
3589 * Returns: -1 if the MST topology was removed while we were suspended, 0
3592 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr,
3598 mutex_lock(&mgr->lock);
3599 if (!mgr->mst_primary)
3602 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd,
3603 DP_RECEIVER_CAP_SIZE);
3604 if (ret != DP_RECEIVER_CAP_SIZE) {
3605 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3609 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3612 DP_UPSTREAM_IS_SRC);
3614 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
3618 /* Some hubs forget their guids after they resume */
3619 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
3621 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3625 ret = drm_dp_check_mstb_guid(mgr->mst_primary, guid);
3627 DRM_DEBUG_KMS("check mstb failed - undocked during suspend?\n");
3632 * For the final step of resuming the topology, we need to bring the
3633 * state of our in-memory topology back into sync with reality. So,
3634 * restart the probing process as if we're probing a new hub
3636 queue_work(system_long_wq, &mgr->work);
3637 mutex_unlock(&mgr->lock);
3640 DRM_DEBUG_KMS("Waiting for link probe work to finish re-syncing topology...\n");
3641 flush_work(&mgr->work);
3647 mutex_unlock(&mgr->lock);
3650 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
3653 drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up,
3654 struct drm_dp_mst_branch **mstb)
3658 int replylen, curreply;
3661 struct drm_dp_sideband_msg_hdr hdr;
3662 struct drm_dp_sideband_msg_rx *msg =
3663 up ? &mgr->up_req_recv : &mgr->down_rep_recv;
3664 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE :
3665 DP_SIDEBAND_MSG_DOWN_REP_BASE;
3670 len = min(mgr->max_dpcd_transaction_bytes, 16);
3671 ret = drm_dp_dpcd_read(mgr->aux, basereg, replyblock, len);
3673 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
3677 ret = drm_dp_decode_sideband_msg_hdr(&hdr, replyblock, len, &hdrlen);
3679 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16,
3680 1, replyblock, len, false);
3681 DRM_DEBUG_KMS("ERROR: failed header\n");
3686 /* Caller is responsible for giving back this reference */
3687 *mstb = drm_dp_get_mst_branch_device(mgr, hdr.lct, hdr.rad);
3689 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3695 if (!drm_dp_sideband_msg_set_header(msg, &hdr, hdrlen)) {
3696 DRM_DEBUG_KMS("sideband msg set header failed %d\n",
3701 replylen = min(msg->curchunk_len, (u8)(len - hdrlen));
3702 ret = drm_dp_sideband_append_payload(msg, replyblock + hdrlen, replylen);
3704 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
3708 replylen = msg->curchunk_len + msg->curchunk_hdrlen - len;
3710 while (replylen > 0) {
3711 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
3712 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
3715 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
3720 ret = drm_dp_sideband_append_payload(msg, replyblock, len);
3722 DRM_DEBUG_KMS("failed to build sideband msg\n");
3732 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
3734 struct drm_dp_sideband_msg_tx *txmsg;
3735 struct drm_dp_mst_branch *mstb = NULL;
3736 struct drm_dp_sideband_msg_rx *msg = &mgr->down_rep_recv;
3738 if (!drm_dp_get_one_sb_msg(mgr, false, &mstb))
3741 /* Multi-packet message transmission, don't clear the reply */
3742 if (!msg->have_eomt)
3745 /* find the message */
3746 mutex_lock(&mgr->qlock);
3747 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq,
3748 struct drm_dp_sideband_msg_tx, next);
3749 mutex_unlock(&mgr->qlock);
3751 /* Were we actually expecting a response, and from this mstb? */
3752 if (!txmsg || txmsg->dst != mstb) {
3753 struct drm_dp_sideband_msg_hdr *hdr;
3754 hdr = &msg->initial_hdr;
3755 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
3756 mstb, hdr->seqno, hdr->lct, hdr->rad[0],
3758 goto out_clear_reply;
3761 drm_dp_sideband_parse_reply(msg, &txmsg->reply);
3763 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
3764 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
3765 txmsg->reply.req_type,
3766 drm_dp_mst_req_type_str(txmsg->reply.req_type),
3767 txmsg->reply.u.nak.reason,
3768 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
3769 txmsg->reply.u.nak.nak_data);
3772 memset(msg, 0, sizeof(struct drm_dp_sideband_msg_rx));
3773 drm_dp_mst_topology_put_mstb(mstb);
3775 mutex_lock(&mgr->qlock);
3776 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
3777 list_del(&txmsg->next);
3778 mutex_unlock(&mgr->qlock);
3780 wake_up_all(&mgr->tx_waitq);
3785 memset(msg, 0, sizeof(struct drm_dp_sideband_msg_rx));
3788 drm_dp_mst_topology_put_mstb(mstb);
3794 drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr *mgr,
3795 struct drm_dp_pending_up_req *up_req)
3797 struct drm_dp_mst_branch *mstb = NULL;
3798 struct drm_dp_sideband_msg_req_body *msg = &up_req->msg;
3799 struct drm_dp_sideband_msg_hdr *hdr = &up_req->hdr;
3800 bool hotplug = false;
3802 if (hdr->broadcast) {
3803 const u8 *guid = NULL;
3805 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY)
3806 guid = msg->u.conn_stat.guid;
3807 else if (msg->req_type == DP_RESOURCE_STATUS_NOTIFY)
3808 guid = msg->u.resource_stat.guid;
3811 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid);
3813 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
3817 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3822 /* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */
3823 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) {
3824 drm_dp_mst_handle_conn_stat(mstb, &msg->u.conn_stat);
3828 drm_dp_mst_topology_put_mstb(mstb);
3832 static void drm_dp_mst_up_req_work(struct work_struct *work)
3834 struct drm_dp_mst_topology_mgr *mgr =
3835 container_of(work, struct drm_dp_mst_topology_mgr,
3837 struct drm_dp_pending_up_req *up_req;
3838 bool send_hotplug = false;
3840 mutex_lock(&mgr->probe_lock);
3842 mutex_lock(&mgr->up_req_lock);
3843 up_req = list_first_entry_or_null(&mgr->up_req_list,
3844 struct drm_dp_pending_up_req,
3847 list_del(&up_req->next);
3848 mutex_unlock(&mgr->up_req_lock);
3853 send_hotplug |= drm_dp_mst_process_up_req(mgr, up_req);
3856 mutex_unlock(&mgr->probe_lock);
3859 drm_kms_helper_hotplug_event(mgr->dev);
3862 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
3864 struct drm_dp_pending_up_req *up_req;
3866 if (!drm_dp_get_one_sb_msg(mgr, true, NULL))
3869 if (!mgr->up_req_recv.have_eomt)
3872 up_req = kzalloc(sizeof(*up_req), GFP_KERNEL);
3874 DRM_ERROR("Not enough memory to process MST up req\n");
3877 INIT_LIST_HEAD(&up_req->next);
3879 drm_dp_sideband_parse_req(&mgr->up_req_recv, &up_req->msg);
3881 if (up_req->msg.req_type != DP_CONNECTION_STATUS_NOTIFY &&
3882 up_req->msg.req_type != DP_RESOURCE_STATUS_NOTIFY) {
3883 DRM_DEBUG_KMS("Received unknown up req type, ignoring: %x\n",
3884 up_req->msg.req_type);
3889 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, up_req->msg.req_type,
3892 if (up_req->msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
3893 const struct drm_dp_connection_status_notify *conn_stat =
3894 &up_req->msg.u.conn_stat;
3896 DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",
3897 conn_stat->port_number,
3898 conn_stat->legacy_device_plug_status,
3899 conn_stat->displayport_device_plug_status,
3900 conn_stat->message_capability_status,
3901 conn_stat->input_port,
3902 conn_stat->peer_device_type);
3903 } else if (up_req->msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
3904 const struct drm_dp_resource_status_notify *res_stat =
3905 &up_req->msg.u.resource_stat;
3907 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n",
3908 res_stat->port_number,
3909 res_stat->available_pbn);
3912 up_req->hdr = mgr->up_req_recv.initial_hdr;
3913 mutex_lock(&mgr->up_req_lock);
3914 list_add_tail(&up_req->next, &mgr->up_req_list);
3915 mutex_unlock(&mgr->up_req_lock);
3916 queue_work(system_long_wq, &mgr->up_req_work);
3919 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3924 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
3925 * @mgr: manager to notify irq for.
3926 * @esi: 4 bytes from SINK_COUNT_ESI
3927 * @handled: whether the hpd interrupt was consumed or not
3929 * This should be called from the driver when it detects a short IRQ,
3930 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
3931 * topology manager will process the sideband messages received as a result
3934 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
3941 if (sc != mgr->sink_count) {
3942 mgr->sink_count = sc;
3946 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
3947 ret = drm_dp_mst_handle_down_rep(mgr);
3951 if (esi[1] & DP_UP_REQ_MSG_RDY) {
3952 ret |= drm_dp_mst_handle_up_req(mgr);
3956 drm_dp_mst_kick_tx(mgr);
3959 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
3962 * drm_dp_mst_detect_port() - get connection status for an MST port
3963 * @connector: DRM connector for this port
3964 * @ctx: The acquisition context to use for grabbing locks
3965 * @mgr: manager for this port
3966 * @port: pointer to a port
3968 * This returns the current connection state for a port.
3971 drm_dp_mst_detect_port(struct drm_connector *connector,
3972 struct drm_modeset_acquire_ctx *ctx,
3973 struct drm_dp_mst_topology_mgr *mgr,
3974 struct drm_dp_mst_port *port)
3978 /* we need to search for the port in the mgr in case it's gone */
3979 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3981 return connector_status_disconnected;
3983 ret = drm_modeset_lock(&mgr->base.lock, ctx);
3987 ret = connector_status_disconnected;
3992 switch (port->pdt) {
3993 case DP_PEER_DEVICE_NONE:
3994 case DP_PEER_DEVICE_MST_BRANCHING:
3996 ret = connector_status_connected;
3999 case DP_PEER_DEVICE_SST_SINK:
4000 ret = connector_status_connected;
4001 /* for logical ports - cache the EDID */
4002 if (port->port_num >= 8 && !port->cached_edid) {
4003 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
4006 case DP_PEER_DEVICE_DP_LEGACY_CONV:
4008 ret = connector_status_connected;
4012 drm_dp_mst_topology_put_port(port);
4015 EXPORT_SYMBOL(drm_dp_mst_detect_port);
4018 * drm_dp_mst_get_edid() - get EDID for an MST port
4019 * @connector: toplevel connector to get EDID for
4020 * @mgr: manager for this port
4021 * @port: unverified pointer to a port.
4023 * This returns an EDID for the port connected to a connector,
4024 * It validates the pointer still exists so the caller doesn't require a
4027 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
4029 struct edid *edid = NULL;
4031 /* we need to search for the port in the mgr in case it's gone */
4032 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4036 if (port->cached_edid)
4037 edid = drm_edid_duplicate(port->cached_edid);
4039 edid = drm_get_edid(connector, &port->aux.ddc);
4041 port->has_audio = drm_detect_monitor_audio(edid);
4042 drm_dp_mst_topology_put_port(port);
4045 EXPORT_SYMBOL(drm_dp_mst_get_edid);
4048 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
4049 * @mgr: manager to use
4050 * @pbn: payload bandwidth to convert into slots.
4052 * Calculate the number of VCPI slots that will be required for the given PBN
4053 * value. This function is deprecated, and should not be used in atomic
4057 * The total slots required for this port, or error.
4059 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
4064 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
4066 /* max. time slots - one slot for MTP header */
4071 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
4073 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
4074 struct drm_dp_vcpi *vcpi, int pbn, int slots)
4078 /* max. time slots - one slot for MTP header */
4083 vcpi->aligned_pbn = slots * mgr->pbn_div;
4084 vcpi->num_slots = slots;
4086 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
4093 * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
4094 * @state: global atomic state
4095 * @mgr: MST topology manager for the port
4096 * @port: port to find vcpi slots for
4097 * @pbn: bandwidth required for the mode in PBN
4098 * @pbn_div: divider for DSC mode that takes FEC into account
4100 * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
4101 * may have had. Any atomic drivers which support MST must call this function
4102 * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
4103 * current VCPI allocation for the new state, but only when
4104 * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
4105 * to ensure compatibility with userspace applications that still use the
4106 * legacy modesetting UAPI.
4108 * Allocations set by this function are not checked against the bandwidth
4109 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
4111 * Additionally, it is OK to call this function multiple times on the same
4112 * @port as needed. It is not OK however, to call this function and
4113 * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
4116 * drm_dp_atomic_release_vcpi_slots()
4117 * drm_dp_mst_atomic_check()
4120 * Total slots in the atomic state assigned for this port, or a negative error
4121 * code if the port no longer exists
4123 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
4124 struct drm_dp_mst_topology_mgr *mgr,
4125 struct drm_dp_mst_port *port, int pbn,
4128 struct drm_dp_mst_topology_state *topology_state;
4129 struct drm_dp_vcpi_allocation *pos, *vcpi = NULL;
4130 int prev_slots, prev_bw, req_slots;
4132 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
4133 if (IS_ERR(topology_state))
4134 return PTR_ERR(topology_state);
4136 /* Find the current allocation for this port, if any */
4137 list_for_each_entry(pos, &topology_state->vcpis, next) {
4138 if (pos->port == port) {
4140 prev_slots = vcpi->vcpi;
4141 prev_bw = vcpi->pbn;
4144 * This should never happen, unless the driver tries
4145 * releasing and allocating the same VCPI allocation,
4148 if (WARN_ON(!prev_slots)) {
4149 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
4163 pbn_div = mgr->pbn_div;
4165 req_slots = DIV_ROUND_UP(pbn, pbn_div);
4167 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
4168 port->connector->base.id, port->connector->name,
4169 port, prev_slots, req_slots);
4170 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n",
4171 port->connector->base.id, port->connector->name,
4172 port, prev_bw, pbn);
4174 /* Add the new allocation to the state */
4176 vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL);
4180 drm_dp_mst_get_port_malloc(port);
4182 list_add(&vcpi->next, &topology_state->vcpis);
4184 vcpi->vcpi = req_slots;
4189 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
4192 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
4193 * @state: global atomic state
4194 * @mgr: MST topology manager for the port
4195 * @port: The port to release the VCPI slots from
4197 * Releases any VCPI slots that have been allocated to a port in the atomic
4198 * state. Any atomic drivers which support MST must call this function in
4199 * their &drm_connector_helper_funcs.atomic_check() callback when the
4200 * connector will no longer have VCPI allocated (e.g. because its CRTC was
4201 * removed) when it had VCPI allocated in the previous atomic state.
4203 * It is OK to call this even if @port has been removed from the system.
4204 * Additionally, it is OK to call this function multiple times on the same
4205 * @port as needed. It is not OK however, to call this function and
4206 * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
4210 * drm_dp_atomic_find_vcpi_slots()
4211 * drm_dp_mst_atomic_check()
4214 * 0 if all slots for this port were added back to
4215 * &drm_dp_mst_topology_state.avail_slots or negative error code
4217 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
4218 struct drm_dp_mst_topology_mgr *mgr,
4219 struct drm_dp_mst_port *port)
4221 struct drm_dp_mst_topology_state *topology_state;
4222 struct drm_dp_vcpi_allocation *pos;
4225 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
4226 if (IS_ERR(topology_state))
4227 return PTR_ERR(topology_state);
4229 list_for_each_entry(pos, &topology_state->vcpis, next) {
4230 if (pos->port == port) {
4235 if (WARN_ON(!found)) {
4236 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
4237 port, &topology_state->base);
4241 DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi);
4243 drm_dp_mst_put_port_malloc(port);
4250 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
4253 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
4254 * @mgr: manager for this port
4255 * @port: port to allocate a virtual channel for.
4256 * @pbn: payload bandwidth number to request
4257 * @slots: returned number of slots for this PBN.
4259 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
4260 struct drm_dp_mst_port *port, int pbn, int slots)
4264 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4271 if (port->vcpi.vcpi > 0) {
4272 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
4273 port->vcpi.vcpi, port->vcpi.pbn, pbn);
4274 if (pbn == port->vcpi.pbn) {
4275 drm_dp_mst_topology_put_port(port);
4280 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
4282 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
4283 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
4286 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
4287 pbn, port->vcpi.num_slots);
4289 /* Keep port allocated until its payload has been removed */
4290 drm_dp_mst_get_port_malloc(port);
4291 drm_dp_mst_topology_put_port(port);
4296 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
4298 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
4301 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4305 slots = port->vcpi.num_slots;
4306 drm_dp_mst_topology_put_port(port);
4309 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
4312 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
4313 * @mgr: manager for this port
4314 * @port: unverified pointer to a port.
4316 * This just resets the number of slots for the ports VCPI for later programming.
4318 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
4321 * A port with VCPI will remain allocated until its VCPI is
4322 * released, no verified ref needed
4325 port->vcpi.num_slots = 0;
4327 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
4330 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
4331 * @mgr: manager for this port
4332 * @port: port to deallocate vcpi for
4334 * This can be called unconditionally, regardless of whether
4335 * drm_dp_mst_allocate_vcpi() succeeded or not.
4337 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
4338 struct drm_dp_mst_port *port)
4340 if (!port->vcpi.vcpi)
4343 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
4344 port->vcpi.num_slots = 0;
4346 port->vcpi.aligned_pbn = 0;
4347 port->vcpi.vcpi = 0;
4348 drm_dp_mst_put_port_malloc(port);
4350 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
4352 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
4353 int id, struct drm_dp_payload *payload)
4355 u8 payload_alloc[3], status;
4359 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
4360 DP_PAYLOAD_TABLE_UPDATED);
4362 payload_alloc[0] = id;
4363 payload_alloc[1] = payload->start_slot;
4364 payload_alloc[2] = payload->num_slots;
4366 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
4368 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
4373 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
4375 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
4379 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
4382 usleep_range(10000, 20000);
4385 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
4394 static int do_get_act_status(struct drm_dp_aux *aux)
4399 ret = drm_dp_dpcd_readb(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
4407 * drm_dp_check_act_status() - Polls for ACT handled status.
4408 * @mgr: manager to use
4410 * Tries waiting for the MST hub to finish updating it's payload table by
4411 * polling for the ACT handled bit for up to 3 seconds (yes-some hubs really
4415 * 0 if the ACT was handled in time, negative error code on failure.
4417 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
4420 * There doesn't seem to be any recommended retry count or timeout in
4421 * the MST specification. Since some hubs have been observed to take
4422 * over 1 second to update their payload allocations under certain
4423 * conditions, we use a rather large timeout value.
4425 const int timeout_ms = 3000;
4428 ret = readx_poll_timeout(do_get_act_status, mgr->aux, status,
4429 status & DP_PAYLOAD_ACT_HANDLED || status < 0,
4430 200, timeout_ms * USEC_PER_MSEC);
4431 if (ret < 0 && status >= 0) {
4432 DRM_ERROR("Failed to get ACT after %dms, last status: %02x\n",
4433 timeout_ms, status);
4435 } else if (status < 0) {
4437 * Failure here isn't unexpected - the hub may have
4438 * just been unplugged
4440 DRM_DEBUG_KMS("Failed to read payload table status: %d\n",
4447 EXPORT_SYMBOL(drm_dp_check_act_status);
4450 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
4451 * @clock: dot clock for the mode
4452 * @bpp: bpp for the mode.
4453 * @dsc: DSC mode. If true, bpp has units of 1/16 of a bit per pixel
4455 * This uses the formula in the spec to calculate the PBN value for a mode.
4457 int drm_dp_calc_pbn_mode(int clock, int bpp, bool dsc)
4460 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
4461 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
4462 * common multiplier to render an integer PBN for all link rate/lane
4463 * counts combinations
4465 * peak_kbps *= (1006/1000)
4466 * peak_kbps *= (64/54)
4467 * peak_kbps *= 8 convert to bytes
4469 * If the bpp is in units of 1/16, further divide by 16. Put this
4470 * factor in the numerator rather than the denominator to avoid
4475 return DIV_ROUND_UP_ULL(mul_u32_u32(clock * (bpp / 16), 64 * 1006),
4476 8 * 54 * 1000 * 1000);
4478 return DIV_ROUND_UP_ULL(mul_u32_u32(clock * bpp, 64 * 1006),
4479 8 * 54 * 1000 * 1000);
4481 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
4483 /* we want to kick the TX after we've ack the up/down IRQs. */
4484 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
4486 queue_work(system_long_wq, &mgr->tx_work);
4489 static void drm_dp_mst_dump_mstb(struct seq_file *m,
4490 struct drm_dp_mst_branch *mstb)
4492 struct drm_dp_mst_port *port;
4493 int tabs = mstb->lct;
4497 for (i = 0; i < tabs; i++)
4501 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
4502 list_for_each_entry(port, &mstb->ports, next) {
4503 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);
4505 drm_dp_mst_dump_mstb(m, port->mstb);
4509 #define DP_PAYLOAD_TABLE_SIZE 64
4511 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
4516 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
4517 if (drm_dp_dpcd_read(mgr->aux,
4518 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
4525 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
4526 struct drm_dp_mst_port *port, char *name,
4529 struct edid *mst_edid;
4531 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
4532 drm_edid_get_monitor_name(mst_edid, name, namelen);
4536 * drm_dp_mst_dump_topology(): dump topology to seq file.
4537 * @m: seq_file to dump output to
4538 * @mgr: manager to dump current topology for.
4540 * helper to dump MST topology to a seq file for debugfs.
4542 void drm_dp_mst_dump_topology(struct seq_file *m,
4543 struct drm_dp_mst_topology_mgr *mgr)
4546 struct drm_dp_mst_port *port;
4548 mutex_lock(&mgr->lock);
4549 if (mgr->mst_primary)
4550 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
4553 mutex_unlock(&mgr->lock);
4555 mutex_lock(&mgr->payload_lock);
4556 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
4559 for (i = 0; i < mgr->max_payloads; i++) {
4560 if (mgr->proposed_vcpis[i]) {
4563 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
4564 fetch_monitor_name(mgr, port, name, sizeof(name));
4565 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
4566 port->port_num, port->vcpi.vcpi,
4567 port->vcpi.num_slots,
4568 (*name != 0) ? name : "Unknown");
4570 seq_printf(m, "vcpi %d:unused\n", i);
4572 for (i = 0; i < mgr->max_payloads; i++) {
4573 seq_printf(m, "payload %d: %d, %d, %d\n",
4575 mgr->payloads[i].payload_state,
4576 mgr->payloads[i].start_slot,
4577 mgr->payloads[i].num_slots);
4581 mutex_unlock(&mgr->payload_lock);
4583 mutex_lock(&mgr->lock);
4584 if (mgr->mst_primary) {
4585 u8 buf[DP_PAYLOAD_TABLE_SIZE];
4588 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
4590 seq_printf(m, "dpcd read failed\n");
4593 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
4595 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
4597 seq_printf(m, "faux/mst read failed\n");
4600 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
4602 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
4604 seq_printf(m, "mst ctrl read failed\n");
4607 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
4609 /* dump the standard OUI branch header */
4610 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
4612 seq_printf(m, "branch oui read failed\n");
4615 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
4617 for (i = 0x3; i < 0x8 && buf[i]; i++)
4618 seq_printf(m, "%c", buf[i]);
4619 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
4620 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
4621 if (dump_dp_payload_table(mgr, buf))
4622 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
4626 mutex_unlock(&mgr->lock);
4629 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
4631 static void drm_dp_tx_work(struct work_struct *work)
4633 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
4635 mutex_lock(&mgr->qlock);
4636 if (!list_empty(&mgr->tx_msg_downq))
4637 process_single_down_tx_qlock(mgr);
4638 mutex_unlock(&mgr->qlock);
4642 drm_dp_delayed_destroy_port(struct drm_dp_mst_port *port)
4644 if (port->connector) {
4645 drm_connector_unregister(port->connector);
4646 drm_connector_put(port->connector);
4649 drm_dp_port_set_pdt(port, DP_PEER_DEVICE_NONE, port->mcs);
4650 drm_dp_mst_put_port_malloc(port);
4654 drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch *mstb)
4656 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
4657 struct drm_dp_mst_port *port, *port_tmp;
4658 struct drm_dp_sideband_msg_tx *txmsg, *txmsg_tmp;
4659 bool wake_tx = false;
4661 mutex_lock(&mgr->lock);
4662 list_for_each_entry_safe(port, port_tmp, &mstb->ports, next) {
4663 list_del(&port->next);
4664 drm_dp_mst_topology_put_port(port);
4666 mutex_unlock(&mgr->lock);
4668 /* drop any tx slot msg */
4669 mutex_lock(&mstb->mgr->qlock);
4670 list_for_each_entry_safe(txmsg, txmsg_tmp, &mgr->tx_msg_downq, next) {
4671 if (txmsg->dst != mstb)
4674 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
4675 list_del(&txmsg->next);
4678 mutex_unlock(&mstb->mgr->qlock);
4681 wake_up_all(&mstb->mgr->tx_waitq);
4683 drm_dp_mst_put_mstb_malloc(mstb);
4686 static void drm_dp_delayed_destroy_work(struct work_struct *work)
4688 struct drm_dp_mst_topology_mgr *mgr =
4689 container_of(work, struct drm_dp_mst_topology_mgr,
4690 delayed_destroy_work);
4691 bool send_hotplug = false, go_again;
4694 * Not a regular list traverse as we have to drop the destroy
4695 * connector lock before destroying the mstb/port, to avoid AB->BA
4696 * ordering between this lock and the config mutex.
4702 struct drm_dp_mst_branch *mstb;
4704 mutex_lock(&mgr->delayed_destroy_lock);
4705 mstb = list_first_entry_or_null(&mgr->destroy_branch_device_list,
4706 struct drm_dp_mst_branch,
4709 list_del(&mstb->destroy_next);
4710 mutex_unlock(&mgr->delayed_destroy_lock);
4715 drm_dp_delayed_destroy_mstb(mstb);
4720 struct drm_dp_mst_port *port;
4722 mutex_lock(&mgr->delayed_destroy_lock);
4723 port = list_first_entry_or_null(&mgr->destroy_port_list,
4724 struct drm_dp_mst_port,
4727 list_del(&port->next);
4728 mutex_unlock(&mgr->delayed_destroy_lock);
4733 drm_dp_delayed_destroy_port(port);
4734 send_hotplug = true;
4740 drm_kms_helper_hotplug_event(mgr->dev);
4743 static struct drm_private_state *
4744 drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
4746 struct drm_dp_mst_topology_state *state, *old_state =
4747 to_dp_mst_topology_state(obj->state);
4748 struct drm_dp_vcpi_allocation *pos, *vcpi;
4750 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
4754 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
4756 INIT_LIST_HEAD(&state->vcpis);
4758 list_for_each_entry(pos, &old_state->vcpis, next) {
4759 /* Prune leftover freed VCPI allocations */
4763 vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL);
4767 drm_dp_mst_get_port_malloc(vcpi->port);
4768 list_add(&vcpi->next, &state->vcpis);
4771 return &state->base;
4774 list_for_each_entry_safe(pos, vcpi, &state->vcpis, next) {
4775 drm_dp_mst_put_port_malloc(pos->port);
4783 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
4784 struct drm_private_state *state)
4786 struct drm_dp_mst_topology_state *mst_state =
4787 to_dp_mst_topology_state(state);
4788 struct drm_dp_vcpi_allocation *pos, *tmp;
4790 list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next) {
4791 /* We only keep references to ports with non-zero VCPIs */
4793 drm_dp_mst_put_port_malloc(pos->port);
4800 static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
4801 struct drm_dp_mst_branch *branch)
4803 while (port->parent) {
4804 if (port->parent == branch)
4807 if (port->parent->port_parent)
4808 port = port->parent->port_parent;
4816 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
4817 struct drm_dp_mst_topology_state *state);
4820 drm_dp_mst_atomic_check_mstb_bw_limit(struct drm_dp_mst_branch *mstb,
4821 struct drm_dp_mst_topology_state *state)
4823 struct drm_dp_vcpi_allocation *vcpi;
4824 struct drm_dp_mst_port *port;
4825 int pbn_used = 0, ret;
4828 /* Check that we have at least one port in our state that's downstream
4829 * of this branch, otherwise we can skip this branch
4831 list_for_each_entry(vcpi, &state->vcpis, next) {
4833 !drm_dp_mst_port_downstream_of_branch(vcpi->port, mstb))
4842 if (mstb->port_parent)
4843 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n",
4844 mstb->port_parent->parent, mstb->port_parent,
4847 DRM_DEBUG_ATOMIC("[MSTB:%p] Checking bandwidth limits\n",
4850 list_for_each_entry(port, &mstb->ports, next) {
4851 ret = drm_dp_mst_atomic_check_port_bw_limit(port, state);
4862 drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
4863 struct drm_dp_mst_topology_state *state)
4865 struct drm_dp_vcpi_allocation *vcpi;
4868 if (port->pdt == DP_PEER_DEVICE_NONE)
4871 if (drm_dp_mst_is_end_device(port->pdt, port->mcs)) {
4874 list_for_each_entry(vcpi, &state->vcpis, next) {
4875 if (vcpi->port != port)
4886 /* This should never happen, as it means we tried to
4887 * set a mode before querying the full_pbn
4889 if (WARN_ON(!port->full_pbn))
4892 pbn_used = vcpi->pbn;
4894 pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(port->mstb,
4900 if (pbn_used > port->full_pbn) {
4901 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n",
4902 port->parent, port, pbn_used,
4907 DRM_DEBUG_ATOMIC("[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n",
4908 port->parent, port, pbn_used, port->full_pbn);
4914 drm_dp_mst_atomic_check_vcpi_alloc_limit(struct drm_dp_mst_topology_mgr *mgr,
4915 struct drm_dp_mst_topology_state *mst_state)
4917 struct drm_dp_vcpi_allocation *vcpi;
4918 int avail_slots = 63, payload_count = 0;
4920 list_for_each_entry(vcpi, &mst_state->vcpis, next) {
4921 /* Releasing VCPI is always OK-even if the port is gone */
4923 DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
4928 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
4929 vcpi->port, vcpi->vcpi);
4931 avail_slots -= vcpi->vcpi;
4932 if (avail_slots < 0) {
4933 DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
4934 vcpi->port, mst_state,
4935 avail_slots + vcpi->vcpi);
4939 if (++payload_count > mgr->max_payloads) {
4940 DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
4941 mgr, mst_state, mgr->max_payloads);
4945 DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
4946 mgr, mst_state, avail_slots,
4953 * drm_dp_mst_add_affected_dsc_crtcs
4954 * @state: Pointer to the new struct drm_dp_mst_topology_state
4955 * @mgr: MST topology manager
4957 * Whenever there is a change in mst topology
4958 * DSC configuration would have to be recalculated
4959 * therefore we need to trigger modeset on all affected
4960 * CRTCs in that topology
4963 * drm_dp_mst_atomic_enable_dsc()
4965 int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_state *state, struct drm_dp_mst_topology_mgr *mgr)
4967 struct drm_dp_mst_topology_state *mst_state;
4968 struct drm_dp_vcpi_allocation *pos;
4969 struct drm_connector *connector;
4970 struct drm_connector_state *conn_state;
4971 struct drm_crtc *crtc;
4972 struct drm_crtc_state *crtc_state;
4974 mst_state = drm_atomic_get_mst_topology_state(state, mgr);
4976 if (IS_ERR(mst_state))
4979 list_for_each_entry(pos, &mst_state->vcpis, next) {
4981 connector = pos->port->connector;
4986 conn_state = drm_atomic_get_connector_state(state, connector);
4988 if (IS_ERR(conn_state))
4989 return PTR_ERR(conn_state);
4991 crtc = conn_state->crtc;
4996 if (!drm_dp_mst_dsc_aux_for_port(pos->port))
4999 crtc_state = drm_atomic_get_crtc_state(mst_state->base.state, crtc);
5001 if (IS_ERR(crtc_state))
5002 return PTR_ERR(crtc_state);
5004 DRM_DEBUG_ATOMIC("[MST MGR:%p] Setting mode_changed flag on CRTC %p\n",
5007 crtc_state->mode_changed = true;
5011 EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs);
5014 * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off
5015 * @state: Pointer to the new drm_atomic_state
5016 * @port: Pointer to the affected MST Port
5017 * @pbn: Newly recalculated bw required for link with DSC enabled
5018 * @pbn_div: Divider to calculate correct number of pbn per slot
5019 * @enable: Boolean flag to enable or disable DSC on the port
5021 * This function enables DSC on the given Port
5022 * by recalculating its vcpi from pbn provided
5023 * and sets dsc_enable flag to keep track of which
5024 * ports have DSC enabled
5027 int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_state *state,
5028 struct drm_dp_mst_port *port,
5029 int pbn, int pbn_div,
5032 struct drm_dp_mst_topology_state *mst_state;
5033 struct drm_dp_vcpi_allocation *pos;
5037 mst_state = drm_atomic_get_mst_topology_state(state, port->mgr);
5039 if (IS_ERR(mst_state))
5040 return PTR_ERR(mst_state);
5042 list_for_each_entry(pos, &mst_state->vcpis, next) {
5043 if (pos->port == port) {
5050 DRM_DEBUG_ATOMIC("[MST PORT:%p] Couldn't find VCPI allocation in mst state %p\n",
5055 if (pos->dsc_enabled == enable) {
5056 DRM_DEBUG_ATOMIC("[MST PORT:%p] DSC flag is already set to %d, returning %d VCPI slots\n",
5057 port, enable, pos->vcpi);
5062 vcpi = drm_dp_atomic_find_vcpi_slots(state, port->mgr, port, pbn, pbn_div);
5063 DRM_DEBUG_ATOMIC("[MST PORT:%p] Enabling DSC flag, reallocating %d VCPI slots on the port\n",
5069 pos->dsc_enabled = enable;
5073 EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc);
5075 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
5076 * atomic update is valid
5077 * @state: Pointer to the new &struct drm_dp_mst_topology_state
5079 * Checks the given topology state for an atomic update to ensure that it's
5080 * valid. This includes checking whether there's enough bandwidth to support
5081 * the new VCPI allocations in the atomic update.
5083 * Any atomic drivers supporting DP MST must make sure to call this after
5084 * checking the rest of their state in their
5085 * &drm_mode_config_funcs.atomic_check() callback.
5088 * drm_dp_atomic_find_vcpi_slots()
5089 * drm_dp_atomic_release_vcpi_slots()
5093 * 0 if the new state is valid, negative error code otherwise.
5095 int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
5097 struct drm_dp_mst_topology_mgr *mgr;
5098 struct drm_dp_mst_topology_state *mst_state;
5101 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
5102 if (!mgr->mst_state)
5105 ret = drm_dp_mst_atomic_check_vcpi_alloc_limit(mgr, mst_state);
5109 mutex_lock(&mgr->lock);
5110 ret = drm_dp_mst_atomic_check_mstb_bw_limit(mgr->mst_primary,
5112 mutex_unlock(&mgr->lock);
5121 EXPORT_SYMBOL(drm_dp_mst_atomic_check);
5123 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
5124 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
5125 .atomic_destroy_state = drm_dp_mst_destroy_state,
5127 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
5130 * drm_atomic_get_mst_topology_state: get MST topology state
5132 * @state: global atomic state
5133 * @mgr: MST topology manager, also the private object in this case
5135 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
5136 * state vtable so that the private object state returned is that of a MST
5137 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
5138 * to care of the locking, so warn if don't hold the connection_mutex.
5142 * The MST topology state or error pointer.
5144 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
5145 struct drm_dp_mst_topology_mgr *mgr)
5147 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
5149 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
5152 * drm_dp_mst_topology_mgr_init - initialise a topology manager
5153 * @mgr: manager struct to initialise
5154 * @dev: device providing this structure - for i2c addition.
5155 * @aux: DP helper aux channel to talk to this device
5156 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
5157 * @max_payloads: maximum number of payloads this GPU can source
5158 * @conn_base_id: the connector object ID the MST device is connected to.
5160 * Return 0 for success, or negative error code on failure
5162 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
5163 struct drm_device *dev, struct drm_dp_aux *aux,
5164 int max_dpcd_transaction_bytes,
5165 int max_payloads, int conn_base_id)
5167 struct drm_dp_mst_topology_state *mst_state;
5169 mutex_init(&mgr->lock);
5170 mutex_init(&mgr->qlock);
5171 mutex_init(&mgr->payload_lock);
5172 mutex_init(&mgr->delayed_destroy_lock);
5173 mutex_init(&mgr->up_req_lock);
5174 mutex_init(&mgr->probe_lock);
5175 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
5176 mutex_init(&mgr->topology_ref_history_lock);
5178 INIT_LIST_HEAD(&mgr->tx_msg_downq);
5179 INIT_LIST_HEAD(&mgr->destroy_port_list);
5180 INIT_LIST_HEAD(&mgr->destroy_branch_device_list);
5181 INIT_LIST_HEAD(&mgr->up_req_list);
5182 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
5183 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
5184 INIT_WORK(&mgr->delayed_destroy_work, drm_dp_delayed_destroy_work);
5185 INIT_WORK(&mgr->up_req_work, drm_dp_mst_up_req_work);
5186 init_waitqueue_head(&mgr->tx_waitq);
5189 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
5190 mgr->max_payloads = max_payloads;
5191 mgr->conn_base_id = conn_base_id;
5192 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
5193 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
5195 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
5198 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
5199 if (!mgr->proposed_vcpis)
5201 set_bit(0, &mgr->payload_mask);
5203 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
5204 if (mst_state == NULL)
5207 mst_state->mgr = mgr;
5208 INIT_LIST_HEAD(&mst_state->vcpis);
5210 drm_atomic_private_obj_init(dev, &mgr->base,
5212 &drm_dp_mst_topology_state_funcs);
5216 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
5219 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
5220 * @mgr: manager to destroy
5222 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
5224 drm_dp_mst_topology_mgr_set_mst(mgr, false);
5225 flush_work(&mgr->work);
5226 cancel_work_sync(&mgr->delayed_destroy_work);
5227 mutex_lock(&mgr->payload_lock);
5228 kfree(mgr->payloads);
5229 mgr->payloads = NULL;
5230 kfree(mgr->proposed_vcpis);
5231 mgr->proposed_vcpis = NULL;
5232 mutex_unlock(&mgr->payload_lock);
5235 drm_atomic_private_obj_fini(&mgr->base);
5238 mutex_destroy(&mgr->delayed_destroy_lock);
5239 mutex_destroy(&mgr->payload_lock);
5240 mutex_destroy(&mgr->qlock);
5241 mutex_destroy(&mgr->lock);
5242 mutex_destroy(&mgr->up_req_lock);
5243 mutex_destroy(&mgr->probe_lock);
5244 #if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
5245 mutex_destroy(&mgr->topology_ref_history_lock);
5248 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
5250 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
5254 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
5257 for (i = 0; i < num - 1; i++) {
5258 if (msgs[i].flags & I2C_M_RD ||
5263 return msgs[num - 1].flags & I2C_M_RD &&
5264 msgs[num - 1].len <= 0xff;
5268 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
5271 struct drm_dp_aux *aux = adapter->algo_data;
5272 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
5273 struct drm_dp_mst_branch *mstb;
5274 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
5276 struct drm_dp_sideband_msg_req_body msg;
5277 struct drm_dp_sideband_msg_tx *txmsg = NULL;
5280 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
5284 if (!remote_i2c_read_ok(msgs, num)) {
5285 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
5290 memset(&msg, 0, sizeof(msg));
5291 msg.req_type = DP_REMOTE_I2C_READ;
5292 msg.u.i2c_read.num_transactions = num - 1;
5293 msg.u.i2c_read.port_number = port->port_num;
5294 for (i = 0; i < num - 1; i++) {
5295 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
5296 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
5297 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
5298 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
5300 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
5301 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
5303 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
5310 drm_dp_encode_sideband_req(&msg, txmsg);
5312 drm_dp_queue_down_tx(mgr, txmsg);
5314 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
5317 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
5321 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
5325 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
5330 drm_dp_mst_topology_put_mstb(mstb);
5334 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
5336 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
5337 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
5338 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
5339 I2C_FUNC_10BIT_ADDR;
5342 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
5343 .functionality = drm_dp_mst_i2c_functionality,
5344 .master_xfer = drm_dp_mst_i2c_xfer,
5348 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
5349 * @aux: DisplayPort AUX channel
5351 * Returns 0 on success or a negative error code on failure.
5353 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
5355 aux->ddc.algo = &drm_dp_mst_i2c_algo;
5356 aux->ddc.algo_data = aux;
5357 aux->ddc.retries = 3;
5359 aux->ddc.class = I2C_CLASS_DDC;
5360 aux->ddc.owner = THIS_MODULE;
5361 aux->ddc.dev.parent = aux->dev;
5362 aux->ddc.dev.of_node = aux->dev->of_node;
5364 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
5365 sizeof(aux->ddc.name));
5367 return i2c_add_adapter(&aux->ddc);
5371 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
5372 * @aux: DisplayPort AUX channel
5374 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
5376 i2c_del_adapter(&aux->ddc);
5380 * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device
5381 * @port: The port to check
5383 * A single physical MST hub object can be represented in the topology
5384 * by multiple branches, with virtual ports between those branches.
5386 * As of DP1.4, An MST hub with internal (virtual) ports must expose
5387 * certain DPCD registers over those ports. See sections 2.6.1.1.1
5388 * and 2.6.1.1.2 of Display Port specification v1.4 for details.
5390 * May acquire mgr->lock
5393 * true if the port is a virtual DP peer device, false otherwise
5395 static bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port *port)
5397 struct drm_dp_mst_port *downstream_port;
5399 if (!port || port->dpcd_rev < DP_DPCD_REV_14)
5402 /* Virtual DP Sink (Internal Display Panel) */
5403 if (port->port_num >= 8)
5406 /* DP-to-HDMI Protocol Converter */
5407 if (port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV &&
5413 mutex_lock(&port->mgr->lock);
5414 if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
5416 port->mstb->num_ports == 2) {
5417 list_for_each_entry(downstream_port, &port->mstb->ports, next) {
5418 if (downstream_port->pdt == DP_PEER_DEVICE_SST_SINK &&
5419 !downstream_port->input) {
5420 mutex_unlock(&port->mgr->lock);
5425 mutex_unlock(&port->mgr->lock);
5431 * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC
5432 * @port: The port to check. A leaf of the MST tree with an attached display.
5434 * Depending on the situation, DSC may be enabled via the endpoint aux,
5435 * the immediately upstream aux, or the connector's physical aux.
5437 * This is both the correct aux to read DSC_CAPABILITY and the
5438 * correct aux to write DSC_ENABLED.
5440 * This operation can be expensive (up to four aux reads), so
5441 * the caller should cache the return.
5444 * NULL if DSC cannot be enabled on this port, otherwise the aux device
5446 struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port)
5448 struct drm_dp_mst_port *immediate_upstream_port;
5449 struct drm_dp_mst_port *fec_port;
5450 struct drm_dp_desc desc = { };
5457 if (port->parent->port_parent)
5458 immediate_upstream_port = port->parent->port_parent;
5460 immediate_upstream_port = NULL;
5462 fec_port = immediate_upstream_port;
5465 * Each physical link (i.e. not a virtual port) between the
5466 * output and the primary device must support FEC
5468 if (!drm_dp_mst_is_virtual_dpcd(fec_port) &&
5469 !fec_port->fec_capable)
5472 fec_port = fec_port->parent->port_parent;
5475 /* DP-to-DP peer device */
5476 if (drm_dp_mst_is_virtual_dpcd(immediate_upstream_port)) {
5479 if (drm_dp_dpcd_read(&port->aux,
5480 DP_DSC_SUPPORT, &endpoint_dsc, 1) != 1)
5482 if (drm_dp_dpcd_read(&port->aux,
5483 DP_FEC_CAPABILITY, &endpoint_fec, 1) != 1)
5485 if (drm_dp_dpcd_read(&immediate_upstream_port->aux,
5486 DP_DSC_SUPPORT, &upstream_dsc, 1) != 1)
5489 /* Enpoint decompression with DP-to-DP peer device */
5490 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
5491 (endpoint_fec & DP_FEC_CAPABLE) &&
5492 (upstream_dsc & 0x2) /* DSC passthrough */)
5495 /* Virtual DPCD decompression with DP-to-DP peer device */
5496 return &immediate_upstream_port->aux;
5499 /* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */
5500 if (drm_dp_mst_is_virtual_dpcd(port))
5505 * Applies to ports for which:
5506 * - Physical aux has Synaptics OUI
5507 * - DPv1.4 or higher
5508 * - Port is on primary branch device
5509 * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG)
5511 if (drm_dp_read_desc(port->mgr->aux, &desc, true))
5514 if (drm_dp_has_quirk(&desc, 0,
5515 DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) &&
5516 port->mgr->dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14 &&
5517 port->parent == port->mgr->mst_primary) {
5520 if (drm_dp_dpcd_read(&port->aux, DP_DOWNSTREAMPORT_PRESENT,
5521 &downstreamport, 1) < 0)
5524 if ((downstreamport & DP_DWN_STRM_PORT_PRESENT) &&
5525 ((downstreamport & DP_DWN_STRM_PORT_TYPE_MASK)
5526 != DP_DWN_STRM_PORT_TYPE_ANALOG))
5527 return port->mgr->aux;
5531 * The check below verifies if the MST sink
5532 * connected to the GPU is capable of DSC -
5533 * therefore the endpoint needs to be
5534 * both DSC and FEC capable.
5536 if (drm_dp_dpcd_read(&port->aux,
5537 DP_DSC_SUPPORT, &endpoint_dsc, 1) != 1)
5539 if (drm_dp_dpcd_read(&port->aux,
5540 DP_FEC_CAPABILITY, &endpoint_fec, 1) != 1)
5542 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
5543 (endpoint_fec & DP_FEC_CAPABLE))
5548 EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port);