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