Home | History | Annotate | Line # | Download | only in bsd-core
drmP.h revision 1.6
      1 /* drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
      2  * Created: Mon Jan  4 10:05:05 1999 by faith (at) 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 (at) valinux.com>
     30  *    Gareth Hughes <gareth (at) 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 #if defined(__NetBSD__) && defined(_KERNEL_OPT)
     43 #include "opt_drm.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/queue.h>
     48 #include <sys/malloc.h>
     49 #include <sys/kernel.h>
     50 #ifndef __OpenBSD__
     51 #include <sys/module.h>
     52 #endif
     53 #include <sys/systm.h>
     54 #include <sys/conf.h>
     55 #include <sys/stat.h>
     56 #if __FreeBSD_version >= 700000
     57 #include <sys/priv.h>
     58 #endif
     59 #include <sys/proc.h>
     60 #include <sys/lock.h>
     61 #include <sys/fcntl.h>
     62 #include <sys/uio.h>
     63 #include <sys/filio.h>
     64 #include <sys/sysctl.h>
     65 #include <sys/poll.h>
     66 #include <sys/tree.h>
     67 #include <sys/bus.h>
     68 #include <sys/signalvar.h>
     69 #ifdef __FreeBSD__
     70 #include <sys/taskqueue.h>
     71 #include <vm/vm.h>
     72 #include <vm/pmap.h>
     73 #include <vm/vm_extern.h>
     74 #include <vm/vm_map.h>
     75 #include <vm/vm_param.h>
     76 #include <machine/resource.h>
     77 #include <machine/param.h>
     78 #endif
     79 #include <machine/pmap.h>
     80 #include <machine/bus.h>
     81 #if !defined(DRM_NO_MTRR)
     82 #include <machine/sysarch.h>
     83 #endif
     84 #include <sys/endian.h>
     85 #include <sys/mman.h>
     86 #if defined(__FreeBSD__)
     87 #include <sys/rman.h>
     88 #include <sys/memrange.h>
     89 #if __FreeBSD_version >= 800004
     90 #include <dev/agp/agpvar.h>
     91 #else /* __FreeBSD_version >= 800004 */
     92 #include <pci/agpvar.h>
     93 #endif /* __FreeBSD_version >= 800004 */
     94 #include <sys/agpio.h>
     95 #if __FreeBSD_version >= 500000
     96 #include <sys/mutex.h>
     97 #include <dev/pci/pcivar.h>
     98 #include <sys/selinfo.h>
     99 #else /* __FreeBSD_version >= 500000 */
    100 #include <pci/pcivar.h>
    101 #include <sys/select.h>
    102 #endif /* __FreeBSD_version < 500000 */
    103 #elif defined(__NetBSD__)
    104 #ifndef DRM_NO_MTRR
    105 #include <machine/mtrr.h>
    106 #endif
    107 #include <sys/vnode.h>
    108 #include <sys/select.h>
    109 #include <sys/device.h>
    110 #include <sys/resourcevar.h>
    111 #include <sys/lkm.h>
    112 #include <sys/agpio.h>
    113 #include <sys/ttycom.h>
    114 #include <sys/mman.h>
    115 #include <sys/kauth.h>
    116 #include <sys/types.h>
    117 #include <sys/file.h>
    118 #include <sys/atomic.h>
    119 #include <sys/workqueue.h>
    120 #include <uvm/uvm.h>
    121 #include <dev/pci/pcireg.h>
    122 #include <dev/pci/pcivar.h>
    123 #include <dev/pci/agpvar.h>
    124 #elif defined(__OpenBSD__)
    125 #include <sys/lkm.h>
    126 #include <uvm/uvm.h>
    127 #endif
    128 
    129 #include "drm.h"
    130 #include "drm_linux_list.h"
    131 #include "drm_atomic.h"
    132 #include "drm_internal.h"
    133 
    134 #ifdef __FreeBSD__
    135 #include <opt_drm.h>
    136 #endif
    137 
    138 #ifdef DRM_DEBUG
    139 #undef DRM_DEBUG
    140 #define DRM_DEBUG_DEFAULT_ON 1
    141 #endif /* DRM_DEBUG */
    142 
    143 #ifdef __FreeBSD__
    144 #define DRM_KASSERT(x,y)	KASSERT(x,y)
    145 #else
    146 #define DRM_KASSERT(x,y)	KASSERT(x)
    147 #define device_get_softc(x)	device_private(x)
    148 #define callout_deactivate(x)	callout_stop(x)
    149 #define callout_drain(x)	callout_stop(x)
    150 #endif
    151 
    152 #if defined(DRM_LINUX) && DRM_LINUX && !defined(__amd64__)
    153 #include <sys/file.h>
    154 #include <sys/proc.h>
    155 #include <machine/../linux/linux.h>
    156 #include <machine/../linux/linux_proto.h>
    157 #else
    158 /* Either it was defined when it shouldn't be (FreeBSD amd64) or it isn't
    159  * supported on this OS yet.
    160  */
    161 #undef DRM_LINUX
    162 #define DRM_LINUX 0
    163 #endif
    164 
    165 #define DRM_HASH_SIZE	      16 /* Size of key hash table		  */
    166 #define DRM_KERNEL_CONTEXT    0	 /* Change drm_resctx if changed	  */
    167 #define DRM_RESERVED_CONTEXTS 1	 /* Change drm_resctx if changed	  */
    168 
    169 #define DRM_MEM_DMA	   0
    170 #define DRM_MEM_SAREA	   1
    171 #define DRM_MEM_DRIVER	   2
    172 #define DRM_MEM_MAGIC	   3
    173 #define DRM_MEM_IOCTLS	   4
    174 #define DRM_MEM_MAPS	   5
    175 #define DRM_MEM_BUFS	   6
    176 #define DRM_MEM_SEGS	   7
    177 #define DRM_MEM_PAGES	   8
    178 #define DRM_MEM_FILES	  9
    179 #define DRM_MEM_QUEUES	  10
    180 #define DRM_MEM_CMDS	  11
    181 #define DRM_MEM_MAPPINGS  12
    182 #define DRM_MEM_BUFLISTS  13
    183 #define DRM_MEM_AGPLISTS  14
    184 #define DRM_MEM_TOTALAGP  15
    185 #define DRM_MEM_BOUNDAGP  16
    186 #define DRM_MEM_CTXBITMAP 17
    187 #define DRM_MEM_STUB	  18
    188 #define DRM_MEM_SGLISTS	  19
    189 #define DRM_MEM_DRAWABLE  20
    190 
    191 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
    192 
    193 				/* Internal types and structures */
    194 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
    195 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
    196 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
    197 
    198 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
    199 
    200 MALLOC_DECLARE(M_DRM);
    201 
    202 #define __OS_HAS_AGP	1
    203 
    204 #define DRM_DEV_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
    205 #define DRM_DEV_UID	0
    206 #define DRM_DEV_GID	0
    207 
    208 #define wait_queue_head_t	atomic_t
    209 #define DRM_WAKEUP(w)		wakeup((void *)w)
    210 #define DRM_WAKEUP_INT(w)	wakeup(w)
    211 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
    212 
    213 #if defined(__FreeBSD__) && __FreeBSD_version < 502109
    214 #define bus_alloc_resource_any(dev, type, rid, flags) \
    215 	bus_alloc_resource(dev, type, rid, 0ul, ~0ul, 1, flags)
    216 #endif
    217 
    218 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    219 #define DRM_CDEV		struct cdev *
    220 #define DRM_CURPROC		curthread
    221 #define DRM_STRUCTPROC		struct thread
    222 #define DRM_STRUCTCDEVPROC	struct thread
    223 #define DRM_CDEVPROC_REAL(p)	(p)
    224 #define DRM_IOCTL_DATA		caddr_t
    225 #define DRM_SPINTYPE		struct mtx
    226 #define DRM_SPININIT(l,name)	mtx_init(l, name, NULL, MTX_DEF)
    227 #define DRM_SPINUNINIT(l)	mtx_destroy(l)
    228 #define DRM_SPINLOCK(l)		mtx_lock(l)
    229 #define DRM_SPINUNLOCK(u)	mtx_unlock(u)
    230 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {		\
    231 	mtx_lock(l);					\
    232 	(void)irqflags;					\
    233 } while (0)
    234 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mtx_unlock(u)
    235 #define DRM_SPINLOCK_ASSERT(l)	mtx_assert(l, MA_OWNED)
    236 #define DRM_CURRENTPID		curthread->td_proc->p_pid
    237 #define DRM_LOCK()		mtx_lock(&dev->dev_lock)
    238 #define DRM_UNLOCK() 		mtx_unlock(&dev->dev_lock)
    239 #define DRM_SYSCTL_HANDLER_ARGS	(SYSCTL_HANDLER_ARGS)
    240 #else /* __FreeBSD__ && __FreeBSD_version >= 500000 */
    241 #ifdef __NetBSD__
    242 #define DRM_CDEV		dev_t
    243 #define DRM_CURPROC		curproc
    244 #define DRM_STRUCTPROC		struct proc
    245 #define DRM_STRUCTCDEVPROC	struct lwp
    246 #define DRM_CDEVPROC_REAL(l)	(l)->l_proc
    247 #define DRM_IOCTL_DATA		void *
    248 #define DRM_SPINTYPE		kmutex_t
    249 #define DRM_SPININIT(l,name)	mutex_init(l, MUTEX_DEFAULT, IPL_NONE)
    250 #define DRM_SPINUNINIT(l)	mutex_destroy(l)
    251 #define DRM_SPINLOCK(l)		mutex_enter(l)
    252 #define DRM_SPINUNLOCK(u)	mutex_exit(u)
    253 #define DRM_SPINLOCK_ASSERT(l)	mutex_owned(l)
    254 #define DRM_LOCK()		DRM_SPINLOCK(&dev->dev_lock)
    255 #define DRM_UNLOCK() 		DRM_SPINUNLOCK(&dev->dev_lock)
    256 #define DRM_CURRENTPID		curproc->p_pid
    257 #define DRM_SYSCTL_HANDLER_ARGS	(SYSCTLFN_ARGS)
    258 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {		\
    259 	mutex_enter(l);					\
    260 	(void)irqflags;					\
    261 } while (0)
    262 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mutex_exit(u)
    263 #else /* __NetBSD__ */
    264 #define DRM_CURPROC		curproc
    265 #define DRM_STRUCTPROC		struct proc
    266 #define DRM_SPINTYPE		struct simplelock
    267 #define DRM_SPININIT(l,name)
    268 #define DRM_SPINUNINIT(l)
    269 #define DRM_SPINLOCK(l)
    270 #define DRM_SPINUNLOCK(u)
    271 #define DRM_SPINLOCK_ASSERT(l)
    272 #define DRM_CURRENTPID		curproc->p_pid
    273 #define DRM_LOCK()
    274 #define DRM_UNLOCK()
    275 #define DRM_SYSCTL_HANDLER_ARGS	SYSCTL_HANDLER_ARGS
    276 #define spldrm()		spltty()
    277 #endif /* __OpenBSD__ */
    278 #endif
    279 
    280 #define DRM_IRQ_ARGS		void *arg
    281 #ifdef __FreeBSD__
    282 typedef void			irqreturn_t;
    283 #define IRQ_HANDLED		/* nothing */
    284 #define IRQ_NONE		/* nothing */
    285 #elif defined(__NetBSD__)
    286 typedef int			irqreturn_t;
    287 #define IRQ_HANDLED		0
    288 #define IRQ_NONE		0
    289 #endif
    290 
    291 enum {
    292 	DRM_IS_NOT_AGP,
    293 	DRM_IS_AGP,
    294 	DRM_MIGHT_BE_AGP
    295 };
    296 #define DRM_AGP_MEM		struct agp_memory_info
    297 
    298 #if defined(__FreeBSD__)
    299 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
    300 #elif defined(__NetBSD__)
    301 #define drm_get_device_from_kdev(_kdev) (minor(_kdev) < DRM_MAXUNITS) ? \
    302 	drm_units[minor(_kdev)] : NULL
    303 #elif defined(__OpenBSD__)
    304 #define drm_get_device_from_kdev(_kdev) device_lookup(&drm_cd, \
    305     minor(_kdev)))->dv_cfdata->cf_driver->cd_devs[minor(_kdev)]
    306 #endif
    307 
    308 #if defined(__FreeBSD__)
    309 #define PAGE_ALIGN(addr) round_page(addr)
    310 /* DRM_SUSER returns true if the user is superuser */
    311 #if __FreeBSD_version >= 700000
    312 #define DRM_SUSER(p)		(priv_check(p, PRIV_DRIVER) == 0)
    313 #else
    314 #define DRM_SUSER(p)		(suser(p) == 0)
    315 #endif
    316 #define DRM_AGP_FIND_DEVICE()	agp_find_device()
    317 #define DRM_MTRR_WC		MDF_WRITECOMBINE
    318 #define jiffies			ticks
    319 
    320 #else /* __FreeBSD__ */
    321 
    322 #define CDEV_MAJOR		34
    323 #define PAGE_ALIGN(addr)	ALIGN(addr)
    324 /* DRM_SUSER returns true if the user is superuser */
    325 #if defined(__NetBSD__)
    326 #define DRM_SUSER(p)		(kauth_cred_getsvuid((p)->p_cred) == 0)
    327 #define DRM_MAXUNITS 128
    328 extern drm_device_t *drm_units[];
    329 #define DRM_MTRR_WC		MTRR_TYPE_WC
    330 #define jiffies			hardclock_ticks
    331 #ifdef __x86_64__
    332 #define DRM_NETBSD_ADDR2HANDLE(addr)	(addr   & 0x7fffffffffffffff)
    333 #define DRM_NETBSD_HANDLE2ADDR(handle)	(handle | 0x8000000000000000)
    334 #else
    335 #define DRM_NETBSD_ADDR2HANDLE(addr)	(addr)
    336 #define DRM_NETBSD_HANDLE2ADDR(handle)	(handle)
    337 #endif
    338 #elif defined(__OpenBSD__)
    339 #define DRM_SUSER(p)		(suser(p->p_ucred, &p->p_acflag) == 0)
    340 #define DRM_MTRR_WC		MTRR_TYPE_WC
    341 #define jiffies			ticks
    342 #endif
    343 
    344 #ifdef DRM_NO_AGP
    345 #define DRM_AGP_FIND_DEVICE() 0
    346 #else
    347 #define DRM_AGP_FIND_DEVICE()	agp_find_device(0)
    348 #endif
    349 
    350 #if !defined(__NetBSD__)
    351 typedef struct drm_device *device_t;
    352 #endif
    353 
    354 extern struct cfdriver drm_cd;
    355 
    356 #endif /* !__FreeBSD__ */
    357 
    358 /* Capabilities taken from src/sys/dev/pci/pcireg.h. */
    359 #ifndef PCIY_AGP
    360 #define PCIY_AGP	0x02
    361 #endif
    362 
    363 #ifndef PCIY_EXPRESS
    364 #define PCIY_EXPRESS	0x10
    365 #endif
    366 
    367 typedef unsigned long dma_addr_t;
    368 typedef u_int64_t u64;
    369 typedef u_int32_t u32;
    370 typedef u_int16_t u16;
    371 typedef u_int8_t u8;
    372 
    373 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
    374  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
    375  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
    376  */
    377 
    378 /* XXX  TODO: change to bus_space_barrier(9), as certain architectures
    379 	might need them--not to mention that MD cruft is ugly.  I just
    380 	didn't feel like writing the macros right now.  The use of mb(9)
    381 	below should be enough for x86, perhaps others. */
    382 
    383 #if defined(__NetBSD__)
    384 #define DRM_READMEMORYBARRIER()		membar_consumer()
    385 #define DRM_WRITEMEMORYBARRIER()	membar_producer()
    386 #define DRM_MEMORYBARRIER()		membar_sync()
    387 #elif defined(__i386__)
    388 #define DRM_READMEMORYBARRIER()		__asm __volatile( \
    389 					"lock; addl $0,0(%%esp)" : : : "memory");
    390 #define DRM_WRITEMEMORYBARRIER()	__asm __volatile("" : : : "memory");
    391 #define DRM_MEMORYBARRIER()		__asm __volatile( \
    392 					"lock; addl $0,0(%%esp)" : : : "memory");
    393 #elif defined(__alpha__)
    394 #define DRM_READMEMORYBARRIER()		alpha_mb();
    395 #define DRM_WRITEMEMORYBARRIER()	alpha_wmb();
    396 #define DRM_MEMORYBARRIER()		alpha_mb();
    397 #elif defined(__amd64__)
    398 #define DRM_READMEMORYBARRIER()		__asm __volatile( \
    399 					"lock; addl $0,0(%%rsp)" : : : "memory");
    400 #define DRM_WRITEMEMORYBARRIER()	__asm __volatile("" : : : "memory");
    401 #define DRM_MEMORYBARRIER()		__asm __volatile( \
    402 					"lock; addl $0,0(%%rsp)" : : : "memory");
    403 #endif
    404 
    405 #ifdef __FreeBSD__
    406 #define DRM_READ8(map, offset)						\
    407 	*(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset))
    408 #define DRM_READ16(map, offset)						\
    409 	*(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset))
    410 #define DRM_READ32(map, offset)						\
    411 	*(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset))
    412 #define DRM_WRITE8(map, offset, val)					\
    413 	*(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset)) = val
    414 #define DRM_WRITE16(map, offset, val)					\
    415 	*(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset)) = val
    416 #define DRM_WRITE32(map, offset, val)					\
    417 	*(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset)) = val
    418 
    419 #define DRM_VERIFYAREA_READ( uaddr, size )		\
    420 	(!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
    421 
    422 #else /* __FreeBSD__ */
    423 
    424 typedef vaddr_t vm_offset_t;
    425 
    426 #define DRM_READ8(map, offset)		\
    427 	bus_space_read_1( (map)->bst, (map)->bsh, (offset))
    428 #define DRM_READ16(map, offset)		\
    429 	bus_space_read_2( (map)->bst, (map)->bsh, (offset))
    430 #define DRM_READ32(map, offset)		\
    431 	bus_space_read_4( (map)->bst, (map)->bsh, (offset))
    432 #define DRM_WRITE8(map, offset, val)	\
    433 	bus_space_write_1((map)->bst, (map)->bsh, (offset), (val))
    434 #define DRM_WRITE16(map, offset, val)	\
    435 	bus_space_write_2((map)->bst, (map)->bsh, (offset), (val))
    436 #define DRM_WRITE32(map, offset, val)	\
    437 	bus_space_write_4((map)->bst, (map)->bsh, (offset), (val))
    438 
    439 #define DRM_VERIFYAREA_READ( uaddr, size )		\
    440 	(!uvm_map_checkprot(&(curproc->p_vmspace->vm_map),              \
    441 		(vaddr_t)uaddr, (vaddr_t)uaddr+size, UVM_PROT_READ))
    442 
    443 #endif /* !__FreeBSD__ */
    444 
    445 #define DRM_COPY_TO_USER(user, kern, size) \
    446 	copyout(kern, user, size)
    447 #define DRM_COPY_FROM_USER(kern, user, size) \
    448 	copyin(user, kern, size)
    449 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) 	\
    450 	copyin(arg2, arg1, arg3)
    451 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)	\
    452 	copyout(arg2, arg1, arg3)
    453 #if __FreeBSD_version > 500000
    454 #define DRM_GET_USER_UNCHECKED(val, uaddr)		\
    455 	((val) = fuword32(uaddr), 0)
    456 #else
    457 #define DRM_GET_USER_UNCHECKED(val, uaddr)		\
    458 	((val) = fuword(uaddr), 0)
    459 #endif
    460 
    461 #define cpu_to_le32(x) htole32(x)
    462 #define le32_to_cpu(x) le32toh(x)
    463 
    464 #define DRM_HZ			hz
    465 #define DRM_UDELAY(udelay)	DELAY(udelay)
    466 #define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
    467 
    468 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {	\
    469 	(_map) = (_dev)->context_sareas[_ctx];		\
    470 } while(0)
    471 
    472 #define LOCK_TEST_WITH_RETURN(dev, file_priv)				\
    473 do {									\
    474 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
    475 	     dev->lock.file_priv != file_priv) {			\
    476 		DRM_ERROR("%s called without lock held\n",		\
    477 			   __FUNCTION__);				\
    478 		return EINVAL;						\
    479 	}								\
    480 } while (0)
    481 
    482 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
    483 /* Returns -errno to shared code */
    484 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
    485 for ( ret = 0 ; !ret && !(condition) ; ) {			\
    486 	DRM_UNLOCK();						\
    487 	mtx_lock(&dev->irq_lock);				\
    488 	if (!(condition))					\
    489 	   ret = -mtx_sleep(&(queue), &dev->irq_lock, 		\
    490 			 PZERO | PCATCH, "drmwtq", (timeout));	\
    491 	mtx_unlock(&dev->irq_lock);				\
    492 	DRM_LOCK();						\
    493 }
    494 #elif defined(__NetBSD__)
    495 /* Returns -errno to shared code */
    496 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
    497 for ( ret = 0 ; !ret && !(condition) ; ) {			\
    498 	DRM_UNLOCK();						\
    499 	mutex_enter(&dev->irq_lock);				\
    500 	if (!(condition))					\
    501 	   ret = -mtsleep(&(queue), PZERO | PCATCH, 		\
    502 			 "drmwtq", (timeout), &dev->irq_lock);	\
    503 	mutex_exit(&dev->irq_lock);				\
    504 	DRM_LOCK();						\
    505 }
    506 #else
    507 /* Returns -errno to shared code */
    508 #define DRM_WAIT_ON( ret, queue, timeout, condition )	\
    509 for ( ret = 0 ; !ret && !(condition) ; ) {		\
    510         int s = spldrm();				\
    511 	if (!(condition))				\
    512 	   ret = -tsleep( &(queue), PZERO | PCATCH, 	\
    513 			 "drmwtq", (timeout) );		\
    514 	splx(s);					\
    515 }
    516 #endif
    517 
    518 #define DRM_ERROR(fmt, arg...) \
    519 	printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
    520 	    DRM_CURRENTPID, __func__ , ## arg)
    521 
    522 #define DRM_INFO(fmt, arg...)  printf("info: [" DRM_NAME "] " fmt , ## arg)
    523 
    524 #define DRM_DEBUG(fmt, arg...) do {					\
    525 	if (drm_debug_flag)						\
    526 		printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
    527 			__func__ , ## arg);				\
    528 } while (0)
    529 
    530 typedef struct drm_pci_id_list
    531 {
    532 	int vendor;
    533 	int device;
    534 	long driver_private;
    535 	const char *name;
    536 } drm_pci_id_list_t;
    537 
    538 #define DRM_AUTH	0x1
    539 #define DRM_MASTER	0x2
    540 #define DRM_ROOT_ONLY	0x4
    541 typedef struct drm_ioctl_desc {
    542 	unsigned long cmd;
    543 	int (*func)(struct drm_device *dev, void *data,
    544 		    struct drm_file *file_priv);
    545 	int flags;
    546 } drm_ioctl_desc_t;
    547 /**
    548  * Creates a driver or general drm_ioctl_desc array entry for the given
    549  * ioctl, for use by drm_ioctl().
    550  */
    551 #define DRM_IOCTL_DEF(ioctl, func, flags) \
    552 	[DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
    553 
    554 typedef struct drm_magic_entry {
    555 	drm_magic_t	       magic;
    556 	struct drm_file	       *priv;
    557 	struct drm_magic_entry *next;
    558 } drm_magic_entry_t;
    559 
    560 typedef struct drm_magic_head {
    561 	struct drm_magic_entry *head;
    562 	struct drm_magic_entry *tail;
    563 } drm_magic_head_t;
    564 
    565 typedef struct drm_buf {
    566 	int		  idx;	       /* Index into master buflist	     */
    567 	int		  total;       /* Buffer size			     */
    568 	int		  order;       /* log-base-2(total)		     */
    569 	int		  used;	       /* Amount of buffer in use (for DMA)  */
    570 	unsigned long	  offset;      /* Byte offset (used internally)	     */
    571 	void		  *address;    /* Address of buffer		     */
    572 	unsigned long	  bus_address; /* Bus address of buffer		     */
    573 	struct drm_buf	  *next;       /* Kernel-only: used for free list    */
    574 	__volatile__ int  pending;     /* On hardware DMA queue		     */
    575 	struct drm_file   *file_priv;  /* Unique identifier of holding process */
    576 	int		  context;     /* Kernel queue for this buffer	     */
    577 	enum {
    578 		DRM_LIST_NONE	 = 0,
    579 		DRM_LIST_FREE	 = 1,
    580 		DRM_LIST_WAIT	 = 2,
    581 		DRM_LIST_PEND	 = 3,
    582 		DRM_LIST_PRIO	 = 4,
    583 		DRM_LIST_RECLAIM = 5
    584 	}		  list;	       /* Which list we're on		     */
    585 
    586 	int		  dev_priv_size; /* Size of buffer private stoarge   */
    587 	void		  *dev_private;  /* Per-buffer private storage       */
    588 } drm_buf_t;
    589 
    590 typedef struct drm_freelist {
    591 	int		  initialized; /* Freelist in use		   */
    592 	atomic_t	  count;       /* Number of free buffers	   */
    593 	drm_buf_t	  *next;       /* End pointer			   */
    594 
    595 	int		  low_mark;    /* Low water mark		   */
    596 	int		  high_mark;   /* High water mark		   */
    597 } drm_freelist_t;
    598 
    599 typedef struct drm_dma_handle {
    600 	void *vaddr;
    601 	bus_addr_t busaddr;
    602 #if defined(__FreeBSD__)
    603 	bus_dma_tag_t tag;
    604 	bus_dmamap_t map;
    605 #elif defined(__NetBSD__)
    606 	bus_dma_tag_t dmat;
    607 	bus_dmamap_t map;
    608 #define DRM_PCI_DMAADDR(p)   ((p)->map->dm_segs[0].ds_addr)
    609 	bus_dma_segment_t segs[1];
    610 	size_t size;
    611 	void *addr;
    612 #endif
    613 } drm_dma_handle_t;
    614 
    615 typedef struct drm_buf_entry {
    616 	int		  buf_size;
    617 	int		  buf_count;
    618 	drm_buf_t	  *buflist;
    619 	int		  seg_count;
    620 	drm_dma_handle_t  **seglist;
    621 	int		  page_order;
    622 
    623 	drm_freelist_t	  freelist;
    624 } drm_buf_entry_t;
    625 
    626 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
    627 struct drm_file {
    628 	TAILQ_ENTRY(drm_file) link;
    629 	int		  authenticated;
    630 	int		  master;
    631 	int		  minor;
    632 	pid_t		  pid;
    633 	uid_t		  uid;
    634 	int		  refs;
    635 	drm_magic_t	  magic;
    636 	unsigned long	  ioctl_count;
    637 	void		 *driver_priv;
    638 };
    639 
    640 typedef struct drm_lock_data {
    641 	drm_hw_lock_t	  *hw_lock;	/* Hardware lock		   */
    642 	struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
    643 	int		  lock_queue;	/* Queue of blocked processes	   */
    644 	unsigned long	  lock_time;	/* Time of last lock in jiffies	   */
    645 } drm_lock_data_t;
    646 
    647 /* This structure, in the struct drm_device, is always initialized while the
    648  * device
    649  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
    650  * when set marks that no further bufs may be allocated until device teardown
    651  * occurs (when the last open of the device has closed).  The high/low
    652  * watermarks of bufs are only touched by the X Server, and thus not
    653  * concurrently accessed, so no locking is needed.
    654  */
    655 typedef struct drm_device_dma {
    656 	drm_buf_entry_t	  bufs[DRM_MAX_ORDER+1];
    657 	int		  buf_count;
    658 	drm_buf_t	  **buflist;	/* Vector of pointers info bufs	   */
    659 	int		  seg_count;
    660 	int		  page_count;
    661 	unsigned long	  *pagelist;
    662 	unsigned long	  byte_count;
    663 	enum {
    664 		_DRM_DMA_USE_AGP = 0x01,
    665 		_DRM_DMA_USE_SG  = 0x02
    666 	} flags;
    667 } drm_device_dma_t;
    668 
    669 typedef struct drm_agp_mem {
    670 	void               *handle;
    671 	unsigned long      bound; /* address */
    672 	int                pages;
    673 	struct drm_agp_mem *prev;
    674 	struct drm_agp_mem *next;
    675 } drm_agp_mem_t;
    676 
    677 typedef struct drm_agp_head {
    678 #ifdef __NetBSD__
    679 	void		   *agpdev;
    680 #else
    681 	device_t	   agpdev;
    682 #endif
    683 	struct agp_info    info;
    684 	const char         *chipset;
    685 	drm_agp_mem_t      *memory;
    686 	unsigned long      mode;
    687 	int                enabled;
    688 	int                acquired;
    689 	unsigned long      base;
    690    	int 		   mtrr;
    691 	int		   cant_use_aperture;
    692 	unsigned long	   page_mask;
    693 } drm_agp_head_t;
    694 
    695 typedef struct drm_sg_mem {
    696 	unsigned long   handle;
    697 	void            *virtual;
    698 	int             pages;
    699 	dma_addr_t	*busaddr;
    700 	drm_dma_handle_t *dmah;	/* Handle to PCI memory for ATI PCIGART table */
    701 } drm_sg_mem_t;
    702 
    703 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
    704 
    705 typedef struct drm_local_map {
    706 	unsigned long	offset;	 /* Physical address (0 for SAREA)*/
    707 	unsigned long	size;	 /* Physical size (bytes)	    */
    708 	drm_map_type_t	type;	 /* Type of memory mapped		    */
    709 	drm_map_flags_t flags;	 /* Flags				    */
    710 	void		*handle; /* User-space: "Handle" to pass to mmap    */
    711 				 /* Kernel-space: kernel-virtual address    */
    712 	int		mtrr;	 /* Boolean: MTRR used */
    713 				 /* Private data			    */
    714 	int		rid;	 /* PCI resource ID for bus_space */
    715 #ifdef __FreeBSD__
    716 	struct resource *bsr;
    717 #endif
    718 	bus_space_tag_t bst;
    719 	bus_space_handle_t bsh;
    720 	drm_dma_handle_t *dmah;
    721 #ifdef __NetBSD__
    722 	int		*cnt;
    723 	bus_size_t	mapsize;
    724 #endif
    725 	TAILQ_ENTRY(drm_local_map) link;
    726 } drm_local_map_t;
    727 
    728 TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
    729 typedef struct drm_vbl_sig {
    730 	TAILQ_ENTRY(drm_vbl_sig) link;
    731 	unsigned int	sequence;
    732 	int		signo;
    733 	int		pid;
    734 } drm_vbl_sig_t;
    735 
    736 /* location of GART table */
    737 #define DRM_ATI_GART_MAIN 1
    738 #define DRM_ATI_GART_FB   2
    739 
    740 #define DRM_ATI_GART_PCI  1
    741 #define DRM_ATI_GART_PCIE 2
    742 #define DRM_ATI_GART_IGP  3
    743 
    744 struct drm_ati_pcigart_info {
    745 	int gart_table_location;
    746 	int gart_reg_if;
    747 	void *addr;
    748 	dma_addr_t bus_addr;
    749 	dma_addr_t table_mask;
    750 	dma_addr_t member_mask;
    751 	struct drm_dma_handle *table_handle;
    752 	drm_local_map_t mapping;
    753 	int table_size;
    754 };
    755 
    756 #ifndef DMA_BIT_MASK
    757 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
    758 #endif
    759 
    760 #define upper_32_bits(_val) (((u64)(_val)) >> 32)
    761 
    762 struct drm_driver_info {
    763 	int	(*load)(struct drm_device *, unsigned long flags);
    764 	int	(*firstopen)(struct drm_device *);
    765 	int	(*open)(struct drm_device *, drm_file_t *);
    766 	void	(*preclose)(struct drm_device *, struct drm_file *file_priv);
    767 	void	(*postclose)(struct drm_device *, drm_file_t *);
    768 	void	(*lastclose)(struct drm_device *);
    769 	int	(*unload)(struct drm_device *);
    770 	void	(*reclaim_buffers_locked)(struct drm_device *,
    771 					  struct drm_file *file_priv);
    772 	int	(*dma_ioctl)(struct drm_device *dev, void *data,
    773 			     struct drm_file *file_priv);
    774 	void	(*dma_ready)(struct drm_device *);
    775 	int	(*dma_quiescent)(struct drm_device *);
    776 	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
    777 					     drm_lock_flags_t flags);
    778 	int	(*dma_flush_unblock)(struct drm_device *, int context,
    779 				     drm_lock_flags_t flags);
    780 	int	(*context_ctor)(struct drm_device *dev, int context);
    781 	int	(*context_dtor)(struct drm_device *dev, int context);
    782 	int	(*kernel_context_switch)(struct drm_device *dev, int old,
    783 					 int new);
    784 	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
    785 	void	(*irq_preinstall)(struct drm_device *dev);
    786 	int	(*irq_postinstall)(struct drm_device *dev);
    787 	void	(*irq_uninstall)(struct drm_device *dev);
    788 	irqreturn_t (*irq_handler)(DRM_IRQ_ARGS);
    789 	u32	(*get_vblank_counter)(struct drm_device *dev, int crtc);
    790 	int	(*enable_vblank)(struct drm_device *dev, int crtc);
    791 	void	(*disable_vblank)(struct drm_device *dev, int crtc);
    792 
    793 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
    794 
    795 	/**
    796 	 * Called by \c drm_device_is_agp.  Typically used to determine if a
    797 	 * card is really attached to AGP or not.
    798 	 *
    799 	 * \param dev  DRM device handle
    800 	 *
    801 	 * \returns
    802 	 * One of three values is returned depending on whether or not the
    803 	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
    804 	 * (return of 1), or may or may not be AGP (return of 2).
    805 	 */
    806 	int	(*device_is_agp) (struct drm_device * dev);
    807 
    808 	drm_ioctl_desc_t *ioctls;
    809 	int	max_ioctl;
    810 
    811 	int	buf_priv_size;
    812 
    813 	int	major;
    814 	int	minor;
    815 	int	patchlevel;
    816 	const char *name;		/* Simple driver name		   */
    817 	const char *desc;		/* Longer driver name		   */
    818 	const char *date;		/* Date of last major changes.	   */
    819 
    820 	unsigned use_agp :1;
    821 	unsigned require_agp :1;
    822 	unsigned use_sg :1;
    823 	unsigned use_dma :1;
    824 	unsigned use_pci_dma :1;
    825 	unsigned use_dma_queue :1;
    826 	unsigned use_irq :1;
    827 	unsigned use_vbl_irq :1;
    828 	unsigned use_vbl_irq2 :1;
    829 	unsigned use_mtrr :1;
    830 };
    831 
    832 /* Length for the array of resource pointers for drm_get_resource_*. */
    833 #define DRM_MAX_PCI_RESOURCE	3
    834 
    835 #if defined(__NetBSD__) || defined(__OpenBSD__)
    836 typedef struct {
    837 	int			mapped;
    838 	int			maptype;
    839 	bus_addr_t		base;
    840 	bus_size_t		size;
    841 	bus_space_handle_t	bsh;
    842 	int			flags;
    843 	void *			vaddr;
    844 } pci_map_data_t;
    845 #endif
    846 
    847 /**
    848  * DRM device functions structure
    849  */
    850 struct drm_device {
    851 #if defined(__NetBSD__) || defined(__OpenBSD__)
    852 	struct device	  device; /* softc is an extension of struct device */
    853 #endif
    854 
    855 	struct drm_driver_info driver;
    856 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
    857 
    858 	u_int16_t pci_device;		/* PCI device id */
    859 	u_int16_t pci_vendor;		/* PCI vendor id */
    860 
    861 	char		  *unique;	/* Unique identifier: e.g., busid  */
    862 	int		  unique_len;	/* Length of unique field	   */
    863 #ifdef __FreeBSD__
    864 	device_t	  device;	/* Device instance from newbus     */
    865 	struct cdev	  *devnode;	/* Device number for mknod	   */
    866 #endif
    867 	int		  if_version;	/* Highest interface version set */
    868 
    869 	int		  flags;	/* Flags to open(2)		   */
    870 
    871 				/* Locks */
    872 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
    873 	struct mtx	  vbl_lock;	/* protects vblank operations */
    874 	struct mtx	  dma_lock;	/* protects dev->dma */
    875 	struct mtx	  irq_lock;	/* protects irq condition checks */
    876 	struct mtx	  dev_lock;	/* protects everything else */
    877 #elif defined(__NetBSD__)
    878 	DRM_SPINTYPE	  vbl_lock;	/* protects vblank operations */
    879 	DRM_SPINTYPE	  dma_lock;	/* protects dev->dma */
    880 	DRM_SPINTYPE	  irq_lock;	/* protects irq condition checks */
    881 	DRM_SPINTYPE      dev_lock;	/* protects everything else */
    882 #endif
    883 	DRM_SPINTYPE	  drw_lock;
    884 	DRM_SPINTYPE	  tsk_lock;
    885 
    886 				/* Usage Counters */
    887 	int		  open_count;	/* Outstanding files open	   */
    888 	int		  buf_use;	/* Buffers in use -- cannot alloc  */
    889 
    890 				/* Performance counters */
    891 	unsigned long     counters;
    892 	drm_stat_type_t   types[15];
    893 	atomic_t          counts[15];
    894 
    895 				/* Authentication */
    896 	drm_file_list_t   files;
    897 	drm_magic_head_t  magiclist[DRM_HASH_SIZE];
    898 
    899 	/* Linked list of mappable regions. Protected by dev_lock */
    900 	drm_map_list_t	  maplist;
    901 
    902 	drm_local_map_t	  **context_sareas;
    903 	int		  max_context;
    904 
    905 	drm_lock_data_t	  lock;		/* Information on hardware lock	   */
    906 
    907 				/* DMA queues (contexts) */
    908 	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
    909 
    910 				/* Context support */
    911 	int		  irq;		/* Interrupt used by board	   */
    912 	int		  irq_enabled;	/* True if the irq handler is enabled */
    913 #ifdef __FreeBSD__
    914 	int		  irqrid;	/* Interrupt used by board */
    915 	struct resource   *irqr;	/* Resource for interrupt used by board	   */
    916 
    917 	/* Storage of resource pointers for drm_get_resource_* */
    918 	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
    919 	int		  pcirid[DRM_MAX_PCI_RESOURCE];
    920 
    921 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    922 	struct pci_attach_args  pa;
    923 	pci_map_data_t	  pci_map_data[DRM_MAX_PCI_RESOURCE];
    924 	pci_map_data_t	  agp_map_data[DRM_MAX_PCI_RESOURCE];
    925 	int		  unit;		/* drm unit number */
    926 	DRM_CDEV	  kdev;
    927 #endif
    928 	void		  *irqh;	/* Handle from bus_setup_intr      */
    929 
    930 	int		  pci_domain;
    931 	int		  pci_bus;
    932 	int		  pci_slot;
    933 	int		  pci_func;
    934 
    935 	atomic_t	  context_flag;	/* Context swapping flag	   */
    936 	int		  last_context;	/* Last current context		   */
    937 	int		  vblank_disable_allowed;
    938 	wait_queue_head_t *vbl_queue;	/* vblank wait queue */
    939 	atomic_t	  *_vblank_count;	/* number of VBLANK interrupts */
    940 						/* (driver must alloc the right number of counters) */
    941 	struct drm_vbl_sig_list *vbl_sigs;	/* signal list to send on VBLANK */
    942 	atomic_t 	  vbl_signal_pending;	/* number of signals pending on all crtcs*/
    943 	atomic_t	  *vblank_refcount;	/* number of users of vblank interrupts per crtc */
    944 	u32		  *last_vblank;		/* protected by dev->vbl_lock, used */
    945 						/* for wraparound handling */
    946 	int		  *vblank_enabled;	/* so we don't call enable more than */
    947 						/* once per disable */
    948 	u32 		  *vblank_premodeset;	/* for compensation of spurious wraparounds */
    949 	int		  *vblank_suspend;	/* Don't wait while crtc is likely disabled */
    950 	struct callout	  vblank_disable_timer;
    951 	u32		  max_vblank_count;	/* size of vblank counter register */
    952 	int		  num_crtcs;
    953 
    954 #ifdef __FreeBSD__
    955 	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
    956 #elif defined(__NetBSD__)
    957 	pid_t		  buf_pgid;
    958 #endif
    959 
    960 				/* Sysctl support */
    961 	struct drm_sysctl_info *sysctl;
    962 
    963 	drm_agp_head_t    *agp;
    964 	drm_sg_mem_t      *sg;  /* Scatter gather memory */
    965 	atomic_t          *ctx_bitmap;
    966 	void		  *dev_private;
    967 	unsigned int	  agp_buffer_token;
    968 	drm_local_map_t   *agp_buffer_map;
    969 
    970 #ifdef __FreeBSD__
    971 	struct unrhdr	  *drw_unrhdr;
    972 #else
    973 	int               drw_no;
    974 #endif
    975 	/* RB tree of drawable infos */
    976 	RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
    977 
    978 #ifdef __FreeBSD__
    979 	struct task	  locked_task;
    980 #endif
    981 #ifdef __NetBSD__
    982 	struct workqueue  *locked_task;
    983 #endif
    984 	void		  (*locked_task_call)(struct drm_device *dev);
    985 };
    986 
    987 extern int	drm_debug_flag;
    988 
    989 /* Device setup support (drm_drv.c) */
    990 #ifdef __FreeBSD__
    991 int	drm_probe(device_t nbdev, drm_pci_id_list_t *idlist);
    992 int	drm_attach(device_t nbdev, drm_pci_id_list_t *idlist);
    993 int	drm_detach(device_t nbdev);
    994 d_ioctl_t drm_ioctl;
    995 d_open_t drm_open;
    996 d_close_t drm_close;
    997 d_read_t drm_read;
    998 d_poll_t drm_poll;
    999 d_mmap_t drm_mmap;
   1000 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1001 int	drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
   1002 void	drm_attach(struct device *kdev, struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
   1003 #if defined(__NetBSD__)
   1004 int     drm_detach(struct device *self, int flags);
   1005 int     drm_activate(struct device *self, enum devact act);
   1006 #endif
   1007 dev_type_ioctl(drm_ioctl);
   1008 dev_type_open(drm_open);
   1009 dev_type_close(drm_close);
   1010 dev_type_read(drm_read);
   1011 dev_type_poll(drm_poll);
   1012 dev_type_mmap(drm_mmap);
   1013 #endif
   1014 extern drm_local_map_t	*drm_getsarea(struct drm_device *dev);
   1015 
   1016 int drm_close_pid(drm_device_t *dev, drm_file_t *priv, pid_t pid);
   1017 
   1018 /* File operations helpers (drm_fops.c) */
   1019 #ifdef __FreeBSD__
   1020 extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
   1021 					 DRM_STRUCTPROC *p,
   1022 					struct drm_device *dev);
   1023 extern drm_file_t	*drm_find_file_by_proc(struct drm_device *dev,
   1024 					       DRM_STRUCTPROC *p);
   1025 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1026 extern int		drm_open_helper(DRM_CDEV kdev, int flags, int fmt,
   1027 					DRM_STRUCTPROC *p, drm_device_t *dev);
   1028 extern drm_file_t	*drm_find_file_by_proc(struct drm_device *dev,
   1029 					       DRM_STRUCTPROC *p);
   1030 #endif /* __NetBSD__ || __OpenBSD__ */
   1031 
   1032 /* Memory management support (drm_memory.c) */
   1033 void	drm_mem_init(void);
   1034 void	drm_mem_uninit(void);
   1035 void	*drm_alloc(size_t size, int area);
   1036 void	*drm_calloc(size_t nmemb, size_t size, int area);
   1037 void	*drm_realloc(void *oldpt, size_t oldsize, size_t size,
   1038 				   int area);
   1039 void	drm_free(void *pt, size_t size, int area);
   1040 void	*drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
   1041 void	drm_ioremapfree(drm_local_map_t *map);
   1042 #ifdef __FreeBSD__
   1043 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
   1044 int	drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
   1045 #else
   1046 #ifndef DRM_NO_MTRR
   1047 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
   1048 int	drm_mtrr_del(unsigned long offset, size_t size, int flags);
   1049 #endif
   1050 #endif /* !__FreeBSD__ */
   1051 
   1052 int	drm_context_switch(struct drm_device *dev, int old, int new);
   1053 int	drm_context_switch_complete(struct drm_device *dev, int new);
   1054 
   1055 int	drm_ctxbitmap_init(struct drm_device *dev);
   1056 void	drm_ctxbitmap_cleanup(struct drm_device *dev);
   1057 void	drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
   1058 int	drm_ctxbitmap_next(struct drm_device *dev);
   1059 
   1060 /* Locking IOCTL support (drm_lock.c) */
   1061 int	drm_lock_take(__volatile__ unsigned int *lock,
   1062 				    unsigned int context);
   1063 int	drm_lock_transfer(struct drm_device *dev,
   1064 			  __volatile__ unsigned int *lock,
   1065 			  unsigned int context);
   1066 int	drm_lock_free(struct drm_device *dev,
   1067 		      __volatile__ unsigned int *lock,
   1068 		      unsigned int context);
   1069 
   1070 /* Buffer management support (drm_bufs.c) */
   1071 unsigned long drm_get_resource_start(struct drm_device *dev,
   1072 				     unsigned int resource);
   1073 unsigned long drm_get_resource_len(struct drm_device *dev,
   1074 				   unsigned int resource);
   1075 void	drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
   1076 int	drm_order(unsigned long size);
   1077 int	drm_addmap(struct drm_device *dev, unsigned long offset,
   1078 		   unsigned long size,
   1079 		   drm_map_type_t type, drm_map_flags_t flags,
   1080 		   drm_local_map_t **map_ptr);
   1081 int	drm_addbufs_pci(struct drm_device *dev, drm_buf_desc_t *request);
   1082 int	drm_addbufs_sg(struct drm_device *dev, drm_buf_desc_t *request);
   1083 int	drm_addbufs_agp(struct drm_device *dev, drm_buf_desc_t *request);
   1084 
   1085 /* DMA support (drm_dma.c) */
   1086 int	drm_dma_setup(struct drm_device *dev);
   1087 void	drm_dma_takedown(struct drm_device *dev);
   1088 void	drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
   1089 void	drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
   1090 #define drm_core_reclaim_buffers drm_reclaim_buffers
   1091 
   1092 /* IRQ support (drm_irq.c) */
   1093 int	drm_irq_install(struct drm_device *dev);
   1094 int	drm_irq_uninstall(struct drm_device *dev);
   1095 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
   1096 void	drm_driver_irq_preinstall(struct drm_device *dev);
   1097 void	drm_driver_irq_postinstall(struct drm_device *dev);
   1098 void	drm_driver_irq_uninstall(struct drm_device *dev);
   1099 void	drm_handle_vblank(struct drm_device *dev, int crtc);
   1100 u32	drm_vblank_count(struct drm_device *dev, int crtc);
   1101 int	drm_vblank_get(struct drm_device *dev, int crtc);
   1102 void	drm_vblank_put(struct drm_device *dev, int crtc);
   1103 void	drm_update_vblank_count(struct drm_device *dev, int crtc);
   1104 int	drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
   1105 int	drm_vblank_init(struct drm_device *dev, int num_crtcs);
   1106 void	drm_vbl_send_signals(struct drm_device *dev, int crtc);
   1107 int 	drm_modeset_ctl(struct drm_device *dev, void *data,
   1108 			struct drm_file *file_priv);
   1109 
   1110 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
   1111 int	drm_device_is_agp(struct drm_device *dev);
   1112 int	drm_device_is_pcie(struct drm_device *dev);
   1113 #ifdef __NetBSD__
   1114 drm_agp_head_t *drm_agp_init(drm_device_t *dev);
   1115 #else
   1116 drm_agp_head_t *drm_agp_init(void);
   1117 #endif
   1118 int	drm_agp_acquire(struct drm_device *dev);
   1119 int	drm_agp_release(struct drm_device *dev);
   1120 int	drm_agp_info(struct drm_device * dev, drm_agp_info_t *info);
   1121 int	drm_agp_enable(struct drm_device *dev, drm_agp_mode_t mode);
   1122 void	*drm_agp_allocate_memory(size_t pages, u32 type);
   1123 int	drm_agp_free_memory(void *handle);
   1124 int	drm_agp_bind_memory(void *handle, off_t start);
   1125 int	drm_agp_unbind_memory(void *handle);
   1126 int	drm_agp_alloc(struct drm_device *dev, drm_agp_buffer_t *request);
   1127 int	drm_agp_free(struct drm_device *dev, drm_agp_buffer_t *request);
   1128 int	drm_agp_bind(struct drm_device *dev, drm_agp_binding_t *request);
   1129 int	drm_agp_unbind(struct drm_device *dev, drm_agp_binding_t *request);
   1130 
   1131 /* Scatter Gather Support (drm_scatter.c) */
   1132 void	drm_sg_cleanup(drm_sg_mem_t *entry);
   1133 int	drm_sg_alloc(struct drm_device *dev, drm_scatter_gather_t * request);
   1134 
   1135 #if defined(__FreeBSD__) || defined(__NetBSD__)
   1136 /* sysctl support (drm_sysctl.h) */
   1137 extern int		drm_sysctl_init(struct drm_device *dev);
   1138 extern int		drm_sysctl_cleanup(struct drm_device *dev);
   1139 #endif /* __FreeBSD__ */
   1140 
   1141 /* ATI PCIGART support (ati_pcigart.c) */
   1142 int	drm_ati_pcigart_init(struct drm_device *dev,
   1143 				struct drm_ati_pcigart_info *gart_info);
   1144 int	drm_ati_pcigart_cleanup(struct drm_device *dev,
   1145 				struct drm_ati_pcigart_info *gart_info);
   1146 
   1147 /* Locking IOCTL support (drm_drv.c) */
   1148 int	drm_lock(struct drm_device *dev, void *data,
   1149 		 struct drm_file *file_priv);
   1150 int	drm_unlock(struct drm_device *dev, void *data,
   1151 		   struct drm_file *file_priv);
   1152 int	drm_version(struct drm_device *dev, void *data,
   1153 		    struct drm_file *file_priv);
   1154 int	drm_setversion(struct drm_device *dev, void *data,
   1155 		       struct drm_file *file_priv);
   1156 
   1157 /* Misc. IOCTL support (drm_ioctl.c) */
   1158 int	drm_irq_by_busid(struct drm_device *dev, void *data,
   1159 			 struct drm_file *file_priv);
   1160 int	drm_getunique(struct drm_device *dev, void *data,
   1161 		      struct drm_file *file_priv);
   1162 int	drm_setunique(struct drm_device *dev, void *data,
   1163 		      struct drm_file *file_priv);
   1164 int	drm_getmap(struct drm_device *dev, void *data,
   1165 		   struct drm_file *file_priv);
   1166 int	drm_getclient(struct drm_device *dev, void *data,
   1167 		      struct drm_file *file_priv);
   1168 int	drm_getstats(struct drm_device *dev, void *data,
   1169 		     struct drm_file *file_priv);
   1170 int	drm_noop(struct drm_device *dev, void *data,
   1171 		 struct drm_file *file_priv);
   1172 
   1173 /* Context IOCTL support (drm_context.c) */
   1174 int	drm_resctx(struct drm_device *dev, void *data,
   1175 		   struct drm_file *file_priv);
   1176 int	drm_addctx(struct drm_device *dev, void *data,
   1177 struct drm_file *file_priv);
   1178 int	drm_modctx(struct drm_device *dev, void *data,
   1179 		   struct drm_file *file_priv);
   1180 int	drm_getctx(struct drm_device *dev, void *data,
   1181 		   struct drm_file *file_priv);
   1182 int	drm_switchctx(struct drm_device *dev, void *data,
   1183 		      struct drm_file *file_priv);
   1184 int	drm_newctx(struct drm_device *dev, void *data,
   1185 		   struct drm_file *file_priv);
   1186 int	drm_rmctx(struct drm_device *dev, void *data,
   1187 		  struct drm_file *file_priv);
   1188 int	drm_setsareactx(struct drm_device *dev, void *data,
   1189 			struct drm_file *file_priv);
   1190 int	drm_getsareactx(struct drm_device *dev, void *data,
   1191 			struct drm_file *file_priv);
   1192 
   1193 /* Drawable IOCTL support (drm_drawable.c) */
   1194 int	drm_adddraw(struct drm_device *dev, void *data,
   1195 		    struct drm_file *file_priv);
   1196 int	drm_rmdraw(struct drm_device *dev, void *data,
   1197 		   struct drm_file *file_priv);
   1198 int	drm_update_draw(struct drm_device *dev, void *data,
   1199 			struct drm_file *file_priv);
   1200 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
   1201 						int handle);
   1202 
   1203 /* Authentication IOCTL support (drm_auth.c) */
   1204 int	drm_getmagic(struct drm_device *dev, void *data,
   1205 		     struct drm_file *file_priv);
   1206 int	drm_authmagic(struct drm_device *dev, void *data,
   1207 		      struct drm_file *file_priv);
   1208 
   1209 /* Buffer management support (drm_bufs.c) */
   1210 int	drm_addmap_ioctl(struct drm_device *dev, void *data,
   1211 			 struct drm_file *file_priv);
   1212 int	drm_rmmap_ioctl(struct drm_device *dev, void *data,
   1213 			struct drm_file *file_priv);
   1214 int	drm_addbufs_ioctl(struct drm_device *dev, void *data,
   1215 			  struct drm_file *file_priv);
   1216 int	drm_infobufs(struct drm_device *dev, void *data,
   1217 		     struct drm_file *file_priv);
   1218 int	drm_markbufs(struct drm_device *dev, void *data,
   1219 		     struct drm_file *file_priv);
   1220 int	drm_freebufs(struct drm_device *dev, void *data,
   1221 		     struct drm_file *file_priv);
   1222 int	drm_mapbufs(struct drm_device *dev, void *data,
   1223 		    struct drm_file *file_priv);
   1224 
   1225 /* DMA support (drm_dma.c) */
   1226 int	drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
   1227 
   1228 /* IRQ support (drm_irq.c) */
   1229 int	drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv);
   1230 int	drm_wait_vblank(struct drm_device *dev, void *data,
   1231 			struct drm_file *file_priv);
   1232 void	drm_locked_tasklet(struct drm_device *dev,
   1233 			   void (*tasklet)(struct drm_device *dev));
   1234 
   1235 /* AGP/GART support (drm_agpsupport.c) */
   1236 int	drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
   1237 			      struct drm_file *file_priv);
   1238 int	drm_agp_release_ioctl(struct drm_device *dev, void *data,
   1239 			      struct drm_file *file_priv);
   1240 int	drm_agp_enable_ioctl(struct drm_device *dev, void *data,
   1241 			     struct drm_file *file_priv);
   1242 int	drm_agp_info_ioctl(struct drm_device *dev, void *data,
   1243 			   struct drm_file *file_priv);
   1244 int	drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
   1245 			    struct drm_file *file_priv);
   1246 int	drm_agp_free_ioctl(struct drm_device *dev, void *data,
   1247 			   struct drm_file *file_priv);
   1248 int	drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
   1249 			     struct drm_file *file_priv);
   1250 int	drm_agp_bind_ioctl(struct drm_device *dev, void *data,
   1251 			   struct drm_file *file_priv);
   1252 
   1253 /* Scatter Gather Support (drm_scatter.c) */
   1254 int	drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
   1255 			   struct drm_file *file_priv);
   1256 int	drm_sg_free(struct drm_device *dev, void *data,
   1257 		    struct drm_file *file_priv);
   1258 
   1259 /* consistent PCI memory functions (drm_pci.c) */
   1260 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
   1261 				size_t align, dma_addr_t maxaddr);
   1262 void	drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
   1263 
   1264 #define drm_core_ioremap_wc drm_core_ioremap
   1265 
   1266 /* Inline replacements for DRM_IOREMAP macros */
   1267 static __inline__ void
   1268 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
   1269 {
   1270 	map->handle = drm_ioremap(dev, map);
   1271 }
   1272 static __inline__ void
   1273 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
   1274 {
   1275 	if ( map->handle && map->size )
   1276 		drm_ioremapfree(map);
   1277 }
   1278 
   1279 static __inline__ struct drm_local_map *
   1280 drm_core_findmap(struct drm_device *dev, unsigned long offset)
   1281 {
   1282 	drm_local_map_t *map;
   1283 
   1284 	DRM_SPINLOCK_ASSERT(&dev->dev_lock);
   1285 	TAILQ_FOREACH(map, &dev->maplist, link) {
   1286 		if (map->offset == offset)
   1287 			return map;
   1288 	}
   1289 	return NULL;
   1290 }
   1291 
   1292 static __inline__ void drm_core_dropmap(struct drm_map *map)
   1293 {
   1294 }
   1295 
   1296 #endif /* __KERNEL__ */
   1297 #endif /* _DRM_P_H_ */
   1298