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);
333 /* get length contained in this portion */
334 msg
->curchunk_len
= recv_hdr
.msg_len
;
335 msg
->curchunk_hdrlen
= hdrlen
;
337 /* we have already gotten an somt - don't bother parsing */
338 if (recv_hdr
.somt
&& msg
->have_somt
)
342 memcpy(&msg
->initial_hdr
, &recv_hdr
, sizeof(struct drm_dp_sideband_msg_hdr
));
343 msg
->have_somt
= true;
346 msg
->have_eomt
= true;
348 /* copy the bytes for the remainder of this header chunk */
349 msg
->curchunk_idx
= min(msg
->curchunk_len
, (u8
)(replybuflen
- hdrlen
));
350 memcpy(&msg
->chunk
[0], replybuf
+ hdrlen
, msg
->curchunk_idx
);
352 memcpy(&msg
->chunk
[msg
->curchunk_idx
], replybuf
, replybuflen
);
353 msg
->curchunk_idx
+= replybuflen
;
356 if (msg
->curchunk_idx
>= msg
->curchunk_len
) {
358 crc4
= drm_dp_msg_data_crc4(msg
->chunk
, msg
->curchunk_len
- 1);
359 /* copy chunk into bigger msg */
360 memcpy(&msg
->msg
[msg
->curlen
], msg
->chunk
, msg
->curchunk_len
- 1);
361 msg
->curlen
+= msg
->curchunk_len
- 1;
366 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx
*raw
,
367 struct drm_dp_sideband_msg_reply_body
*repmsg
)
371 memcpy(repmsg
->u
.link_addr
.guid
, &raw
->msg
[idx
], 16);
373 repmsg
->u
.link_addr
.nports
= raw
->msg
[idx
] & 0xf;
375 if (idx
> raw
->curlen
)
377 for (i
= 0; i
< repmsg
->u
.link_addr
.nports
; i
++) {
378 if (raw
->msg
[idx
] & 0x80)
379 repmsg
->u
.link_addr
.ports
[i
].input_port
= 1;
381 repmsg
->u
.link_addr
.ports
[i
].peer_device_type
= (raw
->msg
[idx
] >> 4) & 0x7;
382 repmsg
->u
.link_addr
.ports
[i
].port_number
= (raw
->msg
[idx
] & 0xf);
385 if (idx
> raw
->curlen
)
387 repmsg
->u
.link_addr
.ports
[i
].mcs
= (raw
->msg
[idx
] >> 7) & 0x1;
388 repmsg
->u
.link_addr
.ports
[i
].ddps
= (raw
->msg
[idx
] >> 6) & 0x1;
389 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0)
390 repmsg
->u
.link_addr
.ports
[i
].legacy_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
392 if (idx
> raw
->curlen
)
394 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0) {
395 repmsg
->u
.link_addr
.ports
[i
].dpcd_revision
= (raw
->msg
[idx
]);
397 if (idx
> raw
->curlen
)
399 memcpy(repmsg
->u
.link_addr
.ports
[i
].peer_guid
, &raw
->msg
[idx
], 16);
401 if (idx
> raw
->curlen
)
403 repmsg
->u
.link_addr
.ports
[i
].num_sdp_streams
= (raw
->msg
[idx
] >> 4) & 0xf;
404 repmsg
->u
.link_addr
.ports
[i
].num_sdp_stream_sinks
= (raw
->msg
[idx
] & 0xf);
408 if (idx
> raw
->curlen
)
414 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
418 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx
*raw
,
419 struct drm_dp_sideband_msg_reply_body
*repmsg
)
422 repmsg
->u
.remote_dpcd_read_ack
.port_number
= raw
->msg
[idx
] & 0xf;
424 if (idx
> raw
->curlen
)
426 repmsg
->u
.remote_dpcd_read_ack
.num_bytes
= raw
->msg
[idx
];
427 if (idx
> raw
->curlen
)
430 memcpy(repmsg
->u
.remote_dpcd_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_dpcd_read_ack
.num_bytes
);
433 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
437 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx
*raw
,
438 struct drm_dp_sideband_msg_reply_body
*repmsg
)
441 repmsg
->u
.remote_dpcd_write_ack
.port_number
= raw
->msg
[idx
] & 0xf;
443 if (idx
> raw
->curlen
)
447 DRM_DEBUG_KMS("parse length fail %d %d\n", idx
, raw
->curlen
);
451 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx
*raw
,
452 struct drm_dp_sideband_msg_reply_body
*repmsg
)
456 repmsg
->u
.remote_i2c_read_ack
.port_number
= (raw
->msg
[idx
] & 0xf);
458 if (idx
> raw
->curlen
)
460 repmsg
->u
.remote_i2c_read_ack
.num_bytes
= raw
->msg
[idx
];
463 memcpy(repmsg
->u
.remote_i2c_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_i2c_read_ack
.num_bytes
);
466 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx
, raw
->curlen
);
470 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx
*raw
,
471 struct drm_dp_sideband_msg_reply_body
*repmsg
)
474 repmsg
->u
.path_resources
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
476 if (idx
> raw
->curlen
)
478 repmsg
->u
.path_resources
.full_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
480 if (idx
> raw
->curlen
)
482 repmsg
->u
.path_resources
.avail_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
484 if (idx
> raw
->curlen
)
488 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx
, raw
->curlen
);
492 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
493 struct drm_dp_sideband_msg_reply_body
*repmsg
)
496 repmsg
->u
.allocate_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
498 if (idx
> raw
->curlen
)
500 repmsg
->u
.allocate_payload
.vcpi
= raw
->msg
[idx
];
502 if (idx
> raw
->curlen
)
504 repmsg
->u
.allocate_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
506 if (idx
> raw
->curlen
)
510 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx
, raw
->curlen
);
514 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
515 struct drm_dp_sideband_msg_reply_body
*repmsg
)
518 repmsg
->u
.query_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
520 if (idx
> raw
->curlen
)
522 repmsg
->u
.query_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
524 if (idx
> raw
->curlen
)
528 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx
, raw
->curlen
);
532 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx
*raw
,
533 struct drm_dp_sideband_msg_reply_body
*msg
)
535 memset(msg
, 0, sizeof(*msg
));
536 msg
->reply_type
= (raw
->msg
[0] & 0x80) >> 7;
537 msg
->req_type
= (raw
->msg
[0] & 0x7f);
539 if (msg
->reply_type
) {
540 memcpy(msg
->u
.nak
.guid
, &raw
->msg
[1], 16);
541 msg
->u
.nak
.reason
= raw
->msg
[17];
542 msg
->u
.nak
.nak_data
= raw
->msg
[18];
546 switch (msg
->req_type
) {
547 case DP_LINK_ADDRESS
:
548 return drm_dp_sideband_parse_link_address(raw
, msg
);
549 case DP_QUERY_PAYLOAD
:
550 return drm_dp_sideband_parse_query_payload_ack(raw
, msg
);
551 case DP_REMOTE_DPCD_READ
:
552 return drm_dp_sideband_parse_remote_dpcd_read(raw
, msg
);
553 case DP_REMOTE_DPCD_WRITE
:
554 return drm_dp_sideband_parse_remote_dpcd_write(raw
, msg
);
555 case DP_REMOTE_I2C_READ
:
556 return drm_dp_sideband_parse_remote_i2c_read_ack(raw
, msg
);
557 case DP_ENUM_PATH_RESOURCES
:
558 return drm_dp_sideband_parse_enum_path_resources_ack(raw
, msg
);
559 case DP_ALLOCATE_PAYLOAD
:
560 return drm_dp_sideband_parse_allocate_payload_ack(raw
, msg
);
562 DRM_ERROR("Got unknown reply 0x%02x\n", msg
->req_type
);
567 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
568 struct drm_dp_sideband_msg_req_body
*msg
)
572 msg
->u
.conn_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
574 if (idx
> raw
->curlen
)
577 memcpy(msg
->u
.conn_stat
.guid
, &raw
->msg
[idx
], 16);
579 if (idx
> raw
->curlen
)
582 msg
->u
.conn_stat
.legacy_device_plug_status
= (raw
->msg
[idx
] >> 6) & 0x1;
583 msg
->u
.conn_stat
.displayport_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
584 msg
->u
.conn_stat
.message_capability_status
= (raw
->msg
[idx
] >> 4) & 0x1;
585 msg
->u
.conn_stat
.input_port
= (raw
->msg
[idx
] >> 3) & 0x1;
586 msg
->u
.conn_stat
.peer_device_type
= (raw
->msg
[idx
] & 0x7);
590 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx
, raw
->curlen
);
594 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
595 struct drm_dp_sideband_msg_req_body
*msg
)
599 msg
->u
.resource_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
601 if (idx
> raw
->curlen
)
604 memcpy(msg
->u
.resource_stat
.guid
, &raw
->msg
[idx
], 16);
606 if (idx
> raw
->curlen
)
609 msg
->u
.resource_stat
.available_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
613 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx
, raw
->curlen
);
617 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx
*raw
,
618 struct drm_dp_sideband_msg_req_body
*msg
)
620 memset(msg
, 0, sizeof(*msg
));
621 msg
->req_type
= (raw
->msg
[0] & 0x7f);
623 switch (msg
->req_type
) {
624 case DP_CONNECTION_STATUS_NOTIFY
:
625 return drm_dp_sideband_parse_connection_status_notify(raw
, msg
);
626 case DP_RESOURCE_STATUS_NOTIFY
:
627 return drm_dp_sideband_parse_resource_status_notify(raw
, msg
);
629 DRM_ERROR("Got unknown request 0x%02x\n", msg
->req_type
);
634 static int build_dpcd_write(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
, u8
*bytes
)
636 struct drm_dp_sideband_msg_req_body req
;
638 req
.req_type
= DP_REMOTE_DPCD_WRITE
;
639 req
.u
.dpcd_write
.port_number
= port_num
;
640 req
.u
.dpcd_write
.dpcd_address
= offset
;
641 req
.u
.dpcd_write
.num_bytes
= num_bytes
;
642 req
.u
.dpcd_write
.bytes
= bytes
;
643 drm_dp_encode_sideband_req(&req
, msg
);
648 static int build_link_address(struct drm_dp_sideband_msg_tx
*msg
)
650 struct drm_dp_sideband_msg_req_body req
;
652 req
.req_type
= DP_LINK_ADDRESS
;
653 drm_dp_encode_sideband_req(&req
, msg
);
657 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx
*msg
, int port_num
)
659 struct drm_dp_sideband_msg_req_body req
;
661 req
.req_type
= DP_ENUM_PATH_RESOURCES
;
662 req
.u
.port_num
.port_number
= port_num
;
663 drm_dp_encode_sideband_req(&req
, msg
);
664 msg
->path_msg
= true;
668 static int build_allocate_payload(struct drm_dp_sideband_msg_tx
*msg
, int port_num
,
669 u8 vcpi
, uint16_t pbn
)
671 struct drm_dp_sideband_msg_req_body req
;
672 memset(&req
, 0, sizeof(req
));
673 req
.req_type
= DP_ALLOCATE_PAYLOAD
;
674 req
.u
.allocate_payload
.port_number
= port_num
;
675 req
.u
.allocate_payload
.vcpi
= vcpi
;
676 req
.u
.allocate_payload
.pbn
= pbn
;
677 drm_dp_encode_sideband_req(&req
, msg
);
678 msg
->path_msg
= true;
682 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
683 struct drm_dp_vcpi
*vcpi
)
687 mutex_lock(&mgr
->payload_lock
);
688 ret
= find_first_zero_bit(&mgr
->payload_mask
, mgr
->max_payloads
+ 1);
689 if (ret
> mgr
->max_payloads
) {
691 DRM_DEBUG_KMS("out of payload ids %d\n", ret
);
695 vcpi_ret
= find_first_zero_bit(&mgr
->vcpi_mask
, mgr
->max_payloads
+ 1);
696 if (vcpi_ret
> mgr
->max_payloads
) {
698 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret
);
702 set_bit(ret
, &mgr
->payload_mask
);
703 set_bit(vcpi_ret
, &mgr
->vcpi_mask
);
704 vcpi
->vcpi
= vcpi_ret
+ 1;
705 mgr
->proposed_vcpis
[ret
- 1] = vcpi
;
707 mutex_unlock(&mgr
->payload_lock
);
711 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
718 mutex_lock(&mgr
->payload_lock
);
719 DRM_DEBUG_KMS("putting payload %d\n", vcpi
);
720 clear_bit(vcpi
- 1, &mgr
->vcpi_mask
);
722 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
723 if (mgr
->proposed_vcpis
[i
])
724 if (mgr
->proposed_vcpis
[i
]->vcpi
== vcpi
) {
725 mgr
->proposed_vcpis
[i
] = NULL
;
726 clear_bit(i
+ 1, &mgr
->payload_mask
);
729 mutex_unlock(&mgr
->payload_lock
);
732 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr
*mgr
,
733 struct drm_dp_sideband_msg_tx
*txmsg
)
738 * All updates to txmsg->state are protected by mgr->qlock, and the two
739 * cases we check here are terminal states. For those the barriers
740 * provided by the wake_up/wait_event pair are enough.
742 ret
= (txmsg
->state
== DRM_DP_SIDEBAND_TX_RX
||
743 txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
);
747 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch
*mstb
,
748 struct drm_dp_sideband_msg_tx
*txmsg
)
750 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
753 ret
= wait_event_timeout(mgr
->tx_waitq
,
754 check_txmsg_state(mgr
, txmsg
),
756 mutex_lock(&mstb
->mgr
->qlock
);
758 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
) {
763 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg
, txmsg
->state
, txmsg
->seqno
);
765 /* dump some state */
769 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
||
770 txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
) {
771 list_del(&txmsg
->next
);
774 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
||
775 txmsg
->state
== DRM_DP_SIDEBAND_TX_SENT
) {
776 mstb
->tx_slots
[txmsg
->seqno
] = NULL
;
780 mutex_unlock(&mgr
->qlock
);
785 static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct
, u8
*rad
)
787 struct drm_dp_mst_branch
*mstb
;
789 mstb
= kzalloc(sizeof(*mstb
), GFP_KERNEL
);
795 memcpy(mstb
->rad
, rad
, lct
/ 2);
796 INIT_LIST_HEAD(&mstb
->ports
);
797 kref_init(&mstb
->kref
);
801 static void drm_dp_destroy_mst_branch_device(struct kref
*kref
)
803 struct drm_dp_mst_branch
*mstb
= container_of(kref
, struct drm_dp_mst_branch
, kref
);
804 struct drm_dp_mst_port
*port
, *tmp
;
805 bool wake_tx
= false;
808 * destroy all ports - don't need lock
809 * as there are no more references to the mst branch
810 * device at this point.
812 list_for_each_entry_safe(port
, tmp
, &mstb
->ports
, next
) {
813 list_del(&port
->next
);
814 drm_dp_put_port(port
);
817 /* drop any tx slots msg */
818 mutex_lock(&mstb
->mgr
->qlock
);
819 if (mstb
->tx_slots
[0]) {
820 mstb
->tx_slots
[0]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
821 mstb
->tx_slots
[0] = NULL
;
824 if (mstb
->tx_slots
[1]) {
825 mstb
->tx_slots
[1]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
826 mstb
->tx_slots
[1] = NULL
;
829 mutex_unlock(&mstb
->mgr
->qlock
);
832 wake_up(&mstb
->mgr
->tx_waitq
);
836 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch
*mstb
)
838 kref_put(&mstb
->kref
, drm_dp_destroy_mst_branch_device
);
842 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port
*port
, int old_pdt
)
844 struct drm_dp_mst_branch
*mstb
;
847 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
848 case DP_PEER_DEVICE_SST_SINK
:
849 /* remove i2c over sideband */
850 drm_dp_mst_unregister_i2c_bus(&port
->aux
);
852 case DP_PEER_DEVICE_MST_BRANCHING
:
855 drm_dp_put_mst_branch_device(mstb
);
860 static void drm_dp_destroy_port(struct kref
*kref
)
862 struct drm_dp_mst_port
*port
= container_of(kref
, struct drm_dp_mst_port
, kref
);
863 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
866 port
->vcpi
.num_slots
= 0;
868 kfree(port
->cached_edid
);
871 * The only time we don't have a connector
872 * on an output port is if the connector init
875 if (port
->connector
) {
876 /* we can't destroy the connector here, as
877 * we might be holding the mode_config.mutex
878 * from an EDID retrieval */
880 mutex_lock(&mgr
->destroy_connector_lock
);
881 list_add(&port
->next
, &mgr
->destroy_connector_list
);
882 mutex_unlock(&mgr
->destroy_connector_lock
);
883 schedule_work(&mgr
->destroy_connector_work
);
886 /* no need to clean up vcpi
887 * as if we have no connector we never setup a vcpi */
888 drm_dp_port_teardown_pdt(port
, port
->pdt
);
893 static void drm_dp_put_port(struct drm_dp_mst_port
*port
)
895 kref_put(&port
->kref
, drm_dp_destroy_port
);
898 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
)
900 struct drm_dp_mst_port
*port
;
901 struct drm_dp_mst_branch
*rmstb
;
902 if (to_find
== mstb
) {
903 kref_get(&mstb
->kref
);
906 list_for_each_entry(port
, &mstb
->ports
, next
) {
908 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(port
->mstb
, to_find
);
916 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
)
918 struct drm_dp_mst_branch
*rmstb
= NULL
;
919 mutex_lock(&mgr
->lock
);
920 if (mgr
->mst_primary
)
921 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(mgr
->mst_primary
, mstb
);
922 mutex_unlock(&mgr
->lock
);
926 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
)
928 struct drm_dp_mst_port
*port
, *mport
;
930 list_for_each_entry(port
, &mstb
->ports
, next
) {
931 if (port
== to_find
) {
932 kref_get(&port
->kref
);
936 mport
= drm_dp_mst_get_port_ref_locked(port
->mstb
, to_find
);
944 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
)
946 struct drm_dp_mst_port
*rport
= NULL
;
947 mutex_lock(&mgr
->lock
);
948 if (mgr
->mst_primary
)
949 rport
= drm_dp_mst_get_port_ref_locked(mgr
->mst_primary
, port
);
950 mutex_unlock(&mgr
->lock
);
954 static struct drm_dp_mst_port
*drm_dp_get_port(struct drm_dp_mst_branch
*mstb
, u8 port_num
)
956 struct drm_dp_mst_port
*port
;
958 list_for_each_entry(port
, &mstb
->ports
, next
) {
959 if (port
->port_num
== port_num
) {
960 kref_get(&port
->kref
);
969 * calculate a new RAD for this MST branch device
970 * if parent has an LCT of 2 then it has 1 nibble of RAD,
971 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
973 static u8
drm_dp_calculate_rad(struct drm_dp_mst_port
*port
,
976 int lct
= port
->parent
->lct
;
980 memcpy(rad
, port
->parent
->rad
, idx
);
981 shift
= (lct
% 2) ? 4 : 0;
985 rad
[idx
] |= port
->port_num
<< shift
;
990 * return sends link address for new mstb
992 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port
*port
)
996 bool send_link
= false;
998 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
999 case DP_PEER_DEVICE_SST_SINK
:
1000 /* add i2c over sideband */
1001 ret
= drm_dp_mst_register_i2c_bus(&port
->aux
);
1003 case DP_PEER_DEVICE_MST_BRANCHING
:
1004 lct
= drm_dp_calculate_rad(port
, rad
);
1006 port
->mstb
= drm_dp_add_mst_branch_device(lct
, rad
);
1007 port
->mstb
->mgr
= port
->mgr
;
1008 port
->mstb
->port_parent
= port
;
1016 static void drm_dp_check_port_guid(struct drm_dp_mst_branch
*mstb
,
1017 struct drm_dp_mst_port
*port
)
1020 if (port
->dpcd_rev
>= 0x12) {
1021 port
->guid_valid
= drm_dp_validate_guid(mstb
->mgr
, port
->guid
);
1022 if (!port
->guid_valid
) {
1023 ret
= drm_dp_send_dpcd_write(mstb
->mgr
,
1027 port
->guid_valid
= true;
1032 static void build_mst_prop_path(const struct drm_dp_mst_branch
*mstb
,
1035 size_t proppath_size
)
1039 snprintf(proppath
, proppath_size
, "mst:%d", mstb
->mgr
->conn_base_id
);
1040 for (i
= 0; i
< (mstb
->lct
- 1); i
++) {
1041 int shift
= (i
% 2) ? 0 : 4;
1042 int port_num
= mstb
->rad
[i
/ 2] >> shift
;
1043 snprintf(temp
, sizeof(temp
), "-%d", port_num
);
1044 strlcat(proppath
, temp
, proppath_size
);
1046 snprintf(temp
, sizeof(temp
), "-%d", pnum
);
1047 strlcat(proppath
, temp
, proppath_size
);
1050 static void drm_dp_add_port(struct drm_dp_mst_branch
*mstb
,
1052 struct drm_dp_link_addr_reply_port
*port_msg
)
1054 struct drm_dp_mst_port
*port
;
1056 bool created
= false;
1059 port
= drm_dp_get_port(mstb
, port_msg
->port_number
);
1061 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1064 kref_init(&port
->kref
);
1065 port
->parent
= mstb
;
1066 port
->port_num
= port_msg
->port_number
;
1067 port
->mgr
= mstb
->mgr
;
1068 port
->aux
.name
= "DPMST";
1069 port
->aux
.dev
= dev
;
1072 old_pdt
= port
->pdt
;
1073 old_ddps
= port
->ddps
;
1076 port
->pdt
= port_msg
->peer_device_type
;
1077 port
->input
= port_msg
->input_port
;
1078 port
->mcs
= port_msg
->mcs
;
1079 port
->ddps
= port_msg
->ddps
;
1080 port
->ldps
= port_msg
->legacy_device_plug_status
;
1081 port
->dpcd_rev
= port_msg
->dpcd_revision
;
1082 port
->num_sdp_streams
= port_msg
->num_sdp_streams
;
1083 port
->num_sdp_stream_sinks
= port_msg
->num_sdp_stream_sinks
;
1084 memcpy(port
->guid
, port_msg
->peer_guid
, 16);
1086 /* manage mstb port lists with mgr lock - take a reference
1089 mutex_lock(&mstb
->mgr
->lock
);
1090 kref_get(&port
->kref
);
1091 list_add(&port
->next
, &mstb
->ports
);
1092 mutex_unlock(&mstb
->mgr
->lock
);
1095 if (old_ddps
!= port
->ddps
) {
1097 drm_dp_check_port_guid(mstb
, port
);
1099 drm_dp_send_enum_path_resources(mstb
->mgr
, mstb
, port
);
1101 port
->guid_valid
= false;
1102 port
->available_pbn
= 0;
1106 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1107 drm_dp_port_teardown_pdt(port
, old_pdt
);
1109 ret
= drm_dp_port_setup_pdt(port
);
1111 drm_dp_send_link_address(mstb
->mgr
, port
->mstb
);
1114 if (created
&& !port
->input
) {
1117 build_mst_prop_path(mstb
, port
->port_num
, proppath
, sizeof(proppath
));
1118 port
->connector
= (*mstb
->mgr
->cbs
->add_connector
)(mstb
->mgr
, port
, proppath
);
1119 if (!port
->connector
) {
1120 /* remove it from the port list */
1121 mutex_lock(&mstb
->mgr
->lock
);
1122 list_del(&port
->next
);
1123 mutex_unlock(&mstb
->mgr
->lock
);
1124 /* drop port list reference */
1125 drm_dp_put_port(port
);
1128 if (port
->port_num
>= DP_MST_LOGICAL_PORT_0
) {
1129 port
->cached_edid
= drm_get_edid(port
->connector
, &port
->aux
.ddc
);
1130 drm_mode_connector_set_tile_property(port
->connector
);
1132 (*mstb
->mgr
->cbs
->register_connector
)(port
->connector
);
1136 /* put reference to this port */
1137 drm_dp_put_port(port
);
1140 static void drm_dp_update_port(struct drm_dp_mst_branch
*mstb
,
1141 struct drm_dp_connection_status_notify
*conn_stat
)
1143 struct drm_dp_mst_port
*port
;
1146 bool dowork
= false;
1147 port
= drm_dp_get_port(mstb
, conn_stat
->port_number
);
1151 old_ddps
= port
->ddps
;
1152 old_pdt
= port
->pdt
;
1153 port
->pdt
= conn_stat
->peer_device_type
;
1154 port
->mcs
= conn_stat
->message_capability_status
;
1155 port
->ldps
= conn_stat
->legacy_device_plug_status
;
1156 port
->ddps
= conn_stat
->displayport_device_plug_status
;
1158 if (old_ddps
!= port
->ddps
) {
1160 drm_dp_check_port_guid(mstb
, port
);
1163 port
->guid_valid
= false;
1164 port
->available_pbn
= 0;
1167 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1168 drm_dp_port_teardown_pdt(port
, old_pdt
);
1170 if (drm_dp_port_setup_pdt(port
))
1174 drm_dp_put_port(port
);
1176 queue_work(system_long_wq
, &mstb
->mgr
->work
);
1180 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr
*mgr
,
1183 struct drm_dp_mst_branch
*mstb
;
1184 struct drm_dp_mst_port
*port
;
1186 /* find the port by iterating down */
1188 mutex_lock(&mgr
->lock
);
1189 mstb
= mgr
->mst_primary
;
1191 for (i
= 0; i
< lct
- 1; i
++) {
1192 int shift
= (i
% 2) ? 0 : 4;
1193 int port_num
= rad
[i
/ 2] >> shift
;
1195 list_for_each_entry(port
, &mstb
->ports
, next
) {
1196 if (port
->port_num
== port_num
) {
1199 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct
, rad
[0]);
1207 kref_get(&mstb
->kref
);
1209 mutex_unlock(&mgr
->lock
);
1213 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1214 struct drm_dp_mst_branch
*mstb
)
1216 struct drm_dp_mst_port
*port
;
1217 struct drm_dp_mst_branch
*mstb_child
;
1218 if (!mstb
->link_address_sent
)
1219 drm_dp_send_link_address(mgr
, mstb
);
1221 list_for_each_entry(port
, &mstb
->ports
, next
) {
1228 if (!port
->available_pbn
)
1229 drm_dp_send_enum_path_resources(mgr
, mstb
, port
);
1232 mstb_child
= drm_dp_get_validated_mstb_ref(mgr
, port
->mstb
);
1234 drm_dp_check_and_send_link_address(mgr
, mstb_child
);
1235 drm_dp_put_mst_branch_device(mstb_child
);
1241 static void drm_dp_mst_link_probe_work(struct work_struct
*work
)
1243 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, work
);
1244 struct drm_dp_mst_branch
*mstb
;
1246 mutex_lock(&mgr
->lock
);
1247 mstb
= mgr
->mst_primary
;
1249 kref_get(&mstb
->kref
);
1251 mutex_unlock(&mgr
->lock
);
1253 drm_dp_check_and_send_link_address(mgr
, mstb
);
1254 drm_dp_put_mst_branch_device(mstb
);
1258 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
1261 static u8 zero_guid
[16];
1263 if (!memcmp(guid
, zero_guid
, 16)) {
1264 u64 salt
= get_jiffies_64();
1265 memcpy(&guid
[0], &salt
, sizeof(u64
));
1266 memcpy(&guid
[8], &salt
, sizeof(u64
));
1273 static int build_dpcd_read(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
)
1275 struct drm_dp_sideband_msg_req_body req
;
1277 req
.req_type
= DP_REMOTE_DPCD_READ
;
1278 req
.u
.dpcd_read
.port_number
= port_num
;
1279 req
.u
.dpcd_read
.dpcd_address
= offset
;
1280 req
.u
.dpcd_read
.num_bytes
= num_bytes
;
1281 drm_dp_encode_sideband_req(&req
, msg
);
1287 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1288 bool up
, u8
*msg
, int len
)
1291 int regbase
= up
? DP_SIDEBAND_MSG_UP_REP_BASE
: DP_SIDEBAND_MSG_DOWN_REQ_BASE
;
1292 int tosend
, total
, offset
;
1299 tosend
= min3(mgr
->max_dpcd_transaction_bytes
, 16, total
);
1301 ret
= drm_dp_dpcd_write(mgr
->aux
, regbase
+ offset
,
1304 if (ret
!= tosend
) {
1305 if (ret
== -EIO
&& retries
< 5) {
1309 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend
, ret
);
1315 } while (total
> 0);
1319 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr
*hdr
,
1320 struct drm_dp_sideband_msg_tx
*txmsg
)
1322 struct drm_dp_mst_branch
*mstb
= txmsg
->dst
;
1324 /* both msg slots are full */
1325 if (txmsg
->seqno
== -1) {
1326 if (mstb
->tx_slots
[0] && mstb
->tx_slots
[1]) {
1327 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__
);
1330 if (mstb
->tx_slots
[0] == NULL
&& mstb
->tx_slots
[1] == NULL
) {
1331 txmsg
->seqno
= mstb
->last_seqno
;
1332 mstb
->last_seqno
^= 1;
1333 } else if (mstb
->tx_slots
[0] == NULL
)
1337 mstb
->tx_slots
[txmsg
->seqno
] = txmsg
;
1340 hdr
->path_msg
= txmsg
->path_msg
;
1341 hdr
->lct
= mstb
->lct
;
1342 hdr
->lcr
= mstb
->lct
- 1;
1344 memcpy(hdr
->rad
, mstb
->rad
, mstb
->lct
/ 2);
1345 hdr
->seqno
= txmsg
->seqno
;
1349 * process a single block of the next message in the sideband queue
1351 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
1352 struct drm_dp_sideband_msg_tx
*txmsg
,
1356 struct drm_dp_sideband_msg_hdr hdr
;
1357 int len
, space
, idx
, tosend
;
1360 memset(&hdr
, 0, sizeof(struct drm_dp_sideband_msg_hdr
));
1362 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
) {
1364 txmsg
->state
= DRM_DP_SIDEBAND_TX_START_SEND
;
1367 /* make hdr from dst mst - for replies use seqno
1368 otherwise assign one */
1369 ret
= set_hdr_from_dst_qlock(&hdr
, txmsg
);
1373 /* amount left to send in this message */
1374 len
= txmsg
->cur_len
- txmsg
->cur_offset
;
1376 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1377 space
= 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr
);
1379 tosend
= min(len
, space
);
1380 if (len
== txmsg
->cur_len
)
1386 hdr
.msg_len
= tosend
+ 1;
1387 drm_dp_encode_sideband_msg_hdr(&hdr
, chunk
, &idx
);
1388 memcpy(&chunk
[idx
], &txmsg
->msg
[txmsg
->cur_offset
], tosend
);
1389 /* add crc at end */
1390 drm_dp_crc_sideband_chunk_req(&chunk
[idx
], tosend
);
1393 ret
= drm_dp_send_sideband_msg(mgr
, up
, chunk
, idx
);
1395 DRM_DEBUG_KMS("sideband msg failed to send\n");
1399 txmsg
->cur_offset
+= tosend
;
1400 if (txmsg
->cur_offset
== txmsg
->cur_len
) {
1401 txmsg
->state
= DRM_DP_SIDEBAND_TX_SENT
;
1407 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
)
1409 struct drm_dp_sideband_msg_tx
*txmsg
;
1412 WARN_ON(!mutex_is_locked(&mgr
->qlock
));
1414 /* construct a chunk from the first msg in the tx_msg queue */
1415 if (list_empty(&mgr
->tx_msg_downq
)) {
1416 mgr
->tx_down_in_progress
= false;
1419 mgr
->tx_down_in_progress
= true;
1421 txmsg
= list_first_entry(&mgr
->tx_msg_downq
, struct drm_dp_sideband_msg_tx
, next
);
1422 ret
= process_single_tx_qlock(mgr
, txmsg
, false);
1424 /* txmsg is sent it should be in the slots now */
1425 list_del(&txmsg
->next
);
1427 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1428 list_del(&txmsg
->next
);
1429 if (txmsg
->seqno
!= -1)
1430 txmsg
->dst
->tx_slots
[txmsg
->seqno
] = NULL
;
1431 txmsg
->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
1432 wake_up(&mgr
->tx_waitq
);
1434 if (list_empty(&mgr
->tx_msg_downq
)) {
1435 mgr
->tx_down_in_progress
= false;
1440 /* called holding qlock */
1441 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
)
1443 struct drm_dp_sideband_msg_tx
*txmsg
;
1446 /* construct a chunk from the first msg in the tx_msg queue */
1447 if (list_empty(&mgr
->tx_msg_upq
)) {
1448 mgr
->tx_up_in_progress
= false;
1452 txmsg
= list_first_entry(&mgr
->tx_msg_upq
, struct drm_dp_sideband_msg_tx
, next
);
1453 ret
= process_single_tx_qlock(mgr
, txmsg
, true);
1455 /* up txmsgs aren't put in slots - so free after we send it */
1456 list_del(&txmsg
->next
);
1459 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1460 mgr
->tx_up_in_progress
= true;
1463 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr
*mgr
,
1464 struct drm_dp_sideband_msg_tx
*txmsg
)
1466 mutex_lock(&mgr
->qlock
);
1467 list_add_tail(&txmsg
->next
, &mgr
->tx_msg_downq
);
1468 if (!mgr
->tx_down_in_progress
)
1469 process_single_down_tx_qlock(mgr
);
1470 mutex_unlock(&mgr
->qlock
);
1473 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1474 struct drm_dp_mst_branch
*mstb
)
1477 struct drm_dp_sideband_msg_tx
*txmsg
;
1480 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1485 len
= build_link_address(txmsg
);
1487 mstb
->link_address_sent
= true;
1488 drm_dp_queue_down_tx(mgr
, txmsg
);
1490 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1494 if (txmsg
->reply
.reply_type
== 1)
1495 DRM_DEBUG_KMS("link address nak received\n");
1497 DRM_DEBUG_KMS("link address reply: %d\n", txmsg
->reply
.u
.link_addr
.nports
);
1498 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1499 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
,
1500 txmsg
->reply
.u
.link_addr
.ports
[i
].input_port
,
1501 txmsg
->reply
.u
.link_addr
.ports
[i
].peer_device_type
,
1502 txmsg
->reply
.u
.link_addr
.ports
[i
].port_number
,
1503 txmsg
->reply
.u
.link_addr
.ports
[i
].dpcd_revision
,
1504 txmsg
->reply
.u
.link_addr
.ports
[i
].mcs
,
1505 txmsg
->reply
.u
.link_addr
.ports
[i
].ddps
,
1506 txmsg
->reply
.u
.link_addr
.ports
[i
].legacy_device_plug_status
,
1507 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_streams
,
1508 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_stream_sinks
);
1510 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1511 drm_dp_add_port(mstb
, mgr
->dev
, &txmsg
->reply
.u
.link_addr
.ports
[i
]);
1513 (*mgr
->cbs
->hotplug
)(mgr
);
1516 mstb
->link_address_sent
= false;
1517 DRM_DEBUG_KMS("link address failed %d\n", ret
);
1523 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
1524 struct drm_dp_mst_branch
*mstb
,
1525 struct drm_dp_mst_port
*port
)
1528 struct drm_dp_sideband_msg_tx
*txmsg
;
1531 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1536 len
= build_enum_path_resources(txmsg
, port
->port_num
);
1538 drm_dp_queue_down_tx(mgr
, txmsg
);
1540 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1542 if (txmsg
->reply
.reply_type
== 1)
1543 DRM_DEBUG_KMS("enum path resources nak received\n");
1545 if (port
->port_num
!= txmsg
->reply
.u
.path_resources
.port_number
)
1546 DRM_ERROR("got incorrect port in response\n");
1547 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
,
1548 txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
);
1549 port
->available_pbn
= txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
;
1557 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1558 struct drm_dp_mst_port
*port
,
1562 struct drm_dp_sideband_msg_tx
*txmsg
;
1563 struct drm_dp_mst_branch
*mstb
;
1566 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
1570 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1577 len
= build_allocate_payload(txmsg
, port
->port_num
,
1581 drm_dp_queue_down_tx(mgr
, txmsg
);
1583 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1585 if (txmsg
->reply
.reply_type
== 1) {
1592 drm_dp_put_mst_branch_device(mstb
);
1596 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1598 struct drm_dp_payload
*payload
)
1602 ret
= drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1604 payload
->payload_state
= 0;
1607 payload
->payload_state
= DP_PAYLOAD_LOCAL
;
1611 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1612 struct drm_dp_mst_port
*port
,
1614 struct drm_dp_payload
*payload
)
1617 ret
= drm_dp_payload_send_msg(mgr
, port
, id
, port
->vcpi
.pbn
);
1620 payload
->payload_state
= DP_PAYLOAD_REMOTE
;
1624 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1625 struct drm_dp_mst_port
*port
,
1627 struct drm_dp_payload
*payload
)
1629 DRM_DEBUG_KMS("\n");
1630 /* its okay for these to fail */
1632 drm_dp_payload_send_msg(mgr
, port
, id
, 0);
1635 drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1636 payload
->payload_state
= DP_PAYLOAD_DELETE_LOCAL
;
1640 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1642 struct drm_dp_payload
*payload
)
1644 payload
->payload_state
= 0;
1649 * drm_dp_update_payload_part1() - Execute payload update part 1
1650 * @mgr: manager to use.
1652 * This iterates over all proposed virtual channels, and tries to
1653 * allocate space in the link for them. For 0->slots transitions,
1654 * this step just writes the VCPI to the MST device. For slots->0
1655 * transitions, this writes the updated VCPIs and removes the
1656 * remote VC payloads.
1658 * after calling this the driver should generate ACT and payload
1661 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr
*mgr
)
1665 struct drm_dp_payload req_payload
;
1666 struct drm_dp_mst_port
*port
;
1668 mutex_lock(&mgr
->payload_lock
);
1669 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1670 /* solve the current payloads - compare to the hw ones
1671 - update the hw view */
1672 req_payload
.start_slot
= cur_slots
;
1673 if (mgr
->proposed_vcpis
[i
]) {
1674 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1675 req_payload
.num_slots
= mgr
->proposed_vcpis
[i
]->num_slots
;
1676 req_payload
.vcpi
= mgr
->proposed_vcpis
[i
]->vcpi
;
1679 req_payload
.num_slots
= 0;
1682 if (mgr
->payloads
[i
].start_slot
!= req_payload
.start_slot
) {
1683 mgr
->payloads
[i
].start_slot
= req_payload
.start_slot
;
1685 /* work out what is required to happen with this payload */
1686 if (mgr
->payloads
[i
].num_slots
!= req_payload
.num_slots
) {
1688 /* need to push an update for this payload */
1689 if (req_payload
.num_slots
) {
1690 drm_dp_create_payload_step1(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &req_payload
);
1691 mgr
->payloads
[i
].num_slots
= req_payload
.num_slots
;
1692 mgr
->payloads
[i
].vcpi
= req_payload
.vcpi
;
1693 } else if (mgr
->payloads
[i
].num_slots
) {
1694 mgr
->payloads
[i
].num_slots
= 0;
1695 drm_dp_destroy_payload_step1(mgr
, port
, port
->vcpi
.vcpi
, &mgr
->payloads
[i
]);
1696 req_payload
.payload_state
= mgr
->payloads
[i
].payload_state
;
1697 mgr
->payloads
[i
].start_slot
= 0;
1699 mgr
->payloads
[i
].payload_state
= req_payload
.payload_state
;
1701 cur_slots
+= req_payload
.num_slots
;
1704 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1705 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1706 DRM_DEBUG_KMS("removing payload %d\n", i
);
1707 for (j
= i
; j
< mgr
->max_payloads
- 1; j
++) {
1708 memcpy(&mgr
->payloads
[j
], &mgr
->payloads
[j
+ 1], sizeof(struct drm_dp_payload
));
1709 mgr
->proposed_vcpis
[j
] = mgr
->proposed_vcpis
[j
+ 1];
1710 if (mgr
->proposed_vcpis
[j
] && mgr
->proposed_vcpis
[j
]->num_slots
) {
1711 set_bit(j
+ 1, &mgr
->payload_mask
);
1713 clear_bit(j
+ 1, &mgr
->payload_mask
);
1716 memset(&mgr
->payloads
[mgr
->max_payloads
- 1], 0, sizeof(struct drm_dp_payload
));
1717 mgr
->proposed_vcpis
[mgr
->max_payloads
- 1] = NULL
;
1718 clear_bit(mgr
->max_payloads
, &mgr
->payload_mask
);
1722 mutex_unlock(&mgr
->payload_lock
);
1726 EXPORT_SYMBOL(drm_dp_update_payload_part1
);
1729 * drm_dp_update_payload_part2() - Execute payload update part 2
1730 * @mgr: manager to use.
1732 * This iterates over all proposed virtual channels, and tries to
1733 * allocate space in the link for them. For 0->slots transitions,
1734 * this step writes the remote VC payload commands. For slots->0
1735 * this just resets some internal state.
1737 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr
*mgr
)
1739 struct drm_dp_mst_port
*port
;
1742 mutex_lock(&mgr
->payload_lock
);
1743 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1745 if (!mgr
->proposed_vcpis
[i
])
1748 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1750 DRM_DEBUG_KMS("payload %d %d\n", i
, mgr
->payloads
[i
].payload_state
);
1751 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_LOCAL
) {
1752 ret
= drm_dp_create_payload_step2(mgr
, port
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1753 } else if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1754 ret
= drm_dp_destroy_payload_step2(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1757 mutex_unlock(&mgr
->payload_lock
);
1761 mutex_unlock(&mgr
->payload_lock
);
1764 EXPORT_SYMBOL(drm_dp_update_payload_part2
);
1766 #if 0 /* unused as of yet */
1767 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr
*mgr
,
1768 struct drm_dp_mst_port
*port
,
1769 int offset
, int size
)
1772 struct drm_dp_sideband_msg_tx
*txmsg
;
1774 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1778 len
= build_dpcd_read(txmsg
, port
->port_num
, 0, 8);
1779 txmsg
->dst
= port
->parent
;
1781 drm_dp_queue_down_tx(mgr
, txmsg
);
1787 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
1788 struct drm_dp_mst_port
*port
,
1789 int offset
, int size
, u8
*bytes
)
1793 struct drm_dp_sideband_msg_tx
*txmsg
;
1794 struct drm_dp_mst_branch
*mstb
;
1796 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
1800 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1806 len
= build_dpcd_write(txmsg
, port
->port_num
, offset
, size
, bytes
);
1809 drm_dp_queue_down_tx(mgr
, txmsg
);
1811 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1813 if (txmsg
->reply
.reply_type
== 1) {
1820 drm_dp_put_mst_branch_device(mstb
);
1824 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx
*msg
, u8 req_type
)
1826 struct drm_dp_sideband_msg_reply_body reply
;
1828 reply
.reply_type
= 0;
1829 reply
.req_type
= req_type
;
1830 drm_dp_encode_sideband_reply(&reply
, msg
);
1834 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr
*mgr
,
1835 struct drm_dp_mst_branch
*mstb
,
1836 int req_type
, int seqno
, bool broadcast
)
1838 struct drm_dp_sideband_msg_tx
*txmsg
;
1840 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1845 txmsg
->seqno
= seqno
;
1846 drm_dp_encode_up_ack_reply(txmsg
, req_type
);
1848 mutex_lock(&mgr
->qlock
);
1849 list_add_tail(&txmsg
->next
, &mgr
->tx_msg_upq
);
1850 if (!mgr
->tx_up_in_progress
) {
1851 process_single_up_tx_qlock(mgr
);
1853 mutex_unlock(&mgr
->qlock
);
1857 static bool drm_dp_get_vc_payload_bw(int dp_link_bw
,
1861 switch (dp_link_bw
) {
1863 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
1864 dp_link_bw
, dp_link_count
);
1867 case DP_LINK_BW_1_62
:
1868 *out
= 3 * dp_link_count
;
1870 case DP_LINK_BW_2_7
:
1871 *out
= 5 * dp_link_count
;
1873 case DP_LINK_BW_5_4
:
1874 *out
= 10 * dp_link_count
;
1881 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
1882 * @mgr: manager to set state for
1883 * @mst_state: true to enable MST on this connector - false to disable.
1885 * This is called by the driver when it detects an MST capable device plugged
1886 * into a DP MST capable port, or when a DP MST capable device is unplugged.
1888 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr
*mgr
, bool mst_state
)
1891 struct drm_dp_mst_branch
*mstb
= NULL
;
1893 mutex_lock(&mgr
->lock
);
1894 if (mst_state
== mgr
->mst_state
)
1897 mgr
->mst_state
= mst_state
;
1898 /* set the device into MST mode */
1900 WARN_ON(mgr
->mst_primary
);
1903 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
1904 if (ret
!= DP_RECEIVER_CAP_SIZE
) {
1905 DRM_DEBUG_KMS("failed to read DPCD\n");
1909 if (!drm_dp_get_vc_payload_bw(mgr
->dpcd
[1],
1910 mgr
->dpcd
[2] & DP_MAX_LANE_COUNT_MASK
,
1916 mgr
->total_pbn
= 2560;
1917 mgr
->total_slots
= DIV_ROUND_UP(mgr
->total_pbn
, mgr
->pbn_div
);
1918 mgr
->avail_slots
= mgr
->total_slots
;
1920 /* add initial branch device at LCT 1 */
1921 mstb
= drm_dp_add_mst_branch_device(1, NULL
);
1928 /* give this the main reference */
1929 mgr
->mst_primary
= mstb
;
1930 kref_get(&mgr
->mst_primary
->kref
);
1933 struct drm_dp_payload reset_pay
;
1934 reset_pay
.start_slot
= 0;
1935 reset_pay
.num_slots
= 0x3f;
1936 drm_dp_dpcd_write_payload(mgr
, 0, &reset_pay
);
1939 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
1940 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
1947 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_GUID
, mgr
->guid
, 16);
1949 DRM_DEBUG_KMS("failed to read DP GUID %d\n", ret
);
1953 mgr
->guid_valid
= drm_dp_validate_guid(mgr
, mgr
->guid
);
1954 if (!mgr
->guid_valid
) {
1955 ret
= drm_dp_dpcd_write(mgr
->aux
, DP_GUID
, mgr
->guid
, 16);
1956 mgr
->guid_valid
= true;
1959 queue_work(system_long_wq
, &mgr
->work
);
1963 /* disable MST on the device */
1964 mstb
= mgr
->mst_primary
;
1965 mgr
->mst_primary
= NULL
;
1966 /* this can fail if the device is gone */
1967 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
, 0);
1969 memset(mgr
->payloads
, 0, mgr
->max_payloads
* sizeof(struct drm_dp_payload
));
1970 mgr
->payload_mask
= 0;
1971 set_bit(0, &mgr
->payload_mask
);
1976 mutex_unlock(&mgr
->lock
);
1978 drm_dp_put_mst_branch_device(mstb
);
1982 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst
);
1985 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
1986 * @mgr: manager to suspend
1988 * This function tells the MST device that we can't handle UP messages
1989 * anymore. This should stop it from sending any since we are suspended.
1991 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr
*mgr
)
1993 mutex_lock(&mgr
->lock
);
1994 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
1995 DP_MST_EN
| DP_UPSTREAM_IS_SRC
);
1996 mutex_unlock(&mgr
->lock
);
1997 flush_work(&mgr
->work
);
1998 flush_work(&mgr
->destroy_connector_work
);
2000 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend
);
2003 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2004 * @mgr: manager to resume
2006 * This will fetch DPCD and see if the device is still there,
2007 * if it is, it will rewrite the MSTM control bits, and return.
2009 * if the device fails this returns -1, and the driver should do
2010 * a full MST reprobe, in case we were undocked.
2012 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr
*mgr
)
2016 mutex_lock(&mgr
->lock
);
2018 if (mgr
->mst_primary
) {
2020 sret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
2021 if (sret
!= DP_RECEIVER_CAP_SIZE
) {
2022 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2027 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2028 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
2030 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2039 mutex_unlock(&mgr
->lock
);
2042 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume
);
2044 static void drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr
*mgr
, bool up
)
2048 int replylen
, origlen
, curreply
;
2050 struct drm_dp_sideband_msg_rx
*msg
;
2051 int basereg
= up
? DP_SIDEBAND_MSG_UP_REQ_BASE
: DP_SIDEBAND_MSG_DOWN_REP_BASE
;
2052 msg
= up
? &mgr
->up_req_recv
: &mgr
->down_rep_recv
;
2054 len
= min(mgr
->max_dpcd_transaction_bytes
, 16);
2055 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
,
2058 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len
, ret
);
2061 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, true);
2063 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock
[0]);
2066 replylen
= msg
->curchunk_len
+ msg
->curchunk_hdrlen
;
2071 while (replylen
> 0) {
2072 len
= min3(replylen
, mgr
->max_dpcd_transaction_bytes
, 16);
2073 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
+ curreply
,
2076 DRM_DEBUG_KMS("failed to read a chunk\n");
2078 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, false);
2080 DRM_DEBUG_KMS("failed to build sideband msg\n");
2086 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr
*mgr
)
2090 drm_dp_get_one_sb_msg(mgr
, false);
2092 if (mgr
->down_rep_recv
.have_eomt
) {
2093 struct drm_dp_sideband_msg_tx
*txmsg
;
2094 struct drm_dp_mst_branch
*mstb
;
2096 mstb
= drm_dp_get_mst_branch_device(mgr
,
2097 mgr
->down_rep_recv
.initial_hdr
.lct
,
2098 mgr
->down_rep_recv
.initial_hdr
.rad
);
2101 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->down_rep_recv
.initial_hdr
.lct
);
2102 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2106 /* find the message */
2107 slot
= mgr
->down_rep_recv
.initial_hdr
.seqno
;
2108 mutex_lock(&mgr
->qlock
);
2109 txmsg
= mstb
->tx_slots
[slot
];
2110 /* remove from slots */
2111 mutex_unlock(&mgr
->qlock
);
2114 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2116 mgr
->down_rep_recv
.initial_hdr
.seqno
,
2117 mgr
->down_rep_recv
.initial_hdr
.lct
,
2118 mgr
->down_rep_recv
.initial_hdr
.rad
[0],
2119 mgr
->down_rep_recv
.msg
[0]);
2120 drm_dp_put_mst_branch_device(mstb
);
2121 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2125 drm_dp_sideband_parse_reply(&mgr
->down_rep_recv
, &txmsg
->reply
);
2126 if (txmsg
->reply
.reply_type
== 1) {
2127 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
);
2130 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2131 drm_dp_put_mst_branch_device(mstb
);
2133 mutex_lock(&mgr
->qlock
);
2134 txmsg
->state
= DRM_DP_SIDEBAND_TX_RX
;
2135 mstb
->tx_slots
[slot
] = NULL
;
2136 mutex_unlock(&mgr
->qlock
);
2138 wake_up(&mgr
->tx_waitq
);
2143 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr
*mgr
)
2146 drm_dp_get_one_sb_msg(mgr
, true);
2148 if (mgr
->up_req_recv
.have_eomt
) {
2149 struct drm_dp_sideband_msg_req_body msg
;
2150 struct drm_dp_mst_branch
*mstb
;
2152 mstb
= drm_dp_get_mst_branch_device(mgr
,
2153 mgr
->up_req_recv
.initial_hdr
.lct
,
2154 mgr
->up_req_recv
.initial_hdr
.rad
);
2156 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2157 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2161 seqno
= mgr
->up_req_recv
.initial_hdr
.seqno
;
2162 drm_dp_sideband_parse_req(&mgr
->up_req_recv
, &msg
);
2164 if (msg
.req_type
== DP_CONNECTION_STATUS_NOTIFY
) {
2165 drm_dp_send_up_ack_reply(mgr
, mstb
, msg
.req_type
, seqno
, false);
2166 drm_dp_update_port(mstb
, &msg
.u
.conn_stat
);
2167 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
);
2168 (*mgr
->cbs
->hotplug
)(mgr
);
2170 } else if (msg
.req_type
== DP_RESOURCE_STATUS_NOTIFY
) {
2171 drm_dp_send_up_ack_reply(mgr
, mstb
, msg
.req_type
, seqno
, false);
2172 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg
.u
.resource_stat
.port_number
, msg
.u
.resource_stat
.available_pbn
);
2175 drm_dp_put_mst_branch_device(mstb
);
2176 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2182 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2183 * @mgr: manager to notify irq for.
2184 * @esi: 4 bytes from SINK_COUNT_ESI
2185 * @handled: whether the hpd interrupt was consumed or not
2187 * This should be called from the driver when it detects a short IRQ,
2188 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2189 * topology manager will process the sideband messages received as a result
2192 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr
*mgr
, u8
*esi
, bool *handled
)
2199 if (sc
!= mgr
->sink_count
) {
2200 mgr
->sink_count
= sc
;
2204 if (esi
[1] & DP_DOWN_REP_MSG_RDY
) {
2205 ret
= drm_dp_mst_handle_down_rep(mgr
);
2209 if (esi
[1] & DP_UP_REQ_MSG_RDY
) {
2210 ret
|= drm_dp_mst_handle_up_req(mgr
);
2214 drm_dp_mst_kick_tx(mgr
);
2217 EXPORT_SYMBOL(drm_dp_mst_hpd_irq
);
2220 * drm_dp_mst_detect_port() - get connection status for an MST port
2221 * @mgr: manager for this port
2222 * @port: unverified pointer to a port
2224 * This returns the current connection state for a port. It validates the
2225 * port pointer still exists so the caller doesn't require a reference
2227 enum drm_connector_status
drm_dp_mst_detect_port(struct drm_connector
*connector
,
2228 struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2230 enum drm_connector_status status
= connector_status_disconnected
;
2232 /* we need to search for the port in the mgr in case its gone */
2233 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2235 return connector_status_disconnected
;
2240 switch (port
->pdt
) {
2241 case DP_PEER_DEVICE_NONE
:
2242 case DP_PEER_DEVICE_MST_BRANCHING
:
2245 case DP_PEER_DEVICE_SST_SINK
:
2246 status
= connector_status_connected
;
2247 /* for logical ports - cache the EDID */
2248 if (port
->port_num
>= 8 && !port
->cached_edid
) {
2249 port
->cached_edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2252 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
2254 status
= connector_status_connected
;
2258 drm_dp_put_port(port
);
2261 EXPORT_SYMBOL(drm_dp_mst_detect_port
);
2264 * drm_dp_mst_get_edid() - get EDID for an MST port
2265 * @connector: toplevel connector to get EDID for
2266 * @mgr: manager for this port
2267 * @port: unverified pointer to a port.
2269 * This returns an EDID for the port connected to a connector,
2270 * It validates the pointer still exists so the caller doesn't require a
2273 struct edid
*drm_dp_mst_get_edid(struct drm_connector
*connector
, struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2275 struct edid
*edid
= NULL
;
2277 /* we need to search for the port in the mgr in case its gone */
2278 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2282 if (port
->cached_edid
)
2283 edid
= drm_edid_duplicate(port
->cached_edid
);
2285 edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2286 drm_mode_connector_set_tile_property(connector
);
2288 drm_dp_put_port(port
);
2291 EXPORT_SYMBOL(drm_dp_mst_get_edid
);
2294 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2295 * @mgr: manager to use
2296 * @pbn: payload bandwidth to convert into slots.
2298 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
,
2303 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2305 if (num_slots
> mgr
->avail_slots
)
2309 EXPORT_SYMBOL(drm_dp_find_vcpi_slots
);
2311 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
2312 struct drm_dp_vcpi
*vcpi
, int pbn
)
2317 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2319 if (num_slots
> mgr
->avail_slots
)
2323 vcpi
->aligned_pbn
= num_slots
* mgr
->pbn_div
;
2324 vcpi
->num_slots
= num_slots
;
2326 ret
= drm_dp_mst_assign_payload_id(mgr
, vcpi
);
2333 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2334 * @mgr: manager for this port
2335 * @port: port to allocate a virtual channel for.
2336 * @pbn: payload bandwidth number to request
2337 * @slots: returned number of slots for this PBN.
2339 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
, int pbn
, int *slots
)
2343 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2347 if (port
->vcpi
.vcpi
> 0) {
2348 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port
->vcpi
.vcpi
, port
->vcpi
.pbn
, pbn
);
2349 if (pbn
== port
->vcpi
.pbn
) {
2350 *slots
= port
->vcpi
.num_slots
;
2355 ret
= drm_dp_init_vcpi(mgr
, &port
->vcpi
, pbn
);
2357 DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn
, mgr
->pbn_div
), mgr
->avail_slots
, ret
);
2360 DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn
, port
->vcpi
.num_slots
);
2361 *slots
= port
->vcpi
.num_slots
;
2363 drm_dp_put_port(port
);
2368 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi
);
2370 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2373 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2377 slots
= port
->vcpi
.num_slots
;
2378 drm_dp_put_port(port
);
2381 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots
);
2384 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2385 * @mgr: manager for this port
2386 * @port: unverified pointer to a port.
2388 * This just resets the number of slots for the ports VCPI for later programming.
2390 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2392 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2395 port
->vcpi
.num_slots
= 0;
2396 drm_dp_put_port(port
);
2398 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots
);
2401 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2402 * @mgr: manager for this port
2403 * @port: unverified port to deallocate vcpi for
2405 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2407 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2411 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
2412 port
->vcpi
.num_slots
= 0;
2414 port
->vcpi
.aligned_pbn
= 0;
2415 port
->vcpi
.vcpi
= 0;
2416 drm_dp_put_port(port
);
2418 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi
);
2420 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
2421 int id
, struct drm_dp_payload
*payload
)
2423 u8 payload_alloc
[3], status
;
2427 drm_dp_dpcd_writeb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
,
2428 DP_PAYLOAD_TABLE_UPDATED
);
2430 payload_alloc
[0] = id
;
2431 payload_alloc
[1] = payload
->start_slot
;
2432 payload_alloc
[2] = payload
->num_slots
;
2434 ret
= drm_dp_dpcd_write(mgr
->aux
, DP_PAYLOAD_ALLOCATE_SET
, payload_alloc
, 3);
2436 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret
);
2441 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2443 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
2447 if (!(status
& DP_PAYLOAD_TABLE_UPDATED
)) {
2450 usleep_range(10000, 20000);
2453 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status
);
2464 * drm_dp_check_act_status() - Check ACT handled status.
2465 * @mgr: manager to use
2467 * Check the payload status bits in the DPCD for ACT handled completion.
2469 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr
*mgr
)
2476 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2479 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
2483 if (status
& DP_PAYLOAD_ACT_HANDLED
)
2488 } while (count
< 30);
2490 if (!(status
& DP_PAYLOAD_ACT_HANDLED
)) {
2491 DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status
, count
);
2499 EXPORT_SYMBOL(drm_dp_check_act_status
);
2502 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2503 * @clock: dot clock for the mode
2504 * @bpp: bpp for the mode.
2506 * This uses the formula in the spec to calculate the PBN value for a mode.
2508 int drm_dp_calc_pbn_mode(int clock
, int bpp
)
2513 fixed20_12 margin
, tmp
;
2516 pix_bw
.full
= dfixed_const(clock
);
2517 fbpp
.full
= dfixed_const(bpp
);
2518 tmp
.full
= dfixed_const(8);
2519 fbpp
.full
= dfixed_div(fbpp
, tmp
);
2521 result
.full
= dfixed_mul(pix_bw
, fbpp
);
2522 margin
.full
= dfixed_const(54);
2523 tmp
.full
= dfixed_const(64);
2524 margin
.full
= dfixed_div(margin
, tmp
);
2525 result
.full
= dfixed_div(result
, margin
);
2527 margin
.full
= dfixed_const(1006);
2528 tmp
.full
= dfixed_const(1000);
2529 margin
.full
= dfixed_div(margin
, tmp
);
2530 result
.full
= dfixed_mul(result
, margin
);
2532 result
.full
= dfixed_div(result
, tmp
);
2533 result
.full
= dfixed_ceil(result
);
2534 res
= dfixed_trunc(result
);
2537 EXPORT_SYMBOL(drm_dp_calc_pbn_mode
);
2539 static int test_calc_pbn_mode(void)
2542 ret
= drm_dp_calc_pbn_mode(154000, 30);
2545 ret
= drm_dp_calc_pbn_mode(234000, 30);
2551 /* we want to kick the TX after we've ack the up/down IRQs. */
2552 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
)
2554 queue_work(system_long_wq
, &mgr
->tx_work
);
2557 static void drm_dp_mst_dump_mstb(struct seq_file
*m
,
2558 struct drm_dp_mst_branch
*mstb
)
2560 struct drm_dp_mst_port
*port
;
2561 int tabs
= mstb
->lct
;
2565 for (i
= 0; i
< tabs
; i
++)
2569 seq_printf(m
, "%smst: %p, %d\n", prefix
, mstb
, mstb
->num_ports
);
2570 list_for_each_entry(port
, &mstb
->ports
, next
) {
2571 seq_printf(m
, "%sport: %d: ddps: %d ldps: %d, %p, conn: %p\n", prefix
, port
->port_num
, port
->ddps
, port
->ldps
, port
, port
->connector
);
2573 drm_dp_mst_dump_mstb(m
, port
->mstb
);
2577 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
2582 for (i
= 0; i
< 4; i
++) {
2583 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
+ (i
* 16), &buf
[i
* 16], 16);
2593 * drm_dp_mst_dump_topology(): dump topology to seq file.
2594 * @m: seq_file to dump output to
2595 * @mgr: manager to dump current topology for.
2597 * helper to dump MST topology to a seq file for debugfs.
2599 void drm_dp_mst_dump_topology(struct seq_file
*m
,
2600 struct drm_dp_mst_topology_mgr
*mgr
)
2603 struct drm_dp_mst_port
*port
;
2604 mutex_lock(&mgr
->lock
);
2605 if (mgr
->mst_primary
)
2606 drm_dp_mst_dump_mstb(m
, mgr
->mst_primary
);
2609 mutex_unlock(&mgr
->lock
);
2611 mutex_lock(&mgr
->payload_lock
);
2612 seq_printf(m
, "vcpi: %lx %lx\n", mgr
->payload_mask
, mgr
->vcpi_mask
);
2614 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
2615 if (mgr
->proposed_vcpis
[i
]) {
2616 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
2617 seq_printf(m
, "vcpi %d: %d %d %d\n", i
, port
->port_num
, port
->vcpi
.vcpi
, port
->vcpi
.num_slots
);
2619 seq_printf(m
, "vcpi %d:unsed\n", i
);
2621 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
2622 seq_printf(m
, "payload %d: %d, %d, %d\n",
2624 mgr
->payloads
[i
].payload_state
,
2625 mgr
->payloads
[i
].start_slot
,
2626 mgr
->payloads
[i
].num_slots
);
2630 mutex_unlock(&mgr
->payload_lock
);
2632 mutex_lock(&mgr
->lock
);
2633 if (mgr
->mst_primary
) {
2637 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, buf
, DP_RECEIVER_CAP_SIZE
);
2638 seq_printf(m
, "dpcd: ");
2639 for (i
= 0; i
< DP_RECEIVER_CAP_SIZE
; i
++)
2640 seq_printf(m
, "%02x ", buf
[i
]);
2641 seq_printf(m
, "\n");
2642 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_FAUX_CAP
, buf
, 2);
2643 seq_printf(m
, "faux/mst: ");
2644 for (i
= 0; i
< 2; i
++)
2645 seq_printf(m
, "%02x ", buf
[i
]);
2646 seq_printf(m
, "\n");
2647 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_MSTM_CTRL
, buf
, 1);
2648 seq_printf(m
, "mst ctrl: ");
2649 for (i
= 0; i
< 1; i
++)
2650 seq_printf(m
, "%02x ", buf
[i
]);
2651 seq_printf(m
, "\n");
2653 /* dump the standard OUI branch header */
2654 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_BRANCH_OUI
, buf
, DP_BRANCH_OUI_HEADER_SIZE
);
2655 seq_printf(m
, "branch oui: ");
2656 for (i
= 0; i
< 0x3; i
++)
2657 seq_printf(m
, "%02x", buf
[i
]);
2658 seq_printf(m
, " devid: ");
2659 for (i
= 0x3; i
< 0x8; i
++)
2660 seq_printf(m
, "%c", buf
[i
]);
2661 seq_printf(m
, " revision: hw: %x.%x sw: %x.%x", buf
[0x9] >> 4, buf
[0x9] & 0xf, buf
[0xa], buf
[0xb]);
2662 seq_printf(m
, "\n");
2663 bret
= dump_dp_payload_table(mgr
, buf
);
2665 seq_printf(m
, "payload table: ");
2666 for (i
= 0; i
< 63; i
++)
2667 seq_printf(m
, "%02x ", buf
[i
]);
2668 seq_printf(m
, "\n");
2673 mutex_unlock(&mgr
->lock
);
2676 EXPORT_SYMBOL(drm_dp_mst_dump_topology
);
2678 static void drm_dp_tx_work(struct work_struct
*work
)
2680 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, tx_work
);
2682 mutex_lock(&mgr
->qlock
);
2683 if (mgr
->tx_down_in_progress
)
2684 process_single_down_tx_qlock(mgr
);
2685 mutex_unlock(&mgr
->qlock
);
2688 static void drm_dp_destroy_connector_work(struct work_struct
*work
)
2690 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, destroy_connector_work
);
2691 struct drm_dp_mst_port
*port
;
2692 bool send_hotplug
= false;
2694 * Not a regular list traverse as we have to drop the destroy
2695 * connector lock before destroying the connector, to avoid AB->BA
2696 * ordering between this lock and the config mutex.
2699 mutex_lock(&mgr
->destroy_connector_lock
);
2700 port
= list_first_entry_or_null(&mgr
->destroy_connector_list
, struct drm_dp_mst_port
, next
);
2702 mutex_unlock(&mgr
->destroy_connector_lock
);
2705 list_del(&port
->next
);
2706 mutex_unlock(&mgr
->destroy_connector_lock
);
2708 mgr
->cbs
->destroy_connector(mgr
, port
->connector
);
2710 drm_dp_port_teardown_pdt(port
, port
->pdt
);
2712 if (!port
->input
&& port
->vcpi
.vcpi
> 0)
2713 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
2715 send_hotplug
= true;
2718 (*mgr
->cbs
->hotplug
)(mgr
);
2722 * drm_dp_mst_topology_mgr_init - initialise a topology manager
2723 * @mgr: manager struct to initialise
2724 * @dev: device providing this structure - for i2c addition.
2725 * @aux: DP helper aux channel to talk to this device
2726 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2727 * @max_payloads: maximum number of payloads this GPU can source
2728 * @conn_base_id: the connector object ID the MST device is connected to.
2730 * Return 0 for success, or negative error code on failure
2732 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr
*mgr
,
2733 struct device
*dev
, struct drm_dp_aux
*aux
,
2734 int max_dpcd_transaction_bytes
,
2735 int max_payloads
, int conn_base_id
)
2737 mutex_init(&mgr
->lock
);
2738 mutex_init(&mgr
->qlock
);
2739 mutex_init(&mgr
->payload_lock
);
2740 mutex_init(&mgr
->destroy_connector_lock
);
2741 INIT_LIST_HEAD(&mgr
->tx_msg_upq
);
2742 INIT_LIST_HEAD(&mgr
->tx_msg_downq
);
2743 INIT_LIST_HEAD(&mgr
->destroy_connector_list
);
2744 INIT_WORK(&mgr
->work
, drm_dp_mst_link_probe_work
);
2745 INIT_WORK(&mgr
->tx_work
, drm_dp_tx_work
);
2746 INIT_WORK(&mgr
->destroy_connector_work
, drm_dp_destroy_connector_work
);
2747 init_waitqueue_head(&mgr
->tx_waitq
);
2750 mgr
->max_dpcd_transaction_bytes
= max_dpcd_transaction_bytes
;
2751 mgr
->max_payloads
= max_payloads
;
2752 mgr
->conn_base_id
= conn_base_id
;
2753 mgr
->payloads
= kcalloc(max_payloads
, sizeof(struct drm_dp_payload
), GFP_KERNEL
);
2756 mgr
->proposed_vcpis
= kcalloc(max_payloads
, sizeof(struct drm_dp_vcpi
*), GFP_KERNEL
);
2757 if (!mgr
->proposed_vcpis
)
2759 set_bit(0, &mgr
->payload_mask
);
2760 test_calc_pbn_mode();
2763 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init
);
2766 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
2767 * @mgr: manager to destroy
2769 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr
*mgr
)
2771 flush_work(&mgr
->work
);
2772 flush_work(&mgr
->destroy_connector_work
);
2773 mutex_lock(&mgr
->payload_lock
);
2774 kfree(mgr
->payloads
);
2775 mgr
->payloads
= NULL
;
2776 kfree(mgr
->proposed_vcpis
);
2777 mgr
->proposed_vcpis
= NULL
;
2778 mutex_unlock(&mgr
->payload_lock
);
2782 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy
);
2785 static int drm_dp_mst_i2c_xfer(struct i2c_adapter
*adapter
, struct i2c_msg
*msgs
,
2788 struct drm_dp_aux
*aux
= adapter
->algo_data
;
2789 struct drm_dp_mst_port
*port
= container_of(aux
, struct drm_dp_mst_port
, aux
);
2790 struct drm_dp_mst_branch
*mstb
;
2791 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
2793 bool reading
= false;
2794 struct drm_dp_sideband_msg_req_body msg
;
2795 struct drm_dp_sideband_msg_tx
*txmsg
= NULL
;
2798 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
2802 /* construct i2c msg */
2803 /* see if last msg is a read */
2804 if (msgs
[num
- 1].flags
& I2C_M_RD
)
2807 if (!reading
|| (num
- 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS
)) {
2808 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
2813 memset(&msg
, 0, sizeof(msg
));
2814 msg
.req_type
= DP_REMOTE_I2C_READ
;
2815 msg
.u
.i2c_read
.num_transactions
= num
- 1;
2816 msg
.u
.i2c_read
.port_number
= port
->port_num
;
2817 for (i
= 0; i
< num
- 1; i
++) {
2818 msg
.u
.i2c_read
.transactions
[i
].i2c_dev_id
= msgs
[i
].addr
;
2819 msg
.u
.i2c_read
.transactions
[i
].num_bytes
= msgs
[i
].len
;
2820 msg
.u
.i2c_read
.transactions
[i
].bytes
= msgs
[i
].buf
;
2822 msg
.u
.i2c_read
.read_i2c_device_id
= msgs
[num
- 1].addr
;
2823 msg
.u
.i2c_read
.num_bytes_read
= msgs
[num
- 1].len
;
2825 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
2832 drm_dp_encode_sideband_req(&msg
, txmsg
);
2834 drm_dp_queue_down_tx(mgr
, txmsg
);
2836 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
2839 if (txmsg
->reply
.reply_type
== 1) { /* got a NAK back */
2843 if (txmsg
->reply
.u
.remote_i2c_read_ack
.num_bytes
!= msgs
[num
- 1].len
) {
2847 memcpy(msgs
[num
- 1].buf
, txmsg
->reply
.u
.remote_i2c_read_ack
.bytes
, msgs
[num
- 1].len
);
2852 drm_dp_put_mst_branch_device(mstb
);
2856 static u32
drm_dp_mst_i2c_functionality(struct i2c_adapter
*adapter
)
2858 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
2859 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
2860 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
2861 I2C_FUNC_10BIT_ADDR
;
2864 static const struct i2c_algorithm drm_dp_mst_i2c_algo
= {
2865 .functionality
= drm_dp_mst_i2c_functionality
,
2866 .master_xfer
= drm_dp_mst_i2c_xfer
,
2870 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
2871 * @aux: DisplayPort AUX channel
2873 * Returns 0 on success or a negative error code on failure.
2875 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux
*aux
)
2877 aux
->ddc
.algo
= &drm_dp_mst_i2c_algo
;
2878 aux
->ddc
.algo_data
= aux
;
2879 aux
->ddc
.retries
= 3;
2881 aux
->ddc
.class = I2C_CLASS_DDC
;
2882 aux
->ddc
.owner
= THIS_MODULE
;
2883 aux
->ddc
.dev
.parent
= aux
->dev
;
2884 aux
->ddc
.dev
.of_node
= aux
->dev
->of_node
;
2886 strlcpy(aux
->ddc
.name
, aux
->name
? aux
->name
: dev_name(aux
->dev
),
2887 sizeof(aux
->ddc
.name
));
2889 return i2c_add_adapter(&aux
->ddc
);
2893 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
2894 * @aux: DisplayPort AUX channel
2896 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux
*aux
)
2898 i2c_del_adapter(&aux
->ddc
);