OSDN Git Service

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