blob: 074e985093ca307644cde4e2d1379d7fb51905a3 [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);
853 kref_init(&mstb->kref);
854 return mstb;
855}
856
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500857static void drm_dp_free_mst_port(struct kref *kref);
858
859static void drm_dp_free_mst_branch_device(struct kref *kref)
860{
861 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
862 if (mstb->port_parent) {
863 if (list_empty(&mstb->port_parent->next))
864 kref_put(&mstb->port_parent->kref, drm_dp_free_mst_port);
865 }
866 kfree(mstb);
867}
868
Dave Airliead7f8a12014-06-05 14:01:32 +1000869static void drm_dp_destroy_mst_branch_device(struct kref *kref)
870{
871 struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
872 struct drm_dp_mst_port *port, *tmp;
873 bool wake_tx = false;
874
Dave Airliead7f8a12014-06-05 14:01:32 +1000875 /*
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500876 * init kref again to be used by ports to remove mst branch when it is
877 * not needed anymore
878 */
879 kref_init(kref);
880
881 if (mstb->port_parent && list_empty(&mstb->port_parent->next))
882 kref_get(&mstb->port_parent->kref);
883
884 /*
Dave Airliead7f8a12014-06-05 14:01:32 +1000885 * destroy all ports - don't need lock
886 * as there are no more references to the mst branch
887 * device at this point.
888 */
889 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
890 list_del(&port->next);
Lyude Pauld0757af2019-01-10 19:53:28 -0500891 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +1000892 }
893
894 /* drop any tx slots msg */
895 mutex_lock(&mstb->mgr->qlock);
896 if (mstb->tx_slots[0]) {
897 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
898 mstb->tx_slots[0] = NULL;
899 wake_tx = true;
900 }
901 if (mstb->tx_slots[1]) {
902 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
903 mstb->tx_slots[1] = NULL;
904 wake_tx = true;
905 }
906 mutex_unlock(&mstb->mgr->qlock);
907
908 if (wake_tx)
Chris Wilson68e989d2017-05-13 11:52:01 +0100909 wake_up_all(&mstb->mgr->tx_waitq);
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500910
911 kref_put(kref, drm_dp_free_mst_branch_device);
Dave Airliead7f8a12014-06-05 14:01:32 +1000912}
913
Lyude Pauld0757af2019-01-10 19:53:28 -0500914static void drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +1000915{
916 kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
917}
918
919
920static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
921{
Daniel Vetter03913592014-12-08 22:55:22 +0100922 struct drm_dp_mst_branch *mstb;
923
Dave Airliead7f8a12014-06-05 14:01:32 +1000924 switch (old_pdt) {
925 case DP_PEER_DEVICE_DP_LEGACY_CONV:
926 case DP_PEER_DEVICE_SST_SINK:
927 /* remove i2c over sideband */
928 drm_dp_mst_unregister_i2c_bus(&port->aux);
929 break;
930 case DP_PEER_DEVICE_MST_BRANCHING:
Daniel Vetter03913592014-12-08 22:55:22 +0100931 mstb = port->mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +1000932 port->mstb = NULL;
Lyude Pauld0757af2019-01-10 19:53:28 -0500933 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +1000934 break;
935 }
936}
937
938static void drm_dp_destroy_port(struct kref *kref)
939{
940 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
941 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
Dave Airliedf4839f2015-09-16 10:37:28 +1000942
Dave Airliead7f8a12014-06-05 14:01:32 +1000943 if (!port->input) {
944 port->vcpi.num_slots = 0;
Dave Airliec6a0aed2014-10-20 16:28:02 +1000945
946 kfree(port->cached_edid);
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000947
Dave Airliedf4839f2015-09-16 10:37:28 +1000948 /*
949 * The only time we don't have a connector
950 * on an output port is if the connector init
951 * fails.
952 */
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000953 if (port->connector) {
Dave Airliedf4839f2015-09-16 10:37:28 +1000954 /* we can't destroy the connector here, as
955 * we might be holding the mode_config.mutex
956 * from an EDID retrieval */
957
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000958 mutex_lock(&mgr->destroy_connector_lock);
Mykola Lysenko91a25e42016-01-27 09:39:36 -0500959 kref_get(&port->parent->kref);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +0200960 list_add(&port->next, &mgr->destroy_connector_list);
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000961 mutex_unlock(&mgr->destroy_connector_lock);
962 schedule_work(&mgr->destroy_connector_work);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +0200963 return;
Dave Airlie6b8eeca2015-06-15 10:34:28 +1000964 }
Dave Airliedf4839f2015-09-16 10:37:28 +1000965 /* no need to clean up vcpi
966 * as if we have no connector we never setup a vcpi */
Dave Airliead7f8a12014-06-05 14:01:32 +1000967 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +0300968 port->pdt = DP_PEER_DEVICE_NONE;
Dave Airliead7f8a12014-06-05 14:01:32 +1000969 }
970 kfree(port);
Dave Airliead7f8a12014-06-05 14:01:32 +1000971}
972
Lyude Pauld0757af2019-01-10 19:53:28 -0500973static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +1000974{
975 kref_put(&port->kref, drm_dp_destroy_port);
976}
977
Lyude Pauld0757af2019-01-10 19:53:28 -0500978static struct drm_dp_mst_branch *
979drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
980 struct drm_dp_mst_branch *to_find)
Dave Airliead7f8a12014-06-05 14:01:32 +1000981{
982 struct drm_dp_mst_port *port;
983 struct drm_dp_mst_branch *rmstb;
984 if (to_find == mstb) {
985 kref_get(&mstb->kref);
986 return mstb;
987 }
988 list_for_each_entry(port, &mstb->ports, next) {
989 if (port->mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -0500990 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
991 port->mstb, to_find);
Dave Airliead7f8a12014-06-05 14:01:32 +1000992 if (rmstb)
993 return rmstb;
994 }
995 }
996 return NULL;
997}
998
Lyude Pauld0757af2019-01-10 19:53:28 -0500999static struct drm_dp_mst_branch *
1000drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1001 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001002{
1003 struct drm_dp_mst_branch *rmstb = NULL;
1004 mutex_lock(&mgr->lock);
1005 if (mgr->mst_primary)
Lyude Pauld0757af2019-01-10 19:53:28 -05001006 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1007 mgr->mst_primary, mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001008 mutex_unlock(&mgr->lock);
1009 return rmstb;
1010}
1011
1012static struct drm_dp_mst_port *drm_dp_mst_get_port_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_port *to_find)
1013{
1014 struct drm_dp_mst_port *port, *mport;
1015
1016 list_for_each_entry(port, &mstb->ports, next) {
1017 if (port == to_find) {
1018 kref_get(&port->kref);
1019 return port;
1020 }
1021 if (port->mstb) {
1022 mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
1023 if (mport)
1024 return mport;
1025 }
1026 }
1027 return NULL;
1028}
1029
Lyude Pauld0757af2019-01-10 19:53:28 -05001030static struct drm_dp_mst_port *
1031drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1032 struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10001033{
1034 struct drm_dp_mst_port *rport = NULL;
1035 mutex_lock(&mgr->lock);
1036 if (mgr->mst_primary)
1037 rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
1038 mutex_unlock(&mgr->lock);
1039 return rport;
1040}
1041
1042static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1043{
1044 struct drm_dp_mst_port *port;
1045
1046 list_for_each_entry(port, &mstb->ports, next) {
1047 if (port->port_num == port_num) {
1048 kref_get(&port->kref);
1049 return port;
1050 }
1051 }
1052
1053 return NULL;
1054}
1055
1056/*
1057 * calculate a new RAD for this MST branch device
1058 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1059 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1060 */
1061static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1062 u8 *rad)
1063{
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001064 int parent_lct = port->parent->lct;
Dave Airliead7f8a12014-06-05 14:01:32 +10001065 int shift = 4;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001066 int idx = (parent_lct - 1) / 2;
1067 if (parent_lct > 1) {
1068 memcpy(rad, port->parent->rad, idx + 1);
1069 shift = (parent_lct % 2) ? 4 : 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001070 } else
1071 rad[0] = 0;
1072
1073 rad[idx] |= port->port_num << shift;
Mykola Lysenko75af4c82015-12-25 16:14:47 +08001074 return parent_lct + 1;
Dave Airliead7f8a12014-06-05 14:01:32 +10001075}
1076
1077/*
1078 * return sends link address for new mstb
1079 */
1080static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1081{
1082 int ret;
1083 u8 rad[6], lct;
1084 bool send_link = false;
1085 switch (port->pdt) {
1086 case DP_PEER_DEVICE_DP_LEGACY_CONV:
1087 case DP_PEER_DEVICE_SST_SINK:
1088 /* add i2c over sideband */
1089 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1090 break;
1091 case DP_PEER_DEVICE_MST_BRANCHING:
1092 lct = drm_dp_calculate_rad(port, rad);
1093
1094 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
Joe Moriarty22a07032018-02-12 14:51:42 -05001095 if (port->mstb) {
1096 port->mstb->mgr = port->mgr;
1097 port->mstb->port_parent = port;
Dave Airliead7f8a12014-06-05 14:01:32 +10001098
Joe Moriarty22a07032018-02-12 14:51:42 -05001099 send_link = true;
1100 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001101 break;
1102 }
1103 return send_link;
1104}
1105
Hersen Wu5e93b822016-01-22 17:07:28 -05001106static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
Dave Airliead7f8a12014-06-05 14:01:32 +10001107{
1108 int ret;
Hersen Wu5e93b822016-01-22 17:07:28 -05001109
1110 memcpy(mstb->guid, guid, 16);
1111
1112 if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1113 if (mstb->port_parent) {
1114 ret = drm_dp_send_dpcd_write(
1115 mstb->mgr,
1116 mstb->port_parent,
1117 DP_GUID,
1118 16,
1119 mstb->guid);
1120 } else {
1121
1122 ret = drm_dp_dpcd_write(
1123 mstb->mgr->aux,
1124 DP_GUID,
1125 mstb->guid,
1126 16);
Dave Airliead7f8a12014-06-05 14:01:32 +10001127 }
1128 }
1129}
1130
Dave Airlie1c960872015-09-16 11:04:49 +10001131static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1132 int pnum,
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001133 char *proppath,
1134 size_t proppath_size)
Dave Airliead7f8a12014-06-05 14:01:32 +10001135{
1136 int i;
1137 char temp[8];
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001138 snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
Dave Airliead7f8a12014-06-05 14:01:32 +10001139 for (i = 0; i < (mstb->lct - 1); i++) {
1140 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001141 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001142 snprintf(temp, sizeof(temp), "-%d", port_num);
1143 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001144 }
Dave Airlie1c960872015-09-16 11:04:49 +10001145 snprintf(temp, sizeof(temp), "-%d", pnum);
Rickard Strandqvistd87af4d2014-10-12 00:02:32 +02001146 strlcat(proppath, temp, proppath_size);
Dave Airliead7f8a12014-06-05 14:01:32 +10001147}
1148
1149static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001150 struct drm_device *dev,
Dave Airliead7f8a12014-06-05 14:01:32 +10001151 struct drm_dp_link_addr_reply_port *port_msg)
1152{
1153 struct drm_dp_mst_port *port;
1154 bool ret;
1155 bool created = false;
1156 int old_pdt = 0;
1157 int old_ddps = 0;
1158 port = drm_dp_get_port(mstb, port_msg->port_number);
1159 if (!port) {
1160 port = kzalloc(sizeof(*port), GFP_KERNEL);
1161 if (!port)
1162 return;
1163 kref_init(&port->kref);
1164 port->parent = mstb;
1165 port->port_num = port_msg->port_number;
1166 port->mgr = mstb->mgr;
1167 port->aux.name = "DPMST";
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08001168 port->aux.dev = dev->dev;
Dave Airliead7f8a12014-06-05 14:01:32 +10001169 created = true;
1170 } else {
1171 old_pdt = port->pdt;
1172 old_ddps = port->ddps;
1173 }
1174
1175 port->pdt = port_msg->peer_device_type;
1176 port->input = port_msg->input_port;
1177 port->mcs = port_msg->mcs;
1178 port->ddps = port_msg->ddps;
1179 port->ldps = port_msg->legacy_device_plug_status;
1180 port->dpcd_rev = port_msg->dpcd_revision;
1181 port->num_sdp_streams = port_msg->num_sdp_streams;
1182 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
Dave Airliead7f8a12014-06-05 14:01:32 +10001183
1184 /* manage mstb port lists with mgr lock - take a reference
1185 for this list */
1186 if (created) {
1187 mutex_lock(&mstb->mgr->lock);
1188 kref_get(&port->kref);
1189 list_add(&port->next, &mstb->ports);
1190 mutex_unlock(&mstb->mgr->lock);
1191 }
1192
1193 if (old_ddps != port->ddps) {
1194 if (port->ddps) {
Lyude Paul3d76df62019-01-10 19:53:24 -05001195 if (!port->input) {
1196 drm_dp_send_enum_path_resources(mstb->mgr,
1197 mstb, port);
1198 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001199 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001200 port->available_pbn = 0;
Lyude Paul3d76df62019-01-10 19:53:24 -05001201 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001202 }
1203
1204 if (old_pdt != port->pdt && !port->input) {
1205 drm_dp_port_teardown_pdt(port, old_pdt);
1206
1207 ret = drm_dp_port_setup_pdt(port);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001208 if (ret == true)
Dave Airliead7f8a12014-06-05 14:01:32 +10001209 drm_dp_send_link_address(mstb->mgr, port->mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10001210 }
1211
1212 if (created && !port->input) {
1213 char proppath[255];
Dave Airlie1c960872015-09-16 11:04:49 +10001214
Lyude Paul3d76df62019-01-10 19:53:24 -05001215 build_mst_prop_path(mstb, port->port_num, proppath,
1216 sizeof(proppath));
1217 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
1218 port,
1219 proppath);
Dave Airliedf4839f2015-09-16 10:37:28 +10001220 if (!port->connector) {
1221 /* remove it from the port list */
1222 mutex_lock(&mstb->mgr->lock);
1223 list_del(&port->next);
1224 mutex_unlock(&mstb->mgr->lock);
1225 /* drop port list reference */
Lyude Pauld0757af2019-01-10 19:53:28 -05001226 drm_dp_mst_topology_put_port(port);
Dave Airliedf4839f2015-09-16 10:37:28 +10001227 goto out;
1228 }
Ville Syrjälä4da5caa2016-10-26 12:05:55 +03001229 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1230 port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1231 port->port_num >= DP_MST_LOGICAL_PORT_0) {
Lyude Paul3d76df62019-01-10 19:53:24 -05001232 port->cached_edid = drm_get_edid(port->connector,
1233 &port->aux.ddc);
Daniel Vetter97e14fb2018-07-09 10:40:08 +02001234 drm_connector_set_tile_property(port->connector);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001235 }
Dave Airlied9515c52015-09-16 17:55:23 +10001236 (*mstb->mgr->cbs->register_connector)(port->connector);
Dave Airliead7f8a12014-06-05 14:01:32 +10001237 }
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001238
Dave Airliedf4839f2015-09-16 10:37:28 +10001239out:
Dave Airliead7f8a12014-06-05 14:01:32 +10001240 /* put reference to this port */
Lyude Pauld0757af2019-01-10 19:53:28 -05001241 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001242}
1243
1244static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1245 struct drm_dp_connection_status_notify *conn_stat)
1246{
1247 struct drm_dp_mst_port *port;
1248 int old_pdt;
1249 int old_ddps;
1250 bool dowork = false;
1251 port = drm_dp_get_port(mstb, conn_stat->port_number);
1252 if (!port)
1253 return;
1254
1255 old_ddps = port->ddps;
1256 old_pdt = port->pdt;
1257 port->pdt = conn_stat->peer_device_type;
1258 port->mcs = conn_stat->message_capability_status;
1259 port->ldps = conn_stat->legacy_device_plug_status;
1260 port->ddps = conn_stat->displayport_device_plug_status;
1261
1262 if (old_ddps != port->ddps) {
1263 if (port->ddps) {
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001264 dowork = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001265 } else {
Dave Airliead7f8a12014-06-05 14:01:32 +10001266 port->available_pbn = 0;
1267 }
1268 }
1269 if (old_pdt != port->pdt && !port->input) {
1270 drm_dp_port_teardown_pdt(port, old_pdt);
1271
1272 if (drm_dp_port_setup_pdt(port))
1273 dowork = true;
1274 }
1275
Lyude Pauld0757af2019-01-10 19:53:28 -05001276 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001277 if (dowork)
1278 queue_work(system_long_wq, &mstb->mgr->work);
1279
1280}
1281
1282static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1283 u8 lct, u8 *rad)
1284{
1285 struct drm_dp_mst_branch *mstb;
1286 struct drm_dp_mst_port *port;
1287 int i;
1288 /* find the port by iterating down */
Dave Airlie9eb1e572015-06-22 14:40:44 +10001289
1290 mutex_lock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001291 mstb = mgr->mst_primary;
1292
Stanislav Lisovskiy23d80032018-11-09 11:00:12 +02001293 if (!mstb)
1294 goto out;
1295
Dave Airliead7f8a12014-06-05 14:01:32 +10001296 for (i = 0; i < lct - 1; i++) {
1297 int shift = (i % 2) ? 0 : 4;
Mykola Lysenko7a11a332015-12-25 16:14:48 +08001298 int port_num = (rad[i / 2] >> shift) & 0xf;
Dave Airliead7f8a12014-06-05 14:01:32 +10001299
1300 list_for_each_entry(port, &mstb->ports, next) {
1301 if (port->port_num == port_num) {
Adam Richter30730c72015-10-16 03:33:02 -07001302 mstb = port->mstb;
1303 if (!mstb) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001304 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
Adam Richter30730c72015-10-16 03:33:02 -07001305 goto out;
Dave Airliead7f8a12014-06-05 14:01:32 +10001306 }
1307
Dave Airliead7f8a12014-06-05 14:01:32 +10001308 break;
1309 }
1310 }
1311 }
1312 kref_get(&mstb->kref);
Adam Richter30730c72015-10-16 03:33:02 -07001313out:
Dave Airlie9eb1e572015-06-22 14:40:44 +10001314 mutex_unlock(&mgr->lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10001315 return mstb;
1316}
1317
Mykola Lysenkobd934322015-12-18 17:14:42 -05001318static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1319 struct drm_dp_mst_branch *mstb,
1320 uint8_t *guid)
1321{
1322 struct drm_dp_mst_branch *found_mstb;
1323 struct drm_dp_mst_port *port;
1324
Hersen Wu5e93b822016-01-22 17:07:28 -05001325 if (memcmp(mstb->guid, guid, 16) == 0)
1326 return mstb;
1327
1328
Mykola Lysenkobd934322015-12-18 17:14:42 -05001329 list_for_each_entry(port, &mstb->ports, next) {
1330 if (!port->mstb)
1331 continue;
1332
Mykola Lysenkobd934322015-12-18 17:14:42 -05001333 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1334
1335 if (found_mstb)
1336 return found_mstb;
1337 }
1338
1339 return NULL;
1340}
1341
Lyude Paulde6d6812019-01-10 19:53:25 -05001342static struct drm_dp_mst_branch *
1343drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
1344 uint8_t *guid)
Mykola Lysenkobd934322015-12-18 17:14:42 -05001345{
1346 struct drm_dp_mst_branch *mstb;
1347
1348 /* find the port by iterating down */
1349 mutex_lock(&mgr->lock);
1350
Hersen Wu5e93b822016-01-22 17:07:28 -05001351 mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
Mykola Lysenkobd934322015-12-18 17:14:42 -05001352
1353 if (mstb)
1354 kref_get(&mstb->kref);
1355
1356 mutex_unlock(&mgr->lock);
1357 return mstb;
1358}
1359
Dave Airliead7f8a12014-06-05 14:01:32 +10001360static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1361 struct drm_dp_mst_branch *mstb)
1362{
1363 struct drm_dp_mst_port *port;
Daniel Vetter9254ec42015-06-22 17:31:59 +10001364 struct drm_dp_mst_branch *mstb_child;
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001365 if (!mstb->link_address_sent)
Dave Airliead7f8a12014-06-05 14:01:32 +10001366 drm_dp_send_link_address(mgr, mstb);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001367
Dave Airliead7f8a12014-06-05 14:01:32 +10001368 list_for_each_entry(port, &mstb->ports, next) {
1369 if (port->input)
1370 continue;
1371
Dave Airlie8ae22cb2016-02-17 11:36:38 +10001372 if (!port->ddps)
Dave Airliead7f8a12014-06-05 14:01:32 +10001373 continue;
1374
1375 if (!port->available_pbn)
1376 drm_dp_send_enum_path_resources(mgr, mstb, port);
1377
Daniel Vetter9254ec42015-06-22 17:31:59 +10001378 if (port->mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001379 mstb_child = drm_dp_mst_topology_get_mstb_validated(
1380 mgr, port->mstb);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001381 if (mstb_child) {
1382 drm_dp_check_and_send_link_address(mgr, mstb_child);
Lyude Pauld0757af2019-01-10 19:53:28 -05001383 drm_dp_mst_topology_put_mstb(mstb_child);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001384 }
1385 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001386 }
1387}
1388
1389static void drm_dp_mst_link_probe_work(struct work_struct *work)
1390{
1391 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001392 struct drm_dp_mst_branch *mstb;
Dave Airliead7f8a12014-06-05 14:01:32 +10001393
Daniel Vetter9254ec42015-06-22 17:31:59 +10001394 mutex_lock(&mgr->lock);
1395 mstb = mgr->mst_primary;
1396 if (mstb) {
1397 kref_get(&mstb->kref);
1398 }
1399 mutex_unlock(&mgr->lock);
1400 if (mstb) {
1401 drm_dp_check_and_send_link_address(mgr, mstb);
Lyude Pauld0757af2019-01-10 19:53:28 -05001402 drm_dp_mst_topology_put_mstb(mstb);
Daniel Vetter9254ec42015-06-22 17:31:59 +10001403 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001404}
1405
1406static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1407 u8 *guid)
1408{
Ville Syrjäläe38e1282017-07-12 18:52:54 +03001409 u64 salt;
Dave Airliead7f8a12014-06-05 14:01:32 +10001410
Ville Syrjäläe38e1282017-07-12 18:52:54 +03001411 if (memchr_inv(guid, 0, 16))
1412 return true;
1413
1414 salt = get_jiffies_64();
1415
1416 memcpy(&guid[0], &salt, sizeof(u64));
1417 memcpy(&guid[8], &salt, sizeof(u64));
1418
1419 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10001420}
1421
1422#if 0
1423static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1424{
1425 struct drm_dp_sideband_msg_req_body req;
1426
1427 req.req_type = DP_REMOTE_DPCD_READ;
1428 req.u.dpcd_read.port_number = port_num;
1429 req.u.dpcd_read.dpcd_address = offset;
1430 req.u.dpcd_read.num_bytes = num_bytes;
1431 drm_dp_encode_sideband_req(&req, msg);
1432
1433 return 0;
1434}
1435#endif
1436
1437static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1438 bool up, u8 *msg, int len)
1439{
1440 int ret;
1441 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1442 int tosend, total, offset;
1443 int retries = 0;
1444
1445retry:
1446 total = len;
1447 offset = 0;
1448 do {
1449 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1450
1451 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1452 &msg[offset],
1453 tosend);
1454 if (ret != tosend) {
1455 if (ret == -EIO && retries < 5) {
1456 retries++;
1457 goto retry;
1458 }
1459 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10001460
1461 return -EIO;
1462 }
1463 offset += tosend;
1464 total -= tosend;
1465 } while (total > 0);
1466 return 0;
1467}
1468
1469static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1470 struct drm_dp_sideband_msg_tx *txmsg)
1471{
1472 struct drm_dp_mst_branch *mstb = txmsg->dst;
Mykola Lysenkobd934322015-12-18 17:14:42 -05001473 u8 req_type;
Dave Airliead7f8a12014-06-05 14:01:32 +10001474
1475 /* both msg slots are full */
1476 if (txmsg->seqno == -1) {
1477 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1478 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1479 return -EAGAIN;
1480 }
1481 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1482 txmsg->seqno = mstb->last_seqno;
1483 mstb->last_seqno ^= 1;
1484 } else if (mstb->tx_slots[0] == NULL)
1485 txmsg->seqno = 0;
1486 else
1487 txmsg->seqno = 1;
1488 mstb->tx_slots[txmsg->seqno] = txmsg;
1489 }
Mykola Lysenkobd934322015-12-18 17:14:42 -05001490
1491 req_type = txmsg->msg[0] & 0x7f;
1492 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1493 req_type == DP_RESOURCE_STATUS_NOTIFY)
1494 hdr->broadcast = 1;
1495 else
1496 hdr->broadcast = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001497 hdr->path_msg = txmsg->path_msg;
1498 hdr->lct = mstb->lct;
1499 hdr->lcr = mstb->lct - 1;
1500 if (mstb->lct > 1)
1501 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1502 hdr->seqno = txmsg->seqno;
1503 return 0;
1504}
1505/*
1506 * process a single block of the next message in the sideband queue
1507 */
1508static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1509 struct drm_dp_sideband_msg_tx *txmsg,
1510 bool up)
1511{
1512 u8 chunk[48];
1513 struct drm_dp_sideband_msg_hdr hdr;
1514 int len, space, idx, tosend;
1515 int ret;
1516
Damien Lespiaubf3719c2014-07-14 12:13:18 +01001517 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1518
Dave Airliead7f8a12014-06-05 14:01:32 +10001519 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1520 txmsg->seqno = -1;
1521 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1522 }
1523
1524 /* make hdr from dst mst - for replies use seqno
1525 otherwise assign one */
1526 ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1527 if (ret < 0)
1528 return ret;
1529
1530 /* amount left to send in this message */
1531 len = txmsg->cur_len - txmsg->cur_offset;
1532
1533 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1534 space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1535
1536 tosend = min(len, space);
1537 if (len == txmsg->cur_len)
1538 hdr.somt = 1;
1539 if (space >= len)
1540 hdr.eomt = 1;
1541
1542
1543 hdr.msg_len = tosend + 1;
1544 drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1545 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1546 /* add crc at end */
1547 drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1548 idx += tosend + 1;
1549
1550 ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1551 if (ret) {
1552 DRM_DEBUG_KMS("sideband msg failed to send\n");
1553 return ret;
1554 }
1555
1556 txmsg->cur_offset += tosend;
1557 if (txmsg->cur_offset == txmsg->cur_len) {
1558 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1559 return 1;
1560 }
1561 return 0;
1562}
1563
Dave Airliead7f8a12014-06-05 14:01:32 +10001564static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1565{
1566 struct drm_dp_sideband_msg_tx *txmsg;
1567 int ret;
1568
Daniel Vettercd961bb2015-01-28 10:02:23 +01001569 WARN_ON(!mutex_is_locked(&mgr->qlock));
1570
Dave Airliead7f8a12014-06-05 14:01:32 +10001571 /* construct a chunk from the first msg in the tx_msg queue */
Daniel Vettercb021a32016-07-15 21:48:03 +02001572 if (list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10001573 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001574
1575 txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1576 ret = process_single_tx_qlock(mgr, txmsg, false);
1577 if (ret == 1) {
1578 /* txmsg is sent it should be in the slots now */
1579 list_del(&txmsg->next);
1580 } else if (ret) {
1581 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1582 list_del(&txmsg->next);
1583 if (txmsg->seqno != -1)
1584 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1585 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
Chris Wilson68e989d2017-05-13 11:52:01 +01001586 wake_up_all(&mgr->tx_waitq);
Dave Airliead7f8a12014-06-05 14:01:32 +10001587 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001588}
1589
1590/* called holding qlock */
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001591static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1592 struct drm_dp_sideband_msg_tx *txmsg)
Dave Airliead7f8a12014-06-05 14:01:32 +10001593{
Dave Airliead7f8a12014-06-05 14:01:32 +10001594 int ret;
1595
1596 /* construct a chunk from the first msg in the tx_msg queue */
Dave Airliead7f8a12014-06-05 14:01:32 +10001597 ret = process_single_tx_qlock(mgr, txmsg, true);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001598
1599 if (ret != 1)
Dave Airliead7f8a12014-06-05 14:01:32 +10001600 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05001601
1602 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001603}
1604
1605static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1606 struct drm_dp_sideband_msg_tx *txmsg)
1607{
1608 mutex_lock(&mgr->qlock);
1609 list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
Daniel Vettercb021a32016-07-15 21:48:03 +02001610 if (list_is_singular(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10001611 process_single_down_tx_qlock(mgr);
1612 mutex_unlock(&mgr->qlock);
1613}
1614
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001615static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1616 struct drm_dp_mst_branch *mstb)
Dave Airliead7f8a12014-06-05 14:01:32 +10001617{
1618 int len;
1619 struct drm_dp_sideband_msg_tx *txmsg;
1620 int ret;
1621
1622 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1623 if (!txmsg)
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001624 return;
Dave Airliead7f8a12014-06-05 14:01:32 +10001625
1626 txmsg->dst = mstb;
1627 len = build_link_address(txmsg);
1628
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001629 mstb->link_address_sent = true;
Dave Airliead7f8a12014-06-05 14:01:32 +10001630 drm_dp_queue_down_tx(mgr, txmsg);
1631
1632 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1633 if (ret > 0) {
1634 int i;
1635
1636 if (txmsg->reply.reply_type == 1)
1637 DRM_DEBUG_KMS("link address nak received\n");
1638 else {
1639 DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1640 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1641 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,
1642 txmsg->reply.u.link_addr.ports[i].input_port,
1643 txmsg->reply.u.link_addr.ports[i].peer_device_type,
1644 txmsg->reply.u.link_addr.ports[i].port_number,
1645 txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1646 txmsg->reply.u.link_addr.ports[i].mcs,
1647 txmsg->reply.u.link_addr.ports[i].ddps,
1648 txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1649 txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1650 txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1651 }
Hersen Wu5e93b822016-01-22 17:07:28 -05001652
1653 drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1654
Dave Airliead7f8a12014-06-05 14:01:32 +10001655 for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1656 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1657 }
Daniel Vetter16bff572018-11-28 23:12:34 +01001658 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airliead7f8a12014-06-05 14:01:32 +10001659 }
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001660 } else {
1661 mstb->link_address_sent = false;
Dave Airliead7f8a12014-06-05 14:01:32 +10001662 DRM_DEBUG_KMS("link address failed %d\n", ret);
Dave Airlie68d8c9f2015-09-06 18:53:00 +10001663 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001664
1665 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10001666}
1667
1668static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1669 struct drm_dp_mst_branch *mstb,
1670 struct drm_dp_mst_port *port)
1671{
1672 int len;
1673 struct drm_dp_sideband_msg_tx *txmsg;
1674 int ret;
1675
1676 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1677 if (!txmsg)
1678 return -ENOMEM;
1679
1680 txmsg->dst = mstb;
1681 len = build_enum_path_resources(txmsg, port->port_num);
1682
1683 drm_dp_queue_down_tx(mgr, txmsg);
1684
1685 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1686 if (ret > 0) {
1687 if (txmsg->reply.reply_type == 1)
1688 DRM_DEBUG_KMS("enum path resources nak received\n");
1689 else {
1690 if (port->port_num != txmsg->reply.u.path_resources.port_number)
1691 DRM_ERROR("got incorrect port in response\n");
1692 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,
1693 txmsg->reply.u.path_resources.avail_payload_bw_number);
1694 port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1695 }
1696 }
1697
1698 kfree(txmsg);
1699 return 0;
1700}
1701
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001702static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
1703{
1704 if (!mstb->port_parent)
1705 return NULL;
1706
1707 if (mstb->port_parent->mstb != mstb)
1708 return mstb->port_parent;
1709
1710 return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
1711}
1712
1713static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
1714 struct drm_dp_mst_branch *mstb,
1715 int *port_num)
1716{
1717 struct drm_dp_mst_branch *rmstb = NULL;
1718 struct drm_dp_mst_port *found_port;
1719 mutex_lock(&mgr->lock);
1720 if (mgr->mst_primary) {
1721 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
1722
1723 if (found_port) {
1724 rmstb = found_port->parent;
1725 kref_get(&rmstb->kref);
1726 *port_num = found_port->port_num;
1727 }
1728 }
1729 mutex_unlock(&mgr->lock);
1730 return rmstb;
1731}
1732
Thierry Reding8fa6a422014-07-21 13:23:57 +02001733static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1734 struct drm_dp_mst_port *port,
1735 int id,
1736 int pbn)
Dave Airliead7f8a12014-06-05 14:01:32 +10001737{
1738 struct drm_dp_sideband_msg_tx *txmsg;
1739 struct drm_dp_mst_branch *mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001740 int len, ret, port_num;
Libin Yangef8f9be2015-12-02 14:09:43 +08001741 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1742 int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10001743
Lyude Pauld0757af2019-01-10 19:53:28 -05001744 port = drm_dp_mst_topology_get_port_validated(mgr, port);
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04001745 if (!port)
1746 return -EINVAL;
1747
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001748 port_num = port->port_num;
Lyude Pauld0757af2019-01-10 19:53:28 -05001749 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001750 if (!mstb) {
Lyude Paulde6d6812019-01-10 19:53:25 -05001751 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
1752 port->parent,
1753 &port_num);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001754
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04001755 if (!mstb) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001756 drm_dp_mst_topology_put_port(port);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001757 return -EINVAL;
cpaul@redhat.comdeba0a22016-04-04 19:58:47 -04001758 }
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001759 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001760
1761 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1762 if (!txmsg) {
1763 ret = -ENOMEM;
1764 goto fail_put;
1765 }
1766
Libin Yangef8f9be2015-12-02 14:09:43 +08001767 for (i = 0; i < port->num_sdp_streams; i++)
1768 sinks[i] = i;
1769
Dave Airliead7f8a12014-06-05 14:01:32 +10001770 txmsg->dst = mstb;
Mykola Lysenko91a25e42016-01-27 09:39:36 -05001771 len = build_allocate_payload(txmsg, port_num,
Dave Airliead7f8a12014-06-05 14:01:32 +10001772 id,
Libin Yangef8f9be2015-12-02 14:09:43 +08001773 pbn, port->num_sdp_streams, sinks);
Dave Airliead7f8a12014-06-05 14:01:32 +10001774
1775 drm_dp_queue_down_tx(mgr, txmsg);
1776
1777 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1778 if (ret > 0) {
Lyude Paulde6d6812019-01-10 19:53:25 -05001779 if (txmsg->reply.reply_type == 1)
Dave Airliead7f8a12014-06-05 14:01:32 +10001780 ret = -EINVAL;
Lyude Paulde6d6812019-01-10 19:53:25 -05001781 else
Dave Airliead7f8a12014-06-05 14:01:32 +10001782 ret = 0;
1783 }
1784 kfree(txmsg);
1785fail_put:
Lyude Pauld0757af2019-01-10 19:53:28 -05001786 drm_dp_mst_topology_put_mstb(mstb);
1787 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001788 return ret;
1789}
1790
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07001791int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
1792 struct drm_dp_mst_port *port, bool power_up)
1793{
1794 struct drm_dp_sideband_msg_tx *txmsg;
1795 int len, ret;
1796
Lyude Pauld0757af2019-01-10 19:53:28 -05001797 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07001798 if (!port)
1799 return -EINVAL;
1800
1801 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1802 if (!txmsg) {
Lyude Pauld0757af2019-01-10 19:53:28 -05001803 drm_dp_mst_topology_put_port(port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07001804 return -ENOMEM;
1805 }
1806
1807 txmsg->dst = port->parent;
1808 len = build_power_updown_phy(txmsg, port->port_num, power_up);
1809 drm_dp_queue_down_tx(mgr, txmsg);
1810
1811 ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
1812 if (ret > 0) {
1813 if (txmsg->reply.reply_type == 1)
1814 ret = -EINVAL;
1815 else
1816 ret = 0;
1817 }
1818 kfree(txmsg);
Lyude Pauld0757af2019-01-10 19:53:28 -05001819 drm_dp_mst_topology_put_port(port);
Dhinakaran Pandiyan0bb9c2b2017-09-06 17:14:58 -07001820
1821 return ret;
1822}
1823EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
1824
Dave Airliead7f8a12014-06-05 14:01:32 +10001825static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1826 int id,
1827 struct drm_dp_payload *payload)
1828{
1829 int ret;
1830
1831 ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1832 if (ret < 0) {
1833 payload->payload_state = 0;
1834 return ret;
1835 }
1836 payload->payload_state = DP_PAYLOAD_LOCAL;
1837 return 0;
1838}
1839
Thierry Reding8fa6a422014-07-21 13:23:57 +02001840static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1841 struct drm_dp_mst_port *port,
1842 int id,
1843 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001844{
1845 int ret;
1846 ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1847 if (ret < 0)
1848 return ret;
1849 payload->payload_state = DP_PAYLOAD_REMOTE;
1850 return ret;
1851}
1852
Thierry Reding8fa6a422014-07-21 13:23:57 +02001853static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1854 struct drm_dp_mst_port *port,
1855 int id,
1856 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001857{
1858 DRM_DEBUG_KMS("\n");
1859 /* its okay for these to fail */
1860 if (port) {
1861 drm_dp_payload_send_msg(mgr, port, id, 0);
1862 }
1863
1864 drm_dp_dpcd_write_payload(mgr, id, payload);
Dave Airliedfda0df2014-08-06 16:26:21 +10001865 payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10001866 return 0;
1867}
1868
Thierry Reding8fa6a422014-07-21 13:23:57 +02001869static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1870 int id,
1871 struct drm_dp_payload *payload)
Dave Airliead7f8a12014-06-05 14:01:32 +10001872{
1873 payload->payload_state = 0;
1874 return 0;
1875}
1876
1877/**
1878 * drm_dp_update_payload_part1() - Execute payload update part 1
1879 * @mgr: manager to use.
1880 *
1881 * This iterates over all proposed virtual channels, and tries to
1882 * allocate space in the link for them. For 0->slots transitions,
1883 * this step just writes the VCPI to the MST device. For slots->0
1884 * transitions, this writes the updated VCPIs and removes the
1885 * remote VC payloads.
1886 *
1887 * after calling this the driver should generate ACT and payload
1888 * packets.
1889 */
1890int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1891{
Dave Airliedfda0df2014-08-06 16:26:21 +10001892 int i, j;
Dave Airliead7f8a12014-06-05 14:01:32 +10001893 int cur_slots = 1;
1894 struct drm_dp_payload req_payload;
1895 struct drm_dp_mst_port *port;
1896
1897 mutex_lock(&mgr->payload_lock);
1898 for (i = 0; i < mgr->max_payloads; i++) {
Lyude Paul706246c2018-12-13 20:25:31 -05001899 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
1900 struct drm_dp_payload *payload = &mgr->payloads[i];
1901
Dave Airliead7f8a12014-06-05 14:01:32 +10001902 /* solve the current payloads - compare to the hw ones
1903 - update the hw view */
1904 req_payload.start_slot = cur_slots;
Lyude Paul706246c2018-12-13 20:25:31 -05001905 if (vcpi) {
1906 port = container_of(vcpi, struct drm_dp_mst_port,
1907 vcpi);
Lyude Pauld0757af2019-01-10 19:53:28 -05001908 port = drm_dp_mst_topology_get_port_validated(mgr,
1909 port);
cpaul@redhat.com263efde2016-04-22 16:08:46 -04001910 if (!port) {
1911 mutex_unlock(&mgr->payload_lock);
1912 return -EINVAL;
1913 }
Lyude Paul706246c2018-12-13 20:25:31 -05001914 req_payload.num_slots = vcpi->num_slots;
1915 req_payload.vcpi = vcpi->vcpi;
Dave Airliead7f8a12014-06-05 14:01:32 +10001916 } else {
1917 port = NULL;
1918 req_payload.num_slots = 0;
1919 }
Dave Airliedfda0df2014-08-06 16:26:21 +10001920
Lyude Paul706246c2018-12-13 20:25:31 -05001921 payload->start_slot = req_payload.start_slot;
Dave Airliead7f8a12014-06-05 14:01:32 +10001922 /* work out what is required to happen with this payload */
Lyude Paul706246c2018-12-13 20:25:31 -05001923 if (payload->num_slots != req_payload.num_slots) {
Dave Airliead7f8a12014-06-05 14:01:32 +10001924
1925 /* need to push an update for this payload */
1926 if (req_payload.num_slots) {
Lyude Paul706246c2018-12-13 20:25:31 -05001927 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
1928 &req_payload);
1929 payload->num_slots = req_payload.num_slots;
1930 payload->vcpi = req_payload.vcpi;
1931
1932 } else if (payload->num_slots) {
1933 payload->num_slots = 0;
1934 drm_dp_destroy_payload_step1(mgr, port,
1935 payload->vcpi,
1936 payload);
1937 req_payload.payload_state =
1938 payload->payload_state;
1939 payload->start_slot = 0;
Dave Airliedfda0df2014-08-06 16:26:21 +10001940 }
Lyude Paul706246c2018-12-13 20:25:31 -05001941 payload->payload_state = req_payload.payload_state;
Dave Airliead7f8a12014-06-05 14:01:32 +10001942 }
1943 cur_slots += req_payload.num_slots;
cpaul@redhat.com263efde2016-04-22 16:08:46 -04001944
1945 if (port)
Lyude Pauld0757af2019-01-10 19:53:28 -05001946 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10001947 }
Dave Airliedfda0df2014-08-06 16:26:21 +10001948
1949 for (i = 0; i < mgr->max_payloads; i++) {
Lyude Paul706246c2018-12-13 20:25:31 -05001950 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL)
1951 continue;
Dave Airliedfda0df2014-08-06 16:26:21 +10001952
Lyude Paul706246c2018-12-13 20:25:31 -05001953 DRM_DEBUG_KMS("removing payload %d\n", i);
1954 for (j = i; j < mgr->max_payloads - 1; j++) {
1955 mgr->payloads[j] = mgr->payloads[j + 1];
1956 mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1957
1958 if (mgr->proposed_vcpis[j] &&
1959 mgr->proposed_vcpis[j]->num_slots) {
1960 set_bit(j + 1, &mgr->payload_mask);
1961 } else {
1962 clear_bit(j + 1, &mgr->payload_mask);
1963 }
Dave Airliedfda0df2014-08-06 16:26:21 +10001964 }
Lyude Paul706246c2018-12-13 20:25:31 -05001965
1966 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
1967 sizeof(struct drm_dp_payload));
1968 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1969 clear_bit(mgr->max_payloads, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10001970 }
Dave Airliead7f8a12014-06-05 14:01:32 +10001971 mutex_unlock(&mgr->payload_lock);
1972
1973 return 0;
1974}
1975EXPORT_SYMBOL(drm_dp_update_payload_part1);
1976
1977/**
1978 * drm_dp_update_payload_part2() - Execute payload update part 2
1979 * @mgr: manager to use.
1980 *
1981 * This iterates over all proposed virtual channels, and tries to
1982 * allocate space in the link for them. For 0->slots transitions,
1983 * this step writes the remote VC payload commands. For slots->0
1984 * this just resets some internal state.
1985 */
1986int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1987{
1988 struct drm_dp_mst_port *port;
1989 int i;
Damien Lespiau7389ad42014-07-14 11:53:44 +01001990 int ret = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10001991 mutex_lock(&mgr->payload_lock);
1992 for (i = 0; i < mgr->max_payloads; i++) {
1993
1994 if (!mgr->proposed_vcpis[i])
1995 continue;
1996
1997 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1998
1999 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2000 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10002001 ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10002002 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
Dave Airliedfda0df2014-08-06 16:26:21 +10002003 ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
Dave Airliead7f8a12014-06-05 14:01:32 +10002004 }
2005 if (ret) {
2006 mutex_unlock(&mgr->payload_lock);
2007 return ret;
2008 }
2009 }
2010 mutex_unlock(&mgr->payload_lock);
2011 return 0;
2012}
2013EXPORT_SYMBOL(drm_dp_update_payload_part2);
2014
2015#if 0 /* unused as of yet */
2016static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2017 struct drm_dp_mst_port *port,
2018 int offset, int size)
2019{
2020 int len;
2021 struct drm_dp_sideband_msg_tx *txmsg;
2022
2023 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2024 if (!txmsg)
2025 return -ENOMEM;
2026
2027 len = build_dpcd_read(txmsg, port->port_num, 0, 8);
2028 txmsg->dst = port->parent;
2029
2030 drm_dp_queue_down_tx(mgr, txmsg);
2031
2032 return 0;
2033}
2034#endif
2035
2036static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2037 struct drm_dp_mst_port *port,
2038 int offset, int size, u8 *bytes)
2039{
2040 int len;
2041 int ret;
2042 struct drm_dp_sideband_msg_tx *txmsg;
2043 struct drm_dp_mst_branch *mstb;
2044
Lyude Pauld0757af2019-01-10 19:53:28 -05002045 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Dave Airliead7f8a12014-06-05 14:01:32 +10002046 if (!mstb)
2047 return -EINVAL;
2048
2049 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2050 if (!txmsg) {
2051 ret = -ENOMEM;
2052 goto fail_put;
2053 }
2054
2055 len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2056 txmsg->dst = mstb;
2057
2058 drm_dp_queue_down_tx(mgr, txmsg);
2059
2060 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2061 if (ret > 0) {
2062 if (txmsg->reply.reply_type == 1) {
2063 ret = -EINVAL;
2064 } else
2065 ret = 0;
2066 }
2067 kfree(txmsg);
2068fail_put:
Lyude Pauld0757af2019-01-10 19:53:28 -05002069 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002070 return ret;
2071}
2072
2073static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2074{
2075 struct drm_dp_sideband_msg_reply_body reply;
2076
Mykola Lysenkoa68d2432015-12-07 13:55:51 -05002077 reply.reply_type = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002078 reply.req_type = req_type;
2079 drm_dp_encode_sideband_reply(&reply, msg);
2080 return 0;
2081}
2082
2083static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2084 struct drm_dp_mst_branch *mstb,
2085 int req_type, int seqno, bool broadcast)
2086{
2087 struct drm_dp_sideband_msg_tx *txmsg;
2088
2089 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2090 if (!txmsg)
2091 return -ENOMEM;
2092
2093 txmsg->dst = mstb;
2094 txmsg->seqno = seqno;
2095 drm_dp_encode_up_ack_reply(txmsg, req_type);
2096
2097 mutex_lock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05002098
2099 process_single_up_tx_qlock(mgr, txmsg);
2100
Dave Airliead7f8a12014-06-05 14:01:32 +10002101 mutex_unlock(&mgr->qlock);
Mykola Lysenko1f16ee7f2015-12-18 17:14:43 -05002102
2103 kfree(txmsg);
Dave Airliead7f8a12014-06-05 14:01:32 +10002104 return 0;
2105}
2106
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002107static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2108 int dp_link_count,
2109 int *out)
Dave Airliead7f8a12014-06-05 14:01:32 +10002110{
2111 switch (dp_link_bw) {
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002112 default:
2113 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2114 dp_link_bw, dp_link_count);
2115 return false;
2116
Dave Airliead7f8a12014-06-05 14:01:32 +10002117 case DP_LINK_BW_1_62:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002118 *out = 3 * dp_link_count;
2119 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002120 case DP_LINK_BW_2_7:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002121 *out = 5 * dp_link_count;
2122 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002123 case DP_LINK_BW_5_4:
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002124 *out = 10 * dp_link_count;
2125 break;
Manasi Navaree0bd8782018-01-22 14:43:10 -08002126 case DP_LINK_BW_8_1:
2127 *out = 15 * dp_link_count;
2128 break;
Dave Airliead7f8a12014-06-05 14:01:32 +10002129 }
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002130 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002131}
2132
2133/**
2134 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2135 * @mgr: manager to set state for
2136 * @mst_state: true to enable MST on this connector - false to disable.
2137 *
2138 * This is called by the driver when it detects an MST capable device plugged
2139 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2140 */
2141int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2142{
2143 int ret = 0;
2144 struct drm_dp_mst_branch *mstb = NULL;
2145
2146 mutex_lock(&mgr->lock);
2147 if (mst_state == mgr->mst_state)
2148 goto out_unlock;
2149
2150 mgr->mst_state = mst_state;
2151 /* set the device into MST mode */
2152 if (mst_state) {
2153 WARN_ON(mgr->mst_primary);
2154
2155 /* get dpcd info */
2156 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2157 if (ret != DP_RECEIVER_CAP_SIZE) {
2158 DRM_DEBUG_KMS("failed to read DPCD\n");
2159 goto out_unlock;
2160 }
2161
Chris Wilsonb853fdb2014-11-12 10:13:37 +00002162 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2163 mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2164 &mgr->pbn_div)) {
2165 ret = -EINVAL;
2166 goto out_unlock;
2167 }
2168
Dave Airliead7f8a12014-06-05 14:01:32 +10002169 /* add initial branch device at LCT 1 */
2170 mstb = drm_dp_add_mst_branch_device(1, NULL);
2171 if (mstb == NULL) {
2172 ret = -ENOMEM;
2173 goto out_unlock;
2174 }
2175 mstb->mgr = mgr;
2176
2177 /* give this the main reference */
2178 mgr->mst_primary = mstb;
2179 kref_get(&mgr->mst_primary->kref);
2180
Andrey Grodzovskyc175cd12016-01-22 17:07:29 -05002181 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2182 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2183 if (ret < 0) {
2184 goto out_unlock;
2185 }
2186
Dave Airliead7f8a12014-06-05 14:01:32 +10002187 {
2188 struct drm_dp_payload reset_pay;
2189 reset_pay.start_slot = 0;
2190 reset_pay.num_slots = 0x3f;
2191 drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2192 }
2193
Dave Airliead7f8a12014-06-05 14:01:32 +10002194 queue_work(system_long_wq, &mgr->work);
2195
2196 ret = 0;
2197 } else {
2198 /* disable MST on the device */
2199 mstb = mgr->mst_primary;
2200 mgr->mst_primary = NULL;
2201 /* this can fail if the device is gone */
2202 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2203 ret = 0;
2204 memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2205 mgr->payload_mask = 0;
2206 set_bit(0, &mgr->payload_mask);
Dave Airliedfda0df2014-08-06 16:26:21 +10002207 mgr->vcpi_mask = 0;
Dave Airliead7f8a12014-06-05 14:01:32 +10002208 }
2209
2210out_unlock:
2211 mutex_unlock(&mgr->lock);
2212 if (mstb)
Lyude Pauld0757af2019-01-10 19:53:28 -05002213 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002214 return ret;
2215
2216}
2217EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2218
2219/**
2220 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2221 * @mgr: manager to suspend
2222 *
2223 * This function tells the MST device that we can't handle UP messages
2224 * anymore. This should stop it from sending any since we are suspended.
2225 */
2226void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2227{
2228 mutex_lock(&mgr->lock);
2229 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2230 DP_MST_EN | DP_UPSTREAM_IS_SRC);
2231 mutex_unlock(&mgr->lock);
Dave Airlie274d8352015-09-30 10:39:42 +10002232 flush_work(&mgr->work);
2233 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10002234}
2235EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2236
2237/**
2238 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2239 * @mgr: manager to resume
2240 *
2241 * This will fetch DPCD and see if the device is still there,
2242 * if it is, it will rewrite the MSTM control bits, and return.
2243 *
2244 * if the device fails this returns -1, and the driver should do
2245 * a full MST reprobe, in case we were undocked.
2246 */
2247int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2248{
2249 int ret = 0;
2250
2251 mutex_lock(&mgr->lock);
2252
2253 if (mgr->mst_primary) {
2254 int sret;
Lyude1652fce2016-04-13 16:50:18 -04002255 u8 guid[16];
2256
Dave Airliead7f8a12014-06-05 14:01:32 +10002257 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2258 if (sret != DP_RECEIVER_CAP_SIZE) {
2259 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2260 ret = -1;
2261 goto out_unlock;
2262 }
2263
2264 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2265 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2266 if (ret < 0) {
2267 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2268 ret = -1;
2269 goto out_unlock;
2270 }
Lyude1652fce2016-04-13 16:50:18 -04002271
2272 /* Some hubs forget their guids after they resume */
2273 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2274 if (sret != 16) {
2275 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2276 ret = -1;
2277 goto out_unlock;
2278 }
2279 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2280
Dave Airliead7f8a12014-06-05 14:01:32 +10002281 ret = 0;
2282 } else
2283 ret = -1;
2284
2285out_unlock:
2286 mutex_unlock(&mgr->lock);
2287 return ret;
2288}
2289EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2290
Imre Deak636c4c32017-07-19 16:46:32 +03002291static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
Dave Airliead7f8a12014-06-05 14:01:32 +10002292{
2293 int len;
2294 u8 replyblock[32];
2295 int replylen, origlen, curreply;
2296 int ret;
2297 struct drm_dp_sideband_msg_rx *msg;
2298 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2299 msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2300
2301 len = min(mgr->max_dpcd_transaction_bytes, 16);
2302 ret = drm_dp_dpcd_read(mgr->aux, basereg,
2303 replyblock, len);
2304 if (ret != len) {
2305 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
Imre Deak636c4c32017-07-19 16:46:32 +03002306 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002307 }
2308 ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2309 if (!ret) {
2310 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
Imre Deak636c4c32017-07-19 16:46:32 +03002311 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002312 }
2313 replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2314
2315 origlen = replylen;
2316 replylen -= len;
2317 curreply = len;
2318 while (replylen > 0) {
2319 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2320 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2321 replyblock, len);
2322 if (ret != len) {
Imre Deak448421b2017-07-19 14:43:28 +03002323 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2324 len, ret);
Imre Deak636c4c32017-07-19 16:46:32 +03002325 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002326 }
Imre Deak448421b2017-07-19 14:43:28 +03002327
Dave Airliead7f8a12014-06-05 14:01:32 +10002328 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
Imre Deak448421b2017-07-19 14:43:28 +03002329 if (!ret) {
Dave Airliead7f8a12014-06-05 14:01:32 +10002330 DRM_DEBUG_KMS("failed to build sideband msg\n");
Imre Deak636c4c32017-07-19 16:46:32 +03002331 return false;
Imre Deak448421b2017-07-19 14:43:28 +03002332 }
2333
Dave Airliead7f8a12014-06-05 14:01:32 +10002334 curreply += len;
2335 replylen -= len;
2336 }
Imre Deak636c4c32017-07-19 16:46:32 +03002337 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10002338}
2339
2340static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2341{
2342 int ret = 0;
2343
Imre Deak636c4c32017-07-19 16:46:32 +03002344 if (!drm_dp_get_one_sb_msg(mgr, false)) {
2345 memset(&mgr->down_rep_recv, 0,
2346 sizeof(struct drm_dp_sideband_msg_rx));
2347 return 0;
2348 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002349
2350 if (mgr->down_rep_recv.have_eomt) {
2351 struct drm_dp_sideband_msg_tx *txmsg;
2352 struct drm_dp_mst_branch *mstb;
2353 int slot = -1;
2354 mstb = drm_dp_get_mst_branch_device(mgr,
2355 mgr->down_rep_recv.initial_hdr.lct,
2356 mgr->down_rep_recv.initial_hdr.rad);
2357
2358 if (!mstb) {
2359 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2360 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2361 return 0;
2362 }
2363
2364 /* find the message */
2365 slot = mgr->down_rep_recv.initial_hdr.seqno;
2366 mutex_lock(&mgr->qlock);
2367 txmsg = mstb->tx_slots[slot];
2368 /* remove from slots */
2369 mutex_unlock(&mgr->qlock);
2370
2371 if (!txmsg) {
2372 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2373 mstb,
2374 mgr->down_rep_recv.initial_hdr.seqno,
2375 mgr->down_rep_recv.initial_hdr.lct,
2376 mgr->down_rep_recv.initial_hdr.rad[0],
2377 mgr->down_rep_recv.msg[0]);
Lyude Pauld0757af2019-01-10 19:53:28 -05002378 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002379 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2380 return 0;
2381 }
2382
2383 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2384 if (txmsg->reply.reply_type == 1) {
2385 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);
2386 }
2387
2388 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
Lyude Pauld0757af2019-01-10 19:53:28 -05002389 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10002390
2391 mutex_lock(&mgr->qlock);
2392 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2393 mstb->tx_slots[slot] = NULL;
2394 mutex_unlock(&mgr->qlock);
2395
Chris Wilson68e989d2017-05-13 11:52:01 +01002396 wake_up_all(&mgr->tx_waitq);
Dave Airliead7f8a12014-06-05 14:01:32 +10002397 }
2398 return ret;
2399}
2400
2401static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2402{
2403 int ret = 0;
Imre Deak636c4c32017-07-19 16:46:32 +03002404
2405 if (!drm_dp_get_one_sb_msg(mgr, true)) {
2406 memset(&mgr->up_req_recv, 0,
2407 sizeof(struct drm_dp_sideband_msg_rx));
2408 return 0;
2409 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002410
2411 if (mgr->up_req_recv.have_eomt) {
2412 struct drm_dp_sideband_msg_req_body msg;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002413 struct drm_dp_mst_branch *mstb = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10002414 bool seqno;
Mykola Lysenkobd934322015-12-18 17:14:42 -05002415
2416 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2417 mstb = drm_dp_get_mst_branch_device(mgr,
2418 mgr->up_req_recv.initial_hdr.lct,
2419 mgr->up_req_recv.initial_hdr.rad);
2420 if (!mstb) {
2421 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2422 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2423 return 0;
2424 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002425 }
2426
2427 seqno = mgr->up_req_recv.initial_hdr.seqno;
2428 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2429
2430 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002431 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2432
2433 if (!mstb)
2434 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2435
2436 if (!mstb) {
2437 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2438 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2439 return 0;
2440 }
2441
Dave Airliead7f8a12014-06-05 14:01:32 +10002442 drm_dp_update_port(mstb, &msg.u.conn_stat);
Hersen Wu5e93b822016-01-22 17:07:28 -05002443
Dave Airliead7f8a12014-06-05 14:01:32 +10002444 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 +01002445 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002446
Dave Airliead7f8a12014-06-05 14:01:32 +10002447 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
Mykola Lysenkobd934322015-12-18 17:14:42 -05002448 drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2449 if (!mstb)
2450 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2451
2452 if (!mstb) {
2453 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2454 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2455 return 0;
2456 }
2457
Dave Airliead7f8a12014-06-05 14:01:32 +10002458 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2459 }
2460
Imre Deak7f8b3982017-07-19 14:43:29 +03002461 if (mstb)
Lyude Pauld0757af2019-01-10 19:53:28 -05002462 drm_dp_mst_topology_put_mstb(mstb);
Imre Deak7f8b3982017-07-19 14:43:29 +03002463
Dave Airliead7f8a12014-06-05 14:01:32 +10002464 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2465 }
2466 return ret;
2467}
2468
2469/**
2470 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2471 * @mgr: manager to notify irq for.
2472 * @esi: 4 bytes from SINK_COUNT_ESI
Daniel Vetter295ee852014-07-30 14:23:44 +02002473 * @handled: whether the hpd interrupt was consumed or not
Dave Airliead7f8a12014-06-05 14:01:32 +10002474 *
2475 * This should be called from the driver when it detects a short IRQ,
2476 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2477 * topology manager will process the sideband messages received as a result
2478 * of this.
2479 */
2480int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2481{
2482 int ret = 0;
2483 int sc;
2484 *handled = false;
2485 sc = esi[0] & 0x3f;
2486
2487 if (sc != mgr->sink_count) {
2488 mgr->sink_count = sc;
2489 *handled = true;
2490 }
2491
2492 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2493 ret = drm_dp_mst_handle_down_rep(mgr);
2494 *handled = true;
2495 }
2496
2497 if (esi[1] & DP_UP_REQ_MSG_RDY) {
2498 ret |= drm_dp_mst_handle_up_req(mgr);
2499 *handled = true;
2500 }
2501
2502 drm_dp_mst_kick_tx(mgr);
2503 return ret;
2504}
2505EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2506
2507/**
2508 * drm_dp_mst_detect_port() - get connection status for an MST port
Daniel Vetter132d49d2016-07-15 21:48:04 +02002509 * @connector: DRM connector for this port
Dave Airliead7f8a12014-06-05 14:01:32 +10002510 * @mgr: manager for this port
2511 * @port: unverified pointer to a port
2512 *
2513 * This returns the current connection state for a port. It validates the
2514 * port pointer still exists so the caller doesn't require a reference
2515 */
Dave Airliec6a0aed2014-10-20 16:28:02 +10002516enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2517 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10002518{
2519 enum drm_connector_status status = connector_status_disconnected;
2520
2521 /* we need to search for the port in the mgr in case its gone */
Lyude Pauld0757af2019-01-10 19:53:28 -05002522 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002523 if (!port)
2524 return connector_status_disconnected;
2525
2526 if (!port->ddps)
2527 goto out;
2528
2529 switch (port->pdt) {
2530 case DP_PEER_DEVICE_NONE:
2531 case DP_PEER_DEVICE_MST_BRANCHING:
2532 break;
2533
2534 case DP_PEER_DEVICE_SST_SINK:
2535 status = connector_status_connected;
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002536 /* for logical ports - cache the EDID */
2537 if (port->port_num >= 8 && !port->cached_edid) {
2538 port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2539 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002540 break;
2541 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2542 if (port->ldps)
2543 status = connector_status_connected;
2544 break;
2545 }
2546out:
Lyude Pauld0757af2019-01-10 19:53:28 -05002547 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002548 return status;
2549}
2550EXPORT_SYMBOL(drm_dp_mst_detect_port);
2551
2552/**
Libin Yangef8f9be2015-12-02 14:09:43 +08002553 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2554 * @mgr: manager for this port
2555 * @port: unverified pointer to a port.
2556 *
2557 * This returns whether the port supports audio or not.
2558 */
2559bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2560 struct drm_dp_mst_port *port)
2561{
2562 bool ret = false;
2563
Lyude Pauld0757af2019-01-10 19:53:28 -05002564 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Libin Yangef8f9be2015-12-02 14:09:43 +08002565 if (!port)
2566 return ret;
2567 ret = port->has_audio;
Lyude Pauld0757af2019-01-10 19:53:28 -05002568 drm_dp_mst_topology_put_port(port);
Libin Yangef8f9be2015-12-02 14:09:43 +08002569 return ret;
2570}
2571EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2572
2573/**
Dave Airliead7f8a12014-06-05 14:01:32 +10002574 * drm_dp_mst_get_edid() - get EDID for an MST port
2575 * @connector: toplevel connector to get EDID for
2576 * @mgr: manager for this port
2577 * @port: unverified pointer to a port.
2578 *
2579 * This returns an EDID for the port connected to a connector,
2580 * It validates the pointer still exists so the caller doesn't require a
2581 * reference.
2582 */
2583struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2584{
2585 struct edid *edid = NULL;
2586
2587 /* we need to search for the port in the mgr in case its gone */
Lyude Pauld0757af2019-01-10 19:53:28 -05002588 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002589 if (!port)
2590 return NULL;
2591
Dave Airliec6a0aed2014-10-20 16:28:02 +10002592 if (port->cached_edid)
2593 edid = drm_edid_duplicate(port->cached_edid);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002594 else {
2595 edid = drm_get_edid(connector, &port->aux.ddc);
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002596 drm_connector_set_tile_property(connector);
Dave Airlie8ae22cb2016-02-17 11:36:38 +10002597 }
Libin Yangef8f9be2015-12-02 14:09:43 +08002598 port->has_audio = drm_detect_monitor_audio(edid);
Lyude Pauld0757af2019-01-10 19:53:28 -05002599 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002600 return edid;
2601}
2602EXPORT_SYMBOL(drm_dp_mst_get_edid);
2603
2604/**
Lyude Paule4b0c862018-10-23 19:12:46 -04002605 * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
Dave Airliead7f8a12014-06-05 14:01:32 +10002606 * @mgr: manager to use
2607 * @pbn: payload bandwidth to convert into slots.
Lyude Paule4b0c862018-10-23 19:12:46 -04002608 *
2609 * Calculate the number of VCPI slots that will be required for the given PBN
2610 * value. This function is deprecated, and should not be used in atomic
2611 * drivers.
2612 *
2613 * RETURNS:
2614 * The total slots required for this port, or error.
Dave Airliead7f8a12014-06-05 14:01:32 +10002615 */
2616int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2617 int pbn)
2618{
2619 int num_slots;
2620
2621 num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2622
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07002623 /* max. time slots - one slot for MTP header */
2624 if (num_slots > 63)
Dave Airliead7f8a12014-06-05 14:01:32 +10002625 return -ENOSPC;
2626 return num_slots;
2627}
2628EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2629
2630static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002631 struct drm_dp_vcpi *vcpi, int pbn, int slots)
Dave Airliead7f8a12014-06-05 14:01:32 +10002632{
Dave Airliead7f8a12014-06-05 14:01:32 +10002633 int ret;
2634
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07002635 /* max. time slots - one slot for MTP header */
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002636 if (slots > 63)
Dave Airliead7f8a12014-06-05 14:01:32 +10002637 return -ENOSPC;
2638
2639 vcpi->pbn = pbn;
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002640 vcpi->aligned_pbn = slots * mgr->pbn_div;
2641 vcpi->num_slots = slots;
Dave Airliead7f8a12014-06-05 14:01:32 +10002642
2643 ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2644 if (ret < 0)
2645 return ret;
2646 return 0;
2647}
2648
2649/**
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07002650 * drm_dp_atomic_find_vcpi_slots() - Find and add vcpi slots to the state
2651 * @state: global atomic state
2652 * @mgr: MST topology manager for the port
2653 * @port: port to find vcpi slots for
2654 * @pbn: bandwidth required for the mode in PBN
2655 *
2656 * RETURNS:
2657 * Total slots in the atomic state assigned for this port or error
2658 */
2659int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
2660 struct drm_dp_mst_topology_mgr *mgr,
2661 struct drm_dp_mst_port *port, int pbn)
2662{
2663 struct drm_dp_mst_topology_state *topology_state;
2664 int req_slots;
2665
2666 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
Ville Syrjälä56a91c42017-07-12 18:51:00 +03002667 if (IS_ERR(topology_state))
2668 return PTR_ERR(topology_state);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07002669
Lyude Pauld0757af2019-01-10 19:53:28 -05002670 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07002671 if (port == NULL)
2672 return -EINVAL;
2673 req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2674 DRM_DEBUG_KMS("vcpi slots req=%d, avail=%d\n",
2675 req_slots, topology_state->avail_slots);
2676
2677 if (req_slots > topology_state->avail_slots) {
Lyude Pauld0757af2019-01-10 19:53:28 -05002678 drm_dp_mst_topology_put_port(port);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07002679 return -ENOSPC;
2680 }
2681
2682 topology_state->avail_slots -= req_slots;
2683 DRM_DEBUG_KMS("vcpi slots avail=%d", topology_state->avail_slots);
2684
Lyude Pauld0757af2019-01-10 19:53:28 -05002685 drm_dp_mst_topology_put_port(port);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07002686 return req_slots;
2687}
2688EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
2689
2690/**
2691 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
2692 * @state: global atomic state
2693 * @mgr: MST topology manager for the port
2694 * @slots: number of vcpi slots to release
2695 *
2696 * RETURNS:
2697 * 0 if @slots were added back to &drm_dp_mst_topology_state->avail_slots or
2698 * negative error code
2699 */
2700int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
2701 struct drm_dp_mst_topology_mgr *mgr,
2702 int slots)
2703{
2704 struct drm_dp_mst_topology_state *topology_state;
2705
2706 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
Ville Syrjälä56a91c42017-07-12 18:51:00 +03002707 if (IS_ERR(topology_state))
2708 return PTR_ERR(topology_state);
Pandiyan, Dhinakaranedb1ed12017-04-20 22:51:32 -07002709
2710 /* We cannot rely on port->vcpi.num_slots to update
2711 * topology_state->avail_slots as the port may not exist if the parent
2712 * branch device was unplugged. This should be fixed by tracking
2713 * per-port slot allocation in drm_dp_mst_topology_state instead of
2714 * depending on the caller to tell us how many slots to release.
2715 */
2716 topology_state->avail_slots += slots;
2717 DRM_DEBUG_KMS("vcpi slots released=%d, avail=%d\n",
2718 slots, topology_state->avail_slots);
2719
2720 return 0;
2721}
2722EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
2723
2724/**
Dave Airliead7f8a12014-06-05 14:01:32 +10002725 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2726 * @mgr: manager for this port
2727 * @port: port to allocate a virtual channel for.
2728 * @pbn: payload bandwidth number to request
2729 * @slots: returned number of slots for this PBN.
2730 */
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002731bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2732 struct drm_dp_mst_port *port, int pbn, int slots)
Dave Airliead7f8a12014-06-05 14:01:32 +10002733{
2734 int ret;
2735
Lyude Pauld0757af2019-01-10 19:53:28 -05002736 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002737 if (!port)
2738 return false;
2739
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002740 if (slots < 0)
2741 return false;
2742
Dave Airliead7f8a12014-06-05 14:01:32 +10002743 if (port->vcpi.vcpi > 0) {
Lyude Paule0ac7112019-01-10 19:53:26 -05002744 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
2745 port->vcpi.vcpi, port->vcpi.pbn, pbn);
Dave Airliead7f8a12014-06-05 14:01:32 +10002746 if (pbn == port->vcpi.pbn) {
Lyude Pauld0757af2019-01-10 19:53:28 -05002747 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002748 return true;
2749 }
2750 }
2751
Pandiyan, Dhinakaran1e797f52017-03-16 00:10:26 -07002752 ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
Dave Airliead7f8a12014-06-05 14:01:32 +10002753 if (ret) {
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07002754 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
Lyude Paule0ac7112019-01-10 19:53:26 -05002755 DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10002756 goto out;
2757 }
Pandiyan, Dhinakaranfeb2c3b2017-03-16 00:10:25 -07002758 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
Lyude Paule0ac7112019-01-10 19:53:26 -05002759 pbn, port->vcpi.num_slots);
Dave Airliead7f8a12014-06-05 14:01:32 +10002760
Lyude Pauld0757af2019-01-10 19:53:28 -05002761 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002762 return true;
2763out:
2764 return false;
2765}
2766EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2767
Dave Airlie87f59422015-02-24 09:23:55 +10002768int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2769{
2770 int slots = 0;
Lyude Pauld0757af2019-01-10 19:53:28 -05002771 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airlie87f59422015-02-24 09:23:55 +10002772 if (!port)
2773 return slots;
2774
2775 slots = port->vcpi.num_slots;
Lyude Pauld0757af2019-01-10 19:53:28 -05002776 drm_dp_mst_topology_put_port(port);
Dave Airlie87f59422015-02-24 09:23:55 +10002777 return slots;
2778}
2779EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2780
Dave Airliead7f8a12014-06-05 14:01:32 +10002781/**
2782 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2783 * @mgr: manager for this port
2784 * @port: unverified pointer to a port.
2785 *
2786 * This just resets the number of slots for the ports VCPI for later programming.
2787 */
2788void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2789{
Lyude Pauld0757af2019-01-10 19:53:28 -05002790 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002791 if (!port)
2792 return;
2793 port->vcpi.num_slots = 0;
Lyude Pauld0757af2019-01-10 19:53:28 -05002794 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002795}
2796EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2797
2798/**
2799 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2800 * @mgr: manager for this port
2801 * @port: unverified port to deallocate vcpi for
2802 */
Lyude Paul4afb8a22019-01-10 19:53:27 -05002803void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2804 struct drm_dp_mst_port *port)
Dave Airliead7f8a12014-06-05 14:01:32 +10002805{
Lyude Pauld0757af2019-01-10 19:53:28 -05002806 port = drm_dp_mst_topology_get_port_validated(mgr, port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002807 if (!port)
2808 return;
2809
2810 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2811 port->vcpi.num_slots = 0;
2812 port->vcpi.pbn = 0;
2813 port->vcpi.aligned_pbn = 0;
2814 port->vcpi.vcpi = 0;
Lyude Pauld0757af2019-01-10 19:53:28 -05002815 drm_dp_mst_topology_put_port(port);
Dave Airliead7f8a12014-06-05 14:01:32 +10002816}
2817EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2818
2819static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2820 int id, struct drm_dp_payload *payload)
2821{
2822 u8 payload_alloc[3], status;
2823 int ret;
2824 int retries = 0;
2825
2826 drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2827 DP_PAYLOAD_TABLE_UPDATED);
2828
2829 payload_alloc[0] = id;
2830 payload_alloc[1] = payload->start_slot;
2831 payload_alloc[2] = payload->num_slots;
2832
2833 ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2834 if (ret != 3) {
2835 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2836 goto fail;
2837 }
2838
2839retry:
2840 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2841 if (ret < 0) {
2842 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2843 goto fail;
2844 }
2845
2846 if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2847 retries++;
2848 if (retries < 20) {
2849 usleep_range(10000, 20000);
2850 goto retry;
2851 }
2852 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2853 ret = -EINVAL;
2854 goto fail;
2855 }
2856 ret = 0;
2857fail:
2858 return ret;
2859}
2860
2861
2862/**
2863 * drm_dp_check_act_status() - Check ACT handled status.
2864 * @mgr: manager to use
2865 *
2866 * Check the payload status bits in the DPCD for ACT handled completion.
2867 */
2868int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2869{
2870 u8 status;
2871 int ret;
2872 int count = 0;
2873
2874 do {
2875 ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2876
2877 if (ret < 0) {
2878 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2879 goto fail;
2880 }
2881
2882 if (status & DP_PAYLOAD_ACT_HANDLED)
2883 break;
2884 count++;
2885 udelay(100);
2886
2887 } while (count < 30);
2888
2889 if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2890 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2891 ret = -EINVAL;
2892 goto fail;
2893 }
2894 return 0;
2895fail:
2896 return ret;
2897}
2898EXPORT_SYMBOL(drm_dp_check_act_status);
2899
2900/**
2901 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2902 * @clock: dot clock for the mode
2903 * @bpp: bpp for the mode.
2904 *
2905 * This uses the formula in the spec to calculate the PBN value for a mode.
2906 */
2907int drm_dp_calc_pbn_mode(int clock, int bpp)
2908{
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002909 u64 kbps;
2910 s64 peak_kbps;
2911 u32 numerator;
2912 u32 denominator;
Dave Airliead7f8a12014-06-05 14:01:32 +10002913
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002914 kbps = clock * bpp;
Dave Airliead7f8a12014-06-05 14:01:32 +10002915
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002916 /*
2917 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2918 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2919 * common multiplier to render an integer PBN for all link rate/lane
2920 * counts combinations
2921 * calculate
2922 * peak_kbps *= (1006/1000)
2923 * peak_kbps *= (64/54)
2924 * peak_kbps *= 8 convert to bytes
2925 */
Dave Airliead7f8a12014-06-05 14:01:32 +10002926
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002927 numerator = 64 * 1006;
2928 denominator = 54 * 8 * 1000 * 1000;
Dave Airliead7f8a12014-06-05 14:01:32 +10002929
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002930 kbps *= numerator;
2931 peak_kbps = drm_fixp_from_fraction(kbps, denominator);
2932
2933 return drm_fixp2int_ceil(peak_kbps);
Dave Airliead7f8a12014-06-05 14:01:32 +10002934}
2935EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2936
2937static int test_calc_pbn_mode(void)
2938{
2939 int ret;
2940 ret = drm_dp_calc_pbn_mode(154000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002941 if (ret != 689) {
2942 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2943 154000, 30, 689, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10002944 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002945 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002946 ret = drm_dp_calc_pbn_mode(234000, 30);
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002947 if (ret != 1047) {
2948 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2949 234000, 30, 1047, ret);
Dave Airliead7f8a12014-06-05 14:01:32 +10002950 return -EINVAL;
Harry Wentlanda9ebb3e2016-01-22 17:07:26 -05002951 }
2952 ret = drm_dp_calc_pbn_mode(297000, 24);
2953 if (ret != 1063) {
2954 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2955 297000, 24, 1063, ret);
2956 return -EINVAL;
2957 }
Dave Airliead7f8a12014-06-05 14:01:32 +10002958 return 0;
2959}
2960
2961/* we want to kick the TX after we've ack the up/down IRQs. */
2962static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2963{
2964 queue_work(system_long_wq, &mgr->tx_work);
2965}
2966
2967static void drm_dp_mst_dump_mstb(struct seq_file *m,
2968 struct drm_dp_mst_branch *mstb)
2969{
2970 struct drm_dp_mst_port *port;
2971 int tabs = mstb->lct;
2972 char prefix[10];
2973 int i;
2974
2975 for (i = 0; i < tabs; i++)
2976 prefix[i] = '\t';
2977 prefix[i] = '\0';
2978
2979 seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2980 list_for_each_entry(port, &mstb->ports, next) {
Jim Bride51108f22016-04-14 10:18:36 -07002981 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 +10002982 if (port->mstb)
2983 drm_dp_mst_dump_mstb(m, port->mstb);
2984 }
2985}
2986
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02002987#define DP_PAYLOAD_TABLE_SIZE 64
2988
Dave Airliead7f8a12014-06-05 14:01:32 +10002989static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2990 char *buf)
2991{
Dave Airliead7f8a12014-06-05 14:01:32 +10002992 int i;
Joe Perches46466b02017-05-30 16:35:37 -07002993
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02002994 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
Joe Perches46466b02017-05-30 16:35:37 -07002995 if (drm_dp_dpcd_read(mgr->aux,
2996 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
2997 &buf[i], 16) != 16)
2998 return false;
Dave Airliead7f8a12014-06-05 14:01:32 +10002999 }
Joe Perches46466b02017-05-30 16:35:37 -07003000 return true;
Dave Airliead7f8a12014-06-05 14:01:32 +10003001}
3002
Jim Bride51108f22016-04-14 10:18:36 -07003003static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3004 struct drm_dp_mst_port *port, char *name,
3005 int namelen)
3006{
3007 struct edid *mst_edid;
3008
3009 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3010 drm_edid_get_monitor_name(mst_edid, name, namelen);
3011}
3012
Dave Airliead7f8a12014-06-05 14:01:32 +10003013/**
3014 * drm_dp_mst_dump_topology(): dump topology to seq file.
3015 * @m: seq_file to dump output to
3016 * @mgr: manager to dump current topology for.
3017 *
3018 * helper to dump MST topology to a seq file for debugfs.
3019 */
3020void drm_dp_mst_dump_topology(struct seq_file *m,
3021 struct drm_dp_mst_topology_mgr *mgr)
3022{
3023 int i;
3024 struct drm_dp_mst_port *port;
Jim Bride51108f22016-04-14 10:18:36 -07003025
Dave Airliead7f8a12014-06-05 14:01:32 +10003026 mutex_lock(&mgr->lock);
3027 if (mgr->mst_primary)
3028 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
3029
3030 /* dump VCPIs */
3031 mutex_unlock(&mgr->lock);
3032
3033 mutex_lock(&mgr->payload_lock);
Jim Bride51108f22016-04-14 10:18:36 -07003034 seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
3035 mgr->max_payloads);
Dave Airliead7f8a12014-06-05 14:01:32 +10003036
3037 for (i = 0; i < mgr->max_payloads; i++) {
3038 if (mgr->proposed_vcpis[i]) {
Jim Bride51108f22016-04-14 10:18:36 -07003039 char name[14];
3040
Dave Airliead7f8a12014-06-05 14:01:32 +10003041 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
Jim Bride51108f22016-04-14 10:18:36 -07003042 fetch_monitor_name(mgr, port, name, sizeof(name));
3043 seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3044 port->port_num, port->vcpi.vcpi,
3045 port->vcpi.num_slots,
3046 (*name != 0) ? name : "Unknown");
Dave Airliead7f8a12014-06-05 14:01:32 +10003047 } else
Jim Bride51108f22016-04-14 10:18:36 -07003048 seq_printf(m, "vcpi %d:unused\n", i);
Dave Airliead7f8a12014-06-05 14:01:32 +10003049 }
3050 for (i = 0; i < mgr->max_payloads; i++) {
3051 seq_printf(m, "payload %d: %d, %d, %d\n",
3052 i,
3053 mgr->payloads[i].payload_state,
3054 mgr->payloads[i].start_slot,
3055 mgr->payloads[i].num_slots);
3056
3057
3058 }
3059 mutex_unlock(&mgr->payload_lock);
3060
3061 mutex_lock(&mgr->lock);
3062 if (mgr->mst_primary) {
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003063 u8 buf[DP_PAYLOAD_TABLE_SIZE];
Dave Airliead7f8a12014-06-05 14:01:32 +10003064 int ret;
Joe Perches46466b02017-05-30 16:35:37 -07003065
Dave Airliead7f8a12014-06-05 14:01:32 +10003066 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
Joe Perches46466b02017-05-30 16:35:37 -07003067 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003068 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
Joe Perches46466b02017-05-30 16:35:37 -07003069 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003070 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
Joe Perches46466b02017-05-30 16:35:37 -07003071 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003072
Dave Airlie44790462015-07-14 11:33:31 +10003073 /* dump the standard OUI branch header */
3074 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
Joe Perches46466b02017-05-30 16:35:37 -07003075 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
Jim Bride51108f22016-04-14 10:18:36 -07003076 for (i = 0x3; i < 0x8 && buf[i]; i++)
Dave Airlie44790462015-07-14 11:33:31 +10003077 seq_printf(m, "%c", buf[i]);
Joe Perches46466b02017-05-30 16:35:37 -07003078 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
3079 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
3080 if (dump_dp_payload_table(mgr, buf))
Andy Shevchenko7056a2b2018-03-19 16:19:32 +02003081 seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
Dave Airliead7f8a12014-06-05 14:01:32 +10003082 }
3083
3084 mutex_unlock(&mgr->lock);
3085
3086}
3087EXPORT_SYMBOL(drm_dp_mst_dump_topology);
3088
3089static void drm_dp_tx_work(struct work_struct *work)
3090{
3091 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
3092
3093 mutex_lock(&mgr->qlock);
Daniel Vettercb021a32016-07-15 21:48:03 +02003094 if (!list_empty(&mgr->tx_msg_downq))
Dave Airliead7f8a12014-06-05 14:01:32 +10003095 process_single_down_tx_qlock(mgr);
3096 mutex_unlock(&mgr->qlock);
3097}
3098
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003099static void drm_dp_free_mst_port(struct kref *kref)
3100{
3101 struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
3102 kref_put(&port->parent->kref, drm_dp_free_mst_branch_device);
3103 kfree(port);
3104}
3105
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003106static void drm_dp_destroy_connector_work(struct work_struct *work)
3107{
3108 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 +02003109 struct drm_dp_mst_port *port;
Dave Airliedf4839f2015-09-16 10:37:28 +10003110 bool send_hotplug = false;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003111 /*
3112 * Not a regular list traverse as we have to drop the destroy
3113 * connector lock before destroying the connector, to avoid AB->BA
3114 * ordering between this lock and the config mutex.
3115 */
3116 for (;;) {
3117 mutex_lock(&mgr->destroy_connector_lock);
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003118 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
3119 if (!port) {
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003120 mutex_unlock(&mgr->destroy_connector_lock);
3121 break;
3122 }
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003123 list_del(&port->next);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003124 mutex_unlock(&mgr->destroy_connector_lock);
3125
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003126 kref_init(&port->kref);
3127 INIT_LIST_HEAD(&port->next);
3128
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003129 mgr->cbs->destroy_connector(mgr, port->connector);
3130
3131 drm_dp_port_teardown_pdt(port, port->pdt);
Ville Syrjälä36e3fa62016-10-26 16:30:33 +03003132 port->pdt = DP_PEER_DEVICE_NONE;
Maarten Lankhorst4772ff02015-08-11 09:54:29 +02003133
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003134 if (!port->input && port->vcpi.vcpi > 0) {
Andrey Grodzovskyfd2d2ba2016-05-25 16:45:43 -04003135 drm_dp_mst_reset_vcpi_slots(mgr, port);
3136 drm_dp_update_payload_part1(mgr);
3137 drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
Mykola Lysenko91a25e42016-01-27 09:39:36 -05003138 }
3139
3140 kref_put(&port->kref, drm_dp_free_mst_port);
Dave Airliedf4839f2015-09-16 10:37:28 +10003141 send_hotplug = true;
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003142 }
Dave Airliedf4839f2015-09-16 10:37:28 +10003143 if (send_hotplug)
Daniel Vetter16bff572018-11-28 23:12:34 +01003144 drm_kms_helper_hotplug_event(mgr->dev);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003145}
3146
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003147static struct drm_private_state *
3148drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003149{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003150 struct drm_dp_mst_topology_state *state;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003151
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003152 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
3153 if (!state)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003154 return NULL;
3155
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003156 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
3157
3158 return &state->base;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003159}
3160
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003161static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
3162 struct drm_private_state *state)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003163{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003164 struct drm_dp_mst_topology_state *mst_state =
3165 to_dp_mst_topology_state(state);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003166
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003167 kfree(mst_state);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003168}
3169
3170static const struct drm_private_state_funcs mst_state_funcs = {
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003171 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
3172 .atomic_destroy_state = drm_dp_mst_destroy_state,
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003173};
3174
3175/**
3176 * drm_atomic_get_mst_topology_state: get MST topology state
3177 *
3178 * @state: global atomic state
3179 * @mgr: MST topology manager, also the private object in this case
3180 *
3181 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3182 * state vtable so that the private object state returned is that of a MST
3183 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3184 * to care of the locking, so warn if don't hold the connection_mutex.
3185 *
3186 * RETURNS:
3187 *
3188 * The MST topology state or error pointer.
3189 */
3190struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
3191 struct drm_dp_mst_topology_mgr *mgr)
3192{
3193 struct drm_device *dev = mgr->dev;
3194
3195 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003196 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003197}
3198EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
3199
Dave Airliead7f8a12014-06-05 14:01:32 +10003200/**
3201 * drm_dp_mst_topology_mgr_init - initialise a topology manager
3202 * @mgr: manager struct to initialise
3203 * @dev: device providing this structure - for i2c addition.
3204 * @aux: DP helper aux channel to talk to this device
3205 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3206 * @max_payloads: maximum number of payloads this GPU can source
3207 * @conn_base_id: the connector object ID the MST device is connected to.
3208 *
3209 * Return 0 for success, or negative error code on failure
3210 */
3211int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
Dhinakaran Pandiyan7b0a89a2017-01-24 15:49:29 -08003212 struct drm_device *dev, struct drm_dp_aux *aux,
Dave Airliead7f8a12014-06-05 14:01:32 +10003213 int max_dpcd_transaction_bytes,
3214 int max_payloads, int conn_base_id)
3215{
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003216 struct drm_dp_mst_topology_state *mst_state;
3217
Dave Airliead7f8a12014-06-05 14:01:32 +10003218 mutex_init(&mgr->lock);
3219 mutex_init(&mgr->qlock);
3220 mutex_init(&mgr->payload_lock);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003221 mutex_init(&mgr->destroy_connector_lock);
Dave Airliead7f8a12014-06-05 14:01:32 +10003222 INIT_LIST_HEAD(&mgr->tx_msg_downq);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003223 INIT_LIST_HEAD(&mgr->destroy_connector_list);
Dave Airliead7f8a12014-06-05 14:01:32 +10003224 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
3225 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003226 INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10003227 init_waitqueue_head(&mgr->tx_waitq);
3228 mgr->dev = dev;
3229 mgr->aux = aux;
3230 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
3231 mgr->max_payloads = max_payloads;
3232 mgr->conn_base_id = conn_base_id;
Imre Deak4d6a10d2016-01-29 14:44:29 +02003233 if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3234 max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3235 return -EINVAL;
Dave Airliead7f8a12014-06-05 14:01:32 +10003236 mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3237 if (!mgr->payloads)
3238 return -ENOMEM;
3239 mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3240 if (!mgr->proposed_vcpis)
3241 return -ENOMEM;
3242 set_bit(0, &mgr->payload_mask);
Imre Deak441388a2016-01-29 14:44:28 +02003243 if (test_calc_pbn_mode() < 0)
3244 DRM_ERROR("MST PBN self-test failed\n");
3245
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003246 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
3247 if (mst_state == NULL)
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003248 return -ENOMEM;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003249
3250 mst_state->mgr = mgr;
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003251
3252 /* max. time slots - one slot for MTP header */
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003253 mst_state->avail_slots = 63;
3254
Rob Clarkb962a122018-10-22 14:31:22 +02003255 drm_atomic_private_obj_init(dev, &mgr->base,
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003256 &mst_state->base,
3257 &mst_state_funcs);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003258
Dave Airliead7f8a12014-06-05 14:01:32 +10003259 return 0;
3260}
3261EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3262
3263/**
3264 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3265 * @mgr: manager to destroy
3266 */
3267void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3268{
Lyude Paulf536e002018-12-11 18:50:26 -05003269 drm_dp_mst_topology_mgr_set_mst(mgr, false);
Dave Airlie274d8352015-09-30 10:39:42 +10003270 flush_work(&mgr->work);
Dave Airlie6b8eeca2015-06-15 10:34:28 +10003271 flush_work(&mgr->destroy_connector_work);
Dave Airliead7f8a12014-06-05 14:01:32 +10003272 mutex_lock(&mgr->payload_lock);
3273 kfree(mgr->payloads);
3274 mgr->payloads = NULL;
3275 kfree(mgr->proposed_vcpis);
3276 mgr->proposed_vcpis = NULL;
3277 mutex_unlock(&mgr->payload_lock);
3278 mgr->dev = NULL;
3279 mgr->aux = NULL;
Ville Syrjäläa4370c72017-07-12 18:51:02 +03003280 drm_atomic_private_obj_fini(&mgr->base);
Pandiyan, Dhinakaran3f3353b2017-04-20 22:51:31 -07003281 mgr->funcs = NULL;
Dave Airliead7f8a12014-06-05 14:01:32 +10003282}
3283EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3284
Ville Syrjäläcb8ce712018-09-28 21:04:00 +03003285static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
3286{
3287 int i;
3288
3289 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
3290 return false;
3291
3292 for (i = 0; i < num - 1; i++) {
3293 if (msgs[i].flags & I2C_M_RD ||
3294 msgs[i].len > 0xff)
3295 return false;
3296 }
3297
3298 return msgs[num - 1].flags & I2C_M_RD &&
3299 msgs[num - 1].len <= 0xff;
3300}
3301
Dave Airliead7f8a12014-06-05 14:01:32 +10003302/* I2C device */
3303static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3304 int num)
3305{
3306 struct drm_dp_aux *aux = adapter->algo_data;
3307 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3308 struct drm_dp_mst_branch *mstb;
3309 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3310 unsigned int i;
Dave Airliead7f8a12014-06-05 14:01:32 +10003311 struct drm_dp_sideband_msg_req_body msg;
3312 struct drm_dp_sideband_msg_tx *txmsg = NULL;
3313 int ret;
3314
Lyude Pauld0757af2019-01-10 19:53:28 -05003315 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
Dave Airliead7f8a12014-06-05 14:01:32 +10003316 if (!mstb)
3317 return -EREMOTEIO;
3318
Ville Syrjäläcb8ce712018-09-28 21:04:00 +03003319 if (!remote_i2c_read_ok(msgs, num)) {
Dave Airliead7f8a12014-06-05 14:01:32 +10003320 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3321 ret = -EIO;
3322 goto out;
3323 }
3324
Dave Airlieae491542015-10-14 18:51:17 +10003325 memset(&msg, 0, sizeof(msg));
Dave Airliead7f8a12014-06-05 14:01:32 +10003326 msg.req_type = DP_REMOTE_I2C_READ;
3327 msg.u.i2c_read.num_transactions = num - 1;
3328 msg.u.i2c_read.port_number = port->port_num;
3329 for (i = 0; i < num - 1; i++) {
3330 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3331 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3332 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
Ville Syrjäläc978ae92018-09-28 21:03:59 +03003333 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
Dave Airliead7f8a12014-06-05 14:01:32 +10003334 }
3335 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3336 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3337
3338 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3339 if (!txmsg) {
3340 ret = -ENOMEM;
3341 goto out;
3342 }
3343
3344 txmsg->dst = mstb;
3345 drm_dp_encode_sideband_req(&msg, txmsg);
3346
3347 drm_dp_queue_down_tx(mgr, txmsg);
3348
3349 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3350 if (ret > 0) {
3351
3352 if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3353 ret = -EREMOTEIO;
3354 goto out;
3355 }
3356 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3357 ret = -EIO;
3358 goto out;
3359 }
3360 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3361 ret = num;
3362 }
3363out:
3364 kfree(txmsg);
Lyude Pauld0757af2019-01-10 19:53:28 -05003365 drm_dp_mst_topology_put_mstb(mstb);
Dave Airliead7f8a12014-06-05 14:01:32 +10003366 return ret;
3367}
3368
3369static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3370{
3371 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3372 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3373 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3374 I2C_FUNC_10BIT_ADDR;
3375}
3376
3377static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3378 .functionality = drm_dp_mst_i2c_functionality,
3379 .master_xfer = drm_dp_mst_i2c_xfer,
3380};
3381
3382/**
3383 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3384 * @aux: DisplayPort AUX channel
3385 *
3386 * Returns 0 on success or a negative error code on failure.
3387 */
3388static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3389{
3390 aux->ddc.algo = &drm_dp_mst_i2c_algo;
3391 aux->ddc.algo_data = aux;
3392 aux->ddc.retries = 3;
3393
3394 aux->ddc.class = I2C_CLASS_DDC;
3395 aux->ddc.owner = THIS_MODULE;
3396 aux->ddc.dev.parent = aux->dev;
3397 aux->ddc.dev.of_node = aux->dev->of_node;
3398
3399 strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3400 sizeof(aux->ddc.name));
3401
3402 return i2c_add_adapter(&aux->ddc);
3403}
3404
3405/**
3406 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3407 * @aux: DisplayPort AUX channel
3408 */
3409static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3410{
3411 i2c_del_adapter(&aux->ddc);
3412}