OSDN Git Service

Use fuword32 for DRM_GET_USER_UNCHECKED when available. May help on 64-bit
[android-x86/external-libdrm.git] / bsd-core / drmP.h
1 /* drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
2  * Created: Mon Jan  4 10:05:05 1999 by faith@precisioninsight.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Gareth Hughes <gareth@valinux.com>
30  *
31  */
32
33 #ifndef _DRM_P_H_
34 #define _DRM_P_H_
35
36 #if defined(_KERNEL) || defined(__KERNEL__)
37
38 typedef struct drm_device drm_device_t;
39 typedef struct drm_file drm_file_t;
40
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/stat.h>
49 #include <sys/proc.h>
50 #include <sys/lock.h>
51 #include <sys/fcntl.h>
52 #include <sys/uio.h>
53 #include <sys/filio.h>
54 #include <sys/sysctl.h>
55 #include <sys/bus.h>
56 #include <sys/signalvar.h>
57 #include <sys/poll.h>
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_param.h>
63 #include <machine/param.h>
64 #include <machine/pmap.h>
65 #include <machine/bus.h>
66 #include <machine/resource.h>
67 #include <machine/sysarch.h>
68 #include <sys/endian.h>
69 #include <sys/mman.h>
70 #if defined(__FreeBSD__)
71 #include <sys/rman.h>
72 #include <sys/memrange.h>
73 #include <pci/agpvar.h>
74 #include <sys/agpio.h>
75 #if __FreeBSD_version >= 500000
76 #include <sys/mutex.h>
77 #include <dev/pci/pcivar.h>
78 #include <sys/selinfo.h>
79 #else /* __FreeBSD_version >= 500000 */
80 #include <pci/pcivar.h>
81 #include <sys/select.h>
82 #endif /* __FreeBSD_version < 500000 */
83 #elif defined(__NetBSD__)
84 #include <machine/mtrr.h>
85 #include <sys/vnode.h>
86 #include <sys/select.h>
87 #include <sys/device.h>
88 #include <sys/resourcevar.h>
89 #include <sys/lkm.h>
90 #include <sys/agpio.h>
91 #include <sys/ttycom.h>
92 #include <uvm/uvm.h>
93 #include <dev/pci/pcireg.h>
94 #include <dev/pci/pcivar.h>
95 #include <dev/pci/agpvar.h>
96 #elif defined(__OpenBSD__)
97 #include <sys/lkm.h>
98 #include <uvm/uvm.h>
99 #endif
100 #include <sys/bus.h>
101
102 #include "drm.h"
103 #include "drm_linux_list.h"
104 #include "drm_atomic.h"
105
106 #ifdef __FreeBSD__
107 #include <opt_drm.h>
108 #ifdef DRM_DEBUG
109 #undef DRM_DEBUG
110 #define DRM_DEBUG_DEFAULT_ON 1
111 #endif /* DRM_DEBUG */
112 #endif
113
114 #if defined(DRM_LINUX) && DRM_LINUX && !defined(__amd64__)
115 #include <sys/file.h>
116 #include <sys/proc.h>
117 #include <machine/../linux/linux.h>
118 #include <machine/../linux/linux_proto.h>
119 #else
120 /* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
121  * supported on this OS yet.
122  */
123 #undef DRM_LINUX
124 #define DRM_LINUX 0
125 #endif
126
127 #define DRM_HASH_SIZE         16 /* Size of key hash table                */
128 #define DRM_KERNEL_CONTEXT    0  /* Change drm_resctx if changed          */
129 #define DRM_RESERVED_CONTEXTS 1  /* Change drm_resctx if changed          */
130
131 #define DRM_MEM_DMA        0
132 #define DRM_MEM_SAREA      1
133 #define DRM_MEM_DRIVER     2
134 #define DRM_MEM_MAGIC      3
135 #define DRM_MEM_IOCTLS     4
136 #define DRM_MEM_MAPS       5
137 #define DRM_MEM_BUFS       6
138 #define DRM_MEM_SEGS       7
139 #define DRM_MEM_PAGES      8
140 #define DRM_MEM_FILES     9
141 #define DRM_MEM_QUEUES    10
142 #define DRM_MEM_CMDS      11
143 #define DRM_MEM_MAPPINGS  12
144 #define DRM_MEM_BUFLISTS  13
145 #define DRM_MEM_AGPLISTS  14
146 #define DRM_MEM_TOTALAGP  15
147 #define DRM_MEM_BOUNDAGP  16
148 #define DRM_MEM_CTXBITMAP 17
149 #define DRM_MEM_STUB      18
150 #define DRM_MEM_SGLISTS   19
151
152 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
153
154                                 /* Internal types and structures */
155 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
156 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
157 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
158
159 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
160
161 MALLOC_DECLARE(M_DRM);
162
163 #define __OS_HAS_AGP    1
164
165 #define DRM_DEV_MODE    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
166 #define DRM_DEV_UID     0
167 #define DRM_DEV_GID     0
168
169 #define wait_queue_head_t       atomic_t
170 #define DRM_WAKEUP(w)           wakeup((void *)w)
171 #define DRM_WAKEUP_INT(w)       wakeup(w)
172 #define DRM_INIT_WAITQUEUE(queue) do {} while (0)
173
174 #if defined(__FreeBSD__) && __FreeBSD_version < 502109
175 #define bus_alloc_resource_any(dev, type, rid, flags) \
176         bus_alloc_resource(dev, type, rid, 0ul, ~0ul, 1, flags)
177 #endif
178
179 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
180 #define DRM_CURPROC             curthread
181 #define DRM_STRUCTPROC          struct thread
182 #define DRM_SPINTYPE            struct mtx
183 #define DRM_SPININIT(l,name)    mtx_init(&l, name, NULL, MTX_DEF)
184 #define DRM_SPINUNINIT(l)       mtx_destroy(&l)
185 #define DRM_SPINLOCK(l)         mtx_lock(l)
186 #define DRM_SPINUNLOCK(u)       mtx_unlock(u);
187 #define DRM_SPINLOCK_ASSERT(l)  mtx_assert(l, MA_OWNED)
188 #define DRM_CURRENTPID          curthread->td_proc->p_pid
189 #define DRM_LOCK()              mtx_lock(&dev->dev_lock)
190 #define DRM_UNLOCK()            mtx_unlock(&dev->dev_lock)
191 #define DRM_SYSCTL_HANDLER_ARGS (SYSCTL_HANDLER_ARGS)
192 #else /* __FreeBSD__ && __FreeBSD_version >= 500000 */
193 #define DRM_CURPROC             curproc
194 #define DRM_STRUCTPROC          struct proc
195 #define DRM_SPINTYPE            struct simplelock
196 #define DRM_SPININIT(l,name)
197 #define DRM_SPINUNINIT(l)
198 #define DRM_SPINLOCK(l) 
199 #define DRM_SPINUNLOCK(u)
200 #define DRM_SPINLOCK_ASSERT(l)
201 #define DRM_CURRENTPID          curproc->p_pid
202 #define DRM_LOCK()
203 #define DRM_UNLOCK()
204 #define DRM_SYSCTL_HANDLER_ARGS SYSCTL_HANDLER_ARGS
205 #define spldrm()                spltty()
206 #endif /* __NetBSD__ || __OpenBSD__ */
207
208 /* Currently our DRMFILE (filp) is a void * which is actually the pid
209  * of the current process.  It should be a per-open unique pointer, but
210  * code for that is not yet written */
211 #define DRMFILE                 void *
212 #define DRM_IRQ_ARGS            void *arg
213 typedef void                    irqreturn_t;
214 #define IRQ_HANDLED             /* nothing */
215 #define IRQ_NONE                /* nothing */
216
217 #if defined(__FreeBSD__)
218 #define DRM_DEVICE                                                      \
219         drm_device_t *dev = kdev->si_drv1
220 #define DRM_IOCTL_ARGS          struct cdev *kdev, u_long cmd, caddr_t data, \
221                                 int flags, DRM_STRUCTPROC *p, DRMFILE filp
222
223 #define PAGE_ALIGN(addr) round_page(addr)
224 #define DRM_SUSER(p)            suser(p)
225 #define DRM_AGP_FIND_DEVICE()   agp_find_device()
226 #define DRM_MTRR_WC             MDF_WRITECOMBINE
227 #define jiffies                 ticks
228
229 #else /* __FreeBSD__ */
230
231 #if defined(__NetBSD__)
232 #define DRM_DEVICE                                                      \
233         drm_device_t *dev = device_lookup(&drm_cd, minor(kdev))
234 #elif defined(__OpenBSD__)
235 #define DRM_DEVICE                                                      \
236         drm_device_t *dev = (device_lookup(&drm_cd,                     \
237             minor(kdev)))->dv_cfdata->cf_driver->cd_devs[minor(kdev)]
238 #endif /* __OpenBSD__ */
239 #define DRM_IOCTL_ARGS          dev_t kdev, u_long cmd, caddr_t data, \
240                                 int flags, DRM_STRUCTPROC *p, DRMFILE filp
241
242 #define CDEV_MAJOR              34
243 #define PAGE_ALIGN(addr)        (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
244 #define DRM_SUSER(p)            suser(p->p_ucred, &p->p_acflag)
245 #define DRM_AGP_FIND_DEVICE()   agp_find_device(0)
246 #define DRM_MTRR_WC             MTRR_TYPE_WC
247 #define jiffies                 hardclock_ticks
248
249 typedef drm_device_t *device_t;
250 extern struct cfdriver drm_cd;
251 #endif /* !__FreeBSD__ */
252
253 typedef unsigned long dma_addr_t;
254 typedef u_int32_t u32;
255 typedef u_int16_t u16;
256 typedef u_int8_t u8;
257
258 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
259  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
260  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
261  */
262 #if defined(__i386__)
263 #define DRM_READMEMORYBARRIER()         __asm __volatile( \
264                                         "lock; addl $0,0(%%esp)" : : : "memory");
265 #define DRM_WRITEMEMORYBARRIER()        __asm __volatile("" : : : "memory");
266 #define DRM_MEMORYBARRIER()             __asm __volatile( \
267                                         "lock; addl $0,0(%%esp)" : : : "memory");
268 #elif defined(__alpha__)
269 #define DRM_READMEMORYBARRIER()         alpha_mb();
270 #define DRM_WRITEMEMORYBARRIER()        alpha_wmb();
271 #define DRM_MEMORYBARRIER()             alpha_mb();
272 #elif defined(__amd64__)
273 #define DRM_READMEMORYBARRIER()         __asm __volatile( \
274                                         "lock; addl $0,0(%%rsp)" : : : "memory");
275 #define DRM_WRITEMEMORYBARRIER()        __asm __volatile("" : : : "memory");
276 #define DRM_MEMORYBARRIER()             __asm __volatile( \
277                                         "lock; addl $0,0(%%rsp)" : : : "memory");
278 #endif
279
280 #ifdef __FreeBSD__
281 #define DRM_READ8(map, offset)                                          \
282         *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset))
283 #define DRM_READ16(map, offset)                                         \
284         *(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset))
285 #define DRM_READ32(map, offset)                                         \
286         *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset))
287 #define DRM_WRITE8(map, offset, val)                                    \
288         *(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset)) = val
289 #define DRM_WRITE16(map, offset, val)                                   \
290         *(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset)) = val
291 #define DRM_WRITE32(map, offset, val)                                   \
292         *(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset)) = val
293
294 #define DRM_VERIFYAREA_READ( uaddr, size )              \
295         (!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
296
297 #else /* __FreeBSD__ */
298
299 typedef vaddr_t vm_offset_t;
300
301 #define DRM_READ8(map, offset)          \
302         bus_space_read_1( (map)->bst, (map)->bsh, (offset))
303 #define DRM_READ16(map, offset)         \
304         bus_space_read_2( (map)->bst, (map)->bsh, (offset))
305 #define DRM_READ32(map, offset)         \
306         bus_space_read_4( (map)->bst, (map)->bsh, (offset))
307 #define DRM_WRITE8(map, offset, val)    \
308         bus_space_write_1((map)->bst, (map)->bsh, (offset), (val))
309 #define DRM_WRITE16(map, offset, val)   \
310         bus_space_write_2((map)->bst, (map)->bsh, (offset), (val))
311 #define DRM_WRITE32(map, offset, val)   \
312         bus_space_write_4((map)->bst, (map)->bsh, (offset), (val))
313
314 #define DRM_VERIFYAREA_READ( uaddr, size )              \
315         (!uvm_useracc((caddr_t)uaddr, size, VM_PROT_READ))
316 #endif /* !__FreeBSD__ */
317
318 #define DRM_COPY_TO_USER_IOCTL(user, kern, size)        \
319         if ( IOCPARM_LEN(cmd) != size)                  \
320                 return EINVAL;                          \
321         *user = kern;
322 #define DRM_COPY_FROM_USER_IOCTL(kern, user, size) \
323         if ( IOCPARM_LEN(cmd) != size)                  \
324                 return EINVAL;                          \
325         kern = *user;
326 #define DRM_COPY_TO_USER(user, kern, size) \
327         copyout(kern, user, size)
328 #define DRM_COPY_FROM_USER(kern, user, size) \
329         copyin(user, kern, size)
330 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
331         copyin(arg2, arg1, arg3)
332 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)    \
333         copyout(arg2, arg1, arg3)
334 #if __FreeBSD_version > 500000
335 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
336         ((val) = fuword32(uaddr), 0)
337 #else
338 #define DRM_GET_USER_UNCHECKED(val, uaddr)              \
339         ((val) = fuword(uaddr), 0)
340 #endif
341
342 #define cpu_to_le32(x) htole32(x)
343 #define le32_to_cpu(x) le32toh(x)
344
345 #define DRM_ERR(v)              v
346 #define DRM_HZ                  hz
347 #define DRM_UDELAY(udelay)      DELAY(udelay)
348 #define DRM_TIME_SLICE          (hz/20)  /* Time slice for GLXContexts    */
349
350 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
351         (_map) = (_dev)->context_sareas[_ctx];          \
352 } while(0)
353
354 #define DRM_GET_PRIV_WITH_RETURN(_priv, _filp)                  \
355 do {                                                            \
356         if (_filp != (DRMFILE)(intptr_t)DRM_CURRENTPID) {       \
357                 DRM_ERROR("filp doesn't match curproc\n");      \
358                 return EINVAL;                                  \
359         }                                                       \
360         _priv = drm_find_file_by_proc(dev, DRM_CURPROC);        \
361         if (_priv == NULL) {                                    \
362                 DRM_ERROR("can't find authenticator\n");        \
363                 return EINVAL;                                  \
364         }                                                       \
365 } while (0)
366
367 #define LOCK_TEST_WITH_RETURN(dev, filp)                                \
368 do {                                                                    \
369         if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||              \
370              dev->lock.filp != filp) {                                  \
371                 DRM_ERROR("%s called without lock held\n",              \
372                            __FUNCTION__);                               \
373                 return EINVAL;                                          \
374         }                                                               \
375 } while (0)
376
377 #define DRM_GETSAREA()                                  \
378 do {                                                            \
379         drm_local_map_t *map;                                   \
380         DRM_SPINLOCK_ASSERT(&dev->dev_lock);                    \
381         TAILQ_FOREACH(map, &dev->maplist, link) {               \
382                 if (map->type == _DRM_SHM &&                    \
383                         map->flags & _DRM_CONTAINS_LOCK) {      \
384                         dev_priv->sarea = map;                  \
385                         break;                                  \
386                 }                                               \
387         }                                                       \
388 } while (0)
389
390 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
391 #define DRM_WAIT_ON( ret, queue, timeout, condition )           \
392 for ( ret = 0 ; !ret && !(condition) ; ) {                      \
393         DRM_UNLOCK();                                           \
394         mtx_lock(&dev->irq_lock);                               \
395         if (!(condition))                                       \
396            ret = msleep(&(queue), &dev->irq_lock,               \
397                          PZERO | PCATCH, "drmwtq", (timeout));  \
398         mtx_unlock(&dev->irq_lock);                             \
399         DRM_LOCK();                                             \
400 }
401 #else
402 #define DRM_WAIT_ON( ret, queue, timeout, condition )   \
403 for ( ret = 0 ; !ret && !(condition) ; ) {              \
404         int s = spldrm();                               \
405         if (!(condition))                               \
406            ret = tsleep( &(queue), PZERO | PCATCH,      \
407                          "drmwtq", (timeout) );         \
408         splx(s);                                        \
409 }
410 #endif
411
412 #define DRM_ERROR(fmt, arg...) \
413         printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,           \
414             DRM_CURRENTPID, __func__ , ## arg)
415
416 #define DRM_INFO(fmt, arg...)  printf("info: [" DRM_NAME "] " fmt , ## arg)
417
418 #define DRM_DEBUG(fmt, arg...) do {                                     \
419         if (drm_debug_flag)                                             \
420                 printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,  \
421                         __func__ , ## arg);                             \
422 } while (0)
423
424 typedef struct drm_pci_id_list
425 {
426         int vendor;
427         int device;
428         long driver_private;
429         char *name;
430 } drm_pci_id_list_t;
431
432 typedef struct drm_ioctl_desc {
433         int                  (*func)(DRM_IOCTL_ARGS);
434         int                  auth_needed;
435         int                  root_only;
436 } drm_ioctl_desc_t;
437
438 typedef struct drm_magic_entry {
439         drm_magic_t            magic;
440         struct drm_file        *priv;
441         struct drm_magic_entry *next;
442 } drm_magic_entry_t;
443
444 typedef struct drm_magic_head {
445         struct drm_magic_entry *head;
446         struct drm_magic_entry *tail;
447 } drm_magic_head_t;
448
449 typedef struct drm_buf {
450         int               idx;         /* Index into master buflist          */
451         int               total;       /* Buffer size                        */
452         int               order;       /* log-base-2(total)                  */
453         int               used;        /* Amount of buffer in use (for DMA)  */
454         unsigned long     offset;      /* Byte offset (used internally)      */
455         void              *address;    /* Address of buffer                  */
456         unsigned long     bus_address; /* Bus address of buffer              */
457         struct drm_buf    *next;       /* Kernel-only: used for free list    */
458         __volatile__ int  pending;     /* On hardware DMA queue              */
459         DRMFILE           filp;        /* Unique identifier of holding process */
460         int               context;     /* Kernel queue for this buffer       */
461         enum {
462                 DRM_LIST_NONE    = 0,
463                 DRM_LIST_FREE    = 1,
464                 DRM_LIST_WAIT    = 2,
465                 DRM_LIST_PEND    = 3,
466                 DRM_LIST_PRIO    = 4,
467                 DRM_LIST_RECLAIM = 5
468         }                 list;        /* Which list we're on                */
469
470         int               dev_priv_size; /* Size of buffer private stoarge   */
471         void              *dev_private;  /* Per-buffer private storage       */
472 } drm_buf_t;
473
474 typedef struct drm_freelist {
475         int               initialized; /* Freelist in use                  */
476         atomic_t          count;       /* Number of free buffers           */
477         drm_buf_t         *next;       /* End pointer                      */
478
479         int               low_mark;    /* Low water mark                   */
480         int               high_mark;   /* High water mark                  */
481 } drm_freelist_t;
482
483 typedef struct drm_buf_entry {
484         int               buf_size;
485         int               buf_count;
486         drm_buf_t         *buflist;
487         int               seg_count;
488         int               page_order;
489         vm_offset_t       *seglist;
490         dma_addr_t        *seglist_bus;
491
492         drm_freelist_t    freelist;
493 } drm_buf_entry_t;
494
495 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
496 struct drm_file {
497         TAILQ_ENTRY(drm_file) link;
498         int               authenticated;
499         int               minor;
500         pid_t             pid;
501         uid_t             uid;
502         int               refs;
503         drm_magic_t       magic;
504         unsigned long     ioctl_count;
505         void             *driver_priv;
506 };
507
508 typedef struct drm_lock_data {
509         drm_hw_lock_t     *hw_lock;     /* Hardware lock                   */
510         DRMFILE           filp;         /* Unique identifier of holding process (NULL is kernel)*/
511         int               lock_queue;   /* Queue of blocked processes      */
512         unsigned long     lock_time;    /* Time of last lock in jiffies    */
513 } drm_lock_data_t;
514
515 /* This structure, in the drm_device_t, is always initialized while the device
516  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
517  * when set marks that no further bufs may be allocated until device teardown
518  * occurs (when the last open of the device has closed).  The high/low
519  * watermarks of bufs are only touched by the X Server, and thus not
520  * concurrently accessed, so no locking is needed.
521  */
522 typedef struct drm_device_dma {
523         drm_buf_entry_t   bufs[DRM_MAX_ORDER+1];
524         int               buf_count;
525         drm_buf_t         **buflist;    /* Vector of pointers info bufs    */
526         int               seg_count;
527         int               page_count;
528         unsigned long     *pagelist;
529         unsigned long     byte_count;
530         enum {
531                 _DRM_DMA_USE_AGP = 0x01,
532                 _DRM_DMA_USE_SG  = 0x02
533         } flags;
534 } drm_device_dma_t;
535
536 typedef struct drm_agp_mem {
537         void               *handle;
538         unsigned long      bound; /* address */
539         int                pages;
540         struct drm_agp_mem *prev;
541         struct drm_agp_mem *next;
542 } drm_agp_mem_t;
543
544 typedef struct drm_agp_head {
545         device_t           agpdev;
546         struct agp_info    info;
547         const char         *chipset;
548         drm_agp_mem_t      *memory;
549         unsigned long      mode;
550         int                enabled;
551         int                acquired;
552         unsigned long      base;
553         int                mtrr;
554         int                cant_use_aperture;
555         unsigned long      page_mask;
556 } drm_agp_head_t;
557
558 typedef struct drm_sg_mem {
559         unsigned long   handle;
560         void            *virtual;
561         int             pages;
562         dma_addr_t      *busaddr;
563 } drm_sg_mem_t;
564
565 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
566
567 typedef struct drm_local_map {
568         unsigned long   offset;  /* Physical address (0 for SAREA)*/
569         unsigned long   size;    /* Physical size (bytes)           */
570         drm_map_type_t  type;    /* Type of memory mapped                   */
571         drm_map_flags_t flags;   /* Flags                                   */
572         void            *handle; /* User-space: "Handle" to pass to mmap    */
573                                  /* Kernel-space: kernel-virtual address    */
574         int             mtrr;    /* Boolean: MTRR used */
575                                  /* Private data                            */
576         int             rid;     /* PCI resource ID for bus_space */
577         int             kernel_owned; /* Boolean: 1 = initmapped, 0 = addmapped */
578         struct resource *bsr;
579         bus_space_tag_t bst;
580         bus_space_handle_t bsh;
581         TAILQ_ENTRY(drm_local_map) link;
582 } drm_local_map_t;
583
584 TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
585 typedef struct drm_vbl_sig {
586         TAILQ_ENTRY(drm_vbl_sig) link;
587         unsigned int    sequence;
588         int             signo;
589         int             pid;
590 } drm_vbl_sig_t;
591
592 /** 
593  * DRM device functions structure
594  */
595 struct drm_device {
596 #if defined(__NetBSD__) || defined(__OpenBSD__)
597         struct device     device; /* softc is an extension of struct device */
598 #endif
599
600         /* Beginning of driver-config section */
601         int     (*preinit)(struct drm_device *, unsigned long flags);
602         int     (*postinit)(struct drm_device *, unsigned long flags);
603         void    (*prerelease)(struct drm_device *, void *filp);
604         void    (*pretakedown)(struct drm_device *);
605         int     (*postcleanup)(struct drm_device *);
606         int     (*presetup)(struct drm_device *);
607         int     (*postsetup)(struct drm_device *);
608         int     (*open_helper)(struct drm_device *, drm_file_t *);
609         void    (*free_filp_priv)(struct drm_device *, drm_file_t *);
610         void    (*release)(struct drm_device *, void *filp);
611         int     (*dma_ioctl)(DRM_IOCTL_ARGS);
612         void    (*dma_ready)(struct drm_device *);
613         int     (*dma_quiescent)(struct drm_device *);
614         int     (*dma_flush_block_and_flush)(struct drm_device *, int context,
615                                              drm_lock_flags_t flags);
616         int     (*dma_flush_unblock)(struct drm_device *, int context,
617                                      drm_lock_flags_t flags);
618         int     (*context_ctor)(struct drm_device *dev, int context);
619         int     (*context_dtor)(struct drm_device *dev, int context);
620         int     (*kernel_context_switch)(struct drm_device *dev, int old,
621                                          int new);
622         int     (*kernel_context_switch_unlock)(struct drm_device *dev);
623         void    (*irq_preinstall)(drm_device_t *dev);
624         void    (*irq_postinstall)(drm_device_t *dev);
625         void    (*irq_uninstall)(drm_device_t *dev);
626         void    (*irq_handler)(DRM_IRQ_ARGS);
627         int     (*vblank_wait)(drm_device_t *dev, unsigned int *sequence);
628
629         drm_ioctl_desc_t *driver_ioctls;
630         int     max_driver_ioctl;
631
632         int     dev_priv_size;
633
634         int     driver_major;
635         int     driver_minor;
636         int     driver_patchlevel;
637         const char *driver_name;        /* Simple driver name              */
638         const char *driver_desc;        /* Longer driver name              */
639         const char *driver_date;        /* Date of last major changes.     */
640
641         unsigned use_agp :1;
642         unsigned require_agp :1;
643         unsigned use_sg :1;
644         unsigned use_dma :1;
645         unsigned use_pci_dma :1;
646         unsigned use_dma_queue :1;
647         unsigned use_irq :1;
648         unsigned use_vbl_irq :1;
649         unsigned use_mtrr :1;
650         /* End of driver-config section */
651
652         char              *unique;      /* Unique identifier: e.g., busid  */
653         int               unique_len;   /* Length of unique field          */
654 #ifdef __FreeBSD__
655         device_t          device;       /* Device instance from newbus     */
656 #endif
657         struct cdev       *devnode;     /* Device number for mknod         */
658         int               if_version;   /* Highest interface version set */
659
660         int               flags;        /* Flags to open(2)                */
661
662                                 /* Locks */
663 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
664         struct mtx        dma_lock;     /* protects dev->dma */
665         struct mtx        irq_lock;     /* protects irq condition checks */
666         struct mtx        dev_lock;     /* protects everything else */
667 #endif
668                                 /* Usage Counters */
669         int               open_count;   /* Outstanding files open          */
670         int               buf_use;      /* Buffers in use -- cannot alloc  */
671
672                                 /* Performance counters */
673         unsigned long     counters;
674         drm_stat_type_t   types[15];
675         atomic_t          counts[15];
676
677                                 /* Authentication */
678         drm_file_list_t   files;
679         drm_magic_head_t  magiclist[DRM_HASH_SIZE];
680
681         /* Linked list of mappable regions. Protected by dev_lock */
682         drm_map_list_t    maplist;
683
684         drm_local_map_t   **context_sareas;
685         int               max_context;
686
687         drm_lock_data_t   lock;         /* Information on hardware lock    */
688
689                                 /* DMA queues (contexts) */
690         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
691
692                                 /* Context support */
693         int               irq;          /* Interrupt used by board         */
694         int               irq_enabled;  /* True if the irq handler is enabled */
695 #ifdef __FreeBSD__
696         int               irqrid;       /* Interrupt used by board */
697         struct resource   *irqr;        /* Resource for interrupt used by board    */
698 #elif defined(__NetBSD__) || defined(__OpenBSD__)
699         struct pci_attach_args  pa;
700         pci_intr_handle_t       ih;
701 #endif
702         void              *irqh;        /* Handle from bus_setup_intr      */
703
704         int               pci_domain;
705         int               pci_bus;
706         int               pci_slot;
707         int               pci_func;
708
709         atomic_t          context_flag; /* Context swapping flag           */
710         int               last_context; /* Last current context            */
711         int               vbl_queue;    /* vbl wait channel */
712         atomic_t          vbl_received;
713
714 #ifdef __FreeBSD__
715         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
716 #elif defined(__NetBSD__)
717         pid_t             buf_pgid;
718 #endif
719
720                                 /* Sysctl support */
721         struct drm_sysctl_info *sysctl;
722
723         drm_agp_head_t    *agp;
724         drm_sg_mem_t      *sg;  /* Scatter gather memory */
725         atomic_t          *ctx_bitmap;
726         void              *dev_private;
727         drm_local_map_t   *agp_buffer_map;
728 };
729
730 extern int      drm_debug_flag;
731
732 /* Device setup support (drm_drv.c) */
733 #ifdef __FreeBSD__
734 int     drm_probe(device_t nbdev, drm_pci_id_list_t *idlist);
735 int     drm_attach(device_t nbdev, drm_pci_id_list_t *idlist);
736 int     drm_detach(device_t nbdev);
737 d_ioctl_t drm_ioctl;
738 d_open_t drm_open;
739 d_close_t drm_close;
740 d_read_t drm_read;
741 d_poll_t drm_poll;
742 d_mmap_t drm_mmap;
743 #elif defined(__NetBSD__) || defined(__OpenBSD__)
744 int     drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
745 int     drm_attach(struct pci_attach_args *pa, dev_t kdev, drm_pci_id_list_t *idlist);
746 dev_type_ioctl(drm_ioctl);
747 dev_type_open(drm_open);
748 dev_type_close(drm_close);
749 dev_type_read(drm_read);
750 dev_type_poll(drm_poll);
751 dev_type_mmap(drm_mmap);
752 #endif
753
754 /* File operations helpers (drm_fops.c) */
755 #ifdef __FreeBSD__
756 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt, 
757                                          DRM_STRUCTPROC *p, drm_device_t *dev);
758 extern drm_file_t       *drm_find_file_by_proc(drm_device_t *dev, 
759                                          DRM_STRUCTPROC *p);
760 #elif defined(__NetBSD__) || defined(__OpenBSD__)
761 extern int              drm_open_helper(dev_t kdev, int flags, int fmt, 
762                                         DRM_STRUCTPROC *p, drm_device_t *dev);
763 extern drm_file_t       *drm_find_file_by_proc(drm_device_t *dev, 
764                                                DRM_STRUCTPROC *p);
765 #endif /* __NetBSD__ || __OpenBSD__ */
766
767 /* Memory management support (drm_memory.c) */
768 void    drm_mem_init(void);
769 void    drm_mem_uninit(void);
770 void    *drm_alloc(size_t size, int area);
771 void    *drm_calloc(size_t nmemb, size_t size, int area);
772 void    *drm_realloc(void *oldpt, size_t oldsize, size_t size,
773                                    int area);
774 void    drm_free(void *pt, size_t size, int area);
775 void    *drm_ioremap(drm_device_t *dev, drm_local_map_t *map);
776 void    drm_ioremapfree(drm_local_map_t *map);
777 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
778 int     drm_mtrr_del(unsigned long offset, size_t size, int flags);
779
780 int     drm_context_switch(drm_device_t *dev, int old, int new);
781 int     drm_context_switch_complete(drm_device_t *dev, int new);
782
783 int     drm_ctxbitmap_init(drm_device_t *dev);
784 void    drm_ctxbitmap_cleanup(drm_device_t *dev);
785 void    drm_ctxbitmap_free(drm_device_t *dev, int ctx_handle);
786 int     drm_ctxbitmap_next(drm_device_t *dev);
787
788 /* Locking IOCTL support (drm_lock.c) */
789 int     drm_lock_take(__volatile__ unsigned int *lock,
790                                     unsigned int context);
791 int     drm_lock_transfer(drm_device_t *dev,
792                                         __volatile__ unsigned int *lock,
793                                         unsigned int context);
794 int     drm_lock_free(drm_device_t *dev,
795                                     __volatile__ unsigned int *lock,
796                                     unsigned int context);
797
798 /* Buffer management support (drm_bufs.c) */
799 unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource);
800 unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource);
801 int     drm_initmap(drm_device_t *dev, unsigned long start, unsigned long len,
802                     unsigned int resource, int type, int flags);
803 void    drm_remove_map(drm_device_t *dev, drm_local_map_t *map);
804 int     drm_order(unsigned long size);
805
806 /* DMA support (drm_dma.c) */
807 int     drm_dma_setup(drm_device_t *dev);
808 void    drm_dma_takedown(drm_device_t *dev);
809 void    drm_free_buffer(drm_device_t *dev, drm_buf_t *buf);
810 void    drm_reclaim_buffers(drm_device_t *dev, DRMFILE filp);
811
812 /* IRQ support (drm_irq.c) */
813 int     drm_irq_install(drm_device_t *dev);
814 int     drm_irq_uninstall(drm_device_t *dev);
815 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
816 void    drm_driver_irq_preinstall(drm_device_t *dev);
817 void    drm_driver_irq_postinstall(drm_device_t *dev);
818 void    drm_driver_irq_uninstall(drm_device_t *dev);
819 int     drm_vblank_wait(drm_device_t *dev, unsigned int *vbl_seq);
820 void    drm_vbl_send_signals(drm_device_t *dev);
821
822 /* AGP/GART support (drm_agpsupport.c) */
823 int     drm_device_is_agp(drm_device_t *dev);
824 drm_agp_head_t *drm_agp_init(void);
825 void    drm_agp_uninit(void);
826 void    drm_agp_do_release(void);
827 void    *drm_agp_allocate_memory(size_t pages, u32 type);
828 int     drm_agp_free_memory(void *handle);
829 int     drm_agp_bind_memory(void *handle, off_t start);
830 int     drm_agp_unbind_memory(void *handle);
831
832 /* Scatter Gather Support (drm_scatter.c) */
833 void    drm_sg_cleanup(drm_sg_mem_t *entry);
834
835 #ifdef __FreeBSD__
836 /* sysctl support (drm_sysctl.h) */
837 extern int              drm_sysctl_init(drm_device_t *dev);
838 extern int              drm_sysctl_cleanup(drm_device_t *dev);
839 #endif /* __FreeBSD__ */
840
841 /* ATI PCIGART support (ati_pcigart.c) */
842 int     drm_ati_pcigart_init(drm_device_t *dev, unsigned long *addr,
843                              dma_addr_t *bus_addr);
844 int     drm_ati_pcigart_cleanup(drm_device_t *dev, unsigned long addr,
845                                 dma_addr_t bus_addr);
846
847 /* Locking IOCTL support (drm_drv.c) */
848 int     drm_lock(DRM_IOCTL_ARGS);
849 int     drm_unlock(DRM_IOCTL_ARGS);
850 int     drm_version(DRM_IOCTL_ARGS);
851 int     drm_setversion(DRM_IOCTL_ARGS);
852
853 /* Misc. IOCTL support (drm_ioctl.c) */
854 int     drm_irq_by_busid(DRM_IOCTL_ARGS);
855 int     drm_getunique(DRM_IOCTL_ARGS);
856 int     drm_setunique(DRM_IOCTL_ARGS);
857 int     drm_getmap(DRM_IOCTL_ARGS);
858 int     drm_getclient(DRM_IOCTL_ARGS);
859 int     drm_getstats(DRM_IOCTL_ARGS);
860 int     drm_noop(DRM_IOCTL_ARGS);
861
862 /* Context IOCTL support (drm_context.c) */
863 int     drm_resctx(DRM_IOCTL_ARGS);
864 int     drm_addctx(DRM_IOCTL_ARGS);
865 int     drm_modctx(DRM_IOCTL_ARGS);
866 int     drm_getctx(DRM_IOCTL_ARGS);
867 int     drm_switchctx(DRM_IOCTL_ARGS);
868 int     drm_newctx(DRM_IOCTL_ARGS);
869 int     drm_rmctx(DRM_IOCTL_ARGS);
870 int     drm_setsareactx(DRM_IOCTL_ARGS);
871 int     drm_getsareactx(DRM_IOCTL_ARGS);
872
873 /* Drawable IOCTL support (drm_drawable.c) */
874 int     drm_adddraw(DRM_IOCTL_ARGS);
875 int     drm_rmdraw(DRM_IOCTL_ARGS);
876
877 /* Authentication IOCTL support (drm_auth.c) */
878 int     drm_getmagic(DRM_IOCTL_ARGS);
879 int     drm_authmagic(DRM_IOCTL_ARGS);
880
881 /* Buffer management support (drm_bufs.c) */
882 int     drm_addmap(DRM_IOCTL_ARGS);
883 int     drm_rmmap(DRM_IOCTL_ARGS);
884 int     drm_addbufs(DRM_IOCTL_ARGS);
885 int     drm_infobufs(DRM_IOCTL_ARGS);
886 int     drm_markbufs(DRM_IOCTL_ARGS);
887 int     drm_freebufs(DRM_IOCTL_ARGS);
888 int     drm_mapbufs(DRM_IOCTL_ARGS);
889
890 /* DMA support (drm_dma.c) */
891 int     drm_dma(DRM_IOCTL_ARGS);
892
893 /* IRQ support (drm_irq.c) */
894 int     drm_control(DRM_IOCTL_ARGS);
895 int     drm_wait_vblank(DRM_IOCTL_ARGS);
896
897 /* AGP/GART support (drm_agpsupport.c) */
898 int     drm_agp_acquire(DRM_IOCTL_ARGS);
899 int     drm_agp_release(DRM_IOCTL_ARGS);
900 int     drm_agp_enable(DRM_IOCTL_ARGS);
901 int     drm_agp_info(DRM_IOCTL_ARGS);
902 int     drm_agp_alloc(DRM_IOCTL_ARGS);
903 int     drm_agp_free(DRM_IOCTL_ARGS);
904 int     drm_agp_unbind(DRM_IOCTL_ARGS);
905 int     drm_agp_bind(DRM_IOCTL_ARGS);
906
907 /* Scatter Gather Support (drm_scatter.c) */
908 int     drm_sg_alloc(DRM_IOCTL_ARGS);
909 int     drm_sg_free(DRM_IOCTL_ARGS);
910
911 /* consistent PCI memory functions (drm_pci.c) */
912 void    *drm_pci_alloc(drm_device_t *dev, size_t size, size_t align,
913                        dma_addr_t maxaddr, dma_addr_t *busaddr);
914 void    drm_pci_free(drm_device_t *dev, size_t size, void *vaddr,
915                      dma_addr_t busaddr);
916
917 /* Inline replacements for DRM_IOREMAP macros */
918 static __inline__ void drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
919 {
920         map->handle = drm_ioremap(dev, map);
921 }
922 static __inline__ void drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
923 {
924         if ( map->handle && map->size )
925                 drm_ioremapfree(map);
926 }
927
928 static __inline__ struct drm_local_map *drm_core_findmap(struct drm_device *dev, unsigned long offset)
929 {
930         drm_local_map_t *map;
931
932         DRM_SPINLOCK_ASSERT(&dev->dev_lock);
933         TAILQ_FOREACH(map, &dev->maplist, link) {
934                 if (map->offset == offset)
935                         return map;
936         }
937         return NULL;
938 }
939
940 static __inline__ void drm_core_dropmap(struct drm_map *map)
941 {
942 }
943
944 #endif /* __KERNEL__ */
945 #endif /* _DRM_P_H_ */