xref: /dragonfly/sys/dev/drm/include/drm/drmP.h (revision 68cca79d)
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 <linux/agp_backend.h>
38 #include <linux/cdev.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/file.h>
41 #include <linux/fs.h>
42 #include <linux/highmem.h>
43 #include <linux/idr.h>
44 #include <linux/init.h>
45 #include <linux/io.h>
46 #include <linux/jiffies.h>
47 #include <linux/kernel.h>
48 #include <linux/kref.h>
49 #include <linux/miscdevice.h>
50 #include <linux/mm.h>
51 #include <linux/mutex.h>
52 #include <linux/pci.h>
53 #include <linux/platform_device.h>
54 #include <linux/poll.h>
55 #include <linux/ratelimit.h>
56 #include <linux/sched.h>
57 #include <linux/slab.h>
58 #include <linux/seq_file.h>
59 #include <linux/types.h>
60 #include <linux/vmalloc.h>
61 #include <linux/workqueue.h>
62 
63 #include <asm/pgalloc.h>
64 #include <asm/uaccess.h>
65 
66 #include <uapi/drm/drm.h>
67 #include <uapi/drm/drm_mode.h>
68 
69 #include <drm/drm_agpsupport.h>
70 #include <drm/drm_crtc.h>
71 #include <drm/drm_global.h>
72 #include <drm/drm_hashtab.h>
73 #include <drm/drm_mem_util.h>
74 #include <drm/drm_mm.h>
75 #include <drm/drm_os_linux.h>
76 #include <drm/drm_sarea.h>
77 #include <drm/drm_vma_manager.h>
78 
79 #include <sys/device.h>
80 
81 #include <vm/vm_extern.h>
82 #include <vm/vm_pager.h>
83 
84 struct module;
85 
86 struct drm_file;
87 struct drm_device;
88 struct drm_agp_head;
89 struct drm_local_map;
90 struct drm_device_dma;
91 struct drm_dma_handle;
92 struct drm_gem_object;
93 
94 struct device_node;
95 struct videomode;
96 struct reservation_object;
97 struct dma_buf_attachment;
98 
99 /*
100  * The following categories are defined:
101  *
102  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
103  *	 This is the category used by the DRM_DEBUG() macro.
104  *
105  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
106  *	   This is the category used by the DRM_DEBUG_DRIVER() macro.
107  *
108  * KMS: used in the modesetting code.
109  *	This is the category used by the DRM_DEBUG_KMS() macro.
110  *
111  * PRIME: used in the prime code.
112  *	  This is the category used by the DRM_DEBUG_PRIME() macro.
113  *
114  * ATOMIC: used in the atomic code.
115  *	  This is the category used by the DRM_DEBUG_ATOMIC() macro.
116  *
117  * PID: used as modifier to include PID number in messages.
118  *	  This is the category used by the all debug macros.
119  *
120  * FIOCTL: used in failed ioctl debugging.
121  *	  This is the category used by the DRM_DEBUG_FIOCTL() macro.
122  *
123  * IOCTL: used in ioctl debugging.
124  *	  This is the category used by the DRM_DEBUG_IOCTL() macro.
125  *
126  * VBLANK: used in vblank debugging.
127  *	  This is the category used by the DRM_DEBUG_VBLANK() macro.
128  *
129  * VBL: used for verbose debug message in the vblank code
130  *	  This is the category used by the DRM_DEBUG_VBL() macro.
131  *
132  * Enabling verbose debug messages is done through the drm.debug parameter,
133  * each category being enabled by a bit.
134  *
135  * drm.debug=0x1 will enable CORE messages
136  * drm.debug=0x2 will enable DRIVER messages
137  * drm.debug=0x3 will enable CORE and DRIVER messages
138  * ...
139  * drm.debug=0x3f will enable all messages
140  *
141  * An interesting feature is that it's possible to enable verbose logging at
142  * run-time by using hw.drm.debug sysctl variable:
143  *   # sysctl hw.drm.debug=0xfff
144  */
145 #define DRM_UT_CORE 		0x01
146 #define DRM_UT_DRIVER		0x02
147 #define DRM_UT_KMS		0x04
148 #define DRM_UT_PRIME		0x08
149 #define DRM_UT_ATOMIC		0x10
150 #define DRM_UT_VBL		0x20
151 /* Extra DragonFly debug categories */
152 #ifdef __DragonFly__
153 #define DRM_UT_RES7		0x40	/* reserved for future expansion */
154 #define DRM_UT_RES8		0x80	/* reserved for future expansion */
155 #define DRM_UT_PID		0x100
156 #define DRM_UT_FIOCTL		0x200
157 #define DRM_UT_IOCTL		0x400
158 #define DRM_UT_VBLANK		0x800
159 #endif
160 
161 extern __printflike(2, 3)
162 void drm_ut_debug_printk(const char *function_name,
163 			 const char *format, ...);
164 extern __printflike(2, 3)
165 void drm_err(const char *func, const char *format, ...);
166 
167 /***********************************************************************/
168 /** \name DRM template customization defaults */
169 /*@{*/
170 
171 /* driver capabilities and requirements mask */
172 #define DRIVER_USE_AGP			0x1
173 #define DRIVER_PCI_DMA			0x8
174 #define DRIVER_SG			0x10
175 #define DRIVER_HAVE_DMA			0x20
176 #define DRIVER_HAVE_IRQ			0x40
177 #define DRIVER_IRQ_SHARED		0x80
178 #define DRIVER_GEM			0x1000
179 #define DRIVER_MODESET			0x2000
180 #define DRIVER_PRIME			0x4000
181 #define DRIVER_RENDER			0x8000
182 #define DRIVER_ATOMIC			0x10000
183 #define DRIVER_KMS_LEGACY_CONTEXT	0x20000
184 
185 /***********************************************************************/
186 /** \name Macros to make printk easier */
187 /*@{*/
188 
189 #define _DRM_PRINTK(once, level, fmt, ...)				\
190 	do {								\
191 		printk##once(KERN_##level "[" DRM_NAME "] " fmt,	\
192 			     ##__VA_ARGS__);				\
193 	} while (0)
194 
195 #define DRM_INFO(fmt, ...)						\
196 	_DRM_PRINTK(, INFO, fmt, ##__VA_ARGS__)
197 #define DRM_NOTE(fmt, ...)						\
198 	_DRM_PRINTK(, NOTICE, fmt, ##__VA_ARGS__)
199 #define DRM_WARN(fmt, ...)						\
200 	_DRM_PRINTK(, WARNING, fmt, ##__VA_ARGS__)
201 
202 #define DRM_INFO_ONCE(fmt, ...)						\
203 	_DRM_PRINTK(_once, INFO, fmt, ##__VA_ARGS__)
204 #define DRM_NOTE_ONCE(fmt, ...)						\
205 	_DRM_PRINTK(_once, NOTICE, fmt, ##__VA_ARGS__)
206 #define DRM_WARN_ONCE(fmt, ...)						\
207 	_DRM_PRINTK(_once, WARNING, fmt, ##__VA_ARGS__)
208 
209 /**
210  * Error output.
211  *
212  * \param fmt printf() like format string.
213  * \param arg arguments
214  */
215 #define DRM_ERROR(fmt, ...)				\
216 	drm_err(__func__, fmt, ##__VA_ARGS__)
217 
218 /**
219  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
220  *
221  * \param fmt printf() like format string.
222  * \param arg arguments
223  */
224 #define DRM_ERROR_RATELIMITED(fmt, ...)				\
225 ({									\
226 	static struct krate krate_derr = { .freq = 1, .count = -16 };	\
227 									\
228 	krateprintf(&krate_derr, "error: [" DRM_NAME ":%s] *ERROR* "	\
229 	    fmt, __func__, ##__VA_ARGS__);				\
230 })
231 
232 /**
233  * Debug output.
234  *
235  * \param fmt printf() like format string.
236  * \param arg arguments
237  */
238 #define DRM_DEBUG(fmt, args...)						\
239 	do {								\
240 		if (unlikely(drm_debug & DRM_UT_CORE))			\
241 			drm_ut_debug_printk(__func__, fmt, ##args);	\
242 	} while (0)
243 
244 #define DRM_DEBUG_DRIVER(fmt, args...)					\
245 	do {								\
246 		if (unlikely(drm_debug & DRM_UT_DRIVER))		\
247 			drm_ut_debug_printk(__func__, fmt, ##args);	\
248 	} while (0)
249 #define DRM_DEBUG_KMS(fmt, args...)					\
250 	do {								\
251 		if (unlikely(drm_debug & DRM_UT_KMS))			\
252 			drm_ut_debug_printk(__func__, fmt, ##args);	\
253 	} while (0)
254 #define DRM_DEBUG_PRIME(fmt, args...)					\
255 	do {								\
256 		if (unlikely(drm_debug & DRM_UT_PRIME))			\
257 			drm_ut_debug_printk(__func__, fmt, ##args);	\
258 	} while (0)
259 #define DRM_DEBUG_ATOMIC(fmt, args...)					\
260 	do {								\
261 		if (unlikely(drm_debug & DRM_UT_ATOMIC))		\
262 			drm_ut_debug_printk(__func__, fmt, ##args);	\
263 	} while (0)
264 #define DRM_DEBUG_VBL(fmt, args...)					\
265 	do {								\
266 		if (unlikely(drm_debug & DRM_UT_VBL))			\
267 			drm_ut_debug_printk(__func__, fmt, ##args);	\
268 	} while (0)
269 #ifdef __DragonFly__
270 #define DRM_DEBUG_FIOCTL(fmt, args...)					\
271 	do {								\
272 		if (unlikely(drm_debug & DRM_UT_FIOCTL))		\
273 			drm_ut_debug_printk(__func__, fmt, ##args);	\
274 	} while (0)
275 #define DRM_DEBUG_IOCTL(fmt, args...)					\
276 	do {								\
277 		if (unlikely(drm_debug & DRM_UT_IOCTL))			\
278 			drm_ut_debug_printk(__func__, fmt, ##args);	\
279 	} while (0)
280 #define DRM_DEBUG_VBLANK(fmt, args...)					\
281 	do {								\
282 		if (unlikely(drm_debug & DRM_UT_VBLANK))		\
283 			drm_ut_debug_printk(__func__, fmt, ##args);	\
284 	} while (0)
285 #endif
286 
287 /*@}*/
288 
289 /***********************************************************************/
290 /** \name Internal types and structures */
291 /*@{*/
292 
293 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
294 
295 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
296 
297 #define DRM_DEV_MODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
298 #define DRM_DEV_UID	UID_ROOT
299 #define DRM_DEV_GID	GID_WHEEL
300 
301 #define DRM_CURPROC		curthread
302 #define DRM_STRUCTPROC		struct thread
303 #define DRM_LOCK(dev)		lockmgr(&(dev)->struct_mutex, LK_EXCLUSIVE)
304 #define DRM_UNLOCK(dev)		lockmgr(&(dev)->struct_mutex, LK_RELEASE)
305 
306 #if defined(INVARIANTS)
307 #define	DRM_LOCK_ASSERT(dev)	KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) != 0);
308 #define	DRM_UNLOCK_ASSERT(dev)	KKASSERT(lockstatus(&(dev)->struct_mutex, curthread) == 0);
309 #else
310 #define	DRM_LOCK_ASSERT(d)
311 #define	DRM_UNLOCK_ASSERT(d)
312 #endif
313 
314 #define DRM_SYSCTL_HANDLER_ARGS	(SYSCTL_HANDLER_ARGS)
315 
316 #define drm_get_device_from_kdev(_kdev) (_kdev->si_drv1)
317 
318 int vm_phys_fictitious_reg_range(vm_paddr_t start, vm_paddr_t end,
319     vm_memattr_t memattr);
320 void vm_phys_fictitious_unreg_range(vm_paddr_t start, vm_paddr_t end);
321 vm_page_t vm_phys_fictitious_to_vm_page(vm_paddr_t pa);
322 
323 typedef struct drm_pci_id_list
324 {
325 	int vendor;
326 	int device;
327 	long driver_private;
328 	char *name;
329 } drm_pci_id_list_t;
330 
331 /**
332  * Ioctl function type.
333  *
334  * \param inode device inode.
335  * \param file_priv DRM file private pointer.
336  * \param cmd command.
337  * \param arg argument.
338  */
339 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
340 			struct drm_file *file_priv);
341 
342 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
343 			       unsigned long arg);
344 
345 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
346 
347 #define DRM_AUTH	0x1
348 #define DRM_MASTER	0x2
349 #define DRM_ROOT_ONLY	0x4
350 #define DRM_CONTROL_ALLOW 0x8
351 #define DRM_UNLOCKED	0x10
352 #define DRM_RENDER_ALLOW 0x20
353 
354 struct drm_ioctl_desc {
355 	unsigned int cmd;
356 	int flags;
357 	drm_ioctl_t *func;
358 	unsigned int cmd_drv;
359 	const char *name;
360 };
361 
362 /**
363  * Creates a driver or general drm_ioctl_desc array entry for the given
364  * ioctl, for use by drm_ioctl().
365  */
366 
367 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)				\
368 	[DRM_IOCTL_NR(DRM_##ioctl)] = {					\
369 		.cmd = DRM_##ioctl,					\
370 		.func = _func,						\
371 		.flags = _flags,					\
372 		.cmd_drv = DRM_IOCTL_##ioctl,				\
373 		.name = #ioctl						\
374 	 }
375 
376 /* Event queued up for userspace to read */
377 struct drm_pending_event {
378 	struct drm_event *event;
379 	struct list_head link;
380 	struct list_head pending_link;
381 	struct drm_file *file_priv;
382 	pid_t pid; /* pid of requester, no guarantee it's valid by the time
383 		      we deliver the event, for tracing only */
384 	void (*destroy)(struct drm_pending_event *event);
385 };
386 
387 /* initial implementaton using a linked list - todo hashtab */
388 struct drm_prime_file_private {
389 	struct list_head head;
390 	struct lock lock;
391 };
392 
393 /** File private data */
394 struct drm_file {
395 	unsigned authenticated :1;
396 	/* Whether we're master for a minor. Protected by master_mutex */
397 	unsigned is_master :1;
398 	/* true when the client has asked us to expose stereo 3D mode flags */
399 	unsigned stereo_allowed :1;
400 	/*
401 	 * true if client understands CRTC primary planes and cursor planes
402 	 * in the plane list
403 	 */
404 	unsigned universal_planes:1;
405 	/* true if client understands atomic properties */
406 	unsigned atomic:1;
407 	/*
408 	 * This client is allowed to gain master privileges for @master.
409 	 * Protected by struct drm_device::master_mutex.
410 	 */
411 	unsigned allowed_master:1;
412 
413 	pid_t		  pid;
414 	uid_t		  uid;
415 	drm_magic_t magic;
416 	struct list_head lhead;
417 	struct drm_minor *minor;
418 	unsigned long lock_count;
419 
420 	/** Mapping of mm object handles to object pointers. */
421 	struct idr object_idr;
422 	/** Lock for synchronization of access to object_idr. */
423 	struct lock table_lock;
424 
425 	struct file *filp;
426 	void *driver_priv;
427 
428 	struct drm_master *masterp;
429 	/**
430 	 * fbs - List of framebuffers associated with this file.
431 	 *
432 	 * Protected by fbs_lock. Note that the fbs list holds a reference on
433 	 * the fb object to prevent it from untimely disappearing.
434 	 */
435 	struct list_head fbs;
436 	struct lock fbs_lock;
437 
438 	/** User-created blob properties; this retains a reference on the
439 	 *  property. */
440 	struct list_head blobs;
441 
442 	wait_queue_head_t event_wait;
443 	struct list_head pending_event_list;
444 	struct list_head event_list;
445 	int event_space;
446 
447 	struct lock event_read_lock;
448 
449 	struct drm_prime_file_private prime;
450 
451 #ifdef __DragonFly__
452 	struct drm_device *dev;
453 	int		  master;
454 	unsigned long ioctl_count;
455 	struct kqinfo	  dkq;
456 #endif
457 };
458 
459 /**
460  * Lock data.
461  */
462 struct drm_lock_data {
463 	struct drm_hw_lock *hw_lock;	/**< Hardware lock */
464 	/** Private of lock holder's file (NULL=kernel) */
465 	struct drm_file *file_priv;
466 	wait_queue_head_t lock_queue;	/**< Queue of blocked processes */
467 	unsigned long lock_time;	/**< Time of last lock in jiffies */
468 	struct spinlock spinlock;
469 	uint32_t kernel_waiters;
470 	uint32_t user_waiters;
471 	int idle_has_lock;
472 };
473 
474 /**
475  * GEM specific mm private for tracking GEM objects
476  */
477 struct drm_gem_mm {
478 	struct drm_vma_offset_manager vma_manager;
479 	struct drm_mm offset_manager;	/**< Offset mgmt for buffer objects */
480 	struct drm_open_hash offset_hash; /**< User token hash table for maps */
481 	struct unrhdr *idxunr;
482 };
483 
484 /**
485  * struct drm_master - drm master structure
486  *
487  * @refcount: Refcount for this master object.
488  * @minor: Link back to minor char device we are master for. Immutable.
489  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
490  * @unique_len: Length of unique field. Protected by drm_global_mutex.
491  * @magic_map: Map of used authentication tokens. Protected by struct_mutex.
492  * @lock: DRI lock information.
493  * @driver_priv: Pointer to driver-private information.
494  */
495 struct drm_master {
496 	struct kref refcount;
497 	struct drm_minor *minor;
498 	char *unique;
499 	int unique_len;
500 	struct idr magic_map;
501 	struct drm_lock_data lock;
502 	void *driver_priv;
503 };
504 
505 /* Size of ringbuffer for vblank timestamps. Just double-buffer
506  * in initial implementation.
507  */
508 #define DRM_VBLANKTIME_RBSIZE 2
509 
510 /* Flags and return codes for get_vblank_timestamp() driver function. */
511 #define DRM_CALLED_FROM_VBLIRQ 1
512 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
513 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
514 
515 /* get_scanout_position() return flags */
516 #define DRM_SCANOUTPOS_VALID        (1 << 0)
517 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
518 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
519 
520 /**
521  * DRM driver structure. This structure represent the common code for
522  * a family of cards. There will one drm_device for each card present
523  * in this family
524  */
525 struct drm_driver {
526 	int (*load) (struct drm_device *, unsigned long flags);
527 	int (*firstopen) (struct drm_device *);
528 	int (*open) (struct drm_device *, struct drm_file *);
529 	void (*preclose) (struct drm_device *, struct drm_file *file_priv);
530 	void (*postclose) (struct drm_device *, struct drm_file *);
531 	void (*lastclose) (struct drm_device *);
532 	int (*unload) (struct drm_device *);
533 	int (*suspend) (struct drm_device *, pm_message_t state);
534 	int (*resume) (struct drm_device *);
535 	int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
536 	int (*dma_quiescent) (struct drm_device *);
537 	int (*context_dtor) (struct drm_device *dev, int context);
538 	int (*set_busid)(struct drm_device *dev, struct drm_master *master);
539 
540 	/**
541 	 * get_vblank_counter - get raw hardware vblank counter
542 	 * @dev: DRM device
543 	 * @pipe: counter to fetch
544 	 *
545 	 * Driver callback for fetching a raw hardware vblank counter for @crtc.
546 	 * If a device doesn't have a hardware counter, the driver can simply
547 	 * return the value of drm_vblank_count. The DRM core will account for
548 	 * missed vblank events while interrupts where disabled based on system
549 	 * timestamps.
550 	 *
551 	 * Wraparound handling and loss of events due to modesetting is dealt
552 	 * with in the DRM core code.
553 	 *
554 	 * RETURNS
555 	 * Raw vblank counter value.
556 	 */
557 	u32 (*get_vblank_counter) (struct drm_device *dev, unsigned int pipe);
558 
559 	/**
560 	 * enable_vblank - enable vblank interrupt events
561 	 * @dev: DRM device
562 	 * @pipe: which irq to enable
563 	 *
564 	 * Enable vblank interrupts for @crtc.  If the device doesn't have
565 	 * a hardware vblank counter, this routine should be a no-op, since
566 	 * interrupts will have to stay on to keep the count accurate.
567 	 *
568 	 * RETURNS
569 	 * Zero on success, appropriate errno if the given @crtc's vblank
570 	 * interrupt cannot be enabled.
571 	 */
572 	int (*enable_vblank) (struct drm_device *dev, unsigned int pipe);
573 
574 	/**
575 	 * disable_vblank - disable vblank interrupt events
576 	 * @dev: DRM device
577 	 * @pipe: which irq to enable
578 	 *
579 	 * Disable vblank interrupts for @crtc.  If the device doesn't have
580 	 * a hardware vblank counter, this routine should be a no-op, since
581 	 * interrupts will have to stay on to keep the count accurate.
582 	 */
583 	void (*disable_vblank) (struct drm_device *dev, unsigned int pipe);
584 
585 	/**
586 	 * Called by \c drm_device_is_agp.  Typically used to determine if a
587 	 * card is really attached to AGP or not.
588 	 *
589 	 * \param dev  DRM device handle
590 	 *
591 	 * \returns
592 	 * One of three values is returned depending on whether or not the
593 	 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
594 	 * (return of 1), or may or may not be AGP (return of 2).
595 	 */
596 	int (*device_is_agp) (struct drm_device *dev);
597 
598 	/**
599 	 * Called by vblank timestamping code.
600 	 *
601 	 * Return the current display scanout position from a crtc, and an
602 	 * optional accurate ktime_get timestamp of when position was measured.
603 	 *
604 	 * \param dev  DRM device.
605 	 * \param pipe Id of the crtc to query.
606 	 * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
607 	 * \param *vpos Target location for current vertical scanout position.
608 	 * \param *hpos Target location for current horizontal scanout position.
609 	 * \param *stime Target location for timestamp taken immediately before
610 	 *               scanout position query. Can be NULL to skip timestamp.
611 	 * \param *etime Target location for timestamp taken immediately after
612 	 *               scanout position query. Can be NULL to skip timestamp.
613 	 * \param mode Current display timings.
614 	 *
615 	 * Returns vpos as a positive number while in active scanout area.
616 	 * Returns vpos as a negative number inside vblank, counting the number
617 	 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
618 	 * until start of active scanout / end of vblank."
619 	 *
620 	 * \return Flags, or'ed together as follows:
621 	 *
622 	 * DRM_SCANOUTPOS_VALID = Query successful.
623 	 * DRM_SCANOUTPOS_INVBL = Inside vblank.
624 	 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
625 	 * this flag means that returned position may be offset by a constant
626 	 * but unknown small number of scanlines wrt. real scanout position.
627 	 *
628 	 */
629 	int (*get_scanout_position) (struct drm_device *dev, unsigned int pipe,
630 				     unsigned int flags, int *vpos, int *hpos,
631 				     ktime_t *stime, ktime_t *etime,
632 				     const struct drm_display_mode *mode);
633 
634 	/**
635 	 * Called by \c drm_get_last_vbltimestamp. Should return a precise
636 	 * timestamp when the most recent VBLANK interval ended or will end.
637 	 *
638 	 * Specifically, the timestamp in @vblank_time should correspond as
639 	 * closely as possible to the time when the first video scanline of
640 	 * the video frame after the end of VBLANK will start scanning out,
641 	 * the time immediately after end of the VBLANK interval. If the
642 	 * @crtc is currently inside VBLANK, this will be a time in the future.
643 	 * If the @crtc is currently scanning out a frame, this will be the
644 	 * past start time of the current scanout. This is meant to adhere
645 	 * to the OpenML OML_sync_control extension specification.
646 	 *
647 	 * \param dev dev DRM device handle.
648 	 * \param pipe crtc for which timestamp should be returned.
649 	 * \param *max_error Maximum allowable timestamp error in nanoseconds.
650 	 *                   Implementation should strive to provide timestamp
651 	 *                   with an error of at most *max_error nanoseconds.
652 	 *                   Returns true upper bound on error for timestamp.
653 	 * \param *vblank_time Target location for returned vblank timestamp.
654 	 * \param flags 0 = Defaults, no special treatment needed.
655 	 * \param       DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
656 	 *	        irq handler. Some drivers need to apply some workarounds
657 	 *              for gpu-specific vblank irq quirks if flag is set.
658 	 *
659 	 * \returns
660 	 * Zero if timestamping isn't supported in current display mode or a
661 	 * negative number on failure. A positive status code on success,
662 	 * which describes how the vblank_time timestamp was computed.
663 	 */
664 	int (*get_vblank_timestamp) (struct drm_device *dev, unsigned int pipe,
665 				     int *max_error,
666 				     struct timeval *vblank_time,
667 				     unsigned flags);
668 
669 	/* these have to be filled in */
670 
671 	irqreturn_t(*irq_handler) (int irq, void *arg);
672 	void (*irq_preinstall) (struct drm_device *dev);
673 	int (*irq_postinstall) (struct drm_device *dev);
674 	void (*irq_uninstall) (struct drm_device *dev);
675 
676 	/* Master routines */
677 	int (*master_create)(struct drm_device *dev, struct drm_master *master);
678 	void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
679 	/**
680 	 * master_set is called whenever the minor master is set.
681 	 * master_drop is called whenever the minor master is dropped.
682 	 */
683 
684 	int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
685 			  bool from_open);
686 	void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
687 			    bool from_release);
688 
689 	int (*debugfs_init)(struct drm_minor *minor);
690 	void (*debugfs_cleanup)(struct drm_minor *minor);
691 
692 	/**
693 	 * @gem_free_object: deconstructor for drm_gem_objects
694 	 *
695 	 * This is deprecated and should not be used by new drivers. Use
696 	 * @gem_free_object_unlocked instead.
697 	 */
698 	void (*gem_free_object) (struct drm_gem_object *obj);
699 
700 	/**
701 	 * @gem_free_object_unlocked: deconstructor for drm_gem_objects
702 	 *
703 	 * This is for drivers which are not encumbered with dev->struct_mutex
704 	 * legacy locking schemes. Use this hook instead of @gem_free_object.
705 	 */
706 	void (*gem_free_object_unlocked) (struct drm_gem_object *obj);
707 
708 	int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
709 	void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
710 
711 	/**
712 	 * Hook for allocating the GEM object struct, for use by core
713 	 * helpers.
714 	 */
715 	struct drm_gem_object *(*gem_create_object)(struct drm_device *dev,
716 						    size_t size);
717 
718 	/* prime: */
719 	/* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
720 	int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
721 				uint32_t handle, uint32_t flags, int *prime_fd);
722 	/* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
723 	int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
724 				int prime_fd, uint32_t *handle);
725 	/* export GEM -> dmabuf */
726 	struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
727 				struct drm_gem_object *obj, int flags);
728 	/* import dmabuf -> GEM */
729 	struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
730 				struct dma_buf *dma_buf);
731 	/* low-level interface used by drm_gem_prime_{import,export} */
732 	int (*gem_prime_pin)(struct drm_gem_object *obj);
733 	void (*gem_prime_unpin)(struct drm_gem_object *obj);
734 	struct reservation_object * (*gem_prime_res_obj)(
735 				struct drm_gem_object *obj);
736 	struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
737 	struct drm_gem_object *(*gem_prime_import_sg_table)(
738 				struct drm_device *dev,
739 				struct dma_buf_attachment *attach,
740 				struct sg_table *sgt);
741 	void *(*gem_prime_vmap)(struct drm_gem_object *obj);
742 	void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
743 	int (*gem_prime_mmap)(struct drm_gem_object *obj,
744 				struct vm_area_struct *vma);
745 
746 	/* vga arb irq handler */
747 	void (*vgaarb_irq)(struct drm_device *dev, bool state);
748 
749 	/* dumb alloc support */
750 	int (*dumb_create)(struct drm_file *file_priv,
751 			   struct drm_device *dev,
752 			   struct drm_mode_create_dumb *args);
753 	int (*dumb_map_offset)(struct drm_file *file_priv,
754 			       struct drm_device *dev, uint32_t handle,
755 			       uint64_t *offset);
756 	int (*dumb_destroy)(struct drm_file *file_priv,
757 			    struct drm_device *dev,
758 			    uint32_t handle);
759 
760 	/* Driver private ops for this object */
761 	struct cdev_pager_ops *gem_vm_ops;
762 
763 	int major;
764 	int minor;
765 	int patchlevel;
766 	char *name;
767 	char *desc;
768 	char *date;
769 
770 	u32 driver_features;
771 	int dev_priv_size;
772 	const struct drm_ioctl_desc *ioctls;
773 	int num_ioctls;
774 	const struct file_operations *fops;
775 
776 	/* List of devices hanging off this driver with stealth attach. */
777 	struct list_head legacy_dev_list;
778 #ifdef __DragonFly__
779 	int (*sysctl_init) (struct drm_device *dev,
780 		    struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
781 	void (*sysctl_cleanup) (struct drm_device *dev);
782 #endif	/* __DragonFly__ */
783 };
784 
785 enum drm_minor_type {
786 	DRM_MINOR_LEGACY,
787 	DRM_MINOR_CONTROL,
788 	DRM_MINOR_RENDER,
789 	DRM_MINOR_CNT,
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 	struct device *kdev;		/**< Linux device */
820 	struct drm_device *dev;
821 
822 	struct dentry *debugfs_root;
823 
824 	struct list_head debugfs_list;
825 	struct lock debugfs_lock; /* Protects debugfs_list. */
826 
827 	/* currently active master for this node. Protected by master_mutex */
828 	struct drm_master *master;
829 };
830 
831 
832 struct drm_pending_vblank_event {
833 	struct drm_pending_event base;
834 	unsigned int pipe;
835 	struct drm_event_vblank event;
836 };
837 
838 struct drm_vblank_crtc {
839 	struct drm_device *dev;		/* pointer to the drm_device */
840 	wait_queue_head_t queue;	/**< VBLANK wait queue */
841 	struct timer_list disable_timer;		/* delayed disable timer */
842 
843 	/* vblank counter, protected by dev->vblank_time_lock for writes */
844 	u32 count;
845 	/* vblank timestamps, protected by dev->vblank_time_lock for writes */
846 	struct timeval time[DRM_VBLANKTIME_RBSIZE];
847 
848 	atomic_t refcount;		/* number of users of vblank interruptsper crtc */
849 	u32 last;			/* protected by dev->vbl_lock, used */
850 					/* for wraparound handling */
851 	u32 last_wait;			/* Last vblank seqno waited per CRTC */
852 	unsigned int inmodeset;		/* Display driver is setting mode */
853 	unsigned int pipe;		/* crtc index */
854 	int framedur_ns;		/* frame/field duration in ns */
855 	int linedur_ns;			/* line duration in ns */
856 	bool enabled;			/* so we don't call enable more than
857 					   once per disable */
858 };
859 
860 struct drm_sysctl_info {
861 	struct sysctl_ctx_list ctx;
862 	char   name[2];
863 };
864 
865 /* Length for the array of resource pointers for drm_get_resource_*. */
866 #define DRM_MAX_PCI_RESOURCE	6
867 
868 /**
869  * DRM device structure. This structure represent a complete card that
870  * may contain multiple heads.
871  */
872 struct drm_device {
873 	struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
874 	int if_version;			/**< Highest interface version set */
875 
876 	int		  unique_len;	/* Length of unique field	   */
877 	struct cdev	  *devnode;	/* Device number for mknod	   */
878 
879 	int		  flags;	/* Flags to open(2)		   */
880 
881 	struct lwkt_serialize irq_lock;	/* protects irq condition checks */
882 	struct lock	  dev_lock;	/* protects everything else */
883 
884 	/** \name Lifetime Management */
885 	/*@{ */
886 	struct kref ref;		/**< Object ref-count */
887 	struct device *dev;		/**< Device structure of bus-device */
888 	struct drm_driver *driver;	/**< DRM driver managing the device */
889 	void *dev_private;		/**< DRM driver private data */
890 	struct drm_minor *control;		/**< Control node */
891 	struct drm_minor *primary;		/**< Primary node */
892 	struct drm_minor *render;		/**< Render node */
893 	atomic_t unplugged;			/**< Flag whether dev is dead */
894 	struct inode *anon_inode;		/**< inode for private address-space */
895 	char *unique;				/**< unique name of the device */
896 	/*@} */
897 
898 	/** \name Locks */
899 	/*@{ */
900 	struct lock struct_mutex;	/**< For others */
901 	struct lock master_mutex;      /**< For drm_minor::master and drm_file::is_master */
902 	/*@} */
903 
904 	/** \name Usage Counters */
905 	/*@{ */
906 	int open_count;			/**< Outstanding files open, protected by drm_global_mutex. */
907 	struct spinlock buf_lock;	/**< For drm_device::buf_use and a few other things. */
908 	int buf_use;			/**< Buffers in use -- cannot alloc */
909 	atomic_t buf_alloc;		/**< Buffer allocation in progress */
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 	struct idr magic_map;
920 
921 	struct lock filelist_mutex;
922 	struct list_head filelist;
923 
924 	/** \name Memory management */
925 	/*@{ */
926 	struct list_head maplist;	/**< Linked list of regions */
927 	struct drm_open_hash map_hash;	/**< User token hash table for maps */
928 
929 	/** \name Context handle management */
930 	/*@{ */
931 	struct list_head ctxlist;	/**< Linked list of context handles */
932 	struct lock ctxlist_mutex;	/**< For ctxlist */
933 
934 	struct idr ctx_idr;
935 
936 	struct list_head vmalist;	/**< List of vmas (for debugging) */
937 
938 	/*@} */
939 
940 	struct drm_lock_data lock;	/* Information on hardware lock	   */
941 
942 	/** \name DMA support */
943 	/*@{ */
944 	struct drm_device_dma *dma;		/**< Optional pointer for DMA support */
945 	/*@} */
946 
947 	/** \name Context support */
948 	/*@{ */
949 
950 	__volatile__ long context_flag;	/**< Context swapping flag */
951 	int last_context;		/**< Last current context */
952 	/*@} */
953 
954 	/** \name VBLANK IRQ support */
955 	/*@{ */
956 	bool irq_enabled;
957 	int irq;
958 
959 	/*
960 	 * If true, vblank interrupt will be disabled immediately when the
961 	 * refcount drops to zero, as opposed to via the vblank disable
962 	 * timer.
963 	 * This can be set to true it the hardware has a working vblank
964 	 * counter and the driver uses drm_vblank_on() and drm_vblank_off()
965 	 * appropriately.
966 	 */
967 	bool vblank_disable_immediate;
968 
969 	/* array of size num_crtcs */
970 	struct drm_vblank_crtc *vblank;
971 
972 	struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
973 	struct lock vbl_lock;
974 
975 	u32 max_vblank_count;           /**< size of vblank counter register */
976 
977 	/**
978 	 * List of events
979 	 */
980 	struct list_head vblank_event_list;
981 	struct lock event_lock;
982 
983 	/*@} */
984 
985 	struct sigio      *buf_sigio;	/* Processes waiting for SIGIO     */
986 	struct drm_agp_head *agp;	/**< AGP data */
987 
988 	struct pci_dev *pdev;		/**< PCI device structure */
989 #ifdef __alpha__
990 	struct pci_controller *hose;
991 #endif
992 
993 	struct platform_device *platformdev; /**< Platform device struture */
994 	struct virtio_device *virtdev;
995 
996 	struct drm_sg_mem *sg;	/**< Scatter gather memory */
997 	unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
998 
999 	void		  *drm_ttm_bdev;
1000 
1001 	struct {
1002 		int context;
1003 		struct drm_hw_lock *lock;
1004 	} sigdata;
1005 
1006 	struct drm_local_map *agp_buffer_map;
1007 	unsigned int agp_buffer_token;
1008 
1009 	struct drm_mode_config mode_config;	/**< Current mode config */
1010 
1011 	/** \name GEM information */
1012 	/*@{ */
1013 	struct lock object_name_lock;
1014 	struct idr object_name_idr;
1015 	struct drm_vma_offset_manager *vma_offset_manager;
1016 	/*@} */
1017 	int switch_power_state;
1018 #ifdef __DragonFly__
1019 	/* Storage of resource pointers for drm_get_resource_* */
1020 	struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
1021 	int		  pcirid[DRM_MAX_PCI_RESOURCE];
1022 
1023 	int		  pci_domain;
1024 	int		  pci_bus;
1025 	int		  pci_slot;
1026 	int		  pci_func;
1027 	drm_pci_id_list_t *id_entry;	/* PCI ID, name, and chipset private */
1028 	char busid_str[128];
1029 	int modesetting;
1030 	void             *mm_private;
1031 	struct drm_sysctl_info *sysctl;
1032 #endif
1033 };
1034 
1035 #define DRM_SWITCH_POWER_ON 0
1036 #define DRM_SWITCH_POWER_OFF 1
1037 #define DRM_SWITCH_POWER_CHANGING 2
1038 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1039 
1040 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1041 					     int feature)
1042 {
1043 	return ((dev->driver->driver_features & feature) ? 1 : 0);
1044 }
1045 
1046 static inline void drm_device_set_unplugged(struct drm_device *dev)
1047 {
1048 	smp_wmb();
1049 	atomic_set(&dev->unplugged, 1);
1050 }
1051 
1052 static inline int drm_device_is_unplugged(struct drm_device *dev)
1053 {
1054 	int ret = atomic_read(&dev->unplugged);
1055 	smp_rmb();
1056 	return ret;
1057 }
1058 
1059 static inline bool drm_is_render_client(const struct drm_file *file_priv)
1060 {
1061 	return file_priv->minor->type == DRM_MINOR_RENDER;
1062 }
1063 
1064 static inline bool drm_is_control_client(const struct drm_file *file_priv)
1065 {
1066 	return file_priv->minor->type == DRM_MINOR_CONTROL;
1067 }
1068 
1069 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1070 {
1071 	return file_priv->minor->type == DRM_MINOR_LEGACY;
1072 }
1073 
1074 /******************************************************************/
1075 /** \name Internal function definitions */
1076 /*@{*/
1077 
1078 				/* Driver support (drm_drv.h) */
1079 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1080 int	drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1081 int	drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1082 int	drm_create_cdevs(device_t kdev);
1083 d_ioctl_t drm_ioctl;
1084 extern long drm_compat_ioctl(struct file *filp,
1085 			     unsigned int cmd, unsigned long arg);
1086 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1087 
1088 /* File Operations (drm_fops.c) */
1089 d_open_t drm_open;
1090 d_close_t drm_close;
1091 d_read_t drm_read;
1092 d_kqfilter_t drm_kqfilter;
1093 int drm_release(device_t kdev);
1094 
1095 d_mmap_t drm_mmap;
1096 d_mmap_single_t drm_mmap_single;
1097 
1098 void drm_cdevpriv_dtor(void *cd);
1099 
1100 int drm_add_busid_modesetting(struct drm_device *dev,
1101     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1102 
1103 /* File operations helpers (drm_fops.c) */
1104 extern int		drm_open_helper(struct cdev *kdev, int flags, int fmt,
1105 					 DRM_STRUCTPROC *p,
1106 					struct drm_device *dev,
1107 					struct file *fp);
1108 
1109 int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv);
1110 int drm_event_reserve_init_locked(struct drm_device *dev,
1111 				  struct drm_file *file_priv,
1112 				  struct drm_pending_event *p,
1113 				  struct drm_event *e);
1114 int drm_event_reserve_init(struct drm_device *dev,
1115 			   struct drm_file *file_priv,
1116 			   struct drm_pending_event *p,
1117 			   struct drm_event *e);
1118 void drm_event_cancel_free(struct drm_device *dev,
1119 			   struct drm_pending_event *p);
1120 void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e);
1121 void drm_send_event(struct drm_device *dev, struct drm_pending_event *e);
1122 
1123 /* Misc. IOCTL support (drm_ioctl.c) */
1124 int drm_noop(struct drm_device *dev, void *data,
1125 	     struct drm_file *file_priv);
1126 int drm_invalid_op(struct drm_device *dev, void *data,
1127 		   struct drm_file *file_priv);
1128 
1129 /* Cache management (drm_cache.c) */
1130 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
1131 void drm_clflush_sg(struct sg_table *st);
1132 void drm_clflush_virt_range(void *addr, unsigned long length);
1133 
1134 /*
1135  * These are exported to drivers so that they can implement fencing using
1136  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1137  */
1138 
1139 unsigned long drm_get_resource_start(struct drm_device *dev,
1140 				     unsigned int resource);
1141 unsigned long drm_get_resource_len(struct drm_device *dev,
1142 				   unsigned int resource);
1143 
1144 				/* IRQ support (drm_irq.h) */
1145 extern int drm_irq_install(struct drm_device *dev, int irq);
1146 extern int drm_irq_uninstall(struct drm_device *dev);
1147 
1148 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
1149 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1150 			   struct drm_file *filp);
1151 extern u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe);
1152 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1153 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1154 				     struct timeval *vblanktime);
1155 extern u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
1156 					  struct timeval *vblanktime);
1157 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1158 				  struct drm_pending_vblank_event *e);
1159 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1160 				       struct drm_pending_vblank_event *e);
1161 extern void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
1162 				 struct drm_pending_vblank_event *e);
1163 extern void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1164 				      struct drm_pending_vblank_event *e);
1165 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
1166 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1167 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
1168 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
1169 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1170 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1171 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
1172 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1173 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
1174 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
1175 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1176 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1177 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1178 extern void drm_vblank_cleanup(struct drm_device *dev);
1179 extern u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe);
1180 
1181 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1182 						 unsigned int pipe, int *max_error,
1183 						 struct timeval *vblank_time,
1184 						 unsigned flags,
1185 						 const struct drm_display_mode *mode);
1186 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1187 					    const struct drm_display_mode *mode);
1188 
1189 /**
1190  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1191  * @crtc: which CRTC's vblank waitqueue to retrieve
1192  *
1193  * This function returns a pointer to the vblank waitqueue for the CRTC.
1194  * Drivers can use this to implement vblank waits using wait_event() & co.
1195  */
1196 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1197 {
1198 	return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1199 }
1200 
1201 /* Modesetting support */
1202 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1203 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1204 
1205 				/* Stub support (drm_stub.h) */
1206 extern struct drm_master *drm_master_get(struct drm_master *master);
1207 extern void drm_master_put(struct drm_master **master);
1208 
1209 extern void drm_put_dev(struct drm_device *dev);
1210 extern void drm_unplug_dev(struct drm_device *dev);
1211 extern unsigned int drm_debug;
1212 extern bool drm_atomic;
1213 
1214 				/* Debugfs support */
1215 #if defined(CONFIG_DEBUG_FS)
1216 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1217 				    int count, struct dentry *root,
1218 				    struct drm_minor *minor);
1219 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1220 				    int count, struct drm_minor *minor);
1221 #else
1222 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1223 					   int count, struct dentry *root,
1224 					   struct drm_minor *minor)
1225 {
1226 	return 0;
1227 }
1228 
1229 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1230 					   int count, struct drm_minor *minor)
1231 {
1232 	return 0;
1233 }
1234 #endif
1235 
1236 /* sysctl support (drm_sysctl.h) */
1237 extern int drm_sysctl_init(struct drm_device *dev);
1238 extern int drm_sysctl_cleanup(struct drm_device *dev);
1239 
1240 /* XXX: These are here only because of drm_sysctl.c */
1241 extern int drm_vblank_offdelay;
1242 extern unsigned int drm_timestamp_precision;
1243 
1244 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1245     vm_size_t size, struct vm_object **obj_res, int nprot);
1246 
1247 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1248 					    struct drm_gem_object *obj,
1249 					    int flags);
1250 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1251 		struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1252 		int *prime_fd);
1253 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1254 		struct dma_buf *dma_buf);
1255 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1256 		struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1257 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1258 
1259 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1260 					    dma_addr_t *addrs, int max_pages);
1261 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages);
1262 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1263 
1264 
1265 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1266 					    size_t align);
1267 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1268 
1269 			       /* sysfs support (drm_sysfs.c) */
1270 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1271 
1272 struct ttm_bo_device;
1273 int ttm_bo_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1274     vm_size_t size, struct vm_object **obj_res, int nprot);
1275 struct ttm_buffer_object;
1276 
1277 /* simplified version of kvasnprintf() for drm needs. */
1278 char *drm_vasprintf(int flags, const char *format, va_list ap) __printflike(2, 0);
1279 char *drm_asprintf(int flags, const char *format, ...) __printflike(2, 3);
1280 
1281 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1282 void drm_init_pdev(device_t dev, struct pci_dev **pdev);
1283 void drm_fini_pdev(struct pci_dev **pdev);
1284 void drm_print_pdev(struct pci_dev *pdev);
1285 
1286 /* Inline drm_free() helper for area kfree() */
1287 static __inline__ void
1288 drm_free(void *pt, struct malloc_type *area)
1289 {
1290 	/* kfree is special!!! */
1291 	if (pt != NULL)
1292 		(kfree)(pt, area);
1293 }
1294 
1295 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1296 				 struct device *parent);
1297 void drm_dev_ref(struct drm_device *dev);
1298 void drm_dev_unref(struct drm_device *dev);
1299 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1300 void drm_dev_unregister(struct drm_device *dev);
1301 int drm_dev_set_unique(struct drm_device *dev, const char *name);
1302 
1303 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1304 void drm_minor_release(struct drm_minor *minor);
1305 
1306 /*@}*/
1307 
1308 /* PCI section */
1309 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1310 {
1311 	if (dev->driver->device_is_agp != NULL) {
1312 		int err = (*dev->driver->device_is_agp) (dev);
1313 
1314 		if (err != 2) {
1315 			return err;
1316 		}
1317 	}
1318 
1319 	return (pci_find_extcap(dev->pdev->dev.bsddev, PCIY_AGP, NULL) == 0);
1320 }
1321 void drm_pci_agp_destroy(struct drm_device *dev);
1322 
1323 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1324 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1325 #ifdef CONFIG_PCI
1326 extern int drm_get_pci_dev(struct pci_dev *pdev,
1327 			   const struct pci_device_id *ent,
1328 			   struct drm_driver *driver);
1329 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1330 #else
1331 static inline int drm_get_pci_dev(struct pci_dev *pdev,
1332 				  const struct pci_device_id *ent,
1333 				  struct drm_driver *driver)
1334 {
1335 	return -ENOSYS;
1336 }
1337 
1338 static inline int drm_pci_set_busid(struct drm_device *dev,
1339 				    struct drm_master *master)
1340 {
1341 	return -ENOSYS;
1342 }
1343 #endif
1344 
1345 #define DRM_PCIE_SPEED_25 1
1346 #define DRM_PCIE_SPEED_50 2
1347 #define DRM_PCIE_SPEED_80 4
1348 
1349 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1350 extern int drm_pcie_get_max_link_width(struct drm_device *dev, u32 *mlw);
1351 
1352 /* platform section */
1353 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1354 extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
1355 
1356 /* XXX bad */
1357 #define	drm_can_sleep()	(HZ & 1)
1358 
1359 #endif /* __KERNEL__ */
1360 
1361 /* helper for handling conditionals in various for_each macros */
1362 #define for_each_if(condition) if (!(condition)) {} else
1363 
1364 #endif
1365