vserver 1.9.5.x5
[linux-2.6.git] / drivers / char / drm / drmP.h
1 /**
2  * \file drmP.h 
3  * Private header for Direct Rendering Manager
4  * 
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8
9 /*
10  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12  * 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 /* If you want the memory alloc debug functionality, change define below */
38 /* #define DEBUG_MEMORY */
39
40 #ifdef __KERNEL__
41 #ifdef __alpha__
42 /* add include of current.h so that "current" is defined
43  * before static inline funcs in wait.h. Doing this so we
44  * can build the DRM (part of PI DRI). 4/21/2000 S + B */
45 #include <asm/current.h>
46 #endif /* __alpha__ */
47 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/miscdevice.h>
51 #include <linux/fs.h>
52 #include <linux/proc_fs.h>
53 #include <linux/init.h>
54 #include <linux/file.h>
55 #include <linux/pci.h>
56 #include <linux/version.h>
57 #include <linux/jiffies.h>
58 #include <linux/smp_lock.h>     /* For (un)lock_kernel */
59 #include <linux/mm.h>
60 #include <linux/cdev.h>
61 #if defined(__alpha__) || defined(__powerpc__)
62 #include <asm/pgtable.h> /* For pte_wrprotect */
63 #endif
64 #include <asm/io.h>
65 #include <asm/mman.h>
66 #include <asm/uaccess.h>
67 #ifdef CONFIG_MTRR
68 #include <asm/mtrr.h>
69 #endif
70 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
71 #include <linux/types.h>
72 #include <linux/agp_backend.h>
73 #endif
74 #include <linux/workqueue.h>
75 #include <linux/poll.h>
76 #include <asm/pgalloc.h>
77 #include "drm.h"
78
79 #define __OS_HAS_AGP (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
80 #define __OS_HAS_MTRR (defined(CONFIG_MTRR))
81
82 #include "drm_os_linux.h"
83
84 /***********************************************************************/
85 /** \name DRM template customization defaults */
86 /*@{*/
87
88 /* driver capabilities and requirements mask */
89 #define DRIVER_USE_AGP     0x1
90 #define DRIVER_REQUIRE_AGP 0x2
91 #define DRIVER_USE_MTRR    0x4
92 #define DRIVER_PCI_DMA     0x8
93 #define DRIVER_SG          0x10
94 #define DRIVER_HAVE_DMA    0x20
95 #define DRIVER_HAVE_IRQ    0x40
96 #define DRIVER_IRQ_SHARED  0x80
97 #define DRIVER_IRQ_VBL     0x100
98 #define DRIVER_DMA_QUEUE   0x200
99
100 /***********************************************************************/
101 /** \name Begin the DRM... */
102 /*@{*/
103
104 #define DRM_DEBUG_CODE 2          /**< Include debugging code if > 1, then
105                                      also include looping detection. */
106
107 #define DRM_HASH_SIZE         16 /**< Size of key hash table. Must be power of 2. */
108 #define DRM_KERNEL_CONTEXT    0  /**< Change drm_resctx if changed */
109 #define DRM_RESERVED_CONTEXTS 1  /**< Change drm_resctx if changed */
110 #define DRM_LOOPING_LIMIT     5000000
111 #define DRM_BSZ               1024 /**< Buffer size for /dev/drm? output */
112 #define DRM_TIME_SLICE        (HZ/20)  /**< Time slice for GLXContexts */
113 #define DRM_LOCK_SLICE        1 /**< Time slice for lock, in jiffies */
114
115 #define DRM_FLAG_DEBUG    0x01
116
117 #define DRM_MEM_DMA        0
118 #define DRM_MEM_SAREA      1
119 #define DRM_MEM_DRIVER     2
120 #define DRM_MEM_MAGIC      3
121 #define DRM_MEM_IOCTLS     4
122 #define DRM_MEM_MAPS       5
123 #define DRM_MEM_VMAS       6
124 #define DRM_MEM_BUFS       7
125 #define DRM_MEM_SEGS       8
126 #define DRM_MEM_PAGES      9
127 #define DRM_MEM_FILES     10
128 #define DRM_MEM_QUEUES    11
129 #define DRM_MEM_CMDS      12
130 #define DRM_MEM_MAPPINGS  13
131 #define DRM_MEM_BUFLISTS  14
132 #define DRM_MEM_AGPLISTS  15
133 #define DRM_MEM_TOTALAGP  16
134 #define DRM_MEM_BOUNDAGP  17
135 #define DRM_MEM_CTXBITMAP 18
136 #define DRM_MEM_STUB      19
137 #define DRM_MEM_SGLISTS   20
138 #define DRM_MEM_CTXLIST  21
139
140 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
141         
142 /*@}*/
143
144
145 /***********************************************************************/
146 /** \name Backward compatibility section */
147 /*@{*/
148
149 #ifndef MODULE_LICENSE
150 #define MODULE_LICENSE(x) 
151 #endif
152
153 #ifndef preempt_disable
154 #define preempt_disable()
155 #define preempt_enable()
156 #endif
157
158 #ifndef pte_offset_map 
159 #define pte_offset_map pte_offset
160 #define pte_unmap(pte)
161 #endif
162
163 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,19)
164 static inline struct page * vmalloc_to_page(void * vmalloc_addr)
165 {
166         unsigned long addr = (unsigned long) vmalloc_addr;
167         struct page *page = NULL;
168         pgd_t *pgd = pgd_offset_k(addr);
169         pmd_t *pmd;
170         pte_t *ptep, pte;
171   
172         if (!pgd_none(*pgd)) {
173                 pmd = pmd_offset(pgd, addr);
174                 if (!pmd_none(*pmd)) {
175                         preempt_disable();
176                         ptep = pte_offset_map(pmd, addr);
177                         pte = *ptep;
178                         if (pte_present(pte))
179                                 page = pte_page(pte);
180                         pte_unmap(ptep);
181                         preempt_enable();
182                 }
183         }
184         return page;
185 }
186 #endif
187
188 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
189 #define DRM_RPR_ARG(vma)
190 #else
191 #define DRM_RPR_ARG(vma) vma,
192 #endif
193
194 #define VM_OFFSET(vma) ((vma)->vm_pgoff << PAGE_SHIFT)
195
196 /*@}*/
197
198
199 /***********************************************************************/
200 /** \name Macros to make printk easier */
201 /*@{*/
202
203 /**
204  * Error output.
205  *
206  * \param fmt printf() like format string.
207  * \param arg arguments
208  */
209 #define DRM_ERROR(fmt, arg...) \
210         printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* " fmt , __FUNCTION__ , ##arg)
211
212 /**
213  * Memory error output.
214  *
215  * \param area memory area where the error occurred.
216  * \param fmt printf() like format string.
217  * \param arg arguments
218  */
219 #define DRM_MEM_ERROR(area, fmt, arg...) \
220         printk(KERN_ERR "[" DRM_NAME ":%s:%s] *ERROR* " fmt , __FUNCTION__, \
221                drm_mem_stats[area].name , ##arg)
222
223 #define DRM_INFO(fmt, arg...)  printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
224
225 /**
226  * Debug output.
227  * 
228  * \param fmt printf() like format string.
229  * \param arg arguments
230  */
231 #if DRM_DEBUG_CODE
232 #define DRM_DEBUG(fmt, arg...)                                          \
233         do {                                                            \
234                 if ( drm_debug )                        \
235                         printk(KERN_DEBUG                               \
236                                "[" DRM_NAME ":%s] " fmt ,       \
237                                __FUNCTION__ , ##arg);                   \
238         } while (0)
239 #else
240 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
241 #endif
242
243 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
244
245 #define DRM_PROC_PRINT(fmt, arg...)                                     \
246    len += sprintf(&buf[len], fmt , ##arg);                              \
247    if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
248
249 #define DRM_PROC_PRINT_RET(ret, fmt, arg...)                            \
250    len += sprintf(&buf[len], fmt , ##arg);                              \
251    if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
252
253 /*@}*/
254
255
256 /***********************************************************************/
257 /** \name Internal types and structures */
258 /*@{*/
259
260 #define DRM_ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
261 #define DRM_MIN(a,b) ((a)<(b)?(a):(b))
262 #define DRM_MAX(a,b) ((a)>(b)?(a):(b))
263
264 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
265 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
266 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
267
268 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
269 /**
270  * Get the private SAREA mapping.
271  *
272  * \param _dev DRM device.
273  * \param _ctx context number.
274  * \param _map output mapping.
275  */
276 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
277         (_map) = (_dev)->context_sareas[_ctx];          \
278 } while(0)
279
280 /**
281  * Test that the hardware lock is held by the caller, returning otherwise.
282  *
283  * \param dev DRM device.
284  * \param filp file pointer of the caller.
285  */
286 #define LOCK_TEST_WITH_RETURN( dev, filp )                              \
287 do {                                                                    \
288         if ( !_DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ) ||           \
289              dev->lock.filp != filp ) {                         \
290                 DRM_ERROR( "%s called without lock held\n",             \
291                            __FUNCTION__ );                              \
292                 return -EINVAL;                                         \
293         }                                                               \
294 } while (0)
295
296 /**
297  * Copy and IOCTL return string to user space
298  */
299 #define DRM_COPY( name, value )                                         \
300         len = strlen( value );                                          \
301         if ( len > name##_len ) len = name##_len;                       \
302         name##_len = strlen( value );                                   \
303         if ( len && name ) {                                            \
304                 if ( copy_to_user( name, value, len ) )                 \
305                         return -EFAULT;                                 \
306         }
307         
308 /**
309  * Ioctl function type.
310  *
311  * \param inode device inode.
312  * \param filp file pointer.
313  * \param cmd command.
314  * \param arg argument.
315  */
316 typedef int drm_ioctl_t( struct inode *inode, struct file *filp,
317                          unsigned int cmd, unsigned long arg );
318
319 typedef struct drm_ioctl_desc {
320         drm_ioctl_t          *func;
321         int                  auth_needed;
322         int                  root_only;
323 } drm_ioctl_desc_t;
324
325 typedef struct drm_devstate {
326         pid_t             owner;        /**< X server pid holding x_lock */
327 } drm_devstate_t;
328
329 typedef struct drm_magic_entry {
330         drm_magic_t            magic;
331         struct drm_file        *priv;
332         struct drm_magic_entry *next;
333 } drm_magic_entry_t;
334
335 typedef struct drm_magic_head {
336         struct drm_magic_entry *head;
337         struct drm_magic_entry *tail;
338 } drm_magic_head_t;
339
340 typedef struct drm_vma_entry {
341         struct vm_area_struct *vma;
342         struct drm_vma_entry  *next;
343         pid_t                 pid;
344 } drm_vma_entry_t;
345
346 /**
347  * DMA buffer.
348  */
349 typedef struct drm_buf {
350         int               idx;         /**< Index into master buflist */
351         int               total;       /**< Buffer size */
352         int               order;       /**< log-base-2(total) */
353         int               used;        /**< Amount of buffer in use (for DMA) */
354         unsigned long     offset;      /**< Byte offset (used internally) */
355         void              *address;    /**< Address of buffer */
356         unsigned long     bus_address; /**< Bus address of buffer */
357         struct drm_buf    *next;       /**< Kernel-only: used for free list */
358         __volatile__ int  waiting;     /**< On kernel DMA queue */
359         __volatile__ int  pending;     /**< On hardware DMA queue */
360         wait_queue_head_t dma_wait;    /**< Processes waiting */
361         struct file       *filp;       /**< Pointer to holding file descr */
362         int               context;     /**< Kernel queue for this buffer */
363         int               while_locked;/**< Dispatch this buffer while locked */
364         enum {
365                 DRM_LIST_NONE    = 0,
366                 DRM_LIST_FREE    = 1,
367                 DRM_LIST_WAIT    = 2,
368                 DRM_LIST_PEND    = 3,
369                 DRM_LIST_PRIO    = 4,
370                 DRM_LIST_RECLAIM = 5
371         }                 list;        /**< Which list we're on */
372
373         int               dev_priv_size; /**< Size of buffer private storage */
374         void              *dev_private;  /**< Per-buffer private storage */
375 } drm_buf_t;
376
377
378 /** bufs is one longer than it has to be */
379 typedef struct drm_waitlist {
380         int               count;        /**< Number of possible buffers */
381         drm_buf_t         **bufs;       /**< List of pointers to buffers */
382         drm_buf_t         **rp;         /**< Read pointer */
383         drm_buf_t         **wp;         /**< Write pointer */
384         drm_buf_t         **end;        /**< End pointer */
385         spinlock_t        read_lock;
386         spinlock_t        write_lock;
387 } drm_waitlist_t;
388
389 typedef struct drm_freelist {
390         int               initialized; /**< Freelist in use */
391         atomic_t          count;       /**< Number of free buffers */
392         drm_buf_t         *next;       /**< End pointer */
393
394         wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
395         int               low_mark;    /**< Low water mark */
396         int               high_mark;   /**< High water mark */
397         atomic_t          wfh;         /**< If waiting for high mark */
398         spinlock_t        lock;
399 } drm_freelist_t;
400
401 /**
402  * Buffer entry.  There is one of this for each buffer size order.
403  */
404 typedef struct drm_buf_entry {
405         int               buf_size;     /**< size */
406         int               buf_count;    /**< number of buffers */
407         drm_buf_t         *buflist;     /**< buffer list */
408         int               seg_count;
409         int               page_order;
410         unsigned long     *seglist;
411
412         drm_freelist_t    freelist;
413 } drm_buf_entry_t;
414
415 /** File private data */
416 typedef struct drm_file {
417         int               authenticated;
418         int               minor;
419         pid_t             pid;
420         uid_t             uid;
421         drm_magic_t       magic;
422         unsigned long     ioctl_count;
423         struct drm_file   *next;
424         struct drm_file   *prev;
425         struct drm_device *dev;
426         int               remove_auth_on_close;
427         unsigned long     lock_count;
428         void              *driver_priv;
429 } drm_file_t;
430
431 /** Wait queue */
432 typedef struct drm_queue {
433         atomic_t          use_count;    /**< Outstanding uses (+1) */
434         atomic_t          finalization; /**< Finalization in progress */
435         atomic_t          block_count;  /**< Count of processes waiting */
436         atomic_t          block_read;   /**< Queue blocked for reads */
437         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
438         atomic_t          block_write;  /**< Queue blocked for writes */
439         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
440 #if 1
441         atomic_t          total_queued; /**< Total queued statistic */
442         atomic_t          total_flushed;/**< Total flushes statistic */
443         atomic_t          total_locks;  /**< Total locks statistics */
444 #endif
445         drm_ctx_flags_t   flags;        /**< Context preserving and 2D-only */
446         drm_waitlist_t    waitlist;     /**< Pending buffers */
447         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
448 } drm_queue_t;
449
450 /**
451  * Lock data.
452  */
453 typedef struct drm_lock_data {
454         drm_hw_lock_t     *hw_lock;     /**< Hardware lock */
455         struct file       *filp;        /**< File descr of lock holder (0=kernel) */
456         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
457         unsigned long     lock_time;    /**< Time of last lock in jiffies */
458 } drm_lock_data_t;
459
460 /**
461  * DMA data.
462  */
463 typedef struct drm_device_dma {
464
465         drm_buf_entry_t   bufs[DRM_MAX_ORDER+1];        /**< buffers, grouped by their size order */
466         int               buf_count;    /**< total number of buffers */
467         drm_buf_t         **buflist;    /**< Vector of pointers into drm_device_dma::bufs */
468         int               seg_count;
469         int               page_count;   /**< number of pages */
470         unsigned long     *pagelist;    /**< page list */
471         unsigned long     byte_count;
472         enum {
473                 _DRM_DMA_USE_AGP = 0x01,
474                 _DRM_DMA_USE_SG  = 0x02
475         } flags;
476
477 } drm_device_dma_t;
478
479 /** 
480  * AGP memory entry.  Stored as a doubly linked list.
481  */
482 typedef struct drm_agp_mem {
483         unsigned long      handle;      /**< handle */
484         DRM_AGP_MEM        *memory;     
485         unsigned long      bound;       /**< address */
486         int                pages;
487         struct drm_agp_mem *prev;       /**< previous entry */
488         struct drm_agp_mem *next;       /**< next entry */
489 } drm_agp_mem_t;
490
491 /**
492  * AGP data.
493  *
494  * \sa drm_agp_init() and drm_device::agp.
495  */
496 typedef struct drm_agp_head {
497         DRM_AGP_KERN       agp_info;    /**< AGP device information */
498         drm_agp_mem_t      *memory;     /**< memory entries */
499         unsigned long      mode;        /**< AGP mode */
500         int                enabled;     /**< whether the AGP bus as been enabled */
501         int                acquired;    /**< whether the AGP device has been acquired */
502         unsigned long      base;
503         int                agp_mtrr;
504         int                cant_use_aperture;
505         unsigned long      page_mask;
506 } drm_agp_head_t;
507
508 /**
509  * Scatter-gather memory.
510  */
511 typedef struct drm_sg_mem {
512         unsigned long   handle;
513         void            *virtual;
514         int             pages;
515         struct page     **pagelist;
516         dma_addr_t      *busaddr;
517 } drm_sg_mem_t;
518
519 typedef struct drm_sigdata {
520         int           context;
521         drm_hw_lock_t *lock;
522 } drm_sigdata_t;
523
524 /**
525  * Mappings list
526  */
527 typedef struct drm_map_list {
528         struct list_head        head;   /**< list head */
529         drm_map_t               *map;   /**< mapping */
530 } drm_map_list_t;
531
532 typedef drm_map_t drm_local_map_t;
533
534 /**
535  * Context handle list
536  */
537 typedef struct drm_ctx_list {
538         struct list_head        head;   /**< list head */
539         drm_context_t           handle; /**< context handle */
540         drm_file_t              *tag;   /**< associated fd private data */
541 } drm_ctx_list_t;
542
543
544 typedef struct drm_vbl_sig {
545         struct list_head        head;
546         unsigned int            sequence;
547         struct siginfo          info;
548         struct task_struct      *task;
549 } drm_vbl_sig_t;
550
551
552 /**
553  * DRM driver structure. This structure represent the common code for
554  * a family of cards. There will one drm_device for each card present
555  * in this family
556  */
557 struct drm_device;
558
559 struct drm_driver {
560         int (*preinit)(struct drm_device *, unsigned long flags);
561         void (*prerelease)(struct drm_device *, struct file *filp);
562         void (*pretakedown)(struct drm_device *);
563         int (*postcleanup)(struct drm_device *);
564         int (*presetup)(struct drm_device *);
565         int (*postsetup)(struct drm_device *);
566         int (*dma_ioctl)( DRM_IOCTL_ARGS );
567         int (*open_helper)(struct drm_device *, drm_file_t *);
568         void (*free_filp_priv)(struct drm_device *, drm_file_t *);
569         void (*release)(struct drm_device *, struct file *filp);
570         void (*dma_ready)(struct drm_device *);
571         int (*dma_quiescent)(struct drm_device *);
572         int (*context_ctor)(struct drm_device *dev, int context);
573         int (*context_dtor)(struct drm_device *dev, int context);
574         int (*kernel_context_switch)(struct drm_device *dev, int old, int new);
575         void (*kernel_context_switch_unlock)(struct drm_device *dev, drm_lock_t *lock);
576         int (*vblank_wait)(struct drm_device *dev, unsigned int *sequence);
577         /* these have to be filled in */
578         int (*postinit)(struct drm_device *, unsigned long flags);
579         irqreturn_t (*irq_handler)( DRM_IRQ_ARGS );
580         void (*irq_preinstall)(struct drm_device *dev);
581         void (*irq_postinstall)(struct drm_device *dev);
582         void (*irq_uninstall)(struct drm_device *dev);
583         void (*reclaim_buffers)(struct drm_device *dev, struct file *filp);
584         unsigned long (*get_map_ofs)(drm_map_t *map);
585         unsigned long (*get_reg_ofs)(struct drm_device *dev);
586         void (*set_version)(struct drm_device *dev, drm_set_version_t *sv);
587         int (*version)(drm_version_t *version);
588         u32 driver_features;
589         int dev_priv_size;
590         drm_ioctl_desc_t *ioctls;
591         int num_ioctls;
592         struct file_operations fops;
593         struct pci_driver pci_driver;
594 };
595 /**
596  * DRM device structure.
597  */
598 typedef struct drm_device {
599         char              *unique;      /**< Unique identifier: e.g., busid */
600         int               unique_len;   /**< Length of unique field */
601         dev_t             device;       /**< Device number for mknod */
602         char              *devname;     /**< For /proc/interrupts */
603         int               minor;        /**< Minor device number */
604         int               if_version;   /**< Highest interface version set */
605
606         int               blocked;      /**< Blocked due to VC switch? */
607         struct proc_dir_entry *root;    /**< Root for this device's entries */
608
609         /** \name Locks */
610         /*@{*/
611         spinlock_t        count_lock;   /**< For inuse, drm_device::open_count, drm_device::buf_use */
612         struct semaphore  struct_sem;   /**< For others */
613         /*@}*/
614
615         /** \name Usage Counters */
616         /*@{*/
617         int               open_count;   /**< Outstanding files open */
618         atomic_t          ioctl_count;  /**< Outstanding IOCTLs pending */
619         atomic_t          vma_count;    /**< Outstanding vma areas open */
620         int               buf_use;      /**< Buffers in use -- cannot alloc */
621         atomic_t          buf_alloc;    /**< Buffer allocation in progress */
622         /*@}*/
623
624         /** \name Performance counters */
625         /*@{*/
626         unsigned long     counters;
627         drm_stat_type_t   types[15];
628         atomic_t          counts[15];
629         /*@}*/
630
631         /** \name Authentication */
632         /*@{*/
633         drm_file_t        *file_first;  /**< file list head */
634         drm_file_t        *file_last;   /**< file list tail */
635         drm_magic_head_t  magiclist[DRM_HASH_SIZE];     /**< magic hash table */
636         /*@}*/
637
638         /** \name Memory management */
639         /*@{*/
640         drm_map_list_t    *maplist;     /**< Linked list of regions */
641         int               map_count;    /**< Number of mappable regions */
642
643         /** \name Context handle management */
644         /*@{*/
645         drm_ctx_list_t    *ctxlist;     /**< Linked list of context handles */
646         int               ctx_count;    /**< Number of context handles */
647         struct semaphore  ctxlist_sem;  /**< For ctxlist */
648
649         drm_map_t         **context_sareas; /**< per-context SAREA's */
650         int               max_context;
651
652         drm_vma_entry_t   *vmalist;     /**< List of vmas (for debugging) */
653         drm_lock_data_t   lock;         /**< Information on hardware lock */
654         /*@}*/
655
656         /** \name DMA queues (contexts) */
657         /*@{*/
658         int               queue_count;  /**< Number of active DMA queues */
659         int               queue_reserved; /**< Number of reserved DMA queues */
660         int               queue_slots;  /**< Actual length of queuelist */
661         drm_queue_t       **queuelist;  /**< Vector of pointers to DMA queues */
662         drm_device_dma_t  *dma;         /**< Optional pointer for DMA support */
663         /*@}*/
664
665         /** \name Context support */
666         /*@{*/
667         int               irq;          /**< Interrupt used by board */
668         int               irq_enabled;  /**< True if irq handler is enabled */
669         __volatile__ long context_flag; /**< Context swapping flag */
670         __volatile__ long interrupt_flag; /**< Interruption handler flag */
671         __volatile__ long dma_flag;     /**< DMA dispatch flag */
672         struct timer_list timer;        /**< Timer for delaying ctx switch */
673         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
674         int               last_checked; /**< Last context checked for DMA */
675         int               last_context; /**< Last current context */
676         unsigned long     last_switch;  /**< jiffies at last context switch */
677         /*@}*/
678         
679         struct work_struct      work;
680         /** \name VBLANK IRQ support */
681         /*@{*/
682
683         wait_queue_head_t vbl_queue;    /**< VBLANK wait queue */
684         atomic_t          vbl_received;
685         spinlock_t        vbl_lock;
686         drm_vbl_sig_t     vbl_sigs;     /**< signal list to send on VBLANK */
687         unsigned int      vbl_pending;
688
689         /*@}*/
690         cycles_t          ctx_start;
691         cycles_t          lck_start;
692
693         char              buf[DRM_BSZ]; /**< Output buffer */
694         char              *buf_rp;      /**< Read pointer */
695         char              *buf_wp;      /**< Write pointer */
696         char              *buf_end;     /**< End pointer */
697         struct fasync_struct *buf_async;/**< Processes waiting for SIGIO */
698         wait_queue_head_t buf_readers;  /**< Processes waiting to read */
699         wait_queue_head_t buf_writers;  /**< Processes waiting to ctx switch */
700
701         drm_agp_head_t    *agp; /**< AGP data */
702
703         struct pci_dev    *pdev;        /**< PCI device structure */
704         int               pci_domain;   /**< PCI bus domain number */
705         int               pci_bus;      /**< PCI bus number */
706         int               pci_slot;     /**< PCI slot number */
707         int               pci_func;     /**< PCI function number */
708 #ifdef __alpha__
709 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,3)
710         struct pci_controler *hose;
711 #else
712         struct pci_controller *hose;
713 #endif
714 #endif
715         drm_sg_mem_t      *sg;  /**< Scatter gather memory */
716         unsigned long     *ctx_bitmap;  /**< context bitmap */
717         void              *dev_private; /**< device private data */
718         drm_sigdata_t     sigdata; /**< For block_all_signals */
719         sigset_t          sigmask;
720
721         struct            drm_driver *driver;
722         drm_local_map_t   *agp_buffer_map;
723 } drm_device_t;
724
725 typedef struct drm_minor {
726         enum {
727                 DRM_MINOR_FREE = 0,
728                 DRM_MINOR_PRIMARY,
729         } type;
730         drm_device_t *dev;
731         struct proc_dir_entry  *dev_root; /**< proc directory entry */
732 } drm_minor_t;
733
734 static __inline__ int drm_core_check_feature(struct drm_device *dev, int feature)
735 {
736         return ((dev->driver->driver_features & feature) ? 1 : 0);
737 }
738
739 #if __OS_HAS_AGP
740 static inline int drm_core_has_AGP(struct drm_device *dev)
741 {
742   return drm_core_check_feature(dev, DRIVER_USE_AGP);
743 }
744 #else
745 #define drm_core_has_AGP(dev) (0)
746 #endif
747
748 #if __OS_HAS_MTRR
749 static inline int drm_core_has_MTRR(struct drm_device *dev)
750 {
751   return drm_core_check_feature(dev, DRIVER_USE_MTRR);
752 }
753 #else
754 #define drm_core_has_MTRR(dev) (0)
755 #endif
756
757 /******************************************************************/
758 /** \name Internal function definitions */
759 /*@{*/
760
761                                 /* Misc. support (drm_init.h) */
762 extern int           drm_flags;
763 extern void          drm_parse_options( char *s );
764 extern int           drm_cpu_valid( void );
765
766                                 /* Driver support (drm_drv.h) */
767 extern int           drm_init(struct drm_driver *driver);
768 extern void          drm_exit(struct drm_driver *driver);
769 extern int           drm_version(struct inode *inode, struct file *filp,
770                                   unsigned int cmd, unsigned long arg);
771 extern int           drm_ioctl(struct inode *inode, struct file *filp,
772                                 unsigned int cmd, unsigned long arg);
773 extern int           drm_takedown(drm_device_t * dev);
774
775                                 /* Device support (drm_fops.h) */
776 extern int           drm_open(struct inode *inode, struct file *filp);
777 extern int           drm_stub_open(struct inode *inode, struct file *filp);
778 extern int           drm_open_helper(struct inode *inode, struct file *filp,
779                                       drm_device_t *dev);
780 extern int           drm_flush(struct file *filp);
781 extern int           drm_fasync(int fd, struct file *filp, int on);
782 extern int           drm_release(struct inode *inode, struct file *filp);
783
784                                 /* Mapping support (drm_vm.h) */
785 extern void          drm_vm_open(struct vm_area_struct *vma);
786 extern void          drm_vm_close(struct vm_area_struct *vma);
787 extern void          drm_vm_shm_close(struct vm_area_struct *vma);
788 extern int           drm_mmap_dma(struct file *filp,
789                                    struct vm_area_struct *vma);
790 extern int           drm_mmap(struct file *filp, struct vm_area_struct *vma);
791 extern unsigned int  drm_poll(struct file *filp, struct poll_table_struct *wait);
792 extern ssize_t       drm_read(struct file *filp, char __user *buf, size_t count, loff_t *off);
793
794                                 /* Memory management support (drm_memory.h) */
795 #include "drm_memory.h"
796 extern void          drm_mem_init(void);
797 extern int           drm_mem_info(char *buf, char **start, off_t offset,
798                                    int request, int *eof, void *data);
799 extern void          *drm_calloc(size_t nmemb, size_t size, int area);
800 extern void          *drm_realloc(void *oldpt, size_t oldsize, size_t size,
801                                    int area);
802 extern unsigned long drm_alloc_pages(int order, int area);
803 extern void          drm_free_pages(unsigned long address, int order,
804                                      int area);
805 extern void          *drm_ioremap(unsigned long offset, unsigned long size, drm_device_t *dev);
806 extern void          *drm_ioremap_nocache(unsigned long offset, unsigned long size,
807                                            drm_device_t *dev);
808 extern void          drm_ioremapfree(void *pt, unsigned long size, drm_device_t *dev);
809
810 extern DRM_AGP_MEM   *drm_alloc_agp(int pages, u32 type);
811 extern int           drm_free_agp(DRM_AGP_MEM *handle, int pages);
812 extern int           drm_bind_agp(DRM_AGP_MEM *handle, unsigned int start);
813 extern int           drm_unbind_agp(DRM_AGP_MEM *handle);
814
815                                 /* Misc. IOCTL support (drm_ioctl.h) */
816 extern int           drm_irq_by_busid(struct inode *inode, struct file *filp,
817                                        unsigned int cmd, unsigned long arg);
818 extern int           drm_getunique(struct inode *inode, struct file *filp,
819                                     unsigned int cmd, unsigned long arg);
820 extern int           drm_setunique(struct inode *inode, struct file *filp,
821                                     unsigned int cmd, unsigned long arg);
822 extern int           drm_getmap(struct inode *inode, struct file *filp,
823                                  unsigned int cmd, unsigned long arg);
824 extern int           drm_getclient(struct inode *inode, struct file *filp,
825                                     unsigned int cmd, unsigned long arg);
826 extern int           drm_getstats(struct inode *inode, struct file *filp,
827                                    unsigned int cmd, unsigned long arg);
828 extern int           drm_setversion(struct inode *inode, struct file *filp,
829                                      unsigned int cmd, unsigned long arg);
830
831                                 /* Context IOCTL support (drm_context.h) */
832 extern int           drm_resctx( struct inode *inode, struct file *filp,
833                                   unsigned int cmd, unsigned long arg );
834 extern int           drm_addctx( struct inode *inode, struct file *filp,
835                                   unsigned int cmd, unsigned long arg );
836 extern int           drm_modctx( struct inode *inode, struct file *filp,
837                                   unsigned int cmd, unsigned long arg );
838 extern int           drm_getctx( struct inode *inode, struct file *filp,
839                                   unsigned int cmd, unsigned long arg );
840 extern int           drm_switchctx( struct inode *inode, struct file *filp,
841                                      unsigned int cmd, unsigned long arg );
842 extern int           drm_newctx( struct inode *inode, struct file *filp,
843                                   unsigned int cmd, unsigned long arg );
844 extern int           drm_rmctx( struct inode *inode, struct file *filp,
845                                  unsigned int cmd, unsigned long arg );
846
847 extern int           drm_context_switch(drm_device_t *dev, int old, int new);
848 extern int           drm_context_switch_complete(drm_device_t *dev, int new);
849
850 extern int           drm_ctxbitmap_init( drm_device_t *dev );
851 extern void          drm_ctxbitmap_cleanup( drm_device_t *dev );
852 extern void          drm_ctxbitmap_free( drm_device_t *dev, int ctx_handle );
853
854 extern int           drm_setsareactx( struct inode *inode, struct file *filp,
855                                        unsigned int cmd, unsigned long arg );
856 extern int           drm_getsareactx( struct inode *inode, struct file *filp,
857                                        unsigned int cmd, unsigned long arg );
858
859                                 /* Drawable IOCTL support (drm_drawable.h) */
860 extern int           drm_adddraw(struct inode *inode, struct file *filp,
861                                   unsigned int cmd, unsigned long arg);
862 extern int           drm_rmdraw(struct inode *inode, struct file *filp,
863                                  unsigned int cmd, unsigned long arg);
864
865
866                                 /* Authentication IOCTL support (drm_auth.h) */
867 extern int           drm_add_magic(drm_device_t *dev, drm_file_t *priv,
868                                     drm_magic_t magic);
869 extern int           drm_remove_magic(drm_device_t *dev, drm_magic_t magic);
870 extern int           drm_getmagic(struct inode *inode, struct file *filp,
871                                    unsigned int cmd, unsigned long arg);
872 extern int           drm_authmagic(struct inode *inode, struct file *filp,
873                                     unsigned int cmd, unsigned long arg);
874
875                                 /* Placeholder for ioctls past */
876 extern int           drm_noop(struct inode *inode, struct file *filp,
877                                   unsigned int cmd, unsigned long arg);
878
879                                 /* Locking IOCTL support (drm_lock.h) */
880 extern int           drm_lock(struct inode *inode, struct file *filp,
881                                unsigned int cmd, unsigned long arg);
882 extern int           drm_unlock(struct inode *inode, struct file *filp,
883                                  unsigned int cmd, unsigned long arg);
884 extern int           drm_lock_take(__volatile__ unsigned int *lock,
885                                     unsigned int context);
886 extern int           drm_lock_transfer(drm_device_t *dev,
887                                         __volatile__ unsigned int *lock,
888                                         unsigned int context);
889 extern int           drm_lock_free(drm_device_t *dev,
890                                     __volatile__ unsigned int *lock,
891                                     unsigned int context);
892 extern int           drm_notifier(void *priv);
893
894                                 /* Buffer management support (drm_bufs.h) */
895 extern int           drm_order( unsigned long size );
896 extern int           drm_addmap( struct inode *inode, struct file *filp,
897                                   unsigned int cmd, unsigned long arg );
898 extern int           drm_rmmap( struct inode *inode, struct file *filp,
899                                  unsigned int cmd, unsigned long arg );
900 extern int           drm_addbufs( struct inode *inode, struct file *filp,
901                                    unsigned int cmd, unsigned long arg );
902 extern int           drm_infobufs( struct inode *inode, struct file *filp,
903                                     unsigned int cmd, unsigned long arg );
904 extern int           drm_markbufs( struct inode *inode, struct file *filp,
905                                     unsigned int cmd, unsigned long arg );
906 extern int           drm_freebufs( struct inode *inode, struct file *filp,
907                                     unsigned int cmd, unsigned long arg );
908 extern int           drm_mapbufs( struct inode *inode, struct file *filp,
909                                    unsigned int cmd, unsigned long arg );
910
911                                 /* DMA support (drm_dma.h) */
912 extern int           drm_dma_setup(drm_device_t *dev);
913 extern void          drm_dma_takedown(drm_device_t *dev);
914 extern void          drm_free_buffer(drm_device_t *dev, drm_buf_t *buf);
915 extern void          drm_core_reclaim_buffers(drm_device_t *dev, struct file *filp);
916
917                                 /* IRQ support (drm_irq.h) */
918 extern int           drm_control( struct inode *inode, struct file *filp,
919                                    unsigned int cmd, unsigned long arg );
920 extern int           drm_irq_install( drm_device_t *dev );
921 extern int           drm_irq_uninstall( drm_device_t *dev );
922 extern irqreturn_t   drm_irq_handler( DRM_IRQ_ARGS );
923 extern void          drm_driver_irq_preinstall( drm_device_t *dev );
924 extern void          drm_driver_irq_postinstall( drm_device_t *dev );
925 extern void          drm_driver_irq_uninstall( drm_device_t *dev );
926
927 extern int           drm_wait_vblank(struct inode *inode, struct file *filp,
928                                       unsigned int cmd, unsigned long arg);
929 extern int           drm_vblank_wait(drm_device_t *dev, unsigned int *vbl_seq);
930 extern void          drm_vbl_send_signals( drm_device_t *dev );
931
932                                 /* AGP/GART support (drm_agpsupport.h) */
933 extern drm_agp_head_t *drm_agp_init(void);
934 extern int            drm_agp_acquire(struct inode *inode, struct file *filp,
935                                        unsigned int cmd, unsigned long arg);
936 extern void           drm_agp_do_release(void);
937 extern int            drm_agp_release(struct inode *inode, struct file *filp,
938                                        unsigned int cmd, unsigned long arg);
939 extern int            drm_agp_enable(struct inode *inode, struct file *filp,
940                                       unsigned int cmd, unsigned long arg);
941 extern int            drm_agp_info(struct inode *inode, struct file *filp,
942                                     unsigned int cmd, unsigned long arg);
943 extern int            drm_agp_alloc(struct inode *inode, struct file *filp,
944                                      unsigned int cmd, unsigned long arg);
945 extern int            drm_agp_free(struct inode *inode, struct file *filp,
946                                     unsigned int cmd, unsigned long arg);
947 extern int            drm_agp_unbind(struct inode *inode, struct file *filp,
948                                       unsigned int cmd, unsigned long arg);
949 extern int            drm_agp_bind(struct inode *inode, struct file *filp,
950                                     unsigned int cmd, unsigned long arg);
951 extern DRM_AGP_MEM    *drm_agp_allocate_memory(size_t pages, u32 type);
952 extern int            drm_agp_free_memory(DRM_AGP_MEM *handle);
953 extern int            drm_agp_bind_memory(DRM_AGP_MEM *handle, off_t start);
954 extern int            drm_agp_unbind_memory(DRM_AGP_MEM *handle);
955
956                                 /* Stub support (drm_stub.h) */
957 extern int            drm_probe(struct pci_dev *pdev, const struct pci_device_id *ent, struct drm_driver *driver);
958
959 extern int            drm_put_minor(drm_device_t *dev);
960 extern unsigned int   drm_debug;
961 extern unsigned int   drm_cards_limit;
962 extern drm_minor_t    *drm_minors;
963 extern struct drm_sysfs_class *drm_class;
964 extern struct proc_dir_entry *drm_proc_root;
965
966                                 /* Proc support (drm_proc.h) */
967 extern int            drm_proc_init(drm_device_t *dev,
968                                              int minor,
969                                              struct proc_dir_entry *root,
970                                              struct proc_dir_entry **dev_root);
971 extern int            drm_proc_cleanup(int minor,
972                                         struct proc_dir_entry *root,
973                                         struct proc_dir_entry *dev_root);
974
975                                 /* Scatter Gather Support (drm_scatter.h) */
976 extern void           drm_sg_cleanup(drm_sg_mem_t *entry);
977 extern int            drm_sg_alloc(struct inode *inode, struct file *filp,
978                                     unsigned int cmd, unsigned long arg);
979 extern int            drm_sg_free(struct inode *inode, struct file *filp,
980                                    unsigned int cmd, unsigned long arg);
981
982                                /* ATI PCIGART support (ati_pcigart.h) */
983 extern int            drm_ati_pcigart_init(drm_device_t *dev,
984                                             unsigned long *addr,
985                                             dma_addr_t *bus_addr);
986 extern int            drm_ati_pcigart_cleanup(drm_device_t *dev,
987                                                unsigned long addr,
988                                                dma_addr_t bus_addr);
989
990 extern void *drm_pci_alloc(drm_device_t * dev, size_t size,
991                            size_t align, dma_addr_t maxaddr,
992                            dma_addr_t * busaddr);
993
994 extern void drm_pci_free(drm_device_t * dev, size_t size,
995                          void *vaddr, dma_addr_t busaddr);
996
997                                /* sysfs support (drm_sysfs.c) */
998 struct drm_sysfs_class;
999 extern struct drm_sysfs_class *drm_sysfs_create(struct module *owner,
1000                                                 char *name);
1001 extern void drm_sysfs_destroy(struct drm_sysfs_class *cs);
1002 extern struct class_device *drm_sysfs_device_add(struct drm_sysfs_class *cs,
1003                                                  dev_t dev,
1004                                                  struct device *device,
1005                                                  const char *fmt, ...);
1006 extern void drm_sysfs_device_remove(dev_t dev);
1007
1008
1009 /* Inline replacements for DRM_IOREMAP macros */
1010 static __inline__ void drm_core_ioremap(struct drm_map *map, struct drm_device *dev)
1011 {
1012         map->handle = drm_ioremap( map->offset, map->size, dev );
1013 }
1014
1015 static __inline__ void drm_core_ioremap_nocache(struct drm_map *map, struct drm_device *dev)
1016 {
1017         map->handle = drm_ioremap_nocache(map->offset, map->size, dev);
1018 }
1019
1020 static __inline__ void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev)
1021 {
1022         if ( map->handle && map->size )
1023                 drm_ioremapfree( map->handle, map->size, dev );
1024 }
1025
1026 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev, unsigned long offset)
1027 {
1028         struct list_head *_list;
1029         list_for_each( _list, &dev->maplist->head ) {
1030                 drm_map_list_t *_entry = list_entry( _list, drm_map_list_t, head );
1031                 if ( _entry->map &&
1032                      _entry->map->offset == offset ) {
1033                         return _entry->map;
1034                 }
1035         }
1036         return NULL;
1037 }
1038
1039 static __inline__ void drm_core_dropmap(struct drm_map *map)
1040 {
1041 }
1042
1043 #ifndef DEBUG_MEMORY
1044 /** Wrapper around kmalloc() */
1045 static __inline__ void *drm_alloc(size_t size, int area)
1046 {
1047         return kmalloc(size, GFP_KERNEL);
1048 }
1049
1050 /** Wrapper around kfree() */
1051 static __inline__ void drm_free(void *pt, size_t size, int area)
1052 {
1053         kfree(pt);
1054 }
1055 #else
1056 extern void *drm_alloc(size_t size, int area);
1057 extern void drm_free(void *pt, size_t size, int area);
1058 #endif
1059
1060 /*@}*/
1061
1062 extern unsigned long drm_core_get_map_ofs(drm_map_t *map);
1063 extern unsigned long drm_core_get_reg_ofs(struct drm_device *dev);
1064
1065 #endif /* __KERNEL__ */
1066 #endif