OSDN Git Service

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