OSDN Git Service

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