ipfw: Add icmpcodes support.
[dragonfly.git] / sys / dev / drm / drm_dp_mst_topology.c
blob681e13f77a97032a125357ecf9b9275727518063
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/errno.h>
26 #include <linux/sched.h>
27 #include <linux/seq_file.h>
28 #include <linux/i2c.h>
29 #include <drm/drm_dp_mst_helper.h>
30 #include <drm/drmP.h>
32 #include <drm/drm_fixed.h>
34 /**
35 * DOC: dp mst helper
37 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
38 * protocol. The helpers contain a topology manager and bandwidth manager.
39 * The helpers encapsulate the sending and received of sideband msgs.
41 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
42 char *buf);
43 static int test_calc_pbn_mode(void);
45 static void drm_dp_put_port(struct drm_dp_mst_port *port);
47 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
48 int id,
49 struct drm_dp_payload *payload);
51 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
52 struct drm_dp_mst_port *port,
53 int offset, int size, u8 *bytes);
55 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
56 struct drm_dp_mst_branch *mstb);
57 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
58 struct drm_dp_mst_branch *mstb,
59 struct drm_dp_mst_port *port);
60 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
61 u8 *guid);
63 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
64 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
65 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
66 /* sideband msg handling */
67 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
69 u8 bitmask = 0x80;
70 u8 bitshift = 7;
71 u8 array_index = 0;
72 int number_of_bits = num_nibbles * 4;
73 u8 remainder = 0;
75 while (number_of_bits != 0) {
76 number_of_bits--;
77 remainder <<= 1;
78 remainder |= (data[array_index] & bitmask) >> bitshift;
79 bitmask >>= 1;
80 bitshift--;
81 if (bitmask == 0) {
82 bitmask = 0x80;
83 bitshift = 7;
84 array_index++;
86 if ((remainder & 0x10) == 0x10)
87 remainder ^= 0x13;
90 number_of_bits = 4;
91 while (number_of_bits != 0) {
92 number_of_bits--;
93 remainder <<= 1;
94 if ((remainder & 0x10) != 0)
95 remainder ^= 0x13;
98 return remainder;
101 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
103 u8 bitmask = 0x80;
104 u8 bitshift = 7;
105 u8 array_index = 0;
106 int number_of_bits = number_of_bytes * 8;
107 u16 remainder = 0;
109 while (number_of_bits != 0) {
110 number_of_bits--;
111 remainder <<= 1;
112 remainder |= (data[array_index] & bitmask) >> bitshift;
113 bitmask >>= 1;
114 bitshift--;
115 if (bitmask == 0) {
116 bitmask = 0x80;
117 bitshift = 7;
118 array_index++;
120 if ((remainder & 0x100) == 0x100)
121 remainder ^= 0xd5;
124 number_of_bits = 8;
125 while (number_of_bits != 0) {
126 number_of_bits--;
127 remainder <<= 1;
128 if ((remainder & 0x100) != 0)
129 remainder ^= 0xd5;
132 return remainder & 0xff;
134 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
136 u8 size = 3;
137 size += (hdr->lct / 2);
138 return size;
141 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
142 u8 *buf, int *len)
144 int idx = 0;
145 int i;
146 u8 crc4;
147 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
148 for (i = 0; i < (hdr->lct / 2); i++)
149 buf[idx++] = hdr->rad[i];
150 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
151 (hdr->msg_len & 0x3f);
152 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
154 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
155 buf[idx - 1] |= (crc4 & 0xf);
157 *len = idx;
160 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
161 u8 *buf, int buflen, u8 *hdrlen)
163 u8 crc4;
164 u8 len;
165 int i;
166 u8 idx;
167 if (buf[0] == 0)
168 return false;
169 len = 3;
170 len += ((buf[0] & 0xf0) >> 4) / 2;
171 if (len > buflen)
172 return false;
173 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
175 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
176 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
177 return false;
180 hdr->lct = (buf[0] & 0xf0) >> 4;
181 hdr->lcr = (buf[0] & 0xf);
182 idx = 1;
183 for (i = 0; i < (hdr->lct / 2); i++)
184 hdr->rad[i] = buf[idx++];
185 hdr->broadcast = (buf[idx] >> 7) & 0x1;
186 hdr->path_msg = (buf[idx] >> 6) & 0x1;
187 hdr->msg_len = buf[idx] & 0x3f;
188 idx++;
189 hdr->somt = (buf[idx] >> 7) & 0x1;
190 hdr->eomt = (buf[idx] >> 6) & 0x1;
191 hdr->seqno = (buf[idx] >> 4) & 0x1;
192 idx++;
193 *hdrlen = idx;
194 return true;
197 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
198 struct drm_dp_sideband_msg_tx *raw)
200 int idx = 0;
201 int i;
202 u8 *buf = raw->msg;
203 buf[idx++] = req->req_type & 0x7f;
205 switch (req->req_type) {
206 case DP_ENUM_PATH_RESOURCES:
207 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
208 idx++;
209 break;
210 case DP_ALLOCATE_PAYLOAD:
211 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
212 (req->u.allocate_payload.number_sdp_streams & 0xf);
213 idx++;
214 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
215 idx++;
216 buf[idx] = (req->u.allocate_payload.pbn >> 8);
217 idx++;
218 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
219 idx++;
220 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
221 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
222 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
223 idx++;
225 if (req->u.allocate_payload.number_sdp_streams & 1) {
226 i = req->u.allocate_payload.number_sdp_streams - 1;
227 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
228 idx++;
230 break;
231 case DP_QUERY_PAYLOAD:
232 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
233 idx++;
234 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
235 idx++;
236 break;
237 case DP_REMOTE_DPCD_READ:
238 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
239 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
240 idx++;
241 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
242 idx++;
243 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
244 idx++;
245 buf[idx] = (req->u.dpcd_read.num_bytes);
246 idx++;
247 break;
249 case DP_REMOTE_DPCD_WRITE:
250 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
251 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
252 idx++;
253 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
254 idx++;
255 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
256 idx++;
257 buf[idx] = (req->u.dpcd_write.num_bytes);
258 idx++;
259 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
260 idx += req->u.dpcd_write.num_bytes;
261 break;
262 case DP_REMOTE_I2C_READ:
263 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
264 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
265 idx++;
266 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
267 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
268 idx++;
269 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
270 idx++;
271 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
272 idx += req->u.i2c_read.transactions[i].num_bytes;
274 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
275 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
276 idx++;
278 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
279 idx++;
280 buf[idx] = (req->u.i2c_read.num_bytes_read);
281 idx++;
282 break;
284 case DP_REMOTE_I2C_WRITE:
285 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
286 idx++;
287 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
288 idx++;
289 buf[idx] = (req->u.i2c_write.num_bytes);
290 idx++;
291 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
292 idx += req->u.i2c_write.num_bytes;
293 break;
295 raw->cur_len = idx;
298 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
300 u8 crc4;
301 crc4 = drm_dp_msg_data_crc4(msg, len);
302 msg[len] = crc4;
305 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
306 struct drm_dp_sideband_msg_tx *raw)
308 int idx = 0;
309 u8 *buf = raw->msg;
311 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
313 raw->cur_len = idx;
316 /* this adds a chunk of msg to the builder to get the final msg */
317 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
318 u8 *replybuf, u8 replybuflen, bool hdr)
320 int ret;
321 u8 crc4;
323 if (hdr) {
324 u8 hdrlen;
325 struct drm_dp_sideband_msg_hdr recv_hdr;
326 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
327 if (ret == false) {
328 #if 0
329 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
330 #endif
331 return false;
334 /* get length contained in this portion */
335 msg->curchunk_len = recv_hdr.msg_len;
336 msg->curchunk_hdrlen = hdrlen;
338 /* we have already gotten an somt - don't bother parsing */
339 if (recv_hdr.somt && msg->have_somt)
340 return false;
342 if (recv_hdr.somt) {
343 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
344 msg->have_somt = true;
346 if (recv_hdr.eomt)
347 msg->have_eomt = true;
349 /* copy the bytes for the remainder of this header chunk */
350 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
351 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
352 } else {
353 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
354 msg->curchunk_idx += replybuflen;
357 if (msg->curchunk_idx >= msg->curchunk_len) {
358 /* do CRC */
359 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
360 /* copy chunk into bigger msg */
361 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
362 msg->curlen += msg->curchunk_len - 1;
364 return true;
367 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
368 struct drm_dp_sideband_msg_reply_body *repmsg)
370 int idx = 1;
371 int i;
372 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
373 idx += 16;
374 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
375 idx++;
376 if (idx > raw->curlen)
377 goto fail_len;
378 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
379 if (raw->msg[idx] & 0x80)
380 repmsg->u.link_addr.ports[i].input_port = 1;
382 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
383 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
385 idx++;
386 if (idx > raw->curlen)
387 goto fail_len;
388 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
389 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
390 if (repmsg->u.link_addr.ports[i].input_port == 0)
391 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
392 idx++;
393 if (idx > raw->curlen)
394 goto fail_len;
395 if (repmsg->u.link_addr.ports[i].input_port == 0) {
396 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
397 idx++;
398 if (idx > raw->curlen)
399 goto fail_len;
400 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
401 idx += 16;
402 if (idx > raw->curlen)
403 goto fail_len;
404 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
405 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
406 idx++;
409 if (idx > raw->curlen)
410 goto fail_len;
413 return true;
414 fail_len:
415 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
416 return false;
419 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
420 struct drm_dp_sideband_msg_reply_body *repmsg)
422 int idx = 1;
423 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
424 idx++;
425 if (idx > raw->curlen)
426 goto fail_len;
427 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
428 if (idx > raw->curlen)
429 goto fail_len;
431 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
432 return true;
433 fail_len:
434 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
435 return false;
438 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
439 struct drm_dp_sideband_msg_reply_body *repmsg)
441 int idx = 1;
442 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
443 idx++;
444 if (idx > raw->curlen)
445 goto fail_len;
446 return true;
447 fail_len:
448 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
449 return false;
452 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
453 struct drm_dp_sideband_msg_reply_body *repmsg)
455 int idx = 1;
457 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
458 idx++;
459 if (idx > raw->curlen)
460 goto fail_len;
461 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
462 idx++;
463 /* TODO check */
464 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
465 return true;
466 fail_len:
467 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
468 return false;
471 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
472 struct drm_dp_sideband_msg_reply_body *repmsg)
474 int idx = 1;
475 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
476 idx++;
477 if (idx > raw->curlen)
478 goto fail_len;
479 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
480 idx += 2;
481 if (idx > raw->curlen)
482 goto fail_len;
483 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
484 idx += 2;
485 if (idx > raw->curlen)
486 goto fail_len;
487 return true;
488 fail_len:
489 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
490 return false;
493 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
494 struct drm_dp_sideband_msg_reply_body *repmsg)
496 int idx = 1;
497 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
498 idx++;
499 if (idx > raw->curlen)
500 goto fail_len;
501 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
502 idx++;
503 if (idx > raw->curlen)
504 goto fail_len;
505 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
506 idx += 2;
507 if (idx > raw->curlen)
508 goto fail_len;
509 return true;
510 fail_len:
511 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
512 return false;
515 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
516 struct drm_dp_sideband_msg_reply_body *repmsg)
518 int idx = 1;
519 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
520 idx++;
521 if (idx > raw->curlen)
522 goto fail_len;
523 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
524 idx += 2;
525 if (idx > raw->curlen)
526 goto fail_len;
527 return true;
528 fail_len:
529 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
530 return false;
533 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
534 struct drm_dp_sideband_msg_reply_body *msg)
536 memset(msg, 0, sizeof(*msg));
537 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
538 msg->req_type = (raw->msg[0] & 0x7f);
540 if (msg->reply_type) {
541 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
542 msg->u.nak.reason = raw->msg[17];
543 msg->u.nak.nak_data = raw->msg[18];
544 return false;
547 switch (msg->req_type) {
548 case DP_LINK_ADDRESS:
549 return drm_dp_sideband_parse_link_address(raw, msg);
550 case DP_QUERY_PAYLOAD:
551 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
552 case DP_REMOTE_DPCD_READ:
553 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
554 case DP_REMOTE_DPCD_WRITE:
555 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
556 case DP_REMOTE_I2C_READ:
557 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
558 case DP_ENUM_PATH_RESOURCES:
559 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
560 case DP_ALLOCATE_PAYLOAD:
561 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
562 default:
563 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
564 return false;
568 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
569 struct drm_dp_sideband_msg_req_body *msg)
571 int idx = 1;
573 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
574 idx++;
575 if (idx > raw->curlen)
576 goto fail_len;
578 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
579 idx += 16;
580 if (idx > raw->curlen)
581 goto fail_len;
583 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
584 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
585 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
586 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
587 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
588 idx++;
589 return true;
590 fail_len:
591 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
592 return false;
595 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
596 struct drm_dp_sideband_msg_req_body *msg)
598 int idx = 1;
600 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
601 idx++;
602 if (idx > raw->curlen)
603 goto fail_len;
605 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
606 idx += 16;
607 if (idx > raw->curlen)
608 goto fail_len;
610 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
611 idx++;
612 return true;
613 fail_len:
614 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
615 return false;
618 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
619 struct drm_dp_sideband_msg_req_body *msg)
621 memset(msg, 0, sizeof(*msg));
622 msg->req_type = (raw->msg[0] & 0x7f);
624 switch (msg->req_type) {
625 case DP_CONNECTION_STATUS_NOTIFY:
626 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
627 case DP_RESOURCE_STATUS_NOTIFY:
628 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
629 default:
630 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
631 return false;
635 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
637 struct drm_dp_sideband_msg_req_body req;
639 req.req_type = DP_REMOTE_DPCD_WRITE;
640 req.u.dpcd_write.port_number = port_num;
641 req.u.dpcd_write.dpcd_address = offset;
642 req.u.dpcd_write.num_bytes = num_bytes;
643 req.u.dpcd_write.bytes = bytes;
644 drm_dp_encode_sideband_req(&req, msg);
646 return 0;
649 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
651 struct drm_dp_sideband_msg_req_body req;
653 req.req_type = DP_LINK_ADDRESS;
654 drm_dp_encode_sideband_req(&req, msg);
655 return 0;
658 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
660 struct drm_dp_sideband_msg_req_body req;
662 req.req_type = DP_ENUM_PATH_RESOURCES;
663 req.u.port_num.port_number = port_num;
664 drm_dp_encode_sideband_req(&req, msg);
665 msg->path_msg = true;
666 return 0;
669 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
670 u8 vcpi, uint16_t pbn,
671 u8 number_sdp_streams,
672 u8 *sdp_stream_sink)
674 struct drm_dp_sideband_msg_req_body req;
675 memset(&req, 0, sizeof(req));
676 req.req_type = DP_ALLOCATE_PAYLOAD;
677 req.u.allocate_payload.port_number = port_num;
678 req.u.allocate_payload.vcpi = vcpi;
679 req.u.allocate_payload.pbn = pbn;
680 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
681 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
682 number_sdp_streams);
683 drm_dp_encode_sideband_req(&req, msg);
684 msg->path_msg = true;
685 return 0;
688 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
689 struct drm_dp_vcpi *vcpi)
691 int ret, vcpi_ret;
693 mutex_lock(&mgr->payload_lock);
694 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
695 if (ret > mgr->max_payloads) {
696 ret = -EINVAL;
697 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
698 goto out_unlock;
701 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
702 if (vcpi_ret > mgr->max_payloads) {
703 ret = -EINVAL;
704 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
705 goto out_unlock;
708 set_bit(ret, &mgr->payload_mask);
709 set_bit(vcpi_ret, &mgr->vcpi_mask);
710 vcpi->vcpi = vcpi_ret + 1;
711 mgr->proposed_vcpis[ret - 1] = vcpi;
712 out_unlock:
713 mutex_unlock(&mgr->payload_lock);
714 return ret;
717 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
718 int vcpi)
720 int i;
721 if (vcpi == 0)
722 return;
724 mutex_lock(&mgr->payload_lock);
725 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
726 clear_bit(vcpi - 1, &mgr->vcpi_mask);
728 for (i = 0; i < mgr->max_payloads; i++) {
729 if (mgr->proposed_vcpis[i])
730 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
731 mgr->proposed_vcpis[i] = NULL;
732 clear_bit(i + 1, &mgr->payload_mask);
735 mutex_unlock(&mgr->payload_lock);
738 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
739 struct drm_dp_sideband_msg_tx *txmsg)
741 bool ret;
744 * All updates to txmsg->state are protected by mgr->qlock, and the two
745 * cases we check here are terminal states. For those the barriers
746 * provided by the wake_up/wait_event pair are enough.
748 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
749 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
750 return ret;
753 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
754 struct drm_dp_sideband_msg_tx *txmsg)
756 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
757 int ret;
759 ret = wait_event_timeout(mgr->tx_waitq,
760 check_txmsg_state(mgr, txmsg),
761 (4 * HZ));
762 mutex_lock(&mstb->mgr->qlock);
763 if (ret > 0) {
764 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
765 ret = -EIO;
766 goto out;
768 } else {
769 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
771 /* dump some state */
772 ret = -EIO;
774 /* remove from q */
775 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
776 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
777 list_del(&txmsg->next);
780 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
781 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
782 mstb->tx_slots[txmsg->seqno] = NULL;
785 out:
786 mutex_unlock(&mgr->qlock);
788 return ret;
791 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
793 struct drm_dp_mst_branch *mstb;
795 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
796 if (!mstb)
797 return NULL;
799 mstb->lct = lct;
800 if (lct > 1)
801 memcpy(mstb->rad, rad, lct / 2);
802 INIT_LIST_HEAD(&mstb->ports);
803 kref_init(&mstb->kref);
804 return mstb;
807 static void drm_dp_free_mst_port(struct kref *kref);
809 static void drm_dp_free_mst_branch_device(struct kref *kref)
811 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
812 if (mstb->port_parent) {
813 if (list_empty(&mstb->port_parent->next))
814 kref_put(&mstb->port_parent->kref, drm_dp_free_mst_port);
816 kfree(mstb);
819 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
821 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
822 struct drm_dp_mst_port *port, *tmp;
823 bool wake_tx = false;
826 * init kref again to be used by ports to remove mst branch when it is
827 * not needed anymore
829 kref_init(kref);
831 if (mstb->port_parent && list_empty(&mstb->port_parent->next))
832 kref_get(&mstb->port_parent->kref);
835 * destroy all ports - don't need lock
836 * as there are no more references to the mst branch
837 * device at this point.
839 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
840 list_del(&port->next);
841 drm_dp_put_port(port);
844 /* drop any tx slots msg */
845 mutex_lock(&mstb->mgr->qlock);
846 if (mstb->tx_slots[0]) {
847 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
848 mstb->tx_slots[0] = NULL;
849 wake_tx = true;
851 if (mstb->tx_slots[1]) {
852 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
853 mstb->tx_slots[1] = NULL;
854 wake_tx = true;
856 mutex_unlock(&mstb->mgr->qlock);
858 if (wake_tx)
859 wake_up(&mstb->mgr->tx_waitq);
861 kref_put(kref, drm_dp_free_mst_branch_device);
864 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
866 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
870 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
872 struct drm_dp_mst_branch *mstb;
874 switch (old_pdt) {
875 case DP_PEER_DEVICE_DP_LEGACY_CONV:
876 case DP_PEER_DEVICE_SST_SINK:
877 /* remove i2c over sideband */
878 drm_dp_mst_unregister_i2c_bus(&port->aux);
879 break;
880 case DP_PEER_DEVICE_MST_BRANCHING:
881 mstb = port->mstb;
882 port->mstb = NULL;
883 drm_dp_put_mst_branch_device(mstb);
884 break;
888 static void drm_dp_destroy_port(struct kref *kref)
890 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
891 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
893 if (!port->input) {
894 port->vcpi.num_slots = 0;
896 kfree(port->cached_edid);
899 * The only time we don't have a connector
900 * on an output port is if the connector init
901 * fails.
903 if (port->connector) {
904 /* we can't destroy the connector here, as
905 * we might be holding the mode_config.mutex
906 * from an EDID retrieval */
908 mutex_lock(&mgr->destroy_connector_lock);
909 kref_get(&port->parent->kref);
910 list_add(&port->next, &mgr->destroy_connector_list);
911 mutex_unlock(&mgr->destroy_connector_lock);
912 schedule_work(&mgr->destroy_connector_work);
913 return;
915 /* no need to clean up vcpi
916 * as if we have no connector we never setup a vcpi */
917 drm_dp_port_teardown_pdt(port, port->pdt);
919 kfree(port);
922 static void drm_dp_put_port(struct drm_dp_mst_port *port)
924 kref_put(&port->kref, drm_dp_destroy_port);
927 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)
929 struct drm_dp_mst_port *port;
930 struct drm_dp_mst_branch *rmstb;
931 if (to_find == mstb) {
932 kref_get(&mstb->kref);
933 return mstb;
935 list_for_each_entry(port, &mstb->ports, next) {
936 if (port->mstb) {
937 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
938 if (rmstb)
939 return rmstb;
942 return NULL;
945 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)
947 struct drm_dp_mst_branch *rmstb = NULL;
948 mutex_lock(&mgr->lock);
949 if (mgr->mst_primary)
950 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
951 mutex_unlock(&mgr->lock);
952 return rmstb;
955 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)
957 struct drm_dp_mst_port *port, *mport;
959 list_for_each_entry(port, &mstb->ports, next) {
960 if (port == to_find) {
961 kref_get(&port->kref);
962 return port;
964 if (port->mstb) {
965 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
966 if (mport)
967 return mport;
970 return NULL;
973 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)
975 struct drm_dp_mst_port *rport = NULL;
976 mutex_lock(&mgr->lock);
977 if (mgr->mst_primary)
978 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
979 mutex_unlock(&mgr->lock);
980 return rport;
983 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
985 struct drm_dp_mst_port *port;
987 list_for_each_entry(port, &mstb->ports, next) {
988 if (port->port_num == port_num) {
989 kref_get(&port->kref);
990 return port;
994 return NULL;
998 * calculate a new RAD for this MST branch device
999 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1000 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1002 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1003 u8 *rad)
1005 int parent_lct = port->parent->lct;
1006 int shift = 4;
1007 int idx = (parent_lct - 1) / 2;
1008 if (parent_lct > 1) {
1009 memcpy(rad, port->parent->rad, idx + 1);
1010 shift = (parent_lct % 2) ? 4 : 0;
1011 } else
1012 rad[0] = 0;
1014 rad[idx] |= port->port_num << shift;
1015 return parent_lct + 1;
1019 * return sends link address for new mstb
1021 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1023 int ret;
1024 u8 rad[6], lct;
1025 bool send_link = false;
1026 switch (port->pdt) {
1027 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1028 case DP_PEER_DEVICE_SST_SINK:
1029 /* add i2c over sideband */
1030 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1031 break;
1032 case DP_PEER_DEVICE_MST_BRANCHING:
1033 lct = drm_dp_calculate_rad(port, rad);
1035 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1036 port->mstb->mgr = port->mgr;
1037 port->mstb->port_parent = port;
1039 send_link = true;
1040 break;
1042 return send_link;
1045 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1047 int ret;
1049 memcpy(mstb->guid, guid, 16);
1051 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1052 if (mstb->port_parent) {
1053 ret = drm_dp_send_dpcd_write(
1054 mstb->mgr,
1055 mstb->port_parent,
1056 DP_GUID,
1058 mstb->guid);
1059 } else {
1061 ret = drm_dp_dpcd_write(
1062 mstb->mgr->aux,
1063 DP_GUID,
1064 mstb->guid,
1065 16);
1070 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1071 int pnum,
1072 char *proppath,
1073 size_t proppath_size)
1075 int i;
1076 char temp[8];
1077 ksnprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1078 for (i = 0; i < (mstb->lct - 1); i++) {
1079 int shift = (i % 2) ? 0 : 4;
1080 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1081 ksnprintf(temp, sizeof(temp), "-%d", port_num);
1082 strlcat(proppath, temp, proppath_size);
1084 ksnprintf(temp, sizeof(temp), "-%d", pnum);
1085 strlcat(proppath, temp, proppath_size);
1088 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1089 struct device *dev,
1090 struct drm_dp_link_addr_reply_port *port_msg)
1092 struct drm_dp_mst_port *port;
1093 bool ret;
1094 bool created = false;
1095 int old_pdt = 0;
1096 int old_ddps = 0;
1097 port = drm_dp_get_port(mstb, port_msg->port_number);
1098 if (!port) {
1099 port = kzalloc(sizeof(*port), GFP_KERNEL);
1100 if (!port)
1101 return;
1102 kref_init(&port->kref);
1103 port->parent = mstb;
1104 port->port_num = port_msg->port_number;
1105 port->mgr = mstb->mgr;
1106 port->aux.name = "DPMST";
1107 port->aux.dev = dev;
1108 created = true;
1109 } else {
1110 old_pdt = port->pdt;
1111 old_ddps = port->ddps;
1114 port->pdt = port_msg->peer_device_type;
1115 port->input = port_msg->input_port;
1116 port->mcs = port_msg->mcs;
1117 port->ddps = port_msg->ddps;
1118 port->ldps = port_msg->legacy_device_plug_status;
1119 port->dpcd_rev = port_msg->dpcd_revision;
1120 port->num_sdp_streams = port_msg->num_sdp_streams;
1121 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1123 /* manage mstb port lists with mgr lock - take a reference
1124 for this list */
1125 if (created) {
1126 mutex_lock(&mstb->mgr->lock);
1127 kref_get(&port->kref);
1128 list_add(&port->next, &mstb->ports);
1129 mutex_unlock(&mstb->mgr->lock);
1132 if (old_ddps != port->ddps) {
1133 if (port->ddps) {
1134 if (!port->input)
1135 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1136 } else {
1137 port->available_pbn = 0;
1141 if (old_pdt != port->pdt && !port->input) {
1142 drm_dp_port_teardown_pdt(port, old_pdt);
1144 ret = drm_dp_port_setup_pdt(port);
1145 if (ret == true)
1146 drm_dp_send_link_address(mstb->mgr, port->mstb);
1149 if (created && !port->input) {
1150 char proppath[255];
1152 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
1153 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1154 if (!port->connector) {
1155 /* remove it from the port list */
1156 mutex_lock(&mstb->mgr->lock);
1157 list_del(&port->next);
1158 mutex_unlock(&mstb->mgr->lock);
1159 /* drop port list reference */
1160 drm_dp_put_port(port);
1161 goto out;
1163 if (port->port_num >= DP_MST_LOGICAL_PORT_0) {
1164 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1165 drm_mode_connector_set_tile_property(port->connector);
1167 (*mstb->mgr->cbs->register_connector)(port->connector);
1170 out:
1171 /* put reference to this port */
1172 drm_dp_put_port(port);
1175 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1176 struct drm_dp_connection_status_notify *conn_stat)
1178 struct drm_dp_mst_port *port;
1179 int old_pdt;
1180 int old_ddps;
1181 bool dowork = false;
1182 port = drm_dp_get_port(mstb, conn_stat->port_number);
1183 if (!port)
1184 return;
1186 old_ddps = port->ddps;
1187 old_pdt = port->pdt;
1188 port->pdt = conn_stat->peer_device_type;
1189 port->mcs = conn_stat->message_capability_status;
1190 port->ldps = conn_stat->legacy_device_plug_status;
1191 port->ddps = conn_stat->displayport_device_plug_status;
1193 if (old_ddps != port->ddps) {
1194 if (port->ddps) {
1195 dowork = true;
1196 } else {
1197 port->available_pbn = 0;
1200 if (old_pdt != port->pdt && !port->input) {
1201 drm_dp_port_teardown_pdt(port, old_pdt);
1203 if (drm_dp_port_setup_pdt(port))
1204 dowork = true;
1207 drm_dp_put_port(port);
1208 if (dowork)
1209 queue_work(system_long_wq, &mstb->mgr->work);
1213 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1214 u8 lct, u8 *rad)
1216 struct drm_dp_mst_branch *mstb;
1217 struct drm_dp_mst_port *port;
1218 int i;
1219 /* find the port by iterating down */
1221 mutex_lock(&mgr->lock);
1222 mstb = mgr->mst_primary;
1224 for (i = 0; i < lct - 1; i++) {
1225 int shift = (i % 2) ? 0 : 4;
1226 int port_num = (rad[i / 2] >> shift) & 0xf;
1228 list_for_each_entry(port, &mstb->ports, next) {
1229 if (port->port_num == port_num) {
1230 mstb = port->mstb;
1231 if (!mstb) {
1232 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1233 goto out;
1236 break;
1240 kref_get(&mstb->kref);
1241 out:
1242 mutex_unlock(&mgr->lock);
1243 return mstb;
1246 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1247 struct drm_dp_mst_branch *mstb,
1248 uint8_t *guid)
1250 struct drm_dp_mst_branch *found_mstb;
1251 struct drm_dp_mst_port *port;
1253 if (memcmp(mstb->guid, guid, 16) == 0)
1254 return mstb;
1257 list_for_each_entry(port, &mstb->ports, next) {
1258 if (!port->mstb)
1259 continue;
1261 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1263 if (found_mstb)
1264 return found_mstb;
1267 return NULL;
1270 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
1271 struct drm_dp_mst_topology_mgr *mgr,
1272 uint8_t *guid)
1274 struct drm_dp_mst_branch *mstb;
1276 /* find the port by iterating down */
1277 mutex_lock(&mgr->lock);
1279 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1281 if (mstb)
1282 kref_get(&mstb->kref);
1284 mutex_unlock(&mgr->lock);
1285 return mstb;
1288 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1289 struct drm_dp_mst_branch *mstb)
1291 struct drm_dp_mst_port *port;
1292 struct drm_dp_mst_branch *mstb_child;
1293 if (!mstb->link_address_sent)
1294 drm_dp_send_link_address(mgr, mstb);
1296 list_for_each_entry(port, &mstb->ports, next) {
1297 if (port->input)
1298 continue;
1300 if (!port->ddps)
1301 continue;
1303 if (!port->available_pbn)
1304 drm_dp_send_enum_path_resources(mgr, mstb, port);
1306 if (port->mstb) {
1307 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1308 if (mstb_child) {
1309 drm_dp_check_and_send_link_address(mgr, mstb_child);
1310 drm_dp_put_mst_branch_device(mstb_child);
1316 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1318 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1319 struct drm_dp_mst_branch *mstb;
1321 mutex_lock(&mgr->lock);
1322 mstb = mgr->mst_primary;
1323 if (mstb) {
1324 kref_get(&mstb->kref);
1326 mutex_unlock(&mgr->lock);
1327 if (mstb) {
1328 drm_dp_check_and_send_link_address(mgr, mstb);
1329 drm_dp_put_mst_branch_device(mstb);
1333 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1334 u8 *guid)
1336 static u8 zero_guid[16];
1338 if (!memcmp(guid, zero_guid, 16)) {
1339 u64 salt = get_jiffies_64();
1340 memcpy(&guid[0], &salt, sizeof(u64));
1341 memcpy(&guid[8], &salt, sizeof(u64));
1342 return false;
1344 return true;
1347 #if 0
1348 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1350 struct drm_dp_sideband_msg_req_body req;
1352 req.req_type = DP_REMOTE_DPCD_READ;
1353 req.u.dpcd_read.port_number = port_num;
1354 req.u.dpcd_read.dpcd_address = offset;
1355 req.u.dpcd_read.num_bytes = num_bytes;
1356 drm_dp_encode_sideband_req(&req, msg);
1358 return 0;
1360 #endif
1362 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1363 bool up, u8 *msg, int len)
1365 int ret;
1366 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1367 int tosend, total, offset;
1368 int retries = 0;
1370 retry:
1371 total = len;
1372 offset = 0;
1373 do {
1374 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1376 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1377 &msg[offset],
1378 tosend);
1379 if (ret != tosend) {
1380 if (ret == -EIO && retries < 5) {
1381 retries++;
1382 goto retry;
1384 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1386 return -EIO;
1388 offset += tosend;
1389 total -= tosend;
1390 } while (total > 0);
1391 return 0;
1394 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1395 struct drm_dp_sideband_msg_tx *txmsg)
1397 struct drm_dp_mst_branch *mstb = txmsg->dst;
1398 u8 req_type;
1400 /* both msg slots are full */
1401 if (txmsg->seqno == -1) {
1402 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1403 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1404 return -EAGAIN;
1406 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1407 txmsg->seqno = mstb->last_seqno;
1408 mstb->last_seqno ^= 1;
1409 } else if (mstb->tx_slots[0] == NULL)
1410 txmsg->seqno = 0;
1411 else
1412 txmsg->seqno = 1;
1413 mstb->tx_slots[txmsg->seqno] = txmsg;
1416 req_type = txmsg->msg[0] & 0x7f;
1417 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1418 req_type == DP_RESOURCE_STATUS_NOTIFY)
1419 hdr->broadcast = 1;
1420 else
1421 hdr->broadcast = 0;
1422 hdr->path_msg = txmsg->path_msg;
1423 hdr->lct = mstb->lct;
1424 hdr->lcr = mstb->lct - 1;
1425 if (mstb->lct > 1)
1426 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1427 hdr->seqno = txmsg->seqno;
1428 return 0;
1431 * process a single block of the next message in the sideband queue
1433 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1434 struct drm_dp_sideband_msg_tx *txmsg,
1435 bool up)
1437 u8 chunk[48];
1438 struct drm_dp_sideband_msg_hdr hdr;
1439 int len, space, idx, tosend;
1440 int ret;
1442 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1444 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1445 txmsg->seqno = -1;
1446 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1449 /* make hdr from dst mst - for replies use seqno
1450 otherwise assign one */
1451 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1452 if (ret < 0)
1453 return ret;
1455 /* amount left to send in this message */
1456 len = txmsg->cur_len - txmsg->cur_offset;
1458 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1459 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1461 tosend = min(len, space);
1462 if (len == txmsg->cur_len)
1463 hdr.somt = 1;
1464 if (space >= len)
1465 hdr.eomt = 1;
1468 hdr.msg_len = tosend + 1;
1469 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1470 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1471 /* add crc at end */
1472 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1473 idx += tosend + 1;
1475 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1476 if (ret) {
1477 DRM_DEBUG_KMS("sideband msg failed to send\n");
1478 return ret;
1481 txmsg->cur_offset += tosend;
1482 if (txmsg->cur_offset == txmsg->cur_len) {
1483 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1484 return 1;
1486 return 0;
1489 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1491 struct drm_dp_sideband_msg_tx *txmsg;
1492 int ret;
1494 WARN_ON(!mutex_is_locked(&mgr->qlock));
1496 /* construct a chunk from the first msg in the tx_msg queue */
1497 if (list_empty(&mgr->tx_msg_downq)) {
1498 mgr->tx_down_in_progress = false;
1499 return;
1501 mgr->tx_down_in_progress = true;
1503 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1504 ret = process_single_tx_qlock(mgr, txmsg, false);
1505 if (ret == 1) {
1506 /* txmsg is sent it should be in the slots now */
1507 list_del(&txmsg->next);
1508 } else if (ret) {
1509 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1510 list_del(&txmsg->next);
1511 if (txmsg->seqno != -1)
1512 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1513 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1514 wake_up(&mgr->tx_waitq);
1516 if (list_empty(&mgr->tx_msg_downq)) {
1517 mgr->tx_down_in_progress = false;
1518 return;
1522 /* called holding qlock */
1523 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1524 struct drm_dp_sideband_msg_tx *txmsg)
1526 int ret;
1528 /* construct a chunk from the first msg in the tx_msg queue */
1529 ret = process_single_tx_qlock(mgr, txmsg, true);
1531 if (ret != 1)
1532 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1534 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1537 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1538 struct drm_dp_sideband_msg_tx *txmsg)
1540 mutex_lock(&mgr->qlock);
1541 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1542 if (!mgr->tx_down_in_progress)
1543 process_single_down_tx_qlock(mgr);
1544 mutex_unlock(&mgr->qlock);
1547 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1548 struct drm_dp_mst_branch *mstb)
1550 int len;
1551 struct drm_dp_sideband_msg_tx *txmsg;
1552 int ret;
1554 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1555 if (!txmsg)
1556 return;
1558 txmsg->dst = mstb;
1559 len = build_link_address(txmsg);
1561 mstb->link_address_sent = true;
1562 drm_dp_queue_down_tx(mgr, txmsg);
1564 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1565 if (ret > 0) {
1566 int i;
1568 if (txmsg->reply.reply_type == 1)
1569 DRM_DEBUG_KMS("link address nak received\n");
1570 else {
1571 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1572 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1573 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,
1574 txmsg->reply.u.link_addr.ports[i].input_port,
1575 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1576 txmsg->reply.u.link_addr.ports[i].port_number,
1577 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1578 txmsg->reply.u.link_addr.ports[i].mcs,
1579 txmsg->reply.u.link_addr.ports[i].ddps,
1580 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1581 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1582 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1585 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1587 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1588 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1590 (*mgr->cbs->hotplug)(mgr);
1592 } else {
1593 mstb->link_address_sent = false;
1594 DRM_DEBUG_KMS("link address failed %d\n", ret);
1597 kfree(txmsg);
1600 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1601 struct drm_dp_mst_branch *mstb,
1602 struct drm_dp_mst_port *port)
1604 int len;
1605 struct drm_dp_sideband_msg_tx *txmsg;
1606 int ret;
1608 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1609 if (!txmsg)
1610 return -ENOMEM;
1612 txmsg->dst = mstb;
1613 len = build_enum_path_resources(txmsg, port->port_num);
1615 drm_dp_queue_down_tx(mgr, txmsg);
1617 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1618 if (ret > 0) {
1619 if (txmsg->reply.reply_type == 1)
1620 DRM_DEBUG_KMS("enum path resources nak received\n");
1621 else {
1622 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1623 DRM_ERROR("got incorrect port in response\n");
1624 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,
1625 txmsg->reply.u.path_resources.avail_payload_bw_number);
1626 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1630 kfree(txmsg);
1631 return 0;
1634 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
1636 if (!mstb->port_parent)
1637 return NULL;
1639 if (mstb->port_parent->mstb != mstb)
1640 return mstb->port_parent;
1642 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
1645 static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
1646 struct drm_dp_mst_branch *mstb,
1647 int *port_num)
1649 struct drm_dp_mst_branch *rmstb = NULL;
1650 struct drm_dp_mst_port *found_port;
1651 mutex_lock(&mgr->lock);
1652 if (mgr->mst_primary) {
1653 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
1655 if (found_port) {
1656 rmstb = found_port->parent;
1657 kref_get(&rmstb->kref);
1658 *port_num = found_port->port_num;
1661 mutex_unlock(&mgr->lock);
1662 return rmstb;
1665 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1666 struct drm_dp_mst_port *port,
1667 int id,
1668 int pbn)
1670 struct drm_dp_sideband_msg_tx *txmsg;
1671 struct drm_dp_mst_branch *mstb;
1672 int len, ret, port_num;
1673 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1674 int i;
1676 port = drm_dp_get_validated_port_ref(mgr, port);
1677 if (!port)
1678 return -EINVAL;
1680 port_num = port->port_num;
1681 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1682 if (!mstb) {
1683 mstb = drm_dp_get_last_connected_port_and_mstb(mgr, port->parent, &port_num);
1685 if (!mstb) {
1686 drm_dp_put_port(port);
1687 return -EINVAL;
1691 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1692 if (!txmsg) {
1693 ret = -ENOMEM;
1694 goto fail_put;
1697 for (i = 0; i < port->num_sdp_streams; i++)
1698 sinks[i] = i;
1700 txmsg->dst = mstb;
1701 len = build_allocate_payload(txmsg, port_num,
1703 pbn, port->num_sdp_streams, sinks);
1705 drm_dp_queue_down_tx(mgr, txmsg);
1707 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1708 if (ret > 0) {
1709 if (txmsg->reply.reply_type == 1) {
1710 ret = -EINVAL;
1711 } else
1712 ret = 0;
1714 kfree(txmsg);
1715 fail_put:
1716 drm_dp_put_mst_branch_device(mstb);
1717 drm_dp_put_port(port);
1718 return ret;
1721 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1722 int id,
1723 struct drm_dp_payload *payload)
1725 int ret;
1727 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1728 if (ret < 0) {
1729 payload->payload_state = 0;
1730 return ret;
1732 payload->payload_state = DP_PAYLOAD_LOCAL;
1733 return 0;
1736 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1737 struct drm_dp_mst_port *port,
1738 int id,
1739 struct drm_dp_payload *payload)
1741 int ret;
1742 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1743 if (ret < 0)
1744 return ret;
1745 payload->payload_state = DP_PAYLOAD_REMOTE;
1746 return ret;
1749 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1750 struct drm_dp_mst_port *port,
1751 int id,
1752 struct drm_dp_payload *payload)
1754 DRM_DEBUG_KMS("\n");
1755 /* its okay for these to fail */
1756 if (port) {
1757 drm_dp_payload_send_msg(mgr, port, id, 0);
1760 drm_dp_dpcd_write_payload(mgr, id, payload);
1761 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
1762 return 0;
1765 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1766 int id,
1767 struct drm_dp_payload *payload)
1769 payload->payload_state = 0;
1770 return 0;
1774 * drm_dp_update_payload_part1() - Execute payload update part 1
1775 * @mgr: manager to use.
1777 * This iterates over all proposed virtual channels, and tries to
1778 * allocate space in the link for them. For 0->slots transitions,
1779 * this step just writes the VCPI to the MST device. For slots->0
1780 * transitions, this writes the updated VCPIs and removes the
1781 * remote VC payloads.
1783 * after calling this the driver should generate ACT and payload
1784 * packets.
1786 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1788 int i, j;
1789 int cur_slots = 1;
1790 struct drm_dp_payload req_payload;
1791 struct drm_dp_mst_port *port;
1793 mutex_lock(&mgr->payload_lock);
1794 for (i = 0; i < mgr->max_payloads; i++) {
1795 /* solve the current payloads - compare to the hw ones
1796 - update the hw view */
1797 req_payload.start_slot = cur_slots;
1798 if (mgr->proposed_vcpis[i]) {
1799 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1800 port = drm_dp_get_validated_port_ref(mgr, port);
1801 if (!port) {
1802 mutex_unlock(&mgr->payload_lock);
1803 return -EINVAL;
1805 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1806 req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
1807 } else {
1808 port = NULL;
1809 req_payload.num_slots = 0;
1812 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1813 mgr->payloads[i].start_slot = req_payload.start_slot;
1815 /* work out what is required to happen with this payload */
1816 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
1818 /* need to push an update for this payload */
1819 if (req_payload.num_slots) {
1820 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
1821 mgr->payloads[i].num_slots = req_payload.num_slots;
1822 mgr->payloads[i].vcpi = req_payload.vcpi;
1823 } else if (mgr->payloads[i].num_slots) {
1824 mgr->payloads[i].num_slots = 0;
1825 drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]);
1826 req_payload.payload_state = mgr->payloads[i].payload_state;
1827 mgr->payloads[i].start_slot = 0;
1829 mgr->payloads[i].payload_state = req_payload.payload_state;
1831 cur_slots += req_payload.num_slots;
1833 if (port)
1834 drm_dp_put_port(port);
1837 for (i = 0; i < mgr->max_payloads; i++) {
1838 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1839 DRM_DEBUG_KMS("removing payload %d\n", i);
1840 for (j = i; j < mgr->max_payloads - 1; j++) {
1841 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1842 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1843 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1844 set_bit(j + 1, &mgr->payload_mask);
1845 } else {
1846 clear_bit(j + 1, &mgr->payload_mask);
1849 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1850 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1851 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1855 mutex_unlock(&mgr->payload_lock);
1857 return 0;
1859 EXPORT_SYMBOL(drm_dp_update_payload_part1);
1862 * drm_dp_update_payload_part2() - Execute payload update part 2
1863 * @mgr: manager to use.
1865 * This iterates over all proposed virtual channels, and tries to
1866 * allocate space in the link for them. For 0->slots transitions,
1867 * this step writes the remote VC payload commands. For slots->0
1868 * this just resets some internal state.
1870 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1872 struct drm_dp_mst_port *port;
1873 int i;
1874 int ret = 0;
1875 mutex_lock(&mgr->payload_lock);
1876 for (i = 0; i < mgr->max_payloads; i++) {
1878 if (!mgr->proposed_vcpis[i])
1879 continue;
1881 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1883 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1884 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1885 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1886 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1887 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1889 if (ret) {
1890 mutex_unlock(&mgr->payload_lock);
1891 return ret;
1894 mutex_unlock(&mgr->payload_lock);
1895 return 0;
1897 EXPORT_SYMBOL(drm_dp_update_payload_part2);
1899 #if 0 /* unused as of yet */
1900 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1901 struct drm_dp_mst_port *port,
1902 int offset, int size)
1904 int len;
1905 struct drm_dp_sideband_msg_tx *txmsg;
1907 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1908 if (!txmsg)
1909 return -ENOMEM;
1911 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1912 txmsg->dst = port->parent;
1914 drm_dp_queue_down_tx(mgr, txmsg);
1916 return 0;
1918 #endif
1920 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1921 struct drm_dp_mst_port *port,
1922 int offset, int size, u8 *bytes)
1924 int len;
1925 int ret;
1926 struct drm_dp_sideband_msg_tx *txmsg;
1927 struct drm_dp_mst_branch *mstb;
1929 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1930 if (!mstb)
1931 return -EINVAL;
1933 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1934 if (!txmsg) {
1935 ret = -ENOMEM;
1936 goto fail_put;
1939 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1940 txmsg->dst = mstb;
1942 drm_dp_queue_down_tx(mgr, txmsg);
1944 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1945 if (ret > 0) {
1946 if (txmsg->reply.reply_type == 1) {
1947 ret = -EINVAL;
1948 } else
1949 ret = 0;
1951 kfree(txmsg);
1952 fail_put:
1953 drm_dp_put_mst_branch_device(mstb);
1954 return ret;
1957 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1959 struct drm_dp_sideband_msg_reply_body reply;
1961 reply.reply_type = 0;
1962 reply.req_type = req_type;
1963 drm_dp_encode_sideband_reply(&reply, msg);
1964 return 0;
1967 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1968 struct drm_dp_mst_branch *mstb,
1969 int req_type, int seqno, bool broadcast)
1971 struct drm_dp_sideband_msg_tx *txmsg;
1973 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1974 if (!txmsg)
1975 return -ENOMEM;
1977 txmsg->dst = mstb;
1978 txmsg->seqno = seqno;
1979 drm_dp_encode_up_ack_reply(txmsg, req_type);
1981 mutex_lock(&mgr->qlock);
1983 process_single_up_tx_qlock(mgr, txmsg);
1985 mutex_unlock(&mgr->qlock);
1987 kfree(txmsg);
1988 return 0;
1991 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1992 int dp_link_count,
1993 int *out)
1995 switch (dp_link_bw) {
1996 default:
1997 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1998 dp_link_bw, dp_link_count);
1999 return false;
2001 case DP_LINK_BW_1_62:
2002 *out = 3 * dp_link_count;
2003 break;
2004 case DP_LINK_BW_2_7:
2005 *out = 5 * dp_link_count;
2006 break;
2007 case DP_LINK_BW_5_4:
2008 *out = 10 * dp_link_count;
2009 break;
2011 return true;
2015 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2016 * @mgr: manager to set state for
2017 * @mst_state: true to enable MST on this connector - false to disable.
2019 * This is called by the driver when it detects an MST capable device plugged
2020 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2022 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2024 int ret = 0;
2025 struct drm_dp_mst_branch *mstb = NULL;
2027 mutex_lock(&mgr->lock);
2028 if (mst_state == mgr->mst_state)
2029 goto out_unlock;
2031 mgr->mst_state = mst_state;
2032 /* set the device into MST mode */
2033 if (mst_state) {
2034 WARN_ON(mgr->mst_primary);
2036 /* get dpcd info */
2037 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2038 if (ret != DP_RECEIVER_CAP_SIZE) {
2039 DRM_DEBUG_KMS("failed to read DPCD\n");
2040 goto out_unlock;
2043 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2044 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2045 &mgr->pbn_div)) {
2046 ret = -EINVAL;
2047 goto out_unlock;
2050 mgr->total_pbn = 2560;
2051 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
2052 mgr->avail_slots = mgr->total_slots;
2054 /* add initial branch device at LCT 1 */
2055 mstb = drm_dp_add_mst_branch_device(1, NULL);
2056 if (mstb == NULL) {
2057 ret = -ENOMEM;
2058 goto out_unlock;
2060 mstb->mgr = mgr;
2062 /* give this the main reference */
2063 mgr->mst_primary = mstb;
2064 kref_get(&mgr->mst_primary->kref);
2066 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2067 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2068 if (ret < 0) {
2069 goto out_unlock;
2073 struct drm_dp_payload reset_pay;
2074 reset_pay.start_slot = 0;
2075 reset_pay.num_slots = 0x3f;
2076 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2079 queue_work(system_long_wq, &mgr->work);
2081 ret = 0;
2082 } else {
2083 /* disable MST on the device */
2084 mstb = mgr->mst_primary;
2085 mgr->mst_primary = NULL;
2086 /* this can fail if the device is gone */
2087 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2088 ret = 0;
2089 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2090 mgr->payload_mask = 0;
2091 set_bit(0, &mgr->payload_mask);
2092 mgr->vcpi_mask = 0;
2095 out_unlock:
2096 mutex_unlock(&mgr->lock);
2097 if (mstb)
2098 drm_dp_put_mst_branch_device(mstb);
2099 return ret;
2102 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2105 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2106 * @mgr: manager to suspend
2108 * This function tells the MST device that we can't handle UP messages
2109 * anymore. This should stop it from sending any since we are suspended.
2111 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2113 mutex_lock(&mgr->lock);
2114 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2115 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2116 mutex_unlock(&mgr->lock);
2117 flush_work(&mgr->work);
2118 flush_work(&mgr->destroy_connector_work);
2120 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2123 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2124 * @mgr: manager to resume
2126 * This will fetch DPCD and see if the device is still there,
2127 * if it is, it will rewrite the MSTM control bits, and return.
2129 * if the device fails this returns -1, and the driver should do
2130 * a full MST reprobe, in case we were undocked.
2132 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2134 int ret = 0;
2136 mutex_lock(&mgr->lock);
2138 if (mgr->mst_primary) {
2139 int sret;
2140 u8 guid[16];
2142 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2143 if (sret != DP_RECEIVER_CAP_SIZE) {
2144 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2145 ret = -1;
2146 goto out_unlock;
2149 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2150 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2151 if (ret < 0) {
2152 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2153 ret = -1;
2154 goto out_unlock;
2157 /* Some hubs forget their guids after they resume */
2158 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2159 if (sret != 16) {
2160 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2161 ret = -1;
2162 goto out_unlock;
2164 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2166 ret = 0;
2167 } else
2168 ret = -1;
2170 out_unlock:
2171 mutex_unlock(&mgr->lock);
2172 return ret;
2174 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2176 static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2178 int len;
2179 u8 replyblock[32];
2180 int replylen, origlen, curreply;
2181 int ret;
2182 struct drm_dp_sideband_msg_rx *msg;
2183 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2184 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2186 len = min(mgr->max_dpcd_transaction_bytes, 16);
2187 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2188 replyblock, len);
2189 if (ret != len) {
2190 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2191 return;
2193 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2194 if (!ret) {
2195 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2196 return;
2198 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2200 origlen = replylen;
2201 replylen -= len;
2202 curreply = len;
2203 while (replylen > 0) {
2204 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2205 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2206 replyblock, len);
2207 if (ret != len) {
2208 DRM_DEBUG_KMS("failed to read a chunk\n");
2210 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2211 if (ret == false)
2212 DRM_DEBUG_KMS("failed to build sideband msg\n");
2213 curreply += len;
2214 replylen -= len;
2218 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2220 int ret = 0;
2222 drm_dp_get_one_sb_msg(mgr, false);
2224 if (mgr->down_rep_recv.have_eomt) {
2225 struct drm_dp_sideband_msg_tx *txmsg;
2226 struct drm_dp_mst_branch *mstb;
2227 int slot = -1;
2228 mstb = drm_dp_get_mst_branch_device(mgr,
2229 mgr->down_rep_recv.initial_hdr.lct,
2230 mgr->down_rep_recv.initial_hdr.rad);
2232 if (!mstb) {
2233 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2234 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2235 return 0;
2238 /* find the message */
2239 slot = mgr->down_rep_recv.initial_hdr.seqno;
2240 mutex_lock(&mgr->qlock);
2241 txmsg = mstb->tx_slots[slot];
2242 /* remove from slots */
2243 mutex_unlock(&mgr->qlock);
2245 if (!txmsg) {
2246 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2247 mstb,
2248 mgr->down_rep_recv.initial_hdr.seqno,
2249 mgr->down_rep_recv.initial_hdr.lct,
2250 mgr->down_rep_recv.initial_hdr.rad[0],
2251 mgr->down_rep_recv.msg[0]);
2252 drm_dp_put_mst_branch_device(mstb);
2253 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2254 return 0;
2257 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2258 if (txmsg->reply.reply_type == 1) {
2259 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);
2262 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2263 drm_dp_put_mst_branch_device(mstb);
2265 mutex_lock(&mgr->qlock);
2266 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2267 mstb->tx_slots[slot] = NULL;
2268 mutex_unlock(&mgr->qlock);
2270 wake_up(&mgr->tx_waitq);
2272 return ret;
2275 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2277 int ret = 0;
2278 drm_dp_get_one_sb_msg(mgr, true);
2280 if (mgr->up_req_recv.have_eomt) {
2281 struct drm_dp_sideband_msg_req_body msg;
2282 struct drm_dp_mst_branch *mstb = NULL;
2283 bool seqno;
2285 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2286 mstb = drm_dp_get_mst_branch_device(mgr,
2287 mgr->up_req_recv.initial_hdr.lct,
2288 mgr->up_req_recv.initial_hdr.rad);
2289 if (!mstb) {
2290 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2291 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2292 return 0;
2296 seqno = mgr->up_req_recv.initial_hdr.seqno;
2297 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2299 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2300 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2302 if (!mstb)
2303 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2305 if (!mstb) {
2306 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2307 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2308 return 0;
2311 drm_dp_update_port(mstb, &msg.u.conn_stat);
2313 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);
2314 (*mgr->cbs->hotplug)(mgr);
2316 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2317 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2318 if (!mstb)
2319 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2321 if (!mstb) {
2322 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2323 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2324 return 0;
2327 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2330 drm_dp_put_mst_branch_device(mstb);
2331 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2333 return ret;
2337 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2338 * @mgr: manager to notify irq for.
2339 * @esi: 4 bytes from SINK_COUNT_ESI
2340 * @handled: whether the hpd interrupt was consumed or not
2342 * This should be called from the driver when it detects a short IRQ,
2343 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2344 * topology manager will process the sideband messages received as a result
2345 * of this.
2347 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2349 int ret = 0;
2350 int sc;
2351 *handled = false;
2352 sc = esi[0] & 0x3f;
2354 if (sc != mgr->sink_count) {
2355 mgr->sink_count = sc;
2356 *handled = true;
2359 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2360 ret = drm_dp_mst_handle_down_rep(mgr);
2361 *handled = true;
2364 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2365 ret |= drm_dp_mst_handle_up_req(mgr);
2366 *handled = true;
2369 drm_dp_mst_kick_tx(mgr);
2370 return ret;
2372 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2375 * drm_dp_mst_detect_port() - get connection status for an MST port
2376 * @mgr: manager for this port
2377 * @port: unverified pointer to a port
2379 * This returns the current connection state for a port. It validates the
2380 * port pointer still exists so the caller doesn't require a reference
2382 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2383 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2385 enum drm_connector_status status = connector_status_disconnected;
2387 /* we need to search for the port in the mgr in case its gone */
2388 port = drm_dp_get_validated_port_ref(mgr, port);
2389 if (!port)
2390 return connector_status_disconnected;
2392 if (!port->ddps)
2393 goto out;
2395 switch (port->pdt) {
2396 case DP_PEER_DEVICE_NONE:
2397 case DP_PEER_DEVICE_MST_BRANCHING:
2398 break;
2400 case DP_PEER_DEVICE_SST_SINK:
2401 status = connector_status_connected;
2402 /* for logical ports - cache the EDID */
2403 if (port->port_num >= 8 && !port->cached_edid) {
2404 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2406 break;
2407 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2408 if (port->ldps)
2409 status = connector_status_connected;
2410 break;
2412 out:
2413 drm_dp_put_port(port);
2414 return status;
2416 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2419 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2420 * @mgr: manager for this port
2421 * @port: unverified pointer to a port.
2423 * This returns whether the port supports audio or not.
2425 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2426 struct drm_dp_mst_port *port)
2428 bool ret = false;
2430 port = drm_dp_get_validated_port_ref(mgr, port);
2431 if (!port)
2432 return ret;
2433 ret = port->has_audio;
2434 drm_dp_put_port(port);
2435 return ret;
2437 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2440 * drm_dp_mst_get_edid() - get EDID for an MST port
2441 * @connector: toplevel connector to get EDID for
2442 * @mgr: manager for this port
2443 * @port: unverified pointer to a port.
2445 * This returns an EDID for the port connected to a connector,
2446 * It validates the pointer still exists so the caller doesn't require a
2447 * reference.
2449 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2451 struct edid *edid = NULL;
2453 /* we need to search for the port in the mgr in case its gone */
2454 port = drm_dp_get_validated_port_ref(mgr, port);
2455 if (!port)
2456 return NULL;
2458 if (port->cached_edid)
2459 edid = drm_edid_duplicate(port->cached_edid);
2460 else {
2461 edid = drm_get_edid(connector, &port->aux.ddc);
2462 drm_mode_connector_set_tile_property(connector);
2464 port->has_audio = drm_detect_monitor_audio(edid);
2465 drm_dp_put_port(port);
2466 return edid;
2468 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2471 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2472 * @mgr: manager to use
2473 * @pbn: payload bandwidth to convert into slots.
2475 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2476 int pbn)
2478 int num_slots;
2480 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2482 if (num_slots > mgr->avail_slots)
2483 return -ENOSPC;
2484 return num_slots;
2486 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2488 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2489 struct drm_dp_vcpi *vcpi, int pbn)
2491 int num_slots;
2492 int ret;
2494 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2496 if (num_slots > mgr->avail_slots)
2497 return -ENOSPC;
2499 vcpi->pbn = pbn;
2500 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2501 vcpi->num_slots = num_slots;
2503 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2504 if (ret < 0)
2505 return ret;
2506 return 0;
2510 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2511 * @mgr: manager for this port
2512 * @port: port to allocate a virtual channel for.
2513 * @pbn: payload bandwidth number to request
2514 * @slots: returned number of slots for this PBN.
2516 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2518 int ret;
2520 port = drm_dp_get_validated_port_ref(mgr, port);
2521 if (!port)
2522 return false;
2524 if (port->vcpi.vcpi > 0) {
2525 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2526 if (pbn == port->vcpi.pbn) {
2527 *slots = port->vcpi.num_slots;
2528 drm_dp_put_port(port);
2529 return true;
2533 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2534 if (ret) {
2535 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2536 goto out;
2538 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2539 *slots = port->vcpi.num_slots;
2541 drm_dp_put_port(port);
2542 return true;
2543 out:
2544 return false;
2546 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2548 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2550 int slots = 0;
2551 port = drm_dp_get_validated_port_ref(mgr, port);
2552 if (!port)
2553 return slots;
2555 slots = port->vcpi.num_slots;
2556 drm_dp_put_port(port);
2557 return slots;
2559 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2562 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2563 * @mgr: manager for this port
2564 * @port: unverified pointer to a port.
2566 * This just resets the number of slots for the ports VCPI for later programming.
2568 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2570 port = drm_dp_get_validated_port_ref(mgr, port);
2571 if (!port)
2572 return;
2573 port->vcpi.num_slots = 0;
2574 drm_dp_put_port(port);
2576 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2579 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2580 * @mgr: manager for this port
2581 * @port: unverified port to deallocate vcpi for
2583 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2585 port = drm_dp_get_validated_port_ref(mgr, port);
2586 if (!port)
2587 return;
2589 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2590 port->vcpi.num_slots = 0;
2591 port->vcpi.pbn = 0;
2592 port->vcpi.aligned_pbn = 0;
2593 port->vcpi.vcpi = 0;
2594 drm_dp_put_port(port);
2596 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2598 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2599 int id, struct drm_dp_payload *payload)
2601 u8 payload_alloc[3], status;
2602 int ret;
2603 int retries = 0;
2605 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2606 DP_PAYLOAD_TABLE_UPDATED);
2608 payload_alloc[0] = id;
2609 payload_alloc[1] = payload->start_slot;
2610 payload_alloc[2] = payload->num_slots;
2612 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2613 if (ret != 3) {
2614 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2615 goto fail;
2618 retry:
2619 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2620 if (ret < 0) {
2621 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2622 goto fail;
2625 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2626 retries++;
2627 if (retries < 20) {
2628 usleep_range(10000, 20000);
2629 goto retry;
2631 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2632 ret = -EINVAL;
2633 goto fail;
2635 ret = 0;
2636 fail:
2637 return ret;
2642 * drm_dp_check_act_status() - Check ACT handled status.
2643 * @mgr: manager to use
2645 * Check the payload status bits in the DPCD for ACT handled completion.
2647 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2649 u8 status;
2650 int ret;
2651 int count = 0;
2653 do {
2654 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2656 if (ret < 0) {
2657 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2658 goto fail;
2661 if (status & DP_PAYLOAD_ACT_HANDLED)
2662 break;
2663 count++;
2664 udelay(100);
2666 } while (count < 30);
2668 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2669 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2670 ret = -EINVAL;
2671 goto fail;
2673 return 0;
2674 fail:
2675 return ret;
2677 EXPORT_SYMBOL(drm_dp_check_act_status);
2680 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2681 * @clock: dot clock for the mode
2682 * @bpp: bpp for the mode.
2684 * This uses the formula in the spec to calculate the PBN value for a mode.
2686 int drm_dp_calc_pbn_mode(int clock, int bpp)
2688 u64 kbps;
2689 s64 peak_kbps;
2690 u32 numerator;
2691 u32 denominator;
2693 kbps = clock * bpp;
2696 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2697 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2698 * common multiplier to render an integer PBN for all link rate/lane
2699 * counts combinations
2700 * calculate
2701 * peak_kbps *= (1006/1000)
2702 * peak_kbps *= (64/54)
2703 * peak_kbps *= 8 convert to bytes
2706 numerator = 64 * 1006;
2707 denominator = 54 * 8 * 1000 * 1000;
2709 kbps *= numerator;
2710 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
2712 return drm_fixp2int_ceil(peak_kbps);
2714 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2716 static int test_calc_pbn_mode(void)
2718 int ret;
2719 ret = drm_dp_calc_pbn_mode(154000, 30);
2720 if (ret != 689) {
2721 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2722 154000, 30, 689, ret);
2723 return -EINVAL;
2725 ret = drm_dp_calc_pbn_mode(234000, 30);
2726 if (ret != 1047) {
2727 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2728 234000, 30, 1047, ret);
2729 return -EINVAL;
2731 ret = drm_dp_calc_pbn_mode(297000, 24);
2732 if (ret != 1063) {
2733 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2734 297000, 24, 1063, ret);
2735 return -EINVAL;
2737 return 0;
2740 /* we want to kick the TX after we've ack the up/down IRQs. */
2741 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2743 queue_work(system_long_wq, &mgr->tx_work);
2746 static void drm_dp_mst_dump_mstb(struct seq_file *m,
2747 struct drm_dp_mst_branch *mstb)
2749 struct drm_dp_mst_port *port;
2750 int tabs = mstb->lct;
2751 char prefix[10];
2752 int i;
2754 for (i = 0; i < tabs; i++)
2755 prefix[i] = '\t';
2756 prefix[i] = '\0';
2758 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2759 list_for_each_entry(port, &mstb->ports, next) {
2760 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);
2761 if (port->mstb)
2762 drm_dp_mst_dump_mstb(m, port->mstb);
2766 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2767 char *buf)
2769 int ret;
2770 int i;
2771 for (i = 0; i < 4; i++) {
2772 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2773 if (ret != 16)
2774 break;
2776 if (i == 4)
2777 return true;
2778 return false;
2781 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
2782 struct drm_dp_mst_port *port, char *name,
2783 int namelen)
2785 struct edid *mst_edid;
2787 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
2788 drm_edid_get_monitor_name(mst_edid, name, namelen);
2792 * drm_dp_mst_dump_topology(): dump topology to seq file.
2793 * @m: seq_file to dump output to
2794 * @mgr: manager to dump current topology for.
2796 * helper to dump MST topology to a seq file for debugfs.
2798 void drm_dp_mst_dump_topology(struct seq_file *m,
2799 struct drm_dp_mst_topology_mgr *mgr)
2801 int i;
2802 struct drm_dp_mst_port *port;
2804 mutex_lock(&mgr->lock);
2805 if (mgr->mst_primary)
2806 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2808 /* dump VCPIs */
2809 mutex_unlock(&mgr->lock);
2811 mutex_lock(&mgr->payload_lock);
2812 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
2813 mgr->max_payloads);
2815 for (i = 0; i < mgr->max_payloads; i++) {
2816 if (mgr->proposed_vcpis[i]) {
2817 char name[14];
2819 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2820 fetch_monitor_name(mgr, port, name, sizeof(name));
2821 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
2822 port->port_num, port->vcpi.vcpi,
2823 port->vcpi.num_slots,
2824 (*name != 0) ? name : "Unknown");
2825 } else
2826 seq_printf(m, "vcpi %d:unused\n", i);
2828 for (i = 0; i < mgr->max_payloads; i++) {
2829 seq_printf(m, "payload %d: %d, %d, %d\n",
2831 mgr->payloads[i].payload_state,
2832 mgr->payloads[i].start_slot,
2833 mgr->payloads[i].num_slots);
2837 mutex_unlock(&mgr->payload_lock);
2839 mutex_lock(&mgr->lock);
2840 if (mgr->mst_primary) {
2841 u8 buf[64];
2842 bool bret;
2843 int ret;
2844 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2845 seq_printf(m, "dpcd: ");
2846 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2847 seq_printf(m, "%02x ", buf[i]);
2848 seq_printf(m, "\n");
2849 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2850 seq_printf(m, "faux/mst: ");
2851 for (i = 0; i < 2; i++)
2852 seq_printf(m, "%02x ", buf[i]);
2853 seq_printf(m, "\n");
2854 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2855 seq_printf(m, "mst ctrl: ");
2856 for (i = 0; i < 1; i++)
2857 seq_printf(m, "%02x ", buf[i]);
2858 seq_printf(m, "\n");
2860 /* dump the standard OUI branch header */
2861 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2862 seq_printf(m, "branch oui: ");
2863 for (i = 0; i < 0x3; i++)
2864 seq_printf(m, "%02x", buf[i]);
2865 seq_printf(m, " devid: ");
2866 for (i = 0x3; i < 0x8 && buf[i]; i++)
2867 seq_printf(m, "%c", buf[i]);
2869 seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2870 seq_printf(m, "\n");
2871 bret = dump_dp_payload_table(mgr, buf);
2872 if (bret == true) {
2873 seq_printf(m, "payload table: ");
2874 for (i = 0; i < 63; i++)
2875 seq_printf(m, "%02x ", buf[i]);
2876 seq_printf(m, "\n");
2881 mutex_unlock(&mgr->lock);
2884 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2886 static void drm_dp_tx_work(struct work_struct *work)
2888 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2890 mutex_lock(&mgr->qlock);
2891 if (mgr->tx_down_in_progress)
2892 process_single_down_tx_qlock(mgr);
2893 mutex_unlock(&mgr->qlock);
2896 static void drm_dp_free_mst_port(struct kref *kref)
2898 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
2899 kref_put(&port->parent->kref, drm_dp_free_mst_branch_device);
2900 kfree(port);
2903 static void drm_dp_destroy_connector_work(struct work_struct *work)
2905 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
2906 struct drm_dp_mst_port *port;
2907 bool send_hotplug = false;
2909 * Not a regular list traverse as we have to drop the destroy
2910 * connector lock before destroying the connector, to avoid AB->BA
2911 * ordering between this lock and the config mutex.
2913 for (;;) {
2914 mutex_lock(&mgr->destroy_connector_lock);
2915 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2916 if (!port) {
2917 mutex_unlock(&mgr->destroy_connector_lock);
2918 break;
2920 list_del(&port->next);
2921 mutex_unlock(&mgr->destroy_connector_lock);
2923 kref_init(&port->kref);
2924 INIT_LIST_HEAD(&port->next);
2926 mgr->cbs->destroy_connector(mgr, port->connector);
2928 drm_dp_port_teardown_pdt(port, port->pdt);
2930 if (!port->input && port->vcpi.vcpi > 0) {
2931 drm_dp_mst_reset_vcpi_slots(mgr, port);
2932 drm_dp_update_payload_part1(mgr);
2933 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2936 kref_put(&port->kref, drm_dp_free_mst_port);
2937 send_hotplug = true;
2939 if (send_hotplug)
2940 (*mgr->cbs->hotplug)(mgr);
2944 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2945 * @mgr: manager struct to initialise
2946 * @dev: device providing this structure - for i2c addition.
2947 * @aux: DP helper aux channel to talk to this device
2948 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2949 * @max_payloads: maximum number of payloads this GPU can source
2950 * @conn_base_id: the connector object ID the MST device is connected to.
2952 * Return 0 for success, or negative error code on failure
2954 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2955 struct device *dev, struct drm_dp_aux *aux,
2956 int max_dpcd_transaction_bytes,
2957 int max_payloads, int conn_base_id)
2959 lockinit(&mgr->lock, "drmml", 0, LK_CANRECURSE);
2960 lockinit(&mgr->qlock, "drmmql", 0, LK_CANRECURSE);
2961 lockinit(&mgr->payload_lock, "drmmpl", 0, LK_CANRECURSE);
2962 lockinit(&mgr->destroy_connector_lock, "drmmdcl", 0, LK_CANRECURSE);
2963 INIT_LIST_HEAD(&mgr->tx_msg_downq);
2964 INIT_LIST_HEAD(&mgr->destroy_connector_list);
2965 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2966 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
2967 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
2968 init_waitqueue_head(&mgr->tx_waitq);
2969 mgr->dev = dev;
2970 mgr->aux = aux;
2971 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2972 mgr->max_payloads = max_payloads;
2973 mgr->conn_base_id = conn_base_id;
2974 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
2975 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
2976 return -EINVAL;
2977 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
2978 if (!mgr->payloads)
2979 return -ENOMEM;
2980 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
2981 if (!mgr->proposed_vcpis)
2982 return -ENOMEM;
2983 set_bit(0, &mgr->payload_mask);
2984 if (test_calc_pbn_mode() < 0)
2985 DRM_ERROR("MST PBN self-test failed\n");
2987 return 0;
2989 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
2992 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
2993 * @mgr: manager to destroy
2995 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
2997 flush_work(&mgr->work);
2998 flush_work(&mgr->destroy_connector_work);
2999 mutex_lock(&mgr->payload_lock);
3000 kfree(mgr->payloads);
3001 mgr->payloads = NULL;
3002 kfree(mgr->proposed_vcpis);
3003 mgr->proposed_vcpis = NULL;
3004 mutex_unlock(&mgr->payload_lock);
3005 mgr->dev = NULL;
3006 mgr->aux = NULL;
3008 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3010 /* I2C device */
3011 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3012 int num)
3014 struct drm_dp_aux *aux = adapter->algo_data;
3015 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3016 struct drm_dp_mst_branch *mstb;
3017 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3018 unsigned int i;
3019 bool reading = false;
3020 struct drm_dp_sideband_msg_req_body msg;
3021 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3022 int ret;
3024 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
3025 if (!mstb)
3026 return -EREMOTEIO;
3028 /* construct i2c msg */
3029 /* see if last msg is a read */
3030 if (msgs[num - 1].flags & I2C_M_RD)
3031 reading = true;
3033 if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
3034 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3035 ret = -EIO;
3036 goto out;
3039 memset(&msg, 0, sizeof(msg));
3040 msg.req_type = DP_REMOTE_I2C_READ;
3041 msg.u.i2c_read.num_transactions = num - 1;
3042 msg.u.i2c_read.port_number = port->port_num;
3043 for (i = 0; i < num - 1; i++) {
3044 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3045 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3046 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3048 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3049 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3051 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3052 if (!txmsg) {
3053 ret = -ENOMEM;
3054 goto out;
3057 txmsg->dst = mstb;
3058 drm_dp_encode_sideband_req(&msg, txmsg);
3060 drm_dp_queue_down_tx(mgr, txmsg);
3062 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3063 if (ret > 0) {
3065 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3066 ret = -EREMOTEIO;
3067 goto out;
3069 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3070 ret = -EIO;
3071 goto out;
3073 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3074 ret = num;
3076 out:
3077 kfree(txmsg);
3078 drm_dp_put_mst_branch_device(mstb);
3079 return ret;
3082 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3084 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3085 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3086 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3087 I2C_FUNC_10BIT_ADDR;
3090 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3091 .functionality = drm_dp_mst_i2c_functionality,
3092 .master_xfer = drm_dp_mst_i2c_xfer,
3096 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3097 * @aux: DisplayPort AUX channel
3099 * Returns 0 on success or a negative error code on failure.
3101 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3103 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3104 aux->ddc.algo_data = aux;
3105 aux->ddc.retries = 3;
3107 #if 0
3108 aux->ddc.class = I2C_CLASS_DDC;
3109 aux->ddc.owner = THIS_MODULE;
3110 #endif
3111 aux->ddc.dev.parent = aux->dev;
3112 #if 0
3113 aux->ddc.dev.of_node = aux->dev->of_node;
3114 #endif
3116 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3117 sizeof(aux->ddc.name));
3119 return i2c_add_adapter(&aux->ddc);
3123 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3124 * @aux: DisplayPort AUX channel
3126 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3128 i2c_del_adapter(&aux->ddc);