OSDN Git Service

Merge branch 'nouveau-1'
[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                            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         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         mutex_lock(&dev->struct_mutex);
162         BUG_ON((dev->dev_mapping != NULL) && 
163                (dev->dev_mapping != inode->i_mapping));
164         if (dev->dev_mapping == NULL)
165                 dev->dev_mapping = inode->i_mapping;
166         mutex_unlock(&dev->struct_mutex);
167
168         return retcode;
169 }
170 EXPORT_SYMBOL(drm_open);
171
172 /**
173  * File \c open operation.
174  *
175  * \param inode device inode.
176  * \param filp file pointer.
177  *
178  * Puts the dev->fops corresponding to the device minor number into
179  * \p filp, call the \c open method, and restore the file operations.
180  */
181 int drm_stub_open(struct inode *inode, struct file *filp)
182 {
183         drm_device_t *dev = NULL;
184         int minor = iminor(inode);
185         int err = -ENODEV;
186         const struct file_operations *old_fops;
187
188         DRM_DEBUG("\n");
189
190         if (!((minor >= 0) && (minor < drm_cards_limit)))
191                 return -ENODEV;
192
193         if (!drm_heads[minor])
194                 return -ENODEV;
195
196         if (!(dev = drm_heads[minor]->dev))
197                 return -ENODEV;
198
199         old_fops = filp->f_op;
200         filp->f_op = fops_get(&dev->driver->fops);
201         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
202                 fops_put(filp->f_op);
203                 filp->f_op = fops_get(old_fops);
204         }
205         fops_put(old_fops);
206
207         return err;
208 }
209
210 /**
211  * Check whether DRI will run on this CPU.
212  *
213  * \return non-zero if the DRI will run on this CPU, or zero otherwise.
214  */
215 static int drm_cpu_valid(void)
216 {
217 #if defined(__i386__)
218         if (boot_cpu_data.x86 == 3)
219                 return 0;       /* No cmpxchg on a 386 */
220 #endif
221 #if defined(__sparc__) && !defined(__sparc_v9__)
222         return 0;               /* No cmpxchg before v9 sparc. */
223 #endif
224         return 1;
225 }
226
227 /**
228  * Called whenever a process opens /dev/drm.
229  *
230  * \param inode device inode.
231  * \param filp file pointer.
232  * \param dev device.
233  * \return zero on success or a negative number on failure.
234  *
235  * Creates and initializes a drm_file structure for the file private data in \p
236  * filp and add it into the double linked list in \p dev.
237  */
238 static int drm_open_helper(struct inode *inode, struct file *filp,
239                            drm_device_t * dev)
240 {
241         int minor = iminor(inode);
242         drm_file_t *priv;
243         int ret;
244         int i,j;
245
246         if (filp->f_flags & O_EXCL)
247                 return -EBUSY;  /* No exclusive opens */
248         if (!drm_cpu_valid())
249                 return -EINVAL;
250
251         DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
252
253         priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
254         if (!priv)
255                 return -ENOMEM;
256
257         memset(priv, 0, sizeof(*priv));
258         filp->private_data = priv;
259         priv->uid = current->euid;
260         priv->pid = current->pid;
261         priv->minor = minor;
262         priv->head = drm_heads[minor];
263         priv->ioctl_count = 0;
264         /* for compatibility root is always authenticated */
265         priv->authenticated = capable(CAP_SYS_ADMIN);
266         priv->lock_count = 0;
267
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 (!dev->file_last) {
292                 priv->next = NULL;
293                 priv->prev = NULL;
294                 dev->file_first = priv;
295                 dev->file_last = priv;
296                 /* first opener automatically becomes master */
297                 priv->master = 1;
298         } else {
299                 priv->next = NULL;
300                 priv->prev = dev->file_last;
301                 dev->file_last->next = priv;
302                 dev->file_last = priv;
303         }
304         mutex_unlock(&dev->struct_mutex);
305
306 #ifdef __alpha__
307         /*
308          * Default the hose
309          */
310         if (!dev->hose) {
311                 struct pci_dev *pci_dev;
312                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
313                 if (pci_dev) {
314                         dev->hose = pci_dev->sysdata;
315                         pci_dev_put(pci_dev);
316                 }
317                 if (!dev->hose) {
318                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
319                         if (b)
320                                 dev->hose = b->sysdata;
321                 }
322         }
323 #endif
324
325         return 0;
326       out_free:
327         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
328         filp->private_data = NULL;
329         return ret;
330 }
331
332 /** No-op. */
333 int drm_fasync(int fd, struct file *filp, int on)
334 {
335         drm_file_t *priv = filp->private_data;
336         drm_device_t *dev = priv->head->dev;
337         int retcode;
338
339         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
340                   (long)old_encode_dev(priv->head->device));
341         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
342         if (retcode < 0)
343                 return retcode;
344         return 0;
345 }
346 EXPORT_SYMBOL(drm_fasync);
347
348 static void drm_object_release(struct file *filp) {
349
350         drm_file_t *priv = filp->private_data;
351         struct list_head *head;
352         drm_user_object_t *user_object;
353         drm_ref_object_t *ref_object;
354         int i;
355
356         /*
357          * Free leftover ref objects created by me. Note that we cannot use
358          * list_for_each() here, as the struct_mutex may be temporarily released 
359          * by the remove_() functions, and thus the lists may be altered.
360          * Also, a drm_remove_ref_object() will not remove it
361          * from the list unless its refcount is 1.
362          */
363
364         head = &priv->refd_objects; 
365         while (head->next != head) {
366                 ref_object = list_entry(head->next, drm_ref_object_t, list);
367                 drm_remove_ref_object(priv, ref_object);                
368                 head = &priv->refd_objects; 
369         }
370                 
371         /*
372          * Free leftover user objects created by me.
373          */
374
375         head = &priv->user_objects; 
376         while (head->next != head) {
377                 user_object = list_entry(head->next, drm_user_object_t, list);
378                 drm_remove_user_object(priv, user_object);              
379                 head = &priv->user_objects; 
380         }
381
382
383
384
385         for(i=0; i<_DRM_NO_REF_TYPES; ++i) {
386                 drm_ht_remove(&priv->refd_object_hash[i]);
387         }
388 }                       
389                 
390
391
392
393
394
395 /**
396  * Release file.
397  *
398  * \param inode device inode
399  * \param filp file pointer.
400  * \return zero on success or a negative number on failure.
401  *
402  * If the hardware lock is held then free it, and take it again for the kernel
403  * context since it's necessary to reclaim buffers. Unlink the file private
404  * data from its list and free it. Decreases the open count and if it reaches
405  * zero calls drm_lastclose().
406  */
407 int drm_release(struct inode *inode, struct file *filp)
408 {
409         drm_file_t *priv = filp->private_data;
410         drm_device_t *dev;
411         int retcode = 0;
412
413         lock_kernel();
414         dev = priv->head->dev;
415
416         DRM_DEBUG("open_count = %d\n", dev->open_count);
417
418         if (dev->driver->preclose)
419                 dev->driver->preclose(dev, filp);
420
421         /* ========================================================
422          * Begin inline drm_release
423          */
424
425         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
426                   current->pid, (long)old_encode_dev(priv->head->device),
427                   dev->open_count);
428
429         if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
430                 unsigned long _end = jiffies + DRM_HZ*3;
431
432                 do {
433                         retcode = drm_kernel_take_hw_lock(filp);
434                 } while(retcode && !time_after_eq(jiffies,_end));
435
436                 if (!retcode) {
437                         dev->driver->reclaim_buffers_locked(dev, filp);
438
439                         drm_lock_free(dev, &dev->lock.hw_lock->lock,
440                                       _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
441                 } else {
442
443                         /*
444                          * FIXME: This is not a good solution. We should perhaps associate the
445                          * DRM lock with a process context, and check whether the current process
446                          * holds the lock. Then we can run reclaim buffers locked anyway.
447                          */
448
449                         DRM_ERROR("Reclaim buffers locked deadlock.\n"
450                                   "\tThis is probably a single thread having multiple\n"
451                                   "\tDRM file descriptors open either dying or"
452                                   " closing file descriptors\n"
453                                   "\twhile having the lock. I will not reclaim buffers.\n"
454                                   "\tLocking context is 0x%08x\n",
455                                   _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
456                 }
457         } else if (drm_i_have_hw_lock(filp)) {
458                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
459                           filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
460
461                 drm_lock_free(dev, &dev->lock.hw_lock->lock,
462                               _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
463         }
464
465
466         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
467             !dev->driver->reclaim_buffers_locked) {
468                 dev->driver->reclaim_buffers(dev, filp);
469         }
470
471         drm_fasync(-1, filp, 0);
472
473         mutex_lock(&dev->ctxlist_mutex);
474
475         if (dev->ctxlist && (!list_empty(&dev->ctxlist->head))) {
476                 drm_ctx_list_t *pos, *n;
477
478                 list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
479                         if (pos->tag == priv &&
480                             pos->handle != DRM_KERNEL_CONTEXT) {
481                                 if (dev->driver->context_dtor)
482                                         dev->driver->context_dtor(dev,
483                                                                   pos->handle);
484
485                                 drm_ctxbitmap_free(dev, pos->handle);
486
487                                 list_del(&pos->head);
488                                 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
489                                 --dev->ctx_count;
490                         }
491                 }
492         }
493         mutex_unlock(&dev->ctxlist_mutex);
494
495         mutex_lock(&dev->struct_mutex);
496         drm_object_release(filp);
497         if (priv->remove_auth_on_close == 1) {
498                 drm_file_t *temp = dev->file_first;
499                 while (temp) {
500                         temp->authenticated = 0;
501                         temp = temp->next;
502                 }
503         }
504         if (priv->prev) {
505                 priv->prev->next = priv->next;
506         } else {
507                 dev->file_first = priv->next;
508         }
509         if (priv->next) {
510                 priv->next->prev = priv->prev;
511         } else {
512                 dev->file_last = priv->prev;
513         }
514         mutex_unlock(&dev->struct_mutex);
515
516         if (dev->driver->postclose)
517                 dev->driver->postclose(dev, priv);
518         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
519
520         /* ========================================================
521          * End inline drm_release
522          */
523
524         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
525         spin_lock(&dev->count_lock);
526         if (!--dev->open_count) {
527                 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
528                         DRM_ERROR("Device busy: %d %d\n",
529                                   atomic_read(&dev->ioctl_count), dev->blocked);
530                         spin_unlock(&dev->count_lock);
531                         unlock_kernel();
532                         return -EBUSY;
533                 }
534                 spin_unlock(&dev->count_lock);
535                 unlock_kernel();
536                 return drm_lastclose(dev);
537         }
538         spin_unlock(&dev->count_lock);
539
540         unlock_kernel();
541
542         return retcode;
543 }
544 EXPORT_SYMBOL(drm_release);
545
546 /** No-op. */
547 /* This is to deal with older X servers that believe 0 means data is
548  * available which is not the correct return for a poll function.
549  * This cannot be fixed until the Xserver is fixed. Xserver will need
550  * to set a newer interface version to avoid breaking older Xservers.
551  * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
552  * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
553  * to return the correct response. 
554  */
555 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
556 {
557         /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
558         return 0;
559 }
560 EXPORT_SYMBOL(drm_poll);
561