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