drm2: Handle locking
[dragonfly.git] / sys / dev / drm2 / drm_fops.c
1 /*-
2  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
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 (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Daryll Strauss <daryll@valinux.com>
28  *    Gareth Hughes <gareth@valinux.com>
29  *
30  * $FreeBSD: src/sys/dev/drm2/drm_fops.c,v 1.1 2012/05/22 11:07:44 kib Exp $
31  */
32
33 /** @file drm_fops.c
34  * Support code for dealing with the file privates associated with each
35  * open of the DRM device.
36  */
37
38 #include <dev/drm2/drmP.h>
39
40 /* drm_open_helper is called whenever a process opens /dev/drm. */
41 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
42                     struct drm_device *dev)
43 {
44         struct drm_file *priv;
45         int retcode;
46
47         if (flags & O_EXCL)
48                 return EBUSY; /* No exclusive opens */
49         dev->flags = flags;
50
51         DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
52
53         priv = kmalloc(sizeof(*priv), DRM_MEM_FILES, M_NOWAIT | M_ZERO);
54         if (priv == NULL) {
55                 return ENOMEM;
56         }
57
58         retcode = devfs_set_cdevpriv(priv, drm_close);
59         if (retcode != 0) {
60                 kfree(priv, DRM_MEM_FILES);
61                 return retcode;
62         }
63
64         DRM_LOCK(dev);
65         priv->dev               = dev;
66         priv->uid               = p->td_ucred->cr_svuid;
67         priv->pid               = p->td_proc->p_pid;
68         priv->ioctl_count       = 0;
69
70         /* for compatibility root is always authenticated */
71         priv->authenticated     = DRM_SUSER(p);
72
73         INIT_LIST_HEAD(&priv->fbs);
74         INIT_LIST_HEAD(&priv->event_list);
75         priv->event_space = 4096; /* set aside 4k for event buffer */
76
77         if (dev->driver->driver_features & DRIVER_GEM)
78                 drm_gem_open(dev, priv);
79
80         if (dev->driver->open) {
81                 /* shared code returns -errno */
82                 retcode = -dev->driver->open(dev, priv);
83                 if (retcode != 0) {
84                         devfs_clear_cdevpriv();
85                         kfree(priv, DRM_MEM_FILES);
86                         DRM_UNLOCK(dev);
87                         return retcode;
88                 }
89         }
90
91         /* first opener automatically becomes master */
92         priv->master = TAILQ_EMPTY(&dev->files);
93
94         TAILQ_INSERT_TAIL(&dev->files, priv, link);
95         DRM_UNLOCK(dev);
96         kdev->si_drv1 = dev;
97         return 0;
98 }
99
100 static bool
101 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
102     struct uio *uio, struct drm_pending_event **out)
103 {
104         struct drm_pending_event *e;
105
106         if (list_empty(&file_priv->event_list))
107                 return (false);
108         e = list_first_entry(&file_priv->event_list,
109             struct drm_pending_event, link);
110         if (e->event->length > uio->uio_resid)
111                 return (false);
112
113         file_priv->event_space += e->event->length;
114         list_del(&e->link);
115         *out = e;
116         return (true);
117 }
118
119 int
120 drm_read(struct cdev *kdev, struct uio *uio, int ioflag)
121 {
122         struct drm_file *file_priv;
123         struct drm_device *dev;
124         struct drm_pending_event *e;
125         int error;
126
127         error = devfs_get_cdevpriv((void **)&file_priv);
128         if (error != 0) {
129                 DRM_ERROR("can't find authenticator\n");
130                 return (EINVAL);
131         }
132         dev = drm_get_device_from_kdev(kdev);
133         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
134         while (list_empty(&file_priv->event_list)) {
135                 if ((ioflag & O_NONBLOCK) != 0) {
136                         error = EAGAIN;
137                         goto out;
138                 }
139                 error = lksleep(&file_priv->event_space, &dev->event_lock,
140                    PCATCH, "drmrea", 0);
141                if (error != 0)
142                        goto out;
143         }
144         while (drm_dequeue_event(dev, file_priv, uio, &e)) {
145                 lockmgr(&dev->event_lock, LK_RELEASE);
146                 error = uiomove(e->event, e->event->length, uio);
147                 e->destroy(e);
148                 if (error != 0)
149                         return (error);
150                 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
151         }
152 out:
153         lockmgr(&dev->event_lock, LK_RELEASE);
154         return (error);
155 }
156
157 void
158 drm_event_wakeup(struct drm_pending_event *e)
159 {
160         struct drm_file *file_priv;
161         struct drm_device *dev;
162
163         file_priv = e->file_priv;
164         dev = file_priv->dev;
165         KKASSERT(lockstatus(&dev->event_lock, curthread) != 0);
166
167         wakeup(&file_priv->event_space);
168         selwakeup(&file_priv->event_poll);
169 }
170
171 int
172 drm_poll(struct cdev *kdev, int events, struct thread *td)
173 {
174         struct drm_file *file_priv;
175         struct drm_device *dev;
176         int error, revents;
177
178         error = devfs_get_cdevpriv((void **)&file_priv);
179         if (error != 0) {
180                 DRM_ERROR("can't find authenticator\n");
181                 return (EINVAL);
182         }
183         dev = drm_get_device_from_kdev(kdev);
184
185         revents = 0;
186         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
187         if ((events & (POLLIN | POLLRDNORM)) != 0) {
188                 if (list_empty(&file_priv->event_list)) {
189                         selrecord(td, &file_priv->event_poll);
190                 } else {
191                         revents |= events & (POLLIN | POLLRDNORM);
192                 }
193         }
194         lockmgr(&dev->event_lock, LK_RELEASE);
195         return (revents);
196 }