xref: /linux/drivers/gpu/drm/nouveau/nvkm/engine/disp/chan.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 /*
2  * Copyright 2021 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 #include "chan.h"
23 
24 #include <core/oproxy.h>
25 #include <core/ramht.h>
26 
27 #include <nvif/if0014.h>
28 
29 static int
30 nvkm_disp_chan_rd32(struct nvkm_object *object, u64 addr, u32 *data)
31 {
32 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
33 	struct nvkm_device *device = chan->disp->engine.subdev.device;
34 	u64 size, base = chan->func->user(chan, &size);
35 
36 	*data = nvkm_rd32(device, base + addr);
37 	return 0;
38 }
39 
40 static int
41 nvkm_disp_chan_wr32(struct nvkm_object *object, u64 addr, u32 data)
42 {
43 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
44 	struct nvkm_device *device = chan->disp->engine.subdev.device;
45 	u64 size, base = chan->func->user(chan, &size);
46 
47 	nvkm_wr32(device, base + addr, data);
48 	return 0;
49 }
50 
51 static int
52 nvkm_disp_chan_ntfy(struct nvkm_object *object, u32 type, struct nvkm_event **pevent)
53 {
54 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
55 	struct nvkm_disp *disp = chan->disp;
56 
57 	switch (type) {
58 	case 0:
59 		*pevent = &disp->uevent;
60 		return 0;
61 	default:
62 		break;
63 	}
64 
65 	return -EINVAL;
66 }
67 
68 static int
69 nvkm_disp_chan_map(struct nvkm_object *object, void *argv, u32 argc,
70 		   enum nvkm_object_map *type, u64 *addr, u64 *size)
71 {
72 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
73 	struct nvkm_device *device = chan->disp->engine.subdev.device;
74 	const u64 base = device->func->resource_addr(device, 0);
75 
76 	*type = NVKM_OBJECT_MAP_IO;
77 	*addr = base + chan->func->user(chan, size);
78 	return 0;
79 }
80 
81 struct nvkm_disp_chan_object {
82 	struct nvkm_oproxy oproxy;
83 	struct nvkm_disp *disp;
84 	int hash;
85 };
86 
87 static void
88 nvkm_disp_chan_child_del_(struct nvkm_oproxy *base)
89 {
90 	struct nvkm_disp_chan_object *object = container_of(base, typeof(*object), oproxy);
91 
92 	nvkm_ramht_remove(object->disp->ramht, object->hash);
93 }
94 
95 static const struct nvkm_oproxy_func
96 nvkm_disp_chan_child_func_ = {
97 	.dtor[0] = nvkm_disp_chan_child_del_,
98 };
99 
100 static int
101 nvkm_disp_chan_child_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
102 			 struct nvkm_object **pobject)
103 {
104 	struct nvkm_disp_chan *chan = nvkm_disp_chan(oclass->parent);
105 	struct nvkm_disp *disp = chan->disp;
106 	struct nvkm_device *device = disp->engine.subdev.device;
107 	const struct nvkm_device_oclass *sclass = oclass->priv;
108 	struct nvkm_disp_chan_object *object;
109 	int ret;
110 
111 	if (!(object = kzalloc(sizeof(*object), GFP_KERNEL)))
112 		return -ENOMEM;
113 	nvkm_oproxy_ctor(&nvkm_disp_chan_child_func_, oclass, &object->oproxy);
114 	object->disp = disp;
115 	*pobject = &object->oproxy.base;
116 
117 	ret = sclass->ctor(device, oclass, argv, argc, &object->oproxy.object);
118 	if (ret)
119 		return ret;
120 
121 	object->hash = chan->func->bind(chan, object->oproxy.object, oclass->handle);
122 	if (object->hash < 0)
123 		return object->hash;
124 
125 	return 0;
126 }
127 
128 static int
129 nvkm_disp_chan_child_get(struct nvkm_object *object, int index, struct nvkm_oclass *sclass)
130 {
131 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
132 	struct nvkm_device *device = chan->disp->engine.subdev.device;
133 	const struct nvkm_device_oclass *oclass = NULL;
134 
135 	if (chan->func->bind)
136 		sclass->engine = nvkm_device_engine(device, NVKM_ENGINE_DMAOBJ, 0);
137 	else
138 		sclass->engine = NULL;
139 
140 	if (sclass->engine && sclass->engine->func->base.sclass) {
141 		sclass->engine->func->base.sclass(sclass, index, &oclass);
142 		if (oclass) {
143 			sclass->ctor = nvkm_disp_chan_child_new;
144 			sclass->priv = oclass;
145 			return 0;
146 		}
147 	}
148 
149 	return -EINVAL;
150 }
151 
152 static int
153 nvkm_disp_chan_fini(struct nvkm_object *object, bool suspend)
154 {
155 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
156 
157 	chan->func->fini(chan);
158 	chan->func->intr(chan, false);
159 	return 0;
160 }
161 
162 static int
163 nvkm_disp_chan_init(struct nvkm_object *object)
164 {
165 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
166 
167 	chan->func->intr(chan, true);
168 	return chan->func->init(chan);
169 }
170 
171 static void *
172 nvkm_disp_chan_dtor(struct nvkm_object *object)
173 {
174 	struct nvkm_disp_chan *chan = nvkm_disp_chan(object);
175 	struct nvkm_disp *disp = chan->disp;
176 
177 	spin_lock(&disp->client.lock);
178 	if (disp->chan[chan->chid.user] == chan)
179 		disp->chan[chan->chid.user] = NULL;
180 	spin_unlock(&disp->client.lock);
181 
182 	nvkm_memory_unref(&chan->memory);
183 	return chan;
184 }
185 
186 static const struct nvkm_object_func
187 nvkm_disp_chan = {
188 	.dtor = nvkm_disp_chan_dtor,
189 	.init = nvkm_disp_chan_init,
190 	.fini = nvkm_disp_chan_fini,
191 	.rd32 = nvkm_disp_chan_rd32,
192 	.wr32 = nvkm_disp_chan_wr32,
193 	.ntfy = nvkm_disp_chan_ntfy,
194 	.map = nvkm_disp_chan_map,
195 	.sclass = nvkm_disp_chan_child_get,
196 };
197 
198 static int
199 nvkm_disp_chan_new_(struct nvkm_disp *disp, int nr, const struct nvkm_oclass *oclass,
200 		    void *argv, u32 argc, struct nvkm_object **pobject)
201 {
202 	const struct nvkm_disp_chan_user *user = NULL;
203 	struct nvkm_disp_chan *chan;
204 	union nvif_disp_chan_args *args = argv;
205 	int ret, i;
206 
207 	for (i = 0; disp->func->user[i].ctor; i++) {
208 		if (disp->func->user[i].base.oclass == oclass->base.oclass) {
209 			user = disp->func->user[i].chan;
210 			break;
211 		}
212 	}
213 
214 	if (WARN_ON(!user))
215 		return -EINVAL;
216 
217 	if (argc != sizeof(args->v0) || args->v0.version != 0)
218 		return -ENOSYS;
219 	if (args->v0.id >= nr || !args->v0.pushbuf != !user->func->push)
220 		return -EINVAL;
221 
222 	if (!(chan = kzalloc(sizeof(*chan), GFP_KERNEL)))
223 		return -ENOMEM;
224 	*pobject = &chan->object;
225 
226 	nvkm_object_ctor(&nvkm_disp_chan, oclass, &chan->object);
227 	chan->func = user->func;
228 	chan->mthd = user->mthd;
229 	chan->disp = disp;
230 	chan->chid.ctrl = user->ctrl + args->v0.id;
231 	chan->chid.user = user->user + args->v0.id;
232 	chan->head = args->v0.id;
233 
234 	if (chan->func->push) {
235 		ret = chan->func->push(chan, args->v0.pushbuf);
236 		if (ret)
237 			return ret;
238 	}
239 
240 	spin_lock(&disp->client.lock);
241 	if (disp->chan[chan->chid.user]) {
242 		spin_unlock(&disp->client.lock);
243 		return -EBUSY;
244 	}
245 	disp->chan[chan->chid.user] = chan;
246 	spin_unlock(&disp->client.lock);
247 	return 0;
248 }
249 
250 int
251 nvkm_disp_wndw_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
252 		   struct nvkm_object **pobject)
253 {
254 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
255 
256 	return nvkm_disp_chan_new_(disp, disp->wndw.nr, oclass, argv, argc, pobject);
257 }
258 
259 int
260 nvkm_disp_chan_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
261 		   struct nvkm_object **pobject)
262 {
263 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
264 
265 	return nvkm_disp_chan_new_(disp, disp->head.nr, oclass, argv, argc, pobject);
266 }
267 
268 int
269 nvkm_disp_core_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
270 		   struct nvkm_object **pobject)
271 {
272 	struct nvkm_disp *disp = nvkm_udisp(oclass->parent);
273 
274 	return nvkm_disp_chan_new_(disp, 1, oclass, argv, argc, pobject);
275 }
276