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