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 #if defined(_KERNEL) || defined(__KERNEL__)
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/sglist.h>
50 #include <sys/spinlock.h>
51 #include <sys/spinlock2.h>
52 #include <sys/fcntl.h>
54 #include <sys/filio.h>
55 #include <sys/sysctl.h>
57 #include <sys/queue.h>
58 #include <sys/signalvar.h>
60 #include <linux/highmem.h>
62 #include <sys/taskqueue.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page2.h>
71 #include <vm/vm_pager.h>
72 #include <vm/vm_param.h>
73 #include <machine/param.h>
74 #include <machine/pmap.h>
76 #include <machine/specialreg.h>
78 #include <machine/sysarch.h>
79 #include <sys/endian.h>
82 #include <sys/memrange.h>
83 #include <sys/mutex.h>
85 #include <uapi_drm/drm.h>
86 #include <uapi_drm/drm_sarea.h>
88 #include <linux/atomic.h>
89 #include <linux/bug.h>
90 #include <linux/dma-mapping.h>
91 #include <linux/capability.h>
92 #include <linux/err.h>
93 #include <linux/idr.h>
94 #include <linux/pci.h>
95 #include <linux/jiffies.h>
96 #include <linux/kernel.h>
98 #include <linux/kref.h>
99 #include <linux/list.h>
100 #include <linux/mm.h>
101 #include <linux/moduleparam.h>
102 #include <linux/mutex.h>
103 #include <linux/slab.h>
104 #include <linux/scatterlist.h>
105 #include <linux/timer.h>
107 #include <linux/seq_file.h>
108 #include <linux/types.h>
109 #include <linux/vmalloc.h>
110 #include <linux/wait.h>
111 #include <linux/workqueue.h>
113 #include <asm/uaccess.h>
115 #include <drm/drm_agpsupport.h>
116 #include <drm/drm_global.h>
118 #include <drm/drm_vma_manager.h>
120 #include <drm/drm_os_linux.h>
121 #include <drm/drm_hashtab.h>
122 #include <drm/drm_mm.h>
127 struct drm_local_map;
128 struct drm_device_dma;
129 struct drm_dma_handle;
130 struct drm_gem_object;
134 struct reservation_object;
135 struct dma_buf_attachment;
138 * 4 debug categories are defined:
140 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
141 * This is the category used by the DRM_DEBUG() macro.
143 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
144 * This is the category used by the DRM_DEBUG_DRIVER() macro.
146 * KMS: used in the modesetting code.
147 * This is the category used by the DRM_DEBUG_KMS() macro.
149 * PRIME: used in the prime code.
150 * This is the category used by the DRM_DEBUG_PRIME() macro.
152 * ATOMIC: used in the atomic code.
153 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
155 * Enabling verbose debug messages is done through the drm.debug parameter,
156 * each category being enabled by a bit.
158 * drm.debug=0x1 will enable CORE messages
159 * drm.debug=0x2 will enable DRIVER messages
160 * drm.debug=0x3 will enable CORE and DRIVER messages
162 * drm.debug=0xf will enable all messages
164 * An interesting feature is that it's possible to enable verbose logging at
165 * run-time by echoing the debug value in its sysfs node:
166 * # echo 0xf > /sys/module/drm/parameters/debug
168 #define DRM_UT_CORE 0x01
169 #define DRM_UT_DRIVER 0x02
170 #define DRM_UT_KMS 0x04
171 #define DRM_UT_PRIME 0x08
172 #define DRM_UT_ATOMIC 0x10
174 void drm_ut_debug_printk(const char *function_name,
175 const char *format, ...);
177 void drm_err(const char *format, ...);
179 /***********************************************************************/
180 /** \name DRM template customization defaults */
183 /* driver capabilities and requirements mask */
184 #define DRIVER_USE_AGP 0x1
185 #define DRIVER_REQUIRE_AGP 0x2
186 #define DRIVER_PCI_DMA 0x8
187 #define DRIVER_SG 0x10
188 #define DRIVER_HAVE_DMA 0x20
189 #define DRIVER_HAVE_IRQ 0x40
190 #define DRIVER_IRQ_SHARED 0x80
191 #define DRIVER_GEM 0x1000
192 #define DRIVER_MODESET 0x2000
193 #define DRIVER_PRIME 0x4000
194 #define DRIVER_RENDER 0x8000
195 #define DRIVER_ATOMIC 0x10000
196 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
198 #define DRM_MAGIC_HASH_ORDER 4 /**< Size of key hash table. Must be power of 2. */
200 /***********************************************************************/
201 /** \name Macros to make printk easier */
207 * \param fmt printf() like format string.
208 * \param arg arguments
210 #define DRM_ERROR(fmt, ...) \
211 kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt, \
212 DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
215 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
217 * \param fmt printf() like format string.
218 * \param arg arguments
221 #define DRM_INFO(fmt, ...) kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
223 #define DRM_INFO_ONCE(fmt, ...) \
224 printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
229 * \param fmt printf() like format string.
230 * \param arg arguments
233 #define DRM_DEBUGBITS_DEBUG 0x1
234 #define DRM_DEBUGBITS_KMS 0x2
235 #define DRM_DEBUGBITS_FAILED_IOCTL 0x4
236 #define DRM_DEBUGBITS_VERBOSE 0x8
238 #define DRM_DEBUG(fmt, ...) do { \
239 if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0) \
240 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
241 __func__ , ##__VA_ARGS__); \
244 #define DRM_DEBUG_VERBOSE(fmt, ...) do { \
245 if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0) \
246 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
247 __func__ , ##__VA_ARGS__); \
250 #define DRM_DEBUG_KMS(fmt, ...) do { \
251 if ((drm_debug & DRM_DEBUGBITS_KMS) != 0) \
252 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
253 __func__ , ##__VA_ARGS__); \
256 #define DRM_DEBUG_DRIVER(fmt, ...) do { \
257 if ((drm_debug & DRM_DEBUGBITS_KMS) != 0) \
258 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
259 __func__ , ##__VA_ARGS__); \
262 #define DRM_DEBUG_ATOMIC(fmt, args...) \
264 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
265 drm_ut_debug_printk(__func__, fmt, ##args); \
270 /***********************************************************************/
271 /** \name Internal types and structures */
274 #define DRM_GEM_MAPPING_MASK (3ULL << 62)
275 #define DRM_GEM_MAPPING_KEY (2ULL << 62) /* Non-canonical address form */
276 #define DRM_GEM_MAX_IDX 0x3fffff
277 #define DRM_GEM_MAPPING_IDX(o) (((o) >> 40) & DRM_GEM_MAX_IDX)
278 #define DRM_GEM_MAPPING_OFF(i) (((uint64_t)(i)) << 40)
279 #define DRM_GEM_MAPPING_MAPOFF(o) \
280 ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
282 SYSCTL_DECL(_hw_drm);
284 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
286 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
288 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
289 #define DRM_DEV_UID UID_ROOT
290 #define DRM_DEV_GID GID_WHEEL
292 #define DRM_CURPROC curthread
293 #define DRM_STRUCTPROC struct thread
294 #define DRM_CURRENTPID (curproc != NULL ? curproc->p_pid : -1)
295 #define DRM_LOCK(dev) lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
296 #define DRM_UNLOCK(dev) lockmgr(&(dev)->struct_mutex, LK_RELEASE)
297 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout) \
298 (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
299 #if defined(INVARIANTS)
300 #define DRM_LOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
301 #define DRM_UNLOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
303 #define DRM_LOCK_ASSERT(d)
304 #define DRM_UNLOCK_ASSERT(d)
307 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
309 typedef void irqreturn_t;
310 #define IRQ_HANDLED /* nothing */
311 #define IRQ_NONE /* nothing */
313 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
315 #define DRM_MTRR_WC MDF_WRITECOMBINE
317 /* Returns -errno to shared code */
318 #define DRM_WAIT_ON( ret, queue, timeout, condition ) \
319 for ( ret = 0 ; !ret && !(condition) ; ) { \
320 lwkt_serialize_enter(&dev->irq_lock); \
321 if (!(condition)) { \
322 tsleep_interlock(&(queue), PCATCH); \
323 lwkt_serialize_exit(&dev->irq_lock); \
324 ret = -tsleep(&(queue), PCATCH | PINTERLOCKED, \
325 "drmwtq", (timeout)); \
327 lwkt_serialize_exit(&dev->irq_lock); \
331 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
332 vm_memattr_t memattr);
333 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
334 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
337 #define dev_dbg(dev, fmt, ...) do { \
338 if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) { \
339 device_printf((dev), "debug: " fmt, ## __VA_ARGS__); \
343 typedef struct drm_pci_id_list
352 * Ioctl function type.
354 * \param inode device inode.
355 * \param file_priv DRM file private pointer.
356 * \param cmd command.
357 * \param arg argument.
359 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
360 struct drm_file *file_priv);
362 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
365 #define DRM_IOCTL_NR(n) ((n) & 0xff)
368 #define DRM_MASTER 0x2
369 #define DRM_ROOT_ONLY 0x4
370 #define DRM_CONTROL_ALLOW 0x8
371 #define DRM_UNLOCKED 0x10
372 #define DRM_RENDER_ALLOW 0x20
374 struct drm_ioctl_desc {
378 unsigned int cmd_drv;
383 * Creates a driver or general drm_ioctl_desc array entry for the given
384 * ioctl, for use by drm_ioctl().
386 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
387 [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
389 typedef struct drm_magic_entry {
390 struct list_head head;
391 struct drm_hash_item hash_item;
392 struct drm_file *priv;
393 struct drm_magic_entry *next;
396 typedef struct drm_magic_head {
397 struct drm_magic_entry *head;
398 struct drm_magic_entry *tail;
401 /** bufs is one longer than it has to be */
402 struct drm_waitlist {
403 int count; /**< Number of possible buffers */
404 struct drm_buf **bufs; /**< List of pointers to buffers */
405 struct drm_buf **rp; /**< Read pointer */
406 struct drm_buf **wp; /**< Write pointer */
407 struct drm_buf **end; /**< End pointer */
408 struct spinlock *read_lock;
409 struct spinlock *write_lock;
412 /* Event queued up for userspace to read */
413 struct drm_pending_event {
414 struct drm_event *event;
415 struct list_head link;
416 struct drm_file *file_priv;
417 pid_t pid; /* pid of requester, no guarantee it's valid by the time
418 we deliver the event, for tracing only */
419 void (*destroy)(struct drm_pending_event *event);
422 /* initial implementaton using a linked list - todo hashtab */
423 struct drm_prime_file_private {
424 struct list_head head;
427 #endif /* DUMBBELL_WIP */
430 /** File private data */
433 struct drm_device *dev;
436 /* true when the client has asked us to expose stereo 3D mode flags */
439 * true if client understands CRTC primary planes and cursor planes
442 unsigned universal_planes:1;
443 /* true if client understands atomic properties */
449 unsigned long ioctl_count;
450 struct list_head lhead;
453 /** Mapping of mm object handles to object pointers. */
454 struct idr object_idr;
455 /** Lock for synchronization of access to object_idr. */
456 struct lock table_lock;
461 struct drm_master *masterp;
464 * fbs - List of framebuffers associated with this file.
466 * Protected by fbs_lock. Note that the fbs list holds a reference on
467 * the fb object to prevent it from untimely disappearing.
469 struct list_head fbs;
470 struct lock fbs_lock;
472 /** User-created blob properties; this retains a reference on the
474 struct list_head blobs;
476 wait_queue_head_t event_wait;
477 struct list_head event_list;
480 struct drm_prime_file_private prime;
486 struct drm_lock_data {
487 struct drm_hw_lock *hw_lock; /**< Hardware lock */
488 /** Private of lock holder's file (NULL=kernel) */
489 struct drm_file *file_priv;
490 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
491 unsigned long lock_time; /**< Time of last lock in jiffies */
495 * GEM specific mm private for tracking GEM objects
498 struct drm_vma_offset_manager vma_manager;
499 struct drm_mm offset_manager; /**< Offset mgmt for buffer objects */
500 struct drm_open_hash offset_hash; /**< User token hash table for maps */
501 struct unrhdr *idxunr;
505 #include "drm_crtc.h"
508 * struct drm_master - drm master structure
510 * @refcount: Refcount for this master object.
511 * @minor: Link back to minor char device we are master for. Immutable.
512 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
513 * @unique_len: Length of unique field. Protected by drm_global_mutex.
514 * @unique_size: Amount allocated. Protected by drm_global_mutex.
515 * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
516 * @magicfree: List of used authentication tokens. Protected by struct_mutex.
517 * @lock: DRI lock information.
518 * @driver_priv: Pointer to driver-private information.
522 struct kref refcount; /* refcount for this master */
524 struct list_head head; /**< each minor contains a list of masters */
525 struct drm_minor *minor; /**< link back to minor we are a master for */
527 char *unique; /**< Unique identifier: e.g., busid */
528 int unique_len; /**< Length of unique field */
529 int unique_size; /**< amount allocated */
531 int blocked; /**< Blocked due to VC switch? */
533 struct drm_open_hash magiclist;
534 struct list_head magicfree;
535 struct drm_lock_data lock;
539 /* Size of ringbuffer for vblank timestamps. Just double-buffer
540 * in initial implementation.
542 #define DRM_VBLANKTIME_RBSIZE 2
544 /* Flags and return codes for get_vblank_timestamp() driver function. */
545 #define DRM_CALLED_FROM_VBLIRQ 1
546 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
547 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
549 /* get_scanout_position() return flags */
550 #define DRM_SCANOUTPOS_VALID (1 << 0)
551 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
552 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
555 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
558 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
561 * DRM driver structure. This structure represent the common code for
562 * a family of cards. There will one drm_device for each card present
566 int (*load)(struct drm_device *, unsigned long flags);
567 int (*use_msi)(struct drm_device *, unsigned long flags);
568 int (*firstopen)(struct drm_device *);
569 int (*open)(struct drm_device *, struct drm_file *);
570 void (*preclose)(struct drm_device *, struct drm_file *file_priv);
571 void (*postclose)(struct drm_device *, struct drm_file *);
572 void (*lastclose)(struct drm_device *);
573 int (*unload)(struct drm_device *);
574 void (*reclaim_buffers_locked)(struct drm_device *,
575 struct drm_file *file_priv);
576 int (*dma_ioctl)(struct drm_device *dev, void *data,
577 struct drm_file *file_priv);
578 void (*dma_ready)(struct drm_device *);
579 int (*dma_quiescent)(struct drm_device *);
580 int (*dma_flush_block_and_flush)(struct drm_device *, int context,
581 enum drm_lock_flags flags);
582 int (*dma_flush_unblock)(struct drm_device *, int context,
583 enum drm_lock_flags flags);
584 int (*context_ctor)(struct drm_device *dev, int context);
585 int (*context_dtor)(struct drm_device *dev, int context);
586 int (*kernel_context_switch)(struct drm_device *dev, int old,
588 int (*kernel_context_switch_unlock)(struct drm_device *dev);
589 void (*irq_preinstall)(struct drm_device *dev);
590 int (*irq_postinstall)(struct drm_device *dev);
591 void (*irq_uninstall)(struct drm_device *dev);
592 void (*irq_handler)(void *arg);
594 u32 (*get_vblank_counter)(struct drm_device *dev, int crtc);
595 int (*enable_vblank)(struct drm_device *dev, int crtc);
596 void (*disable_vblank)(struct drm_device *dev, int crtc);
599 * Called by \c drm_device_is_agp. Typically used to determine if a
600 * card is really attached to AGP or not.
602 * \param dev DRM device handle
605 * One of three values is returned depending on whether or not the
606 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
607 * (return of 1), or may or may not be AGP (return of 2).
609 int (*device_is_agp) (struct drm_device *dev);
612 * Called by vblank timestamping code.
614 * Return the current display scanout position from a crtc, and an
615 * optional accurate ktime_get timestamp of when position was measured.
617 * \param dev DRM device.
618 * \param crtc Id of the crtc to query.
619 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
620 * \param *vpos Target location for current vertical scanout position.
621 * \param *hpos Target location for current horizontal scanout position.
622 * \param *stime Target location for timestamp taken immediately before
623 * scanout position query. Can be NULL to skip timestamp.
624 * \param *etime Target location for timestamp taken immediately after
625 * scanout position query. Can be NULL to skip timestamp.
627 * Returns vpos as a positive number while in active scanout area.
628 * Returns vpos as a negative number inside vblank, counting the number
629 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
630 * until start of active scanout / end of vblank."
632 * \return Flags, or'ed together as follows:
634 * DRM_SCANOUTPOS_VALID = Query successful.
635 * DRM_SCANOUTPOS_INVBL = Inside vblank.
636 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
637 * this flag means that returned position may be offset by a constant
638 * but unknown small number of scanlines wrt. real scanout position.
641 int (*get_scanout_position) (struct drm_device *dev, int crtc,
643 int *vpos, int *hpos, ktime_t *stime,
646 int (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
647 int *max_error, struct timeval *vblank_time,
650 void (*gem_free_object)(struct drm_gem_object *obj);
651 int (*gem_open_object)(struct drm_gem_object *, struct drm_file *);
652 void (*gem_close_object)(struct drm_gem_object *, struct drm_file *);
654 struct cdev_pager_ops *gem_pager_ops;
656 int (*dumb_create)(struct drm_file *file_priv,
657 struct drm_device *dev, struct drm_mode_create_dumb *args);
658 int (*dumb_map_offset)(struct drm_file *file_priv,
659 struct drm_device *dev, uint32_t handle, uint64_t *offset);
660 int (*dumb_destroy)(struct drm_file *file_priv,
661 struct drm_device *dev, uint32_t handle);
663 int (*sysctl_init)(struct drm_device *dev,
664 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
665 void (*sysctl_cleanup)(struct drm_device *dev);
667 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
672 const char *name; /* Simple driver name */
673 const char *desc; /* Longer driver name */
674 const char *date; /* Date of last major changes. */
678 const struct drm_ioctl_desc *ioctls;
683 * Info file list entry. This structure represents a debugfs or proc file to
684 * be created by the drm core
686 struct drm_info_list {
687 const char *name; /** file name */
688 int (*show)(struct seq_file*, void*); /** show callback */
689 u32 driver_features; /**< Required driver features for this entry */
694 * debugfs node structure. This structure represents a debugfs file.
696 struct drm_info_node {
697 struct list_head list;
698 struct drm_minor *minor;
699 const struct drm_info_list *info_ent;
704 * DRM minor structure. This structure represents a drm minor number.
707 int index; /**< Minor device number */
708 int type; /**< Control or render */
709 device_t kdev; /**< OS device */
710 struct drm_device *dev;
712 struct drm_master *master; /* currently active master for this node */
713 struct list_head master_list;
714 struct drm_mode_group mode_group;
717 struct drm_pending_vblank_event {
718 struct drm_pending_event base;
720 struct drm_event_vblank event;
723 struct drm_sysctl_info {
724 struct sysctl_ctx_list ctx;
728 /* Length for the array of resource pointers for drm_get_resource_*. */
729 #define DRM_MAX_PCI_RESOURCE 6
731 struct drm_vblank_crtc {
732 struct drm_device *dev; /* pointer to the drm_device */
733 wait_queue_head_t queue; /**< VBLANK wait queue */
734 struct timer_list disable_timer; /* delayed disable timer */
736 /* vblank counter, protected by dev->vblank_time_lock for writes */
738 /* vblank timestamps, protected by dev->vblank_time_lock for writes */
739 struct timeval time[DRM_VBLANKTIME_RBSIZE];
741 atomic_t refcount; /* number of users of vblank interruptsper crtc */
742 u32 last; /* protected by dev->vbl_lock, used */
743 /* for wraparound handling */
744 u32 last_wait; /* Last vblank seqno waited per CRTC */
745 unsigned int inmodeset; /* Display driver is setting mode */
746 unsigned int pipe; /* crtc index */
747 bool enabled; /* so we don't call enable more than
752 * DRM device structure. This structure represent a complete card that
753 * may contain multiple heads.
756 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
758 char *unique; /* Unique identifier: e.g., busid */
759 int unique_len; /* Length of unique field */
760 struct cdev *devnode; /* Device number for mknod */
761 int if_version; /* Highest interface version set */
763 int flags; /* Flags to open(2) */
766 struct spinlock dma_lock; /* protects dev->dma */
767 struct lwkt_serialize irq_lock; /* protects irq condition checks */
768 struct lock dev_lock; /* protects everything else */
772 struct lock struct_mutex; /**< For others */
773 struct lock master_mutex; /**< For drm_minor::master */
776 /** \name Usage Counters */
778 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
779 struct spinlock buf_lock; /**< For drm_device::buf_use and a few other things. */
780 int buf_use; /**< Buffers in use -- cannot alloc */
781 atomic_t buf_alloc; /**< Buffer allocation in progress */
785 /** \name Performance counters */
787 unsigned long counters;
788 enum drm_stat_type types[15];
793 struct drm_open_hash magiclist; /**< magic hash table */
794 struct list_head magicfree;
796 struct list_head filelist;
798 /** \name Memory management */
800 struct list_head maplist; /**< Linked list of regions */
801 int map_count; /**< Number of mappable regions */
802 struct drm_open_hash map_hash; /**< User token hash table for maps */
804 /** \name Context handle management */
806 struct list_head ctxlist; /**< Linked list of context handles */
807 int ctx_count; /**< Number of context handles */
808 struct lock ctxlist_mutex; /**< For ctxlist */
814 struct drm_lock_data lock; /* Information on hardware lock */
816 /** \name DMA support */
818 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
821 int irq; /* Interrupt used by board */
822 int irq_type; /* IRQ type (MSI enabled or not) */
823 int irqrid; /* Interrupt used by board */
824 struct resource *irqr; /* Resource for interrupt used by board */
825 void *irqh; /* Handle from bus_setup_intr */
827 /* Storage of resource pointers for drm_get_resource_* */
828 struct resource *pcir[DRM_MAX_PCI_RESOURCE];
829 int pcirid[DRM_MAX_PCI_RESOURCE];
836 /** \name Context support */
838 int irq_enabled; /**< True if irq handler is enabled */
839 __volatile__ long context_flag; /**< Context swapping flag */
840 __volatile__ long interrupt_flag; /**< Interruption handler flag */
841 __volatile__ long dma_flag; /**< DMA dispatch flag */
842 wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
843 int last_checked; /**< Last context checked for DMA */
844 int last_context; /**< Last current context */
845 unsigned long last_switch; /**< jiffies at last context switch */
848 /** \name VBLANK IRQ support */
852 * At load time, disabling the vblank interrupt won't be allowed since
853 * old clients may not call the modeset ioctl and therefore misbehave.
854 * Once the modeset ioctl *has* been called though, we can safely
855 * disable them when unused.
857 bool vblank_disable_allowed;
860 * If true, vblank interrupt will be disabled immediately when the
861 * refcount drops to zero, as opposed to via the vblank disable
863 * This can be set to true it the hardware has a working vblank
864 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
867 bool vblank_disable_immediate;
869 /* array of size num_crtcs */
870 struct drm_vblank_crtc *vblank;
872 struct lock vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
873 struct lock vbl_lock;
874 struct timer_list vblank_disable_timer;
876 u32 max_vblank_count; /**< size of vblank counter register */
881 struct list_head vblank_event_list;
882 struct lock event_lock;
886 struct sigio *buf_sigio; /* Processes waiting for SIGIO */
889 struct drm_sysctl_info *sysctl;
892 struct drm_sg_mem *sg; /**< Scatter gather memory */
893 unsigned int num_crtcs; /**< Number of CRTCs on this device */
895 unsigned long *ctx_bitmap;
902 struct drm_agp_head *agp; /**< AGP data */
904 struct device *dev; /**< Device structure */
905 struct pci_dev *pdev; /**< PCI device structure */
907 struct drm_driver *driver;
908 struct drm_local_map *agp_buffer_map;
909 unsigned int agp_buffer_token;
910 struct drm_minor *control; /**< Control node for card */
911 struct drm_minor *primary; /**< render type primary screen head */
913 struct drm_mode_config mode_config; /**< Current mode config */
915 /** \name GEM information */
917 struct lock object_name_lock;
918 struct idr object_name_idr;
922 void *sysctl_private;
926 int switch_power_state;
928 atomic_t unplugged; /* device has been unplugged or gone away */
931 #define DRM_SWITCH_POWER_ON 0
932 #define DRM_SWITCH_POWER_OFF 1
933 #define DRM_SWITCH_POWER_CHANGING 2
934 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
936 static __inline__ int drm_core_check_feature(struct drm_device *dev,
939 return ((dev->driver->driver_features & feature) ? 1 : 0);
942 static inline void drm_device_set_unplugged(struct drm_device *dev)
945 atomic_set(&dev->unplugged, 1);
948 static inline int drm_device_is_unplugged(struct drm_device *dev)
950 int ret = atomic_read(&dev->unplugged);
955 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
957 return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
961 * drm_is_master() - Check whether a DRM open-file is DRM-Master
962 * @file: DRM open-file context
964 * This checks whether a DRM open-file context is owner of the master context
965 * attached to it. If a file owns a master context, it's called DRM-Master.
966 * Per DRM device, only one such file can be DRM-Master at a time.
968 * Returns: True if the file is DRM-Master, otherwise false.
970 static inline bool drm_is_master(const struct drm_file *file)
973 return file->master && file->master == file->minor->master;
979 /******************************************************************/
980 /** \name Internal function definitions */
984 static inline int drm_core_has_AGP(struct drm_device *dev)
986 return drm_core_check_feature(dev, DRIVER_USE_AGP);
989 #define drm_core_has_AGP(dev) (0)
992 extern int drm_notyet_flag;
993 extern unsigned int drm_rnodes;
994 extern unsigned int drm_universal_planes;
996 extern int drm_vblank_offdelay;
997 extern unsigned int drm_timestamp_precision;
999 /* Driver support (drm_drv.h) */
1000 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1001 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1002 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1003 int drm_create_cdevs(device_t kdev);
1004 d_ioctl_t drm_ioctl;
1005 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1007 /* Device support (drm_fops.h) */
1009 d_close_t drm_close;
1011 d_kqfilter_t drm_kqfilter;
1012 int drm_release(device_t kdev);
1015 d_mmap_single_t drm_mmap_single;
1017 void drm_cdevpriv_dtor(void *cd);
1019 int drm_add_busid_modesetting(struct drm_device *dev,
1020 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1022 /* File operations helpers (drm_fops.c) */
1023 extern int drm_open_helper(struct cdev *kdev, int flags, int fmt,
1025 struct drm_device *dev,
1027 extern struct drm_file *drm_find_file_by_proc(struct drm_device *dev,
1031 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1032 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1034 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1035 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1037 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1038 struct drm_file *file_priv);
1039 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1040 struct drm_file *file_priv);
1042 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1043 dma_addr_t *addrs, int max_pages);
1044 #endif /* DUMBBELL_WIP */
1045 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1046 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1049 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1050 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1051 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1052 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1053 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1055 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1056 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1057 struct drm_gem_object **obj);
1058 #endif /* DUMBBELL_WIP */
1060 int drm_mtrr_add(unsigned long offset, size_t size, int flags);
1061 int drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1064 * These are exported to drivers so that they can implement fencing using
1065 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1068 unsigned long drm_get_resource_start(struct drm_device *dev,
1069 unsigned int resource);
1070 unsigned long drm_get_resource_len(struct drm_device *dev,
1071 unsigned int resource);
1073 /* IRQ support (drm_irq.h) */
1074 extern int drm_irq_install(struct drm_device *dev, int irq);
1075 extern int drm_irq_uninstall(struct drm_device *dev);
1077 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
1078 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1079 struct drm_file *filp);
1080 extern u32 drm_vblank_count(struct drm_device *dev, int pipe);
1081 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1082 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1083 struct timeval *vblanktime);
1084 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1085 struct drm_pending_vblank_event *e);
1086 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1087 struct drm_pending_vblank_event *e);
1088 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
1089 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1090 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
1091 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
1092 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1093 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1094 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
1095 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1096 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
1097 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
1098 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1099 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1100 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1101 extern void drm_vblank_cleanup(struct drm_device *dev);
1103 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1104 unsigned int pipe, int *max_error,
1105 struct timeval *vblank_time,
1107 const struct drm_crtc *refcrtc,
1108 const struct drm_display_mode *mode);
1109 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1110 const struct drm_display_mode *mode);
1113 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1114 * @crtc: which CRTC's vblank waitqueue to retrieve
1116 * This function returns a pointer to the vblank waitqueue for the CRTC.
1117 * Drivers can use this to implement vblank waits using wait_event() & co.
1119 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1121 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1124 /* Modesetting support */
1125 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1126 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1128 /* sysctl support (drm_sysctl.h) */
1129 extern int drm_sysctl_init(struct drm_device *dev);
1130 extern int drm_sysctl_cleanup(struct drm_device *dev);
1132 /* Locking IOCTL support (drm_drv.c) */
1133 int drm_lock(struct drm_device *dev, void *data,
1134 struct drm_file *file_priv);
1135 int drm_unlock(struct drm_device *dev, void *data,
1136 struct drm_file *file_priv);
1137 int drm_setversion(struct drm_device *dev, void *data,
1138 struct drm_file *file_priv);
1141 * These are exported to drivers so that they can implement fencing using
1142 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1145 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1147 /* Misc. IOCTL support (drm_ioctl.c) */
1148 int drm_noop(struct drm_device *dev, void *data,
1149 struct drm_file *file_priv);
1151 /* Cache management (drm_cache.c) */
1152 void drm_clflush_pages(struct vm_page *pages[], unsigned long num_pages);
1153 void drm_clflush_sg(struct sg_table *st);
1154 void drm_clflush_virt_range(void *addr, unsigned long length);
1156 /* DMA support (drm_dma.c) */
1157 int drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1159 /* Stub support (drm_stub.h) */
1161 extern void drm_put_dev(struct drm_device *dev);
1162 extern void drm_unplug_dev(struct drm_device *dev);
1163 extern unsigned int drm_debug;
1164 extern bool drm_atomic;
1166 /* Scatter Gather Support (drm_scatter.c) */
1167 int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1168 struct drm_file *file_priv);
1169 int drm_sg_free(struct drm_device *dev, void *data,
1170 struct drm_file *file_priv);
1172 /* consistent PCI memory functions (drm_pci.c) */
1173 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1175 void drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah);
1177 /* sysfs support (drm_sysfs.c) */
1178 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1180 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1181 vm_size_t size, struct vm_object **obj_res, int nprot);
1182 void drm_gem_pager_dtr(void *obj);
1184 struct ttm_bo_device;
1185 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1186 vm_size_t size, struct vm_object **obj_res, int nprot);
1187 struct ttm_buffer_object;
1188 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1190 void drm_device_lock_mtx(struct drm_device *dev);
1191 void drm_device_unlock_mtx(struct drm_device *dev);
1192 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1193 const char *msg, int timeout);
1194 void drm_device_assert_mtx_locked(struct drm_device *dev);
1195 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1197 void drm_device_lock_struct(struct drm_device *dev);
1198 void drm_device_unlock_struct(struct drm_device *dev);
1199 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1200 const char *msg, int timeout);
1201 void drm_device_assert_struct_locked(struct drm_device *dev);
1202 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1204 void drm_compat_locking_init(struct drm_device *dev);
1205 void drm_sleep_locking_init(struct drm_device *dev);
1207 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1208 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1209 void drm_fini_pdev(struct pci_dev **pdev);
1211 /* Inline replacements for drm_alloc and friends */
1212 static __inline__ void *
1213 drm_alloc(size_t size, struct malloc_type *area)
1215 return kmalloc(size, area, M_WAITOK | M_NULLOK);
1218 static __inline__ void *
1219 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1221 return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1224 static __inline__ void
1225 drm_free(void *pt, struct malloc_type *area)
1227 /* kfree is special!!! */
1232 #include <drm/drm_mem_util.h>
1234 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1235 struct device *parent);
1236 void drm_dev_ref(struct drm_device *dev);
1237 void drm_dev_unref(struct drm_device *dev);
1238 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1239 void drm_dev_unregister(struct drm_device *dev);
1240 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1242 /* FreeBSD compatibility macros */
1243 #define VM_OBJECT_WLOCK(object) VM_OBJECT_LOCK(object)
1244 #define VM_OBJECT_WUNLOCK(object) VM_OBJECT_UNLOCK(object)
1247 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1249 if (dev->driver->device_is_agp != NULL) {
1250 int err = (*dev->driver->device_is_agp) (dev);
1257 return (pci_find_extcap(dev->dev, PCIY_AGP, NULL) == 0);
1260 #define DRM_PCIE_SPEED_25 1
1261 #define DRM_PCIE_SPEED_50 2
1262 #define DRM_PCIE_SPEED_80 4
1264 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1267 #define drm_can_sleep() (HZ & 1)
1269 #endif /* __KERNEL__ */
1270 #endif /* _DRM_P_H_ */