OSDN Git Service

Make it compile again.
[android-x86/external-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
409 enum drm_ref_type {
410         _DRM_REF_USE = 0,
411         _DRM_REF_TYPE1,
412         _DRM_NO_REF_TYPES
413 };
414
415
416 /** File private data */
417 struct drm_file {
418         int authenticated;
419         pid_t pid;
420         uid_t uid;
421         drm_magic_t magic;
422         unsigned long ioctl_count;
423         struct list_head lhead;
424         struct drm_minor *minor;
425         unsigned long lock_count;
426
427         /*
428          * The user object hash table is global and resides in the
429          * drm_device structure. We protect the lists and hash tables with the
430          * device struct_mutex. A bit coarse-grained but probably the best
431          * option.
432          */
433
434         struct list_head refd_objects;
435
436         /** Mapping of mm object handles to object pointers. */
437         struct idr object_idr;
438         /** Lock for synchronization of access to object_idr. */
439         spinlock_t table_lock;
440
441         struct drm_open_hash refd_object_hash[_DRM_NO_REF_TYPES];
442         struct file *filp;
443         void *driver_priv;
444
445         int is_master; /* this file private is a master for a minor */
446         struct drm_master *master; /* master this node is currently associated with
447                                       N.B. not always minor->master */
448         struct list_head fbs;
449 };
450
451 /** Wait queue */
452 struct drm_queue {
453         atomic_t use_count;             /**< Outstanding uses (+1) */
454         atomic_t finalization;          /**< Finalization in progress */
455         atomic_t block_count;           /**< Count of processes waiting */
456         atomic_t block_read;            /**< Queue blocked for reads */
457         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
458         atomic_t block_write;           /**< Queue blocked for writes */
459         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
460 #if 1
461         atomic_t total_queued;          /**< Total queued statistic */
462         atomic_t total_flushed;         /**< Total flushes statistic */
463         atomic_t total_locks;           /**< Total locks statistics */
464 #endif
465         enum drm_ctx_flags flags;       /**< Context preserving and 2D-only */
466         struct drm_waitlist waitlist;   /**< Pending buffers */
467         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
468 };
469
470 /**
471  * Lock data.
472  */
473 struct drm_lock_data {
474         struct drm_hw_lock *hw_lock;            /**< Hardware lock */
475         /** Private of lock holder's file (NULL=kernel) */
476         struct drm_file *file_priv;
477         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
478         unsigned long lock_time;        /**< Time of last lock in jiffies */
479         spinlock_t spinlock;
480         uint32_t kernel_waiters;
481         uint32_t user_waiters;
482         int idle_has_lock;
483         /**
484          * Boolean signaling that the lock is held on behalf of the
485          * file_priv client by the kernel in an ioctl handler.
486          */
487         int kernel_held;
488 };
489
490 /**
491  * DMA data.
492  */
493 struct drm_device_dma {
494
495         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
496         int buf_count;                  /**< total number of buffers */
497         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
498         int seg_count;
499         int page_count;                 /**< number of pages */
500         unsigned long *pagelist;        /**< page list */
501         unsigned long byte_count;
502         enum {
503                 _DRM_DMA_USE_AGP = 0x01,
504                 _DRM_DMA_USE_SG = 0x02,
505                 _DRM_DMA_USE_FB = 0x04,
506                 _DRM_DMA_USE_PCI_RO = 0x08
507         } flags;
508
509 };
510
511 /**
512  * AGP memory entry.  Stored as a doubly linked list.
513  */
514 struct drm_agp_mem {
515         unsigned long handle;           /**< handle */
516         DRM_AGP_MEM *memory;
517         unsigned long bound;            /**< address */
518         int pages;
519         struct list_head head;
520 };
521
522 /**
523  * AGP data.
524  *
525  * \sa drm_agp_init() and drm_device::agp.
526  */
527 struct drm_agp_head {
528         DRM_AGP_KERN agp_info;          /**< AGP device information */
529         struct list_head memory;
530         unsigned long mode;             /**< AGP mode */
531 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11)
532         struct agp_bridge_data *bridge;
533 #endif
534         int enabled;                    /**< whether the AGP bus as been enabled */
535         int acquired;                   /**< whether the AGP device has been acquired */
536         unsigned long base;
537         int agp_mtrr;
538         int cant_use_aperture;
539         unsigned long page_mask;
540 };
541
542 /**
543  * Scatter-gather memory.
544  */
545 struct drm_sg_mem {
546         unsigned long handle;
547         void *virtual;
548         int pages;
549         struct page **pagelist;
550         dma_addr_t *busaddr;
551 };
552
553 struct drm_sigdata {
554         int context;
555         struct drm_hw_lock *lock;
556 };
557
558
559 /*
560  * Generic memory manager structs
561  */
562
563 struct drm_memrange_node {
564         struct list_head fl_entry;
565         struct list_head ml_entry;
566         int free;
567         unsigned long start;
568         unsigned long size;
569         struct drm_memrange *mm;
570         void *private;
571 };
572
573 struct drm_memrange {
574         struct list_head fl_entry;
575         struct list_head ml_entry;
576 };
577
578
579 /**
580  * Mappings list
581  */
582 struct drm_map_list {
583         struct list_head head;          /**< list head */
584         struct drm_hash_item hash;
585         struct drm_map *map;                    /**< mapping */
586         uint64_t user_token;
587         struct drm_master *master; /** if this map is associated with a specific
588                                        master */
589         struct drm_memrange_node *file_offset_node;
590 };
591
592 typedef struct drm_map drm_local_map_t;
593
594 /**
595  * Context handle list
596  */
597 struct drm_ctx_list {
598         struct list_head head;          /**< list head */
599         drm_context_t handle;           /**< context handle */
600         struct drm_file *tag;           /**< associated fd private data */
601 };
602
603 struct drm_vbl_sig {
604         struct list_head head;
605         unsigned int sequence;
606         struct siginfo info;
607         struct task_struct *task;
608 };
609
610 struct drm_hotplug_sig {
611         struct list_head head;
612         unsigned int counter;
613         struct siginfo info;
614         struct task_struct *task;
615 };
616
617 /* location of GART table */
618 #define DRM_ATI_GART_MAIN 1
619 #define DRM_ATI_GART_FB   2
620
621 #define DRM_ATI_GART_PCI 1
622 #define DRM_ATI_GART_PCIE 2
623 #define DRM_ATI_GART_IGP 3
624
625 struct drm_ati_pcigart_info {
626         int gart_table_location;
627         int gart_reg_if;
628         void *addr;
629         dma_addr_t bus_addr;
630         dma_addr_t table_mask;
631         dma_addr_t member_mask;
632         struct drm_dma_handle *table_handle;
633         drm_local_map_t mapping;
634         int table_size;
635 };
636
637 /**
638  * This structure defines the drm_mm memory object, which will be used by the
639  * DRM for its buffer objects.
640  */
641 struct drm_gem_object {
642         /** Reference count of this object */
643         struct kref refcount;
644
645         /** Handle count of this object. Each handle also holds a reference */
646         struct kref handlecount;
647
648         /** Related drm device */
649         struct drm_device *dev;
650         
651         /** File representing the shmem storage */
652         struct file *filp;
653
654         /**
655          * Size of the object, in bytes.  Immutable over the object's
656          * lifetime.
657          */
658         size_t size;
659
660         /**
661          * Global name for this object, starts at 1. 0 means unnamed.
662          * Access is covered by the object_name_lock in the related drm_device
663          */
664         int name;
665
666         /**
667          * Memory domains. These monitor which caches contain read/write data
668          * related to the object. When transitioning from one set of domains
669          * to another, the driver is called to ensure that caches are suitably
670          * flushed and invalidated
671          */
672         uint32_t        read_domains;
673         uint32_t        write_domain;
674
675         /**
676          * While validating an exec operation, the
677          * new read/write domain values are computed here.
678          * They will be transferred to the above values
679          * at the point that any cache flushing occurs
680          */
681         uint32_t        pending_read_domains;
682         uint32_t        pending_write_domain;
683
684         void *driver_private;
685 };
686
687 #include "drm_objects.h"
688 #include "drm_crtc.h"
689
690 /* per-master structure */
691 struct drm_master {
692         
693         struct list_head head; /**< each minor contains a list of masters */
694         struct drm_minor *minor; /**< link back to minor we are a master for */
695
696         char *unique;                   /**< Unique identifier: e.g., busid */
697         int unique_len;                 /**< Length of unique field */
698
699         int blocked;                    /**< Blocked due to VC switch? */
700
701         /** \name Authentication */
702         /*@{ */
703         struct drm_open_hash magiclist;
704         struct list_head magicfree;
705         /*@} */
706
707         struct drm_lock_data lock;              /**< Information on hardware lock */
708
709         void *driver_priv; /**< Private structure for driver to use */
710 };
711
712 /**
713  * DRM driver structure. This structure represent the common code for
714  * a family of cards. There will one drm_device for each card present
715  * in this family
716  */
717
718 struct drm_driver {
719         int (*load) (struct drm_device *, unsigned long flags);
720         int (*firstopen) (struct drm_device *);
721         int (*open) (struct drm_device *, struct drm_file *);
722         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
723         void (*postclose) (struct drm_device *, struct drm_file *);
724         void (*lastclose) (struct drm_device *);
725         int (*unload) (struct drm_device *);
726         int (*suspend) (struct drm_device *, pm_message_t state);
727         int (*resume) (struct drm_device *);
728         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
729         void (*dma_ready) (struct drm_device *);
730         int (*dma_quiescent) (struct drm_device *);
731         int (*context_ctor) (struct drm_device *dev, int context);
732         int (*context_dtor) (struct drm_device *dev, int context);
733         int (*kernel_context_switch) (struct drm_device *dev, int old,
734                                       int new);
735         void (*kernel_context_switch_unlock) (struct drm_device * dev);
736         /**
737          * get_vblank_counter - get raw hardware vblank counter
738          * @dev: DRM device
739          * @crtc: counter to fetch
740          *
741          * Driver callback for fetching a raw hardware vblank counter
742          * for @crtc.  If a device doesn't have a hardware counter, the
743          * driver can simply return the value of drm_vblank_count and
744          * make the enable_vblank() and disable_vblank() hooks into no-ops,
745          * leaving interrupts enabled at all times.
746          *
747          * Wraparound handling and loss of events due to modesetting is dealt
748          * with in the DRM core code.
749          *
750          * RETURNS
751          * Raw vblank counter value.
752          */
753         u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
754
755         /**
756          * enable_vblank - enable vblank interrupt events
757          * @dev: DRM device
758          * @crtc: which irq to enable
759          *
760          * Enable vblank interrupts for @crtc.  If the device doesn't have
761          * a hardware vblank counter, this routine should be a no-op, since
762          * interrupts will have to stay on to keep the count accurate.
763          *
764          * RETURNS
765          * Zero on success, appropriate errno if the given @crtc's vblank
766          * interrupt cannot be enabled.
767          */
768         int (*enable_vblank) (struct drm_device *dev, int crtc);
769
770         /**
771          * disable_vblank - disable vblank interrupt events
772          * @dev: DRM device
773          * @crtc: which irq to enable
774          *
775          * Disable vblank interrupts for @crtc.  If the device doesn't have
776          * a hardware vblank counter, this routine should be a no-op, since
777          * interrupts will have to stay on to keep the count accurate.
778          */
779         void (*disable_vblank) (struct drm_device *dev, int crtc);
780         int (*dri_library_name) (struct drm_device *dev, char * buf);
781
782         /**
783          * Called by \c drm_device_is_agp.  Typically used to determine if a
784          * card is really attached to AGP or not.
785          *
786          * \param dev  DRM device handle
787          *
788          * \returns
789          * One of three values is returned depending on whether or not the
790          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
791          * (return of 1), or may or may not be AGP (return of 2).
792          */
793         int (*device_is_agp) (struct drm_device *dev);
794
795 /* these have to be filled in */
796          irqreturn_t(*irq_handler) (DRM_IRQ_ARGS);
797         void (*irq_preinstall) (struct drm_device *dev);
798         int (*irq_postinstall) (struct drm_device *dev);
799         void (*irq_uninstall) (struct drm_device *dev);
800         void (*reclaim_buffers) (struct drm_device *dev,
801                                  struct drm_file *file_priv);
802         void (*reclaim_buffers_locked) (struct drm_device *dev,
803                                         struct drm_file *file_priv);
804         void (*reclaim_buffers_idlelocked) (struct drm_device *dev,
805                                             struct drm_file *file_priv);
806         unsigned long (*get_map_ofs) (struct drm_map *map);
807         unsigned long (*get_reg_ofs) (struct drm_device *dev);
808         void (*set_version) (struct drm_device *dev,
809                              struct drm_set_version *sv);
810
811         /* Master routines */
812         int (*master_create)(struct drm_device *dev, struct drm_master *master);
813         void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
814
815         int (*proc_init)(struct drm_minor *minor);
816         void (*proc_cleanup)(struct drm_minor *minor);
817
818         /**
819          * Driver-specific constructor for drm_gem_objects, to set up
820          * obj->driver_private.
821          *
822          * Returns 0 on success.
823          */
824         int (*gem_init_object) (struct drm_gem_object *obj);
825         void (*gem_free_object) (struct drm_gem_object *obj);
826
827         struct drm_fence_driver *fence_driver;
828         struct drm_bo_driver *bo_driver;
829
830         int major;
831         int minor;
832         int patchlevel;
833         char *name;
834         char *desc;
835         char *date;
836
837 /* variables */
838         u32 driver_features;
839         int dev_priv_size;
840         struct drm_ioctl_desc *ioctls;
841         int num_ioctls;
842         struct file_operations fops;
843         struct pci_driver pci_driver;
844 };
845
846 #define DRM_MINOR_UNASSIGNED 0
847 #define DRM_MINOR_LEGACY 1
848 #define DRM_MINOR_CONTROL 2
849 #define DRM_MINOR_RENDER 3
850
851 /**
852  * DRM minor structure. This structure represents a drm minor number.
853  */
854 struct drm_minor {
855         int index;                      /**< Minor device number */
856         int type;                       /**< Control or render */
857         dev_t device;                   /**< Device number for mknod */
858         struct device kdev;             /**< Linux device */
859         struct drm_device *dev;
860         /* for render nodes */
861         struct proc_dir_entry *dev_root;  /**< proc directory entry */
862         struct class_device *dev_class;
863
864         /* for control nodes - a pointer to the current master for this control node */
865         struct drm_master *master; /* currently active master for this node */
866         struct list_head master_list;
867
868         struct drm_mode_group mode_group;
869 };
870
871
872 /**
873  * DRM device structure. This structure represent a complete card that
874  * may contain multiple heads.
875  */
876 struct drm_device {
877         char *devname;                  /**< For /proc/interrupts */
878         int if_version;                 /**< Highest interface version set */
879
880         /** \name Locks */
881         /*@{ */
882         spinlock_t count_lock;          /**< For inuse, drm_device::open_count, drm_device::buf_use */
883         struct mutex struct_mutex;      /**< For others */
884         /*@} */
885
886         /** \name Usage Counters */
887         /*@{ */
888         int open_count;                 /**< Outstanding files open */
889         atomic_t ioctl_count;           /**< Outstanding IOCTLs pending */
890         atomic_t vma_count;             /**< Outstanding vma areas open */
891         int buf_use;                    /**< Buffers in use -- cannot alloc */
892         atomic_t buf_alloc;             /**< Buffer allocation in progress */
893         /*@} */
894
895         /** \name Performance counters */
896         /*@{ */
897         unsigned long counters;
898         enum drm_stat_type types[15];
899         atomic_t counts[15];
900         /*@} */
901
902
903         /** \name Memory management */
904         /*@{ */
905         struct list_head maplist;       /**< Linked list of regions */
906         int map_count;                  /**< Number of mappable regions */
907         struct drm_open_hash map_hash;       /**< User token hash table for maps */
908         struct drm_memrange offset_manager;  /**< User token manager */
909         struct drm_open_hash object_hash;    /**< User token hash table for objects */
910         struct address_space *dev_mapping;  /**< For unmap_mapping_range() */
911         struct page *ttm_dummy_page;
912
913         /** \name Context handle management */
914         /*@{ */
915         struct list_head ctxlist;       /**< Linked list of context handles */
916         int ctx_count;                  /**< Number of context handles */
917         struct mutex ctxlist_mutex;     /**< For ctxlist */
918
919         struct idr ctx_idr;
920
921         struct list_head vmalist;       /**< List of vmas (for debugging) */
922
923         struct list_head filelist;
924
925         /*@} */
926
927         /** \name DMA queues (contexts) */
928         /*@{ */
929         int queue_count;                /**< Number of active DMA queues */
930         int queue_reserved;             /**< Number of reserved DMA queues */
931         int queue_slots;                /**< Actual length of queuelist */
932         struct drm_queue **queuelist;   /**< Vector of pointers to DMA queues */
933         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
934         /*@} */
935
936
937         /** \name Context support */
938         /*@{ */
939         int irq;                        /**< Interrupt used by board */
940         int irq_enabled;                /**< True if irq handler is enabled */
941         __volatile__ long context_flag; /**< Context swapping flag */
942         __volatile__ long interrupt_flag; /**< Interruption handler flag */
943         __volatile__ long dma_flag;     /**< DMA dispatch flag */
944         struct timer_list timer;        /**< Timer for delaying ctx switch */
945         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
946         int last_checked;               /**< Last context checked for DMA */
947         int last_context;               /**< Last current context */
948         unsigned long last_switch;      /**< jiffies at last context switch */
949         /*@} */
950
951         struct work_struct work;
952
953         /** \name HOTPLUG IRQ support */
954         /*@{ */
955         wait_queue_head_t hotplug_queue;        /**< HOTPLUG wait queue */
956         spinlock_t hotplug_lock;
957         struct list_head *hotplug_sigs;         /**< signal list to send on HOTPLUG */
958         atomic_t hotplug_signal_pending;        /* number of signals pending on all crtcs*/
959
960         /*@} */
961
962         /** \name VBLANK IRQ support */
963         /*@{ */
964
965         wait_queue_head_t *vbl_queue;   /**< VBLANK wait queue */
966         atomic_t *_vblank_count;        /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
967         spinlock_t vbl_lock;
968         struct list_head *vbl_sigs;             /**< signal list to send on VBLANK */
969         atomic_t vbl_signal_pending;    /* number of signals pending on all crtcs*/
970         atomic_t *vblank_refcount;      /* number of users of vblank interrupts per crtc */
971         u32 *last_vblank;               /* protected by dev->vbl_lock, used */
972                                         /* for wraparound handling */
973         u32 *vblank_offset;             /* used to track how many vblanks */
974         int *vblank_enabled;            /* so we don't call enable more than
975                                            once per disable */
976         u32 *vblank_premodeset;         /*  were lost during modeset */
977         struct timer_list vblank_disable_timer;
978
979         unsigned long max_vblank_count; /**< size of vblank counter register */
980         spinlock_t tasklet_lock;        /**< For drm_locked_tasklet */
981         void (*locked_tasklet_func)(struct drm_device *dev);
982
983         /*@} */
984         cycles_t ctx_start;
985         cycles_t lck_start;
986
987         struct fasync_struct *buf_async;/**< Processes waiting for SIGIO */
988         wait_queue_head_t buf_readers;  /**< Processes waiting to read */
989         wait_queue_head_t buf_writers;  /**< Processes waiting to ctx switch */
990
991         struct drm_agp_head *agp;               /**< AGP data */
992
993         struct pci_dev *pdev;           /**< PCI device structure */
994         int pci_vendor;                 /**< PCI vendor id */
995         int pci_device;                 /**< PCI device id */
996 #ifdef __alpha__
997         struct pci_controller *hose;
998 #endif
999         int num_crtcs;                  /**< Number of CRTCs on this device */
1000         struct drm_sg_mem *sg;          /**< Scatter gather memory */
1001         void *dev_private;              /**< device private data */
1002         struct drm_sigdata sigdata;             /**< For block_all_signals */
1003         sigset_t sigmask;
1004
1005         struct drm_driver *driver;
1006         drm_local_map_t *agp_buffer_map;
1007         unsigned int agp_buffer_token;
1008
1009         /* minor number for control node */
1010         struct drm_minor *control;
1011         struct drm_minor *primary;              /**< render type primary screen head */
1012
1013         struct drm_fence_manager fm;
1014         struct drm_buffer_manager bm;
1015
1016         /** \name Drawable information */
1017         /*@{ */
1018         spinlock_t drw_lock;
1019         struct idr drw_idr;
1020         /*@} */
1021
1022         /* DRM mode setting */
1023         struct drm_mode_config mode_config;
1024
1025         /** \name GEM information */
1026         /*@{ */
1027         spinlock_t object_name_lock;
1028         struct idr object_name_idr;
1029         atomic_t object_count;
1030         atomic_t object_memory;
1031         atomic_t pin_count;
1032         atomic_t pin_memory;
1033         atomic_t gtt_count;
1034         atomic_t gtt_memory;
1035         uint32_t gtt_total;
1036         uint32_t invalidate_domains;    /* domains pending invalidation */
1037         uint32_t flush_domains;         /* domains pending flush */
1038         /*@} */
1039 };
1040
1041 #if __OS_HAS_AGP
1042 struct drm_agp_ttm_backend {
1043         struct drm_ttm_backend backend;
1044         DRM_AGP_MEM *mem;
1045         struct agp_bridge_data *bridge;
1046         int populated;
1047 };
1048 #endif
1049 struct ati_pcigart_ttm_backend {
1050         struct drm_ttm_backend backend;
1051         int populated;
1052         void (*gart_flush_fn)(struct drm_device *dev);
1053         struct drm_ati_pcigart_info *gart_info;
1054         unsigned long offset;
1055         struct page **pages;
1056         int num_pages;
1057         int bound;
1058         struct drm_device *dev;
1059 };
1060 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));
1061
1062 static __inline__ int drm_core_check_feature(struct drm_device *dev,
1063                                              int feature)
1064 {
1065         return ((dev->driver->driver_features & feature) ? 1 : 0);
1066 }
1067
1068 #ifdef __alpha__
1069 #define drm_get_pci_domain(dev) dev->hose->index
1070 #else
1071 #define drm_get_pci_domain(dev) 0
1072 #endif
1073
1074 #if __OS_HAS_AGP
1075 static inline int drm_core_has_AGP(struct drm_device *dev)
1076 {
1077         return drm_core_check_feature(dev, DRIVER_USE_AGP);
1078 }
1079 #else
1080 #define drm_core_has_AGP(dev) (0)
1081 #endif
1082
1083 #if __OS_HAS_MTRR
1084 static inline int drm_core_has_MTRR(struct drm_device *dev)
1085 {
1086         return drm_core_check_feature(dev, DRIVER_USE_MTRR);
1087 }
1088
1089 #define DRM_MTRR_WC             MTRR_TYPE_WRCOMB
1090
1091 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
1092                                unsigned int flags)
1093 {
1094         return mtrr_add(offset, size, flags, 1);
1095 }
1096
1097 static inline int drm_mtrr_del(int handle, unsigned long offset,
1098                                unsigned long size, unsigned int flags)
1099 {
1100         return mtrr_del(handle, offset, size);
1101 }
1102
1103 #else
1104 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
1105                                unsigned int flags)
1106 {
1107         return -ENODEV;
1108 }
1109
1110 static inline int drm_mtrr_del(int handle, unsigned long offset,
1111                                unsigned long size, unsigned int flags)
1112 {
1113         return -ENODEV;
1114 }
1115
1116 #define drm_core_has_MTRR(dev) (0)
1117 #define DRM_MTRR_WC             0
1118 #endif
1119
1120
1121 /******************************************************************/
1122 /** \name Internal function definitions */
1123 /*@{*/
1124
1125                                 /* Driver support (drm_drv.h) */
1126 extern int drm_fb_loaded;
1127 extern int drm_init(struct drm_driver *driver,
1128                               struct pci_device_id *pciidlist);
1129 extern void drm_exit(struct drm_driver *driver);
1130 extern void drm_cleanup_pci(struct pci_dev *pdev);
1131 extern int drm_ioctl(struct inode *inode, struct file *filp,
1132                      unsigned int cmd, unsigned long arg);
1133 extern long drm_unlocked_ioctl(struct file *filp,
1134                                unsigned int cmd, unsigned long arg);
1135 extern long drm_compat_ioctl(struct file *filp,
1136                              unsigned int cmd, unsigned long arg);
1137
1138 extern int drm_lastclose(struct drm_device *dev);
1139
1140                                 /* Device support (drm_fops.h) */
1141 extern int drm_open(struct inode *inode, struct file *filp);
1142 extern int drm_stub_open(struct inode *inode, struct file *filp);
1143 extern int drm_fasync(int fd, struct file *filp, int on);
1144 extern int drm_release(struct inode *inode, struct file *filp);
1145 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
1146
1147                                 /* Mapping support (drm_vm.h) */
1148 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
1149 extern unsigned long drm_core_get_map_ofs(struct drm_map * map);
1150 extern unsigned long drm_core_get_reg_ofs(struct drm_device *dev);
1151 extern pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma);
1152
1153                                 /* Memory management support (drm_memory.h) */
1154 #include "drm_memory.h"
1155 extern void drm_mem_init(void);
1156 extern int drm_mem_info(char *buf, char **start, off_t offset,
1157                         int request, int *eof, void *data);
1158 extern void *drm_calloc(size_t nmemb, size_t size, int area);
1159 extern void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area);
1160 extern unsigned long drm_alloc_pages(int order, int area);
1161 extern void drm_free_pages(unsigned long address, int order, int area);
1162 extern DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type);
1163 extern int drm_free_agp(DRM_AGP_MEM * handle, int pages);
1164 extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start);
1165 extern DRM_AGP_MEM *drm_agp_bind_pages(struct drm_device *dev,
1166                                               struct page **pages,
1167                                               unsigned long num_pages,
1168                                               uint32_t gtt_offset);
1169 extern int drm_unbind_agp(DRM_AGP_MEM * handle);
1170
1171 extern void drm_free_memctl(size_t size);
1172 extern int drm_alloc_memctl(size_t size);
1173 extern void drm_query_memctl(uint64_t *cur_used,
1174                              uint64_t *emer_used,
1175                              uint64_t *low_threshold,
1176                              uint64_t *high_threshold,
1177                              uint64_t *emer_threshold);
1178 extern void drm_init_memctl(size_t low_threshold,
1179                             size_t high_threshold,
1180                             size_t unit_size);
1181
1182                                 /* Misc. IOCTL support (drm_ioctl.h) */
1183 extern int drm_irq_by_busid(struct drm_device *dev, void *data,
1184                             struct drm_file *file_priv);
1185 extern int drm_getunique(struct drm_device *dev, void *data,
1186                          struct drm_file *file_priv);
1187 extern int drm_setunique(struct drm_device *dev, void *data,
1188                          struct drm_file *file_priv);
1189 extern int drm_getmap(struct drm_device *dev, void *data,
1190                       struct drm_file *file_priv);
1191 extern int drm_getclient(struct drm_device *dev, void *data,
1192                          struct drm_file *file_priv);
1193 extern int drm_getstats(struct drm_device *dev, void *data,
1194                         struct drm_file *file_priv);
1195 extern int drm_setversion(struct drm_device *dev, void *data,
1196                           struct drm_file *file_priv);
1197 extern int drm_noop(struct drm_device *dev, void *data,
1198                     struct drm_file *file_priv);
1199
1200                                 /* Context IOCTL support (drm_context.h) */
1201 extern int drm_resctx(struct drm_device *dev, void *data,
1202                       struct drm_file *file_priv);
1203 extern int drm_addctx(struct drm_device *dev, void *data,
1204                       struct drm_file *file_priv);
1205 extern int drm_modctx(struct drm_device *dev, void *data,
1206                       struct drm_file *file_priv);
1207 extern int drm_getctx(struct drm_device *dev, void *data,
1208                       struct drm_file *file_priv);
1209 extern int drm_switchctx(struct drm_device *dev, void *data,
1210                          struct drm_file *file_priv);
1211 extern int drm_newctx(struct drm_device *dev, void *data,
1212                       struct drm_file *file_priv);
1213 extern int drm_rmctx(struct drm_device *dev, void *data,
1214                      struct drm_file *file_priv);
1215
1216 extern int drm_ctxbitmap_init(struct drm_device *dev);
1217 extern void drm_ctxbitmap_cleanup(struct drm_device *dev);
1218 extern void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1219
1220 extern int drm_setsareactx(struct drm_device *dev, void *data,
1221                            struct drm_file *file_priv);
1222 extern int drm_getsareactx(struct drm_device *dev, void *data,
1223                            struct drm_file *file_priv);
1224
1225                                 /* Drawable IOCTL support (drm_drawable.h) */
1226 extern int drm_adddraw(struct drm_device *dev, void *data,
1227                        struct drm_file *file_priv);
1228 extern int drm_rmdraw(struct drm_device *dev, void *data,
1229                       struct drm_file *file_priv);
1230 extern int drm_update_drawable_info(struct drm_device *dev, void *data,
1231                                     struct drm_file *file_priv);
1232 extern struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1233                                                        drm_drawable_t id);
1234 extern void drm_drawable_free_all(struct drm_device *dev);
1235
1236                                 /* Authentication IOCTL support (drm_auth.h) */
1237 extern int drm_getmagic(struct drm_device *dev, void *data,
1238                         struct drm_file *file_priv);
1239 extern int drm_authmagic(struct drm_device *dev, void *data,
1240                          struct drm_file *file_priv);
1241
1242                                 /* Locking IOCTL support (drm_lock.h) */
1243 extern int drm_lock(struct drm_device *dev, void *data,
1244                     struct drm_file *file_priv);
1245 extern int drm_unlock(struct drm_device *dev, void *data,
1246                       struct drm_file *file_priv);
1247 extern int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
1248 extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context);
1249 extern void drm_idlelock_take(struct drm_lock_data *lock_data);
1250 extern void drm_idlelock_release(struct drm_lock_data *lock_data);
1251
1252 /*
1253  * These are exported to drivers so that they can implement fencing using
1254  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1255  */
1256
1257 extern int drm_i_have_hw_lock(struct drm_device *dev,
1258                               struct drm_file *file_priv);
1259
1260                                 /* Buffer management support (drm_bufs.h) */
1261 extern int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc * request);
1262 extern int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc * request);
1263 extern int drm_addbufs_fb (struct drm_device *dev, struct drm_buf_desc * request);
1264 extern int drm_addmap(struct drm_device *dev, unsigned int offset,
1265                       unsigned int size, enum drm_map_type type,
1266                       enum drm_map_flags flags, drm_local_map_t ** map_ptr);
1267 extern int drm_addmap_ioctl(struct drm_device *dev, void *data,
1268                             struct drm_file *file_priv);
1269 extern int drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1270 extern int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map);
1271 extern int drm_rmmap_ioctl(struct drm_device *dev, void *data,
1272                            struct drm_file *file_priv);
1273 extern int drm_addbufs(struct drm_device *dev, void *data,
1274                        struct drm_file *file_priv);
1275 extern int drm_infobufs(struct drm_device *dev, void *data,
1276                         struct drm_file *file_priv);
1277 extern int drm_markbufs(struct drm_device *dev, void *data,
1278                         struct drm_file *file_priv);
1279 extern int drm_freebufs(struct drm_device *dev, void *data,
1280                         struct drm_file *file_priv);
1281 extern int drm_mapbufs(struct drm_device *dev, void *data,
1282                        struct drm_file *file_priv);
1283 extern int drm_order(unsigned long size);
1284 extern unsigned long drm_get_resource_start(struct drm_device *dev,
1285                                             unsigned int resource);
1286 extern unsigned long drm_get_resource_len(struct drm_device *dev,
1287                                           unsigned int resource);
1288 extern struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
1289                                                   drm_local_map_t *map);
1290
1291
1292                                 /* DMA support (drm_dma.h) */
1293 extern int drm_dma_setup(struct drm_device *dev);
1294 extern void drm_dma_takedown(struct drm_device *dev);
1295 extern void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf);
1296 extern void drm_core_reclaim_buffers(struct drm_device *dev,
1297                                      struct drm_file *filp);
1298
1299                                 /* IRQ support (drm_irq.h) */
1300 extern int drm_control(struct drm_device *dev, void *data,
1301                        struct drm_file *file_priv);
1302 extern irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1303 extern int drm_irq_install(struct drm_device *dev);
1304 extern int drm_irq_uninstall(struct drm_device *dev);
1305 extern void drm_driver_irq_preinstall(struct drm_device *dev);
1306 extern void drm_driver_irq_postinstall(struct drm_device *dev);
1307 extern void drm_driver_irq_uninstall(struct drm_device *dev);
1308
1309 extern int drm_hotplug_init(struct drm_device *dev);
1310 extern int drm_wait_hotplug(struct drm_device *dev, void *data, struct drm_file *filp);
1311 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1312 extern int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *filp);
1313 extern int drm_wait_hotplug(struct drm_device *dev, void *data, struct drm_file *filp);
1314 extern int drm_vblank_wait(struct drm_device * dev, unsigned int *vbl_seq);
1315 extern void drm_locked_tasklet(struct drm_device *dev, void(*func)(struct drm_device*));
1316 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1317 extern void drm_update_vblank_count(struct drm_device *dev, int crtc);
1318 extern void drm_handle_vblank(struct drm_device *dev, int crtc);
1319 extern void drm_handle_hotplug(struct drm_device *dev);
1320 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1321 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1322
1323                                 /* Modesetting support */
1324 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1325                            struct drm_file *file_priv);
1326
1327                                 /* AGP/GART support (drm_agpsupport.h) */
1328 extern struct drm_agp_head *drm_agp_init(struct drm_device *dev);
1329 extern int drm_agp_acquire(struct drm_device *dev);
1330 extern int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1331                                  struct drm_file *file_priv);
1332 extern int drm_agp_release(struct drm_device *dev);
1333 extern int drm_agp_release_ioctl(struct drm_device *dev, void *data,
1334                                  struct drm_file *file_priv);
1335 extern int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1336 extern int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1337                                 struct drm_file *file_priv);
1338 extern int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info);
1339 extern int drm_agp_info_ioctl(struct drm_device *dev, void *data,
1340                         struct drm_file *file_priv);
1341 extern int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1342 extern int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1343                          struct drm_file *file_priv);
1344 extern int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1345 extern int drm_agp_free_ioctl(struct drm_device *dev, void *data,
1346                         struct drm_file *file_priv);
1347 extern int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1348 extern int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1349                           struct drm_file *file_priv);
1350 extern int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1351 extern int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1352                         struct drm_file *file_priv);
1353 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
1354 extern DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type);
1355 #else
1356 extern DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type);
1357 #endif
1358 extern int drm_agp_free_memory(DRM_AGP_MEM * handle);
1359 extern int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start);
1360 extern int drm_agp_unbind_memory(DRM_AGP_MEM * handle);
1361 extern struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev);
1362 extern void drm_agp_chipset_flush(struct drm_device *dev);
1363                                 /* Stub support (drm_stub.h) */
1364 extern int drm_setmaster_ioctl(struct drm_device *dev, void *data,
1365                                struct drm_file *file_priv);
1366 extern int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
1367                                 struct drm_file *file_priv);
1368 extern struct drm_master *drm_get_master(struct drm_minor *minor);
1369 extern void drm_put_master(struct drm_master *master);
1370 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
1371                      struct drm_driver *driver);
1372 extern int drm_put_dev(struct drm_device *dev);
1373 extern int drm_put_minor(struct drm_device *dev, struct drm_minor **p);
1374 extern unsigned int drm_debug; /* 1 to enable debug output */
1375
1376 extern struct class *drm_class;
1377 extern struct proc_dir_entry *drm_proc_root;
1378
1379 extern struct idr drm_minors_idr;
1380
1381 extern drm_local_map_t *drm_getsarea(struct drm_device *dev);
1382
1383                                 /* Proc support (drm_proc.h) */
1384 int drm_proc_init(struct drm_minor *minor, int minor_id,
1385                   struct proc_dir_entry *root);
1386 int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root);
1387
1388                                 /* Scatter Gather Support (drm_scatter.h) */
1389 extern void drm_sg_cleanup(struct drm_sg_mem * entry);
1390 extern int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1391                         struct drm_file *file_priv);
1392 extern int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1393 extern int drm_sg_free(struct drm_device *dev, void *data,
1394                        struct drm_file *file_priv);
1395
1396                                /* ATI PCIGART support (ati_pcigart.h) */
1397 extern int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1398 extern int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1399 extern int drm_ati_alloc_pcigart_table(struct drm_device *dev,
1400                                        struct drm_ati_pcigart_info *gart_info);
1401
1402 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1403                            size_t align, dma_addr_t maxaddr);
1404 extern void __drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1405 extern void drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1406
1407                                /* sysfs support (drm_sysfs.c) */
1408 struct drm_sysfs_class;
1409 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1410 extern void drm_sysfs_destroy(void);
1411 extern int drm_sysfs_device_add(struct drm_minor *minor);
1412 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1413 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1414 extern char *drm_get_connector_status_name(enum drm_connector_status status);
1415 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1416 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1417
1418 /*
1419  * Basic memory manager support (drm_memrange.c)
1420  */
1421
1422 extern struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent,
1423                                                         unsigned long size,
1424                                                         unsigned alignment);
1425 extern void drm_memrange_put_block(struct drm_memrange_node *cur);
1426 extern struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange *mm,
1427                                                           unsigned long size,
1428                                                           unsigned alignment, int best_match);
1429 extern int drm_memrange_init(struct drm_memrange *mm,
1430                              unsigned long start, unsigned long size);
1431 extern void drm_memrange_takedown(struct drm_memrange *mm);
1432 extern int drm_memrange_clean(struct drm_memrange *mm);
1433 extern unsigned long drm_memrange_tail_space(struct drm_memrange *mm);
1434 extern int drm_memrange_remove_space_from_tail(struct drm_memrange *mm,
1435                                                unsigned long size);
1436 extern int drm_memrange_add_space_to_tail(struct drm_memrange *mm,
1437                                           unsigned long size);
1438 static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block)
1439 {
1440         return block->mm;
1441 }
1442
1443 /* Graphics Execution Manager library functions (drm_gem.c) */
1444 int
1445 drm_gem_init (struct drm_device *dev);
1446
1447 void
1448 drm_gem_object_free (struct kref *kref);
1449
1450 struct drm_gem_object *
1451 drm_gem_object_alloc(struct drm_device *dev, size_t size);
1452
1453 void
1454 drm_gem_object_handle_free (struct kref *kref);
1455     
1456 static inline void drm_gem_object_reference(struct drm_gem_object *obj)
1457 {
1458         kref_get(&obj->refcount);
1459 }
1460
1461 static inline void drm_gem_object_unreference(struct drm_gem_object *obj)
1462 {
1463         if (obj == NULL)
1464                 return;
1465
1466         kref_put (&obj->refcount, drm_gem_object_free);
1467 }
1468
1469 int
1470 drm_gem_handle_create(struct drm_file *file_priv,
1471                       struct drm_gem_object *obj,
1472                       int *handlep);
1473
1474 static inline void drm_gem_object_handle_reference (struct drm_gem_object *obj)
1475 {
1476         drm_gem_object_reference (obj);
1477         kref_get(&obj->handlecount);
1478 }
1479
1480 static inline void drm_gem_object_handle_unreference (struct drm_gem_object *obj)
1481 {
1482         if (obj == NULL)
1483                 return;
1484         
1485         /*
1486          * Must bump handle count first as this may be the last
1487          * ref, in which case the object would disappear before we
1488          * checked for a name
1489          */
1490         kref_put (&obj->handlecount, drm_gem_object_handle_free);
1491         drm_gem_object_unreference (obj);
1492 }
1493
1494 struct drm_gem_object *
1495 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
1496                       int handle);
1497 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1498                         struct drm_file *file_priv);
1499 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1500                         struct drm_file *file_priv);
1501 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1502                        struct drm_file *file_priv);
1503
1504 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private);
1505 void drm_gem_release(struct drm_device *dev, struct drm_file *file_private);
1506
1507 extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev);
1508 extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev);
1509
1510 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev,
1511                                                    unsigned int token)
1512 {
1513         struct drm_map_list *_entry;
1514         list_for_each_entry(_entry, &dev->maplist, head)
1515                 if (_entry->user_token == token)
1516                         return _entry->map;
1517         return NULL;
1518 }
1519
1520 static __inline__ int drm_device_is_agp(struct drm_device *dev)
1521 {
1522         if ( dev->driver->device_is_agp != NULL ) {
1523                 int err = (*dev->driver->device_is_agp)(dev);
1524
1525                 if (err != 2) {
1526                         return err;
1527                 }
1528         }
1529
1530         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1531 }
1532
1533 static __inline__ int drm_device_is_pcie(struct drm_device *dev)
1534 {
1535         return pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
1536 }
1537
1538 static __inline__ void drm_core_dropmap(struct drm_map *map)
1539 {
1540 }
1541
1542 #ifndef DEBUG_MEMORY
1543 /** Wrapper around kmalloc() */
1544 static __inline__ void *drm_alloc(size_t size, int area)
1545 {
1546         return kmalloc(size, GFP_KERNEL);
1547 }
1548
1549 /** Wrapper around kfree() */
1550 static __inline__ void drm_free(void *pt, size_t size, int area)
1551 {
1552         kfree(pt);
1553 }
1554 #else
1555 extern void *drm_alloc(size_t size, int area);
1556 extern void drm_free(void *pt, size_t size, int area);
1557 #endif
1558
1559 /*
1560  * Accounting variants of standard calls.
1561  */
1562
1563 static inline void *drm_ctl_alloc(size_t size, int area)
1564 {
1565         void *ret;
1566         if (drm_alloc_memctl(size))
1567                 return NULL;
1568         ret = drm_alloc(size, area);
1569         if (!ret)
1570                 drm_free_memctl(size);
1571         return ret;
1572 }
1573
1574 static inline void *drm_ctl_calloc(size_t nmemb, size_t size, int area)
1575 {
1576         void *ret;
1577
1578         if (drm_alloc_memctl(nmemb*size))
1579                 return NULL;
1580         ret = drm_calloc(nmemb, size, area);
1581         if (!ret)
1582                 drm_free_memctl(nmemb*size);
1583         return ret;
1584 }
1585
1586 static inline void drm_ctl_free(void *pt, size_t size, int area)
1587 {
1588         drm_free(pt, size, area);
1589         drm_free_memctl(size);
1590 }
1591
1592 /*@}*/
1593
1594 #endif                          /* __KERNEL__ */
1595 #endif