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