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>
32 #include <linux/iopoll.h>
34 #include <drm/drm_fixed.h>
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_atomic_helper.h>
41 * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
42 * protocol. The helpers contain a topology manager and bandwidth manager.
43 * The helpers encapsulate the sending and received of sideband msgs.
45 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
47 static int test_calc_pbn_mode(void);
49 static void drm_dp_put_port(struct drm_dp_mst_port
*port
);
51 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
53 struct drm_dp_payload
*payload
);
55 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
56 struct drm_dp_mst_port
*port
,
57 int offset
, int size
, u8
*bytes
);
59 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
60 struct drm_dp_mst_branch
*mstb
);
61 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
62 struct drm_dp_mst_branch
*mstb
,
63 struct drm_dp_mst_port
*port
);
64 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
67 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux
*aux
);
68 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux
*aux
);
69 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
);
70 /* sideband msg handling */
71 static u8
drm_dp_msg_header_crc4(const uint8_t *data
, size_t num_nibbles
)
76 int number_of_bits
= num_nibbles
* 4;
79 while (number_of_bits
!= 0) {
82 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
90 if ((remainder
& 0x10) == 0x10)
95 while (number_of_bits
!= 0) {
98 if ((remainder
& 0x10) != 0)
105 static u8
drm_dp_msg_data_crc4(const uint8_t *data
, u8 number_of_bytes
)
110 int number_of_bits
= number_of_bytes
* 8;
113 while (number_of_bits
!= 0) {
116 remainder
|= (data
[array_index
] & bitmask
) >> bitshift
;
124 if ((remainder
& 0x100) == 0x100)
129 while (number_of_bits
!= 0) {
132 if ((remainder
& 0x100) != 0)
136 return remainder
& 0xff;
138 static inline u8
drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr
*hdr
)
141 size
+= (hdr
->lct
/ 2);
145 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
151 buf
[idx
++] = ((hdr
->lct
& 0xf) << 4) | (hdr
->lcr
& 0xf);
152 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
153 buf
[idx
++] = hdr
->rad
[i
];
154 buf
[idx
++] = (hdr
->broadcast
<< 7) | (hdr
->path_msg
<< 6) |
155 (hdr
->msg_len
& 0x3f);
156 buf
[idx
++] = (hdr
->somt
<< 7) | (hdr
->eomt
<< 6) | (hdr
->seqno
<< 4);
158 crc4
= drm_dp_msg_header_crc4(buf
, (idx
* 2) - 1);
159 buf
[idx
- 1] |= (crc4
& 0xf);
164 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr
*hdr
,
165 u8
*buf
, int buflen
, u8
*hdrlen
)
174 len
+= ((buf
[0] & 0xf0) >> 4) / 2;
177 crc4
= drm_dp_msg_header_crc4(buf
, (len
* 2) - 1);
179 if ((crc4
& 0xf) != (buf
[len
- 1] & 0xf)) {
180 DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4
, buf
[len
- 1]);
184 hdr
->lct
= (buf
[0] & 0xf0) >> 4;
185 hdr
->lcr
= (buf
[0] & 0xf);
187 for (i
= 0; i
< (hdr
->lct
/ 2); i
++)
188 hdr
->rad
[i
] = buf
[idx
++];
189 hdr
->broadcast
= (buf
[idx
] >> 7) & 0x1;
190 hdr
->path_msg
= (buf
[idx
] >> 6) & 0x1;
191 hdr
->msg_len
= buf
[idx
] & 0x3f;
193 hdr
->somt
= (buf
[idx
] >> 7) & 0x1;
194 hdr
->eomt
= (buf
[idx
] >> 6) & 0x1;
195 hdr
->seqno
= (buf
[idx
] >> 4) & 0x1;
201 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body
*req
,
202 struct drm_dp_sideband_msg_tx
*raw
)
207 buf
[idx
++] = req
->req_type
& 0x7f;
209 switch (req
->req_type
) {
210 case DP_ENUM_PATH_RESOURCES
:
211 buf
[idx
] = (req
->u
.port_num
.port_number
& 0xf) << 4;
214 case DP_ALLOCATE_PAYLOAD
:
215 buf
[idx
] = (req
->u
.allocate_payload
.port_number
& 0xf) << 4 |
216 (req
->u
.allocate_payload
.number_sdp_streams
& 0xf);
218 buf
[idx
] = (req
->u
.allocate_payload
.vcpi
& 0x7f);
220 buf
[idx
] = (req
->u
.allocate_payload
.pbn
>> 8);
222 buf
[idx
] = (req
->u
.allocate_payload
.pbn
& 0xff);
224 for (i
= 0; i
< req
->u
.allocate_payload
.number_sdp_streams
/ 2; i
++) {
225 buf
[idx
] = ((req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2] & 0xf) << 4) |
226 (req
->u
.allocate_payload
.sdp_stream_sink
[i
* 2 + 1] & 0xf);
229 if (req
->u
.allocate_payload
.number_sdp_streams
& 1) {
230 i
= req
->u
.allocate_payload
.number_sdp_streams
- 1;
231 buf
[idx
] = (req
->u
.allocate_payload
.sdp_stream_sink
[i
] & 0xf) << 4;
235 case DP_QUERY_PAYLOAD
:
236 buf
[idx
] = (req
->u
.query_payload
.port_number
& 0xf) << 4;
238 buf
[idx
] = (req
->u
.query_payload
.vcpi
& 0x7f);
241 case DP_REMOTE_DPCD_READ
:
242 buf
[idx
] = (req
->u
.dpcd_read
.port_number
& 0xf) << 4;
243 buf
[idx
] |= ((req
->u
.dpcd_read
.dpcd_address
& 0xf0000) >> 16) & 0xf;
245 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff00) >> 8;
247 buf
[idx
] = (req
->u
.dpcd_read
.dpcd_address
& 0xff);
249 buf
[idx
] = (req
->u
.dpcd_read
.num_bytes
);
253 case DP_REMOTE_DPCD_WRITE
:
254 buf
[idx
] = (req
->u
.dpcd_write
.port_number
& 0xf) << 4;
255 buf
[idx
] |= ((req
->u
.dpcd_write
.dpcd_address
& 0xf0000) >> 16) & 0xf;
257 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff00) >> 8;
259 buf
[idx
] = (req
->u
.dpcd_write
.dpcd_address
& 0xff);
261 buf
[idx
] = (req
->u
.dpcd_write
.num_bytes
);
263 memcpy(&buf
[idx
], req
->u
.dpcd_write
.bytes
, req
->u
.dpcd_write
.num_bytes
);
264 idx
+= req
->u
.dpcd_write
.num_bytes
;
266 case DP_REMOTE_I2C_READ
:
267 buf
[idx
] = (req
->u
.i2c_read
.port_number
& 0xf) << 4;
268 buf
[idx
] |= (req
->u
.i2c_read
.num_transactions
& 0x3);
270 for (i
= 0; i
< (req
->u
.i2c_read
.num_transactions
& 0x3); i
++) {
271 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].i2c_dev_id
& 0x7f;
273 buf
[idx
] = req
->u
.i2c_read
.transactions
[i
].num_bytes
;
275 memcpy(&buf
[idx
], req
->u
.i2c_read
.transactions
[i
].bytes
, req
->u
.i2c_read
.transactions
[i
].num_bytes
);
276 idx
+= req
->u
.i2c_read
.transactions
[i
].num_bytes
;
278 buf
[idx
] = (req
->u
.i2c_read
.transactions
[i
].no_stop_bit
& 0x1) << 4;
279 buf
[idx
] |= (req
->u
.i2c_read
.transactions
[i
].i2c_transaction_delay
& 0xf);
282 buf
[idx
] = (req
->u
.i2c_read
.read_i2c_device_id
) & 0x7f;
284 buf
[idx
] = (req
->u
.i2c_read
.num_bytes_read
);
288 case DP_REMOTE_I2C_WRITE
:
289 buf
[idx
] = (req
->u
.i2c_write
.port_number
& 0xf) << 4;
291 buf
[idx
] = (req
->u
.i2c_write
.write_i2c_device_id
) & 0x7f;
293 buf
[idx
] = (req
->u
.i2c_write
.num_bytes
);
295 memcpy(&buf
[idx
], req
->u
.i2c_write
.bytes
, req
->u
.i2c_write
.num_bytes
);
296 idx
+= req
->u
.i2c_write
.num_bytes
;
299 case DP_POWER_DOWN_PHY
:
300 case DP_POWER_UP_PHY
:
301 buf
[idx
] = (req
->u
.port_num
.port_number
& 0xf) << 4;
308 static void drm_dp_crc_sideband_chunk_req(u8
*msg
, u8 len
)
311 crc4
= drm_dp_msg_data_crc4(msg
, len
);
315 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body
*rep
,
316 struct drm_dp_sideband_msg_tx
*raw
)
321 buf
[idx
++] = (rep
->reply_type
& 0x1) << 7 | (rep
->req_type
& 0x7f);
326 /* this adds a chunk of msg to the builder to get the final msg */
327 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx
*msg
,
328 u8
*replybuf
, u8 replybuflen
, bool hdr
)
335 struct drm_dp_sideband_msg_hdr recv_hdr
;
336 ret
= drm_dp_decode_sideband_msg_hdr(&recv_hdr
, replybuf
, replybuflen
, &hdrlen
);
338 print_hex_dump(KERN_DEBUG
, "failed hdr", DUMP_PREFIX_NONE
, 16, 1, replybuf
, replybuflen
, false);
343 * ignore out-of-order messages or messages that are part of a
346 if (!recv_hdr
.somt
&& !msg
->have_somt
)
349 /* get length contained in this portion */
350 msg
->curchunk_len
= recv_hdr
.msg_len
;
351 msg
->curchunk_hdrlen
= hdrlen
;
353 /* we have already gotten an somt - don't bother parsing */
354 if (recv_hdr
.somt
&& msg
->have_somt
)
358 memcpy(&msg
->initial_hdr
, &recv_hdr
, sizeof(struct drm_dp_sideband_msg_hdr
));
359 msg
->have_somt
= true;
362 msg
->have_eomt
= true;
364 /* copy the bytes for the remainder of this header chunk */
365 msg
->curchunk_idx
= min(msg
->curchunk_len
, (u8
)(replybuflen
- hdrlen
));
366 memcpy(&msg
->chunk
[0], replybuf
+ hdrlen
, msg
->curchunk_idx
);
368 memcpy(&msg
->chunk
[msg
->curchunk_idx
], replybuf
, replybuflen
);
369 msg
->curchunk_idx
+= replybuflen
;
372 if (msg
->curchunk_idx
>= msg
->curchunk_len
) {
374 crc4
= drm_dp_msg_data_crc4(msg
->chunk
, msg
->curchunk_len
- 1);
375 /* copy chunk into bigger msg */
376 memcpy(&msg
->msg
[msg
->curlen
], msg
->chunk
, msg
->curchunk_len
- 1);
377 msg
->curlen
+= msg
->curchunk_len
- 1;
382 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx
*raw
,
383 struct drm_dp_sideband_msg_reply_body
*repmsg
)
387 memcpy(repmsg
->u
.link_addr
.guid
, &raw
->msg
[idx
], 16);
389 repmsg
->u
.link_addr
.nports
= raw
->msg
[idx
] & 0xf;
391 if (idx
> raw
->curlen
)
393 for (i
= 0; i
< repmsg
->u
.link_addr
.nports
; i
++) {
394 if (raw
->msg
[idx
] & 0x80)
395 repmsg
->u
.link_addr
.ports
[i
].input_port
= 1;
397 repmsg
->u
.link_addr
.ports
[i
].peer_device_type
= (raw
->msg
[idx
] >> 4) & 0x7;
398 repmsg
->u
.link_addr
.ports
[i
].port_number
= (raw
->msg
[idx
] & 0xf);
401 if (idx
> raw
->curlen
)
403 repmsg
->u
.link_addr
.ports
[i
].mcs
= (raw
->msg
[idx
] >> 7) & 0x1;
404 repmsg
->u
.link_addr
.ports
[i
].ddps
= (raw
->msg
[idx
] >> 6) & 0x1;
405 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0)
406 repmsg
->u
.link_addr
.ports
[i
].legacy_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
408 if (idx
> raw
->curlen
)
410 if (repmsg
->u
.link_addr
.ports
[i
].input_port
== 0) {
411 repmsg
->u
.link_addr
.ports
[i
].dpcd_revision
= (raw
->msg
[idx
]);
413 if (idx
> raw
->curlen
)
415 memcpy(repmsg
->u
.link_addr
.ports
[i
].peer_guid
, &raw
->msg
[idx
], 16);
417 if (idx
> raw
->curlen
)
419 repmsg
->u
.link_addr
.ports
[i
].num_sdp_streams
= (raw
->msg
[idx
] >> 4) & 0xf;
420 repmsg
->u
.link_addr
.ports
[i
].num_sdp_stream_sinks
= (raw
->msg
[idx
] & 0xf);
424 if (idx
> raw
->curlen
)
430 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
434 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx
*raw
,
435 struct drm_dp_sideband_msg_reply_body
*repmsg
)
438 repmsg
->u
.remote_dpcd_read_ack
.port_number
= raw
->msg
[idx
] & 0xf;
440 if (idx
> raw
->curlen
)
442 repmsg
->u
.remote_dpcd_read_ack
.num_bytes
= raw
->msg
[idx
];
444 if (idx
> raw
->curlen
)
447 memcpy(repmsg
->u
.remote_dpcd_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_dpcd_read_ack
.num_bytes
);
450 DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx
, raw
->curlen
);
454 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx
*raw
,
455 struct drm_dp_sideband_msg_reply_body
*repmsg
)
458 repmsg
->u
.remote_dpcd_write_ack
.port_number
= raw
->msg
[idx
] & 0xf;
460 if (idx
> raw
->curlen
)
464 DRM_DEBUG_KMS("parse length fail %d %d\n", idx
, raw
->curlen
);
468 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx
*raw
,
469 struct drm_dp_sideband_msg_reply_body
*repmsg
)
473 repmsg
->u
.remote_i2c_read_ack
.port_number
= (raw
->msg
[idx
] & 0xf);
475 if (idx
> raw
->curlen
)
477 repmsg
->u
.remote_i2c_read_ack
.num_bytes
= raw
->msg
[idx
];
480 memcpy(repmsg
->u
.remote_i2c_read_ack
.bytes
, &raw
->msg
[idx
], repmsg
->u
.remote_i2c_read_ack
.num_bytes
);
483 DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx
, raw
->curlen
);
487 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx
*raw
,
488 struct drm_dp_sideband_msg_reply_body
*repmsg
)
491 repmsg
->u
.path_resources
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
493 if (idx
> raw
->curlen
)
495 repmsg
->u
.path_resources
.full_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
497 if (idx
> raw
->curlen
)
499 repmsg
->u
.path_resources
.avail_payload_bw_number
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
501 if (idx
> raw
->curlen
)
505 DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx
, raw
->curlen
);
509 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
510 struct drm_dp_sideband_msg_reply_body
*repmsg
)
513 repmsg
->u
.allocate_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
515 if (idx
> raw
->curlen
)
517 repmsg
->u
.allocate_payload
.vcpi
= raw
->msg
[idx
];
519 if (idx
> raw
->curlen
)
521 repmsg
->u
.allocate_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+1]);
523 if (idx
> raw
->curlen
)
527 DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx
, raw
->curlen
);
531 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx
*raw
,
532 struct drm_dp_sideband_msg_reply_body
*repmsg
)
535 repmsg
->u
.query_payload
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
537 if (idx
> raw
->curlen
)
539 repmsg
->u
.query_payload
.allocated_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
541 if (idx
> raw
->curlen
)
545 DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx
, raw
->curlen
);
549 static bool drm_dp_sideband_parse_power_updown_phy_ack(struct drm_dp_sideband_msg_rx
*raw
,
550 struct drm_dp_sideband_msg_reply_body
*repmsg
)
554 repmsg
->u
.port_number
.port_number
= (raw
->msg
[idx
] >> 4) & 0xf;
556 if (idx
> raw
->curlen
) {
557 DRM_DEBUG_KMS("power up/down phy parse length fail %d %d\n",
564 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx
*raw
,
565 struct drm_dp_sideband_msg_reply_body
*msg
)
567 memset(msg
, 0, sizeof(*msg
));
568 msg
->reply_type
= (raw
->msg
[0] & 0x80) >> 7;
569 msg
->req_type
= (raw
->msg
[0] & 0x7f);
571 if (msg
->reply_type
) {
572 memcpy(msg
->u
.nak
.guid
, &raw
->msg
[1], 16);
573 msg
->u
.nak
.reason
= raw
->msg
[17];
574 msg
->u
.nak
.nak_data
= raw
->msg
[18];
578 switch (msg
->req_type
) {
579 case DP_LINK_ADDRESS
:
580 return drm_dp_sideband_parse_link_address(raw
, msg
);
581 case DP_QUERY_PAYLOAD
:
582 return drm_dp_sideband_parse_query_payload_ack(raw
, msg
);
583 case DP_REMOTE_DPCD_READ
:
584 return drm_dp_sideband_parse_remote_dpcd_read(raw
, msg
);
585 case DP_REMOTE_DPCD_WRITE
:
586 return drm_dp_sideband_parse_remote_dpcd_write(raw
, msg
);
587 case DP_REMOTE_I2C_READ
:
588 return drm_dp_sideband_parse_remote_i2c_read_ack(raw
, msg
);
589 case DP_ENUM_PATH_RESOURCES
:
590 return drm_dp_sideband_parse_enum_path_resources_ack(raw
, msg
);
591 case DP_ALLOCATE_PAYLOAD
:
592 return drm_dp_sideband_parse_allocate_payload_ack(raw
, msg
);
593 case DP_POWER_DOWN_PHY
:
594 case DP_POWER_UP_PHY
:
595 return drm_dp_sideband_parse_power_updown_phy_ack(raw
, msg
);
597 DRM_ERROR("Got unknown reply 0x%02x\n", msg
->req_type
);
602 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
603 struct drm_dp_sideband_msg_req_body
*msg
)
607 msg
->u
.conn_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
609 if (idx
> raw
->curlen
)
612 memcpy(msg
->u
.conn_stat
.guid
, &raw
->msg
[idx
], 16);
614 if (idx
> raw
->curlen
)
617 msg
->u
.conn_stat
.legacy_device_plug_status
= (raw
->msg
[idx
] >> 6) & 0x1;
618 msg
->u
.conn_stat
.displayport_device_plug_status
= (raw
->msg
[idx
] >> 5) & 0x1;
619 msg
->u
.conn_stat
.message_capability_status
= (raw
->msg
[idx
] >> 4) & 0x1;
620 msg
->u
.conn_stat
.input_port
= (raw
->msg
[idx
] >> 3) & 0x1;
621 msg
->u
.conn_stat
.peer_device_type
= (raw
->msg
[idx
] & 0x7);
625 DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx
, raw
->curlen
);
629 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx
*raw
,
630 struct drm_dp_sideband_msg_req_body
*msg
)
634 msg
->u
.resource_stat
.port_number
= (raw
->msg
[idx
] & 0xf0) >> 4;
636 if (idx
> raw
->curlen
)
639 memcpy(msg
->u
.resource_stat
.guid
, &raw
->msg
[idx
], 16);
641 if (idx
> raw
->curlen
)
644 msg
->u
.resource_stat
.available_pbn
= (raw
->msg
[idx
] << 8) | (raw
->msg
[idx
+ 1]);
648 DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx
, raw
->curlen
);
652 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx
*raw
,
653 struct drm_dp_sideband_msg_req_body
*msg
)
655 memset(msg
, 0, sizeof(*msg
));
656 msg
->req_type
= (raw
->msg
[0] & 0x7f);
658 switch (msg
->req_type
) {
659 case DP_CONNECTION_STATUS_NOTIFY
:
660 return drm_dp_sideband_parse_connection_status_notify(raw
, msg
);
661 case DP_RESOURCE_STATUS_NOTIFY
:
662 return drm_dp_sideband_parse_resource_status_notify(raw
, msg
);
664 DRM_ERROR("Got unknown request 0x%02x\n", msg
->req_type
);
669 static int build_dpcd_write(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
, u8
*bytes
)
671 struct drm_dp_sideband_msg_req_body req
;
673 req
.req_type
= DP_REMOTE_DPCD_WRITE
;
674 req
.u
.dpcd_write
.port_number
= port_num
;
675 req
.u
.dpcd_write
.dpcd_address
= offset
;
676 req
.u
.dpcd_write
.num_bytes
= num_bytes
;
677 req
.u
.dpcd_write
.bytes
= bytes
;
678 drm_dp_encode_sideband_req(&req
, msg
);
683 static int build_link_address(struct drm_dp_sideband_msg_tx
*msg
)
685 struct drm_dp_sideband_msg_req_body req
;
687 req
.req_type
= DP_LINK_ADDRESS
;
688 drm_dp_encode_sideband_req(&req
, msg
);
692 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx
*msg
, int port_num
)
694 struct drm_dp_sideband_msg_req_body req
;
696 req
.req_type
= DP_ENUM_PATH_RESOURCES
;
697 req
.u
.port_num
.port_number
= port_num
;
698 drm_dp_encode_sideband_req(&req
, msg
);
699 msg
->path_msg
= true;
703 static int build_allocate_payload(struct drm_dp_sideband_msg_tx
*msg
, int port_num
,
704 u8 vcpi
, uint16_t pbn
,
705 u8 number_sdp_streams
,
708 struct drm_dp_sideband_msg_req_body req
;
709 memset(&req
, 0, sizeof(req
));
710 req
.req_type
= DP_ALLOCATE_PAYLOAD
;
711 req
.u
.allocate_payload
.port_number
= port_num
;
712 req
.u
.allocate_payload
.vcpi
= vcpi
;
713 req
.u
.allocate_payload
.pbn
= pbn
;
714 req
.u
.allocate_payload
.number_sdp_streams
= number_sdp_streams
;
715 memcpy(req
.u
.allocate_payload
.sdp_stream_sink
, sdp_stream_sink
,
717 drm_dp_encode_sideband_req(&req
, msg
);
718 msg
->path_msg
= true;
722 static int build_power_updown_phy(struct drm_dp_sideband_msg_tx
*msg
,
723 int port_num
, bool power_up
)
725 struct drm_dp_sideband_msg_req_body req
;
728 req
.req_type
= DP_POWER_UP_PHY
;
730 req
.req_type
= DP_POWER_DOWN_PHY
;
732 req
.u
.port_num
.port_number
= port_num
;
733 drm_dp_encode_sideband_req(&req
, msg
);
734 msg
->path_msg
= true;
738 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
739 struct drm_dp_vcpi
*vcpi
)
743 mutex_lock(&mgr
->payload_lock
);
744 ret
= find_first_zero_bit(&mgr
->payload_mask
, mgr
->max_payloads
+ 1);
745 if (ret
> mgr
->max_payloads
) {
747 DRM_DEBUG_KMS("out of payload ids %d\n", ret
);
751 vcpi_ret
= find_first_zero_bit(&mgr
->vcpi_mask
, mgr
->max_payloads
+ 1);
752 if (vcpi_ret
> mgr
->max_payloads
) {
754 DRM_DEBUG_KMS("out of vcpi ids %d\n", ret
);
758 set_bit(ret
, &mgr
->payload_mask
);
759 set_bit(vcpi_ret
, &mgr
->vcpi_mask
);
760 vcpi
->vcpi
= vcpi_ret
+ 1;
761 mgr
->proposed_vcpis
[ret
- 1] = vcpi
;
763 mutex_unlock(&mgr
->payload_lock
);
767 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr
*mgr
,
774 mutex_lock(&mgr
->payload_lock
);
775 DRM_DEBUG_KMS("putting payload %d\n", vcpi
);
776 clear_bit(vcpi
- 1, &mgr
->vcpi_mask
);
778 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
779 if (mgr
->proposed_vcpis
[i
])
780 if (mgr
->proposed_vcpis
[i
]->vcpi
== vcpi
) {
781 mgr
->proposed_vcpis
[i
] = NULL
;
782 clear_bit(i
+ 1, &mgr
->payload_mask
);
785 mutex_unlock(&mgr
->payload_lock
);
788 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr
*mgr
,
789 struct drm_dp_sideband_msg_tx
*txmsg
)
794 * All updates to txmsg->state are protected by mgr->qlock, and the two
795 * cases we check here are terminal states. For those the barriers
796 * provided by the wake_up/wait_event pair are enough.
798 state
= READ_ONCE(txmsg
->state
);
799 return (state
== DRM_DP_SIDEBAND_TX_RX
||
800 state
== DRM_DP_SIDEBAND_TX_TIMEOUT
);
803 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch
*mstb
,
804 struct drm_dp_sideband_msg_tx
*txmsg
)
806 struct drm_dp_mst_topology_mgr
*mgr
= mstb
->mgr
;
809 ret
= wait_event_timeout(mgr
->tx_waitq
,
810 check_txmsg_state(mgr
, txmsg
),
812 mutex_lock(&mstb
->mgr
->qlock
);
814 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_TIMEOUT
) {
819 DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg
, txmsg
->state
, txmsg
->seqno
);
821 /* dump some state */
825 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
||
826 txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
) {
827 list_del(&txmsg
->next
);
830 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_START_SEND
||
831 txmsg
->state
== DRM_DP_SIDEBAND_TX_SENT
) {
832 mstb
->tx_slots
[txmsg
->seqno
] = NULL
;
836 mutex_unlock(&mgr
->qlock
);
841 static struct drm_dp_mst_branch
*drm_dp_add_mst_branch_device(u8 lct
, u8
*rad
)
843 struct drm_dp_mst_branch
*mstb
;
845 mstb
= kzalloc(sizeof(*mstb
), GFP_KERNEL
);
851 memcpy(mstb
->rad
, rad
, lct
/ 2);
852 INIT_LIST_HEAD(&mstb
->ports
);
853 kref_init(&mstb
->kref
);
857 static void drm_dp_free_mst_port(struct kref
*kref
);
859 static void drm_dp_free_mst_branch_device(struct kref
*kref
)
861 struct drm_dp_mst_branch
*mstb
= container_of(kref
, struct drm_dp_mst_branch
, kref
);
862 if (mstb
->port_parent
) {
863 if (list_empty(&mstb
->port_parent
->next
))
864 kref_put(&mstb
->port_parent
->kref
, drm_dp_free_mst_port
);
869 static void drm_dp_destroy_mst_branch_device(struct kref
*kref
)
871 struct drm_dp_mst_branch
*mstb
= container_of(kref
, struct drm_dp_mst_branch
, kref
);
872 struct drm_dp_mst_port
*port
, *tmp
;
873 bool wake_tx
= false;
876 * init kref again to be used by ports to remove mst branch when it is
881 if (mstb
->port_parent
&& list_empty(&mstb
->port_parent
->next
))
882 kref_get(&mstb
->port_parent
->kref
);
885 * destroy all ports - don't need lock
886 * as there are no more references to the mst branch
887 * device at this point.
889 list_for_each_entry_safe(port
, tmp
, &mstb
->ports
, next
) {
890 list_del(&port
->next
);
891 drm_dp_put_port(port
);
894 /* drop any tx slots msg */
895 mutex_lock(&mstb
->mgr
->qlock
);
896 if (mstb
->tx_slots
[0]) {
897 mstb
->tx_slots
[0]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
898 mstb
->tx_slots
[0] = NULL
;
901 if (mstb
->tx_slots
[1]) {
902 mstb
->tx_slots
[1]->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
903 mstb
->tx_slots
[1] = NULL
;
906 mutex_unlock(&mstb
->mgr
->qlock
);
909 wake_up_all(&mstb
->mgr
->tx_waitq
);
911 kref_put(kref
, drm_dp_free_mst_branch_device
);
914 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch
*mstb
)
916 kref_put(&mstb
->kref
, drm_dp_destroy_mst_branch_device
);
920 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port
*port
, int old_pdt
)
922 struct drm_dp_mst_branch
*mstb
;
925 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
926 case DP_PEER_DEVICE_SST_SINK
:
927 /* remove i2c over sideband */
928 drm_dp_mst_unregister_i2c_bus(&port
->aux
);
930 case DP_PEER_DEVICE_MST_BRANCHING
:
933 drm_dp_put_mst_branch_device(mstb
);
938 static void drm_dp_destroy_port(struct kref
*kref
)
940 struct drm_dp_mst_port
*port
= container_of(kref
, struct drm_dp_mst_port
, kref
);
941 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
944 port
->vcpi
.num_slots
= 0;
946 kfree(port
->cached_edid
);
949 * The only time we don't have a connector
950 * on an output port is if the connector init
953 if (port
->connector
) {
954 /* we can't destroy the connector here, as
955 * we might be holding the mode_config.mutex
956 * from an EDID retrieval */
958 mutex_lock(&mgr
->destroy_connector_lock
);
959 kref_get(&port
->parent
->kref
);
960 list_add(&port
->next
, &mgr
->destroy_connector_list
);
961 mutex_unlock(&mgr
->destroy_connector_lock
);
962 schedule_work(&mgr
->destroy_connector_work
);
965 /* no need to clean up vcpi
966 * as if we have no connector we never setup a vcpi */
967 drm_dp_port_teardown_pdt(port
, port
->pdt
);
968 port
->pdt
= DP_PEER_DEVICE_NONE
;
973 static void drm_dp_put_port(struct drm_dp_mst_port
*port
)
975 kref_put(&port
->kref
, drm_dp_destroy_port
);
978 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
)
980 struct drm_dp_mst_port
*port
;
981 struct drm_dp_mst_branch
*rmstb
;
982 if (to_find
== mstb
) {
983 kref_get(&mstb
->kref
);
986 list_for_each_entry(port
, &mstb
->ports
, next
) {
988 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(port
->mstb
, to_find
);
996 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
)
998 struct drm_dp_mst_branch
*rmstb
= NULL
;
999 mutex_lock(&mgr
->lock
);
1000 if (mgr
->mst_primary
)
1001 rmstb
= drm_dp_mst_get_validated_mstb_ref_locked(mgr
->mst_primary
, mstb
);
1002 mutex_unlock(&mgr
->lock
);
1006 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
)
1008 struct drm_dp_mst_port
*port
, *mport
;
1010 list_for_each_entry(port
, &mstb
->ports
, next
) {
1011 if (port
== to_find
) {
1012 kref_get(&port
->kref
);
1016 mport
= drm_dp_mst_get_port_ref_locked(port
->mstb
, to_find
);
1024 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
)
1026 struct drm_dp_mst_port
*rport
= NULL
;
1027 mutex_lock(&mgr
->lock
);
1028 if (mgr
->mst_primary
)
1029 rport
= drm_dp_mst_get_port_ref_locked(mgr
->mst_primary
, port
);
1030 mutex_unlock(&mgr
->lock
);
1034 static struct drm_dp_mst_port
*drm_dp_get_port(struct drm_dp_mst_branch
*mstb
, u8 port_num
)
1036 struct drm_dp_mst_port
*port
;
1038 list_for_each_entry(port
, &mstb
->ports
, next
) {
1039 if (port
->port_num
== port_num
) {
1040 kref_get(&port
->kref
);
1049 * calculate a new RAD for this MST branch device
1050 * if parent has an LCT of 2 then it has 1 nibble of RAD,
1051 * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1053 static u8
drm_dp_calculate_rad(struct drm_dp_mst_port
*port
,
1056 int parent_lct
= port
->parent
->lct
;
1058 int idx
= (parent_lct
- 1) / 2;
1059 if (parent_lct
> 1) {
1060 memcpy(rad
, port
->parent
->rad
, idx
+ 1);
1061 shift
= (parent_lct
% 2) ? 4 : 0;
1065 rad
[idx
] |= port
->port_num
<< shift
;
1066 return parent_lct
+ 1;
1070 * return sends link address for new mstb
1072 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port
*port
)
1076 bool send_link
= false;
1077 switch (port
->pdt
) {
1078 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
1079 case DP_PEER_DEVICE_SST_SINK
:
1080 /* add i2c over sideband */
1081 ret
= drm_dp_mst_register_i2c_bus(&port
->aux
);
1083 case DP_PEER_DEVICE_MST_BRANCHING
:
1084 lct
= drm_dp_calculate_rad(port
, rad
);
1086 port
->mstb
= drm_dp_add_mst_branch_device(lct
, rad
);
1088 port
->mstb
->mgr
= port
->mgr
;
1089 port
->mstb
->port_parent
= port
;
1098 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch
*mstb
, u8
*guid
)
1102 memcpy(mstb
->guid
, guid
, 16);
1104 if (!drm_dp_validate_guid(mstb
->mgr
, mstb
->guid
)) {
1105 if (mstb
->port_parent
) {
1106 ret
= drm_dp_send_dpcd_write(
1114 ret
= drm_dp_dpcd_write(
1123 static void build_mst_prop_path(const struct drm_dp_mst_branch
*mstb
,
1126 size_t proppath_size
)
1130 snprintf(proppath
, proppath_size
, "mst:%d", mstb
->mgr
->conn_base_id
);
1131 for (i
= 0; i
< (mstb
->lct
- 1); i
++) {
1132 int shift
= (i
% 2) ? 0 : 4;
1133 int port_num
= (mstb
->rad
[i
/ 2] >> shift
) & 0xf;
1134 snprintf(temp
, sizeof(temp
), "-%d", port_num
);
1135 strlcat(proppath
, temp
, proppath_size
);
1137 snprintf(temp
, sizeof(temp
), "-%d", pnum
);
1138 strlcat(proppath
, temp
, proppath_size
);
1141 static void drm_dp_add_port(struct drm_dp_mst_branch
*mstb
,
1142 struct drm_device
*dev
,
1143 struct drm_dp_link_addr_reply_port
*port_msg
)
1145 struct drm_dp_mst_port
*port
;
1147 bool created
= false;
1150 port
= drm_dp_get_port(mstb
, port_msg
->port_number
);
1152 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
1155 kref_init(&port
->kref
);
1156 port
->parent
= mstb
;
1157 port
->port_num
= port_msg
->port_number
;
1158 port
->mgr
= mstb
->mgr
;
1159 port
->aux
.name
= "DPMST";
1160 port
->aux
.dev
= dev
->dev
;
1163 old_pdt
= port
->pdt
;
1164 old_ddps
= port
->ddps
;
1167 port
->pdt
= port_msg
->peer_device_type
;
1168 port
->input
= port_msg
->input_port
;
1169 port
->mcs
= port_msg
->mcs
;
1170 port
->ddps
= port_msg
->ddps
;
1171 port
->ldps
= port_msg
->legacy_device_plug_status
;
1172 port
->dpcd_rev
= port_msg
->dpcd_revision
;
1173 port
->num_sdp_streams
= port_msg
->num_sdp_streams
;
1174 port
->num_sdp_stream_sinks
= port_msg
->num_sdp_stream_sinks
;
1176 /* manage mstb port lists with mgr lock - take a reference
1179 mutex_lock(&mstb
->mgr
->lock
);
1180 kref_get(&port
->kref
);
1181 list_add(&port
->next
, &mstb
->ports
);
1182 mutex_unlock(&mstb
->mgr
->lock
);
1185 if (old_ddps
!= port
->ddps
) {
1188 drm_dp_send_enum_path_resources(mstb
->mgr
, mstb
, port
);
1190 port
->available_pbn
= 0;
1194 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1195 drm_dp_port_teardown_pdt(port
, old_pdt
);
1197 ret
= drm_dp_port_setup_pdt(port
);
1199 drm_dp_send_link_address(mstb
->mgr
, port
->mstb
);
1202 if (created
&& !port
->input
) {
1205 build_mst_prop_path(mstb
, port
->port_num
, proppath
, sizeof(proppath
));
1206 port
->connector
= (*mstb
->mgr
->cbs
->add_connector
)(mstb
->mgr
, port
, proppath
);
1207 if (!port
->connector
) {
1208 /* remove it from the port list */
1209 mutex_lock(&mstb
->mgr
->lock
);
1210 list_del(&port
->next
);
1211 mutex_unlock(&mstb
->mgr
->lock
);
1212 /* drop port list reference */
1213 drm_dp_put_port(port
);
1216 if ((port
->pdt
== DP_PEER_DEVICE_DP_LEGACY_CONV
||
1217 port
->pdt
== DP_PEER_DEVICE_SST_SINK
) &&
1218 port
->port_num
>= DP_MST_LOGICAL_PORT_0
) {
1219 port
->cached_edid
= drm_get_edid(port
->connector
, &port
->aux
.ddc
);
1220 drm_connector_set_tile_property(port
->connector
);
1222 (*mstb
->mgr
->cbs
->register_connector
)(port
->connector
);
1226 /* put reference to this port */
1227 drm_dp_put_port(port
);
1230 static void drm_dp_update_port(struct drm_dp_mst_branch
*mstb
,
1231 struct drm_dp_connection_status_notify
*conn_stat
)
1233 struct drm_dp_mst_port
*port
;
1236 bool dowork
= false;
1237 port
= drm_dp_get_port(mstb
, conn_stat
->port_number
);
1241 old_ddps
= port
->ddps
;
1242 old_pdt
= port
->pdt
;
1243 port
->pdt
= conn_stat
->peer_device_type
;
1244 port
->mcs
= conn_stat
->message_capability_status
;
1245 port
->ldps
= conn_stat
->legacy_device_plug_status
;
1246 port
->ddps
= conn_stat
->displayport_device_plug_status
;
1248 if (old_ddps
!= port
->ddps
) {
1252 port
->available_pbn
= 0;
1255 if (old_pdt
!= port
->pdt
&& !port
->input
) {
1256 drm_dp_port_teardown_pdt(port
, old_pdt
);
1258 if (drm_dp_port_setup_pdt(port
))
1262 drm_dp_put_port(port
);
1264 queue_work(system_long_wq
, &mstb
->mgr
->work
);
1268 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr
*mgr
,
1271 struct drm_dp_mst_branch
*mstb
;
1272 struct drm_dp_mst_port
*port
;
1274 /* find the port by iterating down */
1276 mutex_lock(&mgr
->lock
);
1277 mstb
= mgr
->mst_primary
;
1282 for (i
= 0; i
< lct
- 1; i
++) {
1283 int shift
= (i
% 2) ? 0 : 4;
1284 int port_num
= (rad
[i
/ 2] >> shift
) & 0xf;
1286 list_for_each_entry(port
, &mstb
->ports
, next
) {
1287 if (port
->port_num
== port_num
) {
1290 DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct
, rad
[0]);
1298 kref_get(&mstb
->kref
);
1300 mutex_unlock(&mgr
->lock
);
1304 static struct drm_dp_mst_branch
*get_mst_branch_device_by_guid_helper(
1305 struct drm_dp_mst_branch
*mstb
,
1308 struct drm_dp_mst_branch
*found_mstb
;
1309 struct drm_dp_mst_port
*port
;
1311 if (memcmp(mstb
->guid
, guid
, 16) == 0)
1315 list_for_each_entry(port
, &mstb
->ports
, next
) {
1319 found_mstb
= get_mst_branch_device_by_guid_helper(port
->mstb
, guid
);
1328 static struct drm_dp_mst_branch
*drm_dp_get_mst_branch_device_by_guid(
1329 struct drm_dp_mst_topology_mgr
*mgr
,
1332 struct drm_dp_mst_branch
*mstb
;
1334 /* find the port by iterating down */
1335 mutex_lock(&mgr
->lock
);
1337 mstb
= get_mst_branch_device_by_guid_helper(mgr
->mst_primary
, guid
);
1340 kref_get(&mstb
->kref
);
1342 mutex_unlock(&mgr
->lock
);
1346 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1347 struct drm_dp_mst_branch
*mstb
)
1349 struct drm_dp_mst_port
*port
;
1350 struct drm_dp_mst_branch
*mstb_child
;
1351 if (!mstb
->link_address_sent
)
1352 drm_dp_send_link_address(mgr
, mstb
);
1354 list_for_each_entry(port
, &mstb
->ports
, next
) {
1361 if (!port
->available_pbn
)
1362 drm_dp_send_enum_path_resources(mgr
, mstb
, port
);
1365 mstb_child
= drm_dp_get_validated_mstb_ref(mgr
, port
->mstb
);
1367 drm_dp_check_and_send_link_address(mgr
, mstb_child
);
1368 drm_dp_put_mst_branch_device(mstb_child
);
1374 static void drm_dp_mst_link_probe_work(struct work_struct
*work
)
1376 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, work
);
1377 struct drm_dp_mst_branch
*mstb
;
1379 mutex_lock(&mgr
->lock
);
1380 mstb
= mgr
->mst_primary
;
1382 kref_get(&mstb
->kref
);
1384 mutex_unlock(&mgr
->lock
);
1386 drm_dp_check_and_send_link_address(mgr
, mstb
);
1387 drm_dp_put_mst_branch_device(mstb
);
1391 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr
*mgr
,
1396 if (memchr_inv(guid
, 0, 16))
1399 salt
= get_jiffies_64();
1401 memcpy(&guid
[0], &salt
, sizeof(u64
));
1402 memcpy(&guid
[8], &salt
, sizeof(u64
));
1408 static int build_dpcd_read(struct drm_dp_sideband_msg_tx
*msg
, u8 port_num
, u32 offset
, u8 num_bytes
)
1410 struct drm_dp_sideband_msg_req_body req
;
1412 req
.req_type
= DP_REMOTE_DPCD_READ
;
1413 req
.u
.dpcd_read
.port_number
= port_num
;
1414 req
.u
.dpcd_read
.dpcd_address
= offset
;
1415 req
.u
.dpcd_read
.num_bytes
= num_bytes
;
1416 drm_dp_encode_sideband_req(&req
, msg
);
1422 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1423 bool up
, u8
*msg
, int len
)
1426 int regbase
= up
? DP_SIDEBAND_MSG_UP_REP_BASE
: DP_SIDEBAND_MSG_DOWN_REQ_BASE
;
1427 int tosend
, total
, offset
;
1434 tosend
= min3(mgr
->max_dpcd_transaction_bytes
, 16, total
);
1436 ret
= drm_dp_dpcd_write(mgr
->aux
, regbase
+ offset
,
1439 if (ret
!= tosend
) {
1440 if (ret
== -EIO
&& retries
< 5) {
1444 DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend
, ret
);
1450 } while (total
> 0);
1454 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr
*hdr
,
1455 struct drm_dp_sideband_msg_tx
*txmsg
)
1457 struct drm_dp_mst_branch
*mstb
= txmsg
->dst
;
1460 /* both msg slots are full */
1461 if (txmsg
->seqno
== -1) {
1462 if (mstb
->tx_slots
[0] && mstb
->tx_slots
[1]) {
1463 DRM_DEBUG_KMS("%s: failed to find slot\n", __func__
);
1466 if (mstb
->tx_slots
[0] == NULL
&& mstb
->tx_slots
[1] == NULL
) {
1467 txmsg
->seqno
= mstb
->last_seqno
;
1468 mstb
->last_seqno
^= 1;
1469 } else if (mstb
->tx_slots
[0] == NULL
)
1473 mstb
->tx_slots
[txmsg
->seqno
] = txmsg
;
1476 req_type
= txmsg
->msg
[0] & 0x7f;
1477 if (req_type
== DP_CONNECTION_STATUS_NOTIFY
||
1478 req_type
== DP_RESOURCE_STATUS_NOTIFY
)
1482 hdr
->path_msg
= txmsg
->path_msg
;
1483 hdr
->lct
= mstb
->lct
;
1484 hdr
->lcr
= mstb
->lct
- 1;
1486 memcpy(hdr
->rad
, mstb
->rad
, mstb
->lct
/ 2);
1487 hdr
->seqno
= txmsg
->seqno
;
1491 * process a single block of the next message in the sideband queue
1493 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
1494 struct drm_dp_sideband_msg_tx
*txmsg
,
1498 struct drm_dp_sideband_msg_hdr hdr
;
1499 int len
, space
, idx
, tosend
;
1502 memset(&hdr
, 0, sizeof(struct drm_dp_sideband_msg_hdr
));
1504 if (txmsg
->state
== DRM_DP_SIDEBAND_TX_QUEUED
) {
1506 txmsg
->state
= DRM_DP_SIDEBAND_TX_START_SEND
;
1509 /* make hdr from dst mst - for replies use seqno
1510 otherwise assign one */
1511 ret
= set_hdr_from_dst_qlock(&hdr
, txmsg
);
1515 /* amount left to send in this message */
1516 len
= txmsg
->cur_len
- txmsg
->cur_offset
;
1518 /* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1519 space
= 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr
);
1521 tosend
= min(len
, space
);
1522 if (len
== txmsg
->cur_len
)
1528 hdr
.msg_len
= tosend
+ 1;
1529 drm_dp_encode_sideband_msg_hdr(&hdr
, chunk
, &idx
);
1530 memcpy(&chunk
[idx
], &txmsg
->msg
[txmsg
->cur_offset
], tosend
);
1531 /* add crc at end */
1532 drm_dp_crc_sideband_chunk_req(&chunk
[idx
], tosend
);
1535 ret
= drm_dp_send_sideband_msg(mgr
, up
, chunk
, idx
);
1537 DRM_DEBUG_KMS("sideband msg failed to send\n");
1541 txmsg
->cur_offset
+= tosend
;
1542 if (txmsg
->cur_offset
== txmsg
->cur_len
) {
1543 txmsg
->state
= DRM_DP_SIDEBAND_TX_SENT
;
1549 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
)
1551 struct drm_dp_sideband_msg_tx
*txmsg
;
1554 WARN_ON(!mutex_is_locked(&mgr
->qlock
));
1556 /* construct a chunk from the first msg in the tx_msg queue */
1557 if (list_empty(&mgr
->tx_msg_downq
))
1560 txmsg
= list_first_entry(&mgr
->tx_msg_downq
, struct drm_dp_sideband_msg_tx
, next
);
1561 ret
= process_single_tx_qlock(mgr
, txmsg
, false);
1563 /* txmsg is sent it should be in the slots now */
1564 list_del(&txmsg
->next
);
1566 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1567 list_del(&txmsg
->next
);
1568 if (txmsg
->seqno
!= -1)
1569 txmsg
->dst
->tx_slots
[txmsg
->seqno
] = NULL
;
1570 txmsg
->state
= DRM_DP_SIDEBAND_TX_TIMEOUT
;
1571 wake_up_all(&mgr
->tx_waitq
);
1575 /* called holding qlock */
1576 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr
*mgr
,
1577 struct drm_dp_sideband_msg_tx
*txmsg
)
1581 /* construct a chunk from the first msg in the tx_msg queue */
1582 ret
= process_single_tx_qlock(mgr
, txmsg
, true);
1585 DRM_DEBUG_KMS("failed to send msg in q %d\n", ret
);
1587 if (txmsg
->seqno
!= -1) {
1588 WARN_ON((unsigned int)txmsg
->seqno
>
1589 ARRAY_SIZE(txmsg
->dst
->tx_slots
));
1590 txmsg
->dst
->tx_slots
[txmsg
->seqno
] = NULL
;
1594 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr
*mgr
,
1595 struct drm_dp_sideband_msg_tx
*txmsg
)
1597 mutex_lock(&mgr
->qlock
);
1598 list_add_tail(&txmsg
->next
, &mgr
->tx_msg_downq
);
1599 if (list_is_singular(&mgr
->tx_msg_downq
))
1600 process_single_down_tx_qlock(mgr
);
1601 mutex_unlock(&mgr
->qlock
);
1604 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr
*mgr
,
1605 struct drm_dp_mst_branch
*mstb
)
1608 struct drm_dp_sideband_msg_tx
*txmsg
;
1611 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1616 len
= build_link_address(txmsg
);
1618 mstb
->link_address_sent
= true;
1619 drm_dp_queue_down_tx(mgr
, txmsg
);
1621 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1625 if (txmsg
->reply
.reply_type
== 1)
1626 DRM_DEBUG_KMS("link address nak received\n");
1628 DRM_DEBUG_KMS("link address reply: %d\n", txmsg
->reply
.u
.link_addr
.nports
);
1629 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1630 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
,
1631 txmsg
->reply
.u
.link_addr
.ports
[i
].input_port
,
1632 txmsg
->reply
.u
.link_addr
.ports
[i
].peer_device_type
,
1633 txmsg
->reply
.u
.link_addr
.ports
[i
].port_number
,
1634 txmsg
->reply
.u
.link_addr
.ports
[i
].dpcd_revision
,
1635 txmsg
->reply
.u
.link_addr
.ports
[i
].mcs
,
1636 txmsg
->reply
.u
.link_addr
.ports
[i
].ddps
,
1637 txmsg
->reply
.u
.link_addr
.ports
[i
].legacy_device_plug_status
,
1638 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_streams
,
1639 txmsg
->reply
.u
.link_addr
.ports
[i
].num_sdp_stream_sinks
);
1642 drm_dp_check_mstb_guid(mstb
, txmsg
->reply
.u
.link_addr
.guid
);
1644 for (i
= 0; i
< txmsg
->reply
.u
.link_addr
.nports
; i
++) {
1645 drm_dp_add_port(mstb
, mgr
->dev
, &txmsg
->reply
.u
.link_addr
.ports
[i
]);
1647 (*mgr
->cbs
->hotplug
)(mgr
);
1650 mstb
->link_address_sent
= false;
1651 DRM_DEBUG_KMS("link address failed %d\n", ret
);
1657 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr
*mgr
,
1658 struct drm_dp_mst_branch
*mstb
,
1659 struct drm_dp_mst_port
*port
)
1662 struct drm_dp_sideband_msg_tx
*txmsg
;
1665 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1670 len
= build_enum_path_resources(txmsg
, port
->port_num
);
1672 drm_dp_queue_down_tx(mgr
, txmsg
);
1674 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1676 if (txmsg
->reply
.reply_type
== 1)
1677 DRM_DEBUG_KMS("enum path resources nak received\n");
1679 if (port
->port_num
!= txmsg
->reply
.u
.path_resources
.port_number
)
1680 DRM_ERROR("got incorrect port in response\n");
1681 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
,
1682 txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
);
1683 port
->available_pbn
= txmsg
->reply
.u
.path_resources
.avail_payload_bw_number
;
1691 static struct drm_dp_mst_port
*drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch
*mstb
)
1693 if (!mstb
->port_parent
)
1696 if (mstb
->port_parent
->mstb
!= mstb
)
1697 return mstb
->port_parent
;
1699 return drm_dp_get_last_connected_port_to_mstb(mstb
->port_parent
->parent
);
1702 static struct drm_dp_mst_branch
*drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr
*mgr
,
1703 struct drm_dp_mst_branch
*mstb
,
1706 struct drm_dp_mst_branch
*rmstb
= NULL
;
1707 struct drm_dp_mst_port
*found_port
;
1708 mutex_lock(&mgr
->lock
);
1709 if (mgr
->mst_primary
) {
1710 found_port
= drm_dp_get_last_connected_port_to_mstb(mstb
);
1713 rmstb
= found_port
->parent
;
1714 kref_get(&rmstb
->kref
);
1715 *port_num
= found_port
->port_num
;
1718 mutex_unlock(&mgr
->lock
);
1722 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr
*mgr
,
1723 struct drm_dp_mst_port
*port
,
1727 struct drm_dp_sideband_msg_tx
*txmsg
;
1728 struct drm_dp_mst_branch
*mstb
;
1729 int len
, ret
, port_num
;
1730 u8 sinks
[DRM_DP_MAX_SDP_STREAMS
];
1733 port
= drm_dp_get_validated_port_ref(mgr
, port
);
1737 port_num
= port
->port_num
;
1738 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
1740 mstb
= drm_dp_get_last_connected_port_and_mstb(mgr
, port
->parent
, &port_num
);
1743 drm_dp_put_port(port
);
1748 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1754 for (i
= 0; i
< port
->num_sdp_streams
; i
++)
1758 len
= build_allocate_payload(txmsg
, port_num
,
1760 pbn
, port
->num_sdp_streams
, sinks
);
1762 drm_dp_queue_down_tx(mgr
, txmsg
);
1764 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
1766 if (txmsg
->reply
.reply_type
== 1) {
1773 drm_dp_put_mst_branch_device(mstb
);
1774 drm_dp_put_port(port
);
1778 int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr
*mgr
,
1779 struct drm_dp_mst_port
*port
, bool power_up
)
1781 struct drm_dp_sideband_msg_tx
*txmsg
;
1784 port
= drm_dp_get_validated_port_ref(mgr
, port
);
1788 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
1790 drm_dp_put_port(port
);
1794 txmsg
->dst
= port
->parent
;
1795 len
= build_power_updown_phy(txmsg
, port
->port_num
, power_up
);
1796 drm_dp_queue_down_tx(mgr
, txmsg
);
1798 ret
= drm_dp_mst_wait_tx_reply(port
->parent
, txmsg
);
1800 if (txmsg
->reply
.reply_type
== 1)
1806 drm_dp_put_port(port
);
1810 EXPORT_SYMBOL(drm_dp_send_power_updown_phy
);
1812 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1814 struct drm_dp_payload
*payload
)
1818 ret
= drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1820 payload
->payload_state
= 0;
1823 payload
->payload_state
= DP_PAYLOAD_LOCAL
;
1827 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1828 struct drm_dp_mst_port
*port
,
1830 struct drm_dp_payload
*payload
)
1833 ret
= drm_dp_payload_send_msg(mgr
, port
, id
, port
->vcpi
.pbn
);
1836 payload
->payload_state
= DP_PAYLOAD_REMOTE
;
1840 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr
*mgr
,
1841 struct drm_dp_mst_port
*port
,
1843 struct drm_dp_payload
*payload
)
1845 DRM_DEBUG_KMS("\n");
1846 /* its okay for these to fail */
1848 drm_dp_payload_send_msg(mgr
, port
, id
, 0);
1851 drm_dp_dpcd_write_payload(mgr
, id
, payload
);
1852 payload
->payload_state
= DP_PAYLOAD_DELETE_LOCAL
;
1856 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr
*mgr
,
1858 struct drm_dp_payload
*payload
)
1860 payload
->payload_state
= 0;
1865 * drm_dp_update_payload_part1() - Execute payload update part 1
1866 * @mgr: manager to use.
1868 * This iterates over all proposed virtual channels, and tries to
1869 * allocate space in the link for them. For 0->slots transitions,
1870 * this step just writes the VCPI to the MST device. For slots->0
1871 * transitions, this writes the updated VCPIs and removes the
1872 * remote VC payloads.
1874 * after calling this the driver should generate ACT and payload
1877 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr
*mgr
)
1881 struct drm_dp_payload req_payload
;
1882 struct drm_dp_mst_port
*port
;
1884 mutex_lock(&mgr
->payload_lock
);
1885 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1886 /* solve the current payloads - compare to the hw ones
1887 - update the hw view */
1888 req_payload
.start_slot
= cur_slots
;
1889 if (mgr
->proposed_vcpis
[i
]) {
1890 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1891 port
= drm_dp_get_validated_port_ref(mgr
, port
);
1893 mutex_unlock(&mgr
->payload_lock
);
1896 req_payload
.num_slots
= mgr
->proposed_vcpis
[i
]->num_slots
;
1897 req_payload
.vcpi
= mgr
->proposed_vcpis
[i
]->vcpi
;
1900 req_payload
.num_slots
= 0;
1903 if (mgr
->payloads
[i
].start_slot
!= req_payload
.start_slot
) {
1904 mgr
->payloads
[i
].start_slot
= req_payload
.start_slot
;
1906 /* work out what is required to happen with this payload */
1907 if (mgr
->payloads
[i
].num_slots
!= req_payload
.num_slots
) {
1909 /* need to push an update for this payload */
1910 if (req_payload
.num_slots
) {
1911 drm_dp_create_payload_step1(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &req_payload
);
1912 mgr
->payloads
[i
].num_slots
= req_payload
.num_slots
;
1913 mgr
->payloads
[i
].vcpi
= req_payload
.vcpi
;
1914 } else if (mgr
->payloads
[i
].num_slots
) {
1915 mgr
->payloads
[i
].num_slots
= 0;
1916 drm_dp_destroy_payload_step1(mgr
, port
, mgr
->payloads
[i
].vcpi
, &mgr
->payloads
[i
]);
1917 req_payload
.payload_state
= mgr
->payloads
[i
].payload_state
;
1918 mgr
->payloads
[i
].start_slot
= 0;
1920 mgr
->payloads
[i
].payload_state
= req_payload
.payload_state
;
1922 cur_slots
+= req_payload
.num_slots
;
1925 drm_dp_put_port(port
);
1928 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1929 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1930 DRM_DEBUG_KMS("removing payload %d\n", i
);
1931 for (j
= i
; j
< mgr
->max_payloads
- 1; j
++) {
1932 memcpy(&mgr
->payloads
[j
], &mgr
->payloads
[j
+ 1], sizeof(struct drm_dp_payload
));
1933 mgr
->proposed_vcpis
[j
] = mgr
->proposed_vcpis
[j
+ 1];
1934 if (mgr
->proposed_vcpis
[j
] && mgr
->proposed_vcpis
[j
]->num_slots
) {
1935 set_bit(j
+ 1, &mgr
->payload_mask
);
1937 clear_bit(j
+ 1, &mgr
->payload_mask
);
1940 memset(&mgr
->payloads
[mgr
->max_payloads
- 1], 0, sizeof(struct drm_dp_payload
));
1941 mgr
->proposed_vcpis
[mgr
->max_payloads
- 1] = NULL
;
1942 clear_bit(mgr
->max_payloads
, &mgr
->payload_mask
);
1946 mutex_unlock(&mgr
->payload_lock
);
1950 EXPORT_SYMBOL(drm_dp_update_payload_part1
);
1953 * drm_dp_update_payload_part2() - Execute payload update part 2
1954 * @mgr: manager to use.
1956 * This iterates over all proposed virtual channels, and tries to
1957 * allocate space in the link for them. For 0->slots transitions,
1958 * this step writes the remote VC payload commands. For slots->0
1959 * this just resets some internal state.
1961 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr
*mgr
)
1963 struct drm_dp_mst_port
*port
;
1966 mutex_lock(&mgr
->payload_lock
);
1967 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
1969 if (!mgr
->proposed_vcpis
[i
])
1972 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
1974 DRM_DEBUG_KMS("payload %d %d\n", i
, mgr
->payloads
[i
].payload_state
);
1975 if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_LOCAL
) {
1976 ret
= drm_dp_create_payload_step2(mgr
, port
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1977 } else if (mgr
->payloads
[i
].payload_state
== DP_PAYLOAD_DELETE_LOCAL
) {
1978 ret
= drm_dp_destroy_payload_step2(mgr
, mgr
->proposed_vcpis
[i
]->vcpi
, &mgr
->payloads
[i
]);
1981 mutex_unlock(&mgr
->payload_lock
);
1985 mutex_unlock(&mgr
->payload_lock
);
1988 EXPORT_SYMBOL(drm_dp_update_payload_part2
);
1990 #if 0 /* unused as of yet */
1991 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr
*mgr
,
1992 struct drm_dp_mst_port
*port
,
1993 int offset
, int size
)
1996 struct drm_dp_sideband_msg_tx
*txmsg
;
1998 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
2002 len
= build_dpcd_read(txmsg
, port
->port_num
, 0, 8);
2003 txmsg
->dst
= port
->parent
;
2005 drm_dp_queue_down_tx(mgr
, txmsg
);
2011 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr
*mgr
,
2012 struct drm_dp_mst_port
*port
,
2013 int offset
, int size
, u8
*bytes
)
2017 struct drm_dp_sideband_msg_tx
*txmsg
;
2018 struct drm_dp_mst_branch
*mstb
;
2020 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
2024 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
2030 len
= build_dpcd_write(txmsg
, port
->port_num
, offset
, size
, bytes
);
2033 drm_dp_queue_down_tx(mgr
, txmsg
);
2035 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
2037 if (txmsg
->reply
.reply_type
== 1) {
2044 drm_dp_put_mst_branch_device(mstb
);
2048 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx
*msg
, u8 req_type
)
2050 struct drm_dp_sideband_msg_reply_body reply
;
2052 reply
.reply_type
= 0;
2053 reply
.req_type
= req_type
;
2054 drm_dp_encode_sideband_reply(&reply
, msg
);
2058 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr
*mgr
,
2059 struct drm_dp_mst_branch
*mstb
,
2060 int req_type
, int seqno
, bool broadcast
)
2062 struct drm_dp_sideband_msg_tx
*txmsg
;
2064 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
2069 txmsg
->seqno
= seqno
;
2070 drm_dp_encode_up_ack_reply(txmsg
, req_type
);
2072 mutex_lock(&mgr
->qlock
);
2074 process_single_up_tx_qlock(mgr
, txmsg
);
2076 mutex_unlock(&mgr
->qlock
);
2082 static bool drm_dp_get_vc_payload_bw(int dp_link_bw
,
2086 switch (dp_link_bw
) {
2088 DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2089 dp_link_bw
, dp_link_count
);
2092 case DP_LINK_BW_1_62
:
2093 *out
= 3 * dp_link_count
;
2095 case DP_LINK_BW_2_7
:
2096 *out
= 5 * dp_link_count
;
2098 case DP_LINK_BW_5_4
:
2099 *out
= 10 * dp_link_count
;
2101 case DP_LINK_BW_8_1
:
2102 *out
= 15 * dp_link_count
;
2109 * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2110 * @mgr: manager to set state for
2111 * @mst_state: true to enable MST on this connector - false to disable.
2113 * This is called by the driver when it detects an MST capable device plugged
2114 * into a DP MST capable port, or when a DP MST capable device is unplugged.
2116 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr
*mgr
, bool mst_state
)
2119 struct drm_dp_mst_branch
*mstb
= NULL
;
2121 mutex_lock(&mgr
->payload_lock
);
2122 mutex_lock(&mgr
->lock
);
2123 if (mst_state
== mgr
->mst_state
)
2126 mgr
->mst_state
= mst_state
;
2127 /* set the device into MST mode */
2129 WARN_ON(mgr
->mst_primary
);
2132 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
2133 if (ret
!= DP_RECEIVER_CAP_SIZE
) {
2134 DRM_DEBUG_KMS("failed to read DPCD\n");
2138 if (!drm_dp_get_vc_payload_bw(mgr
->dpcd
[1],
2139 mgr
->dpcd
[2] & DP_MAX_LANE_COUNT_MASK
,
2145 /* add initial branch device at LCT 1 */
2146 mstb
= drm_dp_add_mst_branch_device(1, NULL
);
2153 /* give this the main reference */
2154 mgr
->mst_primary
= mstb
;
2155 kref_get(&mgr
->mst_primary
->kref
);
2157 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2158 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
2164 struct drm_dp_payload reset_pay
;
2165 reset_pay
.start_slot
= 0;
2166 reset_pay
.num_slots
= 0x3f;
2167 drm_dp_dpcd_write_payload(mgr
, 0, &reset_pay
);
2170 queue_work(system_long_wq
, &mgr
->work
);
2174 /* disable MST on the device */
2175 mstb
= mgr
->mst_primary
;
2176 mgr
->mst_primary
= NULL
;
2177 /* this can fail if the device is gone */
2178 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
, 0);
2180 memset(mgr
->payloads
, 0,
2181 mgr
->max_payloads
* sizeof(mgr
->payloads
[0]));
2182 memset(mgr
->proposed_vcpis
, 0,
2183 mgr
->max_payloads
* sizeof(mgr
->proposed_vcpis
[0]));
2184 mgr
->payload_mask
= 0;
2185 set_bit(0, &mgr
->payload_mask
);
2190 mutex_unlock(&mgr
->lock
);
2191 mutex_unlock(&mgr
->payload_lock
);
2193 drm_dp_put_mst_branch_device(mstb
);
2197 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst
);
2200 * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2201 * @mgr: manager to suspend
2203 * This function tells the MST device that we can't handle UP messages
2204 * anymore. This should stop it from sending any since we are suspended.
2206 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr
*mgr
)
2208 mutex_lock(&mgr
->lock
);
2209 drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2210 DP_MST_EN
| DP_UPSTREAM_IS_SRC
);
2211 mutex_unlock(&mgr
->lock
);
2212 flush_work(&mgr
->work
);
2213 flush_work(&mgr
->destroy_connector_work
);
2215 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend
);
2218 * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2219 * @mgr: manager to resume
2221 * This will fetch DPCD and see if the device is still there,
2222 * if it is, it will rewrite the MSTM control bits, and return.
2224 * if the device fails this returns -1, and the driver should do
2225 * a full MST reprobe, in case we were undocked.
2227 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr
*mgr
)
2231 mutex_lock(&mgr
->lock
);
2233 if (mgr
->mst_primary
) {
2237 sret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, mgr
->dpcd
, DP_RECEIVER_CAP_SIZE
);
2238 if (sret
!= DP_RECEIVER_CAP_SIZE
) {
2239 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2244 ret
= drm_dp_dpcd_writeb(mgr
->aux
, DP_MSTM_CTRL
,
2245 DP_MST_EN
| DP_UP_REQ_EN
| DP_UPSTREAM_IS_SRC
);
2247 DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2252 /* Some hubs forget their guids after they resume */
2253 sret
= drm_dp_dpcd_read(mgr
->aux
, DP_GUID
, guid
, 16);
2255 DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2259 drm_dp_check_mstb_guid(mgr
->mst_primary
, guid
);
2266 mutex_unlock(&mgr
->lock
);
2269 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume
);
2271 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr
*mgr
, bool up
)
2275 int replylen
, origlen
, curreply
;
2277 struct drm_dp_sideband_msg_rx
*msg
;
2278 int basereg
= up
? DP_SIDEBAND_MSG_UP_REQ_BASE
: DP_SIDEBAND_MSG_DOWN_REP_BASE
;
2279 msg
= up
? &mgr
->up_req_recv
: &mgr
->down_rep_recv
;
2281 len
= min(mgr
->max_dpcd_transaction_bytes
, 16);
2282 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
,
2285 DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len
, ret
);
2288 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, true);
2290 DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock
[0]);
2293 replylen
= msg
->curchunk_len
+ msg
->curchunk_hdrlen
;
2298 while (replylen
> 0) {
2299 len
= min3(replylen
, mgr
->max_dpcd_transaction_bytes
, 16);
2300 ret
= drm_dp_dpcd_read(mgr
->aux
, basereg
+ curreply
,
2303 DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2308 ret
= drm_dp_sideband_msg_build(msg
, replyblock
, len
, false);
2310 DRM_DEBUG_KMS("failed to build sideband msg\n");
2320 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr
*mgr
)
2324 if (!drm_dp_get_one_sb_msg(mgr
, false)) {
2325 memset(&mgr
->down_rep_recv
, 0,
2326 sizeof(struct drm_dp_sideband_msg_rx
));
2330 if (mgr
->down_rep_recv
.have_eomt
) {
2331 struct drm_dp_sideband_msg_tx
*txmsg
;
2332 struct drm_dp_mst_branch
*mstb
;
2334 mstb
= drm_dp_get_mst_branch_device(mgr
,
2335 mgr
->down_rep_recv
.initial_hdr
.lct
,
2336 mgr
->down_rep_recv
.initial_hdr
.rad
);
2339 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->down_rep_recv
.initial_hdr
.lct
);
2340 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2344 /* find the message */
2345 slot
= mgr
->down_rep_recv
.initial_hdr
.seqno
;
2346 mutex_lock(&mgr
->qlock
);
2347 txmsg
= mstb
->tx_slots
[slot
];
2348 /* remove from slots */
2349 mutex_unlock(&mgr
->qlock
);
2352 DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2354 mgr
->down_rep_recv
.initial_hdr
.seqno
,
2355 mgr
->down_rep_recv
.initial_hdr
.lct
,
2356 mgr
->down_rep_recv
.initial_hdr
.rad
[0],
2357 mgr
->down_rep_recv
.msg
[0]);
2358 drm_dp_put_mst_branch_device(mstb
);
2359 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2363 drm_dp_sideband_parse_reply(&mgr
->down_rep_recv
, &txmsg
->reply
);
2364 if (txmsg
->reply
.reply_type
== 1) {
2365 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
);
2368 memset(&mgr
->down_rep_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2369 drm_dp_put_mst_branch_device(mstb
);
2371 mutex_lock(&mgr
->qlock
);
2372 txmsg
->state
= DRM_DP_SIDEBAND_TX_RX
;
2373 mstb
->tx_slots
[slot
] = NULL
;
2374 mutex_unlock(&mgr
->qlock
);
2376 wake_up_all(&mgr
->tx_waitq
);
2381 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr
*mgr
)
2385 if (!drm_dp_get_one_sb_msg(mgr
, true)) {
2386 memset(&mgr
->up_req_recv
, 0,
2387 sizeof(struct drm_dp_sideband_msg_rx
));
2391 if (mgr
->up_req_recv
.have_eomt
) {
2392 struct drm_dp_sideband_msg_req_body msg
;
2393 struct drm_dp_mst_branch
*mstb
= NULL
;
2396 if (!mgr
->up_req_recv
.initial_hdr
.broadcast
) {
2397 mstb
= drm_dp_get_mst_branch_device(mgr
,
2398 mgr
->up_req_recv
.initial_hdr
.lct
,
2399 mgr
->up_req_recv
.initial_hdr
.rad
);
2401 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2402 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2407 seqno
= mgr
->up_req_recv
.initial_hdr
.seqno
;
2408 drm_dp_sideband_parse_req(&mgr
->up_req_recv
, &msg
);
2410 if (msg
.req_type
== DP_CONNECTION_STATUS_NOTIFY
) {
2411 drm_dp_send_up_ack_reply(mgr
, mgr
->mst_primary
, msg
.req_type
, seqno
, false);
2414 mstb
= drm_dp_get_mst_branch_device_by_guid(mgr
, msg
.u
.conn_stat
.guid
);
2417 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2418 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2422 drm_dp_update_port(mstb
, &msg
.u
.conn_stat
);
2424 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
);
2425 (*mgr
->cbs
->hotplug
)(mgr
);
2427 } else if (msg
.req_type
== DP_RESOURCE_STATUS_NOTIFY
) {
2428 drm_dp_send_up_ack_reply(mgr
, mgr
->mst_primary
, msg
.req_type
, seqno
, false);
2430 mstb
= drm_dp_get_mst_branch_device_by_guid(mgr
, msg
.u
.resource_stat
.guid
);
2433 DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr
->up_req_recv
.initial_hdr
.lct
);
2434 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2438 DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg
.u
.resource_stat
.port_number
, msg
.u
.resource_stat
.available_pbn
);
2442 drm_dp_put_mst_branch_device(mstb
);
2444 memset(&mgr
->up_req_recv
, 0, sizeof(struct drm_dp_sideband_msg_rx
));
2450 * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2451 * @mgr: manager to notify irq for.
2452 * @esi: 4 bytes from SINK_COUNT_ESI
2453 * @handled: whether the hpd interrupt was consumed or not
2455 * This should be called from the driver when it detects a short IRQ,
2456 * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2457 * topology manager will process the sideband messages received as a result
2460 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr
*mgr
, u8
*esi
, bool *handled
)
2467 if (sc
!= mgr
->sink_count
) {
2468 mgr
->sink_count
= sc
;
2472 if (esi
[1] & DP_DOWN_REP_MSG_RDY
) {
2473 ret
= drm_dp_mst_handle_down_rep(mgr
);
2477 if (esi
[1] & DP_UP_REQ_MSG_RDY
) {
2478 ret
|= drm_dp_mst_handle_up_req(mgr
);
2482 drm_dp_mst_kick_tx(mgr
);
2485 EXPORT_SYMBOL(drm_dp_mst_hpd_irq
);
2488 * drm_dp_mst_detect_port() - get connection status for an MST port
2489 * @connector: DRM connector for this port
2490 * @mgr: manager for this port
2491 * @port: unverified pointer to a port
2493 * This returns the current connection state for a port. It validates the
2494 * port pointer still exists so the caller doesn't require a reference
2496 enum drm_connector_status
drm_dp_mst_detect_port(struct drm_connector
*connector
,
2497 struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2499 enum drm_connector_status status
= connector_status_disconnected
;
2501 /* we need to search for the port in the mgr in case its gone */
2502 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2504 return connector_status_disconnected
;
2509 switch (port
->pdt
) {
2510 case DP_PEER_DEVICE_NONE
:
2511 case DP_PEER_DEVICE_MST_BRANCHING
:
2514 case DP_PEER_DEVICE_SST_SINK
:
2515 status
= connector_status_connected
;
2516 /* for logical ports - cache the EDID */
2517 if (port
->port_num
>= 8 && !port
->cached_edid
) {
2518 port
->cached_edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2521 case DP_PEER_DEVICE_DP_LEGACY_CONV
:
2523 status
= connector_status_connected
;
2527 drm_dp_put_port(port
);
2530 EXPORT_SYMBOL(drm_dp_mst_detect_port
);
2533 * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2534 * @mgr: manager for this port
2535 * @port: unverified pointer to a port.
2537 * This returns whether the port supports audio or not.
2539 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr
*mgr
,
2540 struct drm_dp_mst_port
*port
)
2544 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2547 ret
= port
->has_audio
;
2548 drm_dp_put_port(port
);
2551 EXPORT_SYMBOL(drm_dp_mst_port_has_audio
);
2554 * drm_dp_mst_get_edid() - get EDID for an MST port
2555 * @connector: toplevel connector to get EDID for
2556 * @mgr: manager for this port
2557 * @port: unverified pointer to a port.
2559 * This returns an EDID for the port connected to a connector,
2560 * It validates the pointer still exists so the caller doesn't require a
2563 struct edid
*drm_dp_mst_get_edid(struct drm_connector
*connector
, struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2565 struct edid
*edid
= NULL
;
2567 /* we need to search for the port in the mgr in case its gone */
2568 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2572 if (port
->cached_edid
)
2573 edid
= drm_edid_duplicate(port
->cached_edid
);
2575 edid
= drm_get_edid(connector
, &port
->aux
.ddc
);
2576 drm_connector_set_tile_property(connector
);
2578 port
->has_audio
= drm_detect_monitor_audio(edid
);
2579 drm_dp_put_port(port
);
2582 EXPORT_SYMBOL(drm_dp_mst_get_edid
);
2585 * drm_dp_find_vcpi_slots() - find slots for this PBN value
2586 * @mgr: manager to use
2587 * @pbn: payload bandwidth to convert into slots.
2589 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
,
2594 num_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2596 /* max. time slots - one slot for MTP header */
2601 EXPORT_SYMBOL(drm_dp_find_vcpi_slots
);
2603 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
2604 struct drm_dp_vcpi
*vcpi
, int pbn
, int slots
)
2608 /* max. time slots - one slot for MTP header */
2613 vcpi
->aligned_pbn
= slots
* mgr
->pbn_div
;
2614 vcpi
->num_slots
= slots
;
2616 ret
= drm_dp_mst_assign_payload_id(mgr
, vcpi
);
2623 * drm_dp_atomic_find_vcpi_slots() - Find and add vcpi slots to the state
2624 * @state: global atomic state
2625 * @mgr: MST topology manager for the port
2626 * @port: port to find vcpi slots for
2627 * @pbn: bandwidth required for the mode in PBN
2630 * Total slots in the atomic state assigned for this port or error
2632 int drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state
*state
,
2633 struct drm_dp_mst_topology_mgr
*mgr
,
2634 struct drm_dp_mst_port
*port
, int pbn
)
2636 struct drm_dp_mst_topology_state
*topology_state
;
2639 topology_state
= drm_atomic_get_mst_topology_state(state
, mgr
);
2640 if (IS_ERR(topology_state
))
2641 return PTR_ERR(topology_state
);
2643 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2646 req_slots
= DIV_ROUND_UP(pbn
, mgr
->pbn_div
);
2647 DRM_DEBUG_KMS("vcpi slots req=%d, avail=%d\n",
2648 req_slots
, topology_state
->avail_slots
);
2650 if (req_slots
> topology_state
->avail_slots
) {
2651 drm_dp_put_port(port
);
2655 topology_state
->avail_slots
-= req_slots
;
2656 DRM_DEBUG_KMS("vcpi slots avail=%d", topology_state
->avail_slots
);
2658 drm_dp_put_port(port
);
2661 EXPORT_SYMBOL(drm_dp_atomic_find_vcpi_slots
);
2664 * drm_dp_atomic_release_vcpi_slots() - Release allocated vcpi slots
2665 * @state: global atomic state
2666 * @mgr: MST topology manager for the port
2667 * @slots: number of vcpi slots to release
2670 * 0 if @slots were added back to &drm_dp_mst_topology_state->avail_slots or
2671 * negative error code
2673 int drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state
*state
,
2674 struct drm_dp_mst_topology_mgr
*mgr
,
2677 struct drm_dp_mst_topology_state
*topology_state
;
2679 topology_state
= drm_atomic_get_mst_topology_state(state
, mgr
);
2680 if (IS_ERR(topology_state
))
2681 return PTR_ERR(topology_state
);
2683 /* We cannot rely on port->vcpi.num_slots to update
2684 * topology_state->avail_slots as the port may not exist if the parent
2685 * branch device was unplugged. This should be fixed by tracking
2686 * per-port slot allocation in drm_dp_mst_topology_state instead of
2687 * depending on the caller to tell us how many slots to release.
2689 topology_state
->avail_slots
+= slots
;
2690 DRM_DEBUG_KMS("vcpi slots released=%d, avail=%d\n",
2691 slots
, topology_state
->avail_slots
);
2695 EXPORT_SYMBOL(drm_dp_atomic_release_vcpi_slots
);
2698 * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2699 * @mgr: manager for this port
2700 * @port: port to allocate a virtual channel for.
2701 * @pbn: payload bandwidth number to request
2702 * @slots: returned number of slots for this PBN.
2704 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
,
2705 struct drm_dp_mst_port
*port
, int pbn
, int slots
)
2709 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2716 if (port
->vcpi
.vcpi
> 0) {
2717 DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port
->vcpi
.vcpi
, port
->vcpi
.pbn
, pbn
);
2718 if (pbn
== port
->vcpi
.pbn
) {
2719 drm_dp_put_port(port
);
2724 ret
= drm_dp_init_vcpi(mgr
, &port
->vcpi
, pbn
, slots
);
2726 DRM_DEBUG_KMS("failed to init vcpi slots=%d max=63 ret=%d\n",
2727 DIV_ROUND_UP(pbn
, mgr
->pbn_div
), ret
);
2730 DRM_DEBUG_KMS("initing vcpi for pbn=%d slots=%d\n",
2731 pbn
, port
->vcpi
.num_slots
);
2733 drm_dp_put_port(port
);
2738 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi
);
2740 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2743 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2747 slots
= port
->vcpi
.num_slots
;
2748 drm_dp_put_port(port
);
2751 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots
);
2754 * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2755 * @mgr: manager for this port
2756 * @port: unverified pointer to a port.
2758 * This just resets the number of slots for the ports VCPI for later programming.
2760 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2762 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2765 port
->vcpi
.num_slots
= 0;
2766 drm_dp_put_port(port
);
2768 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots
);
2771 * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2772 * @mgr: manager for this port
2773 * @port: unverified port to deallocate vcpi for
2775 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr
*mgr
, struct drm_dp_mst_port
*port
)
2777 port
= drm_dp_get_validated_port_ref(mgr
, port
);
2781 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
2782 port
->vcpi
.num_slots
= 0;
2784 port
->vcpi
.aligned_pbn
= 0;
2785 port
->vcpi
.vcpi
= 0;
2786 drm_dp_put_port(port
);
2788 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi
);
2790 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr
*mgr
,
2791 int id
, struct drm_dp_payload
*payload
)
2793 u8 payload_alloc
[3], status
;
2797 drm_dp_dpcd_writeb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
,
2798 DP_PAYLOAD_TABLE_UPDATED
);
2800 payload_alloc
[0] = id
;
2801 payload_alloc
[1] = payload
->start_slot
;
2802 payload_alloc
[2] = payload
->num_slots
;
2804 ret
= drm_dp_dpcd_write(mgr
->aux
, DP_PAYLOAD_ALLOCATE_SET
, payload_alloc
, 3);
2806 DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret
);
2811 ret
= drm_dp_dpcd_readb(mgr
->aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2813 DRM_DEBUG_KMS("failed to read payload table status %d\n", ret
);
2817 if (!(status
& DP_PAYLOAD_TABLE_UPDATED
)) {
2820 usleep_range(10000, 20000);
2823 DRM_DEBUG_KMS("status not set after read payload table status %d\n", status
);
2832 static int do_get_act_status(struct drm_dp_aux
*aux
)
2837 ret
= drm_dp_dpcd_readb(aux
, DP_PAYLOAD_TABLE_UPDATE_STATUS
, &status
);
2845 * drm_dp_check_act_status() - Check ACT handled status.
2846 * @mgr: manager to use
2848 * Check the payload status bits in the DPCD for ACT handled completion.
2850 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr
*mgr
)
2853 * There doesn't seem to be any recommended retry count or timeout in
2854 * the MST specification. Since some hubs have been observed to take
2855 * over 1 second to update their payload allocations under certain
2856 * conditions, we use a rather large timeout value.
2858 const int timeout_ms
= 3000;
2861 ret
= readx_poll_timeout(do_get_act_status
, mgr
->aux
, status
,
2862 status
& DP_PAYLOAD_ACT_HANDLED
|| status
< 0,
2863 200, timeout_ms
* USEC_PER_MSEC
);
2864 if (ret
< 0 && status
>= 0) {
2865 DRM_DEBUG_KMS("Failed to get ACT after %dms, last status: %02x\n",
2866 timeout_ms
, status
);
2868 } else if (status
< 0) {
2869 DRM_DEBUG_KMS("Failed to read payload table status: %d\n",
2876 EXPORT_SYMBOL(drm_dp_check_act_status
);
2879 * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2880 * @clock: dot clock for the mode
2881 * @bpp: bpp for the mode.
2883 * This uses the formula in the spec to calculate the PBN value for a mode.
2885 int drm_dp_calc_pbn_mode(int clock
, int bpp
)
2895 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2896 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2897 * common multiplier to render an integer PBN for all link rate/lane
2898 * counts combinations
2900 * peak_kbps *= (1006/1000)
2901 * peak_kbps *= (64/54)
2902 * peak_kbps *= 8 convert to bytes
2905 numerator
= 64 * 1006;
2906 denominator
= 54 * 8 * 1000 * 1000;
2909 peak_kbps
= drm_fixp_from_fraction(kbps
, denominator
);
2911 return drm_fixp2int_ceil(peak_kbps
);
2913 EXPORT_SYMBOL(drm_dp_calc_pbn_mode
);
2915 static int test_calc_pbn_mode(void)
2918 ret
= drm_dp_calc_pbn_mode(154000, 30);
2920 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2921 154000, 30, 689, ret
);
2924 ret
= drm_dp_calc_pbn_mode(234000, 30);
2926 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2927 234000, 30, 1047, ret
);
2930 ret
= drm_dp_calc_pbn_mode(297000, 24);
2932 DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2933 297000, 24, 1063, ret
);
2939 /* we want to kick the TX after we've ack the up/down IRQs. */
2940 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr
*mgr
)
2942 queue_work(system_long_wq
, &mgr
->tx_work
);
2945 static void drm_dp_mst_dump_mstb(struct seq_file
*m
,
2946 struct drm_dp_mst_branch
*mstb
)
2948 struct drm_dp_mst_port
*port
;
2949 int tabs
= mstb
->lct
;
2953 for (i
= 0; i
< tabs
; i
++)
2957 seq_printf(m
, "%smst: %p, %d\n", prefix
, mstb
, mstb
->num_ports
);
2958 list_for_each_entry(port
, &mstb
->ports
, next
) {
2959 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
);
2961 drm_dp_mst_dump_mstb(m
, port
->mstb
);
2965 #define DP_PAYLOAD_TABLE_SIZE 64
2967 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr
*mgr
,
2972 for (i
= 0; i
< DP_PAYLOAD_TABLE_SIZE
; i
+= 16) {
2973 if (drm_dp_dpcd_read(mgr
->aux
,
2974 DP_PAYLOAD_TABLE_UPDATE_STATUS
+ i
,
2981 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr
*mgr
,
2982 struct drm_dp_mst_port
*port
, char *name
,
2985 struct edid
*mst_edid
;
2987 mst_edid
= drm_dp_mst_get_edid(port
->connector
, mgr
, port
);
2988 drm_edid_get_monitor_name(mst_edid
, name
, namelen
);
2992 * drm_dp_mst_dump_topology(): dump topology to seq file.
2993 * @m: seq_file to dump output to
2994 * @mgr: manager to dump current topology for.
2996 * helper to dump MST topology to a seq file for debugfs.
2998 void drm_dp_mst_dump_topology(struct seq_file
*m
,
2999 struct drm_dp_mst_topology_mgr
*mgr
)
3002 struct drm_dp_mst_port
*port
;
3004 mutex_lock(&mgr
->lock
);
3005 if (mgr
->mst_primary
)
3006 drm_dp_mst_dump_mstb(m
, mgr
->mst_primary
);
3009 mutex_unlock(&mgr
->lock
);
3011 mutex_lock(&mgr
->payload_lock
);
3012 seq_printf(m
, "vcpi: %lx %lx %d\n", mgr
->payload_mask
, mgr
->vcpi_mask
,
3015 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
3016 if (mgr
->proposed_vcpis
[i
]) {
3019 port
= container_of(mgr
->proposed_vcpis
[i
], struct drm_dp_mst_port
, vcpi
);
3020 fetch_monitor_name(mgr
, port
, name
, sizeof(name
));
3021 seq_printf(m
, "vcpi %d: %d %d %d sink name: %s\n", i
,
3022 port
->port_num
, port
->vcpi
.vcpi
,
3023 port
->vcpi
.num_slots
,
3024 (*name
!= 0) ? name
: "Unknown");
3026 seq_printf(m
, "vcpi %d:unused\n", i
);
3028 for (i
= 0; i
< mgr
->max_payloads
; i
++) {
3029 seq_printf(m
, "payload %d: %d, %d, %d\n",
3031 mgr
->payloads
[i
].payload_state
,
3032 mgr
->payloads
[i
].start_slot
,
3033 mgr
->payloads
[i
].num_slots
);
3037 mutex_unlock(&mgr
->payload_lock
);
3039 mutex_lock(&mgr
->lock
);
3040 if (mgr
->mst_primary
) {
3041 u8 buf
[DP_PAYLOAD_TABLE_SIZE
];
3044 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_DPCD_REV
, buf
, DP_RECEIVER_CAP_SIZE
);
3045 seq_printf(m
, "dpcd: %*ph\n", DP_RECEIVER_CAP_SIZE
, buf
);
3046 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_FAUX_CAP
, buf
, 2);
3047 seq_printf(m
, "faux/mst: %*ph\n", 2, buf
);
3048 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_MSTM_CTRL
, buf
, 1);
3049 seq_printf(m
, "mst ctrl: %*ph\n", 1, buf
);
3051 /* dump the standard OUI branch header */
3052 ret
= drm_dp_dpcd_read(mgr
->aux
, DP_BRANCH_OUI
, buf
, DP_BRANCH_OUI_HEADER_SIZE
);
3053 seq_printf(m
, "branch oui: %*phN devid: ", 3, buf
);
3054 for (i
= 0x3; i
< 0x8 && buf
[i
]; i
++)
3055 seq_printf(m
, "%c", buf
[i
]);
3056 seq_printf(m
, " revision: hw: %x.%x sw: %x.%x\n",
3057 buf
[0x9] >> 4, buf
[0x9] & 0xf, buf
[0xa], buf
[0xb]);
3058 if (dump_dp_payload_table(mgr
, buf
))
3059 seq_printf(m
, "payload table: %*ph\n", DP_PAYLOAD_TABLE_SIZE
, buf
);
3062 mutex_unlock(&mgr
->lock
);
3065 EXPORT_SYMBOL(drm_dp_mst_dump_topology
);
3067 static void drm_dp_tx_work(struct work_struct
*work
)
3069 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, tx_work
);
3071 mutex_lock(&mgr
->qlock
);
3072 if (!list_empty(&mgr
->tx_msg_downq
))
3073 process_single_down_tx_qlock(mgr
);
3074 mutex_unlock(&mgr
->qlock
);
3077 static void drm_dp_free_mst_port(struct kref
*kref
)
3079 struct drm_dp_mst_port
*port
= container_of(kref
, struct drm_dp_mst_port
, kref
);
3080 kref_put(&port
->parent
->kref
, drm_dp_free_mst_branch_device
);
3084 static void drm_dp_destroy_connector_work(struct work_struct
*work
)
3086 struct drm_dp_mst_topology_mgr
*mgr
= container_of(work
, struct drm_dp_mst_topology_mgr
, destroy_connector_work
);
3087 struct drm_dp_mst_port
*port
;
3088 bool send_hotplug
= false;
3090 * Not a regular list traverse as we have to drop the destroy
3091 * connector lock before destroying the connector, to avoid AB->BA
3092 * ordering between this lock and the config mutex.
3095 mutex_lock(&mgr
->destroy_connector_lock
);
3096 port
= list_first_entry_or_null(&mgr
->destroy_connector_list
, struct drm_dp_mst_port
, next
);
3098 mutex_unlock(&mgr
->destroy_connector_lock
);
3101 list_del(&port
->next
);
3102 mutex_unlock(&mgr
->destroy_connector_lock
);
3104 kref_init(&port
->kref
);
3105 INIT_LIST_HEAD(&port
->next
);
3107 mgr
->cbs
->destroy_connector(mgr
, port
->connector
);
3109 drm_dp_port_teardown_pdt(port
, port
->pdt
);
3110 port
->pdt
= DP_PEER_DEVICE_NONE
;
3112 if (!port
->input
&& port
->vcpi
.vcpi
> 0) {
3113 drm_dp_mst_reset_vcpi_slots(mgr
, port
);
3114 drm_dp_update_payload_part1(mgr
);
3115 drm_dp_mst_put_payload_id(mgr
, port
->vcpi
.vcpi
);
3118 kref_put(&port
->kref
, drm_dp_free_mst_port
);
3119 send_hotplug
= true;
3122 (*mgr
->cbs
->hotplug
)(mgr
);
3125 static struct drm_private_state
*
3126 drm_dp_mst_duplicate_state(struct drm_private_obj
*obj
)
3128 struct drm_dp_mst_topology_state
*state
;
3130 state
= kmemdup(obj
->state
, sizeof(*state
), GFP_KERNEL
);
3134 __drm_atomic_helper_private_obj_duplicate_state(obj
, &state
->base
);
3136 return &state
->base
;
3139 static void drm_dp_mst_destroy_state(struct drm_private_obj
*obj
,
3140 struct drm_private_state
*state
)
3142 struct drm_dp_mst_topology_state
*mst_state
=
3143 to_dp_mst_topology_state(state
);
3148 static const struct drm_private_state_funcs mst_state_funcs
= {
3149 .atomic_duplicate_state
= drm_dp_mst_duplicate_state
,
3150 .atomic_destroy_state
= drm_dp_mst_destroy_state
,
3154 * drm_atomic_get_mst_topology_state: get MST topology state
3156 * @state: global atomic state
3157 * @mgr: MST topology manager, also the private object in this case
3159 * This function wraps drm_atomic_get_priv_obj_state() passing in the MST atomic
3160 * state vtable so that the private object state returned is that of a MST
3161 * topology object. Also, drm_atomic_get_private_obj_state() expects the caller
3162 * to care of the locking, so warn if don't hold the connection_mutex.
3166 * The MST topology state or error pointer.
3168 struct drm_dp_mst_topology_state
*drm_atomic_get_mst_topology_state(struct drm_atomic_state
*state
,
3169 struct drm_dp_mst_topology_mgr
*mgr
)
3171 struct drm_device
*dev
= mgr
->dev
;
3173 WARN_ON(!drm_modeset_is_locked(&dev
->mode_config
.connection_mutex
));
3174 return to_dp_mst_topology_state(drm_atomic_get_private_obj_state(state
, &mgr
->base
));
3176 EXPORT_SYMBOL(drm_atomic_get_mst_topology_state
);
3179 * drm_dp_mst_topology_mgr_init - initialise a topology manager
3180 * @mgr: manager struct to initialise
3181 * @dev: device providing this structure - for i2c addition.
3182 * @aux: DP helper aux channel to talk to this device
3183 * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
3184 * @max_payloads: maximum number of payloads this GPU can source
3185 * @conn_base_id: the connector object ID the MST device is connected to.
3187 * Return 0 for success, or negative error code on failure
3189 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr
*mgr
,
3190 struct drm_device
*dev
, struct drm_dp_aux
*aux
,
3191 int max_dpcd_transaction_bytes
,
3192 int max_payloads
, int conn_base_id
)
3194 struct drm_dp_mst_topology_state
*mst_state
;
3196 mutex_init(&mgr
->lock
);
3197 mutex_init(&mgr
->qlock
);
3198 mutex_init(&mgr
->payload_lock
);
3199 mutex_init(&mgr
->destroy_connector_lock
);
3200 INIT_LIST_HEAD(&mgr
->tx_msg_downq
);
3201 INIT_LIST_HEAD(&mgr
->destroy_connector_list
);
3202 INIT_WORK(&mgr
->work
, drm_dp_mst_link_probe_work
);
3203 INIT_WORK(&mgr
->tx_work
, drm_dp_tx_work
);
3204 INIT_WORK(&mgr
->destroy_connector_work
, drm_dp_destroy_connector_work
);
3205 init_waitqueue_head(&mgr
->tx_waitq
);
3208 mgr
->max_dpcd_transaction_bytes
= max_dpcd_transaction_bytes
;
3209 mgr
->max_payloads
= max_payloads
;
3210 mgr
->conn_base_id
= conn_base_id
;
3211 if (max_payloads
+ 1 > sizeof(mgr
->payload_mask
) * 8 ||
3212 max_payloads
+ 1 > sizeof(mgr
->vcpi_mask
) * 8)
3214 mgr
->payloads
= kcalloc(max_payloads
, sizeof(struct drm_dp_payload
), GFP_KERNEL
);
3217 mgr
->proposed_vcpis
= kcalloc(max_payloads
, sizeof(struct drm_dp_vcpi
*), GFP_KERNEL
);
3218 if (!mgr
->proposed_vcpis
)
3220 set_bit(0, &mgr
->payload_mask
);
3221 if (test_calc_pbn_mode() < 0)
3222 DRM_ERROR("MST PBN self-test failed\n");
3224 mst_state
= kzalloc(sizeof(*mst_state
), GFP_KERNEL
);
3225 if (mst_state
== NULL
)
3228 mst_state
->mgr
= mgr
;
3230 /* max. time slots - one slot for MTP header */
3231 mst_state
->avail_slots
= 63;
3233 drm_atomic_private_obj_init(&mgr
->base
,
3239 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init
);
3242 * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3243 * @mgr: manager to destroy
3245 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr
*mgr
)
3247 flush_work(&mgr
->work
);
3248 flush_work(&mgr
->destroy_connector_work
);
3249 mutex_lock(&mgr
->payload_lock
);
3250 kfree(mgr
->payloads
);
3251 mgr
->payloads
= NULL
;
3252 kfree(mgr
->proposed_vcpis
);
3253 mgr
->proposed_vcpis
= NULL
;
3254 mutex_unlock(&mgr
->payload_lock
);
3257 drm_atomic_private_obj_fini(&mgr
->base
);
3260 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy
);
3263 static int drm_dp_mst_i2c_xfer(struct i2c_adapter
*adapter
, struct i2c_msg
*msgs
,
3266 struct drm_dp_aux
*aux
= adapter
->algo_data
;
3267 struct drm_dp_mst_port
*port
= container_of(aux
, struct drm_dp_mst_port
, aux
);
3268 struct drm_dp_mst_branch
*mstb
;
3269 struct drm_dp_mst_topology_mgr
*mgr
= port
->mgr
;
3271 bool reading
= false;
3272 struct drm_dp_sideband_msg_req_body msg
;
3273 struct drm_dp_sideband_msg_tx
*txmsg
= NULL
;
3276 mstb
= drm_dp_get_validated_mstb_ref(mgr
, port
->parent
);
3280 /* construct i2c msg */
3281 /* see if last msg is a read */
3282 if (msgs
[num
- 1].flags
& I2C_M_RD
)
3285 if (!reading
|| (num
- 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS
)) {
3286 DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3291 memset(&msg
, 0, sizeof(msg
));
3292 msg
.req_type
= DP_REMOTE_I2C_READ
;
3293 msg
.u
.i2c_read
.num_transactions
= num
- 1;
3294 msg
.u
.i2c_read
.port_number
= port
->port_num
;
3295 for (i
= 0; i
< num
- 1; i
++) {
3296 msg
.u
.i2c_read
.transactions
[i
].i2c_dev_id
= msgs
[i
].addr
;
3297 msg
.u
.i2c_read
.transactions
[i
].num_bytes
= msgs
[i
].len
;
3298 msg
.u
.i2c_read
.transactions
[i
].bytes
= msgs
[i
].buf
;
3299 msg
.u
.i2c_read
.transactions
[i
].no_stop_bit
= !(msgs
[i
].flags
& I2C_M_STOP
);
3301 msg
.u
.i2c_read
.read_i2c_device_id
= msgs
[num
- 1].addr
;
3302 msg
.u
.i2c_read
.num_bytes_read
= msgs
[num
- 1].len
;
3304 txmsg
= kzalloc(sizeof(*txmsg
), GFP_KERNEL
);
3311 drm_dp_encode_sideband_req(&msg
, txmsg
);
3313 drm_dp_queue_down_tx(mgr
, txmsg
);
3315 ret
= drm_dp_mst_wait_tx_reply(mstb
, txmsg
);
3318 if (txmsg
->reply
.reply_type
== 1) { /* got a NAK back */
3322 if (txmsg
->reply
.u
.remote_i2c_read_ack
.num_bytes
!= msgs
[num
- 1].len
) {
3326 memcpy(msgs
[num
- 1].buf
, txmsg
->reply
.u
.remote_i2c_read_ack
.bytes
, msgs
[num
- 1].len
);
3331 drm_dp_put_mst_branch_device(mstb
);
3335 static u32
drm_dp_mst_i2c_functionality(struct i2c_adapter
*adapter
)
3337 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
3338 I2C_FUNC_SMBUS_READ_BLOCK_DATA
|
3339 I2C_FUNC_SMBUS_BLOCK_PROC_CALL
|
3340 I2C_FUNC_10BIT_ADDR
;
3343 static const struct i2c_algorithm drm_dp_mst_i2c_algo
= {
3344 .functionality
= drm_dp_mst_i2c_functionality
,
3345 .master_xfer
= drm_dp_mst_i2c_xfer
,
3349 * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3350 * @aux: DisplayPort AUX channel
3352 * Returns 0 on success or a negative error code on failure.
3354 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux
*aux
)
3356 aux
->ddc
.algo
= &drm_dp_mst_i2c_algo
;
3357 aux
->ddc
.algo_data
= aux
;
3358 aux
->ddc
.retries
= 3;
3360 aux
->ddc
.class = I2C_CLASS_DDC
;
3361 aux
->ddc
.owner
= THIS_MODULE
;
3362 aux
->ddc
.dev
.parent
= aux
->dev
;
3363 aux
->ddc
.dev
.of_node
= aux
->dev
->of_node
;
3365 strlcpy(aux
->ddc
.name
, aux
->name
? aux
->name
: dev_name(aux
->dev
),
3366 sizeof(aux
->ddc
.name
));
3368 return i2c_add_adapter(&aux
->ddc
);
3372 * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3373 * @aux: DisplayPort AUX channel
3375 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux
*aux
)
3377 i2c_del_adapter(&aux
->ddc
);