i915: more version checks
[profile/ivi/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/pagemap.h>
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
59 #include <linux/mutex.h>
60 #endif
61 #if defined(__alpha__) || defined(__powerpc__)
62 #include <asm/pgtable.h>        /* For pte_wrprotect */
63 #endif
64 #include <asm/io.h>
65 #include <asm/mman.h>
66 #include <asm/uaccess.h>
67 #ifdef CONFIG_MTRR
68 #include <asm/mtrr.h>
69 #endif
70 #include <asm/agp.h>
71 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
72 #include <linux/types.h>
73 #include <linux/agp_backend.h>
74 #endif
75 #include <linux/workqueue.h>
76 #include <linux/poll.h>
77 #include <asm/pgalloc.h>
78 #include "drm.h"
79 #include <linux/slab.h>
80 #include <linux/idr.h>
81
82 #define __OS_HAS_AGP (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
83 #define __OS_HAS_MTRR (defined(CONFIG_MTRR))
84
85 #include "drm_os_linux.h"
86 #include "drm_hashtab.h"
87 #include "drm_internal.h"
88
89 struct drm_device;
90 struct drm_file;
91
92 /* If you want the memory alloc debug functionality, change define below */
93 /* #define DEBUG_MEMORY */
94
95 /***********************************************************************/
96 /** \name DRM template customization defaults */
97 /*@{*/
98
99 /* driver capabilities and requirements mask */
100 #define DRIVER_USE_AGP     0x1
101 #define DRIVER_REQUIRE_AGP 0x2
102 #define DRIVER_USE_MTRR    0x4
103 #define DRIVER_PCI_DMA     0x8
104 #define DRIVER_SG          0x10
105 #define DRIVER_HAVE_DMA    0x20
106 #define DRIVER_HAVE_IRQ    0x40
107 #define DRIVER_IRQ_SHARED  0x80
108 #define DRIVER_DMA_QUEUE   0x100
109 #define DRIVER_FB_DMA      0x200
110
111
112 /*@}*/
113
114 /***********************************************************************/
115 /** \name Begin the DRM... */
116 /*@{*/
117
118 #define DRM_DEBUG_CODE 2          /**< Include debugging code if > 1, then
119                                      also include looping detection. */
120
121 #define DRM_MAGIC_HASH_ORDER  4 /**< Size of key hash table. Must be power of 2. */
122 #define DRM_KERNEL_CONTEXT    0  /**< Change drm_resctx if changed */
123 #define DRM_RESERVED_CONTEXTS 1  /**< Change drm_resctx if changed */
124 #define DRM_LOOPING_LIMIT     5000000
125 #define DRM_TIME_SLICE        (HZ/20)  /**< Time slice for GLXContexts */
126 #define DRM_LOCK_SLICE        1 /**< Time slice for lock, in jiffies */
127
128 #define DRM_FLAG_DEBUG    0x01
129
130 #define DRM_MEM_DMA        0
131 #define DRM_MEM_SAREA      1
132 #define DRM_MEM_DRIVER     2
133 #define DRM_MEM_MAGIC      3
134 #define DRM_MEM_IOCTLS     4
135 #define DRM_MEM_MAPS       5
136 #define DRM_MEM_VMAS       6
137 #define DRM_MEM_BUFS       7
138 #define DRM_MEM_SEGS       8
139 #define DRM_MEM_PAGES      9
140 #define DRM_MEM_FILES     10
141 #define DRM_MEM_QUEUES    11
142 #define DRM_MEM_CMDS      12
143 #define DRM_MEM_MAPPINGS  13
144 #define DRM_MEM_BUFLISTS  14
145 #define DRM_MEM_AGPLISTS  15
146 #define DRM_MEM_TOTALAGP  16
147 #define DRM_MEM_BOUNDAGP  17
148 #define DRM_MEM_CTXBITMAP 18
149 #define DRM_MEM_STUB      19
150 #define DRM_MEM_SGLISTS   20
151 #define DRM_MEM_CTXLIST   21
152 #define DRM_MEM_MM        22
153 #define DRM_MEM_HASHTAB   23
154 #define DRM_MEM_OBJECTS   24
155 #define DRM_MEM_FENCE     25
156 #define DRM_MEM_TTM       26
157 #define DRM_MEM_BUFOBJ    27
158
159 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
160 #define DRM_MAP_HASH_OFFSET 0x10000000
161 #define DRM_MAP_HASH_ORDER 12
162 #define DRM_OBJECT_HASH_ORDER 12
163 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
164 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
165 /*
166  * This should be small enough to allow the use of kmalloc for hash tables
167  * instead of vmalloc.
168  */
169
170 #define DRM_FILE_HASH_ORDER 8
171 #define DRM_MM_INIT_MAX_PAGES 256
172
173 /*@}*/
174
175 #include "drm_compat.h"
176
177 /***********************************************************************/
178 /** \name Macros to make printk easier */
179 /*@{*/
180
181 /**
182  * Error output.
183  *
184  * \param fmt printf() like format string.
185  * \param arg arguments
186  */
187 #define DRM_ERROR(fmt, arg...) \
188         printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* " fmt , __FUNCTION__ , ##arg)
189
190 /**
191  * Memory error output.
192  *
193  * \param area memory area where the error occurred.
194  * \param fmt printf() like format string.
195  * \param arg arguments
196  */
197 #define DRM_MEM_ERROR(area, fmt, arg...) \
198         printk(KERN_ERR "[" DRM_NAME ":%s:%s] *ERROR* " fmt , __FUNCTION__, \
199                drm_mem_stats[area].name , ##arg)
200 #define DRM_INFO(fmt, arg...)  printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
201
202 /**
203  * Debug output.
204  *
205  * \param fmt printf() like format string.
206  * \param arg arguments
207  */
208 #if DRM_DEBUG_CODE
209 #define DRM_DEBUG(fmt, arg...)                                          \
210         do {                                                            \
211                 if ( drm_debug )                                        \
212                         printk(KERN_DEBUG                               \
213                                "[" DRM_NAME ":%s] " fmt ,               \
214                                __FUNCTION__ , ##arg);                   \
215         } while (0)
216 #else
217 #define DRM_DEBUG(fmt, arg...)           do { } while (0)
218 #endif
219
220 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
221
222 #define DRM_PROC_PRINT(fmt, arg...)                                     \
223    len += sprintf(&buf[len], fmt , ##arg);                              \
224    if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
225
226 #define DRM_PROC_PRINT_RET(ret, fmt, arg...)                            \
227    len += sprintf(&buf[len], fmt , ##arg);                              \
228    if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
229
230 /*@}*/
231
232 /***********************************************************************/
233 /** \name Internal types and structures */
234 /*@{*/
235
236 #define DRM_ARRAY_SIZE(x) ARRAY_SIZE(x)
237 #define DRM_MIN(a,b) min(a,b)
238 #define DRM_MAX(a,b) max(a,b)
239
240 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
241 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
242 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
243
244 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
245 /**
246  * Get the private SAREA mapping.
247  *
248  * \param _dev DRM device.
249  * \param _ctx context number.
250  * \param _map output mapping.
251  */
252 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do {       \
253         (_map) = (_dev)->context_sareas[_ctx];          \
254 } while(0)
255
256 /**
257  * Test that the hardware lock is held by the caller, returning otherwise.
258  *
259  * \param dev DRM device.
260  * \param file_priv DRM file private pointer of the caller.
261  */
262 #define LOCK_TEST_WITH_RETURN( dev, file_priv )                         \
263 do {                                                                    \
264         if ( !_DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ) ||           \
265              dev->lock.file_priv != file_priv ) {                       \
266                 DRM_ERROR( "%s called without lock held, held  %d owner %p %p\n",\
267                            __FUNCTION__, _DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ),\
268                            dev->lock.file_priv, file_priv );            \
269                 return -EINVAL;                                         \
270         }                                                               \
271 } while (0)
272
273 /**
274  * Copy and IOCTL return string to user space
275  */
276 #define DRM_COPY( name, value )                                         \
277         len = strlen( value );                                          \
278         if ( len > name##_len ) len = name##_len;                       \
279         name##_len = strlen( value );                                   \
280         if ( len && name ) {                                            \
281                 if ( copy_to_user( name, value, len ) )                 \
282                         return -EFAULT;                                 \
283         }
284
285 /**
286  * Ioctl function type.
287  *
288  * \param dev DRM device structure
289  * \param data pointer to kernel-space stored data, copied in and out according
290  *             to ioctl description.
291  * \param file_priv DRM file private pointer.
292  */
293 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
294                         struct drm_file *file_priv);
295
296 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
297                                unsigned long arg);
298
299 #define DRM_AUTH        0x1
300 #define DRM_MASTER      0x2
301 #define DRM_ROOT_ONLY   0x4
302
303 struct drm_ioctl_desc {
304         unsigned int cmd;
305         drm_ioctl_t *func;
306         int flags;
307 };
308 /**
309  * Creates a driver or general drm_ioctl_desc array entry for the given
310  * ioctl, for use by drm_ioctl().
311  */
312 #define DRM_IOCTL_DEF(ioctl, func, flags) \
313         [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
314
315 struct drm_magic_entry {
316         struct list_head head;
317         struct drm_hash_item hash_item;
318         struct drm_file *priv;
319 };
320
321 struct drm_vma_entry {
322         struct list_head head;
323         struct vm_area_struct *vma;
324         pid_t pid;
325 };
326
327 /**
328  * DMA buffer.
329  */
330 struct drm_buf {
331         int idx;                       /**< Index into master buflist */
332         int total;                     /**< Buffer size */
333         int order;                     /**< log-base-2(total) */
334         int used;                      /**< Amount of buffer in use (for DMA) */
335         unsigned long offset;          /**< Byte offset (used internally) */
336         void *address;                 /**< Address of buffer */
337         unsigned long bus_address;     /**< Bus address of buffer */
338         struct drm_buf *next;          /**< Kernel-only: used for free list */
339         __volatile__ int waiting;      /**< On kernel DMA queue */
340         __volatile__ int pending;      /**< On hardware DMA queue */
341         wait_queue_head_t dma_wait;    /**< Processes waiting */
342         struct drm_file *file_priv;    /**< Private of holding file descr */
343         int context;                   /**< Kernel queue for this buffer */
344         int while_locked;              /**< Dispatch this buffer while locked */
345         enum {
346                 DRM_LIST_NONE = 0,
347                 DRM_LIST_FREE = 1,
348                 DRM_LIST_WAIT = 2,
349                 DRM_LIST_PEND = 3,
350                 DRM_LIST_PRIO = 4,
351                 DRM_LIST_RECLAIM = 5
352         } list;                        /**< Which list we're on */
353
354         int dev_priv_size;              /**< Size of buffer private storage */
355         void *dev_private;              /**< Per-buffer private storage */
356 };
357
358 /** bufs is one longer than it has to be */
359 struct drm_waitlist {
360         int count;                      /**< Number of possible buffers */
361         struct drm_buf **bufs;          /**< List of pointers to buffers */
362         struct drm_buf **rp;                    /**< Read pointer */
363         struct drm_buf **wp;                    /**< Write pointer */
364         struct drm_buf **end;           /**< End pointer */
365         spinlock_t read_lock;
366         spinlock_t write_lock;
367 };
368
369 struct drm_freelist {
370         int initialized;               /**< Freelist in use */
371         atomic_t count;                /**< Number of free buffers */
372         struct drm_buf *next;          /**< End pointer */
373
374         wait_queue_head_t waiting;     /**< Processes waiting on free bufs */
375         int low_mark;                  /**< Low water mark */
376         int high_mark;                 /**< High water mark */
377         atomic_t wfh;                  /**< If waiting for high mark */
378         spinlock_t lock;
379 };
380
381 typedef struct drm_dma_handle {
382         dma_addr_t busaddr;
383         void *vaddr;
384         size_t size;
385 } drm_dma_handle_t;
386
387 /**
388  * Buffer entry.  There is one of this for each buffer size order.
389  */
390 struct drm_buf_entry {
391         int buf_size;                   /**< size */
392         int buf_count;                  /**< number of buffers */
393         struct drm_buf *buflist;                /**< buffer list */
394         int seg_count;
395         int page_order;
396         struct drm_dma_handle **seglist;
397         struct drm_freelist freelist;
398 };
399
400
401 enum drm_ref_type {
402         _DRM_REF_USE = 0,
403         _DRM_REF_TYPE1,
404         _DRM_NO_REF_TYPES
405 };
406
407
408 /** File private data */
409 struct drm_file {
410         int authenticated;
411         int master;
412         pid_t pid;
413         uid_t uid;
414         drm_magic_t magic;
415         unsigned long ioctl_count;
416         struct list_head lhead;
417         struct drm_minor *minor;
418         int remove_auth_on_close;
419         unsigned long lock_count;
420
421         /*
422          * The user object hash table is global and resides in the
423          * drm_device structure. We protect the lists and hash tables with the
424          * device struct_mutex. A bit coarse-grained but probably the best
425          * option.
426          */
427
428         struct list_head refd_objects;
429
430         struct drm_open_hash refd_object_hash[_DRM_NO_REF_TYPES];
431         struct file *filp;
432         void *driver_priv;
433 };
434
435 /** Wait queue */
436 struct drm_queue {
437         atomic_t use_count;             /**< Outstanding uses (+1) */
438         atomic_t finalization;          /**< Finalization in progress */
439         atomic_t block_count;           /**< Count of processes waiting */
440         atomic_t block_read;            /**< Queue blocked for reads */
441         wait_queue_head_t read_queue;   /**< Processes waiting on block_read */
442         atomic_t block_write;           /**< Queue blocked for writes */
443         wait_queue_head_t write_queue;  /**< Processes waiting on block_write */
444 #if 1
445         atomic_t total_queued;          /**< Total queued statistic */
446         atomic_t total_flushed;         /**< Total flushes statistic */
447         atomic_t total_locks;           /**< Total locks statistics */
448 #endif
449         enum drm_ctx_flags flags;       /**< Context preserving and 2D-only */
450         struct drm_waitlist waitlist;   /**< Pending buffers */
451         wait_queue_head_t flush_queue;  /**< Processes waiting until flush */
452 };
453
454 /**
455  * Lock data.
456  */
457 struct drm_lock_data {
458         struct drm_hw_lock *hw_lock;            /**< Hardware lock */
459         /** Private of lock holder's file (NULL=kernel) */
460         struct drm_file *file_priv;
461         wait_queue_head_t lock_queue;   /**< Queue of blocked processes */
462         unsigned long lock_time;        /**< Time of last lock in jiffies */
463         spinlock_t spinlock;
464         uint32_t kernel_waiters;
465         uint32_t user_waiters;
466         int idle_has_lock;
467 };
468
469 /**
470  * DMA data.
471  */
472 struct drm_device_dma {
473
474         struct drm_buf_entry bufs[DRM_MAX_ORDER + 1];   /**< buffers, grouped by their size order */
475         int buf_count;                  /**< total number of buffers */
476         struct drm_buf **buflist;               /**< Vector of pointers into drm_device_dma::bufs */
477         int seg_count;
478         int page_count;                 /**< number of pages */
479         unsigned long *pagelist;        /**< page list */
480         unsigned long byte_count;
481         enum {
482                 _DRM_DMA_USE_AGP = 0x01,
483                 _DRM_DMA_USE_SG = 0x02,
484                 _DRM_DMA_USE_FB = 0x04,
485                 _DRM_DMA_USE_PCI_RO = 0x08
486         } flags;
487
488 };
489
490 /**
491  * AGP memory entry.  Stored as a doubly linked list.
492  */
493 struct drm_agp_mem {
494         unsigned long handle;           /**< handle */
495         DRM_AGP_MEM *memory;
496         unsigned long bound;            /**< address */
497         int pages;
498         struct list_head head;
499 };
500
501 /**
502  * AGP data.
503  *
504  * \sa drm_agp_init() and drm_device::agp.
505  */
506 struct drm_agp_head {
507         DRM_AGP_KERN agp_info;          /**< AGP device information */
508         struct list_head memory;
509         unsigned long mode;             /**< AGP mode */
510 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11)
511         struct agp_bridge_data *bridge;
512 #endif
513         int enabled;                    /**< whether the AGP bus as been enabled */
514         int acquired;                   /**< whether the AGP device has been acquired */
515         unsigned long base;
516         int agp_mtrr;
517         int cant_use_aperture;
518         unsigned long page_mask;
519 };
520
521 /**
522  * Scatter-gather memory.
523  */
524 struct drm_sg_mem {
525         unsigned long handle;
526         void *virtual;
527         int pages;
528         struct page **pagelist;
529         dma_addr_t *busaddr;
530 };
531
532 struct drm_sigdata {
533         int context;
534         struct drm_hw_lock *lock;
535 };
536
537
538 /*
539  * Generic memory manager structs
540  */
541
542 struct drm_mm_node {
543         struct list_head fl_entry;
544         struct list_head ml_entry;
545         int free;
546         unsigned long start;
547         unsigned long size;
548         struct drm_mm *mm;
549         void *private;
550 };
551
552 struct drm_mm {
553         struct list_head fl_entry;
554         struct list_head ml_entry;
555 };
556
557
558 /**
559  * Mappings list
560  */
561 struct drm_map_list {
562         struct list_head head;          /**< list head */
563         struct drm_hash_item hash;
564         struct drm_map *map;                    /**< mapping */
565         uint64_t user_token;
566         struct drm_mm_node *file_offset_node;
567 };
568
569 typedef struct drm_map drm_local_map_t;
570
571 /**
572  * Context handle list
573  */
574 struct drm_ctx_list {
575         struct list_head head;          /**< list head */
576         drm_context_t handle;           /**< context handle */
577         struct drm_file *tag;           /**< associated fd private data */
578 };
579
580 struct drm_vbl_sig {
581         struct list_head head;
582         unsigned int sequence;
583         struct siginfo info;
584         struct task_struct *task;
585 };
586
587 /* location of GART table */
588 #define DRM_ATI_GART_MAIN 1
589 #define DRM_ATI_GART_FB   2
590
591 #define DRM_ATI_GART_PCI 1
592 #define DRM_ATI_GART_PCIE 2
593 #define DRM_ATI_GART_IGP 3
594
595 struct drm_ati_pcigart_info {
596         int gart_table_location;
597         int gart_reg_if;
598         void *addr;
599         dma_addr_t bus_addr;
600         dma_addr_t table_mask;
601         dma_addr_t member_mask;
602         struct drm_dma_handle *table_handle;
603         drm_local_map_t mapping;
604         int table_size;
605 };
606
607 #include "drm_objects.h"
608
609 /**
610  * DRM driver structure. This structure represent the common code for
611  * a family of cards. There will one drm_device for each card present
612  * in this family
613  */
614
615 struct drm_driver {
616         int (*load) (struct drm_device *, unsigned long flags);
617         int (*firstopen) (struct drm_device *);
618         int (*open) (struct drm_device *, struct drm_file *);
619         void (*preclose) (struct drm_device *, struct drm_file *file_priv);
620         void (*postclose) (struct drm_device *, struct drm_file *);
621         void (*lastclose) (struct drm_device *);
622         int (*unload) (struct drm_device *);
623         int (*suspend) (struct drm_device *, pm_message_t state);
624         int (*resume) (struct drm_device *);
625         int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
626         void (*dma_ready) (struct drm_device *);
627         int (*dma_quiescent) (struct drm_device *);
628         int (*context_ctor) (struct drm_device *dev, int context);
629         int (*context_dtor) (struct drm_device *dev, int context);
630         int (*kernel_context_switch) (struct drm_device *dev, int old,
631                                       int new);
632         void (*kernel_context_switch_unlock) (struct drm_device * dev);
633         /**
634          * get_vblank_counter - get raw hardware vblank counter
635          * @dev: DRM device
636          * @crtc: counter to fetch
637          *
638          * Driver callback for fetching a raw hardware vblank counter
639          * for @crtc.  If a device doesn't have a hardware counter, the
640          * driver can simply return the value of drm_vblank_count and
641          * make the enable_vblank() and disable_vblank() hooks into no-ops,
642          * leaving interrupts enabled at all times.
643          *
644          * Wraparound handling and loss of events due to modesetting is dealt
645          * with in the DRM core code.
646          *
647          * RETURNS
648          * Raw vblank counter value.
649          */
650         u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
651
652         /**
653          * enable_vblank - enable vblank interrupt events
654          * @dev: DRM device
655          * @crtc: which irq to enable
656          *
657          * Enable vblank interrupts for @crtc.  If the device doesn't have
658          * a hardware vblank counter, this routine should be a no-op, since
659          * interrupts will have to stay on to keep the count accurate.
660          *
661          * RETURNS
662          * Zero on success, appropriate errno if the given @crtc's vblank
663          * interrupt cannot be enabled.
664          */
665         int (*enable_vblank) (struct drm_device *dev, int crtc);
666
667         /**
668          * disable_vblank - disable vblank interrupt events
669          * @dev: DRM device
670          * @crtc: which irq to enable
671          *
672          * Disable vblank interrupts for @crtc.  If the device doesn't have
673          * a hardware vblank counter, this routine should be a no-op, since
674          * interrupts will have to stay on to keep the count accurate.
675          */
676         void (*disable_vblank) (struct drm_device *dev, int crtc);
677         int (*dri_library_name) (struct drm_device *dev, char * buf);
678
679         /**
680          * Called by \c drm_device_is_agp.  Typically used to determine if a
681          * card is really attached to AGP or not.
682          *
683          * \param dev  DRM device handle
684          *
685          * \returns
686          * One of three values is returned depending on whether or not the
687          * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
688          * (return of 1), or may or may not be AGP (return of 2).
689          */
690         int (*device_is_agp) (struct drm_device *dev);
691
692 /* these have to be filled in */
693          irqreturn_t(*irq_handler) (DRM_IRQ_ARGS);
694         void (*irq_preinstall) (struct drm_device *dev);
695         int (*irq_postinstall) (struct drm_device *dev);
696         void (*irq_uninstall) (struct drm_device *dev);
697         void (*reclaim_buffers) (struct drm_device *dev,
698                                  struct drm_file *file_priv);
699         void (*reclaim_buffers_locked) (struct drm_device *dev,
700                                         struct drm_file *file_priv);
701         void (*reclaim_buffers_idlelocked) (struct drm_device *dev,
702                                             struct drm_file *file_priv);
703         unsigned long (*get_map_ofs) (struct drm_map *map);
704         unsigned long (*get_reg_ofs) (struct drm_device *dev);
705         void (*set_version) (struct drm_device *dev,
706                              struct drm_set_version *sv);
707
708         struct drm_fence_driver *fence_driver;
709         struct drm_bo_driver *bo_driver;
710
711         int major;
712         int minor;
713         int patchlevel;
714         char *name;
715         char *desc;
716         char *date;
717
718 /* variables */
719         u32 driver_features;
720         int dev_priv_size;
721         struct drm_ioctl_desc *ioctls;
722         int num_ioctls;
723         struct file_operations fops;
724         struct pci_driver pci_driver;
725 };
726
727 #define DRM_MINOR_UNASSIGNED 0
728 #define DRM_MINOR_LEGACY 1
729
730 /**
731  * DRM minor structure. This structure represents a drm minor number.
732  */
733 struct drm_minor {
734         int index;                      /**< Minor device number */
735         int type;                       /**< Control or render */
736         dev_t device;                   /**< Device number for mknod */
737         struct device kdev;             /**< Linux device */
738         struct drm_device *dev;
739         struct proc_dir_entry *dev_root;  /**< proc directory entry */
740         struct class_device *dev_class;
741 };
742
743
744 /**
745  * DRM device structure. This structure represent a complete card that
746  * may contain multiple heads.
747  */
748 struct drm_device {
749         char *unique;                   /**< Unique identifier: e.g., busid */
750         int unique_len;                 /**< Length of unique field */
751         char *devname;                  /**< For /proc/interrupts */
752         int if_version;                 /**< Highest interface version set */
753
754         int blocked;                    /**< Blocked due to VC switch? */
755
756         /** \name Locks */
757         /*@{ */
758         spinlock_t count_lock;          /**< For inuse, drm_device::open_count, drm_device::buf_use */
759         struct mutex struct_mutex;      /**< For others */
760         /*@} */
761
762         /** \name Usage Counters */
763         /*@{ */
764         int open_count;                 /**< Outstanding files open */
765         atomic_t ioctl_count;           /**< Outstanding IOCTLs pending */
766         atomic_t vma_count;             /**< Outstanding vma areas open */
767         int buf_use;                    /**< Buffers in use -- cannot alloc */
768         atomic_t buf_alloc;             /**< Buffer allocation in progress */
769         /*@} */
770
771         /** \name Performance counters */
772         /*@{ */
773         unsigned long counters;
774         enum drm_stat_type types[15];
775         atomic_t counts[15];
776         /*@} */
777
778         /** \name Authentication */
779         /*@{ */
780         struct list_head filelist;
781         struct drm_open_hash magiclist;
782         struct list_head magicfree;
783         /*@} */
784
785         /** \name Memory management */
786         /*@{ */
787         struct list_head maplist;       /**< Linked list of regions */
788         int map_count;                  /**< Number of mappable regions */
789         struct drm_open_hash map_hash;       /**< User token hash table for maps */
790         struct drm_mm offset_manager;        /**< User token manager */
791         struct drm_open_hash object_hash;    /**< User token hash table for objects */
792         struct address_space *dev_mapping;  /**< For unmap_mapping_range() */
793         struct page *ttm_dummy_page;
794
795         /** \name Context handle management */
796         /*@{ */
797         struct list_head ctxlist;       /**< Linked list of context handles */
798         int ctx_count;                  /**< Number of context handles */
799         struct mutex ctxlist_mutex;     /**< For ctxlist */
800
801         struct idr ctx_idr;
802
803         struct list_head vmalist;       /**< List of vmas (for debugging) */
804         struct drm_lock_data lock;              /**< Information on hardware lock */
805         /*@} */
806
807         /** \name DMA queues (contexts) */
808         /*@{ */
809         int queue_count;                /**< Number of active DMA queues */
810         int queue_reserved;             /**< Number of reserved DMA queues */
811         int queue_slots;                /**< Actual length of queuelist */
812         struct drm_queue **queuelist;   /**< Vector of pointers to DMA queues */
813         struct drm_device_dma *dma;             /**< Optional pointer for DMA support */
814         /*@} */
815
816         /** \name Context support */
817         /*@{ */
818         int irq;                        /**< Interrupt used by board */
819         int irq_enabled;                /**< True if irq handler is enabled */
820         __volatile__ long context_flag; /**< Context swapping flag */
821         __volatile__ long interrupt_flag; /**< Interruption handler flag */
822         __volatile__ long dma_flag;     /**< DMA dispatch flag */
823         struct timer_list timer;        /**< Timer for delaying ctx switch */
824         wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
825         int last_checked;               /**< Last context checked for DMA */
826         int last_context;               /**< Last current context */
827         unsigned long last_switch;      /**< jiffies at last context switch */
828         /*@} */
829
830         struct work_struct work;
831
832         /** \name VBLANK IRQ support */
833         /*@{ */
834
835         /*
836          * At load time, disabling the vblank interrupt won't be allowed since
837          * old clients may not call the modeset ioctl and therefore misbehave.
838          * Once the modeset ioctl *has* been called though, we can safely
839          * disable them when unused.
840          */
841         int vblank_disable_allowed;
842
843         wait_queue_head_t *vbl_queue;   /**< VBLANK wait queue */
844         atomic_t *_vblank_count;        /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
845         spinlock_t vbl_lock;
846         struct list_head *vbl_sigs;             /**< signal list to send on VBLANK */
847         atomic_t vbl_signal_pending;    /* number of signals pending on all crtcs*/
848         atomic_t *vblank_refcount;      /* number of users of vblank interrupts per crtc */
849         u32 *last_vblank;               /* protected by dev->vbl_lock, used */
850                                         /* for wraparound handling */
851         int *vblank_enabled;            /* so we don't call enable more than
852                                            once per disable */
853         int *vblank_inmodeset;          /* Display driver is setting mode */
854         struct timer_list vblank_disable_timer;
855
856         u32 max_vblank_count;           /**< size of vblank counter register */
857         spinlock_t tasklet_lock;        /**< For drm_locked_tasklet */
858         void (*locked_tasklet_func)(struct drm_device *dev);
859
860         /*@} */
861         cycles_t ctx_start;
862         cycles_t lck_start;
863
864         struct fasync_struct *buf_async;/**< Processes waiting for SIGIO */
865         wait_queue_head_t buf_readers;  /**< Processes waiting to read */
866         wait_queue_head_t buf_writers;  /**< Processes waiting to ctx switch */
867
868         struct drm_agp_head *agp;               /**< AGP data */
869
870         struct pci_dev *pdev;           /**< PCI device structure */
871         int pci_vendor;                 /**< PCI vendor id */
872         int pci_device;                 /**< PCI device id */
873 #ifdef __alpha__
874         struct pci_controller *hose;
875 #endif
876         int num_crtcs;                  /**< Number of CRTCs on this device */
877         struct drm_sg_mem *sg;          /**< Scatter gather memory */
878         void *dev_private;              /**< device private data */
879         struct drm_sigdata sigdata;             /**< For block_all_signals */
880         sigset_t sigmask;
881
882         struct drm_driver *driver;
883         drm_local_map_t *agp_buffer_map;
884         unsigned int agp_buffer_token;
885         struct drm_minor *primary;              /**< render type primary screen head */
886
887         struct drm_fence_manager fm;
888         struct drm_buffer_manager bm;
889
890         /** \name Drawable information */
891         /*@{ */
892         spinlock_t drw_lock;
893         struct idr drw_idr;
894         /*@} */
895 };
896
897 #if __OS_HAS_AGP
898 struct drm_agp_ttm_backend {
899         struct drm_ttm_backend backend;
900         DRM_AGP_MEM *mem;
901         struct agp_bridge_data *bridge;
902         int populated;
903 };
904 #endif
905
906
907 static __inline__ int drm_core_check_feature(struct drm_device *dev,
908                                              int feature)
909 {
910         return ((dev->driver->driver_features & feature) ? 1 : 0);
911 }
912
913 #ifdef __alpha__
914 #define drm_get_pci_domain(dev) dev->hose->index
915 #else
916 #define drm_get_pci_domain(dev) 0
917 #endif
918
919 #if __OS_HAS_AGP
920 static inline int drm_core_has_AGP(struct drm_device *dev)
921 {
922         return drm_core_check_feature(dev, DRIVER_USE_AGP);
923 }
924 #else
925 #define drm_core_has_AGP(dev) (0)
926 #endif
927
928 #if __OS_HAS_MTRR
929 static inline int drm_core_has_MTRR(struct drm_device *dev)
930 {
931         return drm_core_check_feature(dev, DRIVER_USE_MTRR);
932 }
933
934 #define DRM_MTRR_WC             MTRR_TYPE_WRCOMB
935
936 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
937                                unsigned int flags)
938 {
939         return mtrr_add(offset, size, flags, 1);
940 }
941
942 static inline int drm_mtrr_del(int handle, unsigned long offset,
943                                unsigned long size, unsigned int flags)
944 {
945         return mtrr_del(handle, offset, size);
946 }
947
948 #else
949 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
950                                unsigned int flags)
951 {
952         return -ENODEV;
953 }
954
955 static inline int drm_mtrr_del(int handle, unsigned long offset,
956                                unsigned long size, unsigned int flags)
957 {
958         return -ENODEV;
959 }
960
961 #define drm_core_has_MTRR(dev) (0)
962 #define DRM_MTRR_WC             0
963 #endif
964
965
966 /******************************************************************/
967 /** \name Internal function definitions */
968 /*@{*/
969
970                                 /* Driver support (drm_drv.h) */
971 extern int drm_fb_loaded;
972 extern int drm_init(struct drm_driver *driver,
973                               struct pci_device_id *pciidlist);
974 extern void drm_exit(struct drm_driver *driver);
975 extern void drm_cleanup_pci(struct pci_dev *pdev);
976 extern int drm_ioctl(struct inode *inode, struct file *filp,
977                      unsigned int cmd, unsigned long arg);
978 extern long drm_unlocked_ioctl(struct file *filp,
979                                unsigned int cmd, unsigned long arg);
980 extern long drm_compat_ioctl(struct file *filp,
981                              unsigned int cmd, unsigned long arg);
982
983 extern int drm_lastclose(struct drm_device *dev);
984
985                                 /* Device support (drm_fops.h) */
986 extern int drm_open(struct inode *inode, struct file *filp);
987 extern int drm_stub_open(struct inode *inode, struct file *filp);
988 extern int drm_fasync(int fd, struct file *filp, int on);
989 extern int drm_release(struct inode *inode, struct file *filp);
990 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
991
992                                 /* Mapping support (drm_vm.h) */
993 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
994 extern unsigned long drm_core_get_map_ofs(struct drm_map * map);
995 extern unsigned long drm_core_get_reg_ofs(struct drm_device *dev);
996 extern pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma);
997
998                                 /* Memory management support (drm_memory.h) */
999 #include "drm_memory.h"
1000 extern void drm_mem_init(void);
1001 extern int drm_mem_info(char *buf, char **start, off_t offset,
1002                         int request, int *eof, void *data);
1003 extern void *drm_calloc(size_t nmemb, size_t size, int area);
1004 extern void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area);
1005 extern unsigned long drm_alloc_pages(int order, int area);
1006 extern void drm_free_pages(unsigned long address, int order, int area);
1007 extern DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type);
1008 extern int drm_free_agp(DRM_AGP_MEM * handle, int pages);
1009 extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start);
1010 extern int drm_unbind_agp(DRM_AGP_MEM * handle);
1011
1012 extern void drm_free_memctl(size_t size);
1013 extern int drm_alloc_memctl(size_t size);
1014 extern void drm_query_memctl(uint64_t *cur_used,
1015                              uint64_t *emer_used,
1016                              uint64_t *low_threshold,
1017                              uint64_t *high_threshold,
1018                              uint64_t *emer_threshold);
1019 extern void drm_init_memctl(size_t low_threshold,
1020                             size_t high_threshold,
1021                             size_t unit_size);
1022
1023                                 /* Misc. IOCTL support (drm_ioctl.h) */
1024 extern int drm_irq_by_busid(struct drm_device *dev, void *data,
1025                             struct drm_file *file_priv);
1026 extern int drm_getunique(struct drm_device *dev, void *data,
1027                          struct drm_file *file_priv);
1028 extern int drm_setunique(struct drm_device *dev, void *data,
1029                          struct drm_file *file_priv);
1030 extern int drm_getmap(struct drm_device *dev, void *data,
1031                       struct drm_file *file_priv);
1032 extern int drm_getclient(struct drm_device *dev, void *data,
1033                          struct drm_file *file_priv);
1034 extern int drm_getstats(struct drm_device *dev, void *data,
1035                         struct drm_file *file_priv);
1036 extern int drm_setversion(struct drm_device *dev, void *data,
1037                           struct drm_file *file_priv);
1038 extern int drm_noop(struct drm_device *dev, void *data,
1039                     struct drm_file *file_priv);
1040
1041                                 /* Context IOCTL support (drm_context.h) */
1042 extern int drm_resctx(struct drm_device *dev, void *data,
1043                       struct drm_file *file_priv);
1044 extern int drm_addctx(struct drm_device *dev, void *data,
1045                       struct drm_file *file_priv);
1046 extern int drm_modctx(struct drm_device *dev, void *data,
1047                       struct drm_file *file_priv);
1048 extern int drm_getctx(struct drm_device *dev, void *data,
1049                       struct drm_file *file_priv);
1050 extern int drm_switchctx(struct drm_device *dev, void *data,
1051                          struct drm_file *file_priv);
1052 extern int drm_newctx(struct drm_device *dev, void *data,
1053                       struct drm_file *file_priv);
1054 extern int drm_rmctx(struct drm_device *dev, void *data,
1055                      struct drm_file *file_priv);
1056
1057 extern int drm_ctxbitmap_init(struct drm_device *dev);
1058 extern void drm_ctxbitmap_cleanup(struct drm_device *dev);
1059 extern void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1060
1061 extern int drm_setsareactx(struct drm_device *dev, void *data,
1062                            struct drm_file *file_priv);
1063 extern int drm_getsareactx(struct drm_device *dev, void *data,
1064                            struct drm_file *file_priv);
1065
1066                                 /* Drawable IOCTL support (drm_drawable.h) */
1067 extern int drm_adddraw(struct drm_device *dev, void *data,
1068                        struct drm_file *file_priv);
1069 extern int drm_rmdraw(struct drm_device *dev, void *data,
1070                       struct drm_file *file_priv);
1071 extern int drm_update_drawable_info(struct drm_device *dev, void *data,
1072                                     struct drm_file *file_priv);
1073 extern struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1074                                                        drm_drawable_t id);
1075 extern void drm_drawable_free_all(struct drm_device *dev);
1076
1077                                 /* Authentication IOCTL support (drm_auth.h) */
1078 extern int drm_getmagic(struct drm_device *dev, void *data,
1079                         struct drm_file *file_priv);
1080 extern int drm_authmagic(struct drm_device *dev, void *data,
1081                          struct drm_file *file_priv);
1082
1083                                 /* Locking IOCTL support (drm_lock.h) */
1084 extern int drm_lock(struct drm_device *dev, void *data,
1085                     struct drm_file *file_priv);
1086 extern int drm_unlock(struct drm_device *dev, void *data,
1087                       struct drm_file *file_priv);
1088 extern int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
1089 extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context);
1090 extern void drm_idlelock_take(struct drm_lock_data *lock_data);
1091 extern void drm_idlelock_release(struct drm_lock_data *lock_data);
1092
1093 /*
1094  * These are exported to drivers so that they can implement fencing using
1095  * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1096  */
1097
1098 extern int drm_i_have_hw_lock(struct drm_device *dev,
1099                               struct drm_file *file_priv);
1100
1101                                 /* Buffer management support (drm_bufs.h) */
1102 extern int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc * request);
1103 extern int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc * request);
1104 extern int drm_addbufs_fb (struct drm_device *dev, struct drm_buf_desc * request);
1105 extern int drm_addmap(struct drm_device *dev, unsigned int offset,
1106                       unsigned int size, enum drm_map_type type,
1107                       enum drm_map_flags flags, drm_local_map_t ** map_ptr);
1108 extern int drm_addmap_ioctl(struct drm_device *dev, void *data,
1109                             struct drm_file *file_priv);
1110 extern int drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1111 extern int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map);
1112 extern int drm_rmmap_ioctl(struct drm_device *dev, void *data,
1113                            struct drm_file *file_priv);
1114 extern int drm_addbufs(struct drm_device *dev, void *data,
1115                        struct drm_file *file_priv);
1116 extern int drm_infobufs(struct drm_device *dev, void *data,
1117                         struct drm_file *file_priv);
1118 extern int drm_markbufs(struct drm_device *dev, void *data,
1119                         struct drm_file *file_priv);
1120 extern int drm_freebufs(struct drm_device *dev, void *data,
1121                         struct drm_file *file_priv);
1122 extern int drm_mapbufs(struct drm_device *dev, void *data,
1123                        struct drm_file *file_priv);
1124 extern int drm_order(unsigned long size);
1125 extern unsigned long drm_get_resource_start(struct drm_device *dev,
1126                                             unsigned int resource);
1127 extern unsigned long drm_get_resource_len(struct drm_device *dev,
1128                                           unsigned int resource);
1129 extern struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
1130                                                   drm_local_map_t *map);
1131
1132
1133                                 /* DMA support (drm_dma.h) */
1134 extern int drm_dma_setup(struct drm_device *dev);
1135 extern void drm_dma_takedown(struct drm_device *dev);
1136 extern void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf);
1137 extern void drm_core_reclaim_buffers(struct drm_device *dev,
1138                                      struct drm_file *filp);
1139
1140                                 /* IRQ support (drm_irq.h) */
1141 extern int drm_control(struct drm_device *dev, void *data,
1142                        struct drm_file *file_priv);
1143 extern irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1144 extern int drm_irq_install(struct drm_device *dev);
1145 extern int drm_irq_uninstall(struct drm_device *dev);
1146 extern void drm_driver_irq_preinstall(struct drm_device *dev);
1147 extern void drm_driver_irq_postinstall(struct drm_device *dev);
1148 extern void drm_driver_irq_uninstall(struct drm_device *dev);
1149
1150 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1151 extern int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *filp);
1152 extern int drm_vblank_wait(struct drm_device * dev, unsigned int *vbl_seq);
1153 extern void drm_locked_tasklet(struct drm_device *dev, void(*func)(struct drm_device*));
1154 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1155 extern void drm_handle_vblank(struct drm_device *dev, int crtc);
1156 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1157 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1158
1159                                 /* Modesetting support */
1160 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1161                            struct drm_file *file_priv);
1162
1163                                 /* AGP/GART support (drm_agpsupport.h) */
1164 extern struct drm_agp_head *drm_agp_init(struct drm_device *dev);
1165 extern int drm_agp_acquire(struct drm_device *dev);
1166 extern int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1167                                  struct drm_file *file_priv);
1168 extern int drm_agp_release(struct drm_device *dev);
1169 extern int drm_agp_release_ioctl(struct drm_device *dev, void *data,
1170                                  struct drm_file *file_priv);
1171 extern int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1172 extern int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1173                                 struct drm_file *file_priv);
1174 extern int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info);
1175 extern int drm_agp_info_ioctl(struct drm_device *dev, void *data,
1176                         struct drm_file *file_priv);
1177 extern int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1178 extern int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1179                          struct drm_file *file_priv);
1180 extern int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1181 extern int drm_agp_free_ioctl(struct drm_device *dev, void *data,
1182                         struct drm_file *file_priv);
1183 extern int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1184 extern int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1185                           struct drm_file *file_priv);
1186 extern int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1187 extern int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1188                         struct drm_file *file_priv);
1189 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
1190 extern DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type);
1191 #else
1192 extern DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type);
1193 #endif
1194 extern int drm_agp_free_memory(DRM_AGP_MEM * handle);
1195 extern int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start);
1196 extern int drm_agp_unbind_memory(DRM_AGP_MEM * handle);
1197 extern struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev);
1198 extern void drm_agp_chipset_flush(struct drm_device *dev);
1199                                 /* Stub support (drm_stub.h) */
1200 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
1201                      struct drm_driver *driver);
1202 extern int drm_put_dev(struct drm_device *dev);
1203 extern int drm_put_minor(struct drm_minor **minor);
1204 extern unsigned int drm_debug; /* 1 to enable debug output */
1205
1206 extern struct class *drm_class;
1207 extern struct proc_dir_entry *drm_proc_root;
1208
1209 extern struct idr drm_minors_idr;
1210
1211 extern drm_local_map_t *drm_getsarea(struct drm_device *dev);
1212
1213                                 /* Proc support (drm_proc.h) */
1214 int drm_proc_init(struct drm_minor *minor, int minor_id,
1215                   struct proc_dir_entry *root);
1216 int drm_proc_cleanup(struct drm_minor *minor, struct proc_dir_entry *root);
1217
1218                                 /* Scatter Gather Support (drm_scatter.h) */
1219 extern void drm_sg_cleanup(struct drm_sg_mem * entry);
1220 extern int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1221                         struct drm_file *file_priv);
1222 extern int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1223 extern int drm_sg_free(struct drm_device *dev, void *data,
1224                        struct drm_file *file_priv);
1225
1226                                /* ATI PCIGART support (ati_pcigart.h) */
1227 extern int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1228 extern int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1229
1230 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1231                            size_t align, dma_addr_t maxaddr);
1232 extern void __drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1233 extern void drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1234
1235                                /* sysfs support (drm_sysfs.c) */
1236 struct drm_sysfs_class;
1237 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1238 extern void drm_sysfs_destroy(void);
1239 extern int drm_sysfs_device_add(struct drm_minor *minor);
1240 extern void drm_sysfs_device_remove(struct drm_minor *minor);
1241
1242 /*
1243  * Basic memory manager support (drm_mm.c)
1244  */
1245
1246 extern struct drm_mm_node * drm_mm_get_block(struct drm_mm_node * parent, unsigned long size,
1247                                                unsigned alignment);
1248 extern void drm_mm_put_block(struct drm_mm_node *cur);
1249 extern struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm, unsigned long size,
1250                                                 unsigned alignment, int best_match);
1251 extern int drm_mm_init(struct drm_mm *mm, unsigned long start, unsigned long size);
1252 extern void drm_mm_takedown(struct drm_mm *mm);
1253 extern int drm_mm_clean(struct drm_mm *mm);
1254 extern unsigned long drm_mm_tail_space(struct drm_mm *mm);
1255 extern int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size);
1256 extern int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size);
1257
1258 static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
1259 {
1260         return block->mm;
1261 }
1262
1263 extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev);
1264 extern void drm_core_ioremap_wc(struct drm_map *map, struct drm_device *dev);
1265 extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev);
1266
1267 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev,
1268                                                    unsigned int token)
1269 {
1270         struct drm_map_list *_entry;
1271         list_for_each_entry(_entry, &dev->maplist, head)
1272                 if (_entry->user_token == token)
1273                         return _entry->map;
1274         return NULL;
1275 }
1276
1277 static __inline__ int drm_device_is_agp(struct drm_device *dev)
1278 {
1279         if ( dev->driver->device_is_agp != NULL ) {
1280                 int err = (*dev->driver->device_is_agp)(dev);
1281
1282                 if (err != 2) {
1283                         return err;
1284                 }
1285         }
1286
1287         return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1288 }
1289
1290 static __inline__ int drm_device_is_pcie(struct drm_device *dev)
1291 {
1292         return pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
1293 }
1294
1295 static __inline__ void drm_core_dropmap(struct drm_map *map)
1296 {
1297 }
1298
1299 #ifndef DEBUG_MEMORY
1300 /** Wrapper around kmalloc() */
1301 static __inline__ void *drm_alloc(size_t size, int area)
1302 {
1303         return kmalloc(size, GFP_KERNEL);
1304 }
1305
1306 /** Wrapper around kfree() */
1307 static __inline__ void drm_free(void *pt, size_t size, int area)
1308 {
1309         kfree(pt);
1310 }
1311 #else
1312 extern void *drm_alloc(size_t size, int area);
1313 extern void drm_free(void *pt, size_t size, int area);
1314 #endif
1315
1316 /*
1317  * Accounting variants of standard calls.
1318  */
1319
1320 static inline void *drm_ctl_alloc(size_t size, int area)
1321 {
1322         void *ret;
1323         if (drm_alloc_memctl(size))
1324                 return NULL;
1325         ret = drm_alloc(size, area);
1326         if (!ret)
1327                 drm_free_memctl(size);
1328         return ret;
1329 }
1330
1331 static inline void *drm_ctl_calloc(size_t nmemb, size_t size, int area)
1332 {
1333         void *ret;
1334
1335         if (drm_alloc_memctl(nmemb*size))
1336                 return NULL;
1337         ret = drm_calloc(nmemb, size, area);
1338         if (!ret)
1339                 drm_free_memctl(nmemb*size);
1340         return ret;
1341 }
1342
1343 static inline void drm_ctl_free(void *pt, size_t size, int area)
1344 {
1345         drm_free(pt, size, area);
1346         drm_free_memctl(size);
1347 }
1348
1349 /*@}*/
1350
1351 #endif                          /* __KERNEL__ */
1352 #endif