• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 #include "outpdp.h"
25 #include "conn.h"
26 #include "dport.h"
27 #include "priv.h"
28 
29 #include <subdev/i2c.h>
30 
31 #include <nvif/event.h>
32 
33 int
nvkm_output_dp_train(struct nvkm_output * base,u32 datarate,bool wait)34 nvkm_output_dp_train(struct nvkm_output *base, u32 datarate, bool wait)
35 {
36 	struct nvkm_output_dp *outp = nvkm_output_dp(base);
37 	bool retrain = true;
38 	u8 link[2], stat[3];
39 	u32 linkrate;
40 	int ret, i;
41 
42 	/* check that the link is trained at a high enough rate */
43 	ret = nvkm_rdaux(outp->aux, DPCD_LC00_LINK_BW_SET, link, 2);
44 	if (ret) {
45 		OUTP_DBG(&outp->base,
46 			 "failed to read link config, assuming no sink");
47 		goto done;
48 	}
49 
50 	linkrate = link[0] * 27000 * (link[1] & DPCD_LC01_LANE_COUNT_SET);
51 	linkrate = (linkrate * 8) / 10; /* 8B/10B coding overhead */
52 	datarate = (datarate + 9) / 10; /* -> decakilobits */
53 	if (linkrate < datarate) {
54 		OUTP_DBG(&outp->base, "link not trained at sufficient rate");
55 		goto done;
56 	}
57 
58 	/* check that link is still trained */
59 	ret = nvkm_rdaux(outp->aux, DPCD_LS02, stat, 3);
60 	if (ret) {
61 		OUTP_DBG(&outp->base,
62 			 "failed to read link status, assuming no sink");
63 		goto done;
64 	}
65 
66 	if (stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE) {
67 		for (i = 0; i < (link[1] & DPCD_LC01_LANE_COUNT_SET); i++) {
68 			u8 lane = (stat[i >> 1] >> ((i & 1) * 4)) & 0x0f;
69 			if (!(lane & DPCD_LS02_LANE0_CR_DONE) ||
70 			    !(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
71 			    !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED)) {
72 				OUTP_DBG(&outp->base,
73 					 "lane %d not equalised", lane);
74 				goto done;
75 			}
76 		}
77 		retrain = false;
78 	} else {
79 		OUTP_DBG(&outp->base, "no inter-lane alignment");
80 	}
81 
82 done:
83 	if (retrain || !atomic_read(&outp->lt.done)) {
84 		/* no sink, but still need to configure source */
85 		if (outp->dpcd[DPCD_RC00_DPCD_REV] == 0x00) {
86 			outp->dpcd[DPCD_RC01_MAX_LINK_RATE] =
87 				outp->base.info.dpconf.link_bw;
88 			outp->dpcd[DPCD_RC02] =
89 				outp->base.info.dpconf.link_nr;
90 		}
91 		atomic_set(&outp->lt.done, 0);
92 		schedule_work(&outp->lt.work);
93 	} else {
94 		nvkm_notify_get(&outp->irq);
95 	}
96 
97 	if (wait) {
98 		if (!wait_event_timeout(outp->lt.wait,
99 					atomic_read(&outp->lt.done),
100 					msecs_to_jiffies(2000)))
101 			ret = -ETIMEDOUT;
102 	}
103 
104 	return ret;
105 }
106 
107 static void
nvkm_output_dp_enable(struct nvkm_output_dp * outp,bool enable)108 nvkm_output_dp_enable(struct nvkm_output_dp *outp, bool enable)
109 {
110 	struct nvkm_i2c_aux *aux = outp->aux;
111 
112 	if (enable) {
113 		if (!outp->present) {
114 			OUTP_DBG(&outp->base, "aux power -> always");
115 			nvkm_i2c_aux_monitor(aux, true);
116 			outp->present = true;
117 		}
118 
119 		if (!nvkm_rdaux(aux, DPCD_RC00_DPCD_REV, outp->dpcd,
120 				sizeof(outp->dpcd))) {
121 			nvkm_output_dp_train(&outp->base, 0, true);
122 			return;
123 		}
124 	}
125 
126 	if (outp->present) {
127 		OUTP_DBG(&outp->base, "aux power -> demand");
128 		nvkm_i2c_aux_monitor(aux, false);
129 		outp->present = false;
130 	}
131 
132 	atomic_set(&outp->lt.done, 0);
133 }
134 
135 static int
nvkm_output_dp_hpd(struct nvkm_notify * notify)136 nvkm_output_dp_hpd(struct nvkm_notify *notify)
137 {
138 	const struct nvkm_i2c_ntfy_rep *line = notify->data;
139 	struct nvkm_output_dp *outp = container_of(notify, typeof(*outp), hpd);
140 	struct nvkm_connector *conn = outp->base.conn;
141 	struct nvkm_disp *disp = outp->base.disp;
142 	struct nvif_notify_conn_rep_v0 rep = {};
143 
144 	OUTP_DBG(&outp->base, "HPD: %d", line->mask);
145 	nvkm_output_dp_enable(outp, true);
146 
147 	if (line->mask & NVKM_I2C_UNPLUG)
148 		rep.mask |= NVIF_NOTIFY_CONN_V0_UNPLUG;
149 	if (line->mask & NVKM_I2C_PLUG)
150 		rep.mask |= NVIF_NOTIFY_CONN_V0_PLUG;
151 
152 	nvkm_event_send(&disp->hpd, rep.mask, conn->index, &rep, sizeof(rep));
153 	return NVKM_NOTIFY_KEEP;
154 }
155 
156 static int
nvkm_output_dp_irq(struct nvkm_notify * notify)157 nvkm_output_dp_irq(struct nvkm_notify *notify)
158 {
159 	const struct nvkm_i2c_ntfy_rep *line = notify->data;
160 	struct nvkm_output_dp *outp = container_of(notify, typeof(*outp), irq);
161 	struct nvkm_connector *conn = outp->base.conn;
162 	struct nvkm_disp *disp = outp->base.disp;
163 	struct nvif_notify_conn_rep_v0 rep = {
164 		.mask = NVIF_NOTIFY_CONN_V0_IRQ,
165 	};
166 
167 	OUTP_DBG(&outp->base, "IRQ: %d", line->mask);
168 	nvkm_output_dp_train(&outp->base, 0, true);
169 
170 	nvkm_event_send(&disp->hpd, rep.mask, conn->index, &rep, sizeof(rep));
171 	return NVKM_NOTIFY_DROP;
172 }
173 
174 static void
nvkm_output_dp_fini(struct nvkm_output * base)175 nvkm_output_dp_fini(struct nvkm_output *base)
176 {
177 	struct nvkm_output_dp *outp = nvkm_output_dp(base);
178 	nvkm_notify_put(&outp->hpd);
179 	nvkm_notify_put(&outp->irq);
180 	flush_work(&outp->lt.work);
181 	nvkm_output_dp_enable(outp, false);
182 }
183 
184 static void
nvkm_output_dp_init(struct nvkm_output * base)185 nvkm_output_dp_init(struct nvkm_output *base)
186 {
187 	struct nvkm_output_dp *outp = nvkm_output_dp(base);
188 	nvkm_notify_put(&outp->base.conn->hpd);
189 	nvkm_output_dp_enable(outp, true);
190 	nvkm_notify_get(&outp->hpd);
191 }
192 
193 static void *
nvkm_output_dp_dtor(struct nvkm_output * base)194 nvkm_output_dp_dtor(struct nvkm_output *base)
195 {
196 	struct nvkm_output_dp *outp = nvkm_output_dp(base);
197 	nvkm_notify_fini(&outp->hpd);
198 	nvkm_notify_fini(&outp->irq);
199 	return outp;
200 }
201 
202 static const struct nvkm_output_func
203 nvkm_output_dp_func = {
204 	.dtor = nvkm_output_dp_dtor,
205 	.init = nvkm_output_dp_init,
206 	.fini = nvkm_output_dp_fini,
207 };
208 
209 int
nvkm_output_dp_ctor(const struct nvkm_output_dp_func * func,struct nvkm_disp * disp,int index,struct dcb_output * dcbE,struct nvkm_i2c_aux * aux,struct nvkm_output_dp * outp)210 nvkm_output_dp_ctor(const struct nvkm_output_dp_func *func,
211 		    struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
212 		    struct nvkm_i2c_aux *aux, struct nvkm_output_dp *outp)
213 {
214 	struct nvkm_device *device = disp->engine.subdev.device;
215 	struct nvkm_bios *bios = device->bios;
216 	struct nvkm_i2c *i2c = device->i2c;
217 	u8  hdr, cnt, len;
218 	u32 data;
219 	int ret;
220 
221 	nvkm_output_ctor(&nvkm_output_dp_func, disp, index, dcbE, &outp->base);
222 	outp->func = func;
223 	outp->aux = aux;
224 	if (!outp->aux) {
225 		OUTP_ERR(&outp->base, "no aux");
226 		return -ENODEV;
227 	}
228 
229 	/* bios data is not optional */
230 	data = nvbios_dpout_match(bios, outp->base.info.hasht,
231 				  outp->base.info.hashm, &outp->version,
232 				  &hdr, &cnt, &len, &outp->info);
233 	if (!data) {
234 		OUTP_ERR(&outp->base, "no bios dp data");
235 		return -ENODEV;
236 	}
237 
238 	OUTP_DBG(&outp->base, "bios dp %02x %02x %02x %02x",
239 		 outp->version, hdr, cnt, len);
240 
241 	/* link training */
242 	INIT_WORK(&outp->lt.work, nvkm_dp_train);
243 	init_waitqueue_head(&outp->lt.wait);
244 	atomic_set(&outp->lt.done, 0);
245 
246 	/* link maintenance */
247 	ret = nvkm_notify_init(NULL, &i2c->event, nvkm_output_dp_irq, true,
248 			       &(struct nvkm_i2c_ntfy_req) {
249 				.mask = NVKM_I2C_IRQ,
250 				.port = outp->aux->id,
251 			       },
252 			       sizeof(struct nvkm_i2c_ntfy_req),
253 			       sizeof(struct nvkm_i2c_ntfy_rep),
254 			       &outp->irq);
255 	if (ret) {
256 		OUTP_ERR(&outp->base, "error monitoring aux irq: %d", ret);
257 		return ret;
258 	}
259 
260 	/* hotplug detect, replaces gpio-based mechanism with aux events */
261 	ret = nvkm_notify_init(NULL, &i2c->event, nvkm_output_dp_hpd, true,
262 			       &(struct nvkm_i2c_ntfy_req) {
263 				.mask = NVKM_I2C_PLUG | NVKM_I2C_UNPLUG,
264 				.port = outp->aux->id,
265 			       },
266 			       sizeof(struct nvkm_i2c_ntfy_req),
267 			       sizeof(struct nvkm_i2c_ntfy_rep),
268 			       &outp->hpd);
269 	if (ret) {
270 		OUTP_ERR(&outp->base, "error monitoring aux hpd: %d", ret);
271 		return ret;
272 	}
273 
274 	return 0;
275 }
276 
277 int
nvkm_output_dp_new_(const struct nvkm_output_dp_func * func,struct nvkm_disp * disp,int index,struct dcb_output * dcbE,struct nvkm_output ** poutp)278 nvkm_output_dp_new_(const struct nvkm_output_dp_func *func,
279 		    struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
280 		    struct nvkm_output **poutp)
281 {
282 	struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
283 	struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, dcbE->i2c_index);
284 	struct nvkm_output_dp *outp;
285 
286 	if (!(outp = kzalloc(sizeof(*outp), GFP_KERNEL)))
287 		return -ENOMEM;
288 	*poutp = &outp->base;
289 
290 	return nvkm_output_dp_ctor(func, disp, index, dcbE, aux, outp);
291 }
292