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