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;
85 struct drm_gem_object;
89 struct reservation_object;
90 struct dma_buf_attachment;
93 * 4 debug categories are defined:
95 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
96 * This is the category used by the DRM_DEBUG() macro.
98 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
99 * This is the category used by the DRM_DEBUG_DRIVER() macro.
101 * KMS: used in the modesetting code.
102 * This is the category used by the DRM_DEBUG_KMS() macro.
104 * PRIME: used in the prime code.
105 * This is the category used by the DRM_DEBUG_PRIME() macro.
107 * ATOMIC: used in the atomic code.
108 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
110 * VBL: used for verbose debug message in the vblank code
111 * This is the category used by the DRM_DEBUG_VBL() macro.
113 * Enabling verbose debug messages is done through the drm.debug parameter,
114 * each category being enabled by a bit.
116 * drm.debug=0x1 will enable CORE messages
117 * drm.debug=0x2 will enable DRIVER messages
118 * drm.debug=0x3 will enable CORE and DRIVER messages
120 * drm.debug=0x3f will enable all messages
122 * An interesting feature is that it's possible to enable verbose logging at
123 * run-time by echoing the debug value in its sysfs node:
124 * # echo 0xf > /sys/module/drm/parameters/debug
126 #define DRM_UT_CORE 0x01
127 #define DRM_UT_DRIVER 0x02
128 #define DRM_UT_KMS 0x04
129 #define DRM_UT_PRIME 0x08
130 #define DRM_UT_ATOMIC 0x10
131 #define DRM_UT_VBL 0x20
133 extern __printf(2, 3)
134 void drm_ut_debug_printk(const char *function_name,
135 const char *format, ...);
136 extern __printf(1, 2)
137 void drm_err(const char *format, ...);
139 /***********************************************************************/
140 /** \name DRM template customization defaults */
143 /* driver capabilities and requirements mask */
144 #define DRIVER_USE_AGP 0x1
145 #define DRIVER_PCI_DMA 0x8
146 #define DRIVER_SG 0x10
147 #define DRIVER_HAVE_DMA 0x20
148 #define DRIVER_HAVE_IRQ 0x40
149 #define DRIVER_IRQ_SHARED 0x80
150 #define DRIVER_GEM 0x1000
151 #define DRIVER_MODESET 0x2000
152 #define DRIVER_PRIME 0x4000
153 #define DRIVER_RENDER 0x8000
154 #define DRIVER_ATOMIC 0x10000
155 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
157 /***********************************************************************/
158 /** \name Macros to make printk easier */
161 #define _DRM_PRINTK(once, level, fmt, ...) \
163 printk##once(KERN_##level "[" DRM_NAME "] " fmt, \
167 #define DRM_INFO(fmt, ...) \
168 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
169 #define DRM_NOTE(fmt, ...) \
170 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
171 #define DRM_WARN(fmt, ...) \
172 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
174 #define DRM_INFO_ONCE(fmt, ...) \
175 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
176 #define DRM_NOTE_ONCE(fmt, ...) \
177 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
178 #define DRM_WARN_ONCE(fmt, ...) \
179 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
184 * \param fmt printf() like format string.
185 * \param arg arguments
187 #define DRM_ERROR(fmt, ...) \
188 drm_err(fmt, ##__VA_ARGS__)
191 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
193 * \param fmt printf() like format string.
194 * \param arg arguments
196 #define DRM_ERROR_RATELIMITED(fmt, ...) \
198 static DEFINE_RATELIMIT_STATE(_rs, \
199 DEFAULT_RATELIMIT_INTERVAL, \
200 DEFAULT_RATELIMIT_BURST); \
202 if (__ratelimit(&_rs)) \
203 drm_err(fmt, ##__VA_ARGS__); \
209 * \param fmt printf() like format string.
210 * \param arg arguments
212 #define DRM_DEBUG(fmt, args...) \
214 if (unlikely(drm_debug & DRM_UT_CORE)) \
215 drm_ut_debug_printk(__func__, fmt, ##args); \
218 #define DRM_DEBUG_DRIVER(fmt, args...) \
220 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
221 drm_ut_debug_printk(__func__, fmt, ##args); \
223 #define DRM_DEBUG_KMS(fmt, args...) \
225 if (unlikely(drm_debug & DRM_UT_KMS)) \
226 drm_ut_debug_printk(__func__, fmt, ##args); \
228 #define DRM_DEBUG_PRIME(fmt, args...) \
230 if (unlikely(drm_debug & DRM_UT_PRIME)) \
231 drm_ut_debug_printk(__func__, fmt, ##args); \
233 #define DRM_DEBUG_ATOMIC(fmt, args...) \
235 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
236 drm_ut_debug_printk(__func__, fmt, ##args); \
238 #define DRM_DEBUG_VBL(fmt, args...) \
240 if (unlikely(drm_debug & DRM_UT_VBL)) \
241 drm_ut_debug_printk(__func__, fmt, ##args); \
246 /***********************************************************************/
247 /** \name Internal types and structures */
250 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
253 * Ioctl function type.
255 * \param inode device inode.
256 * \param file_priv DRM file private pointer.
257 * \param cmd command.
258 * \param arg argument.
260 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
261 struct drm_file *file_priv);
263 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
266 #define DRM_IOCTL_NR(n) _IOC_NR(n)
267 #define DRM_MAJOR 226
270 #define DRM_MASTER 0x2
271 #define DRM_ROOT_ONLY 0x4
272 #define DRM_CONTROL_ALLOW 0x8
273 #define DRM_UNLOCKED 0x10
274 #define DRM_RENDER_ALLOW 0x20
276 struct drm_ioctl_desc {
284 * Creates a driver or general drm_ioctl_desc array entry for the given
285 * ioctl, for use by drm_ioctl().
288 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
289 [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = { \
290 .cmd = DRM_IOCTL_##ioctl, \
296 /* Event queued up for userspace to read */
297 struct drm_pending_event {
298 struct drm_event *event;
299 struct list_head link;
300 struct drm_file *file_priv;
301 pid_t pid; /* pid of requester, no guarantee it's valid by the time
302 we deliver the event, for tracing only */
303 void (*destroy)(struct drm_pending_event *event);
306 /* initial implementaton using a linked list - todo hashtab */
307 struct drm_prime_file_private {
308 struct list_head head;
312 /** File private data */
314 unsigned authenticated :1;
315 /* Whether we're master for a minor. Protected by master_mutex */
316 unsigned is_master :1;
317 /* true when the client has asked us to expose stereo 3D mode flags */
318 unsigned stereo_allowed :1;
320 * true if client understands CRTC primary planes and cursor planes
323 unsigned universal_planes:1;
324 /* true if client understands atomic properties */
327 * This client is allowed to gain master privileges for @master.
328 * Protected by struct drm_device::master_mutex.
330 unsigned allowed_master:1;
335 struct list_head lhead;
336 struct drm_minor *minor;
337 unsigned long lock_count;
339 /** Mapping of mm object handles to object pointers. */
340 struct idr object_idr;
341 /** Lock for synchronization of access to object_idr. */
342 spinlock_t table_lock;
347 struct drm_master *master; /* master this node is currently associated with
348 N.B. not always minor->master */
350 * fbs - List of framebuffers associated with this file.
352 * Protected by fbs_lock. Note that the fbs list holds a reference on
353 * the fb object to prevent it from untimely disappearing.
355 struct list_head fbs;
356 struct mutex fbs_lock;
358 /** User-created blob properties; this retains a reference on the
360 struct list_head blobs;
362 wait_queue_head_t event_wait;
363 struct list_head event_list;
366 struct drm_prime_file_private prime;
372 struct drm_lock_data {
373 struct drm_hw_lock *hw_lock; /**< Hardware lock */
374 /** Private of lock holder's file (NULL=kernel) */
375 struct drm_file *file_priv;
376 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
377 unsigned long lock_time; /**< Time of last lock in jiffies */
379 uint32_t kernel_waiters;
380 uint32_t user_waiters;
385 * struct drm_master - drm master structure
387 * @refcount: Refcount for this master object.
388 * @minor: Link back to minor char device we are master for. Immutable.
389 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
390 * @unique_len: Length of unique field. Protected by drm_global_mutex.
391 * @magic_map: Map of used authentication tokens. Protected by struct_mutex.
392 * @lock: DRI lock information.
393 * @driver_priv: Pointer to driver-private information.
396 struct kref refcount;
397 struct drm_minor *minor;
400 struct idr magic_map;
401 struct drm_lock_data lock;
405 /* Size of ringbuffer for vblank timestamps. Just double-buffer
406 * in initial implementation.
408 #define DRM_VBLANKTIME_RBSIZE 2
410 /* Flags and return codes for get_vblank_timestamp() driver function. */
411 #define DRM_CALLED_FROM_VBLIRQ 1
412 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
413 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
415 /* get_scanout_position() return flags */
416 #define DRM_SCANOUTPOS_VALID (1 << 0)
417 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
418 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
421 * DRM driver structure. This structure represent the common code for
422 * a family of cards. There will one drm_device for each card present
426 int (*load) (struct drm_device *, unsigned long flags);
427 int (*firstopen) (struct drm_device *);
428 int (*open) (struct drm_device *, struct drm_file *);
429 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
430 void (*postclose) (struct drm_device *, struct drm_file *);
431 void (*lastclose) (struct drm_device *);
432 int (*unload) (struct drm_device *);
433 int (*suspend) (struct drm_device *, pm_message_t state);
434 int (*resume) (struct drm_device *);
435 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
436 int (*dma_quiescent) (struct drm_device *);
437 int (*context_dtor) (struct drm_device *dev, int context);
438 int (*set_busid)(struct drm_device *dev, struct drm_master *master);
441 * get_vblank_counter - get raw hardware vblank counter
443 * @pipe: counter to fetch
445 * Driver callback for fetching a raw hardware vblank counter for @crtc.
446 * If a device doesn't have a hardware counter, the driver can simply
447 * return the value of drm_vblank_count. The DRM core will account for
448 * missed vblank events while interrupts where disabled based on system
451 * Wraparound handling and loss of events due to modesetting is dealt
452 * with in the DRM core code.
455 * Raw vblank counter value.
457 u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
460 * enable_vblank - enable vblank interrupt events
462 * @pipe: which irq to enable
464 * Enable vblank interrupts for @crtc. If the device doesn't have
465 * a hardware vblank counter, this routine should be a no-op, since
466 * interrupts will have to stay on to keep the count accurate.
469 * Zero on success, appropriate errno if the given @crtc's vblank
470 * interrupt cannot be enabled.
472 int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
475 * disable_vblank - disable vblank interrupt events
477 * @pipe: which irq to enable
479 * Disable vblank interrupts for @crtc. If the device doesn't have
480 * a hardware vblank counter, this routine should be a no-op, since
481 * interrupts will have to stay on to keep the count accurate.
483 void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
486 * Called by \c drm_device_is_agp. Typically used to determine if a
487 * card is really attached to AGP or not.
489 * \param dev DRM device handle
492 * One of three values is returned depending on whether or not the
493 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
494 * (return of 1), or may or may not be AGP (return of 2).
496 int (*device_is_agp) (struct drm_device *dev);
499 * Called by vblank timestamping code.
501 * Return the current display scanout position from a crtc, and an
502 * optional accurate ktime_get timestamp of when position was measured.
504 * \param dev DRM device.
505 * \param pipe Id of the crtc to query.
506 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
507 * \param *vpos Target location for current vertical scanout position.
508 * \param *hpos Target location for current horizontal scanout position.
509 * \param *stime Target location for timestamp taken immediately before
510 * scanout position query. Can be NULL to skip timestamp.
511 * \param *etime Target location for timestamp taken immediately after
512 * scanout position query. Can be NULL to skip timestamp.
513 * \param mode Current display timings.
515 * Returns vpos as a positive number while in active scanout area.
516 * Returns vpos as a negative number inside vblank, counting the number
517 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
518 * until start of active scanout / end of vblank."
520 * \return Flags, or'ed together as follows:
522 * DRM_SCANOUTPOS_VALID = Query successful.
523 * DRM_SCANOUTPOS_INVBL = Inside vblank.
524 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
525 * this flag means that returned position may be offset by a constant
526 * but unknown small number of scanlines wrt. real scanout position.
529 int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
530 unsigned int flags, int *vpos, int *hpos,
531 ktime_t *stime, ktime_t *etime,
532 const struct drm_display_mode *mode);
535 * Called by \c drm_get_last_vbltimestamp. Should return a precise
536 * timestamp when the most recent VBLANK interval ended or will end.
538 * Specifically, the timestamp in @vblank_time should correspond as
539 * closely as possible to the time when the first video scanline of
540 * the video frame after the end of VBLANK will start scanning out,
541 * the time immediately after end of the VBLANK interval. If the
542 * @crtc is currently inside VBLANK, this will be a time in the future.
543 * If the @crtc is currently scanning out a frame, this will be the
544 * past start time of the current scanout. This is meant to adhere
545 * to the OpenML OML_sync_control extension specification.
547 * \param dev dev DRM device handle.
548 * \param pipe crtc for which timestamp should be returned.
549 * \param *max_error Maximum allowable timestamp error in nanoseconds.
550 * Implementation should strive to provide timestamp
551 * with an error of at most *max_error nanoseconds.
552 * Returns true upper bound on error for timestamp.
553 * \param *vblank_time Target location for returned vblank timestamp.
554 * \param flags 0 = Defaults, no special treatment needed.
555 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
556 * irq handler. Some drivers need to apply some workarounds
557 * for gpu-specific vblank irq quirks if flag is set.
560 * Zero if timestamping isn't supported in current display mode or a
561 * negative number on failure. A positive status code on success,
562 * which describes how the vblank_time timestamp was computed.
564 int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
566 struct timeval *vblank_time,
569 /* these have to be filled in */
571 irqreturn_t(*irq_handler) (int irq, void *arg);
572 void (*irq_preinstall) (struct drm_device *dev);
573 int (*irq_postinstall) (struct drm_device *dev);
574 void (*irq_uninstall) (struct drm_device *dev);
576 /* Master routines */
577 int (*master_create)(struct drm_device *dev, struct drm_master *master);
578 void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
580 * master_set is called whenever the minor master is set.
581 * master_drop is called whenever the minor master is dropped.
584 int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
586 void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
589 int (*debugfs_init)(struct drm_minor *minor);
590 void (*debugfs_cleanup)(struct drm_minor *minor);
593 * Driver-specific constructor for drm_gem_objects, to set up
594 * obj->driver_private.
596 * Returns 0 on success.
598 void (*gem_free_object) (struct drm_gem_object *obj);
599 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
600 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
603 /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
604 int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
605 uint32_t handle, uint32_t flags, int *prime_fd);
606 /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
607 int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
608 int prime_fd, uint32_t *handle);
609 /* export GEM -> dmabuf */
610 struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
611 struct drm_gem_object *obj, int flags);
612 /* import dmabuf -> GEM */
613 struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
614 struct dma_buf *dma_buf);
615 /* low-level interface used by drm_gem_prime_{import,export} */
616 int (*gem_prime_pin)(struct drm_gem_object *obj);
617 void (*gem_prime_unpin)(struct drm_gem_object *obj);
618 struct reservation_object * (*gem_prime_res_obj)(
619 struct drm_gem_object *obj);
620 struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
621 struct drm_gem_object *(*gem_prime_import_sg_table)(
622 struct drm_device *dev,
623 struct dma_buf_attachment *attach,
624 struct sg_table *sgt);
625 void *(*gem_prime_vmap)(struct drm_gem_object *obj);
626 void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
627 int (*gem_prime_mmap)(struct drm_gem_object *obj,
628 struct vm_area_struct *vma);
630 /* vga arb irq handler */
631 void (*vgaarb_irq)(struct drm_device *dev, bool state);
633 /* dumb alloc support */
634 int (*dumb_create)(struct drm_file *file_priv,
635 struct drm_device *dev,
636 struct drm_mode_create_dumb *args);
637 int (*dumb_map_offset)(struct drm_file *file_priv,
638 struct drm_device *dev, uint32_t handle,
640 int (*dumb_destroy)(struct drm_file *file_priv,
641 struct drm_device *dev,
644 /* Driver private ops for this object */
645 const struct vm_operations_struct *gem_vm_ops;
656 const struct drm_ioctl_desc *ioctls;
658 const struct file_operations *fops;
660 /* List of devices hanging off this driver with stealth attach. */
661 struct list_head legacy_dev_list;
664 enum drm_minor_type {
672 * Info file list entry. This structure represents a debugfs or proc file to
673 * be created by the drm core
675 struct drm_info_list {
676 const char *name; /** file name */
677 int (*show)(struct seq_file*, void*); /** show callback */
678 u32 driver_features; /**< Required driver features for this entry */
683 * debugfs node structure. This structure represents a debugfs file.
685 struct drm_info_node {
686 struct list_head list;
687 struct drm_minor *minor;
688 const struct drm_info_list *info_ent;
693 * DRM minor structure. This structure represents a drm minor number.
696 int index; /**< Minor device number */
697 int type; /**< Control or render */
698 struct device *kdev; /**< Linux device */
699 struct drm_device *dev;
701 struct dentry *debugfs_root;
703 struct list_head debugfs_list;
704 struct mutex debugfs_lock; /* Protects debugfs_list. */
706 /* currently active master for this node. Protected by master_mutex */
707 struct drm_master *master;
711 struct drm_pending_vblank_event {
712 struct drm_pending_event base;
714 struct drm_event_vblank event;
717 struct drm_vblank_crtc {
718 struct drm_device *dev; /* pointer to the drm_device */
719 wait_queue_head_t queue; /**< VBLANK wait queue */
720 struct timer_list disable_timer; /* delayed disable timer */
722 /* vblank counter, protected by dev->vblank_time_lock for writes */
724 /* vblank timestamps, protected by dev->vblank_time_lock for writes */
725 struct timeval time[DRM_VBLANKTIME_RBSIZE];
727 atomic_t refcount; /* number of users of vblank interruptsper crtc */
728 u32 last; /* protected by dev->vbl_lock, used */
729 /* for wraparound handling */
730 u32 last_wait; /* Last vblank seqno waited per CRTC */
731 unsigned int inmodeset; /* Display driver is setting mode */
732 unsigned int pipe; /* crtc index */
733 int framedur_ns; /* frame/field duration in ns */
734 int linedur_ns; /* line duration in ns */
735 bool enabled; /* so we don't call enable more than
740 * DRM device structure. This structure represent a complete card that
741 * may contain multiple heads.
744 struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
745 int if_version; /**< Highest interface version set */
747 /** \name Lifetime Management */
749 struct kref ref; /**< Object ref-count */
750 struct device *dev; /**< Device structure of bus-device */
751 struct drm_driver *driver; /**< DRM driver managing the device */
752 void *dev_private; /**< DRM driver private data */
753 struct drm_minor *control; /**< Control node */
754 struct drm_minor *primary; /**< Primary node */
755 struct drm_minor *render; /**< Render node */
756 atomic_t unplugged; /**< Flag whether dev is dead */
757 struct inode *anon_inode; /**< inode for private address-space */
758 char *unique; /**< unique name of the device */
763 struct mutex struct_mutex; /**< For others */
764 struct mutex master_mutex; /**< For drm_minor::master and drm_file::is_master */
767 /** \name Usage Counters */
769 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
770 spinlock_t buf_lock; /**< For drm_device::buf_use and a few other things. */
771 int buf_use; /**< Buffers in use -- cannot alloc */
772 atomic_t buf_alloc; /**< Buffer allocation in progress */
775 struct list_head filelist;
777 /** \name Memory management */
779 struct list_head maplist; /**< Linked list of regions */
780 struct drm_open_hash map_hash; /**< User token hash table for maps */
782 /** \name Context handle management */
784 struct list_head ctxlist; /**< Linked list of context handles */
785 struct mutex ctxlist_mutex; /**< For ctxlist */
789 struct list_head vmalist; /**< List of vmas (for debugging) */
793 /** \name DMA support */
795 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
798 /** \name Context support */
801 __volatile__ long context_flag; /**< Context swapping flag */
802 int last_context; /**< Last current context */
805 /** \name VBLANK IRQ support */
811 * At load time, disabling the vblank interrupt won't be allowed since
812 * old clients may not call the modeset ioctl and therefore misbehave.
813 * Once the modeset ioctl *has* been called though, we can safely
814 * disable them when unused.
816 bool vblank_disable_allowed;
819 * If true, vblank interrupt will be disabled immediately when the
820 * refcount drops to zero, as opposed to via the vblank disable
822 * This can be set to true it the hardware has a working vblank
823 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
826 bool vblank_disable_immediate;
828 /* array of size num_crtcs */
829 struct drm_vblank_crtc *vblank;
831 spinlock_t vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
834 u32 max_vblank_count; /**< size of vblank counter register */
839 struct list_head vblank_event_list;
840 spinlock_t event_lock;
844 struct drm_agp_head *agp; /**< AGP data */
846 struct pci_dev *pdev; /**< PCI device structure */
848 struct pci_controller *hose;
851 struct platform_device *platformdev; /**< Platform device struture */
852 struct virtio_device *virtdev;
854 struct drm_sg_mem *sg; /**< Scatter gather memory */
855 unsigned int num_crtcs; /**< Number of CRTCs on this device */
859 struct drm_hw_lock *lock;
862 struct drm_local_map *agp_buffer_map;
863 unsigned int agp_buffer_token;
865 struct drm_mode_config mode_config; /**< Current mode config */
867 /** \name GEM information */
869 struct mutex object_name_lock;
870 struct idr object_name_idr;
871 struct drm_vma_offset_manager *vma_offset_manager;
873 int switch_power_state;
876 #define DRM_SWITCH_POWER_ON 0
877 #define DRM_SWITCH_POWER_OFF 1
878 #define DRM_SWITCH_POWER_CHANGING 2
879 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
881 static __inline__ int drm_core_check_feature(struct drm_device *dev,
884 return ((dev->driver->driver_features & feature) ? 1 : 0);
887 static inline void drm_device_set_unplugged(struct drm_device *dev)
890 atomic_set(&dev->unplugged, 1);
893 static inline int drm_device_is_unplugged(struct drm_device *dev)
895 int ret = atomic_read(&dev->unplugged);
900 static inline bool drm_is_render_client(const struct drm_file *file_priv)
902 return file_priv->minor->type == DRM_MINOR_RENDER;
905 static inline bool drm_is_control_client(const struct drm_file *file_priv)
907 return file_priv->minor->type == DRM_MINOR_CONTROL;
910 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
912 return file_priv->minor->type == DRM_MINOR_LEGACY;
915 /******************************************************************/
916 /** \name Internal function definitions */
919 /* Driver support (drm_drv.h) */
920 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
921 extern long drm_ioctl(struct file *filp,
922 unsigned int cmd, unsigned long arg);
923 extern long drm_compat_ioctl(struct file *filp,
924 unsigned int cmd, unsigned long arg);
925 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
927 /* Device support (drm_fops.h) */
928 extern int drm_open(struct inode *inode, struct file *filp);
929 extern ssize_t drm_read(struct file *filp, char __user *buffer,
930 size_t count, loff_t *offset);
931 extern int drm_release(struct inode *inode, struct file *filp);
932 extern int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv);
934 /* Mapping support (drm_vm.h) */
935 extern unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
937 /* Misc. IOCTL support (drm_ioctl.c) */
938 int drm_noop(struct drm_device *dev, void *data,
939 struct drm_file *file_priv);
940 int drm_invalid_op(struct drm_device *dev, void *data,
941 struct drm_file *file_priv);
943 /* Cache management (drm_cache.c) */
944 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
945 void drm_clflush_sg(struct sg_table *st);
946 void drm_clflush_virt_range(void *addr, unsigned long length);
949 * These are exported to drivers so that they can implement fencing using
950 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
953 /* IRQ support (drm_irq.h) */
954 extern int drm_irq_install(struct drm_device *dev, int irq);
955 extern int drm_irq_uninstall(struct drm_device *dev);
957 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
958 extern int drm_wait_vblank(struct drm_device *dev, void *data,
959 struct drm_file *filp);
960 extern u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe);
961 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
962 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
963 struct timeval *vblanktime);
964 extern u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
965 struct timeval *vblanktime);
966 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
967 struct drm_pending_vblank_event *e);
968 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
969 struct drm_pending_vblank_event *e);
970 extern void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
971 struct drm_pending_vblank_event *e);
972 extern void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
973 struct drm_pending_vblank_event *e);
974 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
975 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
976 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
977 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
978 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
979 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
980 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
981 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
982 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
983 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
984 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
985 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
986 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
987 extern void drm_vblank_cleanup(struct drm_device *dev);
988 extern u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe);
990 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
991 unsigned int pipe, int *max_error,
992 struct timeval *vblank_time,
994 const struct drm_display_mode *mode);
995 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
996 const struct drm_display_mode *mode);
999 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1000 * @crtc: which CRTC's vblank waitqueue to retrieve
1002 * This function returns a pointer to the vblank waitqueue for the CRTC.
1003 * Drivers can use this to implement vblank waits using wait_event() & co.
1005 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1007 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1010 /* Modesetting support */
1011 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1012 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1014 /* Stub support (drm_stub.h) */
1015 extern struct drm_master *drm_master_get(struct drm_master *master);
1016 extern void drm_master_put(struct drm_master **master);
1018 extern void drm_put_dev(struct drm_device *dev);
1019 extern void drm_unplug_dev(struct drm_device *dev);
1020 extern unsigned int drm_debug;
1021 extern bool drm_atomic;
1023 /* Debugfs support */
1024 #if defined(CONFIG_DEBUG_FS)
1025 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1026 int count, struct dentry *root,
1027 struct drm_minor *minor);
1028 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1029 int count, struct drm_minor *minor);
1031 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1032 int count, struct dentry *root,
1033 struct drm_minor *minor)
1038 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1039 int count, struct drm_minor *minor)
1045 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1046 struct drm_gem_object *obj,
1048 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1049 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1051 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1052 struct dma_buf *dma_buf);
1053 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1054 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1055 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1057 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1058 dma_addr_t *addrs, int max_pages);
1059 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages);
1060 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1063 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1065 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1067 /* sysfs support (drm_sysfs.c) */
1068 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1071 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1072 struct device *parent);
1073 void drm_dev_ref(struct drm_device *dev);
1074 void drm_dev_unref(struct drm_device *dev);
1075 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1076 void drm_dev_unregister(struct drm_device *dev);
1077 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1079 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1080 void drm_minor_release(struct drm_minor *minor);
1085 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1087 if (dev->driver->device_is_agp != NULL) {
1088 int err = (*dev->driver->device_is_agp) (dev);
1095 return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1097 void drm_pci_agp_destroy(struct drm_device *dev);
1099 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1100 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1102 extern int drm_get_pci_dev(struct pci_dev *pdev,
1103 const struct pci_device_id *ent,
1104 struct drm_driver *driver);
1105 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1107 static inline int drm_get_pci_dev(struct pci_dev *pdev,
1108 const struct pci_device_id *ent,
1109 struct drm_driver *driver)
1114 static inline int drm_pci_set_busid(struct drm_device *dev,
1115 struct drm_master *master)
1121 #define DRM_PCIE_SPEED_25 1
1122 #define DRM_PCIE_SPEED_50 2
1123 #define DRM_PCIE_SPEED_80 4
1125 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1127 /* platform section */
1128 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1129 extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
1131 /* returns true if currently okay to sleep */
1132 static __inline__ bool drm_can_sleep(void)
1134 if (in_atomic() || in_dbg_master() || irqs_disabled())