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