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