45137fb908d0556f36e6759767f0584770ffcc00
[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 #if defined(_KERNEL) || defined(__KERNEL__)
36
37 #include <sys/param.h>
38 #include <sys/queue.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/module.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/sglist.h>
46 #include <sys/stat.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/lock.h>
50 #include <sys/spinlock.h>
51 #include <sys/spinlock2.h>
52 #include <sys/fcntl.h>
53 #include <sys/uio.h>
54 #include <sys/filio.h>
55 #include <sys/sysctl.h>
56 #include <sys/bus.h>
57 #include <sys/signalvar.h>
58 #include <sys/poll.h>
59 #include <linux/highmem.h>
60 #include <sys/sbuf.h>
61 #include <sys/taskqueue.h>
62 #include <sys/tree.h>
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page2.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vm_param.h>
72 #include <machine/param.h>
73 #include <machine/pmap.h>
74 #ifdef __x86_64__
75 #include <machine/specialreg.h>
76 #endif
77 #include <machine/sysarch.h>
78 #include <sys/endian.h>
79 #include <sys/mman.h>
80 #include <sys/rman.h>
81 #include <sys/memrange.h>
82 #include <sys/mutex.h>
83
84 #include <linux/agp_backend.h>
85 #include <linux/atomic.h>
86 #include <linux/bug.h>
87 #include <linux/dma-mapping.h>
88 #include <linux/capability.h>
89 #include <linux/err.h>
90 #include <linux/idr.h>
91 #include <linux/pci.h>
92 #include <linux/jiffies.h>
93 #include <linux/kernel.h>
94 #include <linux/fs.h>
95 #include <linux/kref.h>
96 #include <linux/list.h>
97 #include <linux/mm.h>
98 #include <linux/moduleparam.h>
99 #include <linux/mutex.h>
100 #include <linux/slab.h>
101 #include <linux/scatterlist.h>
102 #include <linux/timer.h>
103 #include <asm/io.h>
104 #include <linux/seq_file.h>
105 #include <linux/types.h>
106 #include <linux/vmalloc.h>
107 #include <linux/wait.h>
108 #include <linux/workqueue.h>
109
110 #include <asm/uaccess.h>
111
112 #include <uapi_drm/drm.h>
113 #include <uapi_drm/drm_mode.h>
114
115 #include <drm/drm_agpsupport.h>
116 #include <drm/drm_crtc.h>
117 #include <drm/drm_global.h>
118 #include <drm/drm_hashtab.h>
119 #include <drm/drm_mem_util.h>
120 #include <drm/drm_mm.h>
121 #include <drm/drm_os_linux.h>
122 #include <uapi_drm/drm_sarea.h>
123 #include <drm/drm_vma_manager.h>
124
125 struct drm_file;
126 struct drm_device;
127 struct drm_agp_head;
128 struct drm_local_map;
129 struct drm_device_dma;
130 struct drm_dma_handle;
131 struct drm_gem_object;
132
133 struct device_node;
134 #ifdef CONFIG_VIDEOMODE_HELPERS
135 struct videomode;       /* XXX empty struct in videomode.h ? */
136 #endif
137 struct reservation_object;
138 struct dma_buf_attachment;
139
140 /*
141  * 4 debug categories are defined:
142  *
143  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
144  *       This is the category used by the DRM_DEBUG() macro.
145  *
146  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
147  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
148  *
149  * KMS: used in the modesetting code.
150  *      This is the category used by the DRM_DEBUG_KMS() macro.
151  *
152  * PRIME: used in the prime code.
153  *        This is the category used by the DRM_DEBUG_PRIME() macro.
154  *
155  * ATOMIC: used in the atomic code.
156  *        This is the category used by the DRM_DEBUG_ATOMIC() macro.
157  *
158  *
159  * PID: used as modifier to include PID number in messages.
160  *        This is the category used by the all debug macros.
161  *
162  * FIOCTL: used in failed ioctl debugging.
163  *        This is the category used by the DRM_DEBUG_FIOCTL() macro.
164  *
165  * IOCTL: used in ioctl debugging.
166  *        This is the category used by the DRM_DEBUG_IOCTL() macro.
167  *
168  * VBLANK: used in vblank debugging.
169  *        This is the category used by the DRM_DEBUG_VBLANK() macro.
170  *
171  * Enabling verbose debug messages is done through the drm.debug parameter,
172  * each category being enabled by a bit.
173  *
174  * drm.debug=0x1 will enable CORE messages
175  * drm.debug=0x2 will enable DRIVER messages
176  * drm.debug=0x3 will enable CORE and DRIVER messages
177  * ...
178  * drm.debug=0xf will enable all messages
179  *
180  * An interesting feature is that it's possible to enable verbose logging at
181  * run-time by using hw.drm.debug sysctl variable:
182  *   # sysctl hw.drm.debug=0xfff
183  */
184 #define DRM_UT_CORE             0x01
185 #define DRM_UT_DRIVER           0x02
186 #define DRM_UT_KMS              0x04
187 #define DRM_UT_PRIME            0x08
188 #define DRM_UT_ATOMIC           0x10
189 #define DRM_UT_VBL              0x20
190 /* Extra DragonFly debug categories */
191 #ifdef __DragonFly__
192 #define DRM_UT_RES7             0x40    /* reserved for future expansion */
193 #define DRM_UT_RES8             0x80    /* reserved for future expansion */
194 #define DRM_UT_PID              0x100
195 #define DRM_UT_FIOCTL           0x200
196 #define DRM_UT_IOCTL            0x400
197 #define DRM_UT_VBLANK           0x800
198 #endif
199
200 extern __printflike(2, 3)
201 void drm_ut_debug_printk(const char *function_name,
202                          const char *format, ...);
203 extern __printflike(2, 3)
204 void drm_err(const char *func, const char *format, ...);
205
206 /***********************************************************************/
207 /** \name DRM template customization defaults */
208 /*@{*/
209
210 /* driver capabilities and requirements mask */
211 #define DRIVER_USE_AGP                  0x1
212 #define DRIVER_PCI_DMA                  0x8
213 #define DRIVER_SG                       0x10
214 #define DRIVER_HAVE_DMA                 0x20
215 #define DRIVER_HAVE_IRQ                 0x40
216 #define DRIVER_IRQ_SHARED               0x80
217 #define DRIVER_GEM                      0x1000
218 #define DRIVER_MODESET                  0x2000
219 #define DRIVER_PRIME                    0x4000
220 #define DRIVER_RENDER                   0x8000
221 #define DRIVER_ATOMIC                   0x10000
222 #define DRIVER_KMS_LEGACY_CONTEXT       0x20000
223
224 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
225
226 /***********************************************************************/
227 /** \name Macros to make printk easier */
228 /*@{*/
229
230 /**
231  * Error output.
232  *
233  * \param fmt printf() like format string.
234  * \param arg arguments
235  */
236 #define DRM_ERROR(fmt, ...)                             \
237         drm_err(__func__, fmt, ##__VA_ARGS__)
238
239 /**
240  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
241  *
242  * \param fmt printf() like format string.
243  * \param arg arguments
244  */
245 #define DRM_ERROR_RATELIMITED(fmt, ...)                         \
246 ({                                                                      \
247         static struct krate krate_derr = { .freq = 1, .count = -16 };   \
248                                                                         \
249         krateprintf(&krate_derr, "error: [" DRM_NAME ":%s] *ERROR* "    \
250             fmt, __func__, ##__VA_ARGS__);                              \
251 })
252
253 #define DRM_INFO(fmt, ...)                              \
254         printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
255
256 #define DRM_INFO_ONCE(fmt, ...)                         \
257         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
258
259 /**
260  * Debug output.
261  *
262  * \param fmt printf() like format string.
263  * \param arg arguments
264  */
265 #define DRM_DEBUG(fmt, args...)                                         \
266         do {                                                            \
267                 if (unlikely(drm_debug & DRM_UT_CORE))                  \
268                         drm_ut_debug_printk(__func__, fmt, ##args);     \
269         } while (0)
270
271 #define DRM_DEBUG_DRIVER(fmt, args...)                                  \
272         do {                                                            \
273                 if (unlikely(drm_debug & DRM_UT_DRIVER))                \
274                         drm_ut_debug_printk(__func__, fmt, ##args);     \
275         } while (0)
276 #define DRM_DEBUG_KMS(fmt, args...)                                     \
277         do {                                                            \
278                 if (unlikely(drm_debug & DRM_UT_KMS))                   \
279                         drm_ut_debug_printk(__func__, fmt, ##args);     \
280         } while (0)
281 #define DRM_DEBUG_PRIME(fmt, args...)                                   \
282         do {                                                            \
283                 if (unlikely(drm_debug & DRM_UT_PRIME))                 \
284                         drm_ut_debug_printk(__func__, fmt, ##args);     \
285         } while (0)
286 #define DRM_DEBUG_ATOMIC(fmt, args...)                                  \
287         do {                                                            \
288                 if (unlikely(drm_debug & DRM_UT_ATOMIC))                \
289                         drm_ut_debug_printk(__func__, fmt, ##args);     \
290         } while (0)
291
292 #ifdef __DragonFly__
293 #define DRM_DEBUG_FIOCTL(fmt, args...)                                  \
294         do {                                                            \
295                 if (unlikely(drm_debug & DRM_UT_FIOCTL))                \
296                         drm_ut_debug_printk(__func__, fmt, ##args);     \
297         } while (0)
298 #define DRM_DEBUG_IOCTL(fmt, args...)                                   \
299         do {                                                            \
300                 if (unlikely(drm_debug & DRM_UT_IOCTL))                 \
301                         drm_ut_debug_printk(__func__, fmt, ##args);     \
302         } while (0)
303 #define DRM_DEBUG_VBLANK(fmt, args...)                                  \
304         do {                                                            \
305                 if (unlikely(drm_debug & DRM_UT_VBLANK))                \
306                         drm_ut_debug_printk(__func__, fmt, ##args);     \
307         } while (0)
308 #define DRM_DEBUG_VBL(fmt, args...)                                     \
309         do {                                                            \
310                 if (unlikely(drm_debug & DRM_UT_VBL))           \
311                         drm_ut_debug_printk(__func__, fmt, ##args);     \
312         } while (0)
313 #endif
314
315 /*@}*/
316
317 /***********************************************************************/
318 /** \name Internal types and structures */
319 /*@{*/
320
321 SYSCTL_DECL(_hw_drm);
322
323 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
324
325 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
326
327 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
328 #define DRM_DEV_UID     UID_ROOT
329 #define DRM_DEV_GID     GID_WHEEL
330
331 #define DRM_CURPROC             curthread
332 #define DRM_STRUCTPROC          struct thread
333 #define DRM_LOCK(dev)           lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
334 #define DRM_UNLOCK(dev)         lockmgr(&(dev)->struct_mutex, LK_RELEASE)
335 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)                  \
336     (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
337 #if defined(INVARIANTS)
338 #define DRM_LOCK_ASSERT(dev)    KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
339 #define DRM_UNLOCK_ASSERT(dev)  KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
340 #else
341 #define DRM_LOCK_ASSERT(d)
342 #define DRM_UNLOCK_ASSERT(d)
343 #endif
344
345 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
346
347 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
348
349 #define DRM_MTRR_WC             MDF_WRITECOMBINE
350
351 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
352     vm_memattr_t memattr);
353 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
354 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
355
356 /* drm_memory.c */
357 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
358 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
359
360 typedef struct drm_pci_id_list
361 {
362         int vendor;
363         int device;
364         long driver_private;
365         char *name;
366 } drm_pci_id_list_t;
367
368 /**
369  * Ioctl function type.
370  *
371  * \param inode device inode.
372  * \param file_priv DRM file private pointer.
373  * \param cmd command.
374  * \param arg argument.
375  */
376 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
377                         struct drm_file *file_priv);
378
379 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
380                                unsigned long arg);
381
382 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
383
384 #define DRM_AUTH        0x1
385 #define DRM_MASTER      0x2
386 #define DRM_ROOT_ONLY   0x4
387 #define DRM_CONTROL_ALLOW 0x8
388 #define DRM_UNLOCKED    0x10
389 #define DRM_RENDER_ALLOW 0x20
390
391 struct drm_ioctl_desc {
392         unsigned int cmd;
393         int flags;
394         drm_ioctl_t *func;
395         unsigned int cmd_drv;
396         const char *name;
397 };
398
399 /**
400  * Creates a driver or general drm_ioctl_desc array entry for the given
401  * ioctl, for use by drm_ioctl().
402  */
403
404 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                         \
405         [DRM_IOCTL_NR(DRM_##ioctl)] = {                                 \
406                 .cmd = DRM_##ioctl,                                     \
407                 .func = _func,                                          \
408                 .flags = _flags,                                        \
409                 .cmd_drv = DRM_IOCTL_##ioctl,                           \
410                 .name = #ioctl                                          \
411         }
412
413 typedef struct drm_magic_entry {
414         struct list_head head;
415         struct drm_hash_item hash_item;
416         struct drm_file        *priv;
417         struct drm_magic_entry *next;
418 } drm_magic_entry_t;
419
420 /* Event queued up for userspace to read */
421 struct drm_pending_event {
422         struct drm_event *event;
423         struct list_head link;
424         struct list_head pending_link;
425         struct drm_file *file_priv;
426         pid_t pid; /* pid of requester, no guarantee it's valid by the time
427                       we deliver the event, for tracing only */
428         void (*destroy)(struct drm_pending_event *event);
429 };
430
431 /* initial implementaton using a linked list - todo hashtab */
432 struct drm_prime_file_private {
433         struct list_head head;
434         struct lock lock;
435 };
436
437 /** File private data */
438 struct drm_file {
439         unsigned authenticated :1;
440         /* Whether we're master for a minor. Protected by master_mutex */
441         unsigned is_master :1;
442
443         /* true when the client has asked us to expose stereo 3D mode flags */
444         unsigned stereo_allowed :1;
445         /*
446          * true if client understands CRTC primary planes and cursor planes
447          * in the plane list
448          */
449         unsigned universal_planes:1;
450         /* true if client understands atomic properties */
451         unsigned atomic:1;
452
453         pid_t             pid;
454         uid_t             uid;
455         drm_magic_t       magic;
456         struct list_head lhead;
457         unsigned long lock_count;
458
459         struct kqinfo     dkq;
460
461         /** Mapping of mm object handles to object pointers. */
462         struct idr object_idr;
463         /** Lock for synchronization of access to object_idr. */
464         struct lock table_lock;
465
466         struct file *filp;
467         void *driver_priv;
468
469         struct drm_master *masterp;
470
471         /**
472          * fbs - List of framebuffers associated with this file.
473          *
474          * Protected by fbs_lock. Note that the fbs list holds a reference on
475          * the fb object to prevent it from untimely disappearing.
476          */
477         struct list_head fbs;
478         struct lock fbs_lock;
479
480         /** User-created blob properties; this retains a reference on the
481          *  property. */
482         struct list_head blobs;
483
484         wait_queue_head_t event_wait;
485         struct list_head pending_event_list;
486         struct list_head event_list;
487         int event_space;
488
489         struct lock event_read_lock;
490
491         struct drm_prime_file_private prime;
492
493 #ifdef __DragonFly__
494         struct drm_device *dev;
495         int               master;
496         unsigned long ioctl_count;
497 #endif
498 };
499
500 /**
501  * Lock data.
502  */
503 struct drm_lock_data {
504         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
505         /** Private of lock holder's file (NULL=kernel) */
506         struct drm_file *file_priv;
507         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
508         unsigned long lock_time;        /**< Time of last lock in jiffies */
509 };
510
511 /**
512  * GEM specific mm private for tracking GEM objects
513  */
514 struct drm_gem_mm {
515         struct drm_vma_offset_manager vma_manager;
516         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
517         struct drm_open_hash offset_hash; /**< User token hash table for maps */
518         struct unrhdr *idxunr;
519 };
520
521 /**
522  * struct drm_master - drm master structure
523  *
524  * @refcount: Refcount for this master object.
525  * @minor: Link back to minor char device we are master for. Immutable.
526  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
527  * @unique_len: Length of unique field. Protected by drm_global_mutex.
528  * @magic_map: Map of used authentication tokens. Protected by struct_mutex.
529  * @lock: DRI lock information.
530  * @driver_priv: Pointer to driver-private information.
531  */
532 struct drm_master {
533         struct kref refcount;           /* refcount for this master */
534         struct list_head head;          /**< each minor contains a list of masters */
535         struct drm_minor *minor;        /**< link back to minor we are a master for */
536         char *unique;                   /**< Unique identifier: e.g., busid */
537         int unique_len;                 /**< Length of unique field */
538         int unique_size;                /**< amount allocated */
539         int blocked;                    /**< Blocked due to VC switch? */
540         struct idr magic_map;
541         struct drm_lock_data lock;
542         void *driver_priv;
543 };
544
545 /* Size of ringbuffer for vblank timestamps. Just double-buffer
546  * in initial implementation.
547  */
548 #define DRM_VBLANKTIME_RBSIZE 2
549
550 /* Flags and return codes for get_vblank_timestamp() driver function. */
551 #define DRM_CALLED_FROM_VBLIRQ 1
552 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
553 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
554
555 /* get_scanout_position() return flags */
556 #define DRM_SCANOUTPOS_VALID        (1 << 0)
557 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
558 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
559
560 /**
561  * DRM driver structure. This structure represent the common code for
562  * a family of cards. There will one drm_device for each card present
563  * in this family
564  */
565 struct drm_driver {
566         int (*load) (struct drm_device *, unsigned long flags);
567         int (*firstopen) (struct drm_device *);
568         int (*open) (struct drm_device *, struct drm_file *);
569         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
570         void (*postclose) (struct drm_device *, struct drm_file *);
571         void (*lastclose) (struct drm_device *);
572         int (*unload) (struct drm_device *);
573         int (*suspend) (struct drm_device *, pm_message_t state);
574         int (*resume) (struct drm_device *);
575         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
576         int (*dma_quiescent) (struct drm_device *);
577         int (*context_dtor) (struct drm_device *dev, int context);
578         int (*set_busid)(struct drm_device *dev, struct drm_master *master);
579
580         /**
581          * get_vblank_counter - get raw hardware vblank counter
582          * @dev: DRM device
583          * @pipe: counter to fetch
584          *
585          * Driver callback for fetching a raw hardware vblank counter for @crtc.
586          * If a device doesn't have a hardware counter, the driver can simply
587          * return the value of drm_vblank_count. The DRM core will account for
588          * missed vblank events while interrupts where disabled based on system
589          * timestamps.
590          *
591          * Wraparound handling and loss of events due to modesetting is dealt
592          * with in the DRM core code.
593          *
594          * RETURNS
595          * Raw vblank counter value.
596          */
597         u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
598
599         /**
600          * enable_vblank - enable vblank interrupt events
601          * @dev: DRM device
602          * @pipe: which irq to enable
603          *
604          * Enable vblank interrupts for @crtc.  If the device doesn't have
605          * a hardware vblank counter, this routine should be a no-op, since
606          * interrupts will have to stay on to keep the count accurate.
607          *
608          * RETURNS
609          * Zero on success, appropriate errno if the given @crtc's vblank
610          * interrupt cannot be enabled.
611          */
612         int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
613
614         /**
615          * disable_vblank - disable vblank interrupt events
616          * @dev: DRM device
617          * @pipe: which irq to enable
618          *
619          * Disable vblank interrupts for @crtc.  If the device doesn't have
620          * a hardware vblank counter, this routine should be a no-op, since
621          * interrupts will have to stay on to keep the count accurate.
622          */
623         void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
624
625         /**
626          * Called by \c drm_device_is_agp.  Typically used to determine if a
627          * card is really attached to AGP or not.
628          *
629          * \param dev  DRM device handle
630          *
631          * \returns
632          * One of three values is returned depending on whether or not the
633          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
634          * (return of 1), or may or may not be AGP (return of 2).
635          */
636         int (*device_is_agp) (struct drm_device *dev);
637
638         /**
639          * Called by vblank timestamping code.
640          *
641          * Return the current display scanout position from a crtc, and an
642          * optional accurate ktime_get timestamp of when position was measured.
643          *
644          * \param dev  DRM device.
645          * \param pipe Id of the crtc to query.
646          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
647          * \param *vpos Target location for current vertical scanout position.
648          * \param *hpos Target location for current horizontal scanout position.
649          * \param *stime Target location for timestamp taken immediately before
650          *               scanout position query. Can be NULL to skip timestamp.
651          * \param *etime Target location for timestamp taken immediately after
652          *               scanout position query. Can be NULL to skip timestamp.
653          * \param mode Current display timings.
654          *
655          * Returns vpos as a positive number while in active scanout area.
656          * Returns vpos as a negative number inside vblank, counting the number
657          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
658          * until start of active scanout / end of vblank."
659          *
660          * \return Flags, or'ed together as follows:
661          *
662          * DRM_SCANOUTPOS_VALID = Query successful.
663          * DRM_SCANOUTPOS_INVBL = Inside vblank.
664          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
665          * this flag means that returned position may be offset by a constant
666          * but unknown small number of scanlines wrt. real scanout position.
667          *
668          */
669         int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
670                                      unsigned int flags, int *vpos, int *hpos,
671                                      ktime_t *stime, ktime_t *etime,
672                                      const struct drm_display_mode *mode);
673
674         /**
675          * Called by \c drm_get_last_vbltimestamp. Should return a precise
676          * timestamp when the most recent VBLANK interval ended or will end.
677          *
678          * Specifically, the timestamp in @vblank_time should correspond as
679          * closely as possible to the time when the first video scanline of
680          * the video frame after the end of VBLANK will start scanning out,
681          * the time immediately after end of the VBLANK interval. If the
682          * @crtc is currently inside VBLANK, this will be a time in the future.
683          * If the @crtc is currently scanning out a frame, this will be the
684          * past start time of the current scanout. This is meant to adhere
685          * to the OpenML OML_sync_control extension specification.
686          *
687          * \param dev dev DRM device handle.
688          * \param pipe crtc for which timestamp should be returned.
689          * \param *max_error Maximum allowable timestamp error in nanoseconds.
690          *                   Implementation should strive to provide timestamp
691          *                   with an error of at most *max_error nanoseconds.
692          *                   Returns true upper bound on error for timestamp.
693          * \param *vblank_time Target location for returned vblank timestamp.
694          * \param flags 0 = Defaults, no special treatment needed.
695          * \param       DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
696          *              irq handler. Some drivers need to apply some workarounds
697          *              for gpu-specific vblank irq quirks if flag is set.
698          *
699          * \returns
700          * Zero if timestamping isn't supported in current display mode or a
701          * negative number on failure. A positive status code on success,
702          * which describes how the vblank_time timestamp was computed.
703          */
704         int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
705                                      int *max_error,
706                                      struct timeval *vblank_time,
707                                      unsigned flags);
708
709         /* these have to be filled in */
710
711         irqreturn_t(*irq_handler) (int irq, void *arg);
712         void (*irq_preinstall) (struct drm_device *dev);
713         int (*irq_postinstall) (struct drm_device *dev);
714         void (*irq_uninstall) (struct drm_device *dev);
715
716         /* Master routines */
717         int (*master_create)(struct drm_device *dev, struct drm_master *master);
718         void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
719         /**
720          * master_set is called whenever the minor master is set.
721          * master_drop is called whenever the minor master is dropped.
722          */
723
724         int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
725                           bool from_open);
726         void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
727                             bool from_release);
728
729         int (*debugfs_init)(struct drm_minor *minor);
730         void (*debugfs_cleanup)(struct drm_minor *minor);
731
732         /**
733          * @gem_free_object: deconstructor for drm_gem_objects
734          *
735          * This is deprecated and should not be used by new drivers. Use
736          * @gem_free_object_unlocked instead.
737          */
738         void (*gem_free_object) (struct drm_gem_object *obj);
739
740         /**
741          * @gem_free_object_unlocked: deconstructor for drm_gem_objects
742          *
743          * This is for drivers which are not encumbered with dev->struct_mutex
744          * legacy locking schemes. Use this hook instead of @gem_free_object.
745          */
746         void (*gem_free_object_unlocked) (struct drm_gem_object *obj);
747
748         int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
749         void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
750
751         /**
752          * Hook for allocating the GEM object struct, for use by core
753          * helpers.
754          */
755         struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
756                                                     size_t size);
757
758         /* prime: */
759         /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
760         int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
761                                 uint32_t handle, uint32_t flags, int *prime_fd);
762         /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
763         int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
764                                 int prime_fd, uint32_t *handle);
765         /* export GEM -> dmabuf */
766         struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
767                                 struct drm_gem_object *obj, int flags);
768         /* import dmabuf -> GEM */
769         struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
770                                 struct dma_buf *dma_buf);
771         /* low-level interface used by drm_gem_prime_{import,export} */
772         int (*gem_prime_pin)(struct drm_gem_object *obj);
773         void (*gem_prime_unpin)(struct drm_gem_object *obj);
774         struct reservation_object * (*gem_prime_res_obj)(
775                                 struct drm_gem_object *obj);
776         struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
777         struct drm_gem_object *(*gem_prime_import_sg_table)(
778                                 struct drm_device *dev,
779                                 struct dma_buf_attachment *attach,
780                                 struct sg_table *sgt);
781         void *(*gem_prime_vmap)(struct drm_gem_object *obj);
782         void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
783         int (*gem_prime_mmap)(struct drm_gem_object *obj,
784                                 struct vm_area_struct *vma);
785
786         /* vga arb irq handler */
787         void (*vgaarb_irq)(struct drm_device *dev, bool state);
788
789         /* dumb alloc support */
790         int (*dumb_create)(struct drm_file *file_priv,
791                            struct drm_device *dev,
792                            struct drm_mode_create_dumb *args);
793         int (*dumb_map_offset)(struct drm_file *file_priv,
794                                struct drm_device *dev, uint32_t handle,
795                                uint64_t *offset);
796         int (*dumb_destroy)(struct drm_file *file_priv,
797                             struct drm_device *dev,
798                             uint32_t handle);
799
800         /* Driver private ops for this object */
801         struct cdev_pager_ops *gem_vm_ops;
802
803         int major;
804         int minor;
805         int patchlevel;
806         char *name;
807         char *desc;
808         char *date;
809
810         u32 driver_features;
811         int dev_priv_size;
812         const struct drm_ioctl_desc *ioctls;
813         int num_ioctls;
814         const struct file_operations *fops;
815
816         /* List of devices hanging off this driver with stealth attach. */
817         struct list_head legacy_dev_list;
818 #ifdef __DragonFly__
819         int (*sysctl_init) (struct drm_device *dev,
820                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
821         void (*sysctl_cleanup) (struct drm_device *dev);
822 #endif  /* __DragonFly__ */
823 };
824
825 enum drm_minor_type {
826         DRM_MINOR_LEGACY,
827         DRM_MINOR_CONTROL,
828         DRM_MINOR_RENDER,
829         DRM_MINOR_CNT,
830 };
831
832 /**
833  * Info file list entry. This structure represents a debugfs or proc file to
834  * be created by the drm core
835  */
836 struct drm_info_list {
837         const char *name; /** file name */
838         int (*show)(struct seq_file*, void*); /** show callback */
839         u32 driver_features; /**< Required driver features for this entry */
840         void *data;
841 };
842
843 /**
844  * debugfs node structure. This structure represents a debugfs file.
845  */
846 struct drm_info_node {
847         struct list_head list;
848         struct drm_minor *minor;
849         const struct drm_info_list *info_ent;
850         struct dentry *dent;
851 };
852
853 /**
854  * DRM minor structure. This structure represents a drm minor number.
855  */
856 struct drm_minor {
857         int index;                      /**< Minor device number */
858         int type;                       /**< Control or render */
859         struct device *kdev;            /**< Linux device */
860         struct drm_device *dev;
861
862         /* currently active master for this node. Protected by master_mutex */
863         struct drm_master *master;
864 };
865
866 struct drm_pending_vblank_event {
867         struct drm_pending_event base;
868         unsigned int pipe;
869         struct drm_event_vblank event;
870 };
871
872 struct drm_vblank_crtc {
873         struct drm_device *dev;         /* pointer to the drm_device */
874         wait_queue_head_t queue;        /**< VBLANK wait queue */
875         struct timer_list disable_timer;                /* delayed disable timer */
876
877         /* vblank counter, protected by dev->vblank_time_lock for writes */
878         u32 count;
879         /* vblank timestamps, protected by dev->vblank_time_lock for writes */
880         struct timeval time[DRM_VBLANKTIME_RBSIZE];
881
882         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
883         u32 last;                       /* protected by dev->vbl_lock, used */
884                                         /* for wraparound handling */
885         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
886         unsigned int inmodeset;         /* Display driver is setting mode */
887         unsigned int pipe;              /* crtc index */
888         int framedur_ns;                /* frame/field duration in ns */
889         int linedur_ns;                 /* line duration in ns */
890         int pixeldur_ns;                /* pixel duration in ns */
891         bool enabled;                   /* so we don't call enable more than
892                                            once per disable */
893 };
894
895 struct drm_sysctl_info {
896         struct sysctl_ctx_list ctx;
897         char   name[2];
898 };
899
900 /* Length for the array of resource pointers for drm_get_resource_*. */
901 #define DRM_MAX_PCI_RESOURCE    6
902
903 /**
904  * DRM device structure. This structure represent a complete card that
905  * may contain multiple heads.
906  */
907 struct drm_device {
908         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
909
910         char              *unique;      /* Unique identifier: e.g., busid  */
911         int               unique_len;   /* Length of unique field          */
912         struct cdev       *devnode;     /* Device number for mknod         */
913         int               if_version;   /* Highest interface version set */
914
915         int               flags;        /* Flags to open(2)                */
916
917                                 /* Locks */
918         struct lwkt_serialize irq_lock; /* protects irq condition checks */
919         struct lock       dev_lock;     /* protects everything else */
920
921         /** \name Locks */
922         /*@{ */
923         struct lock struct_mutex;       /**< For others */
924         struct lock master_mutex;       /**< For drm_minor::master */
925         /*@} */
926
927         /** \name Usage Counters */
928         /*@{ */
929         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
930         struct spinlock buf_lock;       /**< For drm_device::buf_use and a few other things. */
931         int buf_use;                    /**< Buffers in use -- cannot alloc */
932         atomic_t buf_alloc;             /**< Buffer allocation in progress */
933         /*@} */
934
935
936         /** \name Performance counters */
937         /*@{ */
938         unsigned long     counters;
939         enum drm_stat_type      types[15];
940         atomic_t          counts[15];
941         /*@} */
942
943                                 /* Authentication */
944         struct idr magic_map;
945
946         struct lock filelist_mutex;
947         struct list_head filelist;
948
949         /** \name Memory management */
950         /*@{ */
951         struct list_head maplist;       /**< Linked list of regions */
952         int map_count;                  /**< Number of mappable regions */
953         struct drm_open_hash map_hash;  /**< User token hash table for maps */
954
955         /** \name Context handle management */
956         /*@{ */
957         struct list_head ctxlist;       /**< Linked list of context handles */
958         struct lock ctxlist_mutex;      /**< For ctxlist */
959
960         struct idr ctx_idr;
961
962         /*@} */
963
964         struct drm_lock_data lock;      /* Information on hardware lock    */
965
966         /** \name DMA support */
967         /*@{ */
968         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
969         /*@} */
970
971         /* Storage of resource pointers for drm_get_resource_* */
972         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
973         int               pcirid[DRM_MAX_PCI_RESOURCE];
974
975         int               pci_domain;
976         int               pci_bus;
977         int               pci_slot;
978         int               pci_func;
979
980         /** \name Context support */
981         /*@{ */
982
983         __volatile__ long context_flag; /**< Context swapping flag */
984         __volatile__ long interrupt_flag; /**< Interruption handler flag */
985         __volatile__ long dma_flag;     /**< DMA dispatch flag */
986         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
987         int last_checked;               /**< Last context checked for DMA */
988         int last_context;               /**< Last current context */
989         unsigned long last_switch;      /**< jiffies at last context switch */
990         /*@} */
991
992         /** \name VBLANK IRQ support */
993         /*@{ */
994         int irq_enabled;                /**< True if irq handler is enabled */
995         int irq;                        /* Interrupt used by board */
996
997         /*
998          * If true, vblank interrupt will be disabled immediately when the
999          * refcount drops to zero, as opposed to via the vblank disable
1000          * timer.
1001          * This can be set to true it the hardware has a working vblank
1002          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
1003          * appropriately.
1004          */
1005         bool vblank_disable_immediate;
1006
1007         /* array of size num_crtcs */
1008         struct drm_vblank_crtc *vblank;
1009
1010         struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
1011         struct lock vbl_lock;
1012         struct timer_list vblank_disable_timer;
1013
1014         u32 max_vblank_count;           /**< size of vblank counter register */
1015
1016         /**
1017          * List of events
1018          */
1019         struct list_head vblank_event_list;
1020         struct lock event_lock;
1021
1022         /*@} */
1023
1024         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
1025
1026                                 /* Sysctl support */
1027         struct drm_sysctl_info *sysctl;
1028
1029
1030         struct drm_sg_mem *sg;  /**< Scatter gather memory */
1031         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1032
1033         unsigned long     *ctx_bitmap;
1034         void              *dev_private;
1035
1036         void              *drm_ttm_bdev;
1037
1038         /*@} */
1039
1040         struct {
1041                 int context;
1042                 struct drm_hw_lock *lock;
1043         } sigdata;
1044
1045         struct drm_agp_head *agp;       /**< AGP data */
1046
1047         struct device *dev;             /**< Device structure */
1048         struct pci_dev *pdev;           /**< PCI device structure */
1049
1050         struct drm_driver *driver;
1051         struct drm_local_map *agp_buffer_map;
1052         unsigned int agp_buffer_token;
1053         struct drm_minor *control;              /**< Control node for card */
1054         struct drm_minor *primary;              /**< render type primary screen head */
1055
1056         struct drm_mode_config mode_config;     /**< Current mode config */
1057
1058         /** \name GEM information */
1059         /*@{ */
1060         struct lock object_name_lock;
1061         struct idr object_name_idr;
1062         /*@} */
1063         void             *mm_private;
1064
1065         void *sysctl_private;
1066         char busid_str[128];
1067         int modesetting;
1068
1069         int switch_power_state;
1070
1071 #ifdef __DragonFly__
1072         atomic_t unplugged; /* device has been unplugged or gone away */
1073 #endif  /* __DragonFly__ */
1074 };
1075
1076 #define DRM_SWITCH_POWER_ON 0
1077 #define DRM_SWITCH_POWER_OFF 1
1078 #define DRM_SWITCH_POWER_CHANGING 2
1079 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1080
1081 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1082                                              int feature)
1083 {
1084         return ((dev->driver->driver_features & feature) ? 1 : 0);
1085 }
1086
1087 static inline void drm_device_set_unplugged(struct drm_device *dev)
1088 {
1089         smp_wmb();
1090         atomic_set(&dev->unplugged, 1);
1091 }
1092
1093 static inline int drm_device_is_unplugged(struct drm_device *dev)
1094 {
1095         int ret = atomic_read(&dev->unplugged);
1096         smp_rmb();
1097         return ret;
1098 }
1099
1100 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1101 {
1102         return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1103 }
1104
1105 /**
1106  * drm_is_master() - Check whether a DRM open-file is DRM-Master
1107  * @file: DRM open-file context
1108  *
1109  * This checks whether a DRM open-file context is owner of the master context
1110  * attached to it. If a file owns a master context, it's called DRM-Master.
1111  * Per DRM device, only one such file can be DRM-Master at a time.
1112  *
1113  * Returns: True if the file is DRM-Master, otherwise false.
1114  */
1115 static inline bool drm_is_master(const struct drm_file *file)
1116 {
1117 #if 0
1118         return file->master && file->master == file->minor->master;
1119 #else
1120         return true;
1121 #endif
1122 }
1123
1124 /******************************************************************/
1125 /** \name Internal function definitions */
1126 /*@{*/
1127
1128                                 /* Driver support (drm_drv.h) */
1129 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1130 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1131 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1132 int     drm_create_cdevs(device_t kdev);
1133 d_ioctl_t drm_ioctl;
1134 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1135
1136                                 /* Device support (drm_fops.h) */
1137 d_open_t drm_open;
1138 d_close_t drm_close;
1139 d_read_t drm_read;
1140 d_kqfilter_t drm_kqfilter;
1141 int drm_release(device_t kdev);
1142
1143 d_mmap_t drm_mmap;
1144 d_mmap_single_t drm_mmap_single;
1145
1146 void drm_cdevpriv_dtor(void *cd);
1147
1148 int drm_add_busid_modesetting(struct drm_device *dev,
1149     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1150
1151 /* File operations helpers (drm_fops.c) */
1152 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1153                                          DRM_STRUCTPROC *p,
1154                                         struct drm_device *dev,
1155                                         struct file *fp);
1156 int drm_event_reserve_init_locked(struct drm_device *dev,
1157                                   struct drm_file *file_priv,
1158                                   struct drm_pending_event *p,
1159                                   struct drm_event *e);
1160 int drm_event_reserve_init(struct drm_device *dev,
1161                            struct drm_file *file_priv,
1162                            struct drm_pending_event *p,
1163                            struct drm_event *e);
1164 void drm_event_cancel_free(struct drm_device *dev,
1165                            struct drm_pending_event *p);
1166 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
1167 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
1168
1169 /* Misc. IOCTL support (drm_ioctl.c) */
1170 int drm_noop(struct drm_device *dev, void *data,
1171              struct drm_file *file_priv);
1172 int drm_invalid_op(struct drm_device *dev, void *data,
1173                    struct drm_file *file_priv);
1174
1175 /* Cache management (drm_cache.c) */
1176 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
1177 void drm_clflush_sg(struct sg_table *st);
1178 void drm_clflush_virt_range(void *addr, unsigned long length);
1179
1180 /*
1181  * These are exported to drivers so that they can implement fencing using
1182  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1183  */
1184
1185 unsigned long drm_get_resource_start(struct drm_device *dev,
1186                                      unsigned int resource);
1187 unsigned long drm_get_resource_len(struct drm_device *dev,
1188                                    unsigned int resource);
1189
1190                                 /* IRQ support (drm_irq.h) */
1191 extern int drm_irq_install(struct drm_device *dev, int irq);
1192 extern int drm_irq_uninstall(struct drm_device *dev);
1193
1194 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
1195 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1196                            struct drm_file *filp);
1197 extern u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe);
1198 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1199 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1200                                      struct timeval *vblanktime);
1201 extern u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
1202                                           struct timeval *vblanktime);
1203 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1204                                   struct drm_pending_vblank_event *e);
1205 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1206                                        struct drm_pending_vblank_event *e);
1207 extern void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
1208                                  struct drm_pending_vblank_event *e);
1209 extern void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1210                                       struct drm_pending_vblank_event *e);
1211 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
1212 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1213 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
1214 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
1215 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1216 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1217 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
1218 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1219 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
1220 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
1221 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1222 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1223 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1224 extern void drm_vblank_cleanup(struct drm_device *dev);
1225 extern u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe);
1226
1227 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1228                                                  unsigned int pipe, int *max_error,
1229                                                  struct timeval *vblank_time,
1230                                                  unsigned flags,
1231                                                  const struct drm_display_mode *mode);
1232 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1233                                             const struct drm_display_mode *mode);
1234
1235 /**
1236  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1237  * @crtc: which CRTC's vblank waitqueue to retrieve
1238  *
1239  * This function returns a pointer to the vblank waitqueue for the CRTC.
1240  * Drivers can use this to implement vblank waits using wait_event() & co.
1241  */
1242 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1243 {
1244         return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1245 }
1246
1247 /* Modesetting support */
1248 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1249 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1250
1251                                 /* Stub support (drm_stub.h) */
1252
1253 extern void drm_put_dev(struct drm_device *dev);
1254 extern void drm_unplug_dev(struct drm_device *dev);
1255 extern unsigned int drm_debug;
1256 extern bool drm_atomic;
1257
1258 /* consistent PCI memory functions (drm_pci.c) */
1259 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1260                                             size_t align);
1261 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1262
1263                                /* sysfs support (drm_sysfs.c) */
1264 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1265
1266 /* sysctl support (drm_sysctl.h) */
1267 extern int drm_sysctl_init(struct drm_device *dev);
1268 extern int drm_sysctl_cleanup(struct drm_device *dev);
1269
1270 /* XXX: These are here only because of drm_sysctl.c */
1271 extern int drm_vblank_offdelay;
1272 extern unsigned int drm_timestamp_precision;
1273
1274 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1275     vm_size_t size, struct vm_object **obj_res, int nprot);
1276
1277 struct ttm_bo_device;
1278 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1279     vm_size_t size, struct vm_object **obj_res, int nprot);
1280 struct ttm_buffer_object;
1281 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1282
1283 /* simplified version of kvasnprintf() for drm needs. */
1284 char *drm_vasprintf(int flags, const char *format, __va_list ap) __printflike(2, 0);
1285 char *drm_asprintf(int flags, const char *format, ...) __printflike(2, 3);
1286
1287 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1288 void drm_init_pdev(device_t dev, struct pci_dev **pdev);
1289 void drm_fini_pdev(struct pci_dev **pdev);
1290 void drm_print_pdev(struct pci_dev *pdev);
1291
1292 /* Inline drm_free() helper for area kfree() */
1293 static __inline__ void
1294 drm_free(void *pt, struct malloc_type *area)
1295 {
1296         /* kfree is special!!! */
1297         if (pt != NULL)
1298                 (kfree)(pt, area);
1299 }
1300
1301 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1302                                  struct device *parent);
1303 void drm_dev_ref(struct drm_device *dev);
1304 void drm_dev_unref(struct drm_device *dev);
1305 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1306 void drm_dev_unregister(struct drm_device *dev);
1307 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1308
1309
1310 /* PCI section */
1311 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1312 {
1313         if (dev->driver->device_is_agp != NULL) {
1314                 int err = (*dev->driver->device_is_agp) (dev);
1315
1316                 if (err != 2) {
1317                         return err;
1318                 }
1319         }
1320
1321         return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);
1322 }
1323 void drm_pci_agp_destroy(struct drm_device *dev);
1324
1325 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1326 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1327 #ifdef CONFIG_PCI
1328 extern int drm_get_pci_dev(struct pci_dev *pdev,
1329                            const struct pci_device_id *ent,
1330                            struct drm_driver *driver);
1331 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1332 #else
1333 static inline int drm_get_pci_dev(struct pci_dev *pdev,
1334                                   const struct pci_device_id *ent,
1335                                   struct drm_driver *driver)
1336 {
1337         return -ENOSYS;
1338 }
1339
1340 static inline int drm_pci_set_busid(struct drm_device *dev,
1341                                     struct drm_master *master)
1342 {
1343         return -ENOSYS;
1344 }
1345 #endif
1346
1347 #define DRM_PCIE_SPEED_25 1
1348 #define DRM_PCIE_SPEED_50 2
1349 #define DRM_PCIE_SPEED_80 4
1350
1351 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1352
1353 /* XXX bad */
1354 #define drm_can_sleep() (HZ & 1)
1355
1356 #endif /* __KERNEL__ */
1357
1358 /* helper for handling conditionals in various for_each macros */
1359 #define for_each_if(condition) if (!(condition)) {} else
1360
1361 #endif /* _DRM_P_H_ */