Merge branch 'vendor/LIBARCHIVE'
[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 #define dev_dbg(dev, fmt, ...) do {                                     \
326         if ((drm_debug& DRM_DEBUGBITS_KMS) != 0) {                      \
327                 device_printf((dev), "debug: " fmt, ## __VA_ARGS__);    \
328         }                                                               \
329 } while (0)
330
331 typedef struct drm_pci_id_list
332 {
333         int vendor;
334         int device;
335         long driver_private;
336         char *name;
337 } drm_pci_id_list_t;
338
339 /**
340  * Ioctl function type.
341  *
342  * \param inode device inode.
343  * \param file_priv DRM file private pointer.
344  * \param cmd command.
345  * \param arg argument.
346  */
347 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
348                         struct drm_file *file_priv);
349
350 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
351                                unsigned long arg);
352
353 #define DRM_IOCTL_NR(n)                ((n) & 0xff)
354
355 #define DRM_AUTH        0x1
356 #define DRM_MASTER      0x2
357 #define DRM_ROOT_ONLY   0x4
358 #define DRM_CONTROL_ALLOW 0x8
359 #define DRM_UNLOCKED    0x10
360 #define DRM_RENDER_ALLOW 0x20
361
362 struct drm_ioctl_desc {
363         unsigned int cmd;
364         int flags;
365         drm_ioctl_t *func;
366         unsigned int cmd_drv;
367         const char *name;
368 };
369
370 /**
371  * Creates a driver or general drm_ioctl_desc array entry for the given
372  * ioctl, for use by drm_ioctl().
373  */
374 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                 \
375         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl}
376
377 typedef struct drm_magic_entry {
378         struct list_head head;
379         struct drm_hash_item hash_item;
380         struct drm_file        *priv;
381         struct drm_magic_entry *next;
382 } drm_magic_entry_t;
383
384 typedef struct drm_magic_head {
385         struct drm_magic_entry *head;
386         struct drm_magic_entry *tail;
387 } drm_magic_head_t;
388
389 /** bufs is one longer than it has to be */
390 struct drm_waitlist {
391         int count;                      /**< Number of possible buffers */
392         struct drm_buf **bufs;          /**< List of pointers to buffers */
393         struct drm_buf **rp;                    /**< Read pointer */
394         struct drm_buf **wp;                    /**< Write pointer */
395         struct drm_buf **end;           /**< End pointer */
396         struct spinlock *read_lock;
397         struct spinlock *write_lock;
398 };
399
400 /* Event queued up for userspace to read */
401 struct drm_pending_event {
402         struct drm_event *event;
403         struct list_head link;
404         struct drm_file *file_priv;
405         pid_t pid; /* pid of requester, no guarantee it's valid by the time
406                       we deliver the event, for tracing only */
407         void (*destroy)(struct drm_pending_event *event);
408 };
409
410 /* initial implementaton using a linked list - todo hashtab */
411 struct drm_prime_file_private {
412         struct list_head head;
413 #ifdef DUMBBELL_WIP
414         struct mutex lock;
415 #endif /* DUMBBELL_WIP */
416 };
417
418 /** File private data */
419 struct drm_file {
420         int authenticated;
421         struct drm_device *dev;
422         int               master;
423
424         /* true when the client has asked us to expose stereo 3D mode flags */
425         bool stereo_allowed;
426         /*
427          * true if client understands CRTC primary planes and cursor planes
428          * in the plane list
429          */
430         unsigned universal_planes:1;
431         /* true if client understands atomic properties */
432         unsigned atomic:1;
433
434         pid_t             pid;
435         uid_t             uid;
436         drm_magic_t       magic;
437         unsigned long     ioctl_count;
438         struct list_head lhead;
439         struct kqinfo     dkq;
440
441         /** Mapping of mm object handles to object pointers. */
442         struct idr object_idr;
443         /** Lock for synchronization of access to object_idr. */
444         struct lock table_lock;
445
446         void *driver_priv;
447
448         int               is_master;
449         struct drm_master *masterp;
450
451         /**
452          * fbs - List of framebuffers associated with this file.
453          *
454          * Protected by fbs_lock. Note that the fbs list holds a reference on
455          * the fb object to prevent it from untimely disappearing.
456          */
457         struct list_head fbs;
458         struct lock fbs_lock;
459
460         /** User-created blob properties; this retains a reference on the
461          *  property. */
462         struct list_head blobs;
463
464         wait_queue_head_t event_wait;
465         struct list_head event_list;
466         int event_space;
467
468         struct drm_prime_file_private prime;
469 };
470
471 /**
472  * Lock data.
473  */
474 struct drm_lock_data {
475         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
476         /** Private of lock holder's file (NULL=kernel) */
477         struct drm_file *file_priv;
478         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
479         unsigned long lock_time;        /**< Time of last lock in jiffies */
480 };
481
482 /**
483  * GEM specific mm private for tracking GEM objects
484  */
485 struct drm_gem_mm {
486         struct drm_vma_offset_manager vma_manager;
487         struct drm_mm offset_manager;   /**< Offset mgmt for buffer objects */
488         struct drm_open_hash offset_hash; /**< User token hash table for maps */
489         struct unrhdr *idxunr;
490 };
491
492 /**
493  * struct drm_master - drm master structure
494  *
495  * @refcount: Refcount for this master object.
496  * @minor: Link back to minor char device we are master for. Immutable.
497  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
498  * @unique_len: Length of unique field. Protected by drm_global_mutex.
499  * @unique_size: Amount allocated. Protected by drm_global_mutex.
500  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
501  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
502  * @lock: DRI lock information.
503  * @driver_priv: Pointer to driver-private information.
504  */
505 struct drm_master {
506         struct kref refcount;           /* refcount for this master */
507         struct list_head head;          /**< each minor contains a list of masters */
508         struct drm_minor *minor;        /**< link back to minor we are a master for */
509         char *unique;                   /**< Unique identifier: e.g., busid */
510         int unique_len;                 /**< Length of unique field */
511         int unique_size;                /**< amount allocated */
512         int blocked;                    /**< Blocked due to VC switch? */
513         struct drm_open_hash magiclist;
514         struct list_head magicfree;
515         struct drm_lock_data lock;
516         void *driver_priv;
517 };
518
519 /* Size of ringbuffer for vblank timestamps. Just double-buffer
520  * in initial implementation.
521  */
522 #define DRM_VBLANKTIME_RBSIZE 2
523
524 /* Flags and return codes for get_vblank_timestamp() driver function. */
525 #define DRM_CALLED_FROM_VBLIRQ 1
526 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
527 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
528
529 /* get_scanout_position() return flags */
530 #define DRM_SCANOUTPOS_VALID        (1 << 0)
531 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
532 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
533
534 #ifndef DMA_BIT_MASK
535 #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : (1ULL<<(n)) - 1)
536 #endif
537
538 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
539
540 /**
541  * DRM driver structure. This structure represent the common code for
542  * a family of cards. There will one drm_device for each card present
543  * in this family
544  */
545 struct drm_driver {
546         int (*load) (struct drm_device *, unsigned long flags);
547         int (*use_msi) (struct drm_device *, unsigned long flags);
548         int (*firstopen) (struct drm_device *);
549         int (*open) (struct drm_device *, struct drm_file *);
550         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
551         void (*postclose) (struct drm_device *, struct drm_file *);
552         void (*lastclose) (struct drm_device *);
553         int (*unload) (struct drm_device *);
554         void (*reclaim_buffers_locked) (struct drm_device *,
555                                         struct drm_file *file_priv);
556         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
557         int (*dma_quiescent) (struct drm_device *);
558         int (*context_ctor) (struct drm_device *dev, int context);
559         int (*context_dtor) (struct drm_device *dev, int context);
560
561         /**
562          * get_vblank_counter - get raw hardware vblank counter
563          * @dev: DRM device
564          * @crtc: counter to fetch
565          *
566          * Driver callback for fetching a raw hardware vblank counter for @crtc.
567          * If a device doesn't have a hardware counter, the driver can simply
568          * return the value of drm_vblank_count. The DRM core will account for
569          * missed vblank events while interrupts where disabled based on system
570          * timestamps.
571          *
572          * Wraparound handling and loss of events due to modesetting is dealt
573          * with in the DRM core code.
574          *
575          * RETURNS
576          * Raw vblank counter value.
577          */
578         u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
579
580         /**
581          * enable_vblank - enable vblank interrupt events
582          * @dev: DRM device
583          * @crtc: which irq to enable
584          *
585          * Enable vblank interrupts for @crtc.  If the device doesn't have
586          * a hardware vblank counter, this routine should be a no-op, since
587          * interrupts will have to stay on to keep the count accurate.
588          *
589          * RETURNS
590          * Zero on success, appropriate errno if the given @crtc's vblank
591          * interrupt cannot be enabled.
592          */
593         int (*enable_vblank) (struct drm_device *dev, int crtc);
594
595         /**
596          * disable_vblank - disable vblank interrupt events
597          * @dev: DRM device
598          * @crtc: which irq to enable
599          *
600          * Disable vblank interrupts for @crtc.  If the device doesn't have
601          * a hardware vblank counter, this routine should be a no-op, since
602          * interrupts will have to stay on to keep the count accurate.
603          */
604         void (*disable_vblank) (struct drm_device *dev, int crtc);
605
606         /**
607          * Called by \c drm_device_is_agp.  Typically used to determine if a
608          * card is really attached to AGP or not.
609          *
610          * \param dev  DRM device handle
611          *
612          * \returns
613          * One of three values is returned depending on whether or not the
614          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
615          * (return of 1), or may or may not be AGP (return of 2).
616          */
617         int (*device_is_agp) (struct drm_device *dev);
618
619         /**
620          * Called by vblank timestamping code.
621          *
622          * Return the current display scanout position from a crtc, and an
623          * optional accurate ktime_get timestamp of when position was measured.
624          *
625          * \param dev  DRM device.
626          * \param crtc Id of the crtc to query.
627          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
628          * \param *vpos Target location for current vertical scanout position.
629          * \param *hpos Target location for current horizontal scanout position.
630          * \param *stime Target location for timestamp taken immediately before
631          *               scanout position query. Can be NULL to skip timestamp.
632          * \param *etime Target location for timestamp taken immediately after
633          *               scanout position query. Can be NULL to skip timestamp.
634          *
635          * Returns vpos as a positive number while in active scanout area.
636          * Returns vpos as a negative number inside vblank, counting the number
637          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
638          * until start of active scanout / end of vblank."
639          *
640          * \return Flags, or'ed together as follows:
641          *
642          * DRM_SCANOUTPOS_VALID = Query successful.
643          * DRM_SCANOUTPOS_INVBL = Inside vblank.
644          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
645          * this flag means that returned position may be offset by a constant
646          * but unknown small number of scanlines wrt. real scanout position.
647          *
648          */
649         int (*get_scanout_position) (struct drm_device *dev, int crtc,
650                                      unsigned int flags,
651                                      int *vpos, int *hpos, ktime_t *stime,
652                                      ktime_t *etime);
653
654         /**
655          * Called by \c drm_get_last_vbltimestamp. Should return a precise
656          * timestamp when the most recent VBLANK interval ended or will end.
657          *
658          * Specifically, the timestamp in @vblank_time should correspond as
659          * closely as possible to the time when the first video scanline of
660          * the video frame after the end of VBLANK will start scanning out,
661          * the time immediately after end of the VBLANK interval. If the
662          * @crtc is currently inside VBLANK, this will be a time in the future.
663          * If the @crtc is currently scanning out a frame, this will be the
664          * past start time of the current scanout. This is meant to adhere
665          * to the OpenML OML_sync_control extension specification.
666          *
667          * \param dev dev DRM device handle.
668          * \param crtc crtc for which timestamp should be returned.
669          * \param *max_error Maximum allowable timestamp error in nanoseconds.
670          *                   Implementation should strive to provide timestamp
671          *                   with an error of at most *max_error nanoseconds.
672          *                   Returns true upper bound on error for timestamp.
673          * \param *vblank_time Target location for returned vblank timestamp.
674          * \param flags 0 = Defaults, no special treatment needed.
675          * \param       DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
676          *              irq handler. Some drivers need to apply some workarounds
677          *              for gpu-specific vblank irq quirks if flag is set.
678          *
679          * \returns
680          * Zero if timestamping isn't supported in current display mode or a
681          * negative number on failure. A positive status code on success,
682          * which describes how the vblank_time timestamp was computed.
683          */
684         int (*get_vblank_timestamp) (struct drm_device *dev, int crtc,
685                                      int *max_error,
686                                      struct timeval *vblank_time,
687                                      unsigned flags);
688
689         /* these have to be filled in */
690
691         void (*irq_handler) (void *arg);
692         void (*irq_preinstall) (struct drm_device *dev);
693         int (*irq_postinstall) (struct drm_device *dev);
694         void (*irq_uninstall) (struct drm_device *dev);
695
696         /**
697          * Driver-specific constructor for drm_gem_objects, to set up
698          * obj->driver_private.
699          *
700          * Returns 0 on success.
701          */
702         void (*gem_free_object) (struct drm_gem_object *obj);
703         int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
704         void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
705
706         int (*sysctl_init) (struct drm_device *dev,
707                     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
708         void (*sysctl_cleanup) (struct drm_device *dev);
709
710         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
711
712         /* dumb alloc support */
713         int (*dumb_create)(struct drm_file *file_priv,
714                            struct drm_device *dev,
715                            struct drm_mode_create_dumb *args);
716         int (*dumb_map_offset)(struct drm_file *file_priv,
717                                struct drm_device *dev, uint32_t handle,
718                                uint64_t *offset);
719         int (*dumb_destroy)(struct drm_file *file_priv,
720                             struct drm_device *dev,
721                             uint32_t handle);
722
723         /* Driver private ops for this object */
724         struct cdev_pager_ops *gem_pager_ops;
725
726         int major;
727         int minor;
728         int patchlevel;
729         const char *name;               /* Simple driver name              */
730         const char *desc;               /* Longer driver name              */
731         const char *date;               /* Date of last major changes.     */
732
733         u32 driver_features;
734         int dev_priv_size;
735         const struct drm_ioctl_desc *ioctls;
736         int num_ioctls;
737 };
738
739 /**
740  * Info file list entry. This structure represents a debugfs or proc file to
741  * be created by the drm core
742  */
743 struct drm_info_list {
744         const char *name; /** file name */
745         int (*show)(struct seq_file*, void*); /** show callback */
746         u32 driver_features; /**< Required driver features for this entry */
747         void *data;
748 };
749
750 /**
751  * debugfs node structure. This structure represents a debugfs file.
752  */
753 struct drm_info_node {
754         struct list_head list;
755         struct drm_minor *minor;
756         const struct drm_info_list *info_ent;
757         struct dentry *dent;
758 };
759
760 /**
761  * DRM minor structure. This structure represents a drm minor number.
762  */
763 struct drm_minor {
764         int index;                      /**< Minor device number */
765         int type;                       /**< Control or render */
766         device_t kdev;                  /**< OS device */
767         struct drm_device *dev;
768
769         struct drm_master *master; /* currently active master for this node */
770         struct list_head master_list;
771         struct drm_mode_group mode_group;
772 };
773
774 struct drm_pending_vblank_event {
775         struct drm_pending_event base;
776         unsigned int pipe;
777         struct drm_event_vblank event;
778 };
779
780 struct drm_sysctl_info {
781         struct sysctl_ctx_list ctx;
782         char   name[2];
783 };
784
785 /* Length for the array of resource pointers for drm_get_resource_*. */
786 #define DRM_MAX_PCI_RESOURCE    6
787
788 struct drm_vblank_crtc {
789         struct drm_device *dev;         /* pointer to the drm_device */
790         wait_queue_head_t queue;        /**< VBLANK wait queue */
791         struct timer_list disable_timer;                /* delayed disable timer */
792
793         /* vblank counter, protected by dev->vblank_time_lock for writes */
794         unsigned long count;
795         /* vblank timestamps, protected by dev->vblank_time_lock for writes */
796         struct timeval time[DRM_VBLANKTIME_RBSIZE];
797
798         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
799         u32 last;                       /* protected by dev->vbl_lock, used */
800                                         /* for wraparound handling */
801         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
802         unsigned int inmodeset;         /* Display driver is setting mode */
803         unsigned int pipe;              /* crtc index */
804         bool enabled;                   /* so we don't call enable more than
805                                            once per disable */
806 };
807
808 /**
809  * DRM device structure. This structure represent a complete card that
810  * may contain multiple heads.
811  */
812 struct drm_device {
813         drm_pci_id_list_t *id_entry;    /* PCI ID, name, and chipset private */
814
815         char              *unique;      /* Unique identifier: e.g., busid  */
816         int               unique_len;   /* Length of unique field          */
817         struct cdev       *devnode;     /* Device number for mknod         */
818         int               if_version;   /* Highest interface version set */
819
820         int               flags;        /* Flags to open(2)                */
821
822                                 /* Locks */
823         struct spinlock   dma_lock;     /* protects dev->dma */
824         struct lwkt_serialize irq_lock; /* protects irq condition checks */
825         struct lock       dev_lock;     /* protects everything else */
826
827         /** \name Locks */
828         /*@{ */
829         struct lock struct_mutex;       /**< For others */
830         struct lock master_mutex;       /**< For drm_minor::master */
831         /*@} */
832
833         /** \name Usage Counters */
834         /*@{ */
835         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
836         struct spinlock buf_lock;       /**< For drm_device::buf_use and a few other things. */
837         int buf_use;                    /**< Buffers in use -- cannot alloc */
838         atomic_t buf_alloc;             /**< Buffer allocation in progress */
839         /*@} */
840
841
842         /** \name Performance counters */
843         /*@{ */
844         unsigned long     counters;
845         enum drm_stat_type      types[15];
846         atomic_t          counts[15];
847         /*@} */
848
849                                 /* Authentication */
850         struct drm_open_hash magiclist; /**< magic hash table */
851         struct list_head magicfree;
852
853         struct list_head filelist;
854
855         /** \name Memory management */
856         /*@{ */
857         struct list_head maplist;       /**< Linked list of regions */
858         int map_count;                  /**< Number of mappable regions */
859         struct drm_open_hash map_hash;  /**< User token hash table for maps */
860
861         /** \name Context handle management */
862         /*@{ */
863         struct list_head ctxlist;       /**< Linked list of context handles */
864         int ctx_count;                  /**< Number of context handles */
865         struct lock ctxlist_mutex;      /**< For ctxlist */
866
867         struct idr ctx_idr;
868
869         /*@} */
870
871         struct drm_lock_data lock;      /* Information on hardware lock    */
872
873         /** \name DMA support */
874         /*@{ */
875         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
876         /*@} */
877
878         int               irq_type;     /* IRQ type (MSI enabled or not) */
879         int               irqrid;       /* Interrupt used by board */
880         struct resource   *irqr;        /* Resource for interrupt used by board    */
881         void              *irqh;        /* Handle from bus_setup_intr      */
882
883         /* Storage of resource pointers for drm_get_resource_* */
884         struct resource   *pcir[DRM_MAX_PCI_RESOURCE];
885         int               pcirid[DRM_MAX_PCI_RESOURCE];
886
887         int               pci_domain;
888         int               pci_bus;
889         int               pci_slot;
890         int               pci_func;
891
892         /** \name Context support */
893         /*@{ */
894
895         __volatile__ long context_flag; /**< Context swapping flag */
896         __volatile__ long interrupt_flag; /**< Interruption handler flag */
897         __volatile__ long dma_flag;     /**< DMA dispatch flag */
898         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
899         int last_checked;               /**< Last context checked for DMA */
900         int last_context;               /**< Last current context */
901         unsigned long last_switch;      /**< jiffies at last context switch */
902         /*@} */
903
904         /** \name VBLANK IRQ support */
905         /*@{ */
906         int irq_enabled;                /**< True if irq handler is enabled */
907         int irq;                        /* Interrupt used by board */
908
909         /*
910          * At load time, disabling the vblank interrupt won't be allowed since
911          * old clients may not call the modeset ioctl and therefore misbehave.
912          * Once the modeset ioctl *has* been called though, we can safely
913          * disable them when unused.
914          */
915         bool vblank_disable_allowed;
916
917         /*
918          * If true, vblank interrupt will be disabled immediately when the
919          * refcount drops to zero, as opposed to via the vblank disable
920          * timer.
921          * This can be set to true it the hardware has a working vblank
922          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
923          * appropriately.
924          */
925         bool vblank_disable_immediate;
926
927         /* array of size num_crtcs */
928         struct drm_vblank_crtc *vblank;
929
930         struct lock vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
931         struct lock vbl_lock;
932         struct timer_list vblank_disable_timer;
933
934         u32 max_vblank_count;           /**< size of vblank counter register */
935
936         /**
937          * List of events
938          */
939         struct list_head vblank_event_list;
940         struct lock event_lock;
941
942         /*@} */
943
944         struct sigio      *buf_sigio;   /* Processes waiting for SIGIO     */
945
946                                 /* Sysctl support */
947         struct drm_sysctl_info *sysctl;
948
949
950         struct drm_sg_mem *sg;  /**< Scatter gather memory */
951         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
952
953         unsigned long     *ctx_bitmap;
954         void              *dev_private;
955
956         void              *drm_ttm_bdev;
957
958         /*@} */
959
960         struct drm_agp_head *agp;       /**< AGP data */
961
962         struct device *dev;             /**< Device structure */
963         struct pci_dev *pdev;           /**< PCI device structure */
964
965         struct drm_driver *driver;
966         struct drm_local_map *agp_buffer_map;
967         unsigned int agp_buffer_token;
968         struct drm_minor *control;              /**< Control node for card */
969         struct drm_minor *primary;              /**< render type primary screen head */
970
971         struct drm_mode_config mode_config;     /**< Current mode config */
972
973         /** \name GEM information */
974         /*@{ */
975         struct lock object_name_lock;
976         struct idr object_name_idr;
977         /*@} */
978         void             *mm_private;
979
980         void *sysctl_private;
981         char busid_str[128];
982         int modesetting;
983
984         int switch_power_state;
985
986         atomic_t unplugged; /* device has been unplugged or gone away */
987 };
988
989 #define DRM_SWITCH_POWER_ON 0
990 #define DRM_SWITCH_POWER_OFF 1
991 #define DRM_SWITCH_POWER_CHANGING 2
992 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
993
994 static __inline__ int drm_core_check_feature(struct drm_device *dev,
995                                              int feature)
996 {
997         return ((dev->driver->driver_features & feature) ? 1 : 0);
998 }
999
1000 static inline void drm_device_set_unplugged(struct drm_device *dev)
1001 {
1002         smp_wmb();
1003         atomic_set(&dev->unplugged, 1);
1004 }
1005
1006 static inline int drm_device_is_unplugged(struct drm_device *dev)
1007 {
1008         int ret = atomic_read(&dev->unplugged);
1009         smp_rmb();
1010         return ret;
1011 }
1012
1013 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1014 {
1015         return 0 /* file_priv->minor->type == DRM_MINOR_LEGACY */;
1016 }
1017
1018 /**
1019  * drm_is_master() - Check whether a DRM open-file is DRM-Master
1020  * @file: DRM open-file context
1021  *
1022  * This checks whether a DRM open-file context is owner of the master context
1023  * attached to it. If a file owns a master context, it's called DRM-Master.
1024  * Per DRM device, only one such file can be DRM-Master at a time.
1025  *
1026  * Returns: True if the file is DRM-Master, otherwise false.
1027  */
1028 static inline bool drm_is_master(const struct drm_file *file)
1029 {
1030 #if 0
1031         return file->master && file->master == file->minor->master;
1032 #else
1033         return true;
1034 #endif
1035 }
1036
1037 /******************************************************************/
1038 /** \name Internal function definitions */
1039 /*@{*/
1040
1041 #if __OS_HAS_AGP
1042 static inline int drm_core_has_AGP(struct drm_device *dev)
1043 {
1044         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1045 }
1046 #else
1047 #define drm_core_has_AGP(dev) (0)
1048 #endif
1049
1050 extern unsigned int drm_rnodes;
1051 extern unsigned int drm_universal_planes;
1052
1053 extern int drm_vblank_offdelay;
1054 extern unsigned int drm_timestamp_precision;
1055
1056                                 /* Driver support (drm_drv.h) */
1057 extern int drm_ioctl_permit(u32 flags, struct drm_file *file_priv);
1058 int     drm_probe(device_t kdev, drm_pci_id_list_t *idlist);
1059 int     drm_attach(device_t kdev, drm_pci_id_list_t *idlist);
1060 int     drm_create_cdevs(device_t kdev);
1061 d_ioctl_t drm_ioctl;
1062 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1063
1064                                 /* Device support (drm_fops.h) */
1065 d_open_t drm_open;
1066 d_close_t drm_close;
1067 d_read_t drm_read;
1068 d_kqfilter_t drm_kqfilter;
1069 int drm_release(device_t kdev);
1070
1071 d_mmap_t drm_mmap;
1072 d_mmap_single_t drm_mmap_single;
1073
1074 void drm_cdevpriv_dtor(void *cd);
1075
1076 int drm_add_busid_modesetting(struct drm_device *dev,
1077     struct sysctl_ctx_list *ctx, struct sysctl_oid *top);
1078
1079 /* File operations helpers (drm_fops.c) */
1080 extern int              drm_open_helper(struct cdev *kdev, int flags, int fmt,
1081                                          DRM_STRUCTPROC *p,
1082                                         struct drm_device *dev,
1083                                         struct file *fp);
1084 extern struct drm_file  *drm_find_file_by_proc(struct drm_device *dev,
1085                                         DRM_STRUCTPROC *p);
1086
1087 #ifdef DUMBBELL_WIP
1088 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1089                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1090                 int *prime_fd);
1091 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1092                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1093
1094 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1095                                         struct drm_file *file_priv);
1096 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1097                                         struct drm_file *file_priv);
1098
1099 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, vm_page_t *pages,
1100                                             dma_addr_t *addrs, int max_pages);
1101 #endif /* DUMBBELL_WIP */
1102 extern struct sg_table *drm_prime_pages_to_sg(vm_page_t *pages, int nr_pages);
1103 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1104
1105 #ifdef DUMBBELL_WIP
1106 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1107 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1108 int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
1109 int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle);
1110 void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1111
1112 int drm_prime_add_dma_buf(struct drm_device *dev, struct drm_gem_object *obj);
1113 int drm_prime_lookup_obj(struct drm_device *dev, struct dma_buf *buf,
1114                          struct drm_gem_object **obj);
1115 #endif /* DUMBBELL_WIP */
1116
1117 int     drm_mtrr_add(unsigned long offset, size_t size, int flags);
1118 int     drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags);
1119
1120 /*
1121  * These are exported to drivers so that they can implement fencing using
1122  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1123  */
1124
1125 unsigned long drm_get_resource_start(struct drm_device *dev,
1126                                      unsigned int resource);
1127 unsigned long drm_get_resource_len(struct drm_device *dev,
1128                                    unsigned int resource);
1129
1130                                 /* IRQ support (drm_irq.h) */
1131 extern int drm_irq_install(struct drm_device *dev, int irq);
1132 extern int drm_irq_uninstall(struct drm_device *dev);
1133
1134 extern int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
1135 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1136                            struct drm_file *filp);
1137 extern u32 drm_vblank_count(struct drm_device *dev, int pipe);
1138 extern u32 drm_crtc_vblank_count(struct drm_crtc *crtc);
1139 extern u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1140                                      struct timeval *vblanktime);
1141 extern void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1142                                   struct drm_pending_vblank_event *e);
1143 extern void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1144                                        struct drm_pending_vblank_event *e);
1145 extern bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
1146 extern bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
1147 extern int drm_vblank_get(struct drm_device *dev, unsigned int pipe);
1148 extern void drm_vblank_put(struct drm_device *dev, unsigned int pipe);
1149 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1150 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1151 extern void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
1152 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1153 extern void drm_vblank_off(struct drm_device *dev, unsigned int pipe);
1154 extern void drm_vblank_on(struct drm_device *dev, unsigned int pipe);
1155 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1156 extern void drm_crtc_vblank_reset(struct drm_crtc *crtc);
1157 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1158 extern void drm_vblank_cleanup(struct drm_device *dev);
1159
1160 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1161                                                  unsigned int pipe, int *max_error,
1162                                                  struct timeval *vblank_time,
1163                                                  unsigned flags,
1164                                                  const struct drm_crtc *refcrtc,
1165                                                  const struct drm_display_mode *mode);
1166 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1167                                             const struct drm_display_mode *mode);
1168
1169 /**
1170  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1171  * @crtc: which CRTC's vblank waitqueue to retrieve
1172  *
1173  * This function returns a pointer to the vblank waitqueue for the CRTC.
1174  * Drivers can use this to implement vblank waits using wait_event() & co.
1175  */
1176 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1177 {
1178         return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1179 }
1180
1181 /* Modesetting support */
1182 extern void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe);
1183 extern void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe);
1184
1185 /* sysctl support (drm_sysctl.h) */
1186 extern int              drm_sysctl_init(struct drm_device *dev);
1187 extern int              drm_sysctl_cleanup(struct drm_device *dev);
1188
1189 /* Locking IOCTL support (drm_drv.c) */
1190 int     drm_lock(struct drm_device *dev, void *data,
1191                  struct drm_file *file_priv);
1192 int     drm_unlock(struct drm_device *dev, void *data,
1193                    struct drm_file *file_priv);
1194 int     drm_setversion(struct drm_device *dev, void *data,
1195                        struct drm_file *file_priv);
1196
1197 /*
1198  * These are exported to drivers so that they can implement fencing using
1199  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1200  */
1201
1202 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1203
1204 /* Misc. IOCTL support (drm_ioctl.c) */
1205 int     drm_noop(struct drm_device *dev, void *data,
1206                  struct drm_file *file_priv);
1207
1208 /* Cache management (drm_cache.c) */
1209 void drm_clflush_pages(struct vm_page *pages[], unsigned long num_pages);
1210 void drm_clflush_sg(struct sg_table *st);
1211 void drm_clflush_virt_range(void *addr, unsigned long length);
1212
1213 /* DMA support (drm_dma.c) */
1214 int     drm_dma(struct drm_device *dev, void *data, struct drm_file *file_priv);
1215
1216                                 /* Stub support (drm_stub.h) */
1217
1218 extern void drm_put_dev(struct drm_device *dev);
1219 extern void drm_unplug_dev(struct drm_device *dev);
1220 extern unsigned int drm_debug;
1221 extern bool drm_atomic;
1222
1223 /* Scatter Gather Support (drm_scatter.c) */
1224 int     drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1225                            struct drm_file *file_priv);
1226 int     drm_sg_free(struct drm_device *dev, void *data,
1227                     struct drm_file *file_priv);
1228
1229 /* consistent PCI memory functions (drm_pci.c) */
1230 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1231                                             size_t align);
1232 void    drm_pci_free(struct drm_device *dev, struct drm_dma_handle *dmah);
1233
1234                                /* sysfs support (drm_sysfs.c) */
1235 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1236
1237 int drm_gem_mmap_single(struct drm_device *dev, vm_ooffset_t *offset,
1238     vm_size_t size, struct vm_object **obj_res, int nprot);
1239 void drm_gem_pager_dtr(void *obj);
1240
1241 struct ttm_bo_device;
1242 int ttm_bo_mmap_single(struct ttm_bo_device *bdev, vm_ooffset_t *offset,
1243     vm_size_t size, struct vm_object **obj_res, int nprot);
1244 struct ttm_buffer_object;
1245 void ttm_bo_release_mmap(struct ttm_buffer_object *bo);
1246
1247 void drm_device_lock_mtx(struct drm_device *dev);
1248 void drm_device_unlock_mtx(struct drm_device *dev);
1249 int drm_device_sleep_mtx(struct drm_device *dev, void *chan, int flags,
1250     const char *msg, int timeout);
1251 void drm_device_assert_mtx_locked(struct drm_device *dev);
1252 void drm_device_assert_mtx_unlocked(struct drm_device *dev);
1253
1254 void drm_device_lock_struct(struct drm_device *dev);
1255 void drm_device_unlock_struct(struct drm_device *dev);
1256 int drm_device_sleep_struct(struct drm_device *dev, void *chan, int flags,
1257     const char *msg, int timeout);
1258 void drm_device_assert_struct_locked(struct drm_device *dev);
1259 void drm_device_assert_struct_unlocked(struct drm_device *dev);
1260
1261 void drm_compat_locking_init(struct drm_device *dev);
1262 void drm_sleep_locking_init(struct drm_device *dev);
1263
1264 /* XXX glue logic, should be done in drm_pci_init(), pending drm update */
1265 void drm_init_pdev(struct device *dev, struct pci_dev **pdev);
1266 void drm_fini_pdev(struct pci_dev **pdev);
1267
1268 /* Inline replacements for drm_alloc and friends */
1269 static __inline__ void *
1270 drm_alloc(size_t size, struct malloc_type *area)
1271 {
1272         return kmalloc(size, area, M_WAITOK | M_NULLOK);
1273 }
1274
1275 static __inline__ void *
1276 drm_calloc(size_t nmemb, size_t size, struct malloc_type *area)
1277 {
1278         return kmalloc(size * nmemb, area, M_WAITOK | M_NULLOK | M_ZERO);
1279 }
1280
1281 static __inline__ void
1282 drm_free(void *pt, struct malloc_type *area)
1283 {
1284         /* kfree is special!!! */
1285         if (pt != NULL)
1286                 (kfree)(pt, area);
1287 }
1288
1289 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1290                                  struct device *parent);
1291 void drm_dev_ref(struct drm_device *dev);
1292 void drm_dev_unref(struct drm_device *dev);
1293 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1294 void drm_dev_unregister(struct drm_device *dev);
1295 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1296
1297 /* FreeBSD compatibility macros */
1298 #define VM_OBJECT_WLOCK(object)         VM_OBJECT_LOCK(object)
1299 #define VM_OBJECT_WUNLOCK(object)       VM_OBJECT_UNLOCK(object)
1300
1301 /* PCI section */
1302 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1303 {
1304         if (dev->driver->device_is_agp != NULL) {
1305                 int err = (*dev->driver->device_is_agp) (dev);
1306
1307                 if (err != 2) {
1308                         return err;
1309                 }
1310         }
1311
1312         return (pci_find_extcap(dev->dev, PCIY_AGP, NULL) == 0);
1313 }
1314
1315 #define DRM_PCIE_SPEED_25 1
1316 #define DRM_PCIE_SPEED_50 2
1317 #define DRM_PCIE_SPEED_80 4
1318
1319 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1320
1321 /* XXX bad */
1322 #define drm_can_sleep() (HZ & 1)
1323
1324 #endif /* __KERNEL__ */
1325 #endif /* _DRM_P_H_ */