Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / drivers / char / agp / backend.c
blobfc979fb2f788fd990e0af9650e986f60515c6696
1 /*
2 * AGPGART driver backend routines.
3 * Copyright (C) 2002 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.
26 * TODO:
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>
35 #include <linux/pm.h>
36 #include <linux/agp_backend.h>
37 #include <linux/vmalloc.h>
38 #include <asm/io.h>
39 #include "agp.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 = { .type = NOT_SUPPORTED };
49 int agp_backend_acquire(void)
51 if (agp_bridge.type == NOT_SUPPORTED)
52 return -EINVAL;
54 if (atomic_read(&agp_bridge.agp_in_use) != 0)
55 return -EBUSY;
57 atomic_inc(&agp_bridge.agp_in_use);
58 return 0;
61 void agp_backend_release(void)
63 if (agp_bridge.type == NOT_SUPPORTED)
64 return;
66 atomic_dec(&agp_bridge.agp_in_use);
69 struct agp_max_table {
70 int mem;
71 int agp;
74 static struct agp_max_table maxes_table[9] =
76 {0, 0},
77 {32, 4},
78 {64, 28},
79 {128, 96},
80 {256, 204},
81 {512, 440},
82 {1024, 942},
83 {2048, 1920},
84 {4096, 3932}
87 static int agp_find_max (void)
89 long memory, index, result;
91 memory = (num_physpages << PAGE_SHIFT) >> 20;
92 index = 1;
94 while ((memory > maxes_table[index].mem) && (index < 8))
95 index++;
97 result = maxes_table[index - 1].agp +
98 ( (memory - maxes_table[index - 1].mem) *
99 (maxes_table[index].agp - maxes_table[index - 1].agp)) /
100 (maxes_table[index].mem - maxes_table[index - 1].mem);
102 printk(KERN_INFO PFX "Maximum main memory to use for agp memory: %ldM\n", result);
103 result = result << (20 - PAGE_SHIFT);
104 return result;
107 static struct agp_version agp_current_version =
109 .major = AGPGART_VERSION_MAJOR,
110 .minor = AGPGART_VERSION_MINOR,
113 static int agp_backend_initialize(struct pci_dev *dev)
115 int size_value, rc, got_gatt=0, got_keylist=0;
117 agp_bridge.max_memory_agp = agp_find_max();
118 agp_bridge.version = &agp_current_version;
120 if (agp_bridge.needs_scratch_page == TRUE) {
121 void *addr;
122 addr = agp_bridge.agp_alloc_page();
124 if (addr == NULL) {
125 printk(KERN_ERR PFX "unable to get memory for scratch page.\n");
126 return -ENOMEM;
128 agp_bridge.scratch_page_real = virt_to_phys(addr);
129 agp_bridge.scratch_page =
130 agp_bridge.mask_memory(agp_bridge.scratch_page_real, 0);
133 size_value = agp_bridge.fetch_size();
135 if (size_value == 0) {
136 printk(KERN_ERR PFX "unable to determine aperture size.\n");
137 rc = -EINVAL;
138 goto err_out;
140 if (agp_bridge.create_gatt_table()) {
141 printk(KERN_ERR PFX "unable to get memory for graphics translation table.\n");
142 rc = -ENOMEM;
143 goto err_out;
145 got_gatt = 1;
147 agp_bridge.key_list = vmalloc(PAGE_SIZE * 4);
148 if (agp_bridge.key_list == NULL) {
149 printk(KERN_ERR PFX "error allocating memory for key lists.\n");
150 rc = -ENOMEM;
151 goto err_out;
153 got_keylist = 1;
155 /* FIXME vmalloc'd memory not guaranteed contiguous */
156 memset(agp_bridge.key_list, 0, PAGE_SIZE * 4);
158 if (agp_bridge.configure()) {
159 printk(KERN_ERR PFX "error configuring host chipset.\n");
160 rc = -EINVAL;
161 goto err_out;
164 printk(KERN_INFO PFX "AGP aperture is %dM @ 0x%lx\n",
165 size_value, agp_bridge.gart_bus_addr);
167 return 0;
169 err_out:
170 if (agp_bridge.needs_scratch_page == TRUE) {
171 agp_bridge.agp_destroy_page(phys_to_virt(agp_bridge.scratch_page_real));
173 if (got_gatt)
174 agp_bridge.free_gatt_table();
175 if (got_keylist)
176 vfree(agp_bridge.key_list);
177 return rc;
181 /* cannot be __exit b/c as it could be called from __init code */
182 static void agp_backend_cleanup(void)
184 agp_bridge.cleanup();
185 agp_bridge.free_gatt_table();
186 vfree(agp_bridge.key_list);
188 if (agp_bridge.needs_scratch_page == TRUE) {
189 agp_bridge.agp_destroy_page(phys_to_virt(agp_bridge.scratch_page_real));
193 static int agp_power(struct pm_dev *dev, pm_request_t rq, void *data)
195 switch(rq)
197 case PM_SUSPEND:
198 return agp_bridge.suspend();
199 case PM_RESUME:
200 agp_bridge.resume();
201 return 0;
203 return 0;
206 extern int agp_frontend_initialize(void);
207 extern void agp_frontend_cleanup(void);
209 static const drm_agp_t drm_agp = {
210 &agp_free_memory,
211 &agp_allocate_memory,
212 &agp_bind_memory,
213 &agp_unbind_memory,
214 &agp_enable,
215 &agp_backend_acquire,
216 &agp_backend_release,
217 &agp_copy_info
220 static int agp_count=0;
222 int agp_register_driver (struct agp_driver *drv)
224 int ret_val;
226 if (drv->dev == NULL) {
227 printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
228 return -EINVAL;
231 if (agp_count==1) {
232 printk (KERN_DEBUG PFX "Only one agpgart device currently supported.\n");
233 return -ENODEV;
236 /* Grab reference on the chipset driver. */
237 if (!try_module_get(drv->owner))
238 return -EINVAL;
240 ret_val = agp_backend_initialize(drv->dev);
241 if (ret_val)
242 goto err_out;
244 ret_val = agp_frontend_initialize();
245 if (ret_val)
246 goto frontend_err;
248 /* FIXME: What to do with this? */
249 inter_module_register("drm_agp", THIS_MODULE, &drm_agp);
251 pm_register(PM_PCI_DEV, PM_PCI_ID(agp_bridge.dev), agp_power);
252 agp_count++;
253 return 0;
255 frontend_err:
256 agp_backend_cleanup();
257 err_out:
258 agp_bridge.type = NOT_SUPPORTED;
259 module_put(drv->owner);
260 return ret_val;
263 int agp_unregister_driver(struct agp_driver *drv)
265 agp_bridge.type = NOT_SUPPORTED;
266 pm_unregister_all(agp_power);
267 agp_frontend_cleanup();
268 agp_backend_cleanup();
269 inter_module_unregister("drm_agp");
270 agp_count--;
271 module_put(drv->owner);
272 return 0;
276 int __init agp_init(void)
278 static int already_initialised=0;
280 if (already_initialised!=0)
281 return 0;
283 already_initialised = 1;
285 memset(&agp_bridge, 0, sizeof(struct agp_bridge_data));
286 agp_bridge.type = NOT_SUPPORTED;
288 printk(KERN_INFO "Linux agpgart interface v%d.%d (c) Dave Jones\n",
289 AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
290 return 0;
293 void __exit agp_exit(void)
295 if (agp_count!=0)
296 BUG();
299 #ifndef CONFIG_GART_IOMMU
300 module_init(agp_init);
301 module_exit(agp_exit);
302 #endif
304 EXPORT_SYMBOL(agp_backend_acquire);
305 EXPORT_SYMBOL(agp_backend_release);
306 EXPORT_SYMBOL_GPL(agp_register_driver);
307 EXPORT_SYMBOL_GPL(agp_unregister_driver);
309 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
310 MODULE_LICENSE("GPL and additional rights");