xref: /openbsd/sys/dev/pci/drm/apple/dcp-internal.h (revision 48fd60f3)
1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io> */
3 
4 #ifndef __APPLE_DCP_INTERNAL_H__
5 #define __APPLE_DCP_INTERNAL_H__
6 
7 #include <linux/backlight.h>
8 #include <linux/device.h>
9 #include <linux/ioport.h>
10 #include <linux/mutex.h>
11 #include <linux/mux/consumer.h>
12 #include <linux/phy/phy.h>
13 #include <linux/platform_device.h>
14 #include <linux/scatterlist.h>
15 
16 #include "dptxep.h"
17 #include "iomfb.h"
18 #include "iomfb_v12_3.h"
19 #include "iomfb_v13_3.h"
20 
21 #define DCP_MAX_PLANES 2
22 
23 struct apple_dcp;
24 struct apple_dcp_afkep;
25 
26 enum dcp_firmware_version {
27 	DCP_FIRMWARE_UNKNOWN,
28 	DCP_FIRMWARE_V_12_3,
29 	DCP_FIRMWARE_V_13_5,
30 };
31 
32 enum {
33 	SYSTEM_ENDPOINT = 0x20,
34 	TEST_ENDPOINT = 0x21,
35 	DCP_EXPERT_ENDPOINT = 0x22,
36 	DISP0_ENDPOINT = 0x23,
37 	DPTX_ENDPOINT = 0x2a,
38 	HDCP_ENDPOINT = 0x2b,
39 	REMOTE_ALLOC_ENDPOINT = 0x2d,
40 	IOMFB_ENDPOINT = 0x37,
41 };
42 
43 /* Temporary backing for a chunked transfer via setDCPAVPropStart/Chunk/End */
44 struct dcp_chunks {
45 	size_t length;
46 	void *data;
47 };
48 
49 #define DCP_MAX_MAPPINGS (128) /* should be enough */
50 #define MAX_DISP_REGISTERS (7)
51 
52 struct dcp_mem_descriptor {
53 	size_t size;
54 	void *buf;
55 	dma_addr_t dva;
56 	struct sg_table map;
57 	u64 reg;
58 };
59 
60 /* Limit on call stack depth (arbitrary). Some nesting is required */
61 #define DCP_MAX_CALL_DEPTH 8
62 
63 typedef void (*dcp_callback_t)(struct apple_dcp *, void *, void *);
64 
65 struct dcp_channel {
66 	dcp_callback_t callbacks[DCP_MAX_CALL_DEPTH];
67 	void *cookies[DCP_MAX_CALL_DEPTH];
68 	void *output[DCP_MAX_CALL_DEPTH];
69 	u16 end[DCP_MAX_CALL_DEPTH];
70 
71 	/* Current depth of the call stack. Less than DCP_MAX_CALL_DEPTH */
72 	u8 depth;
73 };
74 
75 struct dcp_fb_reference {
76 	struct list_head head;
77 	struct drm_framebuffer *fb;
78 	u32 swap_id;
79 };
80 
81 #define MAX_NOTCH_HEIGHT 160
82 
83 struct dcp_brightness {
84 	struct backlight_device *bl_dev;
85 	u32 maximum;
86 	u32 dac;
87 	int nits;
88 	int scale;
89 	bool update;
90 };
91 
92 /** laptop/AiO integrated panel parameters from DT */
93 struct dcp_panel {
94 	/// panel width in millimeter
95 	int width_mm;
96 	/// panel height in millimeter
97 	int height_mm;
98 	/// panel has a mini-LED backlight
99 	bool has_mini_led;
100 };
101 
102 struct apple_dcp_hw_data {
103 	u32 num_dptx_ports;
104 };
105 
106 /* TODO: move IOMFB members to its own struct */
107 struct apple_dcp {
108 	struct device *dev;
109 	struct platform_device *piodma;
110 	struct iommu_domain *iommu_dom;
111 	struct apple_rtkit *rtk;
112 	struct apple_crtc *crtc;
113 	struct apple_connector *connector;
114 
115 	struct apple_dcp_hw_data hw;
116 
117 	/* firmware version and compatible firmware version */
118 	enum dcp_firmware_version fw_compat;
119 
120 	/* Coprocessor control register */
121 	void __iomem *coproc_reg;
122 
123 	/* DCP has crashed */
124 	bool crashed;
125 
126 	/************* IOMFB **************************************************
127 	 * everything below is mostly used inside IOMFB but it could make     *
128 	 * sense keep some of the the members in apple_dcp.                   *
129 	 **********************************************************************/
130 
131 	/* clock rate request by dcp in */
132 	struct clk *clk;
133 
134 	/* DCP shared memory */
135 	void *shmem;
136 
137 	/* Display registers mappable to the DCP */
138 	struct resource *disp_registers[MAX_DISP_REGISTERS];
139 	unsigned int nr_disp_registers;
140 
141 	struct resource disp_bw_scratch_res;
142 	struct resource disp_bw_doorbell_res;
143 	u32 disp_bw_scratch_index;
144 	u32 disp_bw_scratch_offset;
145 	u32 disp_bw_doorbell_index;
146 	u32 disp_bw_doorbell_offset;
147 
148 	u32 index;
149 
150 	/* Bitmap of memory descriptors used for mappings made by the DCP */
151 	DECLARE_BITMAP(memdesc_map, DCP_MAX_MAPPINGS);
152 
153 	/* Indexed table of memory descriptors */
154 	struct dcp_mem_descriptor memdesc[DCP_MAX_MAPPINGS];
155 
156 	struct dcp_channel ch_cmd, ch_oobcmd;
157 	struct dcp_channel ch_cb, ch_oobcb, ch_async, ch_oobasync;
158 
159 	/* iomfb EP callback handlers */
160 	const iomfb_cb_handler *cb_handlers;
161 
162 	/* Active chunked transfer. There can only be one at a time. */
163 	struct dcp_chunks chunks;
164 
165 	/* Queued swap. Owned by the DCP to avoid per-swap memory allocation */
166 	union {
167 		struct dcp_swap_submit_req_v12_3 v12_3;
168 		struct dcp_swap_submit_req_v13_3 v13_3;
169 	} swap;
170 
171 	/* swap id of the last completed swap */
172 	u32 last_swap_id;
173 
174 	/* Current display mode */
175 	bool during_modeset;
176 	bool valid_mode;
177 	struct dcp_set_digital_out_mode_req mode;
178 
179 	/* completion for active turning true */
180 	struct completion start_done;
181 
182 	/* Is the DCP booted? */
183 	bool active;
184 
185 	/* eDP display without DP-HDMI conversion */
186 	bool main_display;
187 
188 	/* clear all surfaces on init */
189 	bool surfaces_cleared;
190 
191 	/* Modes valid for the connected display */
192 	struct dcp_display_mode *modes;
193 	unsigned int nr_modes;
194 
195 	/* Attributes of the connector */
196 	int connector_type;
197 
198 	/* Attributes of the connected display */
199 	int width_mm, height_mm;
200 
201 	unsigned notch_height;
202 
203 	/* Workqueue for sending vblank events when a dcp swap is not possible */
204 	struct work_struct vblank_wq;
205 
206 	/* List of referenced drm_framebuffers which can be unreferenced
207 	 * on the next successfully completed swap.
208 	 */
209 	struct list_head swapped_out_fbs;
210 
211 	struct dcp_brightness brightness;
212 	/* Workqueue for updating the initial initial brightness */
213 	struct work_struct bl_register_wq;
214 	struct rwlock bl_register_mutex;
215 	/* Workqueue for updating the brightness */
216 	struct work_struct bl_update_wq;
217 
218 	/* integrated panel if present */
219 	struct dcp_panel panel;
220 
221 	struct apple_dcp_afkep *systemep;
222 	struct completion systemep_done;
223 
224 	struct apple_dcp_afkep *ibootep;
225 
226 	struct apple_dcp_afkep *dptxep;
227 
228 	struct dptx_port dptxport[2];
229 
230 	/* these fields are output port specific */
231 	struct phy *phy;
232 	struct mux_control *xbar;
233 
234 	struct gpio_desc *hdmi_hpd;
235 	struct gpio_desc *hdmi_pwren;
236 	struct gpio_desc *dp2hdmi_pwren;
237 
238 	struct rwlock hpd_mutex;
239 
240 	u32 dptx_phy;
241 	u32 dptx_die;
242 	int hdmi_hpd_irq;
243 };
244 
245 int dcp_backlight_register(struct apple_dcp *dcp);
246 int dcp_backlight_update(struct apple_dcp *dcp);
247 bool dcp_has_panel(struct apple_dcp *dcp);
248 
249 #define DCP_AUDIO_MAX_CHANS 15
250 
251 #endif /* __APPLE_DCP_INTERNAL_H__ */
252