LUT updates
[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_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
170 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
171 /*
172  * This should be small enough to allow the use of kmalloc for hash tables
173  * instead of vmalloc.
174  */
175
176 #define DRM_FILE_HASH_ORDER 8
177 #define DRM_MM_INIT_MAX_PAGES 256
178
179 /*@}*/
180
181 #include "drm_compat.h"
182
183 /***********************************************************************/
184 /** \name Macros to make printk easier */
185 /*@{*/
186
187 /**
188  * Error output.
189  *
190  * \param fmt printf() like format string.
191  * \param arg arguments
192  */
193 #define DRM_ERROR(fmt, arg...) \
194         printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* " fmt , __FUNCTION__ , ##arg)
195
196 /**
197  * Memory error output.
198  *
199  * \param area memory area where the error occurred.
200  * \param fmt printf() like format string.
201  * \param arg arguments
202  */
203 #define DRM_MEM_ERROR(area, fmt, arg...) \
204         printk(KERN_ERR "[" DRM_NAME ":%s:%s] *ERROR* " fmt , __FUNCTION__, \
205                drm_mem_stats[area].name , ##arg)
206 #define DRM_INFO(fmt, arg...)  printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
207
208 /**
209  * Debug output.
210  *
211  * \param fmt printf() like format string.
212  * \param arg arguments
213  */
214 #if DRM_DEBUG_CODE
215 #define DRM_DEBUG(fmt, arg...)                                          \
216         do {                                                            \
217                 if ( drm_debug )                                        \
218                         printk(KERN_DEBUG                               \
219                                "[" DRM_NAME ":%s] " fmt ,               \
220                                __FUNCTION__ , ##arg);                   \
221         } while (0)
222 #else
223 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
224 #endif
225
226 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
227
228 #define DRM_PROC_PRINT(fmt, arg...)                                     \
229    len += sprintf(&buf[len], fmt , ##arg);                              \
230    if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
231
232 #define DRM_PROC_PRINT_RET(ret, fmt, arg...)                            \
233    len += sprintf(&buf[len], fmt , ##arg);                              \
234    if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
235
236 /*@}*/
237
238 /***********************************************************************/
239 /** \name Internal types and structures */
240 /*@{*/
241
242 #define DRM_ARRAY_SIZE(x) ARRAY_SIZE(x)
243 #define DRM_MIN(a,b) min(a,b)
244 #define DRM_MAX(a,b) max(a,b)
245
246 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
247 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
248 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
249
250 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
251 /**
252  * Get the private SAREA mapping.
253  *
254  * \param _dev DRM device.
255  * \param _ctx context number.
256  * \param _map output mapping.
257  */
258 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
259         (_map) = (_dev)->context_sareas[_ctx];          \
260 } while(0)
261
262 /**
263  * Test that the hardware lock is held by the caller, returning otherwise.
264  *
265  * \param dev DRM device.
266  * \param file_priv DRM file private pointer of the caller.
267  */
268 #define LOCK_TEST_WITH_RETURN( dev, file_priv )                         \
269 do {                                                                    \
270         if ( !_DRM_LOCK_IS_HELD( file_priv->master->lock.hw_lock->lock ) ||             \
271              file_priv->master->lock.file_priv != file_priv )   {                       \
272                 DRM_ERROR( "%s called without lock held, held  %d owner %p %p\n",\
273                            __FUNCTION__, _DRM_LOCK_IS_HELD( file_priv->master->lock.hw_lock->lock ),\
274                            file_priv->master->lock.file_priv, file_priv );              \
275                 return -EINVAL;                                         \
276         }                                                               \
277 } while (0)
278
279 /**
280  * Copy and IOCTL return string to user space
281  */
282 #define DRM_COPY( name, value )                                         \
283         len = strlen( value );                                          \
284         if ( len > name##_len ) len = name##_len;                       \
285         name##_len = strlen( value );                                   \
286         if ( len && name ) {                                            \
287                 if ( copy_to_user( name, value, len ) )                 \
288                         return -EFAULT;                                 \
289         }
290
291 /**
292  * Ioctl function type.
293  *
294  * \param dev DRM device structure
295  * \param data pointer to kernel-space stored data, copied in and out according
296  *             to ioctl description.
297  * \param file_priv DRM file private pointer.
298  */
299 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
300                         struct drm_file *file_priv);
301
302 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
303                                unsigned long arg);
304
305 #define DRM_AUTH        0x1
306 #define DRM_MASTER      0x2
307 #define DRM_ROOT_ONLY   0x4
308 #define DRM_CONTROL_ALLOW 0x8 // allow ioctl to operate on control node 
309
310 struct drm_ioctl_desc {
311         unsigned int cmd;
312         drm_ioctl_t *func;
313         int flags;
314 };
315 /**
316  * Creates a driver or general drm_ioctl_desc array entry for the given
317  * ioctl, for use by drm_ioctl().
318  */
319 #define DRM_IOCTL_DEF(ioctl, func, flags) \
320         [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
321
322 struct drm_magic_entry {
323         struct list_head head;
324         struct drm_hash_item hash_item;
325         struct drm_file *priv;
326 };
327
328 struct drm_vma_entry {
329         struct list_head head;
330         struct vm_area_struct *vma;
331         pid_t pid;
332 };
333
334 /**
335  * DMA buffer.
336  */
337 struct drm_buf {
338         int idx;                       /**< Index into master buflist */
339         int total;                     /**< Buffer size */
340         int order;                     /**< log-base-2(total) */
341         int used;                      /**< Amount of buffer in use (for DMA) */
342         unsigned long offset;          /**< Byte offset (used internally) */
343         void *address;                 /**< Address of buffer */
344         unsigned long bus_address;     /**< Bus address of buffer */
345         struct drm_buf *next;          /**< Kernel-only: used for free list */
346         __volatile__ int waiting;      /**< On kernel DMA queue */
347         __volatile__ int pending;      /**< On hardware DMA queue */
348         wait_queue_head_t dma_wait;    /**< Processes waiting */
349         struct drm_file *file_priv;    /**< Private of holding file descr */
350         int context;                   /**< Kernel queue for this buffer */
351         int while_locked;              /**< Dispatch this buffer while locked */
352         enum {
353                 DRM_LIST_NONE = 0,
354                 DRM_LIST_FREE = 1,
355                 DRM_LIST_WAIT = 2,
356                 DRM_LIST_PEND = 3,
357                 DRM_LIST_PRIO = 4,
358                 DRM_LIST_RECLAIM = 5
359         } list;                        /**< Which list we're on */
360
361         int dev_priv_size;              /**< Size of buffer private storage */
362         void *dev_private;              /**< Per-buffer private storage */
363 };
364
365 /** bufs is one longer than it has to be */
366 struct drm_waitlist {
367         int count;                      /**< Number of possible buffers */
368         struct drm_buf **bufs;          /**< List of pointers to buffers */
369         struct drm_buf **rp;                    /**< Read pointer */
370         struct drm_buf **wp;                    /**< Write pointer */
371         struct drm_buf **end;           /**< End pointer */
372         spinlock_t read_lock;
373         spinlock_t write_lock;
374 };
375
376 struct drm_freelist {
377         int initialized;               /**< Freelist in use */
378         atomic_t count;                /**< Number of free buffers */
379         struct drm_buf *next;          /**< End pointer */
380
381         wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
382         int low_mark;                  /**< Low water mark */
383         int high_mark;                 /**< High water mark */
384         atomic_t wfh;                  /**< If waiting for high mark */
385         spinlock_t lock;
386 };
387
388 typedef struct drm_dma_handle {
389         dma_addr_t busaddr;
390         void *vaddr;
391         size_t size;
392 } drm_dma_handle_t;
393
394 /**
395  * Buffer entry.  There is one of this for each buffer size order.
396  */
397 struct drm_buf_entry {
398         int buf_size;                   /**< size */
399         int buf_count;                  /**< number of buffers */
400         struct drm_buf *buflist;                /**< buffer list */
401         int seg_count;
402         int page_order;
403         struct drm_dma_handle **seglist;
404         struct drm_freelist freelist;
405 };
406
407 /** File private data */
408 struct drm_file {
409         int authenticated;
410         pid_t pid;
411         uid_t uid;
412         drm_magic_t magic;
413         unsigned long ioctl_count;
414         struct list_head lhead;
415         struct drm_minor *minor;
416         unsigned long lock_count;
417
418         /** Mapping of mm object handles to object pointers. */
419         struct idr object_idr;
420         /** Lock for synchronization of access to object_idr. */
421         spinlock_t table_lock;
422
423         struct file *filp;
424         void *driver_priv;
425
426         int is_master; /* this file private is a master for a minor */
427         struct drm_master *master; /* master this node is currently associated with
428                                       N.B. not always minor->master */
429         struct list_head fbs;
430 };
431
432 /** Wait queue */
433 struct drm_queue {
434         atomic_t use_count;             /**< Outstanding uses (+1) */
435         atomic_t finalization;          /**< Finalization in progress */
436         atomic_t block_count;           /**< Count of processes waiting */
437         atomic_t block_read;            /**< Queue blocked for reads */
438         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
439         atomic_t block_write;           /**< Queue blocked for writes */
440         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
441 #if 1
442         atomic_t total_queued;          /**< Total queued statistic */
443         atomic_t total_flushed;         /**< Total flushes statistic */
444         atomic_t total_locks;           /**< Total locks statistics */
445 #endif
446         enum drm_ctx_flags flags;       /**< Context preserving and 2D-only */
447         struct drm_waitlist waitlist;   /**< Pending buffers */
448         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
449 };
450
451 /**
452  * Lock data.
453  */
454 struct drm_lock_data {
455         struct drm_hw_lock *hw_lock;            /**< Hardware lock */
456         /** Private of lock holder's file (NULL=kernel) */
457         struct drm_file *file_priv;
458         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
459         unsigned long lock_time;        /**< Time of last lock in jiffies */
460         spinlock_t spinlock;
461         uint32_t kernel_waiters;
462         uint32_t user_waiters;
463         int idle_has_lock;
464         /**
465          * Boolean signaling that the lock is held on behalf of the
466          * file_priv client by the kernel in an ioctl handler.
467          */
468         int kernel_held;
469 };
470
471 /**
472  * DMA data.
473  */
474 struct drm_device_dma {
475
476         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
477         int buf_count;                  /**< total number of buffers */
478         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
479         int seg_count;
480         int page_count;                 /**< number of pages */
481         unsigned long *pagelist;        /**< page list */
482         unsigned long byte_count;
483         enum {
484                 _DRM_DMA_USE_AGP = 0x01,
485                 _DRM_DMA_USE_SG = 0x02,
486                 _DRM_DMA_USE_FB = 0x04,
487                 _DRM_DMA_USE_PCI_RO = 0x08
488         } flags;
489
490 };
491
492 /**
493  * AGP memory entry.  Stored as a doubly linked list.
494  */
495 struct drm_agp_mem {
496         unsigned long handle;           /**< handle */
497         DRM_AGP_MEM *memory;
498         unsigned long bound;            /**< address */
499         int pages;
500         struct list_head head;
501 };
502
503 /**
504  * AGP data.
505  *
506  * \sa drm_agp_init() and drm_device::agp.
507  */
508 struct drm_agp_head {
509         DRM_AGP_KERN agp_info;          /**< AGP device information */
510         struct list_head memory;
511         unsigned long mode;             /**< AGP mode */
512 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11)
513         struct agp_bridge_data *bridge;
514 #endif
515         int enabled;                    /**< whether the AGP bus as been enabled */
516         int acquired;                   /**< whether the AGP device has been acquired */
517         unsigned long base;
518         int agp_mtrr;
519         int cant_use_aperture;
520         unsigned long page_mask;
521 };
522
523 /**
524  * Scatter-gather memory.
525  */
526 struct drm_sg_mem {
527         unsigned long handle;
528         void *virtual;
529         int pages;
530         struct page **pagelist;
531         dma_addr_t *busaddr;
532 };
533
534 struct drm_sigdata {
535         int context;
536         struct drm_hw_lock *lock;
537 };
538
539
540 /*
541  * Generic memory manager structs
542  */
543
544 struct drm_memrange_node {
545         struct list_head fl_entry;
546         struct list_head ml_entry;
547         int free;
548         unsigned long start;
549         unsigned long size;
550         struct drm_memrange *mm;
551         void *private;
552 };
553
554 struct drm_memrange {
555         struct list_head fl_entry;
556         struct list_head ml_entry;
557 };
558
559
560 /**
561  * Mappings list
562  */
563 struct drm_map_list {
564         struct list_head head;          /**< list head */
565         struct drm_hash_item hash;
566         struct drm_map *map;                    /**< mapping */
567         uint64_t user_token;
568         struct drm_master *master; /** if this map is associated with a specific
569                                        master */
570         struct drm_memrange_node *file_offset_node;
571 };
572
573 typedef struct drm_map drm_local_map_t;
574
575 /**
576  * Context handle list
577  */
578 struct drm_ctx_list {
579         struct list_head head;          /**< list head */
580         drm_context_t handle;           /**< context handle */
581         struct drm_file *tag;           /**< associated fd private data */
582 };
583
584 struct drm_vbl_sig {
585         struct list_head head;
586         unsigned int sequence;
587         struct siginfo info;
588         struct task_struct *task;
589 };
590
591 struct drm_hotplug_sig {
592         struct list_head head;
593         unsigned int counter;
594         struct siginfo info;
595         struct task_struct *task;
596 };
597
598 /* location of GART table */
599 #define DRM_ATI_GART_MAIN 1
600 #define DRM_ATI_GART_FB   2
601
602 #define DRM_ATI_GART_PCI 1
603 #define DRM_ATI_GART_PCIE 2
604 #define DRM_ATI_GART_IGP 3
605
606 struct drm_ati_pcigart_info {
607         int gart_table_location;
608         int gart_reg_if;
609         void *addr;
610         dma_addr_t bus_addr;
611         dma_addr_t table_mask;
612         dma_addr_t member_mask;
613         struct drm_dma_handle *table_handle;
614         drm_local_map_t mapping;
615         int table_size;
616 };
617
618 /**
619  * This structure defines the drm_mm memory object, which will be used by the
620  * DRM for its buffer objects.
621  */
622 struct drm_gem_object {
623         /** Reference count of this object */
624         struct kref refcount;
625
626         /** Handle count of this object. Each handle also holds a reference */
627         struct kref handlecount;
628
629         /** Related drm device */
630         struct drm_device *dev;
631         
632         /** File representing the shmem storage */
633         struct file *filp;
634
635         /**
636          * Size of the object, in bytes.  Immutable over the object's
637          * lifetime.
638          */
639         size_t size;
640
641         /**
642          * Global name for this object, starts at 1. 0 means unnamed.
643          * Access is covered by the object_name_lock in the related drm_device
644          */
645         int name;
646
647         /**
648          * Memory domains. These monitor which caches contain read/write data
649          * related to the object. When transitioning from one set of domains
650          * to another, the driver is called to ensure that caches are suitably
651          * flushed and invalidated
652          */
653         uint32_t        read_domains;
654         uint32_t        write_domain;
655
656         /**
657          * While validating an exec operation, the
658          * new read/write domain values are computed here.
659          * They will be transferred to the above values
660          * at the point that any cache flushing occurs
661          */
662         uint32_t        pending_read_domains;
663         uint32_t        pending_write_domain;
664
665         void *driver_private;
666 };
667
668 #include "drm_objects.h"
669 #include "drm_crtc.h"
670
671 /* per-master structure */
672 struct drm_master {
673
674         struct kref refcount; /* refcount for this master */
675
676         struct list_head head; /**< each minor contains a list of masters */
677         struct drm_minor *minor; /**< link back to minor we are a master for */
678
679         char *unique;                   /**< Unique identifier: e.g., busid */
680         int unique_len;                 /**< Length of unique field */
681
682         int blocked;                    /**< Blocked due to VC switch? */
683
684         /** \name Authentication */
685         /*@{ */
686         struct drm_open_hash magiclist;
687         struct list_head magicfree;
688         /*@} */
689
690         struct drm_lock_data lock;              /**< Information on hardware lock */
691
692         void *driver_priv; /**< Private structure for driver to use */
693 };
694
695 /**
696  * DRM driver structure. This structure represent the common code for
697  * a family of cards. There will one drm_device for each card present
698  * in this family
699  */
700
701 struct drm_driver {
702         int (*load) (struct drm_device *, unsigned long flags);
703         int (*firstopen) (struct drm_device *);
704         int (*open) (struct drm_device *, struct drm_file *);
705         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
706         void (*postclose) (struct drm_device *, struct drm_file *);
707         void (*lastclose) (struct drm_device *);
708         int (*unload) (struct drm_device *);
709         int (*suspend) (struct drm_device *, pm_message_t state);
710         int (*resume) (struct drm_device *);
711         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
712         void (*dma_ready) (struct drm_device *);
713         int (*dma_quiescent) (struct drm_device *);
714         int (*context_ctor) (struct drm_device *dev, int context);
715         int (*context_dtor) (struct drm_device *dev, int context);
716         int (*kernel_context_switch) (struct drm_device *dev, int old,
717                                       int new);
718         void (*kernel_context_switch_unlock) (struct drm_device * dev);
719         /**
720          * get_vblank_counter - get raw hardware vblank counter
721          * @dev: DRM device
722          * @crtc: counter to fetch
723          *
724          * Driver callback for fetching a raw hardware vblank counter
725          * for @crtc.  If a device doesn't have a hardware counter, the
726          * driver can simply return the value of drm_vblank_count and
727          * make the enable_vblank() and disable_vblank() hooks into no-ops,
728          * leaving interrupts enabled at all times.
729          *
730          * Wraparound handling and loss of events due to modesetting is dealt
731          * with in the DRM core code.
732          *
733          * RETURNS
734          * Raw vblank counter value.
735          */
736         u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
737
738         /**
739          * enable_vblank - enable vblank interrupt events
740          * @dev: DRM device
741          * @crtc: which irq to enable
742          *
743          * Enable vblank interrupts for @crtc.  If the device doesn't have
744          * a hardware vblank counter, this routine should be a no-op, since
745          * interrupts will have to stay on to keep the count accurate.
746          *
747          * RETURNS
748          * Zero on success, appropriate errno if the given @crtc's vblank
749          * interrupt cannot be enabled.
750          */
751         int (*enable_vblank) (struct drm_device *dev, int crtc);
752
753         /**
754          * disable_vblank - disable vblank interrupt events
755          * @dev: DRM device
756          * @crtc: which irq to enable
757          *
758          * Disable vblank interrupts for @crtc.  If the device doesn't have
759          * a hardware vblank counter, this routine should be a no-op, since
760          * interrupts will have to stay on to keep the count accurate.
761          */
762         void (*disable_vblank) (struct drm_device *dev, int crtc);
763         int (*dri_library_name) (struct drm_device *dev, char * buf);
764
765         /**
766          * Called by \c drm_device_is_agp.  Typically used to determine if a
767          * card is really attached to AGP or not.
768          *
769          * \param dev  DRM device handle
770          *
771          * \returns
772          * One of three values is returned depending on whether or not the
773          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
774          * (return of 1), or may or may not be AGP (return of 2).
775          */
776         int (*device_is_agp) (struct drm_device *dev);
777
778 /* these have to be filled in */
779          irqreturn_t(*irq_handler) (DRM_IRQ_ARGS);
780         void (*irq_preinstall) (struct drm_device *dev);
781         int (*irq_postinstall) (struct drm_device *dev);
782         void (*irq_uninstall) (struct drm_device *dev);
783         void (*reclaim_buffers) (struct drm_device *dev,
784                                  struct drm_file *file_priv);
785         void (*reclaim_buffers_locked) (struct drm_device *dev,
786                                         struct drm_file *file_priv);
787         void (*reclaim_buffers_idlelocked) (struct drm_device *dev,
788                                             struct drm_file *file_priv);
789         unsigned long (*get_map_ofs) (struct drm_map *map);
790         unsigned long (*get_reg_ofs) (struct drm_device *dev);
791         void (*set_version) (struct drm_device *dev,
792                              struct drm_set_version *sv);
793
794         /* Master routines */
795         int (*master_create)(struct drm_device *dev, struct drm_master *master);
796         void (*master_destroy)(struct drm_device *dev, struct drm_master *master);
797
798         int (*proc_init)(struct drm_minor *minor);
799         void (*proc_cleanup)(struct drm_minor *minor);
800
801         /**
802          * Driver-specific constructor for drm_gem_objects, to set up
803          * obj->driver_private.
804          *
805          * Returns 0 on success.
806          */
807         int (*gem_init_object) (struct drm_gem_object *obj);
808         void (*gem_free_object) (struct drm_gem_object *obj);
809
810         struct drm_fence_driver *fence_driver;
811         struct drm_bo_driver *bo_driver;
812
813         int major;
814         int minor;
815         int patchlevel;
816         char *name;
817         char *desc;
818         char *date;
819
820 /* variables */
821         u32 driver_features;
822         int dev_priv_size;
823         struct drm_ioctl_desc *ioctls;
824         int num_ioctls;
825         struct file_operations fops;
826         struct pci_driver pci_driver;
827 };
828
829 #define DRM_MINOR_UNASSIGNED 0
830 #define DRM_MINOR_LEGACY 1
831 #define DRM_MINOR_CONTROL 2
832 #define DRM_MINOR_RENDER 3
833
834 /**
835  * DRM minor structure. This structure represents a drm minor number.
836  */
837 struct drm_minor {
838         int index;                      /**< Minor device number */
839         int type;                       /**< Control or render */
840         dev_t device;                   /**< Device number for mknod */
841         struct device kdev;             /**< Linux device */
842         struct drm_device *dev;
843         /* for render nodes */
844         struct proc_dir_entry *dev_root;  /**< proc directory entry */
845         struct class_device *dev_class;
846
847         /* for control nodes - a pointer to the current master for this control node */
848         struct drm_master *master; /* currently active master for this node */
849         struct list_head master_list;
850
851         struct drm_mode_group mode_group;
852 };
853
854
855 /**
856  * DRM device structure. This structure represent a complete card that
857  * may contain multiple heads.
858  */
859 struct drm_device {
860         char *devname;                  /**< For /proc/interrupts */
861         int if_version;                 /**< Highest interface version set */
862
863         /** \name Locks */
864         /*@{ */
865         spinlock_t count_lock;          /**< For inuse, drm_device::open_count, drm_device::buf_use */
866         struct mutex struct_mutex;      /**< For others */
867         /*@} */
868
869         /** \name Usage Counters */
870         /*@{ */
871         int open_count;                 /**< Outstanding files open */
872         atomic_t ioctl_count;           /**< Outstanding IOCTLs pending */
873         atomic_t vma_count;             /**< Outstanding vma areas open */
874         int buf_use;                    /**< Buffers in use -- cannot alloc */
875         atomic_t buf_alloc;             /**< Buffer allocation in progress */
876         /*@} */
877
878         /** \name Performance counters */
879         /*@{ */
880         unsigned long counters;
881         enum drm_stat_type types[15];
882         atomic_t counts[15];
883         /*@} */
884
885
886         /** \name Memory management */
887         /*@{ */
888         struct list_head maplist;       /**< Linked list of regions */
889         int map_count;                  /**< Number of mappable regions */
890         struct drm_open_hash map_hash;       /**< User token hash table for maps */
891         struct drm_memrange offset_manager;  /**< User token manager */
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 struct drm_master *drm_master_create(struct drm_minor *minor);
1351 extern struct drm_master *drm_master_get(struct drm_master *master);
1352 extern void drm_master_put(struct drm_master **master);
1353 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
1354                        struct drm_driver *driver);
1355 extern int drm_put_dev(struct drm_device *dev);
1356 extern int drm_put_minor(struct drm_minor **minor_p);
1357 extern unsigned int drm_debug; /* 1 to enable debug output */
1358
1359 extern struct class *drm_class;
1360 extern struct proc_dir_entry *drm_proc_root;
1361
1362 extern struct idr drm_minors_idr;
1363
1364 extern drm_local_map_t *drm_getsarea(struct drm_device *dev);
1365
1366                                 /* Proc support (drm_proc.h) */
1367 int drm_proc_init(struct drm_minor *minor, int minor_id,
1368                   struct proc_dir_entry *root);
1369 int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root);
1370
1371                                 /* Scatter Gather Support (drm_scatter.h) */
1372 extern void drm_sg_cleanup(struct drm_sg_mem * entry);
1373 extern int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1374                         struct drm_file *file_priv);
1375 extern int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1376 extern int drm_sg_free(struct drm_device *dev, void *data,
1377                        struct drm_file *file_priv);
1378
1379                                /* ATI PCIGART support (ati_pcigart.h) */
1380 extern int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1381 extern int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1382 extern int drm_ati_alloc_pcigart_table(struct drm_device *dev,
1383                                        struct drm_ati_pcigart_info *gart_info);
1384
1385 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1386                            size_t align, dma_addr_t maxaddr);
1387 extern void __drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1388 extern void drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1389
1390                                /* sysfs support (drm_sysfs.c) */
1391 struct drm_sysfs_class;
1392 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1393 extern void drm_sysfs_destroy(void);
1394 extern int drm_sysfs_device_add(struct drm_minor *minor);
1395 extern void drm_sysfs_hotplug_event(struct drm_device *dev);
1396 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1397 extern char *drm_get_connector_status_name(enum drm_connector_status status);
1398 extern int drm_sysfs_connector_add(struct drm_connector *connector);
1399 extern void drm_sysfs_connector_remove(struct drm_connector *connector);
1400
1401 /*
1402  * Basic memory manager support (drm_memrange.c)
1403  */
1404
1405 extern struct drm_memrange_node *drm_memrange_get_block(struct drm_memrange_node * parent,
1406                                                         unsigned long size,
1407                                                         unsigned alignment);
1408 extern void drm_memrange_put_block(struct drm_memrange_node *cur);
1409 extern struct drm_memrange_node *drm_memrange_search_free(const struct drm_memrange *mm,
1410                                                           unsigned long size,
1411                                                           unsigned alignment, int best_match);
1412 extern int drm_memrange_init(struct drm_memrange *mm,
1413                              unsigned long start, unsigned long size);
1414 extern void drm_memrange_takedown(struct drm_memrange *mm);
1415 extern int drm_memrange_clean(struct drm_memrange *mm);
1416 extern unsigned long drm_memrange_tail_space(struct drm_memrange *mm);
1417 extern int drm_memrange_remove_space_from_tail(struct drm_memrange *mm,
1418                                                unsigned long size);
1419 extern int drm_memrange_add_space_to_tail(struct drm_memrange *mm,
1420                                           unsigned long size);
1421 static inline struct drm_memrange *drm_get_mm(struct drm_memrange_node *block)
1422 {
1423         return block->mm;
1424 }
1425
1426 /* Graphics Execution Manager library functions (drm_gem.c) */
1427 int
1428 drm_gem_init (struct drm_device *dev);
1429
1430 void
1431 drm_gem_object_free (struct kref *kref);
1432
1433 struct drm_gem_object *
1434 drm_gem_object_alloc(struct drm_device *dev, size_t size);
1435
1436 void
1437 drm_gem_object_handle_free (struct kref *kref);
1438     
1439 static inline void drm_gem_object_reference(struct drm_gem_object *obj)
1440 {
1441         kref_get(&obj->refcount);
1442 }
1443
1444 static inline void drm_gem_object_unreference(struct drm_gem_object *obj)
1445 {
1446         if (obj == NULL)
1447                 return;
1448
1449         kref_put (&obj->refcount, drm_gem_object_free);
1450 }
1451
1452 int
1453 drm_gem_handle_create(struct drm_file *file_priv,
1454                       struct drm_gem_object *obj,
1455                       int *handlep);
1456
1457 static inline void drm_gem_object_handle_reference (struct drm_gem_object *obj)
1458 {
1459         drm_gem_object_reference (obj);
1460         kref_get(&obj->handlecount);
1461 }
1462
1463 static inline void drm_gem_object_handle_unreference (struct drm_gem_object *obj)
1464 {
1465         if (obj == NULL)
1466                 return;
1467         
1468         /*
1469          * Must bump handle count first as this may be the last
1470          * ref, in which case the object would disappear before we
1471          * checked for a name
1472          */
1473         kref_put (&obj->handlecount, drm_gem_object_handle_free);
1474         drm_gem_object_unreference (obj);
1475 }
1476
1477 struct drm_gem_object *
1478 drm_gem_object_lookup(struct drm_device *dev, struct drm_file *filp,
1479                       int handle);
1480 int drm_gem_close_ioctl(struct drm_device *dev, void *data,
1481                         struct drm_file *file_priv);
1482 int drm_gem_flink_ioctl(struct drm_device *dev, void *data,
1483                         struct drm_file *file_priv);
1484 int drm_gem_open_ioctl(struct drm_device *dev, void *data,
1485                        struct drm_file *file_priv);
1486
1487 void drm_gem_open(struct drm_device *dev, struct drm_file *file_private);
1488 void drm_gem_release(struct drm_device *dev, struct drm_file *file_private);
1489
1490 extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev);
1491 extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev);
1492
1493 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev,
1494                                                    unsigned int token)
1495 {
1496         struct drm_map_list *_entry;
1497         list_for_each_entry(_entry, &dev->maplist, head)
1498                 if (_entry->user_token == token)
1499                         return _entry->map;
1500         return NULL;
1501 }
1502
1503 static __inline__ int drm_device_is_agp(struct drm_device *dev)
1504 {
1505         if ( dev->driver->device_is_agp != NULL ) {
1506                 int err = (*dev->driver->device_is_agp)(dev);
1507
1508                 if (err != 2) {
1509                         return err;
1510                 }
1511         }
1512
1513         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1514 }
1515
1516 static __inline__ int drm_device_is_pcie(struct drm_device *dev)
1517 {
1518         return pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
1519 }
1520
1521 static __inline__ void drm_core_dropmap(struct drm_map *map)
1522 {
1523 }
1524
1525 #ifndef DEBUG_MEMORY
1526 /** Wrapper around kmalloc() */
1527 static __inline__ void *drm_alloc(size_t size, int area)
1528 {
1529         return kmalloc(size, GFP_KERNEL);
1530 }
1531
1532 /** Wrapper around kfree() */
1533 static __inline__ void drm_free(void *pt, size_t size, int area)
1534 {
1535         kfree(pt);
1536 }
1537 #else
1538 extern void *drm_alloc(size_t size, int area);
1539 extern void drm_free(void *pt, size_t size, int area);
1540 #endif
1541
1542 /*
1543  * Accounting variants of standard calls.
1544  */
1545
1546 static inline void *drm_ctl_alloc(size_t size, int area)
1547 {
1548         void *ret;
1549         if (drm_alloc_memctl(size))
1550                 return NULL;
1551         ret = drm_alloc(size, area);
1552         if (!ret)
1553                 drm_free_memctl(size);
1554         return ret;
1555 }
1556
1557 static inline void *drm_ctl_calloc(size_t nmemb, size_t size, int area)
1558 {
1559         void *ret;
1560
1561         if (drm_alloc_memctl(nmemb*size))
1562                 return NULL;
1563         ret = drm_calloc(nmemb, size, area);
1564         if (!ret)
1565                 drm_free_memctl(nmemb*size);
1566         return ret;
1567 }
1568
1569 static inline void drm_ctl_free(void *pt, size_t size, int area)
1570 {
1571         drm_free(pt, size, area);
1572         drm_free_memctl(size);
1573 }
1574
1575 /*@}*/
1576
1577 #endif                          /* __KERNEL__ */
1578 #endif