3 * File operations for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Daryll Strauss <daryll@valinux.com>
7 * \author Gareth Hughes <gareth@valinux.com>
11 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
13 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
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:
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
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.
38 #include <linux/poll.h>
39 #include <linux/slab.h>
40 #include <linux/module.h>
42 /* from BKL pushdown: note that nothing else serializes idr_find() */
43 DEFINE_MUTEX(drm_global_mutex);
44 EXPORT_SYMBOL(drm_global_mutex);
46 static int drm_open_helper(struct inode *inode, struct file *filp,
47 struct drm_device * dev);
49 static int drm_setup(struct drm_device * dev)
54 if (dev->driver->firstopen &&
55 !drm_core_check_feature(dev, DRIVER_MODESET)) {
56 ret = dev->driver->firstopen(dev);
61 atomic_set(&dev->ioctl_count, 0);
62 atomic_set(&dev->vma_count, 0);
64 i = drm_legacy_dma_setup(dev);
68 for (i = 0; i < ARRAY_SIZE(dev->counts); i++)
69 atomic_set(&dev->counts[i], 0);
71 dev->sigdata.lock = NULL;
73 dev->context_flag = 0;
74 dev->last_context = 0;
80 * The kernel's context could be created here, but is now created
81 * in drm_dma_enqueue. This is more resource-efficient for
82 * hardware that does not do DMA, but may mean that
83 * drm_select_queue fails between the time the interrupt is
84 * initialized and the time the queues are initialized.
93 * \param inode device inode
94 * \param filp file pointer.
95 * \return zero on success or a negative number on failure.
97 * Searches the DRM device with the same minor number, calls open_helper(), and
98 * increments the device open count. If the open count was previous at zero,
99 * i.e., it's the first that the device is open, then calls setup().
101 int drm_open(struct inode *inode, struct file *filp)
103 struct drm_device *dev = NULL;
104 int minor_id = iminor(inode);
105 struct drm_minor *minor;
108 struct address_space *old_mapping;
109 struct address_space *old_imapping;
111 minor = idr_find(&drm_minors_idr, minor_id);
115 if (!(dev = minor->dev))
118 if (drm_device_is_unplugged(dev))
121 if (!dev->open_count++)
123 mutex_lock(&dev->struct_mutex);
124 old_imapping = inode->i_mapping;
125 old_mapping = dev->dev_mapping;
126 if (old_mapping == NULL)
127 dev->dev_mapping = &inode->i_data;
128 /* ihold ensures nobody can remove inode with our i_data */
129 ihold(container_of(dev->dev_mapping, struct inode, i_data));
130 inode->i_mapping = dev->dev_mapping;
131 filp->f_mapping = dev->dev_mapping;
132 mutex_unlock(&dev->struct_mutex);
134 retcode = drm_open_helper(inode, filp, dev);
137 atomic_inc(&dev->counts[_DRM_STAT_OPENS]);
139 retcode = drm_setup(dev);
146 mutex_lock(&dev->struct_mutex);
147 filp->f_mapping = old_imapping;
148 inode->i_mapping = old_imapping;
149 iput(container_of(dev->dev_mapping, struct inode, i_data));
150 dev->dev_mapping = old_mapping;
151 mutex_unlock(&dev->struct_mutex);
155 EXPORT_SYMBOL(drm_open);
158 * File \c open operation.
160 * \param inode device inode.
161 * \param filp file pointer.
163 * Puts the dev->fops corresponding to the device minor number into
164 * \p filp, call the \c open method, and restore the file operations.
166 int drm_stub_open(struct inode *inode, struct file *filp)
168 struct drm_device *dev = NULL;
169 struct drm_minor *minor;
170 int minor_id = iminor(inode);
172 const struct file_operations *old_fops;
176 mutex_lock(&drm_global_mutex);
177 minor = idr_find(&drm_minors_idr, minor_id);
181 if (!(dev = minor->dev))
184 if (drm_device_is_unplugged(dev))
187 old_fops = filp->f_op;
188 filp->f_op = fops_get(dev->driver->fops);
189 if (filp->f_op == NULL) {
190 filp->f_op = old_fops;
193 if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
194 fops_put(filp->f_op);
195 filp->f_op = fops_get(old_fops);
200 mutex_unlock(&drm_global_mutex);
205 * Check whether DRI will run on this CPU.
207 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
209 static int drm_cpu_valid(void)
211 #if defined(__i386__)
212 if (boot_cpu_data.x86 == 3)
213 return 0; /* No cmpxchg on a 386 */
215 #if defined(__sparc__) && !defined(__sparc_v9__)
216 return 0; /* No cmpxchg before v9 sparc. */
222 * Called whenever a process opens /dev/drm.
224 * \param inode device inode.
225 * \param filp file pointer.
227 * \return zero on success or a negative number on failure.
229 * Creates and initializes a drm_file structure for the file private data in \p
230 * filp and add it into the double linked list in \p dev.
232 static int drm_open_helper(struct inode *inode, struct file *filp,
233 struct drm_device * dev)
235 int minor_id = iminor(inode);
236 struct drm_file *priv;
239 if (filp->f_flags & O_EXCL)
240 return -EBUSY; /* No exclusive opens */
241 if (!drm_cpu_valid())
243 if (dev->switch_power_state != DRM_SWITCH_POWER_ON)
246 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor_id);
248 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
252 filp->private_data = priv;
254 priv->uid = current_euid();
255 priv->pid = get_pid(task_pid(current));
256 priv->minor = idr_find(&drm_minors_idr, minor_id);
262 priv->ioctl_count = 0;
263 /* for compatibility root is always authenticated */
264 priv->authenticated = capable(CAP_SYS_ADMIN);
265 priv->lock_count = 0;
267 INIT_LIST_HEAD(&priv->lhead);
268 INIT_LIST_HEAD(&priv->fbs);
269 mutex_init(&priv->fbs_lock);
270 INIT_LIST_HEAD(&priv->event_list);
271 init_waitqueue_head(&priv->event_wait);
272 priv->event_space = 4096; /* set aside 4k for event buffer */
274 if (dev->driver->driver_features & DRIVER_GEM)
275 drm_gem_open(dev, priv);
277 if (drm_core_check_feature(dev, DRIVER_PRIME))
278 drm_prime_init_file_private(&priv->prime);
280 if (dev->driver->open) {
281 ret = dev->driver->open(dev, priv);
283 goto out_prime_destroy;
287 /* if there is no current master make this fd it */
288 mutex_lock(&dev->struct_mutex);
289 if (!priv->minor->master) {
290 /* create a new master */
291 priv->minor->master = drm_master_create(priv->minor);
292 if (!priv->minor->master) {
293 mutex_unlock(&dev->struct_mutex);
299 /* take another reference for the copy in the local file priv */
300 priv->master = drm_master_get(priv->minor->master);
302 priv->authenticated = 1;
304 mutex_unlock(&dev->struct_mutex);
305 if (dev->driver->master_create) {
306 ret = dev->driver->master_create(dev, priv->master);
308 mutex_lock(&dev->struct_mutex);
309 /* drop both references if this fails */
310 drm_master_put(&priv->minor->master);
311 drm_master_put(&priv->master);
312 mutex_unlock(&dev->struct_mutex);
316 mutex_lock(&dev->struct_mutex);
317 if (dev->driver->master_set) {
318 ret = dev->driver->master_set(dev, priv, true);
320 /* drop both references if this fails */
321 drm_master_put(&priv->minor->master);
322 drm_master_put(&priv->master);
323 mutex_unlock(&dev->struct_mutex);
327 mutex_unlock(&dev->struct_mutex);
329 /* get a reference to the master */
330 priv->master = drm_master_get(priv->minor->master);
331 mutex_unlock(&dev->struct_mutex);
334 mutex_lock(&dev->struct_mutex);
335 list_add(&priv->lhead, &dev->filelist);
336 mutex_unlock(&dev->struct_mutex);
343 struct pci_dev *pci_dev;
344 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
346 dev->hose = pci_dev->sysdata;
347 pci_dev_put(pci_dev);
350 struct pci_bus *b = pci_bus_b(pci_root_buses.next);
352 dev->hose = b->sysdata;
360 if (dev->driver->postclose)
361 dev->driver->postclose(dev, priv);
363 if (drm_core_check_feature(dev, DRIVER_PRIME))
364 drm_prime_destroy_file_private(&priv->prime);
365 if (dev->driver->driver_features & DRIVER_GEM)
366 drm_gem_release(dev, priv);
370 filp->private_data = NULL;
374 static void drm_master_release(struct drm_device *dev, struct file *filp)
376 struct drm_file *file_priv = filp->private_data;
378 if (drm_i_have_hw_lock(dev, file_priv)) {
379 DRM_DEBUG("File %p released, freeing lock for context %d\n",
380 filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
381 drm_lock_free(&file_priv->master->lock,
382 _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
386 static void drm_events_release(struct drm_file *file_priv)
388 struct drm_device *dev = file_priv->minor->dev;
389 struct drm_pending_event *e, *et;
390 struct drm_pending_vblank_event *v, *vt;
393 spin_lock_irqsave(&dev->event_lock, flags);
395 /* Remove pending flips */
396 list_for_each_entry_safe(v, vt, &dev->vblank_event_list, base.link)
397 if (v->base.file_priv == file_priv) {
398 list_del(&v->base.link);
399 drm_vblank_put(dev, v->pipe);
400 v->base.destroy(&v->base);
403 /* Remove unconsumed events */
404 list_for_each_entry_safe(e, et, &file_priv->event_list, link)
407 spin_unlock_irqrestore(&dev->event_lock, flags);
413 * \param inode device inode
414 * \param file_priv DRM file private.
415 * \return zero on success or a negative number on failure.
417 * If the hardware lock is held then free it, and take it again for the kernel
418 * context since it's necessary to reclaim buffers. Unlink the file private
419 * data from its list and free it. Decreases the open count and if it reaches
420 * zero calls drm_lastclose().
422 int drm_release(struct inode *inode, struct file *filp)
424 struct drm_file *file_priv = filp->private_data;
425 struct drm_device *dev = file_priv->minor->dev;
428 mutex_lock(&drm_global_mutex);
430 DRM_DEBUG("open_count = %d\n", dev->open_count);
432 if (dev->driver->preclose)
433 dev->driver->preclose(dev, file_priv);
435 /* ========================================================
436 * Begin inline drm_release
439 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
440 task_pid_nr(current),
441 (long)old_encode_dev(file_priv->minor->device),
444 /* Release any auth tokens that might point to this file_priv,
445 (do that under the drm_global_mutex) */
446 if (file_priv->magic)
447 (void) drm_remove_magic(file_priv->master, file_priv->magic);
449 /* if the master has gone away we can't do anything with the lock */
450 if (file_priv->minor->master)
451 drm_master_release(dev, filp);
453 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
454 drm_core_reclaim_buffers(dev, file_priv);
456 drm_events_release(file_priv);
458 if (dev->driver->driver_features & DRIVER_MODESET)
459 drm_fb_release(file_priv);
461 if (dev->driver->driver_features & DRIVER_GEM)
462 drm_gem_release(dev, file_priv);
464 drm_legacy_ctxbitmap_release(dev, file_priv);
466 mutex_lock(&dev->struct_mutex);
468 if (file_priv->is_master) {
469 struct drm_master *master = file_priv->master;
470 struct drm_file *temp;
471 list_for_each_entry(temp, &dev->filelist, lhead) {
472 if ((temp->master == file_priv->master) &&
474 temp->authenticated = 0;
478 * Since the master is disappearing, so is the
479 * possibility to lock.
482 if (master->lock.hw_lock) {
483 if (dev->sigdata.lock == master->lock.hw_lock)
484 dev->sigdata.lock = NULL;
485 master->lock.hw_lock = NULL;
486 master->lock.file_priv = NULL;
487 wake_up_interruptible_all(&master->lock.lock_queue);
490 if (file_priv->minor->master == file_priv->master) {
491 /* drop the reference held my the minor */
492 if (dev->driver->master_drop)
493 dev->driver->master_drop(dev, file_priv, true);
494 drm_master_put(&file_priv->minor->master);
498 BUG_ON(dev->dev_mapping == NULL);
499 iput(container_of(dev->dev_mapping, struct inode, i_data));
501 /* drop the reference held my the file priv */
502 drm_master_put(&file_priv->master);
503 file_priv->is_master = 0;
504 list_del(&file_priv->lhead);
505 mutex_unlock(&dev->struct_mutex);
507 if (dev->driver->postclose)
508 dev->driver->postclose(dev, file_priv);
510 if (drm_core_check_feature(dev, DRIVER_PRIME))
511 drm_prime_destroy_file_private(&file_priv->prime);
513 put_pid(file_priv->pid);
516 /* ========================================================
517 * End inline drm_release
520 atomic_inc(&dev->counts[_DRM_STAT_CLOSES]);
521 if (!--dev->open_count) {
522 if (atomic_read(&dev->ioctl_count)) {
523 DRM_ERROR("Device busy: %d\n",
524 atomic_read(&dev->ioctl_count));
527 retcode = drm_lastclose(dev);
528 if (drm_device_is_unplugged(dev))
531 mutex_unlock(&drm_global_mutex);
535 EXPORT_SYMBOL(drm_release);
538 drm_dequeue_event(struct drm_file *file_priv,
539 size_t total, size_t max, struct drm_pending_event **out)
541 struct drm_device *dev = file_priv->minor->dev;
542 struct drm_pending_event *e;
546 spin_lock_irqsave(&dev->event_lock, flags);
549 if (list_empty(&file_priv->event_list))
551 e = list_first_entry(&file_priv->event_list,
552 struct drm_pending_event, link);
553 if (e->event->length + total > max)
556 file_priv->event_space += e->event->length;
562 spin_unlock_irqrestore(&dev->event_lock, flags);
566 ssize_t drm_read(struct file *filp, char __user *buffer,
567 size_t count, loff_t *offset)
569 struct drm_file *file_priv = filp->private_data;
570 struct drm_pending_event *e;
574 ret = wait_event_interruptible(file_priv->event_wait,
575 !list_empty(&file_priv->event_list));
580 while (drm_dequeue_event(file_priv, total, count, &e)) {
581 if (copy_to_user(buffer + total,
582 e->event, e->event->length)) {
587 total += e->event->length;
593 EXPORT_SYMBOL(drm_read);
595 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
597 struct drm_file *file_priv = filp->private_data;
598 unsigned int mask = 0;
600 poll_wait(filp, &file_priv->event_wait, wait);
602 if (!list_empty(&file_priv->event_list))
603 mask |= POLLIN | POLLRDNORM;
607 EXPORT_SYMBOL(drm_poll);