xref: /titanic_41/usr/src/uts/common/io/drm/drmP.h (revision 0485cf53f277ad1364b39e092bfa2480dbcac144)
1 /*
2  * drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
3  * Created: Mon Jan  4 10:05:05 1999 by faith@precisioninsight.com
4  */
5 /*
6  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
7  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8  * All rights reserved.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the next
18  * paragraph) shall be included in all copies or substantial portions of the
19  * Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  *
29  * Authors:
30  *    Rickard E. (Rik) Faith <faith@valinux.com>
31  *    Gareth Hughes <gareth@valinux.com>
32  *
33  */
34 
35 /*
36  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37  * Use is subject to license terms.
38  */
39 
40 #ifndef	_DRMP_H
41 #define	_DRMP_H
42 
43 #include <sys/types.h>
44 #include <sys/conf.h>
45 #include <sys/modctl.h>
46 #include <sys/stat.h>
47 #include <sys/file.h>
48 #include <sys/cmn_err.h>
49 #include <sys/varargs.h>
50 #include <sys/pci.h>
51 #include <sys/ddi.h>
52 #include <sys/sunddi.h>
53 #include <sys/sunldi.h>
54 #include <sys/pmem.h>
55 #include <sys/agpgart.h>
56 #include <sys/time.h>
57 #include "drm_atomic.h"
58 #include "drm.h"
59 #include "queue.h"
60 #include "drm_linux_list.h"
61 
62 #ifndef __inline__
63 #define	__inline__	inline
64 #endif
65 
66 #if !defined(__FUNCTION__)
67 #if defined(C99)
68 #define	__FUNCTION__ __func__
69 #else
70 #define	__FUNCTION__	" "
71 #endif
72 #endif
73 
74 /* DRM space units */
75 #define	DRM_PAGE_SHIFT			PAGESHIFT
76 #define	DRM_PAGE_SIZE			(1 << DRM_PAGE_SHIFT)
77 #define	DRM_PAGE_OFFSET			(DRM_PAGE_SIZE - 1)
78 #define	DRM_PAGE_MASK			~(DRM_PAGE_SIZE - 1)
79 #define	DRM_MB2PAGES(x)			((x) << 8)
80 #define	DRM_PAGES2BYTES(x)		((x) << DRM_PAGE_SHIFT)
81 #define	DRM_BYTES2PAGES(x)		((x) >> DRM_PAGE_SHIFT)
82 #define	DRM_PAGES2KB(x)			((x) << 2)
83 #define	DRM_ALIGNED(offset)		(((offset) & DRM_PAGE_OFFSET) == 0)
84 
85 #define	PAGE_SHIFT			DRM_PAGE_SHIFT
86 #define	PAGE_SIZE			DRM_PAGE_SIZE
87 
88 #define	DRM_MAX_INSTANCES	8
89 #define	DRM_DEVNODE		"drm"
90 #define	DRM_UNOPENED		0
91 #define	DRM_OPENED		1
92 
93 #define	DRM_HASH_SIZE		16 /* Size of key hash table */
94 #define	DRM_KERNEL_CONTEXT	0  /* Change drm_resctx if changed */
95 #define	DRM_RESERVED_CONTEXTS	1  /* Change drm_resctx if changed */
96 
97 #define	DRM_MEM_DMA	   0
98 #define	DRM_MEM_SAREA	   1
99 #define	DRM_MEM_DRIVER	   2
100 #define	DRM_MEM_MAGIC	   3
101 #define	DRM_MEM_IOCTLS	   4
102 #define	DRM_MEM_MAPS	   5
103 #define	DRM_MEM_BUFS	   6
104 #define	DRM_MEM_SEGS	   7
105 #define	DRM_MEM_PAGES	   8
106 #define	DRM_MEM_FILES	  9
107 #define	DRM_MEM_QUEUES	  10
108 #define	DRM_MEM_CMDS	  11
109 #define	DRM_MEM_MAPPINGS  12
110 #define	DRM_MEM_BUFLISTS  13
111 #define	DRM_MEM_DRMLISTS  14
112 #define	DRM_MEM_TOTALDRM  15
113 #define	DRM_MEM_BOUNDDRM  16
114 #define	DRM_MEM_CTXBITMAP 17
115 #define	DRM_MEM_STUB	  18
116 #define	DRM_MEM_SGLISTS	  19
117 #define	DRM_MEM_AGPLISTS  20
118 #define	DRM_MEM_CTXLIST   21
119 #define	DRM_MEM_MM		22
120 #define	DRM_MEM_HASHTAB		23
121 #define	DRM_MEM_OBJECTS		24
122 
123 #define	DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
124 #define	DRM_MAP_HASH_OFFSET 0x10000000
125 #define	DRM_MAP_HASH_ORDER 12
126 #define	DRM_OBJECT_HASH_ORDER 12
127 #define	DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
128 #define	DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
129 #define	DRM_MM_INIT_MAX_PAGES 256
130 
131 
132 /* Internal types and structures */
133 #define	DRM_ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
134 #define	DRM_MIN(a, b) ((a) < (b) ? (a) : (b))
135 #define	DRM_MAX(a, b) ((a) > (b) ? (a) : (b))
136 
137 #define	DRM_IF_VERSION(maj, min) (maj << 16 | min)
138 
139 #define	__OS_HAS_AGP	1
140 
141 #define	DRM_DEV_MOD	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
142 #define	DRM_DEV_UID	0
143 #define	DRM_DEV_GID	0
144 
145 #define	DRM_CURRENTPID		ddi_get_pid()
146 #define	DRM_SPINLOCK(l)		mutex_enter(l)
147 #define	DRM_SPINUNLOCK(u)	mutex_exit(u)
148 #define	DRM_SPINLOCK_ASSERT(l)
149 #define	DRM_LOCK()	mutex_enter(&dev->dev_lock)
150 #define	DRM_UNLOCK()	mutex_exit(&dev->dev_lock)
151 #define	DRM_LOCK_OWNED()	ASSERT(mutex_owned(&dev->dev_lock))
152 #define	spin_lock_irqsave(l, flag)		mutex_enter(l)
153 #define	spin_unlock_irqrestore(u, flag) mutex_exit(u)
154 #define	spin_lock(l)		mutex_enter(l)
155 #define	spin_unlock(u)		mutex_exit(u)
156 
157 #define	DRM_UDELAY(sec)  delay(drv_usectohz(sec *1000))
158 #define	DRM_MEMORYBARRIER()
159 
160 typedef	struct drm_file		drm_file_t;
161 typedef struct drm_device	drm_device_t;
162 typedef struct drm_driver_info drm_driver_t;
163 
164 #define	DRM_DEVICE	drm_device_t *dev = dev1
165 #define	DRM_IOCTL_ARGS	\
166 	drm_device_t *dev1, intptr_t data, drm_file_t *fpriv, int mode
167 
168 #define	DRM_COPYFROM_WITH_RETURN(dest, src, size)	\
169 	if (ddi_copyin(src, dest, size, 0)) \
170 		return (EFAULT)
171 
172 #define	DRM_COPYTO_WITH_RETURN(dest, src, size)	\
173 	if (ddi_copyout((src), (dest), (size), 0)) \
174 		return (EFAULT)
175 
176 #define	DRM_COPY_FROM_USER(dest, src, size) \
177 	ddi_copyin((src), (dest), (size), 0) /* flag for src */
178 
179 #define	DRM_COPY_TO_USER(dest, src, size) \
180 	ddi_copyout((src), (dest), (size), 0) /* flags for dest */
181 
182 #define	DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3)  \
183 	ddi_copyin((arg2), (arg1), (arg3), 0)
184 
185 #define	DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)        \
186 	ddi_copyout((arg2), arg1, arg3, 0)
187 
188 #define	DRM_READ8(map, offset) \
189 	*(uint8_t *)((uintptr_t)((map)->dev_addr) + (offset))
190 #define	DRM_READ16(map, offset) \
191 	*(uint16_t *)((uintptr_t)((map)->dev_addr) + (offset))
192 #define	DRM_READ32(map, offset) \
193 	*(uint32_t *)((uintptr_t)((map)->dev_addr) + (offset))
194 #define	DRM_WRITE8(map, offset, val) \
195 	*(uint8_t *)((uintptr_t)((map)->dev_addr) + (offset)) = (val)
196 #define	DRM_WRITE16(map, offset, val) \
197 	*(uint16_t *)((uintptr_t)((map)->dev_addr) + (offset)) = (val)
198 #define	DRM_WRITE32(map, offset, val) \
199 	*(uint32_t *)((uintptr_t)((map)->dev_addr) + (offset)) = (val)
200 
201 typedef struct drm_wait_queue {
202 	kcondvar_t	cv;
203 	kmutex_t	lock;
204 }wait_queue_head_t;
205 
206 #define	DRM_INIT_WAITQUEUE(q, pri)	\
207 { \
208 	mutex_init(&(q)->lock, NULL, MUTEX_DRIVER, pri); \
209 	cv_init(&(q)->cv, NULL, CV_DRIVER, NULL);	\
210 }
211 
212 #define	DRM_FINI_WAITQUEUE(q)	\
213 { \
214 	mutex_destroy(&(q)->lock);	\
215 	cv_destroy(&(q)->cv);	\
216 }
217 
218 #define	DRM_WAKEUP(q)	\
219 { \
220 	mutex_enter(&(q)->lock); \
221 	cv_broadcast(&(q)->cv);	\
222 	mutex_exit(&(q)->lock);	\
223 }
224 
225 #define	jiffies			ddi_get_lbolt()
226 #define	DRM_WAIT_ON(ret, q, timeout, condition)  \
227 mutex_enter(&(q)->lock);	\
228 while (!(condition)) {	\
229 	ret = cv_timedwait_sig(&(q)->cv, &(q)->lock, jiffies + timeout); \
230 	if (ret == -1) {					\
231 		ret = EBUSY;				\
232 		break;					\
233 	} else if (ret == 0) {				\
234 		ret = EINTR;  \
235 		break; \
236 	} else { \
237 		ret = 0; \
238 	} \
239 } \
240 mutex_exit(&(q)->lock);
241 
242 #define	DRM_GETSAREA()  					\
243 {                                				\
244 	drm_local_map_t *map;					\
245 	DRM_SPINLOCK_ASSERT(&dev->dev_lock);			\
246 	TAILQ_FOREACH(map, &dev->maplist, link) {		\
247 		if (map->type == _DRM_SHM &&			\
248 			map->flags & _DRM_CONTAINS_LOCK) {	\
249 			dev_priv->sarea = map;			\
250 			break;					\
251 		}						\
252 	}							\
253 }
254 
255 #define	LOCK_TEST_WITH_RETURN(dev, fpriv)				\
256 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
257 	    dev->lock.filp != fpriv) {					\
258 		DRM_ERROR("called without lock held");	\
259 		return (EINVAL);					\
260 	}
261 
262 #define	DRM_IRQ_ARGS	caddr_t arg
263 #define	IRQ_HANDLED		DDI_INTR_CLAIMED
264 #define	IRQ_NONE		DDI_INTR_UNCLAIMED
265 
266 enum {
267 	DRM_IS_NOT_AGP,
268 	DRM_IS_AGP,
269 	DRM_MIGHT_BE_AGP
270 };
271 
272 /* Capabilities taken from src/sys/dev/pci/pcireg.h. */
273 #ifndef PCIY_AGP
274 #define	PCIY_AGP		0x02
275 #endif
276 
277 #ifndef PCIY_EXPRESS
278 #define	PCIY_EXPRESS		0x10
279 #endif
280 
281 #define	PAGE_ALIGN(addr)	(((addr) + DRM_PAGE_SIZE - 1) & DRM_PAGE_MASK)
282 #define	DRM_SUSER(p)		(crgetgid(p) == 0 || crgetuid(p) == 0)
283 
284 /*
285  * wait for 400 milliseconds
286  */
287 #define	DRM_HZ			drv_usectohz(400000)
288 
289 typedef unsigned long dma_addr_t;
290 typedef uint64_t	u64;
291 typedef uint32_t	u32;
292 typedef uint16_t	u16;
293 typedef uint8_t		u8;
294 typedef uint_t		irqreturn_t;
295 
296 #define	DRM_SUPPORT	1
297 #define	DRM_UNSUPPORT	0
298 
299 #define	__OS_HAS_AGP	1
300 
301 #define	__offsetof(type, field) ((size_t)(&((type *)0)->field))
302 #define	offsetof(type, field)   __offsetof(type, field)
303 
304 typedef struct drm_pci_id_list
305 {
306 	int vendor;
307 	int device;
308 	long driver_private;
309 	char *name;
310 } drm_pci_id_list_t;
311 
312 #define	DRM_AUTH	0x1
313 #define	DRM_MASTER	0x2
314 #define	DRM_ROOT_ONLY	0x4
315 typedef int drm_ioctl_t(DRM_IOCTL_ARGS);
316 typedef struct drm_ioctl_desc {
317 	int	(*func)(DRM_IOCTL_ARGS);
318 	int	flags;
319 } drm_ioctl_desc_t;
320 
321 typedef struct drm_magic_entry {
322 	drm_magic_t		magic;
323 	struct drm_file		*priv;
324 	struct drm_magic_entry	*next;
325 } drm_magic_entry_t;
326 
327 typedef struct drm_magic_head {
328 	struct drm_magic_entry *head;
329 	struct drm_magic_entry *tail;
330 } drm_magic_head_t;
331 
332 typedef struct drm_buf {
333 	int		idx;		/* Index into master buflist */
334 	int		total;		/* Buffer size */
335 	int		order;		/* log-base-2(total) */
336 	int		used;		/* Amount of buffer in use (for DMA) */
337 	unsigned long	offset;		/* Byte offset (used internally) */
338 	void		*address;	/* Address of buffer */
339 	unsigned long	bus_address;	/* Bus address of buffer */
340 	struct drm_buf	*next;		/* Kernel-only: used for free list */
341 	volatile int	pending;	/* On hardware DMA queue */
342 	drm_file_t		*filp;
343 				/* Uniq. identifier of holding process */
344 	int		context;	/* Kernel queue for this buffer */
345 	enum {
346 		DRM_LIST_NONE	 = 0,
347 		DRM_LIST_FREE	 = 1,
348 		DRM_LIST_WAIT	 = 2,
349 		DRM_LIST_PEND	 = 3,
350 		DRM_LIST_PRIO	 = 4,
351 		DRM_LIST_RECLAIM = 5
352 	}		list;		/* Which list we're on */
353 
354 	int		dev_priv_size;	/* Size of buffer private stoarge */
355 	void		*dev_private;	/* Per-buffer private storage */
356 } drm_buf_t;
357 
358 typedef struct drm_freelist {
359 	int		  initialized;	/* Freelist in use		*/
360 	uint32_t	  count;	/* Number of free buffers	*/
361 	drm_buf_t	  *next;	/* End pointer			*/
362 
363 	int		  low_mark;	/* Low water mark		*/
364 	int		  high_mark;	/* High water mark		*/
365 } drm_freelist_t;
366 
367 typedef struct drm_buf_entry {
368 	int		  buf_size;
369 	int		  buf_count;
370 	drm_buf_t	  *buflist;
371 	int		  seg_count;
372 	int		  page_order;
373 
374 	uint32_t	  *seglist;
375 	unsigned long	  *seglist_bus;
376 
377 	drm_freelist_t	  freelist;
378 } drm_buf_entry_t;
379 
380 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
381 struct drm_file {
382 	TAILQ_ENTRY(drm_file) link;
383 	int		  authenticated;
384 	int		  master;
385 	int		  minor;
386 	pid_t		  pid;
387 	uid_t		  uid;
388 	int		  refs;
389 	drm_magic_t	  magic;
390 	unsigned long	  ioctl_count;
391 	void		 *driver_priv;
392 };
393 
394 typedef struct drm_lock_data {
395 	drm_hw_lock_t	*hw_lock;	/* Hardware lock		*/
396 	drm_file_t	*filp;
397 	/* Uniq. identifier of holding process */
398 	kcondvar_t	lock_cv;	/* lock queue - SOLARIS Specific */
399 	kmutex_t	lock_mutex;	/* lock - SOLARIS Specific */
400 	unsigned long	lock_time;	/* Time of last lock in jiffies */
401 } drm_lock_data_t;
402 
403 /*
404  * This structure, in drm_device_t, is always initialized while the device
405  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
406  * when set marks that no further bufs may be allocated until device teardown
407  * occurs (when the last open of the device has closed).  The high/low
408  * watermarks of bufs are only touched by the X Server, and thus not
409  * concurrently accessed, so no locking is needed.
410  */
411 typedef struct drm_device_dma {
412 	drm_buf_entry_t	bufs[DRM_MAX_ORDER+1];
413 	int		buf_count;
414 	drm_buf_t	**buflist;	/* Vector of pointers info bufs	   */
415 	int		seg_count;
416 	int		page_count;
417 	unsigned long	*pagelist;
418 	unsigned long	byte_count;
419 	enum {
420 		_DRM_DMA_USE_AGP = 0x01,
421 		_DRM_DMA_USE_SG  = 0x02
422 	} flags;
423 } drm_device_dma_t;
424 
425 typedef struct drm_agp_mem {
426 	void		*handle;
427 	unsigned long	bound; /* address */
428 	int		pages;
429 	caddr_t		phys_addr;
430 	struct drm_agp_mem *prev;
431 	struct drm_agp_mem *next;
432 } drm_agp_mem_t;
433 
434 typedef struct drm_agp_head {
435 	agp_info_t	agp_info;
436 	const char	*chipset;
437 	drm_agp_mem_t	*memory;
438 	unsigned long	mode;
439 	int		enabled;
440 	int		acquired;
441 	unsigned long	base;
442 	int		mtrr;
443 	int		cant_use_aperture;
444 	unsigned long	page_mask;
445 	ldi_ident_t	agpgart_li;
446 	ldi_handle_t	agpgart_lh;
447 } drm_agp_head_t;
448 
449 
450 typedef struct drm_dma_handle {
451 	ddi_dma_handle_t	dma_hdl;
452 	ddi_acc_handle_t	acc_hdl;
453 	ddi_dma_cookie_t	cookie;
454 	uint_t		cookie_num;
455 	uintptr_t		vaddr;   /* virtual addr */
456 	uintptr_t		paddr;   /* physical addr */
457 	size_t		req_sz;  /* required size of memory */
458 	size_t		real_sz; /* real size of memory */
459 } drm_dma_handle_t;
460 
461 typedef struct drm_sg_mem {
462 	unsigned long	handle;
463 	void		*virtual;
464 	int		pages;
465 	dma_addr_t  	*busaddr;
466 	ddi_umem_cookie_t	*umem_cookie;
467 	drm_dma_handle_t	*dmah_sg;
468 	drm_dma_handle_t	*dmah_gart; /* Handle to PCI memory */
469 } drm_sg_mem_t;
470 
471 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
472 
473 /* BEGIN CSTYLED */
474 typedef struct drm_local_map {
475 	unsigned long	offset;  /*  Physical address (0 for SAREA)	*/
476 	unsigned long	size;	 /* Physical size (bytes)		*/
477 	drm_map_type_t	type;	 /* Type of memory mapped		*/
478 	drm_map_flags_t flags;	 /* Flags				*/
479 	void		*handle; /* User-space: "Handle" to pass to mmap */
480 				 /* Kernel-space: kernel-virtual address */
481 	int		mtrr;	 /* Boolean: MTRR used 			*/
482 				 /* Private data			*/
483 	int		rid;	 /* PCI resource ID for bus_space 	*/
484 	int		kernel_owned; /* Boolean: 1= initmapped, 0= addmapped */
485 	caddr_t		dev_addr;	  /* base device address 	*/
486 	ddi_acc_handle_t  dev_handle;	  /* The data access handle 	*/
487 	ddi_umem_cookie_t drm_umem_cookie; /* For SAREA alloc and free  */
488 	TAILQ_ENTRY(drm_local_map) link;
489 } drm_local_map_t;
490 /* END CSTYLED */
491 
492 typedef TAILQ_HEAD(drm_vbl_sig_list, drm_vbl_sig) drm_vbl_sig_list_t;
493 typedef struct drm_vbl_sig {
494 	TAILQ_ENTRY(drm_vbl_sig) link;
495 	unsigned int	sequence;
496 	int		signo;
497 	int		pid;
498 } drm_vbl_sig_t;
499 
500 
501 /* used for clone device */
502 typedef TAILQ_HEAD(drm_cminor_list, drm_cminor) drm_cminor_list_t;
503 typedef struct drm_cminor {
504 	TAILQ_ENTRY(drm_cminor) link;
505 	drm_file_t		*fpriv;
506 	int			minor;
507 } drm_cminor_t;
508 
509 /* location of GART table */
510 #define	DRM_ATI_GART_MAIN	1
511 #define	DRM_ATI_GART_FB		2
512 
513 typedef struct ati_pcigart_info {
514 	int gart_table_location;
515 	int is_pcie;
516 	void *addr;
517 	dma_addr_t bus_addr;
518 	drm_local_map_t mapping;
519 } drm_ati_pcigart_info;
520 
521 
522 
523 /* DRM device structure */
524 struct drm_device;
525 struct drm_driver_info {
526 	int (*load)(struct drm_device *, unsigned long);
527 	int (*firstopen)(struct drm_device *);
528 	int (*open)(struct drm_device *, drm_file_t *);
529 	void (*preclose)(struct drm_device *, drm_file_t *);
530 	void (*postclose)(struct drm_device *, drm_file_t *);
531 	void (*lastclose)(struct drm_device *);
532 	int (*unload)(struct drm_device *);
533 	void (*reclaim_buffers_locked)(struct drm_device *, drm_file_t *);
534 	int (*presetup)(struct drm_device *);
535 	int (*postsetup)(struct drm_device *);
536 	int (*open_helper)(struct drm_device *, drm_file_t *);
537 	void (*free_filp_priv)(struct drm_device *, drm_file_t *);
538 	void (*release)(struct drm_device *, void *);
539 	int (*dma_ioctl)(DRM_IOCTL_ARGS);
540 	void (*dma_ready)(struct drm_device *);
541 	int (*dma_quiescent)(struct drm_device *);
542 	int (*dma_flush_block_and_flush)(struct drm_device *,
543 			int, drm_lock_flags_t);
544 	int (*dma_flush_unblock)(struct drm_device *, int,
545 					drm_lock_flags_t);
546 	int (*context_ctor)(struct drm_device *, int);
547 	int (*context_dtor)(struct drm_device *, int);
548 	int (*kernel_context_switch)(struct drm_device *, int, int);
549 	int (*kernel_context_switch_unlock)(struct drm_device *);
550 	int (*device_is_agp) (struct drm_device *);
551 	void (*irq_preinstall)(struct drm_device *);
552 	void (*irq_postinstall)(struct drm_device *);
553 	void (*irq_uninstall)(struct drm_device *dev);
554 	uint_t (*irq_handler)(DRM_IRQ_ARGS);
555 	int (*vblank_wait)(struct drm_device *, unsigned int *);
556 	int (*vblank_wait2)(struct drm_device *, unsigned int *);
557 
558 	drm_ioctl_desc_t *driver_ioctls;
559 	int	max_driver_ioctl;
560 
561 	int	buf_priv_size;
562 	int	driver_major;
563 	int	driver_minor;
564 	int	driver_patchlevel;
565 	const char *driver_name;	/* Simple driver name		   */
566 	const char *driver_desc;	/* Longer driver name		   */
567 	const char *driver_date;	/* Date of last major changes.	   */
568 
569 	unsigned use_agp :1;
570 	unsigned require_agp :1;
571 	unsigned use_sg :1;
572 	unsigned use_dma :1;
573 	unsigned use_pci_dma :1;
574 	unsigned use_dma_queue :1;
575 	unsigned use_irq :1;
576 	unsigned use_vbl_irq :1;
577 	unsigned use_vbl_irq2 :1;
578 	unsigned use_mtrr :1;
579 };
580 
581 
582 /*
583  * hardware-specific code needs to initialize mutexes which
584  * can be used in interrupt context, so they need to know
585  * the interrupt priority. Interrupt cookie in drm_device
586  * structure is the intr_block field.
587  */
588 #define	DRM_INTR_PRI(dev) \
589 	DDI_INTR_PRI((dev)->intr_block)
590 
591 struct drm_device {
592 	drm_driver_t	*driver;
593 	drm_cminor_list_t	minordevs;
594 	dev_info_t *dip;
595 	void	*drm_handle;
596 	int drm_supported;
597 	const char *desc; /* current driver description */
598 	kmutex_t *irq_mutex;
599 	kcondvar_t *irq_cv;
600 
601 	ddi_iblock_cookie_t intr_block;
602 	uint32_t	pci_device;	/* PCI device id */
603 	uint32_t	pci_vendor;
604 	char		*unique;	/* Unique identifier: e.g., busid  */
605 	int		unique_len;	/* Length of unique field	   */
606 	int		if_version;	/* Highest interface version set */
607 	int		flags;	/* Flags to open(2)		   */
608 
609 	/* Locks */
610 	kmutex_t	  dma_lock;	/* protects dev->dma */
611 	kmutex_t	  irq_lock;	/* protects irq condition checks */
612 	kmutex_t	  dev_lock;	/* protects everything else */
613 	drm_lock_data_t   lock;		/* Information on hardware lock    */
614 
615 	/* Usage Counters */
616 	int		  open_count;	/* Outstanding files open	   */
617 	int		  buf_use;	/* Buffers in use -- cannot alloc  */
618 
619 	/* Performance counters */
620 	unsigned long	  counters;
621 	drm_stat_type_t	  types[15];
622 	uint32_t	  counts[15];
623 
624 	/* Authentication */
625 	drm_file_list_t   files;
626 	drm_magic_head_t  magiclist[DRM_HASH_SIZE];
627 
628 	/* Linked list of mappable regions. Protected by dev_lock */
629 	drm_map_list_t	  maplist;
630 
631 	drm_local_map_t	  **context_sareas;
632 	int		  max_context;
633 
634 	/* DMA queues (contexts) */
635 	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
636 
637 	/* Context support */
638 	int		  irq;		/* Interrupt used by board	   */
639 	int		  irq_enabled;	/* True if the irq handler is enabled */
640 	int		  pci_domain;
641 	int		  pci_bus;
642 	int		  pci_slot;
643 	int		  pci_func;
644 	atomic_t	  context_flag;	/* Context swapping flag	   */
645 	int		  last_context;	/* Last current context		   */
646 
647 	atomic_t	vbl_received;
648 	atomic_t	vbl_received2;
649 	drm_vbl_sig_list_t vbl_sig_list;
650 	drm_vbl_sig_list_t vbl_sig_list2;
651 	wait_queue_head_t	vbl_queue;	/* vbl wait channel */
652 
653 	kmutex_t	tasklet_lock;
654 	void (*locked_tasklet_func)(struct drm_device *dev);
655 
656 	pid_t		  buf_pgid;
657 	drm_agp_head_t    *agp;
658 	drm_sg_mem_t	  *sg;  /* Scatter gather memory */
659 	uint32_t	  *ctx_bitmap;
660 	void		  *dev_private;
661 	unsigned int	  agp_buffer_token;
662 	drm_local_map_t   *agp_buffer_map;
663 
664 	kstat_t		  *asoft_ksp; /* kstat support */
665 
666 	/* name Drawable information */
667 	kmutex_t	drw_lock;
668 	unsigned int drw_bitfield_length;
669 	u32 *drw_bitfield;
670 	unsigned int drw_info_length;
671 	drm_drawable_info_t **drw_info;
672 
673 	/*
674 	 * Saving S3 context
675 	 */
676 	void		  *s3_private;
677 };
678 
679 /* Memory management support (drm_memory.c) */
680 void	drm_mem_init(void);
681 void	drm_mem_uninit(void);
682 void	*drm_alloc(size_t, int);
683 void	*drm_calloc(size_t, size_t, int);
684 void	*drm_realloc(void *, size_t, size_t, int);
685 void	drm_free(void *, size_t, int);
686 int 	drm_ioremap(drm_device_t *, drm_local_map_t *);
687 void	drm_ioremapfree(drm_local_map_t *);
688 
689 void drm_core_ioremap(struct drm_local_map *, struct drm_device *);
690 void drm_core_ioremapfree(struct drm_local_map *, struct drm_device *);
691 
692 void drm_pci_free(drm_device_t *, drm_dma_handle_t *);
693 void *drm_pci_alloc(drm_device_t *, size_t, size_t, dma_addr_t, int);
694 
695 struct drm_local_map *drm_core_findmap(struct drm_device *, unsigned long);
696 
697 int	drm_context_switch(drm_device_t *, int, int);
698 int	drm_context_switch_complete(drm_device_t *, int);
699 int	drm_ctxbitmap_init(drm_device_t *);
700 void	drm_ctxbitmap_cleanup(drm_device_t *);
701 void	drm_ctxbitmap_free(drm_device_t *, int);
702 int	drm_ctxbitmap_next(drm_device_t *);
703 
704 /* Locking IOCTL support (drm_lock.c) */
705 int	drm_lock_take(drm_lock_data_t *, unsigned int);
706 int	drm_lock_transfer(drm_device_t *,
707 			volatile unsigned int *, unsigned int);
708 int	drm_lock_free(drm_device_t *,
709 		    volatile unsigned int *, unsigned int);
710 
711 /* Buffer management support (drm_bufs.c) */
712 unsigned long drm_get_resource_start(drm_device_t *, unsigned int);
713 unsigned long drm_get_resource_len(drm_device_t *, unsigned int);
714 int	drm_initmap(drm_device_t *, unsigned long, unsigned long,
715     unsigned int, int, int);
716 void	drm_rmmap(drm_device_t *, drm_local_map_t *);
717 int	drm_addmap(drm_device_t *, unsigned long, unsigned long,
718     drm_map_type_t, drm_map_flags_t, drm_local_map_t **);
719 int	drm_order(unsigned long);
720 
721 /* DMA support (drm_dma.c) */
722 int	drm_dma_setup(drm_device_t *);
723 void	drm_dma_takedown(drm_device_t *);
724 void	drm_free_buffer(drm_device_t *, drm_buf_t *);
725 void	drm_reclaim_buffers(drm_device_t *, drm_file_t *);
726 #define	drm_core_reclaim_buffers	drm_reclaim_buffers
727 
728 /* IRQ support (drm_irq.c) */
729 int	drm_irq_install(drm_device_t *);
730 int	drm_irq_uninstall(drm_device_t *);
731 uint_t	drm_irq_handler(DRM_IRQ_ARGS);
732 void	drm_driver_irq_preinstall(drm_device_t *);
733 void	drm_driver_irq_postinstall(drm_device_t *);
734 void	drm_driver_irq_uninstall(drm_device_t *);
735 int	drm_vblank_wait(drm_device_t *, unsigned int *);
736 void	drm_vbl_send_signals(drm_device_t *);
737 void    drm_locked_tasklet(drm_device_t *, void(*func)(drm_device_t *));
738 
739 /* AGP/GART support (drm_agpsupport.c) */
740 int	drm_device_is_agp(drm_device_t *);
741 int 	drm_device_is_pcie(drm_device_t *);
742 drm_agp_head_t *drm_agp_init(drm_device_t *);
743 void	drm_agp_fini(drm_device_t *);
744 int 	drm_agp_do_release(drm_device_t *);
745 void	*drm_agp_allocate_memory(size_t, uint32_t);
746 int	drm_agp_free_memory(void *);
747 int	drm_agp_bind_memory(unsigned int, uint32_t, drm_device_t *);
748 int	drm_agp_unbind_memory(unsigned long, drm_device_t *);
749 
750 /* kstat support (drm_kstats.c) */
751 int	drm_init_kstats(drm_device_t *);
752 void	drm_fini_kstats(drm_device_t *);
753 
754 /* Scatter Gather Support (drm_scatter.c) */
755 void	drm_sg_cleanup(drm_device_t *, drm_sg_mem_t *);
756 
757 /* ATI PCIGART support (ati_pcigart.c) */
758 int	drm_ati_pcigart_init(drm_device_t *, drm_ati_pcigart_info *);
759 int	drm_ati_pcigart_cleanup(drm_device_t *, drm_ati_pcigart_info *);
760 
761 /* Locking IOCTL support (drm_drv.c) */
762 int	drm_lock(DRM_IOCTL_ARGS);
763 int	drm_unlock(DRM_IOCTL_ARGS);
764 int	drm_version(DRM_IOCTL_ARGS);
765 int	drm_setversion(DRM_IOCTL_ARGS);
766 
767 /* Misc. IOCTL support (drm_ioctl.c) */
768 int	drm_irq_by_busid(DRM_IOCTL_ARGS);
769 int	drm_getunique(DRM_IOCTL_ARGS);
770 int	drm_setunique(DRM_IOCTL_ARGS);
771 int	drm_getmap(DRM_IOCTL_ARGS);
772 int	drm_getclient(DRM_IOCTL_ARGS);
773 int	drm_getstats(DRM_IOCTL_ARGS);
774 int	drm_noop(DRM_IOCTL_ARGS);
775 
776 /* Context IOCTL support (drm_context.c) */
777 int	drm_resctx(DRM_IOCTL_ARGS);
778 int	drm_addctx(DRM_IOCTL_ARGS);
779 int	drm_modctx(DRM_IOCTL_ARGS);
780 int	drm_getctx(DRM_IOCTL_ARGS);
781 int	drm_switchctx(DRM_IOCTL_ARGS);
782 int	drm_newctx(DRM_IOCTL_ARGS);
783 int	drm_rmctx(DRM_IOCTL_ARGS);
784 int	drm_setsareactx(DRM_IOCTL_ARGS);
785 int	drm_getsareactx(DRM_IOCTL_ARGS);
786 
787 /* Drawable IOCTL support (drm_drawable.c) */
788 int	drm_adddraw(DRM_IOCTL_ARGS);
789 int	drm_rmdraw(DRM_IOCTL_ARGS);
790 
791 /* Authentication IOCTL support (drm_auth.c) */
792 int	drm_getmagic(DRM_IOCTL_ARGS);
793 int	drm_authmagic(DRM_IOCTL_ARGS);
794 int	drm_remove_magic(drm_device_t *, drm_magic_t);
795 drm_file_t	*drm_find_file(drm_device_t *, drm_magic_t);
796 /* Buffer management support (drm_bufs.c) */
797 int	drm_addmap_ioctl(DRM_IOCTL_ARGS);
798 int	drm_rmmap_ioctl(DRM_IOCTL_ARGS);
799 int	drm_addbufs_ioctl(DRM_IOCTL_ARGS);
800 int	drm_infobufs(DRM_IOCTL_ARGS);
801 int	drm_markbufs(DRM_IOCTL_ARGS);
802 int	drm_freebufs(DRM_IOCTL_ARGS);
803 int	drm_mapbufs(DRM_IOCTL_ARGS);
804 
805 /* DMA support (drm_dma.c) */
806 int	drm_dma(DRM_IOCTL_ARGS);
807 
808 /* IRQ support (drm_irq.c) */
809 int	drm_control(DRM_IOCTL_ARGS);
810 int	drm_wait_vblank(DRM_IOCTL_ARGS);
811 
812 /* AGP/GART support (drm_agpsupport.c) */
813 int	drm_agp_acquire(DRM_IOCTL_ARGS);
814 int	drm_agp_release(DRM_IOCTL_ARGS);
815 int	drm_agp_enable(DRM_IOCTL_ARGS);
816 int	drm_agp_info(DRM_IOCTL_ARGS);
817 int	drm_agp_alloc(DRM_IOCTL_ARGS);
818 int	drm_agp_free(DRM_IOCTL_ARGS);
819 int	drm_agp_unbind(DRM_IOCTL_ARGS);
820 int	drm_agp_bind(DRM_IOCTL_ARGS);
821 
822 /* Scatter Gather Support (drm_scatter.c) */
823 int	drm_sg_alloc(DRM_IOCTL_ARGS);
824 int	drm_sg_free(DRM_IOCTL_ARGS);
825 
826 extern int drm_debug_flag;
827 
828 
829 /* We add function to support DRM_DEBUG,DRM_ERROR,DRM_INFO */
830 extern void drm_debug(const char *fmt, ...);
831 extern void drm_error(const char *fmt, ...);
832 extern void drm_info(const char *fmt, ...);
833 
834 #ifdef DEBUG
835 #define	DRM_DEBUG		if (drm_debug_flag >= 2) drm_debug
836 #define	DRM_INFO		if (drm_debug_flag >= 1) drm_info
837 #else
838 #define	DRM_DEBUG(...)
839 #define	DRM_INFO(...)
840 #endif
841 
842 #define	DRM_ERROR		drm_error
843 
844 
845 #define	MAX_INSTNUMS 16
846 
847 extern int drm_dev_to_instance(dev_t);
848 extern int drm_dev_to_minor(dev_t);
849 extern void *drm_supp_register(dev_info_t *, drm_device_t *);
850 extern int drm_supp_unregister(void *);
851 
852 extern int drm_open(drm_device_t *, drm_cminor_t *, int, int, cred_t *);
853 extern int drm_close(drm_device_t *, int, int, int, cred_t *);
854 extern int drm_attach(drm_device_t *);
855 extern int drm_detach(drm_device_t *);
856 extern int drm_probe(drm_device_t *, drm_pci_id_list_t *);
857 
858 extern int drm_pci_init(drm_device_t *);
859 extern void drm_pci_end(drm_device_t *);
860 extern int pci_get_info(drm_device_t *, int *, int *, int *);
861 extern int pci_get_irq(drm_device_t *);
862 extern int pci_get_vendor(drm_device_t *);
863 extern int pci_get_device(drm_device_t *);
864 
865 extern drm_drawable_info_t *drm_get_drawable_info(drm_device_t *,
866 	drm_drawable_t);
867 
868 /* File Operations helpers (drm_fops.c) */
869 extern drm_file_t *drm_find_file_by_proc(drm_device_t *, cred_t *);
870 extern drm_cminor_t *drm_find_file_by_minor(drm_device_t *, int);
871 extern int drm_open_helper(drm_device_t *, drm_cminor_t *, int, int,
872     cred_t *);
873 
874 #endif	/* _DRMP_H */
875