xref: /linux/drivers/gpu/drm/nouveau/nvkm/engine/disp/outp.c (revision b68fc09be48edbc47de1a0f3d42ef8adf6c0ac55)
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 "ior.h"
26 
27 #include <subdev/bios.h>
28 #include <subdev/bios/dcb.h>
29 #include <subdev/i2c.h>
30 
31 void
32 nvkm_outp_route(struct nvkm_disp *disp)
33 {
34 	struct nvkm_outp *outp;
35 	struct nvkm_ior *ior;
36 
37 	list_for_each_entry(ior, &disp->ior, head) {
38 		if ((outp = ior->arm.outp) && ior->arm.outp != ior->asy.outp) {
39 			OUTP_DBG(outp, "release %s", ior->name);
40 			if (ior->func->route.set)
41 				ior->func->route.set(outp, NULL);
42 			ior->arm.outp = NULL;
43 		}
44 	}
45 
46 	list_for_each_entry(ior, &disp->ior, head) {
47 		if ((outp = ior->asy.outp)) {
48 			OUTP_DBG(outp, "acquire %s", ior->name);
49 			if (ior->asy.outp != ior->arm.outp) {
50 				if (ior->func->route.set)
51 					ior->func->route.set(outp, ior);
52 				ior->arm.outp = ior->asy.outp;
53 			}
54 		}
55 	}
56 }
57 
58 static enum nvkm_ior_proto
59 nvkm_outp_xlat(struct nvkm_outp *outp, enum nvkm_ior_type *type)
60 {
61 	switch (outp->info.location) {
62 	case 0:
63 		switch (outp->info.type) {
64 		case DCB_OUTPUT_ANALOG: *type = DAC; return  CRT;
65 		case DCB_OUTPUT_TV    : *type = DAC; return   TV;
66 		case DCB_OUTPUT_TMDS  : *type = SOR; return TMDS;
67 		case DCB_OUTPUT_LVDS  : *type = SOR; return LVDS;
68 		case DCB_OUTPUT_DP    : *type = SOR; return   DP;
69 		default:
70 			break;
71 		}
72 		break;
73 	case 1:
74 		switch (outp->info.type) {
75 		case DCB_OUTPUT_TMDS: *type = PIOR; return TMDS;
76 		case DCB_OUTPUT_DP  : *type = PIOR; return TMDS; /* not a bug */
77 		default:
78 			break;
79 		}
80 		break;
81 	default:
82 		break;
83 	}
84 	WARN_ON(1);
85 	return UNKNOWN;
86 }
87 
88 void
89 nvkm_outp_release(struct nvkm_outp *outp, u8 user)
90 {
91 	struct nvkm_ior *ior = outp->ior;
92 	OUTP_TRACE(outp, "release %02x &= %02x %p", outp->acquired, ~user, ior);
93 	if (ior) {
94 		outp->acquired &= ~user;
95 		if (!outp->acquired) {
96 			if (outp->func->release && outp->ior)
97 				outp->func->release(outp);
98 			outp->ior->asy.outp = NULL;
99 			outp->ior = NULL;
100 		}
101 	}
102 }
103 
104 static inline int
105 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior)
106 {
107 	outp->ior = ior;
108 	outp->ior->asy.outp = outp;
109 	outp->ior->asy.link = outp->info.sorconf.link;
110 	outp->acquired |= user;
111 	return 0;
112 }
113 
114 int
115 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user)
116 {
117 	struct nvkm_ior *ior = outp->ior;
118 	enum nvkm_ior_proto proto;
119 	enum nvkm_ior_type type;
120 
121 	OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior);
122 	if (ior) {
123 		outp->acquired |= user;
124 		return 0;
125 	}
126 
127 	/* Lookup a compatible, and unused, OR to assign to the device. */
128 	proto = nvkm_outp_xlat(outp, &type);
129 	if (proto == UNKNOWN)
130 		return -ENOSYS;
131 
132 	/* Deal with panels requiring identity-mapped SOR assignment. */
133 	if (outp->identity) {
134 		ior = nvkm_ior_find(outp->disp, SOR, ffs(outp->info.or) - 1);
135 		if (WARN_ON(!ior))
136 			return -ENOSPC;
137 		return nvkm_outp_acquire_ior(outp, user, ior);
138 	}
139 
140 	/* First preference is to reuse the OR that is currently armed
141 	 * on HW, if any, in order to prevent unnecessary switching.
142 	 */
143 	list_for_each_entry(ior, &outp->disp->ior, head) {
144 		if (!ior->identity && !ior->asy.outp && ior->arm.outp == outp)
145 			return nvkm_outp_acquire_ior(outp, user, ior);
146 	}
147 
148 	/* Failing that, a completely unused OR is the next best thing. */
149 	list_for_each_entry(ior, &outp->disp->ior, head) {
150 		if (!ior->identity &&
151 		    !ior->asy.outp && ior->type == type && !ior->arm.outp &&
152 		    (ior->func->route.set || ior->id == __ffs(outp->info.or)))
153 			return nvkm_outp_acquire_ior(outp, user, ior);
154 	}
155 
156 	/* Last resort is to assign an OR that's already active on HW,
157 	 * but will be released during the next modeset.
158 	 */
159 	list_for_each_entry(ior, &outp->disp->ior, head) {
160 		if (!ior->identity && !ior->asy.outp && ior->type == type &&
161 		    (ior->func->route.set || ior->id == __ffs(outp->info.or)))
162 			return nvkm_outp_acquire_ior(outp, user, ior);
163 	}
164 
165 	return -ENOSPC;
166 }
167 
168 void
169 nvkm_outp_fini(struct nvkm_outp *outp)
170 {
171 	if (outp->func->fini)
172 		outp->func->fini(outp);
173 }
174 
175 static void
176 nvkm_outp_init_route(struct nvkm_outp *outp)
177 {
178 	struct nvkm_disp *disp = outp->disp;
179 	enum nvkm_ior_proto proto;
180 	enum nvkm_ior_type type;
181 	struct nvkm_ior *ior;
182 	int id, link;
183 
184 	/* Find any OR from the class that is able to support this device. */
185 	proto = nvkm_outp_xlat(outp, &type);
186 	if (proto == UNKNOWN)
187 		return;
188 
189 	ior = nvkm_ior_find(disp, type, -1);
190 	if (!ior) {
191 		WARN_ON(1);
192 		return;
193 	}
194 
195 	/* Determine the specific OR, if any, this device is attached to. */
196 	if (ior->func->route.get) {
197 		id = ior->func->route.get(outp, &link);
198 		if (id < 0) {
199 			OUTP_DBG(outp, "no route");
200 			return;
201 		}
202 	} else {
203 		/* Prior to DCB 4.1, this is hardwired like so. */
204 		id   = ffs(outp->info.or) - 1;
205 		link = (ior->type == SOR) ? outp->info.sorconf.link : 0;
206 	}
207 
208 	ior = nvkm_ior_find(disp, type, id);
209 	if (!ior) {
210 		WARN_ON(1);
211 		return;
212 	}
213 
214 	/* Determine if the OR is already configured for this device. */
215 	ior->func->state(ior, &ior->arm);
216 	if (!ior->arm.head || ior->arm.proto != proto) {
217 		OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head,
218 			 ior->arm.proto, proto);
219 		return;
220 	}
221 
222 	OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
223 	ior->arm.outp = outp;
224 }
225 
226 void
227 nvkm_outp_init(struct nvkm_outp *outp)
228 {
229 	nvkm_outp_init_route(outp);
230 	if (outp->func->init)
231 		outp->func->init(outp);
232 }
233 
234 void
235 nvkm_outp_del(struct nvkm_outp **poutp)
236 {
237 	struct nvkm_outp *outp = *poutp;
238 	if (outp && !WARN_ON(!outp->func)) {
239 		if (outp->func->dtor)
240 			*poutp = outp->func->dtor(outp);
241 		kfree(*poutp);
242 		*poutp = NULL;
243 	}
244 }
245 
246 int
247 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp,
248 	       int index, struct dcb_output *dcbE, struct nvkm_outp *outp)
249 {
250 	struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
251 	enum nvkm_ior_proto proto;
252 	enum nvkm_ior_type type;
253 
254 	outp->func = func;
255 	outp->disp = disp;
256 	outp->index = index;
257 	outp->info = *dcbE;
258 	outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
259 
260 	OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x "
261 		       "edid %x bus %d head %x",
262 		 outp->info.type, outp->info.location, outp->info.or,
263 		 outp->info.type >= 2 ? outp->info.sorconf.link : 0,
264 		 outp->info.connector, outp->info.i2c_index,
265 		 outp->info.bus, outp->info.heads);
266 
267 	/* Cull output paths we can't map to an output resource. */
268 	proto = nvkm_outp_xlat(outp, &type);
269 	if (proto == UNKNOWN)
270 		return -ENODEV;
271 
272 	return 0;
273 }
274 
275 static const struct nvkm_outp_func
276 nvkm_outp = {
277 };
278 
279 int
280 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
281 	      struct nvkm_outp **poutp)
282 {
283 	if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
284 		return -ENOMEM;
285 	return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);
286 }
287