1/*	$NetBSD: drm_dp_mst_helper.h,v 1.7 2021/12/18 23:45:45 riastradh Exp $	*/
2
3/*
4 * Copyright �� 2014 Red Hat.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission.  The copyright holders make no representations
13 * about the suitability of this software for any purpose.  It is provided "as
14 * is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24#ifndef _DRM_DP_MST_HELPER_H_
25#define _DRM_DP_MST_HELPER_H_
26
27#include <linux/kref.h>
28#include <linux/list.h>
29#include <linux/mutex.h>
30#include <linux/types.h>
31#include <linux/workqueue.h>
32#include <drm/drm_dp_helper.h>
33#include <drm/drm_atomic.h>
34
35#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
36#include <linux/stackdepot.h>
37#include <linux/timekeeping.h>
38
39enum drm_dp_mst_topology_ref_type {
40	DRM_DP_MST_TOPOLOGY_REF_GET,
41	DRM_DP_MST_TOPOLOGY_REF_PUT,
42};
43
44struct drm_dp_mst_topology_ref_history {
45	struct drm_dp_mst_topology_ref_entry {
46		enum drm_dp_mst_topology_ref_type type;
47		int count;
48		ktime_t ts_nsec;
49		depot_stack_handle_t backtrace;
50	} *entries;
51	int len;
52};
53#endif /* IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS) */
54
55struct drm_dp_mst_branch;
56
57/**
58 * struct drm_dp_vcpi - Virtual Channel Payload Identifier
59 * @vcpi: Virtual channel ID.
60 * @pbn: Payload Bandwidth Number for this channel
61 * @aligned_pbn: PBN aligned with slot size
62 * @num_slots: number of slots for this PBN
63 */
64struct drm_dp_vcpi {
65	int vcpi;
66	int pbn;
67	int aligned_pbn;
68	int num_slots;
69};
70
71/**
72 * struct drm_dp_mst_port - MST port
73 * @port_num: port number
74 * @input: if this port is an input port. Protected by
75 * &drm_dp_mst_topology_mgr.base.lock.
76 * @mcs: message capability status - DP 1.2 spec. Protected by
77 * &drm_dp_mst_topology_mgr.base.lock.
78 * @ddps: DisplayPort Device Plug Status - DP 1.2. Protected by
79 * &drm_dp_mst_topology_mgr.base.lock.
80 * @pdt: Peer Device Type. Protected by
81 * &drm_dp_mst_topology_mgr.base.lock.
82 * @ldps: Legacy Device Plug Status. Protected by
83 * &drm_dp_mst_topology_mgr.base.lock.
84 * @dpcd_rev: DPCD revision of device on this port. Protected by
85 * &drm_dp_mst_topology_mgr.base.lock.
86 * @num_sdp_streams: Number of simultaneous streams. Protected by
87 * &drm_dp_mst_topology_mgr.base.lock.
88 * @num_sdp_stream_sinks: Number of stream sinks. Protected by
89 * &drm_dp_mst_topology_mgr.base.lock.
90 * @available_pbn: Available bandwidth for this port. Protected by
91 * &drm_dp_mst_topology_mgr.base.lock.
92 * @next: link to next port on this branch device
93 * @aux: i2c aux transport to talk to device connected to this port, protected
94 * by &drm_dp_mst_topology_mgr.base.lock.
95 * @parent: branch device parent of this port
96 * @vcpi: Virtual Channel Payload info for this port.
97 * @connector: DRM connector this port is connected to. Protected by
98 * &drm_dp_mst_topology_mgr.base.lock.
99 * @mgr: topology manager this port lives under.
100 *
101 * This structure represents an MST port endpoint on a device somewhere
102 * in the MST topology.
103 */
104struct drm_dp_mst_port {
105	/**
106	 * @topology_kref: refcount for this port's lifetime in the topology,
107	 * only the DP MST helpers should need to touch this
108	 */
109	struct kref topology_kref;
110
111	/**
112	 * @malloc_kref: refcount for the memory allocation containing this
113	 * structure. See drm_dp_mst_get_port_malloc() and
114	 * drm_dp_mst_put_port_malloc().
115	 */
116	struct kref malloc_kref;
117
118#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
119	/**
120	 * @topology_ref_history: A history of each topology
121	 * reference/dereference. See CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS.
122	 */
123	struct drm_dp_mst_topology_ref_history topology_ref_history;
124#endif
125
126	u8 port_num;
127	bool input;
128	bool mcs;
129	bool ddps;
130	u8 pdt;
131	bool ldps;
132	u8 dpcd_rev;
133	u8 num_sdp_streams;
134	u8 num_sdp_stream_sinks;
135	uint16_t available_pbn;
136	struct list_head next;
137	/**
138	 * @mstb: the branch device connected to this port, if there is one.
139	 * This should be considered protected for reading by
140	 * &drm_dp_mst_topology_mgr.lock. There are two exceptions to this:
141	 * &drm_dp_mst_topology_mgr.up_req_work and
142	 * &drm_dp_mst_topology_mgr.work, which do not grab
143	 * &drm_dp_mst_topology_mgr.lock during reads but are the only
144	 * updaters of this list and are protected from writing concurrently
145	 * by &drm_dp_mst_topology_mgr.probe_lock.
146	 */
147	struct drm_dp_mst_branch *mstb;
148	struct drm_dp_aux aux; /* i2c bus for this port? */
149	struct drm_dp_mst_branch *parent;
150
151	struct drm_dp_vcpi vcpi;
152	struct drm_connector *connector;
153	struct drm_dp_mst_topology_mgr *mgr;
154
155	/**
156	 * @cached_edid: for DP logical ports - make tiling work by ensuring
157	 * that the EDID for all connectors is read immediately.
158	 */
159	struct edid *cached_edid;
160	/**
161	 * @has_audio: Tracks whether the sink connector to this port is
162	 * audio-capable.
163	 */
164	bool has_audio;
165
166	bool fec_capable;
167};
168
169/**
170 * struct drm_dp_mst_branch - MST branch device.
171 * @rad: Relative Address to talk to this branch device.
172 * @lct: Link count total to talk to this branch device.
173 * @num_ports: number of ports on the branch.
174 * @msg_slots: one bit per transmitted msg slot.
175 * @port_parent: pointer to the port parent, NULL if toplevel.
176 * @mgr: topology manager for this branch device.
177 * @tx_slots: transmission slots for this device.
178 * @last_seqno: last sequence number used to talk to this.
179 * @link_address_sent: if a link address message has been sent to this device yet.
180 * @guid: guid for DP 1.2 branch device. port under this branch can be
181 * identified by port #.
182 *
183 * This structure represents an MST branch device, there is one
184 * primary branch device at the root, along with any other branches connected
185 * to downstream port of parent branches.
186 */
187struct drm_dp_mst_branch {
188	/**
189	 * @topology_kref: refcount for this branch device's lifetime in the
190	 * topology, only the DP MST helpers should need to touch this
191	 */
192	struct kref topology_kref;
193
194	/**
195	 * @malloc_kref: refcount for the memory allocation containing this
196	 * structure. See drm_dp_mst_get_mstb_malloc() and
197	 * drm_dp_mst_put_mstb_malloc().
198	 */
199	struct kref malloc_kref;
200
201#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
202	/**
203	 * @topology_ref_history: A history of each topology
204	 * reference/dereference. See CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS.
205	 */
206	struct drm_dp_mst_topology_ref_history topology_ref_history;
207#endif
208
209	/**
210	 * @destroy_next: linked-list entry used by
211	 * drm_dp_delayed_destroy_work()
212	 */
213	struct list_head destroy_next;
214
215	u8 rad[8];
216	u8 lct;
217	int num_ports;
218
219	int msg_slots;
220	/**
221	 * @ports: the list of ports on this branch device. This should be
222	 * considered protected for reading by &drm_dp_mst_topology_mgr.lock.
223	 * There are two exceptions to this:
224	 * &drm_dp_mst_topology_mgr.up_req_work and
225	 * &drm_dp_mst_topology_mgr.work, which do not grab
226	 * &drm_dp_mst_topology_mgr.lock during reads but are the only
227	 * updaters of this list and are protected from updating the list
228	 * concurrently by @drm_dp_mst_topology_mgr.probe_lock
229	 */
230	struct list_head ports;
231
232	/* list of tx ops queue for this port */
233	struct drm_dp_mst_port *port_parent;
234	struct drm_dp_mst_topology_mgr *mgr;
235
236	/* slots are protected by mstb->mgr->qlock */
237	struct drm_dp_sideband_msg_tx *tx_slots[2];
238	int last_seqno;
239	bool link_address_sent;
240
241	/* global unique identifier to identify branch devices */
242	u8 guid[16];
243};
244
245
246/* sideband msg header - not bit struct */
247struct drm_dp_sideband_msg_hdr {
248	u8 lct;
249	u8 lcr;
250	u8 rad[8];
251	bool broadcast;
252	bool path_msg;
253	u8 msg_len;
254	bool somt;
255	bool eomt;
256	bool seqno;
257};
258
259struct drm_dp_nak_reply {
260	u8 guid[16];
261	u8 reason;
262	u8 nak_data;
263};
264
265struct drm_dp_link_address_ack_reply {
266	u8 guid[16];
267	u8 nports;
268	struct drm_dp_link_addr_reply_port {
269		bool input_port;
270		u8 peer_device_type;
271		u8 port_number;
272		bool mcs;
273		bool ddps;
274		bool legacy_device_plug_status;
275		u8 dpcd_revision;
276		u8 peer_guid[16];
277		u8 num_sdp_streams;
278		u8 num_sdp_stream_sinks;
279	} ports[16];
280};
281
282struct drm_dp_remote_dpcd_read_ack_reply {
283	u8 port_number;
284	u8 num_bytes;
285	u8 bytes[255];
286};
287
288struct drm_dp_remote_dpcd_write_ack_reply {
289	u8 port_number;
290};
291
292struct drm_dp_remote_dpcd_write_nak_reply {
293	u8 port_number;
294	u8 reason;
295	u8 bytes_written_before_failure;
296};
297
298struct drm_dp_remote_i2c_read_ack_reply {
299	u8 port_number;
300	u8 num_bytes;
301	u8 bytes[255];
302};
303
304struct drm_dp_remote_i2c_read_nak_reply {
305	u8 port_number;
306	u8 nak_reason;
307	u8 i2c_nak_transaction;
308};
309
310struct drm_dp_remote_i2c_write_ack_reply {
311	u8 port_number;
312};
313
314
315struct drm_dp_sideband_msg_rx {
316	u8 chunk[48];
317	u8 msg[256];
318	u8 curchunk_len;
319	u8 curchunk_idx; /* chunk we are parsing now */
320	u8 curchunk_hdrlen;
321	u8 curlen; /* total length of the msg */
322	bool have_somt;
323	bool have_eomt;
324	struct drm_dp_sideband_msg_hdr initial_hdr;
325};
326
327#define DRM_DP_MAX_SDP_STREAMS 16
328struct drm_dp_allocate_payload {
329	u8 port_number;
330	u8 number_sdp_streams;
331	u8 vcpi;
332	u16 pbn;
333	u8 sdp_stream_sink[DRM_DP_MAX_SDP_STREAMS];
334};
335
336struct drm_dp_allocate_payload_ack_reply {
337	u8 port_number;
338	u8 vcpi;
339	u16 allocated_pbn;
340};
341
342struct drm_dp_connection_status_notify {
343	u8 guid[16];
344	u8 port_number;
345	bool legacy_device_plug_status;
346	bool displayport_device_plug_status;
347	bool message_capability_status;
348	bool input_port;
349	u8 peer_device_type;
350};
351
352struct drm_dp_remote_dpcd_read {
353	u8 port_number;
354	u32 dpcd_address;
355	u8 num_bytes;
356};
357
358struct drm_dp_remote_dpcd_write {
359	u8 port_number;
360	u32 dpcd_address;
361	u8 num_bytes;
362	u8 *bytes;
363};
364
365#define DP_REMOTE_I2C_READ_MAX_TRANSACTIONS 4
366struct drm_dp_remote_i2c_read {
367	u8 num_transactions;
368	u8 port_number;
369	struct drm_dp_remote_i2c_read_tx {
370		u8 i2c_dev_id;
371		u8 num_bytes;
372		u8 *bytes;
373		u8 no_stop_bit;
374		u8 i2c_transaction_delay;
375	} transactions[DP_REMOTE_I2C_READ_MAX_TRANSACTIONS];
376	u8 read_i2c_device_id;
377	u8 num_bytes_read;
378};
379
380struct drm_dp_remote_i2c_write {
381	u8 port_number;
382	u8 write_i2c_device_id;
383	u8 num_bytes;
384	u8 *bytes;
385};
386
387/* this covers ENUM_RESOURCES, POWER_DOWN_PHY, POWER_UP_PHY */
388struct drm_dp_port_number_req {
389	u8 port_number;
390};
391
392struct drm_dp_enum_path_resources_ack_reply {
393	u8 port_number;
394	bool fec_capable;
395	u16 full_payload_bw_number;
396	u16 avail_payload_bw_number;
397};
398
399/* covers POWER_DOWN_PHY, POWER_UP_PHY */
400struct drm_dp_port_number_rep {
401	u8 port_number;
402};
403
404struct drm_dp_query_payload {
405	u8 port_number;
406	u8 vcpi;
407};
408
409struct drm_dp_resource_status_notify {
410	u8 port_number;
411	u8 guid[16];
412	u16 available_pbn;
413};
414
415struct drm_dp_query_payload_ack_reply {
416	u8 port_number;
417	u16 allocated_pbn;
418};
419
420struct drm_dp_sideband_msg_req_body {
421	u8 req_type;
422	union ack_req {
423		struct drm_dp_connection_status_notify conn_stat;
424		struct drm_dp_port_number_req port_num;
425		struct drm_dp_resource_status_notify resource_stat;
426
427		struct drm_dp_query_payload query_payload;
428		struct drm_dp_allocate_payload allocate_payload;
429
430		struct drm_dp_remote_dpcd_read dpcd_read;
431		struct drm_dp_remote_dpcd_write dpcd_write;
432
433		struct drm_dp_remote_i2c_read i2c_read;
434		struct drm_dp_remote_i2c_write i2c_write;
435	} u;
436};
437
438struct drm_dp_sideband_msg_reply_body {
439	u8 reply_type;
440	u8 req_type;
441	union ack_replies {
442		struct drm_dp_nak_reply nak;
443		struct drm_dp_link_address_ack_reply link_addr;
444		struct drm_dp_port_number_rep port_number;
445
446		struct drm_dp_enum_path_resources_ack_reply path_resources;
447		struct drm_dp_allocate_payload_ack_reply allocate_payload;
448		struct drm_dp_query_payload_ack_reply query_payload;
449
450		struct drm_dp_remote_dpcd_read_ack_reply remote_dpcd_read_ack;
451		struct drm_dp_remote_dpcd_write_ack_reply remote_dpcd_write_ack;
452		struct drm_dp_remote_dpcd_write_nak_reply remote_dpcd_write_nack;
453
454		struct drm_dp_remote_i2c_read_ack_reply remote_i2c_read_ack;
455		struct drm_dp_remote_i2c_read_nak_reply remote_i2c_read_nack;
456		struct drm_dp_remote_i2c_write_ack_reply remote_i2c_write_ack;
457	} u;
458};
459
460/* msg is queued to be put into a slot */
461#define DRM_DP_SIDEBAND_TX_QUEUED 0
462/* msg has started transmitting on a slot - still on msgq */
463#define DRM_DP_SIDEBAND_TX_START_SEND 1
464/* msg has finished transmitting on a slot - removed from msgq only in slot */
465#define DRM_DP_SIDEBAND_TX_SENT 2
466/* msg has received a response - removed from slot */
467#define DRM_DP_SIDEBAND_TX_RX 3
468#define DRM_DP_SIDEBAND_TX_TIMEOUT 4
469
470struct drm_dp_sideband_msg_tx {
471	u8 msg[256];
472	u8 chunk[48];
473	u8 cur_offset;
474	u8 cur_len;
475	struct drm_dp_mst_branch *dst;
476	struct list_head next;
477	int seqno;
478	int state;
479	bool path_msg;
480	struct drm_dp_sideband_msg_reply_body reply;
481};
482
483/* sideband msg handler */
484struct drm_dp_mst_topology_mgr;
485struct drm_dp_mst_topology_cbs {
486	/* create a connector for a port */
487	struct drm_connector *(*add_connector)(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port, const char *path);
488	void (*register_connector)(struct drm_connector *connector);
489	void (*destroy_connector)(struct drm_dp_mst_topology_mgr *mgr,
490				  struct drm_connector *connector);
491};
492
493#define DP_MAX_PAYLOAD (sizeof(unsigned long) * 8)
494
495#define DP_PAYLOAD_LOCAL 1
496#define DP_PAYLOAD_REMOTE 2
497#define DP_PAYLOAD_DELETE_LOCAL 3
498
499struct drm_dp_payload {
500	int payload_state;
501	int start_slot;
502	int num_slots;
503	int vcpi;
504};
505
506#define to_dp_mst_topology_state(x) container_of(x, struct drm_dp_mst_topology_state, base)
507
508struct drm_dp_vcpi_allocation {
509	struct drm_dp_mst_port *port;
510	int vcpi;
511	int pbn;
512	bool dsc_enabled;
513	struct list_head next;
514};
515
516struct drm_dp_mst_topology_state {
517	struct drm_private_state base;
518	struct list_head vcpis;
519	struct drm_dp_mst_topology_mgr *mgr;
520};
521
522#define to_dp_mst_topology_mgr(x) container_of(x, struct drm_dp_mst_topology_mgr, base)
523
524/**
525 * struct drm_dp_mst_topology_mgr - DisplayPort MST manager
526 *
527 * This struct represents the toplevel displayport MST topology manager.
528 * There should be one instance of this for every MST capable DP connector
529 * on the GPU.
530 */
531struct drm_dp_mst_topology_mgr {
532	/**
533	 * @base: Base private object for atomic
534	 */
535	struct drm_private_obj base;
536
537	/**
538	 * @dev: device pointer for adding i2c devices etc.
539	 */
540	struct drm_device *dev;
541	/**
542	 * @cbs: callbacks for connector addition and destruction.
543	 */
544	const struct drm_dp_mst_topology_cbs *cbs;
545	/**
546	 * @max_dpcd_transaction_bytes: maximum number of bytes to read/write
547	 * in one go.
548	 */
549	int max_dpcd_transaction_bytes;
550	/**
551	 * @aux: AUX channel for the DP MST connector this topolgy mgr is
552	 * controlling.
553	 */
554	struct drm_dp_aux *aux;
555	/**
556	 * @max_payloads: maximum number of payloads the GPU can generate.
557	 */
558	int max_payloads;
559	/**
560	 * @conn_base_id: DRM connector ID this mgr is connected to. Only used
561	 * to build the MST connector path value.
562	 */
563	int conn_base_id;
564
565	/**
566	 * @down_rep_recv: Message receiver state for down replies.
567	 */
568	struct drm_dp_sideband_msg_rx down_rep_recv;
569	/**
570	 * @up_req_recv: Message receiver state for up requests.
571	 */
572	struct drm_dp_sideband_msg_rx up_req_recv;
573
574	/**
575	 * @lock: protects @mst_state, @mst_primary, @dpcd, and
576	 * @payload_id_table_cleared.
577	 */
578	struct mutex lock;
579
580	/**
581	 * @probe_lock: Prevents @work and @up_req_work, the only writers of
582	 * &drm_dp_mst_port.mstb and &drm_dp_mst_branch.ports, from racing
583	 * while they update the topology.
584	 */
585	struct mutex probe_lock;
586
587	/**
588	 * @mst_state: If this manager is enabled for an MST capable port. False
589	 * if no MST sink/branch devices is connected.
590	 */
591	bool mst_state : 1;
592
593	/**
594	 * @payload_id_table_cleared: Whether or not we've cleared the payload
595	 * ID table for @mst_primary. Protected by @lock.
596	 */
597	bool payload_id_table_cleared : 1;
598
599	/**
600	 * @mst_primary: Pointer to the primary/first branch device.
601	 */
602	struct drm_dp_mst_branch *mst_primary;
603
604	/**
605	 * @dpcd: Cache of DPCD for primary port.
606	 */
607	u8 dpcd[DP_RECEIVER_CAP_SIZE];
608	/**
609	 * @sink_count: Sink count from DEVICE_SERVICE_IRQ_VECTOR_ESI0.
610	 */
611	u8 sink_count;
612	/**
613	 * @pbn_div: PBN to slots divisor.
614	 */
615	int pbn_div;
616
617	/**
618	 * @funcs: Atomic helper callbacks
619	 */
620	const struct drm_private_state_funcs *funcs;
621
622	/**
623	 * @qlock: protects @tx_msg_downq, the &drm_dp_mst_branch.txslost and
624	 * &drm_dp_sideband_msg_tx.state once they are queued
625	 */
626	struct mutex qlock;
627
628	/**
629	 * @is_waiting_for_dwn_reply: indicate whether is waiting for down reply
630	 */
631	bool is_waiting_for_dwn_reply;
632
633	/**
634	 * @tx_msg_downq: List of pending down replies.
635	 */
636	struct list_head tx_msg_downq;
637
638	/**
639	 * @payload_lock: Protect payload information.
640	 */
641	struct mutex payload_lock;
642	/**
643	 * @proposed_vcpis: Array of pointers for the new VCPI allocation. The
644	 * VCPI structure itself is &drm_dp_mst_port.vcpi.
645	 */
646	struct drm_dp_vcpi **proposed_vcpis;
647	/**
648	 * @payloads: Array of payloads.
649	 */
650	struct drm_dp_payload *payloads;
651	/**
652	 * @payload_mask: Elements of @payloads actually in use. Since
653	 * reallocation of active outputs isn't possible gaps can be created by
654	 * disabling outputs out of order compared to how they've been enabled.
655	 */
656	unsigned long payload_mask;
657	/**
658	 * @vcpi_mask: Similar to @payload_mask, but for @proposed_vcpis.
659	 */
660	unsigned long vcpi_mask;
661
662	/**
663	 * @tx_waitq: Wait to queue stall for the tx worker.
664	 */
665#ifdef __NetBSD__
666	drm_waitqueue_t tx_waitq;
667#else
668	wait_queue_head_t tx_waitq;
669#endif
670	/**
671	 * @work: Probe work.
672	 */
673	struct work_struct work;
674	/**
675	 * @tx_work: Sideband transmit worker. This can nest within the main
676	 * @work worker for each transaction @work launches.
677	 */
678	struct work_struct tx_work;
679
680	/**
681	 * @destroy_port_list: List of to be destroyed connectors.
682	 */
683	struct list_head destroy_port_list;
684	/**
685	 * @destroy_branch_device_list: List of to be destroyed branch
686	 * devices.
687	 */
688	struct list_head destroy_branch_device_list;
689	/**
690	 * @delayed_destroy_lock: Protects @destroy_port_list and
691	 * @destroy_branch_device_list.
692	 */
693	struct mutex delayed_destroy_lock;
694	/**
695	 * @delayed_destroy_work: Work item to destroy MST port and branch
696	 * devices, needed to avoid locking inversion.
697	 */
698	struct work_struct delayed_destroy_work;
699
700	/**
701	 * @up_req_list: List of pending up requests from the topology that
702	 * need to be processed, in chronological order.
703	 */
704	struct list_head up_req_list;
705	/**
706	 * @up_req_lock: Protects @up_req_list
707	 */
708	struct mutex up_req_lock;
709	/**
710	 * @up_req_work: Work item to process up requests received from the
711	 * topology. Needed to avoid blocking hotplug handling and sideband
712	 * transmissions.
713	 */
714	struct work_struct up_req_work;
715
716#if IS_ENABLED(CONFIG_DRM_DEBUG_DP_MST_TOPOLOGY_REFS)
717	/**
718	 * @topology_ref_history_lock: protects
719	 * &drm_dp_mst_port.topology_ref_history and
720	 * &drm_dp_mst_branch.topology_ref_history.
721	 */
722	struct mutex topology_ref_history_lock;
723#endif
724};
725
726int drm_dp_mst_topology_mgr_init(struct drm_dp_mst_topology_mgr *mgr,
727				 struct drm_device *dev, struct drm_dp_aux *aux,
728				 int max_dpcd_transaction_bytes,
729				 int max_payloads, int conn_base_id);
730
731void drm_dp_mst_topology_mgr_destroy(struct drm_dp_mst_topology_mgr *mgr);
732
733
734int drm_dp_mst_topology_mgr_set_mst(struct drm_dp_mst_topology_mgr *mgr, bool mst_state);
735
736
737int drm_dp_mst_hpd_irq(struct drm_dp_mst_topology_mgr *mgr, u8 *esi, bool *handled);
738
739
740int
741drm_dp_mst_detect_port(struct drm_connector *connector,
742		       struct drm_modeset_acquire_ctx *ctx,
743		       struct drm_dp_mst_topology_mgr *mgr,
744		       struct drm_dp_mst_port *port);
745
746bool drm_dp_mst_port_has_audio(struct drm_dp_mst_topology_mgr *mgr,
747					struct drm_dp_mst_port *port);
748struct edid *drm_dp_mst_get_edid(struct drm_connector *connector, struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port);
749
750
751int drm_dp_calc_pbn_mode(int clock, int bpp, bool dsc);
752
753bool drm_dp_mst_allocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
754			      struct drm_dp_mst_port *port, int pbn, int slots);
755
756int drm_dp_mst_get_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port);
757
758
759void drm_dp_mst_reset_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr, struct drm_dp_mst_port *port);
760
761
762void drm_dp_mst_deallocate_vcpi(struct drm_dp_mst_topology_mgr *mgr,
763				struct drm_dp_mst_port *port);
764
765
766int drm_dp_find_vcpi_slots(struct drm_dp_mst_topology_mgr *mgr,
767			   int pbn);
768
769
770int drm_dp_update_payload_part1(struct drm_dp_mst_topology_mgr *mgr);
771
772
773int drm_dp_update_payload_part2(struct drm_dp_mst_topology_mgr *mgr);
774
775int drm_dp_check_act_status(struct drm_dp_mst_topology_mgr *mgr);
776
777#ifdef CONFIG_DEBUG_FS
778void drm_dp_mst_dump_topology(struct seq_file *m,
779			      struct drm_dp_mst_topology_mgr *mgr);
780#endif
781
782void drm_dp_mst_topology_mgr_suspend(struct drm_dp_mst_topology_mgr *mgr);
783int __must_check
784drm_dp_mst_topology_mgr_resume(struct drm_dp_mst_topology_mgr *mgr,
785			       bool sync);
786
787ssize_t drm_dp_mst_dpcd_read(struct drm_dp_aux *aux,
788			     unsigned int offset, void *buffer, size_t size);
789ssize_t drm_dp_mst_dpcd_write(struct drm_dp_aux *aux,
790			      unsigned int offset, void *buffer, size_t size);
791
792int drm_dp_mst_connector_late_register(struct drm_connector *connector,
793				       struct drm_dp_mst_port *port);
794void drm_dp_mst_connector_early_unregister(struct drm_connector *connector,
795					   struct drm_dp_mst_port *port);
796
797struct drm_dp_mst_topology_state *drm_atomic_get_mst_topology_state(struct drm_atomic_state *state,
798								    struct drm_dp_mst_topology_mgr *mgr);
799int __must_check
800drm_dp_atomic_find_vcpi_slots(struct drm_atomic_state *state,
801			      struct drm_dp_mst_topology_mgr *mgr,
802			      struct drm_dp_mst_port *port, int pbn,
803			      int pbn_div);
804int drm_dp_mst_atomic_enable_dsc(struct drm_atomic_state *state,
805				 struct drm_dp_mst_port *port,
806				 int pbn, int pbn_div,
807				 bool enable);
808int __must_check
809drm_dp_mst_add_affected_dsc_crtcs(struct drm_atomic_state *state,
810				  struct drm_dp_mst_topology_mgr *mgr);
811int __must_check
812drm_dp_atomic_release_vcpi_slots(struct drm_atomic_state *state,
813				 struct drm_dp_mst_topology_mgr *mgr,
814				 struct drm_dp_mst_port *port);
815int drm_dp_send_power_updown_phy(struct drm_dp_mst_topology_mgr *mgr,
816				 struct drm_dp_mst_port *port, bool power_up);
817int __must_check drm_dp_mst_atomic_check(struct drm_atomic_state *state);
818
819void drm_dp_mst_get_port_malloc(struct drm_dp_mst_port *port);
820void drm_dp_mst_put_port_malloc(struct drm_dp_mst_port *port);
821
822struct drm_dp_aux *drm_dp_mst_dsc_aux_for_port(struct drm_dp_mst_port *port);
823
824extern const struct drm_private_state_funcs drm_dp_mst_topology_state_funcs;
825
826/**
827 * __drm_dp_mst_state_iter_get - private atomic state iterator function for
828 * macro-internal use
829 * @state: &struct drm_atomic_state pointer
830 * @mgr: pointer to the &struct drm_dp_mst_topology_mgr iteration cursor
831 * @old_state: optional pointer to the old &struct drm_dp_mst_topology_state
832 * iteration cursor
833 * @new_state: optional pointer to the new &struct drm_dp_mst_topology_state
834 * iteration cursor
835 * @i: int iteration cursor, for macro-internal use
836 *
837 * Used by for_each_oldnew_mst_mgr_in_state(),
838 * for_each_old_mst_mgr_in_state(), and for_each_new_mst_mgr_in_state(). Don't
839 * call this directly.
840 *
841 * Returns:
842 * True if the current &struct drm_private_obj is a &struct
843 * drm_dp_mst_topology_mgr, false otherwise.
844 */
845static inline bool
846__drm_dp_mst_state_iter_get(struct drm_atomic_state *state,
847			    struct drm_dp_mst_topology_mgr **mgr,
848			    struct drm_dp_mst_topology_state **old_state,
849			    struct drm_dp_mst_topology_state **new_state,
850			    int i)
851{
852	struct __drm_private_objs_state *objs_state = &state->private_objs[i];
853
854	if (objs_state->ptr->funcs != &drm_dp_mst_topology_state_funcs)
855		return false;
856
857	*mgr = to_dp_mst_topology_mgr(objs_state->ptr);
858	if (old_state)
859		*old_state = to_dp_mst_topology_state(objs_state->old_state);
860	if (new_state)
861		*new_state = to_dp_mst_topology_state(objs_state->new_state);
862
863	return true;
864}
865
866/**
867 * for_each_oldnew_mst_mgr_in_state - iterate over all DP MST topology
868 * managers in an atomic update
869 * @__state: &struct drm_atomic_state pointer
870 * @mgr: &struct drm_dp_mst_topology_mgr iteration cursor
871 * @old_state: &struct drm_dp_mst_topology_state iteration cursor for the old
872 * state
873 * @new_state: &struct drm_dp_mst_topology_state iteration cursor for the new
874 * state
875 * @__i: int iteration cursor, for macro-internal use
876 *
877 * This iterates over all DRM DP MST topology managers in an atomic update,
878 * tracking both old and new state. This is useful in places where the state
879 * delta needs to be considered, for example in atomic check functions.
880 */
881#define for_each_oldnew_mst_mgr_in_state(__state, mgr, old_state, new_state, __i) \
882	for ((__i) = 0; (__i) < (__state)->num_private_objs; (__i)++) \
883		for_each_if(__drm_dp_mst_state_iter_get((__state), &(mgr), &(old_state), &(new_state), (__i)))
884
885/**
886 * for_each_old_mst_mgr_in_state - iterate over all DP MST topology managers
887 * in an atomic update
888 * @__state: &struct drm_atomic_state pointer
889 * @mgr: &struct drm_dp_mst_topology_mgr iteration cursor
890 * @old_state: &struct drm_dp_mst_topology_state iteration cursor for the old
891 * state
892 * @__i: int iteration cursor, for macro-internal use
893 *
894 * This iterates over all DRM DP MST topology managers in an atomic update,
895 * tracking only the old state. This is useful in disable functions, where we
896 * need the old state the hardware is still in.
897 */
898#define for_each_old_mst_mgr_in_state(__state, mgr, old_state, __i) \
899	for ((__i) = 0; (__i) < (__state)->num_private_objs; (__i)++) \
900		for_each_if(__drm_dp_mst_state_iter_get((__state), &(mgr), &(old_state), NULL, (__i)))
901
902/**
903 * for_each_new_mst_mgr_in_state - iterate over all DP MST topology managers
904 * in an atomic update
905 * @__state: &struct drm_atomic_state pointer
906 * @mgr: &struct drm_dp_mst_topology_mgr iteration cursor
907 * @new_state: &struct drm_dp_mst_topology_state iteration cursor for the new
908 * state
909 * @__i: int iteration cursor, for macro-internal use
910 *
911 * This iterates over all DRM DP MST topology managers in an atomic update,
912 * tracking only the new state. This is useful in enable functions, where we
913 * need the new state the hardware should be in when the atomic commit
914 * operation has completed.
915 */
916#define for_each_new_mst_mgr_in_state(__state, mgr, new_state, __i) \
917	for ((__i) = 0; (__i) < (__state)->num_private_objs; (__i)++) \
918		for_each_if(__drm_dp_mst_state_iter_get((__state), &(mgr), NULL, &(new_state), (__i)))
919
920#endif
921