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