drm/linux: Move M_DRM declaration to linux/slab.h
[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/queue.h>
58 #include <sys/signalvar.h>
59 #include <sys/poll.h>
60 #include <linux/highmem.h>
61 #include <sys/sbuf.h>
62 #include <sys/taskqueue.h>
63 #include <sys/tree.h>
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_map.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page2.h>
71 #include <vm/vm_pager.h>
72 #include <vm/vm_param.h>
73 #include <machine/param.h>
74 #include <machine/pmap.h>
75 #ifdef __x86_64__
76 #include <machine/specialreg.h>
77 #endif
78 #include <machine/sysarch.h>
79 #include <sys/endian.h>
80 #include <sys/mman.h>
81 #include <sys/rman.h>
82 #include <sys/memrange.h>
83 #include <sys/agpio.h>
84 #include <sys/mutex.h>
85
86 #include <uapi_drm/drm.h>
87 #include <uapi_drm/drm_sarea.h>
88
89 #include <linux/atomic.h>
90 #include <linux/bug.h>
91 #include <linux/dma-mapping.h>
92 #include <linux/capability.h>
93 #include <linux/err.h>
94 #include <linux/idr.h>
95 #include <linux/pci.h>
96 #include <linux/jiffies.h>
97 #include <linux/kernel.h>
98 #include <linux/fs.h>
99 #include <linux/kref.h>
100 #include <linux/list.h>
101 #include <linux/mm.h>
102 #include <linux/moduleparam.h>
103 #include <linux/mutex.h>
104 #include <linux/slab.h>
105 #include <linux/scatterlist.h>
106 #include <linux/timer.h>
107 #include <asm/io.h>
108 #include <linux/seq_file.h>
109 #include <linux/types.h>
110 #include <linux/vmalloc.h>
111 #include <linux/wait.h>
112 #include <linux/workqueue.h>
113
114 #include <asm/uaccess.h>
115
116 #include <drm/drm_global.h>
117
118 #include <drm/drm_vma_manager.h>
119
120 #include <drm/drm_os_linux.h>
121 #include <drm/drm_hashtab.h>
122 #include <drm/drm_mm.h>
123
124 struct drm_file;
125 struct drm_device;
126 struct drm_agp_head;
127 struct drm_gem_object;
128
129 struct device_node;
130 struct videomode;
131 struct reservation_object;
132 struct dma_buf_attachment;
133
134 /*
135  * 4 debug categories are defined:
136  *
137  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
138  *       This is the category used by the DRM_DEBUG() macro.
139  *
140  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
141  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
142  *
143  * KMS: used in the modesetting code.
144  *      This is the category used by the DRM_DEBUG_KMS() macro.
145  *
146  * PRIME: used in the prime code.
147  *        This is the category used by the DRM_DEBUG_PRIME() macro.
148  *
149  * ATOMIC: used in the atomic code.
150  *        This is the category used by the DRM_DEBUG_ATOMIC() macro.
151  *
152  * Enabling verbose debug messages is done through the drm.debug parameter,
153  * each category being enabled by a bit.
154  *
155  * drm.debug=0x1 will enable CORE messages
156  * drm.debug=0x2 will enable DRIVER messages
157  * drm.debug=0x3 will enable CORE and DRIVER messages
158  * ...
159  * drm.debug=0xf will enable all messages
160  *
161  * An interesting feature is that it's possible to enable verbose logging at
162  * run-time by echoing the debug value in its sysfs node:
163  *   # echo 0xf > /sys/module/drm/parameters/debug
164  */
165 #define DRM_UT_CORE             0x01
166 #define DRM_UT_DRIVER           0x02
167 #define DRM_UT_KMS              0x04
168 #define DRM_UT_PRIME            0x08
169 #define DRM_UT_ATOMIC           0x10
170
171 void drm_ut_debug_printk(const char *function_name,
172                          const char *format, ...);
173
174 void drm_err(const char *format, ...);
175
176 /***********************************************************************/
177 /** \name DRM template customization defaults */
178 /*@{*/
179
180 /* driver capabilities and requirements mask */
181 #define DRIVER_USE_AGP     0x1
182 #define DRIVER_REQUIRE_AGP 0x2
183 #define DRIVER_PCI_DMA     0x8
184 #define DRIVER_SG          0x10
185 #define DRIVER_HAVE_DMA    0x20
186 #define DRIVER_HAVE_IRQ    0x40
187 #define DRIVER_IRQ_SHARED  0x80
188 #define DRIVER_DMA_QUEUE   0x200
189 #define DRIVER_GEM         0x1000
190 #define DRIVER_MODESET     0x2000
191 #define DRIVER_PRIME       0x4000
192 #define DRIVER_RENDER      0x8000
193 #define DRIVER_ATOMIC      0x10000
194
195 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
196
197 /***********************************************************************/
198 /** \name Macros to make printk easier */
199 /*@{*/
200
201 /**
202  * Error output.
203  *
204  * \param fmt printf() like format string.
205  * \param arg arguments
206  */
207 #define DRM_ERROR(fmt, ...) \
208         kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,          \
209             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
210
211 /**
212  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
213  *
214  * \param fmt printf() like format string.
215  * \param arg arguments
216  */
217
218 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
219
220 #define DRM_INFO_ONCE(fmt, ...)                         \
221         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
222
223 /**
224  * Debug output.
225  *
226  * \param fmt printf() like format string.
227  * \param arg arguments
228  */
229
230 #define DRM_DEBUGBITS_DEBUG             0x1
231 #define DRM_DEBUGBITS_KMS               0x2
232 #define DRM_DEBUGBITS_FAILED_IOCTL      0x4
233 #define DRM_DEBUGBITS_VERBOSE           0x8
234
235 #define DRM_DEBUG(fmt, ...) do {                                        \
236         if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0)             \
237                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
238                         __func__ , ##__VA_ARGS__);                      \
239 } while (0)
240
241 #define DRM_DEBUG_VERBOSE(fmt, ...) do {                                \
242         if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0)           \
243                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
244                         __func__ , ##__VA_ARGS__);                      \
245 } while (0)
246
247 #define DRM_DEBUG_KMS(fmt, ...) do {                                    \
248         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
249                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
250                         __func__ , ##__VA_ARGS__);                      \
251 } while (0)
252
253 #define DRM_DEBUG_DRIVER(fmt, ...) do {                                 \
254         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
255                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
256                         __func__ , ##__VA_ARGS__);                      \
257 } while (0)
258
259 #define DRM_DEBUG_ATOMIC(fmt, args...)                                  \
260         do {                                                            \
261                 if (unlikely(drm_debug & DRM_UT_ATOMIC))                \
262                         drm_ut_debug_printk(__func__, fmt, ##args);     \
263         } while (0)
264
265 /*@}*/
266
267 /***********************************************************************/
268 /** \name Internal types and structures */
269 /*@{*/
270
271 #define DRM_GEM_MAPPING_MASK    (3ULL << 62)
272 #define DRM_GEM_MAPPING_KEY     (2ULL << 62) /* Non-canonical address form */
273 #define DRM_GEM_MAX_IDX         0x3fffff
274 #define DRM_GEM_MAPPING_IDX(o)  (((o) >> 40) & DRM_GEM_MAX_IDX)
275 #define DRM_GEM_MAPPING_OFF(i)  (((uint64_t)(i)) << 40)
276 #define DRM_GEM_MAPPING_MAPOFF(o) \
277     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
278
279 SYSCTL_DECL(_hw_drm);
280
281 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
282
283 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
284
285 #define __OS_HAS_AGP    1
286
287 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
288 #define DRM_DEV_UID     UID_ROOT
289 #define DRM_DEV_GID     GID_WHEEL
290
291 #define DRM_CURPROC             curthread
292 #define DRM_STRUCTPROC          struct thread
293 #define DRM_CURRENTPID          (curproc != NULL ? curproc->p_pid : -1)
294 #define DRM_LOCK(dev)           lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
295 #define DRM_UNLOCK(dev)         lockmgr(&(dev)->struct_mutex, LK_RELEASE)
296 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)                  \
297     (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
298 #if defined(INVARIANTS)
299 #define DRM_LOCK_ASSERT(dev)    KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
300 #define DRM_UNLOCK_ASSERT(dev)  KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
301 #else
302 #define DRM_LOCK_ASSERT(d)
303 #define DRM_UNLOCK_ASSERT(d)
304 #endif
305
306 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
307
308 typedef void                    irqreturn_t;
309 #define IRQ_HANDLED             /* nothing */
310 #define IRQ_NONE                /* nothing */
311
312 enum {
313         DRM_IS_NOT_AGP,
314         DRM_IS_AGP,
315         DRM_MIGHT_BE_AGP
316 };
317 #define DRM_AGP_MEM             struct agp_memory_info
318
319 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
320
321 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
322 #define DRM_MTRR_WC             MDF_WRITECOMBINE
323
324 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
325 do {                                                                    \
326         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
327              dev->lock.file_priv != file_priv) {                        \
328                 DRM_ERROR("%s called without lock held\n",              \
329                            __FUNCTION__);                               \
330                 return EINVAL;                                          \
331         }                                                               \
332 } while (0)
333
334 /* Returns -errno to shared code */
335 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
336 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
337         lwkt_serialize_enter(&dev->irq_lock);                   \
338         if (!(condition)) {                                     \
339             tsleep_interlock(&(queue), PCATCH);                 \
340             lwkt_serialize_exit(&dev->irq_lock);                \
341             ret = -tsleep(&(queue), PCATCH | PINTERLOCKED,      \
342                           "drmwtq", (timeout));                 \
343         } else {                                                \
344                 lwkt_serialize_exit(&dev->irq_lock);            \
345         }                                                       \
346 }
347
348 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
349     vm_memattr_t memattr);
350 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
351 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
352
353
354 #define dev_dbg(dev, fmt, ...) do {                                     \
355         if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {                      \
356                 device_printf((dev), "debug: " fmt, ## __VA_ARGS__);    \
357         }                                                               \
358 } while (0)
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 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                 \
404         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
405
406 typedef struct drm_magic_entry {
407         struct list_head head;
408         struct drm_hash_item hash_item;
409         struct drm_file        *priv;
410         struct drm_magic_entry *next;
411 } drm_magic_entry_t;
412
413 typedef struct drm_magic_head {
414         struct drm_magic_entry *head;
415         struct drm_magic_entry *tail;
416 } drm_magic_head_t;
417
418 typedef struct drm_buf {
419         int idx;                       /**< Index into master buflist */
420         int total;                     /**< Buffer size */
421         int order;                     /**< log-base-2(total) */
422         int used;                      /**< Amount of buffer in use (for DMA) */
423         unsigned long offset;          /**< Byte offset (used internally) */
424         void *address;                 /**< Address of buffer */
425         unsigned long bus_address;     /**< Bus address of buffer */
426         struct drm_buf *next;          /**< Kernel-only: used for free list */
427         __volatile__ int waiting;      /**< On kernel DMA queue */
428         __volatile__ int pending;      /**< On hardware DMA queue */
429         struct drm_file *file_priv;    /**< Private of holding file descr */
430         int context;                   /**< Kernel queue for this buffer */
431         int while_locked;              /**< Dispatch this buffer while locked */
432         enum {
433                 DRM_LIST_NONE = 0,
434                 DRM_LIST_FREE = 1,
435                 DRM_LIST_WAIT = 2,
436                 DRM_LIST_PEND = 3,
437                 DRM_LIST_PRIO = 4,
438                 DRM_LIST_RECLAIM = 5
439         } list;                        /**< Which list we're on */
440
441         int dev_priv_size;               /**< Size of buffer private storage */
442         void *dev_private;               /**< Per-buffer private storage */
443 } drm_buf_t;
444
445 /** bufs is one longer than it has to be */
446 struct drm_waitlist {
447         int count;                      /**< Number of possible buffers */
448         struct drm_buf **bufs;          /**< List of pointers to buffers */
449         struct drm_buf **rp;                    /**< Read pointer */
450         struct drm_buf **wp;                    /**< Write pointer */
451         struct drm_buf **end;           /**< End pointer */
452         struct spinlock *read_lock;
453         struct spinlock *write_lock;
454 };
455
456 typedef struct drm_dma_handle {
457         void *vaddr;
458         size_t size;
459         bus_addr_t busaddr;
460         bus_dma_tag_t tag;
461         bus_dmamap_t map;
462 } drm_dma_handle_t;
463
464 typedef struct drm_buf_entry {
465         int               buf_size;
466         int               buf_count;
467         drm_buf_t         *buflist;
468         int               seg_count;
469         int               page_order;
470         struct drm_dma_handle **seglist;
471
472         int low_mark;                   /**< Low water mark */
473         int high_mark;                  /**< High water mark */
474 } drm_buf_entry_t;
475
476 /* Event queued up for userspace to read */
477 struct drm_pending_event {
478         struct drm_event *event;
479         struct list_head link;
480         struct drm_file *file_priv;
481         pid_t pid; /* pid of requester, no guarantee it's valid by the time
482                       we deliver the event, for tracing only */
483         void (*destroy)(struct drm_pending_event *event);
484 };
485
486 /* initial implementaton using a linked list - todo hashtab */
487 struct drm_prime_file_private {
488         struct list_head head;
489 #ifdef DUMBBELL_WIP
490         struct mutex lock;
491 #endif /* DUMBBELL_WIP */
492 };
493
494 /** File private data */
495 struct drm_file {
496         int authenticated;
497         struct drm_device *dev;
498         int               master;
499
500         /* true when the client has asked us to expose stereo 3D mode flags */
501         bool stereo_allowed;
502         /*
503          * true if client understands CRTC primary planes and cursor planes
504          * in the plane list
505          */
506         unsigned universal_planes:1;
507         /* true if client understands atomic properties */
508         unsigned atomic:1;
509
510         pid_t             pid;
511         uid_t             uid;
512         drm_magic_t       magic;
513         unsigned long     ioctl_count;
514         struct list_head lhead;
515         struct kqinfo     dkq;
516
517         /** Mapping of mm object handles to object pointers. */
518         struct idr object_idr;
519         /** Lock for synchronization of access to object_idr. */
520         struct lock table_lock;
521
522         void             *driver_priv;
523
524         int               is_master;
525         struct drm_master *masterp;
526
527         /**
528          * fbs - List of framebuffers associated with this file.
529          *
530          * Protected by fbs_lock. Note that the fbs list holds a reference on
531          * the fb object to prevent it from untimely disappearing.
532          */
533         struct list_head fbs;
534         struct lock fbs_lock;
535
536         /** User-created blob properties; this retains a reference on the
537          *  property. */
538         struct list_head blobs;
539
540         wait_queue_head_t event_wait;
541         struct list_head  event_list;
542         int               event_space;
543
544         struct drm_prime_file_private prime;
545 };
546
547 /**
548  * Lock data.
549  */
550 struct drm_lock_data {
551         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
552         /** Private of lock holder's file (NULL=kernel) */
553         struct drm_file *file_priv;
554         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
555         unsigned long lock_time;        /**< Time of last lock in jiffies */
556 };
557
558 /* This structure, in the struct drm_device, is always initialized while the
559  * device
560  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
561  * when set marks that no further bufs may be allocated until device teardown
562  * occurs (when the last open of the device has closed).  The high/low
563  * watermarks of bufs are only touched by the X Server, and thus not
564  * concurrently accessed, so no locking is needed.
565  */
566 typedef struct drm_device_dma {
567
568         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
569         int buf_count;                  /**< total number of buffers */
570         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
571         int seg_count;
572         int page_count;                 /**< number of pages */
573         unsigned long *pagelist;        /**< page list */
574         unsigned long byte_count;
575         enum {
576                 _DRM_DMA_USE_AGP = 0x01,
577                 _DRM_DMA_USE_SG = 0x02,
578                 _DRM_DMA_USE_FB = 0x04,
579                 _DRM_DMA_USE_PCI_RO = 0x08
580         } flags;
581
582 } drm_device_dma_t;
583
584 typedef struct drm_sg_mem {
585         vm_offset_t vaddr;
586         vm_paddr_t *busaddr;
587         vm_pindex_t pages;
588 } drm_sg_mem_t;
589
590 /**
591  * Kernel side of a mapping
592  */
593 struct drm_local_map {
594         resource_size_t offset;  /**< Requested physical address (0 for SAREA)*/
595         unsigned long size;      /**< Requested physical size (bytes) */
596         enum drm_map_type type;  /**< Type of memory to map */
597         enum drm_map_flags flags;        /**< Flags */
598         void *handle;            /**< User-space: "Handle" to pass to mmap() */
599                                  /**< Kernel-space: kernel-virtual address */
600         int mtrr;                /**< MTRR slot used */
601 };
602
603 typedef struct drm_local_map drm_local_map_t;
604
605 /**
606  * Mappings list
607  */
608 struct drm_map_list {
609         struct list_head head;          /**< list head */
610         struct drm_hash_item hash;
611         struct drm_local_map *map;      /**< mapping */
612         uint64_t user_token;
613         struct drm_master *master;
614         struct drm_mm_node *file_offset_node;   /**< fake offset */
615 };
616
617 /**
618  * GEM specific mm private for tracking GEM objects
619  */
620 struct drm_gem_mm {
621         struct drm_vma_offset_manager vma_manager;
622         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
623         struct drm_open_hash offset_hash; /**< User token hash table for maps */
624         struct unrhdr *idxunr;
625 };
626
627
628 #include "drm_crtc.h"
629
630 /**
631  * struct drm_master - drm master structure
632  *
633  * @refcount: Refcount for this master object.
634  * @minor: Link back to minor char device we are master for. Immutable.
635  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
636  * @unique_len: Length of unique field. Protected by drm_global_mutex.
637  * @unique_size: Amount allocated. Protected by drm_global_mutex.
638  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
639  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
640  * @lock: DRI lock information.
641  * @driver_priv: Pointer to driver-private information.
642  */
643 struct drm_master {
644
645         struct kref refcount; /* refcount for this master */
646
647         struct list_head head; /**< each minor contains a list of masters */
648         struct drm_minor *minor; /**< link back to minor we are a master for */
649
650         char *unique;                   /**< Unique identifier: e.g., busid */
651         int unique_len;                 /**< Length of unique field */
652         int unique_size;                /**< amount allocated */
653
654         int blocked;                    /**< Blocked due to VC switch? */
655
656         struct drm_open_hash magiclist;
657         struct list_head magicfree;
658         struct drm_lock_data lock;
659         void *driver_priv;
660 };
661
662 /* Size of ringbuffer for vblank timestamps. Just double-buffer
663  * in initial implementation.
664  */
665 #define DRM_VBLANKTIME_RBSIZE 2
666
667 /* Flags and return codes for get_vblank_timestamp() driver function. */
668 #define DRM_CALLED_FROM_VBLIRQ 1
669 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
670 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
671
672 /* get_scanout_position() return flags */
673 #define DRM_SCANOUTPOS_VALID        (1 << 0)
674 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
675 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
676
677 #ifndef DMA_BIT_MASK
678 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
679 #endif
680
681 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
682
683 /**
684  * DRM driver structure. This structure represent the common code for
685  * a family of cards. There will one drm_device for each card present
686  * in this family
687  */
688 struct drm_driver {
689         int     (*load)(struct drm_device *, unsigned long flags);
690         int     (*use_msi)(struct drm_device *, unsigned long flags);
691         int     (*firstopen)(struct drm_device *);
692         int     (*open)(struct drm_device *, struct drm_file *);
693         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
694         void    (*postclose)(struct drm_device *, struct drm_file *);
695         void    (*lastclose)(struct drm_device *);
696         int     (*unload)(struct drm_device *);
697         void    (*reclaim_buffers_locked)(struct drm_device *,
698                                           struct drm_file *file_priv);
699         int     (*dma_ioctl)(struct drm_device *dev, void *data,
700                              struct drm_file *file_priv);
701         void    (*dma_ready)(struct drm_device *);
702         int     (*dma_quiescent)(struct drm_device *);
703         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
704                                              enum drm_lock_flags flags);
705         int     (*dma_flush_unblock)(struct drm_device *, int context,
706                                      enum drm_lock_flags flags);
707         int     (*context_ctor)(struct drm_device *dev, int context);
708         int     (*context_dtor)(struct drm_device *dev, int context);
709         int     (*kernel_context_switch)(struct drm_device *dev, int old,
710                                          int new);
711         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
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         void    (*irq_handler)(void *arg);
716
717         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
718         int     (*enable_vblank)(struct drm_device *dev, int crtc);
719         void    (*disable_vblank)(struct drm_device *dev, int crtc);
720
721         /**
722          * Called by vblank timestamping code.
723          *
724          * Return the current display scanout position from a crtc, and an
725          * optional accurate ktime_get timestamp of when position was measured.
726          *
727          * \param dev  DRM device.
728          * \param crtc Id of the crtc to query.
729          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
730          * \param *vpos Target location for current vertical scanout position.
731          * \param *hpos Target location for current horizontal scanout position.
732          * \param *stime Target location for timestamp taken immediately before
733          *               scanout position query. Can be NULL to skip timestamp.
734          * \param *etime Target location for timestamp taken immediately after
735          *               scanout position query. Can be NULL to skip timestamp.
736          *
737          * Returns vpos as a positive number while in active scanout area.
738          * Returns vpos as a negative number inside vblank, counting the number
739          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
740          * until start of active scanout / end of vblank."
741          *
742          * \return Flags, or'ed together as follows:
743          *
744          * DRM_SCANOUTPOS_VALID = Query successful.
745          * DRM_SCANOUTPOS_INVBL = Inside vblank.
746          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
747          * this flag means that returned position may be offset by a constant
748          * but unknown small number of scanlines wrt. real scanout position.
749          *
750          */
751         int (*get_scanout_position) (struct drm_device *dev, int crtc,
752                                      unsigned int flags,
753                                      int *vpos, int *hpos, ktime_t *stime,
754                                      ktime_t *etime);
755
756         int     (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
757                     int *max_error, struct timeval *vblank_time,
758                     unsigned flags);
759
760         void    (*gem_free_object)(struct drm_gem_object *obj);
761         int     (*gem_open_object)(struct drm_gem_object *, struct drm_file *);
762         void    (*gem_close_object)(struct drm_gem_object *, struct drm_file *);
763
764         struct cdev_pager_ops *gem_pager_ops;
765
766         int     (*dumb_create)(struct drm_file *file_priv,
767                     struct drm_device *dev, struct drm_mode_create_dumb *args);
768         int     (*dumb_map_offset)(struct drm_file *file_priv,
769                     struct drm_device *dev, uint32_t handle, uint64_t *offset);
770         int     (*dumb_destroy)(struct drm_file *file_priv,
771                     struct drm_device *dev, uint32_t handle);
772
773         int     (*sysctl_init)(struct drm_device *dev,
774                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
775         void    (*sysctl_cleanup)(struct drm_device *dev);
776
777         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
778
779         /**
780          * Called by \c drm_device_is_agp.  Typically used to determine if a
781          * card is really attached to AGP or not.
782          *
783          * \param dev  DRM device handle
784          *
785          * \returns
786          * One of three values is returned depending on whether or not the
787          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
788          * (return of 1), or may or may not be AGP (return of 2).
789          */
790         int     (*device_is_agp) (struct drm_device * dev);
791
792         int     major;
793         int     minor;
794         int     patchlevel;
795         const char *name;               /* Simple driver name              */
796         const char *desc;               /* Longer driver name              */
797         const char *date;               /* Date of last major changes.     */
798
799         u32 driver_features;
800         int dev_priv_size;
801         const struct drm_ioctl_desc *ioctls;
802         int num_ioctls;
803 };
804
805 /**
806  * Info file list entry. This structure represents a debugfs or proc file to
807  * be created by the drm core
808  */
809 struct drm_info_list {
810         const char *name; /** file name */
811         int (*show)(struct seq_file*, void*); /** show callback */
812         u32 driver_features; /**< Required driver features for this entry */
813         void *data;
814 };
815
816 /**
817  * debugfs node structure. This structure represents a debugfs file.
818  */
819 struct drm_info_node {
820         struct list_head list;
821         struct drm_minor *minor;
822         const struct drm_info_list *info_ent;
823         struct dentry *dent;
824 };
825
826 /**
827  * DRM minor structure. This structure represents a drm minor number.
828  */
829 struct drm_minor {
830         int index;                      /**< Minor device number */
831         int type;                       /**< Control or render */
832         device_t kdev;                  /**< OS device */
833         struct drm_device *dev;
834
835         struct drm_master *master; /* currently active master for this node */
836         struct list_head master_list;
837         struct drm_mode_group mode_group;
838 };
839
840 struct drm_pending_vblank_event {
841         struct drm_pending_event base;
842         int pipe;
843         struct drm_event_vblank event;
844 };
845
846 struct drm_sysctl_info {
847         struct sysctl_ctx_list ctx;
848         char   name[2];
849 };
850
851 /* Length for the array of resource pointers for drm_get_resource_*. */
852 #define DRM_MAX_PCI_RESOURCE    6
853
854 struct drm_vblank_crtc {
855         struct drm_device *dev;         /* pointer to the drm_device */
856         wait_queue_head_t queue;        /**< VBLANK wait queue */
857         struct timer_list disable_timer;                /* delayed disable timer */
858
859         /* vblank counter, protected by dev->vblank_time_lock for writes */
860         unsigned long count;
861         /* vblank timestamps, protected by dev->vblank_time_lock for writes */
862         struct timeval time[DRM_VBLANKTIME_RBSIZE];
863
864         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
865         u32 last;                       /* protected by dev->vbl_lock, used */
866                                         /* for wraparound handling */
867         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
868         unsigned int inmodeset;         /* Display driver is setting mode */
869         int crtc;                       /* crtc index */
870         bool enabled;                   /* so we don't call enable more than
871                                            once per disable */
872 };
873
874 /**
875  * DRM device structure. This structure represent a complete card that
876  * may contain multiple heads.
877  */
878 struct drm_device {
879         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
880
881         char              *unique;      /* Unique identifier: e.g., busid  */
882         int               unique_len;   /* Length of unique field          */
883         struct cdev       *devnode;     /* Device number for mknod         */
884         int               if_version;   /* Highest interface version set */
885
886         int               flags;        /* Flags to open(2)                */
887
888                                 /* Locks */
889         struct spinlock   dma_lock;     /* protects dev->dma */
890         struct lwkt_serialize irq_lock; /* protects irq condition checks */
891         struct lock       dev_lock;     /* protects everything else */
892
893         /** \name Locks */
894         /*@{ */
895         struct lock struct_mutex;       /**< For others */
896         struct lock master_mutex;       /**< For drm_minor::master */
897         /*@} */
898
899         /** \name Usage Counters */
900         /*@{ */
901         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
902         struct spinlock buf_lock;       /**< For drm_device::buf_use and a few other things. */
903         int buf_use;                    /**< Buffers in use -- cannot alloc */
904         atomic_t buf_alloc;             /**< Buffer allocation in progress */
905         /*@} */
906
907
908         /** \name Performance counters */
909         /*@{ */
910         unsigned long     counters;
911         enum drm_stat_type      types[15];
912         atomic_t          counts[15];
913         /*@} */
914
915                                 /* Authentication */
916         struct drm_open_hash magiclist; /**< magic hash table */
917         struct list_head magicfree;
918
919         struct list_head filelist;
920
921         /** \name Memory management */
922         /*@{ */
923         struct list_head maplist;       /**< Linked list of regions */
924         int map_count;                  /**< Number of mappable regions */
925         struct drm_open_hash map_hash;  /**< User token hash table for maps */
926
927         /** \name Context handle management */
928         /*@{ */
929         struct list_head ctxlist;       /**< Linked list of context handles */
930         int ctx_count;                  /**< Number of context handles */
931         struct lock ctxlist_mutex;      /**< For ctxlist */
932
933         struct idr ctx_idr;
934
935         /*@} */
936
937         struct drm_lock_data lock;      /* Information on hardware lock    */
938
939                                 /* DMA queues (contexts) */
940         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
941
942         int               irq;          /* Interrupt used by board         */
943         int               irq_type;     /* IRQ type (MSI enabled or not) */
944         int               irqrid;       /* Interrupt used by board */
945         struct resource   *irqr;        /* Resource for interrupt used by board    */
946         void              *irqh;        /* Handle from bus_setup_intr      */
947
948         /* Storage of resource pointers for drm_get_resource_* */
949         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
950         int               pcirid[DRM_MAX_PCI_RESOURCE];
951
952         int               pci_domain;
953         int               pci_bus;
954         int               pci_slot;
955         int               pci_func;
956
957         /** \name Context support */
958         /*@{ */
959         int irq_enabled;                /**< True if irq handler is enabled */
960         __volatile__ long context_flag; /**< Context swapping flag */
961         __volatile__ long interrupt_flag; /**< Interruption handler flag */
962         __volatile__ long dma_flag;     /**< DMA dispatch flag */
963         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
964         int last_checked;               /**< Last context checked for DMA */
965         int last_context;               /**< Last current context */
966         unsigned long last_switch;      /**< jiffies at last context switch */
967         /*@} */
968
969         /** \name VBLANK IRQ support */
970         /*@{ */
971
972         /*
973          * At load time, disabling the vblank interrupt won't be allowed since
974          * old clients may not call the modeset ioctl and therefore misbehave.
975          * Once the modeset ioctl *has* been called though, we can safely
976          * disable them when unused.
977          */
978         bool vblank_disable_allowed;
979
980         /*
981          * If true, vblank interrupt will be disabled immediately when the
982          * refcount drops to zero, as opposed to via the vblank disable
983          * timer.
984          * This can be set to true it the hardware has a working vblank
985          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
986          * appropriately.
987          */
988         bool vblank_disable_immediate;
989
990         /* array of size num_crtcs */
991         struct drm_vblank_crtc *vblank;
992
993         struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
994         struct lock vbl_lock;
995         struct timer_list vblank_disable_timer;
996
997         u32 max_vblank_count;           /**< size of vblank counter register */
998
999         /**
1000          * List of events
1001          */
1002         struct list_head vblank_event_list;
1003         struct lock event_lock;
1004
1005         /*@} */
1006
1007         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
1008
1009                                 /* Sysctl support */
1010         struct drm_sysctl_info *sysctl;
1011
1012
1013         drm_sg_mem_t      *sg;  /* Scatter gather memory */
1014         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1015
1016         unsigned long     *ctx_bitmap;
1017         void              *dev_private;
1018
1019         void              *drm_ttm_bdev;
1020
1021         /*@} */
1022
1023         struct drm_agp_head *agp;       /**< AGP data */
1024
1025         struct device *dev;             /**< Device structure */
1026         struct pci_dev *pdev;           /**< PCI device structure */
1027
1028         struct drm_driver *driver;
1029         struct drm_local_map *agp_buffer_map;
1030         unsigned int agp_buffer_token;
1031         struct drm_minor *control;              /**< Control node for card */
1032         struct drm_minor *primary;              /**< render type primary screen head */
1033
1034         struct drm_mode_config mode_config;     /**< Current mode config */
1035
1036         /** \name GEM information */
1037         /*@{ */
1038         struct lock object_name_lock;
1039         struct idr object_name_idr;
1040         /*@} */
1041         void             *mm_private;
1042
1043         void *sysctl_private;
1044         char busid_str[128];
1045         int modesetting;
1046
1047         int switch_power_state;
1048
1049         atomic_t unplugged; /* device has been unplugged or gone away */
1050 };
1051
1052 #define DRM_SWITCH_POWER_ON 0
1053 #define DRM_SWITCH_POWER_OFF 1
1054 #define DRM_SWITCH_POWER_CHANGING 2
1055 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1056
1057 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1058                                              int feature)
1059 {
1060         return ((dev->driver->driver_features & feature) ? 1 : 0);
1061 }
1062
1063 static inline void drm_device_set_unplugged(struct drm_device *dev)
1064 {
1065         smp_wmb();
1066         atomic_set(&dev->unplugged, 1);
1067 }
1068
1069 static inline int drm_device_is_unplugged(struct drm_device *dev)
1070 {
1071         int ret = atomic_read(&dev->unplugged);
1072         smp_rmb();
1073         return ret;
1074 }
1075
1076 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1077 {
1078         return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1079 }
1080
1081 /**
1082  * drm_is_master() - Check whether a DRM open-file is DRM-Master
1083  * @file: DRM open-file context
1084  *
1085  * This checks whether a DRM open-file context is owner of the master context
1086  * attached to it. If a file owns a master context, it's called DRM-Master.
1087  * Per DRM device, only one such file can be DRM-Master at a time.
1088  *
1089  * Returns: True if the file is DRM-Master, otherwise false.
1090  */
1091 static inline bool drm_is_master(const struct drm_file *file)
1092 {
1093 #if 0
1094         return file->master && file->master == file->minor->master;
1095 #else
1096         return true;
1097 #endif
1098 }
1099
1100 /******************************************************************/
1101 /** \name Internal function definitions */
1102 /*@{*/
1103
1104 #if __OS_HAS_AGP
1105 static inline int drm_core_has_AGP(struct drm_device *dev)
1106 {
1107         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1108 }
1109 #else
1110 #define drm_core_has_AGP(dev) (0)
1111 #endif
1112
1113 extern int      drm_notyet_flag;
1114 extern unsigned int drm_rnodes;
1115 extern unsigned int drm_universal_planes;
1116
1117 extern int drm_vblank_offdelay;
1118 extern unsigned int drm_timestamp_precision;
1119
1120                                 /* Driver support (drm_drv.h) */
1121 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1122 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1123 int     drm_create_cdevs(device_t kdev);
1124 d_ioctl_t drm_ioctl;
1125 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1126
1127                                 /* Device support (drm_fops.h) */
1128 d_open_t drm_open;
1129 d_close_t drm_close;
1130 d_read_t drm_read;
1131 d_kqfilter_t drm_kqfilter;
1132 int drm_release(device_t kdev);
1133
1134 d_mmap_t drm_mmap;
1135 d_mmap_single_t drm_mmap_single;
1136
1137 void drm_cdevpriv_dtor(void *cd);
1138
1139 int drm_add_busid_modesetting(struct drm_device *dev,
1140     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1141
1142 /* File operations helpers (drm_fops.c) */
1143 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1144                                          DRM_STRUCTPROC *p,
1145                                         struct drm_device *dev,
1146                                         struct file *fp);
1147 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
1148                                         DRM_STRUCTPROC *p);
1149
1150 #ifdef DUMBBELL_WIP
1151 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1152                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1153                 int *prime_fd);
1154 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1155                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1156
1157 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1158                                         struct drm_file *file_priv);
1159 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1160                                         struct drm_file *file_priv);
1161
1162 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1163                                             dma_addr_t *addrs, int max_pages);
1164 #endif /* DUMBBELL_WIP */
1165 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1166 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1167
1168 #ifdef DUMBBELL_WIP
1169 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1170 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1171 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1172 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1173 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1174
1175 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1176 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1177                          struct drm_gem_object **obj);
1178 #endif /* DUMBBELL_WIP */
1179
1180 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1181 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1182
1183 /* Locking IOCTL support (drm_lock.c) */
1184 int     drm_lock_take(struct drm_lock_data *lock_data,
1185                       unsigned int context);
1186 int     drm_lock_transfer(struct drm_lock_data *lock_data,
1187                           unsigned int context);
1188 int     drm_lock_free(struct drm_lock_data *lock_data,
1189                       unsigned int context);
1190
1191 /*
1192  * These are exported to drivers so that they can implement fencing using
1193  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1194  */
1195
1196 unsigned long drm_get_resource_start(struct drm_device *dev,
1197                                      unsigned int resource);
1198 unsigned long drm_get_resource_len(struct drm_device *dev,
1199                                    unsigned int resource);
1200
1201                                 /* IRQ support (drm_irq.h) */
1202 extern int drm_irq_install(struct drm_device *dev, int irq);
1203 int     drm_irq_uninstall(struct drm_device *dev);
1204 void    drm_driver_irq_preinstall(struct drm_device *dev);
1205 void    drm_driver_irq_postinstall(struct drm_device *dev);
1206 void    drm_driver_irq_uninstall(struct drm_device *dev);
1207
1208 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1209 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1210                            struct drm_file *filp);
1211 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1212 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1213 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1214                                      struct timeval *vblanktime);
1215 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1216                                      struct drm_pending_vblank_event *e);
1217 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1218                                        struct drm_pending_vblank_event *e);
1219 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1220 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1221 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1222 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1223 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1224 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1225 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
1226 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1227 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1228 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1229 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1230 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1231 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1232 extern void drm_vblank_cleanup(struct drm_device *dev);
1233
1234 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1235                                                  int crtc, int *max_error,
1236                                                  struct timeval *vblank_time,
1237                                                  unsigned flags,
1238                                                  const struct drm_crtc *refcrtc,
1239                                                  const struct drm_display_mode *mode);
1240 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1241                                             const struct drm_display_mode *mode);
1242
1243 /**
1244  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1245  * @crtc: which CRTC's vblank waitqueue to retrieve
1246  *
1247  * This function returns a pointer to the vblank waitqueue for the CRTC.
1248  * Drivers can use this to implement vblank waits using wait_event() & co.
1249  */
1250 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1251 {
1252        return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1253 }
1254
1255 /* Modesetting support */
1256 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1257 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1258
1259                                 /* AGP/GART support (drm_agpsupport.h) */
1260
1261 #include <drm/drm_agpsupport.h>
1262
1263 /* sysctl support (drm_sysctl.h) */
1264 extern int              drm_sysctl_init(struct drm_device *dev);
1265 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1266
1267 /* Locking IOCTL support (drm_drv.c) */
1268 int     drm_lock(struct drm_device *dev, void *data,
1269                  struct drm_file *file_priv);
1270 int     drm_unlock(struct drm_device *dev, void *data,
1271                    struct drm_file *file_priv);
1272 int     drm_setversion(struct drm_device *dev, void *data,
1273                        struct drm_file *file_priv);
1274
1275 /*
1276  * These are exported to drivers so that they can implement fencing using
1277  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1278  */
1279
1280 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1281
1282 /* Misc. IOCTL support (drm_ioctl.c) */
1283 int     drm_noop(struct drm_device *dev, void *data,
1284                  struct drm_file *file_priv);
1285
1286 /* Cache management (drm_cache.c) */
1287 void drm_clflush_pages(struct vm_page *pages[], unsigned long num_pages);
1288 void drm_clflush_sg(struct sg_table *st);
1289 void drm_clflush_virt_range(void *addr, unsigned long length);
1290
1291 /* DMA support (drm_dma.c) */
1292 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1293
1294                                 /* Stub support (drm_stub.h) */
1295
1296 extern void drm_put_dev(struct drm_device *dev);
1297 extern void drm_unplug_dev(struct drm_device *dev);
1298 extern unsigned int drm_debug;
1299 extern bool drm_atomic;
1300
1301 /* Scatter Gather Support (drm_scatter.c) */
1302 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1303                            struct drm_file *file_priv);
1304 int     drm_sg_free(struct drm_device *dev, void *data,
1305                     struct drm_file *file_priv);
1306
1307 /* consistent PCI memory functions (drm_pci.c) */
1308 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1309                                        size_t align);
1310 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1311
1312                                /* sysfs support (drm_sysfs.c) */
1313 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1314
1315 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1316     vm_size_t size, struct vm_object **obj_res, int nprot);
1317 void drm_gem_pager_dtr(void *obj);
1318
1319 struct ttm_bo_device;
1320 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1321     vm_size_t size, struct vm_object **obj_res, int nprot);
1322 struct ttm_buffer_object;
1323 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1324
1325 void drm_device_lock_mtx(struct drm_device *dev);
1326 void drm_device_unlock_mtx(struct drm_device *dev);
1327 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1328     const char *msg, int timeout);
1329 void drm_device_assert_mtx_locked(struct drm_device *dev);
1330 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1331
1332 void drm_device_lock_struct(struct drm_device *dev);
1333 void drm_device_unlock_struct(struct drm_device *dev);
1334 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1335     const char *msg, int timeout);
1336 void drm_device_assert_struct_locked(struct drm_device *dev);
1337 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1338
1339 void drm_compat_locking_init(struct drm_device *dev);
1340 void drm_sleep_locking_init(struct drm_device *dev);
1341
1342 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1343 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1344 void drm_fini_pdev(struct pci_dev **pdev);
1345
1346 /* Inline replacements for drm_alloc and friends */
1347 static __inline__ void *
1348 drm_alloc(size_t size, struct malloc_type *area)
1349 {
1350         return kmalloc(size, area, M_WAITOK | M_NULLOK);
1351 }
1352
1353 static __inline__ void *
1354 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1355 {
1356         return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1357 }
1358
1359 static __inline__ void
1360 drm_free(void *pt, struct malloc_type *area)
1361 {
1362         /* kfree is special!!! */
1363         if (pt != NULL)
1364                 (kfree)(pt, area);
1365 }
1366
1367 static __inline__ struct drm_local_map *
1368 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1369 {
1370         struct drm_map_list *_entry;
1371
1372         list_for_each_entry(_entry, &dev->maplist, head) {
1373                 if (offset == (unsigned long)_entry->map->handle)
1374                         return _entry->map;
1375         }
1376         return NULL;
1377 }
1378
1379 static __inline__ void drm_core_dropmap(struct drm_map *map)
1380 {
1381 }
1382
1383 #include <drm/drm_mem_util.h>
1384
1385 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1386                                  struct device *parent);
1387 void drm_dev_ref(struct drm_device *dev);
1388 void drm_dev_unref(struct drm_device *dev);
1389 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1390 void drm_dev_unregister(struct drm_device *dev);
1391 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1392
1393 /* FreeBSD compatibility macros */
1394 #define VM_OBJECT_WLOCK(object)         VM_OBJECT_LOCK(object)
1395 #define VM_OBJECT_WUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1396
1397 #define DRM_PCIE_SPEED_25 1
1398 #define DRM_PCIE_SPEED_50 2
1399 #define DRM_PCIE_SPEED_80 4
1400
1401 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1402
1403 /* XXX bad */
1404 #define drm_can_sleep() (HZ & 1)
1405
1406 #endif /* __KERNEL__ */
1407 #endif /* _DRM_P_H_ */