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/devfs.h>
41 #include <linux/module.h>
43 static int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
44 struct drm_device *dev, struct file *fp);
46 static int drm_setup(struct drm_device *dev)
52 /* prebuild the SAREA */
53 i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
54 _DRM_CONTAINS_LOCK, &map);
58 if (dev->driver->firstopen) {
59 ret = dev->driver->firstopen(dev);
64 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
65 !drm_core_check_feature(dev, DRIVER_MODESET)) {
67 atomic_set(&dev->buf_alloc, 0);
69 i = drm_dma_setup(dev);
74 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
75 atomic_set(&dev->counts[i], 0);
77 dev->sigdata.lock = NULL;
79 dev->context_flag = 0;
80 dev->interrupt_flag = 0;
82 dev->last_context = 0;
84 dev->last_checked = 0;
85 init_waitqueue_head(&dev->context_wait);
91 dev->buf_async = NULL;
92 init_waitqueue_head(&dev->buf_readers);
93 init_waitqueue_head(&dev->buf_writers);
98 * The kernel's context could be created here, but is now created
99 * in drm_dma_enqueue. This is more resource-efficient for
100 * hardware that does not do DMA, but may mean that
101 * drm_select_queue fails between the time the interrupt is
102 * initialized and the time the queues are initialized.
108 #define DRIVER_SOFTC(unit) \
109 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
114 * \param inode device inode
115 * \param filp file pointer.
116 * \return zero on success or a negative number on failure.
118 * Searches the DRM device with the same minor number, calls open_helper(), and
119 * increments the device open count. If the open count was previous at zero,
120 * i.e., it's the first that the device is open, then calls setup().
122 int drm_open(struct dev_open_args *ap)
124 struct cdev *kdev = ap->a_head.a_dev;
125 struct file *filp = ap->a_fp;
126 struct drm_device *dev = NULL;
127 int minor_id = minor(kdev);
128 struct drm_minor *minor;
131 /* old dfly variables below */
132 int flags = ap->a_oflags;
134 struct thread *p = curthread;
136 minor = idr_find(&drm_minors_idr, minor_id);
140 if (!(dev = minor->dev))
143 if (!dev->open_count++)
146 retcode = drm_open_helper(kdev, flags, fmt, p, dev, filp);
149 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
151 retcode = drm_setup(dev);
161 EXPORT_SYMBOL(drm_open);
164 * Called whenever a process opens /dev/drm.
166 * \param inode device inode.
167 * \param filp file pointer.
169 * \return zero on success or a negative number on failure.
171 * Creates and initializes a drm_file structure for the file private data in \p
172 * filp and add it into the double linked list in \p dev.
174 static int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
175 struct drm_device *dev, struct file *filp)
178 struct drm_file *priv;
182 return EBUSY; /* No exclusive opens */
185 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
187 priv = kmalloc(sizeof(*priv), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
191 filp->private_data = priv;
193 priv->uid = p->td_proc->p_ucred->cr_svuid;
194 priv->pid = p->td_proc->p_pid;
195 priv->minor = idr_find(&drm_minors_idr, minor_id);
196 priv->ioctl_count = 0;
197 /* for compatibility root is always authenticated */
198 priv->authenticated = DRM_SUSER(p);
199 priv->lock_count = 0;
201 INIT_LIST_HEAD(&priv->lhead);
202 INIT_LIST_HEAD(&priv->fbs);
203 lockinit(&priv->fbs_lock, "dfbsl", 0, LK_CANRECURSE);
204 INIT_LIST_HEAD(&priv->event_list);
205 init_waitqueue_head(&priv->event_wait);
206 priv->event_space = 4096; /* set aside 4k for event buffer */
208 if (dev->driver->driver_features & DRIVER_GEM)
209 drm_gem_open(dev, priv);
211 if (dev->driver->open) {
212 ret = dev->driver->open(dev, priv);
217 /* if there is no current master make this fd it */
218 mutex_lock(&dev->struct_mutex);
219 if (!priv->minor->master) {
220 /* create a new master */
221 priv->minor->master = drm_master_create(priv->minor);
222 if (!priv->minor->master) {
223 mutex_unlock(&dev->struct_mutex);
229 /* take another reference for the copy in the local file priv */
230 priv->master = drm_master_get(priv->minor->master);
232 priv->authenticated = 1;
234 mutex_unlock(&dev->struct_mutex);
235 if (dev->driver->master_create) {
236 ret = dev->driver->master_create(dev, priv->master);
238 mutex_lock(&dev->struct_mutex);
239 /* drop both references if this fails */
240 drm_master_put(&priv->minor->master);
241 drm_master_put(&priv->master);
242 mutex_unlock(&dev->struct_mutex);
246 mutex_lock(&dev->struct_mutex);
247 if (dev->driver->master_set) {
248 ret = dev->driver->master_set(dev, priv, true);
250 /* drop both references if this fails */
251 drm_master_put(&priv->minor->master);
252 drm_master_put(&priv->master);
253 mutex_unlock(&dev->struct_mutex);
257 mutex_unlock(&dev->struct_mutex);
259 /* get a reference to the master */
260 priv->master = drm_master_get(priv->minor->master);
261 mutex_unlock(&dev->struct_mutex);
264 mutex_lock(&dev->struct_mutex);
265 list_add(&priv->lhead, &dev->filelist);
266 mutex_unlock(&dev->struct_mutex);
271 filp->private_data = NULL;
278 * \param inode device inode
279 * \param file_priv DRM file private.
280 * \return zero on success or a negative number on failure.
282 * If the hardware lock is held then free it, and take it again for the kernel
283 * context since it's necessary to reclaim buffers. Unlink the file private
284 * data from its list and free it. Decreases the open count and if it reaches
285 * zero calls drm_lastclose().
288 #if 0 /* old drm_release equivalent from DragonFly */
289 void drm_cdevpriv_dtor(void *cd)
291 struct drm_file *file_priv = cd;
292 struct drm_device *dev = file_priv->dev;
295 DRM_DEBUG("open_count = %d\n", dev->open_count);
299 if (dev->driver->preclose != NULL)
300 dev->driver->preclose(dev, file_priv);
302 /* ========================================================
303 * Begin inline drm_release
306 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
307 DRM_CURRENTPID, (long)dev->dev, dev->open_count);
309 if (dev->driver->driver_features & DRIVER_GEM)
310 drm_gem_release(dev, file_priv);
312 if (dev->primary->master->lock.hw_lock
313 && _DRM_LOCK_IS_HELD(dev->primary->master->lock.hw_lock->lock)
314 && dev->primary->master->lock.file_priv == file_priv) {
315 DRM_DEBUG("Process %d dead, freeing lock for context %d\n",
317 _DRM_LOCKING_CONTEXT(dev->primary->master->lock.hw_lock->lock));
318 if (dev->driver->reclaim_buffers_locked != NULL)
319 dev->driver->reclaim_buffers_locked(dev, file_priv);
321 drm_lock_free(&dev->primary->master->lock,
322 _DRM_LOCKING_CONTEXT(dev->primary->master->lock.hw_lock->lock));
324 /* FIXME: may require heavy-handed reset of
325 hardware at this point, possibly
326 processed via a callback to the X
328 } else if (dev->driver->reclaim_buffers_locked != NULL &&
329 dev->primary->master->lock.hw_lock != NULL) {
330 /* The lock is required to reclaim buffers */
332 if (!dev->primary->master->lock.hw_lock) {
333 /* Device has been unregistered */
337 if (drm_lock_take(&dev->primary->master->lock, DRM_KERNEL_CONTEXT)) {
338 dev->primary->master->lock.file_priv = file_priv;
339 dev->primary->master->lock.lock_time = jiffies;
340 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
341 break; /* Got lock */
344 retcode = DRM_LOCK_SLEEP(dev, &dev->primary->master->lock.lock_queue,
345 PCATCH, "drmlk2", 0);
350 dev->driver->reclaim_buffers_locked(dev, file_priv);
351 drm_lock_free(&dev->primary->master->lock, DRM_KERNEL_CONTEXT);
355 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
356 !dev->driver->reclaim_buffers_locked)
357 drm_reclaim_buffers(dev, file_priv);
359 funsetown(&dev->buf_sigio);
361 if (dev->driver->postclose != NULL)
362 dev->driver->postclose(dev, file_priv);
363 list_del(&file_priv->lhead);
366 /* ========================================================
367 * End inline drm_release
370 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
371 device_unbusy(dev->dev);
372 if (--dev->open_count == 0) {
373 retcode = drm_lastclose(dev);
380 static void drm_unload(struct drm_device *dev)
386 drm_sysctl_cleanup(dev);
387 if (dev->devnode != NULL)
388 destroy_dev(dev->devnode);
390 drm_ctxbitmap_cleanup(dev);
392 if (dev->driver->driver_features & DRIVER_GEM)
393 drm_gem_destroy(dev);
395 if (dev->agp && dev->agp->agp_mtrr) {
396 int __unused retcode;
398 retcode = drm_mtrr_del(0, dev->agp->agp_info.ai_aperture_base,
399 dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC);
400 DRM_DEBUG("mtrr_del = %d", retcode);
403 drm_vblank_cleanup(dev);
409 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
410 * worried about resource consumption while the DRM is inactive (between
411 * lastclose and firstopen or unload) because these aren't actually
412 * taking up KVA, just keeping the PCI resource allocated.
414 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
415 if (dev->pcir[i] == NULL)
417 bus_release_resource(dev->dev, SYS_RES_MEMORY,
418 dev->pcirid[i], dev->pcir[i]);
423 drm_free(dev->agp, M_DRM);
427 if (dev->driver->unload != NULL) {
429 dev->driver->unload(dev);
435 if (pci_disable_busmaster(dev->dev))
436 DRM_ERROR("Request to disable bus-master failed.\n");
438 lockuninit(&dev->vbl_lock);
439 lockuninit(&dev->dev_lock);
440 lockuninit(&dev->event_lock);
441 lockuninit(&dev->struct_mutex);
444 int drm_release(device_t kdev)
446 struct drm_device *dev;
448 dev = device_get_softc(kdev);
451 bus_release_resource(dev->dev, SYS_RES_IRQ, dev->irqrid,
453 if (dev->msi_enabled) {
454 pci_release_msi(dev->dev);
455 DRM_INFO("MSI released\n");
462 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
463 struct uio *uio, struct drm_pending_event **out)
465 struct drm_pending_event *e;
467 if (list_empty(&file_priv->event_list))
469 e = list_first_entry(&file_priv->event_list,
470 struct drm_pending_event, link);
471 if (e->event->length > uio->uio_resid)
474 file_priv->event_space += e->event->length;
481 drm_read(struct dev_read_args *ap)
483 struct file *filp = ap->a_fp;
484 struct drm_file *file_priv = filp->private_data;
485 struct cdev *kdev = ap->a_head.a_dev;
486 struct uio *uio = ap->a_uio;
487 int ioflag = ap->a_ioflag;
488 struct drm_device *dev;
489 struct drm_pending_event *e;
492 dev = drm_get_device_from_kdev(kdev);
493 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
494 while (list_empty(&file_priv->event_list)) {
495 if ((ioflag & O_NONBLOCK) != 0) {
499 error = lksleep(&file_priv->event_space, &dev->event_lock,
500 PCATCH, "drmrea", 0);
504 while (drm_dequeue_event(dev, file_priv, uio, &e)) {
505 lockmgr(&dev->event_lock, LK_RELEASE);
506 error = uiomove((caddr_t)e->event, e->event->length, uio);
510 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
513 lockmgr(&dev->event_lock, LK_RELEASE);
518 drm_event_wakeup(struct drm_pending_event *e)
520 struct drm_file *file_priv = e->file_priv;
521 struct drm_device *dev = file_priv->minor->dev;
523 KKASSERT(lockstatus(&dev->event_lock, curthread) != 0);
525 wakeup(&file_priv->event_space);
526 KNOTE(&file_priv->dkq.ki_note, 0);
530 drmfilt(struct knote *kn, long hint)
532 struct drm_file *file_priv = (struct drm_file *)kn->kn_hook;
533 struct drm_device *dev = file_priv->minor->dev;
536 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
537 if (!list_empty(&file_priv->event_list))
539 lockmgr(&dev->event_lock, LK_RELEASE);
545 drmfilt_detach(struct knote *kn)
547 struct drm_file *file_priv = (struct drm_file *)kn->kn_hook;
548 struct drm_device *dev = file_priv->minor->dev;
551 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
552 klist = &file_priv->dkq.ki_note;
553 knote_remove(klist, kn);
554 lockmgr(&dev->event_lock, LK_RELEASE);
557 static struct filterops drmfiltops =
558 { FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
561 drm_kqfilter(struct dev_kqfilter_args *ap)
563 struct file *filp = ap->a_fp;
564 struct drm_file *file_priv = filp->private_data;
565 struct drm_device *dev = file_priv->minor->dev;
566 struct knote *kn = ap->a_kn;
571 switch (kn->kn_filter) {
573 kn->kn_fop = &drmfiltops;
574 kn->kn_hook = (caddr_t)file_priv;
577 ap->a_result = EOPNOTSUPP;
581 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
582 klist = &file_priv->dkq.ki_note;
583 knote_insert(klist, kn);
584 lockmgr(&dev->event_lock, LK_RELEASE);