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>
70 #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"
86 #include "drm_internal.h"
91 /* If you want the memory alloc debug functionality, change define below */
92 /* #define DEBUG_MEMORY */
94 /***********************************************************************/
95 /** \name DRM template customization defaults */
98 /* driver capabilities and requirements mask */
99 #define DRIVER_USE_AGP 0x1
100 #define DRIVER_REQUIRE_AGP 0x2
101 #define DRIVER_USE_MTRR 0x4
102 #define DRIVER_PCI_DMA 0x8
103 #define DRIVER_SG 0x10
104 #define DRIVER_HAVE_DMA 0x20
105 #define DRIVER_HAVE_IRQ 0x40
106 #define DRIVER_IRQ_SHARED 0x80
107 #define DRIVER_DMA_QUEUE 0x100
108 #define DRIVER_FB_DMA 0x200
113 /***********************************************************************/
114 /** \name Begin the DRM... */
117 #define DRM_DEBUG_CODE 2 /**< Include debugging code if > 1, then
118 also include looping detection. */
120 #define DRM_MAGIC_HASH_ORDER 4 /**< Size of key hash table. Must be power of 2. */
121 #define DRM_KERNEL_CONTEXT 0 /**< Change drm_resctx if changed */
122 #define DRM_RESERVED_CONTEXTS 1 /**< Change drm_resctx if changed */
123 #define DRM_LOOPING_LIMIT 5000000
124 #define DRM_TIME_SLICE (HZ/20) /**< Time slice for GLXContexts */
125 #define DRM_LOCK_SLICE 1 /**< Time slice for lock, in jiffies */
127 #define DRM_FLAG_DEBUG 0x01
129 #define DRM_MEM_DMA 0
130 #define DRM_MEM_SAREA 1
131 #define DRM_MEM_DRIVER 2
132 #define DRM_MEM_MAGIC 3
133 #define DRM_MEM_IOCTLS 4
134 #define DRM_MEM_MAPS 5
135 #define DRM_MEM_VMAS 6
136 #define DRM_MEM_BUFS 7
137 #define DRM_MEM_SEGS 8
138 #define DRM_MEM_PAGES 9
139 #define DRM_MEM_FILES 10
140 #define DRM_MEM_QUEUES 11
141 #define DRM_MEM_CMDS 12
142 #define DRM_MEM_MAPPINGS 13
143 #define DRM_MEM_BUFLISTS 14
144 #define DRM_MEM_AGPLISTS 15
145 #define DRM_MEM_TOTALAGP 16
146 #define DRM_MEM_BOUNDAGP 17
147 #define DRM_MEM_CTXBITMAP 18
148 #define DRM_MEM_STUB 19
149 #define DRM_MEM_SGLISTS 20
150 #define DRM_MEM_CTXLIST 21
151 #define DRM_MEM_MM 22
152 #define DRM_MEM_HASHTAB 23
153 #define DRM_MEM_OBJECTS 24
154 #define DRM_MEM_FENCE 25
155 #define DRM_MEM_TTM 26
156 #define DRM_MEM_BUFOBJ 27
158 #define DRM_MAX_CTXBITMAP (PAGE_SIZE * 8)
159 #define DRM_MAP_HASH_OFFSET 0x10000000
160 #define DRM_MAP_HASH_ORDER 12
161 #define DRM_OBJECT_HASH_ORDER 12
162 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
163 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 16)
165 * This should be small enough to allow the use of kmalloc for hash tables
166 * instead of vmalloc.
169 #define DRM_FILE_HASH_ORDER 8
170 #define DRM_MM_INIT_MAX_PAGES 256
174 #include "drm_compat.h"
176 /***********************************************************************/
177 /** \name Macros to make printk easier */
183 * \param fmt printf() like format string.
184 * \param arg arguments
186 #define DRM_ERROR(fmt, arg...) \
187 printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* " fmt , __FUNCTION__ , ##arg)
190 * Memory error output.
192 * \param area memory area where the error occurred.
193 * \param fmt printf() like format string.
194 * \param arg arguments
196 #define DRM_MEM_ERROR(area, fmt, arg...) \
197 printk(KERN_ERR "[" DRM_NAME ":%s:%s] *ERROR* " fmt , __FUNCTION__, \
198 drm_mem_stats[area].name , ##arg)
199 #define DRM_INFO(fmt, arg...) printk(KERN_INFO "[" DRM_NAME "] " fmt , ##arg)
204 * \param fmt printf() like format string.
205 * \param arg arguments
208 #define DRM_DEBUG(fmt, arg...) \
212 "[" DRM_NAME ":%s] " fmt , \
213 __FUNCTION__ , ##arg); \
216 #define DRM_DEBUG(fmt, arg...) do { } while (0)
219 #define DRM_PROC_LIMIT (PAGE_SIZE-80)
221 #define DRM_PROC_PRINT(fmt, arg...) \
222 len += sprintf(&buf[len], fmt , ##arg); \
223 if (len > DRM_PROC_LIMIT) { *eof = 1; return len - offset; }
225 #define DRM_PROC_PRINT_RET(ret, fmt, arg...) \
226 len += sprintf(&buf[len], fmt , ##arg); \
227 if (len > DRM_PROC_LIMIT) { ret; *eof = 1; return len - offset; }
231 /***********************************************************************/
232 /** \name Internal types and structures */
235 #define DRM_ARRAY_SIZE(x) ARRAY_SIZE(x)
236 #define DRM_MIN(a,b) min(a,b)
237 #define DRM_MAX(a,b) max(a,b)
239 #define DRM_LEFTCOUNT(x) (((x)->rp + (x)->count - (x)->wp) % ((x)->count + 1))
240 #define DRM_BUFCOUNT(x) ((x)->count - DRM_LEFTCOUNT(x))
241 #define DRM_WAITCOUNT(dev,idx) DRM_BUFCOUNT(&dev->queuelist[idx]->waitlist)
243 #define DRM_IF_VERSION(maj, min) (maj << 16 | min)
245 * Get the private SAREA mapping.
247 * \param _dev DRM device.
248 * \param _ctx context number.
249 * \param _map output mapping.
251 #define DRM_GET_PRIV_SAREA(_dev, _ctx, _map) do { \
252 (_map) = (_dev)->context_sareas[_ctx]; \
256 * Test that the hardware lock is held by the caller, returning otherwise.
258 * \param dev DRM device.
259 * \param file_priv DRM file private pointer of the caller.
261 #define LOCK_TEST_WITH_RETURN( dev, file_priv ) \
263 if ( !_DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ) || \
264 dev->lock.file_priv != file_priv ) { \
265 DRM_ERROR( "%s called without lock held, held %d owner %p %p\n",\
266 __FUNCTION__, _DRM_LOCK_IS_HELD( dev->lock.hw_lock->lock ),\
267 dev->lock.file_priv, file_priv ); \
273 * Copy and IOCTL return string to user space
275 #define DRM_COPY( name, value ) \
276 len = strlen( value ); \
277 if ( len > name##_len ) len = name##_len; \
278 name##_len = strlen( value ); \
279 if ( len && name ) { \
280 if ( copy_to_user( name, value, len ) ) \
285 * Ioctl function type.
287 * \param dev DRM device structure
288 * \param data pointer to kernel-space stored data, copied in and out according
289 * to ioctl description.
290 * \param file_priv DRM file private pointer.
292 typedef int drm_ioctl_t(struct drm_device *dev, void *data,
293 struct drm_file *file_priv);
295 typedef int drm_ioctl_compat_t(struct file *filp, unsigned int cmd,
299 #define DRM_MASTER 0x2
300 #define DRM_ROOT_ONLY 0x4
302 struct drm_ioctl_desc {
308 * Creates a driver or general drm_ioctl_desc array entry for the given
309 * ioctl, for use by drm_ioctl().
311 #define DRM_IOCTL_DEF(ioctl, func, flags) \
312 [DRM_IOCTL_NR(ioctl)] = {ioctl, func, flags}
314 struct drm_magic_entry {
315 struct list_head head;
316 struct drm_hash_item hash_item;
317 struct drm_file *priv;
320 struct drm_vma_entry {
321 struct list_head head;
322 struct vm_area_struct *vma;
330 int idx; /**< Index into master buflist */
331 int total; /**< Buffer size */
332 int order; /**< log-base-2(total) */
333 int used; /**< Amount of buffer in use (for DMA) */
334 unsigned long offset; /**< Byte offset (used internally) */
335 void *address; /**< Address of buffer */
336 unsigned long bus_address; /**< Bus address of buffer */
337 struct drm_buf *next; /**< Kernel-only: used for free list */
338 __volatile__ int waiting; /**< On kernel DMA queue */
339 __volatile__ int pending; /**< On hardware DMA queue */
340 wait_queue_head_t dma_wait; /**< Processes waiting */
341 struct drm_file *file_priv; /**< Private of holding file descr */
342 int context; /**< Kernel queue for this buffer */
343 int while_locked; /**< Dispatch this buffer while locked */
351 } list; /**< Which list we're on */
353 int dev_priv_size; /**< Size of buffer private storage */
354 void *dev_private; /**< Per-buffer private storage */
357 /** bufs is one longer than it has to be */
358 struct drm_waitlist {
359 int count; /**< Number of possible buffers */
360 struct drm_buf **bufs; /**< List of pointers to buffers */
361 struct drm_buf **rp; /**< Read pointer */
362 struct drm_buf **wp; /**< Write pointer */
363 struct drm_buf **end; /**< End pointer */
364 spinlock_t read_lock;
365 spinlock_t write_lock;
368 struct drm_freelist {
369 int initialized; /**< Freelist in use */
370 atomic_t count; /**< Number of free buffers */
371 struct drm_buf *next; /**< End pointer */
373 wait_queue_head_t waiting; /**< Processes waiting on free bufs */
374 int low_mark; /**< Low water mark */
375 int high_mark; /**< High water mark */
376 atomic_t wfh; /**< If waiting for high mark */
380 typedef struct drm_dma_handle {
387 * Buffer entry. There is one of this for each buffer size order.
389 struct drm_buf_entry {
390 int buf_size; /**< size */
391 int buf_count; /**< number of buffers */
392 struct drm_buf *buflist; /**< buffer list */
395 struct drm_dma_handle **seglist;
396 struct drm_freelist freelist;
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;
587 /* location of GART table */
588 #define DRM_ATI_GART_MAIN 1
589 #define DRM_ATI_GART_FB 2
591 #define DRM_ATI_GART_PCI 1
592 #define DRM_ATI_GART_PCIE 2
593 #define DRM_ATI_GART_IGP 3
595 struct drm_ati_pcigart_info {
596 int gart_table_location;
600 drm_local_map_t mapping;
604 #include "drm_objects.h"
607 * DRM driver structure. This structure represent the common code for
608 * a family of cards. There will one drm_device for each card present
613 int (*load) (struct drm_device *, unsigned long flags);
614 int (*firstopen) (struct drm_device *);
615 int (*open) (struct drm_device *, struct drm_file *);
616 void (*preclose) (struct drm_device *, struct drm_file *file_priv);
617 void (*postclose) (struct drm_device *, struct drm_file *);
618 void (*lastclose) (struct drm_device *);
619 int (*unload) (struct drm_device *);
620 int (*suspend) (struct drm_device *);
621 int (*resume) (struct drm_device *);
622 int (*dma_ioctl) (struct drm_device *dev, void *data, struct drm_file *file_priv);
623 void (*dma_ready) (struct drm_device *);
624 int (*dma_quiescent) (struct drm_device *);
625 int (*context_ctor) (struct drm_device *dev, int context);
626 int (*context_dtor) (struct drm_device *dev, int context);
627 int (*kernel_context_switch) (struct drm_device *dev, int old,
629 void (*kernel_context_switch_unlock) (struct drm_device * dev);
631 * get_vblank_counter - get raw hardware vblank counter
633 * @crtc: counter to fetch
635 * Driver callback for fetching a raw hardware vblank counter
636 * for @crtc. If a device doesn't have a hardware counter, the
637 * driver can simply return the value of drm_vblank_count and
638 * make the enable_vblank() and disable_vblank() hooks into no-ops,
639 * leaving interrupts enabled at all times.
641 * Wraparound handling and loss of events due to modesetting is dealt
642 * with in the DRM core code.
645 * Raw vblank counter value.
647 u32 (*get_vblank_counter) (struct drm_device *dev, int crtc);
650 * enable_vblank - enable vblank interrupt events
652 * @crtc: which irq to enable
654 * Enable vblank interrupts for @crtc. If the device doesn't have
655 * a hardware vblank counter, this routine should be a no-op, since
656 * interrupts will have to stay on to keep the count accurate.
659 * Zero on success, appropriate errno if the given @crtc's vblank
660 * interrupt cannot be enabled.
662 int (*enable_vblank) (struct drm_device *dev, int crtc);
665 * disable_vblank - disable vblank interrupt events
667 * @crtc: which irq to enable
669 * Disable vblank interrupts for @crtc. If the device doesn't have
670 * a hardware vblank counter, this routine should be a no-op, since
671 * interrupts will have to stay on to keep the count accurate.
673 void (*disable_vblank) (struct drm_device *dev, int crtc);
674 int (*dri_library_name) (struct drm_device *dev, char * buf);
677 * Called by \c drm_device_is_agp. Typically used to determine if a
678 * card is really attached to AGP or not.
680 * \param dev DRM device handle
683 * One of three values is returned depending on whether or not the
684 * card is absolutely \b not AGP (return of 0), absolutely \b is AGP
685 * (return of 1), or may or may not be AGP (return of 2).
687 int (*device_is_agp) (struct drm_device *dev);
689 /* these have to be filled in */
690 irqreturn_t(*irq_handler) (DRM_IRQ_ARGS);
691 void (*irq_preinstall) (struct drm_device *dev);
692 int (*irq_postinstall) (struct drm_device *dev);
693 void (*irq_uninstall) (struct drm_device *dev);
694 void (*reclaim_buffers) (struct drm_device *dev,
695 struct drm_file *file_priv);
696 void (*reclaim_buffers_locked) (struct drm_device *dev,
697 struct drm_file *file_priv);
698 void (*reclaim_buffers_idlelocked) (struct drm_device *dev,
699 struct drm_file *file_priv);
700 unsigned long (*get_map_ofs) (struct drm_map *map);
701 unsigned long (*get_reg_ofs) (struct drm_device *dev);
702 void (*set_version) (struct drm_device *dev,
703 struct drm_set_version *sv);
705 struct drm_fence_driver *fence_driver;
706 struct drm_bo_driver *bo_driver;
718 struct drm_ioctl_desc *ioctls;
720 struct file_operations fops;
721 struct pci_driver pci_driver;
725 * DRM head structure. This structure represent a video head on a card
726 * that may contain multiple heads. Embed one per head of these in the
727 * private drm_device structure.
730 int minor; /**< Minor device number */
731 struct drm_device *dev;
732 struct proc_dir_entry *dev_root; /**< proc directory entry */
733 dev_t device; /**< Device number for mknod */
734 struct class_device *dev_class;
739 * DRM device structure. This structure represent a complete card that
740 * may contain multiple heads.
743 struct device dev; /**< Linux device */
744 char *unique; /**< Unique identifier: e.g., busid */
745 int unique_len; /**< Length of unique field */
746 char *devname; /**< For /proc/interrupts */
747 int if_version; /**< Highest interface version set */
749 int blocked; /**< Blocked due to VC switch? */
753 spinlock_t count_lock; /**< For inuse, drm_device::open_count, drm_device::buf_use */
754 struct mutex struct_mutex; /**< For others */
757 /** \name Usage Counters */
759 int open_count; /**< Outstanding files open */
760 atomic_t ioctl_count; /**< Outstanding IOCTLs pending */
761 atomic_t vma_count; /**< Outstanding vma areas open */
762 int buf_use; /**< Buffers in use -- cannot alloc */
763 atomic_t buf_alloc; /**< Buffer allocation in progress */
766 /** \name Performance counters */
768 unsigned long counters;
769 enum drm_stat_type types[15];
773 /** \name Authentication */
775 struct list_head filelist;
776 struct drm_open_hash magiclist;
777 struct list_head magicfree;
780 /** \name Memory management */
782 struct list_head maplist; /**< Linked list of regions */
783 int map_count; /**< Number of mappable regions */
784 struct drm_open_hash map_hash; /**< User token hash table for maps */
785 struct drm_mm offset_manager; /**< User token manager */
786 struct drm_open_hash object_hash; /**< User token hash table for objects */
787 struct address_space *dev_mapping; /**< For unmap_mapping_range() */
788 struct page *ttm_dummy_page;
790 /** \name Context handle management */
792 struct list_head ctxlist; /**< Linked list of context handles */
793 int ctx_count; /**< Number of context handles */
794 struct mutex ctxlist_mutex; /**< For ctxlist */
798 struct list_head vmalist; /**< List of vmas (for debugging) */
799 struct drm_lock_data lock; /**< Information on hardware lock */
802 /** \name DMA queues (contexts) */
804 int queue_count; /**< Number of active DMA queues */
805 int queue_reserved; /**< Number of reserved DMA queues */
806 int queue_slots; /**< Actual length of queuelist */
807 struct drm_queue **queuelist; /**< Vector of pointers to DMA queues */
808 struct drm_device_dma *dma; /**< Optional pointer for DMA support */
811 /** \name Context support */
813 int irq; /**< Interrupt used by board */
814 int irq_enabled; /**< True if irq handler is enabled */
815 __volatile__ long context_flag; /**< Context swapping flag */
816 __volatile__ long interrupt_flag; /**< Interruption handler flag */
817 __volatile__ long dma_flag; /**< DMA dispatch flag */
818 struct timer_list timer; /**< Timer for delaying ctx switch */
819 wait_queue_head_t context_wait; /**< Processes waiting on ctx switch */
820 int last_checked; /**< Last context checked for DMA */
821 int last_context; /**< Last current context */
822 unsigned long last_switch; /**< jiffies at last context switch */
825 struct work_struct work;
827 /** \name VBLANK IRQ support */
830 wait_queue_head_t *vbl_queue; /**< VBLANK wait queue */
831 atomic_t *_vblank_count; /**< number of VBLANK interrupts (driver must alloc the right number of counters) */
833 struct list_head *vbl_sigs; /**< signal list to send on VBLANK */
834 atomic_t vbl_signal_pending; /* number of signals pending on all crtcs*/
835 atomic_t *vblank_refcount; /* number of users of vblank interrupts per crtc */
836 u32 *last_vblank; /* protected by dev->vbl_lock, used */
837 /* for wraparound handling */
838 u32 *vblank_offset; /* used to track how many vblanks */
839 int *vblank_enabled; /* so we don't call enable more than
841 u32 *vblank_premodeset; /* were lost during modeset */
842 struct timer_list vblank_disable_timer;
844 unsigned long max_vblank_count; /**< size of vblank counter register */
845 spinlock_t tasklet_lock; /**< For drm_locked_tasklet */
846 void (*locked_tasklet_func)(struct drm_device *dev);
852 struct fasync_struct *buf_async;/**< Processes waiting for SIGIO */
853 wait_queue_head_t buf_readers; /**< Processes waiting to read */
854 wait_queue_head_t buf_writers; /**< Processes waiting to ctx switch */
856 struct drm_agp_head *agp; /**< AGP data */
858 struct pci_dev *pdev; /**< PCI device structure */
859 int pci_vendor; /**< PCI vendor id */
860 int pci_device; /**< PCI device id */
862 struct pci_controller *hose;
864 int num_crtcs; /**< Number of CRTCs on this device */
865 struct drm_sg_mem *sg; /**< Scatter gather memory */
866 void *dev_private; /**< device private data */
867 struct drm_sigdata sigdata; /**< For block_all_signals */
870 struct drm_driver *driver;
871 drm_local_map_t *agp_buffer_map;
872 unsigned int agp_buffer_token;
873 struct drm_head primary; /**< primary screen head */
875 struct drm_fence_manager fm;
876 struct drm_buffer_manager bm;
878 /** \name Drawable information */
886 struct drm_agp_ttm_backend {
887 struct drm_ttm_backend backend;
889 struct agp_bridge_data *bridge;
895 static __inline__ int drm_core_check_feature(struct drm_device *dev,
898 return ((dev->driver->driver_features & feature) ? 1 : 0);
902 #define drm_get_pci_domain(dev) dev->hose->index
904 #define drm_get_pci_domain(dev) 0
908 static inline int drm_core_has_AGP(struct drm_device *dev)
910 return drm_core_check_feature(dev, DRIVER_USE_AGP);
913 #define drm_core_has_AGP(dev) (0)
917 static inline int drm_core_has_MTRR(struct drm_device *dev)
919 return drm_core_check_feature(dev, DRIVER_USE_MTRR);
922 #define DRM_MTRR_WC MTRR_TYPE_WRCOMB
924 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
927 return mtrr_add(offset, size, flags, 1);
930 static inline int drm_mtrr_del(int handle, unsigned long offset,
931 unsigned long size, unsigned int flags)
933 return mtrr_del(handle, offset, size);
937 static inline int drm_mtrr_add(unsigned long offset, unsigned long size,
943 static inline int drm_mtrr_del(int handle, unsigned long offset,
944 unsigned long size, unsigned int flags)
949 #define drm_core_has_MTRR(dev) (0)
950 #define DRM_MTRR_WC 0
954 /******************************************************************/
955 /** \name Internal function definitions */
958 /* Driver support (drm_drv.h) */
959 extern int drm_fb_loaded;
960 extern int drm_init(struct drm_driver *driver,
961 struct pci_device_id *pciidlist);
962 extern void drm_exit(struct drm_driver *driver);
963 extern void drm_cleanup_pci(struct pci_dev *pdev);
964 extern int drm_ioctl(struct inode *inode, struct file *filp,
965 unsigned int cmd, unsigned long arg);
966 extern long drm_unlocked_ioctl(struct file *filp,
967 unsigned int cmd, unsigned long arg);
968 extern long drm_compat_ioctl(struct file *filp,
969 unsigned int cmd, unsigned long arg);
971 extern int drm_lastclose(struct drm_device *dev);
973 /* Device support (drm_fops.h) */
974 extern int drm_open(struct inode *inode, struct file *filp);
975 extern int drm_stub_open(struct inode *inode, struct file *filp);
976 extern int drm_fasync(int fd, struct file *filp, int on);
977 extern int drm_release(struct inode *inode, struct file *filp);
978 unsigned int drm_poll(struct file *filp, struct poll_table_struct *wait);
980 /* Mapping support (drm_vm.h) */
981 extern int drm_mmap(struct file *filp, struct vm_area_struct *vma);
982 extern unsigned long drm_core_get_map_ofs(struct drm_map * map);
983 extern unsigned long drm_core_get_reg_ofs(struct drm_device *dev);
984 extern pgprot_t drm_io_prot(uint32_t map_type, struct vm_area_struct *vma);
986 /* Memory management support (drm_memory.h) */
987 #include "drm_memory.h"
988 extern void drm_mem_init(void);
989 extern int drm_mem_info(char *buf, char **start, off_t offset,
990 int request, int *eof, void *data);
991 extern void *drm_calloc(size_t nmemb, size_t size, int area);
992 extern void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area);
993 extern unsigned long drm_alloc_pages(int order, int area);
994 extern void drm_free_pages(unsigned long address, int order, int area);
995 extern DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type);
996 extern int drm_free_agp(DRM_AGP_MEM * handle, int pages);
997 extern int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start);
998 extern int drm_unbind_agp(DRM_AGP_MEM * handle);
1000 extern void drm_free_memctl(size_t size);
1001 extern int drm_alloc_memctl(size_t size);
1002 extern void drm_query_memctl(uint64_t *cur_used,
1003 uint64_t *low_threshold,
1004 uint64_t *high_threshold);
1005 extern void drm_init_memctl(size_t low_threshold,
1006 size_t high_threshold,
1009 /* Misc. IOCTL support (drm_ioctl.h) */
1010 extern int drm_irq_by_busid(struct drm_device *dev, void *data,
1011 struct drm_file *file_priv);
1012 extern int drm_getunique(struct drm_device *dev, void *data,
1013 struct drm_file *file_priv);
1014 extern int drm_setunique(struct drm_device *dev, void *data,
1015 struct drm_file *file_priv);
1016 extern int drm_getmap(struct drm_device *dev, void *data,
1017 struct drm_file *file_priv);
1018 extern int drm_getclient(struct drm_device *dev, void *data,
1019 struct drm_file *file_priv);
1020 extern int drm_getstats(struct drm_device *dev, void *data,
1021 struct drm_file *file_priv);
1022 extern int drm_setversion(struct drm_device *dev, void *data,
1023 struct drm_file *file_priv);
1024 extern int drm_noop(struct drm_device *dev, void *data,
1025 struct drm_file *file_priv);
1027 /* Context IOCTL support (drm_context.h) */
1028 extern int drm_resctx(struct drm_device *dev, void *data,
1029 struct drm_file *file_priv);
1030 extern int drm_addctx(struct drm_device *dev, void *data,
1031 struct drm_file *file_priv);
1032 extern int drm_modctx(struct drm_device *dev, void *data,
1033 struct drm_file *file_priv);
1034 extern int drm_getctx(struct drm_device *dev, void *data,
1035 struct drm_file *file_priv);
1036 extern int drm_switchctx(struct drm_device *dev, void *data,
1037 struct drm_file *file_priv);
1038 extern int drm_newctx(struct drm_device *dev, void *data,
1039 struct drm_file *file_priv);
1040 extern int drm_rmctx(struct drm_device *dev, void *data,
1041 struct drm_file *file_priv);
1043 extern int drm_ctxbitmap_init(struct drm_device *dev);
1044 extern void drm_ctxbitmap_cleanup(struct drm_device *dev);
1045 extern void drm_ctxbitmap_free(struct drm_device *dev, int ctx_handle);
1047 extern int drm_setsareactx(struct drm_device *dev, void *data,
1048 struct drm_file *file_priv);
1049 extern int drm_getsareactx(struct drm_device *dev, void *data,
1050 struct drm_file *file_priv);
1052 /* Drawable IOCTL support (drm_drawable.h) */
1053 extern int drm_adddraw(struct drm_device *dev, void *data,
1054 struct drm_file *file_priv);
1055 extern int drm_rmdraw(struct drm_device *dev, void *data,
1056 struct drm_file *file_priv);
1057 extern int drm_update_drawable_info(struct drm_device *dev, void *data,
1058 struct drm_file *file_priv);
1059 extern struct drm_drawable_info *drm_get_drawable_info(struct drm_device *dev,
1061 extern void drm_drawable_free_all(struct drm_device *dev);
1063 /* Authentication IOCTL support (drm_auth.h) */
1064 extern int drm_getmagic(struct drm_device *dev, void *data,
1065 struct drm_file *file_priv);
1066 extern int drm_authmagic(struct drm_device *dev, void *data,
1067 struct drm_file *file_priv);
1069 /* Locking IOCTL support (drm_lock.h) */
1070 extern int drm_lock(struct drm_device *dev, void *data,
1071 struct drm_file *file_priv);
1072 extern int drm_unlock(struct drm_device *dev, void *data,
1073 struct drm_file *file_priv);
1074 extern int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
1075 extern int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context);
1076 extern void drm_idlelock_take(struct drm_lock_data *lock_data);
1077 extern void drm_idlelock_release(struct drm_lock_data *lock_data);
1080 * These are exported to drivers so that they can implement fencing using
1081 * DMA quiscent + idle. DMA quiescent usually requires the hardware lock.
1084 extern int drm_i_have_hw_lock(struct drm_device *dev,
1085 struct drm_file *file_priv);
1087 /* Buffer management support (drm_bufs.h) */
1088 extern int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc * request);
1089 extern int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc * request);
1090 extern int drm_addbufs_fb (struct drm_device *dev, struct drm_buf_desc * request);
1091 extern int drm_addmap(struct drm_device *dev, unsigned int offset,
1092 unsigned int size, enum drm_map_type type,
1093 enum drm_map_flags flags, drm_local_map_t ** map_ptr);
1094 extern int drm_addmap_ioctl(struct drm_device *dev, void *data,
1095 struct drm_file *file_priv);
1096 extern int drm_rmmap(struct drm_device *dev, drm_local_map_t *map);
1097 extern int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map);
1098 extern int drm_rmmap_ioctl(struct drm_device *dev, void *data,
1099 struct drm_file *file_priv);
1100 extern int drm_addbufs(struct drm_device *dev, void *data,
1101 struct drm_file *file_priv);
1102 extern int drm_infobufs(struct drm_device *dev, void *data,
1103 struct drm_file *file_priv);
1104 extern int drm_markbufs(struct drm_device *dev, void *data,
1105 struct drm_file *file_priv);
1106 extern int drm_freebufs(struct drm_device *dev, void *data,
1107 struct drm_file *file_priv);
1108 extern int drm_mapbufs(struct drm_device *dev, void *data,
1109 struct drm_file *file_priv);
1110 extern int drm_order(unsigned long size);
1111 extern unsigned long drm_get_resource_start(struct drm_device *dev,
1112 unsigned int resource);
1113 extern unsigned long drm_get_resource_len(struct drm_device *dev,
1114 unsigned int resource);
1115 extern struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
1116 drm_local_map_t *map);
1119 /* DMA support (drm_dma.h) */
1120 extern int drm_dma_setup(struct drm_device *dev);
1121 extern void drm_dma_takedown(struct drm_device *dev);
1122 extern void drm_free_buffer(struct drm_device *dev, struct drm_buf * buf);
1123 extern void drm_core_reclaim_buffers(struct drm_device *dev,
1124 struct drm_file *filp);
1126 /* IRQ support (drm_irq.h) */
1127 extern int drm_control(struct drm_device *dev, void *data,
1128 struct drm_file *file_priv);
1129 extern irqreturn_t drm_irq_handler(DRM_IRQ_ARGS);
1130 extern int drm_irq_install(struct drm_device *dev);
1131 extern int drm_irq_uninstall(struct drm_device *dev);
1132 extern void drm_driver_irq_preinstall(struct drm_device *dev);
1133 extern void drm_driver_irq_postinstall(struct drm_device *dev);
1134 extern void drm_driver_irq_uninstall(struct drm_device *dev);
1136 extern int drm_vblank_init(struct drm_device *dev, int num_crtcs);
1137 extern int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *filp);
1138 extern int drm_vblank_wait(struct drm_device * dev, unsigned int *vbl_seq);
1139 extern void drm_locked_tasklet(struct drm_device *dev, void(*func)(struct drm_device*));
1140 extern u32 drm_vblank_count(struct drm_device *dev, int crtc);
1141 extern void drm_update_vblank_count(struct drm_device *dev, int crtc);
1142 extern void drm_handle_vblank(struct drm_device *dev, int crtc);
1143 extern int drm_vblank_get(struct drm_device *dev, int crtc);
1144 extern void drm_vblank_put(struct drm_device *dev, int crtc);
1146 /* Modesetting support */
1147 extern int drm_modeset_ctl(struct drm_device *dev, void *data,
1148 struct drm_file *file_priv);
1150 /* AGP/GART support (drm_agpsupport.h) */
1151 extern struct drm_agp_head *drm_agp_init(struct drm_device *dev);
1152 extern int drm_agp_acquire(struct drm_device *dev);
1153 extern int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
1154 struct drm_file *file_priv);
1155 extern int drm_agp_release(struct drm_device *dev);
1156 extern int drm_agp_release_ioctl(struct drm_device *dev, void *data,
1157 struct drm_file *file_priv);
1158 extern int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode);
1159 extern int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
1160 struct drm_file *file_priv);
1161 extern int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info);
1162 extern int drm_agp_info_ioctl(struct drm_device *dev, void *data,
1163 struct drm_file *file_priv);
1164 extern int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request);
1165 extern int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
1166 struct drm_file *file_priv);
1167 extern int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request);
1168 extern int drm_agp_free_ioctl(struct drm_device *dev, void *data,
1169 struct drm_file *file_priv);
1170 extern int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request);
1171 extern int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
1172 struct drm_file *file_priv);
1173 extern int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request);
1174 extern int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
1175 struct drm_file *file_priv);
1176 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
1177 extern DRM_AGP_MEM *drm_agp_allocate_memory(size_t pages, u32 type);
1179 extern DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data *bridge, size_t pages, u32 type);
1181 extern int drm_agp_free_memory(DRM_AGP_MEM * handle);
1182 extern int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start);
1183 extern int drm_agp_unbind_memory(DRM_AGP_MEM * handle);
1184 extern struct drm_ttm_backend *drm_agp_init_ttm(struct drm_device *dev);
1185 extern void drm_agp_chipset_flush(struct drm_device *dev);
1186 /* Stub support (drm_stub.h) */
1187 extern int drm_get_dev(struct pci_dev *pdev, const struct pci_device_id *ent,
1188 struct drm_driver *driver);
1189 extern int drm_put_dev(struct drm_device *dev);
1190 extern int drm_put_head(struct drm_head * head);
1191 extern unsigned int drm_debug; /* 1 to enable debug output */
1192 extern unsigned int drm_cards_limit;
1193 extern struct drm_head **drm_heads;
1194 extern struct class *drm_class;
1195 extern struct proc_dir_entry *drm_proc_root;
1197 extern drm_local_map_t *drm_getsarea(struct drm_device *dev);
1199 /* Proc support (drm_proc.h) */
1200 extern int drm_proc_init(struct drm_device *dev,
1202 struct proc_dir_entry *root,
1203 struct proc_dir_entry **dev_root);
1204 extern int drm_proc_cleanup(int minor,
1205 struct proc_dir_entry *root,
1206 struct proc_dir_entry *dev_root);
1208 /* Scatter Gather Support (drm_scatter.h) */
1209 extern void drm_sg_cleanup(struct drm_sg_mem * entry);
1210 extern int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
1211 struct drm_file *file_priv);
1212 extern int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request);
1213 extern int drm_sg_free(struct drm_device *dev, void *data,
1214 struct drm_file *file_priv);
1216 /* ATI PCIGART support (ati_pcigart.h) */
1217 extern int drm_ati_pcigart_init(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1218 extern int drm_ati_pcigart_cleanup(struct drm_device *dev, struct drm_ati_pcigart_info *gart_info);
1220 extern drm_dma_handle_t *drm_pci_alloc(struct drm_device *dev, size_t size,
1221 size_t align, dma_addr_t maxaddr);
1222 extern void __drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1223 extern void drm_pci_free(struct drm_device *dev, drm_dma_handle_t *dmah);
1225 /* sysfs support (drm_sysfs.c) */
1226 struct drm_sysfs_class;
1227 extern struct class *drm_sysfs_create(struct module *owner, char *name);
1228 extern void drm_sysfs_destroy(void);
1229 extern int drm_sysfs_device_add(struct drm_device *dev, struct drm_head *head);
1230 extern void drm_sysfs_device_remove(struct drm_device *dev);
1233 * Basic memory manager support (drm_mm.c)
1236 extern struct drm_mm_node * drm_mm_get_block(struct drm_mm_node * parent, unsigned long size,
1237 unsigned alignment);
1238 extern void drm_mm_put_block(struct drm_mm_node *cur);
1239 extern struct drm_mm_node *drm_mm_search_free(const struct drm_mm *mm, unsigned long size,
1240 unsigned alignment, int best_match);
1241 extern int drm_mm_init(struct drm_mm *mm, unsigned long start, unsigned long size);
1242 extern void drm_mm_takedown(struct drm_mm *mm);
1243 extern int drm_mm_clean(struct drm_mm *mm);
1244 extern unsigned long drm_mm_tail_space(struct drm_mm *mm);
1245 extern int drm_mm_remove_space_from_tail(struct drm_mm *mm, unsigned long size);
1246 extern int drm_mm_add_space_to_tail(struct drm_mm *mm, unsigned long size);
1248 static inline struct drm_mm *drm_get_mm(struct drm_mm_node *block)
1253 extern void drm_core_ioremap(struct drm_map *map, struct drm_device *dev);
1254 extern void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev);
1256 static __inline__ struct drm_map *drm_core_findmap(struct drm_device *dev,
1259 struct drm_map_list *_entry;
1260 list_for_each_entry(_entry, &dev->maplist, head)
1261 if (_entry->user_token == token)
1266 static __inline__ int drm_device_is_agp(struct drm_device *dev)
1268 if ( dev->driver->device_is_agp != NULL ) {
1269 int err = (*dev->driver->device_is_agp)(dev);
1276 return pci_find_capability(dev->pdev, PCI_CAP_ID_AGP);
1279 static __inline__ int drm_device_is_pcie(struct drm_device *dev)
1281 return pci_find_capability(dev->pdev, PCI_CAP_ID_EXP);
1284 static __inline__ void drm_core_dropmap(struct drm_map *map)
1288 #ifndef DEBUG_MEMORY
1289 /** Wrapper around kmalloc() */
1290 static __inline__ void *drm_alloc(size_t size, int area)
1292 return kmalloc(size, GFP_KERNEL);
1295 /** Wrapper around kfree() */
1296 static __inline__ void drm_free(void *pt, size_t size, int area)
1301 extern void *drm_alloc(size_t size, int area);
1302 extern void drm_free(void *pt, size_t size, int area);
1306 * Accounting variants of standard calls.
1309 static inline void *drm_ctl_alloc(size_t size, int area)
1312 if (drm_alloc_memctl(size))
1314 ret = drm_alloc(size, area);
1316 drm_free_memctl(size);
1320 static inline void *drm_ctl_calloc(size_t nmemb, size_t size, int area)
1324 if (drm_alloc_memctl(nmemb*size))
1326 ret = drm_calloc(nmemb, size, area);
1328 drm_free_memctl(nmemb*size);
1332 static inline void drm_ctl_free(void *pt, size_t size, int area)
1334 drm_free(pt, size, area);
1335 drm_free_memctl(size);
1340 #endif /* __KERNEL__ */