1 /*
2  * Copyright 2009 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24 
25 #include <drm/display/drm_dp_helper.h>
26 
27 #include "nouveau_drv.h"
28 #include "nouveau_connector.h"
29 #include "nouveau_encoder.h"
30 #include "nouveau_crtc.h"
31 
32 #include <nvif/if0011.h>
33 
34 MODULE_PARM_DESC(mst, "Enable DisplayPort multi-stream (default: enabled)");
35 static int nouveau_mst = 1;
36 module_param_named(mst, nouveau_mst, int, 0400);
37 
38 static bool
39 nouveau_dp_has_sink_count(struct drm_connector *connector,
40 			  struct nouveau_encoder *outp)
41 {
42 	return drm_dp_read_sink_count_cap(connector, outp->dp.dpcd, &outp->dp.desc);
43 }
44 
45 static enum drm_connector_status
46 nouveau_dp_probe_dpcd(struct nouveau_connector *nv_connector,
47 		      struct nouveau_encoder *outp)
48 {
49 	struct drm_connector *connector = &nv_connector->base;
50 	struct drm_dp_aux *aux = &nv_connector->aux;
51 	struct nv50_mstm *mstm = NULL;
52 	enum drm_connector_status status = connector_status_disconnected;
53 	int ret;
54 	u8 *dpcd = outp->dp.dpcd;
55 
56 	ret = drm_dp_read_dpcd_caps(aux, dpcd);
57 	if (ret < 0)
58 		goto out;
59 
60 	ret = drm_dp_read_desc(aux, &outp->dp.desc, drm_dp_is_branch(dpcd));
61 	if (ret < 0)
62 		goto out;
63 
64 	if (nouveau_mst) {
65 		mstm = outp->dp.mstm;
66 		if (mstm)
67 			mstm->can_mst = drm_dp_read_mst_cap(aux, dpcd);
68 	}
69 
70 	if (nouveau_dp_has_sink_count(connector, outp)) {
71 		ret = drm_dp_read_sink_count(aux);
72 		if (ret < 0)
73 			goto out;
74 
75 		outp->dp.sink_count = ret;
76 
77 		/*
78 		 * Dongle connected, but no display. Don't bother reading
79 		 * downstream port info
80 		 */
81 		if (!outp->dp.sink_count)
82 			return connector_status_disconnected;
83 	}
84 
85 	ret = drm_dp_read_downstream_info(aux, dpcd,
86 					  outp->dp.downstream_ports);
87 	if (ret < 0)
88 		goto out;
89 
90 	status = connector_status_connected;
91 out:
92 	if (status != connector_status_connected) {
93 		/* Clear any cached info */
94 		outp->dp.sink_count = 0;
95 	}
96 	return status;
97 }
98 
99 int
100 nouveau_dp_detect(struct nouveau_connector *nv_connector,
101 		  struct nouveau_encoder *nv_encoder)
102 {
103 	struct drm_device *dev = nv_encoder->base.base.dev;
104 	struct nouveau_drm *drm = nouveau_drm(dev);
105 	struct drm_connector *connector = &nv_connector->base;
106 	struct nv50_mstm *mstm = nv_encoder->dp.mstm;
107 	enum drm_connector_status status;
108 	u8 *dpcd = nv_encoder->dp.dpcd;
109 	int ret = NOUVEAU_DP_NONE, hpd;
110 
111 	/* eDP ports don't support hotplugging - so there's no point in probing eDP ports unless we
112 	 * haven't probed them once before.
113 	 */
114 	if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
115 		if (connector->status == connector_status_connected)
116 			return NOUVEAU_DP_SST;
117 		else if (connector->status == connector_status_disconnected)
118 			return NOUVEAU_DP_NONE;
119 	}
120 
121 	mutex_lock(&nv_encoder->dp.hpd_irq_lock);
122 	if (mstm) {
123 		/* If we're not ready to handle MST state changes yet, just
124 		 * report the last status of the connector. We'll reprobe it
125 		 * once we've resumed.
126 		 */
127 		if (mstm->suspended) {
128 			if (mstm->is_mst)
129 				ret = NOUVEAU_DP_MST;
130 			else if (connector->status ==
131 				 connector_status_connected)
132 				ret = NOUVEAU_DP_SST;
133 
134 			goto out;
135 		}
136 	}
137 
138 	/* Check status of HPD pin before attempting an AUX transaction that
139 	 * would result in a number of (futile) retries on a connector which
140 	 * has no display plugged.
141 	 *
142 	 * TODO: look into checking this before probing I2C to detect DVI/HDMI
143 	 */
144 	hpd = nvif_conn_hpd_status(&nv_connector->conn);
145 	if (hpd == NVIF_CONN_HPD_STATUS_NOT_PRESENT) {
146 		nvif_outp_dp_aux_pwr(&nv_encoder->outp, false);
147 		goto out;
148 	}
149 	nvif_outp_dp_aux_pwr(&nv_encoder->outp, true);
150 
151 	status = nouveau_dp_probe_dpcd(nv_connector, nv_encoder);
152 	if (status == connector_status_disconnected) {
153 		nvif_outp_dp_aux_pwr(&nv_encoder->outp, false);
154 		goto out;
155 	}
156 
157 	/* If we're in MST mode, we're done here */
158 	if (mstm && mstm->can_mst && mstm->is_mst) {
159 		ret = NOUVEAU_DP_MST;
160 		goto out;
161 	}
162 
163 	nv_encoder->dp.link_bw = 27000 * dpcd[DP_MAX_LINK_RATE];
164 	nv_encoder->dp.link_nr =
165 		dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
166 
167 	if (connector->connector_type == DRM_MODE_CONNECTOR_eDP && dpcd[DP_DPCD_REV] >= 0x13) {
168 		struct drm_dp_aux *aux = &nv_connector->aux;
169 		int ret, i;
170 		u8 sink_rates[16];
171 
172 		ret = drm_dp_dpcd_read(aux, DP_SUPPORTED_LINK_RATES, sink_rates, sizeof(sink_rates));
173 		if (ret == sizeof(sink_rates)) {
174 			for (i = 0; i < ARRAY_SIZE(sink_rates); i += 2) {
175 				int val = ((sink_rates[i + 1] << 8) | sink_rates[i]) * 200 / 10;
176 				if (val && (i == 0 || val > nv_encoder->dp.link_bw))
177 					nv_encoder->dp.link_bw = val;
178 			}
179 		}
180 	}
181 
182 	NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
183 		 nv_encoder->dp.link_nr, nv_encoder->dp.link_bw,
184 		 dpcd[DP_DPCD_REV]);
185 	NV_DEBUG(drm, "encoder: %dx%d\n",
186 		 nv_encoder->dcb->dpconf.link_nr,
187 		 nv_encoder->dcb->dpconf.link_bw);
188 
189 	if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
190 		nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
191 	if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
192 		nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
193 
194 	NV_DEBUG(drm, "maximum: %dx%d\n",
195 		 nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
196 
197 	if (mstm && mstm->can_mst) {
198 		ret = nv50_mstm_detect(nv_encoder);
199 		if (ret == 1) {
200 			ret = NOUVEAU_DP_MST;
201 			goto out;
202 		} else if (ret != 0) {
203 			nvif_outp_dp_aux_pwr(&nv_encoder->outp, false);
204 			goto out;
205 		}
206 	}
207 	ret = NOUVEAU_DP_SST;
208 
209 out:
210 	if (mstm && !mstm->suspended && ret != NOUVEAU_DP_MST)
211 		nv50_mstm_remove(mstm);
212 
213 	mutex_unlock(&nv_encoder->dp.hpd_irq_lock);
214 	return ret;
215 }
216 
217 bool
218 nouveau_dp_link_check(struct nouveau_connector *nv_connector)
219 {
220 	struct nouveau_encoder *nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP);
221 
222 	if (!nv_encoder || nv_encoder->outp.or.id < 0)
223 		return true;
224 
225 	return nvif_outp_dp_retrain(&nv_encoder->outp) == 0;
226 }
227 
228 void
229 nouveau_dp_irq(struct work_struct *work)
230 {
231 	struct nouveau_connector *nv_connector =
232 		container_of(work, typeof(*nv_connector), irq_work);
233 	struct drm_connector *connector = &nv_connector->base;
234 	struct nouveau_encoder *outp = find_encoder(connector, DCB_OUTPUT_DP);
235 	struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
236 	struct nv50_mstm *mstm;
237 	u64 hpd = 0;
238 	int ret;
239 
240 	if (!outp)
241 		return;
242 
243 	mstm = outp->dp.mstm;
244 	NV_DEBUG(drm, "service %s\n", connector->name);
245 
246 	mutex_lock(&outp->dp.hpd_irq_lock);
247 
248 	if (mstm && mstm->is_mst) {
249 		if (!nv50_mstm_service(drm, nv_connector, mstm))
250 			hpd |= NVIF_CONN_EVENT_V0_UNPLUG;
251 	} else {
252 		drm_dp_cec_irq(&nv_connector->aux);
253 
254 		if (nouveau_dp_has_sink_count(connector, outp)) {
255 			ret = drm_dp_read_sink_count(&nv_connector->aux);
256 			if (ret != outp->dp.sink_count)
257 				hpd |= NVIF_CONN_EVENT_V0_PLUG;
258 			if (ret >= 0)
259 				outp->dp.sink_count = ret;
260 		}
261 	}
262 
263 	mutex_unlock(&outp->dp.hpd_irq_lock);
264 
265 	nouveau_connector_hpd(nv_connector, NVIF_CONN_EVENT_V0_IRQ | hpd);
266 }
267 
268 /* TODO:
269  * - Validate against the DP caps advertised by the GPU (we don't check these
270  *   yet)
271  */
272 enum drm_mode_status
273 nv50_dp_mode_valid(struct nouveau_encoder *outp,
274 		   const struct drm_display_mode *mode,
275 		   unsigned *out_clock)
276 {
277 	const unsigned int min_clock = 25000;
278 	unsigned int max_rate, mode_rate, ds_max_dotclock, clock = mode->clock;
279 	/* Check with the minmum bpc always, so we can advertise better modes.
280 	 * In particlar not doing this causes modes to be dropped on HDR
281 	 * displays as we might check with a bpc of 16 even.
282 	 */
283 	const u8 bpp = 6 * 3;
284 
285 	if (mode->flags & DRM_MODE_FLAG_INTERLACE && !outp->caps.dp_interlace)
286 		return MODE_NO_INTERLACE;
287 
288 	if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
289 		clock *= 2;
290 
291 	max_rate = outp->dp.link_nr * outp->dp.link_bw;
292 	mode_rate = DIV_ROUND_UP(clock * bpp, 8);
293 	if (mode_rate > max_rate)
294 		return MODE_CLOCK_HIGH;
295 
296 	ds_max_dotclock = drm_dp_downstream_max_dotclock(outp->dp.dpcd, outp->dp.downstream_ports);
297 	if (ds_max_dotclock && clock > ds_max_dotclock)
298 		return MODE_CLOCK_HIGH;
299 
300 	if (clock < min_clock)
301 		return MODE_CLOCK_LOW;
302 
303 	if (out_clock)
304 		*out_clock = clock;
305 
306 	return MODE_OK;
307 }
308