Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / char / drm / drm_fops.c
blob1a920e9560bf1494efe151a71c30a903e6d39fa7
1 /**
2 * \file drm_fops.c
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>
8 */
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
26 * Software.
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 "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)
46 drm_local_map_t *map;
47 int i;
48 int ret;
49 u32 sareapage;
51 if (dev->driver->firstopen) {
52 ret = dev->driver->firstopen(dev);
53 if (ret != 0)
54 return ret;
57 dev->magicfree.next = NULL;
59 /* prebuild the SAREA */
60 sareapage = max_t(unsigned, 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;
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->irq_enabled = 0;
89 dev->context_flag = 0;
90 dev->interrupt_flag = 0;
91 dev->dma_flag = 0;
92 dev->last_context = 0;
93 dev->last_switch = 0;
94 dev->last_checked = 0;
95 init_waitqueue_head(&dev->context_wait);
96 dev->if_version = 0;
98 dev->ctx_start = 0;
99 dev->lck_start = 0;
101 dev->buf_async = NULL;
102 init_waitqueue_head(&dev->buf_readers);
103 init_waitqueue_head(&dev->buf_writers);
105 DRM_DEBUG("\n");
108 * The kernel's context could be created here, but is now created
109 * in drm_dma_enqueue. This is more resource-efficient for
110 * hardware that does not do DMA, but may mean that
111 * drm_select_queue fails between the time the interrupt is
112 * initialized and the time the queues are initialized.
115 return 0;
119 * Open file.
121 * \param inode device inode
122 * \param filp file pointer.
123 * \return zero on success or a negative number on failure.
125 * Searches the DRM device with the same minor number, calls open_helper(), and
126 * increments the device open count. If the open count was previous at zero,
127 * i.e., it's the first that the device is open, then calls setup().
129 int drm_open(struct inode *inode, struct file *filp)
131 struct drm_device *dev = NULL;
132 int minor = iminor(inode);
133 int retcode = 0;
135 if (!((minor >= 0) && (minor < drm_cards_limit)))
136 return -ENODEV;
138 if (!drm_heads[minor])
139 return -ENODEV;
141 if (!(dev = drm_heads[minor]->dev))
142 return -ENODEV;
144 retcode = drm_open_helper(inode, filp, dev);
145 if (!retcode) {
146 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
147 spin_lock(&dev->count_lock);
148 if (!dev->open_count++) {
149 spin_unlock(&dev->count_lock);
150 return drm_setup(dev);
152 spin_unlock(&dev->count_lock);
155 return retcode;
157 EXPORT_SYMBOL(drm_open);
160 * File \c open operation.
162 * \param inode device inode.
163 * \param filp file pointer.
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.
168 int drm_stub_open(struct inode *inode, struct file *filp)
170 struct drm_device *dev = NULL;
171 int minor = iminor(inode);
172 int err = -ENODEV;
173 const struct file_operations *old_fops;
175 DRM_DEBUG("\n");
177 if (!((minor >= 0) && (minor < drm_cards_limit)))
178 return -ENODEV;
180 if (!drm_heads[minor])
181 return -ENODEV;
183 if (!(dev = drm_heads[minor]->dev))
184 return -ENODEV;
186 old_fops = filp->f_op;
187 filp->f_op = fops_get(&dev->driver->fops);
188 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
189 fops_put(filp->f_op);
190 filp->f_op = fops_get(old_fops);
192 fops_put(old_fops);
194 return err;
198 * Check whether DRI will run on this CPU.
200 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
202 static int drm_cpu_valid(void)
204 #if defined(__i386__)
205 if (boot_cpu_data.x86 == 3)
206 return 0; /* No cmpxchg on a 386 */
207 #endif
208 #if defined(__sparc__) && !defined(__sparc_v9__)
209 return 0; /* No cmpxchg before v9 sparc. */
210 #endif
211 return 1;
215 * Called whenever a process opens /dev/drm.
217 * \param inode device inode.
218 * \param filp file pointer.
219 * \param dev device.
220 * \return zero on success or a negative number on failure.
222 * Creates and initializes a drm_file structure for the file private data in \p
223 * filp and add it into the double linked list in \p dev.
225 static int drm_open_helper(struct inode *inode, struct file *filp,
226 struct drm_device * dev)
228 int minor = iminor(inode);
229 struct drm_file *priv;
230 int ret;
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", task_pid_nr(current), minor);
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 = task_pid_nr(current);
248 priv->minor = minor;
249 priv->head = drm_heads[minor];
250 priv->ioctl_count = 0;
251 /* for compatibility root is always authenticated */
252 priv->authenticated = capable(CAP_SYS_ADMIN);
253 priv->lock_count = 0;
255 INIT_LIST_HEAD(&priv->lhead);
257 if (dev->driver->open) {
258 ret = dev->driver->open(dev, priv);
259 if (ret < 0)
260 goto out_free;
263 mutex_lock(&dev->struct_mutex);
264 if (list_empty(&dev->filelist))
265 priv->master = 1;
267 list_add(&priv->lhead, &dev->filelist);
268 mutex_unlock(&dev->struct_mutex);
270 #ifdef __alpha__
272 * Default the hose
274 if (!dev->hose) {
275 struct pci_dev *pci_dev;
276 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
277 if (pci_dev) {
278 dev->hose = pci_dev->sysdata;
279 pci_dev_put(pci_dev);
281 if (!dev->hose) {
282 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
283 if (b)
284 dev->hose = b->sysdata;
287 #endif
289 return 0;
290 out_free:
291 drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
292 filp->private_data = NULL;
293 return ret;
296 /** No-op. */
297 int drm_fasync(int fd, struct file *filp, int on)
299 struct drm_file *priv = filp->private_data;
300 struct drm_device *dev = priv->head->dev;
301 int retcode;
303 DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
304 (long)old_encode_dev(priv->head->device));
305 retcode = fasync_helper(fd, filp, on, &dev->buf_async);
306 if (retcode < 0)
307 return retcode;
308 return 0;
310 EXPORT_SYMBOL(drm_fasync);
313 * Release file.
315 * \param inode device inode
316 * \param file_priv DRM file private.
317 * \return zero on success or a negative number on failure.
319 * If the hardware lock is held then free it, and take it again for the kernel
320 * context since it's necessary to reclaim buffers. Unlink the file private
321 * data from its list and free it. Decreases the open count and if it reaches
322 * zero calls drm_lastclose().
324 int drm_release(struct inode *inode, struct file *filp)
326 struct drm_file *file_priv = filp->private_data;
327 struct drm_device *dev = file_priv->head->dev;
328 int retcode = 0;
329 <<<<<<< HEAD:drivers/char/drm/drm_fops.c
330 =======
331 unsigned long irqflags;
332 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/drm/drm_fops.c
334 lock_kernel();
336 DRM_DEBUG("open_count = %d\n", dev->open_count);
338 if (dev->driver->preclose)
339 dev->driver->preclose(dev, file_priv);
341 /* ========================================================
342 * Begin inline drm_release
345 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
346 task_pid_nr(current),
347 (long)old_encode_dev(file_priv->head->device),
348 dev->open_count);
350 if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
351 if (drm_i_have_hw_lock(dev, file_priv)) {
352 dev->driver->reclaim_buffers_locked(dev, file_priv);
353 } else {
354 unsigned long _end=jiffies + 3*DRM_HZ;
355 int locked = 0;
357 drm_idlelock_take(&dev->lock);
360 * Wait for a while.
364 <<<<<<< HEAD:drivers/char/drm/drm_fops.c
365 spin_lock(&dev->lock.spinlock);
366 =======
367 spin_lock_irqsave(&dev->lock.spinlock,
368 irqflags);
369 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/drm/drm_fops.c
370 locked = dev->lock.idle_has_lock;
371 <<<<<<< HEAD:drivers/char/drm/drm_fops.c
372 spin_unlock(&dev->lock.spinlock);
373 =======
374 spin_unlock_irqrestore(&dev->lock.spinlock,
375 irqflags);
376 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/char/drm/drm_fops.c
377 if (locked)
378 break;
379 schedule();
380 } while (!time_after_eq(jiffies, _end));
382 if (!locked) {
383 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
384 "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
385 "\tI will go on reclaiming the buffers anyway.\n");
388 dev->driver->reclaim_buffers_locked(dev, file_priv);
389 drm_idlelock_release(&dev->lock);
393 if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
395 drm_idlelock_take(&dev->lock);
396 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
397 drm_idlelock_release(&dev->lock);
401 if (drm_i_have_hw_lock(dev, file_priv)) {
402 DRM_DEBUG("File %p released, freeing lock for context %d\n",
403 filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
405 drm_lock_free(&dev->lock,
406 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
410 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
411 !dev->driver->reclaim_buffers_locked) {
412 dev->driver->reclaim_buffers(dev, file_priv);
415 drm_fasync(-1, filp, 0);
417 mutex_lock(&dev->ctxlist_mutex);
418 if (!list_empty(&dev->ctxlist)) {
419 struct drm_ctx_list *pos, *n;
421 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
422 if (pos->tag == file_priv &&
423 pos->handle != DRM_KERNEL_CONTEXT) {
424 if (dev->driver->context_dtor)
425 dev->driver->context_dtor(dev,
426 pos->handle);
428 drm_ctxbitmap_free(dev, pos->handle);
430 list_del(&pos->head);
431 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
432 --dev->ctx_count;
436 mutex_unlock(&dev->ctxlist_mutex);
438 mutex_lock(&dev->struct_mutex);
439 if (file_priv->remove_auth_on_close == 1) {
440 struct drm_file *temp;
442 list_for_each_entry(temp, &dev->filelist, lhead)
443 temp->authenticated = 0;
445 list_del(&file_priv->lhead);
446 mutex_unlock(&dev->struct_mutex);
448 if (dev->driver->postclose)
449 dev->driver->postclose(dev, file_priv);
450 drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
452 /* ========================================================
453 * End inline drm_release
456 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
457 spin_lock(&dev->count_lock);
458 if (!--dev->open_count) {
459 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
460 DRM_ERROR("Device busy: %d %d\n",
461 atomic_read(&dev->ioctl_count), dev->blocked);
462 spin_unlock(&dev->count_lock);
463 unlock_kernel();
464 return -EBUSY;
466 spin_unlock(&dev->count_lock);
467 unlock_kernel();
468 return drm_lastclose(dev);
470 spin_unlock(&dev->count_lock);
472 unlock_kernel();
474 return retcode;
476 EXPORT_SYMBOL(drm_release);
478 /** No-op. */
479 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
481 return 0;
483 EXPORT_SYMBOL(drm_poll);