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>
38 #include <sys/uio.h> /* must come first to avoid kfree() macros issues */
40 #include <linux/poll.h>
41 #include <linux/slab.h>
42 #include <linux/module.h>
43 #include "drm_legacy.h"
44 #include "drm_internal.h"
46 #include <sys/devfs.h>
48 /* from BKL pushdown */
49 DEFINE_MUTEX(drm_global_mutex);
52 * DOC: file operations
54 * Drivers must define the file operations structure that forms the DRM
55 * userspace API entry point, even though most of those operations are
56 * implemented in the DRM core. The mandatory functions are drm_open(),
57 * drm_read(), drm_ioctl() and drm_compat_ioctl if CONFIG_COMPAT is enabled.
58 * Drivers which implement private ioctls that require 32/64 bit compatibility
59 * support must provided their onw .compat_ioctl() handler that processes
60 * private ioctls and calls drm_compat_ioctl() for core ioctls.
62 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
63 * events are a generic and extensible means to send asynchronous events to
64 * userspace through the file descriptor. They are used to send vblank event and
65 * page flip completions by the KMS API. But drivers can also use it for their
66 * own needs, e.g. to signal completion of rendering.
68 * The memory mapping implementation will vary depending on how the driver
69 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
70 * function, modern drivers should use one of the provided memory-manager
71 * specific implementations. For GEM-based drivers this is drm_gem_mmap().
73 * No other file operations are supported by the DRM userspace API. Overall the
74 * following is an example #file_operations structure:
76 * static const example_drm_fops = {
77 * .owner = THIS_MODULE,
79 * .release = drm_release,
80 * .unlocked_ioctl = drm_ioctl,
81 * #ifdef CONFIG_COMPAT
82 * .compat_ioctl = drm_compat_ioctl,
86 * .llseek = no_llseek,
87 * .mmap = drm_gem_mmap,
91 extern drm_pci_id_list_t *drm_find_description(int vendor, int device,
92 drm_pci_id_list_t *idlist);
93 extern devclass_t drm_devclass;
95 static int drm_setup(struct drm_device * dev)
99 if (dev->driver->firstopen &&
100 !drm_core_check_feature(dev, DRIVER_MODESET)) {
101 ret = dev->driver->firstopen(dev);
108 ret = drm_legacy_dma_setup(dev);
112 init_waitqueue_head(&dev->lock.lock_queue);
113 if (!drm_core_check_feature(dev, DRIVER_MODESET))
114 dev->irq_enabled = 0;
115 dev->context_flag = 0;
116 dev->last_context = 0;
119 dev->buf_sigio = NULL;
126 #define DRIVER_SOFTC(unit) \
127 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
130 * drm_open - open method for DRM file
131 * @inode: device inode
132 * @filp: file pointer.
134 * This function must be used by drivers as their .open() #file_operations
135 * method. It looks up the correct DRM device and instantiates all the per-file
140 * 0 on success or negative errno value on falure.
142 int drm_open(struct dev_open_args *ap)
144 struct cdev *kdev = ap->a_head.a_dev;
145 int flags = ap->a_oflags;
147 struct thread *p = curthread;
148 struct drm_device *dev;
151 dev = DRIVER_SOFTC(minor(kdev));
155 DRM_DEBUG("open_count = %d\n", dev->open_count);
157 retcode = drm_open_helper(kdev, flags, fmt, p, dev, ap->a_fp);
160 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
162 device_busy(dev->dev->bsddev);
163 if (!dev->open_count++)
164 retcode = drm_setup(dev);
168 DRM_DEBUG("return %d\n", retcode);
172 EXPORT_SYMBOL(drm_open);
175 * Check whether DRI will run on this CPU.
177 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
181 * drm_new_set_master - Allocate a new master object and become master for the
182 * associated master realm.
184 * @dev: The associated device.
185 * @fpriv: File private identifying the client.
187 * This function must be called with dev::struct_mutex held.
188 * Returns negative error code on failure. Zero on success.
192 * Called whenever a process opens /dev/drm.
194 * \param filp file pointer.
195 * \param minor acquired minor-object.
196 * \return zero on success or a negative number on failure.
198 * Creates and initializes a drm_file structure for the file private data in \p
199 * filp and add it into the double linked list in \p dev.
201 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
202 struct drm_device *dev, struct file *filp)
204 struct drm_file *priv;
208 return EBUSY; /* No exclusive opens */
211 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
213 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
217 filp->private_data = priv;
219 priv->uid = p->td_proc->p_ucred->cr_svuid;
220 priv->pid = p->td_proc->p_pid;
223 /* for compatibility root is always authenticated */
224 priv->authenticated = capable(CAP_SYS_ADMIN);
225 priv->lock_count = 0;
227 INIT_LIST_HEAD(&priv->lhead);
228 INIT_LIST_HEAD(&priv->fbs);
229 lockinit(&priv->fbs_lock, "dpfl", 0, LK_CANRECURSE);
230 INIT_LIST_HEAD(&priv->blobs);
231 INIT_LIST_HEAD(&priv->pending_event_list);
232 INIT_LIST_HEAD(&priv->event_list);
233 init_waitqueue_head(&priv->event_wait);
234 priv->event_space = 4096; /* set aside 4k for event buffer */
236 lockinit(&priv->event_read_lock, "dperl", 0, LK_CANRECURSE);
238 if (drm_core_check_feature(dev, DRIVER_GEM))
239 drm_gem_open(dev, priv);
241 if (dev->driver->open) {
242 /* shared code returns -errno */
243 retcode = -dev->driver->open(dev, priv);
250 /* first opener automatically becomes master */
251 mutex_lock(&dev->master_mutex);
252 priv->is_master = list_empty(&dev->filelist);
253 mutex_unlock(&dev->master_mutex);
255 mutex_lock(&dev->filelist_mutex);
256 list_add(&priv->lhead, &dev->filelist);
257 mutex_unlock(&dev->filelist_mutex);
260 retcode = devfs_set_cdevpriv(filp, priv, &drm_cdevpriv_dtor);
262 drm_cdevpriv_dtor(priv);
268 * drm_legacy_dev_reinit
270 * Reinitializes a legacy/ums drm device in it's lastclose function.
272 static void drm_legacy_dev_reinit(struct drm_device *dev)
274 if (dev->irq_enabled)
275 drm_irq_uninstall(dev);
277 mutex_lock(&dev->struct_mutex);
279 drm_legacy_agp_clear(dev);
281 drm_legacy_sg_cleanup(dev);
283 drm_legacy_vma_flush(dev);
285 drm_legacy_dma_takedown(dev);
287 mutex_unlock(&dev->struct_mutex);
289 dev->sigdata.lock = NULL;
291 dev->context_flag = 0;
292 dev->last_context = 0;
295 DRM_DEBUG("lastclose completed\n");
299 * Take down the DRM device.
301 * \param dev DRM device structure.
303 * Frees every resource in \p dev.
307 void drm_lastclose(struct drm_device * dev)
311 if (dev->driver->lastclose)
312 dev->driver->lastclose(dev);
313 DRM_DEBUG("driver lastclose completed\n");
315 if (dev->irq_enabled && !drm_core_check_feature(dev, DRIVER_MODESET))
316 drm_irq_uninstall(dev);
318 mutex_lock(&dev->struct_mutex);
326 drm_legacy_agp_clear(dev);
328 drm_legacy_sg_cleanup(dev);
329 drm_legacy_dma_takedown(dev);
331 if (dev->lock.hw_lock) {
332 dev->lock.hw_lock = NULL; /* SHM removed */
333 dev->lock.file_priv = NULL;
334 wakeup(&dev->lock.lock_queue);
337 mutex_unlock(&dev->struct_mutex);
339 DRM_DEBUG("lastclose completed\n");
341 if (!drm_core_check_feature(dev, DRIVER_MODESET))
342 drm_legacy_dev_reinit(dev);
346 * drm_release - release method for DRM file
347 * @inode: device inode
348 * @filp: file pointer.
350 * This function must be used by drivers as their .release() #file_operations
351 * method. It frees any resources associated with the open file, and if this is
352 * the last open file for the DRM device also proceeds to call drm_lastclose().
356 * Always succeeds and returns 0.
358 //int drm_release(struct inode *inode, struct file *filp)
359 int drm_release(device_t kdev)
361 // XXX: filp is needed in this function
363 struct drm_file *file_priv = filp->private_data;
365 struct drm_device *dev = device_get_softc(kdev);
368 mutex_lock(&drm_global_mutex);
371 if (dev->magicfree.next) {
372 list_for_each_entry_safe(pt, next, &dev->magicfree, head) {
374 drm_ht_remove_item(&dev->magiclist, &pt->hash_item);
377 drm_ht_remove(&dev->magiclist);
381 /* ========================================================
382 * Begin inline drm_release
387 drm_sysctl_cleanup(dev);
388 if (dev->devnode != NULL)
389 destroy_dev(dev->devnode);
391 if (drm_core_check_feature(dev, DRIVER_GEM))
392 drm_gem_destroy(dev);
394 drm_vblank_cleanup(dev);
400 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
401 * worried about resource consumption while the DRM is inactive (between
402 * lastclose and firstopen or unload) because these aren't actually
403 * taking up KVA, just keeping the PCI resource allocated.
405 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
406 if (dev->pcir[i] == NULL)
408 bus_release_resource(dev->dev->bsddev, SYS_RES_MEMORY,
409 dev->pcirid[i], dev->pcir[i]);
418 if (dev->driver->unload != NULL) {
420 dev->driver->unload(dev);
424 if (pci_disable_busmaster(dev->dev->bsddev))
425 DRM_ERROR("Request to disable bus-master failed.\n");
427 lockuninit(&dev->vbl_lock);
428 lockuninit(&dev->dev_lock);
429 lockuninit(&dev->event_lock);
430 lockuninit(&dev->struct_mutex);
432 /* ========================================================
433 * End inline drm_release
436 mutex_unlock(&drm_global_mutex);
440 EXPORT_SYMBOL(drm_release);
443 * drm_read - read method for DRM file
444 * @filp: file pointer
445 * @buffer: userspace destination pointer for the read
446 * @count: count in bytes to read
447 * @offset: offset to read
449 * This function must be used by drivers as their .read() #file_operations
450 * method iff they use DRM events for asynchronous signalling to userspace.
451 * Since events are used by the KMS API for vblank and page flip completion this
452 * means all modern display drivers must use it.
454 * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
455 * must set the .llseek() #file_operation to no_llseek(). Polling support is
456 * provided by drm_poll().
458 * This function will only ever read a full event. Therefore userspace must
459 * supply a big enough buffer to fit any event to ensure forward progress. Since
460 * the maximum event space is currently 4K it's recommended to just use that for
465 * Number of bytes read (always aligned to full events, and can be 0) or a
466 * negative error code on failure.
469 ssize_t drm_read(struct file *filp, char __user *buffer,
470 size_t count, loff_t *offset)
472 int drm_read(struct dev_read_args *ap)
474 struct file *filp = ap->a_fp;
475 struct cdev *kdev = ap->a_head.a_dev;
476 struct uio *uio = ap->a_uio;
477 size_t count = uio->uio_resid;
478 struct drm_file *file_priv = filp->private_data;
479 struct drm_device *dev = drm_get_device_from_kdev(kdev);
480 int ret = 0; /* drm_read() returns int in DragonFly */
482 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
487 struct drm_pending_event *e = NULL;
489 spin_lock_irq(&dev->event_lock);
490 if (!list_empty(&file_priv->event_list)) {
491 e = list_first_entry(&file_priv->event_list,
492 struct drm_pending_event, link);
493 file_priv->event_space += e->event->length;
496 spin_unlock_irq(&dev->event_lock);
500 ret = 0; /* DragonFly expects a zero return value on success */
504 if (filp->f_flag & O_NONBLOCK) {
509 mutex_unlock(&file_priv->event_read_lock);
510 ret = wait_event_interruptible(file_priv->event_wait,
511 !list_empty(&file_priv->event_list));
513 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
517 unsigned length = e->event->length;
519 if (length > count - ret) {
521 spin_lock_irq(&dev->event_lock);
522 file_priv->event_space -= length;
523 list_add(&e->link, &file_priv->event_list);
524 spin_unlock_irq(&dev->event_lock);
528 if (uiomove((caddr_t)e->event, length, uio)) {
538 mutex_unlock(&file_priv->event_read_lock);
542 EXPORT_SYMBOL(drm_read);
545 * drm_poll - poll method for DRM file
546 * @filp: file pointer
547 * @wait: poll waiter table
549 * This function must be used by drivers as their .read() #file_operations
550 * method iff they use DRM events for asynchronous signalling to userspace.
551 * Since events are used by the KMS API for vblank and page flip completion this
552 * means all modern display drivers must use it.
554 * See also drm_read().
558 * Mask of POLL flags indicating the current status of the file.
562 drmfilt(struct knote *kn, long hint)
564 struct drm_file *file_priv = (struct drm_file *)kn->kn_hook;
567 // poll_wait(filp, &file_priv->event_wait, wait);
569 if (!list_empty(&file_priv->event_list))
576 drmfilt_detach(struct knote *kn)
578 struct drm_file *file_priv;
579 struct drm_device *dev;
582 file_priv = (struct drm_file *)kn->kn_hook;
583 dev = file_priv->dev;
585 klist = &file_priv->dkq.ki_note;
586 knote_remove(klist, kn);
589 static struct filterops drmfiltops =
590 { FILTEROP_MPSAFE | FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
593 drm_kqfilter(struct dev_kqfilter_args *ap)
595 struct file *filp = ap->a_fp;
596 struct drm_file *file_priv = filp->private_data;
597 struct knote *kn = ap->a_kn;
602 switch (kn->kn_filter) {
605 kn->kn_fop = &drmfiltops;
606 kn->kn_hook = (caddr_t)file_priv;
609 ap->a_result = EOPNOTSUPP;
613 klist = &file_priv->dkq.ki_note;
614 knote_insert(klist, kn);
621 * The Linux layer version of kfree() is a macro and can't be called
622 * directly via a function pointer
625 drm_event_destroy(struct drm_pending_event *e)
632 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
634 * @file_priv: DRM file private data
635 * @p: tracking structure for the pending event
636 * @e: actual event data to deliver to userspace
638 * This function prepares the passed in event for eventual delivery. If the event
639 * doesn't get delivered (because the IOCTL fails later on, before queuing up
640 * anything) then the even must be cancelled and freed using
641 * drm_event_cancel_free(). Successfully initialized events should be sent out
642 * using drm_send_event() or drm_send_event_locked() to signal completion of the
643 * asynchronous event to userspace.
645 * If callers embedded @p into a larger structure it must be allocated with
646 * kmalloc and @p must be the first member element.
648 * This is the locked version of drm_event_reserve_init() for callers which
649 * already hold dev->event_lock.
653 * 0 on success or a negative error code on failure.
655 int drm_event_reserve_init_locked(struct drm_device *dev,
656 struct drm_file *file_priv,
657 struct drm_pending_event *p,
660 if (file_priv->event_space < e->length)
663 file_priv->event_space -= e->length;
666 list_add(&p->pending_link, &file_priv->pending_event_list);
667 p->file_priv = file_priv;
669 /* we *could* pass this in as arg, but everyone uses kfree: */
671 p->destroy = drm_event_destroy;
673 p->destroy = (void (*) (struct drm_pending_event *)) kfree;
678 EXPORT_SYMBOL(drm_event_reserve_init_locked);
681 * drm_event_reserve_init - init a DRM event and reserve space for it
683 * @file_priv: DRM file private data
684 * @p: tracking structure for the pending event
685 * @e: actual event data to deliver to userspace
687 * This function prepares the passed in event for eventual delivery. If the event
688 * doesn't get delivered (because the IOCTL fails later on, before queuing up
689 * anything) then the even must be cancelled and freed using
690 * drm_event_cancel_free(). Successfully initialized events should be sent out
691 * using drm_send_event() or drm_send_event_locked() to signal completion of the
692 * asynchronous event to userspace.
694 * If callers embedded @p into a larger structure it must be allocated with
695 * kmalloc and @p must be the first member element.
697 * Callers which already hold dev->event_lock should use
698 * drm_event_reserve_init() instead.
702 * 0 on success or a negative error code on failure.
704 int drm_event_reserve_init(struct drm_device *dev,
705 struct drm_file *file_priv,
706 struct drm_pending_event *p,
712 spin_lock_irqsave(&dev->event_lock, flags);
713 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
714 spin_unlock_irqrestore(&dev->event_lock, flags);
718 EXPORT_SYMBOL(drm_event_reserve_init);
721 * drm_event_cancel_free - free a DRM event and release it's space
723 * @p: tracking structure for the pending event
725 * This function frees the event @p initialized with drm_event_reserve_init()
726 * and releases any allocated space.
728 void drm_event_cancel_free(struct drm_device *dev,
729 struct drm_pending_event *p)
732 spin_lock_irqsave(&dev->event_lock, flags);
734 p->file_priv->event_space += p->event->length;
735 list_del(&p->pending_link);
737 spin_unlock_irqrestore(&dev->event_lock, flags);
740 EXPORT_SYMBOL(drm_event_cancel_free);
743 * drm_send_event_locked - send DRM event to file descriptor
745 * @e: DRM event to deliver
747 * This function sends the event @e, initialized with drm_event_reserve_init(),
748 * to its associated userspace DRM file. Callers must already hold
749 * dev->event_lock, see drm_send_event() for the unlocked version.
751 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
753 assert_spin_locked(&dev->event_lock);
760 list_del(&e->pending_link);
761 list_add_tail(&e->link,
762 &e->file_priv->event_list);
763 wake_up_interruptible(&e->file_priv->event_wait);
765 KNOTE(&e->file_priv->dkq.ki_note, 0);
769 EXPORT_SYMBOL(drm_send_event_locked);
772 * drm_send_event - send DRM event to file descriptor
774 * @e: DRM event to deliver
776 * This function sends the event @e, initialized with drm_event_reserve_init(),
777 * to its associated userspace DRM file. This function acquires dev->event_lock,
778 * see drm_send_event_locked() for callers which already hold this lock.
780 * Note that the core will take care of unlinking and disarming events when the
781 * corresponding DRM file is closed. Drivers need not worry about whether the
782 * DRM file for this event still exists and can call this function upon
783 * completion of the asynchronous work unconditionally.
785 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
787 unsigned long irqflags;
789 spin_lock_irqsave(&dev->event_lock, irqflags);
790 drm_send_event_locked(dev, e);
791 spin_unlock_irqrestore(&dev->event_lock, irqflags);
793 EXPORT_SYMBOL(drm_send_event);