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