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 void 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 Macros to make printk easier */
152 * \param fmt printf() like format string.
153 * \param arg arguments
155 #define DRM_ERROR(fmt, ...) \
156 drm_err(__func__, fmt, ##__VA_ARGS__)
159 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
161 * \param fmt printf() like format string.
162 * \param arg arguments
164 #define DRM_ERROR_RATELIMITED(fmt, ...) \
166 static DEFINE_RATELIMIT_STATE(_rs, \
167 DEFAULT_RATELIMIT_INTERVAL, \
168 DEFAULT_RATELIMIT_BURST); \
170 if (__ratelimit(&_rs)) \
171 drm_err(__func__, fmt, ##__VA_ARGS__); \
174 #define DRM_INFO(fmt, ...) \
175 printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
177 #define DRM_INFO_ONCE(fmt, ...) \
178 printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
183 * \param fmt printf() like format string.
184 * \param arg arguments
186 #define DRM_DEBUG(fmt, args...) \
188 if (unlikely(drm_debug & DRM_UT_CORE)) \
189 drm_ut_debug_printk(__func__, fmt, ##args); \
192 #define DRM_DEBUG_DRIVER(fmt, args...) \
194 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
195 drm_ut_debug_printk(__func__, fmt, ##args); \
197 #define DRM_DEBUG_KMS(fmt, args...) \
199 if (unlikely(drm_debug & DRM_UT_KMS)) \
200 drm_ut_debug_printk(__func__, fmt, ##args); \
202 #define DRM_DEBUG_PRIME(fmt, args...) \
204 if (unlikely(drm_debug & DRM_UT_PRIME)) \
205 drm_ut_debug_printk(__func__, fmt, ##args); \
210 /***********************************************************************/
211 /** \name Internal types and structures */
214 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
217 * Ioctl function type.
219 * \param inode device inode.
220 * \param file_priv DRM file private pointer.
221 * \param cmd command.
222 * \param arg argument.
224 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
225 struct drm_file *file_priv);
227 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
230 #define DRM_IOCTL_NR(n) _IOC_NR(n)
231 #define DRM_MAJOR 226
234 #define DRM_MASTER 0x2
235 #define DRM_ROOT_ONLY 0x4
236 #define DRM_CONTROL_ALLOW 0x8
237 #define DRM_UNLOCKED 0x10
238 #define DRM_RENDER_ALLOW 0x20
240 struct drm_ioctl_desc {
244 unsigned int cmd_drv;
249 * Creates a driver or general drm_ioctl_desc array entry for the given
250 * ioctl, for use by drm_ioctl().
253 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
254 [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl, .name = #ioctl}
256 /* Event queued up for userspace to read */
257 struct drm_pending_event {
258 struct drm_event *event;
259 struct list_head link;
260 struct drm_file *file_priv;
261 pid_t pid; /* pid of requester, no guarantee it's valid by the time
262 we deliver the event, for tracing only */
263 void (*destroy)(struct drm_pending_event *event);
266 /* initial implementaton using a linked list - todo hashtab */
267 struct drm_prime_file_private {
268 struct list_head head;
272 /** File private data */
274 unsigned authenticated :1;
275 /* Whether we're master for a minor. Protected by master_mutex */
276 unsigned is_master :1;
277 /* true when the client has asked us to expose stereo 3D mode flags */
278 unsigned stereo_allowed :1;
280 * true if client understands CRTC primary planes and cursor planes
283 unsigned universal_planes:1;
288 struct list_head lhead;
289 struct drm_minor *minor;
290 unsigned long lock_count;
292 /** Mapping of mm object handles to object pointers. */
293 struct idr object_idr;
294 /** Lock for synchronization of access to object_idr. */
295 spinlock_t table_lock;
300 struct drm_master *master; /* master this node is currently associated with
301 N.B. not always minor->master */
303 * fbs - List of framebuffers associated with this file.
305 * Protected by fbs_lock. Note that the fbs list holds a reference on
306 * the fb object to prevent it from untimely disappearing.
308 struct list_head fbs;
309 struct mutex fbs_lock;
311 wait_queue_head_t event_wait;
312 struct list_head event_list;
315 struct drm_prime_file_private prime;
321 struct drm_lock_data {
322 struct drm_hw_lock *hw_lock; /**< Hardware lock */
323 /** Private of lock holder's file (NULL=kernel) */
324 struct drm_file *file_priv;
325 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
326 unsigned long lock_time; /**< Time of last lock in jiffies */
328 uint32_t kernel_waiters;
329 uint32_t user_waiters;
334 * This structure defines the drm_mm memory object, which will be used by the
335 * DRM for its buffer objects.
337 struct drm_gem_object {
338 /** Reference count of this object */
339 struct kref refcount;
342 * handle_count - gem file_priv handle count of this object
344 * Each handle also holds a reference. Note that when the handle_count
345 * drops to 0 any global names (e.g. the id in the flink namespace) will
348 * Protected by dev->object_name_lock.
350 unsigned handle_count;
352 /** Related drm device */
353 struct drm_device *dev;
355 /** File representing the shmem storage */
358 /* Mapping info for this object */
359 struct drm_vma_offset_node vma_node;
362 * Size of the object, in bytes. Immutable over the object's
368 * Global name for this object, starts at 1. 0 means unnamed.
369 * Access is covered by the object_name_lock in the related drm_device
374 * Memory domains. These monitor which caches contain read/write data
375 * related to the object. When transitioning from one set of domains
376 * to another, the driver is called to ensure that caches are suitably
377 * flushed and invalidated
379 uint32_t read_domains;
380 uint32_t write_domain;
383 * While validating an exec operation, the
384 * new read/write domain values are computed here.
385 * They will be transferred to the above values
386 * at the point that any cache flushing occurs
388 uint32_t pending_read_domains;
389 uint32_t pending_write_domain;
392 * dma_buf - dma buf associated with this GEM object
394 * Pointer to the dma-buf associated with this gem object (either
395 * through importing or exporting). We break the resulting reference
396 * loop when the last gem handle for this object is released.
398 * Protected by obj->object_name_lock
400 struct dma_buf *dma_buf;
403 * import_attach - dma buf attachment backing this object
405 * Any foreign dma_buf imported as a gem object has this set to the
406 * attachment point for the device. This is invariant over the lifetime
409 * The driver's ->gem_free_object callback is responsible for cleaning
410 * up the dma_buf attachment and references acquired at import time.
412 * Note that the drm gem/prime core does not depend upon drivers setting
413 * this field any more. So for drivers where this doesn't make sense
414 * (e.g. virtual devices or a displaylink behind an usb bus) they can
415 * simply leave it as NULL.
417 struct dma_buf_attachment *import_attach;
421 * struct drm_master - drm master structure
423 * @refcount: Refcount for this master object.
424 * @minor: Link back to minor char device we are master for. Immutable.
425 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
426 * @unique_len: Length of unique field. Protected by drm_global_mutex.
427 * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
428 * @magicfree: List of used authentication tokens. Protected by struct_mutex.
429 * @lock: DRI lock information.
430 * @driver_priv: Pointer to driver-private information.
433 struct kref refcount;
434 struct drm_minor *minor;
437 struct drm_open_hash magiclist;
438 struct list_head magicfree;
439 struct drm_lock_data lock;
443 /* Size of ringbuffer for vblank timestamps. Just double-buffer
444 * in initial implementation.
446 #define DRM_VBLANKTIME_RBSIZE 2
448 /* Flags and return codes for get_vblank_timestamp() driver function. */
449 #define DRM_CALLED_FROM_VBLIRQ 1
450 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
451 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
453 /* get_scanout_position() return flags */
454 #define DRM_SCANOUTPOS_VALID (1 << 0)
455 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
456 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
459 * DRM driver structure. This structure represent the common code for
460 * a family of cards. There will one drm_device for each card present
464 int (*load) (struct drm_device *, unsigned long flags);
465 int (*firstopen) (struct drm_device *);
466 int (*open) (struct drm_device *, struct drm_file *);
467 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
468 void (*postclose) (struct drm_device *, struct drm_file *);
469 void (*lastclose) (struct drm_device *);
470 int (*unload) (struct drm_device *);
471 int (*suspend) (struct drm_device *, pm_message_t state);
472 int (*resume) (struct drm_device *);
473 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
474 int (*dma_quiescent) (struct drm_device *);
475 int (*context_dtor) (struct drm_device *dev, int context);
476 int (*set_busid)(struct drm_device *dev, struct drm_master *master);
479 * get_vblank_counter - get raw hardware vblank counter
481 * @crtc: counter to fetch
483 * Driver callback for fetching a raw hardware vblank counter for @crtc.
484 * If a device doesn't have a hardware counter, the driver can simply
485 * return the value of drm_vblank_count. The DRM core will account for
486 * missed vblank events while interrupts where disabled based on system
489 * Wraparound handling and loss of events due to modesetting is dealt
490 * with in the DRM core code.
493 * Raw vblank counter value.
495 u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
498 * enable_vblank - enable vblank interrupt events
500 * @crtc: which irq to enable
502 * Enable vblank interrupts for @crtc. If the device doesn't have
503 * a hardware vblank counter, this routine should be a no-op, since
504 * interrupts will have to stay on to keep the count accurate.
507 * Zero on success, appropriate errno if the given @crtc's vblank
508 * interrupt cannot be enabled.
510 int (*enable_vblank) (struct drm_device *dev, int crtc);
513 * disable_vblank - disable vblank interrupt events
515 * @crtc: which irq to enable
517 * Disable vblank interrupts for @crtc. If the device doesn't have
518 * a hardware vblank counter, this routine should be a no-op, since
519 * interrupts will have to stay on to keep the count accurate.
521 void (*disable_vblank) (struct drm_device *dev, int crtc);
524 * Called by \c drm_device_is_agp. Typically used to determine if a
525 * card is really attached to AGP or not.
527 * \param dev DRM device handle
530 * One of three values is returned depending on whether or not the
531 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
532 * (return of 1), or may or may not be AGP (return of 2).
534 int (*device_is_agp) (struct drm_device *dev);
537 * Called by vblank timestamping code.
539 * Return the current display scanout position from a crtc, and an
540 * optional accurate ktime_get timestamp of when position was measured.
542 * \param dev DRM device.
543 * \param crtc Id of the crtc to query.
544 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
545 * \param *vpos Target location for current vertical scanout position.
546 * \param *hpos Target location for current horizontal scanout position.
547 * \param *stime Target location for timestamp taken immediately before
548 * scanout position query. Can be NULL to skip timestamp.
549 * \param *etime Target location for timestamp taken immediately after
550 * scanout position query. Can be NULL to skip timestamp.
552 * Returns vpos as a positive number while in active scanout area.
553 * Returns vpos as a negative number inside vblank, counting the number
554 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
555 * until start of active scanout / end of vblank."
557 * \return Flags, or'ed together as follows:
559 * DRM_SCANOUTPOS_VALID = Query successful.
560 * DRM_SCANOUTPOS_INVBL = Inside vblank.
561 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
562 * this flag means that returned position may be offset by a constant
563 * but unknown small number of scanlines wrt. real scanout position.
566 int (*get_scanout_position) (struct drm_device *dev, int crtc,
568 int *vpos, int *hpos, ktime_t *stime,
572 * Called by \c drm_get_last_vbltimestamp. Should return a precise
573 * timestamp when the most recent VBLANK interval ended or will end.
575 * Specifically, the timestamp in @vblank_time should correspond as
576 * closely as possible to the time when the first video scanline of
577 * the video frame after the end of VBLANK will start scanning out,
578 * the time immediately after end of the VBLANK interval. If the
579 * @crtc is currently inside VBLANK, this will be a time in the future.
580 * If the @crtc is currently scanning out a frame, this will be the
581 * past start time of the current scanout. This is meant to adhere
582 * to the OpenML OML_sync_control extension specification.
584 * \param dev dev DRM device handle.
585 * \param crtc crtc for which timestamp should be returned.
586 * \param *max_error Maximum allowable timestamp error in nanoseconds.
587 * Implementation should strive to provide timestamp
588 * with an error of at most *max_error nanoseconds.
589 * Returns true upper bound on error for timestamp.
590 * \param *vblank_time Target location for returned vblank timestamp.
591 * \param flags 0 = Defaults, no special treatment needed.
592 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
593 * irq handler. Some drivers need to apply some workarounds
594 * for gpu-specific vblank irq quirks if flag is set.
597 * Zero if timestamping isn't supported in current display mode or a
598 * negative number on failure. A positive status code on success,
599 * which describes how the vblank_time timestamp was computed.
601 int (*get_vblank_timestamp) (struct drm_device *dev, int crtc,
603 struct timeval *vblank_time,
606 /* these have to be filled in */
608 irqreturn_t(*irq_handler) (int irq, void *arg);
609 void (*irq_preinstall) (struct drm_device *dev);
610 int (*irq_postinstall) (struct drm_device *dev);
611 void (*irq_uninstall) (struct drm_device *dev);
613 /* Master routines */
614 int (*master_create)(struct drm_device *dev, struct drm_master *master);
615 void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
617 * master_set is called whenever the minor master is set.
618 * master_drop is called whenever the minor master is dropped.
621 int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
623 void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
626 int (*debugfs_init)(struct drm_minor *minor);
627 void (*debugfs_cleanup)(struct drm_minor *minor);
630 * Driver-specific constructor for drm_gem_objects, to set up
631 * obj->driver_private.
633 * Returns 0 on success.
635 void (*gem_free_object) (struct drm_gem_object *obj);
636 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
637 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
640 /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
641 int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
642 uint32_t handle, uint32_t flags, int *prime_fd);
643 /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
644 int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
645 int prime_fd, uint32_t *handle);
646 /* export GEM -> dmabuf */
647 struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
648 struct drm_gem_object *obj, int flags);
649 /* import dmabuf -> GEM */
650 struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
651 struct dma_buf *dma_buf);
652 /* low-level interface used by drm_gem_prime_{import,export} */
653 int (*gem_prime_pin)(struct drm_gem_object *obj);
654 void (*gem_prime_unpin)(struct drm_gem_object *obj);
655 struct reservation_object * (*gem_prime_res_obj)(
656 struct drm_gem_object *obj);
657 struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
658 struct drm_gem_object *(*gem_prime_import_sg_table)(
659 struct drm_device *dev, size_t size,
660 struct sg_table *sgt);
661 void *(*gem_prime_vmap)(struct drm_gem_object *obj);
662 void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
663 int (*gem_prime_mmap)(struct drm_gem_object *obj,
664 struct vm_area_struct *vma);
666 /* vga arb irq handler */
667 void (*vgaarb_irq)(struct drm_device *dev, bool state);
669 /* dumb alloc support */
670 int (*dumb_create)(struct drm_file *file_priv,
671 struct drm_device *dev,
672 struct drm_mode_create_dumb *args);
673 int (*dumb_map_offset)(struct drm_file *file_priv,
674 struct drm_device *dev, uint32_t handle,
676 int (*dumb_destroy)(struct drm_file *file_priv,
677 struct drm_device *dev,
680 /* Driver private ops for this object */
681 const struct vm_operations_struct *gem_vm_ops;
692 const struct drm_ioctl_desc *ioctls;
694 const struct file_operations *fops;
696 /* List of devices hanging off this driver with stealth attach. */
697 struct list_head legacy_dev_list;
700 enum drm_minor_type {
708 * Info file list entry. This structure represents a debugfs or proc file to
709 * be created by the drm core
711 struct drm_info_list {
712 const char *name; /** file name */
713 int (*show)(struct seq_file*, void*); /** show callback */
714 u32 driver_features; /**< Required driver features for this entry */
719 * debugfs node structure. This structure represents a debugfs file.
721 struct drm_info_node {
722 struct list_head list;
723 struct drm_minor *minor;
724 const struct drm_info_list *info_ent;
729 * DRM minor structure. This structure represents a drm minor number.
732 int index; /**< Minor device number */
733 int type; /**< Control or render */
734 struct device *kdev; /**< Linux device */
735 struct drm_device *dev;
737 struct dentry *debugfs_root;
739 struct list_head debugfs_list;
740 struct mutex debugfs_lock; /* Protects debugfs_list. */
742 /* currently active master for this node. Protected by master_mutex */
743 struct drm_master *master;
744 struct drm_mode_group mode_group;
748 struct drm_pending_vblank_event {
749 struct drm_pending_event base;
751 struct drm_event_vblank event;
754 struct drm_vblank_crtc {
755 struct drm_device *dev; /* pointer to the drm_device */
756 wait_queue_head_t queue; /**< VBLANK wait queue */
757 struct timeval time[DRM_VBLANKTIME_RBSIZE]; /**< timestamp of current count */
758 struct timer_list disable_timer; /* delayed disable timer */
759 atomic_t count; /**< number of VBLANK interrupts */
760 atomic_t refcount; /* number of users of vblank interruptsper crtc */
761 u32 last; /* protected by dev->vbl_lock, used */
762 /* for wraparound handling */
763 u32 last_wait; /* Last vblank seqno waited per CRTC */
764 unsigned int inmodeset; /* Display driver is setting mode */
765 int crtc; /* crtc index */
766 bool enabled; /* so we don't call enable more than
771 * DRM device structure. This structure represent a complete card that
772 * may contain multiple heads.
775 struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
776 int if_version; /**< Highest interface version set */
778 /** \name Lifetime Management */
780 struct kref ref; /**< Object ref-count */
781 struct device *dev; /**< Device structure of bus-device */
782 struct drm_driver *driver; /**< DRM driver managing the device */
783 void *dev_private; /**< DRM driver private data */
784 struct drm_minor *control; /**< Control node */
785 struct drm_minor *primary; /**< Primary node */
786 struct drm_minor *render; /**< Render node */
787 atomic_t unplugged; /**< Flag whether dev is dead */
788 struct inode *anon_inode; /**< inode for private address-space */
789 char *unique; /**< unique name of the device */
794 struct mutex struct_mutex; /**< For others */
795 struct mutex master_mutex; /**< For drm_minor::master and drm_file::is_master */
798 /** \name Usage Counters */
800 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
801 spinlock_t buf_lock; /**< For drm_device::buf_use and a few other things. */
802 int buf_use; /**< Buffers in use -- cannot alloc */
803 atomic_t buf_alloc; /**< Buffer allocation in progress */
806 struct list_head filelist;
808 /** \name Memory management */
810 struct list_head maplist; /**< Linked list of regions */
811 struct drm_open_hash map_hash; /**< User token hash table for maps */
813 /** \name Context handle management */
815 struct list_head ctxlist; /**< Linked list of context handles */
816 struct mutex ctxlist_mutex; /**< For ctxlist */
820 struct list_head vmalist; /**< List of vmas (for debugging) */
824 /** \name DMA support */
826 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
829 /** \name Context support */
831 bool irq_enabled; /**< True if irq handler is enabled */
834 __volatile__ long context_flag; /**< Context swapping flag */
835 int last_context; /**< Last current context */
838 /** \name VBLANK IRQ support */
842 * At load time, disabling the vblank interrupt won't be allowed since
843 * old clients may not call the modeset ioctl and therefore misbehave.
844 * Once the modeset ioctl *has* been called though, we can safely
845 * disable them when unused.
847 bool vblank_disable_allowed;
850 * If true, vblank interrupt will be disabled immediately when the
851 * refcount drops to zero, as opposed to via the vblank disable
853 * This can be set to true it the hardware has a working vblank
854 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
857 bool vblank_disable_immediate;
859 /* array of size num_crtcs */
860 struct drm_vblank_crtc *vblank;
862 spinlock_t vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
865 u32 max_vblank_count; /**< size of vblank counter register */
870 struct list_head vblank_event_list;
871 spinlock_t event_lock;
875 struct drm_agp_head *agp; /**< AGP data */
877 struct pci_dev *pdev; /**< PCI device structure */
879 struct pci_controller *hose;
882 struct platform_device *platformdev; /**< Platform device struture */
884 struct drm_sg_mem *sg; /**< Scatter gather memory */
885 unsigned int num_crtcs; /**< Number of CRTCs on this device */
890 struct drm_hw_lock *lock;
893 struct drm_local_map *agp_buffer_map;
894 unsigned int agp_buffer_token;
896 struct drm_mode_config mode_config; /**< Current mode config */
898 /** \name GEM information */
900 struct mutex object_name_lock;
901 struct idr object_name_idr;
902 struct drm_vma_offset_manager *vma_offset_manager;
904 int switch_power_state;
907 #define DRM_SWITCH_POWER_ON 0
908 #define DRM_SWITCH_POWER_OFF 1
909 #define DRM_SWITCH_POWER_CHANGING 2
910 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
912 static __inline__ int drm_core_check_feature(struct drm_device *dev,
915 return ((dev->driver->driver_features & feature) ? 1 : 0);
918 static inline void drm_device_set_unplugged(struct drm_device *dev)
921 atomic_set(&dev->unplugged, 1);
924 static inline int drm_device_is_unplugged(struct drm_device *dev)
926 int ret = atomic_read(&dev->unplugged);
931 static inline bool drm_is_render_client(const struct drm_file *file_priv)
933 return file_priv->minor->type == DRM_MINOR_RENDER;
936 static inline bool drm_is_control_client(const struct drm_file *file_priv)
938 return file_priv->minor->type == DRM_MINOR_CONTROL;
941 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
943 return file_priv->minor->type == DRM_MINOR_LEGACY;
946 /******************************************************************/
947 /** \name Internal function definitions */
950 /* Driver support (drm_drv.h) */
951 extern long drm_ioctl(struct file *filp,
952 unsigned int cmd, unsigned long arg);
953 extern long drm_compat_ioctl(struct file *filp,
954 unsigned int cmd, unsigned long arg);
955 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
957 /* Device support (drm_fops.h) */
958 extern int drm_open(struct inode *inode, struct file *filp);
959 extern ssize_t drm_read(struct file *filp, char __user *buffer,
960 size_t count, loff_t *offset);
961 extern int drm_release(struct inode *inode, struct file *filp);
963 /* Mapping support (drm_vm.h) */
964 extern void drm_vm_open_locked(struct drm_device *dev, struct vm_area_struct *vma);
965 extern unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
967 /* Misc. IOCTL support (drm_ioctl.c) */
968 int drm_noop(struct drm_device *dev, void *data,
969 struct drm_file *file_priv);
971 /* Cache management (drm_cache.c) */
972 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
973 void drm_clflush_sg(struct sg_table *st);
974 void drm_clflush_virt_range(void *addr, unsigned long length);
977 * These are exported to drivers so that they can implement fencing using
978 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
981 /* IRQ support (drm_irq.h) */
982 extern int drm_irq_install(struct drm_device *dev, int irq);
983 extern int drm_irq_uninstall(struct drm_device *dev);
985 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
986 extern int drm_wait_vblank(struct drm_device *dev, void *data,
987 struct drm_file *filp);
988 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
989 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
990 struct timeval *vblanktime);
991 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
992 struct drm_pending_vblank_event *e);
993 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
994 extern int drm_vblank_get(struct drm_device *dev, int crtc);
995 extern void drm_vblank_put(struct drm_device *dev, int crtc);
996 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
997 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
998 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
999 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1000 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1001 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1002 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1003 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1004 extern void drm_vblank_cleanup(struct drm_device *dev);
1006 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1007 int crtc, int *max_error,
1008 struct timeval *vblank_time,
1010 const struct drm_crtc *refcrtc,
1011 const struct drm_display_mode *mode);
1012 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1013 const struct drm_display_mode *mode);
1016 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1017 * @crtc: which CRTC's vblank waitqueue to retrieve
1019 * This function returns a pointer to the vblank waitqueue for the CRTC.
1020 * Drivers can use this to implement vblank waits using wait_event() & co.
1022 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1024 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1027 /* Modesetting support */
1028 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1029 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1030 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1031 struct drm_file *file_priv);
1033 /* Stub support (drm_stub.h) */
1034 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1035 struct drm_file *file_priv);
1036 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1037 struct drm_file *file_priv);
1038 struct drm_master *drm_master_create(struct drm_minor *minor);
1039 extern struct drm_master *drm_master_get(struct drm_master *master);
1040 extern void drm_master_put(struct drm_master **master);
1042 extern void drm_put_dev(struct drm_device *dev);
1043 extern void drm_unplug_dev(struct drm_device *dev);
1044 extern unsigned int drm_debug;
1046 /* Debugfs support */
1047 #if defined(CONFIG_DEBUG_FS)
1048 extern int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1049 struct dentry *root);
1050 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1051 int count, struct dentry *root,
1052 struct drm_minor *minor);
1053 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1054 int count, struct drm_minor *minor);
1055 extern int drm_debugfs_cleanup(struct drm_minor *minor);
1056 extern int drm_debugfs_connector_add(struct drm_connector *connector);
1057 extern void drm_debugfs_connector_remove(struct drm_connector *connector);
1059 static inline int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1060 struct dentry *root)
1065 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1066 int count, struct dentry *root,
1067 struct drm_minor *minor)
1072 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1073 int count, struct drm_minor *minor)
1078 static inline int drm_debugfs_cleanup(struct drm_minor *minor)
1083 static inline int drm_debugfs_connector_add(struct drm_connector *connector)
1087 static inline void drm_debugfs_connector_remove(struct drm_connector *connector)
1093 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1094 struct drm_gem_object *obj, int flags);
1095 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1096 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1098 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1099 struct dma_buf *dma_buf);
1100 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1101 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1102 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1104 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1105 dma_addr_t *addrs, int max_pages);
1106 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages);
1107 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1109 int drm_gem_dumb_destroy(struct drm_file *file,
1110 struct drm_device *dev,
1114 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1116 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1118 /* sysfs support (drm_sysfs.c) */
1119 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1121 /* Graphics Execution Manager library functions (drm_gem.c) */
1122 void drm_gem_object_release(struct drm_gem_object *obj);
1123 void drm_gem_object_free(struct kref *kref);
1124 int drm_gem_object_init(struct drm_device *dev,
1125 struct drm_gem_object *obj, size_t size);
1126 void drm_gem_private_object_init(struct drm_device *dev,
1127 struct drm_gem_object *obj, size_t size);
1128 void drm_gem_vm_open(struct vm_area_struct *vma);
1129 void drm_gem_vm_close(struct vm_area_struct *vma);
1130 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
1131 struct vm_area_struct *vma);
1132 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
1135 drm_gem_object_reference(struct drm_gem_object *obj)
1137 kref_get(&obj->refcount);
1141 drm_gem_object_unreference(struct drm_gem_object *obj)
1144 kref_put(&obj->refcount, drm_gem_object_free);
1148 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1150 if (obj && !atomic_add_unless(&obj->refcount.refcount, -1, 1)) {
1151 struct drm_device *dev = obj->dev;
1153 mutex_lock(&dev->struct_mutex);
1154 if (likely(atomic_dec_and_test(&obj->refcount.refcount)))
1155 drm_gem_object_free(&obj->refcount);
1156 mutex_unlock(&dev->struct_mutex);
1160 int drm_gem_handle_create(struct drm_file *file_priv,
1161 struct drm_gem_object *obj,
1163 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
1166 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1167 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1168 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
1170 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
1171 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
1172 bool dirty, bool accessed);
1174 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1175 struct drm_file *filp,
1178 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1179 struct device *parent);
1180 void drm_dev_ref(struct drm_device *dev);
1181 void drm_dev_unref(struct drm_device *dev);
1182 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1183 void drm_dev_unregister(struct drm_device *dev);
1184 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1186 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1187 void drm_minor_release(struct drm_minor *minor);
1192 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1194 if (dev->driver->device_is_agp != NULL) {
1195 int err = (*dev->driver->device_is_agp) (dev);
1202 return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1204 void drm_pci_agp_destroy(struct drm_device *dev);
1206 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1207 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1208 extern int drm_get_pci_dev(struct pci_dev *pdev,
1209 const struct pci_device_id *ent,
1210 struct drm_driver *driver);
1211 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1213 #define DRM_PCIE_SPEED_25 1
1214 #define DRM_PCIE_SPEED_50 2
1215 #define DRM_PCIE_SPEED_80 4
1217 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1219 /* platform section */
1220 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1221 extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
1223 /* returns true if currently okay to sleep */
1224 static __inline__ bool drm_can_sleep(void)
1226 if (in_atomic() || in_dbg_master() || irqs_disabled())