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