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