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