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