Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / drm / include / drm / drmP.h
1 /**
2  * \file drmP.h
3  * Private header for Direct Rendering Manager
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8
9 /*
10  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12  * Copyright (c) 2009-2010, Code Aurora Forum.
13  * All rights reserved.
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the next
23  * paragraph) shall be included in all copies or substantial portions of the
24  * Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
29  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32  * OTHER DEALINGS IN THE SOFTWARE.
33  */
34
35 #ifndef _DRM_P_H_
36 #define _DRM_P_H_
37
38 #if defined(_KERNEL) || defined(__KERNEL__)
39
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/module.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/sglist.h>
49 #include <sys/stat.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/lock.h>
53 #include <sys/spinlock.h>
54 #include <sys/spinlock2.h>
55 #include <sys/fcntl.h>
56 #include <sys/uio.h>
57 #include <sys/filio.h>
58 #include <sys/sysctl.h>
59 #include <sys/bus.h>
60 #include <sys/queue.h>
61 #include <sys/signalvar.h>
62 #include <sys/poll.h>
63 #include <sys/sbuf.h>
64 #include <sys/taskqueue.h>
65 #include <sys/tree.h>
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page2.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vm_param.h>
75 #include <machine/param.h>
76 #include <machine/pmap.h>
77 #if defined(__i386__) || defined(__amd64__)
78 #include <machine/specialreg.h>
79 #endif
80 #include <machine/sysarch.h>
81 #include <sys/endian.h>
82 #include <sys/mman.h>
83 #include <sys/rman.h>
84 #include <sys/memrange.h>
85 #include <dev/agp/agpvar.h>
86 #include <sys/agpio.h>
87 #include <sys/mutex.h>
88
89 MALLOC_DECLARE(M_DRM);
90
91 #include <uapi_drm/drm.h>
92 #include <uapi_drm/drm_sarea.h>
93
94 #include <linux/atomic.h>
95 #include <linux/bug.h>
96 #include <linux/err.h>
97 #include <linux/idr.h>
98 #include <linux/pci.h>
99 #include <linux/jiffies.h>
100 #include <linux/kernel.h>
101 #include <linux/kref.h>
102 #include <linux/list.h>
103 #include <linux/mm.h>
104 #include <linux/moduleparam.h>
105 #include <linux/mutex.h>
106 #include <linux/slab.h>
107 #include <linux/scatterlist.h>
108 #include <linux/timer.h>
109 #include <asm/io.h>
110 #include <linux/types.h>
111 #include <linux/wait.h>
112 #include <linux/workqueue.h>
113
114 #include <asm/uaccess.h>
115
116 #include <drm/drm_vma_manager.h>
117
118 struct drm_file;
119 struct drm_device;
120
121 #include <drm/drm_os_linux.h>
122 #include <drm/drm_hashtab.h>
123 #include <drm/drm_mm.h>
124
125 #define DRM_UT_CORE             0x01
126 #define DRM_UT_DRIVER           0x02
127 #define DRM_UT_KMS              0x04
128 #define DRM_UT_PRIME            0x08
129
130 #include "opt_drm.h"
131 #ifdef DRM_DEBUG
132 #  if DRM_DEBUG>1
133 #    define DRM_DEBUG_DEFAULT_ON 2
134 #  else
135 #    define DRM_DEBUG_DEFAULT_ON 1
136 #  endif
137 #undef DRM_DEBUG
138 #endif /* DRM_DEBUG */
139
140 #define DRM_DEBUGBITS_DEBUG             0x1
141 #define DRM_DEBUGBITS_KMS               0x2
142 #define DRM_DEBUGBITS_FAILED_IOCTL      0x4
143 #define DRM_DEBUGBITS_VERBOSE           0x8
144
145 #undef DRM_LINUX
146 #define DRM_LINUX 0
147
148 /***********************************************************************/
149 /** \name DRM template customization defaults */
150 /*@{*/
151
152 /* driver capabilities and requirements mask */
153 #define DRIVER_USE_AGP     0x1
154 #define DRIVER_REQUIRE_AGP 0x2
155 #define DRIVER_PCI_DMA     0x8
156 #define DRIVER_SG          0x10
157 #define DRIVER_HAVE_DMA    0x20
158 #define DRIVER_HAVE_IRQ    0x40
159 #define DRIVER_IRQ_SHARED  0x80
160 #define DRIVER_IRQ_VBL     0x100
161 #define DRIVER_DMA_QUEUE   0x200
162 #define DRIVER_FB_DMA      0x400
163 #define DRIVER_IRQ_VBL2    0x800
164 #define DRIVER_GEM         0x1000
165 #define DRIVER_MODESET     0x2000
166 #define DRIVER_PRIME       0x4000
167
168 /***********************************************************************/
169 /** \name Begin the DRM... */
170 /*@{*/
171
172 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
173 #define DRM_KERNEL_CONTEXT    0  /* Change drm_resctx if changed          */
174 #define DRM_RESERVED_CONTEXTS 1  /* Change drm_resctx if changed          */
175
176 #define DRM_GEM_MAPPING_MASK    (3ULL << 62)
177 #define DRM_GEM_MAPPING_KEY     (2ULL << 62) /* Non-canonical address form */
178 #define DRM_GEM_MAX_IDX         0x3fffff
179 #define DRM_GEM_MAPPING_IDX(o)  (((o) >> 40) & DRM_GEM_MAX_IDX)
180 #define DRM_GEM_MAPPING_OFF(i)  (((uint64_t)(i)) << 40)
181 #define DRM_GEM_MAPPING_MAPOFF(o) \
182     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
183
184 SYSCTL_DECL(_hw_drm);
185
186 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
187
188 /***********************************************************************/
189 /** \name Internal types and structures */
190 /*@{*/
191
192 #define DRM_ARRAY_SIZE(x) NELEM(x)
193 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
194 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
195
196 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
197
198 #define __OS_HAS_AGP    1
199
200 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
201 #define DRM_DEV_UID     0
202 #define DRM_DEV_GID     0
203
204 #define DRM_WAKEUP(w)           wakeup((void *)w)
205 #define DRM_WAKEUP_INT(w)       wakeup(w)
206 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
207
208 #define DRM_CURPROC             curthread
209 #define DRM_STRUCTPROC          struct thread
210 #define DRM_CURRENTPID          (curproc != NULL ? curproc->p_pid : -1)
211 #define DRM_LOCK(dev)           lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
212 #define DRM_UNLOCK(dev)         lockmgr(&(dev)->struct_mutex, LK_RELEASE)
213 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)                  \
214     (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
215 #if defined(INVARIANTS)
216 #define DRM_LOCK_ASSERT(dev)    KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
217 #define DRM_UNLOCK_ASSERT(dev)  KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
218 #else
219 #define DRM_LOCK_ASSERT(d)
220 #define DRM_UNLOCK_ASSERT(d)
221 #endif
222
223 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
224
225 #define DRM_IRQ_ARGS            void *arg
226 typedef void                    irqreturn_t;
227 #define IRQ_HANDLED             /* nothing */
228 #define IRQ_NONE                /* nothing */
229
230 enum {
231         DRM_IS_NOT_AGP,
232         DRM_IS_AGP,
233         DRM_MIGHT_BE_AGP
234 };
235 #define DRM_AGP_MEM             struct agp_memory_info
236
237 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
238
239 /* DRM_SUSER returns true if the user is superuser */
240 #define DRM_SUSER(p)            (priv_check(p, PRIV_DRIVER) == 0)
241 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
242 #define DRM_MTRR_WC             MDF_WRITECOMBINE
243
244 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
245  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
246  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
247  */
248 #define DRM_READMEMORYBARRIER()         cpu_lfence()
249 #define DRM_WRITEMEMORYBARRIER()        cpu_sfence()
250 #define DRM_MEMORYBARRIER()             cpu_mfence()
251
252 #define DRM_VERIFYAREA_READ( uaddr, size )              \
253         (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
254
255 #define DRM_COPY_TO_USER(user, kern, size) \
256         copyout(kern, user, size)
257 #define DRM_COPY_FROM_USER(kern, user, size) \
258         copyin(user, kern, size)
259 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
260         copyin(arg2, arg1, arg3)
261 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
262         copyout(arg2, arg1, arg3)
263 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
264         ((val) = fuword32(uaddr), 0)
265
266 #define drm_can_sleep() (DRM_HZ & 1)
267
268 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
269         (_map) = (_dev)->context_sareas[_ctx];          \
270 } while(0)
271
272 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
273 do {                                                                    \
274         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
275              dev->lock.file_priv != file_priv) {                        \
276                 DRM_ERROR("%s called without lock held\n",              \
277                            __FUNCTION__);                               \
278                 return EINVAL;                                          \
279         }                                                               \
280 } while (0)
281
282 /* Returns -errno to shared code */
283 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
284 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
285         DRM_UNLOCK(dev);                                        \
286         lwkt_serialize_enter(&dev->irq_lock);                   \
287         if (!(condition)) {                                     \
288             tsleep_interlock(&(queue), PCATCH);                 \
289             lwkt_serialize_exit(&dev->irq_lock);                \
290             ret = -tsleep(&(queue), PCATCH | PINTERLOCKED,      \
291                           "drmwtq", (timeout));                 \
292         } else {                                                \
293                 lwkt_serialize_exit(&dev->irq_lock);            \
294         }                                                       \
295         DRM_LOCK(dev);                                          \
296 }
297
298 __inline static void
299 vm_page_unhold_pages(vm_page_t *ma, int count)
300 {
301         int i;
302
303         for (i = 0; i < count; i++)
304                 vm_page_unhold(ma[i]);
305 }
306
307 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
308     vm_memattr_t memattr);
309 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
310 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
311
312 /***********************************************************************/
313 /** \name Macros to make printk easier */
314 /*@{*/
315
316 /**
317  * Error output.
318  *
319  * \param fmt printf() like format string.
320  * \param arg arguments
321  */
322 #define DRM_ERROR(fmt, ...) \
323         kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,          \
324             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
325
326 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
327
328 #define DRM_INFO_ONCE(fmt, ...)                         \
329         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
330
331 #define DRM_DEBUG(fmt, ...) do {                                        \
332         if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0)             \
333                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
334                         __func__ , ##__VA_ARGS__);                      \
335 } while (0)
336
337 #define DRM_DEBUG_VERBOSE(fmt, ...) do {                                \
338         if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0)           \
339                 kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID, \
340                         __func__ , ##__VA_ARGS__);                      \
341 } while (0)
342
343 #define DRM_DEBUG_KMS(fmt, ...) do {                                    \
344         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
345                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
346                         __func__ , ##__VA_ARGS__);                      \
347 } while (0)
348
349 #define DRM_DEBUG_DRIVER(fmt, ...) do {                                 \
350         if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)                       \
351                 kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
352                         __func__ , ##__VA_ARGS__);                      \
353 } while (0)
354
355 #define DRM_LOG_KMS(fmt, ...) do {                                      \
356         kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,     \
357                         __func__ , ##__VA_ARGS__);                      \
358 } while (0)
359
360 #define dev_dbg(dev, fmt, ...) do {                                     \
361         if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {                      \
362                 device_printf((dev), "debug: " fmt, ## __VA_ARGS__);    \
363         }                                                               \
364 } while (0)
365
366 typedef struct drm_pci_id_list
367 {
368         int vendor;
369         int device;
370         long driver_private;
371         char *name;
372 } drm_pci_id_list_t;
373
374 struct drm_msi_blacklist_entry
375 {
376         int vendor;
377         int device;
378 };
379
380 /**
381  * Ioctl function type.
382  *
383  * \param inode device inode.
384  * \param file_priv DRM file private pointer.
385  * \param cmd command.
386  * \param arg argument.
387  */
388 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
389                         struct drm_file *file_priv);
390
391 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
392                                unsigned long arg);
393
394 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
395
396 #define DRM_AUTH        0x1
397 #define DRM_MASTER      0x2
398 #define DRM_ROOT_ONLY   0x4
399 #define DRM_CONTROL_ALLOW 0x8
400 #define DRM_UNLOCKED    0x10
401
402 struct drm_ioctl_desc {
403         unsigned long cmd;
404         int (*func)(struct drm_device *dev, void *data,
405                     struct drm_file *file_priv);
406         int flags;
407         unsigned int cmd_drv;
408 };
409
410 /**
411  * Creates a driver or general drm_ioctl_desc array entry for the given
412  * ioctl, for use by drm_ioctl().
413  */
414 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                 \
415         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
416
417 typedef struct drm_magic_entry {
418         struct list_head head;
419         struct drm_hash_item hash_item;
420         struct drm_file        *priv;
421         struct drm_magic_entry *next;
422 } drm_magic_entry_t;
423
424 typedef struct drm_magic_head {
425         struct drm_magic_entry *head;
426         struct drm_magic_entry *tail;
427 } drm_magic_head_t;
428
429 typedef struct drm_buf {
430         int idx;                       /**< Index into master buflist */
431         int total;                     /**< Buffer size */
432         int order;                     /**< log-base-2(total) */
433         int used;                      /**< Amount of buffer in use (for DMA) */
434         unsigned long offset;          /**< Byte offset (used internally) */
435         void *address;                 /**< Address of buffer */
436         unsigned long bus_address;     /**< Bus address of buffer */
437         struct drm_buf *next;          /**< Kernel-only: used for free list */
438         __volatile__ int waiting;      /**< On kernel DMA queue */
439         __volatile__ int pending;      /**< On hardware DMA queue */
440         struct drm_file *file_priv;    /**< Private of holding file descr */
441         int context;                   /**< Kernel queue for this buffer */
442         int while_locked;              /**< Dispatch this buffer while locked */
443         enum {
444                 DRM_LIST_NONE = 0,
445                 DRM_LIST_FREE = 1,
446                 DRM_LIST_WAIT = 2,
447                 DRM_LIST_PEND = 3,
448                 DRM_LIST_PRIO = 4,
449                 DRM_LIST_RECLAIM = 5
450         } list;                        /**< Which list we're on */
451
452         int dev_priv_size;               /**< Size of buffer private storage */
453         void *dev_private;               /**< Per-buffer private storage */
454 } drm_buf_t;
455
456 struct drm_freelist {
457         int               initialized; /* Freelist in use                  */
458         atomic_t          count;       /* Number of free buffers           */
459         drm_buf_t         *next;       /* End pointer                      */
460
461         wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
462         int               low_mark;    /* Low water mark                   */
463         int               high_mark;   /* High water mark                  */
464 };
465
466 typedef struct drm_dma_handle {
467         void *vaddr;
468         size_t size;
469         bus_addr_t busaddr;
470         bus_dma_tag_t tag;
471         bus_dmamap_t map;
472 } drm_dma_handle_t;
473
474 typedef struct drm_buf_entry {
475         int               buf_size;
476         int               buf_count;
477         drm_buf_t         *buflist;
478         int               seg_count;
479         drm_dma_handle_t  **seglist;
480         int               page_order;
481
482         struct drm_freelist freelist;
483 } drm_buf_entry_t;
484
485 /* Event queued up for userspace to read */
486 struct drm_pending_event {
487         struct drm_event *event;
488         struct list_head link;
489         struct drm_file *file_priv;
490         pid_t pid; /* pid of requester, no guarantee it's valid by the time
491                       we deliver the event, for tracing only */
492         void (*destroy)(struct drm_pending_event *event);
493 };
494
495 /* initial implementaton using a linked list - todo hashtab */
496 struct drm_prime_file_private {
497         struct list_head head;
498 #ifdef DUMBBELL_WIP
499         struct mutex lock;
500 #endif /* DUMBBELL_WIP */
501 };
502
503 /** File private data */
504 struct drm_file {
505         int authenticated;
506         struct drm_device *dev;
507         int               master;
508         pid_t             pid;
509         uid_t             uid;
510         drm_magic_t       magic;
511         unsigned long     ioctl_count;
512         struct list_head lhead;
513         struct kqinfo     dkq;
514
515         /** Mapping of mm object handles to object pointers. */
516         struct idr object_idr;
517         /** Lock for synchronization of access to object_idr. */
518         struct lock table_lock;
519
520         void             *driver_priv;
521
522         int               is_master;
523         struct drm_master *masterp;
524
525         /* true when the client has asked us to expose stereo 3D mode flags */
526         bool stereo_allowed;
527
528         /**
529          * fbs - List of framebuffers associated with this file.
530          *
531          * Protected by fbs_lock. Note that the fbs list holds a reference on
532          * the fb object to prevent it from untimely disappearing.
533          */
534         struct list_head fbs;
535         struct lock fbs_lock;
536
537         wait_queue_head_t event_wait;
538         struct list_head  event_list;
539         int               event_space;
540
541         struct drm_prime_file_private prime;
542 };
543
544 /**
545  * Lock data.
546  */
547 struct drm_lock_data {
548         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
549         /** Private of lock holder's file (NULL=kernel) */
550         struct drm_file *file_priv;
551         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
552         unsigned long lock_time;        /**< Time of last lock in jiffies */
553 };
554
555 /* This structure, in the struct drm_device, is always initialized while the
556  * device
557  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
558  * when set marks that no further bufs may be allocated until device teardown
559  * occurs (when the last open of the device has closed).  The high/low
560  * watermarks of bufs are only touched by the X Server, and thus not
561  * concurrently accessed, so no locking is needed.
562  */
563 typedef struct drm_device_dma {
564
565         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
566         int buf_count;                  /**< total number of buffers */
567         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
568         int seg_count;
569         int page_count;                 /**< number of pages */
570         unsigned long *pagelist;        /**< page list */
571         unsigned long byte_count;
572         enum {
573                 _DRM_DMA_USE_AGP = 0x01,
574                 _DRM_DMA_USE_SG = 0x02,
575                 _DRM_DMA_USE_FB = 0x04,
576                 _DRM_DMA_USE_PCI_RO = 0x08
577         } flags;
578
579 } drm_device_dma_t;
580
581 typedef struct drm_agp_mem {
582         void               *handle;
583         unsigned long      bound; /* address */
584         int                pages;
585         struct drm_agp_mem *prev;
586         struct drm_agp_mem *next;
587 } drm_agp_mem_t;
588
589 typedef struct drm_agp_head {
590         device_t           agpdev;
591         struct agp_info    agp_info;
592         const char         *chipset;
593         drm_agp_mem_t      *memory;
594         unsigned long      mode;
595         int                enabled;
596         int                acquired;
597         unsigned long      base;
598         int                agp_mtrr;
599         int                cant_use_aperture;
600         unsigned long      page_mask;
601 } drm_agp_head_t;
602
603 typedef struct drm_sg_mem {
604         vm_offset_t vaddr;
605         vm_paddr_t *busaddr;
606         vm_pindex_t pages;
607 } drm_sg_mem_t;
608
609 /**
610  * Kernel side of a mapping
611  */
612 struct drm_local_map {
613         resource_size_t offset;  /**< Requested physical address (0 for SAREA)*/
614         unsigned long size;      /**< Requested physical size (bytes) */
615         enum drm_map_type type;  /**< Type of memory to map */
616         enum drm_map_flags flags;        /**< Flags */
617         void *handle;            /**< User-space: "Handle" to pass to mmap() */
618                                  /**< Kernel-space: kernel-virtual address */
619         int mtrr;                /**< MTRR slot used */
620 };
621
622 typedef struct drm_local_map drm_local_map_t;
623
624 /**
625  * Mappings list
626  */
627 struct drm_map_list {
628         struct list_head head;          /**< list head */
629         struct drm_hash_item hash;
630         struct drm_local_map *map;      /**< mapping */
631         uint64_t user_token;
632         struct drm_master *master;
633         struct drm_mm_node *file_offset_node;   /**< fake offset */
634 };
635
636 /**
637  * Context handle list
638  */
639 struct drm_ctx_list {
640         struct list_head head;          /**< list head */
641         drm_context_t handle;           /**< context handle */
642         struct drm_file *tag;           /**< associated fd private data */
643 };
644
645 struct drm_vblank_info {
646         wait_queue_head_t queue;        /* vblank wait queue */
647         atomic_t count;                 /* number of VBLANK interrupts */
648                                         /* (driver must alloc the right number of counters) */
649         atomic_t refcount;              /* number of users of vblank interrupts */
650         u32 last;                       /* protected by dev->vbl_lock, used */
651                                         /* for wraparound handling */
652         int enabled;                    /* so we don't call enable more than */
653                                         /* once per disable */
654         int inmodeset;                  /* Display driver is setting mode */
655 };
656
657 /* location of GART table */
658 #define DRM_ATI_GART_MAIN 1
659 #define DRM_ATI_GART_FB   2
660
661 #define DRM_ATI_GART_PCI 1
662 #define DRM_ATI_GART_PCIE 2
663 #define DRM_ATI_GART_IGP 3
664
665 struct drm_ati_pcigart_info {
666         int gart_table_location;
667         int gart_reg_if;
668         void *addr;
669         dma_addr_t bus_addr;
670         dma_addr_t table_mask;
671         dma_addr_t member_mask;
672         struct drm_dma_handle *table_handle;
673         drm_local_map_t mapping;
674         int table_size;
675         struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
676 };
677
678 /**
679  * GEM specific mm private for tracking GEM objects
680  */
681 struct drm_gem_mm {
682         struct drm_vma_offset_manager vma_manager;
683         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
684         struct drm_open_hash offset_hash; /**< User token hash table for maps */
685         struct unrhdr *idxunr;
686 };
687
688 struct drm_gem_object {
689         /** Reference count of this object */
690         struct kref refcount;
691
692         /** Handle count of this object. Each handle also holds a reference */
693         atomic_t handle_count; /* number of handles on this object */
694
695         /** Related drm device */
696         struct drm_device *dev;
697
698         /** File representing the shmem storage: filp in Linux parlance */
699         vm_object_t vm_obj;
700
701         /* Mapping info for this object */
702         struct drm_vma_offset_node vma_node;
703         bool on_map;
704         struct drm_hash_item map_list;
705
706         /**
707          * Size of the object, in bytes.  Immutable over the object's
708          * lifetime.
709          */
710         size_t size;
711
712         /**
713          * Global name for this object, starts at 1. 0 means unnamed.
714          * Access is covered by the object_name_lock in the related drm_device
715          */
716         int name;
717
718         /**
719          * Memory domains. These monitor which caches contain read/write data
720          * related to the object. When transitioning from one set of domains
721          * to another, the driver is called to ensure that caches are suitably
722          * flushed and invalidated
723          */
724         uint32_t read_domains;
725         uint32_t write_domain;
726
727         /**
728          * While validating an exec operation, the
729          * new read/write domain values are computed here.
730          * They will be transferred to the above values
731          * at the point that any cache flushing occurs
732          */
733         uint32_t pending_read_domains;
734         uint32_t pending_write_domain;
735
736 #ifdef DUMBBELL_WIP
737         /* dma buf exported from this GEM object */
738         struct dma_buf *export_dma_buf;
739
740         /* dma buf attachment backing this object */
741         struct dma_buf_attachment *import_attach;
742 #endif /* DUMBBELL_WIP */
743 };
744
745 #include "drm_crtc.h"
746
747 /* per-master structure */
748 struct drm_master {
749
750         struct kref refcount; /* refcount for this master */
751
752         struct list_head head; /**< each minor contains a list of masters */
753         struct drm_minor *minor; /**< link back to minor we are a master for */
754
755         char *unique;                   /**< Unique identifier: e.g., busid */
756         int unique_len;                 /**< Length of unique field */
757         int unique_size;                /**< amount allocated */
758
759         int blocked;                    /**< Blocked due to VC switch? */
760
761         /** \name Authentication */
762         /*@{ */
763         struct drm_open_hash magiclist;
764         struct list_head magicfree;
765         /*@} */
766
767         struct drm_lock_data lock;      /**< Information on hardware lock */
768
769         void *driver_priv; /**< Private structure for driver to use */
770 };
771
772 /* Size of ringbuffer for vblank timestamps. Just double-buffer
773  * in initial implementation.
774  */
775 #define DRM_VBLANKTIME_RBSIZE 2
776
777 /* Flags and return codes for get_vblank_timestamp() driver function. */
778 #define DRM_CALLED_FROM_VBLIRQ 1
779 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
780 #define DRM_VBLANKTIME_INVBL             (1 << 1)
781
782 /* get_scanout_position() return flags */
783 #define DRM_SCANOUTPOS_VALID        (1 << 0)
784 #define DRM_SCANOUTPOS_INVBL        (1 << 1)
785 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
786
787 #ifndef DMA_BIT_MASK
788 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
789 #endif
790
791 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
792
793 struct drm_driver {
794         int     (*load)(struct drm_device *, unsigned long flags);
795         int     (*use_msi)(struct drm_device *, unsigned long flags);
796         int     (*firstopen)(struct drm_device *);
797         int     (*open)(struct drm_device *, struct drm_file *);
798         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
799         void    (*postclose)(struct drm_device *, struct drm_file *);
800         void    (*lastclose)(struct drm_device *);
801         int     (*unload)(struct drm_device *);
802         void    (*reclaim_buffers_locked)(struct drm_device *,
803                                           struct drm_file *file_priv);
804         int     (*dma_ioctl)(struct drm_device *dev, void *data,
805                              struct drm_file *file_priv);
806         void    (*dma_ready)(struct drm_device *);
807         int     (*dma_quiescent)(struct drm_device *);
808         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
809                                              enum drm_lock_flags flags);
810         int     (*dma_flush_unblock)(struct drm_device *, int context,
811                                      enum drm_lock_flags flags);
812         int     (*context_ctor)(struct drm_device *dev, int context);
813         int     (*context_dtor)(struct drm_device *dev, int context);
814         int     (*kernel_context_switch)(struct drm_device *dev, int old,
815                                          int new);
816         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
817         void    (*irq_preinstall)(struct drm_device *dev);
818         int     (*irq_postinstall)(struct drm_device *dev);
819         void    (*irq_uninstall)(struct drm_device *dev);
820         void    (*irq_handler)(DRM_IRQ_ARGS);
821
822         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
823         int     (*enable_vblank)(struct drm_device *dev, int crtc);
824         void    (*disable_vblank)(struct drm_device *dev, int crtc);
825
826         /**
827          * Called by vblank timestamping code.
828          *
829          * Return the current display scanout position from a crtc, and an
830          * optional accurate ktime_get timestamp of when position was measured.
831          *
832          * \param dev  DRM device.
833          * \param crtc Id of the crtc to query.
834          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
835          * \param *vpos Target location for current vertical scanout position.
836          * \param *hpos Target location for current horizontal scanout position.
837          * \param *stime Target location for timestamp taken immediately before
838          *               scanout position query. Can be NULL to skip timestamp.
839          * \param *etime Target location for timestamp taken immediately after
840          *               scanout position query. Can be NULL to skip timestamp.
841          *
842          * Returns vpos as a positive number while in active scanout area.
843          * Returns vpos as a negative number inside vblank, counting the number
844          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
845          * until start of active scanout / end of vblank."
846          *
847          * \return Flags, or'ed together as follows:
848          *
849          * DRM_SCANOUTPOS_VALID = Query successful.
850          * DRM_SCANOUTPOS_INVBL = Inside vblank.
851          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
852          * this flag means that returned position may be offset by a constant
853          * but unknown small number of scanlines wrt. real scanout position.
854          *
855          */
856         int (*get_scanout_position) (struct drm_device *dev, int crtc,
857                                      unsigned int flags,
858                                      int *vpos, int *hpos, ktime_t *stime,
859                                      ktime_t *etime);
860
861         int     (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
862                     int *max_error, struct timeval *vblank_time,
863                     unsigned flags);
864
865         void    (*gem_free_object)(struct drm_gem_object *obj);
866         int     (*gem_open_object)(struct drm_gem_object *, struct drm_file *);
867         void    (*gem_close_object)(struct drm_gem_object *, struct drm_file *);
868
869         struct cdev_pager_ops *gem_pager_ops;
870
871         int     (*dumb_create)(struct drm_file *file_priv,
872                     struct drm_device *dev, struct drm_mode_create_dumb *args);
873         int     (*dumb_map_offset)(struct drm_file *file_priv,
874                     struct drm_device *dev, uint32_t handle, uint64_t *offset);
875         int     (*dumb_destroy)(struct drm_file *file_priv,
876                     struct drm_device *dev, uint32_t handle);
877
878         int     (*sysctl_init)(struct drm_device *dev,
879                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
880         void    (*sysctl_cleanup)(struct drm_device *dev);
881
882         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
883
884         /**
885          * Called by \c drm_device_is_agp.  Typically used to determine if a
886          * card is really attached to AGP or not.
887          *
888          * \param dev  DRM device handle
889          *
890          * \returns
891          * One of three values is returned depending on whether or not the
892          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
893          * (return of 1), or may or may not be AGP (return of 2).
894          */
895         int     (*device_is_agp) (struct drm_device * dev);
896
897         int     buf_priv_size;
898
899         int     major;
900         int     minor;
901         int     patchlevel;
902         const char *name;               /* Simple driver name              */
903         const char *desc;               /* Longer driver name              */
904         const char *date;               /* Date of last major changes.     */
905
906         u32 driver_features;
907         int dev_priv_size;
908         struct drm_ioctl_desc *ioctls;
909         int num_ioctls;
910 };
911
912 /**
913  * DRM minor structure. This structure represents a drm minor number.
914  */
915 struct drm_minor {
916         int index;                      /**< Minor device number */
917         int type;                       /**< Control or render */
918         device_t kdev;                  /**< OS device */
919         struct drm_device *dev;
920
921         struct drm_master *master; /* currently active master for this node */
922         struct list_head master_list;
923         struct drm_mode_group mode_group;
924 };
925
926 /* mode specified on the command line */
927 struct drm_cmdline_mode {
928         bool specified;
929         bool refresh_specified;
930         bool bpp_specified;
931         int xres, yres;
932         int bpp;
933         int refresh;
934         bool rb;
935         bool interlace;
936         bool cvt;
937         bool margins;
938         enum drm_connector_force force;
939 };
940
941
942 struct drm_pending_vblank_event {
943         struct drm_pending_event base;
944         int pipe;
945         struct drm_event_vblank event;
946 };
947
948 struct drm_sysctl_info {
949         struct sysctl_ctx_list ctx;
950         char   name[2];
951 };
952
953 /* Length for the array of resource pointers for drm_get_resource_*. */
954 #define DRM_MAX_PCI_RESOURCE    6
955
956 struct drm_vblank_crtc {
957         wait_queue_head_t queue;        /**< VBLANK wait queue */
958         struct timeval time[DRM_VBLANKTIME_RBSIZE];     /**< timestamp of current count */
959         atomic_t count;                 /**< number of VBLANK interrupts */
960         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
961         u32 last;                       /* protected by dev->vbl_lock, used */
962                                         /* for wraparound handling */
963         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
964         unsigned int inmodeset;         /* Display driver is setting mode */
965         bool enabled;                   /* so we don't call enable more than
966                                            once per disable */
967 };
968
969 /**
970  * DRM device structure. This structure represent a complete card that
971  * may contain multiple heads.
972  */
973 struct drm_device {
974         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
975
976         uint16_t pci_subdevice;         /* PCI subsystem device id */
977         uint16_t pci_subvendor;         /* PCI subsystem vendor id */
978
979         char              *unique;      /* Unique identifier: e.g., busid  */
980         int               unique_len;   /* Length of unique field          */
981         struct cdev       *devnode;     /* Device number for mknod         */
982         int               if_version;   /* Highest interface version set */
983
984         int               flags;        /* Flags to open(2)                */
985
986                                 /* Locks */
987         struct spinlock   dma_lock;     /* protects dev->dma */
988         struct lwkt_serialize irq_lock; /* protects irq condition checks */
989         struct lock       dev_lock;     /* protects everything else */
990
991         /** \name Locks */
992         /*@{ */
993         struct lock struct_mutex;       /**< For others */
994         /*@} */
995
996         /** \name Usage Counters */
997         /*@{ */
998         int               open_count;   /* Outstanding files open          */
999         int               buf_use;      /* Buffers in use -- cannot alloc  */
1000         /*@} */
1001
1002
1003         /** \name Performance counters */
1004         /*@{ */
1005         unsigned long     counters;
1006         enum drm_stat_type      types[15];
1007         atomic_t          counts[15];
1008         /*@} */
1009
1010                                 /* Authentication */
1011         struct drm_open_hash magiclist; /**< magic hash table */
1012         struct list_head magicfree;
1013
1014         struct list_head filelist;
1015
1016         /** \name Memory management */
1017         /*@{ */
1018         struct list_head maplist;       /**< Linked list of regions */
1019         int map_count;                  /**< Number of mappable regions */
1020         struct drm_open_hash map_hash;  /**< User token hash table for maps */
1021
1022         /** \name Context handle management */
1023         /*@{ */
1024         struct list_head ctxlist;       /**< Linked list of context handles */
1025         int ctx_count;                  /**< Number of context handles */
1026         struct lock ctxlist_mutex;      /**< For ctxlist */
1027
1028         struct idr ctx_idr;
1029
1030         /*@} */
1031
1032         struct drm_lock_data lock;      /* Information on hardware lock    */
1033
1034                                 /* DMA queues (contexts) */
1035         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
1036
1037         int               irq;          /* Interrupt used by board         */
1038         int               msi_enabled;  /* MSI enabled */
1039         int               irqrid;       /* Interrupt used by board */
1040         struct resource   *irqr;        /* Resource for interrupt used by board    */
1041         void              *irqh;        /* Handle from bus_setup_intr      */
1042
1043         /* Storage of resource pointers for drm_get_resource_* */
1044         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
1045         int               pcirid[DRM_MAX_PCI_RESOURCE];
1046
1047         int               pci_domain;
1048         int               pci_bus;
1049         int               pci_slot;
1050         int               pci_func;
1051
1052         /** \name Context support */
1053         /*@{ */
1054         int irq_enabled;                /**< True if irq handler is enabled */
1055         __volatile__ long context_flag; /**< Context swapping flag */
1056         __volatile__ long interrupt_flag; /**< Interruption handler flag */
1057         __volatile__ long dma_flag;     /**< DMA dispatch flag */
1058         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
1059         int last_checked;               /**< Last context checked for DMA */
1060         int last_context;               /**< Last current context */
1061         unsigned long last_switch;      /**< jiffies at last context switch */
1062         /*@} */
1063
1064         /** \name VBLANK IRQ support */
1065         /*@{ */
1066
1067         /*
1068          * At load time, disabling the vblank interrupt won't be allowed since
1069          * old clients may not call the modeset ioctl and therefore misbehave.
1070          * Once the modeset ioctl *has* been called though, we can safely
1071          * disable them when unused.
1072          */
1073         bool vblank_disable_allowed;
1074
1075         /* array of size num_crtcs */
1076         struct drm_vblank_crtc *vblank;
1077
1078         struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
1079         struct lock vbl_lock;
1080         struct timer_list vblank_disable_timer;
1081
1082         u32 max_vblank_count;           /**< size of vblank counter register */
1083
1084         /**
1085          * List of events
1086          */
1087         struct list_head vblank_event_list;
1088         struct lock event_lock;
1089
1090         /*@} */
1091
1092         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
1093
1094                                 /* Sysctl support */
1095         struct drm_sysctl_info *sysctl;
1096         int               sysctl_node_idx;
1097
1098
1099         drm_sg_mem_t      *sg;  /* Scatter gather memory */
1100         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1101
1102         unsigned long     *ctx_bitmap;
1103         void              *dev_private;
1104
1105         void              *drm_ttm_bdev;
1106
1107         /*@} */
1108
1109         struct drm_agp_head *agp;       /**< AGP data */
1110
1111         struct device *dev;             /**< Device structure */
1112         struct pci_dev *pdev;           /**< PCI device structure */
1113         int pci_vendor;                 /**< PCI vendor id */
1114         int pci_device;                 /**< PCI device id */
1115
1116         struct drm_driver *driver;
1117         struct drm_local_map *agp_buffer_map;
1118         unsigned int agp_buffer_token;
1119         struct drm_minor *control;              /**< Control node for card */
1120         struct drm_minor *primary;              /**< render type primary screen head */
1121
1122         struct drm_mode_config mode_config;     /**< Current mode config */
1123
1124         /** \name GEM information */
1125         /*@{ */
1126         struct lock object_name_lock;
1127         struct idr object_name_idr;
1128         /*@} */
1129         void             *mm_private;
1130
1131         void *sysctl_private;
1132         char busid_str[128];
1133         int modesetting;
1134
1135         int switch_power_state;
1136
1137         atomic_t unplugged; /* device has been unplugged or gone away */
1138 };
1139
1140 #define DRM_SWITCH_POWER_ON 0
1141 #define DRM_SWITCH_POWER_OFF 1
1142 #define DRM_SWITCH_POWER_CHANGING 2
1143
1144 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1145                                              int feature)
1146 {
1147         return ((dev->driver->driver_features & feature) ? 1 : 0);
1148 }
1149
1150 static inline int drm_device_is_unplugged(struct drm_device *dev)
1151 {
1152         int ret = atomic_read(&dev->unplugged);
1153         smp_rmb();
1154         return ret;
1155 }
1156
1157 #if __OS_HAS_AGP
1158 static inline int drm_core_has_AGP(struct drm_device *dev)
1159 {
1160         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1161 }
1162 #else
1163 #define drm_core_has_AGP(dev) (0)
1164 #endif
1165
1166 enum dmi_field {
1167         DMI_NONE,
1168         DMI_BIOS_VENDOR,
1169         DMI_BIOS_VERSION,
1170         DMI_BIOS_DATE,
1171         DMI_SYS_VENDOR,
1172         DMI_PRODUCT_NAME,
1173         DMI_PRODUCT_VERSION,
1174         DMI_PRODUCT_SERIAL,
1175         DMI_PRODUCT_UUID,
1176         DMI_BOARD_VENDOR,
1177         DMI_BOARD_NAME,
1178         DMI_BOARD_VERSION,
1179         DMI_BOARD_SERIAL,
1180         DMI_BOARD_ASSET_TAG,
1181         DMI_CHASSIS_VENDOR,
1182         DMI_CHASSIS_TYPE,
1183         DMI_CHASSIS_VERSION,
1184         DMI_CHASSIS_SERIAL,
1185         DMI_CHASSIS_ASSET_TAG,
1186         DMI_STRING_MAX,
1187 };
1188
1189 struct dmi_strmatch {
1190         unsigned char slot;
1191         char substr[79];
1192 };
1193
1194 struct dmi_system_id {
1195         int (*callback)(const struct dmi_system_id *);
1196         const char *ident;
1197         struct dmi_strmatch matches[4];
1198 };
1199 #define DMI_MATCH(a, b) {(a), (b)}
1200 bool dmi_check_system(const struct dmi_system_id *);
1201
1202 extern int      drm_notyet_flag;
1203 extern int      drm_debug;
1204
1205 extern unsigned int drm_vblank_offdelay;
1206 extern unsigned int drm_timestamp_precision;
1207 extern unsigned int drm_timestamp_monotonic;
1208
1209 /* Device setup support (drm_drv.c) */
1210 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1211 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1212 int     drm_create_cdevs(device_t kdev);
1213
1214 d_ioctl_t drm_ioctl;
1215 extern int drm_lastclose(struct drm_device *dev);
1216
1217                                 /* Device support (drm_fops.h) */
1218 extern struct lock drm_global_mutex;
1219 d_open_t drm_open;
1220 d_close_t drm_close;
1221 d_read_t drm_read;
1222 d_kqfilter_t drm_kqfilter;
1223 int drm_release(device_t kdev);
1224
1225 d_mmap_t drm_mmap;
1226 d_mmap_single_t drm_mmap_single;
1227 extern drm_local_map_t  *drm_getsarea(struct drm_device *dev);
1228
1229 void drm_cdevpriv_dtor(void *cd);
1230
1231 void drm_event_wakeup(struct drm_pending_event *e);
1232
1233 int drm_add_busid_modesetting(struct drm_device *dev,
1234     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1235
1236 /* File operations helpers (drm_fops.c) */
1237 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1238                                          DRM_STRUCTPROC *p,
1239                                         struct drm_device *dev,
1240                                         struct file *fp);
1241 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
1242                                         DRM_STRUCTPROC *p);
1243
1244 #ifdef DUMBBELL_WIP
1245 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1246                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1247                 int *prime_fd);
1248 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1249                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1250
1251 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1252                                         struct drm_file *file_priv);
1253 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1254                                         struct drm_file *file_priv);
1255
1256 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1257                                             dma_addr_t *addrs, int max_pages);
1258 #endif /* DUMBBELL_WIP */
1259 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1260 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1261
1262 int drm_gem_dumb_destroy(struct drm_file *file,
1263                          struct drm_device *dev,
1264                          uint32_t handle);
1265
1266 #ifdef DUMBBELL_WIP
1267 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1268 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1269 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1270 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1271 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1272
1273 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1274 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1275                          struct drm_gem_object **obj);
1276 #endif /* DUMBBELL_WIP */
1277
1278                                 /* Memory management support (drm_memory.h) */
1279 #include <drm/drm_memory.h>
1280 void    drm_mem_init(void);
1281 void    drm_mem_uninit(void);
1282 void    *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1283 void    *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1284 void    drm_ioremapfree(drm_local_map_t *map);
1285 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1286 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1287
1288 int     drm_ctxbitmap_init(struct drm_device *dev);
1289 void    drm_ctxbitmap_cleanup(struct drm_device *dev);
1290 void    drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1291
1292 /* Locking IOCTL support (drm_lock.c) */
1293 int     drm_lock_take(struct drm_lock_data *lock_data,
1294                       unsigned int context);
1295 int     drm_lock_transfer(struct drm_lock_data *lock_data,
1296                           unsigned int context);
1297 int     drm_lock_free(struct drm_lock_data *lock_data,
1298                       unsigned int context);
1299
1300 /*
1301  * These are exported to drivers so that they can implement fencing using
1302  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1303  */
1304
1305 unsigned long drm_get_resource_start(struct drm_device *dev,
1306                                      unsigned int resource);
1307 unsigned long drm_get_resource_len(struct drm_device *dev,
1308                                    unsigned int resource);
1309 void    drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1310 int     drm_order(unsigned long size);
1311 int     drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
1312 int     drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
1313 extern int drm_addmap(struct drm_device *dev, resource_size_t offset,
1314                       unsigned int size, enum drm_map_type type,
1315                       enum drm_map_flags flags, struct drm_local_map **map_ptr);
1316
1317 /* DMA support (drm_dma.c) */
1318 int     drm_dma_setup(struct drm_device *dev);
1319 void    drm_dma_takedown(struct drm_device *dev);
1320 void    drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
1321 void    drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
1322 #define drm_core_reclaim_buffers drm_reclaim_buffers
1323
1324                                 /* IRQ support (drm_irq.h) */
1325 int     drm_irq_install(struct drm_device *dev);
1326 int     drm_irq_uninstall(struct drm_device *dev);
1327 void    drm_driver_irq_preinstall(struct drm_device *dev);
1328 void    drm_driver_irq_postinstall(struct drm_device *dev);
1329 void    drm_driver_irq_uninstall(struct drm_device *dev);
1330
1331 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1332 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1333                            struct drm_file *filp);
1334 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1335 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1336                                      struct timeval *vblanktime);
1337 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1338                                      struct drm_pending_vblank_event *e);
1339 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1340 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1341 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1342 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1343 extern void drm_vblank_cleanup(struct drm_device *dev);
1344 extern u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1345                                      struct timeval *tvblank, unsigned flags);
1346 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1347                                                  int crtc, int *max_error,
1348                                                  struct timeval *vblank_time,
1349                                                  unsigned flags,
1350                                                  const struct drm_crtc *refcrtc,
1351                                                  const struct drm_display_mode *mode);
1352 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1353                                             const struct drm_display_mode *mode);
1354
1355 /* Modesetting support */
1356 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1357 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1358 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1359                            struct drm_file *file_priv);
1360
1361 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
1362 int     drm_device_is_agp(struct drm_device *dev);
1363 int     drm_device_is_pcie(struct drm_device *dev);
1364 drm_agp_head_t *drm_agp_init(void);
1365 int     drm_agp_acquire(struct drm_device *dev);
1366 int     drm_agp_release(struct drm_device *dev);
1367 int     drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
1368 int     drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1369 void    *drm_agp_allocate_memory(size_t pages, u32 type);
1370 int     drm_agp_free_memory(void *handle);
1371 int     drm_agp_bind_memory(void *handle, off_t start);
1372 int     drm_agp_unbind_memory(void *handle);
1373 int     drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1374 int     drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1375 int     drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1376 int     drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1377
1378 /* Scatter Gather Support (drm_scatter.c) */
1379 void    drm_sg_cleanup(drm_sg_mem_t *entry);
1380 int     drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1381
1382 /* sysctl support (drm_sysctl.h) */
1383 extern int              drm_sysctl_init(struct drm_device *dev);
1384 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1385
1386 /* ATI PCIGART support (ati_pcigart.c) */
1387 int     drm_ati_pcigart_init(struct drm_device *dev,
1388                                 struct drm_ati_pcigart_info *gart_info);
1389 int     drm_ati_pcigart_cleanup(struct drm_device *dev,
1390                                 struct drm_ati_pcigart_info *gart_info);
1391
1392 /* Cache management (drm_memory.c) */
1393 void    drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1394
1395 /* Locking IOCTL support (drm_drv.c) */
1396 int     drm_lock(struct drm_device *dev, void *data,
1397                  struct drm_file *file_priv);
1398 int     drm_unlock(struct drm_device *dev, void *data,
1399                    struct drm_file *file_priv);
1400 int     drm_version(struct drm_device *dev, void *data,
1401                     struct drm_file *file_priv);
1402 int     drm_setversion(struct drm_device *dev, void *data,
1403                        struct drm_file *file_priv);
1404
1405 /* Misc. IOCTL support (drm_ioctl.c) */
1406 int     drm_irq_by_busid(struct drm_device *dev, void *data,
1407                          struct drm_file *file_priv);
1408 int     drm_getunique(struct drm_device *dev, void *data,
1409                       struct drm_file *file_priv);
1410 int     drm_setunique(struct drm_device *dev, void *data,
1411                       struct drm_file *file_priv);
1412 int     drm_getmap(struct drm_device *dev, void *data,
1413                    struct drm_file *file_priv);
1414 int     drm_getclient(struct drm_device *dev, void *data,
1415                       struct drm_file *file_priv);
1416 int     drm_getstats(struct drm_device *dev, void *data,
1417                      struct drm_file *file_priv);
1418 int     drm_getcap(struct drm_device *dev, void *data,
1419                      struct drm_file *file_priv);
1420 extern int drm_setclientcap(struct drm_device *dev, void *data,
1421                             struct drm_file *file_priv);
1422 int     drm_noop(struct drm_device *dev, void *data,
1423                  struct drm_file *file_priv);
1424
1425 /* Context IOCTL support (drm_context.c) */
1426 int     drm_resctx(struct drm_device *dev, void *data,
1427                    struct drm_file *file_priv);
1428 int     drm_addctx(struct drm_device *dev, void *data,
1429                    struct drm_file *file_priv);
1430 int     drm_getctx(struct drm_device *dev, void *data,
1431                    struct drm_file *file_priv);
1432 int     drm_switchctx(struct drm_device *dev, void *data,
1433                       struct drm_file *file_priv);
1434 int     drm_newctx(struct drm_device *dev, void *data,
1435                    struct drm_file *file_priv);
1436 int     drm_rmctx(struct drm_device *dev, void *data,
1437                   struct drm_file *file_priv);
1438 int     drm_setsareactx(struct drm_device *dev, void *data,
1439                         struct drm_file *file_priv);
1440 int     drm_getsareactx(struct drm_device *dev, void *data,
1441                         struct drm_file *file_priv);
1442
1443 /* Authentication IOCTL support (drm_auth.c) */
1444 int     drm_getmagic(struct drm_device *dev, void *data,
1445                      struct drm_file *file_priv);
1446 int     drm_authmagic(struct drm_device *dev, void *data,
1447                       struct drm_file *file_priv);
1448
1449 /* Cache management (drm_cache.c) */
1450 void drm_clflush_virt_range(char *addr, unsigned long length);
1451
1452 /* Buffer management support (drm_bufs.c) */
1453 int     drm_addmap_ioctl(struct drm_device *dev, void *data,
1454                          struct drm_file *file_priv);
1455 int     drm_rmmap_ioctl(struct drm_device *dev, void *data,
1456                         struct drm_file *file_priv);
1457 int     drm_addbufs(struct drm_device *dev, void *data,
1458                     struct drm_file *file_priv);
1459 int     drm_infobufs(struct drm_device *dev, void *data,
1460                      struct drm_file *file_priv);
1461 int     drm_markbufs(struct drm_device *dev, void *data,
1462                      struct drm_file *file_priv);
1463 int     drm_freebufs(struct drm_device *dev, void *data,
1464                      struct drm_file *file_priv);
1465 int     drm_mapbufs(struct drm_device *dev, void *data,
1466                     struct drm_file *file_priv);
1467
1468 /* DMA support (drm_dma.c) */
1469 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1470
1471 /* IRQ support (drm_irq.c) */
1472 int     drm_control(struct drm_device *dev, void *data,
1473                     struct drm_file *file_priv);
1474
1475 /* AGP/GART support (drm_agpsupport.c) */
1476 int     drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1477                               struct drm_file *file_priv);
1478 int     drm_agp_release_ioctl(struct drm_device *dev, void *data,
1479                               struct drm_file *file_priv);
1480 int     drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1481                              struct drm_file *file_priv);
1482 int     drm_agp_info_ioctl(struct drm_device *dev, void *data,
1483                            struct drm_file *file_priv);
1484 int     drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1485                             struct drm_file *file_priv);
1486 int     drm_agp_free_ioctl(struct drm_device *dev, void *data,
1487                            struct drm_file *file_priv);
1488 int     drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1489                              struct drm_file *file_priv);
1490 int     drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1491                            struct drm_file *file_priv);
1492
1493                                 /* Stub support (drm_stub.h) */
1494 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1495                                struct drm_file *file_priv);
1496 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1497                                 struct drm_file *file_priv);
1498
1499 /* Scatter Gather Support (drm_scatter.c) */
1500 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1501                            struct drm_file *file_priv);
1502 int     drm_sg_free(struct drm_device *dev, void *data,
1503                     struct drm_file *file_priv);
1504
1505 /* consistent PCI memory functions (drm_pci.c) */
1506 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1507                                        size_t align);
1508 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1509
1510                                /* sysfs support (drm_sysfs.c) */
1511 struct drm_sysfs_class;
1512 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1513 extern void drm_sysfs_destroy(void);
1514 extern int drm_sysfs_device_add(struct drm_minor *minor);
1515 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1516 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1517 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1518 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1519
1520 /* Graphics Execution Manager library functions (drm_gem.c) */
1521 int drm_gem_init(struct drm_device *dev);
1522 void drm_gem_destroy(struct drm_device *dev);
1523 void drm_gem_object_release(struct drm_gem_object *obj);
1524 void drm_gem_object_free(struct kref *kref);
1525 int drm_gem_object_init(struct drm_device *dev,
1526                         struct drm_gem_object *obj, size_t size);
1527 void drm_gem_private_object_init(struct drm_device *dev,
1528                                  struct drm_gem_object *obj, size_t size);
1529
1530 int i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
1531     vm_ooffset_t foff, struct ucred *cred, u_short *color);
1532 void i915_gem_pager_dtor(void * handle);
1533
1534 #include <drm/drm_global.h>
1535
1536 static inline void
1537 drm_gem_object_reference(struct drm_gem_object *obj)
1538 {
1539         kref_get(&obj->refcount);
1540 }
1541
1542 static inline void
1543 drm_gem_object_unreference(struct drm_gem_object *obj)
1544 {
1545         if (obj != NULL)
1546                 kref_put(&obj->refcount, drm_gem_object_free);
1547 }
1548
1549 static inline void
1550 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1551 {
1552         if (obj != NULL) {
1553                 struct drm_device *dev = obj->dev;
1554                 DRM_LOCK(dev);
1555                 kref_put(&obj->refcount, drm_gem_object_free);
1556                 DRM_UNLOCK(dev);
1557         }
1558 }
1559
1560 int drm_gem_handle_create(struct drm_file *file_priv,
1561                           struct drm_gem_object *obj,
1562                           u32 *handlep);
1563 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
1564
1565 static inline void
1566 drm_gem_object_handle_reference(struct drm_gem_object *obj)
1567 {
1568         drm_gem_object_reference(obj);
1569         atomic_inc(&obj->handle_count);
1570 }
1571
1572 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1573                                              struct drm_file *filp,
1574                                              u32 handle);
1575 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1576                         struct drm_file *file_priv);
1577 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1578                         struct drm_file *file_priv);
1579 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1580                        struct drm_file *file_priv);
1581 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1582 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1583
1584 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1585 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1586 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
1587
1588 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1589     vm_size_t size, struct vm_object **obj_res, int nprot);
1590 void drm_gem_pager_dtr(void *obj);
1591
1592 struct ttm_bo_device;
1593 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1594     vm_size_t size, struct vm_object **obj_res, int nprot);
1595 struct ttm_buffer_object;
1596 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1597
1598 void drm_device_lock_mtx(struct drm_device *dev);
1599 void drm_device_unlock_mtx(struct drm_device *dev);
1600 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1601     const char *msg, int timeout);
1602 void drm_device_assert_mtx_locked(struct drm_device *dev);
1603 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1604
1605 void drm_device_lock_struct(struct drm_device *dev);
1606 void drm_device_unlock_struct(struct drm_device *dev);
1607 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1608     const char *msg, int timeout);
1609 void drm_device_assert_struct_locked(struct drm_device *dev);
1610 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1611
1612 void drm_compat_locking_init(struct drm_device *dev);
1613 void drm_sleep_locking_init(struct drm_device *dev);
1614
1615 /* drm_modes.c */
1616 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1617     struct drm_connector *connector, struct drm_cmdline_mode *mode);
1618 struct drm_display_mode *drm_mode_create_from_cmdline_mode(
1619     struct drm_device *dev, struct drm_cmdline_mode *cmd);
1620
1621 /* Inline replacements for drm_alloc and friends */
1622 static __inline__ void *
1623 drm_alloc(size_t size, struct malloc_type *area)
1624 {
1625         return kmalloc(size, area, M_WAITOK | M_NULLOK);
1626 }
1627
1628 static __inline__ void *
1629 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1630 {
1631         return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1632 }
1633
1634 static __inline__ void
1635 drm_free(void *pt, struct malloc_type *area)
1636 {
1637         kfree(pt);
1638 }
1639
1640 static __inline__ void
1641 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1642 {
1643         map->handle = drm_ioremap_wc(dev, map);
1644 }
1645 static __inline__ void
1646 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1647 {
1648         map->handle = drm_ioremap(dev, map);
1649 }
1650 static __inline__ void
1651 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1652 {
1653         if ( map->handle && map->size )
1654                 drm_ioremapfree(map);
1655 }
1656
1657 static __inline__ struct drm_local_map *
1658 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1659 {
1660         struct drm_map_list *_entry;
1661
1662         list_for_each_entry(_entry, &dev->maplist, head) {
1663                 if (offset == (unsigned long)_entry->map->handle)
1664                         return _entry->map;
1665         }
1666         return NULL;
1667 }
1668
1669 static __inline__ void drm_core_dropmap(struct drm_map *map)
1670 {
1671 }
1672
1673 #include <drm/drm_mem_util.h>
1674
1675 /* FreeBSD compatibility macros */
1676 #define VM_OBJECT_WLOCK(object)         VM_OBJECT_LOCK(object)
1677 #define VM_OBJECT_WUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1678
1679 #define DRM_PCIE_SPEED_25 1
1680 #define DRM_PCIE_SPEED_50 2
1681 #define DRM_PCIE_SPEED_80 4
1682
1683 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1684
1685 #define drm_can_sleep() (DRM_HZ & 1)
1686
1687 #endif /* __KERNEL__ */
1688 #endif /* _DRM_P_H_ */