drm/nouveau: fence: fix undefined fence state after emit
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / nouveau / nvkm / subdev / i2c / base.c
1 /*
2  * Copyright 2013 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 "priv.h"
25 #include "aux.h"
26 #include "bus.h"
27 #include "pad.h"
28
29 #include <core/option.h>
30 #include <subdev/bios.h>
31 #include <subdev/bios/dcb.h>
32 #include <subdev/bios/i2c.h>
33
34 static struct nvkm_i2c_pad *
35 nvkm_i2c_pad_find(struct nvkm_i2c *i2c, int id)
36 {
37         struct nvkm_i2c_pad *pad;
38
39         list_for_each_entry(pad, &i2c->pad, head) {
40                 if (pad->id == id)
41                         return pad;
42         }
43
44         return NULL;
45 }
46
47 struct nvkm_i2c_bus *
48 nvkm_i2c_bus_find(struct nvkm_i2c *i2c, int id)
49 {
50         struct nvkm_bios *bios = i2c->subdev.device->bios;
51         struct nvkm_i2c_bus *bus;
52
53         if (id == NVKM_I2C_BUS_PRI || id == NVKM_I2C_BUS_SEC) {
54                 u8  ver, hdr, cnt, len;
55                 u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
56                 if (i2c && ver >= 0x30) {
57                         u8 auxidx = nvbios_rd08(bios, i2c + 4);
58                         if (id == NVKM_I2C_BUS_PRI)
59                                 id = NVKM_I2C_BUS_CCB((auxidx & 0x0f) >> 0);
60                         else
61                                 id = NVKM_I2C_BUS_CCB((auxidx & 0xf0) >> 4);
62                 } else {
63                         id = NVKM_I2C_BUS_CCB(2);
64                 }
65         }
66
67         list_for_each_entry(bus, &i2c->bus, head) {
68                 if (bus->id == id)
69                         return bus;
70         }
71
72         return NULL;
73 }
74
75 struct nvkm_i2c_aux *
76 nvkm_i2c_aux_find(struct nvkm_i2c *i2c, int id)
77 {
78         struct nvkm_i2c_aux *aux;
79
80         list_for_each_entry(aux, &i2c->aux, head) {
81                 if (aux->id == id)
82                         return aux;
83         }
84
85         return NULL;
86 }
87
88 static void
89 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int id)
90 {
91         struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
92         struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
93         if (aux)
94                 i2c->func->aux_mask(i2c, type, aux->intr, 0);
95 }
96
97 static void
98 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int id)
99 {
100         struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
101         struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
102         if (aux)
103                 i2c->func->aux_mask(i2c, type, aux->intr, aux->intr);
104 }
105
106 static const struct nvkm_event_func
107 nvkm_i2c_intr_func = {
108         .init = nvkm_i2c_intr_init,
109         .fini = nvkm_i2c_intr_fini,
110 };
111
112 static void
113 nvkm_i2c_intr(struct nvkm_subdev *subdev)
114 {
115         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
116         struct nvkm_i2c_aux *aux;
117         u32 hi, lo, rq, tx;
118
119         if (!i2c->func->aux_stat)
120                 return;
121
122         i2c->func->aux_stat(i2c, &hi, &lo, &rq, &tx);
123         if (!hi && !lo && !rq && !tx)
124                 return;
125
126         list_for_each_entry(aux, &i2c->aux, head) {
127                 u32 mask = 0;
128                 if (hi & aux->intr) mask |= NVKM_I2C_PLUG;
129                 if (lo & aux->intr) mask |= NVKM_I2C_UNPLUG;
130                 if (rq & aux->intr) mask |= NVKM_I2C_IRQ;
131                 if (tx & aux->intr) mask |= NVKM_I2C_DONE;
132                 if (mask)
133                         nvkm_event_ntfy(&i2c->event, aux->id, mask);
134         }
135 }
136
137 static int
138 nvkm_i2c_fini(struct nvkm_subdev *subdev, bool suspend)
139 {
140         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
141         struct nvkm_i2c_pad *pad;
142         struct nvkm_i2c_bus *bus;
143         struct nvkm_i2c_aux *aux;
144         u32 mask;
145
146         list_for_each_entry(aux, &i2c->aux, head) {
147                 nvkm_i2c_aux_fini(aux);
148         }
149
150         list_for_each_entry(bus, &i2c->bus, head) {
151                 nvkm_i2c_bus_fini(bus);
152         }
153
154         if ((mask = (1 << i2c->func->aux) - 1), i2c->func->aux_stat) {
155                 i2c->func->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
156                 i2c->func->aux_stat(i2c, &mask, &mask, &mask, &mask);
157         }
158
159         list_for_each_entry(pad, &i2c->pad, head) {
160                 nvkm_i2c_pad_fini(pad);
161         }
162
163         return 0;
164 }
165
166 static int
167 nvkm_i2c_preinit(struct nvkm_subdev *subdev)
168 {
169         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
170         struct nvkm_i2c_bus *bus;
171         struct nvkm_i2c_pad *pad;
172
173         /*
174          * We init our i2c busses as early as possible, since they may be
175          * needed by the vbios init scripts on some cards
176          */
177         list_for_each_entry(pad, &i2c->pad, head)
178                 nvkm_i2c_pad_init(pad);
179         list_for_each_entry(bus, &i2c->bus, head)
180                 nvkm_i2c_bus_init(bus);
181
182         return 0;
183 }
184
185 static int
186 nvkm_i2c_init(struct nvkm_subdev *subdev)
187 {
188         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
189         struct nvkm_i2c_bus *bus;
190         struct nvkm_i2c_pad *pad;
191         struct nvkm_i2c_aux *aux;
192
193         list_for_each_entry(pad, &i2c->pad, head) {
194                 nvkm_i2c_pad_init(pad);
195         }
196
197         list_for_each_entry(bus, &i2c->bus, head) {
198                 nvkm_i2c_bus_init(bus);
199         }
200
201         list_for_each_entry(aux, &i2c->aux, head) {
202                 nvkm_i2c_aux_init(aux);
203         }
204
205         return 0;
206 }
207
208 static void *
209 nvkm_i2c_dtor(struct nvkm_subdev *subdev)
210 {
211         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
212
213         nvkm_event_fini(&i2c->event);
214
215         while (!list_empty(&i2c->aux)) {
216                 struct nvkm_i2c_aux *aux =
217                         list_first_entry(&i2c->aux, typeof(*aux), head);
218                 nvkm_i2c_aux_del(&aux);
219         }
220
221         while (!list_empty(&i2c->bus)) {
222                 struct nvkm_i2c_bus *bus =
223                         list_first_entry(&i2c->bus, typeof(*bus), head);
224                 nvkm_i2c_bus_del(&bus);
225         }
226
227         while (!list_empty(&i2c->pad)) {
228                 struct nvkm_i2c_pad *pad =
229                         list_first_entry(&i2c->pad, typeof(*pad), head);
230                 nvkm_i2c_pad_del(&pad);
231         }
232
233         return i2c;
234 }
235
236 static const struct nvkm_subdev_func
237 nvkm_i2c = {
238         .dtor = nvkm_i2c_dtor,
239         .preinit = nvkm_i2c_preinit,
240         .init = nvkm_i2c_init,
241         .fini = nvkm_i2c_fini,
242         .intr = nvkm_i2c_intr,
243 };
244
245 static const struct nvkm_i2c_drv {
246         u8 bios;
247         u8 addr;
248         int (*pad_new)(struct nvkm_i2c_bus *, int id, u8 addr,
249                        struct nvkm_i2c_pad **);
250 }
251 nvkm_i2c_drv[] = {
252         { 0x0d, 0x39, anx9805_pad_new },
253         { 0x0e, 0x3b, anx9805_pad_new },
254         {}
255 };
256
257 int
258 nvkm_i2c_new_(const struct nvkm_i2c_func *func, struct nvkm_device *device,
259               enum nvkm_subdev_type type, int inst, struct nvkm_i2c **pi2c)
260 {
261         struct nvkm_bios *bios = device->bios;
262         struct nvkm_i2c *i2c;
263         struct dcb_i2c_entry ccbE;
264         struct dcb_output dcbE;
265         u8 ver, hdr;
266         int ret, i;
267
268         if (!(i2c = *pi2c = kzalloc(sizeof(*i2c), GFP_KERNEL)))
269                 return -ENOMEM;
270
271         nvkm_subdev_ctor(&nvkm_i2c, device, type, inst, &i2c->subdev);
272         i2c->func = func;
273         INIT_LIST_HEAD(&i2c->pad);
274         INIT_LIST_HEAD(&i2c->bus);
275         INIT_LIST_HEAD(&i2c->aux);
276
277         i = -1;
278         while (!dcb_i2c_parse(bios, ++i, &ccbE)) {
279                 struct nvkm_i2c_pad *pad = NULL;
280                 struct nvkm_i2c_bus *bus = NULL;
281                 struct nvkm_i2c_aux *aux = NULL;
282
283                 nvkm_debug(&i2c->subdev, "ccb %02x: type %02x drive %02x "
284                            "sense %02x share %02x auxch %02x\n", i, ccbE.type,
285                            ccbE.drive, ccbE.sense, ccbE.share, ccbE.auxch);
286
287                 if (ccbE.share != DCB_I2C_UNUSED) {
288                         const int id = NVKM_I2C_PAD_HYBRID(ccbE.share);
289                         if (!(pad = nvkm_i2c_pad_find(i2c, id)))
290                                 ret = func->pad_s_new(i2c, id, &pad);
291                         else
292                                 ret = 0;
293                 } else {
294                         ret = func->pad_x_new(i2c, NVKM_I2C_PAD_CCB(i), &pad);
295                 }
296
297                 if (ret) {
298                         nvkm_error(&i2c->subdev, "ccb %02x pad, %d\n", i, ret);
299                         nvkm_i2c_pad_del(&pad);
300                         continue;
301                 }
302
303                 if (pad->func->bus_new_0 && ccbE.type == DCB_I2C_NV04_BIT) {
304                         ret = pad->func->bus_new_0(pad, NVKM_I2C_BUS_CCB(i),
305                                                    ccbE.drive,
306                                                    ccbE.sense, &bus);
307                 } else
308                 if (pad->func->bus_new_4 &&
309                     ( ccbE.type == DCB_I2C_NV4E_BIT ||
310                       ccbE.type == DCB_I2C_NVIO_BIT ||
311                      (ccbE.type == DCB_I2C_PMGR &&
312                       ccbE.drive != DCB_I2C_UNUSED))) {
313                         ret = pad->func->bus_new_4(pad, NVKM_I2C_BUS_CCB(i),
314                                                    ccbE.drive, &bus);
315                 }
316
317                 if (ret) {
318                         nvkm_error(&i2c->subdev, "ccb %02x bus, %d\n", i, ret);
319                         nvkm_i2c_bus_del(&bus);
320                 }
321
322                 if (pad->func->aux_new_6 &&
323                     ( ccbE.type == DCB_I2C_NVIO_AUX ||
324                      (ccbE.type == DCB_I2C_PMGR &&
325                       ccbE.auxch != DCB_I2C_UNUSED))) {
326                         ret = pad->func->aux_new_6(pad, NVKM_I2C_BUS_CCB(i),
327                                                    ccbE.auxch, &aux);
328                 } else {
329                         ret = 0;
330                 }
331
332                 if (ret) {
333                         nvkm_error(&i2c->subdev, "ccb %02x aux, %d\n", i, ret);
334                         nvkm_i2c_aux_del(&aux);
335                 }
336
337                 if (ccbE.type != DCB_I2C_UNUSED && !bus && !aux) {
338                         nvkm_warn(&i2c->subdev, "ccb %02x was ignored\n", i);
339                         continue;
340                 }
341         }
342
343         i = -1;
344         while (dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE)) {
345                 const struct nvkm_i2c_drv *drv = nvkm_i2c_drv;
346                 struct nvkm_i2c_bus *bus;
347                 struct nvkm_i2c_pad *pad;
348
349                 /* internal outputs handled by native i2c busses (above) */
350                 if (!dcbE.location)
351                         continue;
352
353                 /* we need an i2c bus to talk to the external encoder */
354                 bus = nvkm_i2c_bus_find(i2c, dcbE.i2c_index);
355                 if (!bus) {
356                         nvkm_debug(&i2c->subdev, "dcb %02x no bus\n", i);
357                         continue;
358                 }
359
360                 /* ... and a driver for it */
361                 while (drv->pad_new) {
362                         if (drv->bios == dcbE.extdev)
363                                 break;
364                         drv++;
365                 }
366
367                 if (!drv->pad_new) {
368                         nvkm_debug(&i2c->subdev, "dcb %02x drv %02x unknown\n",
369                                    i, dcbE.extdev);
370                         continue;
371                 }
372
373                 /* find/create an instance of the driver */
374                 pad = nvkm_i2c_pad_find(i2c, NVKM_I2C_PAD_EXT(dcbE.extdev));
375                 if (!pad) {
376                         const int id = NVKM_I2C_PAD_EXT(dcbE.extdev);
377                         ret = drv->pad_new(bus, id, drv->addr, &pad);
378                         if (ret) {
379                                 nvkm_error(&i2c->subdev, "dcb %02x pad, %d\n",
380                                            i, ret);
381                                 nvkm_i2c_pad_del(&pad);
382                                 continue;
383                         }
384                 }
385
386                 /* create any i2c bus / aux channel required by the output */
387                 if (pad->func->aux_new_6 && dcbE.type == DCB_OUTPUT_DP) {
388                         const int id = NVKM_I2C_AUX_EXT(dcbE.extdev);
389                         struct nvkm_i2c_aux *aux = NULL;
390                         ret = pad->func->aux_new_6(pad, id, 0, &aux);
391                         if (ret) {
392                                 nvkm_error(&i2c->subdev, "dcb %02x aux, %d\n",
393                                            i, ret);
394                                 nvkm_i2c_aux_del(&aux);
395                         }
396                 } else
397                 if (pad->func->bus_new_4) {
398                         const int id = NVKM_I2C_BUS_EXT(dcbE.extdev);
399                         struct nvkm_i2c_bus *bus = NULL;
400                         ret = pad->func->bus_new_4(pad, id, 0, &bus);
401                         if (ret) {
402                                 nvkm_error(&i2c->subdev, "dcb %02x bus, %d\n",
403                                            i, ret);
404                                 nvkm_i2c_bus_del(&bus);
405                         }
406                 }
407         }
408
409         return nvkm_event_init(&nvkm_i2c_intr_func, &i2c->subdev, 4, i, &i2c->event);
410 }