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