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.
28 #include <subdev/bios.h>
29 #include <subdev/bios/dcb.h>
30 #include <subdev/i2c.h>
33 nvkm_outp_route(struct nvkm_disp *disp)
35 struct nvkm_outp *outp;
38 list_for_each_entry(ior, &disp->ior, 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);
47 list_for_each_entry(ior, &disp->ior, 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;
59 static enum nvkm_ior_proto
60 nvkm_outp_xlat(struct nvkm_outp *outp, enum nvkm_ior_type *type)
62 switch (outp->info.location) {
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;
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 */
90 nvkm_outp_release(struct nvkm_outp *outp, u8 user)
92 struct nvkm_ior *ior = outp->ior;
93 OUTP_TRACE(outp, "release %02x &= %02x %p", outp->acquired, ~user, 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;
106 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior)
109 outp->ior->asy.outp = outp;
110 outp->ior->asy.link = outp->info.sorconf.link;
111 outp->acquired |= user;
116 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user)
118 struct nvkm_ior *ior = outp->ior;
119 enum nvkm_ior_proto proto;
120 enum nvkm_ior_type type;
122 OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior);
124 outp->acquired |= user;
128 /* Lookup a compatible, and unused, OR to assign to the device. */
129 proto = nvkm_outp_xlat(outp, &type);
130 if (proto == UNKNOWN)
133 /* Deal with panels requiring identity-mapped SOR assignment. */
134 if (outp->identity) {
135 ior = nvkm_ior_find(outp->disp, SOR, ffs(outp->info.or) - 1);
138 return nvkm_outp_acquire_ior(outp, user, ior);
141 /* First preference is to reuse the OR that is currently armed
142 * on HW, if any, in order to prevent unnecessary switching.
144 list_for_each_entry(ior, &outp->disp->ior, head) {
145 if (!ior->identity && !ior->asy.outp && ior->arm.outp == outp)
146 return nvkm_outp_acquire_ior(outp, user, ior);
149 /* Failing that, a completely unused OR is the next best thing. */
150 list_for_each_entry(ior, &outp->disp->ior, head) {
151 if (!ior->identity &&
152 !ior->asy.outp && ior->type == type && !ior->arm.outp &&
153 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
154 return nvkm_outp_acquire_ior(outp, user, ior);
157 /* Last resort is to assign an OR that's already active on HW,
158 * but will be released during the next modeset.
160 list_for_each_entry(ior, &outp->disp->ior, head) {
161 if (!ior->identity && !ior->asy.outp && ior->type == type &&
162 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
163 return nvkm_outp_acquire_ior(outp, user, ior);
170 nvkm_outp_fini(struct nvkm_outp *outp)
172 if (outp->func->fini)
173 outp->func->fini(outp);
177 nvkm_outp_init_route(struct nvkm_outp *outp)
179 struct nvkm_disp *disp = outp->disp;
180 enum nvkm_ior_proto proto;
181 enum nvkm_ior_type type;
182 struct nvkm_ior *ior;
185 /* Find any OR from the class that is able to support this device. */
186 proto = nvkm_outp_xlat(outp, &type);
187 if (proto == UNKNOWN)
190 ior = nvkm_ior_find(disp, type, -1);
196 /* Determine the specific OR, if any, this device is attached to. */
197 if (ior->func->route.get) {
198 id = ior->func->route.get(outp, &link);
200 OUTP_DBG(outp, "no route");
204 /* Prior to DCB 4.1, this is hardwired like so. */
205 id = ffs(outp->info.or) - 1;
206 link = (ior->type == SOR) ? outp->info.sorconf.link : 0;
209 ior = nvkm_ior_find(disp, type, id);
215 /* Determine if the OR is already configured for this device. */
216 ior->func->state(ior, &ior->arm);
217 if (!ior->arm.head || ior->arm.proto != proto) {
218 OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head,
219 ior->arm.proto, proto);
221 /* The EFI GOP driver on Ampere can leave unused DP links routed,
222 * which we don't expect. The DisableLT IED script *should* get
223 * us back to where we need to be.
225 if (ior->func->route.get && !ior->arm.head && outp->info.type == DCB_OUTPUT_DP)
226 nvkm_dp_disable(outp, ior);
231 OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
232 ior->arm.outp = outp;
236 nvkm_outp_init(struct nvkm_outp *outp)
238 nvkm_outp_init_route(outp);
239 if (outp->func->init)
240 outp->func->init(outp);
244 nvkm_outp_del(struct nvkm_outp **poutp)
246 struct nvkm_outp *outp = *poutp;
247 if (outp && !WARN_ON(!outp->func)) {
248 if (outp->func->dtor)
249 *poutp = outp->func->dtor(outp);
256 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp,
257 int index, struct dcb_output *dcbE, struct nvkm_outp *outp)
259 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
260 enum nvkm_ior_proto proto;
261 enum nvkm_ior_type type;
267 outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
269 OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x "
270 "edid %x bus %d head %x",
271 outp->info.type, outp->info.location, outp->info.or,
272 outp->info.type >= 2 ? outp->info.sorconf.link : 0,
273 outp->info.connector, outp->info.i2c_index,
274 outp->info.bus, outp->info.heads);
276 /* Cull output paths we can't map to an output resource. */
277 proto = nvkm_outp_xlat(outp, &type);
278 if (proto == UNKNOWN)
284 static const struct nvkm_outp_func
289 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
290 struct nvkm_outp **poutp)
292 if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
294 return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);