drm: Implement and use Linux struct device
[dragonfly.git] / sys / dev / drm / drm_fops.c
blob8ea49270604a57d9373914f3ce15324e405fc272
1 /**
2 * \file drm_fops.c
3 * File operations for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
8 */
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
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:
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.
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.
37 #include <sys/types.h>
38 #include <sys/conf.h>
39 #include <sys/devfs.h>
41 #include <drm/drmP.h>
42 #include <linux/module.h>
43 #include "drm_legacy.h"
44 #include "drm_internal.h"
46 /* from BKL pushdown: note that nothing else serializes idr_find() */
47 DEFINE_MUTEX(drm_global_mutex);
48 EXPORT_SYMBOL(drm_global_mutex);
50 extern drm_pci_id_list_t *drm_find_description(int vendor, int device,
51 drm_pci_id_list_t *idlist);
52 extern devclass_t drm_devclass;
54 static int drm_setup(struct drm_device *dev)
56 drm_local_map_t *map;
57 int i;
59 DRM_LOCK_ASSERT(dev);
61 /* prebuild the SAREA */
62 i = drm_legacy_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
63 _DRM_CONTAINS_LOCK, &map);
64 if (i != 0)
65 return i;
67 if (dev->driver->firstopen)
68 dev->driver->firstopen(dev);
70 dev->buf_use = 0;
72 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
73 i = drm_legacy_dma_setup(dev);
74 if (i != 0)
75 return i;
78 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
79 INIT_LIST_HEAD(&dev->magicfree);
81 init_waitqueue_head(&dev->lock.lock_queue);
82 if (!drm_core_check_feature(dev, DRIVER_MODESET))
83 dev->irq_enabled = 0;
84 dev->context_flag = 0;
85 dev->last_context = 0;
86 dev->if_version = 0;
88 dev->buf_sigio = NULL;
90 DRM_DEBUG("\n");
92 return 0;
95 #define DRIVER_SOFTC(unit) \
96 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
98 int
99 drm_open(struct dev_open_args *ap)
101 struct cdev *kdev = ap->a_head.a_dev;
102 int flags = ap->a_oflags;
103 int fmt = 0;
104 struct thread *p = curthread;
105 struct drm_device *dev;
106 int retcode;
108 dev = DRIVER_SOFTC(minor(kdev));
109 if (dev == NULL)
110 return (ENXIO);
112 DRM_DEBUG("open_count = %d\n", dev->open_count);
114 retcode = drm_open_helper(kdev, flags, fmt, p, dev, ap->a_fp);
116 if (retcode == 0) {
117 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
118 DRM_LOCK(dev);
119 device_busy(dev->dev->bsddev);
120 if (!dev->open_count++)
121 retcode = drm_setup(dev);
122 DRM_UNLOCK(dev);
125 DRM_DEBUG("return %d\n", retcode);
127 return (retcode);
130 /* drm_open_helper is called whenever a process opens /dev/drm. */
131 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
132 struct drm_device *dev, struct file *fp)
134 struct drm_file *priv;
135 int retcode;
137 if (flags & O_EXCL)
138 return EBUSY; /* No exclusive opens */
139 dev->flags = flags;
141 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
143 priv = kmalloc(sizeof(*priv), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
144 if (priv == NULL) {
145 return ENOMEM;
148 DRM_LOCK(dev);
149 priv->dev = dev;
150 priv->uid = p->td_proc->p_ucred->cr_svuid;
151 priv->pid = p->td_proc->p_pid;
153 /* for compatibility root is always authenticated */
154 priv->authenticated = capable(CAP_SYS_ADMIN);
156 INIT_LIST_HEAD(&priv->fbs);
157 INIT_LIST_HEAD(&priv->event_list);
158 init_waitqueue_head(&priv->event_wait);
159 priv->event_space = 4096; /* set aside 4k for event buffer */
161 if (drm_core_check_feature(dev, DRIVER_GEM))
162 drm_gem_open(dev, priv);
164 if (dev->driver->open) {
165 /* shared code returns -errno */
166 retcode = -dev->driver->open(dev, priv);
167 if (retcode != 0) {
168 kfree(priv);
169 DRM_UNLOCK(dev);
170 return retcode;
174 /* first opener automatically becomes master */
175 priv->master = list_empty(&dev->filelist);
177 list_add(&priv->lhead, &dev->filelist);
178 DRM_UNLOCK(dev);
179 kdev->si_drv1 = dev;
181 retcode = devfs_set_cdevpriv(fp, priv, &drm_cdevpriv_dtor);
182 if (retcode != 0)
183 drm_cdevpriv_dtor(priv);
185 return retcode;
189 * Take down the DRM device.
191 * \param dev DRM device structure.
193 * Frees every resource in \p dev.
195 * \sa drm_device
197 int drm_lastclose(struct drm_device * dev)
199 DRM_DEBUG("\n");
201 if (dev->driver->lastclose)
202 dev->driver->lastclose(dev);
203 DRM_DEBUG("driver lastclose completed\n");
205 if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
206 drm_irq_uninstall(dev);
208 mutex_lock(&dev->struct_mutex);
210 if (dev->unique) {
211 kfree(dev->unique);
212 dev->unique = NULL;
213 dev->unique_len = 0;
216 drm_agp_clear(dev);
218 drm_legacy_sg_cleanup(dev);
219 drm_legacy_dma_takedown(dev);
221 if (dev->lock.hw_lock) {
222 dev->lock.hw_lock = NULL; /* SHM removed */
223 dev->lock.file_priv = NULL;
224 wakeup(&dev->lock.lock_queue);
227 mutex_unlock(&dev->struct_mutex);
229 DRM_DEBUG("lastclose completed\n");
230 return 0;
234 * Release file.
236 * \param inode device inode
237 * \param file_priv DRM file private.
238 * \return zero on success or a negative number on failure.
240 * If the hardware lock is held then free it, and take it again for the kernel
241 * context since it's necessary to reclaim buffers. Unlink the file private
242 * data from its list and free it. Decreases the open count and if it reaches
243 * zero calls drm_lastclose().
245 int drm_release(device_t kdev)
247 struct drm_device *dev = device_get_softc(kdev);
248 struct drm_magic_entry *pt, *next;
249 int i;
251 mutex_lock(&drm_global_mutex);
253 /* Clear pid list */
254 if (dev->magicfree.next) {
255 list_for_each_entry_safe(pt, next, &dev->magicfree, head) {
256 list_del(&pt->head);
257 drm_ht_remove_item(&dev->magiclist, &pt->hash_item);
258 kfree(pt);
260 drm_ht_remove(&dev->magiclist);
263 /* ========================================================
264 * Begin inline drm_release
267 DRM_DEBUG("\n");
269 drm_sysctl_cleanup(dev);
270 if (dev->devnode != NULL)
271 destroy_dev(dev->devnode);
273 if (drm_core_check_feature(dev, DRIVER_GEM))
274 drm_gem_destroy(dev);
276 drm_vblank_cleanup(dev);
278 DRM_LOCK(dev);
279 drm_lastclose(dev);
280 DRM_UNLOCK(dev);
282 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
283 * worried about resource consumption while the DRM is inactive (between
284 * lastclose and firstopen or unload) because these aren't actually
285 * taking up KVA, just keeping the PCI resource allocated.
287 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
288 if (dev->pcir[i] == NULL)
289 continue;
290 bus_release_resource(dev->dev->bsddev, SYS_RES_MEMORY,
291 dev->pcirid[i], dev->pcir[i]);
292 dev->pcir[i] = NULL;
295 if (dev->agp) {
296 kfree(dev->agp);
297 dev->agp = NULL;
300 if (dev->driver->unload != NULL) {
301 DRM_LOCK(dev);
302 dev->driver->unload(dev);
303 DRM_UNLOCK(dev);
306 if (pci_disable_busmaster(dev->dev->bsddev))
307 DRM_ERROR("Request to disable bus-master failed.\n");
309 lockuninit(&dev->vbl_lock);
310 lockuninit(&dev->dev_lock);
311 lockuninit(&dev->event_lock);
312 lockuninit(&dev->struct_mutex);
314 /* ========================================================
315 * End inline drm_release
318 if (dev->irqr) {
319 bus_release_resource(dev->dev->bsddev, SYS_RES_IRQ, dev->irqrid,
320 dev->irqr);
321 if (dev->irq_type == PCI_INTR_TYPE_MSI) {
322 pci_release_msi(dev->dev->bsddev);
323 DRM_INFO("MSI released\n");
327 mutex_unlock(&drm_global_mutex);
329 return (0);
332 static bool
333 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
334 struct uio *uio, struct drm_pending_event **out)
336 struct drm_pending_event *e;
337 bool ret = false;
339 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
341 *out = NULL;
342 if (list_empty(&file_priv->event_list))
343 goto out;
344 e = list_first_entry(&file_priv->event_list,
345 struct drm_pending_event, link);
346 if (e->event->length > uio->uio_resid)
347 goto out;
349 file_priv->event_space += e->event->length;
350 list_del(&e->link);
351 *out = e;
352 ret = true;
354 out:
355 lockmgr(&dev->event_lock, LK_RELEASE);
356 return ret;
360 drm_read(struct dev_read_args *ap)
362 struct cdev *kdev = ap->a_head.a_dev;
363 struct uio *uio = ap->a_uio;
364 struct drm_file *file_priv;
365 struct drm_device *dev = drm_get_device_from_kdev(kdev);
366 struct drm_pending_event *e;
367 int error;
368 int ret;
370 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
371 if (error != 0) {
372 DRM_ERROR("can't find authenticator\n");
373 return (EINVAL);
376 ret = wait_event_interruptible(file_priv->event_wait,
377 !list_empty(&file_priv->event_list));
378 if (ret == -ERESTARTSYS)
379 ret = -EINTR;
380 if (ret < 0)
381 return -ret;
383 while (drm_dequeue_event(dev, file_priv, uio, &e)) {
384 error = uiomove((caddr_t)e->event, e->event->length, uio);
385 e->destroy(e);
386 if (error != 0)
387 return (error);
390 return (error);
393 static int
394 drmfilt(struct knote *kn, long hint)
396 struct drm_file *file_priv;
397 struct drm_device *dev;
398 int ready = 0;
400 file_priv = (struct drm_file *)kn->kn_hook;
401 dev = file_priv->dev;
402 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
403 if (!list_empty(&file_priv->event_list))
404 ready = 1;
405 lockmgr(&dev->event_lock, LK_RELEASE);
407 return (ready);
410 static void
411 drmfilt_detach(struct knote *kn)
413 struct drm_file *file_priv;
414 struct drm_device *dev;
415 struct klist *klist;
417 file_priv = (struct drm_file *)kn->kn_hook;
418 dev = file_priv->dev;
420 klist = &file_priv->dkq.ki_note;
421 knote_remove(klist, kn);
424 static struct filterops drmfiltops =
425 { FILTEROP_MPSAFE | FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
428 drm_kqfilter(struct dev_kqfilter_args *ap)
430 struct cdev *kdev = ap->a_head.a_dev;
431 struct drm_file *file_priv;
432 struct drm_device *dev;
433 struct knote *kn = ap->a_kn;
434 struct klist *klist;
435 int error;
437 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
438 if (error != 0) {
439 DRM_ERROR("can't find authenticator\n");
440 return (EINVAL);
442 dev = drm_get_device_from_kdev(kdev);
444 ap->a_result = 0;
446 switch (kn->kn_filter) {
447 case EVFILT_READ:
448 case EVFILT_WRITE:
449 kn->kn_fop = &drmfiltops;
450 kn->kn_hook = (caddr_t)file_priv;
451 break;
452 default:
453 ap->a_result = EOPNOTSUPP;
454 return (0);
457 klist = &file_priv->dkq.ki_note;
458 knote_insert(klist, kn);
460 return (0);