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