drm/nouveau/disp/nv04-nv40: abort scanoutpos query on vga analog.
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / engine / disp / nv04.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
25 #include "priv.h"
26
27 #include <core/event.h>
28 #include <core/class.h>
29
30 struct nv04_disp_priv {
31         struct nouveau_disp base;
32 };
33
34 static int
35 nv04_disp_scanoutpos(struct nouveau_object *object, u32 mthd,
36                      void *data, u32 size)
37 {
38         struct nv04_disp_priv *priv = (void *)object->engine;
39         struct nv04_display_scanoutpos *args = data;
40         const int head = (mthd & NV04_DISP_MTHD_HEAD);
41         u32 line;
42
43         if (size < sizeof(*args))
44                 return -EINVAL;
45
46         args->vblanks = nv_rd32(priv, 0x680800 + (head * 0x2000)) & 0xffff;
47         args->vtotal  = nv_rd32(priv, 0x680804 + (head * 0x2000)) & 0xffff;
48         args->vblanke = args->vtotal - 1;
49
50         args->hblanks = nv_rd32(priv, 0x680820 + (head * 0x2000)) & 0xffff;
51         args->htotal  = nv_rd32(priv, 0x680824 + (head * 0x2000)) & 0xffff;
52         args->hblanke = args->htotal - 1;
53
54         /*
55          * If output is vga instead of digital then vtotal/htotal is invalid
56          * so we have to give up and trigger the timestamping fallback in the
57          * drm core.
58          */
59         if (!args->vtotal || !args->htotal)
60                 return -ENOTSUPP;
61
62         args->time[0] = ktime_to_ns(ktime_get());
63         line = nv_rd32(priv, 0x600868 + (head * 0x2000));
64         args->time[1] = ktime_to_ns(ktime_get());
65         args->hline = (line & 0xffff0000) >> 16;
66         args->vline = (line & 0x0000ffff);
67         return 0;
68 }
69
70 #define HEAD_MTHD(n) (n), (n) + 0x01
71
72 static struct nouveau_omthds
73 nv04_disp_omthds[] = {
74         { HEAD_MTHD(NV04_DISP_SCANOUTPOS), nv04_disp_scanoutpos },
75         {}
76 };
77
78 static struct nouveau_oclass
79 nv04_disp_sclass[] = {
80         { NV04_DISP_CLASS, &nouveau_object_ofuncs, nv04_disp_omthds },
81         {},
82 };
83
84 /*******************************************************************************
85  * Display engine implementation
86  ******************************************************************************/
87
88 static void
89 nv04_disp_vblank_enable(struct nouveau_event *event, int head)
90 {
91         nv_wr32(event->priv, 0x600140 + (head * 0x2000) , 0x00000001);
92 }
93
94 static void
95 nv04_disp_vblank_disable(struct nouveau_event *event, int head)
96 {
97         nv_wr32(event->priv, 0x600140 + (head * 0x2000) , 0x00000000);
98 }
99
100 static void
101 nv04_disp_intr(struct nouveau_subdev *subdev)
102 {
103         struct nv04_disp_priv *priv = (void *)subdev;
104         u32 crtc0 = nv_rd32(priv, 0x600100);
105         u32 crtc1 = nv_rd32(priv, 0x602100);
106         u32 pvideo;
107
108         if (crtc0 & 0x00000001) {
109                 nouveau_event_trigger(priv->base.vblank, 0);
110                 nv_wr32(priv, 0x600100, 0x00000001);
111         }
112
113         if (crtc1 & 0x00000001) {
114                 nouveau_event_trigger(priv->base.vblank, 1);
115                 nv_wr32(priv, 0x602100, 0x00000001);
116         }
117
118         if (nv_device(priv)->chipset >= 0x10 &&
119             nv_device(priv)->chipset <= 0x40) {
120                 pvideo = nv_rd32(priv, 0x8100);
121                 if (pvideo & ~0x11)
122                         nv_info(priv, "PVIDEO intr: %08x\n", pvideo);
123                 nv_wr32(priv, 0x8100, pvideo);
124         }
125 }
126
127 static int
128 nv04_disp_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
129                struct nouveau_oclass *oclass, void *data, u32 size,
130                struct nouveau_object **pobject)
131 {
132         struct nv04_disp_priv *priv;
133         int ret;
134
135         ret = nouveau_disp_create(parent, engine, oclass, 2, "DISPLAY",
136                                   "display", &priv);
137         *pobject = nv_object(priv);
138         if (ret)
139                 return ret;
140
141         nv_engine(priv)->sclass = nv04_disp_sclass;
142         nv_subdev(priv)->intr = nv04_disp_intr;
143         priv->base.vblank->priv = priv;
144         priv->base.vblank->enable = nv04_disp_vblank_enable;
145         priv->base.vblank->disable = nv04_disp_vblank_disable;
146         return 0;
147 }
148
149 struct nouveau_oclass *
150 nv04_disp_oclass = &(struct nouveau_disp_impl) {
151         .base.handle = NV_ENGINE(DISP, 0x04),
152         .base.ofuncs = &(struct nouveau_ofuncs) {
153                 .ctor = nv04_disp_ctor,
154                 .dtor = _nouveau_disp_dtor,
155                 .init = _nouveau_disp_init,
156                 .fini = _nouveau_disp_fini,
157         },
158 }.base;