]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - linux-core/drm_fops.c
libdrm_radeon: add radeon_bo_is_referenced_by_cs function
[glsdk/libdrm.git] / linux-core / drm_fops.c
1 /**
2  * \file drm_fops.c
3  * File operations for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Daryll Strauss <daryll@valinux.com>
7  * \author Gareth Hughes <gareth@valinux.com>
8  */
10 /*
11  * Created: Mon Jan  4 08:58:31 1999 by faith@valinux.com
12  *
13  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15  * All Rights Reserved.
16  *
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:
23  *
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
26  * Software.
27  *
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.
35  */
37 #include "drmP.h"
38 #include "drm_sarea.h"
39 #include <linux/poll.h>
41 static int drm_open_helper(struct inode *inode, struct file *filp,
42                            struct drm_device * dev);
44 static int drm_setup(struct drm_device * dev)
45 {
46         drm_local_map_t *map;
47         int i;
48         int ret;
49         int sareapage;
51         if (dev->driver->firstopen) {
52                 ret = dev->driver->firstopen(dev);
53                 if (ret != 0)
54                         return ret;
55         }
57         dev->magicfree.next = NULL;
59         /* prebuild the SAREA */
60         sareapage = max(SAREA_MAX, PAGE_SIZE);
61         i = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
62         if (i != 0)
63                 return i;
65         atomic_set(&dev->ioctl_count, 0);
66         atomic_set(&dev->vma_count, 0);
67         dev->buf_use = 0;
68         atomic_set(&dev->buf_alloc, 0);
70         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
71                 i = drm_dma_setup(dev);
72                 if (i < 0)
73                         return i;
74         }
76         for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
77                 atomic_set(&dev->counts[i], 0);
79         drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
80         INIT_LIST_HEAD(&dev->magicfree);
82         dev->sigdata.lock = NULL;
83         init_waitqueue_head(&dev->lock.lock_queue);
84         dev->queue_count = 0;
85         dev->queue_reserved = 0;
86         dev->queue_slots = 0;
87         dev->queuelist = NULL;
88         dev->context_flag = 0;
89         dev->interrupt_flag = 0;
90         dev->dma_flag = 0;
91         dev->last_context = 0;
92         dev->last_switch = 0;
93         dev->last_checked = 0;
94         init_waitqueue_head(&dev->context_wait);
95         dev->if_version = 0;
97         dev->ctx_start = 0;
98         dev->lck_start = 0;
100         dev->buf_async = NULL;
101         init_waitqueue_head(&dev->buf_readers);
102         init_waitqueue_head(&dev->buf_writers);
104         DRM_DEBUG("\n");
106         /*
107          * The kernel's context could be created here, but is now created
108          * in drm_dma_enqueue.  This is more resource-efficient for
109          * hardware that does not do DMA, but may mean that
110          * drm_select_queue fails between the time the interrupt is
111          * initialized and the time the queues are initialized.
112          */
114         return 0;
117 /**
118  * Open file.
119  *
120  * \param inode device inode
121  * \param filp file pointer.
122  * \return zero on success or a negative number on failure.
123  *
124  * Searches the DRM device with the same minor number, calls open_helper(), and
125  * increments the device open count. If the open count was previous at zero,
126  * i.e., it's the first that the device is open, then calls setup().
127  */
128 int drm_open(struct inode *inode, struct file *filp)
130         struct drm_device *dev = NULL;
131         int minor_id = iminor(inode);
132         struct drm_minor *minor;
133         int retcode = 0;
135         minor = idr_find(&drm_minors_idr, minor_id);
136         if (!minor)
137                 return -ENODEV;
139         if (!(dev = minor->dev))
140                 return -ENODEV;
142         retcode = drm_open_helper(inode, filp, dev);
143         if (!retcode) {
144                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
145                 spin_lock(&dev->count_lock);
146                 if (!dev->open_count++) {
147                         spin_unlock(&dev->count_lock);
148                         retcode = drm_setup(dev);
149                         goto out;
150                 }
151                 spin_unlock(&dev->count_lock);
152         }
154 out:
155         return retcode;
157 EXPORT_SYMBOL(drm_open);
159 /**
160  * File \c open operation.
161  *
162  * \param inode device inode.
163  * \param filp file pointer.
164  *
165  * Puts the dev->fops corresponding to the device minor number into
166  * \p filp, call the \c open method, and restore the file operations.
167  */
168 int drm_stub_open(struct inode *inode, struct file *filp)
170         struct drm_device *dev = NULL;
171         struct drm_minor *minor;
172         int minor_id = iminor(inode);
173         int err = -ENODEV;
174         const struct file_operations *old_fops;
176         DRM_DEBUG("\n");
178         minor = idr_find(&drm_minors_idr, minor_id);
179         if (!minor)
180                 return -ENODEV;
181         
182         if (!(dev = minor->dev))
183                 return -ENODEV;
185         old_fops = filp->f_op;
186         filp->f_op = fops_get(&dev->driver->fops);
187         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
188                 fops_put(filp->f_op);
189                 filp->f_op = fops_get(old_fops);
190         }
191         fops_put(old_fops);
193         return err;
196 /**
197  * Check whether DRI will run on this CPU.
198  *
199  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
200  */
201 static int drm_cpu_valid(void)
203 #if defined(__i386__)
204         if (boot_cpu_data.x86 == 3)
205                 return 0;       /* No cmpxchg on a 386 */
206 #endif
207 #if defined(__sparc__) && !defined(__sparc_v9__)
208         return 0;               /* No cmpxchg before v9 sparc. */
209 #endif
210         return 1;
213 /**
214  * Called whenever a process opens /dev/drm.
215  *
216  * \param inode device inode.
217  * \param filp file pointer.
218  * \param dev device.
219  * \return zero on success or a negative number on failure.
220  *
221  * Creates and initializes a drm_file structure for the file private data in \p
222  * filp and add it into the double linked list in \p dev.
223  */
224 static int drm_open_helper(struct inode *inode, struct file *filp,
225                            struct drm_device * dev)
227         int minor_id = iminor(inode);
228         struct drm_file *priv;
229         int ret;
230         int i, j;
232         if (filp->f_flags & O_EXCL)
233                 return -EBUSY;  /* No exclusive opens */
234         if (!drm_cpu_valid())
235                 return -EINVAL;
237         DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor_id);
239         priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
240         if (!priv)
241                 return -ENOMEM;
243         memset(priv, 0, sizeof(*priv));
244         filp->private_data = priv;
245         priv->filp = filp;
246         priv->uid = current_euid();
247         priv->pid = current->pid;
248         priv->minor = idr_find(&drm_minors_idr, minor_id);
249         priv->ioctl_count = 0;
250         /* for compatibility root is always authenticated */
251         priv->authenticated = capable(CAP_SYS_ADMIN);
252         priv->lock_count = 0;
254         INIT_LIST_HEAD(&priv->lhead);
255         INIT_LIST_HEAD(&priv->refd_objects);
257         for (i = 0; i < _DRM_NO_REF_TYPES; ++i) {
258                 ret = drm_ht_create(&priv->refd_object_hash[i],
259                                     DRM_FILE_HASH_ORDER);
260                 if (ret)
261                         break;
262         }
264         if (ret) {
265                 for (j = 0; j < i; ++j)
266                         drm_ht_remove(&priv->refd_object_hash[j]);
267                 goto out_free;
268         }
270         if (dev->driver->driver_features & DRIVER_GEM)
271                 drm_gem_open(dev, priv);
273         if (dev->driver->open) {
274                 ret = dev->driver->open(dev, priv);
275                 if (ret < 0)
276                         goto out_free;
277         }
279         mutex_lock(&dev->struct_mutex);
280         if (list_empty(&dev->filelist))
281                 priv->master = 1;
283         list_add(&priv->lhead, &dev->filelist);
284         mutex_unlock(&dev->struct_mutex);
286 #ifdef __alpha__
287         /*
288          * Default the hose
289          */
290         if (!dev->hose) {
291                 struct pci_dev *pci_dev;
292                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
293                 if (pci_dev) {
294                         dev->hose = pci_dev->sysdata;
295                         pci_dev_put(pci_dev);
296                 }
297                 if (!dev->hose) {
298                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
299                         if (b)
300                                 dev->hose = b->sysdata;
301                 }
302         }
303 #endif
305         return 0;
306       out_free:
307         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
308         filp->private_data = NULL;
309         return ret;
312 /** No-op. */
313 int drm_fasync(int fd, struct file *filp, int on)
315         struct drm_file *priv = filp->private_data;
316         struct drm_device *dev = priv->minor->dev;
317         int retcode;
319         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
320                   (long)old_encode_dev(priv->minor->device));
321         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
322         if (retcode < 0)
323                 return retcode;
324         return 0;
326 EXPORT_SYMBOL(drm_fasync);
328 /**
329  * Release file.
330  *
331  * \param inode device inode
332  * \param file_priv DRM file private.
333  * \return zero on success or a negative number on failure.
334  *
335  * If the hardware lock is held then free it, and take it again for the kernel
336  * context since it's necessary to reclaim buffers. Unlink the file private
337  * data from its list and free it. Decreases the open count and if it reaches
338  * zero calls drm_lastclose().
339  */
340 int drm_release(struct inode *inode, struct file *filp)
342         struct drm_file *file_priv = filp->private_data;
343         struct drm_device *dev = file_priv->minor->dev;
344         int retcode = 0;
346         lock_kernel();
348         DRM_DEBUG("open_count = %d\n", dev->open_count);
350         if (dev->driver->preclose)
351                 dev->driver->preclose(dev, file_priv);
353         /* ========================================================
354          * Begin inline drm_release
355          */
357         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
358                   current->pid, (long)old_encode_dev(file_priv->minor->device),
359                   dev->open_count);
361         if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
362                 if (drm_i_have_hw_lock(dev, file_priv)) {
363                         dev->driver->reclaim_buffers_locked(dev, file_priv);
364                 } else {
365                         unsigned long _end=jiffies + 3*DRM_HZ;
366                         int locked = 0;
368                         drm_idlelock_take(&dev->lock);
370                         /*
371                          * Wait for a while.
372                          */
374                         do{
375                                 spin_lock_bh(&dev->lock.spinlock);
376                                 locked = dev->lock.idle_has_lock;
377                                 spin_unlock_bh(&dev->lock.spinlock);
378                                 if (locked)
379                                         break;
380                                 schedule();
381                         } while (!time_after_eq(jiffies, _end));
383                         if (!locked) {
384                                 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
385                                           "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
386                                           "\tI will go on reclaiming the buffers anyway.\n");
387                         }
389                         dev->driver->reclaim_buffers_locked(dev, file_priv);
390                         drm_idlelock_release(&dev->lock);
391                 }
392         }
394         if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
396                 drm_idlelock_take(&dev->lock);
397                 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
398                 drm_idlelock_release(&dev->lock);
400         }
402         if (drm_i_have_hw_lock(dev, file_priv)) {
403                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
404                           filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
406                 drm_lock_free(&dev->lock,
407                               _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
408         }
411         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
412             !dev->driver->reclaim_buffers_locked) {
413                 dev->driver->reclaim_buffers(dev, file_priv);
414         }
416         if (dev->driver->driver_features & DRIVER_GEM)
417                 drm_gem_release(dev, file_priv);
419         drm_fasync(-1, filp, 0);
421         mutex_lock(&dev->ctxlist_mutex);
423         if (!list_empty(&dev->ctxlist)) {
424                 struct drm_ctx_list *pos, *n;
426                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
427                         if (pos->tag == file_priv &&
428                             pos->handle != DRM_KERNEL_CONTEXT) {
429                                 if (dev->driver->context_dtor)
430                                         dev->driver->context_dtor(dev,
431                                                                   pos->handle);
433                                 drm_ctxbitmap_free(dev, pos->handle);
435                                 list_del(&pos->head);
436                                 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
437                                 --dev->ctx_count;
438                         }
439                 }
440         }
441         mutex_unlock(&dev->ctxlist_mutex);
443         mutex_lock(&dev->struct_mutex);
444         if (file_priv->remove_auth_on_close == 1) {
445                 struct drm_file *temp;
447                 list_for_each_entry(temp, &dev->filelist, lhead)
448                         temp->authenticated = 0;
449         }
450         list_del(&file_priv->lhead);
451         mutex_unlock(&dev->struct_mutex);
453         if (dev->driver->postclose)
454                 dev->driver->postclose(dev, file_priv);
455         drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
457         /* ========================================================
458          * End inline drm_release
459          */
461         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
462         spin_lock(&dev->count_lock);
463         if (!--dev->open_count) {
464                 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
465                         DRM_ERROR("Device busy: %d %d\n",
466                                   atomic_read(&dev->ioctl_count), dev->blocked);
467                         spin_unlock(&dev->count_lock);
468                         unlock_kernel();
469                         return -EBUSY;
470                 }
471                 spin_unlock(&dev->count_lock);
472                 unlock_kernel();
473                 return drm_lastclose(dev);
474         }
475         spin_unlock(&dev->count_lock);
477         unlock_kernel();
479         return retcode;
481 EXPORT_SYMBOL(drm_release);
483 /** No-op. */
484 /* This is to deal with older X servers that believe 0 means data is
485  * available which is not the correct return for a poll function.
486  * This cannot be fixed until the Xserver is fixed. Xserver will need
487  * to set a newer interface version to avoid breaking older Xservers.
488  * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
489  * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
490  * to return the correct response.
491  */
492 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
494         /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
495         return 0;
497 EXPORT_SYMBOL(drm_poll);