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