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