Upgrade ldns and drill(1). 1/2
[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/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/types.h>
58 #include <linux/vmalloc.h>
59 #include <linux/workqueue.h>
60 #include <linux/dma-fence.h>
61
62 #include <asm/mman.h>
63 #include <asm/pgalloc.h>
64 #include <linux/uaccess.h>
65
66 #include <uapi/drm/drm.h>
67 #include <uapi/drm/drm_mode.h>
68
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>
79 #include <drm/drm_drv.h>
80
81 #include <sys/conf.h>
82 #include <sys/device.h>
83 #include <sys/sysctl.h>
84
85 #include <vm/vm_extern.h>
86 #include <vm/vm_pager.h>
87
88 struct module;
89
90 struct drm_file;
91 struct drm_device;
92 struct drm_agp_head;
93 struct drm_local_map;
94 struct drm_device_dma;
95 struct drm_dma_handle;
96 struct drm_gem_object;
97 struct drm_master;
98 struct drm_vblank_crtc;
99
100 struct device_node;
101 struct videomode;
102 struct reservation_object;
103 struct dma_buf_attachment;
104
105 /*
106  * The following categories are defined:
107  *
108  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
109  *       This is the category used by the DRM_DEBUG() macro.
110  *
111  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
112  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
113  *
114  * KMS: used in the modesetting code.
115  *      This is the category used by the DRM_DEBUG_KMS() macro.
116  *
117  * PRIME: used in the prime code.
118  *        This is the category used by the DRM_DEBUG_PRIME() macro.
119  *
120  * ATOMIC: used in the atomic code.
121  *        This is the category used by the DRM_DEBUG_ATOMIC() macro.
122  *
123  * VBL: used for verbose debug message in the vblank code
124  *        This is the category used by the DRM_DEBUG_VBL() macro.
125  *
126  * DragonFly-specific categories:
127  *
128  * PID: used as modifier to include PID number in messages.
129  *        This is the category used by the all debug macros.
130  *
131  * FIOCTL: used in failed ioctl debugging.
132  *        This is the category used by the DRM_DEBUG_FIOCTL() macro.
133  *
134  * IOCTL: used in ioctl debugging.
135  *        This is the category used by the DRM_DEBUG_IOCTL() macro.
136  *
137  * Enabling verbose debug messages is done through the drm.debug parameter,
138  * each category being enabled by a bit.
139  *
140  * drm.debug=0x1 will enable CORE messages
141  * drm.debug=0x2 will enable DRIVER messages
142  * drm.debug=0x3 will enable CORE and DRIVER messages
143  * ...
144  * drm.debug=0x3f will enable all messages
145  *
146  * An interesting feature is that it's possible to enable verbose logging at
147  * run-time by using the hw.drm.debug sysctl variable:
148  *   # sysctl hw.drm.debug=0xfff
149  */
150 #define DRM_UT_NONE             0x00
151 #define DRM_UT_CORE             0x01
152 #define DRM_UT_DRIVER           0x02
153 #define DRM_UT_KMS              0x04
154 #define DRM_UT_PRIME            0x08
155 #define DRM_UT_ATOMIC           0x10
156 #define DRM_UT_VBL              0x20
157 #define DRM_UT_STATE            0x40
158 /* Extra DragonFly debug categories */
159 #ifdef __DragonFly__
160 #define DRM_UT_RES8             0x80    /* reserved for future expansion */
161 #define DRM_UT_PID              0x100
162 #define DRM_UT_FIOCTL           0x200
163 #define DRM_UT_IOCTL            0x400
164 #endif
165
166 extern __printflike(2, 3)
167 void drm_ut_debug_printk(const char *function_name,
168                          const char *format, ...);
169 extern __printflike(2, 3)
170 void drm_err(const char *func, const char *format, ...);
171
172 /***********************************************************************/
173 /** \name DRM template customization defaults */
174 /*@{*/
175
176 /***********************************************************************/
177 /** \name Macros to make printk easier */
178 /*@{*/
179
180 #define _DRM_PRINTK(once, level, fmt, ...)                              \
181         do {                                                            \
182                 printk##once(KERN_##level "[" DRM_NAME "] " fmt,        \
183                              ##__VA_ARGS__);                            \
184         } while (0)
185
186 #define DRM_INFO(fmt, ...)                                              \
187         _DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
188 #define DRM_NOTE(fmt, ...)                                              \
189         _DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
190 #define DRM_WARN(fmt, ...)                                              \
191         _DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
192
193 #define DRM_INFO_ONCE(fmt, ...)                                         \
194         _DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
195 #define DRM_NOTE_ONCE(fmt, ...)                                         \
196         _DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
197 #define DRM_WARN_ONCE(fmt, ...)                                         \
198         _DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
199
200 /**
201  * Error output.
202  *
203  * \param fmt printf() like format string.
204  * \param arg arguments
205  */
206 #define DRM_DEV_ERROR(dev, fmt, ...)                                    \
207         drm_dev_printk(dev, KERN_ERR, DRM_UT_NONE, __func__, " *ERROR*",\
208                        fmt, ##__VA_ARGS__)
209 #define DRM_ERROR(fmt, ...)                                             \
210         drm_printk(KERN_ERR, DRM_UT_NONE, fmt,  ##__VA_ARGS__)
211
212 /**
213  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
214  *
215  * \param fmt printf() like format string.
216  * \param arg arguments
217  */
218 #define DRM_DEV_ERROR_RATELIMITED(dev, fmt, ...)                        \
219 ({                                                                      \
220         static DEFINE_RATELIMIT_STATE(_rs,                              \
221                                       DEFAULT_RATELIMIT_INTERVAL,       \
222                                       DEFAULT_RATELIMIT_BURST);         \
223                                                                         \
224         if (__ratelimit(&_rs))                                          \
225                 DRM_DEV_ERROR(dev, fmt, ##__VA_ARGS__);                 \
226 })
227 #define DRM_ERROR_RATELIMITED(fmt, ...)                                 \
228         DRM_DEV_ERROR_RATELIMITED(NULL, fmt, ##__VA_ARGS__)
229
230 #define DRM_DEV_INFO(dev, fmt, ...)                                     \
231         drm_dev_printk(dev, KERN_INFO, DRM_UT_NONE, __func__, "", fmt,  \
232                        ##__VA_ARGS__)
233
234 #define DRM_DEV_INFO_ONCE(dev, fmt, ...)                                \
235 ({                                                                      \
236         static bool __print_once __read_mostly;                         \
237         if (!__print_once) {                                            \
238                 __print_once = true;                                    \
239                 DRM_DEV_INFO(dev, fmt, ##__VA_ARGS__);                  \
240         }                                                               \
241 })
242
243 /**
244  * Debug output.
245  *
246  * \param fmt printf() like format string.
247  * \param arg arguments
248  */
249 #define DRM_DEV_DEBUG(dev, fmt, args...)                                \
250         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_CORE, __func__, "", fmt, \
251                        ##args)
252 #define DRM_DEBUG(fmt, args...)                                         \
253         do {                                                            \
254                 if (unlikely(drm_debug & DRM_UT_CORE))                  \
255                         drm_ut_debug_printk(__func__, fmt, ##args);     \
256         } while (0)
257
258 #define DRM_DEV_DEBUG_DRIVER(dev, fmt, args...)                         \
259         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_DRIVER, __func__, "",    \
260                        fmt, ##args)
261 #define DRM_DEBUG_DRIVER(fmt, args...)                                  \
262         do {                                                            \
263                 if (unlikely(drm_debug & DRM_UT_DRIVER))                \
264                         drm_ut_debug_printk(__func__, fmt, ##args);     \
265         } while (0)
266
267 #define DRM_DEV_DEBUG_KMS(dev, fmt, args...)                            \
268         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_KMS, __func__, "", fmt,  \
269                        ##args)
270 #define DRM_DEBUG_KMS(fmt, args...)                                     \
271         do {                                                            \
272                 if (unlikely(drm_debug & DRM_UT_KMS))                   \
273                         drm_ut_debug_printk(__func__, fmt, ##args);     \
274         } while (0)
275
276 #define DRM_DEV_DEBUG_PRIME(dev, fmt, args...)                          \
277         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_PRIME, __func__, "",     \
278                        fmt, ##args)
279 #define DRM_DEBUG_PRIME(fmt, args...)                                   \
280         do {                                                            \
281                 if (unlikely(drm_debug & DRM_UT_PRIME))                 \
282                         drm_ut_debug_printk(__func__, fmt, ##args);     \
283         } while (0)
284
285 #define DRM_DEV_DEBUG_ATOMIC(dev, fmt, args...)                         \
286         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ATOMIC, __func__, "",    \
287                        fmt, ##args)
288 #define DRM_DEBUG_ATOMIC(fmt, args...)                                  \
289         do {                                                            \
290                 if (unlikely(drm_debug & DRM_UT_ATOMIC))                \
291                         drm_ut_debug_printk(__func__, fmt, ##args);     \
292         } while (0)
293
294 #define DRM_DEV_DEBUG_VBL(dev, fmt, args...)                            \
295         drm_dev_printk(dev, KERN_DEBUG, DRM_UT_VBL, __func__, "", fmt,  \
296                        ##args)
297 #define DRM_DEBUG_VBL(fmt, args...)                                     \
298         do {                                                            \
299                 if (unlikely(drm_debug & DRM_UT_VBL))                   \
300                         drm_ut_debug_printk(__func__, fmt, ##args);     \
301         } while (0)
302
303 #ifdef __DragonFly__
304 #define DRM_DEBUG_FIOCTL(fmt, args...)                                  \
305         do {                                                            \
306                 if (unlikely(drm_debug & DRM_UT_FIOCTL))                \
307                         drm_ut_debug_printk(__func__, fmt, ##args);     \
308         } while (0)
309 #define DRM_DEBUG_IOCTL(fmt, args...)                                   \
310         do {                                                            \
311                 if (unlikely(drm_debug & DRM_UT_IOCTL))                 \
312                         drm_ut_debug_printk(__func__, fmt, ##args);     \
313         } while (0)
314 #define DRM_DEBUG_VBLANK        DRM_DEBUG_VBL
315 #endif  /* __DragonFly__ */
316
317 #define _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, level, fmt, args...)     \
318 ({                                                                      \
319         static DEFINE_RATELIMIT_STATE(_rs,                              \
320                                       DEFAULT_RATELIMIT_INTERVAL,       \
321                                       DEFAULT_RATELIMIT_BURST);         \
322         if (__ratelimit(&_rs))                                          \
323                 drm_dev_printk(dev, KERN_DEBUG, DRM_UT_ ## level,       \
324                                __func__, "", fmt, ##args);              \
325 })
326
327 /**
328  * Rate limited debug output. Like DRM_DEBUG() but won't flood the log.
329  *
330  * \param fmt printf() like format string.
331  * \param arg arguments
332  */
333 #define DRM_DEV_DEBUG_RATELIMITED(dev, fmt, args...)                    \
334         DEV__DRM_DEFINE_DEBUG_RATELIMITED(dev, CORE, fmt, ##args)
335 #define DRM_DEBUG_RATELIMITED(fmt, args...)                             \
336         DRM_DEV_DEBUG_RATELIMITED(NULL, fmt, ##args)
337 #define DRM_DEV_DEBUG_DRIVER_RATELIMITED(dev, fmt, args...)             \
338         _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, DRIVER, fmt, ##args)
339 #define DRM_DEBUG_DRIVER_RATELIMITED(fmt, args...)                      \
340         DRM_DEV_DEBUG_DRIVER_RATELIMITED(NULL, fmt, ##args)
341 #define DRM_DEV_DEBUG_KMS_RATELIMITED(dev, fmt, args...)                \
342         _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, KMS, fmt, ##args)
343 #define DRM_DEBUG_KMS_RATELIMITED(fmt, args...)                         \
344         DRM_DEV_DEBUG_KMS_RATELIMITED(NULL, fmt, ##args)
345 #define DRM_DEV_DEBUG_PRIME_RATELIMITED(dev, fmt, args...)              \
346         _DRM_DEV_DEFINE_DEBUG_RATELIMITED(dev, PRIME, fmt, ##args)
347 #define DRM_DEBUG_PRIME_RATELIMITED(fmt, args...)                       \
348         DRM_DEV_DEBUG_PRIME_RATELIMITED(NULL, fmt, ##args)
349
350 /* Format strings and argument splitters to simplify printing
351  * various "complex" objects
352  */
353 #define DRM_MODE_FMT    "%d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x"
354 #define DRM_MODE_ARG(m) \
355         (m)->base.id, (m)->name, (m)->vrefresh, (m)->clock, \
356         (m)->hdisplay, (m)->hsync_start, (m)->hsync_end, (m)->htotal, \
357         (m)->vdisplay, (m)->vsync_start, (m)->vsync_end, (m)->vtotal, \
358         (m)->type, (m)->flags
359
360 #define DRM_RECT_FMT    "%dx%d%+d%+d"
361 #define DRM_RECT_ARG(r) drm_rect_width(r), drm_rect_height(r), (r)->x1, (r)->y1
362
363 /* for rect's in fixed-point format: */
364 #define DRM_RECT_FP_FMT "%d.%06ux%d.%06u%+d.%06u%+d.%06u"
365 #define DRM_RECT_FP_ARG(r) \
366                 drm_rect_width(r) >> 16, ((drm_rect_width(r) & 0xffff) * 15625) >> 10, \
367                 drm_rect_height(r) >> 16, ((drm_rect_height(r) & 0xffff) * 15625) >> 10, \
368                 (r)->x1 >> 16, (((r)->x1 & 0xffff) * 15625) >> 10, \
369                 (r)->y1 >> 16, (((r)->y1 & 0xffff) * 15625) >> 10
370
371 /*@}*/
372
373 /***********************************************************************/
374 /** \name Internal types and structures */
375 /*@{*/
376
377 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
378
379 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
380 #define DRM_DEV_UID     UID_ROOT
381 #define DRM_DEV_GID     GID_WHEEL
382
383 #define DRM_CURPROC             curthread
384 #define DRM_STRUCTPROC          struct thread
385 #define DRM_LOCK(dev)           lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
386 #define DRM_UNLOCK(dev)         lockmgr(&(dev)->struct_mutex, LK_RELEASE)
387
388 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
389
390 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
391
392 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
393     vm_memattr_t memattr);
394 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
395 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
396
397 /**
398  * Ioctl function type.
399  *
400  * \param inode device inode.
401  * \param file_priv DRM file private pointer.
402  * \param cmd command.
403  * \param arg argument.
404  */
405 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
406                         struct drm_file *file_priv);
407
408 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
409                                unsigned long arg);
410
411 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
412 #define DRM_MAJOR       226
413
414 #define DRM_AUTH        0x1
415 #define DRM_MASTER      0x2
416 #define DRM_ROOT_ONLY   0x4
417 #define DRM_CONTROL_ALLOW 0x8
418 #define DRM_UNLOCKED    0x10
419 #define DRM_RENDER_ALLOW 0x20
420
421 struct drm_ioctl_desc {
422         unsigned int cmd;
423         int flags;
424         drm_ioctl_t *func;
425         const char *name;
426 };
427
428 /**
429  * Creates a driver or general drm_ioctl_desc array entry for the given
430  * ioctl, for use by drm_ioctl().
431  */
432
433 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                         \
434         [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = {        \
435                 .cmd = DRM_IOCTL_##ioctl,                               \
436                 .func = _func,                                          \
437                 .flags = _flags,                                        \
438                 .name = #ioctl                                          \
439          }
440
441 /* Event queued up for userspace to read */
442 struct drm_pending_event {
443         struct completion *completion;
444         void (*completion_release)(struct completion *completion);
445         struct drm_event *event;
446         struct dma_fence *fence;
447         struct list_head link;
448         struct list_head pending_link;
449         struct drm_file *file_priv;
450         pid_t pid; /* pid of requester, no guarantee it's valid by the time
451                       we deliver the event, for tracing only */
452 };
453
454 struct drm_prime_file_private {
455         struct lock lock;
456         struct rb_root dmabufs;
457         struct rb_root handles;
458 };
459
460 /** File private data */
461 struct drm_file {
462         unsigned authenticated :1;
463         /* true when the client has asked us to expose stereo 3D mode flags */
464         unsigned stereo_allowed :1;
465         /*
466          * true if client understands CRTC primary planes and cursor planes
467          * in the plane list
468          */
469         unsigned universal_planes:1;
470         /* true if client understands atomic properties */
471         unsigned atomic:1;
472         /*
473          * This client is the creator of @master.
474          * Protected by struct drm_device::master_mutex.
475          */
476         unsigned is_master:1;
477
478         pid_t pid;
479         drm_magic_t magic;
480         struct list_head lhead;
481         struct drm_minor *minor;
482         unsigned long lock_count;
483
484         /** Mapping of mm object handles to object pointers. */
485         struct idr object_idr;
486         /** Lock for synchronization of access to object_idr. */
487         spinlock_t table_lock;
488
489         struct file *filp;
490         void *driver_priv;
491
492         struct drm_master *master; /* master this node is currently associated with
493                                       N.B. not always dev->master */
494         /**
495          * fbs - List of framebuffers associated with this file.
496          *
497          * Protected by fbs_lock. Note that the fbs list holds a reference on
498          * the fb object to prevent it from untimely disappearing.
499          */
500         struct list_head fbs;
501         struct lock fbs_lock;
502
503         /** User-created blob properties; this retains a reference on the
504          *  property. */
505         struct list_head blobs;
506
507         wait_queue_head_t event_wait;
508         struct list_head pending_event_list;
509         struct list_head event_list;
510         int event_space;
511
512         struct lock event_read_lock;
513
514         struct drm_prime_file_private prime;
515
516 #ifdef __DragonFly__
517         struct drm_device *dev;
518         unsigned long ioctl_count;
519         struct kqinfo     dkq;
520 #endif
521 };
522
523 /**
524  * Lock data.
525  */
526 struct drm_lock_data {
527         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
528         /** Private of lock holder's file (NULL=kernel) */
529         struct drm_file *file_priv;
530         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
531         unsigned long lock_time;        /**< Time of last lock in jiffies */
532         spinlock_t spinlock;
533         uint32_t kernel_waiters;
534         uint32_t user_waiters;
535         int idle_has_lock;
536 };
537
538 /**
539  * GEM specific mm private for tracking GEM objects
540  */
541 struct drm_gem_mm {
542         struct drm_vma_offset_manager vma_manager;
543         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
544         struct drm_open_hash offset_hash; /**< User token hash table for maps */
545         struct unrhdr *idxunr;
546 };
547
548 /* Flags and return codes for get_vblank_timestamp() driver function. */
549 #define DRM_CALLED_FROM_VBLIRQ 1
550 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
551 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
552
553 /* get_scanout_position() return flags */
554 #define DRM_SCANOUTPOS_VALID        (1 << 0)
555 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
556 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
557
558 enum drm_minor_type {
559         DRM_MINOR_PRIMARY,
560         DRM_MINOR_CONTROL,
561         DRM_MINOR_RENDER,
562         DRM_MINOR_CNT,
563 };
564
565 /**
566  * Info file list entry. This structure represents a debugfs or proc file to
567  * be created by the drm core
568  */
569 struct drm_info_list {
570         const char *name; /** file name */
571         int (*show)(struct seq_file*, void*); /** show callback */
572         u32 driver_features; /**< Required driver features for this entry */
573         void *data;
574 };
575
576 /**
577  * debugfs node structure. This structure represents a debugfs file.
578  */
579 struct drm_info_node {
580         struct list_head list;
581         struct drm_minor *minor;
582         const struct drm_info_list *info_ent;
583         struct dentry *dent;
584 };
585
586 /**
587  * DRM minor structure. This structure represents a drm minor number.
588  */
589 struct drm_minor {
590         int index;                      /**< Minor device number */
591         int type;                       /**< Control or render */
592         struct device *kdev;            /**< Linux device */
593         struct drm_device *dev;
594
595         struct dentry *debugfs_root;
596
597         struct list_head debugfs_list;
598         struct lock debugfs_lock; /* Protects debugfs_list. */
599 };
600
601 struct drm_sysctl_info {
602         struct sysctl_ctx_list ctx;
603         char   name[2];
604 };
605
606 /* Length for the array of resource pointers for drm_get_resource_*. */
607 #define DRM_MAX_PCI_RESOURCE    6
608
609 /**
610  * DRM device structure. This structure represent a complete card that
611  * may contain multiple heads.
612  */
613 struct drm_device {
614         struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
615         int if_version;                 /**< Highest interface version set */
616
617         /** \name Lifetime Management */
618         /*@{ */
619         struct kref ref;                /**< Object ref-count */
620         struct device *dev;             /**< Device structure of bus-device */
621         struct drm_driver *driver;      /**< DRM driver managing the device */
622         void *dev_private;              /**< DRM driver private data */
623         struct drm_minor *control;              /**< Control node */
624         struct drm_minor *primary;              /**< Primary node */
625         struct drm_minor *render;               /**< Render node */
626         bool registered;
627
628         /* currently active master for this device. Protected by master_mutex */
629         struct drm_master *master;
630
631         atomic_t unplugged;                     /**< Flag whether dev is dead */
632         struct inode *anon_inode;               /**< inode for private address-space */
633         char *unique;                           /**< unique name of the device */
634         /*@} */
635
636         /** \name Locks */
637         /*@{ */
638         struct lock struct_mutex;       /**< For others */
639         struct lock master_mutex;      /**< For drm_minor::master and drm_file::is_master */
640         /*@} */
641
642         /** \name Usage Counters */
643         /*@{ */
644         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
645         spinlock_t buf_lock;            /**< For drm_device::buf_use and a few other things. */
646         int buf_use;                    /**< Buffers in use -- cannot alloc */
647         atomic_t buf_alloc;             /**< Buffer allocation in progress */
648         /*@} */
649
650         struct lock filelist_mutex;
651         struct list_head filelist;
652
653         /** \name Memory management */
654         /*@{ */
655         struct list_head maplist;       /**< Linked list of regions */
656         struct drm_open_hash map_hash;  /**< User token hash table for maps */
657
658         /** \name Context handle management */
659         /*@{ */
660         struct list_head ctxlist;       /**< Linked list of context handles */
661         struct lock ctxlist_mutex;      /**< For ctxlist */
662
663         struct idr ctx_idr;
664
665         struct list_head vmalist;       /**< List of vmas (for debugging) */
666
667         /*@} */
668
669         /** \name DMA support */
670         /*@{ */
671         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
672         /*@} */
673
674         /** \name Context support */
675         /*@{ */
676
677         __volatile__ long context_flag; /**< Context swapping flag */
678         int last_context;               /**< Last current context */
679         /*@} */
680
681         /** \name VBLANK IRQ support */
682         /*@{ */
683         bool irq_enabled;
684         int irq;
685
686         /*
687          * If true, vblank interrupt will be disabled immediately when the
688          * refcount drops to zero, as opposed to via the vblank disable
689          * timer.
690          * This can be set to true it the hardware has a working vblank
691          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
692          * appropriately.
693          */
694         bool vblank_disable_immediate;
695
696         /* array of size num_crtcs */
697         struct drm_vblank_crtc *vblank;
698
699         spinlock_t vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
700         spinlock_t vbl_lock;
701
702         u32 max_vblank_count;           /**< size of vblank counter register */
703
704         /**
705          * List of events
706          */
707         struct list_head vblank_event_list;
708         spinlock_t event_lock;
709
710         /*@} */
711
712         struct drm_agp_head *agp;       /**< AGP data */
713
714         struct pci_dev *pdev;           /**< PCI device structure */
715 #ifdef __alpha__
716         struct pci_controller *hose;
717 #endif
718
719         struct platform_device *platformdev; /**< Platform device struture */
720         struct virtio_device *virtdev;
721
722         struct drm_sg_mem *sg;  /**< Scatter gather memory */
723         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
724
725         struct {
726                 int context;
727                 struct drm_hw_lock *lock;
728         } sigdata;
729
730         struct drm_local_map *agp_buffer_map;
731         unsigned int agp_buffer_token;
732
733         struct drm_mode_config mode_config;     /**< Current mode config */
734
735         /** \name GEM information */
736         /*@{ */
737         struct lock object_name_lock;
738         struct idr object_name_idr;
739         struct drm_vma_offset_manager *vma_offset_manager;
740         /*@} */
741         int switch_power_state;
742 #ifdef __DragonFly__
743         /* Storage of resource pointers for drm_get_resource_* */
744         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
745         int               pcirid[DRM_MAX_PCI_RESOURCE];
746
747         int               pci_domain;
748         int               pci_bus;
749         int               pci_slot;
750         int               pci_func;
751         char busid_str[128];
752         int modesetting;
753         void             *mm_private;
754         struct drm_sysctl_info *sysctl;
755
756         struct idr magic_map;
757
758         int               unique_len;   /* Length of unique field          */
759         struct cdev       *devnode;     /* Device number for mknod         */
760
761         struct lwkt_serialize irq_lock; /* protects irq condition checks */
762
763         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
764         void              *drm_ttm_bdev;
765
766         struct drm_lock_data lock;      /* Information on hardware lock    */
767 #endif
768 };
769
770 #include <drm/drm_irq.h>
771
772 #define DRM_SWITCH_POWER_ON 0
773 #define DRM_SWITCH_POWER_OFF 1
774 #define DRM_SWITCH_POWER_CHANGING 2
775 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
776
777 static __inline__ int drm_core_check_feature(struct drm_device *dev,
778                                              int feature)
779 {
780         return ((dev->driver->driver_features & feature) ? 1 : 0);
781 }
782
783 static inline void drm_device_set_unplugged(struct drm_device *dev)
784 {
785         smp_wmb();
786         atomic_set(&dev->unplugged, 1);
787 }
788
789 static inline int drm_device_is_unplugged(struct drm_device *dev)
790 {
791         int ret = atomic_read(&dev->unplugged);
792         smp_rmb();
793         return ret;
794 }
795
796 static inline bool drm_is_render_client(const struct drm_file *file_priv)
797 {
798         return file_priv->minor->type == DRM_MINOR_RENDER;
799 }
800
801 static inline bool drm_is_control_client(const struct drm_file *file_priv)
802 {
803         return file_priv->minor->type == DRM_MINOR_CONTROL;
804 }
805
806 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
807 {
808         return file_priv->minor->type == DRM_MINOR_PRIMARY;
809 }
810
811 /******************************************************************/
812 /** \name Internal function definitions */
813 /*@{*/
814
815                                 /* Driver support (drm_drv.h) */
816 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
817 int     drm_create_cdevs(device_t kdev);
818 d_ioctl_t drm_ioctl;
819 #ifdef CONFIG_COMPAT
820 extern long drm_compat_ioctl(struct file *filp,
821                              unsigned int cmd, unsigned long arg);
822 #else
823 /* Let drm_compat_ioctl be assigned to .compat_ioctl unconditionally */
824 #define drm_compat_ioctl NULL
825 #endif
826 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
827
828 /* File Operations (drm_fops.c) */
829 d_open_t drm_open;
830 d_close_t drm_close;
831 d_read_t drm_read;
832 int drm_release(struct inode *inode, struct file *filp);
833
834 /* DragonFly driver framework */
835 #ifdef __DragonFly__
836 d_kqfilter_t drm_kqfilter;
837 d_mmap_t drm_mmap;
838 d_mmap_single_t drm_mmap_single;
839
840 int drm_device_detach(device_t kdev);
841
842 void drm_cdevpriv_dtor(void *cd);
843
844 int drm_add_busid_modesetting(struct drm_device *dev,
845     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
846 #endif
847
848 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
849 int drm_event_reserve_init_locked(struct drm_device *dev,
850                                   struct drm_file *file_priv,
851                                   struct drm_pending_event *p,
852                                   struct drm_event *e);
853 int drm_event_reserve_init(struct drm_device *dev,
854                            struct drm_file *file_priv,
855                            struct drm_pending_event *p,
856                            struct drm_event *e);
857 void drm_event_cancel_free(struct drm_device *dev,
858                            struct drm_pending_event *p);
859 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
860 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
861
862 /* Misc. IOCTL support (drm_ioctl.c) */
863 int drm_noop(struct drm_device *dev, void *data,
864              struct drm_file *file_priv);
865 int drm_invalid_op(struct drm_device *dev, void *data,
866                    struct drm_file *file_priv);
867
868 /* Cache management (drm_cache.c) */
869 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
870 void drm_clflush_sg(struct sg_table *st);
871 void drm_clflush_virt_range(void *addr, unsigned long length);
872
873 /*
874  * These are exported to drivers so that they can implement fencing using
875  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
876  */
877
878                                 /* Debugfs support */
879 #if defined(CONFIG_DEBUG_FS)
880 extern int drm_debugfs_create_files(const struct drm_info_list *files,
881                                     int count, struct dentry *root,
882                                     struct drm_minor *minor);
883 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
884                                     int count, struct drm_minor *minor);
885 #else
886 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
887                                            int count, struct dentry *root,
888                                            struct drm_minor *minor)
889 {
890         return 0;
891 }
892
893 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
894                                            int count, struct drm_minor *minor)
895 {
896         return 0;
897 }
898 #endif
899
900 struct dma_buf_export_info;
901
902 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
903                                             struct drm_gem_object *obj,
904                                             int flags);
905 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
906                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
907                 int *prime_fd);
908 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
909                 struct dma_buf *dma_buf);
910 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
911                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
912 struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
913                                       struct dma_buf_export_info *exp_info);
914 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
915
916 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
917                                             dma_addr_t *addrs, int max_pages);
918 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages);
919 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
920
921
922 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
923                                             size_t align);
924 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
925
926                                /* sysfs support (drm_sysfs.c) */
927 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
928
929
930 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
931 void drm_init_pdev(device_t dev, struct pci_dev **pdev);
932 void drm_fini_pdev(struct pci_dev **pdev);
933 void drm_print_pdev(struct pci_dev *pdev);
934
935 int drm_dev_set_unique(struct drm_device *dev, const char *name);
936
937 /*@}*/
938
939 /* PCI section */
940 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
941 {
942         if (dev->driver->device_is_agp != NULL) {
943                 int err = (*dev->driver->device_is_agp) (dev);
944
945                 if (err != 2) {
946                         return err;
947                 }
948         }
949
950         return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);
951 }
952 void drm_pci_agp_destroy(struct drm_device *dev);
953
954 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
955 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
956 #ifdef CONFIG_PCI
957 extern int drm_get_pci_dev(struct pci_dev *pdev,
958                            const struct pci_device_id *ent,
959                            struct drm_driver *driver);
960 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
961 #else
962 static inline int drm_get_pci_dev(struct pci_dev *pdev,
963                                   const struct pci_device_id *ent,
964                                   struct drm_driver *driver)
965 {
966         return -ENOSYS;
967 }
968
969 static inline int drm_pci_set_busid(struct drm_device *dev,
970                                     struct drm_master *master)
971 {
972         return -ENOSYS;
973 }
974 #endif
975
976 #define DRM_PCIE_SPEED_25 1
977 #define DRM_PCIE_SPEED_50 2
978 #define DRM_PCIE_SPEED_80 4
979
980 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
981 extern int drm_pcie_get_max_link_width(struct drm_device *dev, u32 *mlw);
982
983 /* platform section */
984 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
985
986 /* returns true if currently okay to sleep */
987 static __inline__ bool drm_can_sleep(void)
988 {
989         if (in_atomic() || in_dbg_master() || irqs_disabled())
990                 return false;
991         return true;
992 }
993
994 /* helper for handling conditionals in various for_each macros */
995 #define for_each_if(condition) if (!(condition)) {} else
996
997 #ifdef __DragonFly__
998 struct drm_softc {
999         void *drm_driver_data;
1000 };
1001
1002 /* sysctl support (drm_sysctl.h) */
1003 extern int drm_sysctl_init(struct drm_device *dev);
1004 extern int drm_sysctl_cleanup(struct drm_device *dev);
1005
1006 unsigned long drm_get_resource_start(struct drm_device *dev,
1007                                      unsigned int resource);
1008 unsigned long drm_get_resource_len(struct drm_device *dev,
1009                                    unsigned int resource);
1010
1011 int ttm_bo_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1012     vm_size_t size, struct vm_object **obj_res, int nprot);
1013
1014 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1015     vm_size_t size, struct vm_object **obj_res, int nprot);
1016
1017 /* XXX: These are here only because of drm_sysctl.c */
1018 extern int drm_vblank_offdelay;
1019 extern unsigned int drm_timestamp_precision;
1020 #endif
1021
1022 #endif