ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / agp / backend.c
1 /*
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.
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
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.
25  *
26  * TODO: 
27  * - Allocate more than order 0 pages to avoid too much linear map splitting.
28  */
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/init.h>
32 #include <linux/pagemap.h>
33 #include <linux/miscdevice.h>
34 #include <linux/pm.h>
35 #include <linux/agp_backend.h>
36 #include <linux/agpgart.h>
37 #include <linux/vmalloc.h>
38 #include <asm/io.h>
39 #include "agp.h"
40
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 =
47 {
48         .major = AGPGART_VERSION_MAJOR,
49         .minor = AGPGART_VERSION_MINOR,
50 };
51
52 static int agp_count=0;
53
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);
57
58
59 /**
60  *      agp_backend_acquire  -  attempt to acquire the agp backend.
61  *
62  *      returns -EBUSY if agp is in use,
63  *      returns 0 if the caller owns the agp backend
64  */
65 int agp_backend_acquire(void)
66 {
67         if (agp_bridge->type == NOT_SUPPORTED)
68                 return -EINVAL;
69         if (atomic_read(&agp_bridge->agp_in_use))
70                 return -EBUSY;
71         atomic_inc(&agp_bridge->agp_in_use);
72         return 0;
73 }
74 EXPORT_SYMBOL(agp_backend_acquire);
75
76
77 /**
78  *      agp_backend_release  -  release the lock on the agp backend.
79  *
80  *      The caller must insure that the graphics aperture translation table
81  *      is read for use by another entity.
82  *
83  *      (Ensure that all memory it bound is unbound.)
84  */
85 void agp_backend_release(void)
86 {
87         if (agp_bridge->type != NOT_SUPPORTED)
88                 atomic_dec(&agp_bridge->agp_in_use);
89 }
90 EXPORT_SYMBOL(agp_backend_release);
91
92
93 struct { int mem, agp; } maxes_table[] = {
94         {0, 0},
95         {32, 4},
96         {64, 28},
97         {128, 96},
98         {256, 204},
99         {512, 440},
100         {1024, 942},
101         {2048, 1920},
102         {4096, 3932}
103 };
104
105 static int agp_find_max(void)
106 {
107         long memory, index, result;
108
109 #if PAGE_SHIFT < 20
110         memory = num_physpages >> (20 - PAGE_SHIFT);
111 #else
112         memory = num_physpages << (PAGE_SHIFT - 20);
113 #endif
114         index = 1;
115
116         while ((memory > maxes_table[index].mem) && (index < 8))
117                 index++;
118
119         result = maxes_table[index - 1].agp +
120            ( (memory - maxes_table[index - 1].mem)  *
121              (maxes_table[index].agp - maxes_table[index - 1].agp)) /
122            (maxes_table[index].mem - maxes_table[index - 1].mem);
123
124         printk(KERN_INFO PFX "Maximum main memory to use for agp memory: %ldM\n", result);
125         result = result << (20 - PAGE_SHIFT);
126         return result;
127 }
128
129
130 static int agp_backend_initialize(struct agp_bridge_data *bridge)
131 {
132         int size_value, rc, got_gatt=0, got_keylist=0;
133
134         bridge->max_memory_agp = agp_find_max();
135         bridge->version = &agp_current_version;
136
137         if (bridge->driver->needs_scratch_page) {
138                 void *addr = bridge->driver->agp_alloc_page();
139
140                 if (!addr) {
141                         printk(KERN_ERR PFX "unable to get memory for scratch page.\n");
142                         return -ENOMEM;
143                 }
144
145                 bridge->scratch_page_real = virt_to_phys(addr);
146                 bridge->scratch_page =
147                     bridge->driver->mask_memory(bridge->scratch_page_real, 0);
148         }
149
150         size_value = bridge->driver->fetch_size();
151         if (size_value == 0) {
152                 printk(KERN_ERR PFX "unable to determine aperture size.\n");
153                 rc = -EINVAL;
154                 goto err_out;
155         }
156         if (bridge->driver->create_gatt_table()) {
157                 printk(KERN_ERR PFX
158                     "unable to get memory for graphics translation table.\n");
159                 rc = -ENOMEM;
160                 goto err_out;
161         }
162         got_gatt = 1;
163         
164         bridge->key_list = vmalloc(PAGE_SIZE * 4);
165         if (bridge->key_list == NULL) {
166                 printk(KERN_ERR PFX "error allocating memory for key lists.\n");
167                 rc = -ENOMEM;
168                 goto err_out;
169         }
170         got_keylist = 1;
171         
172         /* FIXME vmalloc'd memory not guaranteed contiguous */
173         memset(bridge->key_list, 0, PAGE_SIZE * 4);
174
175         if (bridge->driver->configure()) {
176                 printk(KERN_ERR PFX "error configuring host chipset.\n");
177                 rc = -EINVAL;
178                 goto err_out;
179         }
180
181         printk(KERN_INFO PFX "AGP aperture is %dM @ 0x%lx\n",
182                size_value, bridge->gart_bus_addr);
183
184         return 0;
185
186 err_out:
187         if (bridge->driver->needs_scratch_page)
188                 bridge->driver->agp_destroy_page(
189                                 phys_to_virt(bridge->scratch_page_real));
190         if (got_gatt)
191                 bridge->driver->free_gatt_table();
192         if (got_keylist) {
193                 vfree(bridge->key_list);
194                 bridge->key_list = NULL;
195         }
196         return rc;
197 }
198
199 /* cannot be __exit b/c as it could be called from __init code */
200 static void agp_backend_cleanup(struct agp_bridge_data *bridge)
201 {
202         if (bridge->driver->cleanup)
203                 bridge->driver->cleanup();
204         if (bridge->driver->free_gatt_table)
205                 bridge->driver->free_gatt_table();
206         if (bridge->key_list) {
207                 vfree(bridge->key_list);
208                 bridge->key_list = NULL;
209         }
210
211         if (bridge->driver->agp_destroy_page &&
212             bridge->driver->needs_scratch_page)
213                 bridge->driver->agp_destroy_page(
214                                 phys_to_virt(bridge->scratch_page_real));
215 }
216
217 static const drm_agp_t drm_agp = {
218         &agp_free_memory,
219         &agp_allocate_memory,
220         &agp_bind_memory,
221         &agp_unbind_memory,
222         &agp_enable,
223         &agp_backend_acquire,
224         &agp_backend_release,
225         &agp_copy_info
226 };
227
228 /* XXX Kludge alert: agpgart isn't ready for multiple bridges yet */
229 struct agp_bridge_data *agp_alloc_bridge(void)
230 {
231         return agp_bridge;
232 }
233 EXPORT_SYMBOL(agp_alloc_bridge);
234
235
236 void agp_put_bridge(struct agp_bridge_data *bridge)
237 {
238 }
239 EXPORT_SYMBOL(agp_put_bridge);
240
241  
242 int agp_add_bridge(struct agp_bridge_data *bridge)
243 {
244         int error;
245
246         if (!bridge->dev) {
247                 printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
248                 return -EINVAL;
249         }
250
251         if (agp_count) {
252                 printk (KERN_INFO PFX
253                        "Only one agpgart device currently supported.\n");
254                 return -ENODEV;
255         }
256
257         /* Grab reference on the chipset driver. */
258         if (!try_module_get(bridge->driver->owner)) {
259                 printk (KERN_INFO PFX "Couldn't lock chipset driver.\n");
260                 return -EINVAL;
261         }
262
263         bridge->type = SUPPORTED;
264
265         error = agp_backend_initialize(agp_bridge);
266         if (error) {
267                 printk (KERN_INFO PFX "agp_backend_initialize() failed.\n");
268                 goto err_out;
269         }
270
271         error = agp_frontend_initialize();
272         if (error) {
273                 printk (KERN_INFO PFX "agp_frontend_initialize() failed.\n");
274                 goto frontend_err;
275         }
276
277         /* FIXME: What to do with this? */
278         inter_module_register("drm_agp", THIS_MODULE, &drm_agp);
279
280         agp_count++;
281         return 0;
282
283 frontend_err:
284         agp_backend_cleanup(agp_bridge);
285 err_out:
286         bridge->type = NOT_SUPPORTED;
287         module_put(bridge->driver->owner);
288         return error;
289 }
290 EXPORT_SYMBOL_GPL(agp_add_bridge);
291
292
293 void agp_remove_bridge(struct agp_bridge_data *bridge)
294 {
295         bridge->type = NOT_SUPPORTED;
296         agp_frontend_cleanup();
297         agp_backend_cleanup(bridge);
298         inter_module_unregister("drm_agp");
299         agp_count--;
300         module_put(bridge->driver->owner);
301 }
302 EXPORT_SYMBOL_GPL(agp_remove_bridge);
303
304 int agp_off;
305 int agp_try_unsupported_boot;
306 EXPORT_SYMBOL(agp_off);
307 EXPORT_SYMBOL(agp_try_unsupported_boot);
308
309 static int __init agp_init(void)
310 {
311         if (!agp_off) 
312         printk(KERN_INFO "Linux agpgart interface v%d.%d (c) Dave Jones\n",
313                AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
314         return 0;
315 }
316
317 void __exit agp_exit(void)
318 {
319 }
320
321 #ifndef MODULE
322 static __init int agp_setup(char *s)
323 {
324         if (!strcmp(s,"off"))
325                 agp_off = 1;
326         if (!strcmp(s,"try_unsupported"))
327                 agp_try_unsupported_boot = 1;
328         return 1;       
329 }
330 __setup("agp=", agp_setup);
331 #endif
332
333 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
334 MODULE_DESCRIPTION("AGP GART driver");
335 MODULE_LICENSE("GPL and additional rights");
336 MODULE_ALIAS_MISCDEV(AGPGART_MINOR);
337
338 module_init(agp_init);
339 module_exit(agp_exit);
340