GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / gpu / drm / nouveau / nvkm / engine / disp / outp.c
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 "dp.h"
26 #include "ior.h"
27
28 #include <subdev/bios.h>
29 #include <subdev/bios/dcb.h>
30 #include <subdev/i2c.h>
31
32 void
33 nvkm_outp_route(struct nvkm_disp *disp)
34 {
35         struct nvkm_outp *outp;
36         struct nvkm_ior *ior;
37
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);
43                         ior->arm.outp = NULL;
44                 }
45         }
46
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;
54                         }
55                 }
56         }
57 }
58
59 static enum nvkm_ior_proto
60 nvkm_outp_xlat(struct nvkm_outp *outp, enum nvkm_ior_type *type)
61 {
62         switch (outp->info.location) {
63         case 0:
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;
70                 default:
71                         break;
72                 }
73                 break;
74         case 1:
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 */
78                 default:
79                         break;
80                 }
81                 break;
82         default:
83                 break;
84         }
85         WARN_ON(1);
86         return UNKNOWN;
87 }
88
89 void
90 nvkm_outp_release(struct nvkm_outp *outp, u8 user)
91 {
92         struct nvkm_ior *ior = outp->ior;
93         OUTP_TRACE(outp, "release %02x &= %02x %p", outp->acquired, ~user, ior);
94         if (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;
100                         outp->ior = NULL;
101                 }
102         }
103 }
104
105 static inline int
106 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior)
107 {
108         outp->ior = ior;
109         outp->ior->asy.outp = outp;
110         outp->ior->asy.link = outp->info.sorconf.link;
111         outp->acquired |= user;
112         return 0;
113 }
114
115 int
116 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user)
117 {
118         struct nvkm_ior *ior = outp->ior;
119         enum nvkm_ior_proto proto;
120         enum nvkm_ior_type type;
121
122         OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior);
123         if (ior) {
124                 outp->acquired |= user;
125                 return 0;
126         }
127
128         /* Lookup a compatible, and unused, OR to assign to the device. */
129         proto = nvkm_outp_xlat(outp, &type);
130         if (proto == UNKNOWN)
131                 return -ENOSYS;
132
133         /* First preference is to reuse the OR that is currently armed
134          * on HW, if any, in order to prevent unnecessary switching.
135          */
136         list_for_each_entry(ior, &outp->disp->ior, head) {
137                 if (!ior->asy.outp && ior->arm.outp == outp)
138                         return nvkm_outp_acquire_ior(outp, user, ior);
139         }
140
141         /* Failing that, a completely unused OR is the next best thing. */
142         list_for_each_entry(ior, &outp->disp->ior, head) {
143                 if (!ior->asy.outp && ior->type == type && !ior->arm.outp &&
144                     (ior->func->route.set || ior->id == __ffs(outp->info.or)))
145                         return nvkm_outp_acquire_ior(outp, user, ior);
146         }
147
148         /* Last resort is to assign an OR that's already active on HW,
149          * but will be released during the next modeset.
150          */
151         list_for_each_entry(ior, &outp->disp->ior, head) {
152                 if (!ior->asy.outp && ior->type == type &&
153                     (ior->func->route.set || ior->id == __ffs(outp->info.or)))
154                         return nvkm_outp_acquire_ior(outp, user, ior);
155         }
156
157         return -ENOSPC;
158 }
159
160 void
161 nvkm_outp_fini(struct nvkm_outp *outp)
162 {
163         if (outp->func->fini)
164                 outp->func->fini(outp);
165 }
166
167 static void
168 nvkm_outp_init_route(struct nvkm_outp *outp)
169 {
170         struct nvkm_disp *disp = outp->disp;
171         enum nvkm_ior_proto proto;
172         enum nvkm_ior_type type;
173         struct nvkm_ior *ior;
174         int id, link;
175
176         /* Find any OR from the class that is able to support this device. */
177         proto = nvkm_outp_xlat(outp, &type);
178         if (proto == UNKNOWN)
179                 return;
180
181         ior = nvkm_ior_find(disp, type, -1);
182         if (!ior) {
183                 WARN_ON(1);
184                 return;
185         }
186
187         /* Determine the specific OR, if any, this device is attached to. */
188         if (ior->func->route.get) {
189                 id = ior->func->route.get(outp, &link);
190                 if (id < 0) {
191                         OUTP_DBG(outp, "no route");
192                         return;
193                 }
194         } else {
195                 /* Prior to DCB 4.1, this is hardwired like so. */
196                 id   = ffs(outp->info.or) - 1;
197                 link = (ior->type == SOR) ? outp->info.sorconf.link : 0;
198         }
199
200         ior = nvkm_ior_find(disp, type, id);
201         if (!ior) {
202                 WARN_ON(1);
203                 return;
204         }
205
206         /* Determine if the OR is already configured for this device. */
207         ior->func->state(ior, &ior->arm);
208         if (!ior->arm.head || ior->arm.proto != proto) {
209                 OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head,
210                          ior->arm.proto, proto);
211
212                 /* The EFI GOP driver on Ampere can leave unused DP links routed,
213                  * which we don't expect.  The DisableLT IED script *should* get
214                  * us back to where we need to be.
215                  */
216                 if (ior->func->route.get && !ior->arm.head && outp->info.type == DCB_OUTPUT_DP)
217                         nvkm_dp_disable(outp, ior);
218
219                 return;
220         }
221
222         OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
223         ior->arm.outp = outp;
224 }
225
226 void
227 nvkm_outp_init(struct nvkm_outp *outp)
228 {
229         nvkm_outp_init_route(outp);
230         if (outp->func->init)
231                 outp->func->init(outp);
232 }
233
234 void
235 nvkm_outp_del(struct nvkm_outp **poutp)
236 {
237         struct nvkm_outp *outp = *poutp;
238         if (outp && !WARN_ON(!outp->func)) {
239                 if (outp->func->dtor)
240                         *poutp = outp->func->dtor(outp);
241                 kfree(*poutp);
242                 *poutp = NULL;
243         }
244 }
245
246 int
247 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp,
248                int index, struct dcb_output *dcbE, struct nvkm_outp *outp)
249 {
250         struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
251         enum nvkm_ior_proto proto;
252         enum nvkm_ior_type type;
253
254         outp->func = func;
255         outp->disp = disp;
256         outp->index = index;
257         outp->info = *dcbE;
258         outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
259         outp->or = ffs(outp->info.or) - 1;
260
261         OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x "
262                        "edid %x bus %d head %x",
263                  outp->info.type, outp->info.location, outp->info.or,
264                  outp->info.type >= 2 ? outp->info.sorconf.link : 0,
265                  outp->info.connector, outp->info.i2c_index,
266                  outp->info.bus, outp->info.heads);
267
268         /* Cull output paths we can't map to an output resource. */
269         proto = nvkm_outp_xlat(outp, &type);
270         if (proto == UNKNOWN)
271                 return -ENODEV;
272
273         return 0;
274 }
275
276 static const struct nvkm_outp_func
277 nvkm_outp = {
278 };
279
280 int
281 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
282               struct nvkm_outp **poutp)
283 {
284         if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
285                 return -ENOMEM;
286         return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);
287 }