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