]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/gpu/drm/drm_dp_mst_topology.c
Merge tag 'drm-intel-next-2019-05-24' of git://anongit.freedesktop.org/drm/drm-intel...
[thirdparty/kernel/stable.git] / drivers / gpu / drm / drm_dp_mst_topology.c
1 /*
2 * Copyright © 2014 Red Hat
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/seq_file.h>
30
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_dp_mst_helper.h>
34 #include <drm/drm_drv.h>
35 #include <drm/drm_fixed.h>
36 #include <drm/drm_print.h>
37 #include <drm/drm_probe_helper.h>
38
39 /**
40 * DOC: dp mst helper
41 *
42 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
43 * protocol. The helpers contain a topology manager and bandwidth manager.
44 * The helpers encapsulate the sending and received of sideband msgs.
45 */
46 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
47 char *buf);
48 static int test_calc_pbn_mode(void);
49
50 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
51
52 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
53 int id,
54 struct drm_dp_payload *payload);
55
56 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
57 struct drm_dp_mst_port *port,
58 int offset, int size, u8 *bytes);
59
60 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
61 struct drm_dp_mst_branch *mstb);
62 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
63 struct drm_dp_mst_branch *mstb,
64 struct drm_dp_mst_port *port);
65 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
66 u8 *guid);
67
68 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
69 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
70 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
71
72 #define DP_STR(x) [DP_ ## x] = #x
73
74 static const char *drm_dp_mst_req_type_str(u8 req_type)
75 {
76 static const char * const req_type_str[] = {
77 DP_STR(GET_MSG_TRANSACTION_VERSION),
78 DP_STR(LINK_ADDRESS),
79 DP_STR(CONNECTION_STATUS_NOTIFY),
80 DP_STR(ENUM_PATH_RESOURCES),
81 DP_STR(ALLOCATE_PAYLOAD),
82 DP_STR(QUERY_PAYLOAD),
83 DP_STR(RESOURCE_STATUS_NOTIFY),
84 DP_STR(CLEAR_PAYLOAD_ID_TABLE),
85 DP_STR(REMOTE_DPCD_READ),
86 DP_STR(REMOTE_DPCD_WRITE),
87 DP_STR(REMOTE_I2C_READ),
88 DP_STR(REMOTE_I2C_WRITE),
89 DP_STR(POWER_UP_PHY),
90 DP_STR(POWER_DOWN_PHY),
91 DP_STR(SINK_EVENT_NOTIFY),
92 DP_STR(QUERY_STREAM_ENC_STATUS),
93 };
94
95 if (req_type >= ARRAY_SIZE(req_type_str) ||
96 !req_type_str[req_type])
97 return "unknown";
98
99 return req_type_str[req_type];
100 }
101
102 #undef DP_STR
103 #define DP_STR(x) [DP_NAK_ ## x] = #x
104
105 static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
106 {
107 static const char * const nak_reason_str[] = {
108 DP_STR(WRITE_FAILURE),
109 DP_STR(INVALID_READ),
110 DP_STR(CRC_FAILURE),
111 DP_STR(BAD_PARAM),
112 DP_STR(DEFER),
113 DP_STR(LINK_FAILURE),
114 DP_STR(NO_RESOURCES),
115 DP_STR(DPCD_FAIL),
116 DP_STR(I2C_NAK),
117 DP_STR(ALLOCATE_FAIL),
118 };
119
120 if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
121 !nak_reason_str[nak_reason])
122 return "unknown";
123
124 return nak_reason_str[nak_reason];
125 }
126
127 #undef DP_STR
128
129 /* sideband msg handling */
130 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
131 {
132 u8 bitmask = 0x80;
133 u8 bitshift = 7;
134 u8 array_index = 0;
135 int number_of_bits = num_nibbles * 4;
136 u8 remainder = 0;
137
138 while (number_of_bits != 0) {
139 number_of_bits--;
140 remainder <<= 1;
141 remainder |= (data[array_index] & bitmask) >> bitshift;
142 bitmask >>= 1;
143 bitshift--;
144 if (bitmask == 0) {
145 bitmask = 0x80;
146 bitshift = 7;
147 array_index++;
148 }
149 if ((remainder & 0x10) == 0x10)
150 remainder ^= 0x13;
151 }
152
153 number_of_bits = 4;
154 while (number_of_bits != 0) {
155 number_of_bits--;
156 remainder <<= 1;
157 if ((remainder & 0x10) != 0)
158 remainder ^= 0x13;
159 }
160
161 return remainder;
162 }
163
164 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
165 {
166 u8 bitmask = 0x80;
167 u8 bitshift = 7;
168 u8 array_index = 0;
169 int number_of_bits = number_of_bytes * 8;
170 u16 remainder = 0;
171
172 while (number_of_bits != 0) {
173 number_of_bits--;
174 remainder <<= 1;
175 remainder |= (data[array_index] & bitmask) >> bitshift;
176 bitmask >>= 1;
177 bitshift--;
178 if (bitmask == 0) {
179 bitmask = 0x80;
180 bitshift = 7;
181 array_index++;
182 }
183 if ((remainder & 0x100) == 0x100)
184 remainder ^= 0xd5;
185 }
186
187 number_of_bits = 8;
188 while (number_of_bits != 0) {
189 number_of_bits--;
190 remainder <<= 1;
191 if ((remainder & 0x100) != 0)
192 remainder ^= 0xd5;
193 }
194
195 return remainder & 0xff;
196 }
197 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
198 {
199 u8 size = 3;
200 size += (hdr->lct / 2);
201 return size;
202 }
203
204 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
205 u8 *buf, int *len)
206 {
207 int idx = 0;
208 int i;
209 u8 crc4;
210 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
211 for (i = 0; i < (hdr->lct / 2); i++)
212 buf[idx++] = hdr->rad[i];
213 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
214 (hdr->msg_len & 0x3f);
215 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
216
217 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
218 buf[idx - 1] |= (crc4 & 0xf);
219
220 *len = idx;
221 }
222
223 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
224 u8 *buf, int buflen, u8 *hdrlen)
225 {
226 u8 crc4;
227 u8 len;
228 int i;
229 u8 idx;
230 if (buf[0] == 0)
231 return false;
232 len = 3;
233 len += ((buf[0] & 0xf0) >> 4) / 2;
234 if (len > buflen)
235 return false;
236 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
237
238 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
239 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
240 return false;
241 }
242
243 hdr->lct = (buf[0] & 0xf0) >> 4;
244 hdr->lcr = (buf[0] & 0xf);
245 idx = 1;
246 for (i = 0; i < (hdr->lct / 2); i++)
247 hdr->rad[i] = buf[idx++];
248 hdr->broadcast = (buf[idx] >> 7) & 0x1;
249 hdr->path_msg = (buf[idx] >> 6) & 0x1;
250 hdr->msg_len = buf[idx] & 0x3f;
251 idx++;
252 hdr->somt = (buf[idx] >> 7) & 0x1;
253 hdr->eomt = (buf[idx] >> 6) & 0x1;
254 hdr->seqno = (buf[idx] >> 4) & 0x1;
255 idx++;
256 *hdrlen = idx;
257 return true;
258 }
259
260 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
261 struct drm_dp_sideband_msg_tx *raw)
262 {
263 int idx = 0;
264 int i;
265 u8 *buf = raw->msg;
266 buf[idx++] = req->req_type & 0x7f;
267
268 switch (req->req_type) {
269 case DP_ENUM_PATH_RESOURCES:
270 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
271 idx++;
272 break;
273 case DP_ALLOCATE_PAYLOAD:
274 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
275 (req->u.allocate_payload.number_sdp_streams & 0xf);
276 idx++;
277 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
278 idx++;
279 buf[idx] = (req->u.allocate_payload.pbn >> 8);
280 idx++;
281 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
282 idx++;
283 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
284 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
285 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
286 idx++;
287 }
288 if (req->u.allocate_payload.number_sdp_streams & 1) {
289 i = req->u.allocate_payload.number_sdp_streams - 1;
290 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
291 idx++;
292 }
293 break;
294 case DP_QUERY_PAYLOAD:
295 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
296 idx++;
297 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
298 idx++;
299 break;
300 case DP_REMOTE_DPCD_READ:
301 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
302 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
303 idx++;
304 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
305 idx++;
306 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
307 idx++;
308 buf[idx] = (req->u.dpcd_read.num_bytes);
309 idx++;
310 break;
311
312 case DP_REMOTE_DPCD_WRITE:
313 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
314 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
315 idx++;
316 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
317 idx++;
318 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
319 idx++;
320 buf[idx] = (req->u.dpcd_write.num_bytes);
321 idx++;
322 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
323 idx += req->u.dpcd_write.num_bytes;
324 break;
325 case DP_REMOTE_I2C_READ:
326 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
327 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
328 idx++;
329 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
330 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
331 idx++;
332 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
333 idx++;
334 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
335 idx += req->u.i2c_read.transactions[i].num_bytes;
336
337 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
338 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
339 idx++;
340 }
341 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
342 idx++;
343 buf[idx] = (req->u.i2c_read.num_bytes_read);
344 idx++;
345 break;
346
347 case DP_REMOTE_I2C_WRITE:
348 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
349 idx++;
350 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
351 idx++;
352 buf[idx] = (req->u.i2c_write.num_bytes);
353 idx++;
354 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
355 idx += req->u.i2c_write.num_bytes;
356 break;
357
358 case DP_POWER_DOWN_PHY:
359 case DP_POWER_UP_PHY:
360 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
361 idx++;
362 break;
363 }
364 raw->cur_len = idx;
365 }
366
367 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
368 {
369 u8 crc4;
370 crc4 = drm_dp_msg_data_crc4(msg, len);
371 msg[len] = crc4;
372 }
373
374 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
375 struct drm_dp_sideband_msg_tx *raw)
376 {
377 int idx = 0;
378 u8 *buf = raw->msg;
379
380 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
381
382 raw->cur_len = idx;
383 }
384
385 /* this adds a chunk of msg to the builder to get the final msg */
386 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
387 u8 *replybuf, u8 replybuflen, bool hdr)
388 {
389 int ret;
390 u8 crc4;
391
392 if (hdr) {
393 u8 hdrlen;
394 struct drm_dp_sideband_msg_hdr recv_hdr;
395 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
396 if (ret == false) {
397 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
398 return false;
399 }
400
401 /*
402 * ignore out-of-order messages or messages that are part of a
403 * failed transaction
404 */
405 if (!recv_hdr.somt && !msg->have_somt)
406 return false;
407
408 /* get length contained in this portion */
409 msg->curchunk_len = recv_hdr.msg_len;
410 msg->curchunk_hdrlen = hdrlen;
411
412 /* we have already gotten an somt - don't bother parsing */
413 if (recv_hdr.somt && msg->have_somt)
414 return false;
415
416 if (recv_hdr.somt) {
417 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
418 msg->have_somt = true;
419 }
420 if (recv_hdr.eomt)
421 msg->have_eomt = true;
422
423 /* copy the bytes for the remainder of this header chunk */
424 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
425 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
426 } else {
427 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
428 msg->curchunk_idx += replybuflen;
429 }
430
431 if (msg->curchunk_idx >= msg->curchunk_len) {
432 /* do CRC */
433 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
434 /* copy chunk into bigger msg */
435 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
436 msg->curlen += msg->curchunk_len - 1;
437 }
438 return true;
439 }
440
441 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
442 struct drm_dp_sideband_msg_reply_body *repmsg)
443 {
444 int idx = 1;
445 int i;
446 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
447 idx += 16;
448 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
449 idx++;
450 if (idx > raw->curlen)
451 goto fail_len;
452 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
453 if (raw->msg[idx] & 0x80)
454 repmsg->u.link_addr.ports[i].input_port = 1;
455
456 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
457 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
458
459 idx++;
460 if (idx > raw->curlen)
461 goto fail_len;
462 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
463 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
464 if (repmsg->u.link_addr.ports[i].input_port == 0)
465 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
466 idx++;
467 if (idx > raw->curlen)
468 goto fail_len;
469 if (repmsg->u.link_addr.ports[i].input_port == 0) {
470 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
471 idx++;
472 if (idx > raw->curlen)
473 goto fail_len;
474 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
475 idx += 16;
476 if (idx > raw->curlen)
477 goto fail_len;
478 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
479 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
480 idx++;
481
482 }
483 if (idx > raw->curlen)
484 goto fail_len;
485 }
486
487 return true;
488 fail_len:
489 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
490 return false;
491 }
492
493 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
494 struct drm_dp_sideband_msg_reply_body *repmsg)
495 {
496 int idx = 1;
497 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
498 idx++;
499 if (idx > raw->curlen)
500 goto fail_len;
501 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
502 idx++;
503 if (idx > raw->curlen)
504 goto fail_len;
505
506 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
507 return true;
508 fail_len:
509 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
510 return false;
511 }
512
513 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
514 struct drm_dp_sideband_msg_reply_body *repmsg)
515 {
516 int idx = 1;
517 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
518 idx++;
519 if (idx > raw->curlen)
520 goto fail_len;
521 return true;
522 fail_len:
523 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
524 return false;
525 }
526
527 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
528 struct drm_dp_sideband_msg_reply_body *repmsg)
529 {
530 int idx = 1;
531
532 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
533 idx++;
534 if (idx > raw->curlen)
535 goto fail_len;
536 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
537 idx++;
538 /* TODO check */
539 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
540 return true;
541 fail_len:
542 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
543 return false;
544 }
545
546 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
547 struct drm_dp_sideband_msg_reply_body *repmsg)
548 {
549 int idx = 1;
550 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
551 idx++;
552 if (idx > raw->curlen)
553 goto fail_len;
554 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
555 idx += 2;
556 if (idx > raw->curlen)
557 goto fail_len;
558 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
559 idx += 2;
560 if (idx > raw->curlen)
561 goto fail_len;
562 return true;
563 fail_len:
564 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
565 return false;
566 }
567
568 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
569 struct drm_dp_sideband_msg_reply_body *repmsg)
570 {
571 int idx = 1;
572 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
573 idx++;
574 if (idx > raw->curlen)
575 goto fail_len;
576 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
577 idx++;
578 if (idx > raw->curlen)
579 goto fail_len;
580 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
581 idx += 2;
582 if (idx > raw->curlen)
583 goto fail_len;
584 return true;
585 fail_len:
586 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
587 return false;
588 }
589
590 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
591 struct drm_dp_sideband_msg_reply_body *repmsg)
592 {
593 int idx = 1;
594 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
595 idx++;
596 if (idx > raw->curlen)
597 goto fail_len;
598 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
599 idx += 2;
600 if (idx > raw->curlen)
601 goto fail_len;
602 return true;
603 fail_len:
604 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
605 return false;
606 }
607
608 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
609 struct drm_dp_sideband_msg_reply_body *repmsg)
610 {
611 int idx = 1;
612
613 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
614 idx++;
615 if (idx > raw->curlen) {
616 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
617 idx, raw->curlen);
618 return false;
619 }
620 return true;
621 }
622
623 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
624 struct drm_dp_sideband_msg_reply_body *msg)
625 {
626 memset(msg, 0, sizeof(*msg));
627 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
628 msg->req_type = (raw->msg[0] & 0x7f);
629
630 if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
631 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
632 msg->u.nak.reason = raw->msg[17];
633 msg->u.nak.nak_data = raw->msg[18];
634 return false;
635 }
636
637 switch (msg->req_type) {
638 case DP_LINK_ADDRESS:
639 return drm_dp_sideband_parse_link_address(raw, msg);
640 case DP_QUERY_PAYLOAD:
641 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
642 case DP_REMOTE_DPCD_READ:
643 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
644 case DP_REMOTE_DPCD_WRITE:
645 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
646 case DP_REMOTE_I2C_READ:
647 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
648 case DP_ENUM_PATH_RESOURCES:
649 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
650 case DP_ALLOCATE_PAYLOAD:
651 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
652 case DP_POWER_DOWN_PHY:
653 case DP_POWER_UP_PHY:
654 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
655 default:
656 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
657 drm_dp_mst_req_type_str(msg->req_type));
658 return false;
659 }
660 }
661
662 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
663 struct drm_dp_sideband_msg_req_body *msg)
664 {
665 int idx = 1;
666
667 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
668 idx++;
669 if (idx > raw->curlen)
670 goto fail_len;
671
672 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
673 idx += 16;
674 if (idx > raw->curlen)
675 goto fail_len;
676
677 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
678 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
679 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
680 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
681 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
682 idx++;
683 return true;
684 fail_len:
685 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
686 return false;
687 }
688
689 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
690 struct drm_dp_sideband_msg_req_body *msg)
691 {
692 int idx = 1;
693
694 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
695 idx++;
696 if (idx > raw->curlen)
697 goto fail_len;
698
699 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
700 idx += 16;
701 if (idx > raw->curlen)
702 goto fail_len;
703
704 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
705 idx++;
706 return true;
707 fail_len:
708 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
709 return false;
710 }
711
712 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
713 struct drm_dp_sideband_msg_req_body *msg)
714 {
715 memset(msg, 0, sizeof(*msg));
716 msg->req_type = (raw->msg[0] & 0x7f);
717
718 switch (msg->req_type) {
719 case DP_CONNECTION_STATUS_NOTIFY:
720 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
721 case DP_RESOURCE_STATUS_NOTIFY:
722 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
723 default:
724 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,
725 drm_dp_mst_req_type_str(msg->req_type));
726 return false;
727 }
728 }
729
730 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
731 {
732 struct drm_dp_sideband_msg_req_body req;
733
734 req.req_type = DP_REMOTE_DPCD_WRITE;
735 req.u.dpcd_write.port_number = port_num;
736 req.u.dpcd_write.dpcd_address = offset;
737 req.u.dpcd_write.num_bytes = num_bytes;
738 req.u.dpcd_write.bytes = bytes;
739 drm_dp_encode_sideband_req(&req, msg);
740
741 return 0;
742 }
743
744 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
745 {
746 struct drm_dp_sideband_msg_req_body req;
747
748 req.req_type = DP_LINK_ADDRESS;
749 drm_dp_encode_sideband_req(&req, msg);
750 return 0;
751 }
752
753 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
754 {
755 struct drm_dp_sideband_msg_req_body req;
756
757 req.req_type = DP_ENUM_PATH_RESOURCES;
758 req.u.port_num.port_number = port_num;
759 drm_dp_encode_sideband_req(&req, msg);
760 msg->path_msg = true;
761 return 0;
762 }
763
764 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
765 u8 vcpi, uint16_t pbn,
766 u8 number_sdp_streams,
767 u8 *sdp_stream_sink)
768 {
769 struct drm_dp_sideband_msg_req_body req;
770 memset(&req, 0, sizeof(req));
771 req.req_type = DP_ALLOCATE_PAYLOAD;
772 req.u.allocate_payload.port_number = port_num;
773 req.u.allocate_payload.vcpi = vcpi;
774 req.u.allocate_payload.pbn = pbn;
775 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
776 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
777 number_sdp_streams);
778 drm_dp_encode_sideband_req(&req, msg);
779 msg->path_msg = true;
780 return 0;
781 }
782
783 static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
784 int port_num, bool power_up)
785 {
786 struct drm_dp_sideband_msg_req_body req;
787
788 if (power_up)
789 req.req_type = DP_POWER_UP_PHY;
790 else
791 req.req_type = DP_POWER_DOWN_PHY;
792
793 req.u.port_num.port_number = port_num;
794 drm_dp_encode_sideband_req(&req, msg);
795 msg->path_msg = true;
796 return 0;
797 }
798
799 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
800 struct drm_dp_vcpi *vcpi)
801 {
802 int ret, vcpi_ret;
803
804 mutex_lock(&mgr->payload_lock);
805 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
806 if (ret > mgr->max_payloads) {
807 ret = -EINVAL;
808 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
809 goto out_unlock;
810 }
811
812 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
813 if (vcpi_ret > mgr->max_payloads) {
814 ret = -EINVAL;
815 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
816 goto out_unlock;
817 }
818
819 set_bit(ret, &mgr->payload_mask);
820 set_bit(vcpi_ret, &mgr->vcpi_mask);
821 vcpi->vcpi = vcpi_ret + 1;
822 mgr->proposed_vcpis[ret - 1] = vcpi;
823 out_unlock:
824 mutex_unlock(&mgr->payload_lock);
825 return ret;
826 }
827
828 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
829 int vcpi)
830 {
831 int i;
832 if (vcpi == 0)
833 return;
834
835 mutex_lock(&mgr->payload_lock);
836 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
837 clear_bit(vcpi - 1, &mgr->vcpi_mask);
838
839 for (i = 0; i < mgr->max_payloads; i++) {
840 if (mgr->proposed_vcpis[i])
841 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
842 mgr->proposed_vcpis[i] = NULL;
843 clear_bit(i + 1, &mgr->payload_mask);
844 }
845 }
846 mutex_unlock(&mgr->payload_lock);
847 }
848
849 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
850 struct drm_dp_sideband_msg_tx *txmsg)
851 {
852 unsigned int state;
853
854 /*
855 * All updates to txmsg->state are protected by mgr->qlock, and the two
856 * cases we check here are terminal states. For those the barriers
857 * provided by the wake_up/wait_event pair are enough.
858 */
859 state = READ_ONCE(txmsg->state);
860 return (state == DRM_DP_SIDEBAND_TX_RX ||
861 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
862 }
863
864 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
865 struct drm_dp_sideband_msg_tx *txmsg)
866 {
867 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
868 int ret;
869
870 ret = wait_event_timeout(mgr->tx_waitq,
871 check_txmsg_state(mgr, txmsg),
872 (4 * HZ));
873 mutex_lock(&mstb->mgr->qlock);
874 if (ret > 0) {
875 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
876 ret = -EIO;
877 goto out;
878 }
879 } else {
880 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
881
882 /* dump some state */
883 ret = -EIO;
884
885 /* remove from q */
886 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
887 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
888 list_del(&txmsg->next);
889 }
890
891 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
892 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
893 mstb->tx_slots[txmsg->seqno] = NULL;
894 }
895 }
896 out:
897 mutex_unlock(&mgr->qlock);
898
899 return ret;
900 }
901
902 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
903 {
904 struct drm_dp_mst_branch *mstb;
905
906 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
907 if (!mstb)
908 return NULL;
909
910 mstb->lct = lct;
911 if (lct > 1)
912 memcpy(mstb->rad, rad, lct / 2);
913 INIT_LIST_HEAD(&mstb->ports);
914 kref_init(&mstb->topology_kref);
915 kref_init(&mstb->malloc_kref);
916 return mstb;
917 }
918
919 static void drm_dp_free_mst_branch_device(struct kref *kref)
920 {
921 struct drm_dp_mst_branch *mstb =
922 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
923
924 if (mstb->port_parent)
925 drm_dp_mst_put_port_malloc(mstb->port_parent);
926
927 kfree(mstb);
928 }
929
930 /**
931 * DOC: Branch device and port refcounting
932 *
933 * Topology refcount overview
934 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
935 *
936 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
937 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
938 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
939 *
940 * Topology refcounts are not exposed to drivers, and are handled internally
941 * by the DP MST helpers. The helpers use them in order to prevent the
942 * in-memory topology state from being changed in the middle of critical
943 * operations like changing the internal state of payload allocations. This
944 * means each branch and port will be considered to be connected to the rest
945 * of the topology until its topology refcount reaches zero. Additionally,
946 * for ports this means that their associated &struct drm_connector will stay
947 * registered with userspace until the port's refcount reaches 0.
948 *
949 * Malloc refcount overview
950 * ~~~~~~~~~~~~~~~~~~~~~~~~
951 *
952 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
953 * drm_dp_mst_branch allocated even after all of its topology references have
954 * been dropped, so that the driver or MST helpers can safely access each
955 * branch's last known state before it was disconnected from the topology.
956 * When the malloc refcount of a port or branch reaches 0, the memory
957 * allocation containing the &struct drm_dp_mst_branch or &struct
958 * drm_dp_mst_port respectively will be freed.
959 *
960 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
961 * to drivers. As of writing this documentation, there are no drivers that
962 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
963 * helpers. Exposing this API to drivers in a race-free manner would take more
964 * tweaking of the refcounting scheme, however patches are welcome provided
965 * there is a legitimate driver usecase for this.
966 *
967 * Refcount relationships in a topology
968 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
969 *
970 * Let's take a look at why the relationship between topology and malloc
971 * refcounts is designed the way it is.
972 *
973 * .. kernel-figure:: dp-mst/topology-figure-1.dot
974 *
975 * An example of topology and malloc refs in a DP MST topology with two
976 * active payloads. Topology refcount increments are indicated by solid
977 * lines, and malloc refcount increments are indicated by dashed lines.
978 * Each starts from the branch which incremented the refcount, and ends at
979 * the branch to which the refcount belongs to, i.e. the arrow points the
980 * same way as the C pointers used to reference a structure.
981 *
982 * As you can see in the above figure, every branch increments the topology
983 * refcount of its children, and increments the malloc refcount of its
984 * parent. Additionally, every payload increments the malloc refcount of its
985 * assigned port by 1.
986 *
987 * So, what would happen if MSTB #3 from the above figure was unplugged from
988 * the system, but the driver hadn't yet removed payload #2 from port #3? The
989 * topology would start to look like the figure below.
990 *
991 * .. kernel-figure:: dp-mst/topology-figure-2.dot
992 *
993 * Ports and branch devices which have been released from memory are
994 * colored grey, and references which have been removed are colored red.
995 *
996 * Whenever a port or branch device's topology refcount reaches zero, it will
997 * decrement the topology refcounts of all its children, the malloc refcount
998 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
999 * #4, this means they both have been disconnected from the topology and freed
1000 * from memory. But, because payload #2 is still holding a reference to port
1001 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1002 * is still accessible from memory. This also means port #3 has not yet
1003 * decremented the malloc refcount of MSTB #3, so its &struct
1004 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1005 * malloc refcount reaches 0.
1006 *
1007 * This relationship is necessary because in order to release payload #2, we
1008 * need to be able to figure out the last relative of port #3 that's still
1009 * connected to the topology. In this case, we would travel up the topology as
1010 * shown below.
1011 *
1012 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1013 *
1014 * And finally, remove payload #2 by communicating with port #2 through
1015 * sideband transactions.
1016 */
1017
1018 /**
1019 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1020 * device
1021 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1022 *
1023 * Increments &drm_dp_mst_branch.malloc_kref. When
1024 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1025 * will be released and @mstb may no longer be used.
1026 *
1027 * See also: drm_dp_mst_put_mstb_malloc()
1028 */
1029 static void
1030 drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1031 {
1032 kref_get(&mstb->malloc_kref);
1033 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
1034 }
1035
1036 /**
1037 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1038 * device
1039 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1040 *
1041 * Decrements &drm_dp_mst_branch.malloc_kref. When
1042 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1043 * will be released and @mstb may no longer be used.
1044 *
1045 * See also: drm_dp_mst_get_mstb_malloc()
1046 */
1047 static void
1048 drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1049 {
1050 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
1051 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
1052 }
1053
1054 static void drm_dp_free_mst_port(struct kref *kref)
1055 {
1056 struct drm_dp_mst_port *port =
1057 container_of(kref, struct drm_dp_mst_port, malloc_kref);
1058
1059 drm_dp_mst_put_mstb_malloc(port->parent);
1060 kfree(port);
1061 }
1062
1063 /**
1064 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1065 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1066 *
1067 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1068 * reaches 0, the memory allocation for @port will be released and @port may
1069 * no longer be used.
1070 *
1071 * Because @port could potentially be freed at any time by the DP MST helpers
1072 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1073 * function, drivers that which to make use of &struct drm_dp_mst_port should
1074 * ensure that they grab at least one main malloc reference to their MST ports
1075 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1076 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1077 *
1078 * See also: drm_dp_mst_put_port_malloc()
1079 */
1080 void
1081 drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1082 {
1083 kref_get(&port->malloc_kref);
1084 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1085 }
1086 EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1087
1088 /**
1089 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1090 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1091 *
1092 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1093 * reaches 0, the memory allocation for @port will be released and @port may
1094 * no longer be used.
1095 *
1096 * See also: drm_dp_mst_get_port_malloc()
1097 */
1098 void
1099 drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1100 {
1101 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1102 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1103 }
1104 EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1105
1106 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1107 {
1108 struct drm_dp_mst_branch *mstb =
1109 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1110 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1111 struct drm_dp_mst_port *port, *tmp;
1112 bool wake_tx = false;
1113
1114 mutex_lock(&mgr->lock);
1115 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
1116 list_del(&port->next);
1117 drm_dp_mst_topology_put_port(port);
1118 }
1119 mutex_unlock(&mgr->lock);
1120
1121 /* drop any tx slots msg */
1122 mutex_lock(&mstb->mgr->qlock);
1123 if (mstb->tx_slots[0]) {
1124 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1125 mstb->tx_slots[0] = NULL;
1126 wake_tx = true;
1127 }
1128 if (mstb->tx_slots[1]) {
1129 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1130 mstb->tx_slots[1] = NULL;
1131 wake_tx = true;
1132 }
1133 mutex_unlock(&mstb->mgr->qlock);
1134
1135 if (wake_tx)
1136 wake_up_all(&mstb->mgr->tx_waitq);
1137
1138 drm_dp_mst_put_mstb_malloc(mstb);
1139 }
1140
1141 /**
1142 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1143 * branch device unless it's zero
1144 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1145 *
1146 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1147 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1148 * reached 0). Holding a topology reference implies that a malloc reference
1149 * will be held to @mstb as long as the user holds the topology reference.
1150 *
1151 * Care should be taken to ensure that the user has at least one malloc
1152 * reference to @mstb. If you already have a topology reference to @mstb, you
1153 * should use drm_dp_mst_topology_get_mstb() instead.
1154 *
1155 * See also:
1156 * drm_dp_mst_topology_get_mstb()
1157 * drm_dp_mst_topology_put_mstb()
1158 *
1159 * Returns:
1160 * * 1: A topology reference was grabbed successfully
1161 * * 0: @port is no longer in the topology, no reference was grabbed
1162 */
1163 static int __must_check
1164 drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1165 {
1166 int ret = kref_get_unless_zero(&mstb->topology_kref);
1167
1168 if (ret)
1169 DRM_DEBUG("mstb %p (%d)\n", mstb,
1170 kref_read(&mstb->topology_kref));
1171
1172 return ret;
1173 }
1174
1175 /**
1176 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1177 * branch device
1178 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1179 *
1180 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1181 * not it's already reached 0. This is only valid to use in scenarios where
1182 * you are already guaranteed to have at least one active topology reference
1183 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1184 *
1185 * See also:
1186 * drm_dp_mst_topology_try_get_mstb()
1187 * drm_dp_mst_topology_put_mstb()
1188 */
1189 static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1190 {
1191 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1192 kref_get(&mstb->topology_kref);
1193 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1194 }
1195
1196 /**
1197 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1198 * device
1199 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1200 *
1201 * Releases a topology reference from @mstb by decrementing
1202 * &drm_dp_mst_branch.topology_kref.
1203 *
1204 * See also:
1205 * drm_dp_mst_topology_try_get_mstb()
1206 * drm_dp_mst_topology_get_mstb()
1207 */
1208 static void
1209 drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1210 {
1211 DRM_DEBUG("mstb %p (%d)\n",
1212 mstb, kref_read(&mstb->topology_kref) - 1);
1213 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1214 }
1215
1216 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
1217 {
1218 struct drm_dp_mst_branch *mstb;
1219
1220 switch (old_pdt) {
1221 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1222 case DP_PEER_DEVICE_SST_SINK:
1223 /* remove i2c over sideband */
1224 drm_dp_mst_unregister_i2c_bus(&port->aux);
1225 break;
1226 case DP_PEER_DEVICE_MST_BRANCHING:
1227 mstb = port->mstb;
1228 port->mstb = NULL;
1229 drm_dp_mst_topology_put_mstb(mstb);
1230 break;
1231 }
1232 }
1233
1234 static void drm_dp_destroy_port(struct kref *kref)
1235 {
1236 struct drm_dp_mst_port *port =
1237 container_of(kref, struct drm_dp_mst_port, topology_kref);
1238 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1239
1240 if (!port->input) {
1241 kfree(port->cached_edid);
1242
1243 /*
1244 * The only time we don't have a connector
1245 * on an output port is if the connector init
1246 * fails.
1247 */
1248 if (port->connector) {
1249 /* we can't destroy the connector here, as
1250 * we might be holding the mode_config.mutex
1251 * from an EDID retrieval */
1252
1253 mutex_lock(&mgr->destroy_connector_lock);
1254 list_add(&port->next, &mgr->destroy_connector_list);
1255 mutex_unlock(&mgr->destroy_connector_lock);
1256 schedule_work(&mgr->destroy_connector_work);
1257 return;
1258 }
1259 /* no need to clean up vcpi
1260 * as if we have no connector we never setup a vcpi */
1261 drm_dp_port_teardown_pdt(port, port->pdt);
1262 port->pdt = DP_PEER_DEVICE_NONE;
1263 }
1264 drm_dp_mst_put_port_malloc(port);
1265 }
1266
1267 /**
1268 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1269 * port unless it's zero
1270 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1271 *
1272 * Attempts to grab a topology reference to @port, if it hasn't yet been
1273 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1274 * 0). Holding a topology reference implies that a malloc reference will be
1275 * held to @port as long as the user holds the topology reference.
1276 *
1277 * Care should be taken to ensure that the user has at least one malloc
1278 * reference to @port. If you already have a topology reference to @port, you
1279 * should use drm_dp_mst_topology_get_port() instead.
1280 *
1281 * See also:
1282 * drm_dp_mst_topology_get_port()
1283 * drm_dp_mst_topology_put_port()
1284 *
1285 * Returns:
1286 * * 1: A topology reference was grabbed successfully
1287 * * 0: @port is no longer in the topology, no reference was grabbed
1288 */
1289 static int __must_check
1290 drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1291 {
1292 int ret = kref_get_unless_zero(&port->topology_kref);
1293
1294 if (ret)
1295 DRM_DEBUG("port %p (%d)\n", port,
1296 kref_read(&port->topology_kref));
1297
1298 return ret;
1299 }
1300
1301 /**
1302 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1303 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1304 *
1305 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1306 * not it's already reached 0. This is only valid to use in scenarios where
1307 * you are already guaranteed to have at least one active topology reference
1308 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1309 *
1310 * See also:
1311 * drm_dp_mst_topology_try_get_port()
1312 * drm_dp_mst_topology_put_port()
1313 */
1314 static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1315 {
1316 WARN_ON(kref_read(&port->topology_kref) == 0);
1317 kref_get(&port->topology_kref);
1318 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1319 }
1320
1321 /**
1322 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1323 * @port: The &struct drm_dp_mst_port to release the topology reference from
1324 *
1325 * Releases a topology reference from @port by decrementing
1326 * &drm_dp_mst_port.topology_kref.
1327 *
1328 * See also:
1329 * drm_dp_mst_topology_try_get_port()
1330 * drm_dp_mst_topology_get_port()
1331 */
1332 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1333 {
1334 DRM_DEBUG("port %p (%d)\n",
1335 port, kref_read(&port->topology_kref) - 1);
1336 kref_put(&port->topology_kref, drm_dp_destroy_port);
1337 }
1338
1339 static struct drm_dp_mst_branch *
1340 drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1341 struct drm_dp_mst_branch *to_find)
1342 {
1343 struct drm_dp_mst_port *port;
1344 struct drm_dp_mst_branch *rmstb;
1345
1346 if (to_find == mstb)
1347 return mstb;
1348
1349 list_for_each_entry(port, &mstb->ports, next) {
1350 if (port->mstb) {
1351 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1352 port->mstb, to_find);
1353 if (rmstb)
1354 return rmstb;
1355 }
1356 }
1357 return NULL;
1358 }
1359
1360 static struct drm_dp_mst_branch *
1361 drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1362 struct drm_dp_mst_branch *mstb)
1363 {
1364 struct drm_dp_mst_branch *rmstb = NULL;
1365
1366 mutex_lock(&mgr->lock);
1367 if (mgr->mst_primary) {
1368 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1369 mgr->mst_primary, mstb);
1370
1371 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1372 rmstb = NULL;
1373 }
1374 mutex_unlock(&mgr->lock);
1375 return rmstb;
1376 }
1377
1378 static struct drm_dp_mst_port *
1379 drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1380 struct drm_dp_mst_port *to_find)
1381 {
1382 struct drm_dp_mst_port *port, *mport;
1383
1384 list_for_each_entry(port, &mstb->ports, next) {
1385 if (port == to_find)
1386 return port;
1387
1388 if (port->mstb) {
1389 mport = drm_dp_mst_topology_get_port_validated_locked(
1390 port->mstb, to_find);
1391 if (mport)
1392 return mport;
1393 }
1394 }
1395 return NULL;
1396 }
1397
1398 static struct drm_dp_mst_port *
1399 drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1400 struct drm_dp_mst_port *port)
1401 {
1402 struct drm_dp_mst_port *rport = NULL;
1403
1404 mutex_lock(&mgr->lock);
1405 if (mgr->mst_primary) {
1406 rport = drm_dp_mst_topology_get_port_validated_locked(
1407 mgr->mst_primary, port);
1408
1409 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1410 rport = NULL;
1411 }
1412 mutex_unlock(&mgr->lock);
1413 return rport;
1414 }
1415
1416 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1417 {
1418 struct drm_dp_mst_port *port;
1419 int ret;
1420
1421 list_for_each_entry(port, &mstb->ports, next) {
1422 if (port->port_num == port_num) {
1423 ret = drm_dp_mst_topology_try_get_port(port);
1424 return ret ? port : NULL;
1425 }
1426 }
1427
1428 return NULL;
1429 }
1430
1431 /*
1432 * calculate a new RAD for this MST branch device
1433 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1434 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1435 */
1436 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1437 u8 *rad)
1438 {
1439 int parent_lct = port->parent->lct;
1440 int shift = 4;
1441 int idx = (parent_lct - 1) / 2;
1442 if (parent_lct > 1) {
1443 memcpy(rad, port->parent->rad, idx + 1);
1444 shift = (parent_lct % 2) ? 4 : 0;
1445 } else
1446 rad[0] = 0;
1447
1448 rad[idx] |= port->port_num << shift;
1449 return parent_lct + 1;
1450 }
1451
1452 /*
1453 * return sends link address for new mstb
1454 */
1455 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1456 {
1457 int ret;
1458 u8 rad[6], lct;
1459 bool send_link = false;
1460 switch (port->pdt) {
1461 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1462 case DP_PEER_DEVICE_SST_SINK:
1463 /* add i2c over sideband */
1464 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1465 break;
1466 case DP_PEER_DEVICE_MST_BRANCHING:
1467 lct = drm_dp_calculate_rad(port, rad);
1468
1469 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1470 if (port->mstb) {
1471 port->mstb->mgr = port->mgr;
1472 port->mstb->port_parent = port;
1473 /*
1474 * Make sure this port's memory allocation stays
1475 * around until its child MSTB releases it
1476 */
1477 drm_dp_mst_get_port_malloc(port);
1478
1479 send_link = true;
1480 }
1481 break;
1482 }
1483 return send_link;
1484 }
1485
1486 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1487 {
1488 int ret;
1489
1490 memcpy(mstb->guid, guid, 16);
1491
1492 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1493 if (mstb->port_parent) {
1494 ret = drm_dp_send_dpcd_write(
1495 mstb->mgr,
1496 mstb->port_parent,
1497 DP_GUID,
1498 16,
1499 mstb->guid);
1500 } else {
1501
1502 ret = drm_dp_dpcd_write(
1503 mstb->mgr->aux,
1504 DP_GUID,
1505 mstb->guid,
1506 16);
1507 }
1508 }
1509 }
1510
1511 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1512 int pnum,
1513 char *proppath,
1514 size_t proppath_size)
1515 {
1516 int i;
1517 char temp[8];
1518 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1519 for (i = 0; i < (mstb->lct - 1); i++) {
1520 int shift = (i % 2) ? 0 : 4;
1521 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1522 snprintf(temp, sizeof(temp), "-%d", port_num);
1523 strlcat(proppath, temp, proppath_size);
1524 }
1525 snprintf(temp, sizeof(temp), "-%d", pnum);
1526 strlcat(proppath, temp, proppath_size);
1527 }
1528
1529 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1530 struct drm_device *dev,
1531 struct drm_dp_link_addr_reply_port *port_msg)
1532 {
1533 struct drm_dp_mst_port *port;
1534 bool ret;
1535 bool created = false;
1536 int old_pdt = 0;
1537 int old_ddps = 0;
1538
1539 port = drm_dp_get_port(mstb, port_msg->port_number);
1540 if (!port) {
1541 port = kzalloc(sizeof(*port), GFP_KERNEL);
1542 if (!port)
1543 return;
1544 kref_init(&port->topology_kref);
1545 kref_init(&port->malloc_kref);
1546 port->parent = mstb;
1547 port->port_num = port_msg->port_number;
1548 port->mgr = mstb->mgr;
1549 port->aux.name = "DPMST";
1550 port->aux.dev = dev->dev;
1551
1552 /*
1553 * Make sure the memory allocation for our parent branch stays
1554 * around until our own memory allocation is released
1555 */
1556 drm_dp_mst_get_mstb_malloc(mstb);
1557
1558 created = true;
1559 } else {
1560 old_pdt = port->pdt;
1561 old_ddps = port->ddps;
1562 }
1563
1564 port->pdt = port_msg->peer_device_type;
1565 port->input = port_msg->input_port;
1566 port->mcs = port_msg->mcs;
1567 port->ddps = port_msg->ddps;
1568 port->ldps = port_msg->legacy_device_plug_status;
1569 port->dpcd_rev = port_msg->dpcd_revision;
1570 port->num_sdp_streams = port_msg->num_sdp_streams;
1571 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1572
1573 /* manage mstb port lists with mgr lock - take a reference
1574 for this list */
1575 if (created) {
1576 mutex_lock(&mstb->mgr->lock);
1577 drm_dp_mst_topology_get_port(port);
1578 list_add(&port->next, &mstb->ports);
1579 mutex_unlock(&mstb->mgr->lock);
1580 }
1581
1582 if (old_ddps != port->ddps) {
1583 if (port->ddps) {
1584 if (!port->input) {
1585 drm_dp_send_enum_path_resources(mstb->mgr,
1586 mstb, port);
1587 }
1588 } else {
1589 port->available_pbn = 0;
1590 }
1591 }
1592
1593 if (old_pdt != port->pdt && !port->input) {
1594 drm_dp_port_teardown_pdt(port, old_pdt);
1595
1596 ret = drm_dp_port_setup_pdt(port);
1597 if (ret == true)
1598 drm_dp_send_link_address(mstb->mgr, port->mstb);
1599 }
1600
1601 if (created && !port->input) {
1602 char proppath[255];
1603
1604 build_mst_prop_path(mstb, port->port_num, proppath,
1605 sizeof(proppath));
1606 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
1607 port,
1608 proppath);
1609 if (!port->connector) {
1610 /* remove it from the port list */
1611 mutex_lock(&mstb->mgr->lock);
1612 list_del(&port->next);
1613 mutex_unlock(&mstb->mgr->lock);
1614 /* drop port list reference */
1615 drm_dp_mst_topology_put_port(port);
1616 goto out;
1617 }
1618 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1619 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1620 port->port_num >= DP_MST_LOGICAL_PORT_0) {
1621 port->cached_edid = drm_get_edid(port->connector,
1622 &port->aux.ddc);
1623 drm_connector_set_tile_property(port->connector);
1624 }
1625 (*mstb->mgr->cbs->register_connector)(port->connector);
1626 }
1627
1628 out:
1629 /* put reference to this port */
1630 drm_dp_mst_topology_put_port(port);
1631 }
1632
1633 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1634 struct drm_dp_connection_status_notify *conn_stat)
1635 {
1636 struct drm_dp_mst_port *port;
1637 int old_pdt;
1638 int old_ddps;
1639 bool dowork = false;
1640 port = drm_dp_get_port(mstb, conn_stat->port_number);
1641 if (!port)
1642 return;
1643
1644 old_ddps = port->ddps;
1645 old_pdt = port->pdt;
1646 port->pdt = conn_stat->peer_device_type;
1647 port->mcs = conn_stat->message_capability_status;
1648 port->ldps = conn_stat->legacy_device_plug_status;
1649 port->ddps = conn_stat->displayport_device_plug_status;
1650
1651 if (old_ddps != port->ddps) {
1652 if (port->ddps) {
1653 dowork = true;
1654 } else {
1655 port->available_pbn = 0;
1656 }
1657 }
1658 if (old_pdt != port->pdt && !port->input) {
1659 drm_dp_port_teardown_pdt(port, old_pdt);
1660
1661 if (drm_dp_port_setup_pdt(port))
1662 dowork = true;
1663 }
1664
1665 drm_dp_mst_topology_put_port(port);
1666 if (dowork)
1667 queue_work(system_long_wq, &mstb->mgr->work);
1668
1669 }
1670
1671 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1672 u8 lct, u8 *rad)
1673 {
1674 struct drm_dp_mst_branch *mstb;
1675 struct drm_dp_mst_port *port;
1676 int i, ret;
1677 /* find the port by iterating down */
1678
1679 mutex_lock(&mgr->lock);
1680 mstb = mgr->mst_primary;
1681
1682 if (!mstb)
1683 goto out;
1684
1685 for (i = 0; i < lct - 1; i++) {
1686 int shift = (i % 2) ? 0 : 4;
1687 int port_num = (rad[i / 2] >> shift) & 0xf;
1688
1689 list_for_each_entry(port, &mstb->ports, next) {
1690 if (port->port_num == port_num) {
1691 mstb = port->mstb;
1692 if (!mstb) {
1693 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1694 goto out;
1695 }
1696
1697 break;
1698 }
1699 }
1700 }
1701 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1702 if (!ret)
1703 mstb = NULL;
1704 out:
1705 mutex_unlock(&mgr->lock);
1706 return mstb;
1707 }
1708
1709 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1710 struct drm_dp_mst_branch *mstb,
1711 uint8_t *guid)
1712 {
1713 struct drm_dp_mst_branch *found_mstb;
1714 struct drm_dp_mst_port *port;
1715
1716 if (memcmp(mstb->guid, guid, 16) == 0)
1717 return mstb;
1718
1719
1720 list_for_each_entry(port, &mstb->ports, next) {
1721 if (!port->mstb)
1722 continue;
1723
1724 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1725
1726 if (found_mstb)
1727 return found_mstb;
1728 }
1729
1730 return NULL;
1731 }
1732
1733 static struct drm_dp_mst_branch *
1734 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
1735 uint8_t *guid)
1736 {
1737 struct drm_dp_mst_branch *mstb;
1738 int ret;
1739
1740 /* find the port by iterating down */
1741 mutex_lock(&mgr->lock);
1742
1743 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1744 if (mstb) {
1745 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1746 if (!ret)
1747 mstb = NULL;
1748 }
1749
1750 mutex_unlock(&mgr->lock);
1751 return mstb;
1752 }
1753
1754 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1755 struct drm_dp_mst_branch *mstb)
1756 {
1757 struct drm_dp_mst_port *port;
1758 struct drm_dp_mst_branch *mstb_child;
1759 if (!mstb->link_address_sent)
1760 drm_dp_send_link_address(mgr, mstb);
1761
1762 list_for_each_entry(port, &mstb->ports, next) {
1763 if (port->input)
1764 continue;
1765
1766 if (!port->ddps)
1767 continue;
1768
1769 if (!port->available_pbn)
1770 drm_dp_send_enum_path_resources(mgr, mstb, port);
1771
1772 if (port->mstb) {
1773 mstb_child = drm_dp_mst_topology_get_mstb_validated(
1774 mgr, port->mstb);
1775 if (mstb_child) {
1776 drm_dp_check_and_send_link_address(mgr, mstb_child);
1777 drm_dp_mst_topology_put_mstb(mstb_child);
1778 }
1779 }
1780 }
1781 }
1782
1783 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1784 {
1785 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1786 struct drm_dp_mst_branch *mstb;
1787 int ret;
1788
1789 mutex_lock(&mgr->lock);
1790 mstb = mgr->mst_primary;
1791 if (mstb) {
1792 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1793 if (!ret)
1794 mstb = NULL;
1795 }
1796 mutex_unlock(&mgr->lock);
1797 if (mstb) {
1798 drm_dp_check_and_send_link_address(mgr, mstb);
1799 drm_dp_mst_topology_put_mstb(mstb);
1800 }
1801 }
1802
1803 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1804 u8 *guid)
1805 {
1806 u64 salt;
1807
1808 if (memchr_inv(guid, 0, 16))
1809 return true;
1810
1811 salt = get_jiffies_64();
1812
1813 memcpy(&guid[0], &salt, sizeof(u64));
1814 memcpy(&guid[8], &salt, sizeof(u64));
1815
1816 return false;
1817 }
1818
1819 #if 0
1820 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1821 {
1822 struct drm_dp_sideband_msg_req_body req;
1823
1824 req.req_type = DP_REMOTE_DPCD_READ;
1825 req.u.dpcd_read.port_number = port_num;
1826 req.u.dpcd_read.dpcd_address = offset;
1827 req.u.dpcd_read.num_bytes = num_bytes;
1828 drm_dp_encode_sideband_req(&req, msg);
1829
1830 return 0;
1831 }
1832 #endif
1833
1834 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1835 bool up, u8 *msg, int len)
1836 {
1837 int ret;
1838 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1839 int tosend, total, offset;
1840 int retries = 0;
1841
1842 retry:
1843 total = len;
1844 offset = 0;
1845 do {
1846 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1847
1848 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1849 &msg[offset],
1850 tosend);
1851 if (ret != tosend) {
1852 if (ret == -EIO && retries < 5) {
1853 retries++;
1854 goto retry;
1855 }
1856 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1857
1858 return -EIO;
1859 }
1860 offset += tosend;
1861 total -= tosend;
1862 } while (total > 0);
1863 return 0;
1864 }
1865
1866 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1867 struct drm_dp_sideband_msg_tx *txmsg)
1868 {
1869 struct drm_dp_mst_branch *mstb = txmsg->dst;
1870 u8 req_type;
1871
1872 /* both msg slots are full */
1873 if (txmsg->seqno == -1) {
1874 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1875 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1876 return -EAGAIN;
1877 }
1878 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1879 txmsg->seqno = mstb->last_seqno;
1880 mstb->last_seqno ^= 1;
1881 } else if (mstb->tx_slots[0] == NULL)
1882 txmsg->seqno = 0;
1883 else
1884 txmsg->seqno = 1;
1885 mstb->tx_slots[txmsg->seqno] = txmsg;
1886 }
1887
1888 req_type = txmsg->msg[0] & 0x7f;
1889 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1890 req_type == DP_RESOURCE_STATUS_NOTIFY)
1891 hdr->broadcast = 1;
1892 else
1893 hdr->broadcast = 0;
1894 hdr->path_msg = txmsg->path_msg;
1895 hdr->lct = mstb->lct;
1896 hdr->lcr = mstb->lct - 1;
1897 if (mstb->lct > 1)
1898 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1899 hdr->seqno = txmsg->seqno;
1900 return 0;
1901 }
1902 /*
1903 * process a single block of the next message in the sideband queue
1904 */
1905 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1906 struct drm_dp_sideband_msg_tx *txmsg,
1907 bool up)
1908 {
1909 u8 chunk[48];
1910 struct drm_dp_sideband_msg_hdr hdr;
1911 int len, space, idx, tosend;
1912 int ret;
1913
1914 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1915
1916 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1917 txmsg->seqno = -1;
1918 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1919 }
1920
1921 /* make hdr from dst mst - for replies use seqno
1922 otherwise assign one */
1923 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1924 if (ret < 0)
1925 return ret;
1926
1927 /* amount left to send in this message */
1928 len = txmsg->cur_len - txmsg->cur_offset;
1929
1930 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1931 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1932
1933 tosend = min(len, space);
1934 if (len == txmsg->cur_len)
1935 hdr.somt = 1;
1936 if (space >= len)
1937 hdr.eomt = 1;
1938
1939
1940 hdr.msg_len = tosend + 1;
1941 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1942 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1943 /* add crc at end */
1944 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1945 idx += tosend + 1;
1946
1947 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1948 if (ret) {
1949 DRM_DEBUG_KMS("sideband msg failed to send\n");
1950 return ret;
1951 }
1952
1953 txmsg->cur_offset += tosend;
1954 if (txmsg->cur_offset == txmsg->cur_len) {
1955 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1956 return 1;
1957 }
1958 return 0;
1959 }
1960
1961 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1962 {
1963 struct drm_dp_sideband_msg_tx *txmsg;
1964 int ret;
1965
1966 WARN_ON(!mutex_is_locked(&mgr->qlock));
1967
1968 /* construct a chunk from the first msg in the tx_msg queue */
1969 if (list_empty(&mgr->tx_msg_downq))
1970 return;
1971
1972 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1973 ret = process_single_tx_qlock(mgr, txmsg, false);
1974 if (ret == 1) {
1975 /* txmsg is sent it should be in the slots now */
1976 list_del(&txmsg->next);
1977 } else if (ret) {
1978 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1979 list_del(&txmsg->next);
1980 if (txmsg->seqno != -1)
1981 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1982 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1983 wake_up_all(&mgr->tx_waitq);
1984 }
1985 }
1986
1987 /* called holding qlock */
1988 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1989 struct drm_dp_sideband_msg_tx *txmsg)
1990 {
1991 int ret;
1992
1993 /* construct a chunk from the first msg in the tx_msg queue */
1994 ret = process_single_tx_qlock(mgr, txmsg, true);
1995
1996 if (ret != 1)
1997 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1998
1999 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
2000 }
2001
2002 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2003 struct drm_dp_sideband_msg_tx *txmsg)
2004 {
2005 mutex_lock(&mgr->qlock);
2006 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
2007 if (list_is_singular(&mgr->tx_msg_downq))
2008 process_single_down_tx_qlock(mgr);
2009 mutex_unlock(&mgr->qlock);
2010 }
2011
2012 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2013 struct drm_dp_mst_branch *mstb)
2014 {
2015 int len;
2016 struct drm_dp_sideband_msg_tx *txmsg;
2017 int ret;
2018
2019 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2020 if (!txmsg)
2021 return;
2022
2023 txmsg->dst = mstb;
2024 len = build_link_address(txmsg);
2025
2026 mstb->link_address_sent = true;
2027 drm_dp_queue_down_tx(mgr, txmsg);
2028
2029 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2030 if (ret > 0) {
2031 int i;
2032
2033 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2034 DRM_DEBUG_KMS("link address nak received\n");
2035 } else {
2036 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
2037 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
2038 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,
2039 txmsg->reply.u.link_addr.ports[i].input_port,
2040 txmsg->reply.u.link_addr.ports[i].peer_device_type,
2041 txmsg->reply.u.link_addr.ports[i].port_number,
2042 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
2043 txmsg->reply.u.link_addr.ports[i].mcs,
2044 txmsg->reply.u.link_addr.ports[i].ddps,
2045 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
2046 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
2047 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
2048 }
2049
2050 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
2051
2052 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
2053 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
2054 }
2055 drm_kms_helper_hotplug_event(mgr->dev);
2056 }
2057 } else {
2058 mstb->link_address_sent = false;
2059 DRM_DEBUG_KMS("link address failed %d\n", ret);
2060 }
2061
2062 kfree(txmsg);
2063 }
2064
2065 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2066 struct drm_dp_mst_branch *mstb,
2067 struct drm_dp_mst_port *port)
2068 {
2069 int len;
2070 struct drm_dp_sideband_msg_tx *txmsg;
2071 int ret;
2072
2073 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2074 if (!txmsg)
2075 return -ENOMEM;
2076
2077 txmsg->dst = mstb;
2078 len = build_enum_path_resources(txmsg, port->port_num);
2079
2080 drm_dp_queue_down_tx(mgr, txmsg);
2081
2082 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2083 if (ret > 0) {
2084 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2085 DRM_DEBUG_KMS("enum path resources nak received\n");
2086 } else {
2087 if (port->port_num != txmsg->reply.u.path_resources.port_number)
2088 DRM_ERROR("got incorrect port in response\n");
2089 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,
2090 txmsg->reply.u.path_resources.avail_payload_bw_number);
2091 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
2092 }
2093 }
2094
2095 kfree(txmsg);
2096 return 0;
2097 }
2098
2099 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2100 {
2101 if (!mstb->port_parent)
2102 return NULL;
2103
2104 if (mstb->port_parent->mstb != mstb)
2105 return mstb->port_parent;
2106
2107 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2108 }
2109
2110 /*
2111 * Searches upwards in the topology starting from mstb to try to find the
2112 * closest available parent of mstb that's still connected to the rest of the
2113 * topology. This can be used in order to perform operations like releasing
2114 * payloads, where the branch device which owned the payload may no longer be
2115 * around and thus would require that the payload on the last living relative
2116 * be freed instead.
2117 */
2118 static struct drm_dp_mst_branch *
2119 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2120 struct drm_dp_mst_branch *mstb,
2121 int *port_num)
2122 {
2123 struct drm_dp_mst_branch *rmstb = NULL;
2124 struct drm_dp_mst_port *found_port;
2125
2126 mutex_lock(&mgr->lock);
2127 if (!mgr->mst_primary)
2128 goto out;
2129
2130 do {
2131 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
2132 if (!found_port)
2133 break;
2134
2135 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
2136 rmstb = found_port->parent;
2137 *port_num = found_port->port_num;
2138 } else {
2139 /* Search again, starting from this parent */
2140 mstb = found_port->parent;
2141 }
2142 } while (!rmstb);
2143 out:
2144 mutex_unlock(&mgr->lock);
2145 return rmstb;
2146 }
2147
2148 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
2149 struct drm_dp_mst_port *port,
2150 int id,
2151 int pbn)
2152 {
2153 struct drm_dp_sideband_msg_tx *txmsg;
2154 struct drm_dp_mst_branch *mstb;
2155 int len, ret, port_num;
2156 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
2157 int i;
2158
2159 port_num = port->port_num;
2160 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2161 if (!mstb) {
2162 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
2163 port->parent,
2164 &port_num);
2165
2166 if (!mstb)
2167 return -EINVAL;
2168 }
2169
2170 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2171 if (!txmsg) {
2172 ret = -ENOMEM;
2173 goto fail_put;
2174 }
2175
2176 for (i = 0; i < port->num_sdp_streams; i++)
2177 sinks[i] = i;
2178
2179 txmsg->dst = mstb;
2180 len = build_allocate_payload(txmsg, port_num,
2181 id,
2182 pbn, port->num_sdp_streams, sinks);
2183
2184 drm_dp_queue_down_tx(mgr, txmsg);
2185
2186 /*
2187 * FIXME: there is a small chance that between getting the last
2188 * connected mstb and sending the payload message, the last connected
2189 * mstb could also be removed from the topology. In the future, this
2190 * needs to be fixed by restarting the
2191 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
2192 * timeout if the topology is still connected to the system.
2193 */
2194 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2195 if (ret > 0) {
2196 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2197 ret = -EINVAL;
2198 else
2199 ret = 0;
2200 }
2201 kfree(txmsg);
2202 fail_put:
2203 drm_dp_mst_topology_put_mstb(mstb);
2204 return ret;
2205 }
2206
2207 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
2208 struct drm_dp_mst_port *port, bool power_up)
2209 {
2210 struct drm_dp_sideband_msg_tx *txmsg;
2211 int len, ret;
2212
2213 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2214 if (!port)
2215 return -EINVAL;
2216
2217 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2218 if (!txmsg) {
2219 drm_dp_mst_topology_put_port(port);
2220 return -ENOMEM;
2221 }
2222
2223 txmsg->dst = port->parent;
2224 len = build_power_updown_phy(txmsg, port->port_num, power_up);
2225 drm_dp_queue_down_tx(mgr, txmsg);
2226
2227 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
2228 if (ret > 0) {
2229 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2230 ret = -EINVAL;
2231 else
2232 ret = 0;
2233 }
2234 kfree(txmsg);
2235 drm_dp_mst_topology_put_port(port);
2236
2237 return ret;
2238 }
2239 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
2240
2241 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2242 int id,
2243 struct drm_dp_payload *payload)
2244 {
2245 int ret;
2246
2247 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
2248 if (ret < 0) {
2249 payload->payload_state = 0;
2250 return ret;
2251 }
2252 payload->payload_state = DP_PAYLOAD_LOCAL;
2253 return 0;
2254 }
2255
2256 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2257 struct drm_dp_mst_port *port,
2258 int id,
2259 struct drm_dp_payload *payload)
2260 {
2261 int ret;
2262 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
2263 if (ret < 0)
2264 return ret;
2265 payload->payload_state = DP_PAYLOAD_REMOTE;
2266 return ret;
2267 }
2268
2269 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2270 struct drm_dp_mst_port *port,
2271 int id,
2272 struct drm_dp_payload *payload)
2273 {
2274 DRM_DEBUG_KMS("\n");
2275 /* it's okay for these to fail */
2276 if (port) {
2277 drm_dp_payload_send_msg(mgr, port, id, 0);
2278 }
2279
2280 drm_dp_dpcd_write_payload(mgr, id, payload);
2281 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
2282 return 0;
2283 }
2284
2285 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2286 int id,
2287 struct drm_dp_payload *payload)
2288 {
2289 payload->payload_state = 0;
2290 return 0;
2291 }
2292
2293 /**
2294 * drm_dp_update_payload_part1() - Execute payload update part 1
2295 * @mgr: manager to use.
2296 *
2297 * This iterates over all proposed virtual channels, and tries to
2298 * allocate space in the link for them. For 0->slots transitions,
2299 * this step just writes the VCPI to the MST device. For slots->0
2300 * transitions, this writes the updated VCPIs and removes the
2301 * remote VC payloads.
2302 *
2303 * after calling this the driver should generate ACT and payload
2304 * packets.
2305 */
2306 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
2307 {
2308 struct drm_dp_payload req_payload;
2309 struct drm_dp_mst_port *port;
2310 int i, j;
2311 int cur_slots = 1;
2312
2313 mutex_lock(&mgr->payload_lock);
2314 for (i = 0; i < mgr->max_payloads; i++) {
2315 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2316 struct drm_dp_payload *payload = &mgr->payloads[i];
2317 bool put_port = false;
2318
2319 /* solve the current payloads - compare to the hw ones
2320 - update the hw view */
2321 req_payload.start_slot = cur_slots;
2322 if (vcpi) {
2323 port = container_of(vcpi, struct drm_dp_mst_port,
2324 vcpi);
2325
2326 /* Validated ports don't matter if we're releasing
2327 * VCPI
2328 */
2329 if (vcpi->num_slots) {
2330 port = drm_dp_mst_topology_get_port_validated(
2331 mgr, port);
2332 if (!port) {
2333 mutex_unlock(&mgr->payload_lock);
2334 return -EINVAL;
2335 }
2336 put_port = true;
2337 }
2338
2339 req_payload.num_slots = vcpi->num_slots;
2340 req_payload.vcpi = vcpi->vcpi;
2341 } else {
2342 port = NULL;
2343 req_payload.num_slots = 0;
2344 }
2345
2346 payload->start_slot = req_payload.start_slot;
2347 /* work out what is required to happen with this payload */
2348 if (payload->num_slots != req_payload.num_slots) {
2349
2350 /* need to push an update for this payload */
2351 if (req_payload.num_slots) {
2352 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
2353 &req_payload);
2354 payload->num_slots = req_payload.num_slots;
2355 payload->vcpi = req_payload.vcpi;
2356
2357 } else if (payload->num_slots) {
2358 payload->num_slots = 0;
2359 drm_dp_destroy_payload_step1(mgr, port,
2360 payload->vcpi,
2361 payload);
2362 req_payload.payload_state =
2363 payload->payload_state;
2364 payload->start_slot = 0;
2365 }
2366 payload->payload_state = req_payload.payload_state;
2367 }
2368 cur_slots += req_payload.num_slots;
2369
2370 if (put_port)
2371 drm_dp_mst_topology_put_port(port);
2372 }
2373
2374 for (i = 0; i < mgr->max_payloads; i++) {
2375 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL)
2376 continue;
2377
2378 DRM_DEBUG_KMS("removing payload %d\n", i);
2379 for (j = i; j < mgr->max_payloads - 1; j++) {
2380 mgr->payloads[j] = mgr->payloads[j + 1];
2381 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
2382
2383 if (mgr->proposed_vcpis[j] &&
2384 mgr->proposed_vcpis[j]->num_slots) {
2385 set_bit(j + 1, &mgr->payload_mask);
2386 } else {
2387 clear_bit(j + 1, &mgr->payload_mask);
2388 }
2389 }
2390
2391 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
2392 sizeof(struct drm_dp_payload));
2393 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
2394 clear_bit(mgr->max_payloads, &mgr->payload_mask);
2395 }
2396 mutex_unlock(&mgr->payload_lock);
2397
2398 return 0;
2399 }
2400 EXPORT_SYMBOL(drm_dp_update_payload_part1);
2401
2402 /**
2403 * drm_dp_update_payload_part2() - Execute payload update part 2
2404 * @mgr: manager to use.
2405 *
2406 * This iterates over all proposed virtual channels, and tries to
2407 * allocate space in the link for them. For 0->slots transitions,
2408 * this step writes the remote VC payload commands. For slots->0
2409 * this just resets some internal state.
2410 */
2411 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
2412 {
2413 struct drm_dp_mst_port *port;
2414 int i;
2415 int ret = 0;
2416 mutex_lock(&mgr->payload_lock);
2417 for (i = 0; i < mgr->max_payloads; i++) {
2418
2419 if (!mgr->proposed_vcpis[i])
2420 continue;
2421
2422 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2423
2424 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2425 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
2426 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2427 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
2428 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2429 }
2430 if (ret) {
2431 mutex_unlock(&mgr->payload_lock);
2432 return ret;
2433 }
2434 }
2435 mutex_unlock(&mgr->payload_lock);
2436 return 0;
2437 }
2438 EXPORT_SYMBOL(drm_dp_update_payload_part2);
2439
2440 #if 0 /* unused as of yet */
2441 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2442 struct drm_dp_mst_port *port,
2443 int offset, int size)
2444 {
2445 int len;
2446 struct drm_dp_sideband_msg_tx *txmsg;
2447
2448 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2449 if (!txmsg)
2450 return -ENOMEM;
2451
2452 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
2453 txmsg->dst = port->parent;
2454
2455 drm_dp_queue_down_tx(mgr, txmsg);
2456
2457 return 0;
2458 }
2459 #endif
2460
2461 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2462 struct drm_dp_mst_port *port,
2463 int offset, int size, u8 *bytes)
2464 {
2465 int len;
2466 int ret;
2467 struct drm_dp_sideband_msg_tx *txmsg;
2468 struct drm_dp_mst_branch *mstb;
2469
2470 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2471 if (!mstb)
2472 return -EINVAL;
2473
2474 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2475 if (!txmsg) {
2476 ret = -ENOMEM;
2477 goto fail_put;
2478 }
2479
2480 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2481 txmsg->dst = mstb;
2482
2483 drm_dp_queue_down_tx(mgr, txmsg);
2484
2485 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2486 if (ret > 0) {
2487 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2488 ret = -EINVAL;
2489 else
2490 ret = 0;
2491 }
2492 kfree(txmsg);
2493 fail_put:
2494 drm_dp_mst_topology_put_mstb(mstb);
2495 return ret;
2496 }
2497
2498 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2499 {
2500 struct drm_dp_sideband_msg_reply_body reply;
2501
2502 reply.reply_type = DP_SIDEBAND_REPLY_ACK;
2503 reply.req_type = req_type;
2504 drm_dp_encode_sideband_reply(&reply, msg);
2505 return 0;
2506 }
2507
2508 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2509 struct drm_dp_mst_branch *mstb,
2510 int req_type, int seqno, bool broadcast)
2511 {
2512 struct drm_dp_sideband_msg_tx *txmsg;
2513
2514 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2515 if (!txmsg)
2516 return -ENOMEM;
2517
2518 txmsg->dst = mstb;
2519 txmsg->seqno = seqno;
2520 drm_dp_encode_up_ack_reply(txmsg, req_type);
2521
2522 mutex_lock(&mgr->qlock);
2523
2524 process_single_up_tx_qlock(mgr, txmsg);
2525
2526 mutex_unlock(&mgr->qlock);
2527
2528 kfree(txmsg);
2529 return 0;
2530 }
2531
2532 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2533 int dp_link_count,
2534 int *out)
2535 {
2536 switch (dp_link_bw) {
2537 default:
2538 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2539 dp_link_bw, dp_link_count);
2540 return false;
2541
2542 case DP_LINK_BW_1_62:
2543 *out = 3 * dp_link_count;
2544 break;
2545 case DP_LINK_BW_2_7:
2546 *out = 5 * dp_link_count;
2547 break;
2548 case DP_LINK_BW_5_4:
2549 *out = 10 * dp_link_count;
2550 break;
2551 case DP_LINK_BW_8_1:
2552 *out = 15 * dp_link_count;
2553 break;
2554 }
2555 return true;
2556 }
2557
2558 /**
2559 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2560 * @mgr: manager to set state for
2561 * @mst_state: true to enable MST on this connector - false to disable.
2562 *
2563 * This is called by the driver when it detects an MST capable device plugged
2564 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2565 */
2566 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2567 {
2568 int ret = 0;
2569 struct drm_dp_mst_branch *mstb = NULL;
2570
2571 mutex_lock(&mgr->lock);
2572 if (mst_state == mgr->mst_state)
2573 goto out_unlock;
2574
2575 mgr->mst_state = mst_state;
2576 /* set the device into MST mode */
2577 if (mst_state) {
2578 WARN_ON(mgr->mst_primary);
2579
2580 /* get dpcd info */
2581 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2582 if (ret != DP_RECEIVER_CAP_SIZE) {
2583 DRM_DEBUG_KMS("failed to read DPCD\n");
2584 goto out_unlock;
2585 }
2586
2587 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2588 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2589 &mgr->pbn_div)) {
2590 ret = -EINVAL;
2591 goto out_unlock;
2592 }
2593
2594 /* add initial branch device at LCT 1 */
2595 mstb = drm_dp_add_mst_branch_device(1, NULL);
2596 if (mstb == NULL) {
2597 ret = -ENOMEM;
2598 goto out_unlock;
2599 }
2600 mstb->mgr = mgr;
2601
2602 /* give this the main reference */
2603 mgr->mst_primary = mstb;
2604 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
2605
2606 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2607 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2608 if (ret < 0) {
2609 goto out_unlock;
2610 }
2611
2612 {
2613 struct drm_dp_payload reset_pay;
2614 reset_pay.start_slot = 0;
2615 reset_pay.num_slots = 0x3f;
2616 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2617 }
2618
2619 queue_work(system_long_wq, &mgr->work);
2620
2621 ret = 0;
2622 } else {
2623 /* disable MST on the device */
2624 mstb = mgr->mst_primary;
2625 mgr->mst_primary = NULL;
2626 /* this can fail if the device is gone */
2627 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2628 ret = 0;
2629 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2630 mgr->payload_mask = 0;
2631 set_bit(0, &mgr->payload_mask);
2632 mgr->vcpi_mask = 0;
2633 }
2634
2635 out_unlock:
2636 mutex_unlock(&mgr->lock);
2637 if (mstb)
2638 drm_dp_mst_topology_put_mstb(mstb);
2639 return ret;
2640
2641 }
2642 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2643
2644 /**
2645 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2646 * @mgr: manager to suspend
2647 *
2648 * This function tells the MST device that we can't handle UP messages
2649 * anymore. This should stop it from sending any since we are suspended.
2650 */
2651 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2652 {
2653 mutex_lock(&mgr->lock);
2654 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2655 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2656 mutex_unlock(&mgr->lock);
2657 flush_work(&mgr->work);
2658 flush_work(&mgr->destroy_connector_work);
2659 }
2660 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2661
2662 /**
2663 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2664 * @mgr: manager to resume
2665 *
2666 * This will fetch DPCD and see if the device is still there,
2667 * if it is, it will rewrite the MSTM control bits, and return.
2668 *
2669 * if the device fails this returns -1, and the driver should do
2670 * a full MST reprobe, in case we were undocked.
2671 */
2672 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2673 {
2674 int ret = 0;
2675
2676 mutex_lock(&mgr->lock);
2677
2678 if (mgr->mst_primary) {
2679 int sret;
2680 u8 guid[16];
2681
2682 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2683 if (sret != DP_RECEIVER_CAP_SIZE) {
2684 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2685 ret = -1;
2686 goto out_unlock;
2687 }
2688
2689 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2690 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2691 if (ret < 0) {
2692 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2693 ret = -1;
2694 goto out_unlock;
2695 }
2696
2697 /* Some hubs forget their guids after they resume */
2698 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2699 if (sret != 16) {
2700 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2701 ret = -1;
2702 goto out_unlock;
2703 }
2704 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2705
2706 ret = 0;
2707 } else
2708 ret = -1;
2709
2710 out_unlock:
2711 mutex_unlock(&mgr->lock);
2712 return ret;
2713 }
2714 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2715
2716 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2717 {
2718 int len;
2719 u8 replyblock[32];
2720 int replylen, origlen, curreply;
2721 int ret;
2722 struct drm_dp_sideband_msg_rx *msg;
2723 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2724 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2725
2726 len = min(mgr->max_dpcd_transaction_bytes, 16);
2727 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2728 replyblock, len);
2729 if (ret != len) {
2730 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2731 return false;
2732 }
2733 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2734 if (!ret) {
2735 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2736 return false;
2737 }
2738 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2739
2740 origlen = replylen;
2741 replylen -= len;
2742 curreply = len;
2743 while (replylen > 0) {
2744 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2745 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2746 replyblock, len);
2747 if (ret != len) {
2748 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2749 len, ret);
2750 return false;
2751 }
2752
2753 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2754 if (!ret) {
2755 DRM_DEBUG_KMS("failed to build sideband msg\n");
2756 return false;
2757 }
2758
2759 curreply += len;
2760 replylen -= len;
2761 }
2762 return true;
2763 }
2764
2765 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2766 {
2767 int ret = 0;
2768
2769 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2770 memset(&mgr->down_rep_recv, 0,
2771 sizeof(struct drm_dp_sideband_msg_rx));
2772 return 0;
2773 }
2774
2775 if (mgr->down_rep_recv.have_eomt) {
2776 struct drm_dp_sideband_msg_tx *txmsg;
2777 struct drm_dp_mst_branch *mstb;
2778 int slot = -1;
2779 mstb = drm_dp_get_mst_branch_device(mgr,
2780 mgr->down_rep_recv.initial_hdr.lct,
2781 mgr->down_rep_recv.initial_hdr.rad);
2782
2783 if (!mstb) {
2784 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2785 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2786 return 0;
2787 }
2788
2789 /* find the message */
2790 slot = mgr->down_rep_recv.initial_hdr.seqno;
2791 mutex_lock(&mgr->qlock);
2792 txmsg = mstb->tx_slots[slot];
2793 /* remove from slots */
2794 mutex_unlock(&mgr->qlock);
2795
2796 if (!txmsg) {
2797 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2798 mstb,
2799 mgr->down_rep_recv.initial_hdr.seqno,
2800 mgr->down_rep_recv.initial_hdr.lct,
2801 mgr->down_rep_recv.initial_hdr.rad[0],
2802 mgr->down_rep_recv.msg[0]);
2803 drm_dp_mst_topology_put_mstb(mstb);
2804 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2805 return 0;
2806 }
2807
2808 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2809
2810 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2811 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
2812 txmsg->reply.req_type,
2813 drm_dp_mst_req_type_str(txmsg->reply.req_type),
2814 txmsg->reply.u.nak.reason,
2815 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
2816 txmsg->reply.u.nak.nak_data);
2817
2818 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2819 drm_dp_mst_topology_put_mstb(mstb);
2820
2821 mutex_lock(&mgr->qlock);
2822 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2823 mstb->tx_slots[slot] = NULL;
2824 mutex_unlock(&mgr->qlock);
2825
2826 wake_up_all(&mgr->tx_waitq);
2827 }
2828 return ret;
2829 }
2830
2831 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2832 {
2833 int ret = 0;
2834
2835 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2836 memset(&mgr->up_req_recv, 0,
2837 sizeof(struct drm_dp_sideband_msg_rx));
2838 return 0;
2839 }
2840
2841 if (mgr->up_req_recv.have_eomt) {
2842 struct drm_dp_sideband_msg_req_body msg;
2843 struct drm_dp_mst_branch *mstb = NULL;
2844 bool seqno;
2845
2846 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2847 mstb = drm_dp_get_mst_branch_device(mgr,
2848 mgr->up_req_recv.initial_hdr.lct,
2849 mgr->up_req_recv.initial_hdr.rad);
2850 if (!mstb) {
2851 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2852 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2853 return 0;
2854 }
2855 }
2856
2857 seqno = mgr->up_req_recv.initial_hdr.seqno;
2858 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2859
2860 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2861 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2862
2863 if (!mstb)
2864 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2865
2866 if (!mstb) {
2867 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2868 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2869 return 0;
2870 }
2871
2872 drm_dp_update_port(mstb, &msg.u.conn_stat);
2873
2874 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);
2875 drm_kms_helper_hotplug_event(mgr->dev);
2876
2877 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2878 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2879 if (!mstb)
2880 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2881
2882 if (!mstb) {
2883 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2884 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2885 return 0;
2886 }
2887
2888 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2889 }
2890
2891 if (mstb)
2892 drm_dp_mst_topology_put_mstb(mstb);
2893
2894 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2895 }
2896 return ret;
2897 }
2898
2899 /**
2900 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2901 * @mgr: manager to notify irq for.
2902 * @esi: 4 bytes from SINK_COUNT_ESI
2903 * @handled: whether the hpd interrupt was consumed or not
2904 *
2905 * This should be called from the driver when it detects a short IRQ,
2906 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2907 * topology manager will process the sideband messages received as a result
2908 * of this.
2909 */
2910 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2911 {
2912 int ret = 0;
2913 int sc;
2914 *handled = false;
2915 sc = esi[0] & 0x3f;
2916
2917 if (sc != mgr->sink_count) {
2918 mgr->sink_count = sc;
2919 *handled = true;
2920 }
2921
2922 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2923 ret = drm_dp_mst_handle_down_rep(mgr);
2924 *handled = true;
2925 }
2926
2927 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2928 ret |= drm_dp_mst_handle_up_req(mgr);
2929 *handled = true;
2930 }
2931
2932 drm_dp_mst_kick_tx(mgr);
2933 return ret;
2934 }
2935 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2936
2937 /**
2938 * drm_dp_mst_detect_port() - get connection status for an MST port
2939 * @connector: DRM connector for this port
2940 * @mgr: manager for this port
2941 * @port: unverified pointer to a port
2942 *
2943 * This returns the current connection state for a port. It validates the
2944 * port pointer still exists so the caller doesn't require a reference
2945 */
2946 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2947 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2948 {
2949 enum drm_connector_status status = connector_status_disconnected;
2950
2951 /* we need to search for the port in the mgr in case it's gone */
2952 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2953 if (!port)
2954 return connector_status_disconnected;
2955
2956 if (!port->ddps)
2957 goto out;
2958
2959 switch (port->pdt) {
2960 case DP_PEER_DEVICE_NONE:
2961 case DP_PEER_DEVICE_MST_BRANCHING:
2962 break;
2963
2964 case DP_PEER_DEVICE_SST_SINK:
2965 status = connector_status_connected;
2966 /* for logical ports - cache the EDID */
2967 if (port->port_num >= 8 && !port->cached_edid) {
2968 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2969 }
2970 break;
2971 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2972 if (port->ldps)
2973 status = connector_status_connected;
2974 break;
2975 }
2976 out:
2977 drm_dp_mst_topology_put_port(port);
2978 return status;
2979 }
2980 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2981
2982 /**
2983 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2984 * @mgr: manager for this port
2985 * @port: unverified pointer to a port.
2986 *
2987 * This returns whether the port supports audio or not.
2988 */
2989 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2990 struct drm_dp_mst_port *port)
2991 {
2992 bool ret = false;
2993
2994 port = drm_dp_mst_topology_get_port_validated(mgr, port);
2995 if (!port)
2996 return ret;
2997 ret = port->has_audio;
2998 drm_dp_mst_topology_put_port(port);
2999 return ret;
3000 }
3001 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
3002
3003 /**
3004 * drm_dp_mst_get_edid() - get EDID for an MST port
3005 * @connector: toplevel connector to get EDID for
3006 * @mgr: manager for this port
3007 * @port: unverified pointer to a port.
3008 *
3009 * This returns an EDID for the port connected to a connector,
3010 * It validates the pointer still exists so the caller doesn't require a
3011 * reference.
3012 */
3013 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3014 {
3015 struct edid *edid = NULL;
3016
3017 /* we need to search for the port in the mgr in case it's gone */
3018 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3019 if (!port)
3020 return NULL;
3021
3022 if (port->cached_edid)
3023 edid = drm_edid_duplicate(port->cached_edid);
3024 else {
3025 edid = drm_get_edid(connector, &port->aux.ddc);
3026 }
3027 port->has_audio = drm_detect_monitor_audio(edid);
3028 drm_dp_mst_topology_put_port(port);
3029 return edid;
3030 }
3031 EXPORT_SYMBOL(drm_dp_mst_get_edid);
3032
3033 /**
3034 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
3035 * @mgr: manager to use
3036 * @pbn: payload bandwidth to convert into slots.
3037 *
3038 * Calculate the number of VCPI slots that will be required for the given PBN
3039 * value. This function is deprecated, and should not be used in atomic
3040 * drivers.
3041 *
3042 * RETURNS:
3043 * The total slots required for this port, or error.
3044 */
3045 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
3046 int pbn)
3047 {
3048 int num_slots;
3049
3050 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3051
3052 /* max. time slots - one slot for MTP header */
3053 if (num_slots > 63)
3054 return -ENOSPC;
3055 return num_slots;
3056 }
3057 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
3058
3059 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3060 struct drm_dp_vcpi *vcpi, int pbn, int slots)
3061 {
3062 int ret;
3063
3064 /* max. time slots - one slot for MTP header */
3065 if (slots > 63)
3066 return -ENOSPC;
3067
3068 vcpi->pbn = pbn;
3069 vcpi->aligned_pbn = slots * mgr->pbn_div;
3070 vcpi->num_slots = slots;
3071
3072 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
3073 if (ret < 0)
3074 return ret;
3075 return 0;
3076 }
3077
3078 /**
3079 * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
3080 * @state: global atomic state
3081 * @mgr: MST topology manager for the port
3082 * @port: port to find vcpi slots for
3083 * @pbn: bandwidth required for the mode in PBN
3084 *
3085 * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
3086 * may have had. Any atomic drivers which support MST must call this function
3087 * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
3088 * current VCPI allocation for the new state, but only when
3089 * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
3090 * to ensure compatibility with userspace applications that still use the
3091 * legacy modesetting UAPI.
3092 *
3093 * Allocations set by this function are not checked against the bandwidth
3094 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
3095 *
3096 * Additionally, it is OK to call this function multiple times on the same
3097 * @port as needed. It is not OK however, to call this function and
3098 * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
3099 *
3100 * See also:
3101 * drm_dp_atomic_release_vcpi_slots()
3102 * drm_dp_mst_atomic_check()
3103 *
3104 * Returns:
3105 * Total slots in the atomic state assigned for this port, or a negative error
3106 * code if the port no longer exists
3107 */
3108 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
3109 struct drm_dp_mst_topology_mgr *mgr,
3110 struct drm_dp_mst_port *port, int pbn)
3111 {
3112 struct drm_dp_mst_topology_state *topology_state;
3113 struct drm_dp_vcpi_allocation *pos, *vcpi = NULL;
3114 int prev_slots, req_slots, ret;
3115
3116 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3117 if (IS_ERR(topology_state))
3118 return PTR_ERR(topology_state);
3119
3120 /* Find the current allocation for this port, if any */
3121 list_for_each_entry(pos, &topology_state->vcpis, next) {
3122 if (pos->port == port) {
3123 vcpi = pos;
3124 prev_slots = vcpi->vcpi;
3125
3126 /*
3127 * This should never happen, unless the driver tries
3128 * releasing and allocating the same VCPI allocation,
3129 * which is an error
3130 */
3131 if (WARN_ON(!prev_slots)) {
3132 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
3133 port);
3134 return -EINVAL;
3135 }
3136
3137 break;
3138 }
3139 }
3140 if (!vcpi)
3141 prev_slots = 0;
3142
3143 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3144
3145 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
3146 port->connector->base.id, port->connector->name,
3147 port, prev_slots, req_slots);
3148
3149 /* Add the new allocation to the state */
3150 if (!vcpi) {
3151 vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL);
3152 if (!vcpi)
3153 return -ENOMEM;
3154
3155 drm_dp_mst_get_port_malloc(port);
3156 vcpi->port = port;
3157 list_add(&vcpi->next, &topology_state->vcpis);
3158 }
3159 vcpi->vcpi = req_slots;
3160
3161 ret = req_slots;
3162 return ret;
3163 }
3164 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
3165
3166 /**
3167 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
3168 * @state: global atomic state
3169 * @mgr: MST topology manager for the port
3170 * @port: The port to release the VCPI slots from
3171 *
3172 * Releases any VCPI slots that have been allocated to a port in the atomic
3173 * state. Any atomic drivers which support MST must call this function in
3174 * their &drm_connector_helper_funcs.atomic_check() callback when the
3175 * connector will no longer have VCPI allocated (e.g. because its CRTC was
3176 * removed) when it had VCPI allocated in the previous atomic state.
3177 *
3178 * It is OK to call this even if @port has been removed from the system.
3179 * Additionally, it is OK to call this function multiple times on the same
3180 * @port as needed. It is not OK however, to call this function and
3181 * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
3182 * phase.
3183 *
3184 * See also:
3185 * drm_dp_atomic_find_vcpi_slots()
3186 * drm_dp_mst_atomic_check()
3187 *
3188 * Returns:
3189 * 0 if all slots for this port were added back to
3190 * &drm_dp_mst_topology_state.avail_slots or negative error code
3191 */
3192 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
3193 struct drm_dp_mst_topology_mgr *mgr,
3194 struct drm_dp_mst_port *port)
3195 {
3196 struct drm_dp_mst_topology_state *topology_state;
3197 struct drm_dp_vcpi_allocation *pos;
3198 bool found = false;
3199
3200 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3201 if (IS_ERR(topology_state))
3202 return PTR_ERR(topology_state);
3203
3204 list_for_each_entry(pos, &topology_state->vcpis, next) {
3205 if (pos->port == port) {
3206 found = true;
3207 break;
3208 }
3209 }
3210 if (WARN_ON(!found)) {
3211 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
3212 port, &topology_state->base);
3213 return -EINVAL;
3214 }
3215
3216 DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi);
3217 if (pos->vcpi) {
3218 drm_dp_mst_put_port_malloc(port);
3219 pos->vcpi = 0;
3220 }
3221
3222 return 0;
3223 }
3224 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
3225
3226 /**
3227 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
3228 * @mgr: manager for this port
3229 * @port: port to allocate a virtual channel for.
3230 * @pbn: payload bandwidth number to request
3231 * @slots: returned number of slots for this PBN.
3232 */
3233 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3234 struct drm_dp_mst_port *port, int pbn, int slots)
3235 {
3236 int ret;
3237
3238 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3239 if (!port)
3240 return false;
3241
3242 if (slots < 0)
3243 return false;
3244
3245 if (port->vcpi.vcpi > 0) {
3246 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
3247 port->vcpi.vcpi, port->vcpi.pbn, pbn);
3248 if (pbn == port->vcpi.pbn) {
3249 drm_dp_mst_topology_put_port(port);
3250 return true;
3251 }
3252 }
3253
3254 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
3255 if (ret) {
3256 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
3257 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
3258 goto out;
3259 }
3260 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
3261 pbn, port->vcpi.num_slots);
3262
3263 /* Keep port allocated until its payload has been removed */
3264 drm_dp_mst_get_port_malloc(port);
3265 drm_dp_mst_topology_put_port(port);
3266 return true;
3267 out:
3268 return false;
3269 }
3270 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
3271
3272 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3273 {
3274 int slots = 0;
3275 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3276 if (!port)
3277 return slots;
3278
3279 slots = port->vcpi.num_slots;
3280 drm_dp_mst_topology_put_port(port);
3281 return slots;
3282 }
3283 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
3284
3285 /**
3286 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
3287 * @mgr: manager for this port
3288 * @port: unverified pointer to a port.
3289 *
3290 * This just resets the number of slots for the ports VCPI for later programming.
3291 */
3292 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3293 {
3294 /*
3295 * A port with VCPI will remain allocated until its VCPI is
3296 * released, no verified ref needed
3297 */
3298
3299 port->vcpi.num_slots = 0;
3300 }
3301 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
3302
3303 /**
3304 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
3305 * @mgr: manager for this port
3306 * @port: port to deallocate vcpi for
3307 *
3308 * This can be called unconditionally, regardless of whether
3309 * drm_dp_mst_allocate_vcpi() succeeded or not.
3310 */
3311 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3312 struct drm_dp_mst_port *port)
3313 {
3314 if (!port->vcpi.vcpi)
3315 return;
3316
3317 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3318 port->vcpi.num_slots = 0;
3319 port->vcpi.pbn = 0;
3320 port->vcpi.aligned_pbn = 0;
3321 port->vcpi.vcpi = 0;
3322 drm_dp_mst_put_port_malloc(port);
3323 }
3324 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
3325
3326 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
3327 int id, struct drm_dp_payload *payload)
3328 {
3329 u8 payload_alloc[3], status;
3330 int ret;
3331 int retries = 0;
3332
3333 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
3334 DP_PAYLOAD_TABLE_UPDATED);
3335
3336 payload_alloc[0] = id;
3337 payload_alloc[1] = payload->start_slot;
3338 payload_alloc[2] = payload->num_slots;
3339
3340 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
3341 if (ret != 3) {
3342 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
3343 goto fail;
3344 }
3345
3346 retry:
3347 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3348 if (ret < 0) {
3349 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3350 goto fail;
3351 }
3352
3353 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
3354 retries++;
3355 if (retries < 20) {
3356 usleep_range(10000, 20000);
3357 goto retry;
3358 }
3359 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
3360 ret = -EINVAL;
3361 goto fail;
3362 }
3363 ret = 0;
3364 fail:
3365 return ret;
3366 }
3367
3368
3369 /**
3370 * drm_dp_check_act_status() - Check ACT handled status.
3371 * @mgr: manager to use
3372 *
3373 * Check the payload status bits in the DPCD for ACT handled completion.
3374 */
3375 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
3376 {
3377 u8 status;
3378 int ret;
3379 int count = 0;
3380
3381 do {
3382 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3383
3384 if (ret < 0) {
3385 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3386 goto fail;
3387 }
3388
3389 if (status & DP_PAYLOAD_ACT_HANDLED)
3390 break;
3391 count++;
3392 udelay(100);
3393
3394 } while (count < 30);
3395
3396 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
3397 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
3398 ret = -EINVAL;
3399 goto fail;
3400 }
3401 return 0;
3402 fail:
3403 return ret;
3404 }
3405 EXPORT_SYMBOL(drm_dp_check_act_status);
3406
3407 /**
3408 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
3409 * @clock: dot clock for the mode
3410 * @bpp: bpp for the mode.
3411 *
3412 * This uses the formula in the spec to calculate the PBN value for a mode.
3413 */
3414 int drm_dp_calc_pbn_mode(int clock, int bpp)
3415 {
3416 u64 kbps;
3417 s64 peak_kbps;
3418 u32 numerator;
3419 u32 denominator;
3420
3421 kbps = clock * bpp;
3422
3423 /*
3424 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
3425 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
3426 * common multiplier to render an integer PBN for all link rate/lane
3427 * counts combinations
3428 * calculate
3429 * peak_kbps *= (1006/1000)
3430 * peak_kbps *= (64/54)
3431 * peak_kbps *= 8 convert to bytes
3432 */
3433
3434 numerator = 64 * 1006;
3435 denominator = 54 * 8 * 1000 * 1000;
3436
3437 kbps *= numerator;
3438 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
3439
3440 return drm_fixp2int_ceil(peak_kbps);
3441 }
3442 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
3443
3444 static int test_calc_pbn_mode(void)
3445 {
3446 int ret;
3447 ret = drm_dp_calc_pbn_mode(154000, 30);
3448 if (ret != 689) {
3449 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3450 154000, 30, 689, ret);
3451 return -EINVAL;
3452 }
3453 ret = drm_dp_calc_pbn_mode(234000, 30);
3454 if (ret != 1047) {
3455 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3456 234000, 30, 1047, ret);
3457 return -EINVAL;
3458 }
3459 ret = drm_dp_calc_pbn_mode(297000, 24);
3460 if (ret != 1063) {
3461 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3462 297000, 24, 1063, ret);
3463 return -EINVAL;
3464 }
3465 return 0;
3466 }
3467
3468 /* we want to kick the TX after we've ack the up/down IRQs. */
3469 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
3470 {
3471 queue_work(system_long_wq, &mgr->tx_work);
3472 }
3473
3474 static void drm_dp_mst_dump_mstb(struct seq_file *m,
3475 struct drm_dp_mst_branch *mstb)
3476 {
3477 struct drm_dp_mst_port *port;
3478 int tabs = mstb->lct;
3479 char prefix[10];
3480 int i;
3481
3482 for (i = 0; i < tabs; i++)
3483 prefix[i] = '\t';
3484 prefix[i] = '\0';
3485
3486 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
3487 list_for_each_entry(port, &mstb->ports, next) {
3488 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);
3489 if (port->mstb)
3490 drm_dp_mst_dump_mstb(m, port->mstb);
3491 }
3492 }
3493
3494 #define DP_PAYLOAD_TABLE_SIZE 64
3495
3496 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
3497 char *buf)
3498 {
3499 int i;
3500
3501 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
3502 if (drm_dp_dpcd_read(mgr->aux,
3503 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
3504 &buf[i], 16) != 16)
3505 return false;
3506 }
3507 return true;
3508 }
3509
3510 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3511 struct drm_dp_mst_port *port, char *name,
3512 int namelen)
3513 {
3514 struct edid *mst_edid;
3515
3516 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3517 drm_edid_get_monitor_name(mst_edid, name, namelen);
3518 }
3519
3520 /**
3521 * drm_dp_mst_dump_topology(): dump topology to seq file.
3522 * @m: seq_file to dump output to
3523 * @mgr: manager to dump current topology for.
3524 *
3525 * helper to dump MST topology to a seq file for debugfs.
3526 */
3527 void drm_dp_mst_dump_topology(struct seq_file *m,
3528 struct drm_dp_mst_topology_mgr *mgr)
3529 {
3530 int i;
3531 struct drm_dp_mst_port *port;
3532
3533 mutex_lock(&mgr->lock);
3534 if (mgr->mst_primary)
3535 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
3536
3537 /* dump VCPIs */
3538 mutex_unlock(&mgr->lock);
3539
3540 mutex_lock(&mgr->payload_lock);
3541 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
3542 mgr->max_payloads);
3543
3544 for (i = 0; i < mgr->max_payloads; i++) {
3545 if (mgr->proposed_vcpis[i]) {
3546 char name[14];
3547
3548 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
3549 fetch_monitor_name(mgr, port, name, sizeof(name));
3550 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3551 port->port_num, port->vcpi.vcpi,
3552 port->vcpi.num_slots,
3553 (*name != 0) ? name : "Unknown");
3554 } else
3555 seq_printf(m, "vcpi %d:unused\n", i);
3556 }
3557 for (i = 0; i < mgr->max_payloads; i++) {
3558 seq_printf(m, "payload %d: %d, %d, %d\n",
3559 i,
3560 mgr->payloads[i].payload_state,
3561 mgr->payloads[i].start_slot,
3562 mgr->payloads[i].num_slots);
3563
3564
3565 }
3566 mutex_unlock(&mgr->payload_lock);
3567
3568 mutex_lock(&mgr->lock);
3569 if (mgr->mst_primary) {
3570 u8 buf[DP_PAYLOAD_TABLE_SIZE];
3571 int ret;
3572
3573 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
3574 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
3575 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
3576 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
3577 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
3578 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
3579
3580 /* dump the standard OUI branch header */
3581 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
3582 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
3583 for (i = 0x3; i < 0x8 && buf[i]; i++)
3584 seq_printf(m, "%c", buf[i]);
3585 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
3586 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
3587 if (dump_dp_payload_table(mgr, buf))
3588 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
3589 }
3590
3591 mutex_unlock(&mgr->lock);
3592
3593 }
3594 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
3595
3596 static void drm_dp_tx_work(struct work_struct *work)
3597 {
3598 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
3599
3600 mutex_lock(&mgr->qlock);
3601 if (!list_empty(&mgr->tx_msg_downq))
3602 process_single_down_tx_qlock(mgr);
3603 mutex_unlock(&mgr->qlock);
3604 }
3605
3606 static void drm_dp_destroy_connector_work(struct work_struct *work)
3607 {
3608 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
3609 struct drm_dp_mst_port *port;
3610 bool send_hotplug = false;
3611 /*
3612 * Not a regular list traverse as we have to drop the destroy
3613 * connector lock before destroying the connector, to avoid AB->BA
3614 * ordering between this lock and the config mutex.
3615 */
3616 for (;;) {
3617 mutex_lock(&mgr->destroy_connector_lock);
3618 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
3619 if (!port) {
3620 mutex_unlock(&mgr->destroy_connector_lock);
3621 break;
3622 }
3623 list_del(&port->next);
3624 mutex_unlock(&mgr->destroy_connector_lock);
3625
3626 INIT_LIST_HEAD(&port->next);
3627
3628 mgr->cbs->destroy_connector(mgr, port->connector);
3629
3630 drm_dp_port_teardown_pdt(port, port->pdt);
3631 port->pdt = DP_PEER_DEVICE_NONE;
3632
3633 drm_dp_mst_put_port_malloc(port);
3634 send_hotplug = true;
3635 }
3636 if (send_hotplug)
3637 drm_kms_helper_hotplug_event(mgr->dev);
3638 }
3639
3640 static struct drm_private_state *
3641 drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
3642 {
3643 struct drm_dp_mst_topology_state *state, *old_state =
3644 to_dp_mst_topology_state(obj->state);
3645 struct drm_dp_vcpi_allocation *pos, *vcpi;
3646
3647 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
3648 if (!state)
3649 return NULL;
3650
3651 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
3652
3653 INIT_LIST_HEAD(&state->vcpis);
3654
3655 list_for_each_entry(pos, &old_state->vcpis, next) {
3656 /* Prune leftover freed VCPI allocations */
3657 if (!pos->vcpi)
3658 continue;
3659
3660 vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL);
3661 if (!vcpi)
3662 goto fail;
3663
3664 drm_dp_mst_get_port_malloc(vcpi->port);
3665 list_add(&vcpi->next, &state->vcpis);
3666 }
3667
3668 return &state->base;
3669
3670 fail:
3671 list_for_each_entry_safe(pos, vcpi, &state->vcpis, next) {
3672 drm_dp_mst_put_port_malloc(pos->port);
3673 kfree(pos);
3674 }
3675 kfree(state);
3676
3677 return NULL;
3678 }
3679
3680 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
3681 struct drm_private_state *state)
3682 {
3683 struct drm_dp_mst_topology_state *mst_state =
3684 to_dp_mst_topology_state(state);
3685 struct drm_dp_vcpi_allocation *pos, *tmp;
3686
3687 list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next) {
3688 /* We only keep references to ports with non-zero VCPIs */
3689 if (pos->vcpi)
3690 drm_dp_mst_put_port_malloc(pos->port);
3691 kfree(pos);
3692 }
3693
3694 kfree(mst_state);
3695 }
3696
3697 static inline int
3698 drm_dp_mst_atomic_check_topology_state(struct drm_dp_mst_topology_mgr *mgr,
3699 struct drm_dp_mst_topology_state *mst_state)
3700 {
3701 struct drm_dp_vcpi_allocation *vcpi;
3702 int avail_slots = 63, payload_count = 0;
3703
3704 list_for_each_entry(vcpi, &mst_state->vcpis, next) {
3705 /* Releasing VCPI is always OK-even if the port is gone */
3706 if (!vcpi->vcpi) {
3707 DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
3708 vcpi->port);
3709 continue;
3710 }
3711
3712 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
3713 vcpi->port, vcpi->vcpi);
3714
3715 avail_slots -= vcpi->vcpi;
3716 if (avail_slots < 0) {
3717 DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
3718 vcpi->port, mst_state,
3719 avail_slots + vcpi->vcpi);
3720 return -ENOSPC;
3721 }
3722
3723 if (++payload_count > mgr->max_payloads) {
3724 DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
3725 mgr, mst_state, mgr->max_payloads);
3726 return -EINVAL;
3727 }
3728 }
3729 DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
3730 mgr, mst_state, avail_slots,
3731 63 - avail_slots);
3732
3733 return 0;
3734 }
3735
3736 /**
3737 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
3738 * atomic update is valid
3739 * @state: Pointer to the new &struct drm_dp_mst_topology_state
3740 *
3741 * Checks the given topology state for an atomic update to ensure that it's
3742 * valid. This includes checking whether there's enough bandwidth to support
3743 * the new VCPI allocations in the atomic update.
3744 *
3745 * Any atomic drivers supporting DP MST must make sure to call this after
3746 * checking the rest of their state in their
3747 * &drm_mode_config_funcs.atomic_check() callback.
3748 *
3749 * See also:
3750 * drm_dp_atomic_find_vcpi_slots()
3751 * drm_dp_atomic_release_vcpi_slots()
3752 *
3753 * Returns:
3754 *
3755 * 0 if the new state is valid, negative error code otherwise.
3756 */
3757 int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
3758 {
3759 struct drm_dp_mst_topology_mgr *mgr;
3760 struct drm_dp_mst_topology_state *mst_state;
3761 int i, ret = 0;
3762
3763 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
3764 ret = drm_dp_mst_atomic_check_topology_state(mgr, mst_state);
3765 if (ret)
3766 break;
3767 }
3768
3769 return ret;
3770 }
3771 EXPORT_SYMBOL(drm_dp_mst_atomic_check);
3772
3773 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
3774 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
3775 .atomic_destroy_state = drm_dp_mst_destroy_state,
3776 };
3777 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
3778
3779 /**
3780 * drm_atomic_get_mst_topology_state: get MST topology state
3781 *
3782 * @state: global atomic state
3783 * @mgr: MST topology manager, also the private object in this case
3784 *
3785 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3786 * state vtable so that the private object state returned is that of a MST
3787 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3788 * to care of the locking, so warn if don't hold the connection_mutex.
3789 *
3790 * RETURNS:
3791 *
3792 * The MST topology state or error pointer.
3793 */
3794 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
3795 struct drm_dp_mst_topology_mgr *mgr)
3796 {
3797 struct drm_device *dev = mgr->dev;
3798
3799 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3800 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
3801 }
3802 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
3803
3804 /**
3805 * drm_dp_mst_topology_mgr_init - initialise a topology manager
3806 * @mgr: manager struct to initialise
3807 * @dev: device providing this structure - for i2c addition.
3808 * @aux: DP helper aux channel to talk to this device
3809 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3810 * @max_payloads: maximum number of payloads this GPU can source
3811 * @conn_base_id: the connector object ID the MST device is connected to.
3812 *
3813 * Return 0 for success, or negative error code on failure
3814 */
3815 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
3816 struct drm_device *dev, struct drm_dp_aux *aux,
3817 int max_dpcd_transaction_bytes,
3818 int max_payloads, int conn_base_id)
3819 {
3820 struct drm_dp_mst_topology_state *mst_state;
3821
3822 mutex_init(&mgr->lock);
3823 mutex_init(&mgr->qlock);
3824 mutex_init(&mgr->payload_lock);
3825 mutex_init(&mgr->destroy_connector_lock);
3826 INIT_LIST_HEAD(&mgr->tx_msg_downq);
3827 INIT_LIST_HEAD(&mgr->destroy_connector_list);
3828 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
3829 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
3830 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
3831 init_waitqueue_head(&mgr->tx_waitq);
3832 mgr->dev = dev;
3833 mgr->aux = aux;
3834 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
3835 mgr->max_payloads = max_payloads;
3836 mgr->conn_base_id = conn_base_id;
3837 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3838 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3839 return -EINVAL;
3840 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3841 if (!mgr->payloads)
3842 return -ENOMEM;
3843 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3844 if (!mgr->proposed_vcpis)
3845 return -ENOMEM;
3846 set_bit(0, &mgr->payload_mask);
3847 if (test_calc_pbn_mode() < 0)
3848 DRM_ERROR("MST PBN self-test failed\n");
3849
3850 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
3851 if (mst_state == NULL)
3852 return -ENOMEM;
3853
3854 mst_state->mgr = mgr;
3855 INIT_LIST_HEAD(&mst_state->vcpis);
3856
3857 drm_atomic_private_obj_init(dev, &mgr->base,
3858 &mst_state->base,
3859 &drm_dp_mst_topology_state_funcs);
3860
3861 return 0;
3862 }
3863 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3864
3865 /**
3866 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3867 * @mgr: manager to destroy
3868 */
3869 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3870 {
3871 drm_dp_mst_topology_mgr_set_mst(mgr, false);
3872 flush_work(&mgr->work);
3873 flush_work(&mgr->destroy_connector_work);
3874 mutex_lock(&mgr->payload_lock);
3875 kfree(mgr->payloads);
3876 mgr->payloads = NULL;
3877 kfree(mgr->proposed_vcpis);
3878 mgr->proposed_vcpis = NULL;
3879 mutex_unlock(&mgr->payload_lock);
3880 mgr->dev = NULL;
3881 mgr->aux = NULL;
3882 drm_atomic_private_obj_fini(&mgr->base);
3883 mgr->funcs = NULL;
3884 }
3885 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3886
3887 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
3888 {
3889 int i;
3890
3891 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
3892 return false;
3893
3894 for (i = 0; i < num - 1; i++) {
3895 if (msgs[i].flags & I2C_M_RD ||
3896 msgs[i].len > 0xff)
3897 return false;
3898 }
3899
3900 return msgs[num - 1].flags & I2C_M_RD &&
3901 msgs[num - 1].len <= 0xff;
3902 }
3903
3904 /* I2C device */
3905 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3906 int num)
3907 {
3908 struct drm_dp_aux *aux = adapter->algo_data;
3909 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3910 struct drm_dp_mst_branch *mstb;
3911 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3912 unsigned int i;
3913 struct drm_dp_sideband_msg_req_body msg;
3914 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3915 int ret;
3916
3917 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
3918 if (!mstb)
3919 return -EREMOTEIO;
3920
3921 if (!remote_i2c_read_ok(msgs, num)) {
3922 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3923 ret = -EIO;
3924 goto out;
3925 }
3926
3927 memset(&msg, 0, sizeof(msg));
3928 msg.req_type = DP_REMOTE_I2C_READ;
3929 msg.u.i2c_read.num_transactions = num - 1;
3930 msg.u.i2c_read.port_number = port->port_num;
3931 for (i = 0; i < num - 1; i++) {
3932 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3933 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3934 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3935 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
3936 }
3937 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3938 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3939
3940 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3941 if (!txmsg) {
3942 ret = -ENOMEM;
3943 goto out;
3944 }
3945
3946 txmsg->dst = mstb;
3947 drm_dp_encode_sideband_req(&msg, txmsg);
3948
3949 drm_dp_queue_down_tx(mgr, txmsg);
3950
3951 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3952 if (ret > 0) {
3953
3954 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
3955 ret = -EREMOTEIO;
3956 goto out;
3957 }
3958 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3959 ret = -EIO;
3960 goto out;
3961 }
3962 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3963 ret = num;
3964 }
3965 out:
3966 kfree(txmsg);
3967 drm_dp_mst_topology_put_mstb(mstb);
3968 return ret;
3969 }
3970
3971 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3972 {
3973 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3974 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3975 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3976 I2C_FUNC_10BIT_ADDR;
3977 }
3978
3979 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3980 .functionality = drm_dp_mst_i2c_functionality,
3981 .master_xfer = drm_dp_mst_i2c_xfer,
3982 };
3983
3984 /**
3985 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3986 * @aux: DisplayPort AUX channel
3987 *
3988 * Returns 0 on success or a negative error code on failure.
3989 */
3990 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3991 {
3992 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3993 aux->ddc.algo_data = aux;
3994 aux->ddc.retries = 3;
3995
3996 aux->ddc.class = I2C_CLASS_DDC;
3997 aux->ddc.owner = THIS_MODULE;
3998 aux->ddc.dev.parent = aux->dev;
3999 aux->ddc.dev.of_node = aux->dev->of_node;
4000
4001 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
4002 sizeof(aux->ddc.name));
4003
4004 return i2c_add_adapter(&aux->ddc);
4005 }
4006
4007 /**
4008 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
4009 * @aux: DisplayPort AUX channel
4010 */
4011 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
4012 {
4013 i2c_del_adapter(&aux->ddc);
4014 }