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/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/seq_file.h>
29 #include <linux/i2c.h>
30 #include <drm/drm_dp_mst_helper.h>
33 #include <drm/drm_fixed.h>
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_atomic_helper.h>
36 #include <drm/drm_crtc_helper.h>
41 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
42 * protocol. The helpers contain a topology manager and bandwidth manager.
43 * The helpers encapsulate the sending and received of sideband msgs.
45 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
47 static int test_calc_pbn_mode(void);
49 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
51 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
53 struct drm_dp_payload *payload);
55 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
56 struct drm_dp_mst_port *port,
57 int offset, int size, u8 *bytes);
59 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
60 struct drm_dp_mst_branch *mstb);
61 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
62 struct drm_dp_mst_branch *mstb,
63 struct drm_dp_mst_port *port);
64 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
67 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
68 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
69 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
70 /* sideband msg handling */
71 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
76 int number_of_bits = num_nibbles * 4;
79 while (number_of_bits != 0) {
82 remainder |= (data[array_index] & bitmask) >> bitshift;
90 if ((remainder & 0x10) == 0x10)
95 while (number_of_bits != 0) {
98 if ((remainder & 0x10) != 0)
105 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
110 int number_of_bits = number_of_bytes * 8;
113 while (number_of_bits != 0) {
116 remainder |= (data[array_index] & bitmask) >> bitshift;
124 if ((remainder & 0x100) == 0x100)
129 while (number_of_bits != 0) {
132 if ((remainder & 0x100) != 0)
136 return remainder & 0xff;
138 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
141 size += (hdr->lct / 2);
145 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
151 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
152 for (i = 0; i < (hdr->lct / 2); i++)
153 buf[idx++] = hdr->rad[i];
154 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
155 (hdr->msg_len & 0x3f);
156 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
158 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
159 buf[idx - 1] |= (crc4 & 0xf);
164 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
165 u8 *buf, int buflen, u8 *hdrlen)
174 len += ((buf[0] & 0xf0) >> 4) / 2;
177 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
179 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
180 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
184 hdr->lct = (buf[0] & 0xf0) >> 4;
185 hdr->lcr = (buf[0] & 0xf);
187 for (i = 0; i < (hdr->lct / 2); i++)
188 hdr->rad[i] = buf[idx++];
189 hdr->broadcast = (buf[idx] >> 7) & 0x1;
190 hdr->path_msg = (buf[idx] >> 6) & 0x1;
191 hdr->msg_len = buf[idx] & 0x3f;
193 hdr->somt = (buf[idx] >> 7) & 0x1;
194 hdr->eomt = (buf[idx] >> 6) & 0x1;
195 hdr->seqno = (buf[idx] >> 4) & 0x1;
201 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
202 struct drm_dp_sideband_msg_tx *raw)
207 buf[idx++] = req->req_type & 0x7f;
209 switch (req->req_type) {
210 case DP_ENUM_PATH_RESOURCES:
211 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
214 case DP_ALLOCATE_PAYLOAD:
215 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
216 (req->u.allocate_payload.number_sdp_streams & 0xf);
218 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
220 buf[idx] = (req->u.allocate_payload.pbn >> 8);
222 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
224 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
225 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
226 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
229 if (req->u.allocate_payload.number_sdp_streams & 1) {
230 i = req->u.allocate_payload.number_sdp_streams - 1;
231 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
235 case DP_QUERY_PAYLOAD:
236 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
238 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
241 case DP_REMOTE_DPCD_READ:
242 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
243 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
245 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
247 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
249 buf[idx] = (req->u.dpcd_read.num_bytes);
253 case DP_REMOTE_DPCD_WRITE:
254 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
255 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
257 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
259 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
261 buf[idx] = (req->u.dpcd_write.num_bytes);
263 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
264 idx += req->u.dpcd_write.num_bytes;
266 case DP_REMOTE_I2C_READ:
267 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
268 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
270 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
271 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
273 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
275 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
276 idx += req->u.i2c_read.transactions[i].num_bytes;
278 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
279 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
282 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
284 buf[idx] = (req->u.i2c_read.num_bytes_read);
288 case DP_REMOTE_I2C_WRITE:
289 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
291 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
293 buf[idx] = (req->u.i2c_write.num_bytes);
295 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
296 idx += req->u.i2c_write.num_bytes;
299 case DP_POWER_DOWN_PHY:
300 case DP_POWER_UP_PHY:
301 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
308 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
311 crc4 = drm_dp_msg_data_crc4(msg, len);
315 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
316 struct drm_dp_sideband_msg_tx *raw)
321 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
326 /* this adds a chunk of msg to the builder to get the final msg */
327 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
328 u8 *replybuf, u8 replybuflen, bool hdr)
335 struct drm_dp_sideband_msg_hdr recv_hdr;
336 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
338 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
343 * ignore out-of-order messages or messages that are part of a
346 if (!recv_hdr.somt && !msg->have_somt)
349 /* get length contained in this portion */
350 msg->curchunk_len = recv_hdr.msg_len;
351 msg->curchunk_hdrlen = hdrlen;
353 /* we have already gotten an somt - don't bother parsing */
354 if (recv_hdr.somt && msg->have_somt)
358 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
359 msg->have_somt = true;
362 msg->have_eomt = true;
364 /* copy the bytes for the remainder of this header chunk */
365 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
366 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
368 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
369 msg->curchunk_idx += replybuflen;
372 if (msg->curchunk_idx >= msg->curchunk_len) {
374 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
375 /* copy chunk into bigger msg */
376 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
377 msg->curlen += msg->curchunk_len - 1;
382 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
383 struct drm_dp_sideband_msg_reply_body *repmsg)
387 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
389 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
391 if (idx > raw->curlen)
393 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
394 if (raw->msg[idx] & 0x80)
395 repmsg->u.link_addr.ports[i].input_port = 1;
397 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
398 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
401 if (idx > raw->curlen)
403 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
404 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
405 if (repmsg->u.link_addr.ports[i].input_port == 0)
406 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
408 if (idx > raw->curlen)
410 if (repmsg->u.link_addr.ports[i].input_port == 0) {
411 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
413 if (idx > raw->curlen)
415 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
417 if (idx > raw->curlen)
419 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
420 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
424 if (idx > raw->curlen)
430 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
434 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
435 struct drm_dp_sideband_msg_reply_body *repmsg)
438 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
440 if (idx > raw->curlen)
442 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
444 if (idx > raw->curlen)
447 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
450 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
454 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
455 struct drm_dp_sideband_msg_reply_body *repmsg)
458 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
460 if (idx > raw->curlen)
464 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
468 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
469 struct drm_dp_sideband_msg_reply_body *repmsg)
473 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
475 if (idx > raw->curlen)
477 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
480 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
483 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
487 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
488 struct drm_dp_sideband_msg_reply_body *repmsg)
491 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
493 if (idx > raw->curlen)
495 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
497 if (idx > raw->curlen)
499 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
501 if (idx > raw->curlen)
505 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
509 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
510 struct drm_dp_sideband_msg_reply_body *repmsg)
513 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
515 if (idx > raw->curlen)
517 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
519 if (idx > raw->curlen)
521 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
523 if (idx > raw->curlen)
527 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
531 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
532 struct drm_dp_sideband_msg_reply_body *repmsg)
535 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
537 if (idx > raw->curlen)
539 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
541 if (idx > raw->curlen)
545 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
549 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
550 struct drm_dp_sideband_msg_reply_body *repmsg)
554 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
556 if (idx > raw->curlen) {
557 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
564 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
565 struct drm_dp_sideband_msg_reply_body *msg)
567 memset(msg, 0, sizeof(*msg));
568 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
569 msg->req_type = (raw->msg[0] & 0x7f);
571 if (msg->reply_type) {
572 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
573 msg->u.nak.reason = raw->msg[17];
574 msg->u.nak.nak_data = raw->msg[18];
578 switch (msg->req_type) {
579 case DP_LINK_ADDRESS:
580 return drm_dp_sideband_parse_link_address(raw, msg);
581 case DP_QUERY_PAYLOAD:
582 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
583 case DP_REMOTE_DPCD_READ:
584 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
585 case DP_REMOTE_DPCD_WRITE:
586 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
587 case DP_REMOTE_I2C_READ:
588 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
589 case DP_ENUM_PATH_RESOURCES:
590 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
591 case DP_ALLOCATE_PAYLOAD:
592 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
593 case DP_POWER_DOWN_PHY:
594 case DP_POWER_UP_PHY:
595 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
597 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
602 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
603 struct drm_dp_sideband_msg_req_body *msg)
607 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
609 if (idx > raw->curlen)
612 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
614 if (idx > raw->curlen)
617 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
618 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
619 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
620 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
621 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
625 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
629 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
630 struct drm_dp_sideband_msg_req_body *msg)
634 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
636 if (idx > raw->curlen)
639 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
641 if (idx > raw->curlen)
644 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
648 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
652 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
653 struct drm_dp_sideband_msg_req_body *msg)
655 memset(msg, 0, sizeof(*msg));
656 msg->req_type = (raw->msg[0] & 0x7f);
658 switch (msg->req_type) {
659 case DP_CONNECTION_STATUS_NOTIFY:
660 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
661 case DP_RESOURCE_STATUS_NOTIFY:
662 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
664 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
669 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
671 struct drm_dp_sideband_msg_req_body req;
673 req.req_type = DP_REMOTE_DPCD_WRITE;
674 req.u.dpcd_write.port_number = port_num;
675 req.u.dpcd_write.dpcd_address = offset;
676 req.u.dpcd_write.num_bytes = num_bytes;
677 req.u.dpcd_write.bytes = bytes;
678 drm_dp_encode_sideband_req(&req, msg);
683 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
685 struct drm_dp_sideband_msg_req_body req;
687 req.req_type = DP_LINK_ADDRESS;
688 drm_dp_encode_sideband_req(&req, msg);
692 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
694 struct drm_dp_sideband_msg_req_body req;
696 req.req_type = DP_ENUM_PATH_RESOURCES;
697 req.u.port_num.port_number = port_num;
698 drm_dp_encode_sideband_req(&req, msg);
699 msg->path_msg = true;
703 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
704 u8 vcpi, uint16_t pbn,
705 u8 number_sdp_streams,
708 struct drm_dp_sideband_msg_req_body req;
709 memset(&req, 0, sizeof(req));
710 req.req_type = DP_ALLOCATE_PAYLOAD;
711 req.u.allocate_payload.port_number = port_num;
712 req.u.allocate_payload.vcpi = vcpi;
713 req.u.allocate_payload.pbn = pbn;
714 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
715 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
717 drm_dp_encode_sideband_req(&req, msg);
718 msg->path_msg = true;
722 static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
723 int port_num, bool power_up)
725 struct drm_dp_sideband_msg_req_body req;
728 req.req_type = DP_POWER_UP_PHY;
730 req.req_type = DP_POWER_DOWN_PHY;
732 req.u.port_num.port_number = port_num;
733 drm_dp_encode_sideband_req(&req, msg);
734 msg->path_msg = true;
738 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
739 struct drm_dp_vcpi *vcpi)
743 mutex_lock(&mgr->payload_lock);
744 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
745 if (ret > mgr->max_payloads) {
747 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
751 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
752 if (vcpi_ret > mgr->max_payloads) {
754 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
758 set_bit(ret, &mgr->payload_mask);
759 set_bit(vcpi_ret, &mgr->vcpi_mask);
760 vcpi->vcpi = vcpi_ret + 1;
761 mgr->proposed_vcpis[ret - 1] = vcpi;
763 mutex_unlock(&mgr->payload_lock);
767 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
774 mutex_lock(&mgr->payload_lock);
775 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
776 clear_bit(vcpi - 1, &mgr->vcpi_mask);
778 for (i = 0; i < mgr->max_payloads; i++) {
779 if (mgr->proposed_vcpis[i])
780 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
781 mgr->proposed_vcpis[i] = NULL;
782 clear_bit(i + 1, &mgr->payload_mask);
785 mutex_unlock(&mgr->payload_lock);
788 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
789 struct drm_dp_sideband_msg_tx *txmsg)
794 * All updates to txmsg->state are protected by mgr->qlock, and the two
795 * cases we check here are terminal states. For those the barriers
796 * provided by the wake_up/wait_event pair are enough.
798 state = READ_ONCE(txmsg->state);
799 return (state == DRM_DP_SIDEBAND_TX_RX ||
800 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
803 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
804 struct drm_dp_sideband_msg_tx *txmsg)
806 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
809 ret = wait_event_timeout(mgr->tx_waitq,
810 check_txmsg_state(mgr, txmsg),
812 mutex_lock(&mstb->mgr->qlock);
814 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
819 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
821 /* dump some state */
825 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
826 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
827 list_del(&txmsg->next);
830 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
831 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
832 mstb->tx_slots[txmsg->seqno] = NULL;
836 mutex_unlock(&mgr->qlock);
841 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
843 struct drm_dp_mst_branch *mstb;
845 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
851 memcpy(mstb->rad, rad, lct / 2);
852 INIT_LIST_HEAD(&mstb->ports);
853 kref_init(&mstb->topology_kref);
854 kref_init(&mstb->malloc_kref);
858 static void drm_dp_free_mst_branch_device(struct kref *kref)
860 struct drm_dp_mst_branch *mstb =
861 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
863 if (mstb->port_parent)
864 drm_dp_mst_put_port_malloc(mstb->port_parent);
870 * DOC: Branch device and port refcounting
872 * Topology refcount overview
873 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
875 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
876 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
877 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
879 * Topology refcounts are not exposed to drivers, and are handled internally
880 * by the DP MST helpers. The helpers use them in order to prevent the
881 * in-memory topology state from being changed in the middle of critical
882 * operations like changing the internal state of payload allocations. This
883 * means each branch and port will be considered to be connected to the rest
884 * of the topology until it's topology refcount reaches zero. Additionally,
885 * for ports this means that their associated &struct drm_connector will stay
886 * registered with userspace until the port's refcount reaches 0.
888 * Malloc refcount overview
889 * ~~~~~~~~~~~~~~~~~~~~~~~~
891 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
892 * drm_dp_mst_branch allocated even after all of its topology references have
893 * been dropped, so that the driver or MST helpers can safely access each
894 * branch's last known state before it was disconnected from the topology.
895 * When the malloc refcount of a port or branch reaches 0, the memory
896 * allocation containing the &struct drm_dp_mst_branch or &struct
897 * drm_dp_mst_port respectively will be freed.
899 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
900 * to drivers. As of writing this documentation, there are no drivers that
901 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
902 * helpers. Exposing this API to drivers in a race-free manner would take more
903 * tweaking of the refcounting scheme, however patches are welcome provided
904 * there is a legitimate driver usecase for this.
906 * Refcount relationships in a topology
907 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
909 * Let's take a look at why the relationship between topology and malloc
910 * refcounts is designed the way it is.
912 * .. kernel-figure:: dp-mst/topology-figure-1.dot
914 * An example of topology and malloc refs in a DP MST topology with two
915 * active payloads. Topology refcount increments are indicated by solid
916 * lines, and malloc refcount increments are indicated by dashed lines.
917 * Each starts from the branch which incremented the refcount, and ends at
918 * the branch to which the refcount belongs to, i.e. the arrow points the
919 * same way as the C pointers used to reference a structure.
921 * As you can see in the above figure, every branch increments the topology
922 * refcount of it's children, and increments the malloc refcount of it's
923 * parent. Additionally, every payload increments the malloc refcount of it's
924 * assigned port by 1.
926 * So, what would happen if MSTB #3 from the above figure was unplugged from
927 * the system, but the driver hadn't yet removed payload #2 from port #3? The
928 * topology would start to look like the figure below.
930 * .. kernel-figure:: dp-mst/topology-figure-2.dot
932 * Ports and branch devices which have been released from memory are
933 * colored grey, and references which have been removed are colored red.
935 * Whenever a port or branch device's topology refcount reaches zero, it will
936 * decrement the topology refcounts of all its children, the malloc refcount
937 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
938 * #4, this means they both have been disconnected from the topology and freed
939 * from memory. But, because payload #2 is still holding a reference to port
940 * #3, port #3 is removed from the topology but it's &struct drm_dp_mst_port
941 * is still accessible from memory. This also means port #3 has not yet
942 * decremented the malloc refcount of MSTB #3, so it's &struct
943 * drm_dp_mst_branch will also stay allocated in memory until port #3's
944 * malloc refcount reaches 0.
946 * This relationship is necessary because in order to release payload #2, we
947 * need to be able to figure out the last relative of port #3 that's still
948 * connected to the topology. In this case, we would travel up the topology as
951 * .. kernel-figure:: dp-mst/topology-figure-3.dot
953 * And finally, remove payload #2 by communicating with port #2 through
954 * sideband transactions.
958 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
960 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
962 * Increments &drm_dp_mst_branch.malloc_kref. When
963 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
964 * will be released and @mstb may no longer be used.
966 * See also: drm_dp_mst_put_mstb_malloc()
969 drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
971 kref_get(&mstb->malloc_kref);
972 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
976 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
978 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
980 * Decrements &drm_dp_mst_branch.malloc_kref. When
981 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
982 * will be released and @mstb may no longer be used.
984 * See also: drm_dp_mst_get_mstb_malloc()
987 drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
989 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
990 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
993 static void drm_dp_free_mst_port(struct kref *kref)
995 struct drm_dp_mst_port *port =
996 container_of(kref, struct drm_dp_mst_port, malloc_kref);
998 drm_dp_mst_put_mstb_malloc(port->parent);
1003 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1004 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1006 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1007 * reaches 0, the memory allocation for @port will be released and @port may
1008 * no longer be used.
1010 * Because @port could potentially be freed at any time by the DP MST helpers
1011 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1012 * function, drivers that which to make use of &struct drm_dp_mst_port should
1013 * ensure that they grab at least one main malloc reference to their MST ports
1014 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1015 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1017 * See also: drm_dp_mst_put_port_malloc()
1020 drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1022 kref_get(&port->malloc_kref);
1023 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1025 EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1028 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1029 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1031 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1032 * reaches 0, the memory allocation for @port will be released and @port may
1033 * no longer be used.
1035 * See also: drm_dp_mst_get_port_malloc()
1038 drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1040 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1041 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1043 EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1045 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1047 struct drm_dp_mst_branch *mstb =
1048 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1049 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1050 struct drm_dp_mst_port *port, *tmp;
1051 bool wake_tx = false;
1053 mutex_lock(&mgr->lock);
1054 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
1055 list_del(&port->next);
1056 drm_dp_mst_topology_put_port(port);
1058 mutex_unlock(&mgr->lock);
1060 /* drop any tx slots msg */
1061 mutex_lock(&mstb->mgr->qlock);
1062 if (mstb->tx_slots[0]) {
1063 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1064 mstb->tx_slots[0] = NULL;
1067 if (mstb->tx_slots[1]) {
1068 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1069 mstb->tx_slots[1] = NULL;
1072 mutex_unlock(&mstb->mgr->qlock);
1075 wake_up_all(&mstb->mgr->tx_waitq);
1077 drm_dp_mst_put_mstb_malloc(mstb);
1081 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1082 * branch device unless its zero
1083 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1085 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1086 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1087 * reached 0). Holding a topology reference implies that a malloc reference
1088 * will be held to @mstb as long as the user holds the topology reference.
1090 * Care should be taken to ensure that the user has at least one malloc
1091 * reference to @mstb. If you already have a topology reference to @mstb, you
1092 * should use drm_dp_mst_topology_get_mstb() instead.
1095 * drm_dp_mst_topology_get_mstb()
1096 * drm_dp_mst_topology_put_mstb()
1099 * * 1: A topology reference was grabbed successfully
1100 * * 0: @port is no longer in the topology, no reference was grabbed
1102 static int __must_check
1103 drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1105 int ret = kref_get_unless_zero(&mstb->topology_kref);
1108 DRM_DEBUG("mstb %p (%d)\n", mstb,
1109 kref_read(&mstb->topology_kref));
1115 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1117 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1119 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1120 * not it's already reached 0. This is only valid to use in scenarios where
1121 * you are already guaranteed to have at least one active topology reference
1122 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1125 * drm_dp_mst_topology_try_get_mstb()
1126 * drm_dp_mst_topology_put_mstb()
1128 static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1130 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1131 kref_get(&mstb->topology_kref);
1132 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1136 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1138 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1140 * Releases a topology reference from @mstb by decrementing
1141 * &drm_dp_mst_branch.topology_kref.
1144 * drm_dp_mst_topology_try_get_mstb()
1145 * drm_dp_mst_topology_get_mstb()
1148 drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1150 DRM_DEBUG("mstb %p (%d)\n",
1151 mstb, kref_read(&mstb->topology_kref) - 1);
1152 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1155 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
1157 struct drm_dp_mst_branch *mstb;
1160 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1161 case DP_PEER_DEVICE_SST_SINK:
1162 /* remove i2c over sideband */
1163 drm_dp_mst_unregister_i2c_bus(&port->aux);
1165 case DP_PEER_DEVICE_MST_BRANCHING:
1168 drm_dp_mst_topology_put_mstb(mstb);
1173 static void drm_dp_destroy_port(struct kref *kref)
1175 struct drm_dp_mst_port *port =
1176 container_of(kref, struct drm_dp_mst_port, topology_kref);
1177 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1180 kfree(port->cached_edid);
1183 * The only time we don't have a connector
1184 * on an output port is if the connector init
1187 if (port->connector) {
1188 /* we can't destroy the connector here, as
1189 * we might be holding the mode_config.mutex
1190 * from an EDID retrieval */
1192 mutex_lock(&mgr->destroy_connector_lock);
1193 list_add(&port->next, &mgr->destroy_connector_list);
1194 mutex_unlock(&mgr->destroy_connector_lock);
1195 schedule_work(&mgr->destroy_connector_work);
1198 /* no need to clean up vcpi
1199 * as if we have no connector we never setup a vcpi */
1200 drm_dp_port_teardown_pdt(port, port->pdt);
1201 port->pdt = DP_PEER_DEVICE_NONE;
1203 drm_dp_mst_put_port_malloc(port);
1207 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1208 * port unless its zero
1209 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1211 * Attempts to grab a topology reference to @port, if it hasn't yet been
1212 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1213 * 0). Holding a topology reference implies that a malloc reference will be
1214 * held to @port as long as the user holds the topology reference.
1216 * Care should be taken to ensure that the user has at least one malloc
1217 * reference to @port. If you already have a topology reference to @port, you
1218 * should use drm_dp_mst_topology_get_port() instead.
1221 * drm_dp_mst_topology_get_port()
1222 * drm_dp_mst_topology_put_port()
1225 * * 1: A topology reference was grabbed successfully
1226 * * 0: @port is no longer in the topology, no reference was grabbed
1228 static int __must_check
1229 drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1231 int ret = kref_get_unless_zero(&port->topology_kref);
1234 DRM_DEBUG("port %p (%d)\n", port,
1235 kref_read(&port->topology_kref));
1241 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1242 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1244 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1245 * not it's already reached 0. This is only valid to use in scenarios where
1246 * you are already guaranteed to have at least one active topology reference
1247 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1250 * drm_dp_mst_topology_try_get_port()
1251 * drm_dp_mst_topology_put_port()
1253 static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1255 WARN_ON(kref_read(&port->topology_kref) == 0);
1256 kref_get(&port->topology_kref);
1257 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1261 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1262 * @port: The &struct drm_dp_mst_port to release the topology reference from
1264 * Releases a topology reference from @port by decrementing
1265 * &drm_dp_mst_port.topology_kref.
1268 * drm_dp_mst_topology_try_get_port()
1269 * drm_dp_mst_topology_get_port()
1271 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1273 DRM_DEBUG("port %p (%d)\n",
1274 port, kref_read(&port->topology_kref) - 1);
1275 kref_put(&port->topology_kref, drm_dp_destroy_port);
1278 static struct drm_dp_mst_branch *
1279 drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1280 struct drm_dp_mst_branch *to_find)
1282 struct drm_dp_mst_port *port;
1283 struct drm_dp_mst_branch *rmstb;
1285 if (to_find == mstb)
1288 list_for_each_entry(port, &mstb->ports, next) {
1290 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1291 port->mstb, to_find);
1299 static struct drm_dp_mst_branch *
1300 drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1301 struct drm_dp_mst_branch *mstb)
1303 struct drm_dp_mst_branch *rmstb = NULL;
1305 mutex_lock(&mgr->lock);
1306 if (mgr->mst_primary) {
1307 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1308 mgr->mst_primary, mstb);
1310 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1313 mutex_unlock(&mgr->lock);
1317 static struct drm_dp_mst_port *
1318 drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1319 struct drm_dp_mst_port *to_find)
1321 struct drm_dp_mst_port *port, *mport;
1323 list_for_each_entry(port, &mstb->ports, next) {
1324 if (port == to_find)
1328 mport = drm_dp_mst_topology_get_port_validated_locked(
1329 port->mstb, to_find);
1337 static struct drm_dp_mst_port *
1338 drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1339 struct drm_dp_mst_port *port)
1341 struct drm_dp_mst_port *rport = NULL;
1343 mutex_lock(&mgr->lock);
1344 if (mgr->mst_primary) {
1345 rport = drm_dp_mst_topology_get_port_validated_locked(
1346 mgr->mst_primary, port);
1348 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1351 mutex_unlock(&mgr->lock);
1355 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1357 struct drm_dp_mst_port *port;
1360 list_for_each_entry(port, &mstb->ports, next) {
1361 if (port->port_num == port_num) {
1362 ret = drm_dp_mst_topology_try_get_port(port);
1363 return ret ? port : NULL;
1371 * calculate a new RAD for this MST branch device
1372 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1373 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1375 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1378 int parent_lct = port->parent->lct;
1380 int idx = (parent_lct - 1) / 2;
1381 if (parent_lct > 1) {
1382 memcpy(rad, port->parent->rad, idx + 1);
1383 shift = (parent_lct % 2) ? 4 : 0;
1387 rad[idx] |= port->port_num << shift;
1388 return parent_lct + 1;
1392 * return sends link address for new mstb
1394 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1398 bool send_link = false;
1399 switch (port->pdt) {
1400 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1401 case DP_PEER_DEVICE_SST_SINK:
1402 /* add i2c over sideband */
1403 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1405 case DP_PEER_DEVICE_MST_BRANCHING:
1406 lct = drm_dp_calculate_rad(port, rad);
1408 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1410 port->mstb->mgr = port->mgr;
1411 port->mstb->port_parent = port;
1413 * Make sure this port's memory allocation stays
1414 * around until it's child MSTB releases it
1416 drm_dp_mst_get_port_malloc(port);
1425 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1429 memcpy(mstb->guid, guid, 16);
1431 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1432 if (mstb->port_parent) {
1433 ret = drm_dp_send_dpcd_write(
1441 ret = drm_dp_dpcd_write(
1450 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1453 size_t proppath_size)
1457 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1458 for (i = 0; i < (mstb->lct - 1); i++) {
1459 int shift = (i % 2) ? 0 : 4;
1460 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1461 snprintf(temp, sizeof(temp), "-%d", port_num);
1462 strlcat(proppath, temp, proppath_size);
1464 snprintf(temp, sizeof(temp), "-%d", pnum);
1465 strlcat(proppath, temp, proppath_size);
1468 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1469 struct drm_device *dev,
1470 struct drm_dp_link_addr_reply_port *port_msg)
1472 struct drm_dp_mst_port *port;
1474 bool created = false;
1478 port = drm_dp_get_port(mstb, port_msg->port_number);
1480 port = kzalloc(sizeof(*port), GFP_KERNEL);
1483 kref_init(&port->topology_kref);
1484 kref_init(&port->malloc_kref);
1485 port->parent = mstb;
1486 port->port_num = port_msg->port_number;
1487 port->mgr = mstb->mgr;
1488 port->aux.name = "DPMST";
1489 port->aux.dev = dev->dev;
1492 * Make sure the memory allocation for our parent branch stays
1493 * around until our own memory allocation is released
1495 drm_dp_mst_get_mstb_malloc(mstb);
1499 old_pdt = port->pdt;
1500 old_ddps = port->ddps;
1503 port->pdt = port_msg->peer_device_type;
1504 port->input = port_msg->input_port;
1505 port->mcs = port_msg->mcs;
1506 port->ddps = port_msg->ddps;
1507 port->ldps = port_msg->legacy_device_plug_status;
1508 port->dpcd_rev = port_msg->dpcd_revision;
1509 port->num_sdp_streams = port_msg->num_sdp_streams;
1510 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1512 /* manage mstb port lists with mgr lock - take a reference
1515 mutex_lock(&mstb->mgr->lock);
1516 drm_dp_mst_topology_get_port(port);
1517 list_add(&port->next, &mstb->ports);
1518 mutex_unlock(&mstb->mgr->lock);
1521 if (old_ddps != port->ddps) {
1524 drm_dp_send_enum_path_resources(mstb->mgr,
1528 port->available_pbn = 0;
1532 if (old_pdt != port->pdt && !port->input) {
1533 drm_dp_port_teardown_pdt(port, old_pdt);
1535 ret = drm_dp_port_setup_pdt(port);
1537 drm_dp_send_link_address(mstb->mgr, port->mstb);
1540 if (created && !port->input) {
1543 build_mst_prop_path(mstb, port->port_num, proppath,
1545 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
1548 if (!port->connector) {
1549 /* remove it from the port list */
1550 mutex_lock(&mstb->mgr->lock);
1551 list_del(&port->next);
1552 mutex_unlock(&mstb->mgr->lock);
1553 /* drop port list reference */
1554 drm_dp_mst_topology_put_port(port);
1557 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1558 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1559 port->port_num >= DP_MST_LOGICAL_PORT_0) {
1560 port->cached_edid = drm_get_edid(port->connector,
1562 drm_connector_set_tile_property(port->connector);
1564 (*mstb->mgr->cbs->register_connector)(port->connector);
1568 /* put reference to this port */
1569 drm_dp_mst_topology_put_port(port);
1572 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1573 struct drm_dp_connection_status_notify *conn_stat)
1575 struct drm_dp_mst_port *port;
1578 bool dowork = false;
1579 port = drm_dp_get_port(mstb, conn_stat->port_number);
1583 old_ddps = port->ddps;
1584 old_pdt = port->pdt;
1585 port->pdt = conn_stat->peer_device_type;
1586 port->mcs = conn_stat->message_capability_status;
1587 port->ldps = conn_stat->legacy_device_plug_status;
1588 port->ddps = conn_stat->displayport_device_plug_status;
1590 if (old_ddps != port->ddps) {
1594 port->available_pbn = 0;
1597 if (old_pdt != port->pdt && !port->input) {
1598 drm_dp_port_teardown_pdt(port, old_pdt);
1600 if (drm_dp_port_setup_pdt(port))
1604 drm_dp_mst_topology_put_port(port);
1606 queue_work(system_long_wq, &mstb->mgr->work);
1610 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1613 struct drm_dp_mst_branch *mstb;
1614 struct drm_dp_mst_port *port;
1616 /* find the port by iterating down */
1618 mutex_lock(&mgr->lock);
1619 mstb = mgr->mst_primary;
1624 for (i = 0; i < lct - 1; i++) {
1625 int shift = (i % 2) ? 0 : 4;
1626 int port_num = (rad[i / 2] >> shift) & 0xf;
1628 list_for_each_entry(port, &mstb->ports, next) {
1629 if (port->port_num == port_num) {
1632 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1640 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1644 mutex_unlock(&mgr->lock);
1648 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1649 struct drm_dp_mst_branch *mstb,
1652 struct drm_dp_mst_branch *found_mstb;
1653 struct drm_dp_mst_port *port;
1655 if (memcmp(mstb->guid, guid, 16) == 0)
1659 list_for_each_entry(port, &mstb->ports, next) {
1663 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1672 static struct drm_dp_mst_branch *
1673 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
1676 struct drm_dp_mst_branch *mstb;
1679 /* find the port by iterating down */
1680 mutex_lock(&mgr->lock);
1682 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1684 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1689 mutex_unlock(&mgr->lock);
1693 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1694 struct drm_dp_mst_branch *mstb)
1696 struct drm_dp_mst_port *port;
1697 struct drm_dp_mst_branch *mstb_child;
1698 if (!mstb->link_address_sent)
1699 drm_dp_send_link_address(mgr, mstb);
1701 list_for_each_entry(port, &mstb->ports, next) {
1708 if (!port->available_pbn)
1709 drm_dp_send_enum_path_resources(mgr, mstb, port);
1712 mstb_child = drm_dp_mst_topology_get_mstb_validated(
1715 drm_dp_check_and_send_link_address(mgr, mstb_child);
1716 drm_dp_mst_topology_put_mstb(mstb_child);
1722 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1724 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1725 struct drm_dp_mst_branch *mstb;
1728 mutex_lock(&mgr->lock);
1729 mstb = mgr->mst_primary;
1731 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1735 mutex_unlock(&mgr->lock);
1737 drm_dp_check_and_send_link_address(mgr, mstb);
1738 drm_dp_mst_topology_put_mstb(mstb);
1742 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1747 if (memchr_inv(guid, 0, 16))
1750 salt = get_jiffies_64();
1752 memcpy(&guid[0], &salt, sizeof(u64));
1753 memcpy(&guid[8], &salt, sizeof(u64));
1759 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1761 struct drm_dp_sideband_msg_req_body req;
1763 req.req_type = DP_REMOTE_DPCD_READ;
1764 req.u.dpcd_read.port_number = port_num;
1765 req.u.dpcd_read.dpcd_address = offset;
1766 req.u.dpcd_read.num_bytes = num_bytes;
1767 drm_dp_encode_sideband_req(&req, msg);
1773 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1774 bool up, u8 *msg, int len)
1777 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1778 int tosend, total, offset;
1785 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1787 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1790 if (ret != tosend) {
1791 if (ret == -EIO && retries < 5) {
1795 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1801 } while (total > 0);
1805 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1806 struct drm_dp_sideband_msg_tx *txmsg)
1808 struct drm_dp_mst_branch *mstb = txmsg->dst;
1811 /* both msg slots are full */
1812 if (txmsg->seqno == -1) {
1813 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1814 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1817 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1818 txmsg->seqno = mstb->last_seqno;
1819 mstb->last_seqno ^= 1;
1820 } else if (mstb->tx_slots[0] == NULL)
1824 mstb->tx_slots[txmsg->seqno] = txmsg;
1827 req_type = txmsg->msg[0] & 0x7f;
1828 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1829 req_type == DP_RESOURCE_STATUS_NOTIFY)
1833 hdr->path_msg = txmsg->path_msg;
1834 hdr->lct = mstb->lct;
1835 hdr->lcr = mstb->lct - 1;
1837 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1838 hdr->seqno = txmsg->seqno;
1842 * process a single block of the next message in the sideband queue
1844 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1845 struct drm_dp_sideband_msg_tx *txmsg,
1849 struct drm_dp_sideband_msg_hdr hdr;
1850 int len, space, idx, tosend;
1853 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1855 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1857 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1860 /* make hdr from dst mst - for replies use seqno
1861 otherwise assign one */
1862 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1866 /* amount left to send in this message */
1867 len = txmsg->cur_len - txmsg->cur_offset;
1869 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1870 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1872 tosend = min(len, space);
1873 if (len == txmsg->cur_len)
1879 hdr.msg_len = tosend + 1;
1880 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1881 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1882 /* add crc at end */
1883 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1886 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1888 DRM_DEBUG_KMS("sideband msg failed to send\n");
1892 txmsg->cur_offset += tosend;
1893 if (txmsg->cur_offset == txmsg->cur_len) {
1894 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1900 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1902 struct drm_dp_sideband_msg_tx *txmsg;
1905 WARN_ON(!mutex_is_locked(&mgr->qlock));
1907 /* construct a chunk from the first msg in the tx_msg queue */
1908 if (list_empty(&mgr->tx_msg_downq))
1911 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1912 ret = process_single_tx_qlock(mgr, txmsg, false);
1914 /* txmsg is sent it should be in the slots now */
1915 list_del(&txmsg->next);
1917 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1918 list_del(&txmsg->next);
1919 if (txmsg->seqno != -1)
1920 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1921 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1922 wake_up_all(&mgr->tx_waitq);
1926 /* called holding qlock */
1927 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1928 struct drm_dp_sideband_msg_tx *txmsg)
1932 /* construct a chunk from the first msg in the tx_msg queue */
1933 ret = process_single_tx_qlock(mgr, txmsg, true);
1936 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1938 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1941 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1942 struct drm_dp_sideband_msg_tx *txmsg)
1944 mutex_lock(&mgr->qlock);
1945 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1946 if (list_is_singular(&mgr->tx_msg_downq))
1947 process_single_down_tx_qlock(mgr);
1948 mutex_unlock(&mgr->qlock);
1951 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1952 struct drm_dp_mst_branch *mstb)
1955 struct drm_dp_sideband_msg_tx *txmsg;
1958 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1963 len = build_link_address(txmsg);
1965 mstb->link_address_sent = true;
1966 drm_dp_queue_down_tx(mgr, txmsg);
1968 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1972 if (txmsg->reply.reply_type == 1)
1973 DRM_DEBUG_KMS("link address nak received\n");
1975 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1976 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1977 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n", i,
1978 txmsg->reply.u.link_addr.ports[i].input_port,
1979 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1980 txmsg->reply.u.link_addr.ports[i].port_number,
1981 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1982 txmsg->reply.u.link_addr.ports[i].mcs,
1983 txmsg->reply.u.link_addr.ports[i].ddps,
1984 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1985 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1986 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1989 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1991 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1992 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1994 drm_kms_helper_hotplug_event(mgr->dev);
1997 mstb->link_address_sent = false;
1998 DRM_DEBUG_KMS("link address failed %d\n", ret);
2004 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2005 struct drm_dp_mst_branch *mstb,
2006 struct drm_dp_mst_port *port)
2009 struct drm_dp_sideband_msg_tx *txmsg;
2012 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2017 len = build_enum_path_resources(txmsg, port->port_num);
2019 drm_dp_queue_down_tx(mgr, txmsg);
2021 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2023 if (txmsg->reply.reply_type == 1)
2024 DRM_DEBUG_KMS("enum path resources nak received\n");
2026 if (port->port_num != txmsg->reply.u.path_resources.port_number)
2027 DRM_ERROR("got incorrect port in response\n");
2028 DRM_DEBUG_KMS("enum path resources %d: %d %d\n", txmsg->reply.u.path_resources.port_number, txmsg->reply.u.path_resources.full_payload_bw_number,
2029 txmsg->reply.u.path_resources.avail_payload_bw_number);
2030 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
2038 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2040 if (!mstb->port_parent)
2043 if (mstb->port_parent->mstb != mstb)
2044 return mstb->port_parent;
2046 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2050 * Searches upwards in the topology starting from mstb to try to find the
2051 * closest available parent of mstb that's still connected to the rest of the
2052 * topology. This can be used in order to perform operations like releasing
2053 * payloads, where the branch device which owned the payload may no longer be
2054 * around and thus would require that the payload on the last living relative
2057 static struct drm_dp_mst_branch *
2058 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2059 struct drm_dp_mst_branch *mstb,
2062 struct drm_dp_mst_branch *rmstb = NULL;
2063 struct drm_dp_mst_port *found_port;
2065 mutex_lock(&mgr->lock);
2066 if (!mgr->mst_primary)
2070 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
2074 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
2075 rmstb = found_port->parent;
2076 *port_num = found_port->port_num;
2078 /* Search again, starting from this parent */
2079 mstb = found_port->parent;
2083 mutex_unlock(&mgr->lock);
2087 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
2088 struct drm_dp_mst_port *port,
2092 struct drm_dp_sideband_msg_tx *txmsg;
2093 struct drm_dp_mst_branch *mstb;
2094 int len, ret, port_num;
2095 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
2098 port_num = port->port_num;
2099 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2101 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
2109 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2115 for (i = 0; i < port->num_sdp_streams; i++)
2119 len = build_allocate_payload(txmsg, port_num,
2121 pbn, port->num_sdp_streams, sinks);
2123 drm_dp_queue_down_tx(mgr, txmsg);
2126 * FIXME: there is a small chance that between getting the last
2127 * connected mstb and sending the payload message, the last connected
2128 * mstb could also be removed from the topology. In the future, this
2129 * needs to be fixed by restarting the
2130 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
2131 * timeout if the topology is still connected to the system.
2133 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2135 if (txmsg->reply.reply_type == 1)
2142 drm_dp_mst_topology_put_mstb(mstb);
2146 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
2147 struct drm_dp_mst_port *port, bool power_up)
2149 struct drm_dp_sideband_msg_tx *txmsg;
2152 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2156 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2158 drm_dp_mst_topology_put_port(port);
2162 txmsg->dst = port->parent;
2163 len = build_power_updown_phy(txmsg, port->port_num, power_up);
2164 drm_dp_queue_down_tx(mgr, txmsg);
2166 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
2168 if (txmsg->reply.reply_type == 1)
2174 drm_dp_mst_topology_put_port(port);
2178 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
2180 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2182 struct drm_dp_payload *payload)
2186 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
2188 payload->payload_state = 0;
2191 payload->payload_state = DP_PAYLOAD_LOCAL;
2195 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2196 struct drm_dp_mst_port *port,
2198 struct drm_dp_payload *payload)
2201 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
2204 payload->payload_state = DP_PAYLOAD_REMOTE;
2208 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2209 struct drm_dp_mst_port *port,
2211 struct drm_dp_payload *payload)
2213 DRM_DEBUG_KMS("\n");
2214 /* its okay for these to fail */
2216 drm_dp_payload_send_msg(mgr, port, id, 0);
2219 drm_dp_dpcd_write_payload(mgr, id, payload);
2220 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
2224 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2226 struct drm_dp_payload *payload)
2228 payload->payload_state = 0;
2233 * drm_dp_update_payload_part1() - Execute payload update part 1
2234 * @mgr: manager to use.
2236 * This iterates over all proposed virtual channels, and tries to
2237 * allocate space in the link for them. For 0->slots transitions,
2238 * this step just writes the VCPI to the MST device. For slots->0
2239 * transitions, this writes the updated VCPIs and removes the
2240 * remote VC payloads.
2242 * after calling this the driver should generate ACT and payload
2245 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
2247 struct drm_dp_payload req_payload;
2248 struct drm_dp_mst_port *port;
2252 mutex_lock(&mgr->payload_lock);
2253 for (i = 0; i < mgr->max_payloads; i++) {
2254 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2255 struct drm_dp_payload *payload = &mgr->payloads[i];
2256 bool put_port = false;
2258 /* solve the current payloads - compare to the hw ones
2259 - update the hw view */
2260 req_payload.start_slot = cur_slots;
2262 port = container_of(vcpi, struct drm_dp_mst_port,
2265 /* Validated ports don't matter if we're releasing
2268 if (vcpi->num_slots) {
2269 port = drm_dp_mst_topology_get_port_validated(
2272 mutex_unlock(&mgr->payload_lock);
2278 req_payload.num_slots = vcpi->num_slots;
2279 req_payload.vcpi = vcpi->vcpi;
2282 req_payload.num_slots = 0;
2285 payload->start_slot = req_payload.start_slot;
2286 /* work out what is required to happen with this payload */
2287 if (payload->num_slots != req_payload.num_slots) {
2289 /* need to push an update for this payload */
2290 if (req_payload.num_slots) {
2291 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
2293 payload->num_slots = req_payload.num_slots;
2294 payload->vcpi = req_payload.vcpi;
2296 } else if (payload->num_slots) {
2297 payload->num_slots = 0;
2298 drm_dp_destroy_payload_step1(mgr, port,
2301 req_payload.payload_state =
2302 payload->payload_state;
2303 payload->start_slot = 0;
2305 payload->payload_state = req_payload.payload_state;
2307 cur_slots += req_payload.num_slots;
2310 drm_dp_mst_topology_put_port(port);
2313 for (i = 0; i < mgr->max_payloads; i++) {
2314 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL)
2317 DRM_DEBUG_KMS("removing payload %d\n", i);
2318 for (j = i; j < mgr->max_payloads - 1; j++) {
2319 mgr->payloads[j] = mgr->payloads[j + 1];
2320 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
2322 if (mgr->proposed_vcpis[j] &&
2323 mgr->proposed_vcpis[j]->num_slots) {
2324 set_bit(j + 1, &mgr->payload_mask);
2326 clear_bit(j + 1, &mgr->payload_mask);
2330 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
2331 sizeof(struct drm_dp_payload));
2332 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
2333 clear_bit(mgr->max_payloads, &mgr->payload_mask);
2335 mutex_unlock(&mgr->payload_lock);
2339 EXPORT_SYMBOL(drm_dp_update_payload_part1);
2342 * drm_dp_update_payload_part2() - Execute payload update part 2
2343 * @mgr: manager to use.
2345 * This iterates over all proposed virtual channels, and tries to
2346 * allocate space in the link for them. For 0->slots transitions,
2347 * this step writes the remote VC payload commands. For slots->0
2348 * this just resets some internal state.
2350 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
2352 struct drm_dp_mst_port *port;
2355 mutex_lock(&mgr->payload_lock);
2356 for (i = 0; i < mgr->max_payloads; i++) {
2358 if (!mgr->proposed_vcpis[i])
2361 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2363 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2364 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
2365 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2366 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
2367 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2370 mutex_unlock(&mgr->payload_lock);
2374 mutex_unlock(&mgr->payload_lock);
2377 EXPORT_SYMBOL(drm_dp_update_payload_part2);
2379 #if 0 /* unused as of yet */
2380 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2381 struct drm_dp_mst_port *port,
2382 int offset, int size)
2385 struct drm_dp_sideband_msg_tx *txmsg;
2387 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2391 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
2392 txmsg->dst = port->parent;
2394 drm_dp_queue_down_tx(mgr, txmsg);
2400 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2401 struct drm_dp_mst_port *port,
2402 int offset, int size, u8 *bytes)
2406 struct drm_dp_sideband_msg_tx *txmsg;
2407 struct drm_dp_mst_branch *mstb;
2409 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2413 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2419 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2422 drm_dp_queue_down_tx(mgr, txmsg);
2424 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2426 if (txmsg->reply.reply_type == 1) {
2433 drm_dp_mst_topology_put_mstb(mstb);
2437 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2439 struct drm_dp_sideband_msg_reply_body reply;
2441 reply.reply_type = 0;
2442 reply.req_type = req_type;
2443 drm_dp_encode_sideband_reply(&reply, msg);
2447 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2448 struct drm_dp_mst_branch *mstb,
2449 int req_type, int seqno, bool broadcast)
2451 struct drm_dp_sideband_msg_tx *txmsg;
2453 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2458 txmsg->seqno = seqno;
2459 drm_dp_encode_up_ack_reply(txmsg, req_type);
2461 mutex_lock(&mgr->qlock);
2463 process_single_up_tx_qlock(mgr, txmsg);
2465 mutex_unlock(&mgr->qlock);
2471 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2475 switch (dp_link_bw) {
2477 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2478 dp_link_bw, dp_link_count);
2481 case DP_LINK_BW_1_62:
2482 *out = 3 * dp_link_count;
2484 case DP_LINK_BW_2_7:
2485 *out = 5 * dp_link_count;
2487 case DP_LINK_BW_5_4:
2488 *out = 10 * dp_link_count;
2490 case DP_LINK_BW_8_1:
2491 *out = 15 * dp_link_count;
2498 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2499 * @mgr: manager to set state for
2500 * @mst_state: true to enable MST on this connector - false to disable.
2502 * This is called by the driver when it detects an MST capable device plugged
2503 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2505 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2508 struct drm_dp_mst_branch *mstb = NULL;
2510 mutex_lock(&mgr->lock);
2511 if (mst_state == mgr->mst_state)
2514 mgr->mst_state = mst_state;
2515 /* set the device into MST mode */
2517 WARN_ON(mgr->mst_primary);
2520 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2521 if (ret != DP_RECEIVER_CAP_SIZE) {
2522 DRM_DEBUG_KMS("failed to read DPCD\n");
2526 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2527 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2533 /* add initial branch device at LCT 1 */
2534 mstb = drm_dp_add_mst_branch_device(1, NULL);
2541 /* give this the main reference */
2542 mgr->mst_primary = mstb;
2543 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
2545 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2546 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2552 struct drm_dp_payload reset_pay;
2553 reset_pay.start_slot = 0;
2554 reset_pay.num_slots = 0x3f;
2555 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2558 queue_work(system_long_wq, &mgr->work);
2562 /* disable MST on the device */
2563 mstb = mgr->mst_primary;
2564 mgr->mst_primary = NULL;
2565 /* this can fail if the device is gone */
2566 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2568 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2569 mgr->payload_mask = 0;
2570 set_bit(0, &mgr->payload_mask);
2575 mutex_unlock(&mgr->lock);
2577 drm_dp_mst_topology_put_mstb(mstb);
2581 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2584 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2585 * @mgr: manager to suspend
2587 * This function tells the MST device that we can't handle UP messages
2588 * anymore. This should stop it from sending any since we are suspended.
2590 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2592 mutex_lock(&mgr->lock);
2593 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2594 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2595 mutex_unlock(&mgr->lock);
2596 flush_work(&mgr->work);
2597 flush_work(&mgr->destroy_connector_work);
2599 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2602 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2603 * @mgr: manager to resume
2605 * This will fetch DPCD and see if the device is still there,
2606 * if it is, it will rewrite the MSTM control bits, and return.
2608 * if the device fails this returns -1, and the driver should do
2609 * a full MST reprobe, in case we were undocked.
2611 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2615 mutex_lock(&mgr->lock);
2617 if (mgr->mst_primary) {
2621 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2622 if (sret != DP_RECEIVER_CAP_SIZE) {
2623 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2628 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2629 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2631 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2636 /* Some hubs forget their guids after they resume */
2637 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2639 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2643 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2650 mutex_unlock(&mgr->lock);
2653 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2655 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2659 int replylen, origlen, curreply;
2661 struct drm_dp_sideband_msg_rx *msg;
2662 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2663 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2665 len = min(mgr->max_dpcd_transaction_bytes, 16);
2666 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2669 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2672 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2674 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2677 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2682 while (replylen > 0) {
2683 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2684 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2687 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2692 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2694 DRM_DEBUG_KMS("failed to build sideband msg\n");
2704 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2708 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2709 memset(&mgr->down_rep_recv, 0,
2710 sizeof(struct drm_dp_sideband_msg_rx));
2714 if (mgr->down_rep_recv.have_eomt) {
2715 struct drm_dp_sideband_msg_tx *txmsg;
2716 struct drm_dp_mst_branch *mstb;
2718 mstb = drm_dp_get_mst_branch_device(mgr,
2719 mgr->down_rep_recv.initial_hdr.lct,
2720 mgr->down_rep_recv.initial_hdr.rad);
2723 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2724 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2728 /* find the message */
2729 slot = mgr->down_rep_recv.initial_hdr.seqno;
2730 mutex_lock(&mgr->qlock);
2731 txmsg = mstb->tx_slots[slot];
2732 /* remove from slots */
2733 mutex_unlock(&mgr->qlock);
2736 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2738 mgr->down_rep_recv.initial_hdr.seqno,
2739 mgr->down_rep_recv.initial_hdr.lct,
2740 mgr->down_rep_recv.initial_hdr.rad[0],
2741 mgr->down_rep_recv.msg[0]);
2742 drm_dp_mst_topology_put_mstb(mstb);
2743 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2747 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2748 if (txmsg->reply.reply_type == 1) {
2749 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x, reason 0x%02x, nak data 0x%02x\n", txmsg->reply.req_type, txmsg->reply.u.nak.reason, txmsg->reply.u.nak.nak_data);
2752 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2753 drm_dp_mst_topology_put_mstb(mstb);
2755 mutex_lock(&mgr->qlock);
2756 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2757 mstb->tx_slots[slot] = NULL;
2758 mutex_unlock(&mgr->qlock);
2760 wake_up_all(&mgr->tx_waitq);
2765 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2769 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2770 memset(&mgr->up_req_recv, 0,
2771 sizeof(struct drm_dp_sideband_msg_rx));
2775 if (mgr->up_req_recv.have_eomt) {
2776 struct drm_dp_sideband_msg_req_body msg;
2777 struct drm_dp_mst_branch *mstb = NULL;
2780 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2781 mstb = drm_dp_get_mst_branch_device(mgr,
2782 mgr->up_req_recv.initial_hdr.lct,
2783 mgr->up_req_recv.initial_hdr.rad);
2785 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2786 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2791 seqno = mgr->up_req_recv.initial_hdr.seqno;
2792 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2794 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2795 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2798 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2801 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2802 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2806 drm_dp_update_port(mstb, &msg.u.conn_stat);
2808 DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n", msg.u.conn_stat.port_number, msg.u.conn_stat.legacy_device_plug_status, msg.u.conn_stat.displayport_device_plug_status, msg.u.conn_stat.message_capability_status, msg.u.conn_stat.input_port, msg.u.conn_stat.peer_device_type);
2809 drm_kms_helper_hotplug_event(mgr->dev);
2811 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2812 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2814 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2817 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2818 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2822 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2826 drm_dp_mst_topology_put_mstb(mstb);
2828 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2834 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2835 * @mgr: manager to notify irq for.
2836 * @esi: 4 bytes from SINK_COUNT_ESI
2837 * @handled: whether the hpd interrupt was consumed or not
2839 * This should be called from the driver when it detects a short IRQ,
2840 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2841 * topology manager will process the sideband messages received as a result
2844 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2851 if (sc != mgr->sink_count) {
2852 mgr->sink_count = sc;
2856 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2857 ret = drm_dp_mst_handle_down_rep(mgr);
2861 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2862 ret |= drm_dp_mst_handle_up_req(mgr);
2866 drm_dp_mst_kick_tx(mgr);
2869 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2872 * drm_dp_mst_detect_port() - get connection status for an MST port
2873 * @connector: DRM connector for this port
2874 * @mgr: manager for this port
2875 * @port: unverified pointer to a port
2877 * This returns the current connection state for a port. It validates the
2878 * port pointer still exists so the caller doesn't require a reference
2880 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2881 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2883 enum drm_connector_status status = connector_status_disconnected;
2885 /* we need to search for the port in the mgr in case its gone */
2886 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2888 return connector_status_disconnected;
2893 switch (port->pdt) {
2894 case DP_PEER_DEVICE_NONE:
2895 case DP_PEER_DEVICE_MST_BRANCHING:
2898 case DP_PEER_DEVICE_SST_SINK:
2899 status = connector_status_connected;
2900 /* for logical ports - cache the EDID */
2901 if (port->port_num >= 8 && !port->cached_edid) {
2902 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2905 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2907 status = connector_status_connected;
2911 drm_dp_mst_topology_put_port(port);
2914 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2917 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2918 * @mgr: manager for this port
2919 * @port: unverified pointer to a port.
2921 * This returns whether the port supports audio or not.
2923 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2924 struct drm_dp_mst_port *port)
2928 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2931 ret = port->has_audio;
2932 drm_dp_mst_topology_put_port(port);
2935 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2938 * drm_dp_mst_get_edid() - get EDID for an MST port
2939 * @connector: toplevel connector to get EDID for
2940 * @mgr: manager for this port
2941 * @port: unverified pointer to a port.
2943 * This returns an EDID for the port connected to a connector,
2944 * It validates the pointer still exists so the caller doesn't require a
2947 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2949 struct edid *edid = NULL;
2951 /* we need to search for the port in the mgr in case its gone */
2952 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2956 if (port->cached_edid)
2957 edid = drm_edid_duplicate(port->cached_edid);
2959 edid = drm_get_edid(connector, &port->aux.ddc);
2960 drm_connector_set_tile_property(connector);
2962 port->has_audio = drm_detect_monitor_audio(edid);
2963 drm_dp_mst_topology_put_port(port);
2966 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2969 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
2970 * @mgr: manager to use
2971 * @pbn: payload bandwidth to convert into slots.
2973 * Calculate the number of VCPI slots that will be required for the given PBN
2974 * value. This function is deprecated, and should not be used in atomic
2978 * The total slots required for this port, or error.
2980 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2985 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2987 /* max. time slots - one slot for MTP header */
2992 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2994 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2995 struct drm_dp_vcpi *vcpi, int pbn, int slots)
2999 /* max. time slots - one slot for MTP header */
3004 vcpi->aligned_pbn = slots * mgr->pbn_div;
3005 vcpi->num_slots = slots;
3007 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
3014 * drm_dp_atomic_find_vcpi_slots() - Find and add vcpi slots to the state
3015 * @state: global atomic state
3016 * @mgr: MST topology manager for the port
3017 * @port: port to find vcpi slots for
3018 * @pbn: bandwidth required for the mode in PBN
3021 * Total slots in the atomic state assigned for this port or error
3023 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
3024 struct drm_dp_mst_topology_mgr *mgr,
3025 struct drm_dp_mst_port *port, int pbn)
3027 struct drm_dp_mst_topology_state *topology_state;
3030 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3031 if (IS_ERR(topology_state))
3032 return PTR_ERR(topology_state);
3034 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3037 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3038 DRM_DEBUG_KMS("vcpi slots req=%d, avail=%d\n",
3039 req_slots, topology_state->avail_slots);
3041 if (req_slots > topology_state->avail_slots) {
3042 drm_dp_mst_topology_put_port(port);
3046 topology_state->avail_slots -= req_slots;
3047 DRM_DEBUG_KMS("vcpi slots avail=%d", topology_state->avail_slots);
3049 drm_dp_mst_topology_put_port(port);
3052 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
3055 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
3056 * @state: global atomic state
3057 * @mgr: MST topology manager for the port
3058 * @slots: number of vcpi slots to release
3061 * 0 if @slots were added back to &drm_dp_mst_topology_state->avail_slots or
3062 * negative error code
3064 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
3065 struct drm_dp_mst_topology_mgr *mgr,
3068 struct drm_dp_mst_topology_state *topology_state;
3070 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3071 if (IS_ERR(topology_state))
3072 return PTR_ERR(topology_state);
3074 /* We cannot rely on port->vcpi.num_slots to update
3075 * topology_state->avail_slots as the port may not exist if the parent
3076 * branch device was unplugged. This should be fixed by tracking
3077 * per-port slot allocation in drm_dp_mst_topology_state instead of
3078 * depending on the caller to tell us how many slots to release.
3080 topology_state->avail_slots += slots;
3081 DRM_DEBUG_KMS("vcpi slots released=%d, avail=%d\n",
3082 slots, topology_state->avail_slots);
3086 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
3089 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
3090 * @mgr: manager for this port
3091 * @port: port to allocate a virtual channel for.
3092 * @pbn: payload bandwidth number to request
3093 * @slots: returned number of slots for this PBN.
3095 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3096 struct drm_dp_mst_port *port, int pbn, int slots)
3100 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3107 if (port->vcpi.vcpi > 0) {
3108 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
3109 port->vcpi.vcpi, port->vcpi.pbn, pbn);
3110 if (pbn == port->vcpi.pbn) {
3111 drm_dp_mst_topology_put_port(port);
3116 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
3118 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
3119 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
3122 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
3123 pbn, port->vcpi.num_slots);
3125 /* Keep port allocated until it's payload has been removed */
3126 drm_dp_mst_get_port_malloc(port);
3127 drm_dp_mst_topology_put_port(port);
3132 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
3134 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3137 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3141 slots = port->vcpi.num_slots;
3142 drm_dp_mst_topology_put_port(port);
3145 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
3148 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
3149 * @mgr: manager for this port
3150 * @port: unverified pointer to a port.
3152 * This just resets the number of slots for the ports VCPI for later programming.
3154 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3157 * A port with VCPI will remain allocated until it's VCPI is
3158 * released, no verified ref needed
3161 port->vcpi.num_slots = 0;
3163 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
3166 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
3167 * @mgr: manager for this port
3168 * @port: unverified port to deallocate vcpi for
3170 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3171 struct drm_dp_mst_port *port)
3174 * A port with VCPI will remain allocated until it's VCPI is
3175 * released, no verified ref needed
3178 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3179 port->vcpi.num_slots = 0;
3181 port->vcpi.aligned_pbn = 0;
3182 port->vcpi.vcpi = 0;
3183 drm_dp_mst_put_port_malloc(port);
3185 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
3187 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
3188 int id, struct drm_dp_payload *payload)
3190 u8 payload_alloc[3], status;
3194 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
3195 DP_PAYLOAD_TABLE_UPDATED);
3197 payload_alloc[0] = id;
3198 payload_alloc[1] = payload->start_slot;
3199 payload_alloc[2] = payload->num_slots;
3201 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
3203 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
3208 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3210 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3214 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
3217 usleep_range(10000, 20000);
3220 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
3231 * drm_dp_check_act_status() - Check ACT handled status.
3232 * @mgr: manager to use
3234 * Check the payload status bits in the DPCD for ACT handled completion.
3236 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
3243 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3246 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3250 if (status & DP_PAYLOAD_ACT_HANDLED)
3255 } while (count < 30);
3257 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
3258 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
3266 EXPORT_SYMBOL(drm_dp_check_act_status);
3269 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
3270 * @clock: dot clock for the mode
3271 * @bpp: bpp for the mode.
3273 * This uses the formula in the spec to calculate the PBN value for a mode.
3275 int drm_dp_calc_pbn_mode(int clock, int bpp)
3285 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
3286 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
3287 * common multiplier to render an integer PBN for all link rate/lane
3288 * counts combinations
3290 * peak_kbps *= (1006/1000)
3291 * peak_kbps *= (64/54)
3292 * peak_kbps *= 8 convert to bytes
3295 numerator = 64 * 1006;
3296 denominator = 54 * 8 * 1000 * 1000;
3299 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
3301 return drm_fixp2int_ceil(peak_kbps);
3303 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
3305 static int test_calc_pbn_mode(void)
3308 ret = drm_dp_calc_pbn_mode(154000, 30);
3310 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3311 154000, 30, 689, ret);
3314 ret = drm_dp_calc_pbn_mode(234000, 30);
3316 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3317 234000, 30, 1047, ret);
3320 ret = drm_dp_calc_pbn_mode(297000, 24);
3322 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3323 297000, 24, 1063, ret);
3329 /* we want to kick the TX after we've ack the up/down IRQs. */
3330 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
3332 queue_work(system_long_wq, &mgr->tx_work);
3335 static void drm_dp_mst_dump_mstb(struct seq_file *m,
3336 struct drm_dp_mst_branch *mstb)
3338 struct drm_dp_mst_port *port;
3339 int tabs = mstb->lct;
3343 for (i = 0; i < tabs; i++)
3347 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
3348 list_for_each_entry(port, &mstb->ports, next) {
3349 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);
3351 drm_dp_mst_dump_mstb(m, port->mstb);
3355 #define DP_PAYLOAD_TABLE_SIZE 64
3357 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
3362 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
3363 if (drm_dp_dpcd_read(mgr->aux,
3364 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
3371 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3372 struct drm_dp_mst_port *port, char *name,
3375 struct edid *mst_edid;
3377 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3378 drm_edid_get_monitor_name(mst_edid, name, namelen);
3382 * drm_dp_mst_dump_topology(): dump topology to seq file.
3383 * @m: seq_file to dump output to
3384 * @mgr: manager to dump current topology for.
3386 * helper to dump MST topology to a seq file for debugfs.
3388 void drm_dp_mst_dump_topology(struct seq_file *m,
3389 struct drm_dp_mst_topology_mgr *mgr)
3392 struct drm_dp_mst_port *port;
3394 mutex_lock(&mgr->lock);
3395 if (mgr->mst_primary)
3396 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
3399 mutex_unlock(&mgr->lock);
3401 mutex_lock(&mgr->payload_lock);
3402 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
3405 for (i = 0; i < mgr->max_payloads; i++) {
3406 if (mgr->proposed_vcpis[i]) {
3409 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
3410 fetch_monitor_name(mgr, port, name, sizeof(name));
3411 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3412 port->port_num, port->vcpi.vcpi,
3413 port->vcpi.num_slots,
3414 (*name != 0) ? name : "Unknown");
3416 seq_printf(m, "vcpi %d:unused\n", i);
3418 for (i = 0; i < mgr->max_payloads; i++) {
3419 seq_printf(m, "payload %d: %d, %d, %d\n",
3421 mgr->payloads[i].payload_state,
3422 mgr->payloads[i].start_slot,
3423 mgr->payloads[i].num_slots);
3427 mutex_unlock(&mgr->payload_lock);
3429 mutex_lock(&mgr->lock);
3430 if (mgr->mst_primary) {
3431 u8 buf[DP_PAYLOAD_TABLE_SIZE];
3434 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
3435 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
3436 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
3437 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
3438 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
3439 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
3441 /* dump the standard OUI branch header */
3442 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
3443 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
3444 for (i = 0x3; i < 0x8 && buf[i]; i++)
3445 seq_printf(m, "%c", buf[i]);
3446 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
3447 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
3448 if (dump_dp_payload_table(mgr, buf))
3449 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
3452 mutex_unlock(&mgr->lock);
3455 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
3457 static void drm_dp_tx_work(struct work_struct *work)
3459 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
3461 mutex_lock(&mgr->qlock);
3462 if (!list_empty(&mgr->tx_msg_downq))
3463 process_single_down_tx_qlock(mgr);
3464 mutex_unlock(&mgr->qlock);
3467 static void drm_dp_destroy_connector_work(struct work_struct *work)
3469 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
3470 struct drm_dp_mst_port *port;
3471 bool send_hotplug = false;
3473 * Not a regular list traverse as we have to drop the destroy
3474 * connector lock before destroying the connector, to avoid AB->BA
3475 * ordering between this lock and the config mutex.
3478 mutex_lock(&mgr->destroy_connector_lock);
3479 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
3481 mutex_unlock(&mgr->destroy_connector_lock);
3484 list_del(&port->next);
3485 mutex_unlock(&mgr->destroy_connector_lock);
3487 INIT_LIST_HEAD(&port->next);
3489 mgr->cbs->destroy_connector(mgr, port->connector);
3491 drm_dp_port_teardown_pdt(port, port->pdt);
3492 port->pdt = DP_PEER_DEVICE_NONE;
3494 drm_dp_mst_put_port_malloc(port);
3495 send_hotplug = true;
3498 drm_kms_helper_hotplug_event(mgr->dev);
3501 static struct drm_private_state *
3502 drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
3504 struct drm_dp_mst_topology_state *state;
3506 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
3510 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
3512 return &state->base;
3515 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
3516 struct drm_private_state *state)
3518 struct drm_dp_mst_topology_state *mst_state =
3519 to_dp_mst_topology_state(state);
3524 static const struct drm_private_state_funcs mst_state_funcs = {
3525 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
3526 .atomic_destroy_state = drm_dp_mst_destroy_state,
3530 * drm_atomic_get_mst_topology_state: get MST topology state
3532 * @state: global atomic state
3533 * @mgr: MST topology manager, also the private object in this case
3535 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3536 * state vtable so that the private object state returned is that of a MST
3537 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3538 * to care of the locking, so warn if don't hold the connection_mutex.
3542 * The MST topology state or error pointer.
3544 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
3545 struct drm_dp_mst_topology_mgr *mgr)
3547 struct drm_device *dev = mgr->dev;
3549 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3550 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
3552 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
3555 * drm_dp_mst_topology_mgr_init - initialise a topology manager
3556 * @mgr: manager struct to initialise
3557 * @dev: device providing this structure - for i2c addition.
3558 * @aux: DP helper aux channel to talk to this device
3559 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3560 * @max_payloads: maximum number of payloads this GPU can source
3561 * @conn_base_id: the connector object ID the MST device is connected to.
3563 * Return 0 for success, or negative error code on failure
3565 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
3566 struct drm_device *dev, struct drm_dp_aux *aux,
3567 int max_dpcd_transaction_bytes,
3568 int max_payloads, int conn_base_id)
3570 struct drm_dp_mst_topology_state *mst_state;
3572 mutex_init(&mgr->lock);
3573 mutex_init(&mgr->qlock);
3574 mutex_init(&mgr->payload_lock);
3575 mutex_init(&mgr->destroy_connector_lock);
3576 INIT_LIST_HEAD(&mgr->tx_msg_downq);
3577 INIT_LIST_HEAD(&mgr->destroy_connector_list);
3578 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
3579 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
3580 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
3581 init_waitqueue_head(&mgr->tx_waitq);
3584 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
3585 mgr->max_payloads = max_payloads;
3586 mgr->conn_base_id = conn_base_id;
3587 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3588 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3590 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3593 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3594 if (!mgr->proposed_vcpis)
3596 set_bit(0, &mgr->payload_mask);
3597 if (test_calc_pbn_mode() < 0)
3598 DRM_ERROR("MST PBN self-test failed\n");
3600 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
3601 if (mst_state == NULL)
3604 mst_state->mgr = mgr;
3606 /* max. time slots - one slot for MTP header */
3607 mst_state->avail_slots = 63;
3609 drm_atomic_private_obj_init(dev, &mgr->base,
3615 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3618 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3619 * @mgr: manager to destroy
3621 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3623 drm_dp_mst_topology_mgr_set_mst(mgr, false);
3624 flush_work(&mgr->work);
3625 flush_work(&mgr->destroy_connector_work);
3626 mutex_lock(&mgr->payload_lock);
3627 kfree(mgr->payloads);
3628 mgr->payloads = NULL;
3629 kfree(mgr->proposed_vcpis);
3630 mgr->proposed_vcpis = NULL;
3631 mutex_unlock(&mgr->payload_lock);
3634 drm_atomic_private_obj_fini(&mgr->base);
3637 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3639 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
3643 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
3646 for (i = 0; i < num - 1; i++) {
3647 if (msgs[i].flags & I2C_M_RD ||
3652 return msgs[num - 1].flags & I2C_M_RD &&
3653 msgs[num - 1].len <= 0xff;
3657 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3660 struct drm_dp_aux *aux = adapter->algo_data;
3661 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3662 struct drm_dp_mst_branch *mstb;
3663 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3665 struct drm_dp_sideband_msg_req_body msg;
3666 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3669 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3673 if (!remote_i2c_read_ok(msgs, num)) {
3674 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3679 memset(&msg, 0, sizeof(msg));
3680 msg.req_type = DP_REMOTE_I2C_READ;
3681 msg.u.i2c_read.num_transactions = num - 1;
3682 msg.u.i2c_read.port_number = port->port_num;
3683 for (i = 0; i < num - 1; i++) {
3684 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3685 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3686 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3687 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
3689 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3690 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3692 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3699 drm_dp_encode_sideband_req(&msg, txmsg);
3701 drm_dp_queue_down_tx(mgr, txmsg);
3703 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3706 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3710 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3714 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3719 drm_dp_mst_topology_put_mstb(mstb);
3723 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3725 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3726 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3727 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3728 I2C_FUNC_10BIT_ADDR;
3731 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3732 .functionality = drm_dp_mst_i2c_functionality,
3733 .master_xfer = drm_dp_mst_i2c_xfer,
3737 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3738 * @aux: DisplayPort AUX channel
3740 * Returns 0 on success or a negative error code on failure.
3742 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3744 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3745 aux->ddc.algo_data = aux;
3746 aux->ddc.retries = 3;
3748 aux->ddc.class = I2C_CLASS_DDC;
3749 aux->ddc.owner = THIS_MODULE;
3750 aux->ddc.dev.parent = aux->dev;
3751 aux->ddc.dev.of_node = aux->dev->of_node;
3753 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3754 sizeof(aux->ddc.name));
3756 return i2c_add_adapter(&aux->ddc);
3760 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3761 * @aux: DisplayPort AUX channel
3763 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3765 i2c_del_adapter(&aux->ddc);