Add a comment to previos commit.
[platform/upstream/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  */
9
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  */
36
37 #include "drmP.h"
38 #include "drm_sarea.h"
39 #include <linux/poll.h>
40
41 static int drm_open_helper(struct inode *inode, struct file *filp,
42                            drm_device_t * dev);
43
44 static int drm_setup(drm_device_t * dev)
45 {
46         drm_local_map_t *map;
47         int i;
48         int ret;
49
50
51         if (dev->driver->firstopen) {
52                 ret = dev->driver->firstopen(dev);
53                 if (ret != 0)
54                         return ret;
55         }
56
57         dev->magicfree.next = NULL;
58
59         /* prebuild the SAREA */
60
61         i = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM, _DRM_CONTAINS_LOCK, &map);
62         if (i != 0)
63                 return i;
64
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);
69
70         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
71                 i = drm_dma_setup(dev);
72                 if (i < 0)
73                         return i;
74         }
75
76         for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
77                 atomic_set(&dev->counts[i], 0);
78
79         drm_ht_create(&dev->magiclist, DRM_MAGIC_HASH_ORDER);
80         INIT_LIST_HEAD(&dev->magicfree);
81
82         dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist), DRM_MEM_CTXLIST);
83         if (dev->ctxlist == NULL)
84                 return -ENOMEM;
85         memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
86         INIT_LIST_HEAD(&dev->ctxlist->head);
87
88         dev->vmalist = NULL;
89         dev->sigdata.lock = NULL;
90         init_waitqueue_head(&dev->lock.lock_queue);
91         dev->queue_count = 0;
92         dev->queue_reserved = 0;
93         dev->queue_slots = 0;
94         dev->queuelist = NULL;
95         dev->irq_enabled = 0;
96         dev->context_flag = 0;
97         dev->interrupt_flag = 0;
98         dev->dma_flag = 0;
99         dev->last_context = 0;
100         dev->last_switch = 0;
101         dev->last_checked = 0;
102         init_waitqueue_head(&dev->context_wait);
103         dev->if_version = 0;
104
105         dev->ctx_start = 0;
106         dev->lck_start = 0;
107
108         dev->buf_async = NULL;
109         init_waitqueue_head(&dev->buf_readers);
110         init_waitqueue_head(&dev->buf_writers);
111
112         DRM_DEBUG("\n");
113
114         /*
115          * The kernel's context could be created here, but is now created
116          * in drm_dma_enqueue.  This is more resource-efficient for
117          * hardware that does not do DMA, but may mean that
118          * drm_select_queue fails between the time the interrupt is
119          * initialized and the time the queues are initialized.
120          */
121
122         return 0;
123 }
124
125 /**
126  * Open file.
127  *
128  * \param inode device inode
129  * \param filp file pointer.
130  * \return zero on success or a negative number on failure.
131  *
132  * Searches the DRM device with the same minor number, calls open_helper(), and
133  * increments the device open count. If the open count was previous at zero,
134  * i.e., it's the first that the device is open, then calls setup().
135  */
136 int drm_open(struct inode *inode, struct file *filp)
137 {
138         drm_device_t *dev = NULL;
139         int minor = iminor(inode);
140         int retcode = 0;
141
142         if (!((minor >= 0) && (minor < drm_cards_limit)))
143                 return -ENODEV;
144
145         if (!drm_heads[minor])
146                 return -ENODEV;
147
148         if (!(dev = drm_heads[minor]->dev))
149                 return -ENODEV;
150
151         retcode = drm_open_helper(inode, filp, dev);
152         if (!retcode) {
153                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
154                 spin_lock(&dev->count_lock);
155                 if (!dev->open_count++) {
156                         spin_unlock(&dev->count_lock);
157                         return drm_setup(dev);
158                 }
159                 spin_unlock(&dev->count_lock);
160         }
161
162         return retcode;
163 }
164 EXPORT_SYMBOL(drm_open);
165
166 /**
167  * File \c open operation.
168  *
169  * \param inode device inode.
170  * \param filp file pointer.
171  *
172  * Puts the dev->fops corresponding to the device minor number into
173  * \p filp, call the \c open method, and restore the file operations.
174  */
175 int drm_stub_open(struct inode *inode, struct file *filp)
176 {
177         drm_device_t *dev = NULL;
178         int minor = iminor(inode);
179         int err = -ENODEV;
180         const struct file_operations *old_fops;
181
182         DRM_DEBUG("\n");
183
184         if (!((minor >= 0) && (minor < drm_cards_limit)))
185                 return -ENODEV;
186
187         if (!drm_heads[minor])
188                 return -ENODEV;
189
190         if (!(dev = drm_heads[minor]->dev))
191                 return -ENODEV;
192
193         old_fops = filp->f_op;
194         filp->f_op = fops_get(&dev->driver->fops);
195         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
196                 fops_put(filp->f_op);
197                 filp->f_op = fops_get(old_fops);
198         }
199         fops_put(old_fops);
200
201         return err;
202 }
203
204 /**
205  * Check whether DRI will run on this CPU.
206  *
207  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
208  */
209 static int drm_cpu_valid(void)
210 {
211 #if defined(__i386__)
212         if (boot_cpu_data.x86 == 3)
213                 return 0;       /* No cmpxchg on a 386 */
214 #endif
215 #if defined(__sparc__) && !defined(__sparc_v9__)
216         return 0;               /* No cmpxchg before v9 sparc. */
217 #endif
218         return 1;
219 }
220
221 /**
222  * Called whenever a process opens /dev/drm.
223  *
224  * \param inode device inode.
225  * \param filp file pointer.
226  * \param dev device.
227  * \return zero on success or a negative number on failure.
228  *
229  * Creates and initializes a drm_file structure for the file private data in \p
230  * filp and add it into the double linked list in \p dev.
231  */
232 static int drm_open_helper(struct inode *inode, struct file *filp,
233                            drm_device_t * dev)
234 {
235         int minor = iminor(inode);
236         drm_file_t *priv;
237         int ret;
238         int i,j;
239
240         if (filp->f_flags & O_EXCL)
241                 return -EBUSY;  /* No exclusive opens */
242         if (!drm_cpu_valid())
243                 return -EINVAL;
244
245         DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
246
247         priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
248         if (!priv)
249                 return -ENOMEM;
250
251         memset(priv, 0, sizeof(*priv));
252         filp->private_data = priv;
253         priv->uid = current->euid;
254         priv->pid = current->pid;
255         priv->minor = minor;
256         priv->head = drm_heads[minor];
257         priv->ioctl_count = 0;
258         /* for compatibility root is always authenticated */
259         priv->authenticated = capable(CAP_SYS_ADMIN);
260         priv->lock_count = 0;
261
262         INIT_LIST_HEAD(&priv->user_objects);
263         INIT_LIST_HEAD(&priv->refd_objects);
264
265         for (i=0; i<_DRM_NO_REF_TYPES; ++i) {
266                 ret = drm_ht_create(&priv->refd_object_hash[i], DRM_FILE_HASH_ORDER);
267                 if (ret)
268                         break;
269         }
270
271         if (ret) {
272                 for(j=0; j<i; ++j) {
273                         drm_ht_remove(&priv->refd_object_hash[j]);
274                 }
275                 goto out_free;
276         }
277
278         if (dev->driver->open) {
279                 ret = dev->driver->open(dev, priv);
280                 if (ret < 0)
281                         goto out_free;
282         }
283
284         mutex_lock(&dev->struct_mutex);
285         if (!dev->file_last) {
286                 priv->next = NULL;
287                 priv->prev = NULL;
288                 dev->file_first = priv;
289                 dev->file_last = priv;
290                 /* first opener automatically becomes master */
291                 priv->master = 1;
292         } else {
293                 priv->next = NULL;
294                 priv->prev = dev->file_last;
295                 dev->file_last->next = priv;
296                 dev->file_last = priv;
297         }
298         mutex_unlock(&dev->struct_mutex);
299
300 #ifdef __alpha__
301         /*
302          * Default the hose
303          */
304         if (!dev->hose) {
305                 struct pci_dev *pci_dev;
306                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
307                 if (pci_dev) {
308                         dev->hose = pci_dev->sysdata;
309                         pci_dev_put(pci_dev);
310                 }
311                 if (!dev->hose) {
312                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
313                         if (b)
314                                 dev->hose = b->sysdata;
315                 }
316         }
317 #endif
318
319         return 0;
320       out_free:
321         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
322         filp->private_data = NULL;
323         return ret;
324 }
325
326 /** No-op. */
327 int drm_fasync(int fd, struct file *filp, int on)
328 {
329         drm_file_t *priv = filp->private_data;
330         drm_device_t *dev = priv->head->dev;
331         int retcode;
332
333         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
334                   (long)old_encode_dev(priv->head->device));
335         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
336         if (retcode < 0)
337                 return retcode;
338         return 0;
339 }
340 EXPORT_SYMBOL(drm_fasync);
341
342 static void drm_object_release(struct file *filp) {
343
344         drm_file_t *priv = filp->private_data;
345         struct list_head *head;
346         drm_user_object_t *user_object;
347         drm_ref_object_t *ref_object;
348         int i;
349
350         /*
351          * Free leftover ref objects created by me. Note that we cannot use
352          * list_for_each() here, as the struct_mutex may be temporarily released 
353          * by the remove_() functions, and thus the lists may be altered.
354          * Also, a drm_remove_ref_object() will not remove it
355          * from the list unless its refcount is 1.
356          */
357
358         head = &priv->refd_objects; 
359         while (head->next != head) {
360                 ref_object = list_entry(head->next, drm_ref_object_t, list);
361                 drm_remove_ref_object(priv, ref_object);                
362                 head = &priv->refd_objects; 
363         }
364                 
365         /*
366          * Free leftover user objects created by me.
367          */
368
369         head = &priv->user_objects; 
370         while (head->next != head) {
371                 user_object = list_entry(head->next, drm_user_object_t, list);
372                 drm_remove_user_object(priv, user_object);              
373                 head = &priv->user_objects; 
374         }
375
376
377
378
379         for(i=0; i<_DRM_NO_REF_TYPES; ++i) {
380                 drm_ht_remove(&priv->refd_object_hash[i]);
381         }
382 }                       
383                 
384
385
386
387
388
389 /**
390  * Release file.
391  *
392  * \param inode device inode
393  * \param filp file pointer.
394  * \return zero on success or a negative number on failure.
395  *
396  * If the hardware lock is held then free it, and take it again for the kernel
397  * context since it's necessary to reclaim buffers. Unlink the file private
398  * data from its list and free it. Decreases the open count and if it reaches
399  * zero calls drm_lastclose().
400  */
401 int drm_release(struct inode *inode, struct file *filp)
402 {
403         drm_file_t *priv = filp->private_data;
404         drm_device_t *dev;
405         int retcode = 0;
406
407         lock_kernel();
408         dev = priv->head->dev;
409
410         DRM_DEBUG("open_count = %d\n", dev->open_count);
411
412         if (dev->driver->preclose)
413                 dev->driver->preclose(dev, filp);
414
415         /* ========================================================
416          * Begin inline drm_release
417          */
418
419         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
420                   current->pid, (long)old_encode_dev(priv->head->device),
421                   dev->open_count);
422
423         if (dev->driver->reclaim_buffers_locked) {
424                 unsigned long _end = jiffies + DRM_HZ*3;
425
426                 do {
427                         retcode = drm_kernel_take_hw_lock(filp);
428                 } while(retcode && !time_after_eq(jiffies,_end));
429
430                 if (!retcode) {
431                         dev->driver->reclaim_buffers_locked(dev, filp);
432
433                         drm_lock_free(dev, &dev->lock.hw_lock->lock,
434                                       _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
435                 } else {
436
437                         /*
438                          * FIXME: This is not a good solution. We should perhaps associate the
439                          * DRM lock with a process context, and check whether the current process
440                          * holds the lock. Then we can run reclaim buffers locked anyway.
441                          */
442
443                         DRM_ERROR("Reclaim buffers locked deadlock.\n");
444                         DRM_ERROR("This is probably a single thread having multiple\n");
445                         DRM_ERROR("DRM file descriptors open either dying or "
446                                   "closing file descriptors\n");
447                         DRM_ERROR("while having the lock. I will not reclaim buffers.\n");
448                         DRM_ERROR("Locking context is 0x%08x\n",
449                                   _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
450                 }
451         } else if (drm_i_have_hw_lock(filp)) {
452                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
453                           filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
454
455                 drm_lock_free(dev, &dev->lock.hw_lock->lock,
456                               _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
457         }
458
459
460         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
461             !dev->driver->reclaim_buffers_locked) {
462                 dev->driver->reclaim_buffers(dev, filp);
463         }
464
465         drm_fasync(-1, filp, 0);
466
467         mutex_lock(&dev->ctxlist_mutex);
468         if (dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
469                 drm_ctx_list_t *pos, *n;
470
471                 list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
472                         if (pos->tag == priv &&
473                             pos->handle != DRM_KERNEL_CONTEXT) {
474                                 if (dev->driver->context_dtor)
475                                         dev->driver->context_dtor(dev,
476                                                                   pos->handle);
477
478                                 drm_ctxbitmap_free(dev, pos->handle);
479
480                                 list_del(&pos->head);
481                                 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
482                                 --dev->ctx_count;
483                         }
484                 }
485         }
486         mutex_unlock(&dev->ctxlist_mutex);
487
488         mutex_lock(&dev->struct_mutex);
489         drm_object_release(filp);
490         if (priv->remove_auth_on_close == 1) {
491                 drm_file_t *temp = dev->file_first;
492                 while (temp) {
493                         temp->authenticated = 0;
494                         temp = temp->next;
495                 }
496         }
497         if (priv->prev) {
498                 priv->prev->next = priv->next;
499         } else {
500                 dev->file_first = priv->next;
501         }
502         if (priv->next) {
503                 priv->next->prev = priv->prev;
504         } else {
505                 dev->file_last = priv->prev;
506         }
507         mutex_unlock(&dev->struct_mutex);
508
509         if (dev->driver->postclose)
510                 dev->driver->postclose(dev, priv);
511         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
512
513         /* ========================================================
514          * End inline drm_release
515          */
516
517         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
518         spin_lock(&dev->count_lock);
519         if (!--dev->open_count) {
520                 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
521                         DRM_ERROR("Device busy: %d %d\n",
522                                   atomic_read(&dev->ioctl_count), dev->blocked);
523                         spin_unlock(&dev->count_lock);
524                         unlock_kernel();
525                         return -EBUSY;
526                 }
527                 spin_unlock(&dev->count_lock);
528                 unlock_kernel();
529                 return drm_lastclose(dev);
530         }
531         spin_unlock(&dev->count_lock);
532
533         unlock_kernel();
534
535         return retcode;
536 }
537 EXPORT_SYMBOL(drm_release);
538
539 /** No-op. */
540 /* This is to deal with older X servers that believe 0 means data is
541  * available which is not the correct return for a poll function.
542  * This cannot be fixed until the Xserver is fixed. Xserver will need
543  * to set a newer interface version to avoid breaking older Xservers.
544  * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
545  * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
546  * to return the correct response. 
547  */
548 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
549 {
550         /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
551         return 0;
552 }
553 EXPORT_SYMBOL(drm_poll);
554