xref: /freebsd/sys/dev/drm2/drmP.h (revision d184218c)
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$");
36 
37 #ifndef _DRM_P_H_
38 #define _DRM_P_H_
39 
40 #if defined(_KERNEL) || defined(__KERNEL__)
41 
42 struct drm_device;
43 struct drm_file;
44 
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/ktr.h>
50 #include <sys/module.h>
51 #include <sys/systm.h>
52 #include <sys/conf.h>
53 #include <sys/sglist.h>
54 #include <sys/stat.h>
55 #include <sys/priv.h>
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/rwlock.h>
62 #include <sys/selinfo.h>
63 #include <sys/sysctl.h>
64 #include <sys/bus.h>
65 #include <sys/queue.h>
66 #include <sys/signalvar.h>
67 #include <sys/poll.h>
68 #include <sys/sbuf.h>
69 #include <sys/taskqueue.h>
70 #include <sys/tree.h>
71 #include <vm/vm.h>
72 #include <vm/pmap.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_pager.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_phys.h>
81 #include <machine/param.h>
82 #include <machine/pmap.h>
83 #include <machine/bus.h>
84 #include <machine/resource.h>
85 #if defined(__i386__) || defined(__amd64__)
86 #include <machine/specialreg.h>
87 #endif
88 #include <machine/sysarch.h>
89 #include <sys/endian.h>
90 #include <sys/mman.h>
91 #include <sys/rman.h>
92 #include <sys/memrange.h>
93 #include <dev/agp/agpvar.h>
94 #include <sys/agpio.h>
95 #include <sys/mutex.h>
96 #include <dev/pci/pcivar.h>
97 #include <dev/pci/pcireg.h>
98 #include <sys/selinfo.h>
99 #include <sys/bus.h>
100 
101 #include <dev/drm2/drm.h>
102 #include <dev/drm2/drm_atomic.h>
103 #include <dev/drm2/drm_internal.h>
104 #include <dev/drm2/drm_linux_list.h>
105 #include <dev/drm2/drm_gem_names.h>
106 #include <dev/drm2/drm_mm.h>
107 #include <dev/drm2/drm_hashtab.h>
108 
109 #include "opt_compat.h"
110 #include "opt_drm.h"
111 #ifdef DRM_DEBUG
112 #undef DRM_DEBUG
113 #define DRM_DEBUG_DEFAULT_ON 1
114 #endif /* DRM_DEBUG */
115 
116 #define	DRM_DEBUGBITS_DEBUG		0x1
117 #define	DRM_DEBUGBITS_KMS		0x2
118 #define	DRM_DEBUGBITS_FAILED_IOCTL	0x4
119 
120 #undef DRM_LINUX
121 #define DRM_LINUX 0
122 
123 /* driver capabilities and requirements mask */
124 #define DRIVER_USE_AGP     0x1
125 #define DRIVER_REQUIRE_AGP 0x2
126 #define DRIVER_USE_MTRR    0x4
127 #define DRIVER_PCI_DMA     0x8
128 #define DRIVER_SG          0x10
129 #define DRIVER_HAVE_DMA    0x20
130 #define DRIVER_HAVE_IRQ    0x40
131 #define DRIVER_IRQ_SHARED  0x80
132 #define DRIVER_IRQ_VBL     0x100
133 #define DRIVER_DMA_QUEUE   0x200
134 #define DRIVER_FB_DMA      0x400
135 #define DRIVER_IRQ_VBL2    0x800
136 #define DRIVER_GEM         0x1000
137 #define DRIVER_MODESET     0x2000
138 #define DRIVER_USE_PLATFORM_DEVICE  0x4000
139 #define	DRIVER_LOCKLESS_IRQ 0x8000
140 
141 
142 #define DRM_HASH_SIZE	      16 /* Size of key hash table		  */
143 #define DRM_KERNEL_CONTEXT    0	 /* Change drm_resctx if changed	  */
144 #define DRM_RESERVED_CONTEXTS 1	 /* Change drm_resctx if changed	  */
145 
146 #define	DRM_GEM_MAPPING_MASK	(3ULL << 62)
147 #define	DRM_GEM_MAPPING_KEY	(2ULL << 62) /* Non-canonical address form */
148 #define	DRM_GEM_MAX_IDX		0x3fffff
149 #define	DRM_GEM_MAPPING_IDX(o)	(((o) >> 40) & DRM_GEM_MAX_IDX)
150 #define	DRM_GEM_MAPPING_OFF(i)	(((uint64_t)(i)) << 40)
151 #define	DRM_GEM_MAPPING_MAPOFF(o) \
152     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
153 
154 MALLOC_DECLARE(DRM_MEM_DMA);
155 MALLOC_DECLARE(DRM_MEM_SAREA);
156 MALLOC_DECLARE(DRM_MEM_DRIVER);
157 MALLOC_DECLARE(DRM_MEM_MAGIC);
158 MALLOC_DECLARE(DRM_MEM_IOCTLS);
159 MALLOC_DECLARE(DRM_MEM_MAPS);
160 MALLOC_DECLARE(DRM_MEM_BUFS);
161 MALLOC_DECLARE(DRM_MEM_SEGS);
162 MALLOC_DECLARE(DRM_MEM_PAGES);
163 MALLOC_DECLARE(DRM_MEM_FILES);
164 MALLOC_DECLARE(DRM_MEM_QUEUES);
165 MALLOC_DECLARE(DRM_MEM_CMDS);
166 MALLOC_DECLARE(DRM_MEM_MAPPINGS);
167 MALLOC_DECLARE(DRM_MEM_BUFLISTS);
168 MALLOC_DECLARE(DRM_MEM_AGPLISTS);
169 MALLOC_DECLARE(DRM_MEM_CTXBITMAP);
170 MALLOC_DECLARE(DRM_MEM_SGLISTS);
171 MALLOC_DECLARE(DRM_MEM_DRAWABLE);
172 MALLOC_DECLARE(DRM_MEM_MM);
173 MALLOC_DECLARE(DRM_MEM_HASHTAB);
174 MALLOC_DECLARE(DRM_MEM_KMS);
175 
176 SYSCTL_DECL(_hw_drm);
177 
178 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
179 
180 				/* Internal types and structures */
181 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
182 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
183 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
184 
185 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
186 
187 #define __OS_HAS_AGP	1
188 
189 #define DRM_DEV_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
190 #define DRM_DEV_UID	0
191 #define DRM_DEV_GID	0
192 
193 #define wait_queue_head_t	atomic_t
194 #define DRM_WAKEUP(w)		wakeup((void *)w)
195 #define DRM_WAKEUP_INT(w)	wakeup(w)
196 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
197 
198 #define DRM_CURPROC		curthread
199 #define DRM_STRUCTPROC		struct thread
200 #define DRM_SPINTYPE		struct mtx
201 #define DRM_SPININIT(l,name)	mtx_init(l, name, NULL, MTX_DEF)
202 #define DRM_SPINUNINIT(l)	mtx_destroy(l)
203 #define DRM_SPINLOCK(l)		mtx_lock(l)
204 #define DRM_SPINUNLOCK(u)	mtx_unlock(u)
205 #define DRM_SPINLOCK_IRQSAVE(l, irqflags) do {		\
206 	mtx_lock(l);					\
207 	(void)irqflags;					\
208 } while (0)
209 #define DRM_SPINUNLOCK_IRQRESTORE(u, irqflags) mtx_unlock(u)
210 #define DRM_SPINLOCK_ASSERT(l)	mtx_assert(l, MA_OWNED)
211 #define DRM_CURRENTPID		curthread->td_proc->p_pid
212 #define DRM_LOCK(dev)		sx_xlock(&(dev)->dev_struct_lock)
213 #define DRM_UNLOCK(dev) 	sx_xunlock(&(dev)->dev_struct_lock)
214 #define	DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)			\
215     (sx_sleep((chan), &(dev)->dev_struct_lock, (flags), (msg), (timeout)))
216 #if defined(INVARIANTS)
217 #define	DRM_LOCK_ASSERT(dev)	sx_assert(&(dev)->dev_struct_lock, SA_XLOCKED)
218 #define	DRM_UNLOCK_ASSERT(dev)	sx_assert(&(dev)->dev_struct_lock, SA_UNLOCKED)
219 #else
220 #define	DRM_LOCK_ASSERT(d)
221 #define	DRM_UNLOCK_ASSERT(d)
222 #endif
223 
224 #define DRM_SYSCTL_HANDLER_ARGS	(SYSCTL_HANDLER_ARGS)
225 
226 #define DRM_IRQ_ARGS		void *arg
227 typedef void			irqreturn_t;
228 #define IRQ_HANDLED		/* nothing */
229 #define IRQ_NONE		/* nothing */
230 
231 #define unlikely(x)            __builtin_expect(!!(x), 0)
232 #define likely(x)              __builtin_expect(!!(x), 1)
233 #define container_of(ptr, type, member) ({			\
234 	__typeof( ((type *)0)->member ) *__mptr = (ptr);	\
235 	(type *)( (char *)__mptr - offsetof(type,member) );})
236 
237 enum {
238 	DRM_IS_NOT_AGP,
239 	DRM_IS_AGP,
240 	DRM_MIGHT_BE_AGP
241 };
242 #define DRM_AGP_MEM		struct agp_memory_info
243 
244 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
245 
246 #define PAGE_ALIGN(addr) round_page(addr)
247 /* DRM_SUSER returns true if the user is superuser */
248 #define DRM_SUSER(p)		(priv_check(p, PRIV_DRIVER) == 0)
249 #define DRM_AGP_FIND_DEVICE()	agp_find_device()
250 #define DRM_MTRR_WC		MDF_WRITECOMBINE
251 #define jiffies			ticks
252 #define	jiffies_to_msecs(x)	(((int64_t)(x)) * 1000 / hz)
253 #define	msecs_to_jiffies(x)	(((int64_t)(x)) * hz / 1000)
254 #define	time_after(a,b)		((long)(b) - (long)(a) < 0)
255 #define	time_after_eq(a,b)	((long)(b) - (long)(a) <= 0)
256 #define drm_msleep(x, msg)	pause((msg), ((int64_t)(x)) * hz / 1000)
257 
258 typedef vm_paddr_t dma_addr_t;
259 typedef uint64_t u64;
260 typedef uint32_t u32;
261 typedef uint16_t u16;
262 typedef uint8_t u8;
263 typedef int64_t s64;
264 typedef int32_t s32;
265 typedef int16_t s16;
266 typedef int8_t s8;
267 
268 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
269  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
270  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
271  */
272 #define DRM_READMEMORYBARRIER()		rmb()
273 #define DRM_WRITEMEMORYBARRIER()	wmb()
274 #define DRM_MEMORYBARRIER()		mb()
275 
276 #define DRM_READ8(map, offset)						\
277 	*(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +		\
278 	    (vm_offset_t)(offset))
279 #define DRM_READ16(map, offset)						\
280 	le16toh(*(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) +	\
281 	    (vm_offset_t)(offset)))
282 #define DRM_READ32(map, offset)						\
283 	le32toh(*(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) +	\
284 	    (vm_offset_t)(offset)))
285 #define DRM_READ64(map, offset)						\
286 	le64toh(*(volatile u_int64_t *)(((vm_offset_t)(map)->virtual) +	\
287 	    (vm_offset_t)(offset)))
288 #define DRM_WRITE8(map, offset, val)					\
289 	*(volatile u_int8_t *)(((vm_offset_t)(map)->virtual) +		\
290 	    (vm_offset_t)(offset)) = val
291 #define DRM_WRITE16(map, offset, val)					\
292 	*(volatile u_int16_t *)(((vm_offset_t)(map)->virtual) +		\
293 	    (vm_offset_t)(offset)) = htole16(val)
294 #define DRM_WRITE32(map, offset, val)					\
295 	*(volatile u_int32_t *)(((vm_offset_t)(map)->virtual) +		\
296 	    (vm_offset_t)(offset)) = htole32(val)
297 #define DRM_WRITE64(map, offset, val)					\
298 	*(volatile u_int64_t *)(((vm_offset_t)(map)->virtual) +		\
299 	    (vm_offset_t)(offset)) = htole64(val)
300 
301 #define DRM_VERIFYAREA_READ( uaddr, size )		\
302 	(!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
303 
304 #define DRM_COPY_TO_USER(user, kern, size) \
305 	copyout(kern, user, size)
306 #define DRM_COPY_FROM_USER(kern, user, size) \
307 	copyin(user, kern, size)
308 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) 	\
309 	copyin(arg2, arg1, arg3)
310 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)	\
311 	copyout(arg2, arg1, arg3)
312 #define DRM_GET_USER_UNCHECKED(val, uaddr)		\
313 	((val) = fuword32(uaddr), 0)
314 
315 #define cpu_to_le32(x) htole32(x)
316 #define le32_to_cpu(x) le32toh(x)
317 
318 #define DRM_HZ			hz
319 #define DRM_UDELAY(udelay)	DELAY(udelay)
320 #define DRM_TIME_SLICE		(hz/20)  /* Time slice for GLXContexts	  */
321 
322 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {	\
323 	(_map) = (_dev)->context_sareas[_ctx];		\
324 } while(0)
325 
326 #define LOCK_TEST_WITH_RETURN(dev, file_priv)				\
327 do {									\
328 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
329 	     dev->lock.file_priv != file_priv) {			\
330 		DRM_ERROR("%s called without lock held\n",		\
331 			   __FUNCTION__);				\
332 		return EINVAL;						\
333 	}								\
334 } while (0)
335 
336 /* Returns -errno to shared code */
337 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
338 for ( ret = 0 ; !ret && !(condition) ; ) {			\
339 	DRM_UNLOCK(dev);						\
340 	mtx_lock(&dev->irq_lock);				\
341 	if (!(condition))					\
342 	    ret = -mtx_sleep(&(queue), &dev->irq_lock, 		\
343 		PCATCH, "drmwtq", (timeout));			\
344 	mtx_unlock(&dev->irq_lock);				\
345 	DRM_LOCK(dev);						\
346 }
347 
348 #define DRM_ERROR(fmt, ...) \
349 	printf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
350 	    DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
351 
352 #define DRM_INFO(fmt, ...)  printf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
353 
354 #define DRM_DEBUG(fmt, ...) do {					\
355 	if ((drm_debug_flag & DRM_DEBUGBITS_DEBUG) != 0)		\
356 		printf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
357 			__func__ , ##__VA_ARGS__);			\
358 } while (0)
359 
360 #define DRM_DEBUG_KMS(fmt, ...) do {					\
361 	if ((drm_debug_flag & DRM_DEBUGBITS_KMS) != 0)			\
362 		printf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
363 			__func__ , ##__VA_ARGS__);			\
364 } while (0)
365 
366 #define DRM_DEBUG_DRIVER(fmt, ...) do {					\
367 	if ((drm_debug_flag & DRM_DEBUGBITS_KMS) != 0)			\
368 		printf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
369 			__func__ , ##__VA_ARGS__);			\
370 } while (0)
371 
372 typedef struct drm_pci_id_list
373 {
374 	int vendor;
375 	int device;
376 	long driver_private;
377 	char *name;
378 } drm_pci_id_list_t;
379 
380 struct drm_msi_blacklist_entry
381 {
382 	int vendor;
383 	int device;
384 };
385 
386 #define DRM_AUTH	0x1
387 #define DRM_MASTER	0x2
388 #define DRM_ROOT_ONLY	0x4
389 #define DRM_CONTROL_ALLOW 0x8
390 #define DRM_UNLOCKED	0x10
391 
392 typedef struct drm_ioctl_desc {
393 	unsigned long cmd;
394 	int (*func)(struct drm_device *dev, void *data,
395 		    struct drm_file *file_priv);
396 	int flags;
397 } drm_ioctl_desc_t;
398 /**
399  * Creates a driver or general drm_ioctl_desc array entry for the given
400  * ioctl, for use by drm_ioctl().
401  */
402 #define DRM_IOCTL_DEF(ioctl, func, flags) \
403 	[DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
404 
405 typedef struct drm_magic_entry {
406 	drm_magic_t	       magic;
407 	struct drm_file	       *priv;
408 	struct drm_magic_entry *next;
409 } drm_magic_entry_t;
410 
411 typedef struct drm_magic_head {
412 	struct drm_magic_entry *head;
413 	struct drm_magic_entry *tail;
414 } drm_magic_head_t;
415 
416 typedef struct drm_buf {
417 	int		  idx;	       /* Index into master buflist	     */
418 	int		  total;       /* Buffer size			     */
419 	int		  order;       /* log-base-2(total)		     */
420 	int		  used;	       /* Amount of buffer in use (for DMA)  */
421 	unsigned long	  offset;      /* Byte offset (used internally)	     */
422 	void		  *address;    /* Address of buffer		     */
423 	unsigned long	  bus_address; /* Bus address of buffer		     */
424 	struct drm_buf	  *next;       /* Kernel-only: used for free list    */
425 	__volatile__ int  pending;     /* On hardware DMA queue		     */
426 	struct drm_file   *file_priv;  /* Unique identifier of holding process */
427 	int		  context;     /* Kernel queue for this buffer	     */
428 	enum {
429 		DRM_LIST_NONE	 = 0,
430 		DRM_LIST_FREE	 = 1,
431 		DRM_LIST_WAIT	 = 2,
432 		DRM_LIST_PEND	 = 3,
433 		DRM_LIST_PRIO	 = 4,
434 		DRM_LIST_RECLAIM = 5
435 	}		  list;	       /* Which list we're on		     */
436 
437 	int		  dev_priv_size; /* Size of buffer private stoarge   */
438 	void		  *dev_private;  /* Per-buffer private storage       */
439 } drm_buf_t;
440 
441 typedef struct drm_freelist {
442 	int		  initialized; /* Freelist in use		   */
443 	atomic_t	  count;       /* Number of free buffers	   */
444 	drm_buf_t	  *next;       /* End pointer			   */
445 
446 	int		  low_mark;    /* Low water mark		   */
447 	int		  high_mark;   /* High water mark		   */
448 } drm_freelist_t;
449 
450 typedef struct drm_dma_handle {
451 	void *vaddr;
452 	bus_addr_t busaddr;
453 	bus_dma_tag_t tag;
454 	bus_dmamap_t map;
455 } drm_dma_handle_t;
456 
457 typedef struct drm_buf_entry {
458 	int		  buf_size;
459 	int		  buf_count;
460 	drm_buf_t	  *buflist;
461 	int		  seg_count;
462 	drm_dma_handle_t  **seglist;
463 	int		  page_order;
464 
465 	drm_freelist_t	  freelist;
466 } drm_buf_entry_t;
467 
468 /* Event queued up for userspace to read */
469 struct drm_pending_event {
470 	struct drm_event *event;
471 	struct list_head link;
472 	struct drm_file *file_priv;
473 	pid_t pid; /* pid of requester, no guarantee it's valid by the time
474 		      we deliver the event, for tracing only */
475 	void (*destroy)(struct drm_pending_event *event);
476 };
477 
478 typedef TAILQ_HEAD(drm_file_list, drm_file) drm_file_list_t;
479 struct drm_file {
480 	TAILQ_ENTRY(drm_file) link;
481 	struct drm_device *dev;
482 	int		  authenticated;
483 	int		  master;
484 	pid_t		  pid;
485 	uid_t		  uid;
486 	drm_magic_t	  magic;
487 	unsigned long	  ioctl_count;
488 
489 	void		 *driver_priv;
490 	struct drm_gem_names object_names;
491 
492 	int		  is_master;
493 	struct drm_master *masterp;
494 
495 	struct list_head  fbs;
496 
497 	struct list_head  event_list;
498 	int		  event_space;
499 	struct selinfo	  event_poll;
500 };
501 
502 typedef struct drm_lock_data {
503 	struct drm_hw_lock	*hw_lock;	/* Hardware lock		   */
504 	struct drm_file   *file_priv;   /* Unique identifier of holding process (NULL is kernel)*/
505 	int		  lock_queue;	/* Queue of blocked processes	   */
506 	unsigned long	  lock_time;	/* Time of last lock in jiffies	   */
507 } drm_lock_data_t;
508 
509 /* This structure, in the struct drm_device, is always initialized while the
510  * device
511  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
512  * when set marks that no further bufs may be allocated until device teardown
513  * occurs (when the last open of the device has closed).  The high/low
514  * watermarks of bufs are only touched by the X Server, and thus not
515  * concurrently accessed, so no locking is needed.
516  */
517 typedef struct drm_device_dma {
518 	drm_buf_entry_t	  bufs[DRM_MAX_ORDER+1];
519 	int		  buf_count;
520 	drm_buf_t	  **buflist;	/* Vector of pointers info bufs	   */
521 	int		  seg_count;
522 	int		  page_count;
523 	unsigned long	  *pagelist;
524 	unsigned long	  byte_count;
525 	enum {
526 		_DRM_DMA_USE_AGP = 0x01,
527 		_DRM_DMA_USE_SG  = 0x02
528 	} flags;
529 } drm_device_dma_t;
530 
531 typedef struct drm_agp_mem {
532 	void               *handle;
533 	unsigned long      bound; /* address */
534 	int                pages;
535 	struct drm_agp_mem *prev;
536 	struct drm_agp_mem *next;
537 } drm_agp_mem_t;
538 
539 typedef struct drm_agp_head {
540 	device_t	   agpdev;
541 	struct agp_info    info;
542 	const char         *chipset;
543 	drm_agp_mem_t      *memory;
544 	unsigned long      mode;
545 	int                enabled;
546 	int                acquired;
547 	unsigned long      base;
548    	int 		   mtrr;
549 	int		   cant_use_aperture;
550 	unsigned long	   page_mask;
551 } drm_agp_head_t;
552 
553 typedef struct drm_sg_mem {
554 	vm_offset_t vaddr;
555 	vm_paddr_t *busaddr;
556 	vm_pindex_t pages;
557 } drm_sg_mem_t;
558 
559 #define DRM_MAP_HANDLE_BITS	(sizeof(void *) == 4 ? 4 : 24)
560 #define DRM_MAP_HANDLE_SHIFT	(sizeof(void *) * 8 - DRM_MAP_HANDLE_BITS)
561 typedef TAILQ_HEAD(drm_map_list, drm_local_map) drm_map_list_t;
562 
563 typedef struct drm_local_map {
564 	unsigned long offset;	  /* Physical address (0 for SAREA)       */
565 	unsigned long size;	  /* Physical size (bytes)                */
566 	enum drm_map_type type;	  /* Type of memory mapped                */
567 	enum drm_map_flags flags; /* Flags                                */
568 	void *handle;		  /* User-space: "Handle" to pass to mmap */
569 				  /* Kernel-space: kernel-virtual address */
570 	int mtrr;		  /* Boolean: MTRR used                   */
571 				  /* Private data                         */
572 	int rid;		  /* PCI resource ID for bus_space        */
573 	void *virtual;		  /* Kernel-space: kernel-virtual address */
574 	struct resource *bsr;
575 	bus_space_tag_t bst;
576 	bus_space_handle_t bsh;
577 	drm_dma_handle_t *dmah;
578 	TAILQ_ENTRY(drm_local_map) link;
579 } drm_local_map_t;
580 
581 struct drm_vblank_info {
582 	wait_queue_head_t queue;	/* vblank wait queue */
583 	atomic_t count;			/* number of VBLANK interrupts */
584 					/* (driver must alloc the right number of counters) */
585 	atomic_t refcount;		/* number of users of vblank interrupts */
586 	u32 last;			/* protected by dev->vbl_lock, used */
587 					/* for wraparound handling */
588 	int enabled;			/* so we don't call enable more than */
589 					/* once per disable */
590 	int inmodeset;			/* Display driver is setting mode */
591 };
592 
593 /* Size of ringbuffer for vblank timestamps. Just double-buffer
594  * in initial implementation.
595  */
596 #define DRM_VBLANKTIME_RBSIZE 2
597 
598 /* Flags and return codes for get_vblank_timestamp() driver function. */
599 #define DRM_CALLED_FROM_VBLIRQ 1
600 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
601 #define DRM_VBLANKTIME_INVBL             (1 << 1)
602 
603 /* get_scanout_position() return flags */
604 #define DRM_SCANOUTPOS_VALID        (1 << 0)
605 #define DRM_SCANOUTPOS_INVBL        (1 << 1)
606 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
607 
608 /* location of GART table */
609 #define DRM_ATI_GART_MAIN 1
610 #define DRM_ATI_GART_FB   2
611 
612 #define DRM_ATI_GART_PCI  1
613 #define DRM_ATI_GART_PCIE 2
614 #define DRM_ATI_GART_IGP  3
615 
616 struct drm_ati_pcigart_info {
617 	int gart_table_location;
618 	int gart_reg_if;
619 	void *addr;
620 	dma_addr_t bus_addr;
621 	dma_addr_t table_mask;
622 	dma_addr_t member_mask;
623 	struct drm_dma_handle *table_handle;
624 	drm_local_map_t mapping;
625 	int table_size;
626 	struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
627 };
628 
629 typedef vm_paddr_t resource_size_t;
630 
631 /**
632  * GEM specific mm private for tracking GEM objects
633  */
634 struct drm_gem_mm {
635 	struct drm_open_hash offset_hash; /**< User token hash table for maps */
636 	struct unrhdr *idxunr;
637 };
638 
639 struct drm_gem_object {
640 	/** Reference count of this object */
641 	u_int refcount;
642 
643 	/** Handle count of this object. Each handle also holds a reference */
644 	u_int handle_count; /* number of handles on this object */
645 
646 	/** Related drm device */
647 	struct drm_device *dev;
648 
649 	/** File representing the shmem storage: filp in Linux parlance */
650 	vm_object_t vm_obj;
651 
652 	bool on_map;
653 	struct drm_hash_item map_list;
654 
655 	/**
656 	 * Size of the object, in bytes.  Immutable over the object's
657 	 * lifetime.
658 	 */
659 	size_t size;
660 
661 	/**
662 	 * Global name for this object, starts at 1. 0 means unnamed.
663 	 * Access is covered by the object_name_lock in the related drm_device
664 	 */
665 	int name;
666 
667 	/**
668 	 * Memory domains. These monitor which caches contain read/write data
669 	 * related to the object. When transitioning from one set of domains
670 	 * to another, the driver is called to ensure that caches are suitably
671 	 * flushed and invalidated
672 	 */
673 	uint32_t read_domains;
674 	uint32_t write_domain;
675 
676 	/**
677 	 * While validating an exec operation, the
678 	 * new read/write domain values are computed here.
679 	 * They will be transferred to the above values
680 	 * at the point that any cache flushing occurs
681 	 */
682 	uint32_t pending_read_domains;
683 	uint32_t pending_write_domain;
684 
685 	void *driver_private;
686 };
687 
688 #include "drm_crtc.h"
689 
690 #ifndef DMA_BIT_MASK
691 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
692 #endif
693 
694 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
695 
696 struct drm_driver_info {
697 	int	(*load)(struct drm_device *, unsigned long flags);
698 	int	(*firstopen)(struct drm_device *);
699 	int	(*open)(struct drm_device *, struct drm_file *);
700 	void	(*preclose)(struct drm_device *, struct drm_file *file_priv);
701 	void	(*postclose)(struct drm_device *, struct drm_file *);
702 	void	(*lastclose)(struct drm_device *);
703 	int	(*unload)(struct drm_device *);
704 	void	(*reclaim_buffers_locked)(struct drm_device *,
705 					  struct drm_file *file_priv);
706 	int	(*dma_ioctl)(struct drm_device *dev, void *data,
707 			     struct drm_file *file_priv);
708 	void	(*dma_ready)(struct drm_device *);
709 	int	(*dma_quiescent)(struct drm_device *);
710 	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
711 					     enum drm_lock_flags flags);
712 	int	(*dma_flush_unblock)(struct drm_device *, int context,
713 				     enum drm_lock_flags flags);
714 	int	(*context_ctor)(struct drm_device *dev, int context);
715 	int	(*context_dtor)(struct drm_device *dev, int context);
716 	int	(*kernel_context_switch)(struct drm_device *dev, int old,
717 					 int new);
718 	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
719 	void	(*irq_preinstall)(struct drm_device *dev);
720 	int	(*irq_postinstall)(struct drm_device *dev);
721 	void	(*irq_uninstall)(struct drm_device *dev);
722 	void	(*irq_handler)(DRM_IRQ_ARGS);
723 
724 	u32	(*get_vblank_counter)(struct drm_device *dev, int crtc);
725 	int	(*enable_vblank)(struct drm_device *dev, int crtc);
726 	void	(*disable_vblank)(struct drm_device *dev, int crtc);
727 	int	(*get_scanout_position)(struct drm_device *dev, int crtc,
728 		    int *vpos, int *hpos);
729 
730 	int	(*get_vblank_timestamp)(struct drm_device *dev, int crtc,
731 		    int *max_error, struct timeval *vblank_time,
732 		    unsigned flags);
733 
734 	int	(*gem_init_object)(struct drm_gem_object *obj);
735 	void	(*gem_free_object)(struct drm_gem_object *obj);
736 
737 	struct cdev_pager_ops *gem_pager_ops;
738 
739 	int	(*dumb_create)(struct drm_file *file_priv,
740 		    struct drm_device *dev, struct drm_mode_create_dumb *args);
741 	int	(*dumb_map_offset)(struct drm_file *file_priv,
742 		    struct drm_device *dev, uint32_t handle, uint64_t *offset);
743 	int	(*dumb_destroy)(struct drm_file *file_priv,
744 		    struct drm_device *dev, uint32_t handle);
745 
746 	int	(*sysctl_init)(struct drm_device *dev,
747 		    struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
748 	void	(*sysctl_cleanup)(struct drm_device *dev);
749 
750 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
751 
752 	/**
753 	 * Called by \c drm_device_is_agp.  Typically used to determine if a
754 	 * card is really attached to AGP or not.
755 	 *
756 	 * \param dev  DRM device handle
757 	 *
758 	 * \returns
759 	 * One of three values is returned depending on whether or not the
760 	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
761 	 * (return of 1), or may or may not be AGP (return of 2).
762 	 */
763 	int	(*device_is_agp) (struct drm_device * dev);
764 
765 	drm_ioctl_desc_t *ioctls;
766 #ifdef COMPAT_FREEBSD32
767 	drm_ioctl_desc_t *compat_ioctls;
768 	int	*compat_ioctls_nr;
769 #endif
770 	int	max_ioctl;
771 
772 	int	buf_priv_size;
773 
774 	int	major;
775 	int	minor;
776 	int	patchlevel;
777 	const char *name;		/* Simple driver name		   */
778 	const char *desc;		/* Longer driver name		   */
779 	const char *date;		/* Date of last major changes.	   */
780 
781 	u32 driver_features;
782 };
783 
784 /**
785  * DRM minor structure. This structure represents a drm minor number.
786  */
787 struct drm_minor {
788 	int index;			/**< Minor device number */
789 	int type;                       /**< Control or render */
790 	device_t kdev;			/**< OS device */
791 	struct drm_device *dev;
792 
793 	struct drm_master *master; /* currently active master for this node */
794 	struct list_head master_list;
795 	struct drm_mode_group mode_group;
796 };
797 
798 /* mode specified on the command line */
799 struct drm_cmdline_mode {
800 	bool specified;
801 	bool refresh_specified;
802 	bool bpp_specified;
803 	int xres, yres;
804 	int bpp;
805 	int refresh;
806 	bool rb;
807 	bool interlace;
808 	bool cvt;
809 	bool margins;
810 	enum drm_connector_force force;
811 };
812 
813 struct drm_pending_vblank_event {
814 	struct drm_pending_event base;
815 	int pipe;
816 	struct drm_event_vblank event;
817 };
818 
819 /* Length for the array of resource pointers for drm_get_resource_*. */
820 #define DRM_MAX_PCI_RESOURCE	6
821 
822 /**
823  * DRM device functions structure
824  */
825 struct drm_device {
826 	struct drm_driver_info *driver;
827 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
828 
829 	u_int16_t pci_device;		/* PCI device id */
830 	u_int16_t pci_vendor;		/* PCI vendor id */
831 
832 	char		  *unique;	/* Unique identifier: e.g., busid  */
833 	int		  unique_len;	/* Length of unique field	   */
834 	device_t	  device;	/* Device instance from newbus     */
835 	struct cdev	  *devnode;	/* Device number for mknod	   */
836 	int		  if_version;	/* Highest interface version set */
837 
838 	int		  flags;	/* Flags to open(2)		   */
839 
840 				/* Locks */
841 	struct mtx	  dma_lock;	/* protects dev->dma */
842 	struct mtx	  irq_lock;	/* protects irq condition checks */
843 	struct mtx	  dev_lock;	/* protects everything else */
844 	struct sx	  dev_struct_lock;
845 	DRM_SPINTYPE	  drw_lock;
846 
847 				/* Usage Counters */
848 	int		  open_count;	/* Outstanding files open	   */
849 	int		  buf_use;	/* Buffers in use -- cannot alloc  */
850 
851 				/* Performance counters */
852 	unsigned long     counters;
853 	enum drm_stat_type	types[15];
854 	atomic_t          counts[15];
855 
856 				/* Authentication */
857 	drm_file_list_t   files;
858 	drm_magic_head_t  magiclist[DRM_HASH_SIZE];
859 
860 	/* Linked list of mappable regions. Protected by dev_lock */
861 	drm_map_list_t	  maplist;
862 	struct unrhdr	  *map_unrhdr;
863 
864 	drm_local_map_t	  **context_sareas;
865 	int		  max_context;
866 
867 	drm_lock_data_t	  lock;		/* Information on hardware lock	   */
868 
869 				/* DMA queues (contexts) */
870 	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
871 
872 				/* Context support */
873 	int		  irq;		/* Interrupt used by board	   */
874 	int		  irq_enabled;	/* True if the irq handler is enabled */
875 	int		  msi_enabled;	/* MSI enabled */
876 	int		  irqrid;	/* Interrupt used by board */
877 	struct resource   *irqr;	/* Resource for interrupt used by board	   */
878 	void		  *irqh;	/* Handle from bus_setup_intr      */
879 
880 	/* Storage of resource pointers for drm_get_resource_* */
881 	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
882 	int		  pcirid[DRM_MAX_PCI_RESOURCE];
883 
884 	int		  pci_domain;
885 	int		  pci_bus;
886 	int		  pci_slot;
887 	int		  pci_func;
888 
889 	atomic_t	  context_flag;	/* Context swapping flag	   */
890 	int		  last_context;	/* Last current context		   */
891 
892 	int		  num_crtcs;
893 
894 	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
895 
896 				/* Sysctl support */
897 	struct drm_sysctl_info *sysctl;
898 	int		  sysctl_node_idx;
899 
900 	drm_agp_head_t    *agp;
901 	drm_sg_mem_t      *sg;  /* Scatter gather memory */
902 	atomic_t          *ctx_bitmap;
903 	void		  *dev_private;
904 	unsigned int	  agp_buffer_token;
905 	drm_local_map_t   *agp_buffer_map;
906 
907 	struct drm_minor *control;		/**< Control node for card */
908 	struct drm_minor *primary;		/**< render type primary screen head */
909 
910 	void		  *drm_ttm_bo;
911 	struct unrhdr	  *drw_unrhdr;
912 	/* RB tree of drawable infos */
913 	RB_HEAD(drawable_tree, bsd_drm_drawable_info) drw_head;
914 
915 	int vblank_disable_allowed;
916 
917 	atomic_t *_vblank_count;        /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
918 	struct timeval *_vblank_time;   /**< timestamp of current vblank_count (drivers must alloc right number of fields) */
919 	struct mtx vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
920 	struct mtx vbl_lock;
921 	atomic_t *vblank_refcount;      /* number of users of vblank interruptsper crtc */
922 	u32 *last_vblank;               /* protected by dev->vbl_lock, used */
923 					/* for wraparound handling */
924 	int *vblank_enabled;            /* so we don't call enable more than
925 					   once per disable */
926 	int *vblank_inmodeset;          /* Display driver is setting mode */
927 	u32 *last_vblank_wait;		/* Last vblank seqno waited per CRTC */
928 	struct callout vblank_disable_callout;
929 
930 	u32 max_vblank_count;           /**< size of vblank counter register */
931 
932 	struct list_head vblank_event_list;
933 	struct mtx	 event_lock;
934 
935         struct drm_mode_config mode_config;	/**< Current mode config */
936 
937 	/* GEM part */
938 	struct sx	  object_name_lock;
939 	struct drm_gem_names object_names;
940 	void		 *mm_private;
941 
942 	void *sysctl_private;
943 	char busid_str[128];
944 	int modesetting;
945 };
946 
947 static __inline__ int drm_core_check_feature(struct drm_device *dev,
948 					     int feature)
949 {
950 	return ((dev->driver->driver_features & feature) ? 1 : 0);
951 }
952 
953 #if __OS_HAS_AGP
954 static inline int drm_core_has_AGP(struct drm_device *dev)
955 {
956 	return drm_core_check_feature(dev, DRIVER_USE_AGP);
957 }
958 #else
959 #define drm_core_has_AGP(dev) (0)
960 #endif
961 
962 enum dmi_field {
963         DMI_NONE,
964         DMI_BIOS_VENDOR,
965         DMI_BIOS_VERSION,
966         DMI_BIOS_DATE,
967         DMI_SYS_VENDOR,
968         DMI_PRODUCT_NAME,
969         DMI_PRODUCT_VERSION,
970         DMI_PRODUCT_SERIAL,
971         DMI_PRODUCT_UUID,
972         DMI_BOARD_VENDOR,
973         DMI_BOARD_NAME,
974         DMI_BOARD_VERSION,
975         DMI_BOARD_SERIAL,
976         DMI_BOARD_ASSET_TAG,
977         DMI_CHASSIS_VENDOR,
978         DMI_CHASSIS_TYPE,
979         DMI_CHASSIS_VERSION,
980         DMI_CHASSIS_SERIAL,
981         DMI_CHASSIS_ASSET_TAG,
982         DMI_STRING_MAX,
983 };
984 
985 struct dmi_strmatch {
986 	unsigned char slot;
987 	char substr[79];
988 };
989 
990 struct dmi_system_id {
991         int (*callback)(const struct dmi_system_id *);
992         const char *ident;
993         struct dmi_strmatch matches[4];
994 };
995 #define	DMI_MATCH(a, b) {(a), (b)}
996 bool dmi_check_system(const struct dmi_system_id *);
997 
998 extern int	drm_debug_flag;
999 extern int	drm_notyet_flag;
1000 extern unsigned int drm_vblank_offdelay;
1001 extern unsigned int drm_timestamp_precision;
1002 
1003 /* Device setup support (drm_drv.c) */
1004 int	drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1005 int	drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1006 int	drm_create_cdevs(device_t kdev);
1007 void	drm_close(void *data);
1008 int	drm_detach(device_t kdev);
1009 d_ioctl_t drm_ioctl;
1010 d_open_t drm_open;
1011 d_read_t drm_read;
1012 d_poll_t drm_poll;
1013 d_mmap_t drm_mmap;
1014 extern drm_local_map_t	*drm_getsarea(struct drm_device *dev);
1015 
1016 void drm_event_wakeup(struct drm_pending_event *e);
1017 
1018 int drm_add_busid_modesetting(struct drm_device *dev,
1019     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1020 
1021 /* File operations helpers (drm_fops.c) */
1022 extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
1023 					 DRM_STRUCTPROC *p,
1024 					struct drm_device *dev);
1025 
1026 /* Memory management support (drm_memory.c) */
1027 void	drm_mem_init(void);
1028 void	drm_mem_uninit(void);
1029 void	*drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1030 void	*drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1031 void	drm_ioremapfree(drm_local_map_t *map);
1032 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
1033 int	drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1034 
1035 int	drm_context_switch(struct drm_device *dev, int old, int new);
1036 int	drm_context_switch_complete(struct drm_device *dev, int new);
1037 
1038 int	drm_ctxbitmap_init(struct drm_device *dev);
1039 void	drm_ctxbitmap_cleanup(struct drm_device *dev);
1040 void	drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1041 int	drm_ctxbitmap_next(struct drm_device *dev);
1042 
1043 /* Locking IOCTL support (drm_lock.c) */
1044 int	drm_lock_take(struct drm_lock_data *lock_data,
1045 		      unsigned int context);
1046 int	drm_lock_transfer(struct drm_lock_data *lock_data,
1047 			  unsigned int context);
1048 int	drm_lock_free(struct drm_lock_data *lock_data,
1049 		      unsigned int context);
1050 
1051 /* Buffer management support (drm_bufs.c) */
1052 unsigned long drm_get_resource_start(struct drm_device *dev,
1053 				     unsigned int resource);
1054 unsigned long drm_get_resource_len(struct drm_device *dev,
1055 				   unsigned int resource);
1056 void	drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1057 int	drm_order(unsigned long size);
1058 int	drm_addmap(struct drm_device *dev, unsigned long offset,
1059 		   unsigned long size,
1060 		   enum drm_map_type type, enum drm_map_flags flags,
1061 		   drm_local_map_t **map_ptr);
1062 int	drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
1063 int	drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request);
1064 int	drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
1065 
1066 /* DMA support (drm_dma.c) */
1067 int	drm_dma_setup(struct drm_device *dev);
1068 void	drm_dma_takedown(struct drm_device *dev);
1069 void	drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
1070 void	drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
1071 #define drm_core_reclaim_buffers drm_reclaim_buffers
1072 
1073 /* IRQ support (drm_irq.c) */
1074 int	drm_irq_install(struct drm_device *dev);
1075 int	drm_irq_uninstall(struct drm_device *dev);
1076 irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1077 void	drm_driver_irq_preinstall(struct drm_device *dev);
1078 void	drm_driver_irq_postinstall(struct drm_device *dev);
1079 void	drm_driver_irq_uninstall(struct drm_device *dev);
1080 
1081 void	drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1082 void	drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1083 int 	drm_modeset_ctl(struct drm_device *dev, void *data,
1084 			struct drm_file *file_priv);
1085 
1086 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1087 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1088 			   struct drm_file *filp);
1089 extern int drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
1090 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1091 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1092 				     struct timeval *vblanktime);
1093 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1094 void drm_handle_vblank_events(struct drm_device *dev, int crtc);
1095 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1096 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1097 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1098 extern void drm_vblank_cleanup(struct drm_device *dev);
1099 extern u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1100 				     struct timeval *tvblank, unsigned flags);
1101 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1102 						 int crtc, int *max_error,
1103 						 struct timeval *vblank_time,
1104 						 unsigned flags,
1105 						 struct drm_crtc *refcrtc);
1106 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc);
1107 
1108 struct timeval ns_to_timeval(const int64_t nsec);
1109 int64_t timeval_to_ns(const struct timeval *tv);
1110 
1111 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
1112 int	drm_device_is_agp(struct drm_device *dev);
1113 int	drm_device_is_pcie(struct drm_device *dev);
1114 drm_agp_head_t *drm_agp_init(void);
1115 int	drm_agp_acquire(struct drm_device *dev);
1116 int	drm_agp_release(struct drm_device *dev);
1117 int	drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
1118 int	drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1119 void	*drm_agp_allocate_memory(size_t pages, u32 type);
1120 int	drm_agp_free_memory(void *handle);
1121 int	drm_agp_bind_memory(void *handle, off_t start);
1122 int	drm_agp_unbind_memory(void *handle);
1123 int	drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1124 int	drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1125 int	drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1126 int	drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1127 
1128 /* Scatter Gather Support (drm_scatter.c) */
1129 void	drm_sg_cleanup(drm_sg_mem_t *entry);
1130 int	drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1131 
1132 /* sysctl support (drm_sysctl.h) */
1133 extern int		drm_sysctl_init(struct drm_device *dev);
1134 extern int		drm_sysctl_cleanup(struct drm_device *dev);
1135 
1136 /* ATI PCIGART support (ati_pcigart.c) */
1137 int	drm_ati_pcigart_init(struct drm_device *dev,
1138 				struct drm_ati_pcigart_info *gart_info);
1139 int	drm_ati_pcigart_cleanup(struct drm_device *dev,
1140 				struct drm_ati_pcigart_info *gart_info);
1141 
1142 /* Cache management (drm_memory.c) */
1143 void	drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1144 
1145 /* Locking IOCTL support (drm_drv.c) */
1146 int	drm_lock(struct drm_device *dev, void *data,
1147 		 struct drm_file *file_priv);
1148 int	drm_unlock(struct drm_device *dev, void *data,
1149 		   struct drm_file *file_priv);
1150 int	drm_version(struct drm_device *dev, void *data,
1151 		    struct drm_file *file_priv);
1152 int	drm_setversion(struct drm_device *dev, void *data,
1153 		       struct drm_file *file_priv);
1154 
1155 /* Misc. IOCTL support (drm_ioctl.c) */
1156 int	drm_irq_by_busid(struct drm_device *dev, void *data,
1157 			 struct drm_file *file_priv);
1158 int	drm_getunique(struct drm_device *dev, void *data,
1159 		      struct drm_file *file_priv);
1160 int	drm_setunique(struct drm_device *dev, void *data,
1161 		      struct drm_file *file_priv);
1162 int	drm_getmap(struct drm_device *dev, void *data,
1163 		   struct drm_file *file_priv);
1164 int	drm_getclient(struct drm_device *dev, void *data,
1165 		      struct drm_file *file_priv);
1166 int	drm_getstats(struct drm_device *dev, void *data,
1167 		     struct drm_file *file_priv);
1168 int	drm_getcap(struct drm_device *dev, void *data,
1169 		     struct drm_file *file_priv);
1170 int	drm_noop(struct drm_device *dev, void *data,
1171 		 struct drm_file *file_priv);
1172 
1173 /* Context IOCTL support (drm_context.c) */
1174 int	drm_resctx(struct drm_device *dev, void *data,
1175 		   struct drm_file *file_priv);
1176 int	drm_addctx(struct drm_device *dev, void *data,
1177 		   struct drm_file *file_priv);
1178 int	drm_modctx(struct drm_device *dev, void *data,
1179 		   struct drm_file *file_priv);
1180 int	drm_getctx(struct drm_device *dev, void *data,
1181 		   struct drm_file *file_priv);
1182 int	drm_switchctx(struct drm_device *dev, void *data,
1183 		      struct drm_file *file_priv);
1184 int	drm_newctx(struct drm_device *dev, void *data,
1185 		   struct drm_file *file_priv);
1186 int	drm_rmctx(struct drm_device *dev, void *data,
1187 		  struct drm_file *file_priv);
1188 int	drm_setsareactx(struct drm_device *dev, void *data,
1189 			struct drm_file *file_priv);
1190 int	drm_getsareactx(struct drm_device *dev, void *data,
1191 			struct drm_file *file_priv);
1192 
1193 /* Drawable IOCTL support (drm_drawable.c) */
1194 int	drm_adddraw(struct drm_device *dev, void *data,
1195 		    struct drm_file *file_priv);
1196 int	drm_rmdraw(struct drm_device *dev, void *data,
1197 		   struct drm_file *file_priv);
1198 int	drm_update_draw(struct drm_device *dev, void *data,
1199 			struct drm_file *file_priv);
1200 struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1201 						int handle);
1202 
1203 /* Drawable support (drm_drawable.c) */
1204 void drm_drawable_free_all(struct drm_device *dev);
1205 
1206 /* Authentication IOCTL support (drm_auth.c) */
1207 int	drm_getmagic(struct drm_device *dev, void *data,
1208 		     struct drm_file *file_priv);
1209 int	drm_authmagic(struct drm_device *dev, void *data,
1210 		      struct drm_file *file_priv);
1211 
1212 /* Buffer management support (drm_bufs.c) */
1213 int	drm_addmap_ioctl(struct drm_device *dev, void *data,
1214 			 struct drm_file *file_priv);
1215 int	drm_rmmap_ioctl(struct drm_device *dev, void *data,
1216 			struct drm_file *file_priv);
1217 int	drm_addbufs(struct drm_device *dev, void *data,
1218 		    struct drm_file *file_priv);
1219 int	drm_infobufs(struct drm_device *dev, void *data,
1220 		     struct drm_file *file_priv);
1221 int	drm_markbufs(struct drm_device *dev, void *data,
1222 		     struct drm_file *file_priv);
1223 int	drm_freebufs(struct drm_device *dev, void *data,
1224 		     struct drm_file *file_priv);
1225 int	drm_mapbufs(struct drm_device *dev, void *data,
1226 		    struct drm_file *file_priv);
1227 
1228 /* DMA support (drm_dma.c) */
1229 int	drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1230 
1231 /* IRQ support (drm_irq.c) */
1232 int	drm_control(struct drm_device *dev, void *data,
1233 		    struct drm_file *file_priv);
1234 
1235 /* AGP/GART support (drm_agpsupport.c) */
1236 int	drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1237 			      struct drm_file *file_priv);
1238 int	drm_agp_release_ioctl(struct drm_device *dev, void *data,
1239 			      struct drm_file *file_priv);
1240 int	drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1241 			     struct drm_file *file_priv);
1242 int	drm_agp_info_ioctl(struct drm_device *dev, void *data,
1243 			   struct drm_file *file_priv);
1244 int	drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1245 			    struct drm_file *file_priv);
1246 int	drm_agp_free_ioctl(struct drm_device *dev, void *data,
1247 			   struct drm_file *file_priv);
1248 int	drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1249 			     struct drm_file *file_priv);
1250 int	drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1251 			   struct drm_file *file_priv);
1252 
1253 				/* Stub support (drm_stub.h) */
1254 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1255 			       struct drm_file *file_priv);
1256 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1257 				struct drm_file *file_priv);
1258 
1259 /* Scatter Gather Support (drm_scatter.c) */
1260 int	drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1261 			   struct drm_file *file_priv);
1262 int	drm_sg_free(struct drm_device *dev, void *data,
1263 		    struct drm_file *file_priv);
1264 
1265 /* consistent PCI memory functions (drm_pci.c) */
1266 drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1267 				size_t align, dma_addr_t maxaddr);
1268 void	drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1269 
1270 /* Graphics Execution Manager library functions (drm_gem.c) */
1271 int drm_gem_init(struct drm_device *dev);
1272 void drm_gem_destroy(struct drm_device *dev);
1273 
1274 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1275 			struct drm_file *file_priv);
1276 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1277 			struct drm_file *file_priv);
1278 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1279 		       struct drm_file *file_priv);
1280 int drm_gem_handle_create(struct drm_file *file_priv,
1281 			  struct drm_gem_object *obj,
1282 			  u32 *handlep);
1283 int drm_gem_handle_delete(struct drm_file *file_priv, uint32_t handle);
1284 void drm_gem_object_handle_reference(struct drm_gem_object *obj);
1285 void drm_gem_object_handle_unreference(struct drm_gem_object *obj);
1286 void drm_gem_object_handle_unreference_unlocked(struct drm_gem_object *obj);
1287 void drm_gem_object_handle_free(struct drm_gem_object *obj);
1288 void drm_gem_object_reference(struct drm_gem_object *obj);
1289 void drm_gem_object_unreference(struct drm_gem_object *obj);
1290 void drm_gem_object_unreference_unlocked(struct drm_gem_object *obj);
1291 void drm_gem_object_release(struct drm_gem_object *obj);
1292 void drm_gem_object_free(struct drm_gem_object *obj);
1293 int drm_gem_object_init(struct drm_device *dev, struct drm_gem_object *obj,
1294     size_t size);
1295 int drm_gem_private_object_init(struct drm_device *dev,
1296     struct drm_gem_object *obj, size_t size);
1297 struct drm_gem_object *drm_gem_object_alloc(struct drm_device *dev,
1298     size_t size);
1299 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1300     struct drm_file *file_priv, uint32_t handle);
1301 
1302 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1303 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1304 
1305 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1306 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1307 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1308     vm_size_t size, struct vm_object **obj_res, int nprot);
1309 void drm_gem_pager_dtr(void *obj);
1310 
1311 struct ttm_bo_device;
1312 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1313     vm_size_t size, struct vm_object **obj_res, int nprot);
1314 
1315 void drm_device_lock_mtx(struct drm_device *dev);
1316 void drm_device_unlock_mtx(struct drm_device *dev);
1317 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1318     const char *msg, int timeout);
1319 void drm_device_assert_mtx_locked(struct drm_device *dev);
1320 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1321 
1322 void drm_device_lock_struct(struct drm_device *dev);
1323 void drm_device_unlock_struct(struct drm_device *dev);
1324 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1325     const char *msg, int timeout);
1326 void drm_device_assert_struct_locked(struct drm_device *dev);
1327 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1328 
1329 void drm_compat_locking_init(struct drm_device *dev);
1330 void drm_sleep_locking_init(struct drm_device *dev);
1331 
1332 /* drm_modes.c */
1333 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1334     struct drm_connector *connector, struct drm_cmdline_mode *mode);
1335 struct drm_display_mode *drm_mode_create_from_cmdline_mode(
1336     struct drm_device *dev, struct drm_cmdline_mode *cmd);
1337 
1338 /* drm_edid.c */
1339 u8 *drm_find_cea_extension(struct edid *edid);
1340 
1341 /* Inline replacements for drm_alloc and friends */
1342 static __inline__ void *
1343 drm_alloc(size_t size, struct malloc_type *area)
1344 {
1345 	return malloc(size, area, M_NOWAIT);
1346 }
1347 
1348 static __inline__ void *
1349 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1350 {
1351 	return malloc(size * nmemb, area, M_NOWAIT | M_ZERO);
1352 }
1353 
1354 static __inline__ void *
1355 drm_realloc(void *oldpt, size_t oldsize, size_t size,
1356     struct malloc_type *area)
1357 {
1358 	return reallocf(oldpt, size, area, M_NOWAIT);
1359 }
1360 
1361 static __inline__ void
1362 drm_free(void *pt, size_t size, struct malloc_type *area)
1363 {
1364 	free(pt, area);
1365 }
1366 
1367 /* Inline replacements for DRM_IOREMAP macros */
1368 static __inline__ void
1369 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1370 {
1371 	map->virtual = drm_ioremap_wc(dev, map);
1372 }
1373 static __inline__ void
1374 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1375 {
1376 	map->virtual = drm_ioremap(dev, map);
1377 }
1378 static __inline__ void
1379 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1380 {
1381 	if ( map->virtual && map->size )
1382 		drm_ioremapfree(map);
1383 }
1384 
1385 static __inline__ struct drm_local_map *
1386 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1387 {
1388 	drm_local_map_t *map;
1389 
1390 	DRM_LOCK_ASSERT(dev);
1391 	TAILQ_FOREACH(map, &dev->maplist, link) {
1392 		if (offset == (unsigned long)map->handle)
1393 			return map;
1394 	}
1395 	return NULL;
1396 }
1397 
1398 static __inline__ void drm_core_dropmap(struct drm_map *map)
1399 {
1400 }
1401 
1402 #define KIB_NOTYET()							\
1403 do {									\
1404 	if (drm_debug_flag && drm_notyet_flag)				\
1405 		printf("NOTYET: %s at %s:%d\n", __func__, __FILE__, __LINE__); \
1406 } while (0)
1407 
1408 #define	KTR_DRM		KTR_DEV
1409 #define	KTR_DRM_REG	KTR_SPARE3
1410 
1411 #endif /* __KERNEL__ */
1412 #endif /* _DRM_P_H_ */
1413