drm: Move DRM_MAGIC_HASH_ORDER into drm_drv.c
[cascardo/linux.git] / 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/sched.h>
55 #include <linux/slab.h>
56 #include <linux/types.h>
57 #include <linux/vmalloc.h>
58 #include <linux/workqueue.h>
59
60 #include <asm/mman.h>
61 #include <asm/pgalloc.h>
62 #include <asm/uaccess.h>
63
64 #include <uapi/drm/drm.h>
65 #include <uapi/drm/drm_mode.h>
66
67 #include <drm/drm_agpsupport.h>
68 #include <drm/drm_crtc.h>
69 #include <drm/drm_global.h>
70 #include <drm/drm_hashtab.h>
71 #include <drm/drm_mem_util.h>
72 #include <drm/drm_mm.h>
73 #include <drm/drm_os_linux.h>
74 #include <drm/drm_sarea.h>
75 #include <drm/drm_vma_manager.h>
76
77 struct module;
78
79 struct drm_file;
80 struct drm_device;
81 struct drm_agp_head;
82 struct drm_local_map;
83 struct drm_device_dma;
84 struct drm_dma_handle;
85
86 struct device_node;
87 struct videomode;
88 struct reservation_object;
89
90 /*
91  * 4 debug categories are defined:
92  *
93  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
94  *       This is the category used by the DRM_DEBUG() macro.
95  *
96  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
97  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
98  *
99  * KMS: used in the modesetting code.
100  *      This is the category used by the DRM_DEBUG_KMS() macro.
101  *
102  * PRIME: used in the prime code.
103  *        This is the category used by the DRM_DEBUG_PRIME() macro.
104  *
105  * Enabling verbose debug messages is done through the drm.debug parameter,
106  * each category being enabled by a bit.
107  *
108  * drm.debug=0x1 will enable CORE messages
109  * drm.debug=0x2 will enable DRIVER messages
110  * drm.debug=0x3 will enable CORE and DRIVER messages
111  * ...
112  * drm.debug=0xf will enable all messages
113  *
114  * An interesting feature is that it's possible to enable verbose logging at
115  * run-time by echoing the debug value in its sysfs node:
116  *   # echo 0xf > /sys/module/drm/parameters/debug
117  */
118 #define DRM_UT_CORE             0x01
119 #define DRM_UT_DRIVER           0x02
120 #define DRM_UT_KMS              0x04
121 #define DRM_UT_PRIME            0x08
122
123 extern __printf(2, 3)
124 void drm_ut_debug_printk(const char *function_name,
125                          const char *format, ...);
126 extern __printf(2, 3)
127 int drm_err(const char *func, const char *format, ...);
128
129 /***********************************************************************/
130 /** \name DRM template customization defaults */
131 /*@{*/
132
133 /* driver capabilities and requirements mask */
134 #define DRIVER_USE_AGP     0x1
135 #define DRIVER_PCI_DMA     0x8
136 #define DRIVER_SG          0x10
137 #define DRIVER_HAVE_DMA    0x20
138 #define DRIVER_HAVE_IRQ    0x40
139 #define DRIVER_IRQ_SHARED  0x80
140 #define DRIVER_GEM         0x1000
141 #define DRIVER_MODESET     0x2000
142 #define DRIVER_PRIME       0x4000
143 #define DRIVER_RENDER      0x8000
144
145 /***********************************************************************/
146 /** \name Macros to make printk easier */
147 /*@{*/
148
149 /**
150  * Error output.
151  *
152  * \param fmt printf() like format string.
153  * \param arg arguments
154  */
155 #define DRM_ERROR(fmt, ...)                             \
156         drm_err(__func__, fmt, ##__VA_ARGS__)
157
158 /**
159  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
160  *
161  * \param fmt printf() like format string.
162  * \param arg arguments
163  */
164 #define DRM_ERROR_RATELIMITED(fmt, ...)                         \
165 ({                                                                      \
166         static DEFINE_RATELIMIT_STATE(_rs,                              \
167                                       DEFAULT_RATELIMIT_INTERVAL,       \
168                                       DEFAULT_RATELIMIT_BURST);         \
169                                                                         \
170         if (__ratelimit(&_rs))                                          \
171                 drm_err(__func__, fmt, ##__VA_ARGS__);                  \
172 })
173
174 #define DRM_INFO(fmt, ...)                              \
175         printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
176
177 #define DRM_INFO_ONCE(fmt, ...)                         \
178         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
179
180 /**
181  * Debug output.
182  *
183  * \param fmt printf() like format string.
184  * \param arg arguments
185  */
186 #define DRM_DEBUG(fmt, args...)                                         \
187         do {                                                            \
188                 if (unlikely(drm_debug & DRM_UT_CORE))                  \
189                         drm_ut_debug_printk(__func__, fmt, ##args);     \
190         } while (0)
191
192 #define DRM_DEBUG_DRIVER(fmt, args...)                                  \
193         do {                                                            \
194                 if (unlikely(drm_debug & DRM_UT_DRIVER))                \
195                         drm_ut_debug_printk(__func__, fmt, ##args);     \
196         } while (0)
197 #define DRM_DEBUG_KMS(fmt, args...)                                     \
198         do {                                                            \
199                 if (unlikely(drm_debug & DRM_UT_KMS))                   \
200                         drm_ut_debug_printk(__func__, fmt, ##args);     \
201         } while (0)
202 #define DRM_DEBUG_PRIME(fmt, args...)                                   \
203         do {                                                            \
204                 if (unlikely(drm_debug & DRM_UT_PRIME))                 \
205                         drm_ut_debug_printk(__func__, fmt, ##args);     \
206         } while (0)
207
208 /*@}*/
209
210 /***********************************************************************/
211 /** \name Internal types and structures */
212 /*@{*/
213
214 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
215
216 /**
217  * Ioctl function type.
218  *
219  * \param inode device inode.
220  * \param file_priv DRM file private pointer.
221  * \param cmd command.
222  * \param arg argument.
223  */
224 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
225                         struct drm_file *file_priv);
226
227 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
228                                unsigned long arg);
229
230 #define DRM_IOCTL_NR(n)                _IOC_NR(n)
231 #define DRM_MAJOR       226
232
233 #define DRM_AUTH        0x1
234 #define DRM_MASTER      0x2
235 #define DRM_ROOT_ONLY   0x4
236 #define DRM_CONTROL_ALLOW 0x8
237 #define DRM_UNLOCKED    0x10
238 #define DRM_RENDER_ALLOW 0x20
239
240 struct drm_ioctl_desc {
241         unsigned int cmd;
242         int flags;
243         drm_ioctl_t *func;
244         unsigned int cmd_drv;
245         const char *name;
246 };
247
248 /**
249  * Creates a driver or general drm_ioctl_desc array entry for the given
250  * ioctl, for use by drm_ioctl().
251  */
252
253 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                 \
254         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl, .name = #ioctl}
255
256 /* Event queued up for userspace to read */
257 struct drm_pending_event {
258         struct drm_event *event;
259         struct list_head link;
260         struct drm_file *file_priv;
261         pid_t pid; /* pid of requester, no guarantee it's valid by the time
262                       we deliver the event, for tracing only */
263         void (*destroy)(struct drm_pending_event *event);
264 };
265
266 /* initial implementaton using a linked list - todo hashtab */
267 struct drm_prime_file_private {
268         struct list_head head;
269         struct mutex lock;
270 };
271
272 /** File private data */
273 struct drm_file {
274         unsigned authenticated :1;
275         /* Whether we're master for a minor. Protected by master_mutex */
276         unsigned is_master :1;
277         /* true when the client has asked us to expose stereo 3D mode flags */
278         unsigned stereo_allowed :1;
279         /*
280          * true if client understands CRTC primary planes and cursor planes
281          * in the plane list
282          */
283         unsigned universal_planes:1;
284
285         struct pid *pid;
286         kuid_t uid;
287         drm_magic_t magic;
288         struct list_head lhead;
289         struct drm_minor *minor;
290         unsigned long lock_count;
291
292         /** Mapping of mm object handles to object pointers. */
293         struct idr object_idr;
294         /** Lock for synchronization of access to object_idr. */
295         spinlock_t table_lock;
296
297         struct file *filp;
298         void *driver_priv;
299
300         struct drm_master *master; /* master this node is currently associated with
301                                       N.B. not always minor->master */
302         /**
303          * fbs - List of framebuffers associated with this file.
304          *
305          * Protected by fbs_lock. Note that the fbs list holds a reference on
306          * the fb object to prevent it from untimely disappearing.
307          */
308         struct list_head fbs;
309         struct mutex fbs_lock;
310
311         wait_queue_head_t event_wait;
312         struct list_head event_list;
313         int event_space;
314
315         struct drm_prime_file_private prime;
316 };
317
318 /**
319  * Lock data.
320  */
321 struct drm_lock_data {
322         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
323         /** Private of lock holder's file (NULL=kernel) */
324         struct drm_file *file_priv;
325         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
326         unsigned long lock_time;        /**< Time of last lock in jiffies */
327         spinlock_t spinlock;
328         uint32_t kernel_waiters;
329         uint32_t user_waiters;
330         int idle_has_lock;
331 };
332
333 /**
334  * This structure defines the drm_mm memory object, which will be used by the
335  * DRM for its buffer objects.
336  */
337 struct drm_gem_object {
338         /** Reference count of this object */
339         struct kref refcount;
340
341         /**
342          * handle_count - gem file_priv handle count of this object
343          *
344          * Each handle also holds a reference. Note that when the handle_count
345          * drops to 0 any global names (e.g. the id in the flink namespace) will
346          * be cleared.
347          *
348          * Protected by dev->object_name_lock.
349          * */
350         unsigned handle_count;
351
352         /** Related drm device */
353         struct drm_device *dev;
354
355         /** File representing the shmem storage */
356         struct file *filp;
357
358         /* Mapping info for this object */
359         struct drm_vma_offset_node vma_node;
360
361         /**
362          * Size of the object, in bytes.  Immutable over the object's
363          * lifetime.
364          */
365         size_t size;
366
367         /**
368          * Global name for this object, starts at 1. 0 means unnamed.
369          * Access is covered by the object_name_lock in the related drm_device
370          */
371         int name;
372
373         /**
374          * Memory domains. These monitor which caches contain read/write data
375          * related to the object. When transitioning from one set of domains
376          * to another, the driver is called to ensure that caches are suitably
377          * flushed and invalidated
378          */
379         uint32_t read_domains;
380         uint32_t write_domain;
381
382         /**
383          * While validating an exec operation, the
384          * new read/write domain values are computed here.
385          * They will be transferred to the above values
386          * at the point that any cache flushing occurs
387          */
388         uint32_t pending_read_domains;
389         uint32_t pending_write_domain;
390
391         /**
392          * dma_buf - dma buf associated with this GEM object
393          *
394          * Pointer to the dma-buf associated with this gem object (either
395          * through importing or exporting). We break the resulting reference
396          * loop when the last gem handle for this object is released.
397          *
398          * Protected by obj->object_name_lock
399          */
400         struct dma_buf *dma_buf;
401
402         /**
403          * import_attach - dma buf attachment backing this object
404          *
405          * Any foreign dma_buf imported as a gem object has this set to the
406          * attachment point for the device. This is invariant over the lifetime
407          * of a gem object.
408          *
409          * The driver's ->gem_free_object callback is responsible for cleaning
410          * up the dma_buf attachment and references acquired at import time.
411          *
412          * Note that the drm gem/prime core does not depend upon drivers setting
413          * this field any more. So for drivers where this doesn't make sense
414          * (e.g. virtual devices or a displaylink behind an usb bus) they can
415          * simply leave it as NULL.
416          */
417         struct dma_buf_attachment *import_attach;
418 };
419
420 /**
421  * struct drm_master - drm master structure
422  *
423  * @refcount: Refcount for this master object.
424  * @minor: Link back to minor char device we are master for. Immutable.
425  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
426  * @unique_len: Length of unique field. Protected by drm_global_mutex.
427  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
428  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
429  * @lock: DRI lock information.
430  * @driver_priv: Pointer to driver-private information.
431  */
432 struct drm_master {
433         struct kref refcount;
434         struct drm_minor *minor;
435         char *unique;
436         int unique_len;
437         struct drm_open_hash magiclist;
438         struct list_head magicfree;
439         struct drm_lock_data lock;
440         void *driver_priv;
441 };
442
443 /* Size of ringbuffer for vblank timestamps. Just double-buffer
444  * in initial implementation.
445  */
446 #define DRM_VBLANKTIME_RBSIZE 2
447
448 /* Flags and return codes for get_vblank_timestamp() driver function. */
449 #define DRM_CALLED_FROM_VBLIRQ 1
450 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
451 #define DRM_VBLANKTIME_IN_VBLANK         (1 << 1)
452
453 /* get_scanout_position() return flags */
454 #define DRM_SCANOUTPOS_VALID        (1 << 0)
455 #define DRM_SCANOUTPOS_IN_VBLANK    (1 << 1)
456 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
457
458 /**
459  * DRM driver structure. This structure represent the common code for
460  * a family of cards. There will one drm_device for each card present
461  * in this family
462  */
463 struct drm_driver {
464         int (*load) (struct drm_device *, unsigned long flags);
465         int (*firstopen) (struct drm_device *);
466         int (*open) (struct drm_device *, struct drm_file *);
467         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
468         void (*postclose) (struct drm_device *, struct drm_file *);
469         void (*lastclose) (struct drm_device *);
470         int (*unload) (struct drm_device *);
471         int (*suspend) (struct drm_device *, pm_message_t state);
472         int (*resume) (struct drm_device *);
473         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
474         int (*dma_quiescent) (struct drm_device *);
475         int (*context_dtor) (struct drm_device *dev, int context);
476         int (*set_busid)(struct drm_device *dev, struct drm_master *master);
477
478         /**
479          * get_vblank_counter - get raw hardware vblank counter
480          * @dev: DRM device
481          * @crtc: counter to fetch
482          *
483          * Driver callback for fetching a raw hardware vblank counter for @crtc.
484          * If a device doesn't have a hardware counter, the driver can simply
485          * return the value of drm_vblank_count. The DRM core will account for
486          * missed vblank events while interrupts where disabled based on system
487          * timestamps.
488          *
489          * Wraparound handling and loss of events due to modesetting is dealt
490          * with in the DRM core code.
491          *
492          * RETURNS
493          * Raw vblank counter value.
494          */
495         u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
496
497         /**
498          * enable_vblank - enable vblank interrupt events
499          * @dev: DRM device
500          * @crtc: which irq to enable
501          *
502          * Enable vblank interrupts for @crtc.  If the device doesn't have
503          * a hardware vblank counter, this routine should be a no-op, since
504          * interrupts will have to stay on to keep the count accurate.
505          *
506          * RETURNS
507          * Zero on success, appropriate errno if the given @crtc's vblank
508          * interrupt cannot be enabled.
509          */
510         int (*enable_vblank) (struct drm_device *dev, int crtc);
511
512         /**
513          * disable_vblank - disable vblank interrupt events
514          * @dev: DRM device
515          * @crtc: which irq to enable
516          *
517          * Disable vblank interrupts for @crtc.  If the device doesn't have
518          * a hardware vblank counter, this routine should be a no-op, since
519          * interrupts will have to stay on to keep the count accurate.
520          */
521         void (*disable_vblank) (struct drm_device *dev, int crtc);
522
523         /**
524          * Called by \c drm_device_is_agp.  Typically used to determine if a
525          * card is really attached to AGP or not.
526          *
527          * \param dev  DRM device handle
528          *
529          * \returns
530          * One of three values is returned depending on whether or not the
531          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
532          * (return of 1), or may or may not be AGP (return of 2).
533          */
534         int (*device_is_agp) (struct drm_device *dev);
535
536         /**
537          * Called by vblank timestamping code.
538          *
539          * Return the current display scanout position from a crtc, and an
540          * optional accurate ktime_get timestamp of when position was measured.
541          *
542          * \param dev  DRM device.
543          * \param crtc Id of the crtc to query.
544          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
545          * \param *vpos Target location for current vertical scanout position.
546          * \param *hpos Target location for current horizontal scanout position.
547          * \param *stime Target location for timestamp taken immediately before
548          *               scanout position query. Can be NULL to skip timestamp.
549          * \param *etime Target location for timestamp taken immediately after
550          *               scanout position query. Can be NULL to skip timestamp.
551          *
552          * Returns vpos as a positive number while in active scanout area.
553          * Returns vpos as a negative number inside vblank, counting the number
554          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
555          * until start of active scanout / end of vblank."
556          *
557          * \return Flags, or'ed together as follows:
558          *
559          * DRM_SCANOUTPOS_VALID = Query successful.
560          * DRM_SCANOUTPOS_INVBL = Inside vblank.
561          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
562          * this flag means that returned position may be offset by a constant
563          * but unknown small number of scanlines wrt. real scanout position.
564          *
565          */
566         int (*get_scanout_position) (struct drm_device *dev, int crtc,
567                                      unsigned int flags,
568                                      int *vpos, int *hpos, ktime_t *stime,
569                                      ktime_t *etime);
570
571         /**
572          * Called by \c drm_get_last_vbltimestamp. Should return a precise
573          * timestamp when the most recent VBLANK interval ended or will end.
574          *
575          * Specifically, the timestamp in @vblank_time should correspond as
576          * closely as possible to the time when the first video scanline of
577          * the video frame after the end of VBLANK will start scanning out,
578          * the time immediately after end of the VBLANK interval. If the
579          * @crtc is currently inside VBLANK, this will be a time in the future.
580          * If the @crtc is currently scanning out a frame, this will be the
581          * past start time of the current scanout. This is meant to adhere
582          * to the OpenML OML_sync_control extension specification.
583          *
584          * \param dev dev DRM device handle.
585          * \param crtc crtc for which timestamp should be returned.
586          * \param *max_error Maximum allowable timestamp error in nanoseconds.
587          *                   Implementation should strive to provide timestamp
588          *                   with an error of at most *max_error nanoseconds.
589          *                   Returns true upper bound on error for timestamp.
590          * \param *vblank_time Target location for returned vblank timestamp.
591          * \param flags 0 = Defaults, no special treatment needed.
592          * \param       DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
593          *              irq handler. Some drivers need to apply some workarounds
594          *              for gpu-specific vblank irq quirks if flag is set.
595          *
596          * \returns
597          * Zero if timestamping isn't supported in current display mode or a
598          * negative number on failure. A positive status code on success,
599          * which describes how the vblank_time timestamp was computed.
600          */
601         int (*get_vblank_timestamp) (struct drm_device *dev, int crtc,
602                                      int *max_error,
603                                      struct timeval *vblank_time,
604                                      unsigned flags);
605
606         /* these have to be filled in */
607
608         irqreturn_t(*irq_handler) (int irq, void *arg);
609         void (*irq_preinstall) (struct drm_device *dev);
610         int (*irq_postinstall) (struct drm_device *dev);
611         void (*irq_uninstall) (struct drm_device *dev);
612
613         /* Master routines */
614         int (*master_create)(struct drm_device *dev, struct drm_master *master);
615         void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
616         /**
617          * master_set is called whenever the minor master is set.
618          * master_drop is called whenever the minor master is dropped.
619          */
620
621         int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
622                           bool from_open);
623         void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
624                             bool from_release);
625
626         int (*debugfs_init)(struct drm_minor *minor);
627         void (*debugfs_cleanup)(struct drm_minor *minor);
628
629         /**
630          * Driver-specific constructor for drm_gem_objects, to set up
631          * obj->driver_private.
632          *
633          * Returns 0 on success.
634          */
635         void (*gem_free_object) (struct drm_gem_object *obj);
636         int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
637         void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
638
639         /* prime: */
640         /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
641         int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
642                                 uint32_t handle, uint32_t flags, int *prime_fd);
643         /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
644         int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
645                                 int prime_fd, uint32_t *handle);
646         /* export GEM -> dmabuf */
647         struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
648                                 struct drm_gem_object *obj, int flags);
649         /* import dmabuf -> GEM */
650         struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
651                                 struct dma_buf *dma_buf);
652         /* low-level interface used by drm_gem_prime_{import,export} */
653         int (*gem_prime_pin)(struct drm_gem_object *obj);
654         void (*gem_prime_unpin)(struct drm_gem_object *obj);
655         struct reservation_object * (*gem_prime_res_obj)(
656                                 struct drm_gem_object *obj);
657         struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
658         struct drm_gem_object *(*gem_prime_import_sg_table)(
659                                 struct drm_device *dev, size_t size,
660                                 struct sg_table *sgt);
661         void *(*gem_prime_vmap)(struct drm_gem_object *obj);
662         void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
663         int (*gem_prime_mmap)(struct drm_gem_object *obj,
664                                 struct vm_area_struct *vma);
665
666         /* vga arb irq handler */
667         void (*vgaarb_irq)(struct drm_device *dev, bool state);
668
669         /* dumb alloc support */
670         int (*dumb_create)(struct drm_file *file_priv,
671                            struct drm_device *dev,
672                            struct drm_mode_create_dumb *args);
673         int (*dumb_map_offset)(struct drm_file *file_priv,
674                                struct drm_device *dev, uint32_t handle,
675                                uint64_t *offset);
676         int (*dumb_destroy)(struct drm_file *file_priv,
677                             struct drm_device *dev,
678                             uint32_t handle);
679
680         /* Driver private ops for this object */
681         const struct vm_operations_struct *gem_vm_ops;
682
683         int major;
684         int minor;
685         int patchlevel;
686         char *name;
687         char *desc;
688         char *date;
689
690         u32 driver_features;
691         int dev_priv_size;
692         const struct drm_ioctl_desc *ioctls;
693         int num_ioctls;
694         const struct file_operations *fops;
695
696         /* List of devices hanging off this driver with stealth attach. */
697         struct list_head legacy_dev_list;
698 };
699
700 enum drm_minor_type {
701         DRM_MINOR_LEGACY,
702         DRM_MINOR_CONTROL,
703         DRM_MINOR_RENDER,
704         DRM_MINOR_CNT,
705 };
706
707 /**
708  * Info file list entry. This structure represents a debugfs or proc file to
709  * be created by the drm core
710  */
711 struct drm_info_list {
712         const char *name; /** file name */
713         int (*show)(struct seq_file*, void*); /** show callback */
714         u32 driver_features; /**< Required driver features for this entry */
715         void *data;
716 };
717
718 /**
719  * debugfs node structure. This structure represents a debugfs file.
720  */
721 struct drm_info_node {
722         struct list_head list;
723         struct drm_minor *minor;
724         const struct drm_info_list *info_ent;
725         struct dentry *dent;
726 };
727
728 /**
729  * DRM minor structure. This structure represents a drm minor number.
730  */
731 struct drm_minor {
732         int index;                      /**< Minor device number */
733         int type;                       /**< Control or render */
734         struct device *kdev;            /**< Linux device */
735         struct drm_device *dev;
736
737         struct dentry *debugfs_root;
738
739         struct list_head debugfs_list;
740         struct mutex debugfs_lock; /* Protects debugfs_list. */
741
742         /* currently active master for this node. Protected by master_mutex */
743         struct drm_master *master;
744         struct drm_mode_group mode_group;
745 };
746
747
748 struct drm_pending_vblank_event {
749         struct drm_pending_event base;
750         int pipe;
751         struct drm_event_vblank event;
752 };
753
754 struct drm_vblank_crtc {
755         struct drm_device *dev;         /* pointer to the drm_device */
756         wait_queue_head_t queue;        /**< VBLANK wait queue */
757         struct timeval time[DRM_VBLANKTIME_RBSIZE];     /**< timestamp of current count */
758         struct timer_list disable_timer;                /* delayed disable timer */
759         atomic_t count;                 /**< number of VBLANK interrupts */
760         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
761         u32 last;                       /* protected by dev->vbl_lock, used */
762                                         /* for wraparound handling */
763         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
764         unsigned int inmodeset;         /* Display driver is setting mode */
765         int crtc;                       /* crtc index */
766         bool enabled;                   /* so we don't call enable more than
767                                            once per disable */
768 };
769
770 /**
771  * DRM device structure. This structure represent a complete card that
772  * may contain multiple heads.
773  */
774 struct drm_device {
775         struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
776         int if_version;                 /**< Highest interface version set */
777
778         /** \name Lifetime Management */
779         /*@{ */
780         struct kref ref;                /**< Object ref-count */
781         struct device *dev;             /**< Device structure of bus-device */
782         struct drm_driver *driver;      /**< DRM driver managing the device */
783         void *dev_private;              /**< DRM driver private data */
784         struct drm_minor *control;              /**< Control node */
785         struct drm_minor *primary;              /**< Primary node */
786         struct drm_minor *render;               /**< Render node */
787         atomic_t unplugged;                     /**< Flag whether dev is dead */
788         struct inode *anon_inode;               /**< inode for private address-space */
789         char *unique;                           /**< unique name of the device */
790         /*@} */
791
792         /** \name Locks */
793         /*@{ */
794         struct mutex struct_mutex;      /**< For others */
795         struct mutex master_mutex;      /**< For drm_minor::master and drm_file::is_master */
796         /*@} */
797
798         /** \name Usage Counters */
799         /*@{ */
800         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
801         spinlock_t buf_lock;            /**< For drm_device::buf_use and a few other things. */
802         int buf_use;                    /**< Buffers in use -- cannot alloc */
803         atomic_t buf_alloc;             /**< Buffer allocation in progress */
804         /*@} */
805
806         struct list_head filelist;
807
808         /** \name Memory management */
809         /*@{ */
810         struct list_head maplist;       /**< Linked list of regions */
811         struct drm_open_hash map_hash;  /**< User token hash table for maps */
812
813         /** \name Context handle management */
814         /*@{ */
815         struct list_head ctxlist;       /**< Linked list of context handles */
816         struct mutex ctxlist_mutex;     /**< For ctxlist */
817
818         struct idr ctx_idr;
819
820         struct list_head vmalist;       /**< List of vmas (for debugging) */
821
822         /*@} */
823
824         /** \name DMA support */
825         /*@{ */
826         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
827         /*@} */
828
829         /** \name Context support */
830         /*@{ */
831         bool irq_enabled;               /**< True if irq handler is enabled */
832         int irq;
833
834         __volatile__ long context_flag; /**< Context swapping flag */
835         int last_context;               /**< Last current context */
836         /*@} */
837
838         /** \name VBLANK IRQ support */
839         /*@{ */
840
841         /*
842          * At load time, disabling the vblank interrupt won't be allowed since
843          * old clients may not call the modeset ioctl and therefore misbehave.
844          * Once the modeset ioctl *has* been called though, we can safely
845          * disable them when unused.
846          */
847         bool vblank_disable_allowed;
848
849         /*
850          * If true, vblank interrupt will be disabled immediately when the
851          * refcount drops to zero, as opposed to via the vblank disable
852          * timer.
853          * This can be set to true it the hardware has a working vblank
854          * counter and the driver uses drm_vblank_on() and drm_vblank_off()
855          * appropriately.
856          */
857         bool vblank_disable_immediate;
858
859         /* array of size num_crtcs */
860         struct drm_vblank_crtc *vblank;
861
862         spinlock_t vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
863         spinlock_t vbl_lock;
864
865         u32 max_vblank_count;           /**< size of vblank counter register */
866
867         /**
868          * List of events
869          */
870         struct list_head vblank_event_list;
871         spinlock_t event_lock;
872
873         /*@} */
874
875         struct drm_agp_head *agp;       /**< AGP data */
876
877         struct pci_dev *pdev;           /**< PCI device structure */
878 #ifdef __alpha__
879         struct pci_controller *hose;
880 #endif
881
882         struct platform_device *platformdev; /**< Platform device struture */
883
884         struct drm_sg_mem *sg;  /**< Scatter gather memory */
885         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
886         sigset_t sigmask;
887
888         struct {
889                 int context;
890                 struct drm_hw_lock *lock;
891         } sigdata;
892
893         struct drm_local_map *agp_buffer_map;
894         unsigned int agp_buffer_token;
895
896         struct drm_mode_config mode_config;     /**< Current mode config */
897
898         /** \name GEM information */
899         /*@{ */
900         struct mutex object_name_lock;
901         struct idr object_name_idr;
902         struct drm_vma_offset_manager *vma_offset_manager;
903         /*@} */
904         int switch_power_state;
905 };
906
907 #define DRM_SWITCH_POWER_ON 0
908 #define DRM_SWITCH_POWER_OFF 1
909 #define DRM_SWITCH_POWER_CHANGING 2
910 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
911
912 static __inline__ int drm_core_check_feature(struct drm_device *dev,
913                                              int feature)
914 {
915         return ((dev->driver->driver_features & feature) ? 1 : 0);
916 }
917
918 static inline void drm_device_set_unplugged(struct drm_device *dev)
919 {
920         smp_wmb();
921         atomic_set(&dev->unplugged, 1);
922 }
923
924 static inline int drm_device_is_unplugged(struct drm_device *dev)
925 {
926         int ret = atomic_read(&dev->unplugged);
927         smp_rmb();
928         return ret;
929 }
930
931 static inline bool drm_is_render_client(const struct drm_file *file_priv)
932 {
933         return file_priv->minor->type == DRM_MINOR_RENDER;
934 }
935
936 static inline bool drm_is_control_client(const struct drm_file *file_priv)
937 {
938         return file_priv->minor->type == DRM_MINOR_CONTROL;
939 }
940
941 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
942 {
943         return file_priv->minor->type == DRM_MINOR_LEGACY;
944 }
945
946 /******************************************************************/
947 /** \name Internal function definitions */
948 /*@{*/
949
950                                 /* Driver support (drm_drv.h) */
951 extern long drm_ioctl(struct file *filp,
952                       unsigned int cmd, unsigned long arg);
953 extern long drm_compat_ioctl(struct file *filp,
954                              unsigned int cmd, unsigned long arg);
955 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
956
957                                 /* Device support (drm_fops.h) */
958 extern int drm_open(struct inode *inode, struct file *filp);
959 extern ssize_t drm_read(struct file *filp, char __user *buffer,
960                         size_t count, loff_t *offset);
961 extern int drm_release(struct inode *inode, struct file *filp);
962
963                                 /* Mapping support (drm_vm.h) */
964 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
965 extern void drm_vm_open_locked(struct drm_device *dev, struct vm_area_struct *vma);
966 extern unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
967
968 /* Misc. IOCTL support (drm_ioctl.c) */
969 int drm_noop(struct drm_device *dev, void *data,
970              struct drm_file *file_priv);
971
972 /* Cache management (drm_cache.c) */
973 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
974 void drm_clflush_sg(struct sg_table *st);
975 void drm_clflush_virt_range(void *addr, unsigned long length);
976
977 /*
978  * These are exported to drivers so that they can implement fencing using
979  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
980  */
981
982                                 /* IRQ support (drm_irq.h) */
983 extern int drm_irq_install(struct drm_device *dev, int irq);
984 extern int drm_irq_uninstall(struct drm_device *dev);
985
986 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
987 extern int drm_wait_vblank(struct drm_device *dev, void *data,
988                            struct drm_file *filp);
989 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
990 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
991                                      struct timeval *vblanktime);
992 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
993                                      struct drm_pending_vblank_event *e);
994 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
995 extern int drm_vblank_get(struct drm_device *dev, int crtc);
996 extern void drm_vblank_put(struct drm_device *dev, int crtc);
997 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
998 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
999 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
1000 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1001 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1002 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1003 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1004 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1005 extern void drm_vblank_cleanup(struct drm_device *dev);
1006
1007 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1008                                                  int crtc, int *max_error,
1009                                                  struct timeval *vblank_time,
1010                                                  unsigned flags,
1011                                                  const struct drm_crtc *refcrtc,
1012                                                  const struct drm_display_mode *mode);
1013 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1014                                             const struct drm_display_mode *mode);
1015
1016 /**
1017  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1018  * @crtc: which CRTC's vblank waitqueue to retrieve
1019  *
1020  * This function returns a pointer to the vblank waitqueue for the CRTC.
1021  * Drivers can use this to implement vblank waits using wait_event() & co.
1022  */
1023 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1024 {
1025         return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1026 }
1027
1028 /* Modesetting support */
1029 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1030 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1031 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1032                            struct drm_file *file_priv);
1033
1034                                 /* Stub support (drm_stub.h) */
1035 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1036                                struct drm_file *file_priv);
1037 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1038                                 struct drm_file *file_priv);
1039 struct drm_master *drm_master_create(struct drm_minor *minor);
1040 extern struct drm_master *drm_master_get(struct drm_master *master);
1041 extern void drm_master_put(struct drm_master **master);
1042
1043 extern void drm_put_dev(struct drm_device *dev);
1044 extern void drm_unplug_dev(struct drm_device *dev);
1045 extern unsigned int drm_debug;
1046
1047                                 /* Debugfs support */
1048 #if defined(CONFIG_DEBUG_FS)
1049 extern int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1050                             struct dentry *root);
1051 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1052                                     int count, struct dentry *root,
1053                                     struct drm_minor *minor);
1054 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1055                                     int count, struct drm_minor *minor);
1056 extern int drm_debugfs_cleanup(struct drm_minor *minor);
1057 extern int drm_debugfs_connector_add(struct drm_connector *connector);
1058 extern void drm_debugfs_connector_remove(struct drm_connector *connector);
1059 #else
1060 static inline int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1061                                    struct dentry *root)
1062 {
1063         return 0;
1064 }
1065
1066 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1067                                            int count, struct dentry *root,
1068                                            struct drm_minor *minor)
1069 {
1070         return 0;
1071 }
1072
1073 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1074                                            int count, struct drm_minor *minor)
1075 {
1076         return 0;
1077 }
1078
1079 static inline int drm_debugfs_cleanup(struct drm_minor *minor)
1080 {
1081         return 0;
1082 }
1083
1084 static inline int drm_debugfs_connector_add(struct drm_connector *connector)
1085 {
1086         return 0;
1087 }
1088 static inline void drm_debugfs_connector_remove(struct drm_connector *connector)
1089 {
1090 }
1091
1092 #endif
1093
1094 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1095                 struct drm_gem_object *obj, int flags);
1096 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1097                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1098                 int *prime_fd);
1099 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1100                 struct dma_buf *dma_buf);
1101 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1102                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1103 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1104
1105 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1106                                             dma_addr_t *addrs, int max_pages);
1107 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages);
1108 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1109
1110 int drm_gem_dumb_destroy(struct drm_file *file,
1111                          struct drm_device *dev,
1112                          uint32_t handle);
1113
1114
1115 extern struct drm_dma_handle *drm_pci_alloc(struct drm_device *dev, size_t size,
1116                                             size_t align);
1117 extern void drm_pci_free(struct drm_device *dev, struct drm_dma_handle * dmah);
1118
1119                                /* sysfs support (drm_sysfs.c) */
1120 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1121
1122 /* Graphics Execution Manager library functions (drm_gem.c) */
1123 void drm_gem_object_release(struct drm_gem_object *obj);
1124 void drm_gem_object_free(struct kref *kref);
1125 int drm_gem_object_init(struct drm_device *dev,
1126                         struct drm_gem_object *obj, size_t size);
1127 void drm_gem_private_object_init(struct drm_device *dev,
1128                                  struct drm_gem_object *obj, size_t size);
1129 void drm_gem_vm_open(struct vm_area_struct *vma);
1130 void drm_gem_vm_close(struct vm_area_struct *vma);
1131 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
1132                      struct vm_area_struct *vma);
1133 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
1134
1135 static inline void
1136 drm_gem_object_reference(struct drm_gem_object *obj)
1137 {
1138         kref_get(&obj->refcount);
1139 }
1140
1141 static inline void
1142 drm_gem_object_unreference(struct drm_gem_object *obj)
1143 {
1144         if (obj != NULL)
1145                 kref_put(&obj->refcount, drm_gem_object_free);
1146 }
1147
1148 static inline void
1149 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1150 {
1151         if (obj && !atomic_add_unless(&obj->refcount.refcount, -1, 1)) {
1152                 struct drm_device *dev = obj->dev;
1153
1154                 mutex_lock(&dev->struct_mutex);
1155                 if (likely(atomic_dec_and_test(&obj->refcount.refcount)))
1156                         drm_gem_object_free(&obj->refcount);
1157                 mutex_unlock(&dev->struct_mutex);
1158         }
1159 }
1160
1161 int drm_gem_handle_create(struct drm_file *file_priv,
1162                           struct drm_gem_object *obj,
1163                           u32 *handlep);
1164 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
1165
1166
1167 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1168 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1169 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
1170
1171 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
1172 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
1173                 bool dirty, bool accessed);
1174
1175 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1176                                              struct drm_file *filp,
1177                                              u32 handle);
1178
1179 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1180                                  struct device *parent);
1181 void drm_dev_ref(struct drm_device *dev);
1182 void drm_dev_unref(struct drm_device *dev);
1183 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1184 void drm_dev_unregister(struct drm_device *dev);
1185 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1186
1187 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1188 void drm_minor_release(struct drm_minor *minor);
1189
1190 /*@}*/
1191
1192 /* PCI section */
1193 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1194 {
1195         if (dev->driver->device_is_agp != NULL) {
1196                 int err = (*dev->driver->device_is_agp) (dev);
1197
1198                 if (err != 2) {
1199                         return err;
1200                 }
1201         }
1202
1203         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1204 }
1205 void drm_pci_agp_destroy(struct drm_device *dev);
1206
1207 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1208 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1209 extern int drm_get_pci_dev(struct pci_dev *pdev,
1210                            const struct pci_device_id *ent,
1211                            struct drm_driver *driver);
1212 extern int drm_pci_set_busid(struct drm_device *dev, struct drm_master *master);
1213
1214 #define DRM_PCIE_SPEED_25 1
1215 #define DRM_PCIE_SPEED_50 2
1216 #define DRM_PCIE_SPEED_80 4
1217
1218 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1219
1220 /* platform section */
1221 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1222 extern int drm_platform_set_busid(struct drm_device *d, struct drm_master *m);
1223
1224 /* returns true if currently okay to sleep */
1225 static __inline__ bool drm_can_sleep(void)
1226 {
1227         if (in_atomic() || in_dbg_master() || irqs_disabled())
1228                 return false;
1229         return true;
1230 }
1231
1232 #endif