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>
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
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>
39 #include <sys/devfs.h>
42 #include <linux/module.h>
43 #include "drm_legacy.h"
44 #include "drm_internal.h"
46 /* from BKL pushdown */
47 DEFINE_MUTEX(drm_global_mutex);
50 * DOC: file operations
52 * Drivers must define the file operations structure that forms the DRM
53 * userspace API entry point, even though most of those operations are
54 * implemented in the DRM core. The mandatory functions are drm_open(),
55 * drm_read(), drm_ioctl() and drm_compat_ioctl if CONFIG_COMPAT is enabled.
56 * Drivers which implement private ioctls that require 32/64 bit compatibility
57 * support must provided their onw .compat_ioctl() handler that processes
58 * private ioctls and calls drm_compat_ioctl() for core ioctls.
60 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
61 * events are a generic and extensible means to send asynchronous events to
62 * userspace through the file descriptor. They are used to send vblank event and
63 * page flip completions by the KMS API. But drivers can also use it for their
64 * own needs, e.g. to signal completion of rendering.
66 * The memory mapping implementation will vary depending on how the driver
67 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
68 * function, modern drivers should use one of the provided memory-manager
69 * specific implementations. For GEM-based drivers this is drm_gem_mmap().
71 * No other file operations are supported by the DRM userspace API. Overall the
72 * following is an example #file_operations structure:
74 * static const example_drm_fops = {
75 * .owner = THIS_MODULE,
77 * .release = drm_release,
78 * .unlocked_ioctl = drm_ioctl,
79 * #ifdef CONFIG_COMPAT
80 * .compat_ioctl = drm_compat_ioctl,
84 * .llseek = no_llseek,
85 * .mmap = drm_gem_mmap,
89 extern drm_pci_id_list_t *drm_find_description(int vendor, int device,
90 drm_pci_id_list_t *idlist);
91 extern devclass_t drm_devclass;
93 static int drm_setup(struct drm_device *dev)
100 /* prebuild the SAREA */
101 i = drm_legacy_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
102 _DRM_CONTAINS_LOCK, &map);
106 if (dev->driver->firstopen)
107 dev->driver->firstopen(dev);
111 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
112 i = drm_legacy_dma_setup(dev);
117 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
118 INIT_LIST_HEAD(&dev->magicfree);
120 init_waitqueue_head(&dev->lock.lock_queue);
121 if (!drm_core_check_feature(dev, DRIVER_MODESET))
122 dev->irq_enabled = 0;
123 dev->context_flag = 0;
124 dev->last_context = 0;
127 dev->buf_sigio = NULL;
134 #define DRIVER_SOFTC(unit) \
135 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
138 * drm_open - open method for DRM file
139 * @inode: device inode
140 * @filp: file pointer.
142 * This function must be used by drivers as their .open() #file_operations
143 * method. It looks up the correct DRM device and instantiates all the per-file
148 * 0 on success or negative errno value on falure.
150 int drm_open(struct dev_open_args *ap)
152 struct cdev *kdev = ap->a_head.a_dev;
153 int flags = ap->a_oflags;
155 struct thread *p = curthread;
156 struct drm_device *dev;
159 dev = DRIVER_SOFTC(minor(kdev));
163 DRM_DEBUG("open_count = %d\n", dev->open_count);
165 retcode = drm_open_helper(kdev, flags, fmt, p, dev, ap->a_fp);
168 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
170 device_busy(dev->dev->bsddev);
171 if (!dev->open_count++)
172 retcode = drm_setup(dev);
176 DRM_DEBUG("return %d\n", retcode);
180 EXPORT_SYMBOL(drm_open);
183 * Check whether DRI will run on this CPU.
185 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
189 * drm_new_set_master - Allocate a new master object and become master for the
190 * associated master realm.
192 * @dev: The associated device.
193 * @fpriv: File private identifying the client.
195 * This function must be called with dev::struct_mutex held.
196 * Returns negative error code on failure. Zero on success.
200 * Called whenever a process opens /dev/drm.
202 * \param filp file pointer.
203 * \param minor acquired minor-object.
204 * \return zero on success or a negative number on failure.
206 * Creates and initializes a drm_file structure for the file private data in \p
207 * filp and add it into the double linked list in \p dev.
209 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
210 struct drm_device *dev, struct file *fp)
212 struct drm_file *priv;
216 return EBUSY; /* No exclusive opens */
219 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
221 priv = kmalloc(sizeof(*priv), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
228 priv->uid = p->td_proc->p_ucred->cr_svuid;
229 priv->pid = p->td_proc->p_pid;
231 /* for compatibility root is always authenticated */
232 priv->authenticated = capable(CAP_SYS_ADMIN);
234 INIT_LIST_HEAD(&priv->lhead);
235 INIT_LIST_HEAD(&priv->fbs);
236 lockinit(&priv->fbs_lock, "dpfl", 0, LK_CANRECURSE);
237 INIT_LIST_HEAD(&priv->blobs);
238 INIT_LIST_HEAD(&priv->pending_event_list);
239 INIT_LIST_HEAD(&priv->event_list);
240 init_waitqueue_head(&priv->event_wait);
241 priv->event_space = 4096; /* set aside 4k for event buffer */
243 if (drm_core_check_feature(dev, DRIVER_GEM))
244 drm_gem_open(dev, priv);
246 if (dev->driver->open) {
247 /* shared code returns -errno */
248 retcode = -dev->driver->open(dev, priv);
256 /* first opener automatically becomes master */
257 priv->master = list_empty(&dev->filelist);
259 list_add(&priv->lhead, &dev->filelist);
263 retcode = devfs_set_cdevpriv(fp, priv, &drm_cdevpriv_dtor);
265 drm_cdevpriv_dtor(priv);
271 * drm_legacy_dev_reinit
273 * Reinitializes a legacy/ums drm device in it's lastclose function.
277 * Take down the DRM device.
279 * \param dev DRM device structure.
281 * Frees every resource in \p dev.
285 int drm_lastclose(struct drm_device * dev)
289 if (dev->driver->lastclose)
290 dev->driver->lastclose(dev);
291 DRM_DEBUG("driver lastclose completed\n");
293 if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
294 drm_irq_uninstall(dev);
296 mutex_lock(&dev->struct_mutex);
306 drm_legacy_sg_cleanup(dev);
307 drm_legacy_dma_takedown(dev);
309 if (dev->lock.hw_lock) {
310 dev->lock.hw_lock = NULL; /* SHM removed */
311 dev->lock.file_priv = NULL;
312 wakeup(&dev->lock.lock_queue);
315 mutex_unlock(&dev->struct_mutex);
317 DRM_DEBUG("lastclose completed\n");
322 * drm_release - release method for DRM file
323 * @inode: device inode
324 * @filp: file pointer.
326 * This function must be used by drivers as their .release() #file_operations
327 * method. It frees any resources associated with the open file, and if this is
328 * the last open file for the DRM device also proceeds to call drm_lastclose().
332 * Always succeeds and returns 0.
334 int drm_release(device_t kdev)
336 struct drm_device *dev = device_get_softc(kdev);
337 struct drm_magic_entry *pt, *next;
340 mutex_lock(&drm_global_mutex);
343 if (dev->magicfree.next) {
344 list_for_each_entry_safe(pt, next, &dev->magicfree, head) {
346 drm_ht_remove_item(&dev->magiclist, &pt->hash_item);
349 drm_ht_remove(&dev->magiclist);
352 /* ========================================================
353 * Begin inline drm_release
358 drm_sysctl_cleanup(dev);
359 if (dev->devnode != NULL)
360 destroy_dev(dev->devnode);
362 if (drm_core_check_feature(dev, DRIVER_GEM))
363 drm_gem_destroy(dev);
365 drm_vblank_cleanup(dev);
371 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
372 * worried about resource consumption while the DRM is inactive (between
373 * lastclose and firstopen or unload) because these aren't actually
374 * taking up KVA, just keeping the PCI resource allocated.
376 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
377 if (dev->pcir[i] == NULL)
379 bus_release_resource(dev->dev->bsddev, SYS_RES_MEMORY,
380 dev->pcirid[i], dev->pcir[i]);
389 if (dev->driver->unload != NULL) {
391 dev->driver->unload(dev);
395 if (pci_disable_busmaster(dev->dev->bsddev))
396 DRM_ERROR("Request to disable bus-master failed.\n");
398 lockuninit(&dev->vbl_lock);
399 lockuninit(&dev->dev_lock);
400 lockuninit(&dev->event_lock);
401 lockuninit(&dev->struct_mutex);
403 /* ========================================================
404 * End inline drm_release
408 bus_release_resource(dev->dev->bsddev, SYS_RES_IRQ, dev->irqrid,
410 if (dev->irq_type == PCI_INTR_TYPE_MSI) {
411 pci_release_msi(dev->dev->bsddev);
412 DRM_INFO("MSI released\n");
416 mutex_unlock(&drm_global_mutex);
420 EXPORT_SYMBOL(drm_release);
423 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
424 struct uio *uio, struct drm_pending_event **out)
426 struct drm_pending_event *e;
429 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
432 if (list_empty(&file_priv->event_list))
434 e = list_first_entry(&file_priv->event_list,
435 struct drm_pending_event, link);
436 if (e->event->length > uio->uio_resid)
439 file_priv->event_space += e->event->length;
445 lockmgr(&dev->event_lock, LK_RELEASE);
450 * drm_read - read method for DRM file
451 * @filp: file pointer
452 * @buffer: userspace destination pointer for the read
453 * @count: count in bytes to read
454 * @offset: offset to read
456 * This function must be used by drivers as their .read() #file_operations
457 * method iff they use DRM events for asynchronous signalling to userspace.
458 * Since events are used by the KMS API for vblank and page flip completion this
459 * means all modern display drivers must use it.
461 * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
462 * must set the .llseek() #file_operation to no_llseek(). Polling support is
463 * provided by drm_poll().
465 * This function will only ever read a full event. Therefore userspace must
466 * supply a big enough buffer to fit any event to ensure forward progress. Since
467 * the maximum event space is currently 4K it's recommended to just use that for
472 * Number of bytes read (always aligned to full events, and can be 0) or a
473 * negative error code on failure.
475 int drm_read(struct dev_read_args *ap)
477 struct cdev *kdev = ap->a_head.a_dev;
478 struct uio *uio = ap->a_uio;
479 struct drm_file *file_priv;
480 struct drm_device *dev = drm_get_device_from_kdev(kdev);
481 struct drm_pending_event *e;
485 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
487 DRM_ERROR("can't find authenticator\n");
491 ret = wait_event_interruptible(file_priv->event_wait,
492 !list_empty(&file_priv->event_list));
493 if (ret == -ERESTARTSYS)
498 while (drm_dequeue_event(dev, file_priv, uio, &e)) {
499 error = uiomove((caddr_t)e->event, e->event->length, uio);
509 * drm_poll - poll method for DRM file
510 * @filp: file pointer
511 * @wait: poll waiter table
513 * This function must be used by drivers as their .read() #file_operations
514 * method iff they use DRM events for asynchronous signalling to userspace.
515 * Since events are used by the KMS API for vblank and page flip completion this
516 * means all modern display drivers must use it.
518 * See also drm_read().
522 * Mask of POLL flags indicating the current status of the file.
526 drmfilt(struct knote *kn, long hint)
528 struct drm_file *file_priv;
529 struct drm_device *dev;
532 file_priv = (struct drm_file *)kn->kn_hook;
533 dev = file_priv->dev;
534 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
535 if (!list_empty(&file_priv->event_list))
537 lockmgr(&dev->event_lock, LK_RELEASE);
543 drmfilt_detach(struct knote *kn)
545 struct drm_file *file_priv;
546 struct drm_device *dev;
549 file_priv = (struct drm_file *)kn->kn_hook;
550 dev = file_priv->dev;
552 klist = &file_priv->dkq.ki_note;
553 knote_remove(klist, kn);
556 static struct filterops drmfiltops =
557 { FILTEROP_MPSAFE | FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
560 drm_kqfilter(struct dev_kqfilter_args *ap)
562 struct cdev *kdev = ap->a_head.a_dev;
563 struct drm_file *file_priv;
564 struct drm_device *dev;
565 struct knote *kn = ap->a_kn;
569 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
571 DRM_ERROR("can't find authenticator\n");
574 dev = drm_get_device_from_kdev(kdev);
578 switch (kn->kn_filter) {
581 kn->kn_fop = &drmfiltops;
582 kn->kn_hook = (caddr_t)file_priv;
585 ap->a_result = EOPNOTSUPP;
589 klist = &file_priv->dkq.ki_note;
590 knote_insert(klist, kn);
597 * The Linux layer version of kfree() is a macro and can't be called
598 * directly via a function pointer
601 drm_event_destroy(struct drm_pending_event *e)
608 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
610 * @file_priv: DRM file private data
611 * @p: tracking structure for the pending event
612 * @e: actual event data to deliver to userspace
614 * This function prepares the passed in event for eventual delivery. If the event
615 * doesn't get delivered (because the IOCTL fails later on, before queuing up
616 * anything) then the even must be cancelled and freed using
617 * drm_event_cancel_free(). Successfully initialized events should be sent out
618 * using drm_send_event() or drm_send_event_locked() to signal completion of the
619 * asynchronous event to userspace.
621 * If callers embedded @p into a larger structure it must be allocated with
622 * kmalloc and @p must be the first member element.
624 * This is the locked version of drm_event_reserve_init() for callers which
625 * already hold dev->event_lock.
629 * 0 on success or a negative error code on failure.
631 int drm_event_reserve_init_locked(struct drm_device *dev,
632 struct drm_file *file_priv,
633 struct drm_pending_event *p,
636 if (file_priv->event_space < e->length)
639 file_priv->event_space -= e->length;
642 p->file_priv = file_priv;
644 /* we *could* pass this in as arg, but everyone uses kfree: */
646 p->destroy = drm_event_destroy;
648 p->destroy = (void (*) (struct drm_pending_event *)) kfree;
653 EXPORT_SYMBOL(drm_event_reserve_init_locked);
656 * drm_event_reserve_init - init a DRM event and reserve space for it
658 * @file_priv: DRM file private data
659 * @p: tracking structure for the pending event
660 * @e: actual event data to deliver to userspace
662 * This function prepares the passed in event for eventual delivery. If the event
663 * doesn't get delivered (because the IOCTL fails later on, before queuing up
664 * anything) then the even must be cancelled and freed using
665 * drm_event_cancel_free(). Successfully initialized events should be sent out
666 * using drm_send_event() or drm_send_event_locked() to signal completion of the
667 * asynchronous event to userspace.
669 * If callers embedded @p into a larger structure it must be allocated with
670 * kmalloc and @p must be the first member element.
672 * Callers which already hold dev->event_lock should use
673 * drm_event_reserve_init() instead.
677 * 0 on success or a negative error code on failure.
679 int drm_event_reserve_init(struct drm_device *dev,
680 struct drm_file *file_priv,
681 struct drm_pending_event *p,
687 spin_lock_irqsave(&dev->event_lock, flags);
688 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
689 spin_unlock_irqrestore(&dev->event_lock, flags);
693 EXPORT_SYMBOL(drm_event_reserve_init);
696 * drm_event_cancel_free - free a DRM event and release it's space
698 * @p: tracking structure for the pending event
700 * This function frees the event @p initialized with drm_event_reserve_init()
701 * and releases any allocated space.
703 void drm_event_cancel_free(struct drm_device *dev,
704 struct drm_pending_event *p)
707 spin_lock_irqsave(&dev->event_lock, flags);
708 p->file_priv->event_space += p->event->length;
709 spin_unlock_irqrestore(&dev->event_lock, flags);
712 EXPORT_SYMBOL(drm_event_cancel_free);
715 * drm_send_event_locked - send DRM event to file descriptor
717 * @e: DRM event to deliver
719 * This function sends the event @e, initialized with drm_event_reserve_init(),
720 * to its associated userspace DRM file. Callers must already hold
721 * dev->event_lock, see drm_send_event() for the unlocked version.
723 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
725 assert_spin_locked(&dev->event_lock);
727 list_add_tail(&e->link,
728 &e->file_priv->event_list);
729 wake_up_interruptible(&e->file_priv->event_wait);
731 KNOTE(&e->file_priv->dkq.ki_note, 0);
735 EXPORT_SYMBOL(drm_send_event_locked);
738 * drm_send_event - send DRM event to file descriptor
740 * @e: DRM event to deliver
742 * This function sends the event @e, initialized with drm_event_reserve_init(),
743 * to its associated userspace DRM file. This function acquires dev->event_lock,
744 * see drm_send_event_locked() for callers which already hold this lock.
746 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
748 unsigned long irqflags;
750 spin_lock_irqsave(&dev->event_lock, irqflags);
751 drm_send_event_locked(dev, e);
752 spin_unlock_irqrestore(&dev->event_lock, irqflags);
754 EXPORT_SYMBOL(drm_send_event);