blob: 6d0d8e01f16efbeac0b86214f30557ddbc782442 [file] [log] [blame]
Dave Airliead7f8a12014-06-05 14:01:32 +10001/*
2 * Copyright © 2014 Red Hat
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <linux/kernel.h>
24#include <linux/delay.h>
25#include <linux/init.h>
26#include <linux/errno.h>
27#include <linux/sched.h>
Dave Airlie75bc08a2014-07-09 11:15:09 +100028#include <linux/seq_file.h>
Dave Airliead7f8a12014-06-05 14:01:32 +100029#include <linux/i2c.h>
30#include <drm/drm_dp_mst_helper.h>
31#include <drm/drmP.h>
32
33#include <drm/drm_fixed.h>
Ville Syrjäläa4370c72017-07-12 18:51:02 +030034#include <drm/drm_atomic.h>
35#include <drm/drm_atomic_helper.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010036#include <drm/drm_probe_helper.h>
Dave Airliead7f8a12014-06-05 14:01:32 +100037
38/**
39 * DOC: dp mst helper
40 *
41 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
42 * protocol. The helpers contain a topology manager and bandwidth manager.
43 * The helpers encapsulate the sending and received of sideband msgs.
44 */
45static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
46 char *buf);
47static int test_calc_pbn_mode(void);
48
Lyude Pauld0757af2019-01-10 19:53:28 -050049static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
Dave Airliead7f8a12014-06-05 14:01:32 +100050
51static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
52 int id,
53 struct drm_dp_payload *payload);
54
55static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
56 struct drm_dp_mst_port *port,
57 int offset, int size, u8 *bytes);
58
Dave Airlie68d8c9f2015-09-06 18:53:00 +100059static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
60 struct drm_dp_mst_branch *mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +100061static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
62 struct drm_dp_mst_branch *mstb,
63 struct drm_dp_mst_port *port);
64static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
65 u8 *guid);
66
67static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
68static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
69static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
Ville Syrjälä3dadbd22019-01-22 22:03:01 +020070
71#define DP_STR(x) [DP_ ## x] = #x
72
73static const char *drm_dp_mst_req_type_str(u8 req_type)
74{
75 static const char * const req_type_str[] = {
76 DP_STR(GET_MSG_TRANSACTION_VERSION),
77 DP_STR(LINK_ADDRESS),
78 DP_STR(CONNECTION_STATUS_NOTIFY),
79 DP_STR(ENUM_PATH_RESOURCES),
80 DP_STR(ALLOCATE_PAYLOAD),
81 DP_STR(QUERY_PAYLOAD),
82 DP_STR(RESOURCE_STATUS_NOTIFY),
83 DP_STR(CLEAR_PAYLOAD_ID_TABLE),
84 DP_STR(REMOTE_DPCD_READ),
85 DP_STR(REMOTE_DPCD_WRITE),
86 DP_STR(REMOTE_I2C_READ),
87 DP_STR(REMOTE_I2C_WRITE),
88 DP_STR(POWER_UP_PHY),
89 DP_STR(POWER_DOWN_PHY),
90 DP_STR(SINK_EVENT_NOTIFY),
91 DP_STR(QUERY_STREAM_ENC_STATUS),
92 };
93
94 if (req_type >= ARRAY_SIZE(req_type_str) ||
95 !req_type_str[req_type])
96 return "unknown";
97
98 return req_type_str[req_type];
99}
100
101#undef DP_STR
102#define DP_STR(x) [DP_NAK_ ## x] = #x
103
104static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
105{
106 static const char * const nak_reason_str[] = {
107 DP_STR(WRITE_FAILURE),
108 DP_STR(INVALID_READ),
109 DP_STR(CRC_FAILURE),
110 DP_STR(BAD_PARAM),
111 DP_STR(DEFER),
112 DP_STR(LINK_FAILURE),
113 DP_STR(NO_RESOURCES),
114 DP_STR(DPCD_FAIL),
115 DP_STR(I2C_NAK),
116 DP_STR(ALLOCATE_FAIL),
117 };
118
119 if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
120 !nak_reason_str[nak_reason])
121 return "unknown";
122
123 return nak_reason_str[nak_reason];
124}
125
126#undef DP_STR
127
Dave Airliead7f8a12014-06-05 14:01:32 +1000128/* sideband msg handling */
129static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
130{
131 u8 bitmask = 0x80;
132 u8 bitshift = 7;
133 u8 array_index = 0;
134 int number_of_bits = num_nibbles * 4;
135 u8 remainder = 0;
136
137 while (number_of_bits != 0) {
138 number_of_bits--;
139 remainder <<= 1;
140 remainder |= (data[array_index] & bitmask) >> bitshift;
141 bitmask >>= 1;
142 bitshift--;
143 if (bitmask == 0) {
144 bitmask = 0x80;
145 bitshift = 7;
146 array_index++;
147 }
148 if ((remainder & 0x10) == 0x10)
149 remainder ^= 0x13;
150 }
151
152 number_of_bits = 4;
153 while (number_of_bits != 0) {
154 number_of_bits--;
155 remainder <<= 1;
156 if ((remainder & 0x10) != 0)
157 remainder ^= 0x13;
158 }
159
160 return remainder;
161}
162
163static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
164{
165 u8 bitmask = 0x80;
166 u8 bitshift = 7;
167 u8 array_index = 0;
168 int number_of_bits = number_of_bytes * 8;
169 u16 remainder = 0;
170
171 while (number_of_bits != 0) {
172 number_of_bits--;
173 remainder <<= 1;
174 remainder |= (data[array_index] & bitmask) >> bitshift;
175 bitmask >>= 1;
176 bitshift--;
177 if (bitmask == 0) {
178 bitmask = 0x80;
179 bitshift = 7;
180 array_index++;
181 }
182 if ((remainder & 0x100) == 0x100)
183 remainder ^= 0xd5;
184 }
185
186 number_of_bits = 8;
187 while (number_of_bits != 0) {
188 number_of_bits--;
189 remainder <<= 1;
190 if ((remainder & 0x100) != 0)
191 remainder ^= 0xd5;
192 }
193
194 return remainder & 0xff;
195}
196static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
197{
198 u8 size = 3;
199 size += (hdr->lct / 2);
200 return size;
201}
202
203static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
204 u8 *buf, int *len)
205{
206 int idx = 0;
207 int i;
208 u8 crc4;
209 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
210 for (i = 0; i < (hdr->lct / 2); i++)
211 buf[idx++] = hdr->rad[i];
212 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
213 (hdr->msg_len & 0x3f);
214 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
215
216 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
217 buf[idx - 1] |= (crc4 & 0xf);
218
219 *len = idx;
220}
221
222static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
223 u8 *buf, int buflen, u8 *hdrlen)
224{
225 u8 crc4;
226 u8 len;
227 int i;
228 u8 idx;
229 if (buf[0] == 0)
230 return false;
231 len = 3;
232 len += ((buf[0] & 0xf0) >> 4) / 2;
233 if (len > buflen)
234 return false;
235 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
236
237 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
238 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
239 return false;
240 }
241
242 hdr->lct = (buf[0] & 0xf0) >> 4;
243 hdr->lcr = (buf[0] & 0xf);
244 idx = 1;
245 for (i = 0; i < (hdr->lct / 2); i++)
246 hdr->rad[i] = buf[idx++];
247 hdr->broadcast = (buf[idx] >> 7) & 0x1;
248 hdr->path_msg = (buf[idx] >> 6) & 0x1;
249 hdr->msg_len = buf[idx] & 0x3f;
250 idx++;
251 hdr->somt = (buf[idx] >> 7) & 0x1;
252 hdr->eomt = (buf[idx] >> 6) & 0x1;
253 hdr->seqno = (buf[idx] >> 4) & 0x1;
254 idx++;
255 *hdrlen = idx;
256 return true;
257}
258
259static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
260 struct drm_dp_sideband_msg_tx *raw)
261{
262 int idx = 0;
263 int i;
264 u8 *buf = raw->msg;
265 buf[idx++] = req->req_type & 0x7f;
266
267 switch (req->req_type) {
268 case DP_ENUM_PATH_RESOURCES:
269 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
270 idx++;
271 break;
272 case DP_ALLOCATE_PAYLOAD:
273 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
274 (req->u.allocate_payload.number_sdp_streams & 0xf);
275 idx++;
276 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
277 idx++;
278 buf[idx] = (req->u.allocate_payload.pbn >> 8);
279 idx++;
280 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
281 idx++;
282 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
283 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
284 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
285 idx++;
286 }
287 if (req->u.allocate_payload.number_sdp_streams & 1) {
288 i = req->u.allocate_payload.number_sdp_streams - 1;
289 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
290 idx++;
291 }
292 break;
293 case DP_QUERY_PAYLOAD:
294 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
295 idx++;
296 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
297 idx++;
298 break;
299 case DP_REMOTE_DPCD_READ:
300 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
301 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
302 idx++;
303 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
304 idx++;
305 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
306 idx++;
307 buf[idx] = (req->u.dpcd_read.num_bytes);
308 idx++;
309 break;
310
311 case DP_REMOTE_DPCD_WRITE:
312 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
313 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
314 idx++;
315 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
316 idx++;
317 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
318 idx++;
319 buf[idx] = (req->u.dpcd_write.num_bytes);
320 idx++;
321 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
322 idx += req->u.dpcd_write.num_bytes;
323 break;
324 case DP_REMOTE_I2C_READ:
325 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
326 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
327 idx++;
328 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
329 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
330 idx++;
331 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
332 idx++;
333 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
334 idx += req->u.i2c_read.transactions[i].num_bytes;
335
336 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
337 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
338 idx++;
339 }
340 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
341 idx++;
342 buf[idx] = (req->u.i2c_read.num_bytes_read);
343 idx++;
344 break;
345
346 case DP_REMOTE_I2C_WRITE:
347 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
348 idx++;
349 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
350 idx++;
351 buf[idx] = (req->u.i2c_write.num_bytes);
352 idx++;
353 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
354 idx += req->u.i2c_write.num_bytes;
355 break;
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700356
357 case DP_POWER_DOWN_PHY:
358 case DP_POWER_UP_PHY:
359 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
360 idx++;
361 break;
Dave Airliead7f8a12014-06-05 14:01:32 +1000362 }
363 raw->cur_len = idx;
364}
365
366static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
367{
368 u8 crc4;
369 crc4 = drm_dp_msg_data_crc4(msg, len);
370 msg[len] = crc4;
371}
372
373static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
374 struct drm_dp_sideband_msg_tx *raw)
375{
376 int idx = 0;
377 u8 *buf = raw->msg;
378
379 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
380
381 raw->cur_len = idx;
382}
383
384/* this adds a chunk of msg to the builder to get the final msg */
385static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
386 u8 *replybuf, u8 replybuflen, bool hdr)
387{
388 int ret;
389 u8 crc4;
390
391 if (hdr) {
392 u8 hdrlen;
393 struct drm_dp_sideband_msg_hdr recv_hdr;
394 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
395 if (ret == false) {
396 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
397 return false;
398 }
399
Imre Deak636c4c32017-07-19 16:46:32 +0300400 /*
401 * ignore out-of-order messages or messages that are part of a
402 * failed transaction
403 */
404 if (!recv_hdr.somt && !msg->have_somt)
405 return false;
406
Dave Airliead7f8a12014-06-05 14:01:32 +1000407 /* get length contained in this portion */
408 msg->curchunk_len = recv_hdr.msg_len;
409 msg->curchunk_hdrlen = hdrlen;
410
411 /* we have already gotten an somt - don't bother parsing */
412 if (recv_hdr.somt && msg->have_somt)
413 return false;
414
415 if (recv_hdr.somt) {
416 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
417 msg->have_somt = true;
418 }
419 if (recv_hdr.eomt)
420 msg->have_eomt = true;
421
422 /* copy the bytes for the remainder of this header chunk */
423 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
424 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
425 } else {
426 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
427 msg->curchunk_idx += replybuflen;
428 }
429
430 if (msg->curchunk_idx >= msg->curchunk_len) {
431 /* do CRC */
432 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
433 /* copy chunk into bigger msg */
434 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
435 msg->curlen += msg->curchunk_len - 1;
436 }
437 return true;
438}
439
440static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
441 struct drm_dp_sideband_msg_reply_body *repmsg)
442{
443 int idx = 1;
444 int i;
445 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
446 idx += 16;
447 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
448 idx++;
449 if (idx > raw->curlen)
450 goto fail_len;
451 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
452 if (raw->msg[idx] & 0x80)
453 repmsg->u.link_addr.ports[i].input_port = 1;
454
455 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
456 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
457
458 idx++;
459 if (idx > raw->curlen)
460 goto fail_len;
461 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
462 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
463 if (repmsg->u.link_addr.ports[i].input_port == 0)
464 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
465 idx++;
466 if (idx > raw->curlen)
467 goto fail_len;
468 if (repmsg->u.link_addr.ports[i].input_port == 0) {
469 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
470 idx++;
471 if (idx > raw->curlen)
472 goto fail_len;
473 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
474 idx += 16;
475 if (idx > raw->curlen)
476 goto fail_len;
477 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
478 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
479 idx++;
480
481 }
482 if (idx > raw->curlen)
483 goto fail_len;
484 }
485
486 return true;
487fail_len:
488 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
489 return false;
490}
491
492static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
493 struct drm_dp_sideband_msg_reply_body *repmsg)
494{
495 int idx = 1;
496 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
497 idx++;
498 if (idx > raw->curlen)
499 goto fail_len;
500 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
Hans Verkuila4c30a42018-08-27 10:07:42 +0200501 idx++;
Dave Airliead7f8a12014-06-05 14:01:32 +1000502 if (idx > raw->curlen)
503 goto fail_len;
504
505 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
506 return true;
507fail_len:
508 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
509 return false;
510}
511
512static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
513 struct drm_dp_sideband_msg_reply_body *repmsg)
514{
515 int idx = 1;
516 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
517 idx++;
518 if (idx > raw->curlen)
519 goto fail_len;
520 return true;
521fail_len:
522 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
523 return false;
524}
525
526static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
527 struct drm_dp_sideband_msg_reply_body *repmsg)
528{
529 int idx = 1;
530
531 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
532 idx++;
533 if (idx > raw->curlen)
534 goto fail_len;
535 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
536 idx++;
537 /* TODO check */
538 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
539 return true;
540fail_len:
541 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
542 return false;
543}
544
545static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
546 struct drm_dp_sideband_msg_reply_body *repmsg)
547{
548 int idx = 1;
549 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
550 idx++;
551 if (idx > raw->curlen)
552 goto fail_len;
553 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
554 idx += 2;
555 if (idx > raw->curlen)
556 goto fail_len;
557 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
558 idx += 2;
559 if (idx > raw->curlen)
560 goto fail_len;
561 return true;
562fail_len:
563 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
564 return false;
565}
566
567static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
568 struct drm_dp_sideband_msg_reply_body *repmsg)
569{
570 int idx = 1;
571 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
572 idx++;
573 if (idx > raw->curlen)
574 goto fail_len;
575 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
576 idx++;
577 if (idx > raw->curlen)
578 goto fail_len;
579 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
580 idx += 2;
581 if (idx > raw->curlen)
582 goto fail_len;
583 return true;
584fail_len:
585 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
586 return false;
587}
588
589static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
590 struct drm_dp_sideband_msg_reply_body *repmsg)
591{
592 int idx = 1;
593 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
594 idx++;
595 if (idx > raw->curlen)
596 goto fail_len;
597 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
598 idx += 2;
599 if (idx > raw->curlen)
600 goto fail_len;
601 return true;
602fail_len:
603 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
604 return false;
605}
606
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700607static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
608 struct drm_dp_sideband_msg_reply_body *repmsg)
609{
610 int idx = 1;
611
612 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
613 idx++;
614 if (idx > raw->curlen) {
615 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
616 idx, raw->curlen);
617 return false;
618 }
619 return true;
620}
621
Dave Airliead7f8a12014-06-05 14:01:32 +1000622static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
623 struct drm_dp_sideband_msg_reply_body *msg)
624{
625 memset(msg, 0, sizeof(*msg));
626 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
627 msg->req_type = (raw->msg[0] & 0x7f);
628
Ville Syrjälä45bbda12019-01-22 22:03:00 +0200629 if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
Dave Airliead7f8a12014-06-05 14:01:32 +1000630 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
631 msg->u.nak.reason = raw->msg[17];
632 msg->u.nak.nak_data = raw->msg[18];
633 return false;
634 }
635
636 switch (msg->req_type) {
637 case DP_LINK_ADDRESS:
638 return drm_dp_sideband_parse_link_address(raw, msg);
639 case DP_QUERY_PAYLOAD:
640 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
641 case DP_REMOTE_DPCD_READ:
642 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
643 case DP_REMOTE_DPCD_WRITE:
644 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
645 case DP_REMOTE_I2C_READ:
646 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
647 case DP_ENUM_PATH_RESOURCES:
648 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
649 case DP_ALLOCATE_PAYLOAD:
650 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700651 case DP_POWER_DOWN_PHY:
652 case DP_POWER_UP_PHY:
653 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
Dave Airliead7f8a12014-06-05 14:01:32 +1000654 default:
Ville Syrjälä3dadbd22019-01-22 22:03:01 +0200655 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
656 drm_dp_mst_req_type_str(msg->req_type));
Dave Airliead7f8a12014-06-05 14:01:32 +1000657 return false;
658 }
659}
660
661static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
662 struct drm_dp_sideband_msg_req_body *msg)
663{
664 int idx = 1;
665
666 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
667 idx++;
668 if (idx > raw->curlen)
669 goto fail_len;
670
671 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
672 idx += 16;
673 if (idx > raw->curlen)
674 goto fail_len;
675
676 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
677 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
678 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
679 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
680 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
681 idx++;
682 return true;
683fail_len:
684 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
685 return false;
686}
687
688static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
689 struct drm_dp_sideband_msg_req_body *msg)
690{
691 int idx = 1;
692
693 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
694 idx++;
695 if (idx > raw->curlen)
696 goto fail_len;
697
698 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
699 idx += 16;
700 if (idx > raw->curlen)
701 goto fail_len;
702
703 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
704 idx++;
705 return true;
706fail_len:
707 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
708 return false;
709}
710
711static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
712 struct drm_dp_sideband_msg_req_body *msg)
713{
714 memset(msg, 0, sizeof(*msg));
715 msg->req_type = (raw->msg[0] & 0x7f);
716
717 switch (msg->req_type) {
718 case DP_CONNECTION_STATUS_NOTIFY:
719 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
720 case DP_RESOURCE_STATUS_NOTIFY:
721 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
722 default:
Ville Syrjälä3dadbd22019-01-22 22:03:01 +0200723 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,
724 drm_dp_mst_req_type_str(msg->req_type));
Dave Airliead7f8a12014-06-05 14:01:32 +1000725 return false;
726 }
727}
728
729static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
730{
731 struct drm_dp_sideband_msg_req_body req;
732
733 req.req_type = DP_REMOTE_DPCD_WRITE;
734 req.u.dpcd_write.port_number = port_num;
735 req.u.dpcd_write.dpcd_address = offset;
736 req.u.dpcd_write.num_bytes = num_bytes;
737 req.u.dpcd_write.bytes = bytes;
738 drm_dp_encode_sideband_req(&req, msg);
739
740 return 0;
741}
742
743static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
744{
745 struct drm_dp_sideband_msg_req_body req;
746
747 req.req_type = DP_LINK_ADDRESS;
748 drm_dp_encode_sideband_req(&req, msg);
749 return 0;
750}
751
752static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
753{
754 struct drm_dp_sideband_msg_req_body req;
755
756 req.req_type = DP_ENUM_PATH_RESOURCES;
757 req.u.port_num.port_number = port_num;
758 drm_dp_encode_sideband_req(&req, msg);
759 msg->path_msg = true;
760 return 0;
761}
762
763static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
Libin Yangef8f9be2015-12-02 14:09:43 +0800764 u8 vcpi, uint16_t pbn,
765 u8 number_sdp_streams,
766 u8 *sdp_stream_sink)
Dave Airliead7f8a12014-06-05 14:01:32 +1000767{
768 struct drm_dp_sideband_msg_req_body req;
769 memset(&req, 0, sizeof(req));
770 req.req_type = DP_ALLOCATE_PAYLOAD;
771 req.u.allocate_payload.port_number = port_num;
772 req.u.allocate_payload.vcpi = vcpi;
773 req.u.allocate_payload.pbn = pbn;
Libin Yangef8f9be2015-12-02 14:09:43 +0800774 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
775 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
776 number_sdp_streams);
Dave Airliead7f8a12014-06-05 14:01:32 +1000777 drm_dp_encode_sideband_req(&req, msg);
778 msg->path_msg = true;
779 return 0;
780}
781
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700782static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
783 int port_num, bool power_up)
784{
785 struct drm_dp_sideband_msg_req_body req;
786
787 if (power_up)
788 req.req_type = DP_POWER_UP_PHY;
789 else
790 req.req_type = DP_POWER_DOWN_PHY;
791
792 req.u.port_num.port_number = port_num;
793 drm_dp_encode_sideband_req(&req, msg);
794 msg->path_msg = true;
795 return 0;
796}
797
Dave Airliead7f8a12014-06-05 14:01:32 +1000798static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
799 struct drm_dp_vcpi *vcpi)
800{
Dave Airliedfda0df2014-08-06 16:26:21 +1000801 int ret, vcpi_ret;
Dave Airliead7f8a12014-06-05 14:01:32 +1000802
803 mutex_lock(&mgr->payload_lock);
804 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
805 if (ret > mgr->max_payloads) {
806 ret = -EINVAL;
807 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
808 goto out_unlock;
809 }
810
Dave Airliedfda0df2014-08-06 16:26:21 +1000811 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
812 if (vcpi_ret > mgr->max_payloads) {
813 ret = -EINVAL;
814 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
815 goto out_unlock;
816 }
817
Dave Airliead7f8a12014-06-05 14:01:32 +1000818 set_bit(ret, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +1000819 set_bit(vcpi_ret, &mgr->vcpi_mask);
820 vcpi->vcpi = vcpi_ret + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +1000821 mgr->proposed_vcpis[ret - 1] = vcpi;
822out_unlock:
823 mutex_unlock(&mgr->payload_lock);
824 return ret;
825}
826
827static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
Dave Airliedfda0df2014-08-06 16:26:21 +1000828 int vcpi)
Dave Airliead7f8a12014-06-05 14:01:32 +1000829{
Dave Airliedfda0df2014-08-06 16:26:21 +1000830 int i;
831 if (vcpi == 0)
Dave Airliead7f8a12014-06-05 14:01:32 +1000832 return;
833
834 mutex_lock(&mgr->payload_lock);
Dave Airliedfda0df2014-08-06 16:26:21 +1000835 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
836 clear_bit(vcpi - 1, &mgr->vcpi_mask);
837
838 for (i = 0; i < mgr->max_payloads; i++) {
839 if (mgr->proposed_vcpis[i])
840 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
841 mgr->proposed_vcpis[i] = NULL;
842 clear_bit(i + 1, &mgr->payload_mask);
843 }
844 }
Dave Airliead7f8a12014-06-05 14:01:32 +1000845 mutex_unlock(&mgr->payload_lock);
846}
847
848static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
849 struct drm_dp_sideband_msg_tx *txmsg)
850{
Chris Wilson992d38c2017-05-13 11:52:00 +0100851 unsigned int state;
Daniel Vettercd961bb2015-01-28 10:02:23 +0100852
853 /*
854 * All updates to txmsg->state are protected by mgr->qlock, and the two
855 * cases we check here are terminal states. For those the barriers
856 * provided by the wake_up/wait_event pair are enough.
857 */
Chris Wilson992d38c2017-05-13 11:52:00 +0100858 state = READ_ONCE(txmsg->state);
859 return (state == DRM_DP_SIDEBAND_TX_RX ||
860 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
Dave Airliead7f8a12014-06-05 14:01:32 +1000861}
862
863static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
864 struct drm_dp_sideband_msg_tx *txmsg)
865{
866 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
867 int ret;
868
869 ret = wait_event_timeout(mgr->tx_waitq,
870 check_txmsg_state(mgr, txmsg),
871 (4 * HZ));
872 mutex_lock(&mstb->mgr->qlock);
873 if (ret > 0) {
874 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
875 ret = -EIO;
876 goto out;
877 }
878 } else {
879 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
880
881 /* dump some state */
882 ret = -EIO;
883
884 /* remove from q */
885 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
886 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
887 list_del(&txmsg->next);
888 }
889
890 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
891 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
892 mstb->tx_slots[txmsg->seqno] = NULL;
893 }
894 }
895out:
896 mutex_unlock(&mgr->qlock);
897
898 return ret;
899}
900
901static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
902{
903 struct drm_dp_mst_branch *mstb;
904
905 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
906 if (!mstb)
907 return NULL;
908
909 mstb->lct = lct;
910 if (lct > 1)
911 memcpy(mstb->rad, rad, lct / 2);
912 INIT_LIST_HEAD(&mstb->ports);
Lyude Paulebcc0e62019-01-10 19:53:29 -0500913 kref_init(&mstb->topology_kref);
914 kref_init(&mstb->malloc_kref);
Dave Airliead7f8a12014-06-05 14:01:32 +1000915 return mstb;
916}
917
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500918static void drm_dp_free_mst_branch_device(struct kref *kref)
919{
Lyude Paulebcc0e62019-01-10 19:53:29 -0500920 struct drm_dp_mst_branch *mstb =
921 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
922
923 if (mstb->port_parent)
924 drm_dp_mst_put_port_malloc(mstb->port_parent);
925
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500926 kfree(mstb);
927}
928
Lyude Paulebcc0e62019-01-10 19:53:29 -0500929/**
930 * DOC: Branch device and port refcounting
931 *
932 * Topology refcount overview
933 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
934 *
935 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
936 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
937 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
938 *
939 * Topology refcounts are not exposed to drivers, and are handled internally
940 * by the DP MST helpers. The helpers use them in order to prevent the
941 * in-memory topology state from being changed in the middle of critical
942 * operations like changing the internal state of payload allocations. This
943 * means each branch and port will be considered to be connected to the rest
Matt Roper1e55a532019-02-01 17:23:26 -0800944 * of the topology until its topology refcount reaches zero. Additionally,
Lyude Paulebcc0e62019-01-10 19:53:29 -0500945 * for ports this means that their associated &struct drm_connector will stay
946 * registered with userspace until the port's refcount reaches 0.
947 *
948 * Malloc refcount overview
949 * ~~~~~~~~~~~~~~~~~~~~~~~~
950 *
951 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
952 * drm_dp_mst_branch allocated even after all of its topology references have
953 * been dropped, so that the driver or MST helpers can safely access each
954 * branch's last known state before it was disconnected from the topology.
955 * When the malloc refcount of a port or branch reaches 0, the memory
956 * allocation containing the &struct drm_dp_mst_branch or &struct
957 * drm_dp_mst_port respectively will be freed.
958 *
959 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
960 * to drivers. As of writing this documentation, there are no drivers that
961 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
962 * helpers. Exposing this API to drivers in a race-free manner would take more
963 * tweaking of the refcounting scheme, however patches are welcome provided
964 * there is a legitimate driver usecase for this.
965 *
966 * Refcount relationships in a topology
967 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
968 *
969 * Let's take a look at why the relationship between topology and malloc
970 * refcounts is designed the way it is.
971 *
972 * .. kernel-figure:: dp-mst/topology-figure-1.dot
973 *
974 * An example of topology and malloc refs in a DP MST topology with two
975 * active payloads. Topology refcount increments are indicated by solid
976 * lines, and malloc refcount increments are indicated by dashed lines.
977 * Each starts from the branch which incremented the refcount, and ends at
978 * the branch to which the refcount belongs to, i.e. the arrow points the
979 * same way as the C pointers used to reference a structure.
980 *
981 * As you can see in the above figure, every branch increments the topology
Matt Roper1e55a532019-02-01 17:23:26 -0800982 * refcount of its children, and increments the malloc refcount of its
983 * parent. Additionally, every payload increments the malloc refcount of its
Lyude Paulebcc0e62019-01-10 19:53:29 -0500984 * assigned port by 1.
985 *
986 * So, what would happen if MSTB #3 from the above figure was unplugged from
987 * the system, but the driver hadn't yet removed payload #2 from port #3? The
988 * topology would start to look like the figure below.
989 *
990 * .. kernel-figure:: dp-mst/topology-figure-2.dot
991 *
992 * Ports and branch devices which have been released from memory are
993 * colored grey, and references which have been removed are colored red.
994 *
995 * Whenever a port or branch device's topology refcount reaches zero, it will
996 * decrement the topology refcounts of all its children, the malloc refcount
997 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
998 * #4, this means they both have been disconnected from the topology and freed
999 * from memory. But, because payload #2 is still holding a reference to port
Matt Roper1e55a532019-02-01 17:23:26 -08001000 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
Lyude Paulebcc0e62019-01-10 19:53:29 -05001001 * is still accessible from memory. This also means port #3 has not yet
Matt Roper1e55a532019-02-01 17:23:26 -08001002 * decremented the malloc refcount of MSTB #3, so its &struct
Lyude Paulebcc0e62019-01-10 19:53:29 -05001003 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1004 * malloc refcount reaches 0.
1005 *
1006 * This relationship is necessary because in order to release payload #2, we
1007 * need to be able to figure out the last relative of port #3 that's still
1008 * connected to the topology. In this case, we would travel up the topology as
1009 * shown below.
1010 *
1011 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1012 *
1013 * And finally, remove payload #2 by communicating with port #2 through
1014 * sideband transactions.
1015 */
1016
1017/**
1018 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1019 * device
1020 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1021 *
1022 * Increments &drm_dp_mst_branch.malloc_kref. When
1023 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1024 * will be released and @mstb may no longer be used.
1025 *
1026 * See also: drm_dp_mst_put_mstb_malloc()
1027 */
1028static void
1029drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1030{
1031 kref_get(&mstb->malloc_kref);
1032 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
1033}
1034
1035/**
1036 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1037 * device
1038 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1039 *
1040 * Decrements &drm_dp_mst_branch.malloc_kref. When
1041 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1042 * will be released and @mstb may no longer be used.
1043 *
1044 * See also: drm_dp_mst_get_mstb_malloc()
1045 */
1046static void
1047drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1048{
1049 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
1050 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
1051}
1052
1053static void drm_dp_free_mst_port(struct kref *kref)
1054{
1055 struct drm_dp_mst_port *port =
1056 container_of(kref, struct drm_dp_mst_port, malloc_kref);
1057
1058 drm_dp_mst_put_mstb_malloc(port->parent);
1059 kfree(port);
1060}
1061
1062/**
1063 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1064 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1065 *
1066 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1067 * reaches 0, the memory allocation for @port will be released and @port may
1068 * no longer be used.
1069 *
1070 * Because @port could potentially be freed at any time by the DP MST helpers
1071 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1072 * function, drivers that which to make use of &struct drm_dp_mst_port should
1073 * ensure that they grab at least one main malloc reference to their MST ports
1074 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1075 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1076 *
1077 * See also: drm_dp_mst_put_port_malloc()
1078 */
1079void
1080drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1081{
1082 kref_get(&port->malloc_kref);
1083 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1084}
1085EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1086
1087/**
1088 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1089 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1090 *
1091 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1092 * reaches 0, the memory allocation for @port will be released and @port may
1093 * no longer be used.
1094 *
1095 * See also: drm_dp_mst_get_port_malloc()
1096 */
1097void
1098drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1099{
1100 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1101 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1102}
1103EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1104
Dave Airliead7f8a12014-06-05 14:01:32 +10001105static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1106{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001107 struct drm_dp_mst_branch *mstb =
1108 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1109 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
Dave Airliead7f8a12014-06-05 14:01:32 +10001110 struct drm_dp_mst_port *port, *tmp;
1111 bool wake_tx = false;
1112
Lyude Paulebcc0e62019-01-10 19:53:29 -05001113 mutex_lock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001114 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
1115 list_del(&port->next);
Lyude Pauld0757af2019-01-10 19:53:28 -05001116 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001117 }
Lyude Paulebcc0e62019-01-10 19:53:29 -05001118 mutex_unlock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001119
1120 /* drop any tx slots msg */
1121 mutex_lock(&mstb->mgr->qlock);
1122 if (mstb->tx_slots[0]) {
1123 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1124 mstb->tx_slots[0] = NULL;
1125 wake_tx = true;
1126 }
1127 if (mstb->tx_slots[1]) {
1128 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1129 mstb->tx_slots[1] = NULL;
1130 wake_tx = true;
1131 }
1132 mutex_unlock(&mstb->mgr->qlock);
1133
1134 if (wake_tx)
Chris Wilson68e989d2017-05-13 11:52:01 +01001135 wake_up_all(&mstb->mgr->tx_waitq);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001136
Lyude Paulebcc0e62019-01-10 19:53:29 -05001137 drm_dp_mst_put_mstb_malloc(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001138}
1139
Lyude Paulebcc0e62019-01-10 19:53:29 -05001140/**
1141 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
Matt Roper1e55a532019-02-01 17:23:26 -08001142 * branch device unless it's zero
Lyude Paulebcc0e62019-01-10 19:53:29 -05001143 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1144 *
1145 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1146 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1147 * reached 0). Holding a topology reference implies that a malloc reference
1148 * will be held to @mstb as long as the user holds the topology reference.
1149 *
1150 * Care should be taken to ensure that the user has at least one malloc
1151 * reference to @mstb. If you already have a topology reference to @mstb, you
1152 * should use drm_dp_mst_topology_get_mstb() instead.
1153 *
1154 * See also:
1155 * drm_dp_mst_topology_get_mstb()
1156 * drm_dp_mst_topology_put_mstb()
1157 *
1158 * Returns:
1159 * * 1: A topology reference was grabbed successfully
1160 * * 0: @port is no longer in the topology, no reference was grabbed
1161 */
1162static int __must_check
1163drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001164{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001165 int ret = kref_get_unless_zero(&mstb->topology_kref);
1166
1167 if (ret)
1168 DRM_DEBUG("mstb %p (%d)\n", mstb,
1169 kref_read(&mstb->topology_kref));
1170
1171 return ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001172}
1173
Lyude Paulebcc0e62019-01-10 19:53:29 -05001174/**
1175 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1176 * branch device
1177 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1178 *
1179 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1180 * not it's already reached 0. This is only valid to use in scenarios where
1181 * you are already guaranteed to have at least one active topology reference
1182 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1183 *
1184 * See also:
1185 * drm_dp_mst_topology_try_get_mstb()
1186 * drm_dp_mst_topology_put_mstb()
1187 */
1188static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1189{
1190 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1191 kref_get(&mstb->topology_kref);
1192 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1193}
1194
1195/**
1196 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1197 * device
1198 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1199 *
1200 * Releases a topology reference from @mstb by decrementing
1201 * &drm_dp_mst_branch.topology_kref.
1202 *
1203 * See also:
1204 * drm_dp_mst_topology_try_get_mstb()
1205 * drm_dp_mst_topology_get_mstb()
1206 */
1207static void
1208drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1209{
1210 DRM_DEBUG("mstb %p (%d)\n",
1211 mstb, kref_read(&mstb->topology_kref) - 1);
1212 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1213}
Dave Airliead7f8a12014-06-05 14:01:32 +10001214
1215static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
1216{
Daniel Vetter03913592014-12-08 22:55:22 +01001217 struct drm_dp_mst_branch *mstb;
1218
Dave Airliead7f8a12014-06-05 14:01:32 +10001219 switch (old_pdt) {
1220 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1221 case DP_PEER_DEVICE_SST_SINK:
1222 /* remove i2c over sideband */
1223 drm_dp_mst_unregister_i2c_bus(&port->aux);
1224 break;
1225 case DP_PEER_DEVICE_MST_BRANCHING:
Daniel Vetter03913592014-12-08 22:55:22 +01001226 mstb = port->mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +10001227 port->mstb = NULL;
Lyude Pauld0757af2019-01-10 19:53:28 -05001228 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001229 break;
1230 }
1231}
1232
1233static void drm_dp_destroy_port(struct kref *kref)
1234{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001235 struct drm_dp_mst_port *port =
1236 container_of(kref, struct drm_dp_mst_port, topology_kref);
Dave Airliead7f8a12014-06-05 14:01:32 +10001237 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
Dave Airliedf4839f2015-09-16 10:37:28 +10001238
Dave Airliead7f8a12014-06-05 14:01:32 +10001239 if (!port->input) {
Dave Airliec6a0aed2014-10-20 16:28:02 +10001240 kfree(port->cached_edid);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001241
Dave Airliedf4839f2015-09-16 10:37:28 +10001242 /*
1243 * The only time we don't have a connector
1244 * on an output port is if the connector init
1245 * fails.
1246 */
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001247 if (port->connector) {
Dave Airliedf4839f2015-09-16 10:37:28 +10001248 /* we can't destroy the connector here, as
1249 * we might be holding the mode_config.mutex
1250 * from an EDID retrieval */
1251
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001252 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02001253 list_add(&port->next, &mgr->destroy_connector_list);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001254 mutex_unlock(&mgr->destroy_connector_lock);
1255 schedule_work(&mgr->destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02001256 return;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001257 }
Dave Airliedf4839f2015-09-16 10:37:28 +10001258 /* no need to clean up vcpi
1259 * as if we have no connector we never setup a vcpi */
Dave Airliead7f8a12014-06-05 14:01:32 +10001260 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +03001261 port->pdt = DP_PEER_DEVICE_NONE;
Dave Airliead7f8a12014-06-05 14:01:32 +10001262 }
Lyude Paulebcc0e62019-01-10 19:53:29 -05001263 drm_dp_mst_put_port_malloc(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001264}
1265
Lyude Paulebcc0e62019-01-10 19:53:29 -05001266/**
1267 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
Matt Roper1e55a532019-02-01 17:23:26 -08001268 * port unless it's zero
Lyude Paulebcc0e62019-01-10 19:53:29 -05001269 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1270 *
1271 * Attempts to grab a topology reference to @port, if it hasn't yet been
1272 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1273 * 0). Holding a topology reference implies that a malloc reference will be
1274 * held to @port as long as the user holds the topology reference.
1275 *
1276 * Care should be taken to ensure that the user has at least one malloc
1277 * reference to @port. If you already have a topology reference to @port, you
1278 * should use drm_dp_mst_topology_get_port() instead.
1279 *
1280 * See also:
1281 * drm_dp_mst_topology_get_port()
1282 * drm_dp_mst_topology_put_port()
1283 *
1284 * Returns:
1285 * * 1: A topology reference was grabbed successfully
1286 * * 0: @port is no longer in the topology, no reference was grabbed
1287 */
1288static int __must_check
1289drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1290{
1291 int ret = kref_get_unless_zero(&port->topology_kref);
1292
1293 if (ret)
1294 DRM_DEBUG("port %p (%d)\n", port,
1295 kref_read(&port->topology_kref));
1296
1297 return ret;
1298}
1299
1300/**
1301 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1302 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1303 *
1304 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1305 * not it's already reached 0. This is only valid to use in scenarios where
1306 * you are already guaranteed to have at least one active topology reference
1307 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1308 *
1309 * See also:
1310 * drm_dp_mst_topology_try_get_port()
1311 * drm_dp_mst_topology_put_port()
1312 */
1313static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1314{
1315 WARN_ON(kref_read(&port->topology_kref) == 0);
1316 kref_get(&port->topology_kref);
1317 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1318}
1319
1320/**
1321 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1322 * @port: The &struct drm_dp_mst_port to release the topology reference from
1323 *
1324 * Releases a topology reference from @port by decrementing
1325 * &drm_dp_mst_port.topology_kref.
1326 *
1327 * See also:
1328 * drm_dp_mst_topology_try_get_port()
1329 * drm_dp_mst_topology_get_port()
1330 */
Lyude Pauld0757af2019-01-10 19:53:28 -05001331static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10001332{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001333 DRM_DEBUG("port %p (%d)\n",
1334 port, kref_read(&port->topology_kref) - 1);
1335 kref_put(&port->topology_kref, drm_dp_destroy_port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001336}
1337
Lyude Pauld0757af2019-01-10 19:53:28 -05001338static struct drm_dp_mst_branch *
1339drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1340 struct drm_dp_mst_branch *to_find)
Dave Airliead7f8a12014-06-05 14:01:32 +10001341{
1342 struct drm_dp_mst_port *port;
1343 struct drm_dp_mst_branch *rmstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001344
1345 if (to_find == mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001346 return mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001347
Dave Airliead7f8a12014-06-05 14:01:32 +10001348 list_for_each_entry(port, &mstb->ports, next) {
1349 if (port->mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001350 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1351 port->mstb, to_find);
Dave Airliead7f8a12014-06-05 14:01:32 +10001352 if (rmstb)
1353 return rmstb;
1354 }
1355 }
1356 return NULL;
1357}
1358
Lyude Pauld0757af2019-01-10 19:53:28 -05001359static struct drm_dp_mst_branch *
1360drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1361 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001362{
1363 struct drm_dp_mst_branch *rmstb = NULL;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001364
Dave Airliead7f8a12014-06-05 14:01:32 +10001365 mutex_lock(&mgr->lock);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001366 if (mgr->mst_primary) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001367 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1368 mgr->mst_primary, mstb);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001369
1370 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1371 rmstb = NULL;
1372 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001373 mutex_unlock(&mgr->lock);
1374 return rmstb;
1375}
1376
Lyude Paulebcc0e62019-01-10 19:53:29 -05001377static struct drm_dp_mst_port *
1378drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1379 struct drm_dp_mst_port *to_find)
Dave Airliead7f8a12014-06-05 14:01:32 +10001380{
1381 struct drm_dp_mst_port *port, *mport;
1382
1383 list_for_each_entry(port, &mstb->ports, next) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001384 if (port == to_find)
Dave Airliead7f8a12014-06-05 14:01:32 +10001385 return port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001386
Dave Airliead7f8a12014-06-05 14:01:32 +10001387 if (port->mstb) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001388 mport = drm_dp_mst_topology_get_port_validated_locked(
1389 port->mstb, to_find);
Dave Airliead7f8a12014-06-05 14:01:32 +10001390 if (mport)
1391 return mport;
1392 }
1393 }
1394 return NULL;
1395}
1396
Lyude Pauld0757af2019-01-10 19:53:28 -05001397static struct drm_dp_mst_port *
1398drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1399 struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10001400{
1401 struct drm_dp_mst_port *rport = NULL;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001402
Dave Airliead7f8a12014-06-05 14:01:32 +10001403 mutex_lock(&mgr->lock);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001404 if (mgr->mst_primary) {
1405 rport = drm_dp_mst_topology_get_port_validated_locked(
1406 mgr->mst_primary, port);
1407
1408 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1409 rport = NULL;
1410 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001411 mutex_unlock(&mgr->lock);
1412 return rport;
1413}
1414
1415static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1416{
1417 struct drm_dp_mst_port *port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001418 int ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001419
1420 list_for_each_entry(port, &mstb->ports, next) {
1421 if (port->port_num == port_num) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001422 ret = drm_dp_mst_topology_try_get_port(port);
1423 return ret ? port : NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001424 }
1425 }
1426
1427 return NULL;
1428}
1429
1430/*
1431 * calculate a new RAD for this MST branch device
1432 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1433 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1434 */
1435static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1436 u8 *rad)
1437{
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001438 int parent_lct = port->parent->lct;
Dave Airliead7f8a12014-06-05 14:01:32 +10001439 int shift = 4;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001440 int idx = (parent_lct - 1) / 2;
1441 if (parent_lct > 1) {
1442 memcpy(rad, port->parent->rad, idx + 1);
1443 shift = (parent_lct % 2) ? 4 : 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001444 } else
1445 rad[0] = 0;
1446
1447 rad[idx] |= port->port_num << shift;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001448 return parent_lct + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +10001449}
1450
1451/*
1452 * return sends link address for new mstb
1453 */
1454static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1455{
1456 int ret;
1457 u8 rad[6], lct;
1458 bool send_link = false;
1459 switch (port->pdt) {
1460 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1461 case DP_PEER_DEVICE_SST_SINK:
1462 /* add i2c over sideband */
1463 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1464 break;
1465 case DP_PEER_DEVICE_MST_BRANCHING:
1466 lct = drm_dp_calculate_rad(port, rad);
1467
1468 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
Joe Moriarty22a07032018-02-12 14:51:42 -05001469 if (port->mstb) {
1470 port->mstb->mgr = port->mgr;
1471 port->mstb->port_parent = port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001472 /*
1473 * Make sure this port's memory allocation stays
Matt Roper1e55a532019-02-01 17:23:26 -08001474 * around until its child MSTB releases it
Lyude Paulebcc0e62019-01-10 19:53:29 -05001475 */
1476 drm_dp_mst_get_port_malloc(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001477
Joe Moriarty22a07032018-02-12 14:51:42 -05001478 send_link = true;
1479 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001480 break;
1481 }
1482 return send_link;
1483}
1484
Hersen Wu5e93b822016-01-22 17:07:28 -05001485static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
Dave Airliead7f8a12014-06-05 14:01:32 +10001486{
1487 int ret;
Hersen Wu5e93b822016-01-22 17:07:28 -05001488
1489 memcpy(mstb->guid, guid, 16);
1490
1491 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1492 if (mstb->port_parent) {
1493 ret = drm_dp_send_dpcd_write(
1494 mstb->mgr,
1495 mstb->port_parent,
1496 DP_GUID,
1497 16,
1498 mstb->guid);
1499 } else {
1500
1501 ret = drm_dp_dpcd_write(
1502 mstb->mgr->aux,
1503 DP_GUID,
1504 mstb->guid,
1505 16);
Dave Airliead7f8a12014-06-05 14:01:32 +10001506 }
1507 }
1508}
1509
Dave Airlie1c960872015-09-16 11:04:49 +10001510static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1511 int pnum,
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001512 char *proppath,
1513 size_t proppath_size)
Dave Airliead7f8a12014-06-05 14:01:32 +10001514{
1515 int i;
1516 char temp[8];
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001517 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
Dave Airliead7f8a12014-06-05 14:01:32 +10001518 for (i = 0; i < (mstb->lct - 1); i++) {
1519 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001520 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001521 snprintf(temp, sizeof(temp), "-%d", port_num);
1522 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001523 }
Dave Airlie1c960872015-09-16 11:04:49 +10001524 snprintf(temp, sizeof(temp), "-%d", pnum);
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001525 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001526}
1527
1528static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001529 struct drm_device *dev,
Dave Airliead7f8a12014-06-05 14:01:32 +10001530 struct drm_dp_link_addr_reply_port *port_msg)
1531{
1532 struct drm_dp_mst_port *port;
1533 bool ret;
1534 bool created = false;
1535 int old_pdt = 0;
1536 int old_ddps = 0;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001537
Dave Airliead7f8a12014-06-05 14:01:32 +10001538 port = drm_dp_get_port(mstb, port_msg->port_number);
1539 if (!port) {
1540 port = kzalloc(sizeof(*port), GFP_KERNEL);
1541 if (!port)
1542 return;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001543 kref_init(&port->topology_kref);
1544 kref_init(&port->malloc_kref);
Dave Airliead7f8a12014-06-05 14:01:32 +10001545 port->parent = mstb;
1546 port->port_num = port_msg->port_number;
1547 port->mgr = mstb->mgr;
1548 port->aux.name = "DPMST";
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001549 port->aux.dev = dev->dev;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001550
1551 /*
1552 * Make sure the memory allocation for our parent branch stays
1553 * around until our own memory allocation is released
1554 */
1555 drm_dp_mst_get_mstb_malloc(mstb);
1556
Dave Airliead7f8a12014-06-05 14:01:32 +10001557 created = true;
1558 } else {
1559 old_pdt = port->pdt;
1560 old_ddps = port->ddps;
1561 }
1562
1563 port->pdt = port_msg->peer_device_type;
1564 port->input = port_msg->input_port;
1565 port->mcs = port_msg->mcs;
1566 port->ddps = port_msg->ddps;
1567 port->ldps = port_msg->legacy_device_plug_status;
1568 port->dpcd_rev = port_msg->dpcd_revision;
1569 port->num_sdp_streams = port_msg->num_sdp_streams;
1570 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
Dave Airliead7f8a12014-06-05 14:01:32 +10001571
1572 /* manage mstb port lists with mgr lock - take a reference
1573 for this list */
1574 if (created) {
1575 mutex_lock(&mstb->mgr->lock);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001576 drm_dp_mst_topology_get_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001577 list_add(&port->next, &mstb->ports);
1578 mutex_unlock(&mstb->mgr->lock);
1579 }
1580
1581 if (old_ddps != port->ddps) {
1582 if (port->ddps) {
Lyude Paul3d76df62019-01-10 19:53:24 -05001583 if (!port->input) {
1584 drm_dp_send_enum_path_resources(mstb->mgr,
1585 mstb, port);
1586 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001587 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001588 port->available_pbn = 0;
Lyude Paul3d76df62019-01-10 19:53:24 -05001589 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001590 }
1591
1592 if (old_pdt != port->pdt && !port->input) {
1593 drm_dp_port_teardown_pdt(port, old_pdt);
1594
1595 ret = drm_dp_port_setup_pdt(port);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001596 if (ret == true)
Dave Airliead7f8a12014-06-05 14:01:32 +10001597 drm_dp_send_link_address(mstb->mgr, port->mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001598 }
1599
1600 if (created && !port->input) {
1601 char proppath[255];
Dave Airlie1c960872015-09-16 11:04:49 +10001602
Lyude Paul3d76df62019-01-10 19:53:24 -05001603 build_mst_prop_path(mstb, port->port_num, proppath,
1604 sizeof(proppath));
1605 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
1606 port,
1607 proppath);
Dave Airliedf4839f2015-09-16 10:37:28 +10001608 if (!port->connector) {
1609 /* remove it from the port list */
1610 mutex_lock(&mstb->mgr->lock);
1611 list_del(&port->next);
1612 mutex_unlock(&mstb->mgr->lock);
1613 /* drop port list reference */
Lyude Pauld0757af2019-01-10 19:53:28 -05001614 drm_dp_mst_topology_put_port(port);
Dave Airliedf4839f2015-09-16 10:37:28 +10001615 goto out;
1616 }
Ville Syrjälä4da5caa2016-10-26 12:05:55 +03001617 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1618 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1619 port->port_num >= DP_MST_LOGICAL_PORT_0) {
Lyude Paul3d76df62019-01-10 19:53:24 -05001620 port->cached_edid = drm_get_edid(port->connector,
1621 &port->aux.ddc);
Daniel Vetter97e14fb2018-07-09 10:40:08 +02001622 drm_connector_set_tile_property(port->connector);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001623 }
Dave Airlied9515c52015-09-16 17:55:23 +10001624 (*mstb->mgr->cbs->register_connector)(port->connector);
Dave Airliead7f8a12014-06-05 14:01:32 +10001625 }
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001626
Dave Airliedf4839f2015-09-16 10:37:28 +10001627out:
Dave Airliead7f8a12014-06-05 14:01:32 +10001628 /* put reference to this port */
Lyude Pauld0757af2019-01-10 19:53:28 -05001629 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001630}
1631
1632static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1633 struct drm_dp_connection_status_notify *conn_stat)
1634{
1635 struct drm_dp_mst_port *port;
1636 int old_pdt;
1637 int old_ddps;
1638 bool dowork = false;
1639 port = drm_dp_get_port(mstb, conn_stat->port_number);
1640 if (!port)
1641 return;
1642
1643 old_ddps = port->ddps;
1644 old_pdt = port->pdt;
1645 port->pdt = conn_stat->peer_device_type;
1646 port->mcs = conn_stat->message_capability_status;
1647 port->ldps = conn_stat->legacy_device_plug_status;
1648 port->ddps = conn_stat->displayport_device_plug_status;
1649
1650 if (old_ddps != port->ddps) {
1651 if (port->ddps) {
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001652 dowork = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001653 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001654 port->available_pbn = 0;
1655 }
1656 }
1657 if (old_pdt != port->pdt && !port->input) {
1658 drm_dp_port_teardown_pdt(port, old_pdt);
1659
1660 if (drm_dp_port_setup_pdt(port))
1661 dowork = true;
1662 }
1663
Lyude Pauld0757af2019-01-10 19:53:28 -05001664 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001665 if (dowork)
1666 queue_work(system_long_wq, &mstb->mgr->work);
1667
1668}
1669
1670static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1671 u8 lct, u8 *rad)
1672{
1673 struct drm_dp_mst_branch *mstb;
1674 struct drm_dp_mst_port *port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001675 int i, ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001676 /* find the port by iterating down */
Dave Airlie9eb1e572015-06-22 14:40:44 +10001677
1678 mutex_lock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001679 mstb = mgr->mst_primary;
1680
Stanislav Lisovskiy23d80032018-11-09 11:00:12 +02001681 if (!mstb)
1682 goto out;
1683
Dave Airliead7f8a12014-06-05 14:01:32 +10001684 for (i = 0; i < lct - 1; i++) {
1685 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001686 int port_num = (rad[i / 2] >> shift) & 0xf;
Dave Airliead7f8a12014-06-05 14:01:32 +10001687
1688 list_for_each_entry(port, &mstb->ports, next) {
1689 if (port->port_num == port_num) {
Adam Richter30730c72015-10-16 03:33:02 -07001690 mstb = port->mstb;
1691 if (!mstb) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001692 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
Adam Richter30730c72015-10-16 03:33:02 -07001693 goto out;
Dave Airliead7f8a12014-06-05 14:01:32 +10001694 }
1695
Dave Airliead7f8a12014-06-05 14:01:32 +10001696 break;
1697 }
1698 }
1699 }
Lyude Paulebcc0e62019-01-10 19:53:29 -05001700 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1701 if (!ret)
1702 mstb = NULL;
Adam Richter30730c72015-10-16 03:33:02 -07001703out:
Dave Airlie9eb1e572015-06-22 14:40:44 +10001704 mutex_unlock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001705 return mstb;
1706}
1707
Mykola Lysenkobd934322015-12-18 17:14:42 -05001708static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1709 struct drm_dp_mst_branch *mstb,
1710 uint8_t *guid)
1711{
1712 struct drm_dp_mst_branch *found_mstb;
1713 struct drm_dp_mst_port *port;
1714
Hersen Wu5e93b822016-01-22 17:07:28 -05001715 if (memcmp(mstb->guid, guid, 16) == 0)
1716 return mstb;
1717
1718
Mykola Lysenkobd934322015-12-18 17:14:42 -05001719 list_for_each_entry(port, &mstb->ports, next) {
1720 if (!port->mstb)
1721 continue;
1722
Mykola Lysenkobd934322015-12-18 17:14:42 -05001723 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1724
1725 if (found_mstb)
1726 return found_mstb;
1727 }
1728
1729 return NULL;
1730}
1731
Lyude Paulde6d6812019-01-10 19:53:25 -05001732static struct drm_dp_mst_branch *
1733drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
1734 uint8_t *guid)
Mykola Lysenkobd934322015-12-18 17:14:42 -05001735{
1736 struct drm_dp_mst_branch *mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001737 int ret;
Mykola Lysenkobd934322015-12-18 17:14:42 -05001738
1739 /* find the port by iterating down */
1740 mutex_lock(&mgr->lock);
1741
Hersen Wu5e93b822016-01-22 17:07:28 -05001742 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001743 if (mstb) {
1744 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1745 if (!ret)
1746 mstb = NULL;
1747 }
Mykola Lysenkobd934322015-12-18 17:14:42 -05001748
1749 mutex_unlock(&mgr->lock);
1750 return mstb;
1751}
1752
Dave Airliead7f8a12014-06-05 14:01:32 +10001753static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1754 struct drm_dp_mst_branch *mstb)
1755{
1756 struct drm_dp_mst_port *port;
Daniel Vetter9254ec42015-06-22 17:31:59 +10001757 struct drm_dp_mst_branch *mstb_child;
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001758 if (!mstb->link_address_sent)
Dave Airliead7f8a12014-06-05 14:01:32 +10001759 drm_dp_send_link_address(mgr, mstb);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001760
Dave Airliead7f8a12014-06-05 14:01:32 +10001761 list_for_each_entry(port, &mstb->ports, next) {
1762 if (port->input)
1763 continue;
1764
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001765 if (!port->ddps)
Dave Airliead7f8a12014-06-05 14:01:32 +10001766 continue;
1767
1768 if (!port->available_pbn)
1769 drm_dp_send_enum_path_resources(mgr, mstb, port);
1770
Daniel Vetter9254ec42015-06-22 17:31:59 +10001771 if (port->mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001772 mstb_child = drm_dp_mst_topology_get_mstb_validated(
1773 mgr, port->mstb);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001774 if (mstb_child) {
1775 drm_dp_check_and_send_link_address(mgr, mstb_child);
Lyude Pauld0757af2019-01-10 19:53:28 -05001776 drm_dp_mst_topology_put_mstb(mstb_child);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001777 }
1778 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001779 }
1780}
1781
1782static void drm_dp_mst_link_probe_work(struct work_struct *work)
1783{
1784 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001785 struct drm_dp_mst_branch *mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001786 int ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001787
Daniel Vetter9254ec42015-06-22 17:31:59 +10001788 mutex_lock(&mgr->lock);
1789 mstb = mgr->mst_primary;
1790 if (mstb) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001791 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1792 if (!ret)
1793 mstb = NULL;
Daniel Vetter9254ec42015-06-22 17:31:59 +10001794 }
1795 mutex_unlock(&mgr->lock);
1796 if (mstb) {
1797 drm_dp_check_and_send_link_address(mgr, mstb);
Lyude Pauld0757af2019-01-10 19:53:28 -05001798 drm_dp_mst_topology_put_mstb(mstb);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001799 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001800}
1801
1802static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1803 u8 *guid)
1804{
Ville Syrjäläe38e1282017-07-12 18:52:54 +03001805 u64 salt;
Dave Airliead7f8a12014-06-05 14:01:32 +10001806
Ville Syrjäläe38e1282017-07-12 18:52:54 +03001807 if (memchr_inv(guid, 0, 16))
1808 return true;
1809
1810 salt = get_jiffies_64();
1811
1812 memcpy(&guid[0], &salt, sizeof(u64));
1813 memcpy(&guid[8], &salt, sizeof(u64));
1814
1815 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10001816}
1817
1818#if 0
1819static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1820{
1821 struct drm_dp_sideband_msg_req_body req;
1822
1823 req.req_type = DP_REMOTE_DPCD_READ;
1824 req.u.dpcd_read.port_number = port_num;
1825 req.u.dpcd_read.dpcd_address = offset;
1826 req.u.dpcd_read.num_bytes = num_bytes;
1827 drm_dp_encode_sideband_req(&req, msg);
1828
1829 return 0;
1830}
1831#endif
1832
1833static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1834 bool up, u8 *msg, int len)
1835{
1836 int ret;
1837 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1838 int tosend, total, offset;
1839 int retries = 0;
1840
1841retry:
1842 total = len;
1843 offset = 0;
1844 do {
1845 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1846
1847 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1848 &msg[offset],
1849 tosend);
1850 if (ret != tosend) {
1851 if (ret == -EIO && retries < 5) {
1852 retries++;
1853 goto retry;
1854 }
1855 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10001856
1857 return -EIO;
1858 }
1859 offset += tosend;
1860 total -= tosend;
1861 } while (total > 0);
1862 return 0;
1863}
1864
1865static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1866 struct drm_dp_sideband_msg_tx *txmsg)
1867{
1868 struct drm_dp_mst_branch *mstb = txmsg->dst;
Mykola Lysenkobd934322015-12-18 17:14:42 -05001869 u8 req_type;
Dave Airliead7f8a12014-06-05 14:01:32 +10001870
1871 /* both msg slots are full */
1872 if (txmsg->seqno == -1) {
1873 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1874 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1875 return -EAGAIN;
1876 }
1877 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1878 txmsg->seqno = mstb->last_seqno;
1879 mstb->last_seqno ^= 1;
1880 } else if (mstb->tx_slots[0] == NULL)
1881 txmsg->seqno = 0;
1882 else
1883 txmsg->seqno = 1;
1884 mstb->tx_slots[txmsg->seqno] = txmsg;
1885 }
Mykola Lysenkobd934322015-12-18 17:14:42 -05001886
1887 req_type = txmsg->msg[0] & 0x7f;
1888 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1889 req_type == DP_RESOURCE_STATUS_NOTIFY)
1890 hdr->broadcast = 1;
1891 else
1892 hdr->broadcast = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001893 hdr->path_msg = txmsg->path_msg;
1894 hdr->lct = mstb->lct;
1895 hdr->lcr = mstb->lct - 1;
1896 if (mstb->lct > 1)
1897 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1898 hdr->seqno = txmsg->seqno;
1899 return 0;
1900}
1901/*
1902 * process a single block of the next message in the sideband queue
1903 */
1904static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1905 struct drm_dp_sideband_msg_tx *txmsg,
1906 bool up)
1907{
1908 u8 chunk[48];
1909 struct drm_dp_sideband_msg_hdr hdr;
1910 int len, space, idx, tosend;
1911 int ret;
1912
Damien Lespiaubf3719c2014-07-14 12:13:18 +01001913 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1914
Dave Airliead7f8a12014-06-05 14:01:32 +10001915 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1916 txmsg->seqno = -1;
1917 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1918 }
1919
1920 /* make hdr from dst mst - for replies use seqno
1921 otherwise assign one */
1922 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1923 if (ret < 0)
1924 return ret;
1925
1926 /* amount left to send in this message */
1927 len = txmsg->cur_len - txmsg->cur_offset;
1928
1929 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1930 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1931
1932 tosend = min(len, space);
1933 if (len == txmsg->cur_len)
1934 hdr.somt = 1;
1935 if (space >= len)
1936 hdr.eomt = 1;
1937
1938
1939 hdr.msg_len = tosend + 1;
1940 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1941 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1942 /* add crc at end */
1943 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1944 idx += tosend + 1;
1945
1946 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1947 if (ret) {
1948 DRM_DEBUG_KMS("sideband msg failed to send\n");
1949 return ret;
1950 }
1951
1952 txmsg->cur_offset += tosend;
1953 if (txmsg->cur_offset == txmsg->cur_len) {
1954 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1955 return 1;
1956 }
1957 return 0;
1958}
1959
Dave Airliead7f8a12014-06-05 14:01:32 +10001960static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1961{
1962 struct drm_dp_sideband_msg_tx *txmsg;
1963 int ret;
1964
Daniel Vettercd961bb2015-01-28 10:02:23 +01001965 WARN_ON(!mutex_is_locked(&mgr->qlock));
1966
Dave Airliead7f8a12014-06-05 14:01:32 +10001967 /* construct a chunk from the first msg in the tx_msg queue */
Daniel Vettercb021a32016-07-15 21:48:03 +02001968 if (list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10001969 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001970
1971 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1972 ret = process_single_tx_qlock(mgr, txmsg, false);
1973 if (ret == 1) {
1974 /* txmsg is sent it should be in the slots now */
1975 list_del(&txmsg->next);
1976 } else if (ret) {
1977 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1978 list_del(&txmsg->next);
1979 if (txmsg->seqno != -1)
1980 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1981 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
Chris Wilson68e989d2017-05-13 11:52:01 +01001982 wake_up_all(&mgr->tx_waitq);
Dave Airliead7f8a12014-06-05 14:01:32 +10001983 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001984}
1985
1986/* called holding qlock */
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001987static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1988 struct drm_dp_sideband_msg_tx *txmsg)
Dave Airliead7f8a12014-06-05 14:01:32 +10001989{
Dave Airliead7f8a12014-06-05 14:01:32 +10001990 int ret;
1991
1992 /* construct a chunk from the first msg in the tx_msg queue */
Dave Airliead7f8a12014-06-05 14:01:32 +10001993 ret = process_single_tx_qlock(mgr, txmsg, true);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001994
1995 if (ret != 1)
Dave Airliead7f8a12014-06-05 14:01:32 +10001996 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001997
1998 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001999}
2000
2001static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2002 struct drm_dp_sideband_msg_tx *txmsg)
2003{
2004 mutex_lock(&mgr->qlock);
2005 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
Daniel Vettercb021a32016-07-15 21:48:03 +02002006 if (list_is_singular(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10002007 process_single_down_tx_qlock(mgr);
2008 mutex_unlock(&mgr->qlock);
2009}
2010
Dave Airlie68d8c9f2015-09-06 18:53:00 +10002011static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2012 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10002013{
2014 int len;
2015 struct drm_dp_sideband_msg_tx *txmsg;
2016 int ret;
2017
2018 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2019 if (!txmsg)
Dave Airlie68d8c9f2015-09-06 18:53:00 +10002020 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10002021
2022 txmsg->dst = mstb;
2023 len = build_link_address(txmsg);
2024
Dave Airlie68d8c9f2015-09-06 18:53:00 +10002025 mstb->link_address_sent = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002026 drm_dp_queue_down_tx(mgr, txmsg);
2027
2028 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2029 if (ret > 0) {
2030 int i;
2031
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002032 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002033 DRM_DEBUG_KMS("link address nak received\n");
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002034 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10002035 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
2036 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
2037 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,
2038 txmsg->reply.u.link_addr.ports[i].input_port,
2039 txmsg->reply.u.link_addr.ports[i].peer_device_type,
2040 txmsg->reply.u.link_addr.ports[i].port_number,
2041 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
2042 txmsg->reply.u.link_addr.ports[i].mcs,
2043 txmsg->reply.u.link_addr.ports[i].ddps,
2044 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
2045 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
2046 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
2047 }
Hersen Wu5e93b822016-01-22 17:07:28 -05002048
2049 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
2050
Dave Airliead7f8a12014-06-05 14:01:32 +10002051 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
2052 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
2053 }
Daniel Vetter16bff572018-11-28 23:12:34 +01002054 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airliead7f8a12014-06-05 14:01:32 +10002055 }
Dave Airlie68d8c9f2015-09-06 18:53:00 +10002056 } else {
2057 mstb->link_address_sent = false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002058 DRM_DEBUG_KMS("link address failed %d\n", ret);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10002059 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002060
2061 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10002062}
2063
2064static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2065 struct drm_dp_mst_branch *mstb,
2066 struct drm_dp_mst_port *port)
2067{
2068 int len;
2069 struct drm_dp_sideband_msg_tx *txmsg;
2070 int ret;
2071
2072 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2073 if (!txmsg)
2074 return -ENOMEM;
2075
2076 txmsg->dst = mstb;
2077 len = build_enum_path_resources(txmsg, port->port_num);
2078
2079 drm_dp_queue_down_tx(mgr, txmsg);
2080
2081 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2082 if (ret > 0) {
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002083 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002084 DRM_DEBUG_KMS("enum path resources nak received\n");
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002085 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10002086 if (port->port_num != txmsg->reply.u.path_resources.port_number)
2087 DRM_ERROR("got incorrect port in response\n");
2088 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,
2089 txmsg->reply.u.path_resources.avail_payload_bw_number);
2090 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
2091 }
2092 }
2093
2094 kfree(txmsg);
2095 return 0;
2096}
2097
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002098static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2099{
2100 if (!mstb->port_parent)
2101 return NULL;
2102
2103 if (mstb->port_parent->mstb != mstb)
2104 return mstb->port_parent;
2105
2106 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2107}
2108
Lyude Paul56d1c142019-01-10 19:53:30 -05002109/*
2110 * Searches upwards in the topology starting from mstb to try to find the
2111 * closest available parent of mstb that's still connected to the rest of the
2112 * topology. This can be used in order to perform operations like releasing
2113 * payloads, where the branch device which owned the payload may no longer be
2114 * around and thus would require that the payload on the last living relative
2115 * be freed instead.
2116 */
2117static struct drm_dp_mst_branch *
2118drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2119 struct drm_dp_mst_branch *mstb,
2120 int *port_num)
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002121{
2122 struct drm_dp_mst_branch *rmstb = NULL;
2123 struct drm_dp_mst_port *found_port;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002124
Lyude Paul56d1c142019-01-10 19:53:30 -05002125 mutex_lock(&mgr->lock);
2126 if (!mgr->mst_primary)
2127 goto out;
2128
2129 do {
2130 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
2131 if (!found_port)
2132 break;
2133
2134 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002135 rmstb = found_port->parent;
Lyude Paul56d1c142019-01-10 19:53:30 -05002136 *port_num = found_port->port_num;
2137 } else {
2138 /* Search again, starting from this parent */
2139 mstb = found_port->parent;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002140 }
Lyude Paul56d1c142019-01-10 19:53:30 -05002141 } while (!rmstb);
2142out:
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002143 mutex_unlock(&mgr->lock);
2144 return rmstb;
2145}
2146
Thierry Reding8fa6a422014-07-21 13:23:57 +02002147static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
2148 struct drm_dp_mst_port *port,
2149 int id,
2150 int pbn)
Dave Airliead7f8a12014-06-05 14:01:32 +10002151{
2152 struct drm_dp_sideband_msg_tx *txmsg;
2153 struct drm_dp_mst_branch *mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002154 int len, ret, port_num;
Libin Yangef8f9be2015-12-02 14:09:43 +08002155 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
2156 int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10002157
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002158 port_num = port->port_num;
Lyude Pauld0757af2019-01-10 19:53:28 -05002159 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002160 if (!mstb) {
Lyude Paulde6d6812019-01-10 19:53:25 -05002161 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
2162 port->parent,
2163 &port_num);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002164
Lyude Paulcfe9f902019-01-10 19:53:32 -05002165 if (!mstb)
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002166 return -EINVAL;
2167 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002168
2169 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2170 if (!txmsg) {
2171 ret = -ENOMEM;
2172 goto fail_put;
2173 }
2174
Libin Yangef8f9be2015-12-02 14:09:43 +08002175 for (i = 0; i < port->num_sdp_streams; i++)
2176 sinks[i] = i;
2177
Dave Airliead7f8a12014-06-05 14:01:32 +10002178 txmsg->dst = mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002179 len = build_allocate_payload(txmsg, port_num,
Dave Airliead7f8a12014-06-05 14:01:32 +10002180 id,
Libin Yangef8f9be2015-12-02 14:09:43 +08002181 pbn, port->num_sdp_streams, sinks);
Dave Airliead7f8a12014-06-05 14:01:32 +10002182
2183 drm_dp_queue_down_tx(mgr, txmsg);
2184
Lyude Paul56d1c142019-01-10 19:53:30 -05002185 /*
2186 * FIXME: there is a small chance that between getting the last
2187 * connected mstb and sending the payload message, the last connected
2188 * mstb could also be removed from the topology. In the future, this
2189 * needs to be fixed by restarting the
2190 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
2191 * timeout if the topology is still connected to the system.
2192 */
Dave Airliead7f8a12014-06-05 14:01:32 +10002193 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2194 if (ret > 0) {
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002195 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
Dave Airliead7f8a12014-06-05 14:01:32 +10002196 ret = -EINVAL;
Lyude Paulde6d6812019-01-10 19:53:25 -05002197 else
Dave Airliead7f8a12014-06-05 14:01:32 +10002198 ret = 0;
2199 }
2200 kfree(txmsg);
2201fail_put:
Lyude Pauld0757af2019-01-10 19:53:28 -05002202 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002203 return ret;
2204}
2205
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002206int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
2207 struct drm_dp_mst_port *port, bool power_up)
2208{
2209 struct drm_dp_sideband_msg_tx *txmsg;
2210 int len, ret;
2211
Lyude Pauld0757af2019-01-10 19:53:28 -05002212 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002213 if (!port)
2214 return -EINVAL;
2215
2216 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2217 if (!txmsg) {
Lyude Pauld0757af2019-01-10 19:53:28 -05002218 drm_dp_mst_topology_put_port(port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002219 return -ENOMEM;
2220 }
2221
2222 txmsg->dst = port->parent;
2223 len = build_power_updown_phy(txmsg, port->port_num, power_up);
2224 drm_dp_queue_down_tx(mgr, txmsg);
2225
2226 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
2227 if (ret > 0) {
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002228 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002229 ret = -EINVAL;
2230 else
2231 ret = 0;
2232 }
2233 kfree(txmsg);
Lyude Pauld0757af2019-01-10 19:53:28 -05002234 drm_dp_mst_topology_put_port(port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002235
2236 return ret;
2237}
2238EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
2239
Dave Airliead7f8a12014-06-05 14:01:32 +10002240static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2241 int id,
2242 struct drm_dp_payload *payload)
2243{
2244 int ret;
2245
2246 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
2247 if (ret < 0) {
2248 payload->payload_state = 0;
2249 return ret;
2250 }
2251 payload->payload_state = DP_PAYLOAD_LOCAL;
2252 return 0;
2253}
2254
Thierry Reding8fa6a422014-07-21 13:23:57 +02002255static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2256 struct drm_dp_mst_port *port,
2257 int id,
2258 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10002259{
2260 int ret;
2261 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
2262 if (ret < 0)
2263 return ret;
2264 payload->payload_state = DP_PAYLOAD_REMOTE;
2265 return ret;
2266}
2267
Thierry Reding8fa6a422014-07-21 13:23:57 +02002268static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2269 struct drm_dp_mst_port *port,
2270 int id,
2271 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10002272{
2273 DRM_DEBUG_KMS("\n");
Matt Roper1e55a532019-02-01 17:23:26 -08002274 /* it's okay for these to fail */
Dave Airliead7f8a12014-06-05 14:01:32 +10002275 if (port) {
2276 drm_dp_payload_send_msg(mgr, port, id, 0);
2277 }
2278
2279 drm_dp_dpcd_write_payload(mgr, id, payload);
Dave Airliedfda0df2014-08-06 16:26:21 +10002280 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002281 return 0;
2282}
2283
Thierry Reding8fa6a422014-07-21 13:23:57 +02002284static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2285 int id,
2286 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10002287{
2288 payload->payload_state = 0;
2289 return 0;
2290}
2291
2292/**
2293 * drm_dp_update_payload_part1() - Execute payload update part 1
2294 * @mgr: manager to use.
2295 *
2296 * This iterates over all proposed virtual channels, and tries to
2297 * allocate space in the link for them. For 0->slots transitions,
2298 * this step just writes the VCPI to the MST device. For slots->0
2299 * transitions, this writes the updated VCPIs and removes the
2300 * remote VC payloads.
2301 *
2302 * after calling this the driver should generate ACT and payload
2303 * packets.
2304 */
2305int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
2306{
Dave Airliead7f8a12014-06-05 14:01:32 +10002307 struct drm_dp_payload req_payload;
2308 struct drm_dp_mst_port *port;
Lyude Paulcfe9f902019-01-10 19:53:32 -05002309 int i, j;
2310 int cur_slots = 1;
Dave Airliead7f8a12014-06-05 14:01:32 +10002311
2312 mutex_lock(&mgr->payload_lock);
2313 for (i = 0; i < mgr->max_payloads; i++) {
Lyude Paul706246c2018-12-13 20:25:31 -05002314 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2315 struct drm_dp_payload *payload = &mgr->payloads[i];
Lyude Paulcfe9f902019-01-10 19:53:32 -05002316 bool put_port = false;
Lyude Paul706246c2018-12-13 20:25:31 -05002317
Dave Airliead7f8a12014-06-05 14:01:32 +10002318 /* solve the current payloads - compare to the hw ones
2319 - update the hw view */
2320 req_payload.start_slot = cur_slots;
Lyude Paul706246c2018-12-13 20:25:31 -05002321 if (vcpi) {
2322 port = container_of(vcpi, struct drm_dp_mst_port,
2323 vcpi);
Lyude Paulcfe9f902019-01-10 19:53:32 -05002324
2325 /* Validated ports don't matter if we're releasing
2326 * VCPI
2327 */
2328 if (vcpi->num_slots) {
2329 port = drm_dp_mst_topology_get_port_validated(
2330 mgr, port);
2331 if (!port) {
2332 mutex_unlock(&mgr->payload_lock);
2333 return -EINVAL;
2334 }
2335 put_port = true;
cpaul@redhat.com263efde2016-04-22 16:08:46 -04002336 }
Lyude Paulcfe9f902019-01-10 19:53:32 -05002337
Lyude Paul706246c2018-12-13 20:25:31 -05002338 req_payload.num_slots = vcpi->num_slots;
2339 req_payload.vcpi = vcpi->vcpi;
Dave Airliead7f8a12014-06-05 14:01:32 +10002340 } else {
2341 port = NULL;
2342 req_payload.num_slots = 0;
2343 }
Dave Airliedfda0df2014-08-06 16:26:21 +10002344
Lyude Paul706246c2018-12-13 20:25:31 -05002345 payload->start_slot = req_payload.start_slot;
Dave Airliead7f8a12014-06-05 14:01:32 +10002346 /* work out what is required to happen with this payload */
Lyude Paul706246c2018-12-13 20:25:31 -05002347 if (payload->num_slots != req_payload.num_slots) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002348
2349 /* need to push an update for this payload */
2350 if (req_payload.num_slots) {
Lyude Paul706246c2018-12-13 20:25:31 -05002351 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
2352 &req_payload);
2353 payload->num_slots = req_payload.num_slots;
2354 payload->vcpi = req_payload.vcpi;
2355
2356 } else if (payload->num_slots) {
2357 payload->num_slots = 0;
2358 drm_dp_destroy_payload_step1(mgr, port,
2359 payload->vcpi,
2360 payload);
2361 req_payload.payload_state =
2362 payload->payload_state;
2363 payload->start_slot = 0;
Dave Airliedfda0df2014-08-06 16:26:21 +10002364 }
Lyude Paul706246c2018-12-13 20:25:31 -05002365 payload->payload_state = req_payload.payload_state;
Dave Airliead7f8a12014-06-05 14:01:32 +10002366 }
2367 cur_slots += req_payload.num_slots;
cpaul@redhat.com263efde2016-04-22 16:08:46 -04002368
Lyude Paulcfe9f902019-01-10 19:53:32 -05002369 if (put_port)
Lyude Pauld0757af2019-01-10 19:53:28 -05002370 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002371 }
Dave Airliedfda0df2014-08-06 16:26:21 +10002372
2373 for (i = 0; i < mgr->max_payloads; i++) {
Lyude Paul706246c2018-12-13 20:25:31 -05002374 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL)
2375 continue;
Dave Airliedfda0df2014-08-06 16:26:21 +10002376
Lyude Paul706246c2018-12-13 20:25:31 -05002377 DRM_DEBUG_KMS("removing payload %d\n", i);
2378 for (j = i; j < mgr->max_payloads - 1; j++) {
2379 mgr->payloads[j] = mgr->payloads[j + 1];
2380 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
2381
2382 if (mgr->proposed_vcpis[j] &&
2383 mgr->proposed_vcpis[j]->num_slots) {
2384 set_bit(j + 1, &mgr->payload_mask);
2385 } else {
2386 clear_bit(j + 1, &mgr->payload_mask);
2387 }
Dave Airliedfda0df2014-08-06 16:26:21 +10002388 }
Lyude Paul706246c2018-12-13 20:25:31 -05002389
2390 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
2391 sizeof(struct drm_dp_payload));
2392 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
2393 clear_bit(mgr->max_payloads, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10002394 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002395 mutex_unlock(&mgr->payload_lock);
2396
2397 return 0;
2398}
2399EXPORT_SYMBOL(drm_dp_update_payload_part1);
2400
2401/**
2402 * drm_dp_update_payload_part2() - Execute payload update part 2
2403 * @mgr: manager to use.
2404 *
2405 * This iterates over all proposed virtual channels, and tries to
2406 * allocate space in the link for them. For 0->slots transitions,
2407 * this step writes the remote VC payload commands. For slots->0
2408 * this just resets some internal state.
2409 */
2410int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
2411{
2412 struct drm_dp_mst_port *port;
2413 int i;
Damien Lespiau7389ad42014-07-14 11:53:44 +01002414 int ret = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002415 mutex_lock(&mgr->payload_lock);
2416 for (i = 0; i < mgr->max_payloads; i++) {
2417
2418 if (!mgr->proposed_vcpis[i])
2419 continue;
2420
2421 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2422
2423 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2424 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10002425 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10002426 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10002427 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10002428 }
2429 if (ret) {
2430 mutex_unlock(&mgr->payload_lock);
2431 return ret;
2432 }
2433 }
2434 mutex_unlock(&mgr->payload_lock);
2435 return 0;
2436}
2437EXPORT_SYMBOL(drm_dp_update_payload_part2);
2438
2439#if 0 /* unused as of yet */
2440static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2441 struct drm_dp_mst_port *port,
2442 int offset, int size)
2443{
2444 int len;
2445 struct drm_dp_sideband_msg_tx *txmsg;
2446
2447 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2448 if (!txmsg)
2449 return -ENOMEM;
2450
2451 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
2452 txmsg->dst = port->parent;
2453
2454 drm_dp_queue_down_tx(mgr, txmsg);
2455
2456 return 0;
2457}
2458#endif
2459
2460static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2461 struct drm_dp_mst_port *port,
2462 int offset, int size, u8 *bytes)
2463{
2464 int len;
2465 int ret;
2466 struct drm_dp_sideband_msg_tx *txmsg;
2467 struct drm_dp_mst_branch *mstb;
2468
Lyude Pauld0757af2019-01-10 19:53:28 -05002469 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Dave Airliead7f8a12014-06-05 14:01:32 +10002470 if (!mstb)
2471 return -EINVAL;
2472
2473 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2474 if (!txmsg) {
2475 ret = -ENOMEM;
2476 goto fail_put;
2477 }
2478
2479 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2480 txmsg->dst = mstb;
2481
2482 drm_dp_queue_down_tx(mgr, txmsg);
2483
2484 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2485 if (ret > 0) {
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002486 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
Dave Airliead7f8a12014-06-05 14:01:32 +10002487 ret = -EINVAL;
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002488 else
Dave Airliead7f8a12014-06-05 14:01:32 +10002489 ret = 0;
2490 }
2491 kfree(txmsg);
2492fail_put:
Lyude Pauld0757af2019-01-10 19:53:28 -05002493 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002494 return ret;
2495}
2496
2497static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2498{
2499 struct drm_dp_sideband_msg_reply_body reply;
2500
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002501 reply.reply_type = DP_SIDEBAND_REPLY_ACK;
Dave Airliead7f8a12014-06-05 14:01:32 +10002502 reply.req_type = req_type;
2503 drm_dp_encode_sideband_reply(&reply, msg);
2504 return 0;
2505}
2506
2507static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2508 struct drm_dp_mst_branch *mstb,
2509 int req_type, int seqno, bool broadcast)
2510{
2511 struct drm_dp_sideband_msg_tx *txmsg;
2512
2513 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2514 if (!txmsg)
2515 return -ENOMEM;
2516
2517 txmsg->dst = mstb;
2518 txmsg->seqno = seqno;
2519 drm_dp_encode_up_ack_reply(txmsg, req_type);
2520
2521 mutex_lock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05002522
2523 process_single_up_tx_qlock(mgr, txmsg);
2524
Dave Airliead7f8a12014-06-05 14:01:32 +10002525 mutex_unlock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05002526
2527 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10002528 return 0;
2529}
2530
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002531static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2532 int dp_link_count,
2533 int *out)
Dave Airliead7f8a12014-06-05 14:01:32 +10002534{
2535 switch (dp_link_bw) {
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002536 default:
2537 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2538 dp_link_bw, dp_link_count);
2539 return false;
2540
Dave Airliead7f8a12014-06-05 14:01:32 +10002541 case DP_LINK_BW_1_62:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002542 *out = 3 * dp_link_count;
2543 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002544 case DP_LINK_BW_2_7:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002545 *out = 5 * dp_link_count;
2546 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002547 case DP_LINK_BW_5_4:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002548 *out = 10 * dp_link_count;
2549 break;
Manasi Navaree0bd8782018-01-22 14:43:10 -08002550 case DP_LINK_BW_8_1:
2551 *out = 15 * dp_link_count;
2552 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002553 }
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002554 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002555}
2556
2557/**
2558 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2559 * @mgr: manager to set state for
2560 * @mst_state: true to enable MST on this connector - false to disable.
2561 *
2562 * This is called by the driver when it detects an MST capable device plugged
2563 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2564 */
2565int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2566{
2567 int ret = 0;
2568 struct drm_dp_mst_branch *mstb = NULL;
2569
2570 mutex_lock(&mgr->lock);
2571 if (mst_state == mgr->mst_state)
2572 goto out_unlock;
2573
2574 mgr->mst_state = mst_state;
2575 /* set the device into MST mode */
2576 if (mst_state) {
2577 WARN_ON(mgr->mst_primary);
2578
2579 /* get dpcd info */
2580 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2581 if (ret != DP_RECEIVER_CAP_SIZE) {
2582 DRM_DEBUG_KMS("failed to read DPCD\n");
2583 goto out_unlock;
2584 }
2585
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002586 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2587 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2588 &mgr->pbn_div)) {
2589 ret = -EINVAL;
2590 goto out_unlock;
2591 }
2592
Dave Airliead7f8a12014-06-05 14:01:32 +10002593 /* add initial branch device at LCT 1 */
2594 mstb = drm_dp_add_mst_branch_device(1, NULL);
2595 if (mstb == NULL) {
2596 ret = -ENOMEM;
2597 goto out_unlock;
2598 }
2599 mstb->mgr = mgr;
2600
2601 /* give this the main reference */
2602 mgr->mst_primary = mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05002603 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
Dave Airliead7f8a12014-06-05 14:01:32 +10002604
Andrey Grodzovskyc175cd12016-01-22 17:07:29 -05002605 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2606 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2607 if (ret < 0) {
2608 goto out_unlock;
2609 }
2610
Dave Airliead7f8a12014-06-05 14:01:32 +10002611 {
2612 struct drm_dp_payload reset_pay;
2613 reset_pay.start_slot = 0;
2614 reset_pay.num_slots = 0x3f;
2615 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2616 }
2617
Dave Airliead7f8a12014-06-05 14:01:32 +10002618 queue_work(system_long_wq, &mgr->work);
2619
2620 ret = 0;
2621 } else {
2622 /* disable MST on the device */
2623 mstb = mgr->mst_primary;
2624 mgr->mst_primary = NULL;
2625 /* this can fail if the device is gone */
2626 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2627 ret = 0;
2628 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2629 mgr->payload_mask = 0;
2630 set_bit(0, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10002631 mgr->vcpi_mask = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002632 }
2633
2634out_unlock:
2635 mutex_unlock(&mgr->lock);
2636 if (mstb)
Lyude Pauld0757af2019-01-10 19:53:28 -05002637 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002638 return ret;
2639
2640}
2641EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2642
2643/**
2644 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2645 * @mgr: manager to suspend
2646 *
2647 * This function tells the MST device that we can't handle UP messages
2648 * anymore. This should stop it from sending any since we are suspended.
2649 */
2650void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2651{
2652 mutex_lock(&mgr->lock);
2653 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2654 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2655 mutex_unlock(&mgr->lock);
Dave Airlie274d8352015-09-30 10:39:42 +10002656 flush_work(&mgr->work);
2657 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002658}
2659EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2660
2661/**
2662 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2663 * @mgr: manager to resume
2664 *
2665 * This will fetch DPCD and see if the device is still there,
2666 * if it is, it will rewrite the MSTM control bits, and return.
2667 *
2668 * if the device fails this returns -1, and the driver should do
2669 * a full MST reprobe, in case we were undocked.
2670 */
2671int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2672{
2673 int ret = 0;
2674
2675 mutex_lock(&mgr->lock);
2676
2677 if (mgr->mst_primary) {
2678 int sret;
Lyude1652fce2016-04-13 16:50:18 -04002679 u8 guid[16];
2680
Dave Airliead7f8a12014-06-05 14:01:32 +10002681 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2682 if (sret != DP_RECEIVER_CAP_SIZE) {
2683 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2684 ret = -1;
2685 goto out_unlock;
2686 }
2687
2688 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2689 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2690 if (ret < 0) {
2691 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2692 ret = -1;
2693 goto out_unlock;
2694 }
Lyude1652fce2016-04-13 16:50:18 -04002695
2696 /* Some hubs forget their guids after they resume */
2697 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2698 if (sret != 16) {
2699 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2700 ret = -1;
2701 goto out_unlock;
2702 }
2703 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2704
Dave Airliead7f8a12014-06-05 14:01:32 +10002705 ret = 0;
2706 } else
2707 ret = -1;
2708
2709out_unlock:
2710 mutex_unlock(&mgr->lock);
2711 return ret;
2712}
2713EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2714
Imre Deak636c4c32017-07-19 16:46:32 +03002715static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
Dave Airliead7f8a12014-06-05 14:01:32 +10002716{
2717 int len;
2718 u8 replyblock[32];
2719 int replylen, origlen, curreply;
2720 int ret;
2721 struct drm_dp_sideband_msg_rx *msg;
2722 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2723 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2724
2725 len = min(mgr->max_dpcd_transaction_bytes, 16);
2726 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2727 replyblock, len);
2728 if (ret != len) {
2729 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
Imre Deak636c4c32017-07-19 16:46:32 +03002730 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002731 }
2732 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2733 if (!ret) {
2734 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
Imre Deak636c4c32017-07-19 16:46:32 +03002735 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002736 }
2737 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2738
2739 origlen = replylen;
2740 replylen -= len;
2741 curreply = len;
2742 while (replylen > 0) {
2743 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2744 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2745 replyblock, len);
2746 if (ret != len) {
Imre Deak448421b2017-07-19 14:43:28 +03002747 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2748 len, ret);
Imre Deak636c4c32017-07-19 16:46:32 +03002749 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002750 }
Imre Deak448421b2017-07-19 14:43:28 +03002751
Dave Airliead7f8a12014-06-05 14:01:32 +10002752 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
Imre Deak448421b2017-07-19 14:43:28 +03002753 if (!ret) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002754 DRM_DEBUG_KMS("failed to build sideband msg\n");
Imre Deak636c4c32017-07-19 16:46:32 +03002755 return false;
Imre Deak448421b2017-07-19 14:43:28 +03002756 }
2757
Dave Airliead7f8a12014-06-05 14:01:32 +10002758 curreply += len;
2759 replylen -= len;
2760 }
Imre Deak636c4c32017-07-19 16:46:32 +03002761 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002762}
2763
2764static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2765{
2766 int ret = 0;
2767
Imre Deak636c4c32017-07-19 16:46:32 +03002768 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2769 memset(&mgr->down_rep_recv, 0,
2770 sizeof(struct drm_dp_sideband_msg_rx));
2771 return 0;
2772 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002773
2774 if (mgr->down_rep_recv.have_eomt) {
2775 struct drm_dp_sideband_msg_tx *txmsg;
2776 struct drm_dp_mst_branch *mstb;
2777 int slot = -1;
2778 mstb = drm_dp_get_mst_branch_device(mgr,
2779 mgr->down_rep_recv.initial_hdr.lct,
2780 mgr->down_rep_recv.initial_hdr.rad);
2781
2782 if (!mstb) {
2783 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2784 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2785 return 0;
2786 }
2787
2788 /* find the message */
2789 slot = mgr->down_rep_recv.initial_hdr.seqno;
2790 mutex_lock(&mgr->qlock);
2791 txmsg = mstb->tx_slots[slot];
2792 /* remove from slots */
2793 mutex_unlock(&mgr->qlock);
2794
2795 if (!txmsg) {
2796 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2797 mstb,
2798 mgr->down_rep_recv.initial_hdr.seqno,
2799 mgr->down_rep_recv.initial_hdr.lct,
2800 mgr->down_rep_recv.initial_hdr.rad[0],
2801 mgr->down_rep_recv.msg[0]);
Lyude Pauld0757af2019-01-10 19:53:28 -05002802 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002803 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2804 return 0;
2805 }
2806
2807 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
Ville Syrjälä45bbda12019-01-22 22:03:00 +02002808
2809 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
Ville Syrjälä3dadbd22019-01-22 22:03:01 +02002810 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
2811 txmsg->reply.req_type,
2812 drm_dp_mst_req_type_str(txmsg->reply.req_type),
2813 txmsg->reply.u.nak.reason,
2814 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
2815 txmsg->reply.u.nak.nak_data);
Dave Airliead7f8a12014-06-05 14:01:32 +10002816
2817 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
Lyude Pauld0757af2019-01-10 19:53:28 -05002818 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002819
2820 mutex_lock(&mgr->qlock);
2821 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2822 mstb->tx_slots[slot] = NULL;
2823 mutex_unlock(&mgr->qlock);
2824
Chris Wilson68e989d2017-05-13 11:52:01 +01002825 wake_up_all(&mgr->tx_waitq);
Dave Airliead7f8a12014-06-05 14:01:32 +10002826 }
2827 return ret;
2828}
2829
2830static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2831{
2832 int ret = 0;
Imre Deak636c4c32017-07-19 16:46:32 +03002833
2834 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2835 memset(&mgr->up_req_recv, 0,
2836 sizeof(struct drm_dp_sideband_msg_rx));
2837 return 0;
2838 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002839
2840 if (mgr->up_req_recv.have_eomt) {
2841 struct drm_dp_sideband_msg_req_body msg;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002842 struct drm_dp_mst_branch *mstb = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002843 bool seqno;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002844
2845 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2846 mstb = drm_dp_get_mst_branch_device(mgr,
2847 mgr->up_req_recv.initial_hdr.lct,
2848 mgr->up_req_recv.initial_hdr.rad);
2849 if (!mstb) {
2850 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2851 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2852 return 0;
2853 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002854 }
2855
2856 seqno = mgr->up_req_recv.initial_hdr.seqno;
2857 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2858
2859 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002860 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2861
2862 if (!mstb)
2863 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2864
2865 if (!mstb) {
2866 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2867 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2868 return 0;
2869 }
2870
Dave Airliead7f8a12014-06-05 14:01:32 +10002871 drm_dp_update_port(mstb, &msg.u.conn_stat);
Hersen Wu5e93b822016-01-22 17:07:28 -05002872
Dave Airliead7f8a12014-06-05 14:01:32 +10002873 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);
Daniel Vetter16bff572018-11-28 23:12:34 +01002874 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002875
Dave Airliead7f8a12014-06-05 14:01:32 +10002876 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002877 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2878 if (!mstb)
2879 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2880
2881 if (!mstb) {
2882 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2883 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2884 return 0;
2885 }
2886
Dave Airliead7f8a12014-06-05 14:01:32 +10002887 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2888 }
2889
Imre Deak7f8b3982017-07-19 14:43:29 +03002890 if (mstb)
Lyude Pauld0757af2019-01-10 19:53:28 -05002891 drm_dp_mst_topology_put_mstb(mstb);
Imre Deak7f8b3982017-07-19 14:43:29 +03002892
Dave Airliead7f8a12014-06-05 14:01:32 +10002893 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2894 }
2895 return ret;
2896}
2897
2898/**
2899 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2900 * @mgr: manager to notify irq for.
2901 * @esi: 4 bytes from SINK_COUNT_ESI
Daniel Vetter295ee852014-07-30 14:23:44 +02002902 * @handled: whether the hpd interrupt was consumed or not
Dave Airliead7f8a12014-06-05 14:01:32 +10002903 *
2904 * This should be called from the driver when it detects a short IRQ,
2905 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2906 * topology manager will process the sideband messages received as a result
2907 * of this.
2908 */
2909int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2910{
2911 int ret = 0;
2912 int sc;
2913 *handled = false;
2914 sc = esi[0] & 0x3f;
2915
2916 if (sc != mgr->sink_count) {
2917 mgr->sink_count = sc;
2918 *handled = true;
2919 }
2920
2921 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2922 ret = drm_dp_mst_handle_down_rep(mgr);
2923 *handled = true;
2924 }
2925
2926 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2927 ret |= drm_dp_mst_handle_up_req(mgr);
2928 *handled = true;
2929 }
2930
2931 drm_dp_mst_kick_tx(mgr);
2932 return ret;
2933}
2934EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2935
2936/**
2937 * drm_dp_mst_detect_port() - get connection status for an MST port
Daniel Vetter132d49d2016-07-15 21:48:04 +02002938 * @connector: DRM connector for this port
Dave Airliead7f8a12014-06-05 14:01:32 +10002939 * @mgr: manager for this port
2940 * @port: unverified pointer to a port
2941 *
2942 * This returns the current connection state for a port. It validates the
2943 * port pointer still exists so the caller doesn't require a reference
2944 */
Dave Airliec6a0aed2014-10-20 16:28:02 +10002945enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2946 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10002947{
2948 enum drm_connector_status status = connector_status_disconnected;
2949
Matt Roper1e55a532019-02-01 17:23:26 -08002950 /* we need to search for the port in the mgr in case it's gone */
Lyude Pauld0757af2019-01-10 19:53:28 -05002951 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002952 if (!port)
2953 return connector_status_disconnected;
2954
2955 if (!port->ddps)
2956 goto out;
2957
2958 switch (port->pdt) {
2959 case DP_PEER_DEVICE_NONE:
2960 case DP_PEER_DEVICE_MST_BRANCHING:
2961 break;
2962
2963 case DP_PEER_DEVICE_SST_SINK:
2964 status = connector_status_connected;
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002965 /* for logical ports - cache the EDID */
2966 if (port->port_num >= 8 && !port->cached_edid) {
2967 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2968 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002969 break;
2970 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2971 if (port->ldps)
2972 status = connector_status_connected;
2973 break;
2974 }
2975out:
Lyude Pauld0757af2019-01-10 19:53:28 -05002976 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002977 return status;
2978}
2979EXPORT_SYMBOL(drm_dp_mst_detect_port);
2980
2981/**
Libin Yangef8f9be2015-12-02 14:09:43 +08002982 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2983 * @mgr: manager for this port
2984 * @port: unverified pointer to a port.
2985 *
2986 * This returns whether the port supports audio or not.
2987 */
2988bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2989 struct drm_dp_mst_port *port)
2990{
2991 bool ret = false;
2992
Lyude Pauld0757af2019-01-10 19:53:28 -05002993 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Libin Yangef8f9be2015-12-02 14:09:43 +08002994 if (!port)
2995 return ret;
2996 ret = port->has_audio;
Lyude Pauld0757af2019-01-10 19:53:28 -05002997 drm_dp_mst_topology_put_port(port);
Libin Yangef8f9be2015-12-02 14:09:43 +08002998 return ret;
2999}
3000EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
3001
3002/**
Dave Airliead7f8a12014-06-05 14:01:32 +10003003 * drm_dp_mst_get_edid() - get EDID for an MST port
3004 * @connector: toplevel connector to get EDID for
3005 * @mgr: manager for this port
3006 * @port: unverified pointer to a port.
3007 *
3008 * This returns an EDID for the port connected to a connector,
3009 * It validates the pointer still exists so the caller doesn't require a
3010 * reference.
3011 */
3012struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3013{
3014 struct edid *edid = NULL;
3015
Matt Roper1e55a532019-02-01 17:23:26 -08003016 /* we need to search for the port in the mgr in case it's gone */
Lyude Pauld0757af2019-01-10 19:53:28 -05003017 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003018 if (!port)
3019 return NULL;
3020
Dave Airliec6a0aed2014-10-20 16:28:02 +10003021 if (port->cached_edid)
3022 edid = drm_edid_duplicate(port->cached_edid);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10003023 else {
3024 edid = drm_get_edid(connector, &port->aux.ddc);
Daniel Vetter97e14fb2018-07-09 10:40:08 +02003025 drm_connector_set_tile_property(connector);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10003026 }
Libin Yangef8f9be2015-12-02 14:09:43 +08003027 port->has_audio = drm_detect_monitor_audio(edid);
Lyude Pauld0757af2019-01-10 19:53:28 -05003028 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003029 return edid;
3030}
3031EXPORT_SYMBOL(drm_dp_mst_get_edid);
3032
3033/**
Lyude Paule4b0c862018-10-23 19:12:46 -04003034 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
Dave Airliead7f8a12014-06-05 14:01:32 +10003035 * @mgr: manager to use
3036 * @pbn: payload bandwidth to convert into slots.
Lyude Paule4b0c862018-10-23 19:12:46 -04003037 *
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.
Dave Airliead7f8a12014-06-05 14:01:32 +10003044 */
3045int 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
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07003052 /* max. time slots - one slot for MTP header */
3053 if (num_slots > 63)
Dave Airliead7f8a12014-06-05 14:01:32 +10003054 return -ENOSPC;
3055 return num_slots;
3056}
3057EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
3058
3059static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003060 struct drm_dp_vcpi *vcpi, int pbn, int slots)
Dave Airliead7f8a12014-06-05 14:01:32 +10003061{
Dave Airliead7f8a12014-06-05 14:01:32 +10003062 int ret;
3063
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07003064 /* max. time slots - one slot for MTP header */
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003065 if (slots > 63)
Dave Airliead7f8a12014-06-05 14:01:32 +10003066 return -ENOSPC;
3067
3068 vcpi->pbn = pbn;
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003069 vcpi->aligned_pbn = slots * mgr->pbn_div;
3070 vcpi->num_slots = slots;
Dave Airliead7f8a12014-06-05 14:01:32 +10003071
3072 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
3073 if (ret < 0)
3074 return ret;
3075 return 0;
3076}
3077
3078/**
Lyude Pauleceae142019-01-10 19:53:41 -05003079 * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003080 * @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 *
Lyude Pauleceae142019-01-10 19:53:41 -05003085 * 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
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003107 */
3108int 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;
Lyude Pauleceae142019-01-10 19:53:41 -05003113 struct drm_dp_vcpi_allocation *pos, *vcpi = NULL;
3114 int prev_slots, req_slots, ret;
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003115
3116 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
Ville Syrjälä56a91c42017-07-12 18:51:00 +03003117 if (IS_ERR(topology_state))
3118 return PTR_ERR(topology_state);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003119
Lyude Pauld0757af2019-01-10 19:53:28 -05003120 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003121 if (port == NULL)
3122 return -EINVAL;
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003123
Lyude Pauleceae142019-01-10 19:53:41 -05003124 /* Find the current allocation for this port, if any */
3125 list_for_each_entry(pos, &topology_state->vcpis, next) {
3126 if (pos->port == port) {
3127 vcpi = pos;
3128 prev_slots = vcpi->vcpi;
3129
3130 /*
3131 * This should never happen, unless the driver tries
3132 * releasing and allocating the same VCPI allocation,
3133 * which is an error
3134 */
3135 if (WARN_ON(!prev_slots)) {
3136 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
3137 port);
3138 return -EINVAL;
3139 }
3140
3141 break;
3142 }
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003143 }
Lyude Pauleceae142019-01-10 19:53:41 -05003144 if (!vcpi)
3145 prev_slots = 0;
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003146
Lyude Pauleceae142019-01-10 19:53:41 -05003147 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003148
Lyude Pauleceae142019-01-10 19:53:41 -05003149 DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
3150 port->connector->base.id, port->connector->name,
3151 port, prev_slots, req_slots);
3152
3153 /* Add the new allocation to the state */
3154 if (!vcpi) {
3155 vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL);
3156 if (!vcpi) {
3157 ret = -ENOMEM;
3158 goto out;
3159 }
3160
3161 drm_dp_mst_get_port_malloc(port);
3162 vcpi->port = port;
3163 list_add(&vcpi->next, &topology_state->vcpis);
3164 }
3165 vcpi->vcpi = req_slots;
3166
3167 ret = req_slots;
3168out:
Lyude Pauld0757af2019-01-10 19:53:28 -05003169 drm_dp_mst_topology_put_port(port);
Lyude Pauleceae142019-01-10 19:53:41 -05003170 return ret;
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003171}
3172EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
3173
3174/**
3175 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
3176 * @state: global atomic state
3177 * @mgr: MST topology manager for the port
Lyude Pauleceae142019-01-10 19:53:41 -05003178 * @port: The port to release the VCPI slots from
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003179 *
Lyude Pauleceae142019-01-10 19:53:41 -05003180 * Releases any VCPI slots that have been allocated to a port in the atomic
3181 * state. Any atomic drivers which support MST must call this function in
3182 * their &drm_connector_helper_funcs.atomic_check() callback when the
Matt Roper1e55a532019-02-01 17:23:26 -08003183 * connector will no longer have VCPI allocated (e.g. because its CRTC was
Lyude Pauleceae142019-01-10 19:53:41 -05003184 * removed) when it had VCPI allocated in the previous atomic state.
3185 *
3186 * It is OK to call this even if @port has been removed from the system.
3187 * Additionally, it is OK to call this function multiple times on the same
3188 * @port as needed. It is not OK however, to call this function and
3189 * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
3190 * phase.
3191 *
3192 * See also:
3193 * drm_dp_atomic_find_vcpi_slots()
3194 * drm_dp_mst_atomic_check()
3195 *
3196 * Returns:
3197 * 0 if all slots for this port were added back to
3198 * &drm_dp_mst_topology_state.avail_slots or negative error code
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003199 */
3200int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
3201 struct drm_dp_mst_topology_mgr *mgr,
Lyude Pauleceae142019-01-10 19:53:41 -05003202 struct drm_dp_mst_port *port)
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003203{
3204 struct drm_dp_mst_topology_state *topology_state;
Lyude Pauleceae142019-01-10 19:53:41 -05003205 struct drm_dp_vcpi_allocation *pos;
3206 bool found = false;
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003207
3208 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
Ville Syrjälä56a91c42017-07-12 18:51:00 +03003209 if (IS_ERR(topology_state))
3210 return PTR_ERR(topology_state);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003211
Lyude Pauleceae142019-01-10 19:53:41 -05003212 list_for_each_entry(pos, &topology_state->vcpis, next) {
3213 if (pos->port == port) {
3214 found = true;
3215 break;
3216 }
3217 }
3218 if (WARN_ON(!found)) {
3219 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
3220 port, &topology_state->base);
3221 return -EINVAL;
3222 }
3223
3224 DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi);
3225 if (pos->vcpi) {
3226 drm_dp_mst_put_port_malloc(port);
3227 pos->vcpi = 0;
3228 }
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003229
3230 return 0;
3231}
3232EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
3233
3234/**
Dave Airliead7f8a12014-06-05 14:01:32 +10003235 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
3236 * @mgr: manager for this port
3237 * @port: port to allocate a virtual channel for.
3238 * @pbn: payload bandwidth number to request
3239 * @slots: returned number of slots for this PBN.
3240 */
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003241bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3242 struct drm_dp_mst_port *port, int pbn, int slots)
Dave Airliead7f8a12014-06-05 14:01:32 +10003243{
3244 int ret;
3245
Lyude Pauld0757af2019-01-10 19:53:28 -05003246 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003247 if (!port)
3248 return false;
3249
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003250 if (slots < 0)
3251 return false;
3252
Dave Airliead7f8a12014-06-05 14:01:32 +10003253 if (port->vcpi.vcpi > 0) {
Lyude Paule0ac7112019-01-10 19:53:26 -05003254 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
3255 port->vcpi.vcpi, port->vcpi.pbn, pbn);
Dave Airliead7f8a12014-06-05 14:01:32 +10003256 if (pbn == port->vcpi.pbn) {
Lyude Pauld0757af2019-01-10 19:53:28 -05003257 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003258 return true;
3259 }
3260 }
3261
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003262 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
Dave Airliead7f8a12014-06-05 14:01:32 +10003263 if (ret) {
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07003264 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
Lyude Paule0ac7112019-01-10 19:53:26 -05003265 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10003266 goto out;
3267 }
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07003268 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
Lyude Paule0ac7112019-01-10 19:53:26 -05003269 pbn, port->vcpi.num_slots);
Dave Airliead7f8a12014-06-05 14:01:32 +10003270
Matt Roper1e55a532019-02-01 17:23:26 -08003271 /* Keep port allocated until its payload has been removed */
Lyude Paulcfe9f902019-01-10 19:53:32 -05003272 drm_dp_mst_get_port_malloc(port);
Lyude Pauld0757af2019-01-10 19:53:28 -05003273 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003274 return true;
3275out:
3276 return false;
3277}
3278EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
3279
Dave Airlie87f59422015-02-24 09:23:55 +10003280int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3281{
3282 int slots = 0;
Lyude Pauld0757af2019-01-10 19:53:28 -05003283 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airlie87f59422015-02-24 09:23:55 +10003284 if (!port)
3285 return slots;
3286
3287 slots = port->vcpi.num_slots;
Lyude Pauld0757af2019-01-10 19:53:28 -05003288 drm_dp_mst_topology_put_port(port);
Dave Airlie87f59422015-02-24 09:23:55 +10003289 return slots;
3290}
3291EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
3292
Dave Airliead7f8a12014-06-05 14:01:32 +10003293/**
3294 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
3295 * @mgr: manager for this port
3296 * @port: unverified pointer to a port.
3297 *
3298 * This just resets the number of slots for the ports VCPI for later programming.
3299 */
3300void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3301{
Lyude Paulcfe9f902019-01-10 19:53:32 -05003302 /*
Matt Roper1e55a532019-02-01 17:23:26 -08003303 * A port with VCPI will remain allocated until its VCPI is
Lyude Paulcfe9f902019-01-10 19:53:32 -05003304 * released, no verified ref needed
3305 */
3306
Dave Airliead7f8a12014-06-05 14:01:32 +10003307 port->vcpi.num_slots = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10003308}
3309EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
3310
3311/**
3312 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
3313 * @mgr: manager for this port
3314 * @port: unverified port to deallocate vcpi for
3315 */
Lyude Paul4afb8a22019-01-10 19:53:27 -05003316void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3317 struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10003318{
Lyude Paulcfe9f902019-01-10 19:53:32 -05003319 /*
Matt Roper1e55a532019-02-01 17:23:26 -08003320 * A port with VCPI will remain allocated until its VCPI is
Lyude Paulcfe9f902019-01-10 19:53:32 -05003321 * released, no verified ref needed
3322 */
Dave Airliead7f8a12014-06-05 14:01:32 +10003323
3324 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3325 port->vcpi.num_slots = 0;
3326 port->vcpi.pbn = 0;
3327 port->vcpi.aligned_pbn = 0;
3328 port->vcpi.vcpi = 0;
Lyude Paulcfe9f902019-01-10 19:53:32 -05003329 drm_dp_mst_put_port_malloc(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003330}
3331EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
3332
3333static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
3334 int id, struct drm_dp_payload *payload)
3335{
3336 u8 payload_alloc[3], status;
3337 int ret;
3338 int retries = 0;
3339
3340 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
3341 DP_PAYLOAD_TABLE_UPDATED);
3342
3343 payload_alloc[0] = id;
3344 payload_alloc[1] = payload->start_slot;
3345 payload_alloc[2] = payload->num_slots;
3346
3347 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
3348 if (ret != 3) {
3349 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
3350 goto fail;
3351 }
3352
3353retry:
3354 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3355 if (ret < 0) {
3356 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3357 goto fail;
3358 }
3359
3360 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
3361 retries++;
3362 if (retries < 20) {
3363 usleep_range(10000, 20000);
3364 goto retry;
3365 }
3366 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
3367 ret = -EINVAL;
3368 goto fail;
3369 }
3370 ret = 0;
3371fail:
3372 return ret;
3373}
3374
3375
3376/**
3377 * drm_dp_check_act_status() - Check ACT handled status.
3378 * @mgr: manager to use
3379 *
3380 * Check the payload status bits in the DPCD for ACT handled completion.
3381 */
3382int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
3383{
3384 u8 status;
3385 int ret;
3386 int count = 0;
3387
3388 do {
3389 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3390
3391 if (ret < 0) {
3392 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3393 goto fail;
3394 }
3395
3396 if (status & DP_PAYLOAD_ACT_HANDLED)
3397 break;
3398 count++;
3399 udelay(100);
3400
3401 } while (count < 30);
3402
3403 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
3404 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
3405 ret = -EINVAL;
3406 goto fail;
3407 }
3408 return 0;
3409fail:
3410 return ret;
3411}
3412EXPORT_SYMBOL(drm_dp_check_act_status);
3413
3414/**
3415 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
3416 * @clock: dot clock for the mode
3417 * @bpp: bpp for the mode.
3418 *
3419 * This uses the formula in the spec to calculate the PBN value for a mode.
3420 */
3421int drm_dp_calc_pbn_mode(int clock, int bpp)
3422{
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003423 u64 kbps;
3424 s64 peak_kbps;
3425 u32 numerator;
3426 u32 denominator;
Dave Airliead7f8a12014-06-05 14:01:32 +10003427
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003428 kbps = clock * bpp;
Dave Airliead7f8a12014-06-05 14:01:32 +10003429
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003430 /*
3431 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
3432 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
3433 * common multiplier to render an integer PBN for all link rate/lane
3434 * counts combinations
3435 * calculate
3436 * peak_kbps *= (1006/1000)
3437 * peak_kbps *= (64/54)
3438 * peak_kbps *= 8 convert to bytes
3439 */
Dave Airliead7f8a12014-06-05 14:01:32 +10003440
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003441 numerator = 64 * 1006;
3442 denominator = 54 * 8 * 1000 * 1000;
Dave Airliead7f8a12014-06-05 14:01:32 +10003443
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003444 kbps *= numerator;
3445 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
3446
3447 return drm_fixp2int_ceil(peak_kbps);
Dave Airliead7f8a12014-06-05 14:01:32 +10003448}
3449EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
3450
3451static int test_calc_pbn_mode(void)
3452{
3453 int ret;
3454 ret = drm_dp_calc_pbn_mode(154000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003455 if (ret != 689) {
3456 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3457 154000, 30, 689, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10003458 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003459 }
Dave Airliead7f8a12014-06-05 14:01:32 +10003460 ret = drm_dp_calc_pbn_mode(234000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003461 if (ret != 1047) {
3462 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3463 234000, 30, 1047, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10003464 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003465 }
3466 ret = drm_dp_calc_pbn_mode(297000, 24);
3467 if (ret != 1063) {
3468 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3469 297000, 24, 1063, ret);
3470 return -EINVAL;
3471 }
Dave Airliead7f8a12014-06-05 14:01:32 +10003472 return 0;
3473}
3474
3475/* we want to kick the TX after we've ack the up/down IRQs. */
3476static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
3477{
3478 queue_work(system_long_wq, &mgr->tx_work);
3479}
3480
3481static void drm_dp_mst_dump_mstb(struct seq_file *m,
3482 struct drm_dp_mst_branch *mstb)
3483{
3484 struct drm_dp_mst_port *port;
3485 int tabs = mstb->lct;
3486 char prefix[10];
3487 int i;
3488
3489 for (i = 0; i < tabs; i++)
3490 prefix[i] = '\t';
3491 prefix[i] = '\0';
3492
3493 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
3494 list_for_each_entry(port, &mstb->ports, next) {
Jim Bride51108f22016-04-14 10:18:36 -07003495 seq_printf(m, "%sport: %d: input: %d: pdt: %d, ddps: %d ldps: %d, sdp: %d/%d, %p, conn: %p\n", prefix, port->port_num, port->input, port->pdt, port->ddps, port->ldps, port->num_sdp_streams, port->num_sdp_stream_sinks, port, port->connector);
Dave Airliead7f8a12014-06-05 14:01:32 +10003496 if (port->mstb)
3497 drm_dp_mst_dump_mstb(m, port->mstb);
3498 }
3499}
3500
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003501#define DP_PAYLOAD_TABLE_SIZE 64
3502
Dave Airliead7f8a12014-06-05 14:01:32 +10003503static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
3504 char *buf)
3505{
Dave Airliead7f8a12014-06-05 14:01:32 +10003506 int i;
Joe Perches46466b02017-05-30 16:35:37 -07003507
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003508 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
Joe Perches46466b02017-05-30 16:35:37 -07003509 if (drm_dp_dpcd_read(mgr->aux,
3510 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
3511 &buf[i], 16) != 16)
3512 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10003513 }
Joe Perches46466b02017-05-30 16:35:37 -07003514 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10003515}
3516
Jim Bride51108f22016-04-14 10:18:36 -07003517static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3518 struct drm_dp_mst_port *port, char *name,
3519 int namelen)
3520{
3521 struct edid *mst_edid;
3522
3523 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3524 drm_edid_get_monitor_name(mst_edid, name, namelen);
3525}
3526
Dave Airliead7f8a12014-06-05 14:01:32 +10003527/**
3528 * drm_dp_mst_dump_topology(): dump topology to seq file.
3529 * @m: seq_file to dump output to
3530 * @mgr: manager to dump current topology for.
3531 *
3532 * helper to dump MST topology to a seq file for debugfs.
3533 */
3534void drm_dp_mst_dump_topology(struct seq_file *m,
3535 struct drm_dp_mst_topology_mgr *mgr)
3536{
3537 int i;
3538 struct drm_dp_mst_port *port;
Jim Bride51108f22016-04-14 10:18:36 -07003539
Dave Airliead7f8a12014-06-05 14:01:32 +10003540 mutex_lock(&mgr->lock);
3541 if (mgr->mst_primary)
3542 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
3543
3544 /* dump VCPIs */
3545 mutex_unlock(&mgr->lock);
3546
3547 mutex_lock(&mgr->payload_lock);
Jim Bride51108f22016-04-14 10:18:36 -07003548 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
3549 mgr->max_payloads);
Dave Airliead7f8a12014-06-05 14:01:32 +10003550
3551 for (i = 0; i < mgr->max_payloads; i++) {
3552 if (mgr->proposed_vcpis[i]) {
Jim Bride51108f22016-04-14 10:18:36 -07003553 char name[14];
3554
Dave Airliead7f8a12014-06-05 14:01:32 +10003555 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
Jim Bride51108f22016-04-14 10:18:36 -07003556 fetch_monitor_name(mgr, port, name, sizeof(name));
3557 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3558 port->port_num, port->vcpi.vcpi,
3559 port->vcpi.num_slots,
3560 (*name != 0) ? name : "Unknown");
Dave Airliead7f8a12014-06-05 14:01:32 +10003561 } else
Jim Bride51108f22016-04-14 10:18:36 -07003562 seq_printf(m, "vcpi %d:unused\n", i);
Dave Airliead7f8a12014-06-05 14:01:32 +10003563 }
3564 for (i = 0; i < mgr->max_payloads; i++) {
3565 seq_printf(m, "payload %d: %d, %d, %d\n",
3566 i,
3567 mgr->payloads[i].payload_state,
3568 mgr->payloads[i].start_slot,
3569 mgr->payloads[i].num_slots);
3570
3571
3572 }
3573 mutex_unlock(&mgr->payload_lock);
3574
3575 mutex_lock(&mgr->lock);
3576 if (mgr->mst_primary) {
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003577 u8 buf[DP_PAYLOAD_TABLE_SIZE];
Dave Airliead7f8a12014-06-05 14:01:32 +10003578 int ret;
Joe Perches46466b02017-05-30 16:35:37 -07003579
Dave Airliead7f8a12014-06-05 14:01:32 +10003580 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
Joe Perches46466b02017-05-30 16:35:37 -07003581 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003582 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
Joe Perches46466b02017-05-30 16:35:37 -07003583 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003584 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
Joe Perches46466b02017-05-30 16:35:37 -07003585 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003586
Dave Airlie44790462015-07-14 11:33:31 +10003587 /* dump the standard OUI branch header */
3588 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
Joe Perches46466b02017-05-30 16:35:37 -07003589 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
Jim Bride51108f22016-04-14 10:18:36 -07003590 for (i = 0x3; i < 0x8 && buf[i]; i++)
Dave Airlie44790462015-07-14 11:33:31 +10003591 seq_printf(m, "%c", buf[i]);
Joe Perches46466b02017-05-30 16:35:37 -07003592 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
3593 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
3594 if (dump_dp_payload_table(mgr, buf))
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003595 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003596 }
3597
3598 mutex_unlock(&mgr->lock);
3599
3600}
3601EXPORT_SYMBOL(drm_dp_mst_dump_topology);
3602
3603static void drm_dp_tx_work(struct work_struct *work)
3604{
3605 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
3606
3607 mutex_lock(&mgr->qlock);
Daniel Vettercb021a32016-07-15 21:48:03 +02003608 if (!list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10003609 process_single_down_tx_qlock(mgr);
3610 mutex_unlock(&mgr->qlock);
3611}
3612
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003613static void drm_dp_destroy_connector_work(struct work_struct *work)
3614{
3615 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003616 struct drm_dp_mst_port *port;
Dave Airliedf4839f2015-09-16 10:37:28 +10003617 bool send_hotplug = false;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003618 /*
3619 * Not a regular list traverse as we have to drop the destroy
3620 * connector lock before destroying the connector, to avoid AB->BA
3621 * ordering between this lock and the config mutex.
3622 */
3623 for (;;) {
3624 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003625 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
3626 if (!port) {
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003627 mutex_unlock(&mgr->destroy_connector_lock);
3628 break;
3629 }
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003630 list_del(&port->next);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003631 mutex_unlock(&mgr->destroy_connector_lock);
3632
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003633 INIT_LIST_HEAD(&port->next);
3634
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003635 mgr->cbs->destroy_connector(mgr, port->connector);
3636
3637 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +03003638 port->pdt = DP_PEER_DEVICE_NONE;
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003639
Lyude Paulebcc0e62019-01-10 19:53:29 -05003640 drm_dp_mst_put_port_malloc(port);
Dave Airliedf4839f2015-09-16 10:37:28 +10003641 send_hotplug = true;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003642 }
Dave Airliedf4839f2015-09-16 10:37:28 +10003643 if (send_hotplug)
Daniel Vetter16bff572018-11-28 23:12:34 +01003644 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003645}
3646
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003647static struct drm_private_state *
3648drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003649{
Lyude Pauleceae142019-01-10 19:53:41 -05003650 struct drm_dp_mst_topology_state *state, *old_state =
3651 to_dp_mst_topology_state(obj->state);
3652 struct drm_dp_vcpi_allocation *pos, *vcpi;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003653
Lyude Pauleceae142019-01-10 19:53:41 -05003654 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003655 if (!state)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003656 return NULL;
3657
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003658 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
3659
Lyude Pauleceae142019-01-10 19:53:41 -05003660 INIT_LIST_HEAD(&state->vcpis);
3661
3662 list_for_each_entry(pos, &old_state->vcpis, next) {
3663 /* Prune leftover freed VCPI allocations */
3664 if (!pos->vcpi)
3665 continue;
3666
3667 vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL);
3668 if (!vcpi)
3669 goto fail;
3670
3671 drm_dp_mst_get_port_malloc(vcpi->port);
3672 list_add(&vcpi->next, &state->vcpis);
3673 }
3674
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003675 return &state->base;
Lyude Pauleceae142019-01-10 19:53:41 -05003676
3677fail:
3678 list_for_each_entry_safe(pos, vcpi, &state->vcpis, next) {
3679 drm_dp_mst_put_port_malloc(pos->port);
3680 kfree(pos);
3681 }
3682 kfree(state);
3683
3684 return NULL;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003685}
3686
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003687static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
3688 struct drm_private_state *state)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003689{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003690 struct drm_dp_mst_topology_state *mst_state =
3691 to_dp_mst_topology_state(state);
Lyude Pauleceae142019-01-10 19:53:41 -05003692 struct drm_dp_vcpi_allocation *pos, *tmp;
3693
3694 list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next) {
3695 /* We only keep references to ports with non-zero VCPIs */
3696 if (pos->vcpi)
3697 drm_dp_mst_put_port_malloc(pos->port);
3698 kfree(pos);
3699 }
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003700
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003701 kfree(mst_state);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003702}
3703
Lyude Pauleceae142019-01-10 19:53:41 -05003704static inline int
3705drm_dp_mst_atomic_check_topology_state(struct drm_dp_mst_topology_mgr *mgr,
3706 struct drm_dp_mst_topology_state *mst_state)
3707{
3708 struct drm_dp_vcpi_allocation *vcpi;
Lyude Paul5e187a02019-01-10 19:53:42 -05003709 int avail_slots = 63, payload_count = 0;
Lyude Pauleceae142019-01-10 19:53:41 -05003710
3711 list_for_each_entry(vcpi, &mst_state->vcpis, next) {
3712 /* Releasing VCPI is always OK-even if the port is gone */
3713 if (!vcpi->vcpi) {
3714 DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
3715 vcpi->port);
3716 continue;
3717 }
3718
3719 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
3720 vcpi->port, vcpi->vcpi);
3721
3722 avail_slots -= vcpi->vcpi;
3723 if (avail_slots < 0) {
3724 DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
3725 vcpi->port, mst_state,
3726 avail_slots + vcpi->vcpi);
3727 return -ENOSPC;
3728 }
Lyude Paul5e187a02019-01-10 19:53:42 -05003729
3730 if (++payload_count > mgr->max_payloads) {
3731 DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
3732 mgr, mst_state, mgr->max_payloads);
3733 return -EINVAL;
3734 }
Lyude Pauleceae142019-01-10 19:53:41 -05003735 }
3736 DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
3737 mgr, mst_state, avail_slots,
3738 63 - avail_slots);
3739
3740 return 0;
3741}
3742
3743/**
3744 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
3745 * atomic update is valid
3746 * @state: Pointer to the new &struct drm_dp_mst_topology_state
3747 *
3748 * Checks the given topology state for an atomic update to ensure that it's
3749 * valid. This includes checking whether there's enough bandwidth to support
3750 * the new VCPI allocations in the atomic update.
3751 *
3752 * Any atomic drivers supporting DP MST must make sure to call this after
3753 * checking the rest of their state in their
3754 * &drm_mode_config_funcs.atomic_check() callback.
3755 *
3756 * See also:
3757 * drm_dp_atomic_find_vcpi_slots()
3758 * drm_dp_atomic_release_vcpi_slots()
3759 *
3760 * Returns:
3761 *
3762 * 0 if the new state is valid, negative error code otherwise.
3763 */
3764int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
3765{
3766 struct drm_dp_mst_topology_mgr *mgr;
3767 struct drm_dp_mst_topology_state *mst_state;
3768 int i, ret = 0;
3769
3770 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
3771 ret = drm_dp_mst_atomic_check_topology_state(mgr, mst_state);
3772 if (ret)
3773 break;
3774 }
3775
3776 return ret;
3777}
3778EXPORT_SYMBOL(drm_dp_mst_atomic_check);
3779
Lyude Paulbea5c382019-01-10 19:53:40 -05003780const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003781 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
3782 .atomic_destroy_state = drm_dp_mst_destroy_state,
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003783};
Lyude Paulbea5c382019-01-10 19:53:40 -05003784EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003785
3786/**
3787 * drm_atomic_get_mst_topology_state: get MST topology state
3788 *
3789 * @state: global atomic state
3790 * @mgr: MST topology manager, also the private object in this case
3791 *
3792 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3793 * state vtable so that the private object state returned is that of a MST
3794 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3795 * to care of the locking, so warn if don't hold the connection_mutex.
3796 *
3797 * RETURNS:
3798 *
3799 * The MST topology state or error pointer.
3800 */
3801struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
3802 struct drm_dp_mst_topology_mgr *mgr)
3803{
3804 struct drm_device *dev = mgr->dev;
3805
3806 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003807 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003808}
3809EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
3810
Dave Airliead7f8a12014-06-05 14:01:32 +10003811/**
3812 * drm_dp_mst_topology_mgr_init - initialise a topology manager
3813 * @mgr: manager struct to initialise
3814 * @dev: device providing this structure - for i2c addition.
3815 * @aux: DP helper aux channel to talk to this device
3816 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3817 * @max_payloads: maximum number of payloads this GPU can source
3818 * @conn_base_id: the connector object ID the MST device is connected to.
3819 *
3820 * Return 0 for success, or negative error code on failure
3821 */
3822int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08003823 struct drm_device *dev, struct drm_dp_aux *aux,
Dave Airliead7f8a12014-06-05 14:01:32 +10003824 int max_dpcd_transaction_bytes,
3825 int max_payloads, int conn_base_id)
3826{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003827 struct drm_dp_mst_topology_state *mst_state;
3828
Dave Airliead7f8a12014-06-05 14:01:32 +10003829 mutex_init(&mgr->lock);
3830 mutex_init(&mgr->qlock);
3831 mutex_init(&mgr->payload_lock);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003832 mutex_init(&mgr->destroy_connector_lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10003833 INIT_LIST_HEAD(&mgr->tx_msg_downq);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003834 INIT_LIST_HEAD(&mgr->destroy_connector_list);
Dave Airliead7f8a12014-06-05 14:01:32 +10003835 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
3836 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003837 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10003838 init_waitqueue_head(&mgr->tx_waitq);
3839 mgr->dev = dev;
3840 mgr->aux = aux;
3841 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
3842 mgr->max_payloads = max_payloads;
3843 mgr->conn_base_id = conn_base_id;
Imre Deak4d6a10d2016-01-29 14:44:29 +02003844 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3845 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3846 return -EINVAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10003847 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3848 if (!mgr->payloads)
3849 return -ENOMEM;
3850 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3851 if (!mgr->proposed_vcpis)
3852 return -ENOMEM;
3853 set_bit(0, &mgr->payload_mask);
Imre Deak441388a2016-01-29 14:44:28 +02003854 if (test_calc_pbn_mode() < 0)
3855 DRM_ERROR("MST PBN self-test failed\n");
3856
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003857 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
3858 if (mst_state == NULL)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003859 return -ENOMEM;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003860
3861 mst_state->mgr = mgr;
Lyude Pauleceae142019-01-10 19:53:41 -05003862 INIT_LIST_HEAD(&mst_state->vcpis);
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003863
Rob Clarkb962a122018-10-22 14:31:22 +02003864 drm_atomic_private_obj_init(dev, &mgr->base,
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003865 &mst_state->base,
Lyude Paulbea5c382019-01-10 19:53:40 -05003866 &drm_dp_mst_topology_state_funcs);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003867
Dave Airliead7f8a12014-06-05 14:01:32 +10003868 return 0;
3869}
3870EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3871
3872/**
3873 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3874 * @mgr: manager to destroy
3875 */
3876void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3877{
Lyude Paulf536e002018-12-11 18:50:26 -05003878 drm_dp_mst_topology_mgr_set_mst(mgr, false);
Dave Airlie274d8352015-09-30 10:39:42 +10003879 flush_work(&mgr->work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003880 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10003881 mutex_lock(&mgr->payload_lock);
3882 kfree(mgr->payloads);
3883 mgr->payloads = NULL;
3884 kfree(mgr->proposed_vcpis);
3885 mgr->proposed_vcpis = NULL;
3886 mutex_unlock(&mgr->payload_lock);
3887 mgr->dev = NULL;
3888 mgr->aux = NULL;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003889 drm_atomic_private_obj_fini(&mgr->base);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003890 mgr->funcs = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10003891}
3892EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3893
Ville Syrjäläcb8ce712018-09-28 21:04:00 +03003894static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
3895{
3896 int i;
3897
3898 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
3899 return false;
3900
3901 for (i = 0; i < num - 1; i++) {
3902 if (msgs[i].flags & I2C_M_RD ||
3903 msgs[i].len > 0xff)
3904 return false;
3905 }
3906
3907 return msgs[num - 1].flags & I2C_M_RD &&
3908 msgs[num - 1].len <= 0xff;
3909}
3910
Dave Airliead7f8a12014-06-05 14:01:32 +10003911/* I2C device */
3912static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3913 int num)
3914{
3915 struct drm_dp_aux *aux = adapter->algo_data;
3916 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3917 struct drm_dp_mst_branch *mstb;
3918 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3919 unsigned int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10003920 struct drm_dp_sideband_msg_req_body msg;
3921 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3922 int ret;
3923
Lyude Pauld0757af2019-01-10 19:53:28 -05003924 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Dave Airliead7f8a12014-06-05 14:01:32 +10003925 if (!mstb)
3926 return -EREMOTEIO;
3927
Ville Syrjäläcb8ce712018-09-28 21:04:00 +03003928 if (!remote_i2c_read_ok(msgs, num)) {
Dave Airliead7f8a12014-06-05 14:01:32 +10003929 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3930 ret = -EIO;
3931 goto out;
3932 }
3933
Dave Airlieae491542015-10-14 18:51:17 +10003934 memset(&msg, 0, sizeof(msg));
Dave Airliead7f8a12014-06-05 14:01:32 +10003935 msg.req_type = DP_REMOTE_I2C_READ;
3936 msg.u.i2c_read.num_transactions = num - 1;
3937 msg.u.i2c_read.port_number = port->port_num;
3938 for (i = 0; i < num - 1; i++) {
3939 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3940 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3941 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
Ville Syrjäläc978ae92018-09-28 21:03:59 +03003942 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
Dave Airliead7f8a12014-06-05 14:01:32 +10003943 }
3944 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3945 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3946
3947 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3948 if (!txmsg) {
3949 ret = -ENOMEM;
3950 goto out;
3951 }
3952
3953 txmsg->dst = mstb;
3954 drm_dp_encode_sideband_req(&msg, txmsg);
3955
3956 drm_dp_queue_down_tx(mgr, txmsg);
3957
3958 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3959 if (ret > 0) {
3960
Ville Syrjälä45bbda12019-01-22 22:03:00 +02003961 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
Dave Airliead7f8a12014-06-05 14:01:32 +10003962 ret = -EREMOTEIO;
3963 goto out;
3964 }
3965 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3966 ret = -EIO;
3967 goto out;
3968 }
3969 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3970 ret = num;
3971 }
3972out:
3973 kfree(txmsg);
Lyude Pauld0757af2019-01-10 19:53:28 -05003974 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10003975 return ret;
3976}
3977
3978static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3979{
3980 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3981 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3982 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3983 I2C_FUNC_10BIT_ADDR;
3984}
3985
3986static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3987 .functionality = drm_dp_mst_i2c_functionality,
3988 .master_xfer = drm_dp_mst_i2c_xfer,
3989};
3990
3991/**
3992 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3993 * @aux: DisplayPort AUX channel
3994 *
3995 * Returns 0 on success or a negative error code on failure.
3996 */
3997static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3998{
3999 aux->ddc.algo = &drm_dp_mst_i2c_algo;
4000 aux->ddc.algo_data = aux;
4001 aux->ddc.retries = 3;
4002
4003 aux->ddc.class = I2C_CLASS_DDC;
4004 aux->ddc.owner = THIS_MODULE;
4005 aux->ddc.dev.parent = aux->dev;
4006 aux->ddc.dev.of_node = aux->dev->of_node;
4007
4008 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
4009 sizeof(aux->ddc.name));
4010
4011 return i2c_add_adapter(&aux->ddc);
4012}
4013
4014/**
4015 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
4016 * @aux: DisplayPort AUX channel
4017 */
4018static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
4019{
4020 i2c_del_adapter(&aux->ddc);
4021}