GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / gpu / drm / nouveau / nvkm / engine / disp / channv50.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 "channv50.h"
25 #include "rootnv50.h"
26
27 #include <core/client.h>
28 #include <core/notify.h>
29 #include <core/ramht.h>
30 #include <engine/dma.h>
31
32 #include <nvif/cl507d.h>
33 #include <nvif/event.h>
34 #include <nvif/unpack.h>
35
36 static void
37 nv50_disp_mthd_list(struct nv50_disp *disp, int debug, u32 base, int c,
38                     const struct nv50_disp_mthd_list *list, int inst)
39 {
40         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
41         struct nvkm_device *device = subdev->device;
42         int i;
43
44         for (i = 0; list->data[i].mthd; i++) {
45                 if (list->data[i].addr) {
46                         u32 next = nvkm_rd32(device, list->data[i].addr + base + 0);
47                         u32 prev = nvkm_rd32(device, list->data[i].addr + base + c);
48                         u32 mthd = list->data[i].mthd + (list->mthd * inst);
49                         const char *name = list->data[i].name;
50                         char mods[16];
51
52                         if (prev != next)
53                                 snprintf(mods, sizeof(mods), "-> %08x", next);
54                         else
55                                 snprintf(mods, sizeof(mods), "%13c", ' ');
56
57                         nvkm_printk_(subdev, debug, info,
58                                      "\t%04x: %08x %s%s%s\n",
59                                      mthd, prev, mods, name ? " // " : "",
60                                      name ? name : "");
61                 }
62         }
63 }
64
65 void
66 nv50_disp_chan_mthd(struct nv50_disp_chan *chan, int debug)
67 {
68         struct nv50_disp *disp = chan->root->disp;
69         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
70         const struct nv50_disp_chan_mthd *mthd = chan->mthd;
71         const struct nv50_disp_mthd_list *list;
72         int i, j;
73
74         if (debug > subdev->debug)
75                 return;
76         if (!mthd)
77                 return;
78
79         for (i = 0; (list = mthd->data[i].mthd) != NULL; i++) {
80                 u32 base = chan->head * mthd->addr;
81                 for (j = 0; j < mthd->data[i].nr; j++, base += list->addr) {
82                         const char *cname = mthd->name;
83                         const char *sname = "";
84                         char cname_[16], sname_[16];
85
86                         if (mthd->addr) {
87                                 snprintf(cname_, sizeof(cname_), "%s %d",
88                                          mthd->name, chan->chid.user);
89                                 cname = cname_;
90                         }
91
92                         if (mthd->data[i].nr > 1) {
93                                 snprintf(sname_, sizeof(sname_), " - %s %d",
94                                          mthd->data[i].name, j);
95                                 sname = sname_;
96                         }
97
98                         nvkm_printk_(subdev, debug, info, "%s%s:\n", cname, sname);
99                         nv50_disp_mthd_list(disp, debug, base, mthd->prev,
100                                             list, j);
101                 }
102         }
103 }
104
105 static void
106 nv50_disp_chan_uevent_fini(struct nvkm_event *event, int type, int index)
107 {
108         struct nv50_disp *disp = container_of(event, typeof(*disp), uevent);
109         struct nvkm_device *device = disp->base.engine.subdev.device;
110         nvkm_mask(device, 0x610028, 0x00000001 << index, 0x00000000 << index);
111         nvkm_wr32(device, 0x610020, 0x00000001 << index);
112 }
113
114 static void
115 nv50_disp_chan_uevent_init(struct nvkm_event *event, int types, int index)
116 {
117         struct nv50_disp *disp = container_of(event, typeof(*disp), uevent);
118         struct nvkm_device *device = disp->base.engine.subdev.device;
119         nvkm_wr32(device, 0x610020, 0x00000001 << index);
120         nvkm_mask(device, 0x610028, 0x00000001 << index, 0x00000001 << index);
121 }
122
123 void
124 nv50_disp_chan_uevent_send(struct nv50_disp *disp, int chid)
125 {
126         struct nvif_notify_uevent_rep {
127         } rep;
128
129         nvkm_event_send(&disp->uevent, 1, chid, &rep, sizeof(rep));
130 }
131
132 int
133 nv50_disp_chan_uevent_ctor(struct nvkm_object *object, void *data, u32 size,
134                            struct nvkm_notify *notify)
135 {
136         struct nv50_disp_chan *chan = nv50_disp_chan(object);
137         union {
138                 struct nvif_notify_uevent_req none;
139         } *args = data;
140         int ret = -ENOSYS;
141
142         if (!(ret = nvif_unvers(ret, &data, &size, args->none))) {
143                 notify->size  = sizeof(struct nvif_notify_uevent_rep);
144                 notify->types = 1;
145                 notify->index = chan->chid.user;
146                 return 0;
147         }
148
149         return ret;
150 }
151
152 const struct nvkm_event_func
153 nv50_disp_chan_uevent = {
154         .ctor = nv50_disp_chan_uevent_ctor,
155         .init = nv50_disp_chan_uevent_init,
156         .fini = nv50_disp_chan_uevent_fini,
157 };
158
159 static int
160 nv50_disp_chan_rd32(struct nvkm_object *object, u64 addr, u32 *data)
161 {
162         struct nv50_disp_chan *chan = nv50_disp_chan(object);
163         struct nv50_disp *disp = chan->root->disp;
164         struct nvkm_device *device = disp->base.engine.subdev.device;
165         *data = nvkm_rd32(device, 0x640000 + (chan->chid.user * 0x1000) + addr);
166         return 0;
167 }
168
169 static int
170 nv50_disp_chan_wr32(struct nvkm_object *object, u64 addr, u32 data)
171 {
172         struct nv50_disp_chan *chan = nv50_disp_chan(object);
173         struct nv50_disp *disp = chan->root->disp;
174         struct nvkm_device *device = disp->base.engine.subdev.device;
175         nvkm_wr32(device, 0x640000 + (chan->chid.user * 0x1000) + addr, data);
176         return 0;
177 }
178
179 static int
180 nv50_disp_chan_ntfy(struct nvkm_object *object, u32 type,
181                     struct nvkm_event **pevent)
182 {
183         struct nv50_disp_chan *chan = nv50_disp_chan(object);
184         struct nv50_disp *disp = chan->root->disp;
185         switch (type) {
186         case NV50_DISP_CORE_CHANNEL_DMA_V0_NTFY_UEVENT:
187                 *pevent = &disp->uevent;
188                 return 0;
189         default:
190                 break;
191         }
192         return -EINVAL;
193 }
194
195 static int
196 nv50_disp_chan_map(struct nvkm_object *object, u64 *addr, u32 *size)
197 {
198         struct nv50_disp_chan *chan = nv50_disp_chan(object);
199         struct nv50_disp *disp = chan->root->disp;
200         struct nvkm_device *device = disp->base.engine.subdev.device;
201         *addr = device->func->resource_addr(device, 0) +
202                 0x640000 + (chan->chid.user * 0x1000);
203         *size = 0x001000;
204         return 0;
205 }
206
207 static int
208 nv50_disp_chan_child_new(const struct nvkm_oclass *oclass,
209                          void *data, u32 size, struct nvkm_object **pobject)
210 {
211         struct nv50_disp_chan *chan = nv50_disp_chan(oclass->parent);
212         return chan->func->child_new(chan, oclass, data, size, pobject);
213 }
214
215 static int
216 nv50_disp_chan_child_get(struct nvkm_object *object, int index,
217                          struct nvkm_oclass *oclass)
218 {
219         struct nv50_disp_chan *chan = nv50_disp_chan(object);
220         if (chan->func->child_get) {
221                 int ret = chan->func->child_get(chan, index, oclass);
222                 if (ret == 0)
223                         oclass->ctor = nv50_disp_chan_child_new;
224                 return ret;
225         }
226         return -EINVAL;
227 }
228
229 static int
230 nv50_disp_chan_fini(struct nvkm_object *object, bool suspend)
231 {
232         struct nv50_disp_chan *chan = nv50_disp_chan(object);
233         chan->func->fini(chan);
234         return 0;
235 }
236
237 static int
238 nv50_disp_chan_init(struct nvkm_object *object)
239 {
240         struct nv50_disp_chan *chan = nv50_disp_chan(object);
241         return chan->func->init(chan);
242 }
243
244 static void *
245 nv50_disp_chan_dtor(struct nvkm_object *object)
246 {
247         struct nv50_disp_chan *chan = nv50_disp_chan(object);
248         struct nv50_disp *disp = chan->root->disp;
249         if (chan->chid.user >= 0)
250                 disp->chan[chan->chid.user] = NULL;
251         return chan->func->dtor ? chan->func->dtor(chan) : chan;
252 }
253
254 static const struct nvkm_object_func
255 nv50_disp_chan = {
256         .dtor = nv50_disp_chan_dtor,
257         .init = nv50_disp_chan_init,
258         .fini = nv50_disp_chan_fini,
259         .rd32 = nv50_disp_chan_rd32,
260         .wr32 = nv50_disp_chan_wr32,
261         .ntfy = nv50_disp_chan_ntfy,
262         .map = nv50_disp_chan_map,
263         .sclass = nv50_disp_chan_child_get,
264 };
265
266 int
267 nv50_disp_chan_ctor(const struct nv50_disp_chan_func *func,
268                     const struct nv50_disp_chan_mthd *mthd,
269                     struct nv50_disp_root *root, int ctrl, int user, int head,
270                     const struct nvkm_oclass *oclass,
271                     struct nv50_disp_chan *chan)
272 {
273         struct nv50_disp *disp = root->disp;
274
275         nvkm_object_ctor(&nv50_disp_chan, oclass, &chan->object);
276         chan->func = func;
277         chan->mthd = mthd;
278         chan->root = root;
279         chan->chid.ctrl = ctrl;
280         chan->chid.user = user;
281         chan->head = head;
282
283         if (disp->chan[chan->chid.user]) {
284                 chan->chid.user = -1;
285                 return -EBUSY;
286         }
287         disp->chan[chan->chid.user] = chan;
288         return 0;
289 }
290
291 int
292 nv50_disp_chan_new_(const struct nv50_disp_chan_func *func,
293                     const struct nv50_disp_chan_mthd *mthd,
294                     struct nv50_disp_root *root, int ctrl, int user, int head,
295                     const struct nvkm_oclass *oclass,
296                     struct nvkm_object **pobject)
297 {
298         struct nv50_disp_chan *chan;
299
300         if (!(chan = kzalloc(sizeof(*chan), GFP_KERNEL)))
301                 return -ENOMEM;
302         *pobject = &chan->object;
303
304         return nv50_disp_chan_ctor(func, mthd, root, ctrl, user,
305                                    head, oclass, chan);
306 }