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/poll.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <drm/drm_client.h>
39 #include <drm/drm_file.h>
42 #include "drm_legacy.h"
43 #include "drm_internal.h"
44 #include "drm_crtc_internal.h"
46 /* from BKL pushdown */
47 DEFINE_MUTEX(drm_global_mutex
);
50 * DOC: file operations
52 * Drivers must define the file operations structure that forms the DRM
53 * userspace API entry point, even though most of those operations are
54 * implemented in the DRM core. The resulting &struct file_operations must be
55 * stored in the &drm_driver.fops field. The mandatory functions are drm_open(),
56 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
57 * Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no
58 * need to sprinkle #ifdef into the code. Drivers which implement private ioctls
59 * that require 32/64 bit compatibility support must provide their own
60 * &file_operations.compat_ioctl handler that processes private ioctls and calls
61 * drm_compat_ioctl() for core ioctls.
63 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
64 * events are a generic and extensible means to send asynchronous events to
65 * userspace through the file descriptor. They are used to send vblank event and
66 * page flip completions by the KMS API. But drivers can also use it for their
67 * own needs, e.g. to signal completion of rendering.
69 * For the driver-side event interface see drm_event_reserve_init() and
70 * drm_send_event() as the main starting points.
72 * The memory mapping implementation will vary depending on how the driver
73 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
74 * function, modern drivers should use one of the provided memory-manager
75 * specific implementations. For GEM-based drivers this is drm_gem_mmap(), and
76 * for drivers which use the CMA GEM helpers it's drm_gem_cma_mmap().
78 * No other file operations are supported by the DRM userspace API. Overall the
79 * following is an example &file_operations structure::
81 * static const example_drm_fops = {
82 * .owner = THIS_MODULE,
84 * .release = drm_release,
85 * .unlocked_ioctl = drm_ioctl,
86 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
89 * .llseek = no_llseek,
90 * .mmap = drm_gem_mmap,
93 * For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS() macro, and for
94 * CMA based drivers there is the DEFINE_DRM_GEM_CMA_FOPS() macro to make this
97 * The driver's &file_operations must be stored in &drm_driver.fops.
99 * For driver-private IOCTL handling see the more detailed discussion in
100 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
103 static int drm_open_helper(struct file
*filp
, struct drm_minor
*minor
);
106 * drm_file_alloc - allocate file context
107 * @minor: minor to allocate on
109 * This allocates a new DRM file context. It is not linked into any context and
110 * can be used by the caller freely. Note that the context keeps a pointer to
111 * @minor, so it must be freed before @minor is.
114 * Pointer to newly allocated context, ERR_PTR on failure.
116 struct drm_file
*drm_file_alloc(struct drm_minor
*minor
)
118 struct drm_device
*dev
= minor
->dev
;
119 struct drm_file
*file
;
122 file
= kzalloc(sizeof(*file
), GFP_KERNEL
);
124 return ERR_PTR(-ENOMEM
);
126 file
->pid
= get_pid(task_pid(current
));
129 /* for compatibility root is always authenticated */
130 file
->authenticated
= capable(CAP_SYS_ADMIN
);
131 file
->lock_count
= 0;
133 INIT_LIST_HEAD(&file
->lhead
);
134 INIT_LIST_HEAD(&file
->fbs
);
135 mutex_init(&file
->fbs_lock
);
136 INIT_LIST_HEAD(&file
->blobs
);
137 INIT_LIST_HEAD(&file
->pending_event_list
);
138 INIT_LIST_HEAD(&file
->event_list
);
139 init_waitqueue_head(&file
->event_wait
);
140 file
->event_space
= 4096; /* set aside 4k for event buffer */
142 mutex_init(&file
->event_read_lock
);
144 if (drm_core_check_feature(dev
, DRIVER_GEM
))
145 drm_gem_open(dev
, file
);
147 if (drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
148 drm_syncobj_open(file
);
150 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
151 drm_prime_init_file_private(&file
->prime
);
153 if (dev
->driver
->open
) {
154 ret
= dev
->driver
->open(dev
, file
);
156 goto out_prime_destroy
;
162 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
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 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
257 drm_prime_destroy_file_private(&file
->prime
);
259 WARN_ON(!list_empty(&file
->event_list
));
265 static int drm_setup(struct drm_device
* dev
)
269 if (dev
->driver
->firstopen
&&
270 drm_core_check_feature(dev
, DRIVER_LEGACY
)) {
271 ret
= dev
->driver
->firstopen(dev
);
276 ret
= drm_legacy_dma_setup(dev
);
286 * drm_open - open method for DRM file
287 * @inode: device inode
288 * @filp: file pointer.
290 * This function must be used by drivers as their &file_operations.open method.
291 * It looks up the correct DRM device and instantiates all the per-file
292 * resources for it. It also calls the &drm_driver.open driver callback.
296 * 0 on success or negative errno value on falure.
298 int drm_open(struct inode
*inode
, struct file
*filp
)
300 struct drm_device
*dev
;
301 struct drm_minor
*minor
;
305 minor
= drm_minor_acquire(iminor(inode
));
307 return PTR_ERR(minor
);
310 if (!dev
->open_count
++)
313 /* share address_space across all char-devs of a single device */
314 filp
->f_mapping
= dev
->anon_inode
->i_mapping
;
316 retcode
= drm_open_helper(filp
, minor
);
320 retcode
= drm_setup(dev
);
328 drm_minor_release(minor
);
331 EXPORT_SYMBOL(drm_open
);
334 * Check whether DRI will run on this CPU.
336 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
338 static int drm_cpu_valid(void)
340 #if defined(__sparc__) && !defined(__sparc_v9__)
341 return 0; /* No cmpxchg before v9 sparc. */
347 * Called whenever a process opens /dev/drm.
349 * \param filp file pointer.
350 * \param minor acquired minor-object.
351 * \return zero on success or a negative number on failure.
353 * Creates and initializes a drm_file structure for the file private data in \p
354 * filp and add it into the double linked list in \p dev.
356 static int drm_open_helper(struct file
*filp
, struct drm_minor
*minor
)
358 struct drm_device
*dev
= minor
->dev
;
359 struct drm_file
*priv
;
362 if (filp
->f_flags
& O_EXCL
)
363 return -EBUSY
; /* No exclusive opens */
364 if (!drm_cpu_valid())
366 if (dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
&& dev
->switch_power_state
!= DRM_SWITCH_POWER_DYNAMIC_OFF
)
369 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current
), minor
->index
);
371 priv
= drm_file_alloc(minor
);
373 return PTR_ERR(priv
);
375 if (drm_is_primary_client(priv
)) {
376 ret
= drm_master_open(priv
);
383 filp
->private_data
= priv
;
384 filp
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
387 mutex_lock(&dev
->filelist_mutex
);
388 list_add(&priv
->lhead
, &dev
->filelist
);
389 mutex_unlock(&dev
->filelist_mutex
);
396 struct pci_dev
*pci_dev
;
397 pci_dev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, NULL
);
399 dev
->hose
= pci_dev
->sysdata
;
400 pci_dev_put(pci_dev
);
403 struct pci_bus
*b
= list_entry(pci_root_buses
.next
,
404 struct pci_bus
, node
);
406 dev
->hose
= b
->sysdata
;
414 static void drm_legacy_dev_reinit(struct drm_device
*dev
)
416 if (dev
->irq_enabled
)
417 drm_irq_uninstall(dev
);
419 mutex_lock(&dev
->struct_mutex
);
421 drm_legacy_agp_clear(dev
);
423 drm_legacy_sg_cleanup(dev
);
424 drm_legacy_vma_flush(dev
);
425 drm_legacy_dma_takedown(dev
);
427 mutex_unlock(&dev
->struct_mutex
);
429 dev
->sigdata
.lock
= NULL
;
431 dev
->context_flag
= 0;
432 dev
->last_context
= 0;
435 DRM_DEBUG("lastclose completed\n");
438 void drm_lastclose(struct drm_device
* dev
)
442 if (dev
->driver
->lastclose
)
443 dev
->driver
->lastclose(dev
);
444 DRM_DEBUG("driver lastclose completed\n");
446 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
447 drm_legacy_dev_reinit(dev
);
449 drm_client_dev_restore(dev
);
453 * drm_release - release method for DRM file
454 * @inode: device inode
455 * @filp: file pointer.
457 * This function must be used by drivers as their &file_operations.release
458 * method. It frees any resources associated with the open file, and calls the
459 * &drm_driver.postclose driver callback. If this is the last open file for the
460 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
464 * Always succeeds and returns 0.
466 int drm_release(struct inode
*inode
, struct file
*filp
)
468 struct drm_file
*file_priv
= filp
->private_data
;
469 struct drm_minor
*minor
= file_priv
->minor
;
470 struct drm_device
*dev
= minor
->dev
;
472 mutex_lock(&drm_global_mutex
);
474 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
476 mutex_lock(&dev
->filelist_mutex
);
477 list_del(&file_priv
->lhead
);
478 mutex_unlock(&dev
->filelist_mutex
);
480 drm_file_free(file_priv
);
482 if (!--dev
->open_count
)
485 mutex_unlock(&drm_global_mutex
);
487 drm_minor_release(minor
);
491 EXPORT_SYMBOL(drm_release
);
494 * drm_read - read method for DRM file
495 * @filp: file pointer
496 * @buffer: userspace destination pointer for the read
497 * @count: count in bytes to read
498 * @offset: offset to read
500 * This function must be used by drivers as their &file_operations.read
501 * method iff they use DRM events for asynchronous signalling to userspace.
502 * Since events are used by the KMS API for vblank and page flip completion this
503 * means all modern display drivers must use it.
505 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
506 * must set the &file_operation.llseek to no_llseek(). Polling support is
507 * provided by drm_poll().
509 * This function will only ever read a full event. Therefore userspace must
510 * supply a big enough buffer to fit any event to ensure forward progress. Since
511 * the maximum event space is currently 4K it's recommended to just use that for
516 * Number of bytes read (always aligned to full events, and can be 0) or a
517 * negative error code on failure.
519 ssize_t
drm_read(struct file
*filp
, char __user
*buffer
,
520 size_t count
, loff_t
*offset
)
522 struct drm_file
*file_priv
= filp
->private_data
;
523 struct drm_device
*dev
= file_priv
->minor
->dev
;
526 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
529 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
534 struct drm_pending_event
*e
= NULL
;
536 spin_lock_irq(&dev
->event_lock
);
537 if (!list_empty(&file_priv
->event_list
)) {
538 e
= list_first_entry(&file_priv
->event_list
,
539 struct drm_pending_event
, link
);
540 file_priv
->event_space
+= e
->event
->length
;
543 spin_unlock_irq(&dev
->event_lock
);
549 if (filp
->f_flags
& O_NONBLOCK
) {
554 mutex_unlock(&file_priv
->event_read_lock
);
555 ret
= wait_event_interruptible(file_priv
->event_wait
,
556 !list_empty(&file_priv
->event_list
));
558 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
562 unsigned length
= e
->event
->length
;
564 if (length
> count
- ret
) {
566 spin_lock_irq(&dev
->event_lock
);
567 file_priv
->event_space
-= length
;
568 list_add(&e
->link
, &file_priv
->event_list
);
569 spin_unlock_irq(&dev
->event_lock
);
570 wake_up_interruptible(&file_priv
->event_wait
);
574 if (copy_to_user(buffer
+ ret
, e
->event
, length
)) {
584 mutex_unlock(&file_priv
->event_read_lock
);
588 EXPORT_SYMBOL(drm_read
);
591 * drm_poll - poll method for DRM file
592 * @filp: file pointer
593 * @wait: poll waiter table
595 * This function must be used by drivers as their &file_operations.read method
596 * iff they use DRM events for asynchronous signalling to userspace. Since
597 * events are used by the KMS API for vblank and page flip completion this means
598 * all modern display drivers must use it.
600 * See also drm_read().
604 * Mask of POLL flags indicating the current status of the file.
606 __poll_t
drm_poll(struct file
*filp
, struct poll_table_struct
*wait
)
608 struct drm_file
*file_priv
= filp
->private_data
;
611 poll_wait(filp
, &file_priv
->event_wait
, wait
);
613 if (!list_empty(&file_priv
->event_list
))
614 mask
|= EPOLLIN
| EPOLLRDNORM
;
618 EXPORT_SYMBOL(drm_poll
);
621 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
623 * @file_priv: DRM file private data
624 * @p: tracking structure for the pending event
625 * @e: actual event data to deliver to userspace
627 * This function prepares the passed in event for eventual delivery. If the event
628 * doesn't get delivered (because the IOCTL fails later on, before queuing up
629 * anything) then the even must be cancelled and freed using
630 * drm_event_cancel_free(). Successfully initialized events should be sent out
631 * using drm_send_event() or drm_send_event_locked() to signal completion of the
632 * asynchronous event to userspace.
634 * If callers embedded @p into a larger structure it must be allocated with
635 * kmalloc and @p must be the first member element.
637 * This is the locked version of drm_event_reserve_init() for callers which
638 * already hold &drm_device.event_lock.
642 * 0 on success or a negative error code on failure.
644 int drm_event_reserve_init_locked(struct drm_device
*dev
,
645 struct drm_file
*file_priv
,
646 struct drm_pending_event
*p
,
649 if (file_priv
->event_space
< e
->length
)
652 file_priv
->event_space
-= e
->length
;
655 list_add(&p
->pending_link
, &file_priv
->pending_event_list
);
656 p
->file_priv
= file_priv
;
660 EXPORT_SYMBOL(drm_event_reserve_init_locked
);
663 * drm_event_reserve_init - init a DRM event and reserve space for it
665 * @file_priv: DRM file private data
666 * @p: tracking structure for the pending event
667 * @e: actual event data to deliver to userspace
669 * This function prepares the passed in event for eventual delivery. If the event
670 * doesn't get delivered (because the IOCTL fails later on, before queuing up
671 * anything) then the even must be cancelled and freed using
672 * drm_event_cancel_free(). Successfully initialized events should be sent out
673 * using drm_send_event() or drm_send_event_locked() to signal completion of the
674 * asynchronous event to userspace.
676 * If callers embedded @p into a larger structure it must be allocated with
677 * kmalloc and @p must be the first member element.
679 * Callers which already hold &drm_device.event_lock should use
680 * drm_event_reserve_init_locked() instead.
684 * 0 on success or a negative error code on failure.
686 int drm_event_reserve_init(struct drm_device
*dev
,
687 struct drm_file
*file_priv
,
688 struct drm_pending_event
*p
,
694 spin_lock_irqsave(&dev
->event_lock
, flags
);
695 ret
= drm_event_reserve_init_locked(dev
, file_priv
, p
, e
);
696 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
700 EXPORT_SYMBOL(drm_event_reserve_init
);
703 * drm_event_cancel_free - free a DRM event and release it's space
705 * @p: tracking structure for the pending event
707 * This function frees the event @p initialized with drm_event_reserve_init()
708 * and releases any allocated space. It is used to cancel an event when the
709 * nonblocking operation could not be submitted and needed to be aborted.
711 void drm_event_cancel_free(struct drm_device
*dev
,
712 struct drm_pending_event
*p
)
715 spin_lock_irqsave(&dev
->event_lock
, flags
);
717 p
->file_priv
->event_space
+= p
->event
->length
;
718 list_del(&p
->pending_link
);
720 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
723 dma_fence_put(p
->fence
);
727 EXPORT_SYMBOL(drm_event_cancel_free
);
730 * drm_send_event_locked - send DRM event to file descriptor
732 * @e: DRM event to deliver
734 * This function sends the event @e, initialized with drm_event_reserve_init(),
735 * to its associated userspace DRM file. Callers must already hold
736 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
738 * Note that the core will take care of unlinking and disarming events when the
739 * corresponding DRM file is closed. Drivers need not worry about whether the
740 * DRM file for this event still exists and can call this function upon
741 * completion of the asynchronous work unconditionally.
743 void drm_send_event_locked(struct drm_device
*dev
, struct drm_pending_event
*e
)
745 assert_spin_locked(&dev
->event_lock
);
748 complete_all(e
->completion
);
749 e
->completion_release(e
->completion
);
750 e
->completion
= NULL
;
754 dma_fence_signal(e
->fence
);
755 dma_fence_put(e
->fence
);
763 list_del(&e
->pending_link
);
764 list_add_tail(&e
->link
,
765 &e
->file_priv
->event_list
);
766 wake_up_interruptible(&e
->file_priv
->event_wait
);
768 EXPORT_SYMBOL(drm_send_event_locked
);
771 * drm_send_event - send DRM event to file descriptor
773 * @e: DRM event to deliver
775 * This function sends the event @e, initialized with drm_event_reserve_init(),
776 * to its associated userspace DRM file. This function acquires
777 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
780 * Note that the core will take care of unlinking and disarming events when the
781 * corresponding DRM file is closed. Drivers need not worry about whether the
782 * DRM file for this event still exists and can call this function upon
783 * completion of the asynchronous work unconditionally.
785 void drm_send_event(struct drm_device
*dev
, struct drm_pending_event
*e
)
787 unsigned long irqflags
;
789 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
790 drm_send_event_locked(dev
, e
);
791 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
793 EXPORT_SYMBOL(drm_send_event
);