drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / drm_dp_mst_topology.c
blobd708472d93c4210b71eff4bf1c862f700d8d5876
1 /*
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
20 * OF THIS SOFTWARE.
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>
31 #include <drm/drmP.h>
33 #include <drm/drm_fixed.h>
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_atomic_helper.h>
37 /**
38 * DOC: dp mst helper
40 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
41 * protocol. The helpers contain a topology manager and bandwidth manager.
42 * The helpers encapsulate the sending and received of sideband msgs.
44 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
45 char *buf);
46 static int test_calc_pbn_mode(void);
48 static void drm_dp_put_port(struct drm_dp_mst_port *port);
50 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
51 int id,
52 struct drm_dp_payload *payload);
54 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
55 struct drm_dp_mst_port *port,
56 int offset, int size, u8 *bytes);
58 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
59 struct drm_dp_mst_branch *mstb);
60 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
61 struct drm_dp_mst_branch *mstb,
62 struct drm_dp_mst_port *port);
63 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
64 u8 *guid);
66 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
67 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
68 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
69 /* sideband msg handling */
70 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
72 u8 bitmask = 0x80;
73 u8 bitshift = 7;
74 u8 array_index = 0;
75 int number_of_bits = num_nibbles * 4;
76 u8 remainder = 0;
78 while (number_of_bits != 0) {
79 number_of_bits--;
80 remainder <<= 1;
81 remainder |= (data[array_index] & bitmask) >> bitshift;
82 bitmask >>= 1;
83 bitshift--;
84 if (bitmask == 0) {
85 bitmask = 0x80;
86 bitshift = 7;
87 array_index++;
89 if ((remainder & 0x10) == 0x10)
90 remainder ^= 0x13;
93 number_of_bits = 4;
94 while (number_of_bits != 0) {
95 number_of_bits--;
96 remainder <<= 1;
97 if ((remainder & 0x10) != 0)
98 remainder ^= 0x13;
101 return remainder;
104 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
106 u8 bitmask = 0x80;
107 u8 bitshift = 7;
108 u8 array_index = 0;
109 int number_of_bits = number_of_bytes * 8;
110 u16 remainder = 0;
112 while (number_of_bits != 0) {
113 number_of_bits--;
114 remainder <<= 1;
115 remainder |= (data[array_index] & bitmask) >> bitshift;
116 bitmask >>= 1;
117 bitshift--;
118 if (bitmask == 0) {
119 bitmask = 0x80;
120 bitshift = 7;
121 array_index++;
123 if ((remainder & 0x100) == 0x100)
124 remainder ^= 0xd5;
127 number_of_bits = 8;
128 while (number_of_bits != 0) {
129 number_of_bits--;
130 remainder <<= 1;
131 if ((remainder & 0x100) != 0)
132 remainder ^= 0xd5;
135 return remainder & 0xff;
137 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
139 u8 size = 3;
140 size += (hdr->lct / 2);
141 return size;
144 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
145 u8 *buf, int *len)
147 int idx = 0;
148 int i;
149 u8 crc4;
150 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
151 for (i = 0; i < (hdr->lct / 2); i++)
152 buf[idx++] = hdr->rad[i];
153 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
154 (hdr->msg_len & 0x3f);
155 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
157 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
158 buf[idx - 1] |= (crc4 & 0xf);
160 *len = idx;
163 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
164 u8 *buf, int buflen, u8 *hdrlen)
166 u8 crc4;
167 u8 len;
168 int i;
169 u8 idx;
170 if (buf[0] == 0)
171 return false;
172 len = 3;
173 len += ((buf[0] & 0xf0) >> 4) / 2;
174 if (len > buflen)
175 return false;
176 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
178 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
179 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
180 return false;
183 hdr->lct = (buf[0] & 0xf0) >> 4;
184 hdr->lcr = (buf[0] & 0xf);
185 idx = 1;
186 for (i = 0; i < (hdr->lct / 2); i++)
187 hdr->rad[i] = buf[idx++];
188 hdr->broadcast = (buf[idx] >> 7) & 0x1;
189 hdr->path_msg = (buf[idx] >> 6) & 0x1;
190 hdr->msg_len = buf[idx] & 0x3f;
191 idx++;
192 hdr->somt = (buf[idx] >> 7) & 0x1;
193 hdr->eomt = (buf[idx] >> 6) & 0x1;
194 hdr->seqno = (buf[idx] >> 4) & 0x1;
195 idx++;
196 *hdrlen = idx;
197 return true;
200 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
201 struct drm_dp_sideband_msg_tx *raw)
203 int idx = 0;
204 int i;
205 u8 *buf = raw->msg;
206 buf[idx++] = req->req_type & 0x7f;
208 switch (req->req_type) {
209 case DP_ENUM_PATH_RESOURCES:
210 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
211 idx++;
212 break;
213 case DP_ALLOCATE_PAYLOAD:
214 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
215 (req->u.allocate_payload.number_sdp_streams & 0xf);
216 idx++;
217 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
218 idx++;
219 buf[idx] = (req->u.allocate_payload.pbn >> 8);
220 idx++;
221 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
222 idx++;
223 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
224 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
225 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
226 idx++;
228 if (req->u.allocate_payload.number_sdp_streams & 1) {
229 i = req->u.allocate_payload.number_sdp_streams - 1;
230 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
231 idx++;
233 break;
234 case DP_QUERY_PAYLOAD:
235 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
236 idx++;
237 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
238 idx++;
239 break;
240 case DP_REMOTE_DPCD_READ:
241 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
242 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
243 idx++;
244 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
245 idx++;
246 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
247 idx++;
248 buf[idx] = (req->u.dpcd_read.num_bytes);
249 idx++;
250 break;
252 case DP_REMOTE_DPCD_WRITE:
253 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
254 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
255 idx++;
256 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
257 idx++;
258 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
259 idx++;
260 buf[idx] = (req->u.dpcd_write.num_bytes);
261 idx++;
262 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
263 idx += req->u.dpcd_write.num_bytes;
264 break;
265 case DP_REMOTE_I2C_READ:
266 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
267 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
268 idx++;
269 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
270 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
271 idx++;
272 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
273 idx++;
274 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
275 idx += req->u.i2c_read.transactions[i].num_bytes;
277 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
278 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
279 idx++;
281 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
282 idx++;
283 buf[idx] = (req->u.i2c_read.num_bytes_read);
284 idx++;
285 break;
287 case DP_REMOTE_I2C_WRITE:
288 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
289 idx++;
290 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
291 idx++;
292 buf[idx] = (req->u.i2c_write.num_bytes);
293 idx++;
294 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
295 idx += req->u.i2c_write.num_bytes;
296 break;
298 case DP_POWER_DOWN_PHY:
299 case DP_POWER_UP_PHY:
300 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
301 idx++;
302 break;
304 raw->cur_len = idx;
307 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
309 u8 crc4;
310 crc4 = drm_dp_msg_data_crc4(msg, len);
311 msg[len] = crc4;
314 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
315 struct drm_dp_sideband_msg_tx *raw)
317 int idx = 0;
318 u8 *buf = raw->msg;
320 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
322 raw->cur_len = idx;
325 /* this adds a chunk of msg to the builder to get the final msg */
326 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
327 u8 *replybuf, u8 replybuflen, bool hdr)
329 int ret;
330 u8 crc4;
332 if (hdr) {
333 u8 hdrlen;
334 struct drm_dp_sideband_msg_hdr recv_hdr;
335 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
336 if (ret == false) {
337 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
338 return false;
342 * ignore out-of-order messages or messages that are part of a
343 * failed transaction
345 if (!recv_hdr.somt && !msg->have_somt)
346 return false;
348 /* get length contained in this portion */
349 msg->curchunk_len = recv_hdr.msg_len;
350 msg->curchunk_hdrlen = hdrlen;
352 /* we have already gotten an somt - don't bother parsing */
353 if (recv_hdr.somt && msg->have_somt)
354 return false;
356 if (recv_hdr.somt) {
357 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
358 msg->have_somt = true;
360 if (recv_hdr.eomt)
361 msg->have_eomt = true;
363 /* copy the bytes for the remainder of this header chunk */
364 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
365 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
366 } else {
367 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
368 msg->curchunk_idx += replybuflen;
371 if (msg->curchunk_idx >= msg->curchunk_len) {
372 /* do CRC */
373 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
374 /* copy chunk into bigger msg */
375 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
376 msg->curlen += msg->curchunk_len - 1;
378 return true;
381 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
382 struct drm_dp_sideband_msg_reply_body *repmsg)
384 int idx = 1;
385 int i;
386 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
387 idx += 16;
388 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
389 idx++;
390 if (idx > raw->curlen)
391 goto fail_len;
392 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
393 if (raw->msg[idx] & 0x80)
394 repmsg->u.link_addr.ports[i].input_port = 1;
396 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
397 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
399 idx++;
400 if (idx > raw->curlen)
401 goto fail_len;
402 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
403 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
404 if (repmsg->u.link_addr.ports[i].input_port == 0)
405 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
406 idx++;
407 if (idx > raw->curlen)
408 goto fail_len;
409 if (repmsg->u.link_addr.ports[i].input_port == 0) {
410 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
411 idx++;
412 if (idx > raw->curlen)
413 goto fail_len;
414 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
415 idx += 16;
416 if (idx > raw->curlen)
417 goto fail_len;
418 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
419 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
420 idx++;
423 if (idx > raw->curlen)
424 goto fail_len;
427 return true;
428 fail_len:
429 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
430 return false;
433 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
434 struct drm_dp_sideband_msg_reply_body *repmsg)
436 int idx = 1;
437 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
438 idx++;
439 if (idx > raw->curlen)
440 goto fail_len;
441 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
442 if (idx > raw->curlen)
443 goto fail_len;
445 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
446 return true;
447 fail_len:
448 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
449 return false;
452 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
453 struct drm_dp_sideband_msg_reply_body *repmsg)
455 int idx = 1;
456 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
457 idx++;
458 if (idx > raw->curlen)
459 goto fail_len;
460 return true;
461 fail_len:
462 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
463 return false;
466 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
467 struct drm_dp_sideband_msg_reply_body *repmsg)
469 int idx = 1;
471 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
472 idx++;
473 if (idx > raw->curlen)
474 goto fail_len;
475 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
476 idx++;
477 /* TODO check */
478 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
479 return true;
480 fail_len:
481 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
482 return false;
485 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
486 struct drm_dp_sideband_msg_reply_body *repmsg)
488 int idx = 1;
489 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
490 idx++;
491 if (idx > raw->curlen)
492 goto fail_len;
493 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
494 idx += 2;
495 if (idx > raw->curlen)
496 goto fail_len;
497 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
498 idx += 2;
499 if (idx > raw->curlen)
500 goto fail_len;
501 return true;
502 fail_len:
503 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
504 return false;
507 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
508 struct drm_dp_sideband_msg_reply_body *repmsg)
510 int idx = 1;
511 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
512 idx++;
513 if (idx > raw->curlen)
514 goto fail_len;
515 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
516 idx++;
517 if (idx > raw->curlen)
518 goto fail_len;
519 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
520 idx += 2;
521 if (idx > raw->curlen)
522 goto fail_len;
523 return true;
524 fail_len:
525 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
526 return false;
529 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
530 struct drm_dp_sideband_msg_reply_body *repmsg)
532 int idx = 1;
533 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
534 idx++;
535 if (idx > raw->curlen)
536 goto fail_len;
537 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
538 idx += 2;
539 if (idx > raw->curlen)
540 goto fail_len;
541 return true;
542 fail_len:
543 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
544 return false;
547 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
548 struct drm_dp_sideband_msg_reply_body *repmsg)
550 int idx = 1;
552 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
553 idx++;
554 if (idx > raw->curlen) {
555 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
556 idx, raw->curlen);
557 return false;
559 return true;
562 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
563 struct drm_dp_sideband_msg_reply_body *msg)
565 memset(msg, 0, sizeof(*msg));
566 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
567 msg->req_type = (raw->msg[0] & 0x7f);
569 if (msg->reply_type) {
570 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
571 msg->u.nak.reason = raw->msg[17];
572 msg->u.nak.nak_data = raw->msg[18];
573 return false;
576 switch (msg->req_type) {
577 case DP_LINK_ADDRESS:
578 return drm_dp_sideband_parse_link_address(raw, msg);
579 case DP_QUERY_PAYLOAD:
580 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
581 case DP_REMOTE_DPCD_READ:
582 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
583 case DP_REMOTE_DPCD_WRITE:
584 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
585 case DP_REMOTE_I2C_READ:
586 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
587 case DP_ENUM_PATH_RESOURCES:
588 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
589 case DP_ALLOCATE_PAYLOAD:
590 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
591 case DP_POWER_DOWN_PHY:
592 case DP_POWER_UP_PHY:
593 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
594 default:
595 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
596 return false;
600 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
601 struct drm_dp_sideband_msg_req_body *msg)
603 int idx = 1;
605 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
606 idx++;
607 if (idx > raw->curlen)
608 goto fail_len;
610 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
611 idx += 16;
612 if (idx > raw->curlen)
613 goto fail_len;
615 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
616 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
617 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
618 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
619 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
620 idx++;
621 return true;
622 fail_len:
623 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
624 return false;
627 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
628 struct drm_dp_sideband_msg_req_body *msg)
630 int idx = 1;
632 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
633 idx++;
634 if (idx > raw->curlen)
635 goto fail_len;
637 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
638 idx += 16;
639 if (idx > raw->curlen)
640 goto fail_len;
642 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
643 idx++;
644 return true;
645 fail_len:
646 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
647 return false;
650 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
651 struct drm_dp_sideband_msg_req_body *msg)
653 memset(msg, 0, sizeof(*msg));
654 msg->req_type = (raw->msg[0] & 0x7f);
656 switch (msg->req_type) {
657 case DP_CONNECTION_STATUS_NOTIFY:
658 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
659 case DP_RESOURCE_STATUS_NOTIFY:
660 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
661 default:
662 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
663 return false;
667 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
669 struct drm_dp_sideband_msg_req_body req;
671 req.req_type = DP_REMOTE_DPCD_WRITE;
672 req.u.dpcd_write.port_number = port_num;
673 req.u.dpcd_write.dpcd_address = offset;
674 req.u.dpcd_write.num_bytes = num_bytes;
675 req.u.dpcd_write.bytes = bytes;
676 drm_dp_encode_sideband_req(&req, msg);
678 return 0;
681 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
683 struct drm_dp_sideband_msg_req_body req;
685 req.req_type = DP_LINK_ADDRESS;
686 drm_dp_encode_sideband_req(&req, msg);
687 return 0;
690 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
692 struct drm_dp_sideband_msg_req_body req;
694 req.req_type = DP_ENUM_PATH_RESOURCES;
695 req.u.port_num.port_number = port_num;
696 drm_dp_encode_sideband_req(&req, msg);
697 msg->path_msg = true;
698 return 0;
701 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
702 u8 vcpi, uint16_t pbn,
703 u8 number_sdp_streams,
704 u8 *sdp_stream_sink)
706 struct drm_dp_sideband_msg_req_body req;
707 memset(&req, 0, sizeof(req));
708 req.req_type = DP_ALLOCATE_PAYLOAD;
709 req.u.allocate_payload.port_number = port_num;
710 req.u.allocate_payload.vcpi = vcpi;
711 req.u.allocate_payload.pbn = pbn;
712 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
713 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
714 number_sdp_streams);
715 drm_dp_encode_sideband_req(&req, msg);
716 msg->path_msg = true;
717 return 0;
720 static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
721 int port_num, bool power_up)
723 struct drm_dp_sideband_msg_req_body req;
725 if (power_up)
726 req.req_type = DP_POWER_UP_PHY;
727 else
728 req.req_type = DP_POWER_DOWN_PHY;
730 req.u.port_num.port_number = port_num;
731 drm_dp_encode_sideband_req(&req, msg);
732 msg->path_msg = true;
733 return 0;
736 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
737 struct drm_dp_vcpi *vcpi)
739 int ret, vcpi_ret;
741 mutex_lock(&mgr->payload_lock);
742 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
743 if (ret > mgr->max_payloads) {
744 ret = -EINVAL;
745 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
746 goto out_unlock;
749 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
750 if (vcpi_ret > mgr->max_payloads) {
751 ret = -EINVAL;
752 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
753 goto out_unlock;
756 set_bit(ret, &mgr->payload_mask);
757 set_bit(vcpi_ret, &mgr->vcpi_mask);
758 vcpi->vcpi = vcpi_ret + 1;
759 mgr->proposed_vcpis[ret - 1] = vcpi;
760 out_unlock:
761 mutex_unlock(&mgr->payload_lock);
762 return ret;
765 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
766 int vcpi)
768 int i;
769 if (vcpi == 0)
770 return;
772 mutex_lock(&mgr->payload_lock);
773 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
774 clear_bit(vcpi - 1, &mgr->vcpi_mask);
776 for (i = 0; i < mgr->max_payloads; i++) {
777 if (mgr->proposed_vcpis[i])
778 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
779 mgr->proposed_vcpis[i] = NULL;
780 clear_bit(i + 1, &mgr->payload_mask);
783 mutex_unlock(&mgr->payload_lock);
786 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
787 struct drm_dp_sideband_msg_tx *txmsg)
789 unsigned int state;
792 * All updates to txmsg->state are protected by mgr->qlock, and the two
793 * cases we check here are terminal states. For those the barriers
794 * provided by the wake_up/wait_event pair are enough.
796 state = READ_ONCE(txmsg->state);
797 return (state == DRM_DP_SIDEBAND_TX_RX ||
798 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
801 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
802 struct drm_dp_sideband_msg_tx *txmsg)
804 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
805 int ret;
807 ret = wait_event_timeout(mgr->tx_waitq,
808 check_txmsg_state(mgr, txmsg),
809 (4 * HZ));
810 mutex_lock(&mstb->mgr->qlock);
811 if (ret > 0) {
812 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
813 ret = -EIO;
814 goto out;
816 } else {
817 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
819 /* dump some state */
820 ret = -EIO;
822 /* remove from q */
823 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
824 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
825 list_del(&txmsg->next);
828 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
829 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
830 mstb->tx_slots[txmsg->seqno] = NULL;
833 out:
834 mutex_unlock(&mgr->qlock);
836 return ret;
839 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
841 struct drm_dp_mst_branch *mstb;
843 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
844 if (!mstb)
845 return NULL;
847 mstb->lct = lct;
848 if (lct > 1)
849 memcpy(mstb->rad, rad, lct / 2);
850 INIT_LIST_HEAD(&mstb->ports);
851 kref_init(&mstb->kref);
852 return mstb;
855 static void drm_dp_free_mst_port(struct kref *kref);
857 static void drm_dp_free_mst_branch_device(struct kref *kref)
859 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
860 if (mstb->port_parent) {
861 if (list_empty(&mstb->port_parent->next))
862 kref_put(&mstb->port_parent->kref, drm_dp_free_mst_port);
864 kfree(mstb);
867 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
869 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
870 struct drm_dp_mst_port *port, *tmp;
871 bool wake_tx = false;
874 * init kref again to be used by ports to remove mst branch when it is
875 * not needed anymore
877 kref_init(kref);
879 if (mstb->port_parent && list_empty(&mstb->port_parent->next))
880 kref_get(&mstb->port_parent->kref);
883 * destroy all ports - don't need lock
884 * as there are no more references to the mst branch
885 * device at this point.
887 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
888 list_del(&port->next);
889 drm_dp_put_port(port);
892 /* drop any tx slots msg */
893 mutex_lock(&mstb->mgr->qlock);
894 if (mstb->tx_slots[0]) {
895 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
896 mstb->tx_slots[0] = NULL;
897 wake_tx = true;
899 if (mstb->tx_slots[1]) {
900 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
901 mstb->tx_slots[1] = NULL;
902 wake_tx = true;
904 mutex_unlock(&mstb->mgr->qlock);
906 if (wake_tx)
907 wake_up_all(&mstb->mgr->tx_waitq);
909 kref_put(kref, drm_dp_free_mst_branch_device);
912 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
914 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
918 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
920 struct drm_dp_mst_branch *mstb;
922 switch (old_pdt) {
923 case DP_PEER_DEVICE_DP_LEGACY_CONV:
924 case DP_PEER_DEVICE_SST_SINK:
925 /* remove i2c over sideband */
926 drm_dp_mst_unregister_i2c_bus(&port->aux);
927 break;
928 case DP_PEER_DEVICE_MST_BRANCHING:
929 mstb = port->mstb;
930 port->mstb = NULL;
931 drm_dp_put_mst_branch_device(mstb);
932 break;
936 static void drm_dp_destroy_port(struct kref *kref)
938 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
939 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
941 if (!port->input) {
942 port->vcpi.num_slots = 0;
944 kfree(port->cached_edid);
947 * The only time we don't have a connector
948 * on an output port is if the connector init
949 * fails.
951 if (port->connector) {
952 /* we can't destroy the connector here, as
953 * we might be holding the mode_config.mutex
954 * from an EDID retrieval */
956 mutex_lock(&mgr->destroy_connector_lock);
957 kref_get(&port->parent->kref);
958 list_add(&port->next, &mgr->destroy_connector_list);
959 mutex_unlock(&mgr->destroy_connector_lock);
960 schedule_work(&mgr->destroy_connector_work);
961 return;
963 /* no need to clean up vcpi
964 * as if we have no connector we never setup a vcpi */
965 drm_dp_port_teardown_pdt(port, port->pdt);
966 port->pdt = DP_PEER_DEVICE_NONE;
968 kfree(port);
971 static void drm_dp_put_port(struct drm_dp_mst_port *port)
973 kref_put(&port->kref, drm_dp_destroy_port);
976 static struct drm_dp_mst_branch *drm_dp_mst_get_validated_mstb_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_branch *to_find)
978 struct drm_dp_mst_port *port;
979 struct drm_dp_mst_branch *rmstb;
980 if (to_find == mstb) {
981 kref_get(&mstb->kref);
982 return mstb;
984 list_for_each_entry(port, &mstb->ports, next) {
985 if (port->mstb) {
986 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
987 if (rmstb)
988 return rmstb;
991 return NULL;
994 static struct drm_dp_mst_branch *drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb)
996 struct drm_dp_mst_branch *rmstb = NULL;
997 mutex_lock(&mgr->lock);
998 if (mgr->mst_primary)
999 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
1000 mutex_unlock(&mgr->lock);
1001 return rmstb;
1004 static struct drm_dp_mst_port *drm_dp_mst_get_port_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_port *to_find)
1006 struct drm_dp_mst_port *port, *mport;
1008 list_for_each_entry(port, &mstb->ports, next) {
1009 if (port == to_find) {
1010 kref_get(&port->kref);
1011 return port;
1013 if (port->mstb) {
1014 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
1015 if (mport)
1016 return mport;
1019 return NULL;
1022 static struct drm_dp_mst_port *drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
1024 struct drm_dp_mst_port *rport = NULL;
1025 mutex_lock(&mgr->lock);
1026 if (mgr->mst_primary)
1027 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
1028 mutex_unlock(&mgr->lock);
1029 return rport;
1032 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1034 struct drm_dp_mst_port *port;
1036 list_for_each_entry(port, &mstb->ports, next) {
1037 if (port->port_num == port_num) {
1038 kref_get(&port->kref);
1039 return port;
1043 return NULL;
1047 * calculate a new RAD for this MST branch device
1048 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1049 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1051 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1052 u8 *rad)
1054 int parent_lct = port->parent->lct;
1055 int shift = 4;
1056 int idx = (parent_lct - 1) / 2;
1057 if (parent_lct > 1) {
1058 memcpy(rad, port->parent->rad, idx + 1);
1059 shift = (parent_lct % 2) ? 4 : 0;
1060 } else
1061 rad[0] = 0;
1063 rad[idx] |= port->port_num << shift;
1064 return parent_lct + 1;
1068 * return sends link address for new mstb
1070 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1072 int ret;
1073 u8 rad[6], lct;
1074 bool send_link = false;
1075 switch (port->pdt) {
1076 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1077 case DP_PEER_DEVICE_SST_SINK:
1078 /* add i2c over sideband */
1079 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1080 break;
1081 case DP_PEER_DEVICE_MST_BRANCHING:
1082 lct = drm_dp_calculate_rad(port, rad);
1084 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1085 if (port->mstb) {
1086 port->mstb->mgr = port->mgr;
1087 port->mstb->port_parent = port;
1089 send_link = true;
1091 break;
1093 return send_link;
1096 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1098 int ret;
1100 memcpy(mstb->guid, guid, 16);
1102 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1103 if (mstb->port_parent) {
1104 ret = drm_dp_send_dpcd_write(
1105 mstb->mgr,
1106 mstb->port_parent,
1107 DP_GUID,
1109 mstb->guid);
1110 } else {
1112 ret = drm_dp_dpcd_write(
1113 mstb->mgr->aux,
1114 DP_GUID,
1115 mstb->guid,
1116 16);
1121 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1122 int pnum,
1123 char *proppath,
1124 size_t proppath_size)
1126 int i;
1127 char temp[8];
1128 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1129 for (i = 0; i < (mstb->lct - 1); i++) {
1130 int shift = (i % 2) ? 0 : 4;
1131 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1132 snprintf(temp, sizeof(temp), "-%d", port_num);
1133 strlcat(proppath, temp, proppath_size);
1135 snprintf(temp, sizeof(temp), "-%d", pnum);
1136 strlcat(proppath, temp, proppath_size);
1139 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1140 struct drm_device *dev,
1141 struct drm_dp_link_addr_reply_port *port_msg)
1143 struct drm_dp_mst_port *port;
1144 bool ret;
1145 bool created = false;
1146 int old_pdt = 0;
1147 int old_ddps = 0;
1148 port = drm_dp_get_port(mstb, port_msg->port_number);
1149 if (!port) {
1150 port = kzalloc(sizeof(*port), GFP_KERNEL);
1151 if (!port)
1152 return;
1153 kref_init(&port->kref);
1154 port->parent = mstb;
1155 port->port_num = port_msg->port_number;
1156 port->mgr = mstb->mgr;
1157 port->aux.name = "DPMST";
1158 port->aux.dev = dev->dev;
1159 created = true;
1160 } else {
1161 old_pdt = port->pdt;
1162 old_ddps = port->ddps;
1165 port->pdt = port_msg->peer_device_type;
1166 port->input = port_msg->input_port;
1167 port->mcs = port_msg->mcs;
1168 port->ddps = port_msg->ddps;
1169 port->ldps = port_msg->legacy_device_plug_status;
1170 port->dpcd_rev = port_msg->dpcd_revision;
1171 port->num_sdp_streams = port_msg->num_sdp_streams;
1172 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1174 /* manage mstb port lists with mgr lock - take a reference
1175 for this list */
1176 if (created) {
1177 mutex_lock(&mstb->mgr->lock);
1178 kref_get(&port->kref);
1179 list_add(&port->next, &mstb->ports);
1180 mutex_unlock(&mstb->mgr->lock);
1183 if (old_ddps != port->ddps) {
1184 if (port->ddps) {
1185 if (!port->input)
1186 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1187 } else {
1188 port->available_pbn = 0;
1192 if (old_pdt != port->pdt && !port->input) {
1193 drm_dp_port_teardown_pdt(port, old_pdt);
1195 ret = drm_dp_port_setup_pdt(port);
1196 if (ret == true)
1197 drm_dp_send_link_address(mstb->mgr, port->mstb);
1200 if (created && !port->input) {
1201 char proppath[255];
1203 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
1204 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1205 if (!port->connector) {
1206 /* remove it from the port list */
1207 mutex_lock(&mstb->mgr->lock);
1208 list_del(&port->next);
1209 mutex_unlock(&mstb->mgr->lock);
1210 /* drop port list reference */
1211 drm_dp_put_port(port);
1212 goto out;
1214 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1215 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1216 port->port_num >= DP_MST_LOGICAL_PORT_0) {
1217 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1218 drm_connector_set_tile_property(port->connector);
1220 (*mstb->mgr->cbs->register_connector)(port->connector);
1223 out:
1224 /* put reference to this port */
1225 drm_dp_put_port(port);
1228 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1229 struct drm_dp_connection_status_notify *conn_stat)
1231 struct drm_dp_mst_port *port;
1232 int old_pdt;
1233 int old_ddps;
1234 bool dowork = false;
1235 port = drm_dp_get_port(mstb, conn_stat->port_number);
1236 if (!port)
1237 return;
1239 old_ddps = port->ddps;
1240 old_pdt = port->pdt;
1241 port->pdt = conn_stat->peer_device_type;
1242 port->mcs = conn_stat->message_capability_status;
1243 port->ldps = conn_stat->legacy_device_plug_status;
1244 port->ddps = conn_stat->displayport_device_plug_status;
1246 if (old_ddps != port->ddps) {
1247 if (port->ddps) {
1248 dowork = true;
1249 } else {
1250 port->available_pbn = 0;
1253 if (old_pdt != port->pdt && !port->input) {
1254 drm_dp_port_teardown_pdt(port, old_pdt);
1256 if (drm_dp_port_setup_pdt(port))
1257 dowork = true;
1260 drm_dp_put_port(port);
1261 if (dowork)
1262 queue_work(system_long_wq, &mstb->mgr->work);
1266 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1267 u8 lct, u8 *rad)
1269 struct drm_dp_mst_branch *mstb;
1270 struct drm_dp_mst_port *port;
1271 int i;
1272 /* find the port by iterating down */
1274 mutex_lock(&mgr->lock);
1275 mstb = mgr->mst_primary;
1277 if (!mstb)
1278 goto out;
1280 for (i = 0; i < lct - 1; i++) {
1281 int shift = (i % 2) ? 0 : 4;
1282 int port_num = (rad[i / 2] >> shift) & 0xf;
1284 list_for_each_entry(port, &mstb->ports, next) {
1285 if (port->port_num == port_num) {
1286 mstb = port->mstb;
1287 if (!mstb) {
1288 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1289 goto out;
1292 break;
1296 kref_get(&mstb->kref);
1297 out:
1298 mutex_unlock(&mgr->lock);
1299 return mstb;
1302 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1303 struct drm_dp_mst_branch *mstb,
1304 uint8_t *guid)
1306 struct drm_dp_mst_branch *found_mstb;
1307 struct drm_dp_mst_port *port;
1309 if (memcmp(mstb->guid, guid, 16) == 0)
1310 return mstb;
1313 list_for_each_entry(port, &mstb->ports, next) {
1314 if (!port->mstb)
1315 continue;
1317 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1319 if (found_mstb)
1320 return found_mstb;
1323 return NULL;
1326 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
1327 struct drm_dp_mst_topology_mgr *mgr,
1328 uint8_t *guid)
1330 struct drm_dp_mst_branch *mstb;
1332 /* find the port by iterating down */
1333 mutex_lock(&mgr->lock);
1335 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1337 if (mstb)
1338 kref_get(&mstb->kref);
1340 mutex_unlock(&mgr->lock);
1341 return mstb;
1344 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1345 struct drm_dp_mst_branch *mstb)
1347 struct drm_dp_mst_port *port;
1348 struct drm_dp_mst_branch *mstb_child;
1349 if (!mstb->link_address_sent)
1350 drm_dp_send_link_address(mgr, mstb);
1352 list_for_each_entry(port, &mstb->ports, next) {
1353 if (port->input)
1354 continue;
1356 if (!port->ddps)
1357 continue;
1359 if (!port->available_pbn)
1360 drm_dp_send_enum_path_resources(mgr, mstb, port);
1362 if (port->mstb) {
1363 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1364 if (mstb_child) {
1365 drm_dp_check_and_send_link_address(mgr, mstb_child);
1366 drm_dp_put_mst_branch_device(mstb_child);
1372 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1374 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1375 struct drm_dp_mst_branch *mstb;
1377 mutex_lock(&mgr->lock);
1378 mstb = mgr->mst_primary;
1379 if (mstb) {
1380 kref_get(&mstb->kref);
1382 mutex_unlock(&mgr->lock);
1383 if (mstb) {
1384 drm_dp_check_and_send_link_address(mgr, mstb);
1385 drm_dp_put_mst_branch_device(mstb);
1389 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1390 u8 *guid)
1392 u64 salt;
1394 if (memchr_inv(guid, 0, 16))
1395 return true;
1397 salt = get_jiffies_64();
1399 memcpy(&guid[0], &salt, sizeof(u64));
1400 memcpy(&guid[8], &salt, sizeof(u64));
1402 return false;
1405 #if 0
1406 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1408 struct drm_dp_sideband_msg_req_body req;
1410 req.req_type = DP_REMOTE_DPCD_READ;
1411 req.u.dpcd_read.port_number = port_num;
1412 req.u.dpcd_read.dpcd_address = offset;
1413 req.u.dpcd_read.num_bytes = num_bytes;
1414 drm_dp_encode_sideband_req(&req, msg);
1416 return 0;
1418 #endif
1420 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1421 bool up, u8 *msg, int len)
1423 int ret;
1424 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1425 int tosend, total, offset;
1426 int retries = 0;
1428 retry:
1429 total = len;
1430 offset = 0;
1431 do {
1432 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1434 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1435 &msg[offset],
1436 tosend);
1437 if (ret != tosend) {
1438 if (ret == -EIO && retries < 5) {
1439 retries++;
1440 goto retry;
1442 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1444 return -EIO;
1446 offset += tosend;
1447 total -= tosend;
1448 } while (total > 0);
1449 return 0;
1452 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1453 struct drm_dp_sideband_msg_tx *txmsg)
1455 struct drm_dp_mst_branch *mstb = txmsg->dst;
1456 u8 req_type;
1458 /* both msg slots are full */
1459 if (txmsg->seqno == -1) {
1460 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1461 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1462 return -EAGAIN;
1464 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1465 txmsg->seqno = mstb->last_seqno;
1466 mstb->last_seqno ^= 1;
1467 } else if (mstb->tx_slots[0] == NULL)
1468 txmsg->seqno = 0;
1469 else
1470 txmsg->seqno = 1;
1471 mstb->tx_slots[txmsg->seqno] = txmsg;
1474 req_type = txmsg->msg[0] & 0x7f;
1475 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1476 req_type == DP_RESOURCE_STATUS_NOTIFY)
1477 hdr->broadcast = 1;
1478 else
1479 hdr->broadcast = 0;
1480 hdr->path_msg = txmsg->path_msg;
1481 hdr->lct = mstb->lct;
1482 hdr->lcr = mstb->lct - 1;
1483 if (mstb->lct > 1)
1484 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1485 hdr->seqno = txmsg->seqno;
1486 return 0;
1489 * process a single block of the next message in the sideband queue
1491 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1492 struct drm_dp_sideband_msg_tx *txmsg,
1493 bool up)
1495 u8 chunk[48];
1496 struct drm_dp_sideband_msg_hdr hdr;
1497 int len, space, idx, tosend;
1498 int ret;
1500 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1502 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1503 txmsg->seqno = -1;
1504 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1507 /* make hdr from dst mst - for replies use seqno
1508 otherwise assign one */
1509 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1510 if (ret < 0)
1511 return ret;
1513 /* amount left to send in this message */
1514 len = txmsg->cur_len - txmsg->cur_offset;
1516 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1517 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1519 tosend = min(len, space);
1520 if (len == txmsg->cur_len)
1521 hdr.somt = 1;
1522 if (space >= len)
1523 hdr.eomt = 1;
1526 hdr.msg_len = tosend + 1;
1527 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1528 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1529 /* add crc at end */
1530 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1531 idx += tosend + 1;
1533 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1534 if (ret) {
1535 DRM_DEBUG_KMS("sideband msg failed to send\n");
1536 return ret;
1539 txmsg->cur_offset += tosend;
1540 if (txmsg->cur_offset == txmsg->cur_len) {
1541 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1542 return 1;
1544 return 0;
1547 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1549 struct drm_dp_sideband_msg_tx *txmsg;
1550 int ret;
1552 WARN_ON(!mutex_is_locked(&mgr->qlock));
1554 /* construct a chunk from the first msg in the tx_msg queue */
1555 if (list_empty(&mgr->tx_msg_downq))
1556 return;
1558 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1559 ret = process_single_tx_qlock(mgr, txmsg, false);
1560 if (ret == 1) {
1561 /* txmsg is sent it should be in the slots now */
1562 list_del(&txmsg->next);
1563 } else if (ret) {
1564 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1565 list_del(&txmsg->next);
1566 if (txmsg->seqno != -1)
1567 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1568 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1569 wake_up_all(&mgr->tx_waitq);
1573 /* called holding qlock */
1574 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1575 struct drm_dp_sideband_msg_tx *txmsg)
1577 int ret;
1579 /* construct a chunk from the first msg in the tx_msg queue */
1580 ret = process_single_tx_qlock(mgr, txmsg, true);
1582 if (ret != 1)
1583 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1585 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1588 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1589 struct drm_dp_sideband_msg_tx *txmsg)
1591 mutex_lock(&mgr->qlock);
1592 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1593 if (list_is_singular(&mgr->tx_msg_downq))
1594 process_single_down_tx_qlock(mgr);
1595 mutex_unlock(&mgr->qlock);
1598 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1599 struct drm_dp_mst_branch *mstb)
1601 int len;
1602 struct drm_dp_sideband_msg_tx *txmsg;
1603 int ret;
1605 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1606 if (!txmsg)
1607 return;
1609 txmsg->dst = mstb;
1610 len = build_link_address(txmsg);
1612 mstb->link_address_sent = true;
1613 drm_dp_queue_down_tx(mgr, txmsg);
1615 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1616 if (ret > 0) {
1617 int i;
1619 if (txmsg->reply.reply_type == 1)
1620 DRM_DEBUG_KMS("link address nak received\n");
1621 else {
1622 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1623 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1624 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,
1625 txmsg->reply.u.link_addr.ports[i].input_port,
1626 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1627 txmsg->reply.u.link_addr.ports[i].port_number,
1628 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1629 txmsg->reply.u.link_addr.ports[i].mcs,
1630 txmsg->reply.u.link_addr.ports[i].ddps,
1631 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1632 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1633 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1636 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1638 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1639 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1641 (*mgr->cbs->hotplug)(mgr);
1643 } else {
1644 mstb->link_address_sent = false;
1645 DRM_DEBUG_KMS("link address failed %d\n", ret);
1648 kfree(txmsg);
1651 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1652 struct drm_dp_mst_branch *mstb,
1653 struct drm_dp_mst_port *port)
1655 int len;
1656 struct drm_dp_sideband_msg_tx *txmsg;
1657 int ret;
1659 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1660 if (!txmsg)
1661 return -ENOMEM;
1663 txmsg->dst = mstb;
1664 len = build_enum_path_resources(txmsg, port->port_num);
1666 drm_dp_queue_down_tx(mgr, txmsg);
1668 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1669 if (ret > 0) {
1670 if (txmsg->reply.reply_type == 1)
1671 DRM_DEBUG_KMS("enum path resources nak received\n");
1672 else {
1673 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1674 DRM_ERROR("got incorrect port in response\n");
1675 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,
1676 txmsg->reply.u.path_resources.avail_payload_bw_number);
1677 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1681 kfree(txmsg);
1682 return 0;
1685 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
1687 if (!mstb->port_parent)
1688 return NULL;
1690 if (mstb->port_parent->mstb != mstb)
1691 return mstb->port_parent;
1693 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
1696 static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
1697 struct drm_dp_mst_branch *mstb,
1698 int *port_num)
1700 struct drm_dp_mst_branch *rmstb = NULL;
1701 struct drm_dp_mst_port *found_port;
1702 mutex_lock(&mgr->lock);
1703 if (mgr->mst_primary) {
1704 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
1706 if (found_port) {
1707 rmstb = found_port->parent;
1708 kref_get(&rmstb->kref);
1709 *port_num = found_port->port_num;
1712 mutex_unlock(&mgr->lock);
1713 return rmstb;
1716 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1717 struct drm_dp_mst_port *port,
1718 int id,
1719 int pbn)
1721 struct drm_dp_sideband_msg_tx *txmsg;
1722 struct drm_dp_mst_branch *mstb;
1723 int len, ret, port_num;
1724 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1725 int i;
1727 port = drm_dp_get_validated_port_ref(mgr, port);
1728 if (!port)
1729 return -EINVAL;
1731 port_num = port->port_num;
1732 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1733 if (!mstb) {
1734 mstb = drm_dp_get_last_connected_port_and_mstb(mgr, port->parent, &port_num);
1736 if (!mstb) {
1737 drm_dp_put_port(port);
1738 return -EINVAL;
1742 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1743 if (!txmsg) {
1744 ret = -ENOMEM;
1745 goto fail_put;
1748 for (i = 0; i < port->num_sdp_streams; i++)
1749 sinks[i] = i;
1751 txmsg->dst = mstb;
1752 len = build_allocate_payload(txmsg, port_num,
1754 pbn, port->num_sdp_streams, sinks);
1756 drm_dp_queue_down_tx(mgr, txmsg);
1758 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1759 if (ret > 0) {
1760 if (txmsg->reply.reply_type == 1) {
1761 ret = -EINVAL;
1762 } else
1763 ret = 0;
1765 kfree(txmsg);
1766 fail_put:
1767 drm_dp_put_mst_branch_device(mstb);
1768 drm_dp_put_port(port);
1769 return ret;
1772 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
1773 struct drm_dp_mst_port *port, bool power_up)
1775 struct drm_dp_sideband_msg_tx *txmsg;
1776 int len, ret;
1778 port = drm_dp_get_validated_port_ref(mgr, port);
1779 if (!port)
1780 return -EINVAL;
1782 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1783 if (!txmsg) {
1784 drm_dp_put_port(port);
1785 return -ENOMEM;
1788 txmsg->dst = port->parent;
1789 len = build_power_updown_phy(txmsg, port->port_num, power_up);
1790 drm_dp_queue_down_tx(mgr, txmsg);
1792 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
1793 if (ret > 0) {
1794 if (txmsg->reply.reply_type == 1)
1795 ret = -EINVAL;
1796 else
1797 ret = 0;
1799 kfree(txmsg);
1800 drm_dp_put_port(port);
1802 return ret;
1804 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
1806 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1807 int id,
1808 struct drm_dp_payload *payload)
1810 int ret;
1812 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1813 if (ret < 0) {
1814 payload->payload_state = 0;
1815 return ret;
1817 payload->payload_state = DP_PAYLOAD_LOCAL;
1818 return 0;
1821 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1822 struct drm_dp_mst_port *port,
1823 int id,
1824 struct drm_dp_payload *payload)
1826 int ret;
1827 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1828 if (ret < 0)
1829 return ret;
1830 payload->payload_state = DP_PAYLOAD_REMOTE;
1831 return ret;
1834 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1835 struct drm_dp_mst_port *port,
1836 int id,
1837 struct drm_dp_payload *payload)
1839 DRM_DEBUG_KMS("\n");
1840 /* its okay for these to fail */
1841 if (port) {
1842 drm_dp_payload_send_msg(mgr, port, id, 0);
1845 drm_dp_dpcd_write_payload(mgr, id, payload);
1846 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
1847 return 0;
1850 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1851 int id,
1852 struct drm_dp_payload *payload)
1854 payload->payload_state = 0;
1855 return 0;
1859 * drm_dp_update_payload_part1() - Execute payload update part 1
1860 * @mgr: manager to use.
1862 * This iterates over all proposed virtual channels, and tries to
1863 * allocate space in the link for them. For 0->slots transitions,
1864 * this step just writes the VCPI to the MST device. For slots->0
1865 * transitions, this writes the updated VCPIs and removes the
1866 * remote VC payloads.
1868 * after calling this the driver should generate ACT and payload
1869 * packets.
1871 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1873 int i, j;
1874 int cur_slots = 1;
1875 struct drm_dp_payload req_payload;
1876 struct drm_dp_mst_port *port;
1878 mutex_lock(&mgr->payload_lock);
1879 for (i = 0; i < mgr->max_payloads; i++) {
1880 /* solve the current payloads - compare to the hw ones
1881 - update the hw view */
1882 req_payload.start_slot = cur_slots;
1883 if (mgr->proposed_vcpis[i]) {
1884 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1885 port = drm_dp_get_validated_port_ref(mgr, port);
1886 if (!port) {
1887 mutex_unlock(&mgr->payload_lock);
1888 return -EINVAL;
1890 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1891 req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
1892 } else {
1893 port = NULL;
1894 req_payload.num_slots = 0;
1897 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1898 mgr->payloads[i].start_slot = req_payload.start_slot;
1900 /* work out what is required to happen with this payload */
1901 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
1903 /* need to push an update for this payload */
1904 if (req_payload.num_slots) {
1905 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
1906 mgr->payloads[i].num_slots = req_payload.num_slots;
1907 mgr->payloads[i].vcpi = req_payload.vcpi;
1908 } else if (mgr->payloads[i].num_slots) {
1909 mgr->payloads[i].num_slots = 0;
1910 drm_dp_destroy_payload_step1(mgr, port, mgr->payloads[i].vcpi, &mgr->payloads[i]);
1911 req_payload.payload_state = mgr->payloads[i].payload_state;
1912 mgr->payloads[i].start_slot = 0;
1914 mgr->payloads[i].payload_state = req_payload.payload_state;
1916 cur_slots += req_payload.num_slots;
1918 if (port)
1919 drm_dp_put_port(port);
1922 for (i = 0; i < mgr->max_payloads; i++) {
1923 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1924 DRM_DEBUG_KMS("removing payload %d\n", i);
1925 for (j = i; j < mgr->max_payloads - 1; j++) {
1926 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1927 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1928 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1929 set_bit(j + 1, &mgr->payload_mask);
1930 } else {
1931 clear_bit(j + 1, &mgr->payload_mask);
1934 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1935 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1936 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1940 mutex_unlock(&mgr->payload_lock);
1942 return 0;
1944 EXPORT_SYMBOL(drm_dp_update_payload_part1);
1947 * drm_dp_update_payload_part2() - Execute payload update part 2
1948 * @mgr: manager to use.
1950 * This iterates over all proposed virtual channels, and tries to
1951 * allocate space in the link for them. For 0->slots transitions,
1952 * this step writes the remote VC payload commands. For slots->0
1953 * this just resets some internal state.
1955 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1957 struct drm_dp_mst_port *port;
1958 int i;
1959 int ret = 0;
1960 mutex_lock(&mgr->payload_lock);
1961 for (i = 0; i < mgr->max_payloads; i++) {
1963 if (!mgr->proposed_vcpis[i])
1964 continue;
1966 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1968 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1969 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1970 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1971 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1972 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1974 if (ret) {
1975 mutex_unlock(&mgr->payload_lock);
1976 return ret;
1979 mutex_unlock(&mgr->payload_lock);
1980 return 0;
1982 EXPORT_SYMBOL(drm_dp_update_payload_part2);
1984 #if 0 /* unused as of yet */
1985 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1986 struct drm_dp_mst_port *port,
1987 int offset, int size)
1989 int len;
1990 struct drm_dp_sideband_msg_tx *txmsg;
1992 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1993 if (!txmsg)
1994 return -ENOMEM;
1996 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1997 txmsg->dst = port->parent;
1999 drm_dp_queue_down_tx(mgr, txmsg);
2001 return 0;
2003 #endif
2005 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2006 struct drm_dp_mst_port *port,
2007 int offset, int size, u8 *bytes)
2009 int len;
2010 int ret;
2011 struct drm_dp_sideband_msg_tx *txmsg;
2012 struct drm_dp_mst_branch *mstb;
2014 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
2015 if (!mstb)
2016 return -EINVAL;
2018 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2019 if (!txmsg) {
2020 ret = -ENOMEM;
2021 goto fail_put;
2024 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2025 txmsg->dst = mstb;
2027 drm_dp_queue_down_tx(mgr, txmsg);
2029 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2030 if (ret > 0) {
2031 if (txmsg->reply.reply_type == 1) {
2032 ret = -EINVAL;
2033 } else
2034 ret = 0;
2036 kfree(txmsg);
2037 fail_put:
2038 drm_dp_put_mst_branch_device(mstb);
2039 return ret;
2042 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2044 struct drm_dp_sideband_msg_reply_body reply;
2046 reply.reply_type = 0;
2047 reply.req_type = req_type;
2048 drm_dp_encode_sideband_reply(&reply, msg);
2049 return 0;
2052 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2053 struct drm_dp_mst_branch *mstb,
2054 int req_type, int seqno, bool broadcast)
2056 struct drm_dp_sideband_msg_tx *txmsg;
2058 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2059 if (!txmsg)
2060 return -ENOMEM;
2062 txmsg->dst = mstb;
2063 txmsg->seqno = seqno;
2064 drm_dp_encode_up_ack_reply(txmsg, req_type);
2066 mutex_lock(&mgr->qlock);
2068 process_single_up_tx_qlock(mgr, txmsg);
2070 mutex_unlock(&mgr->qlock);
2072 kfree(txmsg);
2073 return 0;
2076 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2077 int dp_link_count,
2078 int *out)
2080 switch (dp_link_bw) {
2081 default:
2082 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2083 dp_link_bw, dp_link_count);
2084 return false;
2086 case DP_LINK_BW_1_62:
2087 *out = 3 * dp_link_count;
2088 break;
2089 case DP_LINK_BW_2_7:
2090 *out = 5 * dp_link_count;
2091 break;
2092 case DP_LINK_BW_5_4:
2093 *out = 10 * dp_link_count;
2094 break;
2095 case DP_LINK_BW_8_1:
2096 *out = 15 * dp_link_count;
2097 break;
2099 return true;
2103 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2104 * @mgr: manager to set state for
2105 * @mst_state: true to enable MST on this connector - false to disable.
2107 * This is called by the driver when it detects an MST capable device plugged
2108 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2110 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2112 int ret = 0;
2113 struct drm_dp_mst_branch *mstb = NULL;
2115 mutex_lock(&mgr->lock);
2116 if (mst_state == mgr->mst_state)
2117 goto out_unlock;
2119 mgr->mst_state = mst_state;
2120 /* set the device into MST mode */
2121 if (mst_state) {
2122 WARN_ON(mgr->mst_primary);
2124 /* get dpcd info */
2125 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2126 if (ret != DP_RECEIVER_CAP_SIZE) {
2127 DRM_DEBUG_KMS("failed to read DPCD\n");
2128 goto out_unlock;
2131 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2132 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2133 &mgr->pbn_div)) {
2134 ret = -EINVAL;
2135 goto out_unlock;
2138 /* add initial branch device at LCT 1 */
2139 mstb = drm_dp_add_mst_branch_device(1, NULL);
2140 if (mstb == NULL) {
2141 ret = -ENOMEM;
2142 goto out_unlock;
2144 mstb->mgr = mgr;
2146 /* give this the main reference */
2147 mgr->mst_primary = mstb;
2148 kref_get(&mgr->mst_primary->kref);
2150 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2151 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2152 if (ret < 0) {
2153 goto out_unlock;
2157 struct drm_dp_payload reset_pay;
2158 reset_pay.start_slot = 0;
2159 reset_pay.num_slots = 0x3f;
2160 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2163 queue_work(system_long_wq, &mgr->work);
2165 ret = 0;
2166 } else {
2167 /* disable MST on the device */
2168 mstb = mgr->mst_primary;
2169 mgr->mst_primary = NULL;
2170 /* this can fail if the device is gone */
2171 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2172 ret = 0;
2173 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2174 mgr->payload_mask = 0;
2175 set_bit(0, &mgr->payload_mask);
2176 mgr->vcpi_mask = 0;
2179 out_unlock:
2180 mutex_unlock(&mgr->lock);
2181 if (mstb)
2182 drm_dp_put_mst_branch_device(mstb);
2183 return ret;
2186 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2189 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2190 * @mgr: manager to suspend
2192 * This function tells the MST device that we can't handle UP messages
2193 * anymore. This should stop it from sending any since we are suspended.
2195 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2197 mutex_lock(&mgr->lock);
2198 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2199 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2200 mutex_unlock(&mgr->lock);
2201 flush_work(&mgr->work);
2202 flush_work(&mgr->destroy_connector_work);
2204 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2207 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2208 * @mgr: manager to resume
2210 * This will fetch DPCD and see if the device is still there,
2211 * if it is, it will rewrite the MSTM control bits, and return.
2213 * if the device fails this returns -1, and the driver should do
2214 * a full MST reprobe, in case we were undocked.
2216 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2218 int ret = 0;
2220 mutex_lock(&mgr->lock);
2222 if (mgr->mst_primary) {
2223 int sret;
2224 u8 guid[16];
2226 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2227 if (sret != DP_RECEIVER_CAP_SIZE) {
2228 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2229 ret = -1;
2230 goto out_unlock;
2233 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2234 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2235 if (ret < 0) {
2236 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2237 ret = -1;
2238 goto out_unlock;
2241 /* Some hubs forget their guids after they resume */
2242 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2243 if (sret != 16) {
2244 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2245 ret = -1;
2246 goto out_unlock;
2248 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2250 ret = 0;
2251 } else
2252 ret = -1;
2254 out_unlock:
2255 mutex_unlock(&mgr->lock);
2256 return ret;
2258 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2260 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2262 int len;
2263 u8 replyblock[32];
2264 int replylen, origlen, curreply;
2265 int ret;
2266 struct drm_dp_sideband_msg_rx *msg;
2267 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2268 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2270 len = min(mgr->max_dpcd_transaction_bytes, 16);
2271 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2272 replyblock, len);
2273 if (ret != len) {
2274 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2275 return false;
2277 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2278 if (!ret) {
2279 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2280 return false;
2282 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2284 origlen = replylen;
2285 replylen -= len;
2286 curreply = len;
2287 while (replylen > 0) {
2288 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2289 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2290 replyblock, len);
2291 if (ret != len) {
2292 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2293 len, ret);
2294 return false;
2297 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2298 if (!ret) {
2299 DRM_DEBUG_KMS("failed to build sideband msg\n");
2300 return false;
2303 curreply += len;
2304 replylen -= len;
2306 return true;
2309 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2311 int ret = 0;
2313 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2314 memset(&mgr->down_rep_recv, 0,
2315 sizeof(struct drm_dp_sideband_msg_rx));
2316 return 0;
2319 if (mgr->down_rep_recv.have_eomt) {
2320 struct drm_dp_sideband_msg_tx *txmsg;
2321 struct drm_dp_mst_branch *mstb;
2322 int slot = -1;
2323 mstb = drm_dp_get_mst_branch_device(mgr,
2324 mgr->down_rep_recv.initial_hdr.lct,
2325 mgr->down_rep_recv.initial_hdr.rad);
2327 if (!mstb) {
2328 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2329 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2330 return 0;
2333 /* find the message */
2334 slot = mgr->down_rep_recv.initial_hdr.seqno;
2335 mutex_lock(&mgr->qlock);
2336 txmsg = mstb->tx_slots[slot];
2337 /* remove from slots */
2338 mutex_unlock(&mgr->qlock);
2340 if (!txmsg) {
2341 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2342 mstb,
2343 mgr->down_rep_recv.initial_hdr.seqno,
2344 mgr->down_rep_recv.initial_hdr.lct,
2345 mgr->down_rep_recv.initial_hdr.rad[0],
2346 mgr->down_rep_recv.msg[0]);
2347 drm_dp_put_mst_branch_device(mstb);
2348 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2349 return 0;
2352 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2353 if (txmsg->reply.reply_type == 1) {
2354 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);
2357 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2358 drm_dp_put_mst_branch_device(mstb);
2360 mutex_lock(&mgr->qlock);
2361 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2362 mstb->tx_slots[slot] = NULL;
2363 mutex_unlock(&mgr->qlock);
2365 wake_up_all(&mgr->tx_waitq);
2367 return ret;
2370 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2372 int ret = 0;
2374 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2375 memset(&mgr->up_req_recv, 0,
2376 sizeof(struct drm_dp_sideband_msg_rx));
2377 return 0;
2380 if (mgr->up_req_recv.have_eomt) {
2381 struct drm_dp_sideband_msg_req_body msg;
2382 struct drm_dp_mst_branch *mstb = NULL;
2383 bool seqno;
2385 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2386 mstb = drm_dp_get_mst_branch_device(mgr,
2387 mgr->up_req_recv.initial_hdr.lct,
2388 mgr->up_req_recv.initial_hdr.rad);
2389 if (!mstb) {
2390 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2391 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2392 return 0;
2396 seqno = mgr->up_req_recv.initial_hdr.seqno;
2397 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2399 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2400 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2402 if (!mstb)
2403 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2405 if (!mstb) {
2406 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2407 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2408 return 0;
2411 drm_dp_update_port(mstb, &msg.u.conn_stat);
2413 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);
2414 (*mgr->cbs->hotplug)(mgr);
2416 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2417 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2418 if (!mstb)
2419 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2421 if (!mstb) {
2422 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2423 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2424 return 0;
2427 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2430 if (mstb)
2431 drm_dp_put_mst_branch_device(mstb);
2433 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2435 return ret;
2439 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2440 * @mgr: manager to notify irq for.
2441 * @esi: 4 bytes from SINK_COUNT_ESI
2442 * @handled: whether the hpd interrupt was consumed or not
2444 * This should be called from the driver when it detects a short IRQ,
2445 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2446 * topology manager will process the sideband messages received as a result
2447 * of this.
2449 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2451 int ret = 0;
2452 int sc;
2453 *handled = false;
2454 sc = esi[0] & 0x3f;
2456 if (sc != mgr->sink_count) {
2457 mgr->sink_count = sc;
2458 *handled = true;
2461 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2462 ret = drm_dp_mst_handle_down_rep(mgr);
2463 *handled = true;
2466 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2467 ret |= drm_dp_mst_handle_up_req(mgr);
2468 *handled = true;
2471 drm_dp_mst_kick_tx(mgr);
2472 return ret;
2474 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2477 * drm_dp_mst_detect_port() - get connection status for an MST port
2478 * @connector: DRM connector for this port
2479 * @mgr: manager for this port
2480 * @port: unverified pointer to a port
2482 * This returns the current connection state for a port. It validates the
2483 * port pointer still exists so the caller doesn't require a reference
2485 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2486 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2488 enum drm_connector_status status = connector_status_disconnected;
2490 /* we need to search for the port in the mgr in case its gone */
2491 port = drm_dp_get_validated_port_ref(mgr, port);
2492 if (!port)
2493 return connector_status_disconnected;
2495 if (!port->ddps)
2496 goto out;
2498 switch (port->pdt) {
2499 case DP_PEER_DEVICE_NONE:
2500 case DP_PEER_DEVICE_MST_BRANCHING:
2501 break;
2503 case DP_PEER_DEVICE_SST_SINK:
2504 status = connector_status_connected;
2505 /* for logical ports - cache the EDID */
2506 if (port->port_num >= 8 && !port->cached_edid) {
2507 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2509 break;
2510 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2511 if (port->ldps)
2512 status = connector_status_connected;
2513 break;
2515 out:
2516 drm_dp_put_port(port);
2517 return status;
2519 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2522 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2523 * @mgr: manager for this port
2524 * @port: unverified pointer to a port.
2526 * This returns whether the port supports audio or not.
2528 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2529 struct drm_dp_mst_port *port)
2531 bool ret = false;
2533 port = drm_dp_get_validated_port_ref(mgr, port);
2534 if (!port)
2535 return ret;
2536 ret = port->has_audio;
2537 drm_dp_put_port(port);
2538 return ret;
2540 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2543 * drm_dp_mst_get_edid() - get EDID for an MST port
2544 * @connector: toplevel connector to get EDID for
2545 * @mgr: manager for this port
2546 * @port: unverified pointer to a port.
2548 * This returns an EDID for the port connected to a connector,
2549 * It validates the pointer still exists so the caller doesn't require a
2550 * reference.
2552 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2554 struct edid *edid = NULL;
2556 /* we need to search for the port in the mgr in case its gone */
2557 port = drm_dp_get_validated_port_ref(mgr, port);
2558 if (!port)
2559 return NULL;
2561 if (port->cached_edid)
2562 edid = drm_edid_duplicate(port->cached_edid);
2563 else {
2564 edid = drm_get_edid(connector, &port->aux.ddc);
2565 drm_connector_set_tile_property(connector);
2567 port->has_audio = drm_detect_monitor_audio(edid);
2568 drm_dp_put_port(port);
2569 return edid;
2571 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2574 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2575 * @mgr: manager to use
2576 * @pbn: payload bandwidth to convert into slots.
2578 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2579 int pbn)
2581 int num_slots;
2583 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2585 /* max. time slots - one slot for MTP header */
2586 if (num_slots > 63)
2587 return -ENOSPC;
2588 return num_slots;
2590 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2592 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2593 struct drm_dp_vcpi *vcpi, int pbn, int slots)
2595 int ret;
2597 /* max. time slots - one slot for MTP header */
2598 if (slots > 63)
2599 return -ENOSPC;
2601 vcpi->pbn = pbn;
2602 vcpi->aligned_pbn = slots * mgr->pbn_div;
2603 vcpi->num_slots = slots;
2605 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2606 if (ret < 0)
2607 return ret;
2608 return 0;
2612 * drm_dp_atomic_find_vcpi_slots() - Find and add vcpi slots to the state
2613 * @state: global atomic state
2614 * @mgr: MST topology manager for the port
2615 * @port: port to find vcpi slots for
2616 * @pbn: bandwidth required for the mode in PBN
2618 * RETURNS:
2619 * Total slots in the atomic state assigned for this port or error
2621 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
2622 struct drm_dp_mst_topology_mgr *mgr,
2623 struct drm_dp_mst_port *port, int pbn)
2625 struct drm_dp_mst_topology_state *topology_state;
2626 int req_slots;
2628 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
2629 if (IS_ERR(topology_state))
2630 return PTR_ERR(topology_state);
2632 port = drm_dp_get_validated_port_ref(mgr, port);
2633 if (port == NULL)
2634 return -EINVAL;
2635 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2636 DRM_DEBUG_KMS("vcpi slots req=%d, avail=%d\n",
2637 req_slots, topology_state->avail_slots);
2639 if (req_slots > topology_state->avail_slots) {
2640 drm_dp_put_port(port);
2641 return -ENOSPC;
2644 topology_state->avail_slots -= req_slots;
2645 DRM_DEBUG_KMS("vcpi slots avail=%d", topology_state->avail_slots);
2647 drm_dp_put_port(port);
2648 return req_slots;
2650 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
2653 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
2654 * @state: global atomic state
2655 * @mgr: MST topology manager for the port
2656 * @slots: number of vcpi slots to release
2658 * RETURNS:
2659 * 0 if @slots were added back to &drm_dp_mst_topology_state->avail_slots or
2660 * negative error code
2662 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
2663 struct drm_dp_mst_topology_mgr *mgr,
2664 int slots)
2666 struct drm_dp_mst_topology_state *topology_state;
2668 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
2669 if (IS_ERR(topology_state))
2670 return PTR_ERR(topology_state);
2672 /* We cannot rely on port->vcpi.num_slots to update
2673 * topology_state->avail_slots as the port may not exist if the parent
2674 * branch device was unplugged. This should be fixed by tracking
2675 * per-port slot allocation in drm_dp_mst_topology_state instead of
2676 * depending on the caller to tell us how many slots to release.
2678 topology_state->avail_slots += slots;
2679 DRM_DEBUG_KMS("vcpi slots released=%d, avail=%d\n",
2680 slots, topology_state->avail_slots);
2682 return 0;
2684 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
2687 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2688 * @mgr: manager for this port
2689 * @port: port to allocate a virtual channel for.
2690 * @pbn: payload bandwidth number to request
2691 * @slots: returned number of slots for this PBN.
2693 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2694 struct drm_dp_mst_port *port, int pbn, int slots)
2696 int ret;
2698 port = drm_dp_get_validated_port_ref(mgr, port);
2699 if (!port)
2700 return false;
2702 if (slots < 0)
2703 return false;
2705 if (port->vcpi.vcpi > 0) {
2706 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2707 if (pbn == port->vcpi.pbn) {
2708 drm_dp_put_port(port);
2709 return true;
2713 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
2714 if (ret) {
2715 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
2716 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
2717 goto out;
2719 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
2720 pbn, port->vcpi.num_slots);
2722 drm_dp_put_port(port);
2723 return true;
2724 out:
2725 return false;
2727 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2729 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2731 int slots = 0;
2732 port = drm_dp_get_validated_port_ref(mgr, port);
2733 if (!port)
2734 return slots;
2736 slots = port->vcpi.num_slots;
2737 drm_dp_put_port(port);
2738 return slots;
2740 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2743 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2744 * @mgr: manager for this port
2745 * @port: unverified pointer to a port.
2747 * This just resets the number of slots for the ports VCPI for later programming.
2749 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2751 port = drm_dp_get_validated_port_ref(mgr, port);
2752 if (!port)
2753 return;
2754 port->vcpi.num_slots = 0;
2755 drm_dp_put_port(port);
2757 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2760 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2761 * @mgr: manager for this port
2762 * @port: unverified port to deallocate vcpi for
2764 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2766 port = drm_dp_get_validated_port_ref(mgr, port);
2767 if (!port)
2768 return;
2770 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2771 port->vcpi.num_slots = 0;
2772 port->vcpi.pbn = 0;
2773 port->vcpi.aligned_pbn = 0;
2774 port->vcpi.vcpi = 0;
2775 drm_dp_put_port(port);
2777 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2779 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2780 int id, struct drm_dp_payload *payload)
2782 u8 payload_alloc[3], status;
2783 int ret;
2784 int retries = 0;
2786 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2787 DP_PAYLOAD_TABLE_UPDATED);
2789 payload_alloc[0] = id;
2790 payload_alloc[1] = payload->start_slot;
2791 payload_alloc[2] = payload->num_slots;
2793 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2794 if (ret != 3) {
2795 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2796 goto fail;
2799 retry:
2800 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2801 if (ret < 0) {
2802 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2803 goto fail;
2806 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2807 retries++;
2808 if (retries < 20) {
2809 usleep_range(10000, 20000);
2810 goto retry;
2812 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2813 ret = -EINVAL;
2814 goto fail;
2816 ret = 0;
2817 fail:
2818 return ret;
2823 * drm_dp_check_act_status() - Check ACT handled status.
2824 * @mgr: manager to use
2826 * Check the payload status bits in the DPCD for ACT handled completion.
2828 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2830 u8 status;
2831 int ret;
2832 int count = 0;
2834 do {
2835 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2837 if (ret < 0) {
2838 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2839 goto fail;
2842 if (status & DP_PAYLOAD_ACT_HANDLED)
2843 break;
2844 count++;
2845 udelay(100);
2847 } while (count < 30);
2849 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2850 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2851 ret = -EINVAL;
2852 goto fail;
2854 return 0;
2855 fail:
2856 return ret;
2858 EXPORT_SYMBOL(drm_dp_check_act_status);
2861 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2862 * @clock: dot clock for the mode
2863 * @bpp: bpp for the mode.
2865 * This uses the formula in the spec to calculate the PBN value for a mode.
2867 int drm_dp_calc_pbn_mode(int clock, int bpp)
2869 u64 kbps;
2870 s64 peak_kbps;
2871 u32 numerator;
2872 u32 denominator;
2874 kbps = clock * bpp;
2877 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2878 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2879 * common multiplier to render an integer PBN for all link rate/lane
2880 * counts combinations
2881 * calculate
2882 * peak_kbps *= (1006/1000)
2883 * peak_kbps *= (64/54)
2884 * peak_kbps *= 8 convert to bytes
2887 numerator = 64 * 1006;
2888 denominator = 54 * 8 * 1000 * 1000;
2890 kbps *= numerator;
2891 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
2893 return drm_fixp2int_ceil(peak_kbps);
2895 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2897 static int test_calc_pbn_mode(void)
2899 int ret;
2900 ret = drm_dp_calc_pbn_mode(154000, 30);
2901 if (ret != 689) {
2902 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2903 154000, 30, 689, ret);
2904 return -EINVAL;
2906 ret = drm_dp_calc_pbn_mode(234000, 30);
2907 if (ret != 1047) {
2908 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2909 234000, 30, 1047, ret);
2910 return -EINVAL;
2912 ret = drm_dp_calc_pbn_mode(297000, 24);
2913 if (ret != 1063) {
2914 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2915 297000, 24, 1063, ret);
2916 return -EINVAL;
2918 return 0;
2921 /* we want to kick the TX after we've ack the up/down IRQs. */
2922 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2924 queue_work(system_long_wq, &mgr->tx_work);
2927 static void drm_dp_mst_dump_mstb(struct seq_file *m,
2928 struct drm_dp_mst_branch *mstb)
2930 struct drm_dp_mst_port *port;
2931 int tabs = mstb->lct;
2932 char prefix[10];
2933 int i;
2935 for (i = 0; i < tabs; i++)
2936 prefix[i] = '\t';
2937 prefix[i] = '\0';
2939 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2940 list_for_each_entry(port, &mstb->ports, next) {
2941 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);
2942 if (port->mstb)
2943 drm_dp_mst_dump_mstb(m, port->mstb);
2947 #define DP_PAYLOAD_TABLE_SIZE 64
2949 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2950 char *buf)
2952 int i;
2954 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
2955 if (drm_dp_dpcd_read(mgr->aux,
2956 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
2957 &buf[i], 16) != 16)
2958 return false;
2960 return true;
2963 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
2964 struct drm_dp_mst_port *port, char *name,
2965 int namelen)
2967 struct edid *mst_edid;
2969 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
2970 drm_edid_get_monitor_name(mst_edid, name, namelen);
2974 * drm_dp_mst_dump_topology(): dump topology to seq file.
2975 * @m: seq_file to dump output to
2976 * @mgr: manager to dump current topology for.
2978 * helper to dump MST topology to a seq file for debugfs.
2980 void drm_dp_mst_dump_topology(struct seq_file *m,
2981 struct drm_dp_mst_topology_mgr *mgr)
2983 int i;
2984 struct drm_dp_mst_port *port;
2986 mutex_lock(&mgr->lock);
2987 if (mgr->mst_primary)
2988 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2990 /* dump VCPIs */
2991 mutex_unlock(&mgr->lock);
2993 mutex_lock(&mgr->payload_lock);
2994 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
2995 mgr->max_payloads);
2997 for (i = 0; i < mgr->max_payloads; i++) {
2998 if (mgr->proposed_vcpis[i]) {
2999 char name[14];
3001 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
3002 fetch_monitor_name(mgr, port, name, sizeof(name));
3003 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3004 port->port_num, port->vcpi.vcpi,
3005 port->vcpi.num_slots,
3006 (*name != 0) ? name : "Unknown");
3007 } else
3008 seq_printf(m, "vcpi %d:unused\n", i);
3010 for (i = 0; i < mgr->max_payloads; i++) {
3011 seq_printf(m, "payload %d: %d, %d, %d\n",
3013 mgr->payloads[i].payload_state,
3014 mgr->payloads[i].start_slot,
3015 mgr->payloads[i].num_slots);
3019 mutex_unlock(&mgr->payload_lock);
3021 mutex_lock(&mgr->lock);
3022 if (mgr->mst_primary) {
3023 u8 buf[DP_PAYLOAD_TABLE_SIZE];
3024 int ret;
3026 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
3027 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
3028 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
3029 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
3030 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
3031 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
3033 /* dump the standard OUI branch header */
3034 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
3035 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
3036 for (i = 0x3; i < 0x8 && buf[i]; i++)
3037 seq_printf(m, "%c", buf[i]);
3038 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
3039 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
3040 if (dump_dp_payload_table(mgr, buf))
3041 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
3044 mutex_unlock(&mgr->lock);
3047 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
3049 static void drm_dp_tx_work(struct work_struct *work)
3051 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
3053 mutex_lock(&mgr->qlock);
3054 if (!list_empty(&mgr->tx_msg_downq))
3055 process_single_down_tx_qlock(mgr);
3056 mutex_unlock(&mgr->qlock);
3059 static void drm_dp_free_mst_port(struct kref *kref)
3061 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
3062 kref_put(&port->parent->kref, drm_dp_free_mst_branch_device);
3063 kfree(port);
3066 static void drm_dp_destroy_connector_work(struct work_struct *work)
3068 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
3069 struct drm_dp_mst_port *port;
3070 bool send_hotplug = false;
3072 * Not a regular list traverse as we have to drop the destroy
3073 * connector lock before destroying the connector, to avoid AB->BA
3074 * ordering between this lock and the config mutex.
3076 for (;;) {
3077 mutex_lock(&mgr->destroy_connector_lock);
3078 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
3079 if (!port) {
3080 mutex_unlock(&mgr->destroy_connector_lock);
3081 break;
3083 list_del(&port->next);
3084 mutex_unlock(&mgr->destroy_connector_lock);
3086 kref_init(&port->kref);
3087 INIT_LIST_HEAD(&port->next);
3089 mgr->cbs->destroy_connector(mgr, port->connector);
3091 drm_dp_port_teardown_pdt(port, port->pdt);
3092 port->pdt = DP_PEER_DEVICE_NONE;
3094 if (!port->input && port->vcpi.vcpi > 0) {
3095 drm_dp_mst_reset_vcpi_slots(mgr, port);
3096 drm_dp_update_payload_part1(mgr);
3097 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3100 kref_put(&port->kref, drm_dp_free_mst_port);
3101 send_hotplug = true;
3103 if (send_hotplug)
3104 (*mgr->cbs->hotplug)(mgr);
3107 static struct drm_private_state *
3108 drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
3110 struct drm_dp_mst_topology_state *state;
3112 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
3113 if (!state)
3114 return NULL;
3116 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
3118 return &state->base;
3121 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
3122 struct drm_private_state *state)
3124 struct drm_dp_mst_topology_state *mst_state =
3125 to_dp_mst_topology_state(state);
3127 kfree(mst_state);
3130 static const struct drm_private_state_funcs mst_state_funcs = {
3131 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
3132 .atomic_destroy_state = drm_dp_mst_destroy_state,
3136 * drm_atomic_get_mst_topology_state: get MST topology state
3138 * @state: global atomic state
3139 * @mgr: MST topology manager, also the private object in this case
3141 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3142 * state vtable so that the private object state returned is that of a MST
3143 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3144 * to care of the locking, so warn if don't hold the connection_mutex.
3146 * RETURNS:
3148 * The MST topology state or error pointer.
3150 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
3151 struct drm_dp_mst_topology_mgr *mgr)
3153 struct drm_device *dev = mgr->dev;
3155 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3156 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
3158 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
3161 * drm_dp_mst_topology_mgr_init - initialise a topology manager
3162 * @mgr: manager struct to initialise
3163 * @dev: device providing this structure - for i2c addition.
3164 * @aux: DP helper aux channel to talk to this device
3165 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3166 * @max_payloads: maximum number of payloads this GPU can source
3167 * @conn_base_id: the connector object ID the MST device is connected to.
3169 * Return 0 for success, or negative error code on failure
3171 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
3172 struct drm_device *dev, struct drm_dp_aux *aux,
3173 int max_dpcd_transaction_bytes,
3174 int max_payloads, int conn_base_id)
3176 struct drm_dp_mst_topology_state *mst_state;
3178 mutex_init(&mgr->lock);
3179 mutex_init(&mgr->qlock);
3180 mutex_init(&mgr->payload_lock);
3181 mutex_init(&mgr->destroy_connector_lock);
3182 INIT_LIST_HEAD(&mgr->tx_msg_downq);
3183 INIT_LIST_HEAD(&mgr->destroy_connector_list);
3184 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
3185 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
3186 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
3187 init_waitqueue_head(&mgr->tx_waitq);
3188 mgr->dev = dev;
3189 mgr->aux = aux;
3190 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
3191 mgr->max_payloads = max_payloads;
3192 mgr->conn_base_id = conn_base_id;
3193 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3194 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3195 return -EINVAL;
3196 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3197 if (!mgr->payloads)
3198 return -ENOMEM;
3199 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3200 if (!mgr->proposed_vcpis)
3201 return -ENOMEM;
3202 set_bit(0, &mgr->payload_mask);
3203 if (test_calc_pbn_mode() < 0)
3204 DRM_ERROR("MST PBN self-test failed\n");
3206 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
3207 if (mst_state == NULL)
3208 return -ENOMEM;
3210 mst_state->mgr = mgr;
3212 /* max. time slots - one slot for MTP header */
3213 mst_state->avail_slots = 63;
3215 drm_atomic_private_obj_init(&mgr->base,
3216 &mst_state->base,
3217 &mst_state_funcs);
3219 return 0;
3221 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3224 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3225 * @mgr: manager to destroy
3227 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3229 flush_work(&mgr->work);
3230 flush_work(&mgr->destroy_connector_work);
3231 mutex_lock(&mgr->payload_lock);
3232 kfree(mgr->payloads);
3233 mgr->payloads = NULL;
3234 kfree(mgr->proposed_vcpis);
3235 mgr->proposed_vcpis = NULL;
3236 mutex_unlock(&mgr->payload_lock);
3237 mgr->dev = NULL;
3238 mgr->aux = NULL;
3239 drm_atomic_private_obj_fini(&mgr->base);
3240 mgr->funcs = NULL;
3242 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3244 /* I2C device */
3245 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3246 int num)
3248 struct drm_dp_aux *aux = adapter->algo_data;
3249 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3250 struct drm_dp_mst_branch *mstb;
3251 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3252 unsigned int i;
3253 bool reading = false;
3254 struct drm_dp_sideband_msg_req_body msg;
3255 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3256 int ret;
3258 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
3259 if (!mstb)
3260 return -EREMOTEIO;
3262 /* construct i2c msg */
3263 /* see if last msg is a read */
3264 if (msgs[num - 1].flags & I2C_M_RD)
3265 reading = true;
3267 if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
3268 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3269 ret = -EIO;
3270 goto out;
3273 memset(&msg, 0, sizeof(msg));
3274 msg.req_type = DP_REMOTE_I2C_READ;
3275 msg.u.i2c_read.num_transactions = num - 1;
3276 msg.u.i2c_read.port_number = port->port_num;
3277 for (i = 0; i < num - 1; i++) {
3278 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3279 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3280 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3282 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3283 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3285 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3286 if (!txmsg) {
3287 ret = -ENOMEM;
3288 goto out;
3291 txmsg->dst = mstb;
3292 drm_dp_encode_sideband_req(&msg, txmsg);
3294 drm_dp_queue_down_tx(mgr, txmsg);
3296 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3297 if (ret > 0) {
3299 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3300 ret = -EREMOTEIO;
3301 goto out;
3303 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3304 ret = -EIO;
3305 goto out;
3307 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3308 ret = num;
3310 out:
3311 kfree(txmsg);
3312 drm_dp_put_mst_branch_device(mstb);
3313 return ret;
3316 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3318 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3319 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3320 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3321 I2C_FUNC_10BIT_ADDR;
3324 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3325 .functionality = drm_dp_mst_i2c_functionality,
3326 .master_xfer = drm_dp_mst_i2c_xfer,
3330 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3331 * @aux: DisplayPort AUX channel
3333 * Returns 0 on success or a negative error code on failure.
3335 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3337 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3338 aux->ddc.algo_data = aux;
3339 aux->ddc.retries = 3;
3341 aux->ddc.class = I2C_CLASS_DDC;
3342 aux->ddc.owner = THIS_MODULE;
3343 aux->ddc.dev.parent = aux->dev;
3344 aux->ddc.dev.of_node = aux->dev->of_node;
3346 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3347 sizeof(aux->ddc.name));
3349 return i2c_add_adapter(&aux->ddc);
3353 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3354 * @aux: DisplayPort AUX channel
3356 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3358 i2c_del_adapter(&aux->ddc);