2 * Copyright © 2014 Red Hat
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.
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
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/seq_file.h>
29 #include <linux/i2c.h>
30 #include <drm/drm_dp_mst_helper.h>
33 #include <drm/drm_fixed.h>
38 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
39 * protocol. The helpers contain a topology manager and bandwidth manager.
40 * The helpers encapsulate the sending and received of sideband msgs.
42 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
44 static int test_calc_pbn_mode(void);
46 static void drm_dp_put_port(struct drm_dp_mst_port
*port
);
48 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
50 struct drm_dp_payload
*payload
);
52 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
53 struct drm_dp_mst_port
*port
,
54 int offset
, int size
, u8
*bytes
);
56 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
57 struct drm_dp_mst_branch
*mstb
);
58 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
59 struct drm_dp_mst_branch
*mstb
,
60 struct drm_dp_mst_port
*port
);
61 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
64 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux
*aux
);
65 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux
*aux
);
66 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
);
67 /* sideband msg handling */
68 static u8
drm_dp_msg_header_crc4(const uint8_t *data
, size_t num_nibbles
)
73 int number_of_bits
= num_nibbles
* 4;
76 while (number_of_bits
!= 0) {
79 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
87 if ((remainder
& 0x10) == 0x10)
92 while (number_of_bits
!= 0) {
95 if ((remainder
& 0x10) != 0)
102 static u8
drm_dp_msg_data_crc4(const uint8_t *data
, u8 number_of_bytes
)
107 int number_of_bits
= number_of_bytes
* 8;
110 while (number_of_bits
!= 0) {
113 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
121 if ((remainder
& 0x100) == 0x100)
126 while (number_of_bits
!= 0) {
129 if ((remainder
& 0x100) != 0)
133 return remainder
& 0xff;
135 static inline u8
drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr
*hdr
)
138 size
+= (hdr
->lct
/ 2);
142 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
148 buf
[idx
++] = ((hdr
->lct
& 0xf) << 4) | (hdr
->lcr
& 0xf);
149 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
150 buf
[idx
++] = hdr
->rad
[i
];
151 buf
[idx
++] = (hdr
->broadcast
<< 7) | (hdr
->path_msg
<< 6) |
152 (hdr
->msg_len
& 0x3f);
153 buf
[idx
++] = (hdr
->somt
<< 7) | (hdr
->eomt
<< 6) | (hdr
->seqno
<< 4);
155 crc4
= drm_dp_msg_header_crc4(buf
, (idx
* 2) - 1);
156 buf
[idx
- 1] |= (crc4
& 0xf);
161 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
162 u8
*buf
, int buflen
, u8
*hdrlen
)
171 len
+= ((buf
[0] & 0xf0) >> 4) / 2;
174 crc4
= drm_dp_msg_header_crc4(buf
, (len
* 2) - 1);
176 if ((crc4
& 0xf) != (buf
[len
- 1] & 0xf)) {
177 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4
, buf
[len
- 1]);
181 hdr
->lct
= (buf
[0] & 0xf0) >> 4;
182 hdr
->lcr
= (buf
[0] & 0xf);
184 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
185 hdr
->rad
[i
] = buf
[idx
++];
186 hdr
->broadcast
= (buf
[idx
] >> 7) & 0x1;
187 hdr
->path_msg
= (buf
[idx
] >> 6) & 0x1;
188 hdr
->msg_len
= buf
[idx
] & 0x3f;
190 hdr
->somt
= (buf
[idx
] >> 7) & 0x1;
191 hdr
->eomt
= (buf
[idx
] >> 6) & 0x1;
192 hdr
->seqno
= (buf
[idx
] >> 4) & 0x1;
198 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body
*req
,
199 struct drm_dp_sideband_msg_tx
*raw
)
204 buf
[idx
++] = req
->req_type
& 0x7f;
206 switch (req
->req_type
) {
207 case DP_ENUM_PATH_RESOURCES
:
208 buf
[idx
] = (req
->u
.port_num
.port_number
& 0xf) << 4;
211 case DP_ALLOCATE_PAYLOAD
:
212 buf
[idx
] = (req
->u
.allocate_payload
.port_number
& 0xf) << 4 |
213 (req
->u
.allocate_payload
.number_sdp_streams
& 0xf);
215 buf
[idx
] = (req
->u
.allocate_payload
.vcpi
& 0x7f);
217 buf
[idx
] = (req
->u
.allocate_payload
.pbn
>> 8);
219 buf
[idx
] = (req
->u
.allocate_payload
.pbn
& 0xff);
221 for (i
= 0; i
< req
->u
.allocate_payload
.number_sdp_streams
/ 2; i
++) {
222 buf
[idx
] = ((req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2] & 0xf) << 4) |
223 (req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2 + 1] & 0xf);
226 if (req
->u
.allocate_payload
.number_sdp_streams
& 1) {
227 i
= req
->u
.allocate_payload
.number_sdp_streams
- 1;
228 buf
[idx
] = (req
->u
.allocate_payload
.sdp_stream_sink
[i
] & 0xf) << 4;
232 case DP_QUERY_PAYLOAD
:
233 buf
[idx
] = (req
->u
.query_payload
.port_number
& 0xf) << 4;
235 buf
[idx
] = (req
->u
.query_payload
.vcpi
& 0x7f);
238 case DP_REMOTE_DPCD_READ
:
239 buf
[idx
] = (req
->u
.dpcd_read
.port_number
& 0xf) << 4;
240 buf
[idx
] |= ((req
->u
.dpcd_read
.dpcd_address
& 0xf0000) >> 16) & 0xf;
242 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff00) >> 8;
244 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff);
246 buf
[idx
] = (req
->u
.dpcd_read
.num_bytes
);
250 case DP_REMOTE_DPCD_WRITE
:
251 buf
[idx
] = (req
->u
.dpcd_write
.port_number
& 0xf) << 4;
252 buf
[idx
] |= ((req
->u
.dpcd_write
.dpcd_address
& 0xf0000) >> 16) & 0xf;
254 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff00) >> 8;
256 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff);
258 buf
[idx
] = (req
->u
.dpcd_write
.num_bytes
);
260 memcpy(&buf
[idx
], req
->u
.dpcd_write
.bytes
, req
->u
.dpcd_write
.num_bytes
);
261 idx
+= req
->u
.dpcd_write
.num_bytes
;
263 case DP_REMOTE_I2C_READ
:
264 buf
[idx
] = (req
->u
.i2c_read
.port_number
& 0xf) << 4;
265 buf
[idx
] |= (req
->u
.i2c_read
.num_transactions
& 0x3);
267 for (i
= 0; i
< (req
->u
.i2c_read
.num_transactions
& 0x3); i
++) {
268 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].i2c_dev_id
& 0x7f;
270 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].num_bytes
;
272 memcpy(&buf
[idx
], req
->u
.i2c_read
.transactions
[i
].bytes
, req
->u
.i2c_read
.transactions
[i
].num_bytes
);
273 idx
+= req
->u
.i2c_read
.transactions
[i
].num_bytes
;
275 buf
[idx
] = (req
->u
.i2c_read
.transactions
[i
].no_stop_bit
& 0x1) << 5;
276 buf
[idx
] |= (req
->u
.i2c_read
.transactions
[i
].i2c_transaction_delay
& 0xf);
279 buf
[idx
] = (req
->u
.i2c_read
.read_i2c_device_id
) & 0x7f;
281 buf
[idx
] = (req
->u
.i2c_read
.num_bytes_read
);
285 case DP_REMOTE_I2C_WRITE
:
286 buf
[idx
] = (req
->u
.i2c_write
.port_number
& 0xf) << 4;
288 buf
[idx
] = (req
->u
.i2c_write
.write_i2c_device_id
) & 0x7f;
290 buf
[idx
] = (req
->u
.i2c_write
.num_bytes
);
292 memcpy(&buf
[idx
], req
->u
.i2c_write
.bytes
, req
->u
.i2c_write
.num_bytes
);
293 idx
+= req
->u
.i2c_write
.num_bytes
;
299 static void drm_dp_crc_sideband_chunk_req(u8
*msg
, u8 len
)
302 crc4
= drm_dp_msg_data_crc4(msg
, len
);
306 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body
*rep
,
307 struct drm_dp_sideband_msg_tx
*raw
)
312 buf
[idx
++] = (rep
->reply_type
& 0x1) << 7 | (rep
->req_type
& 0x7f);
317 /* this adds a chunk of msg to the builder to get the final msg */
318 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx
*msg
,
319 u8
*replybuf
, u8 replybuflen
, bool hdr
)
326 struct drm_dp_sideband_msg_hdr recv_hdr
;
327 ret
= drm_dp_decode_sideband_msg_hdr(&recv_hdr
, replybuf
, replybuflen
, &hdrlen
);
329 print_hex_dump(KERN_DEBUG
, "failed hdr", DUMP_PREFIX_NONE
, 16, 1, replybuf
, replybuflen
, false);
334 * ignore out-of-order messages or messages that are part of a
337 if (!recv_hdr
.somt
&& !msg
->have_somt
)
340 /* get length contained in this portion */
341 msg
->curchunk_len
= recv_hdr
.msg_len
;
342 msg
->curchunk_hdrlen
= hdrlen
;
344 /* we have already gotten an somt - don't bother parsing */
345 if (recv_hdr
.somt
&& msg
->have_somt
)
349 memcpy(&msg
->initial_hdr
, &recv_hdr
, sizeof(struct drm_dp_sideband_msg_hdr
));
350 msg
->have_somt
= true;
353 msg
->have_eomt
= true;
355 /* copy the bytes for the remainder of this header chunk */
356 msg
->curchunk_idx
= min(msg
->curchunk_len
, (u8
)(replybuflen
- hdrlen
));
357 memcpy(&msg
->chunk
[0], replybuf
+ hdrlen
, msg
->curchunk_idx
);
359 memcpy(&msg
->chunk
[msg
->curchunk_idx
], replybuf
, replybuflen
);
360 msg
->curchunk_idx
+= replybuflen
;
363 if (msg
->curchunk_idx
>= msg
->curchunk_len
) {
365 crc4
= drm_dp_msg_data_crc4(msg
->chunk
, msg
->curchunk_len
- 1);
366 /* copy chunk into bigger msg */
367 memcpy(&msg
->msg
[msg
->curlen
], msg
->chunk
, msg
->curchunk_len
- 1);
368 msg
->curlen
+= msg
->curchunk_len
- 1;
373 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx
*raw
,
374 struct drm_dp_sideband_msg_reply_body
*repmsg
)
378 memcpy(repmsg
->u
.link_addr
.guid
, &raw
->msg
[idx
], 16);
380 repmsg
->u
.link_addr
.nports
= raw
->msg
[idx
] & 0xf;
382 if (idx
> raw
->curlen
)
384 for (i
= 0; i
< repmsg
->u
.link_addr
.nports
; i
++) {
385 if (raw
->msg
[idx
] & 0x80)
386 repmsg
->u
.link_addr
.ports
[i
].input_port
= 1;
388 repmsg
->u
.link_addr
.ports
[i
].peer_device_type
= (raw
->msg
[idx
] >> 4) & 0x7;
389 repmsg
->u
.link_addr
.ports
[i
].port_number
= (raw
->msg
[idx
] & 0xf);
392 if (idx
> raw
->curlen
)
394 repmsg
->u
.link_addr
.ports
[i
].mcs
= (raw
->msg
[idx
] >> 7) & 0x1;
395 repmsg
->u
.link_addr
.ports
[i
].ddps
= (raw
->msg
[idx
] >> 6) & 0x1;
396 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0)
397 repmsg
->u
.link_addr
.ports
[i
].legacy_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
399 if (idx
> raw
->curlen
)
401 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0) {
402 repmsg
->u
.link_addr
.ports
[i
].dpcd_revision
= (raw
->msg
[idx
]);
404 if (idx
> raw
->curlen
)
406 memcpy(repmsg
->u
.link_addr
.ports
[i
].peer_guid
, &raw
->msg
[idx
], 16);
408 if (idx
> raw
->curlen
)
410 repmsg
->u
.link_addr
.ports
[i
].num_sdp_streams
= (raw
->msg
[idx
] >> 4) & 0xf;
411 repmsg
->u
.link_addr
.ports
[i
].num_sdp_stream_sinks
= (raw
->msg
[idx
] & 0xf);
415 if (idx
> raw
->curlen
)
421 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
425 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx
*raw
,
426 struct drm_dp_sideband_msg_reply_body
*repmsg
)
429 repmsg
->u
.remote_dpcd_read_ack
.port_number
= raw
->msg
[idx
] & 0xf;
431 if (idx
> raw
->curlen
)
433 repmsg
->u
.remote_dpcd_read_ack
.num_bytes
= raw
->msg
[idx
];
434 if (idx
> raw
->curlen
)
437 memcpy(repmsg
->u
.remote_dpcd_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_dpcd_read_ack
.num_bytes
);
440 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
444 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx
*raw
,
445 struct drm_dp_sideband_msg_reply_body
*repmsg
)
448 repmsg
->u
.remote_dpcd_write_ack
.port_number
= raw
->msg
[idx
] & 0xf;
450 if (idx
> raw
->curlen
)
454 DRM_DEBUG_KMS("parse length fail %d %d\n", idx
, raw
->curlen
);
458 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx
*raw
,
459 struct drm_dp_sideband_msg_reply_body
*repmsg
)
463 repmsg
->u
.remote_i2c_read_ack
.port_number
= (raw
->msg
[idx
] & 0xf);
465 if (idx
> raw
->curlen
)
467 repmsg
->u
.remote_i2c_read_ack
.num_bytes
= raw
->msg
[idx
];
470 memcpy(repmsg
->u
.remote_i2c_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_i2c_read_ack
.num_bytes
);
473 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx
, raw
->curlen
);
477 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx
*raw
,
478 struct drm_dp_sideband_msg_reply_body
*repmsg
)
481 repmsg
->u
.path_resources
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
483 if (idx
> raw
->curlen
)
485 repmsg
->u
.path_resources
.full_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
487 if (idx
> raw
->curlen
)
489 repmsg
->u
.path_resources
.avail_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
491 if (idx
> raw
->curlen
)
495 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx
, raw
->curlen
);
499 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
500 struct drm_dp_sideband_msg_reply_body
*repmsg
)
503 repmsg
->u
.allocate_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
505 if (idx
> raw
->curlen
)
507 repmsg
->u
.allocate_payload
.vcpi
= raw
->msg
[idx
];
509 if (idx
> raw
->curlen
)
511 repmsg
->u
.allocate_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
513 if (idx
> raw
->curlen
)
517 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx
, raw
->curlen
);
521 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
522 struct drm_dp_sideband_msg_reply_body
*repmsg
)
525 repmsg
->u
.query_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
527 if (idx
> raw
->curlen
)
529 repmsg
->u
.query_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
531 if (idx
> raw
->curlen
)
535 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx
, raw
->curlen
);
539 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx
*raw
,
540 struct drm_dp_sideband_msg_reply_body
*msg
)
542 memset(msg
, 0, sizeof(*msg
));
543 msg
->reply_type
= (raw
->msg
[0] & 0x80) >> 7;
544 msg
->req_type
= (raw
->msg
[0] & 0x7f);
546 if (msg
->reply_type
) {
547 memcpy(msg
->u
.nak
.guid
, &raw
->msg
[1], 16);
548 msg
->u
.nak
.reason
= raw
->msg
[17];
549 msg
->u
.nak
.nak_data
= raw
->msg
[18];
553 switch (msg
->req_type
) {
554 case DP_LINK_ADDRESS
:
555 return drm_dp_sideband_parse_link_address(raw
, msg
);
556 case DP_QUERY_PAYLOAD
:
557 return drm_dp_sideband_parse_query_payload_ack(raw
, msg
);
558 case DP_REMOTE_DPCD_READ
:
559 return drm_dp_sideband_parse_remote_dpcd_read(raw
, msg
);
560 case DP_REMOTE_DPCD_WRITE
:
561 return drm_dp_sideband_parse_remote_dpcd_write(raw
, msg
);
562 case DP_REMOTE_I2C_READ
:
563 return drm_dp_sideband_parse_remote_i2c_read_ack(raw
, msg
);
564 case DP_ENUM_PATH_RESOURCES
:
565 return drm_dp_sideband_parse_enum_path_resources_ack(raw
, msg
);
566 case DP_ALLOCATE_PAYLOAD
:
567 return drm_dp_sideband_parse_allocate_payload_ack(raw
, msg
);
569 DRM_ERROR("Got unknown reply 0x%02x\n", msg
->req_type
);
574 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
575 struct drm_dp_sideband_msg_req_body
*msg
)
579 msg
->u
.conn_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
581 if (idx
> raw
->curlen
)
584 memcpy(msg
->u
.conn_stat
.guid
, &raw
->msg
[idx
], 16);
586 if (idx
> raw
->curlen
)
589 msg
->u
.conn_stat
.legacy_device_plug_status
= (raw
->msg
[idx
] >> 6) & 0x1;
590 msg
->u
.conn_stat
.displayport_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
591 msg
->u
.conn_stat
.message_capability_status
= (raw
->msg
[idx
] >> 4) & 0x1;
592 msg
->u
.conn_stat
.input_port
= (raw
->msg
[idx
] >> 3) & 0x1;
593 msg
->u
.conn_stat
.peer_device_type
= (raw
->msg
[idx
] & 0x7);
597 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx
, raw
->curlen
);
601 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
602 struct drm_dp_sideband_msg_req_body
*msg
)
606 msg
->u
.resource_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
608 if (idx
> raw
->curlen
)
611 memcpy(msg
->u
.resource_stat
.guid
, &raw
->msg
[idx
], 16);
613 if (idx
> raw
->curlen
)
616 msg
->u
.resource_stat
.available_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
620 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx
, raw
->curlen
);
624 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx
*raw
,
625 struct drm_dp_sideband_msg_req_body
*msg
)
627 memset(msg
, 0, sizeof(*msg
));
628 msg
->req_type
= (raw
->msg
[0] & 0x7f);
630 switch (msg
->req_type
) {
631 case DP_CONNECTION_STATUS_NOTIFY
:
632 return drm_dp_sideband_parse_connection_status_notify(raw
, msg
);
633 case DP_RESOURCE_STATUS_NOTIFY
:
634 return drm_dp_sideband_parse_resource_status_notify(raw
, msg
);
636 DRM_ERROR("Got unknown request 0x%02x\n", msg
->req_type
);
641 static int build_dpcd_write(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
, u8
*bytes
)
643 struct drm_dp_sideband_msg_req_body req
;
645 req
.req_type
= DP_REMOTE_DPCD_WRITE
;
646 req
.u
.dpcd_write
.port_number
= port_num
;
647 req
.u
.dpcd_write
.dpcd_address
= offset
;
648 req
.u
.dpcd_write
.num_bytes
= num_bytes
;
649 req
.u
.dpcd_write
.bytes
= bytes
;
650 drm_dp_encode_sideband_req(&req
, msg
);
655 static int build_link_address(struct drm_dp_sideband_msg_tx
*msg
)
657 struct drm_dp_sideband_msg_req_body req
;
659 req
.req_type
= DP_LINK_ADDRESS
;
660 drm_dp_encode_sideband_req(&req
, msg
);
664 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx
*msg
, int port_num
)
666 struct drm_dp_sideband_msg_req_body req
;
668 req
.req_type
= DP_ENUM_PATH_RESOURCES
;
669 req
.u
.port_num
.port_number
= port_num
;
670 drm_dp_encode_sideband_req(&req
, msg
);
671 msg
->path_msg
= true;
675 static int build_allocate_payload(struct drm_dp_sideband_msg_tx
*msg
, int port_num
,
676 u8 vcpi
, uint16_t pbn
,
677 u8 number_sdp_streams
,
680 struct drm_dp_sideband_msg_req_body req
;
681 memset(&req
, 0, sizeof(req
));
682 req
.req_type
= DP_ALLOCATE_PAYLOAD
;
683 req
.u
.allocate_payload
.port_number
= port_num
;
684 req
.u
.allocate_payload
.vcpi
= vcpi
;
685 req
.u
.allocate_payload
.pbn
= pbn
;
686 req
.u
.allocate_payload
.number_sdp_streams
= number_sdp_streams
;
687 memcpy(req
.u
.allocate_payload
.sdp_stream_sink
, sdp_stream_sink
,
689 drm_dp_encode_sideband_req(&req
, msg
);
690 msg
->path_msg
= true;
694 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
695 struct drm_dp_vcpi
*vcpi
)
699 mutex_lock(&mgr
->payload_lock
);
700 ret
= find_first_zero_bit(&mgr
->payload_mask
, mgr
->max_payloads
+ 1);
701 if (ret
> mgr
->max_payloads
) {
703 DRM_DEBUG_KMS("out of payload ids %d\n", ret
);
707 vcpi_ret
= find_first_zero_bit(&mgr
->vcpi_mask
, mgr
->max_payloads
+ 1);
708 if (vcpi_ret
> mgr
->max_payloads
) {
710 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret
);
714 set_bit(ret
, &mgr
->payload_mask
);
715 set_bit(vcpi_ret
, &mgr
->vcpi_mask
);
716 vcpi
->vcpi
= vcpi_ret
+ 1;
717 mgr
->proposed_vcpis
[ret
- 1] = vcpi
;
719 mutex_unlock(&mgr
->payload_lock
);
723 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
730 mutex_lock(&mgr
->payload_lock
);
731 DRM_DEBUG_KMS("putting payload %d\n", vcpi
);
732 clear_bit(vcpi
- 1, &mgr
->vcpi_mask
);
734 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
735 if (mgr
->proposed_vcpis
[i
])
736 if (mgr
->proposed_vcpis
[i
]->vcpi
== vcpi
) {
737 mgr
->proposed_vcpis
[i
] = NULL
;
738 clear_bit(i
+ 1, &mgr
->payload_mask
);
741 mutex_unlock(&mgr
->payload_lock
);
744 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr
*mgr
,
745 struct drm_dp_sideband_msg_tx
*txmsg
)
750 * All updates to txmsg->state are protected by mgr->qlock, and the two
751 * cases we check here are terminal states. For those the barriers
752 * provided by the wake_up/wait_event pair are enough.
754 ret
= (txmsg
->state
== DRM_DP_SIDEBAND_TX_RX
||
755 txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
);
759 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch
*mstb
,
760 struct drm_dp_sideband_msg_tx
*txmsg
)
762 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
765 ret
= wait_event_timeout(mgr
->tx_waitq
,
766 check_txmsg_state(mgr
, txmsg
),
768 mutex_lock(&mstb
->mgr
->qlock
);
770 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
) {
775 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg
, txmsg
->state
, txmsg
->seqno
);
777 /* dump some state */
781 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
||
782 txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
) {
783 list_del(&txmsg
->next
);
786 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
||
787 txmsg
->state
== DRM_DP_SIDEBAND_TX_SENT
) {
788 mstb
->tx_slots
[txmsg
->seqno
] = NULL
;
792 mutex_unlock(&mgr
->qlock
);
797 static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct
, u8
*rad
)
799 struct drm_dp_mst_branch
*mstb
;
801 mstb
= kzalloc(sizeof(*mstb
), GFP_KERNEL
);
807 memcpy(mstb
->rad
, rad
, lct
/ 2);
808 INIT_LIST_HEAD(&mstb
->ports
);
809 kref_init(&mstb
->kref
);
813 static void drm_dp_free_mst_port(struct kref
*kref
);
815 static void drm_dp_free_mst_branch_device(struct kref
*kref
)
817 struct drm_dp_mst_branch
*mstb
= container_of(kref
, struct drm_dp_mst_branch
, kref
);
818 if (mstb
->port_parent
) {
819 if (list_empty(&mstb
->port_parent
->next
))
820 kref_put(&mstb
->port_parent
->kref
, drm_dp_free_mst_port
);
825 static void drm_dp_destroy_mst_branch_device(struct kref
*kref
)
827 struct drm_dp_mst_branch
*mstb
= container_of(kref
, struct drm_dp_mst_branch
, kref
);
828 struct drm_dp_mst_port
*port
, *tmp
;
829 bool wake_tx
= false;
832 * init kref again to be used by ports to remove mst branch when it is
837 if (mstb
->port_parent
&& list_empty(&mstb
->port_parent
->next
))
838 kref_get(&mstb
->port_parent
->kref
);
841 * destroy all ports - don't need lock
842 * as there are no more references to the mst branch
843 * device at this point.
845 list_for_each_entry_safe(port
, tmp
, &mstb
->ports
, next
) {
846 list_del(&port
->next
);
847 drm_dp_put_port(port
);
850 /* drop any tx slots msg */
851 mutex_lock(&mstb
->mgr
->qlock
);
852 if (mstb
->tx_slots
[0]) {
853 mstb
->tx_slots
[0]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
854 mstb
->tx_slots
[0] = NULL
;
857 if (mstb
->tx_slots
[1]) {
858 mstb
->tx_slots
[1]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
859 mstb
->tx_slots
[1] = NULL
;
862 mutex_unlock(&mstb
->mgr
->qlock
);
865 wake_up(&mstb
->mgr
->tx_waitq
);
867 kref_put(kref
, drm_dp_free_mst_branch_device
);
870 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch
*mstb
)
872 kref_put(&mstb
->kref
, drm_dp_destroy_mst_branch_device
);
876 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port
*port
, int old_pdt
)
878 struct drm_dp_mst_branch
*mstb
;
881 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
882 case DP_PEER_DEVICE_SST_SINK
:
883 /* remove i2c over sideband */
884 drm_dp_mst_unregister_i2c_bus(&port
->aux
);
886 case DP_PEER_DEVICE_MST_BRANCHING
:
889 drm_dp_put_mst_branch_device(mstb
);
894 static void drm_dp_destroy_port(struct kref
*kref
)
896 struct drm_dp_mst_port
*port
= container_of(kref
, struct drm_dp_mst_port
, kref
);
897 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
900 port
->vcpi
.num_slots
= 0;
902 kfree(port
->cached_edid
);
905 * The only time we don't have a connector
906 * on an output port is if the connector init
909 if (port
->connector
) {
910 /* we can't destroy the connector here, as
911 * we might be holding the mode_config.mutex
912 * from an EDID retrieval */
914 mutex_lock(&mgr
->destroy_connector_lock
);
915 kref_get(&port
->parent
->kref
);
916 list_add(&port
->next
, &mgr
->destroy_connector_list
);
917 mutex_unlock(&mgr
->destroy_connector_lock
);
918 schedule_work(&mgr
->destroy_connector_work
);
921 /* no need to clean up vcpi
922 * as if we have no connector we never setup a vcpi */
923 drm_dp_port_teardown_pdt(port
, port
->pdt
);
924 port
->pdt
= DP_PEER_DEVICE_NONE
;
929 static void drm_dp_put_port(struct drm_dp_mst_port
*port
)
931 kref_put(&port
->kref
, drm_dp_destroy_port
);
934 static struct drm_dp_mst_branch
*drm_dp_mst_get_validated_mstb_ref_locked(struct drm_dp_mst_branch
*mstb
, struct drm_dp_mst_branch
*to_find
)
936 struct drm_dp_mst_port
*port
;
937 struct drm_dp_mst_branch
*rmstb
;
938 if (to_find
== mstb
) {
939 kref_get(&mstb
->kref
);
942 list_for_each_entry(port
, &mstb
->ports
, next
) {
944 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(port
->mstb
, to_find
);
952 static struct drm_dp_mst_branch
*drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_branch
*mstb
)
954 struct drm_dp_mst_branch
*rmstb
= NULL
;
955 mutex_lock(&mgr
->lock
);
956 if (mgr
->mst_primary
)
957 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(mgr
->mst_primary
, mstb
);
958 mutex_unlock(&mgr
->lock
);
962 static struct drm_dp_mst_port
*drm_dp_mst_get_port_ref_locked(struct drm_dp_mst_branch
*mstb
, struct drm_dp_mst_port
*to_find
)
964 struct drm_dp_mst_port
*port
, *mport
;
966 list_for_each_entry(port
, &mstb
->ports
, next
) {
967 if (port
== to_find
) {
968 kref_get(&port
->kref
);
972 mport
= drm_dp_mst_get_port_ref_locked(port
->mstb
, to_find
);
980 static struct drm_dp_mst_port
*drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
982 struct drm_dp_mst_port
*rport
= NULL
;
983 mutex_lock(&mgr
->lock
);
984 if (mgr
->mst_primary
)
985 rport
= drm_dp_mst_get_port_ref_locked(mgr
->mst_primary
, port
);
986 mutex_unlock(&mgr
->lock
);
990 static struct drm_dp_mst_port
*drm_dp_get_port(struct drm_dp_mst_branch
*mstb
, u8 port_num
)
992 struct drm_dp_mst_port
*port
;
994 list_for_each_entry(port
, &mstb
->ports
, next
) {
995 if (port
->port_num
== port_num
) {
996 kref_get(&port
->kref
);
1005 * calculate a new RAD for this MST branch device
1006 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1007 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1009 static u8
drm_dp_calculate_rad(struct drm_dp_mst_port
*port
,
1012 int parent_lct
= port
->parent
->lct
;
1014 int idx
= (parent_lct
- 1) / 2;
1015 if (parent_lct
> 1) {
1016 memcpy(rad
, port
->parent
->rad
, idx
+ 1);
1017 shift
= (parent_lct
% 2) ? 4 : 0;
1021 rad
[idx
] |= port
->port_num
<< shift
;
1022 return parent_lct
+ 1;
1026 * return sends link address for new mstb
1028 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port
*port
)
1032 bool send_link
= false;
1033 switch (port
->pdt
) {
1034 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
1035 case DP_PEER_DEVICE_SST_SINK
:
1036 /* add i2c over sideband */
1037 ret
= drm_dp_mst_register_i2c_bus(&port
->aux
);
1039 case DP_PEER_DEVICE_MST_BRANCHING
:
1040 lct
= drm_dp_calculate_rad(port
, rad
);
1042 port
->mstb
= drm_dp_add_mst_branch_device(lct
, rad
);
1043 port
->mstb
->mgr
= port
->mgr
;
1044 port
->mstb
->port_parent
= port
;
1052 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch
*mstb
, u8
*guid
)
1056 memcpy(mstb
->guid
, guid
, 16);
1058 if (!drm_dp_validate_guid(mstb
->mgr
, mstb
->guid
)) {
1059 if (mstb
->port_parent
) {
1060 ret
= drm_dp_send_dpcd_write(
1068 ret
= drm_dp_dpcd_write(
1077 static void build_mst_prop_path(const struct drm_dp_mst_branch
*mstb
,
1080 size_t proppath_size
)
1084 snprintf(proppath
, proppath_size
, "mst:%d", mstb
->mgr
->conn_base_id
);
1085 for (i
= 0; i
< (mstb
->lct
- 1); i
++) {
1086 int shift
= (i
% 2) ? 0 : 4;
1087 int port_num
= (mstb
->rad
[i
/ 2] >> shift
) & 0xf;
1088 snprintf(temp
, sizeof(temp
), "-%d", port_num
);
1089 strlcat(proppath
, temp
, proppath_size
);
1091 snprintf(temp
, sizeof(temp
), "-%d", pnum
);
1092 strlcat(proppath
, temp
, proppath_size
);
1095 static void drm_dp_add_port(struct drm_dp_mst_branch
*mstb
,
1097 struct drm_dp_link_addr_reply_port
*port_msg
)
1099 struct drm_dp_mst_port
*port
;
1101 bool created
= false;
1104 port
= drm_dp_get_port(mstb
, port_msg
->port_number
);
1106 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1109 kref_init(&port
->kref
);
1110 port
->parent
= mstb
;
1111 port
->port_num
= port_msg
->port_number
;
1112 port
->mgr
= mstb
->mgr
;
1113 port
->aux
.name
= "DPMST";
1114 port
->aux
.dev
= dev
;
1117 old_pdt
= port
->pdt
;
1118 old_ddps
= port
->ddps
;
1121 port
->pdt
= port_msg
->peer_device_type
;
1122 port
->input
= port_msg
->input_port
;
1123 port
->mcs
= port_msg
->mcs
;
1124 port
->ddps
= port_msg
->ddps
;
1125 port
->ldps
= port_msg
->legacy_device_plug_status
;
1126 port
->dpcd_rev
= port_msg
->dpcd_revision
;
1127 port
->num_sdp_streams
= port_msg
->num_sdp_streams
;
1128 port
->num_sdp_stream_sinks
= port_msg
->num_sdp_stream_sinks
;
1130 /* manage mstb port lists with mgr lock - take a reference
1133 mutex_lock(&mstb
->mgr
->lock
);
1134 kref_get(&port
->kref
);
1135 list_add(&port
->next
, &mstb
->ports
);
1136 mutex_unlock(&mstb
->mgr
->lock
);
1139 if (old_ddps
!= port
->ddps
) {
1142 drm_dp_send_enum_path_resources(mstb
->mgr
, mstb
, port
);
1144 port
->available_pbn
= 0;
1148 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1149 drm_dp_port_teardown_pdt(port
, old_pdt
);
1151 ret
= drm_dp_port_setup_pdt(port
);
1153 drm_dp_send_link_address(mstb
->mgr
, port
->mstb
);
1156 if (created
&& !port
->input
) {
1159 build_mst_prop_path(mstb
, port
->port_num
, proppath
, sizeof(proppath
));
1160 port
->connector
= (*mstb
->mgr
->cbs
->add_connector
)(mstb
->mgr
, port
, proppath
);
1161 if (!port
->connector
) {
1162 /* remove it from the port list */
1163 mutex_lock(&mstb
->mgr
->lock
);
1164 list_del(&port
->next
);
1165 mutex_unlock(&mstb
->mgr
->lock
);
1166 /* drop port list reference */
1167 drm_dp_put_port(port
);
1170 if ((port
->pdt
== DP_PEER_DEVICE_DP_LEGACY_CONV
||
1171 port
->pdt
== DP_PEER_DEVICE_SST_SINK
) &&
1172 port
->port_num
>= DP_MST_LOGICAL_PORT_0
) {
1173 port
->cached_edid
= drm_get_edid(port
->connector
, &port
->aux
.ddc
);
1174 drm_mode_connector_set_tile_property(port
->connector
);
1176 (*mstb
->mgr
->cbs
->register_connector
)(port
->connector
);
1180 /* put reference to this port */
1181 drm_dp_put_port(port
);
1184 static void drm_dp_update_port(struct drm_dp_mst_branch
*mstb
,
1185 struct drm_dp_connection_status_notify
*conn_stat
)
1187 struct drm_dp_mst_port
*port
;
1190 bool dowork
= false;
1191 port
= drm_dp_get_port(mstb
, conn_stat
->port_number
);
1195 old_ddps
= port
->ddps
;
1196 old_pdt
= port
->pdt
;
1197 port
->pdt
= conn_stat
->peer_device_type
;
1198 port
->mcs
= conn_stat
->message_capability_status
;
1199 port
->ldps
= conn_stat
->legacy_device_plug_status
;
1200 port
->ddps
= conn_stat
->displayport_device_plug_status
;
1202 if (old_ddps
!= port
->ddps
) {
1206 port
->available_pbn
= 0;
1209 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1210 drm_dp_port_teardown_pdt(port
, old_pdt
);
1212 if (drm_dp_port_setup_pdt(port
))
1216 drm_dp_put_port(port
);
1218 queue_work(system_long_wq
, &mstb
->mgr
->work
);
1222 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr
*mgr
,
1225 struct drm_dp_mst_branch
*mstb
;
1226 struct drm_dp_mst_port
*port
;
1228 /* find the port by iterating down */
1230 mutex_lock(&mgr
->lock
);
1231 mstb
= mgr
->mst_primary
;
1236 for (i
= 0; i
< lct
- 1; i
++) {
1237 int shift
= (i
% 2) ? 0 : 4;
1238 int port_num
= (rad
[i
/ 2] >> shift
) & 0xf;
1240 list_for_each_entry(port
, &mstb
->ports
, next
) {
1241 if (port
->port_num
== port_num
) {
1244 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct
, rad
[0]);
1252 kref_get(&mstb
->kref
);
1254 mutex_unlock(&mgr
->lock
);
1258 static struct drm_dp_mst_branch
*get_mst_branch_device_by_guid_helper(
1259 struct drm_dp_mst_branch
*mstb
,
1262 struct drm_dp_mst_branch
*found_mstb
;
1263 struct drm_dp_mst_port
*port
;
1265 if (memcmp(mstb
->guid
, guid
, 16) == 0)
1269 list_for_each_entry(port
, &mstb
->ports
, next
) {
1273 found_mstb
= get_mst_branch_device_by_guid_helper(port
->mstb
, guid
);
1282 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device_by_guid(
1283 struct drm_dp_mst_topology_mgr
*mgr
,
1286 struct drm_dp_mst_branch
*mstb
;
1288 /* find the port by iterating down */
1289 mutex_lock(&mgr
->lock
);
1291 mstb
= get_mst_branch_device_by_guid_helper(mgr
->mst_primary
, guid
);
1294 kref_get(&mstb
->kref
);
1296 mutex_unlock(&mgr
->lock
);
1300 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1301 struct drm_dp_mst_branch
*mstb
)
1303 struct drm_dp_mst_port
*port
;
1304 struct drm_dp_mst_branch
*mstb_child
;
1305 if (!mstb
->link_address_sent
)
1306 drm_dp_send_link_address(mgr
, mstb
);
1308 list_for_each_entry(port
, &mstb
->ports
, next
) {
1315 if (!port
->available_pbn
)
1316 drm_dp_send_enum_path_resources(mgr
, mstb
, port
);
1319 mstb_child
= drm_dp_get_validated_mstb_ref(mgr
, port
->mstb
);
1321 drm_dp_check_and_send_link_address(mgr
, mstb_child
);
1322 drm_dp_put_mst_branch_device(mstb_child
);
1328 static void drm_dp_mst_link_probe_work(struct work_struct
*work
)
1330 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, work
);
1331 struct drm_dp_mst_branch
*mstb
;
1333 mutex_lock(&mgr
->lock
);
1334 mstb
= mgr
->mst_primary
;
1336 kref_get(&mstb
->kref
);
1338 mutex_unlock(&mgr
->lock
);
1340 drm_dp_check_and_send_link_address(mgr
, mstb
);
1341 drm_dp_put_mst_branch_device(mstb
);
1345 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
1348 static u8 zero_guid
[16];
1350 if (!memcmp(guid
, zero_guid
, 16)) {
1351 u64 salt
= get_jiffies_64();
1352 memcpy(&guid
[0], &salt
, sizeof(u64
));
1353 memcpy(&guid
[8], &salt
, sizeof(u64
));
1360 static int build_dpcd_read(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
)
1362 struct drm_dp_sideband_msg_req_body req
;
1364 req
.req_type
= DP_REMOTE_DPCD_READ
;
1365 req
.u
.dpcd_read
.port_number
= port_num
;
1366 req
.u
.dpcd_read
.dpcd_address
= offset
;
1367 req
.u
.dpcd_read
.num_bytes
= num_bytes
;
1368 drm_dp_encode_sideband_req(&req
, msg
);
1374 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1375 bool up
, u8
*msg
, int len
)
1378 int regbase
= up
? DP_SIDEBAND_MSG_UP_REP_BASE
: DP_SIDEBAND_MSG_DOWN_REQ_BASE
;
1379 int tosend
, total
, offset
;
1386 tosend
= min3(mgr
->max_dpcd_transaction_bytes
, 16, total
);
1388 ret
= drm_dp_dpcd_write(mgr
->aux
, regbase
+ offset
,
1391 if (ret
!= tosend
) {
1392 if (ret
== -EIO
&& retries
< 5) {
1396 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend
, ret
);
1402 } while (total
> 0);
1406 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr
*hdr
,
1407 struct drm_dp_sideband_msg_tx
*txmsg
)
1409 struct drm_dp_mst_branch
*mstb
= txmsg
->dst
;
1412 /* both msg slots are full */
1413 if (txmsg
->seqno
== -1) {
1414 if (mstb
->tx_slots
[0] && mstb
->tx_slots
[1]) {
1415 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__
);
1418 if (mstb
->tx_slots
[0] == NULL
&& mstb
->tx_slots
[1] == NULL
) {
1419 txmsg
->seqno
= mstb
->last_seqno
;
1420 mstb
->last_seqno
^= 1;
1421 } else if (mstb
->tx_slots
[0] == NULL
)
1425 mstb
->tx_slots
[txmsg
->seqno
] = txmsg
;
1428 req_type
= txmsg
->msg
[0] & 0x7f;
1429 if (req_type
== DP_CONNECTION_STATUS_NOTIFY
||
1430 req_type
== DP_RESOURCE_STATUS_NOTIFY
)
1434 hdr
->path_msg
= txmsg
->path_msg
;
1435 hdr
->lct
= mstb
->lct
;
1436 hdr
->lcr
= mstb
->lct
- 1;
1438 memcpy(hdr
->rad
, mstb
->rad
, mstb
->lct
/ 2);
1439 hdr
->seqno
= txmsg
->seqno
;
1443 * process a single block of the next message in the sideband queue
1445 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
1446 struct drm_dp_sideband_msg_tx
*txmsg
,
1450 struct drm_dp_sideband_msg_hdr hdr
;
1451 int len
, space
, idx
, tosend
;
1454 memset(&hdr
, 0, sizeof(struct drm_dp_sideband_msg_hdr
));
1456 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
) {
1458 txmsg
->state
= DRM_DP_SIDEBAND_TX_START_SEND
;
1461 /* make hdr from dst mst - for replies use seqno
1462 otherwise assign one */
1463 ret
= set_hdr_from_dst_qlock(&hdr
, txmsg
);
1467 /* amount left to send in this message */
1468 len
= txmsg
->cur_len
- txmsg
->cur_offset
;
1470 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1471 space
= 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr
);
1473 tosend
= min(len
, space
);
1474 if (len
== txmsg
->cur_len
)
1480 hdr
.msg_len
= tosend
+ 1;
1481 drm_dp_encode_sideband_msg_hdr(&hdr
, chunk
, &idx
);
1482 memcpy(&chunk
[idx
], &txmsg
->msg
[txmsg
->cur_offset
], tosend
);
1483 /* add crc at end */
1484 drm_dp_crc_sideband_chunk_req(&chunk
[idx
], tosend
);
1487 ret
= drm_dp_send_sideband_msg(mgr
, up
, chunk
, idx
);
1489 DRM_DEBUG_KMS("sideband msg failed to send\n");
1493 txmsg
->cur_offset
+= tosend
;
1494 if (txmsg
->cur_offset
== txmsg
->cur_len
) {
1495 txmsg
->state
= DRM_DP_SIDEBAND_TX_SENT
;
1501 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
)
1503 struct drm_dp_sideband_msg_tx
*txmsg
;
1506 WARN_ON(!mutex_is_locked(&mgr
->qlock
));
1508 /* construct a chunk from the first msg in the tx_msg queue */
1509 if (list_empty(&mgr
->tx_msg_downq
))
1512 txmsg
= list_first_entry(&mgr
->tx_msg_downq
, struct drm_dp_sideband_msg_tx
, next
);
1513 ret
= process_single_tx_qlock(mgr
, txmsg
, false);
1515 /* txmsg is sent it should be in the slots now */
1516 list_del(&txmsg
->next
);
1518 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1519 list_del(&txmsg
->next
);
1520 if (txmsg
->seqno
!= -1)
1521 txmsg
->dst
->tx_slots
[txmsg
->seqno
] = NULL
;
1522 txmsg
->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
1523 wake_up(&mgr
->tx_waitq
);
1527 /* called holding qlock */
1528 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
1529 struct drm_dp_sideband_msg_tx
*txmsg
)
1533 /* construct a chunk from the first msg in the tx_msg queue */
1534 ret
= process_single_tx_qlock(mgr
, txmsg
, true);
1537 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1539 txmsg
->dst
->tx_slots
[txmsg
->seqno
] = NULL
;
1542 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr
*mgr
,
1543 struct drm_dp_sideband_msg_tx
*txmsg
)
1545 mutex_lock(&mgr
->qlock
);
1546 list_add_tail(&txmsg
->next
, &mgr
->tx_msg_downq
);
1547 if (list_is_singular(&mgr
->tx_msg_downq
))
1548 process_single_down_tx_qlock(mgr
);
1549 mutex_unlock(&mgr
->qlock
);
1552 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1553 struct drm_dp_mst_branch
*mstb
)
1556 struct drm_dp_sideband_msg_tx
*txmsg
;
1559 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1564 len
= build_link_address(txmsg
);
1566 mstb
->link_address_sent
= true;
1567 drm_dp_queue_down_tx(mgr
, txmsg
);
1569 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1573 if (txmsg
->reply
.reply_type
== 1)
1574 DRM_DEBUG_KMS("link address nak received\n");
1576 DRM_DEBUG_KMS("link address reply: %d\n", txmsg
->reply
.u
.link_addr
.nports
);
1577 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1578 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
,
1579 txmsg
->reply
.u
.link_addr
.ports
[i
].input_port
,
1580 txmsg
->reply
.u
.link_addr
.ports
[i
].peer_device_type
,
1581 txmsg
->reply
.u
.link_addr
.ports
[i
].port_number
,
1582 txmsg
->reply
.u
.link_addr
.ports
[i
].dpcd_revision
,
1583 txmsg
->reply
.u
.link_addr
.ports
[i
].mcs
,
1584 txmsg
->reply
.u
.link_addr
.ports
[i
].ddps
,
1585 txmsg
->reply
.u
.link_addr
.ports
[i
].legacy_device_plug_status
,
1586 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_streams
,
1587 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_stream_sinks
);
1590 drm_dp_check_mstb_guid(mstb
, txmsg
->reply
.u
.link_addr
.guid
);
1592 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1593 drm_dp_add_port(mstb
, mgr
->dev
, &txmsg
->reply
.u
.link_addr
.ports
[i
]);
1595 (*mgr
->cbs
->hotplug
)(mgr
);
1598 mstb
->link_address_sent
= false;
1599 DRM_DEBUG_KMS("link address failed %d\n", ret
);
1605 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
1606 struct drm_dp_mst_branch
*mstb
,
1607 struct drm_dp_mst_port
*port
)
1610 struct drm_dp_sideband_msg_tx
*txmsg
;
1613 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1618 len
= build_enum_path_resources(txmsg
, port
->port_num
);
1620 drm_dp_queue_down_tx(mgr
, txmsg
);
1622 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1624 if (txmsg
->reply
.reply_type
== 1)
1625 DRM_DEBUG_KMS("enum path resources nak received\n");
1627 if (port
->port_num
!= txmsg
->reply
.u
.path_resources
.port_number
)
1628 DRM_ERROR("got incorrect port in response\n");
1629 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
,
1630 txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
);
1631 port
->available_pbn
= txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
;
1639 static struct drm_dp_mst_port
*drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch
*mstb
)
1641 if (!mstb
->port_parent
)
1644 if (mstb
->port_parent
->mstb
!= mstb
)
1645 return mstb
->port_parent
;
1647 return drm_dp_get_last_connected_port_to_mstb(mstb
->port_parent
->parent
);
1650 static struct drm_dp_mst_branch
*drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr
*mgr
,
1651 struct drm_dp_mst_branch
*mstb
,
1654 struct drm_dp_mst_branch
*rmstb
= NULL
;
1655 struct drm_dp_mst_port
*found_port
;
1656 mutex_lock(&mgr
->lock
);
1657 if (mgr
->mst_primary
) {
1658 found_port
= drm_dp_get_last_connected_port_to_mstb(mstb
);
1661 rmstb
= found_port
->parent
;
1662 kref_get(&rmstb
->kref
);
1663 *port_num
= found_port
->port_num
;
1666 mutex_unlock(&mgr
->lock
);
1670 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1671 struct drm_dp_mst_port
*port
,
1675 struct drm_dp_sideband_msg_tx
*txmsg
;
1676 struct drm_dp_mst_branch
*mstb
;
1677 int len
, ret
, port_num
;
1678 u8 sinks
[DRM_DP_MAX_SDP_STREAMS
];
1681 port
= drm_dp_get_validated_port_ref(mgr
, port
);
1685 port_num
= port
->port_num
;
1686 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
1688 mstb
= drm_dp_get_last_connected_port_and_mstb(mgr
, port
->parent
, &port_num
);
1691 drm_dp_put_port(port
);
1696 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1702 for (i
= 0; i
< port
->num_sdp_streams
; i
++)
1706 len
= build_allocate_payload(txmsg
, port_num
,
1708 pbn
, port
->num_sdp_streams
, sinks
);
1710 drm_dp_queue_down_tx(mgr
, txmsg
);
1712 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1714 if (txmsg
->reply
.reply_type
== 1) {
1721 drm_dp_put_mst_branch_device(mstb
);
1722 drm_dp_put_port(port
);
1726 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1728 struct drm_dp_payload
*payload
)
1732 ret
= drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1734 payload
->payload_state
= 0;
1737 payload
->payload_state
= DP_PAYLOAD_LOCAL
;
1741 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1742 struct drm_dp_mst_port
*port
,
1744 struct drm_dp_payload
*payload
)
1747 ret
= drm_dp_payload_send_msg(mgr
, port
, id
, port
->vcpi
.pbn
);
1750 payload
->payload_state
= DP_PAYLOAD_REMOTE
;
1754 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1755 struct drm_dp_mst_port
*port
,
1757 struct drm_dp_payload
*payload
)
1759 DRM_DEBUG_KMS("\n");
1760 /* its okay for these to fail */
1762 drm_dp_payload_send_msg(mgr
, port
, id
, 0);
1765 drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1766 payload
->payload_state
= DP_PAYLOAD_DELETE_LOCAL
;
1770 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1772 struct drm_dp_payload
*payload
)
1774 payload
->payload_state
= 0;
1779 * drm_dp_update_payload_part1() - Execute payload update part 1
1780 * @mgr: manager to use.
1782 * This iterates over all proposed virtual channels, and tries to
1783 * allocate space in the link for them. For 0->slots transitions,
1784 * this step just writes the VCPI to the MST device. For slots->0
1785 * transitions, this writes the updated VCPIs and removes the
1786 * remote VC payloads.
1788 * after calling this the driver should generate ACT and payload
1791 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr
*mgr
)
1795 struct drm_dp_payload req_payload
;
1796 struct drm_dp_mst_port
*port
;
1798 mutex_lock(&mgr
->payload_lock
);
1799 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1800 /* solve the current payloads - compare to the hw ones
1801 - update the hw view */
1802 req_payload
.start_slot
= cur_slots
;
1803 if (mgr
->proposed_vcpis
[i
]) {
1804 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1805 port
= drm_dp_get_validated_port_ref(mgr
, port
);
1807 mutex_unlock(&mgr
->payload_lock
);
1810 req_payload
.num_slots
= mgr
->proposed_vcpis
[i
]->num_slots
;
1811 req_payload
.vcpi
= mgr
->proposed_vcpis
[i
]->vcpi
;
1814 req_payload
.num_slots
= 0;
1817 if (mgr
->payloads
[i
].start_slot
!= req_payload
.start_slot
) {
1818 mgr
->payloads
[i
].start_slot
= req_payload
.start_slot
;
1820 /* work out what is required to happen with this payload */
1821 if (mgr
->payloads
[i
].num_slots
!= req_payload
.num_slots
) {
1823 /* need to push an update for this payload */
1824 if (req_payload
.num_slots
) {
1825 drm_dp_create_payload_step1(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &req_payload
);
1826 mgr
->payloads
[i
].num_slots
= req_payload
.num_slots
;
1827 mgr
->payloads
[i
].vcpi
= req_payload
.vcpi
;
1828 } else if (mgr
->payloads
[i
].num_slots
) {
1829 mgr
->payloads
[i
].num_slots
= 0;
1830 drm_dp_destroy_payload_step1(mgr
, port
, mgr
->payloads
[i
].vcpi
, &mgr
->payloads
[i
]);
1831 req_payload
.payload_state
= mgr
->payloads
[i
].payload_state
;
1832 mgr
->payloads
[i
].start_slot
= 0;
1834 mgr
->payloads
[i
].payload_state
= req_payload
.payload_state
;
1836 cur_slots
+= req_payload
.num_slots
;
1839 drm_dp_put_port(port
);
1842 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1843 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1844 DRM_DEBUG_KMS("removing payload %d\n", i
);
1845 for (j
= i
; j
< mgr
->max_payloads
- 1; j
++) {
1846 memcpy(&mgr
->payloads
[j
], &mgr
->payloads
[j
+ 1], sizeof(struct drm_dp_payload
));
1847 mgr
->proposed_vcpis
[j
] = mgr
->proposed_vcpis
[j
+ 1];
1848 if (mgr
->proposed_vcpis
[j
] && mgr
->proposed_vcpis
[j
]->num_slots
) {
1849 set_bit(j
+ 1, &mgr
->payload_mask
);
1851 clear_bit(j
+ 1, &mgr
->payload_mask
);
1854 memset(&mgr
->payloads
[mgr
->max_payloads
- 1], 0, sizeof(struct drm_dp_payload
));
1855 mgr
->proposed_vcpis
[mgr
->max_payloads
- 1] = NULL
;
1856 clear_bit(mgr
->max_payloads
, &mgr
->payload_mask
);
1860 mutex_unlock(&mgr
->payload_lock
);
1864 EXPORT_SYMBOL(drm_dp_update_payload_part1
);
1867 * drm_dp_update_payload_part2() - Execute payload update part 2
1868 * @mgr: manager to use.
1870 * This iterates over all proposed virtual channels, and tries to
1871 * allocate space in the link for them. For 0->slots transitions,
1872 * this step writes the remote VC payload commands. For slots->0
1873 * this just resets some internal state.
1875 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr
*mgr
)
1877 struct drm_dp_mst_port
*port
;
1880 mutex_lock(&mgr
->payload_lock
);
1881 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1883 if (!mgr
->proposed_vcpis
[i
])
1886 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1888 DRM_DEBUG_KMS("payload %d %d\n", i
, mgr
->payloads
[i
].payload_state
);
1889 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_LOCAL
) {
1890 ret
= drm_dp_create_payload_step2(mgr
, port
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1891 } else if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1892 ret
= drm_dp_destroy_payload_step2(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1895 mutex_unlock(&mgr
->payload_lock
);
1899 mutex_unlock(&mgr
->payload_lock
);
1902 EXPORT_SYMBOL(drm_dp_update_payload_part2
);
1904 #if 0 /* unused as of yet */
1905 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr
*mgr
,
1906 struct drm_dp_mst_port
*port
,
1907 int offset
, int size
)
1910 struct drm_dp_sideband_msg_tx
*txmsg
;
1912 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1916 len
= build_dpcd_read(txmsg
, port
->port_num
, 0, 8);
1917 txmsg
->dst
= port
->parent
;
1919 drm_dp_queue_down_tx(mgr
, txmsg
);
1925 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
1926 struct drm_dp_mst_port
*port
,
1927 int offset
, int size
, u8
*bytes
)
1931 struct drm_dp_sideband_msg_tx
*txmsg
;
1932 struct drm_dp_mst_branch
*mstb
;
1934 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
1938 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1944 len
= build_dpcd_write(txmsg
, port
->port_num
, offset
, size
, bytes
);
1947 drm_dp_queue_down_tx(mgr
, txmsg
);
1949 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1951 if (txmsg
->reply
.reply_type
== 1) {
1958 drm_dp_put_mst_branch_device(mstb
);
1962 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx
*msg
, u8 req_type
)
1964 struct drm_dp_sideband_msg_reply_body reply
;
1966 reply
.reply_type
= 0;
1967 reply
.req_type
= req_type
;
1968 drm_dp_encode_sideband_reply(&reply
, msg
);
1972 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr
*mgr
,
1973 struct drm_dp_mst_branch
*mstb
,
1974 int req_type
, int seqno
, bool broadcast
)
1976 struct drm_dp_sideband_msg_tx
*txmsg
;
1978 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1983 txmsg
->seqno
= seqno
;
1984 drm_dp_encode_up_ack_reply(txmsg
, req_type
);
1986 mutex_lock(&mgr
->qlock
);
1988 process_single_up_tx_qlock(mgr
, txmsg
);
1990 mutex_unlock(&mgr
->qlock
);
1996 static bool drm_dp_get_vc_payload_bw(int dp_link_bw
,
2000 switch (dp_link_bw
) {
2002 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2003 dp_link_bw
, dp_link_count
);
2006 case DP_LINK_BW_1_62
:
2007 *out
= 3 * dp_link_count
;
2009 case DP_LINK_BW_2_7
:
2010 *out
= 5 * dp_link_count
;
2012 case DP_LINK_BW_5_4
:
2013 *out
= 10 * dp_link_count
;
2020 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2021 * @mgr: manager to set state for
2022 * @mst_state: true to enable MST on this connector - false to disable.
2024 * This is called by the driver when it detects an MST capable device plugged
2025 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2027 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr
*mgr
, bool mst_state
)
2030 struct drm_dp_mst_branch
*mstb
= NULL
;
2032 mutex_lock(&mgr
->lock
);
2033 if (mst_state
== mgr
->mst_state
)
2036 mgr
->mst_state
= mst_state
;
2037 /* set the device into MST mode */
2039 WARN_ON(mgr
->mst_primary
);
2042 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
2043 if (ret
!= DP_RECEIVER_CAP_SIZE
) {
2044 DRM_DEBUG_KMS("failed to read DPCD\n");
2048 if (!drm_dp_get_vc_payload_bw(mgr
->dpcd
[1],
2049 mgr
->dpcd
[2] & DP_MAX_LANE_COUNT_MASK
,
2055 mgr
->total_pbn
= 2560;
2056 mgr
->total_slots
= DIV_ROUND_UP(mgr
->total_pbn
, mgr
->pbn_div
);
2057 mgr
->avail_slots
= mgr
->total_slots
;
2059 /* add initial branch device at LCT 1 */
2060 mstb
= drm_dp_add_mst_branch_device(1, NULL
);
2067 /* give this the main reference */
2068 mgr
->mst_primary
= mstb
;
2069 kref_get(&mgr
->mst_primary
->kref
);
2071 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2072 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
2078 struct drm_dp_payload reset_pay
;
2079 reset_pay
.start_slot
= 0;
2080 reset_pay
.num_slots
= 0x3f;
2081 drm_dp_dpcd_write_payload(mgr
, 0, &reset_pay
);
2084 queue_work(system_long_wq
, &mgr
->work
);
2088 /* disable MST on the device */
2089 mstb
= mgr
->mst_primary
;
2090 mgr
->mst_primary
= NULL
;
2091 /* this can fail if the device is gone */
2092 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
, 0);
2094 memset(mgr
->payloads
, 0, mgr
->max_payloads
* sizeof(struct drm_dp_payload
));
2095 mgr
->payload_mask
= 0;
2096 set_bit(0, &mgr
->payload_mask
);
2101 mutex_unlock(&mgr
->lock
);
2103 drm_dp_put_mst_branch_device(mstb
);
2107 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst
);
2110 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2111 * @mgr: manager to suspend
2113 * This function tells the MST device that we can't handle UP messages
2114 * anymore. This should stop it from sending any since we are suspended.
2116 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr
*mgr
)
2118 mutex_lock(&mgr
->lock
);
2119 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2120 DP_MST_EN
| DP_UPSTREAM_IS_SRC
);
2121 mutex_unlock(&mgr
->lock
);
2122 flush_work(&mgr
->work
);
2123 flush_work(&mgr
->destroy_connector_work
);
2125 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend
);
2128 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2129 * @mgr: manager to resume
2131 * This will fetch DPCD and see if the device is still there,
2132 * if it is, it will rewrite the MSTM control bits, and return.
2134 * if the device fails this returns -1, and the driver should do
2135 * a full MST reprobe, in case we were undocked.
2137 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr
*mgr
)
2141 mutex_lock(&mgr
->lock
);
2143 if (mgr
->mst_primary
) {
2147 sret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
2148 if (sret
!= DP_RECEIVER_CAP_SIZE
) {
2149 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2154 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2155 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
2157 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2162 /* Some hubs forget their guids after they resume */
2163 sret
= drm_dp_dpcd_read(mgr
->aux
, DP_GUID
, guid
, 16);
2165 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2169 drm_dp_check_mstb_guid(mgr
->mst_primary
, guid
);
2176 mutex_unlock(&mgr
->lock
);
2179 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume
);
2181 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr
*mgr
, bool up
)
2185 int replylen
, origlen
, curreply
;
2187 struct drm_dp_sideband_msg_rx
*msg
;
2188 int basereg
= up
? DP_SIDEBAND_MSG_UP_REQ_BASE
: DP_SIDEBAND_MSG_DOWN_REP_BASE
;
2189 msg
= up
? &mgr
->up_req_recv
: &mgr
->down_rep_recv
;
2191 len
= min(mgr
->max_dpcd_transaction_bytes
, 16);
2192 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
,
2195 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len
, ret
);
2198 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, true);
2200 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock
[0]);
2203 replylen
= msg
->curchunk_len
+ msg
->curchunk_hdrlen
;
2208 while (replylen
> 0) {
2209 len
= min3(replylen
, mgr
->max_dpcd_transaction_bytes
, 16);
2210 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
+ curreply
,
2213 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2218 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, false);
2220 DRM_DEBUG_KMS("failed to build sideband msg\n");
2230 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr
*mgr
)
2234 if (!drm_dp_get_one_sb_msg(mgr
, false)) {
2235 memset(&mgr
->down_rep_recv
, 0,
2236 sizeof(struct drm_dp_sideband_msg_rx
));
2240 if (mgr
->down_rep_recv
.have_eomt
) {
2241 struct drm_dp_sideband_msg_tx
*txmsg
;
2242 struct drm_dp_mst_branch
*mstb
;
2244 mstb
= drm_dp_get_mst_branch_device(mgr
,
2245 mgr
->down_rep_recv
.initial_hdr
.lct
,
2246 mgr
->down_rep_recv
.initial_hdr
.rad
);
2249 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->down_rep_recv
.initial_hdr
.lct
);
2250 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2254 /* find the message */
2255 slot
= mgr
->down_rep_recv
.initial_hdr
.seqno
;
2256 mutex_lock(&mgr
->qlock
);
2257 txmsg
= mstb
->tx_slots
[slot
];
2258 /* remove from slots */
2259 mutex_unlock(&mgr
->qlock
);
2262 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2264 mgr
->down_rep_recv
.initial_hdr
.seqno
,
2265 mgr
->down_rep_recv
.initial_hdr
.lct
,
2266 mgr
->down_rep_recv
.initial_hdr
.rad
[0],
2267 mgr
->down_rep_recv
.msg
[0]);
2268 drm_dp_put_mst_branch_device(mstb
);
2269 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2273 drm_dp_sideband_parse_reply(&mgr
->down_rep_recv
, &txmsg
->reply
);
2274 if (txmsg
->reply
.reply_type
== 1) {
2275 DRM_DEBUG_KMS("Got NAK reply: req 0x%02x, reason 0x%02x, nak data 0x%02x\n", txmsg
->reply
.req_type
, txmsg
->reply
.u
.nak
.reason
, txmsg
->reply
.u
.nak
.nak_data
);
2278 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2279 drm_dp_put_mst_branch_device(mstb
);
2281 mutex_lock(&mgr
->qlock
);
2282 txmsg
->state
= DRM_DP_SIDEBAND_TX_RX
;
2283 mstb
->tx_slots
[slot
] = NULL
;
2284 mutex_unlock(&mgr
->qlock
);
2286 wake_up(&mgr
->tx_waitq
);
2291 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr
*mgr
)
2295 if (!drm_dp_get_one_sb_msg(mgr
, true)) {
2296 memset(&mgr
->up_req_recv
, 0,
2297 sizeof(struct drm_dp_sideband_msg_rx
));
2301 if (mgr
->up_req_recv
.have_eomt
) {
2302 struct drm_dp_sideband_msg_req_body msg
;
2303 struct drm_dp_mst_branch
*mstb
= NULL
;
2306 if (!mgr
->up_req_recv
.initial_hdr
.broadcast
) {
2307 mstb
= drm_dp_get_mst_branch_device(mgr
,
2308 mgr
->up_req_recv
.initial_hdr
.lct
,
2309 mgr
->up_req_recv
.initial_hdr
.rad
);
2311 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2312 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2317 seqno
= mgr
->up_req_recv
.initial_hdr
.seqno
;
2318 drm_dp_sideband_parse_req(&mgr
->up_req_recv
, &msg
);
2320 if (msg
.req_type
== DP_CONNECTION_STATUS_NOTIFY
) {
2321 drm_dp_send_up_ack_reply(mgr
, mgr
->mst_primary
, msg
.req_type
, seqno
, false);
2324 mstb
= drm_dp_get_mst_branch_device_by_guid(mgr
, msg
.u
.conn_stat
.guid
);
2327 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2328 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2332 drm_dp_update_port(mstb
, &msg
.u
.conn_stat
);
2334 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
);
2335 (*mgr
->cbs
->hotplug
)(mgr
);
2337 } else if (msg
.req_type
== DP_RESOURCE_STATUS_NOTIFY
) {
2338 drm_dp_send_up_ack_reply(mgr
, mgr
->mst_primary
, msg
.req_type
, seqno
, false);
2340 mstb
= drm_dp_get_mst_branch_device_by_guid(mgr
, msg
.u
.resource_stat
.guid
);
2343 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2344 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2348 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg
.u
.resource_stat
.port_number
, msg
.u
.resource_stat
.available_pbn
);
2352 drm_dp_put_mst_branch_device(mstb
);
2354 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2360 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2361 * @mgr: manager to notify irq for.
2362 * @esi: 4 bytes from SINK_COUNT_ESI
2363 * @handled: whether the hpd interrupt was consumed or not
2365 * This should be called from the driver when it detects a short IRQ,
2366 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2367 * topology manager will process the sideband messages received as a result
2370 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr
*mgr
, u8
*esi
, bool *handled
)
2377 if (sc
!= mgr
->sink_count
) {
2378 mgr
->sink_count
= sc
;
2382 if (esi
[1] & DP_DOWN_REP_MSG_RDY
) {
2383 ret
= drm_dp_mst_handle_down_rep(mgr
);
2387 if (esi
[1] & DP_UP_REQ_MSG_RDY
) {
2388 ret
|= drm_dp_mst_handle_up_req(mgr
);
2392 drm_dp_mst_kick_tx(mgr
);
2395 EXPORT_SYMBOL(drm_dp_mst_hpd_irq
);
2398 * drm_dp_mst_detect_port() - get connection status for an MST port
2399 * @connector: DRM connector for this port
2400 * @mgr: manager for this port
2401 * @port: unverified pointer to a port
2403 * This returns the current connection state for a port. It validates the
2404 * port pointer still exists so the caller doesn't require a reference
2406 enum drm_connector_status
drm_dp_mst_detect_port(struct drm_connector
*connector
,
2407 struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2409 enum drm_connector_status status
= connector_status_disconnected
;
2411 /* we need to search for the port in the mgr in case its gone */
2412 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2414 return connector_status_disconnected
;
2419 switch (port
->pdt
) {
2420 case DP_PEER_DEVICE_NONE
:
2421 case DP_PEER_DEVICE_MST_BRANCHING
:
2424 case DP_PEER_DEVICE_SST_SINK
:
2425 status
= connector_status_connected
;
2426 /* for logical ports - cache the EDID */
2427 if (port
->port_num
>= 8 && !port
->cached_edid
) {
2428 port
->cached_edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2431 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
2433 status
= connector_status_connected
;
2437 drm_dp_put_port(port
);
2440 EXPORT_SYMBOL(drm_dp_mst_detect_port
);
2443 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2444 * @mgr: manager for this port
2445 * @port: unverified pointer to a port.
2447 * This returns whether the port supports audio or not.
2449 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr
*mgr
,
2450 struct drm_dp_mst_port
*port
)
2454 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2457 ret
= port
->has_audio
;
2458 drm_dp_put_port(port
);
2461 EXPORT_SYMBOL(drm_dp_mst_port_has_audio
);
2464 * drm_dp_mst_get_edid() - get EDID for an MST port
2465 * @connector: toplevel connector to get EDID for
2466 * @mgr: manager for this port
2467 * @port: unverified pointer to a port.
2469 * This returns an EDID for the port connected to a connector,
2470 * It validates the pointer still exists so the caller doesn't require a
2473 struct edid
*drm_dp_mst_get_edid(struct drm_connector
*connector
, struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2475 struct edid
*edid
= NULL
;
2477 /* we need to search for the port in the mgr in case its gone */
2478 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2482 if (port
->cached_edid
)
2483 edid
= drm_edid_duplicate(port
->cached_edid
);
2485 edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2486 drm_mode_connector_set_tile_property(connector
);
2488 port
->has_audio
= drm_detect_monitor_audio(edid
);
2489 drm_dp_put_port(port
);
2492 EXPORT_SYMBOL(drm_dp_mst_get_edid
);
2495 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2496 * @mgr: manager to use
2497 * @pbn: payload bandwidth to convert into slots.
2499 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
,
2504 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2506 if (num_slots
> mgr
->avail_slots
)
2510 EXPORT_SYMBOL(drm_dp_find_vcpi_slots
);
2512 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
2513 struct drm_dp_vcpi
*vcpi
, int pbn
)
2518 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2520 if (num_slots
> mgr
->avail_slots
)
2524 vcpi
->aligned_pbn
= num_slots
* mgr
->pbn_div
;
2525 vcpi
->num_slots
= num_slots
;
2527 ret
= drm_dp_mst_assign_payload_id(mgr
, vcpi
);
2534 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2535 * @mgr: manager for this port
2536 * @port: port to allocate a virtual channel for.
2537 * @pbn: payload bandwidth number to request
2538 * @slots: returned number of slots for this PBN.
2540 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
, int pbn
, int *slots
)
2544 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2548 if (port
->vcpi
.vcpi
> 0) {
2549 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port
->vcpi
.vcpi
, port
->vcpi
.pbn
, pbn
);
2550 if (pbn
== port
->vcpi
.pbn
) {
2551 *slots
= port
->vcpi
.num_slots
;
2552 drm_dp_put_port(port
);
2557 ret
= drm_dp_init_vcpi(mgr
, &port
->vcpi
, pbn
);
2559 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn
, mgr
->pbn_div
), mgr
->avail_slots
, ret
);
2562 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn
, port
->vcpi
.num_slots
);
2563 *slots
= port
->vcpi
.num_slots
;
2565 drm_dp_put_port(port
);
2570 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi
);
2572 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2575 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2579 slots
= port
->vcpi
.num_slots
;
2580 drm_dp_put_port(port
);
2583 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots
);
2586 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2587 * @mgr: manager for this port
2588 * @port: unverified pointer to a port.
2590 * This just resets the number of slots for the ports VCPI for later programming.
2592 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2594 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2597 port
->vcpi
.num_slots
= 0;
2598 drm_dp_put_port(port
);
2600 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots
);
2603 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2604 * @mgr: manager for this port
2605 * @port: unverified port to deallocate vcpi for
2607 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2609 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2613 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
2614 port
->vcpi
.num_slots
= 0;
2616 port
->vcpi
.aligned_pbn
= 0;
2617 port
->vcpi
.vcpi
= 0;
2618 drm_dp_put_port(port
);
2620 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi
);
2622 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
2623 int id
, struct drm_dp_payload
*payload
)
2625 u8 payload_alloc
[3], status
;
2629 drm_dp_dpcd_writeb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
,
2630 DP_PAYLOAD_TABLE_UPDATED
);
2632 payload_alloc
[0] = id
;
2633 payload_alloc
[1] = payload
->start_slot
;
2634 payload_alloc
[2] = payload
->num_slots
;
2636 ret
= drm_dp_dpcd_write(mgr
->aux
, DP_PAYLOAD_ALLOCATE_SET
, payload_alloc
, 3);
2638 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret
);
2643 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2645 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
2649 if (!(status
& DP_PAYLOAD_TABLE_UPDATED
)) {
2652 usleep_range(10000, 20000);
2655 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status
);
2666 * drm_dp_check_act_status() - Check ACT handled status.
2667 * @mgr: manager to use
2669 * Check the payload status bits in the DPCD for ACT handled completion.
2671 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr
*mgr
)
2678 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2681 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
2685 if (status
& DP_PAYLOAD_ACT_HANDLED
)
2690 } while (count
< 30);
2692 if (!(status
& DP_PAYLOAD_ACT_HANDLED
)) {
2693 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status
, count
);
2701 EXPORT_SYMBOL(drm_dp_check_act_status
);
2704 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2705 * @clock: dot clock for the mode
2706 * @bpp: bpp for the mode.
2708 * This uses the formula in the spec to calculate the PBN value for a mode.
2710 int drm_dp_calc_pbn_mode(int clock
, int bpp
)
2720 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2721 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2722 * common multiplier to render an integer PBN for all link rate/lane
2723 * counts combinations
2725 * peak_kbps *= (1006/1000)
2726 * peak_kbps *= (64/54)
2727 * peak_kbps *= 8 convert to bytes
2730 numerator
= 64 * 1006;
2731 denominator
= 54 * 8 * 1000 * 1000;
2734 peak_kbps
= drm_fixp_from_fraction(kbps
, denominator
);
2736 return drm_fixp2int_ceil(peak_kbps
);
2738 EXPORT_SYMBOL(drm_dp_calc_pbn_mode
);
2740 static int test_calc_pbn_mode(void)
2743 ret
= drm_dp_calc_pbn_mode(154000, 30);
2745 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2746 154000, 30, 689, ret
);
2749 ret
= drm_dp_calc_pbn_mode(234000, 30);
2751 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2752 234000, 30, 1047, ret
);
2755 ret
= drm_dp_calc_pbn_mode(297000, 24);
2757 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2758 297000, 24, 1063, ret
);
2764 /* we want to kick the TX after we've ack the up/down IRQs. */
2765 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
)
2767 queue_work(system_long_wq
, &mgr
->tx_work
);
2770 static void drm_dp_mst_dump_mstb(struct seq_file
*m
,
2771 struct drm_dp_mst_branch
*mstb
)
2773 struct drm_dp_mst_port
*port
;
2774 int tabs
= mstb
->lct
;
2778 for (i
= 0; i
< tabs
; i
++)
2782 seq_printf(m
, "%smst: %p, %d\n", prefix
, mstb
, mstb
->num_ports
);
2783 list_for_each_entry(port
, &mstb
->ports
, next
) {
2784 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
);
2786 drm_dp_mst_dump_mstb(m
, port
->mstb
);
2790 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
2795 for (i
= 0; i
< 4; i
++) {
2796 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
+ (i
* 16), &buf
[i
* 16], 16);
2805 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr
*mgr
,
2806 struct drm_dp_mst_port
*port
, char *name
,
2809 struct edid
*mst_edid
;
2811 mst_edid
= drm_dp_mst_get_edid(port
->connector
, mgr
, port
);
2812 drm_edid_get_monitor_name(mst_edid
, name
, namelen
);
2816 * drm_dp_mst_dump_topology(): dump topology to seq file.
2817 * @m: seq_file to dump output to
2818 * @mgr: manager to dump current topology for.
2820 * helper to dump MST topology to a seq file for debugfs.
2822 void drm_dp_mst_dump_topology(struct seq_file
*m
,
2823 struct drm_dp_mst_topology_mgr
*mgr
)
2826 struct drm_dp_mst_port
*port
;
2828 mutex_lock(&mgr
->lock
);
2829 if (mgr
->mst_primary
)
2830 drm_dp_mst_dump_mstb(m
, mgr
->mst_primary
);
2833 mutex_unlock(&mgr
->lock
);
2835 mutex_lock(&mgr
->payload_lock
);
2836 seq_printf(m
, "vcpi: %lx %lx %d\n", mgr
->payload_mask
, mgr
->vcpi_mask
,
2839 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
2840 if (mgr
->proposed_vcpis
[i
]) {
2843 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
2844 fetch_monitor_name(mgr
, port
, name
, sizeof(name
));
2845 seq_printf(m
, "vcpi %d: %d %d %d sink name: %s\n", i
,
2846 port
->port_num
, port
->vcpi
.vcpi
,
2847 port
->vcpi
.num_slots
,
2848 (*name
!= 0) ? name
: "Unknown");
2850 seq_printf(m
, "vcpi %d:unused\n", i
);
2852 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
2853 seq_printf(m
, "payload %d: %d, %d, %d\n",
2855 mgr
->payloads
[i
].payload_state
,
2856 mgr
->payloads
[i
].start_slot
,
2857 mgr
->payloads
[i
].num_slots
);
2861 mutex_unlock(&mgr
->payload_lock
);
2863 mutex_lock(&mgr
->lock
);
2864 if (mgr
->mst_primary
) {
2868 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, buf
, DP_RECEIVER_CAP_SIZE
);
2869 seq_printf(m
, "dpcd: ");
2870 for (i
= 0; i
< DP_RECEIVER_CAP_SIZE
; i
++)
2871 seq_printf(m
, "%02x ", buf
[i
]);
2872 seq_printf(m
, "\n");
2873 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_FAUX_CAP
, buf
, 2);
2874 seq_printf(m
, "faux/mst: ");
2875 for (i
= 0; i
< 2; i
++)
2876 seq_printf(m
, "%02x ", buf
[i
]);
2877 seq_printf(m
, "\n");
2878 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_MSTM_CTRL
, buf
, 1);
2879 seq_printf(m
, "mst ctrl: ");
2880 for (i
= 0; i
< 1; i
++)
2881 seq_printf(m
, "%02x ", buf
[i
]);
2882 seq_printf(m
, "\n");
2884 /* dump the standard OUI branch header */
2885 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_BRANCH_OUI
, buf
, DP_BRANCH_OUI_HEADER_SIZE
);
2886 seq_printf(m
, "branch oui: ");
2887 for (i
= 0; i
< 0x3; i
++)
2888 seq_printf(m
, "%02x", buf
[i
]);
2889 seq_printf(m
, " devid: ");
2890 for (i
= 0x3; i
< 0x8 && buf
[i
]; i
++)
2891 seq_printf(m
, "%c", buf
[i
]);
2893 seq_printf(m
, " revision: hw: %x.%x sw: %x.%x", buf
[0x9] >> 4, buf
[0x9] & 0xf, buf
[0xa], buf
[0xb]);
2894 seq_printf(m
, "\n");
2895 bret
= dump_dp_payload_table(mgr
, buf
);
2897 seq_printf(m
, "payload table: ");
2898 for (i
= 0; i
< 63; i
++)
2899 seq_printf(m
, "%02x ", buf
[i
]);
2900 seq_printf(m
, "\n");
2905 mutex_unlock(&mgr
->lock
);
2908 EXPORT_SYMBOL(drm_dp_mst_dump_topology
);
2910 static void drm_dp_tx_work(struct work_struct
*work
)
2912 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, tx_work
);
2914 mutex_lock(&mgr
->qlock
);
2915 if (!list_empty(&mgr
->tx_msg_downq
))
2916 process_single_down_tx_qlock(mgr
);
2917 mutex_unlock(&mgr
->qlock
);
2920 static void drm_dp_free_mst_port(struct kref
*kref
)
2922 struct drm_dp_mst_port
*port
= container_of(kref
, struct drm_dp_mst_port
, kref
);
2923 kref_put(&port
->parent
->kref
, drm_dp_free_mst_branch_device
);
2927 static void drm_dp_destroy_connector_work(struct work_struct
*work
)
2929 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, destroy_connector_work
);
2930 struct drm_dp_mst_port
*port
;
2931 bool send_hotplug
= false;
2933 * Not a regular list traverse as we have to drop the destroy
2934 * connector lock before destroying the connector, to avoid AB->BA
2935 * ordering between this lock and the config mutex.
2938 mutex_lock(&mgr
->destroy_connector_lock
);
2939 port
= list_first_entry_or_null(&mgr
->destroy_connector_list
, struct drm_dp_mst_port
, next
);
2941 mutex_unlock(&mgr
->destroy_connector_lock
);
2944 list_del(&port
->next
);
2945 mutex_unlock(&mgr
->destroy_connector_lock
);
2947 kref_init(&port
->kref
);
2948 INIT_LIST_HEAD(&port
->next
);
2950 mgr
->cbs
->destroy_connector(mgr
, port
->connector
);
2952 drm_dp_port_teardown_pdt(port
, port
->pdt
);
2953 port
->pdt
= DP_PEER_DEVICE_NONE
;
2955 if (!port
->input
&& port
->vcpi
.vcpi
> 0) {
2956 drm_dp_mst_reset_vcpi_slots(mgr
, port
);
2957 drm_dp_update_payload_part1(mgr
);
2958 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
2961 kref_put(&port
->kref
, drm_dp_free_mst_port
);
2962 send_hotplug
= true;
2965 (*mgr
->cbs
->hotplug
)(mgr
);
2969 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2970 * @mgr: manager struct to initialise
2971 * @dev: device providing this structure - for i2c addition.
2972 * @aux: DP helper aux channel to talk to this device
2973 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2974 * @max_payloads: maximum number of payloads this GPU can source
2975 * @conn_base_id: the connector object ID the MST device is connected to.
2977 * Return 0 for success, or negative error code on failure
2979 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr
*mgr
,
2980 struct device
*dev
, struct drm_dp_aux
*aux
,
2981 int max_dpcd_transaction_bytes
,
2982 int max_payloads
, int conn_base_id
)
2984 mutex_init(&mgr
->lock
);
2985 mutex_init(&mgr
->qlock
);
2986 mutex_init(&mgr
->payload_lock
);
2987 mutex_init(&mgr
->destroy_connector_lock
);
2988 INIT_LIST_HEAD(&mgr
->tx_msg_downq
);
2989 INIT_LIST_HEAD(&mgr
->destroy_connector_list
);
2990 INIT_WORK(&mgr
->work
, drm_dp_mst_link_probe_work
);
2991 INIT_WORK(&mgr
->tx_work
, drm_dp_tx_work
);
2992 INIT_WORK(&mgr
->destroy_connector_work
, drm_dp_destroy_connector_work
);
2993 init_waitqueue_head(&mgr
->tx_waitq
);
2996 mgr
->max_dpcd_transaction_bytes
= max_dpcd_transaction_bytes
;
2997 mgr
->max_payloads
= max_payloads
;
2998 mgr
->conn_base_id
= conn_base_id
;
2999 if (max_payloads
+ 1 > sizeof(mgr
->payload_mask
) * 8 ||
3000 max_payloads
+ 1 > sizeof(mgr
->vcpi_mask
) * 8)
3002 mgr
->payloads
= kcalloc(max_payloads
, sizeof(struct drm_dp_payload
), GFP_KERNEL
);
3005 mgr
->proposed_vcpis
= kcalloc(max_payloads
, sizeof(struct drm_dp_vcpi
*), GFP_KERNEL
);
3006 if (!mgr
->proposed_vcpis
)
3008 set_bit(0, &mgr
->payload_mask
);
3009 if (test_calc_pbn_mode() < 0)
3010 DRM_ERROR("MST PBN self-test failed\n");
3014 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init
);
3017 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3018 * @mgr: manager to destroy
3020 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr
*mgr
)
3022 flush_work(&mgr
->work
);
3023 flush_work(&mgr
->destroy_connector_work
);
3024 mutex_lock(&mgr
->payload_lock
);
3025 kfree(mgr
->payloads
);
3026 mgr
->payloads
= NULL
;
3027 kfree(mgr
->proposed_vcpis
);
3028 mgr
->proposed_vcpis
= NULL
;
3029 mutex_unlock(&mgr
->payload_lock
);
3033 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy
);
3036 static int drm_dp_mst_i2c_xfer(struct i2c_adapter
*adapter
, struct i2c_msg
*msgs
,
3039 struct drm_dp_aux
*aux
= adapter
->algo_data
;
3040 struct drm_dp_mst_port
*port
= container_of(aux
, struct drm_dp_mst_port
, aux
);
3041 struct drm_dp_mst_branch
*mstb
;
3042 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
3044 bool reading
= false;
3045 struct drm_dp_sideband_msg_req_body msg
;
3046 struct drm_dp_sideband_msg_tx
*txmsg
= NULL
;
3049 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
3053 /* construct i2c msg */
3054 /* see if last msg is a read */
3055 if (msgs
[num
- 1].flags
& I2C_M_RD
)
3058 if (!reading
|| (num
- 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS
)) {
3059 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3064 memset(&msg
, 0, sizeof(msg
));
3065 msg
.req_type
= DP_REMOTE_I2C_READ
;
3066 msg
.u
.i2c_read
.num_transactions
= num
- 1;
3067 msg
.u
.i2c_read
.port_number
= port
->port_num
;
3068 for (i
= 0; i
< num
- 1; i
++) {
3069 msg
.u
.i2c_read
.transactions
[i
].i2c_dev_id
= msgs
[i
].addr
;
3070 msg
.u
.i2c_read
.transactions
[i
].num_bytes
= msgs
[i
].len
;
3071 msg
.u
.i2c_read
.transactions
[i
].bytes
= msgs
[i
].buf
;
3073 msg
.u
.i2c_read
.read_i2c_device_id
= msgs
[num
- 1].addr
;
3074 msg
.u
.i2c_read
.num_bytes_read
= msgs
[num
- 1].len
;
3076 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3083 drm_dp_encode_sideband_req(&msg
, txmsg
);
3085 drm_dp_queue_down_tx(mgr
, txmsg
);
3087 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3090 if (txmsg
->reply
.reply_type
== 1) { /* got a NAK back */
3094 if (txmsg
->reply
.u
.remote_i2c_read_ack
.num_bytes
!= msgs
[num
- 1].len
) {
3098 memcpy(msgs
[num
- 1].buf
, txmsg
->reply
.u
.remote_i2c_read_ack
.bytes
, msgs
[num
- 1].len
);
3103 drm_dp_put_mst_branch_device(mstb
);
3107 static u32
drm_dp_mst_i2c_functionality(struct i2c_adapter
*adapter
)
3109 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
3110 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
3111 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
3112 I2C_FUNC_10BIT_ADDR
;
3115 static const struct i2c_algorithm drm_dp_mst_i2c_algo
= {
3116 .functionality
= drm_dp_mst_i2c_functionality
,
3117 .master_xfer
= drm_dp_mst_i2c_xfer
,
3121 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3122 * @aux: DisplayPort AUX channel
3124 * Returns 0 on success or a negative error code on failure.
3126 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux
*aux
)
3128 aux
->ddc
.algo
= &drm_dp_mst_i2c_algo
;
3129 aux
->ddc
.algo_data
= aux
;
3130 aux
->ddc
.retries
= 3;
3132 aux
->ddc
.class = I2C_CLASS_DDC
;
3133 aux
->ddc
.owner
= THIS_MODULE
;
3134 aux
->ddc
.dev
.parent
= aux
->dev
;
3135 aux
->ddc
.dev
.of_node
= aux
->dev
->of_node
;
3137 strlcpy(aux
->ddc
.name
, aux
->name
? aux
->name
: dev_name(aux
->dev
),
3138 sizeof(aux
->ddc
.name
));
3140 return i2c_add_adapter(&aux
->ddc
);
3144 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3145 * @aux: DisplayPort AUX channel
3147 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux
*aux
)
3149 i2c_del_adapter(&aux
->ddc
);