2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
30 #include <core/client.h>
31 #include <core/enum.h>
32 #include <core/ramht.h>
33 #include <subdev/bios.h>
34 #include <subdev/bios/disp.h>
35 #include <subdev/bios/init.h>
36 #include <subdev/bios/pll.h>
37 #include <subdev/devinit.h>
38 #include <subdev/timer.h>
40 static const struct nvkm_disp_oclass *
41 nv50_disp_root_(struct nvkm_disp *base)
43 return nv50_disp(base)->func->root;
47 nv50_disp_intr_(struct nvkm_disp *base)
49 struct nv50_disp *disp = nv50_disp(base);
50 disp->func->intr(disp);
54 nv50_disp_fini_(struct nvkm_disp *base)
56 struct nv50_disp *disp = nv50_disp(base);
57 disp->func->fini(disp);
61 nv50_disp_init_(struct nvkm_disp *base)
63 struct nv50_disp *disp = nv50_disp(base);
64 return disp->func->init(disp);
68 nv50_disp_dtor_(struct nvkm_disp *base)
70 struct nv50_disp *disp = nv50_disp(base);
72 nvkm_ramht_del(&disp->ramht);
73 nvkm_gpuobj_del(&disp->inst);
75 nvkm_event_fini(&disp->uevent);
77 destroy_workqueue(disp->wq);
83 nv50_disp_oneinit_(struct nvkm_disp *base)
85 struct nv50_disp *disp = nv50_disp(base);
86 const struct nv50_disp_func *func = disp->func;
87 struct nvkm_subdev *subdev = &disp->base.engine.subdev;
88 struct nvkm_device *device = subdev->device;
92 disp->wndw.nr = func->wndw.cnt(&disp->base, &disp->wndw.mask);
93 nvkm_debug(subdev, "Window(s): %d (%08lx)\n",
94 disp->wndw.nr, disp->wndw.mask);
97 disp->head.nr = func->head.cnt(&disp->base, &disp->head.mask);
98 nvkm_debug(subdev, " Head(s): %d (%02lx)\n",
99 disp->head.nr, disp->head.mask);
100 for_each_set_bit(i, &disp->head.mask, disp->head.nr) {
101 ret = func->head.new(&disp->base, i);
107 disp->dac.nr = func->dac.cnt(&disp->base, &disp->dac.mask);
108 nvkm_debug(subdev, " DAC(s): %d (%02lx)\n",
109 disp->dac.nr, disp->dac.mask);
110 for_each_set_bit(i, &disp->dac.mask, disp->dac.nr) {
111 ret = func->dac.new(&disp->base, i);
117 if (func->pior.cnt) {
118 disp->pior.nr = func->pior.cnt(&disp->base, &disp->pior.mask);
119 nvkm_debug(subdev, " PIOR(s): %d (%02lx)\n",
120 disp->pior.nr, disp->pior.mask);
121 for_each_set_bit(i, &disp->pior.mask, disp->pior.nr) {
122 ret = func->pior.new(&disp->base, i);
128 disp->sor.nr = func->sor.cnt(&disp->base, &disp->sor.mask);
129 nvkm_debug(subdev, " SOR(s): %d (%02lx)\n",
130 disp->sor.nr, disp->sor.mask);
131 for_each_set_bit(i, &disp->sor.mask, disp->sor.nr) {
132 ret = func->sor.new(&disp->base, i);
137 ret = nvkm_gpuobj_new(device, 0x10000, 0x10000, false, NULL,
142 return nvkm_ramht_new(device, func->ramht_size ? func->ramht_size :
143 0x1000, 0, disp->inst, &disp->ramht);
146 static const struct nvkm_disp_func
148 .dtor = nv50_disp_dtor_,
149 .oneinit = nv50_disp_oneinit_,
150 .init = nv50_disp_init_,
151 .fini = nv50_disp_fini_,
152 .intr = nv50_disp_intr_,
153 .root = nv50_disp_root_,
157 nv50_disp_new_(const struct nv50_disp_func *func, struct nvkm_device *device,
158 int index, struct nvkm_disp **pdisp)
160 struct nv50_disp *disp;
163 if (!(disp = kzalloc(sizeof(*disp), GFP_KERNEL)))
166 *pdisp = &disp->base;
168 ret = nvkm_disp_ctor(&nv50_disp_, device, index, &disp->base);
172 disp->wq = create_singlethread_workqueue("nvkm-disp");
176 INIT_WORK(&disp->supervisor, func->super);
178 return nvkm_event_init(func->uevent, 1, ARRAY_SIZE(disp->chan),
183 nv50_disp_super_iedt(struct nvkm_head *head, struct nvkm_outp *outp,
184 u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
185 struct nvbios_outp *iedt)
187 struct nvkm_bios *bios = head->disp->engine.subdev.device->bios;
188 const u8 l = ffs(outp->info.link);
189 const u16 t = outp->info.hasht;
190 const u16 m = (0x0100 << head->id) | (l << 6) | outp->info.or;
191 u32 data = nvbios_outp_match(bios, t, m, ver, hdr, cnt, len, iedt);
193 OUTP_DBG(outp, "missing IEDT for %04x:%04x", t, m);
198 nv50_disp_super_ied_on(struct nvkm_head *head,
199 struct nvkm_ior *ior, int id, u32 khz)
201 struct nvkm_subdev *subdev = &head->disp->engine.subdev;
202 struct nvkm_bios *bios = subdev->device->bios;
203 struct nvkm_outp *outp = ior->asy.outp;
204 struct nvbios_ocfg iedtrs;
205 struct nvbios_outp iedt;
206 u8 ver, hdr, cnt, len, flags = 0x00;
210 IOR_DBG(ior, "nothing to attach");
214 /* Lookup IED table for the device. */
215 data = nv50_disp_super_iedt(head, outp, &ver, &hdr, &cnt, &len, &iedt);
219 /* Lookup IEDT runtime settings for the current configuration. */
220 if (ior->type == SOR) {
221 if (ior->asy.proto == LVDS) {
222 if (head->asy.or.depth == 24)
225 if (ior->asy.link == 3)
229 data = nvbios_ocfg_match(bios, data, ior->asy.proto_evo, flags,
230 &ver, &hdr, &cnt, &len, &iedtrs);
232 OUTP_DBG(outp, "missing IEDT RS for %02x:%02x",
233 ior->asy.proto_evo, flags);
237 /* Execute the OnInt[23] script for the current frequency. */
238 data = nvbios_oclk_match(bios, iedtrs.clkcmp[id], khz);
240 OUTP_DBG(outp, "missing IEDT RSS %d for %02x:%02x %d khz",
241 id, ior->asy.proto_evo, flags, khz);
245 nvbios_init(subdev, data,
246 init.outp = &outp->info;
248 init.link = ior->asy.link;
249 init.head = head->id;
254 nv50_disp_super_ied_off(struct nvkm_head *head, struct nvkm_ior *ior, int id)
256 struct nvkm_outp *outp = ior->arm.outp;
257 struct nvbios_outp iedt;
258 u8 ver, hdr, cnt, len;
262 IOR_DBG(ior, "nothing attached");
266 data = nv50_disp_super_iedt(head, outp, &ver, &hdr, &cnt, &len, &iedt);
270 nvbios_init(&head->disp->engine.subdev, iedt.script[id],
271 init.outp = &outp->info;
273 init.link = ior->arm.link;
274 init.head = head->id;
278 static struct nvkm_ior *
279 nv50_disp_super_ior_asy(struct nvkm_head *head)
281 struct nvkm_ior *ior;
282 list_for_each_entry(ior, &head->disp->ior, head) {
283 if (ior->asy.head & (1 << head->id)) {
284 HEAD_DBG(head, "to %s", ior->name);
288 HEAD_DBG(head, "nothing to attach");
292 static struct nvkm_ior *
293 nv50_disp_super_ior_arm(struct nvkm_head *head)
295 struct nvkm_ior *ior;
296 list_for_each_entry(ior, &head->disp->ior, head) {
297 if (ior->arm.head & (1 << head->id)) {
298 HEAD_DBG(head, "on %s", ior->name);
302 HEAD_DBG(head, "nothing attached");
307 nv50_disp_super_3_0(struct nv50_disp *disp, struct nvkm_head *head)
309 struct nvkm_ior *ior;
311 /* Determine which OR, if any, we're attaching to the head. */
312 HEAD_DBG(head, "supervisor 3.0");
313 ior = nv50_disp_super_ior_asy(head);
317 /* Execute OnInt3 IED script. */
318 nv50_disp_super_ied_on(head, ior, 1, head->asy.hz / 1000);
320 /* OR-specific handling. */
321 if (ior->func->war_3)
322 ior->func->war_3(ior);
326 nv50_disp_super_2_2_dp(struct nvkm_head *head, struct nvkm_ior *ior)
328 struct nvkm_subdev *subdev = &head->disp->engine.subdev;
329 const u32 khz = head->asy.hz / 1000;
330 const u32 linkKBps = ior->dp.bw * 27000;
331 const u32 symbol = 100000;
332 int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
333 int TU, VTUi, VTUf, VTUa;
334 u64 link_data_rate, link_ratio, unk;
335 u32 best_diff = 64 * symbol;
338 /* symbols/hblank - algorithm taken from comments in tegra driver */
339 h = head->asy.hblanke + head->asy.htotal - head->asy.hblanks - 7;
342 h = h - (3 * ior->dp.ef) - (12 / ior->dp.nr);
344 /* symbols/vblank - algorithm taken from comments in tegra driver */
345 v = head->asy.vblanks - head->asy.vblanke - 25;
348 v = v - ((36 / ior->dp.nr) + 3) - 1;
350 ior->func->dp.audio_sym(ior, head->id, h, v);
352 /* watermark / activesym */
353 link_data_rate = (khz * head->asy.or.depth / 8) / ior->dp.nr;
355 /* calculate ratio of packed data rate to link symbol rate */
356 link_ratio = link_data_rate * symbol;
357 do_div(link_ratio, linkKBps);
359 for (TU = 64; ior->func->dp.activesym && TU >= 32; TU--) {
360 /* calculate average number of valid symbols in each TU */
361 u32 tu_valid = link_ratio * TU;
364 /* find a hw representation for the fraction.. */
365 VTUi = tu_valid / symbol;
366 calc = VTUi * symbol;
367 diff = tu_valid - calc;
369 if (diff >= (symbol / 2)) {
370 VTUf = symbol / (symbol - diff);
371 if (symbol - (VTUf * diff))
376 calc += symbol - (symbol / VTUf);
384 VTUf = min((int)(symbol / diff), 15);
385 calc += symbol / VTUf;
388 diff = calc - tu_valid;
390 /* no remainder, but the hw doesn't like the fractional
391 * part to be zero. decrement the integer part and
392 * have the fraction add a whole symbol back
399 if (diff < best_diff) {
410 if (ior->func->dp.activesym) {
412 nvkm_error(subdev, "unable to determine dp config\n");
415 ior->func->dp.activesym(ior, head->id, bestTU,
416 bestVTUa, bestVTUf, bestVTUi);
421 /* XXX close to vbios numbers, but not right */
422 unk = (symbol - link_ratio) * bestTU;
428 ior->func->dp.watermark(ior, head->id, unk);
432 nv50_disp_super_2_2(struct nv50_disp *disp, struct nvkm_head *head)
434 const u32 khz = head->asy.hz / 1000;
435 struct nvkm_outp *outp;
436 struct nvkm_ior *ior;
438 /* Determine which OR, if any, we're attaching from the head. */
439 HEAD_DBG(head, "supervisor 2.2");
440 ior = nv50_disp_super_ior_asy(head);
444 /* For some reason, NVIDIA decided not to:
446 * A) Give dual-link LVDS a separate EVO protocol, like for TMDS.
448 * B) Use SetControlOutputResource.PixelDepth on LVDS.
450 * Override the values we usually read from HW with the same
451 * data we pass though an ioctl instead.
453 if (ior->type == SOR && ior->asy.proto == LVDS) {
454 head->asy.or.depth = (disp->sor.lvdsconf & 0x0200) ? 24 : 18;
455 ior->asy.link = (disp->sor.lvdsconf & 0x0100) ? 3 : 1;
458 /* Handle any link training, etc. */
459 if ((outp = ior->asy.outp) && outp->func->acquire)
460 outp->func->acquire(outp);
462 /* Execute OnInt2 IED script. */
463 nv50_disp_super_ied_on(head, ior, 0, khz);
465 /* Program RG clock divider. */
466 head->func->rgclk(head, ior->asy.rgdiv);
468 /* Mode-specific internal DP configuration. */
469 if (ior->type == SOR && ior->asy.proto == DP)
470 nv50_disp_super_2_2_dp(head, ior);
472 /* OR-specific handling. */
473 ior->func->clock(ior);
474 if (ior->func->war_2)
475 ior->func->war_2(ior);
479 nv50_disp_super_2_1(struct nv50_disp *disp, struct nvkm_head *head)
481 struct nvkm_devinit *devinit = disp->base.engine.subdev.device->devinit;
482 const u32 khz = head->asy.hz / 1000;
483 HEAD_DBG(head, "supervisor 2.1 - %d khz", khz);
485 nvkm_devinit_pll_set(devinit, PLL_VPLL0 + head->id, khz);
489 nv50_disp_super_2_0(struct nv50_disp *disp, struct nvkm_head *head)
491 struct nvkm_outp *outp;
492 struct nvkm_ior *ior;
494 /* Determine which OR, if any, we're detaching from the head. */
495 HEAD_DBG(head, "supervisor 2.0");
496 ior = nv50_disp_super_ior_arm(head);
500 /* Execute OffInt2 IED script. */
501 nv50_disp_super_ied_off(head, ior, 2);
503 /* If we're shutting down the OR's only active head, execute
504 * the output path's disable function.
506 if (ior->arm.head == (1 << head->id)) {
507 if ((outp = ior->arm.outp) && outp->func->disable)
508 outp->func->disable(outp, ior);
513 nv50_disp_super_1_0(struct nv50_disp *disp, struct nvkm_head *head)
515 struct nvkm_ior *ior;
517 /* Determine which OR, if any, we're detaching from the head. */
518 HEAD_DBG(head, "supervisor 1.0");
519 ior = nv50_disp_super_ior_arm(head);
523 /* Execute OffInt1 IED script. */
524 nv50_disp_super_ied_off(head, ior, 1);
528 nv50_disp_super_1(struct nv50_disp *disp)
530 struct nvkm_head *head;
531 struct nvkm_ior *ior;
533 list_for_each_entry(head, &disp->base.head, head) {
534 head->func->state(head, &head->arm);
535 head->func->state(head, &head->asy);
538 list_for_each_entry(ior, &disp->base.ior, head) {
539 ior->func->state(ior, &ior->arm);
540 ior->func->state(ior, &ior->asy);
545 nv50_disp_super(struct work_struct *work)
547 struct nv50_disp *disp =
548 container_of(work, struct nv50_disp, supervisor);
549 struct nvkm_subdev *subdev = &disp->base.engine.subdev;
550 struct nvkm_device *device = subdev->device;
551 struct nvkm_head *head;
552 u32 super = nvkm_rd32(device, 0x610030);
554 nvkm_debug(subdev, "supervisor %08x %08x\n", disp->super, super);
556 if (disp->super & 0x00000010) {
557 nv50_disp_chan_mthd(disp->chan[0], NV_DBG_DEBUG);
558 nv50_disp_super_1(disp);
559 list_for_each_entry(head, &disp->base.head, head) {
560 if (!(super & (0x00000020 << head->id)))
562 if (!(super & (0x00000080 << head->id)))
564 nv50_disp_super_1_0(disp, head);
567 if (disp->super & 0x00000020) {
568 list_for_each_entry(head, &disp->base.head, head) {
569 if (!(super & (0x00000080 << head->id)))
571 nv50_disp_super_2_0(disp, head);
573 nvkm_outp_route(&disp->base);
574 list_for_each_entry(head, &disp->base.head, head) {
575 if (!(super & (0x00000200 << head->id)))
577 nv50_disp_super_2_1(disp, head);
579 list_for_each_entry(head, &disp->base.head, head) {
580 if (!(super & (0x00000080 << head->id)))
582 nv50_disp_super_2_2(disp, head);
585 if (disp->super & 0x00000040) {
586 list_for_each_entry(head, &disp->base.head, head) {
587 if (!(super & (0x00000080 << head->id)))
589 nv50_disp_super_3_0(disp, head);
593 nvkm_wr32(device, 0x610030, 0x80000000);
596 static const struct nvkm_enum
597 nv50_disp_intr_error_type[] = {
598 { 3, "ILLEGAL_MTHD" },
599 { 4, "INVALID_VALUE" },
600 { 5, "INVALID_STATE" },
601 { 7, "INVALID_HANDLE" },
605 static const struct nvkm_enum
606 nv50_disp_intr_error_code[] = {
612 nv50_disp_intr_error(struct nv50_disp *disp, int chid)
614 struct nvkm_subdev *subdev = &disp->base.engine.subdev;
615 struct nvkm_device *device = subdev->device;
616 u32 data = nvkm_rd32(device, 0x610084 + (chid * 0x08));
617 u32 addr = nvkm_rd32(device, 0x610080 + (chid * 0x08));
618 u32 code = (addr & 0x00ff0000) >> 16;
619 u32 type = (addr & 0x00007000) >> 12;
620 u32 mthd = (addr & 0x00000ffc);
621 const struct nvkm_enum *ec, *et;
623 et = nvkm_enum_find(nv50_disp_intr_error_type, type);
624 ec = nvkm_enum_find(nv50_disp_intr_error_code, code);
627 "ERROR %d [%s] %02x [%s] chid %d mthd %04x data %08x\n",
628 type, et ? et->name : "", code, ec ? ec->name : "",
631 if (chid < ARRAY_SIZE(disp->chan)) {
634 nv50_disp_chan_mthd(disp->chan[chid], NV_DBG_ERROR);
641 nvkm_wr32(device, 0x610020, 0x00010000 << chid);
642 nvkm_wr32(device, 0x610080 + (chid * 0x08), 0x90000000);
646 nv50_disp_intr(struct nv50_disp *disp)
648 struct nvkm_device *device = disp->base.engine.subdev.device;
649 u32 intr0 = nvkm_rd32(device, 0x610020);
650 u32 intr1 = nvkm_rd32(device, 0x610024);
652 while (intr0 & 0x001f0000) {
653 u32 chid = __ffs(intr0 & 0x001f0000) - 16;
654 nv50_disp_intr_error(disp, chid);
655 intr0 &= ~(0x00010000 << chid);
658 while (intr0 & 0x0000001f) {
659 u32 chid = __ffs(intr0 & 0x0000001f);
660 nv50_disp_chan_uevent_send(disp, chid);
661 intr0 &= ~(0x00000001 << chid);
664 if (intr1 & 0x00000004) {
665 nvkm_disp_vblank(&disp->base, 0);
666 nvkm_wr32(device, 0x610024, 0x00000004);
669 if (intr1 & 0x00000008) {
670 nvkm_disp_vblank(&disp->base, 1);
671 nvkm_wr32(device, 0x610024, 0x00000008);
674 if (intr1 & 0x00000070) {
675 disp->super = (intr1 & 0x00000070);
676 queue_work(disp->wq, &disp->supervisor);
677 nvkm_wr32(device, 0x610024, disp->super);
682 nv50_disp_fini(struct nv50_disp *disp)
684 struct nvkm_device *device = disp->base.engine.subdev.device;
685 /* disable all interrupts */
686 nvkm_wr32(device, 0x610024, 0x00000000);
687 nvkm_wr32(device, 0x610020, 0x00000000);
691 nv50_disp_init(struct nv50_disp *disp)
693 struct nvkm_device *device = disp->base.engine.subdev.device;
694 struct nvkm_head *head;
698 /* The below segments of code copying values from one register to
699 * another appear to inform EVO of the display capabilities or
700 * something similar. NFI what the 0x614004 caps are for..
702 tmp = nvkm_rd32(device, 0x614004);
703 nvkm_wr32(device, 0x610184, tmp);
706 list_for_each_entry(head, &disp->base.head, head) {
707 tmp = nvkm_rd32(device, 0x616100 + (head->id * 0x800));
708 nvkm_wr32(device, 0x610190 + (head->id * 0x10), tmp);
709 tmp = nvkm_rd32(device, 0x616104 + (head->id * 0x800));
710 nvkm_wr32(device, 0x610194 + (head->id * 0x10), tmp);
711 tmp = nvkm_rd32(device, 0x616108 + (head->id * 0x800));
712 nvkm_wr32(device, 0x610198 + (head->id * 0x10), tmp);
713 tmp = nvkm_rd32(device, 0x61610c + (head->id * 0x800));
714 nvkm_wr32(device, 0x61019c + (head->id * 0x10), tmp);
718 for (i = 0; i < disp->dac.nr; i++) {
719 tmp = nvkm_rd32(device, 0x61a000 + (i * 0x800));
720 nvkm_wr32(device, 0x6101d0 + (i * 0x04), tmp);
724 for (i = 0; i < disp->sor.nr; i++) {
725 tmp = nvkm_rd32(device, 0x61c000 + (i * 0x800));
726 nvkm_wr32(device, 0x6101e0 + (i * 0x04), tmp);
730 for (i = 0; i < disp->pior.nr; i++) {
731 tmp = nvkm_rd32(device, 0x61e000 + (i * 0x800));
732 nvkm_wr32(device, 0x6101f0 + (i * 0x04), tmp);
735 /* steal display away from vbios, or something like that */
736 if (nvkm_rd32(device, 0x610024) & 0x00000100) {
737 nvkm_wr32(device, 0x610024, 0x00000100);
738 nvkm_mask(device, 0x6194e8, 0x00000001, 0x00000000);
739 if (nvkm_msec(device, 2000,
740 if (!(nvkm_rd32(device, 0x6194e8) & 0x00000002))
746 /* point at display engine memory area (hash table, objects) */
747 nvkm_wr32(device, 0x610010, (disp->inst->addr >> 8) | 9);
749 /* enable supervisor interrupts, disable everything else */
750 nvkm_wr32(device, 0x61002c, 0x00000370);
751 nvkm_wr32(device, 0x610028, 0x00000000);
755 static const struct nv50_disp_func
757 .init = nv50_disp_init,
758 .fini = nv50_disp_fini,
759 .intr = nv50_disp_intr,
760 .uevent = &nv50_disp_chan_uevent,
761 .super = nv50_disp_super,
762 .root = &nv50_disp_root_oclass,
763 .head = { .cnt = nv50_head_cnt, .new = nv50_head_new },
764 .dac = { .cnt = nv50_dac_cnt, .new = nv50_dac_new },
765 .sor = { .cnt = nv50_sor_cnt, .new = nv50_sor_new },
766 .pior = { .cnt = nv50_pior_cnt, .new = nv50_pior_new },
770 nv50_disp_new(struct nvkm_device *device, int index, struct nvkm_disp **pdisp)
772 return nv50_disp_new_(&nv50_disp, device, index, pdisp);