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
nouveau_dp_has_sink_count(struct drm_connector * connector,struct nouveau_encoder * outp)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
nouveau_dp_probe_dpcd(struct nouveau_connector * nv_connector,struct nouveau_encoder * outp)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
nouveau_dp_detect(struct nouveau_connector * nv_connector,struct nouveau_encoder * nv_encoder)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 /* If we've already read the DPCD on an eDP device, we don't need to
112 * reread it as it won't change
113 */
114 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP &&
115 dpcd[DP_DPCD_REV] != 0)
116 return NOUVEAU_DP_SST;
117
118 mutex_lock(&nv_encoder->dp.hpd_irq_lock);
119 if (mstm) {
120 /* If we're not ready to handle MST state changes yet, just
121 * report the last status of the connector. We'll reprobe it
122 * once we've resumed.
123 */
124 if (mstm->suspended) {
125 if (mstm->is_mst)
126 ret = NOUVEAU_DP_MST;
127 else if (connector->status ==
128 connector_status_connected)
129 ret = NOUVEAU_DP_SST;
130
131 goto out;
132 }
133 }
134
135 /* Check status of HPD pin before attempting an AUX transaction that
136 * would result in a number of (futile) retries on a connector which
137 * has no display plugged.
138 *
139 * TODO: look into checking this before probing I2C to detect DVI/HDMI
140 */
141 hpd = nvif_conn_hpd_status(&nv_connector->conn);
142 if (hpd == NVIF_CONN_HPD_STATUS_NOT_PRESENT) {
143 nvif_outp_dp_aux_pwr(&nv_encoder->outp, false);
144 goto out;
145 }
146 nvif_outp_dp_aux_pwr(&nv_encoder->outp, true);
147
148 status = nouveau_dp_probe_dpcd(nv_connector, nv_encoder);
149 if (status == connector_status_disconnected) {
150 nvif_outp_dp_aux_pwr(&nv_encoder->outp, false);
151 goto out;
152 }
153
154 /* If we're in MST mode, we're done here */
155 if (mstm && mstm->can_mst && mstm->is_mst) {
156 ret = NOUVEAU_DP_MST;
157 goto out;
158 }
159
160 nv_encoder->dp.link_bw = 27000 * dpcd[DP_MAX_LINK_RATE];
161 nv_encoder->dp.link_nr =
162 dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
163
164 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP && dpcd[DP_DPCD_REV] >= 0x13) {
165 struct drm_dp_aux *aux = &nv_connector->aux;
166 int ret, i;
167 u8 sink_rates[16];
168
169 ret = drm_dp_dpcd_read(aux, DP_SUPPORTED_LINK_RATES, sink_rates, sizeof(sink_rates));
170 if (ret == sizeof(sink_rates)) {
171 for (i = 0; i < ARRAY_SIZE(sink_rates); i += 2) {
172 int val = ((sink_rates[i + 1] << 8) | sink_rates[i]) * 200 / 10;
173 if (val && (i == 0 || val > nv_encoder->dp.link_bw))
174 nv_encoder->dp.link_bw = val;
175 }
176 }
177 }
178
179 NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
180 nv_encoder->dp.link_nr, nv_encoder->dp.link_bw,
181 dpcd[DP_DPCD_REV]);
182 NV_DEBUG(drm, "encoder: %dx%d\n",
183 nv_encoder->dcb->dpconf.link_nr,
184 nv_encoder->dcb->dpconf.link_bw);
185
186 if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
187 nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
188 if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
189 nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
190
191 NV_DEBUG(drm, "maximum: %dx%d\n",
192 nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
193
194 if (mstm && mstm->can_mst) {
195 ret = nv50_mstm_detect(nv_encoder);
196 if (ret == 1) {
197 ret = NOUVEAU_DP_MST;
198 goto out;
199 } else if (ret != 0) {
200 nvif_outp_dp_aux_pwr(&nv_encoder->outp, false);
201 goto out;
202 }
203 }
204 ret = NOUVEAU_DP_SST;
205
206 out:
207 if (mstm && !mstm->suspended && ret != NOUVEAU_DP_MST)
208 nv50_mstm_remove(mstm);
209
210 mutex_unlock(&nv_encoder->dp.hpd_irq_lock);
211 return ret;
212 }
213
214 bool
nouveau_dp_link_check(struct nouveau_connector * nv_connector)215 nouveau_dp_link_check(struct nouveau_connector *nv_connector)
216 {
217 struct nouveau_encoder *nv_encoder = find_encoder(&nv_connector->base, DCB_OUTPUT_DP);
218
219 if (!nv_encoder || nv_encoder->outp.or.id < 0)
220 return true;
221
222 return nvif_outp_dp_retrain(&nv_encoder->outp) == 0;
223 }
224
225 void
nouveau_dp_irq(struct work_struct * work)226 nouveau_dp_irq(struct work_struct *work)
227 {
228 struct nouveau_connector *nv_connector =
229 container_of(work, typeof(*nv_connector), irq_work);
230 struct drm_connector *connector = &nv_connector->base;
231 struct nouveau_encoder *outp = find_encoder(connector, DCB_OUTPUT_DP);
232 struct nouveau_drm *drm = nouveau_drm(outp->base.base.dev);
233 struct nv50_mstm *mstm;
234 u64 hpd = 0;
235 int ret;
236
237 if (!outp)
238 return;
239
240 mstm = outp->dp.mstm;
241 NV_DEBUG(drm, "service %s\n", connector->name);
242
243 mutex_lock(&outp->dp.hpd_irq_lock);
244
245 if (mstm && mstm->is_mst) {
246 if (!nv50_mstm_service(drm, nv_connector, mstm))
247 hpd |= NVIF_CONN_EVENT_V0_UNPLUG;
248 } else {
249 drm_dp_cec_irq(&nv_connector->aux);
250
251 if (nouveau_dp_has_sink_count(connector, outp)) {
252 ret = drm_dp_read_sink_count(&nv_connector->aux);
253 if (ret != outp->dp.sink_count)
254 hpd |= NVIF_CONN_EVENT_V0_PLUG;
255 if (ret >= 0)
256 outp->dp.sink_count = ret;
257 }
258 }
259
260 mutex_unlock(&outp->dp.hpd_irq_lock);
261
262 nouveau_connector_hpd(nv_connector, NVIF_CONN_EVENT_V0_IRQ | hpd);
263 }
264
265 /* TODO:
266 * - Use the minimum possible BPC here, once we add support for the max bpc
267 * property.
268 * - Validate against the DP caps advertised by the GPU (we don't check these
269 * yet)
270 */
271 enum drm_mode_status
nv50_dp_mode_valid(struct drm_connector * connector,struct nouveau_encoder * outp,const struct drm_display_mode * mode,unsigned * out_clock)272 nv50_dp_mode_valid(struct drm_connector *connector,
273 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 const u8 bpp = connector->display_info.bpc * 3;
280
281 if (mode->flags & DRM_MODE_FLAG_INTERLACE && !outp->caps.dp_interlace)
282 return MODE_NO_INTERLACE;
283
284 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING)
285 clock *= 2;
286
287 max_rate = outp->dp.link_nr * outp->dp.link_bw;
288 mode_rate = DIV_ROUND_UP(clock * bpp, 8);
289 if (mode_rate > max_rate)
290 return MODE_CLOCK_HIGH;
291
292 ds_max_dotclock = drm_dp_downstream_max_dotclock(outp->dp.dpcd, outp->dp.downstream_ports);
293 if (ds_max_dotclock && clock > ds_max_dotclock)
294 return MODE_CLOCK_HIGH;
295
296 if (clock < min_clock)
297 return MODE_CLOCK_LOW;
298
299 if (out_clock)
300 *out_clock = clock;
301
302 return MODE_OK;
303 }
304