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.
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
= { .type
= NOT_SUPPORTED
};
49 int agp_backend_acquire(void)
51 if (agp_bridge
.type
== NOT_SUPPORTED
)
54 if (atomic_read(&agp_bridge
.agp_in_use
) != 0)
57 atomic_inc(&agp_bridge
.agp_in_use
);
61 void agp_backend_release(void)
63 if (agp_bridge
.type
== NOT_SUPPORTED
)
66 atomic_dec(&agp_bridge
.agp_in_use
);
69 struct agp_max_table
{
74 static struct agp_max_table maxes_table
[9] =
87 static int agp_find_max (void)
89 long memory
, index
, result
;
91 memory
= (num_physpages
<< PAGE_SHIFT
) >> 20;
94 while ((memory
> maxes_table
[index
].mem
) && (index
< 8))
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
);
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
) {
122 addr
= agp_bridge
.agp_alloc_page();
125 printk(KERN_ERR PFX
"unable to get memory for scratch page.\n");
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");
140 if (agp_bridge
.create_gatt_table()) {
141 printk(KERN_ERR PFX
"unable to get memory for graphics translation table.\n");
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");
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");
164 printk(KERN_INFO PFX
"AGP aperture is %dM @ 0x%lx\n",
165 size_value
, agp_bridge
.gart_bus_addr
);
170 if (agp_bridge
.needs_scratch_page
== TRUE
) {
171 agp_bridge
.agp_destroy_page(phys_to_virt(agp_bridge
.scratch_page_real
));
174 agp_bridge
.free_gatt_table();
176 vfree(agp_bridge
.key_list
);
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
)
198 return agp_bridge
.suspend();
206 extern int agp_frontend_initialize(void);
207 extern void agp_frontend_cleanup(void);
209 static const drm_agp_t drm_agp
= {
211 &agp_allocate_memory
,
215 &agp_backend_acquire
,
216 &agp_backend_release
,
220 static int agp_count
=0;
222 int agp_register_driver (struct agp_driver
*drv
)
226 if (drv
->dev
== NULL
) {
227 printk (KERN_DEBUG PFX
"Erk, registering with no pci_dev!\n");
232 printk (KERN_DEBUG PFX
"Only one agpgart device currently supported.\n");
236 /* Grab reference on the chipset driver. */
237 if (!try_module_get(drv
->owner
))
240 ret_val
= agp_backend_initialize(drv
->dev
);
244 ret_val
= agp_frontend_initialize();
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
);
256 agp_backend_cleanup();
258 agp_bridge
.type
= NOT_SUPPORTED
;
259 module_put(drv
->owner
);
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");
271 module_put(drv
->owner
);
276 int __init
agp_init(void)
278 static int already_initialised
=0;
280 if (already_initialised
!=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
);
293 void __exit
agp_exit(void)
299 #ifndef CONFIG_GART_IOMMU
300 module_init(agp_init
);
301 module_exit(agp_exit
);
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");