agp: tidy up handling of scratch pages w.r.t. DMA API
[safe/jmp/linux-2.6] / drivers / char / agp / backend.c
1 /*
2  * AGPGART driver backend routines.
3  * Copyright (C) 2004 Silicon Graphics, Inc.
4  * Copyright (C) 2002-2003 Dave Jones.
5  * Copyright (C) 1999 Jeff Hartmann.
6  * Copyright (C) 1999 Precision Insight, Inc.
7  * Copyright (C) 1999 Xi Graphics, Inc.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * JEFF HARTMANN, DAVE JONES, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * TODO:
28  * - Allocate more than order 0 pages to avoid too much linear map splitting.
29  */
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/agpgart.h>
38 #include <linux/vmalloc.h>
39 #include <asm/io.h>
40 #include "agp.h"
41
42 /* Due to XFree86 brain-damage, we can't go to 1.0 until they
43  * fix some real stupidity. It's only by chance we can bump
44  * past 0.99 at all due to some boolean logic error. */
45 #define AGPGART_VERSION_MAJOR 0
46 #define AGPGART_VERSION_MINOR 103
47 static const struct agp_version agp_current_version =
48 {
49         .major = AGPGART_VERSION_MAJOR,
50         .minor = AGPGART_VERSION_MINOR,
51 };
52
53 struct agp_bridge_data *(*agp_find_bridge)(struct pci_dev *) =
54         &agp_generic_find_bridge;
55
56 struct agp_bridge_data *agp_bridge;
57 LIST_HEAD(agp_bridges);
58 EXPORT_SYMBOL(agp_bridge);
59 EXPORT_SYMBOL(agp_bridges);
60 EXPORT_SYMBOL(agp_find_bridge);
61
62 /**
63  *      agp_backend_acquire  -  attempt to acquire an agp backend.
64  *
65  */
66 struct agp_bridge_data *agp_backend_acquire(struct pci_dev *pdev)
67 {
68         struct agp_bridge_data *bridge;
69
70         bridge = agp_find_bridge(pdev);
71
72         if (!bridge)
73                 return NULL;
74
75         if (atomic_read(&bridge->agp_in_use))
76                 return NULL;
77         atomic_inc(&bridge->agp_in_use);
78         return bridge;
79 }
80 EXPORT_SYMBOL(agp_backend_acquire);
81
82
83 /**
84  *      agp_backend_release  -  release the lock on the agp backend.
85  *
86  *      The caller must insure that the graphics aperture translation table
87  *      is read for use by another entity.
88  *
89  *      (Ensure that all memory it bound is unbound.)
90  */
91 void agp_backend_release(struct agp_bridge_data *bridge)
92 {
93
94         if (bridge)
95                 atomic_dec(&bridge->agp_in_use);
96 }
97 EXPORT_SYMBOL(agp_backend_release);
98
99
100 static const struct { int mem, agp; } maxes_table[] = {
101         {0, 0},
102         {32, 4},
103         {64, 28},
104         {128, 96},
105         {256, 204},
106         {512, 440},
107         {1024, 942},
108         {2048, 1920},
109         {4096, 3932}
110 };
111
112 static int agp_find_max(void)
113 {
114         long memory, index, result;
115
116 #if PAGE_SHIFT < 20
117         memory = num_physpages >> (20 - PAGE_SHIFT);
118 #else
119         memory = num_physpages << (PAGE_SHIFT - 20);
120 #endif
121         index = 1;
122
123         while ((memory > maxes_table[index].mem) && (index < 8))
124                 index++;
125
126         result = maxes_table[index - 1].agp +
127            ( (memory - maxes_table[index - 1].mem)  *
128              (maxes_table[index].agp - maxes_table[index - 1].agp)) /
129            (maxes_table[index].mem - maxes_table[index - 1].mem);
130
131         result = result << (20 - PAGE_SHIFT);
132         return result;
133 }
134
135
136 static int agp_backend_initialize(struct agp_bridge_data *bridge)
137 {
138         int size_value, rc, got_gatt=0, got_keylist=0;
139
140         bridge->max_memory_agp = agp_find_max();
141         bridge->version = &agp_current_version;
142
143         if (bridge->driver->needs_scratch_page) {
144                 struct page *page = bridge->driver->agp_alloc_page(bridge);
145
146                 if (!page) {
147                         dev_err(&bridge->dev->dev,
148                                 "can't get memory for scratch page\n");
149                         return -ENOMEM;
150                 }
151
152                 bridge->scratch_page_real = phys_to_gart(page_to_phys(page));
153                 if (bridge->driver->agp_map_page) {
154                         if (bridge->driver->agp_map_page(phys_to_virt(page_to_phys(page)),
155                                                          &bridge->scratch_page_dma)) {
156                                 dev_err(&bridge->dev->dev,
157                                         "unable to dma-map scratch page\n");
158                                 rc = -ENOMEM;
159                                 goto err_out_nounmap;
160                         }
161                 } else {
162                         bridge->scratch_page_dma = phys_to_gart(page_to_phys(page));
163                 }
164
165                 bridge->scratch_page = bridge->driver->mask_memory(bridge,
166                                                    bridge->scratch_page_dma, 0);
167         }
168
169         size_value = bridge->driver->fetch_size();
170         if (size_value == 0) {
171                 dev_err(&bridge->dev->dev, "can't determine aperture size\n");
172                 rc = -EINVAL;
173                 goto err_out;
174         }
175         if (bridge->driver->create_gatt_table(bridge)) {
176                 dev_err(&bridge->dev->dev,
177                         "can't get memory for graphics translation table\n");
178                 rc = -ENOMEM;
179                 goto err_out;
180         }
181         got_gatt = 1;
182
183         bridge->key_list = vmalloc(PAGE_SIZE * 4);
184         if (bridge->key_list == NULL) {
185                 dev_err(&bridge->dev->dev,
186                         "can't allocate memory for key lists\n");
187                 rc = -ENOMEM;
188                 goto err_out;
189         }
190         got_keylist = 1;
191
192         /* FIXME vmalloc'd memory not guaranteed contiguous */
193         memset(bridge->key_list, 0, PAGE_SIZE * 4);
194
195         if (bridge->driver->configure()) {
196                 dev_err(&bridge->dev->dev, "error configuring host chipset\n");
197                 rc = -EINVAL;
198                 goto err_out;
199         }
200         INIT_LIST_HEAD(&bridge->mapped_list);
201         spin_lock_init(&bridge->mapped_lock);
202
203         return 0;
204
205 err_out:
206         if (bridge->driver->needs_scratch_page &&
207             bridge->driver->agp_unmap_page) {
208                 void *va = gart_to_virt(bridge->scratch_page_real);
209
210                 bridge->driver->agp_unmap_page(va, bridge->scratch_page_dma);
211         }
212 err_out_nounmap:
213         if (bridge->driver->needs_scratch_page) {
214                 void *va = gart_to_virt(bridge->scratch_page_real);
215
216                 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
217                 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
218         }
219         if (got_gatt)
220                 bridge->driver->free_gatt_table(bridge);
221         if (got_keylist) {
222                 vfree(bridge->key_list);
223                 bridge->key_list = NULL;
224         }
225         return rc;
226 }
227
228 /* cannot be __exit b/c as it could be called from __init code */
229 static void agp_backend_cleanup(struct agp_bridge_data *bridge)
230 {
231         if (bridge->driver->cleanup)
232                 bridge->driver->cleanup();
233         if (bridge->driver->free_gatt_table)
234                 bridge->driver->free_gatt_table(bridge);
235
236         vfree(bridge->key_list);
237         bridge->key_list = NULL;
238
239         if (bridge->driver->agp_destroy_page &&
240             bridge->driver->needs_scratch_page) {
241                 void *va = gart_to_virt(bridge->scratch_page_real);
242
243                 if (bridge->driver->agp_unmap_page)
244                         bridge->driver->agp_unmap_page(va,
245                                                bridge->scratch_page_dma);
246
247                 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
248                 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
249         }
250 }
251
252 /* When we remove the global variable agp_bridge from all drivers
253  * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
254  */
255
256 struct agp_bridge_data *agp_alloc_bridge(void)
257 {
258         struct agp_bridge_data *bridge;
259
260         bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
261         if (!bridge)
262                 return NULL;
263
264         atomic_set(&bridge->agp_in_use, 0);
265         atomic_set(&bridge->current_memory_agp, 0);
266
267         if (list_empty(&agp_bridges))
268                 agp_bridge = bridge;
269
270         return bridge;
271 }
272 EXPORT_SYMBOL(agp_alloc_bridge);
273
274
275 void agp_put_bridge(struct agp_bridge_data *bridge)
276 {
277         kfree(bridge);
278
279         if (list_empty(&agp_bridges))
280                 agp_bridge = NULL;
281 }
282 EXPORT_SYMBOL(agp_put_bridge);
283
284
285 int agp_add_bridge(struct agp_bridge_data *bridge)
286 {
287         int error;
288
289         if (agp_off)
290                 return -ENODEV;
291
292         if (!bridge->dev) {
293                 printk (KERN_DEBUG PFX "Erk, registering with no pci_dev!\n");
294                 return -EINVAL;
295         }
296
297         /* Grab reference on the chipset driver. */
298         if (!try_module_get(bridge->driver->owner)) {
299                 dev_info(&bridge->dev->dev, "can't lock chipset driver\n");
300                 return -EINVAL;
301         }
302
303         error = agp_backend_initialize(bridge);
304         if (error) {
305                 dev_info(&bridge->dev->dev,
306                          "agp_backend_initialize() failed\n");
307                 goto err_out;
308         }
309
310         if (list_empty(&agp_bridges)) {
311                 error = agp_frontend_initialize();
312                 if (error) {
313                         dev_info(&bridge->dev->dev,
314                                  "agp_frontend_initialize() failed\n");
315                         goto frontend_err;
316                 }
317
318                 dev_info(&bridge->dev->dev, "AGP aperture is %dM @ 0x%lx\n",
319                          bridge->driver->fetch_size(), bridge->gart_bus_addr);
320
321         }
322
323         list_add(&bridge->list, &agp_bridges);
324         return 0;
325
326 frontend_err:
327         agp_backend_cleanup(bridge);
328 err_out:
329         module_put(bridge->driver->owner);
330         agp_put_bridge(bridge);
331         return error;
332 }
333 EXPORT_SYMBOL_GPL(agp_add_bridge);
334
335
336 void agp_remove_bridge(struct agp_bridge_data *bridge)
337 {
338         agp_backend_cleanup(bridge);
339         list_del(&bridge->list);
340         if (list_empty(&agp_bridges))
341                 agp_frontend_cleanup();
342         module_put(bridge->driver->owner);
343 }
344 EXPORT_SYMBOL_GPL(agp_remove_bridge);
345
346 int agp_off;
347 int agp_try_unsupported_boot;
348 EXPORT_SYMBOL(agp_off);
349 EXPORT_SYMBOL(agp_try_unsupported_boot);
350
351 static int __init agp_init(void)
352 {
353         if (!agp_off)
354                 printk(KERN_INFO "Linux agpgart interface v%d.%d\n",
355                         AGPGART_VERSION_MAJOR, AGPGART_VERSION_MINOR);
356         return 0;
357 }
358
359 static void __exit agp_exit(void)
360 {
361 }
362
363 #ifndef MODULE
364 static __init int agp_setup(char *s)
365 {
366         if (!strcmp(s,"off"))
367                 agp_off = 1;
368         if (!strcmp(s,"try_unsupported"))
369                 agp_try_unsupported_boot = 1;
370         return 1;
371 }
372 __setup("agp=", agp_setup);
373 #endif
374
375 MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
376 MODULE_DESCRIPTION("AGP GART driver");
377 MODULE_LICENSE("GPL and additional rights");
378 MODULE_ALIAS_MISCDEV(AGPGART_MINOR);
379
380 module_init(agp_init);
381 module_exit(agp_exit);
382