xref: /dragonfly/sys/dev/drm/include/drm/drmP.h (revision a1282e19)
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 <linux/highmem.h>
64 #include <sys/sbuf.h>
65 #include <sys/taskqueue.h>
66 #include <sys/tree.h>
67 #include <vm/vm.h>
68 #include <vm/pmap.h>
69 #include <vm/vm_extern.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page2.h>
74 #include <vm/vm_pager.h>
75 #include <vm/vm_param.h>
76 #include <machine/param.h>
77 #include <machine/pmap.h>
78 #ifdef __x86_64__
79 #include <machine/specialreg.h>
80 #endif
81 #include <machine/sysarch.h>
82 #include <sys/endian.h>
83 #include <sys/mman.h>
84 #include <sys/rman.h>
85 #include <sys/memrange.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/dma-mapping.h>
97 #include <linux/capability.h>
98 #include <linux/err.h>
99 #include <linux/idr.h>
100 #include <linux/pci.h>
101 #include <linux/jiffies.h>
102 #include <linux/kernel.h>
103 #include <linux/fs.h>
104 #include <linux/kref.h>
105 #include <linux/list.h>
106 #include <linux/mm.h>
107 #include <linux/moduleparam.h>
108 #include <linux/mutex.h>
109 #include <linux/slab.h>
110 #include <linux/scatterlist.h>
111 #include <linux/timer.h>
112 #include <asm/io.h>
113 #include <linux/seq_file.h>
114 #include <linux/types.h>
115 #include <linux/wait.h>
116 #include <linux/workqueue.h>
117 
118 #include <asm/uaccess.h>
119 
120 #include <drm/drm_global.h>
121 
122 #include <drm/drm_vma_manager.h>
123 
124 struct drm_file;
125 struct drm_device;
126 struct drm_agp_head;
127 struct drm_gem_object;
128 
129 struct device_node;
130 struct videomode;
131 
132 #include <drm/drm_os_linux.h>
133 #include <drm/drm_hashtab.h>
134 #include <drm/drm_mm.h>
135 
136 /*
137  * 4 debug categories are defined:
138  *
139  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
140  *	 This is the category used by the DRM_DEBUG() macro.
141  *
142  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
143  *	   This is the category used by the DRM_DEBUG_DRIVER() macro.
144  *
145  * KMS: used in the modesetting code.
146  *	This is the category used by the DRM_DEBUG_KMS() macro.
147  *
148  * PRIME: used in the prime code.
149  *	  This is the category used by the DRM_DEBUG_PRIME() macro.
150  *
151  * Enabling verbose debug messages is done through the drm.debug parameter,
152  * each category being enabled by a bit.
153  *
154  * drm.debug=0x1 will enable CORE messages
155  * drm.debug=0x2 will enable DRIVER messages
156  * drm.debug=0x3 will enable CORE and DRIVER messages
157  * ...
158  * drm.debug=0xf will enable all messages
159  *
160  * An interesting feature is that it's possible to enable verbose logging at
161  * run-time by echoing the debug value in its sysfs node:
162  *   # echo 0xf > /sys/module/drm/parameters/debug
163  */
164 #define DRM_UT_CORE 		0x01
165 #define DRM_UT_DRIVER		0x02
166 #define DRM_UT_KMS		0x04
167 #define DRM_UT_PRIME		0x08
168 
169 void drm_ut_debug_printk(const char *function_name,
170 			 const char *format, ...);
171 
172 void drm_err(const char *func, const char *format, ...);
173 
174 /***********************************************************************/
175 /** \name DRM template customization defaults */
176 /*@{*/
177 
178 /* driver capabilities and requirements mask */
179 #define DRIVER_USE_AGP     0x1
180 #define DRIVER_REQUIRE_AGP 0x2
181 #define DRIVER_PCI_DMA     0x8
182 #define DRIVER_SG          0x10
183 #define DRIVER_HAVE_DMA    0x20
184 #define DRIVER_HAVE_IRQ    0x40
185 #define DRIVER_IRQ_SHARED  0x80
186 #define DRIVER_DMA_QUEUE   0x200
187 #define DRIVER_GEM         0x1000
188 #define DRIVER_MODESET     0x2000
189 #define DRIVER_PRIME       0x4000
190 #define DRIVER_RENDER      0x8000
191 
192 /***********************************************************************/
193 /** \name Begin the DRM... */
194 /*@{*/
195 
196 #define DRM_DEBUG_CODE 2	  /**< Include debugging code if > 1, then
197 				     also include looping detection. */
198 
199 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
200 
201 /*@}*/
202 
203 /***********************************************************************/
204 /** \name Macros to make printk easier */
205 /*@{*/
206 
207 /**
208  * Error output.
209  *
210  * \param fmt printf() like format string.
211  * \param arg arguments
212  */
213 #define DRM_ERROR(fmt, ...) \
214 	kprintf("error: [" DRM_NAME ":pid%d:%s] *ERROR* " fmt,		\
215 	    DRM_CURRENTPID, __func__ , ##__VA_ARGS__)
216 
217 #define	DRM_GEM_MAPPING_MASK	(3ULL << 62)
218 #define	DRM_GEM_MAPPING_KEY	(2ULL << 62) /* Non-canonical address form */
219 #define	DRM_GEM_MAX_IDX		0x3fffff
220 #define	DRM_GEM_MAPPING_IDX(o)	(((o) >> 40) & DRM_GEM_MAX_IDX)
221 #define	DRM_GEM_MAPPING_OFF(i)	(((uint64_t)(i)) << 40)
222 #define	DRM_GEM_MAPPING_MAPOFF(o) \
223     ((o) & ~(DRM_GEM_MAPPING_OFF(DRM_GEM_MAX_IDX) | DRM_GEM_MAPPING_KEY))
224 
225 SYSCTL_DECL(_hw_drm);
226 
227 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
228 
229 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
230 
231 #define __OS_HAS_AGP	1
232 
233 #define DRM_DEV_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
234 #define DRM_DEV_UID	0
235 #define DRM_DEV_GID	0
236 
237 #define DRM_CURPROC		curthread
238 #define DRM_STRUCTPROC		struct thread
239 #define DRM_CURRENTPID		(curproc != NULL ? curproc->p_pid : -1)
240 #define DRM_LOCK(dev)		lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
241 #define DRM_UNLOCK(dev)		lockmgr(&(dev)->struct_mutex, LK_RELEASE)
242 #define	DRM_LOCK_SLEEP(dev, chan, flags, msg, timeout)			\
243     (lksleep((chan), &(dev)->struct_mutex, (flags), (msg), (timeout)))
244 #if defined(INVARIANTS)
245 #define	DRM_LOCK_ASSERT(dev)	KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
246 #define	DRM_UNLOCK_ASSERT(dev)	KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
247 #else
248 #define	DRM_LOCK_ASSERT(d)
249 #define	DRM_UNLOCK_ASSERT(d)
250 #endif
251 
252 #define DRM_SYSCTL_HANDLER_ARGS	(SYSCTL_HANDLER_ARGS)
253 
254 typedef void			irqreturn_t;
255 #define IRQ_HANDLED		/* nothing */
256 #define IRQ_NONE		/* nothing */
257 
258 enum {
259 	DRM_IS_NOT_AGP,
260 	DRM_IS_AGP,
261 	DRM_MIGHT_BE_AGP
262 };
263 #define DRM_AGP_MEM		struct agp_memory_info
264 
265 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
266 
267 #define DRM_AGP_FIND_DEVICE()	agp_find_device()
268 #define DRM_MTRR_WC		MDF_WRITECOMBINE
269 
270 #define LOCK_TEST_WITH_RETURN(dev, file_priv)				\
271 do {									\
272 	if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||		\
273 	     dev->lock.file_priv != file_priv) {			\
274 		DRM_ERROR("%s called without lock held\n",		\
275 			   __FUNCTION__);				\
276 		return EINVAL;						\
277 	}								\
278 } while (0)
279 
280 /* Returns -errno to shared code */
281 #define DRM_WAIT_ON( ret, queue, timeout, condition )		\
282 for ( ret = 0 ; !ret && !(condition) ; ) {			\
283 	DRM_UNLOCK(dev);					\
284 	lwkt_serialize_enter(&dev->irq_lock);			\
285 	if (!(condition)) {					\
286             tsleep_interlock(&(queue), PCATCH);			\
287             lwkt_serialize_exit(&dev->irq_lock);		\
288             ret = -tsleep(&(queue), PCATCH | PINTERLOCKED,	\
289 			  "drmwtq", (timeout));			\
290         } else {                                                \
291                 lwkt_serialize_exit(&dev->irq_lock);            \
292         }                                                 	\
293 	DRM_LOCK(dev);						\
294 }
295 
296 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
297     vm_memattr_t memattr);
298 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
299 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
300 
301 /***********************************************************************/
302 /** \name Macros to make printk easier */
303 /*@{*/
304 
305 
306 #define DRM_INFO(fmt, ...)  kprintf("info: [" DRM_NAME "] " fmt , ##__VA_ARGS__)
307 
308 #define DRM_INFO_ONCE(fmt, ...)				\
309 	printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
310 
311 #define	DRM_DEBUGBITS_DEBUG		0x1
312 #define	DRM_DEBUGBITS_KMS		0x2
313 #define	DRM_DEBUGBITS_FAILED_IOCTL	0x4
314 #define	DRM_DEBUGBITS_VERBOSE		0x8
315 
316 #define DRM_DEBUG(fmt, ...) do {					\
317 	if ((drm_debug & DRM_DEBUGBITS_DEBUG) != 0)		\
318 		kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
319 			__func__ , ##__VA_ARGS__);			\
320 } while (0)
321 
322 #define DRM_DEBUG_VERBOSE(fmt, ...) do {				\
323 	if ((drm_debug & DRM_DEBUGBITS_VERBOSE) != 0)		\
324 		kprintf("[" DRM_NAME ":pid%d:%s] " fmt, DRM_CURRENTPID,	\
325 			__func__ , ##__VA_ARGS__);			\
326 } while (0)
327 
328 #define DRM_DEBUG_KMS(fmt, ...) do {					\
329 	if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)			\
330 		kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
331 			__func__ , ##__VA_ARGS__);			\
332 } while (0)
333 
334 #define DRM_DEBUG_DRIVER(fmt, ...) do {					\
335 	if ((drm_debug & DRM_DEBUGBITS_KMS) != 0)			\
336 		kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,\
337 			__func__ , ##__VA_ARGS__);			\
338 } while (0)
339 
340 #define DRM_LOG_KMS(fmt, ...) do {					\
341 	kprintf("[" DRM_NAME ":KMS:pid%d:%s] " fmt, DRM_CURRENTPID,	\
342 			__func__ , ##__VA_ARGS__);			\
343 } while (0)
344 
345 #define	dev_dbg(dev, fmt, ...) do {					\
346 	if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {			\
347 		device_printf((dev), "debug: " fmt, ## __VA_ARGS__);	\
348 	}								\
349 } while (0)
350 
351 typedef struct drm_pci_id_list
352 {
353 	int vendor;
354 	int device;
355 	long driver_private;
356 	char *name;
357 } drm_pci_id_list_t;
358 
359 struct drm_msi_blacklist_entry
360 {
361 	int vendor;
362 	int device;
363 };
364 
365 /**
366  * Ioctl function type.
367  *
368  * \param inode device inode.
369  * \param file_priv DRM file private pointer.
370  * \param cmd command.
371  * \param arg argument.
372  */
373 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
374 			struct drm_file *file_priv);
375 
376 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
377 			       unsigned long arg);
378 
379 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
380 
381 #define DRM_AUTH	0x1
382 #define DRM_MASTER	0x2
383 #define DRM_ROOT_ONLY	0x4
384 #define DRM_CONTROL_ALLOW 0x8
385 #define DRM_UNLOCKED	0x10
386 #define DRM_RENDER_ALLOW 0x20
387 
388 struct drm_ioctl_desc {
389 	unsigned int cmd;
390 	int flags;
391 	drm_ioctl_t *func;
392 	unsigned int cmd_drv;
393 	const char *name;
394 };
395 
396 /**
397  * Creates a driver or general drm_ioctl_desc array entry for the given
398  * ioctl, for use by drm_ioctl().
399  */
400 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)			\
401 	[DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
402 
403 typedef struct drm_magic_entry {
404 	struct list_head head;
405 	struct drm_hash_item hash_item;
406 	struct drm_file	       *priv;
407 	struct drm_magic_entry *next;
408 } drm_magic_entry_t;
409 
410 typedef struct drm_magic_head {
411 	struct drm_magic_entry *head;
412 	struct drm_magic_entry *tail;
413 } drm_magic_head_t;
414 
415 typedef struct drm_buf {
416 	int idx;		       /**< Index into master buflist */
417 	int total;		       /**< Buffer size */
418 	int order;		       /**< log-base-2(total) */
419 	int used;		       /**< Amount of buffer in use (for DMA) */
420 	unsigned long offset;	       /**< Byte offset (used internally) */
421 	void *address;		       /**< Address of buffer */
422 	unsigned long bus_address;     /**< Bus address of buffer */
423 	struct drm_buf *next;	       /**< Kernel-only: used for free list */
424 	__volatile__ int waiting;      /**< On kernel DMA queue */
425 	__volatile__ int pending;      /**< On hardware DMA queue */
426 	struct drm_file *file_priv;    /**< Private of holding file descr */
427 	int context;		       /**< Kernel queue for this buffer */
428 	int while_locked;	       /**< Dispatch this buffer while locked */
429 	enum {
430 		DRM_LIST_NONE = 0,
431 		DRM_LIST_FREE = 1,
432 		DRM_LIST_WAIT = 2,
433 		DRM_LIST_PEND = 3,
434 		DRM_LIST_PRIO = 4,
435 		DRM_LIST_RECLAIM = 5
436 	} list;			       /**< Which list we're on */
437 
438 	int dev_priv_size;		 /**< Size of buffer private storage */
439 	void *dev_private;		 /**< Per-buffer private storage */
440 } drm_buf_t;
441 
442 /** bufs is one longer than it has to be */
443 struct drm_waitlist {
444 	int count;			/**< Number of possible buffers */
445 	struct drm_buf **bufs;		/**< List of pointers to buffers */
446 	struct drm_buf **rp;			/**< Read pointer */
447 	struct drm_buf **wp;			/**< Write pointer */
448 	struct drm_buf **end;		/**< End pointer */
449 	spinlock_t read_lock;
450 	spinlock_t write_lock;
451 };
452 
453 typedef struct drm_dma_handle {
454 	void *vaddr;
455 	size_t size;
456 	bus_addr_t busaddr;
457 	bus_dma_tag_t tag;
458 	bus_dmamap_t map;
459 } drm_dma_handle_t;
460 
461 typedef struct drm_buf_entry {
462 	int		  buf_size;
463 	int		  buf_count;
464 	drm_buf_t	  *buflist;
465 	int		  seg_count;
466 	int		  page_order;
467 	struct drm_dma_handle **seglist;
468 
469 	int low_mark;			/**< Low water mark */
470 	int high_mark;			/**< High water mark */
471 } drm_buf_entry_t;
472 
473 /* Event queued up for userspace to read */
474 struct drm_pending_event {
475 	struct drm_event *event;
476 	struct list_head link;
477 	struct drm_file *file_priv;
478 	pid_t pid; /* pid of requester, no guarantee it's valid by the time
479 		      we deliver the event, for tracing only */
480 	void (*destroy)(struct drm_pending_event *event);
481 };
482 
483 /* initial implementaton using a linked list - todo hashtab */
484 struct drm_prime_file_private {
485 	struct list_head head;
486 #ifdef DUMBBELL_WIP
487 	struct mutex lock;
488 #endif /* DUMBBELL_WIP */
489 };
490 
491 /** File private data */
492 struct drm_file {
493 	int authenticated;
494 	struct drm_device *dev;
495 	int		  master;
496 
497 	/* true when the client has asked us to expose stereo 3D mode flags */
498 	bool stereo_allowed;
499 	/*
500 	 * true if client understands CRTC primary planes and cursor planes
501 	 * in the plane list
502 	 */
503 	unsigned universal_planes:1;
504 
505 	pid_t		  pid;
506 	uid_t		  uid;
507 	drm_magic_t	  magic;
508 	unsigned long	  ioctl_count;
509 	struct list_head lhead;
510 	struct kqinfo	  dkq;
511 
512 	/** Mapping of mm object handles to object pointers. */
513 	struct idr object_idr;
514 	/** Lock for synchronization of access to object_idr. */
515 	struct lock table_lock;
516 
517 	void		 *driver_priv;
518 
519 	int		  is_master;
520 	struct drm_master *masterp;
521 
522 	/**
523 	 * fbs - List of framebuffers associated with this file.
524 	 *
525 	 * Protected by fbs_lock. Note that the fbs list holds a reference on
526 	 * the fb object to prevent it from untimely disappearing.
527 	 */
528 	struct list_head fbs;
529 	struct lock fbs_lock;
530 
531 	wait_queue_head_t event_wait;
532 	struct list_head  event_list;
533 	int		  event_space;
534 
535 	struct drm_prime_file_private prime;
536 };
537 
538 /** Wait queue */
539 struct drm_queue {
540 	atomic_t use_count;		/**< Outstanding uses (+1) */
541 	atomic_t finalization;		/**< Finalization in progress */
542 	atomic_t block_count;		/**< Count of processes waiting */
543 	atomic_t block_read;		/**< Queue blocked for reads */
544 	wait_queue_head_t read_queue;	/**< Processes waiting on block_read */
545 	atomic_t block_write;		/**< Queue blocked for writes */
546 	wait_queue_head_t write_queue;	/**< Processes waiting on block_write */
547 	atomic_t total_queued;		/**< Total queued statistic */
548 	atomic_t total_flushed;		/**< Total flushes statistic */
549 	atomic_t total_locks;		/**< Total locks statistics */
550 	enum drm_ctx_flags flags;	/**< Context preserving and 2D-only */
551 	struct drm_waitlist waitlist;	/**< Pending buffers */
552 	wait_queue_head_t flush_queue;	/**< Processes waiting until flush */
553 };
554 
555 /**
556  * Lock data.
557  */
558 struct drm_lock_data {
559 	struct drm_hw_lock *hw_lock;	/**< Hardware lock */
560 	/** Private of lock holder's file (NULL=kernel) */
561 	struct drm_file *file_priv;
562 	wait_queue_head_t lock_queue;	/**< Queue of blocked processes */
563 	unsigned long lock_time;	/**< Time of last lock in jiffies */
564 };
565 
566 /* This structure, in the struct drm_device, is always initialized while the
567  * device
568  * is open.  dev->dma_lock protects the incrementing of dev->buf_use, which
569  * when set marks that no further bufs may be allocated until device teardown
570  * occurs (when the last open of the device has closed).  The high/low
571  * watermarks of bufs are only touched by the X Server, and thus not
572  * concurrently accessed, so no locking is needed.
573  */
574 typedef struct drm_device_dma {
575 
576 	struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];	/**< buffers, grouped by their size order */
577 	int buf_count;			/**< total number of buffers */
578 	struct drm_buf **buflist;		/**< Vector of pointers into drm_device_dma::bufs */
579 	int seg_count;
580 	int page_count;			/**< number of pages */
581 	unsigned long *pagelist;	/**< page list */
582 	unsigned long byte_count;
583 	enum {
584 		_DRM_DMA_USE_AGP = 0x01,
585 		_DRM_DMA_USE_SG = 0x02,
586 		_DRM_DMA_USE_FB = 0x04,
587 		_DRM_DMA_USE_PCI_RO = 0x08
588 	} flags;
589 
590 } drm_device_dma_t;
591 
592 typedef struct drm_sg_mem {
593 	vm_offset_t vaddr;
594 	vm_paddr_t *busaddr;
595 	vm_pindex_t pages;
596 } drm_sg_mem_t;
597 
598 /**
599  * Kernel side of a mapping
600  */
601 struct drm_local_map {
602 	resource_size_t offset;	 /**< Requested physical address (0 for SAREA)*/
603 	unsigned long size;	 /**< Requested physical size (bytes) */
604 	enum drm_map_type type;	 /**< Type of memory to map */
605 	enum drm_map_flags flags;	 /**< Flags */
606 	void *handle;		 /**< User-space: "Handle" to pass to mmap() */
607 				 /**< Kernel-space: kernel-virtual address */
608 	int mtrr;		 /**< MTRR slot used */
609 };
610 
611 typedef struct drm_local_map drm_local_map_t;
612 
613 /**
614  * Mappings list
615  */
616 struct drm_map_list {
617 	struct list_head head;		/**< list head */
618 	struct drm_hash_item hash;
619 	struct drm_local_map *map;	/**< mapping */
620 	uint64_t user_token;
621 	struct drm_master *master;
622 	struct drm_mm_node *file_offset_node;	/**< fake offset */
623 };
624 
625 /* location of GART table */
626 #define DRM_ATI_GART_MAIN 1
627 #define DRM_ATI_GART_FB   2
628 
629 #define DRM_ATI_GART_PCI 1
630 #define DRM_ATI_GART_PCIE 2
631 #define DRM_ATI_GART_IGP 3
632 
633 struct drm_ati_pcigart_info {
634 	int gart_table_location;
635 	int gart_reg_if;
636 	void *addr;
637 	dma_addr_t bus_addr;
638 	dma_addr_t table_mask;
639 	dma_addr_t member_mask;
640 	struct drm_dma_handle *table_handle;
641 	drm_local_map_t mapping;
642 	int table_size;
643 	struct drm_dma_handle *dmah; /* handle for ATI PCIGART table */
644 };
645 
646 /**
647  * GEM specific mm private for tracking GEM objects
648  */
649 struct drm_gem_mm {
650 	struct drm_vma_offset_manager vma_manager;
651 	struct drm_mm offset_manager;	/**< Offset mgmt for buffer objects */
652 	struct drm_open_hash offset_hash; /**< User token hash table for maps */
653 	struct unrhdr *idxunr;
654 };
655 
656 
657 #include "drm_crtc.h"
658 
659 /**
660  * struct drm_master - drm master structure
661  *
662  * @refcount: Refcount for this master object.
663  * @minor: Link back to minor char device we are master for. Immutable.
664  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
665  * @unique_len: Length of unique field. Protected by drm_global_mutex.
666  * @unique_size: Amount allocated. Protected by drm_global_mutex.
667  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
668  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
669  * @lock: DRI lock information.
670  * @driver_priv: Pointer to driver-private information.
671  */
672 struct drm_master {
673 
674 	struct kref refcount; /* refcount for this master */
675 
676 	struct list_head head; /**< each minor contains a list of masters */
677 	struct drm_minor *minor; /**< link back to minor we are a master for */
678 
679 	char *unique;			/**< Unique identifier: e.g., busid */
680 	int unique_len;			/**< Length of unique field */
681 	int unique_size;		/**< amount allocated */
682 
683 	int blocked;			/**< Blocked due to VC switch? */
684 
685 	struct drm_open_hash magiclist;
686 	struct list_head magicfree;
687 	struct drm_lock_data lock;
688 	void *driver_priv;
689 };
690 
691 /* Size of ringbuffer for vblank timestamps. Just double-buffer
692  * in initial implementation.
693  */
694 #define DRM_VBLANKTIME_RBSIZE 2
695 
696 /* Flags and return codes for get_vblank_timestamp() driver function. */
697 #define DRM_CALLED_FROM_VBLIRQ 1
698 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
699 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
700 
701 /* get_scanout_position() return flags */
702 #define DRM_SCANOUTPOS_VALID        (1 << 0)
703 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
704 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
705 
706 #ifndef DMA_BIT_MASK
707 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
708 #endif
709 
710 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
711 
712 /**
713  * DRM driver structure. This structure represent the common code for
714  * a family of cards. There will one drm_device for each card present
715  * in this family
716  */
717 struct drm_driver {
718 	int	(*load)(struct drm_device *, unsigned long flags);
719 	int	(*use_msi)(struct drm_device *, unsigned long flags);
720 	int	(*firstopen)(struct drm_device *);
721 	int	(*open)(struct drm_device *, struct drm_file *);
722 	void	(*preclose)(struct drm_device *, struct drm_file *file_priv);
723 	void	(*postclose)(struct drm_device *, struct drm_file *);
724 	void	(*lastclose)(struct drm_device *);
725 	int	(*unload)(struct drm_device *);
726 	void	(*reclaim_buffers_locked)(struct drm_device *,
727 					  struct drm_file *file_priv);
728 	int	(*dma_ioctl)(struct drm_device *dev, void *data,
729 			     struct drm_file *file_priv);
730 	void	(*dma_ready)(struct drm_device *);
731 	int	(*dma_quiescent)(struct drm_device *);
732 	int	(*dma_flush_block_and_flush)(struct drm_device *, int context,
733 					     enum drm_lock_flags flags);
734 	int	(*dma_flush_unblock)(struct drm_device *, int context,
735 				     enum drm_lock_flags flags);
736 	int	(*context_ctor)(struct drm_device *dev, int context);
737 	int	(*context_dtor)(struct drm_device *dev, int context);
738 	int	(*kernel_context_switch)(struct drm_device *dev, int old,
739 					 int new);
740 	int	(*kernel_context_switch_unlock)(struct drm_device *dev);
741 	void	(*irq_preinstall)(struct drm_device *dev);
742 	int	(*irq_postinstall)(struct drm_device *dev);
743 	void	(*irq_uninstall)(struct drm_device *dev);
744 	void	(*irq_handler)(void *arg);
745 
746 	u32	(*get_vblank_counter)(struct drm_device *dev, int crtc);
747 	int	(*enable_vblank)(struct drm_device *dev, int crtc);
748 	void	(*disable_vblank)(struct drm_device *dev, int crtc);
749 
750 	/**
751 	 * Called by vblank timestamping code.
752 	 *
753 	 * Return the current display scanout position from a crtc, and an
754 	 * optional accurate ktime_get timestamp of when position was measured.
755 	 *
756 	 * \param dev  DRM device.
757 	 * \param crtc Id of the crtc to query.
758 	 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
759 	 * \param *vpos Target location for current vertical scanout position.
760 	 * \param *hpos Target location for current horizontal scanout position.
761 	 * \param *stime Target location for timestamp taken immediately before
762 	 *               scanout position query. Can be NULL to skip timestamp.
763 	 * \param *etime Target location for timestamp taken immediately after
764 	 *               scanout position query. Can be NULL to skip timestamp.
765 	 *
766 	 * Returns vpos as a positive number while in active scanout area.
767 	 * Returns vpos as a negative number inside vblank, counting the number
768 	 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
769 	 * until start of active scanout / end of vblank."
770 	 *
771 	 * \return Flags, or'ed together as follows:
772 	 *
773 	 * DRM_SCANOUTPOS_VALID = Query successful.
774 	 * DRM_SCANOUTPOS_INVBL = Inside vblank.
775 	 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
776 	 * this flag means that returned position may be offset by a constant
777 	 * but unknown small number of scanlines wrt. real scanout position.
778 	 *
779 	 */
780 	int (*get_scanout_position) (struct drm_device *dev, int crtc,
781 				     unsigned int flags,
782 				     int *vpos, int *hpos, ktime_t *stime,
783 				     ktime_t *etime);
784 
785 	int	(*get_vblank_timestamp)(struct drm_device *dev, int crtc,
786 		    int *max_error, struct timeval *vblank_time,
787 		    unsigned flags);
788 
789 	void	(*gem_free_object)(struct drm_gem_object *obj);
790 	int	(*gem_open_object)(struct drm_gem_object *, struct drm_file *);
791 	void	(*gem_close_object)(struct drm_gem_object *, struct drm_file *);
792 
793 	struct cdev_pager_ops *gem_pager_ops;
794 
795 	int	(*dumb_create)(struct drm_file *file_priv,
796 		    struct drm_device *dev, struct drm_mode_create_dumb *args);
797 	int	(*dumb_map_offset)(struct drm_file *file_priv,
798 		    struct drm_device *dev, uint32_t handle, uint64_t *offset);
799 	int	(*dumb_destroy)(struct drm_file *file_priv,
800 		    struct drm_device *dev, uint32_t handle);
801 
802 	int	(*sysctl_init)(struct drm_device *dev,
803 		    struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
804 	void	(*sysctl_cleanup)(struct drm_device *dev);
805 
806 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
807 
808 	/**
809 	 * Called by \c drm_device_is_agp.  Typically used to determine if a
810 	 * card is really attached to AGP or not.
811 	 *
812 	 * \param dev  DRM device handle
813 	 *
814 	 * \returns
815 	 * One of three values is returned depending on whether or not the
816 	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
817 	 * (return of 1), or may or may not be AGP (return of 2).
818 	 */
819 	int	(*device_is_agp) (struct drm_device * dev);
820 
821 	int	major;
822 	int	minor;
823 	int	patchlevel;
824 	const char *name;		/* Simple driver name		   */
825 	const char *desc;		/* Longer driver name		   */
826 	const char *date;		/* Date of last major changes.	   */
827 
828 	u32 driver_features;
829 	int dev_priv_size;
830 	const struct drm_ioctl_desc *ioctls;
831 	int num_ioctls;
832 };
833 
834 /**
835  * Info file list entry. This structure represents a debugfs or proc file to
836  * be created by the drm core
837  */
838 struct drm_info_list {
839 	const char *name; /** file name */
840 	int (*show)(struct seq_file*, void*); /** show callback */
841 	u32 driver_features; /**< Required driver features for this entry */
842 	void *data;
843 };
844 
845 /**
846  * debugfs node structure. This structure represents a debugfs file.
847  */
848 struct drm_info_node {
849 	struct list_head list;
850 	struct drm_minor *minor;
851 	const struct drm_info_list *info_ent;
852 	struct dentry *dent;
853 };
854 
855 /**
856  * DRM minor structure. This structure represents a drm minor number.
857  */
858 struct drm_minor {
859 	int index;			/**< Minor device number */
860 	int type;                       /**< Control or render */
861 	device_t kdev;			/**< OS device */
862 	struct drm_device *dev;
863 
864 	struct drm_master *master; /* currently active master for this node */
865 	struct list_head master_list;
866 	struct drm_mode_group mode_group;
867 };
868 
869 struct drm_pending_vblank_event {
870 	struct drm_pending_event base;
871 	int pipe;
872 	struct drm_event_vblank event;
873 };
874 
875 struct drm_sysctl_info {
876 	struct sysctl_ctx_list ctx;
877 	char   name[2];
878 };
879 
880 /* Length for the array of resource pointers for drm_get_resource_*. */
881 #define DRM_MAX_PCI_RESOURCE	6
882 
883 struct drm_vblank_crtc {
884 	struct drm_device *dev;		/* pointer to the drm_device */
885 	wait_queue_head_t queue;	/**< VBLANK wait queue */
886 	struct timeval time[DRM_VBLANKTIME_RBSIZE];	/**< timestamp of current count */
887 	struct timer_list disable_timer;		/* delayed disable timer */
888 	atomic_t count;			/**< number of VBLANK interrupts */
889 	atomic_t refcount;		/* number of users of vblank interruptsper crtc */
890 	u32 last;			/* protected by dev->vbl_lock, used */
891 					/* for wraparound handling */
892 	u32 last_wait;			/* Last vblank seqno waited per CRTC */
893 	unsigned int inmodeset;		/* Display driver is setting mode */
894 	int crtc;			/* crtc index */
895 	bool enabled;			/* so we don't call enable more than
896 					   once per disable */
897 };
898 
899 /**
900  * DRM device structure. This structure represent a complete card that
901  * may contain multiple heads.
902  */
903 struct drm_device {
904 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
905 
906 	uint16_t pci_subdevice;		/* PCI subsystem device id */
907 	uint16_t pci_subvendor;		/* PCI subsystem vendor id */
908 
909 	char		  *unique;	/* Unique identifier: e.g., busid  */
910 	int		  unique_len;	/* Length of unique field	   */
911 	struct cdev	  *devnode;	/* Device number for mknod	   */
912 	int		  if_version;	/* Highest interface version set */
913 
914 	int		  flags;	/* Flags to open(2)		   */
915 
916 				/* Locks */
917 	struct spinlock	  dma_lock;	/* protects dev->dma */
918 	struct lwkt_serialize irq_lock;	/* protects irq condition checks */
919 	struct lock	  dev_lock;	/* protects everything else */
920 
921 	/** \name Locks */
922 	/*@{ */
923 	struct lock struct_mutex;	/**< For others */
924 	struct lock master_mutex;	/**< For drm_minor::master */
925 	/*@} */
926 
927 	/** \name Usage Counters */
928 	/*@{ */
929 	int open_count;			/**< Outstanding files open, protected by drm_global_mutex. */
930 	struct spinlock buf_lock;	/**< For drm_device::buf_use and a few other things. */
931 	int buf_use;			/**< Buffers in use -- cannot alloc */
932 	atomic_t buf_alloc;		/**< Buffer allocation in progress */
933 	/*@} */
934 
935 
936 	/** \name Performance counters */
937 	/*@{ */
938 	unsigned long     counters;
939 	enum drm_stat_type	types[15];
940 	atomic_t          counts[15];
941 	/*@} */
942 
943 				/* Authentication */
944 	struct drm_open_hash magiclist;	/**< magic hash table */
945 	struct list_head magicfree;
946 
947 	struct list_head filelist;
948 
949 	/** \name Memory management */
950 	/*@{ */
951 	struct list_head maplist;	/**< Linked list of regions */
952 	int map_count;			/**< Number of mappable regions */
953 	struct drm_open_hash map_hash;	/**< User token hash table for maps */
954 
955 	/** \name Context handle management */
956 	/*@{ */
957 	struct list_head ctxlist;	/**< Linked list of context handles */
958 	int ctx_count;			/**< Number of context handles */
959 	struct lock ctxlist_mutex;	/**< For ctxlist */
960 
961 	struct idr ctx_idr;
962 
963 	/*@} */
964 
965 	struct drm_lock_data lock;	/* Information on hardware lock	   */
966 
967 				/* DMA queues (contexts) */
968 	drm_device_dma_t  *dma;		/* Optional pointer for DMA support */
969 
970 	int		  irq;		/* Interrupt used by board	   */
971 	int		  irq_type;	/* IRQ type (MSI enabled or not) */
972 	int		  irqrid;	/* Interrupt used by board */
973 	struct resource   *irqr;	/* Resource for interrupt used by board	   */
974 	void		  *irqh;	/* Handle from bus_setup_intr      */
975 
976 	/* Storage of resource pointers for drm_get_resource_* */
977 	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
978 	int		  pcirid[DRM_MAX_PCI_RESOURCE];
979 
980 	int		  pci_domain;
981 	int		  pci_bus;
982 	int		  pci_slot;
983 	int		  pci_func;
984 
985 	/** \name Context support */
986 	/*@{ */
987 	int irq_enabled;		/**< True if irq handler is enabled */
988 	__volatile__ long context_flag;	/**< Context swapping flag */
989 	__volatile__ long interrupt_flag; /**< Interruption handler flag */
990 	__volatile__ long dma_flag;	/**< DMA dispatch flag */
991 	wait_queue_head_t context_wait;	/**< Processes waiting on ctx switch */
992 	int last_checked;		/**< Last context checked for DMA */
993 	int last_context;		/**< Last current context */
994 	unsigned long last_switch;	/**< jiffies at last context switch */
995 	/*@} */
996 
997 	/** \name VBLANK IRQ support */
998 	/*@{ */
999 
1000 	/*
1001 	 * At load time, disabling the vblank interrupt won't be allowed since
1002 	 * old clients may not call the modeset ioctl and therefore misbehave.
1003 	 * Once the modeset ioctl *has* been called though, we can safely
1004 	 * disable them when unused.
1005 	 */
1006 	bool vblank_disable_allowed;
1007 
1008 	/*
1009 	 * If true, vblank interrupt will be disabled immediately when the
1010 	 * refcount drops to zero, as opposed to via the vblank disable
1011 	 * timer.
1012 	 * This can be set to true it the hardware has a working vblank
1013 	 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
1014 	 * appropriately.
1015 	 */
1016 	bool vblank_disable_immediate;
1017 
1018 	/* array of size num_crtcs */
1019 	struct drm_vblank_crtc *vblank;
1020 
1021 	struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
1022 	struct lock vbl_lock;
1023 	struct timer_list vblank_disable_timer;
1024 
1025 	u32 max_vblank_count;           /**< size of vblank counter register */
1026 
1027 	/**
1028 	 * List of events
1029 	 */
1030 	struct list_head vblank_event_list;
1031 	struct lock event_lock;
1032 
1033 	/*@} */
1034 
1035 	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
1036 
1037 				/* Sysctl support */
1038 	struct drm_sysctl_info *sysctl;
1039 
1040 
1041 	drm_sg_mem_t      *sg;  /* Scatter gather memory */
1042 	unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1043 
1044 	unsigned long     *ctx_bitmap;
1045 	void		  *dev_private;
1046 
1047 	void		  *drm_ttm_bdev;
1048 
1049 	/*@} */
1050 
1051 	struct drm_agp_head *agp;	/**< AGP data */
1052 
1053 	struct device *dev;             /**< Device structure */
1054 	struct pci_dev *pdev;		/**< PCI device structure */
1055 	int pci_vendor;			/**< PCI vendor id */
1056 	int pci_device;			/**< PCI device id */
1057 
1058 	struct drm_driver *driver;
1059 	struct drm_local_map *agp_buffer_map;
1060 	unsigned int agp_buffer_token;
1061 	struct drm_minor *control;		/**< Control node for card */
1062 	struct drm_minor *primary;		/**< render type primary screen head */
1063 
1064         struct drm_mode_config mode_config;	/**< Current mode config */
1065 
1066 	/** \name GEM information */
1067 	/*@{ */
1068 	struct lock object_name_lock;
1069 	struct idr object_name_idr;
1070 	/*@} */
1071 	void             *mm_private;
1072 
1073 	void *sysctl_private;
1074 	char busid_str[128];
1075 	int modesetting;
1076 
1077 	int switch_power_state;
1078 
1079 	atomic_t unplugged; /* device has been unplugged or gone away */
1080 };
1081 
1082 #define DRM_SWITCH_POWER_ON 0
1083 #define DRM_SWITCH_POWER_OFF 1
1084 #define DRM_SWITCH_POWER_CHANGING 2
1085 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1086 
1087 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1088 					     int feature)
1089 {
1090 	return ((dev->driver->driver_features & feature) ? 1 : 0);
1091 }
1092 
1093 static inline void drm_device_set_unplugged(struct drm_device *dev)
1094 {
1095 	smp_wmb();
1096 	atomic_set(&dev->unplugged, 1);
1097 }
1098 
1099 static inline int drm_device_is_unplugged(struct drm_device *dev)
1100 {
1101 	int ret = atomic_read(&dev->unplugged);
1102 	smp_rmb();
1103 	return ret;
1104 }
1105 
1106 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1107 {
1108 	return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1109 }
1110 
1111 /**
1112  * drm_is_master() - Check whether a DRM open-file is DRM-Master
1113  * @file: DRM open-file context
1114  *
1115  * This checks whether a DRM open-file context is owner of the master context
1116  * attached to it. If a file owns a master context, it's called DRM-Master.
1117  * Per DRM device, only one such file can be DRM-Master at a time.
1118  *
1119  * Returns: True if the file is DRM-Master, otherwise false.
1120  */
1121 static inline bool drm_is_master(const struct drm_file *file)
1122 {
1123 #if 0
1124 	return file->master && file->master == file->minor->master;
1125 #else
1126 	return true;
1127 #endif
1128 }
1129 
1130 /******************************************************************/
1131 /** \name Internal function definitions */
1132 /*@{*/
1133 
1134 #if __OS_HAS_AGP
1135 static inline int drm_core_has_AGP(struct drm_device *dev)
1136 {
1137 	return drm_core_check_feature(dev, DRIVER_USE_AGP);
1138 }
1139 #else
1140 #define drm_core_has_AGP(dev) (0)
1141 #endif
1142 
1143 extern int	drm_notyet_flag;
1144 extern unsigned int drm_rnodes;
1145 extern unsigned int drm_universal_planes;
1146 
1147 extern int drm_vblank_offdelay;
1148 extern unsigned int drm_timestamp_precision;
1149 extern unsigned int drm_timestamp_monotonic;
1150 
1151 				/* Driver support (drm_drv.h) */
1152 int	drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1153 int	drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1154 int	drm_create_cdevs(device_t kdev);
1155 d_ioctl_t drm_ioctl;
1156 extern int drm_lastclose(struct drm_device *dev);
1157 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1158 
1159 				/* Device support (drm_fops.h) */
1160 extern struct lock drm_global_mutex;
1161 d_open_t drm_open;
1162 d_close_t drm_close;
1163 d_read_t drm_read;
1164 d_kqfilter_t drm_kqfilter;
1165 int drm_release(device_t kdev);
1166 
1167 d_mmap_t drm_mmap;
1168 d_mmap_single_t drm_mmap_single;
1169 
1170 void drm_cdevpriv_dtor(void *cd);
1171 
1172 int drm_add_busid_modesetting(struct drm_device *dev,
1173     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1174 
1175 /* File operations helpers (drm_fops.c) */
1176 extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
1177 					 DRM_STRUCTPROC *p,
1178 					struct drm_device *dev,
1179 					struct file *fp);
1180 extern struct drm_file	*drm_find_file_by_proc(struct drm_device *dev,
1181 					DRM_STRUCTPROC *p);
1182 
1183 #ifdef DUMBBELL_WIP
1184 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1185 		struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1186 		int *prime_fd);
1187 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1188 		struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1189 
1190 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1191 					struct drm_file *file_priv);
1192 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1193 					struct drm_file *file_priv);
1194 
1195 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1196 					    dma_addr_t *addrs, int max_pages);
1197 #endif /* DUMBBELL_WIP */
1198 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1199 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1200 
1201 #ifdef DUMBBELL_WIP
1202 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1203 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1204 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1205 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1206 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1207 
1208 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1209 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1210 			 struct drm_gem_object **obj);
1211 #endif /* DUMBBELL_WIP */
1212 
1213 void	drm_mem_init(void);
1214 void	drm_mem_uninit(void);
1215 void	*drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map);
1216 void	*drm_ioremap(struct drm_device *dev, drm_local_map_t *map);
1217 void	drm_ioremapfree(drm_local_map_t *map);
1218 int	drm_mtrr_add(unsigned long offset, size_t size, int flags);
1219 int	drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1220 
1221 /* Locking IOCTL support (drm_lock.c) */
1222 int	drm_lock_take(struct drm_lock_data *lock_data,
1223 		      unsigned int context);
1224 int	drm_lock_transfer(struct drm_lock_data *lock_data,
1225 			  unsigned int context);
1226 int	drm_lock_free(struct drm_lock_data *lock_data,
1227 		      unsigned int context);
1228 
1229 /*
1230  * These are exported to drivers so that they can implement fencing using
1231  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1232  */
1233 
1234 unsigned long drm_get_resource_start(struct drm_device *dev,
1235 				     unsigned int resource);
1236 unsigned long drm_get_resource_len(struct drm_device *dev,
1237 				   unsigned int resource);
1238 
1239 				/* IRQ support (drm_irq.h) */
1240 extern int drm_irq_install(struct drm_device *dev, int irq);
1241 int	drm_irq_uninstall(struct drm_device *dev);
1242 void	drm_driver_irq_preinstall(struct drm_device *dev);
1243 void	drm_driver_irq_postinstall(struct drm_device *dev);
1244 void	drm_driver_irq_uninstall(struct drm_device *dev);
1245 
1246 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1247 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1248 			   struct drm_file *filp);
1249 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1250 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1251 				     struct timeval *vblanktime);
1252 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1253 				     struct drm_pending_vblank_event *e);
1254 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1255 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1256 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1257 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1258 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1259 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
1260 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1261 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1262 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1263 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1264 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1265 extern void drm_vblank_cleanup(struct drm_device *dev);
1266 
1267 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1268 						 int crtc, int *max_error,
1269 						 struct timeval *vblank_time,
1270 						 unsigned flags,
1271 						 const struct drm_crtc *refcrtc,
1272 						 const struct drm_display_mode *mode);
1273 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1274 					    const struct drm_display_mode *mode);
1275 
1276 /**
1277  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1278  * @crtc: which CRTC's vblank waitqueue to retrieve
1279  *
1280  * This function returns a pointer to the vblank waitqueue for the CRTC.
1281  * Drivers can use this to implement vblank waits using wait_event() & co.
1282  */
1283 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1284 {
1285        return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1286 }
1287 
1288 /* Modesetting support */
1289 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1290 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1291 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1292 			   struct drm_file *file_priv);
1293 
1294 				/* AGP/GART support (drm_agpsupport.h) */
1295 
1296 #include <drm/drm_agpsupport.h>
1297 
1298 /* sysctl support (drm_sysctl.h) */
1299 extern int		drm_sysctl_init(struct drm_device *dev);
1300 extern int		drm_sysctl_cleanup(struct drm_device *dev);
1301 
1302 /* ATI PCIGART support (ati_pcigart.c) */
1303 int	drm_ati_pcigart_init(struct drm_device *dev,
1304 				struct drm_ati_pcigart_info *gart_info);
1305 int	drm_ati_pcigart_cleanup(struct drm_device *dev,
1306 				struct drm_ati_pcigart_info *gart_info);
1307 
1308 /* Cache management (drm_memory.c) */
1309 void	drm_clflush_pages(vm_page_t *pages, unsigned long num_pages);
1310 
1311 /* Locking IOCTL support (drm_drv.c) */
1312 int	drm_lock(struct drm_device *dev, void *data,
1313 		 struct drm_file *file_priv);
1314 int	drm_unlock(struct drm_device *dev, void *data,
1315 		   struct drm_file *file_priv);
1316 int	drm_setversion(struct drm_device *dev, void *data,
1317 		       struct drm_file *file_priv);
1318 
1319 /*
1320  * These are exported to drivers so that they can implement fencing using
1321  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1322  */
1323 
1324 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1325 
1326 /* Misc. IOCTL support (drm_ioctl.c) */
1327 int	drm_irq_by_busid(struct drm_device *dev, void *data,
1328 			 struct drm_file *file_priv);
1329 int	drm_getunique(struct drm_device *dev, void *data,
1330 		      struct drm_file *file_priv);
1331 int	drm_setunique(struct drm_device *dev, void *data,
1332 		      struct drm_file *file_priv);
1333 int	drm_getmap(struct drm_device *dev, void *data,
1334 		   struct drm_file *file_priv);
1335 int	drm_getclient(struct drm_device *dev, void *data,
1336 		      struct drm_file *file_priv);
1337 int	drm_getstats(struct drm_device *dev, void *data,
1338 		     struct drm_file *file_priv);
1339 int	drm_getcap(struct drm_device *dev, void *data,
1340 		     struct drm_file *file_priv);
1341 extern int drm_setclientcap(struct drm_device *dev, void *data,
1342 			    struct drm_file *file_priv);
1343 int	drm_noop(struct drm_device *dev, void *data,
1344 		 struct drm_file *file_priv);
1345 
1346 /* Authentication IOCTL support (drm_auth.c) */
1347 int	drm_getmagic(struct drm_device *dev, void *data,
1348 		     struct drm_file *file_priv);
1349 int	drm_authmagic(struct drm_device *dev, void *data,
1350 		      struct drm_file *file_priv);
1351 
1352 /* Cache management (drm_cache.c) */
1353 void drm_clflush_virt_range(void *addr, unsigned long length);
1354 
1355 /* DMA support (drm_dma.c) */
1356 int	drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1357 
1358 /* IRQ support (drm_irq.c) */
1359 int	drm_control(struct drm_device *dev, void *data,
1360 		    struct drm_file *file_priv);
1361 
1362 				/* Stub support (drm_stub.h) */
1363 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1364 			       struct drm_file *file_priv);
1365 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1366 				struct drm_file *file_priv);
1367 
1368 extern void drm_put_dev(struct drm_device *dev);
1369 extern void drm_unplug_dev(struct drm_device *dev);
1370 extern unsigned int drm_debug;
1371 
1372 /* Scatter Gather Support (drm_scatter.c) */
1373 int	drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1374 			   struct drm_file *file_priv);
1375 int	drm_sg_free(struct drm_device *dev, void *data,
1376 		    struct drm_file *file_priv);
1377 
1378 /* consistent PCI memory functions (drm_pci.c) */
1379 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1380 				       size_t align);
1381 void	drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1382 
1383 			       /* sysfs support (drm_sysfs.c) */
1384 struct drm_sysfs_class;
1385 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1386 extern void drm_sysfs_destroy(void);
1387 extern int drm_sysfs_device_add(struct drm_minor *minor);
1388 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1389 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1390 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1391 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1392 
1393 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1394 			struct drm_file *file_priv);
1395 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1396 			struct drm_file *file_priv);
1397 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1398 		       struct drm_file *file_priv);
1399 void drm_gem_open(struct drm_device *dev, struct drm_file *file_priv);
1400 void drm_gem_release(struct drm_device *dev, struct drm_file *file_priv);
1401 
1402 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1403     vm_size_t size, struct vm_object **obj_res, int nprot);
1404 void drm_gem_pager_dtr(void *obj);
1405 
1406 struct ttm_bo_device;
1407 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1408     vm_size_t size, struct vm_object **obj_res, int nprot);
1409 struct ttm_buffer_object;
1410 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1411 
1412 void drm_device_lock_mtx(struct drm_device *dev);
1413 void drm_device_unlock_mtx(struct drm_device *dev);
1414 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1415     const char *msg, int timeout);
1416 void drm_device_assert_mtx_locked(struct drm_device *dev);
1417 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1418 
1419 void drm_device_lock_struct(struct drm_device *dev);
1420 void drm_device_unlock_struct(struct drm_device *dev);
1421 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1422     const char *msg, int timeout);
1423 void drm_device_assert_struct_locked(struct drm_device *dev);
1424 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1425 
1426 void drm_compat_locking_init(struct drm_device *dev);
1427 void drm_sleep_locking_init(struct drm_device *dev);
1428 
1429 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1430 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1431 void drm_fini_pdev(struct pci_dev **pdev);
1432 
1433 /* Inline replacements for drm_alloc and friends */
1434 static __inline__ void *
1435 drm_alloc(size_t size, struct malloc_type *area)
1436 {
1437 	return kmalloc(size, area, M_WAITOK | M_NULLOK);
1438 }
1439 
1440 static __inline__ void *
1441 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1442 {
1443 	return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1444 }
1445 
1446 static __inline__ void
1447 drm_free(void *pt, struct malloc_type *area)
1448 {
1449 	kfree(pt);
1450 }
1451 
1452 static __inline__ void
1453 drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev)
1454 {
1455 	map->handle = drm_ioremap_wc(dev, map);
1456 }
1457 static __inline__ void
1458 drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev)
1459 {
1460 	map->handle = drm_ioremap(dev, map);
1461 }
1462 static __inline__ void
1463 drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev)
1464 {
1465 	if ( map->handle && map->size )
1466 		drm_ioremapfree(map);
1467 }
1468 
1469 static __inline__ struct drm_local_map *
1470 drm_core_findmap(struct drm_device *dev, unsigned long offset)
1471 {
1472 	struct drm_map_list *_entry;
1473 
1474 	list_for_each_entry(_entry, &dev->maplist, head) {
1475 		if (offset == (unsigned long)_entry->map->handle)
1476 			return _entry->map;
1477 	}
1478 	return NULL;
1479 }
1480 
1481 static __inline__ void drm_core_dropmap(struct drm_map *map)
1482 {
1483 }
1484 
1485 #include <drm/drm_mem_util.h>
1486 
1487 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1488 				 struct device *parent);
1489 void drm_dev_ref(struct drm_device *dev);
1490 void drm_dev_unref(struct drm_device *dev);
1491 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1492 void drm_dev_unregister(struct drm_device *dev);
1493 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1494 
1495 /* FreeBSD compatibility macros */
1496 #define VM_OBJECT_WLOCK(object)		VM_OBJECT_LOCK(object)
1497 #define VM_OBJECT_WUNLOCK(object)	VM_OBJECT_UNLOCK(object)
1498 
1499 #define DRM_PCIE_SPEED_25 1
1500 #define DRM_PCIE_SPEED_50 2
1501 #define DRM_PCIE_SPEED_80 4
1502 
1503 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1504 
1505 /* XXX bad */
1506 #define	drm_can_sleep()	(HZ & 1)
1507 
1508 #endif /* __KERNEL__ */
1509 #endif /* _DRM_P_H_ */
1510