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__)
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 #include <sys/sglist.h>
52 #include <sys/spinlock.h>
53 #include <sys/spinlock2.h>
54 #include <sys/fcntl.h>
56 #include <sys/filio.h>
57 #include <sys/sysctl.h>
59 #include <sys/signalvar.h>
61 #include <linux/highmem.h>
63 #include <sys/taskqueue.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_map.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page2.h>
72 #include <vm/vm_pager.h>
73 #include <vm/vm_param.h>
74 #include <machine/param.h>
75 #include <machine/pmap.h>
77 #include <machine/specialreg.h>
79 #include <machine/sysarch.h>
80 #include <sys/endian.h>
83 #include <sys/memrange.h>
84 #include <sys/mutex.h>
86 #include <linux/agp_backend.h>
87 #include <linux/atomic.h>
88 #include <linux/bug.h>
89 #include <linux/dma-mapping.h>
90 #include <linux/capability.h>
91 #include <linux/err.h>
92 #include <linux/idr.h>
93 #include <linux/pci.h>
94 #include <linux/jiffies.h>
95 #include <linux/kernel.h>
97 #include <linux/kref.h>
98 #include <linux/list.h>
100 #include <linux/moduleparam.h>
101 #include <linux/mutex.h>
102 #include <linux/slab.h>
103 #include <linux/scatterlist.h>
104 #include <linux/timer.h>
106 #include <linux/seq_file.h>
107 #include <linux/types.h>
108 #include <linux/vmalloc.h>
109 #include <linux/wait.h>
110 #include <linux/workqueue.h>
112 #include <asm/uaccess.h>
114 #include <uapi_drm/drm.h>
115 #include <uapi_drm/drm_mode.h>
117 #include <drm/drm_agpsupport.h>
118 #include <drm/drm_crtc.h>
119 #include <drm/drm_global.h>
120 #include <drm/drm_hashtab.h>
121 #include <drm/drm_mem_util.h>
122 #include <drm/drm_mm.h>
123 #include <drm/drm_os_linux.h>
124 #include <uapi_drm/drm_sarea.h>
125 #include <drm/drm_vma_manager.h>
130 struct drm_local_map
;
131 struct drm_device_dma
;
132 struct drm_dma_handle
;
133 struct drm_gem_object
;
136 #ifdef CONFIG_VIDEOMODE_HELPERS
137 struct videomode
; /* XXX empty struct in videomode.h ? */
139 struct reservation_object
;
140 struct dma_buf_attachment
;
143 * 4 debug categories are defined:
145 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
146 * This is the category used by the DRM_DEBUG() macro.
148 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
149 * This is the category used by the DRM_DEBUG_DRIVER() macro.
151 * KMS: used in the modesetting code.
152 * This is the category used by the DRM_DEBUG_KMS() macro.
154 * PRIME: used in the prime code.
155 * This is the category used by the DRM_DEBUG_PRIME() macro.
157 * ATOMIC: used in the atomic code.
158 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
161 * PID: used as modifier to include PID number in messages.
162 * This is the category used by the all debug macros.
164 * FIOCTL: used in failed ioctl debugging.
165 * This is the category used by the DRM_DEBUG_FIOCTL() macro.
167 * IOCTL: used in ioctl debugging.
168 * This is the category used by the DRM_DEBUG_IOCTL() macro.
170 * VBLANK: used in vblank debugging.
171 * This is the category used by the DRM_DEBUG_VBLANK() macro.
173 * Enabling verbose debug messages is done through the drm.debug parameter,
174 * each category being enabled by a bit.
176 * drm.debug=0x1 will enable CORE messages
177 * drm.debug=0x2 will enable DRIVER messages
178 * drm.debug=0x3 will enable CORE and DRIVER messages
180 * drm.debug=0xf will enable all messages
182 * An interesting feature is that it's possible to enable verbose logging at
183 * run-time by using hw.drm.debug sysctl variable:
184 * # sysctl hw.drm.debug=0xfff
186 #define DRM_UT_CORE 0x01
187 #define DRM_UT_DRIVER 0x02
188 #define DRM_UT_KMS 0x04
189 #define DRM_UT_PRIME 0x08
190 #define DRM_UT_ATOMIC 0x10
191 #define DRM_UT_VBL 0x20
192 /* Extra DragonFly debug categories */
194 #define DRM_UT_RES7 0x40 /* reserved for future expansion */
195 #define DRM_UT_RES8 0x80 /* reserved for future expansion */
196 #define DRM_UT_PID 0x100
197 #define DRM_UT_FIOCTL 0x200
198 #define DRM_UT_IOCTL 0x400
199 #define DRM_UT_VBLANK 0x800
202 extern __printflike(2, 3)
203 void drm_ut_debug_printk(const char *function_name
,
204 const char *format
, ...);
205 extern __printflike(2, 3)
206 void drm_err(const char *func
, const char *format
, ...);
208 /***********************************************************************/
209 /** \name DRM template customization defaults */
212 /* driver capabilities and requirements mask */
213 #define DRIVER_USE_AGP 0x1
214 #define DRIVER_PCI_DMA 0x8
215 #define DRIVER_SG 0x10
216 #define DRIVER_HAVE_DMA 0x20
217 #define DRIVER_HAVE_IRQ 0x40
218 #define DRIVER_IRQ_SHARED 0x80
219 #define DRIVER_GEM 0x1000
220 #define DRIVER_MODESET 0x2000
221 #define DRIVER_PRIME 0x4000
222 #define DRIVER_RENDER 0x8000
223 #define DRIVER_ATOMIC 0x10000
224 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
226 #define DRM_MAGIC_HASH_ORDER 4 /**< Size of key hash table. Must be power of 2. */
228 /***********************************************************************/
229 /** \name Macros to make printk easier */
235 * \param fmt printf() like format string.
236 * \param arg arguments
238 #define DRM_ERROR(fmt, ...) \
239 drm_err(__func__, fmt, ##__VA_ARGS__)
242 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
244 * \param fmt printf() like format string.
245 * \param arg arguments
247 #define DRM_ERROR_RATELIMITED(fmt, ...) \
249 static struct krate krate_derr = { .freq = 1, .count = -16 }; \
251 krateprintf(&krate_derr, "error: [" DRM_NAME ":%s] *ERROR* " \
252 fmt, __func__, ##__VA_ARGS__); \
255 #define DRM_INFO(fmt, ...) \
256 printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
258 #define DRM_INFO_ONCE(fmt, ...) \
259 printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
264 * \param fmt printf() like format string.
265 * \param arg arguments
267 #define DRM_DEBUG(fmt, args...) \
269 if (unlikely(drm_debug & DRM_UT_CORE)) \
270 drm_ut_debug_printk(__func__, fmt, ##args); \
273 #define DRM_DEBUG_DRIVER(fmt, args...) \
275 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
276 drm_ut_debug_printk(__func__, fmt, ##args); \
278 #define DRM_DEBUG_KMS(fmt, args...) \
280 if (unlikely(drm_debug & DRM_UT_KMS)) \
281 drm_ut_debug_printk(__func__, fmt, ##args); \
283 #define DRM_DEBUG_PRIME(fmt, args...) \
285 if (unlikely(drm_debug & DRM_UT_PRIME)) \
286 drm_ut_debug_printk(__func__, fmt, ##args); \
288 #define DRM_DEBUG_ATOMIC(fmt, args...) \
290 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
291 drm_ut_debug_printk(__func__, fmt, ##args); \
295 #define DRM_DEBUG_FIOCTL(fmt, args...) \
297 if (unlikely(drm_debug & DRM_UT_FIOCTL)) \
298 drm_ut_debug_printk(__func__, fmt, ##args); \
300 #define DRM_DEBUG_IOCTL(fmt, args...) \
302 if (unlikely(drm_debug & DRM_UT_IOCTL)) \
303 drm_ut_debug_printk(__func__, fmt, ##args); \
305 #define DRM_DEBUG_VBLANK(fmt, args...) \
307 if (unlikely(drm_debug & DRM_UT_VBLANK)) \
308 drm_ut_debug_printk(__func__, fmt, ##args); \
310 #define DRM_DEBUG_VBL(fmt, args...) \
312 if (unlikely(drm_debug & DRM_UT_VBL)) \
313 drm_ut_debug_printk(__func__, fmt, ##args); \
319 /***********************************************************************/
320 /** \name Internal types and structures */
323 SYSCTL_DECL(_hw_drm
);
325 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
327 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
329 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
330 #define DRM_DEV_UID UID_ROOT
331 #define DRM_DEV_GID GID_WHEEL
333 #define DRM_CURPROC curthread
334 #define DRM_STRUCTPROC struct thread
335 #define DRM_LOCK(dev) lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
336 #define DRM_UNLOCK(dev) lockmgr(&(dev)->struct_mutex, LK_RELEASE)
337 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout) \
338 (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
339 #if defined(INVARIANTS)
340 #define DRM_LOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
341 #define DRM_UNLOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
343 #define DRM_LOCK_ASSERT(d)
344 #define DRM_UNLOCK_ASSERT(d)
347 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
349 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
351 #define DRM_MTRR_WC MDF_WRITECOMBINE
353 int vm_phys_fictitious_reg_range(vm_paddr_t start
, vm_paddr_t end
,
354 vm_memattr_t memattr
);
355 void vm_phys_fictitious_unreg_range(vm_paddr_t start
, vm_paddr_t end
);
356 vm_page_t
vm_phys_fictitious_to_vm_page(vm_paddr_t pa
);
359 int drm_mtrr_add(unsigned long offset
, size_t size
, int flags
);
360 int drm_mtrr_del(int handle
, unsigned long offset
, size_t size
, int flags
);
362 typedef struct drm_pci_id_list
371 * Ioctl function type.
373 * \param inode device inode.
374 * \param file_priv DRM file private pointer.
375 * \param cmd command.
376 * \param arg argument.
378 typedef int drm_ioctl_t(struct drm_device
*dev
, void *data
,
379 struct drm_file
*file_priv
);
381 typedef int drm_ioctl_compat_t(struct file
*filp
, unsigned int cmd
,
384 #define DRM_IOCTL_NR(n) ((n) & 0xff)
387 #define DRM_MASTER 0x2
388 #define DRM_ROOT_ONLY 0x4
389 #define DRM_CONTROL_ALLOW 0x8
390 #define DRM_UNLOCKED 0x10
391 #define DRM_RENDER_ALLOW 0x20
393 struct drm_ioctl_desc
{
397 unsigned int cmd_drv
;
402 * Creates a driver or general drm_ioctl_desc array entry for the given
403 * ioctl, for use by drm_ioctl().
406 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
407 [DRM_IOCTL_NR(DRM_##ioctl)] = { \
408 .cmd = DRM_##ioctl, \
411 .cmd_drv = DRM_IOCTL_##ioctl, \
415 typedef struct drm_magic_entry
{
416 struct list_head head
;
417 struct drm_hash_item hash_item
;
418 struct drm_file
*priv
;
419 struct drm_magic_entry
*next
;
422 /* Event queued up for userspace to read */
423 struct drm_pending_event
{
424 struct drm_event
*event
;
425 struct list_head link
;
426 struct list_head pending_link
;
427 struct drm_file
*file_priv
;
428 pid_t pid
; /* pid of requester, no guarantee it's valid by the time
429 we deliver the event, for tracing only */
430 void (*destroy
)(struct drm_pending_event
*event
);
433 /* initial implementaton using a linked list - todo hashtab */
434 struct drm_prime_file_private
{
435 struct list_head head
;
439 /** File private data */
441 unsigned authenticated
:1;
442 /* Whether we're master for a minor. Protected by master_mutex */
443 unsigned is_master
:1;
445 /* true when the client has asked us to expose stereo 3D mode flags */
446 unsigned stereo_allowed
:1;
448 * true if client understands CRTC primary planes and cursor planes
451 unsigned universal_planes
:1;
452 /* true if client understands atomic properties */
458 struct list_head lhead
;
459 unsigned long lock_count
;
463 /** Mapping of mm object handles to object pointers. */
464 struct idr object_idr
;
465 /** Lock for synchronization of access to object_idr. */
466 struct lock table_lock
;
471 struct drm_master
*masterp
;
474 * fbs - List of framebuffers associated with this file.
476 * Protected by fbs_lock. Note that the fbs list holds a reference on
477 * the fb object to prevent it from untimely disappearing.
479 struct list_head fbs
;
480 struct lock fbs_lock
;
482 /** User-created blob properties; this retains a reference on the
484 struct list_head blobs
;
486 wait_queue_head_t event_wait
;
487 struct list_head pending_event_list
;
488 struct list_head event_list
;
491 struct lock event_read_lock
;
493 struct drm_prime_file_private prime
;
496 struct drm_device
*dev
;
498 unsigned long ioctl_count
;
505 struct drm_lock_data
{
506 struct drm_hw_lock
*hw_lock
; /**< Hardware lock */
507 /** Private of lock holder's file (NULL=kernel) */
508 struct drm_file
*file_priv
;
509 wait_queue_head_t lock_queue
; /**< Queue of blocked processes */
510 unsigned long lock_time
; /**< Time of last lock in jiffies */
514 * GEM specific mm private for tracking GEM objects
517 struct drm_vma_offset_manager vma_manager
;
518 struct drm_mm offset_manager
; /**< Offset mgmt for buffer objects */
519 struct drm_open_hash offset_hash
; /**< User token hash table for maps */
520 struct unrhdr
*idxunr
;
524 * struct drm_master - drm master structure
526 * @refcount: Refcount for this master object.
527 * @minor: Link back to minor char device we are master for. Immutable.
528 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
529 * @unique_len: Length of unique field. Protected by drm_global_mutex.
530 * @magic_map: Map of used authentication tokens. Protected by struct_mutex.
531 * @lock: DRI lock information.
532 * @driver_priv: Pointer to driver-private information.
535 struct kref refcount
; /* refcount for this master */
536 struct list_head head
; /**< each minor contains a list of masters */
537 struct drm_minor
*minor
; /**< link back to minor we are a master for */
538 char *unique
; /**< Unique identifier: e.g., busid */
539 int unique_len
; /**< Length of unique field */
540 int unique_size
; /**< amount allocated */
541 int blocked
; /**< Blocked due to VC switch? */
542 struct idr magic_map
;
543 struct drm_lock_data lock
;
547 /* Size of ringbuffer for vblank timestamps. Just double-buffer
548 * in initial implementation.
550 #define DRM_VBLANKTIME_RBSIZE 2
552 /* Flags and return codes for get_vblank_timestamp() driver function. */
553 #define DRM_CALLED_FROM_VBLIRQ 1
554 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
555 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
557 /* get_scanout_position() return flags */
558 #define DRM_SCANOUTPOS_VALID (1 << 0)
559 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
560 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
563 * DRM driver structure. This structure represent the common code for
564 * a family of cards. There will one drm_device for each card present
568 int (*load
) (struct drm_device
*, unsigned long flags
);
569 int (*firstopen
) (struct drm_device
*);
570 int (*open
) (struct drm_device
*, struct drm_file
*);
571 void (*preclose
) (struct drm_device
*, struct drm_file
*file_priv
);
572 void (*postclose
) (struct drm_device
*, struct drm_file
*);
573 void (*lastclose
) (struct drm_device
*);
574 int (*unload
) (struct drm_device
*);
575 int (*suspend
) (struct drm_device
*, pm_message_t state
);
576 int (*resume
) (struct drm_device
*);
577 int (*dma_ioctl
) (struct drm_device
*dev
, void *data
, struct drm_file
*file_priv
);
578 int (*dma_quiescent
) (struct drm_device
*);
579 int (*context_dtor
) (struct drm_device
*dev
, int context
);
580 int (*set_busid
)(struct drm_device
*dev
, struct drm_master
*master
);
583 * get_vblank_counter - get raw hardware vblank counter
585 * @pipe: counter to fetch
587 * Driver callback for fetching a raw hardware vblank counter for @crtc.
588 * If a device doesn't have a hardware counter, the driver can simply
589 * return the value of drm_vblank_count. The DRM core will account for
590 * missed vblank events while interrupts where disabled based on system
593 * Wraparound handling and loss of events due to modesetting is dealt
594 * with in the DRM core code.
597 * Raw vblank counter value.
599 u32 (*get_vblank_counter
) (struct drm_device
*dev
, unsigned int pipe
);
602 * enable_vblank - enable vblank interrupt events
604 * @pipe: which irq to enable
606 * Enable vblank interrupts for @crtc. If the device doesn't have
607 * a hardware vblank counter, this routine should be a no-op, since
608 * interrupts will have to stay on to keep the count accurate.
611 * Zero on success, appropriate errno if the given @crtc's vblank
612 * interrupt cannot be enabled.
614 int (*enable_vblank
) (struct drm_device
*dev
, unsigned int pipe
);
617 * disable_vblank - disable vblank interrupt events
619 * @pipe: which irq to enable
621 * Disable vblank interrupts for @crtc. If the device doesn't have
622 * a hardware vblank counter, this routine should be a no-op, since
623 * interrupts will have to stay on to keep the count accurate.
625 void (*disable_vblank
) (struct drm_device
*dev
, unsigned int pipe
);
628 * Called by \c drm_device_is_agp. Typically used to determine if a
629 * card is really attached to AGP or not.
631 * \param dev DRM device handle
634 * One of three values is returned depending on whether or not the
635 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
636 * (return of 1), or may or may not be AGP (return of 2).
638 int (*device_is_agp
) (struct drm_device
*dev
);
641 * Called by vblank timestamping code.
643 * Return the current display scanout position from a crtc, and an
644 * optional accurate ktime_get timestamp of when position was measured.
646 * \param dev DRM device.
647 * \param pipe Id of the crtc to query.
648 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
649 * \param *vpos Target location for current vertical scanout position.
650 * \param *hpos Target location for current horizontal scanout position.
651 * \param *stime Target location for timestamp taken immediately before
652 * scanout position query. Can be NULL to skip timestamp.
653 * \param *etime Target location for timestamp taken immediately after
654 * scanout position query. Can be NULL to skip timestamp.
655 * \param mode Current display timings.
657 * Returns vpos as a positive number while in active scanout area.
658 * Returns vpos as a negative number inside vblank, counting the number
659 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
660 * until start of active scanout / end of vblank."
662 * \return Flags, or'ed together as follows:
664 * DRM_SCANOUTPOS_VALID = Query successful.
665 * DRM_SCANOUTPOS_INVBL = Inside vblank.
666 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
667 * this flag means that returned position may be offset by a constant
668 * but unknown small number of scanlines wrt. real scanout position.
671 int (*get_scanout_position
) (struct drm_device
*dev
, unsigned int pipe
,
672 unsigned int flags
, int *vpos
, int *hpos
,
673 ktime_t
*stime
, ktime_t
*etime
,
674 const struct drm_display_mode
*mode
);
677 * Called by \c drm_get_last_vbltimestamp. Should return a precise
678 * timestamp when the most recent VBLANK interval ended or will end.
680 * Specifically, the timestamp in @vblank_time should correspond as
681 * closely as possible to the time when the first video scanline of
682 * the video frame after the end of VBLANK will start scanning out,
683 * the time immediately after end of the VBLANK interval. If the
684 * @crtc is currently inside VBLANK, this will be a time in the future.
685 * If the @crtc is currently scanning out a frame, this will be the
686 * past start time of the current scanout. This is meant to adhere
687 * to the OpenML OML_sync_control extension specification.
689 * \param dev dev DRM device handle.
690 * \param pipe crtc for which timestamp should be returned.
691 * \param *max_error Maximum allowable timestamp error in nanoseconds.
692 * Implementation should strive to provide timestamp
693 * with an error of at most *max_error nanoseconds.
694 * Returns true upper bound on error for timestamp.
695 * \param *vblank_time Target location for returned vblank timestamp.
696 * \param flags 0 = Defaults, no special treatment needed.
697 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
698 * irq handler. Some drivers need to apply some workarounds
699 * for gpu-specific vblank irq quirks if flag is set.
702 * Zero if timestamping isn't supported in current display mode or a
703 * negative number on failure. A positive status code on success,
704 * which describes how the vblank_time timestamp was computed.
706 int (*get_vblank_timestamp
) (struct drm_device
*dev
, unsigned int pipe
,
708 struct timeval
*vblank_time
,
711 /* these have to be filled in */
713 irqreturn_t(*irq_handler
) (int irq
, void *arg
);
714 void (*irq_preinstall
) (struct drm_device
*dev
);
715 int (*irq_postinstall
) (struct drm_device
*dev
);
716 void (*irq_uninstall
) (struct drm_device
*dev
);
718 /* Master routines */
719 int (*master_create
)(struct drm_device
*dev
, struct drm_master
*master
);
720 void (*master_destroy
)(struct drm_device
*dev
, struct drm_master
*master
);
722 * master_set is called whenever the minor master is set.
723 * master_drop is called whenever the minor master is dropped.
726 int (*master_set
)(struct drm_device
*dev
, struct drm_file
*file_priv
,
728 void (*master_drop
)(struct drm_device
*dev
, struct drm_file
*file_priv
,
731 int (*debugfs_init
)(struct drm_minor
*minor
);
732 void (*debugfs_cleanup
)(struct drm_minor
*minor
);
735 * @gem_free_object: deconstructor for drm_gem_objects
737 * This is deprecated and should not be used by new drivers. Use
738 * @gem_free_object_unlocked instead.
740 void (*gem_free_object
) (struct drm_gem_object
*obj
);
743 * @gem_free_object_unlocked: deconstructor for drm_gem_objects
745 * This is for drivers which are not encumbered with dev->struct_mutex
746 * legacy locking schemes. Use this hook instead of @gem_free_object.
748 void (*gem_free_object_unlocked
) (struct drm_gem_object
*obj
);
750 int (*gem_open_object
) (struct drm_gem_object
*, struct drm_file
*);
751 void (*gem_close_object
) (struct drm_gem_object
*, struct drm_file
*);
754 * Hook for allocating the GEM object struct, for use by core
757 struct drm_gem_object
*(*gem_create_object
)(struct drm_device
*dev
,
761 /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
762 int (*prime_handle_to_fd
)(struct drm_device
*dev
, struct drm_file
*file_priv
,
763 uint32_t handle
, uint32_t flags
, int *prime_fd
);
764 /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
765 int (*prime_fd_to_handle
)(struct drm_device
*dev
, struct drm_file
*file_priv
,
766 int prime_fd
, uint32_t *handle
);
767 /* export GEM -> dmabuf */
768 struct dma_buf
* (*gem_prime_export
)(struct drm_device
*dev
,
769 struct drm_gem_object
*obj
, int flags
);
770 /* import dmabuf -> GEM */
771 struct drm_gem_object
* (*gem_prime_import
)(struct drm_device
*dev
,
772 struct dma_buf
*dma_buf
);
773 /* low-level interface used by drm_gem_prime_{import,export} */
774 int (*gem_prime_pin
)(struct drm_gem_object
*obj
);
775 void (*gem_prime_unpin
)(struct drm_gem_object
*obj
);
776 struct reservation_object
* (*gem_prime_res_obj
)(
777 struct drm_gem_object
*obj
);
778 struct sg_table
*(*gem_prime_get_sg_table
)(struct drm_gem_object
*obj
);
779 struct drm_gem_object
*(*gem_prime_import_sg_table
)(
780 struct drm_device
*dev
,
781 struct dma_buf_attachment
*attach
,
782 struct sg_table
*sgt
);
783 void *(*gem_prime_vmap
)(struct drm_gem_object
*obj
);
784 void (*gem_prime_vunmap
)(struct drm_gem_object
*obj
, void *vaddr
);
785 int (*gem_prime_mmap
)(struct drm_gem_object
*obj
,
786 struct vm_area_struct
*vma
);
788 /* vga arb irq handler */
789 void (*vgaarb_irq
)(struct drm_device
*dev
, bool state
);
791 /* dumb alloc support */
792 int (*dumb_create
)(struct drm_file
*file_priv
,
793 struct drm_device
*dev
,
794 struct drm_mode_create_dumb
*args
);
795 int (*dumb_map_offset
)(struct drm_file
*file_priv
,
796 struct drm_device
*dev
, uint32_t handle
,
798 int (*dumb_destroy
)(struct drm_file
*file_priv
,
799 struct drm_device
*dev
,
802 /* Driver private ops for this object */
803 struct cdev_pager_ops
*gem_vm_ops
;
814 const struct drm_ioctl_desc
*ioctls
;
816 const struct file_operations
*fops
;
818 /* List of devices hanging off this driver with stealth attach. */
819 struct list_head legacy_dev_list
;
821 int (*sysctl_init
) (struct drm_device
*dev
,
822 struct sysctl_ctx_list
*ctx
, struct sysctl_oid
*top
);
823 void (*sysctl_cleanup
) (struct drm_device
*dev
);
824 #endif /* __DragonFly__ */
827 enum drm_minor_type
{
835 * Info file list entry. This structure represents a debugfs or proc file to
836 * be created by the drm core
838 struct drm_info_list
{
839 const char *name
; /** file name */
840 int (*show
)(struct seq_file
*, void*); /** show callback */
841 u32 driver_features
; /**< Required driver features for this entry */
846 * debugfs node structure. This structure represents a debugfs file.
848 struct drm_info_node
{
849 struct list_head list
;
850 struct drm_minor
*minor
;
851 const struct drm_info_list
*info_ent
;
856 * DRM minor structure. This structure represents a drm minor number.
859 int index
; /**< Minor device number */
860 int type
; /**< Control or render */
861 struct device
*kdev
; /**< Linux device */
862 struct drm_device
*dev
;
864 /* currently active master for this node. Protected by master_mutex */
865 struct drm_master
*master
;
868 struct drm_pending_vblank_event
{
869 struct drm_pending_event base
;
871 struct drm_event_vblank event
;
874 struct drm_vblank_crtc
{
875 struct drm_device
*dev
; /* pointer to the drm_device */
876 wait_queue_head_t queue
; /**< VBLANK wait queue */
877 struct timer_list disable_timer
; /* delayed disable timer */
879 /* vblank counter, protected by dev->vblank_time_lock for writes */
881 /* vblank timestamps, protected by dev->vblank_time_lock for writes */
882 struct timeval time
[DRM_VBLANKTIME_RBSIZE
];
884 atomic_t refcount
; /* number of users of vblank interruptsper crtc */
885 u32 last
; /* protected by dev->vbl_lock, used */
886 /* for wraparound handling */
887 u32 last_wait
; /* Last vblank seqno waited per CRTC */
888 unsigned int inmodeset
; /* Display driver is setting mode */
889 unsigned int pipe
; /* crtc index */
890 int framedur_ns
; /* frame/field duration in ns */
891 int linedur_ns
; /* line duration in ns */
892 int pixeldur_ns
; /* pixel duration in ns */
893 bool enabled
; /* so we don't call enable more than
897 struct drm_sysctl_info
{
898 struct sysctl_ctx_list ctx
;
902 /* Length for the array of resource pointers for drm_get_resource_*. */
903 #define DRM_MAX_PCI_RESOURCE 6
906 * DRM device structure. This structure represent a complete card that
907 * may contain multiple heads.
910 drm_pci_id_list_t
*id_entry
; /* PCI ID, name, and chipset private */
912 char *unique
; /* Unique identifier: e.g., busid */
913 int unique_len
; /* Length of unique field */
914 struct cdev
*devnode
; /* Device number for mknod */
915 int if_version
; /* Highest interface version set */
917 int flags
; /* Flags to open(2) */
920 struct lwkt_serialize irq_lock
; /* protects irq condition checks */
921 struct lock dev_lock
; /* protects everything else */
925 struct lock struct_mutex
; /**< For others */
926 struct lock master_mutex
; /**< For drm_minor::master */
929 /** \name Usage Counters */
931 int open_count
; /**< Outstanding files open, protected by drm_global_mutex. */
932 struct spinlock buf_lock
; /**< For drm_device::buf_use and a few other things. */
933 int buf_use
; /**< Buffers in use -- cannot alloc */
934 atomic_t buf_alloc
; /**< Buffer allocation in progress */
938 /** \name Performance counters */
940 unsigned long counters
;
941 enum drm_stat_type types
[15];
946 struct idr magic_map
;
948 struct lock filelist_mutex
;
949 struct list_head filelist
;
951 /** \name Memory management */
953 struct list_head maplist
; /**< Linked list of regions */
954 int map_count
; /**< Number of mappable regions */
955 struct drm_open_hash map_hash
; /**< User token hash table for maps */
957 /** \name Context handle management */
959 struct list_head ctxlist
; /**< Linked list of context handles */
960 struct lock ctxlist_mutex
; /**< For ctxlist */
966 struct drm_lock_data lock
; /* Information on hardware lock */
968 /** \name DMA support */
970 struct drm_device_dma
*dma
; /**< Optional pointer for DMA support */
973 /* Storage of resource pointers for drm_get_resource_* */
974 struct resource
*pcir
[DRM_MAX_PCI_RESOURCE
];
975 int pcirid
[DRM_MAX_PCI_RESOURCE
];
982 /** \name Context support */
985 __volatile__
long context_flag
; /**< Context swapping flag */
986 __volatile__
long interrupt_flag
; /**< Interruption handler flag */
987 __volatile__
long dma_flag
; /**< DMA dispatch flag */
988 wait_queue_head_t context_wait
; /**< Processes waiting on ctx switch */
989 int last_checked
; /**< Last context checked for DMA */
990 int last_context
; /**< Last current context */
991 unsigned long last_switch
; /**< jiffies at last context switch */
994 /** \name VBLANK IRQ support */
996 int irq_enabled
; /**< True if irq handler is enabled */
997 int irq
; /* Interrupt used by board */
1000 * If true, vblank interrupt will be disabled immediately when the
1001 * refcount drops to zero, as opposed to via the vblank disable
1003 * This can be set to true it the hardware has a working vblank
1004 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
1007 bool vblank_disable_immediate
;
1009 /* array of size num_crtcs */
1010 struct drm_vblank_crtc
*vblank
;
1012 struct lock vblank_time_lock
; /**< Protects vblank count and time updates during vblank enable/disable */
1013 struct lock vbl_lock
;
1014 struct timer_list vblank_disable_timer
;
1016 u32 max_vblank_count
; /**< size of vblank counter register */
1021 struct list_head vblank_event_list
;
1022 struct lock event_lock
;
1026 struct sigio
*buf_sigio
; /* Processes waiting for SIGIO */
1028 /* Sysctl support */
1029 struct drm_sysctl_info
*sysctl
;
1032 struct drm_sg_mem
*sg
; /**< Scatter gather memory */
1033 unsigned int num_crtcs
; /**< Number of CRTCs on this device */
1035 unsigned long *ctx_bitmap
;
1044 struct drm_hw_lock
*lock
;
1047 struct drm_agp_head
*agp
; /**< AGP data */
1049 struct device
*dev
; /**< Device structure */
1050 struct pci_dev
*pdev
; /**< PCI device structure */
1052 struct drm_driver
*driver
;
1053 struct drm_local_map
*agp_buffer_map
;
1054 unsigned int agp_buffer_token
;
1055 struct drm_minor
*control
; /**< Control node for card */
1056 struct drm_minor
*primary
; /**< render type primary screen head */
1058 struct drm_mode_config mode_config
; /**< Current mode config */
1060 /** \name GEM information */
1062 struct lock object_name_lock
;
1063 struct idr object_name_idr
;
1067 void *sysctl_private
;
1068 char busid_str
[128];
1071 int switch_power_state
;
1073 #ifdef __DragonFly__
1074 atomic_t unplugged
; /* device has been unplugged or gone away */
1075 #endif /* __DragonFly__ */
1078 #define DRM_SWITCH_POWER_ON 0
1079 #define DRM_SWITCH_POWER_OFF 1
1080 #define DRM_SWITCH_POWER_CHANGING 2
1081 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1083 static __inline__
int drm_core_check_feature(struct drm_device
*dev
,
1086 return ((dev
->driver
->driver_features
& feature
) ? 1 : 0);
1089 static inline void drm_device_set_unplugged(struct drm_device
*dev
)
1092 atomic_set(&dev
->unplugged
, 1);
1095 static inline int drm_device_is_unplugged(struct drm_device
*dev
)
1097 int ret
= atomic_read(&dev
->unplugged
);
1102 static inline bool drm_is_primary_client(const struct drm_file
*file_priv
)
1104 return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1108 * drm_is_master() - Check whether a DRM open-file is DRM-Master
1109 * @file: DRM open-file context
1111 * This checks whether a DRM open-file context is owner of the master context
1112 * attached to it. If a file owns a master context, it's called DRM-Master.
1113 * Per DRM device, only one such file can be DRM-Master at a time.
1115 * Returns: True if the file is DRM-Master, otherwise false.
1117 static inline bool drm_is_master(const struct drm_file
*file
)
1120 return file
->master
&& file
->master
== file
->minor
->master
;
1126 /******************************************************************/
1127 /** \name Internal function definitions */
1130 /* Driver support (drm_drv.h) */
1131 extern int drm_ioctl_permit(u32 flags
, struct drm_file
*file_priv
);
1132 int drm_probe(device_t kdev
, drm_pci_id_list_t
*idlist
);
1133 int drm_attach(device_t kdev
, drm_pci_id_list_t
*idlist
);
1134 int drm_create_cdevs(device_t kdev
);
1135 d_ioctl_t drm_ioctl
;
1136 extern bool drm_ioctl_flags(unsigned int nr
, unsigned int *flags
);
1138 /* Device support (drm_fops.h) */
1140 d_close_t drm_close
;
1142 d_kqfilter_t drm_kqfilter
;
1143 int drm_release(device_t kdev
);
1146 d_mmap_single_t drm_mmap_single
;
1148 void drm_cdevpriv_dtor(void *cd
);
1150 int drm_add_busid_modesetting(struct drm_device
*dev
,
1151 struct sysctl_ctx_list
*ctx
, struct sysctl_oid
*top
);
1153 /* File operations helpers (drm_fops.c) */
1154 extern int drm_open_helper(struct cdev
*kdev
, int flags
, int fmt
,
1156 struct drm_device
*dev
,
1158 int drm_event_reserve_init_locked(struct drm_device
*dev
,
1159 struct drm_file
*file_priv
,
1160 struct drm_pending_event
*p
,
1161 struct drm_event
*e
);
1162 int drm_event_reserve_init(struct drm_device
*dev
,
1163 struct drm_file
*file_priv
,
1164 struct drm_pending_event
*p
,
1165 struct drm_event
*e
);
1166 void drm_event_cancel_free(struct drm_device
*dev
,
1167 struct drm_pending_event
*p
);
1168 void drm_send_event_locked(struct drm_device
*dev
, struct drm_pending_event
*e
);
1169 void drm_send_event(struct drm_device
*dev
, struct drm_pending_event
*e
);
1171 /* Misc. IOCTL support (drm_ioctl.c) */
1172 int drm_noop(struct drm_device
*dev
, void *data
,
1173 struct drm_file
*file_priv
);
1174 int drm_invalid_op(struct drm_device
*dev
, void *data
,
1175 struct drm_file
*file_priv
);
1177 /* Cache management (drm_cache.c) */
1178 void drm_clflush_pages(struct page
*pages
[], unsigned long num_pages
);
1179 void drm_clflush_sg(struct sg_table
*st
);
1180 void drm_clflush_virt_range(void *addr
, unsigned long length
);
1183 * These are exported to drivers so that they can implement fencing using
1184 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1187 unsigned long drm_get_resource_start(struct drm_device
*dev
,
1188 unsigned int resource
);
1189 unsigned long drm_get_resource_len(struct drm_device
*dev
,
1190 unsigned int resource
);
1192 /* IRQ support (drm_irq.h) */
1193 extern int drm_irq_install(struct drm_device
*dev
, int irq
);
1194 extern int drm_irq_uninstall(struct drm_device
*dev
);
1196 extern int drm_vblank_init(struct drm_device
*dev
, unsigned int num_crtcs
);
1197 extern int drm_wait_vblank(struct drm_device
*dev
, void *data
,
1198 struct drm_file
*filp
);
1199 extern u32
drm_vblank_count(struct drm_device
*dev
, unsigned int pipe
);
1200 extern u32
drm_crtc_vblank_count(struct drm_crtc
*crtc
);
1201 extern u32
drm_vblank_count_and_time(struct drm_device
*dev
, unsigned int pipe
,
1202 struct timeval
*vblanktime
);
1203 extern u32
drm_crtc_vblank_count_and_time(struct drm_crtc
*crtc
,
1204 struct timeval
*vblanktime
);
1205 extern void drm_send_vblank_event(struct drm_device
*dev
, unsigned int pipe
,
1206 struct drm_pending_vblank_event
*e
);
1207 extern void drm_crtc_send_vblank_event(struct drm_crtc
*crtc
,
1208 struct drm_pending_vblank_event
*e
);
1209 extern void drm_arm_vblank_event(struct drm_device
*dev
, unsigned int pipe
,
1210 struct drm_pending_vblank_event
*e
);
1211 extern void drm_crtc_arm_vblank_event(struct drm_crtc
*crtc
,
1212 struct drm_pending_vblank_event
*e
);
1213 extern bool drm_handle_vblank(struct drm_device
*dev
, unsigned int pipe
);
1214 extern bool drm_crtc_handle_vblank(struct drm_crtc
*crtc
);
1215 extern int drm_vblank_get(struct drm_device
*dev
, unsigned int pipe
);
1216 extern void drm_vblank_put(struct drm_device
*dev
, unsigned int pipe
);
1217 extern int drm_crtc_vblank_get(struct drm_crtc
*crtc
);
1218 extern void drm_crtc_vblank_put(struct drm_crtc
*crtc
);
1219 extern void drm_wait_one_vblank(struct drm_device
*dev
, unsigned int pipe
);
1220 extern void drm_crtc_wait_one_vblank(struct drm_crtc
*crtc
);
1221 extern void drm_vblank_off(struct drm_device
*dev
, unsigned int pipe
);
1222 extern void drm_vblank_on(struct drm_device
*dev
, unsigned int pipe
);
1223 extern void drm_crtc_vblank_off(struct drm_crtc
*crtc
);
1224 extern void drm_crtc_vblank_reset(struct drm_crtc
*crtc
);
1225 extern void drm_crtc_vblank_on(struct drm_crtc
*crtc
);
1226 extern void drm_vblank_cleanup(struct drm_device
*dev
);
1227 extern u32
drm_vblank_no_hw_counter(struct drm_device
*dev
, unsigned int pipe
);
1229 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device
*dev
,
1230 unsigned int pipe
, int *max_error
,
1231 struct timeval
*vblank_time
,
1233 const struct drm_display_mode
*mode
);
1234 extern void drm_calc_timestamping_constants(struct drm_crtc
*crtc
,
1235 const struct drm_display_mode
*mode
);
1238 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1239 * @crtc: which CRTC's vblank waitqueue to retrieve
1241 * This function returns a pointer to the vblank waitqueue for the CRTC.
1242 * Drivers can use this to implement vblank waits using wait_event() & co.
1244 static inline wait_queue_head_t
*drm_crtc_vblank_waitqueue(struct drm_crtc
*crtc
)
1246 return &crtc
->dev
->vblank
[drm_crtc_index(crtc
)].queue
;
1249 /* Modesetting support */
1250 extern void drm_vblank_pre_modeset(struct drm_device
*dev
, unsigned int pipe
);
1251 extern void drm_vblank_post_modeset(struct drm_device
*dev
, unsigned int pipe
);
1253 /* Stub support (drm_stub.h) */
1255 extern void drm_put_dev(struct drm_device
*dev
);
1256 extern void drm_unplug_dev(struct drm_device
*dev
);
1257 extern unsigned int drm_debug
;
1258 extern bool drm_atomic
;
1260 /* consistent PCI memory functions (drm_pci.c) */
1261 extern struct drm_dma_handle
*drm_pci_alloc(struct drm_device
*dev
, size_t size
,
1263 extern void drm_pci_free(struct drm_device
*dev
, struct drm_dma_handle
* dmah
);
1265 /* sysfs support (drm_sysfs.c) */
1266 extern void drm_sysfs_hotplug_event(struct drm_device
*dev
);
1268 /* sysctl support (drm_sysctl.h) */
1269 extern int drm_sysctl_init(struct drm_device
*dev
);
1270 extern int drm_sysctl_cleanup(struct drm_device
*dev
);
1272 /* XXX: These are here only because of drm_sysctl.c */
1273 extern int drm_vblank_offdelay
;
1274 extern unsigned int drm_timestamp_precision
;
1276 int drm_gem_mmap_single(struct drm_device
*dev
, vm_ooffset_t
*offset
,
1277 vm_size_t size
, struct vm_object
**obj_res
, int nprot
);
1279 struct ttm_bo_device
;
1280 int ttm_bo_mmap_single(struct ttm_bo_device
*bdev
, vm_ooffset_t
*offset
,
1281 vm_size_t size
, struct vm_object
**obj_res
, int nprot
);
1282 struct ttm_buffer_object
;
1283 void ttm_bo_release_mmap(struct ttm_buffer_object
*bo
);
1285 /* simplified version of kvasnprintf() for drm needs. */
1286 char *drm_vasprintf(int flags
, const char *format
, __va_list ap
) __printflike(2, 0);
1287 char *drm_asprintf(int flags
, const char *format
, ...) __printflike(2, 3);
1289 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1290 void drm_init_pdev(device_t dev
, struct pci_dev
**pdev
);
1291 void drm_fini_pdev(struct pci_dev
**pdev
);
1292 void drm_print_pdev(struct pci_dev
*pdev
);
1294 /* Inline drm_free() helper for area kfree() */
1295 static __inline__
void
1296 drm_free(void *pt
, struct malloc_type
*area
)
1298 /* kfree is special!!! */
1303 struct drm_device
*drm_dev_alloc(struct drm_driver
*driver
,
1304 struct device
*parent
);
1305 void drm_dev_ref(struct drm_device
*dev
);
1306 void drm_dev_unref(struct drm_device
*dev
);
1307 int drm_dev_register(struct drm_device
*dev
, unsigned long flags
);
1308 void drm_dev_unregister(struct drm_device
*dev
);
1309 int drm_dev_set_unique(struct drm_device
*dev
, const char *fmt
, ...);
1313 static __inline__
int drm_pci_device_is_agp(struct drm_device
*dev
)
1315 if (dev
->driver
->device_is_agp
!= NULL
) {
1316 int err
= (*dev
->driver
->device_is_agp
) (dev
);
1323 return (pci_find_extcap(dev
->pdev
->dev
.bsddev
, PCIY_AGP
, NULL
) == 0);
1325 void drm_pci_agp_destroy(struct drm_device
*dev
);
1327 extern int drm_pci_init(struct drm_driver
*driver
, struct pci_driver
*pdriver
);
1328 extern void drm_pci_exit(struct drm_driver
*driver
, struct pci_driver
*pdriver
);
1330 extern int drm_get_pci_dev(struct pci_dev
*pdev
,
1331 const struct pci_device_id
*ent
,
1332 struct drm_driver
*driver
);
1333 extern int drm_pci_set_busid(struct drm_device
*dev
, struct drm_master
*master
);
1335 static inline int drm_get_pci_dev(struct pci_dev
*pdev
,
1336 const struct pci_device_id
*ent
,
1337 struct drm_driver
*driver
)
1342 static inline int drm_pci_set_busid(struct drm_device
*dev
,
1343 struct drm_master
*master
)
1349 #define DRM_PCIE_SPEED_25 1
1350 #define DRM_PCIE_SPEED_50 2
1351 #define DRM_PCIE_SPEED_80 4
1353 extern int drm_pcie_get_speed_cap_mask(struct drm_device
*dev
, u32
*speed_mask
);
1356 #define drm_can_sleep() (HZ & 1)
1358 #endif /* __KERNEL__ */
1360 /* helper for handling conditionals in various for_each macros */
1361 #define for_each_if(condition) if (!(condition)) {} else
1363 #endif /* _DRM_P_H_ */