[intel] Quirk away MSI support on 945G/GM.
[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->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->driver_features & DRIVER_GEM)
278                 drm_gem_open(dev, priv);
279
280         if (dev->driver->open) {
281                 ret = dev->driver->open(dev, priv);
282                 if (ret < 0)
283                         goto out_free;
284         }
285
286         mutex_lock(&dev->struct_mutex);
287         if (list_empty(&dev->filelist))
288                 priv->master = 1;
289
290         list_add(&priv->lhead, &dev->filelist);
291         mutex_unlock(&dev->struct_mutex);
292
293 #ifdef __alpha__
294         /*
295          * Default the hose
296          */
297         if (!dev->hose) {
298                 struct pci_dev *pci_dev;
299                 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
300                 if (pci_dev) {
301                         dev->hose = pci_dev->sysdata;
302                         pci_dev_put(pci_dev);
303                 }
304                 if (!dev->hose) {
305                         struct pci_bus *b = pci_bus_b(pci_root_buses.next);
306                         if (b)
307                                 dev->hose = b->sysdata;
308                 }
309         }
310 #endif
311
312         return 0;
313       out_free:
314         drm_free(priv, sizeof(*priv), DRM_MEM_FILES);
315         filp->private_data = NULL;
316         return ret;
317 }
318
319 /** No-op. */
320 int drm_fasync(int fd, struct file *filp, int on)
321 {
322         struct drm_file *priv = filp->private_data;
323         struct drm_device *dev = priv->minor->dev;
324         int retcode;
325
326         DRM_DEBUG("fd = %d, device = 0x%lx\n", fd,
327                   (long)old_encode_dev(priv->minor->device));
328         retcode = fasync_helper(fd, filp, on, &dev->buf_async);
329         if (retcode < 0)
330                 return retcode;
331         return 0;
332 }
333 EXPORT_SYMBOL(drm_fasync);
334
335 static void drm_object_release(struct file *filp)
336 {
337         struct drm_file *priv = filp->private_data;
338         struct list_head *head;
339         struct drm_ref_object *ref_object;
340         int i;
341
342         /*
343          * Free leftover ref objects created by me. Note that we cannot use
344          * list_for_each() here, as the struct_mutex may be temporarily
345          * released by the remove_() functions, and thus the lists may be
346          * altered.
347          * Also, a drm_remove_ref_object() will not remove it
348          * from the list unless its refcount is 1.
349          */
350
351         head = &priv->refd_objects;
352         while (head->next != head) {
353                 ref_object = list_entry(head->next, struct drm_ref_object, list);
354                 drm_remove_ref_object(priv, ref_object);
355                 head = &priv->refd_objects;
356         }
357
358         for (i = 0; i < _DRM_NO_REF_TYPES; ++i)
359                 drm_ht_remove(&priv->refd_object_hash[i]);
360 }
361
362 /**
363  * Release file.
364  *
365  * \param inode device inode
366  * \param file_priv DRM file private.
367  * \return zero on success or a negative number on failure.
368  *
369  * If the hardware lock is held then free it, and take it again for the kernel
370  * context since it's necessary to reclaim buffers. Unlink the file private
371  * data from its list and free it. Decreases the open count and if it reaches
372  * zero calls drm_lastclose().
373  */
374 int drm_release(struct inode *inode, struct file *filp)
375 {
376         struct drm_file *file_priv = filp->private_data;
377         struct drm_device *dev = file_priv->minor->dev;
378         int retcode = 0;
379
380         lock_kernel();
381
382         DRM_DEBUG("open_count = %d\n", dev->open_count);
383
384         if (dev->driver->preclose)
385                 dev->driver->preclose(dev, file_priv);
386
387         /* ========================================================
388          * Begin inline drm_release
389          */
390
391         DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
392                   current->pid, (long)old_encode_dev(file_priv->minor->device),
393                   dev->open_count);
394
395         if (dev->driver->reclaim_buffers_locked && dev->lock.hw_lock) {
396                 if (drm_i_have_hw_lock(dev, file_priv)) {
397                         dev->driver->reclaim_buffers_locked(dev, file_priv);
398                 } else {
399                         unsigned long _end=jiffies + 3*DRM_HZ;
400                         int locked = 0;
401
402                         drm_idlelock_take(&dev->lock);
403
404                         /*
405                          * Wait for a while.
406                          */
407
408                         do{
409                                 spin_lock_bh(&dev->lock.spinlock);
410                                 locked = dev->lock.idle_has_lock;
411                                 spin_unlock_bh(&dev->lock.spinlock);
412                                 if (locked)
413                                         break;
414                                 schedule();
415                         } while (!time_after_eq(jiffies, _end));
416
417                         if (!locked) {
418                                 DRM_ERROR("reclaim_buffers_locked() deadlock. Please rework this\n"
419                                           "\tdriver to use reclaim_buffers_idlelocked() instead.\n"
420                                           "\tI will go on reclaiming the buffers anyway.\n");
421                         }
422
423                         dev->driver->reclaim_buffers_locked(dev, file_priv);
424                         drm_idlelock_release(&dev->lock);
425                 }
426         }
427
428         if (dev->driver->reclaim_buffers_idlelocked && dev->lock.hw_lock) {
429
430                 drm_idlelock_take(&dev->lock);
431                 dev->driver->reclaim_buffers_idlelocked(dev, file_priv);
432                 drm_idlelock_release(&dev->lock);
433
434         }
435
436         if (drm_i_have_hw_lock(dev, file_priv)) {
437                 DRM_DEBUG("File %p released, freeing lock for context %d\n",
438                           filp, _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
439
440                 drm_lock_free(&dev->lock,
441                               _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock));
442         }
443
444
445         if (drm_core_check_feature(dev, DRIVER_HAVE_DMA) &&
446             !dev->driver->reclaim_buffers_locked) {
447                 dev->driver->reclaim_buffers(dev, file_priv);
448         }
449
450         if (dev->driver->driver_features & DRIVER_GEM)
451                 drm_gem_release(dev, file_priv);
452
453         drm_fasync(-1, filp, 0);
454
455         mutex_lock(&dev->ctxlist_mutex);
456
457         if (!list_empty(&dev->ctxlist)) {
458                 struct drm_ctx_list *pos, *n;
459
460                 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
461                         if (pos->tag == file_priv &&
462                             pos->handle != DRM_KERNEL_CONTEXT) {
463                                 if (dev->driver->context_dtor)
464                                         dev->driver->context_dtor(dev,
465                                                                   pos->handle);
466
467                                 drm_ctxbitmap_free(dev, pos->handle);
468
469                                 list_del(&pos->head);
470                                 drm_free(pos, sizeof(*pos), DRM_MEM_CTXLIST);
471                                 --dev->ctx_count;
472                         }
473                 }
474         }
475         mutex_unlock(&dev->ctxlist_mutex);
476
477         mutex_lock(&dev->struct_mutex);
478         drm_object_release(filp);
479         if (file_priv->remove_auth_on_close == 1) {
480                 struct drm_file *temp;
481
482                 list_for_each_entry(temp, &dev->filelist, lhead)
483                         temp->authenticated = 0;
484         }
485         list_del(&file_priv->lhead);
486         mutex_unlock(&dev->struct_mutex);
487
488         if (dev->driver->postclose)
489                 dev->driver->postclose(dev, file_priv);
490         drm_free(file_priv, sizeof(*file_priv), DRM_MEM_FILES);
491
492         /* ========================================================
493          * End inline drm_release
494          */
495
496         atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
497         spin_lock(&dev->count_lock);
498         if (!--dev->open_count) {
499                 if (atomic_read(&dev->ioctl_count) || dev->blocked) {
500                         DRM_ERROR("Device busy: %d %d\n",
501                                   atomic_read(&dev->ioctl_count), dev->blocked);
502                         spin_unlock(&dev->count_lock);
503                         unlock_kernel();
504                         return -EBUSY;
505                 }
506                 spin_unlock(&dev->count_lock);
507                 unlock_kernel();
508                 return drm_lastclose(dev);
509         }
510         spin_unlock(&dev->count_lock);
511
512         unlock_kernel();
513
514         return retcode;
515 }
516 EXPORT_SYMBOL(drm_release);
517
518 /** No-op. */
519 /* This is to deal with older X servers that believe 0 means data is
520  * available which is not the correct return for a poll function.
521  * This cannot be fixed until the Xserver is fixed. Xserver will need
522  * to set a newer interface version to avoid breaking older Xservers.
523  * Without fixing the Xserver you get: "WaitForSomething(): select: errno=22"
524  * http://freedesktop.org/bugzilla/show_bug.cgi?id=1505 if you try
525  * to return the correct response.
526  */
527 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
528 {
529         /* return (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); */
530         return 0;
531 }
532 EXPORT_SYMBOL(drm_poll);