blob: db7890cb254ed93df1412d3ebd9a1735ff213d30 [file] [log] [blame]
Dave Airliead7f8a12014-06-05 14:01:32 +10001/*
2 * Copyright © 2014 Red Hat
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/init.h>
26#include <linux/errno.h>
27#include <linux/sched.h>
Dave Airlie75bc08a2014-07-09 11:15:09 +100028#include <linux/seq_file.h>
Dave Airliead7f8a12014-06-05 14:01:32 +100029#include <linux/i2c.h>
30#include <drm/drm_dp_mst_helper.h>
31#include <drm/drmP.h>
32
33#include <drm/drm_fixed.h>
34
35/**
36 * DOC: dp mst helper
37 *
38 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
39 * protocol. The helpers contain a topology manager and bandwidth manager.
40 * The helpers encapsulate the sending and received of sideband msgs.
41 */
42static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
43 char *buf);
44static int test_calc_pbn_mode(void);
45
46static void drm_dp_put_port(struct drm_dp_mst_port *port);
47
48static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
49 int id,
50 struct drm_dp_payload *payload);
51
52static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
53 struct drm_dp_mst_port *port,
54 int offset, int size, u8 *bytes);
55
Dave Airlie68d8c9f2015-09-06 18:53:00 +100056static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
57 struct drm_dp_mst_branch *mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +100058static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
59 struct drm_dp_mst_branch *mstb,
60 struct drm_dp_mst_port *port);
61static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
62 u8 *guid);
63
64static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
65static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
66static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
67/* sideband msg handling */
68static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
69{
70 u8 bitmask = 0x80;
71 u8 bitshift = 7;
72 u8 array_index = 0;
73 int number_of_bits = num_nibbles * 4;
74 u8 remainder = 0;
75
76 while (number_of_bits != 0) {
77 number_of_bits--;
78 remainder <<= 1;
79 remainder |= (data[array_index] & bitmask) >> bitshift;
80 bitmask >>= 1;
81 bitshift--;
82 if (bitmask == 0) {
83 bitmask = 0x80;
84 bitshift = 7;
85 array_index++;
86 }
87 if ((remainder & 0x10) == 0x10)
88 remainder ^= 0x13;
89 }
90
91 number_of_bits = 4;
92 while (number_of_bits != 0) {
93 number_of_bits--;
94 remainder <<= 1;
95 if ((remainder & 0x10) != 0)
96 remainder ^= 0x13;
97 }
98
99 return remainder;
100}
101
102static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
103{
104 u8 bitmask = 0x80;
105 u8 bitshift = 7;
106 u8 array_index = 0;
107 int number_of_bits = number_of_bytes * 8;
108 u16 remainder = 0;
109
110 while (number_of_bits != 0) {
111 number_of_bits--;
112 remainder <<= 1;
113 remainder |= (data[array_index] & bitmask) >> bitshift;
114 bitmask >>= 1;
115 bitshift--;
116 if (bitmask == 0) {
117 bitmask = 0x80;
118 bitshift = 7;
119 array_index++;
120 }
121 if ((remainder & 0x100) == 0x100)
122 remainder ^= 0xd5;
123 }
124
125 number_of_bits = 8;
126 while (number_of_bits != 0) {
127 number_of_bits--;
128 remainder <<= 1;
129 if ((remainder & 0x100) != 0)
130 remainder ^= 0xd5;
131 }
132
133 return remainder & 0xff;
134}
135static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
136{
137 u8 size = 3;
138 size += (hdr->lct / 2);
139 return size;
140}
141
142static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
143 u8 *buf, int *len)
144{
145 int idx = 0;
146 int i;
147 u8 crc4;
148 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
149 for (i = 0; i < (hdr->lct / 2); i++)
150 buf[idx++] = hdr->rad[i];
151 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
152 (hdr->msg_len & 0x3f);
153 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
154
155 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
156 buf[idx - 1] |= (crc4 & 0xf);
157
158 *len = idx;
159}
160
161static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
162 u8 *buf, int buflen, u8 *hdrlen)
163{
164 u8 crc4;
165 u8 len;
166 int i;
167 u8 idx;
168 if (buf[0] == 0)
169 return false;
170 len = 3;
171 len += ((buf[0] & 0xf0) >> 4) / 2;
172 if (len > buflen)
173 return false;
174 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
175
176 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
177 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
178 return false;
179 }
180
181 hdr->lct = (buf[0] & 0xf0) >> 4;
182 hdr->lcr = (buf[0] & 0xf);
183 idx = 1;
184 for (i = 0; i < (hdr->lct / 2); i++)
185 hdr->rad[i] = buf[idx++];
186 hdr->broadcast = (buf[idx] >> 7) & 0x1;
187 hdr->path_msg = (buf[idx] >> 6) & 0x1;
188 hdr->msg_len = buf[idx] & 0x3f;
189 idx++;
190 hdr->somt = (buf[idx] >> 7) & 0x1;
191 hdr->eomt = (buf[idx] >> 6) & 0x1;
192 hdr->seqno = (buf[idx] >> 4) & 0x1;
193 idx++;
194 *hdrlen = idx;
195 return true;
196}
197
198static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
199 struct drm_dp_sideband_msg_tx *raw)
200{
201 int idx = 0;
202 int i;
203 u8 *buf = raw->msg;
204 buf[idx++] = req->req_type & 0x7f;
205
206 switch (req->req_type) {
207 case DP_ENUM_PATH_RESOURCES:
208 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
209 idx++;
210 break;
211 case DP_ALLOCATE_PAYLOAD:
212 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
213 (req->u.allocate_payload.number_sdp_streams & 0xf);
214 idx++;
215 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
216 idx++;
217 buf[idx] = (req->u.allocate_payload.pbn >> 8);
218 idx++;
219 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
220 idx++;
221 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
222 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
223 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
224 idx++;
225 }
226 if (req->u.allocate_payload.number_sdp_streams & 1) {
227 i = req->u.allocate_payload.number_sdp_streams - 1;
228 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
229 idx++;
230 }
231 break;
232 case DP_QUERY_PAYLOAD:
233 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
234 idx++;
235 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
236 idx++;
237 break;
238 case DP_REMOTE_DPCD_READ:
239 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
240 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
241 idx++;
242 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
243 idx++;
244 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
245 idx++;
246 buf[idx] = (req->u.dpcd_read.num_bytes);
247 idx++;
248 break;
249
250 case DP_REMOTE_DPCD_WRITE:
251 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
252 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
253 idx++;
254 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
255 idx++;
256 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
257 idx++;
258 buf[idx] = (req->u.dpcd_write.num_bytes);
259 idx++;
260 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
261 idx += req->u.dpcd_write.num_bytes;
262 break;
263 case DP_REMOTE_I2C_READ:
264 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
265 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
266 idx++;
267 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
268 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
269 idx++;
270 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
271 idx++;
272 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
273 idx += req->u.i2c_read.transactions[i].num_bytes;
274
275 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
276 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
277 idx++;
278 }
279 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
280 idx++;
281 buf[idx] = (req->u.i2c_read.num_bytes_read);
282 idx++;
283 break;
284
285 case DP_REMOTE_I2C_WRITE:
286 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
287 idx++;
288 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
289 idx++;
290 buf[idx] = (req->u.i2c_write.num_bytes);
291 idx++;
292 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
293 idx += req->u.i2c_write.num_bytes;
294 break;
295 }
296 raw->cur_len = idx;
297}
298
299static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
300{
301 u8 crc4;
302 crc4 = drm_dp_msg_data_crc4(msg, len);
303 msg[len] = crc4;
304}
305
306static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
307 struct drm_dp_sideband_msg_tx *raw)
308{
309 int idx = 0;
310 u8 *buf = raw->msg;
311
312 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
313
314 raw->cur_len = idx;
315}
316
317/* this adds a chunk of msg to the builder to get the final msg */
318static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
319 u8 *replybuf, u8 replybuflen, bool hdr)
320{
321 int ret;
322 u8 crc4;
323
324 if (hdr) {
325 u8 hdrlen;
326 struct drm_dp_sideband_msg_hdr recv_hdr;
327 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
328 if (ret == false) {
329 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
330 return false;
331 }
332
Imre Deaka4404252017-07-19 16:46:32 +0300333 /*
334 * ignore out-of-order messages or messages that are part of a
335 * failed transaction
336 */
337 if (!recv_hdr.somt && !msg->have_somt)
338 return false;
339
Dave Airliead7f8a12014-06-05 14:01:32 +1000340 /* get length contained in this portion */
341 msg->curchunk_len = recv_hdr.msg_len;
342 msg->curchunk_hdrlen = hdrlen;
343
344 /* we have already gotten an somt - don't bother parsing */
345 if (recv_hdr.somt && msg->have_somt)
346 return false;
347
348 if (recv_hdr.somt) {
349 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
350 msg->have_somt = true;
351 }
352 if (recv_hdr.eomt)
353 msg->have_eomt = true;
354
355 /* copy the bytes for the remainder of this header chunk */
356 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
357 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
358 } else {
359 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
360 msg->curchunk_idx += replybuflen;
361 }
362
363 if (msg->curchunk_idx >= msg->curchunk_len) {
364 /* do CRC */
365 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
366 /* copy chunk into bigger msg */
367 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
368 msg->curlen += msg->curchunk_len - 1;
369 }
370 return true;
371}
372
373static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
374 struct drm_dp_sideband_msg_reply_body *repmsg)
375{
376 int idx = 1;
377 int i;
378 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
379 idx += 16;
380 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
381 idx++;
382 if (idx > raw->curlen)
383 goto fail_len;
384 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
385 if (raw->msg[idx] & 0x80)
386 repmsg->u.link_addr.ports[i].input_port = 1;
387
388 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
389 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
390
391 idx++;
392 if (idx > raw->curlen)
393 goto fail_len;
394 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
395 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
396 if (repmsg->u.link_addr.ports[i].input_port == 0)
397 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
398 idx++;
399 if (idx > raw->curlen)
400 goto fail_len;
401 if (repmsg->u.link_addr.ports[i].input_port == 0) {
402 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
403 idx++;
404 if (idx > raw->curlen)
405 goto fail_len;
406 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
407 idx += 16;
408 if (idx > raw->curlen)
409 goto fail_len;
410 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
411 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
412 idx++;
413
414 }
415 if (idx > raw->curlen)
416 goto fail_len;
417 }
418
419 return true;
420fail_len:
421 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
422 return false;
423}
424
425static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
426 struct drm_dp_sideband_msg_reply_body *repmsg)
427{
428 int idx = 1;
429 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
430 idx++;
431 if (idx > raw->curlen)
432 goto fail_len;
433 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
434 if (idx > raw->curlen)
435 goto fail_len;
436
437 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
438 return true;
439fail_len:
440 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
441 return false;
442}
443
444static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
445 struct drm_dp_sideband_msg_reply_body *repmsg)
446{
447 int idx = 1;
448 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
449 idx++;
450 if (idx > raw->curlen)
451 goto fail_len;
452 return true;
453fail_len:
454 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
455 return false;
456}
457
458static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
459 struct drm_dp_sideband_msg_reply_body *repmsg)
460{
461 int idx = 1;
462
463 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
464 idx++;
465 if (idx > raw->curlen)
466 goto fail_len;
467 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
468 idx++;
469 /* TODO check */
470 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
471 return true;
472fail_len:
473 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
474 return false;
475}
476
477static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
478 struct drm_dp_sideband_msg_reply_body *repmsg)
479{
480 int idx = 1;
481 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
482 idx++;
483 if (idx > raw->curlen)
484 goto fail_len;
485 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
486 idx += 2;
487 if (idx > raw->curlen)
488 goto fail_len;
489 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
490 idx += 2;
491 if (idx > raw->curlen)
492 goto fail_len;
493 return true;
494fail_len:
495 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
496 return false;
497}
498
499static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
500 struct drm_dp_sideband_msg_reply_body *repmsg)
501{
502 int idx = 1;
503 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
504 idx++;
505 if (idx > raw->curlen)
506 goto fail_len;
507 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
508 idx++;
509 if (idx > raw->curlen)
510 goto fail_len;
511 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
512 idx += 2;
513 if (idx > raw->curlen)
514 goto fail_len;
515 return true;
516fail_len:
517 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
518 return false;
519}
520
521static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
522 struct drm_dp_sideband_msg_reply_body *repmsg)
523{
524 int idx = 1;
525 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
526 idx++;
527 if (idx > raw->curlen)
528 goto fail_len;
529 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
530 idx += 2;
531 if (idx > raw->curlen)
532 goto fail_len;
533 return true;
534fail_len:
535 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
536 return false;
537}
538
539static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
540 struct drm_dp_sideband_msg_reply_body *msg)
541{
542 memset(msg, 0, sizeof(*msg));
543 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
544 msg->req_type = (raw->msg[0] & 0x7f);
545
546 if (msg->reply_type) {
547 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
548 msg->u.nak.reason = raw->msg[17];
549 msg->u.nak.nak_data = raw->msg[18];
550 return false;
551 }
552
553 switch (msg->req_type) {
554 case DP_LINK_ADDRESS:
555 return drm_dp_sideband_parse_link_address(raw, msg);
556 case DP_QUERY_PAYLOAD:
557 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
558 case DP_REMOTE_DPCD_READ:
559 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
560 case DP_REMOTE_DPCD_WRITE:
561 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
562 case DP_REMOTE_I2C_READ:
563 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
564 case DP_ENUM_PATH_RESOURCES:
565 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
566 case DP_ALLOCATE_PAYLOAD:
567 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
568 default:
569 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
570 return false;
571 }
572}
573
574static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
575 struct drm_dp_sideband_msg_req_body *msg)
576{
577 int idx = 1;
578
579 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
580 idx++;
581 if (idx > raw->curlen)
582 goto fail_len;
583
584 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
585 idx += 16;
586 if (idx > raw->curlen)
587 goto fail_len;
588
589 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
590 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
591 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
592 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
593 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
594 idx++;
595 return true;
596fail_len:
597 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
598 return false;
599}
600
601static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
602 struct drm_dp_sideband_msg_req_body *msg)
603{
604 int idx = 1;
605
606 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
607 idx++;
608 if (idx > raw->curlen)
609 goto fail_len;
610
611 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
612 idx += 16;
613 if (idx > raw->curlen)
614 goto fail_len;
615
616 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
617 idx++;
618 return true;
619fail_len:
620 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
621 return false;
622}
623
624static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
625 struct drm_dp_sideband_msg_req_body *msg)
626{
627 memset(msg, 0, sizeof(*msg));
628 msg->req_type = (raw->msg[0] & 0x7f);
629
630 switch (msg->req_type) {
631 case DP_CONNECTION_STATUS_NOTIFY:
632 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
633 case DP_RESOURCE_STATUS_NOTIFY:
634 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
635 default:
636 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
637 return false;
638 }
639}
640
641static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
642{
643 struct drm_dp_sideband_msg_req_body req;
644
645 req.req_type = DP_REMOTE_DPCD_WRITE;
646 req.u.dpcd_write.port_number = port_num;
647 req.u.dpcd_write.dpcd_address = offset;
648 req.u.dpcd_write.num_bytes = num_bytes;
649 req.u.dpcd_write.bytes = bytes;
650 drm_dp_encode_sideband_req(&req, msg);
651
652 return 0;
653}
654
655static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
656{
657 struct drm_dp_sideband_msg_req_body req;
658
659 req.req_type = DP_LINK_ADDRESS;
660 drm_dp_encode_sideband_req(&req, msg);
661 return 0;
662}
663
664static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
665{
666 struct drm_dp_sideband_msg_req_body req;
667
668 req.req_type = DP_ENUM_PATH_RESOURCES;
669 req.u.port_num.port_number = port_num;
670 drm_dp_encode_sideband_req(&req, msg);
671 msg->path_msg = true;
672 return 0;
673}
674
675static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
Libin Yangef8f9be2015-12-02 14:09:43 +0800676 u8 vcpi, uint16_t pbn,
677 u8 number_sdp_streams,
678 u8 *sdp_stream_sink)
Dave Airliead7f8a12014-06-05 14:01:32 +1000679{
680 struct drm_dp_sideband_msg_req_body req;
681 memset(&req, 0, sizeof(req));
682 req.req_type = DP_ALLOCATE_PAYLOAD;
683 req.u.allocate_payload.port_number = port_num;
684 req.u.allocate_payload.vcpi = vcpi;
685 req.u.allocate_payload.pbn = pbn;
Libin Yangef8f9be2015-12-02 14:09:43 +0800686 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
687 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
688 number_sdp_streams);
Dave Airliead7f8a12014-06-05 14:01:32 +1000689 drm_dp_encode_sideband_req(&req, msg);
690 msg->path_msg = true;
691 return 0;
692}
693
694static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
695 struct drm_dp_vcpi *vcpi)
696{
Dave Airliedfda0df2014-08-06 16:26:21 +1000697 int ret, vcpi_ret;
Dave Airliead7f8a12014-06-05 14:01:32 +1000698
699 mutex_lock(&mgr->payload_lock);
700 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
701 if (ret > mgr->max_payloads) {
702 ret = -EINVAL;
703 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
704 goto out_unlock;
705 }
706
Dave Airliedfda0df2014-08-06 16:26:21 +1000707 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
708 if (vcpi_ret > mgr->max_payloads) {
709 ret = -EINVAL;
710 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
711 goto out_unlock;
712 }
713
Dave Airliead7f8a12014-06-05 14:01:32 +1000714 set_bit(ret, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +1000715 set_bit(vcpi_ret, &mgr->vcpi_mask);
716 vcpi->vcpi = vcpi_ret + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +1000717 mgr->proposed_vcpis[ret - 1] = vcpi;
718out_unlock:
719 mutex_unlock(&mgr->payload_lock);
720 return ret;
721}
722
723static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
Dave Airliedfda0df2014-08-06 16:26:21 +1000724 int vcpi)
Dave Airliead7f8a12014-06-05 14:01:32 +1000725{
Dave Airliedfda0df2014-08-06 16:26:21 +1000726 int i;
727 if (vcpi == 0)
Dave Airliead7f8a12014-06-05 14:01:32 +1000728 return;
729
730 mutex_lock(&mgr->payload_lock);
Dave Airliedfda0df2014-08-06 16:26:21 +1000731 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
732 clear_bit(vcpi - 1, &mgr->vcpi_mask);
733
734 for (i = 0; i < mgr->max_payloads; i++) {
735 if (mgr->proposed_vcpis[i])
736 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
737 mgr->proposed_vcpis[i] = NULL;
738 clear_bit(i + 1, &mgr->payload_mask);
739 }
740 }
Dave Airliead7f8a12014-06-05 14:01:32 +1000741 mutex_unlock(&mgr->payload_lock);
742}
743
744static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
745 struct drm_dp_sideband_msg_tx *txmsg)
746{
747 bool ret;
Daniel Vettercd961bb2015-01-28 10:02:23 +0100748
749 /*
750 * All updates to txmsg->state are protected by mgr->qlock, and the two
751 * cases we check here are terminal states. For those the barriers
752 * provided by the wake_up/wait_event pair are enough.
753 */
Dave Airliead7f8a12014-06-05 14:01:32 +1000754 ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
755 txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
Dave Airliead7f8a12014-06-05 14:01:32 +1000756 return ret;
757}
758
759static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
760 struct drm_dp_sideband_msg_tx *txmsg)
761{
762 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
763 int ret;
764
765 ret = wait_event_timeout(mgr->tx_waitq,
766 check_txmsg_state(mgr, txmsg),
767 (4 * HZ));
768 mutex_lock(&mstb->mgr->qlock);
769 if (ret > 0) {
770 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
771 ret = -EIO;
772 goto out;
773 }
774 } else {
775 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
776
777 /* dump some state */
778 ret = -EIO;
779
780 /* remove from q */
781 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
782 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
783 list_del(&txmsg->next);
784 }
785
786 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
787 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
788 mstb->tx_slots[txmsg->seqno] = NULL;
789 }
790 }
791out:
792 mutex_unlock(&mgr->qlock);
793
794 return ret;
795}
796
797static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
798{
799 struct drm_dp_mst_branch *mstb;
800
801 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
802 if (!mstb)
803 return NULL;
804
805 mstb->lct = lct;
806 if (lct > 1)
807 memcpy(mstb->rad, rad, lct / 2);
808 INIT_LIST_HEAD(&mstb->ports);
809 kref_init(&mstb->kref);
810 return mstb;
811}
812
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500813static void drm_dp_free_mst_port(struct kref *kref);
814
815static void drm_dp_free_mst_branch_device(struct kref *kref)
816{
817 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
818 if (mstb->port_parent) {
819 if (list_empty(&mstb->port_parent->next))
820 kref_put(&mstb->port_parent->kref, drm_dp_free_mst_port);
821 }
822 kfree(mstb);
823}
824
Dave Airliead7f8a12014-06-05 14:01:32 +1000825static void drm_dp_destroy_mst_branch_device(struct kref *kref)
826{
827 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
828 struct drm_dp_mst_port *port, *tmp;
829 bool wake_tx = false;
830
Dave Airliead7f8a12014-06-05 14:01:32 +1000831 /*
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500832 * init kref again to be used by ports to remove mst branch when it is
833 * not needed anymore
834 */
835 kref_init(kref);
836
837 if (mstb->port_parent && list_empty(&mstb->port_parent->next))
838 kref_get(&mstb->port_parent->kref);
839
840 /*
Dave Airliead7f8a12014-06-05 14:01:32 +1000841 * destroy all ports - don't need lock
842 * as there are no more references to the mst branch
843 * device at this point.
844 */
845 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
846 list_del(&port->next);
847 drm_dp_put_port(port);
848 }
849
850 /* drop any tx slots msg */
851 mutex_lock(&mstb->mgr->qlock);
852 if (mstb->tx_slots[0]) {
853 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
854 mstb->tx_slots[0] = NULL;
855 wake_tx = true;
856 }
857 if (mstb->tx_slots[1]) {
858 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
859 mstb->tx_slots[1] = NULL;
860 wake_tx = true;
861 }
862 mutex_unlock(&mstb->mgr->qlock);
863
864 if (wake_tx)
865 wake_up(&mstb->mgr->tx_waitq);
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500866
867 kref_put(kref, drm_dp_free_mst_branch_device);
Dave Airliead7f8a12014-06-05 14:01:32 +1000868}
869
870static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
871{
872 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
873}
874
875
876static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
877{
Daniel Vetter03913592014-12-08 22:55:22 +0100878 struct drm_dp_mst_branch *mstb;
879
Dave Airliead7f8a12014-06-05 14:01:32 +1000880 switch (old_pdt) {
881 case DP_PEER_DEVICE_DP_LEGACY_CONV:
882 case DP_PEER_DEVICE_SST_SINK:
883 /* remove i2c over sideband */
884 drm_dp_mst_unregister_i2c_bus(&port->aux);
885 break;
886 case DP_PEER_DEVICE_MST_BRANCHING:
Daniel Vetter03913592014-12-08 22:55:22 +0100887 mstb = port->mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +1000888 port->mstb = NULL;
Daniel Vetter03913592014-12-08 22:55:22 +0100889 drm_dp_put_mst_branch_device(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +1000890 break;
891 }
892}
893
894static void drm_dp_destroy_port(struct kref *kref)
895{
896 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
897 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
Dave Airliedf4839f2015-09-16 10:37:28 +1000898
Dave Airliead7f8a12014-06-05 14:01:32 +1000899 if (!port->input) {
900 port->vcpi.num_slots = 0;
Dave Airliec6a0aed2014-10-20 16:28:02 +1000901
902 kfree(port->cached_edid);
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000903
Dave Airliedf4839f2015-09-16 10:37:28 +1000904 /*
905 * The only time we don't have a connector
906 * on an output port is if the connector init
907 * fails.
908 */
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000909 if (port->connector) {
Dave Airliedf4839f2015-09-16 10:37:28 +1000910 /* we can't destroy the connector here, as
911 * we might be holding the mode_config.mutex
912 * from an EDID retrieval */
913
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000914 mutex_lock(&mgr->destroy_connector_lock);
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500915 kref_get(&port->parent->kref);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +0200916 list_add(&port->next, &mgr->destroy_connector_list);
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000917 mutex_unlock(&mgr->destroy_connector_lock);
918 schedule_work(&mgr->destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +0200919 return;
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000920 }
Dave Airliedf4839f2015-09-16 10:37:28 +1000921 /* no need to clean up vcpi
922 * as if we have no connector we never setup a vcpi */
Dave Airliead7f8a12014-06-05 14:01:32 +1000923 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +0300924 port->pdt = DP_PEER_DEVICE_NONE;
Dave Airliead7f8a12014-06-05 14:01:32 +1000925 }
926 kfree(port);
Dave Airliead7f8a12014-06-05 14:01:32 +1000927}
928
929static void drm_dp_put_port(struct drm_dp_mst_port *port)
930{
931 kref_put(&port->kref, drm_dp_destroy_port);
932}
933
934static 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)
935{
936 struct drm_dp_mst_port *port;
937 struct drm_dp_mst_branch *rmstb;
938 if (to_find == mstb) {
939 kref_get(&mstb->kref);
940 return mstb;
941 }
942 list_for_each_entry(port, &mstb->ports, next) {
943 if (port->mstb) {
944 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
945 if (rmstb)
946 return rmstb;
947 }
948 }
949 return NULL;
950}
951
952static struct drm_dp_mst_branch *drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb)
953{
954 struct drm_dp_mst_branch *rmstb = NULL;
955 mutex_lock(&mgr->lock);
956 if (mgr->mst_primary)
957 rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
958 mutex_unlock(&mgr->lock);
959 return rmstb;
960}
961
962static 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)
963{
964 struct drm_dp_mst_port *port, *mport;
965
966 list_for_each_entry(port, &mstb->ports, next) {
967 if (port == to_find) {
968 kref_get(&port->kref);
969 return port;
970 }
971 if (port->mstb) {
972 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
973 if (mport)
974 return mport;
975 }
976 }
977 return NULL;
978}
979
980static struct drm_dp_mst_port *drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
981{
982 struct drm_dp_mst_port *rport = NULL;
983 mutex_lock(&mgr->lock);
984 if (mgr->mst_primary)
985 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
986 mutex_unlock(&mgr->lock);
987 return rport;
988}
989
990static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
991{
992 struct drm_dp_mst_port *port;
993
994 list_for_each_entry(port, &mstb->ports, next) {
995 if (port->port_num == port_num) {
996 kref_get(&port->kref);
997 return port;
998 }
999 }
1000
1001 return NULL;
1002}
1003
1004/*
1005 * calculate a new RAD for this MST branch device
1006 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1007 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1008 */
1009static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1010 u8 *rad)
1011{
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001012 int parent_lct = port->parent->lct;
Dave Airliead7f8a12014-06-05 14:01:32 +10001013 int shift = 4;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001014 int idx = (parent_lct - 1) / 2;
1015 if (parent_lct > 1) {
1016 memcpy(rad, port->parent->rad, idx + 1);
1017 shift = (parent_lct % 2) ? 4 : 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001018 } else
1019 rad[0] = 0;
1020
1021 rad[idx] |= port->port_num << shift;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001022 return parent_lct + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +10001023}
1024
1025/*
1026 * return sends link address for new mstb
1027 */
1028static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1029{
1030 int ret;
1031 u8 rad[6], lct;
1032 bool send_link = false;
1033 switch (port->pdt) {
1034 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1035 case DP_PEER_DEVICE_SST_SINK:
1036 /* add i2c over sideband */
1037 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1038 break;
1039 case DP_PEER_DEVICE_MST_BRANCHING:
1040 lct = drm_dp_calculate_rad(port, rad);
1041
1042 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1043 port->mstb->mgr = port->mgr;
1044 port->mstb->port_parent = port;
1045
1046 send_link = true;
1047 break;
1048 }
1049 return send_link;
1050}
1051
Hersen Wu5e93b822016-01-22 17:07:28 -05001052static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
Dave Airliead7f8a12014-06-05 14:01:32 +10001053{
1054 int ret;
Hersen Wu5e93b822016-01-22 17:07:28 -05001055
1056 memcpy(mstb->guid, guid, 16);
1057
1058 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1059 if (mstb->port_parent) {
1060 ret = drm_dp_send_dpcd_write(
1061 mstb->mgr,
1062 mstb->port_parent,
1063 DP_GUID,
1064 16,
1065 mstb->guid);
1066 } else {
1067
1068 ret = drm_dp_dpcd_write(
1069 mstb->mgr->aux,
1070 DP_GUID,
1071 mstb->guid,
1072 16);
Dave Airliead7f8a12014-06-05 14:01:32 +10001073 }
1074 }
1075}
1076
Dave Airlie1c960872015-09-16 11:04:49 +10001077static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1078 int pnum,
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001079 char *proppath,
1080 size_t proppath_size)
Dave Airliead7f8a12014-06-05 14:01:32 +10001081{
1082 int i;
1083 char temp[8];
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001084 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
Dave Airliead7f8a12014-06-05 14:01:32 +10001085 for (i = 0; i < (mstb->lct - 1); i++) {
1086 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001087 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001088 snprintf(temp, sizeof(temp), "-%d", port_num);
1089 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001090 }
Dave Airlie1c960872015-09-16 11:04:49 +10001091 snprintf(temp, sizeof(temp), "-%d", pnum);
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001092 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001093}
1094
1095static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1096 struct device *dev,
1097 struct drm_dp_link_addr_reply_port *port_msg)
1098{
1099 struct drm_dp_mst_port *port;
1100 bool ret;
1101 bool created = false;
1102 int old_pdt = 0;
1103 int old_ddps = 0;
1104 port = drm_dp_get_port(mstb, port_msg->port_number);
1105 if (!port) {
1106 port = kzalloc(sizeof(*port), GFP_KERNEL);
1107 if (!port)
1108 return;
1109 kref_init(&port->kref);
1110 port->parent = mstb;
1111 port->port_num = port_msg->port_number;
1112 port->mgr = mstb->mgr;
1113 port->aux.name = "DPMST";
1114 port->aux.dev = dev;
1115 created = true;
1116 } else {
1117 old_pdt = port->pdt;
1118 old_ddps = port->ddps;
1119 }
1120
1121 port->pdt = port_msg->peer_device_type;
1122 port->input = port_msg->input_port;
1123 port->mcs = port_msg->mcs;
1124 port->ddps = port_msg->ddps;
1125 port->ldps = port_msg->legacy_device_plug_status;
1126 port->dpcd_rev = port_msg->dpcd_revision;
1127 port->num_sdp_streams = port_msg->num_sdp_streams;
1128 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
Dave Airliead7f8a12014-06-05 14:01:32 +10001129
1130 /* manage mstb port lists with mgr lock - take a reference
1131 for this list */
1132 if (created) {
1133 mutex_lock(&mstb->mgr->lock);
1134 kref_get(&port->kref);
1135 list_add(&port->next, &mstb->ports);
1136 mutex_unlock(&mstb->mgr->lock);
1137 }
1138
1139 if (old_ddps != port->ddps) {
1140 if (port->ddps) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001141 if (!port->input)
1142 drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1143 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001144 port->available_pbn = 0;
1145 }
1146 }
1147
1148 if (old_pdt != port->pdt && !port->input) {
1149 drm_dp_port_teardown_pdt(port, old_pdt);
1150
1151 ret = drm_dp_port_setup_pdt(port);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001152 if (ret == true)
Dave Airliead7f8a12014-06-05 14:01:32 +10001153 drm_dp_send_link_address(mstb->mgr, port->mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001154 }
1155
1156 if (created && !port->input) {
1157 char proppath[255];
Dave Airlie1c960872015-09-16 11:04:49 +10001158
1159 build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
Dave Airliead7f8a12014-06-05 14:01:32 +10001160 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
Dave Airliedf4839f2015-09-16 10:37:28 +10001161 if (!port->connector) {
1162 /* remove it from the port list */
1163 mutex_lock(&mstb->mgr->lock);
1164 list_del(&port->next);
1165 mutex_unlock(&mstb->mgr->lock);
1166 /* drop port list reference */
1167 drm_dp_put_port(port);
1168 goto out;
1169 }
Ville Syrjälä4da5caa2016-10-26 12:05:55 +03001170 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1171 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1172 port->port_num >= DP_MST_LOGICAL_PORT_0) {
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001173 port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1174 drm_mode_connector_set_tile_property(port->connector);
1175 }
Dave Airlied9515c52015-09-16 17:55:23 +10001176 (*mstb->mgr->cbs->register_connector)(port->connector);
Dave Airliead7f8a12014-06-05 14:01:32 +10001177 }
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001178
Dave Airliedf4839f2015-09-16 10:37:28 +10001179out:
Dave Airliead7f8a12014-06-05 14:01:32 +10001180 /* put reference to this port */
1181 drm_dp_put_port(port);
1182}
1183
1184static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1185 struct drm_dp_connection_status_notify *conn_stat)
1186{
1187 struct drm_dp_mst_port *port;
1188 int old_pdt;
1189 int old_ddps;
1190 bool dowork = false;
1191 port = drm_dp_get_port(mstb, conn_stat->port_number);
1192 if (!port)
1193 return;
1194
1195 old_ddps = port->ddps;
1196 old_pdt = port->pdt;
1197 port->pdt = conn_stat->peer_device_type;
1198 port->mcs = conn_stat->message_capability_status;
1199 port->ldps = conn_stat->legacy_device_plug_status;
1200 port->ddps = conn_stat->displayport_device_plug_status;
1201
1202 if (old_ddps != port->ddps) {
1203 if (port->ddps) {
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001204 dowork = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001205 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001206 port->available_pbn = 0;
1207 }
1208 }
1209 if (old_pdt != port->pdt && !port->input) {
1210 drm_dp_port_teardown_pdt(port, old_pdt);
1211
1212 if (drm_dp_port_setup_pdt(port))
1213 dowork = true;
1214 }
1215
1216 drm_dp_put_port(port);
1217 if (dowork)
1218 queue_work(system_long_wq, &mstb->mgr->work);
1219
1220}
1221
1222static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1223 u8 lct, u8 *rad)
1224{
1225 struct drm_dp_mst_branch *mstb;
1226 struct drm_dp_mst_port *port;
1227 int i;
1228 /* find the port by iterating down */
Dave Airlie9eb1e572015-06-22 14:40:44 +10001229
1230 mutex_lock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001231 mstb = mgr->mst_primary;
1232
1233 for (i = 0; i < lct - 1; i++) {
1234 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001235 int port_num = (rad[i / 2] >> shift) & 0xf;
Dave Airliead7f8a12014-06-05 14:01:32 +10001236
1237 list_for_each_entry(port, &mstb->ports, next) {
1238 if (port->port_num == port_num) {
Adam Richter30730c72015-10-16 03:33:02 -07001239 mstb = port->mstb;
1240 if (!mstb) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001241 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
Adam Richter30730c72015-10-16 03:33:02 -07001242 goto out;
Dave Airliead7f8a12014-06-05 14:01:32 +10001243 }
1244
Dave Airliead7f8a12014-06-05 14:01:32 +10001245 break;
1246 }
1247 }
1248 }
1249 kref_get(&mstb->kref);
Adam Richter30730c72015-10-16 03:33:02 -07001250out:
Dave Airlie9eb1e572015-06-22 14:40:44 +10001251 mutex_unlock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001252 return mstb;
1253}
1254
Mykola Lysenkobd934322015-12-18 17:14:42 -05001255static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1256 struct drm_dp_mst_branch *mstb,
1257 uint8_t *guid)
1258{
1259 struct drm_dp_mst_branch *found_mstb;
1260 struct drm_dp_mst_port *port;
1261
Hersen Wu5e93b822016-01-22 17:07:28 -05001262 if (memcmp(mstb->guid, guid, 16) == 0)
1263 return mstb;
1264
1265
Mykola Lysenkobd934322015-12-18 17:14:42 -05001266 list_for_each_entry(port, &mstb->ports, next) {
1267 if (!port->mstb)
1268 continue;
1269
Mykola Lysenkobd934322015-12-18 17:14:42 -05001270 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1271
1272 if (found_mstb)
1273 return found_mstb;
1274 }
1275
1276 return NULL;
1277}
1278
1279static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
1280 struct drm_dp_mst_topology_mgr *mgr,
1281 uint8_t *guid)
1282{
1283 struct drm_dp_mst_branch *mstb;
1284
1285 /* find the port by iterating down */
1286 mutex_lock(&mgr->lock);
1287
Hersen Wu5e93b822016-01-22 17:07:28 -05001288 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
Mykola Lysenkobd934322015-12-18 17:14:42 -05001289
1290 if (mstb)
1291 kref_get(&mstb->kref);
1292
1293 mutex_unlock(&mgr->lock);
1294 return mstb;
1295}
1296
Dave Airliead7f8a12014-06-05 14:01:32 +10001297static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1298 struct drm_dp_mst_branch *mstb)
1299{
1300 struct drm_dp_mst_port *port;
Daniel Vetter9254ec42015-06-22 17:31:59 +10001301 struct drm_dp_mst_branch *mstb_child;
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001302 if (!mstb->link_address_sent)
Dave Airliead7f8a12014-06-05 14:01:32 +10001303 drm_dp_send_link_address(mgr, mstb);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001304
Dave Airliead7f8a12014-06-05 14:01:32 +10001305 list_for_each_entry(port, &mstb->ports, next) {
1306 if (port->input)
1307 continue;
1308
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001309 if (!port->ddps)
Dave Airliead7f8a12014-06-05 14:01:32 +10001310 continue;
1311
1312 if (!port->available_pbn)
1313 drm_dp_send_enum_path_resources(mgr, mstb, port);
1314
Daniel Vetter9254ec42015-06-22 17:31:59 +10001315 if (port->mstb) {
1316 mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1317 if (mstb_child) {
1318 drm_dp_check_and_send_link_address(mgr, mstb_child);
1319 drm_dp_put_mst_branch_device(mstb_child);
1320 }
1321 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001322 }
1323}
1324
1325static void drm_dp_mst_link_probe_work(struct work_struct *work)
1326{
1327 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001328 struct drm_dp_mst_branch *mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +10001329
Daniel Vetter9254ec42015-06-22 17:31:59 +10001330 mutex_lock(&mgr->lock);
1331 mstb = mgr->mst_primary;
1332 if (mstb) {
1333 kref_get(&mstb->kref);
1334 }
1335 mutex_unlock(&mgr->lock);
1336 if (mstb) {
1337 drm_dp_check_and_send_link_address(mgr, mstb);
1338 drm_dp_put_mst_branch_device(mstb);
1339 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001340}
1341
1342static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1343 u8 *guid)
1344{
1345 static u8 zero_guid[16];
1346
1347 if (!memcmp(guid, zero_guid, 16)) {
1348 u64 salt = get_jiffies_64();
1349 memcpy(&guid[0], &salt, sizeof(u64));
1350 memcpy(&guid[8], &salt, sizeof(u64));
1351 return false;
1352 }
1353 return true;
1354}
1355
1356#if 0
1357static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1358{
1359 struct drm_dp_sideband_msg_req_body req;
1360
1361 req.req_type = DP_REMOTE_DPCD_READ;
1362 req.u.dpcd_read.port_number = port_num;
1363 req.u.dpcd_read.dpcd_address = offset;
1364 req.u.dpcd_read.num_bytes = num_bytes;
1365 drm_dp_encode_sideband_req(&req, msg);
1366
1367 return 0;
1368}
1369#endif
1370
1371static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1372 bool up, u8 *msg, int len)
1373{
1374 int ret;
1375 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1376 int tosend, total, offset;
1377 int retries = 0;
1378
1379retry:
1380 total = len;
1381 offset = 0;
1382 do {
1383 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1384
1385 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1386 &msg[offset],
1387 tosend);
1388 if (ret != tosend) {
1389 if (ret == -EIO && retries < 5) {
1390 retries++;
1391 goto retry;
1392 }
1393 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10001394
1395 return -EIO;
1396 }
1397 offset += tosend;
1398 total -= tosend;
1399 } while (total > 0);
1400 return 0;
1401}
1402
1403static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1404 struct drm_dp_sideband_msg_tx *txmsg)
1405{
1406 struct drm_dp_mst_branch *mstb = txmsg->dst;
Mykola Lysenkobd934322015-12-18 17:14:42 -05001407 u8 req_type;
Dave Airliead7f8a12014-06-05 14:01:32 +10001408
1409 /* both msg slots are full */
1410 if (txmsg->seqno == -1) {
1411 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1412 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1413 return -EAGAIN;
1414 }
1415 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1416 txmsg->seqno = mstb->last_seqno;
1417 mstb->last_seqno ^= 1;
1418 } else if (mstb->tx_slots[0] == NULL)
1419 txmsg->seqno = 0;
1420 else
1421 txmsg->seqno = 1;
1422 mstb->tx_slots[txmsg->seqno] = txmsg;
1423 }
Mykola Lysenkobd934322015-12-18 17:14:42 -05001424
1425 req_type = txmsg->msg[0] & 0x7f;
1426 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1427 req_type == DP_RESOURCE_STATUS_NOTIFY)
1428 hdr->broadcast = 1;
1429 else
1430 hdr->broadcast = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001431 hdr->path_msg = txmsg->path_msg;
1432 hdr->lct = mstb->lct;
1433 hdr->lcr = mstb->lct - 1;
1434 if (mstb->lct > 1)
1435 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1436 hdr->seqno = txmsg->seqno;
1437 return 0;
1438}
1439/*
1440 * process a single block of the next message in the sideband queue
1441 */
1442static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1443 struct drm_dp_sideband_msg_tx *txmsg,
1444 bool up)
1445{
1446 u8 chunk[48];
1447 struct drm_dp_sideband_msg_hdr hdr;
1448 int len, space, idx, tosend;
1449 int ret;
1450
Damien Lespiaubf3719c2014-07-14 12:13:18 +01001451 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1452
Dave Airliead7f8a12014-06-05 14:01:32 +10001453 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1454 txmsg->seqno = -1;
1455 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1456 }
1457
1458 /* make hdr from dst mst - for replies use seqno
1459 otherwise assign one */
1460 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1461 if (ret < 0)
1462 return ret;
1463
1464 /* amount left to send in this message */
1465 len = txmsg->cur_len - txmsg->cur_offset;
1466
1467 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1468 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1469
1470 tosend = min(len, space);
1471 if (len == txmsg->cur_len)
1472 hdr.somt = 1;
1473 if (space >= len)
1474 hdr.eomt = 1;
1475
1476
1477 hdr.msg_len = tosend + 1;
1478 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1479 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1480 /* add crc at end */
1481 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1482 idx += tosend + 1;
1483
1484 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1485 if (ret) {
1486 DRM_DEBUG_KMS("sideband msg failed to send\n");
1487 return ret;
1488 }
1489
1490 txmsg->cur_offset += tosend;
1491 if (txmsg->cur_offset == txmsg->cur_len) {
1492 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1493 return 1;
1494 }
1495 return 0;
1496}
1497
Dave Airliead7f8a12014-06-05 14:01:32 +10001498static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1499{
1500 struct drm_dp_sideband_msg_tx *txmsg;
1501 int ret;
1502
Daniel Vettercd961bb2015-01-28 10:02:23 +01001503 WARN_ON(!mutex_is_locked(&mgr->qlock));
1504
Dave Airliead7f8a12014-06-05 14:01:32 +10001505 /* construct a chunk from the first msg in the tx_msg queue */
Daniel Vettercb021a32016-07-15 21:48:03 +02001506 if (list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10001507 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001508
1509 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1510 ret = process_single_tx_qlock(mgr, txmsg, false);
1511 if (ret == 1) {
1512 /* txmsg is sent it should be in the slots now */
1513 list_del(&txmsg->next);
1514 } else if (ret) {
1515 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1516 list_del(&txmsg->next);
1517 if (txmsg->seqno != -1)
1518 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1519 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1520 wake_up(&mgr->tx_waitq);
1521 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001522}
1523
1524/* called holding qlock */
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001525static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1526 struct drm_dp_sideband_msg_tx *txmsg)
Dave Airliead7f8a12014-06-05 14:01:32 +10001527{
Dave Airliead7f8a12014-06-05 14:01:32 +10001528 int ret;
1529
1530 /* construct a chunk from the first msg in the tx_msg queue */
Dave Airliead7f8a12014-06-05 14:01:32 +10001531 ret = process_single_tx_qlock(mgr, txmsg, true);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001532
1533 if (ret != 1)
Dave Airliead7f8a12014-06-05 14:01:32 +10001534 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001535
1536 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001537}
1538
1539static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1540 struct drm_dp_sideband_msg_tx *txmsg)
1541{
1542 mutex_lock(&mgr->qlock);
1543 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
Daniel Vettercb021a32016-07-15 21:48:03 +02001544 if (list_is_singular(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10001545 process_single_down_tx_qlock(mgr);
1546 mutex_unlock(&mgr->qlock);
1547}
1548
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001549static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1550 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001551{
1552 int len;
1553 struct drm_dp_sideband_msg_tx *txmsg;
1554 int ret;
1555
1556 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1557 if (!txmsg)
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001558 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001559
1560 txmsg->dst = mstb;
1561 len = build_link_address(txmsg);
1562
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001563 mstb->link_address_sent = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001564 drm_dp_queue_down_tx(mgr, txmsg);
1565
1566 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1567 if (ret > 0) {
1568 int i;
1569
1570 if (txmsg->reply.reply_type == 1)
1571 DRM_DEBUG_KMS("link address nak received\n");
1572 else {
1573 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1574 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1575 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,
1576 txmsg->reply.u.link_addr.ports[i].input_port,
1577 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1578 txmsg->reply.u.link_addr.ports[i].port_number,
1579 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1580 txmsg->reply.u.link_addr.ports[i].mcs,
1581 txmsg->reply.u.link_addr.ports[i].ddps,
1582 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1583 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1584 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1585 }
Hersen Wu5e93b822016-01-22 17:07:28 -05001586
1587 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1588
Dave Airliead7f8a12014-06-05 14:01:32 +10001589 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1590 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1591 }
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001592 (*mgr->cbs->hotplug)(mgr);
Dave Airliead7f8a12014-06-05 14:01:32 +10001593 }
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001594 } else {
1595 mstb->link_address_sent = false;
Dave Airliead7f8a12014-06-05 14:01:32 +10001596 DRM_DEBUG_KMS("link address failed %d\n", ret);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001597 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001598
1599 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10001600}
1601
1602static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1603 struct drm_dp_mst_branch *mstb,
1604 struct drm_dp_mst_port *port)
1605{
1606 int len;
1607 struct drm_dp_sideband_msg_tx *txmsg;
1608 int ret;
1609
1610 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1611 if (!txmsg)
1612 return -ENOMEM;
1613
1614 txmsg->dst = mstb;
1615 len = build_enum_path_resources(txmsg, port->port_num);
1616
1617 drm_dp_queue_down_tx(mgr, txmsg);
1618
1619 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1620 if (ret > 0) {
1621 if (txmsg->reply.reply_type == 1)
1622 DRM_DEBUG_KMS("enum path resources nak received\n");
1623 else {
1624 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1625 DRM_ERROR("got incorrect port in response\n");
1626 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,
1627 txmsg->reply.u.path_resources.avail_payload_bw_number);
1628 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1629 }
1630 }
1631
1632 kfree(txmsg);
1633 return 0;
1634}
1635
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001636static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
1637{
1638 if (!mstb->port_parent)
1639 return NULL;
1640
1641 if (mstb->port_parent->mstb != mstb)
1642 return mstb->port_parent;
1643
1644 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
1645}
1646
1647static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
1648 struct drm_dp_mst_branch *mstb,
1649 int *port_num)
1650{
1651 struct drm_dp_mst_branch *rmstb = NULL;
1652 struct drm_dp_mst_port *found_port;
1653 mutex_lock(&mgr->lock);
1654 if (mgr->mst_primary) {
1655 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
1656
1657 if (found_port) {
1658 rmstb = found_port->parent;
1659 kref_get(&rmstb->kref);
1660 *port_num = found_port->port_num;
1661 }
1662 }
1663 mutex_unlock(&mgr->lock);
1664 return rmstb;
1665}
1666
Thierry Reding8fa6a422014-07-21 13:23:57 +02001667static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1668 struct drm_dp_mst_port *port,
1669 int id,
1670 int pbn)
Dave Airliead7f8a12014-06-05 14:01:32 +10001671{
1672 struct drm_dp_sideband_msg_tx *txmsg;
1673 struct drm_dp_mst_branch *mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001674 int len, ret, port_num;
Libin Yangef8f9be2015-12-02 14:09:43 +08001675 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1676 int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10001677
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04001678 port = drm_dp_get_validated_port_ref(mgr, port);
1679 if (!port)
1680 return -EINVAL;
1681
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001682 port_num = port->port_num;
Dave Airliead7f8a12014-06-05 14:01:32 +10001683 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001684 if (!mstb) {
1685 mstb = drm_dp_get_last_connected_port_and_mstb(mgr, port->parent, &port_num);
1686
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04001687 if (!mstb) {
1688 drm_dp_put_port(port);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001689 return -EINVAL;
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04001690 }
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001691 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001692
1693 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1694 if (!txmsg) {
1695 ret = -ENOMEM;
1696 goto fail_put;
1697 }
1698
Libin Yangef8f9be2015-12-02 14:09:43 +08001699 for (i = 0; i < port->num_sdp_streams; i++)
1700 sinks[i] = i;
1701
Dave Airliead7f8a12014-06-05 14:01:32 +10001702 txmsg->dst = mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001703 len = build_allocate_payload(txmsg, port_num,
Dave Airliead7f8a12014-06-05 14:01:32 +10001704 id,
Libin Yangef8f9be2015-12-02 14:09:43 +08001705 pbn, port->num_sdp_streams, sinks);
Dave Airliead7f8a12014-06-05 14:01:32 +10001706
1707 drm_dp_queue_down_tx(mgr, txmsg);
1708
1709 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1710 if (ret > 0) {
1711 if (txmsg->reply.reply_type == 1) {
1712 ret = -EINVAL;
1713 } else
1714 ret = 0;
1715 }
1716 kfree(txmsg);
1717fail_put:
1718 drm_dp_put_mst_branch_device(mstb);
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04001719 drm_dp_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001720 return ret;
1721}
1722
1723static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1724 int id,
1725 struct drm_dp_payload *payload)
1726{
1727 int ret;
1728
1729 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1730 if (ret < 0) {
1731 payload->payload_state = 0;
1732 return ret;
1733 }
1734 payload->payload_state = DP_PAYLOAD_LOCAL;
1735 return 0;
1736}
1737
Thierry Reding8fa6a422014-07-21 13:23:57 +02001738static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1739 struct drm_dp_mst_port *port,
1740 int id,
1741 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001742{
1743 int ret;
1744 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1745 if (ret < 0)
1746 return ret;
1747 payload->payload_state = DP_PAYLOAD_REMOTE;
1748 return ret;
1749}
1750
Thierry Reding8fa6a422014-07-21 13:23:57 +02001751static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1752 struct drm_dp_mst_port *port,
1753 int id,
1754 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001755{
1756 DRM_DEBUG_KMS("\n");
1757 /* its okay for these to fail */
1758 if (port) {
1759 drm_dp_payload_send_msg(mgr, port, id, 0);
1760 }
1761
1762 drm_dp_dpcd_write_payload(mgr, id, payload);
Dave Airliedfda0df2014-08-06 16:26:21 +10001763 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001764 return 0;
1765}
1766
Thierry Reding8fa6a422014-07-21 13:23:57 +02001767static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1768 int id,
1769 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001770{
1771 payload->payload_state = 0;
1772 return 0;
1773}
1774
1775/**
1776 * drm_dp_update_payload_part1() - Execute payload update part 1
1777 * @mgr: manager to use.
1778 *
1779 * This iterates over all proposed virtual channels, and tries to
1780 * allocate space in the link for them. For 0->slots transitions,
1781 * this step just writes the VCPI to the MST device. For slots->0
1782 * transitions, this writes the updated VCPIs and removes the
1783 * remote VC payloads.
1784 *
1785 * after calling this the driver should generate ACT and payload
1786 * packets.
1787 */
1788int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1789{
Dave Airliedfda0df2014-08-06 16:26:21 +10001790 int i, j;
Dave Airliead7f8a12014-06-05 14:01:32 +10001791 int cur_slots = 1;
1792 struct drm_dp_payload req_payload;
1793 struct drm_dp_mst_port *port;
1794
1795 mutex_lock(&mgr->payload_lock);
1796 for (i = 0; i < mgr->max_payloads; i++) {
1797 /* solve the current payloads - compare to the hw ones
1798 - update the hw view */
1799 req_payload.start_slot = cur_slots;
1800 if (mgr->proposed_vcpis[i]) {
1801 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
cpaul@redhat.com263efde2016-04-22 16:08:46 -04001802 port = drm_dp_get_validated_port_ref(mgr, port);
1803 if (!port) {
1804 mutex_unlock(&mgr->payload_lock);
1805 return -EINVAL;
1806 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001807 req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
Harry Wentland6cecdf7a2015-12-07 13:55:52 -05001808 req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
Dave Airliead7f8a12014-06-05 14:01:32 +10001809 } else {
1810 port = NULL;
1811 req_payload.num_slots = 0;
1812 }
Dave Airliedfda0df2014-08-06 16:26:21 +10001813
1814 if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1815 mgr->payloads[i].start_slot = req_payload.start_slot;
1816 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001817 /* work out what is required to happen with this payload */
Dave Airliedfda0df2014-08-06 16:26:21 +10001818 if (mgr->payloads[i].num_slots != req_payload.num_slots) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001819
1820 /* need to push an update for this payload */
1821 if (req_payload.num_slots) {
Dave Airliedfda0df2014-08-06 16:26:21 +10001822 drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
Dave Airliead7f8a12014-06-05 14:01:32 +10001823 mgr->payloads[i].num_slots = req_payload.num_slots;
Harry Wentland6cecdf7a2015-12-07 13:55:52 -05001824 mgr->payloads[i].vcpi = req_payload.vcpi;
Dave Airliead7f8a12014-06-05 14:01:32 +10001825 } else if (mgr->payloads[i].num_slots) {
1826 mgr->payloads[i].num_slots = 0;
Pierre-Louis Bossartdfe8f572017-02-14 14:49:21 +02001827 drm_dp_destroy_payload_step1(mgr, port, mgr->payloads[i].vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10001828 req_payload.payload_state = mgr->payloads[i].payload_state;
Dave Airliedfda0df2014-08-06 16:26:21 +10001829 mgr->payloads[i].start_slot = 0;
1830 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001831 mgr->payloads[i].payload_state = req_payload.payload_state;
1832 }
1833 cur_slots += req_payload.num_slots;
cpaul@redhat.com263efde2016-04-22 16:08:46 -04001834
1835 if (port)
1836 drm_dp_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001837 }
Dave Airliedfda0df2014-08-06 16:26:21 +10001838
1839 for (i = 0; i < mgr->max_payloads; i++) {
1840 if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1841 DRM_DEBUG_KMS("removing payload %d\n", i);
1842 for (j = i; j < mgr->max_payloads - 1; j++) {
1843 memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1844 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1845 if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1846 set_bit(j + 1, &mgr->payload_mask);
1847 } else {
1848 clear_bit(j + 1, &mgr->payload_mask);
1849 }
1850 }
1851 memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1852 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1853 clear_bit(mgr->max_payloads, &mgr->payload_mask);
1854
1855 }
1856 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001857 mutex_unlock(&mgr->payload_lock);
1858
1859 return 0;
1860}
1861EXPORT_SYMBOL(drm_dp_update_payload_part1);
1862
1863/**
1864 * drm_dp_update_payload_part2() - Execute payload update part 2
1865 * @mgr: manager to use.
1866 *
1867 * This iterates over all proposed virtual channels, and tries to
1868 * allocate space in the link for them. For 0->slots transitions,
1869 * this step writes the remote VC payload commands. For slots->0
1870 * this just resets some internal state.
1871 */
1872int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1873{
1874 struct drm_dp_mst_port *port;
1875 int i;
Damien Lespiau7389ad42014-07-14 11:53:44 +01001876 int ret = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001877 mutex_lock(&mgr->payload_lock);
1878 for (i = 0; i < mgr->max_payloads; i++) {
1879
1880 if (!mgr->proposed_vcpis[i])
1881 continue;
1882
1883 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1884
1885 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1886 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10001887 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10001888 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10001889 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10001890 }
1891 if (ret) {
1892 mutex_unlock(&mgr->payload_lock);
1893 return ret;
1894 }
1895 }
1896 mutex_unlock(&mgr->payload_lock);
1897 return 0;
1898}
1899EXPORT_SYMBOL(drm_dp_update_payload_part2);
1900
1901#if 0 /* unused as of yet */
1902static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1903 struct drm_dp_mst_port *port,
1904 int offset, int size)
1905{
1906 int len;
1907 struct drm_dp_sideband_msg_tx *txmsg;
1908
1909 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1910 if (!txmsg)
1911 return -ENOMEM;
1912
1913 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1914 txmsg->dst = port->parent;
1915
1916 drm_dp_queue_down_tx(mgr, txmsg);
1917
1918 return 0;
1919}
1920#endif
1921
1922static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1923 struct drm_dp_mst_port *port,
1924 int offset, int size, u8 *bytes)
1925{
1926 int len;
1927 int ret;
1928 struct drm_dp_sideband_msg_tx *txmsg;
1929 struct drm_dp_mst_branch *mstb;
1930
1931 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1932 if (!mstb)
1933 return -EINVAL;
1934
1935 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1936 if (!txmsg) {
1937 ret = -ENOMEM;
1938 goto fail_put;
1939 }
1940
1941 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1942 txmsg->dst = mstb;
1943
1944 drm_dp_queue_down_tx(mgr, txmsg);
1945
1946 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1947 if (ret > 0) {
1948 if (txmsg->reply.reply_type == 1) {
1949 ret = -EINVAL;
1950 } else
1951 ret = 0;
1952 }
1953 kfree(txmsg);
1954fail_put:
1955 drm_dp_put_mst_branch_device(mstb);
1956 return ret;
1957}
1958
1959static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1960{
1961 struct drm_dp_sideband_msg_reply_body reply;
1962
Mykola Lysenkoa68d2432015-12-07 13:55:51 -05001963 reply.reply_type = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001964 reply.req_type = req_type;
1965 drm_dp_encode_sideband_reply(&reply, msg);
1966 return 0;
1967}
1968
1969static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1970 struct drm_dp_mst_branch *mstb,
1971 int req_type, int seqno, bool broadcast)
1972{
1973 struct drm_dp_sideband_msg_tx *txmsg;
1974
1975 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1976 if (!txmsg)
1977 return -ENOMEM;
1978
1979 txmsg->dst = mstb;
1980 txmsg->seqno = seqno;
1981 drm_dp_encode_up_ack_reply(txmsg, req_type);
1982
1983 mutex_lock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001984
1985 process_single_up_tx_qlock(mgr, txmsg);
1986
Dave Airliead7f8a12014-06-05 14:01:32 +10001987 mutex_unlock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001988
1989 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10001990 return 0;
1991}
1992
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001993static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1994 int dp_link_count,
1995 int *out)
Dave Airliead7f8a12014-06-05 14:01:32 +10001996{
1997 switch (dp_link_bw) {
Chris Wilsonb853fdb2014-11-12 10:13:37 +00001998 default:
1999 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2000 dp_link_bw, dp_link_count);
2001 return false;
2002
Dave Airliead7f8a12014-06-05 14:01:32 +10002003 case DP_LINK_BW_1_62:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002004 *out = 3 * dp_link_count;
2005 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002006 case DP_LINK_BW_2_7:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002007 *out = 5 * dp_link_count;
2008 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002009 case DP_LINK_BW_5_4:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002010 *out = 10 * dp_link_count;
2011 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002012 }
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002013 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002014}
2015
2016/**
2017 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2018 * @mgr: manager to set state for
2019 * @mst_state: true to enable MST on this connector - false to disable.
2020 *
2021 * This is called by the driver when it detects an MST capable device plugged
2022 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2023 */
2024int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2025{
2026 int ret = 0;
2027 struct drm_dp_mst_branch *mstb = NULL;
2028
2029 mutex_lock(&mgr->lock);
2030 if (mst_state == mgr->mst_state)
2031 goto out_unlock;
2032
2033 mgr->mst_state = mst_state;
2034 /* set the device into MST mode */
2035 if (mst_state) {
2036 WARN_ON(mgr->mst_primary);
2037
2038 /* get dpcd info */
2039 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2040 if (ret != DP_RECEIVER_CAP_SIZE) {
2041 DRM_DEBUG_KMS("failed to read DPCD\n");
2042 goto out_unlock;
2043 }
2044
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002045 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2046 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2047 &mgr->pbn_div)) {
2048 ret = -EINVAL;
2049 goto out_unlock;
2050 }
2051
Dave Airliead7f8a12014-06-05 14:01:32 +10002052 mgr->total_pbn = 2560;
2053 mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
2054 mgr->avail_slots = mgr->total_slots;
2055
2056 /* add initial branch device at LCT 1 */
2057 mstb = drm_dp_add_mst_branch_device(1, NULL);
2058 if (mstb == NULL) {
2059 ret = -ENOMEM;
2060 goto out_unlock;
2061 }
2062 mstb->mgr = mgr;
2063
2064 /* give this the main reference */
2065 mgr->mst_primary = mstb;
2066 kref_get(&mgr->mst_primary->kref);
2067
Andrey Grodzovskyc175cd12016-01-22 17:07:29 -05002068 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2069 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2070 if (ret < 0) {
2071 goto out_unlock;
2072 }
2073
Dave Airliead7f8a12014-06-05 14:01:32 +10002074 {
2075 struct drm_dp_payload reset_pay;
2076 reset_pay.start_slot = 0;
2077 reset_pay.num_slots = 0x3f;
2078 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2079 }
2080
Dave Airliead7f8a12014-06-05 14:01:32 +10002081 queue_work(system_long_wq, &mgr->work);
2082
2083 ret = 0;
2084 } else {
2085 /* disable MST on the device */
2086 mstb = mgr->mst_primary;
2087 mgr->mst_primary = NULL;
2088 /* this can fail if the device is gone */
2089 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2090 ret = 0;
2091 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2092 mgr->payload_mask = 0;
2093 set_bit(0, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10002094 mgr->vcpi_mask = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002095 }
2096
2097out_unlock:
2098 mutex_unlock(&mgr->lock);
2099 if (mstb)
2100 drm_dp_put_mst_branch_device(mstb);
2101 return ret;
2102
2103}
2104EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2105
2106/**
2107 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2108 * @mgr: manager to suspend
2109 *
2110 * This function tells the MST device that we can't handle UP messages
2111 * anymore. This should stop it from sending any since we are suspended.
2112 */
2113void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2114{
2115 mutex_lock(&mgr->lock);
2116 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2117 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2118 mutex_unlock(&mgr->lock);
Dave Airlie274d8352015-09-30 10:39:42 +10002119 flush_work(&mgr->work);
2120 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002121}
2122EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2123
2124/**
2125 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2126 * @mgr: manager to resume
2127 *
2128 * This will fetch DPCD and see if the device is still there,
2129 * if it is, it will rewrite the MSTM control bits, and return.
2130 *
2131 * if the device fails this returns -1, and the driver should do
2132 * a full MST reprobe, in case we were undocked.
2133 */
2134int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2135{
2136 int ret = 0;
2137
2138 mutex_lock(&mgr->lock);
2139
2140 if (mgr->mst_primary) {
2141 int sret;
Lyude1652fce2016-04-13 16:50:18 -04002142 u8 guid[16];
2143
Dave Airliead7f8a12014-06-05 14:01:32 +10002144 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2145 if (sret != DP_RECEIVER_CAP_SIZE) {
2146 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2147 ret = -1;
2148 goto out_unlock;
2149 }
2150
2151 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2152 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2153 if (ret < 0) {
2154 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2155 ret = -1;
2156 goto out_unlock;
2157 }
Lyude1652fce2016-04-13 16:50:18 -04002158
2159 /* Some hubs forget their guids after they resume */
2160 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2161 if (sret != 16) {
2162 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2163 ret = -1;
2164 goto out_unlock;
2165 }
2166 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2167
Dave Airliead7f8a12014-06-05 14:01:32 +10002168 ret = 0;
2169 } else
2170 ret = -1;
2171
2172out_unlock:
2173 mutex_unlock(&mgr->lock);
2174 return ret;
2175}
2176EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2177
Imre Deaka4404252017-07-19 16:46:32 +03002178static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
Dave Airliead7f8a12014-06-05 14:01:32 +10002179{
2180 int len;
2181 u8 replyblock[32];
2182 int replylen, origlen, curreply;
2183 int ret;
2184 struct drm_dp_sideband_msg_rx *msg;
2185 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2186 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2187
2188 len = min(mgr->max_dpcd_transaction_bytes, 16);
2189 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2190 replyblock, len);
2191 if (ret != len) {
2192 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
Imre Deaka4404252017-07-19 16:46:32 +03002193 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002194 }
2195 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2196 if (!ret) {
2197 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
Imre Deaka4404252017-07-19 16:46:32 +03002198 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002199 }
2200 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2201
2202 origlen = replylen;
2203 replylen -= len;
2204 curreply = len;
2205 while (replylen > 0) {
2206 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2207 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2208 replyblock, len);
2209 if (ret != len) {
Imre Deakbc3bd642017-07-19 14:43:28 +03002210 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2211 len, ret);
Imre Deaka4404252017-07-19 16:46:32 +03002212 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002213 }
Imre Deakbc3bd642017-07-19 14:43:28 +03002214
Dave Airliead7f8a12014-06-05 14:01:32 +10002215 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
Imre Deakbc3bd642017-07-19 14:43:28 +03002216 if (!ret) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002217 DRM_DEBUG_KMS("failed to build sideband msg\n");
Imre Deaka4404252017-07-19 16:46:32 +03002218 return false;
Imre Deakbc3bd642017-07-19 14:43:28 +03002219 }
2220
Dave Airliead7f8a12014-06-05 14:01:32 +10002221 curreply += len;
2222 replylen -= len;
2223 }
Imre Deaka4404252017-07-19 16:46:32 +03002224 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002225}
2226
2227static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2228{
2229 int ret = 0;
2230
Imre Deaka4404252017-07-19 16:46:32 +03002231 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2232 memset(&mgr->down_rep_recv, 0,
2233 sizeof(struct drm_dp_sideband_msg_rx));
2234 return 0;
2235 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002236
2237 if (mgr->down_rep_recv.have_eomt) {
2238 struct drm_dp_sideband_msg_tx *txmsg;
2239 struct drm_dp_mst_branch *mstb;
2240 int slot = -1;
2241 mstb = drm_dp_get_mst_branch_device(mgr,
2242 mgr->down_rep_recv.initial_hdr.lct,
2243 mgr->down_rep_recv.initial_hdr.rad);
2244
2245 if (!mstb) {
2246 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2247 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2248 return 0;
2249 }
2250
2251 /* find the message */
2252 slot = mgr->down_rep_recv.initial_hdr.seqno;
2253 mutex_lock(&mgr->qlock);
2254 txmsg = mstb->tx_slots[slot];
2255 /* remove from slots */
2256 mutex_unlock(&mgr->qlock);
2257
2258 if (!txmsg) {
2259 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2260 mstb,
2261 mgr->down_rep_recv.initial_hdr.seqno,
2262 mgr->down_rep_recv.initial_hdr.lct,
2263 mgr->down_rep_recv.initial_hdr.rad[0],
2264 mgr->down_rep_recv.msg[0]);
2265 drm_dp_put_mst_branch_device(mstb);
2266 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2267 return 0;
2268 }
2269
2270 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2271 if (txmsg->reply.reply_type == 1) {
2272 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);
2273 }
2274
2275 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2276 drm_dp_put_mst_branch_device(mstb);
2277
2278 mutex_lock(&mgr->qlock);
2279 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2280 mstb->tx_slots[slot] = NULL;
2281 mutex_unlock(&mgr->qlock);
2282
2283 wake_up(&mgr->tx_waitq);
2284 }
2285 return ret;
2286}
2287
2288static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2289{
2290 int ret = 0;
Imre Deaka4404252017-07-19 16:46:32 +03002291
2292 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2293 memset(&mgr->up_req_recv, 0,
2294 sizeof(struct drm_dp_sideband_msg_rx));
2295 return 0;
2296 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002297
2298 if (mgr->up_req_recv.have_eomt) {
2299 struct drm_dp_sideband_msg_req_body msg;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002300 struct drm_dp_mst_branch *mstb = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002301 bool seqno;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002302
2303 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2304 mstb = drm_dp_get_mst_branch_device(mgr,
2305 mgr->up_req_recv.initial_hdr.lct,
2306 mgr->up_req_recv.initial_hdr.rad);
2307 if (!mstb) {
2308 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2309 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2310 return 0;
2311 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002312 }
2313
2314 seqno = mgr->up_req_recv.initial_hdr.seqno;
2315 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2316
2317 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002318 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2319
2320 if (!mstb)
2321 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2322
2323 if (!mstb) {
2324 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2325 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2326 return 0;
2327 }
2328
Dave Airliead7f8a12014-06-05 14:01:32 +10002329 drm_dp_update_port(mstb, &msg.u.conn_stat);
Hersen Wu5e93b822016-01-22 17:07:28 -05002330
Dave Airliead7f8a12014-06-05 14:01:32 +10002331 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);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002332 (*mgr->cbs->hotplug)(mgr);
2333
Dave Airliead7f8a12014-06-05 14:01:32 +10002334 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002335 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2336 if (!mstb)
2337 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2338
2339 if (!mstb) {
2340 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2341 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2342 return 0;
2343 }
2344
Dave Airliead7f8a12014-06-05 14:01:32 +10002345 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2346 }
2347
Imre Deak48376e62017-07-19 14:43:29 +03002348 if (mstb)
2349 drm_dp_put_mst_branch_device(mstb);
2350
Dave Airliead7f8a12014-06-05 14:01:32 +10002351 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2352 }
2353 return ret;
2354}
2355
2356/**
2357 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2358 * @mgr: manager to notify irq for.
2359 * @esi: 4 bytes from SINK_COUNT_ESI
Daniel Vetter295ee852014-07-30 14:23:44 +02002360 * @handled: whether the hpd interrupt was consumed or not
Dave Airliead7f8a12014-06-05 14:01:32 +10002361 *
2362 * This should be called from the driver when it detects a short IRQ,
2363 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2364 * topology manager will process the sideband messages received as a result
2365 * of this.
2366 */
2367int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2368{
2369 int ret = 0;
2370 int sc;
2371 *handled = false;
2372 sc = esi[0] & 0x3f;
2373
2374 if (sc != mgr->sink_count) {
2375 mgr->sink_count = sc;
2376 *handled = true;
2377 }
2378
2379 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2380 ret = drm_dp_mst_handle_down_rep(mgr);
2381 *handled = true;
2382 }
2383
2384 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2385 ret |= drm_dp_mst_handle_up_req(mgr);
2386 *handled = true;
2387 }
2388
2389 drm_dp_mst_kick_tx(mgr);
2390 return ret;
2391}
2392EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2393
2394/**
2395 * drm_dp_mst_detect_port() - get connection status for an MST port
Daniel Vetter132d49d2016-07-15 21:48:04 +02002396 * @connector: DRM connector for this port
Dave Airliead7f8a12014-06-05 14:01:32 +10002397 * @mgr: manager for this port
2398 * @port: unverified pointer to a port
2399 *
2400 * This returns the current connection state for a port. It validates the
2401 * port pointer still exists so the caller doesn't require a reference
2402 */
Dave Airliec6a0aed2014-10-20 16:28:02 +10002403enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2404 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10002405{
2406 enum drm_connector_status status = connector_status_disconnected;
2407
2408 /* we need to search for the port in the mgr in case its gone */
2409 port = drm_dp_get_validated_port_ref(mgr, port);
2410 if (!port)
2411 return connector_status_disconnected;
2412
2413 if (!port->ddps)
2414 goto out;
2415
2416 switch (port->pdt) {
2417 case DP_PEER_DEVICE_NONE:
2418 case DP_PEER_DEVICE_MST_BRANCHING:
2419 break;
2420
2421 case DP_PEER_DEVICE_SST_SINK:
2422 status = connector_status_connected;
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002423 /* for logical ports - cache the EDID */
2424 if (port->port_num >= 8 && !port->cached_edid) {
2425 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2426 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002427 break;
2428 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2429 if (port->ldps)
2430 status = connector_status_connected;
2431 break;
2432 }
2433out:
2434 drm_dp_put_port(port);
2435 return status;
2436}
2437EXPORT_SYMBOL(drm_dp_mst_detect_port);
2438
2439/**
Libin Yangef8f9be2015-12-02 14:09:43 +08002440 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2441 * @mgr: manager for this port
2442 * @port: unverified pointer to a port.
2443 *
2444 * This returns whether the port supports audio or not.
2445 */
2446bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2447 struct drm_dp_mst_port *port)
2448{
2449 bool ret = false;
2450
2451 port = drm_dp_get_validated_port_ref(mgr, port);
2452 if (!port)
2453 return ret;
2454 ret = port->has_audio;
2455 drm_dp_put_port(port);
2456 return ret;
2457}
2458EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2459
2460/**
Dave Airliead7f8a12014-06-05 14:01:32 +10002461 * drm_dp_mst_get_edid() - get EDID for an MST port
2462 * @connector: toplevel connector to get EDID for
2463 * @mgr: manager for this port
2464 * @port: unverified pointer to a port.
2465 *
2466 * This returns an EDID for the port connected to a connector,
2467 * It validates the pointer still exists so the caller doesn't require a
2468 * reference.
2469 */
2470struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2471{
2472 struct edid *edid = NULL;
2473
2474 /* we need to search for the port in the mgr in case its gone */
2475 port = drm_dp_get_validated_port_ref(mgr, port);
2476 if (!port)
2477 return NULL;
2478
Dave Airliec6a0aed2014-10-20 16:28:02 +10002479 if (port->cached_edid)
2480 edid = drm_edid_duplicate(port->cached_edid);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002481 else {
2482 edid = drm_get_edid(connector, &port->aux.ddc);
2483 drm_mode_connector_set_tile_property(connector);
2484 }
Libin Yangef8f9be2015-12-02 14:09:43 +08002485 port->has_audio = drm_detect_monitor_audio(edid);
Dave Airliead7f8a12014-06-05 14:01:32 +10002486 drm_dp_put_port(port);
2487 return edid;
2488}
2489EXPORT_SYMBOL(drm_dp_mst_get_edid);
2490
2491/**
2492 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2493 * @mgr: manager to use
2494 * @pbn: payload bandwidth to convert into slots.
2495 */
2496int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2497 int pbn)
2498{
2499 int num_slots;
2500
2501 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2502
2503 if (num_slots > mgr->avail_slots)
2504 return -ENOSPC;
2505 return num_slots;
2506}
2507EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2508
2509static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2510 struct drm_dp_vcpi *vcpi, int pbn)
2511{
2512 int num_slots;
2513 int ret;
2514
2515 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2516
2517 if (num_slots > mgr->avail_slots)
2518 return -ENOSPC;
2519
2520 vcpi->pbn = pbn;
2521 vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2522 vcpi->num_slots = num_slots;
2523
2524 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2525 if (ret < 0)
2526 return ret;
2527 return 0;
2528}
2529
2530/**
2531 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2532 * @mgr: manager for this port
2533 * @port: port to allocate a virtual channel for.
2534 * @pbn: payload bandwidth number to request
2535 * @slots: returned number of slots for this PBN.
2536 */
2537bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2538{
2539 int ret;
2540
2541 port = drm_dp_get_validated_port_ref(mgr, port);
2542 if (!port)
2543 return false;
2544
2545 if (port->vcpi.vcpi > 0) {
2546 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2547 if (pbn == port->vcpi.pbn) {
2548 *slots = port->vcpi.num_slots;
Insu Yundabe1952016-02-01 11:08:29 -05002549 drm_dp_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002550 return true;
2551 }
2552 }
2553
2554 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2555 if (ret) {
2556 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2557 goto out;
2558 }
2559 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2560 *slots = port->vcpi.num_slots;
2561
2562 drm_dp_put_port(port);
2563 return true;
2564out:
2565 return false;
2566}
2567EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2568
Dave Airlie87f59422015-02-24 09:23:55 +10002569int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2570{
2571 int slots = 0;
2572 port = drm_dp_get_validated_port_ref(mgr, port);
2573 if (!port)
2574 return slots;
2575
2576 slots = port->vcpi.num_slots;
2577 drm_dp_put_port(port);
2578 return slots;
2579}
2580EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2581
Dave Airliead7f8a12014-06-05 14:01:32 +10002582/**
2583 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2584 * @mgr: manager for this port
2585 * @port: unverified pointer to a port.
2586 *
2587 * This just resets the number of slots for the ports VCPI for later programming.
2588 */
2589void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2590{
2591 port = drm_dp_get_validated_port_ref(mgr, port);
2592 if (!port)
2593 return;
2594 port->vcpi.num_slots = 0;
2595 drm_dp_put_port(port);
2596}
2597EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2598
2599/**
2600 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2601 * @mgr: manager for this port
2602 * @port: unverified port to deallocate vcpi for
2603 */
2604void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2605{
2606 port = drm_dp_get_validated_port_ref(mgr, port);
2607 if (!port)
2608 return;
2609
2610 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2611 port->vcpi.num_slots = 0;
2612 port->vcpi.pbn = 0;
2613 port->vcpi.aligned_pbn = 0;
2614 port->vcpi.vcpi = 0;
2615 drm_dp_put_port(port);
2616}
2617EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2618
2619static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2620 int id, struct drm_dp_payload *payload)
2621{
2622 u8 payload_alloc[3], status;
2623 int ret;
2624 int retries = 0;
2625
2626 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2627 DP_PAYLOAD_TABLE_UPDATED);
2628
2629 payload_alloc[0] = id;
2630 payload_alloc[1] = payload->start_slot;
2631 payload_alloc[2] = payload->num_slots;
2632
2633 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2634 if (ret != 3) {
2635 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2636 goto fail;
2637 }
2638
2639retry:
2640 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2641 if (ret < 0) {
2642 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2643 goto fail;
2644 }
2645
2646 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2647 retries++;
2648 if (retries < 20) {
2649 usleep_range(10000, 20000);
2650 goto retry;
2651 }
2652 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2653 ret = -EINVAL;
2654 goto fail;
2655 }
2656 ret = 0;
2657fail:
2658 return ret;
2659}
2660
2661
2662/**
2663 * drm_dp_check_act_status() - Check ACT handled status.
2664 * @mgr: manager to use
2665 *
2666 * Check the payload status bits in the DPCD for ACT handled completion.
2667 */
2668int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2669{
2670 u8 status;
2671 int ret;
2672 int count = 0;
2673
2674 do {
2675 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2676
2677 if (ret < 0) {
2678 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2679 goto fail;
2680 }
2681
2682 if (status & DP_PAYLOAD_ACT_HANDLED)
2683 break;
2684 count++;
2685 udelay(100);
2686
2687 } while (count < 30);
2688
2689 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2690 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2691 ret = -EINVAL;
2692 goto fail;
2693 }
2694 return 0;
2695fail:
2696 return ret;
2697}
2698EXPORT_SYMBOL(drm_dp_check_act_status);
2699
2700/**
2701 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2702 * @clock: dot clock for the mode
2703 * @bpp: bpp for the mode.
2704 *
2705 * This uses the formula in the spec to calculate the PBN value for a mode.
2706 */
2707int drm_dp_calc_pbn_mode(int clock, int bpp)
2708{
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002709 u64 kbps;
2710 s64 peak_kbps;
2711 u32 numerator;
2712 u32 denominator;
Dave Airliead7f8a12014-06-05 14:01:32 +10002713
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002714 kbps = clock * bpp;
Dave Airliead7f8a12014-06-05 14:01:32 +10002715
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002716 /*
2717 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2718 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2719 * common multiplier to render an integer PBN for all link rate/lane
2720 * counts combinations
2721 * calculate
2722 * peak_kbps *= (1006/1000)
2723 * peak_kbps *= (64/54)
2724 * peak_kbps *= 8 convert to bytes
2725 */
Dave Airliead7f8a12014-06-05 14:01:32 +10002726
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002727 numerator = 64 * 1006;
2728 denominator = 54 * 8 * 1000 * 1000;
Dave Airliead7f8a12014-06-05 14:01:32 +10002729
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002730 kbps *= numerator;
2731 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
2732
2733 return drm_fixp2int_ceil(peak_kbps);
Dave Airliead7f8a12014-06-05 14:01:32 +10002734}
2735EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2736
2737static int test_calc_pbn_mode(void)
2738{
2739 int ret;
2740 ret = drm_dp_calc_pbn_mode(154000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002741 if (ret != 689) {
2742 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2743 154000, 30, 689, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10002744 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002745 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002746 ret = drm_dp_calc_pbn_mode(234000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002747 if (ret != 1047) {
2748 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2749 234000, 30, 1047, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10002750 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002751 }
2752 ret = drm_dp_calc_pbn_mode(297000, 24);
2753 if (ret != 1063) {
2754 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2755 297000, 24, 1063, ret);
2756 return -EINVAL;
2757 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002758 return 0;
2759}
2760
2761/* we want to kick the TX after we've ack the up/down IRQs. */
2762static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2763{
2764 queue_work(system_long_wq, &mgr->tx_work);
2765}
2766
2767static void drm_dp_mst_dump_mstb(struct seq_file *m,
2768 struct drm_dp_mst_branch *mstb)
2769{
2770 struct drm_dp_mst_port *port;
2771 int tabs = mstb->lct;
2772 char prefix[10];
2773 int i;
2774
2775 for (i = 0; i < tabs; i++)
2776 prefix[i] = '\t';
2777 prefix[i] = '\0';
2778
2779 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2780 list_for_each_entry(port, &mstb->ports, next) {
Jim Bride51108f22016-04-14 10:18:36 -07002781 seq_printf(m, "%sport: %d: input: %d: pdt: %d, ddps: %d ldps: %d, sdp: %d/%d, %p, conn: %p\n", prefix, port->port_num, port->input, port->pdt, port->ddps, port->ldps, port->num_sdp_streams, port->num_sdp_stream_sinks, port, port->connector);
Dave Airliead7f8a12014-06-05 14:01:32 +10002782 if (port->mstb)
2783 drm_dp_mst_dump_mstb(m, port->mstb);
2784 }
2785}
2786
2787static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2788 char *buf)
2789{
2790 int ret;
2791 int i;
2792 for (i = 0; i < 4; i++) {
2793 ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2794 if (ret != 16)
2795 break;
2796 }
2797 if (i == 4)
2798 return true;
2799 return false;
2800}
2801
Jim Bride51108f22016-04-14 10:18:36 -07002802static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
2803 struct drm_dp_mst_port *port, char *name,
2804 int namelen)
2805{
2806 struct edid *mst_edid;
2807
2808 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
2809 drm_edid_get_monitor_name(mst_edid, name, namelen);
2810}
2811
Dave Airliead7f8a12014-06-05 14:01:32 +10002812/**
2813 * drm_dp_mst_dump_topology(): dump topology to seq file.
2814 * @m: seq_file to dump output to
2815 * @mgr: manager to dump current topology for.
2816 *
2817 * helper to dump MST topology to a seq file for debugfs.
2818 */
2819void drm_dp_mst_dump_topology(struct seq_file *m,
2820 struct drm_dp_mst_topology_mgr *mgr)
2821{
2822 int i;
2823 struct drm_dp_mst_port *port;
Jim Bride51108f22016-04-14 10:18:36 -07002824
Dave Airliead7f8a12014-06-05 14:01:32 +10002825 mutex_lock(&mgr->lock);
2826 if (mgr->mst_primary)
2827 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2828
2829 /* dump VCPIs */
2830 mutex_unlock(&mgr->lock);
2831
2832 mutex_lock(&mgr->payload_lock);
Jim Bride51108f22016-04-14 10:18:36 -07002833 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
2834 mgr->max_payloads);
Dave Airliead7f8a12014-06-05 14:01:32 +10002835
2836 for (i = 0; i < mgr->max_payloads; i++) {
2837 if (mgr->proposed_vcpis[i]) {
Jim Bride51108f22016-04-14 10:18:36 -07002838 char name[14];
2839
Dave Airliead7f8a12014-06-05 14:01:32 +10002840 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
Jim Bride51108f22016-04-14 10:18:36 -07002841 fetch_monitor_name(mgr, port, name, sizeof(name));
2842 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
2843 port->port_num, port->vcpi.vcpi,
2844 port->vcpi.num_slots,
2845 (*name != 0) ? name : "Unknown");
Dave Airliead7f8a12014-06-05 14:01:32 +10002846 } else
Jim Bride51108f22016-04-14 10:18:36 -07002847 seq_printf(m, "vcpi %d:unused\n", i);
Dave Airliead7f8a12014-06-05 14:01:32 +10002848 }
2849 for (i = 0; i < mgr->max_payloads; i++) {
2850 seq_printf(m, "payload %d: %d, %d, %d\n",
2851 i,
2852 mgr->payloads[i].payload_state,
2853 mgr->payloads[i].start_slot,
2854 mgr->payloads[i].num_slots);
2855
2856
2857 }
2858 mutex_unlock(&mgr->payload_lock);
2859
2860 mutex_lock(&mgr->lock);
2861 if (mgr->mst_primary) {
2862 u8 buf[64];
2863 bool bret;
2864 int ret;
2865 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2866 seq_printf(m, "dpcd: ");
2867 for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2868 seq_printf(m, "%02x ", buf[i]);
2869 seq_printf(m, "\n");
2870 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2871 seq_printf(m, "faux/mst: ");
2872 for (i = 0; i < 2; i++)
2873 seq_printf(m, "%02x ", buf[i]);
2874 seq_printf(m, "\n");
2875 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2876 seq_printf(m, "mst ctrl: ");
2877 for (i = 0; i < 1; i++)
2878 seq_printf(m, "%02x ", buf[i]);
2879 seq_printf(m, "\n");
2880
Dave Airlie44790462015-07-14 11:33:31 +10002881 /* dump the standard OUI branch header */
2882 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2883 seq_printf(m, "branch oui: ");
2884 for (i = 0; i < 0x3; i++)
2885 seq_printf(m, "%02x", buf[i]);
2886 seq_printf(m, " devid: ");
Jim Bride51108f22016-04-14 10:18:36 -07002887 for (i = 0x3; i < 0x8 && buf[i]; i++)
Dave Airlie44790462015-07-14 11:33:31 +10002888 seq_printf(m, "%c", buf[i]);
Jim Bride51108f22016-04-14 10:18:36 -07002889
Dave Airlie44790462015-07-14 11:33:31 +10002890 seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2891 seq_printf(m, "\n");
Dave Airliead7f8a12014-06-05 14:01:32 +10002892 bret = dump_dp_payload_table(mgr, buf);
2893 if (bret == true) {
2894 seq_printf(m, "payload table: ");
2895 for (i = 0; i < 63; i++)
2896 seq_printf(m, "%02x ", buf[i]);
2897 seq_printf(m, "\n");
2898 }
2899
2900 }
2901
2902 mutex_unlock(&mgr->lock);
2903
2904}
2905EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2906
2907static void drm_dp_tx_work(struct work_struct *work)
2908{
2909 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2910
2911 mutex_lock(&mgr->qlock);
Daniel Vettercb021a32016-07-15 21:48:03 +02002912 if (!list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10002913 process_single_down_tx_qlock(mgr);
2914 mutex_unlock(&mgr->qlock);
2915}
2916
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002917static void drm_dp_free_mst_port(struct kref *kref)
2918{
2919 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
2920 kref_put(&port->parent->kref, drm_dp_free_mst_branch_device);
2921 kfree(port);
2922}
2923
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002924static void drm_dp_destroy_connector_work(struct work_struct *work)
2925{
2926 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002927 struct drm_dp_mst_port *port;
Dave Airliedf4839f2015-09-16 10:37:28 +10002928 bool send_hotplug = false;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002929 /*
2930 * Not a regular list traverse as we have to drop the destroy
2931 * connector lock before destroying the connector, to avoid AB->BA
2932 * ordering between this lock and the config mutex.
2933 */
2934 for (;;) {
2935 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002936 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2937 if (!port) {
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002938 mutex_unlock(&mgr->destroy_connector_lock);
2939 break;
2940 }
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002941 list_del(&port->next);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002942 mutex_unlock(&mgr->destroy_connector_lock);
2943
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002944 kref_init(&port->kref);
2945 INIT_LIST_HEAD(&port->next);
2946
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002947 mgr->cbs->destroy_connector(mgr, port->connector);
2948
2949 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +03002950 port->pdt = DP_PEER_DEVICE_NONE;
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02002951
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002952 if (!port->input && port->vcpi.vcpi > 0) {
Andrey Grodzovskyfd2d2ba2016-05-25 16:45:43 -04002953 drm_dp_mst_reset_vcpi_slots(mgr, port);
2954 drm_dp_update_payload_part1(mgr);
2955 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002956 }
2957
2958 kref_put(&port->kref, drm_dp_free_mst_port);
Dave Airliedf4839f2015-09-16 10:37:28 +10002959 send_hotplug = true;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002960 }
Dave Airliedf4839f2015-09-16 10:37:28 +10002961 if (send_hotplug)
2962 (*mgr->cbs->hotplug)(mgr);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002963}
2964
Dave Airliead7f8a12014-06-05 14:01:32 +10002965/**
2966 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2967 * @mgr: manager struct to initialise
2968 * @dev: device providing this structure - for i2c addition.
2969 * @aux: DP helper aux channel to talk to this device
2970 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2971 * @max_payloads: maximum number of payloads this GPU can source
2972 * @conn_base_id: the connector object ID the MST device is connected to.
2973 *
2974 * Return 0 for success, or negative error code on failure
2975 */
2976int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2977 struct device *dev, struct drm_dp_aux *aux,
2978 int max_dpcd_transaction_bytes,
2979 int max_payloads, int conn_base_id)
2980{
2981 mutex_init(&mgr->lock);
2982 mutex_init(&mgr->qlock);
2983 mutex_init(&mgr->payload_lock);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002984 mutex_init(&mgr->destroy_connector_lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10002985 INIT_LIST_HEAD(&mgr->tx_msg_downq);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002986 INIT_LIST_HEAD(&mgr->destroy_connector_list);
Dave Airliead7f8a12014-06-05 14:01:32 +10002987 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2988 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10002989 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002990 init_waitqueue_head(&mgr->tx_waitq);
2991 mgr->dev = dev;
2992 mgr->aux = aux;
2993 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2994 mgr->max_payloads = max_payloads;
2995 mgr->conn_base_id = conn_base_id;
Imre Deak4d6a10d2016-01-29 14:44:29 +02002996 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
2997 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
2998 return -EINVAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002999 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3000 if (!mgr->payloads)
3001 return -ENOMEM;
3002 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3003 if (!mgr->proposed_vcpis)
3004 return -ENOMEM;
3005 set_bit(0, &mgr->payload_mask);
Imre Deak441388a2016-01-29 14:44:28 +02003006 if (test_calc_pbn_mode() < 0)
3007 DRM_ERROR("MST PBN self-test failed\n");
3008
Dave Airliead7f8a12014-06-05 14:01:32 +10003009 return 0;
3010}
3011EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3012
3013/**
3014 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3015 * @mgr: manager to destroy
3016 */
3017void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3018{
Dave Airlie274d8352015-09-30 10:39:42 +10003019 flush_work(&mgr->work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003020 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10003021 mutex_lock(&mgr->payload_lock);
3022 kfree(mgr->payloads);
3023 mgr->payloads = NULL;
3024 kfree(mgr->proposed_vcpis);
3025 mgr->proposed_vcpis = NULL;
3026 mutex_unlock(&mgr->payload_lock);
3027 mgr->dev = NULL;
3028 mgr->aux = NULL;
3029}
3030EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3031
3032/* I2C device */
3033static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3034 int num)
3035{
3036 struct drm_dp_aux *aux = adapter->algo_data;
3037 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3038 struct drm_dp_mst_branch *mstb;
3039 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3040 unsigned int i;
3041 bool reading = false;
3042 struct drm_dp_sideband_msg_req_body msg;
3043 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3044 int ret;
3045
3046 mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
3047 if (!mstb)
3048 return -EREMOTEIO;
3049
3050 /* construct i2c msg */
3051 /* see if last msg is a read */
3052 if (msgs[num - 1].flags & I2C_M_RD)
3053 reading = true;
3054
Dave Airlieae491542015-10-14 18:51:17 +10003055 if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
Dave Airliead7f8a12014-06-05 14:01:32 +10003056 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3057 ret = -EIO;
3058 goto out;
3059 }
3060
Dave Airlieae491542015-10-14 18:51:17 +10003061 memset(&msg, 0, sizeof(msg));
Dave Airliead7f8a12014-06-05 14:01:32 +10003062 msg.req_type = DP_REMOTE_I2C_READ;
3063 msg.u.i2c_read.num_transactions = num - 1;
3064 msg.u.i2c_read.port_number = port->port_num;
3065 for (i = 0; i < num - 1; i++) {
3066 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3067 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3068 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3069 }
3070 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3071 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3072
3073 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3074 if (!txmsg) {
3075 ret = -ENOMEM;
3076 goto out;
3077 }
3078
3079 txmsg->dst = mstb;
3080 drm_dp_encode_sideband_req(&msg, txmsg);
3081
3082 drm_dp_queue_down_tx(mgr, txmsg);
3083
3084 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3085 if (ret > 0) {
3086
3087 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3088 ret = -EREMOTEIO;
3089 goto out;
3090 }
3091 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3092 ret = -EIO;
3093 goto out;
3094 }
3095 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3096 ret = num;
3097 }
3098out:
3099 kfree(txmsg);
3100 drm_dp_put_mst_branch_device(mstb);
3101 return ret;
3102}
3103
3104static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3105{
3106 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3107 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3108 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3109 I2C_FUNC_10BIT_ADDR;
3110}
3111
3112static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3113 .functionality = drm_dp_mst_i2c_functionality,
3114 .master_xfer = drm_dp_mst_i2c_xfer,
3115};
3116
3117/**
3118 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3119 * @aux: DisplayPort AUX channel
3120 *
3121 * Returns 0 on success or a negative error code on failure.
3122 */
3123static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3124{
3125 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3126 aux->ddc.algo_data = aux;
3127 aux->ddc.retries = 3;
3128
3129 aux->ddc.class = I2C_CLASS_DDC;
3130 aux->ddc.owner = THIS_MODULE;
3131 aux->ddc.dev.parent = aux->dev;
3132 aux->ddc.dev.of_node = aux->dev->of_node;
3133
3134 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3135 sizeof(aux->ddc.name));
3136
3137 return i2c_add_adapter(&aux->ddc);
3138}
3139
3140/**
3141 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3142 * @aux: DisplayPort AUX channel
3143 */
3144static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3145{
3146 i2c_del_adapter(&aux->ddc);
3147}