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"
45 /* from BKL pushdown: note that nothing else serializes idr_find() */
46 DEFINE_MUTEX(drm_global_mutex);
47 EXPORT_SYMBOL(drm_global_mutex);
49 extern drm_pci_id_list_t *drm_find_description(int vendor, int device,
50 drm_pci_id_list_t *idlist);
51 extern devclass_t drm_devclass;
53 static int drm_setup(struct drm_device *dev)
60 /* prebuild the SAREA */
61 i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
62 _DRM_CONTAINS_LOCK, &map);
66 if (dev->driver->firstopen)
67 dev->driver->firstopen(dev);
71 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
72 i = drm_dma_setup(dev);
77 drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
78 INIT_LIST_HEAD(&dev->magicfree);
80 init_waitqueue_head(&dev->lock.lock_queue);
81 if (!drm_core_check_feature(dev, DRIVER_MODESET))
83 dev->context_flag = 0;
84 dev->last_context = 0;
87 dev->buf_sigio = NULL;
94 #define DRIVER_SOFTC(unit) \
95 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
98 drm_open(struct dev_open_args *ap)
100 struct cdev *kdev = ap->a_head.a_dev;
101 int flags = ap->a_oflags;
103 struct thread *p = curthread;
104 struct drm_device *dev;
107 dev = DRIVER_SOFTC(minor(kdev));
111 DRM_DEBUG("open_count = %d\n", dev->open_count);
113 retcode = drm_open_helper(kdev, flags, fmt, p, dev, ap->a_fp);
116 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
118 device_busy(dev->dev);
119 if (!dev->open_count++)
120 retcode = drm_setup(dev);
124 DRM_DEBUG("return %d\n", retcode);
129 /* drm_open_helper is called whenever a process opens /dev/drm. */
130 int drm_open_helper(struct cdev *kdev, int flags, int fmt, DRM_STRUCTPROC *p,
131 struct drm_device *dev, struct file *fp)
133 struct drm_file *priv;
137 return EBUSY; /* No exclusive opens */
140 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID, devtoname(kdev));
142 priv = kmalloc(sizeof(*priv), M_DRM, M_WAITOK | M_NULLOK | M_ZERO);
149 priv->uid = p->td_proc->p_ucred->cr_svuid;
150 priv->pid = p->td_proc->p_pid;
152 /* for compatibility root is always authenticated */
153 priv->authenticated = capable(CAP_SYS_ADMIN);
155 INIT_LIST_HEAD(&priv->fbs);
156 INIT_LIST_HEAD(&priv->event_list);
157 init_waitqueue_head(&priv->event_wait);
158 priv->event_space = 4096; /* set aside 4k for event buffer */
160 if (dev->driver->driver_features & DRIVER_GEM)
161 drm_gem_open(dev, priv);
163 if (dev->driver->open) {
164 /* shared code returns -errno */
165 retcode = -dev->driver->open(dev, priv);
167 drm_free(priv, M_DRM);
173 /* first opener automatically becomes master */
174 priv->master = list_empty(&dev->filelist);
176 list_add(&priv->lhead, &dev->filelist);
180 retcode = devfs_set_cdevpriv(fp, priv, &drm_cdevpriv_dtor);
182 drm_cdevpriv_dtor(priv);
190 * \param inode device inode
191 * \param file_priv DRM file private.
192 * \return zero on success or a negative number on failure.
194 * If the hardware lock is held then free it, and take it again for the kernel
195 * context since it's necessary to reclaim buffers. Unlink the file private
196 * data from its list and free it. Decreases the open count and if it reaches
197 * zero calls drm_lastclose().
200 static void drm_unload(struct drm_device *dev)
206 drm_sysctl_cleanup(dev);
207 if (dev->devnode != NULL)
208 destroy_dev(dev->devnode);
210 if (dev->driver->driver_features & DRIVER_GEM)
211 drm_gem_destroy(dev);
213 if (dev->agp && dev->agp->agp_mtrr) {
214 int __unused retcode;
216 retcode = drm_mtrr_del(0, dev->agp->agp_info.ai_aperture_base,
217 dev->agp->agp_info.ai_aperture_size, DRM_MTRR_WC);
218 DRM_DEBUG("mtrr_del = %d", retcode);
221 drm_vblank_cleanup(dev);
227 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
228 * worried about resource consumption while the DRM is inactive (between
229 * lastclose and firstopen or unload) because these aren't actually
230 * taking up KVA, just keeping the PCI resource allocated.
232 for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
233 if (dev->pcir[i] == NULL)
235 bus_release_resource(dev->dev, SYS_RES_MEMORY,
236 dev->pcirid[i], dev->pcir[i]);
241 drm_free(dev->agp, M_DRM);
245 if (dev->driver->unload != NULL) {
247 dev->driver->unload(dev);
253 if (pci_disable_busmaster(dev->dev))
254 DRM_ERROR("Request to disable bus-master failed.\n");
256 lockuninit(&dev->vbl_lock);
257 lockuninit(&dev->dev_lock);
258 lockuninit(&dev->event_lock);
259 lockuninit(&dev->struct_mutex);
262 int drm_release(device_t kdev)
264 struct drm_device *dev;
266 dev = device_get_softc(kdev);
269 bus_release_resource(dev->dev, SYS_RES_IRQ, dev->irqrid,
271 if (dev->irq_type == PCI_INTR_TYPE_MSI) {
272 pci_release_msi(dev->dev);
273 DRM_INFO("MSI released\n");
280 drm_dequeue_event(struct drm_device *dev, struct drm_file *file_priv,
281 struct uio *uio, struct drm_pending_event **out)
283 struct drm_pending_event *e;
285 if (list_empty(&file_priv->event_list))
287 e = list_first_entry(&file_priv->event_list,
288 struct drm_pending_event, link);
289 if (e->event->length > uio->uio_resid)
292 file_priv->event_space += e->event->length;
299 drm_read(struct dev_read_args *ap)
301 struct cdev *kdev = ap->a_head.a_dev;
302 struct uio *uio = ap->a_uio;
303 int ioflag = ap->a_ioflag;
304 struct drm_file *file_priv;
305 struct drm_device *dev;
306 struct drm_pending_event *e;
309 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
311 DRM_ERROR("can't find authenticator\n");
314 dev = drm_get_device_from_kdev(kdev);
315 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
316 while (list_empty(&file_priv->event_list)) {
317 if ((ioflag & O_NONBLOCK) != 0) {
321 error = lksleep(&file_priv->event_space, &dev->event_lock,
322 PCATCH, "drmrea", 0);
326 while (drm_dequeue_event(dev, file_priv, uio, &e)) {
327 lockmgr(&dev->event_lock, LK_RELEASE);
328 error = uiomove((caddr_t)e->event, e->event->length, uio);
332 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
335 lockmgr(&dev->event_lock, LK_RELEASE);
340 drm_event_wakeup(struct drm_pending_event *e)
342 struct drm_file *file_priv;
343 struct drm_device *dev;
345 file_priv = e->file_priv;
346 dev = file_priv->dev;
347 KKASSERT(lockstatus(&dev->event_lock, curthread) != 0);
349 wakeup(&file_priv->event_space);
350 KNOTE(&file_priv->dkq.ki_note, 0);
354 drmfilt(struct knote *kn, long hint)
356 struct drm_file *file_priv;
357 struct drm_device *dev;
360 file_priv = (struct drm_file *)kn->kn_hook;
361 dev = file_priv->dev;
362 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
363 if (!list_empty(&file_priv->event_list))
365 lockmgr(&dev->event_lock, LK_RELEASE);
371 drmfilt_detach(struct knote *kn)
373 struct drm_file *file_priv;
374 struct drm_device *dev;
377 file_priv = (struct drm_file *)kn->kn_hook;
378 dev = file_priv->dev;
380 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
381 klist = &file_priv->dkq.ki_note;
382 knote_remove(klist, kn);
383 lockmgr(&dev->event_lock, LK_RELEASE);
386 static struct filterops drmfiltops =
387 { FILTEROP_ISFD, NULL, drmfilt_detach, drmfilt };
390 drm_kqfilter(struct dev_kqfilter_args *ap)
392 struct cdev *kdev = ap->a_head.a_dev;
393 struct drm_file *file_priv;
394 struct drm_device *dev;
395 struct knote *kn = ap->a_kn;
399 error = devfs_get_cdevpriv(ap->a_fp, (void **)&file_priv);
401 DRM_ERROR("can't find authenticator\n");
404 dev = drm_get_device_from_kdev(kdev);
408 switch (kn->kn_filter) {
411 kn->kn_fop = &drmfiltops;
412 kn->kn_hook = (caddr_t)file_priv;
415 ap->a_result = EOPNOTSUPP;
419 lockmgr(&dev->event_lock, LK_EXCLUSIVE);
420 klist = &file_priv->dkq.ki_note;
421 knote_insert(klist, kn);
422 lockmgr(&dev->event_lock, LK_RELEASE);