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