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