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 void 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);
304 drm_legacy_agp_clear(dev);
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");
321 * drm_release - release method for DRM file
322 * @inode: device inode
323 * @filp: file pointer.
325 * This function must be used by drivers as their .release() #file_operations
326 * method. It frees any resources associated with the open file, and if this is
327 * the last open file for the DRM device also proceeds to call drm_lastclose().
331 * Always succeeds and returns 0.
333 int drm_release(device_t kdev)
335 struct drm_device *dev = device_get_softc(kdev);
336 struct drm_magic_entry *pt, *next;
339 mutex_lock(&drm_global_mutex);
342 if (dev->magicfree.next) {
343 list_for_each_entry_safe(pt, next, &dev->magicfree, head) {
345 drm_ht_remove_item(&dev->magiclist, &pt->hash_item);
348 drm_ht_remove(&dev->magiclist);
351 /* ========================================================
352 * Begin inline drm_release
357 drm_sysctl_cleanup(dev);
358 if (dev->devnode != NULL)
359 destroy_dev(dev->devnode);
361 if (drm_core_check_feature(dev, DRIVER_GEM))
362 drm_gem_destroy(dev);
364 drm_vblank_cleanup(dev);
370 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
371 * worried about resource consumption while the DRM is inactive (between
372 * lastclose and firstopen or unload) because these aren't actually
373 * taking up KVA, just keeping the PCI resource allocated.
375 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
376 if (dev->pcir[i] == NULL)
378 bus_release_resource(dev->dev->bsddev, SYS_RES_MEMORY,
379 dev->pcirid[i], dev->pcir[i]);
388 if (dev->driver->unload != NULL) {
390 dev->driver->unload(dev);
394 if (pci_disable_busmaster(dev->dev->bsddev))
395 DRM_ERROR("Request to disable bus-master failed.\n");
397 lockuninit(&dev->vbl_lock);
398 lockuninit(&dev->dev_lock);
399 lockuninit(&dev->event_lock);
400 lockuninit(&dev->struct_mutex);
402 /* ========================================================
403 * End inline drm_release
407 bus_release_resource(dev->dev->bsddev, SYS_RES_IRQ, dev->irqrid,
409 if (dev->irq_type == PCI_INTR_TYPE_MSI) {
410 pci_release_msi(dev->dev->bsddev);
411 DRM_INFO("MSI released\n");
415 mutex_unlock(&drm_global_mutex);
419 EXPORT_SYMBOL(drm_release);
422 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
423 struct uio *uio, struct drm_pending_event **out)
425 struct drm_pending_event *e;
428 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
431 if (list_empty(&file_priv->event_list))
433 e = list_first_entry(&file_priv->event_list,
434 struct drm_pending_event, link);
435 if (e->event->length > uio->uio_resid)
438 file_priv->event_space += e->event->length;
444 lockmgr(&dev->event_lock, LK_RELEASE);
449 * drm_read - read method for DRM file
450 * @filp: file pointer
451 * @buffer: userspace destination pointer for the read
452 * @count: count in bytes to read
453 * @offset: offset to read
455 * This function must be used by drivers as their .read() #file_operations
456 * method iff they use DRM events for asynchronous signalling to userspace.
457 * Since events are used by the KMS API for vblank and page flip completion this
458 * means all modern display drivers must use it.
460 * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
461 * must set the .llseek() #file_operation to no_llseek(). Polling support is
462 * provided by drm_poll().
464 * This function will only ever read a full event. Therefore userspace must
465 * supply a big enough buffer to fit any event to ensure forward progress. Since
466 * the maximum event space is currently 4K it's recommended to just use that for
471 * Number of bytes read (always aligned to full events, and can be 0) or a
472 * negative error code on failure.
474 int drm_read(struct dev_read_args *ap)
476 struct cdev *kdev = ap->a_head.a_dev;
477 struct uio *uio = ap->a_uio;
478 struct drm_file *file_priv;
479 struct drm_device *dev = drm_get_device_from_kdev(kdev);
480 struct drm_pending_event *e;
484 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
486 DRM_ERROR("can't find authenticator\n");
490 ret = wait_event_interruptible(file_priv->event_wait,
491 !list_empty(&file_priv->event_list));
492 if (ret == -ERESTARTSYS)
497 while (drm_dequeue_event(dev, file_priv, uio, &e)) {
498 error = uiomove((caddr_t)e->event, e->event->length, uio);
508 * drm_poll - poll method for DRM file
509 * @filp: file pointer
510 * @wait: poll waiter table
512 * This function must be used by drivers as their .read() #file_operations
513 * method iff they use DRM events for asynchronous signalling to userspace.
514 * Since events are used by the KMS API for vblank and page flip completion this
515 * means all modern display drivers must use it.
517 * See also drm_read().
521 * Mask of POLL flags indicating the current status of the file.
525 drmfilt(struct knote *kn, long hint)
527 struct drm_file *file_priv;
528 struct drm_device *dev;
531 file_priv = (struct drm_file *)kn->kn_hook;
532 dev = file_priv->dev;
533 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
534 if (!list_empty(&file_priv->event_list))
536 lockmgr(&dev->event_lock, LK_RELEASE);
542 drmfilt_detach(struct knote *kn)
544 struct drm_file *file_priv;
545 struct drm_device *dev;
548 file_priv = (struct drm_file *)kn->kn_hook;
549 dev = file_priv->dev;
551 klist = &file_priv->dkq.ki_note;
552 knote_remove(klist, kn);
555 static struct filterops drmfiltops =
556 { FILTEROP_MPSAFE | FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
559 drm_kqfilter(struct dev_kqfilter_args *ap)
561 struct cdev *kdev = ap->a_head.a_dev;
562 struct drm_file *file_priv;
563 struct drm_device *dev;
564 struct knote *kn = ap->a_kn;
568 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
570 DRM_ERROR("can't find authenticator\n");
573 dev = drm_get_device_from_kdev(kdev);
577 switch (kn->kn_filter) {
580 kn->kn_fop = &drmfiltops;
581 kn->kn_hook = (caddr_t)file_priv;
584 ap->a_result = EOPNOTSUPP;
588 klist = &file_priv->dkq.ki_note;
589 knote_insert(klist, kn);
596 * The Linux layer version of kfree() is a macro and can't be called
597 * directly via a function pointer
600 drm_event_destroy(struct drm_pending_event *e)
607 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
609 * @file_priv: DRM file private data
610 * @p: tracking structure for the pending event
611 * @e: actual event data to deliver to userspace
613 * This function prepares the passed in event for eventual delivery. If the event
614 * doesn't get delivered (because the IOCTL fails later on, before queuing up
615 * anything) then the even must be cancelled and freed using
616 * drm_event_cancel_free(). Successfully initialized events should be sent out
617 * using drm_send_event() or drm_send_event_locked() to signal completion of the
618 * asynchronous event to userspace.
620 * If callers embedded @p into a larger structure it must be allocated with
621 * kmalloc and @p must be the first member element.
623 * This is the locked version of drm_event_reserve_init() for callers which
624 * already hold dev->event_lock.
628 * 0 on success or a negative error code on failure.
630 int drm_event_reserve_init_locked(struct drm_device *dev,
631 struct drm_file *file_priv,
632 struct drm_pending_event *p,
635 if (file_priv->event_space < e->length)
638 file_priv->event_space -= e->length;
641 p->file_priv = file_priv;
643 /* we *could* pass this in as arg, but everyone uses kfree: */
645 p->destroy = drm_event_destroy;
647 p->destroy = (void (*) (struct drm_pending_event *)) kfree;
652 EXPORT_SYMBOL(drm_event_reserve_init_locked);
655 * drm_event_reserve_init - init a DRM event and reserve space for it
657 * @file_priv: DRM file private data
658 * @p: tracking structure for the pending event
659 * @e: actual event data to deliver to userspace
661 * This function prepares the passed in event for eventual delivery. If the event
662 * doesn't get delivered (because the IOCTL fails later on, before queuing up
663 * anything) then the even must be cancelled and freed using
664 * drm_event_cancel_free(). Successfully initialized events should be sent out
665 * using drm_send_event() or drm_send_event_locked() to signal completion of the
666 * asynchronous event to userspace.
668 * If callers embedded @p into a larger structure it must be allocated with
669 * kmalloc and @p must be the first member element.
671 * Callers which already hold dev->event_lock should use
672 * drm_event_reserve_init() instead.
676 * 0 on success or a negative error code on failure.
678 int drm_event_reserve_init(struct drm_device *dev,
679 struct drm_file *file_priv,
680 struct drm_pending_event *p,
686 spin_lock_irqsave(&dev->event_lock, flags);
687 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
688 spin_unlock_irqrestore(&dev->event_lock, flags);
692 EXPORT_SYMBOL(drm_event_reserve_init);
695 * drm_event_cancel_free - free a DRM event and release it's space
697 * @p: tracking structure for the pending event
699 * This function frees the event @p initialized with drm_event_reserve_init()
700 * and releases any allocated space.
702 void drm_event_cancel_free(struct drm_device *dev,
703 struct drm_pending_event *p)
706 spin_lock_irqsave(&dev->event_lock, flags);
707 p->file_priv->event_space += p->event->length;
708 spin_unlock_irqrestore(&dev->event_lock, flags);
711 EXPORT_SYMBOL(drm_event_cancel_free);
714 * drm_send_event_locked - send DRM event to file descriptor
716 * @e: DRM event to deliver
718 * This function sends the event @e, initialized with drm_event_reserve_init(),
719 * to its associated userspace DRM file. Callers must already hold
720 * dev->event_lock, see drm_send_event() for the unlocked version.
722 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
724 assert_spin_locked(&dev->event_lock);
726 list_add_tail(&e->link,
727 &e->file_priv->event_list);
728 wake_up_interruptible(&e->file_priv->event_wait);
730 KNOTE(&e->file_priv->dkq.ki_note, 0);
734 EXPORT_SYMBOL(drm_send_event_locked);
737 * drm_send_event - send DRM event to file descriptor
739 * @e: DRM event to deliver
741 * This function sends the event @e, initialized with drm_event_reserve_init(),
742 * to its associated userspace DRM file. This function acquires dev->event_lock,
743 * see drm_send_event_locked() for callers which already hold this lock.
745 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
747 unsigned long irqflags;
749 spin_lock_irqsave(&dev->event_lock, irqflags);
750 drm_send_event_locked(dev, e);
751 spin_unlock_irqrestore(&dev->event_lock, irqflags);
753 EXPORT_SYMBOL(drm_send_event);