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 #ifndef _DRM_DP_MST_HELPER_H_
23 #define _DRM_DP_MST_HELPER_H_
24 
25 #include <linux/types.h>
26 #include <drm/drmP.h>
27 #include <drm/drm_dp_helper.h>
28 
29 struct drm_dp_mst_branch;
30 
31 /**
32  * struct drm_dp_vcpi - Virtual Channel Payload Identifier
33  * @vcpi: Virtual channel ID.
34  * @pbn: Payload Bandwidth Number for this channel
35  * @aligned_pbn: PBN aligned with slot size
36  * @num_slots: number of slots for this PBN
37  */
38 struct drm_dp_vcpi {
39 	int vcpi;
40 	int pbn;
41 	int aligned_pbn;
42 	int num_slots;
43 };
44 
45 /**
46  * struct drm_dp_mst_port - MST port
47  * @kref: reference count for this port.
48  * @port_num: port number
49  * @input: if this port is an input port.
50  * @mcs: message capability status - DP 1.2 spec.
51  * @ddps: DisplayPort Device Plug Status - DP 1.2
52  * @pdt: Peer Device Type
53  * @ldps: Legacy Device Plug Status
54  * @dpcd_rev: DPCD revision of device on this port
55  * @num_sdp_streams: Number of simultaneous streams
56  * @num_sdp_stream_sinks: Number of stream sinks
57  * @available_pbn: Available bandwidth for this port.
58  * @next: link to next port on this branch device
59  * @mstb: branch device attach below this port
60  * @aux: i2c aux transport to talk to device connected to this port.
61  * @parent: branch device parent of this port
62  * @vcpi: Virtual Channel Payload info for this port.
63  * @connector: DRM connector this port is connected to.
64  * @mgr: topology manager this port lives under.
65  *
66  * This structure represents an MST port endpoint on a device somewhere
67  * in the MST topology.
68  */
69 struct drm_dp_mst_port {
70 	struct kref kref;
71 
72 	u8 port_num;
73 	bool input;
74 	bool mcs;
75 	bool ddps;
76 	u8 pdt;
77 	bool ldps;
78 	u8 dpcd_rev;
79 	u8 num_sdp_streams;
80 	u8 num_sdp_stream_sinks;
81 	uint16_t available_pbn;
82 	struct list_head next;
83 	struct drm_dp_mst_branch *mstb; /* pointer to an mstb if this port has one */
84 	struct drm_dp_aux aux; /* i2c bus for this port? */
85 	struct drm_dp_mst_branch *parent;
86 
87 	struct drm_dp_vcpi vcpi;
88 	struct drm_connector *connector;
89 	struct drm_dp_mst_topology_mgr *mgr;
90 
91 	struct edid *cached_edid; /* for DP logical ports - make tiling work */
92 	bool has_audio;
93 };
94 
95 /**
96  * struct drm_dp_mst_branch - MST branch device.
97  * @kref: reference count for this port.
98  * @rad: Relative Address to talk to this branch device.
99  * @lct: Link count total to talk to this branch device.
100  * @num_ports: number of ports on the branch.
101  * @msg_slots: one bit per transmitted msg slot.
102  * @ports: linked list of ports on this branch.
103  * @port_parent: pointer to the port parent, NULL if toplevel.
104  * @mgr: topology manager for this branch device.
105  * @tx_slots: transmission slots for this device.
106  * @last_seqno: last sequence number used to talk to this.
107  * @link_address_sent: if a link address message has been sent to this device yet.
108  * @guid: guid for DP 1.2 branch device. port under this branch can be
109  * identified by port #.
110  *
111  * This structure represents an MST branch device, there is one
112  * primary branch device at the root, along with any other branches connected
113  * to downstream port of parent branches.
114  */
115 struct drm_dp_mst_branch {
116 	struct kref kref;
117 	u8 rad[8];
118 	u8 lct;
119 	int num_ports;
120 
121 	int msg_slots;
122 	struct list_head ports;
123 
124 	/* list of tx ops queue for this port */
125 	struct drm_dp_mst_port *port_parent;
126 	struct drm_dp_mst_topology_mgr *mgr;
127 
128 	/* slots are protected by mstb->mgr->qlock */
129 	struct drm_dp_sideband_msg_tx *tx_slots[2];
130 	int last_seqno;
131 	bool link_address_sent;
132 
133 	/* global unique identifier to identify branch devices */
134 	u8 guid[16];
135 };
136 
137 
138 /* sideband msg header - not bit struct */
139 struct drm_dp_sideband_msg_hdr {
140 	u8 lct;
141 	u8 lcr;
142 	u8 rad[8];
143 	bool broadcast;
144 	bool path_msg;
145 	u8 msg_len;
146 	bool somt;
147 	bool eomt;
148 	bool seqno;
149 };
150 
151 struct drm_dp_nak_reply {
152 	u8 guid[16];
153 	u8 reason;
154 	u8 nak_data;
155 };
156 
157 struct drm_dp_link_address_ack_reply {
158 	u8 guid[16];
159 	u8 nports;
160 	struct drm_dp_link_addr_reply_port {
161 		bool input_port;
162 		u8 peer_device_type;
163 		u8 port_number;
164 		bool mcs;
165 		bool ddps;
166 		bool legacy_device_plug_status;
167 		u8 dpcd_revision;
168 		u8 peer_guid[16];
169 		u8 num_sdp_streams;
170 		u8 num_sdp_stream_sinks;
171 	} ports[16];
172 };
173 
174 struct drm_dp_remote_dpcd_read_ack_reply {
175 	u8 port_number;
176 	u8 num_bytes;
177 	u8 bytes[255];
178 };
179 
180 struct drm_dp_remote_dpcd_write_ack_reply {
181 	u8 port_number;
182 };
183 
184 struct drm_dp_remote_dpcd_write_nak_reply {
185 	u8 port_number;
186 	u8 reason;
187 	u8 bytes_written_before_failure;
188 };
189 
190 struct drm_dp_remote_i2c_read_ack_reply {
191 	u8 port_number;
192 	u8 num_bytes;
193 	u8 bytes[255];
194 };
195 
196 struct drm_dp_remote_i2c_read_nak_reply {
197 	u8 port_number;
198 	u8 nak_reason;
199 	u8 i2c_nak_transaction;
200 };
201 
202 struct drm_dp_remote_i2c_write_ack_reply {
203 	u8 port_number;
204 };
205 
206 
207 struct drm_dp_sideband_msg_rx {
208 	u8 chunk[48];
209 	u8 msg[256];
210 	u8 curchunk_len;
211 	u8 curchunk_idx; /* chunk we are parsing now */
212 	u8 curchunk_hdrlen;
213 	u8 curlen; /* total length of the msg */
214 	bool have_somt;
215 	bool have_eomt;
216 	struct drm_dp_sideband_msg_hdr initial_hdr;
217 };
218 
219 #define DRM_DP_MAX_SDP_STREAMS 16
220 struct drm_dp_allocate_payload {
221 	u8 port_number;
222 	u8 number_sdp_streams;
223 	u8 vcpi;
224 	u16 pbn;
225 	u8 sdp_stream_sink[DRM_DP_MAX_SDP_STREAMS];
226 };
227 
228 struct drm_dp_allocate_payload_ack_reply {
229 	u8 port_number;
230 	u8 vcpi;
231 	u16 allocated_pbn;
232 };
233 
234 struct drm_dp_connection_status_notify {
235 	u8 guid[16];
236 	u8 port_number;
237 	bool legacy_device_plug_status;
238 	bool displayport_device_plug_status;
239 	bool message_capability_status;
240 	bool input_port;
241 	u8 peer_device_type;
242 };
243 
244 struct drm_dp_remote_dpcd_read {
245 	u8 port_number;
246 	u32 dpcd_address;
247 	u8 num_bytes;
248 };
249 
250 struct drm_dp_remote_dpcd_write {
251 	u8 port_number;
252 	u32 dpcd_address;
253 	u8 num_bytes;
254 	u8 *bytes;
255 };
256 
257 #define DP_REMOTE_I2C_READ_MAX_TRANSACTIONS 4
258 struct drm_dp_remote_i2c_read {
259 	u8 num_transactions;
260 	u8 port_number;
261 	struct {
262 		u8 i2c_dev_id;
263 		u8 num_bytes;
264 		u8 *bytes;
265 		u8 no_stop_bit;
266 		u8 i2c_transaction_delay;
267 	} transactions[DP_REMOTE_I2C_READ_MAX_TRANSACTIONS];
268 	u8 read_i2c_device_id;
269 	u8 num_bytes_read;
270 };
271 
272 struct drm_dp_remote_i2c_write {
273 	u8 port_number;
274 	u8 write_i2c_device_id;
275 	u8 num_bytes;
276 	u8 *bytes;
277 };
278 
279 /* this covers ENUM_RESOURCES, POWER_DOWN_PHY, POWER_UP_PHY */
280 struct drm_dp_port_number_req {
281 	u8 port_number;
282 };
283 
284 struct drm_dp_enum_path_resources_ack_reply {
285 	u8 port_number;
286 	u16 full_payload_bw_number;
287 	u16 avail_payload_bw_number;
288 };
289 
290 /* covers POWER_DOWN_PHY, POWER_UP_PHY */
291 struct drm_dp_port_number_rep {
292 	u8 port_number;
293 };
294 
295 struct drm_dp_query_payload {
296 	u8 port_number;
297 	u8 vcpi;
298 };
299 
300 struct drm_dp_resource_status_notify {
301 	u8 port_number;
302 	u8 guid[16];
303 	u16 available_pbn;
304 };
305 
306 struct drm_dp_query_payload_ack_reply {
307 	u8 port_number;
308 	u8 allocated_pbn;
309 };
310 
311 struct drm_dp_sideband_msg_req_body {
312 	u8 req_type;
313 	union ack_req {
314 		struct drm_dp_connection_status_notify conn_stat;
315 		struct drm_dp_port_number_req port_num;
316 		struct drm_dp_resource_status_notify resource_stat;
317 
318 		struct drm_dp_query_payload query_payload;
319 		struct drm_dp_allocate_payload allocate_payload;
320 
321 		struct drm_dp_remote_dpcd_read dpcd_read;
322 		struct drm_dp_remote_dpcd_write dpcd_write;
323 
324 		struct drm_dp_remote_i2c_read i2c_read;
325 		struct drm_dp_remote_i2c_write i2c_write;
326 	} u;
327 };
328 
329 struct drm_dp_sideband_msg_reply_body {
330 	u8 reply_type;
331 	u8 req_type;
332 	union ack_replies {
333 		struct drm_dp_nak_reply nak;
334 		struct drm_dp_link_address_ack_reply link_addr;
335 		struct drm_dp_port_number_rep port_number;
336 
337 		struct drm_dp_enum_path_resources_ack_reply path_resources;
338 		struct drm_dp_allocate_payload_ack_reply allocate_payload;
339 		struct drm_dp_query_payload_ack_reply query_payload;
340 
341 		struct drm_dp_remote_dpcd_read_ack_reply remote_dpcd_read_ack;
342 		struct drm_dp_remote_dpcd_write_ack_reply remote_dpcd_write_ack;
343 		struct drm_dp_remote_dpcd_write_nak_reply remote_dpcd_write_nack;
344 
345 		struct drm_dp_remote_i2c_read_ack_reply remote_i2c_read_ack;
346 		struct drm_dp_remote_i2c_read_nak_reply remote_i2c_read_nack;
347 		struct drm_dp_remote_i2c_write_ack_reply remote_i2c_write_ack;
348 	} u;
349 };
350 
351 /* msg is queued to be put into a slot */
352 #define DRM_DP_SIDEBAND_TX_QUEUED 0
353 /* msg has started transmitting on a slot - still on msgq */
354 #define DRM_DP_SIDEBAND_TX_START_SEND 1
355 /* msg has finished transmitting on a slot - removed from msgq only in slot */
356 #define DRM_DP_SIDEBAND_TX_SENT 2
357 /* msg has received a response - removed from slot */
358 #define DRM_DP_SIDEBAND_TX_RX 3
359 #define DRM_DP_SIDEBAND_TX_TIMEOUT 4
360 
361 struct drm_dp_sideband_msg_tx {
362 	u8 msg[256];
363 	u8 chunk[48];
364 	u8 cur_offset;
365 	u8 cur_len;
366 	struct drm_dp_mst_branch *dst;
367 	struct list_head next;
368 	int seqno;
369 	int state;
370 	bool path_msg;
371 	struct drm_dp_sideband_msg_reply_body reply;
372 };
373 
374 /* sideband msg handler */
375 struct drm_dp_mst_topology_mgr;
376 struct drm_dp_mst_topology_cbs {
377 	/* create a connector for a port */
378 	struct drm_connector *(*add_connector)(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, const char *path);
379 	void (*register_connector)(struct drm_connector *connector);
380 	void (*destroy_connector)(struct drm_dp_mst_topology_mgr *mgr,
381 				  struct drm_connector *connector);
382 	void (*hotplug)(struct drm_dp_mst_topology_mgr *mgr);
383 
384 };
385 
386 #define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
387 
388 #define DP_PAYLOAD_LOCAL 1
389 #define DP_PAYLOAD_REMOTE 2
390 #define DP_PAYLOAD_DELETE_LOCAL 3
391 
392 struct drm_dp_payload {
393 	int payload_state;
394 	int start_slot;
395 	int num_slots;
396 	int vcpi;
397 };
398 
399 /**
400  * struct drm_dp_mst_topology_mgr - DisplayPort MST manager
401  * @dev: device pointer for adding i2c devices etc.
402  * @cbs: callbacks for connector addition and destruction.
403  * @max_dpcd_transaction_bytes - maximum number of bytes to read/write in one go.
404  * @aux: aux channel for the DP connector.
405  * @max_payloads: maximum number of payloads the GPU can generate.
406  * @conn_base_id: DRM connector ID this mgr is connected to.
407  * @down_rep_recv: msg receiver state for down replies.
408  * @up_req_recv: msg receiver state for up requests.
409  * @lock: protects mst state, primary, dpcd.
410  * @mst_state: if this manager is enabled for an MST capable port.
411  * @mst_primary: pointer to the primary branch device.
412  * @dpcd: cache of DPCD for primary port.
413  * @pbn_div: PBN to slots divisor.
414  *
415  * This struct represents the toplevel displayport MST topology manager.
416  * There should be one instance of this for every MST capable DP connector
417  * on the GPU.
418  */
419 struct drm_dp_mst_topology_mgr {
420 
421 	struct device *dev;
422 	const struct drm_dp_mst_topology_cbs *cbs;
423 	int max_dpcd_transaction_bytes;
424 	struct drm_dp_aux *aux; /* auxch for this topology mgr to use */
425 	int max_payloads;
426 	int conn_base_id;
427 
428 	/* only ever accessed from the workqueue - which should be serialised */
429 	struct drm_dp_sideband_msg_rx down_rep_recv;
430 	struct drm_dp_sideband_msg_rx up_req_recv;
431 
432 	/* pointer to info about the initial MST device */
433 	struct lock lock; /* protects mst_state + primary + dpcd */
434 
435 	bool mst_state;
436 	struct drm_dp_mst_branch *mst_primary;
437 
438 	u8 dpcd[DP_RECEIVER_CAP_SIZE];
439 	u8 sink_count;
440 	int pbn_div;
441 	int total_slots;
442 	int avail_slots;
443 	int total_pbn;
444 
445 	/* messages to be transmitted */
446 	/* qlock protects the upq/downq and in_progress,
447 	   the mstb tx_slots and txmsg->state once they are queued */
448 	struct lock qlock;
449 	struct list_head tx_msg_downq;
450 	bool tx_down_in_progress;
451 
452 	/* payload info + lock for it */
453 	struct lock payload_lock;
454 	struct drm_dp_vcpi **proposed_vcpis;
455 	struct drm_dp_payload *payloads;
456 	unsigned long payload_mask;
457 	unsigned long vcpi_mask;
458 
459 	wait_queue_head_t tx_waitq;
460 	struct work_struct work;
461 
462 	struct work_struct tx_work;
463 
464 	struct list_head destroy_connector_list;
465 	struct lock destroy_connector_lock;
466 	struct work_struct destroy_connector_work;
467 };
468 
469 int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr, struct device *dev, struct drm_dp_aux *aux, int max_dpcd_transaction_bytes, int max_payloads, int conn_base_id);
470 
471 void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr);
472 
473 
474 int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state);
475 
476 
477 int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled);
478 
479 
480 enum drm_connector_status drm_dp_mst_detect_port(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port);
481 
482 bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
483 					struct drm_dp_mst_port *port);
484 struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port);
485 
486 
487 int drm_dp_calc_pbn_mode(int clock, int bpp);
488 
489 
490 bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, int pbn, int *slots);
491 
492 int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port);
493 
494 
495 void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port);
496 
497 
498 void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
499 				struct drm_dp_mst_port *port);
500 
501 
502 int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
503 			   int pbn);
504 
505 
506 int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr);
507 
508 
509 int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr);
510 
511 int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr);
512 
513 void drm_dp_mst_dump_topology(struct seq_file *m,
514 			      struct drm_dp_mst_topology_mgr *mgr);
515 
516 void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr);
517 int drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr);
518 #endif
519