2 * \author Rickard E. (Rik) Faith <faith@valinux.com>
3 * \author Daryll Strauss <daryll@valinux.com>
4 * \author Gareth Hughes <gareth@valinux.com>
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
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/anon_inodes.h>
35 #include <linux/dma-fence.h>
36 #include <linux/file.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/poll.h>
40 #include <linux/slab.h>
42 #include <drm/drm_client.h>
43 #include <drm/drm_drv.h>
44 #include <drm/drm_file.h>
45 #include <drm/drm_print.h>
47 #include "drm_crtc_internal.h"
48 #include "drm_internal.h"
49 #include "drm_legacy.h"
51 /* from BKL pushdown */
52 DEFINE_MUTEX(drm_global_mutex
);
55 * DOC: file operations
57 * Drivers must define the file operations structure that forms the DRM
58 * userspace API entry point, even though most of those operations are
59 * implemented in the DRM core. The resulting &struct file_operations must be
60 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
61 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
62 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
63 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
64 * that require 32/64 bit compatibility support must provide their own
65 * &file_operations.compat_ioctl handler that processes private ioctls and calls
66 * drm_compat_ioctl() for core ioctls.
68 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
69 * events are a generic and extensible means to send asynchronous events to
70 * userspace through the file descriptor. They are used to send vblank event and
71 * page flip completions by the KMS API. But drivers can also use it for their
72 * own needs, e.g. to signal completion of rendering.
74 * For the driver-side event interface see drm_event_reserve_init() and
75 * drm_send_event() as the main starting points.
77 * The memory mapping implementation will vary depending on how the driver
78 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
79 * function, modern drivers should use one of the provided memory-manager
80 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
81 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
83 * No other file operations are supported by the DRM userspace API. Overall the
84 * following is an example &file_operations structure::
86 * static const example_drm_fops = {
87 * .owner = THIS_MODULE,
89 * .release = drm_release,
90 * .unlocked_ioctl = drm_ioctl,
91 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
94 * .llseek = no_llseek,
95 * .mmap = drm_gem_mmap,
98 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
99 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
102 * The driver's &file_operations must be stored in &drm_driver.fops.
104 * For driver-private IOCTL handling see the more detailed discussion in
105 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
109 * drm_file_alloc - allocate file context
110 * @minor: minor to allocate on
112 * This allocates a new DRM file context. It is not linked into any context and
113 * can be used by the caller freely. Note that the context keeps a pointer to
114 * @minor, so it must be freed before @minor is.
117 * Pointer to newly allocated context, ERR_PTR on failure.
119 struct drm_file
*drm_file_alloc(struct drm_minor
*minor
)
121 struct drm_device
*dev
= minor
->dev
;
122 struct drm_file
*file
;
125 file
= kzalloc(sizeof(*file
), GFP_KERNEL
);
127 return ERR_PTR(-ENOMEM
);
129 file
->pid
= get_pid(task_pid(current
));
132 /* for compatibility root is always authenticated */
133 file
->authenticated
= capable(CAP_SYS_ADMIN
);
135 INIT_LIST_HEAD(&file
->lhead
);
136 INIT_LIST_HEAD(&file
->fbs
);
137 mutex_init(&file
->fbs_lock
);
138 INIT_LIST_HEAD(&file
->blobs
);
139 INIT_LIST_HEAD(&file
->pending_event_list
);
140 INIT_LIST_HEAD(&file
->event_list
);
141 init_waitqueue_head(&file
->event_wait
);
142 file
->event_space
= 4096; /* set aside 4k for event buffer */
144 mutex_init(&file
->event_read_lock
);
146 if (drm_core_check_feature(dev
, DRIVER_GEM
))
147 drm_gem_open(dev
, file
);
149 if (drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
150 drm_syncobj_open(file
);
152 drm_prime_init_file_private(&file
->prime
);
154 if (dev
->driver
->open
) {
155 ret
= dev
->driver
->open(dev
, file
);
157 goto out_prime_destroy
;
163 drm_prime_destroy_file_private(&file
->prime
);
164 if (drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
165 drm_syncobj_release(file
);
166 if (drm_core_check_feature(dev
, DRIVER_GEM
))
167 drm_gem_release(dev
, file
);
174 static void drm_events_release(struct drm_file
*file_priv
)
176 struct drm_device
*dev
= file_priv
->minor
->dev
;
177 struct drm_pending_event
*e
, *et
;
180 spin_lock_irqsave(&dev
->event_lock
, flags
);
182 /* Unlink pending events */
183 list_for_each_entry_safe(e
, et
, &file_priv
->pending_event_list
,
185 list_del(&e
->pending_link
);
189 /* Remove unconsumed events */
190 list_for_each_entry_safe(e
, et
, &file_priv
->event_list
, link
) {
195 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
199 * drm_file_free - free file context
200 * @file: context to free, or NULL
202 * This destroys and deallocates a DRM file context previously allocated via
203 * drm_file_alloc(). The caller must make sure to unlink it from any contexts
204 * before calling this.
206 * If NULL is passed, this is a no-op.
209 * 0 on success, or error code on failure.
211 void drm_file_free(struct drm_file
*file
)
213 struct drm_device
*dev
;
218 dev
= file
->minor
->dev
;
220 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
221 task_pid_nr(current
),
222 (long)old_encode_dev(file
->minor
->kdev
->devt
),
225 if (drm_core_check_feature(dev
, DRIVER_LEGACY
) &&
226 dev
->driver
->preclose
)
227 dev
->driver
->preclose(dev
, file
);
229 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
230 drm_legacy_lock_release(dev
, file
->filp
);
232 if (drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
233 drm_legacy_reclaim_buffers(dev
, file
);
235 drm_events_release(file
);
237 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
238 drm_fb_release(file
);
239 drm_property_destroy_user_blobs(dev
, file
);
242 if (drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
243 drm_syncobj_release(file
);
245 if (drm_core_check_feature(dev
, DRIVER_GEM
))
246 drm_gem_release(dev
, file
);
248 drm_legacy_ctxbitmap_flush(dev
, file
);
250 if (drm_is_primary_client(file
))
251 drm_master_release(file
);
253 if (dev
->driver
->postclose
)
254 dev
->driver
->postclose(dev
, file
);
256 drm_prime_destroy_file_private(&file
->prime
);
258 WARN_ON(!list_empty(&file
->event_list
));
264 static void drm_close_helper(struct file
*filp
)
266 struct drm_file
*file_priv
= filp
->private_data
;
267 struct drm_device
*dev
= file_priv
->minor
->dev
;
269 mutex_lock(&dev
->filelist_mutex
);
270 list_del(&file_priv
->lhead
);
271 mutex_unlock(&dev
->filelist_mutex
);
273 drm_file_free(file_priv
);
277 * Check whether DRI will run on this CPU.
279 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
281 static int drm_cpu_valid(void)
283 #if defined(__sparc__) && !defined(__sparc_v9__)
284 return 0; /* No cmpxchg before v9 sparc. */
290 * Called whenever a process opens a drm node
292 * \param filp file pointer.
293 * \param minor acquired minor-object.
294 * \return zero on success or a negative number on failure.
296 * Creates and initializes a drm_file structure for the file private data in \p
297 * filp and add it into the double linked list in \p dev.
299 static int drm_open_helper(struct file
*filp
, struct drm_minor
*minor
)
301 struct drm_device
*dev
= minor
->dev
;
302 struct drm_file
*priv
;
305 if (filp
->f_flags
& O_EXCL
)
306 return -EBUSY
; /* No exclusive opens */
307 if (!drm_cpu_valid())
309 if (dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
&& dev
->switch_power_state
!= DRM_SWITCH_POWER_DYNAMIC_OFF
)
312 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current
), minor
->index
);
314 priv
= drm_file_alloc(minor
);
316 return PTR_ERR(priv
);
318 if (drm_is_primary_client(priv
)) {
319 ret
= drm_master_open(priv
);
326 filp
->private_data
= priv
;
327 filp
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
330 mutex_lock(&dev
->filelist_mutex
);
331 list_add(&priv
->lhead
, &dev
->filelist
);
332 mutex_unlock(&dev
->filelist_mutex
);
339 struct pci_dev
*pci_dev
;
340 pci_dev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, NULL
);
342 dev
->hose
= pci_dev
->sysdata
;
343 pci_dev_put(pci_dev
);
346 struct pci_bus
*b
= list_entry(pci_root_buses
.next
,
347 struct pci_bus
, node
);
349 dev
->hose
= b
->sysdata
;
358 * drm_open - open method for DRM file
359 * @inode: device inode
360 * @filp: file pointer.
362 * This function must be used by drivers as their &file_operations.open method.
363 * It looks up the correct DRM device and instantiates all the per-file
364 * resources for it. It also calls the &drm_driver.open driver callback.
368 * 0 on success or negative errno value on falure.
370 int drm_open(struct inode
*inode
, struct file
*filp
)
372 struct drm_device
*dev
;
373 struct drm_minor
*minor
;
377 minor
= drm_minor_acquire(iminor(inode
));
379 return PTR_ERR(minor
);
382 if (!dev
->open_count
++)
385 /* share address_space across all char-devs of a single device */
386 filp
->f_mapping
= dev
->anon_inode
->i_mapping
;
388 retcode
= drm_open_helper(filp
, minor
);
392 retcode
= drm_legacy_setup(dev
);
394 drm_close_helper(filp
);
402 drm_minor_release(minor
);
405 EXPORT_SYMBOL(drm_open
);
407 void drm_lastclose(struct drm_device
* dev
)
411 if (dev
->driver
->lastclose
)
412 dev
->driver
->lastclose(dev
);
413 DRM_DEBUG("driver lastclose completed\n");
415 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
416 drm_legacy_dev_reinit(dev
);
418 drm_client_dev_restore(dev
);
422 * drm_release - release method for DRM file
423 * @inode: device inode
424 * @filp: file pointer.
426 * This function must be used by drivers as their &file_operations.release
427 * method. It frees any resources associated with the open file, and calls the
428 * &drm_driver.postclose driver callback. If this is the last open file for the
429 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
433 * Always succeeds and returns 0.
435 int drm_release(struct inode
*inode
, struct file
*filp
)
437 struct drm_file
*file_priv
= filp
->private_data
;
438 struct drm_minor
*minor
= file_priv
->minor
;
439 struct drm_device
*dev
= minor
->dev
;
441 mutex_lock(&drm_global_mutex
);
443 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
445 drm_close_helper(filp
);
447 if (!--dev
->open_count
)
450 mutex_unlock(&drm_global_mutex
);
452 drm_minor_release(minor
);
456 EXPORT_SYMBOL(drm_release
);
459 * drm_read - read method for DRM file
460 * @filp: file pointer
461 * @buffer: userspace destination pointer for the read
462 * @count: count in bytes to read
463 * @offset: offset to read
465 * This function must be used by drivers as their &file_operations.read
466 * method iff they use DRM events for asynchronous signalling to userspace.
467 * Since events are used by the KMS API for vblank and page flip completion this
468 * means all modern display drivers must use it.
470 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
471 * must set the &file_operation.llseek to no_llseek(). Polling support is
472 * provided by drm_poll().
474 * This function will only ever read a full event. Therefore userspace must
475 * supply a big enough buffer to fit any event to ensure forward progress. Since
476 * the maximum event space is currently 4K it's recommended to just use that for
481 * Number of bytes read (always aligned to full events, and can be 0) or a
482 * negative error code on failure.
484 ssize_t
drm_read(struct file
*filp
, char __user
*buffer
,
485 size_t count
, loff_t
*offset
)
487 struct drm_file
*file_priv
= filp
->private_data
;
488 struct drm_device
*dev
= file_priv
->minor
->dev
;
491 if (!access_ok(buffer
, count
))
494 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
499 struct drm_pending_event
*e
= NULL
;
501 spin_lock_irq(&dev
->event_lock
);
502 if (!list_empty(&file_priv
->event_list
)) {
503 e
= list_first_entry(&file_priv
->event_list
,
504 struct drm_pending_event
, link
);
505 file_priv
->event_space
+= e
->event
->length
;
508 spin_unlock_irq(&dev
->event_lock
);
514 if (filp
->f_flags
& O_NONBLOCK
) {
519 mutex_unlock(&file_priv
->event_read_lock
);
520 ret
= wait_event_interruptible(file_priv
->event_wait
,
521 !list_empty(&file_priv
->event_list
));
523 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
527 unsigned length
= e
->event
->length
;
529 if (length
> count
- ret
) {
531 spin_lock_irq(&dev
->event_lock
);
532 file_priv
->event_space
-= length
;
533 list_add(&e
->link
, &file_priv
->event_list
);
534 spin_unlock_irq(&dev
->event_lock
);
535 wake_up_interruptible(&file_priv
->event_wait
);
539 if (copy_to_user(buffer
+ ret
, e
->event
, length
)) {
549 mutex_unlock(&file_priv
->event_read_lock
);
553 EXPORT_SYMBOL(drm_read
);
556 * drm_poll - poll method for DRM file
557 * @filp: file pointer
558 * @wait: poll waiter table
560 * This function must be used by drivers as their &file_operations.read method
561 * iff they use DRM events for asynchronous signalling to userspace. Since
562 * events are used by the KMS API for vblank and page flip completion this means
563 * all modern display drivers must use it.
565 * See also drm_read().
569 * Mask of POLL flags indicating the current status of the file.
571 __poll_t
drm_poll(struct file
*filp
, struct poll_table_struct
*wait
)
573 struct drm_file
*file_priv
= filp
->private_data
;
576 poll_wait(filp
, &file_priv
->event_wait
, wait
);
578 if (!list_empty(&file_priv
->event_list
))
579 mask
|= EPOLLIN
| EPOLLRDNORM
;
583 EXPORT_SYMBOL(drm_poll
);
586 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
588 * @file_priv: DRM file private data
589 * @p: tracking structure for the pending event
590 * @e: actual event data to deliver to userspace
592 * This function prepares the passed in event for eventual delivery. If the event
593 * doesn't get delivered (because the IOCTL fails later on, before queuing up
594 * anything) then the even must be cancelled and freed using
595 * drm_event_cancel_free(). Successfully initialized events should be sent out
596 * using drm_send_event() or drm_send_event_locked() to signal completion of the
597 * asynchronous event to userspace.
599 * If callers embedded @p into a larger structure it must be allocated with
600 * kmalloc and @p must be the first member element.
602 * This is the locked version of drm_event_reserve_init() for callers which
603 * already hold &drm_device.event_lock.
607 * 0 on success or a negative error code on failure.
609 int drm_event_reserve_init_locked(struct drm_device
*dev
,
610 struct drm_file
*file_priv
,
611 struct drm_pending_event
*p
,
614 if (file_priv
->event_space
< e
->length
)
617 file_priv
->event_space
-= e
->length
;
620 list_add(&p
->pending_link
, &file_priv
->pending_event_list
);
621 p
->file_priv
= file_priv
;
625 EXPORT_SYMBOL(drm_event_reserve_init_locked
);
628 * drm_event_reserve_init - init a DRM event and reserve space for it
630 * @file_priv: DRM file private data
631 * @p: tracking structure for the pending event
632 * @e: actual event data to deliver to userspace
634 * This function prepares the passed in event for eventual delivery. If the event
635 * doesn't get delivered (because the IOCTL fails later on, before queuing up
636 * anything) then the even must be cancelled and freed using
637 * drm_event_cancel_free(). Successfully initialized events should be sent out
638 * using drm_send_event() or drm_send_event_locked() to signal completion of the
639 * asynchronous event to userspace.
641 * If callers embedded @p into a larger structure it must be allocated with
642 * kmalloc and @p must be the first member element.
644 * Callers which already hold &drm_device.event_lock should use
645 * drm_event_reserve_init_locked() instead.
649 * 0 on success or a negative error code on failure.
651 int drm_event_reserve_init(struct drm_device
*dev
,
652 struct drm_file
*file_priv
,
653 struct drm_pending_event
*p
,
659 spin_lock_irqsave(&dev
->event_lock
, flags
);
660 ret
= drm_event_reserve_init_locked(dev
, file_priv
, p
, e
);
661 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
665 EXPORT_SYMBOL(drm_event_reserve_init
);
668 * drm_event_cancel_free - free a DRM event and release its space
670 * @p: tracking structure for the pending event
672 * This function frees the event @p initialized with drm_event_reserve_init()
673 * and releases any allocated space. It is used to cancel an event when the
674 * nonblocking operation could not be submitted and needed to be aborted.
676 void drm_event_cancel_free(struct drm_device
*dev
,
677 struct drm_pending_event
*p
)
680 spin_lock_irqsave(&dev
->event_lock
, flags
);
682 p
->file_priv
->event_space
+= p
->event
->length
;
683 list_del(&p
->pending_link
);
685 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
688 dma_fence_put(p
->fence
);
692 EXPORT_SYMBOL(drm_event_cancel_free
);
695 * drm_send_event_locked - send DRM event to file descriptor
697 * @e: DRM event to deliver
699 * This function sends the event @e, initialized with drm_event_reserve_init(),
700 * to its associated userspace DRM file. Callers must already hold
701 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
703 * Note that the core will take care of unlinking and disarming events when the
704 * corresponding DRM file is closed. Drivers need not worry about whether the
705 * DRM file for this event still exists and can call this function upon
706 * completion of the asynchronous work unconditionally.
708 void drm_send_event_locked(struct drm_device
*dev
, struct drm_pending_event
*e
)
710 assert_spin_locked(&dev
->event_lock
);
713 complete_all(e
->completion
);
714 e
->completion_release(e
->completion
);
715 e
->completion
= NULL
;
719 dma_fence_signal(e
->fence
);
720 dma_fence_put(e
->fence
);
728 list_del(&e
->pending_link
);
729 list_add_tail(&e
->link
,
730 &e
->file_priv
->event_list
);
731 wake_up_interruptible(&e
->file_priv
->event_wait
);
733 EXPORT_SYMBOL(drm_send_event_locked
);
736 * drm_send_event - send DRM event to file descriptor
738 * @e: DRM event to deliver
740 * This function sends the event @e, initialized with drm_event_reserve_init(),
741 * to its associated userspace DRM file. This function acquires
742 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
745 * Note that the core will take care of unlinking and disarming events when the
746 * corresponding DRM file is closed. Drivers need not worry about whether the
747 * DRM file for this event still exists and can call this function upon
748 * completion of the asynchronous work unconditionally.
750 void drm_send_event(struct drm_device
*dev
, struct drm_pending_event
*e
)
752 unsigned long irqflags
;
754 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
755 drm_send_event_locked(dev
, e
);
756 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
758 EXPORT_SYMBOL(drm_send_event
);
761 * mock_drm_getfile - Create a new struct file for the drm device
762 * @minor: drm minor to wrap (e.g. #drm_device.primary)
763 * @flags: file creation mode (O_RDWR etc)
765 * This create a new struct file that wraps a DRM file context around a
766 * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without
767 * invoking userspace. The struct file may be operated on using its f_op
768 * (the drm_device.driver.fops) to mimick userspace operations, or be supplied
769 * to userspace facing functions as an internal/anonymous client.
772 * Pointer to newly created struct file, ERR_PTR on failure.
774 struct file
*mock_drm_getfile(struct drm_minor
*minor
, unsigned int flags
)
776 struct drm_device
*dev
= minor
->dev
;
777 struct drm_file
*priv
;
780 priv
= drm_file_alloc(minor
);
782 return ERR_CAST(priv
);
784 file
= anon_inode_getfile("drm", dev
->driver
->fops
, priv
, flags
);
790 /* Everyone shares a single global address space */
791 file
->f_mapping
= dev
->anon_inode
->i_mapping
;
798 EXPORT_SYMBOL_FOR_TESTS_ONLY(mock_drm_getfile
);