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
47 struct agp_bridge_data agp_bridge_dummy = { .type = NOT_SUPPORTED };
48 struct agp_bridge_data *agp_bridge = &agp_bridge_dummy;
50 int agp_backend_acquire(void)
52 if (agp_bridge->type == NOT_SUPPORTED)
55 if (atomic_read(&agp_bridge->agp_in_use) != 0)
58 atomic_inc(&agp_bridge->agp_in_use);
62 void agp_backend_release(void)
64 if (agp_bridge->type == NOT_SUPPORTED)
67 atomic_dec(&agp_bridge->agp_in_use);
70 struct agp_max_table {
75 static struct agp_max_table maxes_table[9] =
88 static int agp_find_max (void)
90 long memory, index, result;
92 memory = (num_physpages << PAGE_SHIFT) >> 20;
95 while ((memory > maxes_table[index].mem) && (index < 8))
98 result = maxes_table[index - 1].agp +
99 ( (memory - maxes_table[index - 1].mem) *
100 (maxes_table[index].agp - maxes_table[index - 1].agp)) /
101 (maxes_table[index].mem - maxes_table[index - 1].mem);
103 printk(KERN_INFO PFX "Maximum main memory to use for agp memory: %ldM\n", result);
104 result = result << (20 - PAGE_SHIFT);
108 static struct agp_version agp_current_version =
110 .major = AGPGART_VERSION_MAJOR,
111 .minor = AGPGART_VERSION_MINOR,
114 static int agp_backend_initialize(struct pci_dev *dev)
116 int size_value, rc, got_gatt=0, got_keylist=0;
118 agp_bridge->max_memory_agp = agp_find_max();
119 agp_bridge->version = &agp_current_version;
121 if (agp_bridge->needs_scratch_page == TRUE) {
123 addr = agp_bridge->agp_alloc_page();
126 printk(KERN_ERR PFX "unable to get memory for scratch page.\n");
129 agp_bridge->scratch_page_real = virt_to_phys(addr);
130 agp_bridge->scratch_page =
131 agp_bridge->mask_memory(agp_bridge->scratch_page_real, 0);
134 size_value = agp_bridge->fetch_size();
136 if (size_value == 0) {
137 printk(KERN_ERR PFX "unable to determine aperture size.\n");
141 if (agp_bridge->create_gatt_table()) {
142 printk(KERN_ERR PFX "unable to get memory for graphics translation table.\n");
148 agp_bridge->key_list = vmalloc(PAGE_SIZE * 4);
149 if (agp_bridge->key_list == NULL) {
150 printk(KERN_ERR PFX "error allocating memory for key lists.\n");
156 /* FIXME vmalloc'd memory not guaranteed contiguous */
157 memset(agp_bridge->key_list, 0, PAGE_SIZE * 4);
159 if (agp_bridge->configure()) {
160 printk(KERN_ERR PFX "error configuring host chipset.\n");
165 printk(KERN_INFO PFX "AGP aperture is %dM @ 0x%lx\n",
166 size_value, agp_bridge->gart_bus_addr);
171 if (agp_bridge->needs_scratch_page == TRUE) {
172 agp_bridge->agp_destroy_page(phys_to_virt(agp_bridge->scratch_page_real));
175 agp_bridge->free_gatt_table();
177 vfree(agp_bridge->key_list);
182 /* cannot be __exit b/c as it could be called from __init code */
183 static void agp_backend_cleanup(void)
185 if (agp_bridge->cleanup != NULL)
186 agp_bridge->cleanup();
187 if (agp_bridge->free_gatt_table != NULL)
188 agp_bridge->free_gatt_table();
189 if (agp_bridge->key_list)
190 vfree(agp_bridge->key_list);
192 if ((agp_bridge->agp_destroy_page!=NULL) &&
193 (agp_bridge->needs_scratch_page == TRUE))
194 agp_bridge->agp_destroy_page(phys_to_virt(agp_bridge->scratch_page_real));
197 static int agp_power(struct pm_dev *dev, pm_request_t rq, void *data)
202 return agp_bridge->suspend();
204 agp_bridge->resume();
210 extern int agp_frontend_initialize(void);
211 extern void agp_frontend_cleanup(void);
213 static const drm_agp_t drm_agp = {
215 &agp_allocate_memory,
219 &agp_backend_acquire,
220 &agp_backend_release,
224 static int agp_count=0;
226 int agp_register_driver (struct agp_driver *drv)
230 if (drv->dev == NULL) {
231 printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
236 printk (KERN_DEBUG PFX "Only one agpgart device currently supported.\n");
240 /* Grab reference on the chipset driver. */
241 if (!try_module_get(drv->owner))
244 ret_val = agp_backend_initialize(drv->dev);
248 ret_val = agp_frontend_initialize();
252 /* FIXME: What to do with this? */
253 inter_module_register("drm_agp", THIS_MODULE, &drm_agp);
256 pm_register(PM_PCI_DEV, PM_PCI_ID(agp_bridge->dev), agp_power);
262 agp_backend_cleanup();
264 agp_bridge->type = NOT_SUPPORTED;
265 module_put(drv->owner);
270 int agp_unregister_driver(struct agp_driver *drv)
275 agp_bridge->type = NOT_SUPPORTED;
276 pm_unregister_all(agp_power);
277 agp_frontend_cleanup();
278 agp_backend_cleanup();
279 inter_module_unregister("drm_agp");
281 module_put(drv->owner);
286 int __init agp_init(void)
288 static int already_initialised=0;
290 if (already_initialised!=0)
293 already_initialised = 1;
295 memset(agp_bridge, 0, sizeof(struct agp_bridge_data));
296 agp_bridge->type = NOT_SUPPORTED;
298 printk(KERN_INFO "Linux agpgart interface v%d.%d (c) Dave Jones\n",
299 AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
303 void __exit agp_exit(void)
309 #ifndef CONFIG_GART_IOMMU
310 module_init(agp_init);
311 module_exit(agp_exit);
314 EXPORT_SYMBOL(agp_backend_acquire);
315 EXPORT_SYMBOL(agp_backend_release);
316 EXPORT_SYMBOL_GPL(agp_register_driver);
317 EXPORT_SYMBOL_GPL(agp_unregister_driver);
319 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
320 MODULE_LICENSE("GPL and additional rights");