blob: a627b771c2ebaab0353932b5c642c26ba1235f5b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070030#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;
46int 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 */
52EXPORT_SYMBOL_GPL(agp_memory_reserved);
53
54#if defined(CONFIG_X86)
55int map_page_into_agp(struct page *page)
56{
57 int i;
58 i = change_page_attr(page, 1, PAGE_KERNEL_NOCACHE);
Alan Hourihane88d51962005-11-06 23:35:34 -080059 /* Caller's responsibility to call global_flush_tlb() for
60 * performance reasons */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return i;
62}
63EXPORT_SYMBOL_GPL(map_page_into_agp);
64
65int unmap_page_from_agp(struct page *page)
66{
67 int i;
68 i = change_page_attr(page, 1, PAGE_KERNEL);
Alan Hourihane88d51962005-11-06 23:35:34 -080069 /* Caller's responsibility to call global_flush_tlb() for
70 * performance reasons */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return i;
72}
73EXPORT_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
81void 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}
89EXPORT_SYMBOL(agp_free_key);
90
91
92static 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
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100104/*
105 * Use kmalloc if possible for the page list. Otherwise fall back to
106 * vmalloc. This speeds things up and also saves memory for small AGP
107 * regions.
108 */
109
110void agp_alloc_page_array(size_t size, struct agp_memory *mem)
111{
112 mem->memory = NULL;
113 mem->vmalloc_flag = 0;
114
115 if (size <= 2*PAGE_SIZE) {
116 mem->memory = kmalloc(size, GFP_KERNEL | __GFP_NORETRY);
117 }
118 if (mem->memory == NULL) {
119 mem->memory = vmalloc(size);
120 mem->vmalloc_flag = 1;
121 }
122}
123EXPORT_SYMBOL(agp_alloc_page_array);
124
125void agp_free_page_array(struct agp_memory *mem)
126{
127 if (mem->vmalloc_flag) {
128 vfree(mem->memory);
129 } else {
130 kfree(mem->memory);
131 }
132}
133EXPORT_SYMBOL(agp_free_page_array);
134
135
136static struct agp_memory *agp_create_user_memory(unsigned long num_agp_pages)
137{
138 struct agp_memory *new;
139 unsigned long alloc_size = num_agp_pages*sizeof(struct page *);
140
141 new = kmalloc(sizeof(struct agp_memory), GFP_KERNEL);
142
143 if (new == NULL)
144 return NULL;
145
146 memset(new, 0, sizeof(struct agp_memory));
147 new->key = agp_get_key();
148
149 if (new->key < 0) {
150 kfree(new);
151 return NULL;
152 }
153
154 agp_alloc_page_array(alloc_size, new);
155
156 if (new->memory == NULL) {
157 agp_free_key(new->key);
158 kfree(new);
159 return NULL;
160 }
161 new->num_scratch_pages = 0;
162 return new;
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166struct agp_memory *agp_create_memory(int scratch_pages)
167{
168 struct agp_memory *new;
169
Dave Jones0ea27d92005-10-20 15:12:16 -0700170 new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (new == NULL)
172 return NULL;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 new->key = agp_get_key();
175
176 if (new->key < 0) {
177 kfree(new);
178 return NULL;
179 }
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100180
181 agp_alloc_page_array(PAGE_SIZE * scratch_pages, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (new->memory == NULL) {
184 agp_free_key(new->key);
185 kfree(new);
186 return NULL;
187 }
188 new->num_scratch_pages = scratch_pages;
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100189 new->type = AGP_NORMAL_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return new;
191}
192EXPORT_SYMBOL(agp_create_memory);
193
194/**
195 * agp_free_memory - free memory associated with an agp_memory pointer.
196 *
197 * @curr: agp_memory pointer to be freed.
198 *
199 * It is the only function that can be called when the backend is not owned
200 * by the caller. (So it can free memory on client death.)
201 */
202void agp_free_memory(struct agp_memory *curr)
203{
204 size_t i;
205
206 if (curr == NULL)
207 return;
208
209 if (curr->is_bound == TRUE)
210 agp_unbind_memory(curr);
211
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100212 if (curr->type >= AGP_USER_TYPES) {
213 agp_generic_free_by_type(curr);
214 return;
215 }
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (curr->type != 0) {
218 curr->bridge->driver->free_by_type(curr);
219 return;
220 }
221 if (curr->page_count != 0) {
222 for (i = 0; i < curr->page_count; i++) {
Keir Fraser07eee782005-03-30 13:17:04 -0800223 curr->bridge->driver->agp_destroy_page(gart_to_virt(curr->memory[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Linus Torvalds6730c3c2005-11-09 14:56:00 -0800225 flush_agp_mappings();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227 agp_free_key(curr->key);
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100228 agp_free_page_array(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 kfree(curr);
230}
231EXPORT_SYMBOL(agp_free_memory);
232
233#define ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(unsigned long))
234
235/**
236 * agp_allocate_memory - allocate a group of pages of a certain type.
237 *
238 * @page_count: size_t argument of the number of pages
239 * @type: u32 argument of the type of memory to be allocated.
240 *
241 * Every agp bridge device will allow you to allocate AGP_NORMAL_MEMORY which
242 * maps to physical ram. Any other type is device dependent.
243 *
244 * It returns NULL whenever memory is unavailable.
245 */
246struct agp_memory *agp_allocate_memory(struct agp_bridge_data *bridge,
247 size_t page_count, u32 type)
248{
249 int scratch_pages;
250 struct agp_memory *new;
251 size_t i;
252
253 if (!bridge)
254 return NULL;
255
256 if ((atomic_read(&bridge->current_memory_agp) + page_count) > bridge->max_memory_agp)
257 return NULL;
258
Thomas Hellstroma030ce42007-01-23 10:33:43 +0100259 if (type >= AGP_USER_TYPES) {
260 new = agp_generic_alloc_user(page_count, type);
261 if (new)
262 new->bridge = bridge;
263 return new;
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (type != 0) {
267 new = bridge->driver->alloc_by_type(page_count, type);
268 if (new)
269 new->bridge = bridge;
270 return new;
271 }
272
273 scratch_pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE;
274
275 new = agp_create_memory(scratch_pages);
276
277 if (new == NULL)
278 return NULL;
279
280 for (i = 0; i < page_count; i++) {
281 void *addr = bridge->driver->agp_alloc_page(bridge);
282
283 if (addr == NULL) {
284 agp_free_memory(new);
285 return NULL;
286 }
Keir Fraser07eee782005-03-30 13:17:04 -0800287 new->memory[i] = virt_to_gart(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 new->page_count++;
289 }
Alan Hourihane88d51962005-11-06 23:35:34 -0800290 new->bridge = bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 flush_agp_mappings();
293
294 return new;
295}
296EXPORT_SYMBOL(agp_allocate_memory);
297
298
299/* End - Generic routines for handling agp_memory structures */
300
301
302static int agp_return_size(void)
303{
304 int current_size;
305 void *temp;
306
307 temp = agp_bridge->current_size;
308
309 switch (agp_bridge->driver->size_type) {
310 case U8_APER_SIZE:
311 current_size = A_SIZE_8(temp)->size;
312 break;
313 case U16_APER_SIZE:
314 current_size = A_SIZE_16(temp)->size;
315 break;
316 case U32_APER_SIZE:
317 current_size = A_SIZE_32(temp)->size;
318 break;
319 case LVL2_APER_SIZE:
320 current_size = A_SIZE_LVL2(temp)->size;
321 break;
322 case FIXED_APER_SIZE:
323 current_size = A_SIZE_FIX(temp)->size;
324 break;
325 default:
326 current_size = 0;
327 break;
328 }
329
330 current_size -= (agp_memory_reserved / (1024*1024));
331 if (current_size <0)
332 current_size = 0;
333 return current_size;
334}
335
336
337int agp_num_entries(void)
338{
339 int num_entries;
340 void *temp;
341
342 temp = agp_bridge->current_size;
343
344 switch (agp_bridge->driver->size_type) {
345 case U8_APER_SIZE:
346 num_entries = A_SIZE_8(temp)->num_entries;
347 break;
348 case U16_APER_SIZE:
349 num_entries = A_SIZE_16(temp)->num_entries;
350 break;
351 case U32_APER_SIZE:
352 num_entries = A_SIZE_32(temp)->num_entries;
353 break;
354 case LVL2_APER_SIZE:
355 num_entries = A_SIZE_LVL2(temp)->num_entries;
356 break;
357 case FIXED_APER_SIZE:
358 num_entries = A_SIZE_FIX(temp)->num_entries;
359 break;
360 default:
361 num_entries = 0;
362 break;
363 }
364
365 num_entries -= agp_memory_reserved>>PAGE_SHIFT;
366 if (num_entries<0)
367 num_entries = 0;
368 return num_entries;
369}
370EXPORT_SYMBOL_GPL(agp_num_entries);
371
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373/**
374 * agp_copy_info - copy bridge state information
375 *
Dave Jones6a92a4e2006-02-28 00:54:25 -0500376 * @info: agp_kern_info pointer. The caller should insure that this pointer is valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
378 * This function copies information about the agp bridge device and the state of
379 * the agp backend into an agp_kern_info pointer.
380 */
381int agp_copy_info(struct agp_bridge_data *bridge, struct agp_kern_info *info)
382{
383 memset(info, 0, sizeof(struct agp_kern_info));
384 if (!bridge) {
385 info->chipset = NOT_SUPPORTED;
386 return -EIO;
387 }
388
389 info->version.major = bridge->version->major;
390 info->version.minor = bridge->version->minor;
391 info->chipset = SUPPORTED;
392 info->device = bridge->dev;
David Mosberger66bb8bf2005-04-04 13:29:43 -0700393 if (bridge->mode & AGPSTAT_MODE_3_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 info->mode = bridge->mode & ~AGP3_RESERVED_MASK;
395 else
396 info->mode = bridge->mode & ~AGP2_RESERVED_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 info->aper_base = bridge->gart_bus_addr;
398 info->aper_size = agp_return_size();
399 info->max_memory = bridge->max_memory_agp;
400 info->current_memory = atomic_read(&bridge->current_memory_agp);
401 info->cant_use_aperture = bridge->driver->cant_use_aperture;
402 info->vm_ops = bridge->vm_ops;
403 info->page_mask = ~0UL;
404 return 0;
405}
406EXPORT_SYMBOL(agp_copy_info);
407
408/* End - Routine to copy over information structure */
409
410/*
411 * Routines for handling swapping of agp_memory into the GATT -
412 * These routines take agp_memory and insert them into the GATT.
413 * They call device specific routines to actually write to the GATT.
414 */
415
416/**
417 * agp_bind_memory - Bind an agp_memory structure into the GATT.
418 *
419 * @curr: agp_memory pointer
420 * @pg_start: an offset into the graphics aperture translation table
421 *
422 * It returns -EINVAL if the pointer == NULL.
423 * It returns -EBUSY if the area of the table requested is already in use.
424 */
425int agp_bind_memory(struct agp_memory *curr, off_t pg_start)
426{
427 int ret_val;
428
429 if (curr == NULL)
430 return -EINVAL;
431
432 if (curr->is_bound == TRUE) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700433 printk(KERN_INFO PFX "memory %p is already bound!\n", curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return -EINVAL;
435 }
436 if (curr->is_flushed == FALSE) {
437 curr->bridge->driver->cache_flush();
438 curr->is_flushed = TRUE;
439 }
440 ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type);
441
442 if (ret_val != 0)
443 return ret_val;
444
445 curr->is_bound = TRUE;
446 curr->pg_start = pg_start;
447 return 0;
448}
449EXPORT_SYMBOL(agp_bind_memory);
450
451
452/**
453 * agp_unbind_memory - Removes an agp_memory structure from the GATT
454 *
455 * @curr: agp_memory pointer to be removed from the GATT.
456 *
457 * It returns -EINVAL if this piece of agp_memory is not currently bound to
458 * the graphics aperture translation table or if the agp_memory pointer == NULL
459 */
460int agp_unbind_memory(struct agp_memory *curr)
461{
462 int ret_val;
463
464 if (curr == NULL)
465 return -EINVAL;
466
467 if (curr->is_bound != TRUE) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700468 printk(KERN_INFO PFX "memory %p was not bound!\n", curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EINVAL;
470 }
471
472 ret_val = curr->bridge->driver->remove_memory(curr, curr->pg_start, curr->type);
473
474 if (ret_val != 0)
475 return ret_val;
476
477 curr->is_bound = FALSE;
478 curr->pg_start = 0;
479 return 0;
480}
481EXPORT_SYMBOL(agp_unbind_memory);
482
483/* End - Routines for handling swapping of agp_memory into the GATT */
484
485
486/* Generic Agp routines - Start */
487static void agp_v2_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
488{
489 u32 tmp;
490
491 if (*requested_mode & AGP2_RESERVED_MASK) {
Dave Jonesc4dd4582005-11-04 15:18:56 -0800492 printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
493 *requested_mode & AGP2_RESERVED_MASK, *requested_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *requested_mode &= ~AGP2_RESERVED_MASK;
495 }
496
Dave Jones28af24b2006-11-03 15:13:27 -0500497 /*
498 * Some dumb bridges are programmed to disobey the AGP2 spec.
499 * This is likely a BIOS misprogramming rather than poweron default, or
500 * it would be a lot more common.
501 * https://bugs.freedesktop.org/show_bug.cgi?id=8816
502 * AGPv2 spec 6.1.9 states:
503 * The RATE field indicates the data transfer rates supported by this
504 * device. A.G.P. devices must report all that apply.
505 * Fix them up as best we can.
506 */
507 switch (*bridge_agpstat & 7) {
508 case 4:
509 *bridge_agpstat |= (AGPSTAT2_2X | AGPSTAT2_1X);
510 printk(KERN_INFO PFX "BIOS bug. AGP bridge claims to only support x4 rate"
511 "Fixing up support for x2 & x1\n");
512 break;
513 case 2:
514 *bridge_agpstat |= AGPSTAT2_1X;
515 printk(KERN_INFO PFX "BIOS bug. AGP bridge claims to only support x2 rate"
516 "Fixing up support for x1\n");
517 break;
518 default:
519 break;
520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Check the speed bits make sense. Only one should be set. */
523 tmp = *requested_mode & 7;
524 switch (tmp) {
525 case 0:
Dave Jones8c8b8382005-08-17 23:08:11 -0700526 printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to x1 mode.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *requested_mode |= AGPSTAT2_1X;
528 break;
529 case 1:
530 case 2:
531 break;
532 case 3:
533 *requested_mode &= ~(AGPSTAT2_1X); /* rate=2 */
534 break;
535 case 4:
536 break;
537 case 5:
538 case 6:
539 case 7:
540 *requested_mode &= ~(AGPSTAT2_1X|AGPSTAT2_2X); /* rate=4*/
541 break;
542 }
543
544 /* disable SBA if it's not supported */
545 if (!((*bridge_agpstat & AGPSTAT_SBA) && (*vga_agpstat & AGPSTAT_SBA) && (*requested_mode & AGPSTAT_SBA)))
546 *bridge_agpstat &= ~AGPSTAT_SBA;
547
548 /* Set rate */
549 if (!((*bridge_agpstat & AGPSTAT2_4X) && (*vga_agpstat & AGPSTAT2_4X) && (*requested_mode & AGPSTAT2_4X)))
550 *bridge_agpstat &= ~AGPSTAT2_4X;
551
552 if (!((*bridge_agpstat & AGPSTAT2_2X) && (*vga_agpstat & AGPSTAT2_2X) && (*requested_mode & AGPSTAT2_2X)))
553 *bridge_agpstat &= ~AGPSTAT2_2X;
554
555 if (!((*bridge_agpstat & AGPSTAT2_1X) && (*vga_agpstat & AGPSTAT2_1X) && (*requested_mode & AGPSTAT2_1X)))
556 *bridge_agpstat &= ~AGPSTAT2_1X;
557
558 /* Now we know what mode it should be, clear out the unwanted bits. */
559 if (*bridge_agpstat & AGPSTAT2_4X)
560 *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_2X); /* 4X */
561
562 if (*bridge_agpstat & AGPSTAT2_2X)
563 *bridge_agpstat &= ~(AGPSTAT2_1X | AGPSTAT2_4X); /* 2X */
564
565 if (*bridge_agpstat & AGPSTAT2_1X)
566 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X); /* 1X */
567
568 /* Apply any errata. */
569 if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
570 *bridge_agpstat &= ~AGPSTAT_FW;
571
572 if (agp_bridge->flags & AGP_ERRATA_SBA)
573 *bridge_agpstat &= ~AGPSTAT_SBA;
574
575 if (agp_bridge->flags & AGP_ERRATA_1X) {
576 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
577 *bridge_agpstat |= AGPSTAT2_1X;
578 }
579
580 /* If we've dropped down to 1X, disable fast writes. */
581 if (*bridge_agpstat & AGPSTAT2_1X)
582 *bridge_agpstat &= ~AGPSTAT_FW;
583}
584
585/*
586 * requested_mode = Mode requested by (typically) X.
587 * bridge_agpstat = PCI_AGP_STATUS from agp bridge.
588 * vga_agpstat = PCI_AGP_STATUS from graphic card.
589 */
590static void agp_v3_parse_one(u32 *requested_mode, u32 *bridge_agpstat, u32 *vga_agpstat)
591{
592 u32 origbridge=*bridge_agpstat, origvga=*vga_agpstat;
593 u32 tmp;
594
595 if (*requested_mode & AGP3_RESERVED_MASK) {
Dave Jonesc4dd4582005-11-04 15:18:56 -0800596 printk(KERN_INFO PFX "reserved bits set (%x) in mode 0x%x. Fixed.\n",
597 *requested_mode & AGP3_RESERVED_MASK, *requested_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *requested_mode &= ~AGP3_RESERVED_MASK;
599 }
600
601 /* Check the speed bits make sense. */
602 tmp = *requested_mode & 7;
603 if (tmp == 0) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700604 printk(KERN_INFO PFX "%s tried to set rate=x0. Setting to AGP3 x4 mode.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *requested_mode |= AGPSTAT3_4X;
606 }
607 if (tmp >= 3) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700608 printk(KERN_INFO PFX "%s tried to set rate=x%d. Setting to AGP3 x8 mode.\n", current->comm, tmp * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 *requested_mode = (*requested_mode & ~7) | AGPSTAT3_8X;
610 }
611
612 /* ARQSZ - Set the value to the maximum one.
613 * Don't allow the mode register to override values. */
614 *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_ARQSZ) |
615 max_t(u32,(*bridge_agpstat & AGPSTAT_ARQSZ),(*vga_agpstat & AGPSTAT_ARQSZ)));
616
617 /* Calibration cycle.
618 * Don't allow the mode register to override values. */
619 *bridge_agpstat = ((*bridge_agpstat & ~AGPSTAT_CAL_MASK) |
620 min_t(u32,(*bridge_agpstat & AGPSTAT_CAL_MASK),(*vga_agpstat & AGPSTAT_CAL_MASK)));
621
622 /* SBA *must* be supported for AGP v3 */
623 *bridge_agpstat |= AGPSTAT_SBA;
624
625 /*
626 * Set speed.
627 * Check for invalid speeds. This can happen when applications
628 * written before the AGP 3.0 standard pass AGP2.x modes to AGP3 hardware
629 */
630 if (*requested_mode & AGPSTAT_MODE_3_0) {
631 /*
632 * Caller hasn't a clue what it is doing. Bridge is in 3.0 mode,
633 * have been passed a 3.0 mode, but with 2.x speed bits set.
634 * AGP2.x 4x -> AGP3.0 4x.
635 */
636 if (*requested_mode & AGPSTAT2_4X) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700637 printk(KERN_INFO PFX "%s passes broken AGP3 flags (%x). Fixed.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 current->comm, *requested_mode);
639 *requested_mode &= ~AGPSTAT2_4X;
640 *requested_mode |= AGPSTAT3_4X;
641 }
642 } else {
643 /*
644 * The caller doesn't know what they are doing. We are in 3.0 mode,
645 * but have been passed an AGP 2.x mode.
646 * Convert AGP 1x,2x,4x -> AGP 3.0 4x.
647 */
Dave Jones8c8b8382005-08-17 23:08:11 -0700648 printk(KERN_INFO PFX "%s passes broken AGP2 flags (%x) in AGP3 mode. Fixed.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 current->comm, *requested_mode);
650 *requested_mode &= ~(AGPSTAT2_4X | AGPSTAT2_2X | AGPSTAT2_1X);
651 *requested_mode |= AGPSTAT3_4X;
652 }
653
654 if (*requested_mode & AGPSTAT3_8X) {
655 if (!(*bridge_agpstat & AGPSTAT3_8X)) {
656 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
657 *bridge_agpstat |= AGPSTAT3_4X;
Dave Jones8c8b8382005-08-17 23:08:11 -0700658 printk(KERN_INFO PFX "%s requested AGPx8 but bridge not capable.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return;
660 }
661 if (!(*vga_agpstat & AGPSTAT3_8X)) {
662 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
663 *bridge_agpstat |= AGPSTAT3_4X;
Dave Jones8c8b8382005-08-17 23:08:11 -0700664 printk(KERN_INFO PFX "%s requested AGPx8 but graphic card not capable.\n", current->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return;
666 }
667 /* All set, bridge & device can do AGP x8*/
668 *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
669 goto done;
670
Dave Jonesedf03fb2006-09-10 21:12:20 -0400671 } else if (*requested_mode & AGPSTAT3_4X) {
672 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
673 *bridge_agpstat |= AGPSTAT3_4X;
674 goto done;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 } else {
677
678 /*
Dave Jonesedf03fb2006-09-10 21:12:20 -0400679 * If we didn't specify an AGP mode, we see if both
680 * the graphics card, and the bridge can do x8, and use if so.
681 * If not, we fall back to x4 mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Dave Jonesedf03fb2006-09-10 21:12:20 -0400683 if ((*bridge_agpstat & AGPSTAT3_8X) && (*vga_agpstat & AGPSTAT3_8X)) {
Dave Jones2cc1a412006-09-28 19:50:07 -0400684 printk(KERN_INFO PFX "No AGP mode specified. Setting to highest mode "
685 "supported by bridge & card (x8).\n");
Dave Jonesedf03fb2006-09-10 21:12:20 -0400686 *bridge_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
687 *vga_agpstat &= ~(AGPSTAT3_4X | AGPSTAT3_RSVD);
688 } else {
689 printk(KERN_INFO PFX "Fell back to AGPx4 mode because");
690 if (!(*bridge_agpstat & AGPSTAT3_8X)) {
Dave Jones2cc1a412006-09-28 19:50:07 -0400691 printk(KERN_INFO PFX "bridge couldn't do x8. bridge_agpstat:%x (orig=%x)\n",
692 *bridge_agpstat, origbridge);
Dave Jonesedf03fb2006-09-10 21:12:20 -0400693 *bridge_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
694 *bridge_agpstat |= AGPSTAT3_4X;
695 }
696 if (!(*vga_agpstat & AGPSTAT3_8X)) {
Dave Jones2cc1a412006-09-28 19:50:07 -0400697 printk(KERN_INFO PFX "graphics card couldn't do x8. vga_agpstat:%x (orig=%x)\n",
698 *vga_agpstat, origvga);
Dave Jonesedf03fb2006-09-10 21:12:20 -0400699 *vga_agpstat &= ~(AGPSTAT3_8X | AGPSTAT3_RSVD);
700 *vga_agpstat |= AGPSTAT3_4X;
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 }
704
705done:
706 /* Apply any errata. */
707 if (agp_bridge->flags & AGP_ERRATA_FASTWRITES)
708 *bridge_agpstat &= ~AGPSTAT_FW;
709
710 if (agp_bridge->flags & AGP_ERRATA_SBA)
711 *bridge_agpstat &= ~AGPSTAT_SBA;
712
713 if (agp_bridge->flags & AGP_ERRATA_1X) {
714 *bridge_agpstat &= ~(AGPSTAT2_2X | AGPSTAT2_4X);
715 *bridge_agpstat |= AGPSTAT2_1X;
716 }
717}
718
719
720/**
721 * agp_collect_device_status - determine correct agp_cmd from various agp_stat's
722 * @bridge: an agp_bridge_data struct allocated for the AGP host bridge.
723 * @requested_mode: requested agp_stat from userspace (Typically from X)
724 * @bridge_agpstat: current agp_stat from AGP bridge.
725 *
726 * This function will hunt for an AGP graphics card, and try to match
727 * the requested mode to the capabilities of both the bridge and the card.
728 */
729u32 agp_collect_device_status(struct agp_bridge_data *bridge, u32 requested_mode, u32 bridge_agpstat)
730{
731 struct pci_dev *device = NULL;
732 u32 vga_agpstat;
733 u8 cap_ptr;
734
735 for (;;) {
736 device = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, device);
737 if (!device) {
Dave Jones8c8b8382005-08-17 23:08:11 -0700738 printk(KERN_INFO PFX "Couldn't find an AGP VGA controller.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return 0;
740 }
741 cap_ptr = pci_find_capability(device, PCI_CAP_ID_AGP);
742 if (cap_ptr)
743 break;
744 }
745
746 /*
747 * Ok, here we have a AGP device. Disable impossible
748 * settings, and adjust the readqueue to the minimum.
749 */
750 pci_read_config_dword(device, cap_ptr+PCI_AGP_STATUS, &vga_agpstat);
751
752 /* adjust RQ depth */
753 bridge_agpstat = ((bridge_agpstat & ~AGPSTAT_RQ_DEPTH) |
754 min_t(u32, (requested_mode & AGPSTAT_RQ_DEPTH),
755 min_t(u32, (bridge_agpstat & AGPSTAT_RQ_DEPTH), (vga_agpstat & AGPSTAT_RQ_DEPTH))));
756
757 /* disable FW if it's not supported */
758 if (!((bridge_agpstat & AGPSTAT_FW) &&
759 (vga_agpstat & AGPSTAT_FW) &&
760 (requested_mode & AGPSTAT_FW)))
761 bridge_agpstat &= ~AGPSTAT_FW;
762
763 /* Check to see if we are operating in 3.0 mode */
David Mosberger66bb8bf2005-04-04 13:29:43 -0700764 if (agp_bridge->mode & AGPSTAT_MODE_3_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 agp_v3_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
766 else
767 agp_v2_parse_one(&requested_mode, &bridge_agpstat, &vga_agpstat);
768
769 pci_dev_put(device);
770 return bridge_agpstat;
771}
772EXPORT_SYMBOL(agp_collect_device_status);
773
774
775void agp_device_command(u32 bridge_agpstat, int agp_v3)
776{
777 struct pci_dev *device = NULL;
778 int mode;
779
780 mode = bridge_agpstat & 0x7;
781 if (agp_v3)
782 mode *= 4;
783
784 for_each_pci_dev(device) {
785 u8 agp = pci_find_capability(device, PCI_CAP_ID_AGP);
786 if (!agp)
787 continue;
788
789 printk(KERN_INFO PFX "Putting AGP V%d device at %s into %dx mode\n",
790 agp_v3 ? 3 : 2, pci_name(device), mode);
791 pci_write_config_dword(device, agp + PCI_AGP_COMMAND, bridge_agpstat);
792 }
793}
794EXPORT_SYMBOL(agp_device_command);
795
796
797void get_agp_version(struct agp_bridge_data *bridge)
798{
799 u32 ncapid;
800
801 /* Exit early if already set by errata workarounds. */
802 if (bridge->major_version != 0)
803 return;
804
805 pci_read_config_dword(bridge->dev, bridge->capndx, &ncapid);
806 bridge->major_version = (ncapid >> AGP_MAJOR_VERSION_SHIFT) & 0xf;
807 bridge->minor_version = (ncapid >> AGP_MINOR_VERSION_SHIFT) & 0xf;
808}
809EXPORT_SYMBOL(get_agp_version);
810
811
812void agp_generic_enable(struct agp_bridge_data *bridge, u32 requested_mode)
813{
814 u32 bridge_agpstat, temp;
815
816 get_agp_version(agp_bridge);
817
818 printk(KERN_INFO PFX "Found an AGP %d.%d compliant device at %s.\n",
819 agp_bridge->major_version,
820 agp_bridge->minor_version,
821 pci_name(agp_bridge->dev));
822
823 pci_read_config_dword(agp_bridge->dev,
824 agp_bridge->capndx + PCI_AGP_STATUS, &bridge_agpstat);
825
826 bridge_agpstat = agp_collect_device_status(agp_bridge, requested_mode, bridge_agpstat);
827 if (bridge_agpstat == 0)
828 /* Something bad happened. FIXME: Return error code? */
829 return;
830
831 bridge_agpstat |= AGPSTAT_AGP_ENABLE;
832
833 /* Do AGP version specific frobbing. */
834 if (bridge->major_version >= 3) {
David Mosberger66bb8bf2005-04-04 13:29:43 -0700835 if (bridge->mode & AGPSTAT_MODE_3_0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* If we have 3.5, we can do the isoch stuff. */
837 if (bridge->minor_version >= 5)
838 agp_3_5_enable(bridge);
839 agp_device_command(bridge_agpstat, TRUE);
840 return;
841 } else {
842 /* Disable calibration cycle in RX91<1> when not in AGP3.0 mode of operation.*/
843 bridge_agpstat &= ~(7<<10) ;
844 pci_read_config_dword(bridge->dev,
845 bridge->capndx+AGPCTRL, &temp);
846 temp |= (1<<9);
847 pci_write_config_dword(bridge->dev,
848 bridge->capndx+AGPCTRL, temp);
849
Dave Jones8c8b8382005-08-17 23:08:11 -0700850 printk(KERN_INFO PFX "Device is in legacy mode,"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 " falling back to 2.x\n");
852 }
853 }
854
855 /* AGP v<3 */
856 agp_device_command(bridge_agpstat, FALSE);
857}
858EXPORT_SYMBOL(agp_generic_enable);
859
860
861int agp_generic_create_gatt_table(struct agp_bridge_data *bridge)
862{
863 char *table;
864 char *table_end;
865 int size;
866 int page_order;
867 int num_entries;
868 int i;
869 void *temp;
870 struct page *page;
871
872 /* The generic routines can't handle 2 level gatt's */
873 if (bridge->driver->size_type == LVL2_APER_SIZE)
874 return -EINVAL;
875
876 table = NULL;
877 i = bridge->aperture_size_idx;
878 temp = bridge->current_size;
879 size = page_order = num_entries = 0;
880
881 if (bridge->driver->size_type != FIXED_APER_SIZE) {
882 do {
883 switch (bridge->driver->size_type) {
884 case U8_APER_SIZE:
885 size = A_SIZE_8(temp)->size;
886 page_order =
887 A_SIZE_8(temp)->page_order;
888 num_entries =
889 A_SIZE_8(temp)->num_entries;
890 break;
891 case U16_APER_SIZE:
892 size = A_SIZE_16(temp)->size;
893 page_order = A_SIZE_16(temp)->page_order;
894 num_entries = A_SIZE_16(temp)->num_entries;
895 break;
896 case U32_APER_SIZE:
897 size = A_SIZE_32(temp)->size;
898 page_order = A_SIZE_32(temp)->page_order;
899 num_entries = A_SIZE_32(temp)->num_entries;
900 break;
901 /* This case will never really happen. */
902 case FIXED_APER_SIZE:
903 case LVL2_APER_SIZE:
904 default:
905 size = page_order = num_entries = 0;
906 break;
907 }
908
Keir Fraser07eee782005-03-30 13:17:04 -0800909 table = alloc_gatt_pages(page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (table == NULL) {
912 i++;
913 switch (bridge->driver->size_type) {
914 case U8_APER_SIZE:
915 bridge->current_size = A_IDX8(bridge);
916 break;
917 case U16_APER_SIZE:
918 bridge->current_size = A_IDX16(bridge);
919 break;
920 case U32_APER_SIZE:
921 bridge->current_size = A_IDX32(bridge);
922 break;
Dave Jones89197e32006-05-30 18:19:39 -0400923 /* These cases will never really happen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 case FIXED_APER_SIZE:
925 case LVL2_APER_SIZE:
926 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 break;
928 }
929 temp = bridge->current_size;
930 } else {
931 bridge->aperture_size_idx = i;
932 }
933 } while (!table && (i < bridge->driver->num_aperture_sizes));
934 } else {
935 size = ((struct aper_size_info_fixed *) temp)->size;
936 page_order = ((struct aper_size_info_fixed *) temp)->page_order;
937 num_entries = ((struct aper_size_info_fixed *) temp)->num_entries;
Keir Fraser07eee782005-03-30 13:17:04 -0800938 table = alloc_gatt_pages(page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
941 if (table == NULL)
942 return -ENOMEM;
943
944 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
945
946 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
947 SetPageReserved(page);
948
949 bridge->gatt_table_real = (u32 *) table;
950 agp_gatt_table = (void *)table;
951
952 bridge->driver->cache_flush();
Keir Fraser07eee782005-03-30 13:17:04 -0800953 bridge->gatt_table = ioremap_nocache(virt_to_gart(table),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 (PAGE_SIZE * (1 << page_order)));
955 bridge->driver->cache_flush();
956
957 if (bridge->gatt_table == NULL) {
958 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
959 ClearPageReserved(page);
960
Keir Fraser07eee782005-03-30 13:17:04 -0800961 free_gatt_pages(table, page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 return -ENOMEM;
964 }
Keir Fraser07eee782005-03-30 13:17:04 -0800965 bridge->gatt_bus_addr = virt_to_gart(bridge->gatt_table_real);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* AK: bogus, should encode addresses > 4GB */
968 for (i = 0; i < num_entries; i++) {
969 writel(bridge->scratch_page, bridge->gatt_table+i);
970 readl(bridge->gatt_table+i); /* PCI Posting. */
971 }
972
973 return 0;
974}
975EXPORT_SYMBOL(agp_generic_create_gatt_table);
976
977int agp_generic_free_gatt_table(struct agp_bridge_data *bridge)
978{
979 int page_order;
980 char *table, *table_end;
981 void *temp;
982 struct page *page;
983
984 temp = bridge->current_size;
985
986 switch (bridge->driver->size_type) {
987 case U8_APER_SIZE:
988 page_order = A_SIZE_8(temp)->page_order;
989 break;
990 case U16_APER_SIZE:
991 page_order = A_SIZE_16(temp)->page_order;
992 break;
993 case U32_APER_SIZE:
994 page_order = A_SIZE_32(temp)->page_order;
995 break;
996 case FIXED_APER_SIZE:
997 page_order = A_SIZE_FIX(temp)->page_order;
998 break;
999 case LVL2_APER_SIZE:
1000 /* The generic routines can't deal with 2 level gatt's */
1001 return -EINVAL;
1002 break;
1003 default:
1004 page_order = 0;
1005 break;
1006 }
1007
1008 /* Do not worry about freeing memory, because if this is
1009 * called, then all agp memory is deallocated and removed
1010 * from the table. */
1011
1012 iounmap(bridge->gatt_table);
1013 table = (char *) bridge->gatt_table_real;
1014 table_end = table + ((PAGE_SIZE * (1 << page_order)) - 1);
1015
1016 for (page = virt_to_page(table); page <= virt_to_page(table_end); page++)
1017 ClearPageReserved(page);
1018
Keir Fraser07eee782005-03-30 13:17:04 -08001019 free_gatt_pages(bridge->gatt_table_real, page_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 agp_gatt_table = NULL;
1022 bridge->gatt_table = NULL;
1023 bridge->gatt_table_real = NULL;
1024 bridge->gatt_bus_addr = 0;
1025
1026 return 0;
1027}
1028EXPORT_SYMBOL(agp_generic_free_gatt_table);
1029
1030
1031int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
1032{
1033 int num_entries;
1034 size_t i;
1035 off_t j;
1036 void *temp;
1037 struct agp_bridge_data *bridge;
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001038 int mask_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 bridge = mem->bridge;
1041 if (!bridge)
1042 return -EINVAL;
1043
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001044 if (mem->page_count == 0)
1045 return 0;
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 temp = bridge->current_size;
1048
1049 switch (bridge->driver->size_type) {
1050 case U8_APER_SIZE:
1051 num_entries = A_SIZE_8(temp)->num_entries;
1052 break;
1053 case U16_APER_SIZE:
1054 num_entries = A_SIZE_16(temp)->num_entries;
1055 break;
1056 case U32_APER_SIZE:
1057 num_entries = A_SIZE_32(temp)->num_entries;
1058 break;
1059 case FIXED_APER_SIZE:
1060 num_entries = A_SIZE_FIX(temp)->num_entries;
1061 break;
1062 case LVL2_APER_SIZE:
1063 /* The generic routines can't deal with 2 level gatt's */
1064 return -EINVAL;
1065 break;
1066 default:
1067 num_entries = 0;
1068 break;
1069 }
1070
1071 num_entries -= agp_memory_reserved/PAGE_SIZE;
1072 if (num_entries < 0) num_entries = 0;
1073
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001074 if (type != mem->type) {
1075 return -EINVAL;
1076 }
1077
1078 mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
1079 if (mask_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* The generic routines know nothing of memory types */
1081 return -EINVAL;
1082 }
1083
1084 /* AK: could wrap */
1085 if ((pg_start + mem->page_count) > num_entries)
1086 return -EINVAL;
1087
1088 j = pg_start;
1089
1090 while (j < (pg_start + mem->page_count)) {
1091 if (!PGE_EMPTY(bridge, readl(bridge->gatt_table+j)))
1092 return -EBUSY;
1093 j++;
1094 }
1095
1096 if (mem->is_flushed == FALSE) {
1097 bridge->driver->cache_flush();
1098 mem->is_flushed = TRUE;
1099 }
1100
1101 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001102 writel(bridge->driver->mask_memory(bridge, mem->memory[i], mask_type),
1103 bridge->gatt_table+j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001105 readl(bridge->gatt_table+j-1); /* PCI Posting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 bridge->driver->tlb_flush(mem);
1108 return 0;
1109}
1110EXPORT_SYMBOL(agp_generic_insert_memory);
1111
1112
1113int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
1114{
1115 size_t i;
1116 struct agp_bridge_data *bridge;
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001117 int mask_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 bridge = mem->bridge;
1120 if (!bridge)
1121 return -EINVAL;
1122
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001123 if (mem->page_count == 0)
1124 return 0;
1125
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001126 if (type != mem->type)
1127 return -EINVAL;
1128
1129 mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
1130 if (mask_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* The generic routines know nothing of memory types */
1132 return -EINVAL;
1133 }
1134
1135 /* AK: bogus, should encode addresses > 4GB */
1136 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
1137 writel(bridge->scratch_page, bridge->gatt_table+i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Thomas Hellstrom5aa80c72006-12-20 16:33:41 +01001139 readl(bridge->gatt_table+i-1); /* PCI Posting. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 bridge->driver->tlb_flush(mem);
1142 return 0;
1143}
1144EXPORT_SYMBOL(agp_generic_remove_memory);
1145
1146
1147struct agp_memory *agp_generic_alloc_by_type(size_t page_count, int type)
1148{
1149 return NULL;
1150}
1151EXPORT_SYMBOL(agp_generic_alloc_by_type);
1152
1153
1154void agp_generic_free_by_type(struct agp_memory *curr)
1155{
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001156 agp_free_page_array(curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 agp_free_key(curr->key);
1158 kfree(curr);
1159}
1160EXPORT_SYMBOL(agp_generic_free_by_type);
1161
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001162struct agp_memory *agp_generic_alloc_user(size_t page_count, int type)
1163{
1164 struct agp_memory *new;
1165 int i;
1166 int pages;
1167
1168 pages = (page_count + ENTRIES_PER_PAGE - 1) / ENTRIES_PER_PAGE;
1169 new = agp_create_user_memory(page_count);
1170 if (new == NULL)
1171 return NULL;
1172
1173 for (i = 0; i < page_count; i++) {
1174 new->memory[i] = 0;
1175 }
1176 new->page_count = 0;
1177 new->type = type;
1178 new->num_scratch_pages = pages;
1179
1180 return new;
1181}
1182EXPORT_SYMBOL(agp_generic_alloc_user);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185/*
1186 * Basic Page Allocation Routines -
1187 * These routines handle page allocation and by default they reserve the allocated
1188 * memory. They also handle incrementing the current_memory_agp value, Which is checked
1189 * against a maximum value.
1190 */
1191
1192void *agp_generic_alloc_page(struct agp_bridge_data *bridge)
1193{
1194 struct page * page;
1195
Linus Torvalds66c669b2006-11-22 14:55:29 -08001196 page = alloc_page(GFP_KERNEL | GFP_DMA32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (page == NULL)
1198 return NULL;
1199
1200 map_page_into_agp(page);
1201
1202 get_page(page);
1203 SetPageLocked(page);
1204 atomic_inc(&agp_bridge->current_memory_agp);
1205 return page_address(page);
1206}
1207EXPORT_SYMBOL(agp_generic_alloc_page);
1208
1209
1210void agp_generic_destroy_page(void *addr)
1211{
1212 struct page *page;
1213
1214 if (addr == NULL)
1215 return;
1216
1217 page = virt_to_page(addr);
1218 unmap_page_from_agp(page);
1219 put_page(page);
1220 unlock_page(page);
1221 free_page((unsigned long)addr);
1222 atomic_dec(&agp_bridge->current_memory_agp);
1223}
1224EXPORT_SYMBOL(agp_generic_destroy_page);
1225
1226/* End Basic Page Allocation Routines */
1227
1228
1229/**
1230 * agp_enable - initialise the agp point-to-point connection.
1231 *
1232 * @mode: agp mode register value to configure with.
1233 */
1234void agp_enable(struct agp_bridge_data *bridge, u32 mode)
1235{
1236 if (!bridge)
1237 return;
1238 bridge->driver->agp_enable(bridge, mode);
1239}
1240EXPORT_SYMBOL(agp_enable);
1241
1242/* When we remove the global variable agp_bridge from all drivers
1243 * then agp_alloc_bridge and agp_generic_find_bridge need to be updated
1244 */
1245
1246struct agp_bridge_data *agp_generic_find_bridge(struct pci_dev *pdev)
1247{
1248 if (list_empty(&agp_bridges))
1249 return NULL;
1250
1251 return agp_bridge;
1252}
1253
1254static void ipi_handler(void *null)
1255{
1256 flush_agp_cache();
1257}
1258
1259void global_cache_flush(void)
1260{
1261 if (on_each_cpu(ipi_handler, NULL, 1, 1) != 0)
1262 panic(PFX "timed out waiting for the other CPUs!\n");
1263}
1264EXPORT_SYMBOL(global_cache_flush);
1265
1266unsigned long agp_generic_mask_memory(struct agp_bridge_data *bridge,
1267 unsigned long addr, int type)
1268{
1269 /* memory type is ignored in the generic routine */
1270 if (bridge->driver->masks)
1271 return addr | bridge->driver->masks[0].mask;
1272 else
1273 return addr;
1274}
1275EXPORT_SYMBOL(agp_generic_mask_memory);
1276
Thomas Hellstroma030ce42007-01-23 10:33:43 +01001277int agp_generic_type_to_mask_type(struct agp_bridge_data *bridge,
1278 int type)
1279{
1280 if (type >= AGP_USER_TYPES)
1281 return 0;
1282 return type;
1283}
1284EXPORT_SYMBOL(agp_generic_type_to_mask_type);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/*
1287 * These functions are implemented according to the AGPv3 spec,
1288 * which covers implementation details that had previously been
1289 * left open.
1290 */
1291
1292int agp3_generic_fetch_size(void)
1293{
1294 u16 temp_size;
1295 int i;
1296 struct aper_size_info_16 *values;
1297
1298 pci_read_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, &temp_size);
1299 values = A_SIZE_16(agp_bridge->driver->aperture_sizes);
1300
1301 for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
1302 if (temp_size == values[i].size_value) {
1303 agp_bridge->previous_size =
1304 agp_bridge->current_size = (void *) (values + i);
1305
1306 agp_bridge->aperture_size_idx = i;
1307 return values[i].size;
1308 }
1309 }
1310 return 0;
1311}
1312EXPORT_SYMBOL(agp3_generic_fetch_size);
1313
1314void agp3_generic_tlbflush(struct agp_memory *mem)
1315{
1316 u32 ctrl;
1317 pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
1318 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_GTLBEN);
1319 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl);
1320}
1321EXPORT_SYMBOL(agp3_generic_tlbflush);
1322
1323int agp3_generic_configure(void)
1324{
1325 u32 temp;
1326 struct aper_size_info_16 *current_size;
1327
1328 current_size = A_SIZE_16(agp_bridge->current_size);
1329
1330 pci_read_config_dword(agp_bridge->dev, AGP_APBASE, &temp);
1331 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
1332
1333 /* set aperture size */
1334 pci_write_config_word(agp_bridge->dev, agp_bridge->capndx+AGPAPSIZE, current_size->size_value);
1335 /* set gart pointer */
1336 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPGARTLO, agp_bridge->gatt_bus_addr);
1337 /* enable aperture and GTLB */
1338 pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &temp);
1339 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, temp | AGPCTRL_APERENB | AGPCTRL_GTLBEN);
1340 return 0;
1341}
1342EXPORT_SYMBOL(agp3_generic_configure);
1343
1344void agp3_generic_cleanup(void)
1345{
1346 u32 ctrl;
1347 pci_read_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, &ctrl);
1348 pci_write_config_dword(agp_bridge->dev, agp_bridge->capndx+AGPCTRL, ctrl & ~AGPCTRL_APERENB);
1349}
1350EXPORT_SYMBOL(agp3_generic_cleanup);
1351
1352struct aper_size_info_16 agp3_generic_sizes[AGP_GENERIC_SIZES_ENTRIES] =
1353{
1354 {4096, 1048576, 10,0x000},
1355 {2048, 524288, 9, 0x800},
1356 {1024, 262144, 8, 0xc00},
1357 { 512, 131072, 7, 0xe00},
1358 { 256, 65536, 6, 0xf00},
1359 { 128, 32768, 5, 0xf20},
1360 { 64, 16384, 4, 0xf30},
1361 { 32, 8192, 3, 0xf38},
1362 { 16, 4096, 2, 0xf3c},
1363 { 8, 2048, 1, 0xf3e},
1364 { 4, 1024, 0, 0xf3f}
1365};
1366EXPORT_SYMBOL(agp3_generic_sizes);
1367