1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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>
32
33#include <drm/drm_fixed.h>
34
35
36
37
38
39
40
41
42static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
43 char *buf);
44static int test_calc_pbn_mode(void);
45
46static void drm_dp_put_port(struct drm_dp_mst_port *port);
47
48static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
49 int id,
50 struct drm_dp_payload *payload);
51
52static 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);
55
56static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
57 struct drm_dp_mst_branch *mstb);
58static 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);
61static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
62 u8 *guid);
63
64static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
65static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
66static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
67
68static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
69{
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;
75
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++;
86 }
87 if ((remainder & 0x10) == 0x10)
88 remainder ^= 0x13;
89 }
90
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;
97 }
98
99 return remainder;
100}
101
102static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
103{
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;
109
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++;
120 }
121 if ((remainder & 0x100) == 0x100)
122 remainder ^= 0xd5;
123 }
124
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;
131 }
132
133 return remainder & 0xff;
134}
135static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
136{
137 u8 size = 3;
138 size += (hdr->lct / 2);
139 return size;
140}
141
142static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
143 u8 *buf, int *len)
144{
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);
154
155 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
156 buf[idx - 1] |= (crc4 & 0xf);
157
158 *len = idx;
159}
160
161static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
162 u8 *buf, int buflen, u8 *hdrlen)
163{
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);
175
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;
179 }
180
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;
196}
197
198static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
199 struct drm_dp_sideband_msg_tx *raw)
200{
201 int idx = 0;
202 int i;
203 u8 *buf = raw->msg;
204 buf[idx++] = req->req_type & 0x7f;
205
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++;
225 }
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++;
230 }
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;
249
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;
274
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++;
278 }
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;
284
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;
295 }
296 raw->cur_len = idx;
297}
298
299static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
300{
301 u8 crc4;
302 crc4 = drm_dp_msg_data_crc4(msg, len);
303 msg[len] = crc4;
304}
305
306static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
307 struct drm_dp_sideband_msg_tx *raw)
308{
309 int idx = 0;
310 u8 *buf = raw->msg;
311
312 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
313
314 raw->cur_len = idx;
315}
316
317
318static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
319 u8 *replybuf, u8 replybuflen, bool hdr)
320{
321 int ret;
322 u8 crc4;
323
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;
331 }
332
333
334 msg->curchunk_len = recv_hdr.msg_len;
335 msg->curchunk_hdrlen = hdrlen;
336
337
338 if (recv_hdr.somt && msg->have_somt)
339 return false;
340
341 if (recv_hdr.somt) {
342 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
343 msg->have_somt = true;
344 }
345 if (recv_hdr.eomt)
346 msg->have_eomt = true;
347
348
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;
354 }
355
356 if (msg->curchunk_idx >= msg->curchunk_len) {
357
358 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
359
360 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
361 msg->curlen += msg->curchunk_len - 1;
362 }
363 return true;
364}
365
366static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
367 struct drm_dp_sideband_msg_reply_body *repmsg)
368{
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;
380
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);
383
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++;
406
407 }
408 if (idx > raw->curlen)
409 goto fail_len;
410 }
411
412 return true;
413fail_len:
414 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
415 return false;
416}
417
418static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
419 struct drm_dp_sideband_msg_reply_body *repmsg)
420{
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;
429
430 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
431 return true;
432fail_len:
433 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
434 return false;
435}
436
437static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
438 struct drm_dp_sideband_msg_reply_body *repmsg)
439{
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;
446fail_len:
447 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
448 return false;
449}
450
451static 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)
453{
454 int idx = 1;
455
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
463 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
464 return true;
465fail_len:
466 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
467 return false;
468}
469
470static 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)
472{
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;
487fail_len:
488 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
489 return false;
490}
491
492static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
493 struct drm_dp_sideband_msg_reply_body *repmsg)
494{
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;
509fail_len:
510 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
511 return false;
512}
513
514static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
515 struct drm_dp_sideband_msg_reply_body *repmsg)
516{
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;
527fail_len:
528 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
529 return false;
530}
531
532static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
533 struct drm_dp_sideband_msg_reply_body *msg)
534{
535 memset(msg, 0, sizeof(*msg));
536 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
537 msg->req_type = (raw->msg[0] & 0x7f);
538
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;
544 }
545
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;
564 }
565}
566
567static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
568 struct drm_dp_sideband_msg_req_body *msg)
569{
570 int idx = 1;
571
572 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
573 idx++;
574 if (idx > raw->curlen)
575 goto fail_len;
576
577 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
578 idx += 16;
579 if (idx > raw->curlen)
580 goto fail_len;
581
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;
589fail_len:
590 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
591 return false;
592}
593
594static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
595 struct drm_dp_sideband_msg_req_body *msg)
596{
597 int idx = 1;
598
599 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
600 idx++;
601 if (idx > raw->curlen)
602 goto fail_len;
603
604 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
605 idx += 16;
606 if (idx > raw->curlen)
607 goto fail_len;
608
609 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
610 idx++;
611 return true;
612fail_len:
613 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
614 return false;
615}
616
617static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
618 struct drm_dp_sideband_msg_req_body *msg)
619{
620 memset(msg, 0, sizeof(*msg));
621 msg->req_type = (raw->msg[0] & 0x7f);
622
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;
631 }
632}
633
634static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
635{
636 struct drm_dp_sideband_msg_req_body req;
637
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);
644
645 return 0;
646}
647
648static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
649{
650 struct drm_dp_sideband_msg_req_body req;
651
652 req.req_type = DP_LINK_ADDRESS;
653 drm_dp_encode_sideband_req(&req, msg);
654 return 0;
655}
656
657static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
658{
659 struct drm_dp_sideband_msg_req_body req;
660
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;
666}
667
668static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
669 u8 vcpi, uint16_t pbn,
670 u8 number_sdp_streams,
671 u8 *sdp_stream_sink)
672{
673 struct drm_dp_sideband_msg_req_body req;
674 memset(&req, 0, sizeof(req));
675 req.req_type = DP_ALLOCATE_PAYLOAD;
676 req.u.allocate_payload.port_number = port_num;
677 req.u.allocate_payload.vcpi = vcpi;
678 req.u.allocate_payload.pbn = pbn;
679 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
680 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
681 number_sdp_streams);
682 drm_dp_encode_sideband_req(&req, msg);
683 msg->path_msg = true;
684 return 0;
685}
686
687static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
688 struct drm_dp_vcpi *vcpi)
689{
690 int ret, vcpi_ret;
691
692 mutex_lock(&mgr->payload_lock);
693 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
694 if (ret > mgr->max_payloads) {
695 ret = -EINVAL;
696 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
697 goto out_unlock;
698 }
699
700 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
701 if (vcpi_ret > mgr->max_payloads) {
702 ret = -EINVAL;
703 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
704 goto out_unlock;
705 }
706
707 set_bit(ret, &mgr->payload_mask);
708 set_bit(vcpi_ret, &mgr->vcpi_mask);
709 vcpi->vcpi = vcpi_ret + 1;
710 mgr->proposed_vcpis[ret - 1] = vcpi;
711out_unlock:
712 mutex_unlock(&mgr->payload_lock);
713 return ret;
714}
715
716static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
717 int vcpi)
718{
719 int i;
720 if (vcpi == 0)
721 return;
722
723 mutex_lock(&mgr->payload_lock);
724 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
725 clear_bit(vcpi - 1, &mgr->vcpi_mask);
726
727 for (i = 0; i < mgr->max_payloads; i++) {
728 if (mgr->proposed_vcpis[i])
729 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
730 mgr->proposed_vcpis[i] = NULL;
731 clear_bit(i + 1, &mgr->payload_mask);
732 }
733 }
734 mutex_unlock(&mgr->payload_lock);
735}
736
737static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
738 struct drm_dp_sideband_msg_tx *txmsg)
739{
740 bool ret;
741
742
743
744
745
746
747 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
748 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
749 return ret;
750}
751
752static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
753 struct drm_dp_sideband_msg_tx *txmsg)
754{
755 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
756 int ret;
757
758 ret = wait_event_timeout(mgr->tx_waitq,
759 check_txmsg_state(mgr, txmsg),
760 (4 * HZ));
761 mutex_lock(&mstb->mgr->qlock);
762 if (ret > 0) {
763 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
764 ret = -EIO;
765 goto out;
766 }
767 } else {
768 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
769
770
771 ret = -EIO;
772
773
774 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
775 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
776 list_del(&txmsg->next);
777 }
778
779 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
780 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
781 mstb->tx_slots[txmsg->seqno] = NULL;
782 }
783 }
784out:
785 mutex_unlock(&mgr->qlock);
786
787 return ret;
788}
789
790static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
791{
792 struct drm_dp_mst_branch *mstb;
793
794 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
795 if (!mstb)
796 return NULL;
797
798 mstb->lct = lct;
799 if (lct > 1)
800 memcpy(mstb->rad, rad, lct / 2);
801 INIT_LIST_HEAD(&mstb->ports);
802 kref_init(&mstb->kref);
803 return mstb;
804}
805
806static void drm_dp_free_mst_port(struct kref *kref);
807
808static void drm_dp_free_mst_branch_device(struct kref *kref)
809{
810 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
811 if (mstb->port_parent) {
812 if (list_empty(&mstb->port_parent->next))
813 kref_put(&mstb->port_parent->kref, drm_dp_free_mst_port);
814 }
815 kfree(mstb);
816}
817
818static void drm_dp_destroy_mst_branch_device(struct kref *kref)
819{
820 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
821 struct drm_dp_mst_port *port, *tmp;
822 bool wake_tx = false;
823
824
825
826
827
828 kref_init(kref);
829
830 if (mstb->port_parent && list_empty(&mstb->port_parent->next))
831 kref_get(&mstb->port_parent->kref);
832
833
834
835
836
837
838 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
839 list_del(&port->next);
840 drm_dp_put_port(port);
841 }
842
843
844 mutex_lock(&mstb->mgr->qlock);
845 if (mstb->tx_slots[0]) {
846 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
847 mstb->tx_slots[0] = NULL;
848 wake_tx = true;
849 }
850 if (mstb->tx_slots[1]) {
851 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
852 mstb->tx_slots[1] = NULL;
853 wake_tx = true;
854 }
855 mutex_unlock(&mstb->mgr->qlock);
856
857 if (wake_tx)
858 wake_up(&mstb->mgr->tx_waitq);
859
860 kref_put(kref, drm_dp_free_mst_branch_device);
861}
862
863static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
864{
865 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
866}
867
868
869static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
870{
871 struct drm_dp_mst_branch *mstb;
872
873 switch (old_pdt) {
874 case DP_PEER_DEVICE_DP_LEGACY_CONV:
875 case DP_PEER_DEVICE_SST_SINK:
876
877 drm_dp_mst_unregister_i2c_bus(&port->aux);
878 break;
879 case DP_PEER_DEVICE_MST_BRANCHING:
880 mstb = port->mstb;
881 port->mstb = NULL;
882 drm_dp_put_mst_branch_device(mstb);
883 break;
884 }
885}
886
887static void drm_dp_destroy_port(struct kref *kref)
888{
889 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
890 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
891
892 if (!port->input) {
893 port->vcpi.num_slots = 0;
894
895 kfree(port->cached_edid);
896
897
898
899
900
901
902 if (port->connector) {
903
904
905
906
907 mutex_lock(&mgr->destroy_connector_lock);
908 kref_get(&port->parent->kref);
909 list_add(&port->next, &mgr->destroy_connector_list);
910 mutex_unlock(&mgr->destroy_connector_lock);
911 schedule_work(&mgr->destroy_connector_work);
912 return;
913 }
914
915
916 drm_dp_port_teardown_pdt(port, port->pdt);
917 }
918 kfree(port);
919}
920
921static void drm_dp_put_port(struct drm_dp_mst_port *port)
922{
923 kref_put(&port->kref, drm_dp_destroy_port);
924}
925
926static 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)
927{
928 struct drm_dp_mst_port *port;
929 struct drm_dp_mst_branch *rmstb;
930 if (to_find == mstb) {
931 kref_get(&mstb->kref);
932 return mstb;
933 }
934 list_for_each_entry(port, &mstb->ports, next) {
935 if (port->mstb) {
936 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
937 if (rmstb)
938 return rmstb;
939 }
940 }
941 return NULL;
942}
943
944static struct drm_dp_mst_branch *drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb)
945{
946 struct drm_dp_mst_branch *rmstb = NULL;
947 mutex_lock(&mgr->lock);
948 if (mgr->mst_primary)
949 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
950 mutex_unlock(&mgr->lock);
951 return rmstb;
952}
953
954static 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)
955{
956 struct drm_dp_mst_port *port, *mport;
957
958 list_for_each_entry(port, &mstb->ports, next) {
959 if (port == to_find) {
960 kref_get(&port->kref);
961 return port;
962 }
963 if (port->mstb) {
964 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
965 if (mport)
966 return mport;
967 }
968 }
969 return NULL;
970}
971
972static struct drm_dp_mst_port *drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
973{
974 struct drm_dp_mst_port *rport = NULL;
975 mutex_lock(&mgr->lock);
976 if (mgr->mst_primary)
977 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
978 mutex_unlock(&mgr->lock);
979 return rport;
980}
981
982static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
983{
984 struct drm_dp_mst_port *port;
985
986 list_for_each_entry(port, &mstb->ports, next) {
987 if (port->port_num == port_num) {
988 kref_get(&port->kref);
989 return port;
990 }
991 }
992
993 return NULL;
994}
995
996
997
998
999
1000
1001static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1002 u8 *rad)
1003{
1004 int parent_lct = port->parent->lct;
1005 int shift = 4;
1006 int idx = (parent_lct - 1) / 2;
1007 if (parent_lct > 1) {
1008 memcpy(rad, port->parent->rad, idx + 1);
1009 shift = (parent_lct % 2) ? 4 : 0;
1010 } else
1011 rad[0] = 0;
1012
1013 rad[idx] |= port->port_num << shift;
1014 return parent_lct + 1;
1015}
1016
1017
1018
1019
1020static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1021{
1022 int ret;
1023 u8 rad[6], lct;
1024 bool send_link = false;
1025 switch (port->pdt) {
1026 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1027 case DP_PEER_DEVICE_SST_SINK:
1028
1029 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1030 break;
1031 case DP_PEER_DEVICE_MST_BRANCHING:
1032 lct = drm_dp_calculate_rad(port, rad);
1033
1034 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1035 port->mstb->mgr = port->mgr;
1036 port->mstb->port_parent = port;
1037
1038 send_link = true;
1039 break;
1040 }
1041 return send_link;
1042}
1043
1044static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1045{
1046 int ret;
1047
1048 memcpy(mstb->guid, guid, 16);
1049
1050 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1051 if (mstb->port_parent) {
1052 ret = drm_dp_send_dpcd_write(
1053 mstb->mgr,
1054 mstb->port_parent,
1055 DP_GUID,
1056 16,
1057 mstb->guid);
1058 } else {
1059
1060 ret = drm_dp_dpcd_write(
1061 mstb->mgr->aux,
1062 DP_GUID,
1063 mstb->guid,
1064 16);
1065 }
1066 }
1067}
1068
1069static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1070 int pnum,
1071 char *proppath,
1072 size_t proppath_size)
1073{
1074 int i;
1075 char temp[8];
1076 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1077 for (i = 0; i < (mstb->lct - 1); i++) {
1078 int shift = (i % 2) ? 0 : 4;
1079 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1080 snprintf(temp, sizeof(temp), "-%d", port_num);
1081 strlcat(proppath, temp, proppath_size);
1082 }
1083 snprintf(temp, sizeof(temp), "-%d", pnum);
1084 strlcat(proppath, temp, proppath_size);
1085}
1086
1087static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1088 struct device *dev,
1089 struct drm_dp_link_addr_reply_port *port_msg)
1090{
1091 struct drm_dp_mst_port *port;
1092 bool ret;
1093 bool created = false;
1094 int old_pdt = 0;
1095 int old_ddps = 0;
1096 port = drm_dp_get_port(mstb, port_msg->port_number);
1097 if (!port) {
1098 port = kzalloc(sizeof(*port), GFP_KERNEL);
1099 if (!port)
1100 return;
1101 kref_init(&port->kref);
1102 port->parent = mstb;
1103 port->port_num = port_msg->port_number;
1104 port->mgr = mstb->mgr;
1105 port->aux.name = "DPMST";
1106 port->aux.dev = dev;
1107 created = true;
1108 } else {
1109 old_pdt = port->pdt;
1110 old_ddps = port->ddps;
1111 }
1112
1113 port->pdt = port_msg->peer_device_type;
1114 port->input = port_msg->input_port;
1115 port->mcs = port_msg->mcs;
1116 port->ddps = port_msg->ddps;
1117 port->ldps = port_msg->legacy_device_plug_status;
1118 port->dpcd_rev = port_msg->dpcd_revision;
1119 port->num_sdp_streams = port_msg->num_sdp_streams;
1120 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1121
1122
1123
1124 if (created) {
1125 mutex_lock(&mstb->mgr->lock);
1126 kref_get(&port->kref);
1127 list_add(&port->next, &mstb->ports);
1128 mutex_unlock(&mstb->mgr->lock);
1129 }
1130
1131 if (old_ddps != port->ddps) {
1132 if (port->ddps) {
1133 if (!port->input)
1134 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1135 } else {
1136 port->available_pbn = 0;
1137 }
1138 }
1139
1140 if (old_pdt != port->pdt && !port->input) {
1141 drm_dp_port_teardown_pdt(port, old_pdt);
1142
1143 ret = drm_dp_port_setup_pdt(port);
1144 if (ret == true)
1145 drm_dp_send_link_address(mstb->mgr, port->mstb);
1146 }
1147
1148 if (created && !port->input) {
1149 char proppath[255];
1150
1151 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
1152 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1153 if (!port->connector) {
1154
1155 mutex_lock(&mstb->mgr->lock);
1156 list_del(&port->next);
1157 mutex_unlock(&mstb->mgr->lock);
1158
1159 drm_dp_put_port(port);
1160 goto out;
1161 }
1162 if (port->port_num >= DP_MST_LOGICAL_PORT_0) {
1163 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1164 drm_mode_connector_set_tile_property(port->connector);
1165 }
1166 (*mstb->mgr->cbs->register_connector)(port->connector);
1167 }
1168
1169out:
1170
1171 drm_dp_put_port(port);
1172}
1173
1174static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1175 struct drm_dp_connection_status_notify *conn_stat)
1176{
1177 struct drm_dp_mst_port *port;
1178 int old_pdt;
1179 int old_ddps;
1180 bool dowork = false;
1181 port = drm_dp_get_port(mstb, conn_stat->port_number);
1182 if (!port)
1183 return;
1184
1185 old_ddps = port->ddps;
1186 old_pdt = port->pdt;
1187 port->pdt = conn_stat->peer_device_type;
1188 port->mcs = conn_stat->message_capability_status;
1189 port->ldps = conn_stat->legacy_device_plug_status;
1190 port->ddps = conn_stat->displayport_device_plug_status;
1191
1192 if (old_ddps != port->ddps) {
1193 if (port->ddps) {
1194 dowork = true;
1195 } else {
1196 port->available_pbn = 0;
1197 }
1198 }
1199 if (old_pdt != port->pdt && !port->input) {
1200 drm_dp_port_teardown_pdt(port, old_pdt);
1201
1202 if (drm_dp_port_setup_pdt(port))
1203 dowork = true;
1204 }
1205
1206 drm_dp_put_port(port);
1207 if (dowork)
1208 queue_work(system_long_wq, &mstb->mgr->work);
1209
1210}
1211
1212static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1213 u8 lct, u8 *rad)
1214{
1215 struct drm_dp_mst_branch *mstb;
1216 struct drm_dp_mst_port *port;
1217 int i;
1218
1219
1220 mutex_lock(&mgr->lock);
1221 mstb = mgr->mst_primary;
1222
1223 for (i = 0; i < lct - 1; i++) {
1224 int shift = (i % 2) ? 0 : 4;
1225 int port_num = (rad[i / 2] >> shift) & 0xf;
1226
1227 list_for_each_entry(port, &mstb->ports, next) {
1228 if (port->port_num == port_num) {
1229 mstb = port->mstb;
1230 if (!mstb) {
1231 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1232 goto out;
1233 }
1234
1235 break;
1236 }
1237 }
1238 }
1239 kref_get(&mstb->kref);
1240out:
1241 mutex_unlock(&mgr->lock);
1242 return mstb;
1243}
1244
1245static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1246 struct drm_dp_mst_branch *mstb,
1247 uint8_t *guid)
1248{
1249 struct drm_dp_mst_branch *found_mstb;
1250 struct drm_dp_mst_port *port;
1251
1252 if (memcmp(mstb->guid, guid, 16) == 0)
1253 return mstb;
1254
1255
1256 list_for_each_entry(port, &mstb->ports, next) {
1257 if (!port->mstb)
1258 continue;
1259
1260 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1261
1262 if (found_mstb)
1263 return found_mstb;
1264 }
1265
1266 return NULL;
1267}
1268
1269static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
1270 struct drm_dp_mst_topology_mgr *mgr,
1271 uint8_t *guid)
1272{
1273 struct drm_dp_mst_branch *mstb;
1274
1275
1276 mutex_lock(&mgr->lock);
1277
1278 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1279
1280 if (mstb)
1281 kref_get(&mstb->kref);
1282
1283 mutex_unlock(&mgr->lock);
1284 return mstb;
1285}
1286
1287static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1288 struct drm_dp_mst_branch *mstb)
1289{
1290 struct drm_dp_mst_port *port;
1291 struct drm_dp_mst_branch *mstb_child;
1292 if (!mstb->link_address_sent)
1293 drm_dp_send_link_address(mgr, mstb);
1294
1295 list_for_each_entry(port, &mstb->ports, next) {
1296 if (port->input)
1297 continue;
1298
1299 if (!port->ddps)
1300 continue;
1301
1302 if (!port->available_pbn)
1303 drm_dp_send_enum_path_resources(mgr, mstb, port);
1304
1305 if (port->mstb) {
1306 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1307 if (mstb_child) {
1308 drm_dp_check_and_send_link_address(mgr, mstb_child);
1309 drm_dp_put_mst_branch_device(mstb_child);
1310 }
1311 }
1312 }
1313}
1314
1315static void drm_dp_mst_link_probe_work(struct work_struct *work)
1316{
1317 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1318 struct drm_dp_mst_branch *mstb;
1319
1320 mutex_lock(&mgr->lock);
1321 mstb = mgr->mst_primary;
1322 if (mstb) {
1323 kref_get(&mstb->kref);
1324 }
1325 mutex_unlock(&mgr->lock);
1326 if (mstb) {
1327 drm_dp_check_and_send_link_address(mgr, mstb);
1328 drm_dp_put_mst_branch_device(mstb);
1329 }
1330}
1331
1332static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1333 u8 *guid)
1334{
1335 static u8 zero_guid[16];
1336
1337 if (!memcmp(guid, zero_guid, 16)) {
1338 u64 salt = get_jiffies_64();
1339 memcpy(&guid[0], &salt, sizeof(u64));
1340 memcpy(&guid[8], &salt, sizeof(u64));
1341 return false;
1342 }
1343 return true;
1344}
1345
1346#if 0
1347static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1348{
1349 struct drm_dp_sideband_msg_req_body req;
1350
1351 req.req_type = DP_REMOTE_DPCD_READ;
1352 req.u.dpcd_read.port_number = port_num;
1353 req.u.dpcd_read.dpcd_address = offset;
1354 req.u.dpcd_read.num_bytes = num_bytes;
1355 drm_dp_encode_sideband_req(&req, msg);
1356
1357 return 0;
1358}
1359#endif
1360
1361static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1362 bool up, u8 *msg, int len)
1363{
1364 int ret;
1365 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1366 int tosend, total, offset;
1367 int retries = 0;
1368
1369retry:
1370 total = len;
1371 offset = 0;
1372 do {
1373 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1374
1375 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1376 &msg[offset],
1377 tosend);
1378 if (ret != tosend) {
1379 if (ret == -EIO && retries < 5) {
1380 retries++;
1381 goto retry;
1382 }
1383 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1384
1385 return -EIO;
1386 }
1387 offset += tosend;
1388 total -= tosend;
1389 } while (total > 0);
1390 return 0;
1391}
1392
1393static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1394 struct drm_dp_sideband_msg_tx *txmsg)
1395{
1396 struct drm_dp_mst_branch *mstb = txmsg->dst;
1397 u8 req_type;
1398
1399
1400 if (txmsg->seqno == -1) {
1401 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1402 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1403 return -EAGAIN;
1404 }
1405 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1406 txmsg->seqno = mstb->last_seqno;
1407 mstb->last_seqno ^= 1;
1408 } else if (mstb->tx_slots[0] == NULL)
1409 txmsg->seqno = 0;
1410 else
1411 txmsg->seqno = 1;
1412 mstb->tx_slots[txmsg->seqno] = txmsg;
1413 }
1414
1415 req_type = txmsg->msg[0] & 0x7f;
1416 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1417 req_type == DP_RESOURCE_STATUS_NOTIFY)
1418 hdr->broadcast = 1;
1419 else
1420 hdr->broadcast = 0;
1421 hdr->path_msg = txmsg->path_msg;
1422 hdr->lct = mstb->lct;
1423 hdr->lcr = mstb->lct - 1;
1424 if (mstb->lct > 1)
1425 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1426 hdr->seqno = txmsg->seqno;
1427 return 0;
1428}
1429
1430
1431
1432static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1433 struct drm_dp_sideband_msg_tx *txmsg,
1434 bool up)
1435{
1436 u8 chunk[48];
1437 struct drm_dp_sideband_msg_hdr hdr;
1438 int len, space, idx, tosend;
1439 int ret;
1440
1441 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1442
1443 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1444 txmsg->seqno = -1;
1445 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1446 }
1447
1448
1449
1450 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1451 if (ret < 0)
1452 return ret;
1453
1454
1455 len = txmsg->cur_len - txmsg->cur_offset;
1456
1457
1458 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1459
1460 tosend = min(len, space);
1461 if (len == txmsg->cur_len)
1462 hdr.somt = 1;
1463 if (space >= len)
1464 hdr.eomt = 1;
1465
1466
1467 hdr.msg_len = tosend + 1;
1468 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1469 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1470
1471 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1472 idx += tosend + 1;
1473
1474 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1475 if (ret) {
1476 DRM_DEBUG_KMS("sideband msg failed to send\n");
1477 return ret;
1478 }
1479
1480 txmsg->cur_offset += tosend;
1481 if (txmsg->cur_offset == txmsg->cur_len) {
1482 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1483 return 1;
1484 }
1485 return 0;
1486}
1487
1488static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1489{
1490 struct drm_dp_sideband_msg_tx *txmsg;
1491 int ret;
1492
1493 WARN_ON(!mutex_is_locked(&mgr->qlock));
1494
1495
1496 if (list_empty(&mgr->tx_msg_downq)) {
1497 mgr->tx_down_in_progress = false;
1498 return;
1499 }
1500 mgr->tx_down_in_progress = true;
1501
1502 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1503 ret = process_single_tx_qlock(mgr, txmsg, false);
1504 if (ret == 1) {
1505
1506 list_del(&txmsg->next);
1507 } else if (ret) {
1508 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1509 list_del(&txmsg->next);
1510 if (txmsg->seqno != -1)
1511 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1512 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1513 wake_up(&mgr->tx_waitq);
1514 }
1515 if (list_empty(&mgr->tx_msg_downq)) {
1516 mgr->tx_down_in_progress = false;
1517 return;
1518 }
1519}
1520
1521
1522static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1523 struct drm_dp_sideband_msg_tx *txmsg)
1524{
1525 int ret;
1526
1527
1528 ret = process_single_tx_qlock(mgr, txmsg, true);
1529
1530 if (ret != 1)
1531 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1532
1533 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1534}
1535
1536static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1537 struct drm_dp_sideband_msg_tx *txmsg)
1538{
1539 mutex_lock(&mgr->qlock);
1540 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1541 if (!mgr->tx_down_in_progress)
1542 process_single_down_tx_qlock(mgr);
1543 mutex_unlock(&mgr->qlock);
1544}
1545
1546static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1547 struct drm_dp_mst_branch *mstb)
1548{
1549 int len;
1550 struct drm_dp_sideband_msg_tx *txmsg;
1551 int ret;
1552
1553 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1554 if (!txmsg)
1555 return;
1556
1557 txmsg->dst = mstb;
1558 len = build_link_address(txmsg);
1559
1560 mstb->link_address_sent = true;
1561 drm_dp_queue_down_tx(mgr, txmsg);
1562
1563 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1564 if (ret > 0) {
1565 int i;
1566
1567 if (txmsg->reply.reply_type == 1)
1568 DRM_DEBUG_KMS("link address nak received\n");
1569 else {
1570 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1571 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1572 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,
1573 txmsg->reply.u.link_addr.ports[i].input_port,
1574 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1575 txmsg->reply.u.link_addr.ports[i].port_number,
1576 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1577 txmsg->reply.u.link_addr.ports[i].mcs,
1578 txmsg->reply.u.link_addr.ports[i].ddps,
1579 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1580 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1581 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1582 }
1583
1584 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1585
1586 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1587 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1588 }
1589 (*mgr->cbs->hotplug)(mgr);
1590 }
1591 } else {
1592 mstb->link_address_sent = false;
1593 DRM_DEBUG_KMS("link address failed %d\n", ret);
1594 }
1595
1596 kfree(txmsg);
1597}
1598
1599static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1600 struct drm_dp_mst_branch *mstb,
1601 struct drm_dp_mst_port *port)
1602{
1603 int len;
1604 struct drm_dp_sideband_msg_tx *txmsg;
1605 int ret;
1606
1607 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1608 if (!txmsg)
1609 return -ENOMEM;
1610
1611 txmsg->dst = mstb;
1612 len = build_enum_path_resources(txmsg, port->port_num);
1613
1614 drm_dp_queue_down_tx(mgr, txmsg);
1615
1616 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1617 if (ret > 0) {
1618 if (txmsg->reply.reply_type == 1)
1619 DRM_DEBUG_KMS("enum path resources nak received\n");
1620 else {
1621 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1622 DRM_ERROR("got incorrect port in response\n");
1623 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,
1624 txmsg->reply.u.path_resources.avail_payload_bw_number);
1625 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1626 }
1627 }
1628
1629 kfree(txmsg);
1630 return 0;
1631}
1632
1633static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
1634{
1635 if (!mstb->port_parent)
1636 return NULL;
1637
1638 if (mstb->port_parent->mstb != mstb)
1639 return mstb->port_parent;
1640
1641 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
1642}
1643
1644static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
1645 struct drm_dp_mst_branch *mstb,
1646 int *port_num)
1647{
1648 struct drm_dp_mst_branch *rmstb = NULL;
1649 struct drm_dp_mst_port *found_port;
1650 mutex_lock(&mgr->lock);
1651 if (mgr->mst_primary) {
1652 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
1653
1654 if (found_port) {
1655 rmstb = found_port->parent;
1656 kref_get(&rmstb->kref);
1657 *port_num = found_port->port_num;
1658 }
1659 }
1660 mutex_unlock(&mgr->lock);
1661 return rmstb;
1662}
1663
1664static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1665 struct drm_dp_mst_port *port,
1666 int id,
1667 int pbn)
1668{
1669 struct drm_dp_sideband_msg_tx *txmsg;
1670 struct drm_dp_mst_branch *mstb;
1671 int len, ret, port_num;
1672 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1673 int i;
1674
1675 port = drm_dp_get_validated_port_ref(mgr, port);
1676 if (!port)
1677 return -EINVAL;
1678
1679 port_num = port->port_num;
1680 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1681 if (!mstb) {
1682 mstb = drm_dp_get_last_connected_port_and_mstb(mgr, port->parent, &port_num);
1683
1684 if (!mstb) {
1685 drm_dp_put_port(port);
1686 return -EINVAL;
1687 }
1688 }
1689
1690 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1691 if (!txmsg) {
1692 ret = -ENOMEM;
1693 goto fail_put;
1694 }
1695
1696 for (i = 0; i < port->num_sdp_streams; i++)
1697 sinks[i] = i;
1698
1699 txmsg->dst = mstb;
1700 len = build_allocate_payload(txmsg, port_num,
1701 id,
1702 pbn, port->num_sdp_streams, sinks);
1703
1704 drm_dp_queue_down_tx(mgr, txmsg);
1705
1706 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1707 if (ret > 0) {
1708 if (txmsg->reply.reply_type == 1) {
1709 ret = -EINVAL;
1710 } else
1711 ret = 0;
1712 }
1713 kfree(txmsg);
1714fail_put:
1715 drm_dp_put_mst_branch_device(mstb);
1716 drm_dp_put_port(port);
1717 return ret;
1718}
1719
1720static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1721 int id,
1722 struct drm_dp_payload *payload)
1723{
1724 int ret;
1725
1726 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1727 if (ret < 0) {
1728 payload->payload_state = 0;
1729 return ret;
1730 }
1731 payload->payload_state = DP_PAYLOAD_LOCAL;
1732 return 0;
1733}
1734
1735static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1736 struct drm_dp_mst_port *port,
1737 int id,
1738 struct drm_dp_payload *payload)
1739{
1740 int ret;
1741 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1742 if (ret < 0)
1743 return ret;
1744 payload->payload_state = DP_PAYLOAD_REMOTE;
1745 return ret;
1746}
1747
1748static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1749 struct drm_dp_mst_port *port,
1750 int id,
1751 struct drm_dp_payload *payload)
1752{
1753 DRM_DEBUG_KMS("\n");
1754
1755 if (port) {
1756 drm_dp_payload_send_msg(mgr, port, id, 0);
1757 }
1758
1759 drm_dp_dpcd_write_payload(mgr, id, payload);
1760 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
1761 return 0;
1762}
1763
1764static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1765 int id,
1766 struct drm_dp_payload *payload)
1767{
1768 payload->payload_state = 0;
1769 return 0;
1770}
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1786{
1787 int i, j;
1788 int cur_slots = 1;
1789 struct drm_dp_payload req_payload;
1790 struct drm_dp_mst_port *port;
1791
1792 mutex_lock(&mgr->payload_lock);
1793 for (i = 0; i < mgr->max_payloads; i++) {
1794
1795
1796 req_payload.start_slot = cur_slots;
1797 if (mgr->proposed_vcpis[i]) {
1798 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1799 port = drm_dp_get_validated_port_ref(mgr, port);
1800 if (!port) {
1801 mutex_unlock(&mgr->payload_lock);
1802 return -EINVAL;
1803 }
1804 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1805 req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
1806 } else {
1807 port = NULL;
1808 req_payload.num_slots = 0;
1809 }
1810
1811 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1812 mgr->payloads[i].start_slot = req_payload.start_slot;
1813 }
1814
1815 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
1816
1817
1818 if (req_payload.num_slots) {
1819 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
1820 mgr->payloads[i].num_slots = req_payload.num_slots;
1821 mgr->payloads[i].vcpi = req_payload.vcpi;
1822 } else if (mgr->payloads[i].num_slots) {
1823 mgr->payloads[i].num_slots = 0;
1824 drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]);
1825 req_payload.payload_state = mgr->payloads[i].payload_state;
1826 mgr->payloads[i].start_slot = 0;
1827 }
1828 mgr->payloads[i].payload_state = req_payload.payload_state;
1829 }
1830 cur_slots += req_payload.num_slots;
1831
1832 if (port)
1833 drm_dp_put_port(port);
1834 }
1835
1836 for (i = 0; i < mgr->max_payloads; i++) {
1837 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1838 DRM_DEBUG_KMS("removing payload %d\n", i);
1839 for (j = i; j < mgr->max_payloads - 1; j++) {
1840 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1841 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1842 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1843 set_bit(j + 1, &mgr->payload_mask);
1844 } else {
1845 clear_bit(j + 1, &mgr->payload_mask);
1846 }
1847 }
1848 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1849 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1850 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1851
1852 }
1853 }
1854 mutex_unlock(&mgr->payload_lock);
1855
1856 return 0;
1857}
1858EXPORT_SYMBOL(drm_dp_update_payload_part1);
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1870{
1871 struct drm_dp_mst_port *port;
1872 int i;
1873 int ret = 0;
1874 mutex_lock(&mgr->payload_lock);
1875 for (i = 0; i < mgr->max_payloads; i++) {
1876
1877 if (!mgr->proposed_vcpis[i])
1878 continue;
1879
1880 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1881
1882 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1883 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1884 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1885 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1886 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1887 }
1888 if (ret) {
1889 mutex_unlock(&mgr->payload_lock);
1890 return ret;
1891 }
1892 }
1893 mutex_unlock(&mgr->payload_lock);
1894 return 0;
1895}
1896EXPORT_SYMBOL(drm_dp_update_payload_part2);
1897
1898#if 0
1899static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1900 struct drm_dp_mst_port *port,
1901 int offset, int size)
1902{
1903 int len;
1904 struct drm_dp_sideband_msg_tx *txmsg;
1905
1906 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1907 if (!txmsg)
1908 return -ENOMEM;
1909
1910 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1911 txmsg->dst = port->parent;
1912
1913 drm_dp_queue_down_tx(mgr, txmsg);
1914
1915 return 0;
1916}
1917#endif
1918
1919static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1920 struct drm_dp_mst_port *port,
1921 int offset, int size, u8 *bytes)
1922{
1923 int len;
1924 int ret;
1925 struct drm_dp_sideband_msg_tx *txmsg;
1926 struct drm_dp_mst_branch *mstb;
1927
1928 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1929 if (!mstb)
1930 return -EINVAL;
1931
1932 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1933 if (!txmsg) {
1934 ret = -ENOMEM;
1935 goto fail_put;
1936 }
1937
1938 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1939 txmsg->dst = mstb;
1940
1941 drm_dp_queue_down_tx(mgr, txmsg);
1942
1943 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1944 if (ret > 0) {
1945 if (txmsg->reply.reply_type == 1) {
1946 ret = -EINVAL;
1947 } else
1948 ret = 0;
1949 }
1950 kfree(txmsg);
1951fail_put:
1952 drm_dp_put_mst_branch_device(mstb);
1953 return ret;
1954}
1955
1956static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1957{
1958 struct drm_dp_sideband_msg_reply_body reply;
1959
1960 reply.reply_type = 0;
1961 reply.req_type = req_type;
1962 drm_dp_encode_sideband_reply(&reply, msg);
1963 return 0;
1964}
1965
1966static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1967 struct drm_dp_mst_branch *mstb,
1968 int req_type, int seqno, bool broadcast)
1969{
1970 struct drm_dp_sideband_msg_tx *txmsg;
1971
1972 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1973 if (!txmsg)
1974 return -ENOMEM;
1975
1976 txmsg->dst = mstb;
1977 txmsg->seqno = seqno;
1978 drm_dp_encode_up_ack_reply(txmsg, req_type);
1979
1980 mutex_lock(&mgr->qlock);
1981
1982 process_single_up_tx_qlock(mgr, txmsg);
1983
1984 mutex_unlock(&mgr->qlock);
1985
1986 kfree(txmsg);
1987 return 0;
1988}
1989
1990static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1991 int dp_link_count,
1992 int *out)
1993{
1994 switch (dp_link_bw) {
1995 default:
1996 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1997 dp_link_bw, dp_link_count);
1998 return false;
1999
2000 case DP_LINK_BW_1_62:
2001 *out = 3 * dp_link_count;
2002 break;
2003 case DP_LINK_BW_2_7:
2004 *out = 5 * dp_link_count;
2005 break;
2006 case DP_LINK_BW_5_4:
2007 *out = 10 * dp_link_count;
2008 break;
2009 }
2010 return true;
2011}
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2022{
2023 int ret = 0;
2024 struct drm_dp_mst_branch *mstb = NULL;
2025
2026 mutex_lock(&mgr->lock);
2027 if (mst_state == mgr->mst_state)
2028 goto out_unlock;
2029
2030 mgr->mst_state = mst_state;
2031
2032 if (mst_state) {
2033 WARN_ON(mgr->mst_primary);
2034
2035
2036 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2037 if (ret != DP_RECEIVER_CAP_SIZE) {
2038 DRM_DEBUG_KMS("failed to read DPCD\n");
2039 goto out_unlock;
2040 }
2041
2042 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2043 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2044 &mgr->pbn_div)) {
2045 ret = -EINVAL;
2046 goto out_unlock;
2047 }
2048
2049 mgr->total_pbn = 2560;
2050 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
2051 mgr->avail_slots = mgr->total_slots;
2052
2053
2054 mstb = drm_dp_add_mst_branch_device(1, NULL);
2055 if (mstb == NULL) {
2056 ret = -ENOMEM;
2057 goto out_unlock;
2058 }
2059 mstb->mgr = mgr;
2060
2061
2062 mgr->mst_primary = mstb;
2063 kref_get(&mgr->mst_primary->kref);
2064
2065 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2066 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2067 if (ret < 0) {
2068 goto out_unlock;
2069 }
2070
2071 {
2072 struct drm_dp_payload reset_pay;
2073 reset_pay.start_slot = 0;
2074 reset_pay.num_slots = 0x3f;
2075 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2076 }
2077
2078 queue_work(system_long_wq, &mgr->work);
2079
2080 ret = 0;
2081 } else {
2082
2083 mstb = mgr->mst_primary;
2084 mgr->mst_primary = NULL;
2085
2086 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2087 ret = 0;
2088 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2089 mgr->payload_mask = 0;
2090 set_bit(0, &mgr->payload_mask);
2091 mgr->vcpi_mask = 0;
2092 }
2093
2094out_unlock:
2095 mutex_unlock(&mgr->lock);
2096 if (mstb)
2097 drm_dp_put_mst_branch_device(mstb);
2098 return ret;
2099
2100}
2101EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2102
2103
2104
2105
2106
2107
2108
2109
2110void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2111{
2112 mutex_lock(&mgr->lock);
2113 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2114 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2115 mutex_unlock(&mgr->lock);
2116 flush_work(&mgr->work);
2117 flush_work(&mgr->destroy_connector_work);
2118}
2119EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2132{
2133 int ret = 0;
2134
2135 mutex_lock(&mgr->lock);
2136
2137 if (mgr->mst_primary) {
2138 int sret;
2139 u8 guid[16];
2140
2141 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2142 if (sret != DP_RECEIVER_CAP_SIZE) {
2143 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2144 ret = -1;
2145 goto out_unlock;
2146 }
2147
2148 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2149 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2150 if (ret < 0) {
2151 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2152 ret = -1;
2153 goto out_unlock;
2154 }
2155
2156
2157 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2158 if (sret != 16) {
2159 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2160 ret = -1;
2161 goto out_unlock;
2162 }
2163 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2164
2165 ret = 0;
2166 } else
2167 ret = -1;
2168
2169out_unlock:
2170 mutex_unlock(&mgr->lock);
2171 return ret;
2172}
2173EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2174
2175static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2176{
2177 int len;
2178 u8 replyblock[32];
2179 int replylen, origlen, curreply;
2180 int ret;
2181 struct drm_dp_sideband_msg_rx *msg;
2182 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2183 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2184
2185 len = min(mgr->max_dpcd_transaction_bytes, 16);
2186 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2187 replyblock, len);
2188 if (ret != len) {
2189 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2190 return;
2191 }
2192 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2193 if (!ret) {
2194 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2195 return;
2196 }
2197 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2198
2199 origlen = replylen;
2200 replylen -= len;
2201 curreply = len;
2202 while (replylen > 0) {
2203 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2204 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2205 replyblock, len);
2206 if (ret != len) {
2207 DRM_DEBUG_KMS("failed to read a chunk\n");
2208 }
2209 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2210 if (ret == false)
2211 DRM_DEBUG_KMS("failed to build sideband msg\n");
2212 curreply += len;
2213 replylen -= len;
2214 }
2215}
2216
2217static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2218{
2219 int ret = 0;
2220
2221 drm_dp_get_one_sb_msg(mgr, false);
2222
2223 if (mgr->down_rep_recv.have_eomt) {
2224 struct drm_dp_sideband_msg_tx *txmsg;
2225 struct drm_dp_mst_branch *mstb;
2226 int slot = -1;
2227 mstb = drm_dp_get_mst_branch_device(mgr,
2228 mgr->down_rep_recv.initial_hdr.lct,
2229 mgr->down_rep_recv.initial_hdr.rad);
2230
2231 if (!mstb) {
2232 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2233 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2234 return 0;
2235 }
2236
2237
2238 slot = mgr->down_rep_recv.initial_hdr.seqno;
2239 mutex_lock(&mgr->qlock);
2240 txmsg = mstb->tx_slots[slot];
2241
2242 mutex_unlock(&mgr->qlock);
2243
2244 if (!txmsg) {
2245 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2246 mstb,
2247 mgr->down_rep_recv.initial_hdr.seqno,
2248 mgr->down_rep_recv.initial_hdr.lct,
2249 mgr->down_rep_recv.initial_hdr.rad[0],
2250 mgr->down_rep_recv.msg[0]);
2251 drm_dp_put_mst_branch_device(mstb);
2252 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2253 return 0;
2254 }
2255
2256 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2257 if (txmsg->reply.reply_type == 1) {
2258 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);
2259 }
2260
2261 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2262 drm_dp_put_mst_branch_device(mstb);
2263
2264 mutex_lock(&mgr->qlock);
2265 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2266 mstb->tx_slots[slot] = NULL;
2267 mutex_unlock(&mgr->qlock);
2268
2269 wake_up(&mgr->tx_waitq);
2270 }
2271 return ret;
2272}
2273
2274static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2275{
2276 int ret = 0;
2277 drm_dp_get_one_sb_msg(mgr, true);
2278
2279 if (mgr->up_req_recv.have_eomt) {
2280 struct drm_dp_sideband_msg_req_body msg;
2281 struct drm_dp_mst_branch *mstb = NULL;
2282 bool seqno;
2283
2284 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2285 mstb = drm_dp_get_mst_branch_device(mgr,
2286 mgr->up_req_recv.initial_hdr.lct,
2287 mgr->up_req_recv.initial_hdr.rad);
2288 if (!mstb) {
2289 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2290 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2291 return 0;
2292 }
2293 }
2294
2295 seqno = mgr->up_req_recv.initial_hdr.seqno;
2296 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2297
2298 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2299 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2300
2301 if (!mstb)
2302 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2303
2304 if (!mstb) {
2305 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2306 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2307 return 0;
2308 }
2309
2310 drm_dp_update_port(mstb, &msg.u.conn_stat);
2311
2312 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);
2313 (*mgr->cbs->hotplug)(mgr);
2314
2315 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2316 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2317 if (!mstb)
2318 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2319
2320 if (!mstb) {
2321 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2322 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2323 return 0;
2324 }
2325
2326 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2327 }
2328
2329 drm_dp_put_mst_branch_device(mstb);
2330 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2331 }
2332 return ret;
2333}
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2347{
2348 int ret = 0;
2349 int sc;
2350 *handled = false;
2351 sc = esi[0] & 0x3f;
2352
2353 if (sc != mgr->sink_count) {
2354 mgr->sink_count = sc;
2355 *handled = true;
2356 }
2357
2358 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2359 ret = drm_dp_mst_handle_down_rep(mgr);
2360 *handled = true;
2361 }
2362
2363 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2364 ret |= drm_dp_mst_handle_up_req(mgr);
2365 *handled = true;
2366 }
2367
2368 drm_dp_mst_kick_tx(mgr);
2369 return ret;
2370}
2371EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2382 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2383{
2384 enum drm_connector_status status = connector_status_disconnected;
2385
2386
2387 port = drm_dp_get_validated_port_ref(mgr, port);
2388 if (!port)
2389 return connector_status_disconnected;
2390
2391 if (!port->ddps)
2392 goto out;
2393
2394 switch (port->pdt) {
2395 case DP_PEER_DEVICE_NONE:
2396 case DP_PEER_DEVICE_MST_BRANCHING:
2397 break;
2398
2399 case DP_PEER_DEVICE_SST_SINK:
2400 status = connector_status_connected;
2401
2402 if (port->port_num >= 8 && !port->cached_edid) {
2403 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2404 }
2405 break;
2406 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2407 if (port->ldps)
2408 status = connector_status_connected;
2409 break;
2410 }
2411out:
2412 drm_dp_put_port(port);
2413 return status;
2414}
2415EXPORT_SYMBOL(drm_dp_mst_detect_port);
2416
2417
2418
2419
2420
2421
2422
2423
2424bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2425 struct drm_dp_mst_port *port)
2426{
2427 bool ret = false;
2428
2429 port = drm_dp_get_validated_port_ref(mgr, port);
2430 if (!port)
2431 return ret;
2432 ret = port->has_audio;
2433 drm_dp_put_port(port);
2434 return ret;
2435}
2436EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2449{
2450 struct edid *edid = NULL;
2451
2452
2453 port = drm_dp_get_validated_port_ref(mgr, port);
2454 if (!port)
2455 return NULL;
2456
2457 if (port->cached_edid)
2458 edid = drm_edid_duplicate(port->cached_edid);
2459 else {
2460 edid = drm_get_edid(connector, &port->aux.ddc);
2461 drm_mode_connector_set_tile_property(connector);
2462 }
2463 port->has_audio = drm_detect_monitor_audio(edid);
2464 drm_dp_put_port(port);
2465 return edid;
2466}
2467EXPORT_SYMBOL(drm_dp_mst_get_edid);
2468
2469
2470
2471
2472
2473
2474int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2475 int pbn)
2476{
2477 int num_slots;
2478
2479 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2480
2481 if (num_slots > mgr->avail_slots)
2482 return -ENOSPC;
2483 return num_slots;
2484}
2485EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2486
2487static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2488 struct drm_dp_vcpi *vcpi, int pbn)
2489{
2490 int num_slots;
2491 int ret;
2492
2493 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2494
2495 if (num_slots > mgr->avail_slots)
2496 return -ENOSPC;
2497
2498 vcpi->pbn = pbn;
2499 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2500 vcpi->num_slots = num_slots;
2501
2502 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2503 if (ret < 0)
2504 return ret;
2505 return 0;
2506}
2507
2508
2509
2510
2511
2512
2513
2514
2515bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2516{
2517 int ret;
2518
2519 port = drm_dp_get_validated_port_ref(mgr, port);
2520 if (!port)
2521 return false;
2522
2523 if (port->vcpi.vcpi > 0) {
2524 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2525 if (pbn == port->vcpi.pbn) {
2526 *slots = port->vcpi.num_slots;
2527 drm_dp_put_port(port);
2528 return true;
2529 }
2530 }
2531
2532 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2533 if (ret) {
2534 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2535 goto out;
2536 }
2537 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2538 *slots = port->vcpi.num_slots;
2539
2540 drm_dp_put_port(port);
2541 return true;
2542out:
2543 return false;
2544}
2545EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2546
2547int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2548{
2549 int slots = 0;
2550 port = drm_dp_get_validated_port_ref(mgr, port);
2551 if (!port)
2552 return slots;
2553
2554 slots = port->vcpi.num_slots;
2555 drm_dp_put_port(port);
2556 return slots;
2557}
2558EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2559
2560
2561
2562
2563
2564
2565
2566
2567void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2568{
2569 port = drm_dp_get_validated_port_ref(mgr, port);
2570 if (!port)
2571 return;
2572 port->vcpi.num_slots = 0;
2573 drm_dp_put_port(port);
2574}
2575EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2576
2577
2578
2579
2580
2581
2582void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2583{
2584 port = drm_dp_get_validated_port_ref(mgr, port);
2585 if (!port)
2586 return;
2587
2588 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2589 port->vcpi.num_slots = 0;
2590 port->vcpi.pbn = 0;
2591 port->vcpi.aligned_pbn = 0;
2592 port->vcpi.vcpi = 0;
2593 drm_dp_put_port(port);
2594}
2595EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2596
2597static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2598 int id, struct drm_dp_payload *payload)
2599{
2600 u8 payload_alloc[3], status;
2601 int ret;
2602 int retries = 0;
2603
2604 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2605 DP_PAYLOAD_TABLE_UPDATED);
2606
2607 payload_alloc[0] = id;
2608 payload_alloc[1] = payload->start_slot;
2609 payload_alloc[2] = payload->num_slots;
2610
2611 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2612 if (ret != 3) {
2613 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2614 goto fail;
2615 }
2616
2617retry:
2618 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2619 if (ret < 0) {
2620 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2621 goto fail;
2622 }
2623
2624 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2625 retries++;
2626 if (retries < 20) {
2627 usleep_range(10000, 20000);
2628 goto retry;
2629 }
2630 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2631 ret = -EINVAL;
2632 goto fail;
2633 }
2634 ret = 0;
2635fail:
2636 return ret;
2637}
2638
2639
2640
2641
2642
2643
2644
2645
2646int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2647{
2648 u8 status;
2649 int ret;
2650 int count = 0;
2651
2652 do {
2653 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2654
2655 if (ret < 0) {
2656 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2657 goto fail;
2658 }
2659
2660 if (status & DP_PAYLOAD_ACT_HANDLED)
2661 break;
2662 count++;
2663 udelay(100);
2664
2665 } while (count < 30);
2666
2667 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2668 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2669 ret = -EINVAL;
2670 goto fail;
2671 }
2672 return 0;
2673fail:
2674 return ret;
2675}
2676EXPORT_SYMBOL(drm_dp_check_act_status);
2677
2678
2679
2680
2681
2682
2683
2684
2685int drm_dp_calc_pbn_mode(int clock, int bpp)
2686{
2687 u64 kbps;
2688 s64 peak_kbps;
2689 u32 numerator;
2690 u32 denominator;
2691
2692 kbps = clock * bpp;
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705 numerator = 64 * 1006;
2706 denominator = 54 * 8 * 1000 * 1000;
2707
2708 kbps *= numerator;
2709 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
2710
2711 return drm_fixp2int_ceil(peak_kbps);
2712}
2713EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2714
2715static int test_calc_pbn_mode(void)
2716{
2717 int ret;
2718 ret = drm_dp_calc_pbn_mode(154000, 30);
2719 if (ret != 689) {
2720 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2721 154000, 30, 689, ret);
2722 return -EINVAL;
2723 }
2724 ret = drm_dp_calc_pbn_mode(234000, 30);
2725 if (ret != 1047) {
2726 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2727 234000, 30, 1047, ret);
2728 return -EINVAL;
2729 }
2730 ret = drm_dp_calc_pbn_mode(297000, 24);
2731 if (ret != 1063) {
2732 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2733 297000, 24, 1063, ret);
2734 return -EINVAL;
2735 }
2736 return 0;
2737}
2738
2739
2740static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2741{
2742 queue_work(system_long_wq, &mgr->tx_work);
2743}
2744
2745static void drm_dp_mst_dump_mstb(struct seq_file *m,
2746 struct drm_dp_mst_branch *mstb)
2747{
2748 struct drm_dp_mst_port *port;
2749 int tabs = mstb->lct;
2750 char prefix[10];
2751 int i;
2752
2753 for (i = 0; i < tabs; i++)
2754 prefix[i] = '\t';
2755 prefix[i] = '\0';
2756
2757 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2758 list_for_each_entry(port, &mstb->ports, next) {
2759 seq_printf(m, "%sport: %d: ddps: %d ldps: %d, sdp: %d/%d, %p, conn: %p\n", prefix, port->port_num, port->ddps, port->ldps, port->num_sdp_streams, port->num_sdp_stream_sinks, port, port->connector);
2760 if (port->mstb)
2761 drm_dp_mst_dump_mstb(m, port->mstb);
2762 }
2763}
2764
2765static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2766 char *buf)
2767{
2768 int ret;
2769 int i;
2770 for (i = 0; i < 4; i++) {
2771 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2772 if (ret != 16)
2773 break;
2774 }
2775 if (i == 4)
2776 return true;
2777 return false;
2778}
2779
2780
2781
2782
2783
2784
2785
2786
2787void drm_dp_mst_dump_topology(struct seq_file *m,
2788 struct drm_dp_mst_topology_mgr *mgr)
2789{
2790 int i;
2791 struct drm_dp_mst_port *port;
2792 mutex_lock(&mgr->lock);
2793 if (mgr->mst_primary)
2794 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2795
2796
2797 mutex_unlock(&mgr->lock);
2798
2799 mutex_lock(&mgr->payload_lock);
2800 seq_printf(m, "vcpi: %lx %lx\n", mgr->payload_mask, mgr->vcpi_mask);
2801
2802 for (i = 0; i < mgr->max_payloads; i++) {
2803 if (mgr->proposed_vcpis[i]) {
2804 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2805 seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots);
2806 } else
2807 seq_printf(m, "vcpi %d:unsed\n", i);
2808 }
2809 for (i = 0; i < mgr->max_payloads; i++) {
2810 seq_printf(m, "payload %d: %d, %d, %d\n",
2811 i,
2812 mgr->payloads[i].payload_state,
2813 mgr->payloads[i].start_slot,
2814 mgr->payloads[i].num_slots);
2815
2816
2817 }
2818 mutex_unlock(&mgr->payload_lock);
2819
2820 mutex_lock(&mgr->lock);
2821 if (mgr->mst_primary) {
2822 u8 buf[64];
2823 bool bret;
2824 int ret;
2825 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2826 seq_printf(m, "dpcd: ");
2827 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2828 seq_printf(m, "%02x ", buf[i]);
2829 seq_printf(m, "\n");
2830 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2831 seq_printf(m, "faux/mst: ");
2832 for (i = 0; i < 2; i++)
2833 seq_printf(m, "%02x ", buf[i]);
2834 seq_printf(m, "\n");
2835 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2836 seq_printf(m, "mst ctrl: ");
2837 for (i = 0; i < 1; i++)
2838 seq_printf(m, "%02x ", buf[i]);
2839 seq_printf(m, "\n");
2840
2841
2842 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2843 seq_printf(m, "branch oui: ");
2844 for (i = 0; i < 0x3; i++)
2845 seq_printf(m, "%02x", buf[i]);
2846 seq_printf(m, " devid: ");
2847 for (i = 0x3; i < 0x8; i++)
2848 seq_printf(m, "%c", buf[i]);
2849 seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2850 seq_printf(m, "\n");
2851 bret = dump_dp_payload_table(mgr, buf);
2852 if (bret == true) {
2853 seq_printf(m, "payload table: ");
2854 for (i = 0; i < 63; i++)
2855 seq_printf(m, "%02x ", buf[i]);
2856 seq_printf(m, "\n");
2857 }
2858
2859 }
2860
2861 mutex_unlock(&mgr->lock);
2862
2863}
2864EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2865
2866static void drm_dp_tx_work(struct work_struct *work)
2867{
2868 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2869
2870 mutex_lock(&mgr->qlock);
2871 if (mgr->tx_down_in_progress)
2872 process_single_down_tx_qlock(mgr);
2873 mutex_unlock(&mgr->qlock);
2874}
2875
2876static void drm_dp_free_mst_port(struct kref *kref)
2877{
2878 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
2879 kref_put(&port->parent->kref, drm_dp_free_mst_branch_device);
2880 kfree(port);
2881}
2882
2883static void drm_dp_destroy_connector_work(struct work_struct *work)
2884{
2885 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
2886 struct drm_dp_mst_port *port;
2887 bool send_hotplug = false;
2888
2889
2890
2891
2892
2893 for (;;) {
2894 mutex_lock(&mgr->destroy_connector_lock);
2895 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2896 if (!port) {
2897 mutex_unlock(&mgr->destroy_connector_lock);
2898 break;
2899 }
2900 list_del(&port->next);
2901 mutex_unlock(&mgr->destroy_connector_lock);
2902
2903 kref_init(&port->kref);
2904 INIT_LIST_HEAD(&port->next);
2905
2906 mgr->cbs->destroy_connector(mgr, port->connector);
2907
2908 drm_dp_port_teardown_pdt(port, port->pdt);
2909
2910 if (!port->input && port->vcpi.vcpi > 0) {
2911 if (mgr->mst_state) {
2912 drm_dp_mst_reset_vcpi_slots(mgr, port);
2913 drm_dp_update_payload_part1(mgr);
2914 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2915 }
2916 }
2917
2918 kref_put(&port->kref, drm_dp_free_mst_port);
2919 send_hotplug = true;
2920 }
2921 if (send_hotplug)
2922 (*mgr->cbs->hotplug)(mgr);
2923}
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2937 struct device *dev, struct drm_dp_aux *aux,
2938 int max_dpcd_transaction_bytes,
2939 int max_payloads, int conn_base_id)
2940{
2941 mutex_init(&mgr->lock);
2942 mutex_init(&mgr->qlock);
2943 mutex_init(&mgr->payload_lock);
2944 mutex_init(&mgr->destroy_connector_lock);
2945 INIT_LIST_HEAD(&mgr->tx_msg_downq);
2946 INIT_LIST_HEAD(&mgr->destroy_connector_list);
2947 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2948 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
2949 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
2950 init_waitqueue_head(&mgr->tx_waitq);
2951 mgr->dev = dev;
2952 mgr->aux = aux;
2953 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2954 mgr->max_payloads = max_payloads;
2955 mgr->conn_base_id = conn_base_id;
2956 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
2957 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
2958 return -EINVAL;
2959 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
2960 if (!mgr->payloads)
2961 return -ENOMEM;
2962 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
2963 if (!mgr->proposed_vcpis)
2964 return -ENOMEM;
2965 set_bit(0, &mgr->payload_mask);
2966 if (test_calc_pbn_mode() < 0)
2967 DRM_ERROR("MST PBN self-test failed\n");
2968
2969 return 0;
2970}
2971EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
2972
2973
2974
2975
2976
2977void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
2978{
2979 flush_work(&mgr->work);
2980 flush_work(&mgr->destroy_connector_work);
2981 mutex_lock(&mgr->payload_lock);
2982 kfree(mgr->payloads);
2983 mgr->payloads = NULL;
2984 kfree(mgr->proposed_vcpis);
2985 mgr->proposed_vcpis = NULL;
2986 mutex_unlock(&mgr->payload_lock);
2987 mgr->dev = NULL;
2988 mgr->aux = NULL;
2989}
2990EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
2991
2992
2993static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
2994 int num)
2995{
2996 struct drm_dp_aux *aux = adapter->algo_data;
2997 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
2998 struct drm_dp_mst_branch *mstb;
2999 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3000 unsigned int i;
3001 bool reading = false;
3002 struct drm_dp_sideband_msg_req_body msg;
3003 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3004 int ret;
3005
3006 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
3007 if (!mstb)
3008 return -EREMOTEIO;
3009
3010
3011
3012 if (msgs[num - 1].flags & I2C_M_RD)
3013 reading = true;
3014
3015 if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
3016 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3017 ret = -EIO;
3018 goto out;
3019 }
3020
3021 memset(&msg, 0, sizeof(msg));
3022 msg.req_type = DP_REMOTE_I2C_READ;
3023 msg.u.i2c_read.num_transactions = num - 1;
3024 msg.u.i2c_read.port_number = port->port_num;
3025 for (i = 0; i < num - 1; i++) {
3026 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3027 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3028 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3029 }
3030 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3031 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3032
3033 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3034 if (!txmsg) {
3035 ret = -ENOMEM;
3036 goto out;
3037 }
3038
3039 txmsg->dst = mstb;
3040 drm_dp_encode_sideband_req(&msg, txmsg);
3041
3042 drm_dp_queue_down_tx(mgr, txmsg);
3043
3044 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3045 if (ret > 0) {
3046
3047 if (txmsg->reply.reply_type == 1) {
3048 ret = -EREMOTEIO;
3049 goto out;
3050 }
3051 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3052 ret = -EIO;
3053 goto out;
3054 }
3055 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3056 ret = num;
3057 }
3058out:
3059 kfree(txmsg);
3060 drm_dp_put_mst_branch_device(mstb);
3061 return ret;
3062}
3063
3064static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3065{
3066 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3067 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3068 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3069 I2C_FUNC_10BIT_ADDR;
3070}
3071
3072static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3073 .functionality = drm_dp_mst_i2c_functionality,
3074 .master_xfer = drm_dp_mst_i2c_xfer,
3075};
3076
3077
3078
3079
3080
3081
3082
3083static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3084{
3085 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3086 aux->ddc.algo_data = aux;
3087 aux->ddc.retries = 3;
3088
3089 aux->ddc.class = I2C_CLASS_DDC;
3090 aux->ddc.owner = THIS_MODULE;
3091 aux->ddc.dev.parent = aux->dev;
3092 aux->ddc.dev.of_node = aux->dev->of_node;
3093
3094 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3095 sizeof(aux->ddc.name));
3096
3097 return i2c_add_adapter(&aux->ddc);
3098}
3099
3100
3101
3102
3103
3104static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3105{
3106 i2c_del_adapter(&aux->ddc);
3107}
3108