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