2 * Copyright 2014 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 #include "nouveau_drv.h"
26 #include "nouveau_usif.h"
27 #include "nouveau_abi16.h"
29 #include <nvif/notify.h>
30 #include <nvif/unpack.h>
31 #include <nvif/client.h>
32 #include <nvif/event.h>
33 #include <nvif/ioctl.h>
35 #include <nvif/class.h>
36 #include <nvif/cl0080.h>
38 struct usif_notify_p {
39 struct drm_pending_event base;
41 struct drm_event base;
47 struct list_head head;
53 struct usif_notify_p *p;
56 static inline struct usif_notify *
57 usif_notify_find(struct drm_file *filp, u32 handle)
59 struct nouveau_cli *cli = nouveau_cli(filp);
60 struct usif_notify *ntfy;
61 list_for_each_entry(ntfy, &cli->notifys, head) {
62 if (ntfy->handle == handle)
69 usif_notify_dtor(struct usif_notify *ntfy)
71 list_del(&ntfy->head);
76 usif_notify(const void *header, u32 length, const void *data, u32 size)
78 struct usif_notify *ntfy = NULL;
80 struct nvif_notify_rep_v0 v0;
82 struct drm_device *dev;
83 struct drm_file *filp;
86 if (length == sizeof(rep->v0) && rep->v0.version == 0) {
87 if (WARN_ON(!(ntfy = (void *)(unsigned long)rep->v0.token)))
88 return NVIF_NOTIFY_DROP;
89 BUG_ON(rep->v0.route != NVDRM_NOTIFY_USIF);
92 return NVIF_NOTIFY_DROP;
94 if (WARN_ON(!ntfy->p || ntfy->reply != (length + size)))
95 return NVIF_NOTIFY_DROP;
96 filp = ntfy->p->base.file_priv;
97 dev = filp->minor->dev;
99 memcpy(&ntfy->p->e.data[0], header, length);
100 memcpy(&ntfy->p->e.data[length], data, size);
101 switch (rep->v0.version) {
103 struct nvif_notify_rep_v0 *rep = (void *)ntfy->p->e.data;
104 rep->route = ntfy->route;
105 rep->token = ntfy->token;
113 spin_lock_irqsave(&dev->event_lock, flags);
114 if (!WARN_ON(filp->event_space < ntfy->p->e.base.length)) {
115 list_add_tail(&ntfy->p->base.link, &filp->event_list);
116 filp->event_space -= ntfy->p->e.base.length;
118 wake_up_interruptible(&filp->event_wait);
119 spin_unlock_irqrestore(&dev->event_lock, flags);
120 atomic_set(&ntfy->enabled, 0);
121 return NVIF_NOTIFY_DROP;
125 usif_notify_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
127 struct nouveau_cli *cli = nouveau_cli(f);
128 struct nvif_client *client = &cli->base;
130 struct nvif_ioctl_ntfy_new_v0 v0;
133 struct nvif_notify_req_v0 v0;
135 struct usif_notify *ntfy;
138 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
139 if (usif_notify_find(f, args->v0.index))
146 if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL)))
148 atomic_set(&ntfy->enabled, 0);
150 if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) {
151 ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply;
152 ntfy->route = req->v0.route;
153 ntfy->token = req->v0.token;
154 req->v0.route = NVDRM_NOTIFY_USIF;
155 req->v0.token = (unsigned long)(void *)ntfy;
156 ret = nvif_client_ioctl(client, argv, argc);
157 req->v0.token = ntfy->token;
158 req->v0.route = ntfy->route;
159 ntfy->handle = args->v0.index;
163 list_add(&ntfy->head, &cli->notifys);
170 usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
172 struct nouveau_cli *cli = nouveau_cli(f);
173 struct nvif_client *client = &cli->base;
175 struct nvif_ioctl_ntfy_del_v0 v0;
177 struct usif_notify *ntfy;
180 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
181 if (!(ntfy = usif_notify_find(f, args->v0.index)))
186 ret = nvif_client_ioctl(client, argv, argc);
188 usif_notify_dtor(ntfy);
193 usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
195 struct nouveau_cli *cli = nouveau_cli(f);
196 struct nvif_client *client = &cli->base;
198 struct nvif_ioctl_ntfy_del_v0 v0;
200 struct usif_notify *ntfy;
203 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
204 if (!(ntfy = usif_notify_find(f, args->v0.index)))
209 if (atomic_xchg(&ntfy->enabled, 1))
212 ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL);
213 if (ret = -ENOMEM, !ntfy->p)
215 ntfy->p->base.event = &ntfy->p->e.base;
216 ntfy->p->base.file_priv = f;
217 ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF;
218 ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply;
220 ret = nvif_client_ioctl(client, argv, argc);
223 atomic_set(&ntfy->enabled, 0);
230 usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
232 struct nouveau_cli *cli = nouveau_cli(f);
233 struct nvif_client *client = &cli->base;
235 struct nvif_ioctl_ntfy_put_v0 v0;
237 struct usif_notify *ntfy;
240 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
241 if (!(ntfy = usif_notify_find(f, args->v0.index)))
246 ret = nvif_client_ioctl(client, argv, argc);
247 if (ret == 0 && atomic_xchg(&ntfy->enabled, 0))
253 struct list_head head;
254 struct list_head ntfy;
260 usif_object_dtor(struct usif_object *object)
262 list_del(&object->head);
267 usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc, bool parent_abi16)
269 struct nouveau_cli *cli = nouveau_cli(f);
270 struct nvif_client *client = &cli->base;
272 struct nvif_ioctl_new_v0 v0;
274 struct usif_object *object;
277 if ((ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true)))
280 switch (args->v0.oclass) {
281 case NV_DMA_FROM_MEMORY:
282 case NV_DMA_TO_MEMORY:
283 case NV_DMA_IN_MEMORY:
287 struct nv_device_v0 v0;
290 if ((ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false)))
293 args->v0.priv = false;
302 if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
304 list_add(&object->head, &cli->objects);
306 object->route = args->v0.route;
307 object->token = args->v0.token;
308 args->v0.route = NVDRM_OBJECT_USIF;
309 args->v0.token = (unsigned long)(void *)object;
310 ret = nvif_client_ioctl(client, argv, argc);
312 usif_object_dtor(object);
316 args->v0.token = object->token;
317 args->v0.route = object->route;
322 usif_ioctl(struct drm_file *filp, void __user *user, u32 argc)
324 struct nouveau_cli *cli = nouveau_cli(filp);
325 struct nvif_client *client = &cli->base;
326 void *data = kmalloc(argc, GFP_KERNEL);
329 struct nvif_ioctl_v0 v0;
331 struct usif_object *object;
336 if (ret = -ENOMEM, !argv)
338 if (ret = -EFAULT, copy_from_user(argv, user, size))
341 if (!(ret = nvif_unpack(-ENOSYS, &data, &size, argv->v0, 0, 0, true))) {
342 /* block access to objects not created via this interface */
343 owner = argv->v0.owner;
344 if (argv->v0.object == 0ULL &&
345 argv->v0.type != NVIF_IOCTL_V0_DEL)
346 argv->v0.owner = NVDRM_OBJECT_ANY; /* except client */
348 argv->v0.owner = NVDRM_OBJECT_USIF;
352 /* USIF slightly abuses some return-only ioctl members in order
353 * to provide interoperability with the older ABI16 objects
355 mutex_lock(&cli->mutex);
356 if (argv->v0.route) {
357 if (ret = -EINVAL, argv->v0.route == 0xff)
358 ret = nouveau_abi16_usif(filp, argv, argc);
360 mutex_unlock(&cli->mutex);
367 switch (argv->v0.type) {
368 case NVIF_IOCTL_V0_NEW:
369 ret = usif_object_new(filp, data, size, argv, argc, abi16);
371 case NVIF_IOCTL_V0_NTFY_NEW:
372 ret = usif_notify_new(filp, data, size, argv, argc);
374 case NVIF_IOCTL_V0_NTFY_DEL:
375 ret = usif_notify_del(filp, data, size, argv, argc);
377 case NVIF_IOCTL_V0_NTFY_GET:
378 ret = usif_notify_get(filp, data, size, argv, argc);
380 case NVIF_IOCTL_V0_NTFY_PUT:
381 ret = usif_notify_put(filp, data, size, argv, argc);
384 ret = nvif_client_ioctl(client, argv, argc);
387 if (argv->v0.route == NVDRM_OBJECT_USIF) {
388 object = (void *)(unsigned long)argv->v0.token;
389 argv->v0.route = object->route;
390 argv->v0.token = object->token;
391 if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) {
392 list_del(&object->head);
396 argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN;
399 argv->v0.owner = owner;
400 mutex_unlock(&cli->mutex);
402 if (copy_to_user(user, argv, argc))
410 usif_client_fini(struct nouveau_cli *cli)
412 struct usif_object *object, *otemp;
413 struct usif_notify *notify, *ntemp;
415 list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) {
416 usif_notify_dtor(notify);
419 list_for_each_entry_safe(object, otemp, &cli->objects, head) {
420 usif_object_dtor(object);
425 usif_client_init(struct nouveau_cli *cli)
427 INIT_LIST_HEAD(&cli->objects);
428 INIT_LIST_HEAD(&cli->notifys);