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