Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / nouveau / nvkm / engine / disp / outp.c
blobbe9e7f8c3b2392fa96643f91391e606196ad7a67
1 /*
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.
22 * Authors: Ben Skeggs
24 #include "outp.h"
25 #include "ior.h"
27 #include <subdev/bios.h>
28 #include <subdev/bios/dcb.h>
29 #include <subdev/i2c.h>
31 void
32 nvkm_outp_route(struct nvkm_disp *disp)
34 struct nvkm_outp *outp;
35 struct nvkm_ior *ior;
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;
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) {
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;
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;
80 break;
81 default:
82 break;
84 WARN_ON(1);
85 return UNKNOWN;
88 void
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);
93 if (ior) {
94 outp->acquired &= ~user;
95 if (!outp->acquired) {
96 outp->ior->asy.outp = NULL;
97 outp->ior = NULL;
102 static inline int
103 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior)
105 outp->ior = ior;
106 outp->ior->asy.outp = outp;
107 outp->ior->asy.link = outp->info.sorconf.link;
108 outp->acquired |= user;
109 return 0;
113 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user)
115 struct nvkm_ior *ior = outp->ior;
116 enum nvkm_ior_proto proto;
117 enum nvkm_ior_type type;
119 OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior);
120 if (ior) {
121 outp->acquired |= user;
122 return 0;
125 /* Lookup a compatible, and unused, OR to assign to the device. */
126 proto = nvkm_outp_xlat(outp, &type);
127 if (proto == UNKNOWN)
128 return -ENOSYS;
130 /* First preference is to reuse the OR that is currently armed
131 * on HW, if any, in order to prevent unnecessary switching.
133 list_for_each_entry(ior, &outp->disp->ior, head) {
134 if (!ior->asy.outp && ior->arm.outp == outp)
135 return nvkm_outp_acquire_ior(outp, user, ior);
138 /* Failing that, a completely unused OR is the next best thing. */
139 list_for_each_entry(ior, &outp->disp->ior, head) {
140 if (!ior->asy.outp && ior->type == type && !ior->arm.outp &&
141 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
142 return nvkm_outp_acquire_ior(outp, user, ior);
145 /* Last resort is to assign an OR that's already active on HW,
146 * but will be released during the next modeset.
148 list_for_each_entry(ior, &outp->disp->ior, head) {
149 if (!ior->asy.outp && ior->type == type &&
150 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
151 return nvkm_outp_acquire_ior(outp, user, ior);
154 return -ENOSPC;
157 void
158 nvkm_outp_fini(struct nvkm_outp *outp)
160 if (outp->func->fini)
161 outp->func->fini(outp);
164 static void
165 nvkm_outp_init_route(struct nvkm_outp *outp)
167 struct nvkm_disp *disp = outp->disp;
168 enum nvkm_ior_proto proto;
169 enum nvkm_ior_type type;
170 struct nvkm_ior *ior;
171 int id, link;
173 /* Find any OR from the class that is able to support this device. */
174 proto = nvkm_outp_xlat(outp, &type);
175 if (proto == UNKNOWN)
176 return;
178 ior = nvkm_ior_find(disp, type, -1);
179 if (!ior) {
180 WARN_ON(1);
181 return;
184 /* Determine the specific OR, if any, this device is attached to. */
185 if (ior->func->route.get) {
186 id = ior->func->route.get(outp, &link);
187 if (id < 0) {
188 OUTP_DBG(outp, "no route");
189 return;
191 } else {
192 /* Prior to DCB 4.1, this is hardwired like so. */
193 id = ffs(outp->info.or) - 1;
194 link = (ior->type == SOR) ? outp->info.sorconf.link : 0;
197 ior = nvkm_ior_find(disp, type, id);
198 if (!ior) {
199 WARN_ON(1);
200 return;
203 /* Determine if the OR is already configured for this device. */
204 ior->func->state(ior, &ior->arm);
205 if (!ior->arm.head || ior->arm.proto != proto) {
206 OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head,
207 ior->arm.proto, proto);
208 return;
211 OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
212 ior->arm.outp = outp;
215 void
216 nvkm_outp_init(struct nvkm_outp *outp)
218 nvkm_outp_init_route(outp);
219 if (outp->func->init)
220 outp->func->init(outp);
223 void
224 nvkm_outp_del(struct nvkm_outp **poutp)
226 struct nvkm_outp *outp = *poutp;
227 if (outp && !WARN_ON(!outp->func)) {
228 if (outp->func->dtor)
229 *poutp = outp->func->dtor(outp);
230 kfree(*poutp);
231 *poutp = NULL;
236 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp,
237 int index, struct dcb_output *dcbE, struct nvkm_outp *outp)
239 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
240 enum nvkm_ior_proto proto;
241 enum nvkm_ior_type type;
243 outp->func = func;
244 outp->disp = disp;
245 outp->index = index;
246 outp->info = *dcbE;
247 outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
248 outp->or = ffs(outp->info.or) - 1;
250 OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x "
251 "edid %x bus %d head %x",
252 outp->info.type, outp->info.location, outp->info.or,
253 outp->info.type >= 2 ? outp->info.sorconf.link : 0,
254 outp->info.connector, outp->info.i2c_index,
255 outp->info.bus, outp->info.heads);
257 /* Cull output paths we can't map to an output resource. */
258 proto = nvkm_outp_xlat(outp, &type);
259 if (proto == UNKNOWN)
260 return -ENODEV;
262 return 0;
265 static const struct nvkm_outp_func
266 nvkm_outp = {
270 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
271 struct nvkm_outp **poutp)
273 if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
274 return -ENOMEM;
275 return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);