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