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