2 * Internal Header for the Direct Rendering Manager
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Copyright (c) 2009-2010, Code Aurora Forum.
9 * Author: Rickard E. (Rik) Faith <faith@valinux.com>
10 * Author: Gareth Hughes <gareth@valinux.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice (including the next
20 * paragraph) shall be included in all copies or substantial portions of the
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
35 #include <linux/agp_backend.h>
36 #include <linux/cdev.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/file.h>
40 #include <linux/highmem.h>
41 #include <linux/idr.h>
42 #include <linux/init.h>
44 #include <linux/jiffies.h>
45 #include <linux/kernel.h>
46 #include <linux/kref.h>
47 #include <linux/miscdevice.h>
49 #include <linux/mutex.h>
50 #include <linux/pci.h>
51 #include <linux/platform_device.h>
52 #include <linux/poll.h>
53 #include <linux/ratelimit.h>
54 #include <linux/rbtree.h>
55 #include <linux/sched.h>
56 #include <linux/slab.h>
57 #include <linux/seq_file.h>
58 #include <linux/types.h>
59 #include <linux/vmalloc.h>
60 #include <linux/workqueue.h>
61 #include <linux/fence.h>
63 #include <asm/pgalloc.h>
64 #include <asm/uaccess.h>
66 #include <uapi/drm/drm.h>
67 #include <uapi/drm/drm_mode.h>
69 #include <drm/drm_agpsupport.h>
70 #include <drm/drm_crtc.h>
71 #include <drm/drm_fourcc.h>
72 #include <drm/drm_global.h>
73 #include <drm/drm_hashtab.h>
74 #include <drm/drm_mem_util.h>
75 #include <drm/drm_mm.h>
76 #include <drm/drm_os_linux.h>
77 #include <drm/drm_sarea.h>
78 #include <drm/drm_vma_manager.h>
81 #include <sys/device.h>
82 #include <sys/sysctl.h>
84 #include <vm/vm_extern.h>
85 #include <vm/vm_pager.h>
93 struct drm_device_dma;
94 struct drm_dma_handle;
95 struct drm_gem_object;
97 struct drm_vblank_crtc;
101 struct reservation_object;
102 struct dma_buf_attachment;
105 * The following categories are defined:
107 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
108 * This is the category used by the DRM_DEBUG() macro.
110 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
111 * This is the category used by the DRM_DEBUG_DRIVER() macro.
113 * KMS: used in the modesetting code.
114 * This is the category used by the DRM_DEBUG_KMS() macro.
116 * PRIME: used in the prime code.
117 * This is the category used by the DRM_DEBUG_PRIME() macro.
119 * ATOMIC: used in the atomic code.
120 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
122 * VBL: used for verbose debug message in the vblank code
123 * This is the category used by the DRM_DEBUG_VBL() macro.
125 * DragonFly-specific categories:
127 * PID: used as modifier to include PID number in messages.
128 * This is the category used by the all debug macros.
130 * FIOCTL: used in failed ioctl debugging.
131 * This is the category used by the DRM_DEBUG_FIOCTL() macro.
133 * IOCTL: used in ioctl debugging.
134 * This is the category used by the DRM_DEBUG_IOCTL() macro.
136 * Enabling verbose debug messages is done through the drm.debug parameter,
137 * each category being enabled by a bit.
139 * drm.debug=0x1 will enable CORE messages
140 * drm.debug=0x2 will enable DRIVER messages
141 * drm.debug=0x3 will enable CORE and DRIVER messages
143 * drm.debug=0x3f will enable all messages
145 * An interesting feature is that it's possible to enable verbose logging at
146 * run-time by using hw.drm.debug sysctl variable:
147 * # sysctl hw.drm.debug=0xfff
149 #define DRM_UT_NONE 0x00
150 #define DRM_UT_CORE 0x01
151 #define DRM_UT_DRIVER 0x02
152 #define DRM_UT_KMS 0x04
153 #define DRM_UT_PRIME 0x08
154 #define DRM_UT_ATOMIC 0x10
155 #define DRM_UT_VBL 0x20
156 /* Extra DragonFly debug categories */
158 #define DRM_UT_RES7 0x40 /* reserved for future expansion */
159 #define DRM_UT_RES8 0x80 /* reserved for future expansion */
160 #define DRM_UT_PID 0x100
161 #define DRM_UT_FIOCTL 0x200
162 #define DRM_UT_IOCTL 0x400
165 extern __printflike(2, 3)
166 void drm_ut_debug_printk(const char *function_name,
167 const char *format, ...);
168 extern __printflike(2, 3)
169 void drm_err(const char *func, const char *format, ...);
171 extern __printf(6, 7)
172 void drm_dev_printk(const struct device *dev, const char *level,
173 unsigned int category, const char *function_name,
174 const char *prefix, const char *format, ...);
176 extern __printf(3, 4)
177 void drm_printk(const char *level, unsigned int category,
178 const char *format, ...);
180 /***********************************************************************/
181 /** \name DRM template customization defaults */
184 /* driver capabilities and requirements mask */
185 #define DRIVER_USE_AGP 0x1
186 #define DRIVER_LEGACY 0x2
187 #define DRIVER_PCI_DMA 0x8
188 #define DRIVER_SG 0x10
189 #define DRIVER_HAVE_DMA 0x20
190 #define DRIVER_HAVE_IRQ 0x40
191 #define DRIVER_IRQ_SHARED 0x80
192 #define DRIVER_GEM 0x1000
193 #define DRIVER_MODESET 0x2000
194 #define DRIVER_PRIME 0x4000
195 #define DRIVER_RENDER 0x8000
196 #define DRIVER_ATOMIC 0x10000
197 #define DRIVER_KMS_LEGACY_CONTEXT 0x20000
199 /***********************************************************************/
200 /** \name Macros to make printk easier */
203 #define _DRM_PRINTK(once, level, fmt, ...) \
205 printk##once(KERN_##level "[" DRM_NAME "] " fmt, \
209 #define DRM_INFO(fmt, ...) \
210 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
211 #define DRM_NOTE(fmt, ...) \
212 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
213 #define DRM_WARN(fmt, ...) \
214 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
216 #define DRM_INFO_ONCE(fmt, ...) \
217 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
218 #define DRM_NOTE_ONCE(fmt, ...) \
219 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
220 #define DRM_WARN_ONCE(fmt, ...) \
221 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
226 * \param fmt printf() like format string.
227 * \param arg arguments
229 #define DRM_DEV_ERROR(dev, fmt, ...) \
230 drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
232 #define DRM_ERROR(fmt, ...) \
233 drm_err(__func__, fmt, ##__VA_ARGS__)
236 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
238 * \param fmt printf() like format string.
239 * \param arg arguments
241 #define DRM_ERROR_RATELIMITED(fmt, ...) \
243 static struct krate krate_derr = { .freq = 1, .count = -16 }; \
245 krateprintf(&krate_derr, "error: [" DRM_NAME ":%s] *ERROR* " \
246 fmt, __func__, ##__VA_ARGS__); \
249 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
251 static DEFINE_RATELIMIT_STATE(_rs, \
252 DEFAULT_RATELIMIT_INTERVAL, \
253 DEFAULT_RATELIMIT_BURST); \
255 if (__ratelimit(&_rs)) \
256 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
259 #define DRM_DEV_INFO(dev, fmt, ...) \
260 drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt, \
263 #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
265 static bool __print_once __read_mostly; \
266 if (!__print_once) { \
267 __print_once = true; \
268 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
275 * \param fmt printf() like format string.
276 * \param arg arguments
278 #define DRM_DEV_DEBUG(dev, fmt, args...) \
279 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
281 #define DRM_DEBUG(fmt, args...) \
283 if (unlikely(drm_debug & DRM_UT_CORE)) \
284 drm_ut_debug_printk(__func__, fmt, ##args); \
287 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...) \
288 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "", \
290 #define DRM_DEBUG_DRIVER(fmt, args...) \
292 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
293 drm_ut_debug_printk(__func__, fmt, ##args); \
296 #define DRM_DEV_DEBUG_KMS(dev, fmt, args...) \
297 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt, \
299 #define DRM_DEBUG_KMS(fmt, args...) \
301 if (unlikely(drm_debug & DRM_UT_KMS)) \
302 drm_ut_debug_printk(__func__, fmt, ##args); \
305 #define DRM_DEV_DEBUG_PRIME(dev, fmt, args...) \
306 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "", \
309 #define DRM_DEBUG_PRIME(fmt, args...) \
311 if (unlikely(drm_debug & DRM_UT_PRIME)) \
312 drm_ut_debug_printk(__func__, fmt, ##args); \
315 #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...) \
316 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "", \
318 #define DRM_DEBUG_ATOMIC(fmt, args...) \
320 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
321 drm_ut_debug_printk(__func__, fmt, ##args); \
324 #define DRM_DEV_DEBUG_VBL(dev, fmt, args...) \
325 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt, \
327 #define DRM_DEBUG_VBL(fmt, args...) \
329 if (unlikely(drm_debug & DRM_UT_VBL)) \
330 drm_ut_debug_printk(__func__, fmt, ##args); \
334 #define DRM_DEBUG_FIOCTL(fmt, args...) \
336 if (unlikely(drm_debug & DRM_UT_FIOCTL)) \
337 drm_ut_debug_printk(__func__, fmt, ##args); \
339 #define DRM_DEBUG_IOCTL(fmt, args...) \
341 if (unlikely(drm_debug & DRM_UT_IOCTL)) \
342 drm_ut_debug_printk(__func__, fmt, ##args); \
344 #define DRM_DEBUG_VBLANK DRM_DEBUG_VBL
345 #endif /* __DragonFly__ */
347 #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...) \
349 static DEFINE_RATELIMIT_STATE(_rs, \
350 DEFAULT_RATELIMIT_INTERVAL, \
351 DEFAULT_RATELIMIT_BURST); \
352 if (__ratelimit(&_rs)) \
353 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ ## level, \
354 __func__, "", fmt, ##args); \
358 * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
360 * \param fmt printf() like format string.
361 * \param arg arguments
363 #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, args...) \
364 DEV__DRM_DEFINE_DEBUG_RATELIMITED(dev, CORE, fmt, ##args)
365 #define DRM_DEBUG_RATELIMITED(fmt, args...) \
366 DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##args)
367 #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, args...) \
368 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRIVER, fmt, ##args)
369 #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, args...) \
370 DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##args)
371 #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, args...) \
372 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, KMS, fmt, ##args)
373 #define DRM_DEBUG_KMS_RATELIMITED(fmt, args...) \
374 DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##args)
375 #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, args...) \
376 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, PRIME, fmt, ##args)
377 #define DRM_DEBUG_PRIME_RATELIMITED(fmt, args...) \
378 DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##args)
382 /***********************************************************************/
383 /** \name Internal types and structures */
386 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
388 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
389 #define DRM_DEV_UID UID_ROOT
390 #define DRM_DEV_GID GID_WHEEL
392 #define DRM_CURPROC curthread
393 #define DRM_STRUCTPROC struct thread
394 #define DRM_LOCK(dev) lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
395 #define DRM_UNLOCK(dev) lockmgr(&(dev)->struct_mutex, LK_RELEASE)
397 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
399 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
401 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
402 vm_memattr_t memattr);
403 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
404 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
406 typedef struct drm_pci_id_list
415 * Ioctl function type.
417 * \param inode device inode.
418 * \param file_priv DRM file private pointer.
419 * \param cmd command.
420 * \param arg argument.
422 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
423 struct drm_file *file_priv);
425 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
428 #define DRM_IOCTL_NR(n) ((n) & 0xff)
429 #define DRM_MAJOR 226
432 #define DRM_MASTER 0x2
433 #define DRM_ROOT_ONLY 0x4
434 #define DRM_CONTROL_ALLOW 0x8
435 #define DRM_UNLOCKED 0x10
436 #define DRM_RENDER_ALLOW 0x20
438 struct drm_ioctl_desc {
446 * Creates a driver or general drm_ioctl_desc array entry for the given
447 * ioctl, for use by drm_ioctl().
450 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
451 [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = { \
452 .cmd = DRM_IOCTL_##ioctl, \
458 /* Event queued up for userspace to read */
459 struct drm_pending_event {
460 struct completion *completion;
461 struct drm_event *event;
463 struct list_head link;
464 struct list_head pending_link;
465 struct drm_file *file_priv;
466 pid_t pid; /* pid of requester, no guarantee it's valid by the time
467 we deliver the event, for tracing only */
470 struct drm_prime_file_private {
472 struct rb_root dmabufs;
473 struct rb_root handles;
476 /** File private data */
478 unsigned authenticated :1;
479 /* true when the client has asked us to expose stereo 3D mode flags */
480 unsigned stereo_allowed :1;
482 * true if client understands CRTC primary planes and cursor planes
485 unsigned universal_planes:1;
486 /* true if client understands atomic properties */
489 * This client is the creator of @master.
490 * Protected by struct drm_device::master_mutex.
492 unsigned is_master:1;
496 struct list_head lhead;
497 struct drm_minor *minor;
498 unsigned long lock_count;
500 /** Mapping of mm object handles to object pointers. */
501 struct idr object_idr;
502 /** Lock for synchronization of access to object_idr. */
503 struct lock table_lock;
508 struct drm_master *master; /* master this node is currently associated with
509 N.B. not always dev->master */
511 * fbs - List of framebuffers associated with this file.
513 * Protected by fbs_lock. Note that the fbs list holds a reference on
514 * the fb object to prevent it from untimely disappearing.
516 struct list_head fbs;
517 struct lock fbs_lock;
519 /** User-created blob properties; this retains a reference on the
521 struct list_head blobs;
523 wait_queue_head_t event_wait;
524 struct list_head pending_event_list;
525 struct list_head event_list;
528 struct lock event_read_lock;
530 struct drm_prime_file_private prime;
533 struct drm_device *dev;
534 unsigned long ioctl_count;
542 struct drm_lock_data {
543 struct drm_hw_lock *hw_lock; /**< Hardware lock */
544 /** Private of lock holder's file (NULL=kernel) */
545 struct drm_file *file_priv;
546 wait_queue_head_t lock_queue; /**< Queue of blocked processes */
547 unsigned long lock_time; /**< Time of last lock in jiffies */
549 uint32_t kernel_waiters;
550 uint32_t user_waiters;
555 * GEM specific mm private for tracking GEM objects
558 struct drm_vma_offset_manager vma_manager;
559 struct drm_mm offset_manager; /**< Offset mgmt for buffer objects */
560 struct drm_open_hash offset_hash; /**< User token hash table for maps */
561 struct unrhdr *idxunr;
564 /* Flags and return codes for get_vblank_timestamp() driver function. */
565 #define DRM_CALLED_FROM_VBLIRQ 1
566 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
567 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
569 /* get_scanout_position() return flags */
570 #define DRM_SCANOUTPOS_VALID (1 << 0)
571 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
572 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
575 * DRM driver structure. This structure represent the common code for
576 * a family of cards. There will one drm_device for each card present
580 int (*load) (struct drm_device *, unsigned long flags);
581 int (*firstopen) (struct drm_device *);
582 int (*open) (struct drm_device *, struct drm_file *);
583 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
584 void (*postclose) (struct drm_device *, struct drm_file *);
585 void (*lastclose) (struct drm_device *);
586 int (*unload) (struct drm_device *);
587 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
588 int (*dma_quiescent) (struct drm_device *);
589 int (*context_dtor) (struct drm_device *dev, int context);
590 int (*set_busid)(struct drm_device *dev, struct drm_master *master);
593 * get_vblank_counter - get raw hardware vblank counter
595 * @pipe: counter to fetch
597 * Driver callback for fetching a raw hardware vblank counter for @crtc.
598 * If a device doesn't have a hardware counter, the driver can simply
599 * use drm_vblank_no_hw_counter() function. The DRM core will account for
600 * missed vblank events while interrupts where disabled based on system
603 * Wraparound handling and loss of events due to modesetting is dealt
604 * with in the DRM core code.
607 * Raw vblank counter value.
609 u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
612 * enable_vblank - enable vblank interrupt events
614 * @pipe: which irq to enable
616 * Enable vblank interrupts for @crtc. If the device doesn't have
617 * a hardware vblank counter, the driver should use the
618 * drm_vblank_no_hw_counter() function that keeps a virtual counter.
621 * Zero on success, appropriate errno if the given @crtc's vblank
622 * interrupt cannot be enabled.
624 int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
627 * disable_vblank - disable vblank interrupt events
629 * @pipe: which irq to enable
631 * Disable vblank interrupts for @crtc. If the device doesn't have
632 * a hardware vblank counter, the driver should use the
633 * drm_vblank_no_hw_counter() function that keeps a virtual counter.
635 void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
638 * Called by \c drm_device_is_agp. Typically used to determine if a
639 * card is really attached to AGP or not.
641 * \param dev DRM device handle
644 * One of three values is returned depending on whether or not the
645 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
646 * (return of 1), or may or may not be AGP (return of 2).
648 int (*device_is_agp) (struct drm_device *dev);
651 * Called by vblank timestamping code.
653 * Return the current display scanout position from a crtc, and an
654 * optional accurate ktime_get timestamp of when position was measured.
656 * \param dev DRM device.
657 * \param pipe Id of the crtc to query.
658 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
659 * \param *vpos Target location for current vertical scanout position.
660 * \param *hpos Target location for current horizontal scanout position.
661 * \param *stime Target location for timestamp taken immediately before
662 * scanout position query. Can be NULL to skip timestamp.
663 * \param *etime Target location for timestamp taken immediately after
664 * scanout position query. Can be NULL to skip timestamp.
665 * \param mode Current display timings.
667 * Returns vpos as a positive number while in active scanout area.
668 * Returns vpos as a negative number inside vblank, counting the number
669 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
670 * until start of active scanout / end of vblank."
672 * \return Flags, or'ed together as follows:
674 * DRM_SCANOUTPOS_VALID = Query successful.
675 * DRM_SCANOUTPOS_INVBL = Inside vblank.
676 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
677 * this flag means that returned position may be offset by a constant
678 * but unknown small number of scanlines wrt. real scanout position.
681 int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
682 unsigned int flags, int *vpos, int *hpos,
683 ktime_t *stime, ktime_t *etime,
684 const struct drm_display_mode *mode);
687 * Called by \c drm_get_last_vbltimestamp. Should return a precise
688 * timestamp when the most recent VBLANK interval ended or will end.
690 * Specifically, the timestamp in @vblank_time should correspond as
691 * closely as possible to the time when the first video scanline of
692 * the video frame after the end of VBLANK will start scanning out,
693 * the time immediately after end of the VBLANK interval. If the
694 * @crtc is currently inside VBLANK, this will be a time in the future.
695 * If the @crtc is currently scanning out a frame, this will be the
696 * past start time of the current scanout. This is meant to adhere
697 * to the OpenML OML_sync_control extension specification.
699 * \param dev dev DRM device handle.
700 * \param pipe crtc for which timestamp should be returned.
701 * \param *max_error Maximum allowable timestamp error in nanoseconds.
702 * Implementation should strive to provide timestamp
703 * with an error of at most *max_error nanoseconds.
704 * Returns true upper bound on error for timestamp.
705 * \param *vblank_time Target location for returned vblank timestamp.
706 * \param flags 0 = Defaults, no special treatment needed.
707 * \param DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
708 * irq handler. Some drivers need to apply some workarounds
709 * for gpu-specific vblank irq quirks if flag is set.
712 * Zero if timestamping isn't supported in current display mode or a
713 * negative number on failure. A positive status code on success,
714 * which describes how the vblank_time timestamp was computed.
716 int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
718 struct timeval *vblank_time,
721 /* these have to be filled in */
723 irqreturn_t(*irq_handler) (int irq, void *arg);
724 void (*irq_preinstall) (struct drm_device *dev);
725 int (*irq_postinstall) (struct drm_device *dev);
726 void (*irq_uninstall) (struct drm_device *dev);
728 /* Master routines */
729 int (*master_create)(struct drm_device *dev, struct drm_master *master);
730 void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
732 * master_set is called whenever the minor master is set.
733 * master_drop is called whenever the minor master is dropped.
736 int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
738 void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv);
740 int (*debugfs_init)(struct drm_minor *minor);
741 void (*debugfs_cleanup)(struct drm_minor *minor);
744 * @gem_free_object: deconstructor for drm_gem_objects
746 * This is deprecated and should not be used by new drivers. Use
747 * @gem_free_object_unlocked instead.
749 void (*gem_free_object) (struct drm_gem_object *obj);
752 * @gem_free_object_unlocked: deconstructor for drm_gem_objects
754 * This is for drivers which are not encumbered with dev->struct_mutex
755 * legacy locking schemes. Use this hook instead of @gem_free_object.
757 void (*gem_free_object_unlocked) (struct drm_gem_object *obj);
759 int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
760 void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
763 * Hook for allocating the GEM object struct, for use by core
766 struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
770 /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
771 int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
772 uint32_t handle, uint32_t flags, int *prime_fd);
773 /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
774 int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
775 int prime_fd, uint32_t *handle);
776 /* export GEM -> dmabuf */
777 struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
778 struct drm_gem_object *obj, int flags);
779 /* import dmabuf -> GEM */
780 struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
781 struct dma_buf *dma_buf);
782 /* low-level interface used by drm_gem_prime_{import,export} */
783 int (*gem_prime_pin)(struct drm_gem_object *obj);
784 void (*gem_prime_unpin)(struct drm_gem_object *obj);
785 struct reservation_object * (*gem_prime_res_obj)(
786 struct drm_gem_object *obj);
787 struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
788 struct drm_gem_object *(*gem_prime_import_sg_table)(
789 struct drm_device *dev,
790 struct dma_buf_attachment *attach,
791 struct sg_table *sgt);
792 void *(*gem_prime_vmap)(struct drm_gem_object *obj);
793 void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
794 int (*gem_prime_mmap)(struct drm_gem_object *obj,
795 struct vm_area_struct *vma);
797 /* vga arb irq handler */
798 void (*vgaarb_irq)(struct drm_device *dev, bool state);
800 /* dumb alloc support */
801 int (*dumb_create)(struct drm_file *file_priv,
802 struct drm_device *dev,
803 struct drm_mode_create_dumb *args);
804 int (*dumb_map_offset)(struct drm_file *file_priv,
805 struct drm_device *dev, uint32_t handle,
807 int (*dumb_destroy)(struct drm_file *file_priv,
808 struct drm_device *dev,
811 /* Driver private ops for this object */
812 struct cdev_pager_ops *gem_vm_ops;
823 const struct drm_ioctl_desc *ioctls;
825 const struct file_operations *fops;
827 /* List of devices hanging off this driver with stealth attach. */
828 struct list_head legacy_dev_list;
830 int (*sysctl_init) (struct drm_device *dev,
831 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
832 void (*sysctl_cleanup) (struct drm_device *dev);
833 #endif /* __DragonFly__ */
836 enum drm_minor_type {
844 * Info file list entry. This structure represents a debugfs or proc file to
845 * be created by the drm core
847 struct drm_info_list {
848 const char *name; /** file name */
849 int (*show)(struct seq_file*, void*); /** show callback */
850 u32 driver_features; /**< Required driver features for this entry */
855 * debugfs node structure. This structure represents a debugfs file.
857 struct drm_info_node {
858 struct list_head list;
859 struct drm_minor *minor;
860 const struct drm_info_list *info_ent;
865 * DRM minor structure. This structure represents a drm minor number.
868 int index; /**< Minor device number */
869 int type; /**< Control or render */
870 struct device *kdev; /**< Linux device */
871 struct drm_device *dev;
873 struct dentry *debugfs_root;
875 struct list_head debugfs_list;
876 struct lock debugfs_lock; /* Protects debugfs_list. */
879 struct drm_sysctl_info {
880 struct sysctl_ctx_list ctx;
884 /* Length for the array of resource pointers for drm_get_resource_*. */
885 #define DRM_MAX_PCI_RESOURCE 6
888 * DRM device structure. This structure represent a complete card that
889 * may contain multiple heads.
892 struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
893 int if_version; /**< Highest interface version set */
895 /** \name Lifetime Management */
897 struct kref ref; /**< Object ref-count */
898 struct device *dev; /**< Device structure of bus-device */
899 struct drm_driver *driver; /**< DRM driver managing the device */
900 void *dev_private; /**< DRM driver private data */
901 struct drm_minor *control; /**< Control node */
902 struct drm_minor *primary; /**< Primary node */
903 struct drm_minor *render; /**< Render node */
905 /* currently active master for this device. Protected by master_mutex */
906 struct drm_master *master;
908 atomic_t unplugged; /**< Flag whether dev is dead */
909 struct inode *anon_inode; /**< inode for private address-space */
910 char *unique; /**< unique name of the device */
915 struct lock struct_mutex; /**< For others */
916 struct lock master_mutex; /**< For drm_minor::master and drm_file::is_master */
919 /** \name Usage Counters */
921 int open_count; /**< Outstanding files open, protected by drm_global_mutex. */
922 spinlock_t buf_lock; /**< For drm_device::buf_use and a few other things. */
923 int buf_use; /**< Buffers in use -- cannot alloc */
924 atomic_t buf_alloc; /**< Buffer allocation in progress */
927 struct lock filelist_mutex;
928 struct list_head filelist;
930 /** \name Memory management */
932 struct list_head maplist; /**< Linked list of regions */
933 struct drm_open_hash map_hash; /**< User token hash table for maps */
935 /** \name Context handle management */
937 struct list_head ctxlist; /**< Linked list of context handles */
938 struct lock ctxlist_mutex; /**< For ctxlist */
942 struct list_head vmalist; /**< List of vmas (for debugging) */
946 struct drm_lock_data lock; /* Information on hardware lock */
948 /** \name DMA support */
950 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
953 /** \name Context support */
956 __volatile__ long context_flag; /**< Context swapping flag */
957 int last_context; /**< Last current context */
960 /** \name VBLANK IRQ support */
966 * If true, vblank interrupt will be disabled immediately when the
967 * refcount drops to zero, as opposed to via the vblank disable
969 * This can be set to true it the hardware has a working vblank
970 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
973 bool vblank_disable_immediate;
975 /* array of size num_crtcs */
976 struct drm_vblank_crtc *vblank;
978 struct lock vblank_time_lock; /**< Protects vblank count and time updates during vblank enable/disable */
979 struct lock vbl_lock;
981 u32 max_vblank_count; /**< size of vblank counter register */
986 struct list_head vblank_event_list;
987 struct lock event_lock;
991 struct drm_agp_head *agp; /**< AGP data */
993 struct pci_dev *pdev; /**< PCI device structure */
995 struct pci_controller *hose;
998 struct platform_device *platformdev; /**< Platform device struture */
999 struct virtio_device *virtdev;
1001 struct drm_sg_mem *sg; /**< Scatter gather memory */
1002 unsigned int num_crtcs; /**< Number of CRTCs on this device */
1006 struct drm_hw_lock *lock;
1009 struct drm_local_map *agp_buffer_map;
1010 unsigned int agp_buffer_token;
1012 struct drm_mode_config mode_config; /**< Current mode config */
1014 /** \name GEM information */
1016 struct lock object_name_lock;
1017 struct idr object_name_idr;
1018 struct drm_vma_offset_manager *vma_offset_manager;
1020 int switch_power_state;
1021 #ifdef __DragonFly__
1022 /* Storage of resource pointers for drm_get_resource_* */
1023 struct resource *pcir[DRM_MAX_PCI_RESOURCE];
1024 int pcirid[DRM_MAX_PCI_RESOURCE];
1030 char busid_str[128];
1033 struct drm_sysctl_info *sysctl;
1035 struct idr magic_map;
1037 int unique_len; /* Length of unique field */
1038 struct cdev *devnode; /* Device number for mknod */
1040 struct lwkt_serialize irq_lock; /* protects irq condition checks */
1042 struct sigio *buf_sigio; /* Processes waiting for SIGIO */
1047 #include <drm/drm_irq.h>
1049 #define DRM_SWITCH_POWER_ON 0
1050 #define DRM_SWITCH_POWER_OFF 1
1051 #define DRM_SWITCH_POWER_CHANGING 2
1052 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1054 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1057 return ((dev->driver->driver_features & feature) ? 1 : 0);
1060 static inline void drm_device_set_unplugged(struct drm_device *dev)
1063 atomic_set(&dev->unplugged, 1);
1066 static inline int drm_device_is_unplugged(struct drm_device *dev)
1068 int ret = atomic_read(&dev->unplugged);
1073 static inline bool drm_is_render_client(const struct drm_file *file_priv)
1075 return file_priv->minor->type == DRM_MINOR_RENDER;
1078 static inline bool drm_is_control_client(const struct drm_file *file_priv)
1080 return file_priv->minor->type == DRM_MINOR_CONTROL;
1083 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1085 return file_priv->minor->type == DRM_MINOR_PRIMARY;
1088 /******************************************************************/
1089 /** \name Internal function definitions */
1092 /* Driver support (drm_drv.h) */
1093 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1094 int drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1095 int drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1096 int drm_create_cdevs(device_t kdev);
1097 d_ioctl_t drm_ioctl;
1098 extern long drm_compat_ioctl(struct file *filp,
1099 unsigned int cmd, unsigned long arg);
1100 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1102 /* File Operations (drm_fops.c) */
1104 d_close_t drm_close;
1106 int drm_release(struct inode *inode, struct file *filp);
1108 /* DragonFly driver framework */
1109 #ifdef __DragonFly__
1110 d_kqfilter_t drm_kqfilter;
1112 d_mmap_single_t drm_mmap_single;
1114 int drm_device_detach(device_t kdev);
1117 void drm_cdevpriv_dtor(void *cd);
1119 int drm_add_busid_modesetting(struct drm_device *dev,
1120 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1122 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
1123 int drm_event_reserve_init_locked(struct drm_device *dev,
1124 struct drm_file *file_priv,
1125 struct drm_pending_event *p,
1126 struct drm_event *e);
1127 int drm_event_reserve_init(struct drm_device *dev,
1128 struct drm_file *file_priv,
1129 struct drm_pending_event *p,
1130 struct drm_event *e);
1131 void drm_event_cancel_free(struct drm_device *dev,
1132 struct drm_pending_event *p);
1133 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
1134 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
1136 /* Misc. IOCTL support (drm_ioctl.c) */
1137 int drm_noop(struct drm_device *dev, void *data,
1138 struct drm_file *file_priv);
1139 int drm_invalid_op(struct drm_device *dev, void *data,
1140 struct drm_file *file_priv);
1142 /* Cache management (drm_cache.c) */
1143 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
1144 void drm_clflush_sg(struct sg_table *st);
1145 void drm_clflush_virt_range(void *addr, unsigned long length);
1148 * These are exported to drivers so that they can implement fencing using
1149 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1152 /* Modesetting support */
1153 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1154 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1157 void drm_put_dev(struct drm_device *dev);
1158 void drm_unplug_dev(struct drm_device *dev);
1159 extern unsigned int drm_debug;
1161 /* Debugfs support */
1162 #if defined(CONFIG_DEBUG_FS)
1163 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1164 int count, struct dentry *root,
1165 struct drm_minor *minor);
1166 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1167 int count, struct drm_minor *minor);
1169 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1170 int count, struct dentry *root,
1171 struct drm_minor *minor)
1176 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1177 int count, struct drm_minor *minor)
1183 struct dma_buf_export_info;
1185 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1186 struct drm_gem_object *obj,
1188 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1189 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1191 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1192 struct dma_buf *dma_buf);
1193 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1194 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1195 struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
1196 struct dma_buf_export_info *exp_info);
1197 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1199 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1200 dma_addr_t *addrs, int max_pages);
1201 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages);
1202 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1205 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1207 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1209 /* sysfs support (drm_sysfs.c) */
1210 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1213 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1214 void drm_init_pdev(device_t dev, struct pci_dev **pdev);
1215 void drm_fini_pdev(struct pci_dev **pdev);
1216 void drm_print_pdev(struct pci_dev *pdev);
1218 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1219 struct device *parent);
1220 int drm_dev_init(struct drm_device *dev,
1221 struct drm_driver *driver,
1222 struct device *parent);
1223 void drm_dev_ref(struct drm_device *dev);
1224 void drm_dev_unref(struct drm_device *dev);
1225 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1226 void drm_dev_unregister(struct drm_device *dev);
1228 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1229 void drm_minor_release(struct drm_minor *minor);
1234 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1236 if (dev->driver->device_is_agp != NULL) {
1237 int err = (*dev->driver->device_is_agp) (dev);
1244 return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);
1246 void drm_pci_agp_destroy(struct drm_device *dev);
1248 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1249 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1251 extern int drm_get_pci_dev(struct pci_dev *pdev,
1252 const struct pci_device_id *ent,
1253 struct drm_driver *driver);
1254 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1256 static inline int drm_get_pci_dev(struct pci_dev *pdev,
1257 const struct pci_device_id *ent,
1258 struct drm_driver *driver)
1263 static inline int drm_pci_set_busid(struct drm_device *dev,
1264 struct drm_master *master)
1270 #define DRM_PCIE_SPEED_25 1
1271 #define DRM_PCIE_SPEED_50 2
1272 #define DRM_PCIE_SPEED_80 4
1274 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1275 extern int drm_pcie_get_max_link_width(struct drm_device *dev, u32 *mlw);
1277 /* platform section */
1278 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1280 /* returns true if currently okay to sleep */
1281 static __inline__ bool drm_can_sleep(void)
1284 if (in_atomic() || in_dbg_master() || irqs_disabled())
1290 /* helper for handling conditionals in various for_each macros */
1291 #define for_each_if(condition) if (!(condition)) {} else
1293 #ifdef __DragonFly__
1295 void *drm_driver_data;
1298 /* sysctl support (drm_sysctl.h) */
1299 extern int drm_sysctl_init(struct drm_device *dev);
1300 extern int drm_sysctl_cleanup(struct drm_device *dev);
1302 unsigned long drm_get_resource_start(struct drm_device *dev,
1303 unsigned int resource);
1304 unsigned long drm_get_resource_len(struct drm_device *dev,
1305 unsigned int resource);
1307 int ttm_bo_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1308 vm_size_t size, struct vm_object **obj_res, int nprot);
1310 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1311 vm_size_t size, struct vm_object **obj_res, int nprot);
1313 /* XXX: These are here only because of drm_sysctl.c */
1314 extern int drm_vblank_offdelay;
1315 extern unsigned int drm_timestamp_precision;