Home | History | Annotate | Line # | Download | only in bsd-core
drmP.h revision 1.9.4.2
      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		1
    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(v)	((vaddr_t)(v) - vm_map_min(kernel_map))
    333 #define DRM_NETBSD_HANDLE2ADDR(u)	((vaddr_t)(u) + vm_map_min(kernel_map))
    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 #define DRM_READ8(map, offset)						\
    406 	*(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset))
    407 #define DRM_READ16(map, offset)						\
    408 	*(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset))
    409 #define DRM_READ32(map, offset)						\
    410 	*(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset))
    411 #define DRM_WRITE8(map, offset, val)					\
    412 	*(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset)) = val
    413 #define DRM_WRITE16(map, offset, val)					\
    414 	*(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset)) = val
    415 #define DRM_WRITE32(map, offset, val)					\
    416 	*(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset)) = val
    417 
    418 #ifdef __FreeBSD__
    419 #define DRM_VERIFYAREA_READ( uaddr, size )		\
    420 	(!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
    421 #else
    422 typedef vaddr_t vm_offset_t;
    423 #define DRM_VERIFYAREA_READ( uaddr, size )		\
    424 	(!uvm_map_checkprot(&(curproc->p_vmspace->vm_map),              \
    425 		(vaddr_t)uaddr, (vaddr_t)uaddr+size, UVM_PROT_READ))
    426 #endif
    427 
    428 #define DRM_COPY_TO_USER(user, kern, size) \
    429 	copyout(kern, user, size)
    430 #define DRM_COPY_FROM_USER(kern, user, size) \
    431 	copyin(user, kern, size)
    432 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) 	\
    433 	copyin(arg2, arg1, arg3)
    434 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)	\
    435 	copyout(arg2, arg1, arg3)
    436 #if __FreeBSD_version > 500000
    437 #define DRM_GET_USER_UNCHECKED(val, uaddr)		\
    438 	((val) = fuword32(uaddr), 0)
    439 #else
    440 #define DRM_GET_USER_UNCHECKED(val, uaddr)		\
    441 	((val) = fuword(uaddr), 0)
    442 #endif
    443 
    444 #define cpu_to_le32(x) htole32(x)
    445 #define le32_to_cpu(x) le32toh(x)
    446 
    447 #define DRM_HZ			hz
    448 #define DRM_UDELAY(udelay)	DELAY(udelay)
    449 #define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
    450 
    451 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {	\
    452 	(_map) = (_dev)->context_sareas[_ctx];		\
    453 } while(0)
    454 
    455 #define LOCK_TEST_WITH_RETURN(dev, file_priv)				\
    456 do {									\
    457 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
    458 	     dev->lock.file_priv != file_priv) {			\
    459 		DRM_ERROR("%s called without lock held\n",		\
    460 			   __FUNCTION__);				\
    461 		return EINVAL;						\
    462 	}								\
    463 } while (0)
    464 
    465 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
    466 /* Returns -errno to shared code */
    467 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
    468 for ( ret = 0 ; !ret && !(condition) ; ) {			\
    469 	DRM_UNLOCK();						\
    470 	mtx_lock(&dev->irq_lock);				\
    471 	if (!(condition))					\
    472 	   ret = -mtx_sleep(&(queue), &dev->irq_lock, 		\
    473 			 PZERO | PCATCH, "drmwtq", (timeout));	\
    474 	mtx_unlock(&dev->irq_lock);				\
    475 	DRM_LOCK();						\
    476 }
    477 #elif defined(__NetBSD__)
    478 /* Returns -errno to shared code */
    479 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
    480 for ( ret = 0 ; !ret && !(condition) ; ) {			\
    481 	DRM_UNLOCK();						\
    482 	mutex_enter(&dev->irq_lock);				\
    483 	if (!(condition))					\
    484 	   ret = -mtsleep(&(queue), PZERO | PCATCH, 		\
    485 			 "drmwtq", (timeout), &dev->irq_lock);	\
    486 	mutex_exit(&dev->irq_lock);				\
    487 	DRM_LOCK();						\
    488 }
    489 #else
    490 /* Returns -errno to shared code */
    491 #define DRM_WAIT_ON( ret, queue, timeout, condition )	\
    492 for ( ret = 0 ; !ret && !(condition) ; ) {		\
    493         int s = spldrm();				\
    494 	if (!(condition))				\
    495 	   ret = -tsleep( &(queue), PZERO | PCATCH, 	\
    496 			 "drmwtq", (timeout) );		\
    497 	splx(s);					\
    498 }
    499 #endif
    500 
    501 #define DRM_ERROR(fmt, arg...) \
    502 	printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
    503 	    DRM_CURRENTPID, __func__ , ## arg)
    504 
    505 #define DRM_INFO(fmt, arg...)  printf("info: [" DRM_NAME "] " fmt , ## arg)
    506 
    507 #define DRM_DEBUG(fmt, arg...) do {					\
    508 	if (drm_debug_flag)						\
    509 		printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
    510 			__func__ , ## arg);				\
    511 } while (0)
    512 
    513 typedef struct drm_pci_id_list
    514 {
    515 	int vendor;
    516 	int device;
    517 	long driver_private;
    518 	const char *name;
    519 } drm_pci_id_list_t;
    520 
    521 #define DRM_AUTH	0x1
    522 #define DRM_MASTER	0x2
    523 #define DRM_ROOT_ONLY	0x4
    524 typedef struct drm_ioctl_desc {
    525 	unsigned long cmd;
    526 	int (*func)(struct drm_device *dev, void *data,
    527 		    struct drm_file *file_priv);
    528 	int flags;
    529 } drm_ioctl_desc_t;
    530 /**
    531  * Creates a driver or general drm_ioctl_desc array entry for the given
    532  * ioctl, for use by drm_ioctl().
    533  */
    534 #define DRM_IOCTL_DEF(ioctl, func, flags) \
    535 	[DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
    536 
    537 typedef struct drm_magic_entry {
    538 	drm_magic_t	       magic;
    539 	struct drm_file	       *priv;
    540 	struct drm_magic_entry *next;
    541 } drm_magic_entry_t;
    542 
    543 typedef struct drm_magic_head {
    544 	struct drm_magic_entry *head;
    545 	struct drm_magic_entry *tail;
    546 } drm_magic_head_t;
    547 
    548 typedef struct drm_buf {
    549 	int		  idx;	       /* Index into master buflist	     */
    550 	int		  total;       /* Buffer size			     */
    551 	int		  order;       /* log-base-2(total)		     */
    552 	int		  used;	       /* Amount of buffer in use (for DMA)  */
    553 	unsigned long	  offset;      /* Byte offset (used internally)	     */
    554 	void		  *address;    /* Address of buffer		     */
    555 	unsigned long	  bus_address; /* Bus address of buffer		     */
    556 	struct drm_buf	  *next;       /* Kernel-only: used for free list    */
    557 	__volatile__ int  pending;     /* On hardware DMA queue		     */
    558 	struct drm_file   *file_priv;  /* Unique identifier of holding process */
    559 	int		  context;     /* Kernel queue for this buffer	     */
    560 	enum {
    561 		DRM_LIST_NONE	 = 0,
    562 		DRM_LIST_FREE	 = 1,
    563 		DRM_LIST_WAIT	 = 2,
    564 		DRM_LIST_PEND	 = 3,
    565 		DRM_LIST_PRIO	 = 4,
    566 		DRM_LIST_RECLAIM = 5
    567 	}		  list;	       /* Which list we're on		     */
    568 
    569 	int		  dev_priv_size; /* Size of buffer private stoarge   */
    570 	void		  *dev_private;  /* Per-buffer private storage       */
    571 } drm_buf_t;
    572 
    573 typedef struct drm_freelist {
    574 	int		  initialized; /* Freelist in use		   */
    575 	atomic_t	  count;       /* Number of free buffers	   */
    576 	drm_buf_t	  *next;       /* End pointer			   */
    577 
    578 	int		  low_mark;    /* Low water mark		   */
    579 	int		  high_mark;   /* High water mark		   */
    580 } drm_freelist_t;
    581 
    582 typedef struct drm_dma_handle {
    583 	void *vaddr;
    584 	bus_addr_t busaddr;
    585 	bus_dma_tag_t tag;
    586 	bus_dmamap_t map;
    587 #if defined(__NetBSD__)
    588 	bus_dma_segment_t segs[1];
    589 	size_t size;
    590 #endif
    591 } drm_dma_handle_t;
    592 
    593 typedef struct drm_buf_entry {
    594 	int		  buf_size;
    595 	int		  buf_count;
    596 	drm_buf_t	  *buflist;
    597 	int		  seg_count;
    598 	drm_dma_handle_t  **seglist;
    599 	int		  page_order;
    600 
    601 	drm_freelist_t	  freelist;
    602 } drm_buf_entry_t;
    603 
    604 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
    605 struct drm_file {
    606 	TAILQ_ENTRY(drm_file) link;
    607 	int		  authenticated;
    608 	int		  master;
    609 	int		  minor;
    610 	pid_t		  pid;
    611 	uid_t		  uid;
    612 	int		  refs;
    613 	drm_magic_t	  magic;
    614 	unsigned long	  ioctl_count;
    615 	void		 *driver_priv;
    616 };
    617 
    618 typedef struct drm_lock_data {
    619 	drm_hw_lock_t	  *hw_lock;	/* Hardware lock		   */
    620 	struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
    621 	int		  lock_queue;	/* Queue of blocked processes	   */
    622 	unsigned long	  lock_time;	/* Time of last lock in jiffies	   */
    623 } drm_lock_data_t;
    624 
    625 /* This structure, in the struct drm_device, is always initialized while the
    626  * device
    627  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
    628  * when set marks that no further bufs may be allocated until device teardown
    629  * occurs (when the last open of the device has closed).  The high/low
    630  * watermarks of bufs are only touched by the X Server, and thus not
    631  * concurrently accessed, so no locking is needed.
    632  */
    633 typedef struct drm_device_dma {
    634 	drm_buf_entry_t	  bufs[DRM_MAX_ORDER+1];
    635 	int		  buf_count;
    636 	drm_buf_t	  **buflist;	/* Vector of pointers info bufs	   */
    637 	int		  seg_count;
    638 	int		  page_count;
    639 	unsigned long	  *pagelist;
    640 	unsigned long	  byte_count;
    641 	enum {
    642 		_DRM_DMA_USE_AGP = 0x01,
    643 		_DRM_DMA_USE_SG  = 0x02
    644 	} flags;
    645 } drm_device_dma_t;
    646 
    647 typedef struct drm_agp_mem {
    648 	void               *handle;
    649 	unsigned long      bound; /* address */
    650 	int                pages;
    651 	struct drm_agp_mem *prev;
    652 	struct drm_agp_mem *next;
    653 } drm_agp_mem_t;
    654 
    655 typedef struct drm_agp_head {
    656 #ifdef __NetBSD__
    657 	void		   *agpdev;
    658 #else
    659 	device_t	   agpdev;
    660 #endif
    661 	struct agp_info    info;
    662 	const char         *chipset;
    663 	drm_agp_mem_t      *memory;
    664 	unsigned long      mode;
    665 	int                enabled;
    666 	int                acquired;
    667 	unsigned long      base;
    668    	int 		   mtrr;
    669 	int		   cant_use_aperture;
    670 	unsigned long	   page_mask;
    671 } drm_agp_head_t;
    672 
    673 typedef struct drm_sg_mem {
    674 	unsigned long   handle;
    675 	void            *virtual;
    676 	int             pages;
    677 	dma_addr_t	*busaddr;
    678 	drm_dma_handle_t *dmah;	/* Handle to PCI memory for ATI PCIGART table */
    679 } drm_sg_mem_t;
    680 
    681 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
    682 
    683 typedef struct drm_local_map {
    684 	unsigned long	offset;	 /* Physical address (0 for SAREA)*/
    685 	unsigned long	size;	 /* Physical size (bytes)	    */
    686 	drm_map_type_t	type;	 /* Type of memory mapped		    */
    687 	drm_map_flags_t flags;	 /* Flags				    */
    688 	void		*handle; /* User-space: "Handle" to pass to mmap    */
    689 				 /* Kernel-space: kernel-virtual address    */
    690 	int		mtrr;	 /* Boolean: MTRR used */
    691 				 /* Private data			    */
    692 	int		rid;	 /* PCI resource ID for bus_space */
    693 #ifdef __FreeBSD__
    694 	struct resource *bsr;
    695 #endif
    696 	bus_space_tag_t bst;
    697 	bus_space_handle_t bsh;
    698 	drm_dma_handle_t *dmah;
    699 #ifdef __NetBSD__
    700 	int		*cnt;
    701 	bus_size_t	mapsize;
    702 #endif
    703 	TAILQ_ENTRY(drm_local_map) link;
    704 } drm_local_map_t;
    705 
    706 TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
    707 typedef struct drm_vbl_sig {
    708 	TAILQ_ENTRY(drm_vbl_sig) link;
    709 	unsigned int	sequence;
    710 	int		signo;
    711 	int		pid;
    712 } drm_vbl_sig_t;
    713 
    714 /* location of GART table */
    715 #define DRM_ATI_GART_MAIN 1
    716 #define DRM_ATI_GART_FB   2
    717 
    718 #define DRM_ATI_GART_PCI  1
    719 #define DRM_ATI_GART_PCIE 2
    720 #define DRM_ATI_GART_IGP  3
    721 
    722 struct drm_ati_pcigart_info {
    723 	int gart_table_location;
    724 	int gart_reg_if;
    725 	void *addr;
    726 	dma_addr_t bus_addr;
    727 	dma_addr_t table_mask;
    728 	dma_addr_t member_mask;
    729 	struct drm_dma_handle *table_handle;
    730 	drm_local_map_t mapping;
    731 	int table_size;
    732 };
    733 
    734 #ifndef DMA_BIT_MASK
    735 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
    736 #endif
    737 
    738 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
    739 
    740 struct drm_driver_info {
    741 	int	(*load)(struct drm_device *, unsigned long flags);
    742 	int	(*firstopen)(struct drm_device *);
    743 	int	(*open)(struct drm_device *, drm_file_t *);
    744 	void	(*preclose)(struct drm_device *, struct drm_file *file_priv);
    745 	void	(*postclose)(struct drm_device *, drm_file_t *);
    746 	void	(*lastclose)(struct drm_device *);
    747 	int	(*unload)(struct drm_device *);
    748 	void	(*reclaim_buffers_locked)(struct drm_device *,
    749 					  struct drm_file *file_priv);
    750 	int	(*dma_ioctl)(struct drm_device *dev, void *data,
    751 			     struct drm_file *file_priv);
    752 	void	(*dma_ready)(struct drm_device *);
    753 	int	(*dma_quiescent)(struct drm_device *);
    754 	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
    755 					     drm_lock_flags_t flags);
    756 	int	(*dma_flush_unblock)(struct drm_device *, int context,
    757 				     drm_lock_flags_t flags);
    758 	int	(*context_ctor)(struct drm_device *dev, int context);
    759 	int	(*context_dtor)(struct drm_device *dev, int context);
    760 	int	(*kernel_context_switch)(struct drm_device *dev, int old,
    761 					 int new);
    762 	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
    763 	void	(*irq_preinstall)(struct drm_device *dev);
    764 	int	(*irq_postinstall)(struct drm_device *dev);
    765 	void	(*irq_uninstall)(struct drm_device *dev);
    766 	irqreturn_t (*irq_handler)(DRM_IRQ_ARGS);
    767 	u32	(*get_vblank_counter)(struct drm_device *dev, int crtc);
    768 	int	(*enable_vblank)(struct drm_device *dev, int crtc);
    769 	void	(*disable_vblank)(struct drm_device *dev, int crtc);
    770 
    771 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
    772 
    773 	/**
    774 	 * Called by \c drm_device_is_agp.  Typically used to determine if a
    775 	 * card is really attached to AGP or not.
    776 	 *
    777 	 * \param dev  DRM device handle
    778 	 *
    779 	 * \returns
    780 	 * One of three values is returned depending on whether or not the
    781 	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
    782 	 * (return of 1), or may or may not be AGP (return of 2).
    783 	 */
    784 	int	(*device_is_agp) (struct drm_device * dev);
    785 
    786 	drm_ioctl_desc_t *ioctls;
    787 	int	max_ioctl;
    788 
    789 	int	buf_priv_size;
    790 
    791 	int	major;
    792 	int	minor;
    793 	int	patchlevel;
    794 	const char *name;		/* Simple driver name		   */
    795 	const char *desc;		/* Longer driver name		   */
    796 	const char *date;		/* Date of last major changes.	   */
    797 
    798 	unsigned use_agp :1;
    799 	unsigned require_agp :1;
    800 	unsigned use_sg :1;
    801 	unsigned use_dma :1;
    802 	unsigned use_pci_dma :1;
    803 	unsigned use_dma_queue :1;
    804 	unsigned use_irq :1;
    805 	unsigned use_vbl_irq :1;
    806 	unsigned use_vbl_irq2 :1;
    807 	unsigned use_mtrr :1;
    808 };
    809 
    810 /* Length for the array of resource pointers for drm_get_resource_*. */
    811 #define DRM_MAX_PCI_RESOURCE	3
    812 
    813 #if defined(__NetBSD__) || defined(__OpenBSD__)
    814 typedef struct {
    815 	int			mapped;
    816 	int			maptype;
    817 	bus_addr_t		base;
    818 	bus_size_t		size;
    819 	bus_space_handle_t	bsh;
    820 	int			flags;
    821 	void *			vaddr;
    822 } pci_map_data_t;
    823 #endif
    824 
    825 /**
    826  * DRM device functions structure
    827  */
    828 struct drm_device {
    829 #if defined(__NetBSD__) || defined(__OpenBSD__)
    830 	struct device	  device; /* softc is an extension of struct device */
    831 #endif
    832 
    833 	struct drm_driver_info driver;
    834 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
    835 
    836 	u_int16_t pci_device;		/* PCI device id */
    837 	u_int16_t pci_vendor;		/* PCI vendor id */
    838 
    839 	char		  *unique;	/* Unique identifier: e.g., busid  */
    840 	int		  unique_len;	/* Length of unique field	   */
    841 #ifdef __FreeBSD__
    842 	device_t	  device;	/* Device instance from newbus     */
    843 	struct cdev	  *devnode;	/* Device number for mknod	   */
    844 #endif
    845 	int		  if_version;	/* Highest interface version set */
    846 
    847 	int		  flags;	/* Flags to open(2)		   */
    848 
    849 				/* Locks */
    850 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
    851 	struct mtx	  vbl_lock;	/* protects vblank operations */
    852 	struct mtx	  dma_lock;	/* protects dev->dma */
    853 	struct mtx	  irq_lock;	/* protects irq condition checks */
    854 	struct mtx	  dev_lock;	/* protects everything else */
    855 #elif defined(__NetBSD__)
    856 	DRM_SPINTYPE	  vbl_lock;	/* protects vblank operations */
    857 	DRM_SPINTYPE	  dma_lock;	/* protects dev->dma */
    858 	DRM_SPINTYPE	  irq_lock;	/* protects irq condition checks */
    859 	DRM_SPINTYPE      dev_lock;	/* protects everything else */
    860 #endif
    861 	DRM_SPINTYPE	  drw_lock;
    862 	DRM_SPINTYPE	  tsk_lock;
    863 
    864 				/* Usage Counters */
    865 	int		  open_count;	/* Outstanding files open	   */
    866 	int		  buf_use;	/* Buffers in use -- cannot alloc  */
    867 
    868 				/* Performance counters */
    869 	unsigned long     counters;
    870 	drm_stat_type_t   types[15];
    871 	atomic_t          counts[15];
    872 
    873 				/* Authentication */
    874 	drm_file_list_t   files;
    875 	drm_magic_head_t  magiclist[DRM_HASH_SIZE];
    876 
    877 	/* Linked list of mappable regions. Protected by dev_lock */
    878 	drm_map_list_t	  maplist;
    879 
    880 	drm_local_map_t	  **context_sareas;
    881 	int		  max_context;
    882 
    883 	drm_lock_data_t	  lock;		/* Information on hardware lock	   */
    884 
    885 				/* DMA queues (contexts) */
    886 	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
    887 
    888 				/* Context support */
    889 	int		  irq;		/* Interrupt used by board	   */
    890 	int		  irq_enabled;	/* True if the irq handler is enabled */
    891 #ifdef __FreeBSD__
    892 	int		  irqrid;	/* Interrupt used by board */
    893 	struct resource   *irqr;	/* Resource for interrupt used by board	   */
    894 
    895 	/* Storage of resource pointers for drm_get_resource_* */
    896 	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
    897 	int		  pcirid[DRM_MAX_PCI_RESOURCE];
    898 
    899 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    900 	struct pci_attach_args  pa;
    901 	pci_map_data_t	  pci_map_data[DRM_MAX_PCI_RESOURCE];
    902 	pci_map_data_t	  agp_map_data[DRM_MAX_PCI_RESOURCE];
    903 	int		  unit;		/* drm unit number */
    904 	DRM_CDEV	  kdev;
    905 #endif
    906 	void		  *irqh;	/* Handle from bus_setup_intr      */
    907 
    908 	int		  pci_domain;
    909 	int		  pci_bus;
    910 	int		  pci_slot;
    911 	int		  pci_func;
    912 
    913 	atomic_t	  context_flag;	/* Context swapping flag	   */
    914 	int		  last_context;	/* Last current context		   */
    915 	int		  vblank_disable_allowed;
    916 	wait_queue_head_t *vbl_queue;	/* vblank wait queue */
    917 	atomic_t	  *_vblank_count;	/* number of VBLANK interrupts */
    918 						/* (driver must alloc the right number of counters) */
    919 	struct drm_vbl_sig_list *vbl_sigs;	/* signal list to send on VBLANK */
    920 	atomic_t 	  vbl_signal_pending;	/* number of signals pending on all crtcs*/
    921 	atomic_t	  *vblank_refcount;	/* number of users of vblank interrupts per crtc */
    922 	u32		  *last_vblank;		/* protected by dev->vbl_lock, used */
    923 						/* for wraparound handling */
    924 	int		  *vblank_enabled;	/* so we don't call enable more than */
    925 						/* once per disable */
    926 	int 		  *vblank_inmodeset;	/* Display driver is setting mode */
    927 	struct callout	  vblank_disable_timer;
    928 	u32		  max_vblank_count;	/* size of vblank counter register */
    929 	int		  num_crtcs;
    930 
    931 #ifdef __FreeBSD__
    932 	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
    933 #elif defined(__NetBSD__)
    934 	pid_t		  buf_pgid;
    935 #endif
    936 
    937 				/* Sysctl support */
    938 	struct drm_sysctl_info *sysctl;
    939 
    940 	drm_agp_head_t    *agp;
    941 	drm_sg_mem_t      *sg;  /* Scatter gather memory */
    942 	atomic_t          *ctx_bitmap;
    943 	void		  *dev_private;
    944 	unsigned int	  agp_buffer_token;
    945 	drm_local_map_t   *agp_buffer_map;
    946 
    947 #ifdef __FreeBSD__
    948 	struct unrhdr	  *drw_unrhdr;
    949 #else
    950 	int               drw_no;
    951 #endif
    952 	/* RB tree of drawable infos */
    953 	RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
    954 
    955 #ifdef __FreeBSD__
    956 	struct task	  locked_task;
    957 #endif
    958 #ifdef __NetBSD__
    959 	struct workqueue  *locked_task;
    960 #endif
    961 	void		  (*locked_task_call)(struct drm_device *dev);
    962 };
    963 
    964 extern int	drm_debug_flag;
    965 
    966 /* Device setup support (drm_drv.c) */
    967 #ifdef __FreeBSD__
    968 int	drm_probe(device_t nbdev, drm_pci_id_list_t *idlist);
    969 int	drm_attach(device_t nbdev, drm_pci_id_list_t *idlist);
    970 int	drm_detach(device_t nbdev);
    971 d_ioctl_t drm_ioctl;
    972 d_open_t drm_open;
    973 d_close_t drm_close;
    974 d_read_t drm_read;
    975 d_poll_t drm_poll;
    976 d_mmap_t drm_mmap;
    977 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    978 int	drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
    979 void	drm_attach(struct device *kdev, struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
    980 #if defined(__NetBSD__)
    981 int     drm_detach(struct device *self, int flags);
    982 int     drm_activate(struct device *self, enum devact act);
    983 #endif
    984 dev_type_ioctl(drm_ioctl);
    985 dev_type_open(drm_open);
    986 dev_type_close(drm_close);
    987 dev_type_read(drm_read);
    988 dev_type_poll(drm_poll);
    989 dev_type_mmap(drm_mmap);
    990 #endif
    991 extern drm_local_map_t	*drm_getsarea(struct drm_device *dev);
    992 
    993 int drm_close_pid(drm_device_t *dev, drm_file_t *priv, pid_t pid);
    994 
    995 /* File operations helpers (drm_fops.c) */
    996 #ifdef __FreeBSD__
    997 extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
    998 					 DRM_STRUCTPROC *p,
    999 					struct drm_device *dev);
   1000 extern drm_file_t	*drm_find_file_by_proc(struct drm_device *dev,
   1001 					       DRM_STRUCTPROC *p);
   1002 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1003 extern int		drm_open_helper(DRM_CDEV kdev, int flags, int fmt,
   1004 					DRM_STRUCTPROC *p, drm_device_t *dev);
   1005 extern drm_file_t	*drm_find_file_by_proc(struct drm_device *dev,
   1006 					       DRM_STRUCTPROC *p);
   1007 #endif /* __NetBSD__ || __OpenBSD__ */
   1008 
   1009 /* Memory management support (drm_memory.c) */
   1010 void	drm_mem_init(void);
   1011 void	drm_mem_uninit(void);
   1012 void	*drm_alloc(size_t size, int area);
   1013 void	*drm_calloc(size_t nmemb, size_t size, int area);
   1014 void	*drm_realloc(void *oldpt, size_t oldsize, size_t size,
   1015 				   int area);
   1016 void	drm_free(void *pt, size_t size, int area);
   1017 void	*drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
   1018 void	drm_ioremapfree(drm_local_map_t *map);
   1019 #ifdef __FreeBSD__
   1020 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
   1021 int	drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
   1022 #else
   1023 #ifndef DRM_NO_MTRR
   1024 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
   1025 int	drm_mtrr_del(unsigned long offset, size_t size, int flags);
   1026 #endif
   1027 #endif /* !__FreeBSD__ */
   1028 
   1029 int	drm_context_switch(struct drm_device *dev, int old, int new);
   1030 int	drm_context_switch_complete(struct drm_device *dev, int new);
   1031 
   1032 int	drm_ctxbitmap_init(struct drm_device *dev);
   1033 void	drm_ctxbitmap_cleanup(struct drm_device *dev);
   1034 void	drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
   1035 int	drm_ctxbitmap_next(struct drm_device *dev);
   1036 
   1037 /* Locking IOCTL support (drm_lock.c) */
   1038 int	drm_lock_take(__volatile__ unsigned int *lock,
   1039 				    unsigned int context);
   1040 int	drm_lock_transfer(struct drm_device *dev,
   1041 			  __volatile__ unsigned int *lock,
   1042 			  unsigned int context);
   1043 int	drm_lock_free(struct drm_device *dev,
   1044 		      __volatile__ unsigned int *lock,
   1045 		      unsigned int context);
   1046 
   1047 /* Buffer management support (drm_bufs.c) */
   1048 unsigned long drm_get_resource_start(struct drm_device *dev,
   1049 				     unsigned int resource);
   1050 unsigned long drm_get_resource_len(struct drm_device *dev,
   1051 				   unsigned int resource);
   1052 void	drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
   1053 int	drm_order(unsigned long size);
   1054 int	drm_addmap(struct drm_device *dev, unsigned long offset,
   1055 		   unsigned long size,
   1056 		   drm_map_type_t type, drm_map_flags_t flags,
   1057 		   drm_local_map_t **map_ptr);
   1058 int	drm_addbufs_pci(struct drm_device *dev, drm_buf_desc_t *request);
   1059 int	drm_addbufs_sg(struct drm_device *dev, drm_buf_desc_t *request);
   1060 int	drm_addbufs_agp(struct drm_device *dev, drm_buf_desc_t *request);
   1061 
   1062 /* DMA support (drm_dma.c) */
   1063 int	drm_dma_setup(struct drm_device *dev);
   1064 void	drm_dma_takedown(struct drm_device *dev);
   1065 void	drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
   1066 void	drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
   1067 #define drm_core_reclaim_buffers drm_reclaim_buffers
   1068 
   1069 /* IRQ support (drm_irq.c) */
   1070 int	drm_irq_install(struct drm_device *dev);
   1071 int	drm_irq_uninstall(struct drm_device *dev);
   1072 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
   1073 void	drm_driver_irq_preinstall(struct drm_device *dev);
   1074 void	drm_driver_irq_postinstall(struct drm_device *dev);
   1075 void	drm_driver_irq_uninstall(struct drm_device *dev);
   1076 void	drm_handle_vblank(struct drm_device *dev, int crtc);
   1077 u32	drm_vblank_count(struct drm_device *dev, int crtc);
   1078 int	drm_vblank_get(struct drm_device *dev, int crtc);
   1079 void	drm_vblank_put(struct drm_device *dev, int crtc);
   1080 int	drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
   1081 int	drm_vblank_init(struct drm_device *dev, int num_crtcs);
   1082 void	drm_vbl_send_signals(struct drm_device *dev, int crtc);
   1083 int 	drm_modeset_ctl(struct drm_device *dev, void *data,
   1084 			struct drm_file *file_priv);
   1085 
   1086 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
   1087 int	drm_device_is_agp(struct drm_device *dev);
   1088 int	drm_device_is_pcie(struct drm_device *dev);
   1089 #ifdef __NetBSD__
   1090 drm_agp_head_t *drm_agp_init(drm_device_t *dev);
   1091 #else
   1092 drm_agp_head_t *drm_agp_init(void);
   1093 #endif
   1094 int	drm_agp_acquire(struct drm_device *dev);
   1095 int	drm_agp_release(struct drm_device *dev);
   1096 int	drm_agp_info(struct drm_device * dev, drm_agp_info_t *info);
   1097 int	drm_agp_enable(struct drm_device *dev, drm_agp_mode_t mode);
   1098 void	*drm_agp_allocate_memory(size_t pages, u32 type);
   1099 int	drm_agp_free_memory(void *handle);
   1100 int	drm_agp_bind_memory(void *handle, off_t start);
   1101 int	drm_agp_unbind_memory(void *handle);
   1102 int	drm_agp_alloc(struct drm_device *dev, drm_agp_buffer_t *request);
   1103 int	drm_agp_free(struct drm_device *dev, drm_agp_buffer_t *request);
   1104 int	drm_agp_bind(struct drm_device *dev, drm_agp_binding_t *request);
   1105 int	drm_agp_unbind(struct drm_device *dev, drm_agp_binding_t *request);
   1106 
   1107 /* Scatter Gather Support (drm_scatter.c) */
   1108 void	drm_sg_cleanup(drm_sg_mem_t *entry);
   1109 int	drm_sg_alloc(struct drm_device *dev, drm_scatter_gather_t * request);
   1110 
   1111 #if defined(__FreeBSD__) || defined(__NetBSD__)
   1112 /* sysctl support (drm_sysctl.h) */
   1113 extern int		drm_sysctl_init(struct drm_device *dev);
   1114 extern int		drm_sysctl_cleanup(struct drm_device *dev);
   1115 #endif /* __FreeBSD__ */
   1116 
   1117 /* ATI PCIGART support (ati_pcigart.c) */
   1118 int	drm_ati_pcigart_init(struct drm_device *dev,
   1119 				struct drm_ati_pcigart_info *gart_info);
   1120 int	drm_ati_pcigart_cleanup(struct drm_device *dev,
   1121 				struct drm_ati_pcigart_info *gart_info);
   1122 
   1123 /* Locking IOCTL support (drm_drv.c) */
   1124 int	drm_lock(struct drm_device *dev, void *data,
   1125 		 struct drm_file *file_priv);
   1126 int	drm_unlock(struct drm_device *dev, void *data,
   1127 		   struct drm_file *file_priv);
   1128 int	drm_version(struct drm_device *dev, void *data,
   1129 		    struct drm_file *file_priv);
   1130 int	drm_setversion(struct drm_device *dev, void *data,
   1131 		       struct drm_file *file_priv);
   1132 
   1133 /* Misc. IOCTL support (drm_ioctl.c) */
   1134 int	drm_irq_by_busid(struct drm_device *dev, void *data,
   1135 			 struct drm_file *file_priv);
   1136 int	drm_getunique(struct drm_device *dev, void *data,
   1137 		      struct drm_file *file_priv);
   1138 int	drm_setunique(struct drm_device *dev, void *data,
   1139 		      struct drm_file *file_priv);
   1140 int	drm_getmap(struct drm_device *dev, void *data,
   1141 		   struct drm_file *file_priv);
   1142 int	drm_getclient(struct drm_device *dev, void *data,
   1143 		      struct drm_file *file_priv);
   1144 int	drm_getstats(struct drm_device *dev, void *data,
   1145 		     struct drm_file *file_priv);
   1146 int	drm_noop(struct drm_device *dev, void *data,
   1147 		 struct drm_file *file_priv);
   1148 
   1149 /* Context IOCTL support (drm_context.c) */
   1150 int	drm_resctx(struct drm_device *dev, void *data,
   1151 		   struct drm_file *file_priv);
   1152 int	drm_addctx(struct drm_device *dev, void *data,
   1153 struct drm_file *file_priv);
   1154 int	drm_modctx(struct drm_device *dev, void *data,
   1155 		   struct drm_file *file_priv);
   1156 int	drm_getctx(struct drm_device *dev, void *data,
   1157 		   struct drm_file *file_priv);
   1158 int	drm_switchctx(struct drm_device *dev, void *data,
   1159 		      struct drm_file *file_priv);
   1160 int	drm_newctx(struct drm_device *dev, void *data,
   1161 		   struct drm_file *file_priv);
   1162 int	drm_rmctx(struct drm_device *dev, void *data,
   1163 		  struct drm_file *file_priv);
   1164 int	drm_setsareactx(struct drm_device *dev, void *data,
   1165 			struct drm_file *file_priv);
   1166 int	drm_getsareactx(struct drm_device *dev, void *data,
   1167 			struct drm_file *file_priv);
   1168 
   1169 /* Drawable IOCTL support (drm_drawable.c) */
   1170 int	drm_adddraw(struct drm_device *dev, void *data,
   1171 		    struct drm_file *file_priv);
   1172 int	drm_rmdraw(struct drm_device *dev, void *data,
   1173 		   struct drm_file *file_priv);
   1174 int	drm_update_draw(struct drm_device *dev, void *data,
   1175 			struct drm_file *file_priv);
   1176 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
   1177 						int handle);
   1178 
   1179 /* Authentication IOCTL support (drm_auth.c) */
   1180 int	drm_getmagic(struct drm_device *dev, void *data,
   1181 		     struct drm_file *file_priv);
   1182 int	drm_authmagic(struct drm_device *dev, void *data,
   1183 		      struct drm_file *file_priv);
   1184 
   1185 /* Buffer management support (drm_bufs.c) */
   1186 int	drm_addmap_ioctl(struct drm_device *dev, void *data,
   1187 			 struct drm_file *file_priv);
   1188 int	drm_rmmap_ioctl(struct drm_device *dev, void *data,
   1189 			struct drm_file *file_priv);
   1190 int	drm_addbufs_ioctl(struct drm_device *dev, void *data,
   1191 			  struct drm_file *file_priv);
   1192 int	drm_infobufs(struct drm_device *dev, void *data,
   1193 		     struct drm_file *file_priv);
   1194 int	drm_markbufs(struct drm_device *dev, void *data,
   1195 		     struct drm_file *file_priv);
   1196 int	drm_freebufs(struct drm_device *dev, void *data,
   1197 		     struct drm_file *file_priv);
   1198 int	drm_mapbufs(struct drm_device *dev, void *data,
   1199 		    struct drm_file *file_priv);
   1200 
   1201 /* DMA support (drm_dma.c) */
   1202 int	drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
   1203 
   1204 /* IRQ support (drm_irq.c) */
   1205 int	drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv);
   1206 int	drm_wait_vblank(struct drm_device *dev, void *data,
   1207 			struct drm_file *file_priv);
   1208 void	drm_locked_tasklet(struct drm_device *dev,
   1209 			   void (*tasklet)(struct drm_device *dev));
   1210 
   1211 /* AGP/GART support (drm_agpsupport.c) */
   1212 int	drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
   1213 			      struct drm_file *file_priv);
   1214 int	drm_agp_release_ioctl(struct drm_device *dev, void *data,
   1215 			      struct drm_file *file_priv);
   1216 int	drm_agp_enable_ioctl(struct drm_device *dev, void *data,
   1217 			     struct drm_file *file_priv);
   1218 int	drm_agp_info_ioctl(struct drm_device *dev, void *data,
   1219 			   struct drm_file *file_priv);
   1220 int	drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
   1221 			    struct drm_file *file_priv);
   1222 int	drm_agp_free_ioctl(struct drm_device *dev, void *data,
   1223 			   struct drm_file *file_priv);
   1224 int	drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
   1225 			     struct drm_file *file_priv);
   1226 int	drm_agp_bind_ioctl(struct drm_device *dev, void *data,
   1227 			   struct drm_file *file_priv);
   1228 
   1229 /* Scatter Gather Support (drm_scatter.c) */
   1230 int	drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
   1231 			   struct drm_file *file_priv);
   1232 int	drm_sg_free(struct drm_device *dev, void *data,
   1233 		    struct drm_file *file_priv);
   1234 
   1235 /* consistent PCI memory functions (drm_pci.c) */
   1236 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
   1237 				size_t align, dma_addr_t maxaddr);
   1238 void	drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
   1239 
   1240 #define drm_core_ioremap_wc drm_core_ioremap
   1241 
   1242 /* Inline replacements for DRM_IOREMAP macros */
   1243 static __inline__ void
   1244 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
   1245 {
   1246 	map->handle = drm_ioremap(dev, map);
   1247 }
   1248 static __inline__ void
   1249 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
   1250 {
   1251 	if ( map->handle && map->size )
   1252 		drm_ioremapfree(map);
   1253 }
   1254 
   1255 static __inline__ struct drm_local_map *
   1256 drm_core_findmap(struct drm_device *dev, unsigned long offset)
   1257 {
   1258 	drm_local_map_t *map;
   1259 
   1260 	DRM_SPINLOCK_ASSERT(&dev->dev_lock);
   1261 	TAILQ_FOREACH(map, &dev->maplist, link) {
   1262 		if (map->offset == offset)
   1263 			return map;
   1264 	}
   1265 	return NULL;
   1266 }
   1267 
   1268 static __inline__ void drm_core_dropmap(struct drm_map *map)
   1269 {
   1270 }
   1271 
   1272 #endif /* __KERNEL__ */
   1273 #endif /* _DRM_P_H_ */
   1274