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/oproxy.h>
30 #include <core/ramht.h>
31 #include <engine/dma.h>
32 
33 #include <nvif/cl507d.h>
34 #include <nvif/event.h>
35 #include <nvif/unpack.h>
36 
37 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)38 nv50_disp_mthd_list(struct nv50_disp *disp, int debug, u32 base, int c,
39 		    const struct nv50_disp_mthd_list *list, int inst)
40 {
41 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
42 	struct nvkm_device *device = subdev->device;
43 	int i;
44 
45 	for (i = 0; list->data[i].mthd; i++) {
46 		if (list->data[i].addr) {
47 			u32 next = nvkm_rd32(device, list->data[i].addr + base + 0);
48 			u32 prev = nvkm_rd32(device, list->data[i].addr + base + c);
49 			u32 mthd = list->data[i].mthd + (list->mthd * inst);
50 			const char *name = list->data[i].name;
51 			char mods[16];
52 
53 			if (prev != next)
54 				snprintf(mods, sizeof(mods), "-> %08x", next);
55 			else
56 				snprintf(mods, sizeof(mods), "%13c", ' ');
57 
58 			nvkm_printk_(subdev, debug, info,
59 				     "\t%04x: %08x %s%s%s\n",
60 				     mthd, prev, mods, name ? " // " : "",
61 				     name ? name : "");
62 		}
63 	}
64 }
65 
66 void
nv50_disp_chan_mthd(struct nv50_disp_chan * chan,int debug)67 nv50_disp_chan_mthd(struct nv50_disp_chan *chan, int debug)
68 {
69 	struct nv50_disp *disp = chan->disp;
70 	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
71 	const struct nv50_disp_chan_mthd *mthd = chan->mthd;
72 	const struct nv50_disp_mthd_list *list;
73 	int i, j;
74 
75 	if (debug > subdev->debug)
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.user);
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 = -ENOSYS;
140 
141 	if (!(ret = nvif_unvers(ret, &data, &size, args->none))) {
142 		notify->size  = sizeof(struct nvif_notify_uevent_rep);
143 		notify->types = 1;
144 		notify->index = chan->chid.user;
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 u64
nv50_disp_chan_user(struct nv50_disp_chan * chan,u64 * psize)159 nv50_disp_chan_user(struct nv50_disp_chan *chan, u64 *psize)
160 {
161 	*psize = 0x1000;
162 	return 0x640000 + (chan->chid.user * 0x1000);
163 }
164 
165 void
nv50_disp_chan_intr(struct nv50_disp_chan * chan,bool en)166 nv50_disp_chan_intr(struct nv50_disp_chan *chan, bool en)
167 {
168 	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
169 	const u32 mask = 0x00010001 << chan->chid.user;
170 	const u32 data = en ? 0x00010000 << chan->chid.user : 0x00000000;
171 	nvkm_mask(device, 0x610028, mask, data);
172 }
173 
174 static int
nv50_disp_chan_rd32(struct nvkm_object * object,u64 addr,u32 * data)175 nv50_disp_chan_rd32(struct nvkm_object *object, u64 addr, u32 *data)
176 {
177 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
178 	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
179 	u64 size, base = chan->func->user(chan, &size);
180 	*data = nvkm_rd32(device, base + addr);
181 	return 0;
182 }
183 
184 static int
nv50_disp_chan_wr32(struct nvkm_object * object,u64 addr,u32 data)185 nv50_disp_chan_wr32(struct nvkm_object *object, u64 addr, u32 data)
186 {
187 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
188 	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
189 	u64 size, base = chan->func->user(chan, &size);
190 	nvkm_wr32(device, base + addr, data);
191 	return 0;
192 }
193 
194 static int
nv50_disp_chan_ntfy(struct nvkm_object * object,u32 type,struct nvkm_event ** pevent)195 nv50_disp_chan_ntfy(struct nvkm_object *object, u32 type,
196 		    struct nvkm_event **pevent)
197 {
198 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
199 	struct nv50_disp *disp = chan->disp;
200 	switch (type) {
201 	case NV50_DISP_CORE_CHANNEL_DMA_V0_NTFY_UEVENT:
202 		*pevent = &disp->uevent;
203 		return 0;
204 	default:
205 		break;
206 	}
207 	return -EINVAL;
208 }
209 
210 static int
nv50_disp_chan_map(struct nvkm_object * object,void * argv,u32 argc,enum nvkm_object_map * type,u64 * addr,u64 * size)211 nv50_disp_chan_map(struct nvkm_object *object, void *argv, u32 argc,
212 		   enum nvkm_object_map *type, u64 *addr, u64 *size)
213 {
214 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
215 	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
216 	const u64 base = device->func->resource_addr(device, 0);
217 	*type = NVKM_OBJECT_MAP_IO;
218 	*addr = base + chan->func->user(chan, size);
219 	return 0;
220 }
221 
222 struct nv50_disp_chan_object {
223 	struct nvkm_oproxy oproxy;
224 	struct nv50_disp *disp;
225 	int hash;
226 };
227 
228 static void
nv50_disp_chan_child_del_(struct nvkm_oproxy * base)229 nv50_disp_chan_child_del_(struct nvkm_oproxy *base)
230 {
231 	struct nv50_disp_chan_object *object =
232 		container_of(base, typeof(*object), oproxy);
233 	nvkm_ramht_remove(object->disp->ramht, object->hash);
234 }
235 
236 static const struct nvkm_oproxy_func
237 nv50_disp_chan_child_func_ = {
238 	.dtor[0] = nv50_disp_chan_child_del_,
239 };
240 
241 static int
nv50_disp_chan_child_new(const struct nvkm_oclass * oclass,void * argv,u32 argc,struct nvkm_object ** pobject)242 nv50_disp_chan_child_new(const struct nvkm_oclass *oclass,
243 			 void *argv, u32 argc, struct nvkm_object **pobject)
244 {
245 	struct nv50_disp_chan *chan = nv50_disp_chan(oclass->parent);
246 	struct nv50_disp *disp = chan->disp;
247 	struct nvkm_device *device = disp->base.engine.subdev.device;
248 	const struct nvkm_device_oclass *sclass = oclass->priv;
249 	struct nv50_disp_chan_object *object;
250 	int ret;
251 
252 	if (!(object = kzalloc(sizeof(*object), GFP_KERNEL)))
253 		return -ENOMEM;
254 	nvkm_oproxy_ctor(&nv50_disp_chan_child_func_, oclass, &object->oproxy);
255 	object->disp = disp;
256 	*pobject = &object->oproxy.base;
257 
258 	ret = sclass->ctor(device, oclass, argv, argc, &object->oproxy.object);
259 	if (ret)
260 		return ret;
261 
262 	object->hash = chan->func->bind(chan, object->oproxy.object,
263 					      oclass->handle);
264 	if (object->hash < 0)
265 		return object->hash;
266 
267 	return 0;
268 }
269 
270 static int
nv50_disp_chan_child_get(struct nvkm_object * object,int index,struct nvkm_oclass * sclass)271 nv50_disp_chan_child_get(struct nvkm_object *object, int index,
272 			 struct nvkm_oclass *sclass)
273 {
274 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
275 	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
276 	const struct nvkm_device_oclass *oclass = NULL;
277 
278 	if (chan->func->bind)
279 		sclass->engine = nvkm_device_engine(device, NVKM_ENGINE_DMAOBJ);
280 	else
281 		sclass->engine = NULL;
282 
283 	if (sclass->engine && sclass->engine->func->base.sclass) {
284 		sclass->engine->func->base.sclass(sclass, index, &oclass);
285 		if (oclass) {
286 			sclass->ctor = nv50_disp_chan_child_new,
287 			sclass->priv = oclass;
288 			return 0;
289 		}
290 	}
291 
292 	return -EINVAL;
293 }
294 
295 static int
nv50_disp_chan_fini(struct nvkm_object * object,bool suspend)296 nv50_disp_chan_fini(struct nvkm_object *object, bool suspend)
297 {
298 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
299 	chan->func->fini(chan);
300 	chan->func->intr(chan, false);
301 	return 0;
302 }
303 
304 static int
nv50_disp_chan_init(struct nvkm_object * object)305 nv50_disp_chan_init(struct nvkm_object *object)
306 {
307 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
308 	chan->func->intr(chan, true);
309 	return chan->func->init(chan);
310 }
311 
312 static void *
nv50_disp_chan_dtor(struct nvkm_object * object)313 nv50_disp_chan_dtor(struct nvkm_object *object)
314 {
315 	struct nv50_disp_chan *chan = nv50_disp_chan(object);
316 	struct nv50_disp *disp = chan->disp;
317 	if (chan->chid.user >= 0)
318 		disp->chan[chan->chid.user] = NULL;
319 	nvkm_memory_unref(&chan->memory);
320 	return chan;
321 }
322 
323 static const struct nvkm_object_func
324 nv50_disp_chan = {
325 	.dtor = nv50_disp_chan_dtor,
326 	.init = nv50_disp_chan_init,
327 	.fini = nv50_disp_chan_fini,
328 	.rd32 = nv50_disp_chan_rd32,
329 	.wr32 = nv50_disp_chan_wr32,
330 	.ntfy = nv50_disp_chan_ntfy,
331 	.map = nv50_disp_chan_map,
332 	.sclass = nv50_disp_chan_child_get,
333 };
334 
335 int
nv50_disp_chan_new_(const struct nv50_disp_chan_func * func,const struct nv50_disp_chan_mthd * mthd,struct nv50_disp * disp,int ctrl,int user,int head,const struct nvkm_oclass * oclass,struct nvkm_object ** pobject)336 nv50_disp_chan_new_(const struct nv50_disp_chan_func *func,
337 		    const struct nv50_disp_chan_mthd *mthd,
338 		    struct nv50_disp *disp, int ctrl, int user, int head,
339 		    const struct nvkm_oclass *oclass,
340 		    struct nvkm_object **pobject)
341 {
342 	struct nv50_disp_chan *chan;
343 
344 	if (!(chan = kzalloc(sizeof(*chan), GFP_KERNEL)))
345 		return -ENOMEM;
346 	*pobject = &chan->object;
347 
348 	nvkm_object_ctor(&nv50_disp_chan, oclass, &chan->object);
349 	chan->func = func;
350 	chan->mthd = mthd;
351 	chan->disp = disp;
352 	chan->chid.ctrl = ctrl;
353 	chan->chid.user = user;
354 	chan->head = head;
355 
356 	if (disp->chan[chan->chid.user]) {
357 		chan->chid.user = -1;
358 		return -EBUSY;
359 	}
360 	disp->chan[chan->chid.user] = chan;
361 	return 0;
362 }
363