2 * AGPGART driver backend routines.
3 * Copyright (C) 2002-2003 Dave Jones.
4 * Copyright (C) 1999 Jeff Hartmann.
5 * Copyright (C) 1999 Precision Insight, Inc.
6 * Copyright (C) 1999 Xi Graphics, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * JEFF HARTMANN, DAVE JONES, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
24 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * - Allocate more than order 0 pages to avoid too much linear map splitting.
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/init.h>
33 #include <linux/pagemap.h>
34 #include <linux/miscdevice.h>
36 #include <linux/agp_backend.h>
37 #include <linux/vmalloc.h>
41 /* Due to XFree86 brain-damage, we can't go to 1.0 until they
42 * fix some real stupidity. It's only by chance we can bump
43 * past 0.99 at all due to some boolean logic error. */
44 #define AGPGART_VERSION_MAJOR 0
45 #define AGPGART_VERSION_MINOR 100
46 static struct agp_version agp_current_version
=
48 .major
= AGPGART_VERSION_MAJOR
,
49 .minor
= AGPGART_VERSION_MINOR
,
52 static int agp_count
=0;
54 struct agp_bridge_data agp_bridge_dummy
= { .type
= NOT_SUPPORTED
};
55 struct agp_bridge_data
*agp_bridge
= &agp_bridge_dummy
;
56 EXPORT_SYMBOL(agp_bridge
);
60 * agp_backend_acquire - attempt to acquire the agp backend.
62 * returns -EBUSY if agp is in use,
63 * returns 0 if the caller owns the agp backend
65 int agp_backend_acquire(void)
67 if (agp_bridge
->type
== NOT_SUPPORTED
)
69 if (atomic_read(&agp_bridge
->agp_in_use
))
71 atomic_inc(&agp_bridge
->agp_in_use
);
74 EXPORT_SYMBOL(agp_backend_acquire
);
78 * agp_backend_release - release the lock on the agp backend.
80 * The caller must insure that the graphics aperture translation table
81 * is read for use by another entity.
83 * (Ensure that all memory it bound is unbound.)
85 void agp_backend_release(void)
87 if (agp_bridge
->type
!= NOT_SUPPORTED
)
88 atomic_dec(&agp_bridge
->agp_in_use
);
90 EXPORT_SYMBOL(agp_backend_release
);
93 struct { int mem
, agp
; } maxes_table
[] = {
105 static int agp_find_max(void)
107 long memory
, index
, result
;
109 memory
= (num_physpages
<< PAGE_SHIFT
) >> 20;
112 while ((memory
> maxes_table
[index
].mem
) && (index
< 8))
115 result
= maxes_table
[index
- 1].agp
+
116 ( (memory
- maxes_table
[index
- 1].mem
) *
117 (maxes_table
[index
].agp
- maxes_table
[index
- 1].agp
)) /
118 (maxes_table
[index
].mem
- maxes_table
[index
- 1].mem
);
120 printk(KERN_INFO PFX
"Maximum main memory to use for agp memory: %ldM\n", result
);
121 result
= result
<< (20 - PAGE_SHIFT
);
126 static int agp_backend_initialize(struct agp_bridge_data
*bridge
)
128 int size_value
, rc
, got_gatt
=0, got_keylist
=0;
130 bridge
->max_memory_agp
= agp_find_max();
131 bridge
->version
= &agp_current_version
;
133 if (bridge
->driver
->needs_scratch_page
) {
134 void *addr
= bridge
->driver
->agp_alloc_page();
137 printk(KERN_ERR PFX
"unable to get memory for scratch page.\n");
141 bridge
->scratch_page_real
= virt_to_phys(addr
);
142 bridge
->scratch_page
=
143 bridge
->driver
->mask_memory(bridge
->scratch_page_real
, 0);
146 size_value
= bridge
->driver
->fetch_size();
147 if (size_value
== 0) {
148 printk(KERN_ERR PFX
"unable to determine aperture size.\n");
152 if (bridge
->driver
->create_gatt_table()) {
154 "unable to get memory for graphics translation table.\n");
160 bridge
->key_list
= vmalloc(PAGE_SIZE
* 4);
161 if (bridge
->key_list
== NULL
) {
162 printk(KERN_ERR PFX
"error allocating memory for key lists.\n");
168 /* FIXME vmalloc'd memory not guaranteed contiguous */
169 memset(bridge
->key_list
, 0, PAGE_SIZE
* 4);
171 if (bridge
->driver
->configure()) {
172 printk(KERN_ERR PFX
"error configuring host chipset.\n");
177 printk(KERN_INFO PFX
"AGP aperture is %dM @ 0x%lx\n",
178 size_value
, bridge
->gart_bus_addr
);
183 if (bridge
->driver
->needs_scratch_page
)
184 bridge
->driver
->agp_destroy_page(
185 phys_to_virt(bridge
->scratch_page_real
));
187 bridge
->driver
->free_gatt_table();
189 vfree(bridge
->key_list
);
190 bridge
->key_list
= NULL
;
195 /* cannot be __exit b/c as it could be called from __init code */
196 static void agp_backend_cleanup(struct agp_bridge_data
*bridge
)
198 if (bridge
->driver
->cleanup
)
199 bridge
->driver
->cleanup();
200 if (bridge
->driver
->free_gatt_table
)
201 bridge
->driver
->free_gatt_table();
202 if (bridge
->key_list
) {
203 vfree(bridge
->key_list
);
204 bridge
->key_list
= NULL
;
207 if (bridge
->driver
->agp_destroy_page
&&
208 bridge
->driver
->needs_scratch_page
)
209 bridge
->driver
->agp_destroy_page(
210 phys_to_virt(bridge
->scratch_page_real
));
213 static const drm_agp_t drm_agp
= {
215 &agp_allocate_memory
,
219 &agp_backend_acquire
,
220 &agp_backend_release
,
224 /* XXX Kludge alert: agpgart isn't ready for multiple bridges yet */
225 struct agp_bridge_data
*agp_alloc_bridge(void)
229 EXPORT_SYMBOL(agp_alloc_bridge
);
232 void agp_put_bridge(struct agp_bridge_data
*bridge
)
235 EXPORT_SYMBOL(agp_put_bridge
);
238 int agp_add_bridge(struct agp_bridge_data
*bridge
)
243 printk (KERN_DEBUG PFX
"Erk, registering with no pci_dev!\n");
248 printk (KERN_INFO PFX
249 "Only one agpgart device currently supported.\n");
253 /* Grab reference on the chipset driver. */
254 if (!try_module_get(bridge
->driver
->owner
)) {
255 printk (KERN_INFO PFX
"Couldn't lock chipset driver.\n");
259 bridge
->type
= SUPPORTED
;
261 error
= agp_backend_initialize(agp_bridge
);
263 printk (KERN_INFO PFX
"agp_backend_initialize() failed.\n");
267 error
= agp_frontend_initialize();
269 printk (KERN_INFO PFX
"agp_frontend_initialize() failed.\n");
273 /* FIXME: What to do with this? */
274 inter_module_register("drm_agp", THIS_MODULE
, &drm_agp
);
280 agp_backend_cleanup(agp_bridge
);
282 bridge
->type
= NOT_SUPPORTED
;
283 module_put(bridge
->driver
->owner
);
286 EXPORT_SYMBOL_GPL(agp_add_bridge
);
289 void agp_remove_bridge(struct agp_bridge_data
*bridge
)
291 bridge
->type
= NOT_SUPPORTED
;
292 agp_frontend_cleanup();
293 agp_backend_cleanup(bridge
);
294 inter_module_unregister("drm_agp");
296 module_put(bridge
->driver
->owner
);
298 EXPORT_SYMBOL_GPL(agp_remove_bridge
);
301 static int __init
agp_init(void)
303 printk(KERN_INFO
"Linux agpgart interface v%d.%d (c) Dave Jones\n",
304 AGPGART_VERSION_MAJOR
, AGPGART_VERSION_MINOR
);
308 void __exit
agp_exit(void)
313 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
314 MODULE_DESCRIPTION("AGP GART driver");
315 MODULE_LICENSE("GPL and additional rights");
317 module_init(agp_init
);
318 module_exit(agp_exit
);