summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Skeggs <bskeggs@redhat.com>2014-08-10 04:10:31 +1000
committerBen Skeggs <bskeggs@redhat.com>2014-08-10 05:26:59 +1000
commit19ae6962b47db91803f3100e9cab6f43ef348fc7 (patch)
tree6d4f2831f2e0f42a7496c049517b3ea04406561d
parent565bf0ec3ba22fc14671dcf0b7372ef585feba80 (diff)
drm: expose the full object/event interfaces to userspace
Signed-off-by: Ben Skeggs <bskeggs@redhat.com>
-rw-r--r--drm/Kbuild2
-rw-r--r--drm/nouveau_drm.c25
-rw-r--r--drm/nouveau_drm.h9
-rw-r--r--drm/nouveau_nvif.c3
-rw-r--r--drm/nouveau_usif.c384
-rw-r--r--drm/nouveau_usif.h9
-rw-r--r--drm/uapi/drm/nouveau_drm.h3
-rw-r--r--lib/Makefile.am8
-rw-r--r--lib/kern.c218
-rw-r--r--nvif/client.c1
-rw-r--r--nvif/driver.h1
11 files changed, 650 insertions, 13 deletions
diff --git a/drm/Kbuild b/drm/Kbuild
index 1a44d952..f5d7f7ce 100644
--- a/drm/Kbuild
+++ b/drm/Kbuild
@@ -340,7 +340,7 @@ nouveau-y += nouveau_drm.o nouveau_chan.o nouveau_dma.o nouveau_fence.o
nouveau-y += nouveau_vga.o nouveau_agp.o
nouveau-y += nouveau_ttm.o nouveau_sgdma.o nouveau_bo.o nouveau_gem.o
nouveau-y += nouveau_prime.o nouveau_abi16.o
-nouveau-y += nouveau_nvif.o
+nouveau-y += nouveau_nvif.o nouveau_usif.o
nouveau-y += nv04_fence.o nv10_fence.o nv17_fence.o
nouveau-y += nv50_fence.o nv84_fence.o nvc0_fence.o
diff --git a/drm/nouveau_drm.c b/drm/nouveau_drm.c
index 14fb8e86..606cc6b4 100644
--- a/drm/nouveau_drm.c
+++ b/drm/nouveau_drm.c
@@ -50,6 +50,7 @@
#include "nouveau_fbcon.h"
#include "nouveau_fence.h"
#include "nouveau_debugfs.h"
+#include "nouveau_usif.h"
MODULE_PARM_DESC(config, "option string to pass to driver core");
static char *nouveau_config;
@@ -107,8 +108,10 @@ nouveau_cli_create(u64 name, const char *sname,
int ret = nvif_client_init(NULL, NULL, sname, name,
nouveau_config, nouveau_debug,
&cli->base);
- if (ret == 0)
+ if (ret == 0) {
mutex_init(&cli->mutex);
+ usif_client_init(cli);
+ }
return ret;
}
return -ENOMEM;
@@ -119,6 +122,7 @@ nouveau_cli_destroy(struct nouveau_cli *cli)
{
nouveau_vm_ref(NULL, &nvkm_client(&cli->base)->vm, NULL);
nvif_client_fini(&cli->base);
+ usif_client_fini(cli);
}
static void
@@ -810,24 +814,31 @@ nouveau_ioctls[] = {
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
};
-long nouveau_drm_ioctl(struct file *filp,
- unsigned int cmd, unsigned long arg)
+long
+nouveau_drm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
- struct drm_file *file_priv = filp->private_data;
- struct drm_device *dev;
+ struct drm_file *filp = file->private_data;
+ struct drm_device *dev = filp->minor->dev;
long ret;
- dev = file_priv->minor->dev;
ret = pm_runtime_get_sync(dev->dev);
if (ret < 0 && ret != -EACCES)
return ret;
- ret = drm_ioctl(filp, cmd, arg);
+ switch (_IOC_NR(cmd) - DRM_COMMAND_BASE) {
+ case DRM_NOUVEAU_NVIF:
+ ret = usif_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
+ break;
+ default:
+ ret = drm_ioctl(file, cmd, arg);
+ break;
+ }
pm_runtime_mark_last_busy(dev->dev);
pm_runtime_put_autosuspend(dev->dev);
return ret;
}
+
static const struct file_operations
nouveau_driver_fops = {
.owner = THIS_MODULE,
diff --git a/drm/nouveau_drm.h b/drm/nouveau_drm.h
index 3a6ef501..b02b0245 100644
--- a/drm/nouveau_drm.h
+++ b/drm/nouveau_drm.h
@@ -9,8 +9,8 @@
#define DRIVER_DATE "20120801"
#define DRIVER_MAJOR 1
-#define DRIVER_MINOR 1
-#define DRIVER_PATCHLEVEL 2
+#define DRIVER_MINOR 2
+#define DRIVER_PATCHLEVEL 0
/*
* 1.1.1:
@@ -23,6 +23,9 @@
* bounds access to local memory to be silently ignored / return 0).
* 1.1.2:
* - fixes multiple bugs in flip completion events and timestamping
+ * 1.2.0:
+ * - object api exposed to userspace
+ * - fermi,kepler,maxwell zbc
*/
#include <nvif/client.h>
@@ -79,6 +82,8 @@ struct nouveau_cli {
struct list_head head;
struct mutex mutex;
void *abi16;
+ struct list_head objects;
+ struct list_head notifys;
};
static inline struct nouveau_cli *
diff --git a/drm/nouveau_nvif.c b/drm/nouveau_nvif.c
index c3838bff..47ca8862 100644
--- a/drm/nouveau_nvif.c
+++ b/drm/nouveau_nvif.c
@@ -37,6 +37,7 @@
#include <nvif/ioctl.h>
#include "nouveau_drm.h"
+#include "nouveau_usif.h"
static void
nvkm_client_unmap(void *priv, void *ptr, u32 size)
@@ -95,6 +96,8 @@ nvkm_client_ntfy(const void *header, u32 length, const void *data, u32 size)
switch (route) {
case NVDRM_NOTIFY_NVIF:
return nvif_notify(header, length, data, size);
+ case NVDRM_NOTIFY_USIF:
+ return usif_notify(header, length, data, size);
default:
WARN_ON(1);
break;
diff --git a/drm/nouveau_usif.c b/drm/nouveau_usif.c
new file mode 100644
index 00000000..cb1182d7
--- /dev/null
+++ b/drm/nouveau_usif.c
@@ -0,0 +1,384 @@
+/*
+ * Copyright 2014 Red Hat Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Ben Skeggs <bskeggs@redhat.com>
+ */
+
+#include "nouveau_drm.h"
+#include "nouveau_usif.h"
+
+#include <nvif/notify.h>
+#include <nvif/unpack.h>
+#include <nvif/client.h>
+#include <nvif/event.h>
+#include <nvif/ioctl.h>
+
+struct usif_notify_p {
+ struct drm_pending_event base;
+ struct {
+ struct drm_event base;
+ u8 data[];
+ } e;
+};
+
+struct usif_notify {
+ struct list_head head;
+ atomic_t enabled;
+ u32 handle;
+ u16 reply;
+ u8 route;
+ u64 token;
+ struct usif_notify_p *p;
+};
+
+static inline struct usif_notify *
+usif_notify_find(struct drm_file *filp, u32 handle)
+{
+ struct nouveau_cli *cli = nouveau_cli(filp);
+ struct usif_notify *ntfy;
+ list_for_each_entry(ntfy, &cli->notifys, head) {
+ if (ntfy->handle == handle)
+ return ntfy;
+ }
+ return NULL;
+}
+
+static inline void
+usif_notify_dtor(struct usif_notify *ntfy)
+{
+ list_del(&ntfy->head);
+ kfree(ntfy);
+}
+
+int
+usif_notify(const void *header, u32 length, const void *data, u32 size)
+{
+ struct usif_notify *ntfy = NULL;
+ const union {
+ struct nvif_notify_rep_v0 v0;
+ } *rep = header;
+ struct drm_device *dev;
+ struct drm_file *filp;
+ unsigned long flags;
+
+ if (length == sizeof(rep->v0) && rep->v0.version == 0) {
+ if (WARN_ON(!(ntfy = (void *)(unsigned long)rep->v0.token)))
+ return NVIF_NOTIFY_DROP;
+ BUG_ON(rep->v0.route != NVDRM_NOTIFY_USIF);
+ } else
+ if (WARN_ON(1))
+ return NVIF_NOTIFY_DROP;
+
+ if (WARN_ON(!ntfy->p || ntfy->reply != (length + size)))
+ return NVIF_NOTIFY_DROP;
+ filp = ntfy->p->base.file_priv;
+ dev = filp->minor->dev;
+
+ memcpy(&ntfy->p->e.data[0], header, length);
+ memcpy(&ntfy->p->e.data[length], data, size);
+ switch (rep->v0.version) {
+ case 0: {
+ struct nvif_notify_rep_v0 *rep = (void *)ntfy->p->e.data;
+ rep->route = ntfy->route;
+ rep->token = ntfy->token;
+ }
+ break;
+ default:
+ BUG_ON(1);
+ break;
+ }
+
+ spin_lock_irqsave(&dev->event_lock, flags);
+ if (!WARN_ON(filp->event_space < ntfy->p->e.base.length)) {
+ list_add_tail(&ntfy->p->base.link, &filp->event_list);
+ filp->event_space -= ntfy->p->e.base.length;
+ }
+ wake_up_interruptible(&filp->event_wait);
+ spin_unlock_irqrestore(&dev->event_lock, flags);
+ atomic_set(&ntfy->enabled, 0);
+ return NVIF_NOTIFY_DROP;
+}
+
+static int
+usif_notify_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
+{
+ struct nouveau_cli *cli = nouveau_cli(f);
+ struct nvif_client *client = &cli->base;
+ union {
+ struct nvif_ioctl_ntfy_new_v0 v0;
+ } *args = data;
+ union {
+ struct nvif_notify_req_v0 v0;
+ } *req;
+ struct usif_notify *ntfy;
+ int ret;
+
+ if (nvif_unpack(args->v0, 0, 0, true)) {
+ if (usif_notify_find(f, args->v0.index))
+ return -EEXIST;
+ } else
+ return ret;
+ req = data;
+
+ if (!(ntfy = kmalloc(sizeof(*ntfy), GFP_KERNEL)))
+ return -ENOMEM;
+ atomic_set(&ntfy->enabled, 0);
+
+ if (nvif_unpack(req->v0, 0, 0, true)) {
+ ntfy->reply = sizeof(struct nvif_notify_rep_v0) + req->v0.reply;
+ ntfy->route = req->v0.route;
+ ntfy->token = req->v0.token;
+ req->v0.route = NVDRM_NOTIFY_USIF;
+ req->v0.token = (unsigned long)(void *)ntfy;
+ ret = nvif_client_ioctl(client, argv, argc);
+ req->v0.token = ntfy->token;
+ req->v0.route = ntfy->route;
+ ntfy->handle = args->v0.index;
+ }
+
+ if (ret == 0)
+ list_add(&ntfy->head, &cli->notifys);
+ if (ret)
+ kfree(ntfy);
+ return ret;
+}
+
+static int
+usif_notify_del(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
+{
+ struct nouveau_cli *cli = nouveau_cli(f);
+ struct nvif_client *client = &cli->base;
+ union {
+ struct nvif_ioctl_ntfy_del_v0 v0;
+ } *args = data;
+ struct usif_notify *ntfy;
+ int ret;
+
+ if (nvif_unpack(args->v0, 0, 0, true)) {
+ if (!(ntfy = usif_notify_find(f, args->v0.index)))
+ return -ENOENT;
+ } else
+ return ret;
+
+ ret = nvif_client_ioctl(client, argv, argc);
+ if (ret == 0)
+ usif_notify_dtor(ntfy);
+ return ret;
+}
+
+static int
+usif_notify_get(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
+{
+ struct nouveau_cli *cli = nouveau_cli(f);
+ struct nvif_client *client = &cli->base;
+ union {
+ struct nvif_ioctl_ntfy_del_v0 v0;
+ } *args = data;
+ struct usif_notify *ntfy;
+ int ret;
+
+ if (nvif_unpack(args->v0, 0, 0, true)) {
+ if (!(ntfy = usif_notify_find(f, args->v0.index)))
+ return -ENOENT;
+ } else
+ return ret;
+
+ if (atomic_xchg(&ntfy->enabled, 1))
+ return 0;
+
+ ntfy->p = kmalloc(sizeof(*ntfy->p) + ntfy->reply, GFP_KERNEL);
+ if (ret = -ENOMEM, !ntfy->p)
+ goto done;
+ ntfy->p->base.event = &ntfy->p->e.base;
+ ntfy->p->base.file_priv = f;
+ ntfy->p->base.pid = current->pid;
+ ntfy->p->base.destroy =(void(*)(struct drm_pending_event *))kfree;
+ ntfy->p->e.base.type = DRM_NOUVEAU_EVENT_NVIF;
+ ntfy->p->e.base.length = sizeof(ntfy->p->e.base) + ntfy->reply;
+
+ ret = nvif_client_ioctl(client, argv, argc);
+done:
+ if (ret) {
+ atomic_set(&ntfy->enabled, 0);
+ kfree(ntfy->p);
+ }
+ return ret;
+}
+
+static int
+usif_notify_put(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
+{
+ struct nouveau_cli *cli = nouveau_cli(f);
+ struct nvif_client *client = &cli->base;
+ union {
+ struct nvif_ioctl_ntfy_put_v0 v0;
+ } *args = data;
+ struct usif_notify *ntfy;
+ int ret;
+
+ if (nvif_unpack(args->v0, 0, 0, true)) {
+ if (!(ntfy = usif_notify_find(f, args->v0.index)))
+ return -ENOENT;
+ } else
+ return ret;
+
+ ret = nvif_client_ioctl(client, argv, argc);
+ if (ret == 0 && atomic_xchg(&ntfy->enabled, 0))
+ kfree(ntfy->p);
+ return ret;
+}
+
+struct usif_object {
+ struct list_head head;
+ struct list_head ntfy;
+ u8 route;
+ u64 token;
+};
+
+static void
+usif_object_dtor(struct usif_object *object)
+{
+ list_del(&object->head);
+ kfree(object);
+}
+
+static int
+usif_object_new(struct drm_file *f, void *data, u32 size, void *argv, u32 argc)
+{
+ struct nouveau_cli *cli = nouveau_cli(f);
+ struct nvif_client *client = &cli->base;
+ union {
+ struct nvif_ioctl_new_v0 v0;
+ } *args = data;
+ struct usif_object *object;
+ int ret;
+
+ if (!(object = kmalloc(sizeof(*object), GFP_KERNEL)))
+ return -ENOMEM;
+ list_add(&object->head, &cli->objects);
+
+ if (nvif_unpack(args->v0, 0, 0, true)) {
+ object->route = args->v0.route;
+ object->token = args->v0.token;
+ args->v0.route = NVDRM_OBJECT_USIF;
+ args->v0.token = (unsigned long)(void *)object;
+ ret = nvif_client_ioctl(client, argv, argc);
+ args->v0.token = object->token;
+ args->v0.route = object->route;
+ }
+
+ if (ret)
+ usif_object_dtor(object);
+ return ret;
+}
+
+int
+usif_ioctl(struct drm_file *filp, void __user *user, u32 argc)
+{
+ struct nouveau_cli *cli = nouveau_cli(filp);
+ struct nvif_client *client = &cli->base;
+ void *data = kmalloc(argc, GFP_KERNEL);
+ u32 size = argc;
+ union {
+ struct nvif_ioctl_v0 v0;
+ } *argv = data;
+ struct usif_object *object;
+ u8 owner;
+ int ret;
+
+ if (ret = -ENOMEM, !argv)
+ goto done;
+ if (ret = -EFAULT, copy_from_user(argv, user, size))
+ goto done;
+
+ if (nvif_unpack(argv->v0, 0, 0, true)) {
+ /* block access to objects not created via this interface */
+ owner = argv->v0.owner;
+ argv->v0.owner = NVDRM_OBJECT_USIF;
+ } else
+ goto done;
+
+ mutex_lock(&cli->mutex);
+ switch (argv->v0.type) {
+ case NVIF_IOCTL_V0_NEW:
+ /* ... except if we're creating children */
+ argv->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
+ ret = usif_object_new(filp, data, size, argv, argc);
+ break;
+ case NVIF_IOCTL_V0_NTFY_NEW:
+ ret = usif_notify_new(filp, data, size, argv, argc);
+ break;
+ case NVIF_IOCTL_V0_NTFY_DEL:
+ ret = usif_notify_del(filp, data, size, argv, argc);
+ break;
+ case NVIF_IOCTL_V0_NTFY_GET:
+ ret = usif_notify_get(filp, data, size, argv, argc);
+ break;
+ case NVIF_IOCTL_V0_NTFY_PUT:
+ ret = usif_notify_put(filp, data, size, argv, argc);
+ break;
+ default:
+ ret = nvif_client_ioctl(client, argv, argc);
+ break;
+ }
+ if (argv->v0.route == NVDRM_OBJECT_USIF) {
+ object = (void *)(unsigned long)argv->v0.token;
+ argv->v0.route = object->route;
+ argv->v0.token = object->token;
+ if (ret == 0 && argv->v0.type == NVIF_IOCTL_V0_DEL) {
+ list_del(&object->head);
+ kfree(object);
+ }
+ } else {
+ argv->v0.route = NVIF_IOCTL_V0_ROUTE_HIDDEN;
+ argv->v0.token = 0;
+ }
+ argv->v0.owner = owner;
+ mutex_unlock(&cli->mutex);
+
+ if (copy_to_user(user, argv, argc))
+ ret = -EFAULT;
+done:
+ kfree(argv);
+ return ret;
+}
+
+void
+usif_client_fini(struct nouveau_cli *cli)
+{
+ struct usif_object *object, *otemp;
+ struct usif_notify *notify, *ntemp;
+
+ list_for_each_entry_safe(notify, ntemp, &cli->notifys, head) {
+ usif_notify_dtor(notify);
+ }
+
+ list_for_each_entry_safe(object, otemp, &cli->objects, head) {
+ usif_object_dtor(object);
+ }
+}
+
+void
+usif_client_init(struct nouveau_cli *cli)
+{
+ INIT_LIST_HEAD(&cli->objects);
+ INIT_LIST_HEAD(&cli->notifys);
+}
diff --git a/drm/nouveau_usif.h b/drm/nouveau_usif.h
new file mode 100644
index 00000000..c037e3ae
--- /dev/null
+++ b/drm/nouveau_usif.h
@@ -0,0 +1,9 @@
+#ifndef __NOUVEAU_USIF_H__
+#define __NOUVEAU_USIF_H__
+
+void usif_client_init(struct nouveau_cli *);
+void usif_client_fini(struct nouveau_cli *);
+int usif_ioctl(struct drm_file *, void __user *, u32);
+int usif_notify(const void *, u32, const void *, u32);
+
+#endif
diff --git a/drm/uapi/drm/nouveau_drm.h b/drm/uapi/drm/nouveau_drm.h
index ed0b7bd4..0d7608dc 100644
--- a/drm/uapi/drm/nouveau_drm.h
+++ b/drm/uapi/drm/nouveau_drm.h
@@ -25,6 +25,8 @@
#ifndef __NOUVEAU_DRM_H__
#define __NOUVEAU_DRM_H__
+#define DRM_NOUVEAU_EVENT_NVIF 0x80000000
+
/* reserved object handles when using deprecated object APIs - these
* are here so that libdrm can allow interoperability with the new
* object APIs
@@ -131,6 +133,7 @@ struct drm_nouveau_gem_cpu_fini {
#define DRM_NOUVEAU_GROBJ_ALLOC 0x04 /* deprecated */
#define DRM_NOUVEAU_NOTIFIEROBJ_ALLOC 0x05 /* deprecated */
#define DRM_NOUVEAU_GPUOBJ_FREE 0x06 /* deprecated */
+#define DRM_NOUVEAU_NVIF 0x07
#define DRM_NOUVEAU_GEM_NEW 0x40
#define DRM_NOUVEAU_GEM_PUSHBUF 0x41
#define DRM_NOUVEAU_GEM_CPU_PREP 0x42
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 9eef51dc..00f8088b 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -2,9 +2,11 @@ noinst_LTLIBRARIES = libpciaccessos.la
libpciaccessos_la_CPPFLAGS = -I${top_srcdir}/nvkm/include \
-I${top_srcdir}/nvkm \
- -I${top_srcdir}
+ -I${top_srcdir} \
+ -I/usr/include/libdrm
libpciaccessos_la_CFLAGS = @PCIACCESS_CFLAGS@
-libpciaccessos_la_LIBADD = @PCIACCESS_LIBS@ -lpthread \
+libpciaccessos_la_LIBADD = @PCIACCESS_LIBS@ -lpthread -ldrm \
$(top_srcdir)/nvkm/libnvkm.la
libpciaccessos_la_SOURCES = intr.c \
- main.c
+ main.c \
+ kern.c
diff --git a/lib/kern.c b/lib/kern.c
new file mode 100644
index 00000000..47725862
--- /dev/null
+++ b/lib/kern.c
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2014 Red Hat Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Authors: Ben Skeggs <bskeggs@redhat.com>
+ */
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+
+#include <xf86drm.h>
+
+#include <nvif/client.h>
+#include <nvif/driver.h>
+#include <nvif/notify.h>
+#include <nvif/unpack.h>
+#include <nvif/event.h>
+#include <nvif/ioctl.h>
+
+#include <drm/uapi/drm/nouveau_drm.h>
+
+struct drm_client_priv {
+ int fd;
+ u32 version;
+ pthread_t event;
+ bool done;
+};
+
+static void
+drm_client_unmap(void *priv, void *ptr, u32 size)
+{
+}
+
+static void *
+drm_client_map(void *priv, u64 handle, u32 size)
+{
+ return NULL;
+}
+
+static int
+drm_client_ioctl(void *priv, bool super, void *data, u32 size, void **hack)
+{
+ const unsigned long request = DRM_IOC(DRM_IOC_READWRITE, DRM_IOCTL_BASE,
+ DRM_COMMAND_BASE +
+ DRM_NOUVEAU_NVIF, size);
+ struct drm_client_priv *drm = priv;
+ int ret = ioctl(drm->fd, request, data);
+ if (ret < 0)
+ return -errno;
+ return ret;
+}
+
+static int
+drm_client_resume(void *priv)
+{
+ return -EINVAL;
+}
+
+static int
+drm_client_suspend(void *priv)
+{
+ return -ENOSYS;
+}
+
+static void
+drm_client_notify(void *repv, u32 repc)
+{
+ union {
+ struct nvif_notify_rep_v0 v0;
+ } *rep = repv;
+ void *data = repv;
+ u32 size = repc;
+ u64 token;
+ u8 route;
+ int ret;
+
+ printf("notify %d\n", repc);
+
+ if (nvif_unpack(rep->v0, 0, 0, true)) {
+ token = rep->v0.token;
+ route = rep->v0.route;
+ } else
+ assert(0);
+
+ switch (route) {
+ case NVIF_NOTIFY_V0_ROUTE_NVIF: {
+ struct nvif_notify *notify = (void *)(unsigned long)token;
+ assert(notify);
+ nvif_notify(repv, sizeof(rep->v0), data, size);
+ }
+ break;
+ default:
+ assert(0);
+ break;
+ }
+}
+
+static void *
+drm_client_event(void *arg)
+{
+ struct drm_client_priv *drm = arg;
+ struct drm_event *e;
+ char data[1024];
+ ssize_t size;
+ int i;
+
+ while (1) {
+ if ((size = read(drm->fd, data, sizeof(data))) <= 0)
+ continue;
+
+ for (i = 0; e = (void *)&data[i], i < size; i += e->length) {
+ u32 size = e->length - sizeof(*e);
+ u8 *data = (u8 *)e + sizeof(*e);
+ switch (e->type) {
+ case DRM_NOUVEAU_EVENT_NVIF:
+ drm_client_notify(data, size);
+ continue;
+ default:
+ break;
+ }
+ fprintf(stderr, "event %08x, %d\n", e->type, size);
+ }
+ }
+
+ return NULL;
+}
+
+static void
+drm_client_fini(void *priv)
+{
+ struct drm_client_priv *drm = priv;
+ if (drm) {
+ if (drm->fd >= 0) {
+ if (drm->done) {
+ pthread_cancel(drm->event);
+ pthread_join(drm->event, NULL);
+ }
+ close(drm->fd);
+ }
+ free(drm);
+ }
+}
+
+#define DRM_RENDER_PATH "/dev/dri/renderD"
+#define DRM_RENDER_MIN 128
+#define DRM_RENDER_MAX (128 + 63)
+static int
+drm_client_init(const char *name, u64 device, const char *cfg,
+ const char *dbg, void **ppriv)
+{
+ struct drm_client_priv *drm;
+ drmVersionPtr ver;
+ int ret, minor;
+ char path[128];
+
+ if (ret = -ENOMEM, !(drm = *ppriv = malloc(sizeof(*drm))))
+ goto fail;
+
+ for (minor = DRM_RENDER_MIN; minor <= DRM_RENDER_MAX; minor++) {
+ snprintf(path, sizeof(path), "/dev/dri/renderD%d", minor);
+ if ((drm->fd = open(path, O_RDWR, 0)) < 0)
+ continue;
+ if ((ver = drmGetVersion(drm->fd)) == NULL)
+ continue;
+ if (!strcmp(ver->name, "nouveau"))
+ break;
+ free(ver);
+ }
+
+ if (ret = -ENODEV, minor > DRM_RENDER_MAX)
+ goto fail;
+
+ drm->version = (ver->version_major << 24) |
+ (ver->version_minor << 8) |
+ ver->version_patchlevel;
+ free(ver);
+ if (ret = -ENOSYS, drm->version < 0x01000200)
+ goto fail;
+
+ if ((ret = pthread_create(&drm->event, NULL, drm_client_event, drm)))
+ goto fail;
+
+ drm->done = true;
+ return 0;
+fail:
+ drm_client_fini(drm);
+ return ret;
+}
+
+const struct nvif_driver
+nvif_driver_drm = {
+ .name = "drm",
+ .init = drm_client_init,
+ .fini = drm_client_fini,
+ .suspend = drm_client_suspend,
+ .resume = drm_client_resume,
+ .ioctl = drm_client_ioctl,
+ .map = drm_client_map,
+ .unmap = drm_client_unmap,
+ .keep = true,
+};
diff --git a/nvif/client.c b/nvif/client.c
index 20fb3868..3c4df1fc 100644
--- a/nvif/client.c
+++ b/nvif/client.c
@@ -60,6 +60,7 @@ nvif_drivers[] = {
#ifdef __KERNEL__
&nvif_driver_nvkm,
#else
+ &nvif_driver_drm,
&nvif_driver_lib,
#endif
NULL
diff --git a/nvif/driver.h b/nvif/driver.h
index ea5b1b88..b72a8f0c 100644
--- a/nvif/driver.h
+++ b/nvif/driver.h
@@ -15,6 +15,7 @@ struct nvif_driver {
};
extern const struct nvif_driver nvif_driver_nvkm;
+extern const struct nvif_driver nvif_driver_drm;
extern const struct nvif_driver nvif_driver_lib;
#endif