proc: Fix proc_sys_prune_dcache to hold a sb reference
[cris-mirror.git] / drivers / gpu / drm / drm_file.c
blob3783b659cd38a6b87f5418d8e2e6dccdb3e38800
1 /*
2 * \author Rickard E. (Rik) Faith <faith@valinux.com>
3 * \author Daryll Strauss <daryll@valinux.com>
4 * \author Gareth Hughes <gareth@valinux.com>
5 */
7 /*
8 * Created: Mon Jan 4 08:58:31 1999 by faith@valinux.com
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
23 * Software.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 * OTHER DEALINGS IN THE SOFTWARE.
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <drm/drm_file.h>
39 #include <drm/drmP.h>
41 #include "drm_legacy.h"
42 #include "drm_internal.h"
43 #include "drm_crtc_internal.h"
45 /* from BKL pushdown */
46 DEFINE_MUTEX(drm_global_mutex);
48 /**
49 * DOC: file operations
51 * Drivers must define the file operations structure that forms the DRM
52 * userspace API entry point, even though most of those operations are
53 * implemented in the DRM core. The resulting &struct file_operations must be
54 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
55 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
56 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
57 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
58 * that require 32/64 bit compatibility support must provide their own
59 * &file_operations.compat_ioctl handler that processes private ioctls and calls
60 * drm_compat_ioctl() for core ioctls.
62 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
63 * events are a generic and extensible means to send asynchronous events to
64 * userspace through the file descriptor. They are used to send vblank event and
65 * page flip completions by the KMS API. But drivers can also use it for their
66 * own needs, e.g. to signal completion of rendering.
68 * For the driver-side event interface see drm_event_reserve_init() and
69 * drm_send_event() as the main starting points.
71 * The memory mapping implementation will vary depending on how the driver
72 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
73 * function, modern drivers should use one of the provided memory-manager
74 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
75 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
77 * No other file operations are supported by the DRM userspace API. Overall the
78 * following is an example #file_operations structure::
80 * static const example_drm_fops = {
81 * .owner = THIS_MODULE,
82 * .open = drm_open,
83 * .release = drm_release,
84 * .unlocked_ioctl = drm_ioctl,
85 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
86 * .poll = drm_poll,
87 * .read = drm_read,
88 * .llseek = no_llseek,
89 * .mmap = drm_gem_mmap,
90 * };
92 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
93 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
94 * simpler.
97 static int drm_open_helper(struct file *filp, struct drm_minor *minor);
99 static int drm_setup(struct drm_device * dev)
101 int ret;
103 if (dev->driver->firstopen &&
104 drm_core_check_feature(dev, DRIVER_LEGACY)) {
105 ret = dev->driver->firstopen(dev);
106 if (ret != 0)
107 return ret;
110 ret = drm_legacy_dma_setup(dev);
111 if (ret < 0)
112 return ret;
115 DRM_DEBUG("\n");
116 return 0;
120 * drm_open - open method for DRM file
121 * @inode: device inode
122 * @filp: file pointer.
124 * This function must be used by drivers as their &file_operations.open method.
125 * It looks up the correct DRM device and instantiates all the per-file
126 * resources for it. It also calls the &drm_driver.open driver callback.
128 * RETURNS:
130 * 0 on success or negative errno value on falure.
132 int drm_open(struct inode *inode, struct file *filp)
134 struct drm_device *dev;
135 struct drm_minor *minor;
136 int retcode;
137 int need_setup = 0;
139 minor = drm_minor_acquire(iminor(inode));
140 if (IS_ERR(minor))
141 return PTR_ERR(minor);
143 dev = minor->dev;
144 if (!dev->open_count++)
145 need_setup = 1;
147 /* share address_space across all char-devs of a single device */
148 filp->f_mapping = dev->anon_inode->i_mapping;
150 retcode = drm_open_helper(filp, minor);
151 if (retcode)
152 goto err_undo;
153 if (need_setup) {
154 retcode = drm_setup(dev);
155 if (retcode)
156 goto err_undo;
158 return 0;
160 err_undo:
161 dev->open_count--;
162 drm_minor_release(minor);
163 return retcode;
165 EXPORT_SYMBOL(drm_open);
168 * Check whether DRI will run on this CPU.
170 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
172 static int drm_cpu_valid(void)
174 #if defined(__sparc__) && !defined(__sparc_v9__)
175 return 0; /* No cmpxchg before v9 sparc. */
176 #endif
177 return 1;
181 * Called whenever a process opens /dev/drm.
183 * \param filp file pointer.
184 * \param minor acquired minor-object.
185 * \return zero on success or a negative number on failure.
187 * Creates and initializes a drm_file structure for the file private data in \p
188 * filp and add it into the double linked list in \p dev.
190 static int drm_open_helper(struct file *filp, struct drm_minor *minor)
192 struct drm_device *dev = minor->dev;
193 struct drm_file *priv;
194 int ret;
196 if (filp->f_flags & O_EXCL)
197 return -EBUSY; /* No exclusive opens */
198 if (!drm_cpu_valid())
199 return -EINVAL;
200 if (dev->switch_power_state != DRM_SWITCH_POWER_ON && dev->switch_power_state != DRM_SWITCH_POWER_DYNAMIC_OFF)
201 return -EINVAL;
203 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
205 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
206 if (!priv)
207 return -ENOMEM;
209 filp->private_data = priv;
210 priv->filp = filp;
211 priv->pid = get_pid(task_pid(current));
212 priv->minor = minor;
214 /* for compatibility root is always authenticated */
215 priv->authenticated = capable(CAP_SYS_ADMIN);
216 priv->lock_count = 0;
218 INIT_LIST_HEAD(&priv->lhead);
219 INIT_LIST_HEAD(&priv->fbs);
220 mutex_init(&priv->fbs_lock);
221 INIT_LIST_HEAD(&priv->blobs);
222 INIT_LIST_HEAD(&priv->pending_event_list);
223 INIT_LIST_HEAD(&priv->event_list);
224 init_waitqueue_head(&priv->event_wait);
225 priv->event_space = 4096; /* set aside 4k for event buffer */
227 mutex_init(&priv->event_read_lock);
229 if (drm_core_check_feature(dev, DRIVER_GEM))
230 drm_gem_open(dev, priv);
232 if (drm_core_check_feature(dev, DRIVER_PRIME))
233 drm_prime_init_file_private(&priv->prime);
235 if (dev->driver->open) {
236 ret = dev->driver->open(dev, priv);
237 if (ret < 0)
238 goto out_prime_destroy;
241 if (drm_is_primary_client(priv)) {
242 ret = drm_master_open(priv);
243 if (ret)
244 goto out_close;
247 mutex_lock(&dev->filelist_mutex);
248 list_add(&priv->lhead, &dev->filelist);
249 mutex_unlock(&dev->filelist_mutex);
251 #ifdef __alpha__
253 * Default the hose
255 if (!dev->hose) {
256 struct pci_dev *pci_dev;
257 pci_dev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
258 if (pci_dev) {
259 dev->hose = pci_dev->sysdata;
260 pci_dev_put(pci_dev);
262 if (!dev->hose) {
263 struct pci_bus *b = list_entry(pci_root_buses.next,
264 struct pci_bus, node);
265 if (b)
266 dev->hose = b->sysdata;
269 #endif
271 return 0;
273 out_close:
274 if (dev->driver->postclose)
275 dev->driver->postclose(dev, priv);
276 out_prime_destroy:
277 if (drm_core_check_feature(dev, DRIVER_PRIME))
278 drm_prime_destroy_file_private(&priv->prime);
279 if (drm_core_check_feature(dev, DRIVER_GEM))
280 drm_gem_release(dev, priv);
281 put_pid(priv->pid);
282 kfree(priv);
283 filp->private_data = NULL;
284 return ret;
287 static void drm_events_release(struct drm_file *file_priv)
289 struct drm_device *dev = file_priv->minor->dev;
290 struct drm_pending_event *e, *et;
291 unsigned long flags;
293 spin_lock_irqsave(&dev->event_lock, flags);
295 /* Unlink pending events */
296 list_for_each_entry_safe(e, et, &file_priv->pending_event_list,
297 pending_link) {
298 list_del(&e->pending_link);
299 e->file_priv = NULL;
302 /* Remove unconsumed events */
303 list_for_each_entry_safe(e, et, &file_priv->event_list, link) {
304 list_del(&e->link);
305 kfree(e);
308 spin_unlock_irqrestore(&dev->event_lock, flags);
311 static void drm_legacy_dev_reinit(struct drm_device *dev)
313 if (dev->irq_enabled)
314 drm_irq_uninstall(dev);
316 mutex_lock(&dev->struct_mutex);
318 drm_legacy_agp_clear(dev);
320 drm_legacy_sg_cleanup(dev);
321 drm_legacy_vma_flush(dev);
322 drm_legacy_dma_takedown(dev);
324 mutex_unlock(&dev->struct_mutex);
326 dev->sigdata.lock = NULL;
328 dev->context_flag = 0;
329 dev->last_context = 0;
330 dev->if_version = 0;
332 DRM_DEBUG("lastclose completed\n");
335 void drm_lastclose(struct drm_device * dev)
337 DRM_DEBUG("\n");
339 if (dev->driver->lastclose)
340 dev->driver->lastclose(dev);
341 DRM_DEBUG("driver lastclose completed\n");
343 if (drm_core_check_feature(dev, DRIVER_LEGACY))
344 drm_legacy_dev_reinit(dev);
348 * drm_release - release method for DRM file
349 * @inode: device inode
350 * @filp: file pointer.
352 * This function must be used by drivers as their &file_operations.release
353 * method. It frees any resources associated with the open file, and calls the
354 * &drm_driver.preclose and &drm_driver.lastclose driver callbacks. If this is
355 * the last open file for the DRM device also proceeds to call the
356 * &drm_driver.lastclose driver callback.
358 * RETURNS:
360 * Always succeeds and returns 0.
362 int drm_release(struct inode *inode, struct file *filp)
364 struct drm_file *file_priv = filp->private_data;
365 struct drm_minor *minor = file_priv->minor;
366 struct drm_device *dev = minor->dev;
368 mutex_lock(&drm_global_mutex);
370 DRM_DEBUG("open_count = %d\n", dev->open_count);
372 mutex_lock(&dev->filelist_mutex);
373 list_del(&file_priv->lhead);
374 mutex_unlock(&dev->filelist_mutex);
376 if (dev->driver->preclose)
377 dev->driver->preclose(dev, file_priv);
379 /* ========================================================
380 * Begin inline drm_release
383 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
384 task_pid_nr(current),
385 (long)old_encode_dev(file_priv->minor->kdev->devt),
386 dev->open_count);
388 if (drm_core_check_feature(dev, DRIVER_LEGACY))
389 drm_legacy_lock_release(dev, filp);
391 if (drm_core_check_feature(dev, DRIVER_HAVE_DMA))
392 drm_legacy_reclaim_buffers(dev, file_priv);
394 drm_events_release(file_priv);
396 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
397 drm_fb_release(file_priv);
398 drm_property_destroy_user_blobs(dev, file_priv);
401 if (drm_core_check_feature(dev, DRIVER_GEM))
402 drm_gem_release(dev, file_priv);
404 drm_legacy_ctxbitmap_flush(dev, file_priv);
406 if (drm_is_primary_client(file_priv))
407 drm_master_release(file_priv);
409 if (dev->driver->postclose)
410 dev->driver->postclose(dev, file_priv);
412 if (drm_core_check_feature(dev, DRIVER_PRIME))
413 drm_prime_destroy_file_private(&file_priv->prime);
415 WARN_ON(!list_empty(&file_priv->event_list));
417 put_pid(file_priv->pid);
418 kfree(file_priv);
420 /* ========================================================
421 * End inline drm_release
424 if (!--dev->open_count) {
425 drm_lastclose(dev);
426 if (drm_device_is_unplugged(dev))
427 drm_put_dev(dev);
429 mutex_unlock(&drm_global_mutex);
431 drm_minor_release(minor);
433 return 0;
435 EXPORT_SYMBOL(drm_release);
438 * drm_read - read method for DRM file
439 * @filp: file pointer
440 * @buffer: userspace destination pointer for the read
441 * @count: count in bytes to read
442 * @offset: offset to read
444 * This function must be used by drivers as their &file_operations.read
445 * method iff they use DRM events for asynchronous signalling to userspace.
446 * Since events are used by the KMS API for vblank and page flip completion this
447 * means all modern display drivers must use it.
449 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
450 * must set the &file_operation.llseek to no_llseek(). Polling support is
451 * provided by drm_poll().
453 * This function will only ever read a full event. Therefore userspace must
454 * supply a big enough buffer to fit any event to ensure forward progress. Since
455 * the maximum event space is currently 4K it's recommended to just use that for
456 * safety.
458 * RETURNS:
460 * Number of bytes read (always aligned to full events, and can be 0) or a
461 * negative error code on failure.
463 ssize_t drm_read(struct file *filp, char __user *buffer,
464 size_t count, loff_t *offset)
466 struct drm_file *file_priv = filp->private_data;
467 struct drm_device *dev = file_priv->minor->dev;
468 ssize_t ret;
470 if (!access_ok(VERIFY_WRITE, buffer, count))
471 return -EFAULT;
473 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
474 if (ret)
475 return ret;
477 for (;;) {
478 struct drm_pending_event *e = NULL;
480 spin_lock_irq(&dev->event_lock);
481 if (!list_empty(&file_priv->event_list)) {
482 e = list_first_entry(&file_priv->event_list,
483 struct drm_pending_event, link);
484 file_priv->event_space += e->event->length;
485 list_del(&e->link);
487 spin_unlock_irq(&dev->event_lock);
489 if (e == NULL) {
490 if (ret)
491 break;
493 if (filp->f_flags & O_NONBLOCK) {
494 ret = -EAGAIN;
495 break;
498 mutex_unlock(&file_priv->event_read_lock);
499 ret = wait_event_interruptible(file_priv->event_wait,
500 !list_empty(&file_priv->event_list));
501 if (ret >= 0)
502 ret = mutex_lock_interruptible(&file_priv->event_read_lock);
503 if (ret)
504 return ret;
505 } else {
506 unsigned length = e->event->length;
508 if (length > count - ret) {
509 put_back_event:
510 spin_lock_irq(&dev->event_lock);
511 file_priv->event_space -= length;
512 list_add(&e->link, &file_priv->event_list);
513 spin_unlock_irq(&dev->event_lock);
514 break;
517 if (copy_to_user(buffer + ret, e->event, length)) {
518 if (ret == 0)
519 ret = -EFAULT;
520 goto put_back_event;
523 ret += length;
524 kfree(e);
527 mutex_unlock(&file_priv->event_read_lock);
529 return ret;
531 EXPORT_SYMBOL(drm_read);
534 * drm_poll - poll method for DRM file
535 * @filp: file pointer
536 * @wait: poll waiter table
538 * This function must be used by drivers as their &file_operations.read method
539 * iff they use DRM events for asynchronous signalling to userspace. Since
540 * events are used by the KMS API for vblank and page flip completion this means
541 * all modern display drivers must use it.
543 * See also drm_read().
545 * RETURNS:
547 * Mask of POLL flags indicating the current status of the file.
549 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait)
551 struct drm_file *file_priv = filp->private_data;
552 unsigned int mask = 0;
554 poll_wait(filp, &file_priv->event_wait, wait);
556 if (!list_empty(&file_priv->event_list))
557 mask |= POLLIN | POLLRDNORM;
559 return mask;
561 EXPORT_SYMBOL(drm_poll);
564 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
565 * @dev: DRM device
566 * @file_priv: DRM file private data
567 * @p: tracking structure for the pending event
568 * @e: actual event data to deliver to userspace
570 * This function prepares the passed in event for eventual delivery. If the event
571 * doesn't get delivered (because the IOCTL fails later on, before queuing up
572 * anything) then the even must be cancelled and freed using
573 * drm_event_cancel_free(). Successfully initialized events should be sent out
574 * using drm_send_event() or drm_send_event_locked() to signal completion of the
575 * asynchronous event to userspace.
577 * If callers embedded @p into a larger structure it must be allocated with
578 * kmalloc and @p must be the first member element.
580 * This is the locked version of drm_event_reserve_init() for callers which
581 * already hold &drm_device.event_lock.
583 * RETURNS:
585 * 0 on success or a negative error code on failure.
587 int drm_event_reserve_init_locked(struct drm_device *dev,
588 struct drm_file *file_priv,
589 struct drm_pending_event *p,
590 struct drm_event *e)
592 if (file_priv->event_space < e->length)
593 return -ENOMEM;
595 file_priv->event_space -= e->length;
597 p->event = e;
598 list_add(&p->pending_link, &file_priv->pending_event_list);
599 p->file_priv = file_priv;
601 return 0;
603 EXPORT_SYMBOL(drm_event_reserve_init_locked);
606 * drm_event_reserve_init - init a DRM event and reserve space for it
607 * @dev: DRM device
608 * @file_priv: DRM file private data
609 * @p: tracking structure for the pending event
610 * @e: actual event data to deliver to userspace
612 * This function prepares the passed in event for eventual delivery. If the event
613 * doesn't get delivered (because the IOCTL fails later on, before queuing up
614 * anything) then the even must be cancelled and freed using
615 * drm_event_cancel_free(). Successfully initialized events should be sent out
616 * using drm_send_event() or drm_send_event_locked() to signal completion of the
617 * asynchronous event to userspace.
619 * If callers embedded @p into a larger structure it must be allocated with
620 * kmalloc and @p must be the first member element.
622 * Callers which already hold &drm_device.event_lock should use
623 * drm_event_reserve_init_locked() instead.
625 * RETURNS:
627 * 0 on success or a negative error code on failure.
629 int drm_event_reserve_init(struct drm_device *dev,
630 struct drm_file *file_priv,
631 struct drm_pending_event *p,
632 struct drm_event *e)
634 unsigned long flags;
635 int ret;
637 spin_lock_irqsave(&dev->event_lock, flags);
638 ret = drm_event_reserve_init_locked(dev, file_priv, p, e);
639 spin_unlock_irqrestore(&dev->event_lock, flags);
641 return ret;
643 EXPORT_SYMBOL(drm_event_reserve_init);
646 * drm_event_cancel_free - free a DRM event and release it's space
647 * @dev: DRM device
648 * @p: tracking structure for the pending event
650 * This function frees the event @p initialized with drm_event_reserve_init()
651 * and releases any allocated space. It is used to cancel an event when the
652 * nonblocking operation could not be submitted and needed to be aborted.
654 void drm_event_cancel_free(struct drm_device *dev,
655 struct drm_pending_event *p)
657 unsigned long flags;
658 spin_lock_irqsave(&dev->event_lock, flags);
659 if (p->file_priv) {
660 p->file_priv->event_space += p->event->length;
661 list_del(&p->pending_link);
663 spin_unlock_irqrestore(&dev->event_lock, flags);
665 if (p->fence)
666 dma_fence_put(p->fence);
668 kfree(p);
670 EXPORT_SYMBOL(drm_event_cancel_free);
673 * drm_send_event_locked - send DRM event to file descriptor
674 * @dev: DRM device
675 * @e: DRM event to deliver
677 * This function sends the event @e, initialized with drm_event_reserve_init(),
678 * to its associated userspace DRM file. Callers must already hold
679 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
681 * Note that the core will take care of unlinking and disarming events when the
682 * corresponding DRM file is closed. Drivers need not worry about whether the
683 * DRM file for this event still exists and can call this function upon
684 * completion of the asynchronous work unconditionally.
686 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e)
688 assert_spin_locked(&dev->event_lock);
690 if (e->completion) {
691 complete_all(e->completion);
692 e->completion_release(e->completion);
693 e->completion = NULL;
696 if (e->fence) {
697 dma_fence_signal(e->fence);
698 dma_fence_put(e->fence);
701 if (!e->file_priv) {
702 kfree(e);
703 return;
706 list_del(&e->pending_link);
707 list_add_tail(&e->link,
708 &e->file_priv->event_list);
709 wake_up_interruptible(&e->file_priv->event_wait);
711 EXPORT_SYMBOL(drm_send_event_locked);
714 * drm_send_event - send DRM event to file descriptor
715 * @dev: DRM device
716 * @e: DRM event to deliver
718 * This function sends the event @e, initialized with drm_event_reserve_init(),
719 * to its associated userspace DRM file. This function acquires
720 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
721 * hold this lock.
723 * Note that the core will take care of unlinking and disarming events when the
724 * corresponding DRM file is closed. Drivers need not worry about whether the
725 * DRM file for this event still exists and can call this function upon
726 * completion of the asynchronous work unconditionally.
728 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e)
730 unsigned long irqflags;
732 spin_lock_irqsave(&dev->event_lock, irqflags);
733 drm_send_event_locked(dev, e);
734 spin_unlock_irqrestore(&dev->event_lock, irqflags);
736 EXPORT_SYMBOL(drm_send_event);