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/errno.h>
26 #include <linux/sched.h>
27 #include <linux/seq_file.h>
28 #include <linux/i2c.h>
29 #include <drm/drm_dp_mst_helper.h>
32 #include <drm/drm_fixed.h>
37 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
38 * protocol. The helpers contain a topology manager and bandwidth manager.
39 * The helpers encapsulate the sending and received of sideband msgs.
41 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
43 static int test_calc_pbn_mode(void);
45 static void drm_dp_put_port(struct drm_dp_mst_port
*port
);
47 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
49 struct drm_dp_payload
*payload
);
51 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
52 struct drm_dp_mst_port
*port
,
53 int offset
, int size
, u8
*bytes
);
55 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
56 struct drm_dp_mst_branch
*mstb
);
57 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
58 struct drm_dp_mst_branch
*mstb
,
59 struct drm_dp_mst_port
*port
);
60 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
63 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux
*aux
);
64 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux
*aux
);
65 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
);
66 /* sideband msg handling */
67 static u8
drm_dp_msg_header_crc4(const uint8_t *data
, size_t num_nibbles
)
72 int number_of_bits
= num_nibbles
* 4;
75 while (number_of_bits
!= 0) {
78 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
86 if ((remainder
& 0x10) == 0x10)
91 while (number_of_bits
!= 0) {
94 if ((remainder
& 0x10) != 0)
101 static u8
drm_dp_msg_data_crc4(const uint8_t *data
, u8 number_of_bytes
)
106 int number_of_bits
= number_of_bytes
* 8;
109 while (number_of_bits
!= 0) {
112 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
120 if ((remainder
& 0x100) == 0x100)
125 while (number_of_bits
!= 0) {
128 if ((remainder
& 0x100) != 0)
132 return remainder
& 0xff;
134 static inline u8
drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr
*hdr
)
137 size
+= (hdr
->lct
/ 2);
141 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
147 buf
[idx
++] = ((hdr
->lct
& 0xf) << 4) | (hdr
->lcr
& 0xf);
148 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
149 buf
[idx
++] = hdr
->rad
[i
];
150 buf
[idx
++] = (hdr
->broadcast
<< 7) | (hdr
->path_msg
<< 6) |
151 (hdr
->msg_len
& 0x3f);
152 buf
[idx
++] = (hdr
->somt
<< 7) | (hdr
->eomt
<< 6) | (hdr
->seqno
<< 4);
154 crc4
= drm_dp_msg_header_crc4(buf
, (idx
* 2) - 1);
155 buf
[idx
- 1] |= (crc4
& 0xf);
160 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
161 u8
*buf
, int buflen
, u8
*hdrlen
)
170 len
+= ((buf
[0] & 0xf0) >> 4) / 2;
173 crc4
= drm_dp_msg_header_crc4(buf
, (len
* 2) - 1);
175 if ((crc4
& 0xf) != (buf
[len
- 1] & 0xf)) {
176 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4
, buf
[len
- 1]);
180 hdr
->lct
= (buf
[0] & 0xf0) >> 4;
181 hdr
->lcr
= (buf
[0] & 0xf);
183 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
184 hdr
->rad
[i
] = buf
[idx
++];
185 hdr
->broadcast
= (buf
[idx
] >> 7) & 0x1;
186 hdr
->path_msg
= (buf
[idx
] >> 6) & 0x1;
187 hdr
->msg_len
= buf
[idx
] & 0x3f;
189 hdr
->somt
= (buf
[idx
] >> 7) & 0x1;
190 hdr
->eomt
= (buf
[idx
] >> 6) & 0x1;
191 hdr
->seqno
= (buf
[idx
] >> 4) & 0x1;
197 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body
*req
,
198 struct drm_dp_sideband_msg_tx
*raw
)
203 buf
[idx
++] = req
->req_type
& 0x7f;
205 switch (req
->req_type
) {
206 case DP_ENUM_PATH_RESOURCES
:
207 buf
[idx
] = (req
->u
.port_num
.port_number
& 0xf) << 4;
210 case DP_ALLOCATE_PAYLOAD
:
211 buf
[idx
] = (req
->u
.allocate_payload
.port_number
& 0xf) << 4 |
212 (req
->u
.allocate_payload
.number_sdp_streams
& 0xf);
214 buf
[idx
] = (req
->u
.allocate_payload
.vcpi
& 0x7f);
216 buf
[idx
] = (req
->u
.allocate_payload
.pbn
>> 8);
218 buf
[idx
] = (req
->u
.allocate_payload
.pbn
& 0xff);
220 for (i
= 0; i
< req
->u
.allocate_payload
.number_sdp_streams
/ 2; i
++) {
221 buf
[idx
] = ((req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2] & 0xf) << 4) |
222 (req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2 + 1] & 0xf);
225 if (req
->u
.allocate_payload
.number_sdp_streams
& 1) {
226 i
= req
->u
.allocate_payload
.number_sdp_streams
- 1;
227 buf
[idx
] = (req
->u
.allocate_payload
.sdp_stream_sink
[i
] & 0xf) << 4;
231 case DP_QUERY_PAYLOAD
:
232 buf
[idx
] = (req
->u
.query_payload
.port_number
& 0xf) << 4;
234 buf
[idx
] = (req
->u
.query_payload
.vcpi
& 0x7f);
237 case DP_REMOTE_DPCD_READ
:
238 buf
[idx
] = (req
->u
.dpcd_read
.port_number
& 0xf) << 4;
239 buf
[idx
] |= ((req
->u
.dpcd_read
.dpcd_address
& 0xf0000) >> 16) & 0xf;
241 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff00) >> 8;
243 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff);
245 buf
[idx
] = (req
->u
.dpcd_read
.num_bytes
);
249 case DP_REMOTE_DPCD_WRITE
:
250 buf
[idx
] = (req
->u
.dpcd_write
.port_number
& 0xf) << 4;
251 buf
[idx
] |= ((req
->u
.dpcd_write
.dpcd_address
& 0xf0000) >> 16) & 0xf;
253 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff00) >> 8;
255 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff);
257 buf
[idx
] = (req
->u
.dpcd_write
.num_bytes
);
259 memcpy(&buf
[idx
], req
->u
.dpcd_write
.bytes
, req
->u
.dpcd_write
.num_bytes
);
260 idx
+= req
->u
.dpcd_write
.num_bytes
;
262 case DP_REMOTE_I2C_READ
:
263 buf
[idx
] = (req
->u
.i2c_read
.port_number
& 0xf) << 4;
264 buf
[idx
] |= (req
->u
.i2c_read
.num_transactions
& 0x3);
266 for (i
= 0; i
< (req
->u
.i2c_read
.num_transactions
& 0x3); i
++) {
267 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].i2c_dev_id
& 0x7f;
269 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].num_bytes
;
271 memcpy(&buf
[idx
], req
->u
.i2c_read
.transactions
[i
].bytes
, req
->u
.i2c_read
.transactions
[i
].num_bytes
);
272 idx
+= req
->u
.i2c_read
.transactions
[i
].num_bytes
;
274 buf
[idx
] = (req
->u
.i2c_read
.transactions
[i
].no_stop_bit
& 0x1) << 5;
275 buf
[idx
] |= (req
->u
.i2c_read
.transactions
[i
].i2c_transaction_delay
& 0xf);
278 buf
[idx
] = (req
->u
.i2c_read
.read_i2c_device_id
) & 0x7f;
280 buf
[idx
] = (req
->u
.i2c_read
.num_bytes_read
);
284 case DP_REMOTE_I2C_WRITE
:
285 buf
[idx
] = (req
->u
.i2c_write
.port_number
& 0xf) << 4;
287 buf
[idx
] = (req
->u
.i2c_write
.write_i2c_device_id
) & 0x7f;
289 buf
[idx
] = (req
->u
.i2c_write
.num_bytes
);
291 memcpy(&buf
[idx
], req
->u
.i2c_write
.bytes
, req
->u
.i2c_write
.num_bytes
);
292 idx
+= req
->u
.i2c_write
.num_bytes
;
298 static void drm_dp_crc_sideband_chunk_req(u8
*msg
, u8 len
)
301 crc4
= drm_dp_msg_data_crc4(msg
, len
);
305 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body
*rep
,
306 struct drm_dp_sideband_msg_tx
*raw
)
311 buf
[idx
++] = (rep
->reply_type
& 0x1) << 7 | (rep
->req_type
& 0x7f);
316 /* this adds a chunk of msg to the builder to get the final msg */
317 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx
*msg
,
318 u8
*replybuf
, u8 replybuflen
, bool hdr
)
325 struct drm_dp_sideband_msg_hdr recv_hdr
;
326 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 /* get length contained in this portion */
335 msg
->curchunk_len
= recv_hdr
.msg_len
;
336 msg
->curchunk_hdrlen
= hdrlen
;
338 /* we have already gotten an somt - don't bother parsing */
339 if (recv_hdr
.somt
&& msg
->have_somt
)
343 memcpy(&msg
->initial_hdr
, &recv_hdr
, sizeof(struct drm_dp_sideband_msg_hdr
));
344 msg
->have_somt
= true;
347 msg
->have_eomt
= true;
349 /* copy the bytes for the remainder of this header chunk */
350 msg
->curchunk_idx
= min(msg
->curchunk_len
, (u8
)(replybuflen
- hdrlen
));
351 memcpy(&msg
->chunk
[0], replybuf
+ hdrlen
, msg
->curchunk_idx
);
353 memcpy(&msg
->chunk
[msg
->curchunk_idx
], replybuf
, replybuflen
);
354 msg
->curchunk_idx
+= replybuflen
;
357 if (msg
->curchunk_idx
>= msg
->curchunk_len
) {
359 crc4
= drm_dp_msg_data_crc4(msg
->chunk
, msg
->curchunk_len
- 1);
360 /* copy chunk into bigger msg */
361 memcpy(&msg
->msg
[msg
->curlen
], msg
->chunk
, msg
->curchunk_len
- 1);
362 msg
->curlen
+= msg
->curchunk_len
- 1;
367 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx
*raw
,
368 struct drm_dp_sideband_msg_reply_body
*repmsg
)
372 memcpy(repmsg
->u
.link_addr
.guid
, &raw
->msg
[idx
], 16);
374 repmsg
->u
.link_addr
.nports
= raw
->msg
[idx
] & 0xf;
376 if (idx
> raw
->curlen
)
378 for (i
= 0; i
< repmsg
->u
.link_addr
.nports
; i
++) {
379 if (raw
->msg
[idx
] & 0x80)
380 repmsg
->u
.link_addr
.ports
[i
].input_port
= 1;
382 repmsg
->u
.link_addr
.ports
[i
].peer_device_type
= (raw
->msg
[idx
] >> 4) & 0x7;
383 repmsg
->u
.link_addr
.ports
[i
].port_number
= (raw
->msg
[idx
] & 0xf);
386 if (idx
> raw
->curlen
)
388 repmsg
->u
.link_addr
.ports
[i
].mcs
= (raw
->msg
[idx
] >> 7) & 0x1;
389 repmsg
->u
.link_addr
.ports
[i
].ddps
= (raw
->msg
[idx
] >> 6) & 0x1;
390 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0)
391 repmsg
->u
.link_addr
.ports
[i
].legacy_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
393 if (idx
> raw
->curlen
)
395 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0) {
396 repmsg
->u
.link_addr
.ports
[i
].dpcd_revision
= (raw
->msg
[idx
]);
398 if (idx
> raw
->curlen
)
400 memcpy(repmsg
->u
.link_addr
.ports
[i
].peer_guid
, &raw
->msg
[idx
], 16);
402 if (idx
> raw
->curlen
)
404 repmsg
->u
.link_addr
.ports
[i
].num_sdp_streams
= (raw
->msg
[idx
] >> 4) & 0xf;
405 repmsg
->u
.link_addr
.ports
[i
].num_sdp_stream_sinks
= (raw
->msg
[idx
] & 0xf);
409 if (idx
> raw
->curlen
)
415 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
419 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx
*raw
,
420 struct drm_dp_sideband_msg_reply_body
*repmsg
)
423 repmsg
->u
.remote_dpcd_read_ack
.port_number
= raw
->msg
[idx
] & 0xf;
425 if (idx
> raw
->curlen
)
427 repmsg
->u
.remote_dpcd_read_ack
.num_bytes
= raw
->msg
[idx
];
428 if (idx
> raw
->curlen
)
431 memcpy(repmsg
->u
.remote_dpcd_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_dpcd_read_ack
.num_bytes
);
434 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
438 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx
*raw
,
439 struct drm_dp_sideband_msg_reply_body
*repmsg
)
442 repmsg
->u
.remote_dpcd_write_ack
.port_number
= raw
->msg
[idx
] & 0xf;
444 if (idx
> raw
->curlen
)
448 DRM_DEBUG_KMS("parse length fail %d %d\n", idx
, raw
->curlen
);
452 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx
*raw
,
453 struct drm_dp_sideband_msg_reply_body
*repmsg
)
457 repmsg
->u
.remote_i2c_read_ack
.port_number
= (raw
->msg
[idx
] & 0xf);
459 if (idx
> raw
->curlen
)
461 repmsg
->u
.remote_i2c_read_ack
.num_bytes
= raw
->msg
[idx
];
464 memcpy(repmsg
->u
.remote_i2c_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_i2c_read_ack
.num_bytes
);
467 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx
, raw
->curlen
);
471 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx
*raw
,
472 struct drm_dp_sideband_msg_reply_body
*repmsg
)
475 repmsg
->u
.path_resources
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
477 if (idx
> raw
->curlen
)
479 repmsg
->u
.path_resources
.full_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
481 if (idx
> raw
->curlen
)
483 repmsg
->u
.path_resources
.avail_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
485 if (idx
> raw
->curlen
)
489 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx
, raw
->curlen
);
493 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
494 struct drm_dp_sideband_msg_reply_body
*repmsg
)
497 repmsg
->u
.allocate_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
499 if (idx
> raw
->curlen
)
501 repmsg
->u
.allocate_payload
.vcpi
= raw
->msg
[idx
];
503 if (idx
> raw
->curlen
)
505 repmsg
->u
.allocate_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
507 if (idx
> raw
->curlen
)
511 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx
, raw
->curlen
);
515 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
516 struct drm_dp_sideband_msg_reply_body
*repmsg
)
519 repmsg
->u
.query_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
521 if (idx
> raw
->curlen
)
523 repmsg
->u
.query_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
525 if (idx
> raw
->curlen
)
529 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx
, raw
->curlen
);
533 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx
*raw
,
534 struct drm_dp_sideband_msg_reply_body
*msg
)
536 memset(msg
, 0, sizeof(*msg
));
537 msg
->reply_type
= (raw
->msg
[0] & 0x80) >> 7;
538 msg
->req_type
= (raw
->msg
[0] & 0x7f);
540 if (msg
->reply_type
) {
541 memcpy(msg
->u
.nak
.guid
, &raw
->msg
[1], 16);
542 msg
->u
.nak
.reason
= raw
->msg
[17];
543 msg
->u
.nak
.nak_data
= raw
->msg
[18];
547 switch (msg
->req_type
) {
548 case DP_LINK_ADDRESS
:
549 return drm_dp_sideband_parse_link_address(raw
, msg
);
550 case DP_QUERY_PAYLOAD
:
551 return drm_dp_sideband_parse_query_payload_ack(raw
, msg
);
552 case DP_REMOTE_DPCD_READ
:
553 return drm_dp_sideband_parse_remote_dpcd_read(raw
, msg
);
554 case DP_REMOTE_DPCD_WRITE
:
555 return drm_dp_sideband_parse_remote_dpcd_write(raw
, msg
);
556 case DP_REMOTE_I2C_READ
:
557 return drm_dp_sideband_parse_remote_i2c_read_ack(raw
, msg
);
558 case DP_ENUM_PATH_RESOURCES
:
559 return drm_dp_sideband_parse_enum_path_resources_ack(raw
, msg
);
560 case DP_ALLOCATE_PAYLOAD
:
561 return drm_dp_sideband_parse_allocate_payload_ack(raw
, msg
);
563 DRM_ERROR("Got unknown reply 0x%02x\n", msg
->req_type
);
568 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
569 struct drm_dp_sideband_msg_req_body
*msg
)
573 msg
->u
.conn_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
575 if (idx
> raw
->curlen
)
578 memcpy(msg
->u
.conn_stat
.guid
, &raw
->msg
[idx
], 16);
580 if (idx
> raw
->curlen
)
583 msg
->u
.conn_stat
.legacy_device_plug_status
= (raw
->msg
[idx
] >> 6) & 0x1;
584 msg
->u
.conn_stat
.displayport_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
585 msg
->u
.conn_stat
.message_capability_status
= (raw
->msg
[idx
] >> 4) & 0x1;
586 msg
->u
.conn_stat
.input_port
= (raw
->msg
[idx
] >> 3) & 0x1;
587 msg
->u
.conn_stat
.peer_device_type
= (raw
->msg
[idx
] & 0x7);
591 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx
, raw
->curlen
);
595 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
596 struct drm_dp_sideband_msg_req_body
*msg
)
600 msg
->u
.resource_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
602 if (idx
> raw
->curlen
)
605 memcpy(msg
->u
.resource_stat
.guid
, &raw
->msg
[idx
], 16);
607 if (idx
> raw
->curlen
)
610 msg
->u
.resource_stat
.available_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
614 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx
, raw
->curlen
);
618 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx
*raw
,
619 struct drm_dp_sideband_msg_req_body
*msg
)
621 memset(msg
, 0, sizeof(*msg
));
622 msg
->req_type
= (raw
->msg
[0] & 0x7f);
624 switch (msg
->req_type
) {
625 case DP_CONNECTION_STATUS_NOTIFY
:
626 return drm_dp_sideband_parse_connection_status_notify(raw
, msg
);
627 case DP_RESOURCE_STATUS_NOTIFY
:
628 return drm_dp_sideband_parse_resource_status_notify(raw
, msg
);
630 DRM_ERROR("Got unknown request 0x%02x\n", msg
->req_type
);
635 static int build_dpcd_write(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
, u8
*bytes
)
637 struct drm_dp_sideband_msg_req_body req
;
639 req
.req_type
= DP_REMOTE_DPCD_WRITE
;
640 req
.u
.dpcd_write
.port_number
= port_num
;
641 req
.u
.dpcd_write
.dpcd_address
= offset
;
642 req
.u
.dpcd_write
.num_bytes
= num_bytes
;
643 req
.u
.dpcd_write
.bytes
= bytes
;
644 drm_dp_encode_sideband_req(&req
, msg
);
649 static int build_link_address(struct drm_dp_sideband_msg_tx
*msg
)
651 struct drm_dp_sideband_msg_req_body req
;
653 req
.req_type
= DP_LINK_ADDRESS
;
654 drm_dp_encode_sideband_req(&req
, msg
);
658 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx
*msg
, int port_num
)
660 struct drm_dp_sideband_msg_req_body req
;
662 req
.req_type
= DP_ENUM_PATH_RESOURCES
;
663 req
.u
.port_num
.port_number
= port_num
;
664 drm_dp_encode_sideband_req(&req
, msg
);
665 msg
->path_msg
= true;
669 static int build_allocate_payload(struct drm_dp_sideband_msg_tx
*msg
, int port_num
,
670 u8 vcpi
, uint16_t pbn
,
671 u8 number_sdp_streams
,
674 struct drm_dp_sideband_msg_req_body req
;
675 memset(&req
, 0, sizeof(req
));
676 req
.req_type
= DP_ALLOCATE_PAYLOAD
;
677 req
.u
.allocate_payload
.port_number
= port_num
;
678 req
.u
.allocate_payload
.vcpi
= vcpi
;
679 req
.u
.allocate_payload
.pbn
= pbn
;
680 req
.u
.allocate_payload
.number_sdp_streams
= number_sdp_streams
;
681 memcpy(req
.u
.allocate_payload
.sdp_stream_sink
, sdp_stream_sink
,
683 drm_dp_encode_sideband_req(&req
, msg
);
684 msg
->path_msg
= true;
688 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
689 struct drm_dp_vcpi
*vcpi
)
693 mutex_lock(&mgr
->payload_lock
);
694 ret
= find_first_zero_bit(&mgr
->payload_mask
, mgr
->max_payloads
+ 1);
695 if (ret
> mgr
->max_payloads
) {
697 DRM_DEBUG_KMS("out of payload ids %d\n", ret
);
701 vcpi_ret
= find_first_zero_bit(&mgr
->vcpi_mask
, mgr
->max_payloads
+ 1);
702 if (vcpi_ret
> mgr
->max_payloads
) {
704 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret
);
708 set_bit(ret
, &mgr
->payload_mask
);
709 set_bit(vcpi_ret
, &mgr
->vcpi_mask
);
710 vcpi
->vcpi
= vcpi_ret
+ 1;
711 mgr
->proposed_vcpis
[ret
- 1] = vcpi
;
713 mutex_unlock(&mgr
->payload_lock
);
717 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
724 mutex_lock(&mgr
->payload_lock
);
725 DRM_DEBUG_KMS("putting payload %d\n", vcpi
);
726 clear_bit(vcpi
- 1, &mgr
->vcpi_mask
);
728 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
729 if (mgr
->proposed_vcpis
[i
])
730 if (mgr
->proposed_vcpis
[i
]->vcpi
== vcpi
) {
731 mgr
->proposed_vcpis
[i
] = NULL
;
732 clear_bit(i
+ 1, &mgr
->payload_mask
);
735 mutex_unlock(&mgr
->payload_lock
);
738 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr
*mgr
,
739 struct drm_dp_sideband_msg_tx
*txmsg
)
744 * All updates to txmsg->state are protected by mgr->qlock, and the two
745 * cases we check here are terminal states. For those the barriers
746 * provided by the wake_up/wait_event pair are enough.
748 ret
= (txmsg
->state
== DRM_DP_SIDEBAND_TX_RX
||
749 txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
);
753 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch
*mstb
,
754 struct drm_dp_sideband_msg_tx
*txmsg
)
756 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
759 ret
= wait_event_timeout(mgr
->tx_waitq
,
760 check_txmsg_state(mgr
, txmsg
),
762 mutex_lock(&mstb
->mgr
->qlock
);
764 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
) {
769 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg
, txmsg
->state
, txmsg
->seqno
);
771 /* dump some state */
775 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
||
776 txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
) {
777 list_del(&txmsg
->next
);
780 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
||
781 txmsg
->state
== DRM_DP_SIDEBAND_TX_SENT
) {
782 mstb
->tx_slots
[txmsg
->seqno
] = NULL
;
786 mutex_unlock(&mgr
->qlock
);
791 static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct
, u8
*rad
)
793 struct drm_dp_mst_branch
*mstb
;
795 mstb
= kzalloc(sizeof(*mstb
), GFP_KERNEL
);
801 memcpy(mstb
->rad
, rad
, lct
/ 2);
802 INIT_LIST_HEAD(&mstb
->ports
);
803 kref_init(&mstb
->kref
);
807 static void drm_dp_free_mst_port(struct kref
*kref
);
809 static void drm_dp_free_mst_branch_device(struct kref
*kref
)
811 struct drm_dp_mst_branch
*mstb
= container_of(kref
, struct drm_dp_mst_branch
, kref
);
812 if (mstb
->port_parent
) {
813 if (list_empty(&mstb
->port_parent
->next
))
814 kref_put(&mstb
->port_parent
->kref
, drm_dp_free_mst_port
);
819 static void drm_dp_destroy_mst_branch_device(struct kref
*kref
)
821 struct drm_dp_mst_branch
*mstb
= container_of(kref
, struct drm_dp_mst_branch
, kref
);
822 struct drm_dp_mst_port
*port
, *tmp
;
823 bool wake_tx
= false;
826 * init kref again to be used by ports to remove mst branch when it is
831 if (mstb
->port_parent
&& list_empty(&mstb
->port_parent
->next
))
832 kref_get(&mstb
->port_parent
->kref
);
835 * destroy all ports - don't need lock
836 * as there are no more references to the mst branch
837 * device at this point.
839 list_for_each_entry_safe(port
, tmp
, &mstb
->ports
, next
) {
840 list_del(&port
->next
);
841 drm_dp_put_port(port
);
844 /* drop any tx slots msg */
845 mutex_lock(&mstb
->mgr
->qlock
);
846 if (mstb
->tx_slots
[0]) {
847 mstb
->tx_slots
[0]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
848 mstb
->tx_slots
[0] = NULL
;
851 if (mstb
->tx_slots
[1]) {
852 mstb
->tx_slots
[1]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
853 mstb
->tx_slots
[1] = NULL
;
856 mutex_unlock(&mstb
->mgr
->qlock
);
859 wake_up(&mstb
->mgr
->tx_waitq
);
861 kref_put(kref
, drm_dp_free_mst_branch_device
);
864 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch
*mstb
)
866 kref_put(&mstb
->kref
, drm_dp_destroy_mst_branch_device
);
870 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port
*port
, int old_pdt
)
872 struct drm_dp_mst_branch
*mstb
;
875 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
876 case DP_PEER_DEVICE_SST_SINK
:
877 /* remove i2c over sideband */
878 drm_dp_mst_unregister_i2c_bus(&port
->aux
);
880 case DP_PEER_DEVICE_MST_BRANCHING
:
883 drm_dp_put_mst_branch_device(mstb
);
888 static void drm_dp_destroy_port(struct kref
*kref
)
890 struct drm_dp_mst_port
*port
= container_of(kref
, struct drm_dp_mst_port
, kref
);
891 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
894 port
->vcpi
.num_slots
= 0;
896 kfree(port
->cached_edid
);
899 * The only time we don't have a connector
900 * on an output port is if the connector init
903 if (port
->connector
) {
904 /* we can't destroy the connector here, as
905 * we might be holding the mode_config.mutex
906 * from an EDID retrieval */
908 mutex_lock(&mgr
->destroy_connector_lock
);
909 kref_get(&port
->parent
->kref
);
910 list_add(&port
->next
, &mgr
->destroy_connector_list
);
911 mutex_unlock(&mgr
->destroy_connector_lock
);
912 schedule_work(&mgr
->destroy_connector_work
);
915 /* no need to clean up vcpi
916 * as if we have no connector we never setup a vcpi */
917 drm_dp_port_teardown_pdt(port
, port
->pdt
);
922 static void drm_dp_put_port(struct drm_dp_mst_port
*port
)
924 kref_put(&port
->kref
, drm_dp_destroy_port
);
927 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
)
929 struct drm_dp_mst_port
*port
;
930 struct drm_dp_mst_branch
*rmstb
;
931 if (to_find
== mstb
) {
932 kref_get(&mstb
->kref
);
935 list_for_each_entry(port
, &mstb
->ports
, next
) {
937 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(port
->mstb
, to_find
);
945 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
)
947 struct drm_dp_mst_branch
*rmstb
= NULL
;
948 mutex_lock(&mgr
->lock
);
949 if (mgr
->mst_primary
)
950 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(mgr
->mst_primary
, mstb
);
951 mutex_unlock(&mgr
->lock
);
955 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
)
957 struct drm_dp_mst_port
*port
, *mport
;
959 list_for_each_entry(port
, &mstb
->ports
, next
) {
960 if (port
== to_find
) {
961 kref_get(&port
->kref
);
965 mport
= drm_dp_mst_get_port_ref_locked(port
->mstb
, to_find
);
973 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
)
975 struct drm_dp_mst_port
*rport
= NULL
;
976 mutex_lock(&mgr
->lock
);
977 if (mgr
->mst_primary
)
978 rport
= drm_dp_mst_get_port_ref_locked(mgr
->mst_primary
, port
);
979 mutex_unlock(&mgr
->lock
);
983 static struct drm_dp_mst_port
*drm_dp_get_port(struct drm_dp_mst_branch
*mstb
, u8 port_num
)
985 struct drm_dp_mst_port
*port
;
987 list_for_each_entry(port
, &mstb
->ports
, next
) {
988 if (port
->port_num
== port_num
) {
989 kref_get(&port
->kref
);
998 * calculate a new RAD for this MST branch device
999 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1000 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1002 static u8
drm_dp_calculate_rad(struct drm_dp_mst_port
*port
,
1005 int parent_lct
= port
->parent
->lct
;
1007 int idx
= (parent_lct
- 1) / 2;
1008 if (parent_lct
> 1) {
1009 memcpy(rad
, port
->parent
->rad
, idx
+ 1);
1010 shift
= (parent_lct
% 2) ? 4 : 0;
1014 rad
[idx
] |= port
->port_num
<< shift
;
1015 return parent_lct
+ 1;
1019 * return sends link address for new mstb
1021 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port
*port
)
1025 bool send_link
= false;
1026 switch (port
->pdt
) {
1027 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
1028 case DP_PEER_DEVICE_SST_SINK
:
1029 /* add i2c over sideband */
1030 ret
= drm_dp_mst_register_i2c_bus(&port
->aux
);
1032 case DP_PEER_DEVICE_MST_BRANCHING
:
1033 lct
= drm_dp_calculate_rad(port
, rad
);
1035 port
->mstb
= drm_dp_add_mst_branch_device(lct
, rad
);
1036 port
->mstb
->mgr
= port
->mgr
;
1037 port
->mstb
->port_parent
= port
;
1045 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch
*mstb
, u8
*guid
)
1049 memcpy(mstb
->guid
, guid
, 16);
1051 if (!drm_dp_validate_guid(mstb
->mgr
, mstb
->guid
)) {
1052 if (mstb
->port_parent
) {
1053 ret
= drm_dp_send_dpcd_write(
1061 ret
= drm_dp_dpcd_write(
1070 static void build_mst_prop_path(const struct drm_dp_mst_branch
*mstb
,
1073 size_t proppath_size
)
1077 ksnprintf(proppath
, proppath_size
, "mst:%d", mstb
->mgr
->conn_base_id
);
1078 for (i
= 0; i
< (mstb
->lct
- 1); i
++) {
1079 int shift
= (i
% 2) ? 0 : 4;
1080 int port_num
= (mstb
->rad
[i
/ 2] >> shift
) & 0xf;
1081 ksnprintf(temp
, sizeof(temp
), "-%d", port_num
);
1082 strlcat(proppath
, temp
, proppath_size
);
1084 ksnprintf(temp
, sizeof(temp
), "-%d", pnum
);
1085 strlcat(proppath
, temp
, proppath_size
);
1088 static void drm_dp_add_port(struct drm_dp_mst_branch
*mstb
,
1090 struct drm_dp_link_addr_reply_port
*port_msg
)
1092 struct drm_dp_mst_port
*port
;
1094 bool created
= false;
1097 port
= drm_dp_get_port(mstb
, port_msg
->port_number
);
1099 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1102 kref_init(&port
->kref
);
1103 port
->parent
= mstb
;
1104 port
->port_num
= port_msg
->port_number
;
1105 port
->mgr
= mstb
->mgr
;
1106 port
->aux
.name
= "DPMST";
1107 port
->aux
.dev
= dev
;
1110 old_pdt
= port
->pdt
;
1111 old_ddps
= port
->ddps
;
1114 port
->pdt
= port_msg
->peer_device_type
;
1115 port
->input
= port_msg
->input_port
;
1116 port
->mcs
= port_msg
->mcs
;
1117 port
->ddps
= port_msg
->ddps
;
1118 port
->ldps
= port_msg
->legacy_device_plug_status
;
1119 port
->dpcd_rev
= port_msg
->dpcd_revision
;
1120 port
->num_sdp_streams
= port_msg
->num_sdp_streams
;
1121 port
->num_sdp_stream_sinks
= port_msg
->num_sdp_stream_sinks
;
1123 /* manage mstb port lists with mgr lock - take a reference
1126 mutex_lock(&mstb
->mgr
->lock
);
1127 kref_get(&port
->kref
);
1128 list_add(&port
->next
, &mstb
->ports
);
1129 mutex_unlock(&mstb
->mgr
->lock
);
1132 if (old_ddps
!= port
->ddps
) {
1135 drm_dp_send_enum_path_resources(mstb
->mgr
, mstb
, port
);
1137 port
->available_pbn
= 0;
1141 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1142 drm_dp_port_teardown_pdt(port
, old_pdt
);
1144 ret
= drm_dp_port_setup_pdt(port
);
1146 drm_dp_send_link_address(mstb
->mgr
, port
->mstb
);
1149 if (created
&& !port
->input
) {
1152 build_mst_prop_path(mstb
, port
->port_num
, proppath
, sizeof(proppath
));
1153 port
->connector
= (*mstb
->mgr
->cbs
->add_connector
)(mstb
->mgr
, port
, proppath
);
1154 if (!port
->connector
) {
1155 /* remove it from the port list */
1156 mutex_lock(&mstb
->mgr
->lock
);
1157 list_del(&port
->next
);
1158 mutex_unlock(&mstb
->mgr
->lock
);
1159 /* drop port list reference */
1160 drm_dp_put_port(port
);
1163 if (port
->port_num
>= DP_MST_LOGICAL_PORT_0
) {
1164 port
->cached_edid
= drm_get_edid(port
->connector
, &port
->aux
.ddc
);
1165 drm_mode_connector_set_tile_property(port
->connector
);
1167 (*mstb
->mgr
->cbs
->register_connector
)(port
->connector
);
1171 /* put reference to this port */
1172 drm_dp_put_port(port
);
1175 static void drm_dp_update_port(struct drm_dp_mst_branch
*mstb
,
1176 struct drm_dp_connection_status_notify
*conn_stat
)
1178 struct drm_dp_mst_port
*port
;
1181 bool dowork
= false;
1182 port
= drm_dp_get_port(mstb
, conn_stat
->port_number
);
1186 old_ddps
= port
->ddps
;
1187 old_pdt
= port
->pdt
;
1188 port
->pdt
= conn_stat
->peer_device_type
;
1189 port
->mcs
= conn_stat
->message_capability_status
;
1190 port
->ldps
= conn_stat
->legacy_device_plug_status
;
1191 port
->ddps
= conn_stat
->displayport_device_plug_status
;
1193 if (old_ddps
!= port
->ddps
) {
1197 port
->available_pbn
= 0;
1200 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1201 drm_dp_port_teardown_pdt(port
, old_pdt
);
1203 if (drm_dp_port_setup_pdt(port
))
1207 drm_dp_put_port(port
);
1209 queue_work(system_long_wq
, &mstb
->mgr
->work
);
1213 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr
*mgr
,
1216 struct drm_dp_mst_branch
*mstb
;
1217 struct drm_dp_mst_port
*port
;
1219 /* find the port by iterating down */
1221 mutex_lock(&mgr
->lock
);
1222 mstb
= mgr
->mst_primary
;
1224 for (i
= 0; i
< lct
- 1; i
++) {
1225 int shift
= (i
% 2) ? 0 : 4;
1226 int port_num
= (rad
[i
/ 2] >> shift
) & 0xf;
1228 list_for_each_entry(port
, &mstb
->ports
, next
) {
1229 if (port
->port_num
== port_num
) {
1232 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct
, rad
[0]);
1240 kref_get(&mstb
->kref
);
1242 mutex_unlock(&mgr
->lock
);
1246 static struct drm_dp_mst_branch
*get_mst_branch_device_by_guid_helper(
1247 struct drm_dp_mst_branch
*mstb
,
1250 struct drm_dp_mst_branch
*found_mstb
;
1251 struct drm_dp_mst_port
*port
;
1253 if (memcmp(mstb
->guid
, guid
, 16) == 0)
1257 list_for_each_entry(port
, &mstb
->ports
, next
) {
1261 found_mstb
= get_mst_branch_device_by_guid_helper(port
->mstb
, guid
);
1270 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device_by_guid(
1271 struct drm_dp_mst_topology_mgr
*mgr
,
1274 struct drm_dp_mst_branch
*mstb
;
1276 /* find the port by iterating down */
1277 mutex_lock(&mgr
->lock
);
1279 mstb
= get_mst_branch_device_by_guid_helper(mgr
->mst_primary
, guid
);
1282 kref_get(&mstb
->kref
);
1284 mutex_unlock(&mgr
->lock
);
1288 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1289 struct drm_dp_mst_branch
*mstb
)
1291 struct drm_dp_mst_port
*port
;
1292 struct drm_dp_mst_branch
*mstb_child
;
1293 if (!mstb
->link_address_sent
)
1294 drm_dp_send_link_address(mgr
, mstb
);
1296 list_for_each_entry(port
, &mstb
->ports
, next
) {
1303 if (!port
->available_pbn
)
1304 drm_dp_send_enum_path_resources(mgr
, mstb
, port
);
1307 mstb_child
= drm_dp_get_validated_mstb_ref(mgr
, port
->mstb
);
1309 drm_dp_check_and_send_link_address(mgr
, mstb_child
);
1310 drm_dp_put_mst_branch_device(mstb_child
);
1316 static void drm_dp_mst_link_probe_work(struct work_struct
*work
)
1318 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, work
);
1319 struct drm_dp_mst_branch
*mstb
;
1321 mutex_lock(&mgr
->lock
);
1322 mstb
= mgr
->mst_primary
;
1324 kref_get(&mstb
->kref
);
1326 mutex_unlock(&mgr
->lock
);
1328 drm_dp_check_and_send_link_address(mgr
, mstb
);
1329 drm_dp_put_mst_branch_device(mstb
);
1333 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
1336 static u8 zero_guid
[16];
1338 if (!memcmp(guid
, zero_guid
, 16)) {
1339 u64 salt
= get_jiffies_64();
1340 memcpy(&guid
[0], &salt
, sizeof(u64
));
1341 memcpy(&guid
[8], &salt
, sizeof(u64
));
1348 static int build_dpcd_read(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
)
1350 struct drm_dp_sideband_msg_req_body req
;
1352 req
.req_type
= DP_REMOTE_DPCD_READ
;
1353 req
.u
.dpcd_read
.port_number
= port_num
;
1354 req
.u
.dpcd_read
.dpcd_address
= offset
;
1355 req
.u
.dpcd_read
.num_bytes
= num_bytes
;
1356 drm_dp_encode_sideband_req(&req
, msg
);
1362 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1363 bool up
, u8
*msg
, int len
)
1366 int regbase
= up
? DP_SIDEBAND_MSG_UP_REP_BASE
: DP_SIDEBAND_MSG_DOWN_REQ_BASE
;
1367 int tosend
, total
, offset
;
1374 tosend
= min3(mgr
->max_dpcd_transaction_bytes
, 16, total
);
1376 ret
= drm_dp_dpcd_write(mgr
->aux
, regbase
+ offset
,
1379 if (ret
!= tosend
) {
1380 if (ret
== -EIO
&& retries
< 5) {
1384 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend
, ret
);
1390 } while (total
> 0);
1394 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr
*hdr
,
1395 struct drm_dp_sideband_msg_tx
*txmsg
)
1397 struct drm_dp_mst_branch
*mstb
= txmsg
->dst
;
1400 /* both msg slots are full */
1401 if (txmsg
->seqno
== -1) {
1402 if (mstb
->tx_slots
[0] && mstb
->tx_slots
[1]) {
1403 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__
);
1406 if (mstb
->tx_slots
[0] == NULL
&& mstb
->tx_slots
[1] == NULL
) {
1407 txmsg
->seqno
= mstb
->last_seqno
;
1408 mstb
->last_seqno
^= 1;
1409 } else if (mstb
->tx_slots
[0] == NULL
)
1413 mstb
->tx_slots
[txmsg
->seqno
] = txmsg
;
1416 req_type
= txmsg
->msg
[0] & 0x7f;
1417 if (req_type
== DP_CONNECTION_STATUS_NOTIFY
||
1418 req_type
== DP_RESOURCE_STATUS_NOTIFY
)
1422 hdr
->path_msg
= txmsg
->path_msg
;
1423 hdr
->lct
= mstb
->lct
;
1424 hdr
->lcr
= mstb
->lct
- 1;
1426 memcpy(hdr
->rad
, mstb
->rad
, mstb
->lct
/ 2);
1427 hdr
->seqno
= txmsg
->seqno
;
1431 * process a single block of the next message in the sideband queue
1433 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
1434 struct drm_dp_sideband_msg_tx
*txmsg
,
1438 struct drm_dp_sideband_msg_hdr hdr
;
1439 int len
, space
, idx
, tosend
;
1442 memset(&hdr
, 0, sizeof(struct drm_dp_sideband_msg_hdr
));
1444 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
) {
1446 txmsg
->state
= DRM_DP_SIDEBAND_TX_START_SEND
;
1449 /* make hdr from dst mst - for replies use seqno
1450 otherwise assign one */
1451 ret
= set_hdr_from_dst_qlock(&hdr
, txmsg
);
1455 /* amount left to send in this message */
1456 len
= txmsg
->cur_len
- txmsg
->cur_offset
;
1458 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1459 space
= 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr
);
1461 tosend
= min(len
, space
);
1462 if (len
== txmsg
->cur_len
)
1468 hdr
.msg_len
= tosend
+ 1;
1469 drm_dp_encode_sideband_msg_hdr(&hdr
, chunk
, &idx
);
1470 memcpy(&chunk
[idx
], &txmsg
->msg
[txmsg
->cur_offset
], tosend
);
1471 /* add crc at end */
1472 drm_dp_crc_sideband_chunk_req(&chunk
[idx
], tosend
);
1475 ret
= drm_dp_send_sideband_msg(mgr
, up
, chunk
, idx
);
1477 DRM_DEBUG_KMS("sideband msg failed to send\n");
1481 txmsg
->cur_offset
+= tosend
;
1482 if (txmsg
->cur_offset
== txmsg
->cur_len
) {
1483 txmsg
->state
= DRM_DP_SIDEBAND_TX_SENT
;
1489 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
)
1491 struct drm_dp_sideband_msg_tx
*txmsg
;
1494 WARN_ON(!mutex_is_locked(&mgr
->qlock
));
1496 /* construct a chunk from the first msg in the tx_msg queue */
1497 if (list_empty(&mgr
->tx_msg_downq
)) {
1498 mgr
->tx_down_in_progress
= false;
1501 mgr
->tx_down_in_progress
= true;
1503 txmsg
= list_first_entry(&mgr
->tx_msg_downq
, struct drm_dp_sideband_msg_tx
, next
);
1504 ret
= process_single_tx_qlock(mgr
, txmsg
, false);
1506 /* txmsg is sent it should be in the slots now */
1507 list_del(&txmsg
->next
);
1509 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1510 list_del(&txmsg
->next
);
1511 if (txmsg
->seqno
!= -1)
1512 txmsg
->dst
->tx_slots
[txmsg
->seqno
] = NULL
;
1513 txmsg
->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
1514 wake_up(&mgr
->tx_waitq
);
1516 if (list_empty(&mgr
->tx_msg_downq
)) {
1517 mgr
->tx_down_in_progress
= false;
1522 /* called holding qlock */
1523 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
1524 struct drm_dp_sideband_msg_tx
*txmsg
)
1528 /* construct a chunk from the first msg in the tx_msg queue */
1529 ret
= process_single_tx_qlock(mgr
, txmsg
, true);
1532 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1534 txmsg
->dst
->tx_slots
[txmsg
->seqno
] = NULL
;
1537 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr
*mgr
,
1538 struct drm_dp_sideband_msg_tx
*txmsg
)
1540 mutex_lock(&mgr
->qlock
);
1541 list_add_tail(&txmsg
->next
, &mgr
->tx_msg_downq
);
1542 if (!mgr
->tx_down_in_progress
)
1543 process_single_down_tx_qlock(mgr
);
1544 mutex_unlock(&mgr
->qlock
);
1547 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1548 struct drm_dp_mst_branch
*mstb
)
1551 struct drm_dp_sideband_msg_tx
*txmsg
;
1554 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1559 len
= build_link_address(txmsg
);
1561 mstb
->link_address_sent
= true;
1562 drm_dp_queue_down_tx(mgr
, txmsg
);
1564 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1568 if (txmsg
->reply
.reply_type
== 1)
1569 DRM_DEBUG_KMS("link address nak received\n");
1571 DRM_DEBUG_KMS("link address reply: %d\n", txmsg
->reply
.u
.link_addr
.nports
);
1572 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1573 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
,
1574 txmsg
->reply
.u
.link_addr
.ports
[i
].input_port
,
1575 txmsg
->reply
.u
.link_addr
.ports
[i
].peer_device_type
,
1576 txmsg
->reply
.u
.link_addr
.ports
[i
].port_number
,
1577 txmsg
->reply
.u
.link_addr
.ports
[i
].dpcd_revision
,
1578 txmsg
->reply
.u
.link_addr
.ports
[i
].mcs
,
1579 txmsg
->reply
.u
.link_addr
.ports
[i
].ddps
,
1580 txmsg
->reply
.u
.link_addr
.ports
[i
].legacy_device_plug_status
,
1581 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_streams
,
1582 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_stream_sinks
);
1585 drm_dp_check_mstb_guid(mstb
, txmsg
->reply
.u
.link_addr
.guid
);
1587 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1588 drm_dp_add_port(mstb
, mgr
->dev
, &txmsg
->reply
.u
.link_addr
.ports
[i
]);
1590 (*mgr
->cbs
->hotplug
)(mgr
);
1593 mstb
->link_address_sent
= false;
1594 DRM_DEBUG_KMS("link address failed %d\n", ret
);
1600 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
1601 struct drm_dp_mst_branch
*mstb
,
1602 struct drm_dp_mst_port
*port
)
1605 struct drm_dp_sideband_msg_tx
*txmsg
;
1608 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1613 len
= build_enum_path_resources(txmsg
, port
->port_num
);
1615 drm_dp_queue_down_tx(mgr
, txmsg
);
1617 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1619 if (txmsg
->reply
.reply_type
== 1)
1620 DRM_DEBUG_KMS("enum path resources nak received\n");
1622 if (port
->port_num
!= txmsg
->reply
.u
.path_resources
.port_number
)
1623 DRM_ERROR("got incorrect port in response\n");
1624 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
,
1625 txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
);
1626 port
->available_pbn
= txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
;
1634 static struct drm_dp_mst_port
*drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch
*mstb
)
1636 if (!mstb
->port_parent
)
1639 if (mstb
->port_parent
->mstb
!= mstb
)
1640 return mstb
->port_parent
;
1642 return drm_dp_get_last_connected_port_to_mstb(mstb
->port_parent
->parent
);
1645 static struct drm_dp_mst_branch
*drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr
*mgr
,
1646 struct drm_dp_mst_branch
*mstb
,
1649 struct drm_dp_mst_branch
*rmstb
= NULL
;
1650 struct drm_dp_mst_port
*found_port
;
1651 mutex_lock(&mgr
->lock
);
1652 if (mgr
->mst_primary
) {
1653 found_port
= drm_dp_get_last_connected_port_to_mstb(mstb
);
1656 rmstb
= found_port
->parent
;
1657 kref_get(&rmstb
->kref
);
1658 *port_num
= found_port
->port_num
;
1661 mutex_unlock(&mgr
->lock
);
1665 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1666 struct drm_dp_mst_port
*port
,
1670 struct drm_dp_sideband_msg_tx
*txmsg
;
1671 struct drm_dp_mst_branch
*mstb
;
1672 int len
, ret
, port_num
;
1673 u8 sinks
[DRM_DP_MAX_SDP_STREAMS
];
1676 port
= drm_dp_get_validated_port_ref(mgr
, port
);
1680 port_num
= port
->port_num
;
1681 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
1683 mstb
= drm_dp_get_last_connected_port_and_mstb(mgr
, port
->parent
, &port_num
);
1686 drm_dp_put_port(port
);
1691 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1697 for (i
= 0; i
< port
->num_sdp_streams
; i
++)
1701 len
= build_allocate_payload(txmsg
, port_num
,
1703 pbn
, port
->num_sdp_streams
, sinks
);
1705 drm_dp_queue_down_tx(mgr
, txmsg
);
1707 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1709 if (txmsg
->reply
.reply_type
== 1) {
1716 drm_dp_put_mst_branch_device(mstb
);
1717 drm_dp_put_port(port
);
1721 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1723 struct drm_dp_payload
*payload
)
1727 ret
= drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1729 payload
->payload_state
= 0;
1732 payload
->payload_state
= DP_PAYLOAD_LOCAL
;
1736 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1737 struct drm_dp_mst_port
*port
,
1739 struct drm_dp_payload
*payload
)
1742 ret
= drm_dp_payload_send_msg(mgr
, port
, id
, port
->vcpi
.pbn
);
1745 payload
->payload_state
= DP_PAYLOAD_REMOTE
;
1749 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1750 struct drm_dp_mst_port
*port
,
1752 struct drm_dp_payload
*payload
)
1754 DRM_DEBUG_KMS("\n");
1755 /* its okay for these to fail */
1757 drm_dp_payload_send_msg(mgr
, port
, id
, 0);
1760 drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1761 payload
->payload_state
= DP_PAYLOAD_DELETE_LOCAL
;
1765 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1767 struct drm_dp_payload
*payload
)
1769 payload
->payload_state
= 0;
1774 * drm_dp_update_payload_part1() - Execute payload update part 1
1775 * @mgr: manager to use.
1777 * This iterates over all proposed virtual channels, and tries to
1778 * allocate space in the link for them. For 0->slots transitions,
1779 * this step just writes the VCPI to the MST device. For slots->0
1780 * transitions, this writes the updated VCPIs and removes the
1781 * remote VC payloads.
1783 * after calling this the driver should generate ACT and payload
1786 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr
*mgr
)
1790 struct drm_dp_payload req_payload
;
1791 struct drm_dp_mst_port
*port
;
1793 mutex_lock(&mgr
->payload_lock
);
1794 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1795 /* solve the current payloads - compare to the hw ones
1796 - update the hw view */
1797 req_payload
.start_slot
= cur_slots
;
1798 if (mgr
->proposed_vcpis
[i
]) {
1799 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1800 port
= drm_dp_get_validated_port_ref(mgr
, port
);
1802 mutex_unlock(&mgr
->payload_lock
);
1805 req_payload
.num_slots
= mgr
->proposed_vcpis
[i
]->num_slots
;
1806 req_payload
.vcpi
= mgr
->proposed_vcpis
[i
]->vcpi
;
1809 req_payload
.num_slots
= 0;
1812 if (mgr
->payloads
[i
].start_slot
!= req_payload
.start_slot
) {
1813 mgr
->payloads
[i
].start_slot
= req_payload
.start_slot
;
1815 /* work out what is required to happen with this payload */
1816 if (mgr
->payloads
[i
].num_slots
!= req_payload
.num_slots
) {
1818 /* need to push an update for this payload */
1819 if (req_payload
.num_slots
) {
1820 drm_dp_create_payload_step1(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &req_payload
);
1821 mgr
->payloads
[i
].num_slots
= req_payload
.num_slots
;
1822 mgr
->payloads
[i
].vcpi
= req_payload
.vcpi
;
1823 } else if (mgr
->payloads
[i
].num_slots
) {
1824 mgr
->payloads
[i
].num_slots
= 0;
1825 drm_dp_destroy_payload_step1(mgr
, port
, port
->vcpi
.vcpi
, &mgr
->payloads
[i
]);
1826 req_payload
.payload_state
= mgr
->payloads
[i
].payload_state
;
1827 mgr
->payloads
[i
].start_slot
= 0;
1829 mgr
->payloads
[i
].payload_state
= req_payload
.payload_state
;
1831 cur_slots
+= req_payload
.num_slots
;
1834 drm_dp_put_port(port
);
1837 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1838 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1839 DRM_DEBUG_KMS("removing payload %d\n", i
);
1840 for (j
= i
; j
< mgr
->max_payloads
- 1; j
++) {
1841 memcpy(&mgr
->payloads
[j
], &mgr
->payloads
[j
+ 1], sizeof(struct drm_dp_payload
));
1842 mgr
->proposed_vcpis
[j
] = mgr
->proposed_vcpis
[j
+ 1];
1843 if (mgr
->proposed_vcpis
[j
] && mgr
->proposed_vcpis
[j
]->num_slots
) {
1844 set_bit(j
+ 1, &mgr
->payload_mask
);
1846 clear_bit(j
+ 1, &mgr
->payload_mask
);
1849 memset(&mgr
->payloads
[mgr
->max_payloads
- 1], 0, sizeof(struct drm_dp_payload
));
1850 mgr
->proposed_vcpis
[mgr
->max_payloads
- 1] = NULL
;
1851 clear_bit(mgr
->max_payloads
, &mgr
->payload_mask
);
1855 mutex_unlock(&mgr
->payload_lock
);
1859 EXPORT_SYMBOL(drm_dp_update_payload_part1
);
1862 * drm_dp_update_payload_part2() - Execute payload update part 2
1863 * @mgr: manager to use.
1865 * This iterates over all proposed virtual channels, and tries to
1866 * allocate space in the link for them. For 0->slots transitions,
1867 * this step writes the remote VC payload commands. For slots->0
1868 * this just resets some internal state.
1870 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr
*mgr
)
1872 struct drm_dp_mst_port
*port
;
1875 mutex_lock(&mgr
->payload_lock
);
1876 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1878 if (!mgr
->proposed_vcpis
[i
])
1881 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1883 DRM_DEBUG_KMS("payload %d %d\n", i
, mgr
->payloads
[i
].payload_state
);
1884 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_LOCAL
) {
1885 ret
= drm_dp_create_payload_step2(mgr
, port
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1886 } else if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1887 ret
= drm_dp_destroy_payload_step2(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1890 mutex_unlock(&mgr
->payload_lock
);
1894 mutex_unlock(&mgr
->payload_lock
);
1897 EXPORT_SYMBOL(drm_dp_update_payload_part2
);
1899 #if 0 /* unused as of yet */
1900 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr
*mgr
,
1901 struct drm_dp_mst_port
*port
,
1902 int offset
, int size
)
1905 struct drm_dp_sideband_msg_tx
*txmsg
;
1907 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1911 len
= build_dpcd_read(txmsg
, port
->port_num
, 0, 8);
1912 txmsg
->dst
= port
->parent
;
1914 drm_dp_queue_down_tx(mgr
, txmsg
);
1920 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
1921 struct drm_dp_mst_port
*port
,
1922 int offset
, int size
, u8
*bytes
)
1926 struct drm_dp_sideband_msg_tx
*txmsg
;
1927 struct drm_dp_mst_branch
*mstb
;
1929 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
1933 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1939 len
= build_dpcd_write(txmsg
, port
->port_num
, offset
, size
, bytes
);
1942 drm_dp_queue_down_tx(mgr
, txmsg
);
1944 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1946 if (txmsg
->reply
.reply_type
== 1) {
1953 drm_dp_put_mst_branch_device(mstb
);
1957 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx
*msg
, u8 req_type
)
1959 struct drm_dp_sideband_msg_reply_body reply
;
1961 reply
.reply_type
= 0;
1962 reply
.req_type
= req_type
;
1963 drm_dp_encode_sideband_reply(&reply
, msg
);
1967 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr
*mgr
,
1968 struct drm_dp_mst_branch
*mstb
,
1969 int req_type
, int seqno
, bool broadcast
)
1971 struct drm_dp_sideband_msg_tx
*txmsg
;
1973 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1978 txmsg
->seqno
= seqno
;
1979 drm_dp_encode_up_ack_reply(txmsg
, req_type
);
1981 mutex_lock(&mgr
->qlock
);
1983 process_single_up_tx_qlock(mgr
, txmsg
);
1985 mutex_unlock(&mgr
->qlock
);
1991 static bool drm_dp_get_vc_payload_bw(int dp_link_bw
,
1995 switch (dp_link_bw
) {
1997 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1998 dp_link_bw
, dp_link_count
);
2001 case DP_LINK_BW_1_62
:
2002 *out
= 3 * dp_link_count
;
2004 case DP_LINK_BW_2_7
:
2005 *out
= 5 * dp_link_count
;
2007 case DP_LINK_BW_5_4
:
2008 *out
= 10 * dp_link_count
;
2015 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2016 * @mgr: manager to set state for
2017 * @mst_state: true to enable MST on this connector - false to disable.
2019 * This is called by the driver when it detects an MST capable device plugged
2020 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2022 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr
*mgr
, bool mst_state
)
2025 struct drm_dp_mst_branch
*mstb
= NULL
;
2027 mutex_lock(&mgr
->lock
);
2028 if (mst_state
== mgr
->mst_state
)
2031 mgr
->mst_state
= mst_state
;
2032 /* set the device into MST mode */
2034 WARN_ON(mgr
->mst_primary
);
2037 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
2038 if (ret
!= DP_RECEIVER_CAP_SIZE
) {
2039 DRM_DEBUG_KMS("failed to read DPCD\n");
2043 if (!drm_dp_get_vc_payload_bw(mgr
->dpcd
[1],
2044 mgr
->dpcd
[2] & DP_MAX_LANE_COUNT_MASK
,
2050 mgr
->total_pbn
= 2560;
2051 mgr
->total_slots
= DIV_ROUND_UP(mgr
->total_pbn
, mgr
->pbn_div
);
2052 mgr
->avail_slots
= mgr
->total_slots
;
2054 /* add initial branch device at LCT 1 */
2055 mstb
= drm_dp_add_mst_branch_device(1, NULL
);
2062 /* give this the main reference */
2063 mgr
->mst_primary
= mstb
;
2064 kref_get(&mgr
->mst_primary
->kref
);
2066 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2067 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
2073 struct drm_dp_payload reset_pay
;
2074 reset_pay
.start_slot
= 0;
2075 reset_pay
.num_slots
= 0x3f;
2076 drm_dp_dpcd_write_payload(mgr
, 0, &reset_pay
);
2079 queue_work(system_long_wq
, &mgr
->work
);
2083 /* disable MST on the device */
2084 mstb
= mgr
->mst_primary
;
2085 mgr
->mst_primary
= NULL
;
2086 /* this can fail if the device is gone */
2087 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
, 0);
2089 memset(mgr
->payloads
, 0, mgr
->max_payloads
* sizeof(struct drm_dp_payload
));
2090 mgr
->payload_mask
= 0;
2091 set_bit(0, &mgr
->payload_mask
);
2096 mutex_unlock(&mgr
->lock
);
2098 drm_dp_put_mst_branch_device(mstb
);
2102 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst
);
2105 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2106 * @mgr: manager to suspend
2108 * This function tells the MST device that we can't handle UP messages
2109 * anymore. This should stop it from sending any since we are suspended.
2111 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr
*mgr
)
2113 mutex_lock(&mgr
->lock
);
2114 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2115 DP_MST_EN
| DP_UPSTREAM_IS_SRC
);
2116 mutex_unlock(&mgr
->lock
);
2117 flush_work(&mgr
->work
);
2118 flush_work(&mgr
->destroy_connector_work
);
2120 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend
);
2123 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2124 * @mgr: manager to resume
2126 * This will fetch DPCD and see if the device is still there,
2127 * if it is, it will rewrite the MSTM control bits, and return.
2129 * if the device fails this returns -1, and the driver should do
2130 * a full MST reprobe, in case we were undocked.
2132 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr
*mgr
)
2136 mutex_lock(&mgr
->lock
);
2138 if (mgr
->mst_primary
) {
2142 sret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
2143 if (sret
!= DP_RECEIVER_CAP_SIZE
) {
2144 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2149 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2150 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
2152 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2157 /* Some hubs forget their guids after they resume */
2158 sret
= drm_dp_dpcd_read(mgr
->aux
, DP_GUID
, guid
, 16);
2160 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2164 drm_dp_check_mstb_guid(mgr
->mst_primary
, guid
);
2171 mutex_unlock(&mgr
->lock
);
2174 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume
);
2176 static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr
*mgr
, bool up
)
2180 int replylen
, origlen
, curreply
;
2182 struct drm_dp_sideband_msg_rx
*msg
;
2183 int basereg
= up
? DP_SIDEBAND_MSG_UP_REQ_BASE
: DP_SIDEBAND_MSG_DOWN_REP_BASE
;
2184 msg
= up
? &mgr
->up_req_recv
: &mgr
->down_rep_recv
;
2186 len
= min(mgr
->max_dpcd_transaction_bytes
, 16);
2187 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
,
2190 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len
, ret
);
2193 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, true);
2195 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock
[0]);
2198 replylen
= msg
->curchunk_len
+ msg
->curchunk_hdrlen
;
2203 while (replylen
> 0) {
2204 len
= min3(replylen
, mgr
->max_dpcd_transaction_bytes
, 16);
2205 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
+ curreply
,
2208 DRM_DEBUG_KMS("failed to read a chunk\n");
2210 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, false);
2212 DRM_DEBUG_KMS("failed to build sideband msg\n");
2218 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr
*mgr
)
2222 drm_dp_get_one_sb_msg(mgr
, false);
2224 if (mgr
->down_rep_recv
.have_eomt
) {
2225 struct drm_dp_sideband_msg_tx
*txmsg
;
2226 struct drm_dp_mst_branch
*mstb
;
2228 mstb
= drm_dp_get_mst_branch_device(mgr
,
2229 mgr
->down_rep_recv
.initial_hdr
.lct
,
2230 mgr
->down_rep_recv
.initial_hdr
.rad
);
2233 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->down_rep_recv
.initial_hdr
.lct
);
2234 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2238 /* find the message */
2239 slot
= mgr
->down_rep_recv
.initial_hdr
.seqno
;
2240 mutex_lock(&mgr
->qlock
);
2241 txmsg
= mstb
->tx_slots
[slot
];
2242 /* remove from slots */
2243 mutex_unlock(&mgr
->qlock
);
2246 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2248 mgr
->down_rep_recv
.initial_hdr
.seqno
,
2249 mgr
->down_rep_recv
.initial_hdr
.lct
,
2250 mgr
->down_rep_recv
.initial_hdr
.rad
[0],
2251 mgr
->down_rep_recv
.msg
[0]);
2252 drm_dp_put_mst_branch_device(mstb
);
2253 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2257 drm_dp_sideband_parse_reply(&mgr
->down_rep_recv
, &txmsg
->reply
);
2258 if (txmsg
->reply
.reply_type
== 1) {
2259 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
);
2262 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2263 drm_dp_put_mst_branch_device(mstb
);
2265 mutex_lock(&mgr
->qlock
);
2266 txmsg
->state
= DRM_DP_SIDEBAND_TX_RX
;
2267 mstb
->tx_slots
[slot
] = NULL
;
2268 mutex_unlock(&mgr
->qlock
);
2270 wake_up(&mgr
->tx_waitq
);
2275 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr
*mgr
)
2278 drm_dp_get_one_sb_msg(mgr
, true);
2280 if (mgr
->up_req_recv
.have_eomt
) {
2281 struct drm_dp_sideband_msg_req_body msg
;
2282 struct drm_dp_mst_branch
*mstb
= NULL
;
2285 if (!mgr
->up_req_recv
.initial_hdr
.broadcast
) {
2286 mstb
= drm_dp_get_mst_branch_device(mgr
,
2287 mgr
->up_req_recv
.initial_hdr
.lct
,
2288 mgr
->up_req_recv
.initial_hdr
.rad
);
2290 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2291 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2296 seqno
= mgr
->up_req_recv
.initial_hdr
.seqno
;
2297 drm_dp_sideband_parse_req(&mgr
->up_req_recv
, &msg
);
2299 if (msg
.req_type
== DP_CONNECTION_STATUS_NOTIFY
) {
2300 drm_dp_send_up_ack_reply(mgr
, mgr
->mst_primary
, msg
.req_type
, seqno
, false);
2303 mstb
= drm_dp_get_mst_branch_device_by_guid(mgr
, msg
.u
.conn_stat
.guid
);
2306 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2307 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2311 drm_dp_update_port(mstb
, &msg
.u
.conn_stat
);
2313 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
);
2314 (*mgr
->cbs
->hotplug
)(mgr
);
2316 } else if (msg
.req_type
== DP_RESOURCE_STATUS_NOTIFY
) {
2317 drm_dp_send_up_ack_reply(mgr
, mgr
->mst_primary
, msg
.req_type
, seqno
, false);
2319 mstb
= drm_dp_get_mst_branch_device_by_guid(mgr
, msg
.u
.resource_stat
.guid
);
2322 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2323 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2327 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg
.u
.resource_stat
.port_number
, msg
.u
.resource_stat
.available_pbn
);
2330 drm_dp_put_mst_branch_device(mstb
);
2331 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2337 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2338 * @mgr: manager to notify irq for.
2339 * @esi: 4 bytes from SINK_COUNT_ESI
2340 * @handled: whether the hpd interrupt was consumed or not
2342 * This should be called from the driver when it detects a short IRQ,
2343 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2344 * topology manager will process the sideband messages received as a result
2347 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr
*mgr
, u8
*esi
, bool *handled
)
2354 if (sc
!= mgr
->sink_count
) {
2355 mgr
->sink_count
= sc
;
2359 if (esi
[1] & DP_DOWN_REP_MSG_RDY
) {
2360 ret
= drm_dp_mst_handle_down_rep(mgr
);
2364 if (esi
[1] & DP_UP_REQ_MSG_RDY
) {
2365 ret
|= drm_dp_mst_handle_up_req(mgr
);
2369 drm_dp_mst_kick_tx(mgr
);
2372 EXPORT_SYMBOL(drm_dp_mst_hpd_irq
);
2375 * drm_dp_mst_detect_port() - get connection status for an MST port
2376 * @mgr: manager for this port
2377 * @port: unverified pointer to a port
2379 * This returns the current connection state for a port. It validates the
2380 * port pointer still exists so the caller doesn't require a reference
2382 enum drm_connector_status
drm_dp_mst_detect_port(struct drm_connector
*connector
,
2383 struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2385 enum drm_connector_status status
= connector_status_disconnected
;
2387 /* we need to search for the port in the mgr in case its gone */
2388 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2390 return connector_status_disconnected
;
2395 switch (port
->pdt
) {
2396 case DP_PEER_DEVICE_NONE
:
2397 case DP_PEER_DEVICE_MST_BRANCHING
:
2400 case DP_PEER_DEVICE_SST_SINK
:
2401 status
= connector_status_connected
;
2402 /* for logical ports - cache the EDID */
2403 if (port
->port_num
>= 8 && !port
->cached_edid
) {
2404 port
->cached_edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2407 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
2409 status
= connector_status_connected
;
2413 drm_dp_put_port(port
);
2416 EXPORT_SYMBOL(drm_dp_mst_detect_port
);
2419 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2420 * @mgr: manager for this port
2421 * @port: unverified pointer to a port.
2423 * This returns whether the port supports audio or not.
2425 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr
*mgr
,
2426 struct drm_dp_mst_port
*port
)
2430 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2433 ret
= port
->has_audio
;
2434 drm_dp_put_port(port
);
2437 EXPORT_SYMBOL(drm_dp_mst_port_has_audio
);
2440 * drm_dp_mst_get_edid() - get EDID for an MST port
2441 * @connector: toplevel connector to get EDID for
2442 * @mgr: manager for this port
2443 * @port: unverified pointer to a port.
2445 * This returns an EDID for the port connected to a connector,
2446 * It validates the pointer still exists so the caller doesn't require a
2449 struct edid
*drm_dp_mst_get_edid(struct drm_connector
*connector
, struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2451 struct edid
*edid
= NULL
;
2453 /* we need to search for the port in the mgr in case its gone */
2454 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2458 if (port
->cached_edid
)
2459 edid
= drm_edid_duplicate(port
->cached_edid
);
2461 edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2462 drm_mode_connector_set_tile_property(connector
);
2464 port
->has_audio
= drm_detect_monitor_audio(edid
);
2465 drm_dp_put_port(port
);
2468 EXPORT_SYMBOL(drm_dp_mst_get_edid
);
2471 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2472 * @mgr: manager to use
2473 * @pbn: payload bandwidth to convert into slots.
2475 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
,
2480 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2482 if (num_slots
> mgr
->avail_slots
)
2486 EXPORT_SYMBOL(drm_dp_find_vcpi_slots
);
2488 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
2489 struct drm_dp_vcpi
*vcpi
, int pbn
)
2494 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2496 if (num_slots
> mgr
->avail_slots
)
2500 vcpi
->aligned_pbn
= num_slots
* mgr
->pbn_div
;
2501 vcpi
->num_slots
= num_slots
;
2503 ret
= drm_dp_mst_assign_payload_id(mgr
, vcpi
);
2510 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2511 * @mgr: manager for this port
2512 * @port: port to allocate a virtual channel for.
2513 * @pbn: payload bandwidth number to request
2514 * @slots: returned number of slots for this PBN.
2516 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
, int pbn
, int *slots
)
2520 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2524 if (port
->vcpi
.vcpi
> 0) {
2525 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port
->vcpi
.vcpi
, port
->vcpi
.pbn
, pbn
);
2526 if (pbn
== port
->vcpi
.pbn
) {
2527 *slots
= port
->vcpi
.num_slots
;
2528 drm_dp_put_port(port
);
2533 ret
= drm_dp_init_vcpi(mgr
, &port
->vcpi
, pbn
);
2535 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn
, mgr
->pbn_div
), mgr
->avail_slots
, ret
);
2538 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn
, port
->vcpi
.num_slots
);
2539 *slots
= port
->vcpi
.num_slots
;
2541 drm_dp_put_port(port
);
2546 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi
);
2548 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2551 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2555 slots
= port
->vcpi
.num_slots
;
2556 drm_dp_put_port(port
);
2559 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots
);
2562 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2563 * @mgr: manager for this port
2564 * @port: unverified pointer to a port.
2566 * This just resets the number of slots for the ports VCPI for later programming.
2568 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2570 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2573 port
->vcpi
.num_slots
= 0;
2574 drm_dp_put_port(port
);
2576 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots
);
2579 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2580 * @mgr: manager for this port
2581 * @port: unverified port to deallocate vcpi for
2583 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2585 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2589 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
2590 port
->vcpi
.num_slots
= 0;
2592 port
->vcpi
.aligned_pbn
= 0;
2593 port
->vcpi
.vcpi
= 0;
2594 drm_dp_put_port(port
);
2596 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi
);
2598 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
2599 int id
, struct drm_dp_payload
*payload
)
2601 u8 payload_alloc
[3], status
;
2605 drm_dp_dpcd_writeb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
,
2606 DP_PAYLOAD_TABLE_UPDATED
);
2608 payload_alloc
[0] = id
;
2609 payload_alloc
[1] = payload
->start_slot
;
2610 payload_alloc
[2] = payload
->num_slots
;
2612 ret
= drm_dp_dpcd_write(mgr
->aux
, DP_PAYLOAD_ALLOCATE_SET
, payload_alloc
, 3);
2614 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret
);
2619 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2621 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
2625 if (!(status
& DP_PAYLOAD_TABLE_UPDATED
)) {
2628 usleep_range(10000, 20000);
2631 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status
);
2642 * drm_dp_check_act_status() - Check ACT handled status.
2643 * @mgr: manager to use
2645 * Check the payload status bits in the DPCD for ACT handled completion.
2647 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr
*mgr
)
2654 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2657 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
2661 if (status
& DP_PAYLOAD_ACT_HANDLED
)
2666 } while (count
< 30);
2668 if (!(status
& DP_PAYLOAD_ACT_HANDLED
)) {
2669 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status
, count
);
2677 EXPORT_SYMBOL(drm_dp_check_act_status
);
2680 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2681 * @clock: dot clock for the mode
2682 * @bpp: bpp for the mode.
2684 * This uses the formula in the spec to calculate the PBN value for a mode.
2686 int drm_dp_calc_pbn_mode(int clock
, int bpp
)
2696 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2697 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2698 * common multiplier to render an integer PBN for all link rate/lane
2699 * counts combinations
2701 * peak_kbps *= (1006/1000)
2702 * peak_kbps *= (64/54)
2703 * peak_kbps *= 8 convert to bytes
2706 numerator
= 64 * 1006;
2707 denominator
= 54 * 8 * 1000 * 1000;
2710 peak_kbps
= drm_fixp_from_fraction(kbps
, denominator
);
2712 return drm_fixp2int_ceil(peak_kbps
);
2714 EXPORT_SYMBOL(drm_dp_calc_pbn_mode
);
2716 static int test_calc_pbn_mode(void)
2719 ret
= drm_dp_calc_pbn_mode(154000, 30);
2721 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2722 154000, 30, 689, ret
);
2725 ret
= drm_dp_calc_pbn_mode(234000, 30);
2727 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2728 234000, 30, 1047, ret
);
2731 ret
= drm_dp_calc_pbn_mode(297000, 24);
2733 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2734 297000, 24, 1063, ret
);
2740 /* we want to kick the TX after we've ack the up/down IRQs. */
2741 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
)
2743 queue_work(system_long_wq
, &mgr
->tx_work
);
2746 static void drm_dp_mst_dump_mstb(struct seq_file
*m
,
2747 struct drm_dp_mst_branch
*mstb
)
2749 struct drm_dp_mst_port
*port
;
2750 int tabs
= mstb
->lct
;
2754 for (i
= 0; i
< tabs
; i
++)
2758 seq_printf(m
, "%smst: %p, %d\n", prefix
, mstb
, mstb
->num_ports
);
2759 list_for_each_entry(port
, &mstb
->ports
, next
) {
2760 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
);
2762 drm_dp_mst_dump_mstb(m
, port
->mstb
);
2766 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
2771 for (i
= 0; i
< 4; i
++) {
2772 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
+ (i
* 16), &buf
[i
* 16], 16);
2781 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr
*mgr
,
2782 struct drm_dp_mst_port
*port
, char *name
,
2785 struct edid
*mst_edid
;
2787 mst_edid
= drm_dp_mst_get_edid(port
->connector
, mgr
, port
);
2788 drm_edid_get_monitor_name(mst_edid
, name
, namelen
);
2792 * drm_dp_mst_dump_topology(): dump topology to seq file.
2793 * @m: seq_file to dump output to
2794 * @mgr: manager to dump current topology for.
2796 * helper to dump MST topology to a seq file for debugfs.
2798 void drm_dp_mst_dump_topology(struct seq_file
*m
,
2799 struct drm_dp_mst_topology_mgr
*mgr
)
2802 struct drm_dp_mst_port
*port
;
2804 mutex_lock(&mgr
->lock
);
2805 if (mgr
->mst_primary
)
2806 drm_dp_mst_dump_mstb(m
, mgr
->mst_primary
);
2809 mutex_unlock(&mgr
->lock
);
2811 mutex_lock(&mgr
->payload_lock
);
2812 seq_printf(m
, "vcpi: %lx %lx %d\n", mgr
->payload_mask
, mgr
->vcpi_mask
,
2815 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
2816 if (mgr
->proposed_vcpis
[i
]) {
2819 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
2820 fetch_monitor_name(mgr
, port
, name
, sizeof(name
));
2821 seq_printf(m
, "vcpi %d: %d %d %d sink name: %s\n", i
,
2822 port
->port_num
, port
->vcpi
.vcpi
,
2823 port
->vcpi
.num_slots
,
2824 (*name
!= 0) ? name
: "Unknown");
2826 seq_printf(m
, "vcpi %d:unused\n", i
);
2828 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
2829 seq_printf(m
, "payload %d: %d, %d, %d\n",
2831 mgr
->payloads
[i
].payload_state
,
2832 mgr
->payloads
[i
].start_slot
,
2833 mgr
->payloads
[i
].num_slots
);
2837 mutex_unlock(&mgr
->payload_lock
);
2839 mutex_lock(&mgr
->lock
);
2840 if (mgr
->mst_primary
) {
2844 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, buf
, DP_RECEIVER_CAP_SIZE
);
2845 seq_printf(m
, "dpcd: ");
2846 for (i
= 0; i
< DP_RECEIVER_CAP_SIZE
; i
++)
2847 seq_printf(m
, "%02x ", buf
[i
]);
2848 seq_printf(m
, "\n");
2849 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_FAUX_CAP
, buf
, 2);
2850 seq_printf(m
, "faux/mst: ");
2851 for (i
= 0; i
< 2; i
++)
2852 seq_printf(m
, "%02x ", buf
[i
]);
2853 seq_printf(m
, "\n");
2854 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_MSTM_CTRL
, buf
, 1);
2855 seq_printf(m
, "mst ctrl: ");
2856 for (i
= 0; i
< 1; i
++)
2857 seq_printf(m
, "%02x ", buf
[i
]);
2858 seq_printf(m
, "\n");
2860 /* dump the standard OUI branch header */
2861 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_BRANCH_OUI
, buf
, DP_BRANCH_OUI_HEADER_SIZE
);
2862 seq_printf(m
, "branch oui: ");
2863 for (i
= 0; i
< 0x3; i
++)
2864 seq_printf(m
, "%02x", buf
[i
]);
2865 seq_printf(m
, " devid: ");
2866 for (i
= 0x3; i
< 0x8 && buf
[i
]; i
++)
2867 seq_printf(m
, "%c", buf
[i
]);
2869 seq_printf(m
, " revision: hw: %x.%x sw: %x.%x", buf
[0x9] >> 4, buf
[0x9] & 0xf, buf
[0xa], buf
[0xb]);
2870 seq_printf(m
, "\n");
2871 bret
= dump_dp_payload_table(mgr
, buf
);
2873 seq_printf(m
, "payload table: ");
2874 for (i
= 0; i
< 63; i
++)
2875 seq_printf(m
, "%02x ", buf
[i
]);
2876 seq_printf(m
, "\n");
2881 mutex_unlock(&mgr
->lock
);
2884 EXPORT_SYMBOL(drm_dp_mst_dump_topology
);
2886 static void drm_dp_tx_work(struct work_struct
*work
)
2888 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, tx_work
);
2890 mutex_lock(&mgr
->qlock
);
2891 if (mgr
->tx_down_in_progress
)
2892 process_single_down_tx_qlock(mgr
);
2893 mutex_unlock(&mgr
->qlock
);
2896 static void drm_dp_free_mst_port(struct kref
*kref
)
2898 struct drm_dp_mst_port
*port
= container_of(kref
, struct drm_dp_mst_port
, kref
);
2899 kref_put(&port
->parent
->kref
, drm_dp_free_mst_branch_device
);
2903 static void drm_dp_destroy_connector_work(struct work_struct
*work
)
2905 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, destroy_connector_work
);
2906 struct drm_dp_mst_port
*port
;
2907 bool send_hotplug
= false;
2909 * Not a regular list traverse as we have to drop the destroy
2910 * connector lock before destroying the connector, to avoid AB->BA
2911 * ordering between this lock and the config mutex.
2914 mutex_lock(&mgr
->destroy_connector_lock
);
2915 port
= list_first_entry_or_null(&mgr
->destroy_connector_list
, struct drm_dp_mst_port
, next
);
2917 mutex_unlock(&mgr
->destroy_connector_lock
);
2920 list_del(&port
->next
);
2921 mutex_unlock(&mgr
->destroy_connector_lock
);
2923 kref_init(&port
->kref
);
2924 INIT_LIST_HEAD(&port
->next
);
2926 mgr
->cbs
->destroy_connector(mgr
, port
->connector
);
2928 drm_dp_port_teardown_pdt(port
, port
->pdt
);
2930 if (!port
->input
&& port
->vcpi
.vcpi
> 0) {
2931 drm_dp_mst_reset_vcpi_slots(mgr
, port
);
2932 drm_dp_update_payload_part1(mgr
);
2933 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
2936 kref_put(&port
->kref
, drm_dp_free_mst_port
);
2937 send_hotplug
= true;
2940 (*mgr
->cbs
->hotplug
)(mgr
);
2944 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2945 * @mgr: manager struct to initialise
2946 * @dev: device providing this structure - for i2c addition.
2947 * @aux: DP helper aux channel to talk to this device
2948 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2949 * @max_payloads: maximum number of payloads this GPU can source
2950 * @conn_base_id: the connector object ID the MST device is connected to.
2952 * Return 0 for success, or negative error code on failure
2954 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr
*mgr
,
2955 struct device
*dev
, struct drm_dp_aux
*aux
,
2956 int max_dpcd_transaction_bytes
,
2957 int max_payloads
, int conn_base_id
)
2959 lockinit(&mgr
->lock
, "drmml", 0, LK_CANRECURSE
);
2960 lockinit(&mgr
->qlock
, "drmmql", 0, LK_CANRECURSE
);
2961 lockinit(&mgr
->payload_lock
, "drmmpl", 0, LK_CANRECURSE
);
2962 lockinit(&mgr
->destroy_connector_lock
, "drmmdcl", 0, LK_CANRECURSE
);
2963 INIT_LIST_HEAD(&mgr
->tx_msg_downq
);
2964 INIT_LIST_HEAD(&mgr
->destroy_connector_list
);
2965 INIT_WORK(&mgr
->work
, drm_dp_mst_link_probe_work
);
2966 INIT_WORK(&mgr
->tx_work
, drm_dp_tx_work
);
2967 INIT_WORK(&mgr
->destroy_connector_work
, drm_dp_destroy_connector_work
);
2968 init_waitqueue_head(&mgr
->tx_waitq
);
2971 mgr
->max_dpcd_transaction_bytes
= max_dpcd_transaction_bytes
;
2972 mgr
->max_payloads
= max_payloads
;
2973 mgr
->conn_base_id
= conn_base_id
;
2974 if (max_payloads
+ 1 > sizeof(mgr
->payload_mask
) * 8 ||
2975 max_payloads
+ 1 > sizeof(mgr
->vcpi_mask
) * 8)
2977 mgr
->payloads
= kcalloc(max_payloads
, sizeof(struct drm_dp_payload
), GFP_KERNEL
);
2980 mgr
->proposed_vcpis
= kcalloc(max_payloads
, sizeof(struct drm_dp_vcpi
*), GFP_KERNEL
);
2981 if (!mgr
->proposed_vcpis
)
2983 set_bit(0, &mgr
->payload_mask
);
2984 if (test_calc_pbn_mode() < 0)
2985 DRM_ERROR("MST PBN self-test failed\n");
2989 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init
);
2992 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
2993 * @mgr: manager to destroy
2995 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr
*mgr
)
2997 flush_work(&mgr
->work
);
2998 flush_work(&mgr
->destroy_connector_work
);
2999 mutex_lock(&mgr
->payload_lock
);
3000 kfree(mgr
->payloads
);
3001 mgr
->payloads
= NULL
;
3002 kfree(mgr
->proposed_vcpis
);
3003 mgr
->proposed_vcpis
= NULL
;
3004 mutex_unlock(&mgr
->payload_lock
);
3008 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy
);
3011 static int drm_dp_mst_i2c_xfer(struct i2c_adapter
*adapter
, struct i2c_msg
*msgs
,
3014 struct drm_dp_aux
*aux
= adapter
->algo_data
;
3015 struct drm_dp_mst_port
*port
= container_of(aux
, struct drm_dp_mst_port
, aux
);
3016 struct drm_dp_mst_branch
*mstb
;
3017 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
3019 bool reading
= false;
3020 struct drm_dp_sideband_msg_req_body msg
;
3021 struct drm_dp_sideband_msg_tx
*txmsg
= NULL
;
3024 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
3028 /* construct i2c msg */
3029 /* see if last msg is a read */
3030 if (msgs
[num
- 1].flags
& I2C_M_RD
)
3033 if (!reading
|| (num
- 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS
)) {
3034 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3039 memset(&msg
, 0, sizeof(msg
));
3040 msg
.req_type
= DP_REMOTE_I2C_READ
;
3041 msg
.u
.i2c_read
.num_transactions
= num
- 1;
3042 msg
.u
.i2c_read
.port_number
= port
->port_num
;
3043 for (i
= 0; i
< num
- 1; i
++) {
3044 msg
.u
.i2c_read
.transactions
[i
].i2c_dev_id
= msgs
[i
].addr
;
3045 msg
.u
.i2c_read
.transactions
[i
].num_bytes
= msgs
[i
].len
;
3046 msg
.u
.i2c_read
.transactions
[i
].bytes
= msgs
[i
].buf
;
3048 msg
.u
.i2c_read
.read_i2c_device_id
= msgs
[num
- 1].addr
;
3049 msg
.u
.i2c_read
.num_bytes_read
= msgs
[num
- 1].len
;
3051 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3058 drm_dp_encode_sideband_req(&msg
, txmsg
);
3060 drm_dp_queue_down_tx(mgr
, txmsg
);
3062 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3065 if (txmsg
->reply
.reply_type
== 1) { /* got a NAK back */
3069 if (txmsg
->reply
.u
.remote_i2c_read_ack
.num_bytes
!= msgs
[num
- 1].len
) {
3073 memcpy(msgs
[num
- 1].buf
, txmsg
->reply
.u
.remote_i2c_read_ack
.bytes
, msgs
[num
- 1].len
);
3078 drm_dp_put_mst_branch_device(mstb
);
3082 static u32
drm_dp_mst_i2c_functionality(struct i2c_adapter
*adapter
)
3084 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
3085 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
3086 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
3087 I2C_FUNC_10BIT_ADDR
;
3090 static const struct i2c_algorithm drm_dp_mst_i2c_algo
= {
3091 .functionality
= drm_dp_mst_i2c_functionality
,
3092 .master_xfer
= drm_dp_mst_i2c_xfer
,
3096 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3097 * @aux: DisplayPort AUX channel
3099 * Returns 0 on success or a negative error code on failure.
3101 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux
*aux
)
3103 aux
->ddc
.algo
= &drm_dp_mst_i2c_algo
;
3104 aux
->ddc
.algo_data
= aux
;
3105 aux
->ddc
.retries
= 3;
3108 aux
->ddc
.class = I2C_CLASS_DDC
;
3109 aux
->ddc
.owner
= THIS_MODULE
;
3111 aux
->ddc
.dev
.parent
= aux
->dev
;
3113 aux
->ddc
.dev
.of_node
= aux
->dev
->of_node
;
3116 strlcpy(aux
->ddc
.name
, aux
->name
? aux
->name
: dev_name(aux
->dev
),
3117 sizeof(aux
->ddc
.name
));
3119 return i2c_add_adapter(&aux
->ddc
);
3123 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3124 * @aux: DisplayPort AUX channel
3126 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux
*aux
)
3128 i2c_del_adapter(&aux
->ddc
);