kernel - Remove kevent subsystem from under mplock
[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  */
33
34 #ifndef _DRM_P_H_
35 #define _DRM_P_H_
36
37 #if defined(_KERNEL) || defined(__KERNEL__)
38
39 struct drm_device;
40 struct drm_file;
41
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
47 #include <sys/systm.h>
48 #include <sys/conf.h>
49 #include <sys/stat.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/lock.h>
53 #include <sys/fcntl.h>
54 #include <sys/uio.h>
55 #include <sys/filio.h>
56 #include <sys/sysctl.h>
57 #include <sys/bus.h>
58 #include <sys/signalvar.h>
59 #include <sys/event.h>
60 #include <sys/tree.h>
61 #include <sys/taskqueue.h>
62 #include <vm/vm.h>
63 #include <vm/pmap.h>
64 #include <vm/vm_extern.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_param.h>
67 #include <machine/param.h>
68 #include <machine/pmap.h>
69 #include <sys/bus.h>
70 #include <sys/resource.h>
71 #include <machine/specialreg.h>
72 #include <machine/sysarch.h>
73 #include <sys/endian.h>
74 #include <sys/mman.h>
75 #include <sys/rman.h>
76 #include <sys/memrange.h>
77 #include <dev/agp/agpvar.h>
78 #include <sys/device.h>
79 #include <sys/agpio.h>
80 #include <sys/spinlock.h>
81 #include <sys/spinlock2.h>
82 #include <bus/pci/pcivar.h>
83 #include <bus/pci/pcireg.h>
84 #include <sys/bus.h>
85
86 #include "dev/drm/drm.h"
87 #include "dev/drm/drm_linux_list.h"
88 #include "dev/drm/drm_atomic.h"
89 #include "dev/drm/drm_internal.h"
90
91 #include <opt_drm.h>
92 #ifdef DRM_DEBUG
93 #undef DRM_DEBUG
94 #define DRM_DEBUG_DEFAULT_ON 1
95 #endif /* DRM_DEBUG */
96
97 #if defined(DRM_LINUX) && DRM_LINUX && !defined(__x86_64__) && !defined(__DragonFly__) /* XXX */
98 #include <sys/file.h>
99 #include <sys/proc.h>
100 #include <machine/../linux/linux.h>
101 #include <machine/../linux/linux_proto.h>
102 #else
103 /* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
104  * supported on this OS yet.
105  */
106 #undef DRM_LINUX
107 #define DRM_LINUX 0
108 #endif
109
110 /* driver capabilities and requirements mask */
111 #define DRIVER_USE_AGP     0x1
112 #define DRIVER_REQUIRE_AGP 0x2
113 #define DRIVER_USE_MTRR    0x4
114 #define DRIVER_PCI_DMA     0x8
115 #define DRIVER_SG          0x10
116 #define DRIVER_HAVE_DMA    0x20
117 #define DRIVER_HAVE_IRQ    0x40
118 #define DRIVER_DMA_QUEUE   0x100
119
120
121 #define DRM_HASH_SIZE         16 /* Size of key hash table                */
122 #define DRM_KERNEL_CONTEXT    0  /* Change drm_resctx if changed          */
123 #define DRM_RESERVED_CONTEXTS 1  /* Change drm_resctx if changed          */
124
125 MALLOC_DECLARE(DRM_MEM_DMA);
126 MALLOC_DECLARE(DRM_MEM_SAREA);
127 MALLOC_DECLARE(DRM_MEM_DRIVER);
128 MALLOC_DECLARE(DRM_MEM_MAGIC);
129 MALLOC_DECLARE(DRM_MEM_IOCTLS);
130 MALLOC_DECLARE(DRM_MEM_MAPS);
131 MALLOC_DECLARE(DRM_MEM_BUFS);
132 MALLOC_DECLARE(DRM_MEM_SEGS);
133 MALLOC_DECLARE(DRM_MEM_PAGES);
134 MALLOC_DECLARE(DRM_MEM_FILES);
135 MALLOC_DECLARE(DRM_MEM_QUEUES);
136 MALLOC_DECLARE(DRM_MEM_CMDS);
137 MALLOC_DECLARE(DRM_MEM_MAPPINGS);
138 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
139 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
140 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
141 MALLOC_DECLARE(DRM_MEM_SGLISTS);
142 MALLOC_DECLARE(DRM_MEM_DRAWABLE);
143
144 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
145
146                                 /* Internal types and structures */
147 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
148 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
149 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
150
151 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
152
153 #define __OS_HAS_AGP    1
154
155 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
156 #define DRM_DEV_UID     0
157 #define DRM_DEV_GID     0
158
159 #define wait_queue_head_t       atomic_t
160 #define DRM_WAKEUP(w)           wakeup((void *)w)
161 #define DRM_WAKEUP_INT(w)       wakeup(w)
162 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
163
164 #define DRM_CURPROC             curthread
165 #define DRM_STRUCTPROC          struct thread
166 #define DRM_SPINTYPE            struct lock
167 #define DRM_SPININIT(l,name)    lockinit(l, name, 0, LK_CANRECURSE)
168 #define DRM_SPINUNINIT(l)
169 #define DRM_SPINLOCK(l)         lockmgr(l, LK_EXCLUSIVE|LK_RETRY|LK_CANRECURSE)
170 #define DRM_SPINUNLOCK(u)       lockmgr(u, LK_RELEASE)
171 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {          \
172         DRM_SPINLOCK(l);                                \
173         (void)irqflags;                                 \
174 } while (0)
175 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) DRM_SPINUNLOCK(u)
176 #define DRM_SPINLOCK_ASSERT(l)
177 #define DRM_CURRENTPID          curthread->td_proc->p_pid
178 #define DRM_LOCK()              DRM_SPINLOCK(&dev->dev_lock)
179 #define DRM_UNLOCK()            DRM_SPINUNLOCK(&dev->dev_lock)
180 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
181
182 #define DRM_IRQ_ARGS            void *arg
183 typedef void                    irqreturn_t;
184 #define IRQ_HANDLED             /* nothing */
185 #define IRQ_NONE                /* nothing */
186
187 enum {
188         DRM_IS_NOT_AGP,
189         DRM_IS_AGP,
190         DRM_MIGHT_BE_AGP
191 };
192 #define DRM_AGP_MEM             struct agp_memory_info
193
194 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
195
196 #define PAGE_ALIGN(addr) round_page(addr)
197 /* DRM_SUSER returns true if the user is superuser */
198 #define DRM_SUSER(p)            (priv_check(p, PRIV_DRIVER) == 0)
199 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
200 #define DRM_MTRR_WC             MDF_WRITECOMBINE
201 #define jiffies                 ticks
202
203 typedef unsigned long dma_addr_t;
204 typedef u_int64_t u64;
205 typedef u_int32_t u32;
206 typedef u_int16_t u16;
207 typedef u_int8_t u8;
208
209 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
210  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
211  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
212  */
213 #if defined(__i386__)
214 #define DRM_READMEMORYBARRIER()         __asm __volatile( \
215                                         "lock; addl $0,0(%%esp)" : : : "memory");
216 #define DRM_WRITEMEMORYBARRIER()        __asm __volatile("" : : : "memory");
217 #define DRM_MEMORYBARRIER()             __asm __volatile( \
218                                         "lock; addl $0,0(%%esp)" : : : "memory");
219 #elif defined(__alpha__)
220 #define DRM_READMEMORYBARRIER()         alpha_mb();
221 #define DRM_WRITEMEMORYBARRIER()        alpha_wmb();
222 #define DRM_MEMORYBARRIER()             alpha_mb();
223 #elif defined(__x86_64__)
224 #define DRM_READMEMORYBARRIER()         __asm __volatile( \
225                                         "lock; addl $0,0(%%rsp)" : : : "memory");
226 #define DRM_WRITEMEMORYBARRIER()        __asm __volatile("" : : : "memory");
227 #define DRM_MEMORYBARRIER()             __asm __volatile( \
228                                         "lock; addl $0,0(%%rsp)" : : : "memory");
229 #endif
230
231 #define DRM_READ8(map, offset)                                          \
232         *(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +           \
233             (vm_offset_t)(offset))
234 #define DRM_READ16(map, offset)                                         \
235         *(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +          \
236             (vm_offset_t)(offset))
237 #define DRM_READ32(map, offset)                                         \
238         *(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +          \
239             (vm_offset_t)(offset))
240 #define DRM_WRITE8(map, offset, val)                                    \
241         *(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +           \
242             (vm_offset_t)(offset)) = val
243 #define DRM_WRITE16(map, offset, val)                                   \
244         *(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +          \
245             (vm_offset_t)(offset)) = val
246 #define DRM_WRITE32(map, offset, val)                                   \
247         *(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +          \
248             (vm_offset_t)(offset)) = val
249
250 #define DRM_VERIFYAREA_READ( uaddr, size )              \
251         (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
252
253 #define DRM_COPY_TO_USER(user, kern, size) \
254         copyout(kern, user, size)
255 #define DRM_COPY_FROM_USER(kern, user, size) \
256         copyin(user, kern, size)
257 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
258         copyin(arg2, arg1, arg3)
259 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
260         copyout(arg2, arg1, arg3)
261 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
262         ((val) = fuword32(uaddr), 0)
263
264 #define cpu_to_le32(x) htole32(x)
265 #define le32_to_cpu(x) le32toh(x)
266
267 #define DRM_HZ                  hz
268 #define DRM_UDELAY(udelay)      DELAY(udelay)
269 #define DRM_TIME_SLICE          (hz/20)  /* Time slice for GLXContexts    */
270
271 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
272         (_map) = (_dev)->context_sareas[_ctx];          \
273 } while(0)
274
275 #define LOCK_TEST_WITH_RETURN(dev, file_priv)                           \
276 do {                                                                    \
277         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
278              dev->lock.file_priv != file_priv) {                        \
279                 DRM_ERROR("%s called without lock held\n",              \
280                            __FUNCTION__);                               \
281                 return EINVAL;                                          \
282         }                                                               \
283 } while (0)
284
285 /* Returns -errno to shared code */
286 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
287 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
288         DRM_UNLOCK();                                           \
289         lwkt_serialize_enter(&dev->irq_lock);                   \
290         if (!(condition)) {                                     \
291             tsleep_interlock(&(queue), PCATCH);                 \
292             lwkt_serialize_exit(&dev->irq_lock);                \
293             ret = -tsleep(&(queue), PCATCH | PINTERLOCKED,      \
294                           "drmwtq", (timeout));                 \
295         } else {                                                \
296                 lwkt_serialize_exit(&dev->irq_lock);            \
297         }                                                       \
298         DRM_LOCK();                                             \
299 }
300
301 #define printf  kprintf
302 #define snprintf ksnprintf
303 #define sscanf  ksscanf
304 #define malloc  kmalloc
305 #define realloc krealloc
306 #define reallocf krealloc       /* XXX XXX XXX */
307
308 __inline static void
309 free(void *addr, struct malloc_type *type)
310 {
311         if (addr != NULL)
312                 kfree(addr, type);
313 }
314
315 #define DRM_ERROR(fmt, ...) \
316         printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,           \
317             DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
318
319 #define DRM_INFO(fmt, ...)  printf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
320         
321 #define DRM_DEBUG(fmt, ...) do {                                        \
322         if (drm_debug_flag)                                             \
323                 printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,  \
324                         __func__ , ##__VA_ARGS__);                      \
325 } while (0)
326
327 typedef struct drm_pci_id_list
328 {
329         int vendor;
330         int device;
331         long driver_private;
332         char *name;
333 } drm_pci_id_list_t;
334
335 struct drm_msi_blacklist_entry
336 {
337         int vendor;
338         int device;
339 };
340
341 #define DRM_AUTH        0x1
342 #define DRM_MASTER      0x2
343 #define DRM_ROOT_ONLY   0x4
344 typedef struct drm_ioctl_desc {
345         unsigned long cmd;
346         int (*func)(struct drm_device *dev, void *data,
347                     struct drm_file *file_priv);
348         int flags;
349 } drm_ioctl_desc_t;
350 /**
351  * Creates a driver or general drm_ioctl_desc array entry for the given
352  * ioctl, for use by drm_ioctl().
353  */
354 #define DRM_IOCTL_DEF(ioctl, func, flags) \
355         [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
356
357 typedef struct drm_magic_entry {
358         drm_magic_t            magic;
359         struct drm_file        *priv;
360         struct drm_magic_entry *next;
361 } drm_magic_entry_t;
362
363 typedef struct drm_magic_head {
364         struct drm_magic_entry *head;
365         struct drm_magic_entry *tail;
366 } drm_magic_head_t;
367
368 typedef struct drm_buf {
369         int               idx;         /* Index into master buflist          */
370         int               total;       /* Buffer size                        */
371         int               order;       /* log-base-2(total)                  */
372         int               used;        /* Amount of buffer in use (for DMA)  */
373         unsigned long     offset;      /* Byte offset (used internally)      */
374         void              *address;    /* Address of buffer                  */
375         unsigned long     bus_address; /* Bus address of buffer              */
376         struct drm_buf    *next;       /* Kernel-only: used for free list    */
377         __volatile__ int  pending;     /* On hardware DMA queue              */
378         struct drm_file   *file_priv;  /* Unique identifier of holding process */
379         int               context;     /* Kernel queue for this buffer       */
380         enum {
381                 DRM_LIST_NONE    = 0,
382                 DRM_LIST_FREE    = 1,
383                 DRM_LIST_WAIT    = 2,
384                 DRM_LIST_PEND    = 3,
385                 DRM_LIST_PRIO    = 4,
386                 DRM_LIST_RECLAIM = 5
387         }                 list;        /* Which list we're on                */
388
389         int               dev_priv_size; /* Size of buffer private stoarge   */
390         void              *dev_private;  /* Per-buffer private storage       */
391 } drm_buf_t;
392
393 typedef struct drm_freelist {
394         int               initialized; /* Freelist in use                  */
395         atomic_t          count;       /* Number of free buffers           */
396         drm_buf_t         *next;       /* End pointer                      */
397
398         int               low_mark;    /* Low water mark                   */
399         int               high_mark;   /* High water mark                  */
400 } drm_freelist_t;
401
402 typedef struct drm_dma_handle {
403         void *vaddr;
404         bus_addr_t busaddr;
405         bus_dma_tag_t tag;
406         bus_dmamap_t map;
407 } drm_dma_handle_t;
408
409 typedef struct drm_buf_entry {
410         int               buf_size;
411         int               buf_count;
412         drm_buf_t         *buflist;
413         int               seg_count;
414         drm_dma_handle_t  **seglist;
415         int               page_order;
416
417         drm_freelist_t    freelist;
418 } drm_buf_entry_t;
419
420 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
421 struct drm_file {
422         TAILQ_ENTRY(drm_file) link;
423         struct drm_device *dev;
424         int               authenticated;
425         int               master;
426         int               minor;
427         pid_t             pid;
428         uid_t             uid;
429         int               refs;
430         drm_magic_t       magic;
431         unsigned long     ioctl_count;
432         void             *driver_priv;
433 };
434
435 typedef struct drm_lock_data {
436         struct drm_hw_lock      *hw_lock;       /* Hardware lock                   */
437         struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
438         int               lock_queue;   /* Queue of blocked processes      */
439         unsigned long     lock_time;    /* Time of last lock in jiffies    */
440 } drm_lock_data_t;
441
442 /* This structure, in the struct drm_device, is always initialized while the
443  * device
444  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
445  * when set marks that no further bufs may be allocated until device teardown
446  * occurs (when the last open of the device has closed).  The high/low
447  * watermarks of bufs are only touched by the X Server, and thus not
448  * concurrently accessed, so no locking is needed.
449  */
450 typedef struct drm_device_dma {
451         drm_buf_entry_t   bufs[DRM_MAX_ORDER+1];
452         int               buf_count;
453         drm_buf_t         **buflist;    /* Vector of pointers info bufs    */
454         int               seg_count;
455         int               page_count;
456         unsigned long     *pagelist;
457         unsigned long     byte_count;
458         enum {
459                 _DRM_DMA_USE_AGP = 0x01,
460                 _DRM_DMA_USE_SG  = 0x02
461         } flags;
462 } drm_device_dma_t;
463
464 typedef struct drm_agp_mem {
465         void               *handle;
466         unsigned long      bound; /* address */
467         int                pages;
468         struct drm_agp_mem *prev;
469         struct drm_agp_mem *next;
470 } drm_agp_mem_t;
471
472 typedef struct drm_agp_head {
473         device_t           agpdev;
474         struct agp_info    info;
475         const char         *chipset;
476         drm_agp_mem_t      *memory;
477         unsigned long      mode;
478         int                enabled;
479         int                acquired;
480         unsigned long      base;
481         int                mtrr;
482         int                cant_use_aperture;
483         unsigned long      page_mask;
484 } drm_agp_head_t;
485
486 typedef struct drm_sg_mem {
487         unsigned long             handle;
488         void                     *virtual;
489         int                       pages;
490         dma_addr_t               *busaddr;
491         struct drm_dma_handle    *dmah;         /* Handle to PCI memory  */
492 } drm_sg_mem_t;
493
494 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
495
496 typedef struct drm_local_map {
497         unsigned long   offset;  /* Physical address (0 for SAREA)*/
498         unsigned long   size;    /* Physical size (bytes)           */
499         enum drm_map_type       type;    /* Type of memory mapped                   */
500         enum drm_map_flags      flags;   /* Flags                                   */
501         void            *handle; /* User-space: "Handle" to pass to mmap    */
502                                  /* Kernel-space: kernel-virtual address    */
503         int             mtrr;    /* Boolean: MTRR used */
504                                  /* Private data                            */
505         int             rid;     /* PCI resource ID for bus_space */
506         struct resource *bsr;
507         bus_space_tag_t bst;
508         bus_space_handle_t bsh;
509         drm_dma_handle_t *dmah;
510         TAILQ_ENTRY(drm_local_map) link;
511 } drm_local_map_t;
512
513 struct drm_vblank_info {
514         wait_queue_head_t queue;        /* vblank wait queue */
515         atomic_t count;                 /* number of VBLANK interrupts */
516                                         /* (driver must alloc the right number of counters) */
517         atomic_t refcount;              /* number of users of vblank interrupts */
518         u32 last;                       /* protected by dev->vbl_lock, used */
519                                         /* for wraparound handling */
520         int enabled;                    /* so we don't call enable more than */
521                                         /* once per disable */
522         int inmodeset;                  /* Display driver is setting mode */
523 };
524
525 /* location of GART table */
526 #define DRM_ATI_GART_MAIN 1
527 #define DRM_ATI_GART_FB   2
528
529 #define DRM_ATI_GART_PCI  1
530 #define DRM_ATI_GART_PCIE 2
531 #define DRM_ATI_GART_IGP  3
532
533 struct drm_ati_pcigart_info {
534         int gart_table_location;
535         int gart_reg_if;
536         void *addr;
537         dma_addr_t bus_addr;
538         dma_addr_t table_mask;
539         dma_addr_t member_mask;
540         struct drm_dma_handle *table_handle;
541         drm_local_map_t mapping;
542         int table_size;
543         struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
544 };
545
546 #ifndef DMA_BIT_MASK
547 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
548 #endif
549
550 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
551
552 struct drm_driver_info {
553         int     (*load)(struct drm_device *, unsigned long flags);
554         int     (*firstopen)(struct drm_device *);
555         int     (*open)(struct drm_device *, struct drm_file *);
556         void    (*preclose)(struct drm_device *, struct drm_file *file_priv);
557         void    (*postclose)(struct drm_device *, struct drm_file *);
558         void    (*lastclose)(struct drm_device *);
559         int     (*unload)(struct drm_device *);
560         void    (*reclaim_buffers_locked)(struct drm_device *,
561                                           struct drm_file *file_priv);
562         int     (*dma_ioctl)(struct drm_device *dev, void *data,
563                              struct drm_file *file_priv);
564         void    (*dma_ready)(struct drm_device *);
565         int     (*dma_quiescent)(struct drm_device *);
566         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
567                                              enum drm_lock_flags flags);
568         int     (*dma_flush_unblock)(struct drm_device *, int context,
569                                      enum drm_lock_flags flags);
570         int     (*context_ctor)(struct drm_device *dev, int context);
571         int     (*context_dtor)(struct drm_device *dev, int context);
572         int     (*kernel_context_switch)(struct drm_device *dev, int old,
573                                          int new);
574         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
575         void    (*irq_preinstall)(struct drm_device *dev);
576         int     (*irq_postinstall)(struct drm_device *dev);
577         void    (*irq_uninstall)(struct drm_device *dev);
578         void    (*irq_handler)(DRM_IRQ_ARGS);
579         u32     (*get_vblank_counter)(struct drm_device *dev, int crtc);
580         int     (*enable_vblank)(struct drm_device *dev, int crtc);
581         void    (*disable_vblank)(struct drm_device *dev, int crtc);
582
583         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
584
585         /**
586          * Called by \c drm_device_is_agp.  Typically used to determine if a
587          * card is really attached to AGP or not.
588          *
589          * \param dev  DRM device handle
590          *
591          * \returns 
592          * One of three values is returned depending on whether or not the
593          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
594          * (return of 1), or may or may not be AGP (return of 2).
595          */
596         int     (*device_is_agp) (struct drm_device * dev);
597
598         drm_ioctl_desc_t *ioctls;
599         int     max_ioctl;
600
601         int     buf_priv_size;
602
603         int     major;
604         int     minor;
605         int     patchlevel;
606         const char *name;               /* Simple driver name              */
607         const char *desc;               /* Longer driver name              */
608         const char *date;               /* Date of last major changes.     */
609
610         u32 driver_features;
611 };
612
613 /* Length for the array of resource pointers for drm_get_resource_*. */
614 #define DRM_MAX_PCI_RESOURCE    6
615
616 /** 
617  * DRM device functions structure
618  */
619 struct drm_device {
620         struct drm_driver_info *driver;
621         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
622
623         u_int16_t pci_device;           /* PCI device id */
624         u_int16_t pci_vendor;           /* PCI vendor id */
625
626         char              *unique;      /* Unique identifier: e.g., busid  */
627         int               unique_len;   /* Length of unique field          */
628         device_t          device;       /* Device instance from newbus     */
629         struct cdev       *devnode;     /* Device number for mknod         */
630         int               if_version;   /* Highest interface version set */
631
632         int               flags;        /* Flags to open(2)                */
633
634                                 /* Locks */
635         DRM_SPINTYPE      vbl_lock;     /* protects vblank operations */
636         DRM_SPINTYPE      dma_lock;     /* protects dev->dma */
637         struct lwkt_serialize irq_lock; /* protects irq condition checks */
638         DRM_SPINTYPE      dev_lock;     /* protects everything else */
639         DRM_SPINTYPE      drw_lock;
640
641                                 /* Usage Counters */
642         int               open_count;   /* Outstanding files open          */
643         int               buf_use;      /* Buffers in use -- cannot alloc  */
644
645                                 /* Performance counters */
646         unsigned long     counters;
647         enum drm_stat_type      types[15];
648         atomic_t          counts[15];
649
650                                 /* Authentication */
651         drm_file_list_t   files;
652         drm_magic_head_t  magiclist[DRM_HASH_SIZE];
653
654         /* Linked list of mappable regions. Protected by dev_lock */
655         drm_map_list_t    maplist;
656
657         drm_local_map_t   **context_sareas;
658         int               max_context;
659
660         drm_lock_data_t   lock;         /* Information on hardware lock    */
661
662                                 /* DMA queues (contexts) */
663         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
664
665                                 /* Context support */
666         int               irq;          /* Interrupt used by board         */
667         int               irq_enabled;  /* True if the irq handler is enabled */
668         int               msi_enabled;  /* MSI enabled */
669         int               irqrid;       /* Interrupt used by board */
670         struct resource   *irqr;        /* Resource for interrupt used by board    */
671         void              *irqh;        /* Handle from bus_setup_intr      */
672
673         /* Storage of resource pointers for drm_get_resource_* */
674         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
675         int               pcirid[DRM_MAX_PCI_RESOURCE];
676
677         int               pci_domain;
678         int               pci_bus;
679         int               pci_slot;
680         int               pci_func;
681
682         atomic_t          context_flag; /* Context swapping flag           */
683         int               last_context; /* Last current context            */
684
685         int               vblank_disable_allowed;
686         struct callout    vblank_disable_timer;
687         u32               max_vblank_count;     /* size of vblank counter register */
688         struct drm_vblank_info *vblank;         /* per crtc vblank info */
689         int               num_crtcs;
690
691         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
692
693                                 /* Sysctl support */
694         struct drm_sysctl_info *sysctl;
695
696         drm_agp_head_t    *agp;
697         drm_sg_mem_t      *sg;  /* Scatter gather memory */
698         atomic_t          *ctx_bitmap;
699         void              *dev_private;
700         unsigned int      agp_buffer_token;
701         drm_local_map_t   *agp_buffer_map;
702
703         /* RB tree of drawable infos */
704         RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
705 };
706
707 static __inline__ int drm_core_check_feature(struct drm_device *dev,
708                                              int feature)
709 {
710         return ((dev->driver->driver_features & feature) ? 1 : 0);
711 }
712
713 #if __OS_HAS_AGP
714 static inline int drm_core_has_AGP(struct drm_device *dev)
715 {
716         return drm_core_check_feature(dev, DRIVER_USE_AGP);
717 }
718 #else
719 #define drm_core_has_AGP(dev) (0)
720 #endif
721
722 extern int      drm_debug_flag;
723
724 /* Device setup support (drm_drv.c) */
725 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
726 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
727 int     drm_detach(device_t kdev);
728 d_ioctl_t drm_ioctl;
729 d_open_t drm_open;
730 d_close_t drm_close;
731 d_read_t drm_read;
732 d_kqfilter_t drm_kqfilter;
733 d_mmap_t drm_mmap;
734 extern drm_local_map_t  *drm_getsarea(struct drm_device *dev);
735
736 /* File operations helpers (drm_fops.c) */
737 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
738                                          DRM_STRUCTPROC *p,
739                                         struct drm_device *dev);
740 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
741                                         DRM_STRUCTPROC *p);
742
743 /* Memory management support (drm_memory.c) */
744 void    drm_mem_init(void);
745 void    drm_mem_uninit(void);
746 void    *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
747 void    *drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
748 void    drm_ioremapfree(drm_local_map_t *map);
749 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
750 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
751
752 int     drm_context_switch(struct drm_device *dev, int old, int new);
753 int     drm_context_switch_complete(struct drm_device *dev, int new);
754
755 int     drm_ctxbitmap_init(struct drm_device *dev);
756 void    drm_ctxbitmap_cleanup(struct drm_device *dev);
757 void    drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
758 int     drm_ctxbitmap_next(struct drm_device *dev);
759
760 /* Locking IOCTL support (drm_lock.c) */
761 int     drm_lock_take(struct drm_lock_data *lock_data,
762                       unsigned int context);
763 int     drm_lock_transfer(struct drm_lock_data *lock_data,
764                           unsigned int context);
765 int     drm_lock_free(struct drm_lock_data *lock_data,
766                       unsigned int context);
767
768 /* Buffer management support (drm_bufs.c) */
769 unsigned long drm_get_resource_start(struct drm_device *dev,
770                                      unsigned int resource);
771 unsigned long drm_get_resource_len(struct drm_device *dev,
772                                    unsigned int resource);
773 void    drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
774 int     drm_order(unsigned long size);
775 int     drm_addmap(struct drm_device *dev, unsigned long offset,
776                    unsigned long size,
777                    enum drm_map_type type, enum drm_map_flags flags,
778                    drm_local_map_t **map_ptr);
779 int     drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
780 int     drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request);
781 int     drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
782
783 /* DMA support (drm_dma.c) */
784 int     drm_dma_setup(struct drm_device *dev);
785 void    drm_dma_takedown(struct drm_device *dev);
786 void    drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
787 void    drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
788 #define drm_core_reclaim_buffers drm_reclaim_buffers
789
790 /* IRQ support (drm_irq.c) */
791 int     drm_irq_install(struct drm_device *dev);
792 int     drm_irq_uninstall(struct drm_device *dev);
793 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
794 void    drm_driver_irq_preinstall(struct drm_device *dev);
795 void    drm_driver_irq_postinstall(struct drm_device *dev);
796 void    drm_driver_irq_uninstall(struct drm_device *dev);
797 void    drm_handle_vblank(struct drm_device *dev, int crtc);
798 u32     drm_vblank_count(struct drm_device *dev, int crtc);
799 int     drm_vblank_get(struct drm_device *dev, int crtc);
800 void    drm_vblank_put(struct drm_device *dev, int crtc);
801 void    drm_vblank_cleanup(struct drm_device *dev);
802 int     drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
803 int     drm_vblank_init(struct drm_device *dev, int num_crtcs);
804 int     drm_modeset_ctl(struct drm_device *dev, void *data,
805                         struct drm_file *file_priv);
806
807 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
808 int     drm_device_is_agp(struct drm_device *dev);
809 int     drm_device_is_pcie(struct drm_device *dev);
810 drm_agp_head_t *drm_agp_init(void);
811 int     drm_agp_acquire(struct drm_device *dev);
812 int     drm_agp_release(struct drm_device *dev);
813 int     drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
814 int     drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
815 void    *drm_agp_allocate_memory(size_t pages, u32 type);
816 int     drm_agp_free_memory(void *handle);
817 int     drm_agp_bind_memory(void *handle, off_t start);
818 int     drm_agp_unbind_memory(void *handle);
819 int     drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
820 int     drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
821 int     drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
822 int     drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
823
824 /* Scatter Gather Support (drm_scatter.c) */
825 void    drm_sg_cleanup(drm_sg_mem_t *entry);
826 int     drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
827
828 /* sysctl support (drm_sysctl.h) */
829 extern int              drm_sysctl_init(struct drm_device *dev);
830 extern int              drm_sysctl_cleanup(struct drm_device *dev);
831
832 /* ATI PCIGART support (ati_pcigart.c) */
833 int     drm_ati_pcigart_init(struct drm_device *dev,
834                                 struct drm_ati_pcigart_info *gart_info);
835 int     drm_ati_pcigart_cleanup(struct drm_device *dev,
836                                 struct drm_ati_pcigart_info *gart_info);
837
838 /* Locking IOCTL support (drm_drv.c) */
839 int     drm_lock(struct drm_device *dev, void *data,
840                  struct drm_file *file_priv);
841 int     drm_unlock(struct drm_device *dev, void *data,
842                    struct drm_file *file_priv);
843 int     drm_version(struct drm_device *dev, void *data,
844                     struct drm_file *file_priv);
845 int     drm_setversion(struct drm_device *dev, void *data,
846                        struct drm_file *file_priv);
847
848 /* Misc. IOCTL support (drm_ioctl.c) */
849 int     drm_irq_by_busid(struct drm_device *dev, void *data,
850                          struct drm_file *file_priv);
851 int     drm_getunique(struct drm_device *dev, void *data,
852                       struct drm_file *file_priv);
853 int     drm_setunique(struct drm_device *dev, void *data,
854                       struct drm_file *file_priv);
855 int     drm_getmap(struct drm_device *dev, void *data,
856                    struct drm_file *file_priv);
857 int     drm_getclient(struct drm_device *dev, void *data,
858                       struct drm_file *file_priv);
859 int     drm_getstats(struct drm_device *dev, void *data,
860                      struct drm_file *file_priv);
861 int     drm_noop(struct drm_device *dev, void *data,
862                  struct drm_file *file_priv);
863
864 /* Context IOCTL support (drm_context.c) */
865 int     drm_resctx(struct drm_device *dev, void *data,
866                    struct drm_file *file_priv);
867 int     drm_addctx(struct drm_device *dev, void *data,
868                    struct drm_file *file_priv);
869 int     drm_modctx(struct drm_device *dev, void *data,
870                    struct drm_file *file_priv);
871 int     drm_getctx(struct drm_device *dev, void *data,
872                    struct drm_file *file_priv);
873 int     drm_switchctx(struct drm_device *dev, void *data,
874                       struct drm_file *file_priv);
875 int     drm_newctx(struct drm_device *dev, void *data,
876                    struct drm_file *file_priv);
877 int     drm_rmctx(struct drm_device *dev, void *data,
878                   struct drm_file *file_priv);
879 int     drm_setsareactx(struct drm_device *dev, void *data,
880                         struct drm_file *file_priv);
881 int     drm_getsareactx(struct drm_device *dev, void *data,
882                         struct drm_file *file_priv);
883
884 /* Drawable IOCTL support (drm_drawable.c) */
885 int     drm_adddraw(struct drm_device *dev, void *data,
886                     struct drm_file *file_priv);
887 int     drm_rmdraw(struct drm_device *dev, void *data,
888                    struct drm_file *file_priv);
889 int     drm_update_draw(struct drm_device *dev, void *data,
890                         struct drm_file *file_priv);
891 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
892                                                 int handle);
893
894 /* Drawable support (drm_drawable.c) */
895 void drm_drawable_free_all(struct drm_device *dev);
896
897 /* Authentication IOCTL support (drm_auth.c) */
898 int     drm_getmagic(struct drm_device *dev, void *data,
899                      struct drm_file *file_priv);
900 int     drm_authmagic(struct drm_device *dev, void *data,
901                       struct drm_file *file_priv);
902
903 /* Buffer management support (drm_bufs.c) */
904 int     drm_addmap_ioctl(struct drm_device *dev, void *data,
905                          struct drm_file *file_priv);
906 int     drm_rmmap_ioctl(struct drm_device *dev, void *data,
907                         struct drm_file *file_priv);
908 int     drm_addbufs(struct drm_device *dev, void *data,
909                     struct drm_file *file_priv);
910 int     drm_infobufs(struct drm_device *dev, void *data,
911                      struct drm_file *file_priv);
912 int     drm_markbufs(struct drm_device *dev, void *data,
913                      struct drm_file *file_priv);
914 int     drm_freebufs(struct drm_device *dev, void *data,
915                      struct drm_file *file_priv);
916 int     drm_mapbufs(struct drm_device *dev, void *data,
917                     struct drm_file *file_priv);
918
919 /* DMA support (drm_dma.c) */
920 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
921
922 /* IRQ support (drm_irq.c) */
923 int     drm_control(struct drm_device *dev, void *data,
924                     struct drm_file *file_priv);
925 int     drm_wait_vblank(struct drm_device *dev, void *data,
926                         struct drm_file *file_priv);
927
928 /* AGP/GART support (drm_agpsupport.c) */
929 int     drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
930                               struct drm_file *file_priv);
931 int     drm_agp_release_ioctl(struct drm_device *dev, void *data,
932                               struct drm_file *file_priv);
933 int     drm_agp_enable_ioctl(struct drm_device *dev, void *data,
934                              struct drm_file *file_priv);
935 int     drm_agp_info_ioctl(struct drm_device *dev, void *data,
936                            struct drm_file *file_priv);
937 int     drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
938                             struct drm_file *file_priv);
939 int     drm_agp_free_ioctl(struct drm_device *dev, void *data,
940                            struct drm_file *file_priv);
941 int     drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
942                              struct drm_file *file_priv);
943 int     drm_agp_bind_ioctl(struct drm_device *dev, void *data,
944                            struct drm_file *file_priv);
945
946 /* Scatter Gather Support (drm_scatter.c) */
947 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
948                            struct drm_file *file_priv);
949 int     drm_sg_free(struct drm_device *dev, void *data,
950                     struct drm_file *file_priv);
951
952 /* consistent PCI memory functions (drm_pci.c) */
953 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
954                                 size_t align, dma_addr_t maxaddr);
955 void    drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
956
957 /* Inline replacements for drm_alloc and friends */
958 static __inline__ void *
959 drm_alloc(size_t size, struct malloc_type *area)
960 {
961         return malloc(size, area, M_NOWAIT);
962 }
963
964 static __inline__ void *
965 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
966 {
967         return malloc(size * nmemb, area, M_NOWAIT | M_ZERO);
968 }
969
970 static __inline__ void *
971 drm_realloc(void *oldpt, size_t oldsize, size_t size,
972     struct malloc_type *area)
973 {
974         return reallocf(oldpt, size, area, M_NOWAIT);
975 }
976
977 static __inline__ void
978 drm_free(void *pt, size_t size, struct malloc_type *area)
979 {
980         free(pt, area);
981 }
982
983 /* Inline replacements for DRM_IOREMAP macros */
984 static __inline__ void
985 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
986 {
987         map->handle = drm_ioremap_wc(dev, map);
988 }
989 static __inline__ void
990 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
991 {
992         map->handle = drm_ioremap(dev, map);
993 }
994 static __inline__ void
995 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
996 {
997         if ( map->handle && map->size )
998                 drm_ioremapfree(map);
999 }
1000
1001 static __inline__ struct drm_local_map *
1002 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1003 {
1004         drm_local_map_t *map;
1005
1006         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
1007         TAILQ_FOREACH(map, &dev->maplist, link) {
1008                 if (map->offset == offset)
1009                         return map;
1010         }
1011         return NULL;
1012 }
1013
1014 static __inline__ void drm_core_dropmap(struct drm_map *map)
1015 {
1016 }
1017
1018 #endif /* __KERNEL__ */
1019 #endif /* _DRM_P_H_ */