drm: replace weird conditional includes
[firefly-linux-kernel-4.4.55.git] / include / drm / drmP.h
1 /**
2  * \file drmP.h
3  * Private header for Direct Rendering Manager
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8
9 /*
10  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12  * Copyright (c) 2009-2010, Code Aurora Forum.
13  * All rights reserved.
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the next
23  * paragraph) shall be included in all copies or substantial portions of the
24  * Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
29  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32  * OTHER DEALINGS IN THE SOFTWARE.
33  */
34
35 #ifndef _DRM_P_H_
36 #define _DRM_P_H_
37
38 #ifdef __KERNEL__
39 #include <linux/sched.h>
40 #include <linux/kernel.h>
41 #include <linux/kref.h>
42 #include <linux/miscdevice.h>
43 #include <linux/fs.h>
44 #include <linux/init.h>
45 #include <linux/file.h>
46 #include <linux/platform_device.h>
47 #include <linux/pci.h>
48 #include <linux/jiffies.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/mm.h>
51 #include <linux/cdev.h>
52 #include <linux/mutex.h>
53 #include <linux/io.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
56 #include <asm/mman.h>
57 #include <asm/uaccess.h>
58 #include <linux/types.h>
59 #include <linux/agp_backend.h>
60 #include <linux/workqueue.h>
61 #include <linux/poll.h>
62 #include <asm/pgalloc.h>
63 #include <drm/drm.h>
64 #include <drm/drm_sarea.h>
65 #include <drm/drm_vma_manager.h>
66
67 #include <linux/idr.h>
68
69 #define __OS_HAS_AGP (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
70
71 struct module;
72
73 struct drm_file;
74 struct drm_device;
75 struct drm_agp_head;
76
77 struct device_node;
78 struct videomode;
79 struct reservation_object;
80
81 #include <drm/drm_os_linux.h>
82 #include <drm/drm_hashtab.h>
83 #include <drm/drm_mm.h>
84
85 /*
86  * 4 debug categories are defined:
87  *
88  * CORE: Used in the generic drm code: drm_ioctl.c, drm_mm.c, drm_memory.c, ...
89  *       This is the category used by the DRM_DEBUG() macro.
90  *
91  * DRIVER: Used in the vendor specific part of the driver: i915, radeon, ...
92  *         This is the category used by the DRM_DEBUG_DRIVER() macro.
93  *
94  * KMS: used in the modesetting code.
95  *      This is the category used by the DRM_DEBUG_KMS() macro.
96  *
97  * PRIME: used in the prime code.
98  *        This is the category used by the DRM_DEBUG_PRIME() macro.
99  *
100  * Enabling verbose debug messages is done through the drm.debug parameter,
101  * each category being enabled by a bit.
102  *
103  * drm.debug=0x1 will enable CORE messages
104  * drm.debug=0x2 will enable DRIVER messages
105  * drm.debug=0x3 will enable CORE and DRIVER messages
106  * ...
107  * drm.debug=0xf will enable all messages
108  *
109  * An interesting feature is that it's possible to enable verbose logging at
110  * run-time by echoing the debug value in its sysfs node:
111  *   # echo 0xf > /sys/module/drm/parameters/debug
112  */
113 #define DRM_UT_CORE             0x01
114 #define DRM_UT_DRIVER           0x02
115 #define DRM_UT_KMS              0x04
116 #define DRM_UT_PRIME            0x08
117
118 extern __printf(2, 3)
119 void drm_ut_debug_printk(const char *function_name,
120                          const char *format, ...);
121 extern __printf(2, 3)
122 int drm_err(const char *func, const char *format, ...);
123
124 /***********************************************************************/
125 /** \name DRM template customization defaults */
126 /*@{*/
127
128 /* driver capabilities and requirements mask */
129 #define DRIVER_USE_AGP     0x1
130 #define DRIVER_PCI_DMA     0x8
131 #define DRIVER_SG          0x10
132 #define DRIVER_HAVE_DMA    0x20
133 #define DRIVER_HAVE_IRQ    0x40
134 #define DRIVER_IRQ_SHARED  0x80
135 #define DRIVER_GEM         0x1000
136 #define DRIVER_MODESET     0x2000
137 #define DRIVER_PRIME       0x4000
138 #define DRIVER_RENDER      0x8000
139
140 /***********************************************************************/
141 /** \name Begin the DRM... */
142 /*@{*/
143
144 #define DRM_DEBUG_CODE 2          /**< Include debugging code if > 1, then
145                                      also include looping detection. */
146
147 #define DRM_MAGIC_HASH_ORDER  4  /**< Size of key hash table. Must be power of 2. */
148
149 /*@}*/
150
151 /***********************************************************************/
152 /** \name Macros to make printk easier */
153 /*@{*/
154
155 /**
156  * Error output.
157  *
158  * \param fmt printf() like format string.
159  * \param arg arguments
160  */
161 #define DRM_ERROR(fmt, ...)                             \
162         drm_err(__func__, fmt, ##__VA_ARGS__)
163
164 /**
165  * Rate limited error output.  Like DRM_ERROR() but won't flood the log.
166  *
167  * \param fmt printf() like format string.
168  * \param arg arguments
169  */
170 #define DRM_ERROR_RATELIMITED(fmt, ...)                         \
171 ({                                                                      \
172         static DEFINE_RATELIMIT_STATE(_rs,                              \
173                                       DEFAULT_RATELIMIT_INTERVAL,       \
174                                       DEFAULT_RATELIMIT_BURST);         \
175                                                                         \
176         if (__ratelimit(&_rs))                                          \
177                 drm_err(__func__, fmt, ##__VA_ARGS__);                  \
178 })
179
180 #define DRM_INFO(fmt, ...)                              \
181         printk(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
182
183 #define DRM_INFO_ONCE(fmt, ...)                         \
184         printk_once(KERN_INFO "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
185
186 /**
187  * Debug output.
188  *
189  * \param fmt printf() like format string.
190  * \param arg arguments
191  */
192 #if DRM_DEBUG_CODE
193 #define DRM_DEBUG(fmt, args...)                                         \
194         do {                                                            \
195                 if (unlikely(drm_debug & DRM_UT_CORE))                  \
196                         drm_ut_debug_printk(__func__, fmt, ##args);     \
197         } while (0)
198
199 #define DRM_DEBUG_DRIVER(fmt, args...)                                  \
200         do {                                                            \
201                 if (unlikely(drm_debug & DRM_UT_DRIVER))                \
202                         drm_ut_debug_printk(__func__, fmt, ##args);     \
203         } while (0)
204 #define DRM_DEBUG_KMS(fmt, args...)                                     \
205         do {                                                            \
206                 if (unlikely(drm_debug & DRM_UT_KMS))                   \
207                         drm_ut_debug_printk(__func__, fmt, ##args);     \
208         } while (0)
209 #define DRM_DEBUG_PRIME(fmt, args...)                                   \
210         do {                                                            \
211                 if (unlikely(drm_debug & DRM_UT_PRIME))                 \
212                         drm_ut_debug_printk(__func__, fmt, ##args);     \
213         } while (0)
214 #else
215 #define DRM_DEBUG_DRIVER(fmt, args...) do { } while (0)
216 #define DRM_DEBUG_KMS(fmt, args...)     do { } while (0)
217 #define DRM_DEBUG_PRIME(fmt, args...)   do { } while (0)
218 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
219 #endif
220
221 /*@}*/
222
223 /***********************************************************************/
224 /** \name Internal types and structures */
225 /*@{*/
226
227 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
228
229 /**
230  * Test that the hardware lock is held by the caller, returning otherwise.
231  *
232  * \param dev DRM device.
233  * \param filp file pointer of the caller.
234  */
235 #define LOCK_TEST_WITH_RETURN( dev, _file_priv )                                \
236 do {                                                                            \
237         if (!_DRM_LOCK_IS_HELD(_file_priv->master->lock.hw_lock->lock) ||       \
238             _file_priv->master->lock.file_priv != _file_priv)   {               \
239                 DRM_ERROR( "%s called without lock held, held  %d owner %p %p\n",\
240                            __func__, _DRM_LOCK_IS_HELD(_file_priv->master->lock.hw_lock->lock),\
241                            _file_priv->master->lock.file_priv, _file_priv);     \
242                 return -EINVAL;                                                 \
243         }                                                                       \
244 } while (0)
245
246 /**
247  * Ioctl function type.
248  *
249  * \param inode device inode.
250  * \param file_priv DRM file private pointer.
251  * \param cmd command.
252  * \param arg argument.
253  */
254 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
255                         struct drm_file *file_priv);
256
257 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
258                                unsigned long arg);
259
260 #define DRM_IOCTL_NR(n)                _IOC_NR(n)
261 #define DRM_MAJOR       226
262
263 #define DRM_AUTH        0x1
264 #define DRM_MASTER      0x2
265 #define DRM_ROOT_ONLY   0x4
266 #define DRM_CONTROL_ALLOW 0x8
267 #define DRM_UNLOCKED    0x10
268 #define DRM_RENDER_ALLOW 0x20
269
270 struct drm_ioctl_desc {
271         unsigned int cmd;
272         int flags;
273         drm_ioctl_t *func;
274         unsigned int cmd_drv;
275         const char *name;
276 };
277
278 /**
279  * Creates a driver or general drm_ioctl_desc array entry for the given
280  * ioctl, for use by drm_ioctl().
281  */
282
283 #define DRM_IOCTL_DEF_DRV(ioctl, _func, _flags)                 \
284         [DRM_IOCTL_NR(DRM_##ioctl)] = {.cmd = DRM_##ioctl, .func = _func, .flags = _flags, .cmd_drv = DRM_IOCTL_##ioctl, .name = #ioctl}
285
286 /**
287  * DMA buffer.
288  */
289 struct drm_buf {
290         int idx;                       /**< Index into master buflist */
291         int total;                     /**< Buffer size */
292         int order;                     /**< log-base-2(total) */
293         int used;                      /**< Amount of buffer in use (for DMA) */
294         unsigned long offset;          /**< Byte offset (used internally) */
295         void *address;                 /**< Address of buffer */
296         unsigned long bus_address;     /**< Bus address of buffer */
297         struct drm_buf *next;          /**< Kernel-only: used for free list */
298         __volatile__ int waiting;      /**< On kernel DMA queue */
299         __volatile__ int pending;      /**< On hardware DMA queue */
300         struct drm_file *file_priv;    /**< Private of holding file descr */
301         int context;                   /**< Kernel queue for this buffer */
302         int while_locked;              /**< Dispatch this buffer while locked */
303         enum {
304                 DRM_LIST_NONE = 0,
305                 DRM_LIST_FREE = 1,
306                 DRM_LIST_WAIT = 2,
307                 DRM_LIST_PEND = 3,
308                 DRM_LIST_PRIO = 4,
309                 DRM_LIST_RECLAIM = 5
310         } list;                        /**< Which list we're on */
311
312         int dev_priv_size;               /**< Size of buffer private storage */
313         void *dev_private;               /**< Per-buffer private storage */
314 };
315
316 typedef struct drm_dma_handle {
317         dma_addr_t busaddr;
318         void *vaddr;
319         size_t size;
320 } drm_dma_handle_t;
321
322 /**
323  * Buffer entry.  There is one of this for each buffer size order.
324  */
325 struct drm_buf_entry {
326         int buf_size;                   /**< size */
327         int buf_count;                  /**< number of buffers */
328         struct drm_buf *buflist;                /**< buffer list */
329         int seg_count;
330         int page_order;
331         struct drm_dma_handle **seglist;
332
333         int low_mark;                   /**< Low water mark */
334         int high_mark;                  /**< High water mark */
335 };
336
337 /* Event queued up for userspace to read */
338 struct drm_pending_event {
339         struct drm_event *event;
340         struct list_head link;
341         struct drm_file *file_priv;
342         pid_t pid; /* pid of requester, no guarantee it's valid by the time
343                       we deliver the event, for tracing only */
344         void (*destroy)(struct drm_pending_event *event);
345 };
346
347 /* initial implementaton using a linked list - todo hashtab */
348 struct drm_prime_file_private {
349         struct list_head head;
350         struct mutex lock;
351 };
352
353 /** File private data */
354 struct drm_file {
355         unsigned authenticated :1;
356         /* Whether we're master for a minor. Protected by master_mutex */
357         unsigned is_master :1;
358         /* true when the client has asked us to expose stereo 3D mode flags */
359         unsigned stereo_allowed :1;
360         /*
361          * true if client understands CRTC primary planes and cursor planes
362          * in the plane list
363          */
364         unsigned universal_planes:1;
365
366         struct pid *pid;
367         kuid_t uid;
368         drm_magic_t magic;
369         struct list_head lhead;
370         struct drm_minor *minor;
371         unsigned long lock_count;
372
373         /** Mapping of mm object handles to object pointers. */
374         struct idr object_idr;
375         /** Lock for synchronization of access to object_idr. */
376         spinlock_t table_lock;
377
378         struct file *filp;
379         void *driver_priv;
380
381         struct drm_master *master; /* master this node is currently associated with
382                                       N.B. not always minor->master */
383         /**
384          * fbs - List of framebuffers associated with this file.
385          *
386          * Protected by fbs_lock. Note that the fbs list holds a reference on
387          * the fb object to prevent it from untimely disappearing.
388          */
389         struct list_head fbs;
390         struct mutex fbs_lock;
391
392         wait_queue_head_t event_wait;
393         struct list_head event_list;
394         int event_space;
395
396         struct drm_prime_file_private prime;
397 };
398
399 /**
400  * Lock data.
401  */
402 struct drm_lock_data {
403         struct drm_hw_lock *hw_lock;    /**< Hardware lock */
404         /** Private of lock holder's file (NULL=kernel) */
405         struct drm_file *file_priv;
406         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
407         unsigned long lock_time;        /**< Time of last lock in jiffies */
408         spinlock_t spinlock;
409         uint32_t kernel_waiters;
410         uint32_t user_waiters;
411         int idle_has_lock;
412 };
413
414 /**
415  * DMA data.
416  */
417 struct drm_device_dma {
418
419         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
420         int buf_count;                  /**< total number of buffers */
421         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
422         int seg_count;
423         int page_count;                 /**< number of pages */
424         unsigned long *pagelist;        /**< page list */
425         unsigned long byte_count;
426         enum {
427                 _DRM_DMA_USE_AGP = 0x01,
428                 _DRM_DMA_USE_SG = 0x02,
429                 _DRM_DMA_USE_FB = 0x04,
430                 _DRM_DMA_USE_PCI_RO = 0x08
431         } flags;
432
433 };
434
435 /**
436  * Scatter-gather memory.
437  */
438 struct drm_sg_mem {
439         unsigned long handle;
440         void *virtual;
441         int pages;
442         struct page **pagelist;
443         dma_addr_t *busaddr;
444 };
445
446 struct drm_sigdata {
447         int context;
448         struct drm_hw_lock *lock;
449 };
450
451
452 /**
453  * Kernel side of a mapping
454  */
455 struct drm_local_map {
456         resource_size_t offset;  /**< Requested physical address (0 for SAREA)*/
457         unsigned long size;      /**< Requested physical size (bytes) */
458         enum drm_map_type type;  /**< Type of memory to map */
459         enum drm_map_flags flags;        /**< Flags */
460         void *handle;            /**< User-space: "Handle" to pass to mmap() */
461                                  /**< Kernel-space: kernel-virtual address */
462         int mtrr;                /**< MTRR slot used */
463 };
464
465 typedef struct drm_local_map drm_local_map_t;
466
467 /**
468  * Mappings list
469  */
470 struct drm_map_list {
471         struct list_head head;          /**< list head */
472         struct drm_hash_item hash;
473         struct drm_local_map *map;      /**< mapping */
474         uint64_t user_token;
475         struct drm_master *master;
476 };
477
478 /* location of GART table */
479 #define DRM_ATI_GART_MAIN 1
480 #define DRM_ATI_GART_FB   2
481
482 #define DRM_ATI_GART_PCI 1
483 #define DRM_ATI_GART_PCIE 2
484 #define DRM_ATI_GART_IGP 3
485
486 struct drm_ati_pcigart_info {
487         int gart_table_location;
488         int gart_reg_if;
489         void *addr;
490         dma_addr_t bus_addr;
491         dma_addr_t table_mask;
492         struct drm_dma_handle *table_handle;
493         struct drm_local_map mapping;
494         int table_size;
495 };
496
497 /**
498  * This structure defines the drm_mm memory object, which will be used by the
499  * DRM for its buffer objects.
500  */
501 struct drm_gem_object {
502         /** Reference count of this object */
503         struct kref refcount;
504
505         /**
506          * handle_count - gem file_priv handle count of this object
507          *
508          * Each handle also holds a reference. Note that when the handle_count
509          * drops to 0 any global names (e.g. the id in the flink namespace) will
510          * be cleared.
511          *
512          * Protected by dev->object_name_lock.
513          * */
514         unsigned handle_count;
515
516         /** Related drm device */
517         struct drm_device *dev;
518
519         /** File representing the shmem storage */
520         struct file *filp;
521
522         /* Mapping info for this object */
523         struct drm_vma_offset_node vma_node;
524
525         /**
526          * Size of the object, in bytes.  Immutable over the object's
527          * lifetime.
528          */
529         size_t size;
530
531         /**
532          * Global name for this object, starts at 1. 0 means unnamed.
533          * Access is covered by the object_name_lock in the related drm_device
534          */
535         int name;
536
537         /**
538          * Memory domains. These monitor which caches contain read/write data
539          * related to the object. When transitioning from one set of domains
540          * to another, the driver is called to ensure that caches are suitably
541          * flushed and invalidated
542          */
543         uint32_t read_domains;
544         uint32_t write_domain;
545
546         /**
547          * While validating an exec operation, the
548          * new read/write domain values are computed here.
549          * They will be transferred to the above values
550          * at the point that any cache flushing occurs
551          */
552         uint32_t pending_read_domains;
553         uint32_t pending_write_domain;
554
555         /**
556          * dma_buf - dma buf associated with this GEM object
557          *
558          * Pointer to the dma-buf associated with this gem object (either
559          * through importing or exporting). We break the resulting reference
560          * loop when the last gem handle for this object is released.
561          *
562          * Protected by obj->object_name_lock
563          */
564         struct dma_buf *dma_buf;
565
566         /**
567          * import_attach - dma buf attachment backing this object
568          *
569          * Any foreign dma_buf imported as a gem object has this set to the
570          * attachment point for the device. This is invariant over the lifetime
571          * of a gem object.
572          *
573          * The driver's ->gem_free_object callback is responsible for cleaning
574          * up the dma_buf attachment and references acquired at import time.
575          *
576          * Note that the drm gem/prime core does not depend upon drivers setting
577          * this field any more. So for drivers where this doesn't make sense
578          * (e.g. virtual devices or a displaylink behind an usb bus) they can
579          * simply leave it as NULL.
580          */
581         struct dma_buf_attachment *import_attach;
582 };
583
584 #include <drm/drm_crtc.h>
585
586 /**
587  * struct drm_master - drm master structure
588  *
589  * @refcount: Refcount for this master object.
590  * @minor: Link back to minor char device we are master for. Immutable.
591  * @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
592  * @unique_len: Length of unique field. Protected by drm_global_mutex.
593  * @unique_size: Amount allocated. Protected by drm_global_mutex.
594  * @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
595  * @magicfree: List of used authentication tokens. Protected by struct_mutex.
596  * @lock: DRI lock information.
597  * @driver_priv: Pointer to driver-private information.
598  */
599 struct drm_master {
600         struct kref refcount;
601         struct drm_minor *minor;
602         char *unique;
603         int unique_len;
604         int unique_size;
605         struct drm_open_hash magiclist;
606         struct list_head magicfree;
607         struct drm_lock_data lock;
608         void *driver_priv;
609 };
610
611 /* Size of ringbuffer for vblank timestamps. Just double-buffer
612  * in initial implementation.
613  */
614 #define DRM_VBLANKTIME_RBSIZE 2
615
616 /* Flags and return codes for get_vblank_timestamp() driver function. */
617 #define DRM_CALLED_FROM_VBLIRQ 1
618 #define DRM_VBLANKTIME_SCANOUTPOS_METHOD (1 << 0)
619 #define DRM_VBLANKTIME_INVBL             (1 << 1)
620
621 /* get_scanout_position() return flags */
622 #define DRM_SCANOUTPOS_VALID        (1 << 0)
623 #define DRM_SCANOUTPOS_INVBL        (1 << 1)
624 #define DRM_SCANOUTPOS_ACCURATE     (1 << 2)
625
626 struct drm_bus {
627         int (*set_busid)(struct drm_device *dev, struct drm_master *master);
628 };
629
630 /**
631  * DRM driver structure. This structure represent the common code for
632  * a family of cards. There will one drm_device for each card present
633  * in this family
634  */
635 struct drm_driver {
636         int (*load) (struct drm_device *, unsigned long flags);
637         int (*firstopen) (struct drm_device *);
638         int (*open) (struct drm_device *, struct drm_file *);
639         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
640         void (*postclose) (struct drm_device *, struct drm_file *);
641         void (*lastclose) (struct drm_device *);
642         int (*unload) (struct drm_device *);
643         int (*suspend) (struct drm_device *, pm_message_t state);
644         int (*resume) (struct drm_device *);
645         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
646         int (*dma_quiescent) (struct drm_device *);
647         int (*context_dtor) (struct drm_device *dev, int context);
648
649         /**
650          * get_vblank_counter - get raw hardware vblank counter
651          * @dev: DRM device
652          * @crtc: counter to fetch
653          *
654          * Driver callback for fetching a raw hardware vblank counter for @crtc.
655          * If a device doesn't have a hardware counter, the driver can simply
656          * return the value of drm_vblank_count. The DRM core will account for
657          * missed vblank events while interrupts where disabled based on system
658          * timestamps.
659          *
660          * Wraparound handling and loss of events due to modesetting is dealt
661          * with in the DRM core code.
662          *
663          * RETURNS
664          * Raw vblank counter value.
665          */
666         u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
667
668         /**
669          * enable_vblank - enable vblank interrupt events
670          * @dev: DRM device
671          * @crtc: which irq to enable
672          *
673          * Enable vblank interrupts for @crtc.  If the device doesn't have
674          * a hardware vblank counter, this routine should be a no-op, since
675          * interrupts will have to stay on to keep the count accurate.
676          *
677          * RETURNS
678          * Zero on success, appropriate errno if the given @crtc's vblank
679          * interrupt cannot be enabled.
680          */
681         int (*enable_vblank) (struct drm_device *dev, int crtc);
682
683         /**
684          * disable_vblank - disable vblank interrupt events
685          * @dev: DRM device
686          * @crtc: which irq to enable
687          *
688          * Disable vblank interrupts for @crtc.  If the device doesn't have
689          * a hardware vblank counter, this routine should be a no-op, since
690          * interrupts will have to stay on to keep the count accurate.
691          */
692         void (*disable_vblank) (struct drm_device *dev, int crtc);
693
694         /**
695          * Called by \c drm_device_is_agp.  Typically used to determine if a
696          * card is really attached to AGP or not.
697          *
698          * \param dev  DRM device handle
699          *
700          * \returns
701          * One of three values is returned depending on whether or not the
702          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
703          * (return of 1), or may or may not be AGP (return of 2).
704          */
705         int (*device_is_agp) (struct drm_device *dev);
706
707         /**
708          * Called by vblank timestamping code.
709          *
710          * Return the current display scanout position from a crtc, and an
711          * optional accurate ktime_get timestamp of when position was measured.
712          *
713          * \param dev  DRM device.
714          * \param crtc Id of the crtc to query.
715          * \param flags Flags from the caller (DRM_CALLED_FROM_VBLIRQ or 0).
716          * \param *vpos Target location for current vertical scanout position.
717          * \param *hpos Target location for current horizontal scanout position.
718          * \param *stime Target location for timestamp taken immediately before
719          *               scanout position query. Can be NULL to skip timestamp.
720          * \param *etime Target location for timestamp taken immediately after
721          *               scanout position query. Can be NULL to skip timestamp.
722          *
723          * Returns vpos as a positive number while in active scanout area.
724          * Returns vpos as a negative number inside vblank, counting the number
725          * of scanlines to go until end of vblank, e.g., -1 means "one scanline
726          * until start of active scanout / end of vblank."
727          *
728          * \return Flags, or'ed together as follows:
729          *
730          * DRM_SCANOUTPOS_VALID = Query successful.
731          * DRM_SCANOUTPOS_INVBL = Inside vblank.
732          * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
733          * this flag means that returned position may be offset by a constant
734          * but unknown small number of scanlines wrt. real scanout position.
735          *
736          */
737         int (*get_scanout_position) (struct drm_device *dev, int crtc,
738                                      unsigned int flags,
739                                      int *vpos, int *hpos, ktime_t *stime,
740                                      ktime_t *etime);
741
742         /**
743          * Called by \c drm_get_last_vbltimestamp. Should return a precise
744          * timestamp when the most recent VBLANK interval ended or will end.
745          *
746          * Specifically, the timestamp in @vblank_time should correspond as
747          * closely as possible to the time when the first video scanline of
748          * the video frame after the end of VBLANK will start scanning out,
749          * the time immediately after end of the VBLANK interval. If the
750          * @crtc is currently inside VBLANK, this will be a time in the future.
751          * If the @crtc is currently scanning out a frame, this will be the
752          * past start time of the current scanout. This is meant to adhere
753          * to the OpenML OML_sync_control extension specification.
754          *
755          * \param dev dev DRM device handle.
756          * \param crtc crtc for which timestamp should be returned.
757          * \param *max_error Maximum allowable timestamp error in nanoseconds.
758          *                   Implementation should strive to provide timestamp
759          *                   with an error of at most *max_error nanoseconds.
760          *                   Returns true upper bound on error for timestamp.
761          * \param *vblank_time Target location for returned vblank timestamp.
762          * \param flags 0 = Defaults, no special treatment needed.
763          * \param       DRM_CALLED_FROM_VBLIRQ = Function is called from vblank
764          *              irq handler. Some drivers need to apply some workarounds
765          *              for gpu-specific vblank irq quirks if flag is set.
766          *
767          * \returns
768          * Zero if timestamping isn't supported in current display mode or a
769          * negative number on failure. A positive status code on success,
770          * which describes how the vblank_time timestamp was computed.
771          */
772         int (*get_vblank_timestamp) (struct drm_device *dev, int crtc,
773                                      int *max_error,
774                                      struct timeval *vblank_time,
775                                      unsigned flags);
776
777         /* these have to be filled in */
778
779         irqreturn_t(*irq_handler) (int irq, void *arg);
780         void (*irq_preinstall) (struct drm_device *dev);
781         int (*irq_postinstall) (struct drm_device *dev);
782         void (*irq_uninstall) (struct drm_device *dev);
783
784         /* Master routines */
785         int (*master_create)(struct drm_device *dev, struct drm_master *master);
786         void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
787         /**
788          * master_set is called whenever the minor master is set.
789          * master_drop is called whenever the minor master is dropped.
790          */
791
792         int (*master_set)(struct drm_device *dev, struct drm_file *file_priv,
793                           bool from_open);
794         void (*master_drop)(struct drm_device *dev, struct drm_file *file_priv,
795                             bool from_release);
796
797         int (*debugfs_init)(struct drm_minor *minor);
798         void (*debugfs_cleanup)(struct drm_minor *minor);
799
800         /**
801          * Driver-specific constructor for drm_gem_objects, to set up
802          * obj->driver_private.
803          *
804          * Returns 0 on success.
805          */
806         void (*gem_free_object) (struct drm_gem_object *obj);
807         int (*gem_open_object) (struct drm_gem_object *, struct drm_file *);
808         void (*gem_close_object) (struct drm_gem_object *, struct drm_file *);
809
810         /* prime: */
811         /* export handle -> fd (see drm_gem_prime_handle_to_fd() helper) */
812         int (*prime_handle_to_fd)(struct drm_device *dev, struct drm_file *file_priv,
813                                 uint32_t handle, uint32_t flags, int *prime_fd);
814         /* import fd -> handle (see drm_gem_prime_fd_to_handle() helper) */
815         int (*prime_fd_to_handle)(struct drm_device *dev, struct drm_file *file_priv,
816                                 int prime_fd, uint32_t *handle);
817         /* export GEM -> dmabuf */
818         struct dma_buf * (*gem_prime_export)(struct drm_device *dev,
819                                 struct drm_gem_object *obj, int flags);
820         /* import dmabuf -> GEM */
821         struct drm_gem_object * (*gem_prime_import)(struct drm_device *dev,
822                                 struct dma_buf *dma_buf);
823         /* low-level interface used by drm_gem_prime_{import,export} */
824         int (*gem_prime_pin)(struct drm_gem_object *obj);
825         void (*gem_prime_unpin)(struct drm_gem_object *obj);
826         struct reservation_object * (*gem_prime_res_obj)(
827                                 struct drm_gem_object *obj);
828         struct sg_table *(*gem_prime_get_sg_table)(struct drm_gem_object *obj);
829         struct drm_gem_object *(*gem_prime_import_sg_table)(
830                                 struct drm_device *dev, size_t size,
831                                 struct sg_table *sgt);
832         void *(*gem_prime_vmap)(struct drm_gem_object *obj);
833         void (*gem_prime_vunmap)(struct drm_gem_object *obj, void *vaddr);
834         int (*gem_prime_mmap)(struct drm_gem_object *obj,
835                                 struct vm_area_struct *vma);
836
837         /* vga arb irq handler */
838         void (*vgaarb_irq)(struct drm_device *dev, bool state);
839
840         /* dumb alloc support */
841         int (*dumb_create)(struct drm_file *file_priv,
842                            struct drm_device *dev,
843                            struct drm_mode_create_dumb *args);
844         int (*dumb_map_offset)(struct drm_file *file_priv,
845                                struct drm_device *dev, uint32_t handle,
846                                uint64_t *offset);
847         int (*dumb_destroy)(struct drm_file *file_priv,
848                             struct drm_device *dev,
849                             uint32_t handle);
850
851         /* Driver private ops for this object */
852         const struct vm_operations_struct *gem_vm_ops;
853
854         int major;
855         int minor;
856         int patchlevel;
857         char *name;
858         char *desc;
859         char *date;
860
861         u32 driver_features;
862         int dev_priv_size;
863         const struct drm_ioctl_desc *ioctls;
864         int num_ioctls;
865         const struct file_operations *fops;
866         struct drm_bus *bus;
867
868         /* List of devices hanging off this driver with stealth attach. */
869         struct list_head legacy_dev_list;
870 };
871
872 enum drm_minor_type {
873         DRM_MINOR_LEGACY,
874         DRM_MINOR_CONTROL,
875         DRM_MINOR_RENDER,
876         DRM_MINOR_CNT,
877 };
878
879 /**
880  * Info file list entry. This structure represents a debugfs or proc file to
881  * be created by the drm core
882  */
883 struct drm_info_list {
884         const char *name; /** file name */
885         int (*show)(struct seq_file*, void*); /** show callback */
886         u32 driver_features; /**< Required driver features for this entry */
887         void *data;
888 };
889
890 /**
891  * debugfs node structure. This structure represents a debugfs file.
892  */
893 struct drm_info_node {
894         struct list_head list;
895         struct drm_minor *minor;
896         const struct drm_info_list *info_ent;
897         struct dentry *dent;
898 };
899
900 /**
901  * DRM minor structure. This structure represents a drm minor number.
902  */
903 struct drm_minor {
904         int index;                      /**< Minor device number */
905         int type;                       /**< Control or render */
906         struct device *kdev;            /**< Linux device */
907         struct drm_device *dev;
908
909         struct dentry *debugfs_root;
910
911         struct list_head debugfs_list;
912         struct mutex debugfs_lock; /* Protects debugfs_list. */
913
914         /* currently active master for this node. Protected by master_mutex */
915         struct drm_master *master;
916         struct drm_mode_group mode_group;
917 };
918
919
920 struct drm_pending_vblank_event {
921         struct drm_pending_event base;
922         int pipe;
923         struct drm_event_vblank event;
924 };
925
926 struct drm_vblank_crtc {
927         struct drm_device *dev;         /* pointer to the drm_device */
928         wait_queue_head_t queue;        /**< VBLANK wait queue */
929         struct timeval time[DRM_VBLANKTIME_RBSIZE];     /**< timestamp of current count */
930         struct timer_list disable_timer;                /* delayed disable timer */
931         atomic_t count;                 /**< number of VBLANK interrupts */
932         atomic_t refcount;              /* number of users of vblank interruptsper crtc */
933         u32 last;                       /* protected by dev->vbl_lock, used */
934                                         /* for wraparound handling */
935         u32 last_wait;                  /* Last vblank seqno waited per CRTC */
936         unsigned int inmodeset;         /* Display driver is setting mode */
937         int crtc;                       /* crtc index */
938         bool enabled;                   /* so we don't call enable more than
939                                            once per disable */
940 };
941
942 /**
943  * DRM device structure. This structure represent a complete card that
944  * may contain multiple heads.
945  */
946 struct drm_device {
947         struct list_head legacy_dev_list;/**< list of devices per driver for stealth attach cleanup */
948         int if_version;                 /**< Highest interface version set */
949
950         /** \name Lifetime Management */
951         /*@{ */
952         struct kref ref;                /**< Object ref-count */
953         struct device *dev;             /**< Device structure of bus-device */
954         struct drm_driver *driver;      /**< DRM driver managing the device */
955         void *dev_private;              /**< DRM driver private data */
956         struct drm_minor *control;              /**< Control node */
957         struct drm_minor *primary;              /**< Primary node */
958         struct drm_minor *render;               /**< Render node */
959         atomic_t unplugged;                     /**< Flag whether dev is dead */
960         struct inode *anon_inode;               /**< inode for private address-space */
961         char *unique;                           /**< unique name of the device */
962         /*@} */
963
964         /** \name Locks */
965         /*@{ */
966         struct mutex struct_mutex;      /**< For others */
967         struct mutex master_mutex;      /**< For drm_minor::master and drm_file::is_master */
968         /*@} */
969
970         /** \name Usage Counters */
971         /*@{ */
972         int open_count;                 /**< Outstanding files open, protected by drm_global_mutex. */
973         spinlock_t buf_lock;            /**< For drm_device::buf_use and a few other things. */
974         int buf_use;                    /**< Buffers in use -- cannot alloc */
975         atomic_t buf_alloc;             /**< Buffer allocation in progress */
976         /*@} */
977
978         struct list_head filelist;
979
980         /** \name Memory management */
981         /*@{ */
982         struct list_head maplist;       /**< Linked list of regions */
983         struct drm_open_hash map_hash;  /**< User token hash table for maps */
984
985         /** \name Context handle management */
986         /*@{ */
987         struct list_head ctxlist;       /**< Linked list of context handles */
988         struct mutex ctxlist_mutex;     /**< For ctxlist */
989
990         struct idr ctx_idr;
991
992         struct list_head vmalist;       /**< List of vmas (for debugging) */
993
994         /*@} */
995
996         /** \name DMA support */
997         /*@{ */
998         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
999         /*@} */
1000
1001         /** \name Context support */
1002         /*@{ */
1003         bool irq_enabled;               /**< True if irq handler is enabled */
1004         int irq;
1005
1006         __volatile__ long context_flag; /**< Context swapping flag */
1007         int last_context;               /**< Last current context */
1008         /*@} */
1009
1010         /** \name VBLANK IRQ support */
1011         /*@{ */
1012
1013         /*
1014          * At load time, disabling the vblank interrupt won't be allowed since
1015          * old clients may not call the modeset ioctl and therefore misbehave.
1016          * Once the modeset ioctl *has* been called though, we can safely
1017          * disable them when unused.
1018          */
1019         bool vblank_disable_allowed;
1020
1021         /* array of size num_crtcs */
1022         struct drm_vblank_crtc *vblank;
1023
1024         spinlock_t vblank_time_lock;    /**< Protects vblank count and time updates during vblank enable/disable */
1025         spinlock_t vbl_lock;
1026
1027         u32 max_vblank_count;           /**< size of vblank counter register */
1028
1029         /**
1030          * List of events
1031          */
1032         struct list_head vblank_event_list;
1033         spinlock_t event_lock;
1034
1035         /*@} */
1036
1037         struct drm_agp_head *agp;       /**< AGP data */
1038
1039         struct pci_dev *pdev;           /**< PCI device structure */
1040 #ifdef __alpha__
1041         struct pci_controller *hose;
1042 #endif
1043
1044         struct platform_device *platformdev; /**< Platform device struture */
1045         struct usb_device *usbdev;
1046
1047         struct drm_sg_mem *sg;  /**< Scatter gather memory */
1048         unsigned int num_crtcs;                  /**< Number of CRTCs on this device */
1049         struct drm_sigdata sigdata;        /**< For block_all_signals */
1050         sigset_t sigmask;
1051
1052         struct drm_local_map *agp_buffer_map;
1053         unsigned int agp_buffer_token;
1054
1055         struct drm_mode_config mode_config;     /**< Current mode config */
1056
1057         /** \name GEM information */
1058         /*@{ */
1059         struct mutex object_name_lock;
1060         struct idr object_name_idr;
1061         struct drm_vma_offset_manager *vma_offset_manager;
1062         /*@} */
1063         int switch_power_state;
1064 };
1065
1066 #define DRM_SWITCH_POWER_ON 0
1067 #define DRM_SWITCH_POWER_OFF 1
1068 #define DRM_SWITCH_POWER_CHANGING 2
1069 #define DRM_SWITCH_POWER_DYNAMIC_OFF 3
1070
1071 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1072                                              int feature)
1073 {
1074         return ((dev->driver->driver_features & feature) ? 1 : 0);
1075 }
1076
1077 static inline void drm_device_set_unplugged(struct drm_device *dev)
1078 {
1079         smp_wmb();
1080         atomic_set(&dev->unplugged, 1);
1081 }
1082
1083 static inline int drm_device_is_unplugged(struct drm_device *dev)
1084 {
1085         int ret = atomic_read(&dev->unplugged);
1086         smp_rmb();
1087         return ret;
1088 }
1089
1090 static inline bool drm_is_render_client(const struct drm_file *file_priv)
1091 {
1092         return file_priv->minor->type == DRM_MINOR_RENDER;
1093 }
1094
1095 static inline bool drm_is_control_client(const struct drm_file *file_priv)
1096 {
1097         return file_priv->minor->type == DRM_MINOR_CONTROL;
1098 }
1099
1100 static inline bool drm_is_primary_client(const struct drm_file *file_priv)
1101 {
1102         return file_priv->minor->type == DRM_MINOR_LEGACY;
1103 }
1104
1105 /******************************************************************/
1106 /** \name Internal function definitions */
1107 /*@{*/
1108
1109                                 /* Driver support (drm_drv.h) */
1110 extern long drm_ioctl(struct file *filp,
1111                       unsigned int cmd, unsigned long arg);
1112 extern long drm_compat_ioctl(struct file *filp,
1113                              unsigned int cmd, unsigned long arg);
1114 extern int drm_lastclose(struct drm_device *dev);
1115 extern bool drm_ioctl_flags(unsigned int nr, unsigned int *flags);
1116
1117                                 /* Device support (drm_fops.h) */
1118 extern struct mutex drm_global_mutex;
1119 extern int drm_open(struct inode *inode, struct file *filp);
1120 extern ssize_t drm_read(struct file *filp, char __user *buffer,
1121                         size_t count, loff_t *offset);
1122 extern int drm_release(struct inode *inode, struct file *filp);
1123
1124                                 /* Mapping support (drm_vm.h) */
1125 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
1126 extern int drm_mmap_locked(struct file *filp, struct vm_area_struct *vma);
1127 extern void drm_vm_open_locked(struct drm_device *dev, struct vm_area_struct *vma);
1128 extern void drm_vm_close_locked(struct drm_device *dev, struct vm_area_struct *vma);
1129 extern unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
1130
1131                                 /* Memory management support (drm_memory.h) */
1132 #include <drm/drm_memory.h>
1133
1134
1135                                 /* Misc. IOCTL support (drm_ioctl.h) */
1136 extern int drm_irq_by_busid(struct drm_device *dev, void *data,
1137                             struct drm_file *file_priv);
1138 extern int drm_getunique(struct drm_device *dev, void *data,
1139                          struct drm_file *file_priv);
1140 extern int drm_setunique(struct drm_device *dev, void *data,
1141                          struct drm_file *file_priv);
1142 extern int drm_getmap(struct drm_device *dev, void *data,
1143                       struct drm_file *file_priv);
1144 extern int drm_getclient(struct drm_device *dev, void *data,
1145                          struct drm_file *file_priv);
1146 extern int drm_getstats(struct drm_device *dev, void *data,
1147                         struct drm_file *file_priv);
1148 extern int drm_getcap(struct drm_device *dev, void *data,
1149                       struct drm_file *file_priv);
1150 extern int drm_setclientcap(struct drm_device *dev, void *data,
1151                             struct drm_file *file_priv);
1152 extern int drm_setversion(struct drm_device *dev, void *data,
1153                           struct drm_file *file_priv);
1154 extern int drm_noop(struct drm_device *dev, void *data,
1155                     struct drm_file *file_priv);
1156
1157                                 /* Authentication IOCTL support (drm_auth.h) */
1158 extern int drm_getmagic(struct drm_device *dev, void *data,
1159                         struct drm_file *file_priv);
1160 extern int drm_authmagic(struct drm_device *dev, void *data,
1161                          struct drm_file *file_priv);
1162 extern int drm_remove_magic(struct drm_master *master, drm_magic_t magic);
1163
1164 /* Cache management (drm_cache.c) */
1165 void drm_clflush_pages(struct page *pages[], unsigned long num_pages);
1166 void drm_clflush_sg(struct sg_table *st);
1167 void drm_clflush_virt_range(void *addr, unsigned long length);
1168
1169                                 /* Locking IOCTL support (drm_lock.h) */
1170 extern int drm_lock(struct drm_device *dev, void *data,
1171                     struct drm_file *file_priv);
1172 extern int drm_unlock(struct drm_device *dev, void *data,
1173                       struct drm_file *file_priv);
1174 extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context);
1175 extern void drm_idlelock_take(struct drm_lock_data *lock_data);
1176 extern void drm_idlelock_release(struct drm_lock_data *lock_data);
1177
1178 /*
1179  * These are exported to drivers so that they can implement fencing using
1180  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1181  */
1182
1183 extern int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv);
1184
1185                                 /* DMA support (drm_dma.h) */
1186 extern int drm_legacy_dma_setup(struct drm_device *dev);
1187 extern void drm_legacy_dma_takedown(struct drm_device *dev);
1188 extern void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf);
1189 extern void drm_core_reclaim_buffers(struct drm_device *dev,
1190                                      struct drm_file *filp);
1191
1192                                 /* IRQ support (drm_irq.h) */
1193 extern int drm_control(struct drm_device *dev, void *data,
1194                        struct drm_file *file_priv);
1195 extern int drm_irq_install(struct drm_device *dev, int irq);
1196 extern int drm_irq_uninstall(struct drm_device *dev);
1197
1198 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1199 extern int drm_wait_vblank(struct drm_device *dev, void *data,
1200                            struct drm_file *filp);
1201 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1202 extern u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
1203                                      struct timeval *vblanktime);
1204 extern void drm_send_vblank_event(struct drm_device *dev, int crtc,
1205                                      struct drm_pending_vblank_event *e);
1206 extern bool drm_handle_vblank(struct drm_device *dev, int crtc);
1207 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1208 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1209 extern int drm_crtc_vblank_get(struct drm_crtc *crtc);
1210 extern void drm_crtc_vblank_put(struct drm_crtc *crtc);
1211 extern void drm_wait_one_vblank(struct drm_device *dev, int crtc);
1212 extern void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
1213 extern void drm_vblank_off(struct drm_device *dev, int crtc);
1214 extern void drm_vblank_on(struct drm_device *dev, int crtc);
1215 extern void drm_crtc_vblank_off(struct drm_crtc *crtc);
1216 extern void drm_crtc_vblank_on(struct drm_crtc *crtc);
1217 extern void drm_vblank_cleanup(struct drm_device *dev);
1218
1219 extern u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
1220                                      struct timeval *tvblank, unsigned flags);
1221 extern int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
1222                                                  int crtc, int *max_error,
1223                                                  struct timeval *vblank_time,
1224                                                  unsigned flags,
1225                                                  const struct drm_crtc *refcrtc,
1226                                                  const struct drm_display_mode *mode);
1227 extern void drm_calc_timestamping_constants(struct drm_crtc *crtc,
1228                                             const struct drm_display_mode *mode);
1229
1230 /**
1231  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
1232  * @crtc: which CRTC's vblank waitqueue to retrieve
1233  *
1234  * This function returns a pointer to the vblank waitqueue for the CRTC.
1235  * Drivers can use this to implement vblank waits using wait_event() & co.
1236  */
1237 static inline wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
1238 {
1239         return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
1240 }
1241
1242 /* Modesetting support */
1243 extern void drm_vblank_pre_modeset(struct drm_device *dev, int crtc);
1244 extern void drm_vblank_post_modeset(struct drm_device *dev, int crtc);
1245 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1246                            struct drm_file *file_priv);
1247
1248                                 /* AGP/GART support (drm_agpsupport.h) */
1249
1250 #include <drm/drm_agpsupport.h>
1251
1252                                 /* Stub support (drm_stub.h) */
1253 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1254                                struct drm_file *file_priv);
1255 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1256                                 struct drm_file *file_priv);
1257 struct drm_master *drm_master_create(struct drm_minor *minor);
1258 extern struct drm_master *drm_master_get(struct drm_master *master);
1259 extern void drm_master_put(struct drm_master **master);
1260
1261 extern void drm_put_dev(struct drm_device *dev);
1262 extern void drm_unplug_dev(struct drm_device *dev);
1263 extern unsigned int drm_debug;
1264
1265 extern unsigned int drm_vblank_offdelay;
1266 extern unsigned int drm_timestamp_precision;
1267 extern unsigned int drm_timestamp_monotonic;
1268
1269 extern struct class *drm_class;
1270
1271                                 /* Debugfs support */
1272 #if defined(CONFIG_DEBUG_FS)
1273 extern int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1274                             struct dentry *root);
1275 extern int drm_debugfs_create_files(const struct drm_info_list *files,
1276                                     int count, struct dentry *root,
1277                                     struct drm_minor *minor);
1278 extern int drm_debugfs_remove_files(const struct drm_info_list *files,
1279                                     int count, struct drm_minor *minor);
1280 extern int drm_debugfs_cleanup(struct drm_minor *minor);
1281 extern int drm_debugfs_connector_add(struct drm_connector *connector);
1282 extern void drm_debugfs_connector_remove(struct drm_connector *connector);
1283 #else
1284 static inline int drm_debugfs_init(struct drm_minor *minor, int minor_id,
1285                                    struct dentry *root)
1286 {
1287         return 0;
1288 }
1289
1290 static inline int drm_debugfs_create_files(const struct drm_info_list *files,
1291                                            int count, struct dentry *root,
1292                                            struct drm_minor *minor)
1293 {
1294         return 0;
1295 }
1296
1297 static inline int drm_debugfs_remove_files(const struct drm_info_list *files,
1298                                            int count, struct drm_minor *minor)
1299 {
1300         return 0;
1301 }
1302
1303 static inline int drm_debugfs_cleanup(struct drm_minor *minor)
1304 {
1305         return 0;
1306 }
1307
1308 static inline int drm_debugfs_connector_add(struct drm_connector *connector)
1309 {
1310         return 0;
1311 }
1312 static inline void drm_debugfs_connector_remove(struct drm_connector *connector)
1313 {
1314 }
1315
1316 #endif
1317
1318                                 /* Info file support */
1319 extern int drm_name_info(struct seq_file *m, void *data);
1320 extern int drm_vm_info(struct seq_file *m, void *data);
1321 extern int drm_bufs_info(struct seq_file *m, void *data);
1322 extern int drm_vblank_info(struct seq_file *m, void *data);
1323 extern int drm_clients_info(struct seq_file *m, void* data);
1324 extern int drm_gem_name_info(struct seq_file *m, void *data);
1325
1326
1327 extern struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
1328                 struct drm_gem_object *obj, int flags);
1329 extern int drm_gem_prime_handle_to_fd(struct drm_device *dev,
1330                 struct drm_file *file_priv, uint32_t handle, uint32_t flags,
1331                 int *prime_fd);
1332 extern struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
1333                 struct dma_buf *dma_buf);
1334 extern int drm_gem_prime_fd_to_handle(struct drm_device *dev,
1335                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
1336 extern void drm_gem_dmabuf_release(struct dma_buf *dma_buf);
1337
1338 extern int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
1339                                         struct drm_file *file_priv);
1340 extern int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
1341                                         struct drm_file *file_priv);
1342
1343 extern int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
1344                                             dma_addr_t *addrs, int max_pages);
1345 extern struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages);
1346 extern void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg);
1347
1348 int drm_gem_dumb_destroy(struct drm_file *file,
1349                          struct drm_device *dev,
1350                          uint32_t handle);
1351
1352 void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv);
1353 void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv);
1354 void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf);
1355
1356 #if DRM_DEBUG_CODE
1357 extern int drm_vma_info(struct seq_file *m, void *data);
1358 #endif
1359
1360                                 /* Scatter Gather Support (drm_scatter.h) */
1361 extern void drm_legacy_sg_cleanup(struct drm_device *dev);
1362 extern int drm_sg_alloc(struct drm_device *dev, void *data,
1363                         struct drm_file *file_priv);
1364 extern int drm_sg_free(struct drm_device *dev, void *data,
1365                        struct drm_file *file_priv);
1366
1367                                /* ATI PCIGART support (ati_pcigart.h) */
1368 extern int drm_ati_pcigart_init(struct drm_device *dev,
1369                                 struct drm_ati_pcigart_info * gart_info);
1370 extern int drm_ati_pcigart_cleanup(struct drm_device *dev,
1371                                    struct drm_ati_pcigart_info * gart_info);
1372
1373 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1374                                        size_t align);
1375 extern void __drm_pci_free(struct drm_device *dev, drm_dma_handle_t * dmah);
1376 extern void drm_pci_free(struct drm_device *dev, drm_dma_handle_t * dmah);
1377 extern int drm_pci_set_unique(struct drm_device *dev,
1378                               struct drm_master *master,
1379                               struct drm_unique *u);
1380
1381                                 /* Legacy Support */
1382
1383 int drm_legacy_addmap(struct drm_device *d, resource_size_t offset,
1384                       unsigned int size, enum drm_map_type type,
1385                       enum drm_map_flags flags, struct drm_local_map **map_p);
1386 int drm_legacy_rmmap(struct drm_device *d, struct drm_local_map *map);
1387 int drm_legacy_rmmap_locked(struct drm_device *d, struct drm_local_map *map);
1388 struct drm_local_map *drm_legacy_getsarea(struct drm_device *dev);
1389
1390 int drm_legacy_addbufs_agp(struct drm_device *d, struct drm_buf_desc *req);
1391 int drm_legacy_addbufs_pci(struct drm_device *d, struct drm_buf_desc *req);
1392
1393 void drm_legacy_vma_flush(struct drm_device *d);
1394
1395                                /* sysfs support (drm_sysfs.c) */
1396 struct drm_sysfs_class;
1397 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1398 extern void drm_sysfs_destroy(void);
1399 extern struct device *drm_sysfs_minor_alloc(struct drm_minor *minor);
1400 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1401 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1402 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1403
1404 /* Graphics Execution Manager library functions (drm_gem.c) */
1405 int drm_gem_init(struct drm_device *dev);
1406 void drm_gem_destroy(struct drm_device *dev);
1407 void drm_gem_object_release(struct drm_gem_object *obj);
1408 void drm_gem_object_free(struct kref *kref);
1409 int drm_gem_object_init(struct drm_device *dev,
1410                         struct drm_gem_object *obj, size_t size);
1411 void drm_gem_private_object_init(struct drm_device *dev,
1412                                  struct drm_gem_object *obj, size_t size);
1413 void drm_gem_vm_open(struct vm_area_struct *vma);
1414 void drm_gem_vm_close(struct vm_area_struct *vma);
1415 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
1416                      struct vm_area_struct *vma);
1417 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
1418
1419 #include <drm/drm_global.h>
1420
1421 static inline void
1422 drm_gem_object_reference(struct drm_gem_object *obj)
1423 {
1424         kref_get(&obj->refcount);
1425 }
1426
1427 static inline void
1428 drm_gem_object_unreference(struct drm_gem_object *obj)
1429 {
1430         if (obj != NULL)
1431                 kref_put(&obj->refcount, drm_gem_object_free);
1432 }
1433
1434 static inline void
1435 drm_gem_object_unreference_unlocked(struct drm_gem_object *obj)
1436 {
1437         if (obj && !atomic_add_unless(&obj->refcount.refcount, -1, 1)) {
1438                 struct drm_device *dev = obj->dev;
1439
1440                 mutex_lock(&dev->struct_mutex);
1441                 if (likely(atomic_dec_and_test(&obj->refcount.refcount)))
1442                         drm_gem_object_free(&obj->refcount);
1443                 mutex_unlock(&dev->struct_mutex);
1444         }
1445 }
1446
1447 int drm_gem_handle_create_tail(struct drm_file *file_priv,
1448                                struct drm_gem_object *obj,
1449                                u32 *handlep);
1450 int drm_gem_handle_create(struct drm_file *file_priv,
1451                           struct drm_gem_object *obj,
1452                           u32 *handlep);
1453 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
1454
1455
1456 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
1457 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
1458 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
1459
1460 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
1461 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
1462                 bool dirty, bool accessed);
1463
1464 struct drm_gem_object *drm_gem_object_lookup(struct drm_device *dev,
1465                                              struct drm_file *filp,
1466                                              u32 handle);
1467 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1468                         struct drm_file *file_priv);
1469 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1470                         struct drm_file *file_priv);
1471 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1472                        struct drm_file *file_priv);
1473 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private);
1474 void drm_gem_release(struct drm_device *dev, struct drm_file *file_private);
1475
1476 extern void drm_core_ioremap(struct drm_local_map *map, struct drm_device *dev);
1477 extern void drm_core_ioremap_wc(struct drm_local_map *map, struct drm_device *dev);
1478 extern void drm_core_ioremapfree(struct drm_local_map *map, struct drm_device *dev);
1479
1480 static __inline__ struct drm_local_map *drm_core_findmap(struct drm_device *dev,
1481                                                          unsigned int token)
1482 {
1483         struct drm_map_list *_entry;
1484         list_for_each_entry(_entry, &dev->maplist, head)
1485             if (_entry->user_token == token)
1486                 return _entry->map;
1487         return NULL;
1488 }
1489
1490 static __inline__ void drm_core_dropmap(struct drm_local_map *map)
1491 {
1492 }
1493
1494 #include <drm/drm_mem_util.h>
1495
1496 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
1497                                  struct device *parent);
1498 void drm_dev_ref(struct drm_device *dev);
1499 void drm_dev_unref(struct drm_device *dev);
1500 int drm_dev_register(struct drm_device *dev, unsigned long flags);
1501 void drm_dev_unregister(struct drm_device *dev);
1502 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...);
1503
1504 struct drm_minor *drm_minor_acquire(unsigned int minor_id);
1505 void drm_minor_release(struct drm_minor *minor);
1506
1507 /*@}*/
1508
1509 /* PCI section */
1510 static __inline__ int drm_pci_device_is_agp(struct drm_device *dev)
1511 {
1512         if (dev->driver->device_is_agp != NULL) {
1513                 int err = (*dev->driver->device_is_agp) (dev);
1514
1515                 if (err != 2) {
1516                         return err;
1517                 }
1518         }
1519
1520         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1521 }
1522 void drm_pci_agp_destroy(struct drm_device *dev);
1523
1524 extern int drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver);
1525 extern void drm_pci_exit(struct drm_driver *driver, struct pci_driver *pdriver);
1526 extern int drm_get_pci_dev(struct pci_dev *pdev,
1527                            const struct pci_device_id *ent,
1528                            struct drm_driver *driver);
1529
1530 #define DRM_PCIE_SPEED_25 1
1531 #define DRM_PCIE_SPEED_50 2
1532 #define DRM_PCIE_SPEED_80 4
1533
1534 extern int drm_pcie_get_speed_cap_mask(struct drm_device *dev, u32 *speed_mask);
1535
1536 /* platform section */
1537 extern int drm_platform_init(struct drm_driver *driver, struct platform_device *platform_device);
1538
1539 /* returns true if currently okay to sleep */
1540 static __inline__ bool drm_can_sleep(void)
1541 {
1542         if (in_atomic() || in_dbg_master() || irqs_disabled())
1543                 return false;
1544         return true;
1545 }
1546
1547 #endif                          /* __KERNEL__ */
1548 #endif