Input: xpad - add support for Xbox1 PDP Camo series gamepad
[linux/fpc-iii.git] / drivers / gpu / drm / drm_dp_mst_topology.c
blobb59441d109a54b9233cc8336f0af4f25fc3a3876
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 void 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;
334 * ignore out-of-order messages or messages that are part of a
335 * failed transaction
337 if (!recv_hdr.somt && !msg->have_somt)
338 return false;
340 /* get length contained in this portion */
341 msg->curchunk_len = recv_hdr.msg_len;
342 msg->curchunk_hdrlen = hdrlen;
344 /* we have already gotten an somt - don't bother parsing */
345 if (recv_hdr.somt && msg->have_somt)
346 return false;
348 if (recv_hdr.somt) {
349 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
350 msg->have_somt = true;
352 if (recv_hdr.eomt)
353 msg->have_eomt = true;
355 /* copy the bytes for the remainder of this header chunk */
356 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
357 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
358 } else {
359 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
360 msg->curchunk_idx += replybuflen;
363 if (msg->curchunk_idx >= msg->curchunk_len) {
364 /* do CRC */
365 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
366 /* copy chunk into bigger msg */
367 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
368 msg->curlen += msg->curchunk_len - 1;
370 return true;
373 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
374 struct drm_dp_sideband_msg_reply_body *repmsg)
376 int idx = 1;
377 int i;
378 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
379 idx += 16;
380 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
381 idx++;
382 if (idx > raw->curlen)
383 goto fail_len;
384 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
385 if (raw->msg[idx] & 0x80)
386 repmsg->u.link_addr.ports[i].input_port = 1;
388 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
389 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
391 idx++;
392 if (idx > raw->curlen)
393 goto fail_len;
394 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
395 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
396 if (repmsg->u.link_addr.ports[i].input_port == 0)
397 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
398 idx++;
399 if (idx > raw->curlen)
400 goto fail_len;
401 if (repmsg->u.link_addr.ports[i].input_port == 0) {
402 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
403 idx++;
404 if (idx > raw->curlen)
405 goto fail_len;
406 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
407 idx += 16;
408 if (idx > raw->curlen)
409 goto fail_len;
410 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
411 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
412 idx++;
415 if (idx > raw->curlen)
416 goto fail_len;
419 return true;
420 fail_len:
421 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
422 return false;
425 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
426 struct drm_dp_sideband_msg_reply_body *repmsg)
428 int idx = 1;
429 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
430 idx++;
431 if (idx > raw->curlen)
432 goto fail_len;
433 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
434 if (idx > raw->curlen)
435 goto fail_len;
437 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
438 return true;
439 fail_len:
440 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
441 return false;
444 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
445 struct drm_dp_sideband_msg_reply_body *repmsg)
447 int idx = 1;
448 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
449 idx++;
450 if (idx > raw->curlen)
451 goto fail_len;
452 return true;
453 fail_len:
454 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
455 return false;
458 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
459 struct drm_dp_sideband_msg_reply_body *repmsg)
461 int idx = 1;
463 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
464 idx++;
465 if (idx > raw->curlen)
466 goto fail_len;
467 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
468 idx++;
469 /* TODO check */
470 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
471 return true;
472 fail_len:
473 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
474 return false;
477 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
478 struct drm_dp_sideband_msg_reply_body *repmsg)
480 int idx = 1;
481 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
482 idx++;
483 if (idx > raw->curlen)
484 goto fail_len;
485 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
486 idx += 2;
487 if (idx > raw->curlen)
488 goto fail_len;
489 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
490 idx += 2;
491 if (idx > raw->curlen)
492 goto fail_len;
493 return true;
494 fail_len:
495 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
496 return false;
499 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
500 struct drm_dp_sideband_msg_reply_body *repmsg)
502 int idx = 1;
503 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
504 idx++;
505 if (idx > raw->curlen)
506 goto fail_len;
507 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
508 idx++;
509 if (idx > raw->curlen)
510 goto fail_len;
511 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
512 idx += 2;
513 if (idx > raw->curlen)
514 goto fail_len;
515 return true;
516 fail_len:
517 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
518 return false;
521 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
522 struct drm_dp_sideband_msg_reply_body *repmsg)
524 int idx = 1;
525 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
526 idx++;
527 if (idx > raw->curlen)
528 goto fail_len;
529 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
530 idx += 2;
531 if (idx > raw->curlen)
532 goto fail_len;
533 return true;
534 fail_len:
535 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
536 return false;
539 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
540 struct drm_dp_sideband_msg_reply_body *msg)
542 memset(msg, 0, sizeof(*msg));
543 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
544 msg->req_type = (raw->msg[0] & 0x7f);
546 if (msg->reply_type) {
547 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
548 msg->u.nak.reason = raw->msg[17];
549 msg->u.nak.nak_data = raw->msg[18];
550 return false;
553 switch (msg->req_type) {
554 case DP_LINK_ADDRESS:
555 return drm_dp_sideband_parse_link_address(raw, msg);
556 case DP_QUERY_PAYLOAD:
557 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
558 case DP_REMOTE_DPCD_READ:
559 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
560 case DP_REMOTE_DPCD_WRITE:
561 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
562 case DP_REMOTE_I2C_READ:
563 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
564 case DP_ENUM_PATH_RESOURCES:
565 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
566 case DP_ALLOCATE_PAYLOAD:
567 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
568 default:
569 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
570 return false;
574 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
575 struct drm_dp_sideband_msg_req_body *msg)
577 int idx = 1;
579 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
580 idx++;
581 if (idx > raw->curlen)
582 goto fail_len;
584 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
585 idx += 16;
586 if (idx > raw->curlen)
587 goto fail_len;
589 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
590 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
591 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
592 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
593 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
594 idx++;
595 return true;
596 fail_len:
597 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
598 return false;
601 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
602 struct drm_dp_sideband_msg_req_body *msg)
604 int idx = 1;
606 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
607 idx++;
608 if (idx > raw->curlen)
609 goto fail_len;
611 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
612 idx += 16;
613 if (idx > raw->curlen)
614 goto fail_len;
616 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
617 idx++;
618 return true;
619 fail_len:
620 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
621 return false;
624 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
625 struct drm_dp_sideband_msg_req_body *msg)
627 memset(msg, 0, sizeof(*msg));
628 msg->req_type = (raw->msg[0] & 0x7f);
630 switch (msg->req_type) {
631 case DP_CONNECTION_STATUS_NOTIFY:
632 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
633 case DP_RESOURCE_STATUS_NOTIFY:
634 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
635 default:
636 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
637 return false;
641 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
643 struct drm_dp_sideband_msg_req_body req;
645 req.req_type = DP_REMOTE_DPCD_WRITE;
646 req.u.dpcd_write.port_number = port_num;
647 req.u.dpcd_write.dpcd_address = offset;
648 req.u.dpcd_write.num_bytes = num_bytes;
649 req.u.dpcd_write.bytes = bytes;
650 drm_dp_encode_sideband_req(&req, msg);
652 return 0;
655 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
657 struct drm_dp_sideband_msg_req_body req;
659 req.req_type = DP_LINK_ADDRESS;
660 drm_dp_encode_sideband_req(&req, msg);
661 return 0;
664 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
666 struct drm_dp_sideband_msg_req_body req;
668 req.req_type = DP_ENUM_PATH_RESOURCES;
669 req.u.port_num.port_number = port_num;
670 drm_dp_encode_sideband_req(&req, msg);
671 msg->path_msg = true;
672 return 0;
675 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
676 u8 vcpi, uint16_t pbn,
677 u8 number_sdp_streams,
678 u8 *sdp_stream_sink)
680 struct drm_dp_sideband_msg_req_body req;
681 memset(&req, 0, sizeof(req));
682 req.req_type = DP_ALLOCATE_PAYLOAD;
683 req.u.allocate_payload.port_number = port_num;
684 req.u.allocate_payload.vcpi = vcpi;
685 req.u.allocate_payload.pbn = pbn;
686 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
687 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
688 number_sdp_streams);
689 drm_dp_encode_sideband_req(&req, msg);
690 msg->path_msg = true;
691 return 0;
694 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
695 struct drm_dp_vcpi *vcpi)
697 int ret, vcpi_ret;
699 mutex_lock(&mgr->payload_lock);
700 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
701 if (ret > mgr->max_payloads) {
702 ret = -EINVAL;
703 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
704 goto out_unlock;
707 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
708 if (vcpi_ret > mgr->max_payloads) {
709 ret = -EINVAL;
710 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
711 goto out_unlock;
714 set_bit(ret, &mgr->payload_mask);
715 set_bit(vcpi_ret, &mgr->vcpi_mask);
716 vcpi->vcpi = vcpi_ret + 1;
717 mgr->proposed_vcpis[ret - 1] = vcpi;
718 out_unlock:
719 mutex_unlock(&mgr->payload_lock);
720 return ret;
723 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
724 int vcpi)
726 int i;
727 if (vcpi == 0)
728 return;
730 mutex_lock(&mgr->payload_lock);
731 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
732 clear_bit(vcpi - 1, &mgr->vcpi_mask);
734 for (i = 0; i < mgr->max_payloads; i++) {
735 if (mgr->proposed_vcpis[i])
736 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
737 mgr->proposed_vcpis[i] = NULL;
738 clear_bit(i + 1, &mgr->payload_mask);
741 mutex_unlock(&mgr->payload_lock);
744 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
745 struct drm_dp_sideband_msg_tx *txmsg)
747 bool ret;
750 * All updates to txmsg->state are protected by mgr->qlock, and the two
751 * cases we check here are terminal states. For those the barriers
752 * provided by the wake_up/wait_event pair are enough.
754 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
755 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
756 return ret;
759 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
760 struct drm_dp_sideband_msg_tx *txmsg)
762 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
763 int ret;
765 ret = wait_event_timeout(mgr->tx_waitq,
766 check_txmsg_state(mgr, txmsg),
767 (4 * HZ));
768 mutex_lock(&mstb->mgr->qlock);
769 if (ret > 0) {
770 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
771 ret = -EIO;
772 goto out;
774 } else {
775 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
777 /* dump some state */
778 ret = -EIO;
780 /* remove from q */
781 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
782 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
783 list_del(&txmsg->next);
786 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
787 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
788 mstb->tx_slots[txmsg->seqno] = NULL;
791 out:
792 mutex_unlock(&mgr->qlock);
794 return ret;
797 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
799 struct drm_dp_mst_branch *mstb;
801 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
802 if (!mstb)
803 return NULL;
805 mstb->lct = lct;
806 if (lct > 1)
807 memcpy(mstb->rad, rad, lct / 2);
808 INIT_LIST_HEAD(&mstb->ports);
809 kref_init(&mstb->kref);
810 return mstb;
813 static void drm_dp_free_mst_port(struct kref *kref);
815 static void drm_dp_free_mst_branch_device(struct kref *kref)
817 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
818 if (mstb->port_parent) {
819 if (list_empty(&mstb->port_parent->next))
820 kref_put(&mstb->port_parent->kref, drm_dp_free_mst_port);
822 kfree(mstb);
825 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
827 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
828 struct drm_dp_mst_port *port, *tmp;
829 bool wake_tx = false;
832 * init kref again to be used by ports to remove mst branch when it is
833 * not needed anymore
835 kref_init(kref);
837 if (mstb->port_parent && list_empty(&mstb->port_parent->next))
838 kref_get(&mstb->port_parent->kref);
841 * destroy all ports - don't need lock
842 * as there are no more references to the mst branch
843 * device at this point.
845 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
846 list_del(&port->next);
847 drm_dp_put_port(port);
850 /* drop any tx slots msg */
851 mutex_lock(&mstb->mgr->qlock);
852 if (mstb->tx_slots[0]) {
853 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
854 mstb->tx_slots[0] = NULL;
855 wake_tx = true;
857 if (mstb->tx_slots[1]) {
858 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
859 mstb->tx_slots[1] = NULL;
860 wake_tx = true;
862 mutex_unlock(&mstb->mgr->qlock);
864 if (wake_tx)
865 wake_up(&mstb->mgr->tx_waitq);
867 kref_put(kref, drm_dp_free_mst_branch_device);
870 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
872 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
876 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
878 struct drm_dp_mst_branch *mstb;
880 switch (old_pdt) {
881 case DP_PEER_DEVICE_DP_LEGACY_CONV:
882 case DP_PEER_DEVICE_SST_SINK:
883 /* remove i2c over sideband */
884 drm_dp_mst_unregister_i2c_bus(&port->aux);
885 break;
886 case DP_PEER_DEVICE_MST_BRANCHING:
887 mstb = port->mstb;
888 port->mstb = NULL;
889 drm_dp_put_mst_branch_device(mstb);
890 break;
894 static void drm_dp_destroy_port(struct kref *kref)
896 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
897 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
899 if (!port->input) {
900 port->vcpi.num_slots = 0;
902 kfree(port->cached_edid);
905 * The only time we don't have a connector
906 * on an output port is if the connector init
907 * fails.
909 if (port->connector) {
910 /* we can't destroy the connector here, as
911 * we might be holding the mode_config.mutex
912 * from an EDID retrieval */
914 mutex_lock(&mgr->destroy_connector_lock);
915 kref_get(&port->parent->kref);
916 list_add(&port->next, &mgr->destroy_connector_list);
917 mutex_unlock(&mgr->destroy_connector_lock);
918 schedule_work(&mgr->destroy_connector_work);
919 return;
921 /* no need to clean up vcpi
922 * as if we have no connector we never setup a vcpi */
923 drm_dp_port_teardown_pdt(port, port->pdt);
924 port->pdt = DP_PEER_DEVICE_NONE;
926 kfree(port);
929 static void drm_dp_put_port(struct drm_dp_mst_port *port)
931 kref_put(&port->kref, drm_dp_destroy_port);
934 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)
936 struct drm_dp_mst_port *port;
937 struct drm_dp_mst_branch *rmstb;
938 if (to_find == mstb) {
939 kref_get(&mstb->kref);
940 return mstb;
942 list_for_each_entry(port, &mstb->ports, next) {
943 if (port->mstb) {
944 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
945 if (rmstb)
946 return rmstb;
949 return NULL;
952 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)
954 struct drm_dp_mst_branch *rmstb = NULL;
955 mutex_lock(&mgr->lock);
956 if (mgr->mst_primary)
957 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
958 mutex_unlock(&mgr->lock);
959 return rmstb;
962 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)
964 struct drm_dp_mst_port *port, *mport;
966 list_for_each_entry(port, &mstb->ports, next) {
967 if (port == to_find) {
968 kref_get(&port->kref);
969 return port;
971 if (port->mstb) {
972 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
973 if (mport)
974 return mport;
977 return NULL;
980 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)
982 struct drm_dp_mst_port *rport = NULL;
983 mutex_lock(&mgr->lock);
984 if (mgr->mst_primary)
985 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
986 mutex_unlock(&mgr->lock);
987 return rport;
990 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
992 struct drm_dp_mst_port *port;
994 list_for_each_entry(port, &mstb->ports, next) {
995 if (port->port_num == port_num) {
996 kref_get(&port->kref);
997 return port;
1001 return NULL;
1005 * calculate a new RAD for this MST branch device
1006 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1007 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1009 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1010 u8 *rad)
1012 int parent_lct = port->parent->lct;
1013 int shift = 4;
1014 int idx = (parent_lct - 1) / 2;
1015 if (parent_lct > 1) {
1016 memcpy(rad, port->parent->rad, idx + 1);
1017 shift = (parent_lct % 2) ? 4 : 0;
1018 } else
1019 rad[0] = 0;
1021 rad[idx] |= port->port_num << shift;
1022 return parent_lct + 1;
1026 * return sends link address for new mstb
1028 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1030 int ret;
1031 u8 rad[6], lct;
1032 bool send_link = false;
1033 switch (port->pdt) {
1034 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1035 case DP_PEER_DEVICE_SST_SINK:
1036 /* add i2c over sideband */
1037 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1038 break;
1039 case DP_PEER_DEVICE_MST_BRANCHING:
1040 lct = drm_dp_calculate_rad(port, rad);
1042 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1043 port->mstb->mgr = port->mgr;
1044 port->mstb->port_parent = port;
1046 send_link = true;
1047 break;
1049 return send_link;
1052 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1054 int ret;
1056 memcpy(mstb->guid, guid, 16);
1058 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1059 if (mstb->port_parent) {
1060 ret = drm_dp_send_dpcd_write(
1061 mstb->mgr,
1062 mstb->port_parent,
1063 DP_GUID,
1065 mstb->guid);
1066 } else {
1068 ret = drm_dp_dpcd_write(
1069 mstb->mgr->aux,
1070 DP_GUID,
1071 mstb->guid,
1072 16);
1077 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1078 int pnum,
1079 char *proppath,
1080 size_t proppath_size)
1082 int i;
1083 char temp[8];
1084 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1085 for (i = 0; i < (mstb->lct - 1); i++) {
1086 int shift = (i % 2) ? 0 : 4;
1087 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1088 snprintf(temp, sizeof(temp), "-%d", port_num);
1089 strlcat(proppath, temp, proppath_size);
1091 snprintf(temp, sizeof(temp), "-%d", pnum);
1092 strlcat(proppath, temp, proppath_size);
1095 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1096 struct device *dev,
1097 struct drm_dp_link_addr_reply_port *port_msg)
1099 struct drm_dp_mst_port *port;
1100 bool ret;
1101 bool created = false;
1102 int old_pdt = 0;
1103 int old_ddps = 0;
1104 port = drm_dp_get_port(mstb, port_msg->port_number);
1105 if (!port) {
1106 port = kzalloc(sizeof(*port), GFP_KERNEL);
1107 if (!port)
1108 return;
1109 kref_init(&port->kref);
1110 port->parent = mstb;
1111 port->port_num = port_msg->port_number;
1112 port->mgr = mstb->mgr;
1113 port->aux.name = "DPMST";
1114 port->aux.dev = dev;
1115 created = true;
1116 } else {
1117 old_pdt = port->pdt;
1118 old_ddps = port->ddps;
1121 port->pdt = port_msg->peer_device_type;
1122 port->input = port_msg->input_port;
1123 port->mcs = port_msg->mcs;
1124 port->ddps = port_msg->ddps;
1125 port->ldps = port_msg->legacy_device_plug_status;
1126 port->dpcd_rev = port_msg->dpcd_revision;
1127 port->num_sdp_streams = port_msg->num_sdp_streams;
1128 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1130 /* manage mstb port lists with mgr lock - take a reference
1131 for this list */
1132 if (created) {
1133 mutex_lock(&mstb->mgr->lock);
1134 kref_get(&port->kref);
1135 list_add(&port->next, &mstb->ports);
1136 mutex_unlock(&mstb->mgr->lock);
1139 if (old_ddps != port->ddps) {
1140 if (port->ddps) {
1141 if (!port->input)
1142 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1143 } else {
1144 port->available_pbn = 0;
1148 if (old_pdt != port->pdt && !port->input) {
1149 drm_dp_port_teardown_pdt(port, old_pdt);
1151 ret = drm_dp_port_setup_pdt(port);
1152 if (ret == true)
1153 drm_dp_send_link_address(mstb->mgr, port->mstb);
1156 if (created && !port->input) {
1157 char proppath[255];
1159 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
1160 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1161 if (!port->connector) {
1162 /* remove it from the port list */
1163 mutex_lock(&mstb->mgr->lock);
1164 list_del(&port->next);
1165 mutex_unlock(&mstb->mgr->lock);
1166 /* drop port list reference */
1167 drm_dp_put_port(port);
1168 goto out;
1170 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1171 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1172 port->port_num >= DP_MST_LOGICAL_PORT_0) {
1173 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1174 drm_mode_connector_set_tile_property(port->connector);
1176 (*mstb->mgr->cbs->register_connector)(port->connector);
1179 out:
1180 /* put reference to this port */
1181 drm_dp_put_port(port);
1184 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1185 struct drm_dp_connection_status_notify *conn_stat)
1187 struct drm_dp_mst_port *port;
1188 int old_pdt;
1189 int old_ddps;
1190 bool dowork = false;
1191 port = drm_dp_get_port(mstb, conn_stat->port_number);
1192 if (!port)
1193 return;
1195 old_ddps = port->ddps;
1196 old_pdt = port->pdt;
1197 port->pdt = conn_stat->peer_device_type;
1198 port->mcs = conn_stat->message_capability_status;
1199 port->ldps = conn_stat->legacy_device_plug_status;
1200 port->ddps = conn_stat->displayport_device_plug_status;
1202 if (old_ddps != port->ddps) {
1203 if (port->ddps) {
1204 dowork = true;
1205 } else {
1206 port->available_pbn = 0;
1209 if (old_pdt != port->pdt && !port->input) {
1210 drm_dp_port_teardown_pdt(port, old_pdt);
1212 if (drm_dp_port_setup_pdt(port))
1213 dowork = true;
1216 drm_dp_put_port(port);
1217 if (dowork)
1218 queue_work(system_long_wq, &mstb->mgr->work);
1222 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1223 u8 lct, u8 *rad)
1225 struct drm_dp_mst_branch *mstb;
1226 struct drm_dp_mst_port *port;
1227 int i;
1228 /* find the port by iterating down */
1230 mutex_lock(&mgr->lock);
1231 mstb = mgr->mst_primary;
1233 if (!mstb)
1234 goto out;
1236 for (i = 0; i < lct - 1; i++) {
1237 int shift = (i % 2) ? 0 : 4;
1238 int port_num = (rad[i / 2] >> shift) & 0xf;
1240 list_for_each_entry(port, &mstb->ports, next) {
1241 if (port->port_num == port_num) {
1242 mstb = port->mstb;
1243 if (!mstb) {
1244 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1245 goto out;
1248 break;
1252 kref_get(&mstb->kref);
1253 out:
1254 mutex_unlock(&mgr->lock);
1255 return mstb;
1258 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1259 struct drm_dp_mst_branch *mstb,
1260 uint8_t *guid)
1262 struct drm_dp_mst_branch *found_mstb;
1263 struct drm_dp_mst_port *port;
1265 if (memcmp(mstb->guid, guid, 16) == 0)
1266 return mstb;
1269 list_for_each_entry(port, &mstb->ports, next) {
1270 if (!port->mstb)
1271 continue;
1273 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1275 if (found_mstb)
1276 return found_mstb;
1279 return NULL;
1282 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
1283 struct drm_dp_mst_topology_mgr *mgr,
1284 uint8_t *guid)
1286 struct drm_dp_mst_branch *mstb;
1288 /* find the port by iterating down */
1289 mutex_lock(&mgr->lock);
1291 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1293 if (mstb)
1294 kref_get(&mstb->kref);
1296 mutex_unlock(&mgr->lock);
1297 return mstb;
1300 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1301 struct drm_dp_mst_branch *mstb)
1303 struct drm_dp_mst_port *port;
1304 struct drm_dp_mst_branch *mstb_child;
1305 if (!mstb->link_address_sent)
1306 drm_dp_send_link_address(mgr, mstb);
1308 list_for_each_entry(port, &mstb->ports, next) {
1309 if (port->input)
1310 continue;
1312 if (!port->ddps)
1313 continue;
1315 if (!port->available_pbn)
1316 drm_dp_send_enum_path_resources(mgr, mstb, port);
1318 if (port->mstb) {
1319 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1320 if (mstb_child) {
1321 drm_dp_check_and_send_link_address(mgr, mstb_child);
1322 drm_dp_put_mst_branch_device(mstb_child);
1328 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1330 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1331 struct drm_dp_mst_branch *mstb;
1333 mutex_lock(&mgr->lock);
1334 mstb = mgr->mst_primary;
1335 if (mstb) {
1336 kref_get(&mstb->kref);
1338 mutex_unlock(&mgr->lock);
1339 if (mstb) {
1340 drm_dp_check_and_send_link_address(mgr, mstb);
1341 drm_dp_put_mst_branch_device(mstb);
1345 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1346 u8 *guid)
1348 static u8 zero_guid[16];
1350 if (!memcmp(guid, zero_guid, 16)) {
1351 u64 salt = get_jiffies_64();
1352 memcpy(&guid[0], &salt, sizeof(u64));
1353 memcpy(&guid[8], &salt, sizeof(u64));
1354 return false;
1356 return true;
1359 #if 0
1360 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1362 struct drm_dp_sideband_msg_req_body req;
1364 req.req_type = DP_REMOTE_DPCD_READ;
1365 req.u.dpcd_read.port_number = port_num;
1366 req.u.dpcd_read.dpcd_address = offset;
1367 req.u.dpcd_read.num_bytes = num_bytes;
1368 drm_dp_encode_sideband_req(&req, msg);
1370 return 0;
1372 #endif
1374 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1375 bool up, u8 *msg, int len)
1377 int ret;
1378 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1379 int tosend, total, offset;
1380 int retries = 0;
1382 retry:
1383 total = len;
1384 offset = 0;
1385 do {
1386 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1388 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1389 &msg[offset],
1390 tosend);
1391 if (ret != tosend) {
1392 if (ret == -EIO && retries < 5) {
1393 retries++;
1394 goto retry;
1396 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1398 return -EIO;
1400 offset += tosend;
1401 total -= tosend;
1402 } while (total > 0);
1403 return 0;
1406 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1407 struct drm_dp_sideband_msg_tx *txmsg)
1409 struct drm_dp_mst_branch *mstb = txmsg->dst;
1410 u8 req_type;
1412 /* both msg slots are full */
1413 if (txmsg->seqno == -1) {
1414 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1415 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1416 return -EAGAIN;
1418 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1419 txmsg->seqno = mstb->last_seqno;
1420 mstb->last_seqno ^= 1;
1421 } else if (mstb->tx_slots[0] == NULL)
1422 txmsg->seqno = 0;
1423 else
1424 txmsg->seqno = 1;
1425 mstb->tx_slots[txmsg->seqno] = txmsg;
1428 req_type = txmsg->msg[0] & 0x7f;
1429 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1430 req_type == DP_RESOURCE_STATUS_NOTIFY)
1431 hdr->broadcast = 1;
1432 else
1433 hdr->broadcast = 0;
1434 hdr->path_msg = txmsg->path_msg;
1435 hdr->lct = mstb->lct;
1436 hdr->lcr = mstb->lct - 1;
1437 if (mstb->lct > 1)
1438 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1439 hdr->seqno = txmsg->seqno;
1440 return 0;
1443 * process a single block of the next message in the sideband queue
1445 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1446 struct drm_dp_sideband_msg_tx *txmsg,
1447 bool up)
1449 u8 chunk[48];
1450 struct drm_dp_sideband_msg_hdr hdr;
1451 int len, space, idx, tosend;
1452 int ret;
1454 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1456 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1457 txmsg->seqno = -1;
1458 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1461 /* make hdr from dst mst - for replies use seqno
1462 otherwise assign one */
1463 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1464 if (ret < 0)
1465 return ret;
1467 /* amount left to send in this message */
1468 len = txmsg->cur_len - txmsg->cur_offset;
1470 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1471 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1473 tosend = min(len, space);
1474 if (len == txmsg->cur_len)
1475 hdr.somt = 1;
1476 if (space >= len)
1477 hdr.eomt = 1;
1480 hdr.msg_len = tosend + 1;
1481 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1482 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1483 /* add crc at end */
1484 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1485 idx += tosend + 1;
1487 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1488 if (ret) {
1489 DRM_DEBUG_KMS("sideband msg failed to send\n");
1490 return ret;
1493 txmsg->cur_offset += tosend;
1494 if (txmsg->cur_offset == txmsg->cur_len) {
1495 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1496 return 1;
1498 return 0;
1501 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1503 struct drm_dp_sideband_msg_tx *txmsg;
1504 int ret;
1506 WARN_ON(!mutex_is_locked(&mgr->qlock));
1508 /* construct a chunk from the first msg in the tx_msg queue */
1509 if (list_empty(&mgr->tx_msg_downq))
1510 return;
1512 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1513 ret = process_single_tx_qlock(mgr, txmsg, false);
1514 if (ret == 1) {
1515 /* txmsg is sent it should be in the slots now */
1516 list_del(&txmsg->next);
1517 } else if (ret) {
1518 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1519 list_del(&txmsg->next);
1520 if (txmsg->seqno != -1)
1521 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1522 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1523 wake_up(&mgr->tx_waitq);
1527 /* called holding qlock */
1528 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1529 struct drm_dp_sideband_msg_tx *txmsg)
1531 int ret;
1533 /* construct a chunk from the first msg in the tx_msg queue */
1534 ret = process_single_tx_qlock(mgr, txmsg, true);
1536 if (ret != 1)
1537 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1539 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1542 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1543 struct drm_dp_sideband_msg_tx *txmsg)
1545 mutex_lock(&mgr->qlock);
1546 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1547 if (list_is_singular(&mgr->tx_msg_downq))
1548 process_single_down_tx_qlock(mgr);
1549 mutex_unlock(&mgr->qlock);
1552 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1553 struct drm_dp_mst_branch *mstb)
1555 int len;
1556 struct drm_dp_sideband_msg_tx *txmsg;
1557 int ret;
1559 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1560 if (!txmsg)
1561 return;
1563 txmsg->dst = mstb;
1564 len = build_link_address(txmsg);
1566 mstb->link_address_sent = true;
1567 drm_dp_queue_down_tx(mgr, txmsg);
1569 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1570 if (ret > 0) {
1571 int i;
1573 if (txmsg->reply.reply_type == 1)
1574 DRM_DEBUG_KMS("link address nak received\n");
1575 else {
1576 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1577 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1578 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,
1579 txmsg->reply.u.link_addr.ports[i].input_port,
1580 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1581 txmsg->reply.u.link_addr.ports[i].port_number,
1582 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1583 txmsg->reply.u.link_addr.ports[i].mcs,
1584 txmsg->reply.u.link_addr.ports[i].ddps,
1585 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1586 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1587 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1590 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1592 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1593 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1595 (*mgr->cbs->hotplug)(mgr);
1597 } else {
1598 mstb->link_address_sent = false;
1599 DRM_DEBUG_KMS("link address failed %d\n", ret);
1602 kfree(txmsg);
1605 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1606 struct drm_dp_mst_branch *mstb,
1607 struct drm_dp_mst_port *port)
1609 int len;
1610 struct drm_dp_sideband_msg_tx *txmsg;
1611 int ret;
1613 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1614 if (!txmsg)
1615 return -ENOMEM;
1617 txmsg->dst = mstb;
1618 len = build_enum_path_resources(txmsg, port->port_num);
1620 drm_dp_queue_down_tx(mgr, txmsg);
1622 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1623 if (ret > 0) {
1624 if (txmsg->reply.reply_type == 1)
1625 DRM_DEBUG_KMS("enum path resources nak received\n");
1626 else {
1627 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1628 DRM_ERROR("got incorrect port in response\n");
1629 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,
1630 txmsg->reply.u.path_resources.avail_payload_bw_number);
1631 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1635 kfree(txmsg);
1636 return 0;
1639 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
1641 if (!mstb->port_parent)
1642 return NULL;
1644 if (mstb->port_parent->mstb != mstb)
1645 return mstb->port_parent;
1647 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
1650 static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
1651 struct drm_dp_mst_branch *mstb,
1652 int *port_num)
1654 struct drm_dp_mst_branch *rmstb = NULL;
1655 struct drm_dp_mst_port *found_port;
1656 mutex_lock(&mgr->lock);
1657 if (mgr->mst_primary) {
1658 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
1660 if (found_port) {
1661 rmstb = found_port->parent;
1662 kref_get(&rmstb->kref);
1663 *port_num = found_port->port_num;
1666 mutex_unlock(&mgr->lock);
1667 return rmstb;
1670 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1671 struct drm_dp_mst_port *port,
1672 int id,
1673 int pbn)
1675 struct drm_dp_sideband_msg_tx *txmsg;
1676 struct drm_dp_mst_branch *mstb;
1677 int len, ret, port_num;
1678 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1679 int i;
1681 port = drm_dp_get_validated_port_ref(mgr, port);
1682 if (!port)
1683 return -EINVAL;
1685 port_num = port->port_num;
1686 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1687 if (!mstb) {
1688 mstb = drm_dp_get_last_connected_port_and_mstb(mgr, port->parent, &port_num);
1690 if (!mstb) {
1691 drm_dp_put_port(port);
1692 return -EINVAL;
1696 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1697 if (!txmsg) {
1698 ret = -ENOMEM;
1699 goto fail_put;
1702 for (i = 0; i < port->num_sdp_streams; i++)
1703 sinks[i] = i;
1705 txmsg->dst = mstb;
1706 len = build_allocate_payload(txmsg, port_num,
1708 pbn, port->num_sdp_streams, sinks);
1710 drm_dp_queue_down_tx(mgr, txmsg);
1712 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1713 if (ret > 0) {
1714 if (txmsg->reply.reply_type == 1) {
1715 ret = -EINVAL;
1716 } else
1717 ret = 0;
1719 kfree(txmsg);
1720 fail_put:
1721 drm_dp_put_mst_branch_device(mstb);
1722 drm_dp_put_port(port);
1723 return ret;
1726 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1727 int id,
1728 struct drm_dp_payload *payload)
1730 int ret;
1732 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1733 if (ret < 0) {
1734 payload->payload_state = 0;
1735 return ret;
1737 payload->payload_state = DP_PAYLOAD_LOCAL;
1738 return 0;
1741 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1742 struct drm_dp_mst_port *port,
1743 int id,
1744 struct drm_dp_payload *payload)
1746 int ret;
1747 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1748 if (ret < 0)
1749 return ret;
1750 payload->payload_state = DP_PAYLOAD_REMOTE;
1751 return ret;
1754 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1755 struct drm_dp_mst_port *port,
1756 int id,
1757 struct drm_dp_payload *payload)
1759 DRM_DEBUG_KMS("\n");
1760 /* its okay for these to fail */
1761 if (port) {
1762 drm_dp_payload_send_msg(mgr, port, id, 0);
1765 drm_dp_dpcd_write_payload(mgr, id, payload);
1766 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
1767 return 0;
1770 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1771 int id,
1772 struct drm_dp_payload *payload)
1774 payload->payload_state = 0;
1775 return 0;
1779 * drm_dp_update_payload_part1() - Execute payload update part 1
1780 * @mgr: manager to use.
1782 * This iterates over all proposed virtual channels, and tries to
1783 * allocate space in the link for them. For 0->slots transitions,
1784 * this step just writes the VCPI to the MST device. For slots->0
1785 * transitions, this writes the updated VCPIs and removes the
1786 * remote VC payloads.
1788 * after calling this the driver should generate ACT and payload
1789 * packets.
1791 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1793 int i, j;
1794 int cur_slots = 1;
1795 struct drm_dp_payload req_payload;
1796 struct drm_dp_mst_port *port;
1798 mutex_lock(&mgr->payload_lock);
1799 for (i = 0; i < mgr->max_payloads; i++) {
1800 /* solve the current payloads - compare to the hw ones
1801 - update the hw view */
1802 req_payload.start_slot = cur_slots;
1803 if (mgr->proposed_vcpis[i]) {
1804 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1805 port = drm_dp_get_validated_port_ref(mgr, port);
1806 if (!port) {
1807 mutex_unlock(&mgr->payload_lock);
1808 return -EINVAL;
1810 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1811 req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
1812 } else {
1813 port = NULL;
1814 req_payload.num_slots = 0;
1817 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1818 mgr->payloads[i].start_slot = req_payload.start_slot;
1820 /* work out what is required to happen with this payload */
1821 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
1823 /* need to push an update for this payload */
1824 if (req_payload.num_slots) {
1825 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
1826 mgr->payloads[i].num_slots = req_payload.num_slots;
1827 mgr->payloads[i].vcpi = req_payload.vcpi;
1828 } else if (mgr->payloads[i].num_slots) {
1829 mgr->payloads[i].num_slots = 0;
1830 drm_dp_destroy_payload_step1(mgr, port, mgr->payloads[i].vcpi, &mgr->payloads[i]);
1831 req_payload.payload_state = mgr->payloads[i].payload_state;
1832 mgr->payloads[i].start_slot = 0;
1834 mgr->payloads[i].payload_state = req_payload.payload_state;
1836 cur_slots += req_payload.num_slots;
1838 if (port)
1839 drm_dp_put_port(port);
1842 for (i = 0; i < mgr->max_payloads; i++) {
1843 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1844 DRM_DEBUG_KMS("removing payload %d\n", i);
1845 for (j = i; j < mgr->max_payloads - 1; j++) {
1846 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1847 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1848 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1849 set_bit(j + 1, &mgr->payload_mask);
1850 } else {
1851 clear_bit(j + 1, &mgr->payload_mask);
1854 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1855 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1856 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1860 mutex_unlock(&mgr->payload_lock);
1862 return 0;
1864 EXPORT_SYMBOL(drm_dp_update_payload_part1);
1867 * drm_dp_update_payload_part2() - Execute payload update part 2
1868 * @mgr: manager to use.
1870 * This iterates over all proposed virtual channels, and tries to
1871 * allocate space in the link for them. For 0->slots transitions,
1872 * this step writes the remote VC payload commands. For slots->0
1873 * this just resets some internal state.
1875 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1877 struct drm_dp_mst_port *port;
1878 int i;
1879 int ret = 0;
1880 mutex_lock(&mgr->payload_lock);
1881 for (i = 0; i < mgr->max_payloads; i++) {
1883 if (!mgr->proposed_vcpis[i])
1884 continue;
1886 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1888 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1889 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1890 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1891 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1892 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1894 if (ret) {
1895 mutex_unlock(&mgr->payload_lock);
1896 return ret;
1899 mutex_unlock(&mgr->payload_lock);
1900 return 0;
1902 EXPORT_SYMBOL(drm_dp_update_payload_part2);
1904 #if 0 /* unused as of yet */
1905 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1906 struct drm_dp_mst_port *port,
1907 int offset, int size)
1909 int len;
1910 struct drm_dp_sideband_msg_tx *txmsg;
1912 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1913 if (!txmsg)
1914 return -ENOMEM;
1916 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1917 txmsg->dst = port->parent;
1919 drm_dp_queue_down_tx(mgr, txmsg);
1921 return 0;
1923 #endif
1925 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1926 struct drm_dp_mst_port *port,
1927 int offset, int size, u8 *bytes)
1929 int len;
1930 int ret;
1931 struct drm_dp_sideband_msg_tx *txmsg;
1932 struct drm_dp_mst_branch *mstb;
1934 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1935 if (!mstb)
1936 return -EINVAL;
1938 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1939 if (!txmsg) {
1940 ret = -ENOMEM;
1941 goto fail_put;
1944 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1945 txmsg->dst = mstb;
1947 drm_dp_queue_down_tx(mgr, txmsg);
1949 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1950 if (ret > 0) {
1951 if (txmsg->reply.reply_type == 1) {
1952 ret = -EINVAL;
1953 } else
1954 ret = 0;
1956 kfree(txmsg);
1957 fail_put:
1958 drm_dp_put_mst_branch_device(mstb);
1959 return ret;
1962 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1964 struct drm_dp_sideband_msg_reply_body reply;
1966 reply.reply_type = 0;
1967 reply.req_type = req_type;
1968 drm_dp_encode_sideband_reply(&reply, msg);
1969 return 0;
1972 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1973 struct drm_dp_mst_branch *mstb,
1974 int req_type, int seqno, bool broadcast)
1976 struct drm_dp_sideband_msg_tx *txmsg;
1978 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1979 if (!txmsg)
1980 return -ENOMEM;
1982 txmsg->dst = mstb;
1983 txmsg->seqno = seqno;
1984 drm_dp_encode_up_ack_reply(txmsg, req_type);
1986 mutex_lock(&mgr->qlock);
1988 process_single_up_tx_qlock(mgr, txmsg);
1990 mutex_unlock(&mgr->qlock);
1992 kfree(txmsg);
1993 return 0;
1996 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1997 int dp_link_count,
1998 int *out)
2000 switch (dp_link_bw) {
2001 default:
2002 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2003 dp_link_bw, dp_link_count);
2004 return false;
2006 case DP_LINK_BW_1_62:
2007 *out = 3 * dp_link_count;
2008 break;
2009 case DP_LINK_BW_2_7:
2010 *out = 5 * dp_link_count;
2011 break;
2012 case DP_LINK_BW_5_4:
2013 *out = 10 * dp_link_count;
2014 break;
2016 return true;
2020 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2021 * @mgr: manager to set state for
2022 * @mst_state: true to enable MST on this connector - false to disable.
2024 * This is called by the driver when it detects an MST capable device plugged
2025 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2027 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2029 int ret = 0;
2030 struct drm_dp_mst_branch *mstb = NULL;
2032 mutex_lock(&mgr->lock);
2033 if (mst_state == mgr->mst_state)
2034 goto out_unlock;
2036 mgr->mst_state = mst_state;
2037 /* set the device into MST mode */
2038 if (mst_state) {
2039 WARN_ON(mgr->mst_primary);
2041 /* get dpcd info */
2042 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2043 if (ret != DP_RECEIVER_CAP_SIZE) {
2044 DRM_DEBUG_KMS("failed to read DPCD\n");
2045 goto out_unlock;
2048 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2049 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2050 &mgr->pbn_div)) {
2051 ret = -EINVAL;
2052 goto out_unlock;
2055 mgr->total_pbn = 2560;
2056 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
2057 mgr->avail_slots = mgr->total_slots;
2059 /* add initial branch device at LCT 1 */
2060 mstb = drm_dp_add_mst_branch_device(1, NULL);
2061 if (mstb == NULL) {
2062 ret = -ENOMEM;
2063 goto out_unlock;
2065 mstb->mgr = mgr;
2067 /* give this the main reference */
2068 mgr->mst_primary = mstb;
2069 kref_get(&mgr->mst_primary->kref);
2071 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2072 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2073 if (ret < 0) {
2074 goto out_unlock;
2078 struct drm_dp_payload reset_pay;
2079 reset_pay.start_slot = 0;
2080 reset_pay.num_slots = 0x3f;
2081 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2084 queue_work(system_long_wq, &mgr->work);
2086 ret = 0;
2087 } else {
2088 /* disable MST on the device */
2089 mstb = mgr->mst_primary;
2090 mgr->mst_primary = NULL;
2091 /* this can fail if the device is gone */
2092 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2093 ret = 0;
2094 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2095 mgr->payload_mask = 0;
2096 set_bit(0, &mgr->payload_mask);
2097 mgr->vcpi_mask = 0;
2100 out_unlock:
2101 mutex_unlock(&mgr->lock);
2102 if (mstb)
2103 drm_dp_put_mst_branch_device(mstb);
2104 return ret;
2107 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2110 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2111 * @mgr: manager to suspend
2113 * This function tells the MST device that we can't handle UP messages
2114 * anymore. This should stop it from sending any since we are suspended.
2116 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2118 mutex_lock(&mgr->lock);
2119 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2120 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2121 mutex_unlock(&mgr->lock);
2122 flush_work(&mgr->work);
2123 flush_work(&mgr->destroy_connector_work);
2125 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2128 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2129 * @mgr: manager to resume
2131 * This will fetch DPCD and see if the device is still there,
2132 * if it is, it will rewrite the MSTM control bits, and return.
2134 * if the device fails this returns -1, and the driver should do
2135 * a full MST reprobe, in case we were undocked.
2137 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2139 int ret = 0;
2141 mutex_lock(&mgr->lock);
2143 if (mgr->mst_primary) {
2144 int sret;
2145 u8 guid[16];
2147 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2148 if (sret != DP_RECEIVER_CAP_SIZE) {
2149 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2150 ret = -1;
2151 goto out_unlock;
2154 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2155 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2156 if (ret < 0) {
2157 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2158 ret = -1;
2159 goto out_unlock;
2162 /* Some hubs forget their guids after they resume */
2163 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2164 if (sret != 16) {
2165 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2166 ret = -1;
2167 goto out_unlock;
2169 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2171 ret = 0;
2172 } else
2173 ret = -1;
2175 out_unlock:
2176 mutex_unlock(&mgr->lock);
2177 return ret;
2179 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2181 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2183 int len;
2184 u8 replyblock[32];
2185 int replylen, origlen, curreply;
2186 int ret;
2187 struct drm_dp_sideband_msg_rx *msg;
2188 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2189 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2191 len = min(mgr->max_dpcd_transaction_bytes, 16);
2192 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2193 replyblock, len);
2194 if (ret != len) {
2195 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2196 return false;
2198 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2199 if (!ret) {
2200 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2201 return false;
2203 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2205 origlen = replylen;
2206 replylen -= len;
2207 curreply = len;
2208 while (replylen > 0) {
2209 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2210 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2211 replyblock, len);
2212 if (ret != len) {
2213 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2214 len, ret);
2215 return false;
2218 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2219 if (!ret) {
2220 DRM_DEBUG_KMS("failed to build sideband msg\n");
2221 return false;
2224 curreply += len;
2225 replylen -= len;
2227 return true;
2230 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2232 int ret = 0;
2234 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2235 memset(&mgr->down_rep_recv, 0,
2236 sizeof(struct drm_dp_sideband_msg_rx));
2237 return 0;
2240 if (mgr->down_rep_recv.have_eomt) {
2241 struct drm_dp_sideband_msg_tx *txmsg;
2242 struct drm_dp_mst_branch *mstb;
2243 int slot = -1;
2244 mstb = drm_dp_get_mst_branch_device(mgr,
2245 mgr->down_rep_recv.initial_hdr.lct,
2246 mgr->down_rep_recv.initial_hdr.rad);
2248 if (!mstb) {
2249 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2250 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2251 return 0;
2254 /* find the message */
2255 slot = mgr->down_rep_recv.initial_hdr.seqno;
2256 mutex_lock(&mgr->qlock);
2257 txmsg = mstb->tx_slots[slot];
2258 /* remove from slots */
2259 mutex_unlock(&mgr->qlock);
2261 if (!txmsg) {
2262 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2263 mstb,
2264 mgr->down_rep_recv.initial_hdr.seqno,
2265 mgr->down_rep_recv.initial_hdr.lct,
2266 mgr->down_rep_recv.initial_hdr.rad[0],
2267 mgr->down_rep_recv.msg[0]);
2268 drm_dp_put_mst_branch_device(mstb);
2269 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2270 return 0;
2273 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2274 if (txmsg->reply.reply_type == 1) {
2275 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);
2278 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2279 drm_dp_put_mst_branch_device(mstb);
2281 mutex_lock(&mgr->qlock);
2282 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2283 mstb->tx_slots[slot] = NULL;
2284 mutex_unlock(&mgr->qlock);
2286 wake_up(&mgr->tx_waitq);
2288 return ret;
2291 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2293 int ret = 0;
2295 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2296 memset(&mgr->up_req_recv, 0,
2297 sizeof(struct drm_dp_sideband_msg_rx));
2298 return 0;
2301 if (mgr->up_req_recv.have_eomt) {
2302 struct drm_dp_sideband_msg_req_body msg;
2303 struct drm_dp_mst_branch *mstb = NULL;
2304 bool seqno;
2306 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2307 mstb = drm_dp_get_mst_branch_device(mgr,
2308 mgr->up_req_recv.initial_hdr.lct,
2309 mgr->up_req_recv.initial_hdr.rad);
2310 if (!mstb) {
2311 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2312 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2313 return 0;
2317 seqno = mgr->up_req_recv.initial_hdr.seqno;
2318 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2320 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2321 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2323 if (!mstb)
2324 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2326 if (!mstb) {
2327 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2328 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2329 return 0;
2332 drm_dp_update_port(mstb, &msg.u.conn_stat);
2334 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);
2335 (*mgr->cbs->hotplug)(mgr);
2337 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2338 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2339 if (!mstb)
2340 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2342 if (!mstb) {
2343 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2344 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2345 return 0;
2348 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2351 if (mstb)
2352 drm_dp_put_mst_branch_device(mstb);
2354 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2356 return ret;
2360 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2361 * @mgr: manager to notify irq for.
2362 * @esi: 4 bytes from SINK_COUNT_ESI
2363 * @handled: whether the hpd interrupt was consumed or not
2365 * This should be called from the driver when it detects a short IRQ,
2366 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2367 * topology manager will process the sideband messages received as a result
2368 * of this.
2370 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2372 int ret = 0;
2373 int sc;
2374 *handled = false;
2375 sc = esi[0] & 0x3f;
2377 if (sc != mgr->sink_count) {
2378 mgr->sink_count = sc;
2379 *handled = true;
2382 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2383 ret = drm_dp_mst_handle_down_rep(mgr);
2384 *handled = true;
2387 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2388 ret |= drm_dp_mst_handle_up_req(mgr);
2389 *handled = true;
2392 drm_dp_mst_kick_tx(mgr);
2393 return ret;
2395 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2398 * drm_dp_mst_detect_port() - get connection status for an MST port
2399 * @connector: DRM connector for this port
2400 * @mgr: manager for this port
2401 * @port: unverified pointer to a port
2403 * This returns the current connection state for a port. It validates the
2404 * port pointer still exists so the caller doesn't require a reference
2406 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2407 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2409 enum drm_connector_status status = connector_status_disconnected;
2411 /* we need to search for the port in the mgr in case its gone */
2412 port = drm_dp_get_validated_port_ref(mgr, port);
2413 if (!port)
2414 return connector_status_disconnected;
2416 if (!port->ddps)
2417 goto out;
2419 switch (port->pdt) {
2420 case DP_PEER_DEVICE_NONE:
2421 case DP_PEER_DEVICE_MST_BRANCHING:
2422 break;
2424 case DP_PEER_DEVICE_SST_SINK:
2425 status = connector_status_connected;
2426 /* for logical ports - cache the EDID */
2427 if (port->port_num >= 8 && !port->cached_edid) {
2428 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2430 break;
2431 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2432 if (port->ldps)
2433 status = connector_status_connected;
2434 break;
2436 out:
2437 drm_dp_put_port(port);
2438 return status;
2440 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2443 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2444 * @mgr: manager for this port
2445 * @port: unverified pointer to a port.
2447 * This returns whether the port supports audio or not.
2449 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2450 struct drm_dp_mst_port *port)
2452 bool ret = false;
2454 port = drm_dp_get_validated_port_ref(mgr, port);
2455 if (!port)
2456 return ret;
2457 ret = port->has_audio;
2458 drm_dp_put_port(port);
2459 return ret;
2461 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2464 * drm_dp_mst_get_edid() - get EDID for an MST port
2465 * @connector: toplevel connector to get EDID for
2466 * @mgr: manager for this port
2467 * @port: unverified pointer to a port.
2469 * This returns an EDID for the port connected to a connector,
2470 * It validates the pointer still exists so the caller doesn't require a
2471 * reference.
2473 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2475 struct edid *edid = NULL;
2477 /* we need to search for the port in the mgr in case its gone */
2478 port = drm_dp_get_validated_port_ref(mgr, port);
2479 if (!port)
2480 return NULL;
2482 if (port->cached_edid)
2483 edid = drm_edid_duplicate(port->cached_edid);
2484 else {
2485 edid = drm_get_edid(connector, &port->aux.ddc);
2486 drm_mode_connector_set_tile_property(connector);
2488 port->has_audio = drm_detect_monitor_audio(edid);
2489 drm_dp_put_port(port);
2490 return edid;
2492 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2495 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2496 * @mgr: manager to use
2497 * @pbn: payload bandwidth to convert into slots.
2499 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2500 int pbn)
2502 int num_slots;
2504 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2506 if (num_slots > mgr->avail_slots)
2507 return -ENOSPC;
2508 return num_slots;
2510 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2512 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2513 struct drm_dp_vcpi *vcpi, int pbn)
2515 int num_slots;
2516 int ret;
2518 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2520 if (num_slots > mgr->avail_slots)
2521 return -ENOSPC;
2523 vcpi->pbn = pbn;
2524 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2525 vcpi->num_slots = num_slots;
2527 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2528 if (ret < 0)
2529 return ret;
2530 return 0;
2534 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2535 * @mgr: manager for this port
2536 * @port: port to allocate a virtual channel for.
2537 * @pbn: payload bandwidth number to request
2538 * @slots: returned number of slots for this PBN.
2540 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2542 int ret;
2544 port = drm_dp_get_validated_port_ref(mgr, port);
2545 if (!port)
2546 return false;
2548 if (port->vcpi.vcpi > 0) {
2549 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2550 if (pbn == port->vcpi.pbn) {
2551 *slots = port->vcpi.num_slots;
2552 drm_dp_put_port(port);
2553 return true;
2557 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2558 if (ret) {
2559 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2560 goto out;
2562 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2563 *slots = port->vcpi.num_slots;
2565 drm_dp_put_port(port);
2566 return true;
2567 out:
2568 return false;
2570 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2572 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2574 int slots = 0;
2575 port = drm_dp_get_validated_port_ref(mgr, port);
2576 if (!port)
2577 return slots;
2579 slots = port->vcpi.num_slots;
2580 drm_dp_put_port(port);
2581 return slots;
2583 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2586 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2587 * @mgr: manager for this port
2588 * @port: unverified pointer to a port.
2590 * This just resets the number of slots for the ports VCPI for later programming.
2592 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2594 port = drm_dp_get_validated_port_ref(mgr, port);
2595 if (!port)
2596 return;
2597 port->vcpi.num_slots = 0;
2598 drm_dp_put_port(port);
2600 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2603 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2604 * @mgr: manager for this port
2605 * @port: unverified port to deallocate vcpi for
2607 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2609 port = drm_dp_get_validated_port_ref(mgr, port);
2610 if (!port)
2611 return;
2613 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2614 port->vcpi.num_slots = 0;
2615 port->vcpi.pbn = 0;
2616 port->vcpi.aligned_pbn = 0;
2617 port->vcpi.vcpi = 0;
2618 drm_dp_put_port(port);
2620 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2622 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2623 int id, struct drm_dp_payload *payload)
2625 u8 payload_alloc[3], status;
2626 int ret;
2627 int retries = 0;
2629 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2630 DP_PAYLOAD_TABLE_UPDATED);
2632 payload_alloc[0] = id;
2633 payload_alloc[1] = payload->start_slot;
2634 payload_alloc[2] = payload->num_slots;
2636 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2637 if (ret != 3) {
2638 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2639 goto fail;
2642 retry:
2643 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2644 if (ret < 0) {
2645 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2646 goto fail;
2649 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2650 retries++;
2651 if (retries < 20) {
2652 usleep_range(10000, 20000);
2653 goto retry;
2655 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2656 ret = -EINVAL;
2657 goto fail;
2659 ret = 0;
2660 fail:
2661 return ret;
2666 * drm_dp_check_act_status() - Check ACT handled status.
2667 * @mgr: manager to use
2669 * Check the payload status bits in the DPCD for ACT handled completion.
2671 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2673 u8 status;
2674 int ret;
2675 int count = 0;
2677 do {
2678 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2680 if (ret < 0) {
2681 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2682 goto fail;
2685 if (status & DP_PAYLOAD_ACT_HANDLED)
2686 break;
2687 count++;
2688 udelay(100);
2690 } while (count < 30);
2692 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2693 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2694 ret = -EINVAL;
2695 goto fail;
2697 return 0;
2698 fail:
2699 return ret;
2701 EXPORT_SYMBOL(drm_dp_check_act_status);
2704 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2705 * @clock: dot clock for the mode
2706 * @bpp: bpp for the mode.
2708 * This uses the formula in the spec to calculate the PBN value for a mode.
2710 int drm_dp_calc_pbn_mode(int clock, int bpp)
2712 u64 kbps;
2713 s64 peak_kbps;
2714 u32 numerator;
2715 u32 denominator;
2717 kbps = clock * bpp;
2720 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2721 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2722 * common multiplier to render an integer PBN for all link rate/lane
2723 * counts combinations
2724 * calculate
2725 * peak_kbps *= (1006/1000)
2726 * peak_kbps *= (64/54)
2727 * peak_kbps *= 8 convert to bytes
2730 numerator = 64 * 1006;
2731 denominator = 54 * 8 * 1000 * 1000;
2733 kbps *= numerator;
2734 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
2736 return drm_fixp2int_ceil(peak_kbps);
2738 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2740 static int test_calc_pbn_mode(void)
2742 int ret;
2743 ret = drm_dp_calc_pbn_mode(154000, 30);
2744 if (ret != 689) {
2745 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2746 154000, 30, 689, ret);
2747 return -EINVAL;
2749 ret = drm_dp_calc_pbn_mode(234000, 30);
2750 if (ret != 1047) {
2751 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2752 234000, 30, 1047, ret);
2753 return -EINVAL;
2755 ret = drm_dp_calc_pbn_mode(297000, 24);
2756 if (ret != 1063) {
2757 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2758 297000, 24, 1063, ret);
2759 return -EINVAL;
2761 return 0;
2764 /* we want to kick the TX after we've ack the up/down IRQs. */
2765 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2767 queue_work(system_long_wq, &mgr->tx_work);
2770 static void drm_dp_mst_dump_mstb(struct seq_file *m,
2771 struct drm_dp_mst_branch *mstb)
2773 struct drm_dp_mst_port *port;
2774 int tabs = mstb->lct;
2775 char prefix[10];
2776 int i;
2778 for (i = 0; i < tabs; i++)
2779 prefix[i] = '\t';
2780 prefix[i] = '\0';
2782 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2783 list_for_each_entry(port, &mstb->ports, next) {
2784 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);
2785 if (port->mstb)
2786 drm_dp_mst_dump_mstb(m, port->mstb);
2790 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2791 char *buf)
2793 int ret;
2794 int i;
2795 for (i = 0; i < 4; i++) {
2796 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2797 if (ret != 16)
2798 break;
2800 if (i == 4)
2801 return true;
2802 return false;
2805 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
2806 struct drm_dp_mst_port *port, char *name,
2807 int namelen)
2809 struct edid *mst_edid;
2811 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
2812 drm_edid_get_monitor_name(mst_edid, name, namelen);
2816 * drm_dp_mst_dump_topology(): dump topology to seq file.
2817 * @m: seq_file to dump output to
2818 * @mgr: manager to dump current topology for.
2820 * helper to dump MST topology to a seq file for debugfs.
2822 void drm_dp_mst_dump_topology(struct seq_file *m,
2823 struct drm_dp_mst_topology_mgr *mgr)
2825 int i;
2826 struct drm_dp_mst_port *port;
2828 mutex_lock(&mgr->lock);
2829 if (mgr->mst_primary)
2830 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2832 /* dump VCPIs */
2833 mutex_unlock(&mgr->lock);
2835 mutex_lock(&mgr->payload_lock);
2836 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
2837 mgr->max_payloads);
2839 for (i = 0; i < mgr->max_payloads; i++) {
2840 if (mgr->proposed_vcpis[i]) {
2841 char name[14];
2843 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2844 fetch_monitor_name(mgr, port, name, sizeof(name));
2845 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
2846 port->port_num, port->vcpi.vcpi,
2847 port->vcpi.num_slots,
2848 (*name != 0) ? name : "Unknown");
2849 } else
2850 seq_printf(m, "vcpi %d:unused\n", i);
2852 for (i = 0; i < mgr->max_payloads; i++) {
2853 seq_printf(m, "payload %d: %d, %d, %d\n",
2855 mgr->payloads[i].payload_state,
2856 mgr->payloads[i].start_slot,
2857 mgr->payloads[i].num_slots);
2861 mutex_unlock(&mgr->payload_lock);
2863 mutex_lock(&mgr->lock);
2864 if (mgr->mst_primary) {
2865 u8 buf[64];
2866 bool bret;
2867 int ret;
2868 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2869 seq_printf(m, "dpcd: ");
2870 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2871 seq_printf(m, "%02x ", buf[i]);
2872 seq_printf(m, "\n");
2873 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2874 seq_printf(m, "faux/mst: ");
2875 for (i = 0; i < 2; i++)
2876 seq_printf(m, "%02x ", buf[i]);
2877 seq_printf(m, "\n");
2878 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2879 seq_printf(m, "mst ctrl: ");
2880 for (i = 0; i < 1; i++)
2881 seq_printf(m, "%02x ", buf[i]);
2882 seq_printf(m, "\n");
2884 /* dump the standard OUI branch header */
2885 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2886 seq_printf(m, "branch oui: ");
2887 for (i = 0; i < 0x3; i++)
2888 seq_printf(m, "%02x", buf[i]);
2889 seq_printf(m, " devid: ");
2890 for (i = 0x3; i < 0x8 && buf[i]; i++)
2891 seq_printf(m, "%c", buf[i]);
2893 seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2894 seq_printf(m, "\n");
2895 bret = dump_dp_payload_table(mgr, buf);
2896 if (bret == true) {
2897 seq_printf(m, "payload table: ");
2898 for (i = 0; i < 63; i++)
2899 seq_printf(m, "%02x ", buf[i]);
2900 seq_printf(m, "\n");
2905 mutex_unlock(&mgr->lock);
2908 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2910 static void drm_dp_tx_work(struct work_struct *work)
2912 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2914 mutex_lock(&mgr->qlock);
2915 if (!list_empty(&mgr->tx_msg_downq))
2916 process_single_down_tx_qlock(mgr);
2917 mutex_unlock(&mgr->qlock);
2920 static void drm_dp_free_mst_port(struct kref *kref)
2922 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
2923 kref_put(&port->parent->kref, drm_dp_free_mst_branch_device);
2924 kfree(port);
2927 static void drm_dp_destroy_connector_work(struct work_struct *work)
2929 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
2930 struct drm_dp_mst_port *port;
2931 bool send_hotplug = false;
2933 * Not a regular list traverse as we have to drop the destroy
2934 * connector lock before destroying the connector, to avoid AB->BA
2935 * ordering between this lock and the config mutex.
2937 for (;;) {
2938 mutex_lock(&mgr->destroy_connector_lock);
2939 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2940 if (!port) {
2941 mutex_unlock(&mgr->destroy_connector_lock);
2942 break;
2944 list_del(&port->next);
2945 mutex_unlock(&mgr->destroy_connector_lock);
2947 kref_init(&port->kref);
2948 INIT_LIST_HEAD(&port->next);
2950 mgr->cbs->destroy_connector(mgr, port->connector);
2952 drm_dp_port_teardown_pdt(port, port->pdt);
2953 port->pdt = DP_PEER_DEVICE_NONE;
2955 if (!port->input && port->vcpi.vcpi > 0) {
2956 drm_dp_mst_reset_vcpi_slots(mgr, port);
2957 drm_dp_update_payload_part1(mgr);
2958 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2961 kref_put(&port->kref, drm_dp_free_mst_port);
2962 send_hotplug = true;
2964 if (send_hotplug)
2965 (*mgr->cbs->hotplug)(mgr);
2969 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2970 * @mgr: manager struct to initialise
2971 * @dev: device providing this structure - for i2c addition.
2972 * @aux: DP helper aux channel to talk to this device
2973 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2974 * @max_payloads: maximum number of payloads this GPU can source
2975 * @conn_base_id: the connector object ID the MST device is connected to.
2977 * Return 0 for success, or negative error code on failure
2979 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2980 struct device *dev, struct drm_dp_aux *aux,
2981 int max_dpcd_transaction_bytes,
2982 int max_payloads, int conn_base_id)
2984 mutex_init(&mgr->lock);
2985 mutex_init(&mgr->qlock);
2986 mutex_init(&mgr->payload_lock);
2987 mutex_init(&mgr->destroy_connector_lock);
2988 INIT_LIST_HEAD(&mgr->tx_msg_downq);
2989 INIT_LIST_HEAD(&mgr->destroy_connector_list);
2990 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2991 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
2992 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
2993 init_waitqueue_head(&mgr->tx_waitq);
2994 mgr->dev = dev;
2995 mgr->aux = aux;
2996 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2997 mgr->max_payloads = max_payloads;
2998 mgr->conn_base_id = conn_base_id;
2999 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3000 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3001 return -EINVAL;
3002 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3003 if (!mgr->payloads)
3004 return -ENOMEM;
3005 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3006 if (!mgr->proposed_vcpis)
3007 return -ENOMEM;
3008 set_bit(0, &mgr->payload_mask);
3009 if (test_calc_pbn_mode() < 0)
3010 DRM_ERROR("MST PBN self-test failed\n");
3012 return 0;
3014 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3017 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3018 * @mgr: manager to destroy
3020 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3022 flush_work(&mgr->work);
3023 flush_work(&mgr->destroy_connector_work);
3024 mutex_lock(&mgr->payload_lock);
3025 kfree(mgr->payloads);
3026 mgr->payloads = NULL;
3027 kfree(mgr->proposed_vcpis);
3028 mgr->proposed_vcpis = NULL;
3029 mutex_unlock(&mgr->payload_lock);
3030 mgr->dev = NULL;
3031 mgr->aux = NULL;
3033 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3035 /* I2C device */
3036 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3037 int num)
3039 struct drm_dp_aux *aux = adapter->algo_data;
3040 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3041 struct drm_dp_mst_branch *mstb;
3042 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3043 unsigned int i;
3044 bool reading = false;
3045 struct drm_dp_sideband_msg_req_body msg;
3046 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3047 int ret;
3049 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
3050 if (!mstb)
3051 return -EREMOTEIO;
3053 /* construct i2c msg */
3054 /* see if last msg is a read */
3055 if (msgs[num - 1].flags & I2C_M_RD)
3056 reading = true;
3058 if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
3059 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3060 ret = -EIO;
3061 goto out;
3064 memset(&msg, 0, sizeof(msg));
3065 msg.req_type = DP_REMOTE_I2C_READ;
3066 msg.u.i2c_read.num_transactions = num - 1;
3067 msg.u.i2c_read.port_number = port->port_num;
3068 for (i = 0; i < num - 1; i++) {
3069 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3070 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3071 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3073 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3074 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3076 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3077 if (!txmsg) {
3078 ret = -ENOMEM;
3079 goto out;
3082 txmsg->dst = mstb;
3083 drm_dp_encode_sideband_req(&msg, txmsg);
3085 drm_dp_queue_down_tx(mgr, txmsg);
3087 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3088 if (ret > 0) {
3090 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3091 ret = -EREMOTEIO;
3092 goto out;
3094 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3095 ret = -EIO;
3096 goto out;
3098 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3099 ret = num;
3101 out:
3102 kfree(txmsg);
3103 drm_dp_put_mst_branch_device(mstb);
3104 return ret;
3107 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3109 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3110 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3111 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3112 I2C_FUNC_10BIT_ADDR;
3115 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3116 .functionality = drm_dp_mst_i2c_functionality,
3117 .master_xfer = drm_dp_mst_i2c_xfer,
3121 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3122 * @aux: DisplayPort AUX channel
3124 * Returns 0 on success or a negative error code on failure.
3126 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3128 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3129 aux->ddc.algo_data = aux;
3130 aux->ddc.retries = 3;
3132 aux->ddc.class = I2C_CLASS_DDC;
3133 aux->ddc.owner = THIS_MODULE;
3134 aux->ddc.dev.parent = aux->dev;
3135 aux->ddc.dev.of_node = aux->dev->of_node;
3137 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3138 sizeof(aux->ddc.name));
3140 return i2c_add_adapter(&aux->ddc);
3144 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3145 * @aux: DisplayPort AUX channel
3147 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3149 i2c_del_adapter(&aux->ddc);