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