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 int
nvkm_outp_acquire(struct nvkm_outp * outp,u8 user)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 /* First preference is to reuse the OR that is currently armed
133 * on HW, if any, in order to prevent unnecessary switching.
134 */
135 list_for_each_entry(ior, &outp->disp->ior, head) {
136 if (!ior->asy.outp && ior->arm.outp == outp)
137 return nvkm_outp_acquire_ior(outp, user, ior);
138 }
139
140 /* Failing that, a completely unused OR is the next best thing. */
141 list_for_each_entry(ior, &outp->disp->ior, head) {
142 if (!ior->asy.outp && ior->type == type && !ior->arm.outp &&
143 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
144 return nvkm_outp_acquire_ior(outp, user, ior);
145 }
146
147 /* Last resort is to assign an OR that's already active on HW,
148 * but will be released during the next modeset.
149 */
150 list_for_each_entry(ior, &outp->disp->ior, head) {
151 if (!ior->asy.outp && ior->type == type &&
152 (ior->func->route.set || ior->id == __ffs(outp->info.or)))
153 return nvkm_outp_acquire_ior(outp, user, ior);
154 }
155
156 return -ENOSPC;
157 }
158
159 void
nvkm_outp_fini(struct nvkm_outp * outp)160 nvkm_outp_fini(struct nvkm_outp *outp)
161 {
162 if (outp->func->fini)
163 outp->func->fini(outp);
164 }
165
166 static void
nvkm_outp_init_route(struct nvkm_outp * outp)167 nvkm_outp_init_route(struct nvkm_outp *outp)
168 {
169 struct nvkm_disp *disp = outp->disp;
170 enum nvkm_ior_proto proto;
171 enum nvkm_ior_type type;
172 struct nvkm_ior *ior;
173 int id, link;
174
175 /* Find any OR from the class that is able to support this device. */
176 proto = nvkm_outp_xlat(outp, &type);
177 if (proto == UNKNOWN)
178 return;
179
180 ior = nvkm_ior_find(disp, type, -1);
181 if (!ior) {
182 WARN_ON(1);
183 return;
184 }
185
186 /* Determine the specific OR, if any, this device is attached to. */
187 if (ior->func->route.get) {
188 id = ior->func->route.get(outp, &link);
189 if (id < 0) {
190 OUTP_DBG(outp, "no route");
191 return;
192 }
193 } else {
194 /* Prior to DCB 4.1, this is hardwired like so. */
195 id = ffs(outp->info.or) - 1;
196 link = (ior->type == SOR) ? outp->info.sorconf.link : 0;
197 }
198
199 ior = nvkm_ior_find(disp, type, id);
200 if (!ior) {
201 WARN_ON(1);
202 return;
203 }
204
205 /* Determine if the OR is already configured for this device. */
206 ior->func->state(ior, &ior->arm);
207 if (!ior->arm.head || ior->arm.proto != proto) {
208 OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head,
209 ior->arm.proto, proto);
210 return;
211 }
212
213 OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
214 ior->arm.outp = outp;
215 }
216
217 void
nvkm_outp_init(struct nvkm_outp * outp)218 nvkm_outp_init(struct nvkm_outp *outp)
219 {
220 nvkm_outp_init_route(outp);
221 if (outp->func->init)
222 outp->func->init(outp);
223 }
224
225 void
nvkm_outp_del(struct nvkm_outp ** poutp)226 nvkm_outp_del(struct nvkm_outp **poutp)
227 {
228 struct nvkm_outp *outp = *poutp;
229 if (outp && !WARN_ON(!outp->func)) {
230 if (outp->func->dtor)
231 *poutp = outp->func->dtor(outp);
232 kfree(*poutp);
233 *poutp = NULL;
234 }
235 }
236
237 int
nvkm_outp_ctor(const struct nvkm_outp_func * func,struct nvkm_disp * disp,int index,struct dcb_output * dcbE,struct nvkm_outp * outp)238 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp,
239 int index, struct dcb_output *dcbE, struct nvkm_outp *outp)
240 {
241 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
242 enum nvkm_ior_proto proto;
243 enum nvkm_ior_type type;
244
245 outp->func = func;
246 outp->disp = disp;
247 outp->index = index;
248 outp->info = *dcbE;
249 outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
250 outp->or = ffs(outp->info.or) - 1;
251
252 OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x "
253 "edid %x bus %d head %x",
254 outp->info.type, outp->info.location, outp->info.or,
255 outp->info.type >= 2 ? outp->info.sorconf.link : 0,
256 outp->info.connector, outp->info.i2c_index,
257 outp->info.bus, outp->info.heads);
258
259 /* Cull output paths we can't map to an output resource. */
260 proto = nvkm_outp_xlat(outp, &type);
261 if (proto == UNKNOWN)
262 return -ENODEV;
263
264 return 0;
265 }
266
267 static const struct nvkm_outp_func
268 nvkm_outp = {
269 };
270
271 int
nvkm_outp_new(struct nvkm_disp * disp,int index,struct dcb_output * dcbE,struct nvkm_outp ** poutp)272 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
273 struct nvkm_outp **poutp)
274 {
275 if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
276 return -ENOMEM;
277 return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);
278 }
279