drmP.h revision 189911
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 189911 2009-03-17 03:44:36Z 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 <vm/vm.h>
67#include <vm/pmap.h>
68#include <vm/vm_extern.h>
69#include <vm/vm_map.h>
70#include <vm/vm_param.h>
71#include <machine/param.h>
72#include <machine/pmap.h>
73#include <machine/bus.h>
74#include <machine/resource.h>
75#include <machine/specialreg.h>
76#include <machine/sysarch.h>
77#include <sys/endian.h>
78#include <sys/mman.h>
79#include <sys/rman.h>
80#include <sys/memrange.h>
81#if __FreeBSD_version >= 800004
82#include <dev/agp/agpvar.h>
83#else /* __FreeBSD_version >= 800004 */
84#include <pci/agpvar.h>
85#endif /* __FreeBSD_version >= 800004 */
86#include <sys/agpio.h>
87#include <sys/mutex.h>
88#include <dev/pci/pcivar.h>
89#include <dev/pci/pcireg.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
214typedef unsigned long dma_addr_t;
215typedef u_int64_t u64;
216typedef u_int32_t u32;
217typedef u_int16_t u16;
218typedef u_int8_t u8;
219
220/* DRM_READMEMORYBARRIER() prevents reordering of reads.
221 * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
222 * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
223 */
224#if defined(__i386__)
225#define DRM_READMEMORYBARRIER()		__asm __volatile( \
226					"lock; addl $0,0(%%esp)" : : : "memory");
227#define DRM_WRITEMEMORYBARRIER()	__asm __volatile("" : : : "memory");
228#define DRM_MEMORYBARRIER()		__asm __volatile( \
229					"lock; addl $0,0(%%esp)" : : : "memory");
230#elif defined(__alpha__)
231#define DRM_READMEMORYBARRIER()		alpha_mb();
232#define DRM_WRITEMEMORYBARRIER()	alpha_wmb();
233#define DRM_MEMORYBARRIER()		alpha_mb();
234#elif defined(__amd64__)
235#define DRM_READMEMORYBARRIER()		__asm __volatile( \
236					"lock; addl $0,0(%%rsp)" : : : "memory");
237#define DRM_WRITEMEMORYBARRIER()	__asm __volatile("" : : : "memory");
238#define DRM_MEMORYBARRIER()		__asm __volatile( \
239					"lock; addl $0,0(%%rsp)" : : : "memory");
240#endif
241
242#define DRM_READ8(map, offset)						\
243	*(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +		\
244	    (vm_offset_t)(offset))
245#define DRM_READ16(map, offset)						\
246	*(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +		\
247	    (vm_offset_t)(offset))
248#define DRM_READ32(map, offset)						\
249	*(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +		\
250	    (vm_offset_t)(offset))
251#define DRM_WRITE8(map, offset, val)					\
252	*(volatile u_int8_t *)(((vm_offset_t)(map)->handle) +		\
253	    (vm_offset_t)(offset)) = val
254#define DRM_WRITE16(map, offset, val)					\
255	*(volatile u_int16_t *)(((vm_offset_t)(map)->handle) +		\
256	    (vm_offset_t)(offset)) = val
257#define DRM_WRITE32(map, offset, val)					\
258	*(volatile u_int32_t *)(((vm_offset_t)(map)->handle) +		\
259	    (vm_offset_t)(offset)) = val
260
261#define DRM_VERIFYAREA_READ( uaddr, size )		\
262	(!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
263
264#define DRM_COPY_TO_USER(user, kern, size) \
265	copyout(kern, user, size)
266#define DRM_COPY_FROM_USER(kern, user, size) \
267	copyin(user, kern, size)
268#define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) 	\
269	copyin(arg2, arg1, arg3)
270#define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)	\
271	copyout(arg2, arg1, arg3)
272#define DRM_GET_USER_UNCHECKED(val, uaddr)		\
273	((val) = fuword32(uaddr), 0)
274
275#define cpu_to_le32(x) htole32(x)
276#define le32_to_cpu(x) le32toh(x)
277
278#define DRM_HZ			hz
279#define DRM_UDELAY(udelay)	DELAY(udelay)
280#define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
281
282#define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {	\
283	(_map) = (_dev)->context_sareas[_ctx];		\
284} while(0)
285
286#define LOCK_TEST_WITH_RETURN(dev, file_priv)				\
287do {									\
288	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
289	     dev->lock.file_priv != file_priv) {			\
290		DRM_ERROR("%s called without lock held\n",		\
291			   __FUNCTION__);				\
292		return EINVAL;						\
293	}								\
294} while (0)
295
296/* Returns -errno to shared code */
297#define DRM_WAIT_ON( ret, queue, timeout, condition )		\
298for ( ret = 0 ; !ret && !(condition) ; ) {			\
299	DRM_UNLOCK();						\
300	mtx_lock(&dev->irq_lock);				\
301	if (!(condition))					\
302	    ret = -mtx_sleep(&(queue), &dev->irq_lock, 		\
303		PCATCH, "drmwtq", (timeout));			\
304	mtx_unlock(&dev->irq_lock);				\
305	DRM_LOCK();						\
306}
307
308#define DRM_ERROR(fmt, ...) \
309	printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
310	    DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
311
312#define DRM_INFO(fmt, ...)  printf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
313
314#define DRM_DEBUG(fmt, ...) do {					\
315	if (drm_debug_flag)						\
316		printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
317			__func__ , ##__VA_ARGS__);			\
318} while (0)
319
320typedef struct drm_pci_id_list
321{
322	int vendor;
323	int device;
324	long driver_private;
325	char *name;
326} drm_pci_id_list_t;
327
328struct drm_msi_blacklist_entry
329{
330	int vendor;
331	int device;
332};
333
334#define DRM_AUTH	0x1
335#define DRM_MASTER	0x2
336#define DRM_ROOT_ONLY	0x4
337typedef struct drm_ioctl_desc {
338	unsigned long cmd;
339	int (*func)(struct drm_device *dev, void *data,
340		    struct drm_file *file_priv);
341	int flags;
342} drm_ioctl_desc_t;
343/**
344 * Creates a driver or general drm_ioctl_desc array entry for the given
345 * ioctl, for use by drm_ioctl().
346 */
347#define DRM_IOCTL_DEF(ioctl, func, flags) \
348	[DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
349
350typedef struct drm_magic_entry {
351	drm_magic_t	       magic;
352	struct drm_file	       *priv;
353	struct drm_magic_entry *next;
354} drm_magic_entry_t;
355
356typedef struct drm_magic_head {
357	struct drm_magic_entry *head;
358	struct drm_magic_entry *tail;
359} drm_magic_head_t;
360
361typedef struct drm_buf {
362	int		  idx;	       /* Index into master buflist	     */
363	int		  total;       /* Buffer size			     */
364	int		  order;       /* log-base-2(total)		     */
365	int		  used;	       /* Amount of buffer in use (for DMA)  */
366	unsigned long	  offset;      /* Byte offset (used internally)	     */
367	void		  *address;    /* Address of buffer		     */
368	unsigned long	  bus_address; /* Bus address of buffer		     */
369	struct drm_buf	  *next;       /* Kernel-only: used for free list    */
370	__volatile__ int  pending;     /* On hardware DMA queue		     */
371	struct drm_file   *file_priv;  /* Unique identifier of holding process */
372	int		  context;     /* Kernel queue for this buffer	     */
373	enum {
374		DRM_LIST_NONE	 = 0,
375		DRM_LIST_FREE	 = 1,
376		DRM_LIST_WAIT	 = 2,
377		DRM_LIST_PEND	 = 3,
378		DRM_LIST_PRIO	 = 4,
379		DRM_LIST_RECLAIM = 5
380	}		  list;	       /* Which list we're on		     */
381
382	int		  dev_priv_size; /* Size of buffer private stoarge   */
383	void		  *dev_private;  /* Per-buffer private storage       */
384} drm_buf_t;
385
386typedef struct drm_freelist {
387	int		  initialized; /* Freelist in use		   */
388	atomic_t	  count;       /* Number of free buffers	   */
389	drm_buf_t	  *next;       /* End pointer			   */
390
391	int		  low_mark;    /* Low water mark		   */
392	int		  high_mark;   /* High water mark		   */
393} drm_freelist_t;
394
395typedef struct drm_dma_handle {
396	void *vaddr;
397	bus_addr_t busaddr;
398	bus_dma_tag_t tag;
399	bus_dmamap_t map;
400} drm_dma_handle_t;
401
402typedef struct drm_buf_entry {
403	int		  buf_size;
404	int		  buf_count;
405	drm_buf_t	  *buflist;
406	int		  seg_count;
407	drm_dma_handle_t  **seglist;
408	int		  page_order;
409
410	drm_freelist_t	  freelist;
411} drm_buf_entry_t;
412
413typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
414struct drm_file {
415	TAILQ_ENTRY(drm_file) link;
416	struct drm_device *dev;
417	int		  authenticated;
418	int		  master;
419	int		  minor;
420	pid_t		  pid;
421	uid_t		  uid;
422	drm_magic_t	  magic;
423	unsigned long	  ioctl_count;
424	void		 *driver_priv;
425};
426
427typedef struct drm_lock_data {
428	struct drm_hw_lock	*hw_lock;	/* Hardware lock		   */
429	struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
430	int		  lock_queue;	/* Queue of blocked processes	   */
431	unsigned long	  lock_time;	/* Time of last lock in jiffies	   */
432} drm_lock_data_t;
433
434/* This structure, in the struct drm_device, is always initialized while the
435 * device
436 * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
437 * when set marks that no further bufs may be allocated until device teardown
438 * occurs (when the last open of the device has closed).  The high/low
439 * watermarks of bufs are only touched by the X Server, and thus not
440 * concurrently accessed, so no locking is needed.
441 */
442typedef struct drm_device_dma {
443	drm_buf_entry_t	  bufs[DRM_MAX_ORDER+1];
444	int		  buf_count;
445	drm_buf_t	  **buflist;	/* Vector of pointers info bufs	   */
446	int		  seg_count;
447	int		  page_count;
448	unsigned long	  *pagelist;
449	unsigned long	  byte_count;
450	enum {
451		_DRM_DMA_USE_AGP = 0x01,
452		_DRM_DMA_USE_SG  = 0x02
453	} flags;
454} drm_device_dma_t;
455
456typedef struct drm_agp_mem {
457	void               *handle;
458	unsigned long      bound; /* address */
459	int                pages;
460	struct drm_agp_mem *prev;
461	struct drm_agp_mem *next;
462} drm_agp_mem_t;
463
464typedef struct drm_agp_head {
465	device_t	   agpdev;
466	struct agp_info    info;
467	const char         *chipset;
468	drm_agp_mem_t      *memory;
469	unsigned long      mode;
470	int                enabled;
471	int                acquired;
472	unsigned long      base;
473   	int 		   mtrr;
474	int		   cant_use_aperture;
475	unsigned long	   page_mask;
476} drm_agp_head_t;
477
478typedef struct drm_sg_mem {
479	unsigned long		  handle;
480	void			 *virtual;
481	int			  pages;
482	dma_addr_t		 *busaddr;
483	struct drm_dma_handle	 *sg_dmah;	/* Handle for sg_pages   */
484	struct drm_dma_handle	 *dmah;		/* Handle to PCI memory  */
485						/* for ATI PCIGART table */
486} drm_sg_mem_t;
487
488typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
489
490typedef struct drm_local_map {
491	unsigned long	offset;	 /* Physical address (0 for SAREA)*/
492	unsigned long	size;	 /* Physical size (bytes)	    */
493	enum drm_map_type	type;	 /* Type of memory mapped		    */
494	enum drm_map_flags	flags;	 /* Flags				    */
495	void		*handle; /* User-space: "Handle" to pass to mmap    */
496				 /* Kernel-space: kernel-virtual address    */
497	int		mtrr;	 /* Boolean: MTRR used */
498				 /* Private data			    */
499	int		rid;	 /* PCI resource ID for bus_space */
500	struct resource *bsr;
501	bus_space_tag_t bst;
502	bus_space_handle_t bsh;
503	drm_dma_handle_t *dmah;
504	TAILQ_ENTRY(drm_local_map) link;
505} drm_local_map_t;
506
507TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
508typedef struct drm_vbl_sig {
509	TAILQ_ENTRY(drm_vbl_sig) link;
510	unsigned int	sequence;
511	int		signo;
512	int		pid;
513} drm_vbl_sig_t;
514
515struct drm_vblank_info {
516	wait_queue_head_t queue;	/* vblank wait queue */
517	atomic_t count;			/* number of VBLANK interrupts */
518					/* (driver must alloc the right number of counters) */
519	struct drm_vbl_sig_list sigs;	/* signal list to send on VBLANK */
520	atomic_t refcount;		/* number of users of vblank interrupts */
521	u32 last;			/* protected by dev->vbl_lock, used */
522					/* for wraparound handling */
523	int enabled;			/* so we don't call enable more than */
524					/* once per disable */
525	int inmodeset;			/* Display driver is setting mode */
526};
527
528/* location of GART table */
529#define DRM_ATI_GART_MAIN 1
530#define DRM_ATI_GART_FB   2
531
532#define DRM_ATI_GART_PCI  1
533#define DRM_ATI_GART_PCIE 2
534#define DRM_ATI_GART_IGP  3
535
536struct drm_ati_pcigart_info {
537	int gart_table_location;
538	int gart_reg_if;
539	void *addr;
540	dma_addr_t bus_addr;
541	dma_addr_t table_mask;
542	dma_addr_t member_mask;
543	struct drm_dma_handle *table_handle;
544	drm_local_map_t mapping;
545	int table_size;
546};
547
548#ifndef DMA_BIT_MASK
549#define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
550#endif
551
552#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
553
554struct drm_driver_info {
555	int	(*load)(struct drm_device *, unsigned long flags);
556	int	(*firstopen)(struct drm_device *);
557	int	(*open)(struct drm_device *, struct drm_file *);
558	void	(*preclose)(struct drm_device *, struct drm_file *file_priv);
559	void	(*postclose)(struct drm_device *, struct drm_file *);
560	void	(*lastclose)(struct drm_device *);
561	int	(*unload)(struct drm_device *);
562	void	(*reclaim_buffers_locked)(struct drm_device *,
563					  struct drm_file *file_priv);
564	int	(*dma_ioctl)(struct drm_device *dev, void *data,
565			     struct drm_file *file_priv);
566	void	(*dma_ready)(struct drm_device *);
567	int	(*dma_quiescent)(struct drm_device *);
568	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
569					     enum drm_lock_flags flags);
570	int	(*dma_flush_unblock)(struct drm_device *, int context,
571				     enum drm_lock_flags flags);
572	int	(*context_ctor)(struct drm_device *dev, int context);
573	int	(*context_dtor)(struct drm_device *dev, int context);
574	int	(*kernel_context_switch)(struct drm_device *dev, int old,
575					 int new);
576	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
577	void	(*irq_preinstall)(struct drm_device *dev);
578	int	(*irq_postinstall)(struct drm_device *dev);
579	void	(*irq_uninstall)(struct drm_device *dev);
580	void	(*irq_handler)(DRM_IRQ_ARGS);
581	u32	(*get_vblank_counter)(struct drm_device *dev, int crtc);
582	int	(*enable_vblank)(struct drm_device *dev, int crtc);
583	void	(*disable_vblank)(struct drm_device *dev, int crtc);
584
585	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
586
587	/**
588	 * Called by \c drm_device_is_agp.  Typically used to determine if a
589	 * card is really attached to AGP or not.
590	 *
591	 * \param dev  DRM device handle
592	 *
593	 * \returns
594	 * One of three values is returned depending on whether or not the
595	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
596	 * (return of 1), or may or may not be AGP (return of 2).
597	 */
598	int	(*device_is_agp) (struct drm_device * dev);
599
600	drm_ioctl_desc_t *ioctls;
601	int	max_ioctl;
602
603	int	buf_priv_size;
604
605	int	major;
606	int	minor;
607	int	patchlevel;
608	const char *name;		/* Simple driver name		   */
609	const char *desc;		/* Longer driver name		   */
610	const char *date;		/* Date of last major changes.	   */
611
612	u32 driver_features;
613};
614
615/* Length for the array of resource pointers for drm_get_resource_*. */
616#define DRM_MAX_PCI_RESOURCE	6
617
618/**
619 * DRM device functions structure
620 */
621struct drm_device {
622	struct drm_driver_info *driver;
623	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
624
625	u_int16_t pci_device;		/* PCI device id */
626	u_int16_t pci_vendor;		/* PCI vendor id */
627
628	char		  *unique;	/* Unique identifier: e.g., busid  */
629	int		  unique_len;	/* Length of unique field	   */
630	device_t	  device;	/* Device instance from newbus     */
631	struct cdev	  *devnode;	/* Device number for mknod	   */
632	int		  if_version;	/* Highest interface version set */
633
634	int		  flags;	/* Flags to open(2)		   */
635
636				/* Locks */
637	struct mtx	  vbl_lock;	/* protects vblank operations */
638	struct mtx	  dma_lock;	/* protects dev->dma */
639	struct mtx	  irq_lock;	/* protects irq condition checks */
640	struct mtx	  dev_lock;	/* protects everything else */
641	DRM_SPINTYPE	  drw_lock;
642
643				/* Usage Counters */
644	int		  open_count;	/* Outstanding files open	   */
645	int		  buf_use;	/* Buffers in use -- cannot alloc  */
646
647				/* Performance counters */
648	unsigned long     counters;
649	enum drm_stat_type	types[15];
650	atomic_t          counts[15];
651
652				/* Authentication */
653	drm_file_list_t   files;
654	drm_magic_head_t  magiclist[DRM_HASH_SIZE];
655
656	/* Linked list of mappable regions. Protected by dev_lock */
657	drm_map_list_t	  maplist;
658
659	drm_local_map_t	  **context_sareas;
660	int		  max_context;
661
662	drm_lock_data_t	  lock;		/* Information on hardware lock	   */
663
664				/* DMA queues (contexts) */
665	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
666
667				/* Context support */
668	int		  irq;		/* Interrupt used by board	   */
669	int		  irq_enabled;	/* True if the irq handler is enabled */
670	int		  msi_enabled;	/* MSI enabled */
671	int		  irqrid;	/* Interrupt used by board */
672	struct resource   *irqr;	/* Resource for interrupt used by board	   */
673	void		  *irqh;	/* Handle from bus_setup_intr      */
674
675	/* Storage of resource pointers for drm_get_resource_* */
676	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
677	int		  pcirid[DRM_MAX_PCI_RESOURCE];
678
679	int		  pci_domain;
680	int		  pci_bus;
681	int		  pci_slot;
682	int		  pci_func;
683
684	atomic_t	  context_flag;	/* Context swapping flag	   */
685	int		  last_context;	/* Last current context		   */
686
687	int		  vblank_disable_allowed;
688	atomic_t 	  vbl_signal_pending;	/* number of signals pending on all crtcs */
689	struct callout	  vblank_disable_timer;
690	u32		  max_vblank_count;	/* size of vblank counter register */
691	struct drm_vblank_info *vblank;		/* per crtc vblank info */
692	int		  num_crtcs;
693
694	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
695
696				/* Sysctl support */
697	struct drm_sysctl_info *sysctl;
698
699	drm_agp_head_t    *agp;
700	drm_sg_mem_t      *sg;  /* Scatter gather memory */
701	atomic_t          *ctx_bitmap;
702	void		  *dev_private;
703	unsigned int	  agp_buffer_token;
704	drm_local_map_t   *agp_buffer_map;
705
706	struct unrhdr	  *drw_unrhdr;
707	/* RB tree of drawable infos */
708	RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
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 kdev, drm_pci_id_list_t *idlist);
730int	drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
731void	drm_close(void *data);
732int	drm_detach(device_t kdev);
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);
803void	drm_vblank_cleanup(struct drm_device *dev);
804int	drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
805int	drm_vblank_init(struct drm_device *dev, int num_crtcs);
806void	drm_vbl_send_signals(struct drm_device *dev, int crtc);
807int 	drm_modeset_ctl(struct drm_device *dev, void *data,
808			struct drm_file *file_priv);
809
810/* AGP/PCI Express/GART support (drm_agpsupport.c) */
811int	drm_device_is_agp(struct drm_device *dev);
812int	drm_device_is_pcie(struct drm_device *dev);
813drm_agp_head_t *drm_agp_init(void);
814int	drm_agp_acquire(struct drm_device *dev);
815int	drm_agp_release(struct drm_device *dev);
816int	drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
817int	drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
818void	*drm_agp_allocate_memory(size_t pages, u32 type);
819int	drm_agp_free_memory(void *handle);
820int	drm_agp_bind_memory(void *handle, off_t start);
821int	drm_agp_unbind_memory(void *handle);
822int	drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
823int	drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
824int	drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
825int	drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
826
827/* Scatter Gather Support (drm_scatter.c) */
828void	drm_sg_cleanup(drm_sg_mem_t *entry);
829int	drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
830
831/* sysctl support (drm_sysctl.h) */
832extern int		drm_sysctl_init(struct drm_device *dev);
833extern int		drm_sysctl_cleanup(struct drm_device *dev);
834
835/* ATI PCIGART support (ati_pcigart.c) */
836int	drm_ati_pcigart_init(struct drm_device *dev,
837				struct drm_ati_pcigart_info *gart_info);
838int	drm_ati_pcigart_cleanup(struct drm_device *dev,
839				struct drm_ati_pcigart_info *gart_info);
840
841/* Locking IOCTL support (drm_drv.c) */
842int	drm_lock(struct drm_device *dev, void *data,
843		 struct drm_file *file_priv);
844int	drm_unlock(struct drm_device *dev, void *data,
845		   struct drm_file *file_priv);
846int	drm_version(struct drm_device *dev, void *data,
847		    struct drm_file *file_priv);
848int	drm_setversion(struct drm_device *dev, void *data,
849		       struct drm_file *file_priv);
850
851/* Misc. IOCTL support (drm_ioctl.c) */
852int	drm_irq_by_busid(struct drm_device *dev, void *data,
853			 struct drm_file *file_priv);
854int	drm_getunique(struct drm_device *dev, void *data,
855		      struct drm_file *file_priv);
856int	drm_setunique(struct drm_device *dev, void *data,
857		      struct drm_file *file_priv);
858int	drm_getmap(struct drm_device *dev, void *data,
859		   struct drm_file *file_priv);
860int	drm_getclient(struct drm_device *dev, void *data,
861		      struct drm_file *file_priv);
862int	drm_getstats(struct drm_device *dev, void *data,
863		     struct drm_file *file_priv);
864int	drm_noop(struct drm_device *dev, void *data,
865		 struct drm_file *file_priv);
866
867/* Context IOCTL support (drm_context.c) */
868int	drm_resctx(struct drm_device *dev, void *data,
869		   struct drm_file *file_priv);
870int	drm_addctx(struct drm_device *dev, void *data,
871		   struct drm_file *file_priv);
872int	drm_modctx(struct drm_device *dev, void *data,
873		   struct drm_file *file_priv);
874int	drm_getctx(struct drm_device *dev, void *data,
875		   struct drm_file *file_priv);
876int	drm_switchctx(struct drm_device *dev, void *data,
877		      struct drm_file *file_priv);
878int	drm_newctx(struct drm_device *dev, void *data,
879		   struct drm_file *file_priv);
880int	drm_rmctx(struct drm_device *dev, void *data,
881		  struct drm_file *file_priv);
882int	drm_setsareactx(struct drm_device *dev, void *data,
883			struct drm_file *file_priv);
884int	drm_getsareactx(struct drm_device *dev, void *data,
885			struct drm_file *file_priv);
886
887/* Drawable IOCTL support (drm_drawable.c) */
888int	drm_adddraw(struct drm_device *dev, void *data,
889		    struct drm_file *file_priv);
890int	drm_rmdraw(struct drm_device *dev, void *data,
891		   struct drm_file *file_priv);
892int	drm_update_draw(struct drm_device *dev, void *data,
893			struct drm_file *file_priv);
894struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
895						int handle);
896
897/* Drawable support (drm_drawable.c) */
898void drm_drawable_free_all(struct drm_device *dev);
899
900/* Authentication IOCTL support (drm_auth.c) */
901int	drm_getmagic(struct drm_device *dev, void *data,
902		     struct drm_file *file_priv);
903int	drm_authmagic(struct drm_device *dev, void *data,
904		      struct drm_file *file_priv);
905
906/* Buffer management support (drm_bufs.c) */
907int	drm_addmap_ioctl(struct drm_device *dev, void *data,
908			 struct drm_file *file_priv);
909int	drm_rmmap_ioctl(struct drm_device *dev, void *data,
910			struct drm_file *file_priv);
911int	drm_addbufs(struct drm_device *dev, void *data,
912		    struct drm_file *file_priv);
913int	drm_infobufs(struct drm_device *dev, void *data,
914		     struct drm_file *file_priv);
915int	drm_markbufs(struct drm_device *dev, void *data,
916		     struct drm_file *file_priv);
917int	drm_freebufs(struct drm_device *dev, void *data,
918		     struct drm_file *file_priv);
919int	drm_mapbufs(struct drm_device *dev, void *data,
920		    struct drm_file *file_priv);
921
922/* DMA support (drm_dma.c) */
923int	drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
924
925/* IRQ support (drm_irq.c) */
926int	drm_control(struct drm_device *dev, void *data,
927		    struct drm_file *file_priv);
928int	drm_wait_vblank(struct drm_device *dev, void *data,
929			struct drm_file *file_priv);
930
931/* AGP/GART support (drm_agpsupport.c) */
932int	drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
933			      struct drm_file *file_priv);
934int	drm_agp_release_ioctl(struct drm_device *dev, void *data,
935			      struct drm_file *file_priv);
936int	drm_agp_enable_ioctl(struct drm_device *dev, void *data,
937			     struct drm_file *file_priv);
938int	drm_agp_info_ioctl(struct drm_device *dev, void *data,
939			   struct drm_file *file_priv);
940int	drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
941			    struct drm_file *file_priv);
942int	drm_agp_free_ioctl(struct drm_device *dev, void *data,
943			   struct drm_file *file_priv);
944int	drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
945			     struct drm_file *file_priv);
946int	drm_agp_bind_ioctl(struct drm_device *dev, void *data,
947			   struct drm_file *file_priv);
948
949/* Scatter Gather Support (drm_scatter.c) */
950int	drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
951			   struct drm_file *file_priv);
952int	drm_sg_free(struct drm_device *dev, void *data,
953		    struct drm_file *file_priv);
954
955/* consistent PCI memory functions (drm_pci.c) */
956drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
957				size_t align, dma_addr_t maxaddr);
958void	drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
959
960/* Inline replacements for drm_alloc and friends */
961static __inline__ void *
962drm_alloc(size_t size, struct malloc_type *area)
963{
964	return malloc(size, area, M_NOWAIT);
965}
966
967static __inline__ void *
968drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
969{
970	return malloc(size * nmemb, area, M_NOWAIT | M_ZERO);
971}
972
973static __inline__ void *
974drm_realloc(void *oldpt, size_t oldsize, size_t size,
975    struct malloc_type *area)
976{
977	return reallocf(oldpt, size, area, M_NOWAIT);
978}
979
980static __inline__ void
981drm_free(void *pt, size_t size, struct malloc_type *area)
982{
983	free(pt, area);
984}
985
986/* Inline replacements for DRM_IOREMAP macros */
987static __inline__ void
988drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
989{
990	map->handle = drm_ioremap_wc(dev, map);
991}
992static __inline__ void
993drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
994{
995	map->handle = drm_ioremap(dev, map);
996}
997static __inline__ void
998drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
999{
1000	if ( map->handle && map->size )
1001		drm_ioremapfree(map);
1002}
1003
1004static __inline__ struct drm_local_map *
1005drm_core_findmap(struct drm_device *dev, unsigned long offset)
1006{
1007	drm_local_map_t *map;
1008
1009	DRM_SPINLOCK_ASSERT(&dev->dev_lock);
1010	TAILQ_FOREACH(map, &dev->maplist, link) {
1011		if (map->offset == offset)
1012			return map;
1013	}
1014	return NULL;
1015}
1016
1017static __inline__ void drm_core_dropmap(struct drm_map *map)
1018{
1019}
1020
1021#endif /* __KERNEL__ */
1022#endif /* _DRM_P_H_ */
1023