xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/engine/sw/nouveau_nvkm_engine_sw_base.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_engine_sw_base.c,v 1.3 2021/12/18 23:45:37 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2015 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 <bskeggs@redhat.com>
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_engine_sw_base.c,v 1.3 2021/12/18 23:45:37 riastradh Exp $");
28 
29 #include "priv.h"
30 #include "chan.h"
31 
32 #include <engine/fifo.h>
33 
34 bool
nvkm_sw_mthd(struct nvkm_sw * sw,int chid,int subc,u32 mthd,u32 data)35 nvkm_sw_mthd(struct nvkm_sw *sw, int chid, int subc, u32 mthd, u32 data)
36 {
37 	struct nvkm_sw_chan *chan;
38 	bool handled = false;
39 	unsigned long flags;
40 
41 	spin_lock_irqsave(&sw->engine.lock, flags);
42 	list_for_each_entry(chan, &sw->chan, head) {
43 		if (chan->fifo->chid == chid) {
44 			handled = nvkm_sw_chan_mthd(chan, subc, mthd, data);
45 			list_del(&chan->head);
46 			list_add(&chan->head, &sw->chan);
47 			break;
48 		}
49 	}
50 	spin_unlock_irqrestore(&sw->engine.lock, flags);
51 	return handled;
52 }
53 
54 static int
nvkm_sw_oclass_new(const struct nvkm_oclass * oclass,void * data,u32 size,struct nvkm_object ** pobject)55 nvkm_sw_oclass_new(const struct nvkm_oclass *oclass, void *data, u32 size,
56 		   struct nvkm_object **pobject)
57 {
58 	struct nvkm_sw_chan *chan = nvkm_sw_chan(oclass->parent);
59 	const struct nvkm_sw_chan_sclass *sclass = oclass->engn;
60 	return sclass->ctor(chan, oclass, data, size, pobject);
61 }
62 
63 static int
nvkm_sw_oclass_get(struct nvkm_oclass * oclass,int index)64 nvkm_sw_oclass_get(struct nvkm_oclass *oclass, int index)
65 {
66 	struct nvkm_sw *sw = nvkm_sw(oclass->engine);
67 	int c = 0;
68 
69 	while (sw->func->sclass[c].ctor) {
70 		if (c++ == index) {
71 			oclass->engn = &sw->func->sclass[index];
72 			oclass->base =  sw->func->sclass[index].base;
73 			oclass->base.ctor = nvkm_sw_oclass_new;
74 			return index;
75 		}
76 	}
77 
78 	return c;
79 }
80 
81 static int
nvkm_sw_cclass_get(struct nvkm_fifo_chan * fifoch,const struct nvkm_oclass * oclass,struct nvkm_object ** pobject)82 nvkm_sw_cclass_get(struct nvkm_fifo_chan *fifoch,
83 		   const struct nvkm_oclass *oclass,
84 		   struct nvkm_object **pobject)
85 {
86 	struct nvkm_sw *sw = nvkm_sw(oclass->engine);
87 	return sw->func->chan_new(sw, fifoch, oclass, pobject);
88 }
89 
90 static void *
nvkm_sw_dtor(struct nvkm_engine * engine)91 nvkm_sw_dtor(struct nvkm_engine *engine)
92 {
93 	return nvkm_sw(engine);
94 }
95 
96 static const struct nvkm_engine_func
97 nvkm_sw = {
98 	.dtor = nvkm_sw_dtor,
99 	.fifo.cclass = nvkm_sw_cclass_get,
100 	.fifo.sclass = nvkm_sw_oclass_get,
101 };
102 
103 int
nvkm_sw_new_(const struct nvkm_sw_func * func,struct nvkm_device * device,int index,struct nvkm_sw ** psw)104 nvkm_sw_new_(const struct nvkm_sw_func *func, struct nvkm_device *device,
105 	     int index, struct nvkm_sw **psw)
106 {
107 	struct nvkm_sw *sw;
108 
109 	if (!(sw = *psw = kzalloc(sizeof(*sw), GFP_KERNEL)))
110 		return -ENOMEM;
111 	INIT_LIST_HEAD(&sw->chan);
112 	sw->func = func;
113 
114 	return nvkm_engine_ctor(&nvkm_sw, device, index, true, &sw->engine);
115 }
116