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