1 /* $NetBSD: nouveau_usif.c,v 1.8 2021/12/18 23:45:32 riastradh Exp $ */
2
3 /*
4 * Copyright 2014 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
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_usif.c,v 1.8 2021/12/18 23:45:32 riastradh Exp $");
29
30 #include "nouveau_drv.h"
31 #include "nouveau_usif.h"
32 #include "nouveau_abi16.h"
33
34 #include <nvif/notify.h>
35 #include <nvif/unpack.h>
36 #include <nvif/client.h>
37 #include <nvif/event.h>
38 #include <nvif/ioctl.h>
39
40 struct usif_notify_p {
41 struct drm_pending_event base;
42 struct {
43 struct drm_event base;
44 u8 data[];
45 } e;
46 };
47
48 struct usif_notify {
49 struct list_head head;
50 atomic_t enabled;
51 u32 handle;
52 u16 reply;
53 u8 route;
54 u64 token;
55 struct usif_notify_p *p;
56 };
57
58 static inline struct usif_notify *
usif_notify_find(struct drm_file * filp,u32 handle)59 usif_notify_find(struct drm_file *filp, u32 handle)
60 {
61 struct nouveau_cli *cli = nouveau_cli(filp);
62 struct usif_notify *ntfy;
63 list_for_each_entry(ntfy, &cli->notifys, head) {
64 if (ntfy->handle == handle)
65 return ntfy;
66 }
67 return NULL;
68 }
69
70 static inline void
usif_notify_dtor(struct usif_notify * ntfy)71 usif_notify_dtor(struct usif_notify *ntfy)
72 {
73 list_del(&ntfy->head);
74 kfree(ntfy);
75 }
76
77 int
usif_notify(const void * header,u32 length,const void * data,u32 size)78 usif_notify(const void *header, u32 length, const void *data, u32 size)
79 {
80 struct usif_notify *ntfy = NULL;
81 const union {
82 struct nvif_notify_rep_v0 v0;
83 } *rep = header;
84 struct drm_device *dev;
85 struct drm_file *filp;
86 unsigned long flags;
87
88 if (length == sizeof(rep->v0) && rep->v0.version == 0) {
89 if (WARN_ON(!(ntfy = (void *)(unsigned long)rep->v0.token)))
90 return NVIF_NOTIFY_DROP;
91 BUG_ON(rep->v0.route != NVDRM_NOTIFY_USIF);
92 } else
93 if (WARN_ON(1))
94 return NVIF_NOTIFY_DROP;
95
96 if (WARN_ON(!ntfy->p || ntfy->reply != (length + size)))
97 return NVIF_NOTIFY_DROP;
98 filp = ntfy->p->base.file_priv;
99 dev = filp->minor->dev;
100
101 memcpy(&ntfy->p->e.data[0], header, length);
102 memcpy(&ntfy->p->e.data[length], data, size);
103 switch (rep->v0.version) {
104 case 0: {
105 struct nvif_notify_rep_v0 *rep = (void *)ntfy->p->e.data;
106 rep->route = ntfy->route;
107 rep->token = ntfy->token;
108 }
109 break;
110 default:
111 BUG();
112 break;
113 }
114
115 spin_lock_irqsave(&dev->event_lock, flags);
116 if (!WARN_ON(filp->event_space < ntfy->p->e.base.length)) {
117 list_add_tail(&ntfy->p->base.link, &filp->event_list);
118 filp->event_space -= ntfy->p->e.base.length;
119 }
120 #ifdef __NetBSD__
121 DRM_SPIN_WAKEUP_ONE(&filp->event_wait, &dev->event_lock);
122 #else
123 wake_up_interruptible(&filp->event_wait);
124 #endif
125 spin_unlock_irqrestore(&dev->event_lock, flags);
126 atomic_set(&ntfy->enabled, 0);
127 return NVIF_NOTIFY_DROP;
128 }
129
130 static int
usif_notify_new(struct drm_file * f,void * data,u32 size,void * argv,u32 argc)131 usif_notify_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
132 {
133 struct nouveau_cli *cli = nouveau_cli(f);
134 struct nvif_client *client = &cli->base;
135 union {
136 struct nvif_ioctl_ntfy_new_v0 v0;
137 } *args = data;
138 union {
139 struct nvif_notify_req_v0 v0;
140 } *req;
141 struct usif_notify *ntfy;
142 int ret = -ENOSYS;
143
144 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
145 if (usif_notify_find(f, args->v0.index))
146 return -EEXIST;
147 } else
148 return ret;
149 req = data;
150 ret = -ENOSYS;
151
152 if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL)))
153 return -ENOMEM;
154 atomic_set(&ntfy->enabled, 0);
155
156 if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) {
157 ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply;
158 ntfy->route = req->v0.route;
159 ntfy->token = req->v0.token;
160 req->v0.route = NVDRM_NOTIFY_USIF;
161 req->v0.token = (unsigned long)(void *)ntfy;
162 ret = nvif_client_ioctl(client, argv, argc);
163 req->v0.token = ntfy->token;
164 req->v0.route = ntfy->route;
165 ntfy->handle = args->v0.index;
166 }
167
168 if (ret == 0)
169 list_add(&ntfy->head, &cli->notifys);
170 if (ret)
171 kfree(ntfy);
172 return ret;
173 }
174
175 static int
usif_notify_del(struct drm_file * f,void * data,u32 size,void * argv,u32 argc)176 usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
177 {
178 struct nouveau_cli *cli = nouveau_cli(f);
179 struct nvif_client *client = &cli->base;
180 union {
181 struct nvif_ioctl_ntfy_del_v0 v0;
182 } *args = data;
183 struct usif_notify *ntfy;
184 int ret = -ENOSYS;
185
186 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
187 if (!(ntfy = usif_notify_find(f, args->v0.index)))
188 return -ENOENT;
189 } else
190 return ret;
191
192 ret = nvif_client_ioctl(client, argv, argc);
193 if (ret == 0)
194 usif_notify_dtor(ntfy);
195 return ret;
196 }
197
198 static int
usif_notify_get(struct drm_file * f,void * data,u32 size,void * argv,u32 argc)199 usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
200 {
201 struct nouveau_cli *cli = nouveau_cli(f);
202 struct nvif_client *client = &cli->base;
203 union {
204 struct nvif_ioctl_ntfy_del_v0 v0;
205 } *args = data;
206 struct usif_notify *ntfy;
207 int ret = -ENOSYS;
208
209 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
210 if (!(ntfy = usif_notify_find(f, args->v0.index)))
211 return -ENOENT;
212 } else
213 return ret;
214
215 if (atomic_xchg(&ntfy->enabled, 1))
216 return 0;
217
218 ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL);
219 if (ret = -ENOMEM, !ntfy->p)
220 goto done;
221 ntfy->p->base.event = &ntfy->p->e.base;
222 ntfy->p->base.file_priv = f;
223 ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF;
224 ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply;
225
226 ret = nvif_client_ioctl(client, argv, argc);
227 done:
228 if (ret) {
229 atomic_set(&ntfy->enabled, 0);
230 kfree(ntfy->p);
231 }
232 return ret;
233 }
234
235 static int
usif_notify_put(struct drm_file * f,void * data,u32 size,void * argv,u32 argc)236 usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
237 {
238 struct nouveau_cli *cli = nouveau_cli(f);
239 struct nvif_client *client = &cli->base;
240 union {
241 struct nvif_ioctl_ntfy_put_v0 v0;
242 } *args = data;
243 struct usif_notify *ntfy;
244 int ret = -ENOSYS;
245
246 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
247 if (!(ntfy = usif_notify_find(f, args->v0.index)))
248 return -ENOENT;
249 } else
250 return ret;
251
252 ret = nvif_client_ioctl(client, argv, argc);
253 if (ret == 0 && atomic_xchg(&ntfy->enabled, 0))
254 kfree(ntfy->p);
255 return ret;
256 }
257
258 struct usif_object {
259 struct list_head head;
260 struct list_head ntfy;
261 u8 route;
262 u64 token;
263 };
264
265 static void
usif_object_dtor(struct usif_object * object)266 usif_object_dtor(struct usif_object *object)
267 {
268 list_del(&object->head);
269 kfree(object);
270 }
271
272 static int
usif_object_new(struct drm_file * f,void * data,u32 size,void * argv,u32 argc)273 usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
274 {
275 struct nouveau_cli *cli = nouveau_cli(f);
276 struct nvif_client *client = &cli->base;
277 union {
278 struct nvif_ioctl_new_v0 v0;
279 } *args = data;
280 struct usif_object *object;
281 int ret = -ENOSYS;
282
283 if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
284 return -ENOMEM;
285 list_add(&object->head, &cli->objects);
286
287 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
288 object->route = args->v0.route;
289 object->token = args->v0.token;
290 args->v0.route = NVDRM_OBJECT_USIF;
291 args->v0.token = (unsigned long)(void *)object;
292 ret = nvif_client_ioctl(client, argv, argc);
293 args->v0.token = object->token;
294 args->v0.route = object->route;
295 }
296
297 if (ret)
298 usif_object_dtor(object);
299 return ret;
300 }
301
302 int
303 #ifdef __NetBSD__
usif_ioctl(struct drm_file * filp,void * data,u32 argc)304 usif_ioctl(struct drm_file *filp, void *data, u32 argc)
305 #else
306 usif_ioctl(struct drm_file *filp, void __user *user, u32 argc)
307 #endif
308 {
309 struct nouveau_cli *cli = nouveau_cli(filp);
310 struct nvif_client *client = &cli->base;
311 #ifndef __NetBSD__
312 void *data = kmalloc(argc, GFP_KERNEL);
313 #endif
314 u32 size = argc;
315 union {
316 struct nvif_ioctl_v0 v0;
317 } *argv = data;
318 struct usif_object *object;
319 u8 owner;
320 int ret;
321
322 #ifndef __NetBSD__
323 if (ret = -ENOMEM, !argv)
324 goto done;
325 if (ret = -EFAULT, copy_from_user(argv, user, size))
326 goto done;
327 #endif
328
329 if (!(ret = nvif_unpack(-ENOSYS, &data, &size, argv->v0, 0, 0, true))) {
330 /* block access to objects not created via this interface */
331 owner = argv->v0.owner;
332 if (argv->v0.object == 0ULL &&
333 argv->v0.type != NVIF_IOCTL_V0_DEL)
334 argv->v0.owner = NVDRM_OBJECT_ANY; /* except client */
335 else
336 argv->v0.owner = NVDRM_OBJECT_USIF;
337 } else
338 goto done;
339
340 /* USIF slightly abuses some return-only ioctl members in order
341 * to provide interoperability with the older ABI16 objects
342 */
343 mutex_lock(&cli->mutex);
344 if (argv->v0.route) {
345 if (ret = -EINVAL, argv->v0.route == 0xff)
346 ret = nouveau_abi16_usif(filp, argv, argc);
347 if (ret) {
348 mutex_unlock(&cli->mutex);
349 goto done;
350 }
351 }
352
353 switch (argv->v0.type) {
354 case NVIF_IOCTL_V0_NEW:
355 ret = usif_object_new(filp, data, size, argv, argc);
356 break;
357 case NVIF_IOCTL_V0_NTFY_NEW:
358 ret = usif_notify_new(filp, data, size, argv, argc);
359 break;
360 case NVIF_IOCTL_V0_NTFY_DEL:
361 ret = usif_notify_del(filp, data, size, argv, argc);
362 break;
363 case NVIF_IOCTL_V0_NTFY_GET:
364 ret = usif_notify_get(filp, data, size, argv, argc);
365 break;
366 case NVIF_IOCTL_V0_NTFY_PUT:
367 ret = usif_notify_put(filp, data, size, argv, argc);
368 break;
369 case NVIF_IOCTL_V0_MAP_NETBSD:
370 /* Kernel-only kludge. */
371 ret = -EINVAL;
372 break;
373 default:
374 ret = nvif_client_ioctl(client, argv, argc);
375 break;
376 }
377 if (argv->v0.route == NVDRM_OBJECT_USIF) {
378 object = (void *)(unsigned long)argv->v0.token;
379 argv->v0.route = object->route;
380 argv->v0.token = object->token;
381 if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) {
382 list_del(&object->head);
383 kfree(object);
384 }
385 } else {
386 argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN;
387 argv->v0.token = 0;
388 }
389 argv->v0.owner = owner;
390 mutex_unlock(&cli->mutex);
391
392 #ifndef __NetBSD__
393 if (copy_to_user(user, argv, argc))
394 ret = -EFAULT;
395 #endif
396 done:
397 #ifndef __NetBSD__
398 kfree(argv);
399 #endif
400 return ret;
401 }
402
403 void
usif_client_fini(struct nouveau_cli * cli)404 usif_client_fini(struct nouveau_cli *cli)
405 {
406 struct usif_object *object, *otemp;
407 struct usif_notify *notify, *ntemp;
408
409 list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) {
410 usif_notify_dtor(notify);
411 }
412
413 list_for_each_entry_safe(object, otemp, &cli->objects, head) {
414 usif_object_dtor(object);
415 }
416 }
417
418 void
usif_client_init(struct nouveau_cli * cli)419 usif_client_init(struct nouveau_cli *cli)
420 {
421 INIT_LIST_HEAD(&cli->objects);
422 INIT_LIST_HEAD(&cli->notifys);
423 }
424