Merge mga-1-0-0-branch into trunk.
[platform/upstream/libdrm.git] / linux-core / drmP.h
1 /* drmP.h -- Private header for Direct Rendering Manager -*- linux-c -*-
2  * Created: Mon Jan  4 10:05:05 1999 by faith@precisioninsight.com
3  *
4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6  * All rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the next
16  * paragraph) shall be included in all copies or substantial portions of the
17  * Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Authors:
28  *    Rickard E. (Rik) Faith <faith@valinux.com>
29  *    Gareth Hughes <gareth@valinux.com>
30  */
31
32 #ifndef _DRM_P_H_
33 #define _DRM_P_H_
34
35 #ifdef __KERNEL__
36 #ifdef __alpha__
37 /* add include of current.h so that "current" is defined
38  * before static inline funcs in wait.h. Doing this so we
39  * can build the DRM (part of PI DRI). 4/21/2000 S + B */
40 #include <asm/current.h>
41 #endif /* __alpha__ */
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/miscdevice.h>
46 #include <linux/fs.h>
47 #include <linux/proc_fs.h>
48 #include <linux/init.h>
49 #include <linux/file.h>
50 #include <linux/pci.h>
51 #include <linux/wrapper.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 #ifdef __alpha__
57 #include <asm/pgtable.h> /* For pte_wrprotect */
58 #endif
59 #include <asm/io.h>
60 #include <asm/mman.h>
61 #include <asm/uaccess.h>
62 #ifdef CONFIG_MTRR
63 #include <asm/mtrr.h>
64 #endif
65 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
66 #include <linux/types.h>
67 #include <linux/agp_backend.h>
68 #endif
69 #if LINUX_VERSION_CODE >= 0x020100 /* KERNEL_VERSION(2,1,0) */
70 #include <linux/tqueue.h>
71 #include <linux/poll.h>
72 #endif
73 #if LINUX_VERSION_CODE < 0x020400
74 #include "compat-pre24.h"
75 #endif
76 #include "drm.h"
77
78 /* DRM template customization defaults
79  */
80 #ifndef __HAVE_AGP
81 #define __HAVE_AGP              0
82 #endif
83 #ifndef __HAVE_MTRR
84 #define __HAVE_MTRR             0
85 #endif
86 #ifndef __HAVE_DMA
87 #define __HAVE_DMA              0
88 #endif
89 #ifndef __HAVE_DMA_WAITLIST
90 #define __HAVE_DMA_WAITLIST     0
91 #endif
92 #ifndef __HAVE_DMA_FREELIST
93 #define __HAVE_DMA_FREELIST     0
94 #endif
95
96 #define __REALLY_HAVE_AGP       (__HAVE_AGP && (defined(CONFIG_AGP) || \
97                                                 defined(CONFIG_AGP_MODULE)))
98 #define __REALLY_HAVE_MTRR      (__HAVE_MTRR && defined(CONFIG_MTRR))
99
100
101 /* Begin the DRM...
102  */
103
104 #define DRM_DEBUG_CODE 2          /* Include debugging code (if > 1, then
105                                      also include looping detection. */
106 #define DRM_DMA_HISTOGRAM 1       /* Make histogram of DMA latency. */
107
108 #define DRM_HASH_SIZE         16 /* Size of key hash table                */
109 #define DRM_KERNEL_CONTEXT    0  /* Change drm_resctx if changed          */
110 #define DRM_RESERVED_CONTEXTS 1  /* Change drm_resctx if changed          */
111 #define DRM_LOOPING_LIMIT     5000000
112 #define DRM_BSZ               1024 /* Buffer size for /dev/drm? output    */
113 #define DRM_TIME_SLICE        (HZ/20)  /* Time slice for GLXContexts      */
114 #define DRM_LOCK_SLICE        1 /* Time slice for lock, in jiffies        */
115
116 #define DRM_FLAG_DEBUG    0x01
117 #define DRM_FLAG_NOCTX    0x02
118
119 #define DRM_MEM_DMA        0
120 #define DRM_MEM_SAREA      1
121 #define DRM_MEM_DRIVER     2
122 #define DRM_MEM_MAGIC      3
123 #define DRM_MEM_IOCTLS     4
124 #define DRM_MEM_MAPS       5
125 #define DRM_MEM_VMAS       6
126 #define DRM_MEM_BUFS       7
127 #define DRM_MEM_SEGS       8
128 #define DRM_MEM_PAGES      9
129 #define DRM_MEM_FILES     10
130 #define DRM_MEM_QUEUES    11
131 #define DRM_MEM_CMDS      12
132 #define DRM_MEM_MAPPINGS  13
133 #define DRM_MEM_BUFLISTS  14
134 #define DRM_MEM_AGPLISTS  15
135 #define DRM_MEM_TOTALAGP  16
136 #define DRM_MEM_BOUNDAGP  17
137 #define DRM_MEM_CTXBITMAP 18
138 #define DRM_MEM_STUB      19
139
140 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
141
142                                 /* Backward compatibility section */
143                                 /* _PAGE_WT changed to _PAGE_PWT in 2.2.6 */
144 #ifndef _PAGE_PWT
145 #define _PAGE_PWT _PAGE_WT
146 #endif
147                                 /* Wait queue declarations changed in 2.3.1 */
148 #ifndef DECLARE_WAITQUEUE
149 #define DECLARE_WAITQUEUE(w,c) struct wait_queue w = { c, NULL }
150 typedef struct wait_queue *wait_queue_head_t;
151 #define init_waitqueue_head(q) *q = NULL;
152 #endif
153
154                                 /* _PAGE_4M changed to _PAGE_PSE in 2.3.23 */
155 #ifndef _PAGE_PSE
156 #define _PAGE_PSE _PAGE_4M
157 #endif
158
159                                 /* vm_offset changed to vm_pgoff in 2.3.25 */
160 #if LINUX_VERSION_CODE < 0x020319
161 #define VM_OFFSET(vma) ((vma)->vm_offset)
162 #else
163 #define VM_OFFSET(vma) ((vma)->vm_pgoff << PAGE_SHIFT)
164 #endif
165
166                                 /* *_nopage return values defined in 2.3.26 */
167 #ifndef NOPAGE_SIGBUS
168 #define NOPAGE_SIGBUS 0
169 #endif
170 #ifndef NOPAGE_OOM
171 #define NOPAGE_OOM 0
172 #endif
173
174                                 /* module_init/module_exit added in 2.3.13 */
175 #ifndef module_init
176 #define module_init(x)  int init_module(void) { return x(); }
177 #endif
178 #ifndef module_exit
179 #define module_exit(x)  void cleanup_module(void) { x(); }
180 #endif
181
182                                 /* Generic cmpxchg added in 2.3.x */
183 #ifndef __HAVE_ARCH_CMPXCHG
184                                 /* Include this here so that driver can be
185                                    used with older kernels. */
186 #if defined(__alpha__)
187 static __inline__ unsigned long
188 __cmpxchg_u32(volatile int *m, int old, int new)
189 {
190         unsigned long prev, cmp;
191
192         __asm__ __volatile__(
193         "1:     ldl_l %0,%2\n"
194         "       cmpeq %0,%3,%1\n"
195         "       beq %1,2f\n"
196         "       mov %4,%1\n"
197         "       stl_c %1,%2\n"
198         "       beq %1,3f\n"
199         "2:     mb\n"
200         ".subsection 2\n"
201         "3:     br 1b\n"
202         ".previous"
203         : "=&r"(prev), "=&r"(cmp), "=m"(*m)
204         : "r"((long) old), "r"(new), "m"(*m));
205
206         return prev;
207 }
208
209 static __inline__ unsigned long
210 __cmpxchg_u64(volatile long *m, unsigned long old, unsigned long new)
211 {
212         unsigned long prev, cmp;
213
214         __asm__ __volatile__(
215         "1:     ldq_l %0,%2\n"
216         "       cmpeq %0,%3,%1\n"
217         "       beq %1,2f\n"
218         "       mov %4,%1\n"
219         "       stq_c %1,%2\n"
220         "       beq %1,3f\n"
221         "2:     mb\n"
222         ".subsection 2\n"
223         "3:     br 1b\n"
224         ".previous"
225         : "=&r"(prev), "=&r"(cmp), "=m"(*m)
226         : "r"((long) old), "r"(new), "m"(*m));
227
228         return prev;
229 }
230
231 static __inline__ unsigned long
232 __cmpxchg(volatile void *ptr, unsigned long old, unsigned long new, int size)
233 {
234         switch (size) {
235                 case 4:
236                         return __cmpxchg_u32(ptr, old, new);
237                 case 8:
238                         return __cmpxchg_u64(ptr, old, new);
239         }
240         return old;
241 }
242 #define cmpxchg(ptr,o,n)                                                 \
243   ({                                                                     \
244      __typeof__(*(ptr)) _o_ = (o);                                       \
245      __typeof__(*(ptr)) _n_ = (n);                                       \
246      (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_,           \
247                                     (unsigned long)_n_, sizeof(*(ptr))); \
248   })
249
250 #elif __i386__
251 static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
252                                       unsigned long new, int size)
253 {
254         unsigned long prev;
255         switch (size) {
256         case 1:
257                 __asm__ __volatile__(LOCK_PREFIX "cmpxchgb %b1,%2"
258                                      : "=a"(prev)
259                                      : "q"(new), "m"(*__xg(ptr)), "0"(old)
260                                      : "memory");
261                 return prev;
262         case 2:
263                 __asm__ __volatile__(LOCK_PREFIX "cmpxchgw %w1,%2"
264                                      : "=a"(prev)
265                                      : "q"(new), "m"(*__xg(ptr)), "0"(old)
266                                      : "memory");
267                 return prev;
268         case 4:
269                 __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %1,%2"
270                                      : "=a"(prev)
271                                      : "q"(new), "m"(*__xg(ptr)), "0"(old)
272                                      : "memory");
273                 return prev;
274         }
275         return old;
276 }
277
278 #define cmpxchg(ptr,o,n)                                                \
279   ((__typeof__(*(ptr)))__cmpxchg((ptr),(unsigned long)(o),              \
280                                  (unsigned long)(n),sizeof(*(ptr))))
281 #endif /* i386 & alpha */
282 #endif
283
284                                 /* Macros to make printk easier */
285 #define DRM_ERROR(fmt, arg...) \
286         printk(KERN_ERR "[" DRM_NAME ":" __FUNCTION__ "] *ERROR* " fmt , ##arg)
287 #define DRM_MEM_ERROR(area, fmt, arg...) \
288         printk(KERN_ERR "[" DRM_NAME ":" __FUNCTION__ ":%s] *ERROR* " fmt , \
289                DRM(mem_stats)[area].name , ##arg)
290 #define DRM_INFO(fmt, arg...)  printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
291
292 #if DRM_DEBUG_CODE
293 #define DRM_DEBUG(fmt, arg...)                                          \
294         do {                                                            \
295                 if ( DRM(flags) & DRM_FLAG_DEBUG )                      \
296                         printk(KERN_DEBUG                               \
297                                "[" DRM_NAME ":" __FUNCTION__ "] " fmt , \
298                                ##arg);                                  \
299         } while (0)
300 #else
301 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
302 #endif
303
304 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
305
306 #define DRM_PROC_PRINT(fmt, arg...)                                     \
307    len += sprintf(&buf[len], fmt , ##arg);                              \
308    if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
309
310 #define DRM_PROC_PRINT_RET(ret, fmt, arg...)                            \
311    len += sprintf(&buf[len], fmt , ##arg);                              \
312    if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
313
314                                 /* Mapping helper macros */
315 #define DRM_IOREMAP(map)                                                \
316         (map)->handle = DRM(ioremap)( (map)->offset, (map)->size )
317
318 #define DRM_IOREMAPFREE(map)                                            \
319         do {                                                            \
320                 if ( (map)->handle && (map)->size )                     \
321                         DRM(ioremapfree)( (map)->handle, (map)->size ); \
322         } while (0)
323
324 #define DRM_FIND_MAP(map, o)                                            \
325         do {                                                            \
326                 int i;                                                  \
327                 for ( i = 0 ; i < dev->map_count ; i++ ) {              \
328                         if ( dev->maplist[i]->offset == o ) {           \
329                                 map = dev->maplist[i];                  \
330                                 break;                                  \
331                         }                                               \
332                 }                                                       \
333         } while (0)
334
335                                 /* Internal types and structures */
336 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
337 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
338 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
339
340 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
341 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
342 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
343
344 typedef int drm_ioctl_t( struct inode *inode, struct file *filp,
345                          unsigned int cmd, unsigned long arg );
346
347 typedef struct drm_ioctl_desc {
348         drm_ioctl_t          *func;
349         int                  auth_needed;
350         int                  root_only;
351 } drm_ioctl_desc_t;
352
353 typedef struct drm_devstate {
354         pid_t             owner;        /* X server pid holding x_lock */
355
356 } drm_devstate_t;
357
358 typedef struct drm_magic_entry {
359         drm_magic_t            magic;
360         struct drm_file        *priv;
361         struct drm_magic_entry *next;
362 } drm_magic_entry_t;
363
364 typedef struct drm_magic_head {
365         struct drm_magic_entry *head;
366         struct drm_magic_entry *tail;
367 } drm_magic_head_t;
368
369 typedef struct drm_vma_entry {
370         struct vm_area_struct *vma;
371         struct drm_vma_entry  *next;
372         pid_t                 pid;
373 } drm_vma_entry_t;
374
375 typedef struct drm_buf {
376         int               idx;         /* Index into master buflist          */
377         int               total;       /* Buffer size                        */
378         int               order;       /* log-base-2(total)                  */
379         int               used;        /* Amount of buffer in use (for DMA)  */
380         unsigned long     offset;      /* Byte offset (used internally)      */
381         void              *address;    /* Address of buffer                  */
382         unsigned long     bus_address; /* Bus address of buffer              */
383         struct drm_buf    *next;       /* Kernel-only: used for free list    */
384         __volatile__ int  waiting;     /* On kernel DMA queue                */
385         __volatile__ int  pending;     /* On hardware DMA queue              */
386         wait_queue_head_t dma_wait;    /* Processes waiting                  */
387         pid_t             pid;         /* PID of holding process             */
388         int               context;     /* Kernel queue for this buffer       */
389         int               while_locked;/* Dispatch this buffer while locked  */
390         enum {
391                 DRM_LIST_NONE    = 0,
392                 DRM_LIST_FREE    = 1,
393                 DRM_LIST_WAIT    = 2,
394                 DRM_LIST_PEND    = 3,
395                 DRM_LIST_PRIO    = 4,
396                 DRM_LIST_RECLAIM = 5
397         }                 list;        /* Which list we're on                */
398
399 #if DRM_DMA_HISTOGRAM
400         cycles_t          time_queued;     /* Queued to kernel DMA queue     */
401         cycles_t          time_dispatched; /* Dispatched to hardware         */
402         cycles_t          time_completed;  /* Completed by hardware          */
403         cycles_t          time_freed;      /* Back on freelist               */
404 #endif
405
406         int               dev_priv_size; /* Size of buffer private stoarge   */
407         void              *dev_private;  /* Per-buffer private storage       */
408 } drm_buf_t;
409
410 #if DRM_DMA_HISTOGRAM
411 #define DRM_DMA_HISTOGRAM_SLOTS           9
412 #define DRM_DMA_HISTOGRAM_INITIAL        10
413 #define DRM_DMA_HISTOGRAM_NEXT(current)  ((current)*10)
414 typedef struct drm_histogram {
415         atomic_t          total;
416
417         atomic_t          queued_to_dispatched[DRM_DMA_HISTOGRAM_SLOTS];
418         atomic_t          dispatched_to_completed[DRM_DMA_HISTOGRAM_SLOTS];
419         atomic_t          completed_to_freed[DRM_DMA_HISTOGRAM_SLOTS];
420
421         atomic_t          queued_to_completed[DRM_DMA_HISTOGRAM_SLOTS];
422         atomic_t          queued_to_freed[DRM_DMA_HISTOGRAM_SLOTS];
423
424         atomic_t          dma[DRM_DMA_HISTOGRAM_SLOTS];
425         atomic_t          schedule[DRM_DMA_HISTOGRAM_SLOTS];
426         atomic_t          ctx[DRM_DMA_HISTOGRAM_SLOTS];
427         atomic_t          lacq[DRM_DMA_HISTOGRAM_SLOTS];
428         atomic_t          lhld[DRM_DMA_HISTOGRAM_SLOTS];
429 } drm_histogram_t;
430 #endif
431
432                                 /* bufs is one longer than it has to be */
433 typedef struct drm_waitlist {
434         int               count;        /* Number of possible buffers      */
435         drm_buf_t         **bufs;       /* List of pointers to buffers     */
436         drm_buf_t         **rp;         /* Read pointer                    */
437         drm_buf_t         **wp;         /* Write pointer                   */
438         drm_buf_t         **end;        /* End pointer                     */
439         spinlock_t        read_lock;
440         spinlock_t        write_lock;
441 } drm_waitlist_t;
442
443 typedef struct drm_freelist {
444         int               initialized; /* Freelist in use                  */
445         atomic_t          count;       /* Number of free buffers           */
446         drm_buf_t         *next;       /* End pointer                      */
447
448         wait_queue_head_t waiting;     /* Processes waiting on free bufs   */
449         int               low_mark;    /* Low water mark                   */
450         int               high_mark;   /* High water mark                  */
451         atomic_t          wfh;         /* If waiting for high mark         */
452         spinlock_t        lock;
453 } drm_freelist_t;
454
455 typedef struct drm_buf_entry {
456         int               buf_size;
457         int               buf_count;
458         drm_buf_t         *buflist;
459         int               seg_count;
460         int               page_order;
461         unsigned long     *seglist;
462
463         drm_freelist_t    freelist;
464 } drm_buf_entry_t;
465
466 typedef struct drm_hw_lock {
467         __volatile__ unsigned int lock;
468         char                      padding[60]; /* Pad to cache line */
469 } drm_hw_lock_t;
470
471 typedef struct drm_file {
472         int               authenticated;
473         int               minor;
474         pid_t             pid;
475         uid_t             uid;
476         drm_magic_t       magic;
477         unsigned long     ioctl_count;
478         struct drm_file   *next;
479         struct drm_file   *prev;
480         struct drm_device *dev;
481         int               remove_auth_on_close;
482 } drm_file_t;
483
484
485 typedef struct drm_queue {
486         atomic_t          use_count;    /* Outstanding uses (+1)            */
487         atomic_t          finalization; /* Finalization in progress         */
488         atomic_t          block_count;  /* Count of processes waiting       */
489         atomic_t          block_read;   /* Queue blocked for reads          */
490         wait_queue_head_t read_queue;   /* Processes waiting on block_read  */
491         atomic_t          block_write;  /* Queue blocked for writes         */
492         wait_queue_head_t write_queue;  /* Processes waiting on block_write */
493 #if 1
494         atomic_t          total_queued; /* Total queued statistic           */
495         atomic_t          total_flushed;/* Total flushes statistic          */
496         atomic_t          total_locks;  /* Total locks statistics           */
497 #endif
498         drm_ctx_flags_t   flags;        /* Context preserving and 2D-only   */
499         drm_waitlist_t    waitlist;     /* Pending buffers                  */
500         wait_queue_head_t flush_queue;  /* Processes waiting until flush    */
501 } drm_queue_t;
502
503 typedef struct drm_lock_data {
504         drm_hw_lock_t     *hw_lock;     /* Hardware lock                   */
505         pid_t             pid;          /* PID of lock holder (0=kernel)   */
506         wait_queue_head_t lock_queue;   /* Queue of blocked processes      */
507         unsigned long     lock_time;    /* Time of last lock in jiffies    */
508 } drm_lock_data_t;
509
510 typedef struct drm_device_dma {
511 #if 0
512                                 /* Performance Counters */
513         atomic_t          total_prio;   /* Total DRM_DMA_PRIORITY          */
514         atomic_t          total_bytes;  /* Total bytes DMA'd               */
515         atomic_t          total_dmas;   /* Total DMA buffers dispatched    */
516
517         atomic_t          total_missed_dma;  /* Missed drm_do_dma           */
518         atomic_t          total_missed_lock; /* Missed lock in drm_do_dma   */
519         atomic_t          total_missed_free; /* Missed drm_free_this_buffer */
520         atomic_t          total_missed_sched;/* Missed drm_dma_schedule     */
521
522         atomic_t          total_tried;  /* Tried next_buffer                */
523         atomic_t          total_hit;    /* Sent next_buffer                 */
524         atomic_t          total_lost;   /* Lost interrupt                   */
525 #endif
526
527         drm_buf_entry_t   bufs[DRM_MAX_ORDER+1];
528         int               buf_count;
529         drm_buf_t         **buflist;    /* Vector of pointers info bufs    */
530         int               seg_count;
531         int               page_count;
532         unsigned long     *pagelist;
533         unsigned long     byte_count;
534         enum {
535                 _DRM_DMA_USE_AGP = 0x01
536         } flags;
537
538                                 /* DMA support */
539         drm_buf_t         *this_buffer; /* Buffer being sent               */
540         drm_buf_t         *next_buffer; /* Selected buffer to send         */
541         drm_queue_t       *next_queue;  /* Queue from which buffer selected*/
542         wait_queue_head_t waiting;      /* Processes waiting on free bufs  */
543 } drm_device_dma_t;
544
545 #if __REALLY_HAVE_AGP
546 typedef struct drm_agp_mem {
547         unsigned long      handle;
548         agp_memory         *memory;
549         unsigned long      bound; /* address */
550         int                pages;
551         struct drm_agp_mem *prev;
552         struct drm_agp_mem *next;
553 } drm_agp_mem_t;
554
555 typedef struct drm_agp_head {
556         agp_kern_info      agp_info;
557         const char         *chipset;
558         drm_agp_mem_t      *memory;
559         unsigned long      mode;
560         int                enabled;
561         int                acquired;
562         unsigned long      base;
563         int                agp_mtrr;
564 } drm_agp_head_t;
565 #endif
566
567 typedef struct drm_sigdata {
568         int           context;
569         drm_hw_lock_t *lock;
570 } drm_sigdata_t;
571
572 typedef struct drm_device {
573         const char        *name;        /* Simple driver name              */
574         char              *unique;      /* Unique identifier: e.g., busid  */
575         int               unique_len;   /* Length of unique field          */
576         dev_t             device;       /* Device number for mknod         */
577         char              *devname;     /* For /proc/interrupts            */
578
579         int               blocked;      /* Blocked due to VC switch?       */
580         struct proc_dir_entry *root;    /* Root for this device's entries  */
581
582                                 /* Locks */
583         spinlock_t        count_lock;   /* For inuse, open_count, buf_use  */
584         struct semaphore  struct_sem;   /* For others                      */
585
586                                 /* Usage Counters */
587         int               open_count;   /* Outstanding files open          */
588         atomic_t          ioctl_count;  /* Outstanding IOCTLs pending      */
589         atomic_t          vma_count;    /* Outstanding vma areas open      */
590         int               buf_use;      /* Buffers in use -- cannot alloc  */
591         atomic_t          buf_alloc;    /* Buffer allocation in progress   */
592
593                                 /* Performance counters */
594         unsigned long     counters;
595         drm_stat_type_t   types[15];
596         atomic_t          counts[15];
597
598                                 /* Authentication */
599         drm_file_t        *file_first;
600         drm_file_t        *file_last;
601         drm_magic_head_t  magiclist[DRM_HASH_SIZE];
602
603                                 /* Memory management */
604         drm_map_t         **maplist;    /* Vector of pointers to regions   */
605         int               map_count;    /* Number of mappable regions      */
606
607         drm_vma_entry_t   *vmalist;     /* List of vmas (for debugging)    */
608         drm_lock_data_t   lock;         /* Information on hardware lock    */
609
610                                 /* DMA queues (contexts) */
611         int               queue_count;  /* Number of active DMA queues     */
612         int               queue_reserved; /* Number of reserved DMA queues */
613         int               queue_slots;  /* Actual length of queuelist      */
614         drm_queue_t       **queuelist;  /* Vector of pointers to DMA queues */
615         drm_device_dma_t  *dma;         /* Optional pointer for DMA support */
616
617                                 /* Context support */
618         int               irq;          /* Interrupt used by board         */
619         __volatile__ long context_flag; /* Context swapping flag           */
620         __volatile__ long interrupt_flag; /* Interruption handler flag     */
621         __volatile__ long dma_flag;     /* DMA dispatch flag               */
622         struct timer_list timer;        /* Timer for delaying ctx switch   */
623         wait_queue_head_t context_wait; /* Processes waiting on ctx switch */
624         int               last_checked; /* Last context checked for DMA    */
625         int               last_context; /* Last current context            */
626         unsigned long     last_switch;  /* jiffies at last context switch  */
627         struct tq_struct  tq;
628         cycles_t          ctx_start;
629         cycles_t          lck_start;
630 #if DRM_DMA_HISTOGRAM
631         drm_histogram_t   histo;
632 #endif
633
634                                 /* Callback to X server for context switch
635                                    and for heavy-handed reset. */
636         char              buf[DRM_BSZ]; /* Output buffer                   */
637         char              *buf_rp;      /* Read pointer                    */
638         char              *buf_wp;      /* Write pointer                   */
639         char              *buf_end;     /* End pointer                     */
640         struct fasync_struct *buf_async;/* Processes waiting for SIGIO     */
641         wait_queue_head_t buf_readers;  /* Processes waiting to read       */
642         wait_queue_head_t buf_writers;  /* Processes waiting to ctx switch */
643
644 #if __REALLY_HAVE_AGP
645         drm_agp_head_t    *agp;
646 #endif
647         unsigned long     *ctx_bitmap;
648         void              *dev_private;
649         drm_sigdata_t     sigdata; /* For block_all_signals */
650         sigset_t          sigmask;
651 } drm_device_t;
652
653
654                                 /* Internal function definitions */
655
656                                 /* Misc. support (init.c) */
657 extern int           DRM(flags);
658 extern void          DRM(parse_options)( char *s );
659 extern int           DRM(cpu_valid)( void );
660
661                                 /* Driver support (drm_drv.h) */
662 extern int           DRM(version)(struct inode *inode, struct file *filp,
663                                   unsigned int cmd, unsigned long arg);
664 extern int           DRM(open)(struct inode *inode, struct file *filp);
665 extern int           DRM(release)(struct inode *inode, struct file *filp);
666 extern int           DRM(ioctl)(struct inode *inode, struct file *filp,
667                                 unsigned int cmd, unsigned long arg);
668 extern int           DRM(lock)(struct inode *inode, struct file *filp,
669                                unsigned int cmd, unsigned long arg);
670 extern int           DRM(unlock)(struct inode *inode, struct file *filp,
671                                  unsigned int cmd, unsigned long arg);
672
673                                 /* Device support (fops.c) */
674 extern int           DRM(open_helper)(struct inode *inode, struct file *filp,
675                                       drm_device_t *dev);
676 extern int           DRM(flush)(struct file *filp);
677 extern int           DRM(release_fuck)(struct inode *inode, struct file *filp);
678 extern int           DRM(fasync)(int fd, struct file *filp, int on);
679 extern ssize_t       DRM(read)(struct file *filp, char *buf, size_t count,
680                                loff_t *off);
681 extern int           DRM(write_string)(drm_device_t *dev, const char *s);
682 extern unsigned int  DRM(poll)(struct file *filp,
683                                struct poll_table_struct *wait);
684
685                                 /* Mapping support (vm.c) */
686 #if LINUX_VERSION_CODE < 0x020317
687 extern unsigned long DRM(vm_nopage)(struct vm_area_struct *vma,
688                                     unsigned long address,
689                                     int write_access);
690 extern unsigned long DRM(vm_shm_nopage)(struct vm_area_struct *vma,
691                                         unsigned long address,
692                                         int write_access);
693 extern unsigned long DRM(vm_shm_nopage_lock)(struct vm_area_struct *vma,
694                                              unsigned long address,
695                                              int write_access);
696 extern unsigned long DRM(vm_dma_nopage)(struct vm_area_struct *vma,
697                                         unsigned long address,
698                                         int write_access);
699 #else
700                                 /* Return type changed in 2.3.23 */
701 extern struct page *DRM(vm_nopage)(struct vm_area_struct *vma,
702                                    unsigned long address,
703                                    int write_access);
704 extern struct page *DRM(vm_shm_nopage)(struct vm_area_struct *vma,
705                                        unsigned long address,
706                                        int write_access);
707 extern struct page *DRM(vm_shm_nopage_lock)(struct vm_area_struct *vma,
708                                             unsigned long address,
709                                             int write_access);
710 extern struct page *DRM(vm_dma_nopage)(struct vm_area_struct *vma,
711                                        unsigned long address,
712                                        int write_access);
713 #endif
714 extern void          DRM(vm_open)(struct vm_area_struct *vma);
715 extern void          DRM(vm_close)(struct vm_area_struct *vma);
716 extern int           DRM(mmap_dma)(struct file *filp,
717                                    struct vm_area_struct *vma);
718 extern int           DRM(mmap)(struct file *filp, struct vm_area_struct *vma);
719
720
721                                 /* Proc support (proc.c) */
722 extern struct proc_dir_entry *drm_proc_init(drm_device_t *dev,
723                                             int minor,
724                                             struct proc_dir_entry *root,
725                                             struct proc_dir_entry **dev_root);
726 extern int          drm_proc_cleanup(int minor,
727                                      struct proc_dir_entry *root,
728                                      struct proc_dir_entry *dev_root);
729
730                                 /* Memory management support (memory.c) */
731 extern void          DRM(mem_init)(void);
732 extern int           DRM(mem_info)(char *buf, char **start, off_t offset,
733                                    int request, int *eof, void *data);
734 extern void          *DRM(alloc)(size_t size, int area);
735 extern void          *DRM(realloc)(void *oldpt, size_t oldsize, size_t size,
736                                    int area);
737 extern char          *DRM(strdup)(const char *s, int area);
738 extern void          DRM(strfree)(const char *s, int area);
739 extern void          DRM(free)(void *pt, size_t size, int area);
740 extern unsigned long DRM(alloc_pages)(int order, int area);
741 extern void          DRM(free_pages)(unsigned long address, int order,
742                                      int area);
743 extern void          *DRM(ioremap)(unsigned long offset, unsigned long size);
744 extern void          DRM(ioremapfree)(void *pt, unsigned long size);
745
746 #if __REALLY_HAVE_AGP
747 extern agp_memory    *DRM(alloc_agp)(int pages, u32 type);
748 extern int           DRM(free_agp)(agp_memory *handle, int pages);
749 extern int           DRM(bind_agp)(agp_memory *handle, unsigned int start);
750 extern int           DRM(unbind_agp)(agp_memory *handle);
751 #endif
752
753                                 /* Misc. IOCTL support (ioctl.c) */
754 extern int           DRM(irq_busid)(struct inode *inode, struct file *filp,
755                                     unsigned int cmd, unsigned long arg);
756 extern int           DRM(getunique)(struct inode *inode, struct file *filp,
757                                     unsigned int cmd, unsigned long arg);
758 extern int           DRM(setunique)(struct inode *inode, struct file *filp,
759                                     unsigned int cmd, unsigned long arg);
760 extern int           DRM(getmap)(struct inode *inode, struct file *filp,
761                                  unsigned int cmd, unsigned long arg);
762 extern int           DRM(getclient)(struct inode *inode, struct file *filp,
763                                     unsigned int cmd, unsigned long arg);
764 extern int           DRM(getstats)(struct inode *inode, struct file *filp,
765                                    unsigned int cmd, unsigned long arg);
766
767                                 /* Context IOCTL support (context.c) */
768 extern int           DRM(resctx)( struct inode *inode, struct file *filp,
769                                   unsigned int cmd, unsigned long arg );
770 extern int           DRM(addctx)( struct inode *inode, struct file *filp,
771                                   unsigned int cmd, unsigned long arg );
772 extern int           DRM(modctx)( struct inode *inode, struct file *filp,
773                                   unsigned int cmd, unsigned long arg );
774 extern int           DRM(getctx)( struct inode *inode, struct file *filp,
775                                   unsigned int cmd, unsigned long arg );
776 extern int           DRM(switchctx)( struct inode *inode, struct file *filp,
777                                      unsigned int cmd, unsigned long arg );
778 extern int           DRM(newctx)( struct inode *inode, struct file *filp,
779                                   unsigned int cmd, unsigned long arg );
780 extern int           DRM(rmctx)( struct inode *inode, struct file *filp,
781                                  unsigned int cmd, unsigned long arg );
782
783 extern int           DRM(context_switch)(drm_device_t *dev, int old, int new);
784 extern int           DRM(context_switch_complete)(drm_device_t *dev, int new);
785
786                                 /* Drawable IOCTL support (drawable.c) */
787 extern int           DRM(adddraw)(struct inode *inode, struct file *filp,
788                                   unsigned int cmd, unsigned long arg);
789 extern int           DRM(rmdraw)(struct inode *inode, struct file *filp,
790                                  unsigned int cmd, unsigned long arg);
791
792
793                                 /* Authentication IOCTL support (auth.c) */
794 extern int           DRM(add_magic)(drm_device_t *dev, drm_file_t *priv,
795                                     drm_magic_t magic);
796 extern int           DRM(remove_magic)(drm_device_t *dev, drm_magic_t magic);
797 extern int           DRM(getmagic)(struct inode *inode, struct file *filp,
798                                    unsigned int cmd, unsigned long arg);
799 extern int           DRM(authmagic)(struct inode *inode, struct file *filp,
800                                     unsigned int cmd, unsigned long arg);
801
802
803                                 /* Locking IOCTL support (lock.c) */
804 extern int           DRM(block)(struct inode *inode, struct file *filp,
805                                 unsigned int cmd, unsigned long arg);
806 extern int           DRM(unblock)(struct inode *inode, struct file *filp,
807                                   unsigned int cmd, unsigned long arg);
808 extern int           DRM(lock_take)(__volatile__ unsigned int *lock,
809                                     unsigned int context);
810 extern int           DRM(lock_transfer)(drm_device_t *dev,
811                                         __volatile__ unsigned int *lock,
812                                         unsigned int context);
813 extern int           DRM(lock_free)(drm_device_t *dev,
814                                     __volatile__ unsigned int *lock,
815                                     unsigned int context);
816 extern int           DRM(finish)(struct inode *inode, struct file *filp,
817                                  unsigned int cmd, unsigned long arg);
818 extern int           DRM(flush_unblock)(drm_device_t *dev, int context,
819                                         drm_lock_flags_t flags);
820 extern int           DRM(flush_block_and_flush)(drm_device_t *dev, int context,
821                                                 drm_lock_flags_t flags);
822 extern int           DRM(notifier)(void *priv);
823
824                                 /* Context Bitmap support (ctxbitmap.c) */
825 extern int           DRM(ctxbitmap_init)( drm_device_t *dev );
826 extern void          DRM(ctxbitmap_cleanup)( drm_device_t *dev );
827
828
829
830
831                                 /* Buffer management support (bufs.c) */
832 extern int           DRM(order)( unsigned long size );
833 extern int           DRM(addmap)( struct inode *inode, struct file *filp,
834                                   unsigned int cmd, unsigned long arg );
835 extern int           DRM(addbufs)( struct inode *inode, struct file *filp,
836                                    unsigned int cmd, unsigned long arg );
837 extern int           DRM(infobufs)( struct inode *inode, struct file *filp,
838                                     unsigned int cmd, unsigned long arg );
839 extern int           DRM(markbufs)( struct inode *inode, struct file *filp,
840                                     unsigned int cmd, unsigned long arg );
841 extern int           DRM(freebufs)( struct inode *inode, struct file *filp,
842                                     unsigned int cmd, unsigned long arg );
843 extern int           DRM(mapbufs)( struct inode *inode, struct file *filp,
844                                    unsigned int cmd, unsigned long arg );
845
846
847 #if __HAVE_DMA
848                                 /* DMA support (dma.c) */
849 extern int           DRM(dma_setup)(drm_device_t *dev);
850 extern void          DRM(dma_takedown)(drm_device_t *dev);
851 extern void          DRM(free_buffer)(drm_device_t *dev, drm_buf_t *buf);
852 extern void          DRM(reclaim_buffers)(drm_device_t *dev, pid_t pid);
853 extern void          DRM(clear_next_buffer)(drm_device_t *dev);
854 extern int           DRM(select_queue)(drm_device_t *dev,
855                                        void (*wrapper)(unsigned long));
856 extern int           DRM(dma_enqueue)(drm_device_t *dev, drm_dma_t *dma);
857 #if 0
858 extern int           DRM(dma_get_buffers)(drm_device_t *dev, drm_dma_t *dma);
859 #endif
860 #if DRM_DMA_HISTOGRAM
861 extern int           DRM(histogram_slot)(unsigned long count);
862 extern void          DRM(histogram_compute)(drm_device_t *dev, drm_buf_t *buf);
863 #endif
864 #endif
865
866                                 /* Buffer list management support (lists.c) */
867 #if __HAVE_DMA_WAITLIST
868 extern int           DRM(waitlist_create)(drm_waitlist_t *bl, int count);
869 extern int           DRM(waitlist_destroy)(drm_waitlist_t *bl);
870 extern int           DRM(waitlist_put)(drm_waitlist_t *bl, drm_buf_t *buf);
871 extern drm_buf_t     *DRM(waitlist_get)(drm_waitlist_t *bl);
872 #endif
873 #if __HAVE_DMA_FREELIST
874 extern int           DRM(freelist_create)(drm_freelist_t *bl, int count);
875 extern int           DRM(freelist_destroy)(drm_freelist_t *bl);
876 extern int           DRM(freelist_put)(drm_device_t *dev, drm_freelist_t *bl,
877                                        drm_buf_t *buf);
878 extern drm_buf_t     *DRM(freelist_get)(drm_freelist_t *bl, int block);
879 #endif
880
881 #if __REALLY_HAVE_AGP
882                                 /* AGP/GART support (agpsupport.c) */
883 extern drm_agp_head_t *DRM(agp_init)(void);
884 extern void           DRM(agp_uninit)(void);
885 extern int            DRM(agp_acquire)(struct inode *inode, struct file *filp,
886                                        unsigned int cmd, unsigned long arg);
887 extern void           DRM(agp_do_release)(void);
888 extern int            DRM(agp_release)(struct inode *inode, struct file *filp,
889                                        unsigned int cmd, unsigned long arg);
890 extern int            DRM(agp_enable)(struct inode *inode, struct file *filp,
891                                       unsigned int cmd, unsigned long arg);
892 extern int            DRM(agp_info)(struct inode *inode, struct file *filp,
893                                     unsigned int cmd, unsigned long arg);
894 extern int            DRM(agp_alloc)(struct inode *inode, struct file *filp,
895                                      unsigned int cmd, unsigned long arg);
896 extern int            DRM(agp_free)(struct inode *inode, struct file *filp,
897                                     unsigned int cmd, unsigned long arg);
898 extern int            DRM(agp_unbind)(struct inode *inode, struct file *filp,
899                                       unsigned int cmd, unsigned long arg);
900 extern int            DRM(agp_bind)(struct inode *inode, struct file *filp,
901                                     unsigned int cmd, unsigned long arg);
902 extern agp_memory     *DRM(agp_allocate_memory)(size_t pages, u32 type);
903 extern int            DRM(agp_free_memory)(agp_memory *handle);
904 extern int            DRM(agp_bind_memory)(agp_memory *handle, off_t start);
905 extern int            DRM(agp_unbind_memory)(agp_memory *handle);
906
907                                 /* Stub support (drm_stub.h) */
908 int                   DRM(stub_register)(const char *name,
909                                          struct file_operations *fops,
910                                          drm_device_t *dev);
911 int                   DRM(stub_unregister)(int minor);
912
913 #endif
914 #endif
915 #endif