da14bdfd656bcb2282da14c3af05d15209a38160
[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/config.h>
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/miscdevice.h>
48 #include <linux/fs.h>
49 #include <linux/proc_fs.h>
50 #include <linux/init.h>
51 #include <linux/file.h>
52 #include <linux/pci.h>
53 #include <linux/version.h>
54 #include <linux/sched.h>
55 #include <linux/smp_lock.h>     /* For (un)lock_kernel */
56 #include <linux/mm.h>
57 #include <linux/pagemap.h>
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
59 #include <linux/mutex.h>
60 #endif
61 #if defined(__alpha__) || defined(__powerpc__)
62 #include <asm/pgtable.h>        /* For pte_wrprotect */
63 #endif
64 #include <asm/io.h>
65 #include <asm/mman.h>
66 #include <asm/uaccess.h>
67 #ifdef CONFIG_MTRR
68 #include <asm/mtrr.h>
69 #endif
70 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
71 #include <linux/types.h>
72 #include <linux/agp_backend.h>
73 #endif
74 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,41)
75 #define HAS_WORKQUEUE 0
76 #else
77 #define HAS_WORKQUEUE 1
78 #endif
79 #if !HAS_WORKQUEUE
80 #include <linux/tqueue.h>
81 #else
82 #include <linux/workqueue.h>
83 #endif
84 #include <linux/poll.h>
85 #include <asm/pgalloc.h>
86 #include "drm.h"
87 #include <linux/slab.h>
88
89 #define __OS_HAS_AGP (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
90 #define __OS_HAS_MTRR (defined(CONFIG_MTRR))
91
92 #include "drm_os_linux.h"
93 #include "drm_hashtab.h"
94
95 /* If you want the memory alloc debug functionality, change define below */
96 /* #define DEBUG_MEMORY */
97
98 /***********************************************************************/
99 /** \name DRM template customization defaults */
100 /*@{*/
101
102 /* driver capabilities and requirements mask */
103 #define DRIVER_USE_AGP     0x1
104 #define DRIVER_REQUIRE_AGP 0x2
105 #define DRIVER_USE_MTRR    0x4
106 #define DRIVER_PCI_DMA     0x8
107 #define DRIVER_SG          0x10
108 #define DRIVER_HAVE_DMA    0x20
109 #define DRIVER_HAVE_IRQ    0x40
110 #define DRIVER_IRQ_SHARED  0x80
111 #define DRIVER_IRQ_VBL     0x100
112 #define DRIVER_DMA_QUEUE   0x200
113 #define DRIVER_FB_DMA      0x400
114
115
116 /*@}*/
117
118 /***********************************************************************/
119 /** \name Begin the DRM... */
120 /*@{*/
121
122 #define DRM_DEBUG_CODE 2          /**< Include debugging code if > 1, then
123                                      also include looping detection. */
124
125 #define DRM_MAGIC_HASH_ORDER  4 /**< Size of key hash table. Must be power of 2. */
126 #define DRM_KERNEL_CONTEXT    0  /**< Change drm_resctx if changed */
127 #define DRM_RESERVED_CONTEXTS 1  /**< Change drm_resctx if changed */
128 #define DRM_LOOPING_LIMIT     5000000
129 #define DRM_TIME_SLICE        (HZ/20)  /**< Time slice for GLXContexts */
130 #define DRM_LOCK_SLICE        1 /**< Time slice for lock, in jiffies */
131
132 #define DRM_FLAG_DEBUG    0x01
133
134 #define DRM_MEM_DMA        0
135 #define DRM_MEM_SAREA      1
136 #define DRM_MEM_DRIVER     2
137 #define DRM_MEM_MAGIC      3
138 #define DRM_MEM_IOCTLS     4
139 #define DRM_MEM_MAPS       5
140 #define DRM_MEM_VMAS       6
141 #define DRM_MEM_BUFS       7
142 #define DRM_MEM_SEGS       8
143 #define DRM_MEM_PAGES      9
144 #define DRM_MEM_FILES     10
145 #define DRM_MEM_QUEUES    11
146 #define DRM_MEM_CMDS      12
147 #define DRM_MEM_MAPPINGS  13
148 #define DRM_MEM_BUFLISTS  14
149 #define DRM_MEM_AGPLISTS  15
150 #define DRM_MEM_TOTALAGP  16
151 #define DRM_MEM_BOUNDAGP  17
152 #define DRM_MEM_CTXBITMAP 18
153 #define DRM_MEM_STUB      19
154 #define DRM_MEM_SGLISTS   20
155 #define DRM_MEM_CTXLIST   21
156 #define DRM_MEM_MM        22
157 #define DRM_MEM_HASHTAB   23
158 #define DRM_MEM_OBJECTS   24
159 #define DRM_MEM_FENCE     25
160 #define DRM_MEM_TTM       26
161 #define DRM_MEM_BUFOBJ    27
162
163 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
164 #define DRM_MAP_HASH_OFFSET 0x10000000
165
166 /*@}*/
167
168 #include "drm_compat.h"
169
170 /***********************************************************************/
171 /** \name Macros to make printk easier */
172 /*@{*/
173
174 /**
175  * Error output.
176  *
177  * \param fmt printf() like format string.
178  * \param arg arguments
179  */
180 #define DRM_ERROR(fmt, arg...) \
181         printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* " fmt , __FUNCTION__ , ##arg)
182
183 /**
184  * Memory error output.
185  *
186  * \param area memory area where the error occurred.
187  * \param fmt printf() like format string.
188  * \param arg arguments
189  */
190 #define DRM_MEM_ERROR(area, fmt, arg...) \
191         printk(KERN_ERR "[" DRM_NAME ":%s:%s] *ERROR* " fmt , __FUNCTION__, \
192                drm_mem_stats[area].name , ##arg)
193 #define DRM_INFO(fmt, arg...)  printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
194
195 /**
196  * Debug output.
197  *
198  * \param fmt printf() like format string.
199  * \param arg arguments
200  */
201 #if DRM_DEBUG_CODE
202 #define DRM_DEBUG(fmt, arg...)                                          \
203         do {                                                            \
204                 if ( drm_debug )                                                \
205                         printk(KERN_DEBUG                               \
206                                "[" DRM_NAME ":%s] " fmt ,               \
207                                __FUNCTION__ , ##arg);                   \
208         } while (0)
209 #else
210 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
211 #endif
212
213 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
214
215 #define DRM_PROC_PRINT(fmt, arg...)                                     \
216    len += sprintf(&buf[len], fmt , ##arg);                              \
217    if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
218
219 #define DRM_PROC_PRINT_RET(ret, fmt, arg...)                            \
220    len += sprintf(&buf[len], fmt , ##arg);                              \
221    if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
222
223 /*@}*/
224
225 /***********************************************************************/
226 /** \name Internal types and structures */
227 /*@{*/
228
229 #define DRM_ARRAY_SIZE(x) ARRAY_SIZE(x)
230 #define DRM_MIN(a,b) min(a,b)
231 #define DRM_MAX(a,b) max(a,b)
232
233 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
234 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
235 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
236
237 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
238 /**
239  * Get the private SAREA mapping.
240  *
241  * \param _dev DRM device.
242  * \param _ctx context number.
243  * \param _map output mapping.
244  */
245 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
246         (_map) = (_dev)->context_sareas[_ctx];          \
247 } while(0)
248
249 /**
250  * Test that the hardware lock is held by the caller, returning otherwise.
251  *
252  * \param dev DRM device.
253  * \param filp file pointer of the caller.
254  */
255 #define LOCK_TEST_WITH_RETURN( dev, filp )                              \
256 do {                                                                    \
257         if ( !_DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ) ||           \
258              dev->lock.filp != filp )   {                               \
259                 DRM_ERROR( "%s called without lock held, held  %d owner %p %p\n",\
260                            __FUNCTION__, _DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ),\
261                            dev->lock.filp, filp );                      \
262                 return -EINVAL;                                         \
263         }                                                               \
264 } while (0)
265
266 /**
267  * Copy and IOCTL return string to user space
268  */
269 #define DRM_COPY( name, value )                                         \
270         len = strlen( value );                                          \
271         if ( len > name##_len ) len = name##_len;                       \
272         name##_len = strlen( value );                                   \
273         if ( len && name ) {                                            \
274                 if ( copy_to_user( name, value, len ) )                 \
275                         return -EFAULT;                                 \
276         }
277
278 /**
279  * Ioctl function type.
280  *
281  * \param inode device inode.
282  * \param filp file pointer.
283  * \param cmd command.
284  * \param arg argument.
285  */
286 typedef int drm_ioctl_t(struct inode *inode, struct file *filp,
287                         unsigned int cmd, unsigned long arg);
288
289 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
290                                unsigned long arg);
291
292 #define DRM_AUTH        0x1
293 #define DRM_MASTER      0x2
294 #define DRM_ROOT_ONLY   0x4
295
296 typedef struct drm_ioctl_desc {
297         drm_ioctl_t *func;
298         int flags;
299 } drm_ioctl_desc_t;
300
301 typedef struct drm_devstate {
302         pid_t owner;                    /**< X server pid holding x_lock */
303 } drm_devstate_t;
304
305 typedef struct drm_magic_entry {
306         drm_hash_item_t hash_item;
307         struct list_head head;
308         struct drm_file *priv;
309 } drm_magic_entry_t;
310
311 typedef struct drm_magic_head {
312         struct drm_magic_entry *head;
313         struct drm_magic_entry *tail;
314 } drm_magic_head_t;
315
316 typedef struct drm_vma_entry {
317         struct vm_area_struct *vma;
318         struct drm_vma_entry *next;
319         pid_t pid;
320 } drm_vma_entry_t;
321
322 /**
323  * DMA buffer.
324  */
325 typedef struct drm_buf {
326         int idx;                       /**< Index into master buflist */
327         int total;                     /**< Buffer size */
328         int order;                     /**< log-base-2(total) */
329         int used;                      /**< Amount of buffer in use (for DMA) */
330         unsigned long offset;          /**< Byte offset (used internally) */
331         void *address;                 /**< Address of buffer */
332         unsigned long bus_address;     /**< Bus address of buffer */
333         struct drm_buf *next;          /**< Kernel-only: used for free list */
334         __volatile__ int waiting;      /**< On kernel DMA queue */
335         __volatile__ int pending;      /**< On hardware DMA queue */
336         wait_queue_head_t dma_wait;    /**< Processes waiting */
337         struct file *filp;             /**< Pointer to holding file descr */
338         int context;                   /**< Kernel queue for this buffer */
339         int while_locked;              /**< Dispatch this buffer while locked */
340         enum {
341                 DRM_LIST_NONE = 0,
342                 DRM_LIST_FREE = 1,
343                 DRM_LIST_WAIT = 2,
344                 DRM_LIST_PEND = 3,
345                 DRM_LIST_PRIO = 4,
346                 DRM_LIST_RECLAIM = 5
347         } list;                        /**< Which list we're on */
348
349         int dev_priv_size;              /**< Size of buffer private storage */
350         void *dev_private;              /**< Per-buffer private storage */
351 } drm_buf_t;
352
353 /** bufs is one longer than it has to be */
354 typedef struct drm_waitlist {
355         int count;                      /**< Number of possible buffers */
356         drm_buf_t **bufs;               /**< List of pointers to buffers */
357         drm_buf_t **rp;                 /**< Read pointer */
358         drm_buf_t **wp;                 /**< Write pointer */
359         drm_buf_t **end;                /**< End pointer */
360         spinlock_t read_lock;
361         spinlock_t write_lock;
362 } drm_waitlist_t;
363
364 typedef struct drm_freelist {
365         int initialized;               /**< Freelist in use */
366         atomic_t count;                /**< Number of free buffers */
367         drm_buf_t *next;               /**< End pointer */
368
369         wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
370         int low_mark;                  /**< Low water mark */
371         int high_mark;                 /**< High water mark */
372         atomic_t wfh;                  /**< If waiting for high mark */
373         spinlock_t lock;
374 } drm_freelist_t;
375
376 typedef struct drm_dma_handle {
377         dma_addr_t busaddr;
378         void *vaddr;
379         size_t size;
380 } drm_dma_handle_t;
381
382 /**
383  * Buffer entry.  There is one of this for each buffer size order.
384  */
385 typedef struct drm_buf_entry {
386         int buf_size;                   /**< size */
387         int buf_count;                  /**< number of buffers */
388         drm_buf_t *buflist;             /**< buffer list */
389         int seg_count;
390         int page_order;
391         drm_dma_handle_t **seglist;
392         drm_freelist_t freelist;
393 } drm_buf_entry_t;
394
395 /*
396  * This should be small enough to allow the use of kmalloc for hash tables
397  * instead of vmalloc.
398  */
399
400 #define DRM_FILE_HASH_ORDER 8
401 typedef enum{
402         _DRM_REF_USE=0,
403         _DRM_REF_TYPE1,
404         _DRM_NO_REF_TYPES
405 } drm_ref_t;
406
407
408 /** File private data */
409 typedef struct drm_file {
410         int authenticated;
411         int master;
412         int minor;
413         pid_t pid;
414         uid_t uid;
415         drm_magic_t magic;
416         unsigned long ioctl_count;
417         struct drm_file *next;
418         struct drm_file *prev;
419         struct drm_head *head;
420         int remove_auth_on_close;
421         unsigned long lock_count;
422         
423         /*
424          * The user object hash table is global and resides in the
425          * drm_device structure. We protect the lists and hash tables with the
426          * device struct_mutex. A bit coarse-grained but probably the best 
427          * option.
428          */
429
430         struct list_head refd_objects;
431         struct list_head user_objects;
432
433         drm_open_hash_t refd_object_hash[_DRM_NO_REF_TYPES];
434         void *driver_priv;
435 } drm_file_t;
436
437 /** Wait queue */
438 typedef struct drm_queue {
439         atomic_t use_count;             /**< Outstanding uses (+1) */
440         atomic_t finalization;          /**< Finalization in progress */
441         atomic_t block_count;           /**< Count of processes waiting */
442         atomic_t block_read;            /**< Queue blocked for reads */
443         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
444         atomic_t block_write;           /**< Queue blocked for writes */
445         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
446 #if 1
447         atomic_t total_queued;          /**< Total queued statistic */
448         atomic_t total_flushed;         /**< Total flushes statistic */
449         atomic_t total_locks;           /**< Total locks statistics */
450 #endif
451         drm_ctx_flags_t flags;          /**< Context preserving and 2D-only */
452         drm_waitlist_t waitlist;        /**< Pending buffers */
453         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
454 } drm_queue_t;
455
456 /**
457  * Lock data.
458  */
459 typedef struct drm_lock_data {
460         drm_hw_lock_t *hw_lock;         /**< Hardware lock */
461         struct file *filp;              /**< File descr of lock holder (0=kernel) */
462         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
463         unsigned long lock_time;        /**< Time of last lock in jiffies */
464 } drm_lock_data_t;
465
466 /**
467  * DMA data.
468  */
469 typedef struct drm_device_dma {
470
471         drm_buf_entry_t bufs[DRM_MAX_ORDER + 1]; /**< buffers, grouped by their size order */
472         int buf_count;                  /**< total number of buffers */
473         drm_buf_t **buflist;            /**< Vector of pointers into drm_device_dma::bufs */
474         int seg_count;
475         int page_count;                 /**< number of pages */
476         unsigned long *pagelist;        /**< page list */
477         unsigned long byte_count;
478         enum {
479                 _DRM_DMA_USE_AGP = 0x01,
480                 _DRM_DMA_USE_SG = 0x02,
481                 _DRM_DMA_USE_FB = 0x04
482         } flags;
483
484 } drm_device_dma_t;
485
486 /**
487  * AGP memory entry.  Stored as a doubly linked list.
488  */
489 typedef struct drm_agp_mem {
490         unsigned long handle;           /**< handle */
491         DRM_AGP_MEM *memory;
492         unsigned long bound;            /**< address */
493         int pages;
494         struct drm_agp_mem *prev;       /**< previous entry */
495         struct drm_agp_mem *next;       /**< next entry */
496 } drm_agp_mem_t;
497
498 /**
499  * AGP data.
500  *
501  * \sa drm_agp_init)() and drm_device::agp.
502  */
503 typedef struct drm_agp_head {
504         DRM_AGP_KERN agp_info;          /**< AGP device information */
505         drm_agp_mem_t *memory;          /**< memory entries */
506         unsigned long mode;             /**< AGP mode */
507 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11)
508         struct agp_bridge_data  *bridge;
509 #endif
510         int enabled;                    /**< whether the AGP bus as been enabled */
511         int acquired;                   /**< whether the AGP device has been acquired */
512         unsigned long base;
513         int agp_mtrr;
514         int cant_use_aperture;
515         unsigned long page_mask;
516 } drm_agp_head_t;
517
518 /**
519  * Scatter-gather memory.
520  */
521 typedef struct drm_sg_mem {
522         unsigned long handle;
523         void *virtual;
524         int pages;
525         struct page **pagelist;
526         dma_addr_t *busaddr;
527 } drm_sg_mem_t;
528
529 typedef struct drm_sigdata {
530         int context;
531         drm_hw_lock_t *lock;
532 } drm_sigdata_t;
533
534 /**
535  * Mappings list
536  */
537 typedef struct drm_map_list {
538         struct list_head head;          /**< list head */
539         drm_hash_item_t hash;
540         drm_map_t *map;                 /**< mapping */
541         unsigned int user_token;
542 } drm_map_list_t;
543
544 typedef drm_map_t drm_local_map_t;
545
546 /**
547  * Context handle list
548  */
549 typedef struct drm_ctx_list {
550         struct list_head head;          /**< list head */
551         drm_context_t handle;           /**< context handle */
552         drm_file_t *tag;                /**< associated fd private data */
553 } drm_ctx_list_t;
554
555 typedef struct drm_vbl_sig {
556         struct list_head head;
557         unsigned int sequence;
558         struct siginfo info;
559         struct task_struct *task;
560 } drm_vbl_sig_t;
561
562 /* location of GART table */
563 #define DRM_ATI_GART_MAIN 1
564 #define DRM_ATI_GART_FB   2
565
566 typedef struct ati_pcigart_info {
567         int gart_table_location;
568         int is_pcie;
569         void *addr;
570         dma_addr_t bus_addr;
571         drm_local_map_t mapping;
572 } drm_ati_pcigart_info;
573
574 /* 
575  * Generic memory manager structs
576  */
577
578 typedef struct drm_mm_node {
579         struct list_head fl_entry;
580         struct list_head ml_entry;
581         int free;
582         unsigned long start;
583         unsigned long size;
584         void *private;
585 } drm_mm_node_t;
586
587 typedef struct drm_mm {
588         drm_mm_node_t root_node;
589 } drm_mm_t;
590
591
592 /*
593  * User space objects and their references.
594  */
595
596 #define drm_user_object_entry(_ptr, _type, _member) container_of(_ptr, _type, _member)
597
598 typedef enum {
599                 drm_fence_type,
600                 drm_buffer_type,
601                 drm_ttm_type
602
603                 /*
604                  * Add other user space object types here. 
605                  */
606
607 } drm_object_type_t;
608
609
610
611
612 /*
613  * A user object is a structure that helps the drm give out user handles
614  * to kernel internal objects and to keep track of these objects so that 
615  * they can be destroyed, for example when the user space process exits.
616  * Designed to be accessible using a user space 32-bit handle. 
617  */
618
619 typedef struct drm_user_object{
620         drm_hash_item_t hash;
621         struct list_head list;
622         drm_object_type_t type;
623         atomic_t refcount;
624         int shareable;
625         drm_file_t *owner;
626         void (*ref_struct_locked) (drm_file_t *priv, struct drm_user_object *obj, 
627                                    drm_ref_t ref_action); 
628         void (*unref)(drm_file_t *priv, struct drm_user_object *obj, 
629                       drm_ref_t unref_action);
630         void (*remove)(drm_file_t *priv, struct drm_user_object *obj);
631 } drm_user_object_t;
632
633 /*
634  * A ref object is a structure which is used to
635  * keep track of references to user objects and to keep track of these
636  * references so that they can be destroyed for example when the user space
637  * process exits. Designed to be accessible using a pointer to the _user_ object.
638  */
639
640
641 typedef struct drm_ref_object {
642         drm_hash_item_t hash;
643         struct list_head list;
644         atomic_t refcount;
645         drm_ref_t unref_action;
646 } drm_ref_object_t;
647
648
649 #include "drm_ttm.h"
650
651 /*
652  * buffer object driver
653  */
654
655 typedef struct drm_bo_driver{
656         int cached_tt;
657         int cached_vram;
658         drm_ttm_backend_t *(*create_ttm_backend_entry) 
659                 (struct drm_device *dev, int cached);
660         int (*fence_type)(uint32_t flags, uint32_t *class, uint32_t *type);
661         int (*invalidate_caches)(struct drm_device *dev, uint32_t flags);
662 } drm_bo_driver_t;
663
664
665 /**
666  * DRM driver structure. This structure represent the common code for
667  * a family of cards. There will one drm_device for each card present
668  * in this family
669  */
670
671 struct drm_device;
672 struct drm_driver {
673         int (*load) (struct drm_device *, unsigned long flags);
674         int (*firstopen) (struct drm_device *);
675         int (*open) (struct drm_device *, drm_file_t *);
676         void (*preclose) (struct drm_device *, struct file * filp);
677         void (*postclose) (struct drm_device *, drm_file_t *);
678         void (*lastclose) (struct drm_device *);
679         int (*unload) (struct drm_device *);
680         int (*dma_ioctl) (DRM_IOCTL_ARGS);
681         void (*dma_ready) (struct drm_device *);
682         int (*dma_quiescent) (struct drm_device *);
683         int (*context_ctor) (struct drm_device * dev, int context);
684         int (*context_dtor) (struct drm_device * dev, int context);
685         int (*kernel_context_switch) (struct drm_device * dev, int old,
686                                       int new);
687         void (*kernel_context_switch_unlock) (struct drm_device * dev);
688         int (*vblank_wait) (struct drm_device * dev, unsigned int *sequence);
689         int (*dri_library_name) (struct drm_device * dev, char * buf);
690
691         /**
692          * Called by \c drm_device_is_agp.  Typically used to determine if a
693          * card is really attached to AGP or not.
694          *
695          * \param dev  DRM device handle
696          *
697          * \returns
698          * One of three values is returned depending on whether or not the
699          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
700          * (return of 1), or may or may not be AGP (return of 2).
701          */
702         int (*device_is_agp) (struct drm_device * dev);
703
704 /* these have to be filled in */
705          irqreturn_t(*irq_handler) (DRM_IRQ_ARGS);
706         void (*irq_preinstall) (struct drm_device * dev);
707         void (*irq_postinstall) (struct drm_device * dev);
708         void (*irq_uninstall) (struct drm_device * dev);
709         void (*reclaim_buffers) (struct drm_device *dev, struct file * filp);
710         void (*reclaim_buffers_locked) (struct drm_device *dev,
711                                         struct file * filp);
712         unsigned long (*get_map_ofs) (drm_map_t * map);
713         unsigned long (*get_reg_ofs) (struct drm_device * dev);
714         void (*set_version) (struct drm_device * dev, drm_set_version_t * sv);
715
716         struct drm_fence_driver *fence_driver;
717         struct drm_bo_driver *bo_driver;
718         
719         int major;
720         int minor;
721         int patchlevel;
722         char *name;
723         char *desc;
724         char *date;
725
726 /* variables */
727         u32 driver_features;
728         int dev_priv_size;
729         drm_ioctl_desc_t *ioctls;
730         int num_ioctls;
731         struct file_operations fops;
732         struct pci_driver pci_driver;
733 };
734
735 /**
736  * DRM head structure. This structure represent a video head on a card
737  * that may contain multiple heads. Embed one per head of these in the
738  * private drm_device structure.
739  */
740 typedef struct drm_head {
741         int minor;                      /**< Minor device number */
742         struct drm_device *dev;
743         struct proc_dir_entry *dev_root;  /**< proc directory entry */
744         dev_t device;                   /**< Device number for mknod */
745         struct class_device *dev_class;
746 } drm_head_t;
747
748 typedef struct drm_cache {
749
750         /*
751          * Memory caches
752          */
753
754         kmem_cache_t *mm;
755         kmem_cache_t *fence_object;
756         kmem_cache_t *ref_object;
757 } drm_cache_t;
758
759
760
761 typedef struct drm_fence_driver{
762         int no_types;
763         uint32_t wrap_diff;
764         uint32_t flush_diff;
765         uint32_t sequence_mask;
766         int lazy_capable;
767         int (*emit) (struct drm_device *dev, uint32_t *breadcrumb);
768         void (*poke_flush) (struct drm_device *dev);
769 } drm_fence_driver_t;
770
771 #define _DRM_FENCE_TYPE_EXE 0x00
772
773 typedef struct drm_fence_manager{
774         int initialized;
775         rwlock_t lock;
776
777         /*
778          * The list below should be maintained in sequence order and 
779          * access is protected by the above spinlock.
780          */
781
782         struct list_head ring;
783         struct list_head *fence_types[32];
784         volatile uint32_t pending_flush;
785         wait_queue_head_t fence_queue;
786         int pending_exe_flush;
787         uint32_t last_exe_flush;
788         uint32_t exe_flush_sequence;
789 } drm_fence_manager_t;
790
791 typedef struct drm_buffer_manager{
792         int initialized;
793         int has_vram;
794         int has_tt;
795         int use_vram;
796         int use_tt;
797         drm_mm_t tt_manager;
798         struct list_head tt_lru;
799         drm_mm_t vram_manager;
800         struct list_head vram_lru;
801         struct list_head unfenced;
802         struct list_head ddestroy;
803         struct list_head other;
804         struct work_struct wq;
805         uint32_t fence_flags;
806 } drm_buffer_manager_t;
807
808
809
810 /**
811  * DRM device structure. This structure represent a complete card that
812  * may contain multiple heads.
813  */
814 typedef struct drm_device {
815         char *unique;                   /**< Unique identifier: e.g., busid */
816         int unique_len;                 /**< Length of unique field */
817         char *devname;                  /**< For /proc/interrupts */
818         int if_version;                 /**< Highest interface version set */
819
820         int blocked;                    /**< Blocked due to VC switch? */
821
822         /** \name Locks */
823         /*@{ */
824         spinlock_t count_lock;          /**< For inuse, drm_device::open_count, drm_device::buf_use */
825         struct mutex struct_mutex;      /**< For others */
826         /*@} */
827
828         /** \name Usage Counters */
829         /*@{ */
830         int open_count;                 /**< Outstanding files open */
831         atomic_t ioctl_count;           /**< Outstanding IOCTLs pending */
832         atomic_t vma_count;             /**< Outstanding vma areas open */
833         int buf_use;                    /**< Buffers in use -- cannot alloc */
834         atomic_t buf_alloc;             /**< Buffer allocation in progress */
835         /*@} */
836
837         /** \name Performance counters */
838         /*@{ */
839         unsigned long counters;
840         drm_stat_type_t types[15];
841         atomic_t counts[15];
842         /*@} */
843
844         /** \name Authentication */
845         /*@{ */
846         drm_file_t *file_first;         /**< file list head */
847         drm_file_t *file_last;          /**< file list tail */
848         drm_open_hash_t magiclist;
849         struct list_head magicfree;
850         /*@} */
851
852         /** \name Memory management */
853         /*@{ */
854         drm_map_list_t *maplist;        /**< Linked list of regions */
855         int map_count;                  /**< Number of mappable regions */
856         drm_open_hash_t map_hash;       /**< User token hash table for maps */
857         drm_open_hash_t object_hash;    /**< User token hash table for objects */
858
859         /** \name Context handle management */
860         /*@{ */
861         drm_ctx_list_t *ctxlist;        /**< Linked list of context handles */
862         int ctx_count;                  /**< Number of context handles */
863         struct mutex ctxlist_mutex;     /**< For ctxlist */
864
865         drm_map_t **context_sareas;     /**< per-context SAREA's */
866         int max_context;
867
868         drm_vma_entry_t *vmalist;       /**< List of vmas (for debugging) */
869         drm_lock_data_t lock;           /**< Information on hardware lock */
870         /*@} */
871
872         /** \name DMA queues (contexts) */
873         /*@{ */
874         int queue_count;                /**< Number of active DMA queues */
875         int queue_reserved;             /**< Number of reserved DMA queues */
876         int queue_slots;                /**< Actual length of queuelist */
877         drm_queue_t **queuelist;        /**< Vector of pointers to DMA queues */
878         drm_device_dma_t *dma;          /**< Optional pointer for DMA support */
879         /*@} */
880
881         /** \name Context support */
882         /*@{ */
883         int irq;                        /**< Interrupt used by board */
884         int irq_enabled;                /**< True if irq handler is enabled */
885         __volatile__ long context_flag; /**< Context swapping flag */
886         __volatile__ long interrupt_flag; /**< Interruption handler flag */
887         __volatile__ long dma_flag;     /**< DMA dispatch flag */
888         struct timer_list timer;        /**< Timer for delaying ctx switch */
889         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
890         int last_checked;               /**< Last context checked for DMA */
891         int last_context;               /**< Last current context */
892         unsigned long last_switch;      /**< jiffies at last context switch */
893         /*@} */
894
895 #if !HAS_WORKQUEUE
896         struct tq_struct tq;
897 #else
898         struct work_struct work;
899 #endif
900         /** \name VBLANK IRQ support */
901         /*@{ */
902
903         wait_queue_head_t vbl_queue;    /**< VBLANK wait queue */
904         atomic_t vbl_received;
905         spinlock_t vbl_lock;
906         drm_vbl_sig_t vbl_sigs;         /**< signal list to send on VBLANK */
907         unsigned int vbl_pending;
908
909         /*@} */
910         cycles_t ctx_start;
911         cycles_t lck_start;
912
913         struct fasync_struct *buf_async;/**< Processes waiting for SIGIO */
914         wait_queue_head_t buf_readers;  /**< Processes waiting to read */
915         wait_queue_head_t buf_writers;  /**< Processes waiting to ctx switch */
916
917         drm_agp_head_t *agp;            /**< AGP data */
918
919         struct pci_dev *pdev;           /**< PCI device structure */
920 #ifdef __alpha__
921 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,3)
922         struct pci_controler *hose;
923 #else
924         struct pci_controller *hose;
925 #endif
926 #endif
927         drm_sg_mem_t *sg;               /**< Scatter gather memory */
928         unsigned long *ctx_bitmap;      /**< context bitmap */
929         void *dev_private;              /**< device private data */
930         drm_sigdata_t sigdata;          /**< For block_all_signals */
931         sigset_t sigmask;
932
933         struct drm_driver *driver;
934         drm_local_map_t *agp_buffer_map;
935         unsigned int agp_buffer_token;
936         drm_head_t primary;             /**< primary screen head */
937
938         drm_fence_manager_t fm;
939         drm_buffer_manager_t bm;
940   
941 } drm_device_t;
942
943 #if __OS_HAS_AGP
944 typedef struct drm_agp_ttm_priv {
945         DRM_AGP_MEM *mem;
946         struct agp_bridge_data *bridge;
947         unsigned mem_type;
948         int populated;
949 } drm_agp_ttm_priv;
950 #endif
951
952 typedef struct drm_fence_object{
953         drm_user_object_t base;
954         atomic_t usage;
955
956         /*
957          * The below three fields are protected by the fence manager spinlock.
958          */
959
960         struct list_head ring;
961         int class;
962         volatile uint32_t type;
963         volatile uint32_t signaled;
964         uint32_t sequence;
965         volatile uint32_t flush_mask;
966         volatile uint32_t submitted_flush;
967 } drm_fence_object_t;
968
969
970 typedef struct drm_buffer_object{
971         drm_device_t *dev;
972         drm_user_object_t base;
973
974     /*
975      * If there is a possibility that the usage variable is zero,
976      * then dev->struct_mutext should be locked before incrementing it.
977      */
978
979         atomic_t usage;
980         drm_ttm_object_t *ttm_object;
981         drm_ttm_backend_list_t *ttm_region;
982         unsigned long num_pages;
983         unsigned long buffer_start;
984         drm_bo_type_t type;
985         unsigned long offset;
986
987         atomic_t mapped;
988         uint32_t flags;
989         uint32_t mask;
990
991         drm_mm_node_t *vram;
992         drm_mm_node_t *tt;
993         struct list_head head;
994         struct list_head ddestroy;
995
996         uint32_t fence_flags;
997         uint32_t fence_class;
998         drm_fence_object_t *fence;
999         uint32_t priv_flags;
1000         wait_queue_head_t event_queue;
1001         struct mutex mutex;
1002 } drm_buffer_object_t;
1003
1004 #define _DRM_BO_FLAG_UNFENCED 0x00000001
1005 #define _DRM_BO_FLAG_EVICTED  0x00000002
1006
1007
1008 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1009                                              int feature)
1010 {
1011         return ((dev->driver->driver_features & feature) ? 1 : 0);
1012 }
1013
1014 #ifdef __alpha__
1015 #define drm_get_pci_domain(dev) dev->hose->bus->number
1016 #else
1017 #define drm_get_pci_domain(dev) pci_domain_nr(dev->pdev->bus)
1018 #endif
1019
1020 #if __OS_HAS_AGP
1021 static inline int drm_core_has_AGP(struct drm_device *dev)
1022 {
1023         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1024 }
1025 #else
1026 #define drm_core_has_AGP(dev) (0)
1027 #endif
1028
1029 #if __OS_HAS_MTRR
1030 static inline int drm_core_has_MTRR(struct drm_device *dev)
1031 {
1032         return drm_core_check_feature(dev, DRIVER_USE_MTRR);
1033 }
1034
1035 #define DRM_MTRR_WC             MTRR_TYPE_WRCOMB
1036
1037 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
1038                                unsigned int flags)
1039 {
1040         return mtrr_add(offset, size, flags, 1);
1041 }
1042
1043 static inline int drm_mtrr_del(int handle, unsigned long offset,
1044                                unsigned long size, unsigned int flags)
1045 {
1046         return mtrr_del(handle, offset, size);
1047 }
1048
1049 #else
1050 #define drm_core_has_MTRR(dev) (0)
1051 #endif
1052
1053
1054 /******************************************************************/
1055 /** \name Internal function definitions */
1056 /*@{*/
1057
1058                                 /* Driver support (drm_drv.h) */
1059 extern int drm_fb_loaded;
1060 extern int drm_init(struct drm_driver *driver,
1061                               struct pci_device_id *pciidlist);
1062 extern void drm_exit(struct drm_driver *driver);
1063 extern void drm_cleanup_pci(struct pci_dev *pdev);
1064 extern int drm_ioctl(struct inode *inode, struct file *filp,
1065                      unsigned int cmd, unsigned long arg);
1066 extern long drm_compat_ioctl(struct file *filp,
1067                              unsigned int cmd, unsigned long arg);
1068
1069 extern int drm_lastclose(drm_device_t * dev);
1070
1071                                 /* Device support (drm_fops.h) */
1072 extern int drm_open(struct inode *inode, struct file *filp);
1073 extern int drm_stub_open(struct inode *inode, struct file *filp);
1074 extern int drm_fasync(int fd, struct file *filp, int on);
1075 extern int drm_release(struct inode *inode, struct file *filp);
1076 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
1077
1078                                 /* Mapping support (drm_vm.h) */
1079 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
1080 extern unsigned long drm_core_get_map_ofs(drm_map_t * map);
1081 extern unsigned long drm_core_get_reg_ofs(struct drm_device *dev);
1082 extern pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma);
1083
1084                                 /* Memory management support (drm_memory.h) */
1085 #include "drm_memory.h"
1086 extern void drm_mem_init(void);
1087 extern int drm_mem_info(char *buf, char **start, off_t offset,
1088                         int request, int *eof, void *data);
1089 extern void *drm_calloc(size_t nmemb, size_t size, int area);
1090 extern void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area);
1091 extern unsigned long drm_alloc_pages(int order, int area);
1092 extern void drm_free_pages(unsigned long address, int order, int area);
1093 extern DRM_AGP_MEM *drm_alloc_agp(drm_device_t *dev, int pages, u32 type);
1094 extern int drm_free_agp(DRM_AGP_MEM * handle, int pages);
1095 extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start);
1096 extern int drm_unbind_agp(DRM_AGP_MEM * handle);
1097
1098                                 /* Misc. IOCTL support (drm_ioctl.h) */
1099 extern int drm_irq_by_busid(struct inode *inode, struct file *filp,
1100                             unsigned int cmd, unsigned long arg);
1101 extern int drm_getunique(struct inode *inode, struct file *filp,
1102                          unsigned int cmd, unsigned long arg);
1103 extern int drm_setunique(struct inode *inode, struct file *filp,
1104                          unsigned int cmd, unsigned long arg);
1105 extern int drm_getmap(struct inode *inode, struct file *filp,
1106                       unsigned int cmd, unsigned long arg);
1107 extern int drm_getclient(struct inode *inode, struct file *filp,
1108                          unsigned int cmd, unsigned long arg);
1109 extern int drm_getstats(struct inode *inode, struct file *filp,
1110                         unsigned int cmd, unsigned long arg);
1111 extern int drm_setversion(struct inode *inode, struct file *filp,
1112                           unsigned int cmd, unsigned long arg);
1113 extern int drm_noop(struct inode *inode, struct file *filp,
1114                     unsigned int cmd, unsigned long arg);
1115
1116                                 /* Context IOCTL support (drm_context.h) */
1117 extern int drm_resctx(struct inode *inode, struct file *filp,
1118                       unsigned int cmd, unsigned long arg);
1119 extern int drm_addctx(struct inode *inode, struct file *filp,
1120                       unsigned int cmd, unsigned long arg);
1121 extern int drm_modctx(struct inode *inode, struct file *filp,
1122                       unsigned int cmd, unsigned long arg);
1123 extern int drm_getctx(struct inode *inode, struct file *filp,
1124                       unsigned int cmd, unsigned long arg);
1125 extern int drm_switchctx(struct inode *inode, struct file *filp,
1126                          unsigned int cmd, unsigned long arg);
1127 extern int drm_newctx(struct inode *inode, struct file *filp,
1128                       unsigned int cmd, unsigned long arg);
1129 extern int drm_rmctx(struct inode *inode, struct file *filp,
1130                      unsigned int cmd, unsigned long arg);
1131
1132 extern int drm_ctxbitmap_init(drm_device_t * dev);
1133 extern void drm_ctxbitmap_cleanup(drm_device_t * dev);
1134 extern void drm_ctxbitmap_free(drm_device_t * dev, int ctx_handle);
1135
1136 extern int drm_setsareactx(struct inode *inode, struct file *filp,
1137                            unsigned int cmd, unsigned long arg);
1138 extern int drm_getsareactx(struct inode *inode, struct file *filp,
1139                            unsigned int cmd, unsigned long arg);
1140
1141                                 /* Drawable IOCTL support (drm_drawable.h) */
1142 extern int drm_adddraw(struct inode *inode, struct file *filp,
1143                        unsigned int cmd, unsigned long arg);
1144 extern int drm_rmdraw(struct inode *inode, struct file *filp,
1145                       unsigned int cmd, unsigned long arg);
1146
1147                                 /* Authentication IOCTL support (drm_auth.h) */
1148 extern int drm_getmagic(struct inode *inode, struct file *filp,
1149                         unsigned int cmd, unsigned long arg);
1150 extern int drm_authmagic(struct inode *inode, struct file *filp,
1151                          unsigned int cmd, unsigned long arg);
1152
1153                                 /* Locking IOCTL support (drm_lock.h) */
1154 extern int drm_lock(struct inode *inode, struct file *filp,
1155                     unsigned int cmd, unsigned long arg);
1156 extern int drm_unlock(struct inode *inode, struct file *filp,
1157                       unsigned int cmd, unsigned long arg);
1158 extern int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context);
1159 extern int drm_lock_free(drm_device_t * dev,
1160                          __volatile__ unsigned int *lock, unsigned int context);
1161 /*
1162  * These are exported to drivers so that they can implement fencing using
1163  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock. 
1164  */
1165
1166 extern int drm_i_have_hw_lock(struct file *filp);
1167 extern int drm_kernel_take_hw_lock(struct file *filp);
1168
1169                                 /* Buffer management support (drm_bufs.h) */
1170 extern int drm_addbufs_agp(drm_device_t * dev, drm_buf_desc_t * request);
1171 extern int drm_addbufs_pci(drm_device_t * dev, drm_buf_desc_t * request);
1172 extern int drm_addbufs_fb (drm_device_t * dev, drm_buf_desc_t * request);
1173 extern int drm_addmap(drm_device_t * dev, unsigned int offset,
1174                       unsigned int size, drm_map_type_t type,
1175                       drm_map_flags_t flags, drm_local_map_t ** map_ptr);
1176 extern int drm_addmap_ioctl(struct inode *inode, struct file *filp,
1177                             unsigned int cmd, unsigned long arg);
1178 extern int drm_rmmap(drm_device_t *dev, drm_local_map_t *map);
1179 extern int drm_rmmap_locked(drm_device_t *dev, drm_local_map_t *map);
1180 extern int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
1181                            unsigned int cmd, unsigned long arg);
1182 extern int drm_addbufs(struct inode *inode, struct file *filp,
1183                        unsigned int cmd, unsigned long arg);
1184 extern int drm_infobufs(struct inode *inode, struct file *filp,
1185                         unsigned int cmd, unsigned long arg);
1186 extern int drm_markbufs(struct inode *inode, struct file *filp,
1187                         unsigned int cmd, unsigned long arg);
1188 extern int drm_freebufs(struct inode *inode, struct file *filp,
1189                         unsigned int cmd, unsigned long arg);
1190 extern int drm_mapbufs(struct inode *inode, struct file *filp,
1191                        unsigned int cmd, unsigned long arg);
1192 extern int drm_order(unsigned long size);
1193 extern unsigned long drm_get_resource_start(drm_device_t *dev,
1194                                             unsigned int resource);
1195 extern unsigned long drm_get_resource_len(drm_device_t *dev,
1196                                           unsigned int resource);
1197
1198                                 /* DMA support (drm_dma.h) */
1199 extern int drm_dma_setup(drm_device_t * dev);
1200 extern void drm_dma_takedown(drm_device_t * dev);
1201 extern void drm_free_buffer(drm_device_t * dev, drm_buf_t * buf);
1202 extern void drm_core_reclaim_buffers(drm_device_t *dev, struct file *filp);
1203
1204                                 /* IRQ support (drm_irq.h) */
1205 extern int drm_control(struct inode *inode, struct file *filp,
1206                        unsigned int cmd, unsigned long arg);
1207 extern irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1208 extern int drm_irq_uninstall(drm_device_t *dev);
1209 extern void drm_driver_irq_preinstall(drm_device_t * dev);
1210 extern void drm_driver_irq_postinstall(drm_device_t * dev);
1211 extern void drm_driver_irq_uninstall(drm_device_t * dev);
1212
1213 extern int drm_wait_vblank(struct inode *inode, struct file *filp,
1214                            unsigned int cmd, unsigned long arg);
1215 extern int drm_vblank_wait(drm_device_t * dev, unsigned int *vbl_seq);
1216 extern void drm_vbl_send_signals(drm_device_t * dev);
1217
1218                                 /* AGP/GART support (drm_agpsupport.h) */
1219 extern drm_agp_head_t *drm_agp_init(drm_device_t *dev);
1220 extern int drm_agp_acquire(drm_device_t * dev);
1221 extern int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp,
1222                            unsigned int cmd, unsigned long arg);
1223 extern int drm_agp_release(drm_device_t *dev);
1224 extern int drm_agp_release_ioctl(struct inode *inode, struct file *filp,
1225                            unsigned int cmd, unsigned long arg);
1226 extern int drm_agp_enable(drm_device_t *dev, drm_agp_mode_t mode);
1227 extern int drm_agp_enable_ioctl(struct inode *inode, struct file *filp,
1228                           unsigned int cmd, unsigned long arg);
1229 extern int drm_agp_info(drm_device_t * dev, drm_agp_info_t *info);
1230 extern int drm_agp_info_ioctl(struct inode *inode, struct file *filp,
1231                         unsigned int cmd, unsigned long arg);
1232 extern int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request);
1233 extern int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp,
1234                          unsigned int cmd, unsigned long arg);
1235 extern int drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request);
1236 extern int drm_agp_free_ioctl(struct inode *inode, struct file *filp,
1237                         unsigned int cmd, unsigned long arg);
1238 extern int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request);
1239 extern int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp,
1240                           unsigned int cmd, unsigned long arg);
1241 extern int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request);
1242 extern int drm_agp_bind_ioctl(struct inode *inode, struct file *filp,
1243                         unsigned int cmd, unsigned long arg);
1244 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
1245 extern DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type);
1246 #else
1247 extern DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type);
1248 #endif
1249 extern int drm_agp_free_memory(DRM_AGP_MEM * handle);
1250 extern int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start);
1251 extern int drm_agp_unbind_memory(DRM_AGP_MEM * handle);
1252 extern drm_ttm_backend_t *drm_agp_init_ttm_cached(struct drm_device *dev);
1253 extern drm_ttm_backend_t *drm_agp_init_ttm_uncached(struct drm_device *dev);
1254
1255                                 /* Stub support (drm_stub.h) */
1256 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
1257                      struct drm_driver *driver);
1258 extern int drm_put_dev(drm_device_t * dev);
1259 extern int drm_put_head(drm_head_t * head);
1260 extern unsigned int drm_debug; /* 1 to enable debug output */
1261 extern unsigned int drm_cards_limit;
1262 extern drm_head_t **drm_heads;
1263 extern drm_cache_t drm_cache;
1264 extern struct drm_sysfs_class *drm_class;
1265 extern struct proc_dir_entry *drm_proc_root;
1266
1267                                 /* Proc support (drm_proc.h) */
1268 extern int drm_proc_init(drm_device_t * dev,
1269                          int minor,
1270                          struct proc_dir_entry *root,
1271                          struct proc_dir_entry **dev_root);
1272 extern int drm_proc_cleanup(int minor,
1273                             struct proc_dir_entry *root,
1274                             struct proc_dir_entry *dev_root);
1275
1276                                 /* Scatter Gather Support (drm_scatter.h) */
1277 extern void drm_sg_cleanup(drm_sg_mem_t * entry);
1278 extern int drm_sg_alloc(struct inode *inode, struct file *filp,
1279                         unsigned int cmd, unsigned long arg);
1280 extern int drm_sg_free(struct inode *inode, struct file *filp,
1281                        unsigned int cmd, unsigned long arg);
1282
1283                                /* ATI PCIGART support (ati_pcigart.h) */
1284 extern int drm_ati_pcigart_init(drm_device_t * dev, drm_ati_pcigart_info *gart_info);
1285 extern int drm_ati_pcigart_cleanup(drm_device_t * dev, drm_ati_pcigart_info *gart_info);
1286
1287 extern drm_dma_handle_t *drm_pci_alloc(drm_device_t * dev, size_t size,
1288                            size_t align, dma_addr_t maxaddr);
1289 extern void __drm_pci_free(drm_device_t * dev, drm_dma_handle_t *dmah);
1290 extern void drm_pci_free(drm_device_t * dev, drm_dma_handle_t *dmah);
1291
1292                                /* sysfs support (drm_sysfs.c) */
1293 struct drm_sysfs_class;
1294 extern struct drm_sysfs_class *drm_sysfs_create(struct module *owner,
1295                                                 char *name);
1296 extern void drm_sysfs_destroy(struct drm_sysfs_class *cs);
1297 extern struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs,
1298                                                  drm_head_t * head);
1299 extern void drm_sysfs_device_remove(struct class_device *class_dev);
1300
1301 /* 
1302  * Basic memory manager support (drm_mm.c) 
1303  */
1304
1305 extern drm_mm_node_t * drm_mm_get_block(drm_mm_node_t * parent, unsigned long size,
1306                                                unsigned alignment);
1307 extern void drm_mm_put_block(drm_mm_t *mm, drm_mm_node_t *cur);
1308 extern drm_mm_node_t *drm_mm_search_free(const drm_mm_t *mm, unsigned long size, 
1309                                                 unsigned alignment, int best_match);
1310 extern int drm_mm_init(drm_mm_t *mm, unsigned long start, unsigned long size);
1311 extern void drm_mm_takedown(drm_mm_t *mm);
1312 extern int drm_mm_clean(drm_mm_t *mm);
1313
1314 /*
1315  * User space object bookkeeping (drm_object.c)
1316  */
1317
1318 /*
1319  * Must be called with the struct_mutex held.
1320  */
1321
1322 extern int drm_add_user_object(drm_file_t *priv, drm_user_object_t *item, 
1323
1324 /*
1325  * Must be called with the struct_mutex held.
1326  */
1327                                int shareable);
1328 extern drm_user_object_t *drm_lookup_user_object(drm_file_t *priv, uint32_t key);
1329
1330 /*
1331  * Must be called with the struct_mutex held.
1332  * If "item" has been obtained by a call to drm_lookup_user_object. You may not
1333  * release the struct_mutex before calling drm_remove_ref_object.
1334  * This function may temporarily release the struct_mutex.
1335  */
1336
1337 extern int drm_remove_user_object(drm_file_t *priv, drm_user_object_t *item);
1338
1339 /*
1340  * Must be called with the struct_mutex held. May temporarily release it.
1341  */
1342
1343 extern int drm_add_ref_object(drm_file_t *priv, drm_user_object_t *referenced_object,
1344                               drm_ref_t ref_action);
1345
1346 /*
1347  * Must be called with the struct_mutex held.
1348  */
1349
1350 drm_ref_object_t *drm_lookup_ref_object(drm_file_t *priv, 
1351                                         drm_user_object_t *referenced_object,
1352                                         drm_ref_t ref_action);
1353 /*
1354  * Must be called with the struct_mutex held.
1355  * If "item" has been obtained by a call to drm_lookup_ref_object. You may not
1356  * release the struct_mutex before calling drm_remove_ref_object.
1357  * This function may temporarily release the struct_mutex.
1358  */
1359
1360 extern void drm_remove_ref_object(drm_file_t *priv, drm_ref_object_t *item);
1361 extern int drm_user_object_ref(drm_file_t *priv, uint32_t user_token, drm_object_type_t type,
1362                                drm_user_object_t **object);
1363 extern int drm_user_object_unref(drm_file_t *priv, uint32_t user_token, drm_object_type_t type);
1364
1365
1366
1367 /*
1368  * fence objects (drm_fence.c)
1369  */
1370
1371 extern void drm_fence_handler(drm_device_t *dev, uint32_t breadcrumb, uint32_t type);
1372 extern void drm_fence_manager_init(drm_device_t *dev);
1373 extern void drm_fence_manager_takedown(drm_device_t *dev);
1374 extern void drm_fence_flush_old(drm_device_t *dev, uint32_t sequence);
1375 extern int drm_fence_object_flush(drm_device_t * dev,
1376                                   drm_fence_object_t * fence, uint32_t type);
1377 extern int drm_fence_object_signaled(drm_fence_object_t * fence, uint32_t type);
1378 extern void drm_fence_usage_deref_locked(drm_device_t * dev,
1379                                          drm_fence_object_t * fence);
1380 extern void drm_fence_usage_deref_unlocked(drm_device_t * dev,
1381                                          drm_fence_object_t * fence);
1382 extern int drm_fence_object_wait(drm_device_t * dev, drm_fence_object_t * fence,
1383                                  int lazy, int ignore_signals, uint32_t mask);
1384 extern int drm_fence_object_create(drm_device_t *dev, uint32_t type,
1385                                    int emit, drm_fence_object_t **c_fence);
1386 extern int drm_fence_add_user_object(drm_file_t *priv, 
1387                                      drm_fence_object_t *fence,
1388                                      int shareable);
1389
1390
1391
1392
1393
1394 extern int drm_fence_ioctl(DRM_IOCTL_ARGS);
1395
1396 /*
1397  * buffer objects (drm_bo.c)
1398  */
1399
1400 extern int drm_bo_ioctl(DRM_IOCTL_ARGS);
1401 extern int drm_mm_init_ioctl(DRM_IOCTL_ARGS);
1402 extern int drm_bo_clean_mm(drm_device_t *dev);
1403 extern int drm_fence_buffer_objects(drm_file_t * priv,
1404                                     struct list_head *list, 
1405                                     drm_fence_object_t *fence,
1406                                     drm_fence_object_t **used_fence);
1407
1408
1409 /* Inline replacements for DRM_IOREMAP macros */
1410 static __inline__ void drm_core_ioremap(struct drm_map *map,
1411                                         struct drm_device *dev)
1412 {
1413         map->handle = drm_ioremap(map->offset, map->size, dev);
1414 }
1415
1416 static __inline__ void drm_core_ioremap_nocache(struct drm_map *map,
1417                                                 struct drm_device *dev)
1418 {
1419         map->handle = drm_ioremap_nocache(map->offset, map->size, dev);
1420 }
1421
1422 static __inline__ void drm_core_ioremapfree(struct drm_map *map,
1423                                             struct drm_device *dev)
1424 {
1425         if (map->handle && map->size)
1426                 drm_ioremapfree(map->handle, map->size, dev);
1427 }
1428
1429 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev,
1430                                                    unsigned int token)
1431 {
1432         drm_map_list_t *_entry;
1433         list_for_each_entry(_entry, &dev->maplist->head, head)
1434                 if (_entry->user_token == token)
1435                         return _entry->map;
1436         return NULL;
1437 }
1438
1439 static __inline__ int drm_device_is_agp(drm_device_t *dev)
1440 {
1441         if ( dev->driver->device_is_agp != NULL ) {
1442                 int err = (*dev->driver->device_is_agp)( dev );
1443         
1444                 if (err != 2) {
1445                         return err;
1446                 }
1447         }
1448
1449         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1450 }
1451
1452 static __inline__ int drm_device_is_pcie(drm_device_t *dev)
1453 {
1454         return pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
1455 }
1456
1457 static __inline__ void drm_core_dropmap(struct drm_map *map)
1458 {
1459 }
1460
1461 #ifndef DEBUG_MEMORY
1462 /** Wrapper around kmalloc() */
1463 static __inline__ void *drm_alloc(size_t size, int area)
1464 {
1465         return kmalloc(size, GFP_KERNEL);
1466 }
1467
1468 /** Wrapper around kfree() */
1469 static __inline__ void drm_free(void *pt, size_t size, int area)
1470 {
1471         kfree(pt);
1472 }
1473 #else
1474 extern void *drm_alloc(size_t size, int area);
1475 extern void drm_free(void *pt, size_t size, int area);
1476 #endif
1477
1478 /*@}*/
1479
1480 #endif                          /* __KERNEL__ */
1481 #endif