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