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