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: note that nothing else serializes idr_find() */
47 DEFINE_MUTEX(drm_global_mutex
);
48 EXPORT_SYMBOL(drm_global_mutex
);
50 extern drm_pci_id_list_t
*drm_find_description(int vendor
, int device
,
51 drm_pci_id_list_t
*idlist
);
52 extern devclass_t drm_devclass
;
54 static int drm_setup(struct drm_device
*dev
)
61 /* prebuild the SAREA */
62 i
= drm_legacy_addmap(dev
, 0, SAREA_MAX
, _DRM_SHM
,
63 _DRM_CONTAINS_LOCK
, &map
);
67 if (dev
->driver
->firstopen
)
68 dev
->driver
->firstopen(dev
);
72 if (drm_core_check_feature(dev
, DRIVER_HAVE_DMA
)) {
73 i
= drm_legacy_dma_setup(dev
);
78 drm_ht_create(&dev
->magiclist
, DRM_MAGIC_HASH_ORDER
);
79 INIT_LIST_HEAD(&dev
->magicfree
);
81 init_waitqueue_head(&dev
->lock
.lock_queue
);
82 if (!drm_core_check_feature(dev
, DRIVER_MODESET
))
84 dev
->context_flag
= 0;
85 dev
->last_context
= 0;
88 dev
->buf_sigio
= NULL
;
95 #define DRIVER_SOFTC(unit) \
96 ((struct drm_device *)devclass_get_softc(drm_devclass, unit))
99 drm_open(struct dev_open_args
*ap
)
101 struct cdev
*kdev
= ap
->a_head
.a_dev
;
102 int flags
= ap
->a_oflags
;
104 struct thread
*p
= curthread
;
105 struct drm_device
*dev
;
108 dev
= DRIVER_SOFTC(minor(kdev
));
112 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
114 retcode
= drm_open_helper(kdev
, flags
, fmt
, p
, dev
, ap
->a_fp
);
117 atomic_inc(&dev
->counts
[_DRM_STAT_OPENS
]);
119 device_busy(dev
->dev
->bsddev
);
120 if (!dev
->open_count
++)
121 retcode
= drm_setup(dev
);
125 DRM_DEBUG("return %d\n", retcode
);
130 /* drm_open_helper is called whenever a process opens /dev/drm. */
131 int drm_open_helper(struct cdev
*kdev
, int flags
, int fmt
, DRM_STRUCTPROC
*p
,
132 struct drm_device
*dev
, struct file
*fp
)
134 struct drm_file
*priv
;
138 return EBUSY
; /* No exclusive opens */
141 DRM_DEBUG("pid = %d, device = %s\n", DRM_CURRENTPID
, devtoname(kdev
));
143 priv
= kmalloc(sizeof(*priv
), M_DRM
, M_WAITOK
| M_NULLOK
| M_ZERO
);
150 priv
->uid
= p
->td_proc
->p_ucred
->cr_svuid
;
151 priv
->pid
= p
->td_proc
->p_pid
;
153 /* for compatibility root is always authenticated */
154 priv
->authenticated
= capable(CAP_SYS_ADMIN
);
156 INIT_LIST_HEAD(&priv
->fbs
);
157 INIT_LIST_HEAD(&priv
->event_list
);
158 init_waitqueue_head(&priv
->event_wait
);
159 priv
->event_space
= 4096; /* set aside 4k for event buffer */
161 if (drm_core_check_feature(dev
, DRIVER_GEM
))
162 drm_gem_open(dev
, priv
);
164 if (dev
->driver
->open
) {
165 /* shared code returns -errno */
166 retcode
= -dev
->driver
->open(dev
, priv
);
174 /* first opener automatically becomes master */
175 priv
->master
= list_empty(&dev
->filelist
);
177 list_add(&priv
->lhead
, &dev
->filelist
);
181 retcode
= devfs_set_cdevpriv(fp
, priv
, &drm_cdevpriv_dtor
);
183 drm_cdevpriv_dtor(priv
);
189 * Take down the DRM device.
191 * \param dev DRM device structure.
193 * Frees every resource in \p dev.
197 int drm_lastclose(struct drm_device
* dev
)
201 if (dev
->driver
->lastclose
)
202 dev
->driver
->lastclose(dev
);
203 DRM_DEBUG("driver lastclose completed\n");
205 if (dev
->irq_enabled
&& !drm_core_check_feature(dev
, DRIVER_MODESET
))
206 drm_irq_uninstall(dev
);
208 mutex_lock(&dev
->struct_mutex
);
218 drm_legacy_sg_cleanup(dev
);
219 drm_legacy_dma_takedown(dev
);
221 if (dev
->lock
.hw_lock
) {
222 dev
->lock
.hw_lock
= NULL
; /* SHM removed */
223 dev
->lock
.file_priv
= NULL
;
224 wakeup(&dev
->lock
.lock_queue
);
227 mutex_unlock(&dev
->struct_mutex
);
229 DRM_DEBUG("lastclose completed\n");
236 * \param inode device inode
237 * \param file_priv DRM file private.
238 * \return zero on success or a negative number on failure.
240 * If the hardware lock is held then free it, and take it again for the kernel
241 * context since it's necessary to reclaim buffers. Unlink the file private
242 * data from its list and free it. Decreases the open count and if it reaches
243 * zero calls drm_lastclose().
245 int drm_release(device_t kdev
)
247 struct drm_device
*dev
= device_get_softc(kdev
);
248 struct drm_magic_entry
*pt
, *next
;
251 mutex_lock(&drm_global_mutex
);
254 if (dev
->magicfree
.next
) {
255 list_for_each_entry_safe(pt
, next
, &dev
->magicfree
, head
) {
257 drm_ht_remove_item(&dev
->magiclist
, &pt
->hash_item
);
260 drm_ht_remove(&dev
->magiclist
);
263 /* ========================================================
264 * Begin inline drm_release
269 drm_sysctl_cleanup(dev
);
270 if (dev
->devnode
!= NULL
)
271 destroy_dev(dev
->devnode
);
273 if (drm_core_check_feature(dev
, DRIVER_GEM
))
274 drm_gem_destroy(dev
);
276 drm_vblank_cleanup(dev
);
282 /* Clean up PCI resources allocated by drm_bufs.c. We're not really
283 * worried about resource consumption while the DRM is inactive (between
284 * lastclose and firstopen or unload) because these aren't actually
285 * taking up KVA, just keeping the PCI resource allocated.
287 for (i
= 0; i
< DRM_MAX_PCI_RESOURCE
; i
++) {
288 if (dev
->pcir
[i
] == NULL
)
290 bus_release_resource(dev
->dev
->bsddev
, SYS_RES_MEMORY
,
291 dev
->pcirid
[i
], dev
->pcir
[i
]);
300 if (dev
->driver
->unload
!= NULL
) {
302 dev
->driver
->unload(dev
);
306 if (pci_disable_busmaster(dev
->dev
->bsddev
))
307 DRM_ERROR("Request to disable bus-master failed.\n");
309 lockuninit(&dev
->vbl_lock
);
310 lockuninit(&dev
->dev_lock
);
311 lockuninit(&dev
->event_lock
);
312 lockuninit(&dev
->struct_mutex
);
314 /* ========================================================
315 * End inline drm_release
319 bus_release_resource(dev
->dev
->bsddev
, SYS_RES_IRQ
, dev
->irqrid
,
321 if (dev
->irq_type
== PCI_INTR_TYPE_MSI
) {
322 pci_release_msi(dev
->dev
->bsddev
);
323 DRM_INFO("MSI released\n");
327 mutex_unlock(&drm_global_mutex
);
333 drm_dequeue_event(struct drm_device
*dev
, struct drm_file
*file_priv
,
334 struct uio
*uio
, struct drm_pending_event
**out
)
336 struct drm_pending_event
*e
;
339 lockmgr(&dev
->event_lock
, LK_EXCLUSIVE
);
342 if (list_empty(&file_priv
->event_list
))
344 e
= list_first_entry(&file_priv
->event_list
,
345 struct drm_pending_event
, link
);
346 if (e
->event
->length
> uio
->uio_resid
)
349 file_priv
->event_space
+= e
->event
->length
;
355 lockmgr(&dev
->event_lock
, LK_RELEASE
);
360 drm_read(struct dev_read_args
*ap
)
362 struct cdev
*kdev
= ap
->a_head
.a_dev
;
363 struct uio
*uio
= ap
->a_uio
;
364 struct drm_file
*file_priv
;
365 struct drm_device
*dev
= drm_get_device_from_kdev(kdev
);
366 struct drm_pending_event
*e
;
370 error
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&file_priv
);
372 DRM_ERROR("can't find authenticator\n");
376 ret
= wait_event_interruptible(file_priv
->event_wait
,
377 !list_empty(&file_priv
->event_list
));
378 if (ret
== -ERESTARTSYS
)
383 while (drm_dequeue_event(dev
, file_priv
, uio
, &e
)) {
384 error
= uiomove((caddr_t
)e
->event
, e
->event
->length
, uio
);
394 drmfilt(struct knote
*kn
, long hint
)
396 struct drm_file
*file_priv
;
397 struct drm_device
*dev
;
400 file_priv
= (struct drm_file
*)kn
->kn_hook
;
401 dev
= file_priv
->dev
;
402 lockmgr(&dev
->event_lock
, LK_EXCLUSIVE
);
403 if (!list_empty(&file_priv
->event_list
))
405 lockmgr(&dev
->event_lock
, LK_RELEASE
);
411 drmfilt_detach(struct knote
*kn
)
413 struct drm_file
*file_priv
;
414 struct drm_device
*dev
;
417 file_priv
= (struct drm_file
*)kn
->kn_hook
;
418 dev
= file_priv
->dev
;
420 klist
= &file_priv
->dkq
.ki_note
;
421 knote_remove(klist
, kn
);
424 static struct filterops drmfiltops
=
425 { FILTEROP_MPSAFE
| FILTEROP_ISFD
, NULL
, drmfilt_detach
, drmfilt
};
428 drm_kqfilter(struct dev_kqfilter_args
*ap
)
430 struct cdev
*kdev
= ap
->a_head
.a_dev
;
431 struct drm_file
*file_priv
;
432 struct drm_device
*dev
;
433 struct knote
*kn
= ap
->a_kn
;
437 error
= devfs_get_cdevpriv(ap
->a_fp
, (void **)&file_priv
);
439 DRM_ERROR("can't find authenticator\n");
442 dev
= drm_get_device_from_kdev(kdev
);
446 switch (kn
->kn_filter
) {
449 kn
->kn_fop
= &drmfiltops
;
450 kn
->kn_hook
= (caddr_t
)file_priv
;
453 ap
->a_result
= EOPNOTSUPP
;
457 klist
= &file_priv
->dkq
.ki_note
;
458 knote_insert(klist
, kn
);