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