drm/dp_mst: Increase ACT retry timeout to 3s
[platform/kernel/linux-rpi.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 #include <linux/iopoll.h>
31
32 #include <drm/drm_atomic.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_dp_mst_helper.h>
35 #include <drm/drm_drv.h>
36 #include <drm/drm_fixed.h>
37 #include <drm/drm_print.h>
38 #include <drm/drm_probe_helper.h>
39
40 #include "drm_crtc_helper_internal.h"
41
42 /**
43  * DOC: dp mst helper
44  *
45  * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
46  * protocol. The helpers contain a topology manager and bandwidth manager.
47  * The helpers encapsulate the sending and received of sideband msgs.
48  */
49 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
50                                   char *buf);
51 static int test_calc_pbn_mode(void);
52
53 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port);
54
55 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
56                                      int id,
57                                      struct drm_dp_payload *payload);
58
59 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
60                                  struct drm_dp_mst_port *port,
61                                  int offset, int size, u8 *bytes);
62 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
63                                   struct drm_dp_mst_port *port,
64                                   int offset, int size, u8 *bytes);
65
66 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
67                                      struct drm_dp_mst_branch *mstb);
68 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
69                                            struct drm_dp_mst_branch *mstb,
70                                            struct drm_dp_mst_port *port);
71 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
72                                  u8 *guid);
73
74 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
75 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
76 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
77
78 #define DP_STR(x) [DP_ ## x] = #x
79
80 static const char *drm_dp_mst_req_type_str(u8 req_type)
81 {
82         static const char * const req_type_str[] = {
83                 DP_STR(GET_MSG_TRANSACTION_VERSION),
84                 DP_STR(LINK_ADDRESS),
85                 DP_STR(CONNECTION_STATUS_NOTIFY),
86                 DP_STR(ENUM_PATH_RESOURCES),
87                 DP_STR(ALLOCATE_PAYLOAD),
88                 DP_STR(QUERY_PAYLOAD),
89                 DP_STR(RESOURCE_STATUS_NOTIFY),
90                 DP_STR(CLEAR_PAYLOAD_ID_TABLE),
91                 DP_STR(REMOTE_DPCD_READ),
92                 DP_STR(REMOTE_DPCD_WRITE),
93                 DP_STR(REMOTE_I2C_READ),
94                 DP_STR(REMOTE_I2C_WRITE),
95                 DP_STR(POWER_UP_PHY),
96                 DP_STR(POWER_DOWN_PHY),
97                 DP_STR(SINK_EVENT_NOTIFY),
98                 DP_STR(QUERY_STREAM_ENC_STATUS),
99         };
100
101         if (req_type >= ARRAY_SIZE(req_type_str) ||
102             !req_type_str[req_type])
103                 return "unknown";
104
105         return req_type_str[req_type];
106 }
107
108 #undef DP_STR
109 #define DP_STR(x) [DP_NAK_ ## x] = #x
110
111 static const char *drm_dp_mst_nak_reason_str(u8 nak_reason)
112 {
113         static const char * const nak_reason_str[] = {
114                 DP_STR(WRITE_FAILURE),
115                 DP_STR(INVALID_READ),
116                 DP_STR(CRC_FAILURE),
117                 DP_STR(BAD_PARAM),
118                 DP_STR(DEFER),
119                 DP_STR(LINK_FAILURE),
120                 DP_STR(NO_RESOURCES),
121                 DP_STR(DPCD_FAIL),
122                 DP_STR(I2C_NAK),
123                 DP_STR(ALLOCATE_FAIL),
124         };
125
126         if (nak_reason >= ARRAY_SIZE(nak_reason_str) ||
127             !nak_reason_str[nak_reason])
128                 return "unknown";
129
130         return nak_reason_str[nak_reason];
131 }
132
133 #undef DP_STR
134
135 /* sideband msg handling */
136 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
137 {
138         u8 bitmask = 0x80;
139         u8 bitshift = 7;
140         u8 array_index = 0;
141         int number_of_bits = num_nibbles * 4;
142         u8 remainder = 0;
143
144         while (number_of_bits != 0) {
145                 number_of_bits--;
146                 remainder <<= 1;
147                 remainder |= (data[array_index] & bitmask) >> bitshift;
148                 bitmask >>= 1;
149                 bitshift--;
150                 if (bitmask == 0) {
151                         bitmask = 0x80;
152                         bitshift = 7;
153                         array_index++;
154                 }
155                 if ((remainder & 0x10) == 0x10)
156                         remainder ^= 0x13;
157         }
158
159         number_of_bits = 4;
160         while (number_of_bits != 0) {
161                 number_of_bits--;
162                 remainder <<= 1;
163                 if ((remainder & 0x10) != 0)
164                         remainder ^= 0x13;
165         }
166
167         return remainder;
168 }
169
170 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
171 {
172         u8 bitmask = 0x80;
173         u8 bitshift = 7;
174         u8 array_index = 0;
175         int number_of_bits = number_of_bytes * 8;
176         u16 remainder = 0;
177
178         while (number_of_bits != 0) {
179                 number_of_bits--;
180                 remainder <<= 1;
181                 remainder |= (data[array_index] & bitmask) >> bitshift;
182                 bitmask >>= 1;
183                 bitshift--;
184                 if (bitmask == 0) {
185                         bitmask = 0x80;
186                         bitshift = 7;
187                         array_index++;
188                 }
189                 if ((remainder & 0x100) == 0x100)
190                         remainder ^= 0xd5;
191         }
192
193         number_of_bits = 8;
194         while (number_of_bits != 0) {
195                 number_of_bits--;
196                 remainder <<= 1;
197                 if ((remainder & 0x100) != 0)
198                         remainder ^= 0xd5;
199         }
200
201         return remainder & 0xff;
202 }
203 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
204 {
205         u8 size = 3;
206         size += (hdr->lct / 2);
207         return size;
208 }
209
210 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
211                                            u8 *buf, int *len)
212 {
213         int idx = 0;
214         int i;
215         u8 crc4;
216         buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
217         for (i = 0; i < (hdr->lct / 2); i++)
218                 buf[idx++] = hdr->rad[i];
219         buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
220                 (hdr->msg_len & 0x3f);
221         buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
222
223         crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
224         buf[idx - 1] |= (crc4 & 0xf);
225
226         *len = idx;
227 }
228
229 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
230                                            u8 *buf, int buflen, u8 *hdrlen)
231 {
232         u8 crc4;
233         u8 len;
234         int i;
235         u8 idx;
236         if (buf[0] == 0)
237                 return false;
238         len = 3;
239         len += ((buf[0] & 0xf0) >> 4) / 2;
240         if (len > buflen)
241                 return false;
242         crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
243
244         if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
245                 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
246                 return false;
247         }
248
249         hdr->lct = (buf[0] & 0xf0) >> 4;
250         hdr->lcr = (buf[0] & 0xf);
251         idx = 1;
252         for (i = 0; i < (hdr->lct / 2); i++)
253                 hdr->rad[i] = buf[idx++];
254         hdr->broadcast = (buf[idx] >> 7) & 0x1;
255         hdr->path_msg = (buf[idx] >> 6) & 0x1;
256         hdr->msg_len = buf[idx] & 0x3f;
257         idx++;
258         hdr->somt = (buf[idx] >> 7) & 0x1;
259         hdr->eomt = (buf[idx] >> 6) & 0x1;
260         hdr->seqno = (buf[idx] >> 4) & 0x1;
261         idx++;
262         *hdrlen = idx;
263         return true;
264 }
265
266 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
267                                        struct drm_dp_sideband_msg_tx *raw)
268 {
269         int idx = 0;
270         int i;
271         u8 *buf = raw->msg;
272         buf[idx++] = req->req_type & 0x7f;
273
274         switch (req->req_type) {
275         case DP_ENUM_PATH_RESOURCES:
276                 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
277                 idx++;
278                 break;
279         case DP_ALLOCATE_PAYLOAD:
280                 buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
281                         (req->u.allocate_payload.number_sdp_streams & 0xf);
282                 idx++;
283                 buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
284                 idx++;
285                 buf[idx] = (req->u.allocate_payload.pbn >> 8);
286                 idx++;
287                 buf[idx] = (req->u.allocate_payload.pbn & 0xff);
288                 idx++;
289                 for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
290                         buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
291                                 (req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
292                         idx++;
293                 }
294                 if (req->u.allocate_payload.number_sdp_streams & 1) {
295                         i = req->u.allocate_payload.number_sdp_streams - 1;
296                         buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
297                         idx++;
298                 }
299                 break;
300         case DP_QUERY_PAYLOAD:
301                 buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
302                 idx++;
303                 buf[idx] = (req->u.query_payload.vcpi & 0x7f);
304                 idx++;
305                 break;
306         case DP_REMOTE_DPCD_READ:
307                 buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
308                 buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
309                 idx++;
310                 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
311                 idx++;
312                 buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
313                 idx++;
314                 buf[idx] = (req->u.dpcd_read.num_bytes);
315                 idx++;
316                 break;
317
318         case DP_REMOTE_DPCD_WRITE:
319                 buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
320                 buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
321                 idx++;
322                 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
323                 idx++;
324                 buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
325                 idx++;
326                 buf[idx] = (req->u.dpcd_write.num_bytes);
327                 idx++;
328                 memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
329                 idx += req->u.dpcd_write.num_bytes;
330                 break;
331         case DP_REMOTE_I2C_READ:
332                 buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
333                 buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
334                 idx++;
335                 for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
336                         buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
337                         idx++;
338                         buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
339                         idx++;
340                         memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
341                         idx += req->u.i2c_read.transactions[i].num_bytes;
342
343                         buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 4;
344                         buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
345                         idx++;
346                 }
347                 buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
348                 idx++;
349                 buf[idx] = (req->u.i2c_read.num_bytes_read);
350                 idx++;
351                 break;
352
353         case DP_REMOTE_I2C_WRITE:
354                 buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
355                 idx++;
356                 buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
357                 idx++;
358                 buf[idx] = (req->u.i2c_write.num_bytes);
359                 idx++;
360                 memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
361                 idx += req->u.i2c_write.num_bytes;
362                 break;
363
364         case DP_POWER_DOWN_PHY:
365         case DP_POWER_UP_PHY:
366                 buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
367                 idx++;
368                 break;
369         }
370         raw->cur_len = idx;
371 }
372
373 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
374 {
375         u8 crc4;
376         crc4 = drm_dp_msg_data_crc4(msg, len);
377         msg[len] = crc4;
378 }
379
380 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
381                                          struct drm_dp_sideband_msg_tx *raw)
382 {
383         int idx = 0;
384         u8 *buf = raw->msg;
385
386         buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
387
388         raw->cur_len = idx;
389 }
390
391 /* this adds a chunk of msg to the builder to get the final msg */
392 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
393                                       u8 *replybuf, u8 replybuflen, bool hdr)
394 {
395         int ret;
396         u8 crc4;
397
398         if (hdr) {
399                 u8 hdrlen;
400                 struct drm_dp_sideband_msg_hdr recv_hdr;
401                 ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
402                 if (ret == false) {
403                         print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
404                         return false;
405                 }
406
407                 /*
408                  * ignore out-of-order messages or messages that are part of a
409                  * failed transaction
410                  */
411                 if (!recv_hdr.somt && !msg->have_somt)
412                         return false;
413
414                 /* get length contained in this portion */
415                 msg->curchunk_len = recv_hdr.msg_len;
416                 msg->curchunk_hdrlen = hdrlen;
417
418                 /* we have already gotten an somt - don't bother parsing */
419                 if (recv_hdr.somt && msg->have_somt)
420                         return false;
421
422                 if (recv_hdr.somt) {
423                         memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
424                         msg->have_somt = true;
425                 }
426                 if (recv_hdr.eomt)
427                         msg->have_eomt = true;
428
429                 /* copy the bytes for the remainder of this header chunk */
430                 msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
431                 memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
432         } else {
433                 memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
434                 msg->curchunk_idx += replybuflen;
435         }
436
437         if (msg->curchunk_idx >= msg->curchunk_len) {
438                 /* do CRC */
439                 crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
440                 /* copy chunk into bigger msg */
441                 memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
442                 msg->curlen += msg->curchunk_len - 1;
443         }
444         return true;
445 }
446
447 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
448                                                struct drm_dp_sideband_msg_reply_body *repmsg)
449 {
450         int idx = 1;
451         int i;
452         memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
453         idx += 16;
454         repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
455         idx++;
456         if (idx > raw->curlen)
457                 goto fail_len;
458         for (i = 0; i < repmsg->u.link_addr.nports; i++) {
459                 if (raw->msg[idx] & 0x80)
460                         repmsg->u.link_addr.ports[i].input_port = 1;
461
462                 repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
463                 repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
464
465                 idx++;
466                 if (idx > raw->curlen)
467                         goto fail_len;
468                 repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
469                 repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
470                 if (repmsg->u.link_addr.ports[i].input_port == 0)
471                         repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
472                 idx++;
473                 if (idx > raw->curlen)
474                         goto fail_len;
475                 if (repmsg->u.link_addr.ports[i].input_port == 0) {
476                         repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
477                         idx++;
478                         if (idx > raw->curlen)
479                                 goto fail_len;
480                         memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
481                         idx += 16;
482                         if (idx > raw->curlen)
483                                 goto fail_len;
484                         repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
485                         repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
486                         idx++;
487
488                 }
489                 if (idx > raw->curlen)
490                         goto fail_len;
491         }
492
493         return true;
494 fail_len:
495         DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
496         return false;
497 }
498
499 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
500                                                    struct drm_dp_sideband_msg_reply_body *repmsg)
501 {
502         int idx = 1;
503         repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
504         idx++;
505         if (idx > raw->curlen)
506                 goto fail_len;
507         repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
508         idx++;
509         if (idx > raw->curlen)
510                 goto fail_len;
511
512         memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
513         return true;
514 fail_len:
515         DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
516         return false;
517 }
518
519 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
520                                                       struct drm_dp_sideband_msg_reply_body *repmsg)
521 {
522         int idx = 1;
523         repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
524         idx++;
525         if (idx > raw->curlen)
526                 goto fail_len;
527         return true;
528 fail_len:
529         DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
530         return false;
531 }
532
533 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
534                                                       struct drm_dp_sideband_msg_reply_body *repmsg)
535 {
536         int idx = 1;
537
538         repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
539         idx++;
540         if (idx > raw->curlen)
541                 goto fail_len;
542         repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
543         idx++;
544         /* TODO check */
545         memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
546         return true;
547 fail_len:
548         DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
549         return false;
550 }
551
552 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
553                                                           struct drm_dp_sideband_msg_reply_body *repmsg)
554 {
555         int idx = 1;
556         repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
557         idx++;
558         if (idx > raw->curlen)
559                 goto fail_len;
560         repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
561         idx += 2;
562         if (idx > raw->curlen)
563                 goto fail_len;
564         repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
565         idx += 2;
566         if (idx > raw->curlen)
567                 goto fail_len;
568         return true;
569 fail_len:
570         DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
571         return false;
572 }
573
574 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
575                                                           struct drm_dp_sideband_msg_reply_body *repmsg)
576 {
577         int idx = 1;
578         repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
579         idx++;
580         if (idx > raw->curlen)
581                 goto fail_len;
582         repmsg->u.allocate_payload.vcpi = raw->msg[idx];
583         idx++;
584         if (idx > raw->curlen)
585                 goto fail_len;
586         repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
587         idx += 2;
588         if (idx > raw->curlen)
589                 goto fail_len;
590         return true;
591 fail_len:
592         DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
593         return false;
594 }
595
596 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
597                                                     struct drm_dp_sideband_msg_reply_body *repmsg)
598 {
599         int idx = 1;
600         repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
601         idx++;
602         if (idx > raw->curlen)
603                 goto fail_len;
604         repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
605         idx += 2;
606         if (idx > raw->curlen)
607                 goto fail_len;
608         return true;
609 fail_len:
610         DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
611         return false;
612 }
613
614 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx *raw,
615                                                        struct drm_dp_sideband_msg_reply_body *repmsg)
616 {
617         int idx = 1;
618
619         repmsg->u.port_number.port_number = (raw->msg[idx] >> 4) & 0xf;
620         idx++;
621         if (idx > raw->curlen) {
622                 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
623                               idx, raw->curlen);
624                 return false;
625         }
626         return true;
627 }
628
629 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
630                                         struct drm_dp_sideband_msg_reply_body *msg)
631 {
632         memset(msg, 0, sizeof(*msg));
633         msg->reply_type = (raw->msg[0] & 0x80) >> 7;
634         msg->req_type = (raw->msg[0] & 0x7f);
635
636         if (msg->reply_type == DP_SIDEBAND_REPLY_NAK) {
637                 memcpy(msg->u.nak.guid, &raw->msg[1], 16);
638                 msg->u.nak.reason = raw->msg[17];
639                 msg->u.nak.nak_data = raw->msg[18];
640                 return false;
641         }
642
643         switch (msg->req_type) {
644         case DP_LINK_ADDRESS:
645                 return drm_dp_sideband_parse_link_address(raw, msg);
646         case DP_QUERY_PAYLOAD:
647                 return drm_dp_sideband_parse_query_payload_ack(raw, msg);
648         case DP_REMOTE_DPCD_READ:
649                 return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
650         case DP_REMOTE_DPCD_WRITE:
651                 return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
652         case DP_REMOTE_I2C_READ:
653                 return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
654         case DP_ENUM_PATH_RESOURCES:
655                 return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
656         case DP_ALLOCATE_PAYLOAD:
657                 return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
658         case DP_POWER_DOWN_PHY:
659         case DP_POWER_UP_PHY:
660                 return drm_dp_sideband_parse_power_updown_phy_ack(raw, msg);
661         default:
662                 DRM_ERROR("Got unknown reply 0x%02x (%s)\n", msg->req_type,
663                           drm_dp_mst_req_type_str(msg->req_type));
664                 return false;
665         }
666 }
667
668 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
669                                                            struct drm_dp_sideband_msg_req_body *msg)
670 {
671         int idx = 1;
672
673         msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
674         idx++;
675         if (idx > raw->curlen)
676                 goto fail_len;
677
678         memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
679         idx += 16;
680         if (idx > raw->curlen)
681                 goto fail_len;
682
683         msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
684         msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
685         msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
686         msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
687         msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
688         idx++;
689         return true;
690 fail_len:
691         DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
692         return false;
693 }
694
695 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
696                                                            struct drm_dp_sideband_msg_req_body *msg)
697 {
698         int idx = 1;
699
700         msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
701         idx++;
702         if (idx > raw->curlen)
703                 goto fail_len;
704
705         memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
706         idx += 16;
707         if (idx > raw->curlen)
708                 goto fail_len;
709
710         msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
711         idx++;
712         return true;
713 fail_len:
714         DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
715         return false;
716 }
717
718 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
719                                       struct drm_dp_sideband_msg_req_body *msg)
720 {
721         memset(msg, 0, sizeof(*msg));
722         msg->req_type = (raw->msg[0] & 0x7f);
723
724         switch (msg->req_type) {
725         case DP_CONNECTION_STATUS_NOTIFY:
726                 return drm_dp_sideband_parse_connection_status_notify(raw, msg);
727         case DP_RESOURCE_STATUS_NOTIFY:
728                 return drm_dp_sideband_parse_resource_status_notify(raw, msg);
729         default:
730                 DRM_ERROR("Got unknown request 0x%02x (%s)\n", msg->req_type,
731                           drm_dp_mst_req_type_str(msg->req_type));
732                 return false;
733         }
734 }
735
736 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
737 {
738         struct drm_dp_sideband_msg_req_body req;
739
740         req.req_type = DP_REMOTE_DPCD_WRITE;
741         req.u.dpcd_write.port_number = port_num;
742         req.u.dpcd_write.dpcd_address = offset;
743         req.u.dpcd_write.num_bytes = num_bytes;
744         req.u.dpcd_write.bytes = bytes;
745         drm_dp_encode_sideband_req(&req, msg);
746
747         return 0;
748 }
749
750 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
751 {
752         struct drm_dp_sideband_msg_req_body req;
753
754         req.req_type = DP_LINK_ADDRESS;
755         drm_dp_encode_sideband_req(&req, msg);
756         return 0;
757 }
758
759 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
760 {
761         struct drm_dp_sideband_msg_req_body req;
762
763         req.req_type = DP_ENUM_PATH_RESOURCES;
764         req.u.port_num.port_number = port_num;
765         drm_dp_encode_sideband_req(&req, msg);
766         msg->path_msg = true;
767         return 0;
768 }
769
770 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
771                                   u8 vcpi, uint16_t pbn,
772                                   u8 number_sdp_streams,
773                                   u8 *sdp_stream_sink)
774 {
775         struct drm_dp_sideband_msg_req_body req;
776         memset(&req, 0, sizeof(req));
777         req.req_type = DP_ALLOCATE_PAYLOAD;
778         req.u.allocate_payload.port_number = port_num;
779         req.u.allocate_payload.vcpi = vcpi;
780         req.u.allocate_payload.pbn = pbn;
781         req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
782         memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
783                    number_sdp_streams);
784         drm_dp_encode_sideband_req(&req, msg);
785         msg->path_msg = true;
786         return 0;
787 }
788
789 static int build_power_updown_phy(struct drm_dp_sideband_msg_tx *msg,
790                                   int port_num, bool power_up)
791 {
792         struct drm_dp_sideband_msg_req_body req;
793
794         if (power_up)
795                 req.req_type = DP_POWER_UP_PHY;
796         else
797                 req.req_type = DP_POWER_DOWN_PHY;
798
799         req.u.port_num.port_number = port_num;
800         drm_dp_encode_sideband_req(&req, msg);
801         msg->path_msg = true;
802         return 0;
803 }
804
805 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
806                                         struct drm_dp_vcpi *vcpi)
807 {
808         int ret, vcpi_ret;
809
810         mutex_lock(&mgr->payload_lock);
811         ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
812         if (ret > mgr->max_payloads) {
813                 ret = -EINVAL;
814                 DRM_DEBUG_KMS("out of payload ids %d\n", ret);
815                 goto out_unlock;
816         }
817
818         vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
819         if (vcpi_ret > mgr->max_payloads) {
820                 ret = -EINVAL;
821                 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
822                 goto out_unlock;
823         }
824
825         set_bit(ret, &mgr->payload_mask);
826         set_bit(vcpi_ret, &mgr->vcpi_mask);
827         vcpi->vcpi = vcpi_ret + 1;
828         mgr->proposed_vcpis[ret - 1] = vcpi;
829 out_unlock:
830         mutex_unlock(&mgr->payload_lock);
831         return ret;
832 }
833
834 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
835                                       int vcpi)
836 {
837         int i;
838         if (vcpi == 0)
839                 return;
840
841         mutex_lock(&mgr->payload_lock);
842         DRM_DEBUG_KMS("putting payload %d\n", vcpi);
843         clear_bit(vcpi - 1, &mgr->vcpi_mask);
844
845         for (i = 0; i < mgr->max_payloads; i++) {
846                 if (mgr->proposed_vcpis[i])
847                         if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
848                                 mgr->proposed_vcpis[i] = NULL;
849                                 clear_bit(i + 1, &mgr->payload_mask);
850                         }
851         }
852         mutex_unlock(&mgr->payload_lock);
853 }
854
855 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
856                               struct drm_dp_sideband_msg_tx *txmsg)
857 {
858         unsigned int state;
859
860         /*
861          * All updates to txmsg->state are protected by mgr->qlock, and the two
862          * cases we check here are terminal states. For those the barriers
863          * provided by the wake_up/wait_event pair are enough.
864          */
865         state = READ_ONCE(txmsg->state);
866         return (state == DRM_DP_SIDEBAND_TX_RX ||
867                 state == DRM_DP_SIDEBAND_TX_TIMEOUT);
868 }
869
870 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
871                                     struct drm_dp_sideband_msg_tx *txmsg)
872 {
873         struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
874         int ret;
875
876         ret = wait_event_timeout(mgr->tx_waitq,
877                                  check_txmsg_state(mgr, txmsg),
878                                  (4 * HZ));
879         mutex_lock(&mstb->mgr->qlock);
880         if (ret > 0) {
881                 if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
882                         ret = -EIO;
883                         goto out;
884                 }
885         } else {
886                 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
887
888                 /* dump some state */
889                 ret = -EIO;
890
891                 /* remove from q */
892                 if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
893                     txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
894                         list_del(&txmsg->next);
895                 }
896
897                 if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
898                     txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
899                         mstb->tx_slots[txmsg->seqno] = NULL;
900                 }
901         }
902 out:
903         mutex_unlock(&mgr->qlock);
904
905         return ret;
906 }
907
908 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
909 {
910         struct drm_dp_mst_branch *mstb;
911
912         mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
913         if (!mstb)
914                 return NULL;
915
916         mstb->lct = lct;
917         if (lct > 1)
918                 memcpy(mstb->rad, rad, lct / 2);
919         INIT_LIST_HEAD(&mstb->ports);
920         kref_init(&mstb->topology_kref);
921         kref_init(&mstb->malloc_kref);
922         return mstb;
923 }
924
925 static void drm_dp_free_mst_branch_device(struct kref *kref)
926 {
927         struct drm_dp_mst_branch *mstb =
928                 container_of(kref, struct drm_dp_mst_branch, malloc_kref);
929
930         if (mstb->port_parent)
931                 drm_dp_mst_put_port_malloc(mstb->port_parent);
932
933         kfree(mstb);
934 }
935
936 /**
937  * DOC: Branch device and port refcounting
938  *
939  * Topology refcount overview
940  * ~~~~~~~~~~~~~~~~~~~~~~~~~~
941  *
942  * The refcounting schemes for &struct drm_dp_mst_branch and &struct
943  * drm_dp_mst_port are somewhat unusual. Both ports and branch devices have
944  * two different kinds of refcounts: topology refcounts, and malloc refcounts.
945  *
946  * Topology refcounts are not exposed to drivers, and are handled internally
947  * by the DP MST helpers. The helpers use them in order to prevent the
948  * in-memory topology state from being changed in the middle of critical
949  * operations like changing the internal state of payload allocations. This
950  * means each branch and port will be considered to be connected to the rest
951  * of the topology until its topology refcount reaches zero. Additionally,
952  * for ports this means that their associated &struct drm_connector will stay
953  * registered with userspace until the port's refcount reaches 0.
954  *
955  * Malloc refcount overview
956  * ~~~~~~~~~~~~~~~~~~~~~~~~
957  *
958  * Malloc references are used to keep a &struct drm_dp_mst_port or &struct
959  * drm_dp_mst_branch allocated even after all of its topology references have
960  * been dropped, so that the driver or MST helpers can safely access each
961  * branch's last known state before it was disconnected from the topology.
962  * When the malloc refcount of a port or branch reaches 0, the memory
963  * allocation containing the &struct drm_dp_mst_branch or &struct
964  * drm_dp_mst_port respectively will be freed.
965  *
966  * For &struct drm_dp_mst_branch, malloc refcounts are not currently exposed
967  * to drivers. As of writing this documentation, there are no drivers that
968  * have a usecase for accessing &struct drm_dp_mst_branch outside of the MST
969  * helpers. Exposing this API to drivers in a race-free manner would take more
970  * tweaking of the refcounting scheme, however patches are welcome provided
971  * there is a legitimate driver usecase for this.
972  *
973  * Refcount relationships in a topology
974  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
975  *
976  * Let's take a look at why the relationship between topology and malloc
977  * refcounts is designed the way it is.
978  *
979  * .. kernel-figure:: dp-mst/topology-figure-1.dot
980  *
981  *    An example of topology and malloc refs in a DP MST topology with two
982  *    active payloads. Topology refcount increments are indicated by solid
983  *    lines, and malloc refcount increments are indicated by dashed lines.
984  *    Each starts from the branch which incremented the refcount, and ends at
985  *    the branch to which the refcount belongs to, i.e. the arrow points the
986  *    same way as the C pointers used to reference a structure.
987  *
988  * As you can see in the above figure, every branch increments the topology
989  * refcount of its children, and increments the malloc refcount of its
990  * parent. Additionally, every payload increments the malloc refcount of its
991  * assigned port by 1.
992  *
993  * So, what would happen if MSTB #3 from the above figure was unplugged from
994  * the system, but the driver hadn't yet removed payload #2 from port #3? The
995  * topology would start to look like the figure below.
996  *
997  * .. kernel-figure:: dp-mst/topology-figure-2.dot
998  *
999  *    Ports and branch devices which have been released from memory are
1000  *    colored grey, and references which have been removed are colored red.
1001  *
1002  * Whenever a port or branch device's topology refcount reaches zero, it will
1003  * decrement the topology refcounts of all its children, the malloc refcount
1004  * of its parent, and finally its own malloc refcount. For MSTB #4 and port
1005  * #4, this means they both have been disconnected from the topology and freed
1006  * from memory. But, because payload #2 is still holding a reference to port
1007  * #3, port #3 is removed from the topology but its &struct drm_dp_mst_port
1008  * is still accessible from memory. This also means port #3 has not yet
1009  * decremented the malloc refcount of MSTB #3, so its &struct
1010  * drm_dp_mst_branch will also stay allocated in memory until port #3's
1011  * malloc refcount reaches 0.
1012  *
1013  * This relationship is necessary because in order to release payload #2, we
1014  * need to be able to figure out the last relative of port #3 that's still
1015  * connected to the topology. In this case, we would travel up the topology as
1016  * shown below.
1017  *
1018  * .. kernel-figure:: dp-mst/topology-figure-3.dot
1019  *
1020  * And finally, remove payload #2 by communicating with port #2 through
1021  * sideband transactions.
1022  */
1023
1024 /**
1025  * drm_dp_mst_get_mstb_malloc() - Increment the malloc refcount of a branch
1026  * device
1027  * @mstb: The &struct drm_dp_mst_branch to increment the malloc refcount of
1028  *
1029  * Increments &drm_dp_mst_branch.malloc_kref. When
1030  * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1031  * will be released and @mstb may no longer be used.
1032  *
1033  * See also: drm_dp_mst_put_mstb_malloc()
1034  */
1035 static void
1036 drm_dp_mst_get_mstb_malloc(struct drm_dp_mst_branch *mstb)
1037 {
1038         kref_get(&mstb->malloc_kref);
1039         DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref));
1040 }
1041
1042 /**
1043  * drm_dp_mst_put_mstb_malloc() - Decrement the malloc refcount of a branch
1044  * device
1045  * @mstb: The &struct drm_dp_mst_branch to decrement the malloc refcount of
1046  *
1047  * Decrements &drm_dp_mst_branch.malloc_kref. When
1048  * &drm_dp_mst_branch.malloc_kref reaches 0, the memory allocation for @mstb
1049  * will be released and @mstb may no longer be used.
1050  *
1051  * See also: drm_dp_mst_get_mstb_malloc()
1052  */
1053 static void
1054 drm_dp_mst_put_mstb_malloc(struct drm_dp_mst_branch *mstb)
1055 {
1056         DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->malloc_kref) - 1);
1057         kref_put(&mstb->malloc_kref, drm_dp_free_mst_branch_device);
1058 }
1059
1060 static void drm_dp_free_mst_port(struct kref *kref)
1061 {
1062         struct drm_dp_mst_port *port =
1063                 container_of(kref, struct drm_dp_mst_port, malloc_kref);
1064
1065         drm_dp_mst_put_mstb_malloc(port->parent);
1066         kfree(port);
1067 }
1068
1069 /**
1070  * drm_dp_mst_get_port_malloc() - Increment the malloc refcount of an MST port
1071  * @port: The &struct drm_dp_mst_port to increment the malloc refcount of
1072  *
1073  * Increments &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1074  * reaches 0, the memory allocation for @port will be released and @port may
1075  * no longer be used.
1076  *
1077  * Because @port could potentially be freed at any time by the DP MST helpers
1078  * if &drm_dp_mst_port.malloc_kref reaches 0, including during a call to this
1079  * function, drivers that which to make use of &struct drm_dp_mst_port should
1080  * ensure that they grab at least one main malloc reference to their MST ports
1081  * in &drm_dp_mst_topology_cbs.add_connector. This callback is called before
1082  * there is any chance for &drm_dp_mst_port.malloc_kref to reach 0.
1083  *
1084  * See also: drm_dp_mst_put_port_malloc()
1085  */
1086 void
1087 drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port)
1088 {
1089         kref_get(&port->malloc_kref);
1090         DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref));
1091 }
1092 EXPORT_SYMBOL(drm_dp_mst_get_port_malloc);
1093
1094 /**
1095  * drm_dp_mst_put_port_malloc() - Decrement the malloc refcount of an MST port
1096  * @port: The &struct drm_dp_mst_port to decrement the malloc refcount of
1097  *
1098  * Decrements &drm_dp_mst_port.malloc_kref. When &drm_dp_mst_port.malloc_kref
1099  * reaches 0, the memory allocation for @port will be released and @port may
1100  * no longer be used.
1101  *
1102  * See also: drm_dp_mst_get_port_malloc()
1103  */
1104 void
1105 drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port)
1106 {
1107         DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->malloc_kref) - 1);
1108         kref_put(&port->malloc_kref, drm_dp_free_mst_port);
1109 }
1110 EXPORT_SYMBOL(drm_dp_mst_put_port_malloc);
1111
1112 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
1113 {
1114         struct drm_dp_mst_branch *mstb =
1115                 container_of(kref, struct drm_dp_mst_branch, topology_kref);
1116         struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
1117         struct drm_dp_mst_port *port, *tmp;
1118         bool wake_tx = false;
1119
1120         mutex_lock(&mgr->lock);
1121         list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
1122                 list_del(&port->next);
1123                 drm_dp_mst_topology_put_port(port);
1124         }
1125         mutex_unlock(&mgr->lock);
1126
1127         /* drop any tx slots msg */
1128         mutex_lock(&mstb->mgr->qlock);
1129         if (mstb->tx_slots[0]) {
1130                 mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1131                 mstb->tx_slots[0] = NULL;
1132                 wake_tx = true;
1133         }
1134         if (mstb->tx_slots[1]) {
1135                 mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1136                 mstb->tx_slots[1] = NULL;
1137                 wake_tx = true;
1138         }
1139         mutex_unlock(&mstb->mgr->qlock);
1140
1141         if (wake_tx)
1142                 wake_up_all(&mstb->mgr->tx_waitq);
1143
1144         drm_dp_mst_put_mstb_malloc(mstb);
1145 }
1146
1147 /**
1148  * drm_dp_mst_topology_try_get_mstb() - Increment the topology refcount of a
1149  * branch device unless it's zero
1150  * @mstb: &struct drm_dp_mst_branch to increment the topology refcount of
1151  *
1152  * Attempts to grab a topology reference to @mstb, if it hasn't yet been
1153  * removed from the topology (e.g. &drm_dp_mst_branch.topology_kref has
1154  * reached 0). Holding a topology reference implies that a malloc reference
1155  * will be held to @mstb as long as the user holds the topology reference.
1156  *
1157  * Care should be taken to ensure that the user has at least one malloc
1158  * reference to @mstb. If you already have a topology reference to @mstb, you
1159  * should use drm_dp_mst_topology_get_mstb() instead.
1160  *
1161  * See also:
1162  * drm_dp_mst_topology_get_mstb()
1163  * drm_dp_mst_topology_put_mstb()
1164  *
1165  * Returns:
1166  * * 1: A topology reference was grabbed successfully
1167  * * 0: @port is no longer in the topology, no reference was grabbed
1168  */
1169 static int __must_check
1170 drm_dp_mst_topology_try_get_mstb(struct drm_dp_mst_branch *mstb)
1171 {
1172         int ret = kref_get_unless_zero(&mstb->topology_kref);
1173
1174         if (ret)
1175                 DRM_DEBUG("mstb %p (%d)\n", mstb,
1176                           kref_read(&mstb->topology_kref));
1177
1178         return ret;
1179 }
1180
1181 /**
1182  * drm_dp_mst_topology_get_mstb() - Increment the topology refcount of a
1183  * branch device
1184  * @mstb: The &struct drm_dp_mst_branch to increment the topology refcount of
1185  *
1186  * Increments &drm_dp_mst_branch.topology_refcount without checking whether or
1187  * not it's already reached 0. This is only valid to use in scenarios where
1188  * you are already guaranteed to have at least one active topology reference
1189  * to @mstb. Otherwise, drm_dp_mst_topology_try_get_mstb() must be used.
1190  *
1191  * See also:
1192  * drm_dp_mst_topology_try_get_mstb()
1193  * drm_dp_mst_topology_put_mstb()
1194  */
1195 static void drm_dp_mst_topology_get_mstb(struct drm_dp_mst_branch *mstb)
1196 {
1197         WARN_ON(kref_read(&mstb->topology_kref) == 0);
1198         kref_get(&mstb->topology_kref);
1199         DRM_DEBUG("mstb %p (%d)\n", mstb, kref_read(&mstb->topology_kref));
1200 }
1201
1202 /**
1203  * drm_dp_mst_topology_put_mstb() - release a topology reference to a branch
1204  * device
1205  * @mstb: The &struct drm_dp_mst_branch to release the topology reference from
1206  *
1207  * Releases a topology reference from @mstb by decrementing
1208  * &drm_dp_mst_branch.topology_kref.
1209  *
1210  * See also:
1211  * drm_dp_mst_topology_try_get_mstb()
1212  * drm_dp_mst_topology_get_mstb()
1213  */
1214 static void
1215 drm_dp_mst_topology_put_mstb(struct drm_dp_mst_branch *mstb)
1216 {
1217         DRM_DEBUG("mstb %p (%d)\n",
1218                   mstb, kref_read(&mstb->topology_kref) - 1);
1219         kref_put(&mstb->topology_kref, drm_dp_destroy_mst_branch_device);
1220 }
1221
1222 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
1223 {
1224         struct drm_dp_mst_branch *mstb;
1225
1226         switch (old_pdt) {
1227         case DP_PEER_DEVICE_DP_LEGACY_CONV:
1228         case DP_PEER_DEVICE_SST_SINK:
1229                 /* remove i2c over sideband */
1230                 drm_dp_mst_unregister_i2c_bus(&port->aux);
1231                 break;
1232         case DP_PEER_DEVICE_MST_BRANCHING:
1233                 mstb = port->mstb;
1234                 port->mstb = NULL;
1235                 drm_dp_mst_topology_put_mstb(mstb);
1236                 break;
1237         }
1238 }
1239
1240 static void drm_dp_destroy_port(struct kref *kref)
1241 {
1242         struct drm_dp_mst_port *port =
1243                 container_of(kref, struct drm_dp_mst_port, topology_kref);
1244         struct drm_dp_mst_topology_mgr *mgr = port->mgr;
1245
1246         if (!port->input) {
1247                 kfree(port->cached_edid);
1248
1249                 /*
1250                  * The only time we don't have a connector
1251                  * on an output port is if the connector init
1252                  * fails.
1253                  */
1254                 if (port->connector) {
1255                         /* we can't destroy the connector here, as
1256                          * we might be holding the mode_config.mutex
1257                          * from an EDID retrieval */
1258
1259                         mutex_lock(&mgr->destroy_connector_lock);
1260                         list_add(&port->next, &mgr->destroy_connector_list);
1261                         mutex_unlock(&mgr->destroy_connector_lock);
1262                         schedule_work(&mgr->destroy_connector_work);
1263                         return;
1264                 }
1265                 /* no need to clean up vcpi
1266                  * as if we have no connector we never setup a vcpi */
1267                 drm_dp_port_teardown_pdt(port, port->pdt);
1268                 port->pdt = DP_PEER_DEVICE_NONE;
1269         }
1270         drm_dp_mst_put_port_malloc(port);
1271 }
1272
1273 /**
1274  * drm_dp_mst_topology_try_get_port() - Increment the topology refcount of a
1275  * port unless it's zero
1276  * @port: &struct drm_dp_mst_port to increment the topology refcount of
1277  *
1278  * Attempts to grab a topology reference to @port, if it hasn't yet been
1279  * removed from the topology (e.g. &drm_dp_mst_port.topology_kref has reached
1280  * 0). Holding a topology reference implies that a malloc reference will be
1281  * held to @port as long as the user holds the topology reference.
1282  *
1283  * Care should be taken to ensure that the user has at least one malloc
1284  * reference to @port. If you already have a topology reference to @port, you
1285  * should use drm_dp_mst_topology_get_port() instead.
1286  *
1287  * See also:
1288  * drm_dp_mst_topology_get_port()
1289  * drm_dp_mst_topology_put_port()
1290  *
1291  * Returns:
1292  * * 1: A topology reference was grabbed successfully
1293  * * 0: @port is no longer in the topology, no reference was grabbed
1294  */
1295 static int __must_check
1296 drm_dp_mst_topology_try_get_port(struct drm_dp_mst_port *port)
1297 {
1298         int ret = kref_get_unless_zero(&port->topology_kref);
1299
1300         if (ret)
1301                 DRM_DEBUG("port %p (%d)\n", port,
1302                           kref_read(&port->topology_kref));
1303
1304         return ret;
1305 }
1306
1307 /**
1308  * drm_dp_mst_topology_get_port() - Increment the topology refcount of a port
1309  * @port: The &struct drm_dp_mst_port to increment the topology refcount of
1310  *
1311  * Increments &drm_dp_mst_port.topology_refcount without checking whether or
1312  * not it's already reached 0. This is only valid to use in scenarios where
1313  * you are already guaranteed to have at least one active topology reference
1314  * to @port. Otherwise, drm_dp_mst_topology_try_get_port() must be used.
1315  *
1316  * See also:
1317  * drm_dp_mst_topology_try_get_port()
1318  * drm_dp_mst_topology_put_port()
1319  */
1320 static void drm_dp_mst_topology_get_port(struct drm_dp_mst_port *port)
1321 {
1322         WARN_ON(kref_read(&port->topology_kref) == 0);
1323         kref_get(&port->topology_kref);
1324         DRM_DEBUG("port %p (%d)\n", port, kref_read(&port->topology_kref));
1325 }
1326
1327 /**
1328  * drm_dp_mst_topology_put_port() - release a topology reference to a port
1329  * @port: The &struct drm_dp_mst_port to release the topology reference from
1330  *
1331  * Releases a topology reference from @port by decrementing
1332  * &drm_dp_mst_port.topology_kref.
1333  *
1334  * See also:
1335  * drm_dp_mst_topology_try_get_port()
1336  * drm_dp_mst_topology_get_port()
1337  */
1338 static void drm_dp_mst_topology_put_port(struct drm_dp_mst_port *port)
1339 {
1340         DRM_DEBUG("port %p (%d)\n",
1341                   port, kref_read(&port->topology_kref) - 1);
1342         kref_put(&port->topology_kref, drm_dp_destroy_port);
1343 }
1344
1345 static struct drm_dp_mst_branch *
1346 drm_dp_mst_topology_get_mstb_validated_locked(struct drm_dp_mst_branch *mstb,
1347                                               struct drm_dp_mst_branch *to_find)
1348 {
1349         struct drm_dp_mst_port *port;
1350         struct drm_dp_mst_branch *rmstb;
1351
1352         if (to_find == mstb)
1353                 return mstb;
1354
1355         list_for_each_entry(port, &mstb->ports, next) {
1356                 if (port->mstb) {
1357                         rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1358                             port->mstb, to_find);
1359                         if (rmstb)
1360                                 return rmstb;
1361                 }
1362         }
1363         return NULL;
1364 }
1365
1366 static struct drm_dp_mst_branch *
1367 drm_dp_mst_topology_get_mstb_validated(struct drm_dp_mst_topology_mgr *mgr,
1368                                        struct drm_dp_mst_branch *mstb)
1369 {
1370         struct drm_dp_mst_branch *rmstb = NULL;
1371
1372         mutex_lock(&mgr->lock);
1373         if (mgr->mst_primary) {
1374                 rmstb = drm_dp_mst_topology_get_mstb_validated_locked(
1375                     mgr->mst_primary, mstb);
1376
1377                 if (rmstb && !drm_dp_mst_topology_try_get_mstb(rmstb))
1378                         rmstb = NULL;
1379         }
1380         mutex_unlock(&mgr->lock);
1381         return rmstb;
1382 }
1383
1384 static struct drm_dp_mst_port *
1385 drm_dp_mst_topology_get_port_validated_locked(struct drm_dp_mst_branch *mstb,
1386                                               struct drm_dp_mst_port *to_find)
1387 {
1388         struct drm_dp_mst_port *port, *mport;
1389
1390         list_for_each_entry(port, &mstb->ports, next) {
1391                 if (port == to_find)
1392                         return port;
1393
1394                 if (port->mstb) {
1395                         mport = drm_dp_mst_topology_get_port_validated_locked(
1396                             port->mstb, to_find);
1397                         if (mport)
1398                                 return mport;
1399                 }
1400         }
1401         return NULL;
1402 }
1403
1404 static struct drm_dp_mst_port *
1405 drm_dp_mst_topology_get_port_validated(struct drm_dp_mst_topology_mgr *mgr,
1406                                        struct drm_dp_mst_port *port)
1407 {
1408         struct drm_dp_mst_port *rport = NULL;
1409
1410         mutex_lock(&mgr->lock);
1411         if (mgr->mst_primary) {
1412                 rport = drm_dp_mst_topology_get_port_validated_locked(
1413                     mgr->mst_primary, port);
1414
1415                 if (rport && !drm_dp_mst_topology_try_get_port(rport))
1416                         rport = NULL;
1417         }
1418         mutex_unlock(&mgr->lock);
1419         return rport;
1420 }
1421
1422 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
1423 {
1424         struct drm_dp_mst_port *port;
1425         int ret;
1426
1427         list_for_each_entry(port, &mstb->ports, next) {
1428                 if (port->port_num == port_num) {
1429                         ret = drm_dp_mst_topology_try_get_port(port);
1430                         return ret ? port : NULL;
1431                 }
1432         }
1433
1434         return NULL;
1435 }
1436
1437 /*
1438  * calculate a new RAD for this MST branch device
1439  * if parent has an LCT of 2 then it has 1 nibble of RAD,
1440  * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1441  */
1442 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1443                                  u8 *rad)
1444 {
1445         int parent_lct = port->parent->lct;
1446         int shift = 4;
1447         int idx = (parent_lct - 1) / 2;
1448         if (parent_lct > 1) {
1449                 memcpy(rad, port->parent->rad, idx + 1);
1450                 shift = (parent_lct % 2) ? 4 : 0;
1451         } else
1452                 rad[0] = 0;
1453
1454         rad[idx] |= port->port_num << shift;
1455         return parent_lct + 1;
1456 }
1457
1458 /*
1459  * return sends link address for new mstb
1460  */
1461 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1462 {
1463         int ret;
1464         u8 rad[6], lct;
1465         bool send_link = false;
1466         switch (port->pdt) {
1467         case DP_PEER_DEVICE_DP_LEGACY_CONV:
1468         case DP_PEER_DEVICE_SST_SINK:
1469                 /* add i2c over sideband */
1470                 ret = drm_dp_mst_register_i2c_bus(&port->aux);
1471                 break;
1472         case DP_PEER_DEVICE_MST_BRANCHING:
1473                 lct = drm_dp_calculate_rad(port, rad);
1474
1475                 port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1476                 if (port->mstb) {
1477                         port->mstb->mgr = port->mgr;
1478                         port->mstb->port_parent = port;
1479                         /*
1480                          * Make sure this port's memory allocation stays
1481                          * around until its child MSTB releases it
1482                          */
1483                         drm_dp_mst_get_port_malloc(port);
1484
1485                         send_link = true;
1486                 }
1487                 break;
1488         }
1489         return send_link;
1490 }
1491
1492 /**
1493  * drm_dp_mst_dpcd_read() - read a series of bytes from the DPCD via sideband
1494  * @aux: Fake sideband AUX CH
1495  * @offset: address of the (first) register to read
1496  * @buffer: buffer to store the register values
1497  * @size: number of bytes in @buffer
1498  *
1499  * Performs the same functionality for remote devices via
1500  * sideband messaging as drm_dp_dpcd_read() does for local
1501  * devices via actual AUX CH.
1502  *
1503  * Return: Number of bytes read, or negative error code on failure.
1504  */
1505 ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
1506                              unsigned int offset, void *buffer, size_t size)
1507 {
1508         struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
1509                                                     aux);
1510
1511         return drm_dp_send_dpcd_read(port->mgr, port,
1512                                      offset, size, buffer);
1513 }
1514
1515 /**
1516  * drm_dp_mst_dpcd_write() - write a series of bytes to the DPCD via sideband
1517  * @aux: Fake sideband AUX CH
1518  * @offset: address of the (first) register to write
1519  * @buffer: buffer containing the values to write
1520  * @size: number of bytes in @buffer
1521  *
1522  * Performs the same functionality for remote devices via
1523  * sideband messaging as drm_dp_dpcd_write() does for local
1524  * devices via actual AUX CH.
1525  *
1526  * Return: 0 on success, negative error code on failure.
1527  */
1528 ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
1529                               unsigned int offset, void *buffer, size_t size)
1530 {
1531         struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port,
1532                                                     aux);
1533
1534         return drm_dp_send_dpcd_write(port->mgr, port,
1535                                       offset, size, buffer);
1536 }
1537
1538 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1539 {
1540         int ret;
1541
1542         memcpy(mstb->guid, guid, 16);
1543
1544         if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1545                 if (mstb->port_parent) {
1546                         ret = drm_dp_send_dpcd_write(
1547                                         mstb->mgr,
1548                                         mstb->port_parent,
1549                                         DP_GUID,
1550                                         16,
1551                                         mstb->guid);
1552                 } else {
1553
1554                         ret = drm_dp_dpcd_write(
1555                                         mstb->mgr->aux,
1556                                         DP_GUID,
1557                                         mstb->guid,
1558                                         16);
1559                 }
1560         }
1561 }
1562
1563 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1564                                 int pnum,
1565                                 char *proppath,
1566                                 size_t proppath_size)
1567 {
1568         int i;
1569         char temp[8];
1570         snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1571         for (i = 0; i < (mstb->lct - 1); i++) {
1572                 int shift = (i % 2) ? 0 : 4;
1573                 int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1574                 snprintf(temp, sizeof(temp), "-%d", port_num);
1575                 strlcat(proppath, temp, proppath_size);
1576         }
1577         snprintf(temp, sizeof(temp), "-%d", pnum);
1578         strlcat(proppath, temp, proppath_size);
1579 }
1580
1581 /**
1582  * drm_dp_mst_connector_late_register() - Late MST connector registration
1583  * @connector: The MST connector
1584  * @port: The MST port for this connector
1585  *
1586  * Helper to register the remote aux device for this MST port. Drivers should
1587  * call this from their mst connector's late_register hook to enable MST aux
1588  * devices.
1589  *
1590  * Return: 0 on success, negative error code on failure.
1591  */
1592 int drm_dp_mst_connector_late_register(struct drm_connector *connector,
1593                                        struct drm_dp_mst_port *port)
1594 {
1595         DRM_DEBUG_KMS("registering %s remote bus for %s\n",
1596                       port->aux.name, connector->kdev->kobj.name);
1597
1598         port->aux.dev = connector->kdev;
1599         return drm_dp_aux_register_devnode(&port->aux);
1600 }
1601 EXPORT_SYMBOL(drm_dp_mst_connector_late_register);
1602
1603 /**
1604  * drm_dp_mst_connector_early_unregister() - Early MST connector unregistration
1605  * @connector: The MST connector
1606  * @port: The MST port for this connector
1607  *
1608  * Helper to unregister the remote aux device for this MST port, registered by
1609  * drm_dp_mst_connector_late_register(). Drivers should call this from their mst
1610  * connector's early_unregister hook.
1611  */
1612 void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
1613                                            struct drm_dp_mst_port *port)
1614 {
1615         DRM_DEBUG_KMS("unregistering %s remote bus for %s\n",
1616                       port->aux.name, connector->kdev->kobj.name);
1617         drm_dp_aux_unregister_devnode(&port->aux);
1618 }
1619 EXPORT_SYMBOL(drm_dp_mst_connector_early_unregister);
1620
1621 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1622                             struct drm_device *dev,
1623                             struct drm_dp_link_addr_reply_port *port_msg)
1624 {
1625         struct drm_dp_mst_port *port;
1626         bool ret;
1627         bool created = false;
1628         int old_pdt = 0;
1629         int old_ddps = 0;
1630
1631         port = drm_dp_get_port(mstb, port_msg->port_number);
1632         if (!port) {
1633                 port = kzalloc(sizeof(*port), GFP_KERNEL);
1634                 if (!port)
1635                         return;
1636                 kref_init(&port->topology_kref);
1637                 kref_init(&port->malloc_kref);
1638                 port->parent = mstb;
1639                 port->port_num = port_msg->port_number;
1640                 port->mgr = mstb->mgr;
1641                 port->aux.name = "DPMST";
1642                 port->aux.dev = dev->dev;
1643                 port->aux.is_remote = true;
1644
1645                 /*
1646                  * Make sure the memory allocation for our parent branch stays
1647                  * around until our own memory allocation is released
1648                  */
1649                 drm_dp_mst_get_mstb_malloc(mstb);
1650
1651                 created = true;
1652         } else {
1653                 old_pdt = port->pdt;
1654                 old_ddps = port->ddps;
1655         }
1656
1657         port->pdt = port_msg->peer_device_type;
1658         port->input = port_msg->input_port;
1659         port->mcs = port_msg->mcs;
1660         port->ddps = port_msg->ddps;
1661         port->ldps = port_msg->legacy_device_plug_status;
1662         port->dpcd_rev = port_msg->dpcd_revision;
1663         port->num_sdp_streams = port_msg->num_sdp_streams;
1664         port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1665
1666         /* manage mstb port lists with mgr lock - take a reference
1667            for this list */
1668         if (created) {
1669                 mutex_lock(&mstb->mgr->lock);
1670                 drm_dp_mst_topology_get_port(port);
1671                 list_add(&port->next, &mstb->ports);
1672                 mutex_unlock(&mstb->mgr->lock);
1673         }
1674
1675         if (old_ddps != port->ddps) {
1676                 if (port->ddps) {
1677                         if (!port->input) {
1678                                 drm_dp_send_enum_path_resources(mstb->mgr,
1679                                                                 mstb, port);
1680                         }
1681                 } else {
1682                         port->available_pbn = 0;
1683                 }
1684         }
1685
1686         if (old_pdt != port->pdt && !port->input) {
1687                 drm_dp_port_teardown_pdt(port, old_pdt);
1688
1689                 ret = drm_dp_port_setup_pdt(port);
1690                 if (ret == true)
1691                         drm_dp_send_link_address(mstb->mgr, port->mstb);
1692         }
1693
1694         if (created && !port->input) {
1695                 char proppath[255];
1696
1697                 build_mst_prop_path(mstb, port->port_num, proppath,
1698                                     sizeof(proppath));
1699                 port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr,
1700                                                                    port,
1701                                                                    proppath);
1702                 if (!port->connector) {
1703                         /* remove it from the port list */
1704                         mutex_lock(&mstb->mgr->lock);
1705                         list_del(&port->next);
1706                         mutex_unlock(&mstb->mgr->lock);
1707                         /* drop port list reference */
1708                         drm_dp_mst_topology_put_port(port);
1709                         goto out;
1710                 }
1711                 if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1712                      port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1713                     port->port_num >= DP_MST_LOGICAL_PORT_0) {
1714                         port->cached_edid = drm_get_edid(port->connector,
1715                                                          &port->aux.ddc);
1716                         drm_connector_set_tile_property(port->connector);
1717                 }
1718                 (*mstb->mgr->cbs->register_connector)(port->connector);
1719         }
1720
1721 out:
1722         /* put reference to this port */
1723         drm_dp_mst_topology_put_port(port);
1724 }
1725
1726 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1727                                struct drm_dp_connection_status_notify *conn_stat)
1728 {
1729         struct drm_dp_mst_port *port;
1730         int old_pdt;
1731         int old_ddps;
1732         bool dowork = false;
1733         port = drm_dp_get_port(mstb, conn_stat->port_number);
1734         if (!port)
1735                 return;
1736
1737         old_ddps = port->ddps;
1738         old_pdt = port->pdt;
1739         port->pdt = conn_stat->peer_device_type;
1740         port->mcs = conn_stat->message_capability_status;
1741         port->ldps = conn_stat->legacy_device_plug_status;
1742         port->ddps = conn_stat->displayport_device_plug_status;
1743
1744         if (old_ddps != port->ddps) {
1745                 if (port->ddps) {
1746                         dowork = true;
1747                 } else {
1748                         port->available_pbn = 0;
1749                 }
1750         }
1751         if (old_pdt != port->pdt && !port->input) {
1752                 drm_dp_port_teardown_pdt(port, old_pdt);
1753
1754                 if (drm_dp_port_setup_pdt(port))
1755                         dowork = true;
1756         }
1757
1758         drm_dp_mst_topology_put_port(port);
1759         if (dowork)
1760                 queue_work(system_long_wq, &mstb->mgr->work);
1761
1762 }
1763
1764 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1765                                                                u8 lct, u8 *rad)
1766 {
1767         struct drm_dp_mst_branch *mstb;
1768         struct drm_dp_mst_port *port;
1769         int i, ret;
1770         /* find the port by iterating down */
1771
1772         mutex_lock(&mgr->lock);
1773         mstb = mgr->mst_primary;
1774
1775         if (!mstb)
1776                 goto out;
1777
1778         for (i = 0; i < lct - 1; i++) {
1779                 int shift = (i % 2) ? 0 : 4;
1780                 int port_num = (rad[i / 2] >> shift) & 0xf;
1781
1782                 list_for_each_entry(port, &mstb->ports, next) {
1783                         if (port->port_num == port_num) {
1784                                 mstb = port->mstb;
1785                                 if (!mstb) {
1786                                         DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1787                                         goto out;
1788                                 }
1789
1790                                 break;
1791                         }
1792                 }
1793         }
1794         ret = drm_dp_mst_topology_try_get_mstb(mstb);
1795         if (!ret)
1796                 mstb = NULL;
1797 out:
1798         mutex_unlock(&mgr->lock);
1799         return mstb;
1800 }
1801
1802 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1803         struct drm_dp_mst_branch *mstb,
1804         uint8_t *guid)
1805 {
1806         struct drm_dp_mst_branch *found_mstb;
1807         struct drm_dp_mst_port *port;
1808
1809         if (memcmp(mstb->guid, guid, 16) == 0)
1810                 return mstb;
1811
1812
1813         list_for_each_entry(port, &mstb->ports, next) {
1814                 if (!port->mstb)
1815                         continue;
1816
1817                 found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1818
1819                 if (found_mstb)
1820                         return found_mstb;
1821         }
1822
1823         return NULL;
1824 }
1825
1826 static struct drm_dp_mst_branch *
1827 drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr *mgr,
1828                                      uint8_t *guid)
1829 {
1830         struct drm_dp_mst_branch *mstb;
1831         int ret;
1832
1833         /* find the port by iterating down */
1834         mutex_lock(&mgr->lock);
1835
1836         mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1837         if (mstb) {
1838                 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1839                 if (!ret)
1840                         mstb = NULL;
1841         }
1842
1843         mutex_unlock(&mgr->lock);
1844         return mstb;
1845 }
1846
1847 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1848                                                struct drm_dp_mst_branch *mstb)
1849 {
1850         struct drm_dp_mst_port *port;
1851         struct drm_dp_mst_branch *mstb_child;
1852         if (!mstb->link_address_sent)
1853                 drm_dp_send_link_address(mgr, mstb);
1854
1855         list_for_each_entry(port, &mstb->ports, next) {
1856                 if (port->input)
1857                         continue;
1858
1859                 if (!port->ddps)
1860                         continue;
1861
1862                 if (!port->available_pbn)
1863                         drm_dp_send_enum_path_resources(mgr, mstb, port);
1864
1865                 if (port->mstb) {
1866                         mstb_child = drm_dp_mst_topology_get_mstb_validated(
1867                             mgr, port->mstb);
1868                         if (mstb_child) {
1869                                 drm_dp_check_and_send_link_address(mgr, mstb_child);
1870                                 drm_dp_mst_topology_put_mstb(mstb_child);
1871                         }
1872                 }
1873         }
1874 }
1875
1876 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1877 {
1878         struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1879         struct drm_dp_mst_branch *mstb;
1880         int ret;
1881
1882         mutex_lock(&mgr->lock);
1883         mstb = mgr->mst_primary;
1884         if (mstb) {
1885                 ret = drm_dp_mst_topology_try_get_mstb(mstb);
1886                 if (!ret)
1887                         mstb = NULL;
1888         }
1889         mutex_unlock(&mgr->lock);
1890         if (mstb) {
1891                 drm_dp_check_and_send_link_address(mgr, mstb);
1892                 drm_dp_mst_topology_put_mstb(mstb);
1893         }
1894 }
1895
1896 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1897                                  u8 *guid)
1898 {
1899         u64 salt;
1900
1901         if (memchr_inv(guid, 0, 16))
1902                 return true;
1903
1904         salt = get_jiffies_64();
1905
1906         memcpy(&guid[0], &salt, sizeof(u64));
1907         memcpy(&guid[8], &salt, sizeof(u64));
1908
1909         return false;
1910 }
1911
1912 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1913 {
1914         struct drm_dp_sideband_msg_req_body req;
1915
1916         req.req_type = DP_REMOTE_DPCD_READ;
1917         req.u.dpcd_read.port_number = port_num;
1918         req.u.dpcd_read.dpcd_address = offset;
1919         req.u.dpcd_read.num_bytes = num_bytes;
1920         drm_dp_encode_sideband_req(&req, msg);
1921
1922         return 0;
1923 }
1924
1925 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1926                                     bool up, u8 *msg, int len)
1927 {
1928         int ret;
1929         int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1930         int tosend, total, offset;
1931         int retries = 0;
1932
1933 retry:
1934         total = len;
1935         offset = 0;
1936         do {
1937                 tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1938
1939                 ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1940                                         &msg[offset],
1941                                         tosend);
1942                 if (ret != tosend) {
1943                         if (ret == -EIO && retries < 5) {
1944                                 retries++;
1945                                 goto retry;
1946                         }
1947                         DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1948
1949                         return -EIO;
1950                 }
1951                 offset += tosend;
1952                 total -= tosend;
1953         } while (total > 0);
1954         return 0;
1955 }
1956
1957 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1958                                   struct drm_dp_sideband_msg_tx *txmsg)
1959 {
1960         struct drm_dp_mst_branch *mstb = txmsg->dst;
1961         u8 req_type;
1962
1963         /* both msg slots are full */
1964         if (txmsg->seqno == -1) {
1965                 if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1966                         DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1967                         return -EAGAIN;
1968                 }
1969                 if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1970                         txmsg->seqno = mstb->last_seqno;
1971                         mstb->last_seqno ^= 1;
1972                 } else if (mstb->tx_slots[0] == NULL)
1973                         txmsg->seqno = 0;
1974                 else
1975                         txmsg->seqno = 1;
1976                 mstb->tx_slots[txmsg->seqno] = txmsg;
1977         }
1978
1979         req_type = txmsg->msg[0] & 0x7f;
1980         if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1981                 req_type == DP_RESOURCE_STATUS_NOTIFY)
1982                 hdr->broadcast = 1;
1983         else
1984                 hdr->broadcast = 0;
1985         hdr->path_msg = txmsg->path_msg;
1986         hdr->lct = mstb->lct;
1987         hdr->lcr = mstb->lct - 1;
1988         if (mstb->lct > 1)
1989                 memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1990         hdr->seqno = txmsg->seqno;
1991         return 0;
1992 }
1993 /*
1994  * process a single block of the next message in the sideband queue
1995  */
1996 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1997                                    struct drm_dp_sideband_msg_tx *txmsg,
1998                                    bool up)
1999 {
2000         u8 chunk[48];
2001         struct drm_dp_sideband_msg_hdr hdr;
2002         int len, space, idx, tosend;
2003         int ret;
2004
2005         memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
2006
2007         if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
2008                 txmsg->seqno = -1;
2009                 txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
2010         }
2011
2012         /* make hdr from dst mst - for replies use seqno
2013            otherwise assign one */
2014         ret = set_hdr_from_dst_qlock(&hdr, txmsg);
2015         if (ret < 0)
2016                 return ret;
2017
2018         /* amount left to send in this message */
2019         len = txmsg->cur_len - txmsg->cur_offset;
2020
2021         /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
2022         space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
2023
2024         tosend = min(len, space);
2025         if (len == txmsg->cur_len)
2026                 hdr.somt = 1;
2027         if (space >= len)
2028                 hdr.eomt = 1;
2029
2030
2031         hdr.msg_len = tosend + 1;
2032         drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
2033         memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
2034         /* add crc at end */
2035         drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
2036         idx += tosend + 1;
2037
2038         ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
2039         if (ret) {
2040                 DRM_DEBUG_KMS("sideband msg failed to send\n");
2041                 return ret;
2042         }
2043
2044         txmsg->cur_offset += tosend;
2045         if (txmsg->cur_offset == txmsg->cur_len) {
2046                 txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
2047                 return 1;
2048         }
2049         return 0;
2050 }
2051
2052 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
2053 {
2054         struct drm_dp_sideband_msg_tx *txmsg;
2055         int ret;
2056
2057         WARN_ON(!mutex_is_locked(&mgr->qlock));
2058
2059         /* construct a chunk from the first msg in the tx_msg queue */
2060         if (list_empty(&mgr->tx_msg_downq))
2061                 return;
2062
2063         txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
2064         ret = process_single_tx_qlock(mgr, txmsg, false);
2065         if (ret == 1) {
2066                 /* txmsg is sent it should be in the slots now */
2067                 list_del(&txmsg->next);
2068         } else if (ret) {
2069                 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
2070                 list_del(&txmsg->next);
2071                 if (txmsg->seqno != -1)
2072                         txmsg->dst->tx_slots[txmsg->seqno] = NULL;
2073                 txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
2074                 wake_up_all(&mgr->tx_waitq);
2075         }
2076 }
2077
2078 /* called holding qlock */
2079 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
2080                                        struct drm_dp_sideband_msg_tx *txmsg)
2081 {
2082         int ret;
2083
2084         /* construct a chunk from the first msg in the tx_msg queue */
2085         ret = process_single_tx_qlock(mgr, txmsg, true);
2086
2087         if (ret != 1)
2088                 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
2089
2090         if (txmsg->seqno != -1) {
2091                 WARN_ON((unsigned int)txmsg->seqno >
2092                         ARRAY_SIZE(txmsg->dst->tx_slots));
2093                 txmsg->dst->tx_slots[txmsg->seqno] = NULL;
2094         }
2095 }
2096
2097 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
2098                                  struct drm_dp_sideband_msg_tx *txmsg)
2099 {
2100         mutex_lock(&mgr->qlock);
2101         list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
2102         if (list_is_singular(&mgr->tx_msg_downq))
2103                 process_single_down_tx_qlock(mgr);
2104         mutex_unlock(&mgr->qlock);
2105 }
2106
2107 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
2108                                      struct drm_dp_mst_branch *mstb)
2109 {
2110         int len;
2111         struct drm_dp_sideband_msg_tx *txmsg;
2112         int ret;
2113
2114         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2115         if (!txmsg)
2116                 return;
2117
2118         txmsg->dst = mstb;
2119         len = build_link_address(txmsg);
2120
2121         mstb->link_address_sent = true;
2122         drm_dp_queue_down_tx(mgr, txmsg);
2123
2124         ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2125         if (ret > 0) {
2126                 int i;
2127
2128                 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2129                         DRM_DEBUG_KMS("link address nak received\n");
2130                 } else {
2131                         DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
2132                         for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
2133                                 DRM_DEBUG_KMS("port %d: input %d, pdt: %d, pn: %d, dpcd_rev: %02x, mcs: %d, ddps: %d, ldps %d, sdp %d/%d\n", i,
2134                                        txmsg->reply.u.link_addr.ports[i].input_port,
2135                                        txmsg->reply.u.link_addr.ports[i].peer_device_type,
2136                                        txmsg->reply.u.link_addr.ports[i].port_number,
2137                                        txmsg->reply.u.link_addr.ports[i].dpcd_revision,
2138                                        txmsg->reply.u.link_addr.ports[i].mcs,
2139                                        txmsg->reply.u.link_addr.ports[i].ddps,
2140                                        txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
2141                                        txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
2142                                        txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
2143                         }
2144
2145                         drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
2146
2147                         for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
2148                                 drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
2149                         }
2150                         drm_kms_helper_hotplug_event(mgr->dev);
2151                 }
2152         } else {
2153                 mstb->link_address_sent = false;
2154                 DRM_DEBUG_KMS("link address failed %d\n", ret);
2155         }
2156
2157         kfree(txmsg);
2158 }
2159
2160 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
2161                                            struct drm_dp_mst_branch *mstb,
2162                                            struct drm_dp_mst_port *port)
2163 {
2164         int len;
2165         struct drm_dp_sideband_msg_tx *txmsg;
2166         int ret;
2167
2168         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2169         if (!txmsg)
2170                 return -ENOMEM;
2171
2172         txmsg->dst = mstb;
2173         len = build_enum_path_resources(txmsg, port->port_num);
2174
2175         drm_dp_queue_down_tx(mgr, txmsg);
2176
2177         ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2178         if (ret > 0) {
2179                 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
2180                         DRM_DEBUG_KMS("enum path resources nak received\n");
2181                 } else {
2182                         if (port->port_num != txmsg->reply.u.path_resources.port_number)
2183                                 DRM_ERROR("got incorrect port in response\n");
2184                         DRM_DEBUG_KMS("enum path resources %d: %d %d\n", txmsg->reply.u.path_resources.port_number, txmsg->reply.u.path_resources.full_payload_bw_number,
2185                                txmsg->reply.u.path_resources.avail_payload_bw_number);
2186                         port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
2187                 }
2188         }
2189
2190         kfree(txmsg);
2191         return 0;
2192 }
2193
2194 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
2195 {
2196         if (!mstb->port_parent)
2197                 return NULL;
2198
2199         if (mstb->port_parent->mstb != mstb)
2200                 return mstb->port_parent;
2201
2202         return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
2203 }
2204
2205 /*
2206  * Searches upwards in the topology starting from mstb to try to find the
2207  * closest available parent of mstb that's still connected to the rest of the
2208  * topology. This can be used in order to perform operations like releasing
2209  * payloads, where the branch device which owned the payload may no longer be
2210  * around and thus would require that the payload on the last living relative
2211  * be freed instead.
2212  */
2213 static struct drm_dp_mst_branch *
2214 drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
2215                                         struct drm_dp_mst_branch *mstb,
2216                                         int *port_num)
2217 {
2218         struct drm_dp_mst_branch *rmstb = NULL;
2219         struct drm_dp_mst_port *found_port;
2220
2221         mutex_lock(&mgr->lock);
2222         if (!mgr->mst_primary)
2223                 goto out;
2224
2225         do {
2226                 found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
2227                 if (!found_port)
2228                         break;
2229
2230                 if (drm_dp_mst_topology_try_get_mstb(found_port->parent)) {
2231                         rmstb = found_port->parent;
2232                         *port_num = found_port->port_num;
2233                 } else {
2234                         /* Search again, starting from this parent */
2235                         mstb = found_port->parent;
2236                 }
2237         } while (!rmstb);
2238 out:
2239         mutex_unlock(&mgr->lock);
2240         return rmstb;
2241 }
2242
2243 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
2244                                    struct drm_dp_mst_port *port,
2245                                    int id,
2246                                    int pbn)
2247 {
2248         struct drm_dp_sideband_msg_tx *txmsg;
2249         struct drm_dp_mst_branch *mstb;
2250         int len, ret, port_num;
2251         u8 sinks[DRM_DP_MAX_SDP_STREAMS];
2252         int i;
2253
2254         port_num = port->port_num;
2255         mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2256         if (!mstb) {
2257                 mstb = drm_dp_get_last_connected_port_and_mstb(mgr,
2258                                                                port->parent,
2259                                                                &port_num);
2260
2261                 if (!mstb)
2262                         return -EINVAL;
2263         }
2264
2265         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2266         if (!txmsg) {
2267                 ret = -ENOMEM;
2268                 goto fail_put;
2269         }
2270
2271         for (i = 0; i < port->num_sdp_streams; i++)
2272                 sinks[i] = i;
2273
2274         txmsg->dst = mstb;
2275         len = build_allocate_payload(txmsg, port_num,
2276                                      id,
2277                                      pbn, port->num_sdp_streams, sinks);
2278
2279         drm_dp_queue_down_tx(mgr, txmsg);
2280
2281         /*
2282          * FIXME: there is a small chance that between getting the last
2283          * connected mstb and sending the payload message, the last connected
2284          * mstb could also be removed from the topology. In the future, this
2285          * needs to be fixed by restarting the
2286          * drm_dp_get_last_connected_port_and_mstb() search in the event of a
2287          * timeout if the topology is still connected to the system.
2288          */
2289         ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2290         if (ret > 0) {
2291                 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2292                         ret = -EINVAL;
2293                 else
2294                         ret = 0;
2295         }
2296         kfree(txmsg);
2297 fail_put:
2298         drm_dp_mst_topology_put_mstb(mstb);
2299         return ret;
2300 }
2301
2302 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
2303                                  struct drm_dp_mst_port *port, bool power_up)
2304 {
2305         struct drm_dp_sideband_msg_tx *txmsg;
2306         int len, ret;
2307
2308         port = drm_dp_mst_topology_get_port_validated(mgr, port);
2309         if (!port)
2310                 return -EINVAL;
2311
2312         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2313         if (!txmsg) {
2314                 drm_dp_mst_topology_put_port(port);
2315                 return -ENOMEM;
2316         }
2317
2318         txmsg->dst = port->parent;
2319         len = build_power_updown_phy(txmsg, port->port_num, power_up);
2320         drm_dp_queue_down_tx(mgr, txmsg);
2321
2322         ret = drm_dp_mst_wait_tx_reply(port->parent, txmsg);
2323         if (ret > 0) {
2324                 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2325                         ret = -EINVAL;
2326                 else
2327                         ret = 0;
2328         }
2329         kfree(txmsg);
2330         drm_dp_mst_topology_put_port(port);
2331
2332         return ret;
2333 }
2334 EXPORT_SYMBOL(drm_dp_send_power_updown_phy);
2335
2336 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2337                                        int id,
2338                                        struct drm_dp_payload *payload)
2339 {
2340         int ret;
2341
2342         ret = drm_dp_dpcd_write_payload(mgr, id, payload);
2343         if (ret < 0) {
2344                 payload->payload_state = 0;
2345                 return ret;
2346         }
2347         payload->payload_state = DP_PAYLOAD_LOCAL;
2348         return 0;
2349 }
2350
2351 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2352                                        struct drm_dp_mst_port *port,
2353                                        int id,
2354                                        struct drm_dp_payload *payload)
2355 {
2356         int ret;
2357         ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
2358         if (ret < 0)
2359                 return ret;
2360         payload->payload_state = DP_PAYLOAD_REMOTE;
2361         return ret;
2362 }
2363
2364 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
2365                                         struct drm_dp_mst_port *port,
2366                                         int id,
2367                                         struct drm_dp_payload *payload)
2368 {
2369         DRM_DEBUG_KMS("\n");
2370         /* it's okay for these to fail */
2371         if (port) {
2372                 drm_dp_payload_send_msg(mgr, port, id, 0);
2373         }
2374
2375         drm_dp_dpcd_write_payload(mgr, id, payload);
2376         payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
2377         return 0;
2378 }
2379
2380 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
2381                                         int id,
2382                                         struct drm_dp_payload *payload)
2383 {
2384         payload->payload_state = 0;
2385         return 0;
2386 }
2387
2388 /**
2389  * drm_dp_update_payload_part1() - Execute payload update part 1
2390  * @mgr: manager to use.
2391  *
2392  * This iterates over all proposed virtual channels, and tries to
2393  * allocate space in the link for them. For 0->slots transitions,
2394  * this step just writes the VCPI to the MST device. For slots->0
2395  * transitions, this writes the updated VCPIs and removes the
2396  * remote VC payloads.
2397  *
2398  * after calling this the driver should generate ACT and payload
2399  * packets.
2400  */
2401 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
2402 {
2403         struct drm_dp_payload req_payload;
2404         struct drm_dp_mst_port *port;
2405         int i, j;
2406         int cur_slots = 1;
2407
2408         mutex_lock(&mgr->payload_lock);
2409         for (i = 0; i < mgr->max_payloads; i++) {
2410                 struct drm_dp_vcpi *vcpi = mgr->proposed_vcpis[i];
2411                 struct drm_dp_payload *payload = &mgr->payloads[i];
2412                 bool put_port = false;
2413
2414                 /* solve the current payloads - compare to the hw ones
2415                    - update the hw view */
2416                 req_payload.start_slot = cur_slots;
2417                 if (vcpi) {
2418                         port = container_of(vcpi, struct drm_dp_mst_port,
2419                                             vcpi);
2420
2421                         /* Validated ports don't matter if we're releasing
2422                          * VCPI
2423                          */
2424                         if (vcpi->num_slots) {
2425                                 port = drm_dp_mst_topology_get_port_validated(
2426                                     mgr, port);
2427                                 if (!port) {
2428                                         mutex_unlock(&mgr->payload_lock);
2429                                         return -EINVAL;
2430                                 }
2431                                 put_port = true;
2432                         }
2433
2434                         req_payload.num_slots = vcpi->num_slots;
2435                         req_payload.vcpi = vcpi->vcpi;
2436                 } else {
2437                         port = NULL;
2438                         req_payload.num_slots = 0;
2439                 }
2440
2441                 payload->start_slot = req_payload.start_slot;
2442                 /* work out what is required to happen with this payload */
2443                 if (payload->num_slots != req_payload.num_slots) {
2444
2445                         /* need to push an update for this payload */
2446                         if (req_payload.num_slots) {
2447                                 drm_dp_create_payload_step1(mgr, vcpi->vcpi,
2448                                                             &req_payload);
2449                                 payload->num_slots = req_payload.num_slots;
2450                                 payload->vcpi = req_payload.vcpi;
2451
2452                         } else if (payload->num_slots) {
2453                                 payload->num_slots = 0;
2454                                 drm_dp_destroy_payload_step1(mgr, port,
2455                                                              payload->vcpi,
2456                                                              payload);
2457                                 req_payload.payload_state =
2458                                         payload->payload_state;
2459                                 payload->start_slot = 0;
2460                         }
2461                         payload->payload_state = req_payload.payload_state;
2462                 }
2463                 cur_slots += req_payload.num_slots;
2464
2465                 if (put_port)
2466                         drm_dp_mst_topology_put_port(port);
2467         }
2468
2469         for (i = 0; i < mgr->max_payloads; /* do nothing */) {
2470                 if (mgr->payloads[i].payload_state != DP_PAYLOAD_DELETE_LOCAL) {
2471                         i++;
2472                         continue;
2473                 }
2474
2475                 DRM_DEBUG_KMS("removing payload %d\n", i);
2476                 for (j = i; j < mgr->max_payloads - 1; j++) {
2477                         mgr->payloads[j] = mgr->payloads[j + 1];
2478                         mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
2479
2480                         if (mgr->proposed_vcpis[j] &&
2481                             mgr->proposed_vcpis[j]->num_slots) {
2482                                 set_bit(j + 1, &mgr->payload_mask);
2483                         } else {
2484                                 clear_bit(j + 1, &mgr->payload_mask);
2485                         }
2486                 }
2487
2488                 memset(&mgr->payloads[mgr->max_payloads - 1], 0,
2489                        sizeof(struct drm_dp_payload));
2490                 mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
2491                 clear_bit(mgr->max_payloads, &mgr->payload_mask);
2492         }
2493         mutex_unlock(&mgr->payload_lock);
2494
2495         return 0;
2496 }
2497 EXPORT_SYMBOL(drm_dp_update_payload_part1);
2498
2499 /**
2500  * drm_dp_update_payload_part2() - Execute payload update part 2
2501  * @mgr: manager to use.
2502  *
2503  * This iterates over all proposed virtual channels, and tries to
2504  * allocate space in the link for them. For 0->slots transitions,
2505  * this step writes the remote VC payload commands. For slots->0
2506  * this just resets some internal state.
2507  */
2508 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
2509 {
2510         struct drm_dp_mst_port *port;
2511         int i;
2512         int ret = 0;
2513         mutex_lock(&mgr->payload_lock);
2514         for (i = 0; i < mgr->max_payloads; i++) {
2515
2516                 if (!mgr->proposed_vcpis[i])
2517                         continue;
2518
2519                 port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2520
2521                 DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
2522                 if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
2523                         ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2524                 } else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
2525                         ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
2526                 }
2527                 if (ret) {
2528                         mutex_unlock(&mgr->payload_lock);
2529                         return ret;
2530                 }
2531         }
2532         mutex_unlock(&mgr->payload_lock);
2533         return 0;
2534 }
2535 EXPORT_SYMBOL(drm_dp_update_payload_part2);
2536
2537 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
2538                                  struct drm_dp_mst_port *port,
2539                                  int offset, int size, u8 *bytes)
2540 {
2541         int len;
2542         int ret = 0;
2543         struct drm_dp_sideband_msg_tx *txmsg;
2544         struct drm_dp_mst_branch *mstb;
2545
2546         mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2547         if (!mstb)
2548                 return -EINVAL;
2549
2550         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2551         if (!txmsg) {
2552                 ret = -ENOMEM;
2553                 goto fail_put;
2554         }
2555
2556         len = build_dpcd_read(txmsg, port->port_num, offset, size);
2557         txmsg->dst = port->parent;
2558
2559         drm_dp_queue_down_tx(mgr, txmsg);
2560
2561         ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2562         if (ret < 0)
2563                 goto fail_free;
2564
2565         /* DPCD read should never be NACKed */
2566         if (txmsg->reply.reply_type == 1) {
2567                 DRM_ERROR("mstb %p port %d: DPCD read on addr 0x%x for %d bytes NAKed\n",
2568                           mstb, port->port_num, offset, size);
2569                 ret = -EIO;
2570                 goto fail_free;
2571         }
2572
2573         if (txmsg->reply.u.remote_dpcd_read_ack.num_bytes != size) {
2574                 ret = -EPROTO;
2575                 goto fail_free;
2576         }
2577
2578         ret = min_t(size_t, txmsg->reply.u.remote_dpcd_read_ack.num_bytes,
2579                     size);
2580         memcpy(bytes, txmsg->reply.u.remote_dpcd_read_ack.bytes, ret);
2581
2582 fail_free:
2583         kfree(txmsg);
2584 fail_put:
2585         drm_dp_mst_topology_put_mstb(mstb);
2586
2587         return ret;
2588 }
2589
2590 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
2591                                   struct drm_dp_mst_port *port,
2592                                   int offset, int size, u8 *bytes)
2593 {
2594         int len;
2595         int ret;
2596         struct drm_dp_sideband_msg_tx *txmsg;
2597         struct drm_dp_mst_branch *mstb;
2598
2599         mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
2600         if (!mstb)
2601                 return -EINVAL;
2602
2603         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2604         if (!txmsg) {
2605                 ret = -ENOMEM;
2606                 goto fail_put;
2607         }
2608
2609         len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
2610         txmsg->dst = mstb;
2611
2612         drm_dp_queue_down_tx(mgr, txmsg);
2613
2614         ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
2615         if (ret > 0) {
2616                 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2617                         ret = -EIO;
2618                 else
2619                         ret = 0;
2620         }
2621         kfree(txmsg);
2622 fail_put:
2623         drm_dp_mst_topology_put_mstb(mstb);
2624         return ret;
2625 }
2626
2627 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
2628 {
2629         struct drm_dp_sideband_msg_reply_body reply;
2630
2631         reply.reply_type = DP_SIDEBAND_REPLY_ACK;
2632         reply.req_type = req_type;
2633         drm_dp_encode_sideband_reply(&reply, msg);
2634         return 0;
2635 }
2636
2637 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
2638                                     struct drm_dp_mst_branch *mstb,
2639                                     int req_type, int seqno, bool broadcast)
2640 {
2641         struct drm_dp_sideband_msg_tx *txmsg;
2642
2643         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
2644         if (!txmsg)
2645                 return -ENOMEM;
2646
2647         txmsg->dst = mstb;
2648         txmsg->seqno = seqno;
2649         drm_dp_encode_up_ack_reply(txmsg, req_type);
2650
2651         mutex_lock(&mgr->qlock);
2652
2653         process_single_up_tx_qlock(mgr, txmsg);
2654
2655         mutex_unlock(&mgr->qlock);
2656
2657         kfree(txmsg);
2658         return 0;
2659 }
2660
2661 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
2662                                      int dp_link_count,
2663                                      int *out)
2664 {
2665         switch (dp_link_bw) {
2666         default:
2667                 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2668                               dp_link_bw, dp_link_count);
2669                 return false;
2670
2671         case DP_LINK_BW_1_62:
2672                 *out = 3 * dp_link_count;
2673                 break;
2674         case DP_LINK_BW_2_7:
2675                 *out = 5 * dp_link_count;
2676                 break;
2677         case DP_LINK_BW_5_4:
2678                 *out = 10 * dp_link_count;
2679                 break;
2680         case DP_LINK_BW_8_1:
2681                 *out = 15 * dp_link_count;
2682                 break;
2683         }
2684         return true;
2685 }
2686
2687 /**
2688  * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2689  * @mgr: manager to set state for
2690  * @mst_state: true to enable MST on this connector - false to disable.
2691  *
2692  * This is called by the driver when it detects an MST capable device plugged
2693  * into a DP MST capable port, or when a DP MST capable device is unplugged.
2694  */
2695 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2696 {
2697         int ret = 0;
2698         struct drm_dp_mst_branch *mstb = NULL;
2699
2700         mutex_lock(&mgr->payload_lock);
2701         mutex_lock(&mgr->lock);
2702         if (mst_state == mgr->mst_state)
2703                 goto out_unlock;
2704
2705         mgr->mst_state = mst_state;
2706         /* set the device into MST mode */
2707         if (mst_state) {
2708                 WARN_ON(mgr->mst_primary);
2709
2710                 /* get dpcd info */
2711                 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2712                 if (ret != DP_RECEIVER_CAP_SIZE) {
2713                         DRM_DEBUG_KMS("failed to read DPCD\n");
2714                         goto out_unlock;
2715                 }
2716
2717                 if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2718                                               mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2719                                               &mgr->pbn_div)) {
2720                         ret = -EINVAL;
2721                         goto out_unlock;
2722                 }
2723
2724                 /* add initial branch device at LCT 1 */
2725                 mstb = drm_dp_add_mst_branch_device(1, NULL);
2726                 if (mstb == NULL) {
2727                         ret = -ENOMEM;
2728                         goto out_unlock;
2729                 }
2730                 mstb->mgr = mgr;
2731
2732                 /* give this the main reference */
2733                 mgr->mst_primary = mstb;
2734                 drm_dp_mst_topology_get_mstb(mgr->mst_primary);
2735
2736                 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2737                                                          DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2738                 if (ret < 0) {
2739                         goto out_unlock;
2740                 }
2741
2742                 {
2743                         struct drm_dp_payload reset_pay;
2744                         reset_pay.start_slot = 0;
2745                         reset_pay.num_slots = 0x3f;
2746                         drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2747                 }
2748
2749                 queue_work(system_long_wq, &mgr->work);
2750
2751                 ret = 0;
2752         } else {
2753                 /* disable MST on the device */
2754                 mstb = mgr->mst_primary;
2755                 mgr->mst_primary = NULL;
2756                 /* this can fail if the device is gone */
2757                 drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2758                 ret = 0;
2759                 memset(mgr->payloads, 0,
2760                        mgr->max_payloads * sizeof(mgr->payloads[0]));
2761                 memset(mgr->proposed_vcpis, 0,
2762                        mgr->max_payloads * sizeof(mgr->proposed_vcpis[0]));
2763                 mgr->payload_mask = 0;
2764                 set_bit(0, &mgr->payload_mask);
2765                 mgr->vcpi_mask = 0;
2766         }
2767
2768 out_unlock:
2769         mutex_unlock(&mgr->lock);
2770         mutex_unlock(&mgr->payload_lock);
2771         if (mstb)
2772                 drm_dp_mst_topology_put_mstb(mstb);
2773         return ret;
2774
2775 }
2776 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2777
2778 /**
2779  * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2780  * @mgr: manager to suspend
2781  *
2782  * This function tells the MST device that we can't handle UP messages
2783  * anymore. This should stop it from sending any since we are suspended.
2784  */
2785 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2786 {
2787         mutex_lock(&mgr->lock);
2788         drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2789                            DP_MST_EN | DP_UPSTREAM_IS_SRC);
2790         mutex_unlock(&mgr->lock);
2791         flush_work(&mgr->work);
2792         flush_work(&mgr->destroy_connector_work);
2793 }
2794 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2795
2796 /**
2797  * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2798  * @mgr: manager to resume
2799  *
2800  * This will fetch DPCD and see if the device is still there,
2801  * if it is, it will rewrite the MSTM control bits, and return.
2802  *
2803  * if the device fails this returns -1, and the driver should do
2804  * a full MST reprobe, in case we were undocked.
2805  */
2806 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2807 {
2808         int ret = 0;
2809
2810         mutex_lock(&mgr->lock);
2811
2812         if (mgr->mst_primary) {
2813                 int sret;
2814                 u8 guid[16];
2815
2816                 sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2817                 if (sret != DP_RECEIVER_CAP_SIZE) {
2818                         DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2819                         ret = -1;
2820                         goto out_unlock;
2821                 }
2822
2823                 ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2824                                          DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2825                 if (ret < 0) {
2826                         DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2827                         ret = -1;
2828                         goto out_unlock;
2829                 }
2830
2831                 /* Some hubs forget their guids after they resume */
2832                 sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2833                 if (sret != 16) {
2834                         DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2835                         ret = -1;
2836                         goto out_unlock;
2837                 }
2838                 drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2839
2840                 ret = 0;
2841         } else
2842                 ret = -1;
2843
2844 out_unlock:
2845         mutex_unlock(&mgr->lock);
2846         return ret;
2847 }
2848 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2849
2850 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2851 {
2852         int len;
2853         u8 replyblock[32];
2854         int replylen, origlen, curreply;
2855         int ret;
2856         struct drm_dp_sideband_msg_rx *msg;
2857         int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2858         msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2859
2860         len = min(mgr->max_dpcd_transaction_bytes, 16);
2861         ret = drm_dp_dpcd_read(mgr->aux, basereg,
2862                                replyblock, len);
2863         if (ret != len) {
2864                 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2865                 return false;
2866         }
2867         ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2868         if (!ret) {
2869                 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2870                 return false;
2871         }
2872         replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2873
2874         origlen = replylen;
2875         replylen -= len;
2876         curreply = len;
2877         while (replylen > 0) {
2878                 len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2879                 ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2880                                     replyblock, len);
2881                 if (ret != len) {
2882                         DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2883                                       len, ret);
2884                         return false;
2885                 }
2886
2887                 ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2888                 if (!ret) {
2889                         DRM_DEBUG_KMS("failed to build sideband msg\n");
2890                         return false;
2891                 }
2892
2893                 curreply += len;
2894                 replylen -= len;
2895         }
2896         return true;
2897 }
2898
2899 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2900 {
2901         int ret = 0;
2902
2903         if (!drm_dp_get_one_sb_msg(mgr, false)) {
2904                 memset(&mgr->down_rep_recv, 0,
2905                        sizeof(struct drm_dp_sideband_msg_rx));
2906                 return 0;
2907         }
2908
2909         if (mgr->down_rep_recv.have_eomt) {
2910                 struct drm_dp_sideband_msg_tx *txmsg;
2911                 struct drm_dp_mst_branch *mstb;
2912                 int slot = -1;
2913                 mstb = drm_dp_get_mst_branch_device(mgr,
2914                                                     mgr->down_rep_recv.initial_hdr.lct,
2915                                                     mgr->down_rep_recv.initial_hdr.rad);
2916
2917                 if (!mstb) {
2918                         DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2919                         memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2920                         return 0;
2921                 }
2922
2923                 /* find the message */
2924                 slot = mgr->down_rep_recv.initial_hdr.seqno;
2925                 mutex_lock(&mgr->qlock);
2926                 txmsg = mstb->tx_slots[slot];
2927                 /* remove from slots */
2928                 mutex_unlock(&mgr->qlock);
2929
2930                 if (!txmsg) {
2931                         DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2932                                mstb,
2933                                mgr->down_rep_recv.initial_hdr.seqno,
2934                                mgr->down_rep_recv.initial_hdr.lct,
2935                                       mgr->down_rep_recv.initial_hdr.rad[0],
2936                                       mgr->down_rep_recv.msg[0]);
2937                         drm_dp_mst_topology_put_mstb(mstb);
2938                         memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2939                         return 0;
2940                 }
2941
2942                 drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2943
2944                 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK)
2945                         DRM_DEBUG_KMS("Got NAK reply: req 0x%02x (%s), reason 0x%02x (%s), nak data 0x%02x\n",
2946                                       txmsg->reply.req_type,
2947                                       drm_dp_mst_req_type_str(txmsg->reply.req_type),
2948                                       txmsg->reply.u.nak.reason,
2949                                       drm_dp_mst_nak_reason_str(txmsg->reply.u.nak.reason),
2950                                       txmsg->reply.u.nak.nak_data);
2951
2952                 memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2953                 drm_dp_mst_topology_put_mstb(mstb);
2954
2955                 mutex_lock(&mgr->qlock);
2956                 txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2957                 mstb->tx_slots[slot] = NULL;
2958                 mutex_unlock(&mgr->qlock);
2959
2960                 wake_up_all(&mgr->tx_waitq);
2961         }
2962         return ret;
2963 }
2964
2965 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2966 {
2967         int ret = 0;
2968
2969         if (!drm_dp_get_one_sb_msg(mgr, true)) {
2970                 memset(&mgr->up_req_recv, 0,
2971                        sizeof(struct drm_dp_sideband_msg_rx));
2972                 return 0;
2973         }
2974
2975         if (mgr->up_req_recv.have_eomt) {
2976                 struct drm_dp_sideband_msg_req_body msg;
2977                 struct drm_dp_mst_branch *mstb = NULL;
2978                 bool seqno;
2979
2980                 if (!mgr->up_req_recv.initial_hdr.broadcast) {
2981                         mstb = drm_dp_get_mst_branch_device(mgr,
2982                                                             mgr->up_req_recv.initial_hdr.lct,
2983                                                             mgr->up_req_recv.initial_hdr.rad);
2984                         if (!mstb) {
2985                                 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2986                                 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2987                                 return 0;
2988                         }
2989                 }
2990
2991                 seqno = mgr->up_req_recv.initial_hdr.seqno;
2992                 drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2993
2994                 if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2995                         drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2996
2997                         if (!mstb)
2998                                 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2999
3000                         if (!mstb) {
3001                                 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
3002                                 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3003                                 return 0;
3004                         }
3005
3006                         drm_dp_update_port(mstb, &msg.u.conn_stat);
3007
3008                         DRM_DEBUG_KMS("Got CSN: pn: %d ldps:%d ddps: %d mcs: %d ip: %d pdt: %d\n", msg.u.conn_stat.port_number, msg.u.conn_stat.legacy_device_plug_status, msg.u.conn_stat.displayport_device_plug_status, msg.u.conn_stat.message_capability_status, msg.u.conn_stat.input_port, msg.u.conn_stat.peer_device_type);
3009                         drm_kms_helper_hotplug_event(mgr->dev);
3010
3011                 } else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
3012                         drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
3013                         if (!mstb)
3014                                 mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
3015
3016                         if (!mstb) {
3017                                 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
3018                                 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3019                                 return 0;
3020                         }
3021
3022                         DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
3023                 }
3024
3025                 if (mstb)
3026                         drm_dp_mst_topology_put_mstb(mstb);
3027
3028                 memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
3029         }
3030         return ret;
3031 }
3032
3033 /**
3034  * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
3035  * @mgr: manager to notify irq for.
3036  * @esi: 4 bytes from SINK_COUNT_ESI
3037  * @handled: whether the hpd interrupt was consumed or not
3038  *
3039  * This should be called from the driver when it detects a short IRQ,
3040  * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
3041  * topology manager will process the sideband messages received as a result
3042  * of this.
3043  */
3044 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
3045 {
3046         int ret = 0;
3047         int sc;
3048         *handled = false;
3049         sc = esi[0] & 0x3f;
3050
3051         if (sc != mgr->sink_count) {
3052                 mgr->sink_count = sc;
3053                 *handled = true;
3054         }
3055
3056         if (esi[1] & DP_DOWN_REP_MSG_RDY) {
3057                 ret = drm_dp_mst_handle_down_rep(mgr);
3058                 *handled = true;
3059         }
3060
3061         if (esi[1] & DP_UP_REQ_MSG_RDY) {
3062                 ret |= drm_dp_mst_handle_up_req(mgr);
3063                 *handled = true;
3064         }
3065
3066         drm_dp_mst_kick_tx(mgr);
3067         return ret;
3068 }
3069 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
3070
3071 /**
3072  * drm_dp_mst_detect_port() - get connection status for an MST port
3073  * @connector: DRM connector for this port
3074  * @mgr: manager for this port
3075  * @port: unverified pointer to a port
3076  *
3077  * This returns the current connection state for a port. It validates the
3078  * port pointer still exists so the caller doesn't require a reference
3079  */
3080 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
3081                                                  struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3082 {
3083         enum drm_connector_status status = connector_status_disconnected;
3084
3085         /* we need to search for the port in the mgr in case it's gone */
3086         port = drm_dp_mst_topology_get_port_validated(mgr, port);
3087         if (!port)
3088                 return connector_status_disconnected;
3089
3090         if (!port->ddps)
3091                 goto out;
3092
3093         switch (port->pdt) {
3094         case DP_PEER_DEVICE_NONE:
3095         case DP_PEER_DEVICE_MST_BRANCHING:
3096                 break;
3097
3098         case DP_PEER_DEVICE_SST_SINK:
3099                 status = connector_status_connected;
3100                 /* for logical ports - cache the EDID */
3101                 if (port->port_num >= 8 && !port->cached_edid) {
3102                         port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
3103                 }
3104                 break;
3105         case DP_PEER_DEVICE_DP_LEGACY_CONV:
3106                 if (port->ldps)
3107                         status = connector_status_connected;
3108                 break;
3109         }
3110 out:
3111         drm_dp_mst_topology_put_port(port);
3112         return status;
3113 }
3114 EXPORT_SYMBOL(drm_dp_mst_detect_port);
3115
3116 /**
3117  * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
3118  * @mgr: manager for this port
3119  * @port: unverified pointer to a port.
3120  *
3121  * This returns whether the port supports audio or not.
3122  */
3123 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
3124                                         struct drm_dp_mst_port *port)
3125 {
3126         bool ret = false;
3127
3128         port = drm_dp_mst_topology_get_port_validated(mgr, port);
3129         if (!port)
3130                 return ret;
3131         ret = port->has_audio;
3132         drm_dp_mst_topology_put_port(port);
3133         return ret;
3134 }
3135 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
3136
3137 /**
3138  * drm_dp_mst_get_edid() - get EDID for an MST port
3139  * @connector: toplevel connector to get EDID for
3140  * @mgr: manager for this port
3141  * @port: unverified pointer to a port.
3142  *
3143  * This returns an EDID for the port connected to a connector,
3144  * It validates the pointer still exists so the caller doesn't require a
3145  * reference.
3146  */
3147 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3148 {
3149         struct edid *edid = NULL;
3150
3151         /* we need to search for the port in the mgr in case it's gone */
3152         port = drm_dp_mst_topology_get_port_validated(mgr, port);
3153         if (!port)
3154                 return NULL;
3155
3156         if (port->cached_edid)
3157                 edid = drm_edid_duplicate(port->cached_edid);
3158         else {
3159                 edid = drm_get_edid(connector, &port->aux.ddc);
3160         }
3161         port->has_audio = drm_detect_monitor_audio(edid);
3162         drm_dp_mst_topology_put_port(port);
3163         return edid;
3164 }
3165 EXPORT_SYMBOL(drm_dp_mst_get_edid);
3166
3167 /**
3168  * drm_dp_find_vcpi_slots() - Find VCPI slots for this PBN value
3169  * @mgr: manager to use
3170  * @pbn: payload bandwidth to convert into slots.
3171  *
3172  * Calculate the number of VCPI slots that will be required for the given PBN
3173  * value. This function is deprecated, and should not be used in atomic
3174  * drivers.
3175  *
3176  * RETURNS:
3177  * The total slots required for this port, or error.
3178  */
3179 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
3180                            int pbn)
3181 {
3182         int num_slots;
3183
3184         num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3185
3186         /* max. time slots - one slot for MTP header */
3187         if (num_slots > 63)
3188                 return -ENOSPC;
3189         return num_slots;
3190 }
3191 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
3192
3193 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3194                             struct drm_dp_vcpi *vcpi, int pbn, int slots)
3195 {
3196         int ret;
3197
3198         /* max. time slots - one slot for MTP header */
3199         if (slots > 63)
3200                 return -ENOSPC;
3201
3202         vcpi->pbn = pbn;
3203         vcpi->aligned_pbn = slots * mgr->pbn_div;
3204         vcpi->num_slots = slots;
3205
3206         ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
3207         if (ret < 0)
3208                 return ret;
3209         return 0;
3210 }
3211
3212 /**
3213  * drm_dp_atomic_find_vcpi_slots() - Find and add VCPI slots to the state
3214  * @state: global atomic state
3215  * @mgr: MST topology manager for the port
3216  * @port: port to find vcpi slots for
3217  * @pbn: bandwidth required for the mode in PBN
3218  *
3219  * Allocates VCPI slots to @port, replacing any previous VCPI allocations it
3220  * may have had. Any atomic drivers which support MST must call this function
3221  * in their &drm_encoder_helper_funcs.atomic_check() callback to change the
3222  * current VCPI allocation for the new state, but only when
3223  * &drm_crtc_state.mode_changed or &drm_crtc_state.connectors_changed is set
3224  * to ensure compatibility with userspace applications that still use the
3225  * legacy modesetting UAPI.
3226  *
3227  * Allocations set by this function are not checked against the bandwidth
3228  * restraints of @mgr until the driver calls drm_dp_mst_atomic_check().
3229  *
3230  * Additionally, it is OK to call this function multiple times on the same
3231  * @port as needed. It is not OK however, to call this function and
3232  * drm_dp_atomic_release_vcpi_slots() in the same atomic check phase.
3233  *
3234  * See also:
3235  * drm_dp_atomic_release_vcpi_slots()
3236  * drm_dp_mst_atomic_check()
3237  *
3238  * Returns:
3239  * Total slots in the atomic state assigned for this port, or a negative error
3240  * code if the port no longer exists
3241  */
3242 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
3243                                   struct drm_dp_mst_topology_mgr *mgr,
3244                                   struct drm_dp_mst_port *port, int pbn)
3245 {
3246         struct drm_dp_mst_topology_state *topology_state;
3247         struct drm_dp_vcpi_allocation *pos, *vcpi = NULL;
3248         int prev_slots, req_slots, ret;
3249
3250         topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3251         if (IS_ERR(topology_state))
3252                 return PTR_ERR(topology_state);
3253
3254         /* Find the current allocation for this port, if any */
3255         list_for_each_entry(pos, &topology_state->vcpis, next) {
3256                 if (pos->port == port) {
3257                         vcpi = pos;
3258                         prev_slots = vcpi->vcpi;
3259
3260                         /*
3261                          * This should never happen, unless the driver tries
3262                          * releasing and allocating the same VCPI allocation,
3263                          * which is an error
3264                          */
3265                         if (WARN_ON(!prev_slots)) {
3266                                 DRM_ERROR("cannot allocate and release VCPI on [MST PORT:%p] in the same state\n",
3267                                           port);
3268                                 return -EINVAL;
3269                         }
3270
3271                         break;
3272                 }
3273         }
3274         if (!vcpi)
3275                 prev_slots = 0;
3276
3277         req_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
3278
3279         DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] [MST PORT:%p] VCPI %d -> %d\n",
3280                          port->connector->base.id, port->connector->name,
3281                          port, prev_slots, req_slots);
3282
3283         /* Add the new allocation to the state */
3284         if (!vcpi) {
3285                 vcpi = kzalloc(sizeof(*vcpi), GFP_KERNEL);
3286                 if (!vcpi)
3287                         return -ENOMEM;
3288
3289                 drm_dp_mst_get_port_malloc(port);
3290                 vcpi->port = port;
3291                 list_add(&vcpi->next, &topology_state->vcpis);
3292         }
3293         vcpi->vcpi = req_slots;
3294
3295         ret = req_slots;
3296         return ret;
3297 }
3298 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots);
3299
3300 /**
3301  * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
3302  * @state: global atomic state
3303  * @mgr: MST topology manager for the port
3304  * @port: The port to release the VCPI slots from
3305  *
3306  * Releases any VCPI slots that have been allocated to a port in the atomic
3307  * state. Any atomic drivers which support MST must call this function in
3308  * their &drm_connector_helper_funcs.atomic_check() callback when the
3309  * connector will no longer have VCPI allocated (e.g. because its CRTC was
3310  * removed) when it had VCPI allocated in the previous atomic state.
3311  *
3312  * It is OK to call this even if @port has been removed from the system.
3313  * Additionally, it is OK to call this function multiple times on the same
3314  * @port as needed. It is not OK however, to call this function and
3315  * drm_dp_atomic_find_vcpi_slots() on the same @port in a single atomic check
3316  * phase.
3317  *
3318  * See also:
3319  * drm_dp_atomic_find_vcpi_slots()
3320  * drm_dp_mst_atomic_check()
3321  *
3322  * Returns:
3323  * 0 if all slots for this port were added back to
3324  * &drm_dp_mst_topology_state.avail_slots or negative error code
3325  */
3326 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
3327                                      struct drm_dp_mst_topology_mgr *mgr,
3328                                      struct drm_dp_mst_port *port)
3329 {
3330         struct drm_dp_mst_topology_state *topology_state;
3331         struct drm_dp_vcpi_allocation *pos;
3332         bool found = false;
3333
3334         topology_state = drm_atomic_get_mst_topology_state(state, mgr);
3335         if (IS_ERR(topology_state))
3336                 return PTR_ERR(topology_state);
3337
3338         list_for_each_entry(pos, &topology_state->vcpis, next) {
3339                 if (pos->port == port) {
3340                         found = true;
3341                         break;
3342                 }
3343         }
3344         if (WARN_ON(!found)) {
3345                 DRM_ERROR("no VCPI for [MST PORT:%p] found in mst state %p\n",
3346                           port, &topology_state->base);
3347                 return -EINVAL;
3348         }
3349
3350         DRM_DEBUG_ATOMIC("[MST PORT:%p] VCPI %d -> 0\n", port, pos->vcpi);
3351         if (pos->vcpi) {
3352                 drm_dp_mst_put_port_malloc(port);
3353                 pos->vcpi = 0;
3354         }
3355
3356         return 0;
3357 }
3358 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots);
3359
3360 /**
3361  * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
3362  * @mgr: manager for this port
3363  * @port: port to allocate a virtual channel for.
3364  * @pbn: payload bandwidth number to request
3365  * @slots: returned number of slots for this PBN.
3366  */
3367 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3368                               struct drm_dp_mst_port *port, int pbn, int slots)
3369 {
3370         int ret;
3371
3372         port = drm_dp_mst_topology_get_port_validated(mgr, port);
3373         if (!port)
3374                 return false;
3375
3376         if (slots < 0)
3377                 return false;
3378
3379         if (port->vcpi.vcpi > 0) {
3380                 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n",
3381                               port->vcpi.vcpi, port->vcpi.pbn, pbn);
3382                 if (pbn == port->vcpi.pbn) {
3383                         drm_dp_mst_topology_put_port(port);
3384                         return true;
3385                 }
3386         }
3387
3388         ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn, slots);
3389         if (ret) {
3390                 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
3391                               DIV_ROUND_UP(pbn, mgr->pbn_div), ret);
3392                 goto out;
3393         }
3394         DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
3395                       pbn, port->vcpi.num_slots);
3396
3397         /* Keep port allocated until its payload has been removed */
3398         drm_dp_mst_get_port_malloc(port);
3399         drm_dp_mst_topology_put_port(port);
3400         return true;
3401 out:
3402         return false;
3403 }
3404 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
3405
3406 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3407 {
3408         int slots = 0;
3409         port = drm_dp_mst_topology_get_port_validated(mgr, port);
3410         if (!port)
3411                 return slots;
3412
3413         slots = port->vcpi.num_slots;
3414         drm_dp_mst_topology_put_port(port);
3415         return slots;
3416 }
3417 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
3418
3419 /**
3420  * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
3421  * @mgr: manager for this port
3422  * @port: unverified pointer to a port.
3423  *
3424  * This just resets the number of slots for the ports VCPI for later programming.
3425  */
3426 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
3427 {
3428         /*
3429          * A port with VCPI will remain allocated until its VCPI is
3430          * released, no verified ref needed
3431          */
3432
3433         port->vcpi.num_slots = 0;
3434 }
3435 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
3436
3437 /**
3438  * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
3439  * @mgr: manager for this port
3440  * @port: port to deallocate vcpi for
3441  *
3442  * This can be called unconditionally, regardless of whether
3443  * drm_dp_mst_allocate_vcpi() succeeded or not.
3444  */
3445 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
3446                                 struct drm_dp_mst_port *port)
3447 {
3448         if (!port->vcpi.vcpi)
3449                 return;
3450
3451         drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
3452         port->vcpi.num_slots = 0;
3453         port->vcpi.pbn = 0;
3454         port->vcpi.aligned_pbn = 0;
3455         port->vcpi.vcpi = 0;
3456         drm_dp_mst_put_port_malloc(port);
3457 }
3458 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
3459
3460 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
3461                                      int id, struct drm_dp_payload *payload)
3462 {
3463         u8 payload_alloc[3], status;
3464         int ret;
3465         int retries = 0;
3466
3467         drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
3468                            DP_PAYLOAD_TABLE_UPDATED);
3469
3470         payload_alloc[0] = id;
3471         payload_alloc[1] = payload->start_slot;
3472         payload_alloc[2] = payload->num_slots;
3473
3474         ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
3475         if (ret != 3) {
3476                 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
3477                 goto fail;
3478         }
3479
3480 retry:
3481         ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3482         if (ret < 0) {
3483                 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
3484                 goto fail;
3485         }
3486
3487         if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
3488                 retries++;
3489                 if (retries < 20) {
3490                         usleep_range(10000, 20000);
3491                         goto retry;
3492                 }
3493                 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
3494                 ret = -EINVAL;
3495                 goto fail;
3496         }
3497         ret = 0;
3498 fail:
3499         return ret;
3500 }
3501
3502 static int do_get_act_status(struct drm_dp_aux *aux)
3503 {
3504         int ret;
3505         u8 status;
3506
3507         ret = drm_dp_dpcd_readb(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
3508         if (ret < 0)
3509                 return ret;
3510
3511         return status;
3512 }
3513
3514 /**
3515  * drm_dp_check_act_status() - Check ACT handled status.
3516  * @mgr: manager to use
3517  *
3518  * Check the payload status bits in the DPCD for ACT handled completion.
3519  */
3520 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
3521 {
3522         /*
3523          * There doesn't seem to be any recommended retry count or timeout in
3524          * the MST specification. Since some hubs have been observed to take
3525          * over 1 second to update their payload allocations under certain
3526          * conditions, we use a rather large timeout value.
3527          */
3528         const int timeout_ms = 3000;
3529         int ret, status;
3530
3531         ret = readx_poll_timeout(do_get_act_status, mgr->aux, status,
3532                                  status & DP_PAYLOAD_ACT_HANDLED || status < 0,
3533                                  200, timeout_ms * USEC_PER_MSEC);
3534         if (ret < 0 && status >= 0) {
3535                 DRM_DEBUG_KMS("Failed to get ACT after %dms, last status: %02x\n",
3536                               timeout_ms, status);
3537                 return -EINVAL;
3538         } else if (status < 0) {
3539                 DRM_DEBUG_KMS("Failed to read payload table status: %d\n",
3540                               status);
3541                 return status;
3542         }
3543
3544         return 0;
3545 }
3546 EXPORT_SYMBOL(drm_dp_check_act_status);
3547
3548 /**
3549  * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
3550  * @clock: dot clock for the mode
3551  * @bpp: bpp for the mode.
3552  *
3553  * This uses the formula in the spec to calculate the PBN value for a mode.
3554  */
3555 int drm_dp_calc_pbn_mode(int clock, int bpp)
3556 {
3557         u64 kbps;
3558         s64 peak_kbps;
3559         u32 numerator;
3560         u32 denominator;
3561
3562         kbps = clock * bpp;
3563
3564         /*
3565          * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
3566          * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
3567          * common multiplier to render an integer PBN for all link rate/lane
3568          * counts combinations
3569          * calculate
3570          * peak_kbps *= (1006/1000)
3571          * peak_kbps *= (64/54)
3572          * peak_kbps *= 8    convert to bytes
3573          */
3574
3575         numerator = 64 * 1006;
3576         denominator = 54 * 8 * 1000 * 1000;
3577
3578         kbps *= numerator;
3579         peak_kbps = drm_fixp_from_fraction(kbps, denominator);
3580
3581         return drm_fixp2int_ceil(peak_kbps);
3582 }
3583 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
3584
3585 static int test_calc_pbn_mode(void)
3586 {
3587         int ret;
3588         ret = drm_dp_calc_pbn_mode(154000, 30);
3589         if (ret != 689) {
3590                 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3591                                 154000, 30, 689, ret);
3592                 return -EINVAL;
3593         }
3594         ret = drm_dp_calc_pbn_mode(234000, 30);
3595         if (ret != 1047) {
3596                 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3597                                 234000, 30, 1047, ret);
3598                 return -EINVAL;
3599         }
3600         ret = drm_dp_calc_pbn_mode(297000, 24);
3601         if (ret != 1063) {
3602                 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
3603                                 297000, 24, 1063, ret);
3604                 return -EINVAL;
3605         }
3606         return 0;
3607 }
3608
3609 /* we want to kick the TX after we've ack the up/down IRQs. */
3610 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
3611 {
3612         queue_work(system_long_wq, &mgr->tx_work);
3613 }
3614
3615 static void drm_dp_mst_dump_mstb(struct seq_file *m,
3616                                  struct drm_dp_mst_branch *mstb)
3617 {
3618         struct drm_dp_mst_port *port;
3619         int tabs = mstb->lct;
3620         char prefix[10];
3621         int i;
3622
3623         for (i = 0; i < tabs; i++)
3624                 prefix[i] = '\t';
3625         prefix[i] = '\0';
3626
3627         seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
3628         list_for_each_entry(port, &mstb->ports, next) {
3629                 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);
3630                 if (port->mstb)
3631                         drm_dp_mst_dump_mstb(m, port->mstb);
3632         }
3633 }
3634
3635 #define DP_PAYLOAD_TABLE_SIZE           64
3636
3637 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
3638                                   char *buf)
3639 {
3640         int i;
3641
3642         for (i = 0; i < DP_PAYLOAD_TABLE_SIZE; i += 16) {
3643                 if (drm_dp_dpcd_read(mgr->aux,
3644                                      DP_PAYLOAD_TABLE_UPDATE_STATUS + i,
3645                                      &buf[i], 16) != 16)
3646                         return false;
3647         }
3648         return true;
3649 }
3650
3651 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
3652                                struct drm_dp_mst_port *port, char *name,
3653                                int namelen)
3654 {
3655         struct edid *mst_edid;
3656
3657         mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
3658         drm_edid_get_monitor_name(mst_edid, name, namelen);
3659 }
3660
3661 /**
3662  * drm_dp_mst_dump_topology(): dump topology to seq file.
3663  * @m: seq_file to dump output to
3664  * @mgr: manager to dump current topology for.
3665  *
3666  * helper to dump MST topology to a seq file for debugfs.
3667  */
3668 void drm_dp_mst_dump_topology(struct seq_file *m,
3669                               struct drm_dp_mst_topology_mgr *mgr)
3670 {
3671         int i;
3672         struct drm_dp_mst_port *port;
3673
3674         mutex_lock(&mgr->lock);
3675         if (mgr->mst_primary)
3676                 drm_dp_mst_dump_mstb(m, mgr->mst_primary);
3677
3678         /* dump VCPIs */
3679         mutex_unlock(&mgr->lock);
3680
3681         mutex_lock(&mgr->payload_lock);
3682         seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
3683                 mgr->max_payloads);
3684
3685         for (i = 0; i < mgr->max_payloads; i++) {
3686                 if (mgr->proposed_vcpis[i]) {
3687                         char name[14];
3688
3689                         port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
3690                         fetch_monitor_name(mgr, port, name, sizeof(name));
3691                         seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
3692                                    port->port_num, port->vcpi.vcpi,
3693                                    port->vcpi.num_slots,
3694                                    (*name != 0) ? name :  "Unknown");
3695                 } else
3696                         seq_printf(m, "vcpi %d:unused\n", i);
3697         }
3698         for (i = 0; i < mgr->max_payloads; i++) {
3699                 seq_printf(m, "payload %d: %d, %d, %d\n",
3700                            i,
3701                            mgr->payloads[i].payload_state,
3702                            mgr->payloads[i].start_slot,
3703                            mgr->payloads[i].num_slots);
3704
3705
3706         }
3707         mutex_unlock(&mgr->payload_lock);
3708
3709         mutex_lock(&mgr->lock);
3710         if (mgr->mst_primary) {
3711                 u8 buf[DP_PAYLOAD_TABLE_SIZE];
3712                 int ret;
3713
3714                 ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
3715                 seq_printf(m, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE, buf);
3716                 ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
3717                 seq_printf(m, "faux/mst: %*ph\n", 2, buf);
3718                 ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
3719                 seq_printf(m, "mst ctrl: %*ph\n", 1, buf);
3720
3721                 /* dump the standard OUI branch header */
3722                 ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
3723                 seq_printf(m, "branch oui: %*phN devid: ", 3, buf);
3724                 for (i = 0x3; i < 0x8 && buf[i]; i++)
3725                         seq_printf(m, "%c", buf[i]);
3726                 seq_printf(m, " revision: hw: %x.%x sw: %x.%x\n",
3727                            buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
3728                 if (dump_dp_payload_table(mgr, buf))
3729                         seq_printf(m, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE, buf);
3730         }
3731
3732         mutex_unlock(&mgr->lock);
3733
3734 }
3735 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
3736
3737 static void drm_dp_tx_work(struct work_struct *work)
3738 {
3739         struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
3740
3741         mutex_lock(&mgr->qlock);
3742         if (!list_empty(&mgr->tx_msg_downq))
3743                 process_single_down_tx_qlock(mgr);
3744         mutex_unlock(&mgr->qlock);
3745 }
3746
3747 static void drm_dp_destroy_connector_work(struct work_struct *work)
3748 {
3749         struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
3750         struct drm_dp_mst_port *port;
3751         bool send_hotplug = false;
3752         /*
3753          * Not a regular list traverse as we have to drop the destroy
3754          * connector lock before destroying the connector, to avoid AB->BA
3755          * ordering between this lock and the config mutex.
3756          */
3757         for (;;) {
3758                 mutex_lock(&mgr->destroy_connector_lock);
3759                 port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
3760                 if (!port) {
3761                         mutex_unlock(&mgr->destroy_connector_lock);
3762                         break;
3763                 }
3764                 list_del(&port->next);
3765                 mutex_unlock(&mgr->destroy_connector_lock);
3766
3767                 INIT_LIST_HEAD(&port->next);
3768
3769                 mgr->cbs->destroy_connector(mgr, port->connector);
3770
3771                 drm_dp_port_teardown_pdt(port, port->pdt);
3772                 port->pdt = DP_PEER_DEVICE_NONE;
3773
3774                 drm_dp_mst_put_port_malloc(port);
3775                 send_hotplug = true;
3776         }
3777         if (send_hotplug)
3778                 drm_kms_helper_hotplug_event(mgr->dev);
3779 }
3780
3781 static struct drm_private_state *
3782 drm_dp_mst_duplicate_state(struct drm_private_obj *obj)
3783 {
3784         struct drm_dp_mst_topology_state *state, *old_state =
3785                 to_dp_mst_topology_state(obj->state);
3786         struct drm_dp_vcpi_allocation *pos, *vcpi;
3787
3788         state = kmemdup(old_state, sizeof(*state), GFP_KERNEL);
3789         if (!state)
3790                 return NULL;
3791
3792         __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
3793
3794         INIT_LIST_HEAD(&state->vcpis);
3795
3796         list_for_each_entry(pos, &old_state->vcpis, next) {
3797                 /* Prune leftover freed VCPI allocations */
3798                 if (!pos->vcpi)
3799                         continue;
3800
3801                 vcpi = kmemdup(pos, sizeof(*vcpi), GFP_KERNEL);
3802                 if (!vcpi)
3803                         goto fail;
3804
3805                 drm_dp_mst_get_port_malloc(vcpi->port);
3806                 list_add(&vcpi->next, &state->vcpis);
3807         }
3808
3809         return &state->base;
3810
3811 fail:
3812         list_for_each_entry_safe(pos, vcpi, &state->vcpis, next) {
3813                 drm_dp_mst_put_port_malloc(pos->port);
3814                 kfree(pos);
3815         }
3816         kfree(state);
3817
3818         return NULL;
3819 }
3820
3821 static void drm_dp_mst_destroy_state(struct drm_private_obj *obj,
3822                                      struct drm_private_state *state)
3823 {
3824         struct drm_dp_mst_topology_state *mst_state =
3825                 to_dp_mst_topology_state(state);
3826         struct drm_dp_vcpi_allocation *pos, *tmp;
3827
3828         list_for_each_entry_safe(pos, tmp, &mst_state->vcpis, next) {
3829                 /* We only keep references to ports with non-zero VCPIs */
3830                 if (pos->vcpi)
3831                         drm_dp_mst_put_port_malloc(pos->port);
3832                 kfree(pos);
3833         }
3834
3835         kfree(mst_state);
3836 }
3837
3838 static inline int
3839 drm_dp_mst_atomic_check_topology_state(struct drm_dp_mst_topology_mgr *mgr,
3840                                        struct drm_dp_mst_topology_state *mst_state)
3841 {
3842         struct drm_dp_vcpi_allocation *vcpi;
3843         int avail_slots = 63, payload_count = 0;
3844
3845         list_for_each_entry(vcpi, &mst_state->vcpis, next) {
3846                 /* Releasing VCPI is always OK-even if the port is gone */
3847                 if (!vcpi->vcpi) {
3848                         DRM_DEBUG_ATOMIC("[MST PORT:%p] releases all VCPI slots\n",
3849                                          vcpi->port);
3850                         continue;
3851                 }
3852
3853                 DRM_DEBUG_ATOMIC("[MST PORT:%p] requires %d vcpi slots\n",
3854                                  vcpi->port, vcpi->vcpi);
3855
3856                 avail_slots -= vcpi->vcpi;
3857                 if (avail_slots < 0) {
3858                         DRM_DEBUG_ATOMIC("[MST PORT:%p] not enough VCPI slots in mst state %p (avail=%d)\n",
3859                                          vcpi->port, mst_state,
3860                                          avail_slots + vcpi->vcpi);
3861                         return -ENOSPC;
3862                 }
3863
3864                 if (++payload_count > mgr->max_payloads) {
3865                         DRM_DEBUG_ATOMIC("[MST MGR:%p] state %p has too many payloads (max=%d)\n",
3866                                          mgr, mst_state, mgr->max_payloads);
3867                         return -EINVAL;
3868                 }
3869         }
3870         DRM_DEBUG_ATOMIC("[MST MGR:%p] mst state %p VCPI avail=%d used=%d\n",
3871                          mgr, mst_state, avail_slots,
3872                          63 - avail_slots);
3873
3874         return 0;
3875 }
3876
3877 /**
3878  * drm_dp_mst_atomic_check - Check that the new state of an MST topology in an
3879  * atomic update is valid
3880  * @state: Pointer to the new &struct drm_dp_mst_topology_state
3881  *
3882  * Checks the given topology state for an atomic update to ensure that it's
3883  * valid. This includes checking whether there's enough bandwidth to support
3884  * the new VCPI allocations in the atomic update.
3885  *
3886  * Any atomic drivers supporting DP MST must make sure to call this after
3887  * checking the rest of their state in their
3888  * &drm_mode_config_funcs.atomic_check() callback.
3889  *
3890  * See also:
3891  * drm_dp_atomic_find_vcpi_slots()
3892  * drm_dp_atomic_release_vcpi_slots()
3893  *
3894  * Returns:
3895  *
3896  * 0 if the new state is valid, negative error code otherwise.
3897  */
3898 int drm_dp_mst_atomic_check(struct drm_atomic_state *state)
3899 {
3900         struct drm_dp_mst_topology_mgr *mgr;
3901         struct drm_dp_mst_topology_state *mst_state;
3902         int i, ret = 0;
3903
3904         for_each_new_mst_mgr_in_state(state, mgr, mst_state, i) {
3905                 ret = drm_dp_mst_atomic_check_topology_state(mgr, mst_state);
3906                 if (ret)
3907                         break;
3908         }
3909
3910         return ret;
3911 }
3912 EXPORT_SYMBOL(drm_dp_mst_atomic_check);
3913
3914 const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs = {
3915         .atomic_duplicate_state = drm_dp_mst_duplicate_state,
3916         .atomic_destroy_state = drm_dp_mst_destroy_state,
3917 };
3918 EXPORT_SYMBOL(drm_dp_mst_topology_state_funcs);
3919
3920 /**
3921  * drm_atomic_get_mst_topology_state: get MST topology state
3922  *
3923  * @state: global atomic state
3924  * @mgr: MST topology manager, also the private object in this case
3925  *
3926  * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3927  * state vtable so that the private object state returned is that of a MST
3928  * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3929  * to care of the locking, so warn if don't hold the connection_mutex.
3930  *
3931  * RETURNS:
3932  *
3933  * The MST topology state or error pointer.
3934  */
3935 struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
3936                                                                     struct drm_dp_mst_topology_mgr *mgr)
3937 {
3938         struct drm_device *dev = mgr->dev;
3939
3940         WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3941         return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state, &mgr->base));
3942 }
3943 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state);
3944
3945 /**
3946  * drm_dp_mst_topology_mgr_init - initialise a topology manager
3947  * @mgr: manager struct to initialise
3948  * @dev: device providing this structure - for i2c addition.
3949  * @aux: DP helper aux channel to talk to this device
3950  * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3951  * @max_payloads: maximum number of payloads this GPU can source
3952  * @conn_base_id: the connector object ID the MST device is connected to.
3953  *
3954  * Return 0 for success, or negative error code on failure
3955  */
3956 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
3957                                  struct drm_device *dev, struct drm_dp_aux *aux,
3958                                  int max_dpcd_transaction_bytes,
3959                                  int max_payloads, int conn_base_id)
3960 {
3961         struct drm_dp_mst_topology_state *mst_state;
3962
3963         mutex_init(&mgr->lock);
3964         mutex_init(&mgr->qlock);
3965         mutex_init(&mgr->payload_lock);
3966         mutex_init(&mgr->destroy_connector_lock);
3967         INIT_LIST_HEAD(&mgr->tx_msg_downq);
3968         INIT_LIST_HEAD(&mgr->destroy_connector_list);
3969         INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
3970         INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
3971         INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
3972         init_waitqueue_head(&mgr->tx_waitq);
3973         mgr->dev = dev;
3974         mgr->aux = aux;
3975         mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
3976         mgr->max_payloads = max_payloads;
3977         mgr->conn_base_id = conn_base_id;
3978         if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
3979             max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
3980                 return -EINVAL;
3981         mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3982         if (!mgr->payloads)
3983                 return -ENOMEM;
3984         mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3985         if (!mgr->proposed_vcpis)
3986                 return -ENOMEM;
3987         set_bit(0, &mgr->payload_mask);
3988         if (test_calc_pbn_mode() < 0)
3989                 DRM_ERROR("MST PBN self-test failed\n");
3990
3991         mst_state = kzalloc(sizeof(*mst_state), GFP_KERNEL);
3992         if (mst_state == NULL)
3993                 return -ENOMEM;
3994
3995         mst_state->mgr = mgr;
3996         INIT_LIST_HEAD(&mst_state->vcpis);
3997
3998         drm_atomic_private_obj_init(dev, &mgr->base,
3999                                     &mst_state->base,
4000                                     &drm_dp_mst_topology_state_funcs);
4001
4002         return 0;
4003 }
4004 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
4005
4006 /**
4007  * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
4008  * @mgr: manager to destroy
4009  */
4010 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
4011 {
4012         drm_dp_mst_topology_mgr_set_mst(mgr, false);
4013         flush_work(&mgr->work);
4014         flush_work(&mgr->destroy_connector_work);
4015         mutex_lock(&mgr->payload_lock);
4016         kfree(mgr->payloads);
4017         mgr->payloads = NULL;
4018         kfree(mgr->proposed_vcpis);
4019         mgr->proposed_vcpis = NULL;
4020         mutex_unlock(&mgr->payload_lock);
4021         mgr->dev = NULL;
4022         mgr->aux = NULL;
4023         drm_atomic_private_obj_fini(&mgr->base);
4024         mgr->funcs = NULL;
4025 }
4026 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
4027
4028 static bool remote_i2c_read_ok(const struct i2c_msg msgs[], int num)
4029 {
4030         int i;
4031
4032         if (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)
4033                 return false;
4034
4035         for (i = 0; i < num - 1; i++) {
4036                 if (msgs[i].flags & I2C_M_RD ||
4037                     msgs[i].len > 0xff)
4038                         return false;
4039         }
4040
4041         return msgs[num - 1].flags & I2C_M_RD &&
4042                 msgs[num - 1].len <= 0xff;
4043 }
4044
4045 /* I2C device */
4046 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
4047                                int num)
4048 {
4049         struct drm_dp_aux *aux = adapter->algo_data;
4050         struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
4051         struct drm_dp_mst_branch *mstb;
4052         struct drm_dp_mst_topology_mgr *mgr = port->mgr;
4053         unsigned int i;
4054         struct drm_dp_sideband_msg_req_body msg;
4055         struct drm_dp_sideband_msg_tx *txmsg = NULL;
4056         int ret;
4057
4058         mstb = drm_dp_mst_topology_get_mstb_validated(mgr, port->parent);
4059         if (!mstb)
4060                 return -EREMOTEIO;
4061
4062         if (!remote_i2c_read_ok(msgs, num)) {
4063                 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
4064                 ret = -EIO;
4065                 goto out;
4066         }
4067
4068         memset(&msg, 0, sizeof(msg));
4069         msg.req_type = DP_REMOTE_I2C_READ;
4070         msg.u.i2c_read.num_transactions = num - 1;
4071         msg.u.i2c_read.port_number = port->port_num;
4072         for (i = 0; i < num - 1; i++) {
4073                 msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
4074                 msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
4075                 msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
4076                 msg.u.i2c_read.transactions[i].no_stop_bit = !(msgs[i].flags & I2C_M_STOP);
4077         }
4078         msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
4079         msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
4080
4081         txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
4082         if (!txmsg) {
4083                 ret = -ENOMEM;
4084                 goto out;
4085         }
4086
4087         txmsg->dst = mstb;
4088         drm_dp_encode_sideband_req(&msg, txmsg);
4089
4090         drm_dp_queue_down_tx(mgr, txmsg);
4091
4092         ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
4093         if (ret > 0) {
4094
4095                 if (txmsg->reply.reply_type == DP_SIDEBAND_REPLY_NAK) {
4096                         ret = -EREMOTEIO;
4097                         goto out;
4098                 }
4099                 if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
4100                         ret = -EIO;
4101                         goto out;
4102                 }
4103                 memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
4104                 ret = num;
4105         }
4106 out:
4107         kfree(txmsg);
4108         drm_dp_mst_topology_put_mstb(mstb);
4109         return ret;
4110 }
4111
4112 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
4113 {
4114         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
4115                I2C_FUNC_SMBUS_READ_BLOCK_DATA |
4116                I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
4117                I2C_FUNC_10BIT_ADDR;
4118 }
4119
4120 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
4121         .functionality = drm_dp_mst_i2c_functionality,
4122         .master_xfer = drm_dp_mst_i2c_xfer,
4123 };
4124
4125 /**
4126  * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
4127  * @aux: DisplayPort AUX channel
4128  *
4129  * Returns 0 on success or a negative error code on failure.
4130  */
4131 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
4132 {
4133         aux->ddc.algo = &drm_dp_mst_i2c_algo;
4134         aux->ddc.algo_data = aux;
4135         aux->ddc.retries = 3;
4136
4137         aux->ddc.class = I2C_CLASS_DDC;
4138         aux->ddc.owner = THIS_MODULE;
4139         aux->ddc.dev.parent = aux->dev;
4140         aux->ddc.dev.of_node = aux->dev->of_node;
4141
4142         strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
4143                 sizeof(aux->ddc.name));
4144
4145         return i2c_add_adapter(&aux->ddc);
4146 }
4147
4148 /**
4149  * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
4150  * @aux: DisplayPort AUX channel
4151  */
4152 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
4153 {
4154         i2c_del_adapter(&aux->ddc);
4155 }