drm/i915: Update to Linux 3.17
[dragonfly.git] / sys / dev / drm / drm_fops.c
1 /**
2  * \file drm_fops.c
3  * File operations for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Daryll Strauss <daryll@valinux.com>
7  * \author Gareth Hughes <gareth@valinux.com>
8  */
9
10 /*
11  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
12  *
13  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15  * All Rights Reserved.
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  */
36
37 #include <sys/types.h>
38 #include <sys/conf.h>
39 #include <sys/devfs.h>
40
41 #include <drm/drmP.h>
42 #include <linux/module.h>
43 #include "drm_legacy.h"
44
45 /* from BKL pushdown: note that nothing else serializes idr_find() */
46 DEFINE_MUTEX(drm_global_mutex);
47 EXPORT_SYMBOL(drm_global_mutex);
48
49 extern drm_pci_id_list_t *drm_find_description(int vendor, int device,
50     drm_pci_id_list_t *idlist);
51 extern devclass_t drm_devclass;
52
53 static int drm_setup(struct drm_device *dev)
54 {
55         drm_local_map_t *map;
56         int i;
57
58         DRM_LOCK_ASSERT(dev);
59
60         /* prebuild the SAREA */
61         i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
62             _DRM_CONTAINS_LOCK, &map);
63         if (i != 0)
64                 return i;
65
66         if (dev->driver->firstopen)
67                 dev->driver->firstopen(dev);
68
69         dev->buf_use = 0;
70
71         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
72                 i = drm_dma_setup(dev);
73                 if (i != 0)
74                         return i;
75         }
76
77         drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
78         INIT_LIST_HEAD(&dev->magicfree);
79
80         init_waitqueue_head(&dev->lock.lock_queue);
81         if (!drm_core_check_feature(dev, DRIVER_MODESET))
82                 dev->irq_enabled = 0;
83         dev->context_flag = 0;
84         dev->last_context = 0;
85         dev->if_version = 0;
86
87         dev->buf_sigio = NULL;
88
89         DRM_DEBUG("\n");
90
91         return 0;
92 }
93
94 #define DRIVER_SOFTC(unit) \
95         ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
96
97 int
98 drm_open(struct dev_open_args *ap)
99 {
100         struct cdev *kdev = ap->a_head.a_dev;
101         int flags = ap->a_oflags;
102         int fmt = 0;
103         struct thread *p = curthread;
104         struct drm_device *dev;
105         int retcode;
106
107         dev = DRIVER_SOFTC(minor(kdev));
108         if (dev == NULL)
109                 return (ENXIO);
110
111         DRM_DEBUG("open_count = %d\n", dev->open_count);
112
113         retcode = drm_open_helper(kdev, flags, fmt, p, dev, ap->a_fp);
114
115         if (retcode == 0) {
116                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
117                 DRM_LOCK(dev);
118                 device_busy(dev->dev);
119                 if (!dev->open_count++)
120                         retcode = drm_setup(dev);
121                 DRM_UNLOCK(dev);
122         }
123
124         DRM_DEBUG("return %d\n", retcode);
125
126         return (retcode);
127 }
128
129 /* drm_open_helper is called whenever a process opens /dev/drm. */
130 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
131                     struct drm_device *dev, struct file *fp)
132 {
133         struct drm_file *priv;
134         int retcode;
135
136         if (flags & O_EXCL)
137                 return EBUSY; /* No exclusive opens */
138         dev->flags = flags;
139
140         DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
141
142         priv = kmalloc(sizeof(*priv), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
143         if (priv == NULL) {
144                 return ENOMEM;
145         }
146         
147         DRM_LOCK(dev);
148         priv->dev               = dev;
149         priv->uid               = p->td_proc->p_ucred->cr_svuid;
150         priv->pid               = p->td_proc->p_pid;
151
152         /* for compatibility root is always authenticated */
153         priv->authenticated = capable(CAP_SYS_ADMIN);
154
155         INIT_LIST_HEAD(&priv->fbs);
156         INIT_LIST_HEAD(&priv->event_list);
157         init_waitqueue_head(&priv->event_wait);
158         priv->event_space = 4096; /* set aside 4k for event buffer */
159
160         if (dev->driver->driver_features & DRIVER_GEM)
161                 drm_gem_open(dev, priv);
162
163         if (dev->driver->open) {
164                 /* shared code returns -errno */
165                 retcode = -dev->driver->open(dev, priv);
166                 if (retcode != 0) {
167                         drm_free(priv, M_DRM);
168                         DRM_UNLOCK(dev);
169                         return retcode;
170                 }
171         }
172
173         /* first opener automatically becomes master */
174         priv->master = list_empty(&dev->filelist);
175
176         list_add(&priv->lhead, &dev->filelist);
177         DRM_UNLOCK(dev);
178         kdev->si_drv1 = dev;
179
180         retcode = devfs_set_cdevpriv(fp, priv, &drm_cdevpriv_dtor);
181         if (retcode != 0)
182                 drm_cdevpriv_dtor(priv);
183
184         return retcode;
185 }
186
187 /**
188  * Release file.
189  *
190  * \param inode device inode
191  * \param file_priv DRM file private.
192  * \return zero on success or a negative number on failure.
193  *
194  * If the hardware lock is held then free it, and take it again for the kernel
195  * context since it's necessary to reclaim buffers. Unlink the file private
196  * data from its list and free it. Decreases the open count and if it reaches
197  * zero calls drm_lastclose().
198  */
199
200 static void drm_unload(struct drm_device *dev)
201 {
202         int i;
203
204         DRM_DEBUG("\n");
205
206         drm_sysctl_cleanup(dev);
207         if (dev->devnode != NULL)
208                 destroy_dev(dev->devnode);
209
210         if (dev->driver->driver_features & DRIVER_GEM)
211                 drm_gem_destroy(dev);
212
213         if (dev->agp && dev->agp->agp_mtrr) {
214                 int __unused retcode;
215
216                 retcode = drm_mtrr_del(0, dev->agp->agp_info.ai_aperture_base,
217                     dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC);
218                 DRM_DEBUG("mtrr_del = %d", retcode);
219         }
220
221         drm_vblank_cleanup(dev);
222
223         DRM_LOCK(dev);
224         drm_lastclose(dev);
225         DRM_UNLOCK(dev);
226
227         /* Clean up PCI resources allocated by drm_bufs.c.  We're not really
228          * worried about resource consumption while the DRM is inactive (between
229          * lastclose and firstopen or unload) because these aren't actually
230          * taking up KVA, just keeping the PCI resource allocated.
231          */
232         for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
233                 if (dev->pcir[i] == NULL)
234                         continue;
235                 bus_release_resource(dev->dev, SYS_RES_MEMORY,
236                     dev->pcirid[i], dev->pcir[i]);
237                 dev->pcir[i] = NULL;
238         }
239
240         if (dev->agp) {
241                 drm_free(dev->agp, M_DRM);
242                 dev->agp = NULL;
243         }
244
245         if (dev->driver->unload != NULL) {
246                 DRM_LOCK(dev);
247                 dev->driver->unload(dev);
248                 DRM_UNLOCK(dev);
249         }
250
251         drm_mem_uninit();
252
253         if (pci_disable_busmaster(dev->dev))
254                 DRM_ERROR("Request to disable bus-master failed.\n");
255
256         lockuninit(&dev->vbl_lock);
257         lockuninit(&dev->dev_lock);
258         lockuninit(&dev->event_lock);
259         lockuninit(&dev->struct_mutex);
260 }
261
262 int drm_release(device_t kdev)
263 {
264         struct drm_device *dev;
265
266         dev = device_get_softc(kdev);
267         drm_unload(dev);
268         if (dev->irqr) {
269                 bus_release_resource(dev->dev, SYS_RES_IRQ, dev->irqrid,
270                     dev->irqr);
271                 if (dev->irq_type == PCI_INTR_TYPE_MSI) {
272                         pci_release_msi(dev->dev);
273                         DRM_INFO("MSI released\n");
274                 }
275         }
276         return (0);
277 }
278
279 static bool
280 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
281     struct uio *uio, struct drm_pending_event **out)
282 {
283         struct drm_pending_event *e;
284
285         if (list_empty(&file_priv->event_list))
286                 return (false);
287         e = list_first_entry(&file_priv->event_list,
288             struct drm_pending_event, link);
289         if (e->event->length > uio->uio_resid)
290                 return (false);
291
292         file_priv->event_space += e->event->length;
293         list_del(&e->link);
294         *out = e;
295         return (true);
296 }
297
298 int
299 drm_read(struct dev_read_args *ap)
300 {
301         struct cdev *kdev = ap->a_head.a_dev;
302         struct uio *uio = ap->a_uio;
303         int ioflag = ap->a_ioflag;
304         struct drm_file *file_priv;
305         struct drm_device *dev;
306         struct drm_pending_event *e;
307         int error;
308
309         error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
310         if (error != 0) {
311                 DRM_ERROR("can't find authenticator\n");
312                 return (EINVAL);
313         }
314         dev = drm_get_device_from_kdev(kdev);
315         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
316         while (list_empty(&file_priv->event_list)) {
317                 if ((ioflag & O_NONBLOCK) != 0) {
318                         error = EAGAIN;
319                         goto out;
320                 }
321                 error = lksleep(&file_priv->event_space, &dev->event_lock,
322                    PCATCH, "drmrea", 0);
323                if (error != 0)
324                        goto out;
325         }
326         while (drm_dequeue_event(dev, file_priv, uio, &e)) {
327                 lockmgr(&dev->event_lock, LK_RELEASE);
328                 error = uiomove((caddr_t)e->event, e->event->length, uio);
329                 e->destroy(e);
330                 if (error != 0)
331                         return (error);
332                 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
333         }
334 out:
335         lockmgr(&dev->event_lock, LK_RELEASE);
336         return (error);
337 }
338
339 void
340 drm_event_wakeup(struct drm_pending_event *e)
341 {
342         struct drm_file *file_priv;
343         struct drm_device *dev;
344
345         file_priv = e->file_priv;
346         dev = file_priv->dev;
347         KKASSERT(lockstatus(&dev->event_lock, curthread) != 0);
348
349         wakeup(&file_priv->event_space);
350         KNOTE(&file_priv->dkq.ki_note, 0);
351 }
352
353 static int
354 drmfilt(struct knote *kn, long hint)
355 {
356         struct drm_file *file_priv;
357         struct drm_device *dev;
358         int ready = 0;
359
360         file_priv = (struct drm_file *)kn->kn_hook;
361         dev = file_priv->dev;
362         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
363         if (!list_empty(&file_priv->event_list))
364                 ready = 1;
365         lockmgr(&dev->event_lock, LK_RELEASE);
366
367         return (ready);
368 }
369
370 static void
371 drmfilt_detach(struct knote *kn)
372 {
373         struct drm_file *file_priv;
374         struct drm_device *dev;
375         struct klist *klist;
376
377         file_priv = (struct drm_file *)kn->kn_hook;
378         dev = file_priv->dev;
379
380         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
381         klist = &file_priv->dkq.ki_note;
382         knote_remove(klist, kn);
383         lockmgr(&dev->event_lock, LK_RELEASE);
384 }
385
386 static struct filterops drmfiltops =
387         { FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
388
389 int
390 drm_kqfilter(struct dev_kqfilter_args *ap)
391 {
392         struct cdev *kdev = ap->a_head.a_dev;
393         struct drm_file *file_priv;
394         struct drm_device *dev;
395         struct knote *kn = ap->a_kn;
396         struct klist *klist;
397         int error;
398
399         error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
400         if (error != 0) {
401                 DRM_ERROR("can't find authenticator\n");
402                 return (EINVAL);
403         }
404         dev = drm_get_device_from_kdev(kdev);
405
406         ap->a_result = 0;
407
408         switch (kn->kn_filter) {
409         case EVFILT_READ:
410         case EVFILT_WRITE:
411                 kn->kn_fop = &drmfiltops;
412                 kn->kn_hook = (caddr_t)file_priv;
413                 break;
414         default:
415                 ap->a_result = EOPNOTSUPP;
416                 return (0);
417         }
418
419         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
420         klist = &file_priv->dkq.ki_note;
421         knote_insert(klist, kn);
422         lockmgr(&dev->event_lock, LK_RELEASE);
423
424         return (0);
425 }