Linux 4.2.1
[linux/fpc-iii.git] / drivers / gpu / drm / drm_dp_mst_topology.c
blobeb603f1defc2250ea158864ea4371e24655138e1
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>
35 /**
36 * DOC: dp mst helper
38 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
39 * protocol. The helpers contain a topology manager and bandwidth manager.
40 * The helpers encapsulate the sending and received of sideband msgs.
42 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
43 char *buf);
44 static int test_calc_pbn_mode(void);
46 static void drm_dp_put_port(struct drm_dp_mst_port *port);
48 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
49 int id,
50 struct drm_dp_payload *payload);
52 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
53 struct drm_dp_mst_port *port,
54 int offset, int size, u8 *bytes);
56 static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
57 struct drm_dp_mst_branch *mstb);
58 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
59 struct drm_dp_mst_branch *mstb,
60 struct drm_dp_mst_port *port);
61 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
62 u8 *guid);
64 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
65 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
66 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
67 /* sideband msg handling */
68 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
70 u8 bitmask = 0x80;
71 u8 bitshift = 7;
72 u8 array_index = 0;
73 int number_of_bits = num_nibbles * 4;
74 u8 remainder = 0;
76 while (number_of_bits != 0) {
77 number_of_bits--;
78 remainder <<= 1;
79 remainder |= (data[array_index] & bitmask) >> bitshift;
80 bitmask >>= 1;
81 bitshift--;
82 if (bitmask == 0) {
83 bitmask = 0x80;
84 bitshift = 7;
85 array_index++;
87 if ((remainder & 0x10) == 0x10)
88 remainder ^= 0x13;
91 number_of_bits = 4;
92 while (number_of_bits != 0) {
93 number_of_bits--;
94 remainder <<= 1;
95 if ((remainder & 0x10) != 0)
96 remainder ^= 0x13;
99 return remainder;
102 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
104 u8 bitmask = 0x80;
105 u8 bitshift = 7;
106 u8 array_index = 0;
107 int number_of_bits = number_of_bytes * 8;
108 u16 remainder = 0;
110 while (number_of_bits != 0) {
111 number_of_bits--;
112 remainder <<= 1;
113 remainder |= (data[array_index] & bitmask) >> bitshift;
114 bitmask >>= 1;
115 bitshift--;
116 if (bitmask == 0) {
117 bitmask = 0x80;
118 bitshift = 7;
119 array_index++;
121 if ((remainder & 0x100) == 0x100)
122 remainder ^= 0xd5;
125 number_of_bits = 8;
126 while (number_of_bits != 0) {
127 number_of_bits--;
128 remainder <<= 1;
129 if ((remainder & 0x100) != 0)
130 remainder ^= 0xd5;
133 return remainder & 0xff;
135 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
137 u8 size = 3;
138 size += (hdr->lct / 2);
139 return size;
142 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
143 u8 *buf, int *len)
145 int idx = 0;
146 int i;
147 u8 crc4;
148 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
149 for (i = 0; i < (hdr->lct / 2); i++)
150 buf[idx++] = hdr->rad[i];
151 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
152 (hdr->msg_len & 0x3f);
153 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
155 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
156 buf[idx - 1] |= (crc4 & 0xf);
158 *len = idx;
161 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
162 u8 *buf, int buflen, u8 *hdrlen)
164 u8 crc4;
165 u8 len;
166 int i;
167 u8 idx;
168 if (buf[0] == 0)
169 return false;
170 len = 3;
171 len += ((buf[0] & 0xf0) >> 4) / 2;
172 if (len > buflen)
173 return false;
174 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
176 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
177 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
178 return false;
181 hdr->lct = (buf[0] & 0xf0) >> 4;
182 hdr->lcr = (buf[0] & 0xf);
183 idx = 1;
184 for (i = 0; i < (hdr->lct / 2); i++)
185 hdr->rad[i] = buf[idx++];
186 hdr->broadcast = (buf[idx] >> 7) & 0x1;
187 hdr->path_msg = (buf[idx] >> 6) & 0x1;
188 hdr->msg_len = buf[idx] & 0x3f;
189 idx++;
190 hdr->somt = (buf[idx] >> 7) & 0x1;
191 hdr->eomt = (buf[idx] >> 6) & 0x1;
192 hdr->seqno = (buf[idx] >> 4) & 0x1;
193 idx++;
194 *hdrlen = idx;
195 return true;
198 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
199 struct drm_dp_sideband_msg_tx *raw)
201 int idx = 0;
202 int i;
203 u8 *buf = raw->msg;
204 buf[idx++] = req->req_type & 0x7f;
206 switch (req->req_type) {
207 case DP_ENUM_PATH_RESOURCES:
208 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
209 idx++;
210 break;
211 case DP_ALLOCATE_PAYLOAD:
212 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
213 (req->u.allocate_payload.number_sdp_streams & 0xf);
214 idx++;
215 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
216 idx++;
217 buf[idx] = (req->u.allocate_payload.pbn >> 8);
218 idx++;
219 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
220 idx++;
221 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
222 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
223 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
224 idx++;
226 if (req->u.allocate_payload.number_sdp_streams & 1) {
227 i = req->u.allocate_payload.number_sdp_streams - 1;
228 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
229 idx++;
231 break;
232 case DP_QUERY_PAYLOAD:
233 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
234 idx++;
235 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
236 idx++;
237 break;
238 case DP_REMOTE_DPCD_READ:
239 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
240 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
241 idx++;
242 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
243 idx++;
244 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
245 idx++;
246 buf[idx] = (req->u.dpcd_read.num_bytes);
247 idx++;
248 break;
250 case DP_REMOTE_DPCD_WRITE:
251 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
252 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
253 idx++;
254 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
255 idx++;
256 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
257 idx++;
258 buf[idx] = (req->u.dpcd_write.num_bytes);
259 idx++;
260 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
261 idx += req->u.dpcd_write.num_bytes;
262 break;
263 case DP_REMOTE_I2C_READ:
264 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
265 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
266 idx++;
267 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
268 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
269 idx++;
270 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
271 idx++;
272 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
273 idx += req->u.i2c_read.transactions[i].num_bytes;
275 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
276 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
277 idx++;
279 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
280 idx++;
281 buf[idx] = (req->u.i2c_read.num_bytes_read);
282 idx++;
283 break;
285 case DP_REMOTE_I2C_WRITE:
286 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
287 idx++;
288 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
289 idx++;
290 buf[idx] = (req->u.i2c_write.num_bytes);
291 idx++;
292 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
293 idx += req->u.i2c_write.num_bytes;
294 break;
296 raw->cur_len = idx;
299 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
301 u8 crc4;
302 crc4 = drm_dp_msg_data_crc4(msg, len);
303 msg[len] = crc4;
306 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
307 struct drm_dp_sideband_msg_tx *raw)
309 int idx = 0;
310 u8 *buf = raw->msg;
312 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
314 raw->cur_len = idx;
317 /* this adds a chunk of msg to the builder to get the final msg */
318 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
319 u8 *replybuf, u8 replybuflen, bool hdr)
321 int ret;
322 u8 crc4;
324 if (hdr) {
325 u8 hdrlen;
326 struct drm_dp_sideband_msg_hdr recv_hdr;
327 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
328 if (ret == false) {
329 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
330 return false;
333 /* get length contained in this portion */
334 msg->curchunk_len = recv_hdr.msg_len;
335 msg->curchunk_hdrlen = hdrlen;
337 /* we have already gotten an somt - don't bother parsing */
338 if (recv_hdr.somt && msg->have_somt)
339 return false;
341 if (recv_hdr.somt) {
342 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
343 msg->have_somt = true;
345 if (recv_hdr.eomt)
346 msg->have_eomt = true;
348 /* copy the bytes for the remainder of this header chunk */
349 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
350 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
351 } else {
352 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
353 msg->curchunk_idx += replybuflen;
356 if (msg->curchunk_idx >= msg->curchunk_len) {
357 /* do CRC */
358 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
359 /* copy chunk into bigger msg */
360 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
361 msg->curlen += msg->curchunk_len - 1;
363 return true;
366 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
367 struct drm_dp_sideband_msg_reply_body *repmsg)
369 int idx = 1;
370 int i;
371 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
372 idx += 16;
373 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
374 idx++;
375 if (idx > raw->curlen)
376 goto fail_len;
377 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
378 if (raw->msg[idx] & 0x80)
379 repmsg->u.link_addr.ports[i].input_port = 1;
381 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
382 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
384 idx++;
385 if (idx > raw->curlen)
386 goto fail_len;
387 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
388 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
389 if (repmsg->u.link_addr.ports[i].input_port == 0)
390 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
391 idx++;
392 if (idx > raw->curlen)
393 goto fail_len;
394 if (repmsg->u.link_addr.ports[i].input_port == 0) {
395 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
396 idx++;
397 if (idx > raw->curlen)
398 goto fail_len;
399 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
400 idx += 16;
401 if (idx > raw->curlen)
402 goto fail_len;
403 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
404 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
405 idx++;
408 if (idx > raw->curlen)
409 goto fail_len;
412 return true;
413 fail_len:
414 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
415 return false;
418 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
419 struct drm_dp_sideband_msg_reply_body *repmsg)
421 int idx = 1;
422 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
423 idx++;
424 if (idx > raw->curlen)
425 goto fail_len;
426 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
427 if (idx > raw->curlen)
428 goto fail_len;
430 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
431 return true;
432 fail_len:
433 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
434 return false;
437 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
438 struct drm_dp_sideband_msg_reply_body *repmsg)
440 int idx = 1;
441 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
442 idx++;
443 if (idx > raw->curlen)
444 goto fail_len;
445 return true;
446 fail_len:
447 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
448 return false;
451 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
452 struct drm_dp_sideband_msg_reply_body *repmsg)
454 int idx = 1;
456 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
457 idx++;
458 if (idx > raw->curlen)
459 goto fail_len;
460 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
461 idx++;
462 /* TODO check */
463 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
464 return true;
465 fail_len:
466 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
467 return false;
470 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
471 struct drm_dp_sideband_msg_reply_body *repmsg)
473 int idx = 1;
474 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
475 idx++;
476 if (idx > raw->curlen)
477 goto fail_len;
478 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
479 idx += 2;
480 if (idx > raw->curlen)
481 goto fail_len;
482 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
483 idx += 2;
484 if (idx > raw->curlen)
485 goto fail_len;
486 return true;
487 fail_len:
488 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
489 return false;
492 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
493 struct drm_dp_sideband_msg_reply_body *repmsg)
495 int idx = 1;
496 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
497 idx++;
498 if (idx > raw->curlen)
499 goto fail_len;
500 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
501 idx++;
502 if (idx > raw->curlen)
503 goto fail_len;
504 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
505 idx += 2;
506 if (idx > raw->curlen)
507 goto fail_len;
508 return true;
509 fail_len:
510 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
511 return false;
514 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
515 struct drm_dp_sideband_msg_reply_body *repmsg)
517 int idx = 1;
518 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
519 idx++;
520 if (idx > raw->curlen)
521 goto fail_len;
522 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
523 idx += 2;
524 if (idx > raw->curlen)
525 goto fail_len;
526 return true;
527 fail_len:
528 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
529 return false;
532 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
533 struct drm_dp_sideband_msg_reply_body *msg)
535 memset(msg, 0, sizeof(*msg));
536 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
537 msg->req_type = (raw->msg[0] & 0x7f);
539 if (msg->reply_type) {
540 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
541 msg->u.nak.reason = raw->msg[17];
542 msg->u.nak.nak_data = raw->msg[18];
543 return false;
546 switch (msg->req_type) {
547 case DP_LINK_ADDRESS:
548 return drm_dp_sideband_parse_link_address(raw, msg);
549 case DP_QUERY_PAYLOAD:
550 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
551 case DP_REMOTE_DPCD_READ:
552 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
553 case DP_REMOTE_DPCD_WRITE:
554 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
555 case DP_REMOTE_I2C_READ:
556 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
557 case DP_ENUM_PATH_RESOURCES:
558 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
559 case DP_ALLOCATE_PAYLOAD:
560 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
561 default:
562 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
563 return false;
567 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
568 struct drm_dp_sideband_msg_req_body *msg)
570 int idx = 1;
572 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
573 idx++;
574 if (idx > raw->curlen)
575 goto fail_len;
577 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
578 idx += 16;
579 if (idx > raw->curlen)
580 goto fail_len;
582 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
583 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
584 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
585 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
586 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
587 idx++;
588 return true;
589 fail_len:
590 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
591 return false;
594 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
595 struct drm_dp_sideband_msg_req_body *msg)
597 int idx = 1;
599 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
600 idx++;
601 if (idx > raw->curlen)
602 goto fail_len;
604 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
605 idx += 16;
606 if (idx > raw->curlen)
607 goto fail_len;
609 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
610 idx++;
611 return true;
612 fail_len:
613 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
614 return false;
617 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
618 struct drm_dp_sideband_msg_req_body *msg)
620 memset(msg, 0, sizeof(*msg));
621 msg->req_type = (raw->msg[0] & 0x7f);
623 switch (msg->req_type) {
624 case DP_CONNECTION_STATUS_NOTIFY:
625 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
626 case DP_RESOURCE_STATUS_NOTIFY:
627 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
628 default:
629 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
630 return false;
634 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
636 struct drm_dp_sideband_msg_req_body req;
638 req.req_type = DP_REMOTE_DPCD_WRITE;
639 req.u.dpcd_write.port_number = port_num;
640 req.u.dpcd_write.dpcd_address = offset;
641 req.u.dpcd_write.num_bytes = num_bytes;
642 req.u.dpcd_write.bytes = bytes;
643 drm_dp_encode_sideband_req(&req, msg);
645 return 0;
648 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
650 struct drm_dp_sideband_msg_req_body req;
652 req.req_type = DP_LINK_ADDRESS;
653 drm_dp_encode_sideband_req(&req, msg);
654 return 0;
657 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
659 struct drm_dp_sideband_msg_req_body req;
661 req.req_type = DP_ENUM_PATH_RESOURCES;
662 req.u.port_num.port_number = port_num;
663 drm_dp_encode_sideband_req(&req, msg);
664 msg->path_msg = true;
665 return 0;
668 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
669 u8 vcpi, uint16_t pbn)
671 struct drm_dp_sideband_msg_req_body req;
672 memset(&req, 0, sizeof(req));
673 req.req_type = DP_ALLOCATE_PAYLOAD;
674 req.u.allocate_payload.port_number = port_num;
675 req.u.allocate_payload.vcpi = vcpi;
676 req.u.allocate_payload.pbn = pbn;
677 drm_dp_encode_sideband_req(&req, msg);
678 msg->path_msg = true;
679 return 0;
682 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
683 struct drm_dp_vcpi *vcpi)
685 int ret, vcpi_ret;
687 mutex_lock(&mgr->payload_lock);
688 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
689 if (ret > mgr->max_payloads) {
690 ret = -EINVAL;
691 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
692 goto out_unlock;
695 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
696 if (vcpi_ret > mgr->max_payloads) {
697 ret = -EINVAL;
698 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
699 goto out_unlock;
702 set_bit(ret, &mgr->payload_mask);
703 set_bit(vcpi_ret, &mgr->vcpi_mask);
704 vcpi->vcpi = vcpi_ret + 1;
705 mgr->proposed_vcpis[ret - 1] = vcpi;
706 out_unlock:
707 mutex_unlock(&mgr->payload_lock);
708 return ret;
711 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
712 int vcpi)
714 int i;
715 if (vcpi == 0)
716 return;
718 mutex_lock(&mgr->payload_lock);
719 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
720 clear_bit(vcpi - 1, &mgr->vcpi_mask);
722 for (i = 0; i < mgr->max_payloads; i++) {
723 if (mgr->proposed_vcpis[i])
724 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
725 mgr->proposed_vcpis[i] = NULL;
726 clear_bit(i + 1, &mgr->payload_mask);
729 mutex_unlock(&mgr->payload_lock);
732 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
733 struct drm_dp_sideband_msg_tx *txmsg)
735 bool ret;
738 * All updates to txmsg->state are protected by mgr->qlock, and the two
739 * cases we check here are terminal states. For those the barriers
740 * provided by the wake_up/wait_event pair are enough.
742 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
743 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
744 return ret;
747 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
748 struct drm_dp_sideband_msg_tx *txmsg)
750 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
751 int ret;
753 ret = wait_event_timeout(mgr->tx_waitq,
754 check_txmsg_state(mgr, txmsg),
755 (4 * HZ));
756 mutex_lock(&mstb->mgr->qlock);
757 if (ret > 0) {
758 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
759 ret = -EIO;
760 goto out;
762 } else {
763 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
765 /* dump some state */
766 ret = -EIO;
768 /* remove from q */
769 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
770 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
771 list_del(&txmsg->next);
774 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
775 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
776 mstb->tx_slots[txmsg->seqno] = NULL;
779 out:
780 mutex_unlock(&mgr->qlock);
782 return ret;
785 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
787 struct drm_dp_mst_branch *mstb;
789 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
790 if (!mstb)
791 return NULL;
793 mstb->lct = lct;
794 if (lct > 1)
795 memcpy(mstb->rad, rad, lct / 2);
796 INIT_LIST_HEAD(&mstb->ports);
797 kref_init(&mstb->kref);
798 return mstb;
801 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
803 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
804 struct drm_dp_mst_port *port, *tmp;
805 bool wake_tx = false;
807 cancel_work_sync(&mstb->mgr->work);
810 * destroy all ports - don't need lock
811 * as there are no more references to the mst branch
812 * device at this point.
814 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
815 list_del(&port->next);
816 drm_dp_put_port(port);
819 /* drop any tx slots msg */
820 mutex_lock(&mstb->mgr->qlock);
821 if (mstb->tx_slots[0]) {
822 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
823 mstb->tx_slots[0] = NULL;
824 wake_tx = true;
826 if (mstb->tx_slots[1]) {
827 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
828 mstb->tx_slots[1] = NULL;
829 wake_tx = true;
831 mutex_unlock(&mstb->mgr->qlock);
833 if (wake_tx)
834 wake_up(&mstb->mgr->tx_waitq);
835 kfree(mstb);
838 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
840 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
844 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
846 struct drm_dp_mst_branch *mstb;
848 switch (old_pdt) {
849 case DP_PEER_DEVICE_DP_LEGACY_CONV:
850 case DP_PEER_DEVICE_SST_SINK:
851 /* remove i2c over sideband */
852 drm_dp_mst_unregister_i2c_bus(&port->aux);
853 break;
854 case DP_PEER_DEVICE_MST_BRANCHING:
855 mstb = port->mstb;
856 port->mstb = NULL;
857 drm_dp_put_mst_branch_device(mstb);
858 break;
862 static void drm_dp_destroy_port(struct kref *kref)
864 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
865 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
866 if (!port->input) {
867 port->vcpi.num_slots = 0;
869 kfree(port->cached_edid);
871 /* we can't destroy the connector here, as
872 we might be holding the mode_config.mutex
873 from an EDID retrieval */
874 if (port->connector) {
875 mutex_lock(&mgr->destroy_connector_lock);
876 list_add(&port->next, &mgr->destroy_connector_list);
877 mutex_unlock(&mgr->destroy_connector_lock);
878 schedule_work(&mgr->destroy_connector_work);
879 return;
881 drm_dp_port_teardown_pdt(port, port->pdt);
883 if (!port->input && port->vcpi.vcpi > 0)
884 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
886 kfree(port);
888 (*mgr->cbs->hotplug)(mgr);
891 static void drm_dp_put_port(struct drm_dp_mst_port *port)
893 kref_put(&port->kref, drm_dp_destroy_port);
896 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)
898 struct drm_dp_mst_port *port;
899 struct drm_dp_mst_branch *rmstb;
900 if (to_find == mstb) {
901 kref_get(&mstb->kref);
902 return mstb;
904 list_for_each_entry(port, &mstb->ports, next) {
905 if (port->mstb) {
906 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
907 if (rmstb)
908 return rmstb;
911 return NULL;
914 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)
916 struct drm_dp_mst_branch *rmstb = NULL;
917 mutex_lock(&mgr->lock);
918 if (mgr->mst_primary)
919 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
920 mutex_unlock(&mgr->lock);
921 return rmstb;
924 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)
926 struct drm_dp_mst_port *port, *mport;
928 list_for_each_entry(port, &mstb->ports, next) {
929 if (port == to_find) {
930 kref_get(&port->kref);
931 return port;
933 if (port->mstb) {
934 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
935 if (mport)
936 return mport;
939 return NULL;
942 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)
944 struct drm_dp_mst_port *rport = NULL;
945 mutex_lock(&mgr->lock);
946 if (mgr->mst_primary)
947 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
948 mutex_unlock(&mgr->lock);
949 return rport;
952 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
954 struct drm_dp_mst_port *port;
956 list_for_each_entry(port, &mstb->ports, next) {
957 if (port->port_num == port_num) {
958 kref_get(&port->kref);
959 return port;
963 return NULL;
967 * calculate a new RAD for this MST branch device
968 * if parent has an LCT of 2 then it has 1 nibble of RAD,
969 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
971 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
972 u8 *rad)
974 int lct = port->parent->lct;
975 int shift = 4;
976 int idx = lct / 2;
977 if (lct > 1) {
978 memcpy(rad, port->parent->rad, idx);
979 shift = (lct % 2) ? 4 : 0;
980 } else
981 rad[0] = 0;
983 rad[idx] |= port->port_num << shift;
984 return lct + 1;
988 * return sends link address for new mstb
990 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
992 int ret;
993 u8 rad[6], lct;
994 bool send_link = false;
995 switch (port->pdt) {
996 case DP_PEER_DEVICE_DP_LEGACY_CONV:
997 case DP_PEER_DEVICE_SST_SINK:
998 /* add i2c over sideband */
999 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1000 break;
1001 case DP_PEER_DEVICE_MST_BRANCHING:
1002 lct = drm_dp_calculate_rad(port, rad);
1004 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1005 port->mstb->mgr = port->mgr;
1006 port->mstb->port_parent = port;
1008 send_link = true;
1009 break;
1011 return send_link;
1014 static void drm_dp_check_port_guid(struct drm_dp_mst_branch *mstb,
1015 struct drm_dp_mst_port *port)
1017 int ret;
1018 if (port->dpcd_rev >= 0x12) {
1019 port->guid_valid = drm_dp_validate_guid(mstb->mgr, port->guid);
1020 if (!port->guid_valid) {
1021 ret = drm_dp_send_dpcd_write(mstb->mgr,
1022 port,
1023 DP_GUID,
1024 16, port->guid);
1025 port->guid_valid = true;
1030 static void build_mst_prop_path(struct drm_dp_mst_port *port,
1031 struct drm_dp_mst_branch *mstb,
1032 char *proppath,
1033 size_t proppath_size)
1035 int i;
1036 char temp[8];
1037 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1038 for (i = 0; i < (mstb->lct - 1); i++) {
1039 int shift = (i % 2) ? 0 : 4;
1040 int port_num = mstb->rad[i / 2] >> shift;
1041 snprintf(temp, sizeof(temp), "-%d", port_num);
1042 strlcat(proppath, temp, proppath_size);
1044 snprintf(temp, sizeof(temp), "-%d", port->port_num);
1045 strlcat(proppath, temp, proppath_size);
1048 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1049 struct device *dev,
1050 struct drm_dp_link_addr_reply_port *port_msg)
1052 struct drm_dp_mst_port *port;
1053 bool ret;
1054 bool created = false;
1055 int old_pdt = 0;
1056 int old_ddps = 0;
1057 port = drm_dp_get_port(mstb, port_msg->port_number);
1058 if (!port) {
1059 port = kzalloc(sizeof(*port), GFP_KERNEL);
1060 if (!port)
1061 return;
1062 kref_init(&port->kref);
1063 port->parent = mstb;
1064 port->port_num = port_msg->port_number;
1065 port->mgr = mstb->mgr;
1066 port->aux.name = "DPMST";
1067 port->aux.dev = dev;
1068 created = true;
1069 } else {
1070 old_pdt = port->pdt;
1071 old_ddps = port->ddps;
1074 port->pdt = port_msg->peer_device_type;
1075 port->input = port_msg->input_port;
1076 port->mcs = port_msg->mcs;
1077 port->ddps = port_msg->ddps;
1078 port->ldps = port_msg->legacy_device_plug_status;
1079 port->dpcd_rev = port_msg->dpcd_revision;
1080 port->num_sdp_streams = port_msg->num_sdp_streams;
1081 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1082 memcpy(port->guid, port_msg->peer_guid, 16);
1084 /* manage mstb port lists with mgr lock - take a reference
1085 for this list */
1086 if (created) {
1087 mutex_lock(&mstb->mgr->lock);
1088 kref_get(&port->kref);
1089 list_add(&port->next, &mstb->ports);
1090 mutex_unlock(&mstb->mgr->lock);
1093 if (old_ddps != port->ddps) {
1094 if (port->ddps) {
1095 drm_dp_check_port_guid(mstb, port);
1096 if (!port->input)
1097 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1098 } else {
1099 port->guid_valid = false;
1100 port->available_pbn = 0;
1104 if (old_pdt != port->pdt && !port->input) {
1105 drm_dp_port_teardown_pdt(port, old_pdt);
1107 ret = drm_dp_port_setup_pdt(port);
1108 if (ret == true) {
1109 drm_dp_send_link_address(mstb->mgr, port->mstb);
1110 port->mstb->link_address_sent = true;
1114 if (created && !port->input) {
1115 char proppath[255];
1116 build_mst_prop_path(port, mstb, proppath, sizeof(proppath));
1117 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1119 if (port->port_num >= 8) {
1120 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1124 /* put reference to this port */
1125 drm_dp_put_port(port);
1128 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1129 struct drm_dp_connection_status_notify *conn_stat)
1131 struct drm_dp_mst_port *port;
1132 int old_pdt;
1133 int old_ddps;
1134 bool dowork = false;
1135 port = drm_dp_get_port(mstb, conn_stat->port_number);
1136 if (!port)
1137 return;
1139 old_ddps = port->ddps;
1140 old_pdt = port->pdt;
1141 port->pdt = conn_stat->peer_device_type;
1142 port->mcs = conn_stat->message_capability_status;
1143 port->ldps = conn_stat->legacy_device_plug_status;
1144 port->ddps = conn_stat->displayport_device_plug_status;
1146 if (old_ddps != port->ddps) {
1147 if (port->ddps) {
1148 drm_dp_check_port_guid(mstb, port);
1149 dowork = true;
1150 } else {
1151 port->guid_valid = false;
1152 port->available_pbn = 0;
1155 if (old_pdt != port->pdt && !port->input) {
1156 drm_dp_port_teardown_pdt(port, old_pdt);
1158 if (drm_dp_port_setup_pdt(port))
1159 dowork = true;
1162 drm_dp_put_port(port);
1163 if (dowork)
1164 queue_work(system_long_wq, &mstb->mgr->work);
1168 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1169 u8 lct, u8 *rad)
1171 struct drm_dp_mst_branch *mstb;
1172 struct drm_dp_mst_port *port;
1173 int i;
1174 /* find the port by iterating down */
1176 mutex_lock(&mgr->lock);
1177 mstb = mgr->mst_primary;
1179 for (i = 0; i < lct - 1; i++) {
1180 int shift = (i % 2) ? 0 : 4;
1181 int port_num = rad[i / 2] >> shift;
1183 list_for_each_entry(port, &mstb->ports, next) {
1184 if (port->port_num == port_num) {
1185 if (!port->mstb) {
1186 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1187 return NULL;
1190 mstb = port->mstb;
1191 break;
1195 kref_get(&mstb->kref);
1196 mutex_unlock(&mgr->lock);
1197 return mstb;
1200 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1201 struct drm_dp_mst_branch *mstb)
1203 struct drm_dp_mst_port *port;
1204 struct drm_dp_mst_branch *mstb_child;
1205 if (!mstb->link_address_sent) {
1206 drm_dp_send_link_address(mgr, mstb);
1207 mstb->link_address_sent = true;
1209 list_for_each_entry(port, &mstb->ports, next) {
1210 if (port->input)
1211 continue;
1213 if (!port->ddps)
1214 continue;
1216 if (!port->available_pbn)
1217 drm_dp_send_enum_path_resources(mgr, mstb, port);
1219 if (port->mstb) {
1220 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1221 if (mstb_child) {
1222 drm_dp_check_and_send_link_address(mgr, mstb_child);
1223 drm_dp_put_mst_branch_device(mstb_child);
1229 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1231 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1232 struct drm_dp_mst_branch *mstb;
1234 mutex_lock(&mgr->lock);
1235 mstb = mgr->mst_primary;
1236 if (mstb) {
1237 kref_get(&mstb->kref);
1239 mutex_unlock(&mgr->lock);
1240 if (mstb) {
1241 drm_dp_check_and_send_link_address(mgr, mstb);
1242 drm_dp_put_mst_branch_device(mstb);
1246 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1247 u8 *guid)
1249 static u8 zero_guid[16];
1251 if (!memcmp(guid, zero_guid, 16)) {
1252 u64 salt = get_jiffies_64();
1253 memcpy(&guid[0], &salt, sizeof(u64));
1254 memcpy(&guid[8], &salt, sizeof(u64));
1255 return false;
1257 return true;
1260 #if 0
1261 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1263 struct drm_dp_sideband_msg_req_body req;
1265 req.req_type = DP_REMOTE_DPCD_READ;
1266 req.u.dpcd_read.port_number = port_num;
1267 req.u.dpcd_read.dpcd_address = offset;
1268 req.u.dpcd_read.num_bytes = num_bytes;
1269 drm_dp_encode_sideband_req(&req, msg);
1271 return 0;
1273 #endif
1275 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1276 bool up, u8 *msg, int len)
1278 int ret;
1279 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1280 int tosend, total, offset;
1281 int retries = 0;
1283 retry:
1284 total = len;
1285 offset = 0;
1286 do {
1287 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1289 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1290 &msg[offset],
1291 tosend);
1292 if (ret != tosend) {
1293 if (ret == -EIO && retries < 5) {
1294 retries++;
1295 goto retry;
1297 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1299 return -EIO;
1301 offset += tosend;
1302 total -= tosend;
1303 } while (total > 0);
1304 return 0;
1307 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1308 struct drm_dp_sideband_msg_tx *txmsg)
1310 struct drm_dp_mst_branch *mstb = txmsg->dst;
1312 /* both msg slots are full */
1313 if (txmsg->seqno == -1) {
1314 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1315 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1316 return -EAGAIN;
1318 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1319 txmsg->seqno = mstb->last_seqno;
1320 mstb->last_seqno ^= 1;
1321 } else if (mstb->tx_slots[0] == NULL)
1322 txmsg->seqno = 0;
1323 else
1324 txmsg->seqno = 1;
1325 mstb->tx_slots[txmsg->seqno] = txmsg;
1327 hdr->broadcast = 0;
1328 hdr->path_msg = txmsg->path_msg;
1329 hdr->lct = mstb->lct;
1330 hdr->lcr = mstb->lct - 1;
1331 if (mstb->lct > 1)
1332 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1333 hdr->seqno = txmsg->seqno;
1334 return 0;
1337 * process a single block of the next message in the sideband queue
1339 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1340 struct drm_dp_sideband_msg_tx *txmsg,
1341 bool up)
1343 u8 chunk[48];
1344 struct drm_dp_sideband_msg_hdr hdr;
1345 int len, space, idx, tosend;
1346 int ret;
1348 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1350 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1351 txmsg->seqno = -1;
1352 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1355 /* make hdr from dst mst - for replies use seqno
1356 otherwise assign one */
1357 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1358 if (ret < 0)
1359 return ret;
1361 /* amount left to send in this message */
1362 len = txmsg->cur_len - txmsg->cur_offset;
1364 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1365 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1367 tosend = min(len, space);
1368 if (len == txmsg->cur_len)
1369 hdr.somt = 1;
1370 if (space >= len)
1371 hdr.eomt = 1;
1374 hdr.msg_len = tosend + 1;
1375 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1376 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1377 /* add crc at end */
1378 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1379 idx += tosend + 1;
1381 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1382 if (ret) {
1383 DRM_DEBUG_KMS("sideband msg failed to send\n");
1384 return ret;
1387 txmsg->cur_offset += tosend;
1388 if (txmsg->cur_offset == txmsg->cur_len) {
1389 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1390 return 1;
1392 return 0;
1395 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1397 struct drm_dp_sideband_msg_tx *txmsg;
1398 int ret;
1400 WARN_ON(!mutex_is_locked(&mgr->qlock));
1402 /* construct a chunk from the first msg in the tx_msg queue */
1403 if (list_empty(&mgr->tx_msg_downq)) {
1404 mgr->tx_down_in_progress = false;
1405 return;
1407 mgr->tx_down_in_progress = true;
1409 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1410 ret = process_single_tx_qlock(mgr, txmsg, false);
1411 if (ret == 1) {
1412 /* txmsg is sent it should be in the slots now */
1413 list_del(&txmsg->next);
1414 } else if (ret) {
1415 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1416 list_del(&txmsg->next);
1417 if (txmsg->seqno != -1)
1418 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1419 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1420 wake_up(&mgr->tx_waitq);
1422 if (list_empty(&mgr->tx_msg_downq)) {
1423 mgr->tx_down_in_progress = false;
1424 return;
1428 /* called holding qlock */
1429 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1431 struct drm_dp_sideband_msg_tx *txmsg;
1432 int ret;
1434 /* construct a chunk from the first msg in the tx_msg queue */
1435 if (list_empty(&mgr->tx_msg_upq)) {
1436 mgr->tx_up_in_progress = false;
1437 return;
1440 txmsg = list_first_entry(&mgr->tx_msg_upq, struct drm_dp_sideband_msg_tx, next);
1441 ret = process_single_tx_qlock(mgr, txmsg, true);
1442 if (ret == 1) {
1443 /* up txmsgs aren't put in slots - so free after we send it */
1444 list_del(&txmsg->next);
1445 kfree(txmsg);
1446 } else if (ret)
1447 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1448 mgr->tx_up_in_progress = true;
1451 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1452 struct drm_dp_sideband_msg_tx *txmsg)
1454 mutex_lock(&mgr->qlock);
1455 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1456 if (!mgr->tx_down_in_progress)
1457 process_single_down_tx_qlock(mgr);
1458 mutex_unlock(&mgr->qlock);
1461 static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1462 struct drm_dp_mst_branch *mstb)
1464 int len;
1465 struct drm_dp_sideband_msg_tx *txmsg;
1466 int ret;
1468 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1469 if (!txmsg)
1470 return -ENOMEM;
1472 txmsg->dst = mstb;
1473 len = build_link_address(txmsg);
1475 drm_dp_queue_down_tx(mgr, txmsg);
1477 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1478 if (ret > 0) {
1479 int i;
1481 if (txmsg->reply.reply_type == 1)
1482 DRM_DEBUG_KMS("link address nak received\n");
1483 else {
1484 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1485 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1486 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,
1487 txmsg->reply.u.link_addr.ports[i].input_port,
1488 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1489 txmsg->reply.u.link_addr.ports[i].port_number,
1490 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1491 txmsg->reply.u.link_addr.ports[i].mcs,
1492 txmsg->reply.u.link_addr.ports[i].ddps,
1493 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1494 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1495 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1497 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1498 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1500 (*mgr->cbs->hotplug)(mgr);
1502 } else
1503 DRM_DEBUG_KMS("link address failed %d\n", ret);
1505 kfree(txmsg);
1506 return 0;
1509 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1510 struct drm_dp_mst_branch *mstb,
1511 struct drm_dp_mst_port *port)
1513 int len;
1514 struct drm_dp_sideband_msg_tx *txmsg;
1515 int ret;
1517 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1518 if (!txmsg)
1519 return -ENOMEM;
1521 txmsg->dst = mstb;
1522 len = build_enum_path_resources(txmsg, port->port_num);
1524 drm_dp_queue_down_tx(mgr, txmsg);
1526 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1527 if (ret > 0) {
1528 if (txmsg->reply.reply_type == 1)
1529 DRM_DEBUG_KMS("enum path resources nak received\n");
1530 else {
1531 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1532 DRM_ERROR("got incorrect port in response\n");
1533 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,
1534 txmsg->reply.u.path_resources.avail_payload_bw_number);
1535 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1539 kfree(txmsg);
1540 return 0;
1543 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1544 struct drm_dp_mst_port *port,
1545 int id,
1546 int pbn)
1548 struct drm_dp_sideband_msg_tx *txmsg;
1549 struct drm_dp_mst_branch *mstb;
1550 int len, ret;
1552 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1553 if (!mstb)
1554 return -EINVAL;
1556 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1557 if (!txmsg) {
1558 ret = -ENOMEM;
1559 goto fail_put;
1562 txmsg->dst = mstb;
1563 len = build_allocate_payload(txmsg, port->port_num,
1565 pbn);
1567 drm_dp_queue_down_tx(mgr, txmsg);
1569 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1570 if (ret > 0) {
1571 if (txmsg->reply.reply_type == 1) {
1572 ret = -EINVAL;
1573 } else
1574 ret = 0;
1576 kfree(txmsg);
1577 fail_put:
1578 drm_dp_put_mst_branch_device(mstb);
1579 return ret;
1582 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1583 int id,
1584 struct drm_dp_payload *payload)
1586 int ret;
1588 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1589 if (ret < 0) {
1590 payload->payload_state = 0;
1591 return ret;
1593 payload->payload_state = DP_PAYLOAD_LOCAL;
1594 return 0;
1597 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1598 struct drm_dp_mst_port *port,
1599 int id,
1600 struct drm_dp_payload *payload)
1602 int ret;
1603 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1604 if (ret < 0)
1605 return ret;
1606 payload->payload_state = DP_PAYLOAD_REMOTE;
1607 return ret;
1610 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1611 struct drm_dp_mst_port *port,
1612 int id,
1613 struct drm_dp_payload *payload)
1615 DRM_DEBUG_KMS("\n");
1616 /* its okay for these to fail */
1617 if (port) {
1618 drm_dp_payload_send_msg(mgr, port, id, 0);
1621 drm_dp_dpcd_write_payload(mgr, id, payload);
1622 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
1623 return 0;
1626 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1627 int id,
1628 struct drm_dp_payload *payload)
1630 payload->payload_state = 0;
1631 return 0;
1635 * drm_dp_update_payload_part1() - Execute payload update part 1
1636 * @mgr: manager to use.
1638 * This iterates over all proposed virtual channels, and tries to
1639 * allocate space in the link for them. For 0->slots transitions,
1640 * this step just writes the VCPI to the MST device. For slots->0
1641 * transitions, this writes the updated VCPIs and removes the
1642 * remote VC payloads.
1644 * after calling this the driver should generate ACT and payload
1645 * packets.
1647 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1649 int i, j;
1650 int cur_slots = 1;
1651 struct drm_dp_payload req_payload;
1652 struct drm_dp_mst_port *port;
1654 mutex_lock(&mgr->payload_lock);
1655 for (i = 0; i < mgr->max_payloads; i++) {
1656 /* solve the current payloads - compare to the hw ones
1657 - update the hw view */
1658 req_payload.start_slot = cur_slots;
1659 if (mgr->proposed_vcpis[i]) {
1660 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1661 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1662 } else {
1663 port = NULL;
1664 req_payload.num_slots = 0;
1667 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1668 mgr->payloads[i].start_slot = req_payload.start_slot;
1670 /* work out what is required to happen with this payload */
1671 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
1673 /* need to push an update for this payload */
1674 if (req_payload.num_slots) {
1675 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
1676 mgr->payloads[i].num_slots = req_payload.num_slots;
1677 } else if (mgr->payloads[i].num_slots) {
1678 mgr->payloads[i].num_slots = 0;
1679 drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]);
1680 req_payload.payload_state = mgr->payloads[i].payload_state;
1681 mgr->payloads[i].start_slot = 0;
1683 mgr->payloads[i].payload_state = req_payload.payload_state;
1685 cur_slots += req_payload.num_slots;
1688 for (i = 0; i < mgr->max_payloads; i++) {
1689 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1690 DRM_DEBUG_KMS("removing payload %d\n", i);
1691 for (j = i; j < mgr->max_payloads - 1; j++) {
1692 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1693 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1694 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1695 set_bit(j + 1, &mgr->payload_mask);
1696 } else {
1697 clear_bit(j + 1, &mgr->payload_mask);
1700 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1701 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1702 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1706 mutex_unlock(&mgr->payload_lock);
1708 return 0;
1710 EXPORT_SYMBOL(drm_dp_update_payload_part1);
1713 * drm_dp_update_payload_part2() - Execute payload update part 2
1714 * @mgr: manager to use.
1716 * This iterates over all proposed virtual channels, and tries to
1717 * allocate space in the link for them. For 0->slots transitions,
1718 * this step writes the remote VC payload commands. For slots->0
1719 * this just resets some internal state.
1721 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1723 struct drm_dp_mst_port *port;
1724 int i;
1725 int ret = 0;
1726 mutex_lock(&mgr->payload_lock);
1727 for (i = 0; i < mgr->max_payloads; i++) {
1729 if (!mgr->proposed_vcpis[i])
1730 continue;
1732 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1734 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1735 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1736 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1737 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1738 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1740 if (ret) {
1741 mutex_unlock(&mgr->payload_lock);
1742 return ret;
1745 mutex_unlock(&mgr->payload_lock);
1746 return 0;
1748 EXPORT_SYMBOL(drm_dp_update_payload_part2);
1750 #if 0 /* unused as of yet */
1751 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1752 struct drm_dp_mst_port *port,
1753 int offset, int size)
1755 int len;
1756 struct drm_dp_sideband_msg_tx *txmsg;
1758 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1759 if (!txmsg)
1760 return -ENOMEM;
1762 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1763 txmsg->dst = port->parent;
1765 drm_dp_queue_down_tx(mgr, txmsg);
1767 return 0;
1769 #endif
1771 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1772 struct drm_dp_mst_port *port,
1773 int offset, int size, u8 *bytes)
1775 int len;
1776 int ret;
1777 struct drm_dp_sideband_msg_tx *txmsg;
1778 struct drm_dp_mst_branch *mstb;
1780 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1781 if (!mstb)
1782 return -EINVAL;
1784 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1785 if (!txmsg) {
1786 ret = -ENOMEM;
1787 goto fail_put;
1790 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1791 txmsg->dst = mstb;
1793 drm_dp_queue_down_tx(mgr, txmsg);
1795 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1796 if (ret > 0) {
1797 if (txmsg->reply.reply_type == 1) {
1798 ret = -EINVAL;
1799 } else
1800 ret = 0;
1802 kfree(txmsg);
1803 fail_put:
1804 drm_dp_put_mst_branch_device(mstb);
1805 return ret;
1808 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1810 struct drm_dp_sideband_msg_reply_body reply;
1812 reply.reply_type = 1;
1813 reply.req_type = req_type;
1814 drm_dp_encode_sideband_reply(&reply, msg);
1815 return 0;
1818 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1819 struct drm_dp_mst_branch *mstb,
1820 int req_type, int seqno, bool broadcast)
1822 struct drm_dp_sideband_msg_tx *txmsg;
1824 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1825 if (!txmsg)
1826 return -ENOMEM;
1828 txmsg->dst = mstb;
1829 txmsg->seqno = seqno;
1830 drm_dp_encode_up_ack_reply(txmsg, req_type);
1832 mutex_lock(&mgr->qlock);
1833 list_add_tail(&txmsg->next, &mgr->tx_msg_upq);
1834 if (!mgr->tx_up_in_progress) {
1835 process_single_up_tx_qlock(mgr);
1837 mutex_unlock(&mgr->qlock);
1838 return 0;
1841 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1842 int dp_link_count,
1843 int *out)
1845 switch (dp_link_bw) {
1846 default:
1847 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1848 dp_link_bw, dp_link_count);
1849 return false;
1851 case DP_LINK_BW_1_62:
1852 *out = 3 * dp_link_count;
1853 break;
1854 case DP_LINK_BW_2_7:
1855 *out = 5 * dp_link_count;
1856 break;
1857 case DP_LINK_BW_5_4:
1858 *out = 10 * dp_link_count;
1859 break;
1861 return true;
1865 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
1866 * @mgr: manager to set state for
1867 * @mst_state: true to enable MST on this connector - false to disable.
1869 * This is called by the driver when it detects an MST capable device plugged
1870 * into a DP MST capable port, or when a DP MST capable device is unplugged.
1872 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
1874 int ret = 0;
1875 struct drm_dp_mst_branch *mstb = NULL;
1877 mutex_lock(&mgr->lock);
1878 if (mst_state == mgr->mst_state)
1879 goto out_unlock;
1881 mgr->mst_state = mst_state;
1882 /* set the device into MST mode */
1883 if (mst_state) {
1884 WARN_ON(mgr->mst_primary);
1886 /* get dpcd info */
1887 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
1888 if (ret != DP_RECEIVER_CAP_SIZE) {
1889 DRM_DEBUG_KMS("failed to read DPCD\n");
1890 goto out_unlock;
1893 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
1894 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
1895 &mgr->pbn_div)) {
1896 ret = -EINVAL;
1897 goto out_unlock;
1900 mgr->total_pbn = 2560;
1901 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
1902 mgr->avail_slots = mgr->total_slots;
1904 /* add initial branch device at LCT 1 */
1905 mstb = drm_dp_add_mst_branch_device(1, NULL);
1906 if (mstb == NULL) {
1907 ret = -ENOMEM;
1908 goto out_unlock;
1910 mstb->mgr = mgr;
1912 /* give this the main reference */
1913 mgr->mst_primary = mstb;
1914 kref_get(&mgr->mst_primary->kref);
1917 struct drm_dp_payload reset_pay;
1918 reset_pay.start_slot = 0;
1919 reset_pay.num_slots = 0x3f;
1920 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
1923 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1924 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
1925 if (ret < 0) {
1926 goto out_unlock;
1930 /* sort out guid */
1931 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, mgr->guid, 16);
1932 if (ret != 16) {
1933 DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret);
1934 goto out_unlock;
1937 mgr->guid_valid = drm_dp_validate_guid(mgr, mgr->guid);
1938 if (!mgr->guid_valid) {
1939 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, mgr->guid, 16);
1940 mgr->guid_valid = true;
1943 queue_work(system_long_wq, &mgr->work);
1945 ret = 0;
1946 } else {
1947 /* disable MST on the device */
1948 mstb = mgr->mst_primary;
1949 mgr->mst_primary = NULL;
1950 /* this can fail if the device is gone */
1951 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
1952 ret = 0;
1953 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
1954 mgr->payload_mask = 0;
1955 set_bit(0, &mgr->payload_mask);
1956 mgr->vcpi_mask = 0;
1959 out_unlock:
1960 mutex_unlock(&mgr->lock);
1961 if (mstb)
1962 drm_dp_put_mst_branch_device(mstb);
1963 return ret;
1966 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
1969 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
1970 * @mgr: manager to suspend
1972 * This function tells the MST device that we can't handle UP messages
1973 * anymore. This should stop it from sending any since we are suspended.
1975 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
1977 mutex_lock(&mgr->lock);
1978 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1979 DP_MST_EN | DP_UPSTREAM_IS_SRC);
1980 mutex_unlock(&mgr->lock);
1982 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
1985 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
1986 * @mgr: manager to resume
1988 * This will fetch DPCD and see if the device is still there,
1989 * if it is, it will rewrite the MSTM control bits, and return.
1991 * if the device fails this returns -1, and the driver should do
1992 * a full MST reprobe, in case we were undocked.
1994 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
1996 int ret = 0;
1998 mutex_lock(&mgr->lock);
2000 if (mgr->mst_primary) {
2001 int sret;
2002 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2003 if (sret != DP_RECEIVER_CAP_SIZE) {
2004 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2005 ret = -1;
2006 goto out_unlock;
2009 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2010 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2011 if (ret < 0) {
2012 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2013 ret = -1;
2014 goto out_unlock;
2016 ret = 0;
2017 } else
2018 ret = -1;
2020 out_unlock:
2021 mutex_unlock(&mgr->lock);
2022 return ret;
2024 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2026 static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2028 int len;
2029 u8 replyblock[32];
2030 int replylen, origlen, curreply;
2031 int ret;
2032 struct drm_dp_sideband_msg_rx *msg;
2033 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2034 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2036 len = min(mgr->max_dpcd_transaction_bytes, 16);
2037 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2038 replyblock, len);
2039 if (ret != len) {
2040 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2041 return;
2043 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2044 if (!ret) {
2045 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2046 return;
2048 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2050 origlen = replylen;
2051 replylen -= len;
2052 curreply = len;
2053 while (replylen > 0) {
2054 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2055 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2056 replyblock, len);
2057 if (ret != len) {
2058 DRM_DEBUG_KMS("failed to read a chunk\n");
2060 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2061 if (ret == false)
2062 DRM_DEBUG_KMS("failed to build sideband msg\n");
2063 curreply += len;
2064 replylen -= len;
2068 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2070 int ret = 0;
2072 drm_dp_get_one_sb_msg(mgr, false);
2074 if (mgr->down_rep_recv.have_eomt) {
2075 struct drm_dp_sideband_msg_tx *txmsg;
2076 struct drm_dp_mst_branch *mstb;
2077 int slot = -1;
2078 mstb = drm_dp_get_mst_branch_device(mgr,
2079 mgr->down_rep_recv.initial_hdr.lct,
2080 mgr->down_rep_recv.initial_hdr.rad);
2082 if (!mstb) {
2083 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2084 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2085 return 0;
2088 /* find the message */
2089 slot = mgr->down_rep_recv.initial_hdr.seqno;
2090 mutex_lock(&mgr->qlock);
2091 txmsg = mstb->tx_slots[slot];
2092 /* remove from slots */
2093 mutex_unlock(&mgr->qlock);
2095 if (!txmsg) {
2096 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2097 mstb,
2098 mgr->down_rep_recv.initial_hdr.seqno,
2099 mgr->down_rep_recv.initial_hdr.lct,
2100 mgr->down_rep_recv.initial_hdr.rad[0],
2101 mgr->down_rep_recv.msg[0]);
2102 drm_dp_put_mst_branch_device(mstb);
2103 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2104 return 0;
2107 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2108 if (txmsg->reply.reply_type == 1) {
2109 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);
2112 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2113 drm_dp_put_mst_branch_device(mstb);
2115 mutex_lock(&mgr->qlock);
2116 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2117 mstb->tx_slots[slot] = NULL;
2118 mutex_unlock(&mgr->qlock);
2120 wake_up(&mgr->tx_waitq);
2122 return ret;
2125 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2127 int ret = 0;
2128 drm_dp_get_one_sb_msg(mgr, true);
2130 if (mgr->up_req_recv.have_eomt) {
2131 struct drm_dp_sideband_msg_req_body msg;
2132 struct drm_dp_mst_branch *mstb;
2133 bool seqno;
2134 mstb = drm_dp_get_mst_branch_device(mgr,
2135 mgr->up_req_recv.initial_hdr.lct,
2136 mgr->up_req_recv.initial_hdr.rad);
2137 if (!mstb) {
2138 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2139 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2140 return 0;
2143 seqno = mgr->up_req_recv.initial_hdr.seqno;
2144 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2146 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2147 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
2148 drm_dp_update_port(mstb, &msg.u.conn_stat);
2149 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);
2150 (*mgr->cbs->hotplug)(mgr);
2152 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2153 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
2154 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2157 drm_dp_put_mst_branch_device(mstb);
2158 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2160 return ret;
2164 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2165 * @mgr: manager to notify irq for.
2166 * @esi: 4 bytes from SINK_COUNT_ESI
2167 * @handled: whether the hpd interrupt was consumed or not
2169 * This should be called from the driver when it detects a short IRQ,
2170 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2171 * topology manager will process the sideband messages received as a result
2172 * of this.
2174 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2176 int ret = 0;
2177 int sc;
2178 *handled = false;
2179 sc = esi[0] & 0x3f;
2181 if (sc != mgr->sink_count) {
2182 mgr->sink_count = sc;
2183 *handled = true;
2186 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2187 ret = drm_dp_mst_handle_down_rep(mgr);
2188 *handled = true;
2191 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2192 ret |= drm_dp_mst_handle_up_req(mgr);
2193 *handled = true;
2196 drm_dp_mst_kick_tx(mgr);
2197 return ret;
2199 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2202 * drm_dp_mst_detect_port() - get connection status for an MST port
2203 * @mgr: manager for this port
2204 * @port: unverified pointer to a port
2206 * This returns the current connection state for a port. It validates the
2207 * port pointer still exists so the caller doesn't require a reference
2209 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2210 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2212 enum drm_connector_status status = connector_status_disconnected;
2214 /* we need to search for the port in the mgr in case its gone */
2215 port = drm_dp_get_validated_port_ref(mgr, port);
2216 if (!port)
2217 return connector_status_disconnected;
2219 if (!port->ddps)
2220 goto out;
2222 switch (port->pdt) {
2223 case DP_PEER_DEVICE_NONE:
2224 case DP_PEER_DEVICE_MST_BRANCHING:
2225 break;
2227 case DP_PEER_DEVICE_SST_SINK:
2228 status = connector_status_connected;
2229 /* for logical ports - cache the EDID */
2230 if (port->port_num >= 8 && !port->cached_edid) {
2231 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2233 break;
2234 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2235 if (port->ldps)
2236 status = connector_status_connected;
2237 break;
2239 out:
2240 drm_dp_put_port(port);
2241 return status;
2243 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2246 * drm_dp_mst_get_edid() - get EDID for an MST port
2247 * @connector: toplevel connector to get EDID for
2248 * @mgr: manager for this port
2249 * @port: unverified pointer to a port.
2251 * This returns an EDID for the port connected to a connector,
2252 * It validates the pointer still exists so the caller doesn't require a
2253 * reference.
2255 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2257 struct edid *edid = NULL;
2259 /* we need to search for the port in the mgr in case its gone */
2260 port = drm_dp_get_validated_port_ref(mgr, port);
2261 if (!port)
2262 return NULL;
2264 if (port->cached_edid)
2265 edid = drm_edid_duplicate(port->cached_edid);
2266 else
2267 edid = drm_get_edid(connector, &port->aux.ddc);
2269 drm_mode_connector_set_tile_property(connector);
2270 drm_dp_put_port(port);
2271 return edid;
2273 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2276 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2277 * @mgr: manager to use
2278 * @pbn: payload bandwidth to convert into slots.
2280 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2281 int pbn)
2283 int num_slots;
2285 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2287 if (num_slots > mgr->avail_slots)
2288 return -ENOSPC;
2289 return num_slots;
2291 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2293 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2294 struct drm_dp_vcpi *vcpi, int pbn)
2296 int num_slots;
2297 int ret;
2299 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2301 if (num_slots > mgr->avail_slots)
2302 return -ENOSPC;
2304 vcpi->pbn = pbn;
2305 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2306 vcpi->num_slots = num_slots;
2308 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2309 if (ret < 0)
2310 return ret;
2311 return 0;
2315 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2316 * @mgr: manager for this port
2317 * @port: port to allocate a virtual channel for.
2318 * @pbn: payload bandwidth number to request
2319 * @slots: returned number of slots for this PBN.
2321 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2323 int ret;
2325 port = drm_dp_get_validated_port_ref(mgr, port);
2326 if (!port)
2327 return false;
2329 if (port->vcpi.vcpi > 0) {
2330 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2331 if (pbn == port->vcpi.pbn) {
2332 *slots = port->vcpi.num_slots;
2333 return true;
2337 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2338 if (ret) {
2339 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2340 goto out;
2342 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2343 *slots = port->vcpi.num_slots;
2345 drm_dp_put_port(port);
2346 return true;
2347 out:
2348 return false;
2350 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2352 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2354 int slots = 0;
2355 port = drm_dp_get_validated_port_ref(mgr, port);
2356 if (!port)
2357 return slots;
2359 slots = port->vcpi.num_slots;
2360 drm_dp_put_port(port);
2361 return slots;
2363 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2366 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2367 * @mgr: manager for this port
2368 * @port: unverified pointer to a port.
2370 * This just resets the number of slots for the ports VCPI for later programming.
2372 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2374 port = drm_dp_get_validated_port_ref(mgr, port);
2375 if (!port)
2376 return;
2377 port->vcpi.num_slots = 0;
2378 drm_dp_put_port(port);
2380 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2383 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2384 * @mgr: manager for this port
2385 * @port: unverified port to deallocate vcpi for
2387 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2389 port = drm_dp_get_validated_port_ref(mgr, port);
2390 if (!port)
2391 return;
2393 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2394 port->vcpi.num_slots = 0;
2395 port->vcpi.pbn = 0;
2396 port->vcpi.aligned_pbn = 0;
2397 port->vcpi.vcpi = 0;
2398 drm_dp_put_port(port);
2400 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2402 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2403 int id, struct drm_dp_payload *payload)
2405 u8 payload_alloc[3], status;
2406 int ret;
2407 int retries = 0;
2409 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2410 DP_PAYLOAD_TABLE_UPDATED);
2412 payload_alloc[0] = id;
2413 payload_alloc[1] = payload->start_slot;
2414 payload_alloc[2] = payload->num_slots;
2416 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2417 if (ret != 3) {
2418 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2419 goto fail;
2422 retry:
2423 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2424 if (ret < 0) {
2425 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2426 goto fail;
2429 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2430 retries++;
2431 if (retries < 20) {
2432 usleep_range(10000, 20000);
2433 goto retry;
2435 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2436 ret = -EINVAL;
2437 goto fail;
2439 ret = 0;
2440 fail:
2441 return ret;
2446 * drm_dp_check_act_status() - Check ACT handled status.
2447 * @mgr: manager to use
2449 * Check the payload status bits in the DPCD for ACT handled completion.
2451 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2453 u8 status;
2454 int ret;
2455 int count = 0;
2457 do {
2458 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2460 if (ret < 0) {
2461 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2462 goto fail;
2465 if (status & DP_PAYLOAD_ACT_HANDLED)
2466 break;
2467 count++;
2468 udelay(100);
2470 } while (count < 30);
2472 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2473 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2474 ret = -EINVAL;
2475 goto fail;
2477 return 0;
2478 fail:
2479 return ret;
2481 EXPORT_SYMBOL(drm_dp_check_act_status);
2484 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2485 * @clock: dot clock for the mode
2486 * @bpp: bpp for the mode.
2488 * This uses the formula in the spec to calculate the PBN value for a mode.
2490 int drm_dp_calc_pbn_mode(int clock, int bpp)
2492 fixed20_12 pix_bw;
2493 fixed20_12 fbpp;
2494 fixed20_12 result;
2495 fixed20_12 margin, tmp;
2496 u32 res;
2498 pix_bw.full = dfixed_const(clock);
2499 fbpp.full = dfixed_const(bpp);
2500 tmp.full = dfixed_const(8);
2501 fbpp.full = dfixed_div(fbpp, tmp);
2503 result.full = dfixed_mul(pix_bw, fbpp);
2504 margin.full = dfixed_const(54);
2505 tmp.full = dfixed_const(64);
2506 margin.full = dfixed_div(margin, tmp);
2507 result.full = dfixed_div(result, margin);
2509 margin.full = dfixed_const(1006);
2510 tmp.full = dfixed_const(1000);
2511 margin.full = dfixed_div(margin, tmp);
2512 result.full = dfixed_mul(result, margin);
2514 result.full = dfixed_div(result, tmp);
2515 result.full = dfixed_ceil(result);
2516 res = dfixed_trunc(result);
2517 return res;
2519 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2521 static int test_calc_pbn_mode(void)
2523 int ret;
2524 ret = drm_dp_calc_pbn_mode(154000, 30);
2525 if (ret != 689)
2526 return -EINVAL;
2527 ret = drm_dp_calc_pbn_mode(234000, 30);
2528 if (ret != 1047)
2529 return -EINVAL;
2530 return 0;
2533 /* we want to kick the TX after we've ack the up/down IRQs. */
2534 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2536 queue_work(system_long_wq, &mgr->tx_work);
2539 static void drm_dp_mst_dump_mstb(struct seq_file *m,
2540 struct drm_dp_mst_branch *mstb)
2542 struct drm_dp_mst_port *port;
2543 int tabs = mstb->lct;
2544 char prefix[10];
2545 int i;
2547 for (i = 0; i < tabs; i++)
2548 prefix[i] = '\t';
2549 prefix[i] = '\0';
2551 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2552 list_for_each_entry(port, &mstb->ports, next) {
2553 seq_printf(m, "%sport: %d: ddps: %d ldps: %d, %p, conn: %p\n", prefix, port->port_num, port->ddps, port->ldps, port, port->connector);
2554 if (port->mstb)
2555 drm_dp_mst_dump_mstb(m, port->mstb);
2559 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2560 char *buf)
2562 int ret;
2563 int i;
2564 for (i = 0; i < 4; i++) {
2565 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2566 if (ret != 16)
2567 break;
2569 if (i == 4)
2570 return true;
2571 return false;
2575 * drm_dp_mst_dump_topology(): dump topology to seq file.
2576 * @m: seq_file to dump output to
2577 * @mgr: manager to dump current topology for.
2579 * helper to dump MST topology to a seq file for debugfs.
2581 void drm_dp_mst_dump_topology(struct seq_file *m,
2582 struct drm_dp_mst_topology_mgr *mgr)
2584 int i;
2585 struct drm_dp_mst_port *port;
2586 mutex_lock(&mgr->lock);
2587 if (mgr->mst_primary)
2588 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2590 /* dump VCPIs */
2591 mutex_unlock(&mgr->lock);
2593 mutex_lock(&mgr->payload_lock);
2594 seq_printf(m, "vcpi: %lx %lx\n", mgr->payload_mask, mgr->vcpi_mask);
2596 for (i = 0; i < mgr->max_payloads; i++) {
2597 if (mgr->proposed_vcpis[i]) {
2598 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2599 seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots);
2600 } else
2601 seq_printf(m, "vcpi %d:unsed\n", i);
2603 for (i = 0; i < mgr->max_payloads; i++) {
2604 seq_printf(m, "payload %d: %d, %d, %d\n",
2606 mgr->payloads[i].payload_state,
2607 mgr->payloads[i].start_slot,
2608 mgr->payloads[i].num_slots);
2612 mutex_unlock(&mgr->payload_lock);
2614 mutex_lock(&mgr->lock);
2615 if (mgr->mst_primary) {
2616 u8 buf[64];
2617 bool bret;
2618 int ret;
2619 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2620 seq_printf(m, "dpcd: ");
2621 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2622 seq_printf(m, "%02x ", buf[i]);
2623 seq_printf(m, "\n");
2624 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2625 seq_printf(m, "faux/mst: ");
2626 for (i = 0; i < 2; i++)
2627 seq_printf(m, "%02x ", buf[i]);
2628 seq_printf(m, "\n");
2629 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2630 seq_printf(m, "mst ctrl: ");
2631 for (i = 0; i < 1; i++)
2632 seq_printf(m, "%02x ", buf[i]);
2633 seq_printf(m, "\n");
2635 bret = dump_dp_payload_table(mgr, buf);
2636 if (bret == true) {
2637 seq_printf(m, "payload table: ");
2638 for (i = 0; i < 63; i++)
2639 seq_printf(m, "%02x ", buf[i]);
2640 seq_printf(m, "\n");
2645 mutex_unlock(&mgr->lock);
2648 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2650 static void drm_dp_tx_work(struct work_struct *work)
2652 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2654 mutex_lock(&mgr->qlock);
2655 if (mgr->tx_down_in_progress)
2656 process_single_down_tx_qlock(mgr);
2657 mutex_unlock(&mgr->qlock);
2660 static void drm_dp_destroy_connector_work(struct work_struct *work)
2662 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
2663 struct drm_dp_mst_port *port;
2666 * Not a regular list traverse as we have to drop the destroy
2667 * connector lock before destroying the connector, to avoid AB->BA
2668 * ordering between this lock and the config mutex.
2670 for (;;) {
2671 mutex_lock(&mgr->destroy_connector_lock);
2672 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2673 if (!port) {
2674 mutex_unlock(&mgr->destroy_connector_lock);
2675 break;
2677 list_del(&port->next);
2678 mutex_unlock(&mgr->destroy_connector_lock);
2680 mgr->cbs->destroy_connector(mgr, port->connector);
2682 drm_dp_port_teardown_pdt(port, port->pdt);
2684 if (!port->input && port->vcpi.vcpi > 0)
2685 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2686 kfree(port);
2691 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2692 * @mgr: manager struct to initialise
2693 * @dev: device providing this structure - for i2c addition.
2694 * @aux: DP helper aux channel to talk to this device
2695 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2696 * @max_payloads: maximum number of payloads this GPU can source
2697 * @conn_base_id: the connector object ID the MST device is connected to.
2699 * Return 0 for success, or negative error code on failure
2701 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2702 struct device *dev, struct drm_dp_aux *aux,
2703 int max_dpcd_transaction_bytes,
2704 int max_payloads, int conn_base_id)
2706 mutex_init(&mgr->lock);
2707 mutex_init(&mgr->qlock);
2708 mutex_init(&mgr->payload_lock);
2709 mutex_init(&mgr->destroy_connector_lock);
2710 INIT_LIST_HEAD(&mgr->tx_msg_upq);
2711 INIT_LIST_HEAD(&mgr->tx_msg_downq);
2712 INIT_LIST_HEAD(&mgr->destroy_connector_list);
2713 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2714 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
2715 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
2716 init_waitqueue_head(&mgr->tx_waitq);
2717 mgr->dev = dev;
2718 mgr->aux = aux;
2719 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2720 mgr->max_payloads = max_payloads;
2721 mgr->conn_base_id = conn_base_id;
2722 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
2723 if (!mgr->payloads)
2724 return -ENOMEM;
2725 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
2726 if (!mgr->proposed_vcpis)
2727 return -ENOMEM;
2728 set_bit(0, &mgr->payload_mask);
2729 test_calc_pbn_mode();
2730 return 0;
2732 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
2735 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
2736 * @mgr: manager to destroy
2738 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
2740 flush_work(&mgr->destroy_connector_work);
2741 mutex_lock(&mgr->payload_lock);
2742 kfree(mgr->payloads);
2743 mgr->payloads = NULL;
2744 kfree(mgr->proposed_vcpis);
2745 mgr->proposed_vcpis = NULL;
2746 mutex_unlock(&mgr->payload_lock);
2747 mgr->dev = NULL;
2748 mgr->aux = NULL;
2750 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
2752 /* I2C device */
2753 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
2754 int num)
2756 struct drm_dp_aux *aux = adapter->algo_data;
2757 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
2758 struct drm_dp_mst_branch *mstb;
2759 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2760 unsigned int i;
2761 bool reading = false;
2762 struct drm_dp_sideband_msg_req_body msg;
2763 struct drm_dp_sideband_msg_tx *txmsg = NULL;
2764 int ret;
2766 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
2767 if (!mstb)
2768 return -EREMOTEIO;
2770 /* construct i2c msg */
2771 /* see if last msg is a read */
2772 if (msgs[num - 1].flags & I2C_M_RD)
2773 reading = true;
2775 if (!reading) {
2776 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
2777 ret = -EIO;
2778 goto out;
2781 msg.req_type = DP_REMOTE_I2C_READ;
2782 msg.u.i2c_read.num_transactions = num - 1;
2783 msg.u.i2c_read.port_number = port->port_num;
2784 for (i = 0; i < num - 1; i++) {
2785 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
2786 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
2787 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
2789 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
2790 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
2792 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2793 if (!txmsg) {
2794 ret = -ENOMEM;
2795 goto out;
2798 txmsg->dst = mstb;
2799 drm_dp_encode_sideband_req(&msg, txmsg);
2801 drm_dp_queue_down_tx(mgr, txmsg);
2803 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2804 if (ret > 0) {
2806 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
2807 ret = -EREMOTEIO;
2808 goto out;
2810 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
2811 ret = -EIO;
2812 goto out;
2814 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
2815 ret = num;
2817 out:
2818 kfree(txmsg);
2819 drm_dp_put_mst_branch_device(mstb);
2820 return ret;
2823 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
2825 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
2826 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
2827 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
2828 I2C_FUNC_10BIT_ADDR;
2831 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
2832 .functionality = drm_dp_mst_i2c_functionality,
2833 .master_xfer = drm_dp_mst_i2c_xfer,
2837 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
2838 * @aux: DisplayPort AUX channel
2840 * Returns 0 on success or a negative error code on failure.
2842 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
2844 aux->ddc.algo = &drm_dp_mst_i2c_algo;
2845 aux->ddc.algo_data = aux;
2846 aux->ddc.retries = 3;
2848 aux->ddc.class = I2C_CLASS_DDC;
2849 aux->ddc.owner = THIS_MODULE;
2850 aux->ddc.dev.parent = aux->dev;
2851 aux->ddc.dev.of_node = aux->dev->of_node;
2853 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
2854 sizeof(aux->ddc.name));
2856 return i2c_add_adapter(&aux->ddc);
2860 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
2861 * @aux: DisplayPort AUX channel
2863 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
2865 i2c_del_adapter(&aux->ddc);