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