blob: 79698560993306933cf12188390355ab2aca83f1 [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 Vetter16bff572018-11-28 23:12:34 +010036#include <drm/drm_crtc_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);
70/* sideband msg handling */
71static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
72{
73 u8 bitmask = 0x80;
74 u8 bitshift = 7;
75 u8 array_index = 0;
76 int number_of_bits = num_nibbles * 4;
77 u8 remainder = 0;
78
79 while (number_of_bits != 0) {
80 number_of_bits--;
81 remainder <<= 1;
82 remainder |= (data[array_index] & bitmask) >> bitshift;
83 bitmask >>= 1;
84 bitshift--;
85 if (bitmask == 0) {
86 bitmask = 0x80;
87 bitshift = 7;
88 array_index++;
89 }
90 if ((remainder & 0x10) == 0x10)
91 remainder ^= 0x13;
92 }
93
94 number_of_bits = 4;
95 while (number_of_bits != 0) {
96 number_of_bits--;
97 remainder <<= 1;
98 if ((remainder & 0x10) != 0)
99 remainder ^= 0x13;
100 }
101
102 return remainder;
103}
104
105static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
106{
107 u8 bitmask = 0x80;
108 u8 bitshift = 7;
109 u8 array_index = 0;
110 int number_of_bits = number_of_bytes * 8;
111 u16 remainder = 0;
112
113 while (number_of_bits != 0) {
114 number_of_bits--;
115 remainder <<= 1;
116 remainder |= (data[array_index] & bitmask) >> bitshift;
117 bitmask >>= 1;
118 bitshift--;
119 if (bitmask == 0) {
120 bitmask = 0x80;
121 bitshift = 7;
122 array_index++;
123 }
124 if ((remainder & 0x100) == 0x100)
125 remainder ^= 0xd5;
126 }
127
128 number_of_bits = 8;
129 while (number_of_bits != 0) {
130 number_of_bits--;
131 remainder <<= 1;
132 if ((remainder & 0x100) != 0)
133 remainder ^= 0xd5;
134 }
135
136 return remainder & 0xff;
137}
138static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
139{
140 u8 size = 3;
141 size += (hdr->lct / 2);
142 return size;
143}
144
145static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
146 u8 *buf, int *len)
147{
148 int idx = 0;
149 int i;
150 u8 crc4;
151 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
152 for (i = 0; i < (hdr->lct / 2); i++)
153 buf[idx++] = hdr->rad[i];
154 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
155 (hdr->msg_len & 0x3f);
156 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
157
158 crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
159 buf[idx - 1] |= (crc4 & 0xf);
160
161 *len = idx;
162}
163
164static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
165 u8 *buf, int buflen, u8 *hdrlen)
166{
167 u8 crc4;
168 u8 len;
169 int i;
170 u8 idx;
171 if (buf[0] == 0)
172 return false;
173 len = 3;
174 len += ((buf[0] & 0xf0) >> 4) / 2;
175 if (len > buflen)
176 return false;
177 crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
178
179 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
180 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
181 return false;
182 }
183
184 hdr->lct = (buf[0] & 0xf0) >> 4;
185 hdr->lcr = (buf[0] & 0xf);
186 idx = 1;
187 for (i = 0; i < (hdr->lct / 2); i++)
188 hdr->rad[i] = buf[idx++];
189 hdr->broadcast = (buf[idx] >> 7) & 0x1;
190 hdr->path_msg = (buf[idx] >> 6) & 0x1;
191 hdr->msg_len = buf[idx] & 0x3f;
192 idx++;
193 hdr->somt = (buf[idx] >> 7) & 0x1;
194 hdr->eomt = (buf[idx] >> 6) & 0x1;
195 hdr->seqno = (buf[idx] >> 4) & 0x1;
196 idx++;
197 *hdrlen = idx;
198 return true;
199}
200
201static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
202 struct drm_dp_sideband_msg_tx *raw)
203{
204 int idx = 0;
205 int i;
206 u8 *buf = raw->msg;
207 buf[idx++] = req->req_type & 0x7f;
208
209 switch (req->req_type) {
210 case DP_ENUM_PATH_RESOURCES:
211 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
212 idx++;
213 break;
214 case DP_ALLOCATE_PAYLOAD:
215 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
216 (req->u.allocate_payload.number_sdp_streams & 0xf);
217 idx++;
218 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
219 idx++;
220 buf[idx] = (req->u.allocate_payload.pbn >> 8);
221 idx++;
222 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
223 idx++;
224 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
225 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
226 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
227 idx++;
228 }
229 if (req->u.allocate_payload.number_sdp_streams & 1) {
230 i = req->u.allocate_payload.number_sdp_streams - 1;
231 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
232 idx++;
233 }
234 break;
235 case DP_QUERY_PAYLOAD:
236 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
237 idx++;
238 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
239 idx++;
240 break;
241 case DP_REMOTE_DPCD_READ:
242 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
243 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
244 idx++;
245 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
246 idx++;
247 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
248 idx++;
249 buf[idx] = (req->u.dpcd_read.num_bytes);
250 idx++;
251 break;
252
253 case DP_REMOTE_DPCD_WRITE:
254 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
255 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
256 idx++;
257 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
258 idx++;
259 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
260 idx++;
261 buf[idx] = (req->u.dpcd_write.num_bytes);
262 idx++;
263 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
264 idx += req->u.dpcd_write.num_bytes;
265 break;
266 case DP_REMOTE_I2C_READ:
267 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
268 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
269 idx++;
270 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
271 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
272 idx++;
273 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
274 idx++;
275 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
276 idx += req->u.i2c_read.transactions[i].num_bytes;
277
278 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
279 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
280 idx++;
281 }
282 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
283 idx++;
284 buf[idx] = (req->u.i2c_read.num_bytes_read);
285 idx++;
286 break;
287
288 case DP_REMOTE_I2C_WRITE:
289 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
290 idx++;
291 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
292 idx++;
293 buf[idx] = (req->u.i2c_write.num_bytes);
294 idx++;
295 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
296 idx += req->u.i2c_write.num_bytes;
297 break;
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700298
299 case DP_POWER_DOWN_PHY:
300 case DP_POWER_UP_PHY:
301 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
302 idx++;
303 break;
Dave Airliead7f8a12014-06-05 14:01:32 +1000304 }
305 raw->cur_len = idx;
306}
307
308static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
309{
310 u8 crc4;
311 crc4 = drm_dp_msg_data_crc4(msg, len);
312 msg[len] = crc4;
313}
314
315static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
316 struct drm_dp_sideband_msg_tx *raw)
317{
318 int idx = 0;
319 u8 *buf = raw->msg;
320
321 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
322
323 raw->cur_len = idx;
324}
325
326/* this adds a chunk of msg to the builder to get the final msg */
327static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
328 u8 *replybuf, u8 replybuflen, bool hdr)
329{
330 int ret;
331 u8 crc4;
332
333 if (hdr) {
334 u8 hdrlen;
335 struct drm_dp_sideband_msg_hdr recv_hdr;
336 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
337 if (ret == false) {
338 print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
339 return false;
340 }
341
Imre Deak636c4c32017-07-19 16:46:32 +0300342 /*
343 * ignore out-of-order messages or messages that are part of a
344 * failed transaction
345 */
346 if (!recv_hdr.somt && !msg->have_somt)
347 return false;
348
Dave Airliead7f8a12014-06-05 14:01:32 +1000349 /* get length contained in this portion */
350 msg->curchunk_len = recv_hdr.msg_len;
351 msg->curchunk_hdrlen = hdrlen;
352
353 /* we have already gotten an somt - don't bother parsing */
354 if (recv_hdr.somt && msg->have_somt)
355 return false;
356
357 if (recv_hdr.somt) {
358 memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
359 msg->have_somt = true;
360 }
361 if (recv_hdr.eomt)
362 msg->have_eomt = true;
363
364 /* copy the bytes for the remainder of this header chunk */
365 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
366 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
367 } else {
368 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
369 msg->curchunk_idx += replybuflen;
370 }
371
372 if (msg->curchunk_idx >= msg->curchunk_len) {
373 /* do CRC */
374 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
375 /* copy chunk into bigger msg */
376 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
377 msg->curlen += msg->curchunk_len - 1;
378 }
379 return true;
380}
381
382static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
383 struct drm_dp_sideband_msg_reply_body *repmsg)
384{
385 int idx = 1;
386 int i;
387 memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
388 idx += 16;
389 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
390 idx++;
391 if (idx > raw->curlen)
392 goto fail_len;
393 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
394 if (raw->msg[idx] & 0x80)
395 repmsg->u.link_addr.ports[i].input_port = 1;
396
397 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
398 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
399
400 idx++;
401 if (idx > raw->curlen)
402 goto fail_len;
403 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
404 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
405 if (repmsg->u.link_addr.ports[i].input_port == 0)
406 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
407 idx++;
408 if (idx > raw->curlen)
409 goto fail_len;
410 if (repmsg->u.link_addr.ports[i].input_port == 0) {
411 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
412 idx++;
413 if (idx > raw->curlen)
414 goto fail_len;
415 memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
416 idx += 16;
417 if (idx > raw->curlen)
418 goto fail_len;
419 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
420 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
421 idx++;
422
423 }
424 if (idx > raw->curlen)
425 goto fail_len;
426 }
427
428 return true;
429fail_len:
430 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
431 return false;
432}
433
434static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
435 struct drm_dp_sideband_msg_reply_body *repmsg)
436{
437 int idx = 1;
438 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
439 idx++;
440 if (idx > raw->curlen)
441 goto fail_len;
442 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
Hans Verkuila4c30a42018-08-27 10:07:42 +0200443 idx++;
Dave Airliead7f8a12014-06-05 14:01:32 +1000444 if (idx > raw->curlen)
445 goto fail_len;
446
447 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
448 return true;
449fail_len:
450 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
451 return false;
452}
453
454static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
455 struct drm_dp_sideband_msg_reply_body *repmsg)
456{
457 int idx = 1;
458 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
459 idx++;
460 if (idx > raw->curlen)
461 goto fail_len;
462 return true;
463fail_len:
464 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
465 return false;
466}
467
468static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
469 struct drm_dp_sideband_msg_reply_body *repmsg)
470{
471 int idx = 1;
472
473 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
474 idx++;
475 if (idx > raw->curlen)
476 goto fail_len;
477 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
478 idx++;
479 /* TODO check */
480 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
481 return true;
482fail_len:
483 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
484 return false;
485}
486
487static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
488 struct drm_dp_sideband_msg_reply_body *repmsg)
489{
490 int idx = 1;
491 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
492 idx++;
493 if (idx > raw->curlen)
494 goto fail_len;
495 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
496 idx += 2;
497 if (idx > raw->curlen)
498 goto fail_len;
499 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
500 idx += 2;
501 if (idx > raw->curlen)
502 goto fail_len;
503 return true;
504fail_len:
505 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
506 return false;
507}
508
509static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
510 struct drm_dp_sideband_msg_reply_body *repmsg)
511{
512 int idx = 1;
513 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
514 idx++;
515 if (idx > raw->curlen)
516 goto fail_len;
517 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
518 idx++;
519 if (idx > raw->curlen)
520 goto fail_len;
521 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
522 idx += 2;
523 if (idx > raw->curlen)
524 goto fail_len;
525 return true;
526fail_len:
527 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
528 return false;
529}
530
531static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
532 struct drm_dp_sideband_msg_reply_body *repmsg)
533{
534 int idx = 1;
535 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
536 idx++;
537 if (idx > raw->curlen)
538 goto fail_len;
539 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
540 idx += 2;
541 if (idx > raw->curlen)
542 goto fail_len;
543 return true;
544fail_len:
545 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
546 return false;
547}
548
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700549static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
550 struct drm_dp_sideband_msg_reply_body *repmsg)
551{
552 int idx = 1;
553
554 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
555 idx++;
556 if (idx > raw->curlen) {
557 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
558 idx, raw->curlen);
559 return false;
560 }
561 return true;
562}
563
Dave Airliead7f8a12014-06-05 14:01:32 +1000564static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
565 struct drm_dp_sideband_msg_reply_body *msg)
566{
567 memset(msg, 0, sizeof(*msg));
568 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
569 msg->req_type = (raw->msg[0] & 0x7f);
570
571 if (msg->reply_type) {
572 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
573 msg->u.nak.reason = raw->msg[17];
574 msg->u.nak.nak_data = raw->msg[18];
575 return false;
576 }
577
578 switch (msg->req_type) {
579 case DP_LINK_ADDRESS:
580 return drm_dp_sideband_parse_link_address(raw, msg);
581 case DP_QUERY_PAYLOAD:
582 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
583 case DP_REMOTE_DPCD_READ:
584 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
585 case DP_REMOTE_DPCD_WRITE:
586 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
587 case DP_REMOTE_I2C_READ:
588 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
589 case DP_ENUM_PATH_RESOURCES:
590 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
591 case DP_ALLOCATE_PAYLOAD:
592 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700593 case DP_POWER_DOWN_PHY:
594 case DP_POWER_UP_PHY:
595 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
Dave Airliead7f8a12014-06-05 14:01:32 +1000596 default:
597 DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
598 return false;
599 }
600}
601
602static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
603 struct drm_dp_sideband_msg_req_body *msg)
604{
605 int idx = 1;
606
607 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
608 idx++;
609 if (idx > raw->curlen)
610 goto fail_len;
611
612 memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
613 idx += 16;
614 if (idx > raw->curlen)
615 goto fail_len;
616
617 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
618 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
619 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
620 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
621 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
622 idx++;
623 return true;
624fail_len:
625 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
626 return false;
627}
628
629static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
630 struct drm_dp_sideband_msg_req_body *msg)
631{
632 int idx = 1;
633
634 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
635 idx++;
636 if (idx > raw->curlen)
637 goto fail_len;
638
639 memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
640 idx += 16;
641 if (idx > raw->curlen)
642 goto fail_len;
643
644 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
645 idx++;
646 return true;
647fail_len:
648 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
649 return false;
650}
651
652static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
653 struct drm_dp_sideband_msg_req_body *msg)
654{
655 memset(msg, 0, sizeof(*msg));
656 msg->req_type = (raw->msg[0] & 0x7f);
657
658 switch (msg->req_type) {
659 case DP_CONNECTION_STATUS_NOTIFY:
660 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
661 case DP_RESOURCE_STATUS_NOTIFY:
662 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
663 default:
664 DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
665 return false;
666 }
667}
668
669static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
670{
671 struct drm_dp_sideband_msg_req_body req;
672
673 req.req_type = DP_REMOTE_DPCD_WRITE;
674 req.u.dpcd_write.port_number = port_num;
675 req.u.dpcd_write.dpcd_address = offset;
676 req.u.dpcd_write.num_bytes = num_bytes;
677 req.u.dpcd_write.bytes = bytes;
678 drm_dp_encode_sideband_req(&req, msg);
679
680 return 0;
681}
682
683static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
684{
685 struct drm_dp_sideband_msg_req_body req;
686
687 req.req_type = DP_LINK_ADDRESS;
688 drm_dp_encode_sideband_req(&req, msg);
689 return 0;
690}
691
692static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
693{
694 struct drm_dp_sideband_msg_req_body req;
695
696 req.req_type = DP_ENUM_PATH_RESOURCES;
697 req.u.port_num.port_number = port_num;
698 drm_dp_encode_sideband_req(&req, msg);
699 msg->path_msg = true;
700 return 0;
701}
702
703static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
Libin Yangef8f9be2015-12-02 14:09:43 +0800704 u8 vcpi, uint16_t pbn,
705 u8 number_sdp_streams,
706 u8 *sdp_stream_sink)
Dave Airliead7f8a12014-06-05 14:01:32 +1000707{
708 struct drm_dp_sideband_msg_req_body req;
709 memset(&req, 0, sizeof(req));
710 req.req_type = DP_ALLOCATE_PAYLOAD;
711 req.u.allocate_payload.port_number = port_num;
712 req.u.allocate_payload.vcpi = vcpi;
713 req.u.allocate_payload.pbn = pbn;
Libin Yangef8f9be2015-12-02 14:09:43 +0800714 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
715 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
716 number_sdp_streams);
Dave Airliead7f8a12014-06-05 14:01:32 +1000717 drm_dp_encode_sideband_req(&req, msg);
718 msg->path_msg = true;
719 return 0;
720}
721
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -0700722static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
723 int port_num, bool power_up)
724{
725 struct drm_dp_sideband_msg_req_body req;
726
727 if (power_up)
728 req.req_type = DP_POWER_UP_PHY;
729 else
730 req.req_type = DP_POWER_DOWN_PHY;
731
732 req.u.port_num.port_number = port_num;
733 drm_dp_encode_sideband_req(&req, msg);
734 msg->path_msg = true;
735 return 0;
736}
737
Dave Airliead7f8a12014-06-05 14:01:32 +1000738static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
739 struct drm_dp_vcpi *vcpi)
740{
Dave Airliedfda0df2014-08-06 16:26:21 +1000741 int ret, vcpi_ret;
Dave Airliead7f8a12014-06-05 14:01:32 +1000742
743 mutex_lock(&mgr->payload_lock);
744 ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
745 if (ret > mgr->max_payloads) {
746 ret = -EINVAL;
747 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
748 goto out_unlock;
749 }
750
Dave Airliedfda0df2014-08-06 16:26:21 +1000751 vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
752 if (vcpi_ret > mgr->max_payloads) {
753 ret = -EINVAL;
754 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
755 goto out_unlock;
756 }
757
Dave Airliead7f8a12014-06-05 14:01:32 +1000758 set_bit(ret, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +1000759 set_bit(vcpi_ret, &mgr->vcpi_mask);
760 vcpi->vcpi = vcpi_ret + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +1000761 mgr->proposed_vcpis[ret - 1] = vcpi;
762out_unlock:
763 mutex_unlock(&mgr->payload_lock);
764 return ret;
765}
766
767static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
Dave Airliedfda0df2014-08-06 16:26:21 +1000768 int vcpi)
Dave Airliead7f8a12014-06-05 14:01:32 +1000769{
Dave Airliedfda0df2014-08-06 16:26:21 +1000770 int i;
771 if (vcpi == 0)
Dave Airliead7f8a12014-06-05 14:01:32 +1000772 return;
773
774 mutex_lock(&mgr->payload_lock);
Dave Airliedfda0df2014-08-06 16:26:21 +1000775 DRM_DEBUG_KMS("putting payload %d\n", vcpi);
776 clear_bit(vcpi - 1, &mgr->vcpi_mask);
777
778 for (i = 0; i < mgr->max_payloads; i++) {
779 if (mgr->proposed_vcpis[i])
780 if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
781 mgr->proposed_vcpis[i] = NULL;
782 clear_bit(i + 1, &mgr->payload_mask);
783 }
784 }
Dave Airliead7f8a12014-06-05 14:01:32 +1000785 mutex_unlock(&mgr->payload_lock);
786}
787
788static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
789 struct drm_dp_sideband_msg_tx *txmsg)
790{
Chris Wilson992d38c2017-05-13 11:52:00 +0100791 unsigned int state;
Daniel Vettercd961bb2015-01-28 10:02:23 +0100792
793 /*
794 * All updates to txmsg->state are protected by mgr->qlock, and the two
795 * cases we check here are terminal states. For those the barriers
796 * provided by the wake_up/wait_event pair are enough.
797 */
Chris Wilson992d38c2017-05-13 11:52:00 +0100798 state = READ_ONCE(txmsg->state);
799 return (state == DRM_DP_SIDEBAND_TX_RX ||
800 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
Dave Airliead7f8a12014-06-05 14:01:32 +1000801}
802
803static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
804 struct drm_dp_sideband_msg_tx *txmsg)
805{
806 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
807 int ret;
808
809 ret = wait_event_timeout(mgr->tx_waitq,
810 check_txmsg_state(mgr, txmsg),
811 (4 * HZ));
812 mutex_lock(&mstb->mgr->qlock);
813 if (ret > 0) {
814 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
815 ret = -EIO;
816 goto out;
817 }
818 } else {
819 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
820
821 /* dump some state */
822 ret = -EIO;
823
824 /* remove from q */
825 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
826 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
827 list_del(&txmsg->next);
828 }
829
830 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
831 txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
832 mstb->tx_slots[txmsg->seqno] = NULL;
833 }
834 }
835out:
836 mutex_unlock(&mgr->qlock);
837
838 return ret;
839}
840
841static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
842{
843 struct drm_dp_mst_branch *mstb;
844
845 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
846 if (!mstb)
847 return NULL;
848
849 mstb->lct = lct;
850 if (lct > 1)
851 memcpy(mstb->rad, rad, lct / 2);
852 INIT_LIST_HEAD(&mstb->ports);
Lyude Paulebcc0e62019-01-10 19:53:29 -0500853 kref_init(&mstb->topology_kref);
854 kref_init(&mstb->malloc_kref);
Dave Airliead7f8a12014-06-05 14:01:32 +1000855 return mstb;
856}
857
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500858static void drm_dp_free_mst_branch_device(struct kref *kref)
859{
Lyude Paulebcc0e62019-01-10 19:53:29 -0500860 struct drm_dp_mst_branch *mstb =
861 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
862
863 if (mstb->port_parent)
864 drm_dp_mst_put_port_malloc(mstb->port_parent);
865
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500866 kfree(mstb);
867}
868
Lyude Paulebcc0e62019-01-10 19:53:29 -0500869/**
870 * DOC: Branch device and port refcounting
871 *
872 * Topology refcount overview
873 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
874 *
875 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
876 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
877 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
878 *
879 * Topology refcounts are not exposed to drivers, and are handled internally
880 * by the DP MST helpers. The helpers use them in order to prevent the
881 * in-memory topology state from being changed in the middle of critical
882 * operations like changing the internal state of payload allocations. This
883 * means each branch and port will be considered to be connected to the rest
884 * of the topology until it's topology refcount reaches zero. Additionally,
885 * for ports this means that their associated &struct drm_connector will stay
886 * registered with userspace until the port's refcount reaches 0.
887 *
888 * Malloc refcount overview
889 * ~~~~~~~~~~~~~~~~~~~~~~~~
890 *
891 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
892 * drm_dp_mst_branch allocated even after all of its topology references have
893 * been dropped, so that the driver or MST helpers can safely access each
894 * branch's last known state before it was disconnected from the topology.
895 * When the malloc refcount of a port or branch reaches 0, the memory
896 * allocation containing the &struct drm_dp_mst_branch or &struct
897 * drm_dp_mst_port respectively will be freed.
898 *
899 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
900 * to drivers. As of writing this documentation, there are no drivers that
901 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
902 * helpers. Exposing this API to drivers in a race-free manner would take more
903 * tweaking of the refcounting scheme, however patches are welcome provided
904 * there is a legitimate driver usecase for this.
905 *
906 * Refcount relationships in a topology
907 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
908 *
909 * Let's take a look at why the relationship between topology and malloc
910 * refcounts is designed the way it is.
911 *
912 * .. kernel-figure:: dp-mst/topology-figure-1.dot
913 *
914 * An example of topology and malloc refs in a DP MST topology with two
915 * active payloads. Topology refcount increments are indicated by solid
916 * lines, and malloc refcount increments are indicated by dashed lines.
917 * Each starts from the branch which incremented the refcount, and ends at
918 * the branch to which the refcount belongs to, i.e. the arrow points the
919 * same way as the C pointers used to reference a structure.
920 *
921 * As you can see in the above figure, every branch increments the topology
922 * refcount of it's children, and increments the malloc refcount of it's
923 * parent. Additionally, every payload increments the malloc refcount of it's
924 * assigned port by 1.
925 *
926 * So, what would happen if MSTB #3 from the above figure was unplugged from
927 * the system, but the driver hadn't yet removed payload #2 from port #3? The
928 * topology would start to look like the figure below.
929 *
930 * .. kernel-figure:: dp-mst/topology-figure-2.dot
931 *
932 * Ports and branch devices which have been released from memory are
933 * colored grey, and references which have been removed are colored red.
934 *
935 * Whenever a port or branch device's topology refcount reaches zero, it will
936 * decrement the topology refcounts of all its children, the malloc refcount
937 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
938 * #4, this means they both have been disconnected from the topology and freed
939 * from memory. But, because payload #2 is still holding a reference to port
940 * #3, port #3 is removed from the topology but it's &struct drm_dp_mst_port
941 * is still accessible from memory. This also means port #3 has not yet
942 * decremented the malloc refcount of MSTB #3, so it's &struct
943 * drm_dp_mst_branch will also stay allocated in memory until port #3's
944 * malloc refcount reaches 0.
945 *
946 * This relationship is necessary because in order to release payload #2, we
947 * need to be able to figure out the last relative of port #3 that's still
948 * connected to the topology. In this case, we would travel up the topology as
949 * shown below.
950 *
951 * .. kernel-figure:: dp-mst/topology-figure-3.dot
952 *
953 * And finally, remove payload #2 by communicating with port #2 through
954 * sideband transactions.
955 */
956
957/**
958 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
959 * device
960 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
961 *
962 * Increments &drm_dp_mst_branch.malloc_kref. When
963 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
964 * will be released and @mstb may no longer be used.
965 *
966 * See also: drm_dp_mst_put_mstb_malloc()
967 */
968static void
969drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
970{
971 kref_get(&mstb->malloc_kref);
972 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
973}
974
975/**
976 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
977 * device
978 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
979 *
980 * Decrements &drm_dp_mst_branch.malloc_kref. When
981 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
982 * will be released and @mstb may no longer be used.
983 *
984 * See also: drm_dp_mst_get_mstb_malloc()
985 */
986static void
987drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
988{
989 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
990 kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
991}
992
993static void drm_dp_free_mst_port(struct kref *kref)
994{
995 struct drm_dp_mst_port *port =
996 container_of(kref, struct drm_dp_mst_port, malloc_kref);
997
998 drm_dp_mst_put_mstb_malloc(port->parent);
999 kfree(port);
1000}
1001
1002/**
1003 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1004 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1005 *
1006 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1007 * reaches 0, the memory allocation for @port will be released and @port may
1008 * no longer be used.
1009 *
1010 * Because @port could potentially be freed at any time by the DP MST helpers
1011 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1012 * function, drivers that which to make use of &struct drm_dp_mst_port should
1013 * ensure that they grab at least one main malloc reference to their MST ports
1014 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1015 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1016 *
1017 * See also: drm_dp_mst_put_port_malloc()
1018 */
1019void
1020drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1021{
1022 kref_get(&port->malloc_kref);
1023 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1024}
1025EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1026
1027/**
1028 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1029 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1030 *
1031 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1032 * reaches 0, the memory allocation for @port will be released and @port may
1033 * no longer be used.
1034 *
1035 * See also: drm_dp_mst_get_port_malloc()
1036 */
1037void
1038drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1039{
1040 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1041 kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1042}
1043EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1044
Dave Airliead7f8a12014-06-05 14:01:32 +10001045static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1046{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001047 struct drm_dp_mst_branch *mstb =
1048 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1049 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
Dave Airliead7f8a12014-06-05 14:01:32 +10001050 struct drm_dp_mst_port *port, *tmp;
1051 bool wake_tx = false;
1052
Lyude Paulebcc0e62019-01-10 19:53:29 -05001053 mutex_lock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001054 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
1055 list_del(&port->next);
Lyude Pauld0757af2019-01-10 19:53:28 -05001056 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001057 }
Lyude Paulebcc0e62019-01-10 19:53:29 -05001058 mutex_unlock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001059
1060 /* drop any tx slots msg */
1061 mutex_lock(&mstb->mgr->qlock);
1062 if (mstb->tx_slots[0]) {
1063 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1064 mstb->tx_slots[0] = NULL;
1065 wake_tx = true;
1066 }
1067 if (mstb->tx_slots[1]) {
1068 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1069 mstb->tx_slots[1] = NULL;
1070 wake_tx = true;
1071 }
1072 mutex_unlock(&mstb->mgr->qlock);
1073
1074 if (wake_tx)
Chris Wilson68e989d2017-05-13 11:52:01 +01001075 wake_up_all(&mstb->mgr->tx_waitq);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001076
Lyude Paulebcc0e62019-01-10 19:53:29 -05001077 drm_dp_mst_put_mstb_malloc(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001078}
1079
Lyude Paulebcc0e62019-01-10 19:53:29 -05001080/**
1081 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1082 * branch device unless its zero
1083 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1084 *
1085 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1086 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1087 * reached 0). Holding a topology reference implies that a malloc reference
1088 * will be held to @mstb as long as the user holds the topology reference.
1089 *
1090 * Care should be taken to ensure that the user has at least one malloc
1091 * reference to @mstb. If you already have a topology reference to @mstb, you
1092 * should use drm_dp_mst_topology_get_mstb() instead.
1093 *
1094 * See also:
1095 * drm_dp_mst_topology_get_mstb()
1096 * drm_dp_mst_topology_put_mstb()
1097 *
1098 * Returns:
1099 * * 1: A topology reference was grabbed successfully
1100 * * 0: @port is no longer in the topology, no reference was grabbed
1101 */
1102static int __must_check
1103drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001104{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001105 int ret = kref_get_unless_zero(&mstb->topology_kref);
1106
1107 if (ret)
1108 DRM_DEBUG("mstb %p (%d)\n", mstb,
1109 kref_read(&mstb->topology_kref));
1110
1111 return ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001112}
1113
Lyude Paulebcc0e62019-01-10 19:53:29 -05001114/**
1115 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1116 * branch device
1117 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1118 *
1119 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1120 * not it's already reached 0. This is only valid to use in scenarios where
1121 * you are already guaranteed to have at least one active topology reference
1122 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1123 *
1124 * See also:
1125 * drm_dp_mst_topology_try_get_mstb()
1126 * drm_dp_mst_topology_put_mstb()
1127 */
1128static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1129{
1130 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1131 kref_get(&mstb->topology_kref);
1132 DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1133}
1134
1135/**
1136 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1137 * device
1138 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1139 *
1140 * Releases a topology reference from @mstb by decrementing
1141 * &drm_dp_mst_branch.topology_kref.
1142 *
1143 * See also:
1144 * drm_dp_mst_topology_try_get_mstb()
1145 * drm_dp_mst_topology_get_mstb()
1146 */
1147static void
1148drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1149{
1150 DRM_DEBUG("mstb %p (%d)\n",
1151 mstb, kref_read(&mstb->topology_kref) - 1);
1152 kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1153}
Dave Airliead7f8a12014-06-05 14:01:32 +10001154
1155static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
1156{
Daniel Vetter03913592014-12-08 22:55:22 +01001157 struct drm_dp_mst_branch *mstb;
1158
Dave Airliead7f8a12014-06-05 14:01:32 +10001159 switch (old_pdt) {
1160 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1161 case DP_PEER_DEVICE_SST_SINK:
1162 /* remove i2c over sideband */
1163 drm_dp_mst_unregister_i2c_bus(&port->aux);
1164 break;
1165 case DP_PEER_DEVICE_MST_BRANCHING:
Daniel Vetter03913592014-12-08 22:55:22 +01001166 mstb = port->mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +10001167 port->mstb = NULL;
Lyude Pauld0757af2019-01-10 19:53:28 -05001168 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001169 break;
1170 }
1171}
1172
1173static void drm_dp_destroy_port(struct kref *kref)
1174{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001175 struct drm_dp_mst_port *port =
1176 container_of(kref, struct drm_dp_mst_port, topology_kref);
Dave Airliead7f8a12014-06-05 14:01:32 +10001177 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
Dave Airliedf4839f2015-09-16 10:37:28 +10001178
Dave Airliead7f8a12014-06-05 14:01:32 +10001179 if (!port->input) {
1180 port->vcpi.num_slots = 0;
Dave Airliec6a0aed2014-10-20 16:28:02 +10001181
1182 kfree(port->cached_edid);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001183
Dave Airliedf4839f2015-09-16 10:37:28 +10001184 /*
1185 * The only time we don't have a connector
1186 * on an output port is if the connector init
1187 * fails.
1188 */
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001189 if (port->connector) {
Dave Airliedf4839f2015-09-16 10:37:28 +10001190 /* we can't destroy the connector here, as
1191 * we might be holding the mode_config.mutex
1192 * from an EDID retrieval */
1193
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001194 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02001195 list_add(&port->next, &mgr->destroy_connector_list);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001196 mutex_unlock(&mgr->destroy_connector_lock);
1197 schedule_work(&mgr->destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02001198 return;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10001199 }
Dave Airliedf4839f2015-09-16 10:37:28 +10001200 /* no need to clean up vcpi
1201 * as if we have no connector we never setup a vcpi */
Dave Airliead7f8a12014-06-05 14:01:32 +10001202 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +03001203 port->pdt = DP_PEER_DEVICE_NONE;
Dave Airliead7f8a12014-06-05 14:01:32 +10001204 }
Lyude Paulebcc0e62019-01-10 19:53:29 -05001205 drm_dp_mst_put_port_malloc(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001206}
1207
Lyude Paulebcc0e62019-01-10 19:53:29 -05001208/**
1209 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1210 * port unless its zero
1211 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1212 *
1213 * Attempts to grab a topology reference to @port, if it hasn't yet been
1214 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1215 * 0). Holding a topology reference implies that a malloc reference will be
1216 * held to @port as long as the user holds the topology reference.
1217 *
1218 * Care should be taken to ensure that the user has at least one malloc
1219 * reference to @port. If you already have a topology reference to @port, you
1220 * should use drm_dp_mst_topology_get_port() instead.
1221 *
1222 * See also:
1223 * drm_dp_mst_topology_get_port()
1224 * drm_dp_mst_topology_put_port()
1225 *
1226 * Returns:
1227 * * 1: A topology reference was grabbed successfully
1228 * * 0: @port is no longer in the topology, no reference was grabbed
1229 */
1230static int __must_check
1231drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1232{
1233 int ret = kref_get_unless_zero(&port->topology_kref);
1234
1235 if (ret)
1236 DRM_DEBUG("port %p (%d)\n", port,
1237 kref_read(&port->topology_kref));
1238
1239 return ret;
1240}
1241
1242/**
1243 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1244 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1245 *
1246 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1247 * not it's already reached 0. This is only valid to use in scenarios where
1248 * you are already guaranteed to have at least one active topology reference
1249 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1250 *
1251 * See also:
1252 * drm_dp_mst_topology_try_get_port()
1253 * drm_dp_mst_topology_put_port()
1254 */
1255static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1256{
1257 WARN_ON(kref_read(&port->topology_kref) == 0);
1258 kref_get(&port->topology_kref);
1259 DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1260}
1261
1262/**
1263 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1264 * @port: The &struct drm_dp_mst_port to release the topology reference from
1265 *
1266 * Releases a topology reference from @port by decrementing
1267 * &drm_dp_mst_port.topology_kref.
1268 *
1269 * See also:
1270 * drm_dp_mst_topology_try_get_port()
1271 * drm_dp_mst_topology_get_port()
1272 */
Lyude Pauld0757af2019-01-10 19:53:28 -05001273static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10001274{
Lyude Paulebcc0e62019-01-10 19:53:29 -05001275 DRM_DEBUG("port %p (%d)\n",
1276 port, kref_read(&port->topology_kref) - 1);
1277 kref_put(&port->topology_kref, drm_dp_destroy_port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001278}
1279
Lyude Pauld0757af2019-01-10 19:53:28 -05001280static struct drm_dp_mst_branch *
1281drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1282 struct drm_dp_mst_branch *to_find)
Dave Airliead7f8a12014-06-05 14:01:32 +10001283{
1284 struct drm_dp_mst_port *port;
1285 struct drm_dp_mst_branch *rmstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001286
1287 if (to_find == mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001288 return mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001289
Dave Airliead7f8a12014-06-05 14:01:32 +10001290 list_for_each_entry(port, &mstb->ports, next) {
1291 if (port->mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001292 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1293 port->mstb, to_find);
Dave Airliead7f8a12014-06-05 14:01:32 +10001294 if (rmstb)
1295 return rmstb;
1296 }
1297 }
1298 return NULL;
1299}
1300
Lyude Pauld0757af2019-01-10 19:53:28 -05001301static struct drm_dp_mst_branch *
1302drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1303 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001304{
1305 struct drm_dp_mst_branch *rmstb = NULL;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001306
Dave Airliead7f8a12014-06-05 14:01:32 +10001307 mutex_lock(&mgr->lock);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001308 if (mgr->mst_primary) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001309 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1310 mgr->mst_primary, mstb);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001311
1312 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1313 rmstb = NULL;
1314 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001315 mutex_unlock(&mgr->lock);
1316 return rmstb;
1317}
1318
Lyude Paulebcc0e62019-01-10 19:53:29 -05001319static struct drm_dp_mst_port *
1320drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1321 struct drm_dp_mst_port *to_find)
Dave Airliead7f8a12014-06-05 14:01:32 +10001322{
1323 struct drm_dp_mst_port *port, *mport;
1324
1325 list_for_each_entry(port, &mstb->ports, next) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001326 if (port == to_find)
Dave Airliead7f8a12014-06-05 14:01:32 +10001327 return port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001328
Dave Airliead7f8a12014-06-05 14:01:32 +10001329 if (port->mstb) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001330 mport = drm_dp_mst_topology_get_port_validated_locked(
1331 port->mstb, to_find);
Dave Airliead7f8a12014-06-05 14:01:32 +10001332 if (mport)
1333 return mport;
1334 }
1335 }
1336 return NULL;
1337}
1338
Lyude Pauld0757af2019-01-10 19:53:28 -05001339static struct drm_dp_mst_port *
1340drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1341 struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10001342{
1343 struct drm_dp_mst_port *rport = NULL;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001344
Dave Airliead7f8a12014-06-05 14:01:32 +10001345 mutex_lock(&mgr->lock);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001346 if (mgr->mst_primary) {
1347 rport = drm_dp_mst_topology_get_port_validated_locked(
1348 mgr->mst_primary, port);
1349
1350 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1351 rport = NULL;
1352 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001353 mutex_unlock(&mgr->lock);
1354 return rport;
1355}
1356
1357static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1358{
1359 struct drm_dp_mst_port *port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001360 int ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001361
1362 list_for_each_entry(port, &mstb->ports, next) {
1363 if (port->port_num == port_num) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001364 ret = drm_dp_mst_topology_try_get_port(port);
1365 return ret ? port : NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001366 }
1367 }
1368
1369 return NULL;
1370}
1371
1372/*
1373 * calculate a new RAD for this MST branch device
1374 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1375 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1376 */
1377static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1378 u8 *rad)
1379{
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001380 int parent_lct = port->parent->lct;
Dave Airliead7f8a12014-06-05 14:01:32 +10001381 int shift = 4;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001382 int idx = (parent_lct - 1) / 2;
1383 if (parent_lct > 1) {
1384 memcpy(rad, port->parent->rad, idx + 1);
1385 shift = (parent_lct % 2) ? 4 : 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001386 } else
1387 rad[0] = 0;
1388
1389 rad[idx] |= port->port_num << shift;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001390 return parent_lct + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +10001391}
1392
1393/*
1394 * return sends link address for new mstb
1395 */
1396static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1397{
1398 int ret;
1399 u8 rad[6], lct;
1400 bool send_link = false;
1401 switch (port->pdt) {
1402 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1403 case DP_PEER_DEVICE_SST_SINK:
1404 /* add i2c over sideband */
1405 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1406 break;
1407 case DP_PEER_DEVICE_MST_BRANCHING:
1408 lct = drm_dp_calculate_rad(port, rad);
1409
1410 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
Joe Moriarty22a07032018-02-12 14:51:42 -05001411 if (port->mstb) {
1412 port->mstb->mgr = port->mgr;
1413 port->mstb->port_parent = port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001414 /*
1415 * Make sure this port's memory allocation stays
1416 * around until it's child MSTB releases it
1417 */
1418 drm_dp_mst_get_port_malloc(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001419
Joe Moriarty22a07032018-02-12 14:51:42 -05001420 send_link = true;
1421 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001422 break;
1423 }
1424 return send_link;
1425}
1426
Hersen Wu5e93b822016-01-22 17:07:28 -05001427static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
Dave Airliead7f8a12014-06-05 14:01:32 +10001428{
1429 int ret;
Hersen Wu5e93b822016-01-22 17:07:28 -05001430
1431 memcpy(mstb->guid, guid, 16);
1432
1433 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1434 if (mstb->port_parent) {
1435 ret = drm_dp_send_dpcd_write(
1436 mstb->mgr,
1437 mstb->port_parent,
1438 DP_GUID,
1439 16,
1440 mstb->guid);
1441 } else {
1442
1443 ret = drm_dp_dpcd_write(
1444 mstb->mgr->aux,
1445 DP_GUID,
1446 mstb->guid,
1447 16);
Dave Airliead7f8a12014-06-05 14:01:32 +10001448 }
1449 }
1450}
1451
Dave Airlie1c960872015-09-16 11:04:49 +10001452static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1453 int pnum,
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001454 char *proppath,
1455 size_t proppath_size)
Dave Airliead7f8a12014-06-05 14:01:32 +10001456{
1457 int i;
1458 char temp[8];
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001459 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
Dave Airliead7f8a12014-06-05 14:01:32 +10001460 for (i = 0; i < (mstb->lct - 1); i++) {
1461 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001462 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001463 snprintf(temp, sizeof(temp), "-%d", port_num);
1464 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001465 }
Dave Airlie1c960872015-09-16 11:04:49 +10001466 snprintf(temp, sizeof(temp), "-%d", pnum);
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001467 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001468}
1469
1470static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001471 struct drm_device *dev,
Dave Airliead7f8a12014-06-05 14:01:32 +10001472 struct drm_dp_link_addr_reply_port *port_msg)
1473{
1474 struct drm_dp_mst_port *port;
1475 bool ret;
1476 bool created = false;
1477 int old_pdt = 0;
1478 int old_ddps = 0;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001479
Dave Airliead7f8a12014-06-05 14:01:32 +10001480 port = drm_dp_get_port(mstb, port_msg->port_number);
1481 if (!port) {
1482 port = kzalloc(sizeof(*port), GFP_KERNEL);
1483 if (!port)
1484 return;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001485 kref_init(&port->topology_kref);
1486 kref_init(&port->malloc_kref);
Dave Airliead7f8a12014-06-05 14:01:32 +10001487 port->parent = mstb;
1488 port->port_num = port_msg->port_number;
1489 port->mgr = mstb->mgr;
1490 port->aux.name = "DPMST";
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001491 port->aux.dev = dev->dev;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001492
1493 /*
1494 * Make sure the memory allocation for our parent branch stays
1495 * around until our own memory allocation is released
1496 */
1497 drm_dp_mst_get_mstb_malloc(mstb);
1498
Dave Airliead7f8a12014-06-05 14:01:32 +10001499 created = true;
1500 } else {
1501 old_pdt = port->pdt;
1502 old_ddps = port->ddps;
1503 }
1504
1505 port->pdt = port_msg->peer_device_type;
1506 port->input = port_msg->input_port;
1507 port->mcs = port_msg->mcs;
1508 port->ddps = port_msg->ddps;
1509 port->ldps = port_msg->legacy_device_plug_status;
1510 port->dpcd_rev = port_msg->dpcd_revision;
1511 port->num_sdp_streams = port_msg->num_sdp_streams;
1512 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
Dave Airliead7f8a12014-06-05 14:01:32 +10001513
1514 /* manage mstb port lists with mgr lock - take a reference
1515 for this list */
1516 if (created) {
1517 mutex_lock(&mstb->mgr->lock);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001518 drm_dp_mst_topology_get_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001519 list_add(&port->next, &mstb->ports);
1520 mutex_unlock(&mstb->mgr->lock);
1521 }
1522
1523 if (old_ddps != port->ddps) {
1524 if (port->ddps) {
Lyude Paul3d76df62019-01-10 19:53:24 -05001525 if (!port->input) {
1526 drm_dp_send_enum_path_resources(mstb->mgr,
1527 mstb, port);
1528 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001529 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001530 port->available_pbn = 0;
Lyude Paul3d76df62019-01-10 19:53:24 -05001531 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001532 }
1533
1534 if (old_pdt != port->pdt && !port->input) {
1535 drm_dp_port_teardown_pdt(port, old_pdt);
1536
1537 ret = drm_dp_port_setup_pdt(port);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001538 if (ret == true)
Dave Airliead7f8a12014-06-05 14:01:32 +10001539 drm_dp_send_link_address(mstb->mgr, port->mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001540 }
1541
1542 if (created && !port->input) {
1543 char proppath[255];
Dave Airlie1c960872015-09-16 11:04:49 +10001544
Lyude Paul3d76df62019-01-10 19:53:24 -05001545 build_mst_prop_path(mstb, port->port_num, proppath,
1546 sizeof(proppath));
1547 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
1548 port,
1549 proppath);
Dave Airliedf4839f2015-09-16 10:37:28 +10001550 if (!port->connector) {
1551 /* remove it from the port list */
1552 mutex_lock(&mstb->mgr->lock);
1553 list_del(&port->next);
1554 mutex_unlock(&mstb->mgr->lock);
1555 /* drop port list reference */
Lyude Pauld0757af2019-01-10 19:53:28 -05001556 drm_dp_mst_topology_put_port(port);
Dave Airliedf4839f2015-09-16 10:37:28 +10001557 goto out;
1558 }
Ville Syrjälä4da5caa2016-10-26 12:05:55 +03001559 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1560 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1561 port->port_num >= DP_MST_LOGICAL_PORT_0) {
Lyude Paul3d76df62019-01-10 19:53:24 -05001562 port->cached_edid = drm_get_edid(port->connector,
1563 &port->aux.ddc);
Daniel Vetter97e14fb2018-07-09 10:40:08 +02001564 drm_connector_set_tile_property(port->connector);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001565 }
Dave Airlied9515c52015-09-16 17:55:23 +10001566 (*mstb->mgr->cbs->register_connector)(port->connector);
Dave Airliead7f8a12014-06-05 14:01:32 +10001567 }
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001568
Dave Airliedf4839f2015-09-16 10:37:28 +10001569out:
Dave Airliead7f8a12014-06-05 14:01:32 +10001570 /* put reference to this port */
Lyude Pauld0757af2019-01-10 19:53:28 -05001571 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001572}
1573
1574static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1575 struct drm_dp_connection_status_notify *conn_stat)
1576{
1577 struct drm_dp_mst_port *port;
1578 int old_pdt;
1579 int old_ddps;
1580 bool dowork = false;
1581 port = drm_dp_get_port(mstb, conn_stat->port_number);
1582 if (!port)
1583 return;
1584
1585 old_ddps = port->ddps;
1586 old_pdt = port->pdt;
1587 port->pdt = conn_stat->peer_device_type;
1588 port->mcs = conn_stat->message_capability_status;
1589 port->ldps = conn_stat->legacy_device_plug_status;
1590 port->ddps = conn_stat->displayport_device_plug_status;
1591
1592 if (old_ddps != port->ddps) {
1593 if (port->ddps) {
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001594 dowork = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001595 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001596 port->available_pbn = 0;
1597 }
1598 }
1599 if (old_pdt != port->pdt && !port->input) {
1600 drm_dp_port_teardown_pdt(port, old_pdt);
1601
1602 if (drm_dp_port_setup_pdt(port))
1603 dowork = true;
1604 }
1605
Lyude Pauld0757af2019-01-10 19:53:28 -05001606 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001607 if (dowork)
1608 queue_work(system_long_wq, &mstb->mgr->work);
1609
1610}
1611
1612static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1613 u8 lct, u8 *rad)
1614{
1615 struct drm_dp_mst_branch *mstb;
1616 struct drm_dp_mst_port *port;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001617 int i, ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001618 /* find the port by iterating down */
Dave Airlie9eb1e572015-06-22 14:40:44 +10001619
1620 mutex_lock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001621 mstb = mgr->mst_primary;
1622
Stanislav Lisovskiy23d80032018-11-09 11:00:12 +02001623 if (!mstb)
1624 goto out;
1625
Dave Airliead7f8a12014-06-05 14:01:32 +10001626 for (i = 0; i < lct - 1; i++) {
1627 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001628 int port_num = (rad[i / 2] >> shift) & 0xf;
Dave Airliead7f8a12014-06-05 14:01:32 +10001629
1630 list_for_each_entry(port, &mstb->ports, next) {
1631 if (port->port_num == port_num) {
Adam Richter30730c72015-10-16 03:33:02 -07001632 mstb = port->mstb;
1633 if (!mstb) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001634 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
Adam Richter30730c72015-10-16 03:33:02 -07001635 goto out;
Dave Airliead7f8a12014-06-05 14:01:32 +10001636 }
1637
Dave Airliead7f8a12014-06-05 14:01:32 +10001638 break;
1639 }
1640 }
1641 }
Lyude Paulebcc0e62019-01-10 19:53:29 -05001642 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1643 if (!ret)
1644 mstb = NULL;
Adam Richter30730c72015-10-16 03:33:02 -07001645out:
Dave Airlie9eb1e572015-06-22 14:40:44 +10001646 mutex_unlock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001647 return mstb;
1648}
1649
Mykola Lysenkobd934322015-12-18 17:14:42 -05001650static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1651 struct drm_dp_mst_branch *mstb,
1652 uint8_t *guid)
1653{
1654 struct drm_dp_mst_branch *found_mstb;
1655 struct drm_dp_mst_port *port;
1656
Hersen Wu5e93b822016-01-22 17:07:28 -05001657 if (memcmp(mstb->guid, guid, 16) == 0)
1658 return mstb;
1659
1660
Mykola Lysenkobd934322015-12-18 17:14:42 -05001661 list_for_each_entry(port, &mstb->ports, next) {
1662 if (!port->mstb)
1663 continue;
1664
Mykola Lysenkobd934322015-12-18 17:14:42 -05001665 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1666
1667 if (found_mstb)
1668 return found_mstb;
1669 }
1670
1671 return NULL;
1672}
1673
Lyude Paulde6d6812019-01-10 19:53:25 -05001674static struct drm_dp_mst_branch *
1675drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
1676 uint8_t *guid)
Mykola Lysenkobd934322015-12-18 17:14:42 -05001677{
1678 struct drm_dp_mst_branch *mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001679 int ret;
Mykola Lysenkobd934322015-12-18 17:14:42 -05001680
1681 /* find the port by iterating down */
1682 mutex_lock(&mgr->lock);
1683
Hersen Wu5e93b822016-01-22 17:07:28 -05001684 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
Lyude Paulebcc0e62019-01-10 19:53:29 -05001685 if (mstb) {
1686 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1687 if (!ret)
1688 mstb = NULL;
1689 }
Mykola Lysenkobd934322015-12-18 17:14:42 -05001690
1691 mutex_unlock(&mgr->lock);
1692 return mstb;
1693}
1694
Dave Airliead7f8a12014-06-05 14:01:32 +10001695static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1696 struct drm_dp_mst_branch *mstb)
1697{
1698 struct drm_dp_mst_port *port;
Daniel Vetter9254ec42015-06-22 17:31:59 +10001699 struct drm_dp_mst_branch *mstb_child;
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001700 if (!mstb->link_address_sent)
Dave Airliead7f8a12014-06-05 14:01:32 +10001701 drm_dp_send_link_address(mgr, mstb);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001702
Dave Airliead7f8a12014-06-05 14:01:32 +10001703 list_for_each_entry(port, &mstb->ports, next) {
1704 if (port->input)
1705 continue;
1706
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001707 if (!port->ddps)
Dave Airliead7f8a12014-06-05 14:01:32 +10001708 continue;
1709
1710 if (!port->available_pbn)
1711 drm_dp_send_enum_path_resources(mgr, mstb, port);
1712
Daniel Vetter9254ec42015-06-22 17:31:59 +10001713 if (port->mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001714 mstb_child = drm_dp_mst_topology_get_mstb_validated(
1715 mgr, port->mstb);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001716 if (mstb_child) {
1717 drm_dp_check_and_send_link_address(mgr, mstb_child);
Lyude Pauld0757af2019-01-10 19:53:28 -05001718 drm_dp_mst_topology_put_mstb(mstb_child);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001719 }
1720 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001721 }
1722}
1723
1724static void drm_dp_mst_link_probe_work(struct work_struct *work)
1725{
1726 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001727 struct drm_dp_mst_branch *mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05001728 int ret;
Dave Airliead7f8a12014-06-05 14:01:32 +10001729
Daniel Vetter9254ec42015-06-22 17:31:59 +10001730 mutex_lock(&mgr->lock);
1731 mstb = mgr->mst_primary;
1732 if (mstb) {
Lyude Paulebcc0e62019-01-10 19:53:29 -05001733 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1734 if (!ret)
1735 mstb = NULL;
Daniel Vetter9254ec42015-06-22 17:31:59 +10001736 }
1737 mutex_unlock(&mgr->lock);
1738 if (mstb) {
1739 drm_dp_check_and_send_link_address(mgr, mstb);
Lyude Pauld0757af2019-01-10 19:53:28 -05001740 drm_dp_mst_topology_put_mstb(mstb);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001741 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001742}
1743
1744static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1745 u8 *guid)
1746{
Ville Syrjäläe38e1282017-07-12 18:52:54 +03001747 u64 salt;
Dave Airliead7f8a12014-06-05 14:01:32 +10001748
Ville Syrjäläe38e1282017-07-12 18:52:54 +03001749 if (memchr_inv(guid, 0, 16))
1750 return true;
1751
1752 salt = get_jiffies_64();
1753
1754 memcpy(&guid[0], &salt, sizeof(u64));
1755 memcpy(&guid[8], &salt, sizeof(u64));
1756
1757 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10001758}
1759
1760#if 0
1761static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1762{
1763 struct drm_dp_sideband_msg_req_body req;
1764
1765 req.req_type = DP_REMOTE_DPCD_READ;
1766 req.u.dpcd_read.port_number = port_num;
1767 req.u.dpcd_read.dpcd_address = offset;
1768 req.u.dpcd_read.num_bytes = num_bytes;
1769 drm_dp_encode_sideband_req(&req, msg);
1770
1771 return 0;
1772}
1773#endif
1774
1775static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1776 bool up, u8 *msg, int len)
1777{
1778 int ret;
1779 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1780 int tosend, total, offset;
1781 int retries = 0;
1782
1783retry:
1784 total = len;
1785 offset = 0;
1786 do {
1787 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1788
1789 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1790 &msg[offset],
1791 tosend);
1792 if (ret != tosend) {
1793 if (ret == -EIO && retries < 5) {
1794 retries++;
1795 goto retry;
1796 }
1797 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10001798
1799 return -EIO;
1800 }
1801 offset += tosend;
1802 total -= tosend;
1803 } while (total > 0);
1804 return 0;
1805}
1806
1807static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1808 struct drm_dp_sideband_msg_tx *txmsg)
1809{
1810 struct drm_dp_mst_branch *mstb = txmsg->dst;
Mykola Lysenkobd934322015-12-18 17:14:42 -05001811 u8 req_type;
Dave Airliead7f8a12014-06-05 14:01:32 +10001812
1813 /* both msg slots are full */
1814 if (txmsg->seqno == -1) {
1815 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1816 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1817 return -EAGAIN;
1818 }
1819 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1820 txmsg->seqno = mstb->last_seqno;
1821 mstb->last_seqno ^= 1;
1822 } else if (mstb->tx_slots[0] == NULL)
1823 txmsg->seqno = 0;
1824 else
1825 txmsg->seqno = 1;
1826 mstb->tx_slots[txmsg->seqno] = txmsg;
1827 }
Mykola Lysenkobd934322015-12-18 17:14:42 -05001828
1829 req_type = txmsg->msg[0] & 0x7f;
1830 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1831 req_type == DP_RESOURCE_STATUS_NOTIFY)
1832 hdr->broadcast = 1;
1833 else
1834 hdr->broadcast = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001835 hdr->path_msg = txmsg->path_msg;
1836 hdr->lct = mstb->lct;
1837 hdr->lcr = mstb->lct - 1;
1838 if (mstb->lct > 1)
1839 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1840 hdr->seqno = txmsg->seqno;
1841 return 0;
1842}
1843/*
1844 * process a single block of the next message in the sideband queue
1845 */
1846static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1847 struct drm_dp_sideband_msg_tx *txmsg,
1848 bool up)
1849{
1850 u8 chunk[48];
1851 struct drm_dp_sideband_msg_hdr hdr;
1852 int len, space, idx, tosend;
1853 int ret;
1854
Damien Lespiaubf3719c2014-07-14 12:13:18 +01001855 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1856
Dave Airliead7f8a12014-06-05 14:01:32 +10001857 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1858 txmsg->seqno = -1;
1859 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1860 }
1861
1862 /* make hdr from dst mst - for replies use seqno
1863 otherwise assign one */
1864 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1865 if (ret < 0)
1866 return ret;
1867
1868 /* amount left to send in this message */
1869 len = txmsg->cur_len - txmsg->cur_offset;
1870
1871 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1872 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1873
1874 tosend = min(len, space);
1875 if (len == txmsg->cur_len)
1876 hdr.somt = 1;
1877 if (space >= len)
1878 hdr.eomt = 1;
1879
1880
1881 hdr.msg_len = tosend + 1;
1882 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1883 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1884 /* add crc at end */
1885 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1886 idx += tosend + 1;
1887
1888 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1889 if (ret) {
1890 DRM_DEBUG_KMS("sideband msg failed to send\n");
1891 return ret;
1892 }
1893
1894 txmsg->cur_offset += tosend;
1895 if (txmsg->cur_offset == txmsg->cur_len) {
1896 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1897 return 1;
1898 }
1899 return 0;
1900}
1901
Dave Airliead7f8a12014-06-05 14:01:32 +10001902static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1903{
1904 struct drm_dp_sideband_msg_tx *txmsg;
1905 int ret;
1906
Daniel Vettercd961bb2015-01-28 10:02:23 +01001907 WARN_ON(!mutex_is_locked(&mgr->qlock));
1908
Dave Airliead7f8a12014-06-05 14:01:32 +10001909 /* construct a chunk from the first msg in the tx_msg queue */
Daniel Vettercb021a32016-07-15 21:48:03 +02001910 if (list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10001911 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001912
1913 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1914 ret = process_single_tx_qlock(mgr, txmsg, false);
1915 if (ret == 1) {
1916 /* txmsg is sent it should be in the slots now */
1917 list_del(&txmsg->next);
1918 } else if (ret) {
1919 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1920 list_del(&txmsg->next);
1921 if (txmsg->seqno != -1)
1922 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1923 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
Chris Wilson68e989d2017-05-13 11:52:01 +01001924 wake_up_all(&mgr->tx_waitq);
Dave Airliead7f8a12014-06-05 14:01:32 +10001925 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001926}
1927
1928/* called holding qlock */
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001929static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1930 struct drm_dp_sideband_msg_tx *txmsg)
Dave Airliead7f8a12014-06-05 14:01:32 +10001931{
Dave Airliead7f8a12014-06-05 14:01:32 +10001932 int ret;
1933
1934 /* construct a chunk from the first msg in the tx_msg queue */
Dave Airliead7f8a12014-06-05 14:01:32 +10001935 ret = process_single_tx_qlock(mgr, txmsg, true);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001936
1937 if (ret != 1)
Dave Airliead7f8a12014-06-05 14:01:32 +10001938 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001939
1940 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001941}
1942
1943static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1944 struct drm_dp_sideband_msg_tx *txmsg)
1945{
1946 mutex_lock(&mgr->qlock);
1947 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
Daniel Vettercb021a32016-07-15 21:48:03 +02001948 if (list_is_singular(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10001949 process_single_down_tx_qlock(mgr);
1950 mutex_unlock(&mgr->qlock);
1951}
1952
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001953static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1954 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001955{
1956 int len;
1957 struct drm_dp_sideband_msg_tx *txmsg;
1958 int ret;
1959
1960 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1961 if (!txmsg)
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001962 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001963
1964 txmsg->dst = mstb;
1965 len = build_link_address(txmsg);
1966
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001967 mstb->link_address_sent = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001968 drm_dp_queue_down_tx(mgr, txmsg);
1969
1970 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1971 if (ret > 0) {
1972 int i;
1973
1974 if (txmsg->reply.reply_type == 1)
1975 DRM_DEBUG_KMS("link address nak received\n");
1976 else {
1977 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1978 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1979 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,
1980 txmsg->reply.u.link_addr.ports[i].input_port,
1981 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1982 txmsg->reply.u.link_addr.ports[i].port_number,
1983 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1984 txmsg->reply.u.link_addr.ports[i].mcs,
1985 txmsg->reply.u.link_addr.ports[i].ddps,
1986 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1987 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1988 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1989 }
Hersen Wu5e93b822016-01-22 17:07:28 -05001990
1991 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1992
Dave Airliead7f8a12014-06-05 14:01:32 +10001993 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1994 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1995 }
Daniel Vetter16bff572018-11-28 23:12:34 +01001996 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airliead7f8a12014-06-05 14:01:32 +10001997 }
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001998 } else {
1999 mstb->link_address_sent = false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002000 DRM_DEBUG_KMS("link address failed %d\n", ret);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10002001 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002002
2003 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10002004}
2005
2006static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2007 struct drm_dp_mst_branch *mstb,
2008 struct drm_dp_mst_port *port)
2009{
2010 int len;
2011 struct drm_dp_sideband_msg_tx *txmsg;
2012 int ret;
2013
2014 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2015 if (!txmsg)
2016 return -ENOMEM;
2017
2018 txmsg->dst = mstb;
2019 len = build_enum_path_resources(txmsg, port->port_num);
2020
2021 drm_dp_queue_down_tx(mgr, txmsg);
2022
2023 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2024 if (ret > 0) {
2025 if (txmsg->reply.reply_type == 1)
2026 DRM_DEBUG_KMS("enum path resources nak received\n");
2027 else {
2028 if (port->port_num != txmsg->reply.u.path_resources.port_number)
2029 DRM_ERROR("got incorrect port in response\n");
2030 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,
2031 txmsg->reply.u.path_resources.avail_payload_bw_number);
2032 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
2033 }
2034 }
2035
2036 kfree(txmsg);
2037 return 0;
2038}
2039
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002040static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2041{
2042 if (!mstb->port_parent)
2043 return NULL;
2044
2045 if (mstb->port_parent->mstb != mstb)
2046 return mstb->port_parent;
2047
2048 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2049}
2050
2051static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2052 struct drm_dp_mst_branch *mstb,
2053 int *port_num)
2054{
2055 struct drm_dp_mst_branch *rmstb = NULL;
2056 struct drm_dp_mst_port *found_port;
2057 mutex_lock(&mgr->lock);
2058 if (mgr->mst_primary) {
2059 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
2060
2061 if (found_port) {
2062 rmstb = found_port->parent;
Lyude Paulebcc0e62019-01-10 19:53:29 -05002063 if (drm_dp_mst_topology_try_get_mstb(rmstb))
2064 *port_num = found_port->port_num;
2065 else
2066 rmstb = NULL;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002067 }
2068 }
2069 mutex_unlock(&mgr->lock);
2070 return rmstb;
2071}
2072
Thierry Reding8fa6a422014-07-21 13:23:57 +02002073static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
2074 struct drm_dp_mst_port *port,
2075 int id,
2076 int pbn)
Dave Airliead7f8a12014-06-05 14:01:32 +10002077{
2078 struct drm_dp_sideband_msg_tx *txmsg;
2079 struct drm_dp_mst_branch *mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002080 int len, ret, port_num;
Libin Yangef8f9be2015-12-02 14:09:43 +08002081 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
2082 int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10002083
Lyude Pauld0757af2019-01-10 19:53:28 -05002084 port = drm_dp_mst_topology_get_port_validated(mgr, port);
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04002085 if (!port)
2086 return -EINVAL;
2087
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002088 port_num = port->port_num;
Lyude Pauld0757af2019-01-10 19:53:28 -05002089 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002090 if (!mstb) {
Lyude Paulde6d6812019-01-10 19:53:25 -05002091 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
2092 port->parent,
2093 &port_num);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002094
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04002095 if (!mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -05002096 drm_dp_mst_topology_put_port(port);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002097 return -EINVAL;
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04002098 }
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002099 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002100
2101 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2102 if (!txmsg) {
2103 ret = -ENOMEM;
2104 goto fail_put;
2105 }
2106
Libin Yangef8f9be2015-12-02 14:09:43 +08002107 for (i = 0; i < port->num_sdp_streams; i++)
2108 sinks[i] = i;
2109
Dave Airliead7f8a12014-06-05 14:01:32 +10002110 txmsg->dst = mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05002111 len = build_allocate_payload(txmsg, port_num,
Dave Airliead7f8a12014-06-05 14:01:32 +10002112 id,
Libin Yangef8f9be2015-12-02 14:09:43 +08002113 pbn, port->num_sdp_streams, sinks);
Dave Airliead7f8a12014-06-05 14:01:32 +10002114
2115 drm_dp_queue_down_tx(mgr, txmsg);
2116
2117 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2118 if (ret > 0) {
Lyude Paulde6d6812019-01-10 19:53:25 -05002119 if (txmsg->reply.reply_type == 1)
Dave Airliead7f8a12014-06-05 14:01:32 +10002120 ret = -EINVAL;
Lyude Paulde6d6812019-01-10 19:53:25 -05002121 else
Dave Airliead7f8a12014-06-05 14:01:32 +10002122 ret = 0;
2123 }
2124 kfree(txmsg);
2125fail_put:
Lyude Pauld0757af2019-01-10 19:53:28 -05002126 drm_dp_mst_topology_put_mstb(mstb);
2127 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002128 return ret;
2129}
2130
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002131int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
2132 struct drm_dp_mst_port *port, bool power_up)
2133{
2134 struct drm_dp_sideband_msg_tx *txmsg;
2135 int len, ret;
2136
Lyude Pauld0757af2019-01-10 19:53:28 -05002137 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002138 if (!port)
2139 return -EINVAL;
2140
2141 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2142 if (!txmsg) {
Lyude Pauld0757af2019-01-10 19:53:28 -05002143 drm_dp_mst_topology_put_port(port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002144 return -ENOMEM;
2145 }
2146
2147 txmsg->dst = port->parent;
2148 len = build_power_updown_phy(txmsg, port->port_num, power_up);
2149 drm_dp_queue_down_tx(mgr, txmsg);
2150
2151 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
2152 if (ret > 0) {
2153 if (txmsg->reply.reply_type == 1)
2154 ret = -EINVAL;
2155 else
2156 ret = 0;
2157 }
2158 kfree(txmsg);
Lyude Pauld0757af2019-01-10 19:53:28 -05002159 drm_dp_mst_topology_put_port(port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07002160
2161 return ret;
2162}
2163EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
2164
Dave Airliead7f8a12014-06-05 14:01:32 +10002165static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2166 int id,
2167 struct drm_dp_payload *payload)
2168{
2169 int ret;
2170
2171 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
2172 if (ret < 0) {
2173 payload->payload_state = 0;
2174 return ret;
2175 }
2176 payload->payload_state = DP_PAYLOAD_LOCAL;
2177 return 0;
2178}
2179
Thierry Reding8fa6a422014-07-21 13:23:57 +02002180static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2181 struct drm_dp_mst_port *port,
2182 int id,
2183 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10002184{
2185 int ret;
2186 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
2187 if (ret < 0)
2188 return ret;
2189 payload->payload_state = DP_PAYLOAD_REMOTE;
2190 return ret;
2191}
2192
Thierry Reding8fa6a422014-07-21 13:23:57 +02002193static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2194 struct drm_dp_mst_port *port,
2195 int id,
2196 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10002197{
2198 DRM_DEBUG_KMS("\n");
2199 /* its okay for these to fail */
2200 if (port) {
2201 drm_dp_payload_send_msg(mgr, port, id, 0);
2202 }
2203
2204 drm_dp_dpcd_write_payload(mgr, id, payload);
Dave Airliedfda0df2014-08-06 16:26:21 +10002205 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002206 return 0;
2207}
2208
Thierry Reding8fa6a422014-07-21 13:23:57 +02002209static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2210 int id,
2211 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10002212{
2213 payload->payload_state = 0;
2214 return 0;
2215}
2216
2217/**
2218 * drm_dp_update_payload_part1() - Execute payload update part 1
2219 * @mgr: manager to use.
2220 *
2221 * This iterates over all proposed virtual channels, and tries to
2222 * allocate space in the link for them. For 0->slots transitions,
2223 * this step just writes the VCPI to the MST device. For slots->0
2224 * transitions, this writes the updated VCPIs and removes the
2225 * remote VC payloads.
2226 *
2227 * after calling this the driver should generate ACT and payload
2228 * packets.
2229 */
2230int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
2231{
Dave Airliedfda0df2014-08-06 16:26:21 +10002232 int i, j;
Dave Airliead7f8a12014-06-05 14:01:32 +10002233 int cur_slots = 1;
2234 struct drm_dp_payload req_payload;
2235 struct drm_dp_mst_port *port;
2236
2237 mutex_lock(&mgr->payload_lock);
2238 for (i = 0; i < mgr->max_payloads; i++) {
Lyude Paul706246c2018-12-13 20:25:31 -05002239 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2240 struct drm_dp_payload *payload = &mgr->payloads[i];
2241
Dave Airliead7f8a12014-06-05 14:01:32 +10002242 /* solve the current payloads - compare to the hw ones
2243 - update the hw view */
2244 req_payload.start_slot = cur_slots;
Lyude Paul706246c2018-12-13 20:25:31 -05002245 if (vcpi) {
2246 port = container_of(vcpi, struct drm_dp_mst_port,
2247 vcpi);
Lyude Pauld0757af2019-01-10 19:53:28 -05002248 port = drm_dp_mst_topology_get_port_validated(mgr,
2249 port);
cpaul@redhat.com263efde2016-04-22 16:08:46 -04002250 if (!port) {
2251 mutex_unlock(&mgr->payload_lock);
2252 return -EINVAL;
2253 }
Lyude Paul706246c2018-12-13 20:25:31 -05002254 req_payload.num_slots = vcpi->num_slots;
2255 req_payload.vcpi = vcpi->vcpi;
Dave Airliead7f8a12014-06-05 14:01:32 +10002256 } else {
2257 port = NULL;
2258 req_payload.num_slots = 0;
2259 }
Dave Airliedfda0df2014-08-06 16:26:21 +10002260
Lyude Paul706246c2018-12-13 20:25:31 -05002261 payload->start_slot = req_payload.start_slot;
Dave Airliead7f8a12014-06-05 14:01:32 +10002262 /* work out what is required to happen with this payload */
Lyude Paul706246c2018-12-13 20:25:31 -05002263 if (payload->num_slots != req_payload.num_slots) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002264
2265 /* need to push an update for this payload */
2266 if (req_payload.num_slots) {
Lyude Paul706246c2018-12-13 20:25:31 -05002267 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
2268 &req_payload);
2269 payload->num_slots = req_payload.num_slots;
2270 payload->vcpi = req_payload.vcpi;
2271
2272 } else if (payload->num_slots) {
2273 payload->num_slots = 0;
2274 drm_dp_destroy_payload_step1(mgr, port,
2275 payload->vcpi,
2276 payload);
2277 req_payload.payload_state =
2278 payload->payload_state;
2279 payload->start_slot = 0;
Dave Airliedfda0df2014-08-06 16:26:21 +10002280 }
Lyude Paul706246c2018-12-13 20:25:31 -05002281 payload->payload_state = req_payload.payload_state;
Dave Airliead7f8a12014-06-05 14:01:32 +10002282 }
2283 cur_slots += req_payload.num_slots;
cpaul@redhat.com263efde2016-04-22 16:08:46 -04002284
2285 if (port)
Lyude Pauld0757af2019-01-10 19:53:28 -05002286 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002287 }
Dave Airliedfda0df2014-08-06 16:26:21 +10002288
2289 for (i = 0; i < mgr->max_payloads; i++) {
Lyude Paul706246c2018-12-13 20:25:31 -05002290 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL)
2291 continue;
Dave Airliedfda0df2014-08-06 16:26:21 +10002292
Lyude Paul706246c2018-12-13 20:25:31 -05002293 DRM_DEBUG_KMS("removing payload %d\n", i);
2294 for (j = i; j < mgr->max_payloads - 1; j++) {
2295 mgr->payloads[j] = mgr->payloads[j + 1];
2296 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
2297
2298 if (mgr->proposed_vcpis[j] &&
2299 mgr->proposed_vcpis[j]->num_slots) {
2300 set_bit(j + 1, &mgr->payload_mask);
2301 } else {
2302 clear_bit(j + 1, &mgr->payload_mask);
2303 }
Dave Airliedfda0df2014-08-06 16:26:21 +10002304 }
Lyude Paul706246c2018-12-13 20:25:31 -05002305
2306 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
2307 sizeof(struct drm_dp_payload));
2308 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
2309 clear_bit(mgr->max_payloads, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10002310 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002311 mutex_unlock(&mgr->payload_lock);
2312
2313 return 0;
2314}
2315EXPORT_SYMBOL(drm_dp_update_payload_part1);
2316
2317/**
2318 * drm_dp_update_payload_part2() - Execute payload update part 2
2319 * @mgr: manager to use.
2320 *
2321 * This iterates over all proposed virtual channels, and tries to
2322 * allocate space in the link for them. For 0->slots transitions,
2323 * this step writes the remote VC payload commands. For slots->0
2324 * this just resets some internal state.
2325 */
2326int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
2327{
2328 struct drm_dp_mst_port *port;
2329 int i;
Damien Lespiau7389ad42014-07-14 11:53:44 +01002330 int ret = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002331 mutex_lock(&mgr->payload_lock);
2332 for (i = 0; i < mgr->max_payloads; i++) {
2333
2334 if (!mgr->proposed_vcpis[i])
2335 continue;
2336
2337 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2338
2339 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2340 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10002341 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10002342 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10002343 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10002344 }
2345 if (ret) {
2346 mutex_unlock(&mgr->payload_lock);
2347 return ret;
2348 }
2349 }
2350 mutex_unlock(&mgr->payload_lock);
2351 return 0;
2352}
2353EXPORT_SYMBOL(drm_dp_update_payload_part2);
2354
2355#if 0 /* unused as of yet */
2356static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2357 struct drm_dp_mst_port *port,
2358 int offset, int size)
2359{
2360 int len;
2361 struct drm_dp_sideband_msg_tx *txmsg;
2362
2363 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2364 if (!txmsg)
2365 return -ENOMEM;
2366
2367 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
2368 txmsg->dst = port->parent;
2369
2370 drm_dp_queue_down_tx(mgr, txmsg);
2371
2372 return 0;
2373}
2374#endif
2375
2376static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2377 struct drm_dp_mst_port *port,
2378 int offset, int size, u8 *bytes)
2379{
2380 int len;
2381 int ret;
2382 struct drm_dp_sideband_msg_tx *txmsg;
2383 struct drm_dp_mst_branch *mstb;
2384
Lyude Pauld0757af2019-01-10 19:53:28 -05002385 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Dave Airliead7f8a12014-06-05 14:01:32 +10002386 if (!mstb)
2387 return -EINVAL;
2388
2389 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2390 if (!txmsg) {
2391 ret = -ENOMEM;
2392 goto fail_put;
2393 }
2394
2395 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2396 txmsg->dst = mstb;
2397
2398 drm_dp_queue_down_tx(mgr, txmsg);
2399
2400 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2401 if (ret > 0) {
2402 if (txmsg->reply.reply_type == 1) {
2403 ret = -EINVAL;
2404 } else
2405 ret = 0;
2406 }
2407 kfree(txmsg);
2408fail_put:
Lyude Pauld0757af2019-01-10 19:53:28 -05002409 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002410 return ret;
2411}
2412
2413static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2414{
2415 struct drm_dp_sideband_msg_reply_body reply;
2416
Mykola Lysenkoa68d2432015-12-07 13:55:51 -05002417 reply.reply_type = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002418 reply.req_type = req_type;
2419 drm_dp_encode_sideband_reply(&reply, msg);
2420 return 0;
2421}
2422
2423static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2424 struct drm_dp_mst_branch *mstb,
2425 int req_type, int seqno, bool broadcast)
2426{
2427 struct drm_dp_sideband_msg_tx *txmsg;
2428
2429 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2430 if (!txmsg)
2431 return -ENOMEM;
2432
2433 txmsg->dst = mstb;
2434 txmsg->seqno = seqno;
2435 drm_dp_encode_up_ack_reply(txmsg, req_type);
2436
2437 mutex_lock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05002438
2439 process_single_up_tx_qlock(mgr, txmsg);
2440
Dave Airliead7f8a12014-06-05 14:01:32 +10002441 mutex_unlock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05002442
2443 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10002444 return 0;
2445}
2446
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002447static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2448 int dp_link_count,
2449 int *out)
Dave Airliead7f8a12014-06-05 14:01:32 +10002450{
2451 switch (dp_link_bw) {
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002452 default:
2453 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2454 dp_link_bw, dp_link_count);
2455 return false;
2456
Dave Airliead7f8a12014-06-05 14:01:32 +10002457 case DP_LINK_BW_1_62:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002458 *out = 3 * dp_link_count;
2459 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002460 case DP_LINK_BW_2_7:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002461 *out = 5 * dp_link_count;
2462 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002463 case DP_LINK_BW_5_4:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002464 *out = 10 * dp_link_count;
2465 break;
Manasi Navaree0bd8782018-01-22 14:43:10 -08002466 case DP_LINK_BW_8_1:
2467 *out = 15 * dp_link_count;
2468 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002469 }
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002470 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002471}
2472
2473/**
2474 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2475 * @mgr: manager to set state for
2476 * @mst_state: true to enable MST on this connector - false to disable.
2477 *
2478 * This is called by the driver when it detects an MST capable device plugged
2479 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2480 */
2481int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2482{
2483 int ret = 0;
2484 struct drm_dp_mst_branch *mstb = NULL;
2485
2486 mutex_lock(&mgr->lock);
2487 if (mst_state == mgr->mst_state)
2488 goto out_unlock;
2489
2490 mgr->mst_state = mst_state;
2491 /* set the device into MST mode */
2492 if (mst_state) {
2493 WARN_ON(mgr->mst_primary);
2494
2495 /* get dpcd info */
2496 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2497 if (ret != DP_RECEIVER_CAP_SIZE) {
2498 DRM_DEBUG_KMS("failed to read DPCD\n");
2499 goto out_unlock;
2500 }
2501
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002502 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2503 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2504 &mgr->pbn_div)) {
2505 ret = -EINVAL;
2506 goto out_unlock;
2507 }
2508
Dave Airliead7f8a12014-06-05 14:01:32 +10002509 /* add initial branch device at LCT 1 */
2510 mstb = drm_dp_add_mst_branch_device(1, NULL);
2511 if (mstb == NULL) {
2512 ret = -ENOMEM;
2513 goto out_unlock;
2514 }
2515 mstb->mgr = mgr;
2516
2517 /* give this the main reference */
2518 mgr->mst_primary = mstb;
Lyude Paulebcc0e62019-01-10 19:53:29 -05002519 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
Dave Airliead7f8a12014-06-05 14:01:32 +10002520
Andrey Grodzovskyc175cd12016-01-22 17:07:29 -05002521 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2522 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2523 if (ret < 0) {
2524 goto out_unlock;
2525 }
2526
Dave Airliead7f8a12014-06-05 14:01:32 +10002527 {
2528 struct drm_dp_payload reset_pay;
2529 reset_pay.start_slot = 0;
2530 reset_pay.num_slots = 0x3f;
2531 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2532 }
2533
Dave Airliead7f8a12014-06-05 14:01:32 +10002534 queue_work(system_long_wq, &mgr->work);
2535
2536 ret = 0;
2537 } else {
2538 /* disable MST on the device */
2539 mstb = mgr->mst_primary;
2540 mgr->mst_primary = NULL;
2541 /* this can fail if the device is gone */
2542 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2543 ret = 0;
2544 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2545 mgr->payload_mask = 0;
2546 set_bit(0, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10002547 mgr->vcpi_mask = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002548 }
2549
2550out_unlock:
2551 mutex_unlock(&mgr->lock);
2552 if (mstb)
Lyude Pauld0757af2019-01-10 19:53:28 -05002553 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002554 return ret;
2555
2556}
2557EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2558
2559/**
2560 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2561 * @mgr: manager to suspend
2562 *
2563 * This function tells the MST device that we can't handle UP messages
2564 * anymore. This should stop it from sending any since we are suspended.
2565 */
2566void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2567{
2568 mutex_lock(&mgr->lock);
2569 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2570 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2571 mutex_unlock(&mgr->lock);
Dave Airlie274d8352015-09-30 10:39:42 +10002572 flush_work(&mgr->work);
2573 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002574}
2575EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2576
2577/**
2578 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2579 * @mgr: manager to resume
2580 *
2581 * This will fetch DPCD and see if the device is still there,
2582 * if it is, it will rewrite the MSTM control bits, and return.
2583 *
2584 * if the device fails this returns -1, and the driver should do
2585 * a full MST reprobe, in case we were undocked.
2586 */
2587int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2588{
2589 int ret = 0;
2590
2591 mutex_lock(&mgr->lock);
2592
2593 if (mgr->mst_primary) {
2594 int sret;
Lyude1652fce2016-04-13 16:50:18 -04002595 u8 guid[16];
2596
Dave Airliead7f8a12014-06-05 14:01:32 +10002597 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2598 if (sret != DP_RECEIVER_CAP_SIZE) {
2599 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2600 ret = -1;
2601 goto out_unlock;
2602 }
2603
2604 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2605 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2606 if (ret < 0) {
2607 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2608 ret = -1;
2609 goto out_unlock;
2610 }
Lyude1652fce2016-04-13 16:50:18 -04002611
2612 /* Some hubs forget their guids after they resume */
2613 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2614 if (sret != 16) {
2615 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2616 ret = -1;
2617 goto out_unlock;
2618 }
2619 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2620
Dave Airliead7f8a12014-06-05 14:01:32 +10002621 ret = 0;
2622 } else
2623 ret = -1;
2624
2625out_unlock:
2626 mutex_unlock(&mgr->lock);
2627 return ret;
2628}
2629EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2630
Imre Deak636c4c32017-07-19 16:46:32 +03002631static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
Dave Airliead7f8a12014-06-05 14:01:32 +10002632{
2633 int len;
2634 u8 replyblock[32];
2635 int replylen, origlen, curreply;
2636 int ret;
2637 struct drm_dp_sideband_msg_rx *msg;
2638 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2639 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2640
2641 len = min(mgr->max_dpcd_transaction_bytes, 16);
2642 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2643 replyblock, len);
2644 if (ret != len) {
2645 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
Imre Deak636c4c32017-07-19 16:46:32 +03002646 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002647 }
2648 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2649 if (!ret) {
2650 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
Imre Deak636c4c32017-07-19 16:46:32 +03002651 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002652 }
2653 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2654
2655 origlen = replylen;
2656 replylen -= len;
2657 curreply = len;
2658 while (replylen > 0) {
2659 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2660 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2661 replyblock, len);
2662 if (ret != len) {
Imre Deak448421b2017-07-19 14:43:28 +03002663 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2664 len, ret);
Imre Deak636c4c32017-07-19 16:46:32 +03002665 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002666 }
Imre Deak448421b2017-07-19 14:43:28 +03002667
Dave Airliead7f8a12014-06-05 14:01:32 +10002668 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
Imre Deak448421b2017-07-19 14:43:28 +03002669 if (!ret) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002670 DRM_DEBUG_KMS("failed to build sideband msg\n");
Imre Deak636c4c32017-07-19 16:46:32 +03002671 return false;
Imre Deak448421b2017-07-19 14:43:28 +03002672 }
2673
Dave Airliead7f8a12014-06-05 14:01:32 +10002674 curreply += len;
2675 replylen -= len;
2676 }
Imre Deak636c4c32017-07-19 16:46:32 +03002677 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002678}
2679
2680static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2681{
2682 int ret = 0;
2683
Imre Deak636c4c32017-07-19 16:46:32 +03002684 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2685 memset(&mgr->down_rep_recv, 0,
2686 sizeof(struct drm_dp_sideband_msg_rx));
2687 return 0;
2688 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002689
2690 if (mgr->down_rep_recv.have_eomt) {
2691 struct drm_dp_sideband_msg_tx *txmsg;
2692 struct drm_dp_mst_branch *mstb;
2693 int slot = -1;
2694 mstb = drm_dp_get_mst_branch_device(mgr,
2695 mgr->down_rep_recv.initial_hdr.lct,
2696 mgr->down_rep_recv.initial_hdr.rad);
2697
2698 if (!mstb) {
2699 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2700 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2701 return 0;
2702 }
2703
2704 /* find the message */
2705 slot = mgr->down_rep_recv.initial_hdr.seqno;
2706 mutex_lock(&mgr->qlock);
2707 txmsg = mstb->tx_slots[slot];
2708 /* remove from slots */
2709 mutex_unlock(&mgr->qlock);
2710
2711 if (!txmsg) {
2712 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2713 mstb,
2714 mgr->down_rep_recv.initial_hdr.seqno,
2715 mgr->down_rep_recv.initial_hdr.lct,
2716 mgr->down_rep_recv.initial_hdr.rad[0],
2717 mgr->down_rep_recv.msg[0]);
Lyude Pauld0757af2019-01-10 19:53:28 -05002718 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002719 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2720 return 0;
2721 }
2722
2723 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2724 if (txmsg->reply.reply_type == 1) {
2725 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x, reason 0x%02x, nak data 0x%02x\n", txmsg->reply.req_type, txmsg->reply.u.nak.reason, txmsg->reply.u.nak.nak_data);
2726 }
2727
2728 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
Lyude Pauld0757af2019-01-10 19:53:28 -05002729 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002730
2731 mutex_lock(&mgr->qlock);
2732 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2733 mstb->tx_slots[slot] = NULL;
2734 mutex_unlock(&mgr->qlock);
2735
Chris Wilson68e989d2017-05-13 11:52:01 +01002736 wake_up_all(&mgr->tx_waitq);
Dave Airliead7f8a12014-06-05 14:01:32 +10002737 }
2738 return ret;
2739}
2740
2741static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2742{
2743 int ret = 0;
Imre Deak636c4c32017-07-19 16:46:32 +03002744
2745 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2746 memset(&mgr->up_req_recv, 0,
2747 sizeof(struct drm_dp_sideband_msg_rx));
2748 return 0;
2749 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002750
2751 if (mgr->up_req_recv.have_eomt) {
2752 struct drm_dp_sideband_msg_req_body msg;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002753 struct drm_dp_mst_branch *mstb = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002754 bool seqno;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002755
2756 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2757 mstb = drm_dp_get_mst_branch_device(mgr,
2758 mgr->up_req_recv.initial_hdr.lct,
2759 mgr->up_req_recv.initial_hdr.rad);
2760 if (!mstb) {
2761 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2762 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2763 return 0;
2764 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002765 }
2766
2767 seqno = mgr->up_req_recv.initial_hdr.seqno;
2768 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2769
2770 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002771 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2772
2773 if (!mstb)
2774 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2775
2776 if (!mstb) {
2777 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2778 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2779 return 0;
2780 }
2781
Dave Airliead7f8a12014-06-05 14:01:32 +10002782 drm_dp_update_port(mstb, &msg.u.conn_stat);
Hersen Wu5e93b822016-01-22 17:07:28 -05002783
Dave Airliead7f8a12014-06-05 14:01:32 +10002784 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 +01002785 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002786
Dave Airliead7f8a12014-06-05 14:01:32 +10002787 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002788 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2789 if (!mstb)
2790 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2791
2792 if (!mstb) {
2793 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2794 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2795 return 0;
2796 }
2797
Dave Airliead7f8a12014-06-05 14:01:32 +10002798 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2799 }
2800
Imre Deak7f8b3982017-07-19 14:43:29 +03002801 if (mstb)
Lyude Pauld0757af2019-01-10 19:53:28 -05002802 drm_dp_mst_topology_put_mstb(mstb);
Imre Deak7f8b3982017-07-19 14:43:29 +03002803
Dave Airliead7f8a12014-06-05 14:01:32 +10002804 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2805 }
2806 return ret;
2807}
2808
2809/**
2810 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2811 * @mgr: manager to notify irq for.
2812 * @esi: 4 bytes from SINK_COUNT_ESI
Daniel Vetter295ee852014-07-30 14:23:44 +02002813 * @handled: whether the hpd interrupt was consumed or not
Dave Airliead7f8a12014-06-05 14:01:32 +10002814 *
2815 * This should be called from the driver when it detects a short IRQ,
2816 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2817 * topology manager will process the sideband messages received as a result
2818 * of this.
2819 */
2820int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2821{
2822 int ret = 0;
2823 int sc;
2824 *handled = false;
2825 sc = esi[0] & 0x3f;
2826
2827 if (sc != mgr->sink_count) {
2828 mgr->sink_count = sc;
2829 *handled = true;
2830 }
2831
2832 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2833 ret = drm_dp_mst_handle_down_rep(mgr);
2834 *handled = true;
2835 }
2836
2837 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2838 ret |= drm_dp_mst_handle_up_req(mgr);
2839 *handled = true;
2840 }
2841
2842 drm_dp_mst_kick_tx(mgr);
2843 return ret;
2844}
2845EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2846
2847/**
2848 * drm_dp_mst_detect_port() - get connection status for an MST port
Daniel Vetter132d49d2016-07-15 21:48:04 +02002849 * @connector: DRM connector for this port
Dave Airliead7f8a12014-06-05 14:01:32 +10002850 * @mgr: manager for this port
2851 * @port: unverified pointer to a port
2852 *
2853 * This returns the current connection state for a port. It validates the
2854 * port pointer still exists so the caller doesn't require a reference
2855 */
Dave Airliec6a0aed2014-10-20 16:28:02 +10002856enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2857 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10002858{
2859 enum drm_connector_status status = connector_status_disconnected;
2860
2861 /* we need to search for the port in the mgr in case its gone */
Lyude Pauld0757af2019-01-10 19:53:28 -05002862 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002863 if (!port)
2864 return connector_status_disconnected;
2865
2866 if (!port->ddps)
2867 goto out;
2868
2869 switch (port->pdt) {
2870 case DP_PEER_DEVICE_NONE:
2871 case DP_PEER_DEVICE_MST_BRANCHING:
2872 break;
2873
2874 case DP_PEER_DEVICE_SST_SINK:
2875 status = connector_status_connected;
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002876 /* for logical ports - cache the EDID */
2877 if (port->port_num >= 8 && !port->cached_edid) {
2878 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2879 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002880 break;
2881 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2882 if (port->ldps)
2883 status = connector_status_connected;
2884 break;
2885 }
2886out:
Lyude Pauld0757af2019-01-10 19:53:28 -05002887 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002888 return status;
2889}
2890EXPORT_SYMBOL(drm_dp_mst_detect_port);
2891
2892/**
Libin Yangef8f9be2015-12-02 14:09:43 +08002893 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2894 * @mgr: manager for this port
2895 * @port: unverified pointer to a port.
2896 *
2897 * This returns whether the port supports audio or not.
2898 */
2899bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2900 struct drm_dp_mst_port *port)
2901{
2902 bool ret = false;
2903
Lyude Pauld0757af2019-01-10 19:53:28 -05002904 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Libin Yangef8f9be2015-12-02 14:09:43 +08002905 if (!port)
2906 return ret;
2907 ret = port->has_audio;
Lyude Pauld0757af2019-01-10 19:53:28 -05002908 drm_dp_mst_topology_put_port(port);
Libin Yangef8f9be2015-12-02 14:09:43 +08002909 return ret;
2910}
2911EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2912
2913/**
Dave Airliead7f8a12014-06-05 14:01:32 +10002914 * drm_dp_mst_get_edid() - get EDID for an MST port
2915 * @connector: toplevel connector to get EDID for
2916 * @mgr: manager for this port
2917 * @port: unverified pointer to a port.
2918 *
2919 * This returns an EDID for the port connected to a connector,
2920 * It validates the pointer still exists so the caller doesn't require a
2921 * reference.
2922 */
2923struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2924{
2925 struct edid *edid = NULL;
2926
2927 /* we need to search for the port in the mgr in case its gone */
Lyude Pauld0757af2019-01-10 19:53:28 -05002928 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002929 if (!port)
2930 return NULL;
2931
Dave Airliec6a0aed2014-10-20 16:28:02 +10002932 if (port->cached_edid)
2933 edid = drm_edid_duplicate(port->cached_edid);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002934 else {
2935 edid = drm_get_edid(connector, &port->aux.ddc);
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002936 drm_connector_set_tile_property(connector);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002937 }
Libin Yangef8f9be2015-12-02 14:09:43 +08002938 port->has_audio = drm_detect_monitor_audio(edid);
Lyude Pauld0757af2019-01-10 19:53:28 -05002939 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002940 return edid;
2941}
2942EXPORT_SYMBOL(drm_dp_mst_get_edid);
2943
2944/**
Lyude Paule4b0c862018-10-23 19:12:46 -04002945 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
Dave Airliead7f8a12014-06-05 14:01:32 +10002946 * @mgr: manager to use
2947 * @pbn: payload bandwidth to convert into slots.
Lyude Paule4b0c862018-10-23 19:12:46 -04002948 *
2949 * Calculate the number of VCPI slots that will be required for the given PBN
2950 * value. This function is deprecated, and should not be used in atomic
2951 * drivers.
2952 *
2953 * RETURNS:
2954 * The total slots required for this port, or error.
Dave Airliead7f8a12014-06-05 14:01:32 +10002955 */
2956int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2957 int pbn)
2958{
2959 int num_slots;
2960
2961 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2962
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07002963 /* max. time slots - one slot for MTP header */
2964 if (num_slots > 63)
Dave Airliead7f8a12014-06-05 14:01:32 +10002965 return -ENOSPC;
2966 return num_slots;
2967}
2968EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2969
2970static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002971 struct drm_dp_vcpi *vcpi, int pbn, int slots)
Dave Airliead7f8a12014-06-05 14:01:32 +10002972{
Dave Airliead7f8a12014-06-05 14:01:32 +10002973 int ret;
2974
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07002975 /* max. time slots - one slot for MTP header */
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002976 if (slots > 63)
Dave Airliead7f8a12014-06-05 14:01:32 +10002977 return -ENOSPC;
2978
2979 vcpi->pbn = pbn;
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002980 vcpi->aligned_pbn = slots * mgr->pbn_div;
2981 vcpi->num_slots = slots;
Dave Airliead7f8a12014-06-05 14:01:32 +10002982
2983 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2984 if (ret < 0)
2985 return ret;
2986 return 0;
2987}
2988
2989/**
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07002990 * drm_dp_atomic_find_vcpi_slots() - Find and add vcpi slots to the state
2991 * @state: global atomic state
2992 * @mgr: MST topology manager for the port
2993 * @port: port to find vcpi slots for
2994 * @pbn: bandwidth required for the mode in PBN
2995 *
2996 * RETURNS:
2997 * Total slots in the atomic state assigned for this port or error
2998 */
2999int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
3000 struct drm_dp_mst_topology_mgr *mgr,
3001 struct drm_dp_mst_port *port, int pbn)
3002{
3003 struct drm_dp_mst_topology_state *topology_state;
3004 int req_slots;
3005
3006 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
Ville Syrjälä56a91c42017-07-12 18:51:00 +03003007 if (IS_ERR(topology_state))
3008 return PTR_ERR(topology_state);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003009
Lyude Pauld0757af2019-01-10 19:53:28 -05003010 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003011 if (port == NULL)
3012 return -EINVAL;
3013 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3014 DRM_DEBUG_KMS("vcpi slots req=%d, avail=%d\n",
3015 req_slots, topology_state->avail_slots);
3016
3017 if (req_slots > topology_state->avail_slots) {
Lyude Pauld0757af2019-01-10 19:53:28 -05003018 drm_dp_mst_topology_put_port(port);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003019 return -ENOSPC;
3020 }
3021
3022 topology_state->avail_slots -= req_slots;
3023 DRM_DEBUG_KMS("vcpi slots avail=%d", topology_state->avail_slots);
3024
Lyude Pauld0757af2019-01-10 19:53:28 -05003025 drm_dp_mst_topology_put_port(port);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003026 return req_slots;
3027}
3028EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
3029
3030/**
3031 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
3032 * @state: global atomic state
3033 * @mgr: MST topology manager for the port
3034 * @slots: number of vcpi slots to release
3035 *
3036 * RETURNS:
3037 * 0 if @slots were added back to &drm_dp_mst_topology_state->avail_slots or
3038 * negative error code
3039 */
3040int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
3041 struct drm_dp_mst_topology_mgr *mgr,
3042 int slots)
3043{
3044 struct drm_dp_mst_topology_state *topology_state;
3045
3046 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
Ville Syrjälä56a91c42017-07-12 18:51:00 +03003047 if (IS_ERR(topology_state))
3048 return PTR_ERR(topology_state);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07003049
3050 /* We cannot rely on port->vcpi.num_slots to update
3051 * topology_state->avail_slots as the port may not exist if the parent
3052 * branch device was unplugged. This should be fixed by tracking
3053 * per-port slot allocation in drm_dp_mst_topology_state instead of
3054 * depending on the caller to tell us how many slots to release.
3055 */
3056 topology_state->avail_slots += slots;
3057 DRM_DEBUG_KMS("vcpi slots released=%d, avail=%d\n",
3058 slots, topology_state->avail_slots);
3059
3060 return 0;
3061}
3062EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
3063
3064/**
Dave Airliead7f8a12014-06-05 14:01:32 +10003065 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
3066 * @mgr: manager for this port
3067 * @port: port to allocate a virtual channel for.
3068 * @pbn: payload bandwidth number to request
3069 * @slots: returned number of slots for this PBN.
3070 */
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003071bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3072 struct drm_dp_mst_port *port, int pbn, int slots)
Dave Airliead7f8a12014-06-05 14:01:32 +10003073{
3074 int ret;
3075
Lyude Pauld0757af2019-01-10 19:53:28 -05003076 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003077 if (!port)
3078 return false;
3079
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003080 if (slots < 0)
3081 return false;
3082
Dave Airliead7f8a12014-06-05 14:01:32 +10003083 if (port->vcpi.vcpi > 0) {
Lyude Paule0ac7112019-01-10 19:53:26 -05003084 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
3085 port->vcpi.vcpi, port->vcpi.pbn, pbn);
Dave Airliead7f8a12014-06-05 14:01:32 +10003086 if (pbn == port->vcpi.pbn) {
Lyude Pauld0757af2019-01-10 19:53:28 -05003087 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003088 return true;
3089 }
3090 }
3091
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07003092 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
Dave Airliead7f8a12014-06-05 14:01:32 +10003093 if (ret) {
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07003094 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
Lyude Paule0ac7112019-01-10 19:53:26 -05003095 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10003096 goto out;
3097 }
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07003098 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
Lyude Paule0ac7112019-01-10 19:53:26 -05003099 pbn, port->vcpi.num_slots);
Dave Airliead7f8a12014-06-05 14:01:32 +10003100
Lyude Pauld0757af2019-01-10 19:53:28 -05003101 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003102 return true;
3103out:
3104 return false;
3105}
3106EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
3107
Dave Airlie87f59422015-02-24 09:23:55 +10003108int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3109{
3110 int slots = 0;
Lyude Pauld0757af2019-01-10 19:53:28 -05003111 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airlie87f59422015-02-24 09:23:55 +10003112 if (!port)
3113 return slots;
3114
3115 slots = port->vcpi.num_slots;
Lyude Pauld0757af2019-01-10 19:53:28 -05003116 drm_dp_mst_topology_put_port(port);
Dave Airlie87f59422015-02-24 09:23:55 +10003117 return slots;
3118}
3119EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
3120
Dave Airliead7f8a12014-06-05 14:01:32 +10003121/**
3122 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
3123 * @mgr: manager for this port
3124 * @port: unverified pointer to a port.
3125 *
3126 * This just resets the number of slots for the ports VCPI for later programming.
3127 */
3128void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3129{
Lyude Pauld0757af2019-01-10 19:53:28 -05003130 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003131 if (!port)
3132 return;
3133 port->vcpi.num_slots = 0;
Lyude Pauld0757af2019-01-10 19:53:28 -05003134 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003135}
3136EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
3137
3138/**
3139 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
3140 * @mgr: manager for this port
3141 * @port: unverified port to deallocate vcpi for
3142 */
Lyude Paul4afb8a22019-01-10 19:53:27 -05003143void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3144 struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10003145{
Lyude Pauld0757af2019-01-10 19:53:28 -05003146 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003147 if (!port)
3148 return;
3149
3150 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3151 port->vcpi.num_slots = 0;
3152 port->vcpi.pbn = 0;
3153 port->vcpi.aligned_pbn = 0;
3154 port->vcpi.vcpi = 0;
Lyude Pauld0757af2019-01-10 19:53:28 -05003155 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10003156}
3157EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
3158
3159static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
3160 int id, struct drm_dp_payload *payload)
3161{
3162 u8 payload_alloc[3], status;
3163 int ret;
3164 int retries = 0;
3165
3166 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
3167 DP_PAYLOAD_TABLE_UPDATED);
3168
3169 payload_alloc[0] = id;
3170 payload_alloc[1] = payload->start_slot;
3171 payload_alloc[2] = payload->num_slots;
3172
3173 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
3174 if (ret != 3) {
3175 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
3176 goto fail;
3177 }
3178
3179retry:
3180 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3181 if (ret < 0) {
3182 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3183 goto fail;
3184 }
3185
3186 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
3187 retries++;
3188 if (retries < 20) {
3189 usleep_range(10000, 20000);
3190 goto retry;
3191 }
3192 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
3193 ret = -EINVAL;
3194 goto fail;
3195 }
3196 ret = 0;
3197fail:
3198 return ret;
3199}
3200
3201
3202/**
3203 * drm_dp_check_act_status() - Check ACT handled status.
3204 * @mgr: manager to use
3205 *
3206 * Check the payload status bits in the DPCD for ACT handled completion.
3207 */
3208int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
3209{
3210 u8 status;
3211 int ret;
3212 int count = 0;
3213
3214 do {
3215 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3216
3217 if (ret < 0) {
3218 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3219 goto fail;
3220 }
3221
3222 if (status & DP_PAYLOAD_ACT_HANDLED)
3223 break;
3224 count++;
3225 udelay(100);
3226
3227 } while (count < 30);
3228
3229 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
3230 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
3231 ret = -EINVAL;
3232 goto fail;
3233 }
3234 return 0;
3235fail:
3236 return ret;
3237}
3238EXPORT_SYMBOL(drm_dp_check_act_status);
3239
3240/**
3241 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
3242 * @clock: dot clock for the mode
3243 * @bpp: bpp for the mode.
3244 *
3245 * This uses the formula in the spec to calculate the PBN value for a mode.
3246 */
3247int drm_dp_calc_pbn_mode(int clock, int bpp)
3248{
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003249 u64 kbps;
3250 s64 peak_kbps;
3251 u32 numerator;
3252 u32 denominator;
Dave Airliead7f8a12014-06-05 14:01:32 +10003253
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003254 kbps = clock * bpp;
Dave Airliead7f8a12014-06-05 14:01:32 +10003255
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003256 /*
3257 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
3258 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
3259 * common multiplier to render an integer PBN for all link rate/lane
3260 * counts combinations
3261 * calculate
3262 * peak_kbps *= (1006/1000)
3263 * peak_kbps *= (64/54)
3264 * peak_kbps *= 8 convert to bytes
3265 */
Dave Airliead7f8a12014-06-05 14:01:32 +10003266
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003267 numerator = 64 * 1006;
3268 denominator = 54 * 8 * 1000 * 1000;
Dave Airliead7f8a12014-06-05 14:01:32 +10003269
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003270 kbps *= numerator;
3271 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
3272
3273 return drm_fixp2int_ceil(peak_kbps);
Dave Airliead7f8a12014-06-05 14:01:32 +10003274}
3275EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
3276
3277static int test_calc_pbn_mode(void)
3278{
3279 int ret;
3280 ret = drm_dp_calc_pbn_mode(154000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003281 if (ret != 689) {
3282 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3283 154000, 30, 689, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10003284 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003285 }
Dave Airliead7f8a12014-06-05 14:01:32 +10003286 ret = drm_dp_calc_pbn_mode(234000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003287 if (ret != 1047) {
3288 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3289 234000, 30, 1047, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10003290 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05003291 }
3292 ret = drm_dp_calc_pbn_mode(297000, 24);
3293 if (ret != 1063) {
3294 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3295 297000, 24, 1063, ret);
3296 return -EINVAL;
3297 }
Dave Airliead7f8a12014-06-05 14:01:32 +10003298 return 0;
3299}
3300
3301/* we want to kick the TX after we've ack the up/down IRQs. */
3302static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
3303{
3304 queue_work(system_long_wq, &mgr->tx_work);
3305}
3306
3307static void drm_dp_mst_dump_mstb(struct seq_file *m,
3308 struct drm_dp_mst_branch *mstb)
3309{
3310 struct drm_dp_mst_port *port;
3311 int tabs = mstb->lct;
3312 char prefix[10];
3313 int i;
3314
3315 for (i = 0; i < tabs; i++)
3316 prefix[i] = '\t';
3317 prefix[i] = '\0';
3318
3319 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
3320 list_for_each_entry(port, &mstb->ports, next) {
Jim Bride51108f22016-04-14 10:18:36 -07003321 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 +10003322 if (port->mstb)
3323 drm_dp_mst_dump_mstb(m, port->mstb);
3324 }
3325}
3326
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003327#define DP_PAYLOAD_TABLE_SIZE 64
3328
Dave Airliead7f8a12014-06-05 14:01:32 +10003329static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
3330 char *buf)
3331{
Dave Airliead7f8a12014-06-05 14:01:32 +10003332 int i;
Joe Perches46466b02017-05-30 16:35:37 -07003333
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003334 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
Joe Perches46466b02017-05-30 16:35:37 -07003335 if (drm_dp_dpcd_read(mgr->aux,
3336 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
3337 &buf[i], 16) != 16)
3338 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10003339 }
Joe Perches46466b02017-05-30 16:35:37 -07003340 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10003341}
3342
Jim Bride51108f22016-04-14 10:18:36 -07003343static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3344 struct drm_dp_mst_port *port, char *name,
3345 int namelen)
3346{
3347 struct edid *mst_edid;
3348
3349 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3350 drm_edid_get_monitor_name(mst_edid, name, namelen);
3351}
3352
Dave Airliead7f8a12014-06-05 14:01:32 +10003353/**
3354 * drm_dp_mst_dump_topology(): dump topology to seq file.
3355 * @m: seq_file to dump output to
3356 * @mgr: manager to dump current topology for.
3357 *
3358 * helper to dump MST topology to a seq file for debugfs.
3359 */
3360void drm_dp_mst_dump_topology(struct seq_file *m,
3361 struct drm_dp_mst_topology_mgr *mgr)
3362{
3363 int i;
3364 struct drm_dp_mst_port *port;
Jim Bride51108f22016-04-14 10:18:36 -07003365
Dave Airliead7f8a12014-06-05 14:01:32 +10003366 mutex_lock(&mgr->lock);
3367 if (mgr->mst_primary)
3368 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
3369
3370 /* dump VCPIs */
3371 mutex_unlock(&mgr->lock);
3372
3373 mutex_lock(&mgr->payload_lock);
Jim Bride51108f22016-04-14 10:18:36 -07003374 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
3375 mgr->max_payloads);
Dave Airliead7f8a12014-06-05 14:01:32 +10003376
3377 for (i = 0; i < mgr->max_payloads; i++) {
3378 if (mgr->proposed_vcpis[i]) {
Jim Bride51108f22016-04-14 10:18:36 -07003379 char name[14];
3380
Dave Airliead7f8a12014-06-05 14:01:32 +10003381 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
Jim Bride51108f22016-04-14 10:18:36 -07003382 fetch_monitor_name(mgr, port, name, sizeof(name));
3383 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3384 port->port_num, port->vcpi.vcpi,
3385 port->vcpi.num_slots,
3386 (*name != 0) ? name : "Unknown");
Dave Airliead7f8a12014-06-05 14:01:32 +10003387 } else
Jim Bride51108f22016-04-14 10:18:36 -07003388 seq_printf(m, "vcpi %d:unused\n", i);
Dave Airliead7f8a12014-06-05 14:01:32 +10003389 }
3390 for (i = 0; i < mgr->max_payloads; i++) {
3391 seq_printf(m, "payload %d: %d, %d, %d\n",
3392 i,
3393 mgr->payloads[i].payload_state,
3394 mgr->payloads[i].start_slot,
3395 mgr->payloads[i].num_slots);
3396
3397
3398 }
3399 mutex_unlock(&mgr->payload_lock);
3400
3401 mutex_lock(&mgr->lock);
3402 if (mgr->mst_primary) {
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003403 u8 buf[DP_PAYLOAD_TABLE_SIZE];
Dave Airliead7f8a12014-06-05 14:01:32 +10003404 int ret;
Joe Perches46466b02017-05-30 16:35:37 -07003405
Dave Airliead7f8a12014-06-05 14:01:32 +10003406 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
Joe Perches46466b02017-05-30 16:35:37 -07003407 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003408 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
Joe Perches46466b02017-05-30 16:35:37 -07003409 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003410 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
Joe Perches46466b02017-05-30 16:35:37 -07003411 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003412
Dave Airlie44790462015-07-14 11:33:31 +10003413 /* dump the standard OUI branch header */
3414 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
Joe Perches46466b02017-05-30 16:35:37 -07003415 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
Jim Bride51108f22016-04-14 10:18:36 -07003416 for (i = 0x3; i < 0x8 && buf[i]; i++)
Dave Airlie44790462015-07-14 11:33:31 +10003417 seq_printf(m, "%c", buf[i]);
Joe Perches46466b02017-05-30 16:35:37 -07003418 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
3419 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
3420 if (dump_dp_payload_table(mgr, buf))
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003421 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003422 }
3423
3424 mutex_unlock(&mgr->lock);
3425
3426}
3427EXPORT_SYMBOL(drm_dp_mst_dump_topology);
3428
3429static void drm_dp_tx_work(struct work_struct *work)
3430{
3431 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
3432
3433 mutex_lock(&mgr->qlock);
Daniel Vettercb021a32016-07-15 21:48:03 +02003434 if (!list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10003435 process_single_down_tx_qlock(mgr);
3436 mutex_unlock(&mgr->qlock);
3437}
3438
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003439static void drm_dp_destroy_connector_work(struct work_struct *work)
3440{
3441 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 +02003442 struct drm_dp_mst_port *port;
Dave Airliedf4839f2015-09-16 10:37:28 +10003443 bool send_hotplug = false;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003444 /*
3445 * Not a regular list traverse as we have to drop the destroy
3446 * connector lock before destroying the connector, to avoid AB->BA
3447 * ordering between this lock and the config mutex.
3448 */
3449 for (;;) {
3450 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003451 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
3452 if (!port) {
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003453 mutex_unlock(&mgr->destroy_connector_lock);
3454 break;
3455 }
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003456 list_del(&port->next);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003457 mutex_unlock(&mgr->destroy_connector_lock);
3458
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003459 INIT_LIST_HEAD(&port->next);
3460
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003461 mgr->cbs->destroy_connector(mgr, port->connector);
3462
3463 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +03003464 port->pdt = DP_PEER_DEVICE_NONE;
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003465
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003466 if (!port->input && port->vcpi.vcpi > 0) {
Andrey Grodzovskyfd2d2ba2016-05-25 16:45:43 -04003467 drm_dp_mst_reset_vcpi_slots(mgr, port);
3468 drm_dp_update_payload_part1(mgr);
3469 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003470 }
3471
Lyude Paulebcc0e62019-01-10 19:53:29 -05003472 drm_dp_mst_put_port_malloc(port);
Dave Airliedf4839f2015-09-16 10:37:28 +10003473 send_hotplug = true;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003474 }
Dave Airliedf4839f2015-09-16 10:37:28 +10003475 if (send_hotplug)
Daniel Vetter16bff572018-11-28 23:12:34 +01003476 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003477}
3478
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003479static struct drm_private_state *
3480drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003481{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003482 struct drm_dp_mst_topology_state *state;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003483
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003484 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
3485 if (!state)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003486 return NULL;
3487
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003488 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
3489
3490 return &state->base;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003491}
3492
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003493static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
3494 struct drm_private_state *state)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003495{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003496 struct drm_dp_mst_topology_state *mst_state =
3497 to_dp_mst_topology_state(state);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003498
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003499 kfree(mst_state);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003500}
3501
3502static const struct drm_private_state_funcs mst_state_funcs = {
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003503 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
3504 .atomic_destroy_state = drm_dp_mst_destroy_state,
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003505};
3506
3507/**
3508 * drm_atomic_get_mst_topology_state: get MST topology state
3509 *
3510 * @state: global atomic state
3511 * @mgr: MST topology manager, also the private object in this case
3512 *
3513 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3514 * state vtable so that the private object state returned is that of a MST
3515 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3516 * to care of the locking, so warn if don't hold the connection_mutex.
3517 *
3518 * RETURNS:
3519 *
3520 * The MST topology state or error pointer.
3521 */
3522struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
3523 struct drm_dp_mst_topology_mgr *mgr)
3524{
3525 struct drm_device *dev = mgr->dev;
3526
3527 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003528 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003529}
3530EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
3531
Dave Airliead7f8a12014-06-05 14:01:32 +10003532/**
3533 * drm_dp_mst_topology_mgr_init - initialise a topology manager
3534 * @mgr: manager struct to initialise
3535 * @dev: device providing this structure - for i2c addition.
3536 * @aux: DP helper aux channel to talk to this device
3537 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3538 * @max_payloads: maximum number of payloads this GPU can source
3539 * @conn_base_id: the connector object ID the MST device is connected to.
3540 *
3541 * Return 0 for success, or negative error code on failure
3542 */
3543int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08003544 struct drm_device *dev, struct drm_dp_aux *aux,
Dave Airliead7f8a12014-06-05 14:01:32 +10003545 int max_dpcd_transaction_bytes,
3546 int max_payloads, int conn_base_id)
3547{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003548 struct drm_dp_mst_topology_state *mst_state;
3549
Dave Airliead7f8a12014-06-05 14:01:32 +10003550 mutex_init(&mgr->lock);
3551 mutex_init(&mgr->qlock);
3552 mutex_init(&mgr->payload_lock);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003553 mutex_init(&mgr->destroy_connector_lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10003554 INIT_LIST_HEAD(&mgr->tx_msg_downq);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003555 INIT_LIST_HEAD(&mgr->destroy_connector_list);
Dave Airliead7f8a12014-06-05 14:01:32 +10003556 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
3557 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003558 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10003559 init_waitqueue_head(&mgr->tx_waitq);
3560 mgr->dev = dev;
3561 mgr->aux = aux;
3562 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
3563 mgr->max_payloads = max_payloads;
3564 mgr->conn_base_id = conn_base_id;
Imre Deak4d6a10d2016-01-29 14:44:29 +02003565 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3566 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3567 return -EINVAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10003568 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3569 if (!mgr->payloads)
3570 return -ENOMEM;
3571 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3572 if (!mgr->proposed_vcpis)
3573 return -ENOMEM;
3574 set_bit(0, &mgr->payload_mask);
Imre Deak441388a2016-01-29 14:44:28 +02003575 if (test_calc_pbn_mode() < 0)
3576 DRM_ERROR("MST PBN self-test failed\n");
3577
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003578 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
3579 if (mst_state == NULL)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003580 return -ENOMEM;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003581
3582 mst_state->mgr = mgr;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003583
3584 /* max. time slots - one slot for MTP header */
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003585 mst_state->avail_slots = 63;
3586
Rob Clarkb962a122018-10-22 14:31:22 +02003587 drm_atomic_private_obj_init(dev, &mgr->base,
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003588 &mst_state->base,
3589 &mst_state_funcs);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003590
Dave Airliead7f8a12014-06-05 14:01:32 +10003591 return 0;
3592}
3593EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3594
3595/**
3596 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3597 * @mgr: manager to destroy
3598 */
3599void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3600{
Lyude Paulf536e002018-12-11 18:50:26 -05003601 drm_dp_mst_topology_mgr_set_mst(mgr, false);
Dave Airlie274d8352015-09-30 10:39:42 +10003602 flush_work(&mgr->work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003603 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10003604 mutex_lock(&mgr->payload_lock);
3605 kfree(mgr->payloads);
3606 mgr->payloads = NULL;
3607 kfree(mgr->proposed_vcpis);
3608 mgr->proposed_vcpis = NULL;
3609 mutex_unlock(&mgr->payload_lock);
3610 mgr->dev = NULL;
3611 mgr->aux = NULL;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003612 drm_atomic_private_obj_fini(&mgr->base);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003613 mgr->funcs = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10003614}
3615EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3616
Ville Syrjäläcb8ce712018-09-28 21:04:00 +03003617static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
3618{
3619 int i;
3620
3621 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
3622 return false;
3623
3624 for (i = 0; i < num - 1; i++) {
3625 if (msgs[i].flags & I2C_M_RD ||
3626 msgs[i].len > 0xff)
3627 return false;
3628 }
3629
3630 return msgs[num - 1].flags & I2C_M_RD &&
3631 msgs[num - 1].len <= 0xff;
3632}
3633
Dave Airliead7f8a12014-06-05 14:01:32 +10003634/* I2C device */
3635static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3636 int num)
3637{
3638 struct drm_dp_aux *aux = adapter->algo_data;
3639 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3640 struct drm_dp_mst_branch *mstb;
3641 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3642 unsigned int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10003643 struct drm_dp_sideband_msg_req_body msg;
3644 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3645 int ret;
3646
Lyude Pauld0757af2019-01-10 19:53:28 -05003647 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Dave Airliead7f8a12014-06-05 14:01:32 +10003648 if (!mstb)
3649 return -EREMOTEIO;
3650
Ville Syrjäläcb8ce712018-09-28 21:04:00 +03003651 if (!remote_i2c_read_ok(msgs, num)) {
Dave Airliead7f8a12014-06-05 14:01:32 +10003652 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3653 ret = -EIO;
3654 goto out;
3655 }
3656
Dave Airlieae491542015-10-14 18:51:17 +10003657 memset(&msg, 0, sizeof(msg));
Dave Airliead7f8a12014-06-05 14:01:32 +10003658 msg.req_type = DP_REMOTE_I2C_READ;
3659 msg.u.i2c_read.num_transactions = num - 1;
3660 msg.u.i2c_read.port_number = port->port_num;
3661 for (i = 0; i < num - 1; i++) {
3662 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3663 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3664 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
Ville Syrjäläc978ae92018-09-28 21:03:59 +03003665 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
Dave Airliead7f8a12014-06-05 14:01:32 +10003666 }
3667 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3668 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3669
3670 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3671 if (!txmsg) {
3672 ret = -ENOMEM;
3673 goto out;
3674 }
3675
3676 txmsg->dst = mstb;
3677 drm_dp_encode_sideband_req(&msg, txmsg);
3678
3679 drm_dp_queue_down_tx(mgr, txmsg);
3680
3681 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3682 if (ret > 0) {
3683
3684 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3685 ret = -EREMOTEIO;
3686 goto out;
3687 }
3688 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3689 ret = -EIO;
3690 goto out;
3691 }
3692 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3693 ret = num;
3694 }
3695out:
3696 kfree(txmsg);
Lyude Pauld0757af2019-01-10 19:53:28 -05003697 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10003698 return ret;
3699}
3700
3701static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3702{
3703 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3704 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3705 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3706 I2C_FUNC_10BIT_ADDR;
3707}
3708
3709static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3710 .functionality = drm_dp_mst_i2c_functionality,
3711 .master_xfer = drm_dp_mst_i2c_xfer,
3712};
3713
3714/**
3715 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3716 * @aux: DisplayPort AUX channel
3717 *
3718 * Returns 0 on success or a negative error code on failure.
3719 */
3720static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3721{
3722 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3723 aux->ddc.algo_data = aux;
3724 aux->ddc.retries = 3;
3725
3726 aux->ddc.class = I2C_CLASS_DDC;
3727 aux->ddc.owner = THIS_MODULE;
3728 aux->ddc.dev.parent = aux->dev;
3729 aux->ddc.dev.of_node = aux->dev->of_node;
3730
3731 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3732 sizeof(aux->ddc.name));
3733
3734 return i2c_add_adapter(&aux->ddc);
3735}
3736
3737/**
3738 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3739 * @aux: DisplayPort AUX channel
3740 */
3741static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3742{
3743 i2c_del_adapter(&aux->ddc);
3744}