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