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