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