drmP.h revision 162132
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 162132 2006-09-07 23:04:47Z 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 {(void)(queue);} 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_IS_AGP,
224	DRM_MIGHT_BE_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_int64_t u64;
277typedef u_int32_t u32;
278typedef u_int16_t u16;
279typedef u_int8_t u8;
280
281/* DRM_READMEMORYBARRIER() prevents reordering of reads.
282 * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
283 * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
284 */
285#if defined(__i386__)
286#define DRM_READMEMORYBARRIER()		__asm __volatile( \
287					"lock; addl $0,0(%%esp)" : : : "memory");
288#define DRM_WRITEMEMORYBARRIER()	__asm __volatile("" : : : "memory");
289#define DRM_MEMORYBARRIER()		__asm __volatile( \
290					"lock; addl $0,0(%%esp)" : : : "memory");
291#elif defined(__alpha__)
292#define DRM_READMEMORYBARRIER()		alpha_mb();
293#define DRM_WRITEMEMORYBARRIER()	alpha_wmb();
294#define DRM_MEMORYBARRIER()		alpha_mb();
295#elif defined(__amd64__)
296#define DRM_READMEMORYBARRIER()		__asm __volatile( \
297					"lock; addl $0,0(%%rsp)" : : : "memory");
298#define DRM_WRITEMEMORYBARRIER()	__asm __volatile("" : : : "memory");
299#define DRM_MEMORYBARRIER()		__asm __volatile( \
300					"lock; addl $0,0(%%rsp)" : : : "memory");
301#endif
302
303#ifdef __FreeBSD__
304#define DRM_READ8(map, offset)						\
305	*(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset))
306#define DRM_READ16(map, offset)						\
307	*(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset))
308#define DRM_READ32(map, offset)						\
309	*(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset))
310#define DRM_WRITE8(map, offset, val)					\
311	*(volatile u_int8_t *) (((unsigned long)(map)->handle) + (offset)) = val
312#define DRM_WRITE16(map, offset, val)					\
313	*(volatile u_int16_t *) (((unsigned long)(map)->handle) + (offset)) = val
314#define DRM_WRITE32(map, offset, val)					\
315	*(volatile u_int32_t *)(((unsigned long)(map)->handle) + (offset)) = val
316
317#define DRM_VERIFYAREA_READ( uaddr, size )		\
318	(!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
319
320#else /* __FreeBSD__ */
321
322typedef vaddr_t vm_offset_t;
323
324#define DRM_READ8(map, offset)		\
325	bus_space_read_1( (map)->bst, (map)->bsh, (offset))
326#define DRM_READ16(map, offset)		\
327	bus_space_read_2( (map)->bst, (map)->bsh, (offset))
328#define DRM_READ32(map, offset)		\
329	bus_space_read_4( (map)->bst, (map)->bsh, (offset))
330#define DRM_WRITE8(map, offset, val)	\
331	bus_space_write_1((map)->bst, (map)->bsh, (offset), (val))
332#define DRM_WRITE16(map, offset, val)	\
333	bus_space_write_2((map)->bst, (map)->bsh, (offset), (val))
334#define DRM_WRITE32(map, offset, val)	\
335	bus_space_write_4((map)->bst, (map)->bsh, (offset), (val))
336
337#define DRM_VERIFYAREA_READ( uaddr, size )		\
338	(!uvm_useracc((caddr_t)uaddr, size, VM_PROT_READ))
339#endif /* !__FreeBSD__ */
340
341#define DRM_COPY_TO_USER_IOCTL(user, kern, size)	\
342	if ( IOCPARM_LEN(cmd) != size)			\
343		return EINVAL;				\
344	*user = kern;
345#define DRM_COPY_FROM_USER_IOCTL(kern, user, size) \
346	if ( IOCPARM_LEN(cmd) != size)			\
347		return EINVAL;				\
348	kern = *user;
349#define DRM_COPY_TO_USER(user, kern, size) \
350	copyout(kern, user, size)
351#define DRM_COPY_FROM_USER(kern, user, size) \
352	copyin(user, kern, size)
353#define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) 	\
354	copyin(arg2, arg1, arg3)
355#define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)	\
356	copyout(arg2, arg1, arg3)
357#if __FreeBSD_version > 500000
358#define DRM_GET_USER_UNCHECKED(val, uaddr)		\
359	((val) = fuword32(uaddr), 0)
360#else
361#define DRM_GET_USER_UNCHECKED(val, uaddr)		\
362	((val) = fuword(uaddr), 0)
363#endif
364
365#define cpu_to_le32(x) htole32(x)
366#define le32_to_cpu(x) le32toh(x)
367
368#define DRM_ERR(v)		v
369#define DRM_HZ			hz
370#define DRM_UDELAY(udelay)	DELAY(udelay)
371#define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
372
373#define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {	\
374	(_map) = (_dev)->context_sareas[_ctx];		\
375} while(0)
376
377#define DRM_GET_PRIV_WITH_RETURN(_priv, _filp)			\
378do {								\
379	if (_filp != (DRMFILE)(intptr_t)DRM_CURRENTPID) {	\
380		DRM_ERROR("filp doesn't match curproc\n");	\
381		return EINVAL;					\
382	}							\
383	_priv = drm_find_file_by_proc(dev, DRM_CURPROC);	\
384	if (_priv == NULL) {					\
385		DRM_ERROR("can't find authenticator\n");	\
386		return EINVAL;					\
387	}							\
388} while (0)
389
390#define LOCK_TEST_WITH_RETURN(dev, filp)				\
391do {									\
392	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
393	     dev->lock.filp != filp) {					\
394		DRM_ERROR("%s called without lock held\n",		\
395			   __FUNCTION__);				\
396		return EINVAL;						\
397	}								\
398} while (0)
399
400#define DRM_GETSAREA()					\
401do {								\
402	drm_local_map_t *map;					\
403	DRM_SPINLOCK_ASSERT(&dev->dev_lock);			\
404	TAILQ_FOREACH(map, &dev->maplist, link) {		\
405		if (map->type == _DRM_SHM &&			\
406			map->flags & _DRM_CONTAINS_LOCK) {	\
407			dev_priv->sarea = map;			\
408			break;					\
409		}						\
410	}							\
411} while (0)
412
413#if defined(__FreeBSD__) && __FreeBSD_version > 500000
414#define DRM_WAIT_ON( ret, queue, timeout, condition )		\
415for ( ret = 0 ; !ret && !(condition) ; ) {			\
416	DRM_UNLOCK();						\
417	mtx_lock(&dev->irq_lock);				\
418	if (!(condition))					\
419	   ret = msleep(&(queue), &dev->irq_lock, 		\
420			 PZERO | PCATCH, "drmwtq", (timeout));	\
421	mtx_unlock(&dev->irq_lock);				\
422	DRM_LOCK();						\
423}
424#else
425#define DRM_WAIT_ON( ret, queue, timeout, condition )	\
426for ( ret = 0 ; !ret && !(condition) ; ) {		\
427        int s = spldrm();				\
428	if (!(condition))				\
429	   ret = tsleep( &(queue), PZERO | PCATCH, 	\
430			 "drmwtq", (timeout) );		\
431	splx(s);					\
432}
433#endif
434
435#define DRM_ERROR(fmt, arg...) \
436	printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
437	    DRM_CURRENTPID, __func__ , ## arg)
438
439#define DRM_INFO(fmt, arg...)  printf("info: [" DRM_NAME "] " fmt , ## arg)
440
441#define DRM_DEBUG(fmt, arg...) do {					\
442	if (drm_debug_flag)						\
443		printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
444			__func__ , ## arg);				\
445} while (0)
446
447typedef struct drm_pci_id_list
448{
449	int vendor;
450	int device;
451	long driver_private;
452	char *name;
453} drm_pci_id_list_t;
454
455#define DRM_AUTH	0x1
456#define DRM_MASTER	0x2
457#define DRM_ROOT_ONLY	0x4
458typedef struct drm_ioctl_desc {
459	int		     (*func)(DRM_IOCTL_ARGS);
460	int flags;
461} drm_ioctl_desc_t;
462
463typedef struct drm_magic_entry {
464	drm_magic_t	       magic;
465	struct drm_file	       *priv;
466	struct drm_magic_entry *next;
467} drm_magic_entry_t;
468
469typedef struct drm_magic_head {
470	struct drm_magic_entry *head;
471	struct drm_magic_entry *tail;
472} drm_magic_head_t;
473
474typedef struct drm_buf {
475	int		  idx;	       /* Index into master buflist	     */
476	int		  total;       /* Buffer size			     */
477	int		  order;       /* log-base-2(total)		     */
478	int		  used;	       /* Amount of buffer in use (for DMA)  */
479	unsigned long	  offset;      /* Byte offset (used internally)	     */
480	void		  *address;    /* Address of buffer		     */
481	unsigned long	  bus_address; /* Bus address of buffer		     */
482	struct drm_buf	  *next;       /* Kernel-only: used for free list    */
483	__volatile__ int  pending;     /* On hardware DMA queue		     */
484	DRMFILE		  filp;	       /* Unique identifier of holding process */
485	int		  context;     /* Kernel queue for this buffer	     */
486	enum {
487		DRM_LIST_NONE	 = 0,
488		DRM_LIST_FREE	 = 1,
489		DRM_LIST_WAIT	 = 2,
490		DRM_LIST_PEND	 = 3,
491		DRM_LIST_PRIO	 = 4,
492		DRM_LIST_RECLAIM = 5
493	}		  list;	       /* Which list we're on		     */
494
495	int		  dev_priv_size; /* Size of buffer private stoarge   */
496	void		  *dev_private;  /* Per-buffer private storage       */
497} drm_buf_t;
498
499typedef struct drm_freelist {
500	int		  initialized; /* Freelist in use		   */
501	atomic_t	  count;       /* Number of free buffers	   */
502	drm_buf_t	  *next;       /* End pointer			   */
503
504	int		  low_mark;    /* Low water mark		   */
505	int		  high_mark;   /* High water mark		   */
506} drm_freelist_t;
507
508typedef struct drm_dma_handle {
509	void *vaddr;
510	bus_addr_t busaddr;
511#if defined(__FreeBSD__)
512	bus_dma_tag_t tag;
513	bus_dmamap_t map;
514#elif defined(__NetBSD__)
515	bus_dma_segment_t seg;
516#endif
517} drm_dma_handle_t;
518
519typedef struct drm_buf_entry {
520	int		  buf_size;
521	int		  buf_count;
522	drm_buf_t	  *buflist;
523	int		  seg_count;
524	drm_dma_handle_t  **seglist;
525	int		  page_order;
526
527	drm_freelist_t	  freelist;
528} drm_buf_entry_t;
529
530typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
531struct drm_file {
532	TAILQ_ENTRY(drm_file) link;
533	int		  authenticated;
534	int		  master;
535	int		  minor;
536	pid_t		  pid;
537	uid_t		  uid;
538	int		  refs;
539	drm_magic_t	  magic;
540	unsigned long	  ioctl_count;
541	void		 *driver_priv;
542};
543
544typedef struct drm_lock_data {
545	drm_hw_lock_t	  *hw_lock;	/* Hardware lock		   */
546	DRMFILE		  filp;	        /* Unique identifier of holding process (NULL is kernel)*/
547	int		  lock_queue;	/* Queue of blocked processes	   */
548	unsigned long	  lock_time;	/* Time of last lock in jiffies	   */
549} drm_lock_data_t;
550
551/* This structure, in the drm_device_t, is always initialized while the device
552 * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
553 * when set marks that no further bufs may be allocated until device teardown
554 * occurs (when the last open of the device has closed).  The high/low
555 * watermarks of bufs are only touched by the X Server, and thus not
556 * concurrently accessed, so no locking is needed.
557 */
558typedef struct drm_device_dma {
559	drm_buf_entry_t	  bufs[DRM_MAX_ORDER+1];
560	int		  buf_count;
561	drm_buf_t	  **buflist;	/* Vector of pointers info bufs	   */
562	int		  seg_count;
563	int		  page_count;
564	unsigned long	  *pagelist;
565	unsigned long	  byte_count;
566	enum {
567		_DRM_DMA_USE_AGP = 0x01,
568		_DRM_DMA_USE_SG  = 0x02
569	} flags;
570} drm_device_dma_t;
571
572typedef struct drm_agp_mem {
573	void               *handle;
574	unsigned long      bound; /* address */
575	int                pages;
576	struct drm_agp_mem *prev;
577	struct drm_agp_mem *next;
578} drm_agp_mem_t;
579
580typedef struct drm_agp_head {
581	device_t	   agpdev;
582	struct agp_info    info;
583	const char         *chipset;
584	drm_agp_mem_t      *memory;
585	unsigned long      mode;
586	int                enabled;
587	int                acquired;
588	unsigned long      base;
589   	int 		   mtrr;
590	int		   cant_use_aperture;
591	unsigned long	   page_mask;
592} drm_agp_head_t;
593
594typedef struct drm_sg_mem {
595	unsigned long   handle;
596	void            *virtual;
597	int             pages;
598	dma_addr_t	*busaddr;
599	drm_dma_handle_t *dmah;	/* Handle to PCI memory for ATI PCIGART table */
600} drm_sg_mem_t;
601
602typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
603
604typedef struct drm_local_map {
605	unsigned long	offset;	 /* Physical address (0 for SAREA)*/
606	unsigned long	size;	 /* Physical size (bytes)	    */
607	drm_map_type_t	type;	 /* Type of memory mapped		    */
608	drm_map_flags_t flags;	 /* Flags				    */
609	void		*handle; /* User-space: "Handle" to pass to mmap    */
610				 /* Kernel-space: kernel-virtual address    */
611	int		mtrr;	 /* Boolean: MTRR used */
612				 /* Private data			    */
613	int		rid;	 /* PCI resource ID for bus_space */
614	struct resource *bsr;
615	bus_space_tag_t bst;
616	bus_space_handle_t bsh;
617	drm_dma_handle_t *dmah;
618	TAILQ_ENTRY(drm_local_map) link;
619} drm_local_map_t;
620
621TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig);
622typedef struct drm_vbl_sig {
623	TAILQ_ENTRY(drm_vbl_sig) link;
624	unsigned int	sequence;
625	int		signo;
626	int		pid;
627} drm_vbl_sig_t;
628
629/* location of GART table */
630#define DRM_ATI_GART_MAIN 1
631#define DRM_ATI_GART_FB   2
632
633typedef struct ati_pcigart_info {
634	int gart_table_location;
635	int is_pcie;
636	void *addr;
637	dma_addr_t bus_addr;
638	drm_local_map_t mapping;
639} drm_ati_pcigart_info;
640
641struct drm_driver_info {
642	int	(*load)(struct drm_device *, unsigned long flags);
643	int	(*firstopen)(struct drm_device *);
644	int	(*open)(struct drm_device *, drm_file_t *);
645	void	(*preclose)(struct drm_device *, void *filp);
646	void	(*postclose)(struct drm_device *, drm_file_t *);
647	void	(*lastclose)(struct drm_device *);
648	int	(*unload)(struct drm_device *);
649	void	(*reclaim_buffers_locked)(struct drm_device *, void *filp);
650	int	(*dma_ioctl)(DRM_IOCTL_ARGS);
651	void	(*dma_ready)(struct drm_device *);
652	int	(*dma_quiescent)(struct drm_device *);
653	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
654					     drm_lock_flags_t flags);
655	int	(*dma_flush_unblock)(struct drm_device *, int context,
656				     drm_lock_flags_t flags);
657	int	(*context_ctor)(struct drm_device *dev, int context);
658	int	(*context_dtor)(struct drm_device *dev, int context);
659	int	(*kernel_context_switch)(struct drm_device *dev, int old,
660					 int new);
661	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
662	void	(*irq_preinstall)(drm_device_t *dev);
663	void	(*irq_postinstall)(drm_device_t *dev);
664	void	(*irq_uninstall)(drm_device_t *dev);
665	void	(*irq_handler)(DRM_IRQ_ARGS);
666	int	(*vblank_wait)(drm_device_t *dev, unsigned int *sequence);
667
668	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
669
670	/**
671	 * Called by \c drm_device_is_agp.  Typically used to determine if a
672	 * card is really attached to AGP or not.
673	 *
674	 * \param dev  DRM device handle
675	 *
676	 * \returns
677	 * One of three values is returned depending on whether or not the
678	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
679	 * (return of 1), or may or may not be AGP (return of 2).
680	 */
681	int	(*device_is_agp) (struct drm_device * dev);
682
683	drm_ioctl_desc_t *ioctls;
684	int	max_ioctl;
685
686	int	buf_priv_size;
687
688	int	major;
689	int	minor;
690	int	patchlevel;
691	const char *name;		/* Simple driver name		   */
692	const char *desc;		/* Longer driver name		   */
693	const char *date;		/* Date of last major changes.	   */
694
695	unsigned use_agp :1;
696	unsigned require_agp :1;
697	unsigned use_sg :1;
698	unsigned use_dma :1;
699	unsigned use_pci_dma :1;
700	unsigned use_dma_queue :1;
701	unsigned use_irq :1;
702	unsigned use_vbl_irq :1;
703	unsigned use_mtrr :1;
704};
705
706/* Length for the array of resource pointers for drm_get_resource_*. */
707#define DRM_MAX_PCI_RESOURCE	3
708
709/**
710 * DRM device functions structure
711 */
712struct drm_device {
713#if defined(__NetBSD__) || defined(__OpenBSD__)
714	struct device	  device; /* softc is an extension of struct device */
715#endif
716
717	struct drm_driver_info driver;
718	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
719
720	u_int16_t pci_device;		/* PCI device id */
721	u_int16_t pci_vendor;		/* PCI vendor id */
722
723	char		  *unique;	/* Unique identifier: e.g., busid  */
724	int		  unique_len;	/* Length of unique field	   */
725#ifdef __FreeBSD__
726	device_t	  device;	/* Device instance from newbus     */
727#endif
728	struct cdev	  *devnode;	/* Device number for mknod	   */
729	int		  if_version;	/* Highest interface version set */
730
731	int		  flags;	/* Flags to open(2)		   */
732
733				/* Locks */
734#if defined(__FreeBSD__) && __FreeBSD_version > 500000
735	struct mtx	  dma_lock;	/* protects dev->dma */
736	struct mtx	  irq_lock;	/* protects irq condition checks */
737	struct mtx	  dev_lock;	/* protects everything else */
738#endif
739				/* Usage Counters */
740	int		  open_count;	/* Outstanding files open	   */
741	int		  buf_use;	/* Buffers in use -- cannot alloc  */
742
743				/* Performance counters */
744	unsigned long     counters;
745	drm_stat_type_t   types[15];
746	atomic_t          counts[15];
747
748				/* Authentication */
749	drm_file_list_t   files;
750	drm_magic_head_t  magiclist[DRM_HASH_SIZE];
751
752	/* Linked list of mappable regions. Protected by dev_lock */
753	drm_map_list_t	  maplist;
754
755	drm_local_map_t	  **context_sareas;
756	int		  max_context;
757
758	drm_lock_data_t	  lock;		/* Information on hardware lock	   */
759
760				/* DMA queues (contexts) */
761	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
762
763				/* Context support */
764	int		  irq;		/* Interrupt used by board	   */
765	int		  irq_enabled;	/* True if the irq handler is enabled */
766#ifdef __FreeBSD__
767	int		  irqrid;	/* Interrupt used by board */
768	struct resource   *irqr;	/* Resource for interrupt used by board	   */
769#elif defined(__NetBSD__) || defined(__OpenBSD__)
770	struct pci_attach_args  pa;
771#endif
772	void		  *irqh;	/* Handle from bus_setup_intr      */
773
774	/* Storage of resource pointers for drm_get_resource_* */
775	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
776	int		  pcirid[DRM_MAX_PCI_RESOURCE];
777
778	int		  pci_domain;
779	int		  pci_bus;
780	int		  pci_slot;
781	int		  pci_func;
782
783	atomic_t	  context_flag;	/* Context swapping flag	   */
784	int		  last_context;	/* Last current context		   */
785   	int		  vbl_queue;	/* vbl wait channel */
786   	atomic_t          vbl_received;
787
788#ifdef __FreeBSD__
789	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
790#elif defined(__NetBSD__)
791	pid_t		  buf_pgid;
792#endif
793
794				/* Sysctl support */
795	struct drm_sysctl_info *sysctl;
796
797	drm_agp_head_t    *agp;
798	drm_sg_mem_t      *sg;  /* Scatter gather memory */
799	atomic_t          *ctx_bitmap;
800	void		  *dev_private;
801	unsigned int	  agp_buffer_token;
802	drm_local_map_t   *agp_buffer_map;
803};
804
805extern int	drm_debug_flag;
806
807/* Device setup support (drm_drv.c) */
808#ifdef __FreeBSD__
809int	drm_probe(device_t nbdev, drm_pci_id_list_t *idlist);
810int	drm_attach(device_t nbdev, drm_pci_id_list_t *idlist);
811int	drm_detach(device_t nbdev);
812d_ioctl_t drm_ioctl;
813d_open_t drm_open;
814d_close_t drm_close;
815d_read_t drm_read;
816d_poll_t drm_poll;
817d_mmap_t drm_mmap;
818#elif defined(__NetBSD__) || defined(__OpenBSD__)
819int	drm_probe(struct pci_attach_args *pa, drm_pci_id_list_t *idlist);
820int	drm_attach(struct pci_attach_args *pa, dev_t kdev, drm_pci_id_list_t *idlist);
821dev_type_ioctl(drm_ioctl);
822dev_type_open(drm_open);
823dev_type_close(drm_close);
824dev_type_read(drm_read);
825dev_type_poll(drm_poll);
826dev_type_mmap(drm_mmap);
827#endif
828
829/* File operations helpers (drm_fops.c) */
830#ifdef __FreeBSD__
831extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
832					 DRM_STRUCTPROC *p, drm_device_t *dev);
833extern drm_file_t	*drm_find_file_by_proc(drm_device_t *dev,
834					 DRM_STRUCTPROC *p);
835#elif defined(__NetBSD__) || defined(__OpenBSD__)
836extern int		drm_open_helper(dev_t kdev, int flags, int fmt,
837					DRM_STRUCTPROC *p, drm_device_t *dev);
838extern drm_file_t	*drm_find_file_by_proc(drm_device_t *dev,
839					       DRM_STRUCTPROC *p);
840#endif /* __NetBSD__ || __OpenBSD__ */
841
842/* Memory management support (drm_memory.c) */
843void	drm_mem_init(void);
844void	drm_mem_uninit(void);
845void	*drm_alloc(size_t size, int area);
846void	*drm_calloc(size_t nmemb, size_t size, int area);
847void	*drm_realloc(void *oldpt, size_t oldsize, size_t size,
848				   int area);
849void	drm_free(void *pt, size_t size, int area);
850void	*drm_ioremap(drm_device_t *dev, drm_local_map_t *map);
851void	drm_ioremapfree(drm_local_map_t *map);
852int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
853int	drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
854
855int	drm_context_switch(drm_device_t *dev, int old, int new);
856int	drm_context_switch_complete(drm_device_t *dev, int new);
857
858int	drm_ctxbitmap_init(drm_device_t *dev);
859void	drm_ctxbitmap_cleanup(drm_device_t *dev);
860void	drm_ctxbitmap_free(drm_device_t *dev, int ctx_handle);
861int	drm_ctxbitmap_next(drm_device_t *dev);
862
863/* Locking IOCTL support (drm_lock.c) */
864int	drm_lock_take(__volatile__ unsigned int *lock,
865				    unsigned int context);
866int	drm_lock_transfer(drm_device_t *dev,
867					__volatile__ unsigned int *lock,
868					unsigned int context);
869int	drm_lock_free(drm_device_t *dev,
870				    __volatile__ unsigned int *lock,
871				    unsigned int context);
872
873/* Buffer management support (drm_bufs.c) */
874unsigned long drm_get_resource_start(drm_device_t *dev, unsigned int resource);
875unsigned long drm_get_resource_len(drm_device_t *dev, unsigned int resource);
876void	drm_rmmap(drm_device_t *dev, drm_local_map_t *map);
877int	drm_order(unsigned long size);
878int	drm_addmap(drm_device_t * dev, unsigned long offset, unsigned long size,
879		   drm_map_type_t type, drm_map_flags_t flags,
880		   drm_local_map_t **map_ptr);
881int	drm_addbufs_pci(drm_device_t *dev, drm_buf_desc_t *request);
882int	drm_addbufs_sg(drm_device_t *dev, drm_buf_desc_t *request);
883int	drm_addbufs_agp(drm_device_t *dev, drm_buf_desc_t *request);
884
885/* DMA support (drm_dma.c) */
886int	drm_dma_setup(drm_device_t *dev);
887void	drm_dma_takedown(drm_device_t *dev);
888void	drm_free_buffer(drm_device_t *dev, drm_buf_t *buf);
889void	drm_reclaim_buffers(drm_device_t *dev, DRMFILE filp);
890#define drm_core_reclaim_buffers drm_reclaim_buffers
891
892/* IRQ support (drm_irq.c) */
893int	drm_irq_install(drm_device_t *dev);
894int	drm_irq_uninstall(drm_device_t *dev);
895irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
896void	drm_driver_irq_preinstall(drm_device_t *dev);
897void	drm_driver_irq_postinstall(drm_device_t *dev);
898void	drm_driver_irq_uninstall(drm_device_t *dev);
899int	drm_vblank_wait(drm_device_t *dev, unsigned int *vbl_seq);
900void	drm_vbl_send_signals(drm_device_t *dev);
901
902/* AGP/PCI Express/GART support (drm_agpsupport.c) */
903int	drm_device_is_agp(drm_device_t *dev);
904int	drm_device_is_pcie(drm_device_t *dev);
905drm_agp_head_t *drm_agp_init(void);
906int	drm_agp_acquire(drm_device_t *dev);
907int	drm_agp_release(drm_device_t *dev);
908int	drm_agp_info(drm_device_t * dev, drm_agp_info_t *info);
909int	drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode);
910void	*drm_agp_allocate_memory(size_t pages, u32 type);
911int	drm_agp_free_memory(void *handle);
912int	drm_agp_bind_memory(void *handle, off_t start);
913int	drm_agp_unbind_memory(void *handle);
914int	drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request);
915int	drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request);
916int	drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request);
917int	drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request);
918
919/* Scatter Gather Support (drm_scatter.c) */
920void	drm_sg_cleanup(drm_sg_mem_t *entry);
921
922#ifdef __FreeBSD__
923/* sysctl support (drm_sysctl.h) */
924extern int		drm_sysctl_init(drm_device_t *dev);
925extern int		drm_sysctl_cleanup(drm_device_t *dev);
926#endif /* __FreeBSD__ */
927
928/* ATI PCIGART support (ati_pcigart.c) */
929int	drm_ati_pcigart_init(drm_device_t *dev,
930			     drm_ati_pcigart_info *gart_info);
931int	drm_ati_pcigart_cleanup(drm_device_t *dev,
932				drm_ati_pcigart_info *gart_info);
933
934/* Locking IOCTL support (drm_drv.c) */
935int	drm_lock(DRM_IOCTL_ARGS);
936int	drm_unlock(DRM_IOCTL_ARGS);
937int	drm_version(DRM_IOCTL_ARGS);
938int	drm_setversion(DRM_IOCTL_ARGS);
939
940/* Misc. IOCTL support (drm_ioctl.c) */
941int	drm_irq_by_busid(DRM_IOCTL_ARGS);
942int	drm_getunique(DRM_IOCTL_ARGS);
943int	drm_setunique(DRM_IOCTL_ARGS);
944int	drm_getmap(DRM_IOCTL_ARGS);
945int	drm_getclient(DRM_IOCTL_ARGS);
946int	drm_getstats(DRM_IOCTL_ARGS);
947int	drm_noop(DRM_IOCTL_ARGS);
948
949/* Context IOCTL support (drm_context.c) */
950int	drm_resctx(DRM_IOCTL_ARGS);
951int	drm_addctx(DRM_IOCTL_ARGS);
952int	drm_modctx(DRM_IOCTL_ARGS);
953int	drm_getctx(DRM_IOCTL_ARGS);
954int	drm_switchctx(DRM_IOCTL_ARGS);
955int	drm_newctx(DRM_IOCTL_ARGS);
956int	drm_rmctx(DRM_IOCTL_ARGS);
957int	drm_setsareactx(DRM_IOCTL_ARGS);
958int	drm_getsareactx(DRM_IOCTL_ARGS);
959
960/* Drawable IOCTL support (drm_drawable.c) */
961int	drm_adddraw(DRM_IOCTL_ARGS);
962int	drm_rmdraw(DRM_IOCTL_ARGS);
963
964/* Authentication IOCTL support (drm_auth.c) */
965int	drm_getmagic(DRM_IOCTL_ARGS);
966int	drm_authmagic(DRM_IOCTL_ARGS);
967
968/* Buffer management support (drm_bufs.c) */
969int	drm_addmap_ioctl(DRM_IOCTL_ARGS);
970int	drm_rmmap_ioctl(DRM_IOCTL_ARGS);
971int	drm_addbufs_ioctl(DRM_IOCTL_ARGS);
972int	drm_infobufs(DRM_IOCTL_ARGS);
973int	drm_markbufs(DRM_IOCTL_ARGS);
974int	drm_freebufs(DRM_IOCTL_ARGS);
975int	drm_mapbufs(DRM_IOCTL_ARGS);
976
977/* DMA support (drm_dma.c) */
978int	drm_dma(DRM_IOCTL_ARGS);
979
980/* IRQ support (drm_irq.c) */
981int	drm_control(DRM_IOCTL_ARGS);
982int	drm_wait_vblank(DRM_IOCTL_ARGS);
983
984/* AGP/GART support (drm_agpsupport.c) */
985int	drm_agp_acquire_ioctl(DRM_IOCTL_ARGS);
986int	drm_agp_release_ioctl(DRM_IOCTL_ARGS);
987int	drm_agp_enable_ioctl(DRM_IOCTL_ARGS);
988int	drm_agp_info_ioctl(DRM_IOCTL_ARGS);
989int	drm_agp_alloc_ioctl(DRM_IOCTL_ARGS);
990int	drm_agp_free_ioctl(DRM_IOCTL_ARGS);
991int	drm_agp_unbind_ioctl(DRM_IOCTL_ARGS);
992int	drm_agp_bind_ioctl(DRM_IOCTL_ARGS);
993
994/* Scatter Gather Support (drm_scatter.c) */
995int	drm_sg_alloc(DRM_IOCTL_ARGS);
996int	drm_sg_free(DRM_IOCTL_ARGS);
997
998/* consistent PCI memory functions (drm_pci.c) */
999drm_dma_handle_t *drm_pci_alloc(drm_device_t *dev, size_t size, size_t align,
1000				dma_addr_t maxaddr);
1001void	drm_pci_free(drm_device_t *dev, drm_dma_handle_t *dmah);
1002
1003/* Inline replacements for DRM_IOREMAP macros */
1004static __inline__ void drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1005{
1006	map->handle = drm_ioremap(dev, map);
1007}
1008static __inline__ void drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1009{
1010	if ( map->handle && map->size )
1011		drm_ioremapfree(map);
1012}
1013
1014static __inline__ struct drm_local_map *drm_core_findmap(struct drm_device *dev, unsigned long offset)
1015{
1016	drm_local_map_t *map;
1017
1018	DRM_SPINLOCK_ASSERT(&dev->dev_lock);
1019	TAILQ_FOREACH(map, &dev->maplist, link) {
1020		if (map->offset == offset)
1021			return map;
1022	}
1023	return NULL;
1024}
1025
1026static __inline__ void drm_core_dropmap(struct drm_map *map)
1027{
1028}
1029
1030#endif /* __KERNEL__ */
1031#endif /* _DRM_P_H_ */
1032