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_mm.h>
74 #include <drm/drm_os_linux.h>
75 #include <drm/drm_sarea.h>
76 #include <drm/drm_drv.h>
77 #include <drm/drm_prime.h>
78 #include <drm/drm_pci.h>
79 #include <drm/drm_file.h>
80 #include <drm/drm_debugfs.h>
81 #include <drm/drm_ioctl.h>
82 #include <drm/drm_sysfs.h>
83 #include <drm/drm_vblank.h>
84 #include <drm/drm_irq.h>
86 #include <drm/drm_device.h>
90 #include <sys/sysctl.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_pager.h>
100 struct reservation_object;
101 struct dma_buf_attachment;
104 struct pci_controller;
107 * The following categories are defined:
109 * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
110 * This is the category used by the DRM_DEBUG() macro.
112 * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
113 * This is the category used by the DRM_DEBUG_DRIVER() macro.
115 * KMS: used in the modesetting code.
116 * This is the category used by the DRM_DEBUG_KMS() macro.
118 * PRIME: used in the prime code.
119 * This is the category used by the DRM_DEBUG_PRIME() macro.
121 * ATOMIC: used in the atomic code.
122 * This is the category used by the DRM_DEBUG_ATOMIC() macro.
124 * VBL: used for verbose debug message in the vblank code
125 * This is the category used by the DRM_DEBUG_VBL() macro.
127 * DragonFly-specific categories:
129 * PID: used as modifier to include PID number in messages.
130 * This is the category used by the all debug macros.
132 * FIOCTL: used in failed ioctl debugging.
133 * This is the category used by the DRM_DEBUG_FIOCTL() macro.
135 * IOCTL: used in ioctl debugging.
136 * This is the category used by the DRM_DEBUG_IOCTL() macro.
138 * Enabling verbose debug messages is done through the drm.debug parameter,
139 * each category being enabled by a bit.
141 * drm.debug=0x1 will enable CORE messages
142 * drm.debug=0x2 will enable DRIVER messages
143 * drm.debug=0x3 will enable CORE and DRIVER messages
145 * drm.debug=0x3f will enable all messages
147 * An interesting feature is that it's possible to enable verbose logging at
148 * run-time by using the hw.drm.debug sysctl variable:
149 * # sysctl hw.drm.debug=0xfff
151 #define DRM_UT_NONE 0x00
152 #define DRM_UT_CORE 0x01
153 #define DRM_UT_DRIVER 0x02
154 #define DRM_UT_KMS 0x04
155 #define DRM_UT_PRIME 0x08
156 #define DRM_UT_ATOMIC 0x10
157 #define DRM_UT_VBL 0x20
158 #define DRM_UT_STATE 0x40
159 #define DRM_UT_LEASE 0x80
160 /* Extra DragonFly debug categories */
162 #define DRM_UT_PID 0x100
163 #define DRM_UT_FIOCTL 0x200
164 #define DRM_UT_IOCTL 0x400
166 extern __printf(2, 3)
167 void drm_ut_debug_printk(const char *function_name,
168 const char *format, ...);
169 extern __printf(2, 3)
170 void drm_err(const char *func, const char *format, ...);
173 /***********************************************************************/
174 /** \name DRM template customization defaults */
177 /***********************************************************************/
178 /** \name Macros to make printk easier */
181 #define _DRM_PRINTK(once, level, fmt, ...) \
183 printk##once(KERN_##level "[" DRM_NAME "] " fmt, \
187 #define DRM_INFO(fmt, ...) \
188 _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
189 #define DRM_NOTE(fmt, ...) \
190 _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
191 #define DRM_WARN(fmt, ...) \
192 _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
194 #define DRM_INFO_ONCE(fmt, ...) \
195 _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
196 #define DRM_NOTE_ONCE(fmt, ...) \
197 _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
198 #define DRM_WARN_ONCE(fmt, ...) \
199 _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
204 * \param fmt printf() like format string.
205 * \param arg arguments
207 #define DRM_DEV_ERROR(dev, fmt, ...) \
208 drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
210 #define DRM_ERROR(fmt, ...) \
211 drm_err(__func__, fmt, ##__VA_ARGS__)
214 * Rate limited error output. Like DRM_ERROR() but won't flood the log.
216 * \param fmt printf() like format string.
217 * \param arg arguments
219 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...) \
221 static DEFINE_RATELIMIT_STATE(_rs, \
222 DEFAULT_RATELIMIT_INTERVAL, \
223 DEFAULT_RATELIMIT_BURST); \
225 if (__ratelimit(&_rs)) \
226 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__); \
228 #define DRM_ERROR_RATELIMITED(fmt, ...) \
229 DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
231 #define DRM_DEV_INFO(dev, fmt, ...) \
232 drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt, \
235 #define DRM_DEV_INFO_ONCE(dev, fmt, ...) \
237 static bool __print_once __read_mostly; \
238 if (!__print_once) { \
239 __print_once = true; \
240 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__); \
247 * \param fmt printf() like format string.
248 * \param arg arguments
250 #define DRM_DEV_DEBUG(dev, fmt, args...) \
251 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
253 #define DRM_DEBUG(fmt, args...) \
255 if (unlikely(drm_debug & DRM_UT_CORE)) \
256 drm_ut_debug_printk(__func__, fmt, ##args); \
259 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...) \
260 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "", \
262 #define DRM_DEBUG_DRIVER(fmt, args...) \
264 if (unlikely(drm_debug & DRM_UT_DRIVER)) \
265 drm_ut_debug_printk(__func__, fmt, ##args); \
268 #define DRM_DEV_DEBUG_KMS(dev, fmt, args...) \
269 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt, \
271 #define DRM_DEBUG_KMS(fmt, args...) \
273 if (unlikely(drm_debug & DRM_UT_KMS)) \
274 drm_ut_debug_printk(__func__, fmt, ##args); \
277 #define DRM_DEV_DEBUG_PRIME(dev, fmt, args...) \
278 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "", \
280 #define DRM_DEBUG_PRIME(fmt, args...) \
282 if (unlikely(drm_debug & DRM_UT_PRIME)) \
283 drm_ut_debug_printk(__func__, fmt, ##args); \
286 #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...) \
287 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "", \
289 #define DRM_DEBUG_ATOMIC(fmt, ...) \
291 if (unlikely(drm_debug & DRM_UT_ATOMIC)) \
292 drm_ut_debug_printk(__func__, fmt, ##__VA_ARGS__); \
295 #define DRM_DEV_DEBUG_VBL(dev, fmt, args...) \
296 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt, \
298 #define DRM_DEBUG_VBL(fmt, args...) \
300 if (unlikely(drm_debug & DRM_UT_VBL)) \
301 drm_ut_debug_printk(__func__, fmt, ##args); \
305 #define DRM_DEBUG_FIOCTL(fmt, args...) \
307 if (unlikely(drm_debug & DRM_UT_FIOCTL)) \
308 drm_ut_debug_printk(__func__, fmt, ##args); \
310 #define DRM_DEBUG_IOCTL(fmt, args...) \
312 if (unlikely(drm_debug & DRM_UT_IOCTL)) \
313 drm_ut_debug_printk(__func__, fmt, ##args); \
315 #define DRM_DEBUG_VBLANK DRM_DEBUG_VBL
316 #endif /* __DragonFly__ */
318 #define DRM_DEBUG_LEASE(fmt, ...) \
319 drm_printk(KERN_DEBUG, DRM_UT_LEASE, fmt, ##__VA_ARGS__)
321 #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...) \
323 static DEFINE_RATELIMIT_STATE(_rs, \
324 DEFAULT_RATELIMIT_INTERVAL, \
325 DEFAULT_RATELIMIT_BURST); \
326 if (__ratelimit(&_rs)) \
327 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ ## level, \
328 __func__, "", fmt, ##args); \
332 * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
334 * \param fmt printf() like format string.
335 * \param arg arguments
337 #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, args...) \
338 DEV__DRM_DEFINE_DEBUG_RATELIMITED(dev, CORE, fmt, ##args)
339 #define DRM_DEBUG_RATELIMITED(fmt, args...) \
340 DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##args)
341 #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, args...) \
342 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRIVER, fmt, ##args)
343 #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, args...) \
344 DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##args)
345 #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, args...) \
346 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, KMS, fmt, ##args)
347 #define DRM_DEBUG_KMS_RATELIMITED(fmt, args...) \
348 DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##args)
349 #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, args...) \
350 _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, PRIME, fmt, ##args)
351 #define DRM_DEBUG_PRIME_RATELIMITED(fmt, args...) \
352 DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##args)
354 /* Format strings and argument splitters to simplify printing
355 * various "complex" objects
360 /***********************************************************************/
361 /** \name Internal types and structures */
364 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
366 #define DRM_DEV_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
367 #define DRM_DEV_UID UID_ROOT
368 #define DRM_DEV_GID GID_VIDEO
370 #define DRM_CURPROC curthread
371 #define DRM_STRUCTPROC struct thread
372 #define DRM_LOCK(dev) lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
373 #define DRM_UNLOCK(dev) lockmgr(&(dev)->struct_mutex, LK_RELEASE)
375 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
377 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
379 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
380 vm_memattr_t memattr);
381 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
382 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
384 /* Flags and return codes for get_vblank_timestamp() driver function. */
385 #define DRM_CALLED_FROM_VBLIRQ 1
387 /* get_scanout_position() return flags */
388 #define DRM_SCANOUTPOS_VALID (1 << 0)
389 #define DRM_SCANOUTPOS_IN_VBLANK (1 << 1)
390 #define DRM_SCANOUTPOS_ACCURATE (1 << 2)
393 struct drm_sysctl_info {
394 struct sysctl_ctx_list ctx;
398 /* Length for the array of resource pointers for drm_get_resource_*. */
399 #define DRM_MAX_PCI_RESOURCE 6
403 * drm_drv_uses_atomic_modeset - check if the driver implements
407 * This check is useful if drivers do not have DRIVER_ATOMIC set but
408 * have atomic modesetting internally implemented.
410 static inline bool drm_drv_uses_atomic_modeset(struct drm_device *dev)
412 return dev->mode_config.funcs->atomic_commit != NULL;
415 #define DRM_SWITCH_POWER_ON 0
416 #define DRM_SWITCH_POWER_OFF 1
417 #define DRM_SWITCH_POWER_CHANGING 2
418 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
420 static __inline__ int drm_core_check_feature(struct drm_device *dev,
423 return ((dev->driver->driver_features & feature) ? 1 : 0);
426 /******************************************************************/
427 /** \name Internal function definitions */
431 int drm_create_cdevs(device_t kdev);
433 d_kqfilter_t drm_kqfilter;
435 d_mmap_single_t drm_mmap_single;
437 int drm_device_detach(device_t kdev);
439 void drm_cdevpriv_dtor(void *cd);
441 int drm_add_busid_modesetting(struct drm_device *dev,
442 struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
444 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
445 void drm_init_pdev(device_t dev, struct pci_dev **pdev);
446 void drm_fini_pdev(struct pci_dev **pdev);
447 void drm_print_pdev(struct pci_dev *pdev);
451 * These are exported to drivers so that they can implement fencing using
452 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
457 /* returns true if currently okay to sleep */
458 static __inline__ bool drm_can_sleep(void)
460 if (in_atomic() || in_dbg_master() || irqs_disabled())
465 /* helper for handling conditionals in various for_each macros */
466 #define for_each_if(condition) if (!(condition)) {} else
470 void *drm_driver_data;
473 /* sysctl support (drm_sysctl.h) */
474 extern int drm_sysctl_init(struct drm_device *dev);
475 extern int drm_sysctl_cleanup(struct drm_device *dev);
476 unsigned long drm_get_resource_start(struct drm_device *dev,
477 unsigned int resource);
478 unsigned long drm_get_resource_len(struct drm_device *dev,
479 unsigned int resource);
481 int ttm_bo_mmap_single(struct file *fp, struct drm_device *dev, vm_ooffset_t *offset,
482 vm_size_t size, struct vm_object **obj_res, int nprot);
484 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
485 vm_size_t size, struct vm_object **obj_res, int nprot);
487 /* XXX: These are here only because of drm_sysctl.c */
488 extern int drm_vblank_offdelay;
489 extern unsigned int drm_timestamp_precision;
491 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
493 return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);