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