blob: 7710de6e8a55f9640f0548b8464ab892f93746a4 [file] [log] [blame]
Dave Airliead7f8a12014-06-05 14:01:32 +10001/*
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>
Dave Airlie75bc08a2014-07-09 11:15:09 +100028#include <linux/seq_file.h>
Dave Airliead7f8a12014-06-05 14:01:32 +100029#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 */
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
Dave Airlie68d8c9f2015-09-06 18:53:00 +100056static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
57 struct drm_dp_mst_branch *mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +100058static 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/* sideband msg handling */
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/* this adds a chunk of msg to the builder to get the final msg */
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 /* 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
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 /* 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;
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,
Libin Yangef8f9be2015-12-02 14:09:43 +0800669 u8 vcpi, uint16_t pbn,
670 u8 number_sdp_streams,
671 u8 *sdp_stream_sink)
Dave Airliead7f8a12014-06-05 14:01:32 +1000672{
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;
Libin Yangef8f9be2015-12-02 14:09:43 +0800679 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);
Dave Airliead7f8a12014-06-05 14:01:32 +1000682 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{
Dave Airliedfda0df2014-08-06 16:26:21 +1000690 int ret, vcpi_ret;
Dave Airliead7f8a12014-06-05 14:01:32 +1000691
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
Dave Airliedfda0df2014-08-06 16:26:21 +1000700 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
Dave Airliead7f8a12014-06-05 14:01:32 +1000707 set_bit(ret, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +1000708 set_bit(vcpi_ret, &mgr->vcpi_mask);
709 vcpi->vcpi = vcpi_ret + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +1000710 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,
Dave Airliedfda0df2014-08-06 16:26:21 +1000717 int vcpi)
Dave Airliead7f8a12014-06-05 14:01:32 +1000718{
Dave Airliedfda0df2014-08-06 16:26:21 +1000719 int i;
720 if (vcpi == 0)
Dave Airliead7f8a12014-06-05 14:01:32 +1000721 return;
722
723 mutex_lock(&mgr->payload_lock);
Dave Airliedfda0df2014-08-06 16:26:21 +1000724 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 }
Dave Airliead7f8a12014-06-05 14:01:32 +1000734 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;
Daniel Vettercd961bb2015-01-28 10:02:23 +0100741
742 /*
743 * All updates to txmsg->state are protected by mgr->qlock, and the two
744 * cases we check here are terminal states. For those the barriers
745 * provided by the wake_up/wait_event pair are enough.
746 */
Dave Airliead7f8a12014-06-05 14:01:32 +1000747 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
748 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
Dave Airliead7f8a12014-06-05 14:01:32 +1000749 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 /* dump some state */
771 ret = -EIO;
772
773 /* remove from q */
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_destroy_mst_branch_device(struct kref *kref)
807{
808 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
809 struct drm_dp_mst_port *port, *tmp;
810 bool wake_tx = false;
811
Dave Airliead7f8a12014-06-05 14:01:32 +1000812 /*
813 * destroy all ports - don't need lock
814 * as there are no more references to the mst branch
815 * device at this point.
816 */
817 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
818 list_del(&port->next);
819 drm_dp_put_port(port);
820 }
821
822 /* drop any tx slots msg */
823 mutex_lock(&mstb->mgr->qlock);
824 if (mstb->tx_slots[0]) {
825 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
826 mstb->tx_slots[0] = NULL;
827 wake_tx = true;
828 }
829 if (mstb->tx_slots[1]) {
830 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
831 mstb->tx_slots[1] = NULL;
832 wake_tx = true;
833 }
834 mutex_unlock(&mstb->mgr->qlock);
835
836 if (wake_tx)
837 wake_up(&mstb->mgr->tx_waitq);
838 kfree(mstb);
839}
840
841static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
842{
843 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
844}
845
846
847static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
848{
Daniel Vetter03913592014-12-08 22:55:22 +0100849 struct drm_dp_mst_branch *mstb;
850
Dave Airliead7f8a12014-06-05 14:01:32 +1000851 switch (old_pdt) {
852 case DP_PEER_DEVICE_DP_LEGACY_CONV:
853 case DP_PEER_DEVICE_SST_SINK:
854 /* remove i2c over sideband */
855 drm_dp_mst_unregister_i2c_bus(&port->aux);
856 break;
857 case DP_PEER_DEVICE_MST_BRANCHING:
Daniel Vetter03913592014-12-08 22:55:22 +0100858 mstb = port->mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +1000859 port->mstb = NULL;
Daniel Vetter03913592014-12-08 22:55:22 +0100860 drm_dp_put_mst_branch_device(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +1000861 break;
862 }
863}
864
865static void drm_dp_destroy_port(struct kref *kref)
866{
867 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
868 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
Dave Airliedf4839f2015-09-16 10:37:28 +1000869
Dave Airliead7f8a12014-06-05 14:01:32 +1000870 if (!port->input) {
871 port->vcpi.num_slots = 0;
Dave Airliec6a0aed2014-10-20 16:28:02 +1000872
873 kfree(port->cached_edid);
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000874
Dave Airliedf4839f2015-09-16 10:37:28 +1000875 /*
876 * The only time we don't have a connector
877 * on an output port is if the connector init
878 * fails.
879 */
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000880 if (port->connector) {
Dave Airliedf4839f2015-09-16 10:37:28 +1000881 /* we can't destroy the connector here, as
882 * we might be holding the mode_config.mutex
883 * from an EDID retrieval */
884
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000885 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +0200886 list_add(&port->next, &mgr->destroy_connector_list);
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000887 mutex_unlock(&mgr->destroy_connector_lock);
888 schedule_work(&mgr->destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +0200889 return;
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000890 }
Dave Airliedf4839f2015-09-16 10:37:28 +1000891 /* no need to clean up vcpi
892 * as if we have no connector we never setup a vcpi */
Dave Airliead7f8a12014-06-05 14:01:32 +1000893 drm_dp_port_teardown_pdt(port, port->pdt);
Dave Airliead7f8a12014-06-05 14:01:32 +1000894 }
895 kfree(port);
Dave Airliead7f8a12014-06-05 14:01:32 +1000896}
897
898static void drm_dp_put_port(struct drm_dp_mst_port *port)
899{
900 kref_put(&port->kref, drm_dp_destroy_port);
901}
902
903static 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)
904{
905 struct drm_dp_mst_port *port;
906 struct drm_dp_mst_branch *rmstb;
907 if (to_find == mstb) {
908 kref_get(&mstb->kref);
909 return mstb;
910 }
911 list_for_each_entry(port, &mstb->ports, next) {
912 if (port->mstb) {
913 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
914 if (rmstb)
915 return rmstb;
916 }
917 }
918 return NULL;
919}
920
921static struct drm_dp_mst_branch *drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb)
922{
923 struct drm_dp_mst_branch *rmstb = NULL;
924 mutex_lock(&mgr->lock);
925 if (mgr->mst_primary)
926 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
927 mutex_unlock(&mgr->lock);
928 return rmstb;
929}
930
931static 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)
932{
933 struct drm_dp_mst_port *port, *mport;
934
935 list_for_each_entry(port, &mstb->ports, next) {
936 if (port == to_find) {
937 kref_get(&port->kref);
938 return port;
939 }
940 if (port->mstb) {
941 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
942 if (mport)
943 return mport;
944 }
945 }
946 return NULL;
947}
948
949static struct drm_dp_mst_port *drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
950{
951 struct drm_dp_mst_port *rport = NULL;
952 mutex_lock(&mgr->lock);
953 if (mgr->mst_primary)
954 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
955 mutex_unlock(&mgr->lock);
956 return rport;
957}
958
959static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
960{
961 struct drm_dp_mst_port *port;
962
963 list_for_each_entry(port, &mstb->ports, next) {
964 if (port->port_num == port_num) {
965 kref_get(&port->kref);
966 return port;
967 }
968 }
969
970 return NULL;
971}
972
973/*
974 * calculate a new RAD for this MST branch device
975 * if parent has an LCT of 2 then it has 1 nibble of RAD,
976 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
977 */
978static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
979 u8 *rad)
980{
981 int lct = port->parent->lct;
982 int shift = 4;
983 int idx = lct / 2;
984 if (lct > 1) {
985 memcpy(rad, port->parent->rad, idx);
986 shift = (lct % 2) ? 4 : 0;
987 } else
988 rad[0] = 0;
989
990 rad[idx] |= port->port_num << shift;
991 return lct + 1;
992}
993
994/*
995 * return sends link address for new mstb
996 */
997static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
998{
999 int ret;
1000 u8 rad[6], lct;
1001 bool send_link = false;
1002 switch (port->pdt) {
1003 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1004 case DP_PEER_DEVICE_SST_SINK:
1005 /* add i2c over sideband */
1006 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1007 break;
1008 case DP_PEER_DEVICE_MST_BRANCHING:
1009 lct = drm_dp_calculate_rad(port, rad);
1010
1011 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1012 port->mstb->mgr = port->mgr;
1013 port->mstb->port_parent = port;
1014
1015 send_link = true;
1016 break;
1017 }
1018 return send_link;
1019}
1020
1021static void drm_dp_check_port_guid(struct drm_dp_mst_branch *mstb,
1022 struct drm_dp_mst_port *port)
1023{
1024 int ret;
1025 if (port->dpcd_rev >= 0x12) {
1026 port->guid_valid = drm_dp_validate_guid(mstb->mgr, port->guid);
1027 if (!port->guid_valid) {
1028 ret = drm_dp_send_dpcd_write(mstb->mgr,
1029 port,
1030 DP_GUID,
1031 16, port->guid);
1032 port->guid_valid = true;
1033 }
1034 }
1035}
1036
Dave Airlie1c960872015-09-16 11:04:49 +10001037static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1038 int pnum,
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001039 char *proppath,
1040 size_t proppath_size)
Dave Airliead7f8a12014-06-05 14:01:32 +10001041{
1042 int i;
1043 char temp[8];
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001044 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
Dave Airliead7f8a12014-06-05 14:01:32 +10001045 for (i = 0; i < (mstb->lct - 1); i++) {
1046 int shift = (i % 2) ? 0 : 4;
1047 int port_num = mstb->rad[i / 2] >> shift;
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001048 snprintf(temp, sizeof(temp), "-%d", port_num);
1049 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001050 }
Dave Airlie1c960872015-09-16 11:04:49 +10001051 snprintf(temp, sizeof(temp), "-%d", pnum);
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001052 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001053}
1054
1055static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1056 struct device *dev,
1057 struct drm_dp_link_addr_reply_port *port_msg)
1058{
1059 struct drm_dp_mst_port *port;
1060 bool ret;
1061 bool created = false;
1062 int old_pdt = 0;
1063 int old_ddps = 0;
1064 port = drm_dp_get_port(mstb, port_msg->port_number);
1065 if (!port) {
1066 port = kzalloc(sizeof(*port), GFP_KERNEL);
1067 if (!port)
1068 return;
1069 kref_init(&port->kref);
1070 port->parent = mstb;
1071 port->port_num = port_msg->port_number;
1072 port->mgr = mstb->mgr;
1073 port->aux.name = "DPMST";
1074 port->aux.dev = dev;
1075 created = true;
1076 } else {
1077 old_pdt = port->pdt;
1078 old_ddps = port->ddps;
1079 }
1080
1081 port->pdt = port_msg->peer_device_type;
1082 port->input = port_msg->input_port;
1083 port->mcs = port_msg->mcs;
1084 port->ddps = port_msg->ddps;
1085 port->ldps = port_msg->legacy_device_plug_status;
1086 port->dpcd_rev = port_msg->dpcd_revision;
1087 port->num_sdp_streams = port_msg->num_sdp_streams;
1088 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1089 memcpy(port->guid, port_msg->peer_guid, 16);
1090
1091 /* manage mstb port lists with mgr lock - take a reference
1092 for this list */
1093 if (created) {
1094 mutex_lock(&mstb->mgr->lock);
1095 kref_get(&port->kref);
1096 list_add(&port->next, &mstb->ports);
1097 mutex_unlock(&mstb->mgr->lock);
1098 }
1099
1100 if (old_ddps != port->ddps) {
1101 if (port->ddps) {
1102 drm_dp_check_port_guid(mstb, port);
1103 if (!port->input)
1104 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1105 } else {
1106 port->guid_valid = false;
1107 port->available_pbn = 0;
1108 }
1109 }
1110
1111 if (old_pdt != port->pdt && !port->input) {
1112 drm_dp_port_teardown_pdt(port, old_pdt);
1113
1114 ret = drm_dp_port_setup_pdt(port);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001115 if (ret == true)
Dave Airliead7f8a12014-06-05 14:01:32 +10001116 drm_dp_send_link_address(mstb->mgr, port->mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001117 }
1118
1119 if (created && !port->input) {
1120 char proppath[255];
Dave Airlie1c960872015-09-16 11:04:49 +10001121
1122 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
Dave Airliead7f8a12014-06-05 14:01:32 +10001123 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
Dave Airliedf4839f2015-09-16 10:37:28 +10001124 if (!port->connector) {
1125 /* remove it from the port list */
1126 mutex_lock(&mstb->mgr->lock);
1127 list_del(&port->next);
1128 mutex_unlock(&mstb->mgr->lock);
1129 /* drop port list reference */
1130 drm_dp_put_port(port);
1131 goto out;
1132 }
Dave Airlieccf03d62015-10-01 16:28:25 +10001133 if (port->port_num >= DP_MST_LOGICAL_PORT_0) {
Dave Airliec6a0aed2014-10-20 16:28:02 +10001134 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
Dave Airlied9515c52015-09-16 17:55:23 +10001135 drm_mode_connector_set_tile_property(port->connector);
Dave Airliec6a0aed2014-10-20 16:28:02 +10001136 }
Dave Airlied9515c52015-09-16 17:55:23 +10001137 (*mstb->mgr->cbs->register_connector)(port->connector);
Dave Airliead7f8a12014-06-05 14:01:32 +10001138 }
1139
Dave Airliedf4839f2015-09-16 10:37:28 +10001140out:
Dave Airliead7f8a12014-06-05 14:01:32 +10001141 /* put reference to this port */
1142 drm_dp_put_port(port);
1143}
1144
1145static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1146 struct drm_dp_connection_status_notify *conn_stat)
1147{
1148 struct drm_dp_mst_port *port;
1149 int old_pdt;
1150 int old_ddps;
1151 bool dowork = false;
1152 port = drm_dp_get_port(mstb, conn_stat->port_number);
1153 if (!port)
1154 return;
1155
1156 old_ddps = port->ddps;
1157 old_pdt = port->pdt;
1158 port->pdt = conn_stat->peer_device_type;
1159 port->mcs = conn_stat->message_capability_status;
1160 port->ldps = conn_stat->legacy_device_plug_status;
1161 port->ddps = conn_stat->displayport_device_plug_status;
1162
1163 if (old_ddps != port->ddps) {
1164 if (port->ddps) {
1165 drm_dp_check_port_guid(mstb, port);
1166 dowork = true;
1167 } else {
1168 port->guid_valid = false;
1169 port->available_pbn = 0;
1170 }
1171 }
1172 if (old_pdt != port->pdt && !port->input) {
1173 drm_dp_port_teardown_pdt(port, old_pdt);
1174
1175 if (drm_dp_port_setup_pdt(port))
1176 dowork = true;
1177 }
1178
1179 drm_dp_put_port(port);
1180 if (dowork)
1181 queue_work(system_long_wq, &mstb->mgr->work);
1182
1183}
1184
1185static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1186 u8 lct, u8 *rad)
1187{
1188 struct drm_dp_mst_branch *mstb;
1189 struct drm_dp_mst_port *port;
1190 int i;
1191 /* find the port by iterating down */
Dave Airlie9eb1e572015-06-22 14:40:44 +10001192
1193 mutex_lock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001194 mstb = mgr->mst_primary;
1195
1196 for (i = 0; i < lct - 1; i++) {
1197 int shift = (i % 2) ? 0 : 4;
1198 int port_num = rad[i / 2] >> shift;
1199
1200 list_for_each_entry(port, &mstb->ports, next) {
1201 if (port->port_num == port_num) {
Adam Richter30730c72015-10-16 03:33:02 -07001202 mstb = port->mstb;
1203 if (!mstb) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001204 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
Adam Richter30730c72015-10-16 03:33:02 -07001205 goto out;
Dave Airliead7f8a12014-06-05 14:01:32 +10001206 }
1207
Dave Airliead7f8a12014-06-05 14:01:32 +10001208 break;
1209 }
1210 }
1211 }
1212 kref_get(&mstb->kref);
Adam Richter30730c72015-10-16 03:33:02 -07001213out:
Dave Airlie9eb1e572015-06-22 14:40:44 +10001214 mutex_unlock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001215 return mstb;
1216}
1217
Mykola Lysenkobd934322015-12-18 17:14:42 -05001218static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1219 struct drm_dp_mst_branch *mstb,
1220 uint8_t *guid)
1221{
1222 struct drm_dp_mst_branch *found_mstb;
1223 struct drm_dp_mst_port *port;
1224
1225 list_for_each_entry(port, &mstb->ports, next) {
1226 if (!port->mstb)
1227 continue;
1228
1229 if (port->guid_valid && memcmp(port->guid, guid, 16) == 0)
1230 return port->mstb;
1231
1232 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1233
1234 if (found_mstb)
1235 return found_mstb;
1236 }
1237
1238 return NULL;
1239}
1240
1241static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
1242 struct drm_dp_mst_topology_mgr *mgr,
1243 uint8_t *guid)
1244{
1245 struct drm_dp_mst_branch *mstb;
1246
1247 /* find the port by iterating down */
1248 mutex_lock(&mgr->lock);
1249
1250 if (mgr->guid_valid && memcmp(mgr->guid, guid, 16) == 0)
1251 mstb = mgr->mst_primary;
1252 else
1253 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1254
1255 if (mstb)
1256 kref_get(&mstb->kref);
1257
1258 mutex_unlock(&mgr->lock);
1259 return mstb;
1260}
1261
Dave Airliead7f8a12014-06-05 14:01:32 +10001262static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1263 struct drm_dp_mst_branch *mstb)
1264{
1265 struct drm_dp_mst_port *port;
Daniel Vetter9254ec42015-06-22 17:31:59 +10001266 struct drm_dp_mst_branch *mstb_child;
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001267 if (!mstb->link_address_sent)
Dave Airliead7f8a12014-06-05 14:01:32 +10001268 drm_dp_send_link_address(mgr, mstb);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001269
Dave Airliead7f8a12014-06-05 14:01:32 +10001270 list_for_each_entry(port, &mstb->ports, next) {
1271 if (port->input)
1272 continue;
1273
1274 if (!port->ddps)
1275 continue;
1276
1277 if (!port->available_pbn)
1278 drm_dp_send_enum_path_resources(mgr, mstb, port);
1279
Daniel Vetter9254ec42015-06-22 17:31:59 +10001280 if (port->mstb) {
1281 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1282 if (mstb_child) {
1283 drm_dp_check_and_send_link_address(mgr, mstb_child);
1284 drm_dp_put_mst_branch_device(mstb_child);
1285 }
1286 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001287 }
1288}
1289
1290static void drm_dp_mst_link_probe_work(struct work_struct *work)
1291{
1292 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001293 struct drm_dp_mst_branch *mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +10001294
Daniel Vetter9254ec42015-06-22 17:31:59 +10001295 mutex_lock(&mgr->lock);
1296 mstb = mgr->mst_primary;
1297 if (mstb) {
1298 kref_get(&mstb->kref);
1299 }
1300 mutex_unlock(&mgr->lock);
1301 if (mstb) {
1302 drm_dp_check_and_send_link_address(mgr, mstb);
1303 drm_dp_put_mst_branch_device(mstb);
1304 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001305}
1306
1307static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1308 u8 *guid)
1309{
1310 static u8 zero_guid[16];
1311
1312 if (!memcmp(guid, zero_guid, 16)) {
1313 u64 salt = get_jiffies_64();
1314 memcpy(&guid[0], &salt, sizeof(u64));
1315 memcpy(&guid[8], &salt, sizeof(u64));
1316 return false;
1317 }
1318 return true;
1319}
1320
1321#if 0
1322static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1323{
1324 struct drm_dp_sideband_msg_req_body req;
1325
1326 req.req_type = DP_REMOTE_DPCD_READ;
1327 req.u.dpcd_read.port_number = port_num;
1328 req.u.dpcd_read.dpcd_address = offset;
1329 req.u.dpcd_read.num_bytes = num_bytes;
1330 drm_dp_encode_sideband_req(&req, msg);
1331
1332 return 0;
1333}
1334#endif
1335
1336static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1337 bool up, u8 *msg, int len)
1338{
1339 int ret;
1340 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1341 int tosend, total, offset;
1342 int retries = 0;
1343
1344retry:
1345 total = len;
1346 offset = 0;
1347 do {
1348 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1349
1350 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1351 &msg[offset],
1352 tosend);
1353 if (ret != tosend) {
1354 if (ret == -EIO && retries < 5) {
1355 retries++;
1356 goto retry;
1357 }
1358 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10001359
1360 return -EIO;
1361 }
1362 offset += tosend;
1363 total -= tosend;
1364 } while (total > 0);
1365 return 0;
1366}
1367
1368static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1369 struct drm_dp_sideband_msg_tx *txmsg)
1370{
1371 struct drm_dp_mst_branch *mstb = txmsg->dst;
Mykola Lysenkobd934322015-12-18 17:14:42 -05001372 u8 req_type;
Dave Airliead7f8a12014-06-05 14:01:32 +10001373
1374 /* both msg slots are full */
1375 if (txmsg->seqno == -1) {
1376 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1377 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1378 return -EAGAIN;
1379 }
1380 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1381 txmsg->seqno = mstb->last_seqno;
1382 mstb->last_seqno ^= 1;
1383 } else if (mstb->tx_slots[0] == NULL)
1384 txmsg->seqno = 0;
1385 else
1386 txmsg->seqno = 1;
1387 mstb->tx_slots[txmsg->seqno] = txmsg;
1388 }
Mykola Lysenkobd934322015-12-18 17:14:42 -05001389
1390 req_type = txmsg->msg[0] & 0x7f;
1391 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1392 req_type == DP_RESOURCE_STATUS_NOTIFY)
1393 hdr->broadcast = 1;
1394 else
1395 hdr->broadcast = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001396 hdr->path_msg = txmsg->path_msg;
1397 hdr->lct = mstb->lct;
1398 hdr->lcr = mstb->lct - 1;
1399 if (mstb->lct > 1)
1400 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1401 hdr->seqno = txmsg->seqno;
1402 return 0;
1403}
1404/*
1405 * process a single block of the next message in the sideband queue
1406 */
1407static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1408 struct drm_dp_sideband_msg_tx *txmsg,
1409 bool up)
1410{
1411 u8 chunk[48];
1412 struct drm_dp_sideband_msg_hdr hdr;
1413 int len, space, idx, tosend;
1414 int ret;
1415
Damien Lespiaubf3719c2014-07-14 12:13:18 +01001416 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1417
Dave Airliead7f8a12014-06-05 14:01:32 +10001418 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1419 txmsg->seqno = -1;
1420 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1421 }
1422
1423 /* make hdr from dst mst - for replies use seqno
1424 otherwise assign one */
1425 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1426 if (ret < 0)
1427 return ret;
1428
1429 /* amount left to send in this message */
1430 len = txmsg->cur_len - txmsg->cur_offset;
1431
1432 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1433 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1434
1435 tosend = min(len, space);
1436 if (len == txmsg->cur_len)
1437 hdr.somt = 1;
1438 if (space >= len)
1439 hdr.eomt = 1;
1440
1441
1442 hdr.msg_len = tosend + 1;
1443 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1444 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1445 /* add crc at end */
1446 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1447 idx += tosend + 1;
1448
1449 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1450 if (ret) {
1451 DRM_DEBUG_KMS("sideband msg failed to send\n");
1452 return ret;
1453 }
1454
1455 txmsg->cur_offset += tosend;
1456 if (txmsg->cur_offset == txmsg->cur_len) {
1457 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1458 return 1;
1459 }
1460 return 0;
1461}
1462
Dave Airliead7f8a12014-06-05 14:01:32 +10001463static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1464{
1465 struct drm_dp_sideband_msg_tx *txmsg;
1466 int ret;
1467
Daniel Vettercd961bb2015-01-28 10:02:23 +01001468 WARN_ON(!mutex_is_locked(&mgr->qlock));
1469
Dave Airliead7f8a12014-06-05 14:01:32 +10001470 /* construct a chunk from the first msg in the tx_msg queue */
1471 if (list_empty(&mgr->tx_msg_downq)) {
1472 mgr->tx_down_in_progress = false;
1473 return;
1474 }
1475 mgr->tx_down_in_progress = true;
1476
1477 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1478 ret = process_single_tx_qlock(mgr, txmsg, false);
1479 if (ret == 1) {
1480 /* txmsg is sent it should be in the slots now */
1481 list_del(&txmsg->next);
1482 } else if (ret) {
1483 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1484 list_del(&txmsg->next);
1485 if (txmsg->seqno != -1)
1486 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1487 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1488 wake_up(&mgr->tx_waitq);
1489 }
1490 if (list_empty(&mgr->tx_msg_downq)) {
1491 mgr->tx_down_in_progress = false;
1492 return;
1493 }
1494}
1495
1496/* called holding qlock */
1497static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1498{
1499 struct drm_dp_sideband_msg_tx *txmsg;
1500 int ret;
1501
1502 /* construct a chunk from the first msg in the tx_msg queue */
1503 if (list_empty(&mgr->tx_msg_upq)) {
1504 mgr->tx_up_in_progress = false;
1505 return;
1506 }
1507
1508 txmsg = list_first_entry(&mgr->tx_msg_upq, struct drm_dp_sideband_msg_tx, next);
1509 ret = process_single_tx_qlock(mgr, txmsg, true);
1510 if (ret == 1) {
1511 /* up txmsgs aren't put in slots - so free after we send it */
1512 list_del(&txmsg->next);
1513 kfree(txmsg);
1514 } else if (ret)
1515 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1516 mgr->tx_up_in_progress = true;
1517}
1518
1519static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1520 struct drm_dp_sideband_msg_tx *txmsg)
1521{
1522 mutex_lock(&mgr->qlock);
1523 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1524 if (!mgr->tx_down_in_progress)
1525 process_single_down_tx_qlock(mgr);
1526 mutex_unlock(&mgr->qlock);
1527}
1528
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001529static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1530 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001531{
1532 int len;
1533 struct drm_dp_sideband_msg_tx *txmsg;
1534 int ret;
1535
1536 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1537 if (!txmsg)
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001538 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001539
1540 txmsg->dst = mstb;
1541 len = build_link_address(txmsg);
1542
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001543 mstb->link_address_sent = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001544 drm_dp_queue_down_tx(mgr, txmsg);
1545
1546 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1547 if (ret > 0) {
1548 int i;
1549
1550 if (txmsg->reply.reply_type == 1)
1551 DRM_DEBUG_KMS("link address nak received\n");
1552 else {
1553 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1554 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1555 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,
1556 txmsg->reply.u.link_addr.ports[i].input_port,
1557 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1558 txmsg->reply.u.link_addr.ports[i].port_number,
1559 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1560 txmsg->reply.u.link_addr.ports[i].mcs,
1561 txmsg->reply.u.link_addr.ports[i].ddps,
1562 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1563 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1564 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1565 }
1566 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1567 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1568 }
1569 (*mgr->cbs->hotplug)(mgr);
1570 }
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001571 } else {
1572 mstb->link_address_sent = false;
Dave Airliead7f8a12014-06-05 14:01:32 +10001573 DRM_DEBUG_KMS("link address failed %d\n", ret);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001574 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001575
1576 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10001577}
1578
1579static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1580 struct drm_dp_mst_branch *mstb,
1581 struct drm_dp_mst_port *port)
1582{
1583 int len;
1584 struct drm_dp_sideband_msg_tx *txmsg;
1585 int ret;
1586
1587 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1588 if (!txmsg)
1589 return -ENOMEM;
1590
1591 txmsg->dst = mstb;
1592 len = build_enum_path_resources(txmsg, port->port_num);
1593
1594 drm_dp_queue_down_tx(mgr, txmsg);
1595
1596 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1597 if (ret > 0) {
1598 if (txmsg->reply.reply_type == 1)
1599 DRM_DEBUG_KMS("enum path resources nak received\n");
1600 else {
1601 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1602 DRM_ERROR("got incorrect port in response\n");
1603 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,
1604 txmsg->reply.u.path_resources.avail_payload_bw_number);
1605 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1606 }
1607 }
1608
1609 kfree(txmsg);
1610 return 0;
1611}
1612
Thierry Reding8fa6a422014-07-21 13:23:57 +02001613static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1614 struct drm_dp_mst_port *port,
1615 int id,
1616 int pbn)
Dave Airliead7f8a12014-06-05 14:01:32 +10001617{
1618 struct drm_dp_sideband_msg_tx *txmsg;
1619 struct drm_dp_mst_branch *mstb;
1620 int len, ret;
Libin Yangef8f9be2015-12-02 14:09:43 +08001621 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1622 int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10001623
1624 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1625 if (!mstb)
1626 return -EINVAL;
1627
1628 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1629 if (!txmsg) {
1630 ret = -ENOMEM;
1631 goto fail_put;
1632 }
1633
Libin Yangef8f9be2015-12-02 14:09:43 +08001634 for (i = 0; i < port->num_sdp_streams; i++)
1635 sinks[i] = i;
1636
Dave Airliead7f8a12014-06-05 14:01:32 +10001637 txmsg->dst = mstb;
1638 len = build_allocate_payload(txmsg, port->port_num,
1639 id,
Libin Yangef8f9be2015-12-02 14:09:43 +08001640 pbn, port->num_sdp_streams, sinks);
Dave Airliead7f8a12014-06-05 14:01:32 +10001641
1642 drm_dp_queue_down_tx(mgr, txmsg);
1643
1644 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1645 if (ret > 0) {
1646 if (txmsg->reply.reply_type == 1) {
1647 ret = -EINVAL;
1648 } else
1649 ret = 0;
1650 }
1651 kfree(txmsg);
1652fail_put:
1653 drm_dp_put_mst_branch_device(mstb);
1654 return ret;
1655}
1656
1657static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1658 int id,
1659 struct drm_dp_payload *payload)
1660{
1661 int ret;
1662
1663 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1664 if (ret < 0) {
1665 payload->payload_state = 0;
1666 return ret;
1667 }
1668 payload->payload_state = DP_PAYLOAD_LOCAL;
1669 return 0;
1670}
1671
Thierry Reding8fa6a422014-07-21 13:23:57 +02001672static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1673 struct drm_dp_mst_port *port,
1674 int id,
1675 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001676{
1677 int ret;
1678 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1679 if (ret < 0)
1680 return ret;
1681 payload->payload_state = DP_PAYLOAD_REMOTE;
1682 return ret;
1683}
1684
Thierry Reding8fa6a422014-07-21 13:23:57 +02001685static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1686 struct drm_dp_mst_port *port,
1687 int id,
1688 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001689{
1690 DRM_DEBUG_KMS("\n");
1691 /* its okay for these to fail */
1692 if (port) {
1693 drm_dp_payload_send_msg(mgr, port, id, 0);
1694 }
1695
1696 drm_dp_dpcd_write_payload(mgr, id, payload);
Dave Airliedfda0df2014-08-06 16:26:21 +10001697 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001698 return 0;
1699}
1700
Thierry Reding8fa6a422014-07-21 13:23:57 +02001701static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1702 int id,
1703 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001704{
1705 payload->payload_state = 0;
1706 return 0;
1707}
1708
1709/**
1710 * drm_dp_update_payload_part1() - Execute payload update part 1
1711 * @mgr: manager to use.
1712 *
1713 * This iterates over all proposed virtual channels, and tries to
1714 * allocate space in the link for them. For 0->slots transitions,
1715 * this step just writes the VCPI to the MST device. For slots->0
1716 * transitions, this writes the updated VCPIs and removes the
1717 * remote VC payloads.
1718 *
1719 * after calling this the driver should generate ACT and payload
1720 * packets.
1721 */
1722int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1723{
Dave Airliedfda0df2014-08-06 16:26:21 +10001724 int i, j;
Dave Airliead7f8a12014-06-05 14:01:32 +10001725 int cur_slots = 1;
1726 struct drm_dp_payload req_payload;
1727 struct drm_dp_mst_port *port;
1728
1729 mutex_lock(&mgr->payload_lock);
1730 for (i = 0; i < mgr->max_payloads; i++) {
1731 /* solve the current payloads - compare to the hw ones
1732 - update the hw view */
1733 req_payload.start_slot = cur_slots;
1734 if (mgr->proposed_vcpis[i]) {
1735 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1736 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
Harry Wentland6cecdf7a2015-12-07 13:55:52 -05001737 req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
Dave Airliead7f8a12014-06-05 14:01:32 +10001738 } else {
1739 port = NULL;
1740 req_payload.num_slots = 0;
1741 }
Dave Airliedfda0df2014-08-06 16:26:21 +10001742
1743 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1744 mgr->payloads[i].start_slot = req_payload.start_slot;
1745 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001746 /* work out what is required to happen with this payload */
Dave Airliedfda0df2014-08-06 16:26:21 +10001747 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001748
1749 /* need to push an update for this payload */
1750 if (req_payload.num_slots) {
Dave Airliedfda0df2014-08-06 16:26:21 +10001751 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
Dave Airliead7f8a12014-06-05 14:01:32 +10001752 mgr->payloads[i].num_slots = req_payload.num_slots;
Harry Wentland6cecdf7a2015-12-07 13:55:52 -05001753 mgr->payloads[i].vcpi = req_payload.vcpi;
Dave Airliead7f8a12014-06-05 14:01:32 +10001754 } else if (mgr->payloads[i].num_slots) {
1755 mgr->payloads[i].num_slots = 0;
Dave Airliedfda0df2014-08-06 16:26:21 +10001756 drm_dp_destroy_payload_step1(mgr, port, port->vcpi.vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10001757 req_payload.payload_state = mgr->payloads[i].payload_state;
Dave Airliedfda0df2014-08-06 16:26:21 +10001758 mgr->payloads[i].start_slot = 0;
1759 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001760 mgr->payloads[i].payload_state = req_payload.payload_state;
1761 }
1762 cur_slots += req_payload.num_slots;
1763 }
Dave Airliedfda0df2014-08-06 16:26:21 +10001764
1765 for (i = 0; i < mgr->max_payloads; i++) {
1766 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1767 DRM_DEBUG_KMS("removing payload %d\n", i);
1768 for (j = i; j < mgr->max_payloads - 1; j++) {
1769 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1770 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1771 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1772 set_bit(j + 1, &mgr->payload_mask);
1773 } else {
1774 clear_bit(j + 1, &mgr->payload_mask);
1775 }
1776 }
1777 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1778 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1779 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1780
1781 }
1782 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001783 mutex_unlock(&mgr->payload_lock);
1784
1785 return 0;
1786}
1787EXPORT_SYMBOL(drm_dp_update_payload_part1);
1788
1789/**
1790 * drm_dp_update_payload_part2() - Execute payload update part 2
1791 * @mgr: manager to use.
1792 *
1793 * This iterates over all proposed virtual channels, and tries to
1794 * allocate space in the link for them. For 0->slots transitions,
1795 * this step writes the remote VC payload commands. For slots->0
1796 * this just resets some internal state.
1797 */
1798int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1799{
1800 struct drm_dp_mst_port *port;
1801 int i;
Damien Lespiau7389ad42014-07-14 11:53:44 +01001802 int ret = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001803 mutex_lock(&mgr->payload_lock);
1804 for (i = 0; i < mgr->max_payloads; i++) {
1805
1806 if (!mgr->proposed_vcpis[i])
1807 continue;
1808
1809 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1810
1811 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1812 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10001813 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10001814 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10001815 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10001816 }
1817 if (ret) {
1818 mutex_unlock(&mgr->payload_lock);
1819 return ret;
1820 }
1821 }
1822 mutex_unlock(&mgr->payload_lock);
1823 return 0;
1824}
1825EXPORT_SYMBOL(drm_dp_update_payload_part2);
1826
1827#if 0 /* unused as of yet */
1828static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1829 struct drm_dp_mst_port *port,
1830 int offset, int size)
1831{
1832 int len;
1833 struct drm_dp_sideband_msg_tx *txmsg;
1834
1835 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1836 if (!txmsg)
1837 return -ENOMEM;
1838
1839 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1840 txmsg->dst = port->parent;
1841
1842 drm_dp_queue_down_tx(mgr, txmsg);
1843
1844 return 0;
1845}
1846#endif
1847
1848static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1849 struct drm_dp_mst_port *port,
1850 int offset, int size, u8 *bytes)
1851{
1852 int len;
1853 int ret;
1854 struct drm_dp_sideband_msg_tx *txmsg;
1855 struct drm_dp_mst_branch *mstb;
1856
1857 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1858 if (!mstb)
1859 return -EINVAL;
1860
1861 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1862 if (!txmsg) {
1863 ret = -ENOMEM;
1864 goto fail_put;
1865 }
1866
1867 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1868 txmsg->dst = mstb;
1869
1870 drm_dp_queue_down_tx(mgr, txmsg);
1871
1872 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1873 if (ret > 0) {
1874 if (txmsg->reply.reply_type == 1) {
1875 ret = -EINVAL;
1876 } else
1877 ret = 0;
1878 }
1879 kfree(txmsg);
1880fail_put:
1881 drm_dp_put_mst_branch_device(mstb);
1882 return ret;
1883}
1884
1885static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1886{
1887 struct drm_dp_sideband_msg_reply_body reply;
1888
Mykola Lysenkoa68d2432015-12-07 13:55:51 -05001889 reply.reply_type = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001890 reply.req_type = req_type;
1891 drm_dp_encode_sideband_reply(&reply, msg);
1892 return 0;
1893}
1894
1895static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1896 struct drm_dp_mst_branch *mstb,
1897 int req_type, int seqno, bool broadcast)
1898{
1899 struct drm_dp_sideband_msg_tx *txmsg;
1900
1901 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1902 if (!txmsg)
1903 return -ENOMEM;
1904
1905 txmsg->dst = mstb;
1906 txmsg->seqno = seqno;
1907 drm_dp_encode_up_ack_reply(txmsg, req_type);
1908
1909 mutex_lock(&mgr->qlock);
1910 list_add_tail(&txmsg->next, &mgr->tx_msg_upq);
1911 if (!mgr->tx_up_in_progress) {
1912 process_single_up_tx_qlock(mgr);
1913 }
1914 mutex_unlock(&mgr->qlock);
1915 return 0;
1916}
1917
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001918static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1919 int dp_link_count,
1920 int *out)
Dave Airliead7f8a12014-06-05 14:01:32 +10001921{
1922 switch (dp_link_bw) {
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001923 default:
1924 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1925 dp_link_bw, dp_link_count);
1926 return false;
1927
Dave Airliead7f8a12014-06-05 14:01:32 +10001928 case DP_LINK_BW_1_62:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001929 *out = 3 * dp_link_count;
1930 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10001931 case DP_LINK_BW_2_7:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001932 *out = 5 * dp_link_count;
1933 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10001934 case DP_LINK_BW_5_4:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001935 *out = 10 * dp_link_count;
1936 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10001937 }
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001938 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001939}
1940
1941/**
1942 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
1943 * @mgr: manager to set state for
1944 * @mst_state: true to enable MST on this connector - false to disable.
1945 *
1946 * This is called by the driver when it detects an MST capable device plugged
1947 * into a DP MST capable port, or when a DP MST capable device is unplugged.
1948 */
1949int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
1950{
1951 int ret = 0;
1952 struct drm_dp_mst_branch *mstb = NULL;
1953
1954 mutex_lock(&mgr->lock);
1955 if (mst_state == mgr->mst_state)
1956 goto out_unlock;
1957
1958 mgr->mst_state = mst_state;
1959 /* set the device into MST mode */
1960 if (mst_state) {
1961 WARN_ON(mgr->mst_primary);
1962
1963 /* get dpcd info */
1964 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
1965 if (ret != DP_RECEIVER_CAP_SIZE) {
1966 DRM_DEBUG_KMS("failed to read DPCD\n");
1967 goto out_unlock;
1968 }
1969
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001970 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
1971 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
1972 &mgr->pbn_div)) {
1973 ret = -EINVAL;
1974 goto out_unlock;
1975 }
1976
Dave Airliead7f8a12014-06-05 14:01:32 +10001977 mgr->total_pbn = 2560;
1978 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
1979 mgr->avail_slots = mgr->total_slots;
1980
1981 /* add initial branch device at LCT 1 */
1982 mstb = drm_dp_add_mst_branch_device(1, NULL);
1983 if (mstb == NULL) {
1984 ret = -ENOMEM;
1985 goto out_unlock;
1986 }
1987 mstb->mgr = mgr;
1988
1989 /* give this the main reference */
1990 mgr->mst_primary = mstb;
1991 kref_get(&mgr->mst_primary->kref);
1992
1993 {
1994 struct drm_dp_payload reset_pay;
1995 reset_pay.start_slot = 0;
1996 reset_pay.num_slots = 0x3f;
1997 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
1998 }
1999
2000 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2001 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2002 if (ret < 0) {
2003 goto out_unlock;
2004 }
2005
2006
2007 /* sort out guid */
2008 ret = drm_dp_dpcd_read(mgr->aux, DP_GUID, mgr->guid, 16);
2009 if (ret != 16) {
2010 DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret);
2011 goto out_unlock;
2012 }
2013
2014 mgr->guid_valid = drm_dp_validate_guid(mgr, mgr->guid);
2015 if (!mgr->guid_valid) {
2016 ret = drm_dp_dpcd_write(mgr->aux, DP_GUID, mgr->guid, 16);
2017 mgr->guid_valid = true;
2018 }
2019
2020 queue_work(system_long_wq, &mgr->work);
2021
2022 ret = 0;
2023 } else {
2024 /* disable MST on the device */
2025 mstb = mgr->mst_primary;
2026 mgr->mst_primary = NULL;
2027 /* this can fail if the device is gone */
2028 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2029 ret = 0;
2030 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2031 mgr->payload_mask = 0;
2032 set_bit(0, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10002033 mgr->vcpi_mask = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002034 }
2035
2036out_unlock:
2037 mutex_unlock(&mgr->lock);
2038 if (mstb)
2039 drm_dp_put_mst_branch_device(mstb);
2040 return ret;
2041
2042}
2043EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2044
2045/**
2046 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2047 * @mgr: manager to suspend
2048 *
2049 * This function tells the MST device that we can't handle UP messages
2050 * anymore. This should stop it from sending any since we are suspended.
2051 */
2052void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2053{
2054 mutex_lock(&mgr->lock);
2055 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2056 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2057 mutex_unlock(&mgr->lock);
Dave Airlie274d8352015-09-30 10:39:42 +10002058 flush_work(&mgr->work);
2059 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002060}
2061EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2062
2063/**
2064 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2065 * @mgr: manager to resume
2066 *
2067 * This will fetch DPCD and see if the device is still there,
2068 * if it is, it will rewrite the MSTM control bits, and return.
2069 *
2070 * if the device fails this returns -1, and the driver should do
2071 * a full MST reprobe, in case we were undocked.
2072 */
2073int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2074{
2075 int ret = 0;
2076
2077 mutex_lock(&mgr->lock);
2078
2079 if (mgr->mst_primary) {
2080 int sret;
2081 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2082 if (sret != DP_RECEIVER_CAP_SIZE) {
2083 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2084 ret = -1;
2085 goto out_unlock;
2086 }
2087
2088 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2089 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2090 if (ret < 0) {
2091 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2092 ret = -1;
2093 goto out_unlock;
2094 }
2095 ret = 0;
2096 } else
2097 ret = -1;
2098
2099out_unlock:
2100 mutex_unlock(&mgr->lock);
2101 return ret;
2102}
2103EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2104
2105static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2106{
2107 int len;
2108 u8 replyblock[32];
2109 int replylen, origlen, curreply;
2110 int ret;
2111 struct drm_dp_sideband_msg_rx *msg;
2112 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2113 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2114
2115 len = min(mgr->max_dpcd_transaction_bytes, 16);
2116 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2117 replyblock, len);
2118 if (ret != len) {
2119 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2120 return;
2121 }
2122 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2123 if (!ret) {
2124 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2125 return;
2126 }
2127 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2128
2129 origlen = replylen;
2130 replylen -= len;
2131 curreply = len;
2132 while (replylen > 0) {
2133 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2134 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2135 replyblock, len);
2136 if (ret != len) {
2137 DRM_DEBUG_KMS("failed to read a chunk\n");
2138 }
2139 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2140 if (ret == false)
2141 DRM_DEBUG_KMS("failed to build sideband msg\n");
2142 curreply += len;
2143 replylen -= len;
2144 }
2145}
2146
2147static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2148{
2149 int ret = 0;
2150
2151 drm_dp_get_one_sb_msg(mgr, false);
2152
2153 if (mgr->down_rep_recv.have_eomt) {
2154 struct drm_dp_sideband_msg_tx *txmsg;
2155 struct drm_dp_mst_branch *mstb;
2156 int slot = -1;
2157 mstb = drm_dp_get_mst_branch_device(mgr,
2158 mgr->down_rep_recv.initial_hdr.lct,
2159 mgr->down_rep_recv.initial_hdr.rad);
2160
2161 if (!mstb) {
2162 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2163 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2164 return 0;
2165 }
2166
2167 /* find the message */
2168 slot = mgr->down_rep_recv.initial_hdr.seqno;
2169 mutex_lock(&mgr->qlock);
2170 txmsg = mstb->tx_slots[slot];
2171 /* remove from slots */
2172 mutex_unlock(&mgr->qlock);
2173
2174 if (!txmsg) {
2175 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2176 mstb,
2177 mgr->down_rep_recv.initial_hdr.seqno,
2178 mgr->down_rep_recv.initial_hdr.lct,
2179 mgr->down_rep_recv.initial_hdr.rad[0],
2180 mgr->down_rep_recv.msg[0]);
2181 drm_dp_put_mst_branch_device(mstb);
2182 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2183 return 0;
2184 }
2185
2186 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2187 if (txmsg->reply.reply_type == 1) {
2188 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);
2189 }
2190
2191 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2192 drm_dp_put_mst_branch_device(mstb);
2193
2194 mutex_lock(&mgr->qlock);
2195 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2196 mstb->tx_slots[slot] = NULL;
2197 mutex_unlock(&mgr->qlock);
2198
2199 wake_up(&mgr->tx_waitq);
2200 }
2201 return ret;
2202}
2203
2204static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2205{
2206 int ret = 0;
2207 drm_dp_get_one_sb_msg(mgr, true);
2208
2209 if (mgr->up_req_recv.have_eomt) {
2210 struct drm_dp_sideband_msg_req_body msg;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002211 struct drm_dp_mst_branch *mstb = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002212 bool seqno;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002213
2214 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2215 mstb = drm_dp_get_mst_branch_device(mgr,
2216 mgr->up_req_recv.initial_hdr.lct,
2217 mgr->up_req_recv.initial_hdr.rad);
2218 if (!mstb) {
2219 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2220 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2221 return 0;
2222 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002223 }
2224
2225 seqno = mgr->up_req_recv.initial_hdr.seqno;
2226 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2227
2228 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002229 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2230
2231 if (!mstb)
2232 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2233
2234 if (!mstb) {
2235 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2236 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2237 return 0;
2238 }
2239
Dave Airliead7f8a12014-06-05 14:01:32 +10002240 drm_dp_update_port(mstb, &msg.u.conn_stat);
2241 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);
2242 (*mgr->cbs->hotplug)(mgr);
2243
2244 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002245 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2246 if (!mstb)
2247 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2248
2249 if (!mstb) {
2250 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2251 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2252 return 0;
2253 }
2254
Dave Airliead7f8a12014-06-05 14:01:32 +10002255 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2256 }
2257
2258 drm_dp_put_mst_branch_device(mstb);
2259 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2260 }
2261 return ret;
2262}
2263
2264/**
2265 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2266 * @mgr: manager to notify irq for.
2267 * @esi: 4 bytes from SINK_COUNT_ESI
Daniel Vetter295ee852014-07-30 14:23:44 +02002268 * @handled: whether the hpd interrupt was consumed or not
Dave Airliead7f8a12014-06-05 14:01:32 +10002269 *
2270 * This should be called from the driver when it detects a short IRQ,
2271 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2272 * topology manager will process the sideband messages received as a result
2273 * of this.
2274 */
2275int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2276{
2277 int ret = 0;
2278 int sc;
2279 *handled = false;
2280 sc = esi[0] & 0x3f;
2281
2282 if (sc != mgr->sink_count) {
2283 mgr->sink_count = sc;
2284 *handled = true;
2285 }
2286
2287 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2288 ret = drm_dp_mst_handle_down_rep(mgr);
2289 *handled = true;
2290 }
2291
2292 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2293 ret |= drm_dp_mst_handle_up_req(mgr);
2294 *handled = true;
2295 }
2296
2297 drm_dp_mst_kick_tx(mgr);
2298 return ret;
2299}
2300EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2301
2302/**
2303 * drm_dp_mst_detect_port() - get connection status for an MST port
2304 * @mgr: manager for this port
2305 * @port: unverified pointer to a port
2306 *
2307 * This returns the current connection state for a port. It validates the
2308 * port pointer still exists so the caller doesn't require a reference
2309 */
Dave Airliec6a0aed2014-10-20 16:28:02 +10002310enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2311 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10002312{
2313 enum drm_connector_status status = connector_status_disconnected;
2314
2315 /* we need to search for the port in the mgr in case its gone */
2316 port = drm_dp_get_validated_port_ref(mgr, port);
2317 if (!port)
2318 return connector_status_disconnected;
2319
2320 if (!port->ddps)
2321 goto out;
2322
2323 switch (port->pdt) {
2324 case DP_PEER_DEVICE_NONE:
2325 case DP_PEER_DEVICE_MST_BRANCHING:
2326 break;
2327
2328 case DP_PEER_DEVICE_SST_SINK:
2329 status = connector_status_connected;
Dave Airliec6a0aed2014-10-20 16:28:02 +10002330 /* for logical ports - cache the EDID */
2331 if (port->port_num >= 8 && !port->cached_edid) {
2332 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2333 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002334 break;
2335 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2336 if (port->ldps)
2337 status = connector_status_connected;
2338 break;
2339 }
2340out:
2341 drm_dp_put_port(port);
2342 return status;
2343}
2344EXPORT_SYMBOL(drm_dp_mst_detect_port);
2345
2346/**
Libin Yangef8f9be2015-12-02 14:09:43 +08002347 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2348 * @mgr: manager for this port
2349 * @port: unverified pointer to a port.
2350 *
2351 * This returns whether the port supports audio or not.
2352 */
2353bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2354 struct drm_dp_mst_port *port)
2355{
2356 bool ret = false;
2357
2358 port = drm_dp_get_validated_port_ref(mgr, port);
2359 if (!port)
2360 return ret;
2361 ret = port->has_audio;
2362 drm_dp_put_port(port);
2363 return ret;
2364}
2365EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2366
2367/**
Dave Airliead7f8a12014-06-05 14:01:32 +10002368 * drm_dp_mst_get_edid() - get EDID for an MST port
2369 * @connector: toplevel connector to get EDID for
2370 * @mgr: manager for this port
2371 * @port: unverified pointer to a port.
2372 *
2373 * This returns an EDID for the port connected to a connector,
2374 * It validates the pointer still exists so the caller doesn't require a
2375 * reference.
2376 */
2377struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2378{
2379 struct edid *edid = NULL;
2380
2381 /* we need to search for the port in the mgr in case its gone */
2382 port = drm_dp_get_validated_port_ref(mgr, port);
2383 if (!port)
2384 return NULL;
2385
Dave Airliec6a0aed2014-10-20 16:28:02 +10002386 if (port->cached_edid)
2387 edid = drm_edid_duplicate(port->cached_edid);
Dave Airlied9515c52015-09-16 17:55:23 +10002388 else {
Dave Airliec6a0aed2014-10-20 16:28:02 +10002389 edid = drm_get_edid(connector, &port->aux.ddc);
Dave Airlied9515c52015-09-16 17:55:23 +10002390 drm_mode_connector_set_tile_property(connector);
2391 }
Libin Yangef8f9be2015-12-02 14:09:43 +08002392 port->has_audio = drm_detect_monitor_audio(edid);
Dave Airliead7f8a12014-06-05 14:01:32 +10002393 drm_dp_put_port(port);
2394 return edid;
2395}
2396EXPORT_SYMBOL(drm_dp_mst_get_edid);
2397
2398/**
2399 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2400 * @mgr: manager to use
2401 * @pbn: payload bandwidth to convert into slots.
2402 */
2403int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2404 int pbn)
2405{
2406 int num_slots;
2407
2408 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2409
2410 if (num_slots > mgr->avail_slots)
2411 return -ENOSPC;
2412 return num_slots;
2413}
2414EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2415
2416static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2417 struct drm_dp_vcpi *vcpi, int pbn)
2418{
2419 int num_slots;
2420 int ret;
2421
2422 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2423
2424 if (num_slots > mgr->avail_slots)
2425 return -ENOSPC;
2426
2427 vcpi->pbn = pbn;
2428 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2429 vcpi->num_slots = num_slots;
2430
2431 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2432 if (ret < 0)
2433 return ret;
2434 return 0;
2435}
2436
2437/**
2438 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2439 * @mgr: manager for this port
2440 * @port: port to allocate a virtual channel for.
2441 * @pbn: payload bandwidth number to request
2442 * @slots: returned number of slots for this PBN.
2443 */
2444bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2445{
2446 int ret;
2447
2448 port = drm_dp_get_validated_port_ref(mgr, port);
2449 if (!port)
2450 return false;
2451
2452 if (port->vcpi.vcpi > 0) {
2453 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2454 if (pbn == port->vcpi.pbn) {
2455 *slots = port->vcpi.num_slots;
2456 return true;
2457 }
2458 }
2459
2460 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2461 if (ret) {
2462 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2463 goto out;
2464 }
2465 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2466 *slots = port->vcpi.num_slots;
2467
2468 drm_dp_put_port(port);
2469 return true;
2470out:
2471 return false;
2472}
2473EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2474
Dave Airlie87f59422015-02-24 09:23:55 +10002475int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2476{
2477 int slots = 0;
2478 port = drm_dp_get_validated_port_ref(mgr, port);
2479 if (!port)
2480 return slots;
2481
2482 slots = port->vcpi.num_slots;
2483 drm_dp_put_port(port);
2484 return slots;
2485}
2486EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2487
Dave Airliead7f8a12014-06-05 14:01:32 +10002488/**
2489 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2490 * @mgr: manager for this port
2491 * @port: unverified pointer to a port.
2492 *
2493 * This just resets the number of slots for the ports VCPI for later programming.
2494 */
2495void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2496{
2497 port = drm_dp_get_validated_port_ref(mgr, port);
2498 if (!port)
2499 return;
2500 port->vcpi.num_slots = 0;
2501 drm_dp_put_port(port);
2502}
2503EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2504
2505/**
2506 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2507 * @mgr: manager for this port
2508 * @port: unverified port to deallocate vcpi for
2509 */
2510void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2511{
2512 port = drm_dp_get_validated_port_ref(mgr, port);
2513 if (!port)
2514 return;
2515
2516 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2517 port->vcpi.num_slots = 0;
2518 port->vcpi.pbn = 0;
2519 port->vcpi.aligned_pbn = 0;
2520 port->vcpi.vcpi = 0;
2521 drm_dp_put_port(port);
2522}
2523EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2524
2525static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2526 int id, struct drm_dp_payload *payload)
2527{
2528 u8 payload_alloc[3], status;
2529 int ret;
2530 int retries = 0;
2531
2532 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2533 DP_PAYLOAD_TABLE_UPDATED);
2534
2535 payload_alloc[0] = id;
2536 payload_alloc[1] = payload->start_slot;
2537 payload_alloc[2] = payload->num_slots;
2538
2539 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2540 if (ret != 3) {
2541 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2542 goto fail;
2543 }
2544
2545retry:
2546 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2547 if (ret < 0) {
2548 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2549 goto fail;
2550 }
2551
2552 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2553 retries++;
2554 if (retries < 20) {
2555 usleep_range(10000, 20000);
2556 goto retry;
2557 }
2558 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2559 ret = -EINVAL;
2560 goto fail;
2561 }
2562 ret = 0;
2563fail:
2564 return ret;
2565}
2566
2567
2568/**
2569 * drm_dp_check_act_status() - Check ACT handled status.
2570 * @mgr: manager to use
2571 *
2572 * Check the payload status bits in the DPCD for ACT handled completion.
2573 */
2574int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2575{
2576 u8 status;
2577 int ret;
2578 int count = 0;
2579
2580 do {
2581 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2582
2583 if (ret < 0) {
2584 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2585 goto fail;
2586 }
2587
2588 if (status & DP_PAYLOAD_ACT_HANDLED)
2589 break;
2590 count++;
2591 udelay(100);
2592
2593 } while (count < 30);
2594
2595 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2596 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2597 ret = -EINVAL;
2598 goto fail;
2599 }
2600 return 0;
2601fail:
2602 return ret;
2603}
2604EXPORT_SYMBOL(drm_dp_check_act_status);
2605
2606/**
2607 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2608 * @clock: dot clock for the mode
2609 * @bpp: bpp for the mode.
2610 *
2611 * This uses the formula in the spec to calculate the PBN value for a mode.
2612 */
2613int drm_dp_calc_pbn_mode(int clock, int bpp)
2614{
2615 fixed20_12 pix_bw;
2616 fixed20_12 fbpp;
2617 fixed20_12 result;
2618 fixed20_12 margin, tmp;
2619 u32 res;
2620
2621 pix_bw.full = dfixed_const(clock);
2622 fbpp.full = dfixed_const(bpp);
2623 tmp.full = dfixed_const(8);
2624 fbpp.full = dfixed_div(fbpp, tmp);
2625
2626 result.full = dfixed_mul(pix_bw, fbpp);
2627 margin.full = dfixed_const(54);
2628 tmp.full = dfixed_const(64);
2629 margin.full = dfixed_div(margin, tmp);
2630 result.full = dfixed_div(result, margin);
2631
2632 margin.full = dfixed_const(1006);
2633 tmp.full = dfixed_const(1000);
2634 margin.full = dfixed_div(margin, tmp);
2635 result.full = dfixed_mul(result, margin);
2636
2637 result.full = dfixed_div(result, tmp);
2638 result.full = dfixed_ceil(result);
2639 res = dfixed_trunc(result);
2640 return res;
2641}
2642EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2643
2644static int test_calc_pbn_mode(void)
2645{
2646 int ret;
2647 ret = drm_dp_calc_pbn_mode(154000, 30);
2648 if (ret != 689)
2649 return -EINVAL;
2650 ret = drm_dp_calc_pbn_mode(234000, 30);
2651 if (ret != 1047)
2652 return -EINVAL;
2653 return 0;
2654}
2655
2656/* we want to kick the TX after we've ack the up/down IRQs. */
2657static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2658{
2659 queue_work(system_long_wq, &mgr->tx_work);
2660}
2661
2662static void drm_dp_mst_dump_mstb(struct seq_file *m,
2663 struct drm_dp_mst_branch *mstb)
2664{
2665 struct drm_dp_mst_port *port;
2666 int tabs = mstb->lct;
2667 char prefix[10];
2668 int i;
2669
2670 for (i = 0; i < tabs; i++)
2671 prefix[i] = '\t';
2672 prefix[i] = '\0';
2673
2674 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2675 list_for_each_entry(port, &mstb->ports, next) {
Libin Yangef8f9be2015-12-02 14:09:43 +08002676 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);
Dave Airliead7f8a12014-06-05 14:01:32 +10002677 if (port->mstb)
2678 drm_dp_mst_dump_mstb(m, port->mstb);
2679 }
2680}
2681
2682static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2683 char *buf)
2684{
2685 int ret;
2686 int i;
2687 for (i = 0; i < 4; i++) {
2688 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2689 if (ret != 16)
2690 break;
2691 }
2692 if (i == 4)
2693 return true;
2694 return false;
2695}
2696
2697/**
2698 * drm_dp_mst_dump_topology(): dump topology to seq file.
2699 * @m: seq_file to dump output to
2700 * @mgr: manager to dump current topology for.
2701 *
2702 * helper to dump MST topology to a seq file for debugfs.
2703 */
2704void drm_dp_mst_dump_topology(struct seq_file *m,
2705 struct drm_dp_mst_topology_mgr *mgr)
2706{
2707 int i;
2708 struct drm_dp_mst_port *port;
2709 mutex_lock(&mgr->lock);
2710 if (mgr->mst_primary)
2711 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2712
2713 /* dump VCPIs */
2714 mutex_unlock(&mgr->lock);
2715
2716 mutex_lock(&mgr->payload_lock);
Dave Airliedfda0df2014-08-06 16:26:21 +10002717 seq_printf(m, "vcpi: %lx %lx\n", mgr->payload_mask, mgr->vcpi_mask);
Dave Airliead7f8a12014-06-05 14:01:32 +10002718
2719 for (i = 0; i < mgr->max_payloads; i++) {
2720 if (mgr->proposed_vcpis[i]) {
2721 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2722 seq_printf(m, "vcpi %d: %d %d %d\n", i, port->port_num, port->vcpi.vcpi, port->vcpi.num_slots);
2723 } else
2724 seq_printf(m, "vcpi %d:unsed\n", i);
2725 }
2726 for (i = 0; i < mgr->max_payloads; i++) {
2727 seq_printf(m, "payload %d: %d, %d, %d\n",
2728 i,
2729 mgr->payloads[i].payload_state,
2730 mgr->payloads[i].start_slot,
2731 mgr->payloads[i].num_slots);
2732
2733
2734 }
2735 mutex_unlock(&mgr->payload_lock);
2736
2737 mutex_lock(&mgr->lock);
2738 if (mgr->mst_primary) {
2739 u8 buf[64];
2740 bool bret;
2741 int ret;
2742 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2743 seq_printf(m, "dpcd: ");
2744 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2745 seq_printf(m, "%02x ", buf[i]);
2746 seq_printf(m, "\n");
2747 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2748 seq_printf(m, "faux/mst: ");
2749 for (i = 0; i < 2; i++)
2750 seq_printf(m, "%02x ", buf[i]);
2751 seq_printf(m, "\n");
2752 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2753 seq_printf(m, "mst ctrl: ");
2754 for (i = 0; i < 1; i++)
2755 seq_printf(m, "%02x ", buf[i]);
2756 seq_printf(m, "\n");
2757
Dave Airlie44790462015-07-14 11:33:31 +10002758 /* dump the standard OUI branch header */
2759 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2760 seq_printf(m, "branch oui: ");
2761 for (i = 0; i < 0x3; i++)
2762 seq_printf(m, "%02x", buf[i]);
2763 seq_printf(m, " devid: ");
2764 for (i = 0x3; i < 0x8; i++)
2765 seq_printf(m, "%c", buf[i]);
2766 seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2767 seq_printf(m, "\n");
Dave Airliead7f8a12014-06-05 14:01:32 +10002768 bret = dump_dp_payload_table(mgr, buf);
2769 if (bret == true) {
2770 seq_printf(m, "payload table: ");
2771 for (i = 0; i < 63; i++)
2772 seq_printf(m, "%02x ", buf[i]);
2773 seq_printf(m, "\n");
2774 }
2775
2776 }
2777
2778 mutex_unlock(&mgr->lock);
2779
2780}
2781EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2782
2783static void drm_dp_tx_work(struct work_struct *work)
2784{
2785 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2786
2787 mutex_lock(&mgr->qlock);
2788 if (mgr->tx_down_in_progress)
2789 process_single_down_tx_qlock(mgr);
2790 mutex_unlock(&mgr->qlock);
2791}
2792
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002793static void drm_dp_destroy_connector_work(struct work_struct *work)
2794{
2795 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002796 struct drm_dp_mst_port *port;
Dave Airliedf4839f2015-09-16 10:37:28 +10002797 bool send_hotplug = false;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002798 /*
2799 * Not a regular list traverse as we have to drop the destroy
2800 * connector lock before destroying the connector, to avoid AB->BA
2801 * ordering between this lock and the config mutex.
2802 */
2803 for (;;) {
2804 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002805 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2806 if (!port) {
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002807 mutex_unlock(&mgr->destroy_connector_lock);
2808 break;
2809 }
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002810 list_del(&port->next);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002811 mutex_unlock(&mgr->destroy_connector_lock);
2812
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002813 mgr->cbs->destroy_connector(mgr, port->connector);
2814
2815 drm_dp_port_teardown_pdt(port, port->pdt);
2816
2817 if (!port->input && port->vcpi.vcpi > 0)
2818 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2819 kfree(port);
Dave Airliedf4839f2015-09-16 10:37:28 +10002820 send_hotplug = true;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002821 }
Dave Airliedf4839f2015-09-16 10:37:28 +10002822 if (send_hotplug)
2823 (*mgr->cbs->hotplug)(mgr);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002824}
2825
Dave Airliead7f8a12014-06-05 14:01:32 +10002826/**
2827 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2828 * @mgr: manager struct to initialise
2829 * @dev: device providing this structure - for i2c addition.
2830 * @aux: DP helper aux channel to talk to this device
2831 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2832 * @max_payloads: maximum number of payloads this GPU can source
2833 * @conn_base_id: the connector object ID the MST device is connected to.
2834 *
2835 * Return 0 for success, or negative error code on failure
2836 */
2837int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2838 struct device *dev, struct drm_dp_aux *aux,
2839 int max_dpcd_transaction_bytes,
2840 int max_payloads, int conn_base_id)
2841{
2842 mutex_init(&mgr->lock);
2843 mutex_init(&mgr->qlock);
2844 mutex_init(&mgr->payload_lock);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002845 mutex_init(&mgr->destroy_connector_lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10002846 INIT_LIST_HEAD(&mgr->tx_msg_upq);
2847 INIT_LIST_HEAD(&mgr->tx_msg_downq);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002848 INIT_LIST_HEAD(&mgr->destroy_connector_list);
Dave Airliead7f8a12014-06-05 14:01:32 +10002849 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2850 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002851 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002852 init_waitqueue_head(&mgr->tx_waitq);
2853 mgr->dev = dev;
2854 mgr->aux = aux;
2855 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2856 mgr->max_payloads = max_payloads;
2857 mgr->conn_base_id = conn_base_id;
2858 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
2859 if (!mgr->payloads)
2860 return -ENOMEM;
2861 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
2862 if (!mgr->proposed_vcpis)
2863 return -ENOMEM;
2864 set_bit(0, &mgr->payload_mask);
2865 test_calc_pbn_mode();
2866 return 0;
2867}
2868EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
2869
2870/**
2871 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
2872 * @mgr: manager to destroy
2873 */
2874void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
2875{
Dave Airlie274d8352015-09-30 10:39:42 +10002876 flush_work(&mgr->work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002877 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002878 mutex_lock(&mgr->payload_lock);
2879 kfree(mgr->payloads);
2880 mgr->payloads = NULL;
2881 kfree(mgr->proposed_vcpis);
2882 mgr->proposed_vcpis = NULL;
2883 mutex_unlock(&mgr->payload_lock);
2884 mgr->dev = NULL;
2885 mgr->aux = NULL;
2886}
2887EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
2888
2889/* I2C device */
2890static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
2891 int num)
2892{
2893 struct drm_dp_aux *aux = adapter->algo_data;
2894 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
2895 struct drm_dp_mst_branch *mstb;
2896 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2897 unsigned int i;
2898 bool reading = false;
2899 struct drm_dp_sideband_msg_req_body msg;
2900 struct drm_dp_sideband_msg_tx *txmsg = NULL;
2901 int ret;
2902
2903 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
2904 if (!mstb)
2905 return -EREMOTEIO;
2906
2907 /* construct i2c msg */
2908 /* see if last msg is a read */
2909 if (msgs[num - 1].flags & I2C_M_RD)
2910 reading = true;
2911
Dave Airlieae491542015-10-14 18:51:17 +10002912 if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002913 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
2914 ret = -EIO;
2915 goto out;
2916 }
2917
Dave Airlieae491542015-10-14 18:51:17 +10002918 memset(&msg, 0, sizeof(msg));
Dave Airliead7f8a12014-06-05 14:01:32 +10002919 msg.req_type = DP_REMOTE_I2C_READ;
2920 msg.u.i2c_read.num_transactions = num - 1;
2921 msg.u.i2c_read.port_number = port->port_num;
2922 for (i = 0; i < num - 1; i++) {
2923 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
2924 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
2925 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
2926 }
2927 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
2928 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
2929
2930 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2931 if (!txmsg) {
2932 ret = -ENOMEM;
2933 goto out;
2934 }
2935
2936 txmsg->dst = mstb;
2937 drm_dp_encode_sideband_req(&msg, txmsg);
2938
2939 drm_dp_queue_down_tx(mgr, txmsg);
2940
2941 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2942 if (ret > 0) {
2943
2944 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
2945 ret = -EREMOTEIO;
2946 goto out;
2947 }
2948 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
2949 ret = -EIO;
2950 goto out;
2951 }
2952 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
2953 ret = num;
2954 }
2955out:
2956 kfree(txmsg);
2957 drm_dp_put_mst_branch_device(mstb);
2958 return ret;
2959}
2960
2961static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
2962{
2963 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
2964 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
2965 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
2966 I2C_FUNC_10BIT_ADDR;
2967}
2968
2969static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
2970 .functionality = drm_dp_mst_i2c_functionality,
2971 .master_xfer = drm_dp_mst_i2c_xfer,
2972};
2973
2974/**
2975 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
2976 * @aux: DisplayPort AUX channel
2977 *
2978 * Returns 0 on success or a negative error code on failure.
2979 */
2980static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
2981{
2982 aux->ddc.algo = &drm_dp_mst_i2c_algo;
2983 aux->ddc.algo_data = aux;
2984 aux->ddc.retries = 3;
2985
2986 aux->ddc.class = I2C_CLASS_DDC;
2987 aux->ddc.owner = THIS_MODULE;
2988 aux->ddc.dev.parent = aux->dev;
2989 aux->ddc.dev.of_node = aux->dev->of_node;
2990
2991 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
2992 sizeof(aux->ddc.name));
2993
2994 return i2c_add_adapter(&aux->ddc);
2995}
2996
2997/**
2998 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
2999 * @aux: DisplayPort AUX channel
3000 */
3001static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3002{
3003 i2c_del_adapter(&aux->ddc);
3004}