Add ia64 patch.
[linux-flexiantxendom0-3.2.10.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/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"
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
47 struct agp_bridge_data agp_bridge_dummy = { .type = NOT_SUPPORTED };
48 struct agp_bridge_data *agp_bridge = &agp_bridge_dummy;
49
50 int agp_backend_acquire(void)
51 {
52         if (agp_bridge->type == NOT_SUPPORTED)
53                 return -EINVAL;
54
55         if (atomic_read(&agp_bridge->agp_in_use) != 0)
56                 return -EBUSY;
57
58         atomic_inc(&agp_bridge->agp_in_use);
59         return 0;
60 }
61
62 void agp_backend_release(void)
63 {
64         if (agp_bridge->type == NOT_SUPPORTED)
65                 return;
66
67         atomic_dec(&agp_bridge->agp_in_use);
68 }
69
70 struct agp_max_table {
71         int mem;
72         int agp;
73 };
74
75 static struct agp_max_table maxes_table[9] =
76 {
77         {0, 0},
78         {32, 4},
79         {64, 28},
80         {128, 96},
81         {256, 204},
82         {512, 440},
83         {1024, 942},
84         {2048, 1920},
85         {4096, 3932}
86 };
87
88 static int agp_find_max (void)
89 {
90         long memory, index, result;
91
92         memory = (num_physpages << PAGE_SHIFT) >> 20;
93         index = 1;
94
95         while ((memory > maxes_table[index].mem) && (index < 8))
96                 index++;
97
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);
102
103         printk(KERN_INFO PFX "Maximum main memory to use for agp memory: %ldM\n", result);
104         result = result << (20 - PAGE_SHIFT);
105         return result;
106 }
107
108 static struct agp_version agp_current_version =
109 {
110         .major = AGPGART_VERSION_MAJOR,
111         .minor = AGPGART_VERSION_MINOR,
112 };
113
114 static int agp_backend_initialize(struct pci_dev *dev)
115 {
116         int size_value, rc, got_gatt=0, got_keylist=0;
117
118         agp_bridge->max_memory_agp = agp_find_max();
119         agp_bridge->version = &agp_current_version;
120
121         if (agp_bridge->needs_scratch_page == TRUE) {
122                 void *addr;
123                 addr = agp_bridge->agp_alloc_page();
124
125                 if (addr == NULL) {
126                         printk(KERN_ERR PFX "unable to get memory for scratch page.\n");
127                         return -ENOMEM;
128                 }
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);
132         }
133
134         size_value = agp_bridge->fetch_size();
135
136         if (size_value == 0) {
137                 printk(KERN_ERR PFX "unable to determine aperture size.\n");
138                 rc = -EINVAL;
139                 goto err_out;
140         }
141         if (agp_bridge->create_gatt_table()) {
142                 printk(KERN_ERR PFX "unable to get memory for graphics translation table.\n");
143                 rc = -ENOMEM;
144                 goto err_out;
145         }
146         got_gatt = 1;
147         
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");
151                 rc = -ENOMEM;
152                 goto err_out;
153         }
154         got_keylist = 1;
155         
156         /* FIXME vmalloc'd memory not guaranteed contiguous */
157         memset(agp_bridge->key_list, 0, PAGE_SIZE * 4);
158
159         if (agp_bridge->configure()) {
160                 printk(KERN_ERR PFX "error configuring host chipset.\n");
161                 rc = -EINVAL;
162                 goto err_out;
163         }
164
165         printk(KERN_INFO PFX "AGP aperture is %dM @ 0x%lx\n",
166                size_value, agp_bridge->gart_bus_addr);
167
168         return 0;
169
170 err_out:
171         if (agp_bridge->needs_scratch_page == TRUE) {
172                 agp_bridge->agp_destroy_page(phys_to_virt(agp_bridge->scratch_page_real));
173         }
174         if (got_gatt)
175                 agp_bridge->free_gatt_table();
176         if (got_keylist)
177                 vfree(agp_bridge->key_list);
178         return rc;
179 }
180
181
182 /* cannot be __exit b/c as it could be called from __init code */
183 static void agp_backend_cleanup(void)
184 {
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);
191
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));
195 }
196
197 static int agp_power(struct pm_dev *dev, pm_request_t rq, void *data)
198 {
199         switch(rq)
200         {
201                 case PM_SUSPEND:
202                         return agp_bridge->suspend();
203                 case PM_RESUME:
204                         agp_bridge->resume();
205                         return 0;
206         }               
207         return 0;
208 }
209
210 extern int agp_frontend_initialize(void);
211 extern void agp_frontend_cleanup(void);
212
213 static const drm_agp_t drm_agp = {
214         &agp_free_memory,
215         &agp_allocate_memory,
216         &agp_bind_memory,
217         &agp_unbind_memory,
218         &agp_enable,
219         &agp_backend_acquire,
220         &agp_backend_release,
221         &agp_copy_info
222 };
223
224 static int agp_count=0;
225
226 int agp_register_driver (struct agp_driver *drv)
227 {
228         int ret_val;
229
230         if (drv->dev == NULL) {
231                 printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
232                 return -EINVAL;
233         }
234
235         if (agp_count==1) {
236                 printk (KERN_DEBUG PFX "Only one agpgart device currently supported.\n");
237                 return -ENODEV;
238         }
239
240         /* Grab reference on the chipset driver. */
241         if (!try_module_get(drv->owner))
242                 return -EINVAL;
243
244         ret_val = agp_backend_initialize(drv->dev);
245         if (ret_val)
246                 goto err_out;
247
248         ret_val = agp_frontend_initialize();
249         if (ret_val)
250                 goto frontend_err;
251
252         /* FIXME: What to do with this? */
253         inter_module_register("drm_agp", THIS_MODULE, &drm_agp);
254
255 #if 0
256         pm_register(PM_PCI_DEV, PM_PCI_ID(agp_bridge->dev), agp_power);
257 #endif
258         agp_count++;
259         return 0;
260
261 frontend_err:
262         agp_backend_cleanup();
263 err_out:
264         agp_bridge->type = NOT_SUPPORTED;
265         module_put(drv->owner);
266         drv->dev = NULL;
267         return ret_val;
268 }
269
270 int agp_unregister_driver(struct agp_driver *drv)
271 {
272         if (drv->dev==NULL)
273                 return -ENODEV;
274
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");
280         agp_count--;
281         module_put(drv->owner);
282         return 0;
283 }
284
285
286 int __init agp_init(void)
287 {
288         static int already_initialised=0;
289
290         if (already_initialised!=0)
291                 return 0;
292
293         already_initialised = 1;
294
295         memset(agp_bridge, 0, sizeof(struct agp_bridge_data));
296         agp_bridge->type = NOT_SUPPORTED;
297
298         printk(KERN_INFO "Linux agpgart interface v%d.%d (c) Dave Jones\n",
299                AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
300         return 0;
301 }
302
303 void __exit agp_exit(void)
304 {
305         if (agp_count!=0)
306                 BUG();
307 }
308
309 #ifndef CONFIG_GART_IOMMU
310 module_init(agp_init);
311 module_exit(agp_exit);
312 #endif
313
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);
318
319 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>");
320 MODULE_LICENSE("GPL and additional rights");