2 * Internal Header for the Direct Rendering Manager
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Copyright (c) 2009-2010, Code Aurora Forum.
9 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
10 * Author: Gareth Hughes <gareth@valinux.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice (including the next
20 * paragraph) shall be included in all copies or substantial portions of the
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
35 #include <linux/agp_backend.h>
36 #include <linux/cdev.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/file.h>
40 #include <linux/highmem.h>
41 #include <linux/idr.h>
42 #include <linux/init.h>
44 #include <linux/jiffies.h>
45 #include <linux/kernel.h>
46 #include <linux/kref.h>
47 #include <linux/miscdevice.h>
49 #include <linux/mutex.h>
50 #include <linux/pci.h>
51 #include <linux/platform_device.h>
52 #include <linux/poll.h>
53 #include <linux/ratelimit.h>
54 #include <linux/sched.h>
55 #include <linux/slab.h>
56 #include <linux/types.h>
57 #include <linux/vmalloc.h>
58 #include <linux/workqueue.h>
61 #include <asm/pgalloc.h>
62 #include <asm/uaccess.h>
64 #include <uapi/drm/drm.h>
65 #include <uapi/drm/drm_mode.h>
67 #include <drm/drm_agpsupport.h>
68 #include <drm/drm_crtc.h>
69 #include <drm/drm_global.h>
70 #include <drm/drm_hashtab.h>
71 #include <drm/drm_mem_util.h>
72 #include <drm/drm_mm.h>
73 #include <drm/drm_os_linux.h>
74 #include <drm/drm_sarea.h>
75 #include <drm/drm_vma_manager.h>
83 struct drm_device_dma;
84 struct drm_dma_handle;
88 struct reservation_object;
91 * 4 debug categories are defined:
93 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
94 * This is the category used by the DRM_DEBUG() macro.
96 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
97 * This is the category used by the DRM_DEBUG_DRIVER() macro.
99 * KMS: used in the modesetting code.
100 * This is the category used by the DRM_DEBUG_KMS() macro.
102 * PRIME: used in the prime code.
103 * This is the category used by the DRM_DEBUG_PRIME() macro.
105 * Enabling verbose debug messages is done through the drm.debug parameter,
106 * each category being enabled by a bit.
108 * drm.debug=0x1 will enable CORE messages
109 * drm.debug=0x2 will enable DRIVER messages
110 * drm.debug=0x3 will enable CORE and DRIVER messages
112 * drm.debug=0xf will enable all messages
114 * An interesting feature is that it's possible to enable verbose logging at
115 * run-time by echoing the debug value in its sysfs node:
116 * # echo 0xf > /sys/module/drm/parameters/debug
118 #define DRM_UT_CORE 0x01
119 #define DRM_UT_DRIVER 0x02
120 #define DRM_UT_KMS 0x04
121 #define DRM_UT_PRIME 0x08
123 extern __printf(2, 3)
124 void drm_ut_debug_printk(const char *function_name,
125 const char *format, ...);
126 extern __printf(2, 3)
127 int drm_err(const char *func, const char *format, ...);
129 /***********************************************************************/
130 /** \name DRM template customization defaults */
133 /* driver capabilities and requirements mask */
134 #define DRIVER_USE_AGP 0x1
135 #define DRIVER_PCI_DMA 0x8
136 #define DRIVER_SG 0x10
137 #define DRIVER_HAVE_DMA 0x20
138 #define DRIVER_HAVE_IRQ 0x40
139 #define DRIVER_IRQ_SHARED 0x80
140 #define DRIVER_GEM 0x1000
141 #define DRIVER_MODESET 0x2000
142 #define DRIVER_PRIME 0x4000
143 #define DRIVER_RENDER 0x8000
145 /***********************************************************************/
146 /** \name Begin the DRM... */
149 #define DRM_MAGIC_HASH_ORDER 4 /**< Size of key hash table. Must be power of 2. */
153 /***********************************************************************/
154 /** \name Macros to make printk easier */
160 * \param fmt printf() like format string.
161 * \param arg arguments
163 #define DRM_ERROR(fmt, ...) \
164 drm_err(__func__, fmt, ##__VA_ARGS__)
167 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
169 * \param fmt printf() like format string.
170 * \param arg arguments
172 #define DRM_ERROR_RATELIMITED(fmt, ...) \
174 static DEFINE_RATELIMIT_STATE(_rs, \
175 DEFAULT_RATELIMIT_INTERVAL, \
176 DEFAULT_RATELIMIT_BURST); \
178 if (__ratelimit(&_rs)) \
179 drm_err(__func__, fmt, ##__VA_ARGS__); \
182 #define DRM_INFO(fmt, ...) \
183 printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
185 #define DRM_INFO_ONCE(fmt, ...) \
186 printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
191 * \param fmt printf() like format string.
192 * \param arg arguments
194 #define DRM_DEBUG(fmt, args...) \
196 if (unlikely(drm_debug & DRM_UT_CORE)) \
197 drm_ut_debug_printk(__func__, fmt, ##args); \
200 #define DRM_DEBUG_DRIVER(fmt, args...) \
202 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
203 drm_ut_debug_printk(__func__, fmt, ##args); \
205 #define DRM_DEBUG_KMS(fmt, args...) \
207 if (unlikely(drm_debug & DRM_UT_KMS)) \
208 drm_ut_debug_printk(__func__, fmt, ##args); \
210 #define DRM_DEBUG_PRIME(fmt, args...) \
212 if (unlikely(drm_debug & DRM_UT_PRIME)) \
213 drm_ut_debug_printk(__func__, fmt, ##args); \
218 /***********************************************************************/
219 /** \name Internal types and structures */
222 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
225 * Ioctl function type.
227 * \param inode device inode.
228 * \param file_priv DRM file private pointer.
229 * \param cmd command.
230 * \param arg argument.
232 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
233 struct drm_file *file_priv);
235 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
238 #define DRM_IOCTL_NR(n) _IOC_NR(n)
239 #define DRM_MAJOR 226
242 #define DRM_MASTER 0x2
243 #define DRM_ROOT_ONLY 0x4
244 #define DRM_CONTROL_ALLOW 0x8
245 #define DRM_UNLOCKED 0x10
246 #define DRM_RENDER_ALLOW 0x20
248 struct drm_ioctl_desc {
252 unsigned int cmd_drv;
257 * Creates a driver or general drm_ioctl_desc array entry for the given
258 * ioctl, for use by drm_ioctl().
261 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
262 [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl, .name = #ioctl}
264 /* Event queued up for userspace to read */
265 struct drm_pending_event {
266 struct drm_event *event;
267 struct list_head link;
268 struct drm_file *file_priv;
269 pid_t pid; /* pid of requester, no guarantee it's valid by the time
270 we deliver the event, for tracing only */
271 void (*destroy)(struct drm_pending_event *event);
274 /* initial implementaton using a linked list - todo hashtab */
275 struct drm_prime_file_private {
276 struct list_head head;
280 /** File private data */
282 unsigned authenticated :1;
283 /* Whether we're master for a minor. Protected by master_mutex */
284 unsigned is_master :1;
285 /* true when the client has asked us to expose stereo 3D mode flags */
286 unsigned stereo_allowed :1;
288 * true if client understands CRTC primary planes and cursor planes
291 unsigned universal_planes:1;
296 struct list_head lhead;
297 struct drm_minor *minor;
298 unsigned long lock_count;
300 /** Mapping of mm object handles to object pointers. */
301 struct idr object_idr;
302 /** Lock for synchronization of access to object_idr. */
303 spinlock_t table_lock;
308 struct drm_master *master; /* master this node is currently associated with
309 N.B. not always minor->master */
311 * fbs - List of framebuffers associated with this file.
313 * Protected by fbs_lock. Note that the fbs list holds a reference on
314 * the fb object to prevent it from untimely disappearing.
316 struct list_head fbs;
317 struct mutex fbs_lock;
319 wait_queue_head_t event_wait;
320 struct list_head event_list;
323 struct drm_prime_file_private prime;
329 struct drm_lock_data {
330 struct drm_hw_lock *hw_lock; /**< Hardware lock */
331 /** Private of lock holder's file (NULL=kernel) */
332 struct drm_file *file_priv;
333 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
334 unsigned long lock_time; /**< Time of last lock in jiffies */
336 uint32_t kernel_waiters;
337 uint32_t user_waiters;
342 * This structure defines the drm_mm memory object, which will be used by the
343 * DRM for its buffer objects.
345 struct drm_gem_object {
346 /** Reference count of this object */
347 struct kref refcount;
350 * handle_count - gem file_priv handle count of this object
352 * Each handle also holds a reference. Note that when the handle_count
353 * drops to 0 any global names (e.g. the id in the flink namespace) will
356 * Protected by dev->object_name_lock.
358 unsigned handle_count;
360 /** Related drm device */
361 struct drm_device *dev;
363 /** File representing the shmem storage */
366 /* Mapping info for this object */
367 struct drm_vma_offset_node vma_node;
370 * Size of the object, in bytes. Immutable over the object's
376 * Global name for this object, starts at 1. 0 means unnamed.
377 * Access is covered by the object_name_lock in the related drm_device
382 * Memory domains. These monitor which caches contain read/write data
383 * related to the object. When transitioning from one set of domains
384 * to another, the driver is called to ensure that caches are suitably
385 * flushed and invalidated
387 uint32_t read_domains;
388 uint32_t write_domain;
391 * While validating an exec operation, the
392 * new read/write domain values are computed here.
393 * They will be transferred to the above values
394 * at the point that any cache flushing occurs
396 uint32_t pending_read_domains;
397 uint32_t pending_write_domain;
400 * dma_buf - dma buf associated with this GEM object
402 * Pointer to the dma-buf associated with this gem object (either
403 * through importing or exporting). We break the resulting reference
404 * loop when the last gem handle for this object is released.
406 * Protected by obj->object_name_lock
408 struct dma_buf *dma_buf;
411 * import_attach - dma buf attachment backing this object
413 * Any foreign dma_buf imported as a gem object has this set to the
414 * attachment point for the device. This is invariant over the lifetime
417 * The driver's ->gem_free_object callback is responsible for cleaning
418 * up the dma_buf attachment and references acquired at import time.
420 * Note that the drm gem/prime core does not depend upon drivers setting
421 * this field any more. So for drivers where this doesn't make sense
422 * (e.g. virtual devices or a displaylink behind an usb bus) they can
423 * simply leave it as NULL.
425 struct dma_buf_attachment *import_attach;
429 * struct drm_master - drm master structure
431 * @refcount: Refcount for this master object.
432 * @minor: Link back to minor char device we are master for. Immutable.
433 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
434 * @unique_len: Length of unique field. Protected by drm_global_mutex.
435 * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
436 * @magicfree: List of used authentication tokens. Protected by struct_mutex.
437 * @lock: DRI lock information.
438 * @driver_priv: Pointer to driver-private information.
441 struct kref refcount;
442 struct drm_minor *minor;
445 struct drm_open_hash magiclist;
446 struct list_head magicfree;
447 struct drm_lock_data lock;
451 /* Size of ringbuffer for vblank timestamps. Just double-buffer
452 * in initial implementation.
454 #define DRM_VBLANKTIME_RBSIZE 2
456 /* Flags and return codes for get_vblank_timestamp() driver function. */
457 #define DRM_CALLED_FROM_VBLIRQ 1
458 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
459 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
461 /* get_scanout_position() return flags */
462 #define DRM_SCANOUTPOS_VALID (1 << 0)
463 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
464 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
467 * DRM driver structure. This structure represent the common code for
468 * a family of cards. There will one drm_device for each card present
472 int (*load) (struct drm_device *, unsigned long flags);
473 int (*firstopen) (struct drm_device *);
474 int (*open) (struct drm_device *, struct drm_file *);
475 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
476 void (*postclose) (struct drm_device *, struct drm_file *);
477 void (*lastclose) (struct drm_device *);
478 int (*unload) (struct drm_device *);
479 int (*suspend) (struct drm_device *, pm_message_t state);
480 int (*resume) (struct drm_device *);
481 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
482 int (*dma_quiescent) (struct drm_device *);
483 int (*context_dtor) (struct drm_device *dev, int context);
484 int (*set_busid)(struct drm_device *dev, struct drm_master *master);
487 * get_vblank_counter - get raw hardware vblank counter
489 * @crtc: counter to fetch
491 * Driver callback for fetching a raw hardware vblank counter for @crtc.
492 * If a device doesn't have a hardware counter, the driver can simply
493 * return the value of drm_vblank_count. The DRM core will account for
494 * missed vblank events while interrupts where disabled based on system
497 * Wraparound handling and loss of events due to modesetting is dealt
498 * with in the DRM core code.
501 * Raw vblank counter value.
503 u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
506 * enable_vblank - enable vblank interrupt events
508 * @crtc: which irq to enable
510 * Enable vblank interrupts for @crtc. If the device doesn't have
511 * a hardware vblank counter, this routine should be a no-op, since
512 * interrupts will have to stay on to keep the count accurate.
515 * Zero on success, appropriate errno if the given @crtc's vblank
516 * interrupt cannot be enabled.
518 int (*enable_vblank) (struct drm_device *dev, int crtc);
521 * disable_vblank - disable vblank interrupt events
523 * @crtc: which irq to enable
525 * Disable vblank interrupts for @crtc. If the device doesn't have
526 * a hardware vblank counter, this routine should be a no-op, since
527 * interrupts will have to stay on to keep the count accurate.
529 void (*disable_vblank) (struct drm_device *dev, int crtc);
532 * Called by \c drm_device_is_agp. Typically used to determine if a
533 * card is really attached to AGP or not.
535 * \param dev DRM device handle
538 * One of three values is returned depending on whether or not the
539 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
540 * (return of 1), or may or may not be AGP (return of 2).
542 int (*device_is_agp) (struct drm_device *dev);
545 * Called by vblank timestamping code.
547 * Return the current display scanout position from a crtc, and an
548 * optional accurate ktime_get timestamp of when position was measured.
550 * \param dev DRM device.
551 * \param crtc Id of the crtc to query.
552 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
553 * \param *vpos Target location for current vertical scanout position.
554 * \param *hpos Target location for current horizontal scanout position.
555 * \param *stime Target location for timestamp taken immediately before
556 * scanout position query. Can be NULL to skip timestamp.
557 * \param *etime Target location for timestamp taken immediately after
558 * scanout position query. Can be NULL to skip timestamp.
560 * Returns vpos as a positive number while in active scanout area.
561 * Returns vpos as a negative number inside vblank, counting the number
562 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
563 * until start of active scanout / end of vblank."
565 * \return Flags, or'ed together as follows:
567 * DRM_SCANOUTPOS_VALID = Query successful.
568 * DRM_SCANOUTPOS_INVBL = Inside vblank.
569 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
570 * this flag means that returned position may be offset by a constant
571 * but unknown small number of scanlines wrt. real scanout position.
574 int (*get_scanout_position) (struct drm_device *dev, int crtc,
576 int *vpos, int *hpos, ktime_t *stime,
580 * Called by \c drm_get_last_vbltimestamp. Should return a precise
581 * timestamp when the most recent VBLANK interval ended or will end.
583 * Specifically, the timestamp in @vblank_time should correspond as
584 * closely as possible to the time when the first video scanline of
585 * the video frame after the end of VBLANK will start scanning out,
586 * the time immediately after end of the VBLANK interval. If the
587 * @crtc is currently inside VBLANK, this will be a time in the future.
588 * If the @crtc is currently scanning out a frame, this will be the
589 * past start time of the current scanout. This is meant to adhere
590 * to the OpenML OML_sync_control extension specification.
592 * \param dev dev DRM device handle.
593 * \param crtc crtc for which timestamp should be returned.
594 * \param *max_error Maximum allowable timestamp error in nanoseconds.
595 * Implementation should strive to provide timestamp
596 * with an error of at most *max_error nanoseconds.
597 * Returns true upper bound on error for timestamp.
598 * \param *vblank_time Target location for returned vblank timestamp.
599 * \param flags 0 = Defaults, no special treatment needed.
600 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
601 * irq handler. Some drivers need to apply some workarounds
602 * for gpu-specific vblank irq quirks if flag is set.
605 * Zero if timestamping isn't supported in current display mode or a
606 * negative number on failure. A positive status code on success,
607 * which describes how the vblank_time timestamp was computed.
609 int (*get_vblank_timestamp) (struct drm_device *dev, int crtc,
611 struct timeval *vblank_time,
614 /* these have to be filled in */
616 irqreturn_t(*irq_handler) (int irq, void *arg);
617 void (*irq_preinstall) (struct drm_device *dev);
618 int (*irq_postinstall) (struct drm_device *dev);
619 void (*irq_uninstall) (struct drm_device *dev);
621 /* Master routines */
622 int (*master_create)(struct drm_device *dev, struct drm_master *master);
623 void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
625 * master_set is called whenever the minor master is set.
626 * master_drop is called whenever the minor master is dropped.
629 int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
631 void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
634 int (*debugfs_init)(struct drm_minor *minor);
635 void (*debugfs_cleanup)(struct drm_minor *minor);
638 * Driver-specific constructor for drm_gem_objects, to set up
639 * obj->driver_private.
641 * Returns 0 on success.
643 void (*gem_free_object) (struct drm_gem_object *obj);
644 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
645 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
648 /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
649 int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
650 uint32_t handle, uint32_t flags, int *prime_fd);
651 /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
652 int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
653 int prime_fd, uint32_t *handle);
654 /* export GEM -> dmabuf */
655 struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
656 struct drm_gem_object *obj, int flags);
657 /* import dmabuf -> GEM */
658 struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
659 struct dma_buf *dma_buf);
660 /* low-level interface used by drm_gem_prime_{import,export} */
661 int (*gem_prime_pin)(struct drm_gem_object *obj);
662 void (*gem_prime_unpin)(struct drm_gem_object *obj);
663 struct reservation_object * (*gem_prime_res_obj)(
664 struct drm_gem_object *obj);
665 struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
666 struct drm_gem_object *(*gem_prime_import_sg_table)(
667 struct drm_device *dev, size_t size,
668 struct sg_table *sgt);
669 void *(*gem_prime_vmap)(struct drm_gem_object *obj);
670 void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
671 int (*gem_prime_mmap)(struct drm_gem_object *obj,
672 struct vm_area_struct *vma);
674 /* vga arb irq handler */
675 void (*vgaarb_irq)(struct drm_device *dev, bool state);
677 /* dumb alloc support */
678 int (*dumb_create)(struct drm_file *file_priv,
679 struct drm_device *dev,
680 struct drm_mode_create_dumb *args);
681 int (*dumb_map_offset)(struct drm_file *file_priv,
682 struct drm_device *dev, uint32_t handle,
684 int (*dumb_destroy)(struct drm_file *file_priv,
685 struct drm_device *dev,
688 /* Driver private ops for this object */
689 const struct vm_operations_struct *gem_vm_ops;
700 const struct drm_ioctl_desc *ioctls;
702 const struct file_operations *fops;
704 /* List of devices hanging off this driver with stealth attach. */
705 struct list_head legacy_dev_list;
708 enum drm_minor_type {
716 * Info file list entry. This structure represents a debugfs or proc file to
717 * be created by the drm core
719 struct drm_info_list {
720 const char *name; /** file name */
721 int (*show)(struct seq_file*, void*); /** show callback */
722 u32 driver_features; /**< Required driver features for this entry */
727 * debugfs node structure. This structure represents a debugfs file.
729 struct drm_info_node {
730 struct list_head list;
731 struct drm_minor *minor;
732 const struct drm_info_list *info_ent;
737 * DRM minor structure. This structure represents a drm minor number.
740 int index; /**< Minor device number */
741 int type; /**< Control or render */
742 struct device *kdev; /**< Linux device */
743 struct drm_device *dev;
745 struct dentry *debugfs_root;
747 struct list_head debugfs_list;
748 struct mutex debugfs_lock; /* Protects debugfs_list. */
750 /* currently active master for this node. Protected by master_mutex */
751 struct drm_master *master;
752 struct drm_mode_group mode_group;
756 struct drm_pending_vblank_event {
757 struct drm_pending_event base;
759 struct drm_event_vblank event;
762 struct drm_vblank_crtc {
763 struct drm_device *dev; /* pointer to the drm_device */
764 wait_queue_head_t queue; /**< VBLANK wait queue */
765 struct timeval time[DRM_VBLANKTIME_RBSIZE]; /**< timestamp of current count */
766 struct timer_list disable_timer; /* delayed disable timer */
767 atomic_t count; /**< number of VBLANK interrupts */
768 atomic_t refcount; /* number of users of vblank interruptsper crtc */
769 u32 last; /* protected by dev->vbl_lock, used */
770 /* for wraparound handling */
771 u32 last_wait; /* Last vblank seqno waited per CRTC */
772 unsigned int inmodeset; /* Display driver is setting mode */
773 int crtc; /* crtc index */
774 bool enabled; /* so we don't call enable more than
779 * DRM device structure. This structure represent a complete card that
780 * may contain multiple heads.
783 struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
784 int if_version; /**< Highest interface version set */
786 /** \name Lifetime Management */
788 struct kref ref; /**< Object ref-count */
789 struct device *dev; /**< Device structure of bus-device */
790 struct drm_driver *driver; /**< DRM driver managing the device */
791 void *dev_private; /**< DRM driver private data */
792 struct drm_minor *control; /**< Control node */
793 struct drm_minor *primary; /**< Primary node */
794 struct drm_minor *render; /**< Render node */
795 atomic_t unplugged; /**< Flag whether dev is dead */
796 struct inode *anon_inode; /**< inode for private address-space */
797 char *unique; /**< unique name of the device */
802 struct mutex struct_mutex; /**< For others */
803 struct mutex master_mutex; /**< For drm_minor::master and drm_file::is_master */
806 /** \name Usage Counters */
808 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
809 spinlock_t buf_lock; /**< For drm_device::buf_use and a few other things. */
810 int buf_use; /**< Buffers in use -- cannot alloc */
811 atomic_t buf_alloc; /**< Buffer allocation in progress */
814 struct list_head filelist;
816 /** \name Memory management */
818 struct list_head maplist; /**< Linked list of regions */
819 struct drm_open_hash map_hash; /**< User token hash table for maps */
821 /** \name Context handle management */
823 struct list_head ctxlist; /**< Linked list of context handles */
824 struct mutex ctxlist_mutex; /**< For ctxlist */
828 struct list_head vmalist; /**< List of vmas (for debugging) */
832 /** \name DMA support */
834 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
837 /** \name Context support */
839 bool irq_enabled; /**< True if irq handler is enabled */
842 __volatile__ long context_flag; /**< Context swapping flag */
843 int last_context; /**< Last current context */
846 /** \name VBLANK IRQ support */
850 * At load time, disabling the vblank interrupt won't be allowed since
851 * old clients may not call the modeset ioctl and therefore misbehave.
852 * Once the modeset ioctl *has* been called though, we can safely
853 * disable them when unused.
855 bool vblank_disable_allowed;
858 * If true, vblank interrupt will be disabled immediately when the
859 * refcount drops to zero, as opposed to via the vblank disable
861 * This can be set to true it the hardware has a working vblank
862 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
865 bool vblank_disable_immediate;
867 /* array of size num_crtcs */
868 struct drm_vblank_crtc *vblank;
870 spinlock_t vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
873 u32 max_vblank_count; /**< size of vblank counter register */
878 struct list_head vblank_event_list;
879 spinlock_t event_lock;
883 struct drm_agp_head *agp; /**< AGP data */
885 struct pci_dev *pdev; /**< PCI device structure */
887 struct pci_controller *hose;
890 struct platform_device *platformdev; /**< Platform device struture */
892 struct drm_sg_mem *sg; /**< Scatter gather memory */
893 unsigned int num_crtcs; /**< Number of CRTCs on this device */
898 struct drm_hw_lock *lock;
901 struct drm_local_map *agp_buffer_map;
902 unsigned int agp_buffer_token;
904 struct drm_mode_config mode_config; /**< Current mode config */
906 /** \name GEM information */
908 struct mutex object_name_lock;
909 struct idr object_name_idr;
910 struct drm_vma_offset_manager *vma_offset_manager;
912 int switch_power_state;
915 #define DRM_SWITCH_POWER_ON 0
916 #define DRM_SWITCH_POWER_OFF 1
917 #define DRM_SWITCH_POWER_CHANGING 2
918 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
920 static __inline__ int drm_core_check_feature(struct drm_device *dev,
923 return ((dev->driver->driver_features & feature) ? 1 : 0);
926 static inline void drm_device_set_unplugged(struct drm_device *dev)
929 atomic_set(&dev->unplugged, 1);
932 static inline int drm_device_is_unplugged(struct drm_device *dev)
934 int ret = atomic_read(&dev->unplugged);
939 static inline bool drm_is_render_client(const struct drm_file *file_priv)
941 return file_priv->minor->type == DRM_MINOR_RENDER;
944 static inline bool drm_is_control_client(const struct drm_file *file_priv)
946 return file_priv->minor->type == DRM_MINOR_CONTROL;
949 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
951 return file_priv->minor->type == DRM_MINOR_LEGACY;
954 /******************************************************************/
955 /** \name Internal function definitions */
958 /* Driver support (drm_drv.h) */
959 extern long drm_ioctl(struct file *filp,
960 unsigned int cmd, unsigned long arg);
961 extern long drm_compat_ioctl(struct file *filp,
962 unsigned int cmd, unsigned long arg);
963 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
965 /* Device support (drm_fops.h) */
966 extern int drm_open(struct inode *inode, struct file *filp);
967 extern ssize_t drm_read(struct file *filp, char __user *buffer,
968 size_t count, loff_t *offset);
969 extern int drm_release(struct inode *inode, struct file *filp);
971 /* Mapping support (drm_vm.h) */
972 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
973 extern void drm_vm_open_locked(struct drm_device *dev, struct vm_area_struct *vma);
974 extern unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
976 /* Misc. IOCTL support (drm_ioctl.c) */
977 int drm_noop(struct drm_device *dev, void *data,
978 struct drm_file *file_priv);
980 /* Cache management (drm_cache.c) */
981 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
982 void drm_clflush_sg(struct sg_table *st);
983 void drm_clflush_virt_range(void *addr, unsigned long length);
986 * These are exported to drivers so that they can implement fencing using
987 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
990 /* IRQ support (drm_irq.h) */
991 extern int drm_irq_install(struct drm_device *dev, int irq);
992 extern int drm_irq_uninstall(struct drm_device *dev);
994 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
995 extern int drm_wait_vblank(struct drm_device *dev, void *data,
996 struct drm_file *filp);
997 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
998 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
999 struct timeval *vblanktime);
1000 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1001 struct drm_pending_vblank_event *e);
1002 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1003 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1004 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1005 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1006 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1007 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
1008 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1009 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1010 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1011 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1012 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1013 extern void drm_vblank_cleanup(struct drm_device *dev);
1015 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1016 int crtc, int *max_error,
1017 struct timeval *vblank_time,
1019 const struct drm_crtc *refcrtc,
1020 const struct drm_display_mode *mode);
1021 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1022 const struct drm_display_mode *mode);
1025 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1026 * @crtc: which CRTC's vblank waitqueue to retrieve
1028 * This function returns a pointer to the vblank waitqueue for the CRTC.
1029 * Drivers can use this to implement vblank waits using wait_event() & co.
1031 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1033 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1036 /* Modesetting support */
1037 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1038 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1039 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1040 struct drm_file *file_priv);
1042 /* Stub support (drm_stub.h) */
1043 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1044 struct drm_file *file_priv);
1045 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1046 struct drm_file *file_priv);
1047 struct drm_master *drm_master_create(struct drm_minor *minor);
1048 extern struct drm_master *drm_master_get(struct drm_master *master);
1049 extern void drm_master_put(struct drm_master **master);
1051 extern void drm_put_dev(struct drm_device *dev);
1052 extern void drm_unplug_dev(struct drm_device *dev);
1053 extern unsigned int drm_debug;
1055 extern struct class *drm_class;
1057 /* Debugfs support */
1058 #if defined(CONFIG_DEBUG_FS)
1059 extern int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1060 struct dentry *root);
1061 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1062 int count, struct dentry *root,
1063 struct drm_minor *minor);
1064 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1065 int count, struct drm_minor *minor);
1066 extern int drm_debugfs_cleanup(struct drm_minor *minor);
1067 extern int drm_debugfs_connector_add(struct drm_connector *connector);
1068 extern void drm_debugfs_connector_remove(struct drm_connector *connector);
1070 static inline int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1071 struct dentry *root)
1076 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1077 int count, struct dentry *root,
1078 struct drm_minor *minor)
1083 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1084 int count, struct drm_minor *minor)
1089 static inline int drm_debugfs_cleanup(struct drm_minor *minor)
1094 static inline int drm_debugfs_connector_add(struct drm_connector *connector)
1098 static inline void drm_debugfs_connector_remove(struct drm_connector *connector)
1104 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1105 struct drm_gem_object *obj, int flags);
1106 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1107 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1109 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1110 struct dma_buf *dma_buf);
1111 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1112 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1113 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1115 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1116 dma_addr_t *addrs, int max_pages);
1117 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages);
1118 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1120 int drm_gem_dumb_destroy(struct drm_file *file,
1121 struct drm_device *dev,
1125 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1127 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1129 /* sysfs support (drm_sysfs.c) */
1130 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1132 /* Graphics Execution Manager library functions (drm_gem.c) */
1133 void drm_gem_object_release(struct drm_gem_object *obj);
1134 void drm_gem_object_free(struct kref *kref);
1135 int drm_gem_object_init(struct drm_device *dev,
1136 struct drm_gem_object *obj, size_t size);
1137 void drm_gem_private_object_init(struct drm_device *dev,
1138 struct drm_gem_object *obj, size_t size);
1139 void drm_gem_vm_open(struct vm_area_struct *vma);
1140 void drm_gem_vm_close(struct vm_area_struct *vma);
1141 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
1142 struct vm_area_struct *vma);
1143 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
1146 drm_gem_object_reference(struct drm_gem_object *obj)
1148 kref_get(&obj->refcount);
1152 drm_gem_object_unreference(struct drm_gem_object *obj)
1155 kref_put(&obj->refcount, drm_gem_object_free);
1159 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1161 if (obj && !atomic_add_unless(&obj->refcount.refcount, -1, 1)) {
1162 struct drm_device *dev = obj->dev;
1164 mutex_lock(&dev->struct_mutex);
1165 if (likely(atomic_dec_and_test(&obj->refcount.refcount)))
1166 drm_gem_object_free(&obj->refcount);
1167 mutex_unlock(&dev->struct_mutex);
1171 int drm_gem_handle_create(struct drm_file *file_priv,
1172 struct drm_gem_object *obj,
1174 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
1177 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1178 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1179 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
1181 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
1182 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
1183 bool dirty, bool accessed);
1185 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1186 struct drm_file *filp,
1189 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1190 struct device *parent);
1191 void drm_dev_ref(struct drm_device *dev);
1192 void drm_dev_unref(struct drm_device *dev);
1193 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1194 void drm_dev_unregister(struct drm_device *dev);
1195 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1197 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1198 void drm_minor_release(struct drm_minor *minor);
1203 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1205 if (dev->driver->device_is_agp != NULL) {
1206 int err = (*dev->driver->device_is_agp) (dev);
1213 return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1215 void drm_pci_agp_destroy(struct drm_device *dev);
1217 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1218 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1219 extern int drm_get_pci_dev(struct pci_dev *pdev,
1220 const struct pci_device_id *ent,
1221 struct drm_driver *driver);
1222 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1224 #define DRM_PCIE_SPEED_25 1
1225 #define DRM_PCIE_SPEED_50 2
1226 #define DRM_PCIE_SPEED_80 4
1228 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1230 /* platform section */
1231 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1232 extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
1234 /* returns true if currently okay to sleep */
1235 static __inline__ bool drm_can_sleep(void)
1237 if (in_atomic() || in_dbg_master() || irqs_disabled())