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