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 <linux/atomic.h>
86 #include <linux/bug.h>
87 #include <linux/dma-mapping.h>
88 #include <linux/capability.h>
89 #include <linux/err.h>
90 #include <linux/idr.h>
91 #include <linux/pci.h>
92 #include <linux/jiffies.h>
93 #include <linux/kernel.h>
95 #include <linux/kref.h>
96 #include <linux/list.h>
98 #include <linux/moduleparam.h>
99 #include <linux/mutex.h>
100 #include <linux/slab.h>
101 #include <linux/scatterlist.h>
102 #include <linux/timer.h>
104 #include <linux/seq_file.h>
105 #include <linux/types.h>
106 #include <linux/vmalloc.h>
107 #include <linux/wait.h>
108 #include <linux/workqueue.h>
110 #include <asm/uaccess.h>
112 #include <uapi_drm/drm.h>
113 #include <uapi_drm/drm_mode.h>
115 #include <drm/drm_agpsupport.h>
116 #include <drm/drm_crtc.h>
117 #include <drm/drm_global.h>
118 #include <drm/drm_hashtab.h>
119 #include <drm/drm_mem_util.h>
120 #include <drm/drm_mm.h>
121 #include <drm/drm_os_linux.h>
122 #include <uapi_drm/drm_sarea.h>
123 #include <drm/drm_vma_manager.h>
128 struct drm_local_map;
129 struct drm_device_dma;
130 struct drm_dma_handle;
131 struct drm_gem_object;
134 #ifdef CONFIG_VIDEOMODE_HELPERS
135 struct videomode; /* XXX empty struct in videomode.h ? */
137 struct reservation_object;
138 struct dma_buf_attachment;
141 * 4 debug categories are defined:
143 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
144 * This is the category used by the DRM_DEBUG() macro.
146 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
147 * This is the category used by the DRM_DEBUG_DRIVER() macro.
149 * KMS: used in the modesetting code.
150 * This is the category used by the DRM_DEBUG_KMS() macro.
152 * PRIME: used in the prime code.
153 * This is the category used by the DRM_DEBUG_PRIME() macro.
155 * ATOMIC: used in the atomic code.
156 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
158 * Enabling verbose debug messages is done through the drm.debug parameter,
159 * each category being enabled by a bit.
161 * drm.debug=0x1 will enable CORE messages
162 * drm.debug=0x2 will enable DRIVER messages
163 * drm.debug=0x3 will enable CORE and DRIVER messages
165 * drm.debug=0xf will enable all messages
167 * An interesting feature is that it's possible to enable verbose logging at
168 * run-time by echoing the debug value in its sysfs node:
169 * # echo 0xf > /sys/module/drm/parameters/debug
171 #define DRM_UT_CORE 0x01
172 #define DRM_UT_DRIVER 0x02
173 #define DRM_UT_KMS 0x04
174 #define DRM_UT_PRIME 0x08
175 #define DRM_UT_ATOMIC 0x10
177 void drm_ut_debug_printk(const char *function_name,
178 const char *format, ...);
180 void drm_err(const char *format, ...);
182 /***********************************************************************/
183 /** \name DRM template customization defaults */
186 /* driver capabilities and requirements mask */
187 #define DRIVER_USE_AGP 0x1
188 #define DRIVER_REQUIRE_AGP 0x2
189 #define DRIVER_PCI_DMA 0x8
190 #define DRIVER_SG 0x10
191 #define DRIVER_HAVE_DMA 0x20
192 #define DRIVER_HAVE_IRQ 0x40
193 #define DRIVER_IRQ_SHARED 0x80
194 #define DRIVER_GEM 0x1000
195 #define DRIVER_MODESET 0x2000
196 #define DRIVER_PRIME 0x4000
197 #define DRIVER_RENDER 0x8000
198 #define DRIVER_ATOMIC 0x10000
199 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
201 #define DRM_MAGIC_HASH_ORDER 4 /**< Size of key hash table. Must be power of 2. */
203 /***********************************************************************/
204 /** \name Macros to make printk easier */
210 * \param fmt printf() like format string.
211 * \param arg arguments
213 #define DRM_ERROR(fmt, ...) \
214 kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt, \
215 DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
218 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
220 * \param fmt printf() like format string.
221 * \param arg arguments
224 #define DRM_INFO(fmt, ...) kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
226 #define DRM_INFO_ONCE(fmt, ...) \
227 printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
232 * \param fmt printf() like format string.
233 * \param arg arguments
236 #define DRM_DEBUGBITS_DEBUG 0x1
237 #define DRM_DEBUGBITS_KMS 0x2
238 #define DRM_DEBUGBITS_FAILED_IOCTL 0x4
239 #define DRM_DEBUGBITS_VERBOSE 0x8
241 #define DRM_DEBUG(fmt, ...) do { \
242 if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0) \
243 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
244 __func__ , ##__VA_ARGS__); \
247 #define DRM_DEBUG_VERBOSE(fmt, ...) do { \
248 if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0) \
249 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
250 __func__ , ##__VA_ARGS__); \
253 #define DRM_DEBUG_KMS(fmt, ...) do { \
254 if ((drm_debug & DRM_DEBUGBITS_KMS) != 0) \
255 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
256 __func__ , ##__VA_ARGS__); \
259 #define DRM_DEBUG_DRIVER(fmt, ...) do { \
260 if ((drm_debug & DRM_DEBUGBITS_KMS) != 0) \
261 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
262 __func__ , ##__VA_ARGS__); \
265 #define DRM_DEBUG_ATOMIC(fmt, args...) \
267 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
268 drm_ut_debug_printk(__func__, fmt, ##args); \
273 /***********************************************************************/
274 /** \name Internal types and structures */
277 #define DRM_GEM_MAPPING_MASK (3ULL << 62)
278 #define DRM_GEM_MAPPING_KEY (2ULL << 62) /* Non-canonical address form */
279 #define DRM_GEM_MAX_IDX 0x3fffff
280 #define DRM_GEM_MAPPING_IDX(o) (((o) >> 40) & DRM_GEM_MAX_IDX)
281 #define DRM_GEM_MAPPING_OFF(i) (((uint64_t)(i)) << 40)
282 #define DRM_GEM_MAPPING_MAPOFF(o) \
283 ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
285 SYSCTL_DECL(_hw_drm);
287 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
289 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
291 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
292 #define DRM_DEV_UID UID_ROOT
293 #define DRM_DEV_GID GID_WHEEL
295 #define DRM_CURPROC curthread
296 #define DRM_STRUCTPROC struct thread
297 #define DRM_LOCK(dev) lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
298 #define DRM_UNLOCK(dev) lockmgr(&(dev)->struct_mutex, LK_RELEASE)
299 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout) \
300 (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
301 #if defined(INVARIANTS)
302 #define DRM_LOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
303 #define DRM_UNLOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
305 #define DRM_LOCK_ASSERT(d)
306 #define DRM_UNLOCK_ASSERT(d)
309 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
311 typedef void irqreturn_t;
312 #define IRQ_HANDLED /* nothing */
313 #define IRQ_NONE /* nothing */
315 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
317 #define DRM_MTRR_WC MDF_WRITECOMBINE
319 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
320 vm_memattr_t memattr);
321 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
322 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
325 typedef struct drm_pci_id_list
334 * Ioctl function type.
336 * \param inode device inode.
337 * \param file_priv DRM file private pointer.
338 * \param cmd command.
339 * \param arg argument.
341 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
342 struct drm_file *file_priv);
344 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
347 #define DRM_IOCTL_NR(n) ((n) & 0xff)
350 #define DRM_MASTER 0x2
351 #define DRM_ROOT_ONLY 0x4
352 #define DRM_CONTROL_ALLOW 0x8
353 #define DRM_UNLOCKED 0x10
354 #define DRM_RENDER_ALLOW 0x20
356 struct drm_ioctl_desc {
360 unsigned int cmd_drv;
365 * Creates a driver or general drm_ioctl_desc array entry for the given
366 * ioctl, for use by drm_ioctl().
368 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
369 [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
371 typedef struct drm_magic_entry {
372 struct list_head head;
373 struct drm_hash_item hash_item;
374 struct drm_file *priv;
375 struct drm_magic_entry *next;
378 typedef struct drm_magic_head {
379 struct drm_magic_entry *head;
380 struct drm_magic_entry *tail;
383 /** bufs is one longer than it has to be */
384 struct drm_waitlist {
385 int count; /**< Number of possible buffers */
386 struct drm_buf **bufs; /**< List of pointers to buffers */
387 struct drm_buf **rp; /**< Read pointer */
388 struct drm_buf **wp; /**< Write pointer */
389 struct drm_buf **end; /**< End pointer */
390 struct spinlock *read_lock;
391 struct spinlock *write_lock;
394 /* Event queued up for userspace to read */
395 struct drm_pending_event {
396 struct drm_event *event;
397 struct list_head link;
398 struct drm_file *file_priv;
399 pid_t pid; /* pid of requester, no guarantee it's valid by the time
400 we deliver the event, for tracing only */
401 void (*destroy)(struct drm_pending_event *event);
404 /* initial implementaton using a linked list - todo hashtab */
405 struct drm_prime_file_private {
406 struct list_head head;
409 #endif /* DUMBBELL_WIP */
412 /** File private data */
415 struct drm_device *dev;
418 /* true when the client has asked us to expose stereo 3D mode flags */
421 * true if client understands CRTC primary planes and cursor planes
424 unsigned universal_planes:1;
425 /* true if client understands atomic properties */
431 unsigned long ioctl_count;
432 struct list_head lhead;
435 /** Mapping of mm object handles to object pointers. */
436 struct idr object_idr;
437 /** Lock for synchronization of access to object_idr. */
438 struct lock table_lock;
443 struct drm_master *masterp;
446 * fbs - List of framebuffers associated with this file.
448 * Protected by fbs_lock. Note that the fbs list holds a reference on
449 * the fb object to prevent it from untimely disappearing.
451 struct list_head fbs;
452 struct lock fbs_lock;
454 /** User-created blob properties; this retains a reference on the
456 struct list_head blobs;
458 wait_queue_head_t event_wait;
459 struct list_head event_list;
462 struct drm_prime_file_private prime;
468 struct drm_lock_data {
469 struct drm_hw_lock *hw_lock; /**< Hardware lock */
470 /** Private of lock holder's file (NULL=kernel) */
471 struct drm_file *file_priv;
472 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
473 unsigned long lock_time; /**< Time of last lock in jiffies */
477 * GEM specific mm private for tracking GEM objects
480 struct drm_vma_offset_manager vma_manager;
481 struct drm_mm offset_manager; /**< Offset mgmt for buffer objects */
482 struct drm_open_hash offset_hash; /**< User token hash table for maps */
483 struct unrhdr *idxunr;
487 * struct drm_master - drm master structure
489 * @refcount: Refcount for this master object.
490 * @minor: Link back to minor char device we are master for. Immutable.
491 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
492 * @unique_len: Length of unique field. Protected by drm_global_mutex.
493 * @unique_size: Amount allocated. Protected by drm_global_mutex.
494 * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
495 * @magicfree: List of used authentication tokens. Protected by struct_mutex.
496 * @lock: DRI lock information.
497 * @driver_priv: Pointer to driver-private information.
500 struct kref refcount; /* refcount for this master */
501 struct list_head head; /**< each minor contains a list of masters */
502 struct drm_minor *minor; /**< link back to minor we are a master for */
503 char *unique; /**< Unique identifier: e.g., busid */
504 int unique_len; /**< Length of unique field */
505 int unique_size; /**< amount allocated */
506 int blocked; /**< Blocked due to VC switch? */
507 struct drm_open_hash magiclist;
508 struct list_head magicfree;
509 struct drm_lock_data lock;
513 /* Size of ringbuffer for vblank timestamps. Just double-buffer
514 * in initial implementation.
516 #define DRM_VBLANKTIME_RBSIZE 2
518 /* Flags and return codes for get_vblank_timestamp() driver function. */
519 #define DRM_CALLED_FROM_VBLIRQ 1
520 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
521 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
523 /* get_scanout_position() return flags */
524 #define DRM_SCANOUTPOS_VALID (1 << 0)
525 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
526 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
529 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
532 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
535 * DRM driver structure. This structure represent the common code for
536 * a family of cards. There will one drm_device for each card present
540 int (*load) (struct drm_device *, unsigned long flags);
541 int (*use_msi) (struct drm_device *, unsigned long flags);
542 int (*firstopen) (struct drm_device *);
543 int (*open) (struct drm_device *, struct drm_file *);
544 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
545 void (*postclose) (struct drm_device *, struct drm_file *);
546 void (*lastclose) (struct drm_device *);
547 int (*unload) (struct drm_device *);
548 void (*reclaim_buffers_locked) (struct drm_device *,
549 struct drm_file *file_priv);
550 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
551 int (*dma_quiescent) (struct drm_device *);
552 int (*context_ctor) (struct drm_device *dev, int context);
553 int (*context_dtor) (struct drm_device *dev, int context);
556 * get_vblank_counter - get raw hardware vblank counter
558 * @crtc: counter to fetch
560 * Driver callback for fetching a raw hardware vblank counter for @crtc.
561 * If a device doesn't have a hardware counter, the driver can simply
562 * return the value of drm_vblank_count. The DRM core will account for
563 * missed vblank events while interrupts where disabled based on system
566 * Wraparound handling and loss of events due to modesetting is dealt
567 * with in the DRM core code.
570 * Raw vblank counter value.
572 u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
575 * enable_vblank - enable vblank interrupt events
577 * @crtc: which irq to enable
579 * Enable vblank interrupts for @crtc. If the device doesn't have
580 * a hardware vblank counter, this routine should be a no-op, since
581 * interrupts will have to stay on to keep the count accurate.
584 * Zero on success, appropriate errno if the given @crtc's vblank
585 * interrupt cannot be enabled.
587 int (*enable_vblank) (struct drm_device *dev, int crtc);
590 * disable_vblank - disable vblank interrupt events
592 * @crtc: which irq to enable
594 * Disable vblank interrupts for @crtc. If the device doesn't have
595 * a hardware vblank counter, this routine should be a no-op, since
596 * interrupts will have to stay on to keep the count accurate.
598 void (*disable_vblank) (struct drm_device *dev, int crtc);
601 * Called by \c drm_device_is_agp. Typically used to determine if a
602 * card is really attached to AGP or not.
604 * \param dev DRM device handle
607 * One of three values is returned depending on whether or not the
608 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
609 * (return of 1), or may or may not be AGP (return of 2).
611 int (*device_is_agp) (struct drm_device *dev);
614 * Called by vblank timestamping code.
616 * Return the current display scanout position from a crtc, and an
617 * optional accurate ktime_get timestamp of when position was measured.
619 * \param dev DRM device.
620 * \param crtc Id of the crtc to query.
621 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
622 * \param *vpos Target location for current vertical scanout position.
623 * \param *hpos Target location for current horizontal scanout position.
624 * \param *stime Target location for timestamp taken immediately before
625 * scanout position query. Can be NULL to skip timestamp.
626 * \param *etime Target location for timestamp taken immediately after
627 * scanout position query. Can be NULL to skip timestamp.
629 * Returns vpos as a positive number while in active scanout area.
630 * Returns vpos as a negative number inside vblank, counting the number
631 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
632 * until start of active scanout / end of vblank."
634 * \return Flags, or'ed together as follows:
636 * DRM_SCANOUTPOS_VALID = Query successful.
637 * DRM_SCANOUTPOS_INVBL = Inside vblank.
638 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
639 * this flag means that returned position may be offset by a constant
640 * but unknown small number of scanlines wrt. real scanout position.
643 int (*get_scanout_position) (struct drm_device *dev, int crtc,
645 int *vpos, int *hpos, ktime_t *stime,
649 * Called by \c drm_get_last_vbltimestamp. Should return a precise
650 * timestamp when the most recent VBLANK interval ended or will end.
652 * Specifically, the timestamp in @vblank_time should correspond as
653 * closely as possible to the time when the first video scanline of
654 * the video frame after the end of VBLANK will start scanning out,
655 * the time immediately after end of the VBLANK interval. If the
656 * @crtc is currently inside VBLANK, this will be a time in the future.
657 * If the @crtc is currently scanning out a frame, this will be the
658 * past start time of the current scanout. This is meant to adhere
659 * to the OpenML OML_sync_control extension specification.
661 * \param dev dev DRM device handle.
662 * \param crtc crtc for which timestamp should be returned.
663 * \param *max_error Maximum allowable timestamp error in nanoseconds.
664 * Implementation should strive to provide timestamp
665 * with an error of at most *max_error nanoseconds.
666 * Returns true upper bound on error for timestamp.
667 * \param *vblank_time Target location for returned vblank timestamp.
668 * \param flags 0 = Defaults, no special treatment needed.
669 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
670 * irq handler. Some drivers need to apply some workarounds
671 * for gpu-specific vblank irq quirks if flag is set.
674 * Zero if timestamping isn't supported in current display mode or a
675 * negative number on failure. A positive status code on success,
676 * which describes how the vblank_time timestamp was computed.
678 int (*get_vblank_timestamp) (struct drm_device *dev, int crtc,
680 struct timeval *vblank_time,
683 /* these have to be filled in */
685 void (*irq_handler) (void *arg);
686 void (*irq_preinstall) (struct drm_device *dev);
687 int (*irq_postinstall) (struct drm_device *dev);
688 void (*irq_uninstall) (struct drm_device *dev);
691 * Driver-specific constructor for drm_gem_objects, to set up
692 * obj->driver_private.
694 * Returns 0 on success.
696 void (*gem_free_object) (struct drm_gem_object *obj);
697 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
698 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
700 int (*sysctl_init) (struct drm_device *dev,
701 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
702 void (*sysctl_cleanup) (struct drm_device *dev);
704 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
706 /* dumb alloc support */
707 int (*dumb_create)(struct drm_file *file_priv,
708 struct drm_device *dev,
709 struct drm_mode_create_dumb *args);
710 int (*dumb_map_offset)(struct drm_file *file_priv,
711 struct drm_device *dev, uint32_t handle,
713 int (*dumb_destroy)(struct drm_file *file_priv,
714 struct drm_device *dev,
717 /* Driver private ops for this object */
718 struct cdev_pager_ops *gem_pager_ops;
723 const char *name; /* Simple driver name */
724 const char *desc; /* Longer driver name */
725 const char *date; /* Date of last major changes. */
729 const struct drm_ioctl_desc *ioctls;
734 * Info file list entry. This structure represents a debugfs or proc file to
735 * be created by the drm core
737 struct drm_info_list {
738 const char *name; /** file name */
739 int (*show)(struct seq_file*, void*); /** show callback */
740 u32 driver_features; /**< Required driver features for this entry */
745 * debugfs node structure. This structure represents a debugfs file.
747 struct drm_info_node {
748 struct list_head list;
749 struct drm_minor *minor;
750 const struct drm_info_list *info_ent;
755 * DRM minor structure. This structure represents a drm minor number.
758 int index; /**< Minor device number */
759 int type; /**< Control or render */
760 device_t kdev; /**< OS device */
761 struct drm_device *dev;
763 struct drm_master *master; /* currently active master for this node */
764 struct list_head master_list;
765 struct drm_mode_group mode_group;
768 struct drm_pending_vblank_event {
769 struct drm_pending_event base;
771 struct drm_event_vblank event;
774 struct drm_sysctl_info {
775 struct sysctl_ctx_list ctx;
779 /* Length for the array of resource pointers for drm_get_resource_*. */
780 #define DRM_MAX_PCI_RESOURCE 6
782 struct drm_vblank_crtc {
783 struct drm_device *dev; /* pointer to the drm_device */
784 wait_queue_head_t queue; /**< VBLANK wait queue */
785 struct timer_list disable_timer; /* delayed disable timer */
787 /* vblank counter, protected by dev->vblank_time_lock for writes */
789 /* vblank timestamps, protected by dev->vblank_time_lock for writes */
790 struct timeval time[DRM_VBLANKTIME_RBSIZE];
792 atomic_t refcount; /* number of users of vblank interruptsper crtc */
793 u32 last; /* protected by dev->vbl_lock, used */
794 /* for wraparound handling */
795 u32 last_wait; /* Last vblank seqno waited per CRTC */
796 unsigned int inmodeset; /* Display driver is setting mode */
797 unsigned int pipe; /* crtc index */
798 bool enabled; /* so we don't call enable more than
803 * DRM device structure. This structure represent a complete card that
804 * may contain multiple heads.
807 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
809 char *unique; /* Unique identifier: e.g., busid */
810 int unique_len; /* Length of unique field */
811 struct cdev *devnode; /* Device number for mknod */
812 int if_version; /* Highest interface version set */
814 int flags; /* Flags to open(2) */
817 struct spinlock dma_lock; /* protects dev->dma */
818 struct lwkt_serialize irq_lock; /* protects irq condition checks */
819 struct lock dev_lock; /* protects everything else */
823 struct lock struct_mutex; /**< For others */
824 struct lock master_mutex; /**< For drm_minor::master */
827 /** \name Usage Counters */
829 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
830 struct spinlock buf_lock; /**< For drm_device::buf_use and a few other things. */
831 int buf_use; /**< Buffers in use -- cannot alloc */
832 atomic_t buf_alloc; /**< Buffer allocation in progress */
836 /** \name Performance counters */
838 unsigned long counters;
839 enum drm_stat_type types[15];
844 struct drm_open_hash magiclist; /**< magic hash table */
845 struct list_head magicfree;
847 struct list_head filelist;
849 /** \name Memory management */
851 struct list_head maplist; /**< Linked list of regions */
852 int map_count; /**< Number of mappable regions */
853 struct drm_open_hash map_hash; /**< User token hash table for maps */
855 /** \name Context handle management */
857 struct list_head ctxlist; /**< Linked list of context handles */
858 int ctx_count; /**< Number of context handles */
859 struct lock ctxlist_mutex; /**< For ctxlist */
865 struct drm_lock_data lock; /* Information on hardware lock */
867 /** \name DMA support */
869 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
872 int irq_type; /* IRQ type (MSI enabled or not) */
873 int irqrid; /* Interrupt used by board */
874 struct resource *irqr; /* Resource for interrupt used by board */
875 void *irqh; /* Handle from bus_setup_intr */
877 /* Storage of resource pointers for drm_get_resource_* */
878 struct resource *pcir[DRM_MAX_PCI_RESOURCE];
879 int pcirid[DRM_MAX_PCI_RESOURCE];
886 /** \name Context support */
889 __volatile__ long context_flag; /**< Context swapping flag */
890 __volatile__ long interrupt_flag; /**< Interruption handler flag */
891 __volatile__ long dma_flag; /**< DMA dispatch flag */
892 wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
893 int last_checked; /**< Last context checked for DMA */
894 int last_context; /**< Last current context */
895 unsigned long last_switch; /**< jiffies at last context switch */
898 /** \name VBLANK IRQ support */
900 int irq_enabled; /**< True if irq handler is enabled */
901 int irq; /* Interrupt used by board */
904 * At load time, disabling the vblank interrupt won't be allowed since
905 * old clients may not call the modeset ioctl and therefore misbehave.
906 * Once the modeset ioctl *has* been called though, we can safely
907 * disable them when unused.
909 bool vblank_disable_allowed;
912 * If true, vblank interrupt will be disabled immediately when the
913 * refcount drops to zero, as opposed to via the vblank disable
915 * This can be set to true it the hardware has a working vblank
916 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
919 bool vblank_disable_immediate;
921 /* array of size num_crtcs */
922 struct drm_vblank_crtc *vblank;
924 struct lock vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
925 struct lock vbl_lock;
926 struct timer_list vblank_disable_timer;
928 u32 max_vblank_count; /**< size of vblank counter register */
933 struct list_head vblank_event_list;
934 struct lock event_lock;
938 struct sigio *buf_sigio; /* Processes waiting for SIGIO */
941 struct drm_sysctl_info *sysctl;
944 struct drm_sg_mem *sg; /**< Scatter gather memory */
945 unsigned int num_crtcs; /**< Number of CRTCs on this device */
947 unsigned long *ctx_bitmap;
954 struct drm_agp_head *agp; /**< AGP data */
956 struct device *dev; /**< Device structure */
957 struct pci_dev *pdev; /**< PCI device structure */
959 struct drm_driver *driver;
960 struct drm_local_map *agp_buffer_map;
961 unsigned int agp_buffer_token;
962 struct drm_minor *control; /**< Control node for card */
963 struct drm_minor *primary; /**< render type primary screen head */
965 struct drm_mode_config mode_config; /**< Current mode config */
967 /** \name GEM information */
969 struct lock object_name_lock;
970 struct idr object_name_idr;
974 void *sysctl_private;
978 int switch_power_state;
980 atomic_t unplugged; /* device has been unplugged or gone away */
983 #define DRM_SWITCH_POWER_ON 0
984 #define DRM_SWITCH_POWER_OFF 1
985 #define DRM_SWITCH_POWER_CHANGING 2
986 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
988 static __inline__ int drm_core_check_feature(struct drm_device *dev,
991 return ((dev->driver->driver_features & feature) ? 1 : 0);
994 static inline void drm_device_set_unplugged(struct drm_device *dev)
997 atomic_set(&dev->unplugged, 1);
1000 static inline int drm_device_is_unplugged(struct drm_device *dev)
1002 int ret = atomic_read(&dev->unplugged);
1007 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1009 return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1013 * drm_is_master() - Check whether a DRM open-file is DRM-Master
1014 * @file: DRM open-file context
1016 * This checks whether a DRM open-file context is owner of the master context
1017 * attached to it. If a file owns a master context, it's called DRM-Master.
1018 * Per DRM device, only one such file can be DRM-Master at a time.
1020 * Returns: True if the file is DRM-Master, otherwise false.
1022 static inline bool drm_is_master(const struct drm_file *file)
1025 return file->master && file->master == file->minor->master;
1031 /******************************************************************/
1032 /** \name Internal function definitions */
1036 static inline int drm_core_has_AGP(struct drm_device *dev)
1038 return drm_core_check_feature(dev, DRIVER_USE_AGP);
1041 #define drm_core_has_AGP(dev) (0)
1044 extern unsigned int drm_rnodes;
1045 extern unsigned int drm_universal_planes;
1047 extern int drm_vblank_offdelay;
1048 extern unsigned int drm_timestamp_precision;
1050 /* Driver support (drm_drv.h) */
1051 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1052 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1053 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1054 int drm_create_cdevs(device_t kdev);
1055 d_ioctl_t drm_ioctl;
1056 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1058 /* Device support (drm_fops.h) */
1060 d_close_t drm_close;
1062 d_kqfilter_t drm_kqfilter;
1063 int drm_release(device_t kdev);
1066 d_mmap_single_t drm_mmap_single;
1068 void drm_cdevpriv_dtor(void *cd);
1070 int drm_add_busid_modesetting(struct drm_device *dev,
1071 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1073 /* File operations helpers (drm_fops.c) */
1074 extern int drm_open_helper(struct cdev *kdev, int flags, int fmt,
1076 struct drm_device *dev,
1078 extern struct drm_file *drm_find_file_by_proc(struct drm_device *dev,
1082 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1083 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1085 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1086 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1088 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1089 struct drm_file *file_priv);
1090 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1091 struct drm_file *file_priv);
1093 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1094 dma_addr_t *addrs, int max_pages);
1095 #endif /* DUMBBELL_WIP */
1096 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1097 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1100 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1101 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1102 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1103 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1104 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1106 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1107 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1108 struct drm_gem_object **obj);
1109 #endif /* DUMBBELL_WIP */
1111 int drm_mtrr_add(unsigned long offset, size_t size, int flags);
1112 int drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1115 * These are exported to drivers so that they can implement fencing using
1116 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1119 unsigned long drm_get_resource_start(struct drm_device *dev,
1120 unsigned int resource);
1121 unsigned long drm_get_resource_len(struct drm_device *dev,
1122 unsigned int resource);
1124 /* IRQ support (drm_irq.h) */
1125 extern int drm_irq_install(struct drm_device *dev, int irq);
1126 extern int drm_irq_uninstall(struct drm_device *dev);
1128 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
1129 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1130 struct drm_file *filp);
1131 extern u32 drm_vblank_count(struct drm_device *dev, int pipe);
1132 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1133 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1134 struct timeval *vblanktime);
1135 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1136 struct drm_pending_vblank_event *e);
1137 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1138 struct drm_pending_vblank_event *e);
1139 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
1140 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1141 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
1142 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
1143 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1144 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1145 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
1146 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1147 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
1148 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
1149 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1150 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1151 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1152 extern void drm_vblank_cleanup(struct drm_device *dev);
1154 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1155 unsigned int pipe, int *max_error,
1156 struct timeval *vblank_time,
1158 const struct drm_crtc *refcrtc,
1159 const struct drm_display_mode *mode);
1160 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1161 const struct drm_display_mode *mode);
1164 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1165 * @crtc: which CRTC's vblank waitqueue to retrieve
1167 * This function returns a pointer to the vblank waitqueue for the CRTC.
1168 * Drivers can use this to implement vblank waits using wait_event() & co.
1170 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1172 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1175 /* Modesetting support */
1176 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1177 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1179 /* sysctl support (drm_sysctl.h) */
1180 extern int drm_sysctl_init(struct drm_device *dev);
1181 extern int drm_sysctl_cleanup(struct drm_device *dev);
1183 /* Locking IOCTL support (drm_drv.c) */
1184 int drm_lock(struct drm_device *dev, void *data,
1185 struct drm_file *file_priv);
1186 int drm_unlock(struct drm_device *dev, void *data,
1187 struct drm_file *file_priv);
1188 int drm_setversion(struct drm_device *dev, void *data,
1189 struct drm_file *file_priv);
1192 * These are exported to drivers so that they can implement fencing using
1193 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1196 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1198 /* Misc. IOCTL support (drm_ioctl.c) */
1199 int drm_noop(struct drm_device *dev, void *data,
1200 struct drm_file *file_priv);
1202 /* Cache management (drm_cache.c) */
1203 void drm_clflush_pages(struct vm_page *pages[], unsigned long num_pages);
1204 void drm_clflush_sg(struct sg_table *st);
1205 void drm_clflush_virt_range(void *addr, unsigned long length);
1207 /* DMA support (drm_dma.c) */
1208 int drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1210 /* Stub support (drm_stub.h) */
1212 extern void drm_put_dev(struct drm_device *dev);
1213 extern void drm_unplug_dev(struct drm_device *dev);
1214 extern unsigned int drm_debug;
1215 extern bool drm_atomic;
1217 /* Scatter Gather Support (drm_scatter.c) */
1218 int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1219 struct drm_file *file_priv);
1220 int drm_sg_free(struct drm_device *dev, void *data,
1221 struct drm_file *file_priv);
1223 /* consistent PCI memory functions (drm_pci.c) */
1224 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1226 void drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah);
1228 /* sysfs support (drm_sysfs.c) */
1229 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1231 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1232 vm_size_t size, struct vm_object **obj_res, int nprot);
1233 void drm_gem_pager_dtr(void *obj);
1235 struct ttm_bo_device;
1236 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1237 vm_size_t size, struct vm_object **obj_res, int nprot);
1238 struct ttm_buffer_object;
1239 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1241 void drm_device_lock_mtx(struct drm_device *dev);
1242 void drm_device_unlock_mtx(struct drm_device *dev);
1243 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1244 const char *msg, int timeout);
1245 void drm_device_assert_mtx_locked(struct drm_device *dev);
1246 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1248 void drm_device_lock_struct(struct drm_device *dev);
1249 void drm_device_unlock_struct(struct drm_device *dev);
1250 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1251 const char *msg, int timeout);
1252 void drm_device_assert_struct_locked(struct drm_device *dev);
1253 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1255 void drm_compat_locking_init(struct drm_device *dev);
1256 void drm_sleep_locking_init(struct drm_device *dev);
1258 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1259 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1260 void drm_fini_pdev(struct pci_dev **pdev);
1262 /* Inline replacements for drm_alloc and friends */
1263 static __inline__ void *
1264 drm_alloc(size_t size, struct malloc_type *area)
1266 return kmalloc(size, area, M_WAITOK | M_NULLOK);
1269 static __inline__ void *
1270 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1272 return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1275 static __inline__ void
1276 drm_free(void *pt, struct malloc_type *area)
1278 /* kfree is special!!! */
1283 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1284 struct device *parent);
1285 void drm_dev_ref(struct drm_device *dev);
1286 void drm_dev_unref(struct drm_device *dev);
1287 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1288 void drm_dev_unregister(struct drm_device *dev);
1289 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1291 /* FreeBSD compatibility macros */
1292 #define VM_OBJECT_WLOCK(object) VM_OBJECT_LOCK(object)
1293 #define VM_OBJECT_WUNLOCK(object) VM_OBJECT_UNLOCK(object)
1296 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1298 if (dev->driver->device_is_agp != NULL) {
1299 int err = (*dev->driver->device_is_agp) (dev);
1306 return (pci_find_extcap(dev->dev, PCIY_AGP, NULL) == 0);
1309 #define DRM_PCIE_SPEED_25 1
1310 #define DRM_PCIE_SPEED_50 2
1311 #define DRM_PCIE_SPEED_80 4
1313 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1316 #define drm_can_sleep() (HZ & 1)
1318 #endif /* __KERNEL__ */
1319 #endif /* _DRM_P_H_ */