nouveau_usif.c revision 1.1.1.2 1 /* $NetBSD: nouveau_usif.c,v 1.1.1.2 2021/12/18 20:15:36 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 (at) redhat.com>
25 */
26
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_usif.c,v 1.1.1.2 2021/12/18 20:15:36 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 *
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
71 usif_notify_dtor(struct usif_notify *ntfy)
72 {
73 list_del(&ntfy->head);
74 kfree(ntfy);
75 }
76
77 int
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 wake_up_interruptible(&filp->event_wait);
121 spin_unlock_irqrestore(&dev->event_lock, flags);
122 atomic_set(&ntfy->enabled, 0);
123 return NVIF_NOTIFY_DROP;
124 }
125
126 static int
127 usif_notify_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
128 {
129 struct nouveau_cli *cli = nouveau_cli(f);
130 struct nvif_client *client = &cli->base;
131 union {
132 struct nvif_ioctl_ntfy_new_v0 v0;
133 } *args = data;
134 union {
135 struct nvif_notify_req_v0 v0;
136 } *req;
137 struct usif_notify *ntfy;
138 int ret = -ENOSYS;
139
140 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
141 if (usif_notify_find(f, args->v0.index))
142 return -EEXIST;
143 } else
144 return ret;
145 req = data;
146 ret = -ENOSYS;
147
148 if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL)))
149 return -ENOMEM;
150 atomic_set(&ntfy->enabled, 0);
151
152 if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) {
153 ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply;
154 ntfy->route = req->v0.route;
155 ntfy->token = req->v0.token;
156 req->v0.route = NVDRM_NOTIFY_USIF;
157 req->v0.token = (unsigned long)(void *)ntfy;
158 ret = nvif_client_ioctl(client, argv, argc);
159 req->v0.token = ntfy->token;
160 req->v0.route = ntfy->route;
161 ntfy->handle = args->v0.index;
162 }
163
164 if (ret == 0)
165 list_add(&ntfy->head, &cli->notifys);
166 if (ret)
167 kfree(ntfy);
168 return ret;
169 }
170
171 static int
172 usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
173 {
174 struct nouveau_cli *cli = nouveau_cli(f);
175 struct nvif_client *client = &cli->base;
176 union {
177 struct nvif_ioctl_ntfy_del_v0 v0;
178 } *args = data;
179 struct usif_notify *ntfy;
180 int ret = -ENOSYS;
181
182 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
183 if (!(ntfy = usif_notify_find(f, args->v0.index)))
184 return -ENOENT;
185 } else
186 return ret;
187
188 ret = nvif_client_ioctl(client, argv, argc);
189 if (ret == 0)
190 usif_notify_dtor(ntfy);
191 return ret;
192 }
193
194 static int
195 usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
196 {
197 struct nouveau_cli *cli = nouveau_cli(f);
198 struct nvif_client *client = &cli->base;
199 union {
200 struct nvif_ioctl_ntfy_del_v0 v0;
201 } *args = data;
202 struct usif_notify *ntfy;
203 int ret = -ENOSYS;
204
205 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
206 if (!(ntfy = usif_notify_find(f, args->v0.index)))
207 return -ENOENT;
208 } else
209 return ret;
210
211 if (atomic_xchg(&ntfy->enabled, 1))
212 return 0;
213
214 ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL);
215 if (ret = -ENOMEM, !ntfy->p)
216 goto done;
217 ntfy->p->base.event = &ntfy->p->e.base;
218 ntfy->p->base.file_priv = f;
219 ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF;
220 ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply;
221
222 ret = nvif_client_ioctl(client, argv, argc);
223 done:
224 if (ret) {
225 atomic_set(&ntfy->enabled, 0);
226 kfree(ntfy->p);
227 }
228 return ret;
229 }
230
231 static int
232 usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
233 {
234 struct nouveau_cli *cli = nouveau_cli(f);
235 struct nvif_client *client = &cli->base;
236 union {
237 struct nvif_ioctl_ntfy_put_v0 v0;
238 } *args = data;
239 struct usif_notify *ntfy;
240 int ret = -ENOSYS;
241
242 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
243 if (!(ntfy = usif_notify_find(f, args->v0.index)))
244 return -ENOENT;
245 } else
246 return ret;
247
248 ret = nvif_client_ioctl(client, argv, argc);
249 if (ret == 0 && atomic_xchg(&ntfy->enabled, 0))
250 kfree(ntfy->p);
251 return ret;
252 }
253
254 struct usif_object {
255 struct list_head head;
256 struct list_head ntfy;
257 u8 route;
258 u64 token;
259 };
260
261 static void
262 usif_object_dtor(struct usif_object *object)
263 {
264 list_del(&object->head);
265 kfree(object);
266 }
267
268 static int
269 usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
270 {
271 struct nouveau_cli *cli = nouveau_cli(f);
272 struct nvif_client *client = &cli->base;
273 union {
274 struct nvif_ioctl_new_v0 v0;
275 } *args = data;
276 struct usif_object *object;
277 int ret = -ENOSYS;
278
279 if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
280 return -ENOMEM;
281 list_add(&object->head, &cli->objects);
282
283 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
284 object->route = args->v0.route;
285 object->token = args->v0.token;
286 args->v0.route = NVDRM_OBJECT_USIF;
287 args->v0.token = (unsigned long)(void *)object;
288 ret = nvif_client_ioctl(client, argv, argc);
289 args->v0.token = object->token;
290 args->v0.route = object->route;
291 }
292
293 if (ret)
294 usif_object_dtor(object);
295 return ret;
296 }
297
298 int
299 usif_ioctl(struct drm_file *filp, void __user *user, u32 argc)
300 {
301 struct nouveau_cli *cli = nouveau_cli(filp);
302 struct nvif_client *client = &cli->base;
303 void *data = kmalloc(argc, GFP_KERNEL);
304 u32 size = argc;
305 union {
306 struct nvif_ioctl_v0 v0;
307 } *argv = data;
308 struct usif_object *object;
309 u8 owner;
310 int ret;
311
312 if (ret = -ENOMEM, !argv)
313 goto done;
314 if (ret = -EFAULT, copy_from_user(argv, user, size))
315 goto done;
316
317 if (!(ret = nvif_unpack(-ENOSYS, &data, &size, argv->v0, 0, 0, true))) {
318 /* block access to objects not created via this interface */
319 owner = argv->v0.owner;
320 if (argv->v0.object == 0ULL &&
321 argv->v0.type != NVIF_IOCTL_V0_DEL)
322 argv->v0.owner = NVDRM_OBJECT_ANY; /* except client */
323 else
324 argv->v0.owner = NVDRM_OBJECT_USIF;
325 } else
326 goto done;
327
328 /* USIF slightly abuses some return-only ioctl members in order
329 * to provide interoperability with the older ABI16 objects
330 */
331 mutex_lock(&cli->mutex);
332 if (argv->v0.route) {
333 if (ret = -EINVAL, argv->v0.route == 0xff)
334 ret = nouveau_abi16_usif(filp, argv, argc);
335 if (ret) {
336 mutex_unlock(&cli->mutex);
337 goto done;
338 }
339 }
340
341 switch (argv->v0.type) {
342 case NVIF_IOCTL_V0_NEW:
343 ret = usif_object_new(filp, data, size, argv, argc);
344 break;
345 case NVIF_IOCTL_V0_NTFY_NEW:
346 ret = usif_notify_new(filp, data, size, argv, argc);
347 break;
348 case NVIF_IOCTL_V0_NTFY_DEL:
349 ret = usif_notify_del(filp, data, size, argv, argc);
350 break;
351 case NVIF_IOCTL_V0_NTFY_GET:
352 ret = usif_notify_get(filp, data, size, argv, argc);
353 break;
354 case NVIF_IOCTL_V0_NTFY_PUT:
355 ret = usif_notify_put(filp, data, size, argv, argc);
356 break;
357 default:
358 ret = nvif_client_ioctl(client, argv, argc);
359 break;
360 }
361 if (argv->v0.route == NVDRM_OBJECT_USIF) {
362 object = (void *)(unsigned long)argv->v0.token;
363 argv->v0.route = object->route;
364 argv->v0.token = object->token;
365 if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) {
366 list_del(&object->head);
367 kfree(object);
368 }
369 } else {
370 argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN;
371 argv->v0.token = 0;
372 }
373 argv->v0.owner = owner;
374 mutex_unlock(&cli->mutex);
375
376 if (copy_to_user(user, argv, argc))
377 ret = -EFAULT;
378 done:
379 kfree(argv);
380 return ret;
381 }
382
383 void
384 usif_client_fini(struct nouveau_cli *cli)
385 {
386 struct usif_object *object, *otemp;
387 struct usif_notify *notify, *ntemp;
388
389 list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) {
390 usif_notify_dtor(notify);
391 }
392
393 list_for_each_entry_safe(object, otemp, &cli->objects, head) {
394 usif_object_dtor(object);
395 }
396 }
397
398 void
399 usif_client_init(struct nouveau_cli *cli)
400 {
401 INIT_LIST_HEAD(&cli->objects);
402 INIT_LIST_HEAD(&cli->notifys);
403 }
404