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/signalvar.h>
59 #include <linux/highmem.h>
61 #include <sys/taskqueue.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page2.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vm_param.h>
72 #include <machine/param.h>
73 #include <machine/pmap.h>
75 #include <machine/specialreg.h>
77 #include <machine/sysarch.h>
78 #include <sys/endian.h>
81 #include <sys/memrange.h>
82 #include <sys/mutex.h>
84 #include <linux/atomic.h>
85 #include <linux/bug.h>
86 #include <linux/dma-mapping.h>
87 #include <linux/capability.h>
88 #include <linux/err.h>
89 #include <linux/idr.h>
90 #include <linux/pci.h>
91 #include <linux/jiffies.h>
92 #include <linux/kernel.h>
94 #include <linux/kref.h>
95 #include <linux/list.h>
97 #include <linux/moduleparam.h>
98 #include <linux/mutex.h>
99 #include <linux/slab.h>
100 #include <linux/scatterlist.h>
101 #include <linux/timer.h>
103 #include <linux/seq_file.h>
104 #include <linux/types.h>
105 #include <linux/vmalloc.h>
106 #include <linux/wait.h>
107 #include <linux/workqueue.h>
109 #include <asm/uaccess.h>
111 #include <uapi_drm/drm.h>
112 #include <uapi_drm/drm_mode.h>
114 #include <drm/drm_agpsupport.h>
115 #include <drm/drm_crtc.h>
116 #include <drm/drm_global.h>
117 #include <drm/drm_hashtab.h>
118 #include <drm/drm_mem_util.h>
119 #include <drm/drm_mm.h>
120 #include <drm/drm_os_linux.h>
121 #include <uapi_drm/drm_sarea.h>
122 #include <drm/drm_vma_manager.h>
127 struct drm_local_map;
128 struct drm_device_dma;
129 struct drm_dma_handle;
130 struct drm_gem_object;
133 #ifdef CONFIG_VIDEOMODE_HELPERS
134 struct videomode; /* XXX empty struct in videomode.h ? */
136 struct reservation_object;
137 struct dma_buf_attachment;
140 * 4 debug categories are defined:
142 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
143 * This is the category used by the DRM_DEBUG() macro.
145 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
146 * This is the category used by the DRM_DEBUG_DRIVER() macro.
148 * KMS: used in the modesetting code.
149 * This is the category used by the DRM_DEBUG_KMS() macro.
151 * PRIME: used in the prime code.
152 * This is the category used by the DRM_DEBUG_PRIME() macro.
154 * ATOMIC: used in the atomic code.
155 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
158 * PID: used as modifier to include PID number in messages.
159 * This is the category used by the all debug macros.
161 * FIOCTL: used in failed ioctl debugging.
162 * This is the category used by the DRM_DEBUG_FIOCTL() macro.
164 * IOCTL: used in ioctl debugging.
165 * This is the category used by the DRM_DEBUG_IOCTL() macro.
167 * VBLANK: used in vblank debugging.
168 * This is the category used by the DRM_DEBUG_VBLANK() macro.
170 * Enabling verbose debug messages is done through the drm.debug parameter,
171 * each category being enabled by a bit.
173 * drm.debug=0x1 will enable CORE messages
174 * drm.debug=0x2 will enable DRIVER messages
175 * drm.debug=0x3 will enable CORE and DRIVER messages
177 * drm.debug=0xf will enable all messages
179 * An interesting feature is that it's possible to enable verbose logging at
180 * run-time by using hw.drm.debug sysctl variable:
181 * # sysctl hw.drm.debug=0xfff
183 #define DRM_UT_CORE 0x01
184 #define DRM_UT_DRIVER 0x02
185 #define DRM_UT_KMS 0x04
186 #define DRM_UT_PRIME 0x08
187 #define DRM_UT_ATOMIC 0x10
188 #define DRM_UT_VBL 0x20
189 /* Extra DragonFly debug categories */
191 #define DRM_UT_RES7 0x40 /* reserved for future expansion */
192 #define DRM_UT_RES8 0x80 /* reserved for future expansion */
193 #define DRM_UT_PID 0x100
194 #define DRM_UT_FIOCTL 0x200
195 #define DRM_UT_IOCTL 0x400
196 #define DRM_UT_VBLANK 0x800
199 extern __printflike(2, 3)
200 void drm_ut_debug_printk(const char *function_name,
201 const char *format, ...);
202 extern __printflike(2, 3)
203 void drm_err(const char *func, const char *format, ...);
205 /***********************************************************************/
206 /** \name DRM template customization defaults */
209 /* driver capabilities and requirements mask */
210 #define DRIVER_USE_AGP 0x1
211 #define DRIVER_PCI_DMA 0x8
212 #define DRIVER_SG 0x10
213 #define DRIVER_HAVE_DMA 0x20
214 #define DRIVER_HAVE_IRQ 0x40
215 #define DRIVER_IRQ_SHARED 0x80
216 #define DRIVER_GEM 0x1000
217 #define DRIVER_MODESET 0x2000
218 #define DRIVER_PRIME 0x4000
219 #define DRIVER_RENDER 0x8000
220 #define DRIVER_ATOMIC 0x10000
221 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
223 #define DRM_MAGIC_HASH_ORDER 4 /**< Size of key hash table. Must be power of 2. */
225 /***********************************************************************/
226 /** \name Macros to make printk easier */
232 * \param fmt printf() like format string.
233 * \param arg arguments
235 #define DRM_ERROR(fmt, ...) \
236 drm_err(__func__, fmt, ##__VA_ARGS__)
239 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
241 * \param fmt printf() like format string.
242 * \param arg arguments
244 #define DRM_ERROR_RATELIMITED(fmt, ...) \
246 static struct krate krate_derr = { .freq = 1, .count = -16 }; \
248 krateprintf(&krate_derr, "error: [" DRM_NAME ":%s] *ERROR* " \
249 fmt, __func__, ##__VA_ARGS__); \
252 #define DRM_INFO(fmt, ...) \
253 printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
255 #define DRM_INFO_ONCE(fmt, ...) \
256 printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
261 * \param fmt printf() like format string.
262 * \param arg arguments
264 #define DRM_DEBUG(fmt, args...) \
266 if (unlikely(drm_debug & DRM_UT_CORE)) \
267 drm_ut_debug_printk(__func__, fmt, ##args); \
270 #define DRM_DEBUG_DRIVER(fmt, args...) \
272 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
273 drm_ut_debug_printk(__func__, fmt, ##args); \
275 #define DRM_DEBUG_KMS(fmt, args...) \
277 if (unlikely(drm_debug & DRM_UT_KMS)) \
278 drm_ut_debug_printk(__func__, fmt, ##args); \
280 #define DRM_DEBUG_PRIME(fmt, args...) \
282 if (unlikely(drm_debug & DRM_UT_PRIME)) \
283 drm_ut_debug_printk(__func__, fmt, ##args); \
285 #define DRM_DEBUG_ATOMIC(fmt, args...) \
287 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
288 drm_ut_debug_printk(__func__, fmt, ##args); \
292 #define DRM_DEBUG_FIOCTL(fmt, args...) \
294 if (unlikely(drm_debug & DRM_UT_FIOCTL)) \
295 drm_ut_debug_printk(__func__, fmt, ##args); \
297 #define DRM_DEBUG_IOCTL(fmt, args...) \
299 if (unlikely(drm_debug & DRM_UT_IOCTL)) \
300 drm_ut_debug_printk(__func__, fmt, ##args); \
302 #define DRM_DEBUG_VBLANK(fmt, args...) \
304 if (unlikely(drm_debug & DRM_UT_VBLANK)) \
305 drm_ut_debug_printk(__func__, fmt, ##args); \
307 #define DRM_DEBUG_VBL(fmt, args...) \
309 if (unlikely(drm_debug & DRM_UT_VBL)) \
310 drm_ut_debug_printk(__func__, fmt, ##args); \
316 /***********************************************************************/
317 /** \name Internal types and structures */
320 SYSCTL_DECL(_hw_drm);
322 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
324 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
326 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
327 #define DRM_DEV_UID UID_ROOT
328 #define DRM_DEV_GID GID_WHEEL
330 #define DRM_CURPROC curthread
331 #define DRM_STRUCTPROC struct thread
332 #define DRM_LOCK(dev) lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
333 #define DRM_UNLOCK(dev) lockmgr(&(dev)->struct_mutex, LK_RELEASE)
334 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout) \
335 (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
336 #if defined(INVARIANTS)
337 #define DRM_LOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
338 #define DRM_UNLOCK_ASSERT(dev) KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
340 #define DRM_LOCK_ASSERT(d)
341 #define DRM_UNLOCK_ASSERT(d)
344 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
346 typedef void irqreturn_t;
347 #define IRQ_HANDLED /* nothing */
348 #define IRQ_NONE /* nothing */
350 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
352 #define DRM_MTRR_WC MDF_WRITECOMBINE
354 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
355 vm_memattr_t memattr);
356 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
357 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
360 int drm_mtrr_add(unsigned long offset, size_t size, int flags);
361 int drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
363 typedef struct drm_pci_id_list
372 * Ioctl function type.
374 * \param inode device inode.
375 * \param file_priv DRM file private pointer.
376 * \param cmd command.
377 * \param arg argument.
379 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
380 struct drm_file *file_priv);
382 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
385 #define DRM_IOCTL_NR(n) ((n) & 0xff)
388 #define DRM_MASTER 0x2
389 #define DRM_ROOT_ONLY 0x4
390 #define DRM_CONTROL_ALLOW 0x8
391 #define DRM_UNLOCKED 0x10
392 #define DRM_RENDER_ALLOW 0x20
394 struct drm_ioctl_desc {
398 unsigned int cmd_drv;
403 * Creates a driver or general drm_ioctl_desc array entry for the given
404 * ioctl, for use by drm_ioctl().
407 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
408 [DRM_IOCTL_NR(DRM_##ioctl)] = { \
409 .cmd = DRM_##ioctl, \
412 .cmd_drv = DRM_IOCTL_##ioctl, \
416 typedef struct drm_magic_entry {
417 struct list_head head;
418 struct drm_hash_item hash_item;
419 struct drm_file *priv;
420 struct drm_magic_entry *next;
423 /* Event queued up for userspace to read */
424 struct drm_pending_event {
425 struct drm_event *event;
426 struct list_head 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;
438 #endif /* DUMBBELL_WIP */
441 /** File private data */
444 struct drm_device *dev;
447 /* true when the client has asked us to expose stereo 3D mode flags */
450 * true if client understands CRTC primary planes and cursor planes
453 unsigned universal_planes:1;
454 /* true if client understands atomic properties */
460 unsigned long ioctl_count;
461 struct list_head lhead;
464 /** Mapping of mm object handles to object pointers. */
465 struct idr object_idr;
466 /** Lock for synchronization of access to object_idr. */
467 struct lock table_lock;
472 struct drm_master *masterp;
475 * fbs - List of framebuffers associated with this file.
477 * Protected by fbs_lock. Note that the fbs list holds a reference on
478 * the fb object to prevent it from untimely disappearing.
480 struct list_head fbs;
481 struct lock fbs_lock;
483 /** User-created blob properties; this retains a reference on the
485 struct list_head blobs;
487 wait_queue_head_t event_wait;
488 struct list_head pending_event_list;
489 struct list_head event_list;
492 struct lock event_read_lock;
494 struct drm_prime_file_private prime;
500 struct drm_lock_data {
501 struct drm_hw_lock *hw_lock; /**< Hardware lock */
502 /** Private of lock holder's file (NULL=kernel) */
503 struct drm_file *file_priv;
504 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
505 unsigned long lock_time; /**< Time of last lock in jiffies */
509 * GEM specific mm private for tracking GEM objects
512 struct drm_vma_offset_manager vma_manager;
513 struct drm_mm offset_manager; /**< Offset mgmt for buffer objects */
514 struct drm_open_hash offset_hash; /**< User token hash table for maps */
515 struct unrhdr *idxunr;
519 * struct drm_master - drm master structure
521 * @refcount: Refcount for this master object.
522 * @minor: Link back to minor char device we are master for. Immutable.
523 * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
524 * @unique_len: Length of unique field. Protected by drm_global_mutex.
525 * @unique_size: Amount allocated. Protected by drm_global_mutex.
526 * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
527 * @magicfree: List of used authentication tokens. Protected by struct_mutex.
528 * @lock: DRI lock information.
529 * @driver_priv: Pointer to driver-private information.
532 struct kref refcount; /* refcount for this master */
533 struct list_head head; /**< each minor contains a list of masters */
534 struct drm_minor *minor; /**< link back to minor we are a master for */
535 char *unique; /**< Unique identifier: e.g., busid */
536 int unique_len; /**< Length of unique field */
537 int unique_size; /**< amount allocated */
538 int blocked; /**< Blocked due to VC switch? */
539 struct drm_open_hash magiclist;
540 struct list_head magicfree;
541 struct drm_lock_data lock;
545 /* Size of ringbuffer for vblank timestamps. Just double-buffer
546 * in initial implementation.
548 #define DRM_VBLANKTIME_RBSIZE 2
550 /* Flags and return codes for get_vblank_timestamp() driver function. */
551 #define DRM_CALLED_FROM_VBLIRQ 1
552 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
553 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
555 /* get_scanout_position() return flags */
556 #define DRM_SCANOUTPOS_VALID (1 << 0)
557 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
558 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
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, struct drm_file *file_priv);
577 int (*dma_quiescent) (struct drm_device *);
578 int (*context_ctor) (struct drm_device *dev, int context);
579 int (*context_dtor) (struct drm_device *dev, int context);
582 * get_vblank_counter - get raw hardware vblank counter
584 * @pipe: counter to fetch
586 * Driver callback for fetching a raw hardware vblank counter for @crtc.
587 * If a device doesn't have a hardware counter, the driver can simply
588 * return the value of drm_vblank_count. The DRM core will account for
589 * missed vblank events while interrupts where disabled based on system
592 * Wraparound handling and loss of events due to modesetting is dealt
593 * with in the DRM core code.
596 * Raw vblank counter value.
598 u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
601 * enable_vblank - enable vblank interrupt events
603 * @pipe: which irq to enable
605 * Enable vblank interrupts for @crtc. If the device doesn't have
606 * a hardware vblank counter, this routine should be a no-op, since
607 * interrupts will have to stay on to keep the count accurate.
610 * Zero on success, appropriate errno if the given @crtc's vblank
611 * interrupt cannot be enabled.
613 int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
616 * disable_vblank - disable vblank interrupt events
618 * @pipe: which irq to enable
620 * Disable vblank interrupts for @crtc. If the device doesn't have
621 * a hardware vblank counter, this routine should be a no-op, since
622 * interrupts will have to stay on to keep the count accurate.
624 void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
627 * Called by \c drm_device_is_agp. Typically used to determine if a
628 * card is really attached to AGP or not.
630 * \param dev DRM device handle
633 * One of three values is returned depending on whether or not the
634 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
635 * (return of 1), or may or may not be AGP (return of 2).
637 int (*device_is_agp) (struct drm_device *dev);
640 * Called by vblank timestamping code.
642 * Return the current display scanout position from a crtc, and an
643 * optional accurate ktime_get timestamp of when position was measured.
645 * \param dev DRM device.
646 * \param pipe Id of the crtc to query.
647 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
648 * \param *vpos Target location for current vertical scanout position.
649 * \param *hpos Target location for current horizontal scanout position.
650 * \param *stime Target location for timestamp taken immediately before
651 * scanout position query. Can be NULL to skip timestamp.
652 * \param *etime Target location for timestamp taken immediately after
653 * scanout position query. Can be NULL to skip timestamp.
655 * Returns vpos as a positive number while in active scanout area.
656 * Returns vpos as a negative number inside vblank, counting the number
657 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
658 * until start of active scanout / end of vblank."
660 * \return Flags, or'ed together as follows:
662 * DRM_SCANOUTPOS_VALID = Query successful.
663 * DRM_SCANOUTPOS_INVBL = Inside vblank.
664 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
665 * this flag means that returned position may be offset by a constant
666 * but unknown small number of scanlines wrt. real scanout position.
669 int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
670 unsigned int flags, int *vpos, int *hpos,
671 ktime_t *stime, ktime_t *etime,
672 const struct drm_display_mode *mode);
675 * Called by \c drm_get_last_vbltimestamp. Should return a precise
676 * timestamp when the most recent VBLANK interval ended or will end.
678 * Specifically, the timestamp in @vblank_time should correspond as
679 * closely as possible to the time when the first video scanline of
680 * the video frame after the end of VBLANK will start scanning out,
681 * the time immediately after end of the VBLANK interval. If the
682 * @crtc is currently inside VBLANK, this will be a time in the future.
683 * If the @crtc is currently scanning out a frame, this will be the
684 * past start time of the current scanout. This is meant to adhere
685 * to the OpenML OML_sync_control extension specification.
687 * \param dev dev DRM device handle.
688 * \param pipe crtc for which timestamp should be returned.
689 * \param *max_error Maximum allowable timestamp error in nanoseconds.
690 * Implementation should strive to provide timestamp
691 * with an error of at most *max_error nanoseconds.
692 * Returns true upper bound on error for timestamp.
693 * \param *vblank_time Target location for returned vblank timestamp.
694 * \param flags 0 = Defaults, no special treatment needed.
695 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
696 * irq handler. Some drivers need to apply some workarounds
697 * for gpu-specific vblank irq quirks if flag is set.
700 * Zero if timestamping isn't supported in current display mode or a
701 * negative number on failure. A positive status code on success,
702 * which describes how the vblank_time timestamp was computed.
704 int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
706 struct timeval *vblank_time,
709 /* these have to be filled in */
711 void (*irq_handler) (void *arg);
712 void (*irq_preinstall) (struct drm_device *dev);
713 int (*irq_postinstall) (struct drm_device *dev);
714 void (*irq_uninstall) (struct drm_device *dev);
717 * Driver-specific constructor for drm_gem_objects, to set up
718 * obj->driver_private.
720 * Returns 0 on success.
722 void (*gem_free_object) (struct drm_gem_object *obj);
723 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
724 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
726 int (*sysctl_init) (struct drm_device *dev,
727 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
728 void (*sysctl_cleanup) (struct drm_device *dev);
730 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
732 /* dumb alloc support */
733 int (*dumb_create)(struct drm_file *file_priv,
734 struct drm_device *dev,
735 struct drm_mode_create_dumb *args);
736 int (*dumb_map_offset)(struct drm_file *file_priv,
737 struct drm_device *dev, uint32_t handle,
739 int (*dumb_destroy)(struct drm_file *file_priv,
740 struct drm_device *dev,
743 /* Driver private ops for this object */
744 struct cdev_pager_ops *gem_pager_ops;
749 const char *name; /* Simple driver name */
750 const char *desc; /* Longer driver name */
751 const char *date; /* Date of last major changes. */
755 const struct drm_ioctl_desc *ioctls;
760 * Info file list entry. This structure represents a debugfs or proc file to
761 * be created by the drm core
763 struct drm_info_list {
764 const char *name; /** file name */
765 int (*show)(struct seq_file*, void*); /** show callback */
766 u32 driver_features; /**< Required driver features for this entry */
771 * debugfs node structure. This structure represents a debugfs file.
773 struct drm_info_node {
774 struct list_head list;
775 struct drm_minor *minor;
776 const struct drm_info_list *info_ent;
781 * DRM minor structure. This structure represents a drm minor number.
784 int index; /**< Minor device number */
785 int type; /**< Control or render */
786 struct device *kdev; /**< Linux device */
787 struct drm_device *dev;
789 /* currently active master for this node. Protected by master_mutex */
790 struct drm_master *master;
793 struct drm_pending_vblank_event {
794 struct drm_pending_event base;
796 struct drm_event_vblank event;
799 struct drm_vblank_crtc {
800 struct drm_device *dev; /* pointer to the drm_device */
801 wait_queue_head_t queue; /**< VBLANK wait queue */
802 struct timer_list disable_timer; /* delayed disable timer */
804 /* vblank counter, protected by dev->vblank_time_lock for writes */
806 /* vblank timestamps, protected by dev->vblank_time_lock for writes */
807 struct timeval time[DRM_VBLANKTIME_RBSIZE];
809 atomic_t refcount; /* number of users of vblank interruptsper crtc */
810 u32 last; /* protected by dev->vbl_lock, used */
811 /* for wraparound handling */
812 u32 last_wait; /* Last vblank seqno waited per CRTC */
813 unsigned int inmodeset; /* Display driver is setting mode */
814 unsigned int pipe; /* crtc index */
815 int framedur_ns; /* frame/field duration in ns */
816 int linedur_ns; /* line duration in ns */
817 int pixeldur_ns; /* pixel duration in ns */
818 bool enabled; /* so we don't call enable more than
822 struct drm_sysctl_info {
823 struct sysctl_ctx_list ctx;
827 /* Length for the array of resource pointers for drm_get_resource_*. */
828 #define DRM_MAX_PCI_RESOURCE 6
831 * DRM device structure. This structure represent a complete card that
832 * may contain multiple heads.
835 drm_pci_id_list_t *id_entry; /* PCI ID, name, and chipset private */
837 char *unique; /* Unique identifier: e.g., busid */
838 int unique_len; /* Length of unique field */
839 struct cdev *devnode; /* Device number for mknod */
840 int if_version; /* Highest interface version set */
842 int flags; /* Flags to open(2) */
845 struct spinlock dma_lock; /* protects dev->dma */
846 struct lwkt_serialize irq_lock; /* protects irq condition checks */
847 struct lock dev_lock; /* protects everything else */
851 struct lock struct_mutex; /**< For others */
852 struct lock master_mutex; /**< For drm_minor::master */
855 /** \name Usage Counters */
857 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
858 struct spinlock buf_lock; /**< For drm_device::buf_use and a few other things. */
859 int buf_use; /**< Buffers in use -- cannot alloc */
860 atomic_t buf_alloc; /**< Buffer allocation in progress */
864 /** \name Performance counters */
866 unsigned long counters;
867 enum drm_stat_type types[15];
872 struct drm_open_hash magiclist; /**< magic hash table */
873 struct list_head magicfree;
875 struct list_head filelist;
877 /** \name Memory management */
879 struct list_head maplist; /**< Linked list of regions */
880 int map_count; /**< Number of mappable regions */
881 struct drm_open_hash map_hash; /**< User token hash table for maps */
883 /** \name Context handle management */
885 struct list_head ctxlist; /**< Linked list of context handles */
886 int ctx_count; /**< Number of context handles */
887 struct lock ctxlist_mutex; /**< For ctxlist */
893 struct drm_lock_data lock; /* Information on hardware lock */
895 /** \name DMA support */
897 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
900 int irq_type; /* IRQ type (MSI enabled or not) */
901 int irqrid; /* Interrupt used by board */
902 struct resource *irqr; /* Resource for interrupt used by board */
903 void *irqh; /* Handle from bus_setup_intr */
905 /* Storage of resource pointers for drm_get_resource_* */
906 struct resource *pcir[DRM_MAX_PCI_RESOURCE];
907 int pcirid[DRM_MAX_PCI_RESOURCE];
914 /** \name Context support */
917 __volatile__ long context_flag; /**< Context swapping flag */
918 __volatile__ long interrupt_flag; /**< Interruption handler flag */
919 __volatile__ long dma_flag; /**< DMA dispatch flag */
920 wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
921 int last_checked; /**< Last context checked for DMA */
922 int last_context; /**< Last current context */
923 unsigned long last_switch; /**< jiffies at last context switch */
926 /** \name VBLANK IRQ support */
928 int irq_enabled; /**< True if irq handler is enabled */
929 int irq; /* Interrupt used by board */
932 * If true, vblank interrupt will be disabled immediately when the
933 * refcount drops to zero, as opposed to via the vblank disable
935 * This can be set to true it the hardware has a working vblank
936 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
939 bool vblank_disable_immediate;
941 /* array of size num_crtcs */
942 struct drm_vblank_crtc *vblank;
944 struct lock vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
945 struct lock vbl_lock;
946 struct timer_list vblank_disable_timer;
948 u32 max_vblank_count; /**< size of vblank counter register */
953 struct list_head vblank_event_list;
954 struct lock event_lock;
958 struct sigio *buf_sigio; /* Processes waiting for SIGIO */
961 struct drm_sysctl_info *sysctl;
964 struct drm_sg_mem *sg; /**< Scatter gather memory */
965 unsigned int num_crtcs; /**< Number of CRTCs on this device */
967 unsigned long *ctx_bitmap;
976 struct drm_hw_lock *lock;
979 struct drm_agp_head *agp; /**< AGP data */
981 struct device *dev; /**< Device structure */
982 struct pci_dev *pdev; /**< PCI device structure */
984 struct drm_driver *driver;
985 struct drm_local_map *agp_buffer_map;
986 unsigned int agp_buffer_token;
987 struct drm_minor *control; /**< Control node for card */
988 struct drm_minor *primary; /**< render type primary screen head */
990 struct drm_mode_config mode_config; /**< Current mode config */
992 /** \name GEM information */
994 struct lock object_name_lock;
995 struct idr object_name_idr;
999 void *sysctl_private;
1000 char busid_str[128];
1003 int switch_power_state;
1005 #ifdef __DragonFly__
1006 atomic_t unplugged; /* device has been unplugged or gone away */
1007 #endif /* __DragonFly__ */
1010 #define DRM_SWITCH_POWER_ON 0
1011 #define DRM_SWITCH_POWER_OFF 1
1012 #define DRM_SWITCH_POWER_CHANGING 2
1013 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1015 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1018 return ((dev->driver->driver_features & feature) ? 1 : 0);
1021 static inline void drm_device_set_unplugged(struct drm_device *dev)
1024 atomic_set(&dev->unplugged, 1);
1027 static inline int drm_device_is_unplugged(struct drm_device *dev)
1029 int ret = atomic_read(&dev->unplugged);
1034 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1036 return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1040 * drm_is_master() - Check whether a DRM open-file is DRM-Master
1041 * @file: DRM open-file context
1043 * This checks whether a DRM open-file context is owner of the master context
1044 * attached to it. If a file owns a master context, it's called DRM-Master.
1045 * Per DRM device, only one such file can be DRM-Master at a time.
1047 * Returns: True if the file is DRM-Master, otherwise false.
1049 static inline bool drm_is_master(const struct drm_file *file)
1052 return file->master && file->master == file->minor->master;
1058 /******************************************************************/
1059 /** \name Internal function definitions */
1062 /* Driver support (drm_drv.h) */
1063 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1064 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1065 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1066 int drm_create_cdevs(device_t kdev);
1067 d_ioctl_t drm_ioctl;
1068 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1070 /* Device support (drm_fops.h) */
1072 d_close_t drm_close;
1074 d_kqfilter_t drm_kqfilter;
1075 int drm_release(device_t kdev);
1078 d_mmap_single_t drm_mmap_single;
1080 void drm_cdevpriv_dtor(void *cd);
1082 int drm_add_busid_modesetting(struct drm_device *dev,
1083 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1085 /* File operations helpers (drm_fops.c) */
1086 extern int drm_open_helper(struct cdev *kdev, int flags, int fmt,
1088 struct drm_device *dev,
1090 int drm_event_reserve_init_locked(struct drm_device *dev,
1091 struct drm_file *file_priv,
1092 struct drm_pending_event *p,
1093 struct drm_event *e);
1094 int drm_event_reserve_init(struct drm_device *dev,
1095 struct drm_file *file_priv,
1096 struct drm_pending_event *p,
1097 struct drm_event *e);
1098 void drm_event_cancel_free(struct drm_device *dev,
1099 struct drm_pending_event *p);
1100 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
1101 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
1103 /* Misc. IOCTL support (drm_ioctl.c) */
1104 int drm_noop(struct drm_device *dev, void *data,
1105 struct drm_file *file_priv);
1106 int drm_invalid_op(struct drm_device *dev, void *data,
1107 struct drm_file *file_priv);
1109 /* Cache management (drm_cache.c) */
1110 void drm_clflush_pages(struct vm_page *pages[], unsigned long num_pages);
1111 void drm_clflush_sg(struct sg_table *st);
1112 void drm_clflush_virt_range(void *addr, unsigned long length);
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, unsigned 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 u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
1136 struct timeval *vblanktime);
1137 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1138 struct drm_pending_vblank_event *e);
1139 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1140 struct drm_pending_vblank_event *e);
1141 extern void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
1142 struct drm_pending_vblank_event *e);
1143 extern void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1144 struct drm_pending_vblank_event *e);
1145 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
1146 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1147 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
1148 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
1149 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1150 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1151 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
1152 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1153 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
1154 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
1155 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1156 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1157 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1158 extern void drm_vblank_cleanup(struct drm_device *dev);
1159 extern u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe);
1161 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1162 unsigned int pipe, int *max_error,
1163 struct timeval *vblank_time,
1165 const struct drm_display_mode *mode);
1166 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1167 const struct drm_display_mode *mode);
1170 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1171 * @crtc: which CRTC's vblank waitqueue to retrieve
1173 * This function returns a pointer to the vblank waitqueue for the CRTC.
1174 * Drivers can use this to implement vblank waits using wait_event() & co.
1176 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1178 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1181 /* Modesetting support */
1182 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1183 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1185 /* Stub support (drm_stub.h) */
1187 extern void drm_put_dev(struct drm_device *dev);
1188 extern void drm_unplug_dev(struct drm_device *dev);
1189 extern unsigned int drm_debug;
1190 extern bool drm_atomic;
1192 /* consistent PCI memory functions (drm_pci.c) */
1193 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1195 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1197 /* sysfs support (drm_sysfs.c) */
1198 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1200 /* sysctl support (drm_sysctl.h) */
1201 extern int drm_sysctl_init(struct drm_device *dev);
1202 extern int drm_sysctl_cleanup(struct drm_device *dev);
1204 /* XXX: These are here only because of drm_sysctl.c */
1205 extern int drm_vblank_offdelay;
1206 extern unsigned int drm_timestamp_precision;
1208 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1209 vm_size_t size, struct vm_object **obj_res, int nprot);
1211 struct ttm_bo_device;
1212 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1213 vm_size_t size, struct vm_object **obj_res, int nprot);
1214 struct ttm_buffer_object;
1215 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1217 /* simplified version of kvasnrprintf() for drm needs. */
1218 char *drm_vasprintf(int flags, const char *format, __va_list ap) __printflike(2, 0);
1219 char *drm_asprintf(int flags, const char *format, ...) __printflike(2, 3);
1221 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1222 void drm_init_pdev(device_t dev, struct pci_dev **pdev);
1223 void drm_fini_pdev(struct pci_dev **pdev);
1224 void drm_print_pdev(struct pci_dev *pdev);
1226 /* Inline drm_free() helper for area kfree() */
1227 static __inline__ void
1228 drm_free(void *pt, struct malloc_type *area)
1230 /* kfree is special!!! */
1235 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1236 struct device *parent);
1237 void drm_dev_ref(struct drm_device *dev);
1238 void drm_dev_unref(struct drm_device *dev);
1239 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1240 void drm_dev_unregister(struct drm_device *dev);
1241 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1245 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1247 if (dev->driver->device_is_agp != NULL) {
1248 int err = (*dev->driver->device_is_agp) (dev);
1255 return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);
1258 #define DRM_PCIE_SPEED_25 1
1259 #define DRM_PCIE_SPEED_50 2
1260 #define DRM_PCIE_SPEED_80 4
1262 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1265 #define drm_can_sleep() (HZ & 1)
1267 #endif /* __KERNEL__ */
1269 /* helper for handling conditionals in various for_each macros */
1270 #define for_each_if(condition) if (!(condition)) {} else
1272 #endif /* _DRM_P_H_ */