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