drm/dp/mst: split connector registration into two parts (v2)
[deliverable/linux.git] / drivers / gpu / drm / drm_dp_mst_topology.c
1 /*
2 * Copyright © 2014 Red Hat
3 *
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.
13 *
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.
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 * DOC: dp mst helper
37 *
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.
41 */
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);
45
46 static void drm_dp_put_port(struct drm_dp_mst_port *port);
47
48 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
49 int id,
50 struct drm_dp_payload *payload);
51
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);
55
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);
63
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)
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
102 static 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 }
135 static 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
142 static 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
161 static 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
198 static 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
299 static 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
306 static 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 /* 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)
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 /* get length contained in this portion */
334 msg->curchunk_len = recv_hdr.msg_len;
335 msg->curchunk_hdrlen = hdrlen;
336
337 /* we have already gotten an somt - don't bother parsing */
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 /* 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;
354 }
355
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;
362 }
363 return true;
364 }
365
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)
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;
413 fail_len:
414 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
415 return false;
416 }
417
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)
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;
432 fail_len:
433 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
434 return false;
435 }
436
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)
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;
446 fail_len:
447 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
448 return false;
449 }
450
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)
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 /* 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;
468 }
469
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)
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;
487 fail_len:
488 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
489 return false;
490 }
491
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)
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;
509 fail_len:
510 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
511 return false;
512 }
513
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)
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;
527 fail_len:
528 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
529 return false;
530 }
531
532 static 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
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)
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;
589 fail_len:
590 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
591 return false;
592 }
593
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)
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;
612 fail_len:
613 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
614 return false;
615 }
616
617 static 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
634 static 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
648 static 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
657 static 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
668 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
669 u8 vcpi, uint16_t pbn)
670 {
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;
680 }
681
682 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
683 struct drm_dp_vcpi *vcpi)
684 {
685 int ret, vcpi_ret;
686
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;
693 }
694
695 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
696 if (vcpi_ret > mgr->max_payloads) {
697 ret = -EINVAL;
698 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
699 goto out_unlock;
700 }
701
702 set_bit(ret, &mgr->payload_mask);
703 set_bit(vcpi_ret, &mgr->vcpi_mask);
704 vcpi->vcpi = vcpi_ret + 1;
705 mgr->proposed_vcpis[ret - 1] = vcpi;
706 out_unlock:
707 mutex_unlock(&mgr->payload_lock);
708 return ret;
709 }
710
711 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
712 int vcpi)
713 {
714 int i;
715 if (vcpi == 0)
716 return;
717
718 mutex_lock(&mgr->payload_lock);
719 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
720 clear_bit(vcpi - 1, &mgr->vcpi_mask);
721
722 for (i = 0; i < mgr->max_payloads; i++) {
723 if (mgr->proposed_vcpis[i])
724 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
725 mgr->proposed_vcpis[i] = NULL;
726 clear_bit(i + 1, &mgr->payload_mask);
727 }
728 }
729 mutex_unlock(&mgr->payload_lock);
730 }
731
732 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
733 struct drm_dp_sideband_msg_tx *txmsg)
734 {
735 bool ret;
736
737 /*
738 * All updates to txmsg->state are protected by mgr->qlock, and the two
739 * cases we check here are terminal states. For those the barriers
740 * provided by the wake_up/wait_event pair are enough.
741 */
742 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
743 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
744 return ret;
745 }
746
747 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
748 struct drm_dp_sideband_msg_tx *txmsg)
749 {
750 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
751 int ret;
752
753 ret = wait_event_timeout(mgr->tx_waitq,
754 check_txmsg_state(mgr, txmsg),
755 (4 * HZ));
756 mutex_lock(&mstb->mgr->qlock);
757 if (ret > 0) {
758 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
759 ret = -EIO;
760 goto out;
761 }
762 } else {
763 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
764
765 /* dump some state */
766 ret = -EIO;
767
768 /* remove from q */
769 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
770 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
771 list_del(&txmsg->next);
772 }
773
774 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
775 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
776 mstb->tx_slots[txmsg->seqno] = NULL;
777 }
778 }
779 out:
780 mutex_unlock(&mgr->qlock);
781
782 return ret;
783 }
784
785 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
786 {
787 struct drm_dp_mst_branch *mstb;
788
789 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
790 if (!mstb)
791 return NULL;
792
793 mstb->lct = lct;
794 if (lct > 1)
795 memcpy(mstb->rad, rad, lct / 2);
796 INIT_LIST_HEAD(&mstb->ports);
797 kref_init(&mstb->kref);
798 return mstb;
799 }
800
801 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
802 {
803 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
804 struct drm_dp_mst_port *port, *tmp;
805 bool wake_tx = false;
806
807 cancel_work_sync(&mstb->mgr->work);
808
809 /*
810 * destroy all ports - don't need lock
811 * as there are no more references to the mst branch
812 * device at this point.
813 */
814 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
815 list_del(&port->next);
816 drm_dp_put_port(port);
817 }
818
819 /* drop any tx slots msg */
820 mutex_lock(&mstb->mgr->qlock);
821 if (mstb->tx_slots[0]) {
822 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
823 mstb->tx_slots[0] = NULL;
824 wake_tx = true;
825 }
826 if (mstb->tx_slots[1]) {
827 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
828 mstb->tx_slots[1] = NULL;
829 wake_tx = true;
830 }
831 mutex_unlock(&mstb->mgr->qlock);
832
833 if (wake_tx)
834 wake_up(&mstb->mgr->tx_waitq);
835 kfree(mstb);
836 }
837
838 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
839 {
840 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
841 }
842
843
844 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
845 {
846 struct drm_dp_mst_branch *mstb;
847
848 switch (old_pdt) {
849 case DP_PEER_DEVICE_DP_LEGACY_CONV:
850 case DP_PEER_DEVICE_SST_SINK:
851 /* remove i2c over sideband */
852 drm_dp_mst_unregister_i2c_bus(&port->aux);
853 break;
854 case DP_PEER_DEVICE_MST_BRANCHING:
855 mstb = port->mstb;
856 port->mstb = NULL;
857 drm_dp_put_mst_branch_device(mstb);
858 break;
859 }
860 }
861
862 static void drm_dp_destroy_port(struct kref *kref)
863 {
864 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
865 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
866
867 if (!port->input) {
868 port->vcpi.num_slots = 0;
869
870 kfree(port->cached_edid);
871
872 /*
873 * The only time we don't have a connector
874 * on an output port is if the connector init
875 * fails.
876 */
877 if (port->connector) {
878 /* we can't destroy the connector here, as
879 * we might be holding the mode_config.mutex
880 * from an EDID retrieval */
881
882 mutex_lock(&mgr->destroy_connector_lock);
883 list_add(&port->next, &mgr->destroy_connector_list);
884 mutex_unlock(&mgr->destroy_connector_lock);
885 schedule_work(&mgr->destroy_connector_work);
886 return;
887 }
888 /* no need to clean up vcpi
889 * as if we have no connector we never setup a vcpi */
890 drm_dp_port_teardown_pdt(port, port->pdt);
891 }
892 kfree(port);
893 }
894
895 static void drm_dp_put_port(struct drm_dp_mst_port *port)
896 {
897 kref_put(&port->kref, drm_dp_destroy_port);
898 }
899
900 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)
901 {
902 struct drm_dp_mst_port *port;
903 struct drm_dp_mst_branch *rmstb;
904 if (to_find == mstb) {
905 kref_get(&mstb->kref);
906 return mstb;
907 }
908 list_for_each_entry(port, &mstb->ports, next) {
909 if (port->mstb) {
910 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
911 if (rmstb)
912 return rmstb;
913 }
914 }
915 return NULL;
916 }
917
918 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)
919 {
920 struct drm_dp_mst_branch *rmstb = NULL;
921 mutex_lock(&mgr->lock);
922 if (mgr->mst_primary)
923 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
924 mutex_unlock(&mgr->lock);
925 return rmstb;
926 }
927
928 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)
929 {
930 struct drm_dp_mst_port *port, *mport;
931
932 list_for_each_entry(port, &mstb->ports, next) {
933 if (port == to_find) {
934 kref_get(&port->kref);
935 return port;
936 }
937 if (port->mstb) {
938 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
939 if (mport)
940 return mport;
941 }
942 }
943 return NULL;
944 }
945
946 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)
947 {
948 struct drm_dp_mst_port *rport = NULL;
949 mutex_lock(&mgr->lock);
950 if (mgr->mst_primary)
951 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
952 mutex_unlock(&mgr->lock);
953 return rport;
954 }
955
956 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
957 {
958 struct drm_dp_mst_port *port;
959
960 list_for_each_entry(port, &mstb->ports, next) {
961 if (port->port_num == port_num) {
962 kref_get(&port->kref);
963 return port;
964 }
965 }
966
967 return NULL;
968 }
969
970 /*
971 * calculate a new RAD for this MST branch device
972 * if parent has an LCT of 2 then it has 1 nibble of RAD,
973 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
974 */
975 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
976 u8 *rad)
977 {
978 int lct = port->parent->lct;
979 int shift = 4;
980 int idx = lct / 2;
981 if (lct > 1) {
982 memcpy(rad, port->parent->rad, idx);
983 shift = (lct % 2) ? 4 : 0;
984 } else
985 rad[0] = 0;
986
987 rad[idx] |= port->port_num << shift;
988 return lct + 1;
989 }
990
991 /*
992 * return sends link address for new mstb
993 */
994 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
995 {
996 int ret;
997 u8 rad[6], lct;
998 bool send_link = false;
999 switch (port->pdt) {
1000 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1001 case DP_PEER_DEVICE_SST_SINK:
1002 /* add i2c over sideband */
1003 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1004 break;
1005 case DP_PEER_DEVICE_MST_BRANCHING:
1006 lct = drm_dp_calculate_rad(port, rad);
1007
1008 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1009 port->mstb->mgr = port->mgr;
1010 port->mstb->port_parent = port;
1011
1012 send_link = true;
1013 break;
1014 }
1015 return send_link;
1016 }
1017
1018 static void drm_dp_check_port_guid(struct drm_dp_mst_branch *mstb,
1019 struct drm_dp_mst_port *port)
1020 {
1021 int ret;
1022 if (port->dpcd_rev >= 0x12) {
1023 port->guid_valid = drm_dp_validate_guid(mstb->mgr, port->guid);
1024 if (!port->guid_valid) {
1025 ret = drm_dp_send_dpcd_write(mstb->mgr,
1026 port,
1027 DP_GUID,
1028 16, port->guid);
1029 port->guid_valid = true;
1030 }
1031 }
1032 }
1033
1034 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1035 int pnum,
1036 char *proppath,
1037 size_t proppath_size)
1038 {
1039 int i;
1040 char temp[8];
1041 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1042 for (i = 0; i < (mstb->lct - 1); i++) {
1043 int shift = (i % 2) ? 0 : 4;
1044 int port_num = mstb->rad[i / 2] >> shift;
1045 snprintf(temp, sizeof(temp), "-%d", port_num);
1046 strlcat(proppath, temp, proppath_size);
1047 }
1048 snprintf(temp, sizeof(temp), "-%d", pnum);
1049 strlcat(proppath, temp, proppath_size);
1050 }
1051
1052 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1053 struct device *dev,
1054 struct drm_dp_link_addr_reply_port *port_msg)
1055 {
1056 struct drm_dp_mst_port *port;
1057 bool ret;
1058 bool created = false;
1059 int old_pdt = 0;
1060 int old_ddps = 0;
1061 port = drm_dp_get_port(mstb, port_msg->port_number);
1062 if (!port) {
1063 port = kzalloc(sizeof(*port), GFP_KERNEL);
1064 if (!port)
1065 return;
1066 kref_init(&port->kref);
1067 port->parent = mstb;
1068 port->port_num = port_msg->port_number;
1069 port->mgr = mstb->mgr;
1070 port->aux.name = "DPMST";
1071 port->aux.dev = dev;
1072 created = true;
1073 } else {
1074 old_pdt = port->pdt;
1075 old_ddps = port->ddps;
1076 }
1077
1078 port->pdt = port_msg->peer_device_type;
1079 port->input = port_msg->input_port;
1080 port->mcs = port_msg->mcs;
1081 port->ddps = port_msg->ddps;
1082 port->ldps = port_msg->legacy_device_plug_status;
1083 port->dpcd_rev = port_msg->dpcd_revision;
1084 port->num_sdp_streams = port_msg->num_sdp_streams;
1085 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1086 memcpy(port->guid, port_msg->peer_guid, 16);
1087
1088 /* manage mstb port lists with mgr lock - take a reference
1089 for this list */
1090 if (created) {
1091 mutex_lock(&mstb->mgr->lock);
1092 kref_get(&port->kref);
1093 list_add(&port->next, &mstb->ports);
1094 mutex_unlock(&mstb->mgr->lock);
1095 }
1096
1097 if (old_ddps != port->ddps) {
1098 if (port->ddps) {
1099 drm_dp_check_port_guid(mstb, port);
1100 if (!port->input)
1101 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1102 } else {
1103 port->guid_valid = false;
1104 port->available_pbn = 0;
1105 }
1106 }
1107
1108 if (old_pdt != port->pdt && !port->input) {
1109 drm_dp_port_teardown_pdt(port, old_pdt);
1110
1111 ret = drm_dp_port_setup_pdt(port);
1112 if (ret == true)
1113 drm_dp_send_link_address(mstb->mgr, port->mstb);
1114 }
1115
1116 if (created && !port->input) {
1117 char proppath[255];
1118
1119 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
1120 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1121 if (!port->connector) {
1122 /* remove it from the port list */
1123 mutex_lock(&mstb->mgr->lock);
1124 list_del(&port->next);
1125 mutex_unlock(&mstb->mgr->lock);
1126 /* drop port list reference */
1127 drm_dp_put_port(port);
1128 goto out;
1129 }
1130 if (port->port_num >= 8) {
1131 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1132 drm_mode_connector_set_tile_property(port->connector);
1133 }
1134 (*mstb->mgr->cbs->register_connector)(port->connector);
1135 }
1136
1137 out:
1138 /* put reference to this port */
1139 drm_dp_put_port(port);
1140 }
1141
1142 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1143 struct drm_dp_connection_status_notify *conn_stat)
1144 {
1145 struct drm_dp_mst_port *port;
1146 int old_pdt;
1147 int old_ddps;
1148 bool dowork = false;
1149 port = drm_dp_get_port(mstb, conn_stat->port_number);
1150 if (!port)
1151 return;
1152
1153 old_ddps = port->ddps;
1154 old_pdt = port->pdt;
1155 port->pdt = conn_stat->peer_device_type;
1156 port->mcs = conn_stat->message_capability_status;
1157 port->ldps = conn_stat->legacy_device_plug_status;
1158 port->ddps = conn_stat->displayport_device_plug_status;
1159
1160 if (old_ddps != port->ddps) {
1161 if (port->ddps) {
1162 drm_dp_check_port_guid(mstb, port);
1163 dowork = true;
1164 } else {
1165 port->guid_valid = false;
1166 port->available_pbn = 0;
1167 }
1168 }
1169 if (old_pdt != port->pdt && !port->input) {
1170 drm_dp_port_teardown_pdt(port, old_pdt);
1171
1172 if (drm_dp_port_setup_pdt(port))
1173 dowork = true;
1174 }
1175
1176 drm_dp_put_port(port);
1177 if (dowork)
1178 queue_work(system_long_wq, &mstb->mgr->work);
1179
1180 }
1181
1182 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1183 u8 lct, u8 *rad)
1184 {
1185 struct drm_dp_mst_branch *mstb;
1186 struct drm_dp_mst_port *port;
1187 int i;
1188 /* find the port by iterating down */
1189
1190 mutex_lock(&mgr->lock);
1191 mstb = mgr->mst_primary;
1192
1193 for (i = 0; i < lct - 1; i++) {
1194 int shift = (i % 2) ? 0 : 4;
1195 int port_num = rad[i / 2] >> shift;
1196
1197 list_for_each_entry(port, &mstb->ports, next) {
1198 if (port->port_num == port_num) {
1199 if (!port->mstb) {
1200 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1201 return NULL;
1202 }
1203
1204 mstb = port->mstb;
1205 break;
1206 }
1207 }
1208 }
1209 kref_get(&mstb->kref);
1210 mutex_unlock(&mgr->lock);
1211 return mstb;
1212 }
1213
1214 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1215 struct drm_dp_mst_branch *mstb)
1216 {
1217 struct drm_dp_mst_port *port;
1218 struct drm_dp_mst_branch *mstb_child;
1219 if (!mstb->link_address_sent)
1220 drm_dp_send_link_address(mgr, mstb);
1221
1222 list_for_each_entry(port, &mstb->ports, next) {
1223 if (port->input)
1224 continue;
1225
1226 if (!port->ddps)
1227 continue;
1228
1229 if (!port->available_pbn)
1230 drm_dp_send_enum_path_resources(mgr, mstb, port);
1231
1232 if (port->mstb) {
1233 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1234 if (mstb_child) {
1235 drm_dp_check_and_send_link_address(mgr, mstb_child);
1236 drm_dp_put_mst_branch_device(mstb_child);
1237 }
1238 }
1239 }
1240 }
1241
1242 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1243 {
1244 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1245 struct drm_dp_mst_branch *mstb;
1246
1247 mutex_lock(&mgr->lock);
1248 mstb = mgr->mst_primary;
1249 if (mstb) {
1250 kref_get(&mstb->kref);
1251 }
1252 mutex_unlock(&mgr->lock);
1253 if (mstb) {
1254 drm_dp_check_and_send_link_address(mgr, mstb);
1255 drm_dp_put_mst_branch_device(mstb);
1256 }
1257 }
1258
1259 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1260 u8 *guid)
1261 {
1262 static u8 zero_guid[16];
1263
1264 if (!memcmp(guid, zero_guid, 16)) {
1265 u64 salt = get_jiffies_64();
1266 memcpy(&guid[0], &salt, sizeof(u64));
1267 memcpy(&guid[8], &salt, sizeof(u64));
1268 return false;
1269 }
1270 return true;
1271 }
1272
1273 #if 0
1274 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1275 {
1276 struct drm_dp_sideband_msg_req_body req;
1277
1278 req.req_type = DP_REMOTE_DPCD_READ;
1279 req.u.dpcd_read.port_number = port_num;
1280 req.u.dpcd_read.dpcd_address = offset;
1281 req.u.dpcd_read.num_bytes = num_bytes;
1282 drm_dp_encode_sideband_req(&req, msg);
1283
1284 return 0;
1285 }
1286 #endif
1287
1288 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1289 bool up, u8 *msg, int len)
1290 {
1291 int ret;
1292 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1293 int tosend, total, offset;
1294 int retries = 0;
1295
1296 retry:
1297 total = len;
1298 offset = 0;
1299 do {
1300 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1301
1302 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1303 &msg[offset],
1304 tosend);
1305 if (ret != tosend) {
1306 if (ret == -EIO && retries < 5) {
1307 retries++;
1308 goto retry;
1309 }
1310 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1311
1312 return -EIO;
1313 }
1314 offset += tosend;
1315 total -= tosend;
1316 } while (total > 0);
1317 return 0;
1318 }
1319
1320 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1321 struct drm_dp_sideband_msg_tx *txmsg)
1322 {
1323 struct drm_dp_mst_branch *mstb = txmsg->dst;
1324
1325 /* both msg slots are full */
1326 if (txmsg->seqno == -1) {
1327 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1328 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1329 return -EAGAIN;
1330 }
1331 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1332 txmsg->seqno = mstb->last_seqno;
1333 mstb->last_seqno ^= 1;
1334 } else if (mstb->tx_slots[0] == NULL)
1335 txmsg->seqno = 0;
1336 else
1337 txmsg->seqno = 1;
1338 mstb->tx_slots[txmsg->seqno] = txmsg;
1339 }
1340 hdr->broadcast = 0;
1341 hdr->path_msg = txmsg->path_msg;
1342 hdr->lct = mstb->lct;
1343 hdr->lcr = mstb->lct - 1;
1344 if (mstb->lct > 1)
1345 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1346 hdr->seqno = txmsg->seqno;
1347 return 0;
1348 }
1349 /*
1350 * process a single block of the next message in the sideband queue
1351 */
1352 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1353 struct drm_dp_sideband_msg_tx *txmsg,
1354 bool up)
1355 {
1356 u8 chunk[48];
1357 struct drm_dp_sideband_msg_hdr hdr;
1358 int len, space, idx, tosend;
1359 int ret;
1360
1361 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1362
1363 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1364 txmsg->seqno = -1;
1365 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1366 }
1367
1368 /* make hdr from dst mst - for replies use seqno
1369 otherwise assign one */
1370 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1371 if (ret < 0)
1372 return ret;
1373
1374 /* amount left to send in this message */
1375 len = txmsg->cur_len - txmsg->cur_offset;
1376
1377 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1378 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1379
1380 tosend = min(len, space);
1381 if (len == txmsg->cur_len)
1382 hdr.somt = 1;
1383 if (space >= len)
1384 hdr.eomt = 1;
1385
1386
1387 hdr.msg_len = tosend + 1;
1388 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1389 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1390 /* add crc at end */
1391 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1392 idx += tosend + 1;
1393
1394 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1395 if (ret) {
1396 DRM_DEBUG_KMS("sideband msg failed to send\n");
1397 return ret;
1398 }
1399
1400 txmsg->cur_offset += tosend;
1401 if (txmsg->cur_offset == txmsg->cur_len) {
1402 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1403 return 1;
1404 }
1405 return 0;
1406 }
1407
1408 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1409 {
1410 struct drm_dp_sideband_msg_tx *txmsg;
1411 int ret;
1412
1413 WARN_ON(!mutex_is_locked(&mgr->qlock));
1414
1415 /* construct a chunk from the first msg in the tx_msg queue */
1416 if (list_empty(&mgr->tx_msg_downq)) {
1417 mgr->tx_down_in_progress = false;
1418 return;
1419 }
1420 mgr->tx_down_in_progress = true;
1421
1422 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1423 ret = process_single_tx_qlock(mgr, txmsg, false);
1424 if (ret == 1) {
1425 /* txmsg is sent it should be in the slots now */
1426 list_del(&txmsg->next);
1427 } else if (ret) {
1428 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1429 list_del(&txmsg->next);
1430 if (txmsg->seqno != -1)
1431 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1432 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1433 wake_up(&mgr->tx_waitq);
1434 }
1435 if (list_empty(&mgr->tx_msg_downq)) {
1436 mgr->tx_down_in_progress = false;
1437 return;
1438 }
1439 }
1440
1441 /* called holding qlock */
1442 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1443 {
1444 struct drm_dp_sideband_msg_tx *txmsg;
1445 int ret;
1446
1447 /* construct a chunk from the first msg in the tx_msg queue */
1448 if (list_empty(&mgr->tx_msg_upq)) {
1449 mgr->tx_up_in_progress = false;
1450 return;
1451 }
1452
1453 txmsg = list_first_entry(&mgr->tx_msg_upq, struct drm_dp_sideband_msg_tx, next);
1454 ret = process_single_tx_qlock(mgr, txmsg, true);
1455 if (ret == 1) {
1456 /* up txmsgs aren't put in slots - so free after we send it */
1457 list_del(&txmsg->next);
1458 kfree(txmsg);
1459 } else if (ret)
1460 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1461 mgr->tx_up_in_progress = true;
1462 }
1463
1464 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1465 struct drm_dp_sideband_msg_tx *txmsg)
1466 {
1467 mutex_lock(&mgr->qlock);
1468 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1469 if (!mgr->tx_down_in_progress)
1470 process_single_down_tx_qlock(mgr);
1471 mutex_unlock(&mgr->qlock);
1472 }
1473
1474 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1475 struct drm_dp_mst_branch *mstb)
1476 {
1477 int len;
1478 struct drm_dp_sideband_msg_tx *txmsg;
1479 int ret;
1480
1481 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1482 if (!txmsg)
1483 return;
1484
1485 txmsg->dst = mstb;
1486 len = build_link_address(txmsg);
1487
1488 mstb->link_address_sent = true;
1489 drm_dp_queue_down_tx(mgr, txmsg);
1490
1491 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1492 if (ret > 0) {
1493 int i;
1494
1495 if (txmsg->reply.reply_type == 1)
1496 DRM_DEBUG_KMS("link address nak received\n");
1497 else {
1498 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1499 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1500 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,
1501 txmsg->reply.u.link_addr.ports[i].input_port,
1502 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1503 txmsg->reply.u.link_addr.ports[i].port_number,
1504 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1505 txmsg->reply.u.link_addr.ports[i].mcs,
1506 txmsg->reply.u.link_addr.ports[i].ddps,
1507 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1508 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1509 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1510 }
1511 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1512 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1513 }
1514 (*mgr->cbs->hotplug)(mgr);
1515 }
1516 } else {
1517 mstb->link_address_sent = false;
1518 DRM_DEBUG_KMS("link address failed %d\n", ret);
1519 }
1520
1521 kfree(txmsg);
1522 }
1523
1524 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1525 struct drm_dp_mst_branch *mstb,
1526 struct drm_dp_mst_port *port)
1527 {
1528 int len;
1529 struct drm_dp_sideband_msg_tx *txmsg;
1530 int ret;
1531
1532 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1533 if (!txmsg)
1534 return -ENOMEM;
1535
1536 txmsg->dst = mstb;
1537 len = build_enum_path_resources(txmsg, port->port_num);
1538
1539 drm_dp_queue_down_tx(mgr, txmsg);
1540
1541 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1542 if (ret > 0) {
1543 if (txmsg->reply.reply_type == 1)
1544 DRM_DEBUG_KMS("enum path resources nak received\n");
1545 else {
1546 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1547 DRM_ERROR("got incorrect port in response\n");
1548 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,
1549 txmsg->reply.u.path_resources.avail_payload_bw_number);
1550 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1551 }
1552 }
1553
1554 kfree(txmsg);
1555 return 0;
1556 }
1557
1558 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1559 struct drm_dp_mst_port *port,
1560 int id,
1561 int pbn)
1562 {
1563 struct drm_dp_sideband_msg_tx *txmsg;
1564 struct drm_dp_mst_branch *mstb;
1565 int len, ret;
1566
1567 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1568 if (!mstb)
1569 return -EINVAL;
1570
1571 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1572 if (!txmsg) {
1573 ret = -ENOMEM;
1574 goto fail_put;
1575 }
1576
1577 txmsg->dst = mstb;
1578 len = build_allocate_payload(txmsg, port->port_num,
1579 id,
1580 pbn);
1581
1582 drm_dp_queue_down_tx(mgr, txmsg);
1583
1584 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1585 if (ret > 0) {
1586 if (txmsg->reply.reply_type == 1) {
1587 ret = -EINVAL;
1588 } else
1589 ret = 0;
1590 }
1591 kfree(txmsg);
1592 fail_put:
1593 drm_dp_put_mst_branch_device(mstb);
1594 return ret;
1595 }
1596
1597 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1598 int id,
1599 struct drm_dp_payload *payload)
1600 {
1601 int ret;
1602
1603 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1604 if (ret < 0) {
1605 payload->payload_state = 0;
1606 return ret;
1607 }
1608 payload->payload_state = DP_PAYLOAD_LOCAL;
1609 return 0;
1610 }
1611
1612 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1613 struct drm_dp_mst_port *port,
1614 int id,
1615 struct drm_dp_payload *payload)
1616 {
1617 int ret;
1618 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1619 if (ret < 0)
1620 return ret;
1621 payload->payload_state = DP_PAYLOAD_REMOTE;
1622 return ret;
1623 }
1624
1625 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1626 struct drm_dp_mst_port *port,
1627 int id,
1628 struct drm_dp_payload *payload)
1629 {
1630 DRM_DEBUG_KMS("\n");
1631 /* its okay for these to fail */
1632 if (port) {
1633 drm_dp_payload_send_msg(mgr, port, id, 0);
1634 }
1635
1636 drm_dp_dpcd_write_payload(mgr, id, payload);
1637 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
1638 return 0;
1639 }
1640
1641 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1642 int id,
1643 struct drm_dp_payload *payload)
1644 {
1645 payload->payload_state = 0;
1646 return 0;
1647 }
1648
1649 /**
1650 * drm_dp_update_payload_part1() - Execute payload update part 1
1651 * @mgr: manager to use.
1652 *
1653 * This iterates over all proposed virtual channels, and tries to
1654 * allocate space in the link for them. For 0->slots transitions,
1655 * this step just writes the VCPI to the MST device. For slots->0
1656 * transitions, this writes the updated VCPIs and removes the
1657 * remote VC payloads.
1658 *
1659 * after calling this the driver should generate ACT and payload
1660 * packets.
1661 */
1662 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1663 {
1664 int i, j;
1665 int cur_slots = 1;
1666 struct drm_dp_payload req_payload;
1667 struct drm_dp_mst_port *port;
1668
1669 mutex_lock(&mgr->payload_lock);
1670 for (i = 0; i < mgr->max_payloads; i++) {
1671 /* solve the current payloads - compare to the hw ones
1672 - update the hw view */
1673 req_payload.start_slot = cur_slots;
1674 if (mgr->proposed_vcpis[i]) {
1675 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1676 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1677 } else {
1678 port = NULL;
1679 req_payload.num_slots = 0;
1680 }
1681
1682 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1683 mgr->payloads[i].start_slot = req_payload.start_slot;
1684 }
1685 /* work out what is required to happen with this payload */
1686 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
1687
1688 /* need to push an update for this payload */
1689 if (req_payload.num_slots) {
1690 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
1691 mgr->payloads[i].num_slots = req_payload.num_slots;
1692 } else if (mgr->payloads[i].num_slots) {
1693 mgr->payloads[i].num_slots = 0;
1694 drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]);
1695 req_payload.payload_state = mgr->payloads[i].payload_state;
1696 mgr->payloads[i].start_slot = 0;
1697 }
1698 mgr->payloads[i].payload_state = req_payload.payload_state;
1699 }
1700 cur_slots += req_payload.num_slots;
1701 }
1702
1703 for (i = 0; i < mgr->max_payloads; i++) {
1704 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1705 DRM_DEBUG_KMS("removing payload %d\n", i);
1706 for (j = i; j < mgr->max_payloads - 1; j++) {
1707 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1708 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1709 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1710 set_bit(j + 1, &mgr->payload_mask);
1711 } else {
1712 clear_bit(j + 1, &mgr->payload_mask);
1713 }
1714 }
1715 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1716 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1717 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1718
1719 }
1720 }
1721 mutex_unlock(&mgr->payload_lock);
1722
1723 return 0;
1724 }
1725 EXPORT_SYMBOL(drm_dp_update_payload_part1);
1726
1727 /**
1728 * drm_dp_update_payload_part2() - Execute payload update part 2
1729 * @mgr: manager to use.
1730 *
1731 * This iterates over all proposed virtual channels, and tries to
1732 * allocate space in the link for them. For 0->slots transitions,
1733 * this step writes the remote VC payload commands. For slots->0
1734 * this just resets some internal state.
1735 */
1736 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1737 {
1738 struct drm_dp_mst_port *port;
1739 int i;
1740 int ret = 0;
1741 mutex_lock(&mgr->payload_lock);
1742 for (i = 0; i < mgr->max_payloads; i++) {
1743
1744 if (!mgr->proposed_vcpis[i])
1745 continue;
1746
1747 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1748
1749 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1750 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1751 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1752 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1753 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1754 }
1755 if (ret) {
1756 mutex_unlock(&mgr->payload_lock);
1757 return ret;
1758 }
1759 }
1760 mutex_unlock(&mgr->payload_lock);
1761 return 0;
1762 }
1763 EXPORT_SYMBOL(drm_dp_update_payload_part2);
1764
1765 #if 0 /* unused as of yet */
1766 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1767 struct drm_dp_mst_port *port,
1768 int offset, int size)
1769 {
1770 int len;
1771 struct drm_dp_sideband_msg_tx *txmsg;
1772
1773 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1774 if (!txmsg)
1775 return -ENOMEM;
1776
1777 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1778 txmsg->dst = port->parent;
1779
1780 drm_dp_queue_down_tx(mgr, txmsg);
1781
1782 return 0;
1783 }
1784 #endif
1785
1786 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1787 struct drm_dp_mst_port *port,
1788 int offset, int size, u8 *bytes)
1789 {
1790 int len;
1791 int ret;
1792 struct drm_dp_sideband_msg_tx *txmsg;
1793 struct drm_dp_mst_branch *mstb;
1794
1795 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1796 if (!mstb)
1797 return -EINVAL;
1798
1799 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1800 if (!txmsg) {
1801 ret = -ENOMEM;
1802 goto fail_put;
1803 }
1804
1805 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1806 txmsg->dst = mstb;
1807
1808 drm_dp_queue_down_tx(mgr, txmsg);
1809
1810 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1811 if (ret > 0) {
1812 if (txmsg->reply.reply_type == 1) {
1813 ret = -EINVAL;
1814 } else
1815 ret = 0;
1816 }
1817 kfree(txmsg);
1818 fail_put:
1819 drm_dp_put_mst_branch_device(mstb);
1820 return ret;
1821 }
1822
1823 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1824 {
1825 struct drm_dp_sideband_msg_reply_body reply;
1826
1827 reply.reply_type = 1;
1828 reply.req_type = req_type;
1829 drm_dp_encode_sideband_reply(&reply, msg);
1830 return 0;
1831 }
1832
1833 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1834 struct drm_dp_mst_branch *mstb,
1835 int req_type, int seqno, bool broadcast)
1836 {
1837 struct drm_dp_sideband_msg_tx *txmsg;
1838
1839 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1840 if (!txmsg)
1841 return -ENOMEM;
1842
1843 txmsg->dst = mstb;
1844 txmsg->seqno = seqno;
1845 drm_dp_encode_up_ack_reply(txmsg, req_type);
1846
1847 mutex_lock(&mgr->qlock);
1848 list_add_tail(&txmsg->next, &mgr->tx_msg_upq);
1849 if (!mgr->tx_up_in_progress) {
1850 process_single_up_tx_qlock(mgr);
1851 }
1852 mutex_unlock(&mgr->qlock);
1853 return 0;
1854 }
1855
1856 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1857 int dp_link_count,
1858 int *out)
1859 {
1860 switch (dp_link_bw) {
1861 default:
1862 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1863 dp_link_bw, dp_link_count);
1864 return false;
1865
1866 case DP_LINK_BW_1_62:
1867 *out = 3 * dp_link_count;
1868 break;
1869 case DP_LINK_BW_2_7:
1870 *out = 5 * dp_link_count;
1871 break;
1872 case DP_LINK_BW_5_4:
1873 *out = 10 * dp_link_count;
1874 break;
1875 }
1876 return true;
1877 }
1878
1879 /**
1880 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
1881 * @mgr: manager to set state for
1882 * @mst_state: true to enable MST on this connector - false to disable.
1883 *
1884 * This is called by the driver when it detects an MST capable device plugged
1885 * into a DP MST capable port, or when a DP MST capable device is unplugged.
1886 */
1887 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
1888 {
1889 int ret = 0;
1890 struct drm_dp_mst_branch *mstb = NULL;
1891
1892 mutex_lock(&mgr->lock);
1893 if (mst_state == mgr->mst_state)
1894 goto out_unlock;
1895
1896 mgr->mst_state = mst_state;
1897 /* set the device into MST mode */
1898 if (mst_state) {
1899 WARN_ON(mgr->mst_primary);
1900
1901 /* get dpcd info */
1902 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
1903 if (ret != DP_RECEIVER_CAP_SIZE) {
1904 DRM_DEBUG_KMS("failed to read DPCD\n");
1905 goto out_unlock;
1906 }
1907
1908 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
1909 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
1910 &mgr->pbn_div)) {
1911 ret = -EINVAL;
1912 goto out_unlock;
1913 }
1914
1915 mgr->total_pbn = 2560;
1916 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
1917 mgr->avail_slots = mgr->total_slots;
1918
1919 /* add initial branch device at LCT 1 */
1920 mstb = drm_dp_add_mst_branch_device(1, NULL);
1921 if (mstb == NULL) {
1922 ret = -ENOMEM;
1923 goto out_unlock;
1924 }
1925 mstb->mgr = mgr;
1926
1927 /* give this the main reference */
1928 mgr->mst_primary = mstb;
1929 kref_get(&mgr->mst_primary->kref);
1930
1931 {
1932 struct drm_dp_payload reset_pay;
1933 reset_pay.start_slot = 0;
1934 reset_pay.num_slots = 0x3f;
1935 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
1936 }
1937
1938 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1939 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
1940 if (ret < 0) {
1941 goto out_unlock;
1942 }
1943
1944
1945 /* sort out guid */
1946 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, mgr->guid, 16);
1947 if (ret != 16) {
1948 DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret);
1949 goto out_unlock;
1950 }
1951
1952 mgr->guid_valid = drm_dp_validate_guid(mgr, mgr->guid);
1953 if (!mgr->guid_valid) {
1954 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, mgr->guid, 16);
1955 mgr->guid_valid = true;
1956 }
1957
1958 queue_work(system_long_wq, &mgr->work);
1959
1960 ret = 0;
1961 } else {
1962 /* disable MST on the device */
1963 mstb = mgr->mst_primary;
1964 mgr->mst_primary = NULL;
1965 /* this can fail if the device is gone */
1966 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
1967 ret = 0;
1968 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
1969 mgr->payload_mask = 0;
1970 set_bit(0, &mgr->payload_mask);
1971 mgr->vcpi_mask = 0;
1972 }
1973
1974 out_unlock:
1975 mutex_unlock(&mgr->lock);
1976 if (mstb)
1977 drm_dp_put_mst_branch_device(mstb);
1978 return ret;
1979
1980 }
1981 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
1982
1983 /**
1984 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
1985 * @mgr: manager to suspend
1986 *
1987 * This function tells the MST device that we can't handle UP messages
1988 * anymore. This should stop it from sending any since we are suspended.
1989 */
1990 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
1991 {
1992 mutex_lock(&mgr->lock);
1993 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
1994 DP_MST_EN | DP_UPSTREAM_IS_SRC);
1995 mutex_unlock(&mgr->lock);
1996 }
1997 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
1998
1999 /**
2000 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2001 * @mgr: manager to resume
2002 *
2003 * This will fetch DPCD and see if the device is still there,
2004 * if it is, it will rewrite the MSTM control bits, and return.
2005 *
2006 * if the device fails this returns -1, and the driver should do
2007 * a full MST reprobe, in case we were undocked.
2008 */
2009 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2010 {
2011 int ret = 0;
2012
2013 mutex_lock(&mgr->lock);
2014
2015 if (mgr->mst_primary) {
2016 int sret;
2017 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2018 if (sret != DP_RECEIVER_CAP_SIZE) {
2019 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2020 ret = -1;
2021 goto out_unlock;
2022 }
2023
2024 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2025 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2026 if (ret < 0) {
2027 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2028 ret = -1;
2029 goto out_unlock;
2030 }
2031 ret = 0;
2032 } else
2033 ret = -1;
2034
2035 out_unlock:
2036 mutex_unlock(&mgr->lock);
2037 return ret;
2038 }
2039 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2040
2041 static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2042 {
2043 int len;
2044 u8 replyblock[32];
2045 int replylen, origlen, curreply;
2046 int ret;
2047 struct drm_dp_sideband_msg_rx *msg;
2048 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2049 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2050
2051 len = min(mgr->max_dpcd_transaction_bytes, 16);
2052 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2053 replyblock, len);
2054 if (ret != len) {
2055 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2056 return;
2057 }
2058 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2059 if (!ret) {
2060 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2061 return;
2062 }
2063 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2064
2065 origlen = replylen;
2066 replylen -= len;
2067 curreply = len;
2068 while (replylen > 0) {
2069 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2070 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2071 replyblock, len);
2072 if (ret != len) {
2073 DRM_DEBUG_KMS("failed to read a chunk\n");
2074 }
2075 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2076 if (ret == false)
2077 DRM_DEBUG_KMS("failed to build sideband msg\n");
2078 curreply += len;
2079 replylen -= len;
2080 }
2081 }
2082
2083 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2084 {
2085 int ret = 0;
2086
2087 drm_dp_get_one_sb_msg(mgr, false);
2088
2089 if (mgr->down_rep_recv.have_eomt) {
2090 struct drm_dp_sideband_msg_tx *txmsg;
2091 struct drm_dp_mst_branch *mstb;
2092 int slot = -1;
2093 mstb = drm_dp_get_mst_branch_device(mgr,
2094 mgr->down_rep_recv.initial_hdr.lct,
2095 mgr->down_rep_recv.initial_hdr.rad);
2096
2097 if (!mstb) {
2098 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2099 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2100 return 0;
2101 }
2102
2103 /* find the message */
2104 slot = mgr->down_rep_recv.initial_hdr.seqno;
2105 mutex_lock(&mgr->qlock);
2106 txmsg = mstb->tx_slots[slot];
2107 /* remove from slots */
2108 mutex_unlock(&mgr->qlock);
2109
2110 if (!txmsg) {
2111 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2112 mstb,
2113 mgr->down_rep_recv.initial_hdr.seqno,
2114 mgr->down_rep_recv.initial_hdr.lct,
2115 mgr->down_rep_recv.initial_hdr.rad[0],
2116 mgr->down_rep_recv.msg[0]);
2117 drm_dp_put_mst_branch_device(mstb);
2118 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2119 return 0;
2120 }
2121
2122 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2123 if (txmsg->reply.reply_type == 1) {
2124 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);
2125 }
2126
2127 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2128 drm_dp_put_mst_branch_device(mstb);
2129
2130 mutex_lock(&mgr->qlock);
2131 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2132 mstb->tx_slots[slot] = NULL;
2133 mutex_unlock(&mgr->qlock);
2134
2135 wake_up(&mgr->tx_waitq);
2136 }
2137 return ret;
2138 }
2139
2140 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2141 {
2142 int ret = 0;
2143 drm_dp_get_one_sb_msg(mgr, true);
2144
2145 if (mgr->up_req_recv.have_eomt) {
2146 struct drm_dp_sideband_msg_req_body msg;
2147 struct drm_dp_mst_branch *mstb;
2148 bool seqno;
2149 mstb = drm_dp_get_mst_branch_device(mgr,
2150 mgr->up_req_recv.initial_hdr.lct,
2151 mgr->up_req_recv.initial_hdr.rad);
2152 if (!mstb) {
2153 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2154 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2155 return 0;
2156 }
2157
2158 seqno = mgr->up_req_recv.initial_hdr.seqno;
2159 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2160
2161 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2162 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
2163 drm_dp_update_port(mstb, &msg.u.conn_stat);
2164 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);
2165 (*mgr->cbs->hotplug)(mgr);
2166
2167 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2168 drm_dp_send_up_ack_reply(mgr, mstb, msg.req_type, seqno, false);
2169 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2170 }
2171
2172 drm_dp_put_mst_branch_device(mstb);
2173 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2174 }
2175 return ret;
2176 }
2177
2178 /**
2179 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2180 * @mgr: manager to notify irq for.
2181 * @esi: 4 bytes from SINK_COUNT_ESI
2182 * @handled: whether the hpd interrupt was consumed or not
2183 *
2184 * This should be called from the driver when it detects a short IRQ,
2185 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2186 * topology manager will process the sideband messages received as a result
2187 * of this.
2188 */
2189 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2190 {
2191 int ret = 0;
2192 int sc;
2193 *handled = false;
2194 sc = esi[0] & 0x3f;
2195
2196 if (sc != mgr->sink_count) {
2197 mgr->sink_count = sc;
2198 *handled = true;
2199 }
2200
2201 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2202 ret = drm_dp_mst_handle_down_rep(mgr);
2203 *handled = true;
2204 }
2205
2206 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2207 ret |= drm_dp_mst_handle_up_req(mgr);
2208 *handled = true;
2209 }
2210
2211 drm_dp_mst_kick_tx(mgr);
2212 return ret;
2213 }
2214 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2215
2216 /**
2217 * drm_dp_mst_detect_port() - get connection status for an MST port
2218 * @mgr: manager for this port
2219 * @port: unverified pointer to a port
2220 *
2221 * This returns the current connection state for a port. It validates the
2222 * port pointer still exists so the caller doesn't require a reference
2223 */
2224 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2225 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2226 {
2227 enum drm_connector_status status = connector_status_disconnected;
2228
2229 /* we need to search for the port in the mgr in case its gone */
2230 port = drm_dp_get_validated_port_ref(mgr, port);
2231 if (!port)
2232 return connector_status_disconnected;
2233
2234 if (!port->ddps)
2235 goto out;
2236
2237 switch (port->pdt) {
2238 case DP_PEER_DEVICE_NONE:
2239 case DP_PEER_DEVICE_MST_BRANCHING:
2240 break;
2241
2242 case DP_PEER_DEVICE_SST_SINK:
2243 status = connector_status_connected;
2244 /* for logical ports - cache the EDID */
2245 if (port->port_num >= 8 && !port->cached_edid) {
2246 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2247 }
2248 break;
2249 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2250 if (port->ldps)
2251 status = connector_status_connected;
2252 break;
2253 }
2254 out:
2255 drm_dp_put_port(port);
2256 return status;
2257 }
2258 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2259
2260 /**
2261 * drm_dp_mst_get_edid() - get EDID for an MST port
2262 * @connector: toplevel connector to get EDID for
2263 * @mgr: manager for this port
2264 * @port: unverified pointer to a port.
2265 *
2266 * This returns an EDID for the port connected to a connector,
2267 * It validates the pointer still exists so the caller doesn't require a
2268 * reference.
2269 */
2270 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2271 {
2272 struct edid *edid = NULL;
2273
2274 /* we need to search for the port in the mgr in case its gone */
2275 port = drm_dp_get_validated_port_ref(mgr, port);
2276 if (!port)
2277 return NULL;
2278
2279 if (port->cached_edid)
2280 edid = drm_edid_duplicate(port->cached_edid);
2281 else {
2282 edid = drm_get_edid(connector, &port->aux.ddc);
2283 drm_mode_connector_set_tile_property(connector);
2284 }
2285 drm_dp_put_port(port);
2286 return edid;
2287 }
2288 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2289
2290 /**
2291 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2292 * @mgr: manager to use
2293 * @pbn: payload bandwidth to convert into slots.
2294 */
2295 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2296 int pbn)
2297 {
2298 int num_slots;
2299
2300 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2301
2302 if (num_slots > mgr->avail_slots)
2303 return -ENOSPC;
2304 return num_slots;
2305 }
2306 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2307
2308 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2309 struct drm_dp_vcpi *vcpi, int pbn)
2310 {
2311 int num_slots;
2312 int ret;
2313
2314 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2315
2316 if (num_slots > mgr->avail_slots)
2317 return -ENOSPC;
2318
2319 vcpi->pbn = pbn;
2320 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2321 vcpi->num_slots = num_slots;
2322
2323 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2324 if (ret < 0)
2325 return ret;
2326 return 0;
2327 }
2328
2329 /**
2330 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2331 * @mgr: manager for this port
2332 * @port: port to allocate a virtual channel for.
2333 * @pbn: payload bandwidth number to request
2334 * @slots: returned number of slots for this PBN.
2335 */
2336 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2337 {
2338 int ret;
2339
2340 port = drm_dp_get_validated_port_ref(mgr, port);
2341 if (!port)
2342 return false;
2343
2344 if (port->vcpi.vcpi > 0) {
2345 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2346 if (pbn == port->vcpi.pbn) {
2347 *slots = port->vcpi.num_slots;
2348 return true;
2349 }
2350 }
2351
2352 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2353 if (ret) {
2354 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2355 goto out;
2356 }
2357 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2358 *slots = port->vcpi.num_slots;
2359
2360 drm_dp_put_port(port);
2361 return true;
2362 out:
2363 return false;
2364 }
2365 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2366
2367 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2368 {
2369 int slots = 0;
2370 port = drm_dp_get_validated_port_ref(mgr, port);
2371 if (!port)
2372 return slots;
2373
2374 slots = port->vcpi.num_slots;
2375 drm_dp_put_port(port);
2376 return slots;
2377 }
2378 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2379
2380 /**
2381 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2382 * @mgr: manager for this port
2383 * @port: unverified pointer to a port.
2384 *
2385 * This just resets the number of slots for the ports VCPI for later programming.
2386 */
2387 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2388 {
2389 port = drm_dp_get_validated_port_ref(mgr, port);
2390 if (!port)
2391 return;
2392 port->vcpi.num_slots = 0;
2393 drm_dp_put_port(port);
2394 }
2395 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2396
2397 /**
2398 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2399 * @mgr: manager for this port
2400 * @port: unverified port to deallocate vcpi for
2401 */
2402 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2403 {
2404 port = drm_dp_get_validated_port_ref(mgr, port);
2405 if (!port)
2406 return;
2407
2408 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2409 port->vcpi.num_slots = 0;
2410 port->vcpi.pbn = 0;
2411 port->vcpi.aligned_pbn = 0;
2412 port->vcpi.vcpi = 0;
2413 drm_dp_put_port(port);
2414 }
2415 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2416
2417 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2418 int id, struct drm_dp_payload *payload)
2419 {
2420 u8 payload_alloc[3], status;
2421 int ret;
2422 int retries = 0;
2423
2424 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2425 DP_PAYLOAD_TABLE_UPDATED);
2426
2427 payload_alloc[0] = id;
2428 payload_alloc[1] = payload->start_slot;
2429 payload_alloc[2] = payload->num_slots;
2430
2431 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2432 if (ret != 3) {
2433 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2434 goto fail;
2435 }
2436
2437 retry:
2438 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2439 if (ret < 0) {
2440 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2441 goto fail;
2442 }
2443
2444 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2445 retries++;
2446 if (retries < 20) {
2447 usleep_range(10000, 20000);
2448 goto retry;
2449 }
2450 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2451 ret = -EINVAL;
2452 goto fail;
2453 }
2454 ret = 0;
2455 fail:
2456 return ret;
2457 }
2458
2459
2460 /**
2461 * drm_dp_check_act_status() - Check ACT handled status.
2462 * @mgr: manager to use
2463 *
2464 * Check the payload status bits in the DPCD for ACT handled completion.
2465 */
2466 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2467 {
2468 u8 status;
2469 int ret;
2470 int count = 0;
2471
2472 do {
2473 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2474
2475 if (ret < 0) {
2476 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2477 goto fail;
2478 }
2479
2480 if (status & DP_PAYLOAD_ACT_HANDLED)
2481 break;
2482 count++;
2483 udelay(100);
2484
2485 } while (count < 30);
2486
2487 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2488 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2489 ret = -EINVAL;
2490 goto fail;
2491 }
2492 return 0;
2493 fail:
2494 return ret;
2495 }
2496 EXPORT_SYMBOL(drm_dp_check_act_status);
2497
2498 /**
2499 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2500 * @clock: dot clock for the mode
2501 * @bpp: bpp for the mode.
2502 *
2503 * This uses the formula in the spec to calculate the PBN value for a mode.
2504 */
2505 int drm_dp_calc_pbn_mode(int clock, int bpp)
2506 {
2507 fixed20_12 pix_bw;
2508 fixed20_12 fbpp;
2509 fixed20_12 result;
2510 fixed20_12 margin, tmp;
2511 u32 res;
2512
2513 pix_bw.full = dfixed_const(clock);
2514 fbpp.full = dfixed_const(bpp);
2515 tmp.full = dfixed_const(8);
2516 fbpp.full = dfixed_div(fbpp, tmp);
2517
2518 result.full = dfixed_mul(pix_bw, fbpp);
2519 margin.full = dfixed_const(54);
2520 tmp.full = dfixed_const(64);
2521 margin.full = dfixed_div(margin, tmp);
2522 result.full = dfixed_div(result, margin);
2523
2524 margin.full = dfixed_const(1006);
2525 tmp.full = dfixed_const(1000);
2526 margin.full = dfixed_div(margin, tmp);
2527 result.full = dfixed_mul(result, margin);
2528
2529 result.full = dfixed_div(result, tmp);
2530 result.full = dfixed_ceil(result);
2531 res = dfixed_trunc(result);
2532 return res;
2533 }
2534 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2535
2536 static int test_calc_pbn_mode(void)
2537 {
2538 int ret;
2539 ret = drm_dp_calc_pbn_mode(154000, 30);
2540 if (ret != 689)
2541 return -EINVAL;
2542 ret = drm_dp_calc_pbn_mode(234000, 30);
2543 if (ret != 1047)
2544 return -EINVAL;
2545 return 0;
2546 }
2547
2548 /* we want to kick the TX after we've ack the up/down IRQs. */
2549 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2550 {
2551 queue_work(system_long_wq, &mgr->tx_work);
2552 }
2553
2554 static void drm_dp_mst_dump_mstb(struct seq_file *m,
2555 struct drm_dp_mst_branch *mstb)
2556 {
2557 struct drm_dp_mst_port *port;
2558 int tabs = mstb->lct;
2559 char prefix[10];
2560 int i;
2561
2562 for (i = 0; i < tabs; i++)
2563 prefix[i] = '\t';
2564 prefix[i] = '\0';
2565
2566 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2567 list_for_each_entry(port, &mstb->ports, next) {
2568 seq_printf(m, "%sport: %d: ddps: %d ldps: %d, %p, conn: %p\n", prefix, port->port_num, port->ddps, port->ldps, port, port->connector);
2569 if (port->mstb)
2570 drm_dp_mst_dump_mstb(m, port->mstb);
2571 }
2572 }
2573
2574 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2575 char *buf)
2576 {
2577 int ret;
2578 int i;
2579 for (i = 0; i < 4; i++) {
2580 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2581 if (ret != 16)
2582 break;
2583 }
2584 if (i == 4)
2585 return true;
2586 return false;
2587 }
2588
2589 /**
2590 * drm_dp_mst_dump_topology(): dump topology to seq file.
2591 * @m: seq_file to dump output to
2592 * @mgr: manager to dump current topology for.
2593 *
2594 * helper to dump MST topology to a seq file for debugfs.
2595 */
2596 void drm_dp_mst_dump_topology(struct seq_file *m,
2597 struct drm_dp_mst_topology_mgr *mgr)
2598 {
2599 int i;
2600 struct drm_dp_mst_port *port;
2601 mutex_lock(&mgr->lock);
2602 if (mgr->mst_primary)
2603 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2604
2605 /* dump VCPIs */
2606 mutex_unlock(&mgr->lock);
2607
2608 mutex_lock(&mgr->payload_lock);
2609 seq_printf(m, "vcpi: %lx %lx\n", mgr->payload_mask, mgr->vcpi_mask);
2610
2611 for (i = 0; i < mgr->max_payloads; i++) {
2612 if (mgr->proposed_vcpis[i]) {
2613 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2614 seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots);
2615 } else
2616 seq_printf(m, "vcpi %d:unsed\n", i);
2617 }
2618 for (i = 0; i < mgr->max_payloads; i++) {
2619 seq_printf(m, "payload %d: %d, %d, %d\n",
2620 i,
2621 mgr->payloads[i].payload_state,
2622 mgr->payloads[i].start_slot,
2623 mgr->payloads[i].num_slots);
2624
2625
2626 }
2627 mutex_unlock(&mgr->payload_lock);
2628
2629 mutex_lock(&mgr->lock);
2630 if (mgr->mst_primary) {
2631 u8 buf[64];
2632 bool bret;
2633 int ret;
2634 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2635 seq_printf(m, "dpcd: ");
2636 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2637 seq_printf(m, "%02x ", buf[i]);
2638 seq_printf(m, "\n");
2639 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2640 seq_printf(m, "faux/mst: ");
2641 for (i = 0; i < 2; i++)
2642 seq_printf(m, "%02x ", buf[i]);
2643 seq_printf(m, "\n");
2644 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2645 seq_printf(m, "mst ctrl: ");
2646 for (i = 0; i < 1; i++)
2647 seq_printf(m, "%02x ", buf[i]);
2648 seq_printf(m, "\n");
2649
2650 /* dump the standard OUI branch header */
2651 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2652 seq_printf(m, "branch oui: ");
2653 for (i = 0; i < 0x3; i++)
2654 seq_printf(m, "%02x", buf[i]);
2655 seq_printf(m, " devid: ");
2656 for (i = 0x3; i < 0x8; i++)
2657 seq_printf(m, "%c", buf[i]);
2658 seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2659 seq_printf(m, "\n");
2660 bret = dump_dp_payload_table(mgr, buf);
2661 if (bret == true) {
2662 seq_printf(m, "payload table: ");
2663 for (i = 0; i < 63; i++)
2664 seq_printf(m, "%02x ", buf[i]);
2665 seq_printf(m, "\n");
2666 }
2667
2668 }
2669
2670 mutex_unlock(&mgr->lock);
2671
2672 }
2673 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2674
2675 static void drm_dp_tx_work(struct work_struct *work)
2676 {
2677 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2678
2679 mutex_lock(&mgr->qlock);
2680 if (mgr->tx_down_in_progress)
2681 process_single_down_tx_qlock(mgr);
2682 mutex_unlock(&mgr->qlock);
2683 }
2684
2685 static void drm_dp_destroy_connector_work(struct work_struct *work)
2686 {
2687 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
2688 struct drm_dp_mst_port *port;
2689 bool send_hotplug = false;
2690 /*
2691 * Not a regular list traverse as we have to drop the destroy
2692 * connector lock before destroying the connector, to avoid AB->BA
2693 * ordering between this lock and the config mutex.
2694 */
2695 for (;;) {
2696 mutex_lock(&mgr->destroy_connector_lock);
2697 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2698 if (!port) {
2699 mutex_unlock(&mgr->destroy_connector_lock);
2700 break;
2701 }
2702 list_del(&port->next);
2703 mutex_unlock(&mgr->destroy_connector_lock);
2704
2705 mgr->cbs->destroy_connector(mgr, port->connector);
2706
2707 drm_dp_port_teardown_pdt(port, port->pdt);
2708
2709 if (!port->input && port->vcpi.vcpi > 0)
2710 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2711 kfree(port);
2712 send_hotplug = true;
2713 }
2714 if (send_hotplug)
2715 (*mgr->cbs->hotplug)(mgr);
2716 }
2717
2718 /**
2719 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2720 * @mgr: manager struct to initialise
2721 * @dev: device providing this structure - for i2c addition.
2722 * @aux: DP helper aux channel to talk to this device
2723 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2724 * @max_payloads: maximum number of payloads this GPU can source
2725 * @conn_base_id: the connector object ID the MST device is connected to.
2726 *
2727 * Return 0 for success, or negative error code on failure
2728 */
2729 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2730 struct device *dev, struct drm_dp_aux *aux,
2731 int max_dpcd_transaction_bytes,
2732 int max_payloads, int conn_base_id)
2733 {
2734 mutex_init(&mgr->lock);
2735 mutex_init(&mgr->qlock);
2736 mutex_init(&mgr->payload_lock);
2737 mutex_init(&mgr->destroy_connector_lock);
2738 INIT_LIST_HEAD(&mgr->tx_msg_upq);
2739 INIT_LIST_HEAD(&mgr->tx_msg_downq);
2740 INIT_LIST_HEAD(&mgr->destroy_connector_list);
2741 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2742 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
2743 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
2744 init_waitqueue_head(&mgr->tx_waitq);
2745 mgr->dev = dev;
2746 mgr->aux = aux;
2747 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2748 mgr->max_payloads = max_payloads;
2749 mgr->conn_base_id = conn_base_id;
2750 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
2751 if (!mgr->payloads)
2752 return -ENOMEM;
2753 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
2754 if (!mgr->proposed_vcpis)
2755 return -ENOMEM;
2756 set_bit(0, &mgr->payload_mask);
2757 test_calc_pbn_mode();
2758 return 0;
2759 }
2760 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
2761
2762 /**
2763 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
2764 * @mgr: manager to destroy
2765 */
2766 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
2767 {
2768 flush_work(&mgr->destroy_connector_work);
2769 mutex_lock(&mgr->payload_lock);
2770 kfree(mgr->payloads);
2771 mgr->payloads = NULL;
2772 kfree(mgr->proposed_vcpis);
2773 mgr->proposed_vcpis = NULL;
2774 mutex_unlock(&mgr->payload_lock);
2775 mgr->dev = NULL;
2776 mgr->aux = NULL;
2777 }
2778 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
2779
2780 /* I2C device */
2781 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
2782 int num)
2783 {
2784 struct drm_dp_aux *aux = adapter->algo_data;
2785 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
2786 struct drm_dp_mst_branch *mstb;
2787 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2788 unsigned int i;
2789 bool reading = false;
2790 struct drm_dp_sideband_msg_req_body msg;
2791 struct drm_dp_sideband_msg_tx *txmsg = NULL;
2792 int ret;
2793
2794 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
2795 if (!mstb)
2796 return -EREMOTEIO;
2797
2798 /* construct i2c msg */
2799 /* see if last msg is a read */
2800 if (msgs[num - 1].flags & I2C_M_RD)
2801 reading = true;
2802
2803 if (!reading) {
2804 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
2805 ret = -EIO;
2806 goto out;
2807 }
2808
2809 msg.req_type = DP_REMOTE_I2C_READ;
2810 msg.u.i2c_read.num_transactions = num - 1;
2811 msg.u.i2c_read.port_number = port->port_num;
2812 for (i = 0; i < num - 1; i++) {
2813 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
2814 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
2815 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
2816 }
2817 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
2818 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
2819
2820 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2821 if (!txmsg) {
2822 ret = -ENOMEM;
2823 goto out;
2824 }
2825
2826 txmsg->dst = mstb;
2827 drm_dp_encode_sideband_req(&msg, txmsg);
2828
2829 drm_dp_queue_down_tx(mgr, txmsg);
2830
2831 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2832 if (ret > 0) {
2833
2834 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
2835 ret = -EREMOTEIO;
2836 goto out;
2837 }
2838 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
2839 ret = -EIO;
2840 goto out;
2841 }
2842 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
2843 ret = num;
2844 }
2845 out:
2846 kfree(txmsg);
2847 drm_dp_put_mst_branch_device(mstb);
2848 return ret;
2849 }
2850
2851 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
2852 {
2853 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
2854 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
2855 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
2856 I2C_FUNC_10BIT_ADDR;
2857 }
2858
2859 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
2860 .functionality = drm_dp_mst_i2c_functionality,
2861 .master_xfer = drm_dp_mst_i2c_xfer,
2862 };
2863
2864 /**
2865 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
2866 * @aux: DisplayPort AUX channel
2867 *
2868 * Returns 0 on success or a negative error code on failure.
2869 */
2870 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
2871 {
2872 aux->ddc.algo = &drm_dp_mst_i2c_algo;
2873 aux->ddc.algo_data = aux;
2874 aux->ddc.retries = 3;
2875
2876 aux->ddc.class = I2C_CLASS_DDC;
2877 aux->ddc.owner = THIS_MODULE;
2878 aux->ddc.dev.parent = aux->dev;
2879 aux->ddc.dev.of_node = aux->dev->of_node;
2880
2881 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
2882 sizeof(aux->ddc.name));
2883
2884 return i2c_add_adapter(&aux->ddc);
2885 }
2886
2887 /**
2888 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
2889 * @aux: DisplayPort AUX channel
2890 */
2891 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
2892 {
2893 i2c_del_adapter(&aux->ddc);
2894 }
This page took 0.43374 seconds and 6 git commands to generate.