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.
38 #include <linux/poll.h>
40 static int drm_setup(drm_device_t * dev)
44 if (dev->fn_tbl->presetup)
45 dev->fn_tbl->presetup(dev);
47 atomic_set(&dev->ioctl_count, 0);
48 atomic_set(&dev->vma_count, 0);
50 atomic_set(&dev->buf_alloc, 0);
52 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
53 i = drm_dma_setup(dev);
58 for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
59 atomic_set(&dev->counts[i], 0);
61 for (i = 0; i < DRM_HASH_SIZE; i++) {
62 dev->magiclist[i].head = NULL;
63 dev->magiclist[i].tail = NULL;
66 dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist), DRM_MEM_CTXLIST);
67 if (dev->ctxlist == NULL)
69 memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
70 INIT_LIST_HEAD(&dev->ctxlist->head);
73 dev->sigdata.lock = dev->lock.hw_lock = NULL;
74 init_waitqueue_head(&dev->lock.lock_queue);
76 dev->queue_reserved = 0;
78 dev->queuelist = NULL;
80 dev->context_flag = 0;
81 dev->interrupt_flag = 0;
83 dev->last_context = 0;
85 dev->last_checked = 0;
86 init_waitqueue_head(&dev->context_wait);
92 dev->buf_rp = dev->buf;
93 dev->buf_wp = dev->buf;
94 dev->buf_end = dev->buf + DRM_BSZ;
95 dev->buf_async = NULL;
96 init_waitqueue_head(&dev->buf_readers);
97 init_waitqueue_head(&dev->buf_writers);
102 * The kernel's context could be created here, but is now created
103 * in drm_dma_enqueue. This is more resource-efficient for
104 * hardware that does not do DMA, but may mean that
105 * drm_select_queue fails between the time the interrupt is
106 * initialized and the time the queues are initialized.
108 if (dev->fn_tbl->postsetup)
109 dev->fn_tbl->postsetup(dev);
117 * \param inode device inode
118 * \param filp file pointer.
119 * \return zero on success or a negative number on failure.
121 * Searches the DRM device with the same minor number, calls open_helper(), and
122 * increments the device open count. If the open count was previous at zero,
123 * i.e., it's the first that the device is open, then calls setup().
125 int drm_open(struct inode *inode, struct file *filp)
127 drm_device_t *dev = NULL;
128 int minor = iminor(inode);
131 if (!((minor >= 0) && (minor < cards_limit)))
134 dev = drm_minors[minor].dev;
138 retcode = drm_open_helper(inode, filp, dev);
140 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
141 spin_lock(&dev->count_lock);
142 if (!dev->open_count++) {
143 spin_unlock(&dev->count_lock);
144 return drm_setup(dev);
146 spin_unlock(&dev->count_lock);
151 EXPORT_SYMBOL(drm_open);
154 * File \c open operation.
156 * \param inode device inode.
157 * \param filp file pointer.
159 * Puts the dev->fops corresponding to the device minor number into
160 * \p filp, call the \c open method, and restore the file operations.
162 int drm_stub_open(struct inode *inode, struct file *filp)
164 drm_device_t *dev = NULL;
165 int minor = iminor(inode);
167 struct file_operations *old_fops;
171 if (!((minor >= 0) && (minor < cards_limit)))
174 dev = drm_minors[minor].dev;
178 old_fops = filp->f_op;
179 filp->f_op = fops_get(&dev->fn_tbl->fops);
180 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
181 fops_put(filp->f_op);
182 filp->f_op = fops_get(old_fops);
190 * Called whenever a process opens /dev/drm.
192 * \param inode device inode.
193 * \param filp file pointer.
195 * \return zero on success or a negative number on failure.
197 * Creates and initializes a drm_file structure for the file private data in \p
198 * filp and add it into the double linked list in \p dev.
200 int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t * dev)
202 int minor = iminor(inode);
206 if (filp->f_flags & O_EXCL)
207 return -EBUSY; /* No exclusive opens */
208 if (!drm_cpu_valid())
211 DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
213 priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
217 memset(priv, 0, sizeof(*priv));
218 filp->private_data = priv;
219 priv->uid = current->euid;
220 priv->pid = current->pid;
223 priv->ioctl_count = 0;
224 priv->authenticated = capable(CAP_SYS_ADMIN);
225 priv->lock_count = 0;
227 if (dev->fn_tbl->open_helper) {
228 ret = dev->fn_tbl->open_helper(dev, priv);
233 down(&dev->struct_sem);
234 if (!dev->file_last) {
237 dev->file_first = priv;
238 dev->file_last = priv;
241 priv->prev = dev->file_last;
242 dev->file_last->next = priv;
243 dev->file_last = priv;
245 up(&dev->struct_sem);
252 struct pci_dev *pci_dev;
253 pci_dev = pci_find_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
255 dev->hose = pci_dev->sysdata;
257 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
259 dev->hose = b->sysdata;
266 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
267 filp->private_data = NULL;
272 int drm_fasync(int fd, struct file *filp, int on)
274 drm_file_t *priv = filp->private_data;
275 drm_device_t *dev = priv->dev;
278 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
279 (long)old_encode_dev(dev->device));
280 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
285 EXPORT_SYMBOL(drm_fasync);
290 * \param inode device inode
291 * \param filp file pointer.
292 * \return zero on success or a negative number on failure.
294 * If the hardware lock is held then free it, and take it again for the kernel
295 * context since it's necessary to reclaim buffers. Unlink the file private
296 * data from its list and free it. Decreases the open count and if it reaches
297 * zero calls takedown().
299 int drm_release(struct inode *inode, struct file *filp)
301 drm_file_t *priv = filp->private_data;
308 DRM_DEBUG("open_count = %d\n", dev->open_count);
310 if (dev->fn_tbl->prerelease)
311 dev->fn_tbl->prerelease(dev, filp);
313 /* ========================================================
314 * Begin inline drm_release
317 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
318 current->pid, (long)old_encode_dev(dev->device),
321 if (priv->lock_count && dev->lock.hw_lock &&
322 _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
323 dev->lock.filp == filp) {
324 DRM_DEBUG("File %p released, freeing lock for context %d\n",
325 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
327 if (dev->fn_tbl->release)
328 dev->fn_tbl->release(dev, filp);
330 drm_lock_free(dev, &dev->lock.hw_lock->lock,
331 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
333 /* FIXME: may require heavy-handed reset of
334 hardware at this point, possibly
335 processed via a callback to the X
337 } else if (dev->fn_tbl->release && priv->lock_count
338 && dev->lock.hw_lock) {
339 /* The lock is required to reclaim buffers */
340 DECLARE_WAITQUEUE(entry, current);
342 add_wait_queue(&dev->lock.lock_queue, &entry);
344 current->state = TASK_INTERRUPTIBLE;
345 if (!dev->lock.hw_lock) {
346 /* Device has been unregistered */
350 if (drm_lock_take(&dev->lock.hw_lock->lock,
351 DRM_KERNEL_CONTEXT)) {
352 dev->lock.filp = filp;
353 dev->lock.lock_time = jiffies;
354 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
355 break; /* Got lock */
359 if (signal_pending(current)) {
360 retcode = -ERESTARTSYS;
364 current->state = TASK_RUNNING;
365 remove_wait_queue(&dev->lock.lock_queue, &entry);
367 if (dev->fn_tbl->release)
368 dev->fn_tbl->release(dev, filp);
369 drm_lock_free(dev, &dev->lock.hw_lock->lock,
374 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
375 dev->fn_tbl->reclaim_buffers(filp);
378 drm_fasync(-1, filp, 0);
380 down(&dev->ctxlist_sem);
381 if (!list_empty(&dev->ctxlist->head)) {
382 drm_ctx_list_t *pos, *n;
384 list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
385 if (pos->tag == priv &&
386 pos->handle != DRM_KERNEL_CONTEXT) {
387 if (dev->fn_tbl->context_dtor)
388 dev->fn_tbl->context_dtor(dev,
391 drm_ctxbitmap_free(dev, pos->handle);
393 list_del(&pos->head);
394 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
399 up(&dev->ctxlist_sem);
401 down(&dev->struct_sem);
402 if (priv->remove_auth_on_close == 1) {
403 drm_file_t *temp = dev->file_first;
405 temp->authenticated = 0;
410 priv->prev->next = priv->next;
412 dev->file_first = priv->next;
415 priv->next->prev = priv->prev;
417 dev->file_last = priv->prev;
419 up(&dev->struct_sem);
421 if (dev->fn_tbl->free_filp_priv)
422 dev->fn_tbl->free_filp_priv(dev, priv);
423 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
425 /* ========================================================
426 * End inline drm_release
429 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
430 spin_lock(&dev->count_lock);
431 if (!--dev->open_count) {
432 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
433 DRM_ERROR("Device busy: %d %d\n",
434 atomic_read(&dev->ioctl_count), dev->blocked);
435 spin_unlock(&dev->count_lock);
439 spin_unlock(&dev->count_lock);
441 return drm_takedown(dev);
443 spin_unlock(&dev->count_lock);
449 EXPORT_SYMBOL(drm_release);