GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / gpu / drm / nouveau / nvkm / engine / disp / rootnv04.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 #define nv04_disp_root(p) container_of((p), struct nv04_disp_root, object)
25 #include "priv.h"
26
27 #include <core/client.h>
28
29 #include <nvif/class.h>
30 #include <nvif/cl0046.h>
31 #include <nvif/unpack.h>
32
33 struct nv04_disp_root {
34         struct nvkm_object object;
35         struct nvkm_disp *disp;
36 };
37
38 static int
39 nv04_disp_scanoutpos(struct nv04_disp_root *root,
40                      void *data, u32 size, int head)
41 {
42         struct nvkm_device *device = root->disp->engine.subdev.device;
43         struct nvkm_object *object = &root->object;
44         const u32 hoff = head * 0x2000;
45         union {
46                 struct nv04_disp_scanoutpos_v0 v0;
47         } *args = data;
48         u32 line;
49         int ret = -ENOSYS;
50
51         nvif_ioctl(object, "disp scanoutpos size %d\n", size);
52         if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
53                 nvif_ioctl(object, "disp scanoutpos vers %d\n",
54                            args->v0.version);
55                 args->v0.vblanks = nvkm_rd32(device, 0x680800 + hoff) & 0xffff;
56                 args->v0.vtotal  = nvkm_rd32(device, 0x680804 + hoff) & 0xffff;
57                 args->v0.vblanke = args->v0.vtotal - 1;
58
59                 args->v0.hblanks = nvkm_rd32(device, 0x680820 + hoff) & 0xffff;
60                 args->v0.htotal  = nvkm_rd32(device, 0x680824 + hoff) & 0xffff;
61                 args->v0.hblanke = args->v0.htotal - 1;
62
63                 /*
64                  * If output is vga instead of digital then vtotal/htotal is
65                  * invalid so we have to give up and trigger the timestamping
66                  * fallback in the drm core.
67                  */
68                 if (!args->v0.vtotal || !args->v0.htotal)
69                         return -ENOTSUPP;
70
71                 args->v0.time[0] = ktime_to_ns(ktime_get());
72                 line = nvkm_rd32(device, 0x600868 + hoff);
73                 args->v0.time[1] = ktime_to_ns(ktime_get());
74                 args->v0.hline = (line & 0xffff0000) >> 16;
75                 args->v0.vline = (line & 0x0000ffff);
76         } else
77                 return ret;
78
79         return 0;
80 }
81
82 static int
83 nv04_disp_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
84 {
85         struct nv04_disp_root *root = nv04_disp_root(object);
86         union {
87                 struct nv04_disp_mthd_v0 v0;
88         } *args = data;
89         int head, ret = -ENOSYS;
90
91         nvif_ioctl(object, "disp mthd size %d\n", size);
92         if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
93                 nvif_ioctl(object, "disp mthd vers %d mthd %02x head %d\n",
94                            args->v0.version, args->v0.method, args->v0.head);
95                 mthd = args->v0.method;
96                 head = args->v0.head;
97         } else
98                 return ret;
99
100         if (head < 0 || head >= 2)
101                 return -ENXIO;
102
103         switch (mthd) {
104         case NV04_DISP_SCANOUTPOS:
105                 return nv04_disp_scanoutpos(root, data, size, head);
106         default:
107                 break;
108         }
109
110         return -EINVAL;
111 }
112
113 static const struct nvkm_object_func
114 nv04_disp_root = {
115         .mthd = nv04_disp_mthd,
116         .ntfy = nvkm_disp_ntfy,
117 };
118
119 static int
120 nv04_disp_root_new(struct nvkm_disp *disp, const struct nvkm_oclass *oclass,
121                    void *data, u32 size, struct nvkm_object **pobject)
122 {
123         struct nv04_disp_root *root;
124
125         if (!(root = kzalloc(sizeof(*root), GFP_KERNEL)))
126                 return -ENOMEM;
127         root->disp = disp;
128         *pobject = &root->object;
129
130         nvkm_object_ctor(&nv04_disp_root, oclass, &root->object);
131         return 0;
132 }
133
134 const struct nvkm_disp_oclass
135 nv04_disp_root_oclass = {
136         .base.oclass = NV04_DISP,
137         .base.minver = -1,
138         .base.maxver = -1,
139         .ctor = nv04_disp_root_new,
140 };