]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/nvkm/engine/disp/nv50.c
drm/nouveau/disp/dp: only check for re-train when the link is active
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / nvkm / engine / disp / nv50.c
1 /*
2  * Copyright 2012 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 "nv50.h"
25 #include "head.h"
26 #include "ior.h"
27 #include "rootnv50.h"
28
29 #include <core/client.h>
30 #include <core/enum.h>
31 #include <core/gpuobj.h>
32 #include <subdev/bios.h>
33 #include <subdev/bios/disp.h>
34 #include <subdev/bios/init.h>
35 #include <subdev/bios/pll.h>
36 #include <subdev/devinit.h>
37 #include <subdev/timer.h>
38
39 static const struct nvkm_disp_oclass *
40 nv50_disp_root_(struct nvkm_disp *base)
41 {
42         return nv50_disp(base)->func->root;
43 }
44
45 static int
46 nv50_disp_outp_internal_crt_(struct nvkm_disp *base, int index,
47                              struct dcb_output *dcb, struct nvkm_output **poutp)
48 {
49         struct nv50_disp *disp = nv50_disp(base);
50         return disp->func->outp.internal.crt(base, index, dcb, poutp);
51 }
52
53 static int
54 nv50_disp_outp_internal_tmds_(struct nvkm_disp *base, int index,
55                               struct dcb_output *dcb,
56                               struct nvkm_output **poutp)
57 {
58         struct nv50_disp *disp = nv50_disp(base);
59         return disp->func->outp.internal.tmds(base, index, dcb, poutp);
60 }
61
62 static int
63 nv50_disp_outp_internal_lvds_(struct nvkm_disp *base, int index,
64                               struct dcb_output *dcb,
65                               struct nvkm_output **poutp)
66 {
67         struct nv50_disp *disp = nv50_disp(base);
68         return disp->func->outp.internal.lvds(base, index, dcb, poutp);
69 }
70
71 static int
72 nv50_disp_outp_internal_dp_(struct nvkm_disp *base, int index,
73                             struct dcb_output *dcb, struct nvkm_output **poutp)
74 {
75         struct nv50_disp *disp = nv50_disp(base);
76         if (disp->func->outp.internal.dp)
77                 return disp->func->outp.internal.dp(base, index, dcb, poutp);
78         return -ENODEV;
79 }
80
81 static int
82 nv50_disp_outp_external_tmds_(struct nvkm_disp *base, int index,
83                               struct dcb_output *dcb,
84                               struct nvkm_output **poutp)
85 {
86         struct nv50_disp *disp = nv50_disp(base);
87         if (disp->func->outp.external.tmds)
88                 return disp->func->outp.external.tmds(base, index, dcb, poutp);
89         return -ENODEV;
90 }
91
92 static int
93 nv50_disp_outp_external_dp_(struct nvkm_disp *base, int index,
94                             struct dcb_output *dcb, struct nvkm_output **poutp)
95 {
96         struct nv50_disp *disp = nv50_disp(base);
97         if (disp->func->outp.external.dp)
98                 return disp->func->outp.external.dp(base, index, dcb, poutp);
99         return -ENODEV;
100 }
101
102 static void
103 nv50_disp_intr_(struct nvkm_disp *base)
104 {
105         struct nv50_disp *disp = nv50_disp(base);
106         disp->func->intr(disp);
107 }
108
109 static void *
110 nv50_disp_dtor_(struct nvkm_disp *base)
111 {
112         struct nv50_disp *disp = nv50_disp(base);
113         nvkm_event_fini(&disp->uevent);
114         return disp;
115 }
116
117 static const struct nvkm_disp_func
118 nv50_disp_ = {
119         .dtor = nv50_disp_dtor_,
120         .intr = nv50_disp_intr_,
121         .root = nv50_disp_root_,
122         .outp.internal.crt = nv50_disp_outp_internal_crt_,
123         .outp.internal.tmds = nv50_disp_outp_internal_tmds_,
124         .outp.internal.lvds = nv50_disp_outp_internal_lvds_,
125         .outp.internal.dp = nv50_disp_outp_internal_dp_,
126         .outp.external.tmds = nv50_disp_outp_external_tmds_,
127         .outp.external.dp = nv50_disp_outp_external_dp_,
128 };
129
130 int
131 nv50_disp_new_(const struct nv50_disp_func *func, struct nvkm_device *device,
132                int index, int heads, struct nvkm_disp **pdisp)
133 {
134         struct nv50_disp *disp;
135         int ret, i;
136
137         if (!(disp = kzalloc(sizeof(*disp), GFP_KERNEL)))
138                 return -ENOMEM;
139         INIT_WORK(&disp->supervisor, func->super);
140         disp->func = func;
141         *pdisp = &disp->base;
142
143         ret = nvkm_disp_ctor(&nv50_disp_, device, index, &disp->base);
144         if (ret)
145                 return ret;
146
147         for (i = 0; func->head.new && i < heads; i++) {
148                 ret = func->head.new(&disp->base, i);
149                 if (ret)
150                         return ret;
151         }
152
153         for (i = 0; func->dac.new && i < func->dac.nr; i++) {
154                 ret = func->dac.new(&disp->base, i);
155                 if (ret)
156                         return ret;
157         }
158
159         for (i = 0; func->pior.new && i < func->pior.nr; i++) {
160                 ret = func->pior.new(&disp->base, i);
161                 if (ret)
162                         return ret;
163         }
164
165         for (i = 0; func->sor.new && i < func->sor.nr; i++) {
166                 ret = func->sor.new(&disp->base, i);
167                 if (ret)
168                         return ret;
169         }
170
171         return nvkm_event_init(func->uevent, 1, 1 + (heads * 4), &disp->uevent);
172 }
173
174 static struct nvkm_output *
175 exec_lookup(struct nv50_disp *disp, int head, int or, u32 ctrl,
176             u32 *data, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
177             struct nvbios_outp *info)
178 {
179         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
180         struct nvkm_bios *bios = subdev->device->bios;
181         struct nvkm_output *outp;
182         u16 mask, type;
183
184         if (or < 4) {
185                 type = DCB_OUTPUT_ANALOG;
186                 mask = 0;
187         } else
188         if (or < 8) {
189                 switch (ctrl & 0x00000f00) {
190                 case 0x00000000: type = DCB_OUTPUT_LVDS; mask = 1; break;
191                 case 0x00000100: type = DCB_OUTPUT_TMDS; mask = 1; break;
192                 case 0x00000200: type = DCB_OUTPUT_TMDS; mask = 2; break;
193                 case 0x00000500: type = DCB_OUTPUT_TMDS; mask = 3; break;
194                 case 0x00000800: type = DCB_OUTPUT_DP; mask = 1; break;
195                 case 0x00000900: type = DCB_OUTPUT_DP; mask = 2; break;
196                 default:
197                         nvkm_error(subdev, "unknown SOR mc %08x\n", ctrl);
198                         return NULL;
199                 }
200                 or  -= 4;
201         } else {
202                 or   = or - 8;
203                 type = 0x0010;
204                 mask = 0;
205                 switch (ctrl & 0x00000f00) {
206                 case 0x00000000: type |= disp->pior.type[or]; break;
207                 default:
208                         nvkm_error(subdev, "unknown PIOR mc %08x\n", ctrl);
209                         return NULL;
210                 }
211         }
212
213         mask  = 0x00c0 & (mask << 6);
214         mask |= 0x0001 << or;
215         mask |= 0x0100 << head;
216
217         list_for_each_entry(outp, &disp->base.outp, head) {
218                 if ((outp->info.hasht & 0xff) == type &&
219                     (outp->info.hashm & mask) == mask) {
220                         *data = nvbios_outp_match(bios, outp->info.hasht, mask,
221                                                   ver, hdr, cnt, len, info);
222                         if (!*data)
223                                 return NULL;
224                         return outp;
225                 }
226         }
227
228         return NULL;
229 }
230
231 static struct nvkm_output *
232 exec_script(struct nv50_disp *disp, int head, int id)
233 {
234         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
235         struct nvkm_device *device = subdev->device;
236         struct nvkm_bios *bios = device->bios;
237         struct nvkm_output *outp;
238         struct nvbios_outp info;
239         u8  ver, hdr, cnt, len;
240         u32 data, ctrl = 0;
241         u32 reg;
242         int i;
243
244         /* DAC */
245         for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
246                 ctrl = nvkm_rd32(device, 0x610b5c + (i * 8));
247
248         /* SOR */
249         if (!(ctrl & (1 << head))) {
250                 if (device->chipset  < 0x90 ||
251                     device->chipset == 0x92 ||
252                     device->chipset == 0xa0) {
253                         reg = 0x610b74;
254                 } else {
255                         reg = 0x610798;
256                 }
257                 for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
258                         ctrl = nvkm_rd32(device, reg + (i * 8));
259                 i += 4;
260         }
261
262         /* PIOR */
263         if (!(ctrl & (1 << head))) {
264                 for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
265                         ctrl = nvkm_rd32(device, 0x610b84 + (i * 8));
266                 i += 8;
267         }
268
269         if (!(ctrl & (1 << head)))
270                 return NULL;
271         i--;
272
273         outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info);
274         if (outp) {
275                 struct nvbios_init init = {
276                         .subdev = subdev,
277                         .bios = bios,
278                         .offset = info.script[id],
279                         .outp = &outp->info,
280                         .crtc = head,
281                         .execute = 1,
282                 };
283
284                 nvbios_exec(&init);
285         }
286
287         return outp;
288 }
289
290 static struct nvkm_output *
291 exec_clkcmp(struct nv50_disp *disp, int head, int id, u32 pclk, u32 *conf)
292 {
293         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
294         struct nvkm_device *device = subdev->device;
295         struct nvkm_bios *bios = device->bios;
296         struct nvkm_output *outp;
297         struct nvbios_outp info1;
298         struct nvbios_ocfg info2;
299         u8  ver, hdr, cnt, len;
300         u32 data, ctrl = 0;
301         u32 reg;
302         int i;
303
304         /* DAC */
305         for (i = 0; !(ctrl & (1 << head)) && i < disp->func->dac.nr; i++)
306                 ctrl = nvkm_rd32(device, 0x610b58 + (i * 8));
307
308         /* SOR */
309         if (!(ctrl & (1 << head))) {
310                 if (device->chipset  < 0x90 ||
311                     device->chipset == 0x92 ||
312                     device->chipset == 0xa0) {
313                         reg = 0x610b70;
314                 } else {
315                         reg = 0x610794;
316                 }
317                 for (i = 0; !(ctrl & (1 << head)) && i < disp->func->sor.nr; i++)
318                         ctrl = nvkm_rd32(device, reg + (i * 8));
319                 i += 4;
320         }
321
322         /* PIOR */
323         if (!(ctrl & (1 << head))) {
324                 for (i = 0; !(ctrl & (1 << head)) && i < disp->func->pior.nr; i++)
325                         ctrl = nvkm_rd32(device, 0x610b80 + (i * 8));
326                 i += 8;
327         }
328
329         if (!(ctrl & (1 << head)))
330                 return NULL;
331         i--;
332
333         outp = exec_lookup(disp, head, i, ctrl, &data, &ver, &hdr, &cnt, &len, &info1);
334         if (!outp)
335                 return NULL;
336
337         *conf = (ctrl & 0x00000f00) >> 8;
338         if (outp->info.location == 0) {
339                 switch (outp->info.type) {
340                 case DCB_OUTPUT_TMDS:
341                         if (*conf == 5)
342                                 *conf |= 0x0100;
343                         break;
344                 case DCB_OUTPUT_LVDS:
345                         *conf |= disp->sor.lvdsconf;
346                         break;
347                 default:
348                         break;
349                 }
350         } else {
351                 *conf = (ctrl & 0x00000f00) >> 8;
352                 pclk = pclk / 2;
353         }
354
355         data = nvbios_ocfg_match(bios, data, *conf & 0xff, *conf >> 8,
356                                  &ver, &hdr, &cnt, &len, &info2);
357         if (data && id < 0xff) {
358                 data = nvbios_oclk_match(bios, info2.clkcmp[id], pclk);
359                 if (data) {
360                         struct nvbios_init init = {
361                                 .subdev = subdev,
362                                 .bios = bios,
363                                 .offset = data,
364                                 .outp = &outp->info,
365                                 .crtc = head,
366                                 .execute = 1,
367                         };
368
369                         nvbios_exec(&init);
370                 }
371         }
372
373         return outp;
374 }
375
376 /* If programming a TMDS output on a SOR that can also be configured for
377  * DisplayPort, make sure NV50_SOR_DP_CTRL_ENABLE is forced off.
378  *
379  * It looks like the VBIOS TMDS scripts make an attempt at this, however,
380  * the VBIOS scripts on at least one board I have only switch it off on
381  * link 0, causing a blank display if the output has previously been
382  * programmed for DisplayPort.
383  */
384 static void
385 nv50_disp_intr_unk40_0_tmds(struct nv50_disp *disp,
386                             struct dcb_output *outp)
387 {
388         struct nvkm_device *device = disp->base.engine.subdev.device;
389         struct nvkm_bios *bios = device->bios;
390         const int link = !(outp->sorconf.link & 1);
391         const int   or = ffs(outp->or) - 1;
392         const u32 loff = (or * 0x800) + (link * 0x80);
393         const u16 mask = (outp->sorconf.link << 6) | outp->or;
394         struct dcb_output match;
395         u8  ver, hdr;
396
397         if (dcb_outp_match(bios, DCB_OUTPUT_DP, mask, &ver, &hdr, &match))
398                 nvkm_mask(device, 0x61c10c + loff, 0x00000001, 0x00000000);
399 }
400
401 static void
402 nv50_disp_intr_unk40_0(struct nv50_disp *disp, int head)
403 {
404         struct nvkm_device *device = disp->base.engine.subdev.device;
405         struct nvkm_output *outp;
406         u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
407         u32 conf;
408
409         outp = exec_clkcmp(disp, head, 1, pclk, &conf);
410         if (!outp)
411                 return;
412
413         if (outp->info.location == 0 && outp->info.type == DCB_OUTPUT_TMDS)
414                 nv50_disp_intr_unk40_0_tmds(disp, &outp->info);
415         nv50_disp_dptmds_war_3(disp, &outp->info);
416 }
417
418 static void
419 nv50_disp_intr_unk20_2_dp(struct nv50_disp *disp, int head,
420                           struct dcb_output *outp, u32 pclk)
421 {
422         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
423         struct nvkm_device *device = subdev->device;
424         const int link = !(outp->sorconf.link & 1);
425         const int   or = ffs(outp->or) - 1;
426         const u32 soff = (  or * 0x800);
427         const u32 loff = (link * 0x080) + soff;
428         const u32 ctrl = nvkm_rd32(device, 0x610794 + (or * 8));
429         const u32 symbol = 100000;
430         const s32 vactive = nvkm_rd32(device, 0x610af8 + (head * 0x540)) & 0xffff;
431         const s32 vblanke = nvkm_rd32(device, 0x610ae8 + (head * 0x540)) & 0xffff;
432         const s32 vblanks = nvkm_rd32(device, 0x610af0 + (head * 0x540)) & 0xffff;
433         u32 dpctrl = nvkm_rd32(device, 0x61c10c + loff);
434         u32 clksor = nvkm_rd32(device, 0x614300 + soff);
435         int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
436         int TU, VTUi, VTUf, VTUa;
437         u64 link_data_rate, link_ratio, unk;
438         u32 best_diff = 64 * symbol;
439         u32 link_nr, link_bw, bits;
440         u64 value;
441
442         link_bw = (clksor & 0x000c0000) ? 270000 : 162000;
443         link_nr = hweight32(dpctrl & 0x000f0000);
444
445         /* symbols/hblank - algorithm taken from comments in tegra driver */
446         value = vblanke + vactive - vblanks - 7;
447         value = value * link_bw;
448         do_div(value, pclk);
449         value = value - (3 * !!(dpctrl & 0x00004000)) - (12 / link_nr);
450         nvkm_mask(device, 0x61c1e8 + soff, 0x0000ffff, value);
451
452         /* symbols/vblank - algorithm taken from comments in tegra driver */
453         value = vblanks - vblanke - 25;
454         value = value * link_bw;
455         do_div(value, pclk);
456         value = value - ((36 / link_nr) + 3) - 1;
457         nvkm_mask(device, 0x61c1ec + soff, 0x00ffffff, value);
458
459         /* watermark / activesym */
460         if      ((ctrl & 0xf0000) == 0x60000) bits = 30;
461         else if ((ctrl & 0xf0000) == 0x50000) bits = 24;
462         else                                  bits = 18;
463
464         link_data_rate = (pclk * bits / 8) / link_nr;
465
466         /* calculate ratio of packed data rate to link symbol rate */
467         link_ratio = link_data_rate * symbol;
468         do_div(link_ratio, link_bw);
469
470         for (TU = 64; TU >= 32; TU--) {
471                 /* calculate average number of valid symbols in each TU */
472                 u32 tu_valid = link_ratio * TU;
473                 u32 calc, diff;
474
475                 /* find a hw representation for the fraction.. */
476                 VTUi = tu_valid / symbol;
477                 calc = VTUi * symbol;
478                 diff = tu_valid - calc;
479                 if (diff) {
480                         if (diff >= (symbol / 2)) {
481                                 VTUf = symbol / (symbol - diff);
482                                 if (symbol - (VTUf * diff))
483                                         VTUf++;
484
485                                 if (VTUf <= 15) {
486                                         VTUa  = 1;
487                                         calc += symbol - (symbol / VTUf);
488                                 } else {
489                                         VTUa  = 0;
490                                         VTUf  = 1;
491                                         calc += symbol;
492                                 }
493                         } else {
494                                 VTUa  = 0;
495                                 VTUf  = min((int)(symbol / diff), 15);
496                                 calc += symbol / VTUf;
497                         }
498
499                         diff = calc - tu_valid;
500                 } else {
501                         /* no remainder, but the hw doesn't like the fractional
502                          * part to be zero.  decrement the integer part and
503                          * have the fraction add a whole symbol back
504                          */
505                         VTUa = 0;
506                         VTUf = 1;
507                         VTUi--;
508                 }
509
510                 if (diff < best_diff) {
511                         best_diff = diff;
512                         bestTU = TU;
513                         bestVTUa = VTUa;
514                         bestVTUf = VTUf;
515                         bestVTUi = VTUi;
516                         if (diff == 0)
517                                 break;
518                 }
519         }
520
521         if (!bestTU) {
522                 nvkm_error(subdev, "unable to find suitable dp config\n");
523                 return;
524         }
525
526         /* XXX close to vbios numbers, but not right */
527         unk  = (symbol - link_ratio) * bestTU;
528         unk *= link_ratio;
529         do_div(unk, symbol);
530         do_div(unk, symbol);
531         unk += 6;
532
533         nvkm_mask(device, 0x61c10c + loff, 0x000001fc, bestTU << 2);
534         nvkm_mask(device, 0x61c128 + loff, 0x010f7f3f, bestVTUa << 24 |
535                                                    bestVTUf << 16 |
536                                                    bestVTUi << 8 | unk);
537 }
538
539 static void
540 nv50_disp_intr_unk20_2(struct nv50_disp *disp, int head)
541 {
542         struct nvkm_device *device = disp->base.engine.subdev.device;
543         struct nvkm_output *outp;
544         u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
545         u32 hval, hreg = 0x614200 + (head * 0x800);
546         u32 oval, oreg;
547         u32 mask, conf;
548
549         outp = exec_clkcmp(disp, head, 0xff, pclk, &conf);
550         if (!outp)
551                 return;
552
553         /* we allow both encoder attach and detach operations to occur
554          * within a single supervisor (ie. modeset) sequence.  the
555          * encoder detach scripts quite often switch off power to the
556          * lanes, which requires the link to be re-trained.
557          *
558          * this is not generally an issue as the sink "must" (heh)
559          * signal an irq when it's lost sync so the driver can
560          * re-train.
561          *
562          * however, on some boards, if one does not configure at least
563          * the gpu side of the link *before* attaching, then various
564          * things can go horribly wrong (PDISP disappearing from mmio,
565          * third supervisor never happens, etc).
566          *
567          * the solution is simply to retrain here, if necessary.  last
568          * i checked, the binary driver userspace does not appear to
569          * trigger this situation (it forces an UPDATE between steps).
570          */
571         if (outp->info.type == DCB_OUTPUT_DP) {
572                 u32 soff = (ffs(outp->info.or) - 1) * 0x08;
573                 u32 ctrl, datarate;
574
575                 if (outp->info.location == 0) {
576                         ctrl = nvkm_rd32(device, 0x610794 + soff);
577                         soff = 1;
578                 } else {
579                         ctrl = nvkm_rd32(device, 0x610b80 + soff);
580                         soff = 2;
581                 }
582
583                 switch ((ctrl & 0x000f0000) >> 16) {
584                 case 6: datarate = pclk * 30; break;
585                 case 5: datarate = pclk * 24; break;
586                 case 2:
587                 default:
588                         datarate = pclk * 18;
589                         break;
590                 }
591
592                 if (nvkm_output_dp_train(outp, datarate / soff))
593                         OUTP_ERR(outp, "link not trained before attach");
594         }
595
596         exec_clkcmp(disp, head, 0, pclk, &conf);
597
598         if (!outp->info.location && outp->info.type == DCB_OUTPUT_ANALOG) {
599                 oreg = 0x614280 + (ffs(outp->info.or) - 1) * 0x800;
600                 oval = 0x00000000;
601                 hval = 0x00000000;
602                 mask = 0xffffffff;
603         } else
604         if (!outp->info.location) {
605                 if (outp->info.type == DCB_OUTPUT_DP)
606                         nv50_disp_intr_unk20_2_dp(disp, head, &outp->info, pclk);
607                 oreg = 0x614300 + (ffs(outp->info.or) - 1) * 0x800;
608                 oval = (conf & 0x0100) ? 0x00000101 : 0x00000000;
609                 hval = 0x00000000;
610                 mask = 0x00000707;
611         } else {
612                 oreg = 0x614380 + (ffs(outp->info.or) - 1) * 0x800;
613                 oval = 0x00000001;
614                 hval = 0x00000001;
615                 mask = 0x00000707;
616         }
617
618         nvkm_mask(device, hreg, 0x0000000f, hval);
619         nvkm_mask(device, oreg, mask, oval);
620
621         nv50_disp_dptmds_war_2(disp, &outp->info);
622 }
623
624 static void
625 nv50_disp_intr_unk20_1(struct nv50_disp *disp, int head)
626 {
627         struct nvkm_device *device = disp->base.engine.subdev.device;
628         struct nvkm_devinit *devinit = device->devinit;
629         u32 pclk = nvkm_rd32(device, 0x610ad0 + (head * 0x540)) & 0x3fffff;
630         if (pclk)
631                 nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head, pclk);
632 }
633
634 static void
635 nv50_disp_intr_unk20_0(struct nv50_disp *disp, int head)
636 {
637         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
638         struct nvkm_output *outp = exec_script(disp, head, 2);
639
640         /* the binary driver does this outside of the supervisor handling
641          * (after the third supervisor from a detach).  we (currently?)
642          * allow both detach/attach to happen in the same set of
643          * supervisor interrupts, so it would make sense to execute this
644          * (full power down?) script after all the detach phases of the
645          * supervisor handling.  like with training if needed from the
646          * second supervisor, nvidia doesn't do this, so who knows if it's
647          * entirely safe, but it does appear to work..
648          *
649          * without this script being run, on some configurations i've
650          * seen, switching from DP to TMDS on a DP connector may result
651          * in a blank screen (SOR_PWR off/on can restore it)
652          */
653         if (outp && outp->info.type == DCB_OUTPUT_DP) {
654                 struct nvkm_output_dp *outpdp = nvkm_output_dp(outp);
655                 struct nvbios_init init = {
656                         .subdev = subdev,
657                         .bios = subdev->device->bios,
658                         .outp = &outp->info,
659                         .crtc = head,
660                         .offset = outpdp->info.script[4],
661                         .execute = 1,
662                 };
663
664                 atomic_set(&outpdp->lt.done, 0);
665                 nvbios_exec(&init);
666         }
667 }
668
669 static void
670 nv50_disp_intr_unk10_0(struct nv50_disp *disp, int head)
671 {
672         exec_script(disp, head, 1);
673 }
674
675 void
676 nv50_disp_super(struct work_struct *work)
677 {
678         struct nv50_disp *disp =
679                 container_of(work, struct nv50_disp, supervisor);
680         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
681         struct nvkm_device *device = subdev->device;
682         struct nvkm_head *head;
683         u32 super = nvkm_rd32(device, 0x610030);
684
685         nvkm_debug(subdev, "supervisor %08x %08x\n", disp->super, super);
686
687         if (disp->super & 0x00000010) {
688                 nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
689                 list_for_each_entry(head, &disp->base.head, head) {
690                         if (!(super & (0x00000020 << head->id)))
691                                 continue;
692                         if (!(super & (0x00000080 << head->id)))
693                                 continue;
694                         nv50_disp_intr_unk10_0(disp, head->id);
695                 }
696         } else
697         if (disp->super & 0x00000020) {
698                 list_for_each_entry(head, &disp->base.head, head) {
699                         if (!(super & (0x00000080 << head->id)))
700                                 continue;
701                         nv50_disp_intr_unk20_0(disp, head->id);
702                 }
703                 list_for_each_entry(head, &disp->base.head, head) {
704                         if (!(super & (0x00000200 << head->id)))
705                                 continue;
706                         nv50_disp_intr_unk20_1(disp, head->id);
707                 }
708                 list_for_each_entry(head, &disp->base.head, head) {
709                         if (!(super & (0x00000080 << head->id)))
710                                 continue;
711                         nv50_disp_intr_unk20_2(disp, head->id);
712                 }
713         } else
714         if (disp->super & 0x00000040) {
715                 list_for_each_entry(head, &disp->base.head, head) {
716                         if (!(super & (0x00000080 << head->id)))
717                                 continue;
718                         nv50_disp_intr_unk40_0(disp, head->id);
719                 }
720                 nv50_disp_update_sppll1(disp);
721         }
722
723         nvkm_wr32(device, 0x610030, 0x80000000);
724 }
725
726 static const struct nvkm_enum
727 nv50_disp_intr_error_type[] = {
728         { 3, "ILLEGAL_MTHD" },
729         { 4, "INVALID_VALUE" },
730         { 5, "INVALID_STATE" },
731         { 7, "INVALID_HANDLE" },
732         {}
733 };
734
735 static const struct nvkm_enum
736 nv50_disp_intr_error_code[] = {
737         { 0x00, "" },
738         {}
739 };
740
741 static void
742 nv50_disp_intr_error(struct nv50_disp *disp, int chid)
743 {
744         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
745         struct nvkm_device *device = subdev->device;
746         u32 data = nvkm_rd32(device, 0x610084 + (chid * 0x08));
747         u32 addr = nvkm_rd32(device, 0x610080 + (chid * 0x08));
748         u32 code = (addr & 0x00ff0000) >> 16;
749         u32 type = (addr & 0x00007000) >> 12;
750         u32 mthd = (addr & 0x00000ffc);
751         const struct nvkm_enum *ec, *et;
752
753         et = nvkm_enum_find(nv50_disp_intr_error_type, type);
754         ec = nvkm_enum_find(nv50_disp_intr_error_code, code);
755
756         nvkm_error(subdev,
757                    "ERROR %d [%s] %02x [%s] chid %d mthd %04x data %08x\n",
758                    type, et ? et->name : "", code, ec ? ec->name : "",
759                    chid, mthd, data);
760
761         if (chid < ARRAY_SIZE(disp->chan)) {
762                 switch (mthd) {
763                 case 0x0080:
764                         nv50_disp_chan_mthd(disp->chan[chid], NV_DBG_ERROR);
765                         break;
766                 default:
767                         break;
768                 }
769         }
770
771         nvkm_wr32(device, 0x610020, 0x00010000 << chid);
772         nvkm_wr32(device, 0x610080 + (chid * 0x08), 0x90000000);
773 }
774
775 void
776 nv50_disp_intr(struct nv50_disp *disp)
777 {
778         struct nvkm_device *device = disp->base.engine.subdev.device;
779         u32 intr0 = nvkm_rd32(device, 0x610020);
780         u32 intr1 = nvkm_rd32(device, 0x610024);
781
782         while (intr0 & 0x001f0000) {
783                 u32 chid = __ffs(intr0 & 0x001f0000) - 16;
784                 nv50_disp_intr_error(disp, chid);
785                 intr0 &= ~(0x00010000 << chid);
786         }
787
788         while (intr0 & 0x0000001f) {
789                 u32 chid = __ffs(intr0 & 0x0000001f);
790                 nv50_disp_chan_uevent_send(disp, chid);
791                 intr0 &= ~(0x00000001 << chid);
792         }
793
794         if (intr1 & 0x00000004) {
795                 nvkm_disp_vblank(&disp->base, 0);
796                 nvkm_wr32(device, 0x610024, 0x00000004);
797         }
798
799         if (intr1 & 0x00000008) {
800                 nvkm_disp_vblank(&disp->base, 1);
801                 nvkm_wr32(device, 0x610024, 0x00000008);
802         }
803
804         if (intr1 & 0x00000070) {
805                 disp->super = (intr1 & 0x00000070);
806                 schedule_work(&disp->supervisor);
807                 nvkm_wr32(device, 0x610024, disp->super);
808         }
809 }
810
811 static const struct nv50_disp_func
812 nv50_disp = {
813         .intr = nv50_disp_intr,
814         .uevent = &nv50_disp_chan_uevent,
815         .super = nv50_disp_super,
816         .root = &nv50_disp_root_oclass,
817         .head.new = nv50_head_new,
818         .outp.internal.crt = nv50_dac_output_new,
819         .outp.internal.tmds = nv50_sor_output_new,
820         .outp.internal.lvds = nv50_sor_output_new,
821         .outp.external.tmds = nv50_pior_output_new,
822         .outp.external.dp = nv50_pior_dp_new,
823         .dac.nr = 3,
824         .dac.new = nv50_dac_new,
825         .dac.power = nv50_dac_power,
826         .dac.sense = nv50_dac_sense,
827         .sor.nr = 2,
828         .sor.new = nv50_sor_new,
829         .sor.power = nv50_sor_power,
830         .pior.nr = 3,
831         .pior.new = nv50_pior_new,
832         .pior.power = nv50_pior_power,
833 };
834
835 int
836 nv50_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
837 {
838         return nv50_disp_new_(&nv50_disp, device, index, 2, pdisp);
839 }