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