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