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