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