e6e0e86787130f2f06896e67c320553b27ac9161
[profile/ivi/libdrm.git] / linux-core / drm_fops.c
1 /**
2  * \file drm_fops.h
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 <linux/poll.h>
39
40 static int drm_setup(drm_device_t * dev)
41 {
42         int i;
43
44         if (dev->fn_tbl->presetup)
45                 dev->fn_tbl->presetup(dev);
46
47         atomic_set(&dev->ioctl_count, 0);
48         atomic_set(&dev->vma_count, 0);
49         dev->buf_use = 0;
50         atomic_set(&dev->buf_alloc, 0);
51
52         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
53                 i = drm_dma_setup(dev);
54                 if (i < 0)
55                         return i;
56         }
57
58         for (i = 0; i < DRM_ARRAY_SIZE(dev->counts); i++)
59                 atomic_set(&dev->counts[i], 0);
60
61         for (i = 0; i < DRM_HASH_SIZE; i++) {
62                 dev->magiclist[i].head = NULL;
63                 dev->magiclist[i].tail = NULL;
64         }
65
66         dev->ctxlist = drm_alloc(sizeof(*dev->ctxlist), DRM_MEM_CTXLIST);
67         if (dev->ctxlist == NULL)
68                 return -ENOMEM;
69         memset(dev->ctxlist, 0, sizeof(*dev->ctxlist));
70         INIT_LIST_HEAD(&dev->ctxlist->head);
71
72         dev->vmalist = NULL;
73         dev->sigdata.lock = dev->lock.hw_lock = NULL;
74         init_waitqueue_head(&dev->lock.lock_queue);
75         dev->queue_count = 0;
76         dev->queue_reserved = 0;
77         dev->queue_slots = 0;
78         dev->queuelist = NULL;
79         dev->irq_enabled = 0;
80         dev->context_flag = 0;
81         dev->interrupt_flag = 0;
82         dev->dma_flag = 0;
83         dev->last_context = 0;
84         dev->last_switch = 0;
85         dev->last_checked = 0;
86         init_waitqueue_head(&dev->context_wait);
87         dev->if_version = 0;
88
89         dev->ctx_start = 0;
90         dev->lck_start = 0;
91
92         dev->buf_rp = dev->buf;
93         dev->buf_wp = dev->buf;
94         dev->buf_end = dev->buf + DRM_BSZ;
95         dev->buf_async = NULL;
96         init_waitqueue_head(&dev->buf_readers);
97         init_waitqueue_head(&dev->buf_writers);
98
99         DRM_DEBUG("\n");
100
101         /*
102          * The kernel's context could be created here, but is now created
103          * in drm_dma_enqueue.  This is more resource-efficient for
104          * hardware that does not do DMA, but may mean that
105          * drm_select_queue fails between the time the interrupt is
106          * initialized and the time the queues are initialized.
107          */
108         if (dev->fn_tbl->postsetup)
109                 dev->fn_tbl->postsetup(dev);
110
111         return 0;
112 }
113
114 /**
115  * Open file.
116  *
117  * \param inode device inode
118  * \param filp file pointer.
119  * \return zero on success or a negative number on failure.
120  *
121  * Searches the DRM device with the same minor number, calls open_helper(), and
122  * increments the device open count. If the open count was previous at zero,
123  * i.e., it's the first that the device is open, then calls setup().
124  */
125 int drm_open(struct inode *inode, struct file *filp)
126 {
127         drm_device_t *dev = NULL;
128         int minor = iminor(inode);
129         int retcode = 0;
130
131         if (!((minor >= 0) && (minor < cards_limit)))
132                 return -ENODEV;
133
134         dev = drm_minors[minor].dev;
135         if (!dev)
136                 return -ENODEV;
137
138         retcode = drm_open_helper(inode, filp, dev);
139         if (!retcode) {
140                 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
141                 spin_lock(&dev->count_lock);
142                 if (!dev->open_count++) {
143                         spin_unlock(&dev->count_lock);
144                         return drm_setup(dev);
145                 }
146                 spin_unlock(&dev->count_lock);
147         }
148
149         return retcode;
150 }
151 EXPORT_SYMBOL(drm_open);
152
153 /**
154  * File \c open operation.
155  *
156  * \param inode device inode.
157  * \param filp file pointer.
158  *
159  * Puts the dev->fops corresponding to the device minor number into
160  * \p filp, call the \c open method, and restore the file operations.
161  */
162 int drm_stub_open(struct inode *inode, struct file *filp)
163 {
164         drm_device_t *dev = NULL;
165         int minor = iminor(inode);
166         int err = -ENODEV;
167         struct file_operations *old_fops;
168
169         DRM_DEBUG("\n");
170
171         if (!((minor >= 0) && (minor < cards_limit)))
172                 return -ENODEV;
173
174         dev = drm_minors[minor].dev;
175         if (!dev)
176                 return -ENODEV;
177
178         old_fops = filp->f_op;
179         filp->f_op = fops_get(&dev->fn_tbl->fops);
180         if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
181                 fops_put(filp->f_op);
182                 filp->f_op = fops_get(old_fops);
183         }
184         fops_put(old_fops);
185
186         return err;
187 }
188
189 /**
190  * Called whenever a process opens /dev/drm.
191  *
192  * \param inode device inode.
193  * \param filp file pointer.
194  * \param dev device.
195  * \return zero on success or a negative number on failure.
196  *
197  * Creates and initializes a drm_file structure for the file private data in \p
198  * filp and add it into the double linked list in \p dev.
199  */
200 int drm_open_helper(struct inode *inode, struct file *filp, drm_device_t * dev)
201 {
202         int minor = iminor(inode);
203         drm_file_t *priv;
204         int ret;
205
206         if (filp->f_flags & O_EXCL)
207                 return -EBUSY;  /* No exclusive opens */
208         if (!drm_cpu_valid())
209                 return -EINVAL;
210
211         DRM_DEBUG("pid = %d, minor = %d\n", current->pid, minor);
212
213         priv = drm_alloc(sizeof(*priv), DRM_MEM_FILES);
214         if (!priv)
215                 return -ENOMEM;
216
217         memset(priv, 0, sizeof(*priv));
218         filp->private_data = priv;
219         priv->uid = current->euid;
220         priv->pid = current->pid;
221         priv->minor = minor;
222         priv->dev = dev;
223         priv->ioctl_count = 0;
224         priv->authenticated = capable(CAP_SYS_ADMIN);
225         priv->lock_count = 0;
226
227         if (dev->fn_tbl->open_helper) {
228                 ret = dev->fn_tbl->open_helper(dev, priv);
229                 if (ret < 0)
230                         goto out_free;
231         }
232
233         down(&dev->struct_sem);
234         if (!dev->file_last) {
235                 priv->next = NULL;
236                 priv->prev = NULL;
237                 dev->file_first = priv;
238                 dev->file_last = priv;
239         } else {
240                 priv->next = NULL;
241                 priv->prev = dev->file_last;
242                 dev->file_last->next = priv;
243                 dev->file_last = priv;
244         }
245         up(&dev->struct_sem);
246
247 #ifdef __alpha__
248         /*
249          * Default the hose
250          */
251         if (!dev->hose) {
252                 struct pci_dev *pci_dev;
253                 pci_dev = pci_find_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
254                 if (pci_dev)
255                         dev->hose = pci_dev->sysdata;
256                 if (!dev->hose) {
257                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
258                         if (b)
259                                 dev->hose = b->sysdata;
260                 }
261         }
262 #endif
263
264         return 0;
265       out_free:
266         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
267         filp->private_data = NULL;
268         return ret;
269 }
270
271 /** No-op. */
272 int drm_fasync(int fd, struct file *filp, int on)
273 {
274         drm_file_t *priv = filp->private_data;
275         drm_device_t *dev = priv->dev;
276         int retcode;
277
278         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
279                   (long)old_encode_dev(dev->device));
280         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
281         if (retcode < 0)
282                 return retcode;
283         return 0;
284 }
285 EXPORT_SYMBOL(drm_fasync);
286
287 /**
288  * Release file.
289  *
290  * \param inode device inode
291  * \param filp file pointer.
292  * \return zero on success or a negative number on failure.
293  *
294  * If the hardware lock is held then free it, and take it again for the kernel
295  * context since it's necessary to reclaim buffers. Unlink the file private
296  * data from its list and free it. Decreases the open count and if it reaches
297  * zero calls takedown().
298  */
299 int drm_release(struct inode *inode, struct file *filp)
300 {
301         drm_file_t *priv = filp->private_data;
302         drm_device_t *dev;
303         int retcode = 0;
304
305         lock_kernel();
306         dev = priv->dev;
307
308         DRM_DEBUG("open_count = %d\n", dev->open_count);
309
310         if (dev->fn_tbl->prerelease)
311                 dev->fn_tbl->prerelease(dev, filp);
312
313         /* ========================================================
314          * Begin inline drm_release
315          */
316
317         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
318                   current->pid, (long)old_encode_dev(dev->device),
319                   dev->open_count);
320
321         if (priv->lock_count && dev->lock.hw_lock &&
322             _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
323             dev->lock.filp == filp) {
324                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
325                           filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
326
327                 if (dev->fn_tbl->release)
328                         dev->fn_tbl->release(dev, filp);
329
330                 drm_lock_free(dev, &dev->lock.hw_lock->lock,
331                               _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
332
333                 /* FIXME: may require heavy-handed reset of
334                    hardware at this point, possibly
335                    processed via a callback to the X
336                    server. */
337         } else if (dev->fn_tbl->release && priv->lock_count
338                    && dev->lock.hw_lock) {
339                 /* The lock is required to reclaim buffers */
340                 DECLARE_WAITQUEUE(entry, current);
341
342                 add_wait_queue(&dev->lock.lock_queue, &entry);
343                 for (;;) {
344                         current->state = TASK_INTERRUPTIBLE;
345                         if (!dev->lock.hw_lock) {
346                                 /* Device has been unregistered */
347                                 retcode = -EINTR;
348                                 break;
349                         }
350                         if (drm_lock_take(&dev->lock.hw_lock->lock,
351                                           DRM_KERNEL_CONTEXT)) {
352                                 dev->lock.filp = filp;
353                                 dev->lock.lock_time = jiffies;
354                                 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
355                                 break;  /* Got lock */
356                         }
357                         /* Contention */
358                         schedule();
359                         if (signal_pending(current)) {
360                                 retcode = -ERESTARTSYS;
361                                 break;
362                         }
363                 }
364                 current->state = TASK_RUNNING;
365                 remove_wait_queue(&dev->lock.lock_queue, &entry);
366                 if (!retcode) {
367                         if (dev->fn_tbl->release)
368                                 dev->fn_tbl->release(dev, filp);
369                         drm_lock_free(dev, &dev->lock.hw_lock->lock,
370                                       DRM_KERNEL_CONTEXT);
371                 }
372         }
373
374         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA)) {
375                 dev->fn_tbl->reclaim_buffers(filp);
376         }
377
378         drm_fasync(-1, filp, 0);
379
380         down(&dev->ctxlist_sem);
381         if (!list_empty(&dev->ctxlist->head)) {
382                 drm_ctx_list_t *pos, *n;
383
384                 list_for_each_entry_safe(pos, n, &dev->ctxlist->head, head) {
385                         if (pos->tag == priv &&
386                             pos->handle != DRM_KERNEL_CONTEXT) {
387                                 if (dev->fn_tbl->context_dtor)
388                                         dev->fn_tbl->context_dtor(dev,
389                                                                   pos->handle);
390
391                                 drm_ctxbitmap_free(dev, pos->handle);
392
393                                 list_del(&pos->head);
394                                 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
395                                 --dev->ctx_count;
396                         }
397                 }
398         }
399         up(&dev->ctxlist_sem);
400
401         down(&dev->struct_sem);
402         if (priv->remove_auth_on_close == 1) {
403                 drm_file_t *temp = dev->file_first;
404                 while (temp) {
405                         temp->authenticated = 0;
406                         temp = temp->next;
407                 }
408         }
409         if (priv->prev) {
410                 priv->prev->next = priv->next;
411         } else {
412                 dev->file_first = priv->next;
413         }
414         if (priv->next) {
415                 priv->next->prev = priv->prev;
416         } else {
417                 dev->file_last = priv->prev;
418         }
419         up(&dev->struct_sem);
420
421         if (dev->fn_tbl->free_filp_priv)
422                 dev->fn_tbl->free_filp_priv(dev, priv);
423         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
424
425         /* ========================================================
426          * End inline drm_release
427          */
428
429         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
430         spin_lock(&dev->count_lock);
431         if (!--dev->open_count) {
432                 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
433                         DRM_ERROR("Device busy: %d %d\n",
434                                   atomic_read(&dev->ioctl_count), dev->blocked);
435                         spin_unlock(&dev->count_lock);
436                         unlock_kernel();
437                         return -EBUSY;
438                 }
439                 spin_unlock(&dev->count_lock);
440                 unlock_kernel();
441                 return drm_takedown(dev);
442         }
443         spin_unlock(&dev->count_lock);
444
445         unlock_kernel();
446
447         return retcode;
448 }
449 EXPORT_SYMBOL(drm_release);