2999996497fffccbecca56b8e9a3bc60ca34923c
[cascardo/linux.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 <nvif/class.h>
28 #include <nvif/event.h>
29 #include <nvif/unpack.h>
30
31 static void
32 nv50_disp_mthd_list(struct nv50_disp *disp, int debug, u32 base, int c,
33                     const struct nv50_disp_mthd_list *list, int inst)
34 {
35         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
36         struct nvkm_device *device = subdev->device;
37         int i;
38
39         for (i = 0; list->data[i].mthd; i++) {
40                 if (list->data[i].addr) {
41                         u32 next = nvkm_rd32(device, list->data[i].addr + base + 0);
42                         u32 prev = nvkm_rd32(device, list->data[i].addr + base + c);
43                         u32 mthd = list->data[i].mthd + (list->mthd * inst);
44                         const char *name = list->data[i].name;
45                         char mods[16];
46
47                         if (prev != next)
48                                 snprintf(mods, sizeof(mods), "-> %08x", next);
49                         else
50                                 snprintf(mods, sizeof(mods), "%13c", ' ');
51
52                         nvkm_printk_(subdev, debug, info,
53                                      "\t%04x: %08x %s%s%s\n",
54                                      mthd, prev, mods, name ? " // " : "",
55                                      name ? name : "");
56                 }
57         }
58 }
59
60 void
61 nv50_disp_mthd_chan(struct nv50_disp *disp, int debug, int head,
62                     const struct nv50_disp_mthd_chan *chan)
63 {
64         struct nvkm_object *object = nv_object(disp);
65         const struct nv50_disp_impl *impl = (void *)object->oclass;
66         const struct nv50_disp_mthd_list *list;
67         struct nvkm_subdev *subdev = &disp->base.engine.subdev;
68         int i, j;
69
70         if (debug > nv_subdev(disp)->debug)
71                 return;
72
73         for (i = 0; (list = chan->data[i].mthd) != NULL; i++) {
74                 u32 base = head * chan->addr;
75                 for (j = 0; j < chan->data[i].nr; j++, base += list->addr) {
76                         const char *cname = chan->name;
77                         const char *sname = "";
78                         char cname_[16], sname_[16];
79
80                         if (chan->addr) {
81                                 snprintf(cname_, sizeof(cname_), "%s %d",
82                                          chan->name, head);
83                                 cname = cname_;
84                         }
85
86                         if (chan->data[i].nr > 1) {
87                                 snprintf(sname_, sizeof(sname_), " - %s %d",
88                                          chan->data[i].name, j);
89                                 sname = sname_;
90                         }
91
92                         nvkm_printk_(subdev, debug, info, "%s%s:\n", cname, sname);
93                         nv50_disp_mthd_list(disp, debug, base, impl->mthd.prev,
94                                             list, j);
95                 }
96         }
97 }
98
99 static void
100 nv50_disp_chan_uevent_fini(struct nvkm_event *event, int type, int index)
101 {
102         struct nv50_disp *disp = container_of(event, typeof(*disp), uevent);
103         struct nvkm_device *device = disp->base.engine.subdev.device;
104         nvkm_mask(device, 0x610028, 0x00000001 << index, 0x00000000 << index);
105         nvkm_wr32(device, 0x610020, 0x00000001 << index);
106 }
107
108 static void
109 nv50_disp_chan_uevent_init(struct nvkm_event *event, int types, int index)
110 {
111         struct nv50_disp *disp = container_of(event, typeof(*disp), uevent);
112         struct nvkm_device *device = disp->base.engine.subdev.device;
113         nvkm_wr32(device, 0x610020, 0x00000001 << index);
114         nvkm_mask(device, 0x610028, 0x00000001 << index, 0x00000001 << index);
115 }
116
117 void
118 nv50_disp_chan_uevent_send(struct nv50_disp *disp, int chid)
119 {
120         struct nvif_notify_uevent_rep {
121         } rep;
122
123         nvkm_event_send(&disp->uevent, 1, chid, &rep, sizeof(rep));
124 }
125
126 int
127 nv50_disp_chan_uevent_ctor(struct nvkm_object *object, void *data, u32 size,
128                            struct nvkm_notify *notify)
129 {
130         struct nv50_disp_dmac *dmac = (void *)object;
131         union {
132                 struct nvif_notify_uevent_req none;
133         } *args = data;
134         int ret;
135
136         if (nvif_unvers(args->none)) {
137                 notify->size  = sizeof(struct nvif_notify_uevent_rep);
138                 notify->types = 1;
139                 notify->index = dmac->base.chid;
140                 return 0;
141         }
142
143         return ret;
144 }
145
146 const struct nvkm_event_func
147 nv50_disp_chan_uevent = {
148         .ctor = nv50_disp_chan_uevent_ctor,
149         .init = nv50_disp_chan_uevent_init,
150         .fini = nv50_disp_chan_uevent_fini,
151 };
152
153 int
154 nv50_disp_chan_ntfy(struct nvkm_object *object, u32 type,
155                     struct nvkm_event **pevent)
156 {
157         struct nv50_disp *disp = (void *)object->engine;
158         switch (type) {
159         case NV50_DISP_CORE_CHANNEL_DMA_V0_NTFY_UEVENT:
160                 *pevent = &disp->uevent;
161                 return 0;
162         default:
163                 break;
164         }
165         return -EINVAL;
166 }
167
168 int
169 nv50_disp_chan_map(struct nvkm_object *object, u64 *addr, u32 *size)
170 {
171         struct nv50_disp_chan *chan = (void *)object;
172         *addr = nv_device_resource_start(nv_device(object), 0) +
173                 0x640000 + (chan->chid * 0x1000);
174         *size = 0x001000;
175         return 0;
176 }
177
178 u32
179 nv50_disp_chan_rd32(struct nvkm_object *object, u64 addr)
180 {
181         struct nv50_disp_chan *chan = (void *)object;
182         struct nvkm_device *device = object->engine->subdev.device;
183         return nvkm_rd32(device, 0x640000 + (chan->chid * 0x1000) + addr);
184 }
185
186 void
187 nv50_disp_chan_wr32(struct nvkm_object *object, u64 addr, u32 data)
188 {
189         struct nv50_disp_chan *chan = (void *)object;
190         struct nvkm_device *device = object->engine->subdev.device;
191         nvkm_wr32(device, 0x640000 + (chan->chid * 0x1000) + addr, data);
192 }
193
194 void
195 nv50_disp_chan_destroy(struct nv50_disp_chan *chan)
196 {
197         struct nv50_disp_root *root = (void *)nv_object(chan)->parent;
198         root->chan &= ~(1 << chan->chid);
199         nvkm_namedb_destroy(&chan->base);
200 }
201
202 int
203 nv50_disp_chan_create_(struct nvkm_object *parent,
204                        struct nvkm_object *engine,
205                        struct nvkm_oclass *oclass, int head,
206                        int length, void **pobject)
207 {
208         const struct nv50_disp_chan_impl *impl = (void *)oclass->ofuncs;
209         struct nv50_disp_root *root = (void *)parent;
210         struct nv50_disp_chan *chan;
211         int chid = impl->chid + head;
212         int ret;
213
214         if (root->chan & (1 << chid))
215                 return -EBUSY;
216         root->chan |= (1 << chid);
217
218         ret = nvkm_namedb_create_(parent, engine, oclass, 0, NULL,
219                                   (1ULL << NVDEV_ENGINE_DMAOBJ),
220                                   length, pobject);
221         chan = *pobject;
222         if (ret)
223                 return ret;
224         chan->chid = chid;
225
226         nv_parent(chan)->object_attach = impl->attach;
227         nv_parent(chan)->object_detach = impl->detach;
228         return 0;
229 }