Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * AGPGART driver. |
| 3 | * Copyright (C) 2004 Silicon Graphics, Inc. |
| 4 | * Copyright (C) 2002-2005 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, 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 <linux/dma-mapping.h> |
| 39 | #include <linux/mm.h> |
| 40 | #include <asm/io.h> |
| 41 | #include <asm/cacheflush.h> |
| 42 | #include <asm/pgtable.h> |
| 43 | #include "agp.h" |
| 44 | |
| 45 | __u32 *agp_gatt_table; |
| 46 | int agp_memory_reserved; |
| 47 | |
| 48 | /* |
| 49 | * Needed by the Nforce GART driver for the time being. Would be |
| 50 | * nice to do this some other way instead of needing this export. |
| 51 | */ |
| 52 | EXPORT_SYMBOL_GPL(agp_memory_reserved); |
| 53 | |
| 54 | #if defined(CONFIG_X86) |
| 55 | int map_page_into_agp(struct page *page) |
| 56 | { |
| 57 | int i; |
| 58 | i = change_page_attr(page, 1, PAGE_KERNEL_NOCACHE); |
Alan Hourihane | 88d5196 | 2005-11-06 23:35:34 -0800 | [diff] [blame] | 59 | /* Caller's responsibility to call global_flush_tlb() for |
| 60 | * performance reasons */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return i; |
| 62 | } |
| 63 | EXPORT_SYMBOL_GPL(map_page_into_agp); |
| 64 | |
| 65 | int unmap_page_from_agp(struct page *page) |
| 66 | { |
| 67 | int i; |
| 68 | i = change_page_attr(page, 1, PAGE_KERNEL); |
Alan Hourihane | 88d5196 | 2005-11-06 23:35:34 -0800 | [diff] [blame] | 69 | /* Caller's responsibility to call global_flush_tlb() for |
| 70 | * performance reasons */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return i; |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(unmap_page_from_agp); |
| 74 | #endif |
| 75 | |
| 76 | /* |
| 77 | * Generic routines for handling agp_memory structures - |
| 78 | * They use the basic page allocation routines to do the brunt of the work. |
| 79 | */ |
| 80 | |
| 81 | void agp_free_key(int key) |
| 82 | { |
| 83 | if (key < 0) |
| 84 | return; |
| 85 | |
| 86 | if (key < MAXKEY) |
| 87 | clear_bit(key, agp_bridge->key_list); |
| 88 | } |
| 89 | EXPORT_SYMBOL(agp_free_key); |
| 90 | |
| 91 | |
| 92 | static int agp_get_key(void) |
| 93 | { |
| 94 | int bit; |
| 95 | |
| 96 | bit = find_first_zero_bit(agp_bridge->key_list, MAXKEY); |
| 97 | if (bit < MAXKEY) { |
| 98 | set_bit(bit, agp_bridge->key_list); |
| 99 | return bit; |
| 100 | } |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | struct agp_memory *agp_create_memory(int scratch_pages) |
| 106 | { |
| 107 | struct agp_memory *new; |
| 108 | |
Dave Jones | 0ea27d9 | 2005-10-20 15:12:16 -0700 | [diff] [blame] | 109 | new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (new == NULL) |
| 111 | return NULL; |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | new->key = agp_get_key(); |
| 114 | |
| 115 | if (new->key < 0) { |
| 116 | kfree(new); |
| 117 | return NULL; |
| 118 | } |
| 119 | new->memory = vmalloc(PAGE_SIZE * scratch_pages); |
| 120 | |
| 121 | if (new->memory == NULL) { |
| 122 | agp_free_key(new->key); |
| 123 | kfree(new); |
| 124 | return NULL; |
| 125 | } |
| 126 | new->num_scratch_pages = scratch_pages; |
| 127 | return new; |
| 128 | } |
| 129 | EXPORT_SYMBOL(agp_create_memory); |
| 130 | |
| 131 | /** |
| 132 | * agp_free_memory - free memory associated with an agp_memory pointer. |
| 133 | * |
| 134 | * @curr: agp_memory pointer to be freed. |
| 135 | * |
| 136 | * It is the only function that can be called when the backend is not owned |
| 137 | * by the caller. (So it can free memory on client death.) |
| 138 | */ |
| 139 | void agp_free_memory(struct agp_memory *curr) |
| 140 | { |
| 141 | size_t i; |
| 142 | |
| 143 | if (curr == NULL) |
| 144 | return; |
| 145 | |
| 146 | if (curr->is_bound == TRUE) |
| 147 | agp_unbind_memory(curr); |
| 148 | |
| 149 | if (curr->type != 0) { |
| 150 | curr->bridge->driver->free_by_type(curr); |
| 151 | return; |
| 152 | } |
| 153 | if (curr->page_count != 0) { |
| 154 | for (i = 0; i < curr->page_count; i++) { |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 155 | curr->bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
Linus Torvalds | 6730c3c | 2005-11-09 14:56:00 -0800 | [diff] [blame] | 157 | flush_agp_mappings(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | agp_free_key(curr->key); |
| 160 | vfree(curr->memory); |
| 161 | kfree(curr); |
| 162 | } |
| 163 | EXPORT_SYMBOL(agp_free_memory); |
| 164 | |
| 165 | #define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long)) |
| 166 | |
| 167 | /** |
| 168 | * agp_allocate_memory - allocate a group of pages of a certain type. |
| 169 | * |
| 170 | * @page_count: size_t argument of the number of pages |
| 171 | * @type: u32 argument of the type of memory to be allocated. |
| 172 | * |
| 173 | * Every agp bridge device will allow you to allocate AGP_NORMAL_MEMORY which |
| 174 | * maps to physical ram. Any other type is device dependent. |
| 175 | * |
| 176 | * It returns NULL whenever memory is unavailable. |
| 177 | */ |
| 178 | struct agp_memory *agp_allocate_memory(struct agp_bridge_data *bridge, |
| 179 | size_t page_count, u32 type) |
| 180 | { |
| 181 | int scratch_pages; |
| 182 | struct agp_memory *new; |
| 183 | size_t i; |
| 184 | |
| 185 | if (!bridge) |
| 186 | return NULL; |
| 187 | |
| 188 | if ((atomic_read(&bridge->current_memory_agp) + page_count) > bridge->max_memory_agp) |
| 189 | return NULL; |
| 190 | |
| 191 | if (type != 0) { |
| 192 | new = bridge->driver->alloc_by_type(page_count, type); |
| 193 | if (new) |
| 194 | new->bridge = bridge; |
| 195 | return new; |
| 196 | } |
| 197 | |
| 198 | scratch_pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE; |
| 199 | |
| 200 | new = agp_create_memory(scratch_pages); |
| 201 | |
| 202 | if (new == NULL) |
| 203 | return NULL; |
| 204 | |
| 205 | for (i = 0; i < page_count; i++) { |
| 206 | void *addr = bridge->driver->agp_alloc_page(bridge); |
| 207 | |
| 208 | if (addr == NULL) { |
| 209 | agp_free_memory(new); |
| 210 | return NULL; |
| 211 | } |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 212 | new->memory[i] = virt_to_gart(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | new->page_count++; |
| 214 | } |
Alan Hourihane | 88d5196 | 2005-11-06 23:35:34 -0800 | [diff] [blame] | 215 | new->bridge = bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | flush_agp_mappings(); |
| 218 | |
| 219 | return new; |
| 220 | } |
| 221 | EXPORT_SYMBOL(agp_allocate_memory); |
| 222 | |
| 223 | |
| 224 | /* End - Generic routines for handling agp_memory structures */ |
| 225 | |
| 226 | |
| 227 | static int agp_return_size(void) |
| 228 | { |
| 229 | int current_size; |
| 230 | void *temp; |
| 231 | |
| 232 | temp = agp_bridge->current_size; |
| 233 | |
| 234 | switch (agp_bridge->driver->size_type) { |
| 235 | case U8_APER_SIZE: |
| 236 | current_size = A_SIZE_8(temp)->size; |
| 237 | break; |
| 238 | case U16_APER_SIZE: |
| 239 | current_size = A_SIZE_16(temp)->size; |
| 240 | break; |
| 241 | case U32_APER_SIZE: |
| 242 | current_size = A_SIZE_32(temp)->size; |
| 243 | break; |
| 244 | case LVL2_APER_SIZE: |
| 245 | current_size = A_SIZE_LVL2(temp)->size; |
| 246 | break; |
| 247 | case FIXED_APER_SIZE: |
| 248 | current_size = A_SIZE_FIX(temp)->size; |
| 249 | break; |
| 250 | default: |
| 251 | current_size = 0; |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | current_size -= (agp_memory_reserved / (1024*1024)); |
| 256 | if (current_size <0) |
| 257 | current_size = 0; |
| 258 | return current_size; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | int agp_num_entries(void) |
| 263 | { |
| 264 | int num_entries; |
| 265 | void *temp; |
| 266 | |
| 267 | temp = agp_bridge->current_size; |
| 268 | |
| 269 | switch (agp_bridge->driver->size_type) { |
| 270 | case U8_APER_SIZE: |
| 271 | num_entries = A_SIZE_8(temp)->num_entries; |
| 272 | break; |
| 273 | case U16_APER_SIZE: |
| 274 | num_entries = A_SIZE_16(temp)->num_entries; |
| 275 | break; |
| 276 | case U32_APER_SIZE: |
| 277 | num_entries = A_SIZE_32(temp)->num_entries; |
| 278 | break; |
| 279 | case LVL2_APER_SIZE: |
| 280 | num_entries = A_SIZE_LVL2(temp)->num_entries; |
| 281 | break; |
| 282 | case FIXED_APER_SIZE: |
| 283 | num_entries = A_SIZE_FIX(temp)->num_entries; |
| 284 | break; |
| 285 | default: |
| 286 | num_entries = 0; |
| 287 | break; |
| 288 | } |
| 289 | |
| 290 | num_entries -= agp_memory_reserved>>PAGE_SHIFT; |
| 291 | if (num_entries<0) |
| 292 | num_entries = 0; |
| 293 | return num_entries; |
| 294 | } |
| 295 | EXPORT_SYMBOL_GPL(agp_num_entries); |
| 296 | |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | /** |
| 299 | * agp_copy_info - copy bridge state information |
| 300 | * |
Dave Jones | 6a92a4e | 2006-02-28 00:54:25 -0500 | [diff] [blame] | 301 | * @info: agp_kern_info pointer. The caller should insure that this pointer is valid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * |
| 303 | * This function copies information about the agp bridge device and the state of |
| 304 | * the agp backend into an agp_kern_info pointer. |
| 305 | */ |
| 306 | int agp_copy_info(struct agp_bridge_data *bridge, struct agp_kern_info *info) |
| 307 | { |
| 308 | memset(info, 0, sizeof(struct agp_kern_info)); |
| 309 | if (!bridge) { |
| 310 | info->chipset = NOT_SUPPORTED; |
| 311 | return -EIO; |
| 312 | } |
| 313 | |
| 314 | info->version.major = bridge->version->major; |
| 315 | info->version.minor = bridge->version->minor; |
| 316 | info->chipset = SUPPORTED; |
| 317 | info->device = bridge->dev; |
David Mosberger | 66bb8bf | 2005-04-04 13:29:43 -0700 | [diff] [blame] | 318 | if (bridge->mode & AGPSTAT_MODE_3_0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | info->mode = bridge->mode & ~AGP3_RESERVED_MASK; |
| 320 | else |
| 321 | info->mode = bridge->mode & ~AGP2_RESERVED_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | info->aper_base = bridge->gart_bus_addr; |
| 323 | info->aper_size = agp_return_size(); |
| 324 | info->max_memory = bridge->max_memory_agp; |
| 325 | info->current_memory = atomic_read(&bridge->current_memory_agp); |
| 326 | info->cant_use_aperture = bridge->driver->cant_use_aperture; |
| 327 | info->vm_ops = bridge->vm_ops; |
| 328 | info->page_mask = ~0UL; |
| 329 | return 0; |
| 330 | } |
| 331 | EXPORT_SYMBOL(agp_copy_info); |
| 332 | |
| 333 | /* End - Routine to copy over information structure */ |
| 334 | |
| 335 | /* |
| 336 | * Routines for handling swapping of agp_memory into the GATT - |
| 337 | * These routines take agp_memory and insert them into the GATT. |
| 338 | * They call device specific routines to actually write to the GATT. |
| 339 | */ |
| 340 | |
| 341 | /** |
| 342 | * agp_bind_memory - Bind an agp_memory structure into the GATT. |
| 343 | * |
| 344 | * @curr: agp_memory pointer |
| 345 | * @pg_start: an offset into the graphics aperture translation table |
| 346 | * |
| 347 | * It returns -EINVAL if the pointer == NULL. |
| 348 | * It returns -EBUSY if the area of the table requested is already in use. |
| 349 | */ |
| 350 | int agp_bind_memory(struct agp_memory *curr, off_t pg_start) |
| 351 | { |
| 352 | int ret_val; |
| 353 | |
| 354 | if (curr == NULL) |
| 355 | return -EINVAL; |
| 356 | |
| 357 | if (curr->is_bound == TRUE) { |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 358 | printk(KERN_INFO PFX "memory %p is already bound!\n", curr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return -EINVAL; |
| 360 | } |
| 361 | if (curr->is_flushed == FALSE) { |
| 362 | curr->bridge->driver->cache_flush(); |
| 363 | curr->is_flushed = TRUE; |
| 364 | } |
| 365 | ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type); |
| 366 | |
| 367 | if (ret_val != 0) |
| 368 | return ret_val; |
| 369 | |
| 370 | curr->is_bound = TRUE; |
| 371 | curr->pg_start = pg_start; |
| 372 | return 0; |
| 373 | } |
| 374 | EXPORT_SYMBOL(agp_bind_memory); |
| 375 | |
| 376 | |
| 377 | /** |
| 378 | * agp_unbind_memory - Removes an agp_memory structure from the GATT |
| 379 | * |
| 380 | * @curr: agp_memory pointer to be removed from the GATT. |
| 381 | * |
| 382 | * It returns -EINVAL if this piece of agp_memory is not currently bound to |
| 383 | * the graphics aperture translation table or if the agp_memory pointer == NULL |
| 384 | */ |
| 385 | int agp_unbind_memory(struct agp_memory *curr) |
| 386 | { |
| 387 | int ret_val; |
| 388 | |
| 389 | if (curr == NULL) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | if (curr->is_bound != TRUE) { |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 393 | printk(KERN_INFO PFX "memory %p was not bound!\n", curr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | return -EINVAL; |
| 395 | } |
| 396 | |
| 397 | ret_val = curr->bridge->driver->remove_memory(curr, curr->pg_start, curr->type); |
| 398 | |
| 399 | if (ret_val != 0) |
| 400 | return ret_val; |
| 401 | |
| 402 | curr->is_bound = FALSE; |
| 403 | curr->pg_start = 0; |
| 404 | return 0; |
| 405 | } |
| 406 | EXPORT_SYMBOL(agp_unbind_memory); |
| 407 | |
| 408 | /* End - Routines for handling swapping of agp_memory into the GATT */ |
| 409 | |
| 410 | |
| 411 | /* Generic Agp routines - Start */ |
| 412 | static void agp_v2_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat) |
| 413 | { |
| 414 | u32 tmp; |
| 415 | |
| 416 | if (*requested_mode & AGP2_RESERVED_MASK) { |
Dave Jones | c4dd458 | 2005-11-04 15:18:56 -0800 | [diff] [blame] | 417 | printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n", |
| 418 | *requested_mode & AGP2_RESERVED_MASK, *requested_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | *requested_mode &= ~AGP2_RESERVED_MASK; |
| 420 | } |
| 421 | |
Dave Jones | 28af24b | 2006-11-03 15:13:27 -0500 | [diff] [blame^] | 422 | /* |
| 423 | * Some dumb bridges are programmed to disobey the AGP2 spec. |
| 424 | * This is likely a BIOS misprogramming rather than poweron default, or |
| 425 | * it would be a lot more common. |
| 426 | * https://bugs.freedesktop.org/show_bug.cgi?id=8816 |
| 427 | * AGPv2 spec 6.1.9 states: |
| 428 | * The RATE field indicates the data transfer rates supported by this |
| 429 | * device. A.G.P. devices must report all that apply. |
| 430 | * Fix them up as best we can. |
| 431 | */ |
| 432 | switch (*bridge_agpstat & 7) { |
| 433 | case 4: |
| 434 | *bridge_agpstat |= (AGPSTAT2_2X | AGPSTAT2_1X); |
| 435 | printk(KERN_INFO PFX "BIOS bug. AGP bridge claims to only support x4 rate" |
| 436 | "Fixing up support for x2 & x1\n"); |
| 437 | break; |
| 438 | case 2: |
| 439 | *bridge_agpstat |= AGPSTAT2_1X; |
| 440 | printk(KERN_INFO PFX "BIOS bug. AGP bridge claims to only support x2 rate" |
| 441 | "Fixing up support for x1\n"); |
| 442 | break; |
| 443 | default: |
| 444 | break; |
| 445 | } |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | /* Check the speed bits make sense. Only one should be set. */ |
| 448 | tmp = *requested_mode & 7; |
| 449 | switch (tmp) { |
| 450 | case 0: |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 451 | printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to x1 mode.\n", current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | *requested_mode |= AGPSTAT2_1X; |
| 453 | break; |
| 454 | case 1: |
| 455 | case 2: |
| 456 | break; |
| 457 | case 3: |
| 458 | *requested_mode &= ~(AGPSTAT2_1X); /* rate=2 */ |
| 459 | break; |
| 460 | case 4: |
| 461 | break; |
| 462 | case 5: |
| 463 | case 6: |
| 464 | case 7: |
| 465 | *requested_mode &= ~(AGPSTAT2_1X|AGPSTAT2_2X); /* rate=4*/ |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | /* disable SBA if it's not supported */ |
| 470 | if (!((*bridge_agpstat & AGPSTAT_SBA) && (*vga_agpstat & AGPSTAT_SBA) && (*requested_mode & AGPSTAT_SBA))) |
| 471 | *bridge_agpstat &= ~AGPSTAT_SBA; |
| 472 | |
| 473 | /* Set rate */ |
| 474 | if (!((*bridge_agpstat & AGPSTAT2_4X) && (*vga_agpstat & AGPSTAT2_4X) && (*requested_mode & AGPSTAT2_4X))) |
| 475 | *bridge_agpstat &= ~AGPSTAT2_4X; |
| 476 | |
| 477 | if (!((*bridge_agpstat & AGPSTAT2_2X) && (*vga_agpstat & AGPSTAT2_2X) && (*requested_mode & AGPSTAT2_2X))) |
| 478 | *bridge_agpstat &= ~AGPSTAT2_2X; |
| 479 | |
| 480 | if (!((*bridge_agpstat & AGPSTAT2_1X) && (*vga_agpstat & AGPSTAT2_1X) && (*requested_mode & AGPSTAT2_1X))) |
| 481 | *bridge_agpstat &= ~AGPSTAT2_1X; |
| 482 | |
| 483 | /* Now we know what mode it should be, clear out the unwanted bits. */ |
| 484 | if (*bridge_agpstat & AGPSTAT2_4X) |
| 485 | *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_2X); /* 4X */ |
| 486 | |
| 487 | if (*bridge_agpstat & AGPSTAT2_2X) |
| 488 | *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_4X); /* 2X */ |
| 489 | |
| 490 | if (*bridge_agpstat & AGPSTAT2_1X) |
| 491 | *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X); /* 1X */ |
| 492 | |
| 493 | /* Apply any errata. */ |
| 494 | if (agp_bridge->flags & AGP_ERRATA_FASTWRITES) |
| 495 | *bridge_agpstat &= ~AGPSTAT_FW; |
| 496 | |
| 497 | if (agp_bridge->flags & AGP_ERRATA_SBA) |
| 498 | *bridge_agpstat &= ~AGPSTAT_SBA; |
| 499 | |
| 500 | if (agp_bridge->flags & AGP_ERRATA_1X) { |
| 501 | *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X); |
| 502 | *bridge_agpstat |= AGPSTAT2_1X; |
| 503 | } |
| 504 | |
| 505 | /* If we've dropped down to 1X, disable fast writes. */ |
| 506 | if (*bridge_agpstat & AGPSTAT2_1X) |
| 507 | *bridge_agpstat &= ~AGPSTAT_FW; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * requested_mode = Mode requested by (typically) X. |
| 512 | * bridge_agpstat = PCI_AGP_STATUS from agp bridge. |
| 513 | * vga_agpstat = PCI_AGP_STATUS from graphic card. |
| 514 | */ |
| 515 | static void agp_v3_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat) |
| 516 | { |
| 517 | u32 origbridge=*bridge_agpstat, origvga=*vga_agpstat; |
| 518 | u32 tmp; |
| 519 | |
| 520 | if (*requested_mode & AGP3_RESERVED_MASK) { |
Dave Jones | c4dd458 | 2005-11-04 15:18:56 -0800 | [diff] [blame] | 521 | printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n", |
| 522 | *requested_mode & AGP3_RESERVED_MASK, *requested_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | *requested_mode &= ~AGP3_RESERVED_MASK; |
| 524 | } |
| 525 | |
| 526 | /* Check the speed bits make sense. */ |
| 527 | tmp = *requested_mode & 7; |
| 528 | if (tmp == 0) { |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 529 | printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to AGP3 x4 mode.\n", current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | *requested_mode |= AGPSTAT3_4X; |
| 531 | } |
| 532 | if (tmp >= 3) { |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 533 | printk(KERN_INFO PFX "%s tried to set rate=x%d. Setting to AGP3 x8 mode.\n", current->comm, tmp * 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | *requested_mode = (*requested_mode & ~7) | AGPSTAT3_8X; |
| 535 | } |
| 536 | |
| 537 | /* ARQSZ - Set the value to the maximum one. |
| 538 | * Don't allow the mode register to override values. */ |
| 539 | *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_ARQSZ) | |
| 540 | max_t(u32,(*bridge_agpstat & AGPSTAT_ARQSZ),(*vga_agpstat & AGPSTAT_ARQSZ))); |
| 541 | |
| 542 | /* Calibration cycle. |
| 543 | * Don't allow the mode register to override values. */ |
| 544 | *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_CAL_MASK) | |
| 545 | min_t(u32,(*bridge_agpstat & AGPSTAT_CAL_MASK),(*vga_agpstat & AGPSTAT_CAL_MASK))); |
| 546 | |
| 547 | /* SBA *must* be supported for AGP v3 */ |
| 548 | *bridge_agpstat |= AGPSTAT_SBA; |
| 549 | |
| 550 | /* |
| 551 | * Set speed. |
| 552 | * Check for invalid speeds. This can happen when applications |
| 553 | * written before the AGP 3.0 standard pass AGP2.x modes to AGP3 hardware |
| 554 | */ |
| 555 | if (*requested_mode & AGPSTAT_MODE_3_0) { |
| 556 | /* |
| 557 | * Caller hasn't a clue what it is doing. Bridge is in 3.0 mode, |
| 558 | * have been passed a 3.0 mode, but with 2.x speed bits set. |
| 559 | * AGP2.x 4x -> AGP3.0 4x. |
| 560 | */ |
| 561 | if (*requested_mode & AGPSTAT2_4X) { |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 562 | printk(KERN_INFO PFX "%s passes broken AGP3 flags (%x). Fixed.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | current->comm, *requested_mode); |
| 564 | *requested_mode &= ~AGPSTAT2_4X; |
| 565 | *requested_mode |= AGPSTAT3_4X; |
| 566 | } |
| 567 | } else { |
| 568 | /* |
| 569 | * The caller doesn't know what they are doing. We are in 3.0 mode, |
| 570 | * but have been passed an AGP 2.x mode. |
| 571 | * Convert AGP 1x,2x,4x -> AGP 3.0 4x. |
| 572 | */ |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 573 | printk(KERN_INFO PFX "%s passes broken AGP2 flags (%x) in AGP3 mode. Fixed.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | current->comm, *requested_mode); |
| 575 | *requested_mode &= ~(AGPSTAT2_4X | AGPSTAT2_2X | AGPSTAT2_1X); |
| 576 | *requested_mode |= AGPSTAT3_4X; |
| 577 | } |
| 578 | |
| 579 | if (*requested_mode & AGPSTAT3_8X) { |
| 580 | if (!(*bridge_agpstat & AGPSTAT3_8X)) { |
| 581 | *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD); |
| 582 | *bridge_agpstat |= AGPSTAT3_4X; |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 583 | printk(KERN_INFO PFX "%s requested AGPx8 but bridge not capable.\n", current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | if (!(*vga_agpstat & AGPSTAT3_8X)) { |
| 587 | *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD); |
| 588 | *bridge_agpstat |= AGPSTAT3_4X; |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 589 | printk(KERN_INFO PFX "%s requested AGPx8 but graphic card not capable.\n", current->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return; |
| 591 | } |
| 592 | /* All set, bridge & device can do AGP x8*/ |
| 593 | *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD); |
| 594 | goto done; |
| 595 | |
Dave Jones | edf03fb | 2006-09-10 21:12:20 -0400 | [diff] [blame] | 596 | } else if (*requested_mode & AGPSTAT3_4X) { |
| 597 | *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD); |
| 598 | *bridge_agpstat |= AGPSTAT3_4X; |
| 599 | goto done; |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } else { |
| 602 | |
| 603 | /* |
Dave Jones | edf03fb | 2006-09-10 21:12:20 -0400 | [diff] [blame] | 604 | * If we didn't specify an AGP mode, we see if both |
| 605 | * the graphics card, and the bridge can do x8, and use if so. |
| 606 | * If not, we fall back to x4 mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | */ |
Dave Jones | edf03fb | 2006-09-10 21:12:20 -0400 | [diff] [blame] | 608 | if ((*bridge_agpstat & AGPSTAT3_8X) && (*vga_agpstat & AGPSTAT3_8X)) { |
Dave Jones | 2cc1a41 | 2006-09-28 19:50:07 -0400 | [diff] [blame] | 609 | printk(KERN_INFO PFX "No AGP mode specified. Setting to highest mode " |
| 610 | "supported by bridge & card (x8).\n"); |
Dave Jones | edf03fb | 2006-09-10 21:12:20 -0400 | [diff] [blame] | 611 | *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD); |
| 612 | *vga_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD); |
| 613 | } else { |
| 614 | printk(KERN_INFO PFX "Fell back to AGPx4 mode because"); |
| 615 | if (!(*bridge_agpstat & AGPSTAT3_8X)) { |
Dave Jones | 2cc1a41 | 2006-09-28 19:50:07 -0400 | [diff] [blame] | 616 | printk(KERN_INFO PFX "bridge couldn't do x8. bridge_agpstat:%x (orig=%x)\n", |
| 617 | *bridge_agpstat, origbridge); |
Dave Jones | edf03fb | 2006-09-10 21:12:20 -0400 | [diff] [blame] | 618 | *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD); |
| 619 | *bridge_agpstat |= AGPSTAT3_4X; |
| 620 | } |
| 621 | if (!(*vga_agpstat & AGPSTAT3_8X)) { |
Dave Jones | 2cc1a41 | 2006-09-28 19:50:07 -0400 | [diff] [blame] | 622 | printk(KERN_INFO PFX "graphics card couldn't do x8. vga_agpstat:%x (orig=%x)\n", |
| 623 | *vga_agpstat, origvga); |
Dave Jones | edf03fb | 2006-09-10 21:12:20 -0400 | [diff] [blame] | 624 | *vga_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD); |
| 625 | *vga_agpstat |= AGPSTAT3_4X; |
| 626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
| 630 | done: |
| 631 | /* Apply any errata. */ |
| 632 | if (agp_bridge->flags & AGP_ERRATA_FASTWRITES) |
| 633 | *bridge_agpstat &= ~AGPSTAT_FW; |
| 634 | |
| 635 | if (agp_bridge->flags & AGP_ERRATA_SBA) |
| 636 | *bridge_agpstat &= ~AGPSTAT_SBA; |
| 637 | |
| 638 | if (agp_bridge->flags & AGP_ERRATA_1X) { |
| 639 | *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X); |
| 640 | *bridge_agpstat |= AGPSTAT2_1X; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | |
| 645 | /** |
| 646 | * agp_collect_device_status - determine correct agp_cmd from various agp_stat's |
| 647 | * @bridge: an agp_bridge_data struct allocated for the AGP host bridge. |
| 648 | * @requested_mode: requested agp_stat from userspace (Typically from X) |
| 649 | * @bridge_agpstat: current agp_stat from AGP bridge. |
| 650 | * |
| 651 | * This function will hunt for an AGP graphics card, and try to match |
| 652 | * the requested mode to the capabilities of both the bridge and the card. |
| 653 | */ |
| 654 | u32 agp_collect_device_status(struct agp_bridge_data *bridge, u32 requested_mode, u32 bridge_agpstat) |
| 655 | { |
| 656 | struct pci_dev *device = NULL; |
| 657 | u32 vga_agpstat; |
| 658 | u8 cap_ptr; |
| 659 | |
| 660 | for (;;) { |
| 661 | device = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, device); |
| 662 | if (!device) { |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 663 | printk(KERN_INFO PFX "Couldn't find an AGP VGA controller.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | return 0; |
| 665 | } |
| 666 | cap_ptr = pci_find_capability(device, PCI_CAP_ID_AGP); |
| 667 | if (cap_ptr) |
| 668 | break; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Ok, here we have a AGP device. Disable impossible |
| 673 | * settings, and adjust the readqueue to the minimum. |
| 674 | */ |
| 675 | pci_read_config_dword(device, cap_ptr+PCI_AGP_STATUS, &vga_agpstat); |
| 676 | |
| 677 | /* adjust RQ depth */ |
| 678 | bridge_agpstat = ((bridge_agpstat & ~AGPSTAT_RQ_DEPTH) | |
| 679 | min_t(u32, (requested_mode & AGPSTAT_RQ_DEPTH), |
| 680 | min_t(u32, (bridge_agpstat & AGPSTAT_RQ_DEPTH), (vga_agpstat & AGPSTAT_RQ_DEPTH)))); |
| 681 | |
| 682 | /* disable FW if it's not supported */ |
| 683 | if (!((bridge_agpstat & AGPSTAT_FW) && |
| 684 | (vga_agpstat & AGPSTAT_FW) && |
| 685 | (requested_mode & AGPSTAT_FW))) |
| 686 | bridge_agpstat &= ~AGPSTAT_FW; |
| 687 | |
| 688 | /* Check to see if we are operating in 3.0 mode */ |
David Mosberger | 66bb8bf | 2005-04-04 13:29:43 -0700 | [diff] [blame] | 689 | if (agp_bridge->mode & AGPSTAT_MODE_3_0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | agp_v3_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat); |
| 691 | else |
| 692 | agp_v2_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat); |
| 693 | |
| 694 | pci_dev_put(device); |
| 695 | return bridge_agpstat; |
| 696 | } |
| 697 | EXPORT_SYMBOL(agp_collect_device_status); |
| 698 | |
| 699 | |
| 700 | void agp_device_command(u32 bridge_agpstat, int agp_v3) |
| 701 | { |
| 702 | struct pci_dev *device = NULL; |
| 703 | int mode; |
| 704 | |
| 705 | mode = bridge_agpstat & 0x7; |
| 706 | if (agp_v3) |
| 707 | mode *= 4; |
| 708 | |
| 709 | for_each_pci_dev(device) { |
| 710 | u8 agp = pci_find_capability(device, PCI_CAP_ID_AGP); |
| 711 | if (!agp) |
| 712 | continue; |
| 713 | |
| 714 | printk(KERN_INFO PFX "Putting AGP V%d device at %s into %dx mode\n", |
| 715 | agp_v3 ? 3 : 2, pci_name(device), mode); |
| 716 | pci_write_config_dword(device, agp + PCI_AGP_COMMAND, bridge_agpstat); |
| 717 | } |
| 718 | } |
| 719 | EXPORT_SYMBOL(agp_device_command); |
| 720 | |
| 721 | |
| 722 | void get_agp_version(struct agp_bridge_data *bridge) |
| 723 | { |
| 724 | u32 ncapid; |
| 725 | |
| 726 | /* Exit early if already set by errata workarounds. */ |
| 727 | if (bridge->major_version != 0) |
| 728 | return; |
| 729 | |
| 730 | pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid); |
| 731 | bridge->major_version = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf; |
| 732 | bridge->minor_version = (ncapid >> AGP_MINOR_VERSION_SHIFT) & 0xf; |
| 733 | } |
| 734 | EXPORT_SYMBOL(get_agp_version); |
| 735 | |
| 736 | |
| 737 | void agp_generic_enable(struct agp_bridge_data *bridge, u32 requested_mode) |
| 738 | { |
| 739 | u32 bridge_agpstat, temp; |
| 740 | |
| 741 | get_agp_version(agp_bridge); |
| 742 | |
| 743 | printk(KERN_INFO PFX "Found an AGP %d.%d compliant device at %s.\n", |
| 744 | agp_bridge->major_version, |
| 745 | agp_bridge->minor_version, |
| 746 | pci_name(agp_bridge->dev)); |
| 747 | |
| 748 | pci_read_config_dword(agp_bridge->dev, |
| 749 | agp_bridge->capndx + PCI_AGP_STATUS, &bridge_agpstat); |
| 750 | |
| 751 | bridge_agpstat = agp_collect_device_status(agp_bridge, requested_mode, bridge_agpstat); |
| 752 | if (bridge_agpstat == 0) |
| 753 | /* Something bad happened. FIXME: Return error code? */ |
| 754 | return; |
| 755 | |
| 756 | bridge_agpstat |= AGPSTAT_AGP_ENABLE; |
| 757 | |
| 758 | /* Do AGP version specific frobbing. */ |
| 759 | if (bridge->major_version >= 3) { |
David Mosberger | 66bb8bf | 2005-04-04 13:29:43 -0700 | [diff] [blame] | 760 | if (bridge->mode & AGPSTAT_MODE_3_0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | /* If we have 3.5, we can do the isoch stuff. */ |
| 762 | if (bridge->minor_version >= 5) |
| 763 | agp_3_5_enable(bridge); |
| 764 | agp_device_command(bridge_agpstat, TRUE); |
| 765 | return; |
| 766 | } else { |
| 767 | /* Disable calibration cycle in RX91<1> when not in AGP3.0 mode of operation.*/ |
| 768 | bridge_agpstat &= ~(7<<10) ; |
| 769 | pci_read_config_dword(bridge->dev, |
| 770 | bridge->capndx+AGPCTRL, &temp); |
| 771 | temp |= (1<<9); |
| 772 | pci_write_config_dword(bridge->dev, |
| 773 | bridge->capndx+AGPCTRL, temp); |
| 774 | |
Dave Jones | 8c8b838 | 2005-08-17 23:08:11 -0700 | [diff] [blame] | 775 | printk(KERN_INFO PFX "Device is in legacy mode," |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | " falling back to 2.x\n"); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /* AGP v<3 */ |
| 781 | agp_device_command(bridge_agpstat, FALSE); |
| 782 | } |
| 783 | EXPORT_SYMBOL(agp_generic_enable); |
| 784 | |
| 785 | |
| 786 | int agp_generic_create_gatt_table(struct agp_bridge_data *bridge) |
| 787 | { |
| 788 | char *table; |
| 789 | char *table_end; |
| 790 | int size; |
| 791 | int page_order; |
| 792 | int num_entries; |
| 793 | int i; |
| 794 | void *temp; |
| 795 | struct page *page; |
| 796 | |
| 797 | /* The generic routines can't handle 2 level gatt's */ |
| 798 | if (bridge->driver->size_type == LVL2_APER_SIZE) |
| 799 | return -EINVAL; |
| 800 | |
| 801 | table = NULL; |
| 802 | i = bridge->aperture_size_idx; |
| 803 | temp = bridge->current_size; |
| 804 | size = page_order = num_entries = 0; |
| 805 | |
| 806 | if (bridge->driver->size_type != FIXED_APER_SIZE) { |
| 807 | do { |
| 808 | switch (bridge->driver->size_type) { |
| 809 | case U8_APER_SIZE: |
| 810 | size = A_SIZE_8(temp)->size; |
| 811 | page_order = |
| 812 | A_SIZE_8(temp)->page_order; |
| 813 | num_entries = |
| 814 | A_SIZE_8(temp)->num_entries; |
| 815 | break; |
| 816 | case U16_APER_SIZE: |
| 817 | size = A_SIZE_16(temp)->size; |
| 818 | page_order = A_SIZE_16(temp)->page_order; |
| 819 | num_entries = A_SIZE_16(temp)->num_entries; |
| 820 | break; |
| 821 | case U32_APER_SIZE: |
| 822 | size = A_SIZE_32(temp)->size; |
| 823 | page_order = A_SIZE_32(temp)->page_order; |
| 824 | num_entries = A_SIZE_32(temp)->num_entries; |
| 825 | break; |
| 826 | /* This case will never really happen. */ |
| 827 | case FIXED_APER_SIZE: |
| 828 | case LVL2_APER_SIZE: |
| 829 | default: |
| 830 | size = page_order = num_entries = 0; |
| 831 | break; |
| 832 | } |
| 833 | |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 834 | table = alloc_gatt_pages(page_order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | if (table == NULL) { |
| 837 | i++; |
| 838 | switch (bridge->driver->size_type) { |
| 839 | case U8_APER_SIZE: |
| 840 | bridge->current_size = A_IDX8(bridge); |
| 841 | break; |
| 842 | case U16_APER_SIZE: |
| 843 | bridge->current_size = A_IDX16(bridge); |
| 844 | break; |
| 845 | case U32_APER_SIZE: |
| 846 | bridge->current_size = A_IDX32(bridge); |
| 847 | break; |
Dave Jones | 89197e3 | 2006-05-30 18:19:39 -0400 | [diff] [blame] | 848 | /* These cases will never really happen. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | case FIXED_APER_SIZE: |
| 850 | case LVL2_APER_SIZE: |
| 851 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | break; |
| 853 | } |
| 854 | temp = bridge->current_size; |
| 855 | } else { |
| 856 | bridge->aperture_size_idx = i; |
| 857 | } |
| 858 | } while (!table && (i < bridge->driver->num_aperture_sizes)); |
| 859 | } else { |
| 860 | size = ((struct aper_size_info_fixed *) temp)->size; |
| 861 | page_order = ((struct aper_size_info_fixed *) temp)->page_order; |
| 862 | num_entries = ((struct aper_size_info_fixed *) temp)->num_entries; |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 863 | table = alloc_gatt_pages(page_order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | if (table == NULL) |
| 867 | return -ENOMEM; |
| 868 | |
| 869 | table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1); |
| 870 | |
| 871 | for (page = virt_to_page(table); page <= virt_to_page(table_end); page++) |
| 872 | SetPageReserved(page); |
| 873 | |
| 874 | bridge->gatt_table_real = (u32 *) table; |
| 875 | agp_gatt_table = (void *)table; |
| 876 | |
| 877 | bridge->driver->cache_flush(); |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 878 | bridge->gatt_table = ioremap_nocache(virt_to_gart(table), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | (PAGE_SIZE * (1 << page_order))); |
| 880 | bridge->driver->cache_flush(); |
| 881 | |
| 882 | if (bridge->gatt_table == NULL) { |
| 883 | for (page = virt_to_page(table); page <= virt_to_page(table_end); page++) |
| 884 | ClearPageReserved(page); |
| 885 | |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 886 | free_gatt_pages(table, page_order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
| 888 | return -ENOMEM; |
| 889 | } |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 890 | bridge->gatt_bus_addr = virt_to_gart(bridge->gatt_table_real); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | /* AK: bogus, should encode addresses > 4GB */ |
| 893 | for (i = 0; i < num_entries; i++) { |
| 894 | writel(bridge->scratch_page, bridge->gatt_table+i); |
| 895 | readl(bridge->gatt_table+i); /* PCI Posting. */ |
| 896 | } |
| 897 | |
| 898 | return 0; |
| 899 | } |
| 900 | EXPORT_SYMBOL(agp_generic_create_gatt_table); |
| 901 | |
| 902 | int agp_generic_free_gatt_table(struct agp_bridge_data *bridge) |
| 903 | { |
| 904 | int page_order; |
| 905 | char *table, *table_end; |
| 906 | void *temp; |
| 907 | struct page *page; |
| 908 | |
| 909 | temp = bridge->current_size; |
| 910 | |
| 911 | switch (bridge->driver->size_type) { |
| 912 | case U8_APER_SIZE: |
| 913 | page_order = A_SIZE_8(temp)->page_order; |
| 914 | break; |
| 915 | case U16_APER_SIZE: |
| 916 | page_order = A_SIZE_16(temp)->page_order; |
| 917 | break; |
| 918 | case U32_APER_SIZE: |
| 919 | page_order = A_SIZE_32(temp)->page_order; |
| 920 | break; |
| 921 | case FIXED_APER_SIZE: |
| 922 | page_order = A_SIZE_FIX(temp)->page_order; |
| 923 | break; |
| 924 | case LVL2_APER_SIZE: |
| 925 | /* The generic routines can't deal with 2 level gatt's */ |
| 926 | return -EINVAL; |
| 927 | break; |
| 928 | default: |
| 929 | page_order = 0; |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | /* Do not worry about freeing memory, because if this is |
| 934 | * called, then all agp memory is deallocated and removed |
| 935 | * from the table. */ |
| 936 | |
| 937 | iounmap(bridge->gatt_table); |
| 938 | table = (char *) bridge->gatt_table_real; |
| 939 | table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1); |
| 940 | |
| 941 | for (page = virt_to_page(table); page <= virt_to_page(table_end); page++) |
| 942 | ClearPageReserved(page); |
| 943 | |
Keir Fraser | 07eee78 | 2005-03-30 13:17:04 -0800 | [diff] [blame] | 944 | free_gatt_pages(bridge->gatt_table_real, page_order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
| 946 | agp_gatt_table = NULL; |
| 947 | bridge->gatt_table = NULL; |
| 948 | bridge->gatt_table_real = NULL; |
| 949 | bridge->gatt_bus_addr = 0; |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | EXPORT_SYMBOL(agp_generic_free_gatt_table); |
| 954 | |
| 955 | |
| 956 | int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type) |
| 957 | { |
| 958 | int num_entries; |
| 959 | size_t i; |
| 960 | off_t j; |
| 961 | void *temp; |
| 962 | struct agp_bridge_data *bridge; |
| 963 | |
| 964 | bridge = mem->bridge; |
| 965 | if (!bridge) |
| 966 | return -EINVAL; |
| 967 | |
| 968 | temp = bridge->current_size; |
| 969 | |
| 970 | switch (bridge->driver->size_type) { |
| 971 | case U8_APER_SIZE: |
| 972 | num_entries = A_SIZE_8(temp)->num_entries; |
| 973 | break; |
| 974 | case U16_APER_SIZE: |
| 975 | num_entries = A_SIZE_16(temp)->num_entries; |
| 976 | break; |
| 977 | case U32_APER_SIZE: |
| 978 | num_entries = A_SIZE_32(temp)->num_entries; |
| 979 | break; |
| 980 | case FIXED_APER_SIZE: |
| 981 | num_entries = A_SIZE_FIX(temp)->num_entries; |
| 982 | break; |
| 983 | case LVL2_APER_SIZE: |
| 984 | /* The generic routines can't deal with 2 level gatt's */ |
| 985 | return -EINVAL; |
| 986 | break; |
| 987 | default: |
| 988 | num_entries = 0; |
| 989 | break; |
| 990 | } |
| 991 | |
| 992 | num_entries -= agp_memory_reserved/PAGE_SIZE; |
| 993 | if (num_entries < 0) num_entries = 0; |
| 994 | |
| 995 | if (type != 0 || mem->type != 0) { |
| 996 | /* The generic routines know nothing of memory types */ |
| 997 | return -EINVAL; |
| 998 | } |
| 999 | |
| 1000 | /* AK: could wrap */ |
| 1001 | if ((pg_start + mem->page_count) > num_entries) |
| 1002 | return -EINVAL; |
| 1003 | |
| 1004 | j = pg_start; |
| 1005 | |
| 1006 | while (j < (pg_start + mem->page_count)) { |
| 1007 | if (!PGE_EMPTY(bridge, readl(bridge->gatt_table+j))) |
| 1008 | return -EBUSY; |
| 1009 | j++; |
| 1010 | } |
| 1011 | |
| 1012 | if (mem->is_flushed == FALSE) { |
| 1013 | bridge->driver->cache_flush(); |
| 1014 | mem->is_flushed = TRUE; |
| 1015 | } |
| 1016 | |
| 1017 | for (i = 0, j = pg_start; i < mem->page_count; i++, j++) { |
| 1018 | writel(bridge->driver->mask_memory(bridge, mem->memory[i], mem->type), bridge->gatt_table+j); |
| 1019 | readl(bridge->gatt_table+j); /* PCI Posting. */ |
| 1020 | } |
| 1021 | |
| 1022 | bridge->driver->tlb_flush(mem); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | EXPORT_SYMBOL(agp_generic_insert_memory); |
| 1026 | |
| 1027 | |
| 1028 | int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type) |
| 1029 | { |
| 1030 | size_t i; |
| 1031 | struct agp_bridge_data *bridge; |
| 1032 | |
| 1033 | bridge = mem->bridge; |
| 1034 | if (!bridge) |
| 1035 | return -EINVAL; |
| 1036 | |
| 1037 | if (type != 0 || mem->type != 0) { |
| 1038 | /* The generic routines know nothing of memory types */ |
| 1039 | return -EINVAL; |
| 1040 | } |
| 1041 | |
| 1042 | /* AK: bogus, should encode addresses > 4GB */ |
| 1043 | for (i = pg_start; i < (mem->page_count + pg_start); i++) { |
| 1044 | writel(bridge->scratch_page, bridge->gatt_table+i); |
| 1045 | readl(bridge->gatt_table+i); /* PCI Posting. */ |
| 1046 | } |
| 1047 | |
| 1048 | global_cache_flush(); |
| 1049 | bridge->driver->tlb_flush(mem); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | EXPORT_SYMBOL(agp_generic_remove_memory); |
| 1053 | |
| 1054 | |
| 1055 | struct agp_memory *agp_generic_alloc_by_type(size_t page_count, int type) |
| 1056 | { |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | EXPORT_SYMBOL(agp_generic_alloc_by_type); |
| 1060 | |
| 1061 | |
| 1062 | void agp_generic_free_by_type(struct agp_memory *curr) |
| 1063 | { |
| 1064 | vfree(curr->memory); |
| 1065 | agp_free_key(curr->key); |
| 1066 | kfree(curr); |
| 1067 | } |
| 1068 | EXPORT_SYMBOL(agp_generic_free_by_type); |
| 1069 | |
| 1070 | |
| 1071 | /* |
| 1072 | * Basic Page Allocation Routines - |
| 1073 | * These routines handle page allocation and by default they reserve the allocated |
| 1074 | * memory. They also handle incrementing the current_memory_agp value, Which is checked |
| 1075 | * against a maximum value. |
| 1076 | */ |
| 1077 | |
| 1078 | void *agp_generic_alloc_page(struct agp_bridge_data *bridge) |
| 1079 | { |
| 1080 | struct page * page; |
| 1081 | |
| 1082 | page = alloc_page(GFP_KERNEL); |
| 1083 | if (page == NULL) |
| 1084 | return NULL; |
| 1085 | |
| 1086 | map_page_into_agp(page); |
| 1087 | |
| 1088 | get_page(page); |
| 1089 | SetPageLocked(page); |
| 1090 | atomic_inc(&agp_bridge->current_memory_agp); |
| 1091 | return page_address(page); |
| 1092 | } |
| 1093 | EXPORT_SYMBOL(agp_generic_alloc_page); |
| 1094 | |
| 1095 | |
| 1096 | void agp_generic_destroy_page(void *addr) |
| 1097 | { |
| 1098 | struct page *page; |
| 1099 | |
| 1100 | if (addr == NULL) |
| 1101 | return; |
| 1102 | |
| 1103 | page = virt_to_page(addr); |
| 1104 | unmap_page_from_agp(page); |
| 1105 | put_page(page); |
| 1106 | unlock_page(page); |
| 1107 | free_page((unsigned long)addr); |
| 1108 | atomic_dec(&agp_bridge->current_memory_agp); |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(agp_generic_destroy_page); |
| 1111 | |
| 1112 | /* End Basic Page Allocation Routines */ |
| 1113 | |
| 1114 | |
| 1115 | /** |
| 1116 | * agp_enable - initialise the agp point-to-point connection. |
| 1117 | * |
| 1118 | * @mode: agp mode register value to configure with. |
| 1119 | */ |
| 1120 | void agp_enable(struct agp_bridge_data *bridge, u32 mode) |
| 1121 | { |
| 1122 | if (!bridge) |
| 1123 | return; |
| 1124 | bridge->driver->agp_enable(bridge, mode); |
| 1125 | } |
| 1126 | EXPORT_SYMBOL(agp_enable); |
| 1127 | |
| 1128 | /* When we remove the global variable agp_bridge from all drivers |
| 1129 | * then agp_alloc_bridge and agp_generic_find_bridge need to be updated |
| 1130 | */ |
| 1131 | |
| 1132 | struct agp_bridge_data *agp_generic_find_bridge(struct pci_dev *pdev) |
| 1133 | { |
| 1134 | if (list_empty(&agp_bridges)) |
| 1135 | return NULL; |
| 1136 | |
| 1137 | return agp_bridge; |
| 1138 | } |
| 1139 | |
| 1140 | static void ipi_handler(void *null) |
| 1141 | { |
| 1142 | flush_agp_cache(); |
| 1143 | } |
| 1144 | |
| 1145 | void global_cache_flush(void) |
| 1146 | { |
| 1147 | if (on_each_cpu(ipi_handler, NULL, 1, 1) != 0) |
| 1148 | panic(PFX "timed out waiting for the other CPUs!\n"); |
| 1149 | } |
| 1150 | EXPORT_SYMBOL(global_cache_flush); |
| 1151 | |
| 1152 | unsigned long agp_generic_mask_memory(struct agp_bridge_data *bridge, |
| 1153 | unsigned long addr, int type) |
| 1154 | { |
| 1155 | /* memory type is ignored in the generic routine */ |
| 1156 | if (bridge->driver->masks) |
| 1157 | return addr | bridge->driver->masks[0].mask; |
| 1158 | else |
| 1159 | return addr; |
| 1160 | } |
| 1161 | EXPORT_SYMBOL(agp_generic_mask_memory); |
| 1162 | |
| 1163 | /* |
| 1164 | * These functions are implemented according to the AGPv3 spec, |
| 1165 | * which covers implementation details that had previously been |
| 1166 | * left open. |
| 1167 | */ |
| 1168 | |
| 1169 | int agp3_generic_fetch_size(void) |
| 1170 | { |
| 1171 | u16 temp_size; |
| 1172 | int i; |
| 1173 | struct aper_size_info_16 *values; |
| 1174 | |
| 1175 | pci_read_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, &temp_size); |
| 1176 | values = A_SIZE_16(agp_bridge->driver->aperture_sizes); |
| 1177 | |
| 1178 | for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) { |
| 1179 | if (temp_size == values[i].size_value) { |
| 1180 | agp_bridge->previous_size = |
| 1181 | agp_bridge->current_size = (void *) (values + i); |
| 1182 | |
| 1183 | agp_bridge->aperture_size_idx = i; |
| 1184 | return values[i].size; |
| 1185 | } |
| 1186 | } |
| 1187 | return 0; |
| 1188 | } |
| 1189 | EXPORT_SYMBOL(agp3_generic_fetch_size); |
| 1190 | |
| 1191 | void agp3_generic_tlbflush(struct agp_memory *mem) |
| 1192 | { |
| 1193 | u32 ctrl; |
| 1194 | pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl); |
| 1195 | pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_GTLBEN); |
| 1196 | pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl); |
| 1197 | } |
| 1198 | EXPORT_SYMBOL(agp3_generic_tlbflush); |
| 1199 | |
| 1200 | int agp3_generic_configure(void) |
| 1201 | { |
| 1202 | u32 temp; |
| 1203 | struct aper_size_info_16 *current_size; |
| 1204 | |
| 1205 | current_size = A_SIZE_16(agp_bridge->current_size); |
| 1206 | |
| 1207 | pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp); |
| 1208 | agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK); |
| 1209 | |
| 1210 | /* set aperture size */ |
| 1211 | pci_write_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, current_size->size_value); |
| 1212 | /* set gart pointer */ |
| 1213 | pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPGARTLO, agp_bridge->gatt_bus_addr); |
| 1214 | /* enable aperture and GTLB */ |
| 1215 | pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &temp); |
| 1216 | pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, temp | AGPCTRL_APERENB | AGPCTRL_GTLBEN); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | EXPORT_SYMBOL(agp3_generic_configure); |
| 1220 | |
| 1221 | void agp3_generic_cleanup(void) |
| 1222 | { |
| 1223 | u32 ctrl; |
| 1224 | pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl); |
| 1225 | pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_APERENB); |
| 1226 | } |
| 1227 | EXPORT_SYMBOL(agp3_generic_cleanup); |
| 1228 | |
| 1229 | struct aper_size_info_16 agp3_generic_sizes[AGP_GENERIC_SIZES_ENTRIES] = |
| 1230 | { |
| 1231 | {4096, 1048576, 10,0x000}, |
| 1232 | {2048, 524288, 9, 0x800}, |
| 1233 | {1024, 262144, 8, 0xc00}, |
| 1234 | { 512, 131072, 7, 0xe00}, |
| 1235 | { 256, 65536, 6, 0xf00}, |
| 1236 | { 128, 32768, 5, 0xf20}, |
| 1237 | { 64, 16384, 4, 0xf30}, |
| 1238 | { 32, 8192, 3, 0xf38}, |
| 1239 | { 16, 4096, 2, 0xf3c}, |
| 1240 | { 8, 2048, 1, 0xf3e}, |
| 1241 | { 4, 1024, 0, 0xf3f} |
| 1242 | }; |
| 1243 | EXPORT_SYMBOL(agp3_generic_sizes); |
| 1244 | |