drm/i915/dp: Extract drm_dp_read_mst_cap()
[linux-block.git] / drivers / gpu / drm / nouveau / nouveau_dp.c
CommitLineData
6ee73861
BS
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
612a9aab 25#include <drm/drm_dp_helper.h>
b01f0608 26
4dc28134 27#include "nouveau_drv.h"
b01f0608 28#include "nouveau_connector.h"
6ee73861 29#include "nouveau_encoder.h"
27a45987 30#include "nouveau_crtc.h"
6ee73861 31
52aa30f2
BS
32#include <nvif/class.h>
33#include <nvif/cl5070.h>
34
f479c0ba
BS
35MODULE_PARM_DESC(mst, "Enable DisplayPort multi-stream (default: enabled)");
36static int nouveau_mst = 1;
37module_param_named(mst, nouveau_mst, int, 0400);
38
a0922278
LP
39static enum drm_connector_status
40nouveau_dp_probe_dpcd(struct nouveau_connector *nv_connector,
41 struct nouveau_encoder *outp)
42{
43 struct drm_dp_aux *aux = &nv_connector->aux;
44 struct nv50_mstm *mstm = NULL;
45 int ret;
46 u8 *dpcd = outp->dp.dpcd;
47 u8 tmp;
48
49 ret = drm_dp_dpcd_read(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);
50 if (ret == DP_RECEIVER_CAP_SIZE && dpcd[DP_DPCD_REV]) {
51 ret = drm_dp_read_desc(aux, &outp->dp.desc,
52 drm_dp_is_branch(dpcd));
53 if (ret < 0)
54 return connector_status_disconnected;
55 } else {
56 return connector_status_disconnected;
57 }
58
59 if (nouveau_mst)
60 mstm = outp->dp.mstm;
61
62 if (mstm) {
63 if (dpcd[DP_DPCD_REV] >= DP_DPCD_REV_12) {
64 ret = drm_dp_dpcd_readb(aux, DP_MSTM_CAP, &tmp);
65 if (ret < 0)
66 return connector_status_disconnected;
67
68 mstm->can_mst = !!(tmp & DP_MST_CAP);
69 } else {
70 mstm->can_mst = false;
71 }
72 }
73
74 return connector_status_connected;
75}
76
8777c5c1 77int
6ba11932
LP
78nouveau_dp_detect(struct nouveau_connector *nv_connector,
79 struct nouveau_encoder *nv_encoder)
6ee73861 80{
8777c5c1 81 struct drm_device *dev = nv_encoder->base.base.dev;
77145f1c 82 struct nouveau_drm *drm = nouveau_drm(dev);
a0922278
LP
83 struct drm_connector *connector = &nv_connector->base;
84 struct nv50_mstm *mstm = nv_encoder->dp.mstm;
85 enum drm_connector_status status;
86 u8 *dpcd = nv_encoder->dp.dpcd;
87 int ret = NOUVEAU_DP_NONE;
88
89 /* If we've already read the DPCD on an eDP device, we don't need to
90 * reread it as it won't change
91 */
92 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP &&
93 dpcd[DP_DPCD_REV] != 0)
94 return NOUVEAU_DP_SST;
6ee73861 95
a0922278
LP
96 mutex_lock(&nv_encoder->dp.hpd_irq_lock);
97 if (mstm) {
98 /* If we're not ready to handle MST state changes yet, just
99 * report the last status of the connector. We'll reprobe it
100 * once we've resumed.
101 */
102 if (mstm->suspended) {
103 if (mstm->is_mst)
104 ret = NOUVEAU_DP_MST;
105 else if (connector->status ==
106 connector_status_connected)
107 ret = NOUVEAU_DP_SST;
108
109 goto out;
110 }
111 }
112
113 status = nouveau_dp_probe_dpcd(nv_connector, nv_encoder);
114 if (status == connector_status_disconnected)
115 goto out;
116
117 /* If we're in MST mode, we're done here */
118 if (mstm && mstm->can_mst && mstm->is_mst) {
119 ret = NOUVEAU_DP_MST;
120 goto out;
121 }
6ee73861 122
57940402
LP
123 nv_encoder->dp.link_bw = 27000 * dpcd[DP_MAX_LINK_RATE];
124 nv_encoder->dp.link_nr =
125 dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
6ee73861 126
77145f1c 127 NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
57940402
LP
128 nv_encoder->dp.link_nr, nv_encoder->dp.link_bw,
129 dpcd[DP_DPCD_REV]);
77145f1c 130 NV_DEBUG(drm, "encoder: %dx%d\n",
bbcd521e
LP
131 nv_encoder->dcb->dpconf.link_nr,
132 nv_encoder->dcb->dpconf.link_bw);
6ee73861 133
75a1fccf 134 if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
6ee73861 135 nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
75a1fccf
BS
136 if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
137 nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
6ee73861 138
77145f1c 139 NV_DEBUG(drm, "maximum: %dx%d\n",
bbcd521e 140 nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
fe224bb7 141
a0922278
LP
142 if (mstm && mstm->can_mst) {
143 ret = nv50_mstm_detect(nv_encoder);
144 if (ret == 1) {
145 ret = NOUVEAU_DP_MST;
146 goto out;
147 } else if (ret != 0) {
148 goto out;
149 }
6ba11932 150 }
a0922278 151 ret = NOUVEAU_DP_SST;
52aa30f2 152
a0922278
LP
153out:
154 if (mstm && !mstm->suspended && ret != NOUVEAU_DP_MST)
155 nv50_mstm_remove(mstm);
156
157 mutex_unlock(&nv_encoder->dp.hpd_irq_lock);
52aa30f2 158 return ret;
6ee73861 159}
d6a9efec 160
a0922278
LP
161void nouveau_dp_irq(struct nouveau_drm *drm,
162 struct nouveau_connector *nv_connector)
163{
164 struct drm_connector *connector = &nv_connector->base;
165 struct nouveau_encoder *outp = find_encoder(connector, DCB_OUTPUT_DP);
166 struct nv50_mstm *mstm;
167
168 if (!outp)
169 return;
170
171 mstm = outp->dp.mstm;
172 NV_DEBUG(drm, "service %s\n", connector->name);
173
174 mutex_lock(&outp->dp.hpd_irq_lock);
175
176 if (mstm && mstm->is_mst) {
177 if (!nv50_mstm_service(drm, nv_connector, mstm))
178 schedule_work(&drm->hpd_work);
179 } else {
180 drm_dp_cec_irq(&nv_connector->aux);
181 }
182
183 mutex_unlock(&outp->dp.hpd_irq_lock);
184}
185
d6a9efec
LP
186/* TODO:
187 * - Use the minimum possible BPC here, once we add support for the max bpc
188 * property.
189 * - Validate the mode against downstream port caps (see
190 * drm_dp_downstream_max_clock())
191 * - Validate against the DP caps advertised by the GPU (we don't check these
192 * yet)
193 */
194enum drm_mode_status
195nv50_dp_mode_valid(struct drm_connector *connector,
196 struct nouveau_encoder *outp,
197 const struct drm_display_mode *mode,
198 unsigned *out_clock)
199{
200 const unsigned min_clock = 25000;
201 unsigned max_clock, clock;
202 enum drm_mode_status ret;
203
204 if (mode->flags & DRM_MODE_FLAG_INTERLACE && !outp->caps.dp_interlace)
205 return MODE_NO_INTERLACE;
206
207 max_clock = outp->dp.link_nr * outp->dp.link_bw;
208 clock = mode->clock * (connector->display_info.bpc * 3) / 10;
209
210 ret = nouveau_conn_mode_clock_valid(mode, min_clock, max_clock,
211 &clock);
212 if (out_clock)
213 *out_clock = clock;
214 return ret;
215}