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