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.
35 #include <linux/poll.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
38 #include "drm_legacy.h"
39 #include "drm_internal.h"
40 #include "drm_crtc_internal.h"
42 /* from BKL pushdown */
43 DEFINE_MUTEX(drm_global_mutex
);
46 * DOC: file operations
48 * Drivers must define the file operations structure that forms the DRM
49 * userspace API entry point, even though most of those operations are
50 * implemented in the DRM core. The mandatory functions are drm_open(),
51 * drm_read(), drm_ioctl() and drm_compat_ioctl() if CONFIG_COMPAT is enabled
52 * (note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n). Drivers which
53 * implement private ioctls that require 32/64 bit compatibility support must
54 * provide their own .compat_ioctl() handler that processes private ioctls and
55 * calls drm_compat_ioctl() for core ioctls.
57 * In addition drm_read() and drm_poll() provide support for DRM events. DRM
58 * events are a generic and extensible means to send asynchronous events to
59 * userspace through the file descriptor. They are used to send vblank event and
60 * page flip completions by the KMS API. But drivers can also use it for their
61 * own needs, e.g. to signal completion of rendering.
63 * The memory mapping implementation will vary depending on how the driver
64 * manages memory. Legacy drivers will use the deprecated drm_legacy_mmap()
65 * function, modern drivers should use one of the provided memory-manager
66 * specific implementations. For GEM-based drivers this is drm_gem_mmap().
68 * No other file operations are supported by the DRM userspace API. Overall the
69 * following is an example #file_operations structure::
71 * static const example_drm_fops = {
72 * .owner = THIS_MODULE,
74 * .release = drm_release,
75 * .unlocked_ioctl = drm_ioctl,
76 * .compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
79 * .llseek = no_llseek,
80 * .mmap = drm_gem_mmap,
84 static int drm_open_helper(struct file
*filp
, struct drm_minor
*minor
);
86 static int drm_setup(struct drm_device
* dev
)
90 if (dev
->driver
->firstopen
&&
91 drm_core_check_feature(dev
, DRIVER_LEGACY
)) {
92 ret
= dev
->driver
->firstopen(dev
);
97 ret
= drm_legacy_dma_setup(dev
);
107 * drm_open - open method for DRM file
108 * @inode: device inode
109 * @filp: file pointer.
111 * This function must be used by drivers as their .open() #file_operations
112 * method. It looks up the correct DRM device and instantiates all the per-file
117 * 0 on success or negative errno value on falure.
119 int drm_open(struct inode
*inode
, struct file
*filp
)
121 struct drm_device
*dev
;
122 struct drm_minor
*minor
;
126 minor
= drm_minor_acquire(iminor(inode
));
128 return PTR_ERR(minor
);
131 if (!dev
->open_count
++)
134 /* share address_space across all char-devs of a single device */
135 filp
->f_mapping
= dev
->anon_inode
->i_mapping
;
137 retcode
= drm_open_helper(filp
, minor
);
141 retcode
= drm_setup(dev
);
149 drm_minor_release(minor
);
152 EXPORT_SYMBOL(drm_open
);
155 * Check whether DRI will run on this CPU.
157 * \return non-zero if the DRI will run on this CPU, or zero otherwise.
159 static int drm_cpu_valid(void)
161 #if defined(__sparc__) && !defined(__sparc_v9__)
162 return 0; /* No cmpxchg before v9 sparc. */
168 * Called whenever a process opens /dev/drm.
170 * \param filp file pointer.
171 * \param minor acquired minor-object.
172 * \return zero on success or a negative number on failure.
174 * Creates and initializes a drm_file structure for the file private data in \p
175 * filp and add it into the double linked list in \p dev.
177 static int drm_open_helper(struct file
*filp
, struct drm_minor
*minor
)
179 struct drm_device
*dev
= minor
->dev
;
180 struct drm_file
*priv
;
183 if (filp
->f_flags
& O_EXCL
)
184 return -EBUSY
; /* No exclusive opens */
185 if (!drm_cpu_valid())
187 if (dev
->switch_power_state
!= DRM_SWITCH_POWER_ON
&& dev
->switch_power_state
!= DRM_SWITCH_POWER_DYNAMIC_OFF
)
190 DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current
), minor
->index
);
192 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
196 filp
->private_data
= priv
;
198 priv
->pid
= get_pid(task_pid(current
));
201 /* for compatibility root is always authenticated */
202 priv
->authenticated
= capable(CAP_SYS_ADMIN
);
203 priv
->lock_count
= 0;
205 INIT_LIST_HEAD(&priv
->lhead
);
206 INIT_LIST_HEAD(&priv
->fbs
);
207 mutex_init(&priv
->fbs_lock
);
208 INIT_LIST_HEAD(&priv
->blobs
);
209 INIT_LIST_HEAD(&priv
->pending_event_list
);
210 INIT_LIST_HEAD(&priv
->event_list
);
211 init_waitqueue_head(&priv
->event_wait
);
212 priv
->event_space
= 4096; /* set aside 4k for event buffer */
214 mutex_init(&priv
->event_read_lock
);
216 if (drm_core_check_feature(dev
, DRIVER_GEM
))
217 drm_gem_open(dev
, priv
);
219 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
220 drm_prime_init_file_private(&priv
->prime
);
222 if (dev
->driver
->open
) {
223 ret
= dev
->driver
->open(dev
, priv
);
225 goto out_prime_destroy
;
228 if (drm_is_primary_client(priv
)) {
229 ret
= drm_master_open(priv
);
234 mutex_lock(&dev
->filelist_mutex
);
235 list_add(&priv
->lhead
, &dev
->filelist
);
236 mutex_unlock(&dev
->filelist_mutex
);
243 struct pci_dev
*pci_dev
;
244 pci_dev
= pci_get_class(PCI_CLASS_DISPLAY_VGA
<< 8, NULL
);
246 dev
->hose
= pci_dev
->sysdata
;
247 pci_dev_put(pci_dev
);
250 struct pci_bus
*b
= list_entry(pci_root_buses
.next
,
251 struct pci_bus
, node
);
253 dev
->hose
= b
->sysdata
;
261 if (dev
->driver
->postclose
)
262 dev
->driver
->postclose(dev
, priv
);
264 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
265 drm_prime_destroy_file_private(&priv
->prime
);
266 if (drm_core_check_feature(dev
, DRIVER_GEM
))
267 drm_gem_release(dev
, priv
);
270 filp
->private_data
= NULL
;
274 static void drm_events_release(struct drm_file
*file_priv
)
276 struct drm_device
*dev
= file_priv
->minor
->dev
;
277 struct drm_pending_event
*e
, *et
;
280 spin_lock_irqsave(&dev
->event_lock
, flags
);
282 /* Unlink pending events */
283 list_for_each_entry_safe(e
, et
, &file_priv
->pending_event_list
,
285 list_del(&e
->pending_link
);
289 /* Remove unconsumed events */
290 list_for_each_entry_safe(e
, et
, &file_priv
->event_list
, link
) {
295 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
299 * drm_legacy_dev_reinit
301 * Reinitializes a legacy/ums drm device in it's lastclose function.
303 static void drm_legacy_dev_reinit(struct drm_device
*dev
)
305 if (dev
->irq_enabled
)
306 drm_irq_uninstall(dev
);
308 mutex_lock(&dev
->struct_mutex
);
310 drm_legacy_agp_clear(dev
);
312 drm_legacy_sg_cleanup(dev
);
313 drm_legacy_vma_flush(dev
);
314 drm_legacy_dma_takedown(dev
);
316 mutex_unlock(&dev
->struct_mutex
);
318 dev
->sigdata
.lock
= NULL
;
320 dev
->context_flag
= 0;
321 dev
->last_context
= 0;
324 DRM_DEBUG("lastclose completed\n");
328 * Take down the DRM device.
330 * \param dev DRM device structure.
332 * Frees every resource in \p dev.
336 void drm_lastclose(struct drm_device
* dev
)
340 if (dev
->driver
->lastclose
)
341 dev
->driver
->lastclose(dev
);
342 DRM_DEBUG("driver lastclose completed\n");
344 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
345 drm_legacy_dev_reinit(dev
);
349 * drm_release - release method for DRM file
350 * @inode: device inode
351 * @filp: file pointer.
353 * This function must be used by drivers as their .release() #file_operations
354 * method. It frees any resources associated with the open file, and if this is
355 * the last open file for the DRM device also proceeds to call drm_lastclose().
359 * Always succeeds and returns 0.
361 int drm_release(struct inode
*inode
, struct file
*filp
)
363 struct drm_file
*file_priv
= filp
->private_data
;
364 struct drm_minor
*minor
= file_priv
->minor
;
365 struct drm_device
*dev
= minor
->dev
;
367 mutex_lock(&drm_global_mutex
);
369 DRM_DEBUG("open_count = %d\n", dev
->open_count
);
371 mutex_lock(&dev
->filelist_mutex
);
372 list_del(&file_priv
->lhead
);
373 mutex_unlock(&dev
->filelist_mutex
);
375 if (dev
->driver
->preclose
)
376 dev
->driver
->preclose(dev
, file_priv
);
378 /* ========================================================
379 * Begin inline drm_release
382 DRM_DEBUG("pid = %d, device = 0x%lx, open_count = %d\n",
383 task_pid_nr(current
),
384 (long)old_encode_dev(file_priv
->minor
->kdev
->devt
),
387 if (drm_core_check_feature(dev
, DRIVER_LEGACY
))
388 drm_legacy_lock_release(dev
, filp
);
390 if (drm_core_check_feature(dev
, DRIVER_HAVE_DMA
))
391 drm_legacy_reclaim_buffers(dev
, file_priv
);
393 drm_events_release(file_priv
);
395 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
396 drm_fb_release(file_priv
);
397 drm_property_destroy_user_blobs(dev
, file_priv
);
400 if (drm_core_check_feature(dev
, DRIVER_GEM
))
401 drm_gem_release(dev
, file_priv
);
403 drm_legacy_ctxbitmap_flush(dev
, file_priv
);
405 if (drm_is_primary_client(file_priv
))
406 drm_master_release(file_priv
);
408 if (dev
->driver
->postclose
)
409 dev
->driver
->postclose(dev
, file_priv
);
411 if (drm_core_check_feature(dev
, DRIVER_PRIME
))
412 drm_prime_destroy_file_private(&file_priv
->prime
);
414 WARN_ON(!list_empty(&file_priv
->event_list
));
416 put_pid(file_priv
->pid
);
419 /* ========================================================
420 * End inline drm_release
423 if (!--dev
->open_count
) {
425 if (drm_device_is_unplugged(dev
))
428 mutex_unlock(&drm_global_mutex
);
430 drm_minor_release(minor
);
434 EXPORT_SYMBOL(drm_release
);
437 * drm_read - read method for DRM file
438 * @filp: file pointer
439 * @buffer: userspace destination pointer for the read
440 * @count: count in bytes to read
441 * @offset: offset to read
443 * This function must be used by drivers as their .read() #file_operations
444 * method iff they use DRM events for asynchronous signalling to userspace.
445 * Since events are used by the KMS API for vblank and page flip completion this
446 * means all modern display drivers must use it.
448 * @offset is ignore, DRM events are read like a pipe. Therefore drivers also
449 * must set the .llseek() #file_operation to no_llseek(). Polling support is
450 * provided by drm_poll().
452 * This function will only ever read a full event. Therefore userspace must
453 * supply a big enough buffer to fit any event to ensure forward progress. Since
454 * the maximum event space is currently 4K it's recommended to just use that for
459 * Number of bytes read (always aligned to full events, and can be 0) or a
460 * negative error code on failure.
462 ssize_t
drm_read(struct file
*filp
, char __user
*buffer
,
463 size_t count
, loff_t
*offset
)
465 struct drm_file
*file_priv
= filp
->private_data
;
466 struct drm_device
*dev
= file_priv
->minor
->dev
;
469 if (!access_ok(VERIFY_WRITE
, buffer
, count
))
472 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
477 struct drm_pending_event
*e
= NULL
;
479 spin_lock_irq(&dev
->event_lock
);
480 if (!list_empty(&file_priv
->event_list
)) {
481 e
= list_first_entry(&file_priv
->event_list
,
482 struct drm_pending_event
, link
);
483 file_priv
->event_space
+= e
->event
->length
;
486 spin_unlock_irq(&dev
->event_lock
);
492 if (filp
->f_flags
& O_NONBLOCK
) {
497 mutex_unlock(&file_priv
->event_read_lock
);
498 ret
= wait_event_interruptible(file_priv
->event_wait
,
499 !list_empty(&file_priv
->event_list
));
501 ret
= mutex_lock_interruptible(&file_priv
->event_read_lock
);
505 unsigned length
= e
->event
->length
;
507 if (length
> count
- ret
) {
509 spin_lock_irq(&dev
->event_lock
);
510 file_priv
->event_space
-= length
;
511 list_add(&e
->link
, &file_priv
->event_list
);
512 spin_unlock_irq(&dev
->event_lock
);
516 if (copy_to_user(buffer
+ ret
, e
->event
, length
)) {
526 mutex_unlock(&file_priv
->event_read_lock
);
530 EXPORT_SYMBOL(drm_read
);
533 * drm_poll - poll method for DRM file
534 * @filp: file pointer
535 * @wait: poll waiter table
537 * This function must be used by drivers as their .read() #file_operations
538 * method iff they use DRM events for asynchronous signalling to userspace.
539 * Since events are used by the KMS API for vblank and page flip completion this
540 * means all modern display drivers must use it.
542 * See also drm_read().
546 * Mask of POLL flags indicating the current status of the file.
548 unsigned int drm_poll(struct file
*filp
, struct poll_table_struct
*wait
)
550 struct drm_file
*file_priv
= filp
->private_data
;
551 unsigned int mask
= 0;
553 poll_wait(filp
, &file_priv
->event_wait
, wait
);
555 if (!list_empty(&file_priv
->event_list
))
556 mask
|= POLLIN
| POLLRDNORM
;
560 EXPORT_SYMBOL(drm_poll
);
563 * drm_event_reserve_init_locked - init a DRM event and reserve space for it
565 * @file_priv: DRM file private data
566 * @p: tracking structure for the pending event
567 * @e: actual event data to deliver to userspace
569 * This function prepares the passed in event for eventual delivery. If the event
570 * doesn't get delivered (because the IOCTL fails later on, before queuing up
571 * anything) then the even must be cancelled and freed using
572 * drm_event_cancel_free(). Successfully initialized events should be sent out
573 * using drm_send_event() or drm_send_event_locked() to signal completion of the
574 * asynchronous event to userspace.
576 * If callers embedded @p into a larger structure it must be allocated with
577 * kmalloc and @p must be the first member element.
579 * This is the locked version of drm_event_reserve_init() for callers which
580 * already hold &drm_device.event_lock.
584 * 0 on success or a negative error code on failure.
586 int drm_event_reserve_init_locked(struct drm_device
*dev
,
587 struct drm_file
*file_priv
,
588 struct drm_pending_event
*p
,
591 if (file_priv
->event_space
< e
->length
)
594 file_priv
->event_space
-= e
->length
;
597 list_add(&p
->pending_link
, &file_priv
->pending_event_list
);
598 p
->file_priv
= file_priv
;
602 EXPORT_SYMBOL(drm_event_reserve_init_locked
);
605 * drm_event_reserve_init - init a DRM event and reserve space for it
607 * @file_priv: DRM file private data
608 * @p: tracking structure for the pending event
609 * @e: actual event data to deliver to userspace
611 * This function prepares the passed in event for eventual delivery. If the event
612 * doesn't get delivered (because the IOCTL fails later on, before queuing up
613 * anything) then the even must be cancelled and freed using
614 * drm_event_cancel_free(). Successfully initialized events should be sent out
615 * using drm_send_event() or drm_send_event_locked() to signal completion of the
616 * asynchronous event to userspace.
618 * If callers embedded @p into a larger structure it must be allocated with
619 * kmalloc and @p must be the first member element.
621 * Callers which already hold &drm_device.event_lock should use
622 * drm_event_reserve_init_locked() instead.
626 * 0 on success or a negative error code on failure.
628 int drm_event_reserve_init(struct drm_device
*dev
,
629 struct drm_file
*file_priv
,
630 struct drm_pending_event
*p
,
636 spin_lock_irqsave(&dev
->event_lock
, flags
);
637 ret
= drm_event_reserve_init_locked(dev
, file_priv
, p
, e
);
638 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
642 EXPORT_SYMBOL(drm_event_reserve_init
);
645 * drm_event_cancel_free - free a DRM event and release it's space
647 * @p: tracking structure for the pending event
649 * This function frees the event @p initialized with drm_event_reserve_init()
650 * and releases any allocated space.
652 void drm_event_cancel_free(struct drm_device
*dev
,
653 struct drm_pending_event
*p
)
656 spin_lock_irqsave(&dev
->event_lock
, flags
);
658 p
->file_priv
->event_space
+= p
->event
->length
;
659 list_del(&p
->pending_link
);
661 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
664 dma_fence_put(p
->fence
);
668 EXPORT_SYMBOL(drm_event_cancel_free
);
671 * drm_send_event_locked - send DRM event to file descriptor
673 * @e: DRM event to deliver
675 * This function sends the event @e, initialized with drm_event_reserve_init(),
676 * to its associated userspace DRM file. Callers must already hold
677 * &drm_device.event_lock, see drm_send_event() for the unlocked version.
679 * Note that the core will take care of unlinking and disarming events when the
680 * corresponding DRM file is closed. Drivers need not worry about whether the
681 * DRM file for this event still exists and can call this function upon
682 * completion of the asynchronous work unconditionally.
684 void drm_send_event_locked(struct drm_device
*dev
, struct drm_pending_event
*e
)
686 assert_spin_locked(&dev
->event_lock
);
689 complete_all(e
->completion
);
690 e
->completion_release(e
->completion
);
691 e
->completion
= NULL
;
695 dma_fence_signal(e
->fence
);
696 dma_fence_put(e
->fence
);
704 list_del(&e
->pending_link
);
705 list_add_tail(&e
->link
,
706 &e
->file_priv
->event_list
);
707 wake_up_interruptible(&e
->file_priv
->event_wait
);
709 EXPORT_SYMBOL(drm_send_event_locked
);
712 * drm_send_event - send DRM event to file descriptor
714 * @e: DRM event to deliver
716 * This function sends the event @e, initialized with drm_event_reserve_init(),
717 * to its associated userspace DRM file. This function acquires
718 * &drm_device.event_lock, see drm_send_event_locked() for callers which already
721 * Note that the core will take care of unlinking and disarming events when the
722 * corresponding DRM file is closed. Drivers need not worry about whether the
723 * DRM file for this event still exists and can call this function upon
724 * completion of the asynchronous work unconditionally.
726 void drm_send_event(struct drm_device
*dev
, struct drm_pending_event
*e
)
728 unsigned long irqflags
;
730 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
731 drm_send_event_locked(dev
, e
);
732 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
734 EXPORT_SYMBOL(drm_send_event
);