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
nvkm_outp_route(struct nvkm_disp * disp)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
nvkm_outp_xlat(struct nvkm_outp * outp,enum nvkm_ior_type * type)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
nvkm_outp_release(struct nvkm_outp * outp,u8 user)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
nvkm_outp_acquire_ior(struct nvkm_outp * outp,u8 user,struct nvkm_ior * ior)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 static inline int
nvkm_outp_acquire_hda(struct nvkm_outp * outp,enum nvkm_ior_type type,u8 user,bool hda)115 nvkm_outp_acquire_hda(struct nvkm_outp *outp, enum nvkm_ior_type type,
116 u8 user, bool hda)
117 {
118 struct nvkm_ior *ior;
119
120 /* Failing that, a completely unused OR is the next best thing. */
121 list_for_each_entry(ior, &outp->disp->ior, head) {
122 if (!ior->identity && !!ior->func->hda.hpd == hda &&
123 !ior->asy.outp && ior->type == type && !ior->arm.outp &&
124 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
125 return nvkm_outp_acquire_ior(outp, user, ior);
126 }
127
128 /* Last resort is to assign an OR that's already active on HW,
129 * but will be released during the next modeset.
130 */
131 list_for_each_entry(ior, &outp->disp->ior, head) {
132 if (!ior->identity && !!ior->func->hda.hpd == hda &&
133 !ior->asy.outp && ior->type == type &&
134 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
135 return nvkm_outp_acquire_ior(outp, user, ior);
136 }
137
138 return -ENOSPC;
139 }
140
141 int
nvkm_outp_acquire(struct nvkm_outp * outp,u8 user,bool hda)142 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user, bool hda)
143 {
144 struct nvkm_ior *ior = outp->ior;
145 enum nvkm_ior_proto proto;
146 enum nvkm_ior_type type;
147
148 OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior);
149 if (ior) {
150 outp->acquired |= user;
151 return 0;
152 }
153
154 /* Lookup a compatible, and unused, OR to assign to the device. */
155 proto = nvkm_outp_xlat(outp, &type);
156 if (proto == UNKNOWN)
157 return -ENOSYS;
158
159 /* Deal with panels requiring identity-mapped SOR assignment. */
160 if (outp->identity) {
161 ior = nvkm_ior_find(outp->disp, SOR, ffs(outp->info.or) - 1);
162 if (WARN_ON(!ior))
163 return -ENOSPC;
164 return nvkm_outp_acquire_ior(outp, user, ior);
165 }
166
167 /* First preference is to reuse the OR that is currently armed
168 * on HW, if any, in order to prevent unnecessary switching.
169 */
170 list_for_each_entry(ior, &outp->disp->ior, head) {
171 if (!ior->identity && !ior->asy.outp && ior->arm.outp == outp) {
172 /*XXX: For various complicated reasons, we can't outright switch
173 * the boot-time OR on the first modeset without some fairly
174 * invasive changes.
175 *
176 * The systems that were fixed by modifying the OR selection
177 * code to account for HDA support shouldn't regress here as
178 * the HDA-enabled ORs match the relevant output's pad macro
179 * index, and the firmware seems to select an OR this way.
180 *
181 * This warning is to make it obvious if that proves wrong.
182 */
183 WARN_ON(hda && !ior->func->hda.hpd);
184 return nvkm_outp_acquire_ior(outp, user, ior);
185 }
186 }
187
188 /* If we don't need HDA, first try to acquire an OR that doesn't
189 * support it to leave free the ones that do.
190 */
191 if (!hda) {
192 if (!nvkm_outp_acquire_hda(outp, type, user, false))
193 return 0;
194
195 /* Use a HDA-supporting SOR anyway. */
196 return nvkm_outp_acquire_hda(outp, type, user, true);
197 }
198
199 /* We want HDA, try to acquire an OR that supports it. */
200 if (!nvkm_outp_acquire_hda(outp, type, user, true))
201 return 0;
202
203 /* There weren't any free ORs that support HDA, grab one that
204 * doesn't and at least allow display to work still.
205 */
206 return nvkm_outp_acquire_hda(outp, type, user, false);
207 }
208
209 void
nvkm_outp_fini(struct nvkm_outp * outp)210 nvkm_outp_fini(struct nvkm_outp *outp)
211 {
212 if (outp->func->fini)
213 outp->func->fini(outp);
214 }
215
216 static void
nvkm_outp_init_route(struct nvkm_outp * outp)217 nvkm_outp_init_route(struct nvkm_outp *outp)
218 {
219 struct nvkm_disp *disp = outp->disp;
220 enum nvkm_ior_proto proto;
221 enum nvkm_ior_type type;
222 struct nvkm_ior *ior;
223 int id, link;
224
225 /* Find any OR from the class that is able to support this device. */
226 proto = nvkm_outp_xlat(outp, &type);
227 if (proto == UNKNOWN)
228 return;
229
230 ior = nvkm_ior_find(disp, type, -1);
231 if (!ior) {
232 WARN_ON(1);
233 return;
234 }
235
236 /* Determine the specific OR, if any, this device is attached to. */
237 if (ior->func->route.get) {
238 id = ior->func->route.get(outp, &link);
239 if (id < 0) {
240 OUTP_DBG(outp, "no route");
241 return;
242 }
243 } else {
244 /* Prior to DCB 4.1, this is hardwired like so. */
245 id = ffs(outp->info.or) - 1;
246 link = (ior->type == SOR) ? outp->info.sorconf.link : 0;
247 }
248
249 ior = nvkm_ior_find(disp, type, id);
250 if (!ior) {
251 WARN_ON(1);
252 return;
253 }
254
255 /* Determine if the OR is already configured for this device. */
256 ior->func->state(ior, &ior->arm);
257 if (!ior->arm.head || ior->arm.proto != proto) {
258 OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head,
259 ior->arm.proto, proto);
260 return;
261 }
262
263 OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
264 ior->arm.outp = outp;
265 }
266
267 void
nvkm_outp_init(struct nvkm_outp * outp)268 nvkm_outp_init(struct nvkm_outp *outp)
269 {
270 nvkm_outp_init_route(outp);
271 if (outp->func->init)
272 outp->func->init(outp);
273 }
274
275 void
nvkm_outp_del(struct nvkm_outp ** poutp)276 nvkm_outp_del(struct nvkm_outp **poutp)
277 {
278 struct nvkm_outp *outp = *poutp;
279 if (outp && !WARN_ON(!outp->func)) {
280 if (outp->func->dtor)
281 *poutp = outp->func->dtor(outp);
282 kfree(*poutp);
283 *poutp = NULL;
284 }
285 }
286
287 int
nvkm_outp_ctor(const struct nvkm_outp_func * func,struct nvkm_disp * disp,int index,struct dcb_output * dcbE,struct nvkm_outp * outp)288 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp,
289 int index, struct dcb_output *dcbE, struct nvkm_outp *outp)
290 {
291 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
292 enum nvkm_ior_proto proto;
293 enum nvkm_ior_type type;
294
295 outp->func = func;
296 outp->disp = disp;
297 outp->index = index;
298 outp->info = *dcbE;
299 outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
300
301 OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x "
302 "edid %x bus %d head %x",
303 outp->info.type, outp->info.location, outp->info.or,
304 outp->info.type >= 2 ? outp->info.sorconf.link : 0,
305 outp->info.connector, outp->info.i2c_index,
306 outp->info.bus, outp->info.heads);
307
308 /* Cull output paths we can't map to an output resource. */
309 proto = nvkm_outp_xlat(outp, &type);
310 if (proto == UNKNOWN)
311 return -ENODEV;
312
313 return 0;
314 }
315
316 static const struct nvkm_outp_func
317 nvkm_outp = {
318 };
319
320 int
nvkm_outp_new(struct nvkm_disp * disp,int index,struct dcb_output * dcbE,struct nvkm_outp ** poutp)321 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
322 struct nvkm_outp **poutp)
323 {
324 if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
325 return -ENOMEM;
326 return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);
327 }
328