GNU Linux-libre 4.19.264-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         /* 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);
136                 if (WARN_ON(!ior))
137                         return -ENOSPC;
138                 return nvkm_outp_acquire_ior(outp, user, ior);
139         }
140
141         /* First preference is to reuse the OR that is currently armed
142          * on HW, if any, in order to prevent unnecessary switching.
143          */
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);
147         }
148
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);
155         }
156
157         /* Last resort is to assign an OR that's already active on HW,
158          * but will be released during the next modeset.
159          */
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);
164         }
165
166         return -ENOSPC;
167 }
168
169 void
170 nvkm_outp_fini(struct nvkm_outp *outp)
171 {
172         if (outp->func->fini)
173                 outp->func->fini(outp);
174 }
175
176 static void
177 nvkm_outp_init_route(struct nvkm_outp *outp)
178 {
179         struct nvkm_disp *disp = outp->disp;
180         enum nvkm_ior_proto proto;
181         enum nvkm_ior_type type;
182         struct nvkm_ior *ior;
183         int id, link;
184
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)
188                 return;
189
190         ior = nvkm_ior_find(disp, type, -1);
191         if (!ior) {
192                 WARN_ON(1);
193                 return;
194         }
195
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);
199                 if (id < 0) {
200                         OUTP_DBG(outp, "no route");
201                         return;
202                 }
203         } else {
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;
207         }
208
209         ior = nvkm_ior_find(disp, type, id);
210         if (!ior) {
211                 WARN_ON(1);
212                 return;
213         }
214
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);
220
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.
224                  */
225                 if (ior->func->route.get && !ior->arm.head && outp->info.type == DCB_OUTPUT_DP)
226                         nvkm_dp_disable(outp, ior);
227
228                 return;
229         }
230
231         OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link);
232         ior->arm.outp = outp;
233 }
234
235 void
236 nvkm_outp_init(struct nvkm_outp *outp)
237 {
238         nvkm_outp_init_route(outp);
239         if (outp->func->init)
240                 outp->func->init(outp);
241 }
242
243 void
244 nvkm_outp_del(struct nvkm_outp **poutp)
245 {
246         struct nvkm_outp *outp = *poutp;
247         if (outp && !WARN_ON(!outp->func)) {
248                 if (outp->func->dtor)
249                         *poutp = outp->func->dtor(outp);
250                 kfree(*poutp);
251                 *poutp = NULL;
252         }
253 }
254
255 int
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)
258 {
259         struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c;
260         enum nvkm_ior_proto proto;
261         enum nvkm_ior_type type;
262
263         outp->func = func;
264         outp->disp = disp;
265         outp->index = index;
266         outp->info = *dcbE;
267         outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index);
268
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);
275
276         /* Cull output paths we can't map to an output resource. */
277         proto = nvkm_outp_xlat(outp, &type);
278         if (proto == UNKNOWN)
279                 return -ENODEV;
280
281         return 0;
282 }
283
284 static const struct nvkm_outp_func
285 nvkm_outp = {
286 };
287
288 int
289 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE,
290               struct nvkm_outp **poutp)
291 {
292         if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL)))
293                 return -ENOMEM;
294         return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp);
295 }