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