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