]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/drm2/drmP.h
MFC r254848, r258930:
[FreeBSD/stable/9.git] / sys / dev / drm2 / drmP.h
1 /* drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
2  * Created: Mon Jan  4 10:05:05 1999 by faith@precisioninsight.com
3  */
4 /*-
5  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  *
28  * Authors:
29  *    Rickard E. (Rik) Faith <faith@valinux.com>
30  *    Gareth Hughes <gareth@valinux.com>
31  *
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #ifndef _DRM_P_H_
38 #define _DRM_P_H_
39
40 #if defined(_KERNEL) || defined(__KERNEL__)
41
42 struct drm_device;
43 struct drm_file;
44
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/ktr.h>
50 #include <sys/module.h>
51 #include <sys/systm.h>
52 #include <sys/conf.h>
53 #include <sys/sglist.h>
54 #include <sys/stat.h>
55 #include <sys/priv.h>
56 #include <sys/proc.h>
57 #include <sys/lock.h>
58 #include <sys/fcntl.h>
59 #include <sys/uio.h>
60 #include <sys/filio.h>
61 #include <sys/selinfo.h>
62 #include <sys/sysctl.h>
63 #include <sys/bus.h>
64 #include <sys/queue.h>
65 #include <sys/signalvar.h>
66 #include <sys/poll.h>
67 #include <sys/sbuf.h>
68 #include <sys/taskqueue.h>
69 #include <sys/tree.h>
70 #include <vm/vm.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vm_param.h>
79 #include <vm/vm_phys.h>
80 #include <machine/param.h>
81 #include <machine/pmap.h>
82 #include <machine/bus.h>
83 #include <machine/resource.h>
84 #if defined(__i386__) || defined(__amd64__)
85 #include <machine/specialreg.h>
86 #endif
87 #include <machine/sysarch.h>
88 #include <sys/endian.h>
89 #include <sys/mman.h>
90 #include <sys/rman.h>
91 #include <sys/memrange.h>
92 #include <dev/agp/agpvar.h>
93 #include <sys/agpio.h>
94 #include <sys/mutex.h>
95 #include <dev/pci/pcivar.h>
96 #include <dev/pci/pcireg.h>
97 #include <sys/selinfo.h>
98 #include <sys/bus.h>
99
100 #include <dev/drm2/drm.h>
101 #include <dev/drm2/drm_atomic.h>
102 #include <dev/drm2/drm_internal.h>
103 #include <dev/drm2/drm_linux_list.h>
104 #include <dev/drm2/drm_gem_names.h>
105 #include <dev/drm2/drm_mm.h>
106 #include <dev/drm2/drm_hashtab.h>
107
108 #include "opt_compat.h"
109 #include "opt_drm.h"
110 #ifdef DRM_DEBUG
111 #undef DRM_DEBUG
112 #define DRM_DEBUG_DEFAULT_ON 1
113 #endif /* DRM_DEBUG */
114
115 #define DRM_DEBUGBITS_DEBUG             0x1
116 #define DRM_DEBUGBITS_KMS               0x2
117 #define DRM_DEBUGBITS_FAILED_IOCTL      0x4
118
119 #undef DRM_LINUX
120 #define DRM_LINUX 0
121
122 /* driver capabilities and requirements mask */
123 #define DRIVER_USE_AGP     0x1
124 #define DRIVER_REQUIRE_AGP 0x2
125 #define DRIVER_USE_MTRR    0x4
126 #define DRIVER_PCI_DMA     0x8
127 #define DRIVER_SG          0x10
128 #define DRIVER_HAVE_DMA    0x20
129 #define DRIVER_HAVE_IRQ    0x40
130 #define DRIVER_IRQ_SHARED  0x80
131 #define DRIVER_IRQ_VBL     0x100
132 #define DRIVER_DMA_QUEUE   0x200
133 #define DRIVER_FB_DMA      0x400
134 #define DRIVER_IRQ_VBL2    0x800
135 #define DRIVER_GEM         0x1000
136 #define DRIVER_MODESET     0x2000
137 #define DRIVER_USE_PLATFORM_DEVICE  0x4000
138 #define DRIVER_LOCKLESS_IRQ 0x8000
139
140
141 #define DRM_HASH_SIZE         16 /* Size of key hash table                */
142 #define DRM_KERNEL_CONTEXT    0  /* Change drm_resctx if changed          */
143 #define DRM_RESERVED_CONTEXTS 1  /* Change drm_resctx if changed          */
144
145 #define DRM_GEM_MAPPING_MASK    (3ULL << 62)
146 #define DRM_GEM_MAPPING_KEY     (2ULL << 62) /* Non-canonical address form */
147 #define DRM_GEM_MAX_IDX         0x3fffff
148 #define DRM_GEM_MAPPING_IDX(o)  (((o) >> 40) & DRM_GEM_MAX_IDX)
149 #define DRM_GEM_MAPPING_OFF(i)  (((uint64_t)(i)) << 40)
150 #define DRM_GEM_MAPPING_MAPOFF(o) \
151     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
152
153 MALLOC_DECLARE(DRM_MEM_DMA);
154 MALLOC_DECLARE(DRM_MEM_SAREA);
155 MALLOC_DECLARE(DRM_MEM_DRIVER);
156 MALLOC_DECLARE(DRM_MEM_MAGIC);
157 MALLOC_DECLARE(DRM_MEM_IOCTLS);
158 MALLOC_DECLARE(DRM_MEM_MAPS);
159 MALLOC_DECLARE(DRM_MEM_BUFS);
160 MALLOC_DECLARE(DRM_MEM_SEGS);
161 MALLOC_DECLARE(DRM_MEM_PAGES);
162 MALLOC_DECLARE(DRM_MEM_FILES);
163 MALLOC_DECLARE(DRM_MEM_QUEUES);
164 MALLOC_DECLARE(DRM_MEM_CMDS);
165 MALLOC_DECLARE(DRM_MEM_MAPPINGS);
166 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
167 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
168 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
169 MALLOC_DECLARE(DRM_MEM_SGLISTS);
170 MALLOC_DECLARE(DRM_MEM_DRAWABLE);
171 MALLOC_DECLARE(DRM_MEM_MM);
172 MALLOC_DECLARE(DRM_MEM_HASHTAB);
173 MALLOC_DECLARE(DRM_MEM_KMS);
174
175 SYSCTL_DECL(_hw_drm);
176
177 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
178
179                                 /* Internal types and structures */
180 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
181 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
182 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
183
184 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
185
186 #define __OS_HAS_AGP    1
187
188 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
189 #define DRM_DEV_UID     0
190 #define DRM_DEV_GID     0
191
192 #define wait_queue_head_t       atomic_t
193 #define DRM_WAKEUP(w)           wakeup((void *)w)
194 #define DRM_WAKEUP_INT(w)       wakeup(w)
195 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
196
197 #define DRM_CURPROC             curthread
198 #define DRM_STRUCTPROC          struct thread
199 #define DRM_SPINTYPE            struct mtx
200 #define DRM_SPININIT(l,name)    mtx_init(l, name, NULL, MTX_DEF)
201 #define DRM_SPINUNINIT(l)       mtx_destroy(l)
202 #define DRM_SPINLOCK(l)         mtx_lock(l)
203 #define DRM_SPINUNLOCK(u)       mtx_unlock(u)
204 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {          \
205         mtx_lock(l);                                    \
206         (void)irqflags;                                 \
207 } while (0)
208 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mtx_unlock(u)
209 #define DRM_SPINLOCK_ASSERT(l)  mtx_assert(l, MA_OWNED)
210 #define DRM_CURRENTPID          curthread->td_proc->p_pid
211 #define DRM_LOCK(dev)           sx_xlock(&(dev)->dev_struct_lock)
212 #define DRM_UNLOCK(dev)         sx_xunlock(&(dev)->dev_struct_lock)
213 #define DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)                  \
214     (sx_sleep((chan), &(dev)->dev_struct_lock, (flags), (msg), (timeout)))
215 #if defined(INVARIANTS)
216 #define DRM_LOCK_ASSERT(dev)    sx_assert(&(dev)->dev_struct_lock, SA_XLOCKED)
217 #define DRM_UNLOCK_ASSERT(dev)  sx_assert(&(dev)->dev_struct_lock, SA_UNLOCKED)
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 #define unlikely(x)            __builtin_expect(!!(x), 0)
231 #define likely(x)              __builtin_expect(!!(x), 1)
232 #define container_of(ptr, type, member) ({                      \
233         __typeof( ((type *)0)->member ) *__mptr = (ptr);        \
234         (type *)( (char *)__mptr - offsetof(type,member) );})
235
236 enum {
237         DRM_IS_NOT_AGP,
238         DRM_IS_AGP,
239         DRM_MIGHT_BE_AGP
240 };
241 #define DRM_AGP_MEM             struct agp_memory_info
242
243 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
244
245 #define PAGE_ALIGN(addr) round_page(addr)
246 /* DRM_SUSER returns true if the user is superuser */
247 #define DRM_SUSER(p)            (priv_check(p, PRIV_DRIVER) == 0)
248 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
249 #define DRM_MTRR_WC             MDF_WRITECOMBINE
250 #define jiffies                 ticks
251 #define jiffies_to_msecs(x)     (((int64_t)(x)) * 1000 / hz)
252 #define msecs_to_jiffies(x)     (((int64_t)(x)) * hz / 1000)
253 #define time_after(a,b)         ((long)(b) - (long)(a) < 0)
254 #define time_after_eq(a,b)      ((long)(b) - (long)(a) <= 0)
255 #define drm_msleep(x, msg)      pause((msg), ((int64_t)(x)) * hz / 1000)
256
257 typedef vm_paddr_t dma_addr_t;
258 typedef uint64_t u64;
259 typedef uint32_t u32;
260 typedef uint16_t u16;
261 typedef uint8_t u8;
262 typedef int64_t s64;
263 typedef int32_t s32;
264 typedef int16_t s16;
265 typedef int8_t s8;
266
267 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
268  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
269  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
270  */
271 #define DRM_READMEMORYBARRIER()         rmb()
272 #define DRM_WRITEMEMORYBARRIER()        wmb()
273 #define DRM_MEMORYBARRIER()             mb()
274
275 #define DRM_READ8(map, offset)                                          \
276         *(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +          \
277             (vm_offset_t)(offset))
278 #define DRM_READ16(map, offset)                                         \
279         le16toh(*(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) + \
280             (vm_offset_t)(offset)))
281 #define DRM_READ32(map, offset)                                         \
282         le32toh(*(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) + \
283             (vm_offset_t)(offset)))
284 #define DRM_READ64(map, offset)                                         \
285         le64toh(*(volatile u_int64_t *)(((vm_offset_t)(map)->virtual) + \
286             (vm_offset_t)(offset)))
287 #define DRM_WRITE8(map, offset, val)                                    \
288         *(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +          \
289             (vm_offset_t)(offset)) = val
290 #define DRM_WRITE16(map, offset, val)                                   \
291         *(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) +         \
292             (vm_offset_t)(offset)) = htole16(val)
293 #define DRM_WRITE32(map, offset, val)                                   \
294         *(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) +         \
295             (vm_offset_t)(offset)) = htole32(val)
296 #define DRM_WRITE64(map, offset, val)                                   \
297         *(volatile u_int64_t *)(((vm_offset_t)(map)->virtual) +         \
298             (vm_offset_t)(offset)) = htole64(val)
299
300 #define DRM_VERIFYAREA_READ( uaddr, size )              \
301         (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
302
303 #define DRM_COPY_TO_USER(user, kern, size) \
304         copyout(kern, user, size)
305 #define DRM_COPY_FROM_USER(kern, user, size) \
306         copyin(user, kern, size)
307 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
308         copyin(arg2, arg1, arg3)
309 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
310         copyout(arg2, arg1, arg3)
311 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
312         ((val) = fuword32(uaddr), 0)
313
314 #define cpu_to_le32(x) htole32(x)
315 #define le32_to_cpu(x) le32toh(x)
316
317 #define DRM_HZ                  hz
318 #define DRM_UDELAY(udelay)      DELAY(udelay)
319 #define DRM_MDELAY(msecs)       do { int loops = (msecs);               \
320                                   while (loops--) DELAY(1000);          \
321                                 } while (0)
322 #define DRM_TIME_SLICE          (hz/20)  /* Time slice for GLXContexts    */
323
324 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
325         (_map) = (_dev)->context_sareas[_ctx];          \
326 } while(0)
327
328 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
329 do {                                                                    \
330         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
331              dev->lock.file_priv != file_priv) {                        \
332                 DRM_ERROR("%s called without lock held\n",              \
333                            __FUNCTION__);                               \
334                 return EINVAL;                                          \
335         }                                                               \
336 } while (0)
337
338 /* Returns -errno to shared code */
339 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
340 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
341         DRM_UNLOCK(dev);                                                \
342         mtx_lock(&dev->irq_lock);                               \
343         if (!(condition))                                       \
344             ret = -mtx_sleep(&(queue), &dev->irq_lock,          \
345                 PCATCH, "drmwtq", (timeout));                   \
346         mtx_unlock(&dev->irq_lock);                             \
347         DRM_LOCK(dev);                                          \
348 }
349
350 #define DRM_ERROR(fmt, ...) \
351         printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,           \
352             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
353
354 #define DRM_INFO(fmt, ...)  printf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
355
356 #define DRM_DEBUG(fmt, ...) do {                                        \
357         if ((drm_debug_flag & DRM_DEBUGBITS_DEBUG) != 0)                \
358                 printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,  \
359                         __func__ , ##__VA_ARGS__);                      \
360 } while (0)
361
362 #define DRM_DEBUG_KMS(fmt, ...) do {                                    \
363         if ((drm_debug_flag & DRM_DEBUGBITS_KMS) != 0)                  \
364                 printf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
365                         __func__ , ##__VA_ARGS__);                      \
366 } while (0)
367
368 #define DRM_DEBUG_DRIVER(fmt, ...) do {                                 \
369         if ((drm_debug_flag & DRM_DEBUGBITS_KMS) != 0)                  \
370                 printf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
371                         __func__ , ##__VA_ARGS__);                      \
372 } while (0)
373
374 typedef struct drm_pci_id_list
375 {
376         int vendor;
377         int device;
378         long driver_private;
379         char *name;
380 } drm_pci_id_list_t;
381
382 struct drm_msi_blacklist_entry
383 {
384         int vendor;
385         int device;
386 };
387
388 #define DRM_AUTH        0x1
389 #define DRM_MASTER      0x2
390 #define DRM_ROOT_ONLY   0x4
391 #define DRM_CONTROL_ALLOW 0x8
392 #define DRM_UNLOCKED    0x10
393
394 typedef struct drm_ioctl_desc {
395         unsigned long cmd;
396         int (*func)(struct drm_device *dev, void *data,
397                     struct drm_file *file_priv);
398         int flags;
399 } drm_ioctl_desc_t;
400 /**
401  * Creates a driver or general drm_ioctl_desc array entry for the given
402  * ioctl, for use by drm_ioctl().
403  */
404 #define DRM_IOCTL_DEF(ioctl, func, flags) \
405         [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
406
407 typedef struct drm_magic_entry {
408         drm_magic_t            magic;
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  pending;     /* On hardware DMA queue              */
428         struct drm_file   *file_priv;  /* Unique identifier of holding process */
429         int               context;     /* Kernel queue for this buffer       */
430         enum {
431                 DRM_LIST_NONE    = 0,
432                 DRM_LIST_FREE    = 1,
433                 DRM_LIST_WAIT    = 2,
434                 DRM_LIST_PEND    = 3,
435                 DRM_LIST_PRIO    = 4,
436                 DRM_LIST_RECLAIM = 5
437         }                 list;        /* Which list we're on                */
438
439         int               dev_priv_size; /* Size of buffer private stoarge   */
440         void              *dev_private;  /* Per-buffer private storage       */
441 } drm_buf_t;
442
443 typedef struct drm_freelist {
444         int               initialized; /* Freelist in use                  */
445         atomic_t          count;       /* Number of free buffers           */
446         drm_buf_t         *next;       /* End pointer                      */
447
448         int               low_mark;    /* Low water mark                   */
449         int               high_mark;   /* High water mark                  */
450 } drm_freelist_t;
451
452 typedef struct drm_dma_handle {
453         void *vaddr;
454         bus_addr_t busaddr;
455         bus_dma_tag_t tag;
456         bus_dmamap_t map;
457 } drm_dma_handle_t;
458
459 typedef struct drm_buf_entry {
460         int               buf_size;
461         int               buf_count;
462         drm_buf_t         *buflist;
463         int               seg_count;
464         drm_dma_handle_t  **seglist;
465         int               page_order;
466
467         drm_freelist_t    freelist;
468 } drm_buf_entry_t;
469
470 /* Event queued up for userspace to read */
471 struct drm_pending_event {
472         struct drm_event *event;
473         struct list_head link;
474         struct drm_file *file_priv;
475         pid_t pid; /* pid of requester, no guarantee it's valid by the time
476                       we deliver the event, for tracing only */
477         void (*destroy)(struct drm_pending_event *event);
478 };
479
480 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
481 struct drm_file {
482         TAILQ_ENTRY(drm_file) link;
483         struct drm_device *dev;
484         int               authenticated;
485         int               master;
486         pid_t             pid;
487         uid_t             uid;
488         drm_magic_t       magic;
489         unsigned long     ioctl_count;
490
491         void             *driver_priv;
492         struct drm_gem_names object_names;
493
494         int               is_master;
495         struct drm_master *masterp;
496
497         struct list_head  fbs;
498
499         struct list_head  event_list;
500         int               event_space;
501         struct selinfo    event_poll;
502 };
503
504 typedef struct drm_lock_data {
505         struct drm_hw_lock      *hw_lock;       /* Hardware lock                   */
506         struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
507         int               lock_queue;   /* Queue of blocked processes      */
508         unsigned long     lock_time;    /* Time of last lock in jiffies    */
509 } drm_lock_data_t;
510
511 /* This structure, in the struct drm_device, is always initialized while the
512  * device
513  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
514  * when set marks that no further bufs may be allocated until device teardown
515  * occurs (when the last open of the device has closed).  The high/low
516  * watermarks of bufs are only touched by the X Server, and thus not
517  * concurrently accessed, so no locking is needed.
518  */
519 typedef struct drm_device_dma {
520         drm_buf_entry_t   bufs[DRM_MAX_ORDER+1];
521         int               buf_count;
522         drm_buf_t         **buflist;    /* Vector of pointers info bufs    */
523         int               seg_count;
524         int               page_count;
525         unsigned long     *pagelist;
526         unsigned long     byte_count;
527         enum {
528                 _DRM_DMA_USE_AGP = 0x01,
529                 _DRM_DMA_USE_SG  = 0x02
530         } flags;
531 } drm_device_dma_t;
532
533 typedef struct drm_agp_mem {
534         void               *handle;
535         unsigned long      bound; /* address */
536         int                pages;
537         struct drm_agp_mem *prev;
538         struct drm_agp_mem *next;
539 } drm_agp_mem_t;
540
541 typedef struct drm_agp_head {
542         device_t           agpdev;
543         struct agp_info    info;
544         const char         *chipset;
545         drm_agp_mem_t      *memory;
546         unsigned long      mode;
547         int                enabled;
548         int                acquired;
549         unsigned long      base;
550         int                mtrr;
551         int                cant_use_aperture;
552         unsigned long      page_mask;
553 } drm_agp_head_t;
554
555 typedef struct drm_sg_mem {
556         vm_offset_t vaddr;
557         vm_paddr_t *busaddr;
558         vm_pindex_t pages;
559 } drm_sg_mem_t;
560
561 #define DRM_MAP_HANDLE_BITS     (sizeof(void *) == 4 ? 4 : 24)
562 #define DRM_MAP_HANDLE_SHIFT    (sizeof(void *) * 8 - DRM_MAP_HANDLE_BITS)
563 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
564
565 typedef struct drm_local_map {
566         unsigned long offset;     /* Physical address (0 for SAREA)       */
567         unsigned long size;       /* Physical size (bytes)                */
568         enum drm_map_type type;   /* Type of memory mapped                */
569         enum drm_map_flags flags; /* Flags                                */
570         void *handle;             /* User-space: "Handle" to pass to mmap */
571                                   /* Kernel-space: kernel-virtual address */
572         int mtrr;                 /* Boolean: MTRR used                   */
573                                   /* Private data                         */
574         int rid;                  /* PCI resource ID for bus_space        */
575         void *virtual;            /* Kernel-space: kernel-virtual address */
576         struct resource *bsr;
577         bus_space_tag_t bst;
578         bus_space_handle_t bsh;
579         drm_dma_handle_t *dmah;
580         TAILQ_ENTRY(drm_local_map) link;
581 } drm_local_map_t;
582
583 struct drm_vblank_info {
584         wait_queue_head_t queue;        /* vblank wait queue */
585         atomic_t count;                 /* number of VBLANK interrupts */
586                                         /* (driver must alloc the right number of counters) */
587         atomic_t refcount;              /* number of users of vblank interrupts */
588         u32 last;                       /* protected by dev->vbl_lock, used */
589                                         /* for wraparound handling */
590         int enabled;                    /* so we don't call enable more than */
591                                         /* once per disable */
592         int inmodeset;                  /* Display driver is setting mode */
593 };
594
595 /* Size of ringbuffer for vblank timestamps. Just double-buffer
596  * in initial implementation.
597  */
598 #define DRM_VBLANKTIME_RBSIZE 2
599
600 /* Flags and return codes for get_vblank_timestamp() driver function. */
601 #define DRM_CALLED_FROM_VBLIRQ 1
602 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
603 #define DRM_VBLANKTIME_INVBL             (1 << 1)
604
605 /* get_scanout_position() return flags */
606 #define DRM_SCANOUTPOS_VALID        (1 << 0)
607 #define DRM_SCANOUTPOS_INVBL        (1 << 1)
608 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
609
610 /* location of GART table */
611 #define DRM_ATI_GART_MAIN 1
612 #define DRM_ATI_GART_FB   2
613
614 #define DRM_ATI_GART_PCI  1
615 #define DRM_ATI_GART_PCIE 2
616 #define DRM_ATI_GART_IGP  3
617
618 struct drm_ati_pcigart_info {
619         int gart_table_location;
620         int gart_reg_if;
621         void *addr;
622         dma_addr_t bus_addr;
623         dma_addr_t table_mask;
624         dma_addr_t member_mask;
625         struct drm_dma_handle *table_handle;
626         drm_local_map_t mapping;
627         int table_size;
628         struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
629 };
630
631 typedef vm_paddr_t resource_size_t;
632
633 /**
634  * GEM specific mm private for tracking GEM objects
635  */
636 struct drm_gem_mm {
637         struct drm_open_hash offset_hash; /**< User token hash table for maps */
638         struct unrhdr *idxunr;
639 };
640
641 struct drm_gem_object {
642         /** Reference count of this object */
643         u_int refcount;
644
645         /** Handle count of this object. Each handle also holds a reference */
646         u_int handle_count; /* number of handles on this object */
647
648         /** Related drm device */
649         struct drm_device *dev;
650
651         /** File representing the shmem storage: filp in Linux parlance */
652         vm_object_t vm_obj;
653
654         bool on_map;
655         struct drm_hash_item map_list;
656
657         /**
658          * Size of the object, in bytes.  Immutable over the object's
659          * lifetime.
660          */
661         size_t size;
662
663         /**
664          * Global name for this object, starts at 1. 0 means unnamed.
665          * Access is covered by the object_name_lock in the related drm_device
666          */
667         int name;
668
669         /**
670          * Memory domains. These monitor which caches contain read/write data
671          * related to the object. When transitioning from one set of domains
672          * to another, the driver is called to ensure that caches are suitably
673          * flushed and invalidated
674          */
675         uint32_t read_domains;
676         uint32_t write_domain;
677
678         /**
679          * While validating an exec operation, the
680          * new read/write domain values are computed here.
681          * They will be transferred to the above values
682          * at the point that any cache flushing occurs
683          */
684         uint32_t pending_read_domains;
685         uint32_t pending_write_domain;
686
687         void *driver_private;
688 };
689
690 #include "drm_crtc.h"
691
692 #ifndef DMA_BIT_MASK
693 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
694 #endif
695
696 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
697
698 struct drm_driver_info {
699         int     (*load)(struct drm_device *, unsigned long flags);
700         int     (*use_msi)(struct drm_device *, unsigned long flags);
701         int     (*firstopen)(struct drm_device *);
702         int     (*open)(struct drm_device *, struct drm_file *);
703         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
704         void    (*postclose)(struct drm_device *, struct drm_file *);
705         void    (*lastclose)(struct drm_device *);
706         int     (*unload)(struct drm_device *);
707         void    (*reclaim_buffers_locked)(struct drm_device *,
708                                           struct drm_file *file_priv);
709         int     (*dma_ioctl)(struct drm_device *dev, void *data,
710                              struct drm_file *file_priv);
711         void    (*dma_ready)(struct drm_device *);
712         int     (*dma_quiescent)(struct drm_device *);
713         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
714                                              enum drm_lock_flags flags);
715         int     (*dma_flush_unblock)(struct drm_device *, int context,
716                                      enum drm_lock_flags flags);
717         int     (*context_ctor)(struct drm_device *dev, int context);
718         int     (*context_dtor)(struct drm_device *dev, int context);
719         int     (*kernel_context_switch)(struct drm_device *dev, int old,
720                                          int new);
721         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
722         void    (*irq_preinstall)(struct drm_device *dev);
723         int     (*irq_postinstall)(struct drm_device *dev);
724         void    (*irq_uninstall)(struct drm_device *dev);
725         void    (*irq_handler)(DRM_IRQ_ARGS);
726
727         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
728         int     (*enable_vblank)(struct drm_device *dev, int crtc);
729         void    (*disable_vblank)(struct drm_device *dev, int crtc);
730         int     (*get_scanout_position)(struct drm_device *dev, int crtc,
731                     int *vpos, int *hpos);
732
733         int     (*get_vblank_timestamp)(struct drm_device *dev, int crtc,
734                     int *max_error, struct timeval *vblank_time,
735                     unsigned flags);
736
737         int     (*gem_init_object)(struct drm_gem_object *obj);
738         void    (*gem_free_object)(struct drm_gem_object *obj);
739         int     (*gem_open_object)(struct drm_gem_object *, struct drm_file *);
740         void    (*gem_close_object)(struct drm_gem_object *, struct drm_file *);
741
742         struct cdev_pager_ops *gem_pager_ops;
743
744         int     (*dumb_create)(struct drm_file *file_priv,
745                     struct drm_device *dev, struct drm_mode_create_dumb *args);
746         int     (*dumb_map_offset)(struct drm_file *file_priv,
747                     struct drm_device *dev, uint32_t handle, uint64_t *offset);
748         int     (*dumb_destroy)(struct drm_file *file_priv,
749                     struct drm_device *dev, uint32_t handle);
750
751         int     (*sysctl_init)(struct drm_device *dev,
752                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
753         void    (*sysctl_cleanup)(struct drm_device *dev);
754
755         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
756
757         /**
758          * Called by \c drm_device_is_agp.  Typically used to determine if a
759          * card is really attached to AGP or not.
760          *
761          * \param dev  DRM device handle
762          *
763          * \returns 
764          * One of three values is returned depending on whether or not the
765          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
766          * (return of 1), or may or may not be AGP (return of 2).
767          */
768         int     (*device_is_agp) (struct drm_device * dev);
769
770         drm_ioctl_desc_t *ioctls;
771 #ifdef COMPAT_FREEBSD32
772         drm_ioctl_desc_t *compat_ioctls;
773         int     *compat_ioctls_nr;
774 #endif
775         int     max_ioctl;
776
777         int     buf_priv_size;
778
779         int     major;
780         int     minor;
781         int     patchlevel;
782         const char *name;               /* Simple driver name              */
783         const char *desc;               /* Longer driver name              */
784         const char *date;               /* Date of last major changes.     */
785
786         u32 driver_features;
787 };
788
789 /**
790  * DRM minor structure. This structure represents a drm minor number.
791  */
792 struct drm_minor {
793         int index;                      /**< Minor device number */
794         int type;                       /**< Control or render */
795         device_t kdev;                  /**< OS device */
796         struct drm_device *dev;
797
798         struct drm_master *master; /* currently active master for this node */
799         struct list_head master_list;
800         struct drm_mode_group mode_group;
801 };
802
803 /* mode specified on the command line */
804 struct drm_cmdline_mode {
805         bool specified;
806         bool refresh_specified;
807         bool bpp_specified;
808         int xres, yres;
809         int bpp;
810         int refresh;
811         bool rb;
812         bool interlace;
813         bool cvt;
814         bool margins;
815         enum drm_connector_force force;
816 };
817
818 struct drm_pending_vblank_event {
819         struct drm_pending_event base;
820         int pipe;
821         struct drm_event_vblank event;
822 };
823
824 /* Length for the array of resource pointers for drm_get_resource_*. */
825 #define DRM_MAX_PCI_RESOURCE    6
826
827 /** 
828  * DRM device functions structure
829  */
830 struct drm_device {
831         struct drm_driver_info *driver;
832         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
833
834         uint16_t pci_device;            /* PCI device id */
835         uint16_t pci_vendor;            /* PCI vendor id */
836         uint16_t pci_subdevice;         /* PCI subsystem device id */
837         uint16_t pci_subvendor;         /* PCI subsystem vendor id */
838
839         char              *unique;      /* Unique identifier: e.g., busid  */
840         int               unique_len;   /* Length of unique field          */
841         device_t          device;       /* Device instance from newbus     */
842         struct cdev       *devnode;     /* Device number for mknod         */
843         int               if_version;   /* Highest interface version set */
844
845         int               flags;        /* Flags to open(2)                */
846
847                                 /* Locks */
848         struct mtx        dma_lock;     /* protects dev->dma */
849         struct mtx        irq_lock;     /* protects irq condition checks */
850         struct mtx        dev_lock;     /* protects everything else */
851         struct sx         dev_struct_lock;
852         DRM_SPINTYPE      drw_lock;
853
854                                 /* Usage Counters */
855         int               open_count;   /* Outstanding files open          */
856         int               buf_use;      /* Buffers in use -- cannot alloc  */
857
858                                 /* Performance counters */
859         unsigned long     counters;
860         enum drm_stat_type      types[15];
861         atomic_t          counts[15];
862
863                                 /* Authentication */
864         drm_file_list_t   files;
865         drm_magic_head_t  magiclist[DRM_HASH_SIZE];
866
867         /* Linked list of mappable regions. Protected by dev_lock */
868         drm_map_list_t    maplist;
869         struct unrhdr     *map_unrhdr;
870
871         drm_local_map_t   **context_sareas;
872         int               max_context;
873
874         drm_lock_data_t   lock;         /* Information on hardware lock    */
875
876                                 /* DMA queues (contexts) */
877         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
878
879                                 /* Context support */
880         int               irq;          /* Interrupt used by board         */
881         int               irq_enabled;  /* True if the irq handler is enabled */
882         int               msi_enabled;  /* MSI enabled */
883         int               irqrid;       /* Interrupt used by board */
884         struct resource   *irqr;        /* Resource for interrupt used by board    */
885         void              *irqh;        /* Handle from bus_setup_intr      */
886
887         /* Storage of resource pointers for drm_get_resource_* */
888         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
889         int               pcirid[DRM_MAX_PCI_RESOURCE];
890
891         int               pci_domain;
892         int               pci_bus;
893         int               pci_slot;
894         int               pci_func;
895
896         atomic_t          context_flag; /* Context swapping flag           */
897         int               last_context; /* Last current context            */
898
899         int               num_crtcs;
900
901         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
902
903                                 /* Sysctl support */
904         struct drm_sysctl_info *sysctl;
905         int               sysctl_node_idx;
906
907         drm_agp_head_t    *agp;
908         drm_sg_mem_t      *sg;  /* Scatter gather memory */
909         atomic_t          *ctx_bitmap;
910         void              *dev_private;
911         unsigned int      agp_buffer_token;
912         drm_local_map_t   *agp_buffer_map;
913
914         struct drm_minor *control;              /**< Control node for card */
915         struct drm_minor *primary;              /**< render type primary screen head */
916
917         struct unrhdr     *drw_unrhdr;
918         /* RB tree of drawable infos */
919         RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
920
921         int vblank_disable_allowed;
922
923         atomic_t *_vblank_count;        /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
924         struct timeval *_vblank_time;   /**< timestamp of current vblank_count (drivers must alloc right number of fields) */
925         struct mtx vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
926         struct mtx vbl_lock;
927         atomic_t *vblank_refcount;      /* number of users of vblank interruptsper crtc */
928         u32 *last_vblank;               /* protected by dev->vbl_lock, used */
929                                         /* for wraparound handling */
930         int *vblank_enabled;            /* so we don't call enable more than
931                                            once per disable */
932         int *vblank_inmodeset;          /* Display driver is setting mode */
933         u32 *last_vblank_wait;          /* Last vblank seqno waited per CRTC */
934         struct callout vblank_disable_callout;
935
936         u32 max_vblank_count;           /**< size of vblank counter register */
937
938         struct list_head vblank_event_list;
939         struct mtx       event_lock;
940
941         struct drm_mode_config mode_config;     /**< Current mode config */
942
943         /* GEM part */
944         struct sx         object_name_lock;
945         struct drm_gem_names object_names;
946         void             *mm_private;
947
948         void *sysctl_private;
949         char busid_str[128];
950         int modesetting;
951 };
952
953 static __inline__ int drm_core_check_feature(struct drm_device *dev,
954                                              int feature)
955 {
956         return ((dev->driver->driver_features & feature) ? 1 : 0);
957 }
958
959 #if __OS_HAS_AGP
960 static inline int drm_core_has_AGP(struct drm_device *dev)
961 {
962         return drm_core_check_feature(dev, DRIVER_USE_AGP);
963 }
964 #else
965 #define drm_core_has_AGP(dev) (0)
966 #endif
967
968 enum dmi_field {
969         DMI_NONE,
970         DMI_BIOS_VENDOR,
971         DMI_BIOS_VERSION,
972         DMI_BIOS_DATE,
973         DMI_SYS_VENDOR,
974         DMI_PRODUCT_NAME,
975         DMI_PRODUCT_VERSION,
976         DMI_PRODUCT_SERIAL,
977         DMI_PRODUCT_UUID,
978         DMI_BOARD_VENDOR,
979         DMI_BOARD_NAME,
980         DMI_BOARD_VERSION,
981         DMI_BOARD_SERIAL,
982         DMI_BOARD_ASSET_TAG,
983         DMI_CHASSIS_VENDOR,
984         DMI_CHASSIS_TYPE,
985         DMI_CHASSIS_VERSION,
986         DMI_CHASSIS_SERIAL,
987         DMI_CHASSIS_ASSET_TAG,
988         DMI_STRING_MAX,
989 };
990
991 struct dmi_strmatch {
992         unsigned char slot;
993         char substr[79];
994 };
995
996 struct dmi_system_id {
997         int (*callback)(const struct dmi_system_id *);
998         const char *ident;
999         struct dmi_strmatch matches[4];
1000 };
1001 #define DMI_MATCH(a, b) {(a), (b)}
1002 bool dmi_check_system(const struct dmi_system_id *);
1003
1004 extern int      drm_debug_flag;
1005 extern int      drm_notyet_flag;
1006 extern unsigned int drm_vblank_offdelay;
1007 extern unsigned int drm_timestamp_precision;
1008
1009 /* Device setup support (drm_drv.c) */
1010 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1011 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1012 int     drm_create_cdevs(device_t kdev);
1013 void    drm_close(void *data);
1014 int     drm_detach(device_t kdev);
1015 d_ioctl_t drm_ioctl;
1016 d_open_t drm_open;
1017 d_read_t drm_read;
1018 d_poll_t drm_poll;
1019 d_mmap_t drm_mmap;
1020 extern drm_local_map_t  *drm_getsarea(struct drm_device *dev);
1021
1022 void drm_event_wakeup(struct drm_pending_event *e);
1023
1024 int drm_add_busid_modesetting(struct drm_device *dev,
1025     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1026
1027 /* File operations helpers (drm_fops.c) */
1028 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1029                                          DRM_STRUCTPROC *p,
1030                                         struct drm_device *dev);
1031
1032 /* Memory management support (drm_memory.c) */
1033 void    drm_mem_init(void);
1034 void    drm_mem_uninit(void);
1035 void    *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1036 void    *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1037 void    drm_ioremapfree(drm_local_map_t *map);
1038 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1039 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1040
1041 int     drm_context_switch(struct drm_device *dev, int old, int new);
1042 int     drm_context_switch_complete(struct drm_device *dev, int new);
1043
1044 int     drm_ctxbitmap_init(struct drm_device *dev);
1045 void    drm_ctxbitmap_cleanup(struct drm_device *dev);
1046 void    drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1047 int     drm_ctxbitmap_next(struct drm_device *dev);
1048
1049 /* Locking IOCTL support (drm_lock.c) */
1050 int     drm_lock_take(struct drm_lock_data *lock_data,
1051                       unsigned int context);
1052 int     drm_lock_transfer(struct drm_lock_data *lock_data,
1053                           unsigned int context);
1054 int     drm_lock_free(struct drm_lock_data *lock_data,
1055                       unsigned int context);
1056
1057 /* Buffer management support (drm_bufs.c) */
1058 unsigned long drm_get_resource_start(struct drm_device *dev,
1059                                      unsigned int resource);
1060 unsigned long drm_get_resource_len(struct drm_device *dev,
1061                                    unsigned int resource);
1062 void    drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1063 int     drm_order(unsigned long size);
1064 int     drm_addmap(struct drm_device *dev, unsigned long offset,
1065                    unsigned long size,
1066                    enum drm_map_type type, enum drm_map_flags flags,
1067                    drm_local_map_t **map_ptr);
1068 int     drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
1069 int     drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request);
1070 int     drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
1071
1072 /* DMA support (drm_dma.c) */
1073 int     drm_dma_setup(struct drm_device *dev);
1074 void    drm_dma_takedown(struct drm_device *dev);
1075 void    drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
1076 void    drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
1077 #define drm_core_reclaim_buffers drm_reclaim_buffers
1078
1079 /* IRQ support (drm_irq.c) */
1080 int     drm_irq_install(struct drm_device *dev);
1081 int     drm_irq_uninstall(struct drm_device *dev);
1082 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1083 void    drm_driver_irq_preinstall(struct drm_device *dev);
1084 void    drm_driver_irq_postinstall(struct drm_device *dev);
1085 void    drm_driver_irq_uninstall(struct drm_device *dev);
1086
1087 void    drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1088 void    drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1089 int     drm_modeset_ctl(struct drm_device *dev, void *data,
1090                         struct drm_file *file_priv);
1091
1092 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1093 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1094                            struct drm_file *filp);
1095 extern int drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
1096 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1097 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1098                                      struct timeval *vblanktime);
1099 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1100 void drm_handle_vblank_events(struct drm_device *dev, int crtc);
1101 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1102 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1103 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1104 extern void drm_vblank_cleanup(struct drm_device *dev);
1105 extern u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1106                                      struct timeval *tvblank, unsigned flags);
1107 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1108                                                  int crtc, int *max_error,
1109                                                  struct timeval *vblank_time,
1110                                                  unsigned flags,
1111                                                  struct drm_crtc *refcrtc);
1112 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc);
1113
1114 struct timeval ns_to_timeval(const int64_t nsec);
1115 int64_t timeval_to_ns(const struct timeval *tv);
1116
1117 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
1118 int     drm_device_is_agp(struct drm_device *dev);
1119 int     drm_device_is_pcie(struct drm_device *dev);
1120 drm_agp_head_t *drm_agp_init(void);
1121 int     drm_agp_acquire(struct drm_device *dev);
1122 int     drm_agp_release(struct drm_device *dev);
1123 int     drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
1124 int     drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1125 void    *drm_agp_allocate_memory(size_t pages, u32 type);
1126 int     drm_agp_free_memory(void *handle);
1127 int     drm_agp_bind_memory(void *handle, off_t start);
1128 int     drm_agp_unbind_memory(void *handle);
1129 int     drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1130 int     drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1131 int     drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1132 int     drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1133
1134 /* Scatter Gather Support (drm_scatter.c) */
1135 void    drm_sg_cleanup(drm_sg_mem_t *entry);
1136 int     drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1137
1138 /* sysctl support (drm_sysctl.h) */
1139 extern int              drm_sysctl_init(struct drm_device *dev);
1140 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1141
1142 /* ATI PCIGART support (ati_pcigart.c) */
1143 int     drm_ati_pcigart_init(struct drm_device *dev,
1144                                 struct drm_ati_pcigart_info *gart_info);
1145 int     drm_ati_pcigart_cleanup(struct drm_device *dev,
1146                                 struct drm_ati_pcigart_info *gart_info);
1147
1148 /* Cache management (drm_memory.c) */
1149 void    drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1150
1151 /* Locking IOCTL support (drm_drv.c) */
1152 int     drm_lock(struct drm_device *dev, void *data,
1153                  struct drm_file *file_priv);
1154 int     drm_unlock(struct drm_device *dev, void *data,
1155                    struct drm_file *file_priv);
1156 int     drm_version(struct drm_device *dev, void *data,
1157                     struct drm_file *file_priv);
1158 int     drm_setversion(struct drm_device *dev, void *data,
1159                        struct drm_file *file_priv);
1160
1161 /* Misc. IOCTL support (drm_ioctl.c) */
1162 int     drm_irq_by_busid(struct drm_device *dev, void *data,
1163                          struct drm_file *file_priv);
1164 int     drm_getunique(struct drm_device *dev, void *data,
1165                       struct drm_file *file_priv);
1166 int     drm_setunique(struct drm_device *dev, void *data,
1167                       struct drm_file *file_priv);
1168 int     drm_getmap(struct drm_device *dev, void *data,
1169                    struct drm_file *file_priv);
1170 int     drm_getclient(struct drm_device *dev, void *data,
1171                       struct drm_file *file_priv);
1172 int     drm_getstats(struct drm_device *dev, void *data,
1173                      struct drm_file *file_priv);
1174 int     drm_getcap(struct drm_device *dev, void *data,
1175                      struct drm_file *file_priv);
1176 int     drm_noop(struct drm_device *dev, void *data,
1177                  struct drm_file *file_priv);
1178
1179 /* Context IOCTL support (drm_context.c) */
1180 int     drm_resctx(struct drm_device *dev, void *data,
1181                    struct drm_file *file_priv);
1182 int     drm_addctx(struct drm_device *dev, void *data,
1183                    struct drm_file *file_priv);
1184 int     drm_modctx(struct drm_device *dev, void *data,
1185                    struct drm_file *file_priv);
1186 int     drm_getctx(struct drm_device *dev, void *data,
1187                    struct drm_file *file_priv);
1188 int     drm_switchctx(struct drm_device *dev, void *data,
1189                       struct drm_file *file_priv);
1190 int     drm_newctx(struct drm_device *dev, void *data,
1191                    struct drm_file *file_priv);
1192 int     drm_rmctx(struct drm_device *dev, void *data,
1193                   struct drm_file *file_priv);
1194 int     drm_setsareactx(struct drm_device *dev, void *data,
1195                         struct drm_file *file_priv);
1196 int     drm_getsareactx(struct drm_device *dev, void *data,
1197                         struct drm_file *file_priv);
1198
1199 /* Drawable IOCTL support (drm_drawable.c) */
1200 int     drm_adddraw(struct drm_device *dev, void *data,
1201                     struct drm_file *file_priv);
1202 int     drm_rmdraw(struct drm_device *dev, void *data,
1203                    struct drm_file *file_priv);
1204 int     drm_update_draw(struct drm_device *dev, void *data,
1205                         struct drm_file *file_priv);
1206 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1207                                                 int handle);
1208
1209 /* Drawable support (drm_drawable.c) */
1210 void drm_drawable_free_all(struct drm_device *dev);
1211
1212 /* Authentication IOCTL support (drm_auth.c) */
1213 int     drm_getmagic(struct drm_device *dev, void *data,
1214                      struct drm_file *file_priv);
1215 int     drm_authmagic(struct drm_device *dev, void *data,
1216                       struct drm_file *file_priv);
1217
1218 /* Buffer management support (drm_bufs.c) */
1219 int     drm_addmap_ioctl(struct drm_device *dev, void *data,
1220                          struct drm_file *file_priv);
1221 int     drm_rmmap_ioctl(struct drm_device *dev, void *data,
1222                         struct drm_file *file_priv);
1223 int     drm_addbufs(struct drm_device *dev, void *data,
1224                     struct drm_file *file_priv);
1225 int     drm_infobufs(struct drm_device *dev, void *data,
1226                      struct drm_file *file_priv);
1227 int     drm_markbufs(struct drm_device *dev, void *data,
1228                      struct drm_file *file_priv);
1229 int     drm_freebufs(struct drm_device *dev, void *data,
1230                      struct drm_file *file_priv);
1231 int     drm_mapbufs(struct drm_device *dev, void *data,
1232                     struct drm_file *file_priv);
1233
1234 /* DMA support (drm_dma.c) */
1235 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1236
1237 /* IRQ support (drm_irq.c) */
1238 int     drm_control(struct drm_device *dev, void *data,
1239                     struct drm_file *file_priv);
1240
1241 /* AGP/GART support (drm_agpsupport.c) */
1242 int     drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1243                               struct drm_file *file_priv);
1244 int     drm_agp_release_ioctl(struct drm_device *dev, void *data,
1245                               struct drm_file *file_priv);
1246 int     drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1247                              struct drm_file *file_priv);
1248 int     drm_agp_info_ioctl(struct drm_device *dev, void *data,
1249                            struct drm_file *file_priv);
1250 int     drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1251                             struct drm_file *file_priv);
1252 int     drm_agp_free_ioctl(struct drm_device *dev, void *data,
1253                            struct drm_file *file_priv);
1254 int     drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1255                              struct drm_file *file_priv);
1256 int     drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1257                            struct drm_file *file_priv);
1258
1259                                 /* Stub support (drm_stub.h) */
1260 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1261                                struct drm_file *file_priv);
1262 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1263                                 struct drm_file *file_priv);
1264
1265 /* Scatter Gather Support (drm_scatter.c) */
1266 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1267                            struct drm_file *file_priv);
1268 int     drm_sg_free(struct drm_device *dev, void *data,
1269                     struct drm_file *file_priv);
1270
1271 /* consistent PCI memory functions (drm_pci.c) */
1272 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1273                                 size_t align, dma_addr_t maxaddr);
1274 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1275
1276 /* Graphics Execution Manager library functions (drm_gem.c) */
1277 int drm_gem_init(struct drm_device *dev);
1278 void drm_gem_destroy(struct drm_device *dev);
1279
1280 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1281                         struct drm_file *file_priv);
1282 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1283                         struct drm_file *file_priv);
1284 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1285                        struct drm_file *file_priv);
1286 int drm_gem_handle_create(struct drm_file *file_priv,
1287                           struct drm_gem_object *obj,
1288                           u32 *handlep);
1289 int drm_gem_handle_delete(struct drm_file *file_priv, uint32_t handle);
1290 void drm_gem_object_handle_reference(struct drm_gem_object *obj);
1291 void drm_gem_object_handle_unreference(struct drm_gem_object *obj);
1292 void drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj);
1293 void drm_gem_object_handle_free(struct drm_gem_object *obj);
1294 void drm_gem_object_reference(struct drm_gem_object *obj);
1295 void drm_gem_object_unreference(struct drm_gem_object *obj);
1296 void drm_gem_object_unreference_unlocked(struct drm_gem_object *obj);
1297 void drm_gem_object_release(struct drm_gem_object *obj);
1298 void drm_gem_object_free(struct drm_gem_object *obj);
1299 int drm_gem_object_init(struct drm_device *dev, struct drm_gem_object *obj,
1300     size_t size);
1301 int drm_gem_private_object_init(struct drm_device *dev,
1302     struct drm_gem_object *obj, size_t size);
1303 struct drm_gem_object *drm_gem_object_alloc(struct drm_device *dev,
1304     size_t size);
1305 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1306     struct drm_file *file_priv, uint32_t handle);
1307
1308 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1309 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1310
1311 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1312 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1313 int drm_gem_mmap_single(struct cdev *kdev, vm_ooffset_t *offset, vm_size_t size,
1314     struct vm_object **obj_res, int nprot);
1315 void drm_gem_pager_dtr(void *obj);
1316
1317 void drm_device_lock_mtx(struct drm_device *dev);
1318 void drm_device_unlock_mtx(struct drm_device *dev);
1319 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1320     const char *msg, int timeout);
1321 void drm_device_assert_mtx_locked(struct drm_device *dev);
1322 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1323
1324 void drm_device_lock_struct(struct drm_device *dev);
1325 void drm_device_unlock_struct(struct drm_device *dev);
1326 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1327     const char *msg, int timeout);
1328 void drm_device_assert_struct_locked(struct drm_device *dev);
1329 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1330
1331 void drm_compat_locking_init(struct drm_device *dev);
1332 void drm_sleep_locking_init(struct drm_device *dev);
1333
1334 /* drm_modes.c */
1335 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1336     struct drm_connector *connector, struct drm_cmdline_mode *mode);
1337 struct drm_display_mode *drm_mode_create_from_cmdline_mode(
1338     struct drm_device *dev, struct drm_cmdline_mode *cmd);
1339
1340 /* drm_edid.c */
1341 u8 *drm_find_cea_extension(struct edid *edid);
1342
1343 /* Inline replacements for drm_alloc and friends */
1344 static __inline__ void *
1345 drm_alloc(size_t size, struct malloc_type *area)
1346 {
1347         return malloc(size, area, M_NOWAIT);
1348 }
1349
1350 static __inline__ void *
1351 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1352 {
1353         return malloc(size * nmemb, area, M_NOWAIT | M_ZERO);
1354 }
1355
1356 static __inline__ void *
1357 drm_realloc(void *oldpt, size_t oldsize, size_t size,
1358     struct malloc_type *area)
1359 {
1360         return reallocf(oldpt, size, area, M_NOWAIT);
1361 }
1362
1363 static __inline__ void
1364 drm_free(void *pt, size_t size, struct malloc_type *area)
1365 {
1366         free(pt, area);
1367 }
1368
1369 /* Inline replacements for DRM_IOREMAP macros */
1370 static __inline__ void
1371 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1372 {
1373         map->virtual = drm_ioremap_wc(dev, map);
1374 }
1375 static __inline__ void
1376 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1377 {
1378         map->virtual = drm_ioremap(dev, map);
1379 }
1380 static __inline__ void
1381 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1382 {
1383         if ( map->virtual && map->size )
1384                 drm_ioremapfree(map);
1385 }
1386
1387 static __inline__ struct drm_local_map *
1388 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1389 {
1390         drm_local_map_t *map;
1391
1392         DRM_LOCK_ASSERT(dev);
1393         TAILQ_FOREACH(map, &dev->maplist, link) {
1394                 if (offset == (unsigned long)map->handle)
1395                         return map;
1396         }
1397         return NULL;
1398 }
1399
1400 static __inline__ void drm_core_dropmap(struct drm_map *map)
1401 {
1402 }
1403
1404 #define KIB_NOTYET()                                                    \
1405 do {                                                                    \
1406         if (drm_debug_flag && drm_notyet_flag)                          \
1407                 printf("NOTYET: %s at %s:%d\n", __func__, __FILE__, __LINE__); \
1408 } while (0)
1409
1410 #define KTR_DRM         KTR_DEV
1411 #define KTR_DRM_REG     KTR_SPARE3
1412
1413 /* Error codes conversion from Linux to FreeBSD. */
1414 /* XXXKIB what is the right code for EREMOTEIO on FreeBSD? */
1415 #define EREMOTEIO       ENXIO
1416 #define ERESTARTSYS     ERESTART
1417
1418 #define PCI_VENDOR_ID_APPLE             0x106b
1419 #define PCI_VENDOR_ID_ASUSTEK           0x1043
1420 #define PCI_VENDOR_ID_ATI               0x1002
1421 #define PCI_VENDOR_ID_DELL              0x1028
1422 #define PCI_VENDOR_ID_HP                0x103c
1423 #define PCI_VENDOR_ID_IBM               0x1014
1424 #define PCI_VENDOR_ID_INTEL             0x8086
1425 #define PCI_VENDOR_ID_SERVERWORKS       0x1166
1426 #define PCI_VENDOR_ID_SONY              0x104d
1427 #define PCI_VENDOR_ID_VIA               0x1106
1428
1429 #define DRM_PCIE_SPEED_25 1
1430 #define DRM_PCIE_SPEED_50 2
1431 #define DRM_PCIE_SPEED_80 4
1432
1433 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1434
1435 #endif /* __KERNEL__ */
1436 #endif /* _DRM_P_H_ */