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/platform_device.h>
51 #include <linux/poll.h>
52 #include <linux/ratelimit.h>
53 #include <linux/sched.h>
54 #include <linux/slab.h>
55 #include <linux/types.h>
56 #include <linux/vmalloc.h>
57 #include <linux/workqueue.h>
58 #include <linux/dma-fence.h>
59 #include <linux/module.h>
62 #include <asm/pgalloc.h>
63 #include <linux/uaccess.h>
65 #include <uapi/drm/drm.h>
66 #include <uapi/drm/drm_mode.h>
68 #include <drm/drm_agpsupport.h>
69 #include <drm/drm_crtc.h>
70 #include <drm/drm_fourcc.h>
71 #include <drm/drm_global.h>
72 #include <drm/drm_hashtab.h>
73 #include <drm/drm_mem_util.h>
74 #include <drm/drm_mm.h>
75 #include <drm/drm_os_linux.h>
76 #include <drm/drm_sarea.h>
77 #include <drm/drm_drv.h>
78 #include <drm/drm_prime.h>
79 #include <drm/drm_pci.h>
80 #include <drm/drm_file.h>
87 struct drm_device_dma
;
88 struct drm_gem_object
;
90 struct drm_vblank_crtc
;
91 struct drm_vma_offset_manager
;
95 struct reservation_object
;
96 struct dma_buf_attachment
;
99 struct pci_controller
;
102 * The following categories are defined:
104 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
105 * This is the category used by the DRM_DEBUG() macro.
107 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
108 * This is the category used by the DRM_DEBUG_DRIVER() macro.
110 * KMS: used in the modesetting code.
111 * This is the category used by the DRM_DEBUG_KMS() macro.
113 * PRIME: used in the prime code.
114 * This is the category used by the DRM_DEBUG_PRIME() macro.
116 * ATOMIC: used in the atomic code.
117 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
119 * VBL: used for verbose debug message in the vblank code
120 * This is the category used by the DRM_DEBUG_VBL() macro.
122 * Enabling verbose debug messages is done through the drm.debug parameter,
123 * each category being enabled by a bit.
125 * drm.debug=0x1 will enable CORE messages
126 * drm.debug=0x2 will enable DRIVER messages
127 * drm.debug=0x3 will enable CORE and DRIVER messages
129 * drm.debug=0x3f will enable all messages
131 * An interesting feature is that it's possible to enable verbose logging at
132 * run-time by echoing the debug value in its sysfs node:
133 * # echo 0xf > /sys/module/drm/parameters/debug
135 #define DRM_UT_NONE 0x00
136 #define DRM_UT_CORE 0x01
137 #define DRM_UT_DRIVER 0x02
138 #define DRM_UT_KMS 0x04
139 #define DRM_UT_PRIME 0x08
140 #define DRM_UT_ATOMIC 0x10
141 #define DRM_UT_VBL 0x20
142 #define DRM_UT_STATE 0x40
144 /***********************************************************************/
145 /** \name DRM template customization defaults */
148 /***********************************************************************/
149 /** \name Macros to make printk easier */
152 #define _DRM_PRINTK(once, level, fmt, ...) \
154 printk##once(KERN_##level "[" DRM_NAME "] " fmt, \
158 #define DRM_INFO(fmt, ...) \
159 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
160 #define DRM_NOTE(fmt, ...) \
161 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
162 #define DRM_WARN(fmt, ...) \
163 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
165 #define DRM_INFO_ONCE(fmt, ...) \
166 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
167 #define DRM_NOTE_ONCE(fmt, ...) \
168 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
169 #define DRM_WARN_ONCE(fmt, ...) \
170 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
175 * \param fmt printf() like format string.
176 * \param arg arguments
178 #define DRM_DEV_ERROR(dev, fmt, ...) \
179 drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
181 #define DRM_ERROR(fmt, ...) \
182 drm_printk(KERN_ERR, DRM_UT_NONE, fmt, ##__VA_ARGS__)
185 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
187 * \param fmt printf() like format string.
188 * \param arg arguments
190 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
192 static DEFINE_RATELIMIT_STATE(_rs, \
193 DEFAULT_RATELIMIT_INTERVAL, \
194 DEFAULT_RATELIMIT_BURST); \
196 if (__ratelimit(&_rs)) \
197 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
199 #define DRM_ERROR_RATELIMITED(fmt, ...) \
200 DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
202 #define DRM_DEV_INFO(dev, fmt, ...) \
203 drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt, \
206 #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
208 static bool __print_once __read_mostly; \
209 if (!__print_once) { \
210 __print_once = true; \
211 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
218 * \param fmt printf() like format string.
219 * \param arg arguments
221 #define DRM_DEV_DEBUG(dev, fmt, args...) \
222 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
224 #define DRM_DEBUG(fmt, ...) \
225 drm_printk(KERN_DEBUG, DRM_UT_CORE, fmt, ##__VA_ARGS__)
227 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...) \
228 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "", \
230 #define DRM_DEBUG_DRIVER(fmt, ...) \
231 drm_printk(KERN_DEBUG, DRM_UT_DRIVER, fmt, ##__VA_ARGS__)
233 #define DRM_DEV_DEBUG_KMS(dev, fmt, args...) \
234 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt, \
236 #define DRM_DEBUG_KMS(fmt, ...) \
237 drm_printk(KERN_DEBUG, DRM_UT_KMS, fmt, ##__VA_ARGS__)
239 #define DRM_DEV_DEBUG_PRIME(dev, fmt, args...) \
240 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "", \
242 #define DRM_DEBUG_PRIME(fmt, ...) \
243 drm_printk(KERN_DEBUG, DRM_UT_PRIME, fmt, ##__VA_ARGS__)
245 #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...) \
246 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "", \
248 #define DRM_DEBUG_ATOMIC(fmt, ...) \
249 drm_printk(KERN_DEBUG, DRM_UT_ATOMIC, fmt, ##__VA_ARGS__)
251 #define DRM_DEV_DEBUG_VBL(dev, fmt, args...) \
252 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt, \
254 #define DRM_DEBUG_VBL(fmt, ...) \
255 drm_printk(KERN_DEBUG, DRM_UT_VBL, fmt, ##__VA_ARGS__)
257 #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...) \
259 static DEFINE_RATELIMIT_STATE(_rs, \
260 DEFAULT_RATELIMIT_INTERVAL, \
261 DEFAULT_RATELIMIT_BURST); \
262 if (__ratelimit(&_rs)) \
263 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ ## level, \
264 __func__, "", fmt, ##args); \
268 * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
270 * \param fmt printf() like format string.
271 * \param arg arguments
273 #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, args...) \
274 DEV__DRM_DEFINE_DEBUG_RATELIMITED(dev, CORE, fmt, ##args)
275 #define DRM_DEBUG_RATELIMITED(fmt, args...) \
276 DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##args)
277 #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, args...) \
278 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRIVER, fmt, ##args)
279 #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, args...) \
280 DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##args)
281 #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, args...) \
282 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, KMS, fmt, ##args)
283 #define DRM_DEBUG_KMS_RATELIMITED(fmt, args...) \
284 DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##args)
285 #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, args...) \
286 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, PRIME, fmt, ##args)
287 #define DRM_DEBUG_PRIME_RATELIMITED(fmt, args...) \
288 DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##args)
290 /* Format strings and argument splitters to simplify printing
291 * various "complex" objects
293 #define DRM_MODE_FMT "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x"
294 #define DRM_MODE_ARG(m) \
295 (m)->base.id, (m)->name, (m)->vrefresh, (m)->clock, \
296 (m)->hdisplay, (m)->hsync_start, (m)->hsync_end, (m)->htotal, \
297 (m)->vdisplay, (m)->vsync_start, (m)->vsync_end, (m)->vtotal, \
298 (m)->type, (m)->flags
300 #define DRM_RECT_FMT "%dx%d%+d%+d"
301 #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1
303 /* for rect's in fixed-point format: */
304 #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u"
305 #define DRM_RECT_FP_ARG(r) \
306 drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \
307 drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \
308 (r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \
309 (r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10
313 /***********************************************************************/
314 /** \name Internal types and structures */
317 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
320 * Ioctl function type.
322 * \param inode device inode.
323 * \param file_priv DRM file private pointer.
324 * \param cmd command.
325 * \param arg argument.
327 typedef int drm_ioctl_t(struct drm_device
*dev
, void *data
,
328 struct drm_file
*file_priv
);
330 typedef int drm_ioctl_compat_t(struct file
*filp
, unsigned int cmd
,
333 #define DRM_IOCTL_NR(n) _IOC_NR(n)
334 #define DRM_MAJOR 226
337 #define DRM_MASTER 0x2
338 #define DRM_ROOT_ONLY 0x4
339 #define DRM_CONTROL_ALLOW 0x8
340 #define DRM_UNLOCKED 0x10
341 #define DRM_RENDER_ALLOW 0x20
343 struct drm_ioctl_desc
{
351 * Creates a driver or general drm_ioctl_desc array entry for the given
352 * ioctl, for use by drm_ioctl().
355 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags) \
356 [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = { \
357 .cmd = DRM_IOCTL_##ioctl, \
363 /* Flags and return codes for get_vblank_timestamp() driver function. */
364 #define DRM_CALLED_FROM_VBLIRQ 1
365 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
366 #define DRM_VBLANKTIME_IN_VBLANK (1 << 1)
368 /* get_scanout_position() return flags */
369 #define DRM_SCANOUTPOS_VALID (1 << 0)
370 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
371 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
374 * Info file list entry. This structure represents a debugfs or proc file to
375 * be created by the drm core
377 struct drm_info_list
{
378 const char *name
; /** file name */
379 int (*show
)(struct seq_file
*, void*); /** show callback */
380 u32 driver_features
; /**< Required driver features for this entry */
385 * debugfs node structure. This structure represents a debugfs file.
387 struct drm_info_node
{
388 struct list_head list
;
389 struct drm_minor
*minor
;
390 const struct drm_info_list
*info_ent
;
395 * DRM device structure. This structure represent a complete card that
396 * may contain multiple heads.
399 struct list_head legacy_dev_list
;/**< list of devices per driver for stealth attach cleanup */
400 int if_version
; /**< Highest interface version set */
402 /** \name Lifetime Management */
404 struct kref ref
; /**< Object ref-count */
405 struct device
*dev
; /**< Device structure of bus-device */
406 struct drm_driver
*driver
; /**< DRM driver managing the device */
407 void *dev_private
; /**< DRM driver private data */
408 struct drm_minor
*control
; /**< Control node */
409 struct drm_minor
*primary
; /**< Primary node */
410 struct drm_minor
*render
; /**< Render node */
413 /* currently active master for this device. Protected by master_mutex */
414 struct drm_master
*master
;
416 atomic_t unplugged
; /**< Flag whether dev is dead */
417 struct inode
*anon_inode
; /**< inode for private address-space */
418 char *unique
; /**< unique name of the device */
423 struct mutex struct_mutex
; /**< For others */
424 struct mutex master_mutex
; /**< For drm_minor::master and drm_file::is_master */
427 /** \name Usage Counters */
429 int open_count
; /**< Outstanding files open, protected by drm_global_mutex. */
430 spinlock_t buf_lock
; /**< For drm_device::buf_use and a few other things. */
431 int buf_use
; /**< Buffers in use -- cannot alloc */
432 atomic_t buf_alloc
; /**< Buffer allocation in progress */
435 struct mutex filelist_mutex
;
436 struct list_head filelist
;
438 /** \name Memory management */
440 struct list_head maplist
; /**< Linked list of regions */
441 struct drm_open_hash map_hash
; /**< User token hash table for maps */
443 /** \name Context handle management */
445 struct list_head ctxlist
; /**< Linked list of context handles */
446 struct mutex ctxlist_mutex
; /**< For ctxlist */
450 struct list_head vmalist
; /**< List of vmas (for debugging) */
454 /** \name DMA support */
456 struct drm_device_dma
*dma
; /**< Optional pointer for DMA support */
459 /** \name Context support */
462 __volatile__
long context_flag
; /**< Context swapping flag */
463 int last_context
; /**< Last current context */
466 /** \name VBLANK IRQ support */
472 * If true, vblank interrupt will be disabled immediately when the
473 * refcount drops to zero, as opposed to via the vblank disable
475 * This can be set to true it the hardware has a working vblank
476 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
479 bool vblank_disable_immediate
;
481 /* array of size num_crtcs */
482 struct drm_vblank_crtc
*vblank
;
484 spinlock_t vblank_time_lock
; /**< Protects vblank count and time updates during vblank enable/disable */
487 u32 max_vblank_count
; /**< size of vblank counter register */
492 struct list_head vblank_event_list
;
493 spinlock_t event_lock
;
497 struct drm_agp_head
*agp
; /**< AGP data */
499 struct pci_dev
*pdev
; /**< PCI device structure */
501 struct pci_controller
*hose
;
504 struct platform_device
*platformdev
; /**< Platform device struture */
505 struct virtio_device
*virtdev
;
507 struct drm_sg_mem
*sg
; /**< Scatter gather memory */
508 unsigned int num_crtcs
; /**< Number of CRTCs on this device */
512 struct drm_hw_lock
*lock
;
515 struct drm_local_map
*agp_buffer_map
;
516 unsigned int agp_buffer_token
;
518 struct drm_mode_config mode_config
; /**< Current mode config */
520 /** \name GEM information */
522 struct mutex object_name_lock
;
523 struct idr object_name_idr
;
524 struct drm_vma_offset_manager
*vma_offset_manager
;
526 int switch_power_state
;
530 * drm_drv_uses_atomic_modeset - check if the driver implements
534 * This check is useful if drivers do not have DRIVER_ATOMIC set but
535 * have atomic modesetting internally implemented.
537 static inline bool drm_drv_uses_atomic_modeset(struct drm_device
*dev
)
539 return dev
->mode_config
.funcs
->atomic_commit
!= NULL
;
542 #include <drm/drm_irq.h>
544 #define DRM_SWITCH_POWER_ON 0
545 #define DRM_SWITCH_POWER_OFF 1
546 #define DRM_SWITCH_POWER_CHANGING 2
547 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
549 static __inline__
int drm_core_check_feature(struct drm_device
*dev
,
552 return ((dev
->driver
->driver_features
& feature
) ? 1 : 0);
555 static inline void drm_device_set_unplugged(struct drm_device
*dev
)
558 atomic_set(&dev
->unplugged
, 1);
561 static inline int drm_device_is_unplugged(struct drm_device
*dev
)
563 int ret
= atomic_read(&dev
->unplugged
);
568 /******************************************************************/
569 /** \name Internal function definitions */
572 /* Driver support (drm_drv.h) */
573 extern int drm_ioctl_permit(u32 flags
, struct drm_file
*file_priv
);
574 extern long drm_ioctl(struct file
*filp
,
575 unsigned int cmd
, unsigned long arg
);
577 extern long drm_compat_ioctl(struct file
*filp
,
578 unsigned int cmd
, unsigned long arg
);
580 /* Let drm_compat_ioctl be assigned to .compat_ioctl unconditionally */
581 #define drm_compat_ioctl NULL
583 extern bool drm_ioctl_flags(unsigned int nr
, unsigned int *flags
);
585 /* Misc. IOCTL support (drm_ioctl.c) */
586 int drm_noop(struct drm_device
*dev
, void *data
,
587 struct drm_file
*file_priv
);
588 int drm_invalid_op(struct drm_device
*dev
, void *data
,
589 struct drm_file
*file_priv
);
592 * These are exported to drivers so that they can implement fencing using
593 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
596 /* Debugfs support */
597 #if defined(CONFIG_DEBUG_FS)
598 extern int drm_debugfs_create_files(const struct drm_info_list
*files
,
599 int count
, struct dentry
*root
,
600 struct drm_minor
*minor
);
601 extern int drm_debugfs_remove_files(const struct drm_info_list
*files
,
602 int count
, struct drm_minor
*minor
);
604 static inline int drm_debugfs_create_files(const struct drm_info_list
*files
,
605 int count
, struct dentry
*root
,
606 struct drm_minor
*minor
)
611 static inline int drm_debugfs_remove_files(const struct drm_info_list
*files
,
612 int count
, struct drm_minor
*minor
)
618 /* sysfs support (drm_sysfs.c) */
619 extern void drm_sysfs_hotplug_event(struct drm_device
*dev
);
624 /* returns true if currently okay to sleep */
625 static __inline__
bool drm_can_sleep(void)
627 if (in_atomic() || in_dbg_master() || irqs_disabled())
632 /* helper for handling conditionals in various for_each macros */
633 #define for_each_if(condition) if (!(condition)) {} else