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