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