nrelease - fix/improve livecd
[dragonfly.git] / sys / dev / drm / include / drm / drmP.h
1 /*
2  * Internal Header for the Direct Rendering Manager
3  *
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.
7  * All rights reserved.
8  *
9  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
10  * Author: Gareth Hughes <gareth@valinux.com>
11  *
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:
18  *
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
21  * Software.
22  *
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.
30  */
31
32 #ifndef _DRM_P_H_
33 #define _DRM_P_H_
34
35 #include <linux/agp_backend.h>
36 #include <linux/cdev.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/file.h>
39 #include <linux/fs.h>
40 #include <linux/highmem.h>
41 #include <linux/idr.h>
42 #include <linux/init.h>
43 #include <linux/io.h>
44 #include <linux/jiffies.h>
45 #include <linux/kernel.h>
46 #include <linux/kref.h>
47 #include <linux/miscdevice.h>
48 #include <linux/mm.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>
60
61 #include <asm/mman.h>
62 #include <asm/pgalloc.h>
63 #include <linux/uaccess.h>
64
65 #include <uapi/drm/drm.h>
66 #include <uapi/drm/drm_mode.h>
67
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>
85
86 #include <drm/drm_device.h>
87
88 #ifdef __DragonFly__
89 #include <sys/conf.h>
90 #include <sys/sysctl.h>
91
92 #include <vm/vm_extern.h>
93 #include <vm/vm_pager.h>
94 #endif
95
96 struct module;
97
98 struct device_node;
99 struct videomode;
100 struct reservation_object;
101 struct dma_buf_attachment;
102
103 struct pci_dev;
104 struct pci_controller;
105
106 /*
107  * The following categories are defined:
108  *
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.
111  *
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.
114  *
115  * KMS: used in the modesetting code.
116  *      This is the category used by the DRM_DEBUG_KMS() macro.
117  *
118  * PRIME: used in the prime code.
119  *        This is the category used by the DRM_DEBUG_PRIME() macro.
120  *
121  * ATOMIC: used in the atomic code.
122  *        This is the category used by the DRM_DEBUG_ATOMIC() macro.
123  *
124  * VBL: used for verbose debug message in the vblank code
125  *        This is the category used by the DRM_DEBUG_VBL() macro.
126  *
127  * DragonFly-specific categories:
128  *
129  * PID: used as modifier to include PID number in messages.
130  *        This is the category used by the all debug macros.
131  *
132  * FIOCTL: used in failed ioctl debugging.
133  *        This is the category used by the DRM_DEBUG_FIOCTL() macro.
134  *
135  * IOCTL: used in ioctl debugging.
136  *        This is the category used by the DRM_DEBUG_IOCTL() macro.
137  *
138  * Enabling verbose debug messages is done through the drm.debug parameter,
139  * each category being enabled by a bit.
140  *
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
144  * ...
145  * drm.debug=0x3f will enable all messages
146  *
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
150  */
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 */
161 #ifdef __DragonFly__
162 #define DRM_UT_PID              0x100
163 #define DRM_UT_FIOCTL           0x200
164 #define DRM_UT_IOCTL            0x400
165
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, ...);
171 #endif
172
173 /***********************************************************************/
174 /** \name DRM template customization defaults */
175 /*@{*/
176
177 /***********************************************************************/
178 /** \name Macros to make printk easier */
179 /*@{*/
180
181 #define _DRM_PRINTK(once, level, fmt, ...)                              \
182         do {                                                            \
183                 printk##once(KERN_##level "[" DRM_NAME "] " fmt,        \
184                              ##__VA_ARGS__);                            \
185         } while (0)
186
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__)
193
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__)
200
201 /**
202  * Error output.
203  *
204  * \param fmt printf() like format string.
205  * \param arg arguments
206  */
207 #define DRM_DEV_ERROR(dev, fmt, ...)                                    \
208         drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
209                        fmt, ##__VA_ARGS__)
210 #define DRM_ERROR(fmt, ...)                                             \
211         drm_err(__func__, fmt, ##__VA_ARGS__)
212
213 /**
214  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
215  *
216  * \param fmt printf() like format string.
217  * \param arg arguments
218  */
219 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...)                        \
220 ({                                                                      \
221         static DEFINE_RATELIMIT_STATE(_rs,                              \
222                                       DEFAULT_RATELIMIT_INTERVAL,       \
223                                       DEFAULT_RATELIMIT_BURST);         \
224                                                                         \
225         if (__ratelimit(&_rs))                                          \
226                 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__);                 \
227 })
228 #define DRM_ERROR_RATELIMITED(fmt, ...)                                 \
229         DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
230
231 #define DRM_DEV_INFO(dev, fmt, ...)                                     \
232         drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt,  \
233                        ##__VA_ARGS__)
234
235 #define DRM_DEV_INFO_ONCE(dev, fmt, ...)                                \
236 ({                                                                      \
237         static bool __print_once __read_mostly;                         \
238         if (!__print_once) {                                            \
239                 __print_once = true;                                    \
240                 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__);                  \
241         }                                                               \
242 })
243
244 /**
245  * Debug output.
246  *
247  * \param fmt printf() like format string.
248  * \param arg arguments
249  */
250 #define DRM_DEV_DEBUG(dev, fmt, args...)                                \
251         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
252                        ##args)
253 #define DRM_DEBUG(fmt, args...)                                         \
254         do {                                                            \
255                 if (unlikely(drm_debug & DRM_UT_CORE))                  \
256                         drm_ut_debug_printk(__func__, fmt, ##args);     \
257         } while (0)
258
259 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...)                         \
260         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "",    \
261                        fmt, ##args)
262 #define DRM_DEBUG_DRIVER(fmt, args...)                                  \
263         do {                                                            \
264                 if (unlikely(drm_debug & DRM_UT_DRIVER))                \
265                         drm_ut_debug_printk(__func__, fmt, ##args);     \
266         } while (0)
267
268 #define DRM_DEV_DEBUG_KMS(dev, fmt, args...)                            \
269         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt,  \
270                        ##args)
271 #define DRM_DEBUG_KMS(fmt, args...)                                     \
272         do {                                                            \
273                 if (unlikely(drm_debug & DRM_UT_KMS))                   \
274                         drm_ut_debug_printk(__func__, fmt, ##args);     \
275         } while (0)
276
277 #define DRM_DEV_DEBUG_PRIME(dev, fmt, args...)                          \
278         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "",     \
279                        fmt, ##args)
280 #define DRM_DEBUG_PRIME(fmt, args...)                                   \
281         do {                                                            \
282                 if (unlikely(drm_debug & DRM_UT_PRIME))                 \
283                         drm_ut_debug_printk(__func__, fmt, ##args);     \
284         } while (0)
285
286 #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...)                         \
287         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "",    \
288                        fmt, ##args)
289 #define DRM_DEBUG_ATOMIC(fmt, ...)                                              \
290         do {                                                                    \
291                 if (unlikely(drm_debug & DRM_UT_ATOMIC))                        \
292                         drm_ut_debug_printk(__func__, fmt, ##__VA_ARGS__);      \
293         } while(0)
294
295 #define DRM_DEV_DEBUG_VBL(dev, fmt, args...)                            \
296         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt,  \
297                        ##args)
298 #define DRM_DEBUG_VBL(fmt, args...)                                     \
299         do {                                                            \
300                 if (unlikely(drm_debug & DRM_UT_VBL))                   \
301                         drm_ut_debug_printk(__func__, fmt, ##args);     \
302         } while (0)
303
304 #ifdef __DragonFly__
305 #define DRM_DEBUG_FIOCTL(fmt, args...)                                  \
306         do {                                                            \
307                 if (unlikely(drm_debug & DRM_UT_FIOCTL))                \
308                         drm_ut_debug_printk(__func__, fmt, ##args);     \
309         } while (0)
310 #define DRM_DEBUG_IOCTL(fmt, args...)                                   \
311         do {                                                            \
312                 if (unlikely(drm_debug & DRM_UT_IOCTL))                 \
313                         drm_ut_debug_printk(__func__, fmt, ##args);     \
314         } while (0)
315 #define DRM_DEBUG_VBLANK        DRM_DEBUG_VBL
316 #endif  /* __DragonFly__ */
317
318 #define DRM_DEBUG_LEASE(fmt, ...)                                       \
319         drm_printk(KERN_DEBUG, DRM_UT_LEASE, fmt, ##__VA_ARGS__)
320
321 #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...)     \
322 ({                                                                      \
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);              \
329 })
330
331 /**
332  * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
333  *
334  * \param fmt printf() like format string.
335  * \param arg arguments
336  */
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)
353
354 /* Format strings and argument splitters to simplify printing
355  * various "complex" objects
356  */
357
358 /*@}*/
359
360 /***********************************************************************/
361 /** \name Internal types and structures */
362 /*@{*/
363
364 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
365
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
369
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)
374
375 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
376
377 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
378
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);
383
384 /* Flags and return codes for get_vblank_timestamp() driver function. */
385 #define DRM_CALLED_FROM_VBLIRQ 1
386
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)
391
392 #ifdef __DragonFly__
393 struct drm_sysctl_info {
394         struct sysctl_ctx_list ctx;
395         char   name[2];
396 };
397
398 /* Length for the array of resource pointers for drm_get_resource_*. */
399 #define DRM_MAX_PCI_RESOURCE    6
400 #endif
401
402 /**
403  * drm_drv_uses_atomic_modeset - check if the driver implements
404  * atomic_commit()
405  * @dev: DRM device
406  *
407  * This check is useful if drivers do not have DRIVER_ATOMIC set but
408  * have atomic modesetting internally implemented.
409  */
410 static inline bool drm_drv_uses_atomic_modeset(struct drm_device *dev)
411 {
412         return dev->mode_config.funcs->atomic_commit != NULL;
413 }
414
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
419
420 static __inline__ int drm_core_check_feature(struct drm_device *dev,
421                                              int feature)
422 {
423         return ((dev->driver->driver_features & feature) ? 1 : 0);
424 }
425
426 /******************************************************************/
427 /** \name Internal function definitions */
428 /*@{*/
429
430 #ifdef __DragonFly__
431 int    drm_create_cdevs(device_t kdev);
432
433 d_kqfilter_t drm_kqfilter;
434 d_mmap_t drm_mmap;
435 d_mmap_single_t drm_mmap_single;
436
437 int drm_device_detach(device_t kdev);
438
439 void drm_cdevpriv_dtor(void *cd);
440
441 int drm_add_busid_modesetting(struct drm_device *dev,
442     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
443
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);
448 #endif
449
450 /*
451  * These are exported to drivers so that they can implement fencing using
452  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
453  */
454
455 /*@}*/
456
457 /* returns true if currently okay to sleep */
458 static __inline__ bool drm_can_sleep(void)
459 {
460         if (in_atomic() || in_dbg_master() || irqs_disabled())
461                 return false;
462         return true;
463 }
464
465 /* helper for handling conditionals in various for_each macros */
466 #define for_each_if(condition) if (!(condition)) {} else
467
468 #ifdef __DragonFly__
469 struct drm_softc {
470         void *drm_driver_data;
471 };
472
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);
480
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);
483
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);
486
487 /* XXX: These are here only because of drm_sysctl.c */
488 extern int drm_vblank_offdelay;
489 extern unsigned int drm_timestamp_precision;
490
491 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
492 {
493         return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);
494 }
495 #endif
496
497 #endif