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_fixed.h>
36 #include <drm/drm_print.h>
37 #include <drm/drm_probe_helper.h>
39 #include "drm_crtc_helper_internal.h"
40 #include "drm_dp_mst_topology_internal.h"
45 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
46 * protocol. The helpers contain a topology manager and bandwidth manager.
47 * The helpers encapsulate the sending and received of sideband msgs.
49 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
52 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
54 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
56 struct drm_dp_payload *payload);
58 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
59 struct drm_dp_mst_port *port,
60 int offset, int size, u8 *bytes);
61 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
62 struct drm_dp_mst_port *port,
63 int offset, int size, u8 *bytes);
65 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
66 struct drm_dp_mst_branch *mstb);
67 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
68 struct drm_dp_mst_branch *mstb,
69 struct drm_dp_mst_port *port);
70 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
73 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
74 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
75 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
77 #define DBG_PREFIX "[dp_mst]"
79 #define DP_STR(x) [DP_ ## x] = #x
81 static const char *drm_dp_mst_req_type_str(u8 req_type)
83 static const char * const req_type_str[] = {
84 DP_STR(GET_MSG_TRANSACTION_VERSION),
86 DP_STR(CONNECTION_STATUS_NOTIFY),
87 DP_STR(ENUM_PATH_RESOURCES),
88 DP_STR(ALLOCATE_PAYLOAD),
89 DP_STR(QUERY_PAYLOAD),
90 DP_STR(RESOURCE_STATUS_NOTIFY),
91 DP_STR(CLEAR_PAYLOAD_ID_TABLE),
92 DP_STR(REMOTE_DPCD_READ),
93 DP_STR(REMOTE_DPCD_WRITE),
94 DP_STR(REMOTE_I2C_READ),
95 DP_STR(REMOTE_I2C_WRITE),
97 DP_STR(POWER_DOWN_PHY),
98 DP_STR(SINK_EVENT_NOTIFY),
99 DP_STR(QUERY_STREAM_ENC_STATUS),
102 if (req_type >= ARRAY_SIZE(req_type_str) ||
103 !req_type_str[req_type])
106 return req_type_str[req_type];
110 #define DP_STR(x) [DP_NAK_ ## x] = #x
112 static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
114 static const char * const nak_reason_str[] = {
115 DP_STR(WRITE_FAILURE),
116 DP_STR(INVALID_READ),
120 DP_STR(LINK_FAILURE),
121 DP_STR(NO_RESOURCES),
124 DP_STR(ALLOCATE_FAIL),
127 if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
128 !nak_reason_str[nak_reason])
131 return nak_reason_str[nak_reason];
135 #define DP_STR(x) [DRM_DP_SIDEBAND_TX_ ## x] = #x
137 static const char *drm_dp_mst_sideband_tx_state_str(int state)
139 static const char * const sideband_reason_str[] = {
147 if (state >= ARRAY_SIZE(sideband_reason_str) ||
148 !sideband_reason_str[state])
151 return sideband_reason_str[state];
155 drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len)
160 for (i = 0; i < lct; i++) {
162 unpacked_rad[i] = rad[i / 2] >> 4;
164 unpacked_rad[i] = rad[i / 2] & BIT_MASK(4);
167 /* TODO: Eventually add something to printk so we can format the rad
170 return snprintf(out, len, "%*phC", lct, unpacked_rad);
173 /* sideband msg handling */
174 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
179 int number_of_bits = num_nibbles * 4;
182 while (number_of_bits != 0) {
185 remainder |= (data[array_index] & bitmask) >> bitshift;
193 if ((remainder & 0x10) == 0x10)
198 while (number_of_bits != 0) {
201 if ((remainder & 0x10) != 0)
208 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
213 int number_of_bits = number_of_bytes * 8;
216 while (number_of_bits != 0) {
219 remainder |= (data[array_index] & bitmask) >> bitshift;
227 if ((remainder & 0x100) == 0x100)
232 while (number_of_bits != 0) {
235 if ((remainder & 0x100) != 0)
239 return remainder & 0xff;
241 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
244 size += (hdr->lct / 2);
248 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
254 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
255 for (i = 0; i < (hdr->lct / 2); i++)
256 buf[idx++] = hdr->rad[i];
257 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
258 (hdr->msg_len & 0x3f);
259 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
261 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
262 buf[idx - 1] |= (crc4 & 0xf);
267 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
268 u8 *buf, int buflen, u8 *hdrlen)
277 len += ((buf[0] & 0xf0) >> 4) / 2;
280 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
282 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
283 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
287 hdr->lct = (buf[0] & 0xf0) >> 4;
288 hdr->lcr = (buf[0] & 0xf);
290 for (i = 0; i < (hdr->lct / 2); i++)
291 hdr->rad[i] = buf[idx++];
292 hdr->broadcast = (buf[idx] >> 7) & 0x1;
293 hdr->path_msg = (buf[idx] >> 6) & 0x1;
294 hdr->msg_len = buf[idx] & 0x3f;
296 hdr->somt = (buf[idx] >> 7) & 0x1;
297 hdr->eomt = (buf[idx] >> 6) & 0x1;
298 hdr->seqno = (buf[idx] >> 4) & 0x1;
305 drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body *req,
306 struct drm_dp_sideband_msg_tx *raw)
311 buf[idx++] = req->req_type & 0x7f;
313 switch (req->req_type) {
314 case DP_ENUM_PATH_RESOURCES:
315 case DP_POWER_DOWN_PHY:
316 case DP_POWER_UP_PHY:
317 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
320 case DP_ALLOCATE_PAYLOAD:
321 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
322 (req->u.allocate_payload.number_sdp_streams & 0xf);
324 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
326 buf[idx] = (req->u.allocate_payload.pbn >> 8);
328 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
330 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
331 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
332 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
335 if (req->u.allocate_payload.number_sdp_streams & 1) {
336 i = req->u.allocate_payload.number_sdp_streams - 1;
337 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
341 case DP_QUERY_PAYLOAD:
342 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
344 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
347 case DP_REMOTE_DPCD_READ:
348 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
349 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
351 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
353 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
355 buf[idx] = (req->u.dpcd_read.num_bytes);
359 case DP_REMOTE_DPCD_WRITE:
360 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
361 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
363 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
365 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
367 buf[idx] = (req->u.dpcd_write.num_bytes);
369 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
370 idx += req->u.dpcd_write.num_bytes;
372 case DP_REMOTE_I2C_READ:
373 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
374 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
376 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
377 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
379 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
381 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
382 idx += req->u.i2c_read.transactions[i].num_bytes;
384 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
385 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
388 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
390 buf[idx] = (req->u.i2c_read.num_bytes_read);
394 case DP_REMOTE_I2C_WRITE:
395 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
397 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
399 buf[idx] = (req->u.i2c_write.num_bytes);
401 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
402 idx += req->u.i2c_write.num_bytes;
407 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req);
409 /* Decode a sideband request we've encoded, mainly used for debugging */
411 drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx *raw,
412 struct drm_dp_sideband_msg_req_body *req)
414 const u8 *buf = raw->msg;
417 req->req_type = buf[idx++] & 0x7f;
418 switch (req->req_type) {
419 case DP_ENUM_PATH_RESOURCES:
420 case DP_POWER_DOWN_PHY:
421 case DP_POWER_UP_PHY:
422 req->u.port_num.port_number = (buf[idx] >> 4) & 0xf;
424 case DP_ALLOCATE_PAYLOAD:
426 struct drm_dp_allocate_payload *a =
427 &req->u.allocate_payload;
429 a->number_sdp_streams = buf[idx] & 0xf;
430 a->port_number = (buf[idx] >> 4) & 0xf;
432 WARN_ON(buf[++idx] & 0x80);
433 a->vcpi = buf[idx] & 0x7f;
435 a->pbn = buf[++idx] << 8;
436 a->pbn |= buf[++idx];
439 for (i = 0; i < a->number_sdp_streams; i++) {
440 a->sdp_stream_sink[i] =
441 (buf[idx + (i / 2)] >> ((i % 2) ? 0 : 4)) & 0xf;
445 case DP_QUERY_PAYLOAD:
446 req->u.query_payload.port_number = (buf[idx] >> 4) & 0xf;
447 WARN_ON(buf[++idx] & 0x80);
448 req->u.query_payload.vcpi = buf[idx] & 0x7f;
450 case DP_REMOTE_DPCD_READ:
452 struct drm_dp_remote_dpcd_read *r = &req->u.dpcd_read;
454 r->port_number = (buf[idx] >> 4) & 0xf;
456 r->dpcd_address = (buf[idx] << 16) & 0xf0000;
457 r->dpcd_address |= (buf[++idx] << 8) & 0xff00;
458 r->dpcd_address |= buf[++idx] & 0xff;
460 r->num_bytes = buf[++idx];
463 case DP_REMOTE_DPCD_WRITE:
465 struct drm_dp_remote_dpcd_write *w =
468 w->port_number = (buf[idx] >> 4) & 0xf;
470 w->dpcd_address = (buf[idx] << 16) & 0xf0000;
471 w->dpcd_address |= (buf[++idx] << 8) & 0xff00;
472 w->dpcd_address |= buf[++idx] & 0xff;
474 w->num_bytes = buf[++idx];
476 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
482 case DP_REMOTE_I2C_READ:
484 struct drm_dp_remote_i2c_read *r = &req->u.i2c_read;
485 struct drm_dp_remote_i2c_read_tx *tx;
488 r->num_transactions = buf[idx] & 0x3;
489 r->port_number = (buf[idx] >> 4) & 0xf;
490 for (i = 0; i < r->num_transactions; i++) {
491 tx = &r->transactions[i];
493 tx->i2c_dev_id = buf[++idx] & 0x7f;
494 tx->num_bytes = buf[++idx];
495 tx->bytes = kmemdup(&buf[++idx],
502 idx += tx->num_bytes;
503 tx->no_stop_bit = (buf[idx] >> 5) & 0x1;
504 tx->i2c_transaction_delay = buf[idx] & 0xf;
508 for (i = 0; i < r->num_transactions; i++)
513 r->read_i2c_device_id = buf[++idx] & 0x7f;
514 r->num_bytes_read = buf[++idx];
517 case DP_REMOTE_I2C_WRITE:
519 struct drm_dp_remote_i2c_write *w = &req->u.i2c_write;
521 w->port_number = (buf[idx] >> 4) & 0xf;
522 w->write_i2c_device_id = buf[++idx] & 0x7f;
523 w->num_bytes = buf[++idx];
524 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
534 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req);
537 drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body *req,
538 int indent, struct drm_printer *printer)
542 #define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)
543 if (req->req_type == DP_LINK_ADDRESS) {
544 /* No contents to print */
545 P("type=%s\n", drm_dp_mst_req_type_str(req->req_type));
549 P("type=%s contents:\n", drm_dp_mst_req_type_str(req->req_type));
552 switch (req->req_type) {
553 case DP_ENUM_PATH_RESOURCES:
554 case DP_POWER_DOWN_PHY:
555 case DP_POWER_UP_PHY:
556 P("port=%d\n", req->u.port_num.port_number);
558 case DP_ALLOCATE_PAYLOAD:
559 P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
560 req->u.allocate_payload.port_number,
561 req->u.allocate_payload.vcpi, req->u.allocate_payload.pbn,
562 req->u.allocate_payload.number_sdp_streams,
563 req->u.allocate_payload.number_sdp_streams,
564 req->u.allocate_payload.sdp_stream_sink);
566 case DP_QUERY_PAYLOAD:
567 P("port=%d vcpi=%d\n",
568 req->u.query_payload.port_number,
569 req->u.query_payload.vcpi);
571 case DP_REMOTE_DPCD_READ:
572 P("port=%d dpcd_addr=%05x len=%d\n",
573 req->u.dpcd_read.port_number, req->u.dpcd_read.dpcd_address,
574 req->u.dpcd_read.num_bytes);
576 case DP_REMOTE_DPCD_WRITE:
577 P("port=%d addr=%05x len=%d: %*ph\n",
578 req->u.dpcd_write.port_number,
579 req->u.dpcd_write.dpcd_address,
580 req->u.dpcd_write.num_bytes, req->u.dpcd_write.num_bytes,
581 req->u.dpcd_write.bytes);
583 case DP_REMOTE_I2C_READ:
584 P("port=%d num_tx=%d id=%d size=%d:\n",
585 req->u.i2c_read.port_number,
586 req->u.i2c_read.num_transactions,
587 req->u.i2c_read.read_i2c_device_id,
588 req->u.i2c_read.num_bytes_read);
591 for (i = 0; i < req->u.i2c_read.num_transactions; i++) {
592 const struct drm_dp_remote_i2c_read_tx *rtx =
593 &req->u.i2c_read.transactions[i];
595 P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
596 i, rtx->i2c_dev_id, rtx->num_bytes,
597 rtx->no_stop_bit, rtx->i2c_transaction_delay,
598 rtx->num_bytes, rtx->bytes);
601 case DP_REMOTE_I2C_WRITE:
602 P("port=%d id=%d size=%d: %*ph\n",
603 req->u.i2c_write.port_number,
604 req->u.i2c_write.write_i2c_device_id,
605 req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
606 req->u.i2c_write.bytes);
614 EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body);
617 drm_dp_mst_dump_sideband_msg_tx(struct drm_printer *p,
618 const struct drm_dp_sideband_msg_tx *txmsg)
620 struct drm_dp_sideband_msg_req_body req;
625 drm_dp_mst_rad_to_str(txmsg->dst->rad, txmsg->dst->lct, buf,
627 drm_printf(p, "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
628 txmsg->cur_offset, txmsg->cur_len, txmsg->seqno,
629 drm_dp_mst_sideband_tx_state_str(txmsg->state),
630 txmsg->path_msg, buf);
632 ret = drm_dp_decode_sideband_req(txmsg, &req);
634 drm_printf(p, "<failed to decode sideband req: %d>\n", ret);
637 drm_dp_dump_sideband_msg_req_body(&req, 1, p);
639 switch (req.req_type) {
640 case DP_REMOTE_DPCD_WRITE:
641 kfree(req.u.dpcd_write.bytes);
643 case DP_REMOTE_I2C_READ:
644 for (i = 0; i < req.u.i2c_read.num_transactions; i++)
645 kfree(req.u.i2c_read.transactions[i].bytes);
647 case DP_REMOTE_I2C_WRITE:
648 kfree(req.u.i2c_write.bytes);
653 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
656 crc4 = drm_dp_msg_data_crc4(msg, len);
660 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
661 struct drm_dp_sideband_msg_tx *raw)
666 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
671 /* this adds a chunk of msg to the builder to get the final msg */
672 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
673 u8 *replybuf, u8 replybuflen, bool hdr)
680 struct drm_dp_sideband_msg_hdr recv_hdr;
681 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
683 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
688 * ignore out-of-order messages or messages that are part of a
691 if (!recv_hdr.somt && !msg->have_somt)
694 /* get length contained in this portion */
695 msg->curchunk_len = recv_hdr.msg_len;
696 msg->curchunk_hdrlen = hdrlen;
698 /* we have already gotten an somt - don't bother parsing */
699 if (recv_hdr.somt && msg->have_somt)
703 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
704 msg->have_somt = true;
707 msg->have_eomt = true;
709 /* copy the bytes for the remainder of this header chunk */
710 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
711 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
713 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
714 msg->curchunk_idx += replybuflen;
717 if (msg->curchunk_idx >= msg->curchunk_len) {
719 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
720 /* copy chunk into bigger msg */
721 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
722 msg->curlen += msg->curchunk_len - 1;
727 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
728 struct drm_dp_sideband_msg_reply_body *repmsg)
732 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
734 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
736 if (idx > raw->curlen)
738 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
739 if (raw->msg[idx] & 0x80)
740 repmsg->u.link_addr.ports[i].input_port = 1;
742 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
743 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
746 if (idx > raw->curlen)
748 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
749 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
750 if (repmsg->u.link_addr.ports[i].input_port == 0)
751 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
753 if (idx > raw->curlen)
755 if (repmsg->u.link_addr.ports[i].input_port == 0) {
756 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
758 if (idx > raw->curlen)
760 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
762 if (idx > raw->curlen)
764 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
765 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
769 if (idx > raw->curlen)
775 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
779 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
780 struct drm_dp_sideband_msg_reply_body *repmsg)
783 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
785 if (idx > raw->curlen)
787 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
789 if (idx > raw->curlen)
792 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
795 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
799 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
800 struct drm_dp_sideband_msg_reply_body *repmsg)
803 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
805 if (idx > raw->curlen)
809 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
813 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
814 struct drm_dp_sideband_msg_reply_body *repmsg)
818 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
820 if (idx > raw->curlen)
822 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
825 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
828 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
832 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
833 struct drm_dp_sideband_msg_reply_body *repmsg)
836 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
838 if (idx > raw->curlen)
840 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
842 if (idx > raw->curlen)
844 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
846 if (idx > raw->curlen)
850 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
854 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
855 struct drm_dp_sideband_msg_reply_body *repmsg)
858 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
860 if (idx > raw->curlen)
862 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
864 if (idx > raw->curlen)
866 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
868 if (idx > raw->curlen)
872 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
876 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
877 struct drm_dp_sideband_msg_reply_body *repmsg)
880 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
882 if (idx > raw->curlen)
884 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
886 if (idx > raw->curlen)
890 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
894 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
895 struct drm_dp_sideband_msg_reply_body *repmsg)
899 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
901 if (idx > raw->curlen) {
902 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
909 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
910 struct drm_dp_sideband_msg_reply_body *msg)
912 memset(msg, 0, sizeof(*msg));
913 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
914 msg->req_type = (raw->msg[0] & 0x7f);
916 if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
917 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
918 msg->u.nak.reason = raw->msg[17];
919 msg->u.nak.nak_data = raw->msg[18];
923 switch (msg->req_type) {
924 case DP_LINK_ADDRESS:
925 return drm_dp_sideband_parse_link_address(raw, msg);
926 case DP_QUERY_PAYLOAD:
927 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
928 case DP_REMOTE_DPCD_READ:
929 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
930 case DP_REMOTE_DPCD_WRITE:
931 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
932 case DP_REMOTE_I2C_READ:
933 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
934 case DP_ENUM_PATH_RESOURCES:
935 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
936 case DP_ALLOCATE_PAYLOAD:
937 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
938 case DP_POWER_DOWN_PHY:
939 case DP_POWER_UP_PHY:
940 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
942 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
943 drm_dp_mst_req_type_str(msg->req_type));
948 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
949 struct drm_dp_sideband_msg_req_body *msg)
953 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
955 if (idx > raw->curlen)
958 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
960 if (idx > raw->curlen)
963 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
964 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
965 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
966 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
967 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
971 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
975 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
976 struct drm_dp_sideband_msg_req_body *msg)
980 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
982 if (idx > raw->curlen)
985 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
987 if (idx > raw->curlen)
990 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
994 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
998 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
999 struct drm_dp_sideband_msg_req_body *msg)
1001 memset(msg, 0, sizeof(*msg));
1002 msg->req_type = (raw->msg[0] & 0x7f);
1004 switch (msg->req_type) {
1005 case DP_CONNECTION_STATUS_NOTIFY:
1006 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
1007 case DP_RESOURCE_STATUS_NOTIFY:
1008 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
1010 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,
1011 drm_dp_mst_req_type_str(msg->req_type));
1016 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
1018 struct drm_dp_sideband_msg_req_body req;
1020 req.req_type = DP_REMOTE_DPCD_WRITE;
1021 req.u.dpcd_write.port_number = port_num;
1022 req.u.dpcd_write.dpcd_address = offset;
1023 req.u.dpcd_write.num_bytes = num_bytes;
1024 req.u.dpcd_write.bytes = bytes;
1025 drm_dp_encode_sideband_req(&req, msg);
1030 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
1032 struct drm_dp_sideband_msg_req_body req;
1034 req.req_type = DP_LINK_ADDRESS;
1035 drm_dp_encode_sideband_req(&req, msg);
1039 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
1041 struct drm_dp_sideband_msg_req_body req;
1043 req.req_type = DP_ENUM_PATH_RESOURCES;
1044 req.u.port_num.port_number = port_num;
1045 drm_dp_encode_sideband_req(&req, msg);
1046 msg->path_msg = true;
1050 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
1051 u8 vcpi, uint16_t pbn,
1052 u8 number_sdp_streams,
1053 u8 *sdp_stream_sink)
1055 struct drm_dp_sideband_msg_req_body req;
1056 memset(&req, 0, sizeof(req));
1057 req.req_type = DP_ALLOCATE_PAYLOAD;
1058 req.u.allocate_payload.port_number = port_num;
1059 req.u.allocate_payload.vcpi = vcpi;
1060 req.u.allocate_payload.pbn = pbn;
1061 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
1062 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
1063 number_sdp_streams);
1064 drm_dp_encode_sideband_req(&req, msg);
1065 msg->path_msg = true;
1069 static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
1070 int port_num, bool power_up)
1072 struct drm_dp_sideband_msg_req_body req;
1075 req.req_type = DP_POWER_UP_PHY;
1077 req.req_type = DP_POWER_DOWN_PHY;
1079 req.u.port_num.port_number = port_num;
1080 drm_dp_encode_sideband_req(&req, msg);
1081 msg->path_msg = true;
1085 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1086 struct drm_dp_vcpi *vcpi)
1090 mutex_lock(&mgr->payload_lock);
1091 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
1092 if (ret > mgr->max_payloads) {
1094 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
1098 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
1099 if (vcpi_ret > mgr->max_payloads) {
1101 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
1105 set_bit(ret, &mgr->payload_mask);
1106 set_bit(vcpi_ret, &mgr->vcpi_mask);
1107 vcpi->vcpi = vcpi_ret + 1;
1108 mgr->proposed_vcpis[ret - 1] = vcpi;
1110 mutex_unlock(&mgr->payload_lock);
1114 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
1121 mutex_lock(&mgr->payload_lock);
1122 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
1123 clear_bit(vcpi - 1, &mgr->vcpi_mask);
1125 for (i = 0; i < mgr->max_payloads; i++) {
1126 if (mgr->proposed_vcpis[i])
1127 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
1128 mgr->proposed_vcpis[i] = NULL;
1129 clear_bit(i + 1, &mgr->payload_mask);
1132 mutex_unlock(&mgr->payload_lock);
1135 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
1136 struct drm_dp_sideband_msg_tx *txmsg)
1141 * All updates to txmsg->state are protected by mgr->qlock, and the two
1142 * cases we check here are terminal states. For those the barriers
1143 * provided by the wake_up/wait_event pair are enough.
1145 state = READ_ONCE(txmsg->state);
1146 return (state == DRM_DP_SIDEBAND_TX_RX ||
1147 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
1150 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
1151 struct drm_dp_sideband_msg_tx *txmsg)
1153 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1156 ret = wait_event_timeout(mgr->tx_waitq,
1157 check_txmsg_state(mgr, txmsg),
1159 mutex_lock(&mstb->mgr->qlock);
1161 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
1166 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
1168 /* dump some state */
1172 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
1173 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
1174 list_del(&txmsg->next);
1177 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
1178 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
1179 mstb->tx_slots[txmsg->seqno] = NULL;
1183 if (unlikely(ret == -EIO && drm_debug & DRM_UT_DP)) {
1184 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
1186 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
1188 mutex_unlock(&mgr->qlock);
1193 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
1195 struct drm_dp_mst_branch *mstb;
1197 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
1203 memcpy(mstb->rad, rad, lct / 2);
1204 INIT_LIST_HEAD(&mstb->ports);
1205 kref_init(&mstb->topology_kref);
1206 kref_init(&mstb->malloc_kref);
1210 static void drm_dp_free_mst_branch_device(struct kref *kref)
1212 struct drm_dp_mst_branch *mstb =
1213 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
1215 if (mstb->port_parent)
1216 drm_dp_mst_put_port_malloc(mstb->port_parent);
1222 * DOC: Branch device and port refcounting
1224 * Topology refcount overview
1225 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
1227 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
1228 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
1229 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
1231 * Topology refcounts are not exposed to drivers, and are handled internally
1232 * by the DP MST helpers. The helpers use them in order to prevent the
1233 * in-memory topology state from being changed in the middle of critical
1234 * operations like changing the internal state of payload allocations. This
1235 * means each branch and port will be considered to be connected to the rest
1236 * of the topology until its topology refcount reaches zero. Additionally,
1237 * for ports this means that their associated &struct drm_connector will stay
1238 * registered with userspace until the port's refcount reaches 0.
1240 * Malloc refcount overview
1241 * ~~~~~~~~~~~~~~~~~~~~~~~~
1243 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
1244 * drm_dp_mst_branch allocated even after all of its topology references have
1245 * been dropped, so that the driver or MST helpers can safely access each
1246 * branch's last known state before it was disconnected from the topology.
1247 * When the malloc refcount of a port or branch reaches 0, the memory
1248 * allocation containing the &struct drm_dp_mst_branch or &struct
1249 * drm_dp_mst_port respectively will be freed.
1251 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
1252 * to drivers. As of writing this documentation, there are no drivers that
1253 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
1254 * helpers. Exposing this API to drivers in a race-free manner would take more
1255 * tweaking of the refcounting scheme, however patches are welcome provided
1256 * there is a legitimate driver usecase for this.
1258 * Refcount relationships in a topology
1259 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1261 * Let's take a look at why the relationship between topology and malloc
1262 * refcounts is designed the way it is.
1264 * .. kernel-figure:: dp-mst/topology-figure-1.dot
1266 * An example of topology and malloc refs in a DP MST topology with two
1267 * active payloads. Topology refcount increments are indicated by solid
1268 * lines, and malloc refcount increments are indicated by dashed lines.
1269 * Each starts from the branch which incremented the refcount, and ends at
1270 * the branch to which the refcount belongs to, i.e. the arrow points the
1271 * same way as the C pointers used to reference a structure.
1273 * As you can see in the above figure, every branch increments the topology
1274 * refcount of its children, and increments the malloc refcount of its
1275 * parent. Additionally, every payload increments the malloc refcount of its
1276 * assigned port by 1.
1278 * So, what would happen if MSTB #3 from the above figure was unplugged from
1279 * the system, but the driver hadn't yet removed payload #2 from port #3? The
1280 * topology would start to look like the figure below.
1282 * .. kernel-figure:: dp-mst/topology-figure-2.dot
1284 * Ports and branch devices which have been released from memory are
1285 * colored grey, and references which have been removed are colored red.
1287 * Whenever a port or branch device's topology refcount reaches zero, it will
1288 * decrement the topology refcounts of all its children, the malloc refcount
1289 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
1290 * #4, this means they both have been disconnected from the topology and freed
1291 * from memory. But, because payload #2 is still holding a reference to port
1292 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1293 * is still accessible from memory. This also means port #3 has not yet
1294 * decremented the malloc refcount of MSTB #3, so its &struct
1295 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1296 * malloc refcount reaches 0.
1298 * This relationship is necessary because in order to release payload #2, we
1299 * need to be able to figure out the last relative of port #3 that's still
1300 * connected to the topology. In this case, we would travel up the topology as
1303 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1305 * And finally, remove payload #2 by communicating with port #2 through
1306 * sideband transactions.
1310 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1312 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1314 * Increments &drm_dp_mst_branch.malloc_kref. When
1315 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1316 * will be released and @mstb may no longer be used.
1318 * See also: drm_dp_mst_put_mstb_malloc()
1321 drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1323 kref_get(&mstb->malloc_kref);
1324 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
1328 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1330 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1332 * Decrements &drm_dp_mst_branch.malloc_kref. When
1333 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1334 * will be released and @mstb may no longer be used.
1336 * See also: drm_dp_mst_get_mstb_malloc()
1339 drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1341 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
1342 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
1345 static void drm_dp_free_mst_port(struct kref *kref)
1347 struct drm_dp_mst_port *port =
1348 container_of(kref, struct drm_dp_mst_port, malloc_kref);
1350 drm_dp_mst_put_mstb_malloc(port->parent);
1355 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1356 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1358 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1359 * reaches 0, the memory allocation for @port will be released and @port may
1360 * no longer be used.
1362 * Because @port could potentially be freed at any time by the DP MST helpers
1363 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1364 * function, drivers that which to make use of &struct drm_dp_mst_port should
1365 * ensure that they grab at least one main malloc reference to their MST ports
1366 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1367 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1369 * See also: drm_dp_mst_put_port_malloc()
1372 drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1374 kref_get(&port->malloc_kref);
1375 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1377 EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1380 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1381 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1383 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1384 * reaches 0, the memory allocation for @port will be released and @port may
1385 * no longer be used.
1387 * See also: drm_dp_mst_get_port_malloc()
1390 drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1392 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1393 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1395 EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1397 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1399 struct drm_dp_mst_branch *mstb =
1400 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1401 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1402 struct drm_dp_mst_port *port, *tmp;
1403 bool wake_tx = false;
1405 mutex_lock(&mgr->lock);
1406 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
1407 list_del(&port->next);
1408 drm_dp_mst_topology_put_port(port);
1410 mutex_unlock(&mgr->lock);
1412 /* drop any tx slots msg */
1413 mutex_lock(&mstb->mgr->qlock);
1414 if (mstb->tx_slots[0]) {
1415 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1416 mstb->tx_slots[0] = NULL;
1419 if (mstb->tx_slots[1]) {
1420 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1421 mstb->tx_slots[1] = NULL;
1424 mutex_unlock(&mstb->mgr->qlock);
1427 wake_up_all(&mstb->mgr->tx_waitq);
1429 drm_dp_mst_put_mstb_malloc(mstb);
1433 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1434 * branch device unless it's zero
1435 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1437 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1438 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1439 * reached 0). Holding a topology reference implies that a malloc reference
1440 * will be held to @mstb as long as the user holds the topology reference.
1442 * Care should be taken to ensure that the user has at least one malloc
1443 * reference to @mstb. If you already have a topology reference to @mstb, you
1444 * should use drm_dp_mst_topology_get_mstb() instead.
1447 * drm_dp_mst_topology_get_mstb()
1448 * drm_dp_mst_topology_put_mstb()
1451 * * 1: A topology reference was grabbed successfully
1452 * * 0: @port is no longer in the topology, no reference was grabbed
1454 static int __must_check
1455 drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1457 int ret = kref_get_unless_zero(&mstb->topology_kref);
1460 DRM_DEBUG("mstb %p (%d)\n", mstb,
1461 kref_read(&mstb->topology_kref));
1467 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1469 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1471 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1472 * not it's already reached 0. This is only valid to use in scenarios where
1473 * you are already guaranteed to have at least one active topology reference
1474 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1477 * drm_dp_mst_topology_try_get_mstb()
1478 * drm_dp_mst_topology_put_mstb()
1480 static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1482 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1483 kref_get(&mstb->topology_kref);
1484 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1488 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1490 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1492 * Releases a topology reference from @mstb by decrementing
1493 * &drm_dp_mst_branch.topology_kref.
1496 * drm_dp_mst_topology_try_get_mstb()
1497 * drm_dp_mst_topology_get_mstb()
1500 drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1502 DRM_DEBUG("mstb %p (%d)\n",
1503 mstb, kref_read(&mstb->topology_kref) - 1);
1504 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1507 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
1509 struct drm_dp_mst_branch *mstb;
1512 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1513 case DP_PEER_DEVICE_SST_SINK:
1514 /* remove i2c over sideband */
1515 drm_dp_mst_unregister_i2c_bus(&port->aux);
1517 case DP_PEER_DEVICE_MST_BRANCHING:
1520 drm_dp_mst_topology_put_mstb(mstb);
1525 static void drm_dp_destroy_port(struct kref *kref)
1527 struct drm_dp_mst_port *port =
1528 container_of(kref, struct drm_dp_mst_port, topology_kref);
1529 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1532 kfree(port->cached_edid);
1535 * The only time we don't have a connector
1536 * on an output port is if the connector init
1539 if (port->connector) {
1540 /* we can't destroy the connector here, as
1541 * we might be holding the mode_config.mutex
1542 * from an EDID retrieval */
1544 mutex_lock(&mgr->destroy_connector_lock);
1545 list_add(&port->next, &mgr->destroy_connector_list);
1546 mutex_unlock(&mgr->destroy_connector_lock);
1547 schedule_work(&mgr->destroy_connector_work);
1550 /* no need to clean up vcpi
1551 * as if we have no connector we never setup a vcpi */
1552 drm_dp_port_teardown_pdt(port, port->pdt);
1553 port->pdt = DP_PEER_DEVICE_NONE;
1555 drm_dp_mst_put_port_malloc(port);
1559 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1560 * port unless it's zero
1561 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1563 * Attempts to grab a topology reference to @port, if it hasn't yet been
1564 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1565 * 0). Holding a topology reference implies that a malloc reference will be
1566 * held to @port as long as the user holds the topology reference.
1568 * Care should be taken to ensure that the user has at least one malloc
1569 * reference to @port. If you already have a topology reference to @port, you
1570 * should use drm_dp_mst_topology_get_port() instead.
1573 * drm_dp_mst_topology_get_port()
1574 * drm_dp_mst_topology_put_port()
1577 * * 1: A topology reference was grabbed successfully
1578 * * 0: @port is no longer in the topology, no reference was grabbed
1580 static int __must_check
1581 drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1583 int ret = kref_get_unless_zero(&port->topology_kref);
1586 DRM_DEBUG("port %p (%d)\n", port,
1587 kref_read(&port->topology_kref));
1593 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1594 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1596 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1597 * not it's already reached 0. This is only valid to use in scenarios where
1598 * you are already guaranteed to have at least one active topology reference
1599 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1602 * drm_dp_mst_topology_try_get_port()
1603 * drm_dp_mst_topology_put_port()
1605 static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1607 WARN_ON(kref_read(&port->topology_kref) == 0);
1608 kref_get(&port->topology_kref);
1609 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1613 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1614 * @port: The &struct drm_dp_mst_port to release the topology reference from
1616 * Releases a topology reference from @port by decrementing
1617 * &drm_dp_mst_port.topology_kref.
1620 * drm_dp_mst_topology_try_get_port()
1621 * drm_dp_mst_topology_get_port()
1623 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1625 DRM_DEBUG("port %p (%d)\n",
1626 port, kref_read(&port->topology_kref) - 1);
1627 kref_put(&port->topology_kref, drm_dp_destroy_port);
1630 static struct drm_dp_mst_branch *
1631 drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1632 struct drm_dp_mst_branch *to_find)
1634 struct drm_dp_mst_port *port;
1635 struct drm_dp_mst_branch *rmstb;
1637 if (to_find == mstb)
1640 list_for_each_entry(port, &mstb->ports, next) {
1642 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1643 port->mstb, to_find);
1651 static struct drm_dp_mst_branch *
1652 drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1653 struct drm_dp_mst_branch *mstb)
1655 struct drm_dp_mst_branch *rmstb = NULL;
1657 mutex_lock(&mgr->lock);
1658 if (mgr->mst_primary) {
1659 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1660 mgr->mst_primary, mstb);
1662 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1665 mutex_unlock(&mgr->lock);
1669 static struct drm_dp_mst_port *
1670 drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1671 struct drm_dp_mst_port *to_find)
1673 struct drm_dp_mst_port *port, *mport;
1675 list_for_each_entry(port, &mstb->ports, next) {
1676 if (port == to_find)
1680 mport = drm_dp_mst_topology_get_port_validated_locked(
1681 port->mstb, to_find);
1689 static struct drm_dp_mst_port *
1690 drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1691 struct drm_dp_mst_port *port)
1693 struct drm_dp_mst_port *rport = NULL;
1695 mutex_lock(&mgr->lock);
1696 if (mgr->mst_primary) {
1697 rport = drm_dp_mst_topology_get_port_validated_locked(
1698 mgr->mst_primary, port);
1700 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1703 mutex_unlock(&mgr->lock);
1707 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1709 struct drm_dp_mst_port *port;
1712 list_for_each_entry(port, &mstb->ports, next) {
1713 if (port->port_num == port_num) {
1714 ret = drm_dp_mst_topology_try_get_port(port);
1715 return ret ? port : NULL;
1723 * calculate a new RAD for this MST branch device
1724 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1725 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1727 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1730 int parent_lct = port->parent->lct;
1732 int idx = (parent_lct - 1) / 2;
1733 if (parent_lct > 1) {
1734 memcpy(rad, port->parent->rad, idx + 1);
1735 shift = (parent_lct % 2) ? 4 : 0;
1739 rad[idx] |= port->port_num << shift;
1740 return parent_lct + 1;
1744 * return sends link address for new mstb
1746 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1750 bool send_link = false;
1751 switch (port->pdt) {
1752 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1753 case DP_PEER_DEVICE_SST_SINK:
1754 /* add i2c over sideband */
1755 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1757 case DP_PEER_DEVICE_MST_BRANCHING:
1758 lct = drm_dp_calculate_rad(port, rad);
1760 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1762 port->mstb->mgr = port->mgr;
1763 port->mstb->port_parent = port;
1765 * Make sure this port's memory allocation stays
1766 * around until its child MSTB releases it
1768 drm_dp_mst_get_port_malloc(port);
1778 * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
1779 * @aux: Fake sideband AUX CH
1780 * @offset: address of the (first) register to read
1781 * @buffer: buffer to store the register values
1782 * @size: number of bytes in @buffer
1784 * Performs the same functionality for remote devices via
1785 * sideband messaging as drm_dp_dpcd_read() does for local
1786 * devices via actual AUX CH.
1788 * Return: Number of bytes read, or negative error code on failure.
1790 ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
1791 unsigned int offset, void *buffer, size_t size)
1793 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
1796 return drm_dp_send_dpcd_read(port->mgr, port,
1797 offset, size, buffer);
1801 * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
1802 * @aux: Fake sideband AUX CH
1803 * @offset: address of the (first) register to write
1804 * @buffer: buffer containing the values to write
1805 * @size: number of bytes in @buffer
1807 * Performs the same functionality for remote devices via
1808 * sideband messaging as drm_dp_dpcd_write() does for local
1809 * devices via actual AUX CH.
1811 * Return: 0 on success, negative error code on failure.
1813 ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
1814 unsigned int offset, void *buffer, size_t size)
1816 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
1819 return drm_dp_send_dpcd_write(port->mgr, port,
1820 offset, size, buffer);
1823 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1827 memcpy(mstb->guid, guid, 16);
1829 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1830 if (mstb->port_parent) {
1831 ret = drm_dp_send_dpcd_write(
1839 ret = drm_dp_dpcd_write(
1848 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1851 size_t proppath_size)
1855 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1856 for (i = 0; i < (mstb->lct - 1); i++) {
1857 int shift = (i % 2) ? 0 : 4;
1858 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1859 snprintf(temp, sizeof(temp), "-%d", port_num);
1860 strlcat(proppath, temp, proppath_size);
1862 snprintf(temp, sizeof(temp), "-%d", pnum);
1863 strlcat(proppath, temp, proppath_size);
1867 * drm_dp_mst_connector_late_register() - Late MST connector registration
1868 * @connector: The MST connector
1869 * @port: The MST port for this connector
1871 * Helper to register the remote aux device for this MST port. Drivers should
1872 * call this from their mst connector's late_register hook to enable MST aux
1875 * Return: 0 on success, negative error code on failure.
1877 int drm_dp_mst_connector_late_register(struct drm_connector *connector,
1878 struct drm_dp_mst_port *port)
1880 DRM_DEBUG_KMS("registering %s remote bus for %s\n",
1881 port->aux.name, connector->kdev->kobj.name);
1883 port->aux.dev = connector->kdev;
1884 return drm_dp_aux_register_devnode(&port->aux);
1886 EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
1889 * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
1890 * @connector: The MST connector
1891 * @port: The MST port for this connector
1893 * Helper to unregister the remote aux device for this MST port, registered by
1894 * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
1895 * connector's early_unregister hook.
1897 void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
1898 struct drm_dp_mst_port *port)
1900 DRM_DEBUG_KMS("unregistering %s remote bus for %s\n",
1901 port->aux.name, connector->kdev->kobj.name);
1902 drm_dp_aux_unregister_devnode(&port->aux);
1904 EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
1906 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1907 struct drm_device *dev,
1908 struct drm_dp_link_addr_reply_port *port_msg)
1910 struct drm_dp_mst_port *port;
1912 bool created = false;
1916 port = drm_dp_get_port(mstb, port_msg->port_number);
1918 port = kzalloc(sizeof(*port), GFP_KERNEL);
1921 kref_init(&port->topology_kref);
1922 kref_init(&port->malloc_kref);
1923 port->parent = mstb;
1924 port->port_num = port_msg->port_number;
1925 port->mgr = mstb->mgr;
1926 port->aux.name = "DPMST";
1927 port->aux.dev = dev->dev;
1928 port->aux.is_remote = true;
1931 * Make sure the memory allocation for our parent branch stays
1932 * around until our own memory allocation is released
1934 drm_dp_mst_get_mstb_malloc(mstb);
1938 old_pdt = port->pdt;
1939 old_ddps = port->ddps;
1942 port->pdt = port_msg->peer_device_type;
1943 port->input = port_msg->input_port;
1944 port->mcs = port_msg->mcs;
1945 port->ddps = port_msg->ddps;
1946 port->ldps = port_msg->legacy_device_plug_status;
1947 port->dpcd_rev = port_msg->dpcd_revision;
1948 port->num_sdp_streams = port_msg->num_sdp_streams;
1949 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1951 /* manage mstb port lists with mgr lock - take a reference
1954 mutex_lock(&mstb->mgr->lock);
1955 drm_dp_mst_topology_get_port(port);
1956 list_add(&port->next, &mstb->ports);
1957 mutex_unlock(&mstb->mgr->lock);
1960 if (old_ddps != port->ddps) {
1963 drm_dp_send_enum_path_resources(mstb->mgr,
1967 port->available_pbn = 0;
1971 if (old_pdt != port->pdt && !port->input) {
1972 drm_dp_port_teardown_pdt(port, old_pdt);
1974 ret = drm_dp_port_setup_pdt(port);
1976 drm_dp_send_link_address(mstb->mgr, port->mstb);
1979 if (created && !port->input) {
1982 build_mst_prop_path(mstb, port->port_num, proppath,
1984 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
1987 if (!port->connector) {
1988 /* remove it from the port list */
1989 mutex_lock(&mstb->mgr->lock);
1990 list_del(&port->next);
1991 mutex_unlock(&mstb->mgr->lock);
1992 /* drop port list reference */
1993 drm_dp_mst_topology_put_port(port);
1996 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1997 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1998 port->port_num >= DP_MST_LOGICAL_PORT_0) {
1999 port->cached_edid = drm_get_edid(port->connector,
2001 drm_connector_set_tile_property(port->connector);
2003 (*mstb->mgr->cbs->register_connector)(port->connector);
2007 /* put reference to this port */
2008 drm_dp_mst_topology_put_port(port);
2011 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
2012 struct drm_dp_connection_status_notify *conn_stat)
2014 struct drm_dp_mst_port *port;
2017 bool dowork = false;
2018 port = drm_dp_get_port(mstb, conn_stat->port_number);
2022 old_ddps = port->ddps;
2023 old_pdt = port->pdt;
2024 port->pdt = conn_stat->peer_device_type;
2025 port->mcs = conn_stat->message_capability_status;
2026 port->ldps = conn_stat->legacy_device_plug_status;
2027 port->ddps = conn_stat->displayport_device_plug_status;
2029 if (old_ddps != port->ddps) {
2033 port->available_pbn = 0;
2036 if (old_pdt != port->pdt && !port->input) {
2037 drm_dp_port_teardown_pdt(port, old_pdt);
2039 if (drm_dp_port_setup_pdt(port))
2043 drm_dp_mst_topology_put_port(port);
2045 queue_work(system_long_wq, &mstb->mgr->work);
2049 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
2052 struct drm_dp_mst_branch *mstb;
2053 struct drm_dp_mst_port *port;
2055 /* find the port by iterating down */
2057 mutex_lock(&mgr->lock);
2058 mstb = mgr->mst_primary;
2063 for (i = 0; i < lct - 1; i++) {
2064 int shift = (i % 2) ? 0 : 4;
2065 int port_num = (rad[i / 2] >> shift) & 0xf;
2067 list_for_each_entry(port, &mstb->ports, next) {
2068 if (port->port_num == port_num) {
2071 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
2079 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2083 mutex_unlock(&mgr->lock);
2087 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
2088 struct drm_dp_mst_branch *mstb,
2089 const uint8_t *guid)
2091 struct drm_dp_mst_branch *found_mstb;
2092 struct drm_dp_mst_port *port;
2094 if (memcmp(mstb->guid, guid, 16) == 0)
2098 list_for_each_entry(port, &mstb->ports, next) {
2102 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
2111 static struct drm_dp_mst_branch *
2112 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
2113 const uint8_t *guid)
2115 struct drm_dp_mst_branch *mstb;
2118 /* find the port by iterating down */
2119 mutex_lock(&mgr->lock);
2121 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
2123 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2128 mutex_unlock(&mgr->lock);
2132 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2133 struct drm_dp_mst_branch *mstb)
2135 struct drm_dp_mst_port *port;
2136 struct drm_dp_mst_branch *mstb_child;
2137 if (!mstb->link_address_sent)
2138 drm_dp_send_link_address(mgr, mstb);
2140 list_for_each_entry(port, &mstb->ports, next) {
2147 if (!port->available_pbn)
2148 drm_dp_send_enum_path_resources(mgr, mstb, port);
2151 mstb_child = drm_dp_mst_topology_get_mstb_validated(
2154 drm_dp_check_and_send_link_address(mgr, mstb_child);
2155 drm_dp_mst_topology_put_mstb(mstb_child);
2161 static void drm_dp_mst_link_probe_work(struct work_struct *work)
2163 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
2164 struct drm_dp_mst_branch *mstb;
2167 mutex_lock(&mgr->lock);
2168 mstb = mgr->mst_primary;
2170 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2174 mutex_unlock(&mgr->lock);
2176 drm_dp_check_and_send_link_address(mgr, mstb);
2177 drm_dp_mst_topology_put_mstb(mstb);
2181 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
2186 if (memchr_inv(guid, 0, 16))
2189 salt = get_jiffies_64();
2191 memcpy(&guid[0], &salt, sizeof(u64));
2192 memcpy(&guid[8], &salt, sizeof(u64));
2197 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
2199 struct drm_dp_sideband_msg_req_body req;
2201 req.req_type = DP_REMOTE_DPCD_READ;
2202 req.u.dpcd_read.port_number = port_num;
2203 req.u.dpcd_read.dpcd_address = offset;
2204 req.u.dpcd_read.num_bytes = num_bytes;
2205 drm_dp_encode_sideband_req(&req, msg);
2210 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
2211 bool up, u8 *msg, int len)
2214 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
2215 int tosend, total, offset;
2222 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
2224 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
2227 if (ret != tosend) {
2228 if (ret == -EIO && retries < 5) {
2232 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
2238 } while (total > 0);
2242 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
2243 struct drm_dp_sideband_msg_tx *txmsg)
2245 struct drm_dp_mst_branch *mstb = txmsg->dst;
2248 /* both msg slots are full */
2249 if (txmsg->seqno == -1) {
2250 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
2251 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
2254 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
2255 txmsg->seqno = mstb->last_seqno;
2256 mstb->last_seqno ^= 1;
2257 } else if (mstb->tx_slots[0] == NULL)
2261 mstb->tx_slots[txmsg->seqno] = txmsg;
2264 req_type = txmsg->msg[0] & 0x7f;
2265 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
2266 req_type == DP_RESOURCE_STATUS_NOTIFY)
2270 hdr->path_msg = txmsg->path_msg;
2271 hdr->lct = mstb->lct;
2272 hdr->lcr = mstb->lct - 1;
2274 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
2275 hdr->seqno = txmsg->seqno;
2279 * process a single block of the next message in the sideband queue
2281 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2282 struct drm_dp_sideband_msg_tx *txmsg,
2286 struct drm_dp_sideband_msg_hdr hdr;
2287 int len, space, idx, tosend;
2290 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
2292 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
2294 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
2297 /* make hdr from dst mst - for replies use seqno
2298 otherwise assign one */
2299 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
2303 /* amount left to send in this message */
2304 len = txmsg->cur_len - txmsg->cur_offset;
2306 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
2307 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
2309 tosend = min(len, space);
2310 if (len == txmsg->cur_len)
2316 hdr.msg_len = tosend + 1;
2317 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
2318 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
2319 /* add crc at end */
2320 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
2323 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
2324 if (unlikely(ret && drm_debug & DRM_UT_DP)) {
2325 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
2327 drm_printf(&p, "sideband msg failed to send\n");
2328 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2332 txmsg->cur_offset += tosend;
2333 if (txmsg->cur_offset == txmsg->cur_len) {
2334 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
2340 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
2342 struct drm_dp_sideband_msg_tx *txmsg;
2345 WARN_ON(!mutex_is_locked(&mgr->qlock));
2347 /* construct a chunk from the first msg in the tx_msg queue */
2348 if (list_empty(&mgr->tx_msg_downq))
2351 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
2352 ret = process_single_tx_qlock(mgr, txmsg, false);
2354 /* txmsg is sent it should be in the slots now */
2355 list_del(&txmsg->next);
2357 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
2358 list_del(&txmsg->next);
2359 if (txmsg->seqno != -1)
2360 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
2361 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
2362 wake_up_all(&mgr->tx_waitq);
2366 /* called holding qlock */
2367 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2368 struct drm_dp_sideband_msg_tx *txmsg)
2372 /* construct a chunk from the first msg in the tx_msg queue */
2373 ret = process_single_tx_qlock(mgr, txmsg, true);
2376 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
2378 if (txmsg->seqno != -1) {
2379 WARN_ON((unsigned int)txmsg->seqno >
2380 ARRAY_SIZE(txmsg->dst->tx_slots));
2381 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
2385 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2386 struct drm_dp_sideband_msg_tx *txmsg)
2388 mutex_lock(&mgr->qlock);
2389 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
2391 if (unlikely(drm_debug & DRM_UT_DP)) {
2392 struct drm_printer p = drm_debug_printer(DBG_PREFIX);
2394 drm_dp_mst_dump_sideband_msg_tx(&p, txmsg);
2397 if (list_is_singular(&mgr->tx_msg_downq))
2398 process_single_down_tx_qlock(mgr);
2399 mutex_unlock(&mgr->qlock);
2403 drm_dp_dump_link_address(struct drm_dp_link_address_ack_reply *reply)
2405 struct drm_dp_link_addr_reply_port *port_reply;
2408 for (i = 0; i < reply->nports; i++) {
2409 port_reply = &reply->ports[i];
2410 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",
2412 port_reply->input_port,
2413 port_reply->peer_device_type,
2414 port_reply->port_number,
2415 port_reply->dpcd_revision,
2418 port_reply->legacy_device_plug_status,
2419 port_reply->num_sdp_streams,
2420 port_reply->num_sdp_stream_sinks);
2424 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2425 struct drm_dp_mst_branch *mstb)
2428 struct drm_dp_sideband_msg_tx *txmsg;
2431 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2436 len = build_link_address(txmsg);
2438 mstb->link_address_sent = true;
2439 drm_dp_queue_down_tx(mgr, txmsg);
2441 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2445 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2446 DRM_DEBUG_KMS("link address nak received\n");
2448 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
2449 drm_dp_dump_link_address(&txmsg->reply.u.link_addr);
2451 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
2453 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
2454 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
2456 drm_kms_helper_hotplug_event(mgr->dev);
2459 mstb->link_address_sent = false;
2460 DRM_DEBUG_KMS("link address failed %d\n", ret);
2467 drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2468 struct drm_dp_mst_branch *mstb,
2469 struct drm_dp_mst_port *port)
2471 struct drm_dp_enum_path_resources_ack_reply *path_res;
2472 struct drm_dp_sideband_msg_tx *txmsg;
2476 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2481 len = build_enum_path_resources(txmsg, port->port_num);
2483 drm_dp_queue_down_tx(mgr, txmsg);
2485 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2487 path_res = &txmsg->reply.u.path_resources;
2489 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2490 DRM_DEBUG_KMS("enum path resources nak received\n");
2492 if (port->port_num != path_res->port_number)
2493 DRM_ERROR("got incorrect port in response\n");
2495 DRM_DEBUG_KMS("enum path resources %d: %d %d\n",
2496 path_res->port_number,
2497 path_res->full_payload_bw_number,
2498 path_res->avail_payload_bw_number);
2499 port->available_pbn =
2500 path_res->avail_payload_bw_number;
2508 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2510 if (!mstb->port_parent)
2513 if (mstb->port_parent->mstb != mstb)
2514 return mstb->port_parent;
2516 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2520 * Searches upwards in the topology starting from mstb to try to find the
2521 * closest available parent of mstb that's still connected to the rest of the
2522 * topology. This can be used in order to perform operations like releasing
2523 * payloads, where the branch device which owned the payload may no longer be
2524 * around and thus would require that the payload on the last living relative
2527 static struct drm_dp_mst_branch *
2528 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2529 struct drm_dp_mst_branch *mstb,
2532 struct drm_dp_mst_branch *rmstb = NULL;
2533 struct drm_dp_mst_port *found_port;
2535 mutex_lock(&mgr->lock);
2536 if (!mgr->mst_primary)
2540 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
2544 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
2545 rmstb = found_port->parent;
2546 *port_num = found_port->port_num;
2548 /* Search again, starting from this parent */
2549 mstb = found_port->parent;
2553 mutex_unlock(&mgr->lock);
2557 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
2558 struct drm_dp_mst_port *port,
2562 struct drm_dp_sideband_msg_tx *txmsg;
2563 struct drm_dp_mst_branch *mstb;
2564 int len, ret, port_num;
2565 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
2568 port_num = port->port_num;
2569 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2571 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
2579 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2585 for (i = 0; i < port->num_sdp_streams; i++)
2589 len = build_allocate_payload(txmsg, port_num,
2591 pbn, port->num_sdp_streams, sinks);
2593 drm_dp_queue_down_tx(mgr, txmsg);
2596 * FIXME: there is a small chance that between getting the last
2597 * connected mstb and sending the payload message, the last connected
2598 * mstb could also be removed from the topology. In the future, this
2599 * needs to be fixed by restarting the
2600 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
2601 * timeout if the topology is still connected to the system.
2603 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2605 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2612 drm_dp_mst_topology_put_mstb(mstb);
2616 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
2617 struct drm_dp_mst_port *port, bool power_up)
2619 struct drm_dp_sideband_msg_tx *txmsg;
2622 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2626 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2628 drm_dp_mst_topology_put_port(port);
2632 txmsg->dst = port->parent;
2633 len = build_power_updown_phy(txmsg, port->port_num, power_up);
2634 drm_dp_queue_down_tx(mgr, txmsg);
2636 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
2638 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2644 drm_dp_mst_topology_put_port(port);
2648 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
2650 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2652 struct drm_dp_payload *payload)
2656 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
2658 payload->payload_state = 0;
2661 payload->payload_state = DP_PAYLOAD_LOCAL;
2665 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2666 struct drm_dp_mst_port *port,
2668 struct drm_dp_payload *payload)
2671 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
2674 payload->payload_state = DP_PAYLOAD_REMOTE;
2678 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2679 struct drm_dp_mst_port *port,
2681 struct drm_dp_payload *payload)
2683 DRM_DEBUG_KMS("\n");
2684 /* it's okay for these to fail */
2686 drm_dp_payload_send_msg(mgr, port, id, 0);
2689 drm_dp_dpcd_write_payload(mgr, id, payload);
2690 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
2694 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2696 struct drm_dp_payload *payload)
2698 payload->payload_state = 0;
2703 * drm_dp_update_payload_part1() - Execute payload update part 1
2704 * @mgr: manager to use.
2706 * This iterates over all proposed virtual channels, and tries to
2707 * allocate space in the link for them. For 0->slots transitions,
2708 * this step just writes the VCPI to the MST device. For slots->0
2709 * transitions, this writes the updated VCPIs and removes the
2710 * remote VC payloads.
2712 * after calling this the driver should generate ACT and payload
2715 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
2717 struct drm_dp_payload req_payload;
2718 struct drm_dp_mst_port *port;
2722 mutex_lock(&mgr->payload_lock);
2723 for (i = 0; i < mgr->max_payloads; i++) {
2724 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2725 struct drm_dp_payload *payload = &mgr->payloads[i];
2726 bool put_port = false;
2728 /* solve the current payloads - compare to the hw ones
2729 - update the hw view */
2730 req_payload.start_slot = cur_slots;
2732 port = container_of(vcpi, struct drm_dp_mst_port,
2735 /* Validated ports don't matter if we're releasing
2738 if (vcpi->num_slots) {
2739 port = drm_dp_mst_topology_get_port_validated(
2742 mutex_unlock(&mgr->payload_lock);
2748 req_payload.num_slots = vcpi->num_slots;
2749 req_payload.vcpi = vcpi->vcpi;
2752 req_payload.num_slots = 0;
2755 payload->start_slot = req_payload.start_slot;
2756 /* work out what is required to happen with this payload */
2757 if (payload->num_slots != req_payload.num_slots) {
2759 /* need to push an update for this payload */
2760 if (req_payload.num_slots) {
2761 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
2763 payload->num_slots = req_payload.num_slots;
2764 payload->vcpi = req_payload.vcpi;
2766 } else if (payload->num_slots) {
2767 payload->num_slots = 0;
2768 drm_dp_destroy_payload_step1(mgr, port,
2771 req_payload.payload_state =
2772 payload->payload_state;
2773 payload->start_slot = 0;
2775 payload->payload_state = req_payload.payload_state;
2777 cur_slots += req_payload.num_slots;
2780 drm_dp_mst_topology_put_port(port);
2783 for (i = 0; i < mgr->max_payloads; i++) {
2784 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL)
2787 DRM_DEBUG_KMS("removing payload %d\n", i);
2788 for (j = i; j < mgr->max_payloads - 1; j++) {
2789 mgr->payloads[j] = mgr->payloads[j + 1];
2790 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
2792 if (mgr->proposed_vcpis[j] &&
2793 mgr->proposed_vcpis[j]->num_slots) {
2794 set_bit(j + 1, &mgr->payload_mask);
2796 clear_bit(j + 1, &mgr->payload_mask);
2800 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
2801 sizeof(struct drm_dp_payload));
2802 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
2803 clear_bit(mgr->max_payloads, &mgr->payload_mask);
2805 mutex_unlock(&mgr->payload_lock);
2809 EXPORT_SYMBOL(drm_dp_update_payload_part1);
2812 * drm_dp_update_payload_part2() - Execute payload update part 2
2813 * @mgr: manager to use.
2815 * This iterates over all proposed virtual channels, and tries to
2816 * allocate space in the link for them. For 0->slots transitions,
2817 * this step writes the remote VC payload commands. For slots->0
2818 * this just resets some internal state.
2820 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
2822 struct drm_dp_mst_port *port;
2825 mutex_lock(&mgr->payload_lock);
2826 for (i = 0; i < mgr->max_payloads; i++) {
2828 if (!mgr->proposed_vcpis[i])
2831 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2833 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2834 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
2835 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2836 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
2837 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2840 mutex_unlock(&mgr->payload_lock);
2844 mutex_unlock(&mgr->payload_lock);
2847 EXPORT_SYMBOL(drm_dp_update_payload_part2);
2849 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2850 struct drm_dp_mst_port *port,
2851 int offset, int size, u8 *bytes)
2855 struct drm_dp_sideband_msg_tx *txmsg;
2856 struct drm_dp_mst_branch *mstb;
2858 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2862 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2868 len = build_dpcd_read(txmsg, port->port_num, offset, size);
2869 txmsg->dst = port->parent;
2871 drm_dp_queue_down_tx(mgr, txmsg);
2873 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2877 /* DPCD read should never be NACKed */
2878 if (txmsg->reply.reply_type == 1) {
2879 DRM_ERROR("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
2880 mstb, port->port_num, offset, size);
2885 if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
2890 ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,
2892 memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret);
2897 drm_dp_mst_topology_put_mstb(mstb);
2902 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2903 struct drm_dp_mst_port *port,
2904 int offset, int size, u8 *bytes)
2908 struct drm_dp_sideband_msg_tx *txmsg;
2909 struct drm_dp_mst_branch *mstb;
2911 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2915 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2921 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2924 drm_dp_queue_down_tx(mgr, txmsg);
2926 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2928 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2935 drm_dp_mst_topology_put_mstb(mstb);
2939 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2941 struct drm_dp_sideband_msg_reply_body reply;
2943 reply.reply_type = DP_SIDEBAND_REPLY_ACK;
2944 reply.req_type = req_type;
2945 drm_dp_encode_sideband_reply(&reply, msg);
2949 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2950 struct drm_dp_mst_branch *mstb,
2951 int req_type, int seqno, bool broadcast)
2953 struct drm_dp_sideband_msg_tx *txmsg;
2955 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2960 txmsg->seqno = seqno;
2961 drm_dp_encode_up_ack_reply(txmsg, req_type);
2963 mutex_lock(&mgr->qlock);
2965 process_single_up_tx_qlock(mgr, txmsg);
2967 mutex_unlock(&mgr->qlock);
2973 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2977 switch (dp_link_bw) {
2979 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2980 dp_link_bw, dp_link_count);
2983 case DP_LINK_BW_1_62:
2984 *out = 3 * dp_link_count;
2986 case DP_LINK_BW_2_7:
2987 *out = 5 * dp_link_count;
2989 case DP_LINK_BW_5_4:
2990 *out = 10 * dp_link_count;
2992 case DP_LINK_BW_8_1:
2993 *out = 15 * dp_link_count;
3000 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
3001 * @mgr: manager to set state for
3002 * @mst_state: true to enable MST on this connector - false to disable.
3004 * This is called by the driver when it detects an MST capable device plugged
3005 * into a DP MST capable port, or when a DP MST capable device is unplugged.
3007 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
3010 struct drm_dp_mst_branch *mstb = NULL;
3012 mutex_lock(&mgr->lock);
3013 if (mst_state == mgr->mst_state)
3016 mgr->mst_state = mst_state;
3017 /* set the device into MST mode */
3019 WARN_ON(mgr->mst_primary);
3022 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
3023 if (ret != DP_RECEIVER_CAP_SIZE) {
3024 DRM_DEBUG_KMS("failed to read DPCD\n");
3028 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
3029 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
3035 /* add initial branch device at LCT 1 */
3036 mstb = drm_dp_add_mst_branch_device(1, NULL);
3043 /* give this the main reference */
3044 mgr->mst_primary = mstb;
3045 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
3047 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3048 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
3054 struct drm_dp_payload reset_pay;
3055 reset_pay.start_slot = 0;
3056 reset_pay.num_slots = 0x3f;
3057 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
3060 queue_work(system_long_wq, &mgr->work);
3064 /* disable MST on the device */
3065 mstb = mgr->mst_primary;
3066 mgr->mst_primary = NULL;
3067 /* this can fail if the device is gone */
3068 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
3070 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
3071 mgr->payload_mask = 0;
3072 set_bit(0, &mgr->payload_mask);
3077 mutex_unlock(&mgr->lock);
3079 drm_dp_mst_topology_put_mstb(mstb);
3083 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
3086 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
3087 * @mgr: manager to suspend
3089 * This function tells the MST device that we can't handle UP messages
3090 * anymore. This should stop it from sending any since we are suspended.
3092 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
3094 mutex_lock(&mgr->lock);
3095 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3096 DP_MST_EN | DP_UPSTREAM_IS_SRC);
3097 mutex_unlock(&mgr->lock);
3098 flush_work(&mgr->work);
3099 flush_work(&mgr->destroy_connector_work);
3101 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
3104 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
3105 * @mgr: manager to resume
3107 * This will fetch DPCD and see if the device is still there,
3108 * if it is, it will rewrite the MSTM control bits, and return.
3110 * if the device fails this returns -1, and the driver should do
3111 * a full MST reprobe, in case we were undocked.
3113 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
3117 mutex_lock(&mgr->lock);
3119 if (mgr->mst_primary) {
3123 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
3124 if (sret != DP_RECEIVER_CAP_SIZE) {
3125 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3130 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
3131 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
3133 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
3138 /* Some hubs forget their guids after they resume */
3139 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
3141 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
3145 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
3152 mutex_unlock(&mgr->lock);
3155 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
3157 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
3161 int replylen, origlen, curreply;
3163 struct drm_dp_sideband_msg_rx *msg;
3164 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
3165 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
3167 len = min(mgr->max_dpcd_transaction_bytes, 16);
3168 ret = drm_dp_dpcd_read(mgr->aux, basereg,
3171 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
3174 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
3176 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
3179 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
3184 while (replylen > 0) {
3185 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
3186 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
3189 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
3194 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
3196 DRM_DEBUG_KMS("failed to build sideband msg\n");
3206 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
3208 struct drm_dp_sideband_msg_tx *txmsg;
3209 struct drm_dp_mst_branch *mstb;
3210 struct drm_dp_sideband_msg_hdr *hdr = &mgr->down_rep_recv.initial_hdr;
3213 if (!drm_dp_get_one_sb_msg(mgr, false))
3214 goto clear_down_rep_recv;
3216 if (!mgr->down_rep_recv.have_eomt)
3219 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
3221 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3223 goto clear_down_rep_recv;
3226 /* find the message */
3228 mutex_lock(&mgr->qlock);
3229 txmsg = mstb->tx_slots[slot];
3230 /* remove from slots */
3231 mutex_unlock(&mgr->qlock);
3234 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
3235 mstb, hdr->seqno, hdr->lct, hdr->rad[0],
3236 mgr->down_rep_recv.msg[0]);
3240 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
3242 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3243 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
3244 txmsg->reply.req_type,
3245 drm_dp_mst_req_type_str(txmsg->reply.req_type),
3246 txmsg->reply.u.nak.reason,
3247 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
3248 txmsg->reply.u.nak.nak_data);
3250 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3251 drm_dp_mst_topology_put_mstb(mstb);
3253 mutex_lock(&mgr->qlock);
3254 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
3255 mstb->tx_slots[slot] = NULL;
3256 mutex_unlock(&mgr->qlock);
3258 wake_up_all(&mgr->tx_waitq);
3263 drm_dp_mst_topology_put_mstb(mstb);
3264 clear_down_rep_recv:
3265 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3270 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
3272 struct drm_dp_sideband_msg_req_body msg;
3273 struct drm_dp_sideband_msg_hdr *hdr = &mgr->up_req_recv.initial_hdr;
3274 struct drm_dp_mst_branch *mstb = NULL;
3278 if (!drm_dp_get_one_sb_msg(mgr, true))
3281 if (!mgr->up_req_recv.have_eomt)
3284 if (!hdr->broadcast) {
3285 mstb = drm_dp_get_mst_branch_device(mgr, hdr->lct, hdr->rad);
3287 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3294 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
3296 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY)
3297 guid = msg.u.conn_stat.guid;
3298 else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY)
3299 guid = msg.u.resource_stat.guid;
3303 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno,
3307 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid);
3309 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n",
3315 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
3316 drm_dp_update_port(mstb, &msg.u.conn_stat);
3318 DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",
3319 msg.u.conn_stat.port_number,
3320 msg.u.conn_stat.legacy_device_plug_status,
3321 msg.u.conn_stat.displayport_device_plug_status,
3322 msg.u.conn_stat.message_capability_status,
3323 msg.u.conn_stat.input_port,
3324 msg.u.conn_stat.peer_device_type);
3326 drm_kms_helper_hotplug_event(mgr->dev);
3327 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
3328 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n",
3329 msg.u.resource_stat.port_number,
3330 msg.u.resource_stat.available_pbn);
3333 drm_dp_mst_topology_put_mstb(mstb);
3335 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3340 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
3341 * @mgr: manager to notify irq for.
3342 * @esi: 4 bytes from SINK_COUNT_ESI
3343 * @handled: whether the hpd interrupt was consumed or not
3345 * This should be called from the driver when it detects a short IRQ,
3346 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
3347 * topology manager will process the sideband messages received as a result
3350 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
3357 if (sc != mgr->sink_count) {
3358 mgr->sink_count = sc;
3362 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
3363 ret = drm_dp_mst_handle_down_rep(mgr);
3367 if (esi[1] & DP_UP_REQ_MSG_RDY) {
3368 ret |= drm_dp_mst_handle_up_req(mgr);
3372 drm_dp_mst_kick_tx(mgr);
3375 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
3378 * drm_dp_mst_detect_port() - get connection status for an MST port
3379 * @connector: DRM connector for this port
3380 * @mgr: manager for this port
3381 * @port: unverified pointer to a port
3383 * This returns the current connection state for a port. It validates the
3384 * port pointer still exists so the caller doesn't require a reference
3386 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
3387 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3389 enum drm_connector_status status = connector_status_disconnected;
3391 /* we need to search for the port in the mgr in case it's gone */
3392 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3394 return connector_status_disconnected;
3399 switch (port->pdt) {
3400 case DP_PEER_DEVICE_NONE:
3401 case DP_PEER_DEVICE_MST_BRANCHING:
3404 case DP_PEER_DEVICE_SST_SINK:
3405 status = connector_status_connected;
3406 /* for logical ports - cache the EDID */
3407 if (port->port_num >= 8 && !port->cached_edid) {
3408 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
3411 case DP_PEER_DEVICE_DP_LEGACY_CONV:
3413 status = connector_status_connected;
3417 drm_dp_mst_topology_put_port(port);
3420 EXPORT_SYMBOL(drm_dp_mst_detect_port);
3423 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
3424 * @mgr: manager for this port
3425 * @port: unverified pointer to a port.
3427 * This returns whether the port supports audio or not.
3429 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
3430 struct drm_dp_mst_port *port)
3434 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3437 ret = port->has_audio;
3438 drm_dp_mst_topology_put_port(port);
3441 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
3444 * drm_dp_mst_get_edid() - get EDID for an MST port
3445 * @connector: toplevel connector to get EDID for
3446 * @mgr: manager for this port
3447 * @port: unverified pointer to a port.
3449 * This returns an EDID for the port connected to a connector,
3450 * It validates the pointer still exists so the caller doesn't require a
3453 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3455 struct edid *edid = NULL;
3457 /* we need to search for the port in the mgr in case it's gone */
3458 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3462 if (port->cached_edid)
3463 edid = drm_edid_duplicate(port->cached_edid);
3465 edid = drm_get_edid(connector, &port->aux.ddc);
3467 port->has_audio = drm_detect_monitor_audio(edid);
3468 drm_dp_mst_topology_put_port(port);
3471 EXPORT_SYMBOL(drm_dp_mst_get_edid);
3474 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
3475 * @mgr: manager to use
3476 * @pbn: payload bandwidth to convert into slots.
3478 * Calculate the number of VCPI slots that will be required for the given PBN
3479 * value. This function is deprecated, and should not be used in atomic
3483 * The total slots required for this port, or error.
3485 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
3490 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3492 /* max. time slots - one slot for MTP header */
3497 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
3499 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3500 struct drm_dp_vcpi *vcpi, int pbn, int slots)
3504 /* max. time slots - one slot for MTP header */
3509 vcpi->aligned_pbn = slots * mgr->pbn_div;
3510 vcpi->num_slots = slots;
3512 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
3519 * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
3520 * @state: global atomic state
3521 * @mgr: MST topology manager for the port
3522 * @port: port to find vcpi slots for
3523 * @pbn: bandwidth required for the mode in PBN
3525 * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
3526 * may have had. Any atomic drivers which support MST must call this function
3527 * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
3528 * current VCPI allocation for the new state, but only when
3529 * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
3530 * to ensure compatibility with userspace applications that still use the
3531 * legacy modesetting UAPI.
3533 * Allocations set by this function are not checked against the bandwidth
3534 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
3536 * Additionally, it is OK to call this function multiple times on the same
3537 * @port as needed. It is not OK however, to call this function and
3538 * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
3541 * drm_dp_atomic_release_vcpi_slots()
3542 * drm_dp_mst_atomic_check()
3545 * Total slots in the atomic state assigned for this port, or a negative error
3546 * code if the port no longer exists
3548 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
3549 struct drm_dp_mst_topology_mgr *mgr,
3550 struct drm_dp_mst_port *port, int pbn)
3552 struct drm_dp_mst_topology_state *topology_state;
3553 struct drm_dp_vcpi_allocation *pos, *vcpi = NULL;
3554 int prev_slots, req_slots, ret;
3556 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3557 if (IS_ERR(topology_state))
3558 return PTR_ERR(topology_state);
3560 /* Find the current allocation for this port, if any */
3561 list_for_each_entry(pos, &topology_state->vcpis, next) {
3562 if (pos->port == port) {
3564 prev_slots = vcpi->vcpi;
3567 * This should never happen, unless the driver tries
3568 * releasing and allocating the same VCPI allocation,
3571 if (WARN_ON(!prev_slots)) {
3572 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
3583 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3585 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
3586 port->connector->base.id, port->connector->name,
3587 port, prev_slots, req_slots);
3589 /* Add the new allocation to the state */
3591 vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL);
3595 drm_dp_mst_get_port_malloc(port);
3597 list_add(&vcpi->next, &topology_state->vcpis);
3599 vcpi->vcpi = req_slots;
3604 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
3607 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
3608 * @state: global atomic state
3609 * @mgr: MST topology manager for the port
3610 * @port: The port to release the VCPI slots from
3612 * Releases any VCPI slots that have been allocated to a port in the atomic
3613 * state. Any atomic drivers which support MST must call this function in
3614 * their &drm_connector_helper_funcs.atomic_check() callback when the
3615 * connector will no longer have VCPI allocated (e.g. because its CRTC was
3616 * removed) when it had VCPI allocated in the previous atomic state.
3618 * It is OK to call this even if @port has been removed from the system.
3619 * Additionally, it is OK to call this function multiple times on the same
3620 * @port as needed. It is not OK however, to call this function and
3621 * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
3625 * drm_dp_atomic_find_vcpi_slots()
3626 * drm_dp_mst_atomic_check()
3629 * 0 if all slots for this port were added back to
3630 * &drm_dp_mst_topology_state.avail_slots or negative error code
3632 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
3633 struct drm_dp_mst_topology_mgr *mgr,
3634 struct drm_dp_mst_port *port)
3636 struct drm_dp_mst_topology_state *topology_state;
3637 struct drm_dp_vcpi_allocation *pos;
3640 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3641 if (IS_ERR(topology_state))
3642 return PTR_ERR(topology_state);
3644 list_for_each_entry(pos, &topology_state->vcpis, next) {
3645 if (pos->port == port) {
3650 if (WARN_ON(!found)) {
3651 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
3652 port, &topology_state->base);
3656 DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi);
3658 drm_dp_mst_put_port_malloc(port);
3664 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
3667 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
3668 * @mgr: manager for this port
3669 * @port: port to allocate a virtual channel for.
3670 * @pbn: payload bandwidth number to request
3671 * @slots: returned number of slots for this PBN.
3673 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3674 struct drm_dp_mst_port *port, int pbn, int slots)
3678 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3685 if (port->vcpi.vcpi > 0) {
3686 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
3687 port->vcpi.vcpi, port->vcpi.pbn, pbn);
3688 if (pbn == port->vcpi.pbn) {
3689 drm_dp_mst_topology_put_port(port);
3694 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
3696 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
3697 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
3700 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
3701 pbn, port->vcpi.num_slots);
3703 /* Keep port allocated until its payload has been removed */
3704 drm_dp_mst_get_port_malloc(port);
3705 drm_dp_mst_topology_put_port(port);
3710 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
3712 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3715 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3719 slots = port->vcpi.num_slots;
3720 drm_dp_mst_topology_put_port(port);
3723 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
3726 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
3727 * @mgr: manager for this port
3728 * @port: unverified pointer to a port.
3730 * This just resets the number of slots for the ports VCPI for later programming.
3732 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3735 * A port with VCPI will remain allocated until its VCPI is
3736 * released, no verified ref needed
3739 port->vcpi.num_slots = 0;
3741 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
3744 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
3745 * @mgr: manager for this port
3746 * @port: port to deallocate vcpi for
3748 * This can be called unconditionally, regardless of whether
3749 * drm_dp_mst_allocate_vcpi() succeeded or not.
3751 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3752 struct drm_dp_mst_port *port)
3754 if (!port->vcpi.vcpi)
3757 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3758 port->vcpi.num_slots = 0;
3760 port->vcpi.aligned_pbn = 0;
3761 port->vcpi.vcpi = 0;
3762 drm_dp_mst_put_port_malloc(port);
3764 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
3766 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
3767 int id, struct drm_dp_payload *payload)
3769 u8 payload_alloc[3], status;
3773 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
3774 DP_PAYLOAD_TABLE_UPDATED);
3776 payload_alloc[0] = id;
3777 payload_alloc[1] = payload->start_slot;
3778 payload_alloc[2] = payload->num_slots;
3780 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
3782 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
3787 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3789 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3793 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
3796 usleep_range(10000, 20000);
3799 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
3810 * drm_dp_check_act_status() - Check ACT handled status.
3811 * @mgr: manager to use
3813 * Check the payload status bits in the DPCD for ACT handled completion.
3815 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
3822 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3825 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3829 if (status & DP_PAYLOAD_ACT_HANDLED)
3834 } while (count < 30);
3836 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
3837 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
3845 EXPORT_SYMBOL(drm_dp_check_act_status);
3848 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
3849 * @clock: dot clock for the mode
3850 * @bpp: bpp for the mode.
3852 * This uses the formula in the spec to calculate the PBN value for a mode.
3854 int drm_dp_calc_pbn_mode(int clock, int bpp)
3864 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
3865 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
3866 * common multiplier to render an integer PBN for all link rate/lane
3867 * counts combinations
3869 * peak_kbps *= (1006/1000)
3870 * peak_kbps *= (64/54)
3871 * peak_kbps *= 8 convert to bytes
3874 numerator = 64 * 1006;
3875 denominator = 54 * 8 * 1000 * 1000;
3878 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
3880 return drm_fixp2int_ceil(peak_kbps);
3882 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
3884 /* we want to kick the TX after we've ack the up/down IRQs. */
3885 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
3887 queue_work(system_long_wq, &mgr->tx_work);
3890 static void drm_dp_mst_dump_mstb(struct seq_file *m,
3891 struct drm_dp_mst_branch *mstb)
3893 struct drm_dp_mst_port *port;
3894 int tabs = mstb->lct;
3898 for (i = 0; i < tabs; i++)
3902 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
3903 list_for_each_entry(port, &mstb->ports, next) {
3904 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);
3906 drm_dp_mst_dump_mstb(m, port->mstb);
3910 #define DP_PAYLOAD_TABLE_SIZE 64
3912 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
3917 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
3918 if (drm_dp_dpcd_read(mgr->aux,
3919 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
3926 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3927 struct drm_dp_mst_port *port, char *name,
3930 struct edid *mst_edid;
3932 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3933 drm_edid_get_monitor_name(mst_edid, name, namelen);
3937 * drm_dp_mst_dump_topology(): dump topology to seq file.
3938 * @m: seq_file to dump output to
3939 * @mgr: manager to dump current topology for.
3941 * helper to dump MST topology to a seq file for debugfs.
3943 void drm_dp_mst_dump_topology(struct seq_file *m,
3944 struct drm_dp_mst_topology_mgr *mgr)
3947 struct drm_dp_mst_port *port;
3949 mutex_lock(&mgr->lock);
3950 if (mgr->mst_primary)
3951 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
3954 mutex_unlock(&mgr->lock);
3956 mutex_lock(&mgr->payload_lock);
3957 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
3960 for (i = 0; i < mgr->max_payloads; i++) {
3961 if (mgr->proposed_vcpis[i]) {
3964 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
3965 fetch_monitor_name(mgr, port, name, sizeof(name));
3966 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3967 port->port_num, port->vcpi.vcpi,
3968 port->vcpi.num_slots,
3969 (*name != 0) ? name : "Unknown");
3971 seq_printf(m, "vcpi %d:unused\n", i);
3973 for (i = 0; i < mgr->max_payloads; i++) {
3974 seq_printf(m, "payload %d: %d, %d, %d\n",
3976 mgr->payloads[i].payload_state,
3977 mgr->payloads[i].start_slot,
3978 mgr->payloads[i].num_slots);
3982 mutex_unlock(&mgr->payload_lock);
3984 mutex_lock(&mgr->lock);
3985 if (mgr->mst_primary) {
3986 u8 buf[DP_PAYLOAD_TABLE_SIZE];
3989 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
3990 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
3991 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
3992 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
3993 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
3994 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
3996 /* dump the standard OUI branch header */
3997 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
3998 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
3999 for (i = 0x3; i < 0x8 && buf[i]; i++)
4000 seq_printf(m, "%c", buf[i]);
4001 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
4002 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
4003 if (dump_dp_payload_table(mgr, buf))
4004 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
4007 mutex_unlock(&mgr->lock);
4010 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
4012 static void drm_dp_tx_work(struct work_struct *work)
4014 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
4016 mutex_lock(&mgr->qlock);
4017 if (!list_empty(&mgr->tx_msg_downq))
4018 process_single_down_tx_qlock(mgr);
4019 mutex_unlock(&mgr->qlock);
4022 static void drm_dp_destroy_connector_work(struct work_struct *work)
4024 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
4025 struct drm_dp_mst_port *port;
4026 bool send_hotplug = false;
4028 * Not a regular list traverse as we have to drop the destroy
4029 * connector lock before destroying the connector, to avoid AB->BA
4030 * ordering between this lock and the config mutex.
4033 mutex_lock(&mgr->destroy_connector_lock);
4034 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
4036 mutex_unlock(&mgr->destroy_connector_lock);
4039 list_del(&port->next);
4040 mutex_unlock(&mgr->destroy_connector_lock);
4042 mgr->cbs->destroy_connector(mgr, port->connector);
4044 drm_dp_port_teardown_pdt(port, port->pdt);
4045 port->pdt = DP_PEER_DEVICE_NONE;
4047 drm_dp_mst_put_port_malloc(port);
4048 send_hotplug = true;
4051 drm_kms_helper_hotplug_event(mgr->dev);
4054 static struct drm_private_state *
4055 drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
4057 struct drm_dp_mst_topology_state *state, *old_state =
4058 to_dp_mst_topology_state(obj->state);
4059 struct drm_dp_vcpi_allocation *pos, *vcpi;
4061 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
4065 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
4067 INIT_LIST_HEAD(&state->vcpis);
4069 list_for_each_entry(pos, &old_state->vcpis, next) {
4070 /* Prune leftover freed VCPI allocations */
4074 vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL);
4078 drm_dp_mst_get_port_malloc(vcpi->port);
4079 list_add(&vcpi->next, &state->vcpis);
4082 return &state->base;
4085 list_for_each_entry_safe(pos, vcpi, &state->vcpis, next) {
4086 drm_dp_mst_put_port_malloc(pos->port);
4094 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
4095 struct drm_private_state *state)
4097 struct drm_dp_mst_topology_state *mst_state =
4098 to_dp_mst_topology_state(state);
4099 struct drm_dp_vcpi_allocation *pos, *tmp;
4101 list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next) {
4102 /* We only keep references to ports with non-zero VCPIs */
4104 drm_dp_mst_put_port_malloc(pos->port);
4112 drm_dp_mst_atomic_check_topology_state(struct drm_dp_mst_topology_mgr *mgr,
4113 struct drm_dp_mst_topology_state *mst_state)
4115 struct drm_dp_vcpi_allocation *vcpi;
4116 int avail_slots = 63, payload_count = 0;
4118 list_for_each_entry(vcpi, &mst_state->vcpis, next) {
4119 /* Releasing VCPI is always OK-even if the port is gone */
4121 DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
4126 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
4127 vcpi->port, vcpi->vcpi);
4129 avail_slots -= vcpi->vcpi;
4130 if (avail_slots < 0) {
4131 DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
4132 vcpi->port, mst_state,
4133 avail_slots + vcpi->vcpi);
4137 if (++payload_count > mgr->max_payloads) {
4138 DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
4139 mgr, mst_state, mgr->max_payloads);
4143 DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
4144 mgr, mst_state, avail_slots,
4151 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
4152 * atomic update is valid
4153 * @state: Pointer to the new &struct drm_dp_mst_topology_state
4155 * Checks the given topology state for an atomic update to ensure that it's
4156 * valid. This includes checking whether there's enough bandwidth to support
4157 * the new VCPI allocations in the atomic update.
4159 * Any atomic drivers supporting DP MST must make sure to call this after
4160 * checking the rest of their state in their
4161 * &drm_mode_config_funcs.atomic_check() callback.
4164 * drm_dp_atomic_find_vcpi_slots()
4165 * drm_dp_atomic_release_vcpi_slots()
4169 * 0 if the new state is valid, negative error code otherwise.
4171 int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
4173 struct drm_dp_mst_topology_mgr *mgr;
4174 struct drm_dp_mst_topology_state *mst_state;
4177 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
4178 ret = drm_dp_mst_atomic_check_topology_state(mgr, mst_state);
4185 EXPORT_SYMBOL(drm_dp_mst_atomic_check);
4187 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
4188 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
4189 .atomic_destroy_state = drm_dp_mst_destroy_state,
4191 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
4194 * drm_atomic_get_mst_topology_state: get MST topology state
4196 * @state: global atomic state
4197 * @mgr: MST topology manager, also the private object in this case
4199 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
4200 * state vtable so that the private object state returned is that of a MST
4201 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
4202 * to care of the locking, so warn if don't hold the connection_mutex.
4206 * The MST topology state or error pointer.
4208 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
4209 struct drm_dp_mst_topology_mgr *mgr)
4211 struct drm_device *dev = mgr->dev;
4213 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
4214 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
4216 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
4219 * drm_dp_mst_topology_mgr_init - initialise a topology manager
4220 * @mgr: manager struct to initialise
4221 * @dev: device providing this structure - for i2c addition.
4222 * @aux: DP helper aux channel to talk to this device
4223 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
4224 * @max_payloads: maximum number of payloads this GPU can source
4225 * @conn_base_id: the connector object ID the MST device is connected to.
4227 * Return 0 for success, or negative error code on failure
4229 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
4230 struct drm_device *dev, struct drm_dp_aux *aux,
4231 int max_dpcd_transaction_bytes,
4232 int max_payloads, int conn_base_id)
4234 struct drm_dp_mst_topology_state *mst_state;
4236 mutex_init(&mgr->lock);
4237 mutex_init(&mgr->qlock);
4238 mutex_init(&mgr->payload_lock);
4239 mutex_init(&mgr->destroy_connector_lock);
4240 INIT_LIST_HEAD(&mgr->tx_msg_downq);
4241 INIT_LIST_HEAD(&mgr->destroy_connector_list);
4242 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
4243 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
4244 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
4245 init_waitqueue_head(&mgr->tx_waitq);
4248 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
4249 mgr->max_payloads = max_payloads;
4250 mgr->conn_base_id = conn_base_id;
4251 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
4252 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
4254 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
4257 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
4258 if (!mgr->proposed_vcpis)
4260 set_bit(0, &mgr->payload_mask);
4262 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
4263 if (mst_state == NULL)
4266 mst_state->mgr = mgr;
4267 INIT_LIST_HEAD(&mst_state->vcpis);
4269 drm_atomic_private_obj_init(dev, &mgr->base,
4271 &drm_dp_mst_topology_state_funcs);
4275 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
4278 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
4279 * @mgr: manager to destroy
4281 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
4283 drm_dp_mst_topology_mgr_set_mst(mgr, false);
4284 flush_work(&mgr->work);
4285 flush_work(&mgr->destroy_connector_work);
4286 mutex_lock(&mgr->payload_lock);
4287 kfree(mgr->payloads);
4288 mgr->payloads = NULL;
4289 kfree(mgr->proposed_vcpis);
4290 mgr->proposed_vcpis = NULL;
4291 mutex_unlock(&mgr->payload_lock);
4294 drm_atomic_private_obj_fini(&mgr->base);
4297 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
4299 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
4303 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
4306 for (i = 0; i < num - 1; i++) {
4307 if (msgs[i].flags & I2C_M_RD ||
4312 return msgs[num - 1].flags & I2C_M_RD &&
4313 msgs[num - 1].len <= 0xff;
4317 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
4320 struct drm_dp_aux *aux = adapter->algo_data;
4321 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
4322 struct drm_dp_mst_branch *mstb;
4323 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
4325 struct drm_dp_sideband_msg_req_body msg;
4326 struct drm_dp_sideband_msg_tx *txmsg = NULL;
4329 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
4333 if (!remote_i2c_read_ok(msgs, num)) {
4334 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
4339 memset(&msg, 0, sizeof(msg));
4340 msg.req_type = DP_REMOTE_I2C_READ;
4341 msg.u.i2c_read.num_transactions = num - 1;
4342 msg.u.i2c_read.port_number = port->port_num;
4343 for (i = 0; i < num - 1; i++) {
4344 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
4345 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
4346 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
4347 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
4349 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
4350 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
4352 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
4359 drm_dp_encode_sideband_req(&msg, txmsg);
4361 drm_dp_queue_down_tx(mgr, txmsg);
4363 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
4366 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
4370 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
4374 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
4379 drm_dp_mst_topology_put_mstb(mstb);
4383 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
4385 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
4386 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
4387 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
4388 I2C_FUNC_10BIT_ADDR;
4391 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
4392 .functionality = drm_dp_mst_i2c_functionality,
4393 .master_xfer = drm_dp_mst_i2c_xfer,
4397 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
4398 * @aux: DisplayPort AUX channel
4400 * Returns 0 on success or a negative error code on failure.
4402 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
4404 aux->ddc.algo = &drm_dp_mst_i2c_algo;
4405 aux->ddc.algo_data = aux;
4406 aux->ddc.retries = 3;
4408 aux->ddc.class = I2C_CLASS_DDC;
4409 aux->ddc.owner = THIS_MODULE;
4410 aux->ddc.dev.parent = aux->dev;
4411 aux->ddc.dev.of_node = aux->dev->of_node;
4413 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
4414 sizeof(aux->ddc.name));
4416 return i2c_add_adapter(&aux->ddc);
4420 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
4421 * @aux: DisplayPort AUX channel
4423 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
4425 i2c_del_adapter(&aux->ddc);