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 "outp.h"
25 #include "dp.h"
26 #include "ior.h"
27 
28 #include <subdev/bios.h>
29 #include <subdev/bios/dcb.h>
30 #include <subdev/i2c.h>
31 
32 void
33 nvkm_outp_route(struct nvkm_disp *disp)
34 {
35 	struct nvkm_outp *outp;
36 	struct nvkm_ior *ior;
37 
38 	list_for_each_entry(ior, &disp->iors, head) {
39 		if ((outp = ior->arm.outp) && ior->arm.outp != ior->asy.outp) {
40 			OUTP_DBG(outp, "release %s", ior->name);
41 			if (ior->func->route.set)
42 				ior->func->route.set(outp, NULL);
43 			ior->arm.outp = NULL;
44 		}
45 	}
46 
47 	list_for_each_entry(ior, &disp->iors, head) {
48 		if ((outp = ior->asy.outp)) {
49 			OUTP_DBG(outp, "acquire %s", ior->name);
50 			if (ior->asy.outp != ior->arm.outp) {
51 				if (ior->func->route.set)
52 					ior->func->route.set(outp, ior);
53 				ior->arm.outp = ior->asy.outp;
54 			}
55 		}
56 	}
57 }
58 
59 static enum nvkm_ior_proto
60 nvkm_outp_xlat(struct nvkm_outp *outp, enum nvkm_ior_type *type)
61 {
62 	switch (outp->info.location) {
63 	case 0:
64 		switch (outp->info.type) {
65 		case DCB_OUTPUT_ANALOG: *type = DAC; return  CRT;
66 		case DCB_OUTPUT_TV    : *type = DAC; return   TV;
67 		case DCB_OUTPUT_TMDS  : *type = SOR; return TMDS;
68 		case DCB_OUTPUT_LVDS  : *type = SOR; return LVDS;
69 		case DCB_OUTPUT_DP    : *type = SOR; return   DP;
70 		default:
71 			break;
72 		}
73 		break;
74 	case 1:
75 		switch (outp->info.type) {
76 		case DCB_OUTPUT_TMDS: *type = PIOR; return TMDS;
77 		case DCB_OUTPUT_DP  : *type = PIOR; return TMDS; /* not a bug */
78 		default:
79 			break;
80 		}
81 		break;
82 	default:
83 		break;
84 	}
85 	WARN_ON(1);
86 	return UNKNOWN;
87 }
88 
89 void
90 nvkm_outp_release(struct nvkm_outp *outp, u8 user)
91 {
92 	struct nvkm_ior *ior = outp->ior;
93 	OUTP_TRACE(outp, "release %02x &= %02x %p", outp->acquired, ~user, ior);
94 	if (ior) {
95 		outp->acquired &= ~user;
96 		if (!outp->acquired) {
97 			if (outp->func->release && outp->ior)
98 				outp->func->release(outp);
99 			outp->ior->asy.outp = NULL;
100 			outp->ior = NULL;
101 		}
102 	}
103 }
104 
105 static inline int
106 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior)
107 {
108 	outp->ior = ior;
109 	outp->ior->asy.outp = outp;
110 	outp->ior->asy.link = outp->info.sorconf.link;
111 	outp->acquired |= user;
112 	return 0;
113 }
114 
115 static inline int
116 nvkm_outp_acquire_hda(struct nvkm_outp *outp, enum nvkm_ior_type type,
117 		      u8 user, bool hda)
118 {
119 	struct nvkm_ior *ior;
120 
121 	/* Failing that, a completely unused OR is the next best thing. */
122 	list_for_each_entry(ior, &outp->disp->iors, head) {
123 		if (!ior->identity && ior->hda == hda &&
124 		    !ior->asy.outp && ior->type == type && !ior->arm.outp &&
125 		    (ior->func->route.set || ior->id == __ffs(outp->info.or)))
126 			return nvkm_outp_acquire_ior(outp, user, ior);
127 	}
128 
129 	/* Last resort is to assign an OR that's already active on HW,
130 	 * but will be released during the next modeset.
131 	 */
132 	list_for_each_entry(ior, &outp->disp->iors, head) {
133 		if (!ior->identity && ior->hda == hda &&
134 		    !ior->asy.outp && ior->type == type &&
135 		    (ior->func->route.set || ior->id == __ffs(outp->info.or)))
136 			return nvkm_outp_acquire_ior(outp, user, ior);
137 	}
138 
139 	return -ENOSPC;
140 }
141 
142 int
143 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user, bool hda)
144 {
145 	struct nvkm_ior *ior = outp->ior;
146 	enum nvkm_ior_proto proto;
147 	enum nvkm_ior_type type;
148 
149 	OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior);
150 	if (ior) {
151 		outp->acquired |= user;
152 		return 0;
153 	}
154 
155 	/* Lookup a compatible, and unused, OR to assign to the device. */
156 	proto = nvkm_outp_xlat(outp, &type);
157 	if (proto == UNKNOWN)
158 		return -ENOSYS;
159 
160 	/* Deal with panels requiring identity-mapped SOR assignment. */
161 	if (outp->identity) {
162 		ior = nvkm_ior_find(outp->disp, SOR, ffs(outp->info.or) - 1);
163 		if (WARN_ON(!ior))
164 			return -ENOSPC;
165 		return nvkm_outp_acquire_ior(outp, user, ior);
166 	}
167 
168 	/* First preference is to reuse the OR that is currently armed
169 	 * on HW, if any, in order to prevent unnecessary switching.
170 	 */
171 	list_for_each_entry(ior, &outp->disp->iors, head) {
172 		if (!ior->identity && !ior->asy.outp && ior->arm.outp == outp) {
173 			/*XXX: For various complicated reasons, we can't outright switch
174 			 *     the boot-time OR on the first modeset without some fairly
175 			 *     invasive changes.
176 			 *
177 			 *     The systems that were fixed by modifying the OR selection
178 			 *     code to account for HDA support shouldn't regress here as
179 			 *     the HDA-enabled ORs match the relevant output's pad macro
180 			 *     index, and the firmware seems to select an OR this way.
181 			 *
182 			 *     This warning is to make it obvious if that proves wrong.
183 			 */
184 			WARN_ON(hda && !ior->hda);
185 			return nvkm_outp_acquire_ior(outp, user, ior);
186 		}
187 	}
188 
189 	/* If we don't need HDA, first try to acquire an OR that doesn't
190 	 * support it to leave free the ones that do.
191 	 */
192 	if (!hda) {
193 		if (!nvkm_outp_acquire_hda(outp, type, user, false))
194 			return 0;
195 
196 		/* Use a HDA-supporting SOR anyway. */
197 		return nvkm_outp_acquire_hda(outp, type, user, true);
198 	}
199 
200 	/* We want HDA, try to acquire an OR that supports it. */
201 	if (!nvkm_outp_acquire_hda(outp, type, user, true))
202 		return 0;
203 
204 	/* There weren't any free ORs that support HDA, grab one that
205 	 * doesn't and at least allow display to work still.
206 	 */
207 	return nvkm_outp_acquire_hda(outp, type, user, false);
208 }
209 
210 void
211 nvkm_outp_fini(struct nvkm_outp *outp)
212 {
213 	if (outp->func->fini)
214 		outp->func->fini(outp);
215 }
216 
217 static void
218 nvkm_outp_init_route(struct nvkm_outp *outp)
219 {
220 	struct nvkm_disp *disp = outp->disp;
221 	enum nvkm_ior_proto proto;
222 	enum nvkm_ior_type type;
223 	struct nvkm_ior *ior;
224 	int id, link;
225 
226 	/* Find any OR from the class that is able to support this device. */
227 	proto = nvkm_outp_xlat(outp, &type);
228 	if (proto == UNKNOWN)
229 		return;
230 
231 	ior = nvkm_ior_find(disp, type, -1);
232 	if (!ior) {
233 		WARN_ON(1);
234 		return;
235 	}
236 
237 	/* Determine the specific OR, if any, this device is attached to. */
238 	if (ior->func->route.get) {
239 		id = ior->func->route.get(outp, &link);
240 		if (id < 0) {
241 			OUTP_DBG(outp, "no route");
242 			return;
243 		}
244 	} else {
245 		/* Prior to DCB 4.1, this is hardwired like so. */
246 		id   = ffs(outp->info.or) - 1;
247 		link = (ior->type == SOR) ? outp->info.sorconf.link : 0;
248 	}
249 
250 	ior = nvkm_ior_find(disp, type, id);
251 	if (!ior) {
252 		WARN_ON(1);
253 		return;
254 	}
255 
256 	/* Determine if the OR is already configured for this device. */
257 	ior->func->state(ior, &ior->arm);
258 	if (!ior->arm.head || ior->arm.proto != proto) {
259 		OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head,
260 			 ior->arm.proto, proto);
261 
262 		/* The EFI GOP driver on Ampere can leave unused DP links routed,
263 		 * which we don't expect.  The DisableLT IED script *should* get
264 		 * us back to where we need to be.
265 		 */
266 		if (ior->func->route.get && !ior->arm.head && outp->info.type == DCB_OUTPUT_DP)
267 			nvkm_dp_disable(outp, ior);
268 
269 		return;
270 	}
271 
272 	OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
273 	ior->arm.outp = outp;
274 }
275 
276 void
277 nvkm_outp_init(struct nvkm_outp *outp)
278 {
279 	nvkm_outp_init_route(outp);
280 	if (outp->func->init)
281 		outp->func->init(outp);
282 }
283 
284 void
285 nvkm_outp_del(struct nvkm_outp **poutp)
286 {
287 	struct nvkm_outp *outp = *poutp;
288 	if (outp && !WARN_ON(!outp->func)) {
289 		if (outp->func->dtor)
290 			*poutp = outp->func->dtor(outp);
291 		kfree(*poutp);
292 		*poutp = NULL;
293 	}
294 }
295 
296 int
297 nvkm_outp_new_(const struct nvkm_outp_func *func, struct nvkm_disp *disp,
298 	       int index, struct dcb_output *dcbE, struct nvkm_outp **poutp)
299 {
300 	struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
301 	struct nvkm_outp *outp;
302 	enum nvkm_ior_proto proto;
303 	enum nvkm_ior_type type;
304 
305 	if (!(outp = *poutp = kzalloc(sizeof(*outp), GFP_KERNEL)))
306 		return -ENOMEM;
307 
308 	outp->func = func;
309 	outp->disp = disp;
310 	outp->index = index;
311 	outp->info = *dcbE;
312 	outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
313 
314 	OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x "
315 		       "edid %x bus %d head %x",
316 		 outp->info.type, outp->info.location, outp->info.or,
317 		 outp->info.type >= 2 ? outp->info.sorconf.link : 0,
318 		 outp->info.connector, outp->info.i2c_index,
319 		 outp->info.bus, outp->info.heads);
320 
321 	/* Cull output paths we can't map to an output resource. */
322 	proto = nvkm_outp_xlat(outp, &type);
323 	if (proto == UNKNOWN)
324 		return -ENODEV;
325 
326 	return 0;
327 }
328 
329 static const struct nvkm_outp_func
330 nvkm_outp = {
331 };
332 
333 int
334 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
335 	      struct nvkm_outp **poutp)
336 {
337 	return nvkm_outp_new_(&nvkm_outp, disp, index, dcbE, poutp);
338 }
339