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