2 * Copyright 2014 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 #include <subdev/bios.h>
28 #include <subdev/bios/dcb.h>
29 #include <subdev/i2c.h>
32 nvkm_outp_route(struct nvkm_disp *disp)
34 struct nvkm_outp *outp;
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);
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;
58 static enum nvkm_ior_proto
59 nvkm_outp_xlat(struct nvkm_outp *outp, enum nvkm_ior_type *type)
61 switch (outp->info.location) {
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;
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 */
89 nvkm_outp_release(struct nvkm_outp *outp, u8 user)
91 struct nvkm_ior *ior = outp->ior;
92 OUTP_TRACE(outp, "release %02x &= %02x %p", outp->acquired, ~user, 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;
105 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior)
108 outp->ior->asy.outp = outp;
109 outp->ior->asy.link = outp->info.sorconf.link;
110 outp->acquired |= user;
115 nvkm_outp_acquire_hda(struct nvkm_outp *outp, enum nvkm_ior_type type,
118 struct nvkm_ior *ior;
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);
128 /* Last resort is to assign an OR that's already active on HW,
129 * but will be released during the next modeset.
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);
142 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user, bool hda)
144 struct nvkm_ior *ior = outp->ior;
145 enum nvkm_ior_proto proto;
146 enum nvkm_ior_type type;
148 OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior);
150 outp->acquired |= user;
154 /* Lookup a compatible, and unused, OR to assign to the device. */
155 proto = nvkm_outp_xlat(outp, &type);
156 if (proto == UNKNOWN)
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);
164 return nvkm_outp_acquire_ior(outp, user, ior);
167 /* First preference is to reuse the OR that is currently armed
168 * on HW, if any, in order to prevent unnecessary switching.
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
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.
181 * This warning is to make it obvious if that proves wrong.
183 WARN_ON(hda && !ior->func->hda.hpd);
184 return nvkm_outp_acquire_ior(outp, user, ior);
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.
192 if (!nvkm_outp_acquire_hda(outp, type, user, false))
195 /* Use a HDA-supporting SOR anyway. */
196 return nvkm_outp_acquire_hda(outp, type, user, true);
199 /* We want HDA, try to acquire an OR that supports it. */
200 if (!nvkm_outp_acquire_hda(outp, type, user, true))
203 /* There weren't any free ORs that support HDA, grab one that
204 * doesn't and at least allow display to work still.
206 return nvkm_outp_acquire_hda(outp, type, user, false);
210 nvkm_outp_fini(struct nvkm_outp *outp)
212 if (outp->func->fini)
213 outp->func->fini(outp);
217 nvkm_outp_init_route(struct nvkm_outp *outp)
219 struct nvkm_disp *disp = outp->disp;
220 enum nvkm_ior_proto proto;
221 enum nvkm_ior_type type;
222 struct nvkm_ior *ior;
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)
230 ior = nvkm_ior_find(disp, type, -1);
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);
240 OUTP_DBG(outp, "no route");
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;
249 ior = nvkm_ior_find(disp, type, id);
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);
263 OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
264 ior->arm.outp = outp;
268 nvkm_outp_init(struct nvkm_outp *outp)
270 nvkm_outp_init_route(outp);
271 if (outp->func->init)
272 outp->func->init(outp);
276 nvkm_outp_del(struct nvkm_outp **poutp)
278 struct nvkm_outp *outp = *poutp;
279 if (outp && !WARN_ON(!outp->func)) {
280 if (outp->func->dtor)
281 *poutp = outp->func->dtor(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)
291 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
292 enum nvkm_ior_proto proto;
293 enum nvkm_ior_type type;
299 outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
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);
308 /* Cull output paths we can't map to an output resource. */
309 proto = nvkm_outp_xlat(outp, &type);
310 if (proto == UNKNOWN)
316 static const struct nvkm_outp_func
321 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
322 struct nvkm_outp **poutp)
324 if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
326 return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);