1/*	$NetBSD: nouveau_nvkm_engine_disp_channv50.c,v 1.6 2021/12/18 23:45:35 riastradh Exp $	*/
2
3/*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_engine_disp_channv50.c,v 1.6 2021/12/18 23:45:35 riastradh Exp $");
28
29#include "channv50.h"
30#include "rootnv50.h"
31
32#include <core/client.h>
33#include <core/notify.h>
34#include <core/oproxy.h>
35#include <core/ramht.h>
36#include <engine/dma.h>
37
38#include <nvif/cl507d.h>
39#include <nvif/event.h>
40#include <nvif/unpack.h>
41
42static void
43nv50_disp_mthd_list(struct nv50_disp *disp, int debug, u32 base, int c,
44		    const struct nv50_disp_mthd_list *list, int inst)
45{
46	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
47	struct nvkm_device *device = subdev->device;
48	int i;
49
50	for (i = 0; list->data[i].mthd; i++) {
51		if (list->data[i].addr) {
52			u32 next = nvkm_rd32(device, list->data[i].addr + base + 0);
53			u32 prev = nvkm_rd32(device, list->data[i].addr + base + c);
54			u32 mthd = list->data[i].mthd + (list->mthd * inst);
55			const char *name = list->data[i].name;
56			char mods[16];
57
58			if (prev != next)
59				snprintf(mods, sizeof(mods), "-> %08x", next);
60			else
61				snprintf(mods, sizeof(mods), "%13c", ' ');
62
63			nvkm_printk_(subdev, debug, info,
64				     "\t%04x: %08x %s%s%s\n",
65				     mthd, prev, mods, name ? " // " : "",
66				     name ? name : "");
67		}
68	}
69}
70
71void
72nv50_disp_chan_mthd(struct nv50_disp_chan *chan, int debug)
73{
74	struct nv50_disp *disp = chan->disp;
75	struct nvkm_subdev *subdev = &disp->base.engine.subdev;
76	const struct nv50_disp_chan_mthd *mthd = chan->mthd;
77	const struct nv50_disp_mthd_list *list;
78	int i, j;
79
80	if (debug > subdev->debug)
81		return;
82	if (!mthd)
83		return;
84
85	for (i = 0; (list = mthd->data[i].mthd) != NULL; i++) {
86		u32 base = chan->head * mthd->addr;
87		for (j = 0; j < mthd->data[i].nr; j++, base += list->addr) {
88			const char *cname = mthd->name;
89			const char *sname = "";
90			char cname_[16], sname_[16];
91
92			if (mthd->addr) {
93				snprintf(cname_, sizeof(cname_), "%s %d",
94					 mthd->name, chan->chid.user);
95				cname = cname_;
96			}
97
98			if (mthd->data[i].nr > 1) {
99				snprintf(sname_, sizeof(sname_), " - %s %d",
100					 mthd->data[i].name, j);
101				sname = sname_;
102			}
103
104			nvkm_printk_(subdev, debug, info, "%s%s:\n", cname, sname);
105			nv50_disp_mthd_list(disp, debug, base, mthd->prev,
106					    list, j);
107		}
108	}
109}
110
111static void
112nv50_disp_chan_uevent_fini(struct nvkm_event *event, int type, 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_mask(device, 0x610028, 0x00000001 << index, 0x00000000 << index);
117	nvkm_wr32(device, 0x610020, 0x00000001 << index);
118}
119
120static void
121nv50_disp_chan_uevent_init(struct nvkm_event *event, int types, int index)
122{
123	struct nv50_disp *disp = container_of(event, typeof(*disp), uevent);
124	struct nvkm_device *device = disp->base.engine.subdev.device;
125	nvkm_wr32(device, 0x610020, 0x00000001 << index);
126	nvkm_mask(device, 0x610028, 0x00000001 << index, 0x00000001 << index);
127}
128
129void
130nv50_disp_chan_uevent_send(struct nv50_disp *disp, int chid)
131{
132	struct nvif_notify_uevent_rep {
133	} rep;
134
135	nvkm_event_send(&disp->uevent, 1, chid, &rep, sizeof(rep));
136}
137
138int
139nv50_disp_chan_uevent_ctor(struct nvkm_object *object, void *data, u32 size,
140			   struct nvkm_notify *notify)
141{
142	struct nv50_disp_chan *chan = nv50_disp_chan(object);
143	union {
144		struct nvif_notify_uevent_req none;
145	} *args = data;
146	int ret = -ENOSYS;
147
148	if (!(ret = nvif_unvers(ret, &data, &size, args->none))) {
149		notify->size  = sizeof(struct nvif_notify_uevent_rep);
150		notify->types = 1;
151		notify->index = chan->chid.user;
152		return 0;
153	}
154
155	return ret;
156}
157
158const struct nvkm_event_func
159nv50_disp_chan_uevent = {
160	.ctor = nv50_disp_chan_uevent_ctor,
161	.init = nv50_disp_chan_uevent_init,
162	.fini = nv50_disp_chan_uevent_fini,
163};
164
165u64
166nv50_disp_chan_user(struct nv50_disp_chan *chan, u64 *psize)
167{
168	*psize = 0x1000;
169	return 0x640000 + (chan->chid.user * 0x1000);
170}
171
172void
173nv50_disp_chan_intr(struct nv50_disp_chan *chan, bool en)
174{
175	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
176	const u32 mask = 0x00010001 << chan->chid.user;
177	const u32 data = en ? 0x00010000 << chan->chid.user : 0x00000000;
178	nvkm_mask(device, 0x610028, mask, data);
179}
180
181static int
182nv50_disp_chan_rd32(struct nvkm_object *object, u64 addr, u32 *data)
183{
184	struct nv50_disp_chan *chan = nv50_disp_chan(object);
185	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
186	u64 size, base = chan->func->user(chan, &size);
187	*data = nvkm_rd32(device, base + addr);
188	return 0;
189}
190
191static int
192nv50_disp_chan_wr32(struct nvkm_object *object, u64 addr, u32 data)
193{
194	struct nv50_disp_chan *chan = nv50_disp_chan(object);
195	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
196	u64 size, base = chan->func->user(chan, &size);
197	nvkm_wr32(device, base + addr, data);
198	return 0;
199}
200
201static int
202nv50_disp_chan_ntfy(struct nvkm_object *object, u32 type,
203		    struct nvkm_event **pevent)
204{
205	struct nv50_disp_chan *chan = nv50_disp_chan(object);
206	struct nv50_disp *disp = chan->disp;
207	switch (type) {
208	case NV50_DISP_CORE_CHANNEL_DMA_V0_NTFY_UEVENT:
209		*pevent = &disp->uevent;
210		return 0;
211	default:
212		break;
213	}
214	return -EINVAL;
215}
216
217static int
218#ifdef __NetBSD__
219nv50_disp_chan_map(struct nvkm_object *object, void *argv, u32 argc,
220		   enum nvkm_object_map *type, bus_space_tag_t *tagp,
221		   u64 *addr, u64 *size)
222#else
223nv50_disp_chan_map(struct nvkm_object *object, void *argv, u32 argc,
224		   enum nvkm_object_map *type, u64 *addr, u64 *size)
225#endif
226{
227	struct nv50_disp_chan *chan = nv50_disp_chan(object);
228	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
229	const u64 base = device->func->resource_addr(device, 0);
230#ifdef __NetBSD__
231	*tagp = device->func->resource_tag(device, 0);
232#endif
233	*type = NVKM_OBJECT_MAP_IO;
234	*addr = base + chan->func->user(chan, size);
235	return 0;
236}
237
238struct nv50_disp_chan_object {
239	struct nvkm_oproxy oproxy;
240	struct nv50_disp *disp;
241	int hash;
242};
243
244static void
245nv50_disp_chan_child_del_(struct nvkm_oproxy *base)
246{
247	struct nv50_disp_chan_object *object =
248		container_of(base, typeof(*object), oproxy);
249	nvkm_ramht_remove(object->disp->ramht, object->hash);
250}
251
252static const struct nvkm_oproxy_func
253nv50_disp_chan_child_func_ = {
254	.dtor[0] = nv50_disp_chan_child_del_,
255};
256
257static int
258nv50_disp_chan_child_new(const struct nvkm_oclass *oclass,
259			 void *argv, u32 argc, struct nvkm_object **pobject)
260{
261	struct nv50_disp_chan *chan = nv50_disp_chan(oclass->parent);
262	struct nv50_disp *disp = chan->disp;
263	struct nvkm_device *device = disp->base.engine.subdev.device;
264	const struct nvkm_device_oclass *sclass = oclass->priv;
265	struct nv50_disp_chan_object *object;
266	int ret;
267
268	if (!(object = kzalloc(sizeof(*object), GFP_KERNEL)))
269		return -ENOMEM;
270	nvkm_oproxy_ctor(&nv50_disp_chan_child_func_, oclass, &object->oproxy);
271	object->disp = disp;
272	*pobject = &object->oproxy.base;
273
274	ret = sclass->ctor(device, oclass, argv, argc, &object->oproxy.object);
275	if (ret)
276		return ret;
277
278	object->hash = chan->func->bind(chan, object->oproxy.object,
279					      oclass->handle);
280	if (object->hash < 0)
281		return object->hash;
282
283	return 0;
284}
285
286static int
287nv50_disp_chan_child_get(struct nvkm_object *object, int index,
288			 struct nvkm_oclass *sclass)
289{
290	struct nv50_disp_chan *chan = nv50_disp_chan(object);
291	struct nvkm_device *device = chan->disp->base.engine.subdev.device;
292	const struct nvkm_device_oclass *oclass = NULL;
293
294	if (chan->func->bind)
295		sclass->engine = nvkm_device_engine(device, NVKM_ENGINE_DMAOBJ);
296	else
297		sclass->engine = NULL;
298
299	if (sclass->engine && sclass->engine->func->base.sclass) {
300		sclass->engine->func->base.sclass(sclass, index, &oclass);
301		if (oclass) {
302			sclass->ctor = nv50_disp_chan_child_new,
303			sclass->priv = oclass;
304			return 0;
305		}
306	}
307
308	return -EINVAL;
309}
310
311static int
312nv50_disp_chan_fini(struct nvkm_object *object, bool suspend)
313{
314	struct nv50_disp_chan *chan = nv50_disp_chan(object);
315	chan->func->fini(chan);
316	chan->func->intr(chan, false);
317	return 0;
318}
319
320static int
321nv50_disp_chan_init(struct nvkm_object *object)
322{
323	struct nv50_disp_chan *chan = nv50_disp_chan(object);
324	chan->func->intr(chan, true);
325	return chan->func->init(chan);
326}
327
328static void *
329nv50_disp_chan_dtor(struct nvkm_object *object)
330{
331	struct nv50_disp_chan *chan = nv50_disp_chan(object);
332	struct nv50_disp *disp = chan->disp;
333	if (chan->chid.user >= 0)
334		disp->chan[chan->chid.user] = NULL;
335	nvkm_memory_unref(&chan->memory);
336	return chan;
337}
338
339static const struct nvkm_object_func
340nv50_disp_chan = {
341	.dtor = nv50_disp_chan_dtor,
342	.init = nv50_disp_chan_init,
343	.fini = nv50_disp_chan_fini,
344	.rd32 = nv50_disp_chan_rd32,
345	.wr32 = nv50_disp_chan_wr32,
346	.ntfy = nv50_disp_chan_ntfy,
347	.map = nv50_disp_chan_map,
348	.sclass = nv50_disp_chan_child_get,
349};
350
351int
352nv50_disp_chan_new_(const struct nv50_disp_chan_func *func,
353		    const struct nv50_disp_chan_mthd *mthd,
354		    struct nv50_disp *disp, int ctrl, int user, int head,
355		    const struct nvkm_oclass *oclass,
356		    struct nvkm_object **pobject)
357{
358	struct nv50_disp_chan *chan;
359
360	if (!(chan = kzalloc(sizeof(*chan), GFP_KERNEL)))
361		return -ENOMEM;
362	*pobject = &chan->object;
363
364	nvkm_object_ctor(&nv50_disp_chan, oclass, &chan->object);
365	chan->func = func;
366	chan->mthd = mthd;
367	chan->disp = disp;
368	chan->chid.ctrl = ctrl;
369	chan->chid.user = user;
370	chan->head = head;
371
372	if (disp->chan[chan->chid.user]) {
373		chan->chid.user = -1;
374		return -EBUSY;
375	}
376	disp->chan[chan->chid.user] = chan;
377	return 0;
378}
379