1/*
2 * Copyright © 2014 Red Hat
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <linux/bitfield.h>
24#include <linux/delay.h>
25#include <linux/errno.h>
26#include <linux/i2c.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
29#include <linux/random.h>
30#include <linux/sched.h>
31#include <linux/seq_file.h>
32
33#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
34#include <linux/stacktrace.h>
35#include <linux/sort.h>
36#include <linux/timekeeping.h>
37#include <linux/math64.h>
38#endif
39
40#include <drm/display/drm_dp_mst_helper.h>
41#include <drm/drm_atomic.h>
42#include <drm/drm_atomic_helper.h>
43#include <drm/drm_drv.h>
44#include <drm/drm_edid.h>
45#include <drm/drm_fixed.h>
46#include <drm/drm_print.h>
47#include <drm/drm_probe_helper.h>
48
49#include "drm_dp_helper_internal.h"
50#include "drm_dp_mst_topology_internal.h"
51
52/**
53 * DOC: dp mst helper
54 *
55 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
56 * protocol. The helpers contain a topology manager and bandwidth manager.
57 * The helpers encapsulate the sending and received of sideband msgs.
58 */
59struct drm_dp_pending_up_req {
60 struct drm_dp_sideband_msg_hdr hdr;
61 struct drm_dp_sideband_msg_req_body msg;
62 struct list_head next;
63};
64
65static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
66 char *buf);
67
68static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
69
70static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
71 struct drm_dp_mst_port *port,
72 int offset, int size, u8 *bytes);
73static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
74 struct drm_dp_mst_port *port,
75 int offset, int size, u8 *bytes);
76
77static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
78 struct drm_dp_mst_branch *mstb);
79
80static void
81drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
82 struct drm_dp_mst_branch *mstb);
83
84static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
85 struct drm_dp_mst_branch *mstb,
86 struct drm_dp_mst_port *port);
87static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
88 guid_t *guid);
89
90static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port);
91static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port);
92static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
93
94static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
95 struct drm_dp_mst_branch *branch);
96
97#define DBG_PREFIX "[dp_mst]"
98
99#define DP_STR(x) [DP_ ## x] = #x
100
101static const char *drm_dp_mst_req_type_str(u8 req_type)
102{
103 static const char * const req_type_str[] = {
104 DP_STR(GET_MSG_TRANSACTION_VERSION),
105 DP_STR(LINK_ADDRESS),
106 DP_STR(CONNECTION_STATUS_NOTIFY),
107 DP_STR(ENUM_PATH_RESOURCES),
108 DP_STR(ALLOCATE_PAYLOAD),
109 DP_STR(QUERY_PAYLOAD),
110 DP_STR(RESOURCE_STATUS_NOTIFY),
111 DP_STR(CLEAR_PAYLOAD_ID_TABLE),
112 DP_STR(REMOTE_DPCD_READ),
113 DP_STR(REMOTE_DPCD_WRITE),
114 DP_STR(REMOTE_I2C_READ),
115 DP_STR(REMOTE_I2C_WRITE),
116 DP_STR(POWER_UP_PHY),
117 DP_STR(POWER_DOWN_PHY),
118 DP_STR(SINK_EVENT_NOTIFY),
119 DP_STR(QUERY_STREAM_ENC_STATUS),
120 };
121
122 if (req_type >= ARRAY_SIZE(req_type_str) ||
123 !req_type_str[req_type])
124 return "unknown";
125
126 return req_type_str[req_type];
127}
128
129#undef DP_STR
130#define DP_STR(x) [DP_NAK_ ## x] = #x
131
132static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
133{
134 static const char * const nak_reason_str[] = {
135 DP_STR(WRITE_FAILURE),
136 DP_STR(INVALID_READ),
137 DP_STR(CRC_FAILURE),
138 DP_STR(BAD_PARAM),
139 DP_STR(DEFER),
140 DP_STR(LINK_FAILURE),
141 DP_STR(NO_RESOURCES),
142 DP_STR(DPCD_FAIL),
143 DP_STR(I2C_NAK),
144 DP_STR(ALLOCATE_FAIL),
145 };
146
147 if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
148 !nak_reason_str[nak_reason])
149 return "unknown";
150
151 return nak_reason_str[nak_reason];
152}
153
154#undef DP_STR
155#define DP_STR(x) [DRM_DP_SIDEBAND_TX_ ## x] = #x
156
157static const char *drm_dp_mst_sideband_tx_state_str(int state)
158{
159 static const char * const sideband_reason_str[] = {
160 DP_STR(QUEUED),
161 DP_STR(START_SEND),
162 DP_STR(SENT),
163 DP_STR(RX),
164 DP_STR(TIMEOUT),
165 };
166
167 if (state >= ARRAY_SIZE(sideband_reason_str) ||
168 !sideband_reason_str[state])
169 return "unknown";
170
171 return sideband_reason_str[state];
172}
173
174static inline u8
175drm_dp_mst_get_ufp_num_at_lct_from_rad(u8 lct, const u8 *rad)
176{
177 int idx = (lct / 2) - 1;
178 int shift = (lct % 2) ? 0 : 4;
179 u8 ufp_num;
180
181 /* mst_primary, it's rad is unset*/
182 if (lct == 1)
183 return 0;
184
185 ufp_num = (rad[idx] >> shift) & 0xf;
186
187 return ufp_num;
188}
189
190static int
191drm_dp_mst_rad_to_str(const u8 rad[8], u8 lct, char *out, size_t len)
192{
193 int i;
194 u8 unpacked_rad[16] = {};
195
196 for (i = 0; i < lct; i++)
197 unpacked_rad[i] = drm_dp_mst_get_ufp_num_at_lct_from_rad(lct: i + 1, rad);
198
199 /* TODO: Eventually add something to printk so we can format the rad
200 * like this: 1.2.3
201 */
202 return snprintf(buf: out, size: len, fmt: "%*phC", lct, unpacked_rad);
203}
204
205/* sideband msg handling */
206static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
207{
208 u8 bitmask = 0x80;
209 u8 bitshift = 7;
210 u8 array_index = 0;
211 int number_of_bits = num_nibbles * 4;
212 u8 remainder = 0;
213
214 while (number_of_bits != 0) {
215 number_of_bits--;
216 remainder <<= 1;
217 remainder |= (data[array_index] & bitmask) >> bitshift;
218 bitmask >>= 1;
219 bitshift--;
220 if (bitmask == 0) {
221 bitmask = 0x80;
222 bitshift = 7;
223 array_index++;
224 }
225 if ((remainder & 0x10) == 0x10)
226 remainder ^= 0x13;
227 }
228
229 number_of_bits = 4;
230 while (number_of_bits != 0) {
231 number_of_bits--;
232 remainder <<= 1;
233 if ((remainder & 0x10) != 0)
234 remainder ^= 0x13;
235 }
236
237 return remainder;
238}
239
240static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
241{
242 u8 bitmask = 0x80;
243 u8 bitshift = 7;
244 u8 array_index = 0;
245 int number_of_bits = number_of_bytes * 8;
246 u16 remainder = 0;
247
248 while (number_of_bits != 0) {
249 number_of_bits--;
250 remainder <<= 1;
251 remainder |= (data[array_index] & bitmask) >> bitshift;
252 bitmask >>= 1;
253 bitshift--;
254 if (bitmask == 0) {
255 bitmask = 0x80;
256 bitshift = 7;
257 array_index++;
258 }
259 if ((remainder & 0x100) == 0x100)
260 remainder ^= 0xd5;
261 }
262
263 number_of_bits = 8;
264 while (number_of_bits != 0) {
265 number_of_bits--;
266 remainder <<= 1;
267 if ((remainder & 0x100) != 0)
268 remainder ^= 0xd5;
269 }
270
271 return remainder & 0xff;
272}
273static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
274{
275 u8 size = 3;
276
277 size += (hdr->lct / 2);
278 return size;
279}
280
281static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
282 u8 *buf, int *len)
283{
284 int idx = 0;
285 int i;
286 u8 crc4;
287
288 buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
289 for (i = 0; i < (hdr->lct / 2); i++)
290 buf[idx++] = hdr->rad[i];
291 buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
292 (hdr->msg_len & 0x3f);
293 buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
294
295 crc4 = drm_dp_msg_header_crc4(data: buf, num_nibbles: (idx * 2) - 1);
296 buf[idx - 1] |= (crc4 & 0xf);
297
298 *len = idx;
299}
300
301static bool drm_dp_decode_sideband_msg_hdr(const struct drm_dp_mst_topology_mgr *mgr,
302 struct drm_dp_sideband_msg_hdr *hdr,
303 u8 *buf, int buflen, u8 *hdrlen)
304{
305 u8 crc4;
306 u8 len;
307 int i;
308 u8 idx;
309
310 if (buf[0] == 0)
311 return false;
312 len = 3;
313 len += ((buf[0] & 0xf0) >> 4) / 2;
314 if (len > buflen)
315 return false;
316 crc4 = drm_dp_msg_header_crc4(data: buf, num_nibbles: (len * 2) - 1);
317
318 if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
319 drm_dbg_kms(mgr->dev, "crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
320 return false;
321 }
322
323 hdr->lct = (buf[0] & 0xf0) >> 4;
324 hdr->lcr = (buf[0] & 0xf);
325 idx = 1;
326 for (i = 0; i < (hdr->lct / 2); i++)
327 hdr->rad[i] = buf[idx++];
328 hdr->broadcast = (buf[idx] >> 7) & 0x1;
329 hdr->path_msg = (buf[idx] >> 6) & 0x1;
330 hdr->msg_len = buf[idx] & 0x3f;
331 if (hdr->msg_len < 1) /* min space for body CRC */
332 return false;
333
334 idx++;
335 hdr->somt = (buf[idx] >> 7) & 0x1;
336 hdr->eomt = (buf[idx] >> 6) & 0x1;
337 hdr->seqno = (buf[idx] >> 4) & 0x1;
338 idx++;
339 *hdrlen = idx;
340 return true;
341}
342
343void
344drm_dp_encode_sideband_req(const struct drm_dp_sideband_msg_req_body *req,
345 struct drm_dp_sideband_msg_tx *raw)
346{
347 int idx = 0;
348 int i;
349 u8 *buf = raw->msg;
350
351 buf[idx++] = req->req_type & 0x7f;
352
353 switch (req->req_type) {
354 case DP_ENUM_PATH_RESOURCES:
355 case DP_POWER_DOWN_PHY:
356 case DP_POWER_UP_PHY:
357 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
358 idx++;
359 break;
360 case DP_ALLOCATE_PAYLOAD:
361 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
362 (req->u.allocate_payload.number_sdp_streams & 0xf);
363 idx++;
364 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
365 idx++;
366 buf[idx] = (req->u.allocate_payload.pbn >> 8);
367 idx++;
368 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
369 idx++;
370 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
371 buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
372 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
373 idx++;
374 }
375 if (req->u.allocate_payload.number_sdp_streams & 1) {
376 i = req->u.allocate_payload.number_sdp_streams - 1;
377 buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
378 idx++;
379 }
380 break;
381 case DP_QUERY_PAYLOAD:
382 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
383 idx++;
384 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
385 idx++;
386 break;
387 case DP_REMOTE_DPCD_READ:
388 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
389 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
390 idx++;
391 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
392 idx++;
393 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
394 idx++;
395 buf[idx] = (req->u.dpcd_read.num_bytes);
396 idx++;
397 break;
398
399 case DP_REMOTE_DPCD_WRITE:
400 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
401 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
402 idx++;
403 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
404 idx++;
405 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
406 idx++;
407 buf[idx] = (req->u.dpcd_write.num_bytes);
408 idx++;
409 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
410 idx += req->u.dpcd_write.num_bytes;
411 break;
412 case DP_REMOTE_I2C_READ:
413 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
414 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
415 idx++;
416 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
417 buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
418 idx++;
419 buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
420 idx++;
421 memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
422 idx += req->u.i2c_read.transactions[i].num_bytes;
423
424 buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 4;
425 buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
426 idx++;
427 }
428 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
429 idx++;
430 buf[idx] = (req->u.i2c_read.num_bytes_read);
431 idx++;
432 break;
433
434 case DP_REMOTE_I2C_WRITE:
435 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
436 idx++;
437 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
438 idx++;
439 buf[idx] = (req->u.i2c_write.num_bytes);
440 idx++;
441 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
442 idx += req->u.i2c_write.num_bytes;
443 break;
444 case DP_QUERY_STREAM_ENC_STATUS: {
445 const struct drm_dp_query_stream_enc_status *msg;
446
447 msg = &req->u.enc_status;
448 buf[idx] = msg->stream_id;
449 idx++;
450 memcpy(&buf[idx], msg->client_id, sizeof(msg->client_id));
451 idx += sizeof(msg->client_id);
452 buf[idx] = 0;
453 buf[idx] |= FIELD_PREP(GENMASK(1, 0), msg->stream_event);
454 buf[idx] |= msg->valid_stream_event ? BIT(2) : 0;
455 buf[idx] |= FIELD_PREP(GENMASK(4, 3), msg->stream_behavior);
456 buf[idx] |= msg->valid_stream_behavior ? BIT(5) : 0;
457 idx++;
458 }
459 break;
460 }
461 raw->cur_len = idx;
462}
463EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_encode_sideband_req);
464
465/* Decode a sideband request we've encoded, mainly used for debugging */
466int
467drm_dp_decode_sideband_req(const struct drm_dp_sideband_msg_tx *raw,
468 struct drm_dp_sideband_msg_req_body *req)
469{
470 const u8 *buf = raw->msg;
471 int i, idx = 0;
472
473 req->req_type = buf[idx++] & 0x7f;
474 switch (req->req_type) {
475 case DP_ENUM_PATH_RESOURCES:
476 case DP_POWER_DOWN_PHY:
477 case DP_POWER_UP_PHY:
478 req->u.port_num.port_number = (buf[idx] >> 4) & 0xf;
479 break;
480 case DP_ALLOCATE_PAYLOAD:
481 {
482 struct drm_dp_allocate_payload *a =
483 &req->u.allocate_payload;
484
485 a->number_sdp_streams = buf[idx] & 0xf;
486 a->port_number = (buf[idx] >> 4) & 0xf;
487
488 WARN_ON(buf[++idx] & 0x80);
489 a->vcpi = buf[idx] & 0x7f;
490
491 a->pbn = buf[++idx] << 8;
492 a->pbn |= buf[++idx];
493
494 idx++;
495 for (i = 0; i < a->number_sdp_streams; i++) {
496 a->sdp_stream_sink[i] =
497 (buf[idx + (i / 2)] >> ((i % 2) ? 0 : 4)) & 0xf;
498 }
499 }
500 break;
501 case DP_QUERY_PAYLOAD:
502 req->u.query_payload.port_number = (buf[idx] >> 4) & 0xf;
503 WARN_ON(buf[++idx] & 0x80);
504 req->u.query_payload.vcpi = buf[idx] & 0x7f;
505 break;
506 case DP_REMOTE_DPCD_READ:
507 {
508 struct drm_dp_remote_dpcd_read *r = &req->u.dpcd_read;
509
510 r->port_number = (buf[idx] >> 4) & 0xf;
511
512 r->dpcd_address = (buf[idx] << 16) & 0xf0000;
513 r->dpcd_address |= (buf[++idx] << 8) & 0xff00;
514 r->dpcd_address |= buf[++idx] & 0xff;
515
516 r->num_bytes = buf[++idx];
517 }
518 break;
519 case DP_REMOTE_DPCD_WRITE:
520 {
521 struct drm_dp_remote_dpcd_write *w =
522 &req->u.dpcd_write;
523
524 w->port_number = (buf[idx] >> 4) & 0xf;
525
526 w->dpcd_address = (buf[idx] << 16) & 0xf0000;
527 w->dpcd_address |= (buf[++idx] << 8) & 0xff00;
528 w->dpcd_address |= buf[++idx] & 0xff;
529
530 w->num_bytes = buf[++idx];
531
532 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
533 GFP_KERNEL);
534 if (!w->bytes)
535 return -ENOMEM;
536 }
537 break;
538 case DP_REMOTE_I2C_READ:
539 {
540 struct drm_dp_remote_i2c_read *r = &req->u.i2c_read;
541 struct drm_dp_remote_i2c_read_tx *tx;
542 bool failed = false;
543
544 r->num_transactions = buf[idx] & 0x3;
545 r->port_number = (buf[idx] >> 4) & 0xf;
546 for (i = 0; i < r->num_transactions; i++) {
547 tx = &r->transactions[i];
548
549 tx->i2c_dev_id = buf[++idx] & 0x7f;
550 tx->num_bytes = buf[++idx];
551 tx->bytes = kmemdup(&buf[++idx],
552 tx->num_bytes,
553 GFP_KERNEL);
554 if (!tx->bytes) {
555 failed = true;
556 break;
557 }
558 idx += tx->num_bytes;
559 tx->no_stop_bit = (buf[idx] >> 5) & 0x1;
560 tx->i2c_transaction_delay = buf[idx] & 0xf;
561 }
562
563 if (failed) {
564 for (i = 0; i < r->num_transactions; i++) {
565 tx = &r->transactions[i];
566 kfree(objp: tx->bytes);
567 }
568 return -ENOMEM;
569 }
570
571 r->read_i2c_device_id = buf[++idx] & 0x7f;
572 r->num_bytes_read = buf[++idx];
573 }
574 break;
575 case DP_REMOTE_I2C_WRITE:
576 {
577 struct drm_dp_remote_i2c_write *w = &req->u.i2c_write;
578
579 w->port_number = (buf[idx] >> 4) & 0xf;
580 w->write_i2c_device_id = buf[++idx] & 0x7f;
581 w->num_bytes = buf[++idx];
582 w->bytes = kmemdup(&buf[++idx], w->num_bytes,
583 GFP_KERNEL);
584 if (!w->bytes)
585 return -ENOMEM;
586 }
587 break;
588 case DP_QUERY_STREAM_ENC_STATUS:
589 req->u.enc_status.stream_id = buf[idx++];
590 for (i = 0; i < sizeof(req->u.enc_status.client_id); i++)
591 req->u.enc_status.client_id[i] = buf[idx++];
592
593 req->u.enc_status.stream_event = FIELD_GET(GENMASK(1, 0),
594 buf[idx]);
595 req->u.enc_status.valid_stream_event = FIELD_GET(BIT(2),
596 buf[idx]);
597 req->u.enc_status.stream_behavior = FIELD_GET(GENMASK(4, 3),
598 buf[idx]);
599 req->u.enc_status.valid_stream_behavior = FIELD_GET(BIT(5),
600 buf[idx]);
601 break;
602 }
603
604 return 0;
605}
606EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_decode_sideband_req);
607
608void
609drm_dp_dump_sideband_msg_req_body(const struct drm_dp_sideband_msg_req_body *req,
610 int indent, struct drm_printer *printer)
611{
612 int i;
613
614#define P(f, ...) drm_printf_indent(printer, indent, f, ##__VA_ARGS__)
615 if (req->req_type == DP_LINK_ADDRESS) {
616 /* No contents to print */
617 P("type=%s\n", drm_dp_mst_req_type_str(req->req_type));
618 return;
619 }
620
621 P("type=%s contents:\n", drm_dp_mst_req_type_str(req->req_type));
622 indent++;
623
624 switch (req->req_type) {
625 case DP_ENUM_PATH_RESOURCES:
626 case DP_POWER_DOWN_PHY:
627 case DP_POWER_UP_PHY:
628 P("port=%d\n", req->u.port_num.port_number);
629 break;
630 case DP_ALLOCATE_PAYLOAD:
631 P("port=%d vcpi=%d pbn=%d sdp_streams=%d %*ph\n",
632 req->u.allocate_payload.port_number,
633 req->u.allocate_payload.vcpi, req->u.allocate_payload.pbn,
634 req->u.allocate_payload.number_sdp_streams,
635 req->u.allocate_payload.number_sdp_streams,
636 req->u.allocate_payload.sdp_stream_sink);
637 break;
638 case DP_QUERY_PAYLOAD:
639 P("port=%d vcpi=%d\n",
640 req->u.query_payload.port_number,
641 req->u.query_payload.vcpi);
642 break;
643 case DP_REMOTE_DPCD_READ:
644 P("port=%d dpcd_addr=%05x len=%d\n",
645 req->u.dpcd_read.port_number, req->u.dpcd_read.dpcd_address,
646 req->u.dpcd_read.num_bytes);
647 break;
648 case DP_REMOTE_DPCD_WRITE:
649 P("port=%d addr=%05x len=%d: %*ph\n",
650 req->u.dpcd_write.port_number,
651 req->u.dpcd_write.dpcd_address,
652 req->u.dpcd_write.num_bytes, req->u.dpcd_write.num_bytes,
653 req->u.dpcd_write.bytes);
654 break;
655 case DP_REMOTE_I2C_READ:
656 P("port=%d num_tx=%d id=%d size=%d:\n",
657 req->u.i2c_read.port_number,
658 req->u.i2c_read.num_transactions,
659 req->u.i2c_read.read_i2c_device_id,
660 req->u.i2c_read.num_bytes_read);
661
662 indent++;
663 for (i = 0; i < req->u.i2c_read.num_transactions; i++) {
664 const struct drm_dp_remote_i2c_read_tx *rtx =
665 &req->u.i2c_read.transactions[i];
666
667 P("%d: id=%03d size=%03d no_stop_bit=%d tx_delay=%03d: %*ph\n",
668 i, rtx->i2c_dev_id, rtx->num_bytes,
669 rtx->no_stop_bit, rtx->i2c_transaction_delay,
670 rtx->num_bytes, rtx->bytes);
671 }
672 break;
673 case DP_REMOTE_I2C_WRITE:
674 P("port=%d id=%d size=%d: %*ph\n",
675 req->u.i2c_write.port_number,
676 req->u.i2c_write.write_i2c_device_id,
677 req->u.i2c_write.num_bytes, req->u.i2c_write.num_bytes,
678 req->u.i2c_write.bytes);
679 break;
680 case DP_QUERY_STREAM_ENC_STATUS:
681 P("stream_id=%u client_id=%*ph stream_event=%x "
682 "valid_event=%d stream_behavior=%x valid_behavior=%d",
683 req->u.enc_status.stream_id,
684 (int)ARRAY_SIZE(req->u.enc_status.client_id),
685 req->u.enc_status.client_id, req->u.enc_status.stream_event,
686 req->u.enc_status.valid_stream_event,
687 req->u.enc_status.stream_behavior,
688 req->u.enc_status.valid_stream_behavior);
689 break;
690 default:
691 P("???\n");
692 break;
693 }
694#undef P
695}
696EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_dp_dump_sideband_msg_req_body);
697
698static inline void
699drm_dp_mst_dump_sideband_msg_tx(struct drm_printer *p,
700 const struct drm_dp_sideband_msg_tx *txmsg)
701{
702 struct drm_dp_sideband_msg_req_body req;
703 char buf[64];
704 int ret;
705 int i;
706
707 drm_dp_mst_rad_to_str(rad: txmsg->dst->rad, lct: txmsg->dst->lct, out: buf,
708 len: sizeof(buf));
709 drm_printf(p, f: "txmsg cur_offset=%x cur_len=%x seqno=%x state=%s path_msg=%d dst=%s\n",
710 txmsg->cur_offset, txmsg->cur_len, txmsg->seqno,
711 drm_dp_mst_sideband_tx_state_str(state: txmsg->state),
712 txmsg->path_msg, buf);
713
714 ret = drm_dp_decode_sideband_req(txmsg, &req);
715 if (ret) {
716 drm_printf(p, f: "<failed to decode sideband req: %d>\n", ret);
717 return;
718 }
719 drm_dp_dump_sideband_msg_req_body(&req, 1, p);
720
721 switch (req.req_type) {
722 case DP_REMOTE_DPCD_WRITE:
723 kfree(objp: req.u.dpcd_write.bytes);
724 break;
725 case DP_REMOTE_I2C_READ:
726 for (i = 0; i < req.u.i2c_read.num_transactions; i++)
727 kfree(objp: req.u.i2c_read.transactions[i].bytes);
728 break;
729 case DP_REMOTE_I2C_WRITE:
730 kfree(objp: req.u.i2c_write.bytes);
731 break;
732 }
733}
734
735static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
736{
737 u8 crc4;
738
739 crc4 = drm_dp_msg_data_crc4(data: msg, number_of_bytes: len);
740 msg[len] = crc4;
741}
742
743static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
744 struct drm_dp_sideband_msg_tx *raw)
745{
746 int idx = 0;
747 u8 *buf = raw->msg;
748
749 buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
750
751 raw->cur_len = idx;
752}
753
754static int drm_dp_sideband_msg_set_header(struct drm_dp_sideband_msg_rx *msg,
755 struct drm_dp_sideband_msg_hdr *hdr,
756 u8 hdrlen)
757{
758 /*
759 * ignore out-of-order messages or messages that are part of a
760 * failed transaction
761 */
762 if (!hdr->somt && !msg->have_somt)
763 return false;
764
765 /* get length contained in this portion */
766 msg->curchunk_idx = 0;
767 msg->curchunk_len = hdr->msg_len;
768 msg->curchunk_hdrlen = hdrlen;
769
770 /* we have already gotten an somt - don't bother parsing */
771 if (hdr->somt && msg->have_somt)
772 return false;
773
774 if (hdr->somt) {
775 memcpy(&msg->initial_hdr, hdr,
776 sizeof(struct drm_dp_sideband_msg_hdr));
777 msg->have_somt = true;
778 }
779 if (hdr->eomt)
780 msg->have_eomt = true;
781
782 return true;
783}
784
785/* this adds a chunk of msg to the builder to get the final msg */
786static bool drm_dp_sideband_append_payload(struct drm_dp_sideband_msg_rx *msg,
787 u8 *replybuf, u8 replybuflen)
788{
789 u8 crc4;
790
791 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
792 msg->curchunk_idx += replybuflen;
793
794 if (msg->curchunk_idx >= msg->curchunk_len) {
795 /* do CRC */
796 crc4 = drm_dp_msg_data_crc4(data: msg->chunk, number_of_bytes: msg->curchunk_len - 1);
797 if (crc4 != msg->chunk[msg->curchunk_len - 1])
798 print_hex_dump(KERN_DEBUG, prefix_str: "wrong crc",
799 prefix_type: DUMP_PREFIX_NONE, rowsize: 16, groupsize: 1,
800 buf: msg->chunk, len: msg->curchunk_len, ascii: false);
801 /* copy chunk into bigger msg */
802 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
803 msg->curlen += msg->curchunk_len - 1;
804 }
805 return true;
806}
807
808static bool drm_dp_sideband_parse_link_address(const struct drm_dp_mst_topology_mgr *mgr,
809 struct drm_dp_sideband_msg_rx *raw,
810 struct drm_dp_sideband_msg_reply_body *repmsg)
811{
812 int idx = 1;
813 int i;
814
815 import_guid(dst: &repmsg->u.link_addr.guid, src: &raw->msg[idx]);
816 idx += 16;
817 repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
818 idx++;
819 if (idx > raw->curlen)
820 goto fail_len;
821 for (i = 0; i < repmsg->u.link_addr.nports; i++) {
822 if (raw->msg[idx] & 0x80)
823 repmsg->u.link_addr.ports[i].input_port = 1;
824
825 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
826 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
827
828 idx++;
829 if (idx > raw->curlen)
830 goto fail_len;
831 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
832 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
833 if (repmsg->u.link_addr.ports[i].input_port == 0)
834 repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
835 idx++;
836 if (idx > raw->curlen)
837 goto fail_len;
838 if (repmsg->u.link_addr.ports[i].input_port == 0) {
839 repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
840 idx++;
841 if (idx > raw->curlen)
842 goto fail_len;
843 import_guid(dst: &repmsg->u.link_addr.ports[i].peer_guid, src: &raw->msg[idx]);
844 idx += 16;
845 if (idx > raw->curlen)
846 goto fail_len;
847 repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
848 repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
849 idx++;
850
851 }
852 if (idx > raw->curlen)
853 goto fail_len;
854 }
855
856 return true;
857fail_len:
858 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
859 return false;
860}
861
862static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
863 struct drm_dp_sideband_msg_reply_body *repmsg)
864{
865 int idx = 1;
866
867 repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
868 idx++;
869 if (idx > raw->curlen)
870 goto fail_len;
871 repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
872 idx++;
873 if (idx > raw->curlen)
874 goto fail_len;
875
876 memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
877 return true;
878fail_len:
879 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
880 return false;
881}
882
883static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
884 struct drm_dp_sideband_msg_reply_body *repmsg)
885{
886 int idx = 1;
887
888 repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
889 idx++;
890 if (idx > raw->curlen)
891 goto fail_len;
892 return true;
893fail_len:
894 DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
895 return false;
896}
897
898static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
899 struct drm_dp_sideband_msg_reply_body *repmsg)
900{
901 int idx = 1;
902
903 repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
904 idx++;
905 if (idx > raw->curlen)
906 goto fail_len;
907 repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
908 idx++;
909 /* TODO check */
910 memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
911 return true;
912fail_len:
913 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
914 return false;
915}
916
917static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
918 struct drm_dp_sideband_msg_reply_body *repmsg)
919{
920 int idx = 1;
921
922 repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
923 repmsg->u.path_resources.fec_capable = raw->msg[idx] & 0x1;
924 idx++;
925 if (idx > raw->curlen)
926 goto fail_len;
927 repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
928 idx += 2;
929 if (idx > raw->curlen)
930 goto fail_len;
931 repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
932 idx += 2;
933 if (idx > raw->curlen)
934 goto fail_len;
935 return true;
936fail_len:
937 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
938 return false;
939}
940
941static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
942 struct drm_dp_sideband_msg_reply_body *repmsg)
943{
944 int idx = 1;
945
946 repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
947 idx++;
948 if (idx > raw->curlen)
949 goto fail_len;
950 repmsg->u.allocate_payload.vcpi = raw->msg[idx];
951 idx++;
952 if (idx > raw->curlen)
953 goto fail_len;
954 repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
955 idx += 2;
956 if (idx > raw->curlen)
957 goto fail_len;
958 return true;
959fail_len:
960 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
961 return false;
962}
963
964static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
965 struct drm_dp_sideband_msg_reply_body *repmsg)
966{
967 int idx = 1;
968
969 repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
970 idx++;
971 if (idx > raw->curlen)
972 goto fail_len;
973 repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
974 idx += 2;
975 if (idx > raw->curlen)
976 goto fail_len;
977 return true;
978fail_len:
979 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
980 return false;
981}
982
983static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
984 struct drm_dp_sideband_msg_reply_body *repmsg)
985{
986 int idx = 1;
987
988 repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
989 idx++;
990 if (idx > raw->curlen) {
991 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
992 idx, raw->curlen);
993 return false;
994 }
995 return true;
996}
997
998static bool
999drm_dp_sideband_parse_query_stream_enc_status(
1000 struct drm_dp_sideband_msg_rx *raw,
1001 struct drm_dp_sideband_msg_reply_body *repmsg)
1002{
1003 struct drm_dp_query_stream_enc_status_ack_reply *reply;
1004
1005 reply = &repmsg->u.enc_status;
1006
1007 reply->stream_id = raw->msg[3];
1008
1009 reply->reply_signed = raw->msg[2] & BIT(0);
1010
1011 /*
1012 * NOTE: It's my impression from reading the spec that the below parsing
1013 * is correct. However I noticed while testing with an HDCP 1.4 display
1014 * through an HDCP 2.2 hub that only bit 3 was set. In that case, I
1015 * would expect both bits to be set. So keep the parsing following the
1016 * spec, but beware reality might not match the spec (at least for some
1017 * configurations).
1018 */
1019 reply->hdcp_1x_device_present = raw->msg[2] & BIT(4);
1020 reply->hdcp_2x_device_present = raw->msg[2] & BIT(3);
1021
1022 reply->query_capable_device_present = raw->msg[2] & BIT(5);
1023 reply->legacy_device_present = raw->msg[2] & BIT(6);
1024 reply->unauthorizable_device_present = raw->msg[2] & BIT(7);
1025
1026 reply->auth_completed = !!(raw->msg[1] & BIT(3));
1027 reply->encryption_enabled = !!(raw->msg[1] & BIT(4));
1028 reply->repeater_present = !!(raw->msg[1] & BIT(5));
1029 reply->state = (raw->msg[1] & GENMASK(7, 6)) >> 6;
1030
1031 return true;
1032}
1033
1034static bool drm_dp_sideband_parse_reply(const struct drm_dp_mst_topology_mgr *mgr,
1035 struct drm_dp_sideband_msg_rx *raw,
1036 struct drm_dp_sideband_msg_reply_body *msg)
1037{
1038 memset(msg, 0, sizeof(*msg));
1039 msg->reply_type = (raw->msg[0] & 0x80) >> 7;
1040 msg->req_type = (raw->msg[0] & 0x7f);
1041
1042 if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
1043 import_guid(dst: &msg->u.nak.guid, src: &raw->msg[1]);
1044 msg->u.nak.reason = raw->msg[17];
1045 msg->u.nak.nak_data = raw->msg[18];
1046 return false;
1047 }
1048
1049 switch (msg->req_type) {
1050 case DP_LINK_ADDRESS:
1051 return drm_dp_sideband_parse_link_address(mgr, raw, repmsg: msg);
1052 case DP_QUERY_PAYLOAD:
1053 return drm_dp_sideband_parse_query_payload_ack(raw, repmsg: msg);
1054 case DP_REMOTE_DPCD_READ:
1055 return drm_dp_sideband_parse_remote_dpcd_read(raw, repmsg: msg);
1056 case DP_REMOTE_DPCD_WRITE:
1057 return drm_dp_sideband_parse_remote_dpcd_write(raw, repmsg: msg);
1058 case DP_REMOTE_I2C_READ:
1059 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, repmsg: msg);
1060 case DP_REMOTE_I2C_WRITE:
1061 return true; /* since there's nothing to parse */
1062 case DP_ENUM_PATH_RESOURCES:
1063 return drm_dp_sideband_parse_enum_path_resources_ack(raw, repmsg: msg);
1064 case DP_ALLOCATE_PAYLOAD:
1065 return drm_dp_sideband_parse_allocate_payload_ack(raw, repmsg: msg);
1066 case DP_POWER_DOWN_PHY:
1067 case DP_POWER_UP_PHY:
1068 return drm_dp_sideband_parse_power_updown_phy_ack(raw, repmsg: msg);
1069 case DP_CLEAR_PAYLOAD_ID_TABLE:
1070 return true; /* since there's nothing to parse */
1071 case DP_QUERY_STREAM_ENC_STATUS:
1072 return drm_dp_sideband_parse_query_stream_enc_status(raw, repmsg: msg);
1073 default:
1074 drm_err(mgr->dev, "Got unknown reply 0x%02x (%s)\n",
1075 msg->req_type, drm_dp_mst_req_type_str(msg->req_type));
1076 return false;
1077 }
1078}
1079
1080static bool
1081drm_dp_sideband_parse_connection_status_notify(const struct drm_dp_mst_topology_mgr *mgr,
1082 struct drm_dp_sideband_msg_rx *raw,
1083 struct drm_dp_sideband_msg_req_body *msg)
1084{
1085 int idx = 1;
1086
1087 msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
1088 idx++;
1089 if (idx > raw->curlen)
1090 goto fail_len;
1091
1092 import_guid(dst: &msg->u.conn_stat.guid, src: &raw->msg[idx]);
1093 idx += 16;
1094 if (idx > raw->curlen)
1095 goto fail_len;
1096
1097 msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
1098 msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
1099 msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
1100 msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
1101 msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
1102 idx++;
1103 return true;
1104fail_len:
1105 drm_dbg_kms(mgr->dev, "connection status reply parse length fail %d %d\n",
1106 idx, raw->curlen);
1107 return false;
1108}
1109
1110static bool drm_dp_sideband_parse_resource_status_notify(const struct drm_dp_mst_topology_mgr *mgr,
1111 struct drm_dp_sideband_msg_rx *raw,
1112 struct drm_dp_sideband_msg_req_body *msg)
1113{
1114 int idx = 1;
1115
1116 msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
1117 idx++;
1118 if (idx > raw->curlen)
1119 goto fail_len;
1120
1121 import_guid(dst: &msg->u.resource_stat.guid, src: &raw->msg[idx]);
1122 idx += 16;
1123 if (idx > raw->curlen)
1124 goto fail_len;
1125
1126 msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
1127 idx++;
1128 return true;
1129fail_len:
1130 drm_dbg_kms(mgr->dev, "resource status reply parse length fail %d %d\n", idx, raw->curlen);
1131 return false;
1132}
1133
1134static bool drm_dp_sideband_parse_req(const struct drm_dp_mst_topology_mgr *mgr,
1135 struct drm_dp_sideband_msg_rx *raw,
1136 struct drm_dp_sideband_msg_req_body *msg)
1137{
1138 memset(msg, 0, sizeof(*msg));
1139 msg->req_type = (raw->msg[0] & 0x7f);
1140
1141 switch (msg->req_type) {
1142 case DP_CONNECTION_STATUS_NOTIFY:
1143 return drm_dp_sideband_parse_connection_status_notify(mgr, raw, msg);
1144 case DP_RESOURCE_STATUS_NOTIFY:
1145 return drm_dp_sideband_parse_resource_status_notify(mgr, raw, msg);
1146 default:
1147 drm_err(mgr->dev, "Got unknown request 0x%02x (%s)\n",
1148 msg->req_type, drm_dp_mst_req_type_str(msg->req_type));
1149 return false;
1150 }
1151}
1152
1153static void build_dpcd_write(struct drm_dp_sideband_msg_tx *msg,
1154 u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
1155{
1156 struct drm_dp_sideband_msg_req_body req;
1157
1158 req.req_type = DP_REMOTE_DPCD_WRITE;
1159 req.u.dpcd_write.port_number = port_num;
1160 req.u.dpcd_write.dpcd_address = offset;
1161 req.u.dpcd_write.num_bytes = num_bytes;
1162 req.u.dpcd_write.bytes = bytes;
1163 drm_dp_encode_sideband_req(&req, msg);
1164}
1165
1166static void build_link_address(struct drm_dp_sideband_msg_tx *msg)
1167{
1168 struct drm_dp_sideband_msg_req_body req;
1169
1170 req.req_type = DP_LINK_ADDRESS;
1171 drm_dp_encode_sideband_req(&req, msg);
1172}
1173
1174static void build_clear_payload_id_table(struct drm_dp_sideband_msg_tx *msg)
1175{
1176 struct drm_dp_sideband_msg_req_body req;
1177
1178 req.req_type = DP_CLEAR_PAYLOAD_ID_TABLE;
1179 drm_dp_encode_sideband_req(&req, msg);
1180 msg->path_msg = true;
1181}
1182
1183static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg,
1184 int port_num)
1185{
1186 struct drm_dp_sideband_msg_req_body req;
1187
1188 req.req_type = DP_ENUM_PATH_RESOURCES;
1189 req.u.port_num.port_number = port_num;
1190 drm_dp_encode_sideband_req(&req, msg);
1191 msg->path_msg = true;
1192 return 0;
1193}
1194
1195static void build_allocate_payload(struct drm_dp_sideband_msg_tx *msg,
1196 int port_num,
1197 u8 vcpi, uint16_t pbn,
1198 u8 number_sdp_streams,
1199 u8 *sdp_stream_sink)
1200{
1201 struct drm_dp_sideband_msg_req_body req;
1202
1203 memset(&req, 0, sizeof(req));
1204 req.req_type = DP_ALLOCATE_PAYLOAD;
1205 req.u.allocate_payload.port_number = port_num;
1206 req.u.allocate_payload.vcpi = vcpi;
1207 req.u.allocate_payload.pbn = pbn;
1208 req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
1209 memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
1210 number_sdp_streams);
1211 drm_dp_encode_sideband_req(&req, msg);
1212 msg->path_msg = true;
1213}
1214
1215static void build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
1216 int port_num, bool power_up)
1217{
1218 struct drm_dp_sideband_msg_req_body req;
1219
1220 if (power_up)
1221 req.req_type = DP_POWER_UP_PHY;
1222 else
1223 req.req_type = DP_POWER_DOWN_PHY;
1224
1225 req.u.port_num.port_number = port_num;
1226 drm_dp_encode_sideband_req(&req, msg);
1227 msg->path_msg = true;
1228}
1229
1230static int
1231build_query_stream_enc_status(struct drm_dp_sideband_msg_tx *msg, u8 stream_id,
1232 u8 *q_id)
1233{
1234 struct drm_dp_sideband_msg_req_body req;
1235
1236 req.req_type = DP_QUERY_STREAM_ENC_STATUS;
1237 req.u.enc_status.stream_id = stream_id;
1238 memcpy(req.u.enc_status.client_id, q_id,
1239 sizeof(req.u.enc_status.client_id));
1240 req.u.enc_status.stream_event = 0;
1241 req.u.enc_status.valid_stream_event = false;
1242 req.u.enc_status.stream_behavior = 0;
1243 req.u.enc_status.valid_stream_behavior = false;
1244
1245 drm_dp_encode_sideband_req(&req, msg);
1246 return 0;
1247}
1248
1249static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
1250 struct drm_dp_sideband_msg_tx *txmsg)
1251{
1252 unsigned int state;
1253
1254 /*
1255 * All updates to txmsg->state are protected by mgr->qlock, and the two
1256 * cases we check here are terminal states. For those the barriers
1257 * provided by the wake_up/wait_event pair are enough.
1258 */
1259 state = READ_ONCE(txmsg->state);
1260 return (state == DRM_DP_SIDEBAND_TX_RX ||
1261 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
1262}
1263
1264static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
1265 struct drm_dp_sideband_msg_tx *txmsg)
1266{
1267 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1268 unsigned long wait_timeout = msecs_to_jiffies(m: 4000);
1269 unsigned long wait_expires = jiffies + wait_timeout;
1270 int ret;
1271
1272 for (;;) {
1273 /*
1274 * If the driver provides a way for this, change to
1275 * poll-waiting for the MST reply interrupt if we didn't receive
1276 * it for 50 msec. This would cater for cases where the HPD
1277 * pulse signal got lost somewhere, even though the sink raised
1278 * the corresponding MST interrupt correctly. One example is the
1279 * Club 3D CAC-1557 TypeC -> DP adapter which for some reason
1280 * filters out short pulses with a duration less than ~540 usec.
1281 *
1282 * The poll period is 50 msec to avoid missing an interrupt
1283 * after the sink has cleared it (after a 110msec timeout
1284 * since it raised the interrupt).
1285 */
1286 ret = wait_event_timeout(mgr->tx_waitq,
1287 check_txmsg_state(mgr, txmsg),
1288 mgr->cbs->poll_hpd_irq ?
1289 msecs_to_jiffies(50) :
1290 wait_timeout);
1291
1292 if (ret || !mgr->cbs->poll_hpd_irq ||
1293 time_after(jiffies, wait_expires))
1294 break;
1295
1296 mgr->cbs->poll_hpd_irq(mgr);
1297 }
1298
1299 mutex_lock(&mgr->qlock);
1300 if (ret > 0) {
1301 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
1302 ret = -EIO;
1303 goto out;
1304 }
1305 } else {
1306 drm_dbg_kms(mgr->dev, "timedout msg send %p %d %d\n",
1307 txmsg, txmsg->state, txmsg->seqno);
1308
1309 /* dump some state */
1310 ret = -EIO;
1311
1312 /* remove from q */
1313 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
1314 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
1315 txmsg->state == DRM_DP_SIDEBAND_TX_SENT)
1316 list_del(entry: &txmsg->next);
1317 }
1318out:
1319 if (unlikely(ret == -EIO) && drm_debug_enabled(DRM_UT_DP)) {
1320 struct drm_printer p = drm_dbg_printer(drm: mgr->dev, category: DRM_UT_DP,
1321 DBG_PREFIX);
1322
1323 drm_dp_mst_dump_sideband_msg_tx(p: &p, txmsg);
1324 }
1325 mutex_unlock(lock: &mgr->qlock);
1326
1327 drm_dp_mst_kick_tx(mgr);
1328 return ret;
1329}
1330
1331static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
1332{
1333 struct drm_dp_mst_branch *mstb;
1334
1335 mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
1336 if (!mstb)
1337 return NULL;
1338
1339 mstb->lct = lct;
1340 if (lct > 1)
1341 memcpy(mstb->rad, rad, lct / 2);
1342 INIT_LIST_HEAD(list: &mstb->ports);
1343 kref_init(kref: &mstb->topology_kref);
1344 kref_init(kref: &mstb->malloc_kref);
1345 return mstb;
1346}
1347
1348static void drm_dp_free_mst_branch_device(struct kref *kref)
1349{
1350 struct drm_dp_mst_branch *mstb =
1351 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
1352
1353 if (mstb->port_parent)
1354 drm_dp_mst_put_port_malloc(port: mstb->port_parent);
1355
1356 kfree(objp: mstb);
1357}
1358
1359/**
1360 * DOC: Branch device and port refcounting
1361 *
1362 * Topology refcount overview
1363 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
1364 *
1365 * The refcounting schemes for &struct drm_dp_mst_branch and &struct
1366 * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
1367 * two different kinds of refcounts: topology refcounts, and malloc refcounts.
1368 *
1369 * Topology refcounts are not exposed to drivers, and are handled internally
1370 * by the DP MST helpers. The helpers use them in order to prevent the
1371 * in-memory topology state from being changed in the middle of critical
1372 * operations like changing the internal state of payload allocations. This
1373 * means each branch and port will be considered to be connected to the rest
1374 * of the topology until its topology refcount reaches zero. Additionally,
1375 * for ports this means that their associated &struct drm_connector will stay
1376 * registered with userspace until the port's refcount reaches 0.
1377 *
1378 * Malloc refcount overview
1379 * ~~~~~~~~~~~~~~~~~~~~~~~~
1380 *
1381 * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
1382 * drm_dp_mst_branch allocated even after all of its topology references have
1383 * been dropped, so that the driver or MST helpers can safely access each
1384 * branch's last known state before it was disconnected from the topology.
1385 * When the malloc refcount of a port or branch reaches 0, the memory
1386 * allocation containing the &struct drm_dp_mst_branch or &struct
1387 * drm_dp_mst_port respectively will be freed.
1388 *
1389 * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
1390 * to drivers. As of writing this documentation, there are no drivers that
1391 * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
1392 * helpers. Exposing this API to drivers in a race-free manner would take more
1393 * tweaking of the refcounting scheme, however patches are welcome provided
1394 * there is a legitimate driver usecase for this.
1395 *
1396 * Refcount relationships in a topology
1397 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1398 *
1399 * Let's take a look at why the relationship between topology and malloc
1400 * refcounts is designed the way it is.
1401 *
1402 * .. kernel-figure:: dp-mst/topology-figure-1.dot
1403 *
1404 * An example of topology and malloc refs in a DP MST topology with two
1405 * active payloads. Topology refcount increments are indicated by solid
1406 * lines, and malloc refcount increments are indicated by dashed lines.
1407 * Each starts from the branch which incremented the refcount, and ends at
1408 * the branch to which the refcount belongs to, i.e. the arrow points the
1409 * same way as the C pointers used to reference a structure.
1410 *
1411 * As you can see in the above figure, every branch increments the topology
1412 * refcount of its children, and increments the malloc refcount of its
1413 * parent. Additionally, every payload increments the malloc refcount of its
1414 * assigned port by 1.
1415 *
1416 * So, what would happen if MSTB #3 from the above figure was unplugged from
1417 * the system, but the driver hadn't yet removed payload #2 from port #3? The
1418 * topology would start to look like the figure below.
1419 *
1420 * .. kernel-figure:: dp-mst/topology-figure-2.dot
1421 *
1422 * Ports and branch devices which have been released from memory are
1423 * colored grey, and references which have been removed are colored red.
1424 *
1425 * Whenever a port or branch device's topology refcount reaches zero, it will
1426 * decrement the topology refcounts of all its children, the malloc refcount
1427 * of its parent, and finally its own malloc refcount. For MSTB #4 and port
1428 * #4, this means they both have been disconnected from the topology and freed
1429 * from memory. But, because payload #2 is still holding a reference to port
1430 * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1431 * is still accessible from memory. This also means port #3 has not yet
1432 * decremented the malloc refcount of MSTB #3, so its &struct
1433 * drm_dp_mst_branch will also stay allocated in memory until port #3's
1434 * malloc refcount reaches 0.
1435 *
1436 * This relationship is necessary because in order to release payload #2, we
1437 * need to be able to figure out the last relative of port #3 that's still
1438 * connected to the topology. In this case, we would travel up the topology as
1439 * shown below.
1440 *
1441 * .. kernel-figure:: dp-mst/topology-figure-3.dot
1442 *
1443 * And finally, remove payload #2 by communicating with port #2 through
1444 * sideband transactions.
1445 */
1446
1447/**
1448 * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1449 * device
1450 * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1451 *
1452 * Increments &drm_dp_mst_branch.malloc_kref. When
1453 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1454 * will be released and @mstb may no longer be used.
1455 *
1456 * See also: drm_dp_mst_put_mstb_malloc()
1457 */
1458static void
1459drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1460{
1461 kref_get(kref: &mstb->malloc_kref);
1462 drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
1463}
1464
1465/**
1466 * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1467 * device
1468 * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1469 *
1470 * Decrements &drm_dp_mst_branch.malloc_kref. When
1471 * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1472 * will be released and @mstb may no longer be used.
1473 *
1474 * See also: drm_dp_mst_get_mstb_malloc()
1475 */
1476static void
1477drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1478{
1479 drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
1480 kref_put(kref: &mstb->malloc_kref, release: drm_dp_free_mst_branch_device);
1481}
1482
1483static void drm_dp_free_mst_port(struct kref *kref)
1484{
1485 struct drm_dp_mst_port *port =
1486 container_of(kref, struct drm_dp_mst_port, malloc_kref);
1487
1488 drm_dp_mst_put_mstb_malloc(mstb: port->parent);
1489 kfree(objp: port);
1490}
1491
1492/**
1493 * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1494 * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1495 *
1496 * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1497 * reaches 0, the memory allocation for @port will be released and @port may
1498 * no longer be used.
1499 *
1500 * Because @port could potentially be freed at any time by the DP MST helpers
1501 * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1502 * function, drivers that which to make use of &struct drm_dp_mst_port should
1503 * ensure that they grab at least one main malloc reference to their MST ports
1504 * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1505 * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1506 *
1507 * See also: drm_dp_mst_put_port_malloc()
1508 */
1509void
1510drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1511{
1512 kref_get(kref: &port->malloc_kref);
1513 drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->malloc_kref));
1514}
1515EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1516
1517/**
1518 * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1519 * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1520 *
1521 * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1522 * reaches 0, the memory allocation for @port will be released and @port may
1523 * no longer be used.
1524 *
1525 * See also: drm_dp_mst_get_port_malloc()
1526 */
1527void
1528drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1529{
1530 drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1531 kref_put(kref: &port->malloc_kref, release: drm_dp_free_mst_port);
1532}
1533EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1534
1535#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
1536
1537#define STACK_DEPTH 8
1538
1539static noinline void
1540__topology_ref_save(struct drm_dp_mst_topology_mgr *mgr,
1541 struct drm_dp_mst_topology_ref_history *history,
1542 enum drm_dp_mst_topology_ref_type type)
1543{
1544 struct drm_dp_mst_topology_ref_entry *entry = NULL;
1545 depot_stack_handle_t backtrace;
1546 ulong stack_entries[STACK_DEPTH];
1547 uint n;
1548 int i;
1549
1550 n = stack_trace_save(store: stack_entries, ARRAY_SIZE(stack_entries), skipnr: 1);
1551 backtrace = stack_depot_save(entries: stack_entries, nr_entries: n, GFP_KERNEL);
1552 if (!backtrace)
1553 return;
1554
1555 /* Try to find an existing entry for this backtrace */
1556 for (i = 0; i < history->len; i++) {
1557 if (history->entries[i].backtrace == backtrace) {
1558 entry = &history->entries[i];
1559 break;
1560 }
1561 }
1562
1563 /* Otherwise add one */
1564 if (!entry) {
1565 struct drm_dp_mst_topology_ref_entry *new;
1566 int new_len = history->len + 1;
1567
1568 new = krealloc(history->entries, sizeof(*new) * new_len,
1569 GFP_KERNEL);
1570 if (!new)
1571 return;
1572
1573 entry = &new[history->len];
1574 history->len = new_len;
1575 history->entries = new;
1576
1577 entry->backtrace = backtrace;
1578 entry->type = type;
1579 entry->count = 0;
1580 }
1581 entry->count++;
1582 entry->ts_nsec = ktime_get_ns();
1583}
1584
1585static int
1586topology_ref_history_cmp(const void *a, const void *b)
1587{
1588 const struct drm_dp_mst_topology_ref_entry *entry_a = a, *entry_b = b;
1589
1590 if (entry_a->ts_nsec > entry_b->ts_nsec)
1591 return 1;
1592 else if (entry_a->ts_nsec < entry_b->ts_nsec)
1593 return -1;
1594 else
1595 return 0;
1596}
1597
1598static inline const char *
1599topology_ref_type_to_str(enum drm_dp_mst_topology_ref_type type)
1600{
1601 if (type == DRM_DP_MST_TOPOLOGY_REF_GET)
1602 return "get";
1603 else
1604 return "put";
1605}
1606
1607static void
1608__dump_topology_ref_history(struct drm_device *drm,
1609 struct drm_dp_mst_topology_ref_history *history,
1610 void *ptr, const char *type_str)
1611{
1612 struct drm_printer p = drm_dbg_printer(drm, category: DRM_UT_DP, DBG_PREFIX);
1613 char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
1614 int i;
1615
1616 if (!buf)
1617 return;
1618
1619 if (!history->len)
1620 goto out;
1621
1622 /* First, sort the list so that it goes from oldest to newest
1623 * reference entry
1624 */
1625 sort(base: history->entries, num: history->len, size: sizeof(*history->entries),
1626 cmp_func: topology_ref_history_cmp, NULL);
1627
1628 drm_printf(p: &p, f: "%s (%p) topology count reached 0, dumping history:\n",
1629 type_str, ptr);
1630
1631 for (i = 0; i < history->len; i++) {
1632 const struct drm_dp_mst_topology_ref_entry *entry =
1633 &history->entries[i];
1634 u64 ts_nsec = entry->ts_nsec;
1635 u32 rem_nsec = do_div(ts_nsec, 1000000000);
1636
1637 stack_depot_snprint(handle: entry->backtrace, buf, PAGE_SIZE, spaces: 4);
1638
1639 drm_printf(p: &p, f: " %d %ss (last at %5llu.%06u):\n%s",
1640 entry->count,
1641 topology_ref_type_to_str(type: entry->type),
1642 ts_nsec, rem_nsec / 1000, buf);
1643 }
1644
1645 /* Now free the history, since this is the only time we expose it */
1646 kfree(objp: history->entries);
1647out:
1648 kfree(objp: buf);
1649}
1650
1651static __always_inline void
1652drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb)
1653{
1654 __dump_topology_ref_history(drm: mstb->mgr->dev, history: &mstb->topology_ref_history,
1655 ptr: mstb, type_str: "MSTB");
1656}
1657
1658static __always_inline void
1659drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port)
1660{
1661 __dump_topology_ref_history(drm: port->mgr->dev, history: &port->topology_ref_history,
1662 ptr: port, type_str: "Port");
1663}
1664
1665static __always_inline void
1666save_mstb_topology_ref(struct drm_dp_mst_branch *mstb,
1667 enum drm_dp_mst_topology_ref_type type)
1668{
1669 __topology_ref_save(mgr: mstb->mgr, history: &mstb->topology_ref_history, type);
1670}
1671
1672static __always_inline void
1673save_port_topology_ref(struct drm_dp_mst_port *port,
1674 enum drm_dp_mst_topology_ref_type type)
1675{
1676 __topology_ref_save(mgr: port->mgr, history: &port->topology_ref_history, type);
1677}
1678
1679static inline void
1680topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr)
1681{
1682 mutex_lock(&mgr->topology_ref_history_lock);
1683}
1684
1685static inline void
1686topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr)
1687{
1688 mutex_unlock(lock: &mgr->topology_ref_history_lock);
1689}
1690#else
1691static inline void
1692topology_ref_history_lock(struct drm_dp_mst_topology_mgr *mgr) {}
1693static inline void
1694topology_ref_history_unlock(struct drm_dp_mst_topology_mgr *mgr) {}
1695static inline void
1696drm_dp_mst_dump_mstb_topology_history(struct drm_dp_mst_branch *mstb) {}
1697static inline void
1698drm_dp_mst_dump_port_topology_history(struct drm_dp_mst_port *port) {}
1699#define save_mstb_topology_ref(mstb, type)
1700#define save_port_topology_ref(port, type)
1701#endif
1702
1703struct drm_dp_mst_atomic_payload *
1704drm_atomic_get_mst_payload_state(struct drm_dp_mst_topology_state *state,
1705 struct drm_dp_mst_port *port)
1706{
1707 struct drm_dp_mst_atomic_payload *payload;
1708
1709 list_for_each_entry(payload, &state->payloads, next)
1710 if (payload->port == port)
1711 return payload;
1712
1713 return NULL;
1714}
1715EXPORT_SYMBOL(drm_atomic_get_mst_payload_state);
1716
1717static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1718{
1719 struct drm_dp_mst_branch *mstb =
1720 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1721 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1722
1723 drm_dp_mst_dump_mstb_topology_history(mstb);
1724
1725 INIT_LIST_HEAD(list: &mstb->destroy_next);
1726
1727 /*
1728 * This can get called under mgr->mutex, so we need to perform the
1729 * actual destruction of the mstb in another worker
1730 */
1731 mutex_lock(&mgr->delayed_destroy_lock);
1732 list_add(new: &mstb->destroy_next, head: &mgr->destroy_branch_device_list);
1733 mutex_unlock(lock: &mgr->delayed_destroy_lock);
1734 queue_work(wq: mgr->delayed_destroy_wq, work: &mgr->delayed_destroy_work);
1735}
1736
1737/**
1738 * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1739 * branch device unless it's zero
1740 * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1741 *
1742 * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1743 * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1744 * reached 0). Holding a topology reference implies that a malloc reference
1745 * will be held to @mstb as long as the user holds the topology reference.
1746 *
1747 * Care should be taken to ensure that the user has at least one malloc
1748 * reference to @mstb. If you already have a topology reference to @mstb, you
1749 * should use drm_dp_mst_topology_get_mstb() instead.
1750 *
1751 * See also:
1752 * drm_dp_mst_topology_get_mstb()
1753 * drm_dp_mst_topology_put_mstb()
1754 *
1755 * Returns:
1756 * * 1: A topology reference was grabbed successfully
1757 * * 0: @port is no longer in the topology, no reference was grabbed
1758 */
1759static int __must_check
1760drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1761{
1762 int ret;
1763
1764 topology_ref_history_lock(mgr: mstb->mgr);
1765 ret = kref_get_unless_zero(kref: &mstb->topology_kref);
1766 if (ret) {
1767 drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1768 save_mstb_topology_ref(mstb, type: DRM_DP_MST_TOPOLOGY_REF_GET);
1769 }
1770
1771 topology_ref_history_unlock(mgr: mstb->mgr);
1772
1773 return ret;
1774}
1775
1776/**
1777 * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1778 * branch device
1779 * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1780 *
1781 * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1782 * not it's already reached 0. This is only valid to use in scenarios where
1783 * you are already guaranteed to have at least one active topology reference
1784 * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1785 *
1786 * See also:
1787 * drm_dp_mst_topology_try_get_mstb()
1788 * drm_dp_mst_topology_put_mstb()
1789 */
1790static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1791{
1792 topology_ref_history_lock(mgr: mstb->mgr);
1793
1794 save_mstb_topology_ref(mstb, type: DRM_DP_MST_TOPOLOGY_REF_GET);
1795 WARN_ON(kref_read(&mstb->topology_kref) == 0);
1796 kref_get(kref: &mstb->topology_kref);
1797 drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1798
1799 topology_ref_history_unlock(mgr: mstb->mgr);
1800}
1801
1802/**
1803 * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1804 * device
1805 * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1806 *
1807 * Releases a topology reference from @mstb by decrementing
1808 * &drm_dp_mst_branch.topology_kref.
1809 *
1810 * See also:
1811 * drm_dp_mst_topology_try_get_mstb()
1812 * drm_dp_mst_topology_get_mstb()
1813 */
1814static void
1815drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1816{
1817 topology_ref_history_lock(mgr: mstb->mgr);
1818
1819 drm_dbg(mstb->mgr->dev, "mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref) - 1);
1820 save_mstb_topology_ref(mstb, type: DRM_DP_MST_TOPOLOGY_REF_PUT);
1821
1822 topology_ref_history_unlock(mgr: mstb->mgr);
1823 kref_put(kref: &mstb->topology_kref, release: drm_dp_destroy_mst_branch_device);
1824}
1825
1826static void drm_dp_destroy_port(struct kref *kref)
1827{
1828 struct drm_dp_mst_port *port =
1829 container_of(kref, struct drm_dp_mst_port, topology_kref);
1830 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1831
1832 drm_dp_mst_dump_port_topology_history(port);
1833
1834 /* There's nothing that needs locking to destroy an input port yet */
1835 if (port->input) {
1836 drm_dp_mst_put_port_malloc(port);
1837 return;
1838 }
1839
1840 drm_edid_free(drm_edid: port->cached_edid);
1841
1842 /*
1843 * we can't destroy the connector here, as we might be holding the
1844 * mode_config.mutex from an EDID retrieval
1845 */
1846 mutex_lock(&mgr->delayed_destroy_lock);
1847 list_add(new: &port->next, head: &mgr->destroy_port_list);
1848 mutex_unlock(lock: &mgr->delayed_destroy_lock);
1849 queue_work(wq: mgr->delayed_destroy_wq, work: &mgr->delayed_destroy_work);
1850}
1851
1852/**
1853 * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1854 * port unless it's zero
1855 * @port: &struct drm_dp_mst_port to increment the topology refcount of
1856 *
1857 * Attempts to grab a topology reference to @port, if it hasn't yet been
1858 * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1859 * 0). Holding a topology reference implies that a malloc reference will be
1860 * held to @port as long as the user holds the topology reference.
1861 *
1862 * Care should be taken to ensure that the user has at least one malloc
1863 * reference to @port. If you already have a topology reference to @port, you
1864 * should use drm_dp_mst_topology_get_port() instead.
1865 *
1866 * See also:
1867 * drm_dp_mst_topology_get_port()
1868 * drm_dp_mst_topology_put_port()
1869 *
1870 * Returns:
1871 * * 1: A topology reference was grabbed successfully
1872 * * 0: @port is no longer in the topology, no reference was grabbed
1873 */
1874static int __must_check
1875drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1876{
1877 int ret;
1878
1879 topology_ref_history_lock(mgr: port->mgr);
1880 ret = kref_get_unless_zero(kref: &port->topology_kref);
1881 if (ret) {
1882 drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->topology_kref));
1883 save_port_topology_ref(port, type: DRM_DP_MST_TOPOLOGY_REF_GET);
1884 }
1885
1886 topology_ref_history_unlock(mgr: port->mgr);
1887 return ret;
1888}
1889
1890/**
1891 * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1892 * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1893 *
1894 * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1895 * not it's already reached 0. This is only valid to use in scenarios where
1896 * you are already guaranteed to have at least one active topology reference
1897 * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1898 *
1899 * See also:
1900 * drm_dp_mst_topology_try_get_port()
1901 * drm_dp_mst_topology_put_port()
1902 */
1903static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1904{
1905 topology_ref_history_lock(mgr: port->mgr);
1906
1907 WARN_ON(kref_read(&port->topology_kref) == 0);
1908 kref_get(kref: &port->topology_kref);
1909 drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->topology_kref));
1910 save_port_topology_ref(port, type: DRM_DP_MST_TOPOLOGY_REF_GET);
1911
1912 topology_ref_history_unlock(mgr: port->mgr);
1913}
1914
1915/**
1916 * drm_dp_mst_topology_put_port() - release a topology reference to a port
1917 * @port: The &struct drm_dp_mst_port to release the topology reference from
1918 *
1919 * Releases a topology reference from @port by decrementing
1920 * &drm_dp_mst_port.topology_kref.
1921 *
1922 * See also:
1923 * drm_dp_mst_topology_try_get_port()
1924 * drm_dp_mst_topology_get_port()
1925 */
1926static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1927{
1928 topology_ref_history_lock(mgr: port->mgr);
1929
1930 drm_dbg(port->mgr->dev, "port %p (%d)\n", port, kref_read(&port->topology_kref) - 1);
1931 save_port_topology_ref(port, type: DRM_DP_MST_TOPOLOGY_REF_PUT);
1932
1933 topology_ref_history_unlock(mgr: port->mgr);
1934 kref_put(kref: &port->topology_kref, release: drm_dp_destroy_port);
1935}
1936
1937static struct drm_dp_mst_branch *
1938drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1939 struct drm_dp_mst_branch *to_find)
1940{
1941 struct drm_dp_mst_port *port;
1942 struct drm_dp_mst_branch *rmstb;
1943
1944 if (to_find == mstb)
1945 return mstb;
1946
1947 list_for_each_entry(port, &mstb->ports, next) {
1948 if (port->mstb) {
1949 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1950 mstb: port->mstb, to_find);
1951 if (rmstb)
1952 return rmstb;
1953 }
1954 }
1955 return NULL;
1956}
1957
1958static struct drm_dp_mst_branch *
1959drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1960 struct drm_dp_mst_branch *mstb)
1961{
1962 struct drm_dp_mst_branch *rmstb = NULL;
1963
1964 mutex_lock(&mgr->lock);
1965 if (mgr->mst_primary) {
1966 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1967 mstb: mgr->mst_primary, to_find: mstb);
1968
1969 if (rmstb && !drm_dp_mst_topology_try_get_mstb(mstb: rmstb))
1970 rmstb = NULL;
1971 }
1972 mutex_unlock(lock: &mgr->lock);
1973 return rmstb;
1974}
1975
1976static struct drm_dp_mst_port *
1977drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1978 struct drm_dp_mst_port *to_find)
1979{
1980 struct drm_dp_mst_port *port, *mport;
1981
1982 list_for_each_entry(port, &mstb->ports, next) {
1983 if (port == to_find)
1984 return port;
1985
1986 if (port->mstb) {
1987 mport = drm_dp_mst_topology_get_port_validated_locked(
1988 mstb: port->mstb, to_find);
1989 if (mport)
1990 return mport;
1991 }
1992 }
1993 return NULL;
1994}
1995
1996static struct drm_dp_mst_port *
1997drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1998 struct drm_dp_mst_port *port)
1999{
2000 struct drm_dp_mst_port *rport = NULL;
2001
2002 mutex_lock(&mgr->lock);
2003 if (mgr->mst_primary) {
2004 rport = drm_dp_mst_topology_get_port_validated_locked(
2005 mstb: mgr->mst_primary, to_find: port);
2006
2007 if (rport && !drm_dp_mst_topology_try_get_port(port: rport))
2008 rport = NULL;
2009 }
2010 mutex_unlock(lock: &mgr->lock);
2011 return rport;
2012}
2013
2014static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
2015{
2016 struct drm_dp_mst_port *port;
2017 int ret;
2018
2019 list_for_each_entry(port, &mstb->ports, next) {
2020 if (port->port_num == port_num) {
2021 ret = drm_dp_mst_topology_try_get_port(port);
2022 return ret ? port : NULL;
2023 }
2024 }
2025
2026 return NULL;
2027}
2028
2029/*
2030 * calculate a new RAD for this MST branch device
2031 * if parent has an LCT of 2 then it has 1 nibble of RAD,
2032 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
2033 */
2034static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
2035 u8 *rad)
2036{
2037 int parent_lct = port->parent->lct;
2038 int shift = 4;
2039 int idx = (parent_lct - 1) / 2;
2040
2041 if (parent_lct > 1) {
2042 memcpy(rad, port->parent->rad, idx + 1);
2043 shift = (parent_lct % 2) ? 4 : 0;
2044 } else
2045 rad[0] = 0;
2046
2047 rad[idx] |= port->port_num << shift;
2048 return parent_lct + 1;
2049}
2050
2051static bool drm_dp_mst_is_end_device(u8 pdt, bool mcs)
2052{
2053 switch (pdt) {
2054 case DP_PEER_DEVICE_DP_LEGACY_CONV:
2055 case DP_PEER_DEVICE_SST_SINK:
2056 return true;
2057 case DP_PEER_DEVICE_MST_BRANCHING:
2058 /* For sst branch device */
2059 if (!mcs)
2060 return true;
2061
2062 return false;
2063 }
2064 return true;
2065}
2066
2067static int
2068drm_dp_port_set_pdt(struct drm_dp_mst_port *port, u8 new_pdt,
2069 bool new_mcs)
2070{
2071 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2072 struct drm_dp_mst_branch *mstb;
2073 u8 rad[8], lct;
2074 int ret = 0;
2075
2076 if (port->pdt == new_pdt && port->mcs == new_mcs)
2077 return 0;
2078
2079 /* Teardown the old pdt, if there is one */
2080 if (port->pdt != DP_PEER_DEVICE_NONE) {
2081 if (drm_dp_mst_is_end_device(pdt: port->pdt, mcs: port->mcs)) {
2082 /*
2083 * If the new PDT would also have an i2c bus,
2084 * don't bother with reregistering it
2085 */
2086 if (new_pdt != DP_PEER_DEVICE_NONE &&
2087 drm_dp_mst_is_end_device(pdt: new_pdt, mcs: new_mcs)) {
2088 port->pdt = new_pdt;
2089 port->mcs = new_mcs;
2090 return 0;
2091 }
2092
2093 /* remove i2c over sideband */
2094 drm_dp_mst_unregister_i2c_bus(port);
2095 } else {
2096 mutex_lock(&mgr->lock);
2097 drm_dp_mst_topology_put_mstb(mstb: port->mstb);
2098 port->mstb = NULL;
2099 mutex_unlock(lock: &mgr->lock);
2100 }
2101 }
2102
2103 port->pdt = new_pdt;
2104 port->mcs = new_mcs;
2105
2106 if (port->pdt != DP_PEER_DEVICE_NONE) {
2107 if (drm_dp_mst_is_end_device(pdt: port->pdt, mcs: port->mcs)) {
2108 /* add i2c over sideband */
2109 ret = drm_dp_mst_register_i2c_bus(port);
2110 } else {
2111 lct = drm_dp_calculate_rad(port, rad);
2112 mstb = drm_dp_add_mst_branch_device(lct, rad);
2113 if (!mstb) {
2114 ret = -ENOMEM;
2115 drm_err(mgr->dev, "Failed to create MSTB for port %p", port);
2116 goto out;
2117 }
2118
2119 mutex_lock(&mgr->lock);
2120 port->mstb = mstb;
2121 mstb->mgr = port->mgr;
2122 mstb->port_parent = port;
2123
2124 /*
2125 * Make sure this port's memory allocation stays
2126 * around until its child MSTB releases it
2127 */
2128 drm_dp_mst_get_port_malloc(port);
2129 mutex_unlock(lock: &mgr->lock);
2130
2131 /* And make sure we send a link address for this */
2132 ret = 1;
2133 }
2134 }
2135
2136out:
2137 if (ret < 0)
2138 port->pdt = DP_PEER_DEVICE_NONE;
2139 return ret;
2140}
2141
2142/**
2143 * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
2144 * @aux: Fake sideband AUX CH
2145 * @offset: address of the (first) register to read
2146 * @buffer: buffer to store the register values
2147 * @size: number of bytes in @buffer
2148 *
2149 * Performs the same functionality for remote devices via
2150 * sideband messaging as drm_dp_dpcd_read() does for local
2151 * devices via actual AUX CH.
2152 *
2153 * Return: Number of bytes read, or negative error code on failure.
2154 */
2155ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
2156 unsigned int offset, void *buffer, size_t size)
2157{
2158 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
2159 aux);
2160
2161 return drm_dp_send_dpcd_read(mgr: port->mgr, port,
2162 offset, size, bytes: buffer);
2163}
2164
2165/**
2166 * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
2167 * @aux: Fake sideband AUX CH
2168 * @offset: address of the (first) register to write
2169 * @buffer: buffer containing the values to write
2170 * @size: number of bytes in @buffer
2171 *
2172 * Performs the same functionality for remote devices via
2173 * sideband messaging as drm_dp_dpcd_write() does for local
2174 * devices via actual AUX CH.
2175 *
2176 * Return: number of bytes written on success, negative error code on failure.
2177 */
2178ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
2179 unsigned int offset, void *buffer, size_t size)
2180{
2181 struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
2182 aux);
2183
2184 return drm_dp_send_dpcd_write(mgr: port->mgr, port,
2185 offset, size, bytes: buffer);
2186}
2187
2188static int drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, guid_t *guid)
2189{
2190 int ret = 0;
2191
2192 guid_copy(dst: &mstb->guid, src: guid);
2193
2194 if (!drm_dp_validate_guid(mgr: mstb->mgr, guid: &mstb->guid)) {
2195 struct drm_dp_aux *aux;
2196 u8 buf[UUID_SIZE];
2197
2198 export_guid(dst: buf, src: &mstb->guid);
2199
2200 if (mstb->port_parent)
2201 aux = &mstb->port_parent->aux;
2202 else
2203 aux = mstb->mgr->aux;
2204
2205 ret = drm_dp_dpcd_write_data(aux, DP_GUID, buffer: buf, size: sizeof(buf));
2206 }
2207
2208 return ret;
2209}
2210
2211static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
2212 int pnum,
2213 char *proppath,
2214 size_t proppath_size)
2215{
2216 int i;
2217 char temp[8];
2218
2219 snprintf(buf: proppath, size: proppath_size, fmt: "mst:%d", mstb->mgr->conn_base_id);
2220 for (i = 0; i < (mstb->lct - 1); i++) {
2221 int shift = (i % 2) ? 0 : 4;
2222 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
2223
2224 snprintf(buf: temp, size: sizeof(temp), fmt: "-%d", port_num);
2225 strlcat(p: proppath, q: temp, avail: proppath_size);
2226 }
2227 snprintf(buf: temp, size: sizeof(temp), fmt: "-%d", pnum);
2228 strlcat(p: proppath, q: temp, avail: proppath_size);
2229}
2230
2231/**
2232 * drm_dp_mst_connector_late_register() - Late MST connector registration
2233 * @connector: The MST connector
2234 * @port: The MST port for this connector
2235 *
2236 * Helper to register the remote aux device for this MST port. Drivers should
2237 * call this from their mst connector's late_register hook to enable MST aux
2238 * devices.
2239 *
2240 * Return: 0 on success, negative error code on failure.
2241 */
2242int drm_dp_mst_connector_late_register(struct drm_connector *connector,
2243 struct drm_dp_mst_port *port)
2244{
2245 drm_dbg_kms(port->mgr->dev, "registering %s remote bus for %s\n",
2246 port->aux.name, connector->kdev->kobj.name);
2247
2248 port->aux.dev = connector->kdev;
2249 return drm_dp_aux_register_devnode(aux: &port->aux);
2250}
2251EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
2252
2253/**
2254 * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
2255 * @connector: The MST connector
2256 * @port: The MST port for this connector
2257 *
2258 * Helper to unregister the remote aux device for this MST port, registered by
2259 * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
2260 * connector's early_unregister hook.
2261 */
2262void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
2263 struct drm_dp_mst_port *port)
2264{
2265 drm_dbg_kms(port->mgr->dev, "unregistering %s remote bus for %s\n",
2266 port->aux.name, connector->kdev->kobj.name);
2267 drm_dp_aux_unregister_devnode(aux: &port->aux);
2268}
2269EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
2270
2271static void
2272drm_dp_mst_port_add_connector(struct drm_dp_mst_branch *mstb,
2273 struct drm_dp_mst_port *port)
2274{
2275 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
2276 char proppath[255];
2277 int ret;
2278
2279 build_mst_prop_path(mstb, pnum: port->port_num, proppath, proppath_size: sizeof(proppath));
2280 port->connector = mgr->cbs->add_connector(mgr, port, proppath);
2281 if (!port->connector) {
2282 ret = -ENOMEM;
2283 goto error;
2284 }
2285
2286 if (port->pdt != DP_PEER_DEVICE_NONE &&
2287 drm_dp_mst_is_end_device(pdt: port->pdt, mcs: port->mcs) &&
2288 drm_dp_mst_port_is_logical(port))
2289 port->cached_edid = drm_edid_read_ddc(connector: port->connector,
2290 adapter: &port->aux.ddc);
2291
2292 drm_connector_dynamic_register(connector: port->connector);
2293 return;
2294
2295error:
2296 drm_err(mgr->dev, "Failed to create connector for port %p: %d\n", port, ret);
2297}
2298
2299/*
2300 * Drop a topology reference, and unlink the port from the in-memory topology
2301 * layout
2302 */
2303static void
2304drm_dp_mst_topology_unlink_port(struct drm_dp_mst_topology_mgr *mgr,
2305 struct drm_dp_mst_port *port)
2306{
2307 mutex_lock(&mgr->lock);
2308 port->parent->num_ports--;
2309 list_del(entry: &port->next);
2310 mutex_unlock(lock: &mgr->lock);
2311 drm_dp_mst_topology_put_port(port);
2312}
2313
2314static struct drm_dp_mst_port *
2315drm_dp_mst_add_port(struct drm_device *dev,
2316 struct drm_dp_mst_topology_mgr *mgr,
2317 struct drm_dp_mst_branch *mstb, u8 port_number)
2318{
2319 struct drm_dp_mst_port *port = kzalloc(sizeof(*port), GFP_KERNEL);
2320
2321 if (!port)
2322 return NULL;
2323
2324 kref_init(kref: &port->topology_kref);
2325 kref_init(kref: &port->malloc_kref);
2326 port->parent = mstb;
2327 port->port_num = port_number;
2328 port->mgr = mgr;
2329 port->aux.name = "DPMST";
2330 port->aux.dev = dev->dev;
2331 port->aux.is_remote = true;
2332
2333 /* initialize the MST downstream port's AUX crc work queue */
2334 port->aux.drm_dev = dev;
2335 drm_dp_remote_aux_init(aux: &port->aux);
2336
2337 /*
2338 * Make sure the memory allocation for our parent branch stays
2339 * around until our own memory allocation is released
2340 */
2341 drm_dp_mst_get_mstb_malloc(mstb);
2342
2343 return port;
2344}
2345
2346static int
2347drm_dp_mst_handle_link_address_port(struct drm_dp_mst_branch *mstb,
2348 struct drm_device *dev,
2349 struct drm_dp_link_addr_reply_port *port_msg)
2350{
2351 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
2352 struct drm_dp_mst_port *port;
2353 int ret;
2354 u8 new_pdt = DP_PEER_DEVICE_NONE;
2355 bool new_mcs = 0;
2356 bool created = false, send_link_addr = false, changed = false;
2357
2358 port = drm_dp_get_port(mstb, port_num: port_msg->port_number);
2359 if (!port) {
2360 port = drm_dp_mst_add_port(dev, mgr, mstb,
2361 port_number: port_msg->port_number);
2362 if (!port)
2363 return -ENOMEM;
2364 created = true;
2365 changed = true;
2366 } else if (!port->input && port_msg->input_port && port->connector) {
2367 /* Since port->connector can't be changed here, we create a
2368 * new port if input_port changes from 0 to 1
2369 */
2370 drm_dp_mst_topology_unlink_port(mgr, port);
2371 drm_dp_mst_topology_put_port(port);
2372 port = drm_dp_mst_add_port(dev, mgr, mstb,
2373 port_number: port_msg->port_number);
2374 if (!port)
2375 return -ENOMEM;
2376 changed = true;
2377 created = true;
2378 } else if (port->input && !port_msg->input_port) {
2379 changed = true;
2380 } else if (port->connector) {
2381 /* We're updating a port that's exposed to userspace, so do it
2382 * under lock
2383 */
2384 drm_modeset_lock(lock: &mgr->base.lock, NULL);
2385
2386 changed = port->ddps != port_msg->ddps ||
2387 (port->ddps &&
2388 (port->ldps != port_msg->legacy_device_plug_status ||
2389 port->dpcd_rev != port_msg->dpcd_revision ||
2390 port->mcs != port_msg->mcs ||
2391 port->pdt != port_msg->peer_device_type ||
2392 port->num_sdp_stream_sinks !=
2393 port_msg->num_sdp_stream_sinks));
2394 }
2395
2396 port->input = port_msg->input_port;
2397 if (!port->input)
2398 new_pdt = port_msg->peer_device_type;
2399 new_mcs = port_msg->mcs;
2400 port->ddps = port_msg->ddps;
2401 port->ldps = port_msg->legacy_device_plug_status;
2402 port->dpcd_rev = port_msg->dpcd_revision;
2403 port->num_sdp_streams = port_msg->num_sdp_streams;
2404 port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
2405
2406 /* manage mstb port lists with mgr lock - take a reference
2407 for this list */
2408 if (created) {
2409 mutex_lock(&mgr->lock);
2410 drm_dp_mst_topology_get_port(port);
2411 list_add(new: &port->next, head: &mstb->ports);
2412 mstb->num_ports++;
2413 mutex_unlock(lock: &mgr->lock);
2414 }
2415
2416 /*
2417 * Reprobe PBN caps on both hotplug, and when re-probing the link
2418 * for our parent mstb
2419 */
2420 if (port->ddps && !port->input) {
2421 ret = drm_dp_send_enum_path_resources(mgr, mstb,
2422 port);
2423 if (ret == 1)
2424 changed = true;
2425 } else {
2426 port->full_pbn = 0;
2427 }
2428
2429 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
2430 if (ret == 1) {
2431 send_link_addr = true;
2432 } else if (ret < 0) {
2433 drm_err(dev, "Failed to change PDT on port %p: %d\n", port, ret);
2434 goto fail;
2435 }
2436
2437 /*
2438 * If this port wasn't just created, then we're reprobing because
2439 * we're coming out of suspend. In this case, always resend the link
2440 * address if there's an MSTB on this port
2441 */
2442 if (!created && port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
2443 port->mcs)
2444 send_link_addr = true;
2445
2446 if (port->connector)
2447 drm_modeset_unlock(lock: &mgr->base.lock);
2448 else if (!port->input)
2449 drm_dp_mst_port_add_connector(mstb, port);
2450
2451 if (send_link_addr && port->mstb) {
2452 ret = drm_dp_send_link_address(mgr, mstb: port->mstb);
2453 if (ret == 1) /* MSTB below us changed */
2454 changed = true;
2455 else if (ret < 0)
2456 goto fail_put;
2457 }
2458
2459 /* put reference to this port */
2460 drm_dp_mst_topology_put_port(port);
2461 return changed;
2462
2463fail:
2464 drm_dp_mst_topology_unlink_port(mgr, port);
2465 if (port->connector)
2466 drm_modeset_unlock(lock: &mgr->base.lock);
2467fail_put:
2468 drm_dp_mst_topology_put_port(port);
2469 return ret;
2470}
2471
2472static int
2473drm_dp_mst_handle_conn_stat(struct drm_dp_mst_branch *mstb,
2474 struct drm_dp_connection_status_notify *conn_stat)
2475{
2476 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
2477 struct drm_dp_mst_port *port;
2478 int old_ddps, ret;
2479 u8 new_pdt;
2480 bool new_mcs;
2481 bool dowork = false, create_connector = false;
2482
2483 port = drm_dp_get_port(mstb, port_num: conn_stat->port_number);
2484 if (!port)
2485 return 0;
2486
2487 if (port->connector) {
2488 if (!port->input && conn_stat->input_port) {
2489 /*
2490 * We can't remove a connector from an already exposed
2491 * port, so just throw the port out and make sure we
2492 * reprobe the link address of it's parent MSTB
2493 */
2494 drm_dp_mst_topology_unlink_port(mgr, port);
2495 mstb->link_address_sent = false;
2496 dowork = true;
2497 goto out;
2498 }
2499
2500 /* Locking is only needed if the port's exposed to userspace */
2501 drm_modeset_lock(lock: &mgr->base.lock, NULL);
2502 } else if (port->input && !conn_stat->input_port) {
2503 create_connector = true;
2504 /* Reprobe link address so we get num_sdp_streams */
2505 mstb->link_address_sent = false;
2506 dowork = true;
2507 }
2508
2509 old_ddps = port->ddps;
2510 port->input = conn_stat->input_port;
2511 port->ldps = conn_stat->legacy_device_plug_status;
2512 port->ddps = conn_stat->displayport_device_plug_status;
2513
2514 if (old_ddps != port->ddps) {
2515 if (port->ddps && !port->input)
2516 drm_dp_send_enum_path_resources(mgr, mstb, port);
2517 else
2518 port->full_pbn = 0;
2519 }
2520
2521 new_pdt = port->input ? DP_PEER_DEVICE_NONE : conn_stat->peer_device_type;
2522 new_mcs = conn_stat->message_capability_status;
2523 ret = drm_dp_port_set_pdt(port, new_pdt, new_mcs);
2524 if (ret == 1) {
2525 dowork = true;
2526 } else if (ret < 0) {
2527 drm_err(mgr->dev, "Failed to change PDT for port %p: %d\n", port, ret);
2528 dowork = false;
2529 }
2530
2531 if (port->connector)
2532 drm_modeset_unlock(lock: &mgr->base.lock);
2533 else if (create_connector)
2534 drm_dp_mst_port_add_connector(mstb, port);
2535
2536out:
2537 drm_dp_mst_topology_put_port(port);
2538 return dowork;
2539}
2540
2541static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
2542 u8 lct, u8 *rad)
2543{
2544 struct drm_dp_mst_branch *mstb;
2545 struct drm_dp_mst_port *port;
2546 int i, ret;
2547 /* find the port by iterating down */
2548
2549 mutex_lock(&mgr->lock);
2550 mstb = mgr->mst_primary;
2551
2552 if (!mstb)
2553 goto out;
2554
2555 for (i = 1; i < lct; i++) {
2556 int port_num = drm_dp_mst_get_ufp_num_at_lct_from_rad(lct: i + 1, rad);
2557
2558 list_for_each_entry(port, &mstb->ports, next) {
2559 if (port->port_num == port_num) {
2560 mstb = port->mstb;
2561 if (!mstb) {
2562 drm_err(mgr->dev,
2563 "failed to lookup MSTB with lct %d, rad %02x\n",
2564 lct, rad[0]);
2565 goto out;
2566 }
2567
2568 break;
2569 }
2570 }
2571 }
2572 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2573 if (!ret)
2574 mstb = NULL;
2575out:
2576 mutex_unlock(lock: &mgr->lock);
2577 return mstb;
2578}
2579
2580static struct drm_dp_mst_branch *
2581get_mst_branch_device_by_guid_helper(struct drm_dp_mst_branch *mstb,
2582 const guid_t *guid)
2583{
2584 struct drm_dp_mst_branch *found_mstb;
2585 struct drm_dp_mst_port *port;
2586
2587 if (!mstb)
2588 return NULL;
2589
2590 if (guid_equal(u1: &mstb->guid, u2: guid))
2591 return mstb;
2592
2593 list_for_each_entry(port, &mstb->ports, next) {
2594 found_mstb = get_mst_branch_device_by_guid_helper(mstb: port->mstb, guid);
2595
2596 if (found_mstb)
2597 return found_mstb;
2598 }
2599
2600 return NULL;
2601}
2602
2603static struct drm_dp_mst_branch *
2604drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
2605 const guid_t *guid)
2606{
2607 struct drm_dp_mst_branch *mstb;
2608 int ret;
2609
2610 /* find the port by iterating down */
2611 mutex_lock(&mgr->lock);
2612
2613 mstb = get_mst_branch_device_by_guid_helper(mstb: mgr->mst_primary, guid);
2614 if (mstb) {
2615 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2616 if (!ret)
2617 mstb = NULL;
2618 }
2619
2620 mutex_unlock(lock: &mgr->lock);
2621 return mstb;
2622}
2623
2624static int drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2625 struct drm_dp_mst_branch *mstb)
2626{
2627 struct drm_dp_mst_port *port;
2628 int ret;
2629 bool changed = false;
2630
2631 if (!mstb->link_address_sent) {
2632 ret = drm_dp_send_link_address(mgr, mstb);
2633 if (ret == 1)
2634 changed = true;
2635 else if (ret < 0)
2636 return ret;
2637 }
2638
2639 list_for_each_entry(port, &mstb->ports, next) {
2640 if (port->input || !port->ddps || !port->mstb)
2641 continue;
2642
2643 ret = drm_dp_check_and_send_link_address(mgr, mstb: port->mstb);
2644 if (ret == 1)
2645 changed = true;
2646 else if (ret < 0)
2647 return ret;
2648 }
2649
2650 return changed;
2651}
2652
2653static void drm_dp_mst_link_probe_work(struct work_struct *work)
2654{
2655 struct drm_dp_mst_topology_mgr *mgr =
2656 container_of(work, struct drm_dp_mst_topology_mgr, work);
2657 struct drm_device *dev = mgr->dev;
2658 struct drm_dp_mst_branch *mstb;
2659 int ret;
2660 bool clear_payload_id_table;
2661
2662 mutex_lock(&mgr->probe_lock);
2663
2664 mutex_lock(&mgr->lock);
2665 clear_payload_id_table = !mgr->payload_id_table_cleared;
2666 mgr->payload_id_table_cleared = true;
2667
2668 mstb = mgr->mst_primary;
2669 if (mstb) {
2670 ret = drm_dp_mst_topology_try_get_mstb(mstb);
2671 if (!ret)
2672 mstb = NULL;
2673 }
2674 mutex_unlock(lock: &mgr->lock);
2675 if (!mstb) {
2676 mutex_unlock(lock: &mgr->probe_lock);
2677 return;
2678 }
2679
2680 /*
2681 * Certain branch devices seem to incorrectly report an available_pbn
2682 * of 0 on downstream sinks, even after clearing the
2683 * DP_PAYLOAD_ALLOCATE_* registers in
2684 * drm_dp_mst_topology_mgr_set_mst(). Namely, the CableMatters USB-C
2685 * 2x DP hub. Sending a CLEAR_PAYLOAD_ID_TABLE message seems to make
2686 * things work again.
2687 */
2688 if (clear_payload_id_table) {
2689 drm_dbg_kms(dev, "Clearing payload ID table\n");
2690 drm_dp_send_clear_payload_id_table(mgr, mstb);
2691 }
2692
2693 ret = drm_dp_check_and_send_link_address(mgr, mstb);
2694 drm_dp_mst_topology_put_mstb(mstb);
2695
2696 mutex_unlock(lock: &mgr->probe_lock);
2697 if (ret > 0)
2698 drm_kms_helper_hotplug_event(dev);
2699}
2700
2701static void drm_dp_mst_queue_probe_work(struct drm_dp_mst_topology_mgr *mgr)
2702{
2703 queue_work(wq: system_long_wq, work: &mgr->work);
2704}
2705
2706static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
2707 guid_t *guid)
2708{
2709 if (!guid_is_null(guid))
2710 return true;
2711
2712 guid_gen(u: guid);
2713
2714 return false;
2715}
2716
2717static void build_dpcd_read(struct drm_dp_sideband_msg_tx *msg,
2718 u8 port_num, u32 offset, u8 num_bytes)
2719{
2720 struct drm_dp_sideband_msg_req_body req;
2721
2722 req.req_type = DP_REMOTE_DPCD_READ;
2723 req.u.dpcd_read.port_number = port_num;
2724 req.u.dpcd_read.dpcd_address = offset;
2725 req.u.dpcd_read.num_bytes = num_bytes;
2726 drm_dp_encode_sideband_req(&req, msg);
2727}
2728
2729static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
2730 bool up, u8 *msg, int len)
2731{
2732 int ret;
2733 int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
2734 int tosend, total, offset;
2735 int retries = 0;
2736
2737retry:
2738 total = len;
2739 offset = 0;
2740 do {
2741 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
2742
2743 ret = drm_dp_dpcd_write_data(aux: mgr->aux, offset: regbase + offset,
2744 buffer: &msg[offset],
2745 size: tosend);
2746 if (ret == -EIO && retries < 5) {
2747 retries++;
2748 goto retry;
2749 } else if (ret < 0) {
2750 drm_dbg_kms(mgr->dev, "failed to dpcd write %d %d\n", tosend, ret);
2751
2752 return -EIO;
2753 }
2754 offset += tosend;
2755 total -= tosend;
2756 } while (total > 0);
2757 return 0;
2758}
2759
2760static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
2761 struct drm_dp_sideband_msg_tx *txmsg)
2762{
2763 struct drm_dp_mst_branch *mstb = txmsg->dst;
2764 u8 req_type;
2765
2766 req_type = txmsg->msg[0] & 0x7f;
2767 if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
2768 req_type == DP_RESOURCE_STATUS_NOTIFY ||
2769 req_type == DP_CLEAR_PAYLOAD_ID_TABLE)
2770 hdr->broadcast = 1;
2771 else
2772 hdr->broadcast = 0;
2773 hdr->path_msg = txmsg->path_msg;
2774 if (hdr->broadcast) {
2775 hdr->lct = 1;
2776 hdr->lcr = 6;
2777 } else {
2778 hdr->lct = mstb->lct;
2779 hdr->lcr = mstb->lct - 1;
2780 }
2781
2782 memcpy(hdr->rad, mstb->rad, hdr->lct / 2);
2783
2784 return 0;
2785}
2786/*
2787 * process a single block of the next message in the sideband queue
2788 */
2789static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2790 struct drm_dp_sideband_msg_tx *txmsg,
2791 bool up)
2792{
2793 u8 chunk[48];
2794 struct drm_dp_sideband_msg_hdr hdr;
2795 int len, space, idx, tosend;
2796 int ret;
2797
2798 if (txmsg->state == DRM_DP_SIDEBAND_TX_SENT)
2799 return 0;
2800
2801 memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
2802
2803 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED)
2804 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
2805
2806 /* make hdr from dst mst */
2807 ret = set_hdr_from_dst_qlock(hdr: &hdr, txmsg);
2808 if (ret < 0)
2809 return ret;
2810
2811 /* amount left to send in this message */
2812 len = txmsg->cur_len - txmsg->cur_offset;
2813
2814 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
2815 space = 48 - 1 - drm_dp_calc_sb_hdr_size(hdr: &hdr);
2816
2817 tosend = min(len, space);
2818 if (len == txmsg->cur_len)
2819 hdr.somt = 1;
2820 if (space >= len)
2821 hdr.eomt = 1;
2822
2823
2824 hdr.msg_len = tosend + 1;
2825 drm_dp_encode_sideband_msg_hdr(hdr: &hdr, buf: chunk, len: &idx);
2826 memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
2827 /* add crc at end */
2828 drm_dp_crc_sideband_chunk_req(msg: &chunk[idx], len: tosend);
2829 idx += tosend + 1;
2830
2831 ret = drm_dp_send_sideband_msg(mgr, up, msg: chunk, len: idx);
2832 if (ret) {
2833 if (drm_debug_enabled(DRM_UT_DP)) {
2834 struct drm_printer p = drm_dbg_printer(drm: mgr->dev,
2835 category: DRM_UT_DP,
2836 DBG_PREFIX);
2837
2838 drm_printf(p: &p, f: "sideband msg failed to send\n");
2839 drm_dp_mst_dump_sideband_msg_tx(p: &p, txmsg);
2840 }
2841 return ret;
2842 }
2843
2844 txmsg->cur_offset += tosend;
2845 if (txmsg->cur_offset == txmsg->cur_len) {
2846 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
2847 return 1;
2848 }
2849 return 0;
2850}
2851
2852static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
2853{
2854 struct drm_dp_sideband_msg_tx *txmsg;
2855 int ret;
2856
2857 WARN_ON(!mutex_is_locked(&mgr->qlock));
2858
2859 /* construct a chunk from the first msg in the tx_msg queue */
2860 if (list_empty(head: &mgr->tx_msg_downq))
2861 return;
2862
2863 txmsg = list_first_entry(&mgr->tx_msg_downq,
2864 struct drm_dp_sideband_msg_tx, next);
2865 ret = process_single_tx_qlock(mgr, txmsg, up: false);
2866 if (ret < 0) {
2867 drm_dbg_kms(mgr->dev, "failed to send msg in q %d\n", ret);
2868 list_del(entry: &txmsg->next);
2869 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
2870 wake_up_all(&mgr->tx_waitq);
2871 }
2872}
2873
2874static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2875 struct drm_dp_sideband_msg_tx *txmsg)
2876{
2877 mutex_lock(&mgr->qlock);
2878 list_add_tail(new: &txmsg->next, head: &mgr->tx_msg_downq);
2879
2880 if (drm_debug_enabled(DRM_UT_DP)) {
2881 struct drm_printer p = drm_dbg_printer(drm: mgr->dev, category: DRM_UT_DP,
2882 DBG_PREFIX);
2883
2884 drm_dp_mst_dump_sideband_msg_tx(p: &p, txmsg);
2885 }
2886
2887 if (list_is_singular(head: &mgr->tx_msg_downq))
2888 process_single_down_tx_qlock(mgr);
2889 mutex_unlock(lock: &mgr->qlock);
2890}
2891
2892static void
2893drm_dp_dump_link_address(const struct drm_dp_mst_topology_mgr *mgr,
2894 struct drm_dp_link_address_ack_reply *reply)
2895{
2896 struct drm_dp_link_addr_reply_port *port_reply;
2897 int i;
2898
2899 for (i = 0; i < reply->nports; i++) {
2900 port_reply = &reply->ports[i];
2901 drm_dbg_kms(mgr->dev,
2902 "port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n",
2903 i,
2904 port_reply->input_port,
2905 port_reply->peer_device_type,
2906 port_reply->port_number,
2907 port_reply->dpcd_revision,
2908 port_reply->mcs,
2909 port_reply->ddps,
2910 port_reply->legacy_device_plug_status,
2911 port_reply->num_sdp_streams,
2912 port_reply->num_sdp_stream_sinks);
2913 }
2914}
2915
2916static int drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2917 struct drm_dp_mst_branch *mstb)
2918{
2919 struct drm_dp_sideband_msg_tx *txmsg;
2920 struct drm_dp_link_address_ack_reply *reply;
2921 struct drm_dp_mst_port *port, *tmp;
2922 int i, ret, port_mask = 0;
2923 bool changed = false;
2924
2925 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2926 if (!txmsg)
2927 return -ENOMEM;
2928
2929 txmsg->dst = mstb;
2930 build_link_address(msg: txmsg);
2931
2932 mstb->link_address_sent = true;
2933 drm_dp_queue_down_tx(mgr, txmsg);
2934
2935 /* FIXME: Actually do some real error handling here */
2936 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2937 if (ret < 0) {
2938 drm_err(mgr->dev, "Sending link address failed with %d\n", ret);
2939 goto out;
2940 }
2941 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2942 drm_err(mgr->dev, "link address NAK received\n");
2943 ret = -EIO;
2944 goto out;
2945 }
2946
2947 reply = &txmsg->reply.u.link_addr;
2948 drm_dbg_kms(mgr->dev, "link address reply: %d\n", reply->nports);
2949 drm_dp_dump_link_address(mgr, reply);
2950
2951 ret = drm_dp_check_mstb_guid(mstb, guid: &reply->guid);
2952 if (ret) {
2953 char buf[64];
2954
2955 drm_dp_mst_rad_to_str(rad: mstb->rad, lct: mstb->lct, out: buf, len: sizeof(buf));
2956 drm_err(mgr->dev, "GUID check on %s failed: %d\n", buf, ret);
2957 goto out;
2958 }
2959
2960 for (i = 0; i < reply->nports; i++) {
2961 port_mask |= BIT(reply->ports[i].port_number);
2962 ret = drm_dp_mst_handle_link_address_port(mstb, dev: mgr->dev,
2963 port_msg: &reply->ports[i]);
2964 if (ret == 1)
2965 changed = true;
2966 else if (ret < 0)
2967 goto out;
2968 }
2969
2970 /* Prune any ports that are currently a part of mstb in our in-memory
2971 * topology, but were not seen in this link address. Usually this
2972 * means that they were removed while the topology was out of sync,
2973 * e.g. during suspend/resume
2974 */
2975 mutex_lock(&mgr->lock);
2976 list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
2977 if (port_mask & BIT(port->port_num))
2978 continue;
2979
2980 drm_dbg_kms(mgr->dev, "port %d was not in link address, removing\n",
2981 port->port_num);
2982 list_del(entry: &port->next);
2983 drm_dp_mst_topology_put_port(port);
2984 changed = true;
2985 }
2986 mutex_unlock(lock: &mgr->lock);
2987
2988out:
2989 if (ret < 0)
2990 mstb->link_address_sent = false;
2991 kfree(objp: txmsg);
2992 return ret < 0 ? ret : changed;
2993}
2994
2995static void
2996drm_dp_send_clear_payload_id_table(struct drm_dp_mst_topology_mgr *mgr,
2997 struct drm_dp_mst_branch *mstb)
2998{
2999 struct drm_dp_sideband_msg_tx *txmsg;
3000 int ret;
3001
3002 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3003 if (!txmsg)
3004 return;
3005
3006 txmsg->dst = mstb;
3007 build_clear_payload_id_table(msg: txmsg);
3008
3009 drm_dp_queue_down_tx(mgr, txmsg);
3010
3011 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3012 if (ret > 0 && txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3013 drm_dbg_kms(mgr->dev, "clear payload table id nak received\n");
3014
3015 kfree(objp: txmsg);
3016}
3017
3018static int
3019drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
3020 struct drm_dp_mst_branch *mstb,
3021 struct drm_dp_mst_port *port)
3022{
3023 struct drm_dp_enum_path_resources_ack_reply *path_res;
3024 struct drm_dp_sideband_msg_tx *txmsg;
3025 int ret;
3026
3027 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3028 if (!txmsg)
3029 return -ENOMEM;
3030
3031 txmsg->dst = mstb;
3032 build_enum_path_resources(msg: txmsg, port_num: port->port_num);
3033
3034 drm_dp_queue_down_tx(mgr, txmsg);
3035
3036 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3037 if (ret > 0) {
3038 ret = 0;
3039 path_res = &txmsg->reply.u.path_resources;
3040
3041 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
3042 drm_dbg_kms(mgr->dev, "enum path resources nak received\n");
3043 } else {
3044 if (port->port_num != path_res->port_number)
3045 DRM_ERROR("got incorrect port in response\n");
3046
3047 drm_dbg_kms(mgr->dev, "enum path resources %d: %d %d\n",
3048 path_res->port_number,
3049 path_res->full_payload_bw_number,
3050 path_res->avail_payload_bw_number);
3051
3052 /*
3053 * If something changed, make sure we send a
3054 * hotplug
3055 */
3056 if (port->full_pbn != path_res->full_payload_bw_number ||
3057 port->fec_capable != path_res->fec_capable)
3058 ret = 1;
3059
3060 port->full_pbn = path_res->full_payload_bw_number;
3061 port->fec_capable = path_res->fec_capable;
3062 }
3063 }
3064
3065 kfree(objp: txmsg);
3066 return ret;
3067}
3068
3069static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
3070{
3071 if (!mstb->port_parent)
3072 return NULL;
3073
3074 if (mstb->port_parent->mstb != mstb)
3075 return mstb->port_parent;
3076
3077 return drm_dp_get_last_connected_port_to_mstb(mstb: mstb->port_parent->parent);
3078}
3079
3080/*
3081 * Searches upwards in the topology starting from mstb to try to find the
3082 * closest available parent of mstb that's still connected to the rest of the
3083 * topology. This can be used in order to perform operations like releasing
3084 * payloads, where the branch device which owned the payload may no longer be
3085 * around and thus would require that the payload on the last living relative
3086 * be freed instead.
3087 */
3088static struct drm_dp_mst_branch *
3089drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
3090 struct drm_dp_mst_branch *mstb,
3091 int *port_num)
3092{
3093 struct drm_dp_mst_branch *rmstb = NULL;
3094 struct drm_dp_mst_port *found_port;
3095
3096 mutex_lock(&mgr->lock);
3097 if (!mgr->mst_primary)
3098 goto out;
3099
3100 do {
3101 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
3102 if (!found_port)
3103 break;
3104
3105 if (drm_dp_mst_topology_try_get_mstb(mstb: found_port->parent)) {
3106 rmstb = found_port->parent;
3107 *port_num = found_port->port_num;
3108 } else {
3109 /* Search again, starting from this parent */
3110 mstb = found_port->parent;
3111 }
3112 } while (!rmstb);
3113out:
3114 mutex_unlock(lock: &mgr->lock);
3115 return rmstb;
3116}
3117
3118static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
3119 struct drm_dp_mst_port *port,
3120 int id,
3121 int pbn)
3122{
3123 struct drm_dp_sideband_msg_tx *txmsg;
3124 struct drm_dp_mst_branch *mstb;
3125 int ret, port_num;
3126 u8 sinks[DRM_DP_MAX_SDP_STREAMS];
3127 int i;
3128
3129 port_num = port->port_num;
3130 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, mstb: port->parent);
3131 if (!mstb) {
3132 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
3133 mstb: port->parent,
3134 port_num: &port_num);
3135
3136 if (!mstb)
3137 return -EINVAL;
3138 }
3139
3140 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3141 if (!txmsg) {
3142 ret = -ENOMEM;
3143 goto fail_put;
3144 }
3145
3146 for (i = 0; i < port->num_sdp_streams; i++)
3147 sinks[i] = i;
3148
3149 txmsg->dst = mstb;
3150 build_allocate_payload(msg: txmsg, port_num,
3151 vcpi: id,
3152 pbn, number_sdp_streams: port->num_sdp_streams, sdp_stream_sink: sinks);
3153
3154 drm_dp_queue_down_tx(mgr, txmsg);
3155
3156 /*
3157 * FIXME: there is a small chance that between getting the last
3158 * connected mstb and sending the payload message, the last connected
3159 * mstb could also be removed from the topology. In the future, this
3160 * needs to be fixed by restarting the
3161 * drm_dp_get_last_connected_port_and_mstb() search in the event of a
3162 * timeout if the topology is still connected to the system.
3163 */
3164 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3165 if (ret > 0) {
3166 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3167 ret = -EINVAL;
3168 else
3169 ret = 0;
3170 }
3171 kfree(objp: txmsg);
3172fail_put:
3173 drm_dp_mst_topology_put_mstb(mstb);
3174 return ret;
3175}
3176
3177int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
3178 struct drm_dp_mst_port *port, bool power_up)
3179{
3180 struct drm_dp_sideband_msg_tx *txmsg;
3181 int ret;
3182
3183 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3184 if (!port)
3185 return -EINVAL;
3186
3187 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3188 if (!txmsg) {
3189 drm_dp_mst_topology_put_port(port);
3190 return -ENOMEM;
3191 }
3192
3193 txmsg->dst = port->parent;
3194 build_power_updown_phy(msg: txmsg, port_num: port->port_num, power_up);
3195 drm_dp_queue_down_tx(mgr, txmsg);
3196
3197 ret = drm_dp_mst_wait_tx_reply(mstb: port->parent, txmsg);
3198 if (ret > 0) {
3199 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3200 ret = -EINVAL;
3201 else
3202 ret = 0;
3203 }
3204 kfree(objp: txmsg);
3205 drm_dp_mst_topology_put_port(port);
3206
3207 return ret;
3208}
3209EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
3210
3211int drm_dp_send_query_stream_enc_status(struct drm_dp_mst_topology_mgr *mgr,
3212 struct drm_dp_mst_port *port,
3213 struct drm_dp_query_stream_enc_status_ack_reply *status)
3214{
3215 struct drm_dp_mst_topology_state *state;
3216 struct drm_dp_mst_atomic_payload *payload;
3217 struct drm_dp_sideband_msg_tx *txmsg;
3218 u8 nonce[7];
3219 int ret;
3220
3221 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3222 if (!txmsg)
3223 return -ENOMEM;
3224
3225 port = drm_dp_mst_topology_get_port_validated(mgr, port);
3226 if (!port) {
3227 ret = -EINVAL;
3228 goto out_get_port;
3229 }
3230
3231 get_random_bytes(buf: nonce, len: sizeof(nonce));
3232
3233 drm_modeset_lock(lock: &mgr->base.lock, NULL);
3234 state = to_drm_dp_mst_topology_state(state: mgr->base.state);
3235 payload = drm_atomic_get_mst_payload_state(state, port);
3236
3237 /*
3238 * "Source device targets the QUERY_STREAM_ENCRYPTION_STATUS message
3239 * transaction at the MST Branch device directly connected to the
3240 * Source"
3241 */
3242 txmsg->dst = mgr->mst_primary;
3243
3244 build_query_stream_enc_status(msg: txmsg, stream_id: payload->vcpi, q_id: nonce);
3245
3246 drm_dp_queue_down_tx(mgr, txmsg);
3247
3248 ret = drm_dp_mst_wait_tx_reply(mstb: mgr->mst_primary, txmsg);
3249 if (ret < 0) {
3250 goto out;
3251 } else if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
3252 drm_dbg_kms(mgr->dev, "query encryption status nak received\n");
3253 ret = -ENXIO;
3254 goto out;
3255 }
3256
3257 ret = 0;
3258 memcpy(status, &txmsg->reply.u.enc_status, sizeof(*status));
3259
3260out:
3261 drm_modeset_unlock(lock: &mgr->base.lock);
3262 drm_dp_mst_topology_put_port(port);
3263out_get_port:
3264 kfree(objp: txmsg);
3265 return ret;
3266}
3267EXPORT_SYMBOL(drm_dp_send_query_stream_enc_status);
3268
3269static int drm_dp_create_payload_at_dfp(struct drm_dp_mst_topology_mgr *mgr,
3270 struct drm_dp_mst_atomic_payload *payload)
3271{
3272 return drm_dp_dpcd_write_payload(aux: mgr->aux, vcpid: payload->vcpi, start_time_slot: payload->vc_start_slot,
3273 time_slot_count: payload->time_slots);
3274}
3275
3276static int drm_dp_create_payload_to_remote(struct drm_dp_mst_topology_mgr *mgr,
3277 struct drm_dp_mst_atomic_payload *payload)
3278{
3279 int ret;
3280 struct drm_dp_mst_port *port = drm_dp_mst_topology_get_port_validated(mgr, port: payload->port);
3281
3282 if (!port)
3283 return -EIO;
3284
3285 ret = drm_dp_payload_send_msg(mgr, port, id: payload->vcpi, pbn: payload->pbn);
3286 drm_dp_mst_topology_put_port(port);
3287 return ret;
3288}
3289
3290static void drm_dp_destroy_payload_at_remote_and_dfp(struct drm_dp_mst_topology_mgr *mgr,
3291 struct drm_dp_mst_topology_state *mst_state,
3292 struct drm_dp_mst_atomic_payload *payload)
3293{
3294 drm_dbg_kms(mgr->dev, "\n");
3295
3296 /* it's okay for these to fail */
3297 if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE) {
3298 drm_dp_payload_send_msg(mgr, port: payload->port, id: payload->vcpi, pbn: 0);
3299 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP;
3300 }
3301
3302 if (payload->payload_allocation_status == DRM_DP_MST_PAYLOAD_ALLOCATION_DFP)
3303 drm_dp_dpcd_write_payload(aux: mgr->aux, vcpid: payload->vcpi, start_time_slot: payload->vc_start_slot, time_slot_count: 0);
3304}
3305
3306/**
3307 * drm_dp_add_payload_part1() - Execute payload update part 1
3308 * @mgr: Manager to use.
3309 * @mst_state: The MST atomic state
3310 * @payload: The payload to write
3311 *
3312 * Determines the starting time slot for the given payload, and programs the VCPI for this payload
3313 * into the DPCD of DPRX. After calling this, the driver should generate ACT and payload packets.
3314 *
3315 * Returns: 0 on success, error code on failure.
3316 */
3317int drm_dp_add_payload_part1(struct drm_dp_mst_topology_mgr *mgr,
3318 struct drm_dp_mst_topology_state *mst_state,
3319 struct drm_dp_mst_atomic_payload *payload)
3320{
3321 struct drm_dp_mst_port *port;
3322 int ret;
3323
3324 /* Update mst mgr info */
3325 if (mgr->payload_count == 0)
3326 mgr->next_start_slot = mst_state->start_slot;
3327
3328 payload->vc_start_slot = mgr->next_start_slot;
3329
3330 mgr->payload_count++;
3331 mgr->next_start_slot += payload->time_slots;
3332
3333 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_LOCAL;
3334
3335 /* Allocate payload to immediate downstream facing port */
3336 port = drm_dp_mst_topology_get_port_validated(mgr, port: payload->port);
3337 if (!port) {
3338 drm_dbg_kms(mgr->dev,
3339 "VCPI %d for port %p not in topology, not creating a payload to remote\n",
3340 payload->vcpi, payload->port);
3341 return -EIO;
3342 }
3343
3344 ret = drm_dp_create_payload_at_dfp(mgr, payload);
3345 if (ret < 0) {
3346 drm_dbg_kms(mgr->dev, "Failed to create MST payload for port %p: %d\n",
3347 payload->port, ret);
3348 goto put_port;
3349 }
3350
3351 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_DFP;
3352
3353put_port:
3354 drm_dp_mst_topology_put_port(port);
3355
3356 return ret;
3357}
3358EXPORT_SYMBOL(drm_dp_add_payload_part1);
3359
3360/**
3361 * drm_dp_remove_payload_part1() - Remove an MST payload along the virtual channel
3362 * @mgr: Manager to use.
3363 * @mst_state: The MST atomic state
3364 * @payload: The payload to remove
3365 *
3366 * Removes a payload along the virtual channel if it was successfully allocated.
3367 * After calling this, the driver should set HW to generate ACT and then switch to new
3368 * payload allocation state.
3369 */
3370void drm_dp_remove_payload_part1(struct drm_dp_mst_topology_mgr *mgr,
3371 struct drm_dp_mst_topology_state *mst_state,
3372 struct drm_dp_mst_atomic_payload *payload)
3373{
3374 /* Remove remote payload allocation */
3375 bool send_remove = false;
3376
3377 mutex_lock(&mgr->lock);
3378 send_remove = drm_dp_mst_port_downstream_of_branch(port: payload->port, branch: mgr->mst_primary);
3379 mutex_unlock(lock: &mgr->lock);
3380
3381 if (send_remove)
3382 drm_dp_destroy_payload_at_remote_and_dfp(mgr, mst_state, payload);
3383 else
3384 drm_dbg_kms(mgr->dev, "Payload for VCPI %d not in topology, not sending remove\n",
3385 payload->vcpi);
3386
3387 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_LOCAL;
3388}
3389EXPORT_SYMBOL(drm_dp_remove_payload_part1);
3390
3391/**
3392 * drm_dp_remove_payload_part2() - Remove an MST payload locally
3393 * @mgr: Manager to use.
3394 * @mst_state: The MST atomic state
3395 * @old_payload: The payload with its old state
3396 * @new_payload: The payload with its latest state
3397 *
3398 * Updates the starting time slots of all other payloads which would have been shifted towards
3399 * the start of the payload ID table as a result of removing a payload. Driver should call this
3400 * function whenever it removes a payload in its HW. It's independent to the result of payload
3401 * allocation/deallocation at branch devices along the virtual channel.
3402 */
3403void drm_dp_remove_payload_part2(struct drm_dp_mst_topology_mgr *mgr,
3404 struct drm_dp_mst_topology_state *mst_state,
3405 const struct drm_dp_mst_atomic_payload *old_payload,
3406 struct drm_dp_mst_atomic_payload *new_payload)
3407{
3408 struct drm_dp_mst_atomic_payload *pos;
3409
3410 /* Remove local payload allocation */
3411 list_for_each_entry(pos, &mst_state->payloads, next) {
3412 if (pos != new_payload && pos->vc_start_slot > new_payload->vc_start_slot)
3413 pos->vc_start_slot -= old_payload->time_slots;
3414 }
3415 new_payload->vc_start_slot = -1;
3416
3417 mgr->payload_count--;
3418 mgr->next_start_slot -= old_payload->time_slots;
3419
3420 if (new_payload->delete)
3421 drm_dp_mst_put_port_malloc(new_payload->port);
3422
3423 new_payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_NONE;
3424}
3425EXPORT_SYMBOL(drm_dp_remove_payload_part2);
3426/**
3427 * drm_dp_add_payload_part2() - Execute payload update part 2
3428 * @mgr: Manager to use.
3429 * @payload: The payload to update
3430 *
3431 * If @payload was successfully assigned a starting time slot by drm_dp_add_payload_part1(), this
3432 * function will send the sideband messages to finish allocating this payload.
3433 *
3434 * Returns: 0 on success, negative error code on failure.
3435 */
3436int drm_dp_add_payload_part2(struct drm_dp_mst_topology_mgr *mgr,
3437 struct drm_dp_mst_atomic_payload *payload)
3438{
3439 int ret = 0;
3440
3441 /* Skip failed payloads */
3442 if (payload->payload_allocation_status != DRM_DP_MST_PAYLOAD_ALLOCATION_DFP) {
3443 drm_dbg_kms(mgr->dev, "Part 1 of payload creation for %s failed, skipping part 2\n",
3444 payload->port->connector->name);
3445 return -EIO;
3446 }
3447
3448 /* Allocate payload to remote end */
3449 ret = drm_dp_create_payload_to_remote(mgr, payload);
3450 if (ret < 0)
3451 drm_err(mgr->dev, "Step 2 of creating MST payload for %p failed: %d\n",
3452 payload->port, ret);
3453 else
3454 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_REMOTE;
3455
3456 return ret;
3457}
3458EXPORT_SYMBOL(drm_dp_add_payload_part2);
3459
3460static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
3461 struct drm_dp_mst_port *port,
3462 int offset, int size, u8 *bytes)
3463{
3464 int ret = 0;
3465 struct drm_dp_sideband_msg_tx *txmsg;
3466 struct drm_dp_mst_branch *mstb;
3467
3468 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, mstb: port->parent);
3469 if (!mstb)
3470 return -EINVAL;
3471
3472 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3473 if (!txmsg) {
3474 ret = -ENOMEM;
3475 goto fail_put;
3476 }
3477
3478 build_dpcd_read(msg: txmsg, port_num: port->port_num, offset, num_bytes: size);
3479 txmsg->dst = port->parent;
3480
3481 drm_dp_queue_down_tx(mgr, txmsg);
3482
3483 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3484 if (ret < 0)
3485 goto fail_free;
3486
3487 if (txmsg->reply.reply_type == 1) {
3488 drm_dbg_kms(mgr->dev, "mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
3489 mstb, port->port_num, offset, size);
3490 ret = -EIO;
3491 goto fail_free;
3492 }
3493
3494 if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
3495 ret = -EPROTO;
3496 goto fail_free;
3497 }
3498
3499 ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,
3500 size);
3501 memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret);
3502
3503fail_free:
3504 kfree(objp: txmsg);
3505fail_put:
3506 drm_dp_mst_topology_put_mstb(mstb);
3507
3508 return ret;
3509}
3510
3511static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
3512 struct drm_dp_mst_port *port,
3513 int offset, int size, u8 *bytes)
3514{
3515 int ret;
3516 struct drm_dp_sideband_msg_tx *txmsg;
3517 struct drm_dp_mst_branch *mstb;
3518
3519 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, mstb: port->parent);
3520 if (!mstb)
3521 return -EINVAL;
3522
3523 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3524 if (!txmsg) {
3525 ret = -ENOMEM;
3526 goto fail_put;
3527 }
3528
3529 build_dpcd_write(msg: txmsg, port_num: port->port_num, offset, num_bytes: size, bytes);
3530 txmsg->dst = mstb;
3531
3532 drm_dp_queue_down_tx(mgr, txmsg);
3533
3534 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3535 if (ret > 0) {
3536 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
3537 ret = -EIO;
3538 else
3539 ret = size;
3540 }
3541
3542 kfree(objp: txmsg);
3543fail_put:
3544 drm_dp_mst_topology_put_mstb(mstb);
3545 return ret;
3546}
3547
3548static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
3549{
3550 struct drm_dp_sideband_msg_reply_body reply;
3551
3552 reply.reply_type = DP_SIDEBAND_REPLY_ACK;
3553 reply.req_type = req_type;
3554 drm_dp_encode_sideband_reply(rep: &reply, raw: msg);
3555 return 0;
3556}
3557
3558static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
3559 struct drm_dp_mst_branch *mstb,
3560 int req_type, bool broadcast)
3561{
3562 struct drm_dp_sideband_msg_tx *txmsg;
3563
3564 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3565 if (!txmsg)
3566 return -ENOMEM;
3567
3568 txmsg->dst = mstb;
3569 drm_dp_encode_up_ack_reply(msg: txmsg, req_type);
3570
3571 mutex_lock(&mgr->qlock);
3572 /* construct a chunk from the first msg in the tx_msg queue */
3573 process_single_tx_qlock(mgr, txmsg, up: true);
3574 mutex_unlock(lock: &mgr->qlock);
3575
3576 kfree(objp: txmsg);
3577 return 0;
3578}
3579
3580/**
3581 * drm_dp_get_vc_payload_bw - get the VC payload BW for an MTP link
3582 * @link_rate: link rate in 10kbits/s units
3583 * @link_lane_count: lane count
3584 *
3585 * Calculate the total bandwidth of a MultiStream Transport link. The returned
3586 * value is in units of PBNs/(timeslots/1 MTP). This value can be used to
3587 * convert the number of PBNs required for a given stream to the number of
3588 * timeslots this stream requires in each MTP.
3589 *
3590 * Returns the BW / timeslot value in 20.12 fixed point format.
3591 */
3592fixed20_12 drm_dp_get_vc_payload_bw(int link_rate, int link_lane_count)
3593{
3594 int ch_coding_efficiency =
3595 drm_dp_bw_channel_coding_efficiency(is_uhbr: drm_dp_is_uhbr_rate(link_rate));
3596 fixed20_12 ret;
3597
3598 /* See DP v2.0 2.6.4.2, 2.7.6.3 VCPayload_Bandwidth_for_OneTimeSlotPer_MTP_Allocation */
3599 ret.full = DIV_ROUND_DOWN_ULL(mul_u32_u32(link_rate * link_lane_count,
3600 ch_coding_efficiency),
3601 (1000000ULL * 8 * 5400) >> 12);
3602
3603 return ret;
3604}
3605EXPORT_SYMBOL(drm_dp_get_vc_payload_bw);
3606
3607/**
3608 * drm_dp_read_mst_cap() - Read the sink's MST mode capability
3609 * @aux: The DP AUX channel to use
3610 * @dpcd: A cached copy of the DPCD capabilities for this sink
3611 *
3612 * Returns: enum drm_dp_mst_mode to indicate MST mode capability
3613 */
3614enum drm_dp_mst_mode drm_dp_read_mst_cap(struct drm_dp_aux *aux,
3615 const u8 dpcd[DP_RECEIVER_CAP_SIZE])
3616{
3617 u8 mstm_cap;
3618
3619 if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_12)
3620 return DRM_DP_SST;
3621
3622 if (drm_dp_dpcd_read_byte(aux, DP_MSTM_CAP, valuep: &mstm_cap) < 0)
3623 return DRM_DP_SST;
3624
3625 if (mstm_cap & DP_MST_CAP)
3626 return DRM_DP_MST;
3627
3628 if (mstm_cap & DP_SINGLE_STREAM_SIDEBAND_MSG)
3629 return DRM_DP_SST_SIDEBAND_MSG;
3630
3631 return DRM_DP_SST;
3632}
3633EXPORT_SYMBOL(drm_dp_read_mst_cap);
3634
3635/**
3636 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
3637 * @mgr: manager to set state for
3638 * @mst_state: true to enable MST on this connector - false to disable.
3639 *
3640 * This is called by the driver when it detects an MST capable device plugged
3641 * into a DP MST capable port, or when a DP MST capable device is unplugged.
3642 */
3643int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
3644{
3645 int ret = 0;
3646 struct drm_dp_mst_branch *mstb = NULL;
3647
3648 mutex_lock(&mgr->lock);
3649 if (mst_state == mgr->mst_state)
3650 goto out_unlock;
3651
3652 mgr->mst_state = mst_state;
3653 /* set the device into MST mode */
3654 if (mst_state) {
3655 WARN_ON(mgr->mst_primary);
3656
3657 /* get dpcd info */
3658 ret = drm_dp_read_dpcd_caps(aux: mgr->aux, dpcd: mgr->dpcd);
3659 if (ret < 0) {
3660 drm_dbg_kms(mgr->dev, "%s: failed to read DPCD, ret %d\n",
3661 mgr->aux->name, ret);
3662 goto out_unlock;
3663 }
3664
3665 /* add initial branch device at LCT 1 */
3666 mstb = drm_dp_add_mst_branch_device(lct: 1, NULL);
3667 if (mstb == NULL) {
3668 ret = -ENOMEM;
3669 goto out_unlock;
3670 }
3671 mstb->mgr = mgr;
3672
3673 /* give this the main reference */
3674 mgr->mst_primary = mstb;
3675 drm_dp_mst_topology_get_mstb(mstb: mgr->mst_primary);
3676
3677 ret = drm_dp_dpcd_write_byte(aux: mgr->aux, DP_MSTM_CTRL,
3678 DP_MST_EN |
3679 DP_UP_REQ_EN |
3680 DP_UPSTREAM_IS_SRC);
3681 if (ret < 0)
3682 goto out_unlock;
3683
3684 /* Write reset payload */
3685 drm_dp_dpcd_clear_payload(aux: mgr->aux);
3686
3687 drm_dp_mst_queue_probe_work(mgr);
3688
3689 ret = 0;
3690 } else {
3691 /* disable MST on the device */
3692 mstb = mgr->mst_primary;
3693 mgr->mst_primary = NULL;
3694 /* this can fail if the device is gone */
3695 drm_dp_dpcd_write_byte(aux: mgr->aux, DP_MSTM_CTRL, value: 0);
3696 ret = 0;
3697 mgr->payload_id_table_cleared = false;
3698
3699 mgr->reset_rx_state = true;
3700 }
3701
3702out_unlock:
3703 mutex_unlock(lock: &mgr->lock);
3704 if (mstb)
3705 drm_dp_mst_topology_put_mstb(mstb);
3706 return ret;
3707
3708}
3709EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
3710
3711static void
3712drm_dp_mst_topology_mgr_invalidate_mstb(struct drm_dp_mst_branch *mstb)
3713{
3714 struct drm_dp_mst_port *port;
3715
3716 /* The link address will need to be re-sent on resume */
3717 mstb->link_address_sent = false;
3718
3719 list_for_each_entry(port, &mstb->ports, next)
3720 if (port->mstb)
3721 drm_dp_mst_topology_mgr_invalidate_mstb(mstb: port->mstb);
3722}
3723
3724/**
3725 * drm_dp_mst_topology_queue_probe - Queue a topology probe
3726 * @mgr: manager to probe
3727 *
3728 * Queue a work to probe the MST topology. Driver's should call this only to
3729 * sync the topology's HW->SW state after the MST link's parameters have
3730 * changed in a way the state could've become out-of-sync. This is the case
3731 * for instance when the link rate between the source and first downstream
3732 * branch device has switched between UHBR and non-UHBR rates. Except of those
3733 * cases - for instance when a sink gets plugged/unplugged to a port - the SW
3734 * state will get updated automatically via MST UP message notifications.
3735 */
3736void drm_dp_mst_topology_queue_probe(struct drm_dp_mst_topology_mgr *mgr)
3737{
3738 mutex_lock(&mgr->lock);
3739
3740 if (drm_WARN_ON(mgr->dev, !mgr->mst_state || !mgr->mst_primary))
3741 goto out_unlock;
3742
3743 drm_dp_mst_topology_mgr_invalidate_mstb(mstb: mgr->mst_primary);
3744 drm_dp_mst_queue_probe_work(mgr);
3745
3746out_unlock:
3747 mutex_unlock(lock: &mgr->lock);
3748}
3749EXPORT_SYMBOL(drm_dp_mst_topology_queue_probe);
3750
3751/**
3752 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
3753 * @mgr: manager to suspend
3754 *
3755 * This function tells the MST device that we can't handle UP messages
3756 * anymore. This should stop it from sending any since we are suspended.
3757 */
3758void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
3759{
3760 mutex_lock(&mgr->lock);
3761 drm_dp_dpcd_write_byte(aux: mgr->aux, DP_MSTM_CTRL,
3762 DP_MST_EN | DP_UPSTREAM_IS_SRC);
3763 mutex_unlock(lock: &mgr->lock);
3764 flush_work(work: &mgr->up_req_work);
3765 flush_work(work: &mgr->work);
3766 flush_work(work: &mgr->delayed_destroy_work);
3767
3768 mutex_lock(&mgr->lock);
3769 if (mgr->mst_state && mgr->mst_primary)
3770 drm_dp_mst_topology_mgr_invalidate_mstb(mstb: mgr->mst_primary);
3771 mutex_unlock(lock: &mgr->lock);
3772}
3773EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
3774
3775/**
3776 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
3777 * @mgr: manager to resume
3778 * @sync: whether or not to perform topology reprobing synchronously
3779 *
3780 * This will fetch DPCD and see if the device is still there,
3781 * if it is, it will rewrite the MSTM control bits, and return.
3782 *
3783 * If the device fails this returns -1, and the driver should do
3784 * a full MST reprobe, in case we were undocked.
3785 *
3786 * During system resume (where it is assumed that the driver will be calling
3787 * drm_atomic_helper_resume()) this function should be called beforehand with
3788 * @sync set to true. In contexts like runtime resume where the driver is not
3789 * expected to be calling drm_atomic_helper_resume(), this function should be
3790 * called with @sync set to false in order to avoid deadlocking.
3791 *
3792 * Returns: -1 if the MST topology was removed while we were suspended, 0
3793 * otherwise.
3794 */
3795int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr,
3796 bool sync)
3797{
3798 u8 buf[UUID_SIZE];
3799 guid_t guid;
3800 int ret;
3801
3802 mutex_lock(&mgr->lock);
3803 if (!mgr->mst_primary)
3804 goto out_fail;
3805
3806 if (drm_dp_read_dpcd_caps(aux: mgr->aux, dpcd: mgr->dpcd) < 0) {
3807 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
3808 goto out_fail;
3809 }
3810
3811 ret = drm_dp_dpcd_write_byte(aux: mgr->aux, DP_MSTM_CTRL,
3812 DP_MST_EN |
3813 DP_UP_REQ_EN |
3814 DP_UPSTREAM_IS_SRC);
3815 if (ret < 0) {
3816 drm_dbg_kms(mgr->dev, "mst write failed - undocked during suspend?\n");
3817 goto out_fail;
3818 }
3819
3820 /* Some hubs forget their guids after they resume */
3821 ret = drm_dp_dpcd_read_data(aux: mgr->aux, DP_GUID, buffer: buf, size: sizeof(buf));
3822 if (ret < 0) {
3823 drm_dbg_kms(mgr->dev, "dpcd read failed - undocked during suspend?\n");
3824 goto out_fail;
3825 }
3826
3827 import_guid(dst: &guid, src: buf);
3828
3829 ret = drm_dp_check_mstb_guid(mstb: mgr->mst_primary, guid: &guid);
3830 if (ret) {
3831 drm_dbg_kms(mgr->dev, "check mstb failed - undocked during suspend?\n");
3832 goto out_fail;
3833 }
3834
3835 /*
3836 * For the final step of resuming the topology, we need to bring the
3837 * state of our in-memory topology back into sync with reality. So,
3838 * restart the probing process as if we're probing a new hub
3839 */
3840 drm_dp_mst_queue_probe_work(mgr);
3841 mutex_unlock(lock: &mgr->lock);
3842
3843 if (sync) {
3844 drm_dbg_kms(mgr->dev,
3845 "Waiting for link probe work to finish re-syncing topology...\n");
3846 flush_work(work: &mgr->work);
3847 }
3848
3849 return 0;
3850
3851out_fail:
3852 mutex_unlock(lock: &mgr->lock);
3853 return -1;
3854}
3855EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
3856
3857static void reset_msg_rx_state(struct drm_dp_sideband_msg_rx *msg)
3858{
3859 memset(msg, 0, sizeof(*msg));
3860}
3861
3862static bool
3863drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up,
3864 struct drm_dp_mst_branch **mstb)
3865{
3866 int len;
3867 u8 replyblock[32];
3868 int replylen, curreply;
3869 int ret;
3870 u8 hdrlen;
3871 struct drm_dp_sideband_msg_hdr hdr;
3872 struct drm_dp_sideband_msg_rx *msg =
3873 up ? &mgr->up_req_recv : &mgr->down_rep_recv;
3874 int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE :
3875 DP_SIDEBAND_MSG_DOWN_REP_BASE;
3876
3877 if (!up)
3878 *mstb = NULL;
3879
3880 len = min(mgr->max_dpcd_transaction_bytes, 16);
3881 ret = drm_dp_dpcd_read_data(aux: mgr->aux, offset: basereg, buffer: replyblock, size: len);
3882 if (ret < 0) {
3883 drm_dbg_kms(mgr->dev, "failed to read DPCD down rep %d %d\n", len, ret);
3884 return false;
3885 }
3886
3887 ret = drm_dp_decode_sideband_msg_hdr(mgr, hdr: &hdr, buf: replyblock, buflen: len, hdrlen: &hdrlen);
3888 if (ret == false) {
3889 print_hex_dump(KERN_DEBUG, prefix_str: "failed hdr", prefix_type: DUMP_PREFIX_NONE, rowsize: 16,
3890 groupsize: 1, buf: replyblock, len, ascii: false);
3891 drm_dbg_kms(mgr->dev, "ERROR: failed header\n");
3892 return false;
3893 }
3894
3895 if (!up) {
3896 /* Caller is responsible for giving back this reference */
3897 *mstb = drm_dp_get_mst_branch_device(mgr, lct: hdr.lct, rad: hdr.rad);
3898 if (!*mstb) {
3899 drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr.lct);
3900 return false;
3901 }
3902 }
3903
3904 if (!drm_dp_sideband_msg_set_header(msg, hdr: &hdr, hdrlen)) {
3905 drm_dbg_kms(mgr->dev, "sideband msg set header failed %d\n", replyblock[0]);
3906 return false;
3907 }
3908
3909 replylen = min(msg->curchunk_len, (u8)(len - hdrlen));
3910 ret = drm_dp_sideband_append_payload(msg, replybuf: replyblock + hdrlen, replybuflen: replylen);
3911 if (!ret) {
3912 drm_dbg_kms(mgr->dev, "sideband msg build failed %d\n", replyblock[0]);
3913 return false;
3914 }
3915
3916 replylen = msg->curchunk_len + msg->curchunk_hdrlen - len;
3917 curreply = len;
3918 while (replylen > 0) {
3919 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
3920 ret = drm_dp_dpcd_read_data(aux: mgr->aux, offset: basereg + curreply,
3921 buffer: replyblock, size: len);
3922 if (ret < 0) {
3923 drm_dbg_kms(mgr->dev, "failed to read a chunk (len %d, ret %d)\n",
3924 len, ret);
3925 return false;
3926 }
3927
3928 ret = drm_dp_sideband_append_payload(msg, replybuf: replyblock, replybuflen: len);
3929 if (!ret) {
3930 drm_dbg_kms(mgr->dev, "failed to build sideband msg\n");
3931 return false;
3932 }
3933
3934 curreply += len;
3935 replylen -= len;
3936 }
3937 return true;
3938}
3939
3940static int get_msg_request_type(u8 data)
3941{
3942 return data & 0x7f;
3943}
3944
3945static bool verify_rx_request_type(struct drm_dp_mst_topology_mgr *mgr,
3946 const struct drm_dp_sideband_msg_tx *txmsg,
3947 const struct drm_dp_sideband_msg_rx *rxmsg)
3948{
3949 const struct drm_dp_sideband_msg_hdr *hdr = &rxmsg->initial_hdr;
3950 const struct drm_dp_mst_branch *mstb = txmsg->dst;
3951 int tx_req_type = get_msg_request_type(data: txmsg->msg[0]);
3952 int rx_req_type = get_msg_request_type(data: rxmsg->msg[0]);
3953 char rad_str[64];
3954
3955 if (tx_req_type == rx_req_type)
3956 return true;
3957
3958 drm_dp_mst_rad_to_str(rad: mstb->rad, lct: mstb->lct, out: rad_str, len: sizeof(rad_str));
3959 drm_dbg_kms(mgr->dev,
3960 "Got unexpected MST reply, mstb: %p seqno: %d lct: %d rad: %s rx_req_type: %s (%02x) != tx_req_type: %s (%02x)\n",
3961 mstb, hdr->seqno, mstb->lct, rad_str,
3962 drm_dp_mst_req_type_str(rx_req_type), rx_req_type,
3963 drm_dp_mst_req_type_str(tx_req_type), tx_req_type);
3964
3965 return false;
3966}
3967
3968static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
3969{
3970 struct drm_dp_sideband_msg_tx *txmsg;
3971 struct drm_dp_mst_branch *mstb = NULL;
3972 struct drm_dp_sideband_msg_rx *msg = &mgr->down_rep_recv;
3973
3974 if (!drm_dp_get_one_sb_msg(mgr, up: false, mstb: &mstb))
3975 goto out_clear_reply;
3976
3977 /* Multi-packet message transmission, don't clear the reply */
3978 if (!msg->have_eomt)
3979 goto out;
3980
3981 /* find the message */
3982 mutex_lock(&mgr->qlock);
3983
3984 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq,
3985 struct drm_dp_sideband_msg_tx, next);
3986
3987 /* Were we actually expecting a response, and from this mstb? */
3988 if (!txmsg || txmsg->dst != mstb) {
3989 struct drm_dp_sideband_msg_hdr *hdr;
3990
3991 hdr = &msg->initial_hdr;
3992 drm_dbg_kms(mgr->dev, "Got MST reply with no msg %p %d %d %02x %02x\n",
3993 mstb, hdr->seqno, hdr->lct, hdr->rad[0], msg->msg[0]);
3994
3995 mutex_unlock(lock: &mgr->qlock);
3996
3997 goto out_clear_reply;
3998 }
3999
4000 if (!verify_rx_request_type(mgr, txmsg, rxmsg: msg)) {
4001 mutex_unlock(lock: &mgr->qlock);
4002
4003 goto out_clear_reply;
4004 }
4005
4006 drm_dp_sideband_parse_reply(mgr, raw: msg, msg: &txmsg->reply);
4007
4008 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
4009 drm_dbg_kms(mgr->dev,
4010 "Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
4011 txmsg->reply.req_type,
4012 drm_dp_mst_req_type_str(txmsg->reply.req_type),
4013 txmsg->reply.u.nak.reason,
4014 drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
4015 txmsg->reply.u.nak.nak_data);
4016 }
4017
4018 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
4019 list_del(entry: &txmsg->next);
4020
4021 mutex_unlock(lock: &mgr->qlock);
4022
4023 wake_up_all(&mgr->tx_waitq);
4024
4025out_clear_reply:
4026 reset_msg_rx_state(msg);
4027out:
4028 if (mstb)
4029 drm_dp_mst_topology_put_mstb(mstb);
4030
4031 return 0;
4032}
4033
4034static bool primary_mstb_probing_is_done(struct drm_dp_mst_topology_mgr *mgr)
4035{
4036 bool probing_done = false;
4037
4038 mutex_lock(&mgr->lock);
4039
4040 if (mgr->mst_primary && drm_dp_mst_topology_try_get_mstb(mstb: mgr->mst_primary)) {
4041 probing_done = mgr->mst_primary->link_address_sent;
4042 drm_dp_mst_topology_put_mstb(mstb: mgr->mst_primary);
4043 }
4044
4045 mutex_unlock(lock: &mgr->lock);
4046
4047 return probing_done;
4048}
4049
4050static inline bool
4051drm_dp_mst_process_up_req(struct drm_dp_mst_topology_mgr *mgr,
4052 struct drm_dp_pending_up_req *up_req)
4053{
4054 struct drm_dp_mst_branch *mstb = NULL;
4055 struct drm_dp_sideband_msg_req_body *msg = &up_req->msg;
4056 struct drm_dp_sideband_msg_hdr *hdr = &up_req->hdr;
4057 bool hotplug = false, dowork = false;
4058
4059 if (hdr->broadcast) {
4060 const guid_t *guid = NULL;
4061
4062 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY)
4063 guid = &msg->u.conn_stat.guid;
4064 else if (msg->req_type == DP_RESOURCE_STATUS_NOTIFY)
4065 guid = &msg->u.resource_stat.guid;
4066
4067 if (guid)
4068 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, guid);
4069 } else {
4070 mstb = drm_dp_get_mst_branch_device(mgr, lct: hdr->lct, rad: hdr->rad);
4071 }
4072
4073 if (!mstb) {
4074 drm_dbg_kms(mgr->dev, "Got MST reply from unknown device %d\n", hdr->lct);
4075 return false;
4076 }
4077
4078 /* TODO: Add missing handler for DP_RESOURCE_STATUS_NOTIFY events */
4079 if (msg->req_type == DP_CONNECTION_STATUS_NOTIFY) {
4080 if (!primary_mstb_probing_is_done(mgr)) {
4081 drm_dbg_kms(mgr->dev, "Got CSN before finish topology probing. Skip it.\n");
4082 } else {
4083 dowork = drm_dp_mst_handle_conn_stat(mstb, conn_stat: &msg->u.conn_stat);
4084 hotplug = true;
4085 }
4086 }
4087
4088 drm_dp_mst_topology_put_mstb(mstb);
4089
4090 if (dowork)
4091 queue_work(wq: system_long_wq, work: &mgr->work);
4092 return hotplug;
4093}
4094
4095static void drm_dp_mst_up_req_work(struct work_struct *work)
4096{
4097 struct drm_dp_mst_topology_mgr *mgr =
4098 container_of(work, struct drm_dp_mst_topology_mgr,
4099 up_req_work);
4100 struct drm_dp_pending_up_req *up_req;
4101 bool send_hotplug = false;
4102
4103 mutex_lock(&mgr->probe_lock);
4104 while (true) {
4105 mutex_lock(&mgr->up_req_lock);
4106 up_req = list_first_entry_or_null(&mgr->up_req_list,
4107 struct drm_dp_pending_up_req,
4108 next);
4109 if (up_req)
4110 list_del(entry: &up_req->next);
4111 mutex_unlock(lock: &mgr->up_req_lock);
4112
4113 if (!up_req)
4114 break;
4115
4116 send_hotplug |= drm_dp_mst_process_up_req(mgr, up_req);
4117 kfree(objp: up_req);
4118 }
4119 mutex_unlock(lock: &mgr->probe_lock);
4120
4121 if (send_hotplug)
4122 drm_kms_helper_hotplug_event(dev: mgr->dev);
4123}
4124
4125static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
4126{
4127 struct drm_dp_pending_up_req *up_req;
4128 struct drm_dp_mst_branch *mst_primary;
4129 int ret = 0;
4130
4131 if (!drm_dp_get_one_sb_msg(mgr, up: true, NULL))
4132 goto out_clear_reply;
4133
4134 if (!mgr->up_req_recv.have_eomt)
4135 return 0;
4136
4137 up_req = kzalloc(sizeof(*up_req), GFP_KERNEL);
4138 if (!up_req) {
4139 ret = -ENOMEM;
4140 goto out_clear_reply;
4141 }
4142
4143 INIT_LIST_HEAD(list: &up_req->next);
4144
4145 drm_dp_sideband_parse_req(mgr, raw: &mgr->up_req_recv, msg: &up_req->msg);
4146
4147 if (up_req->msg.req_type != DP_CONNECTION_STATUS_NOTIFY &&
4148 up_req->msg.req_type != DP_RESOURCE_STATUS_NOTIFY) {
4149 drm_dbg_kms(mgr->dev, "Received unknown up req type, ignoring: %x\n",
4150 up_req->msg.req_type);
4151 kfree(objp: up_req);
4152 goto out_clear_reply;
4153 }
4154
4155 mutex_lock(&mgr->lock);
4156 mst_primary = mgr->mst_primary;
4157 if (!mst_primary || !drm_dp_mst_topology_try_get_mstb(mstb: mst_primary)) {
4158 mutex_unlock(lock: &mgr->lock);
4159 kfree(objp: up_req);
4160 goto out_clear_reply;
4161 }
4162 mutex_unlock(lock: &mgr->lock);
4163
4164 drm_dp_send_up_ack_reply(mgr, mstb: mst_primary, req_type: up_req->msg.req_type,
4165 broadcast: false);
4166
4167 drm_dp_mst_topology_put_mstb(mstb: mst_primary);
4168
4169 if (up_req->msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
4170 const struct drm_dp_connection_status_notify *conn_stat =
4171 &up_req->msg.u.conn_stat;
4172
4173 drm_dbg_kms(mgr->dev, "Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n",
4174 conn_stat->port_number,
4175 conn_stat->legacy_device_plug_status,
4176 conn_stat->displayport_device_plug_status,
4177 conn_stat->message_capability_status,
4178 conn_stat->input_port,
4179 conn_stat->peer_device_type);
4180 } else if (up_req->msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
4181 const struct drm_dp_resource_status_notify *res_stat =
4182 &up_req->msg.u.resource_stat;
4183
4184 drm_dbg_kms(mgr->dev, "Got RSN: pn: %d avail_pbn %d\n",
4185 res_stat->port_number,
4186 res_stat->available_pbn);
4187 }
4188
4189 up_req->hdr = mgr->up_req_recv.initial_hdr;
4190 mutex_lock(&mgr->up_req_lock);
4191 list_add_tail(new: &up_req->next, head: &mgr->up_req_list);
4192 mutex_unlock(lock: &mgr->up_req_lock);
4193 queue_work(wq: system_long_wq, work: &mgr->up_req_work);
4194out_clear_reply:
4195 reset_msg_rx_state(msg: &mgr->up_req_recv);
4196 return ret;
4197}
4198
4199static void update_msg_rx_state(struct drm_dp_mst_topology_mgr *mgr)
4200{
4201 mutex_lock(&mgr->lock);
4202 if (mgr->reset_rx_state) {
4203 mgr->reset_rx_state = false;
4204 reset_msg_rx_state(msg: &mgr->down_rep_recv);
4205 reset_msg_rx_state(msg: &mgr->up_req_recv);
4206 }
4207 mutex_unlock(lock: &mgr->lock);
4208}
4209
4210/**
4211 * drm_dp_mst_hpd_irq_handle_event() - MST hotplug IRQ handle MST event
4212 * @mgr: manager to notify irq for.
4213 * @esi: 4 bytes from SINK_COUNT_ESI
4214 * @ack: 4 bytes used to ack events starting from SINK_COUNT_ESI
4215 * @handled: whether the hpd interrupt was consumed or not
4216 *
4217 * This should be called from the driver when it detects a HPD IRQ,
4218 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
4219 * topology manager will process the sideband messages received
4220 * as indicated in the DEVICE_SERVICE_IRQ_VECTOR_ESI0 and set the
4221 * corresponding flags that Driver has to ack the DP receiver later.
4222 *
4223 * Note that driver shall also call
4224 * drm_dp_mst_hpd_irq_send_new_request() if the 'handled' is set
4225 * after calling this function, to try to kick off a new request in
4226 * the queue if the previous message transaction is completed.
4227 *
4228 * See also:
4229 * drm_dp_mst_hpd_irq_send_new_request()
4230 */
4231int drm_dp_mst_hpd_irq_handle_event(struct drm_dp_mst_topology_mgr *mgr, const u8 *esi,
4232 u8 *ack, bool *handled)
4233{
4234 int ret = 0;
4235 int sc;
4236 *handled = false;
4237 sc = DP_GET_SINK_COUNT(esi[0]);
4238
4239 if (sc != mgr->sink_count) {
4240 mgr->sink_count = sc;
4241 *handled = true;
4242 }
4243
4244 update_msg_rx_state(mgr);
4245
4246 if (esi[1] & DP_DOWN_REP_MSG_RDY) {
4247 ret = drm_dp_mst_handle_down_rep(mgr);
4248 *handled = true;
4249 ack[1] |= DP_DOWN_REP_MSG_RDY;
4250 }
4251
4252 if (esi[1] & DP_UP_REQ_MSG_RDY) {
4253 ret |= drm_dp_mst_handle_up_req(mgr);
4254 *handled = true;
4255 ack[1] |= DP_UP_REQ_MSG_RDY;
4256 }
4257
4258 return ret;
4259}
4260EXPORT_SYMBOL(drm_dp_mst_hpd_irq_handle_event);
4261
4262/**
4263 * drm_dp_mst_hpd_irq_send_new_request() - MST hotplug IRQ kick off new request
4264 * @mgr: manager to notify irq for.
4265 *
4266 * This should be called from the driver when mst irq event is handled
4267 * and acked. Note that new down request should only be sent when
4268 * previous message transaction is completed. Source is not supposed to generate
4269 * interleaved message transactions.
4270 */
4271void drm_dp_mst_hpd_irq_send_new_request(struct drm_dp_mst_topology_mgr *mgr)
4272{
4273 struct drm_dp_sideband_msg_tx *txmsg;
4274 bool kick = true;
4275
4276 mutex_lock(&mgr->qlock);
4277 txmsg = list_first_entry_or_null(&mgr->tx_msg_downq,
4278 struct drm_dp_sideband_msg_tx, next);
4279 /* If last transaction is not completed yet*/
4280 if (!txmsg ||
4281 txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
4282 txmsg->state == DRM_DP_SIDEBAND_TX_SENT)
4283 kick = false;
4284 mutex_unlock(lock: &mgr->qlock);
4285
4286 if (kick)
4287 drm_dp_mst_kick_tx(mgr);
4288}
4289EXPORT_SYMBOL(drm_dp_mst_hpd_irq_send_new_request);
4290/**
4291 * drm_dp_mst_detect_port() - get connection status for an MST port
4292 * @connector: DRM connector for this port
4293 * @ctx: The acquisition context to use for grabbing locks
4294 * @mgr: manager for this port
4295 * @port: pointer to a port
4296 *
4297 * This returns the current connection state for a port.
4298 */
4299int
4300drm_dp_mst_detect_port(struct drm_connector *connector,
4301 struct drm_modeset_acquire_ctx *ctx,
4302 struct drm_dp_mst_topology_mgr *mgr,
4303 struct drm_dp_mst_port *port)
4304{
4305 int ret;
4306
4307 /* we need to search for the port in the mgr in case it's gone */
4308 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4309 if (!port)
4310 return connector_status_disconnected;
4311
4312 ret = drm_modeset_lock(lock: &mgr->base.lock, ctx);
4313 if (ret)
4314 goto out;
4315
4316 ret = connector_status_disconnected;
4317
4318 if (!port->ddps)
4319 goto out;
4320
4321 switch (port->pdt) {
4322 case DP_PEER_DEVICE_NONE:
4323 break;
4324 case DP_PEER_DEVICE_MST_BRANCHING:
4325 if (!port->mcs)
4326 ret = connector_status_connected;
4327 break;
4328
4329 case DP_PEER_DEVICE_SST_SINK:
4330 ret = connector_status_connected;
4331 /* for logical ports - cache the EDID */
4332 if (drm_dp_mst_port_is_logical(port) && !port->cached_edid)
4333 port->cached_edid = drm_edid_read_ddc(connector, adapter: &port->aux.ddc);
4334 break;
4335 case DP_PEER_DEVICE_DP_LEGACY_CONV:
4336 if (port->ldps)
4337 ret = connector_status_connected;
4338 break;
4339 }
4340out:
4341 drm_dp_mst_topology_put_port(port);
4342 return ret;
4343}
4344EXPORT_SYMBOL(drm_dp_mst_detect_port);
4345
4346/**
4347 * drm_dp_mst_edid_read() - get EDID for an MST port
4348 * @connector: toplevel connector to get EDID for
4349 * @mgr: manager for this port
4350 * @port: unverified pointer to a port.
4351 *
4352 * This returns an EDID for the port connected to a connector,
4353 * It validates the pointer still exists so the caller doesn't require a
4354 * reference.
4355 */
4356const struct drm_edid *drm_dp_mst_edid_read(struct drm_connector *connector,
4357 struct drm_dp_mst_topology_mgr *mgr,
4358 struct drm_dp_mst_port *port)
4359{
4360 const struct drm_edid *drm_edid;
4361
4362 /* we need to search for the port in the mgr in case it's gone */
4363 port = drm_dp_mst_topology_get_port_validated(mgr, port);
4364 if (!port)
4365 return NULL;
4366
4367 if (port->cached_edid)
4368 drm_edid = drm_edid_dup(drm_edid: port->cached_edid);
4369 else
4370 drm_edid = drm_edid_read_ddc(connector, adapter: &port->aux.ddc);
4371
4372 drm_dp_mst_topology_put_port(port);
4373
4374 return drm_edid;
4375}
4376EXPORT_SYMBOL(drm_dp_mst_edid_read);
4377
4378/**
4379 * drm_dp_mst_get_edid() - get EDID for an MST port
4380 * @connector: toplevel connector to get EDID for
4381 * @mgr: manager for this port
4382 * @port: unverified pointer to a port.
4383 *
4384 * This function is deprecated; please use drm_dp_mst_edid_read() instead.
4385 *
4386 * This returns an EDID for the port connected to a connector,
4387 * It validates the pointer still exists so the caller doesn't require a
4388 * reference.
4389 */
4390struct edid *drm_dp_mst_get_edid(struct drm_connector *connector,
4391 struct drm_dp_mst_topology_mgr *mgr,
4392 struct drm_dp_mst_port *port)
4393{
4394 const struct drm_edid *drm_edid;
4395 struct edid *edid;
4396
4397 drm_edid = drm_dp_mst_edid_read(connector, mgr, port);
4398
4399 edid = drm_edid_duplicate(edid: drm_edid_raw(drm_edid));
4400
4401 drm_edid_free(drm_edid);
4402
4403 return edid;
4404}
4405EXPORT_SYMBOL(drm_dp_mst_get_edid);
4406
4407/**
4408 * drm_dp_atomic_find_time_slots() - Find and add time slots to the state
4409 * @state: global atomic state
4410 * @mgr: MST topology manager for the port
4411 * @port: port to find time slots for
4412 * @pbn: bandwidth required for the mode in PBN
4413 *
4414 * Allocates time slots to @port, replacing any previous time slot allocations it may
4415 * have had. Any atomic drivers which support MST must call this function in
4416 * their &drm_encoder_helper_funcs.atomic_check() callback unconditionally to
4417 * change the current time slot allocation for the new state, and ensure the MST
4418 * atomic state is added whenever the state of payloads in the topology changes.
4419 *
4420 * Allocations set by this function are not checked against the bandwidth
4421 * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
4422 *
4423 * Additionally, it is OK to call this function multiple times on the same
4424 * @port as needed. It is not OK however, to call this function and
4425 * drm_dp_atomic_release_time_slots() in the same atomic check phase.
4426 *
4427 * See also:
4428 * drm_dp_atomic_release_time_slots()
4429 * drm_dp_mst_atomic_check()
4430 *
4431 * Returns:
4432 * Total slots in the atomic state assigned for this port, or a negative error
4433 * code if the port no longer exists
4434 */
4435int drm_dp_atomic_find_time_slots(struct drm_atomic_state *state,
4436 struct drm_dp_mst_topology_mgr *mgr,
4437 struct drm_dp_mst_port *port, int pbn)
4438{
4439 struct drm_dp_mst_topology_state *topology_state;
4440 struct drm_dp_mst_atomic_payload *payload = NULL;
4441 struct drm_connector_state *conn_state;
4442 int prev_slots = 0, prev_bw = 0, req_slots;
4443
4444 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
4445 if (IS_ERR(ptr: topology_state))
4446 return PTR_ERR(ptr: topology_state);
4447
4448 conn_state = drm_atomic_get_new_connector_state(state, connector: port->connector);
4449 topology_state->pending_crtc_mask |= drm_crtc_mask(crtc: conn_state->crtc);
4450
4451 /* Find the current allocation for this port, if any */
4452 payload = drm_atomic_get_mst_payload_state(topology_state, port);
4453 if (payload) {
4454 prev_slots = payload->time_slots;
4455 prev_bw = payload->pbn;
4456
4457 /*
4458 * This should never happen, unless the driver tries
4459 * releasing and allocating the same timeslot allocation,
4460 * which is an error
4461 */
4462 if (drm_WARN_ON(mgr->dev, payload->delete)) {
4463 drm_err(mgr->dev,
4464 "cannot allocate and release time slots on [MST PORT:%p] in the same state\n",
4465 port);
4466 return -EINVAL;
4467 }
4468 }
4469
4470 req_slots = DIV_ROUND_UP(dfixed_const(pbn), topology_state->pbn_div.full);
4471
4472 drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] TU %d -> %d\n",
4473 port->connector->base.id, port->connector->name,
4474 port, prev_slots, req_slots);
4475 drm_dbg_atomic(mgr->dev, "[CONNECTOR:%d:%s] [MST PORT:%p] PBN %d -> %d\n",
4476 port->connector->base.id, port->connector->name,
4477 port, prev_bw, pbn);
4478
4479 /* Add the new allocation to the state, note the VCPI isn't assigned until the end */
4480 if (!payload) {
4481 payload = kzalloc(sizeof(*payload), GFP_KERNEL);
4482 if (!payload)
4483 return -ENOMEM;
4484
4485 drm_dp_mst_get_port_malloc(port);
4486 payload->port = port;
4487 payload->vc_start_slot = -1;
4488 payload->payload_allocation_status = DRM_DP_MST_PAYLOAD_ALLOCATION_NONE;
4489 list_add(new: &payload->next, head: &topology_state->payloads);
4490 }
4491 payload->time_slots = req_slots;
4492 payload->pbn = pbn;
4493
4494 return req_slots;
4495}
4496EXPORT_SYMBOL(drm_dp_atomic_find_time_slots);
4497
4498/**
4499 * drm_dp_atomic_release_time_slots() - Release allocated time slots
4500 * @state: global atomic state
4501 * @mgr: MST topology manager for the port
4502 * @port: The port to release the time slots from
4503 *
4504 * Releases any time slots that have been allocated to a port in the atomic
4505 * state. Any atomic drivers which support MST must call this function
4506 * unconditionally in their &drm_connector_helper_funcs.atomic_check() callback.
4507 * This helper will check whether time slots would be released by the new state and
4508 * respond accordingly, along with ensuring the MST state is always added to the
4509 * atomic state whenever a new state would modify the state of payloads on the
4510 * topology.
4511 *
4512 * It is OK to call this even if @port has been removed from the system.
4513 * Additionally, it is OK to call this function multiple times on the same
4514 * @port as needed. It is not OK however, to call this function and
4515 * drm_dp_atomic_find_time_slots() on the same @port in a single atomic check
4516 * phase.
4517 *
4518 * See also:
4519 * drm_dp_atomic_find_time_slots()
4520 * drm_dp_mst_atomic_check()
4521 *
4522 * Returns:
4523 * 0 on success, negative error code otherwise
4524 */
4525int drm_dp_atomic_release_time_slots(struct drm_atomic_state *state,
4526 struct drm_dp_mst_topology_mgr *mgr,
4527 struct drm_dp_mst_port *port)
4528{
4529 struct drm_dp_mst_topology_state *topology_state;
4530 struct drm_dp_mst_atomic_payload *payload;
4531 struct drm_connector_state *old_conn_state, *new_conn_state;
4532 bool update_payload = true;
4533
4534 old_conn_state = drm_atomic_get_old_connector_state(state, connector: port->connector);
4535 if (!old_conn_state->crtc)
4536 return 0;
4537
4538 /* If the CRTC isn't disabled by this state, don't release it's payload */
4539 new_conn_state = drm_atomic_get_new_connector_state(state, connector: port->connector);
4540 if (new_conn_state->crtc) {
4541 struct drm_crtc_state *crtc_state =
4542 drm_atomic_get_new_crtc_state(state, crtc: new_conn_state->crtc);
4543
4544 /* No modeset means no payload changes, so it's safe to not pull in the MST state */
4545 if (!crtc_state || !drm_atomic_crtc_needs_modeset(state: crtc_state))
4546 return 0;
4547
4548 if (!crtc_state->mode_changed && !crtc_state->connectors_changed)
4549 update_payload = false;
4550 }
4551
4552 topology_state = drm_atomic_get_mst_topology_state(state, mgr);
4553 if (IS_ERR(ptr: topology_state))
4554 return PTR_ERR(ptr: topology_state);
4555
4556 topology_state->pending_crtc_mask |= drm_crtc_mask(crtc: old_conn_state->crtc);
4557 if (!update_payload)
4558 return 0;
4559
4560 payload = drm_atomic_get_mst_payload_state(topology_state, port);
4561 if (WARN_ON(!payload)) {
4562 drm_err(mgr->dev, "No payload for [MST PORT:%p] found in mst state %p\n",
4563 port, &topology_state->base);
4564 return -EINVAL;
4565 }
4566
4567 if (new_conn_state->crtc)
4568 return 0;
4569
4570 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] TU %d -> 0\n", port, payload->time_slots);
4571 if (!payload->delete) {
4572 payload->pbn = 0;
4573 payload->delete = true;
4574 topology_state->payload_mask &= ~BIT(payload->vcpi - 1);
4575 }
4576
4577 return 0;
4578}
4579EXPORT_SYMBOL(drm_dp_atomic_release_time_slots);
4580
4581/**
4582 * drm_dp_mst_atomic_setup_commit() - setup_commit hook for MST helpers
4583 * @state: global atomic state
4584 *
4585 * This function saves all of the &drm_crtc_commit structs in an atomic state that touch any CRTCs
4586 * currently assigned to an MST topology. Drivers must call this hook from their
4587 * &drm_mode_config_helper_funcs.atomic_commit_setup hook.
4588 *
4589 * Returns:
4590 * 0 if all CRTC commits were retrieved successfully, negative error code otherwise
4591 */
4592int drm_dp_mst_atomic_setup_commit(struct drm_atomic_state *state)
4593{
4594 struct drm_dp_mst_topology_mgr *mgr;
4595 struct drm_dp_mst_topology_state *mst_state;
4596 struct drm_crtc *crtc;
4597 struct drm_crtc_state *crtc_state;
4598 int i, j, commit_idx, num_commit_deps;
4599
4600 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
4601 if (!mst_state->pending_crtc_mask)
4602 continue;
4603
4604 num_commit_deps = hweight32(mst_state->pending_crtc_mask);
4605 mst_state->commit_deps = kmalloc_array(num_commit_deps,
4606 sizeof(*mst_state->commit_deps), GFP_KERNEL);
4607 if (!mst_state->commit_deps)
4608 return -ENOMEM;
4609 mst_state->num_commit_deps = num_commit_deps;
4610
4611 commit_idx = 0;
4612 for_each_new_crtc_in_state(state, crtc, crtc_state, j) {
4613 if (mst_state->pending_crtc_mask & drm_crtc_mask(crtc)) {
4614 mst_state->commit_deps[commit_idx++] =
4615 drm_crtc_commit_get(commit: crtc_state->commit);
4616 }
4617 }
4618 }
4619
4620 return 0;
4621}
4622EXPORT_SYMBOL(drm_dp_mst_atomic_setup_commit);
4623
4624/**
4625 * drm_dp_mst_atomic_wait_for_dependencies() - Wait for all pending commits on MST topologies,
4626 * prepare new MST state for commit
4627 * @state: global atomic state
4628 *
4629 * Goes through any MST topologies in this atomic state, and waits for any pending commits which
4630 * touched CRTCs that were/are on an MST topology to be programmed to hardware and flipped to before
4631 * returning. This is to prevent multiple non-blocking commits affecting an MST topology from racing
4632 * with eachother by forcing them to be executed sequentially in situations where the only resources
4633 * the modeset objects in these commits share are an MST topology.
4634 *
4635 * This function also prepares the new MST state for commit by performing some state preparation
4636 * which can't be done until this point, such as reading back the final VC start slots (which are
4637 * determined at commit-time) from the previous state.
4638 *
4639 * All MST drivers must call this function after calling drm_atomic_helper_wait_for_dependencies(),
4640 * or whatever their equivalent of that is.
4641 */
4642void drm_dp_mst_atomic_wait_for_dependencies(struct drm_atomic_state *state)
4643{
4644 struct drm_dp_mst_topology_state *old_mst_state, *new_mst_state;
4645 struct drm_dp_mst_topology_mgr *mgr;
4646 struct drm_dp_mst_atomic_payload *old_payload, *new_payload;
4647 int i, j, ret;
4648
4649 for_each_oldnew_mst_mgr_in_state(state, mgr, old_mst_state, new_mst_state, i) {
4650 for (j = 0; j < old_mst_state->num_commit_deps; j++) {
4651 ret = drm_crtc_commit_wait(commit: old_mst_state->commit_deps[j]);
4652 if (ret < 0)
4653 drm_err(state->dev, "Failed to wait for %s: %d\n",
4654 old_mst_state->commit_deps[j]->crtc->name, ret);
4655 }
4656
4657 /* Now that previous state is committed, it's safe to copy over the start slot
4658 * and allocation status assignments
4659 */
4660 list_for_each_entry(old_payload, &old_mst_state->payloads, next) {
4661 if (old_payload->delete)
4662 continue;
4663
4664 new_payload = drm_atomic_get_mst_payload_state(new_mst_state,
4665 old_payload->port);
4666 new_payload->vc_start_slot = old_payload->vc_start_slot;
4667 new_payload->payload_allocation_status =
4668 old_payload->payload_allocation_status;
4669 }
4670 }
4671}
4672EXPORT_SYMBOL(drm_dp_mst_atomic_wait_for_dependencies);
4673
4674/**
4675 * drm_dp_mst_root_conn_atomic_check() - Serialize CRTC commits on MST-capable connectors operating
4676 * in SST mode
4677 * @new_conn_state: The new connector state of the &drm_connector
4678 * @mgr: The MST topology manager for the &drm_connector
4679 *
4680 * Since MST uses fake &drm_encoder structs, the generic atomic modesetting code isn't able to
4681 * serialize non-blocking commits happening on the real DP connector of an MST topology switching
4682 * into/away from MST mode - as the CRTC on the real DP connector and the CRTCs on the connector's
4683 * MST topology will never share the same &drm_encoder.
4684 *
4685 * This function takes care of this serialization issue, by checking a root MST connector's atomic
4686 * state to determine if it is about to have a modeset - and then pulling in the MST topology state
4687 * if so, along with adding any relevant CRTCs to &drm_dp_mst_topology_state.pending_crtc_mask.
4688 *
4689 * Drivers implementing MST must call this function from the
4690 * &drm_connector_helper_funcs.atomic_check hook of any physical DP &drm_connector capable of
4691 * driving MST sinks.
4692 *
4693 * Returns:
4694 * 0 on success, negative error code otherwise
4695 */
4696int drm_dp_mst_root_conn_atomic_check(struct drm_connector_state *new_conn_state,
4697 struct drm_dp_mst_topology_mgr *mgr)
4698{
4699 struct drm_atomic_state *state = new_conn_state->state;
4700 struct drm_connector_state *old_conn_state =
4701 drm_atomic_get_old_connector_state(state, connector: new_conn_state->connector);
4702 struct drm_crtc_state *crtc_state;
4703 struct drm_dp_mst_topology_state *mst_state = NULL;
4704
4705 if (new_conn_state->crtc) {
4706 crtc_state = drm_atomic_get_new_crtc_state(state, crtc: new_conn_state->crtc);
4707 if (crtc_state && drm_atomic_crtc_needs_modeset(state: crtc_state)) {
4708 mst_state = drm_atomic_get_mst_topology_state(state, mgr);
4709 if (IS_ERR(ptr: mst_state))
4710 return PTR_ERR(ptr: mst_state);
4711
4712 mst_state->pending_crtc_mask |= drm_crtc_mask(crtc: new_conn_state->crtc);
4713 }
4714 }
4715
4716 if (old_conn_state->crtc) {
4717 crtc_state = drm_atomic_get_new_crtc_state(state, crtc: old_conn_state->crtc);
4718 if (crtc_state && drm_atomic_crtc_needs_modeset(state: crtc_state)) {
4719 if (!mst_state) {
4720 mst_state = drm_atomic_get_mst_topology_state(state, mgr);
4721 if (IS_ERR(ptr: mst_state))
4722 return PTR_ERR(ptr: mst_state);
4723 }
4724
4725 mst_state->pending_crtc_mask |= drm_crtc_mask(crtc: old_conn_state->crtc);
4726 }
4727 }
4728
4729 return 0;
4730}
4731EXPORT_SYMBOL(drm_dp_mst_root_conn_atomic_check);
4732
4733/**
4734 * drm_dp_mst_update_slots() - updates the slot info depending on the DP ecoding format
4735 * @mst_state: mst_state to update
4736 * @link_encoding_cap: the ecoding format on the link
4737 */
4738void drm_dp_mst_update_slots(struct drm_dp_mst_topology_state *mst_state, uint8_t link_encoding_cap)
4739{
4740 if (link_encoding_cap == DP_CAP_ANSI_128B132B) {
4741 mst_state->total_avail_slots = 64;
4742 mst_state->start_slot = 0;
4743 } else {
4744 mst_state->total_avail_slots = 63;
4745 mst_state->start_slot = 1;
4746 }
4747
4748 DRM_DEBUG_KMS("%s encoding format on mst_state 0x%p\n",
4749 (link_encoding_cap == DP_CAP_ANSI_128B132B) ? "128b/132b":"8b/10b",
4750 mst_state);
4751}
4752EXPORT_SYMBOL(drm_dp_mst_update_slots);
4753
4754/**
4755 * drm_dp_check_act_status() - Polls for ACT handled status.
4756 * @mgr: manager to use
4757 *
4758 * Tries waiting for the MST hub to finish updating it's payload table by
4759 * polling for the ACT handled bit for up to 3 seconds (yes-some hubs really
4760 * take that long).
4761 *
4762 * Returns:
4763 * 0 if the ACT was handled in time, negative error code on failure.
4764 */
4765int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
4766{
4767 /*
4768 * There doesn't seem to be any recommended retry count or timeout in
4769 * the MST specification. Since some hubs have been observed to take
4770 * over 1 second to update their payload allocations under certain
4771 * conditions, we use a rather large timeout value of 3 seconds.
4772 */
4773 return drm_dp_dpcd_poll_act_handled(aux: mgr->aux, timeout_ms: 3000);
4774}
4775EXPORT_SYMBOL(drm_dp_check_act_status);
4776
4777/**
4778 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
4779 * @clock: dot clock
4780 * @bpp: bpp as .4 binary fixed point
4781 *
4782 * This uses the formula in the spec to calculate the PBN value for a mode.
4783 */
4784int drm_dp_calc_pbn_mode(int clock, int bpp)
4785{
4786 /*
4787 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
4788 * common multiplier to render an integer PBN for all link rate/lane
4789 * counts combinations
4790 * calculate
4791 * peak_kbps = clock * bpp / 16
4792 * peak_kbps *= SSC overhead / 1000000
4793 * peak_kbps /= 8 convert to Kbytes
4794 * peak_kBps *= (64/54) / 1000 convert to PBN
4795 */
4796 /*
4797 * TODO: Use the actual link and mode parameters to calculate
4798 * the overhead. For now it's assumed that these are
4799 * 4 link lanes, 4096 hactive pixels, which don't add any
4800 * significant data padding overhead and that there is no DSC
4801 * or FEC overhead.
4802 */
4803 int overhead = drm_dp_bw_overhead(lane_count: 4, hactive: 4096, dsc_slice_count: 0, bpp_x16: bpp,
4804 DRM_DP_BW_OVERHEAD_MST |
4805 DRM_DP_BW_OVERHEAD_SSC_REF_CLK);
4806
4807 return DIV64_U64_ROUND_UP(mul_u32_u32(clock * bpp, 64 * overhead >> 4),
4808 1000000ULL * 8 * 54 * 1000);
4809}
4810EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
4811
4812/* we want to kick the TX after we've ack the up/down IRQs. */
4813static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
4814{
4815 queue_work(wq: system_long_wq, work: &mgr->tx_work);
4816}
4817
4818/*
4819 * Helper function for parsing DP device types into convenient strings
4820 * for use with dp_mst_topology
4821 */
4822static const char *pdt_to_string(u8 pdt)
4823{
4824 switch (pdt) {
4825 case DP_PEER_DEVICE_NONE:
4826 return "NONE";
4827 case DP_PEER_DEVICE_SOURCE_OR_SST:
4828 return "SOURCE OR SST";
4829 case DP_PEER_DEVICE_MST_BRANCHING:
4830 return "MST BRANCHING";
4831 case DP_PEER_DEVICE_SST_SINK:
4832 return "SST SINK";
4833 case DP_PEER_DEVICE_DP_LEGACY_CONV:
4834 return "DP LEGACY CONV";
4835 default:
4836 return "ERR";
4837 }
4838}
4839
4840static void drm_dp_mst_dump_mstb(struct seq_file *m,
4841 struct drm_dp_mst_branch *mstb)
4842{
4843 struct drm_dp_mst_port *port;
4844 int tabs = mstb->lct;
4845 char prefix[10];
4846 int i;
4847
4848 for (i = 0; i < tabs; i++)
4849 prefix[i] = '\t';
4850 prefix[i] = '\0';
4851
4852 seq_printf(m, fmt: "%smstb - [%p]: num_ports: %d\n", prefix, mstb, mstb->num_ports);
4853 list_for_each_entry(port, &mstb->ports, next) {
4854 seq_printf(m, fmt: "%sport %d - [%p] (%s - %s): ddps: %d, ldps: %d, sdp: %d/%d, fec: %s, conn: %p\n",
4855 prefix,
4856 port->port_num,
4857 port,
4858 port->input ? "input" : "output",
4859 pdt_to_string(pdt: port->pdt),
4860 port->ddps,
4861 port->ldps,
4862 port->num_sdp_streams,
4863 port->num_sdp_stream_sinks,
4864 port->fec_capable ? "true" : "false",
4865 port->connector);
4866 if (port->mstb)
4867 drm_dp_mst_dump_mstb(m, mstb: port->mstb);
4868 }
4869}
4870
4871#define DP_PAYLOAD_TABLE_SIZE 64
4872
4873static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
4874 char *buf)
4875{
4876 int i;
4877
4878 for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
4879 if (drm_dp_dpcd_read_data(aux: mgr->aux,
4880 DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
4881 buffer: &buf[i], size: 16) < 0)
4882 return false;
4883 }
4884 return true;
4885}
4886
4887static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
4888 struct drm_dp_mst_port *port, char *name,
4889 int namelen)
4890{
4891 struct edid *mst_edid;
4892
4893 mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
4894 drm_edid_get_monitor_name(edid: mst_edid, name, buflen: namelen);
4895 kfree(objp: mst_edid);
4896}
4897
4898/**
4899 * drm_dp_mst_dump_topology(): dump topology to seq file.
4900 * @m: seq_file to dump output to
4901 * @mgr: manager to dump current topology for.
4902 *
4903 * helper to dump MST topology to a seq file for debugfs.
4904 */
4905void drm_dp_mst_dump_topology(struct seq_file *m,
4906 struct drm_dp_mst_topology_mgr *mgr)
4907{
4908 struct drm_dp_mst_topology_state *state;
4909 struct drm_dp_mst_atomic_payload *payload;
4910 int i, ret;
4911
4912 static const char *const status[] = {
4913 "None",
4914 "Local",
4915 "DFP",
4916 "Remote",
4917 };
4918
4919 mutex_lock(&mgr->lock);
4920 if (mgr->mst_primary)
4921 drm_dp_mst_dump_mstb(m, mstb: mgr->mst_primary);
4922
4923 /* dump VCPIs */
4924 mutex_unlock(lock: &mgr->lock);
4925
4926 ret = drm_modeset_lock_single_interruptible(lock: &mgr->base.lock);
4927 if (ret < 0)
4928 return;
4929
4930 state = to_drm_dp_mst_topology_state(state: mgr->base.state);
4931 seq_printf(m, fmt: "\n*** Atomic state info ***\n");
4932 seq_printf(m, fmt: "payload_mask: %x, max_payloads: %d, start_slot: %u, pbn_div: %d\n",
4933 state->payload_mask, mgr->max_payloads, state->start_slot,
4934 dfixed_trunc(state->pbn_div));
4935
4936 seq_printf(m, fmt: "\n| idx | port | vcpi | slots | pbn | dsc | status | sink name |\n");
4937 for (i = 0; i < mgr->max_payloads; i++) {
4938 list_for_each_entry(payload, &state->payloads, next) {
4939 char name[14];
4940
4941 if (payload->vcpi != i || payload->delete)
4942 continue;
4943
4944 fetch_monitor_name(mgr, port: payload->port, name, namelen: sizeof(name));
4945 seq_printf(m, fmt: " %5d %6d %6d %02d - %02d %5d %5s %8s %19s\n",
4946 i,
4947 payload->port->port_num,
4948 payload->vcpi,
4949 payload->vc_start_slot,
4950 payload->vc_start_slot + payload->time_slots - 1,
4951 payload->pbn,
4952 payload->dsc_enabled ? "Y" : "N",
4953 status[payload->payload_allocation_status],
4954 (*name != 0) ? name : "Unknown");
4955 }
4956 }
4957
4958 seq_printf(m, fmt: "\n*** DPCD Info ***\n");
4959 mutex_lock(&mgr->lock);
4960 if (mgr->mst_primary) {
4961 u8 buf[DP_PAYLOAD_TABLE_SIZE];
4962 int ret;
4963
4964 if (drm_dp_read_dpcd_caps(aux: mgr->aux, dpcd: buf) < 0) {
4965 seq_printf(m, fmt: "dpcd read failed\n");
4966 goto out;
4967 }
4968 seq_printf(m, fmt: "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
4969
4970 ret = drm_dp_dpcd_read_data(aux: mgr->aux, DP_FAUX_CAP, buffer: buf, size: 2);
4971 if (ret < 0) {
4972 seq_printf(m, fmt: "faux/mst read failed\n");
4973 goto out;
4974 }
4975 seq_printf(m, fmt: "faux/mst: %*ph\n", 2, buf);
4976
4977 ret = drm_dp_dpcd_read_data(aux: mgr->aux, DP_MSTM_CTRL, buffer: buf, size: 1);
4978 if (ret < 0) {
4979 seq_printf(m, fmt: "mst ctrl read failed\n");
4980 goto out;
4981 }
4982 seq_printf(m, fmt: "mst ctrl: %*ph\n", 1, buf);
4983
4984 /* dump the standard OUI branch header */
4985 ret = drm_dp_dpcd_read_data(aux: mgr->aux, DP_BRANCH_OUI, buffer: buf,
4986 DP_BRANCH_OUI_HEADER_SIZE);
4987 if (ret < 0) {
4988 seq_printf(m, fmt: "branch oui read failed\n");
4989 goto out;
4990 }
4991 seq_printf(m, fmt: "branch oui: %*phN devid: ", 3, buf);
4992
4993 for (i = 0x3; i < 0x8 && buf[i]; i++)
4994 seq_putc(m, c: buf[i]);
4995 seq_printf(m, fmt: " revision: hw: %x.%x sw: %x.%x\n",
4996 buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
4997 if (dump_dp_payload_table(mgr, buf))
4998 seq_printf(m, fmt: "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
4999 }
5000
5001out:
5002 mutex_unlock(lock: &mgr->lock);
5003 drm_modeset_unlock(lock: &mgr->base.lock);
5004}
5005EXPORT_SYMBOL(drm_dp_mst_dump_topology);
5006
5007static void drm_dp_tx_work(struct work_struct *work)
5008{
5009 struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
5010
5011 mutex_lock(&mgr->qlock);
5012 if (!list_empty(head: &mgr->tx_msg_downq))
5013 process_single_down_tx_qlock(mgr);
5014 mutex_unlock(lock: &mgr->qlock);
5015}
5016
5017static inline void
5018drm_dp_delayed_destroy_port(struct drm_dp_mst_port *port)
5019{
5020 drm_dp_port_set_pdt(port, DP_PEER_DEVICE_NONE, new_mcs: port->mcs);
5021
5022 if (port->connector) {
5023 drm_connector_unregister(connector: port->connector);
5024 drm_connector_put(connector: port->connector);
5025 }
5026
5027 drm_dp_mst_put_port_malloc(port);
5028}
5029
5030static inline void
5031drm_dp_delayed_destroy_mstb(struct drm_dp_mst_branch *mstb)
5032{
5033 struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
5034 struct drm_dp_mst_port *port, *port_tmp;
5035 struct drm_dp_sideband_msg_tx *txmsg, *txmsg_tmp;
5036 bool wake_tx = false;
5037
5038 mutex_lock(&mgr->lock);
5039 list_for_each_entry_safe(port, port_tmp, &mstb->ports, next) {
5040 list_del(entry: &port->next);
5041 drm_dp_mst_topology_put_port(port);
5042 }
5043 mutex_unlock(lock: &mgr->lock);
5044
5045 /* drop any tx slot msg */
5046 mutex_lock(&mstb->mgr->qlock);
5047 list_for_each_entry_safe(txmsg, txmsg_tmp, &mgr->tx_msg_downq, next) {
5048 if (txmsg->dst != mstb)
5049 continue;
5050
5051 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
5052 list_del(entry: &txmsg->next);
5053 wake_tx = true;
5054 }
5055 mutex_unlock(lock: &mstb->mgr->qlock);
5056
5057 if (wake_tx)
5058 wake_up_all(&mstb->mgr->tx_waitq);
5059
5060 drm_dp_mst_put_mstb_malloc(mstb);
5061}
5062
5063static void drm_dp_delayed_destroy_work(struct work_struct *work)
5064{
5065 struct drm_dp_mst_topology_mgr *mgr =
5066 container_of(work, struct drm_dp_mst_topology_mgr,
5067 delayed_destroy_work);
5068 bool send_hotplug = false, go_again;
5069
5070 /*
5071 * Not a regular list traverse as we have to drop the destroy
5072 * connector lock before destroying the mstb/port, to avoid AB->BA
5073 * ordering between this lock and the config mutex.
5074 */
5075 do {
5076 go_again = false;
5077
5078 for (;;) {
5079 struct drm_dp_mst_branch *mstb;
5080
5081 mutex_lock(&mgr->delayed_destroy_lock);
5082 mstb = list_first_entry_or_null(&mgr->destroy_branch_device_list,
5083 struct drm_dp_mst_branch,
5084 destroy_next);
5085 if (mstb)
5086 list_del(entry: &mstb->destroy_next);
5087 mutex_unlock(lock: &mgr->delayed_destroy_lock);
5088
5089 if (!mstb)
5090 break;
5091
5092 drm_dp_delayed_destroy_mstb(mstb);
5093 go_again = true;
5094 }
5095
5096 for (;;) {
5097 struct drm_dp_mst_port *port;
5098
5099 mutex_lock(&mgr->delayed_destroy_lock);
5100 port = list_first_entry_or_null(&mgr->destroy_port_list,
5101 struct drm_dp_mst_port,
5102 next);
5103 if (port)
5104 list_del(entry: &port->next);
5105 mutex_unlock(lock: &mgr->delayed_destroy_lock);
5106
5107 if (!port)
5108 break;
5109
5110 drm_dp_delayed_destroy_port(port);
5111 send_hotplug = true;
5112 go_again = true;
5113 }
5114 } while (go_again);
5115
5116 if (send_hotplug)
5117 drm_kms_helper_hotplug_event(dev: mgr->dev);
5118}
5119
5120static struct drm_private_state *
5121drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
5122{
5123 struct drm_dp_mst_topology_state *state, *old_state =
5124 to_dp_mst_topology_state(obj->state);
5125 struct drm_dp_mst_atomic_payload *pos, *payload;
5126
5127 state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
5128 if (!state)
5129 return NULL;
5130
5131 __drm_atomic_helper_private_obj_duplicate_state(obj, state: &state->base);
5132
5133 INIT_LIST_HEAD(list: &state->payloads);
5134 state->commit_deps = NULL;
5135 state->num_commit_deps = 0;
5136 state->pending_crtc_mask = 0;
5137
5138 list_for_each_entry(pos, &old_state->payloads, next) {
5139 /* Prune leftover freed timeslot allocations */
5140 if (pos->delete)
5141 continue;
5142
5143 payload = kmemdup(pos, sizeof(*payload), GFP_KERNEL);
5144 if (!payload)
5145 goto fail;
5146
5147 drm_dp_mst_get_port_malloc(payload->port);
5148 list_add(new: &payload->next, head: &state->payloads);
5149 }
5150
5151 return &state->base;
5152
5153fail:
5154 list_for_each_entry_safe(pos, payload, &state->payloads, next) {
5155 drm_dp_mst_put_port_malloc(pos->port);
5156 kfree(objp: pos);
5157 }
5158 kfree(objp: state);
5159
5160 return NULL;
5161}
5162
5163static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
5164 struct drm_private_state *state)
5165{
5166 struct drm_dp_mst_topology_state *mst_state =
5167 to_dp_mst_topology_state(state);
5168 struct drm_dp_mst_atomic_payload *pos, *tmp;
5169 int i;
5170
5171 list_for_each_entry_safe(pos, tmp, &mst_state->payloads, next) {
5172 /* We only keep references to ports with active payloads */
5173 if (!pos->delete)
5174 drm_dp_mst_put_port_malloc(pos->port);
5175 kfree(objp: pos);
5176 }
5177
5178 for (i = 0; i < mst_state->num_commit_deps; i++)
5179 drm_crtc_commit_put(commit: mst_state->commit_deps[i]);
5180
5181 kfree(objp: mst_state->commit_deps);
5182 kfree(objp: mst_state);
5183}
5184
5185static bool drm_dp_mst_port_downstream_of_branch(struct drm_dp_mst_port *port,
5186 struct drm_dp_mst_branch *branch)
5187{
5188 while (port->parent) {
5189 if (port->parent == branch)
5190 return true;
5191
5192 if (port->parent->port_parent)
5193 port = port->parent->port_parent;
5194 else
5195 break;
5196 }
5197 return false;
5198}
5199
5200static bool
5201drm_dp_mst_port_downstream_of_parent_locked(struct drm_dp_mst_topology_mgr *mgr,
5202 struct drm_dp_mst_port *port,
5203 struct drm_dp_mst_port *parent)
5204{
5205 if (!mgr->mst_primary)
5206 return false;
5207
5208 port = drm_dp_mst_topology_get_port_validated_locked(mstb: mgr->mst_primary,
5209 to_find: port);
5210 if (!port)
5211 return false;
5212
5213 if (!parent)
5214 return true;
5215
5216 parent = drm_dp_mst_topology_get_port_validated_locked(mstb: mgr->mst_primary,
5217 to_find: parent);
5218 if (!parent)
5219 return false;
5220
5221 if (!parent->mstb)
5222 return false;
5223
5224 return drm_dp_mst_port_downstream_of_branch(port, branch: parent->mstb);
5225}
5226
5227/**
5228 * drm_dp_mst_port_downstream_of_parent - check if a port is downstream of a parent port
5229 * @mgr: MST topology manager
5230 * @port: the port being looked up
5231 * @parent: the parent port
5232 *
5233 * The function returns %true if @port is downstream of @parent. If @parent is
5234 * %NULL - denoting the root port - the function returns %true if @port is in
5235 * @mgr's topology.
5236 */
5237bool
5238drm_dp_mst_port_downstream_of_parent(struct drm_dp_mst_topology_mgr *mgr,
5239 struct drm_dp_mst_port *port,
5240 struct drm_dp_mst_port *parent)
5241{
5242 bool ret;
5243
5244 mutex_lock(&mgr->lock);
5245 ret = drm_dp_mst_port_downstream_of_parent_locked(mgr, port, parent);
5246 mutex_unlock(lock: &mgr->lock);
5247
5248 return ret;
5249}
5250EXPORT_SYMBOL(drm_dp_mst_port_downstream_of_parent);
5251
5252static int
5253drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
5254 struct drm_dp_mst_topology_state *state,
5255 struct drm_dp_mst_port **failing_port);
5256
5257static int
5258drm_dp_mst_atomic_check_mstb_bw_limit(struct drm_dp_mst_branch *mstb,
5259 struct drm_dp_mst_topology_state *state,
5260 struct drm_dp_mst_port **failing_port)
5261{
5262 struct drm_dp_mst_atomic_payload *payload;
5263 struct drm_dp_mst_port *port;
5264 int pbn_used = 0, ret;
5265 bool found = false;
5266
5267 /* Check that we have at least one port in our state that's downstream
5268 * of this branch, otherwise we can skip this branch
5269 */
5270 list_for_each_entry(payload, &state->payloads, next) {
5271 if (!payload->pbn ||
5272 !drm_dp_mst_port_downstream_of_branch(port: payload->port, branch: mstb))
5273 continue;
5274
5275 found = true;
5276 break;
5277 }
5278 if (!found)
5279 return 0;
5280
5281 if (mstb->port_parent)
5282 drm_dbg_atomic(mstb->mgr->dev,
5283 "[MSTB:%p] [MST PORT:%p] Checking bandwidth limits on [MSTB:%p]\n",
5284 mstb->port_parent->parent, mstb->port_parent, mstb);
5285 else
5286 drm_dbg_atomic(mstb->mgr->dev, "[MSTB:%p] Checking bandwidth limits\n", mstb);
5287
5288 list_for_each_entry(port, &mstb->ports, next) {
5289 ret = drm_dp_mst_atomic_check_port_bw_limit(port, state, failing_port);
5290 if (ret < 0)
5291 return ret;
5292
5293 pbn_used += ret;
5294 }
5295
5296 return pbn_used;
5297}
5298
5299static int
5300drm_dp_mst_atomic_check_port_bw_limit(struct drm_dp_mst_port *port,
5301 struct drm_dp_mst_topology_state *state,
5302 struct drm_dp_mst_port **failing_port)
5303{
5304 struct drm_dp_mst_atomic_payload *payload;
5305 int pbn_used = 0;
5306
5307 if (port->pdt == DP_PEER_DEVICE_NONE)
5308 return 0;
5309
5310 if (drm_dp_mst_is_end_device(pdt: port->pdt, mcs: port->mcs)) {
5311 payload = drm_atomic_get_mst_payload_state(state, port);
5312 if (!payload)
5313 return 0;
5314
5315 /*
5316 * This could happen if the sink deasserted its HPD line, but
5317 * the branch device still reports it as attached (PDT != NONE).
5318 */
5319 if (!port->full_pbn) {
5320 drm_dbg_atomic(port->mgr->dev,
5321 "[MSTB:%p] [MST PORT:%p] no BW available for the port\n",
5322 port->parent, port);
5323 *failing_port = port;
5324 return -EINVAL;
5325 }
5326
5327 pbn_used = payload->pbn;
5328 } else {
5329 pbn_used = drm_dp_mst_atomic_check_mstb_bw_limit(mstb: port->mstb,
5330 state,
5331 failing_port);
5332 if (pbn_used <= 0)
5333 return pbn_used;
5334 }
5335
5336 if (pbn_used > port->full_pbn) {
5337 drm_dbg_atomic(port->mgr->dev,
5338 "[MSTB:%p] [MST PORT:%p] required PBN of %d exceeds port limit of %d\n",
5339 port->parent, port, pbn_used, port->full_pbn);
5340 *failing_port = port;
5341 return -ENOSPC;
5342 }
5343
5344 drm_dbg_atomic(port->mgr->dev, "[MSTB:%p] [MST PORT:%p] uses %d out of %d PBN\n",
5345 port->parent, port, pbn_used, port->full_pbn);
5346
5347 return pbn_used;
5348}
5349
5350static inline int
5351drm_dp_mst_atomic_check_payload_alloc_limits(struct drm_dp_mst_topology_mgr *mgr,
5352 struct drm_dp_mst_topology_state *mst_state)
5353{
5354 struct drm_dp_mst_atomic_payload *payload;
5355 int avail_slots = mst_state->total_avail_slots, payload_count = 0;
5356
5357 list_for_each_entry(payload, &mst_state->payloads, next) {
5358 /* Releasing payloads is always OK-even if the port is gone */
5359 if (payload->delete) {
5360 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] releases all time slots\n",
5361 payload->port);
5362 continue;
5363 }
5364
5365 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] requires %d time slots\n",
5366 payload->port, payload->time_slots);
5367
5368 avail_slots -= payload->time_slots;
5369 if (avail_slots < 0) {
5370 drm_dbg_atomic(mgr->dev,
5371 "[MST PORT:%p] not enough time slots in mst state %p (avail=%d)\n",
5372 payload->port, mst_state, avail_slots + payload->time_slots);
5373 return -ENOSPC;
5374 }
5375
5376 if (++payload_count > mgr->max_payloads) {
5377 drm_dbg_atomic(mgr->dev,
5378 "[MST MGR:%p] state %p has too many payloads (max=%d)\n",
5379 mgr, mst_state, mgr->max_payloads);
5380 return -EINVAL;
5381 }
5382
5383 /* Assign a VCPI */
5384 if (!payload->vcpi) {
5385 payload->vcpi = ffz(mst_state->payload_mask) + 1;
5386 drm_dbg_atomic(mgr->dev, "[MST PORT:%p] assigned VCPI #%d\n",
5387 payload->port, payload->vcpi);
5388 mst_state->payload_mask |= BIT(payload->vcpi - 1);
5389 }
5390 }
5391
5392 if (!payload_count)
5393 mst_state->pbn_div.full = dfixed_const(0);
5394
5395 drm_dbg_atomic(mgr->dev, "[MST MGR:%p] mst state %p TU pbn_div=%d avail=%d used=%d\n",
5396 mgr, mst_state, dfixed_trunc(mst_state->pbn_div), avail_slots,
5397 mst_state->total_avail_slots - avail_slots);
5398
5399 return 0;
5400}
5401
5402/**
5403 * drm_dp_mst_add_affected_dsc_crtcs
5404 * @state: Pointer to the new struct drm_dp_mst_topology_state
5405 * @mgr: MST topology manager
5406 *
5407 * Whenever there is a change in mst topology
5408 * DSC configuration would have to be recalculated
5409 * therefore we need to trigger modeset on all affected
5410 * CRTCs in that topology
5411 *
5412 * See also:
5413 * drm_dp_mst_atomic_enable_dsc()
5414 */
5415int drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_state *state, struct drm_dp_mst_topology_mgr *mgr)
5416{
5417 struct drm_dp_mst_topology_state *mst_state;
5418 struct drm_dp_mst_atomic_payload *pos;
5419 struct drm_connector *connector;
5420 struct drm_connector_state *conn_state;
5421 struct drm_crtc *crtc;
5422 struct drm_crtc_state *crtc_state;
5423
5424 mst_state = drm_atomic_get_mst_topology_state(state, mgr);
5425
5426 if (IS_ERR(ptr: mst_state))
5427 return PTR_ERR(ptr: mst_state);
5428
5429 list_for_each_entry(pos, &mst_state->payloads, next) {
5430
5431 connector = pos->port->connector;
5432
5433 if (!connector)
5434 return -EINVAL;
5435
5436 conn_state = drm_atomic_get_connector_state(state, connector);
5437
5438 if (IS_ERR(ptr: conn_state))
5439 return PTR_ERR(ptr: conn_state);
5440
5441 crtc = conn_state->crtc;
5442
5443 if (!crtc)
5444 continue;
5445
5446 if (!drm_dp_mst_dsc_aux_for_port(port: pos->port))
5447 continue;
5448
5449 crtc_state = drm_atomic_get_crtc_state(state: mst_state->base.state, crtc);
5450
5451 if (IS_ERR(ptr: crtc_state))
5452 return PTR_ERR(ptr: crtc_state);
5453
5454 drm_dbg_atomic(mgr->dev, "[MST MGR:%p] Setting mode_changed flag on CRTC %p\n",
5455 mgr, crtc);
5456
5457 crtc_state->mode_changed = true;
5458 }
5459 return 0;
5460}
5461EXPORT_SYMBOL(drm_dp_mst_add_affected_dsc_crtcs);
5462
5463/**
5464 * drm_dp_mst_atomic_enable_dsc - Set DSC Enable Flag to On/Off
5465 * @state: Pointer to the new drm_atomic_state
5466 * @port: Pointer to the affected MST Port
5467 * @pbn: Newly recalculated bw required for link with DSC enabled
5468 * @enable: Boolean flag to enable or disable DSC on the port
5469 *
5470 * This function enables DSC on the given Port
5471 * by recalculating its vcpi from pbn provided
5472 * and sets dsc_enable flag to keep track of which
5473 * ports have DSC enabled
5474 *
5475 */
5476int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_state *state,
5477 struct drm_dp_mst_port *port,
5478 int pbn, bool enable)
5479{
5480 struct drm_dp_mst_topology_state *mst_state;
5481 struct drm_dp_mst_atomic_payload *payload;
5482 int time_slots = 0;
5483
5484 mst_state = drm_atomic_get_mst_topology_state(state, mgr: port->mgr);
5485 if (IS_ERR(ptr: mst_state))
5486 return PTR_ERR(ptr: mst_state);
5487
5488 payload = drm_atomic_get_mst_payload_state(mst_state, port);
5489 if (!payload) {
5490 drm_dbg_atomic(state->dev,
5491 "[MST PORT:%p] Couldn't find payload in mst state %p\n",
5492 port, mst_state);
5493 return -EINVAL;
5494 }
5495
5496 if (payload->dsc_enabled == enable) {
5497 drm_dbg_atomic(state->dev,
5498 "[MST PORT:%p] DSC flag is already set to %d, returning %d time slots\n",
5499 port, enable, payload->time_slots);
5500 time_slots = payload->time_slots;
5501 }
5502
5503 if (enable) {
5504 time_slots = drm_dp_atomic_find_time_slots(state, port->mgr, port, pbn);
5505 drm_dbg_atomic(state->dev,
5506 "[MST PORT:%p] Enabling DSC flag, reallocating %d time slots on the port\n",
5507 port, time_slots);
5508 if (time_slots < 0)
5509 return -EINVAL;
5510 }
5511
5512 payload->dsc_enabled = enable;
5513
5514 return time_slots;
5515}
5516EXPORT_SYMBOL(drm_dp_mst_atomic_enable_dsc);
5517
5518/**
5519 * drm_dp_mst_atomic_check_mgr - Check the atomic state of an MST topology manager
5520 * @state: The global atomic state
5521 * @mgr: Manager to check
5522 * @mst_state: The MST atomic state for @mgr
5523 * @failing_port: Returns the port with a BW limitation
5524 *
5525 * Checks the given MST manager's topology state for an atomic update to ensure
5526 * that it's valid. This includes checking whether there's enough bandwidth to
5527 * support the new timeslot allocations in the atomic update.
5528 *
5529 * Any atomic drivers supporting DP MST must make sure to call this or
5530 * the drm_dp_mst_atomic_check() function after checking the rest of their state
5531 * in their &drm_mode_config_funcs.atomic_check() callback.
5532 *
5533 * See also:
5534 * drm_dp_mst_atomic_check()
5535 * drm_dp_atomic_find_time_slots()
5536 * drm_dp_atomic_release_time_slots()
5537 *
5538 * Returns:
5539 * - 0 if the new state is valid
5540 * - %-ENOSPC, if the new state is invalid, because of BW limitation
5541 * @failing_port is set to:
5542 *
5543 * - The non-root port where a BW limit check failed
5544 * with all the ports downstream of @failing_port passing
5545 * the BW limit check.
5546 * The returned port pointer is valid until at least
5547 * one payload downstream of it exists.
5548 * - %NULL if the BW limit check failed at the root port
5549 * with all the ports downstream of the root port passing
5550 * the BW limit check.
5551 *
5552 * - %-EINVAL, if the new state is invalid, because the root port has
5553 * too many payloads.
5554 */
5555int drm_dp_mst_atomic_check_mgr(struct drm_atomic_state *state,
5556 struct drm_dp_mst_topology_mgr *mgr,
5557 struct drm_dp_mst_topology_state *mst_state,
5558 struct drm_dp_mst_port **failing_port)
5559{
5560 int ret;
5561
5562 *failing_port = NULL;
5563
5564 if (!mgr->mst_state)
5565 return 0;
5566
5567 mutex_lock(&mgr->lock);
5568 ret = drm_dp_mst_atomic_check_mstb_bw_limit(mstb: mgr->mst_primary,
5569 state: mst_state,
5570 failing_port);
5571 mutex_unlock(lock: &mgr->lock);
5572
5573 if (ret < 0)
5574 return ret;
5575
5576 return drm_dp_mst_atomic_check_payload_alloc_limits(mgr, mst_state);
5577}
5578EXPORT_SYMBOL(drm_dp_mst_atomic_check_mgr);
5579
5580/**
5581 * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
5582 * atomic update is valid
5583 * @state: Pointer to the new &struct drm_dp_mst_topology_state
5584 *
5585 * Checks the given topology state for an atomic update to ensure that it's
5586 * valid, calling drm_dp_mst_atomic_check_mgr() for all MST manager in the
5587 * atomic state. This includes checking whether there's enough bandwidth to
5588 * support the new timeslot allocations in the atomic update.
5589 *
5590 * Any atomic drivers supporting DP MST must make sure to call this after
5591 * checking the rest of their state in their
5592 * &drm_mode_config_funcs.atomic_check() callback.
5593 *
5594 * See also:
5595 * drm_dp_mst_atomic_check_mgr()
5596 * drm_dp_atomic_find_time_slots()
5597 * drm_dp_atomic_release_time_slots()
5598 *
5599 * Returns:
5600 * 0 if the new state is valid, negative error code otherwise.
5601 */
5602int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
5603{
5604 struct drm_dp_mst_topology_mgr *mgr;
5605 struct drm_dp_mst_topology_state *mst_state;
5606 int i, ret = 0;
5607
5608 for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
5609 struct drm_dp_mst_port *tmp_port;
5610
5611 ret = drm_dp_mst_atomic_check_mgr(state, mgr, mst_state, &tmp_port);
5612 if (ret)
5613 break;
5614 }
5615
5616 return ret;
5617}
5618EXPORT_SYMBOL(drm_dp_mst_atomic_check);
5619
5620const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
5621 .atomic_duplicate_state = drm_dp_mst_duplicate_state,
5622 .atomic_destroy_state = drm_dp_mst_destroy_state,
5623};
5624EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
5625
5626/**
5627 * drm_atomic_get_mst_topology_state: get MST topology state
5628 * @state: global atomic state
5629 * @mgr: MST topology manager, also the private object in this case
5630 *
5631 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
5632 * state vtable so that the private object state returned is that of a MST
5633 * topology object.
5634 *
5635 * RETURNS:
5636 * The MST topology state or error pointer.
5637 */
5638struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
5639 struct drm_dp_mst_topology_mgr *mgr)
5640{
5641 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
5642}
5643EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
5644
5645/**
5646 * drm_atomic_get_old_mst_topology_state: get old MST topology state in atomic state, if any
5647 * @state: global atomic state
5648 * @mgr: MST topology manager, also the private object in this case
5649 *
5650 * This function wraps drm_atomic_get_old_private_obj_state() passing in the MST atomic
5651 * state vtable so that the private object state returned is that of a MST
5652 * topology object.
5653 *
5654 * Returns:
5655 * The old MST topology state, or NULL if there's no topology state for this MST mgr
5656 * in the global atomic state
5657 */
5658struct drm_dp_mst_topology_state *
5659drm_atomic_get_old_mst_topology_state(struct drm_atomic_state *state,
5660 struct drm_dp_mst_topology_mgr *mgr)
5661{
5662 struct drm_private_state *old_priv_state =
5663 drm_atomic_get_old_private_obj_state(state, obj: &mgr->base);
5664
5665 return old_priv_state ? to_dp_mst_topology_state(old_priv_state) : NULL;
5666}
5667EXPORT_SYMBOL(drm_atomic_get_old_mst_topology_state);
5668
5669/**
5670 * drm_atomic_get_new_mst_topology_state: get new MST topology state in atomic state, if any
5671 * @state: global atomic state
5672 * @mgr: MST topology manager, also the private object in this case
5673 *
5674 * This function wraps drm_atomic_get_new_private_obj_state() passing in the MST atomic
5675 * state vtable so that the private object state returned is that of a MST
5676 * topology object.
5677 *
5678 * Returns:
5679 * The new MST topology state, or NULL if there's no topology state for this MST mgr
5680 * in the global atomic state
5681 */
5682struct drm_dp_mst_topology_state *
5683drm_atomic_get_new_mst_topology_state(struct drm_atomic_state *state,
5684 struct drm_dp_mst_topology_mgr *mgr)
5685{
5686 struct drm_private_state *new_priv_state =
5687 drm_atomic_get_new_private_obj_state(state, obj: &mgr->base);
5688
5689 return new_priv_state ? to_dp_mst_topology_state(new_priv_state) : NULL;
5690}
5691EXPORT_SYMBOL(drm_atomic_get_new_mst_topology_state);
5692
5693/**
5694 * drm_dp_mst_topology_mgr_init - initialise a topology manager
5695 * @mgr: manager struct to initialise
5696 * @dev: device providing this structure - for i2c addition.
5697 * @aux: DP helper aux channel to talk to this device
5698 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
5699 * @max_payloads: maximum number of payloads this GPU can source
5700 * @conn_base_id: the connector object ID the MST device is connected to.
5701 *
5702 * Return 0 for success, or negative error code on failure
5703 */
5704int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
5705 struct drm_device *dev, struct drm_dp_aux *aux,
5706 int max_dpcd_transaction_bytes, int max_payloads,
5707 int conn_base_id)
5708{
5709 struct drm_dp_mst_topology_state *mst_state;
5710
5711 mutex_init(&mgr->lock);
5712 mutex_init(&mgr->qlock);
5713 mutex_init(&mgr->delayed_destroy_lock);
5714 mutex_init(&mgr->up_req_lock);
5715 mutex_init(&mgr->probe_lock);
5716#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
5717 mutex_init(&mgr->topology_ref_history_lock);
5718 stack_depot_init();
5719#endif
5720 INIT_LIST_HEAD(list: &mgr->tx_msg_downq);
5721 INIT_LIST_HEAD(list: &mgr->destroy_port_list);
5722 INIT_LIST_HEAD(list: &mgr->destroy_branch_device_list);
5723 INIT_LIST_HEAD(list: &mgr->up_req_list);
5724
5725 /*
5726 * delayed_destroy_work will be queued on a dedicated WQ, so that any
5727 * requeuing will be also flushed when deiniting the topology manager.
5728 */
5729 mgr->delayed_destroy_wq = alloc_ordered_workqueue("drm_dp_mst_wq", 0);
5730 if (mgr->delayed_destroy_wq == NULL)
5731 return -ENOMEM;
5732
5733 INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
5734 INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
5735 INIT_WORK(&mgr->delayed_destroy_work, drm_dp_delayed_destroy_work);
5736 INIT_WORK(&mgr->up_req_work, drm_dp_mst_up_req_work);
5737 init_waitqueue_head(&mgr->tx_waitq);
5738 mgr->dev = dev;
5739 mgr->aux = aux;
5740 mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
5741 mgr->max_payloads = max_payloads;
5742 mgr->conn_base_id = conn_base_id;
5743
5744 mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
5745 if (mst_state == NULL)
5746 return -ENOMEM;
5747
5748 mst_state->total_avail_slots = 63;
5749 mst_state->start_slot = 1;
5750
5751 mst_state->mgr = mgr;
5752 INIT_LIST_HEAD(list: &mst_state->payloads);
5753
5754 drm_atomic_private_obj_init(dev, obj: &mgr->base,
5755 state: &mst_state->base,
5756 funcs: &drm_dp_mst_topology_state_funcs);
5757
5758 return 0;
5759}
5760EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
5761
5762/**
5763 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
5764 * @mgr: manager to destroy
5765 */
5766void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
5767{
5768 drm_dp_mst_topology_mgr_set_mst(mgr, false);
5769 flush_work(work: &mgr->work);
5770 /* The following will also drain any requeued work on the WQ. */
5771 if (mgr->delayed_destroy_wq) {
5772 destroy_workqueue(wq: mgr->delayed_destroy_wq);
5773 mgr->delayed_destroy_wq = NULL;
5774 }
5775 mgr->dev = NULL;
5776 mgr->aux = NULL;
5777 drm_atomic_private_obj_fini(obj: &mgr->base);
5778 mgr->funcs = NULL;
5779
5780 mutex_destroy(lock: &mgr->delayed_destroy_lock);
5781 mutex_destroy(lock: &mgr->qlock);
5782 mutex_destroy(lock: &mgr->lock);
5783 mutex_destroy(lock: &mgr->up_req_lock);
5784 mutex_destroy(lock: &mgr->probe_lock);
5785#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
5786 mutex_destroy(lock: &mgr->topology_ref_history_lock);
5787#endif
5788}
5789EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
5790
5791static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
5792{
5793 int i;
5794
5795 if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
5796 return false;
5797
5798 for (i = 0; i < num - 1; i++) {
5799 if (msgs[i].flags & I2C_M_RD ||
5800 msgs[i].len > 0xff)
5801 return false;
5802 }
5803
5804 return msgs[num - 1].flags & I2C_M_RD &&
5805 msgs[num - 1].len <= 0xff;
5806}
5807
5808static bool remote_i2c_write_ok(const struct i2c_msg msgs[], int num)
5809{
5810 int i;
5811
5812 for (i = 0; i < num - 1; i++) {
5813 if (msgs[i].flags & I2C_M_RD || !(msgs[i].flags & I2C_M_STOP) ||
5814 msgs[i].len > 0xff)
5815 return false;
5816 }
5817
5818 return !(msgs[num - 1].flags & I2C_M_RD) && msgs[num - 1].len <= 0xff;
5819}
5820
5821static int drm_dp_mst_i2c_read(struct drm_dp_mst_branch *mstb,
5822 struct drm_dp_mst_port *port,
5823 struct i2c_msg *msgs, int num)
5824{
5825 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
5826 unsigned int i;
5827 struct drm_dp_sideband_msg_req_body msg;
5828 struct drm_dp_sideband_msg_tx *txmsg = NULL;
5829 int ret;
5830
5831 memset(&msg, 0, sizeof(msg));
5832 msg.req_type = DP_REMOTE_I2C_READ;
5833 msg.u.i2c_read.num_transactions = num - 1;
5834 msg.u.i2c_read.port_number = port->port_num;
5835 for (i = 0; i < num - 1; i++) {
5836 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
5837 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
5838 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
5839 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
5840 }
5841 msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
5842 msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
5843
5844 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
5845 if (!txmsg) {
5846 ret = -ENOMEM;
5847 goto out;
5848 }
5849
5850 txmsg->dst = mstb;
5851 drm_dp_encode_sideband_req(&msg, txmsg);
5852
5853 drm_dp_queue_down_tx(mgr, txmsg);
5854
5855 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
5856 if (ret > 0) {
5857
5858 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
5859 ret = -EREMOTEIO;
5860 goto out;
5861 }
5862 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
5863 ret = -EIO;
5864 goto out;
5865 }
5866 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
5867 ret = num;
5868 }
5869out:
5870 kfree(objp: txmsg);
5871 return ret;
5872}
5873
5874static int drm_dp_mst_i2c_write(struct drm_dp_mst_branch *mstb,
5875 struct drm_dp_mst_port *port,
5876 struct i2c_msg *msgs, int num)
5877{
5878 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
5879 unsigned int i;
5880 struct drm_dp_sideband_msg_req_body msg;
5881 struct drm_dp_sideband_msg_tx *txmsg = NULL;
5882 int ret;
5883
5884 txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
5885 if (!txmsg) {
5886 ret = -ENOMEM;
5887 goto out;
5888 }
5889 for (i = 0; i < num; i++) {
5890 memset(&msg, 0, sizeof(msg));
5891 msg.req_type = DP_REMOTE_I2C_WRITE;
5892 msg.u.i2c_write.port_number = port->port_num;
5893 msg.u.i2c_write.write_i2c_device_id = msgs[i].addr;
5894 msg.u.i2c_write.num_bytes = msgs[i].len;
5895 msg.u.i2c_write.bytes = msgs[i].buf;
5896
5897 memset(txmsg, 0, sizeof(*txmsg));
5898 txmsg->dst = mstb;
5899
5900 drm_dp_encode_sideband_req(&msg, txmsg);
5901 drm_dp_queue_down_tx(mgr, txmsg);
5902
5903 ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
5904 if (ret > 0) {
5905 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
5906 ret = -EREMOTEIO;
5907 goto out;
5908 }
5909 } else {
5910 goto out;
5911 }
5912 }
5913 ret = num;
5914out:
5915 kfree(objp: txmsg);
5916 return ret;
5917}
5918
5919/* I2C device */
5920static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter,
5921 struct i2c_msg *msgs, int num)
5922{
5923 struct drm_dp_aux *aux = adapter->algo_data;
5924 struct drm_dp_mst_port *port =
5925 container_of(aux, struct drm_dp_mst_port, aux);
5926 struct drm_dp_mst_branch *mstb;
5927 struct drm_dp_mst_topology_mgr *mgr = port->mgr;
5928 int ret;
5929
5930 mstb = drm_dp_mst_topology_get_mstb_validated(mgr, mstb: port->parent);
5931 if (!mstb)
5932 return -EREMOTEIO;
5933
5934 if (remote_i2c_read_ok(msgs, num)) {
5935 ret = drm_dp_mst_i2c_read(mstb, port, msgs, num);
5936 } else if (remote_i2c_write_ok(msgs, num)) {
5937 ret = drm_dp_mst_i2c_write(mstb, port, msgs, num);
5938 } else {
5939 drm_dbg_kms(mgr->dev, "Unsupported I2C transaction for MST device\n");
5940 ret = -EIO;
5941 }
5942
5943 drm_dp_mst_topology_put_mstb(mstb);
5944 return ret;
5945}
5946
5947static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
5948{
5949 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
5950 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
5951 I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
5952 I2C_FUNC_10BIT_ADDR;
5953}
5954
5955static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
5956 .functionality = drm_dp_mst_i2c_functionality,
5957 .master_xfer = drm_dp_mst_i2c_xfer,
5958};
5959
5960/**
5961 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
5962 * @port: The port to add the I2C bus on
5963 *
5964 * Returns 0 on success or a negative error code on failure.
5965 */
5966static int drm_dp_mst_register_i2c_bus(struct drm_dp_mst_port *port)
5967{
5968 struct drm_dp_aux *aux = &port->aux;
5969 struct device *parent_dev = port->mgr->dev->dev;
5970
5971 aux->ddc.algo = &drm_dp_mst_i2c_algo;
5972 aux->ddc.algo_data = aux;
5973 aux->ddc.retries = 3;
5974
5975 aux->ddc.owner = THIS_MODULE;
5976 /* FIXME: set the kdev of the port's connector as parent */
5977 aux->ddc.dev.parent = parent_dev;
5978 aux->ddc.dev.of_node = parent_dev->of_node;
5979
5980 strscpy(aux->ddc.name, aux->name ? aux->name : dev_name(parent_dev),
5981 sizeof(aux->ddc.name));
5982
5983 return i2c_add_adapter(adap: &aux->ddc);
5984}
5985
5986/**
5987 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
5988 * @port: The port to remove the I2C bus from
5989 */
5990static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_mst_port *port)
5991{
5992 i2c_del_adapter(adap: &port->aux.ddc);
5993}
5994
5995/**
5996 * drm_dp_mst_is_virtual_dpcd() - Is the given port a virtual DP Peer Device
5997 * @port: The port to check
5998 *
5999 * A single physical MST hub object can be represented in the topology
6000 * by multiple branches, with virtual ports between those branches.
6001 *
6002 * As of DP1.4, An MST hub with internal (virtual) ports must expose
6003 * certain DPCD registers over those ports. See sections 2.6.1.1.1
6004 * and 2.6.1.1.2 of Display Port specification v1.4 for details.
6005 *
6006 * May acquire mgr->lock
6007 *
6008 * Returns:
6009 * true if the port is a virtual DP peer device, false otherwise
6010 */
6011static bool drm_dp_mst_is_virtual_dpcd(struct drm_dp_mst_port *port)
6012{
6013 struct drm_dp_mst_port *downstream_port;
6014
6015 if (!port || port->dpcd_rev < DP_DPCD_REV_14)
6016 return false;
6017
6018 /* Virtual DP Sink (Internal Display Panel) */
6019 if (drm_dp_mst_port_is_logical(port))
6020 return true;
6021
6022 /* DP-to-HDMI Protocol Converter */
6023 if (port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV &&
6024 !port->mcs &&
6025 port->ldps)
6026 return true;
6027
6028 /* DP-to-DP */
6029 mutex_lock(&port->mgr->lock);
6030 if (port->pdt == DP_PEER_DEVICE_MST_BRANCHING &&
6031 port->mstb &&
6032 port->mstb->num_ports == 2) {
6033 list_for_each_entry(downstream_port, &port->mstb->ports, next) {
6034 if (downstream_port->pdt == DP_PEER_DEVICE_SST_SINK &&
6035 !downstream_port->input) {
6036 mutex_unlock(lock: &port->mgr->lock);
6037 return true;
6038 }
6039 }
6040 }
6041 mutex_unlock(lock: &port->mgr->lock);
6042
6043 return false;
6044}
6045
6046/**
6047 * drm_dp_mst_aux_for_parent() - Get the AUX device for an MST port's parent
6048 * @port: MST port whose parent's AUX device is returned
6049 *
6050 * Return the AUX device for @port's parent or NULL if port's parent is the
6051 * root port.
6052 */
6053struct drm_dp_aux *drm_dp_mst_aux_for_parent(struct drm_dp_mst_port *port)
6054{
6055 if (!port->parent || !port->parent->port_parent)
6056 return NULL;
6057
6058 return &port->parent->port_parent->aux;
6059}
6060EXPORT_SYMBOL(drm_dp_mst_aux_for_parent);
6061
6062/**
6063 * drm_dp_mst_dsc_aux_for_port() - Find the correct aux for DSC
6064 * @port: The port to check. A leaf of the MST tree with an attached display.
6065 *
6066 * Depending on the situation, DSC may be enabled via the endpoint aux,
6067 * the immediately upstream aux, or the connector's physical aux.
6068 *
6069 * This is both the correct aux to read DSC_CAPABILITY and the
6070 * correct aux to write DSC_ENABLED.
6071 *
6072 * This operation can be expensive (up to four aux reads), so
6073 * the caller should cache the return.
6074 *
6075 * Returns:
6076 * NULL if DSC cannot be enabled on this port, otherwise the aux device
6077 */
6078struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port)
6079{
6080 struct drm_dp_mst_port *immediate_upstream_port;
6081 struct drm_dp_aux *immediate_upstream_aux;
6082 struct drm_dp_mst_port *fec_port;
6083 struct drm_dp_desc desc = {};
6084 u8 upstream_dsc;
6085 u8 endpoint_fec;
6086 u8 endpoint_dsc;
6087
6088 if (!port)
6089 return NULL;
6090
6091 if (port->parent->port_parent)
6092 immediate_upstream_port = port->parent->port_parent;
6093 else
6094 immediate_upstream_port = NULL;
6095
6096 fec_port = immediate_upstream_port;
6097 while (fec_port) {
6098 /*
6099 * Each physical link (i.e. not a virtual port) between the
6100 * output and the primary device must support FEC
6101 */
6102 if (!drm_dp_mst_is_virtual_dpcd(port: fec_port) &&
6103 !fec_port->fec_capable)
6104 return NULL;
6105
6106 fec_port = fec_port->parent->port_parent;
6107 }
6108
6109 /* DP-to-DP peer device */
6110 if (drm_dp_mst_is_virtual_dpcd(port: immediate_upstream_port)) {
6111 if (drm_dp_dpcd_read_data(aux: &port->aux,
6112 DP_DSC_SUPPORT, buffer: &endpoint_dsc, size: 1) < 0)
6113 return NULL;
6114 if (drm_dp_dpcd_read_data(aux: &port->aux,
6115 DP_FEC_CAPABILITY, buffer: &endpoint_fec, size: 1) < 0)
6116 return NULL;
6117 if (drm_dp_dpcd_read_data(aux: &immediate_upstream_port->aux,
6118 DP_DSC_SUPPORT, buffer: &upstream_dsc, size: 1) < 0)
6119 return NULL;
6120
6121 /* Enpoint decompression with DP-to-DP peer device */
6122 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
6123 (endpoint_fec & DP_FEC_CAPABLE) &&
6124 (upstream_dsc & DP_DSC_PASSTHROUGH_IS_SUPPORTED)) {
6125 port->passthrough_aux = &immediate_upstream_port->aux;
6126 return &port->aux;
6127 }
6128
6129 /* Virtual DPCD decompression with DP-to-DP peer device */
6130 return &immediate_upstream_port->aux;
6131 }
6132
6133 /* Virtual DPCD decompression with DP-to-HDMI or Virtual DP Sink */
6134 if (drm_dp_mst_is_virtual_dpcd(port))
6135 return &port->aux;
6136
6137 /*
6138 * Synaptics quirk
6139 * Applies to ports for which:
6140 * - Physical aux has Synaptics OUI
6141 * - DPv1.4 or higher
6142 * - Port is on primary branch device
6143 * - Not a VGA adapter (DP_DWN_STRM_PORT_TYPE_ANALOG)
6144 */
6145 if (immediate_upstream_port)
6146 immediate_upstream_aux = &immediate_upstream_port->aux;
6147 else
6148 immediate_upstream_aux = port->mgr->aux;
6149
6150 if (drm_dp_read_desc(aux: immediate_upstream_aux, desc: &desc, is_branch: true))
6151 return NULL;
6152
6153 if (drm_dp_has_quirk(desc: &desc, quirk: DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD)) {
6154 u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];
6155
6156 if (drm_dp_dpcd_read_data(aux: immediate_upstream_aux,
6157 DP_DSC_SUPPORT, buffer: &upstream_dsc, size: 1) < 0)
6158 return NULL;
6159
6160 if (!(upstream_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED))
6161 return NULL;
6162
6163 if (drm_dp_read_dpcd_caps(aux: immediate_upstream_aux, dpcd: dpcd_ext) < 0)
6164 return NULL;
6165
6166 if (dpcd_ext[DP_DPCD_REV] >= DP_DPCD_REV_14 &&
6167 ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT) &&
6168 ((dpcd_ext[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK)
6169 != DP_DWN_STRM_PORT_TYPE_ANALOG)))
6170 return immediate_upstream_aux;
6171 }
6172
6173 /*
6174 * The check below verifies if the MST sink
6175 * connected to the GPU is capable of DSC -
6176 * therefore the endpoint needs to be
6177 * both DSC and FEC capable.
6178 */
6179 if (drm_dp_dpcd_read_data(aux: &port->aux,
6180 DP_DSC_SUPPORT, buffer: &endpoint_dsc, size: 1) < 0)
6181 return NULL;
6182 if (drm_dp_dpcd_read_data(aux: &port->aux,
6183 DP_FEC_CAPABILITY, buffer: &endpoint_fec, size: 1) < 0)
6184 return NULL;
6185 if ((endpoint_dsc & DP_DSC_DECOMPRESSION_IS_SUPPORTED) &&
6186 (endpoint_fec & DP_FEC_CAPABLE))
6187 return &port->aux;
6188
6189 return NULL;
6190}
6191EXPORT_SYMBOL(drm_dp_mst_dsc_aux_for_port);
6192

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/drivers/gpu/drm/display/drm_dp_mst_topology.c