• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
28 #include <linux/seq_file.h>
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>
34 
35 /**
36  * DOC: dp mst helper
37  *
38  * These functions contain parts of the DisplayPort 1.2a MultiStream Transport
39  * protocol. The helpers contain a topology manager and bandwidth manager.
40  * The helpers encapsulate the sending and received of sideband msgs.
41  */
42 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
43 				  char *buf);
44 static int test_calc_pbn_mode(void);
45 
46 static void drm_dp_put_port(struct drm_dp_mst_port *port);
47 
48 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
49 				     int id,
50 				     struct drm_dp_payload *payload);
51 
52 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
53 				  struct drm_dp_mst_port *port,
54 				  int offset, int size, u8 *bytes);
55 
56 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
57 				     struct drm_dp_mst_branch *mstb);
58 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
59 					   struct drm_dp_mst_branch *mstb,
60 					   struct drm_dp_mst_port *port);
61 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
62 				 u8 *guid);
63 
64 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux);
65 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux);
66 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr);
67 /* sideband msg handling */
drm_dp_msg_header_crc4(const uint8_t * data,size_t num_nibbles)68 static u8 drm_dp_msg_header_crc4(const uint8_t *data, size_t num_nibbles)
69 {
70 	u8 bitmask = 0x80;
71 	u8 bitshift = 7;
72 	u8 array_index = 0;
73 	int number_of_bits = num_nibbles * 4;
74 	u8 remainder = 0;
75 
76 	while (number_of_bits != 0) {
77 		number_of_bits--;
78 		remainder <<= 1;
79 		remainder |= (data[array_index] & bitmask) >> bitshift;
80 		bitmask >>= 1;
81 		bitshift--;
82 		if (bitmask == 0) {
83 			bitmask = 0x80;
84 			bitshift = 7;
85 			array_index++;
86 		}
87 		if ((remainder & 0x10) == 0x10)
88 			remainder ^= 0x13;
89 	}
90 
91 	number_of_bits = 4;
92 	while (number_of_bits != 0) {
93 		number_of_bits--;
94 		remainder <<= 1;
95 		if ((remainder & 0x10) != 0)
96 			remainder ^= 0x13;
97 	}
98 
99 	return remainder;
100 }
101 
drm_dp_msg_data_crc4(const uint8_t * data,u8 number_of_bytes)102 static u8 drm_dp_msg_data_crc4(const uint8_t *data, u8 number_of_bytes)
103 {
104 	u8 bitmask = 0x80;
105 	u8 bitshift = 7;
106 	u8 array_index = 0;
107 	int number_of_bits = number_of_bytes * 8;
108 	u16 remainder = 0;
109 
110 	while (number_of_bits != 0) {
111 		number_of_bits--;
112 		remainder <<= 1;
113 		remainder |= (data[array_index] & bitmask) >> bitshift;
114 		bitmask >>= 1;
115 		bitshift--;
116 		if (bitmask == 0) {
117 			bitmask = 0x80;
118 			bitshift = 7;
119 			array_index++;
120 		}
121 		if ((remainder & 0x100) == 0x100)
122 			remainder ^= 0xd5;
123 	}
124 
125 	number_of_bits = 8;
126 	while (number_of_bits != 0) {
127 		number_of_bits--;
128 		remainder <<= 1;
129 		if ((remainder & 0x100) != 0)
130 			remainder ^= 0xd5;
131 	}
132 
133 	return remainder & 0xff;
134 }
drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr * hdr)135 static inline u8 drm_dp_calc_sb_hdr_size(struct drm_dp_sideband_msg_hdr *hdr)
136 {
137 	u8 size = 3;
138 	size += (hdr->lct / 2);
139 	return size;
140 }
141 
drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr * hdr,u8 * buf,int * len)142 static void drm_dp_encode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
143 					   u8 *buf, int *len)
144 {
145 	int idx = 0;
146 	int i;
147 	u8 crc4;
148 	buf[idx++] = ((hdr->lct & 0xf) << 4) | (hdr->lcr & 0xf);
149 	for (i = 0; i < (hdr->lct / 2); i++)
150 		buf[idx++] = hdr->rad[i];
151 	buf[idx++] = (hdr->broadcast << 7) | (hdr->path_msg << 6) |
152 		(hdr->msg_len & 0x3f);
153 	buf[idx++] = (hdr->somt << 7) | (hdr->eomt << 6) | (hdr->seqno << 4);
154 
155 	crc4 = drm_dp_msg_header_crc4(buf, (idx * 2) - 1);
156 	buf[idx - 1] |= (crc4 & 0xf);
157 
158 	*len = idx;
159 }
160 
drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr * hdr,u8 * buf,int buflen,u8 * hdrlen)161 static bool drm_dp_decode_sideband_msg_hdr(struct drm_dp_sideband_msg_hdr *hdr,
162 					   u8 *buf, int buflen, u8 *hdrlen)
163 {
164 	u8 crc4;
165 	u8 len;
166 	int i;
167 	u8 idx;
168 	if (buf[0] == 0)
169 		return false;
170 	len = 3;
171 	len += ((buf[0] & 0xf0) >> 4) / 2;
172 	if (len > buflen)
173 		return false;
174 	crc4 = drm_dp_msg_header_crc4(buf, (len * 2) - 1);
175 
176 	if ((crc4 & 0xf) != (buf[len - 1] & 0xf)) {
177 		DRM_DEBUG_KMS("crc4 mismatch 0x%x 0x%x\n", crc4, buf[len - 1]);
178 		return false;
179 	}
180 
181 	hdr->lct = (buf[0] & 0xf0) >> 4;
182 	hdr->lcr = (buf[0] & 0xf);
183 	idx = 1;
184 	for (i = 0; i < (hdr->lct / 2); i++)
185 		hdr->rad[i] = buf[idx++];
186 	hdr->broadcast = (buf[idx] >> 7) & 0x1;
187 	hdr->path_msg = (buf[idx] >> 6) & 0x1;
188 	hdr->msg_len = buf[idx] & 0x3f;
189 	idx++;
190 	hdr->somt = (buf[idx] >> 7) & 0x1;
191 	hdr->eomt = (buf[idx] >> 6) & 0x1;
192 	hdr->seqno = (buf[idx] >> 4) & 0x1;
193 	idx++;
194 	*hdrlen = idx;
195 	return true;
196 }
197 
drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body * req,struct drm_dp_sideband_msg_tx * raw)198 static void drm_dp_encode_sideband_req(struct drm_dp_sideband_msg_req_body *req,
199 				       struct drm_dp_sideband_msg_tx *raw)
200 {
201 	int idx = 0;
202 	int i;
203 	u8 *buf = raw->msg;
204 	buf[idx++] = req->req_type & 0x7f;
205 
206 	switch (req->req_type) {
207 	case DP_ENUM_PATH_RESOURCES:
208 		buf[idx] = (req->u.port_num.port_number & 0xf) << 4;
209 		idx++;
210 		break;
211 	case DP_ALLOCATE_PAYLOAD:
212 		buf[idx] = (req->u.allocate_payload.port_number & 0xf) << 4 |
213 			(req->u.allocate_payload.number_sdp_streams & 0xf);
214 		idx++;
215 		buf[idx] = (req->u.allocate_payload.vcpi & 0x7f);
216 		idx++;
217 		buf[idx] = (req->u.allocate_payload.pbn >> 8);
218 		idx++;
219 		buf[idx] = (req->u.allocate_payload.pbn & 0xff);
220 		idx++;
221 		for (i = 0; i < req->u.allocate_payload.number_sdp_streams / 2; i++) {
222 			buf[idx] = ((req->u.allocate_payload.sdp_stream_sink[i * 2] & 0xf) << 4) |
223 				(req->u.allocate_payload.sdp_stream_sink[i * 2 + 1] & 0xf);
224 			idx++;
225 		}
226 		if (req->u.allocate_payload.number_sdp_streams & 1) {
227 			i = req->u.allocate_payload.number_sdp_streams - 1;
228 			buf[idx] = (req->u.allocate_payload.sdp_stream_sink[i] & 0xf) << 4;
229 			idx++;
230 		}
231 		break;
232 	case DP_QUERY_PAYLOAD:
233 		buf[idx] = (req->u.query_payload.port_number & 0xf) << 4;
234 		idx++;
235 		buf[idx] = (req->u.query_payload.vcpi & 0x7f);
236 		idx++;
237 		break;
238 	case DP_REMOTE_DPCD_READ:
239 		buf[idx] = (req->u.dpcd_read.port_number & 0xf) << 4;
240 		buf[idx] |= ((req->u.dpcd_read.dpcd_address & 0xf0000) >> 16) & 0xf;
241 		idx++;
242 		buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff00) >> 8;
243 		idx++;
244 		buf[idx] = (req->u.dpcd_read.dpcd_address & 0xff);
245 		idx++;
246 		buf[idx] = (req->u.dpcd_read.num_bytes);
247 		idx++;
248 		break;
249 
250 	case DP_REMOTE_DPCD_WRITE:
251 		buf[idx] = (req->u.dpcd_write.port_number & 0xf) << 4;
252 		buf[idx] |= ((req->u.dpcd_write.dpcd_address & 0xf0000) >> 16) & 0xf;
253 		idx++;
254 		buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff00) >> 8;
255 		idx++;
256 		buf[idx] = (req->u.dpcd_write.dpcd_address & 0xff);
257 		idx++;
258 		buf[idx] = (req->u.dpcd_write.num_bytes);
259 		idx++;
260 		memcpy(&buf[idx], req->u.dpcd_write.bytes, req->u.dpcd_write.num_bytes);
261 		idx += req->u.dpcd_write.num_bytes;
262 		break;
263 	case DP_REMOTE_I2C_READ:
264 		buf[idx] = (req->u.i2c_read.port_number & 0xf) << 4;
265 		buf[idx] |= (req->u.i2c_read.num_transactions & 0x3);
266 		idx++;
267 		for (i = 0; i < (req->u.i2c_read.num_transactions & 0x3); i++) {
268 			buf[idx] = req->u.i2c_read.transactions[i].i2c_dev_id & 0x7f;
269 			idx++;
270 			buf[idx] = req->u.i2c_read.transactions[i].num_bytes;
271 			idx++;
272 			memcpy(&buf[idx], req->u.i2c_read.transactions[i].bytes, req->u.i2c_read.transactions[i].num_bytes);
273 			idx += req->u.i2c_read.transactions[i].num_bytes;
274 
275 			buf[idx] = (req->u.i2c_read.transactions[i].no_stop_bit & 0x1) << 5;
276 			buf[idx] |= (req->u.i2c_read.transactions[i].i2c_transaction_delay & 0xf);
277 			idx++;
278 		}
279 		buf[idx] = (req->u.i2c_read.read_i2c_device_id) & 0x7f;
280 		idx++;
281 		buf[idx] = (req->u.i2c_read.num_bytes_read);
282 		idx++;
283 		break;
284 
285 	case DP_REMOTE_I2C_WRITE:
286 		buf[idx] = (req->u.i2c_write.port_number & 0xf) << 4;
287 		idx++;
288 		buf[idx] = (req->u.i2c_write.write_i2c_device_id) & 0x7f;
289 		idx++;
290 		buf[idx] = (req->u.i2c_write.num_bytes);
291 		idx++;
292 		memcpy(&buf[idx], req->u.i2c_write.bytes, req->u.i2c_write.num_bytes);
293 		idx += req->u.i2c_write.num_bytes;
294 		break;
295 	}
296 	raw->cur_len = idx;
297 }
298 
drm_dp_crc_sideband_chunk_req(u8 * msg,u8 len)299 static void drm_dp_crc_sideband_chunk_req(u8 *msg, u8 len)
300 {
301 	u8 crc4;
302 	crc4 = drm_dp_msg_data_crc4(msg, len);
303 	msg[len] = crc4;
304 }
305 
drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body * rep,struct drm_dp_sideband_msg_tx * raw)306 static void drm_dp_encode_sideband_reply(struct drm_dp_sideband_msg_reply_body *rep,
307 					 struct drm_dp_sideband_msg_tx *raw)
308 {
309 	int idx = 0;
310 	u8 *buf = raw->msg;
311 
312 	buf[idx++] = (rep->reply_type & 0x1) << 7 | (rep->req_type & 0x7f);
313 
314 	raw->cur_len = idx;
315 }
316 
317 /* this adds a chunk of msg to the builder to get the final msg */
drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx * msg,u8 * replybuf,u8 replybuflen,bool hdr)318 static bool drm_dp_sideband_msg_build(struct drm_dp_sideband_msg_rx *msg,
319 				      u8 *replybuf, u8 replybuflen, bool hdr)
320 {
321 	int ret;
322 	u8 crc4;
323 
324 	if (hdr) {
325 		u8 hdrlen;
326 		struct drm_dp_sideband_msg_hdr recv_hdr;
327 		ret = drm_dp_decode_sideband_msg_hdr(&recv_hdr, replybuf, replybuflen, &hdrlen);
328 		if (ret == false) {
329 			print_hex_dump(KERN_DEBUG, "failed hdr", DUMP_PREFIX_NONE, 16, 1, replybuf, replybuflen, false);
330 			return false;
331 		}
332 
333 		/*
334 		 * ignore out-of-order messages or messages that are part of a
335 		 * failed transaction
336 		 */
337 		if (!recv_hdr.somt && !msg->have_somt)
338 			return false;
339 
340 		/* get length contained in this portion */
341 		msg->curchunk_len = recv_hdr.msg_len;
342 		msg->curchunk_hdrlen = hdrlen;
343 
344 		/* we have already gotten an somt - don't bother parsing */
345 		if (recv_hdr.somt && msg->have_somt)
346 			return false;
347 
348 		if (recv_hdr.somt) {
349 			memcpy(&msg->initial_hdr, &recv_hdr, sizeof(struct drm_dp_sideband_msg_hdr));
350 			msg->have_somt = true;
351 		}
352 		if (recv_hdr.eomt)
353 			msg->have_eomt = true;
354 
355 		/* copy the bytes for the remainder of this header chunk */
356 		msg->curchunk_idx = min(msg->curchunk_len, (u8)(replybuflen - hdrlen));
357 		memcpy(&msg->chunk[0], replybuf + hdrlen, msg->curchunk_idx);
358 	} else {
359 		memcpy(&msg->chunk[msg->curchunk_idx], replybuf, replybuflen);
360 		msg->curchunk_idx += replybuflen;
361 	}
362 
363 	if (msg->curchunk_idx >= msg->curchunk_len) {
364 		/* do CRC */
365 		crc4 = drm_dp_msg_data_crc4(msg->chunk, msg->curchunk_len - 1);
366 		/* copy chunk into bigger msg */
367 		memcpy(&msg->msg[msg->curlen], msg->chunk, msg->curchunk_len - 1);
368 		msg->curlen += msg->curchunk_len - 1;
369 	}
370 	return true;
371 }
372 
drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * repmsg)373 static bool drm_dp_sideband_parse_link_address(struct drm_dp_sideband_msg_rx *raw,
374 					       struct drm_dp_sideband_msg_reply_body *repmsg)
375 {
376 	int idx = 1;
377 	int i;
378 	memcpy(repmsg->u.link_addr.guid, &raw->msg[idx], 16);
379 	idx += 16;
380 	repmsg->u.link_addr.nports = raw->msg[idx] & 0xf;
381 	idx++;
382 	if (idx > raw->curlen)
383 		goto fail_len;
384 	for (i = 0; i < repmsg->u.link_addr.nports; i++) {
385 		if (raw->msg[idx] & 0x80)
386 			repmsg->u.link_addr.ports[i].input_port = 1;
387 
388 		repmsg->u.link_addr.ports[i].peer_device_type = (raw->msg[idx] >> 4) & 0x7;
389 		repmsg->u.link_addr.ports[i].port_number = (raw->msg[idx] & 0xf);
390 
391 		idx++;
392 		if (idx > raw->curlen)
393 			goto fail_len;
394 		repmsg->u.link_addr.ports[i].mcs = (raw->msg[idx] >> 7) & 0x1;
395 		repmsg->u.link_addr.ports[i].ddps = (raw->msg[idx] >> 6) & 0x1;
396 		if (repmsg->u.link_addr.ports[i].input_port == 0)
397 			repmsg->u.link_addr.ports[i].legacy_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
398 		idx++;
399 		if (idx > raw->curlen)
400 			goto fail_len;
401 		if (repmsg->u.link_addr.ports[i].input_port == 0) {
402 			repmsg->u.link_addr.ports[i].dpcd_revision = (raw->msg[idx]);
403 			idx++;
404 			if (idx > raw->curlen)
405 				goto fail_len;
406 			memcpy(repmsg->u.link_addr.ports[i].peer_guid, &raw->msg[idx], 16);
407 			idx += 16;
408 			if (idx > raw->curlen)
409 				goto fail_len;
410 			repmsg->u.link_addr.ports[i].num_sdp_streams = (raw->msg[idx] >> 4) & 0xf;
411 			repmsg->u.link_addr.ports[i].num_sdp_stream_sinks = (raw->msg[idx] & 0xf);
412 			idx++;
413 
414 		}
415 		if (idx > raw->curlen)
416 			goto fail_len;
417 	}
418 
419 	return true;
420 fail_len:
421 	DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
422 	return false;
423 }
424 
drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * repmsg)425 static bool drm_dp_sideband_parse_remote_dpcd_read(struct drm_dp_sideband_msg_rx *raw,
426 						   struct drm_dp_sideband_msg_reply_body *repmsg)
427 {
428 	int idx = 1;
429 	repmsg->u.remote_dpcd_read_ack.port_number = raw->msg[idx] & 0xf;
430 	idx++;
431 	if (idx > raw->curlen)
432 		goto fail_len;
433 	repmsg->u.remote_dpcd_read_ack.num_bytes = raw->msg[idx];
434 	if (idx > raw->curlen)
435 		goto fail_len;
436 
437 	memcpy(repmsg->u.remote_dpcd_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_dpcd_read_ack.num_bytes);
438 	return true;
439 fail_len:
440 	DRM_DEBUG_KMS("link address reply parse length fail %d %d\n", idx, raw->curlen);
441 	return false;
442 }
443 
drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * repmsg)444 static bool drm_dp_sideband_parse_remote_dpcd_write(struct drm_dp_sideband_msg_rx *raw,
445 						      struct drm_dp_sideband_msg_reply_body *repmsg)
446 {
447 	int idx = 1;
448 	repmsg->u.remote_dpcd_write_ack.port_number = raw->msg[idx] & 0xf;
449 	idx++;
450 	if (idx > raw->curlen)
451 		goto fail_len;
452 	return true;
453 fail_len:
454 	DRM_DEBUG_KMS("parse length fail %d %d\n", idx, raw->curlen);
455 	return false;
456 }
457 
drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * repmsg)458 static bool drm_dp_sideband_parse_remote_i2c_read_ack(struct drm_dp_sideband_msg_rx *raw,
459 						      struct drm_dp_sideband_msg_reply_body *repmsg)
460 {
461 	int idx = 1;
462 
463 	repmsg->u.remote_i2c_read_ack.port_number = (raw->msg[idx] & 0xf);
464 	idx++;
465 	if (idx > raw->curlen)
466 		goto fail_len;
467 	repmsg->u.remote_i2c_read_ack.num_bytes = raw->msg[idx];
468 	idx++;
469 	/* TODO check */
470 	memcpy(repmsg->u.remote_i2c_read_ack.bytes, &raw->msg[idx], repmsg->u.remote_i2c_read_ack.num_bytes);
471 	return true;
472 fail_len:
473 	DRM_DEBUG_KMS("remote i2c reply parse length fail %d %d\n", idx, raw->curlen);
474 	return false;
475 }
476 
drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * repmsg)477 static bool drm_dp_sideband_parse_enum_path_resources_ack(struct drm_dp_sideband_msg_rx *raw,
478 							  struct drm_dp_sideband_msg_reply_body *repmsg)
479 {
480 	int idx = 1;
481 	repmsg->u.path_resources.port_number = (raw->msg[idx] >> 4) & 0xf;
482 	idx++;
483 	if (idx > raw->curlen)
484 		goto fail_len;
485 	repmsg->u.path_resources.full_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
486 	idx += 2;
487 	if (idx > raw->curlen)
488 		goto fail_len;
489 	repmsg->u.path_resources.avail_payload_bw_number = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
490 	idx += 2;
491 	if (idx > raw->curlen)
492 		goto fail_len;
493 	return true;
494 fail_len:
495 	DRM_DEBUG_KMS("enum resource parse length fail %d %d\n", idx, raw->curlen);
496 	return false;
497 }
498 
drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * repmsg)499 static bool drm_dp_sideband_parse_allocate_payload_ack(struct drm_dp_sideband_msg_rx *raw,
500 							  struct drm_dp_sideband_msg_reply_body *repmsg)
501 {
502 	int idx = 1;
503 	repmsg->u.allocate_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
504 	idx++;
505 	if (idx > raw->curlen)
506 		goto fail_len;
507 	repmsg->u.allocate_payload.vcpi = raw->msg[idx];
508 	idx++;
509 	if (idx > raw->curlen)
510 		goto fail_len;
511 	repmsg->u.allocate_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx+1]);
512 	idx += 2;
513 	if (idx > raw->curlen)
514 		goto fail_len;
515 	return true;
516 fail_len:
517 	DRM_DEBUG_KMS("allocate payload parse length fail %d %d\n", idx, raw->curlen);
518 	return false;
519 }
520 
drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * repmsg)521 static bool drm_dp_sideband_parse_query_payload_ack(struct drm_dp_sideband_msg_rx *raw,
522 						    struct drm_dp_sideband_msg_reply_body *repmsg)
523 {
524 	int idx = 1;
525 	repmsg->u.query_payload.port_number = (raw->msg[idx] >> 4) & 0xf;
526 	idx++;
527 	if (idx > raw->curlen)
528 		goto fail_len;
529 	repmsg->u.query_payload.allocated_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
530 	idx += 2;
531 	if (idx > raw->curlen)
532 		goto fail_len;
533 	return true;
534 fail_len:
535 	DRM_DEBUG_KMS("query payload parse length fail %d %d\n", idx, raw->curlen);
536 	return false;
537 }
538 
drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_reply_body * msg)539 static bool drm_dp_sideband_parse_reply(struct drm_dp_sideband_msg_rx *raw,
540 					struct drm_dp_sideband_msg_reply_body *msg)
541 {
542 	memset(msg, 0, sizeof(*msg));
543 	msg->reply_type = (raw->msg[0] & 0x80) >> 7;
544 	msg->req_type = (raw->msg[0] & 0x7f);
545 
546 	if (msg->reply_type) {
547 		memcpy(msg->u.nak.guid, &raw->msg[1], 16);
548 		msg->u.nak.reason = raw->msg[17];
549 		msg->u.nak.nak_data = raw->msg[18];
550 		return false;
551 	}
552 
553 	switch (msg->req_type) {
554 	case DP_LINK_ADDRESS:
555 		return drm_dp_sideband_parse_link_address(raw, msg);
556 	case DP_QUERY_PAYLOAD:
557 		return drm_dp_sideband_parse_query_payload_ack(raw, msg);
558 	case DP_REMOTE_DPCD_READ:
559 		return drm_dp_sideband_parse_remote_dpcd_read(raw, msg);
560 	case DP_REMOTE_DPCD_WRITE:
561 		return drm_dp_sideband_parse_remote_dpcd_write(raw, msg);
562 	case DP_REMOTE_I2C_READ:
563 		return drm_dp_sideband_parse_remote_i2c_read_ack(raw, msg);
564 	case DP_ENUM_PATH_RESOURCES:
565 		return drm_dp_sideband_parse_enum_path_resources_ack(raw, msg);
566 	case DP_ALLOCATE_PAYLOAD:
567 		return drm_dp_sideband_parse_allocate_payload_ack(raw, msg);
568 	default:
569 		DRM_ERROR("Got unknown reply 0x%02x\n", msg->req_type);
570 		return false;
571 	}
572 }
573 
drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_req_body * msg)574 static bool drm_dp_sideband_parse_connection_status_notify(struct drm_dp_sideband_msg_rx *raw,
575 							   struct drm_dp_sideband_msg_req_body *msg)
576 {
577 	int idx = 1;
578 
579 	msg->u.conn_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
580 	idx++;
581 	if (idx > raw->curlen)
582 		goto fail_len;
583 
584 	memcpy(msg->u.conn_stat.guid, &raw->msg[idx], 16);
585 	idx += 16;
586 	if (idx > raw->curlen)
587 		goto fail_len;
588 
589 	msg->u.conn_stat.legacy_device_plug_status = (raw->msg[idx] >> 6) & 0x1;
590 	msg->u.conn_stat.displayport_device_plug_status = (raw->msg[idx] >> 5) & 0x1;
591 	msg->u.conn_stat.message_capability_status = (raw->msg[idx] >> 4) & 0x1;
592 	msg->u.conn_stat.input_port = (raw->msg[idx] >> 3) & 0x1;
593 	msg->u.conn_stat.peer_device_type = (raw->msg[idx] & 0x7);
594 	idx++;
595 	return true;
596 fail_len:
597 	DRM_DEBUG_KMS("connection status reply parse length fail %d %d\n", idx, raw->curlen);
598 	return false;
599 }
600 
drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_req_body * msg)601 static bool drm_dp_sideband_parse_resource_status_notify(struct drm_dp_sideband_msg_rx *raw,
602 							   struct drm_dp_sideband_msg_req_body *msg)
603 {
604 	int idx = 1;
605 
606 	msg->u.resource_stat.port_number = (raw->msg[idx] & 0xf0) >> 4;
607 	idx++;
608 	if (idx > raw->curlen)
609 		goto fail_len;
610 
611 	memcpy(msg->u.resource_stat.guid, &raw->msg[idx], 16);
612 	idx += 16;
613 	if (idx > raw->curlen)
614 		goto fail_len;
615 
616 	msg->u.resource_stat.available_pbn = (raw->msg[idx] << 8) | (raw->msg[idx + 1]);
617 	idx++;
618 	return true;
619 fail_len:
620 	DRM_DEBUG_KMS("resource status reply parse length fail %d %d\n", idx, raw->curlen);
621 	return false;
622 }
623 
drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx * raw,struct drm_dp_sideband_msg_req_body * msg)624 static bool drm_dp_sideband_parse_req(struct drm_dp_sideband_msg_rx *raw,
625 				      struct drm_dp_sideband_msg_req_body *msg)
626 {
627 	memset(msg, 0, sizeof(*msg));
628 	msg->req_type = (raw->msg[0] & 0x7f);
629 
630 	switch (msg->req_type) {
631 	case DP_CONNECTION_STATUS_NOTIFY:
632 		return drm_dp_sideband_parse_connection_status_notify(raw, msg);
633 	case DP_RESOURCE_STATUS_NOTIFY:
634 		return drm_dp_sideband_parse_resource_status_notify(raw, msg);
635 	default:
636 		DRM_ERROR("Got unknown request 0x%02x\n", msg->req_type);
637 		return false;
638 	}
639 }
640 
build_dpcd_write(struct drm_dp_sideband_msg_tx * msg,u8 port_num,u32 offset,u8 num_bytes,u8 * bytes)641 static int build_dpcd_write(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes, u8 *bytes)
642 {
643 	struct drm_dp_sideband_msg_req_body req;
644 
645 	req.req_type = DP_REMOTE_DPCD_WRITE;
646 	req.u.dpcd_write.port_number = port_num;
647 	req.u.dpcd_write.dpcd_address = offset;
648 	req.u.dpcd_write.num_bytes = num_bytes;
649 	req.u.dpcd_write.bytes = bytes;
650 	drm_dp_encode_sideband_req(&req, msg);
651 
652 	return 0;
653 }
654 
build_link_address(struct drm_dp_sideband_msg_tx * msg)655 static int build_link_address(struct drm_dp_sideband_msg_tx *msg)
656 {
657 	struct drm_dp_sideband_msg_req_body req;
658 
659 	req.req_type = DP_LINK_ADDRESS;
660 	drm_dp_encode_sideband_req(&req, msg);
661 	return 0;
662 }
663 
build_enum_path_resources(struct drm_dp_sideband_msg_tx * msg,int port_num)664 static int build_enum_path_resources(struct drm_dp_sideband_msg_tx *msg, int port_num)
665 {
666 	struct drm_dp_sideband_msg_req_body req;
667 
668 	req.req_type = DP_ENUM_PATH_RESOURCES;
669 	req.u.port_num.port_number = port_num;
670 	drm_dp_encode_sideband_req(&req, msg);
671 	msg->path_msg = true;
672 	return 0;
673 }
674 
build_allocate_payload(struct drm_dp_sideband_msg_tx * msg,int port_num,u8 vcpi,uint16_t pbn,u8 number_sdp_streams,u8 * sdp_stream_sink)675 static int build_allocate_payload(struct drm_dp_sideband_msg_tx *msg, int port_num,
676 				  u8 vcpi, uint16_t pbn,
677 				  u8 number_sdp_streams,
678 				  u8 *sdp_stream_sink)
679 {
680 	struct drm_dp_sideband_msg_req_body req;
681 	memset(&req, 0, sizeof(req));
682 	req.req_type = DP_ALLOCATE_PAYLOAD;
683 	req.u.allocate_payload.port_number = port_num;
684 	req.u.allocate_payload.vcpi = vcpi;
685 	req.u.allocate_payload.pbn = pbn;
686 	req.u.allocate_payload.number_sdp_streams = number_sdp_streams;
687 	memcpy(req.u.allocate_payload.sdp_stream_sink, sdp_stream_sink,
688 		   number_sdp_streams);
689 	drm_dp_encode_sideband_req(&req, msg);
690 	msg->path_msg = true;
691 	return 0;
692 }
693 
drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_vcpi * vcpi)694 static int drm_dp_mst_assign_payload_id(struct drm_dp_mst_topology_mgr *mgr,
695 					struct drm_dp_vcpi *vcpi)
696 {
697 	int ret, vcpi_ret;
698 
699 	mutex_lock(&mgr->payload_lock);
700 	ret = find_first_zero_bit(&mgr->payload_mask, mgr->max_payloads + 1);
701 	if (ret > mgr->max_payloads) {
702 		ret = -EINVAL;
703 		DRM_DEBUG_KMS("out of payload ids %d\n", ret);
704 		goto out_unlock;
705 	}
706 
707 	vcpi_ret = find_first_zero_bit(&mgr->vcpi_mask, mgr->max_payloads + 1);
708 	if (vcpi_ret > mgr->max_payloads) {
709 		ret = -EINVAL;
710 		DRM_DEBUG_KMS("out of vcpi ids %d\n", ret);
711 		goto out_unlock;
712 	}
713 
714 	set_bit(ret, &mgr->payload_mask);
715 	set_bit(vcpi_ret, &mgr->vcpi_mask);
716 	vcpi->vcpi = vcpi_ret + 1;
717 	mgr->proposed_vcpis[ret - 1] = vcpi;
718 out_unlock:
719 	mutex_unlock(&mgr->payload_lock);
720 	return ret;
721 }
722 
drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr * mgr,int vcpi)723 static void drm_dp_mst_put_payload_id(struct drm_dp_mst_topology_mgr *mgr,
724 				      int vcpi)
725 {
726 	int i;
727 	if (vcpi == 0)
728 		return;
729 
730 	mutex_lock(&mgr->payload_lock);
731 	DRM_DEBUG_KMS("putting payload %d\n", vcpi);
732 	clear_bit(vcpi - 1, &mgr->vcpi_mask);
733 
734 	for (i = 0; i < mgr->max_payloads; i++) {
735 		if (mgr->proposed_vcpis[i])
736 			if (mgr->proposed_vcpis[i]->vcpi == vcpi) {
737 				mgr->proposed_vcpis[i] = NULL;
738 				clear_bit(i + 1, &mgr->payload_mask);
739 			}
740 	}
741 	mutex_unlock(&mgr->payload_lock);
742 }
743 
check_txmsg_state(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_sideband_msg_tx * txmsg)744 static bool check_txmsg_state(struct drm_dp_mst_topology_mgr *mgr,
745 			      struct drm_dp_sideband_msg_tx *txmsg)
746 {
747 	bool ret;
748 
749 	/*
750 	 * All updates to txmsg->state are protected by mgr->qlock, and the two
751 	 * cases we check here are terminal states. For those the barriers
752 	 * provided by the wake_up/wait_event pair are enough.
753 	 */
754 	ret = (txmsg->state == DRM_DP_SIDEBAND_TX_RX ||
755 	       txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT);
756 	return ret;
757 }
758 
drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch * mstb,struct drm_dp_sideband_msg_tx * txmsg)759 static int drm_dp_mst_wait_tx_reply(struct drm_dp_mst_branch *mstb,
760 				    struct drm_dp_sideband_msg_tx *txmsg)
761 {
762 	struct drm_dp_mst_topology_mgr *mgr = mstb->mgr;
763 	int ret;
764 
765 	ret = wait_event_timeout(mgr->tx_waitq,
766 				 check_txmsg_state(mgr, txmsg),
767 				 (4 * HZ));
768 	mutex_lock(&mstb->mgr->qlock);
769 	if (ret > 0) {
770 		if (txmsg->state == DRM_DP_SIDEBAND_TX_TIMEOUT) {
771 			ret = -EIO;
772 			goto out;
773 		}
774 	} else {
775 		DRM_DEBUG_KMS("timedout msg send %p %d %d\n", txmsg, txmsg->state, txmsg->seqno);
776 
777 		/* dump some state */
778 		ret = -EIO;
779 
780 		/* remove from q */
781 		if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED ||
782 		    txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND) {
783 			list_del(&txmsg->next);
784 		}
785 
786 		if (txmsg->state == DRM_DP_SIDEBAND_TX_START_SEND ||
787 		    txmsg->state == DRM_DP_SIDEBAND_TX_SENT) {
788 			mstb->tx_slots[txmsg->seqno] = NULL;
789 		}
790 	}
791 out:
792 	mutex_unlock(&mgr->qlock);
793 
794 	return ret;
795 }
796 
drm_dp_add_mst_branch_device(u8 lct,u8 * rad)797 static struct drm_dp_mst_branch *drm_dp_add_mst_branch_device(u8 lct, u8 *rad)
798 {
799 	struct drm_dp_mst_branch *mstb;
800 
801 	mstb = kzalloc(sizeof(*mstb), GFP_KERNEL);
802 	if (!mstb)
803 		return NULL;
804 
805 	mstb->lct = lct;
806 	if (lct > 1)
807 		memcpy(mstb->rad, rad, lct / 2);
808 	INIT_LIST_HEAD(&mstb->ports);
809 	kref_init(&mstb->kref);
810 	return mstb;
811 }
812 
813 static void drm_dp_free_mst_port(struct kref *kref);
814 
drm_dp_free_mst_branch_device(struct kref * kref)815 static void drm_dp_free_mst_branch_device(struct kref *kref)
816 {
817 	struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
818 	if (mstb->port_parent) {
819 		if (list_empty(&mstb->port_parent->next))
820 			kref_put(&mstb->port_parent->kref, drm_dp_free_mst_port);
821 	}
822 	kfree(mstb);
823 }
824 
drm_dp_destroy_mst_branch_device(struct kref * kref)825 static void drm_dp_destroy_mst_branch_device(struct kref *kref)
826 {
827 	struct drm_dp_mst_branch *mstb = container_of(kref, struct drm_dp_mst_branch, kref);
828 	struct drm_dp_mst_port *port, *tmp;
829 	bool wake_tx = false;
830 
831 	/*
832 	 * init kref again to be used by ports to remove mst branch when it is
833 	 * not needed anymore
834 	 */
835 	kref_init(kref);
836 
837 	if (mstb->port_parent && list_empty(&mstb->port_parent->next))
838 		kref_get(&mstb->port_parent->kref);
839 
840 	/*
841 	 * destroy all ports - don't need lock
842 	 * as there are no more references to the mst branch
843 	 * device at this point.
844 	 */
845 	list_for_each_entry_safe(port, tmp, &mstb->ports, next) {
846 		list_del(&port->next);
847 		drm_dp_put_port(port);
848 	}
849 
850 	/* drop any tx slots msg */
851 	mutex_lock(&mstb->mgr->qlock);
852 	if (mstb->tx_slots[0]) {
853 		mstb->tx_slots[0]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
854 		mstb->tx_slots[0] = NULL;
855 		wake_tx = true;
856 	}
857 	if (mstb->tx_slots[1]) {
858 		mstb->tx_slots[1]->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
859 		mstb->tx_slots[1] = NULL;
860 		wake_tx = true;
861 	}
862 	mutex_unlock(&mstb->mgr->qlock);
863 
864 	if (wake_tx)
865 		wake_up(&mstb->mgr->tx_waitq);
866 
867 	kref_put(kref, drm_dp_free_mst_branch_device);
868 }
869 
drm_dp_put_mst_branch_device(struct drm_dp_mst_branch * mstb)870 static void drm_dp_put_mst_branch_device(struct drm_dp_mst_branch *mstb)
871 {
872 	kref_put(&mstb->kref, drm_dp_destroy_mst_branch_device);
873 }
874 
875 
drm_dp_port_teardown_pdt(struct drm_dp_mst_port * port,int old_pdt)876 static void drm_dp_port_teardown_pdt(struct drm_dp_mst_port *port, int old_pdt)
877 {
878 	struct drm_dp_mst_branch *mstb;
879 
880 	switch (old_pdt) {
881 	case DP_PEER_DEVICE_DP_LEGACY_CONV:
882 	case DP_PEER_DEVICE_SST_SINK:
883 		/* remove i2c over sideband */
884 		drm_dp_mst_unregister_i2c_bus(&port->aux);
885 		break;
886 	case DP_PEER_DEVICE_MST_BRANCHING:
887 		mstb = port->mstb;
888 		port->mstb = NULL;
889 		drm_dp_put_mst_branch_device(mstb);
890 		break;
891 	}
892 }
893 
drm_dp_destroy_port(struct kref * kref)894 static void drm_dp_destroy_port(struct kref *kref)
895 {
896 	struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
897 	struct drm_dp_mst_topology_mgr *mgr = port->mgr;
898 
899 	if (!port->input) {
900 		port->vcpi.num_slots = 0;
901 
902 		kfree(port->cached_edid);
903 
904 		/*
905 		 * The only time we don't have a connector
906 		 * on an output port is if the connector init
907 		 * fails.
908 		 */
909 		if (port->connector) {
910 			/* we can't destroy the connector here, as
911 			 * we might be holding the mode_config.mutex
912 			 * from an EDID retrieval */
913 
914 			mutex_lock(&mgr->destroy_connector_lock);
915 			kref_get(&port->parent->kref);
916 			list_add(&port->next, &mgr->destroy_connector_list);
917 			mutex_unlock(&mgr->destroy_connector_lock);
918 			schedule_work(&mgr->destroy_connector_work);
919 			return;
920 		}
921 		/* no need to clean up vcpi
922 		 * as if we have no connector we never setup a vcpi */
923 		drm_dp_port_teardown_pdt(port, port->pdt);
924 		port->pdt = DP_PEER_DEVICE_NONE;
925 	}
926 	kfree(port);
927 }
928 
drm_dp_put_port(struct drm_dp_mst_port * port)929 static void drm_dp_put_port(struct drm_dp_mst_port *port)
930 {
931 	kref_put(&port->kref, drm_dp_destroy_port);
932 }
933 
drm_dp_mst_get_validated_mstb_ref_locked(struct drm_dp_mst_branch * mstb,struct drm_dp_mst_branch * to_find)934 static struct drm_dp_mst_branch *drm_dp_mst_get_validated_mstb_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_branch *to_find)
935 {
936 	struct drm_dp_mst_port *port;
937 	struct drm_dp_mst_branch *rmstb;
938 	if (to_find == mstb) {
939 		kref_get(&mstb->kref);
940 		return mstb;
941 	}
942 	list_for_each_entry(port, &mstb->ports, next) {
943 		if (port->mstb) {
944 			rmstb = drm_dp_mst_get_validated_mstb_ref_locked(port->mstb, to_find);
945 			if (rmstb)
946 				return rmstb;
947 		}
948 	}
949 	return NULL;
950 }
951 
drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_branch * mstb)952 static struct drm_dp_mst_branch *drm_dp_get_validated_mstb_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_branch *mstb)
953 {
954 	struct drm_dp_mst_branch *rmstb = NULL;
955 	mutex_lock(&mgr->lock);
956 	if (mgr->mst_primary)
957 		rmstb = drm_dp_mst_get_validated_mstb_ref_locked(mgr->mst_primary, mstb);
958 	mutex_unlock(&mgr->lock);
959 	return rmstb;
960 }
961 
drm_dp_mst_get_port_ref_locked(struct drm_dp_mst_branch * mstb,struct drm_dp_mst_port * to_find)962 static struct drm_dp_mst_port *drm_dp_mst_get_port_ref_locked(struct drm_dp_mst_branch *mstb, struct drm_dp_mst_port *to_find)
963 {
964 	struct drm_dp_mst_port *port, *mport;
965 
966 	list_for_each_entry(port, &mstb->ports, next) {
967 		if (port == to_find) {
968 			kref_get(&port->kref);
969 			return port;
970 		}
971 		if (port->mstb) {
972 			mport = drm_dp_mst_get_port_ref_locked(port->mstb, to_find);
973 			if (mport)
974 				return mport;
975 		}
976 	}
977 	return NULL;
978 }
979 
drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port)980 static struct drm_dp_mst_port *drm_dp_get_validated_port_ref(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
981 {
982 	struct drm_dp_mst_port *rport = NULL;
983 	mutex_lock(&mgr->lock);
984 	if (mgr->mst_primary)
985 		rport = drm_dp_mst_get_port_ref_locked(mgr->mst_primary, port);
986 	mutex_unlock(&mgr->lock);
987 	return rport;
988 }
989 
drm_dp_get_port(struct drm_dp_mst_branch * mstb,u8 port_num)990 static struct drm_dp_mst_port *drm_dp_get_port(struct drm_dp_mst_branch *mstb, u8 port_num)
991 {
992 	struct drm_dp_mst_port *port;
993 
994 	list_for_each_entry(port, &mstb->ports, next) {
995 		if (port->port_num == port_num) {
996 			kref_get(&port->kref);
997 			return port;
998 		}
999 	}
1000 
1001 	return NULL;
1002 }
1003 
1004 /*
1005  * calculate a new RAD for this MST branch device
1006  * if parent has an LCT of 2 then it has 1 nibble of RAD,
1007  * if parent has an LCT of 3 then it has 2 nibbles of RAD,
1008  */
drm_dp_calculate_rad(struct drm_dp_mst_port * port,u8 * rad)1009 static u8 drm_dp_calculate_rad(struct drm_dp_mst_port *port,
1010 				 u8 *rad)
1011 {
1012 	int parent_lct = port->parent->lct;
1013 	int shift = 4;
1014 	int idx = (parent_lct - 1) / 2;
1015 	if (parent_lct > 1) {
1016 		memcpy(rad, port->parent->rad, idx + 1);
1017 		shift = (parent_lct % 2) ? 4 : 0;
1018 	} else
1019 		rad[0] = 0;
1020 
1021 	rad[idx] |= port->port_num << shift;
1022 	return parent_lct + 1;
1023 }
1024 
1025 /*
1026  * return sends link address for new mstb
1027  */
drm_dp_port_setup_pdt(struct drm_dp_mst_port * port)1028 static bool drm_dp_port_setup_pdt(struct drm_dp_mst_port *port)
1029 {
1030 	int ret;
1031 	u8 rad[6], lct;
1032 	bool send_link = false;
1033 	switch (port->pdt) {
1034 	case DP_PEER_DEVICE_DP_LEGACY_CONV:
1035 	case DP_PEER_DEVICE_SST_SINK:
1036 		/* add i2c over sideband */
1037 		ret = drm_dp_mst_register_i2c_bus(&port->aux);
1038 		break;
1039 	case DP_PEER_DEVICE_MST_BRANCHING:
1040 		lct = drm_dp_calculate_rad(port, rad);
1041 
1042 		port->mstb = drm_dp_add_mst_branch_device(lct, rad);
1043 		port->mstb->mgr = port->mgr;
1044 		port->mstb->port_parent = port;
1045 
1046 		send_link = true;
1047 		break;
1048 	}
1049 	return send_link;
1050 }
1051 
drm_dp_check_mstb_guid(struct drm_dp_mst_branch * mstb,u8 * guid)1052 static void drm_dp_check_mstb_guid(struct drm_dp_mst_branch *mstb, u8 *guid)
1053 {
1054 	int ret;
1055 
1056 	memcpy(mstb->guid, guid, 16);
1057 
1058 	if (!drm_dp_validate_guid(mstb->mgr, mstb->guid)) {
1059 		if (mstb->port_parent) {
1060 			ret = drm_dp_send_dpcd_write(
1061 					mstb->mgr,
1062 					mstb->port_parent,
1063 					DP_GUID,
1064 					16,
1065 					mstb->guid);
1066 		} else {
1067 
1068 			ret = drm_dp_dpcd_write(
1069 					mstb->mgr->aux,
1070 					DP_GUID,
1071 					mstb->guid,
1072 					16);
1073 		}
1074 	}
1075 }
1076 
build_mst_prop_path(const struct drm_dp_mst_branch * mstb,int pnum,char * proppath,size_t proppath_size)1077 static void build_mst_prop_path(const struct drm_dp_mst_branch *mstb,
1078 				int pnum,
1079 				char *proppath,
1080 				size_t proppath_size)
1081 {
1082 	int i;
1083 	char temp[8];
1084 	snprintf(proppath, proppath_size, "mst:%d", mstb->mgr->conn_base_id);
1085 	for (i = 0; i < (mstb->lct - 1); i++) {
1086 		int shift = (i % 2) ? 0 : 4;
1087 		int port_num = (mstb->rad[i / 2] >> shift) & 0xf;
1088 		snprintf(temp, sizeof(temp), "-%d", port_num);
1089 		strlcat(proppath, temp, proppath_size);
1090 	}
1091 	snprintf(temp, sizeof(temp), "-%d", pnum);
1092 	strlcat(proppath, temp, proppath_size);
1093 }
1094 
drm_dp_add_port(struct drm_dp_mst_branch * mstb,struct device * dev,struct drm_dp_link_addr_reply_port * port_msg)1095 static void drm_dp_add_port(struct drm_dp_mst_branch *mstb,
1096 			    struct device *dev,
1097 			    struct drm_dp_link_addr_reply_port *port_msg)
1098 {
1099 	struct drm_dp_mst_port *port;
1100 	bool ret;
1101 	bool created = false;
1102 	int old_pdt = 0;
1103 	int old_ddps = 0;
1104 	port = drm_dp_get_port(mstb, port_msg->port_number);
1105 	if (!port) {
1106 		port = kzalloc(sizeof(*port), GFP_KERNEL);
1107 		if (!port)
1108 			return;
1109 		kref_init(&port->kref);
1110 		port->parent = mstb;
1111 		port->port_num = port_msg->port_number;
1112 		port->mgr = mstb->mgr;
1113 		port->aux.name = "DPMST";
1114 		port->aux.dev = dev;
1115 		created = true;
1116 	} else {
1117 		old_pdt = port->pdt;
1118 		old_ddps = port->ddps;
1119 	}
1120 
1121 	port->pdt = port_msg->peer_device_type;
1122 	port->input = port_msg->input_port;
1123 	port->mcs = port_msg->mcs;
1124 	port->ddps = port_msg->ddps;
1125 	port->ldps = port_msg->legacy_device_plug_status;
1126 	port->dpcd_rev = port_msg->dpcd_revision;
1127 	port->num_sdp_streams = port_msg->num_sdp_streams;
1128 	port->num_sdp_stream_sinks = port_msg->num_sdp_stream_sinks;
1129 
1130 	/* manage mstb port lists with mgr lock - take a reference
1131 	   for this list */
1132 	if (created) {
1133 		mutex_lock(&mstb->mgr->lock);
1134 		kref_get(&port->kref);
1135 		list_add(&port->next, &mstb->ports);
1136 		mutex_unlock(&mstb->mgr->lock);
1137 	}
1138 
1139 	if (old_ddps != port->ddps) {
1140 		if (port->ddps) {
1141 			if (!port->input)
1142 				drm_dp_send_enum_path_resources(mstb->mgr, mstb, port);
1143 		} else {
1144 			port->available_pbn = 0;
1145 			}
1146 	}
1147 
1148 	if (old_pdt != port->pdt && !port->input) {
1149 		drm_dp_port_teardown_pdt(port, old_pdt);
1150 
1151 		ret = drm_dp_port_setup_pdt(port);
1152 		if (ret == true)
1153 			drm_dp_send_link_address(mstb->mgr, port->mstb);
1154 	}
1155 
1156 	if (created && !port->input) {
1157 		char proppath[255];
1158 
1159 		build_mst_prop_path(mstb, port->port_num, proppath, sizeof(proppath));
1160 		port->connector = (*mstb->mgr->cbs->add_connector)(mstb->mgr, port, proppath);
1161 		if (!port->connector) {
1162 			/* remove it from the port list */
1163 			mutex_lock(&mstb->mgr->lock);
1164 			list_del(&port->next);
1165 			mutex_unlock(&mstb->mgr->lock);
1166 			/* drop port list reference */
1167 			drm_dp_put_port(port);
1168 			goto out;
1169 		}
1170 		if ((port->pdt == DP_PEER_DEVICE_DP_LEGACY_CONV ||
1171 		     port->pdt == DP_PEER_DEVICE_SST_SINK) &&
1172 		    port->port_num >= DP_MST_LOGICAL_PORT_0) {
1173 			port->cached_edid = drm_get_edid(port->connector, &port->aux.ddc);
1174 			drm_mode_connector_set_tile_property(port->connector);
1175 		}
1176 		(*mstb->mgr->cbs->register_connector)(port->connector);
1177 	}
1178 
1179 out:
1180 	/* put reference to this port */
1181 	drm_dp_put_port(port);
1182 }
1183 
drm_dp_update_port(struct drm_dp_mst_branch * mstb,struct drm_dp_connection_status_notify * conn_stat)1184 static void drm_dp_update_port(struct drm_dp_mst_branch *mstb,
1185 			       struct drm_dp_connection_status_notify *conn_stat)
1186 {
1187 	struct drm_dp_mst_port *port;
1188 	int old_pdt;
1189 	int old_ddps;
1190 	bool dowork = false;
1191 	port = drm_dp_get_port(mstb, conn_stat->port_number);
1192 	if (!port)
1193 		return;
1194 
1195 	old_ddps = port->ddps;
1196 	old_pdt = port->pdt;
1197 	port->pdt = conn_stat->peer_device_type;
1198 	port->mcs = conn_stat->message_capability_status;
1199 	port->ldps = conn_stat->legacy_device_plug_status;
1200 	port->ddps = conn_stat->displayport_device_plug_status;
1201 
1202 	if (old_ddps != port->ddps) {
1203 		if (port->ddps) {
1204 			dowork = true;
1205 		} else {
1206 			port->available_pbn = 0;
1207 		}
1208 	}
1209 	if (old_pdt != port->pdt && !port->input) {
1210 		drm_dp_port_teardown_pdt(port, old_pdt);
1211 
1212 		if (drm_dp_port_setup_pdt(port))
1213 			dowork = true;
1214 	}
1215 
1216 	drm_dp_put_port(port);
1217 	if (dowork)
1218 		queue_work(system_long_wq, &mstb->mgr->work);
1219 
1220 }
1221 
drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr * mgr,u8 lct,u8 * rad)1222 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device(struct drm_dp_mst_topology_mgr *mgr,
1223 							       u8 lct, u8 *rad)
1224 {
1225 	struct drm_dp_mst_branch *mstb;
1226 	struct drm_dp_mst_port *port;
1227 	int i;
1228 	/* find the port by iterating down */
1229 
1230 	mutex_lock(&mgr->lock);
1231 	mstb = mgr->mst_primary;
1232 
1233 	for (i = 0; i < lct - 1; i++) {
1234 		int shift = (i % 2) ? 0 : 4;
1235 		int port_num = (rad[i / 2] >> shift) & 0xf;
1236 
1237 		list_for_each_entry(port, &mstb->ports, next) {
1238 			if (port->port_num == port_num) {
1239 				mstb = port->mstb;
1240 				if (!mstb) {
1241 					DRM_ERROR("failed to lookup MSTB with lct %d, rad %02x\n", lct, rad[0]);
1242 					goto out;
1243 				}
1244 
1245 				break;
1246 			}
1247 		}
1248 	}
1249 	kref_get(&mstb->kref);
1250 out:
1251 	mutex_unlock(&mgr->lock);
1252 	return mstb;
1253 }
1254 
get_mst_branch_device_by_guid_helper(struct drm_dp_mst_branch * mstb,uint8_t * guid)1255 static struct drm_dp_mst_branch *get_mst_branch_device_by_guid_helper(
1256 	struct drm_dp_mst_branch *mstb,
1257 	uint8_t *guid)
1258 {
1259 	struct drm_dp_mst_branch *found_mstb;
1260 	struct drm_dp_mst_port *port;
1261 
1262 	if (memcmp(mstb->guid, guid, 16) == 0)
1263 		return mstb;
1264 
1265 
1266 	list_for_each_entry(port, &mstb->ports, next) {
1267 		if (!port->mstb)
1268 			continue;
1269 
1270 		found_mstb = get_mst_branch_device_by_guid_helper(port->mstb, guid);
1271 
1272 		if (found_mstb)
1273 			return found_mstb;
1274 	}
1275 
1276 	return NULL;
1277 }
1278 
drm_dp_get_mst_branch_device_by_guid(struct drm_dp_mst_topology_mgr * mgr,uint8_t * guid)1279 static struct drm_dp_mst_branch *drm_dp_get_mst_branch_device_by_guid(
1280 	struct drm_dp_mst_topology_mgr *mgr,
1281 	uint8_t *guid)
1282 {
1283 	struct drm_dp_mst_branch *mstb;
1284 
1285 	/* find the port by iterating down */
1286 	mutex_lock(&mgr->lock);
1287 
1288 	mstb = get_mst_branch_device_by_guid_helper(mgr->mst_primary, guid);
1289 
1290 	if (mstb)
1291 		kref_get(&mstb->kref);
1292 
1293 	mutex_unlock(&mgr->lock);
1294 	return mstb;
1295 }
1296 
drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_branch * mstb)1297 static void drm_dp_check_and_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1298 					       struct drm_dp_mst_branch *mstb)
1299 {
1300 	struct drm_dp_mst_port *port;
1301 	struct drm_dp_mst_branch *mstb_child;
1302 	if (!mstb->link_address_sent)
1303 		drm_dp_send_link_address(mgr, mstb);
1304 
1305 	list_for_each_entry(port, &mstb->ports, next) {
1306 		if (port->input)
1307 			continue;
1308 
1309 		if (!port->ddps)
1310 			continue;
1311 
1312 		if (!port->available_pbn)
1313 			drm_dp_send_enum_path_resources(mgr, mstb, port);
1314 
1315 		if (port->mstb) {
1316 			mstb_child = drm_dp_get_validated_mstb_ref(mgr, port->mstb);
1317 			if (mstb_child) {
1318 				drm_dp_check_and_send_link_address(mgr, mstb_child);
1319 				drm_dp_put_mst_branch_device(mstb_child);
1320 			}
1321 		}
1322 	}
1323 }
1324 
drm_dp_mst_link_probe_work(struct work_struct * work)1325 static void drm_dp_mst_link_probe_work(struct work_struct *work)
1326 {
1327 	struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, work);
1328 	struct drm_dp_mst_branch *mstb;
1329 
1330 	mutex_lock(&mgr->lock);
1331 	mstb = mgr->mst_primary;
1332 	if (mstb) {
1333 		kref_get(&mstb->kref);
1334 	}
1335 	mutex_unlock(&mgr->lock);
1336 	if (mstb) {
1337 		drm_dp_check_and_send_link_address(mgr, mstb);
1338 		drm_dp_put_mst_branch_device(mstb);
1339 	}
1340 }
1341 
drm_dp_validate_guid(struct drm_dp_mst_topology_mgr * mgr,u8 * guid)1342 static bool drm_dp_validate_guid(struct drm_dp_mst_topology_mgr *mgr,
1343 				 u8 *guid)
1344 {
1345 	static u8 zero_guid[16];
1346 
1347 	if (!memcmp(guid, zero_guid, 16)) {
1348 		u64 salt = get_jiffies_64();
1349 		memcpy(&guid[0], &salt, sizeof(u64));
1350 		memcpy(&guid[8], &salt, sizeof(u64));
1351 		return false;
1352 	}
1353 	return true;
1354 }
1355 
1356 #if 0
1357 static int build_dpcd_read(struct drm_dp_sideband_msg_tx *msg, u8 port_num, u32 offset, u8 num_bytes)
1358 {
1359 	struct drm_dp_sideband_msg_req_body req;
1360 
1361 	req.req_type = DP_REMOTE_DPCD_READ;
1362 	req.u.dpcd_read.port_number = port_num;
1363 	req.u.dpcd_read.dpcd_address = offset;
1364 	req.u.dpcd_read.num_bytes = num_bytes;
1365 	drm_dp_encode_sideband_req(&req, msg);
1366 
1367 	return 0;
1368 }
1369 #endif
1370 
drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr * mgr,bool up,u8 * msg,int len)1371 static int drm_dp_send_sideband_msg(struct drm_dp_mst_topology_mgr *mgr,
1372 				    bool up, u8 *msg, int len)
1373 {
1374 	int ret;
1375 	int regbase = up ? DP_SIDEBAND_MSG_UP_REP_BASE : DP_SIDEBAND_MSG_DOWN_REQ_BASE;
1376 	int tosend, total, offset;
1377 	int retries = 0;
1378 
1379 retry:
1380 	total = len;
1381 	offset = 0;
1382 	do {
1383 		tosend = min3(mgr->max_dpcd_transaction_bytes, 16, total);
1384 
1385 		ret = drm_dp_dpcd_write(mgr->aux, regbase + offset,
1386 					&msg[offset],
1387 					tosend);
1388 		if (ret != tosend) {
1389 			if (ret == -EIO && retries < 5) {
1390 				retries++;
1391 				goto retry;
1392 			}
1393 			DRM_DEBUG_KMS("failed to dpcd write %d %d\n", tosend, ret);
1394 
1395 			return -EIO;
1396 		}
1397 		offset += tosend;
1398 		total -= tosend;
1399 	} while (total > 0);
1400 	return 0;
1401 }
1402 
set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr * hdr,struct drm_dp_sideband_msg_tx * txmsg)1403 static int set_hdr_from_dst_qlock(struct drm_dp_sideband_msg_hdr *hdr,
1404 				  struct drm_dp_sideband_msg_tx *txmsg)
1405 {
1406 	struct drm_dp_mst_branch *mstb = txmsg->dst;
1407 	u8 req_type;
1408 
1409 	/* both msg slots are full */
1410 	if (txmsg->seqno == -1) {
1411 		if (mstb->tx_slots[0] && mstb->tx_slots[1]) {
1412 			DRM_DEBUG_KMS("%s: failed to find slot\n", __func__);
1413 			return -EAGAIN;
1414 		}
1415 		if (mstb->tx_slots[0] == NULL && mstb->tx_slots[1] == NULL) {
1416 			txmsg->seqno = mstb->last_seqno;
1417 			mstb->last_seqno ^= 1;
1418 		} else if (mstb->tx_slots[0] == NULL)
1419 			txmsg->seqno = 0;
1420 		else
1421 			txmsg->seqno = 1;
1422 		mstb->tx_slots[txmsg->seqno] = txmsg;
1423 	}
1424 
1425 	req_type = txmsg->msg[0] & 0x7f;
1426 	if (req_type == DP_CONNECTION_STATUS_NOTIFY ||
1427 		req_type == DP_RESOURCE_STATUS_NOTIFY)
1428 		hdr->broadcast = 1;
1429 	else
1430 		hdr->broadcast = 0;
1431 	hdr->path_msg = txmsg->path_msg;
1432 	hdr->lct = mstb->lct;
1433 	hdr->lcr = mstb->lct - 1;
1434 	if (mstb->lct > 1)
1435 		memcpy(hdr->rad, mstb->rad, mstb->lct / 2);
1436 	hdr->seqno = txmsg->seqno;
1437 	return 0;
1438 }
1439 /*
1440  * process a single block of the next message in the sideband queue
1441  */
process_single_tx_qlock(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_sideband_msg_tx * txmsg,bool up)1442 static int process_single_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1443 				   struct drm_dp_sideband_msg_tx *txmsg,
1444 				   bool up)
1445 {
1446 	u8 chunk[48];
1447 	struct drm_dp_sideband_msg_hdr hdr;
1448 	int len, space, idx, tosend;
1449 	int ret;
1450 
1451 	memset(&hdr, 0, sizeof(struct drm_dp_sideband_msg_hdr));
1452 
1453 	if (txmsg->state == DRM_DP_SIDEBAND_TX_QUEUED) {
1454 		txmsg->seqno = -1;
1455 		txmsg->state = DRM_DP_SIDEBAND_TX_START_SEND;
1456 	}
1457 
1458 	/* make hdr from dst mst - for replies use seqno
1459 	   otherwise assign one */
1460 	ret = set_hdr_from_dst_qlock(&hdr, txmsg);
1461 	if (ret < 0)
1462 		return ret;
1463 
1464 	/* amount left to send in this message */
1465 	len = txmsg->cur_len - txmsg->cur_offset;
1466 
1467 	/* 48 - sideband msg size - 1 byte for data CRC, x header bytes */
1468 	space = 48 - 1 - drm_dp_calc_sb_hdr_size(&hdr);
1469 
1470 	tosend = min(len, space);
1471 	if (len == txmsg->cur_len)
1472 		hdr.somt = 1;
1473 	if (space >= len)
1474 		hdr.eomt = 1;
1475 
1476 
1477 	hdr.msg_len = tosend + 1;
1478 	drm_dp_encode_sideband_msg_hdr(&hdr, chunk, &idx);
1479 	memcpy(&chunk[idx], &txmsg->msg[txmsg->cur_offset], tosend);
1480 	/* add crc at end */
1481 	drm_dp_crc_sideband_chunk_req(&chunk[idx], tosend);
1482 	idx += tosend + 1;
1483 
1484 	ret = drm_dp_send_sideband_msg(mgr, up, chunk, idx);
1485 	if (ret) {
1486 		DRM_DEBUG_KMS("sideband msg failed to send\n");
1487 		return ret;
1488 	}
1489 
1490 	txmsg->cur_offset += tosend;
1491 	if (txmsg->cur_offset == txmsg->cur_len) {
1492 		txmsg->state = DRM_DP_SIDEBAND_TX_SENT;
1493 		return 1;
1494 	}
1495 	return 0;
1496 }
1497 
process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr * mgr)1498 static void process_single_down_tx_qlock(struct drm_dp_mst_topology_mgr *mgr)
1499 {
1500 	struct drm_dp_sideband_msg_tx *txmsg;
1501 	int ret;
1502 
1503 	WARN_ON(!mutex_is_locked(&mgr->qlock));
1504 
1505 	/* construct a chunk from the first msg in the tx_msg queue */
1506 	if (list_empty(&mgr->tx_msg_downq))
1507 		return;
1508 
1509 	txmsg = list_first_entry(&mgr->tx_msg_downq, struct drm_dp_sideband_msg_tx, next);
1510 	ret = process_single_tx_qlock(mgr, txmsg, false);
1511 	if (ret == 1) {
1512 		/* txmsg is sent it should be in the slots now */
1513 		list_del(&txmsg->next);
1514 	} else if (ret) {
1515 		DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1516 		list_del(&txmsg->next);
1517 		if (txmsg->seqno != -1)
1518 			txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1519 		txmsg->state = DRM_DP_SIDEBAND_TX_TIMEOUT;
1520 		wake_up(&mgr->tx_waitq);
1521 	}
1522 }
1523 
1524 /* called holding qlock */
process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_sideband_msg_tx * txmsg)1525 static void process_single_up_tx_qlock(struct drm_dp_mst_topology_mgr *mgr,
1526 				       struct drm_dp_sideband_msg_tx *txmsg)
1527 {
1528 	int ret;
1529 
1530 	/* construct a chunk from the first msg in the tx_msg queue */
1531 	ret = process_single_tx_qlock(mgr, txmsg, true);
1532 
1533 	if (ret != 1)
1534 		DRM_DEBUG_KMS("failed to send msg in q %d\n", ret);
1535 
1536 	txmsg->dst->tx_slots[txmsg->seqno] = NULL;
1537 }
1538 
drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_sideband_msg_tx * txmsg)1539 static void drm_dp_queue_down_tx(struct drm_dp_mst_topology_mgr *mgr,
1540 				 struct drm_dp_sideband_msg_tx *txmsg)
1541 {
1542 	mutex_lock(&mgr->qlock);
1543 	list_add_tail(&txmsg->next, &mgr->tx_msg_downq);
1544 	if (list_is_singular(&mgr->tx_msg_downq))
1545 		process_single_down_tx_qlock(mgr);
1546 	mutex_unlock(&mgr->qlock);
1547 }
1548 
drm_dp_send_link_address(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_branch * mstb)1549 static void drm_dp_send_link_address(struct drm_dp_mst_topology_mgr *mgr,
1550 				     struct drm_dp_mst_branch *mstb)
1551 {
1552 	int len;
1553 	struct drm_dp_sideband_msg_tx *txmsg;
1554 	int ret;
1555 
1556 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1557 	if (!txmsg)
1558 		return;
1559 
1560 	txmsg->dst = mstb;
1561 	len = build_link_address(txmsg);
1562 
1563 	mstb->link_address_sent = true;
1564 	drm_dp_queue_down_tx(mgr, txmsg);
1565 
1566 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1567 	if (ret > 0) {
1568 		int i;
1569 
1570 		if (txmsg->reply.reply_type == 1)
1571 			DRM_DEBUG_KMS("link address nak received\n");
1572 		else {
1573 			DRM_DEBUG_KMS("link address reply: %d\n", txmsg->reply.u.link_addr.nports);
1574 			for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1575 				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,
1576 				       txmsg->reply.u.link_addr.ports[i].input_port,
1577 				       txmsg->reply.u.link_addr.ports[i].peer_device_type,
1578 				       txmsg->reply.u.link_addr.ports[i].port_number,
1579 				       txmsg->reply.u.link_addr.ports[i].dpcd_revision,
1580 				       txmsg->reply.u.link_addr.ports[i].mcs,
1581 				       txmsg->reply.u.link_addr.ports[i].ddps,
1582 				       txmsg->reply.u.link_addr.ports[i].legacy_device_plug_status,
1583 				       txmsg->reply.u.link_addr.ports[i].num_sdp_streams,
1584 				       txmsg->reply.u.link_addr.ports[i].num_sdp_stream_sinks);
1585 			}
1586 
1587 			drm_dp_check_mstb_guid(mstb, txmsg->reply.u.link_addr.guid);
1588 
1589 			for (i = 0; i < txmsg->reply.u.link_addr.nports; i++) {
1590 				drm_dp_add_port(mstb, mgr->dev, &txmsg->reply.u.link_addr.ports[i]);
1591 			}
1592 			(*mgr->cbs->hotplug)(mgr);
1593 		}
1594 	} else {
1595 		mstb->link_address_sent = false;
1596 		DRM_DEBUG_KMS("link address failed %d\n", ret);
1597 	}
1598 
1599 	kfree(txmsg);
1600 }
1601 
drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_branch * mstb,struct drm_dp_mst_port * port)1602 static int drm_dp_send_enum_path_resources(struct drm_dp_mst_topology_mgr *mgr,
1603 					   struct drm_dp_mst_branch *mstb,
1604 					   struct drm_dp_mst_port *port)
1605 {
1606 	int len;
1607 	struct drm_dp_sideband_msg_tx *txmsg;
1608 	int ret;
1609 
1610 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1611 	if (!txmsg)
1612 		return -ENOMEM;
1613 
1614 	txmsg->dst = mstb;
1615 	len = build_enum_path_resources(txmsg, port->port_num);
1616 
1617 	drm_dp_queue_down_tx(mgr, txmsg);
1618 
1619 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1620 	if (ret > 0) {
1621 		if (txmsg->reply.reply_type == 1)
1622 			DRM_DEBUG_KMS("enum path resources nak received\n");
1623 		else {
1624 			if (port->port_num != txmsg->reply.u.path_resources.port_number)
1625 				DRM_ERROR("got incorrect port in response\n");
1626 			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,
1627 			       txmsg->reply.u.path_resources.avail_payload_bw_number);
1628 			port->available_pbn = txmsg->reply.u.path_resources.avail_payload_bw_number;
1629 		}
1630 	}
1631 
1632 	kfree(txmsg);
1633 	return 0;
1634 }
1635 
drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch * mstb)1636 static struct drm_dp_mst_port *drm_dp_get_last_connected_port_to_mstb(struct drm_dp_mst_branch *mstb)
1637 {
1638 	if (!mstb->port_parent)
1639 		return NULL;
1640 
1641 	if (mstb->port_parent->mstb != mstb)
1642 		return mstb->port_parent;
1643 
1644 	return drm_dp_get_last_connected_port_to_mstb(mstb->port_parent->parent);
1645 }
1646 
drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_branch * mstb,int * port_num)1647 static struct drm_dp_mst_branch *drm_dp_get_last_connected_port_and_mstb(struct drm_dp_mst_topology_mgr *mgr,
1648 									 struct drm_dp_mst_branch *mstb,
1649 									 int *port_num)
1650 {
1651 	struct drm_dp_mst_branch *rmstb = NULL;
1652 	struct drm_dp_mst_port *found_port;
1653 	mutex_lock(&mgr->lock);
1654 	if (mgr->mst_primary) {
1655 		found_port = drm_dp_get_last_connected_port_to_mstb(mstb);
1656 
1657 		if (found_port) {
1658 			rmstb = found_port->parent;
1659 			kref_get(&rmstb->kref);
1660 			*port_num = found_port->port_num;
1661 		}
1662 	}
1663 	mutex_unlock(&mgr->lock);
1664 	return rmstb;
1665 }
1666 
drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,int id,int pbn)1667 static int drm_dp_payload_send_msg(struct drm_dp_mst_topology_mgr *mgr,
1668 				   struct drm_dp_mst_port *port,
1669 				   int id,
1670 				   int pbn)
1671 {
1672 	struct drm_dp_sideband_msg_tx *txmsg;
1673 	struct drm_dp_mst_branch *mstb;
1674 	int len, ret, port_num;
1675 	u8 sinks[DRM_DP_MAX_SDP_STREAMS];
1676 	int i;
1677 
1678 	port = drm_dp_get_validated_port_ref(mgr, port);
1679 	if (!port)
1680 		return -EINVAL;
1681 
1682 	port_num = port->port_num;
1683 	mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1684 	if (!mstb) {
1685 		mstb = drm_dp_get_last_connected_port_and_mstb(mgr, port->parent, &port_num);
1686 
1687 		if (!mstb) {
1688 			drm_dp_put_port(port);
1689 			return -EINVAL;
1690 		}
1691 	}
1692 
1693 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1694 	if (!txmsg) {
1695 		ret = -ENOMEM;
1696 		goto fail_put;
1697 	}
1698 
1699 	for (i = 0; i < port->num_sdp_streams; i++)
1700 		sinks[i] = i;
1701 
1702 	txmsg->dst = mstb;
1703 	len = build_allocate_payload(txmsg, port_num,
1704 				     id,
1705 				     pbn, port->num_sdp_streams, sinks);
1706 
1707 	drm_dp_queue_down_tx(mgr, txmsg);
1708 
1709 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1710 	if (ret > 0) {
1711 		if (txmsg->reply.reply_type == 1) {
1712 			ret = -EINVAL;
1713 		} else
1714 			ret = 0;
1715 	}
1716 	kfree(txmsg);
1717 fail_put:
1718 	drm_dp_put_mst_branch_device(mstb);
1719 	drm_dp_put_port(port);
1720 	return ret;
1721 }
1722 
drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr * mgr,int id,struct drm_dp_payload * payload)1723 static int drm_dp_create_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1724 				       int id,
1725 				       struct drm_dp_payload *payload)
1726 {
1727 	int ret;
1728 
1729 	ret = drm_dp_dpcd_write_payload(mgr, id, payload);
1730 	if (ret < 0) {
1731 		payload->payload_state = 0;
1732 		return ret;
1733 	}
1734 	payload->payload_state = DP_PAYLOAD_LOCAL;
1735 	return 0;
1736 }
1737 
drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,int id,struct drm_dp_payload * payload)1738 static int drm_dp_create_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1739 				       struct drm_dp_mst_port *port,
1740 				       int id,
1741 				       struct drm_dp_payload *payload)
1742 {
1743 	int ret;
1744 	ret = drm_dp_payload_send_msg(mgr, port, id, port->vcpi.pbn);
1745 	if (ret < 0)
1746 		return ret;
1747 	payload->payload_state = DP_PAYLOAD_REMOTE;
1748 	return ret;
1749 }
1750 
drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,int id,struct drm_dp_payload * payload)1751 static int drm_dp_destroy_payload_step1(struct drm_dp_mst_topology_mgr *mgr,
1752 					struct drm_dp_mst_port *port,
1753 					int id,
1754 					struct drm_dp_payload *payload)
1755 {
1756 	DRM_DEBUG_KMS("\n");
1757 	/* its okay for these to fail */
1758 	if (port) {
1759 		drm_dp_payload_send_msg(mgr, port, id, 0);
1760 	}
1761 
1762 	drm_dp_dpcd_write_payload(mgr, id, payload);
1763 	payload->payload_state = DP_PAYLOAD_DELETE_LOCAL;
1764 	return 0;
1765 }
1766 
drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr * mgr,int id,struct drm_dp_payload * payload)1767 static int drm_dp_destroy_payload_step2(struct drm_dp_mst_topology_mgr *mgr,
1768 					int id,
1769 					struct drm_dp_payload *payload)
1770 {
1771 	payload->payload_state = 0;
1772 	return 0;
1773 }
1774 
1775 /**
1776  * drm_dp_update_payload_part1() - Execute payload update part 1
1777  * @mgr: manager to use.
1778  *
1779  * This iterates over all proposed virtual channels, and tries to
1780  * allocate space in the link for them. For 0->slots transitions,
1781  * this step just writes the VCPI to the MST device. For slots->0
1782  * transitions, this writes the updated VCPIs and removes the
1783  * remote VC payloads.
1784  *
1785  * after calling this the driver should generate ACT and payload
1786  * packets.
1787  */
drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr * mgr)1788 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr)
1789 {
1790 	int i, j;
1791 	int cur_slots = 1;
1792 	struct drm_dp_payload req_payload;
1793 	struct drm_dp_mst_port *port;
1794 
1795 	mutex_lock(&mgr->payload_lock);
1796 	for (i = 0; i < mgr->max_payloads; i++) {
1797 		/* solve the current payloads - compare to the hw ones
1798 		   - update the hw view */
1799 		req_payload.start_slot = cur_slots;
1800 		if (mgr->proposed_vcpis[i]) {
1801 			port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1802 			port = drm_dp_get_validated_port_ref(mgr, port);
1803 			if (!port) {
1804 				mutex_unlock(&mgr->payload_lock);
1805 				return -EINVAL;
1806 			}
1807 			req_payload.num_slots = mgr->proposed_vcpis[i]->num_slots;
1808 			req_payload.vcpi = mgr->proposed_vcpis[i]->vcpi;
1809 		} else {
1810 			port = NULL;
1811 			req_payload.num_slots = 0;
1812 		}
1813 
1814 		if (mgr->payloads[i].start_slot != req_payload.start_slot) {
1815 			mgr->payloads[i].start_slot = req_payload.start_slot;
1816 		}
1817 		/* work out what is required to happen with this payload */
1818 		if (mgr->payloads[i].num_slots != req_payload.num_slots) {
1819 
1820 			/* need to push an update for this payload */
1821 			if (req_payload.num_slots) {
1822 				drm_dp_create_payload_step1(mgr, mgr->proposed_vcpis[i]->vcpi, &req_payload);
1823 				mgr->payloads[i].num_slots = req_payload.num_slots;
1824 				mgr->payloads[i].vcpi = req_payload.vcpi;
1825 			} else if (mgr->payloads[i].num_slots) {
1826 				mgr->payloads[i].num_slots = 0;
1827 				drm_dp_destroy_payload_step1(mgr, port, mgr->payloads[i].vcpi, &mgr->payloads[i]);
1828 				req_payload.payload_state = mgr->payloads[i].payload_state;
1829 				mgr->payloads[i].start_slot = 0;
1830 			}
1831 			mgr->payloads[i].payload_state = req_payload.payload_state;
1832 		}
1833 		cur_slots += req_payload.num_slots;
1834 
1835 		if (port)
1836 			drm_dp_put_port(port);
1837 	}
1838 
1839 	for (i = 0; i < mgr->max_payloads; i++) {
1840 		if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1841 			DRM_DEBUG_KMS("removing payload %d\n", i);
1842 			for (j = i; j < mgr->max_payloads - 1; j++) {
1843 				memcpy(&mgr->payloads[j], &mgr->payloads[j + 1], sizeof(struct drm_dp_payload));
1844 				mgr->proposed_vcpis[j] = mgr->proposed_vcpis[j + 1];
1845 				if (mgr->proposed_vcpis[j] && mgr->proposed_vcpis[j]->num_slots) {
1846 					set_bit(j + 1, &mgr->payload_mask);
1847 				} else {
1848 					clear_bit(j + 1, &mgr->payload_mask);
1849 				}
1850 			}
1851 			memset(&mgr->payloads[mgr->max_payloads - 1], 0, sizeof(struct drm_dp_payload));
1852 			mgr->proposed_vcpis[mgr->max_payloads - 1] = NULL;
1853 			clear_bit(mgr->max_payloads, &mgr->payload_mask);
1854 
1855 		}
1856 	}
1857 	mutex_unlock(&mgr->payload_lock);
1858 
1859 	return 0;
1860 }
1861 EXPORT_SYMBOL(drm_dp_update_payload_part1);
1862 
1863 /**
1864  * drm_dp_update_payload_part2() - Execute payload update part 2
1865  * @mgr: manager to use.
1866  *
1867  * This iterates over all proposed virtual channels, and tries to
1868  * allocate space in the link for them. For 0->slots transitions,
1869  * this step writes the remote VC payload commands. For slots->0
1870  * this just resets some internal state.
1871  */
drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr * mgr)1872 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr)
1873 {
1874 	struct drm_dp_mst_port *port;
1875 	int i;
1876 	int ret = 0;
1877 	mutex_lock(&mgr->payload_lock);
1878 	for (i = 0; i < mgr->max_payloads; i++) {
1879 
1880 		if (!mgr->proposed_vcpis[i])
1881 			continue;
1882 
1883 		port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
1884 
1885 		DRM_DEBUG_KMS("payload %d %d\n", i, mgr->payloads[i].payload_state);
1886 		if (mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL) {
1887 			ret = drm_dp_create_payload_step2(mgr, port, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1888 		} else if (mgr->payloads[i].payload_state == DP_PAYLOAD_DELETE_LOCAL) {
1889 			ret = drm_dp_destroy_payload_step2(mgr, mgr->proposed_vcpis[i]->vcpi, &mgr->payloads[i]);
1890 		}
1891 		if (ret) {
1892 			mutex_unlock(&mgr->payload_lock);
1893 			return ret;
1894 		}
1895 	}
1896 	mutex_unlock(&mgr->payload_lock);
1897 	return 0;
1898 }
1899 EXPORT_SYMBOL(drm_dp_update_payload_part2);
1900 
1901 #if 0 /* unused as of yet */
1902 static int drm_dp_send_dpcd_read(struct drm_dp_mst_topology_mgr *mgr,
1903 				 struct drm_dp_mst_port *port,
1904 				 int offset, int size)
1905 {
1906 	int len;
1907 	struct drm_dp_sideband_msg_tx *txmsg;
1908 
1909 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1910 	if (!txmsg)
1911 		return -ENOMEM;
1912 
1913 	len = build_dpcd_read(txmsg, port->port_num, 0, 8);
1914 	txmsg->dst = port->parent;
1915 
1916 	drm_dp_queue_down_tx(mgr, txmsg);
1917 
1918 	return 0;
1919 }
1920 #endif
1921 
drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,int offset,int size,u8 * bytes)1922 static int drm_dp_send_dpcd_write(struct drm_dp_mst_topology_mgr *mgr,
1923 				  struct drm_dp_mst_port *port,
1924 				  int offset, int size, u8 *bytes)
1925 {
1926 	int len;
1927 	int ret;
1928 	struct drm_dp_sideband_msg_tx *txmsg;
1929 	struct drm_dp_mst_branch *mstb;
1930 
1931 	mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
1932 	if (!mstb)
1933 		return -EINVAL;
1934 
1935 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1936 	if (!txmsg) {
1937 		ret = -ENOMEM;
1938 		goto fail_put;
1939 	}
1940 
1941 	len = build_dpcd_write(txmsg, port->port_num, offset, size, bytes);
1942 	txmsg->dst = mstb;
1943 
1944 	drm_dp_queue_down_tx(mgr, txmsg);
1945 
1946 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
1947 	if (ret > 0) {
1948 		if (txmsg->reply.reply_type == 1) {
1949 			ret = -EINVAL;
1950 		} else
1951 			ret = 0;
1952 	}
1953 	kfree(txmsg);
1954 fail_put:
1955 	drm_dp_put_mst_branch_device(mstb);
1956 	return ret;
1957 }
1958 
drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx * msg,u8 req_type)1959 static int drm_dp_encode_up_ack_reply(struct drm_dp_sideband_msg_tx *msg, u8 req_type)
1960 {
1961 	struct drm_dp_sideband_msg_reply_body reply;
1962 
1963 	reply.reply_type = 0;
1964 	reply.req_type = req_type;
1965 	drm_dp_encode_sideband_reply(&reply, msg);
1966 	return 0;
1967 }
1968 
drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_branch * mstb,int req_type,int seqno,bool broadcast)1969 static int drm_dp_send_up_ack_reply(struct drm_dp_mst_topology_mgr *mgr,
1970 				    struct drm_dp_mst_branch *mstb,
1971 				    int req_type, int seqno, bool broadcast)
1972 {
1973 	struct drm_dp_sideband_msg_tx *txmsg;
1974 
1975 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
1976 	if (!txmsg)
1977 		return -ENOMEM;
1978 
1979 	txmsg->dst = mstb;
1980 	txmsg->seqno = seqno;
1981 	drm_dp_encode_up_ack_reply(txmsg, req_type);
1982 
1983 	mutex_lock(&mgr->qlock);
1984 
1985 	process_single_up_tx_qlock(mgr, txmsg);
1986 
1987 	mutex_unlock(&mgr->qlock);
1988 
1989 	kfree(txmsg);
1990 	return 0;
1991 }
1992 
drm_dp_get_vc_payload_bw(int dp_link_bw,int dp_link_count,int * out)1993 static bool drm_dp_get_vc_payload_bw(int dp_link_bw,
1994 				     int dp_link_count,
1995 				     int *out)
1996 {
1997 	switch (dp_link_bw) {
1998 	default:
1999 		DRM_DEBUG_KMS("invalid link bandwidth in DPCD: %x (link count: %d)\n",
2000 			      dp_link_bw, dp_link_count);
2001 		return false;
2002 
2003 	case DP_LINK_BW_1_62:
2004 		*out = 3 * dp_link_count;
2005 		break;
2006 	case DP_LINK_BW_2_7:
2007 		*out = 5 * dp_link_count;
2008 		break;
2009 	case DP_LINK_BW_5_4:
2010 		*out = 10 * dp_link_count;
2011 		break;
2012 	}
2013 	return true;
2014 }
2015 
2016 /**
2017  * drm_dp_mst_topology_mgr_set_mst() - Set the MST state for a topology manager
2018  * @mgr: manager to set state for
2019  * @mst_state: true to enable MST on this connector - false to disable.
2020  *
2021  * This is called by the driver when it detects an MST capable device plugged
2022  * into a DP MST capable port, or when a DP MST capable device is unplugged.
2023  */
drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr * mgr,bool mst_state)2024 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state)
2025 {
2026 	int ret = 0;
2027 	struct drm_dp_mst_branch *mstb = NULL;
2028 
2029 	mutex_lock(&mgr->lock);
2030 	if (mst_state == mgr->mst_state)
2031 		goto out_unlock;
2032 
2033 	mgr->mst_state = mst_state;
2034 	/* set the device into MST mode */
2035 	if (mst_state) {
2036 		WARN_ON(mgr->mst_primary);
2037 
2038 		/* get dpcd info */
2039 		ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2040 		if (ret != DP_RECEIVER_CAP_SIZE) {
2041 			DRM_DEBUG_KMS("failed to read DPCD\n");
2042 			goto out_unlock;
2043 		}
2044 
2045 		if (!drm_dp_get_vc_payload_bw(mgr->dpcd[1],
2046 					      mgr->dpcd[2] & DP_MAX_LANE_COUNT_MASK,
2047 					      &mgr->pbn_div)) {
2048 			ret = -EINVAL;
2049 			goto out_unlock;
2050 		}
2051 
2052 		mgr->total_pbn = 2560;
2053 		mgr->total_slots = DIV_ROUND_UP(mgr->total_pbn, mgr->pbn_div);
2054 		mgr->avail_slots = mgr->total_slots;
2055 
2056 		/* add initial branch device at LCT 1 */
2057 		mstb = drm_dp_add_mst_branch_device(1, NULL);
2058 		if (mstb == NULL) {
2059 			ret = -ENOMEM;
2060 			goto out_unlock;
2061 		}
2062 		mstb->mgr = mgr;
2063 
2064 		/* give this the main reference */
2065 		mgr->mst_primary = mstb;
2066 		kref_get(&mgr->mst_primary->kref);
2067 
2068 		ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2069 							 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2070 		if (ret < 0) {
2071 			goto out_unlock;
2072 		}
2073 
2074 		{
2075 			struct drm_dp_payload reset_pay;
2076 			reset_pay.start_slot = 0;
2077 			reset_pay.num_slots = 0x3f;
2078 			drm_dp_dpcd_write_payload(mgr, 0, &reset_pay);
2079 		}
2080 
2081 		queue_work(system_long_wq, &mgr->work);
2082 
2083 		ret = 0;
2084 	} else {
2085 		/* disable MST on the device */
2086 		mstb = mgr->mst_primary;
2087 		mgr->mst_primary = NULL;
2088 		/* this can fail if the device is gone */
2089 		drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL, 0);
2090 		ret = 0;
2091 		memset(mgr->payloads, 0, mgr->max_payloads * sizeof(struct drm_dp_payload));
2092 		mgr->payload_mask = 0;
2093 		set_bit(0, &mgr->payload_mask);
2094 		mgr->vcpi_mask = 0;
2095 	}
2096 
2097 out_unlock:
2098 	mutex_unlock(&mgr->lock);
2099 	if (mstb)
2100 		drm_dp_put_mst_branch_device(mstb);
2101 	return ret;
2102 
2103 }
2104 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_set_mst);
2105 
2106 /**
2107  * drm_dp_mst_topology_mgr_suspend() - suspend the MST manager
2108  * @mgr: manager to suspend
2109  *
2110  * This function tells the MST device that we can't handle UP messages
2111  * anymore. This should stop it from sending any since we are suspended.
2112  */
drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr * mgr)2113 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr)
2114 {
2115 	mutex_lock(&mgr->lock);
2116 	drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2117 			   DP_MST_EN | DP_UPSTREAM_IS_SRC);
2118 	mutex_unlock(&mgr->lock);
2119 	flush_work(&mgr->work);
2120 	flush_work(&mgr->destroy_connector_work);
2121 }
2122 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_suspend);
2123 
2124 /**
2125  * drm_dp_mst_topology_mgr_resume() - resume the MST manager
2126  * @mgr: manager to resume
2127  *
2128  * This will fetch DPCD and see if the device is still there,
2129  * if it is, it will rewrite the MSTM control bits, and return.
2130  *
2131  * if the device fails this returns -1, and the driver should do
2132  * a full MST reprobe, in case we were undocked.
2133  */
drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr * mgr)2134 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr)
2135 {
2136 	int ret = 0;
2137 
2138 	mutex_lock(&mgr->lock);
2139 
2140 	if (mgr->mst_primary) {
2141 		int sret;
2142 		u8 guid[16];
2143 
2144 		sret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, mgr->dpcd, DP_RECEIVER_CAP_SIZE);
2145 		if (sret != DP_RECEIVER_CAP_SIZE) {
2146 			DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2147 			ret = -1;
2148 			goto out_unlock;
2149 		}
2150 
2151 		ret = drm_dp_dpcd_writeb(mgr->aux, DP_MSTM_CTRL,
2152 					 DP_MST_EN | DP_UP_REQ_EN | DP_UPSTREAM_IS_SRC);
2153 		if (ret < 0) {
2154 			DRM_DEBUG_KMS("mst write failed - undocked during suspend?\n");
2155 			ret = -1;
2156 			goto out_unlock;
2157 		}
2158 
2159 		/* Some hubs forget their guids after they resume */
2160 		sret = drm_dp_dpcd_read(mgr->aux, DP_GUID, guid, 16);
2161 		if (sret != 16) {
2162 			DRM_DEBUG_KMS("dpcd read failed - undocked during suspend?\n");
2163 			ret = -1;
2164 			goto out_unlock;
2165 		}
2166 		drm_dp_check_mstb_guid(mgr->mst_primary, guid);
2167 
2168 		ret = 0;
2169 	} else
2170 		ret = -1;
2171 
2172 out_unlock:
2173 	mutex_unlock(&mgr->lock);
2174 	return ret;
2175 }
2176 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_resume);
2177 
drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr * mgr,bool up)2178 static bool drm_dp_get_one_sb_msg(struct drm_dp_mst_topology_mgr *mgr, bool up)
2179 {
2180 	int len;
2181 	u8 replyblock[32];
2182 	int replylen, origlen, curreply;
2183 	int ret;
2184 	struct drm_dp_sideband_msg_rx *msg;
2185 	int basereg = up ? DP_SIDEBAND_MSG_UP_REQ_BASE : DP_SIDEBAND_MSG_DOWN_REP_BASE;
2186 	msg = up ? &mgr->up_req_recv : &mgr->down_rep_recv;
2187 
2188 	len = min(mgr->max_dpcd_transaction_bytes, 16);
2189 	ret = drm_dp_dpcd_read(mgr->aux, basereg,
2190 			       replyblock, len);
2191 	if (ret != len) {
2192 		DRM_DEBUG_KMS("failed to read DPCD down rep %d %d\n", len, ret);
2193 		return false;
2194 	}
2195 	ret = drm_dp_sideband_msg_build(msg, replyblock, len, true);
2196 	if (!ret) {
2197 		DRM_DEBUG_KMS("sideband msg build failed %d\n", replyblock[0]);
2198 		return false;
2199 	}
2200 	replylen = msg->curchunk_len + msg->curchunk_hdrlen;
2201 
2202 	origlen = replylen;
2203 	replylen -= len;
2204 	curreply = len;
2205 	while (replylen > 0) {
2206 		len = min3(replylen, mgr->max_dpcd_transaction_bytes, 16);
2207 		ret = drm_dp_dpcd_read(mgr->aux, basereg + curreply,
2208 				    replyblock, len);
2209 		if (ret != len) {
2210 			DRM_DEBUG_KMS("failed to read a chunk (len %d, ret %d)\n",
2211 				      len, ret);
2212 			return false;
2213 		}
2214 
2215 		ret = drm_dp_sideband_msg_build(msg, replyblock, len, false);
2216 		if (!ret) {
2217 			DRM_DEBUG_KMS("failed to build sideband msg\n");
2218 			return false;
2219 		}
2220 
2221 		curreply += len;
2222 		replylen -= len;
2223 	}
2224 	return true;
2225 }
2226 
drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr * mgr)2227 static int drm_dp_mst_handle_down_rep(struct drm_dp_mst_topology_mgr *mgr)
2228 {
2229 	int ret = 0;
2230 
2231 	if (!drm_dp_get_one_sb_msg(mgr, false)) {
2232 		memset(&mgr->down_rep_recv, 0,
2233 		       sizeof(struct drm_dp_sideband_msg_rx));
2234 		return 0;
2235 	}
2236 
2237 	if (mgr->down_rep_recv.have_eomt) {
2238 		struct drm_dp_sideband_msg_tx *txmsg;
2239 		struct drm_dp_mst_branch *mstb;
2240 		int slot = -1;
2241 		mstb = drm_dp_get_mst_branch_device(mgr,
2242 						    mgr->down_rep_recv.initial_hdr.lct,
2243 						    mgr->down_rep_recv.initial_hdr.rad);
2244 
2245 		if (!mstb) {
2246 			DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->down_rep_recv.initial_hdr.lct);
2247 			memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2248 			return 0;
2249 		}
2250 
2251 		/* find the message */
2252 		slot = mgr->down_rep_recv.initial_hdr.seqno;
2253 		mutex_lock(&mgr->qlock);
2254 		txmsg = mstb->tx_slots[slot];
2255 		/* remove from slots */
2256 		mutex_unlock(&mgr->qlock);
2257 
2258 		if (!txmsg) {
2259 			DRM_DEBUG_KMS("Got MST reply with no msg %p %d %d %02x %02x\n",
2260 			       mstb,
2261 			       mgr->down_rep_recv.initial_hdr.seqno,
2262 			       mgr->down_rep_recv.initial_hdr.lct,
2263 				      mgr->down_rep_recv.initial_hdr.rad[0],
2264 				      mgr->down_rep_recv.msg[0]);
2265 			drm_dp_put_mst_branch_device(mstb);
2266 			memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2267 			return 0;
2268 		}
2269 
2270 		drm_dp_sideband_parse_reply(&mgr->down_rep_recv, &txmsg->reply);
2271 		if (txmsg->reply.reply_type == 1) {
2272 			DRM_DEBUG_KMS("Got NAK reply: req 0x%02x, reason 0x%02x, nak data 0x%02x\n", txmsg->reply.req_type, txmsg->reply.u.nak.reason, txmsg->reply.u.nak.nak_data);
2273 		}
2274 
2275 		memset(&mgr->down_rep_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2276 		drm_dp_put_mst_branch_device(mstb);
2277 
2278 		mutex_lock(&mgr->qlock);
2279 		txmsg->state = DRM_DP_SIDEBAND_TX_RX;
2280 		mstb->tx_slots[slot] = NULL;
2281 		mutex_unlock(&mgr->qlock);
2282 
2283 		wake_up(&mgr->tx_waitq);
2284 	}
2285 	return ret;
2286 }
2287 
drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr * mgr)2288 static int drm_dp_mst_handle_up_req(struct drm_dp_mst_topology_mgr *mgr)
2289 {
2290 	int ret = 0;
2291 
2292 	if (!drm_dp_get_one_sb_msg(mgr, true)) {
2293 		memset(&mgr->up_req_recv, 0,
2294 		       sizeof(struct drm_dp_sideband_msg_rx));
2295 		return 0;
2296 	}
2297 
2298 	if (mgr->up_req_recv.have_eomt) {
2299 		struct drm_dp_sideband_msg_req_body msg;
2300 		struct drm_dp_mst_branch *mstb = NULL;
2301 		bool seqno;
2302 
2303 		if (!mgr->up_req_recv.initial_hdr.broadcast) {
2304 			mstb = drm_dp_get_mst_branch_device(mgr,
2305 							    mgr->up_req_recv.initial_hdr.lct,
2306 							    mgr->up_req_recv.initial_hdr.rad);
2307 			if (!mstb) {
2308 				DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2309 				memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2310 				return 0;
2311 			}
2312 		}
2313 
2314 		seqno = mgr->up_req_recv.initial_hdr.seqno;
2315 		drm_dp_sideband_parse_req(&mgr->up_req_recv, &msg);
2316 
2317 		if (msg.req_type == DP_CONNECTION_STATUS_NOTIFY) {
2318 			drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2319 
2320 			if (!mstb)
2321 				mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.conn_stat.guid);
2322 
2323 			if (!mstb) {
2324 				DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2325 				memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2326 				return 0;
2327 			}
2328 
2329 			drm_dp_update_port(mstb, &msg.u.conn_stat);
2330 
2331 			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);
2332 			(*mgr->cbs->hotplug)(mgr);
2333 
2334 		} else if (msg.req_type == DP_RESOURCE_STATUS_NOTIFY) {
2335 			drm_dp_send_up_ack_reply(mgr, mgr->mst_primary, msg.req_type, seqno, false);
2336 			if (!mstb)
2337 				mstb = drm_dp_get_mst_branch_device_by_guid(mgr, msg.u.resource_stat.guid);
2338 
2339 			if (!mstb) {
2340 				DRM_DEBUG_KMS("Got MST reply from unknown device %d\n", mgr->up_req_recv.initial_hdr.lct);
2341 				memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2342 				return 0;
2343 			}
2344 
2345 			DRM_DEBUG_KMS("Got RSN: pn: %d avail_pbn %d\n", msg.u.resource_stat.port_number, msg.u.resource_stat.available_pbn);
2346 		}
2347 
2348 		if (mstb)
2349 			drm_dp_put_mst_branch_device(mstb);
2350 
2351 		memset(&mgr->up_req_recv, 0, sizeof(struct drm_dp_sideband_msg_rx));
2352 	}
2353 	return ret;
2354 }
2355 
2356 /**
2357  * drm_dp_mst_hpd_irq() - MST hotplug IRQ notify
2358  * @mgr: manager to notify irq for.
2359  * @esi: 4 bytes from SINK_COUNT_ESI
2360  * @handled: whether the hpd interrupt was consumed or not
2361  *
2362  * This should be called from the driver when it detects a short IRQ,
2363  * along with the value of the DEVICE_SERVICE_IRQ_VECTOR_ESI0. The
2364  * topology manager will process the sideband messages received as a result
2365  * of this.
2366  */
drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr * mgr,u8 * esi,bool * handled)2367 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled)
2368 {
2369 	int ret = 0;
2370 	int sc;
2371 	*handled = false;
2372 	sc = esi[0] & 0x3f;
2373 
2374 	if (sc != mgr->sink_count) {
2375 		mgr->sink_count = sc;
2376 		*handled = true;
2377 	}
2378 
2379 	if (esi[1] & DP_DOWN_REP_MSG_RDY) {
2380 		ret = drm_dp_mst_handle_down_rep(mgr);
2381 		*handled = true;
2382 	}
2383 
2384 	if (esi[1] & DP_UP_REQ_MSG_RDY) {
2385 		ret |= drm_dp_mst_handle_up_req(mgr);
2386 		*handled = true;
2387 	}
2388 
2389 	drm_dp_mst_kick_tx(mgr);
2390 	return ret;
2391 }
2392 EXPORT_SYMBOL(drm_dp_mst_hpd_irq);
2393 
2394 /**
2395  * drm_dp_mst_detect_port() - get connection status for an MST port
2396  * @connector: DRM connector for this port
2397  * @mgr: manager for this port
2398  * @port: unverified pointer to a port
2399  *
2400  * This returns the current connection state for a port. It validates the
2401  * port pointer still exists so the caller doesn't require a reference
2402  */
drm_dp_mst_detect_port(struct drm_connector * connector,struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port)2403 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector,
2404 						 struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2405 {
2406 	enum drm_connector_status status = connector_status_disconnected;
2407 
2408 	/* we need to search for the port in the mgr in case its gone */
2409 	port = drm_dp_get_validated_port_ref(mgr, port);
2410 	if (!port)
2411 		return connector_status_disconnected;
2412 
2413 	if (!port->ddps)
2414 		goto out;
2415 
2416 	switch (port->pdt) {
2417 	case DP_PEER_DEVICE_NONE:
2418 	case DP_PEER_DEVICE_MST_BRANCHING:
2419 		break;
2420 
2421 	case DP_PEER_DEVICE_SST_SINK:
2422 		status = connector_status_connected;
2423 		/* for logical ports - cache the EDID */
2424 		if (port->port_num >= 8 && !port->cached_edid) {
2425 			port->cached_edid = drm_get_edid(connector, &port->aux.ddc);
2426 		}
2427 		break;
2428 	case DP_PEER_DEVICE_DP_LEGACY_CONV:
2429 		if (port->ldps)
2430 			status = connector_status_connected;
2431 		break;
2432 	}
2433 out:
2434 	drm_dp_put_port(port);
2435 	return status;
2436 }
2437 EXPORT_SYMBOL(drm_dp_mst_detect_port);
2438 
2439 /**
2440  * drm_dp_mst_port_has_audio() - Check whether port has audio capability or not
2441  * @mgr: manager for this port
2442  * @port: unverified pointer to a port.
2443  *
2444  * This returns whether the port supports audio or not.
2445  */
drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port)2446 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
2447 					struct drm_dp_mst_port *port)
2448 {
2449 	bool ret = false;
2450 
2451 	port = drm_dp_get_validated_port_ref(mgr, port);
2452 	if (!port)
2453 		return ret;
2454 	ret = port->has_audio;
2455 	drm_dp_put_port(port);
2456 	return ret;
2457 }
2458 EXPORT_SYMBOL(drm_dp_mst_port_has_audio);
2459 
2460 /**
2461  * drm_dp_mst_get_edid() - get EDID for an MST port
2462  * @connector: toplevel connector to get EDID for
2463  * @mgr: manager for this port
2464  * @port: unverified pointer to a port.
2465  *
2466  * This returns an EDID for the port connected to a connector,
2467  * It validates the pointer still exists so the caller doesn't require a
2468  * reference.
2469  */
drm_dp_mst_get_edid(struct drm_connector * connector,struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port)2470 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2471 {
2472 	struct edid *edid = NULL;
2473 
2474 	/* we need to search for the port in the mgr in case its gone */
2475 	port = drm_dp_get_validated_port_ref(mgr, port);
2476 	if (!port)
2477 		return NULL;
2478 
2479 	if (port->cached_edid)
2480 		edid = drm_edid_duplicate(port->cached_edid);
2481 	else {
2482 		edid = drm_get_edid(connector, &port->aux.ddc);
2483 		drm_mode_connector_set_tile_property(connector);
2484 	}
2485 	port->has_audio = drm_detect_monitor_audio(edid);
2486 	drm_dp_put_port(port);
2487 	return edid;
2488 }
2489 EXPORT_SYMBOL(drm_dp_mst_get_edid);
2490 
2491 /**
2492  * drm_dp_find_vcpi_slots() - find slots for this PBN value
2493  * @mgr: manager to use
2494  * @pbn: payload bandwidth to convert into slots.
2495  */
drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr * mgr,int pbn)2496 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
2497 			   int pbn)
2498 {
2499 	int num_slots;
2500 
2501 	num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2502 
2503 	if (num_slots > mgr->avail_slots)
2504 		return -ENOSPC;
2505 	return num_slots;
2506 }
2507 EXPORT_SYMBOL(drm_dp_find_vcpi_slots);
2508 
drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_vcpi * vcpi,int pbn)2509 static int drm_dp_init_vcpi(struct drm_dp_mst_topology_mgr *mgr,
2510 			    struct drm_dp_vcpi *vcpi, int pbn)
2511 {
2512 	int num_slots;
2513 	int ret;
2514 
2515 	num_slots = DIV_ROUND_UP(pbn, mgr->pbn_div);
2516 
2517 	if (num_slots > mgr->avail_slots)
2518 		return -ENOSPC;
2519 
2520 	vcpi->pbn = pbn;
2521 	vcpi->aligned_pbn = num_slots * mgr->pbn_div;
2522 	vcpi->num_slots = num_slots;
2523 
2524 	ret = drm_dp_mst_assign_payload_id(mgr, vcpi);
2525 	if (ret < 0)
2526 		return ret;
2527 	return 0;
2528 }
2529 
2530 /**
2531  * drm_dp_mst_allocate_vcpi() - Allocate a virtual channel
2532  * @mgr: manager for this port
2533  * @port: port to allocate a virtual channel for.
2534  * @pbn: payload bandwidth number to request
2535  * @slots: returned number of slots for this PBN.
2536  */
drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,int pbn,int * slots)2537 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots)
2538 {
2539 	int ret;
2540 
2541 	port = drm_dp_get_validated_port_ref(mgr, port);
2542 	if (!port)
2543 		return false;
2544 
2545 	if (port->vcpi.vcpi > 0) {
2546 		DRM_DEBUG_KMS("payload: vcpi %d already allocated for pbn %d - requested pbn %d\n", port->vcpi.vcpi, port->vcpi.pbn, pbn);
2547 		if (pbn == port->vcpi.pbn) {
2548 			*slots = port->vcpi.num_slots;
2549 			drm_dp_put_port(port);
2550 			return true;
2551 		}
2552 	}
2553 
2554 	ret = drm_dp_init_vcpi(mgr, &port->vcpi, pbn);
2555 	if (ret) {
2556 		DRM_DEBUG_KMS("failed to init vcpi %d %d %d\n", DIV_ROUND_UP(pbn, mgr->pbn_div), mgr->avail_slots, ret);
2557 		goto out;
2558 	}
2559 	DRM_DEBUG_KMS("initing vcpi for %d %d\n", pbn, port->vcpi.num_slots);
2560 	*slots = port->vcpi.num_slots;
2561 
2562 	drm_dp_put_port(port);
2563 	return true;
2564 out:
2565 	return false;
2566 }
2567 EXPORT_SYMBOL(drm_dp_mst_allocate_vcpi);
2568 
drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port)2569 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2570 {
2571 	int slots = 0;
2572 	port = drm_dp_get_validated_port_ref(mgr, port);
2573 	if (!port)
2574 		return slots;
2575 
2576 	slots = port->vcpi.num_slots;
2577 	drm_dp_put_port(port);
2578 	return slots;
2579 }
2580 EXPORT_SYMBOL(drm_dp_mst_get_vcpi_slots);
2581 
2582 /**
2583  * drm_dp_mst_reset_vcpi_slots() - Reset number of slots to 0 for VCPI
2584  * @mgr: manager for this port
2585  * @port: unverified pointer to a port.
2586  *
2587  * This just resets the number of slots for the ports VCPI for later programming.
2588  */
drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port)2589 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2590 {
2591 	port = drm_dp_get_validated_port_ref(mgr, port);
2592 	if (!port)
2593 		return;
2594 	port->vcpi.num_slots = 0;
2595 	drm_dp_put_port(port);
2596 }
2597 EXPORT_SYMBOL(drm_dp_mst_reset_vcpi_slots);
2598 
2599 /**
2600  * drm_dp_mst_deallocate_vcpi() - deallocate a VCPI
2601  * @mgr: manager for this port
2602  * @port: unverified port to deallocate vcpi for
2603  */
drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port)2604 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port)
2605 {
2606 	port = drm_dp_get_validated_port_ref(mgr, port);
2607 	if (!port)
2608 		return;
2609 
2610 	drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2611 	port->vcpi.num_slots = 0;
2612 	port->vcpi.pbn = 0;
2613 	port->vcpi.aligned_pbn = 0;
2614 	port->vcpi.vcpi = 0;
2615 	drm_dp_put_port(port);
2616 }
2617 EXPORT_SYMBOL(drm_dp_mst_deallocate_vcpi);
2618 
drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr * mgr,int id,struct drm_dp_payload * payload)2619 static int drm_dp_dpcd_write_payload(struct drm_dp_mst_topology_mgr *mgr,
2620 				     int id, struct drm_dp_payload *payload)
2621 {
2622 	u8 payload_alloc[3], status;
2623 	int ret;
2624 	int retries = 0;
2625 
2626 	drm_dp_dpcd_writeb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,
2627 			   DP_PAYLOAD_TABLE_UPDATED);
2628 
2629 	payload_alloc[0] = id;
2630 	payload_alloc[1] = payload->start_slot;
2631 	payload_alloc[2] = payload->num_slots;
2632 
2633 	ret = drm_dp_dpcd_write(mgr->aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);
2634 	if (ret != 3) {
2635 		DRM_DEBUG_KMS("failed to write payload allocation %d\n", ret);
2636 		goto fail;
2637 	}
2638 
2639 retry:
2640 	ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2641 	if (ret < 0) {
2642 		DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2643 		goto fail;
2644 	}
2645 
2646 	if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {
2647 		retries++;
2648 		if (retries < 20) {
2649 			usleep_range(10000, 20000);
2650 			goto retry;
2651 		}
2652 		DRM_DEBUG_KMS("status not set after read payload table status %d\n", status);
2653 		ret = -EINVAL;
2654 		goto fail;
2655 	}
2656 	ret = 0;
2657 fail:
2658 	return ret;
2659 }
2660 
2661 
2662 /**
2663  * drm_dp_check_act_status() - Check ACT handled status.
2664  * @mgr: manager to use
2665  *
2666  * Check the payload status bits in the DPCD for ACT handled completion.
2667  */
drm_dp_check_act_status(struct drm_dp_mst_topology_mgr * mgr)2668 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr)
2669 {
2670 	u8 status;
2671 	int ret;
2672 	int count = 0;
2673 
2674 	do {
2675 		ret = drm_dp_dpcd_readb(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);
2676 
2677 		if (ret < 0) {
2678 			DRM_DEBUG_KMS("failed to read payload table status %d\n", ret);
2679 			goto fail;
2680 		}
2681 
2682 		if (status & DP_PAYLOAD_ACT_HANDLED)
2683 			break;
2684 		count++;
2685 		udelay(100);
2686 
2687 	} while (count < 30);
2688 
2689 	if (!(status & DP_PAYLOAD_ACT_HANDLED)) {
2690 		DRM_DEBUG_KMS("failed to get ACT bit %d after %d retries\n", status, count);
2691 		ret = -EINVAL;
2692 		goto fail;
2693 	}
2694 	return 0;
2695 fail:
2696 	return ret;
2697 }
2698 EXPORT_SYMBOL(drm_dp_check_act_status);
2699 
2700 /**
2701  * drm_dp_calc_pbn_mode() - Calculate the PBN for a mode.
2702  * @clock: dot clock for the mode
2703  * @bpp: bpp for the mode.
2704  *
2705  * This uses the formula in the spec to calculate the PBN value for a mode.
2706  */
drm_dp_calc_pbn_mode(int clock,int bpp)2707 int drm_dp_calc_pbn_mode(int clock, int bpp)
2708 {
2709 	u64 kbps;
2710 	s64 peak_kbps;
2711 	u32 numerator;
2712 	u32 denominator;
2713 
2714 	kbps = clock * bpp;
2715 
2716 	/*
2717 	 * margin 5300ppm + 300ppm ~ 0.6% as per spec, factor is 1.006
2718 	 * The unit of 54/64Mbytes/sec is an arbitrary unit chosen based on
2719 	 * common multiplier to render an integer PBN for all link rate/lane
2720 	 * counts combinations
2721 	 * calculate
2722 	 * peak_kbps *= (1006/1000)
2723 	 * peak_kbps *= (64/54)
2724 	 * peak_kbps *= 8    convert to bytes
2725 	 */
2726 
2727 	numerator = 64 * 1006;
2728 	denominator = 54 * 8 * 1000 * 1000;
2729 
2730 	kbps *= numerator;
2731 	peak_kbps = drm_fixp_from_fraction(kbps, denominator);
2732 
2733 	return drm_fixp2int_ceil(peak_kbps);
2734 }
2735 EXPORT_SYMBOL(drm_dp_calc_pbn_mode);
2736 
test_calc_pbn_mode(void)2737 static int test_calc_pbn_mode(void)
2738 {
2739 	int ret;
2740 	ret = drm_dp_calc_pbn_mode(154000, 30);
2741 	if (ret != 689) {
2742 		DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2743 				154000, 30, 689, ret);
2744 		return -EINVAL;
2745 	}
2746 	ret = drm_dp_calc_pbn_mode(234000, 30);
2747 	if (ret != 1047) {
2748 		DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2749 				234000, 30, 1047, ret);
2750 		return -EINVAL;
2751 	}
2752 	ret = drm_dp_calc_pbn_mode(297000, 24);
2753 	if (ret != 1063) {
2754 		DRM_ERROR("PBN calculation test failed - clock %d, bpp %d, expected PBN %d, actual PBN %d.\n",
2755 				297000, 24, 1063, ret);
2756 		return -EINVAL;
2757 	}
2758 	return 0;
2759 }
2760 
2761 /* we want to kick the TX after we've ack the up/down IRQs. */
drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr * mgr)2762 static void drm_dp_mst_kick_tx(struct drm_dp_mst_topology_mgr *mgr)
2763 {
2764 	queue_work(system_long_wq, &mgr->tx_work);
2765 }
2766 
drm_dp_mst_dump_mstb(struct seq_file * m,struct drm_dp_mst_branch * mstb)2767 static void drm_dp_mst_dump_mstb(struct seq_file *m,
2768 				 struct drm_dp_mst_branch *mstb)
2769 {
2770 	struct drm_dp_mst_port *port;
2771 	int tabs = mstb->lct;
2772 	char prefix[10];
2773 	int i;
2774 
2775 	for (i = 0; i < tabs; i++)
2776 		prefix[i] = '\t';
2777 	prefix[i] = '\0';
2778 
2779 	seq_printf(m, "%smst: %p, %d\n", prefix, mstb, mstb->num_ports);
2780 	list_for_each_entry(port, &mstb->ports, next) {
2781 		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);
2782 		if (port->mstb)
2783 			drm_dp_mst_dump_mstb(m, port->mstb);
2784 	}
2785 }
2786 
dump_dp_payload_table(struct drm_dp_mst_topology_mgr * mgr,char * buf)2787 static bool dump_dp_payload_table(struct drm_dp_mst_topology_mgr *mgr,
2788 				  char *buf)
2789 {
2790 	int ret;
2791 	int i;
2792 	for (i = 0; i < 4; i++) {
2793 		ret = drm_dp_dpcd_read(mgr->aux, DP_PAYLOAD_TABLE_UPDATE_STATUS + (i * 16), &buf[i * 16], 16);
2794 		if (ret != 16)
2795 			break;
2796 	}
2797 	if (i == 4)
2798 		return true;
2799 	return false;
2800 }
2801 
fetch_monitor_name(struct drm_dp_mst_topology_mgr * mgr,struct drm_dp_mst_port * port,char * name,int namelen)2802 static void fetch_monitor_name(struct drm_dp_mst_topology_mgr *mgr,
2803 			       struct drm_dp_mst_port *port, char *name,
2804 			       int namelen)
2805 {
2806 	struct edid *mst_edid;
2807 
2808 	mst_edid = drm_dp_mst_get_edid(port->connector, mgr, port);
2809 	drm_edid_get_monitor_name(mst_edid, name, namelen);
2810 }
2811 
2812 /**
2813  * drm_dp_mst_dump_topology(): dump topology to seq file.
2814  * @m: seq_file to dump output to
2815  * @mgr: manager to dump current topology for.
2816  *
2817  * helper to dump MST topology to a seq file for debugfs.
2818  */
drm_dp_mst_dump_topology(struct seq_file * m,struct drm_dp_mst_topology_mgr * mgr)2819 void drm_dp_mst_dump_topology(struct seq_file *m,
2820 			      struct drm_dp_mst_topology_mgr *mgr)
2821 {
2822 	int i;
2823 	struct drm_dp_mst_port *port;
2824 
2825 	mutex_lock(&mgr->lock);
2826 	if (mgr->mst_primary)
2827 		drm_dp_mst_dump_mstb(m, mgr->mst_primary);
2828 
2829 	/* dump VCPIs */
2830 	mutex_unlock(&mgr->lock);
2831 
2832 	mutex_lock(&mgr->payload_lock);
2833 	seq_printf(m, "vcpi: %lx %lx %d\n", mgr->payload_mask, mgr->vcpi_mask,
2834 		mgr->max_payloads);
2835 
2836 	for (i = 0; i < mgr->max_payloads; i++) {
2837 		if (mgr->proposed_vcpis[i]) {
2838 			char name[14];
2839 
2840 			port = container_of(mgr->proposed_vcpis[i], struct drm_dp_mst_port, vcpi);
2841 			fetch_monitor_name(mgr, port, name, sizeof(name));
2842 			seq_printf(m, "vcpi %d: %d %d %d sink name: %s\n", i,
2843 				   port->port_num, port->vcpi.vcpi,
2844 				   port->vcpi.num_slots,
2845 				   (*name != 0) ? name :  "Unknown");
2846 		} else
2847 			seq_printf(m, "vcpi %d:unused\n", i);
2848 	}
2849 	for (i = 0; i < mgr->max_payloads; i++) {
2850 		seq_printf(m, "payload %d: %d, %d, %d\n",
2851 			   i,
2852 			   mgr->payloads[i].payload_state,
2853 			   mgr->payloads[i].start_slot,
2854 			   mgr->payloads[i].num_slots);
2855 
2856 
2857 	}
2858 	mutex_unlock(&mgr->payload_lock);
2859 
2860 	mutex_lock(&mgr->lock);
2861 	if (mgr->mst_primary) {
2862 		u8 buf[64];
2863 		bool bret;
2864 		int ret;
2865 		ret = drm_dp_dpcd_read(mgr->aux, DP_DPCD_REV, buf, DP_RECEIVER_CAP_SIZE);
2866 		seq_printf(m, "dpcd: ");
2867 		for (i = 0; i < DP_RECEIVER_CAP_SIZE; i++)
2868 			seq_printf(m, "%02x ", buf[i]);
2869 		seq_printf(m, "\n");
2870 		ret = drm_dp_dpcd_read(mgr->aux, DP_FAUX_CAP, buf, 2);
2871 		seq_printf(m, "faux/mst: ");
2872 		for (i = 0; i < 2; i++)
2873 			seq_printf(m, "%02x ", buf[i]);
2874 		seq_printf(m, "\n");
2875 		ret = drm_dp_dpcd_read(mgr->aux, DP_MSTM_CTRL, buf, 1);
2876 		seq_printf(m, "mst ctrl: ");
2877 		for (i = 0; i < 1; i++)
2878 			seq_printf(m, "%02x ", buf[i]);
2879 		seq_printf(m, "\n");
2880 
2881 		/* dump the standard OUI branch header */
2882 		ret = drm_dp_dpcd_read(mgr->aux, DP_BRANCH_OUI, buf, DP_BRANCH_OUI_HEADER_SIZE);
2883 		seq_printf(m, "branch oui: ");
2884 		for (i = 0; i < 0x3; i++)
2885 			seq_printf(m, "%02x", buf[i]);
2886 		seq_printf(m, " devid: ");
2887 		for (i = 0x3; i < 0x8 && buf[i]; i++)
2888 			seq_printf(m, "%c", buf[i]);
2889 
2890 		seq_printf(m, " revision: hw: %x.%x sw: %x.%x", buf[0x9] >> 4, buf[0x9] & 0xf, buf[0xa], buf[0xb]);
2891 		seq_printf(m, "\n");
2892 		bret = dump_dp_payload_table(mgr, buf);
2893 		if (bret == true) {
2894 			seq_printf(m, "payload table: ");
2895 			for (i = 0; i < 63; i++)
2896 				seq_printf(m, "%02x ", buf[i]);
2897 			seq_printf(m, "\n");
2898 		}
2899 
2900 	}
2901 
2902 	mutex_unlock(&mgr->lock);
2903 
2904 }
2905 EXPORT_SYMBOL(drm_dp_mst_dump_topology);
2906 
drm_dp_tx_work(struct work_struct * work)2907 static void drm_dp_tx_work(struct work_struct *work)
2908 {
2909 	struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, tx_work);
2910 
2911 	mutex_lock(&mgr->qlock);
2912 	if (!list_empty(&mgr->tx_msg_downq))
2913 		process_single_down_tx_qlock(mgr);
2914 	mutex_unlock(&mgr->qlock);
2915 }
2916 
drm_dp_free_mst_port(struct kref * kref)2917 static void drm_dp_free_mst_port(struct kref *kref)
2918 {
2919 	struct drm_dp_mst_port *port = container_of(kref, struct drm_dp_mst_port, kref);
2920 	kref_put(&port->parent->kref, drm_dp_free_mst_branch_device);
2921 	kfree(port);
2922 }
2923 
drm_dp_destroy_connector_work(struct work_struct * work)2924 static void drm_dp_destroy_connector_work(struct work_struct *work)
2925 {
2926 	struct drm_dp_mst_topology_mgr *mgr = container_of(work, struct drm_dp_mst_topology_mgr, destroy_connector_work);
2927 	struct drm_dp_mst_port *port;
2928 	bool send_hotplug = false;
2929 	/*
2930 	 * Not a regular list traverse as we have to drop the destroy
2931 	 * connector lock before destroying the connector, to avoid AB->BA
2932 	 * ordering between this lock and the config mutex.
2933 	 */
2934 	for (;;) {
2935 		mutex_lock(&mgr->destroy_connector_lock);
2936 		port = list_first_entry_or_null(&mgr->destroy_connector_list, struct drm_dp_mst_port, next);
2937 		if (!port) {
2938 			mutex_unlock(&mgr->destroy_connector_lock);
2939 			break;
2940 		}
2941 		list_del(&port->next);
2942 		mutex_unlock(&mgr->destroy_connector_lock);
2943 
2944 		kref_init(&port->kref);
2945 		INIT_LIST_HEAD(&port->next);
2946 
2947 		mgr->cbs->destroy_connector(mgr, port->connector);
2948 
2949 		drm_dp_port_teardown_pdt(port, port->pdt);
2950 		port->pdt = DP_PEER_DEVICE_NONE;
2951 
2952 		if (!port->input && port->vcpi.vcpi > 0) {
2953 			drm_dp_mst_reset_vcpi_slots(mgr, port);
2954 			drm_dp_update_payload_part1(mgr);
2955 			drm_dp_mst_put_payload_id(mgr, port->vcpi.vcpi);
2956 		}
2957 
2958 		kref_put(&port->kref, drm_dp_free_mst_port);
2959 		send_hotplug = true;
2960 	}
2961 	if (send_hotplug)
2962 		(*mgr->cbs->hotplug)(mgr);
2963 }
2964 
2965 /**
2966  * drm_dp_mst_topology_mgr_init - initialise a topology manager
2967  * @mgr: manager struct to initialise
2968  * @dev: device providing this structure - for i2c addition.
2969  * @aux: DP helper aux channel to talk to this device
2970  * @max_dpcd_transaction_bytes: hw specific DPCD transaction limit
2971  * @max_payloads: maximum number of payloads this GPU can source
2972  * @conn_base_id: the connector object ID the MST device is connected to.
2973  *
2974  * Return 0 for success, or negative error code on failure
2975  */
drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr * mgr,struct device * dev,struct drm_dp_aux * aux,int max_dpcd_transaction_bytes,int max_payloads,int conn_base_id)2976 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
2977 				 struct device *dev, struct drm_dp_aux *aux,
2978 				 int max_dpcd_transaction_bytes,
2979 				 int max_payloads, int conn_base_id)
2980 {
2981 	mutex_init(&mgr->lock);
2982 	mutex_init(&mgr->qlock);
2983 	mutex_init(&mgr->payload_lock);
2984 	mutex_init(&mgr->destroy_connector_lock);
2985 	INIT_LIST_HEAD(&mgr->tx_msg_downq);
2986 	INIT_LIST_HEAD(&mgr->destroy_connector_list);
2987 	INIT_WORK(&mgr->work, drm_dp_mst_link_probe_work);
2988 	INIT_WORK(&mgr->tx_work, drm_dp_tx_work);
2989 	INIT_WORK(&mgr->destroy_connector_work, drm_dp_destroy_connector_work);
2990 	init_waitqueue_head(&mgr->tx_waitq);
2991 	mgr->dev = dev;
2992 	mgr->aux = aux;
2993 	mgr->max_dpcd_transaction_bytes = max_dpcd_transaction_bytes;
2994 	mgr->max_payloads = max_payloads;
2995 	mgr->conn_base_id = conn_base_id;
2996 	if (max_payloads + 1 > sizeof(mgr->payload_mask) * 8 ||
2997 	    max_payloads + 1 > sizeof(mgr->vcpi_mask) * 8)
2998 		return -EINVAL;
2999 	mgr->payloads = kcalloc(max_payloads, sizeof(struct drm_dp_payload), GFP_KERNEL);
3000 	if (!mgr->payloads)
3001 		return -ENOMEM;
3002 	mgr->proposed_vcpis = kcalloc(max_payloads, sizeof(struct drm_dp_vcpi *), GFP_KERNEL);
3003 	if (!mgr->proposed_vcpis)
3004 		return -ENOMEM;
3005 	set_bit(0, &mgr->payload_mask);
3006 	if (test_calc_pbn_mode() < 0)
3007 		DRM_ERROR("MST PBN self-test failed\n");
3008 
3009 	return 0;
3010 }
3011 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_init);
3012 
3013 /**
3014  * drm_dp_mst_topology_mgr_destroy() - destroy topology manager.
3015  * @mgr: manager to destroy
3016  */
drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr * mgr)3017 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr)
3018 {
3019 	flush_work(&mgr->work);
3020 	flush_work(&mgr->destroy_connector_work);
3021 	mutex_lock(&mgr->payload_lock);
3022 	kfree(mgr->payloads);
3023 	mgr->payloads = NULL;
3024 	kfree(mgr->proposed_vcpis);
3025 	mgr->proposed_vcpis = NULL;
3026 	mutex_unlock(&mgr->payload_lock);
3027 	mgr->dev = NULL;
3028 	mgr->aux = NULL;
3029 }
3030 EXPORT_SYMBOL(drm_dp_mst_topology_mgr_destroy);
3031 
3032 /* I2C device */
drm_dp_mst_i2c_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)3033 static int drm_dp_mst_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,
3034 			       int num)
3035 {
3036 	struct drm_dp_aux *aux = adapter->algo_data;
3037 	struct drm_dp_mst_port *port = container_of(aux, struct drm_dp_mst_port, aux);
3038 	struct drm_dp_mst_branch *mstb;
3039 	struct drm_dp_mst_topology_mgr *mgr = port->mgr;
3040 	unsigned int i;
3041 	bool reading = false;
3042 	struct drm_dp_sideband_msg_req_body msg;
3043 	struct drm_dp_sideband_msg_tx *txmsg = NULL;
3044 	int ret;
3045 
3046 	mstb = drm_dp_get_validated_mstb_ref(mgr, port->parent);
3047 	if (!mstb)
3048 		return -EREMOTEIO;
3049 
3050 	/* construct i2c msg */
3051 	/* see if last msg is a read */
3052 	if (msgs[num - 1].flags & I2C_M_RD)
3053 		reading = true;
3054 
3055 	if (!reading || (num - 1 > DP_REMOTE_I2C_READ_MAX_TRANSACTIONS)) {
3056 		DRM_DEBUG_KMS("Unsupported I2C transaction for MST device\n");
3057 		ret = -EIO;
3058 		goto out;
3059 	}
3060 
3061 	memset(&msg, 0, sizeof(msg));
3062 	msg.req_type = DP_REMOTE_I2C_READ;
3063 	msg.u.i2c_read.num_transactions = num - 1;
3064 	msg.u.i2c_read.port_number = port->port_num;
3065 	for (i = 0; i < num - 1; i++) {
3066 		msg.u.i2c_read.transactions[i].i2c_dev_id = msgs[i].addr;
3067 		msg.u.i2c_read.transactions[i].num_bytes = msgs[i].len;
3068 		msg.u.i2c_read.transactions[i].bytes = msgs[i].buf;
3069 	}
3070 	msg.u.i2c_read.read_i2c_device_id = msgs[num - 1].addr;
3071 	msg.u.i2c_read.num_bytes_read = msgs[num - 1].len;
3072 
3073 	txmsg = kzalloc(sizeof(*txmsg), GFP_KERNEL);
3074 	if (!txmsg) {
3075 		ret = -ENOMEM;
3076 		goto out;
3077 	}
3078 
3079 	txmsg->dst = mstb;
3080 	drm_dp_encode_sideband_req(&msg, txmsg);
3081 
3082 	drm_dp_queue_down_tx(mgr, txmsg);
3083 
3084 	ret = drm_dp_mst_wait_tx_reply(mstb, txmsg);
3085 	if (ret > 0) {
3086 
3087 		if (txmsg->reply.reply_type == 1) { /* got a NAK back */
3088 			ret = -EREMOTEIO;
3089 			goto out;
3090 		}
3091 		if (txmsg->reply.u.remote_i2c_read_ack.num_bytes != msgs[num - 1].len) {
3092 			ret = -EIO;
3093 			goto out;
3094 		}
3095 		memcpy(msgs[num - 1].buf, txmsg->reply.u.remote_i2c_read_ack.bytes, msgs[num - 1].len);
3096 		ret = num;
3097 	}
3098 out:
3099 	kfree(txmsg);
3100 	drm_dp_put_mst_branch_device(mstb);
3101 	return ret;
3102 }
3103 
drm_dp_mst_i2c_functionality(struct i2c_adapter * adapter)3104 static u32 drm_dp_mst_i2c_functionality(struct i2c_adapter *adapter)
3105 {
3106 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
3107 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
3108 	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
3109 	       I2C_FUNC_10BIT_ADDR;
3110 }
3111 
3112 static const struct i2c_algorithm drm_dp_mst_i2c_algo = {
3113 	.functionality = drm_dp_mst_i2c_functionality,
3114 	.master_xfer = drm_dp_mst_i2c_xfer,
3115 };
3116 
3117 /**
3118  * drm_dp_mst_register_i2c_bus() - register an I2C adapter for I2C-over-AUX
3119  * @aux: DisplayPort AUX channel
3120  *
3121  * Returns 0 on success or a negative error code on failure.
3122  */
drm_dp_mst_register_i2c_bus(struct drm_dp_aux * aux)3123 static int drm_dp_mst_register_i2c_bus(struct drm_dp_aux *aux)
3124 {
3125 	aux->ddc.algo = &drm_dp_mst_i2c_algo;
3126 	aux->ddc.algo_data = aux;
3127 	aux->ddc.retries = 3;
3128 
3129 	aux->ddc.class = I2C_CLASS_DDC;
3130 	aux->ddc.owner = THIS_MODULE;
3131 	aux->ddc.dev.parent = aux->dev;
3132 	aux->ddc.dev.of_node = aux->dev->of_node;
3133 
3134 	strlcpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),
3135 		sizeof(aux->ddc.name));
3136 
3137 	return i2c_add_adapter(&aux->ddc);
3138 }
3139 
3140 /**
3141  * drm_dp_mst_unregister_i2c_bus() - unregister an I2C-over-AUX adapter
3142  * @aux: DisplayPort AUX channel
3143  */
drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux * aux)3144 static void drm_dp_mst_unregister_i2c_bus(struct drm_dp_aux *aux)
3145 {
3146 	i2c_del_adapter(&aux->ddc);
3147 }
3148