[intel] Quirk away MSI support on 945G/GM.
[platform/upstream/libdrm.git] / linux-core / 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  * All rights reserved.
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the "Software"),
16  * to deal in the Software without restriction, including without limitation
17  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18  * and/or sell copies of the Software, and to permit persons to whom the
19  * Software is furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice (including the next
22  * paragraph) shall be included in all copies or substantial portions of the
23  * Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31  * OTHER DEALINGS IN THE SOFTWARE.
32  */
33
34 #ifndef _DRM_P_H_
35 #define _DRM_P_H_
36
37 #ifdef __KERNEL__
38 #ifdef __alpha__
39 /* add include of current.h so that "current" is defined
40  * before static inline funcs in wait.h. Doing this so we
41  * can build the DRM (part of PI DRI). 4/21/2000 S + B */
42 #include <asm/current.h>
43 #endif                          /* __alpha__ */
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/miscdevice.h>
47 #include <linux/fs.h>
48 #include <linux/proc_fs.h>
49 #include <linux/init.h>
50 #include <linux/file.h>
51 #include <linux/pci.h>
52 #include <linux/version.h>
53 #include <linux/sched.h>
54 #include <linux/smp_lock.h>     /* For (un)lock_kernel */
55 #include <linux/dma-mapping.h>
56 #include <linux/mm.h>
57 #include <linux/kref.h>
58 #include <linux/pagemap.h>
59 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
60 #include <linux/mutex.h>
61 #endif
62 #if defined(__alpha__) || defined(__powerpc__)
63 #include <asm/pgtable.h>        /* For pte_wrprotect */
64 #endif
65 #include <asm/io.h>
66 #include <asm/mman.h>
67 #include <asm/uaccess.h>
68 #ifdef CONFIG_MTRR
69 #include <asm/mtrr.h>
70 #endif
71 #include <asm/agp.h>
72 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
73 #include <linux/types.h>
74 #include <linux/agp_backend.h>
75 #endif
76 #include <linux/workqueue.h>
77 #include <linux/poll.h>
78 #include <asm/pgalloc.h>
79 #include "drm.h"
80 #include <linux/slab.h>
81 #include <linux/idr.h>
82
83 #define __OS_HAS_AGP (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
84 #define __OS_HAS_MTRR (defined(CONFIG_MTRR))
85
86 #include "drm_os_linux.h"
87 #include "drm_hashtab.h"
88 #include "drm_internal.h"
89
90 struct drm_device;
91 struct drm_file;
92
93 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
94 typedef unsigned long uintptr_t;
95 #endif
96
97 /* If you want the memory alloc debug functionality, change define below */
98 /* #define DEBUG_MEMORY */
99
100 /***********************************************************************/
101 /** \name DRM template customization defaults */
102 /*@{*/
103
104 /* driver capabilities and requirements mask */
105 #define DRIVER_USE_AGP     0x1
106 #define DRIVER_REQUIRE_AGP 0x2
107 #define DRIVER_USE_MTRR    0x4
108 #define DRIVER_PCI_DMA     0x8
109 #define DRIVER_SG          0x10
110 #define DRIVER_HAVE_DMA    0x20
111 #define DRIVER_HAVE_IRQ    0x40
112 #define DRIVER_IRQ_SHARED  0x80
113 #define DRIVER_DMA_QUEUE   0x100
114 #define DRIVER_FB_DMA      0x200
115 #define DRIVER_GEM         0x400
116
117 /*@}*/
118
119 /***********************************************************************/
120 /** \name Begin the DRM... */
121 /*@{*/
122
123 #define DRM_DEBUG_CODE 2          /**< Include debugging code if > 1, then
124                                      also include looping detection. */
125
126 #define DRM_MAGIC_HASH_ORDER  4 /**< Size of key hash table. Must be power of 2. */
127 #define DRM_KERNEL_CONTEXT    0  /**< Change drm_resctx if changed */
128 #define DRM_RESERVED_CONTEXTS 1  /**< Change drm_resctx if changed */
129 #define DRM_LOOPING_LIMIT     5000000
130 #define DRM_TIME_SLICE        (HZ/20)  /**< Time slice for GLXContexts */
131 #define DRM_LOCK_SLICE        1 /**< Time slice for lock, in jiffies */
132
133 #define DRM_FLAG_DEBUG    0x01
134
135 #define DRM_MEM_DMA        0
136 #define DRM_MEM_SAREA      1
137 #define DRM_MEM_DRIVER     2
138 #define DRM_MEM_MAGIC      3
139 #define DRM_MEM_IOCTLS     4
140 #define DRM_MEM_MAPS       5
141 #define DRM_MEM_VMAS       6
142 #define DRM_MEM_BUFS       7
143 #define DRM_MEM_SEGS       8
144 #define DRM_MEM_PAGES      9
145 #define DRM_MEM_FILES     10
146 #define DRM_MEM_QUEUES    11
147 #define DRM_MEM_CMDS      12
148 #define DRM_MEM_MAPPINGS  13
149 #define DRM_MEM_BUFLISTS  14
150 #define DRM_MEM_AGPLISTS  15
151 #define DRM_MEM_TOTALAGP  16
152 #define DRM_MEM_BOUNDAGP  17
153 #define DRM_MEM_CTXBITMAP 18
154 #define DRM_MEM_STUB      19
155 #define DRM_MEM_SGLISTS   20
156 #define DRM_MEM_CTXLIST   21
157 #define DRM_MEM_MM        22
158 #define DRM_MEM_HASHTAB   23
159 #define DRM_MEM_OBJECTS   24
160 #define DRM_MEM_FENCE     25
161 #define DRM_MEM_TTM       26
162 #define DRM_MEM_BUFOBJ    27
163
164 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
165 #define DRM_MAP_HASH_OFFSET 0x10000000
166 #define DRM_MAP_HASH_ORDER 12
167 #define DRM_OBJECT_HASH_ORDER 12
168 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
169 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
170 /*
171  * This should be small enough to allow the use of kmalloc for hash tables
172  * instead of vmalloc.
173  */
174
175 #define DRM_FILE_HASH_ORDER 8
176 #define DRM_MM_INIT_MAX_PAGES 256
177
178 /*@}*/
179
180 #include "drm_compat.h"
181
182 /***********************************************************************/
183 /** \name Macros to make printk easier */
184 /*@{*/
185
186 /**
187  * Error output.
188  *
189  * \param fmt printf() like format string.
190  * \param arg arguments
191  */
192 #define DRM_ERROR(fmt, arg...) \
193         printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* " fmt , __FUNCTION__ , ##arg)
194
195 /**
196  * Memory error output.
197  *
198  * \param area memory area where the error occurred.
199  * \param fmt printf() like format string.
200  * \param arg arguments
201  */
202 #define DRM_MEM_ERROR(area, fmt, arg...) \
203         printk(KERN_ERR "[" DRM_NAME ":%s:%s] *ERROR* " fmt , __FUNCTION__, \
204                drm_mem_stats[area].name , ##arg)
205 #define DRM_INFO(fmt, arg...)  printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
206
207 /**
208  * Debug output.
209  *
210  * \param fmt printf() like format string.
211  * \param arg arguments
212  */
213 #if DRM_DEBUG_CODE
214 #define DRM_DEBUG(fmt, arg...)                                          \
215         do {                                                            \
216                 if ( drm_debug )                                        \
217                         printk(KERN_DEBUG                               \
218                                "[" DRM_NAME ":%s] " fmt ,               \
219                                __FUNCTION__ , ##arg);                   \
220         } while (0)
221 #else
222 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
223 #endif
224
225 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
226
227 #define DRM_PROC_PRINT(fmt, arg...)                                     \
228    len += sprintf(&buf[len], fmt , ##arg);                              \
229    if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
230
231 #define DRM_PROC_PRINT_RET(ret, fmt, arg...)                            \
232    len += sprintf(&buf[len], fmt , ##arg);                              \
233    if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
234
235 /*@}*/
236
237 /***********************************************************************/
238 /** \name Internal types and structures */
239 /*@{*/
240
241 #define DRM_ARRAY_SIZE(x) ARRAY_SIZE(x)
242 #define DRM_MIN(a,b) min(a,b)
243 #define DRM_MAX(a,b) max(a,b)
244
245 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
246 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
247 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
248
249 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
250 /**
251  * Get the private SAREA mapping.
252  *
253  * \param _dev DRM device.
254  * \param _ctx context number.
255  * \param _map output mapping.
256  */
257 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
258         (_map) = (_dev)->context_sareas[_ctx];          \
259 } while(0)
260
261 /**
262  * Test that the hardware lock is held by the caller, returning otherwise.
263  *
264  * \param dev DRM device.
265  * \param file_priv DRM file private pointer of the caller.
266  */
267 #define LOCK_TEST_WITH_RETURN( dev, file_priv )                         \
268 do {                                                                    \
269         if ( !_DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ) ||           \
270              dev->lock.file_priv != file_priv ) {                       \
271                 DRM_ERROR( "%s called without lock held, held  %d owner %p %p\n",\
272                            __FUNCTION__, _DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ),\
273                            dev->lock.file_priv, file_priv );            \
274                 return -EINVAL;                                         \
275         }                                                               \
276 } while (0)
277
278 /**
279  * Copy and IOCTL return string to user space
280  */
281 #define DRM_COPY( name, value )                                         \
282         len = strlen( value );                                          \
283         if ( len > name##_len ) len = name##_len;                       \
284         name##_len = strlen( value );                                   \
285         if ( len && name ) {                                            \
286                 if ( copy_to_user( name, value, len ) )                 \
287                         return -EFAULT;                                 \
288         }
289
290 /**
291  * Ioctl function type.
292  *
293  * \param dev DRM device structure
294  * \param data pointer to kernel-space stored data, copied in and out according
295  *             to ioctl description.
296  * \param file_priv DRM file private pointer.
297  */
298 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
299                         struct drm_file *file_priv);
300
301 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
302                                unsigned long arg);
303
304 #define DRM_AUTH        0x1
305 #define DRM_MASTER      0x2
306 #define DRM_ROOT_ONLY   0x4
307
308 struct drm_ioctl_desc {
309         unsigned int cmd;
310         drm_ioctl_t *func;
311         int flags;
312 };
313 /**
314  * Creates a driver or general drm_ioctl_desc array entry for the given
315  * ioctl, for use by drm_ioctl().
316  */
317 #define DRM_IOCTL_DEF(ioctl, func, flags) \
318         [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
319
320 struct drm_magic_entry {
321         struct list_head head;
322         struct drm_hash_item hash_item;
323         struct drm_file *priv;
324 };
325
326 struct drm_vma_entry {
327         struct list_head head;
328         struct vm_area_struct *vma;
329         pid_t pid;
330 };
331
332 /**
333  * DMA buffer.
334  */
335 struct drm_buf {
336         int idx;                       /**< Index into master buflist */
337         int total;                     /**< Buffer size */
338         int order;                     /**< log-base-2(total) */
339         int used;                      /**< Amount of buffer in use (for DMA) */
340         unsigned long offset;          /**< Byte offset (used internally) */
341         void *address;                 /**< Address of buffer */
342         unsigned long bus_address;     /**< Bus address of buffer */
343         struct drm_buf *next;          /**< Kernel-only: used for free list */
344         __volatile__ int waiting;      /**< On kernel DMA queue */
345         __volatile__ int pending;      /**< On hardware DMA queue */
346         wait_queue_head_t dma_wait;    /**< Processes waiting */
347         struct drm_file *file_priv;    /**< Private of holding file descr */
348         int context;                   /**< Kernel queue for this buffer */
349         int while_locked;              /**< Dispatch this buffer while locked */
350         enum {
351                 DRM_LIST_NONE = 0,
352                 DRM_LIST_FREE = 1,
353                 DRM_LIST_WAIT = 2,
354                 DRM_LIST_PEND = 3,
355                 DRM_LIST_PRIO = 4,
356                 DRM_LIST_RECLAIM = 5
357         } list;                        /**< Which list we're on */
358
359         int dev_priv_size;              /**< Size of buffer private storage */
360         void *dev_private;              /**< Per-buffer private storage */
361 };
362
363 /** bufs is one longer than it has to be */
364 struct drm_waitlist {
365         int count;                      /**< Number of possible buffers */
366         struct drm_buf **bufs;          /**< List of pointers to buffers */
367         struct drm_buf **rp;                    /**< Read pointer */
368         struct drm_buf **wp;                    /**< Write pointer */
369         struct drm_buf **end;           /**< End pointer */
370         spinlock_t read_lock;
371         spinlock_t write_lock;
372 };
373
374 struct drm_freelist {
375         int initialized;               /**< Freelist in use */
376         atomic_t count;                /**< Number of free buffers */
377         struct drm_buf *next;          /**< End pointer */
378
379         wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
380         int low_mark;                  /**< Low water mark */
381         int high_mark;                 /**< High water mark */
382         atomic_t wfh;                  /**< If waiting for high mark */
383         spinlock_t lock;
384 };
385
386 typedef struct drm_dma_handle {
387         dma_addr_t busaddr;
388         void *vaddr;
389         size_t size;
390 } drm_dma_handle_t;
391
392 /**
393  * Buffer entry.  There is one of this for each buffer size order.
394  */
395 struct drm_buf_entry {
396         int buf_size;                   /**< size */
397         int buf_count;                  /**< number of buffers */
398         struct drm_buf *buflist;                /**< buffer list */
399         int seg_count;
400         int page_order;
401         struct drm_dma_handle **seglist;
402         struct drm_freelist freelist;
403 };
404
405
406 enum drm_ref_type {
407         _DRM_REF_USE = 0,
408         _DRM_REF_TYPE1,
409         _DRM_NO_REF_TYPES
410 };
411
412
413 /** File private data */
414 struct drm_file {
415         int authenticated;
416         int master;
417         pid_t pid;
418         uid_t uid;
419         drm_magic_t magic;
420         unsigned long ioctl_count;
421         struct list_head lhead;
422         struct drm_minor *minor;
423         int remove_auth_on_close;
424         unsigned long lock_count;
425
426         /*
427          * The user object hash table is global and resides in the
428          * drm_device structure. We protect the lists and hash tables with the
429          * device struct_mutex. A bit coarse-grained but probably the best
430          * option.
431          */
432
433         struct list_head refd_objects;
434
435         /** Mapping of mm object handles to object pointers. */
436         struct idr object_idr;
437         /** Lock for synchronization of access to object_idr. */
438         spinlock_t table_lock;
439
440         struct drm_open_hash refd_object_hash[_DRM_NO_REF_TYPES];
441         struct file *filp;
442         void *driver_priv;
443 };
444
445 /** Wait queue */
446 struct drm_queue {
447         atomic_t use_count;             /**< Outstanding uses (+1) */
448         atomic_t finalization;          /**< Finalization in progress */
449         atomic_t block_count;           /**< Count of processes waiting */
450         atomic_t block_read;            /**< Queue blocked for reads */
451         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
452         atomic_t block_write;           /**< Queue blocked for writes */
453         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
454 #if 1
455         atomic_t total_queued;          /**< Total queued statistic */
456         atomic_t total_flushed;         /**< Total flushes statistic */
457         atomic_t total_locks;           /**< Total locks statistics */
458 #endif
459         enum drm_ctx_flags flags;       /**< Context preserving and 2D-only */
460         struct drm_waitlist waitlist;   /**< Pending buffers */
461         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
462 };
463
464 /**
465  * Lock data.
466  */
467 struct drm_lock_data {
468         struct drm_hw_lock *hw_lock;            /**< Hardware lock */
469         /** Private of lock holder's file (NULL=kernel) */
470         struct drm_file *file_priv;
471         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
472         unsigned long lock_time;        /**< Time of last lock in jiffies */
473         spinlock_t spinlock;
474         uint32_t kernel_waiters;
475         uint32_t user_waiters;
476         int idle_has_lock;
477         /**
478          * Boolean signaling that the lock is held on behalf of the
479          * file_priv client by the kernel in an ioctl handler.
480          */
481         int kernel_held;
482 };
483
484 /**
485  * DMA data.
486  */
487 struct drm_device_dma {
488
489         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
490         int buf_count;                  /**< total number of buffers */
491         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
492         int seg_count;
493         int page_count;                 /**< number of pages */
494         unsigned long *pagelist;        /**< page list */
495         unsigned long byte_count;
496         enum {
497                 _DRM_DMA_USE_AGP = 0x01,
498                 _DRM_DMA_USE_SG = 0x02,
499                 _DRM_DMA_USE_FB = 0x04,
500                 _DRM_DMA_USE_PCI_RO = 0x08
501         } flags;
502
503 };
504
505 /**
506  * AGP memory entry.  Stored as a doubly linked list.
507  */
508 struct drm_agp_mem {
509         unsigned long handle;           /**< handle */
510         DRM_AGP_MEM *memory;
511         unsigned long bound;            /**< address */
512         int pages;
513         struct list_head head;
514 };
515
516 /**
517  * AGP data.
518  *
519  * \sa drm_agp_init() and drm_device::agp.
520  */
521 struct drm_agp_head {
522         DRM_AGP_KERN agp_info;          /**< AGP device information */
523         struct list_head memory;
524         unsigned long mode;             /**< AGP mode */
525 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11)
526         struct agp_bridge_data *bridge;
527 #endif
528         int enabled;                    /**< whether the AGP bus as been enabled */
529         int acquired;                   /**< whether the AGP device has been acquired */
530         unsigned long base;
531         int agp_mtrr;
532         int cant_use_aperture;
533         unsigned long page_mask;
534 };
535
536 /**
537  * Scatter-gather memory.
538  */
539 struct drm_sg_mem {
540         unsigned long handle;
541         void *virtual;
542         int pages;
543         struct page **pagelist;
544         dma_addr_t *busaddr;
545 };
546
547 struct drm_sigdata {
548         int context;
549         struct drm_hw_lock *lock;
550 };
551
552
553 /*
554  * Generic memory manager structs
555  */
556
557 struct drm_memrange_node {
558         struct list_head fl_entry;
559         struct list_head ml_entry;
560         int free;
561         unsigned long start;
562         unsigned long size;
563         struct drm_memrange *mm;
564         void *private;
565 };
566
567 struct drm_memrange {
568         struct list_head fl_entry;
569         struct list_head ml_entry;
570 };
571
572
573 /**
574  * Mappings list
575  */
576 struct drm_map_list {
577         struct list_head head;          /**< list head */
578         struct drm_hash_item hash;
579         struct drm_map *map;                    /**< mapping */
580         uint64_t user_token;
581         struct drm_memrange_node *file_offset_node;
582 };
583
584 typedef struct drm_map drm_local_map_t;
585
586 /**
587  * Context handle list
588  */
589 struct drm_ctx_list {
590         struct list_head head;          /**< list head */
591         drm_context_t handle;           /**< context handle */
592         struct drm_file *tag;           /**< associated fd private data */
593 };
594
595 struct drm_vbl_sig {
596         struct list_head head;
597         unsigned int sequence;
598         struct siginfo info;
599         struct task_struct *task;
600 };
601
602 /* location of GART table */
603 #define DRM_ATI_GART_MAIN 1
604 #define DRM_ATI_GART_FB   2
605
606 #define DRM_ATI_GART_PCI 1
607 #define DRM_ATI_GART_PCIE 2
608 #define DRM_ATI_GART_IGP 3
609
610 struct drm_ati_pcigart_info {
611         int gart_table_location;
612         int gart_reg_if;
613         void *addr;
614         dma_addr_t bus_addr;
615         dma_addr_t table_mask;
616         dma_addr_t member_mask;
617         struct drm_dma_handle *table_handle;
618         drm_local_map_t mapping;
619         int table_size;
620 };
621
622 /**
623  * This structure defines the drm_mm memory object, which will be used by the
624  * DRM for its buffer objects.
625  */
626 struct drm_gem_object {
627         /** Reference count of this object */
628         struct kref refcount;
629
630         /** Handle count of this object. Each handle also holds a reference */
631         struct kref handlecount;
632
633         /** Related drm device */
634         struct drm_device *dev;
635         
636         /** File representing the shmem storage */
637         struct file *filp;
638
639         /**
640          * Size of the object, in bytes.  Immutable over the object's
641          * lifetime.
642          */
643         size_t size;
644
645         /**
646          * Global name for this object, starts at 1. 0 means unnamed.
647          * Access is covered by the object_name_lock in the related drm_device
648          */
649         int name;
650
651         /**
652          * Memory domains. These monitor which caches contain read/write data
653          * related to the object. When transitioning from one set of domains
654          * to another, the driver is called to ensure that caches are suitably
655          * flushed and invalidated
656          */
657         uint32_t        read_domains;
658         uint32_t        write_domain;
659
660         /**
661          * While validating an exec operation, the
662          * new read/write domain values are computed here.
663          * They will be transferred to the above values
664          * at the point that any cache flushing occurs
665          */
666         uint32_t        pending_read_domains;
667         uint32_t        pending_write_domain;
668
669         void *driver_private;
670 };
671
672 #include "drm_objects.h"
673
674 /**
675  * DRM driver structure. This structure represent the common code for
676  * a family of cards. There will one drm_device for each card present
677  * in this family
678  */
679
680 struct drm_driver {
681         int (*load) (struct drm_device *, unsigned long flags);
682         int (*firstopen) (struct drm_device *);
683         int (*open) (struct drm_device *, struct drm_file *);
684         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
685         void (*postclose) (struct drm_device *, struct drm_file *);
686         void (*lastclose) (struct drm_device *);
687         int (*unload) (struct drm_device *);
688         int (*suspend) (struct drm_device *, pm_message_t state);
689         int (*resume) (struct drm_device *);
690         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
691         void (*dma_ready) (struct drm_device *);
692         int (*dma_quiescent) (struct drm_device *);
693         int (*context_ctor) (struct drm_device *dev, int context);
694         int (*context_dtor) (struct drm_device *dev, int context);
695         int (*kernel_context_switch) (struct drm_device *dev, int old,
696                                       int new);
697         void (*kernel_context_switch_unlock) (struct drm_device * dev);
698         /**
699          * get_vblank_counter - get raw hardware vblank counter
700          * @dev: DRM device
701          * @crtc: counter to fetch
702          *
703          * Driver callback for fetching a raw hardware vblank counter
704          * for @crtc.  If a device doesn't have a hardware counter, the
705          * driver can simply return the value of drm_vblank_count and
706          * make the enable_vblank() and disable_vblank() hooks into no-ops,
707          * leaving interrupts enabled at all times.
708          *
709          * Wraparound handling and loss of events due to modesetting is dealt
710          * with in the DRM core code.
711          *
712          * RETURNS
713          * Raw vblank counter value.
714          */
715         u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
716
717         /**
718          * enable_vblank - enable vblank interrupt events
719          * @dev: DRM device
720          * @crtc: which irq to enable
721          *
722          * Enable vblank interrupts for @crtc.  If the device doesn't have
723          * a hardware vblank counter, this routine should be a no-op, since
724          * interrupts will have to stay on to keep the count accurate.
725          *
726          * RETURNS
727          * Zero on success, appropriate errno if the given @crtc's vblank
728          * interrupt cannot be enabled.
729          */
730         int (*enable_vblank) (struct drm_device *dev, int crtc);
731
732         /**
733          * disable_vblank - disable vblank interrupt events
734          * @dev: DRM device
735          * @crtc: which irq to enable
736          *
737          * Disable vblank interrupts for @crtc.  If the device doesn't have
738          * a hardware vblank counter, this routine should be a no-op, since
739          * interrupts will have to stay on to keep the count accurate.
740          */
741         void (*disable_vblank) (struct drm_device *dev, int crtc);
742         int (*dri_library_name) (struct drm_device *dev, char * buf);
743
744         /**
745          * Called by \c drm_device_is_agp.  Typically used to determine if a
746          * card is really attached to AGP or not.
747          *
748          * \param dev  DRM device handle
749          *
750          * \returns
751          * One of three values is returned depending on whether or not the
752          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
753          * (return of 1), or may or may not be AGP (return of 2).
754          */
755         int (*device_is_agp) (struct drm_device *dev);
756
757 /* these have to be filled in */
758          irqreturn_t(*irq_handler) (DRM_IRQ_ARGS);
759         void (*irq_preinstall) (struct drm_device *dev);
760         int (*irq_postinstall) (struct drm_device *dev);
761         void (*irq_uninstall) (struct drm_device *dev);
762         void (*reclaim_buffers) (struct drm_device *dev,
763                                  struct drm_file *file_priv);
764         void (*reclaim_buffers_locked) (struct drm_device *dev,
765                                         struct drm_file *file_priv);
766         void (*reclaim_buffers_idlelocked) (struct drm_device *dev,
767                                             struct drm_file *file_priv);
768         unsigned long (*get_map_ofs) (struct drm_map *map);
769         unsigned long (*get_reg_ofs) (struct drm_device *dev);
770         void (*set_version) (struct drm_device *dev,
771                              struct drm_set_version *sv);
772
773         /**
774          * Driver-specific constructor for drm_gem_objects, to set up
775          * obj->driver_private.
776          *
777          * Returns 0 on success.
778          */
779         int (*gem_init_object) (struct drm_gem_object *obj);
780         void (*gem_free_object) (struct drm_gem_object *obj);
781
782         struct drm_fence_driver *fence_driver;
783         struct drm_bo_driver *bo_driver;
784
785         int major;
786         int minor;
787         int patchlevel;
788         char *name;
789         char *desc;
790         char *date;
791
792 /* variables */
793         u32 driver_features;
794         int dev_priv_size;
795         struct drm_ioctl_desc *ioctls;
796         int num_ioctls;
797         struct file_operations fops;
798         struct pci_driver pci_driver;
799 };
800
801 #define DRM_MINOR_UNASSIGNED 0
802 #define DRM_MINOR_LEGACY 1
803
804 /**
805  * DRM minor structure. This structure represents a drm minor number.
806  */
807 struct drm_minor {
808         int index;                      /**< Minor device number */
809         int type;                       /**< Control or render */
810         dev_t device;                   /**< Device number for mknod */
811         struct device kdev;             /**< Linux device */
812         struct drm_device *dev;
813         struct proc_dir_entry *dev_root;  /**< proc directory entry */
814         struct class_device *dev_class;
815 };
816
817
818 /**
819  * DRM device structure. This structure represent a complete card that
820  * may contain multiple heads.
821  */
822 struct drm_device {
823         char *unique;                   /**< Unique identifier: e.g., busid */
824         int unique_len;                 /**< Length of unique field */
825         char *devname;                  /**< For /proc/interrupts */
826         int if_version;                 /**< Highest interface version set */
827
828         int blocked;                    /**< Blocked due to VC switch? */
829
830         /** \name Locks */
831         /*@{ */
832         spinlock_t count_lock;          /**< For inuse, drm_device::open_count, drm_device::buf_use */
833         struct mutex struct_mutex;      /**< For others */
834         /*@} */
835
836         /** \name Usage Counters */
837         /*@{ */
838         int open_count;                 /**< Outstanding files open */
839         atomic_t ioctl_count;           /**< Outstanding IOCTLs pending */
840         atomic_t vma_count;             /**< Outstanding vma areas open */
841         int buf_use;                    /**< Buffers in use -- cannot alloc */
842         atomic_t buf_alloc;             /**< Buffer allocation in progress */
843         /*@} */
844
845         /** \name Performance counters */
846         /*@{ */
847         unsigned long counters;
848         enum drm_stat_type types[15];
849         atomic_t counts[15];
850         /*@} */
851
852         /** \name Authentication */
853         /*@{ */
854         struct list_head filelist;
855         struct drm_open_hash magiclist;
856         struct list_head magicfree;
857         /*@} */
858
859         /** \name Memory management */
860         /*@{ */
861         struct list_head maplist;       /**< Linked list of regions */
862         int map_count;                  /**< Number of mappable regions */
863         struct drm_open_hash map_hash;       /**< User token hash table for maps */
864         struct drm_memrange offset_manager;  /**< User token manager */
865         struct drm_open_hash object_hash;    /**< User token hash table for objects */
866         struct address_space *dev_mapping;  /**< For unmap_mapping_range() */
867         struct page *ttm_dummy_page;
868
869         /** \name Context handle management */
870         /*@{ */
871         struct list_head ctxlist;       /**< Linked list of context handles */
872         int ctx_count;                  /**< Number of context handles */
873         struct mutex ctxlist_mutex;     /**< For ctxlist */
874
875         struct idr ctx_idr;
876
877         struct list_head vmalist;       /**< List of vmas (for debugging) */
878         struct drm_lock_data lock;              /**< Information on hardware lock */
879         /*@} */
880
881         /** \name DMA queues (contexts) */
882         /*@{ */
883         int queue_count;                /**< Number of active DMA queues */
884         int queue_reserved;             /**< Number of reserved DMA queues */
885         int queue_slots;                /**< Actual length of queuelist */
886         struct drm_queue **queuelist;   /**< Vector of pointers to DMA queues */
887         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
888         /*@} */
889
890         /** \name Context support */
891         /*@{ */
892         int irq;                        /**< Interrupt used by board */
893         int irq_enabled;                /**< True if irq handler is enabled */
894         __volatile__ long context_flag; /**< Context swapping flag */
895         __volatile__ long interrupt_flag; /**< Interruption handler flag */
896         __volatile__ long dma_flag;     /**< DMA dispatch flag */
897         struct timer_list timer;        /**< Timer for delaying ctx switch */
898         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
899         int last_checked;               /**< Last context checked for DMA */
900         int last_context;               /**< Last current context */
901         unsigned long last_switch;      /**< jiffies at last context switch */
902         /*@} */
903
904         struct work_struct work;
905
906         /** \name VBLANK IRQ support */
907         /*@{ */
908
909         wait_queue_head_t *vbl_queue;   /**< VBLANK wait queue */
910         atomic_t *_vblank_count;        /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
911         spinlock_t vbl_lock;
912         struct list_head *vbl_sigs;             /**< signal list to send on VBLANK */
913         atomic_t vbl_signal_pending;    /* number of signals pending on all crtcs*/
914         atomic_t *vblank_refcount;      /* number of users of vblank interrupts per crtc */
915         u32 *last_vblank;               /* protected by dev->vbl_lock, used */
916                                         /* for wraparound handling */
917         u32 *vblank_offset;             /* used to track how many vblanks */
918         int *vblank_enabled;            /* so we don't call enable more than
919                                            once per disable */
920         u32 *vblank_premodeset;         /*  were lost during modeset */
921         struct timer_list vblank_disable_timer;
922
923         unsigned long max_vblank_count; /**< size of vblank counter register */
924         spinlock_t tasklet_lock;        /**< For drm_locked_tasklet */
925         void (*locked_tasklet_func)(struct drm_device *dev);
926
927         /*@} */
928         cycles_t ctx_start;
929         cycles_t lck_start;
930
931         struct fasync_struct *buf_async;/**< Processes waiting for SIGIO */
932         wait_queue_head_t buf_readers;  /**< Processes waiting to read */
933         wait_queue_head_t buf_writers;  /**< Processes waiting to ctx switch */
934
935         struct drm_agp_head *agp;               /**< AGP data */
936
937         struct pci_dev *pdev;           /**< PCI device structure */
938         int pci_vendor;                 /**< PCI vendor id */
939         int pci_device;                 /**< PCI device id */
940 #ifdef __alpha__
941         struct pci_controller *hose;
942 #endif
943         int num_crtcs;                  /**< Number of CRTCs on this device */
944         struct drm_sg_mem *sg;          /**< Scatter gather memory */
945         void *dev_private;              /**< device private data */
946         struct drm_sigdata sigdata;             /**< For block_all_signals */
947         sigset_t sigmask;
948
949         struct drm_driver *driver;
950         drm_local_map_t *agp_buffer_map;
951         unsigned int agp_buffer_token;
952         struct drm_minor *primary;              /**< render type primary screen head */
953
954         struct drm_fence_manager fm;
955         struct drm_buffer_manager bm;
956
957         /** \name Drawable information */
958         /*@{ */
959         spinlock_t drw_lock;
960         struct idr drw_idr;
961         /*@} */
962
963         /** \name GEM information */
964         /*@{ */
965         spinlock_t object_name_lock;
966         struct idr object_name_idr;
967         atomic_t object_count;
968         atomic_t object_memory;
969         atomic_t pin_count;
970         atomic_t pin_memory;
971         atomic_t gtt_count;
972         atomic_t gtt_memory;
973         uint32_t gtt_total;
974         uint32_t invalidate_domains;    /* domains pending invalidation */
975         uint32_t flush_domains;         /* domains pending flush */
976         /*@} */
977 };
978
979 #if __OS_HAS_AGP
980 struct drm_agp_ttm_backend {
981         struct drm_ttm_backend backend;
982         DRM_AGP_MEM *mem;
983         struct agp_bridge_data *bridge;
984         int populated;
985 };
986 #endif
987
988
989 static __inline__ int drm_core_check_feature(struct drm_device *dev,
990                                              int feature)
991 {
992         return ((dev->driver->driver_features & feature) ? 1 : 0);
993 }
994
995 #ifdef __alpha__
996 #define drm_get_pci_domain(dev) dev->hose->index
997 #else
998 #define drm_get_pci_domain(dev) 0
999 #endif
1000
1001 #if __OS_HAS_AGP
1002 static inline int drm_core_has_AGP(struct drm_device *dev)
1003 {
1004         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1005 }
1006 #else
1007 #define drm_core_has_AGP(dev) (0)
1008 #endif
1009
1010 #if __OS_HAS_MTRR
1011 static inline int drm_core_has_MTRR(struct drm_device *dev)
1012 {
1013         return drm_core_check_feature(dev, DRIVER_USE_MTRR);
1014 }
1015
1016 #define DRM_MTRR_WC             MTRR_TYPE_WRCOMB
1017
1018 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
1019                                unsigned int flags)
1020 {
1021         return mtrr_add(offset, size, flags, 1);
1022 }
1023
1024 static inline int drm_mtrr_del(int handle, unsigned long offset,
1025                                unsigned long size, unsigned int flags)
1026 {
1027         return mtrr_del(handle, offset, size);
1028 }
1029
1030 #else
1031 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
1032                                unsigned int flags)
1033 {
1034         return -ENODEV;
1035 }
1036
1037 static inline int drm_mtrr_del(int handle, unsigned long offset,
1038                                unsigned long size, unsigned int flags)
1039 {
1040         return -ENODEV;
1041 }
1042
1043 #define drm_core_has_MTRR(dev) (0)
1044 #define DRM_MTRR_WC             0
1045 #endif
1046
1047
1048 /******************************************************************/
1049 /** \name Internal function definitions */
1050 /*@{*/
1051
1052                                 /* Driver support (drm_drv.h) */
1053 extern int drm_fb_loaded;
1054 extern int drm_init(struct drm_driver *driver,
1055                               struct pci_device_id *pciidlist);
1056 extern void drm_exit(struct drm_driver *driver);
1057 extern void drm_cleanup_pci(struct pci_dev *pdev);
1058 extern int drm_ioctl(struct inode *inode, struct file *filp,
1059                      unsigned int cmd, unsigned long arg);
1060 extern long drm_unlocked_ioctl(struct file *filp,
1061                                unsigned int cmd, unsigned long arg);
1062 extern long drm_compat_ioctl(struct file *filp,
1063                              unsigned int cmd, unsigned long arg);
1064
1065 extern int drm_lastclose(struct drm_device *dev);
1066
1067                                 /* Device support (drm_fops.h) */
1068 extern int drm_open(struct inode *inode, struct file *filp);
1069 extern int drm_stub_open(struct inode *inode, struct file *filp);
1070 extern int drm_fasync(int fd, struct file *filp, int on);
1071 extern int drm_release(struct inode *inode, struct file *filp);
1072 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
1073
1074                                 /* Mapping support (drm_vm.h) */
1075 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
1076 extern unsigned long drm_core_get_map_ofs(struct drm_map * map);
1077 extern unsigned long drm_core_get_reg_ofs(struct drm_device *dev);
1078 extern pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma);
1079
1080                                 /* Memory management support (drm_memory.h) */
1081 #include "drm_memory.h"
1082 extern void drm_mem_init(void);
1083 extern int drm_mem_info(char *buf, char **start, off_t offset,
1084                         int request, int *eof, void *data);
1085 extern void *drm_calloc(size_t nmemb, size_t size, int area);
1086 extern void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area);
1087 extern unsigned long drm_alloc_pages(int order, int area);
1088 extern void drm_free_pages(unsigned long address, int order, int area);
1089 extern DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type);
1090 extern int drm_free_agp(DRM_AGP_MEM * handle, int pages);
1091 extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start);
1092 extern DRM_AGP_MEM *drm_agp_bind_pages(struct drm_device *dev,
1093                                               struct page **pages,
1094                                               unsigned long num_pages,
1095                                               uint32_t gtt_offset);
1096 extern int drm_unbind_agp(DRM_AGP_MEM * handle);
1097
1098 extern void drm_free_memctl(size_t size);
1099 extern int drm_alloc_memctl(size_t size);
1100 extern void drm_query_memctl(uint64_t *cur_used,
1101                              uint64_t *emer_used,
1102                              uint64_t *low_threshold,
1103                              uint64_t *high_threshold,
1104                              uint64_t *emer_threshold);
1105 extern void drm_init_memctl(size_t low_threshold,
1106                             size_t high_threshold,
1107                             size_t unit_size);
1108
1109                                 /* Misc. IOCTL support (drm_ioctl.h) */
1110 extern int drm_irq_by_busid(struct drm_device *dev, void *data,
1111                             struct drm_file *file_priv);
1112 extern int drm_getunique(struct drm_device *dev, void *data,
1113                          struct drm_file *file_priv);
1114 extern int drm_setunique(struct drm_device *dev, void *data,
1115                          struct drm_file *file_priv);
1116 extern int drm_getmap(struct drm_device *dev, void *data,
1117                       struct drm_file *file_priv);
1118 extern int drm_getclient(struct drm_device *dev, void *data,
1119                          struct drm_file *file_priv);
1120 extern int drm_getstats(struct drm_device *dev, void *data,
1121                         struct drm_file *file_priv);
1122 extern int drm_setversion(struct drm_device *dev, void *data,
1123                           struct drm_file *file_priv);
1124 extern int drm_noop(struct drm_device *dev, void *data,
1125                     struct drm_file *file_priv);
1126
1127                                 /* Context IOCTL support (drm_context.h) */
1128 extern int drm_resctx(struct drm_device *dev, void *data,
1129                       struct drm_file *file_priv);
1130 extern int drm_addctx(struct drm_device *dev, void *data,
1131                       struct drm_file *file_priv);
1132 extern int drm_modctx(struct drm_device *dev, void *data,
1133                       struct drm_file *file_priv);
1134 extern int drm_getctx(struct drm_device *dev, void *data,
1135                       struct drm_file *file_priv);
1136 extern int drm_switchctx(struct drm_device *dev, void *data,
1137                          struct drm_file *file_priv);
1138 extern int drm_newctx(struct drm_device *dev, void *data,
1139                       struct drm_file *file_priv);
1140 extern int drm_rmctx(struct drm_device *dev, void *data,
1141                      struct drm_file *file_priv);
1142
1143 extern int drm_ctxbitmap_init(struct drm_device *dev);
1144 extern void drm_ctxbitmap_cleanup(struct drm_device *dev);
1145 extern void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1146
1147 extern int drm_setsareactx(struct drm_device *dev, void *data,
1148                            struct drm_file *file_priv);
1149 extern int drm_getsareactx(struct drm_device *dev, void *data,
1150                            struct drm_file *file_priv);
1151
1152                                 /* Drawable IOCTL support (drm_drawable.h) */
1153 extern int drm_adddraw(struct drm_device *dev, void *data,
1154                        struct drm_file *file_priv);
1155 extern int drm_rmdraw(struct drm_device *dev, void *data,
1156                       struct drm_file *file_priv);
1157 extern int drm_update_drawable_info(struct drm_device *dev, void *data,
1158                                     struct drm_file *file_priv);
1159 extern struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1160                                                        drm_drawable_t id);
1161 extern void drm_drawable_free_all(struct drm_device *dev);
1162
1163                                 /* Authentication IOCTL support (drm_auth.h) */
1164 extern int drm_getmagic(struct drm_device *dev, void *data,
1165                         struct drm_file *file_priv);
1166 extern int drm_authmagic(struct drm_device *dev, void *data,
1167                          struct drm_file *file_priv);
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_take(struct drm_lock_data *lock_data, unsigned int context);
1175 extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context);
1176 extern void drm_idlelock_take(struct drm_lock_data *lock_data);
1177 extern void drm_idlelock_release(struct drm_lock_data *lock_data);
1178
1179 /*
1180  * These are exported to drivers so that they can implement fencing using
1181  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1182  */
1183
1184 extern int drm_i_have_hw_lock(struct drm_device *dev,
1185                               struct drm_file *file_priv);
1186
1187                                 /* Buffer management support (drm_bufs.h) */
1188 extern int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc * request);
1189 extern int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc * request);
1190 extern int drm_addbufs_fb (struct drm_device *dev, struct drm_buf_desc * request);
1191 extern int drm_addmap(struct drm_device *dev, unsigned int offset,
1192                       unsigned int size, enum drm_map_type type,
1193                       enum drm_map_flags flags, drm_local_map_t ** map_ptr);
1194 extern int drm_addmap_ioctl(struct drm_device *dev, void *data,
1195                             struct drm_file *file_priv);
1196 extern int drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1197 extern int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map);
1198 extern int drm_rmmap_ioctl(struct drm_device *dev, void *data,
1199                            struct drm_file *file_priv);
1200 extern int drm_addbufs(struct drm_device *dev, void *data,
1201                        struct drm_file *file_priv);
1202 extern int drm_infobufs(struct drm_device *dev, void *data,
1203                         struct drm_file *file_priv);
1204 extern int drm_markbufs(struct drm_device *dev, void *data,
1205                         struct drm_file *file_priv);
1206 extern int drm_freebufs(struct drm_device *dev, void *data,
1207                         struct drm_file *file_priv);
1208 extern int drm_mapbufs(struct drm_device *dev, void *data,
1209                        struct drm_file *file_priv);
1210 extern int drm_order(unsigned long size);
1211 extern unsigned long drm_get_resource_start(struct drm_device *dev,
1212                                             unsigned int resource);
1213 extern unsigned long drm_get_resource_len(struct drm_device *dev,
1214                                           unsigned int resource);
1215 extern struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
1216                                                   drm_local_map_t *map);
1217
1218
1219                                 /* DMA support (drm_dma.h) */
1220 extern int drm_dma_setup(struct drm_device *dev);
1221 extern void drm_dma_takedown(struct drm_device *dev);
1222 extern void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf);
1223 extern void drm_core_reclaim_buffers(struct drm_device *dev,
1224                                      struct drm_file *filp);
1225
1226                                 /* IRQ support (drm_irq.h) */
1227 extern int drm_control(struct drm_device *dev, void *data,
1228                        struct drm_file *file_priv);
1229 extern irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1230 extern int drm_irq_install(struct drm_device *dev);
1231 extern int drm_irq_uninstall(struct drm_device *dev);
1232 extern void drm_driver_irq_preinstall(struct drm_device *dev);
1233 extern void drm_driver_irq_postinstall(struct drm_device *dev);
1234 extern void drm_driver_irq_uninstall(struct drm_device *dev);
1235
1236 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1237 extern int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *filp);
1238 extern int drm_vblank_wait(struct drm_device * dev, unsigned int *vbl_seq);
1239 extern void drm_locked_tasklet(struct drm_device *dev, void(*func)(struct drm_device*));
1240 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1241 extern void drm_update_vblank_count(struct drm_device *dev, int crtc);
1242 extern void drm_handle_vblank(struct drm_device *dev, int crtc);
1243 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1244 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1245
1246                                 /* Modesetting support */
1247 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1248                            struct drm_file *file_priv);
1249
1250                                 /* AGP/GART support (drm_agpsupport.h) */
1251 extern struct drm_agp_head *drm_agp_init(struct drm_device *dev);
1252 extern int drm_agp_acquire(struct drm_device *dev);
1253 extern int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1254                                  struct drm_file *file_priv);
1255 extern int drm_agp_release(struct drm_device *dev);
1256 extern int drm_agp_release_ioctl(struct drm_device *dev, void *data,
1257                                  struct drm_file *file_priv);
1258 extern int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1259 extern int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1260                                 struct drm_file *file_priv);
1261 extern int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info);
1262 extern int drm_agp_info_ioctl(struct drm_device *dev, void *data,
1263                         struct drm_file *file_priv);
1264 extern int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1265 extern int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1266                          struct drm_file *file_priv);
1267 extern int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1268 extern int drm_agp_free_ioctl(struct drm_device *dev, void *data,
1269                         struct drm_file *file_priv);
1270 extern int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1271 extern int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1272                           struct drm_file *file_priv);
1273 extern int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1274 extern int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1275                         struct drm_file *file_priv);
1276 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
1277 extern DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type);
1278 #else
1279 extern DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type);
1280 #endif
1281 extern int drm_agp_free_memory(DRM_AGP_MEM * handle);
1282 extern int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start);
1283 extern int drm_agp_unbind_memory(DRM_AGP_MEM * handle);
1284 extern struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev);
1285 extern void drm_agp_chipset_flush(struct drm_device *dev);
1286                                 /* Stub support (drm_stub.h) */
1287 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
1288                      struct drm_driver *driver);
1289 extern int drm_put_dev(struct drm_device *dev);
1290 extern int drm_put_minor(struct drm_minor **minor);
1291 extern unsigned int drm_debug; /* 1 to enable debug output */
1292
1293 extern struct class *drm_class;
1294 extern struct proc_dir_entry *drm_proc_root;
1295
1296 extern struct idr drm_minors_idr;
1297
1298 extern drm_local_map_t *drm_getsarea(struct drm_device *dev);
1299
1300                                 /* Proc support (drm_proc.h) */
1301 int drm_proc_init(struct drm_minor *minor, int minor_id,
1302                   struct proc_dir_entry *root);
1303 int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root);
1304
1305                                 /* Scatter Gather Support (drm_scatter.h) */
1306 extern void drm_sg_cleanup(struct drm_sg_mem * entry);
1307 extern int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1308                         struct drm_file *file_priv);
1309 extern int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1310 extern int drm_sg_free(struct drm_device *dev, void *data,
1311                        struct drm_file *file_priv);
1312
1313                                /* ATI PCIGART support (ati_pcigart.h) */
1314 extern int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1315 extern int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1316
1317 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1318                            size_t align, dma_addr_t maxaddr);
1319 extern void __drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1320 extern void drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1321
1322                                /* sysfs support (drm_sysfs.c) */
1323 struct drm_sysfs_class;
1324 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1325 extern void drm_sysfs_destroy(void);
1326 extern int drm_sysfs_device_add(struct drm_minor *minor);
1327 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1328
1329 /*
1330  * Basic memory manager support (drm_memrange.c)
1331  */
1332
1333 extern struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent,
1334                                                         unsigned long size,
1335                                                         unsigned alignment);
1336 extern void drm_memrange_put_block(struct drm_memrange_node *cur);
1337 extern struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange *mm,
1338                                                           unsigned long size,
1339                                                           unsigned alignment, int best_match);
1340 extern int drm_memrange_init(struct drm_memrange *mm,
1341                              unsigned long start, unsigned long size);
1342 extern void drm_memrange_takedown(struct drm_memrange *mm);
1343 extern int drm_memrange_clean(struct drm_memrange *mm);
1344 extern unsigned long drm_memrange_tail_space(struct drm_memrange *mm);
1345 extern int drm_memrange_remove_space_from_tail(struct drm_memrange *mm,
1346                                                unsigned long size);
1347 extern int drm_memrange_add_space_to_tail(struct drm_memrange *mm,
1348                                           unsigned long size);
1349
1350 static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block)
1351 {
1352         return block->mm;
1353 }
1354
1355 /* Graphics Execution Manager library functions (drm_gem.c) */
1356 int
1357 drm_gem_init (struct drm_device *dev);
1358
1359 void
1360 drm_gem_object_free (struct kref *kref);
1361
1362 struct drm_gem_object *
1363 drm_gem_object_alloc(struct drm_device *dev, size_t size);
1364
1365 void
1366 drm_gem_object_handle_free (struct kref *kref);
1367     
1368 static inline void drm_gem_object_reference(struct drm_gem_object *obj)
1369 {
1370         kref_get(&obj->refcount);
1371 }
1372
1373 static inline void drm_gem_object_unreference(struct drm_gem_object *obj)
1374 {
1375         if (obj == NULL)
1376                 return;
1377
1378         kref_put (&obj->refcount, drm_gem_object_free);
1379 }
1380
1381 int
1382 drm_gem_handle_create(struct drm_file *file_priv,
1383                       struct drm_gem_object *obj,
1384                       int *handlep);
1385
1386 static inline void drm_gem_object_handle_reference (struct drm_gem_object *obj)
1387 {
1388         drm_gem_object_reference (obj);
1389         kref_get(&obj->handlecount);
1390 }
1391
1392 static inline void drm_gem_object_handle_unreference (struct drm_gem_object *obj)
1393 {
1394         if (obj == NULL)
1395                 return;
1396         
1397         /*
1398          * Must bump handle count first as this may be the last
1399          * ref, in which case the object would disappear before we
1400          * checked for a name
1401          */
1402         kref_put (&obj->handlecount, drm_gem_object_handle_free);
1403         drm_gem_object_unreference (obj);
1404 }
1405
1406 struct drm_gem_object *
1407 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
1408                       int handle);
1409 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1410                         struct drm_file *file_priv);
1411 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1412                         struct drm_file *file_priv);
1413 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1414                        struct drm_file *file_priv);
1415
1416 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private);
1417 void drm_gem_release(struct drm_device *dev, struct drm_file *file_private);
1418
1419 extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev);
1420 extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev);
1421
1422 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev,
1423                                                    unsigned int token)
1424 {
1425         struct drm_map_list *_entry;
1426         list_for_each_entry(_entry, &dev->maplist, head)
1427                 if (_entry->user_token == token)
1428                         return _entry->map;
1429         return NULL;
1430 }
1431
1432 static __inline__ int drm_device_is_agp(struct drm_device *dev)
1433 {
1434         if ( dev->driver->device_is_agp != NULL ) {
1435                 int err = (*dev->driver->device_is_agp)(dev);
1436
1437                 if (err != 2) {
1438                         return err;
1439                 }
1440         }
1441
1442         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1443 }
1444
1445 static __inline__ int drm_device_is_pcie(struct drm_device *dev)
1446 {
1447         return pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
1448 }
1449
1450 static __inline__ void drm_core_dropmap(struct drm_map *map)
1451 {
1452 }
1453
1454 #ifndef DEBUG_MEMORY
1455 /** Wrapper around kmalloc() */
1456 static __inline__ void *drm_alloc(size_t size, int area)
1457 {
1458         return kmalloc(size, GFP_KERNEL);
1459 }
1460
1461 /** Wrapper around kfree() */
1462 static __inline__ void drm_free(void *pt, size_t size, int area)
1463 {
1464         kfree(pt);
1465 }
1466 #else
1467 extern void *drm_alloc(size_t size, int area);
1468 extern void drm_free(void *pt, size_t size, int area);
1469 #endif
1470
1471 /*
1472  * Accounting variants of standard calls.
1473  */
1474
1475 static inline void *drm_ctl_alloc(size_t size, int area)
1476 {
1477         void *ret;
1478         if (drm_alloc_memctl(size))
1479                 return NULL;
1480         ret = drm_alloc(size, area);
1481         if (!ret)
1482                 drm_free_memctl(size);
1483         return ret;
1484 }
1485
1486 static inline void *drm_ctl_calloc(size_t nmemb, size_t size, int area)
1487 {
1488         void *ret;
1489
1490         if (drm_alloc_memctl(nmemb*size))
1491                 return NULL;
1492         ret = drm_calloc(nmemb, size, area);
1493         if (!ret)
1494                 drm_free_memctl(nmemb*size);
1495         return ret;
1496 }
1497
1498 static inline void drm_ctl_free(void *pt, size_t size, int area)
1499 {
1500         drm_free(pt, size, area);
1501         drm_free_memctl(size);
1502 }
1503
1504 /*@}*/
1505
1506 #endif                          /* __KERNEL__ */
1507 #endif