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