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_file.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
);
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,
83 * .release = drm_release,
84 * .unlocked_ioctl = drm_ioctl,
85 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
88 * .llseek = no_llseek,
89 * .mmap = drm_gem_mmap,
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
96 * The driver's &file_operations must be stored in &drm_driver.fops.
98 * For driver-private IOCTL handling see the more detailed discussion in
99 * :ref:`IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`.
102 static int drm_open_helper(struct file
*filp
, struct drm_minor
*minor
);
104 static int drm_setup(struct drm_device
* dev
)
108 if (dev
->driver
->firstopen
&&
109 drm_core_check_feature(dev
, DRIVER_LEGACY
)) {
110 ret
= dev
->driver
->firstopen(dev
);
115 ret
= drm_legacy_dma_setup(dev
);
125 * drm_open - open method for DRM file
126 * @inode: device inode
127 * @filp: file pointer.
129 * This function must be used by drivers as their &file_operations.open method.
130 * It looks up the correct DRM device and instantiates all the per-file
131 * resources for it. It also calls the &drm_driver.open driver callback.
135 * 0 on success or negative errno value on falure.
137 int drm_open(struct inode
*inode
, struct file
*filp
)
139 struct drm_device
*dev
;
140 struct drm_minor
*minor
;
144 minor
= drm_minor_acquire(iminor(inode
));
146 return PTR_ERR(minor
);
149 if (!dev
->open_count
++)
152 /* share address_space across all char-devs of a single device */
153 filp
->f_mapping
= dev
->anon_inode
->i_mapping
;
155 retcode
= drm_open_helper(filp
, minor
);
159 retcode
= drm_setup(dev
);
167 drm_minor_release(minor
);
170 EXPORT_SYMBOL(drm_open
);
173 * Check whether DRI will run on this CPU.
175 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
177 static int drm_cpu_valid(void)
179 #if defined(__sparc__) && !defined(__sparc_v9__)
180 return 0; /* No cmpxchg before v9 sparc. */
186 * Called whenever a process opens /dev/drm.
188 * \param filp file pointer.
189 * \param minor acquired minor-object.
190 * \return zero on success or a negative number on failure.
192 * Creates and initializes a drm_file structure for the file private data in \p
193 * filp and add it into the double linked list in \p dev.
195 static int drm_open_helper(struct file
*filp
, struct drm_minor
*minor
)
197 struct drm_device
*dev
= minor
->dev
;
198 struct drm_file
*priv
;
201 if (filp
->f_flags
& O_EXCL
)
202 return -EBUSY
; /* No exclusive opens */
203 if (!drm_cpu_valid())
205 if (dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
&& dev
->switch_power_state
!= DRM_SWITCH_POWER_DYNAMIC_OFF
)
208 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current
), minor
->index
);
210 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
214 filp
->private_data
= priv
;
216 priv
->pid
= get_pid(task_pid(current
));
219 /* for compatibility root is always authenticated */
220 priv
->authenticated
= capable(CAP_SYS_ADMIN
);
221 priv
->lock_count
= 0;
223 INIT_LIST_HEAD(&priv
->lhead
);
224 INIT_LIST_HEAD(&priv
->fbs
);
225 mutex_init(&priv
->fbs_lock
);
226 INIT_LIST_HEAD(&priv
->blobs
);
227 INIT_LIST_HEAD(&priv
->pending_event_list
);
228 INIT_LIST_HEAD(&priv
->event_list
);
229 init_waitqueue_head(&priv
->event_wait
);
230 priv
->event_space
= 4096; /* set aside 4k for event buffer */
232 mutex_init(&priv
->event_read_lock
);
234 if (drm_core_check_feature(dev
, DRIVER_GEM
))
235 drm_gem_open(dev
, priv
);
237 if (drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
238 drm_syncobj_open(priv
);
240 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
241 drm_prime_init_file_private(&priv
->prime
);
243 if (dev
->driver
->open
) {
244 ret
= dev
->driver
->open(dev
, priv
);
246 goto out_prime_destroy
;
249 if (drm_is_primary_client(priv
)) {
250 ret
= drm_master_open(priv
);
255 mutex_lock(&dev
->filelist_mutex
);
256 list_add(&priv
->lhead
, &dev
->filelist
);
257 mutex_unlock(&dev
->filelist_mutex
);
264 struct pci_dev
*pci_dev
;
265 pci_dev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, NULL
);
267 dev
->hose
= pci_dev
->sysdata
;
268 pci_dev_put(pci_dev
);
271 struct pci_bus
*b
= list_entry(pci_root_buses
.next
,
272 struct pci_bus
, node
);
274 dev
->hose
= b
->sysdata
;
282 if (dev
->driver
->postclose
)
283 dev
->driver
->postclose(dev
, priv
);
285 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
286 drm_prime_destroy_file_private(&priv
->prime
);
287 if (drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
288 drm_syncobj_release(priv
);
289 if (drm_core_check_feature(dev
, DRIVER_GEM
))
290 drm_gem_release(dev
, priv
);
293 filp
->private_data
= NULL
;
297 static void drm_events_release(struct drm_file
*file_priv
)
299 struct drm_device
*dev
= file_priv
->minor
->dev
;
300 struct drm_pending_event
*e
, *et
;
303 spin_lock_irqsave(&dev
->event_lock
, flags
);
305 /* Unlink pending events */
306 list_for_each_entry_safe(e
, et
, &file_priv
->pending_event_list
,
308 list_del(&e
->pending_link
);
312 /* Remove unconsumed events */
313 list_for_each_entry_safe(e
, et
, &file_priv
->event_list
, link
) {
318 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
321 static void drm_legacy_dev_reinit(struct drm_device
*dev
)
323 if (dev
->irq_enabled
)
324 drm_irq_uninstall(dev
);
326 mutex_lock(&dev
->struct_mutex
);
328 drm_legacy_agp_clear(dev
);
330 drm_legacy_sg_cleanup(dev
);
331 drm_legacy_vma_flush(dev
);
332 drm_legacy_dma_takedown(dev
);
334 mutex_unlock(&dev
->struct_mutex
);
336 dev
->sigdata
.lock
= NULL
;
338 dev
->context_flag
= 0;
339 dev
->last_context
= 0;
342 DRM_DEBUG("lastclose completed\n");
345 void drm_lastclose(struct drm_device
* dev
)
349 if (dev
->driver
->lastclose
)
350 dev
->driver
->lastclose(dev
);
351 DRM_DEBUG("driver lastclose completed\n");
353 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
354 drm_legacy_dev_reinit(dev
);
358 * drm_release - release method for DRM file
359 * @inode: device inode
360 * @filp: file pointer.
362 * This function must be used by drivers as their &file_operations.release
363 * method. It frees any resources associated with the open file, and calls the
364 * &drm_driver.postclose driver callback. If this is the last open file for the
365 * DRM device also proceeds to call the &drm_driver.lastclose driver callback.
369 * Always succeeds and returns 0.
371 int drm_release(struct inode
*inode
, struct file
*filp
)
373 struct drm_file
*file_priv
= filp
->private_data
;
374 struct drm_minor
*minor
= file_priv
->minor
;
375 struct drm_device
*dev
= minor
->dev
;
377 mutex_lock(&drm_global_mutex
);
379 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
381 mutex_lock(&dev
->filelist_mutex
);
382 list_del(&file_priv
->lhead
);
383 mutex_unlock(&dev
->filelist_mutex
);
385 if (drm_core_check_feature(dev
, DRIVER_LEGACY
) &&
386 dev
->driver
->preclose
)
387 dev
->driver
->preclose(dev
, file_priv
);
389 /* ========================================================
390 * Begin inline drm_release
393 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
394 task_pid_nr(current
),
395 (long)old_encode_dev(file_priv
->minor
->kdev
->devt
),
398 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
399 drm_legacy_lock_release(dev
, filp
);
401 if (drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
402 drm_legacy_reclaim_buffers(dev
, file_priv
);
404 drm_events_release(file_priv
);
406 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
407 drm_fb_release(file_priv
);
408 drm_property_destroy_user_blobs(dev
, file_priv
);
411 if (drm_core_check_feature(dev
, DRIVER_SYNCOBJ
))
412 drm_syncobj_release(file_priv
);
414 if (drm_core_check_feature(dev
, DRIVER_GEM
))
415 drm_gem_release(dev
, file_priv
);
417 drm_legacy_ctxbitmap_flush(dev
, file_priv
);
419 if (drm_is_primary_client(file_priv
))
420 drm_master_release(file_priv
);
422 if (dev
->driver
->postclose
)
423 dev
->driver
->postclose(dev
, file_priv
);
425 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
426 drm_prime_destroy_file_private(&file_priv
->prime
);
428 WARN_ON(!list_empty(&file_priv
->event_list
));
430 put_pid(file_priv
->pid
);
433 /* ========================================================
434 * End inline drm_release
437 if (!--dev
->open_count
) {
439 if (drm_dev_is_unplugged(dev
))
442 mutex_unlock(&drm_global_mutex
);
444 drm_minor_release(minor
);
448 EXPORT_SYMBOL(drm_release
);
451 * drm_read - read method for DRM file
452 * @filp: file pointer
453 * @buffer: userspace destination pointer for the read
454 * @count: count in bytes to read
455 * @offset: offset to read
457 * This function must be used by drivers as their &file_operations.read
458 * method iff they use DRM events for asynchronous signalling to userspace.
459 * Since events are used by the KMS API for vblank and page flip completion this
460 * means all modern display drivers must use it.
462 * @offset is ignored, DRM events are read like a pipe. Therefore drivers also
463 * must set the &file_operation.llseek to no_llseek(). Polling support is
464 * provided by drm_poll().
466 * This function will only ever read a full event. Therefore userspace must
467 * supply a big enough buffer to fit any event to ensure forward progress. Since
468 * the maximum event space is currently 4K it's recommended to just use that for
473 * Number of bytes read (always aligned to full events, and can be 0) or a
474 * negative error code on failure.
476 ssize_t
drm_read(struct file
*filp
, char __user
*buffer
,
477 size_t count
, loff_t
*offset
)
479 struct drm_file
*file_priv
= filp
->private_data
;
480 struct drm_device
*dev
= file_priv
->minor
->dev
;
483 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
486 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
491 struct drm_pending_event
*e
= NULL
;
493 spin_lock_irq(&dev
->event_lock
);
494 if (!list_empty(&file_priv
->event_list
)) {
495 e
= list_first_entry(&file_priv
->event_list
,
496 struct drm_pending_event
, link
);
497 file_priv
->event_space
+= e
->event
->length
;
500 spin_unlock_irq(&dev
->event_lock
);
506 if (filp
->f_flags
& O_NONBLOCK
) {
511 mutex_unlock(&file_priv
->event_read_lock
);
512 ret
= wait_event_interruptible(file_priv
->event_wait
,
513 !list_empty(&file_priv
->event_list
));
515 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
519 unsigned length
= e
->event
->length
;
521 if (length
> count
- ret
) {
523 spin_lock_irq(&dev
->event_lock
);
524 file_priv
->event_space
-= length
;
525 list_add(&e
->link
, &file_priv
->event_list
);
526 spin_unlock_irq(&dev
->event_lock
);
530 if (copy_to_user(buffer
+ ret
, e
->event
, length
)) {
540 mutex_unlock(&file_priv
->event_read_lock
);
544 EXPORT_SYMBOL(drm_read
);
547 * drm_poll - poll method for DRM file
548 * @filp: file pointer
549 * @wait: poll waiter table
551 * This function must be used by drivers as their &file_operations.read method
552 * iff they use DRM events for asynchronous signalling to userspace. Since
553 * events are used by the KMS API for vblank and page flip completion this means
554 * all modern display drivers must use it.
556 * See also drm_read().
560 * Mask of POLL flags indicating the current status of the file.
562 __poll_t
drm_poll(struct file
*filp
, struct poll_table_struct
*wait
)
564 struct drm_file
*file_priv
= filp
->private_data
;
567 poll_wait(filp
, &file_priv
->event_wait
, wait
);
569 if (!list_empty(&file_priv
->event_list
))
570 mask
|= EPOLLIN
| EPOLLRDNORM
;
574 EXPORT_SYMBOL(drm_poll
);
577 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
579 * @file_priv: DRM file private data
580 * @p: tracking structure for the pending event
581 * @e: actual event data to deliver to userspace
583 * This function prepares the passed in event for eventual delivery. If the event
584 * doesn't get delivered (because the IOCTL fails later on, before queuing up
585 * anything) then the even must be cancelled and freed using
586 * drm_event_cancel_free(). Successfully initialized events should be sent out
587 * using drm_send_event() or drm_send_event_locked() to signal completion of the
588 * asynchronous event to userspace.
590 * If callers embedded @p into a larger structure it must be allocated with
591 * kmalloc and @p must be the first member element.
593 * This is the locked version of drm_event_reserve_init() for callers which
594 * already hold &drm_device.event_lock.
598 * 0 on success or a negative error code on failure.
600 int drm_event_reserve_init_locked(struct drm_device
*dev
,
601 struct drm_file
*file_priv
,
602 struct drm_pending_event
*p
,
605 if (file_priv
->event_space
< e
->length
)
608 file_priv
->event_space
-= e
->length
;
611 list_add(&p
->pending_link
, &file_priv
->pending_event_list
);
612 p
->file_priv
= file_priv
;
616 EXPORT_SYMBOL(drm_event_reserve_init_locked
);
619 * drm_event_reserve_init - init a DRM event and reserve space for it
621 * @file_priv: DRM file private data
622 * @p: tracking structure for the pending event
623 * @e: actual event data to deliver to userspace
625 * This function prepares the passed in event for eventual delivery. If the event
626 * doesn't get delivered (because the IOCTL fails later on, before queuing up
627 * anything) then the even must be cancelled and freed using
628 * drm_event_cancel_free(). Successfully initialized events should be sent out
629 * using drm_send_event() or drm_send_event_locked() to signal completion of the
630 * asynchronous event to userspace.
632 * If callers embedded @p into a larger structure it must be allocated with
633 * kmalloc and @p must be the first member element.
635 * Callers which already hold &drm_device.event_lock should use
636 * drm_event_reserve_init_locked() instead.
640 * 0 on success or a negative error code on failure.
642 int drm_event_reserve_init(struct drm_device
*dev
,
643 struct drm_file
*file_priv
,
644 struct drm_pending_event
*p
,
650 spin_lock_irqsave(&dev
->event_lock
, flags
);
651 ret
= drm_event_reserve_init_locked(dev
, file_priv
, p
, e
);
652 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
656 EXPORT_SYMBOL(drm_event_reserve_init
);
659 * drm_event_cancel_free - free a DRM event and release it's space
661 * @p: tracking structure for the pending event
663 * This function frees the event @p initialized with drm_event_reserve_init()
664 * and releases any allocated space. It is used to cancel an event when the
665 * nonblocking operation could not be submitted and needed to be aborted.
667 void drm_event_cancel_free(struct drm_device
*dev
,
668 struct drm_pending_event
*p
)
671 spin_lock_irqsave(&dev
->event_lock
, flags
);
673 p
->file_priv
->event_space
+= p
->event
->length
;
674 list_del(&p
->pending_link
);
676 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
679 dma_fence_put(p
->fence
);
683 EXPORT_SYMBOL(drm_event_cancel_free
);
686 * drm_send_event_locked - send DRM event to file descriptor
688 * @e: DRM event to deliver
690 * This function sends the event @e, initialized with drm_event_reserve_init(),
691 * to its associated userspace DRM file. Callers must already hold
692 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
694 * Note that the core will take care of unlinking and disarming events when the
695 * corresponding DRM file is closed. Drivers need not worry about whether the
696 * DRM file for this event still exists and can call this function upon
697 * completion of the asynchronous work unconditionally.
699 void drm_send_event_locked(struct drm_device
*dev
, struct drm_pending_event
*e
)
701 assert_spin_locked(&dev
->event_lock
);
704 complete_all(e
->completion
);
705 e
->completion_release(e
->completion
);
706 e
->completion
= NULL
;
710 dma_fence_signal(e
->fence
);
711 dma_fence_put(e
->fence
);
719 list_del(&e
->pending_link
);
720 list_add_tail(&e
->link
,
721 &e
->file_priv
->event_list
);
722 wake_up_interruptible(&e
->file_priv
->event_wait
);
724 EXPORT_SYMBOL(drm_send_event_locked
);
727 * drm_send_event - send DRM event to file descriptor
729 * @e: DRM event to deliver
731 * This function sends the event @e, initialized with drm_event_reserve_init(),
732 * to its associated userspace DRM file. This function acquires
733 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
736 * Note that the core will take care of unlinking and disarming events when the
737 * corresponding DRM file is closed. Drivers need not worry about whether the
738 * DRM file for this event still exists and can call this function upon
739 * completion of the asynchronous work unconditionally.
741 void drm_send_event(struct drm_device
*dev
, struct drm_pending_event
*e
)
743 unsigned long irqflags
;
745 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
746 drm_send_event_locked(dev
, e
);
747 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
749 EXPORT_SYMBOL(drm_send_event
);