xref: /dragonfly/sys/dev/drm/include/drm/drmP.h (revision 896f2e3a)
1 /**
2  * \file drmP.h
3  * Private header for Direct Rendering Manager
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8 
9 /*
10  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12  * Copyright (c) 2009-2010, Code Aurora Forum.
13  * All rights reserved.
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the next
23  * paragraph) shall be included in all copies or substantial portions of the
24  * Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
29  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32  * OTHER DEALINGS IN THE SOFTWARE.
33  */
34 
35 #ifndef _DRM_P_H_
36 #define _DRM_P_H_
37 
38 #if defined(_KERNEL) || defined(__KERNEL__)
39 
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/ktr.h>
45 #include <sys/module.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/sglist.h>
49 #include <sys/stat.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/lock.h>
53 #include <sys/spinlock.h>
54 #include <sys/spinlock2.h>
55 #include <sys/fcntl.h>
56 #include <sys/uio.h>
57 #include <sys/filio.h>
58 #include <sys/sysctl.h>
59 #include <sys/bus.h>
60 #include <sys/queue.h>
61 #include <sys/signalvar.h>
62 #include <sys/poll.h>
63 #include <sys/sbuf.h>
64 #include <sys/taskqueue.h>
65 #include <sys/tree.h>
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page2.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vm_param.h>
75 #include <machine/param.h>
76 #include <machine/pmap.h>
77 #if defined(__i386__) || defined(__amd64__)
78 #include <machine/specialreg.h>
79 #endif
80 #include <machine/sysarch.h>
81 #include <sys/endian.h>
82 #include <sys/mman.h>
83 #include <sys/rman.h>
84 #include <sys/memrange.h>
85 #include <dev/agp/agpvar.h>
86 #include <sys/agpio.h>
87 #include <sys/mutex.h>
88 
89 MALLOC_DECLARE(M_DRM);
90 
91 #include <uapi_drm/drm.h>
92 #include <uapi_drm/drm_sarea.h>
93 
94 #include <linux/atomic.h>
95 #include <linux/bug.h>
96 #include <linux/err.h>
97 #include <linux/idr.h>
98 #include <linux/pci.h>
99 #include <linux/jiffies.h>
100 #include <linux/kernel.h>
101 #include <linux/kref.h>
102 #include <linux/list.h>
103 #include <linux/mm.h>
104 #include <linux/moduleparam.h>
105 #include <linux/mutex.h>
106 #include <linux/slab.h>
107 #include <linux/scatterlist.h>
108 #include <linux/timer.h>
109 #include <asm/io.h>
110 #include <linux/types.h>
111 #include <linux/wait.h>
112 #include <linux/workqueue.h>
113 
114 #include <asm/uaccess.h>
115 
116 #include <drm/drm_vma_manager.h>
117 
118 struct drm_file;
119 struct drm_device;
120 
121 #include <drm/drm_os_linux.h>
122 #include <drm/drm_hashtab.h>
123 #include <drm/drm_mm.h>
124 
125 #define DRM_UT_CORE 		0x01
126 #define DRM_UT_DRIVER		0x02
127 #define DRM_UT_KMS		0x04
128 #define DRM_UT_PRIME		0x08
129 
130 #include "opt_drm.h"
131 #ifdef DRM_DEBUG
132 #  if DRM_DEBUG>1
133 #    define DRM_DEBUG_DEFAULT_ON 2
134 #  else
135 #    define DRM_DEBUG_DEFAULT_ON 1
136 #  endif
137 #undef DRM_DEBUG
138 #endif /* DRM_DEBUG */
139 
140 #define	DRM_DEBUGBITS_DEBUG		0x1
141 #define	DRM_DEBUGBITS_KMS		0x2
142 #define	DRM_DEBUGBITS_FAILED_IOCTL	0x4
143 #define	DRM_DEBUGBITS_VERBOSE		0x8
144 
145 #undef DRM_LINUX
146 #define DRM_LINUX 0
147 
148 /***********************************************************************/
149 /** \name DRM template customization defaults */
150 /*@{*/
151 
152 /* driver capabilities and requirements mask */
153 #define DRIVER_USE_AGP     0x1
154 #define DRIVER_REQUIRE_AGP 0x2
155 #define DRIVER_PCI_DMA     0x8
156 #define DRIVER_SG          0x10
157 #define DRIVER_HAVE_DMA    0x20
158 #define DRIVER_HAVE_IRQ    0x40
159 #define DRIVER_IRQ_SHARED  0x80
160 #define DRIVER_IRQ_VBL     0x100
161 #define DRIVER_DMA_QUEUE   0x200
162 #define DRIVER_FB_DMA      0x400
163 #define DRIVER_IRQ_VBL2    0x800
164 #define DRIVER_GEM         0x1000
165 #define DRIVER_MODESET     0x2000
166 #define DRIVER_PRIME       0x4000
167 
168 /***********************************************************************/
169 /** \name Begin the DRM... */
170 /*@{*/
171 
172 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
173 #define DRM_KERNEL_CONTEXT    0	 /* Change drm_resctx if changed	  */
174 #define DRM_RESERVED_CONTEXTS 1	 /* Change drm_resctx if changed	  */
175 
176 #define	DRM_GEM_MAPPING_MASK	(3ULL << 62)
177 #define	DRM_GEM_MAPPING_KEY	(2ULL << 62) /* Non-canonical address form */
178 #define	DRM_GEM_MAX_IDX		0x3fffff
179 #define	DRM_GEM_MAPPING_IDX(o)	(((o) >> 40) & DRM_GEM_MAX_IDX)
180 #define	DRM_GEM_MAPPING_OFF(i)	(((uint64_t)(i)) << 40)
181 #define	DRM_GEM_MAPPING_MAPOFF(o) \
182     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
183 
184 SYSCTL_DECL(_hw_drm);
185 
186 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
187 
188 /***********************************************************************/
189 /** \name Internal types and structures */
190 /*@{*/
191 
192 #define DRM_ARRAY_SIZE(x) NELEM(x)
193 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
194 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
195 
196 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
197 
198 #define __OS_HAS_AGP	1
199 
200 #define DRM_DEV_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
201 #define DRM_DEV_UID	0
202 #define DRM_DEV_GID	0
203 
204 #define DRM_WAKEUP(w)		wakeup((void *)w)
205 #define DRM_WAKEUP_INT(w)	wakeup(w)
206 #define DRM_INIT_WAITQUEUE(queue) do {(void)(queue);} while (0)
207 
208 #define DRM_CURPROC		curthread
209 #define DRM_STRUCTPROC		struct thread
210 #define DRM_CURRENTPID		(curproc != NULL ? curproc->p_pid : -1)
211 #define DRM_LOCK(dev)		lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
212 #define DRM_UNLOCK(dev)		lockmgr(&(dev)->struct_mutex, LK_RELEASE)
213 #define	DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)			\
214     (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
215 #if defined(INVARIANTS)
216 #define	DRM_LOCK_ASSERT(dev)	KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
217 #define	DRM_UNLOCK_ASSERT(dev)	KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
218 #else
219 #define	DRM_LOCK_ASSERT(d)
220 #define	DRM_UNLOCK_ASSERT(d)
221 #endif
222 
223 #define DRM_SYSCTL_HANDLER_ARGS	(SYSCTL_HANDLER_ARGS)
224 
225 #define DRM_IRQ_ARGS		void *arg
226 typedef void			irqreturn_t;
227 #define IRQ_HANDLED		/* nothing */
228 #define IRQ_NONE		/* nothing */
229 
230 enum {
231 	DRM_IS_NOT_AGP,
232 	DRM_IS_AGP,
233 	DRM_MIGHT_BE_AGP
234 };
235 #define DRM_AGP_MEM		struct agp_memory_info
236 
237 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
238 
239 /* DRM_SUSER returns true if the user is superuser */
240 #define DRM_SUSER(p)		(priv_check(p, PRIV_DRIVER) == 0)
241 #define DRM_AGP_FIND_DEVICE()	agp_find_device()
242 #define DRM_MTRR_WC		MDF_WRITECOMBINE
243 
244 /* DRM_READMEMORYBARRIER() prevents reordering of reads.
245  * DRM_WRITEMEMORYBARRIER() prevents reordering of writes.
246  * DRM_MEMORYBARRIER() prevents reordering of reads and writes.
247  */
248 #define DRM_READMEMORYBARRIER()		cpu_lfence()
249 #define DRM_WRITEMEMORYBARRIER()	cpu_sfence()
250 #define DRM_MEMORYBARRIER()		cpu_mfence()
251 
252 #define DRM_VERIFYAREA_READ( uaddr, size )		\
253 	(!useracc(__DECONST(caddr_t, uaddr), size, VM_PROT_READ))
254 
255 #define DRM_COPY_TO_USER(user, kern, size) \
256 	copyout(kern, user, size)
257 #define DRM_COPY_FROM_USER(kern, user, size) \
258 	copyin(user, kern, size)
259 #define DRM_COPY_FROM_USER_UNCHECKED(arg1, arg2, arg3) 	\
260 	copyin(arg2, arg1, arg3)
261 #define DRM_COPY_TO_USER_UNCHECKED(arg1, arg2, arg3)	\
262 	copyout(arg2, arg1, arg3)
263 #define DRM_GET_USER_UNCHECKED(val, uaddr)		\
264 	((val) = fuword32(uaddr), 0)
265 
266 #define	drm_can_sleep()	(DRM_HZ & 1)
267 
268 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {	\
269 	(_map) = (_dev)->context_sareas[_ctx];		\
270 } while(0)
271 
272 #define LOCK_TEST_WITH_RETURN(dev, file_priv)				\
273 do {									\
274 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
275 	     dev->lock.file_priv != file_priv) {			\
276 		DRM_ERROR("%s called without lock held\n",		\
277 			   __FUNCTION__);				\
278 		return EINVAL;						\
279 	}								\
280 } while (0)
281 
282 /* Returns -errno to shared code */
283 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
284 for ( ret = 0 ; !ret && !(condition) ; ) {			\
285 	DRM_UNLOCK(dev);					\
286 	lwkt_serialize_enter(&dev->irq_lock);			\
287 	if (!(condition)) {					\
288             tsleep_interlock(&(queue), PCATCH);			\
289             lwkt_serialize_exit(&dev->irq_lock);		\
290             ret = -tsleep(&(queue), PCATCH | PINTERLOCKED,	\
291 			  "drmwtq", (timeout));			\
292         } else {                                                \
293                 lwkt_serialize_exit(&dev->irq_lock);            \
294         }                                                 	\
295 	DRM_LOCK(dev);						\
296 }
297 
298 __inline static void
299 vm_page_unhold_pages(vm_page_t *ma, int count)
300 {
301 	int i;
302 
303 	for (i = 0; i < count; i++)
304 		vm_page_unhold(ma[i]);
305 }
306 
307 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
308     vm_memattr_t memattr);
309 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
310 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
311 
312 /***********************************************************************/
313 /** \name Macros to make printk easier */
314 /*@{*/
315 
316 /**
317  * Error output.
318  *
319  * \param fmt printf() like format string.
320  * \param arg arguments
321  */
322 #define DRM_ERROR(fmt, ...) \
323 	kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
324 	    DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
325 
326 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
327 
328 #define DRM_INFO_ONCE(fmt, ...)				\
329 	printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
330 
331 #define DRM_DEBUG(fmt, ...) do {					\
332 	if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0)		\
333 		kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
334 			__func__ , ##__VA_ARGS__);			\
335 } while (0)
336 
337 #define DRM_DEBUG_VERBOSE(fmt, ...) do {				\
338 	if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0)		\
339 		kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
340 			__func__ , ##__VA_ARGS__);			\
341 } while (0)
342 
343 #define DRM_DEBUG_KMS(fmt, ...) do {					\
344 	if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)			\
345 		kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
346 			__func__ , ##__VA_ARGS__);			\
347 } while (0)
348 
349 #define DRM_DEBUG_DRIVER(fmt, ...) do {					\
350 	if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)			\
351 		kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
352 			__func__ , ##__VA_ARGS__);			\
353 } while (0)
354 
355 #define DRM_LOG_KMS(fmt, ...) do {					\
356 	kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,	\
357 			__func__ , ##__VA_ARGS__);			\
358 } while (0)
359 
360 #define	dev_dbg(dev, fmt, ...) do {					\
361 	if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {			\
362 		device_printf((dev), "debug: " fmt, ## __VA_ARGS__);	\
363 	}								\
364 } while (0)
365 
366 typedef struct drm_pci_id_list
367 {
368 	int vendor;
369 	int device;
370 	long driver_private;
371 	char *name;
372 } drm_pci_id_list_t;
373 
374 struct drm_msi_blacklist_entry
375 {
376 	int vendor;
377 	int device;
378 };
379 
380 /**
381  * Ioctl function type.
382  *
383  * \param inode device inode.
384  * \param file_priv DRM file private pointer.
385  * \param cmd command.
386  * \param arg argument.
387  */
388 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
389 			struct drm_file *file_priv);
390 
391 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
392 			       unsigned long arg);
393 
394 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
395 
396 #define DRM_AUTH	0x1
397 #define DRM_MASTER	0x2
398 #define DRM_ROOT_ONLY	0x4
399 #define DRM_CONTROL_ALLOW 0x8
400 #define DRM_UNLOCKED	0x10
401 
402 struct drm_ioctl_desc {
403 	unsigned long cmd;
404 	int (*func)(struct drm_device *dev, void *data,
405 		    struct drm_file *file_priv);
406 	int flags;
407 	unsigned int cmd_drv;
408 };
409 
410 /**
411  * Creates a driver or general drm_ioctl_desc array entry for the given
412  * ioctl, for use by drm_ioctl().
413  */
414 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)			\
415 	[DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
416 
417 typedef struct drm_magic_entry {
418 	struct list_head head;
419 	struct drm_hash_item hash_item;
420 	struct drm_file	       *priv;
421 	struct drm_magic_entry *next;
422 } drm_magic_entry_t;
423 
424 typedef struct drm_magic_head {
425 	struct drm_magic_entry *head;
426 	struct drm_magic_entry *tail;
427 } drm_magic_head_t;
428 
429 typedef struct drm_buf {
430 	int idx;		       /**< Index into master buflist */
431 	int total;		       /**< Buffer size */
432 	int order;		       /**< log-base-2(total) */
433 	int used;		       /**< Amount of buffer in use (for DMA) */
434 	unsigned long offset;	       /**< Byte offset (used internally) */
435 	void *address;		       /**< Address of buffer */
436 	unsigned long bus_address;     /**< Bus address of buffer */
437 	struct drm_buf *next;	       /**< Kernel-only: used for free list */
438 	__volatile__ int waiting;      /**< On kernel DMA queue */
439 	__volatile__ int pending;      /**< On hardware DMA queue */
440 	struct drm_file *file_priv;    /**< Private of holding file descr */
441 	int context;		       /**< Kernel queue for this buffer */
442 	int while_locked;	       /**< Dispatch this buffer while locked */
443 	enum {
444 		DRM_LIST_NONE = 0,
445 		DRM_LIST_FREE = 1,
446 		DRM_LIST_WAIT = 2,
447 		DRM_LIST_PEND = 3,
448 		DRM_LIST_PRIO = 4,
449 		DRM_LIST_RECLAIM = 5
450 	} list;			       /**< Which list we're on */
451 
452 	int dev_priv_size;		 /**< Size of buffer private storage */
453 	void *dev_private;		 /**< Per-buffer private storage */
454 } drm_buf_t;
455 
456 struct drm_freelist {
457 	int		  initialized; /* Freelist in use		   */
458 	atomic_t	  count;       /* Number of free buffers	   */
459 	drm_buf_t	  *next;       /* End pointer			   */
460 
461 	wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
462 	int		  low_mark;    /* Low water mark		   */
463 	int		  high_mark;   /* High water mark		   */
464 };
465 
466 typedef struct drm_dma_handle {
467 	void *vaddr;
468 	size_t size;
469 	bus_addr_t busaddr;
470 	bus_dma_tag_t tag;
471 	bus_dmamap_t map;
472 } drm_dma_handle_t;
473 
474 typedef struct drm_buf_entry {
475 	int		  buf_size;
476 	int		  buf_count;
477 	drm_buf_t	  *buflist;
478 	int		  seg_count;
479 	drm_dma_handle_t  **seglist;
480 	int		  page_order;
481 
482 	struct drm_freelist freelist;
483 } drm_buf_entry_t;
484 
485 /* Event queued up for userspace to read */
486 struct drm_pending_event {
487 	struct drm_event *event;
488 	struct list_head link;
489 	struct drm_file *file_priv;
490 	pid_t pid; /* pid of requester, no guarantee it's valid by the time
491 		      we deliver the event, for tracing only */
492 	void (*destroy)(struct drm_pending_event *event);
493 };
494 
495 /* initial implementaton using a linked list - todo hashtab */
496 struct drm_prime_file_private {
497 	struct list_head head;
498 #ifdef DUMBBELL_WIP
499 	struct mutex lock;
500 #endif /* DUMBBELL_WIP */
501 };
502 
503 /** File private data */
504 struct drm_file {
505 	int authenticated;
506 	struct drm_device *dev;
507 	int		  master;
508 	pid_t		  pid;
509 	uid_t		  uid;
510 	drm_magic_t	  magic;
511 	unsigned long	  ioctl_count;
512 	struct list_head lhead;
513 	struct kqinfo	  dkq;
514 
515 	/** Mapping of mm object handles to object pointers. */
516 	struct idr object_idr;
517 	/** Lock for synchronization of access to object_idr. */
518 	struct lock table_lock;
519 
520 	void		 *driver_priv;
521 
522 	int		  is_master;
523 	struct drm_master *masterp;
524 
525 	/* true when the client has asked us to expose stereo 3D mode flags */
526 	bool stereo_allowed;
527 
528 	/**
529 	 * fbs - List of framebuffers associated with this file.
530 	 *
531 	 * Protected by fbs_lock. Note that the fbs list holds a reference on
532 	 * the fb object to prevent it from untimely disappearing.
533 	 */
534 	struct list_head fbs;
535 	struct lock fbs_lock;
536 
537 	wait_queue_head_t event_wait;
538 	struct list_head  event_list;
539 	int		  event_space;
540 
541 	struct drm_prime_file_private prime;
542 };
543 
544 /**
545  * Lock data.
546  */
547 struct drm_lock_data {
548 	struct drm_hw_lock *hw_lock;	/**< Hardware lock */
549 	/** Private of lock holder's file (NULL=kernel) */
550 	struct drm_file *file_priv;
551 	wait_queue_head_t lock_queue;	/**< Queue of blocked processes */
552 	unsigned long lock_time;	/**< Time of last lock in jiffies */
553 };
554 
555 /* This structure, in the struct drm_device, is always initialized while the
556  * device
557  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
558  * when set marks that no further bufs may be allocated until device teardown
559  * occurs (when the last open of the device has closed).  The high/low
560  * watermarks of bufs are only touched by the X Server, and thus not
561  * concurrently accessed, so no locking is needed.
562  */
563 typedef struct drm_device_dma {
564 
565 	struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];	/**< buffers, grouped by their size order */
566 	int buf_count;			/**< total number of buffers */
567 	struct drm_buf **buflist;		/**< Vector of pointers into drm_device_dma::bufs */
568 	int seg_count;
569 	int page_count;			/**< number of pages */
570 	unsigned long *pagelist;	/**< page list */
571 	unsigned long byte_count;
572 	enum {
573 		_DRM_DMA_USE_AGP = 0x01,
574 		_DRM_DMA_USE_SG = 0x02,
575 		_DRM_DMA_USE_FB = 0x04,
576 		_DRM_DMA_USE_PCI_RO = 0x08
577 	} flags;
578 
579 } drm_device_dma_t;
580 
581 typedef struct drm_agp_mem {
582 	void               *handle;
583 	unsigned long      bound; /* address */
584 	int                pages;
585 	struct drm_agp_mem *prev;
586 	struct drm_agp_mem *next;
587 } drm_agp_mem_t;
588 
589 typedef struct drm_agp_head {
590 	device_t	   agpdev;
591 	struct agp_info    agp_info;
592 	const char         *chipset;
593 	drm_agp_mem_t      *memory;
594 	unsigned long      mode;
595 	int                enabled;
596 	int                acquired;
597 	unsigned long      base;
598 	int		   agp_mtrr;
599 	int		   cant_use_aperture;
600 	unsigned long	   page_mask;
601 } drm_agp_head_t;
602 
603 typedef struct drm_sg_mem {
604 	vm_offset_t vaddr;
605 	vm_paddr_t *busaddr;
606 	vm_pindex_t pages;
607 } drm_sg_mem_t;
608 
609 /**
610  * Kernel side of a mapping
611  */
612 struct drm_local_map {
613 	resource_size_t offset;	 /**< Requested physical address (0 for SAREA)*/
614 	unsigned long size;	 /**< Requested physical size (bytes) */
615 	enum drm_map_type type;	 /**< Type of memory to map */
616 	enum drm_map_flags flags;	 /**< Flags */
617 	void *handle;		 /**< User-space: "Handle" to pass to mmap() */
618 				 /**< Kernel-space: kernel-virtual address */
619 	int mtrr;		 /**< MTRR slot used */
620 };
621 
622 typedef struct drm_local_map drm_local_map_t;
623 
624 /**
625  * Mappings list
626  */
627 struct drm_map_list {
628 	struct list_head head;		/**< list head */
629 	struct drm_hash_item hash;
630 	struct drm_local_map *map;	/**< mapping */
631 	uint64_t user_token;
632 	struct drm_master *master;
633 	struct drm_mm_node *file_offset_node;	/**< fake offset */
634 };
635 
636 /**
637  * Context handle list
638  */
639 struct drm_ctx_list {
640 	struct list_head head;		/**< list head */
641 	drm_context_t handle;		/**< context handle */
642 	struct drm_file *tag;		/**< associated fd private data */
643 };
644 
645 struct drm_vblank_info {
646 	wait_queue_head_t queue;	/* vblank wait queue */
647 	atomic_t count;			/* number of VBLANK interrupts */
648 					/* (driver must alloc the right number of counters) */
649 	atomic_t refcount;		/* number of users of vblank interrupts */
650 	u32 last;			/* protected by dev->vbl_lock, used */
651 					/* for wraparound handling */
652 	int enabled;			/* so we don't call enable more than */
653 					/* once per disable */
654 	int inmodeset;			/* Display driver is setting mode */
655 };
656 
657 /* location of GART table */
658 #define DRM_ATI_GART_MAIN 1
659 #define DRM_ATI_GART_FB   2
660 
661 #define DRM_ATI_GART_PCI 1
662 #define DRM_ATI_GART_PCIE 2
663 #define DRM_ATI_GART_IGP 3
664 
665 struct drm_ati_pcigart_info {
666 	int gart_table_location;
667 	int gart_reg_if;
668 	void *addr;
669 	dma_addr_t bus_addr;
670 	dma_addr_t table_mask;
671 	dma_addr_t member_mask;
672 	struct drm_dma_handle *table_handle;
673 	drm_local_map_t mapping;
674 	int table_size;
675 	struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
676 };
677 
678 /**
679  * GEM specific mm private for tracking GEM objects
680  */
681 struct drm_gem_mm {
682 	struct drm_vma_offset_manager vma_manager;
683 	struct drm_mm offset_manager;	/**< Offset mgmt for buffer objects */
684 	struct drm_open_hash offset_hash; /**< User token hash table for maps */
685 	struct unrhdr *idxunr;
686 };
687 
688 struct drm_gem_object {
689 	/** Reference count of this object */
690 	struct kref refcount;
691 
692 	/** Handle count of this object. Each handle also holds a reference */
693 	atomic_t handle_count; /* number of handles on this object */
694 
695 	/** Related drm device */
696 	struct drm_device *dev;
697 
698 	/** File representing the shmem storage: filp in Linux parlance */
699 	vm_object_t vm_obj;
700 
701 	/* Mapping info for this object */
702 	struct drm_vma_offset_node vma_node;
703 	bool on_map;
704 	struct drm_hash_item map_list;
705 
706 	/**
707 	 * Size of the object, in bytes.  Immutable over the object's
708 	 * lifetime.
709 	 */
710 	size_t size;
711 
712 	/**
713 	 * Global name for this object, starts at 1. 0 means unnamed.
714 	 * Access is covered by the object_name_lock in the related drm_device
715 	 */
716 	int name;
717 
718 	/**
719 	 * Memory domains. These monitor which caches contain read/write data
720 	 * related to the object. When transitioning from one set of domains
721 	 * to another, the driver is called to ensure that caches are suitably
722 	 * flushed and invalidated
723 	 */
724 	uint32_t read_domains;
725 	uint32_t write_domain;
726 
727 	/**
728 	 * While validating an exec operation, the
729 	 * new read/write domain values are computed here.
730 	 * They will be transferred to the above values
731 	 * at the point that any cache flushing occurs
732 	 */
733 	uint32_t pending_read_domains;
734 	uint32_t pending_write_domain;
735 
736 #ifdef DUMBBELL_WIP
737 	/* dma buf exported from this GEM object */
738 	struct dma_buf *export_dma_buf;
739 
740 	/* dma buf attachment backing this object */
741 	struct dma_buf_attachment *import_attach;
742 #endif /* DUMBBELL_WIP */
743 };
744 
745 #include "drm_crtc.h"
746 
747 /* per-master structure */
748 struct drm_master {
749 
750 	struct kref refcount; /* refcount for this master */
751 
752 	struct list_head head; /**< each minor contains a list of masters */
753 	struct drm_minor *minor; /**< link back to minor we are a master for */
754 
755 	char *unique;			/**< Unique identifier: e.g., busid */
756 	int unique_len;			/**< Length of unique field */
757 	int unique_size;		/**< amount allocated */
758 
759 	int blocked;			/**< Blocked due to VC switch? */
760 
761 	/** \name Authentication */
762 	/*@{ */
763 	struct drm_open_hash magiclist;
764 	struct list_head magicfree;
765 	/*@} */
766 
767 	struct drm_lock_data lock;	/**< Information on hardware lock */
768 
769 	void *driver_priv; /**< Private structure for driver to use */
770 };
771 
772 /* Size of ringbuffer for vblank timestamps. Just double-buffer
773  * in initial implementation.
774  */
775 #define DRM_VBLANKTIME_RBSIZE 2
776 
777 /* Flags and return codes for get_vblank_timestamp() driver function. */
778 #define DRM_CALLED_FROM_VBLIRQ 1
779 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
780 #define DRM_VBLANKTIME_INVBL             (1 << 1)
781 
782 /* get_scanout_position() return flags */
783 #define DRM_SCANOUTPOS_VALID        (1 << 0)
784 #define DRM_SCANOUTPOS_INVBL        (1 << 1)
785 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
786 
787 #ifndef DMA_BIT_MASK
788 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
789 #endif
790 
791 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
792 
793 struct drm_driver {
794 	int	(*load)(struct drm_device *, unsigned long flags);
795 	int	(*use_msi)(struct drm_device *, unsigned long flags);
796 	int	(*firstopen)(struct drm_device *);
797 	int	(*open)(struct drm_device *, struct drm_file *);
798 	void	(*preclose)(struct drm_device *, struct drm_file *file_priv);
799 	void	(*postclose)(struct drm_device *, struct drm_file *);
800 	void	(*lastclose)(struct drm_device *);
801 	int	(*unload)(struct drm_device *);
802 	void	(*reclaim_buffers_locked)(struct drm_device *,
803 					  struct drm_file *file_priv);
804 	int	(*dma_ioctl)(struct drm_device *dev, void *data,
805 			     struct drm_file *file_priv);
806 	void	(*dma_ready)(struct drm_device *);
807 	int	(*dma_quiescent)(struct drm_device *);
808 	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
809 					     enum drm_lock_flags flags);
810 	int	(*dma_flush_unblock)(struct drm_device *, int context,
811 				     enum drm_lock_flags flags);
812 	int	(*context_ctor)(struct drm_device *dev, int context);
813 	int	(*context_dtor)(struct drm_device *dev, int context);
814 	int	(*kernel_context_switch)(struct drm_device *dev, int old,
815 					 int new);
816 	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
817 	void	(*irq_preinstall)(struct drm_device *dev);
818 	int	(*irq_postinstall)(struct drm_device *dev);
819 	void	(*irq_uninstall)(struct drm_device *dev);
820 	void	(*irq_handler)(DRM_IRQ_ARGS);
821 
822 	u32	(*get_vblank_counter)(struct drm_device *dev, int crtc);
823 	int	(*enable_vblank)(struct drm_device *dev, int crtc);
824 	void	(*disable_vblank)(struct drm_device *dev, int crtc);
825 	int	(*get_scanout_position)(struct drm_device *dev, int crtc,
826 		    int *vpos, int *hpos);
827 
828 	int	(*get_vblank_timestamp)(struct drm_device *dev, int crtc,
829 		    int *max_error, struct timeval *vblank_time,
830 		    unsigned flags);
831 
832 	void	(*gem_free_object)(struct drm_gem_object *obj);
833 	int	(*gem_open_object)(struct drm_gem_object *, struct drm_file *);
834 	void	(*gem_close_object)(struct drm_gem_object *, struct drm_file *);
835 
836 	struct cdev_pager_ops *gem_pager_ops;
837 
838 	int	(*dumb_create)(struct drm_file *file_priv,
839 		    struct drm_device *dev, struct drm_mode_create_dumb *args);
840 	int	(*dumb_map_offset)(struct drm_file *file_priv,
841 		    struct drm_device *dev, uint32_t handle, uint64_t *offset);
842 	int	(*dumb_destroy)(struct drm_file *file_priv,
843 		    struct drm_device *dev, uint32_t handle);
844 
845 	int	(*sysctl_init)(struct drm_device *dev,
846 		    struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
847 	void	(*sysctl_cleanup)(struct drm_device *dev);
848 
849 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
850 
851 	/**
852 	 * Called by \c drm_device_is_agp.  Typically used to determine if a
853 	 * card is really attached to AGP or not.
854 	 *
855 	 * \param dev  DRM device handle
856 	 *
857 	 * \returns
858 	 * One of three values is returned depending on whether or not the
859 	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
860 	 * (return of 1), or may or may not be AGP (return of 2).
861 	 */
862 	int	(*device_is_agp) (struct drm_device * dev);
863 
864 	int	buf_priv_size;
865 
866 	int	major;
867 	int	minor;
868 	int	patchlevel;
869 	const char *name;		/* Simple driver name		   */
870 	const char *desc;		/* Longer driver name		   */
871 	const char *date;		/* Date of last major changes.	   */
872 
873 	u32 driver_features;
874 	int dev_priv_size;
875 	struct drm_ioctl_desc *ioctls;
876 	int num_ioctls;
877 };
878 
879 /**
880  * DRM minor structure. This structure represents a drm minor number.
881  */
882 struct drm_minor {
883 	int index;			/**< Minor device number */
884 	int type;                       /**< Control or render */
885 	device_t kdev;			/**< OS device */
886 	struct drm_device *dev;
887 
888 	struct drm_master *master; /* currently active master for this node */
889 	struct list_head master_list;
890 	struct drm_mode_group mode_group;
891 };
892 
893 /* mode specified on the command line */
894 struct drm_cmdline_mode {
895 	bool specified;
896 	bool refresh_specified;
897 	bool bpp_specified;
898 	int xres, yres;
899 	int bpp;
900 	int refresh;
901 	bool rb;
902 	bool interlace;
903 	bool cvt;
904 	bool margins;
905 	enum drm_connector_force force;
906 };
907 
908 
909 struct drm_pending_vblank_event {
910 	struct drm_pending_event base;
911 	int pipe;
912 	struct drm_event_vblank event;
913 };
914 
915 struct drm_sysctl_info {
916 	struct sysctl_ctx_list ctx;
917 	char   name[2];
918 };
919 
920 /* Length for the array of resource pointers for drm_get_resource_*. */
921 #define DRM_MAX_PCI_RESOURCE	6
922 
923 /**
924  * DRM device structure. This structure represent a complete card that
925  * may contain multiple heads.
926  */
927 struct drm_device {
928 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
929 
930 	uint16_t pci_subdevice;		/* PCI subsystem device id */
931 	uint16_t pci_subvendor;		/* PCI subsystem vendor id */
932 
933 	char		  *unique;	/* Unique identifier: e.g., busid  */
934 	int		  unique_len;	/* Length of unique field	   */
935 	struct cdev	  *devnode;	/* Device number for mknod	   */
936 	int		  if_version;	/* Highest interface version set */
937 
938 	int		  flags;	/* Flags to open(2)		   */
939 
940 				/* Locks */
941 	struct spinlock	  dma_lock;	/* protects dev->dma */
942 	struct lwkt_serialize irq_lock;	/* protects irq condition checks */
943 	struct lock	  dev_lock;	/* protects everything else */
944 
945 	/** \name Locks */
946 	/*@{ */
947 	struct lock struct_mutex;	/**< For others */
948 	/*@} */
949 
950 	/** \name Usage Counters */
951 	/*@{ */
952 	int		  open_count;	/* Outstanding files open	   */
953 	int		  buf_use;	/* Buffers in use -- cannot alloc  */
954 	/*@} */
955 
956 
957 	/** \name Performance counters */
958 	/*@{ */
959 	unsigned long     counters;
960 	enum drm_stat_type	types[15];
961 	atomic_t          counts[15];
962 	/*@} */
963 
964 				/* Authentication */
965 	struct drm_open_hash magiclist;	/**< magic hash table */
966 	struct list_head magicfree;
967 
968 	struct list_head filelist;
969 
970 	/** \name Memory management */
971 	/*@{ */
972 	struct list_head maplist;	/**< Linked list of regions */
973 	int map_count;			/**< Number of mappable regions */
974 	struct drm_open_hash map_hash;	/**< User token hash table for maps */
975 
976 	/** \name Context handle management */
977 	/*@{ */
978 	struct list_head ctxlist;	/**< Linked list of context handles */
979 	int ctx_count;			/**< Number of context handles */
980 	struct lock ctxlist_mutex;	/**< For ctxlist */
981 
982 	struct idr ctx_idr;
983 
984 	/*@} */
985 
986 	struct drm_lock_data lock;	/* Information on hardware lock	   */
987 
988 				/* DMA queues (contexts) */
989 	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
990 
991 	int		  irq;		/* Interrupt used by board	   */
992 	int		  msi_enabled;	/* MSI enabled */
993 	int		  irqrid;	/* Interrupt used by board */
994 	struct resource   *irqr;	/* Resource for interrupt used by board	   */
995 	void		  *irqh;	/* Handle from bus_setup_intr      */
996 
997 	/* Storage of resource pointers for drm_get_resource_* */
998 	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
999 	int		  pcirid[DRM_MAX_PCI_RESOURCE];
1000 
1001 	int		  pci_domain;
1002 	int		  pci_bus;
1003 	int		  pci_slot;
1004 	int		  pci_func;
1005 
1006 	/** \name Context support */
1007 	/*@{ */
1008 	int irq_enabled;		/**< True if irq handler is enabled */
1009 	__volatile__ long context_flag;	/**< Context swapping flag */
1010 	__volatile__ long interrupt_flag; /**< Interruption handler flag */
1011 	__volatile__ long dma_flag;	/**< DMA dispatch flag */
1012 	wait_queue_head_t context_wait;	/**< Processes waiting on ctx switch */
1013 	int last_checked;		/**< Last context checked for DMA */
1014 	int last_context;		/**< Last current context */
1015 	unsigned long last_switch;	/**< jiffies at last context switch */
1016 	/*@} */
1017 
1018 	int		  num_crtcs;
1019 
1020 	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
1021 
1022 				/* Sysctl support */
1023 	struct drm_sysctl_info *sysctl;
1024 	int		  sysctl_node_idx;
1025 
1026 
1027 	drm_sg_mem_t      *sg;  /* Scatter gather memory */
1028 	unsigned long     *ctx_bitmap;
1029 	void		  *dev_private;
1030 
1031 	void		  *drm_ttm_bdev;
1032 
1033 	/*
1034 	 * At load time, disabling the vblank interrupt won't be allowed since
1035 	 * old clients may not call the modeset ioctl and therefore misbehave.
1036 	 * Once the modeset ioctl *has* been called though, we can safely
1037 	 * disable them when unused.
1038 	 */
1039 	int vblank_disable_allowed;
1040 
1041 	wait_queue_head_t *vbl_queue;   /**< VBLANK wait queue */
1042 	atomic_t *_vblank_count;        /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
1043 	struct timeval *_vblank_time;   /**< timestamp of current vblank_count (drivers must alloc right number of fields) */
1044 	struct lock vblank_time_lock;   /**< Protects vblank count and time updates during vblank enable/disable */
1045 	struct lock vbl_lock;
1046 	atomic_t *vblank_refcount;      /* number of users of vblank interruptsper crtc */
1047 	u32 *last_vblank;               /* protected by dev->vbl_lock, used */
1048 					/* for wraparound handling */
1049 	int *vblank_enabled;            /* so we don't call enable more than
1050 					   once per disable */
1051 	int *vblank_inmodeset;          /* Display driver is setting mode */
1052 	u32 *last_vblank_wait;		/* Last vblank seqno waited per CRTC */
1053 	struct timer_list vblank_disable_timer;
1054 
1055 	u32 max_vblank_count;           /**< size of vblank counter register */
1056 
1057 	/**
1058 	 * List of events
1059 	 */
1060 	struct list_head vblank_event_list;
1061 	struct lock	event_lock;
1062 
1063 	/*@} */
1064 
1065 	struct drm_agp_head *agp;	/**< AGP data */
1066 
1067 	struct device *dev;             /**< Device structure */
1068 	struct pci_dev *pdev;		/**< PCI device structure */
1069 	int pci_vendor;			/**< PCI vendor id */
1070 	int pci_device;			/**< PCI device id */
1071 
1072 	struct drm_driver *driver;
1073 	struct drm_local_map *agp_buffer_map;
1074 	unsigned int agp_buffer_token;
1075 	struct drm_minor *control;		/**< Control node for card */
1076 	struct drm_minor *primary;		/**< render type primary screen head */
1077 
1078         struct drm_mode_config mode_config;	/**< Current mode config */
1079 
1080 	/** \name GEM information */
1081 	/*@{ */
1082 	struct lock object_name_lock;
1083 	struct idr object_name_idr;
1084 	/*@} */
1085 	void             *mm_private;
1086 
1087 	void *sysctl_private;
1088 	char busid_str[128];
1089 	int modesetting;
1090 
1091 	int switch_power_state;
1092 };
1093 
1094 #define DRM_SWITCH_POWER_ON 0
1095 #define DRM_SWITCH_POWER_OFF 1
1096 #define DRM_SWITCH_POWER_CHANGING 2
1097 
1098 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1099 					     int feature)
1100 {
1101 	return ((dev->driver->driver_features & feature) ? 1 : 0);
1102 }
1103 
1104 #if __OS_HAS_AGP
1105 static inline int drm_core_has_AGP(struct drm_device *dev)
1106 {
1107 	return drm_core_check_feature(dev, DRIVER_USE_AGP);
1108 }
1109 #else
1110 #define drm_core_has_AGP(dev) (0)
1111 #endif
1112 
1113 enum dmi_field {
1114         DMI_NONE,
1115         DMI_BIOS_VENDOR,
1116         DMI_BIOS_VERSION,
1117         DMI_BIOS_DATE,
1118         DMI_SYS_VENDOR,
1119         DMI_PRODUCT_NAME,
1120         DMI_PRODUCT_VERSION,
1121         DMI_PRODUCT_SERIAL,
1122         DMI_PRODUCT_UUID,
1123         DMI_BOARD_VENDOR,
1124         DMI_BOARD_NAME,
1125         DMI_BOARD_VERSION,
1126         DMI_BOARD_SERIAL,
1127         DMI_BOARD_ASSET_TAG,
1128         DMI_CHASSIS_VENDOR,
1129         DMI_CHASSIS_TYPE,
1130         DMI_CHASSIS_VERSION,
1131         DMI_CHASSIS_SERIAL,
1132         DMI_CHASSIS_ASSET_TAG,
1133         DMI_STRING_MAX,
1134 };
1135 
1136 struct dmi_strmatch {
1137 	unsigned char slot;
1138 	char substr[79];
1139 };
1140 
1141 struct dmi_system_id {
1142         int (*callback)(const struct dmi_system_id *);
1143         const char *ident;
1144         struct dmi_strmatch matches[4];
1145 };
1146 #define	DMI_MATCH(a, b) {(a), (b)}
1147 bool dmi_check_system(const struct dmi_system_id *);
1148 
1149 extern int	drm_notyet_flag;
1150 extern int	drm_debug;
1151 
1152 extern unsigned int drm_vblank_offdelay;
1153 extern unsigned int drm_timestamp_precision;
1154 extern unsigned int drm_timestamp_monotonic;
1155 
1156 /* Device setup support (drm_drv.c) */
1157 int	drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1158 int	drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1159 int	drm_create_cdevs(device_t kdev);
1160 
1161 d_ioctl_t drm_ioctl;
1162 extern int drm_lastclose(struct drm_device *dev);
1163 
1164 				/* Device support (drm_fops.h) */
1165 d_open_t drm_open;
1166 d_close_t drm_close;
1167 d_read_t drm_read;
1168 d_kqfilter_t drm_kqfilter;
1169 int drm_release(device_t kdev);
1170 
1171 d_mmap_t drm_mmap;
1172 d_mmap_single_t drm_mmap_single;
1173 extern drm_local_map_t	*drm_getsarea(struct drm_device *dev);
1174 
1175 void drm_cdevpriv_dtor(void *cd);
1176 
1177 void drm_event_wakeup(struct drm_pending_event *e);
1178 
1179 int drm_add_busid_modesetting(struct drm_device *dev,
1180     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1181 
1182 /* File operations helpers (drm_fops.c) */
1183 extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
1184 					 DRM_STRUCTPROC *p,
1185 					struct drm_device *dev,
1186 					struct file *fp);
1187 extern struct drm_file	*drm_find_file_by_proc(struct drm_device *dev,
1188 					DRM_STRUCTPROC *p);
1189 
1190 #ifdef DUMBBELL_WIP
1191 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1192 		struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1193 		int *prime_fd);
1194 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1195 		struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1196 
1197 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1198 					struct drm_file *file_priv);
1199 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1200 					struct drm_file *file_priv);
1201 
1202 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1203 					    dma_addr_t *addrs, int max_pages);
1204 #endif /* DUMBBELL_WIP */
1205 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1206 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1207 
1208 int drm_gem_dumb_destroy(struct drm_file *file,
1209 			 struct drm_device *dev,
1210 			 uint32_t handle);
1211 
1212 #ifdef DUMBBELL_WIP
1213 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1214 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1215 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1216 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1217 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1218 
1219 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1220 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1221 			 struct drm_gem_object **obj);
1222 #endif /* DUMBBELL_WIP */
1223 
1224 				/* Memory management support (drm_memory.h) */
1225 #include <drm/drm_memory.h>
1226 void	drm_mem_init(void);
1227 void	drm_mem_uninit(void);
1228 void	*drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1229 void	*drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1230 void	drm_ioremapfree(drm_local_map_t *map);
1231 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
1232 int	drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1233 
1234 int	drm_ctxbitmap_init(struct drm_device *dev);
1235 void	drm_ctxbitmap_cleanup(struct drm_device *dev);
1236 void	drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1237 
1238 /* Locking IOCTL support (drm_lock.c) */
1239 int	drm_lock_take(struct drm_lock_data *lock_data,
1240 		      unsigned int context);
1241 int	drm_lock_transfer(struct drm_lock_data *lock_data,
1242 			  unsigned int context);
1243 int	drm_lock_free(struct drm_lock_data *lock_data,
1244 		      unsigned int context);
1245 
1246 /*
1247  * These are exported to drivers so that they can implement fencing using
1248  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1249  */
1250 
1251 unsigned long drm_get_resource_start(struct drm_device *dev,
1252 				     unsigned int resource);
1253 unsigned long drm_get_resource_len(struct drm_device *dev,
1254 				   unsigned int resource);
1255 void	drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1256 int	drm_order(unsigned long size);
1257 int	drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request);
1258 int	drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request);
1259 extern int drm_addmap(struct drm_device *dev, resource_size_t offset,
1260 		      unsigned int size, enum drm_map_type type,
1261 		      enum drm_map_flags flags, struct drm_local_map **map_ptr);
1262 
1263 /* DMA support (drm_dma.c) */
1264 int	drm_dma_setup(struct drm_device *dev);
1265 void	drm_dma_takedown(struct drm_device *dev);
1266 void	drm_free_buffer(struct drm_device *dev, drm_buf_t *buf);
1267 void	drm_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv);
1268 #define drm_core_reclaim_buffers drm_reclaim_buffers
1269 
1270 				/* IRQ support (drm_irq.h) */
1271 int	drm_irq_install(struct drm_device *dev);
1272 int	drm_irq_uninstall(struct drm_device *dev);
1273 void	drm_driver_irq_preinstall(struct drm_device *dev);
1274 void	drm_driver_irq_postinstall(struct drm_device *dev);
1275 void	drm_driver_irq_uninstall(struct drm_device *dev);
1276 
1277 void	drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1278 void	drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1279 int 	drm_modeset_ctl(struct drm_device *dev, void *data,
1280 			struct drm_file *file_priv);
1281 
1282 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1283 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1284 			   struct drm_file *filp);
1285 extern int drm_vblank_wait(struct drm_device *dev, unsigned int *vbl_seq);
1286 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1287 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1288 				     struct timeval *vblanktime);
1289 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1290 				     struct drm_pending_vblank_event *e);
1291 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1292 void drm_handle_vblank_events(struct drm_device *dev, int crtc);
1293 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1294 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1295 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1296 extern void drm_vblank_cleanup(struct drm_device *dev);
1297 extern u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1298 				     struct timeval *tvblank, unsigned flags);
1299 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1300 						 int crtc, int *max_error,
1301 						 struct timeval *vblank_time,
1302 						 unsigned flags,
1303 						 struct drm_crtc *refcrtc);
1304 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1305 					    const struct drm_display_mode *mode);
1306 
1307 /* AGP/PCI Express/GART support (drm_agpsupport.c) */
1308 int	drm_device_is_agp(struct drm_device *dev);
1309 int	drm_device_is_pcie(struct drm_device *dev);
1310 drm_agp_head_t *drm_agp_init(void);
1311 int	drm_agp_acquire(struct drm_device *dev);
1312 int	drm_agp_release(struct drm_device *dev);
1313 int	drm_agp_info(struct drm_device * dev, struct drm_agp_info *info);
1314 int	drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1315 void	*drm_agp_allocate_memory(size_t pages, u32 type);
1316 int	drm_agp_free_memory(void *handle);
1317 int	drm_agp_bind_memory(void *handle, off_t start);
1318 int	drm_agp_unbind_memory(void *handle);
1319 int	drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1320 int	drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1321 int	drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1322 int	drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1323 
1324 /* Scatter Gather Support (drm_scatter.c) */
1325 void	drm_sg_cleanup(drm_sg_mem_t *entry);
1326 int	drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1327 
1328 /* sysctl support (drm_sysctl.h) */
1329 extern int		drm_sysctl_init(struct drm_device *dev);
1330 extern int		drm_sysctl_cleanup(struct drm_device *dev);
1331 
1332 /* ATI PCIGART support (ati_pcigart.c) */
1333 int	drm_ati_pcigart_init(struct drm_device *dev,
1334 				struct drm_ati_pcigart_info *gart_info);
1335 int	drm_ati_pcigart_cleanup(struct drm_device *dev,
1336 				struct drm_ati_pcigart_info *gart_info);
1337 
1338 /* Cache management (drm_memory.c) */
1339 void	drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1340 
1341 /* Locking IOCTL support (drm_drv.c) */
1342 int	drm_lock(struct drm_device *dev, void *data,
1343 		 struct drm_file *file_priv);
1344 int	drm_unlock(struct drm_device *dev, void *data,
1345 		   struct drm_file *file_priv);
1346 int	drm_version(struct drm_device *dev, void *data,
1347 		    struct drm_file *file_priv);
1348 int	drm_setversion(struct drm_device *dev, void *data,
1349 		       struct drm_file *file_priv);
1350 
1351 /* Misc. IOCTL support (drm_ioctl.c) */
1352 int	drm_irq_by_busid(struct drm_device *dev, void *data,
1353 			 struct drm_file *file_priv);
1354 int	drm_getunique(struct drm_device *dev, void *data,
1355 		      struct drm_file *file_priv);
1356 int	drm_setunique(struct drm_device *dev, void *data,
1357 		      struct drm_file *file_priv);
1358 int	drm_getmap(struct drm_device *dev, void *data,
1359 		   struct drm_file *file_priv);
1360 int	drm_getclient(struct drm_device *dev, void *data,
1361 		      struct drm_file *file_priv);
1362 int	drm_getstats(struct drm_device *dev, void *data,
1363 		     struct drm_file *file_priv);
1364 int	drm_getcap(struct drm_device *dev, void *data,
1365 		     struct drm_file *file_priv);
1366 extern int drm_setclientcap(struct drm_device *dev, void *data,
1367 			    struct drm_file *file_priv);
1368 int	drm_noop(struct drm_device *dev, void *data,
1369 		 struct drm_file *file_priv);
1370 
1371 /* Context IOCTL support (drm_context.c) */
1372 int	drm_resctx(struct drm_device *dev, void *data,
1373 		   struct drm_file *file_priv);
1374 int	drm_addctx(struct drm_device *dev, void *data,
1375 		   struct drm_file *file_priv);
1376 int	drm_modctx(struct drm_device *dev, void *data,
1377 		   struct drm_file *file_priv);
1378 int	drm_getctx(struct drm_device *dev, void *data,
1379 		   struct drm_file *file_priv);
1380 int	drm_switchctx(struct drm_device *dev, void *data,
1381 		      struct drm_file *file_priv);
1382 int	drm_newctx(struct drm_device *dev, void *data,
1383 		   struct drm_file *file_priv);
1384 int	drm_rmctx(struct drm_device *dev, void *data,
1385 		  struct drm_file *file_priv);
1386 int	drm_setsareactx(struct drm_device *dev, void *data,
1387 			struct drm_file *file_priv);
1388 int	drm_getsareactx(struct drm_device *dev, void *data,
1389 			struct drm_file *file_priv);
1390 
1391 /* Authentication IOCTL support (drm_auth.c) */
1392 int	drm_getmagic(struct drm_device *dev, void *data,
1393 		     struct drm_file *file_priv);
1394 int	drm_authmagic(struct drm_device *dev, void *data,
1395 		      struct drm_file *file_priv);
1396 
1397 /* Cache management (drm_cache.c) */
1398 void drm_clflush_virt_range(char *addr, unsigned long length);
1399 
1400 /* Buffer management support (drm_bufs.c) */
1401 int	drm_addmap_ioctl(struct drm_device *dev, void *data,
1402 			 struct drm_file *file_priv);
1403 int	drm_rmmap_ioctl(struct drm_device *dev, void *data,
1404 			struct drm_file *file_priv);
1405 int	drm_addbufs(struct drm_device *dev, void *data,
1406 		    struct drm_file *file_priv);
1407 int	drm_infobufs(struct drm_device *dev, void *data,
1408 		     struct drm_file *file_priv);
1409 int	drm_markbufs(struct drm_device *dev, void *data,
1410 		     struct drm_file *file_priv);
1411 int	drm_freebufs(struct drm_device *dev, void *data,
1412 		     struct drm_file *file_priv);
1413 int	drm_mapbufs(struct drm_device *dev, void *data,
1414 		    struct drm_file *file_priv);
1415 
1416 /* DMA support (drm_dma.c) */
1417 int	drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1418 
1419 /* IRQ support (drm_irq.c) */
1420 int	drm_control(struct drm_device *dev, void *data,
1421 		    struct drm_file *file_priv);
1422 
1423 /* AGP/GART support (drm_agpsupport.c) */
1424 int	drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1425 			      struct drm_file *file_priv);
1426 int	drm_agp_release_ioctl(struct drm_device *dev, void *data,
1427 			      struct drm_file *file_priv);
1428 int	drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1429 			     struct drm_file *file_priv);
1430 int	drm_agp_info_ioctl(struct drm_device *dev, void *data,
1431 			   struct drm_file *file_priv);
1432 int	drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1433 			    struct drm_file *file_priv);
1434 int	drm_agp_free_ioctl(struct drm_device *dev, void *data,
1435 			   struct drm_file *file_priv);
1436 int	drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1437 			     struct drm_file *file_priv);
1438 int	drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1439 			   struct drm_file *file_priv);
1440 
1441 				/* Stub support (drm_stub.h) */
1442 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1443 			       struct drm_file *file_priv);
1444 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1445 				struct drm_file *file_priv);
1446 
1447 /* Scatter Gather Support (drm_scatter.c) */
1448 int	drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1449 			   struct drm_file *file_priv);
1450 int	drm_sg_free(struct drm_device *dev, void *data,
1451 		    struct drm_file *file_priv);
1452 
1453 /* consistent PCI memory functions (drm_pci.c) */
1454 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1455 				       size_t align);
1456 void	drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1457 
1458 			       /* sysfs support (drm_sysfs.c) */
1459 struct drm_sysfs_class;
1460 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1461 extern void drm_sysfs_destroy(void);
1462 extern int drm_sysfs_device_add(struct drm_minor *minor);
1463 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1464 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1465 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1466 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1467 
1468 /* Graphics Execution Manager library functions (drm_gem.c) */
1469 int drm_gem_init(struct drm_device *dev);
1470 void drm_gem_destroy(struct drm_device *dev);
1471 void drm_gem_object_release(struct drm_gem_object *obj);
1472 void drm_gem_object_free(struct kref *kref);
1473 int drm_gem_object_init(struct drm_device *dev,
1474 			struct drm_gem_object *obj, size_t size);
1475 void drm_gem_private_object_init(struct drm_device *dev,
1476 				 struct drm_gem_object *obj, size_t size);
1477 
1478 int i915_gem_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
1479     vm_ooffset_t foff, struct ucred *cred, u_short *color);
1480 void i915_gem_pager_dtor(void * handle);
1481 
1482 #include <drm/drm_global.h>
1483 
1484 static inline void
1485 drm_gem_object_reference(struct drm_gem_object *obj)
1486 {
1487 	kref_get(&obj->refcount);
1488 }
1489 
1490 static inline void
1491 drm_gem_object_unreference(struct drm_gem_object *obj)
1492 {
1493 	if (obj != NULL)
1494 		kref_put(&obj->refcount, drm_gem_object_free);
1495 }
1496 
1497 static inline void
1498 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1499 {
1500 	if (obj != NULL) {
1501 		struct drm_device *dev = obj->dev;
1502 		DRM_LOCK(dev);
1503 		kref_put(&obj->refcount, drm_gem_object_free);
1504 		DRM_UNLOCK(dev);
1505 	}
1506 }
1507 
1508 int drm_gem_handle_create(struct drm_file *file_priv,
1509 			  struct drm_gem_object *obj,
1510 			  u32 *handlep);
1511 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
1512 
1513 static inline void
1514 drm_gem_object_handle_reference(struct drm_gem_object *obj)
1515 {
1516 	drm_gem_object_reference(obj);
1517 	atomic_inc(&obj->handle_count);
1518 }
1519 
1520 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1521 					     struct drm_file *filp,
1522 					     u32 handle);
1523 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1524 			struct drm_file *file_priv);
1525 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1526 			struct drm_file *file_priv);
1527 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1528 		       struct drm_file *file_priv);
1529 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1530 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1531 
1532 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1533 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1534 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
1535 
1536 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1537     vm_size_t size, struct vm_object **obj_res, int nprot);
1538 void drm_gem_pager_dtr(void *obj);
1539 
1540 struct ttm_bo_device;
1541 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1542     vm_size_t size, struct vm_object **obj_res, int nprot);
1543 struct ttm_buffer_object;
1544 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1545 
1546 void drm_device_lock_mtx(struct drm_device *dev);
1547 void drm_device_unlock_mtx(struct drm_device *dev);
1548 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1549     const char *msg, int timeout);
1550 void drm_device_assert_mtx_locked(struct drm_device *dev);
1551 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1552 
1553 void drm_device_lock_struct(struct drm_device *dev);
1554 void drm_device_unlock_struct(struct drm_device *dev);
1555 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1556     const char *msg, int timeout);
1557 void drm_device_assert_struct_locked(struct drm_device *dev);
1558 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1559 
1560 void drm_compat_locking_init(struct drm_device *dev);
1561 void drm_sleep_locking_init(struct drm_device *dev);
1562 
1563 /* drm_modes.c */
1564 bool drm_mode_parse_command_line_for_connector(const char *mode_option,
1565     struct drm_connector *connector, struct drm_cmdline_mode *mode);
1566 struct drm_display_mode *drm_mode_create_from_cmdline_mode(
1567     struct drm_device *dev, struct drm_cmdline_mode *cmd);
1568 
1569 /* Inline replacements for drm_alloc and friends */
1570 static __inline__ void *
1571 drm_alloc(size_t size, struct malloc_type *area)
1572 {
1573 	return kmalloc(size, area, M_WAITOK | M_NULLOK);
1574 }
1575 
1576 static __inline__ void *
1577 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1578 {
1579 	return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1580 }
1581 
1582 static __inline__ void
1583 drm_free(void *pt, struct malloc_type *area)
1584 {
1585 	kfree(pt);
1586 }
1587 
1588 static __inline__ void
1589 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1590 {
1591 	map->handle = drm_ioremap_wc(dev, map);
1592 }
1593 static __inline__ void
1594 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1595 {
1596 	map->handle = drm_ioremap(dev, map);
1597 }
1598 static __inline__ void
1599 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1600 {
1601 	if ( map->handle && map->size )
1602 		drm_ioremapfree(map);
1603 }
1604 
1605 static __inline__ struct drm_local_map *
1606 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1607 {
1608 	struct drm_map_list *_entry;
1609 
1610 	list_for_each_entry(_entry, &dev->maplist, head) {
1611 		if (offset == (unsigned long)_entry->map->handle)
1612 			return _entry->map;
1613 	}
1614 	return NULL;
1615 }
1616 
1617 static __inline__ void drm_core_dropmap(struct drm_map *map)
1618 {
1619 }
1620 
1621 #include <drm/drm_mem_util.h>
1622 
1623 /* FreeBSD compatibility macros */
1624 #define VM_OBJECT_WLOCK(object)		VM_OBJECT_LOCK(object)
1625 #define VM_OBJECT_WUNLOCK(object)	VM_OBJECT_UNLOCK(object)
1626 
1627 #define DRM_PCIE_SPEED_25 1
1628 #define DRM_PCIE_SPEED_50 2
1629 #define DRM_PCIE_SPEED_80 4
1630 
1631 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1632 
1633 #define	drm_can_sleep()	(DRM_HZ & 1)
1634 
1635 #endif /* __KERNEL__ */
1636 #endif /* _DRM_P_H_ */
1637