drm/linux: Add sign_extend64()
[dragonfly.git] / sys / dev / drm / drm_dp_mst_topology.c
blob396c35cca74d205d4e7b986cb127a11df8675554
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;
333 /* get length contained in this portion */
334 msg->curchunk_len = recv_hdr.msg_len;
335 msg->curchunk_hdrlen = hdrlen;
337 /* we have already gotten an somt - don't bother parsing */
338 if (recv_hdr.somt && msg->have_somt)
339 return false;
341 if (recv_hdr.somt) {
342 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
343 msg->have_somt = true;
345 if (recv_hdr.eomt)
346 msg->have_eomt = true;
348 /* copy the bytes for the remainder of this header chunk */
349 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
350 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
351 } else {
352 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
353 msg->curchunk_idx += replybuflen;
356 if (msg->curchunk_idx >= msg->curchunk_len) {
357 /* do CRC */
358 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
359 /* copy chunk into bigger msg */
360 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
361 msg->curlen += msg->curchunk_len - 1;
363 return true;
366 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
367 struct drm_dp_sideband_msg_reply_body *repmsg)
369 int idx = 1;
370 int i;
371 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
372 idx += 16;
373 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
374 idx++;
375 if (idx > raw->curlen)
376 goto fail_len;
377 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
378 if (raw->msg[idx] & 0x80)
379 repmsg->u.link_addr.ports[i].input_port = 1;
381 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
382 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
384 idx++;
385 if (idx > raw->curlen)
386 goto fail_len;
387 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
388 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
389 if (repmsg->u.link_addr.ports[i].input_port == 0)
390 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
391 idx++;
392 if (idx > raw->curlen)
393 goto fail_len;
394 if (repmsg->u.link_addr.ports[i].input_port == 0) {
395 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
396 idx++;
397 if (idx > raw->curlen)
398 goto fail_len;
399 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
400 idx += 16;
401 if (idx > raw->curlen)
402 goto fail_len;
403 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
404 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
405 idx++;
408 if (idx > raw->curlen)
409 goto fail_len;
412 return true;
413 fail_len:
414 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
415 return false;
418 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
419 struct drm_dp_sideband_msg_reply_body *repmsg)
421 int idx = 1;
422 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
423 idx++;
424 if (idx > raw->curlen)
425 goto fail_len;
426 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
427 if (idx > raw->curlen)
428 goto fail_len;
430 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
431 return true;
432 fail_len:
433 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
434 return false;
437 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
438 struct drm_dp_sideband_msg_reply_body *repmsg)
440 int idx = 1;
441 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
442 idx++;
443 if (idx > raw->curlen)
444 goto fail_len;
445 return true;
446 fail_len:
447 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
448 return false;
451 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
452 struct drm_dp_sideband_msg_reply_body *repmsg)
454 int idx = 1;
456 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
457 idx++;
458 if (idx > raw->curlen)
459 goto fail_len;
460 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
461 idx++;
462 /* TODO check */
463 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
464 return true;
465 fail_len:
466 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
467 return false;
470 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
471 struct drm_dp_sideband_msg_reply_body *repmsg)
473 int idx = 1;
474 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
475 idx++;
476 if (idx > raw->curlen)
477 goto fail_len;
478 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
479 idx += 2;
480 if (idx > raw->curlen)
481 goto fail_len;
482 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
483 idx += 2;
484 if (idx > raw->curlen)
485 goto fail_len;
486 return true;
487 fail_len:
488 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
489 return false;
492 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
493 struct drm_dp_sideband_msg_reply_body *repmsg)
495 int idx = 1;
496 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
497 idx++;
498 if (idx > raw->curlen)
499 goto fail_len;
500 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
501 idx++;
502 if (idx > raw->curlen)
503 goto fail_len;
504 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
505 idx += 2;
506 if (idx > raw->curlen)
507 goto fail_len;
508 return true;
509 fail_len:
510 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
511 return false;
514 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
515 struct drm_dp_sideband_msg_reply_body *repmsg)
517 int idx = 1;
518 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
519 idx++;
520 if (idx > raw->curlen)
521 goto fail_len;
522 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
523 idx += 2;
524 if (idx > raw->curlen)
525 goto fail_len;
526 return true;
527 fail_len:
528 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
529 return false;
532 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
533 struct drm_dp_sideband_msg_reply_body *msg)
535 memset(msg, 0, sizeof(*msg));
536 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
537 msg->req_type = (raw->msg[0] & 0x7f);
539 if (msg->reply_type) {
540 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
541 msg->u.nak.reason = raw->msg[17];
542 msg->u.nak.nak_data = raw->msg[18];
543 return false;
546 switch (msg->req_type) {
547 case DP_LINK_ADDRESS:
548 return drm_dp_sideband_parse_link_address(raw, msg);
549 case DP_QUERY_PAYLOAD:
550 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
551 case DP_REMOTE_DPCD_READ:
552 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
553 case DP_REMOTE_DPCD_WRITE:
554 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
555 case DP_REMOTE_I2C_READ:
556 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
557 case DP_ENUM_PATH_RESOURCES:
558 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
559 case DP_ALLOCATE_PAYLOAD:
560 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
561 default:
562 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
563 return false;
567 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
568 struct drm_dp_sideband_msg_req_body *msg)
570 int idx = 1;
572 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
573 idx++;
574 if (idx > raw->curlen)
575 goto fail_len;
577 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
578 idx += 16;
579 if (idx > raw->curlen)
580 goto fail_len;
582 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
583 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
584 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
585 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
586 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
587 idx++;
588 return true;
589 fail_len:
590 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
591 return false;
594 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
595 struct drm_dp_sideband_msg_req_body *msg)
597 int idx = 1;
599 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
600 idx++;
601 if (idx > raw->curlen)
602 goto fail_len;
604 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
605 idx += 16;
606 if (idx > raw->curlen)
607 goto fail_len;
609 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
610 idx++;
611 return true;
612 fail_len:
613 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
614 return false;
617 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
618 struct drm_dp_sideband_msg_req_body *msg)
620 memset(msg, 0, sizeof(*msg));
621 msg->req_type = (raw->msg[0] & 0x7f);
623 switch (msg->req_type) {
624 case DP_CONNECTION_STATUS_NOTIFY:
625 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
626 case DP_RESOURCE_STATUS_NOTIFY:
627 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
628 default:
629 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
630 return false;
634 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
636 struct drm_dp_sideband_msg_req_body req;
638 req.req_type = DP_REMOTE_DPCD_WRITE;
639 req.u.dpcd_write.port_number = port_num;
640 req.u.dpcd_write.dpcd_address = offset;
641 req.u.dpcd_write.num_bytes = num_bytes;
642 req.u.dpcd_write.bytes = bytes;
643 drm_dp_encode_sideband_req(&req, msg);
645 return 0;
648 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
650 struct drm_dp_sideband_msg_req_body req;
652 req.req_type = DP_LINK_ADDRESS;
653 drm_dp_encode_sideband_req(&req, msg);
654 return 0;
657 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
659 struct drm_dp_sideband_msg_req_body req;
661 req.req_type = DP_ENUM_PATH_RESOURCES;
662 req.u.port_num.port_number = port_num;
663 drm_dp_encode_sideband_req(&req, msg);
664 msg->path_msg = true;
665 return 0;
668 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
669 u8 vcpi, uint16_t pbn)
671 struct drm_dp_sideband_msg_req_body req;
672 memset(&req, 0, sizeof(req));
673 req.req_type = DP_ALLOCATE_PAYLOAD;
674 req.u.allocate_payload.port_number = port_num;
675 req.u.allocate_payload.vcpi = vcpi;
676 req.u.allocate_payload.pbn = pbn;
677 drm_dp_encode_sideband_req(&req, msg);
678 msg->path_msg = true;
679 return 0;
682 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
683 struct drm_dp_vcpi *vcpi)
685 int ret;
687 mutex_lock(&mgr->payload_lock);
688 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
689 if (ret > mgr->max_payloads) {
690 ret = -EINVAL;
691 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
692 goto out_unlock;
695 set_bit(ret, &mgr->payload_mask);
696 vcpi->vcpi = ret;
697 mgr->proposed_vcpis[ret - 1] = vcpi;
698 out_unlock:
699 mutex_unlock(&mgr->payload_lock);
700 return ret;
703 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
704 int id)
706 if (id == 0)
707 return;
709 mutex_lock(&mgr->payload_lock);
710 DRM_DEBUG_KMS("putting payload %d\n", id);
711 clear_bit(id, &mgr->payload_mask);
712 mgr->proposed_vcpis[id - 1] = NULL;
713 mutex_unlock(&mgr->payload_lock);
716 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
717 struct drm_dp_sideband_msg_tx *txmsg)
719 bool ret;
720 mutex_lock(&mgr->qlock);
721 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
722 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
723 mutex_unlock(&mgr->qlock);
724 return ret;
727 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
728 struct drm_dp_sideband_msg_tx *txmsg)
730 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
731 int ret;
733 ret = wait_event_timeout(mgr->tx_waitq,
734 check_txmsg_state(mgr, txmsg),
735 (4 * HZ));
736 mutex_lock(&mstb->mgr->qlock);
737 if (ret > 0) {
738 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
739 ret = -EIO;
740 goto out;
742 } else {
743 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
745 /* dump some state */
746 ret = -EIO;
748 /* remove from q */
749 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
750 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
751 list_del(&txmsg->next);
754 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
755 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
756 mstb->tx_slots[txmsg->seqno] = NULL;
759 out:
760 mutex_unlock(&mgr->qlock);
762 return ret;
765 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
767 struct drm_dp_mst_branch *mstb;
769 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
770 if (!mstb)
771 return NULL;
773 mstb->lct = lct;
774 if (lct > 1)
775 memcpy(mstb->rad, rad, lct / 2);
776 INIT_LIST_HEAD(&mstb->ports);
777 kref_init(&mstb->kref);
778 return mstb;
781 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
783 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
784 struct drm_dp_mst_port *port, *tmp;
785 bool wake_tx = false;
788 * destroy all ports - don't need lock
789 * as there are no more references to the mst branch
790 * device at this point.
792 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
793 list_del(&port->next);
794 drm_dp_put_port(port);
797 /* drop any tx slots msg */
798 mutex_lock(&mstb->mgr->qlock);
799 if (mstb->tx_slots[0]) {
800 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
801 mstb->tx_slots[0] = NULL;
802 wake_tx = true;
804 if (mstb->tx_slots[1]) {
805 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
806 mstb->tx_slots[1] = NULL;
807 wake_tx = true;
809 mutex_unlock(&mstb->mgr->qlock);
811 if (wake_tx)
812 wake_up(&mstb->mgr->tx_waitq);
813 kfree(mstb);
816 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
818 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
822 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
824 struct drm_dp_mst_branch *mstb;
826 switch (old_pdt) {
827 case DP_PEER_DEVICE_DP_LEGACY_CONV:
828 case DP_PEER_DEVICE_SST_SINK:
829 /* remove i2c over sideband */
830 drm_dp_mst_unregister_i2c_bus(&port->aux);
831 break;
832 case DP_PEER_DEVICE_MST_BRANCHING:
833 mstb = port->mstb;
834 port->mstb = NULL;
835 drm_dp_put_mst_branch_device(mstb);
836 break;
840 static void drm_dp_destroy_port(struct kref *kref)
842 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
843 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
845 if (!port->input) {
846 port->vcpi.num_slots = 0;
848 kfree(port->cached_edid);
851 * The only time we don't have a connector
852 * on an output port is if the connector init
853 * fails.
855 if (port->connector) {
856 /* we can't destroy the connector here, as
857 * we might be holding the mode_config.mutex
858 * from an EDID retrieval */
860 mutex_lock(&mgr->destroy_connector_lock);
861 list_add(&port->next, &mgr->destroy_connector_list);
862 mutex_unlock(&mgr->destroy_connector_lock);
863 schedule_work(&mgr->destroy_connector_work);
864 return;
866 /* no need to clean up vcpi
867 * as if we have no connector we never setup a vcpi */
868 drm_dp_port_teardown_pdt(port, port->pdt);
870 kfree(port);
873 static void drm_dp_put_port(struct drm_dp_mst_port *port)
875 kref_put(&port->kref, drm_dp_destroy_port);
878 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)
880 struct drm_dp_mst_port *port;
881 struct drm_dp_mst_branch *rmstb;
882 if (to_find == mstb) {
883 kref_get(&mstb->kref);
884 return mstb;
886 list_for_each_entry(port, &mstb->ports, next) {
887 if (port->mstb) {
888 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
889 if (rmstb)
890 return rmstb;
893 return NULL;
896 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)
898 struct drm_dp_mst_branch *rmstb = NULL;
899 mutex_lock(&mgr->lock);
900 if (mgr->mst_primary)
901 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
902 mutex_unlock(&mgr->lock);
903 return rmstb;
906 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)
908 struct drm_dp_mst_port *port, *mport;
910 list_for_each_entry(port, &mstb->ports, next) {
911 if (port == to_find) {
912 kref_get(&port->kref);
913 return port;
915 if (port->mstb) {
916 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
917 if (mport)
918 return mport;
921 return NULL;
924 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)
926 struct drm_dp_mst_port *rport = NULL;
927 mutex_lock(&mgr->lock);
928 if (mgr->mst_primary)
929 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
930 mutex_unlock(&mgr->lock);
931 return rport;
934 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
936 struct drm_dp_mst_port *port;
938 list_for_each_entry(port, &mstb->ports, next) {
939 if (port->port_num == port_num) {
940 kref_get(&port->kref);
941 return port;
945 return NULL;
949 * calculate a new RAD for this MST branch device
950 * if parent has an LCT of 2 then it has 1 nibble of RAD,
951 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
953 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
954 u8 *rad)
956 int lct = port->parent->lct;
957 int shift = 4;
958 int idx = lct / 2;
959 if (lct > 1) {
960 memcpy(rad, port->parent->rad, idx);
961 shift = (lct % 2) ? 4 : 0;
962 } else
963 rad[0] = 0;
965 rad[idx] |= port->port_num << shift;
966 return lct + 1;
970 * return sends link address for new mstb
972 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
974 int ret;
975 u8 rad[6], lct;
976 bool send_link = false;
977 switch (port->pdt) {
978 case DP_PEER_DEVICE_DP_LEGACY_CONV:
979 case DP_PEER_DEVICE_SST_SINK:
980 /* add i2c over sideband */
981 ret = drm_dp_mst_register_i2c_bus(&port->aux);
982 break;
983 case DP_PEER_DEVICE_MST_BRANCHING:
984 lct = drm_dp_calculate_rad(port, rad);
986 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
987 port->mstb->mgr = port->mgr;
988 port->mstb->port_parent = port;
990 send_link = true;
991 break;
993 return send_link;
996 static void drm_dp_check_port_guid(struct drm_dp_mst_branch *mstb,
997 struct drm_dp_mst_port *port)
999 int ret;
1000 if (port->dpcd_rev >= 0x12) {
1001 port->guid_valid = drm_dp_validate_guid(mstb->mgr, port->guid);
1002 if (!port->guid_valid) {
1003 ret = drm_dp_send_dpcd_write(mstb->mgr,
1004 port,
1005 DP_GUID,
1006 16, port->guid);
1007 port->guid_valid = true;
1012 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1013 int pnum,
1014 char *proppath,
1015 size_t proppath_size)
1017 int i;
1018 char temp[8];
1019 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1020 for (i = 0; i < (mstb->lct - 1); i++) {
1021 int shift = (i % 2) ? 0 : 4;
1022 int port_num = mstb->rad[i / 2] >> shift;
1023 snprintf(temp, sizeof(temp), "-%d", port_num);
1024 strlcat(proppath, temp, proppath_size);
1026 snprintf(temp, sizeof(temp), "-%d", pnum);
1027 strlcat(proppath, temp, proppath_size);
1030 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1031 struct device *dev,
1032 struct drm_dp_link_addr_reply_port *port_msg)
1034 struct drm_dp_mst_port *port;
1035 bool ret;
1036 bool created = false;
1037 int old_pdt = 0;
1038 int old_ddps = 0;
1039 port = drm_dp_get_port(mstb, port_msg->port_number);
1040 if (!port) {
1041 port = kzalloc(sizeof(*port), GFP_KERNEL);
1042 if (!port)
1043 return;
1044 kref_init(&port->kref);
1045 port->parent = mstb;
1046 port->port_num = port_msg->port_number;
1047 port->mgr = mstb->mgr;
1048 port->aux.name = "DPMST";
1049 port->aux.dev = dev;
1050 created = true;
1051 } else {
1052 old_pdt = port->pdt;
1053 old_ddps = port->ddps;
1056 port->pdt = port_msg->peer_device_type;
1057 port->input = port_msg->input_port;
1058 port->mcs = port_msg->mcs;
1059 port->ddps = port_msg->ddps;
1060 port->ldps = port_msg->legacy_device_plug_status;
1061 port->dpcd_rev = port_msg->dpcd_revision;
1062 port->num_sdp_streams = port_msg->num_sdp_streams;
1063 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1064 memcpy(port->guid, port_msg->peer_guid, 16);
1066 /* manage mstb port lists with mgr lock - take a reference
1067 for this list */
1068 if (created) {
1069 mutex_lock(&mstb->mgr->lock);
1070 kref_get(&port->kref);
1071 list_add(&port->next, &mstb->ports);
1072 mutex_unlock(&mstb->mgr->lock);
1075 if (old_ddps != port->ddps) {
1076 if (port->ddps) {
1077 drm_dp_check_port_guid(mstb, port);
1078 if (!port->input)
1079 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1080 } else {
1081 port->guid_valid = false;
1082 port->available_pbn = 0;
1086 if (old_pdt != port->pdt && !port->input) {
1087 drm_dp_port_teardown_pdt(port, old_pdt);
1089 ret = drm_dp_port_setup_pdt(port);
1090 if (ret == true)
1091 drm_dp_send_link_address(mstb->mgr, port->mstb);
1094 if (created && !port->input) {
1095 char proppath[255];
1097 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
1098 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1099 if (!port->connector) {
1100 /* remove it from the port list */
1101 mutex_lock(&mstb->mgr->lock);
1102 list_del(&port->next);
1103 mutex_unlock(&mstb->mgr->lock);
1104 /* drop port list reference */
1105 drm_dp_put_port(port);
1106 goto out;
1108 if (port->port_num >= DP_MST_LOGICAL_PORT_0) {
1109 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1110 drm_mode_connector_set_tile_property(port->connector);
1112 (*mstb->mgr->cbs->register_connector)(port->connector);
1115 out:
1116 /* put reference to this port */
1117 drm_dp_put_port(port);
1120 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1121 struct drm_dp_connection_status_notify *conn_stat)
1123 struct drm_dp_mst_port *port;
1124 int old_pdt;
1125 int old_ddps;
1126 bool dowork = false;
1127 port = drm_dp_get_port(mstb, conn_stat->port_number);
1128 if (!port)
1129 return;
1131 old_ddps = port->ddps;
1132 old_pdt = port->pdt;
1133 port->pdt = conn_stat->peer_device_type;
1134 port->mcs = conn_stat->message_capability_status;
1135 port->ldps = conn_stat->legacy_device_plug_status;
1136 port->ddps = conn_stat->displayport_device_plug_status;
1138 if (old_ddps != port->ddps) {
1139 if (port->ddps) {
1140 drm_dp_check_port_guid(mstb, port);
1141 dowork = true;
1142 } else {
1143 port->guid_valid = false;
1144 port->available_pbn = 0;
1147 if (old_pdt != port->pdt && !port->input) {
1148 drm_dp_port_teardown_pdt(port, old_pdt);
1150 if (drm_dp_port_setup_pdt(port))
1151 dowork = true;
1154 drm_dp_put_port(port);
1155 if (dowork)
1156 queue_work(system_long_wq, &mstb->mgr->work);
1160 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1161 u8 lct, u8 *rad)
1163 struct drm_dp_mst_branch *mstb;
1164 struct drm_dp_mst_port *port;
1165 int i;
1166 /* find the port by iterating down */
1168 mutex_lock(&mgr->lock);
1169 mstb = mgr->mst_primary;
1171 for (i = 0; i < lct - 1; i++) {
1172 int shift = (i % 2) ? 0 : 4;
1173 int port_num = rad[i / 2] >> shift;
1175 list_for_each_entry(port, &mstb->ports, next) {
1176 if (port->port_num == port_num) {
1177 mstb = port->mstb;
1178 if (!mstb) {
1179 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1180 goto out;
1183 break;
1187 kref_get(&mstb->kref);
1188 out:
1189 mutex_unlock(&mgr->lock);
1190 return mstb;
1193 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1194 struct drm_dp_mst_branch *mstb)
1196 struct drm_dp_mst_port *port;
1197 struct drm_dp_mst_branch *mstb_child;
1198 if (!mstb->link_address_sent)
1199 drm_dp_send_link_address(mgr, mstb);
1201 list_for_each_entry(port, &mstb->ports, next) {
1202 if (port->input)
1203 continue;
1205 if (!port->ddps)
1206 continue;
1208 if (!port->available_pbn)
1209 drm_dp_send_enum_path_resources(mgr, mstb, port);
1211 if (port->mstb) {
1212 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1213 if (mstb_child) {
1214 drm_dp_check_and_send_link_address(mgr, mstb_child);
1215 drm_dp_put_mst_branch_device(mstb_child);
1221 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1223 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1224 struct drm_dp_mst_branch *mstb;
1226 mutex_lock(&mgr->lock);
1227 mstb = mgr->mst_primary;
1228 if (mstb) {
1229 kref_get(&mstb->kref);
1231 mutex_unlock(&mgr->lock);
1232 if (mstb) {
1233 drm_dp_check_and_send_link_address(mgr, mstb);
1234 drm_dp_put_mst_branch_device(mstb);
1238 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1239 u8 *guid)
1241 static u8 zero_guid[16];
1243 if (!memcmp(guid, zero_guid, 16)) {
1244 u64 salt = get_jiffies_64();
1245 memcpy(&guid[0], &salt, sizeof(u64));
1246 memcpy(&guid[8], &salt, sizeof(u64));
1247 return false;
1249 return true;
1252 #if 0
1253 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1255 struct drm_dp_sideband_msg_req_body req;
1257 req.req_type = DP_REMOTE_DPCD_READ;
1258 req.u.dpcd_read.port_number = port_num;
1259 req.u.dpcd_read.dpcd_address = offset;
1260 req.u.dpcd_read.num_bytes = num_bytes;
1261 drm_dp_encode_sideband_req(&req, msg);
1263 return 0;
1265 #endif
1267 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1268 bool up, u8 *msg, int len)
1270 int ret;
1271 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1272 int tosend, total, offset;
1273 int retries = 0;
1275 retry:
1276 total = len;
1277 offset = 0;
1278 do {
1279 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1281 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1282 &msg[offset],
1283 tosend);
1284 if (ret != tosend) {
1285 if (ret == -EIO && retries < 5) {
1286 retries++;
1287 goto retry;
1289 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1291 return -EIO;
1293 offset += tosend;
1294 total -= tosend;
1295 } while (total > 0);
1296 return 0;
1299 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1300 struct drm_dp_sideband_msg_tx *txmsg)
1302 struct drm_dp_mst_branch *mstb = txmsg->dst;
1304 /* both msg slots are full */
1305 if (txmsg->seqno == -1) {
1306 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1307 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1308 return -EAGAIN;
1310 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1311 txmsg->seqno = mstb->last_seqno;
1312 mstb->last_seqno ^= 1;
1313 } else if (mstb->tx_slots[0] == NULL)
1314 txmsg->seqno = 0;
1315 else
1316 txmsg->seqno = 1;
1317 mstb->tx_slots[txmsg->seqno] = txmsg;
1319 hdr->broadcast = 0;
1320 hdr->path_msg = txmsg->path_msg;
1321 hdr->lct = mstb->lct;
1322 hdr->lcr = mstb->lct - 1;
1323 if (mstb->lct > 1)
1324 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1325 hdr->seqno = txmsg->seqno;
1326 return 0;
1329 * process a single block of the next message in the sideband queue
1331 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1332 struct drm_dp_sideband_msg_tx *txmsg,
1333 bool up)
1335 u8 chunk[48];
1336 struct drm_dp_sideband_msg_hdr hdr;
1337 int len, space, idx, tosend;
1338 int ret;
1340 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1342 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1343 txmsg->seqno = -1;
1344 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1347 /* make hdr from dst mst - for replies use seqno
1348 otherwise assign one */
1349 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1350 if (ret < 0)
1351 return ret;
1353 /* amount left to send in this message */
1354 len = txmsg->cur_len - txmsg->cur_offset;
1356 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1357 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1359 tosend = min(len, space);
1360 if (len == txmsg->cur_len)
1361 hdr.somt = 1;
1362 if (space >= len)
1363 hdr.eomt = 1;
1366 hdr.msg_len = tosend + 1;
1367 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1368 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1369 /* add crc at end */
1370 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1371 idx += tosend + 1;
1373 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1374 if (ret) {
1375 DRM_DEBUG_KMS("sideband msg failed to send\n");
1376 return ret;
1379 txmsg->cur_offset += tosend;
1380 if (txmsg->cur_offset == txmsg->cur_len) {
1381 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1382 return 1;
1384 return 0;
1387 /* must be called holding qlock */
1388 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1390 struct drm_dp_sideband_msg_tx *txmsg;
1391 int ret;
1393 /* construct a chunk from the first msg in the tx_msg queue */
1394 if (list_empty(&mgr->tx_msg_downq)) {
1395 mgr->tx_down_in_progress = false;
1396 return;
1398 mgr->tx_down_in_progress = true;
1400 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1401 ret = process_single_tx_qlock(mgr, txmsg, false);
1402 if (ret == 1) {
1403 /* txmsg is sent it should be in the slots now */
1404 list_del(&txmsg->next);
1405 } else if (ret) {
1406 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1407 list_del(&txmsg->next);
1408 if (txmsg->seqno != -1)
1409 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1410 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1411 wake_up(&mgr->tx_waitq);
1413 if (list_empty(&mgr->tx_msg_downq)) {
1414 mgr->tx_down_in_progress = false;
1415 return;
1419 /* called holding qlock */
1420 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1422 struct drm_dp_sideband_msg_tx *txmsg;
1423 int ret;
1425 /* construct a chunk from the first msg in the tx_msg queue */
1426 if (list_empty(&mgr->tx_msg_upq)) {
1427 mgr->tx_up_in_progress = false;
1428 return;
1431 txmsg = list_first_entry(&mgr->tx_msg_upq, struct drm_dp_sideband_msg_tx, next);
1432 ret = process_single_tx_qlock(mgr, txmsg, true);
1433 if (ret == 1) {
1434 /* up txmsgs aren't put in slots - so free after we send it */
1435 list_del(&txmsg->next);
1436 kfree(txmsg);
1437 } else if (ret)
1438 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1439 mgr->tx_up_in_progress = true;
1442 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1443 struct drm_dp_sideband_msg_tx *txmsg)
1445 mutex_lock(&mgr->qlock);
1446 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1447 if (!mgr->tx_down_in_progress)
1448 process_single_down_tx_qlock(mgr);
1449 mutex_unlock(&mgr->qlock);
1452 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1453 struct drm_dp_mst_branch *mstb)
1455 int len;
1456 struct drm_dp_sideband_msg_tx *txmsg;
1457 int ret;
1459 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1460 if (!txmsg)
1461 return;
1463 txmsg->dst = mstb;
1464 len = build_link_address(txmsg);
1466 mstb->link_address_sent = true;
1467 drm_dp_queue_down_tx(mgr, txmsg);
1469 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1470 if (ret > 0) {
1471 int i;
1473 if (txmsg->reply.reply_type == 1)
1474 DRM_DEBUG_KMS("link address nak received\n");
1475 else {
1476 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1477 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1478 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,
1479 txmsg->reply.u.link_addr.ports[i].input_port,
1480 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1481 txmsg->reply.u.link_addr.ports[i].port_number,
1482 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1483 txmsg->reply.u.link_addr.ports[i].mcs,
1484 txmsg->reply.u.link_addr.ports[i].ddps,
1485 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1486 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1487 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1489 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1490 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1492 (*mgr->cbs->hotplug)(mgr);
1494 } else {
1495 mstb->link_address_sent = false;
1496 DRM_DEBUG_KMS("link address failed %d\n", ret);
1499 kfree(txmsg);
1502 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1503 struct drm_dp_mst_branch *mstb,
1504 struct drm_dp_mst_port *port)
1506 int len;
1507 struct drm_dp_sideband_msg_tx *txmsg;
1508 int ret;
1510 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1511 if (!txmsg)
1512 return -ENOMEM;
1514 txmsg->dst = mstb;
1515 len = build_enum_path_resources(txmsg, port->port_num);
1517 drm_dp_queue_down_tx(mgr, txmsg);
1519 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1520 if (ret > 0) {
1521 if (txmsg->reply.reply_type == 1)
1522 DRM_DEBUG_KMS("enum path resources nak received\n");
1523 else {
1524 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1525 DRM_ERROR("got incorrect port in response\n");
1526 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,
1527 txmsg->reply.u.path_resources.avail_payload_bw_number);
1528 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1532 kfree(txmsg);
1533 return 0;
1536 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1537 struct drm_dp_mst_port *port,
1538 int id,
1539 int pbn)
1541 struct drm_dp_sideband_msg_tx *txmsg;
1542 struct drm_dp_mst_branch *mstb;
1543 int len, ret;
1545 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1546 if (!mstb)
1547 return -EINVAL;
1549 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1550 if (!txmsg) {
1551 ret = -ENOMEM;
1552 goto fail_put;
1555 txmsg->dst = mstb;
1556 len = build_allocate_payload(txmsg, port->port_num,
1558 pbn);
1560 drm_dp_queue_down_tx(mgr, txmsg);
1562 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1563 if (ret > 0) {
1564 if (txmsg->reply.reply_type == 1) {
1565 ret = -EINVAL;
1566 } else
1567 ret = 0;
1569 kfree(txmsg);
1570 fail_put:
1571 drm_dp_put_mst_branch_device(mstb);
1572 return ret;
1575 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1576 int id,
1577 struct drm_dp_payload *payload)
1579 int ret;
1581 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1582 if (ret < 0) {
1583 payload->payload_state = 0;
1584 return ret;
1586 payload->payload_state = DP_PAYLOAD_LOCAL;
1587 return 0;
1590 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1591 struct drm_dp_mst_port *port,
1592 int id,
1593 struct drm_dp_payload *payload)
1595 int ret;
1596 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1597 if (ret < 0)
1598 return ret;
1599 payload->payload_state = DP_PAYLOAD_REMOTE;
1600 return ret;
1603 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1604 struct drm_dp_mst_port *port,
1605 int id,
1606 struct drm_dp_payload *payload)
1608 DRM_DEBUG_KMS("\n");
1609 /* its okay for these to fail */
1610 if (port) {
1611 drm_dp_payload_send_msg(mgr, port, id, 0);
1614 drm_dp_dpcd_write_payload(mgr, id, payload);
1615 payload->payload_state = 0;
1616 return 0;
1619 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1620 int id,
1621 struct drm_dp_payload *payload)
1623 payload->payload_state = 0;
1624 return 0;
1628 * drm_dp_update_payload_part1() - Execute payload update part 1
1629 * @mgr: manager to use.
1631 * This iterates over all proposed virtual channels, and tries to
1632 * allocate space in the link for them. For 0->slots transitions,
1633 * this step just writes the VCPI to the MST device. For slots->0
1634 * transitions, this writes the updated VCPIs and removes the
1635 * remote VC payloads.
1637 * after calling this the driver should generate ACT and payload
1638 * packets.
1640 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1642 int i;
1643 int cur_slots = 1;
1644 struct drm_dp_payload req_payload;
1645 struct drm_dp_mst_port *port;
1647 mutex_lock(&mgr->payload_lock);
1648 for (i = 0; i < mgr->max_payloads; i++) {
1649 /* solve the current payloads - compare to the hw ones
1650 - update the hw view */
1651 req_payload.start_slot = cur_slots;
1652 if (mgr->proposed_vcpis[i]) {
1653 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1654 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1655 } else {
1656 port = NULL;
1657 req_payload.num_slots = 0;
1659 /* work out what is required to happen with this payload */
1660 if (mgr->payloads[i].start_slot != req_payload.start_slot ||
1661 mgr->payloads[i].num_slots != req_payload.num_slots) {
1663 /* need to push an update for this payload */
1664 if (req_payload.num_slots) {
1665 drm_dp_create_payload_step1(mgr, i + 1, &req_payload);
1666 mgr->payloads[i].num_slots = req_payload.num_slots;
1667 } else if (mgr->payloads[i].num_slots) {
1668 mgr->payloads[i].num_slots = 0;
1669 drm_dp_destroy_payload_step1(mgr, port, i + 1, &mgr->payloads[i]);
1670 req_payload.payload_state = mgr->payloads[i].payload_state;
1671 } else
1672 req_payload.payload_state = 0;
1674 mgr->payloads[i].start_slot = req_payload.start_slot;
1675 mgr->payloads[i].payload_state = req_payload.payload_state;
1677 cur_slots += req_payload.num_slots;
1679 mutex_unlock(&mgr->payload_lock);
1681 return 0;
1683 EXPORT_SYMBOL(drm_dp_update_payload_part1);
1686 * drm_dp_update_payload_part2() - Execute payload update part 2
1687 * @mgr: manager to use.
1689 * This iterates over all proposed virtual channels, and tries to
1690 * allocate space in the link for them. For 0->slots transitions,
1691 * this step writes the remote VC payload commands. For slots->0
1692 * this just resets some internal state.
1694 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1696 struct drm_dp_mst_port *port;
1697 int i;
1698 int ret;
1699 mutex_lock(&mgr->payload_lock);
1700 for (i = 0; i < mgr->max_payloads; i++) {
1702 if (!mgr->proposed_vcpis[i])
1703 continue;
1705 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1707 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1708 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1709 ret = drm_dp_create_payload_step2(mgr, port, i + 1, &mgr->payloads[i]);
1710 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1711 ret = drm_dp_destroy_payload_step2(mgr, i + 1, &mgr->payloads[i]);
1713 if (ret) {
1714 mutex_unlock(&mgr->payload_lock);
1715 return ret;
1718 mutex_unlock(&mgr->payload_lock);
1719 return 0;
1721 EXPORT_SYMBOL(drm_dp_update_payload_part2);
1723 #if 0 /* unused as of yet */
1724 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1725 struct drm_dp_mst_port *port,
1726 int offset, int size)
1728 int len;
1729 struct drm_dp_sideband_msg_tx *txmsg;
1731 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1732 if (!txmsg)
1733 return -ENOMEM;
1735 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1736 txmsg->dst = port->parent;
1738 drm_dp_queue_down_tx(mgr, txmsg);
1740 return 0;
1742 #endif
1744 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1745 struct drm_dp_mst_port *port,
1746 int offset, int size, u8 *bytes)
1748 int len;
1749 int ret;
1750 struct drm_dp_sideband_msg_tx *txmsg;
1751 struct drm_dp_mst_branch *mstb;
1753 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1754 if (!mstb)
1755 return -EINVAL;
1757 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1758 if (!txmsg) {
1759 ret = -ENOMEM;
1760 goto fail_put;
1763 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1764 txmsg->dst = mstb;
1766 drm_dp_queue_down_tx(mgr, txmsg);
1768 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1769 if (ret > 0) {
1770 if (txmsg->reply.reply_type == 1) {
1771 ret = -EINVAL;
1772 } else
1773 ret = 0;
1775 kfree(txmsg);
1776 fail_put:
1777 drm_dp_put_mst_branch_device(mstb);
1778 return ret;
1781 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1783 struct drm_dp_sideband_msg_reply_body reply;
1785 reply.reply_type = 1;
1786 reply.req_type = req_type;
1787 drm_dp_encode_sideband_reply(&reply, msg);
1788 return 0;
1791 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1792 struct drm_dp_mst_branch *mstb,
1793 int req_type, int seqno, bool broadcast)
1795 struct drm_dp_sideband_msg_tx *txmsg;
1797 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1798 if (!txmsg)
1799 return -ENOMEM;
1801 txmsg->dst = mstb;
1802 txmsg->seqno = seqno;
1803 drm_dp_encode_up_ack_reply(txmsg, req_type);
1805 mutex_lock(&mgr->qlock);
1806 list_add_tail(&txmsg->next, &mgr->tx_msg_upq);
1807 if (!mgr->tx_up_in_progress) {
1808 process_single_up_tx_qlock(mgr);
1810 mutex_unlock(&mgr->qlock);
1811 return 0;
1814 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1815 int dp_link_count,
1816 int *out)
1818 switch (dp_link_bw) {
1819 default:
1820 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1821 dp_link_bw, dp_link_count);
1822 return false;
1824 case DP_LINK_BW_1_62:
1825 *out = 3 * dp_link_count;
1826 break;
1827 case DP_LINK_BW_2_7:
1828 *out = 5 * dp_link_count;
1829 break;
1830 case DP_LINK_BW_5_4:
1831 *out = 10 * dp_link_count;
1832 break;
1834 return true;
1838 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
1839 * @mgr: manager to set state for
1840 * @mst_state: true to enable MST on this connector - false to disable.
1842 * This is called by the driver when it detects an MST capable device plugged
1843 * into a DP MST capable port, or when a DP MST capable device is unplugged.
1845 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
1847 int ret = 0;
1848 struct drm_dp_mst_branch *mstb = NULL;
1850 mutex_lock(&mgr->lock);
1851 if (mst_state == mgr->mst_state)
1852 goto out_unlock;
1854 mgr->mst_state = mst_state;
1855 /* set the device into MST mode */
1856 if (mst_state) {
1857 WARN_ON(mgr->mst_primary);
1859 /* get dpcd info */
1860 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
1861 if (ret != DP_RECEIVER_CAP_SIZE) {
1862 DRM_DEBUG_KMS("failed to read DPCD\n");
1863 goto out_unlock;
1866 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
1867 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
1868 &mgr->pbn_div)) {
1869 ret = -EINVAL;
1870 goto out_unlock;
1873 mgr->total_pbn = 2560;
1874 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
1875 mgr->avail_slots = mgr->total_slots;
1877 /* add initial branch device at LCT 1 */
1878 mstb = drm_dp_add_mst_branch_device(1, NULL);
1879 if (mstb == NULL) {
1880 ret = -ENOMEM;
1881 goto out_unlock;
1883 mstb->mgr = mgr;
1885 /* give this the main reference */
1886 mgr->mst_primary = mstb;
1887 kref_get(&mgr->mst_primary->kref);
1890 struct drm_dp_payload reset_pay;
1891 reset_pay.start_slot = 0;
1892 reset_pay.num_slots = 0x3f;
1893 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
1896 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1897 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
1898 if (ret < 0) {
1899 goto out_unlock;
1903 /* sort out guid */
1904 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, mgr->guid, 16);
1905 if (ret != 16) {
1906 DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret);
1907 goto out_unlock;
1910 mgr->guid_valid = drm_dp_validate_guid(mgr, mgr->guid);
1911 if (!mgr->guid_valid) {
1912 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, mgr->guid, 16);
1913 mgr->guid_valid = true;
1916 queue_work(system_long_wq, &mgr->work);
1918 ret = 0;
1919 } else {
1920 /* disable MST on the device */
1921 mstb = mgr->mst_primary;
1922 mgr->mst_primary = NULL;
1923 /* this can fail if the device is gone */
1924 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
1925 ret = 0;
1926 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
1927 mgr->payload_mask = 0;
1928 set_bit(0, &mgr->payload_mask);
1931 out_unlock:
1932 mutex_unlock(&mgr->lock);
1933 if (mstb)
1934 drm_dp_put_mst_branch_device(mstb);
1935 return ret;
1938 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
1941 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
1942 * @mgr: manager to suspend
1944 * This function tells the MST device that we can't handle UP messages
1945 * anymore. This should stop it from sending any since we are suspended.
1947 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
1949 mutex_lock(&mgr->lock);
1950 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1951 DP_MST_EN | DP_UPSTREAM_IS_SRC);
1952 mutex_unlock(&mgr->lock);
1953 flush_work(&mgr->work);
1954 flush_work(&mgr->destroy_connector_work);
1956 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
1959 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
1960 * @mgr: manager to resume
1962 * This will fetch DPCD and see if the device is still there,
1963 * if it is, it will rewrite the MSTM control bits, and return.
1965 * if the device fails this returns -1, and the driver should do
1966 * a full MST reprobe, in case we were undocked.
1968 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
1970 int ret = 0;
1972 mutex_lock(&mgr->lock);
1974 if (mgr->mst_primary) {
1975 int sret;
1976 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
1977 if (sret != DP_RECEIVER_CAP_SIZE) {
1978 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
1979 ret = -1;
1980 goto out_unlock;
1983 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1984 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
1985 if (ret < 0) {
1986 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
1987 ret = -1;
1988 goto out_unlock;
1990 ret = 0;
1991 } else
1992 ret = -1;
1994 out_unlock:
1995 mutex_unlock(&mgr->lock);
1996 return ret;
1998 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2000 static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2002 int len;
2003 u8 replyblock[32];
2004 int replylen, origlen, curreply;
2005 int ret;
2006 struct drm_dp_sideband_msg_rx *msg;
2007 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2008 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2010 len = min(mgr->max_dpcd_transaction_bytes, 16);
2011 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2012 replyblock, len);
2013 if (ret != len) {
2014 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2015 return;
2017 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2018 if (!ret) {
2019 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2020 return;
2022 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2024 origlen = replylen;
2025 replylen -= len;
2026 curreply = len;
2027 while (replylen > 0) {
2028 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2029 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2030 replyblock, len);
2031 if (ret != len) {
2032 DRM_DEBUG_KMS("failed to read a chunk\n");
2034 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2035 if (ret == false)
2036 DRM_DEBUG_KMS("failed to build sideband msg\n");
2037 curreply += len;
2038 replylen -= len;
2042 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2044 int ret = 0;
2046 drm_dp_get_one_sb_msg(mgr, false);
2048 if (mgr->down_rep_recv.have_eomt) {
2049 struct drm_dp_sideband_msg_tx *txmsg;
2050 struct drm_dp_mst_branch *mstb;
2051 int slot = -1;
2052 mstb = drm_dp_get_mst_branch_device(mgr,
2053 mgr->down_rep_recv.initial_hdr.lct,
2054 mgr->down_rep_recv.initial_hdr.rad);
2056 if (!mstb) {
2057 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2058 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2059 return 0;
2062 /* find the message */
2063 slot = mgr->down_rep_recv.initial_hdr.seqno;
2064 mutex_lock(&mgr->qlock);
2065 txmsg = mstb->tx_slots[slot];
2066 /* remove from slots */
2067 mutex_unlock(&mgr->qlock);
2069 if (!txmsg) {
2070 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2071 mstb,
2072 mgr->down_rep_recv.initial_hdr.seqno,
2073 mgr->down_rep_recv.initial_hdr.lct,
2074 mgr->down_rep_recv.initial_hdr.rad[0],
2075 mgr->down_rep_recv.msg[0]);
2076 drm_dp_put_mst_branch_device(mstb);
2077 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2078 return 0;
2081 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2082 if (txmsg->reply.reply_type == 1) {
2083 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);
2086 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2087 drm_dp_put_mst_branch_device(mstb);
2089 mutex_lock(&mgr->qlock);
2090 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2091 mstb->tx_slots[slot] = NULL;
2092 mutex_unlock(&mgr->qlock);
2094 wake_up(&mgr->tx_waitq);
2096 return ret;
2099 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2101 int ret = 0;
2102 drm_dp_get_one_sb_msg(mgr, true);
2104 if (mgr->up_req_recv.have_eomt) {
2105 struct drm_dp_sideband_msg_req_body msg;
2106 struct drm_dp_mst_branch *mstb;
2107 bool seqno;
2108 mstb = drm_dp_get_mst_branch_device(mgr,
2109 mgr->up_req_recv.initial_hdr.lct,
2110 mgr->up_req_recv.initial_hdr.rad);
2111 if (!mstb) {
2112 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2113 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2114 return 0;
2117 seqno = mgr->up_req_recv.initial_hdr.seqno;
2118 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2120 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2121 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
2122 drm_dp_update_port(mstb, &msg.u.conn_stat);
2123 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);
2124 (*mgr->cbs->hotplug)(mgr);
2126 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2127 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
2128 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2131 drm_dp_put_mst_branch_device(mstb);
2132 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2134 return ret;
2138 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2139 * @mgr: manager to notify irq for.
2140 * @esi: 4 bytes from SINK_COUNT_ESI
2142 * This should be called from the driver when it detects a short IRQ,
2143 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2144 * topology manager will process the sideband messages received as a result
2145 * of this.
2147 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2149 int ret = 0;
2150 int sc;
2151 *handled = false;
2152 sc = esi[0] & 0x3f;
2154 if (sc != mgr->sink_count) {
2155 mgr->sink_count = sc;
2156 *handled = true;
2159 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2160 ret = drm_dp_mst_handle_down_rep(mgr);
2161 *handled = true;
2164 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2165 ret |= drm_dp_mst_handle_up_req(mgr);
2166 *handled = true;
2169 drm_dp_mst_kick_tx(mgr);
2170 return ret;
2172 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2175 * drm_dp_mst_detect_port() - get connection status for an MST port
2176 * @mgr: manager for this port
2177 * @port: unverified pointer to a port
2179 * This returns the current connection state for a port. It validates the
2180 * port pointer still exists so the caller doesn't require a reference
2182 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2183 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2185 enum drm_connector_status status = connector_status_disconnected;
2187 /* we need to search for the port in the mgr in case its gone */
2188 port = drm_dp_get_validated_port_ref(mgr, port);
2189 if (!port)
2190 return connector_status_disconnected;
2192 if (!port->ddps)
2193 goto out;
2195 switch (port->pdt) {
2196 case DP_PEER_DEVICE_NONE:
2197 case DP_PEER_DEVICE_MST_BRANCHING:
2198 break;
2200 case DP_PEER_DEVICE_SST_SINK:
2201 status = connector_status_connected;
2202 /* for logical ports - cache the EDID */
2203 if (port->port_num >= 8 && !port->cached_edid) {
2204 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2206 break;
2207 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2208 if (port->ldps)
2209 status = connector_status_connected;
2210 break;
2212 out:
2213 drm_dp_put_port(port);
2214 return status;
2216 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2219 * drm_dp_mst_get_edid() - get EDID for an MST port
2220 * @connector: toplevel connector to get EDID for
2221 * @mgr: manager for this port
2222 * @port: unverified pointer to a port.
2224 * This returns an EDID for the port connected to a connector,
2225 * It validates the pointer still exists so the caller doesn't require a
2226 * reference.
2228 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2230 struct edid *edid = NULL;
2232 /* we need to search for the port in the mgr in case its gone */
2233 port = drm_dp_get_validated_port_ref(mgr, port);
2234 if (!port)
2235 return NULL;
2237 if (port->cached_edid)
2238 edid = drm_edid_duplicate(port->cached_edid);
2239 else {
2240 edid = drm_get_edid(connector, &port->aux.ddc);
2241 drm_mode_connector_set_tile_property(connector);
2243 drm_dp_put_port(port);
2244 return edid;
2246 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2249 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2250 * @mgr: manager to use
2251 * @pbn: payload bandwidth to convert into slots.
2253 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2254 int pbn)
2256 int num_slots;
2258 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2260 if (num_slots > mgr->avail_slots)
2261 return -ENOSPC;
2262 return num_slots;
2264 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2266 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2267 struct drm_dp_vcpi *vcpi, int pbn)
2269 int num_slots;
2270 int ret;
2272 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2274 if (num_slots > mgr->avail_slots)
2275 return -ENOSPC;
2277 vcpi->pbn = pbn;
2278 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2279 vcpi->num_slots = num_slots;
2281 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2282 if (ret < 0)
2283 return ret;
2284 return 0;
2288 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2289 * @mgr: manager for this port
2290 * @port: port to allocate a virtual channel for.
2291 * @pbn: payload bandwidth number to request
2292 * @slots: returned number of slots for this PBN.
2294 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2296 int ret;
2298 port = drm_dp_get_validated_port_ref(mgr, port);
2299 if (!port)
2300 return false;
2302 if (port->vcpi.vcpi > 0) {
2303 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2304 if (pbn == port->vcpi.pbn) {
2305 *slots = port->vcpi.num_slots;
2306 return true;
2310 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2311 if (ret) {
2312 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2313 goto out;
2315 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2316 *slots = port->vcpi.num_slots;
2318 drm_dp_put_port(port);
2319 return true;
2320 out:
2321 return false;
2323 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2325 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2327 int slots = 0;
2328 port = drm_dp_get_validated_port_ref(mgr, port);
2329 if (!port)
2330 return slots;
2332 slots = port->vcpi.num_slots;
2333 drm_dp_put_port(port);
2334 return slots;
2336 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2339 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2340 * @mgr: manager for this port
2341 * @port: unverified pointer to a port.
2343 * This just resets the number of slots for the ports VCPI for later programming.
2345 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2347 port = drm_dp_get_validated_port_ref(mgr, port);
2348 if (!port)
2349 return;
2350 port->vcpi.num_slots = 0;
2351 drm_dp_put_port(port);
2353 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2356 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2357 * @mgr: manager for this port
2358 * @port: unverified port to deallocate vcpi for
2360 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2362 port = drm_dp_get_validated_port_ref(mgr, port);
2363 if (!port)
2364 return;
2366 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2367 port->vcpi.num_slots = 0;
2368 port->vcpi.pbn = 0;
2369 port->vcpi.aligned_pbn = 0;
2370 port->vcpi.vcpi = 0;
2371 drm_dp_put_port(port);
2373 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2375 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2376 int id, struct drm_dp_payload *payload)
2378 u8 payload_alloc[3], status;
2379 int ret;
2380 int retries = 0;
2382 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2383 DP_PAYLOAD_TABLE_UPDATED);
2385 payload_alloc[0] = id;
2386 payload_alloc[1] = payload->start_slot;
2387 payload_alloc[2] = payload->num_slots;
2389 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2390 if (ret != 3) {
2391 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2392 goto fail;
2395 retry:
2396 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2397 if (ret < 0) {
2398 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2399 goto fail;
2402 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2403 retries++;
2404 if (retries < 20) {
2405 usleep_range(10000, 20000);
2406 goto retry;
2408 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2409 ret = -EINVAL;
2410 goto fail;
2412 ret = 0;
2413 fail:
2414 return ret;
2419 * drm_dp_check_act_status() - Check ACT handled status.
2420 * @mgr: manager to use
2422 * Check the payload status bits in the DPCD for ACT handled completion.
2424 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2426 u8 status;
2427 int ret;
2428 int count = 0;
2430 do {
2431 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2433 if (ret < 0) {
2434 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2435 goto fail;
2438 if (status & DP_PAYLOAD_ACT_HANDLED)
2439 break;
2440 count++;
2441 udelay(100);
2443 } while (count < 30);
2445 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2446 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2447 ret = -EINVAL;
2448 goto fail;
2450 return 0;
2451 fail:
2452 return ret;
2454 EXPORT_SYMBOL(drm_dp_check_act_status);
2457 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2458 * @clock: dot clock for the mode
2459 * @bpp: bpp for the mode.
2461 * This uses the formula in the spec to calculate the PBN value for a mode.
2463 int drm_dp_calc_pbn_mode(int clock, int bpp)
2465 fixed20_12 pix_bw;
2466 fixed20_12 fbpp;
2467 fixed20_12 result;
2468 fixed20_12 margin, tmp;
2469 u32 res;
2471 pix_bw.full = dfixed_const(clock);
2472 fbpp.full = dfixed_const(bpp);
2473 tmp.full = dfixed_const(8);
2474 fbpp.full = dfixed_div(fbpp, tmp);
2476 result.full = dfixed_mul(pix_bw, fbpp);
2477 margin.full = dfixed_const(54);
2478 tmp.full = dfixed_const(64);
2479 margin.full = dfixed_div(margin, tmp);
2480 result.full = dfixed_div(result, margin);
2482 margin.full = dfixed_const(1006);
2483 tmp.full = dfixed_const(1000);
2484 margin.full = dfixed_div(margin, tmp);
2485 result.full = dfixed_mul(result, margin);
2487 result.full = dfixed_div(result, tmp);
2488 result.full = dfixed_ceil(result);
2489 res = dfixed_trunc(result);
2490 return res;
2492 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2494 static int test_calc_pbn_mode(void)
2496 int ret;
2497 ret = drm_dp_calc_pbn_mode(154000, 30);
2498 if (ret != 689)
2499 return -EINVAL;
2500 ret = drm_dp_calc_pbn_mode(234000, 30);
2501 if (ret != 1047)
2502 return -EINVAL;
2503 return 0;
2506 /* we want to kick the TX after we've ack the up/down IRQs. */
2507 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2509 queue_work(system_long_wq, &mgr->tx_work);
2512 static void drm_dp_mst_dump_mstb(struct seq_file *m,
2513 struct drm_dp_mst_branch *mstb)
2515 struct drm_dp_mst_port *port;
2516 int tabs = mstb->lct;
2517 char prefix[10];
2518 int i;
2520 for (i = 0; i < tabs; i++)
2521 prefix[i] = '\t';
2522 prefix[i] = '\0';
2524 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2525 list_for_each_entry(port, &mstb->ports, next) {
2526 seq_printf(m, "%sport: %d: ddps: %d ldps: %d, %p, conn: %p\n", prefix, port->port_num, port->ddps, port->ldps, port, port->connector);
2527 if (port->mstb)
2528 drm_dp_mst_dump_mstb(m, port->mstb);
2532 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2533 char *buf)
2535 int ret;
2536 int i;
2537 for (i = 0; i < 4; i++) {
2538 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2539 if (ret != 16)
2540 break;
2542 if (i == 4)
2543 return true;
2544 return false;
2548 * drm_dp_mst_dump_topology(): dump topology to seq file.
2549 * @m: seq_file to dump output to
2550 * @mgr: manager to dump current topology for.
2552 * helper to dump MST topology to a seq file for debugfs.
2554 void drm_dp_mst_dump_topology(struct seq_file *m,
2555 struct drm_dp_mst_topology_mgr *mgr)
2557 int i;
2558 struct drm_dp_mst_port *port;
2559 mutex_lock(&mgr->lock);
2560 if (mgr->mst_primary)
2561 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2563 /* dump VCPIs */
2564 mutex_unlock(&mgr->lock);
2566 mutex_lock(&mgr->payload_lock);
2567 seq_printf(m, "vcpi: %lx\n", mgr->payload_mask);
2569 for (i = 0; i < mgr->max_payloads; i++) {
2570 if (mgr->proposed_vcpis[i]) {
2571 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2572 seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots);
2573 } else
2574 seq_printf(m, "vcpi %d:unsed\n", i);
2576 for (i = 0; i < mgr->max_payloads; i++) {
2577 seq_printf(m, "payload %d: %d, %d, %d\n",
2579 mgr->payloads[i].payload_state,
2580 mgr->payloads[i].start_slot,
2581 mgr->payloads[i].num_slots);
2585 mutex_unlock(&mgr->payload_lock);
2587 mutex_lock(&mgr->lock);
2588 if (mgr->mst_primary) {
2589 u8 buf[64];
2590 bool bret;
2591 int ret;
2592 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2593 seq_printf(m, "dpcd: ");
2594 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2595 seq_printf(m, "%02x ", buf[i]);
2596 seq_printf(m, "\n");
2597 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2598 seq_printf(m, "faux/mst: ");
2599 for (i = 0; i < 2; i++)
2600 seq_printf(m, "%02x ", buf[i]);
2601 seq_printf(m, "\n");
2602 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2603 seq_printf(m, "mst ctrl: ");
2604 for (i = 0; i < 1; i++)
2605 seq_printf(m, "%02x ", buf[i]);
2606 seq_printf(m, "\n");
2608 /* dump the standard OUI branch header */
2609 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2610 seq_printf(m, "branch oui: ");
2611 for (i = 0; i < 0x3; i++)
2612 seq_printf(m, "%02x", buf[i]);
2613 seq_printf(m, " devid: ");
2614 for (i = 0x3; i < 0x8; i++)
2615 seq_printf(m, "%c", buf[i]);
2616 seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2617 seq_printf(m, "\n");
2618 bret = dump_dp_payload_table(mgr, buf);
2619 if (bret == true) {
2620 seq_printf(m, "payload table: ");
2621 for (i = 0; i < 63; i++)
2622 seq_printf(m, "%02x ", buf[i]);
2623 seq_printf(m, "\n");
2628 mutex_unlock(&mgr->lock);
2631 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2633 static void drm_dp_tx_work(struct work_struct *work)
2635 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2637 mutex_lock(&mgr->qlock);
2638 if (mgr->tx_down_in_progress)
2639 process_single_down_tx_qlock(mgr);
2640 mutex_unlock(&mgr->qlock);
2643 static void drm_dp_destroy_connector_work(struct work_struct *work)
2645 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
2646 struct drm_dp_mst_port *port;
2647 bool send_hotplug = false;
2649 * Not a regular list traverse as we have to drop the destroy
2650 * connector lock before destroying the connector, to avoid AB->BA
2651 * ordering between this lock and the config mutex.
2653 for (;;) {
2654 mutex_lock(&mgr->destroy_connector_lock);
2655 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2656 if (!port) {
2657 mutex_unlock(&mgr->destroy_connector_lock);
2658 break;
2660 list_del(&port->next);
2661 mutex_unlock(&mgr->destroy_connector_lock);
2663 mgr->cbs->destroy_connector(mgr, port->connector);
2665 drm_dp_port_teardown_pdt(port, port->pdt);
2667 if (!port->input && port->vcpi.vcpi > 0)
2668 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2669 kfree(port);
2670 send_hotplug = true;
2672 if (send_hotplug)
2673 (*mgr->cbs->hotplug)(mgr);
2677 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2678 * @mgr: manager struct to initialise
2679 * @dev: device providing this structure - for i2c addition.
2680 * @aux: DP helper aux channel to talk to this device
2681 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2682 * @max_payloads: maximum number of payloads this GPU can source
2683 * @conn_base_id: the connector object ID the MST device is connected to.
2685 * Return 0 for success, or negative error code on failure
2687 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2688 struct device *dev, struct drm_dp_aux *aux,
2689 int max_dpcd_transaction_bytes,
2690 int max_payloads, int conn_base_id)
2692 mutex_init(&mgr->lock);
2693 mutex_init(&mgr->qlock);
2694 mutex_init(&mgr->payload_lock);
2695 mutex_init(&mgr->destroy_connector_lock);
2696 INIT_LIST_HEAD(&mgr->tx_msg_upq);
2697 INIT_LIST_HEAD(&mgr->tx_msg_downq);
2698 INIT_LIST_HEAD(&mgr->destroy_connector_list);
2699 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2700 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
2701 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
2702 init_waitqueue_head(&mgr->tx_waitq);
2703 mgr->dev = dev;
2704 mgr->aux = aux;
2705 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2706 mgr->max_payloads = max_payloads;
2707 mgr->conn_base_id = conn_base_id;
2708 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
2709 if (!mgr->payloads)
2710 return -ENOMEM;
2711 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
2712 if (!mgr->proposed_vcpis)
2713 return -ENOMEM;
2714 set_bit(0, &mgr->payload_mask);
2715 test_calc_pbn_mode();
2716 return 0;
2718 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
2721 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
2722 * @mgr: manager to destroy
2724 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
2726 flush_work(&mgr->work);
2727 flush_work(&mgr->destroy_connector_work);
2728 mutex_lock(&mgr->payload_lock);
2729 kfree(mgr->payloads);
2730 mgr->payloads = NULL;
2731 kfree(mgr->proposed_vcpis);
2732 mgr->proposed_vcpis = NULL;
2733 mutex_unlock(&mgr->payload_lock);
2734 mgr->dev = NULL;
2735 mgr->aux = NULL;
2737 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
2739 /* I2C device */
2740 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
2741 int num)
2743 struct drm_dp_aux *aux = adapter->algo_data;
2744 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
2745 struct drm_dp_mst_branch *mstb;
2746 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2747 unsigned int i;
2748 bool reading = false;
2749 struct drm_dp_sideband_msg_req_body msg;
2750 struct drm_dp_sideband_msg_tx *txmsg = NULL;
2751 int ret;
2753 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
2754 if (!mstb)
2755 return -EREMOTEIO;
2757 /* construct i2c msg */
2758 /* see if last msg is a read */
2759 if (msgs[num - 1].flags & I2C_M_RD)
2760 reading = true;
2762 if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
2763 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
2764 ret = -EIO;
2765 goto out;
2768 memset(&msg, 0, sizeof(msg));
2769 msg.req_type = DP_REMOTE_I2C_READ;
2770 msg.u.i2c_read.num_transactions = num - 1;
2771 msg.u.i2c_read.port_number = port->port_num;
2772 for (i = 0; i < num - 1; i++) {
2773 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
2774 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
2775 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
2777 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
2778 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
2780 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2781 if (!txmsg) {
2782 ret = -ENOMEM;
2783 goto out;
2786 txmsg->dst = mstb;
2787 drm_dp_encode_sideband_req(&msg, txmsg);
2789 drm_dp_queue_down_tx(mgr, txmsg);
2791 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2792 if (ret > 0) {
2794 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
2795 ret = -EREMOTEIO;
2796 goto out;
2798 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
2799 ret = -EIO;
2800 goto out;
2802 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
2803 ret = num;
2805 out:
2806 kfree(txmsg);
2807 drm_dp_put_mst_branch_device(mstb);
2808 return ret;
2811 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
2813 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
2814 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
2815 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
2816 I2C_FUNC_10BIT_ADDR;
2819 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
2820 .functionality = drm_dp_mst_i2c_functionality,
2821 .master_xfer = drm_dp_mst_i2c_xfer,
2825 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
2826 * @aux: DisplayPort AUX channel
2828 * Returns 0 on success or a negative error code on failure.
2830 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
2832 aux->ddc.algo = &drm_dp_mst_i2c_algo;
2833 aux->ddc.algo_data = aux;
2834 aux->ddc.retries = 3;
2836 aux->ddc.class = I2C_CLASS_DDC;
2837 aux->ddc.owner = THIS_MODULE;
2838 aux->ddc.dev.parent = aux->dev;
2839 aux->ddc.dev.of_node = aux->dev->of_node;
2841 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
2842 sizeof(aux->ddc.name));
2844 return i2c_add_adapter(&aux->ddc);
2848 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
2849 * @aux: DisplayPort AUX channel
2851 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
2853 i2c_del_adapter(&aux->ddc);