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