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>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_dp_mst_helper.h>
34 #include <drm/drm_drv.h>
35 #include <drm/drm_print.h>
36 #include <drm/drm_probe_helper.h>
38 #include "drm_crtc_helper_internal.h"
39 #include "drm_dp_mst_topology_internal.h"
44 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
45 * protocol. The helpers contain a topology manager and bandwidth manager.
46 * The helpers encapsulate the sending and received of sideband msgs.
48 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
51 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
53 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
55 struct drm_dp_payload *payload);
57 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
58 struct drm_dp_mst_port *port,
59 int offset, int size, u8 *bytes);
60 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
61 struct drm_dp_mst_port *port,
62 int offset, int size, u8 *bytes);
64 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
65 struct drm_dp_mst_branch *mstb);
68 drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
69 struct drm_dp_mst_branch *mstb);
71 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
72 struct drm_dp_mst_branch *mstb,
73 struct drm_dp_mst_port *port);
74 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
77 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
78 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
79 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
81 #define DBG_PREFIX "[dp_mst]"
83 #define DP_STR(x) [DP_ ## x] = #x
85 static const char *drm_dp_mst_req_type_str(u8 req_type)
87 static const char * const req_type_str[] = {
88 DP_STR(GET_MSG_TRANSACTION_VERSION),
90 DP_STR(CONNECTION_STATUS_NOTIFY),
91 DP_STR(ENUM_PATH_RESOURCES),
92 DP_STR(ALLOCATE_PAYLOAD),
93 DP_STR(QUERY_PAYLOAD),
94 DP_STR(RESOURCE_STATUS_NOTIFY),
95 DP_STR(CLEAR_PAYLOAD_ID_TABLE),
96 DP_STR(REMOTE_DPCD_READ),
97 DP_STR(REMOTE_DPCD_WRITE),
98 DP_STR(REMOTE_I2C_READ),
99 DP_STR(REMOTE_I2C_WRITE),
100 DP_STR(POWER_UP_PHY),
101 DP_STR(POWER_DOWN_PHY),
102 DP_STR(SINK_EVENT_NOTIFY),
103 DP_STR(QUERY_STREAM_ENC_STATUS),
106 if (req_type >= ARRAY_SIZE(req_type_str) ||
107 !req_type_str[req_type])
110 return req_type_str[req_type];
114 #define DP_STR(x) [DP_NAK_ ## x] = #x
116 static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
118 static const char * const nak_reason_str[] = {
119 DP_STR(WRITE_FAILURE),
120 DP_STR(INVALID_READ),
124 DP_STR(LINK_FAILURE),
125 DP_STR(NO_RESOURCES),
128 DP_STR(ALLOCATE_FAIL),
131 if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
132 !nak_reason_str[nak_reason])
135 return nak_reason_str[nak_reason];
139 #define DP_STR(x) [DRM_DP_SIDEBAND_TX_ ## x] = #x
141 static const char *drm_dp_mst_sideband_tx_state_str(int state)
143 static const char * const sideband_reason_str[] = {
151 if (state >= ARRAY_SIZE(sideband_reason_str) ||
152 !sideband_reason_str[state])
155 return sideband_reason_str[state];
159 drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len)
164 for (i = 0; i < lct; i++) {
166 unpacked_rad[i] = rad[i / 2] >> 4;
168 unpacked_rad[i] = rad[i / 2] & BIT_MASK(4);
171 /* TODO: Eventually add something to printk so we can format the rad
174 return snprintf(out, len, "%*phC", lct, unpacked_rad);
177 /* sideband msg handling */
178 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
183 int number_of_bits = num_nibbles * 4;
186 while (number_of_bits != 0) {
189 remainder |= (data[array_index] & bitmask) >> bitshift;
197 if ((remainder & 0x10) == 0x10)
202 while (number_of_bits != 0) {
205 if ((remainder & 0x10) != 0)
212 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
217 int number_of_bits = number_of_bytes * 8;
220 while (number_of_bits != 0) {
223 remainder |= (data[array_index] & bitmask) >> bitshift;
231 if ((remainder & 0x100) == 0x100)
236 while (number_of_bits != 0) {
239 if ((remainder & 0x100) != 0)
243 return remainder & 0xff;
245 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
248 size += (hdr->lct / 2);
252 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
258 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
259 for (i = 0; i < (hdr->lct / 2); i++)
260 buf[idx++] = hdr->rad[i];
261 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
262 (hdr->msg_len & 0x3f);
263 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
265 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
266 buf[idx - 1] |= (crc4 & 0xf);
271 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
272 u8 *buf, int buflen, u8 *hdrlen)
281 len += ((buf[0] & 0xf0) >> 4) / 2;
284 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
286 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
287 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
291 hdr->lct = (buf[0] & 0xf0) >> 4;
292 hdr->lcr = (buf[0] & 0xf);
294 for (i = 0; i < (hdr->lct / 2); i++)
295 hdr->rad[i] = buf[idx++];
296 hdr->broadcast = (buf[idx] >> 7) & 0x1;
297 hdr->path_msg = (buf[idx] >> 6) & 0x1;
298 hdr->msg_len = buf[idx] & 0x3f;
300 hdr->somt = (buf[idx] >> 7) & 0x1;
301 hdr->eomt = (buf[idx] >> 6) & 0x1;
302 hdr->seqno = (buf[idx] >> 4) & 0x1;
309 drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body *req,
310 struct drm_dp_sideband_msg_tx *raw)
315 buf[idx++] = req->req_type & 0x7f;
317 switch (req->req_type) {
318 case DP_ENUM_PATH_RESOURCES:
319 case DP_POWER_DOWN_PHY:
320 case DP_POWER_UP_PHY:
321 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
324 case DP_ALLOCATE_PAYLOAD:
325 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
326 (req->u.allocate_payload.number_sdp_streams & 0xf);
328 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
330 buf[idx] = (req->u.allocate_payload.pbn >> 8);
332 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
334 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
335 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
336 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
339 if (req->u.allocate_payload.number_sdp_streams & 1) {
340 i = req->u.allocate_payload.number_sdp_streams - 1;
341 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
345 case DP_QUERY_PAYLOAD:
346 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
348 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
351 case DP_REMOTE_DPCD_READ:
352 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
353 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
355 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
357 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
359 buf[idx] = (req->u.dpcd_read.num_bytes);
363 case DP_REMOTE_DPCD_WRITE:
364 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
365 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
367 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
369 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
371 buf[idx] = (req->u.dpcd_write.num_bytes);
373 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
374 idx += req->u.dpcd_write.num_bytes;
376 case DP_REMOTE_I2C_READ:
377 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
378 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
380 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
381 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
383 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
385 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
386 idx += req->u.i2c_read.transactions[i].num_bytes;
388 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
389 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
392 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
394 buf[idx] = (req->u.i2c_read.num_bytes_read);
398 case DP_REMOTE_I2C_WRITE:
399 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
401 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
403 buf[idx] = (req->u.i2c_write.num_bytes);
405 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
406 idx += req->u.i2c_write.num_bytes;
411 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req);
413 /* Decode a sideband request we've encoded, mainly used for debugging */
415 drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx *raw,
416 struct drm_dp_sideband_msg_req_body *req)
418 const u8 *buf = raw->msg;
421 req->req_type = buf[idx++] & 0x7f;
422 switch (req->req_type) {
423 case DP_ENUM_PATH_RESOURCES:
424 case DP_POWER_DOWN_PHY:
425 case DP_POWER_UP_PHY:
426 req->u.port_num.port_number = (buf[idx] >> 4) & 0xf;
428 case DP_ALLOCATE_PAYLOAD:
430 struct drm_dp_allocate_payload *a =
431 &req->u.allocate_payload;
433 a->number_sdp_streams = buf[idx] & 0xf;
434 a->port_number = (buf[idx] >> 4) & 0xf;
436 WARN_ON(buf[++idx] & 0x80);
437 a->vcpi = buf[idx] & 0x7f;
439 a->pbn = buf[++idx] << 8;
440 a->pbn |= buf[++idx];
443 for (i = 0; i < a->number_sdp_streams; i++) {
444 a->sdp_stream_sink[i] =
445 (buf[idx + (i / 2)] >> ((i % 2) ? 0 : 4)) & 0xf;
449 case DP_QUERY_PAYLOAD:
450 req->u.query_payload.port_number = (buf[idx] >> 4) & 0xf;
451 WARN_ON(buf[++idx] & 0x80);
452 req->u.query_payload.vcpi = buf[idx] & 0x7f;
454 case DP_REMOTE_DPCD_READ:
456 struct drm_dp_remote_dpcd_read *r = &req->u.dpcd_read;
458 r->port_number = (buf[idx] >> 4) & 0xf;
460 r->dpcd_address = (buf[idx] << 16) & 0xf0000;
461 r->dpcd_address |= (buf[++idx] << 8) & 0xff00;
462 r->dpcd_address |= buf[++idx] & 0xff;
464 r->num_bytes = buf[++idx];
467 case DP_REMOTE_DPCD_WRITE:
469 struct drm_dp_remote_dpcd_write *w =
472 w->port_number = (buf[idx] >> 4) & 0xf;
474 w->dpcd_address = (buf[idx] << 16) & 0xf0000;
475 w->dpcd_address |= (buf[++idx] << 8) & 0xff00;
476 w->dpcd_address |= buf[++idx] & 0xff;
478 w->num_bytes = buf[++idx];
480 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
486 case DP_REMOTE_I2C_READ:
488 struct drm_dp_remote_i2c_read *r = &req->u.i2c_read;
489 struct drm_dp_remote_i2c_read_tx *tx;
492 r->num_transactions = buf[idx] & 0x3;
493 r->port_number = (buf[idx] >> 4) & 0xf;
494 for (i = 0; i < r->num_transactions; i++) {
495 tx = &r->transactions[i];
497 tx->i2c_dev_id = buf[++idx] & 0x7f;
498 tx->num_bytes = buf[++idx];
499 tx->bytes = kmemdup(&buf[++idx],
506 idx += tx->num_bytes;
507 tx->no_stop_bit = (buf[idx] >> 5) & 0x1;
508 tx->i2c_transaction_delay = buf[idx] & 0xf;
512 for (i = 0; i < r->num_transactions; i++) {
513 tx = &r->transactions[i];
519 r->read_i2c_device_id = buf[++idx] & 0x7f;
520 r->num_bytes_read = buf[++idx];
523 case DP_REMOTE_I2C_WRITE:
525 struct drm_dp_remote_i2c_write *w = &req->u.i2c_write;
527 w->port_number = (buf[idx] >> 4) & 0xf;
528 w->write_i2c_device_id = buf[++idx] & 0x7f;
529 w->num_bytes = buf[++idx];
530 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
540 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req);
543 drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body *req,
544 int indent, struct drm_printer *printer)
548 #define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)
549 if (req->req_type == DP_LINK_ADDRESS) {
550 /* No contents to print */
551 P("type=%s\n", drm_dp_mst_req_type_str(req->req_type));
555 P("type=%s contents:\n", drm_dp_mst_req_type_str(req->req_type));
558 switch (req->req_type) {
559 case DP_ENUM_PATH_RESOURCES:
560 case DP_POWER_DOWN_PHY:
561 case DP_POWER_UP_PHY:
562 P("port=%d\n", req->u.port_num.port_number);
564 case DP_ALLOCATE_PAYLOAD:
565 P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
566 req->u.allocate_payload.port_number,
567 req->u.allocate_payload.vcpi, req->u.allocate_payload.pbn,
568 req->u.allocate_payload.number_sdp_streams,
569 req->u.allocate_payload.number_sdp_streams,
570 req->u.allocate_payload.sdp_stream_sink);
572 case DP_QUERY_PAYLOAD:
573 P("port=%d vcpi=%d\n",
574 req->u.query_payload.port_number,
575 req->u.query_payload.vcpi);
577 case DP_REMOTE_DPCD_READ:
578 P("port=%d dpcd_addr=%05x len=%d\n",
579 req->u.dpcd_read.port_number, req->u.dpcd_read.dpcd_address,
580 req->u.dpcd_read.num_bytes);
582 case DP_REMOTE_DPCD_WRITE:
583 P("port=%d addr=%05x len=%d: %*ph\n",
584 req->u.dpcd_write.port_number,
585 req->u.dpcd_write.dpcd_address,
586 req->u.dpcd_write.num_bytes, req->u.dpcd_write.num_bytes,
587 req->u.dpcd_write.bytes);
589 case DP_REMOTE_I2C_READ:
590 P("port=%d num_tx=%d id=%d size=%d:\n",
591 req->u.i2c_read.port_number,
592 req->u.i2c_read.num_transactions,
593 req->u.i2c_read.read_i2c_device_id,
594 req->u.i2c_read.num_bytes_read);
597 for (i = 0; i < req->u.i2c_read.num_transactions; i++) {
598 const struct drm_dp_remote_i2c_read_tx *rtx =
599 &req->u.i2c_read.transactions[i];
601 P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
602 i, rtx->i2c_dev_id, rtx->num_bytes,
603 rtx->no_stop_bit, rtx->i2c_transaction_delay,
604 rtx->num_bytes, rtx->bytes);
607 case DP_REMOTE_I2C_WRITE:
608 P("port=%d id=%d size=%d: %*ph\n",
609 req->u.i2c_write.port_number,
610 req->u.i2c_write.write_i2c_device_id,
611 req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
612 req->u.i2c_write.bytes);
620 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body);
623 drm_dp_mst_dump_sideband_msg_tx(struct drm_printer *p,
624 const struct drm_dp_sideband_msg_tx *txmsg)
626 struct drm_dp_sideband_msg_req_body req;
631 drm_dp_mst_rad_to_str(txmsg->dst->rad, txmsg->dst->lct, buf,
633 drm_printf(p, "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
634 txmsg->cur_offset, txmsg->cur_len, txmsg->seqno,
635 drm_dp_mst_sideband_tx_state_str(txmsg->state),
636 txmsg->path_msg, buf);
638 ret = drm_dp_decode_sideband_req(txmsg, &req);
640 drm_printf(p, "<failed to decode sideband req: %d>\n", ret);
643 drm_dp_dump_sideband_msg_req_body(&req, 1, p);
645 switch (req.req_type) {
646 case DP_REMOTE_DPCD_WRITE:
647 kfree(req.u.dpcd_write.bytes);
649 case DP_REMOTE_I2C_READ:
650 for (i = 0; i < req.u.i2c_read.num_transactions; i++)
651 kfree(req.u.i2c_read.transactions[i].bytes);
653 case DP_REMOTE_I2C_WRITE:
654 kfree(req.u.i2c_write.bytes);
659 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
662 crc4 = drm_dp_msg_data_crc4(msg, len);
666 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
667 struct drm_dp_sideband_msg_tx *raw)
672 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
677 /* this adds a chunk of msg to the builder to get the final msg */
678 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
679 u8 *replybuf, u8 replybuflen, bool hdr)
686 struct drm_dp_sideband_msg_hdr recv_hdr;
687 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
689 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
694 * ignore out-of-order messages or messages that are part of a
697 if (!recv_hdr.somt && !msg->have_somt)
700 /* get length contained in this portion */
701 msg->curchunk_len = recv_hdr.msg_len;
702 msg->curchunk_hdrlen = hdrlen;
704 /* we have already gotten an somt - don't bother parsing */
705 if (recv_hdr.somt && msg->have_somt)
709 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
710 msg->have_somt = true;
713 msg->have_eomt = true;
715 /* copy the bytes for the remainder of this header chunk */
716 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
717 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
719 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
720 msg->curchunk_idx += replybuflen;
723 if (msg->curchunk_idx >= msg->curchunk_len) {
725 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
726 /* copy chunk into bigger msg */
727 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
728 msg->curlen += msg->curchunk_len - 1;
733 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
734 struct drm_dp_sideband_msg_reply_body *repmsg)
738 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
740 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
742 if (idx > raw->curlen)
744 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
745 if (raw->msg[idx] & 0x80)
746 repmsg->u.link_addr.ports[i].input_port = 1;
748 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
749 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
752 if (idx > raw->curlen)
754 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
755 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
756 if (repmsg->u.link_addr.ports[i].input_port == 0)
757 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
759 if (idx > raw->curlen)
761 if (repmsg->u.link_addr.ports[i].input_port == 0) {
762 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
764 if (idx > raw->curlen)
766 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
768 if (idx > raw->curlen)
770 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
771 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
775 if (idx > raw->curlen)
781 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
785 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
786 struct drm_dp_sideband_msg_reply_body *repmsg)
789 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
791 if (idx > raw->curlen)
793 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
795 if (idx > raw->curlen)
798 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
801 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
805 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
806 struct drm_dp_sideband_msg_reply_body *repmsg)
809 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
811 if (idx > raw->curlen)
815 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
819 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
820 struct drm_dp_sideband_msg_reply_body *repmsg)
824 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
826 if (idx > raw->curlen)
828 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
831 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
834 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
838 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
839 struct drm_dp_sideband_msg_reply_body *repmsg)
842 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
844 if (idx > raw->curlen)
846 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
848 if (idx > raw->curlen)
850 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
852 if (idx > raw->curlen)
856 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
860 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
861 struct drm_dp_sideband_msg_reply_body *repmsg)
864 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
866 if (idx > raw->curlen)
868 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
870 if (idx > raw->curlen)
872 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
874 if (idx > raw->curlen)
878 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
882 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
883 struct drm_dp_sideband_msg_reply_body *repmsg)
886 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
888 if (idx > raw->curlen)
890 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
892 if (idx > raw->curlen)
896 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
900 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
901 struct drm_dp_sideband_msg_reply_body *repmsg)
905 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
907 if (idx > raw->curlen) {
908 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
915 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
916 struct drm_dp_sideband_msg_reply_body *msg)
918 memset(msg, 0, sizeof(*msg));
919 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
920 msg->req_type = (raw->msg[0] & 0x7f);
922 if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
923 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
924 msg->u.nak.reason = raw->msg[17];
925 msg->u.nak.nak_data = raw->msg[18];
929 switch (msg->req_type) {
930 case DP_LINK_ADDRESS:
931 return drm_dp_sideband_parse_link_address(raw, msg);
932 case DP_QUERY_PAYLOAD:
933 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
934 case DP_REMOTE_DPCD_READ:
935 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
936 case DP_REMOTE_DPCD_WRITE:
937 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
938 case DP_REMOTE_I2C_READ:
939 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
940 case DP_ENUM_PATH_RESOURCES:
941 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
942 case DP_ALLOCATE_PAYLOAD:
943 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
944 case DP_POWER_DOWN_PHY:
945 case DP_POWER_UP_PHY:
946 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
947 case DP_CLEAR_PAYLOAD_ID_TABLE:
948 return true; /* since there's nothing to parse */
950 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
951 drm_dp_mst_req_type_str(msg->req_type));
956 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
957 struct drm_dp_sideband_msg_req_body *msg)
961 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
963 if (idx > raw->curlen)
966 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
968 if (idx > raw->curlen)
971 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
972 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
973 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
974 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
975 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
979 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
983 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
984 struct drm_dp_sideband_msg_req_body *msg)
988 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
990 if (idx > raw->curlen)
993 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
995 if (idx > raw->curlen)
998 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
1002 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
1006 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
1007 struct drm_dp_sideband_msg_req_body *msg)
1009 memset(msg, 0, sizeof(*msg));
1010 msg->req_type = (raw->msg[0] & 0x7f);
1012 switch (msg->req_type) {
1013 case DP_CONNECTION_STATUS_NOTIFY:
1014 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
1015 case DP_RESOURCE_STATUS_NOTIFY:
1016 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
1018 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,
1019 drm_dp_mst_req_type_str(msg->req_type));
1024 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
1026 struct drm_dp_sideband_msg_req_body req;
1028 req.req_type = DP_REMOTE_DPCD_WRITE;
1029 req.u.dpcd_write.port_number = port_num;
1030 req.u.dpcd_write.dpcd_address = offset;
1031 req.u.dpcd_write.num_bytes = num_bytes;
1032 req.u.dpcd_write.bytes = bytes;
1033 drm_dp_encode_sideband_req(&req, msg);
1038 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
1040 struct drm_dp_sideband_msg_req_body req;
1042 req.req_type = DP_LINK_ADDRESS;
1043 drm_dp_encode_sideband_req(&req, msg);
1047 static int build_clear_payload_id_table(struct drm_dp_sideband_msg_tx *msg)
1049 struct drm_dp_sideband_msg_req_body req;
1051 req.req_type = DP_CLEAR_PAYLOAD_ID_TABLE;
1052 drm_dp_encode_sideband_req(&req, msg);
1056 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
1058 struct drm_dp_sideband_msg_req_body req;
1060 req.req_type = DP_ENUM_PATH_RESOURCES;
1061 req.u.port_num.port_number = port_num;
1062 drm_dp_encode_sideband_req(&req, msg);
1063 msg->path_msg = true;
1067 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
1068 u8 vcpi, uint16_t pbn,
1069 u8 number_sdp_streams,
1070 u8 *sdp_stream_sink)
1072 struct drm_dp_sideband_msg_req_body req;
1073 memset(&req, 0, sizeof(req));
1074 req.req_type = DP_ALLOCATE_PAYLOAD;
1075 req.u.allocate_payload.port_number = port_num;
1076 req.u.allocate_payload.vcpi = vcpi;
1077 req.u.allocate_payload.pbn = pbn;
1078 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
1079 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
1080 number_sdp_streams);
1081 drm_dp_encode_sideband_req(&req, msg);
1082 msg->path_msg = true;
1086 static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
1087 int port_num, bool power_up)
1089 struct drm_dp_sideband_msg_req_body req;
1092 req.req_type = DP_POWER_UP_PHY;
1094 req.req_type = DP_POWER_DOWN_PHY;
1096 req.u.port_num.port_number = port_num;
1097 drm_dp_encode_sideband_req(&req, msg);
1098 msg->path_msg = true;
1102 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1103 struct drm_dp_vcpi *vcpi)
1107 mutex_lock(&mgr->payload_lock);
1108 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
1109 if (ret > mgr->max_payloads) {
1111 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
1115 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
1116 if (vcpi_ret > mgr->max_payloads) {
1118 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
1122 set_bit(ret, &mgr->payload_mask);
1123 set_bit(vcpi_ret, &mgr->vcpi_mask);
1124 vcpi->vcpi = vcpi_ret + 1;
1125 mgr->proposed_vcpis[ret - 1] = vcpi;
1127 mutex_unlock(&mgr->payload_lock);
1131 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1138 mutex_lock(&mgr->payload_lock);
1139 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
1140 clear_bit(vcpi - 1, &mgr->vcpi_mask);
1142 for (i = 0; i < mgr->max_payloads; i++) {
1143 if (mgr->proposed_vcpis[i] &&
1144 mgr->proposed_vcpis[i]->vcpi == vcpi) {
1145 mgr->proposed_vcpis[i] = NULL;
1146 clear_bit(i + 1, &mgr->payload_mask);
1149 mutex_unlock(&mgr->payload_lock);
1152 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
1153 struct drm_dp_sideband_msg_tx *txmsg)
1158 * All updates to txmsg->state are protected by mgr->qlock, and the two
1159 * cases we check here are terminal states. For those the barriers
1160 * provided by the wake_up/wait_event pair are enough.
1162 state = READ_ONCE(txmsg->state);
1163 return (state == DRM_DP_SIDEBAND_TX_RX ||
1164 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
1167 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
1168 struct drm_dp_sideband_msg_tx *txmsg)
1170 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1173 ret = wait_event_timeout(mgr->tx_waitq,
1174 check_txmsg_state(mgr, txmsg),
1176 mutex_lock(&mstb->mgr->qlock);
1178 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
1183 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
1185 /* dump some state */
1189 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
1190 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
1191 list_del(&txmsg->next);
1194 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
1195 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
1196 mstb->tx_slots[txmsg->seqno] = NULL;
1200 if (unlikely(ret == -EIO) && drm_debug_enabled(DRM_UT_DP)) {
1201 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
1203 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
1205 mutex_unlock(&mgr->qlock);
1210 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
1212 struct drm_dp_mst_branch *mstb;
1214 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
1220 memcpy(mstb->rad, rad, lct / 2);
1221 INIT_LIST_HEAD(&mstb->ports);
1222 kref_init(&mstb->topology_kref);
1223 kref_init(&mstb->malloc_kref);
1227 static void drm_dp_free_mst_branch_device(struct kref *kref)
1229 struct drm_dp_mst_branch *mstb =
1230 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
1232 if (mstb->port_parent)
1233 drm_dp_mst_put_port_malloc(mstb->port_parent);
1239 * DOC: Branch device and port refcounting
1241 * Topology refcount overview
1242 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
1244 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
1245 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
1246 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
1248 * Topology refcounts are not exposed to drivers, and are handled internally
1249 * by the DP MST helpers. The helpers use them in order to prevent the
1250 * in-memory topology state from being changed in the middle of critical
1251 * operations like changing the internal state of payload allocations. This
1252 * means each branch and port will be considered to be connected to the rest
1253 * of the topology until its topology refcount reaches zero. Additionally,
1254 * for ports this means that their associated &struct drm_connector will stay
1255 * registered with userspace until the port's refcount reaches 0.
1257 * Malloc refcount overview
1258 * ~~~~~~~~~~~~~~~~~~~~~~~~
1260 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
1261 * drm_dp_mst_branch allocated even after all of its topology references have
1262 * been dropped, so that the driver or MST helpers can safely access each
1263 * branch's last known state before it was disconnected from the topology.
1264 * When the malloc refcount of a port or branch reaches 0, the memory
1265 * allocation containing the &struct drm_dp_mst_branch or &struct
1266 * drm_dp_mst_port respectively will be freed.
1268 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
1269 * to drivers. As of writing this documentation, there are no drivers that
1270 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
1271 * helpers. Exposing this API to drivers in a race-free manner would take more
1272 * tweaking of the refcounting scheme, however patches are welcome provided
1273 * there is a legitimate driver usecase for this.
1275 * Refcount relationships in a topology
1276 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1278 * Let's take a look at why the relationship between topology and malloc
1279 * refcounts is designed the way it is.
1281 * .. kernel-figure:: dp-mst/topology-figure-1.dot
1283 * An example of topology and malloc refs in a DP MST topology with two
1284 * active payloads. Topology refcount increments are indicated by solid
1285 * lines, and malloc refcount increments are indicated by dashed lines.
1286 * Each starts from the branch which incremented the refcount, and ends at
1287 * the branch to which the refcount belongs to, i.e. the arrow points the
1288 * same way as the C pointers used to reference a structure.
1290 * As you can see in the above figure, every branch increments the topology
1291 * refcount of its children, and increments the malloc refcount of its
1292 * parent. Additionally, every payload increments the malloc refcount of its
1293 * assigned port by 1.
1295 * So, what would happen if MSTB #3 from the above figure was unplugged from
1296 * the system, but the driver hadn't yet removed payload #2 from port #3? The
1297 * topology would start to look like the figure below.
1299 * .. kernel-figure:: dp-mst/topology-figure-2.dot
1301 * Ports and branch devices which have been released from memory are
1302 * colored grey, and references which have been removed are colored red.
1304 * Whenever a port or branch device's topology refcount reaches zero, it will
1305 * decrement the topology refcounts of all its children, the malloc refcount
1306 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
1307 * #4, this means they both have been disconnected from the topology and freed
1308 * from memory. But, because payload #2 is still holding a reference to port
1309 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1310 * is still accessible from memory. This also means port #3 has not yet
1311 * decremented the malloc refcount of MSTB #3, so its &struct
1312 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1313 * malloc refcount reaches 0.
1315 * This relationship is necessary because in order to release payload #2, we
1316 * need to be able to figure out the last relative of port #3 that's still
1317 * connected to the topology. In this case, we would travel up the topology as
1320 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1322 * And finally, remove payload #2 by communicating with port #2 through
1323 * sideband transactions.
1327 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1329 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1331 * Increments &drm_dp_mst_branch.malloc_kref. When
1332 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1333 * will be released and @mstb may no longer be used.
1335 * See also: drm_dp_mst_put_mstb_malloc()
1338 drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1340 kref_get(&mstb->malloc_kref);
1341 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
1345 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1347 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1349 * Decrements &drm_dp_mst_branch.malloc_kref. When
1350 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1351 * will be released and @mstb may no longer be used.
1353 * See also: drm_dp_mst_get_mstb_malloc()
1356 drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1358 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
1359 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
1362 static void drm_dp_free_mst_port(struct kref *kref)
1364 struct drm_dp_mst_port *port =
1365 container_of(kref, struct drm_dp_mst_port, malloc_kref);
1367 drm_dp_mst_put_mstb_malloc(port->parent);
1372 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1373 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1375 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1376 * reaches 0, the memory allocation for @port will be released and @port may
1377 * no longer be used.
1379 * Because @port could potentially be freed at any time by the DP MST helpers
1380 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1381 * function, drivers that which to make use of &struct drm_dp_mst_port should
1382 * ensure that they grab at least one main malloc reference to their MST ports
1383 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1384 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1386 * See also: drm_dp_mst_put_port_malloc()
1389 drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1391 kref_get(&port->malloc_kref);
1392 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1394 EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1397 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1398 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1400 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1401 * reaches 0, the memory allocation for @port will be released and @port may
1402 * no longer be used.
1404 * See also: drm_dp_mst_get_port_malloc()
1407 drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1409 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1410 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1412 EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1414 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1416 struct drm_dp_mst_branch *mstb =
1417 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1418 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1419 struct drm_dp_mst_port *port, *tmp;
1420 bool wake_tx = false;
1422 mutex_lock(&mgr->lock);
1423 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
1424 list_del(&port->next);
1425 drm_dp_mst_topology_put_port(port);
1427 mutex_unlock(&mgr->lock);
1429 /* drop any tx slots msg */
1430 mutex_lock(&mstb->mgr->qlock);
1431 if (mstb->tx_slots[0]) {
1432 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1433 mstb->tx_slots[0] = NULL;
1436 if (mstb->tx_slots[1]) {
1437 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1438 mstb->tx_slots[1] = NULL;
1441 mutex_unlock(&mstb->mgr->qlock);
1444 wake_up_all(&mstb->mgr->tx_waitq);
1446 drm_dp_mst_put_mstb_malloc(mstb);
1450 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1451 * branch device unless it's zero
1452 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1454 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1455 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1456 * reached 0). Holding a topology reference implies that a malloc reference
1457 * will be held to @mstb as long as the user holds the topology reference.
1459 * Care should be taken to ensure that the user has at least one malloc
1460 * reference to @mstb. If you already have a topology reference to @mstb, you
1461 * should use drm_dp_mst_topology_get_mstb() instead.
1464 * drm_dp_mst_topology_get_mstb()
1465 * drm_dp_mst_topology_put_mstb()
1468 * * 1: A topology reference was grabbed successfully
1469 * * 0: @port is no longer in the topology, no reference was grabbed
1471 static int __must_check
1472 drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1474 int ret = kref_get_unless_zero(&mstb->topology_kref);
1477 DRM_DEBUG("mstb %p (%d)\n", mstb,
1478 kref_read(&mstb->topology_kref));
1484 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1486 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1488 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1489 * not it's already reached 0. This is only valid to use in scenarios where
1490 * you are already guaranteed to have at least one active topology reference
1491 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1494 * drm_dp_mst_topology_try_get_mstb()
1495 * drm_dp_mst_topology_put_mstb()
1497 static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1499 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1500 kref_get(&mstb->topology_kref);
1501 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1505 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1507 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1509 * Releases a topology reference from @mstb by decrementing
1510 * &drm_dp_mst_branch.topology_kref.
1513 * drm_dp_mst_topology_try_get_mstb()
1514 * drm_dp_mst_topology_get_mstb()
1517 drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1519 DRM_DEBUG("mstb %p (%d)\n",
1520 mstb, kref_read(&mstb->topology_kref) - 1);
1521 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1524 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
1526 struct drm_dp_mst_branch *mstb;
1529 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1530 case DP_PEER_DEVICE_SST_SINK:
1531 /* remove i2c over sideband */
1532 drm_dp_mst_unregister_i2c_bus(&port->aux);
1534 case DP_PEER_DEVICE_MST_BRANCHING:
1537 drm_dp_mst_topology_put_mstb(mstb);
1542 static void drm_dp_destroy_port(struct kref *kref)
1544 struct drm_dp_mst_port *port =
1545 container_of(kref, struct drm_dp_mst_port, topology_kref);
1546 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1549 kfree(port->cached_edid);
1552 * The only time we don't have a connector
1553 * on an output port is if the connector init
1556 if (port->connector) {
1557 /* we can't destroy the connector here, as
1558 * we might be holding the mode_config.mutex
1559 * from an EDID retrieval */
1561 mutex_lock(&mgr->destroy_connector_lock);
1562 list_add(&port->next, &mgr->destroy_connector_list);
1563 mutex_unlock(&mgr->destroy_connector_lock);
1564 schedule_work(&mgr->destroy_connector_work);
1567 /* no need to clean up vcpi
1568 * as if we have no connector we never setup a vcpi */
1569 drm_dp_port_teardown_pdt(port, port->pdt);
1570 port->pdt = DP_PEER_DEVICE_NONE;
1572 drm_dp_mst_put_port_malloc(port);
1576 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1577 * port unless it's zero
1578 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1580 * Attempts to grab a topology reference to @port, if it hasn't yet been
1581 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1582 * 0). Holding a topology reference implies that a malloc reference will be
1583 * held to @port as long as the user holds the topology reference.
1585 * Care should be taken to ensure that the user has at least one malloc
1586 * reference to @port. If you already have a topology reference to @port, you
1587 * should use drm_dp_mst_topology_get_port() instead.
1590 * drm_dp_mst_topology_get_port()
1591 * drm_dp_mst_topology_put_port()
1594 * * 1: A topology reference was grabbed successfully
1595 * * 0: @port is no longer in the topology, no reference was grabbed
1597 static int __must_check
1598 drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1600 int ret = kref_get_unless_zero(&port->topology_kref);
1603 DRM_DEBUG("port %p (%d)\n", port,
1604 kref_read(&port->topology_kref));
1610 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1611 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1613 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1614 * not it's already reached 0. This is only valid to use in scenarios where
1615 * you are already guaranteed to have at least one active topology reference
1616 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1619 * drm_dp_mst_topology_try_get_port()
1620 * drm_dp_mst_topology_put_port()
1622 static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1624 WARN_ON(kref_read(&port->topology_kref) == 0);
1625 kref_get(&port->topology_kref);
1626 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1630 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1631 * @port: The &struct drm_dp_mst_port to release the topology reference from
1633 * Releases a topology reference from @port by decrementing
1634 * &drm_dp_mst_port.topology_kref.
1637 * drm_dp_mst_topology_try_get_port()
1638 * drm_dp_mst_topology_get_port()
1640 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1642 DRM_DEBUG("port %p (%d)\n",
1643 port, kref_read(&port->topology_kref) - 1);
1644 kref_put(&port->topology_kref, drm_dp_destroy_port);
1647 static struct drm_dp_mst_branch *
1648 drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1649 struct drm_dp_mst_branch *to_find)
1651 struct drm_dp_mst_port *port;
1652 struct drm_dp_mst_branch *rmstb;
1654 if (to_find == mstb)
1657 list_for_each_entry(port, &mstb->ports, next) {
1659 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1660 port->mstb, to_find);
1668 static struct drm_dp_mst_branch *
1669 drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1670 struct drm_dp_mst_branch *mstb)
1672 struct drm_dp_mst_branch *rmstb = NULL;
1674 mutex_lock(&mgr->lock);
1675 if (mgr->mst_primary) {
1676 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1677 mgr->mst_primary, mstb);
1679 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1682 mutex_unlock(&mgr->lock);
1686 static struct drm_dp_mst_port *
1687 drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1688 struct drm_dp_mst_port *to_find)
1690 struct drm_dp_mst_port *port, *mport;
1692 list_for_each_entry(port, &mstb->ports, next) {
1693 if (port == to_find)
1697 mport = drm_dp_mst_topology_get_port_validated_locked(
1698 port->mstb, to_find);
1706 static struct drm_dp_mst_port *
1707 drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1708 struct drm_dp_mst_port *port)
1710 struct drm_dp_mst_port *rport = NULL;
1712 mutex_lock(&mgr->lock);
1713 if (mgr->mst_primary) {
1714 rport = drm_dp_mst_topology_get_port_validated_locked(
1715 mgr->mst_primary, port);
1717 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1720 mutex_unlock(&mgr->lock);
1724 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1726 struct drm_dp_mst_port *port;
1729 list_for_each_entry(port, &mstb->ports, next) {
1730 if (port->port_num == port_num) {
1731 ret = drm_dp_mst_topology_try_get_port(port);
1732 return ret ? port : NULL;
1740 * calculate a new RAD for this MST branch device
1741 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1742 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1744 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1747 int parent_lct = port->parent->lct;
1749 int idx = (parent_lct - 1) / 2;
1750 if (parent_lct > 1) {
1751 memcpy(rad, port->parent->rad, idx + 1);
1752 shift = (parent_lct % 2) ? 4 : 0;
1756 rad[idx] |= port->port_num << shift;
1757 return parent_lct + 1;
1761 * return sends link address for new mstb
1763 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1767 bool send_link = false;
1768 switch (port->pdt) {
1769 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1770 case DP_PEER_DEVICE_SST_SINK:
1771 /* add i2c over sideband */
1772 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1774 case DP_PEER_DEVICE_MST_BRANCHING:
1775 lct = drm_dp_calculate_rad(port, rad);
1777 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1779 port->mstb->mgr = port->mgr;
1780 port->mstb->port_parent = port;
1782 * Make sure this port's memory allocation stays
1783 * around until its child MSTB releases it
1785 drm_dp_mst_get_port_malloc(port);
1795 * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
1796 * @aux: Fake sideband AUX CH
1797 * @offset: address of the (first) register to read
1798 * @buffer: buffer to store the register values
1799 * @size: number of bytes in @buffer
1801 * Performs the same functionality for remote devices via
1802 * sideband messaging as drm_dp_dpcd_read() does for local
1803 * devices via actual AUX CH.
1805 * Return: Number of bytes read, or negative error code on failure.
1807 ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
1808 unsigned int offset, void *buffer, size_t size)
1810 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
1813 return drm_dp_send_dpcd_read(port->mgr, port,
1814 offset, size, buffer);
1818 * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
1819 * @aux: Fake sideband AUX CH
1820 * @offset: address of the (first) register to write
1821 * @buffer: buffer containing the values to write
1822 * @size: number of bytes in @buffer
1824 * Performs the same functionality for remote devices via
1825 * sideband messaging as drm_dp_dpcd_write() does for local
1826 * devices via actual AUX CH.
1828 * Return: 0 on success, negative error code on failure.
1830 ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
1831 unsigned int offset, void *buffer, size_t size)
1833 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
1836 return drm_dp_send_dpcd_write(port->mgr, port,
1837 offset, size, buffer);
1840 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1844 memcpy(mstb->guid, guid, 16);
1846 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1847 if (mstb->port_parent) {
1848 ret = drm_dp_send_dpcd_write(
1856 ret = drm_dp_dpcd_write(
1865 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1868 size_t proppath_size)
1872 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1873 for (i = 0; i < (mstb->lct - 1); i++) {
1874 int shift = (i % 2) ? 0 : 4;
1875 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1876 snprintf(temp, sizeof(temp), "-%d", port_num);
1877 strlcat(proppath, temp, proppath_size);
1879 snprintf(temp, sizeof(temp), "-%d", pnum);
1880 strlcat(proppath, temp, proppath_size);
1884 * drm_dp_mst_connector_late_register() - Late MST connector registration
1885 * @connector: The MST connector
1886 * @port: The MST port for this connector
1888 * Helper to register the remote aux device for this MST port. Drivers should
1889 * call this from their mst connector's late_register hook to enable MST aux
1892 * Return: 0 on success, negative error code on failure.
1894 int drm_dp_mst_connector_late_register(struct drm_connector *connector,
1895 struct drm_dp_mst_port *port)
1897 DRM_DEBUG_KMS("registering %s remote bus for %s\n",
1898 port->aux.name, connector->kdev->kobj.name);
1900 port->aux.dev = connector->kdev;
1901 return drm_dp_aux_register_devnode(&port->aux);
1903 EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
1906 * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
1907 * @connector: The MST connector
1908 * @port: The MST port for this connector
1910 * Helper to unregister the remote aux device for this MST port, registered by
1911 * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
1912 * connector's early_unregister hook.
1914 void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
1915 struct drm_dp_mst_port *port)
1917 DRM_DEBUG_KMS("unregistering %s remote bus for %s\n",
1918 port->aux.name, connector->kdev->kobj.name);
1919 drm_dp_aux_unregister_devnode(&port->aux);
1921 EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
1924 drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch *mstb,
1925 struct drm_device *dev,
1926 struct drm_dp_link_addr_reply_port *port_msg)
1928 struct drm_dp_mst_port *port;
1930 bool created = false;
1934 port = drm_dp_get_port(mstb, port_msg->port_number);
1936 port = kzalloc(sizeof(*port), GFP_KERNEL);
1939 kref_init(&port->topology_kref);
1940 kref_init(&port->malloc_kref);
1941 port->parent = mstb;
1942 port->port_num = port_msg->port_number;
1943 port->mgr = mstb->mgr;
1944 port->aux.name = "DPMST";
1945 port->aux.dev = dev->dev;
1946 port->aux.is_remote = true;
1949 * Make sure the memory allocation for our parent branch stays
1950 * around until our own memory allocation is released
1952 drm_dp_mst_get_mstb_malloc(mstb);
1956 old_pdt = port->pdt;
1957 old_ddps = port->ddps;
1960 port->pdt = port_msg->peer_device_type;
1961 port->input = port_msg->input_port;
1962 port->mcs = port_msg->mcs;
1963 port->ddps = port_msg->ddps;
1964 port->ldps = port_msg->legacy_device_plug_status;
1965 port->dpcd_rev = port_msg->dpcd_revision;
1966 port->num_sdp_streams = port_msg->num_sdp_streams;
1967 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1969 /* manage mstb port lists with mgr lock - take a reference
1972 mutex_lock(&mstb->mgr->lock);
1973 drm_dp_mst_topology_get_port(port);
1974 list_add(&port->next, &mstb->ports);
1975 mutex_unlock(&mstb->mgr->lock);
1978 if (old_ddps != port->ddps) {
1981 drm_dp_send_enum_path_resources(mstb->mgr,
1985 port->available_pbn = 0;
1989 if (old_pdt != port->pdt && !port->input) {
1990 drm_dp_port_teardown_pdt(port, old_pdt);
1992 ret = drm_dp_port_setup_pdt(port);
1994 drm_dp_send_link_address(mstb->mgr, port->mstb);
1997 if (created && !port->input) {
2000 build_mst_prop_path(mstb, port->port_num, proppath,
2002 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
2005 if (!port->connector) {
2006 /* remove it from the port list */
2007 mutex_lock(&mstb->mgr->lock);
2008 list_del(&port->next);
2009 mutex_unlock(&mstb->mgr->lock);
2010 /* drop port list reference */
2011 drm_dp_mst_topology_put_port(port);
2014 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
2015 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
2016 port->port_num >= DP_MST_LOGICAL_PORT_0) {
2017 port->cached_edid = drm_get_edid(port->connector,
2019 drm_connector_set_tile_property(port->connector);
2021 (*mstb->mgr->cbs->register_connector)(port->connector);
2025 /* put reference to this port */
2026 drm_dp_mst_topology_put_port(port);
2030 drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
2031 struct drm_dp_connection_status_notify *conn_stat)
2033 struct drm_dp_mst_port *port;
2036 bool dowork = false;
2037 port = drm_dp_get_port(mstb, conn_stat->port_number);
2041 old_ddps = port->ddps;
2042 old_pdt = port->pdt;
2043 port->pdt = conn_stat->peer_device_type;
2044 port->mcs = conn_stat->message_capability_status;
2045 port->ldps = conn_stat->legacy_device_plug_status;
2046 port->ddps = conn_stat->displayport_device_plug_status;
2048 if (old_ddps != port->ddps) {
2052 port->available_pbn = 0;
2055 if (old_pdt != port->pdt && !port->input) {
2056 drm_dp_port_teardown_pdt(port, old_pdt);
2058 if (drm_dp_port_setup_pdt(port))
2062 drm_dp_mst_topology_put_port(port);
2064 queue_work(system_long_wq, &mstb->mgr->work);
2068 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
2071 struct drm_dp_mst_branch *mstb;
2072 struct drm_dp_mst_port *port;
2074 /* find the port by iterating down */
2076 mutex_lock(&mgr->lock);
2077 mstb = mgr->mst_primary;
2082 for (i = 0; i < lct - 1; i++) {
2083 int shift = (i % 2) ? 0 : 4;
2084 int port_num = (rad[i / 2] >> shift) & 0xf;
2086 list_for_each_entry(port, &mstb->ports, next) {
2087 if (port->port_num == port_num) {
2090 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
2098 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2102 mutex_unlock(&mgr->lock);
2106 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
2107 struct drm_dp_mst_branch *mstb,
2108 const uint8_t *guid)
2110 struct drm_dp_mst_branch *found_mstb;
2111 struct drm_dp_mst_port *port;
2113 if (memcmp(mstb->guid, guid, 16) == 0)
2117 list_for_each_entry(port, &mstb->ports, next) {
2121 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
2130 static struct drm_dp_mst_branch *
2131 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
2132 const uint8_t *guid)
2134 struct drm_dp_mst_branch *mstb;
2137 /* find the port by iterating down */
2138 mutex_lock(&mgr->lock);
2140 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
2142 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2147 mutex_unlock(&mgr->lock);
2151 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2152 struct drm_dp_mst_branch *mstb)
2154 struct drm_dp_mst_port *port;
2155 struct drm_dp_mst_branch *mstb_child;
2156 if (!mstb->link_address_sent)
2157 drm_dp_send_link_address(mgr, mstb);
2159 list_for_each_entry(port, &mstb->ports, next) {
2166 if (!port->available_pbn)
2167 drm_dp_send_enum_path_resources(mgr, mstb, port);
2170 mstb_child = drm_dp_mst_topology_get_mstb_validated(
2173 drm_dp_check_and_send_link_address(mgr, mstb_child);
2174 drm_dp_mst_topology_put_mstb(mstb_child);
2180 static void drm_dp_mst_link_probe_work(struct work_struct *work)
2182 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
2183 struct drm_dp_mst_branch *mstb;
2185 bool clear_payload_id_table;
2187 mutex_lock(&mgr->lock);
2188 clear_payload_id_table = !mgr->payload_id_table_cleared;
2189 mgr->payload_id_table_cleared = true;
2191 mstb = mgr->mst_primary;
2193 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2197 mutex_unlock(&mgr->lock);
2202 * Certain branch devices seem to incorrectly report an available_pbn
2203 * of 0 on downstream sinks, even after clearing the
2204 * DP_PAYLOAD_ALLOCATE_* registers in
2205 * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C
2206 * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make
2207 * things work again.
2209 if (clear_payload_id_table) {
2210 DRM_DEBUG_KMS("Clearing payload ID table\n");
2211 drm_dp_send_clear_payload_id_table(mgr, mstb);
2214 drm_dp_check_and_send_link_address(mgr, mstb);
2215 drm_dp_mst_topology_put_mstb(mstb);
2218 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
2223 if (memchr_inv(guid, 0, 16))
2226 salt = get_jiffies_64();
2228 memcpy(&guid[0], &salt, sizeof(u64));
2229 memcpy(&guid[8], &salt, sizeof(u64));
2234 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
2236 struct drm_dp_sideband_msg_req_body req;
2238 req.req_type = DP_REMOTE_DPCD_READ;
2239 req.u.dpcd_read.port_number = port_num;
2240 req.u.dpcd_read.dpcd_address = offset;
2241 req.u.dpcd_read.num_bytes = num_bytes;
2242 drm_dp_encode_sideband_req(&req, msg);
2247 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
2248 bool up, u8 *msg, int len)
2251 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
2252 int tosend, total, offset;
2259 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
2261 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
2264 if (ret != tosend) {
2265 if (ret == -EIO && retries < 5) {
2269 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
2275 } while (total > 0);
2279 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
2280 struct drm_dp_sideband_msg_tx *txmsg)
2282 struct drm_dp_mst_branch *mstb = txmsg->dst;
2285 /* both msg slots are full */
2286 if (txmsg->seqno == -1) {
2287 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
2288 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
2291 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
2292 txmsg->seqno = mstb->last_seqno;
2293 mstb->last_seqno ^= 1;
2294 } else if (mstb->tx_slots[0] == NULL)
2298 mstb->tx_slots[txmsg->seqno] = txmsg;
2301 req_type = txmsg->msg[0] & 0x7f;
2302 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
2303 req_type == DP_RESOURCE_STATUS_NOTIFY)
2307 hdr->path_msg = txmsg->path_msg;
2308 hdr->lct = mstb->lct;
2309 hdr->lcr = mstb->lct - 1;
2311 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
2312 hdr->seqno = txmsg->seqno;
2316 * process a single block of the next message in the sideband queue
2318 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2319 struct drm_dp_sideband_msg_tx *txmsg,
2323 struct drm_dp_sideband_msg_hdr hdr;
2324 int len, space, idx, tosend;
2327 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
2329 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
2331 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
2334 /* make hdr from dst mst - for replies use seqno
2335 otherwise assign one */
2336 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
2340 /* amount left to send in this message */
2341 len = txmsg->cur_len - txmsg->cur_offset;
2343 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
2344 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
2346 tosend = min(len, space);
2347 if (len == txmsg->cur_len)
2353 hdr.msg_len = tosend + 1;
2354 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
2355 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
2356 /* add crc at end */
2357 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
2360 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
2361 if (unlikely(ret) && drm_debug_enabled(DRM_UT_DP)) {
2362 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
2364 drm_printf(&p, "sideband msg failed to send\n");
2365 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2369 txmsg->cur_offset += tosend;
2370 if (txmsg->cur_offset == txmsg->cur_len) {
2371 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
2377 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
2379 struct drm_dp_sideband_msg_tx *txmsg;
2382 WARN_ON(!mutex_is_locked(&mgr->qlock));
2384 /* construct a chunk from the first msg in the tx_msg queue */
2385 if (list_empty(&mgr->tx_msg_downq))
2388 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
2389 ret = process_single_tx_qlock(mgr, txmsg, false);
2391 /* txmsg is sent it should be in the slots now */
2392 list_del(&txmsg->next);
2394 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
2395 list_del(&txmsg->next);
2396 if (txmsg->seqno != -1)
2397 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
2398 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
2399 wake_up_all(&mgr->tx_waitq);
2403 /* called holding qlock */
2404 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2405 struct drm_dp_sideband_msg_tx *txmsg)
2409 /* construct a chunk from the first msg in the tx_msg queue */
2410 ret = process_single_tx_qlock(mgr, txmsg, true);
2413 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
2415 if (txmsg->seqno != -1) {
2416 WARN_ON((unsigned int)txmsg->seqno >
2417 ARRAY_SIZE(txmsg->dst->tx_slots));
2418 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
2422 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2423 struct drm_dp_sideband_msg_tx *txmsg)
2425 mutex_lock(&mgr->qlock);
2426 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
2428 if (drm_debug_enabled(DRM_UT_DP)) {
2429 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
2431 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2434 if (list_is_singular(&mgr->tx_msg_downq))
2435 process_single_down_tx_qlock(mgr);
2436 mutex_unlock(&mgr->qlock);
2440 drm_dp_dump_link_address(struct drm_dp_link_address_ack_reply *reply)
2442 struct drm_dp_link_addr_reply_port *port_reply;
2445 for (i = 0; i < reply->nports; i++) {
2446 port_reply = &reply->ports[i];
2447 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",
2449 port_reply->input_port,
2450 port_reply->peer_device_type,
2451 port_reply->port_number,
2452 port_reply->dpcd_revision,
2455 port_reply->legacy_device_plug_status,
2456 port_reply->num_sdp_streams,
2457 port_reply->num_sdp_stream_sinks);
2461 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2462 struct drm_dp_mst_branch *mstb)
2464 struct drm_dp_sideband_msg_tx *txmsg;
2465 struct drm_dp_link_address_ack_reply *reply;
2468 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2473 len = build_link_address(txmsg);
2475 mstb->link_address_sent = true;
2476 drm_dp_queue_down_tx(mgr, txmsg);
2478 /* FIXME: Actually do some real error handling here */
2479 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2481 DRM_ERROR("Sending link address failed with %d\n", ret);
2484 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2485 DRM_ERROR("link address NAK received\n");
2490 reply = &txmsg->reply.u.link_addr;
2491 DRM_DEBUG_KMS("link address reply: %d\n", reply->nports);
2492 drm_dp_dump_link_address(reply);
2494 drm_dp_check_mstb_guid(mstb, reply->guid);
2496 for (i = 0; i < reply->nports; i++)
2497 drm_dp_mst_handle_link_address_port(mstb, mgr->dev,
2500 drm_kms_helper_hotplug_event(mgr->dev);
2504 mstb->link_address_sent = false;
2508 void drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
2509 struct drm_dp_mst_branch *mstb)
2511 struct drm_dp_sideband_msg_tx *txmsg;
2514 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2519 len = build_clear_payload_id_table(txmsg);
2521 drm_dp_queue_down_tx(mgr, txmsg);
2523 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2524 if (ret > 0 && txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2525 DRM_DEBUG_KMS("clear payload table id nak received\n");
2531 drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2532 struct drm_dp_mst_branch *mstb,
2533 struct drm_dp_mst_port *port)
2535 struct drm_dp_enum_path_resources_ack_reply *path_res;
2536 struct drm_dp_sideband_msg_tx *txmsg;
2540 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2545 len = build_enum_path_resources(txmsg, port->port_num);
2547 drm_dp_queue_down_tx(mgr, txmsg);
2549 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2551 path_res = &txmsg->reply.u.path_resources;
2553 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2554 DRM_DEBUG_KMS("enum path resources nak received\n");
2556 if (port->port_num != path_res->port_number)
2557 DRM_ERROR("got incorrect port in response\n");
2559 DRM_DEBUG_KMS("enum path resources %d: %d %d\n",
2560 path_res->port_number,
2561 path_res->full_payload_bw_number,
2562 path_res->avail_payload_bw_number);
2563 port->available_pbn =
2564 path_res->avail_payload_bw_number;
2572 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2574 if (!mstb->port_parent)
2577 if (mstb->port_parent->mstb != mstb)
2578 return mstb->port_parent;
2580 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2584 * Searches upwards in the topology starting from mstb to try to find the
2585 * closest available parent of mstb that's still connected to the rest of the
2586 * topology. This can be used in order to perform operations like releasing
2587 * payloads, where the branch device which owned the payload may no longer be
2588 * around and thus would require that the payload on the last living relative
2591 static struct drm_dp_mst_branch *
2592 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2593 struct drm_dp_mst_branch *mstb,
2596 struct drm_dp_mst_branch *rmstb = NULL;
2597 struct drm_dp_mst_port *found_port;
2599 mutex_lock(&mgr->lock);
2600 if (!mgr->mst_primary)
2604 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
2608 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
2609 rmstb = found_port->parent;
2610 *port_num = found_port->port_num;
2612 /* Search again, starting from this parent */
2613 mstb = found_port->parent;
2617 mutex_unlock(&mgr->lock);
2621 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
2622 struct drm_dp_mst_port *port,
2626 struct drm_dp_sideband_msg_tx *txmsg;
2627 struct drm_dp_mst_branch *mstb;
2628 int len, ret, port_num;
2629 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
2632 port_num = port->port_num;
2633 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2635 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
2643 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2649 for (i = 0; i < port->num_sdp_streams; i++)
2653 len = build_allocate_payload(txmsg, port_num,
2655 pbn, port->num_sdp_streams, sinks);
2657 drm_dp_queue_down_tx(mgr, txmsg);
2660 * FIXME: there is a small chance that between getting the last
2661 * connected mstb and sending the payload message, the last connected
2662 * mstb could also be removed from the topology. In the future, this
2663 * needs to be fixed by restarting the
2664 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
2665 * timeout if the topology is still connected to the system.
2667 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2669 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2676 drm_dp_mst_topology_put_mstb(mstb);
2680 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
2681 struct drm_dp_mst_port *port, bool power_up)
2683 struct drm_dp_sideband_msg_tx *txmsg;
2686 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2690 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2692 drm_dp_mst_topology_put_port(port);
2696 txmsg->dst = port->parent;
2697 len = build_power_updown_phy(txmsg, port->port_num, power_up);
2698 drm_dp_queue_down_tx(mgr, txmsg);
2700 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
2702 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2708 drm_dp_mst_topology_put_port(port);
2712 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
2714 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2716 struct drm_dp_payload *payload)
2720 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
2722 payload->payload_state = 0;
2725 payload->payload_state = DP_PAYLOAD_LOCAL;
2729 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2730 struct drm_dp_mst_port *port,
2732 struct drm_dp_payload *payload)
2735 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
2738 payload->payload_state = DP_PAYLOAD_REMOTE;
2742 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2743 struct drm_dp_mst_port *port,
2745 struct drm_dp_payload *payload)
2747 DRM_DEBUG_KMS("\n");
2748 /* it's okay for these to fail */
2750 drm_dp_payload_send_msg(mgr, port, id, 0);
2753 drm_dp_dpcd_write_payload(mgr, id, payload);
2754 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
2758 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2760 struct drm_dp_payload *payload)
2762 payload->payload_state = 0;
2767 * drm_dp_update_payload_part1() - Execute payload update part 1
2768 * @mgr: manager to use.
2770 * This iterates over all proposed virtual channels, and tries to
2771 * allocate space in the link for them. For 0->slots transitions,
2772 * this step just writes the VCPI to the MST device. For slots->0
2773 * transitions, this writes the updated VCPIs and removes the
2774 * remote VC payloads.
2776 * after calling this the driver should generate ACT and payload
2779 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
2781 struct drm_dp_payload req_payload;
2782 struct drm_dp_mst_port *port;
2786 mutex_lock(&mgr->payload_lock);
2787 for (i = 0; i < mgr->max_payloads; i++) {
2788 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2789 struct drm_dp_payload *payload = &mgr->payloads[i];
2790 bool put_port = false;
2792 /* solve the current payloads - compare to the hw ones
2793 - update the hw view */
2794 req_payload.start_slot = cur_slots;
2796 port = container_of(vcpi, struct drm_dp_mst_port,
2799 /* Validated ports don't matter if we're releasing
2802 if (vcpi->num_slots) {
2803 port = drm_dp_mst_topology_get_port_validated(
2806 mutex_unlock(&mgr->payload_lock);
2812 req_payload.num_slots = vcpi->num_slots;
2813 req_payload.vcpi = vcpi->vcpi;
2816 req_payload.num_slots = 0;
2819 payload->start_slot = req_payload.start_slot;
2820 /* work out what is required to happen with this payload */
2821 if (payload->num_slots != req_payload.num_slots) {
2823 /* need to push an update for this payload */
2824 if (req_payload.num_slots) {
2825 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
2827 payload->num_slots = req_payload.num_slots;
2828 payload->vcpi = req_payload.vcpi;
2830 } else if (payload->num_slots) {
2831 payload->num_slots = 0;
2832 drm_dp_destroy_payload_step1(mgr, port,
2835 req_payload.payload_state =
2836 payload->payload_state;
2837 payload->start_slot = 0;
2839 payload->payload_state = req_payload.payload_state;
2841 cur_slots += req_payload.num_slots;
2844 drm_dp_mst_topology_put_port(port);
2847 for (i = 0; i < mgr->max_payloads; i++) {
2848 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL)
2851 DRM_DEBUG_KMS("removing payload %d\n", i);
2852 for (j = i; j < mgr->max_payloads - 1; j++) {
2853 mgr->payloads[j] = mgr->payloads[j + 1];
2854 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
2856 if (mgr->proposed_vcpis[j] &&
2857 mgr->proposed_vcpis[j]->num_slots) {
2858 set_bit(j + 1, &mgr->payload_mask);
2860 clear_bit(j + 1, &mgr->payload_mask);
2864 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
2865 sizeof(struct drm_dp_payload));
2866 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
2867 clear_bit(mgr->max_payloads, &mgr->payload_mask);
2869 mutex_unlock(&mgr->payload_lock);
2873 EXPORT_SYMBOL(drm_dp_update_payload_part1);
2876 * drm_dp_update_payload_part2() - Execute payload update part 2
2877 * @mgr: manager to use.
2879 * This iterates over all proposed virtual channels, and tries to
2880 * allocate space in the link for them. For 0->slots transitions,
2881 * this step writes the remote VC payload commands. For slots->0
2882 * this just resets some internal state.
2884 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
2886 struct drm_dp_mst_port *port;
2889 mutex_lock(&mgr->payload_lock);
2890 for (i = 0; i < mgr->max_payloads; i++) {
2892 if (!mgr->proposed_vcpis[i])
2895 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2897 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2898 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
2899 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2900 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
2901 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2904 mutex_unlock(&mgr->payload_lock);
2908 mutex_unlock(&mgr->payload_lock);
2911 EXPORT_SYMBOL(drm_dp_update_payload_part2);
2913 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2914 struct drm_dp_mst_port *port,
2915 int offset, int size, u8 *bytes)
2919 struct drm_dp_sideband_msg_tx *txmsg;
2920 struct drm_dp_mst_branch *mstb;
2922 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2926 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2932 len = build_dpcd_read(txmsg, port->port_num, offset, size);
2933 txmsg->dst = port->parent;
2935 drm_dp_queue_down_tx(mgr, txmsg);
2937 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2941 /* DPCD read should never be NACKed */
2942 if (txmsg->reply.reply_type == 1) {
2943 DRM_ERROR("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
2944 mstb, port->port_num, offset, size);
2949 if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
2954 ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,
2956 memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret);
2961 drm_dp_mst_topology_put_mstb(mstb);
2966 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2967 struct drm_dp_mst_port *port,
2968 int offset, int size, u8 *bytes)
2972 struct drm_dp_sideband_msg_tx *txmsg;
2973 struct drm_dp_mst_branch *mstb;
2975 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2979 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2985 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2988 drm_dp_queue_down_tx(mgr, txmsg);
2990 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2992 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2999 drm_dp_mst_topology_put_mstb(mstb);
3003 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
3005 struct drm_dp_sideband_msg_reply_body reply;
3007 reply.reply_type = DP_SIDEBAND_REPLY_ACK;
3008 reply.req_type = req_type;
3009 drm_dp_encode_sideband_reply(&reply, msg);
3013 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
3014 struct drm_dp_mst_branch *mstb,
3015 int req_type, int seqno, bool broadcast)
3017 struct drm_dp_sideband_msg_tx *txmsg;
3019 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3024 txmsg->seqno = seqno;
3025 drm_dp_encode_up_ack_reply(txmsg, req_type);
3027 mutex_lock(&mgr->qlock);
3029 process_single_up_tx_qlock(mgr, txmsg);
3031 mutex_unlock(&mgr->qlock);
3037 static int drm_dp_get_vc_payload_bw(u8 dp_link_bw, u8 dp_link_count)
3039 if (dp_link_bw == 0 || dp_link_count == 0)
3040 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
3041 dp_link_bw, dp_link_count);
3043 return dp_link_bw * dp_link_count / 2;
3047 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
3048 * @mgr: manager to set state for
3049 * @mst_state: true to enable MST on this connector - false to disable.
3051 * This is called by the driver when it detects an MST capable device plugged
3052 * into a DP MST capable port, or when a DP MST capable device is unplugged.
3054 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
3058 struct drm_dp_mst_branch *mstb = NULL;
3060 mutex_lock(&mgr->lock);
3061 if (mst_state == mgr->mst_state)
3064 mgr->mst_state = mst_state;
3065 /* set the device into MST mode */
3067 WARN_ON(mgr->mst_primary);
3070 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
3071 if (ret != DP_RECEIVER_CAP_SIZE) {
3072 DRM_DEBUG_KMS("failed to read DPCD\n");
3076 mgr->pbn_div = drm_dp_get_vc_payload_bw(mgr->dpcd[1],
3077 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK);
3078 if (mgr->pbn_div == 0) {
3083 /* add initial branch device at LCT 1 */
3084 mstb = drm_dp_add_mst_branch_device(1, NULL);
3091 /* give this the main reference */
3092 mgr->mst_primary = mstb;
3093 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
3095 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3096 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
3102 struct drm_dp_payload reset_pay;
3103 reset_pay.start_slot = 0;
3104 reset_pay.num_slots = 0x3f;
3105 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
3108 queue_work(system_long_wq, &mgr->work);
3112 /* disable MST on the device */
3113 mstb = mgr->mst_primary;
3114 mgr->mst_primary = NULL;
3115 /* this can fail if the device is gone */
3116 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
3118 mutex_lock(&mgr->payload_lock);
3119 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
3120 mgr->payload_mask = 0;
3121 set_bit(0, &mgr->payload_mask);
3122 for (i = 0; i < mgr->max_payloads; i++) {
3123 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
3127 vcpi->num_slots = 0;
3129 mgr->proposed_vcpis[i] = NULL;
3132 mutex_unlock(&mgr->payload_lock);
3134 mgr->payload_id_table_cleared = false;
3138 mutex_unlock(&mgr->lock);
3140 drm_dp_mst_topology_put_mstb(mstb);
3144 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
3147 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
3148 * @mgr: manager to suspend
3150 * This function tells the MST device that we can't handle UP messages
3151 * anymore. This should stop it from sending any since we are suspended.
3153 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
3155 mutex_lock(&mgr->lock);
3156 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3157 DP_MST_EN | DP_UPSTREAM_IS_SRC);
3158 mutex_unlock(&mgr->lock);
3159 flush_work(&mgr->work);
3160 flush_work(&mgr->destroy_connector_work);
3162 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
3165 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
3166 * @mgr: manager to resume
3168 * This will fetch DPCD and see if the device is still there,
3169 * if it is, it will rewrite the MSTM control bits, and return.
3171 * if the device fails this returns -1, and the driver should do
3172 * a full MST reprobe, in case we were undocked.
3174 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
3178 mutex_lock(&mgr->lock);
3180 if (mgr->mst_primary) {
3184 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
3185 if (sret != DP_RECEIVER_CAP_SIZE) {
3186 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3191 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3192 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
3194 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
3199 /* Some hubs forget their guids after they resume */
3200 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
3202 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3206 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
3213 mutex_unlock(&mgr->lock);
3216 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
3218 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
3222 int replylen, origlen, curreply;
3224 struct drm_dp_sideband_msg_rx *msg;
3225 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
3226 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
3228 len = min(mgr->max_dpcd_transaction_bytes, 16);
3229 ret = drm_dp_dpcd_read(mgr->aux, basereg,
3232 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
3235 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
3237 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
3240 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
3245 while (replylen > 0) {
3246 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
3247 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
3250 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
3255 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
3257 DRM_DEBUG_KMS("failed to build sideband msg\n");
3267 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
3269 struct drm_dp_sideband_msg_tx *txmsg;
3270 struct drm_dp_mst_branch *mstb;
3271 struct drm_dp_sideband_msg_hdr *hdr = &mgr->down_rep_recv.initial_hdr;
3274 if (!drm_dp_get_one_sb_msg(mgr, false))
3275 goto clear_down_rep_recv;
3277 if (!mgr->down_rep_recv.have_eomt)
3280 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
3282 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3284 goto clear_down_rep_recv;
3287 /* find the message */
3289 mutex_lock(&mgr->qlock);
3290 txmsg = mstb->tx_slots[slot];
3291 /* remove from slots */
3292 mutex_unlock(&mgr->qlock);
3295 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
3296 mstb, hdr->seqno, hdr->lct, hdr->rad[0],
3297 mgr->down_rep_recv.msg[0]);
3301 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
3303 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3304 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
3305 txmsg->reply.req_type,
3306 drm_dp_mst_req_type_str(txmsg->reply.req_type),
3307 txmsg->reply.u.nak.reason,
3308 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
3309 txmsg->reply.u.nak.nak_data);
3311 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3312 drm_dp_mst_topology_put_mstb(mstb);
3314 mutex_lock(&mgr->qlock);
3315 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
3316 mstb->tx_slots[slot] = NULL;
3317 mutex_unlock(&mgr->qlock);
3319 wake_up_all(&mgr->tx_waitq);
3324 drm_dp_mst_topology_put_mstb(mstb);
3325 clear_down_rep_recv:
3326 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3331 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
3333 struct drm_dp_sideband_msg_req_body msg;
3334 struct drm_dp_sideband_msg_hdr *hdr = &mgr->up_req_recv.initial_hdr;
3335 struct drm_dp_mst_branch *mstb = NULL;
3339 if (!drm_dp_get_one_sb_msg(mgr, true))
3342 if (!mgr->up_req_recv.have_eomt)
3345 if (!hdr->broadcast) {
3346 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
3348 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3355 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
3357 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY)
3358 guid = msg.u.conn_stat.guid;
3359 else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY)
3360 guid = msg.u.resource_stat.guid;
3364 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno,
3368 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid);
3370 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3376 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
3377 drm_dp_mst_handle_conn_stat(mstb, &msg.u.conn_stat);
3379 DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",
3380 msg.u.conn_stat.port_number,
3381 msg.u.conn_stat.legacy_device_plug_status,
3382 msg.u.conn_stat.displayport_device_plug_status,
3383 msg.u.conn_stat.message_capability_status,
3384 msg.u.conn_stat.input_port,
3385 msg.u.conn_stat.peer_device_type);
3387 drm_kms_helper_hotplug_event(mgr->dev);
3388 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
3389 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n",
3390 msg.u.resource_stat.port_number,
3391 msg.u.resource_stat.available_pbn);
3394 drm_dp_mst_topology_put_mstb(mstb);
3396 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3401 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
3402 * @mgr: manager to notify irq for.
3403 * @esi: 4 bytes from SINK_COUNT_ESI
3404 * @handled: whether the hpd interrupt was consumed or not
3406 * This should be called from the driver when it detects a short IRQ,
3407 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
3408 * topology manager will process the sideband messages received as a result
3411 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
3418 if (sc != mgr->sink_count) {
3419 mgr->sink_count = sc;
3423 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
3424 ret = drm_dp_mst_handle_down_rep(mgr);
3428 if (esi[1] & DP_UP_REQ_MSG_RDY) {
3429 ret |= drm_dp_mst_handle_up_req(mgr);
3433 drm_dp_mst_kick_tx(mgr);
3436 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
3439 * drm_dp_mst_detect_port() - get connection status for an MST port
3440 * @connector: DRM connector for this port
3441 * @mgr: manager for this port
3442 * @port: unverified pointer to a port
3444 * This returns the current connection state for a port. It validates the
3445 * port pointer still exists so the caller doesn't require a reference
3447 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
3448 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3450 enum drm_connector_status status = connector_status_disconnected;
3452 /* we need to search for the port in the mgr in case it's gone */
3453 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3455 return connector_status_disconnected;
3460 switch (port->pdt) {
3461 case DP_PEER_DEVICE_NONE:
3462 case DP_PEER_DEVICE_MST_BRANCHING:
3465 case DP_PEER_DEVICE_SST_SINK:
3466 status = connector_status_connected;
3467 /* for logical ports - cache the EDID */
3468 if (port->port_num >= 8 && !port->cached_edid) {
3469 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
3472 case DP_PEER_DEVICE_DP_LEGACY_CONV:
3474 status = connector_status_connected;
3478 drm_dp_mst_topology_put_port(port);
3481 EXPORT_SYMBOL(drm_dp_mst_detect_port);
3484 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
3485 * @mgr: manager for this port
3486 * @port: unverified pointer to a port.
3488 * This returns whether the port supports audio or not.
3490 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
3491 struct drm_dp_mst_port *port)
3495 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3498 ret = port->has_audio;
3499 drm_dp_mst_topology_put_port(port);
3502 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
3505 * drm_dp_mst_get_edid() - get EDID for an MST port
3506 * @connector: toplevel connector to get EDID for
3507 * @mgr: manager for this port
3508 * @port: unverified pointer to a port.
3510 * This returns an EDID for the port connected to a connector,
3511 * It validates the pointer still exists so the caller doesn't require a
3514 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3516 struct edid *edid = NULL;
3518 /* we need to search for the port in the mgr in case it's gone */
3519 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3523 if (port->cached_edid)
3524 edid = drm_edid_duplicate(port->cached_edid);
3526 edid = drm_get_edid(connector, &port->aux.ddc);
3528 port->has_audio = drm_detect_monitor_audio(edid);
3529 drm_dp_mst_topology_put_port(port);
3532 EXPORT_SYMBOL(drm_dp_mst_get_edid);
3535 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
3536 * @mgr: manager to use
3537 * @pbn: payload bandwidth to convert into slots.
3539 * Calculate the number of VCPI slots that will be required for the given PBN
3540 * value. This function is deprecated, and should not be used in atomic
3544 * The total slots required for this port, or error.
3546 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
3551 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3553 /* max. time slots - one slot for MTP header */
3558 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
3560 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3561 struct drm_dp_vcpi *vcpi, int pbn, int slots)
3565 /* max. time slots - one slot for MTP header */
3570 vcpi->aligned_pbn = slots * mgr->pbn_div;
3571 vcpi->num_slots = slots;
3573 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
3580 * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
3581 * @state: global atomic state
3582 * @mgr: MST topology manager for the port
3583 * @port: port to find vcpi slots for
3584 * @pbn: bandwidth required for the mode in PBN
3586 * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
3587 * may have had. Any atomic drivers which support MST must call this function
3588 * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
3589 * current VCPI allocation for the new state, but only when
3590 * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
3591 * to ensure compatibility with userspace applications that still use the
3592 * legacy modesetting UAPI.
3594 * Allocations set by this function are not checked against the bandwidth
3595 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
3597 * Additionally, it is OK to call this function multiple times on the same
3598 * @port as needed. It is not OK however, to call this function and
3599 * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
3602 * drm_dp_atomic_release_vcpi_slots()
3603 * drm_dp_mst_atomic_check()
3606 * Total slots in the atomic state assigned for this port, or a negative error
3607 * code if the port no longer exists
3609 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
3610 struct drm_dp_mst_topology_mgr *mgr,
3611 struct drm_dp_mst_port *port, int pbn)
3613 struct drm_dp_mst_topology_state *topology_state;
3614 struct drm_dp_vcpi_allocation *pos, *vcpi = NULL;
3615 int prev_slots, req_slots;
3617 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3618 if (IS_ERR(topology_state))
3619 return PTR_ERR(topology_state);
3621 /* Find the current allocation for this port, if any */
3622 list_for_each_entry(pos, &topology_state->vcpis, next) {
3623 if (pos->port == port) {
3625 prev_slots = vcpi->vcpi;
3628 * This should never happen, unless the driver tries
3629 * releasing and allocating the same VCPI allocation,
3632 if (WARN_ON(!prev_slots)) {
3633 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
3644 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3646 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
3647 port->connector->base.id, port->connector->name,
3648 port, prev_slots, req_slots);
3650 /* Add the new allocation to the state */
3652 vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL);
3656 drm_dp_mst_get_port_malloc(port);
3658 list_add(&vcpi->next, &topology_state->vcpis);
3660 vcpi->vcpi = req_slots;
3664 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
3667 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
3668 * @state: global atomic state
3669 * @mgr: MST topology manager for the port
3670 * @port: The port to release the VCPI slots from
3672 * Releases any VCPI slots that have been allocated to a port in the atomic
3673 * state. Any atomic drivers which support MST must call this function in
3674 * their &drm_connector_helper_funcs.atomic_check() callback when the
3675 * connector will no longer have VCPI allocated (e.g. because its CRTC was
3676 * removed) when it had VCPI allocated in the previous atomic state.
3678 * It is OK to call this even if @port has been removed from the system.
3679 * Additionally, it is OK to call this function multiple times on the same
3680 * @port as needed. It is not OK however, to call this function and
3681 * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
3685 * drm_dp_atomic_find_vcpi_slots()
3686 * drm_dp_mst_atomic_check()
3689 * 0 if all slots for this port were added back to
3690 * &drm_dp_mst_topology_state.avail_slots or negative error code
3692 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
3693 struct drm_dp_mst_topology_mgr *mgr,
3694 struct drm_dp_mst_port *port)
3696 struct drm_dp_mst_topology_state *topology_state;
3697 struct drm_dp_vcpi_allocation *pos;
3700 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3701 if (IS_ERR(topology_state))
3702 return PTR_ERR(topology_state);
3704 list_for_each_entry(pos, &topology_state->vcpis, next) {
3705 if (pos->port == port) {
3710 if (WARN_ON(!found)) {
3711 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
3712 port, &topology_state->base);
3716 DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi);
3718 drm_dp_mst_put_port_malloc(port);
3724 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
3727 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
3728 * @mgr: manager for this port
3729 * @port: port to allocate a virtual channel for.
3730 * @pbn: payload bandwidth number to request
3731 * @slots: returned number of slots for this PBN.
3733 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3734 struct drm_dp_mst_port *port, int pbn, int slots)
3738 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3745 if (port->vcpi.vcpi > 0) {
3746 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
3747 port->vcpi.vcpi, port->vcpi.pbn, pbn);
3748 if (pbn == port->vcpi.pbn) {
3749 drm_dp_mst_topology_put_port(port);
3754 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
3756 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
3757 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
3760 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
3761 pbn, port->vcpi.num_slots);
3763 /* Keep port allocated until its payload has been removed */
3764 drm_dp_mst_get_port_malloc(port);
3765 drm_dp_mst_topology_put_port(port);
3770 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
3772 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3775 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3779 slots = port->vcpi.num_slots;
3780 drm_dp_mst_topology_put_port(port);
3783 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
3786 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
3787 * @mgr: manager for this port
3788 * @port: unverified pointer to a port.
3790 * This just resets the number of slots for the ports VCPI for later programming.
3792 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3795 * A port with VCPI will remain allocated until its VCPI is
3796 * released, no verified ref needed
3799 port->vcpi.num_slots = 0;
3801 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
3804 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
3805 * @mgr: manager for this port
3806 * @port: port to deallocate vcpi for
3808 * This can be called unconditionally, regardless of whether
3809 * drm_dp_mst_allocate_vcpi() succeeded or not.
3811 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3812 struct drm_dp_mst_port *port)
3814 if (!port->vcpi.vcpi)
3817 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3818 port->vcpi.num_slots = 0;
3820 port->vcpi.aligned_pbn = 0;
3821 port->vcpi.vcpi = 0;
3822 drm_dp_mst_put_port_malloc(port);
3824 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
3826 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
3827 int id, struct drm_dp_payload *payload)
3829 u8 payload_alloc[3], status;
3833 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
3834 DP_PAYLOAD_TABLE_UPDATED);
3836 payload_alloc[0] = id;
3837 payload_alloc[1] = payload->start_slot;
3838 payload_alloc[2] = payload->num_slots;
3840 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
3842 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
3847 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3849 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3853 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
3856 usleep_range(10000, 20000);
3859 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
3870 * drm_dp_check_act_status() - Check ACT handled status.
3871 * @mgr: manager to use
3873 * Check the payload status bits in the DPCD for ACT handled completion.
3875 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
3882 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3885 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3889 if (status & DP_PAYLOAD_ACT_HANDLED)
3894 } while (count < 30);
3896 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
3897 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
3905 EXPORT_SYMBOL(drm_dp_check_act_status);
3908 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
3909 * @clock: dot clock for the mode
3910 * @bpp: bpp for the mode.
3912 * This uses the formula in the spec to calculate the PBN value for a mode.
3914 int drm_dp_calc_pbn_mode(int clock, int bpp)
3917 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
3918 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
3919 * common multiplier to render an integer PBN for all link rate/lane
3920 * counts combinations
3922 * peak_kbps *= (1006/1000)
3923 * peak_kbps *= (64/54)
3924 * peak_kbps *= 8 convert to bytes
3926 return DIV_ROUND_UP_ULL(mul_u32_u32(clock * bpp, 64 * 1006),
3927 8 * 54 * 1000 * 1000);
3929 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
3931 /* we want to kick the TX after we've ack the up/down IRQs. */
3932 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
3934 queue_work(system_long_wq, &mgr->tx_work);
3937 static void drm_dp_mst_dump_mstb(struct seq_file *m,
3938 struct drm_dp_mst_branch *mstb)
3940 struct drm_dp_mst_port *port;
3941 int tabs = mstb->lct;
3945 for (i = 0; i < tabs; i++)
3949 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
3950 list_for_each_entry(port, &mstb->ports, next) {
3951 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);
3953 drm_dp_mst_dump_mstb(m, port->mstb);
3957 #define DP_PAYLOAD_TABLE_SIZE 64
3959 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
3964 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
3965 if (drm_dp_dpcd_read(mgr->aux,
3966 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
3973 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3974 struct drm_dp_mst_port *port, char *name,
3977 struct edid *mst_edid;
3979 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3980 drm_edid_get_monitor_name(mst_edid, name, namelen);
3984 * drm_dp_mst_dump_topology(): dump topology to seq file.
3985 * @m: seq_file to dump output to
3986 * @mgr: manager to dump current topology for.
3988 * helper to dump MST topology to a seq file for debugfs.
3990 void drm_dp_mst_dump_topology(struct seq_file *m,
3991 struct drm_dp_mst_topology_mgr *mgr)
3994 struct drm_dp_mst_port *port;
3996 mutex_lock(&mgr->lock);
3997 if (mgr->mst_primary)
3998 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
4001 mutex_unlock(&mgr->lock);
4003 mutex_lock(&mgr->payload_lock);
4004 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
4007 for (i = 0; i < mgr->max_payloads; i++) {
4008 if (mgr->proposed_vcpis[i]) {
4011 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
4012 fetch_monitor_name(mgr, port, name, sizeof(name));
4013 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
4014 port->port_num, port->vcpi.vcpi,
4015 port->vcpi.num_slots,
4016 (*name != 0) ? name : "Unknown");
4018 seq_printf(m, "vcpi %d:unused\n", i);
4020 for (i = 0; i < mgr->max_payloads; i++) {
4021 seq_printf(m, "payload %d: %d, %d, %d\n",
4023 mgr->payloads[i].payload_state,
4024 mgr->payloads[i].start_slot,
4025 mgr->payloads[i].num_slots);
4029 mutex_unlock(&mgr->payload_lock);
4031 mutex_lock(&mgr->lock);
4032 if (mgr->mst_primary) {
4033 u8 buf[DP_PAYLOAD_TABLE_SIZE];
4036 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
4037 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
4038 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
4039 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
4040 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
4041 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
4043 /* dump the standard OUI branch header */
4044 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
4045 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
4046 for (i = 0x3; i < 0x8 && buf[i]; i++)
4047 seq_printf(m, "%c", buf[i]);
4048 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
4049 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
4050 if (dump_dp_payload_table(mgr, buf))
4051 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
4054 mutex_unlock(&mgr->lock);
4057 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
4059 static void drm_dp_tx_work(struct work_struct *work)
4061 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
4063 mutex_lock(&mgr->qlock);
4064 if (!list_empty(&mgr->tx_msg_downq))
4065 process_single_down_tx_qlock(mgr);
4066 mutex_unlock(&mgr->qlock);
4069 static void drm_dp_destroy_connector_work(struct work_struct *work)
4071 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
4072 struct drm_dp_mst_port *port;
4073 bool send_hotplug = false;
4075 * Not a regular list traverse as we have to drop the destroy
4076 * connector lock before destroying the connector, to avoid AB->BA
4077 * ordering between this lock and the config mutex.
4080 mutex_lock(&mgr->destroy_connector_lock);
4081 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
4083 mutex_unlock(&mgr->destroy_connector_lock);
4086 list_del(&port->next);
4087 mutex_unlock(&mgr->destroy_connector_lock);
4089 mgr->cbs->destroy_connector(mgr, port->connector);
4091 drm_dp_port_teardown_pdt(port, port->pdt);
4092 port->pdt = DP_PEER_DEVICE_NONE;
4094 drm_dp_mst_put_port_malloc(port);
4095 send_hotplug = true;
4098 drm_kms_helper_hotplug_event(mgr->dev);
4101 static struct drm_private_state *
4102 drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
4104 struct drm_dp_mst_topology_state *state, *old_state =
4105 to_dp_mst_topology_state(obj->state);
4106 struct drm_dp_vcpi_allocation *pos, *vcpi;
4108 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
4112 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
4114 INIT_LIST_HEAD(&state->vcpis);
4116 list_for_each_entry(pos, &old_state->vcpis, next) {
4117 /* Prune leftover freed VCPI allocations */
4121 vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL);
4125 drm_dp_mst_get_port_malloc(vcpi->port);
4126 list_add(&vcpi->next, &state->vcpis);
4129 return &state->base;
4132 list_for_each_entry_safe(pos, vcpi, &state->vcpis, next) {
4133 drm_dp_mst_put_port_malloc(pos->port);
4141 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
4142 struct drm_private_state *state)
4144 struct drm_dp_mst_topology_state *mst_state =
4145 to_dp_mst_topology_state(state);
4146 struct drm_dp_vcpi_allocation *pos, *tmp;
4148 list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next) {
4149 /* We only keep references to ports with non-zero VCPIs */
4151 drm_dp_mst_put_port_malloc(pos->port);
4159 drm_dp_mst_atomic_check_topology_state(struct drm_dp_mst_topology_mgr *mgr,
4160 struct drm_dp_mst_topology_state *mst_state)
4162 struct drm_dp_vcpi_allocation *vcpi;
4163 int avail_slots = 63, payload_count = 0;
4165 list_for_each_entry(vcpi, &mst_state->vcpis, next) {
4166 /* Releasing VCPI is always OK-even if the port is gone */
4168 DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
4173 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
4174 vcpi->port, vcpi->vcpi);
4176 avail_slots -= vcpi->vcpi;
4177 if (avail_slots < 0) {
4178 DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
4179 vcpi->port, mst_state,
4180 avail_slots + vcpi->vcpi);
4184 if (++payload_count > mgr->max_payloads) {
4185 DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
4186 mgr, mst_state, mgr->max_payloads);
4190 DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
4191 mgr, mst_state, avail_slots,
4198 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
4199 * atomic update is valid
4200 * @state: Pointer to the new &struct drm_dp_mst_topology_state
4202 * Checks the given topology state for an atomic update to ensure that it's
4203 * valid. This includes checking whether there's enough bandwidth to support
4204 * the new VCPI allocations in the atomic update.
4206 * Any atomic drivers supporting DP MST must make sure to call this after
4207 * checking the rest of their state in their
4208 * &drm_mode_config_funcs.atomic_check() callback.
4211 * drm_dp_atomic_find_vcpi_slots()
4212 * drm_dp_atomic_release_vcpi_slots()
4216 * 0 if the new state is valid, negative error code otherwise.
4218 int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
4220 struct drm_dp_mst_topology_mgr *mgr;
4221 struct drm_dp_mst_topology_state *mst_state;
4224 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
4225 ret = drm_dp_mst_atomic_check_topology_state(mgr, mst_state);
4232 EXPORT_SYMBOL(drm_dp_mst_atomic_check);
4234 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
4235 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
4236 .atomic_destroy_state = drm_dp_mst_destroy_state,
4238 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
4241 * drm_atomic_get_mst_topology_state: get MST topology state
4243 * @state: global atomic state
4244 * @mgr: MST topology manager, also the private object in this case
4246 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
4247 * state vtable so that the private object state returned is that of a MST
4248 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
4249 * to care of the locking, so warn if don't hold the connection_mutex.
4253 * The MST topology state or error pointer.
4255 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
4256 struct drm_dp_mst_topology_mgr *mgr)
4258 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
4260 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
4263 * drm_dp_mst_topology_mgr_init - initialise a topology manager
4264 * @mgr: manager struct to initialise
4265 * @dev: device providing this structure - for i2c addition.
4266 * @aux: DP helper aux channel to talk to this device
4267 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
4268 * @max_payloads: maximum number of payloads this GPU can source
4269 * @conn_base_id: the connector object ID the MST device is connected to.
4271 * Return 0 for success, or negative error code on failure
4273 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
4274 struct drm_device *dev, struct drm_dp_aux *aux,
4275 int max_dpcd_transaction_bytes,
4276 int max_payloads, int conn_base_id)
4278 struct drm_dp_mst_topology_state *mst_state;
4280 mutex_init(&mgr->lock);
4281 mutex_init(&mgr->qlock);
4282 mutex_init(&mgr->payload_lock);
4283 mutex_init(&mgr->destroy_connector_lock);
4284 INIT_LIST_HEAD(&mgr->tx_msg_downq);
4285 INIT_LIST_HEAD(&mgr->destroy_connector_list);
4286 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
4287 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
4288 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
4289 init_waitqueue_head(&mgr->tx_waitq);
4292 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
4293 mgr->max_payloads = max_payloads;
4294 mgr->conn_base_id = conn_base_id;
4295 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
4296 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
4298 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
4301 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
4302 if (!mgr->proposed_vcpis)
4304 set_bit(0, &mgr->payload_mask);
4306 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
4307 if (mst_state == NULL)
4310 mst_state->mgr = mgr;
4311 INIT_LIST_HEAD(&mst_state->vcpis);
4313 drm_atomic_private_obj_init(dev, &mgr->base,
4315 &drm_dp_mst_topology_state_funcs);
4319 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
4322 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
4323 * @mgr: manager to destroy
4325 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
4327 drm_dp_mst_topology_mgr_set_mst(mgr, false);
4328 flush_work(&mgr->work);
4329 flush_work(&mgr->destroy_connector_work);
4330 mutex_lock(&mgr->payload_lock);
4331 kfree(mgr->payloads);
4332 mgr->payloads = NULL;
4333 kfree(mgr->proposed_vcpis);
4334 mgr->proposed_vcpis = NULL;
4335 mutex_unlock(&mgr->payload_lock);
4338 drm_atomic_private_obj_fini(&mgr->base);
4341 mutex_destroy(&mgr->destroy_connector_lock);
4342 mutex_destroy(&mgr->payload_lock);
4343 mutex_destroy(&mgr->qlock);
4344 mutex_destroy(&mgr->lock);
4346 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
4348 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
4352 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
4355 for (i = 0; i < num - 1; i++) {
4356 if (msgs[i].flags & I2C_M_RD ||
4361 return msgs[num - 1].flags & I2C_M_RD &&
4362 msgs[num - 1].len <= 0xff;
4366 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
4369 struct drm_dp_aux *aux = adapter->algo_data;
4370 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
4371 struct drm_dp_mst_branch *mstb;
4372 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
4374 struct drm_dp_sideband_msg_req_body msg;
4375 struct drm_dp_sideband_msg_tx *txmsg = NULL;
4378 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
4382 if (!remote_i2c_read_ok(msgs, num)) {
4383 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
4388 memset(&msg, 0, sizeof(msg));
4389 msg.req_type = DP_REMOTE_I2C_READ;
4390 msg.u.i2c_read.num_transactions = num - 1;
4391 msg.u.i2c_read.port_number = port->port_num;
4392 for (i = 0; i < num - 1; i++) {
4393 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
4394 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
4395 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
4396 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
4398 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
4399 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
4401 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
4408 drm_dp_encode_sideband_req(&msg, txmsg);
4410 drm_dp_queue_down_tx(mgr, txmsg);
4412 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
4415 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
4419 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
4423 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
4428 drm_dp_mst_topology_put_mstb(mstb);
4432 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
4434 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
4435 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
4436 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
4437 I2C_FUNC_10BIT_ADDR;
4440 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
4441 .functionality = drm_dp_mst_i2c_functionality,
4442 .master_xfer = drm_dp_mst_i2c_xfer,
4446 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
4447 * @aux: DisplayPort AUX channel
4449 * Returns 0 on success or a negative error code on failure.
4451 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
4453 aux->ddc.algo = &drm_dp_mst_i2c_algo;
4454 aux->ddc.algo_data = aux;
4455 aux->ddc.retries = 3;
4457 aux->ddc.class = I2C_CLASS_DDC;
4458 aux->ddc.owner = THIS_MODULE;
4459 aux->ddc.dev.parent = aux->dev;
4460 aux->ddc.dev.of_node = aux->dev->of_node;
4462 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
4463 sizeof(aux->ddc.name));
4465 return i2c_add_adapter(&aux->ddc);
4469 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
4470 * @aux: DisplayPort AUX channel
4472 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
4474 i2c_del_adapter(&aux->ddc);