3 * Private header for Direct Rendering Manager
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All rights reserved.
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:
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
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.
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>
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 */
56 #include <linux/pagemap.h>
57 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
58 #include <linux/mutex.h>
60 #if defined(__alpha__) || defined(__powerpc__)
61 #include <asm/pgtable.h> /* For pte_wrprotect */
65 #include <asm/uaccess.h>
69 #if defined(CONFIG_AGP) || defined(CONFIG_AGP_MODULE)
71 #include <linux/types.h>
72 #include <linux/agp_backend.h>
74 #include <linux/workqueue.h>
75 #include <linux/poll.h>
76 #include <asm/pgalloc.h>
78 #include <linux/slab.h>
79 #include <linux/idr.h>
81 #define __OS_HAS_AGP (defined(CONFIG_AGP) || (defined(CONFIG_AGP_MODULE) && defined(MODULE)))
82 #define __OS_HAS_MTRR (defined(CONFIG_MTRR))
84 #include "drm_os_linux.h"
85 #include "drm_hashtab.h"
89 /* If you want the memory alloc debug functionality, change define below */
90 /* #define DEBUG_MEMORY */
92 /***********************************************************************/
93 /** \name DRM template customization defaults */
96 /* driver capabilities and requirements mask */
97 #define DRIVER_USE_AGP 0x1
98 #define DRIVER_REQUIRE_AGP 0x2
99 #define DRIVER_USE_MTRR 0x4
100 #define DRIVER_PCI_DMA 0x8
101 #define DRIVER_SG 0x10
102 #define DRIVER_HAVE_DMA 0x20
103 #define DRIVER_HAVE_IRQ 0x40
104 #define DRIVER_IRQ_SHARED 0x80
105 #define DRIVER_DMA_QUEUE 0x100
106 #define DRIVER_FB_DMA 0x200
111 /***********************************************************************/
112 /** \name Begin the DRM... */
115 #define DRM_DEBUG_CODE 2 /**< Include debugging code if > 1, then
116 also include looping detection. */
118 #define DRM_MAGIC_HASH_ORDER 4 /**< Size of key hash table. Must be power of 2. */
119 #define DRM_KERNEL_CONTEXT 0 /**< Change drm_resctx if changed */
120 #define DRM_RESERVED_CONTEXTS 1 /**< Change drm_resctx if changed */
121 #define DRM_LOOPING_LIMIT 5000000
122 #define DRM_TIME_SLICE (HZ/20) /**< Time slice for GLXContexts */
123 #define DRM_LOCK_SLICE 1 /**< Time slice for lock, in jiffies */
125 #define DRM_FLAG_DEBUG 0x01
127 #define DRM_MEM_DMA 0
128 #define DRM_MEM_SAREA 1
129 #define DRM_MEM_DRIVER 2
130 #define DRM_MEM_MAGIC 3
131 #define DRM_MEM_IOCTLS 4
132 #define DRM_MEM_MAPS 5
133 #define DRM_MEM_VMAS 6
134 #define DRM_MEM_BUFS 7
135 #define DRM_MEM_SEGS 8
136 #define DRM_MEM_PAGES 9
137 #define DRM_MEM_FILES 10
138 #define DRM_MEM_QUEUES 11
139 #define DRM_MEM_CMDS 12
140 #define DRM_MEM_MAPPINGS 13
141 #define DRM_MEM_BUFLISTS 14
142 #define DRM_MEM_AGPLISTS 15
143 #define DRM_MEM_TOTALAGP 16
144 #define DRM_MEM_BOUNDAGP 17
145 #define DRM_MEM_CTXBITMAP 18
146 #define DRM_MEM_STUB 19
147 #define DRM_MEM_SGLISTS 20
148 #define DRM_MEM_CTXLIST 21
149 #define DRM_MEM_MM 22
150 #define DRM_MEM_HASHTAB 23
151 #define DRM_MEM_OBJECTS 24
152 #define DRM_MEM_FENCE 25
153 #define DRM_MEM_TTM 26
154 #define DRM_MEM_BUFOBJ 27
156 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
157 #define DRM_MAP_HASH_OFFSET 0x10000000
158 #define DRM_MAP_HASH_ORDER 12
159 #define DRM_OBJECT_HASH_ORDER 12
160 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
161 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
162 #define DRM_MM_INIT_MAX_PAGES 256
166 #include "drm_compat.h"
168 /***********************************************************************/
169 /** \name Macros to make printk easier */
175 * \param fmt printf() like format string.
176 * \param arg arguments
178 #define DRM_ERROR(fmt, arg...) \
179 printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* " fmt , __FUNCTION__ , ##arg)
182 * Memory error output.
184 * \param area memory area where the error occurred.
185 * \param fmt printf() like format string.
186 * \param arg arguments
188 #define DRM_MEM_ERROR(area, fmt, arg...) \
189 printk(KERN_ERR "[" DRM_NAME ":%s:%s] *ERROR* " fmt , __FUNCTION__, \
190 drm_mem_stats[area].name , ##arg)
191 #define DRM_INFO(fmt, arg...) printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
196 * \param fmt printf() like format string.
197 * \param arg arguments
200 #define DRM_DEBUG(fmt, arg...) \
204 "[" DRM_NAME ":%s] " fmt , \
205 __FUNCTION__ , ##arg); \
208 #define DRM_DEBUG(fmt, arg...) do { } while (0)
211 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
213 #define DRM_PROC_PRINT(fmt, arg...) \
214 len += sprintf(&buf[len], fmt , ##arg); \
215 if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
217 #define DRM_PROC_PRINT_RET(ret, fmt, arg...) \
218 len += sprintf(&buf[len], fmt , ##arg); \
219 if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
223 /***********************************************************************/
224 /** \name Internal types and structures */
227 #define DRM_ARRAY_SIZE(x) ARRAY_SIZE(x)
228 #define DRM_MIN(a,b) min(a,b)
229 #define DRM_MAX(a,b) max(a,b)
231 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
232 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
233 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
235 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
237 * Get the private SAREA mapping.
239 * \param _dev DRM device.
240 * \param _ctx context number.
241 * \param _map output mapping.
243 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do { \
244 (_map) = (_dev)->context_sareas[_ctx]; \
248 * Test that the hardware lock is held by the caller, returning otherwise.
250 * \param dev DRM device.
251 * \param file_priv DRM file private pointer of the caller.
253 #define LOCK_TEST_WITH_RETURN( dev, file_priv ) \
255 if ( !_DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ) || \
256 dev->lock.file_priv != file_priv ) { \
257 DRM_ERROR( "%s called without lock held, held %d owner %p %p\n",\
258 __FUNCTION__, _DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ),\
259 dev->lock.file_priv, file_priv ); \
265 * Copy and IOCTL return string to user space
267 #define DRM_COPY( name, value ) \
268 len = strlen( value ); \
269 if ( len > name##_len ) len = name##_len; \
270 name##_len = strlen( value ); \
271 if ( len && name ) { \
272 if ( copy_to_user( name, value, len ) ) \
280 * Ioctl function type.
282 * \param dev DRM device structure
283 * \param data pointer to kernel-space stored data, copied in and out according
284 * to ioctl description.
285 * \param file_priv DRM file private pointer.
287 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
288 struct drm_file *file_priv);
290 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
294 #define DRM_MASTER 0x2
295 #define DRM_ROOT_ONLY 0x4
297 struct drm_ioctl_desc {
303 * Creates a driver or general drm_ioctl_desc array entry for the given
304 * ioctl, for use by drm_ioctl().
306 #define DRM_IOCTL_DEF(ioctl, func, flags) \
307 [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
309 struct drm_magic_entry {
310 struct list_head head;
311 struct drm_hash_item hash_item;
312 struct drm_file *priv;
315 struct drm_vma_entry {
316 struct list_head head;
317 struct vm_area_struct *vma;
325 int idx; /**< Index into master buflist */
326 int total; /**< Buffer size */
327 int order; /**< log-base-2(total) */
328 int used; /**< Amount of buffer in use (for DMA) */
329 unsigned long offset; /**< Byte offset (used internally) */
330 void *address; /**< Address of buffer */
331 unsigned long bus_address; /**< Bus address of buffer */
332 struct drm_buf *next; /**< Kernel-only: used for free list */
333 __volatile__ int waiting; /**< On kernel DMA queue */
334 __volatile__ int pending; /**< On hardware DMA queue */
335 wait_queue_head_t dma_wait; /**< Processes waiting */
336 struct drm_file *file_priv; /**< Private of holding file descr */
337 int context; /**< Kernel queue for this buffer */
338 int while_locked; /**< Dispatch this buffer while locked */
346 } list; /**< Which list we're on */
348 int dev_priv_size; /**< Size of buffer private storage */
349 void *dev_private; /**< Per-buffer private storage */
352 /** bufs is one longer than it has to be */
353 struct drm_waitlist {
354 int count; /**< Number of possible buffers */
355 struct drm_buf **bufs; /**< List of pointers to buffers */
356 struct drm_buf **rp; /**< Read pointer */
357 struct drm_buf **wp; /**< Write pointer */
358 struct drm_buf **end; /**< End pointer */
359 spinlock_t read_lock;
360 spinlock_t write_lock;
363 struct drm_freelist {
364 int initialized; /**< Freelist in use */
365 atomic_t count; /**< Number of free buffers */
366 struct drm_buf *next; /**< End pointer */
368 wait_queue_head_t waiting; /**< Processes waiting on free bufs */
369 int low_mark; /**< Low water mark */
370 int high_mark; /**< High water mark */
371 atomic_t wfh; /**< If waiting for high mark */
375 typedef struct drm_dma_handle {
382 * Buffer entry. There is one of this for each buffer size order.
384 struct drm_buf_entry {
385 int buf_size; /**< size */
386 int buf_count; /**< number of buffers */
387 struct drm_buf *buflist; /**< buffer list */
390 struct drm_dma_handle **seglist;
391 struct drm_freelist freelist;
395 * This should be small enough to allow the use of kmalloc for hash tables
396 * instead of vmalloc.
399 #define DRM_FILE_HASH_ORDER 8
407 /** File private data */
415 unsigned long ioctl_count;
416 struct list_head lhead;
417 struct drm_head *head;
418 int remove_auth_on_close;
419 unsigned long lock_count;
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
428 struct list_head refd_objects;
430 struct drm_open_hash refd_object_hash[_DRM_NO_REF_TYPES];
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 */
445 atomic_t total_queued; /**< Total queued statistic */
446 atomic_t total_flushed; /**< Total flushes statistic */
447 atomic_t total_locks; /**< Total locks statistics */
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 */
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 */
464 uint32_t kernel_waiters;
465 uint32_t user_waiters;
472 struct drm_device_dma {
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 */
478 int page_count; /**< number of pages */
479 unsigned long *pagelist; /**< page list */
480 unsigned long byte_count;
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
491 * AGP memory entry. Stored as a doubly linked list.
494 unsigned long handle; /**< handle */
496 unsigned long bound; /**< address */
498 struct list_head head;
504 * \sa drm_agp_init)() and drm_device::agp.
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;
513 int enabled; /**< whether the AGP bus as been enabled */
514 int acquired; /**< whether the AGP device has been acquired */
517 int cant_use_aperture;
518 unsigned long page_mask;
522 * Scatter-gather memory.
525 unsigned long handle;
528 struct page **pagelist;
534 struct drm_hw_lock *lock;
539 * Generic memory manager structs
543 struct list_head fl_entry;
544 struct list_head ml_entry;
553 struct list_head fl_entry;
554 struct list_head ml_entry;
561 struct drm_map_list {
562 struct list_head head; /**< list head */
563 struct drm_hash_item hash;
564 struct drm_map *map; /**< mapping */
566 struct drm_mm_node *file_offset_node;
569 typedef struct drm_map drm_local_map_t;
572 * Context handle list
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 */
581 struct list_head head;
582 unsigned int sequence;
584 struct task_struct *task;
588 * Drawable information.
590 struct drm_drawable_info {
591 unsigned int num_rects;
592 struct drm_clip_rect *rects;
596 /* location of GART table */
597 #define DRM_ATI_GART_MAIN 1
598 #define DRM_ATI_GART_FB 2
600 #define DRM_ATI_GART_PCI 1
601 #define DRM_ATI_GART_PCIE 2
602 #define DRM_ATI_GART_IGP 3
604 struct ati_pcigart_info {
605 int gart_table_location;
609 drm_local_map_t mapping;
613 #include "drm_objects.h"
616 * DRM driver structure. This structure represent the common code for
617 * a family of cards. There will one drm_device for each card present
622 int (*load) (struct drm_device *, unsigned long flags);
623 int (*firstopen) (struct drm_device *);
624 int (*open) (struct drm_device *, struct drm_file *);
625 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
626 void (*postclose) (struct drm_device *, struct drm_file *);
627 void (*lastclose) (struct drm_device *);
628 int (*unload) (struct drm_device *);
629 int (*suspend) (struct drm_device *);
630 int (*resume) (struct drm_device *);
631 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
632 void (*dma_ready) (struct drm_device *);
633 int (*dma_quiescent) (struct drm_device *);
634 int (*context_ctor) (struct drm_device * dev, int context);
635 int (*context_dtor) (struct drm_device * dev, int context);
636 int (*kernel_context_switch) (struct drm_device * dev, int old,
638 void (*kernel_context_switch_unlock) (struct drm_device * dev);
640 * get_vblank_counter - get raw hardware vblank counter
642 * @crtc: counter to fetch
644 * Driver callback for fetching a raw hardware vblank counter
645 * for @crtc. If a device doesn't have a hardware counter, the
646 * driver can simply return the value of drm_vblank_count and
647 * make the enable_vblank() and disable_vblank() hooks into no-ops,
648 * leaving interrupts enabled at all times.
650 * Wraparound handling and loss of events due to modesetting is dealt
651 * with in the DRM core code.
654 * Raw vblank counter value.
656 u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
659 * enable_vblank - enable vblank interrupt events
661 * @crtc: which irq to enable
663 * Enable vblank interrupts for @crtc. If the device doesn't have
664 * a hardware vblank counter, this routine should be a no-op, since
665 * interrupts will have to stay on to keep the count accurate.
668 * Zero on success, appropriate errno if the given @crtc's vblank
669 * interrupt cannot be enabled.
671 int (*enable_vblank) (struct drm_device *dev, int crtc);
674 * disable_vblank - disable vblank interrupt events
676 * @crtc: which irq to enable
678 * Disable vblank interrupts for @crtc. If the device doesn't have
679 * a hardware vblank counter, this routine should be a no-op, since
680 * interrupts will have to stay on to keep the count accurate.
682 void (*disable_vblank) (struct drm_device *dev, int crtc);
683 int (*dri_library_name) (struct drm_device * dev, char * buf);
686 * Called by \c drm_device_is_agp. Typically used to determine if a
687 * card is really attached to AGP or not.
689 * \param dev DRM device handle
692 * One of three values is returned depending on whether or not the
693 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
694 * (return of 1), or may or may not be AGP (return of 2).
696 int (*device_is_agp) (struct drm_device * dev);
698 /* these have to be filled in */
699 irqreturn_t(*irq_handler) (DRM_IRQ_ARGS);
700 void (*irq_preinstall) (struct drm_device * dev);
701 int (*irq_postinstall) (struct drm_device * dev);
702 void (*irq_uninstall) (struct drm_device * dev);
703 void (*reclaim_buffers) (struct drm_device *dev,
704 struct drm_file *file_priv);
705 void (*reclaim_buffers_locked) (struct drm_device *dev,
706 struct drm_file *file_priv);
707 void (*reclaim_buffers_idlelocked) (struct drm_device *dev,
708 struct drm_file *file_priv);
709 unsigned long (*get_map_ofs) (struct drm_map * map);
710 unsigned long (*get_reg_ofs) (struct drm_device * dev);
711 void (*set_version) (struct drm_device * dev, struct drm_set_version * sv);
713 struct drm_fence_driver *fence_driver;
714 struct drm_bo_driver *bo_driver;
726 struct drm_ioctl_desc *ioctls;
728 struct file_operations fops;
729 struct pci_driver pci_driver;
733 * DRM head structure. This structure represent a video head on a card
734 * that may contain multiple heads. Embed one per head of these in the
735 * private drm_device structure.
738 int minor; /**< Minor device number */
739 struct drm_device *dev;
740 struct proc_dir_entry *dev_root; /**< proc directory entry */
741 dev_t device; /**< Device number for mknod */
742 struct class_device *dev_class;
747 * DRM device structure. This structure represent a complete card that
748 * may contain multiple heads.
751 struct device dev; /**< Linux device */
752 char *unique; /**< Unique identifier: e.g., busid */
753 int unique_len; /**< Length of unique field */
754 char *devname; /**< For /proc/interrupts */
755 int if_version; /**< Highest interface version set */
757 int blocked; /**< Blocked due to VC switch? */
761 spinlock_t count_lock; /**< For inuse, drm_device::open_count, drm_device::buf_use */
762 struct mutex struct_mutex; /**< For others */
765 /** \name Usage Counters */
767 int open_count; /**< Outstanding files open */
768 atomic_t ioctl_count; /**< Outstanding IOCTLs pending */
769 atomic_t vma_count; /**< Outstanding vma areas open */
770 int buf_use; /**< Buffers in use -- cannot alloc */
771 atomic_t buf_alloc; /**< Buffer allocation in progress */
774 /** \name Performance counters */
776 unsigned long counters;
777 enum drm_stat_type types[15];
781 /** \name Authentication */
783 struct list_head filelist;
784 struct drm_open_hash magiclist;
785 struct list_head magicfree;
788 /** \name Memory management */
790 struct list_head maplist; /**< Linked list of regions */
791 int map_count; /**< Number of mappable regions */
792 struct drm_open_hash map_hash; /**< User token hash table for maps */
793 struct drm_mm offset_manager; /**< User token manager */
794 struct drm_open_hash object_hash; /**< User token hash table for objects */
795 struct address_space *dev_mapping; /**< For unmap_mapping_range() */
796 struct page *ttm_dummy_page;
798 /** \name Context handle management */
800 struct list_head ctxlist; /**< Linked list of context handles */
801 int ctx_count; /**< Number of context handles */
802 struct mutex ctxlist_mutex; /**< For ctxlist */
806 struct list_head vmalist; /**< List of vmas (for debugging) */
807 struct drm_lock_data lock; /**< Information on hardware lock */
810 /** \name DMA queues (contexts) */
812 int queue_count; /**< Number of active DMA queues */
813 int queue_reserved; /**< Number of reserved DMA queues */
814 int queue_slots; /**< Actual length of queuelist */
815 struct drm_queue **queuelist; /**< Vector of pointers to DMA queues */
816 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
819 /** \name Context support */
821 int irq; /**< Interrupt used by board */
822 int irq_enabled; /**< True if irq handler is enabled */
823 __volatile__ long context_flag; /**< Context swapping flag */
824 __volatile__ long interrupt_flag; /**< Interruption handler flag */
825 __volatile__ long dma_flag; /**< DMA dispatch flag */
826 struct timer_list timer; /**< Timer for delaying ctx switch */
827 wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
828 int last_checked; /**< Last context checked for DMA */
829 int last_context; /**< Last current context */
830 unsigned long last_switch; /**< jiffies at last context switch */
833 struct work_struct work;
835 /** \name VBLANK IRQ support */
838 wait_queue_head_t *vbl_queue; /**< VBLANK wait queue */
839 atomic_t *_vblank_count; /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
841 struct list_head *vbl_sigs; /**< signal list to send on VBLANK */
842 atomic_t vbl_signal_pending; /* number of signals pending on all crtcs*/
843 atomic_t *vblank_refcount; /* number of users of vblank interrupts per crtc */
844 u32 *last_vblank; /* protected by dev->vbl_lock, used */
845 /* for wraparound handling */
846 u32 *vblank_offset; /* used to track how many vblanks */
847 u32 *vblank_premodeset; /* were lost during modeset */
848 struct timer_list vblank_disable_timer;
850 unsigned long max_vblank_count; /**< size of vblank counter register */
851 spinlock_t tasklet_lock; /**< For drm_locked_tasklet */
852 void (*locked_tasklet_func)(struct drm_device *dev);
858 struct fasync_struct *buf_async;/**< Processes waiting for SIGIO */
859 wait_queue_head_t buf_readers; /**< Processes waiting to read */
860 wait_queue_head_t buf_writers; /**< Processes waiting to ctx switch */
862 struct drm_agp_head *agp; /**< AGP data */
864 struct pci_dev *pdev; /**< PCI device structure */
865 int pci_vendor; /**< PCI vendor id */
866 int pci_device; /**< PCI device id */
868 struct pci_controller *hose;
870 int num_crtcs; /**< Number of CRTCs on this device */
871 struct drm_sg_mem *sg; /**< Scatter gather memory */
872 void *dev_private; /**< device private data */
873 struct drm_sigdata sigdata; /**< For block_all_signals */
876 struct drm_driver *driver;
877 drm_local_map_t *agp_buffer_map;
878 unsigned int agp_buffer_token;
879 struct drm_head primary; /**< primary screen head */
881 struct drm_fence_manager fm;
882 struct drm_buffer_manager bm;
884 /** \name Drawable information */
892 struct drm_agp_ttm_backend {
893 struct drm_ttm_backend backend;
895 struct agp_bridge_data *bridge;
901 static __inline__ int drm_core_check_feature(struct drm_device *dev,
904 return ((dev->driver->driver_features & feature) ? 1 : 0);
908 #define drm_get_pci_domain(dev) dev->hose->index
910 #define drm_get_pci_domain(dev) 0
914 static inline int drm_core_has_AGP(struct drm_device *dev)
916 return drm_core_check_feature(dev, DRIVER_USE_AGP);
919 #define drm_core_has_AGP(dev) (0)
923 static inline int drm_core_has_MTRR(struct drm_device *dev)
925 return drm_core_check_feature(dev, DRIVER_USE_MTRR);
928 #define DRM_MTRR_WC MTRR_TYPE_WRCOMB
930 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
933 return mtrr_add(offset, size, flags, 1);
936 static inline int drm_mtrr_del(int handle, unsigned long offset,
937 unsigned long size, unsigned int flags)
939 return mtrr_del(handle, offset, size);
943 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
949 static inline int drm_mtrr_del(int handle, unsigned long offset,
950 unsigned long size, unsigned int flags)
955 #define drm_core_has_MTRR(dev) (0)
956 #define DRM_MTRR_WC 0
960 /******************************************************************/
961 /** \name Internal function definitions */
964 /* Driver support (drm_drv.h) */
965 extern int drm_fb_loaded;
966 extern int drm_init(struct drm_driver *driver,
967 struct pci_device_id *pciidlist);
968 extern void drm_exit(struct drm_driver *driver);
969 extern void drm_cleanup_pci(struct pci_dev *pdev);
970 extern int drm_ioctl(struct inode *inode, struct file *filp,
971 unsigned int cmd, unsigned long arg);
972 extern long drm_unlocked_ioctl(struct file *filp,
973 unsigned int cmd, unsigned long arg);
974 extern long drm_compat_ioctl(struct file *filp,
975 unsigned int cmd, unsigned long arg);
977 extern int drm_lastclose(struct drm_device *dev);
979 /* Device support (drm_fops.h) */
980 extern int drm_open(struct inode *inode, struct file *filp);
981 extern int drm_stub_open(struct inode *inode, struct file *filp);
982 extern int drm_fasync(int fd, struct file *filp, int on);
983 extern int drm_release(struct inode *inode, struct file *filp);
984 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
986 /* Mapping support (drm_vm.h) */
987 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
988 extern unsigned long drm_core_get_map_ofs(struct drm_map * map);
989 extern unsigned long drm_core_get_reg_ofs(struct drm_device *dev);
990 extern pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma);
992 /* Memory management support (drm_memory.h) */
993 #include "drm_memory.h"
994 extern void drm_mem_init(void);
995 extern int drm_mem_info(char *buf, char **start, off_t offset,
996 int request, int *eof, void *data);
997 extern void *drm_calloc(size_t nmemb, size_t size, int area);
998 extern void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area);
999 extern unsigned long drm_alloc_pages(int order, int area);
1000 extern void drm_free_pages(unsigned long address, int order, int area);
1001 extern DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type);
1002 extern int drm_free_agp(DRM_AGP_MEM * handle, int pages);
1003 extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start);
1004 extern int drm_unbind_agp(DRM_AGP_MEM * handle);
1006 extern void drm_free_memctl(size_t size);
1007 extern int drm_alloc_memctl(size_t size);
1008 extern void drm_query_memctl(uint64_t *cur_used,
1009 uint64_t *low_threshold,
1010 uint64_t *high_threshold);
1011 extern void drm_init_memctl(size_t low_threshold,
1012 size_t high_threshold,
1015 /* Misc. IOCTL support (drm_ioctl.h) */
1016 extern int drm_irq_by_busid(struct drm_device *dev, void *data,
1017 struct drm_file *file_priv);
1018 extern int drm_getunique(struct drm_device *dev, void *data,
1019 struct drm_file *file_priv);
1020 extern int drm_setunique(struct drm_device *dev, void *data,
1021 struct drm_file *file_priv);
1022 extern int drm_getmap(struct drm_device *dev, void *data,
1023 struct drm_file *file_priv);
1024 extern int drm_getclient(struct drm_device *dev, void *data,
1025 struct drm_file *file_priv);
1026 extern int drm_getstats(struct drm_device *dev, void *data,
1027 struct drm_file *file_priv);
1028 extern int drm_setversion(struct drm_device *dev, void *data,
1029 struct drm_file *file_priv);
1030 extern int drm_noop(struct drm_device *dev, void *data,
1031 struct drm_file *file_priv);
1033 /* Context IOCTL support (drm_context.h) */
1034 extern int drm_resctx(struct drm_device *dev, void *data,
1035 struct drm_file *file_priv);
1036 extern int drm_addctx(struct drm_device *dev, void *data,
1037 struct drm_file *file_priv);
1038 extern int drm_modctx(struct drm_device *dev, void *data,
1039 struct drm_file *file_priv);
1040 extern int drm_getctx(struct drm_device *dev, void *data,
1041 struct drm_file *file_priv);
1042 extern int drm_switchctx(struct drm_device *dev, void *data,
1043 struct drm_file *file_priv);
1044 extern int drm_newctx(struct drm_device *dev, void *data,
1045 struct drm_file *file_priv);
1046 extern int drm_rmctx(struct drm_device *dev, void *data,
1047 struct drm_file *file_priv);
1049 extern int drm_ctxbitmap_init(struct drm_device *dev);
1050 extern void drm_ctxbitmap_cleanup(struct drm_device *dev);
1051 extern void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1053 extern int drm_setsareactx(struct drm_device *dev, void *data,
1054 struct drm_file *file_priv);
1055 extern int drm_getsareactx(struct drm_device *dev, void *data,
1056 struct drm_file *file_priv);
1058 /* Drawable IOCTL support (drm_drawable.h) */
1059 extern int drm_adddraw(struct drm_device *dev, void *data,
1060 struct drm_file *file_priv);
1061 extern int drm_rmdraw(struct drm_device *dev, void *data,
1062 struct drm_file *file_priv);
1063 extern int drm_update_drawable_info(struct drm_device *dev, void *data,
1064 struct drm_file *file_priv);
1065 extern struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1067 extern void drm_drawable_free_all(struct drm_device *dev);
1069 /* Authentication IOCTL support (drm_auth.h) */
1070 extern int drm_getmagic(struct drm_device *dev, void *data,
1071 struct drm_file *file_priv);
1072 extern int drm_authmagic(struct drm_device *dev, void *data,
1073 struct drm_file *file_priv);
1075 /* Locking IOCTL support (drm_lock.h) */
1076 extern int drm_lock(struct drm_device *dev, void *data,
1077 struct drm_file *file_priv);
1078 extern int drm_unlock(struct drm_device *dev, void *data,
1079 struct drm_file *file_priv);
1080 extern int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
1081 extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context);
1082 extern void drm_idlelock_take(struct drm_lock_data *lock_data);
1083 extern void drm_idlelock_release(struct drm_lock_data *lock_data);
1086 * These are exported to drivers so that they can implement fencing using
1087 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1090 extern int drm_i_have_hw_lock(struct drm_device *dev,
1091 struct drm_file *file_priv);
1093 /* Buffer management support (drm_bufs.h) */
1094 extern int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc * request);
1095 extern int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc * request);
1096 extern int drm_addbufs_fb (struct drm_device *dev, struct drm_buf_desc * request);
1097 extern int drm_addmap(struct drm_device *dev, unsigned int offset,
1098 unsigned int size, enum drm_map_type type,
1099 enum drm_map_flags flags, drm_local_map_t ** map_ptr);
1100 extern int drm_addmap_ioctl(struct drm_device *dev, void *data,
1101 struct drm_file *file_priv);
1102 extern int drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1103 extern int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map);
1104 extern int drm_rmmap_ioctl(struct drm_device *dev, void *data,
1105 struct drm_file *file_priv);
1106 extern int drm_addbufs(struct drm_device *dev, void *data,
1107 struct drm_file *file_priv);
1108 extern int drm_infobufs(struct drm_device *dev, void *data,
1109 struct drm_file *file_priv);
1110 extern int drm_markbufs(struct drm_device *dev, void *data,
1111 struct drm_file *file_priv);
1112 extern int drm_freebufs(struct drm_device *dev, void *data,
1113 struct drm_file *file_priv);
1114 extern int drm_mapbufs(struct drm_device *dev, void *data,
1115 struct drm_file *file_priv);
1116 extern int drm_order(unsigned long size);
1117 extern unsigned long drm_get_resource_start(struct drm_device *dev,
1118 unsigned int resource);
1119 extern unsigned long drm_get_resource_len(struct drm_device *dev,
1120 unsigned int resource);
1121 extern struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
1122 drm_local_map_t *map);
1125 /* DMA support (drm_dma.h) */
1126 extern int drm_dma_setup(struct drm_device *dev);
1127 extern void drm_dma_takedown(struct drm_device *dev);
1128 extern void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf);
1129 extern void drm_core_reclaim_buffers(struct drm_device *dev,
1130 struct drm_file *filp);
1132 /* IRQ support (drm_irq.h) */
1133 extern int drm_control(struct drm_device *dev, void *data,
1134 struct drm_file *file_priv);
1135 extern irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1136 extern int drm_irq_install(struct drm_device *dev);
1137 extern int drm_irq_uninstall(struct drm_device *dev);
1138 extern void drm_driver_irq_preinstall(struct drm_device *dev);
1139 extern void drm_driver_irq_postinstall(struct drm_device *dev);
1140 extern void drm_driver_irq_uninstall(struct drm_device *dev);
1142 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1143 extern int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *filp);
1144 extern int drm_vblank_wait(struct drm_device * dev, unsigned int *vbl_seq);
1145 extern void drm_locked_tasklet(struct drm_device *dev, void(*func)(struct drm_device*));
1146 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1147 extern void drm_update_vblank_count(struct drm_device *dev, int crtc);
1148 extern void drm_handle_vblank(struct drm_device *dev, int crtc);
1149 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1150 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1152 /* Modesetting support */
1153 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1154 struct drm_file *file_priv);
1156 /* AGP/GART support (drm_agpsupport.h) */
1157 extern struct drm_agp_head *drm_agp_init(struct drm_device *dev);
1158 extern int drm_agp_acquire(struct drm_device *dev);
1159 extern int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1160 struct drm_file *file_priv);
1161 extern int drm_agp_release(struct drm_device *dev);
1162 extern int drm_agp_release_ioctl(struct drm_device *dev, void *data,
1163 struct drm_file *file_priv);
1164 extern int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1165 extern int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1166 struct drm_file *file_priv);
1167 extern int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info);
1168 extern int drm_agp_info_ioctl(struct drm_device *dev, void *data,
1169 struct drm_file *file_priv);
1170 extern int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1171 extern int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1172 struct drm_file *file_priv);
1173 extern int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1174 extern int drm_agp_free_ioctl(struct drm_device *dev, void *data,
1175 struct drm_file *file_priv);
1176 extern int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1177 extern int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1178 struct drm_file *file_priv);
1179 extern int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1180 extern int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1181 struct drm_file *file_priv);
1182 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
1183 extern DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type);
1185 extern DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type);
1187 extern int drm_agp_free_memory(DRM_AGP_MEM * handle);
1188 extern int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start);
1189 extern int drm_agp_unbind_memory(DRM_AGP_MEM * handle);
1190 extern struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev);
1191 /* Stub support (drm_stub.h) */
1192 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
1193 struct drm_driver *driver);
1194 extern int drm_put_dev(struct drm_device *dev);
1195 extern int drm_put_head(struct drm_head * head);
1196 extern unsigned int drm_debug; /* 1 to enable debug output */
1197 extern unsigned int drm_cards_limit;
1198 extern struct drm_head **drm_heads;
1199 extern struct class *drm_class;
1200 extern struct proc_dir_entry *drm_proc_root;
1202 extern drm_local_map_t *drm_getsarea(struct drm_device *dev);
1204 /* Proc support (drm_proc.h) */
1205 extern int drm_proc_init(struct drm_device *dev,
1207 struct proc_dir_entry *root,
1208 struct proc_dir_entry **dev_root);
1209 extern int drm_proc_cleanup(int minor,
1210 struct proc_dir_entry *root,
1211 struct proc_dir_entry *dev_root);
1213 /* Scatter Gather Support (drm_scatter.h) */
1214 extern void drm_sg_cleanup(struct drm_sg_mem * entry);
1215 extern int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1216 struct drm_file *file_priv);
1217 extern int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1218 extern int drm_sg_free(struct drm_device *dev, void *data,
1219 struct drm_file *file_priv);
1221 /* ATI PCIGART support (ati_pcigart.h) */
1222 extern int drm_ati_pcigart_init(struct drm_device *dev, struct ati_pcigart_info *gart_info);
1223 extern int drm_ati_pcigart_cleanup(struct drm_device *dev, struct ati_pcigart_info *gart_info);
1225 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1226 size_t align, dma_addr_t maxaddr);
1227 extern void __drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1228 extern void drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1230 /* sysfs support (drm_sysfs.c) */
1231 struct drm_sysfs_class;
1232 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1233 extern void drm_sysfs_destroy(void);
1234 extern int drm_sysfs_device_add(struct drm_device *dev, struct drm_head * head);
1235 extern void drm_sysfs_device_remove(struct drm_device *dev);
1238 * Basic memory manager support (drm_mm.c)
1241 extern struct drm_mm_node * drm_mm_get_block(struct drm_mm_node * parent, unsigned long size,
1242 unsigned alignment);
1243 extern void drm_mm_put_block(struct drm_mm_node *cur);
1244 extern struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm, unsigned long size,
1245 unsigned alignment, int best_match);
1246 extern int drm_mm_init(struct drm_mm *mm, unsigned long start, unsigned long size);
1247 extern void drm_mm_takedown(struct drm_mm *mm);
1248 extern int drm_mm_clean(struct drm_mm *mm);
1249 extern unsigned long drm_mm_tail_space(struct drm_mm *mm);
1250 extern int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size);
1251 extern int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size);
1253 static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
1258 extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev);
1259 extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev);
1261 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev,
1264 struct drm_map_list *_entry;
1265 list_for_each_entry(_entry, &dev->maplist, head)
1266 if (_entry->user_token == token)
1271 static __inline__ int drm_device_is_agp(struct drm_device *dev)
1273 if ( dev->driver->device_is_agp != NULL ) {
1274 int err = (*dev->driver->device_is_agp)( dev );
1281 return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1284 static __inline__ int drm_device_is_pcie(struct drm_device *dev)
1286 return pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
1289 static __inline__ void drm_core_dropmap(struct drm_map *map)
1293 #ifndef DEBUG_MEMORY
1294 /** Wrapper around kmalloc() */
1295 static __inline__ void *drm_alloc(size_t size, int area)
1297 return kmalloc(size, GFP_KERNEL);
1300 /** Wrapper around kfree() */
1301 static __inline__ void drm_free(void *pt, size_t size, int area)
1306 extern void *drm_alloc(size_t size, int area);
1307 extern void drm_free(void *pt, size_t size, int area);
1311 * Accounting variants of standard calls.
1314 static inline void *drm_ctl_alloc(size_t size, int area)
1317 if (drm_alloc_memctl(size))
1319 ret = drm_alloc(size, area);
1321 drm_free_memctl(size);
1325 static inline void *drm_ctl_calloc(size_t nmemb, size_t size, int area)
1329 if (drm_alloc_memctl(nmemb*size))
1331 ret = drm_calloc(nmemb, size, area);
1333 drm_free_memctl(nmemb*size);
1337 static inline void drm_ctl_free(void *pt, size_t size, int area)
1339 drm_free(pt, size, area);
1340 drm_free_memctl(size);
1345 /** Type for the OS's non-sleepable mutex lock */
1346 #define DRM_SPINTYPE spinlock_t
1348 * Initialize the lock for use. name is an optional string describing the
1351 #define DRM_SPININIT(l,name) spin_lock_init(l)
1352 #define DRM_SPINUNINIT(l)
1353 #define DRM_SPINLOCK(l) spin_lock(l)
1354 #define DRM_SPINUNLOCK(l) spin_unlock(l)
1355 #define DRM_SPINLOCK_IRQSAVE(l, _flags) spin_lock_irqsave(l, _flags);
1356 #define DRM_SPINUNLOCK_IRQRESTORE(l, _flags) spin_unlock_irqrestore(l, _flags);
1357 #define DRM_SPINLOCK_ASSERT(l) do {} while (0)
1359 #endif /* __KERNEL__ */