vserver 1.9.5.x5
[linux-2.6.git] / drivers / char / agp / amd64-agp.c
1 /*
2  * Copyright 2001-2003 SuSE Labs.
3  * Distributed under the GNU public license, v2.
4  *
5  * This is a GART driver for the AMD Opteron/Athlon64 on-CPU northbridge.
6  * It also includes support for the AMD 8151 AGP bridge,
7  * although it doesn't actually do much, as all the real
8  * work is done in the northbridge(s).
9  */
10
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/init.h>
15 #include <linux/agp_backend.h>
16 #include "agp.h"
17
18 /* Will need to be increased if AMD64 ever goes >8-way. */
19 #define MAX_HAMMER_GARTS   8
20
21 /* PTE bits. */
22 #define GPTE_VALID      1
23 #define GPTE_COHERENT   2
24
25 /* Aperture control register bits. */
26 #define GARTEN          (1<<0)
27 #define DISGARTCPU      (1<<4)
28 #define DISGARTIO       (1<<5)
29
30 /* GART cache control register bits. */
31 #define INVGART         (1<<0)
32 #define GARTPTEERR      (1<<1)
33
34 /* K8 On-cpu GART registers */
35 #define AMD64_GARTAPERTURECTL   0x90
36 #define AMD64_GARTAPERTUREBASE  0x94
37 #define AMD64_GARTTABLEBASE     0x98
38 #define AMD64_GARTCACHECTL      0x9c
39 #define AMD64_GARTEN            (1<<0)
40
41 /* NVIDIA K8 registers */
42 #define NVIDIA_X86_64_0_APBASE          0x10
43 #define NVIDIA_X86_64_1_APBASE1         0x50
44 #define NVIDIA_X86_64_1_APLIMIT1        0x54
45 #define NVIDIA_X86_64_1_APSIZE          0xa8
46 #define NVIDIA_X86_64_1_APBASE2         0xd8
47 #define NVIDIA_X86_64_1_APLIMIT2        0xdc
48
49 /* ULi K8 registers */
50 #define ULI_X86_64_BASE_ADDR            0x10
51 #define ULI_X86_64_HTT_FEA_REG          0x50
52 #define ULI_X86_64_ENU_SCR_REG          0x54
53
54 static int nr_garts;
55 static struct pci_dev * hammers[MAX_HAMMER_GARTS];
56
57 static struct resource *aperture_resource;
58 static int __initdata agp_try_unsupported;
59
60 static int gart_iterator;
61 #define for_each_nb() for(gart_iterator=0;gart_iterator<nr_garts;gart_iterator++)
62
63 static void flush_amd64_tlb(struct pci_dev *dev)
64 {
65         u32 tmp;
66
67         pci_read_config_dword (dev, AMD64_GARTCACHECTL, &tmp);
68         tmp |= INVGART;
69         pci_write_config_dword (dev, AMD64_GARTCACHECTL, tmp);
70 }
71
72 static void amd64_tlbflush(struct agp_memory *temp)
73 {
74         for_each_nb()
75                 flush_amd64_tlb(hammers[gart_iterator]);
76 }
77
78 static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
79 {
80         int i, j, num_entries;
81         long long tmp;
82         u32 pte;
83
84         num_entries = agp_num_entries();
85
86         if (type != 0 || mem->type != 0)
87                 return -EINVAL;
88
89         /* Make sure we can fit the range in the gatt table. */
90         /* FIXME: could wrap */
91         if (((unsigned long)pg_start + mem->page_count) > num_entries)
92                 return -EINVAL;
93
94         j = pg_start;
95
96         /* gatt table should be empty. */
97         while (j < (pg_start + mem->page_count)) {
98                 if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+j)))
99                         return -EBUSY;
100                 j++;
101         }
102
103         if (mem->is_flushed == FALSE) {
104                 global_cache_flush();
105                 mem->is_flushed = TRUE;
106         }
107
108         for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
109                 tmp = agp_bridge->driver->mask_memory(mem->memory[i], mem->type);
110
111                 BUG_ON(tmp & 0xffffff0000000ffcULL);
112                 pte = (tmp & 0x000000ff00000000ULL) >> 28;
113                 pte |=(tmp & 0x00000000fffff000ULL);
114                 pte |= GPTE_VALID | GPTE_COHERENT;
115
116                 writel(pte, agp_bridge->gatt_table+j);
117                 readl(agp_bridge->gatt_table+j);        /* PCI Posting. */
118         }
119         amd64_tlbflush(mem);
120         return 0;
121 }
122
123 /*
124  * This hack alters the order element according
125  * to the size of a long. It sucks. I totally disown this, even
126  * though it does appear to work for the most part.
127  */
128 static struct aper_size_info_32 amd64_aperture_sizes[7] =
129 {
130         {32,   8192,   3+(sizeof(long)/8), 0 },
131         {64,   16384,  4+(sizeof(long)/8), 1<<1 },
132         {128,  32768,  5+(sizeof(long)/8), 1<<2 },
133         {256,  65536,  6+(sizeof(long)/8), 1<<1 | 1<<2 },
134         {512,  131072, 7+(sizeof(long)/8), 1<<3 },
135         {1024, 262144, 8+(sizeof(long)/8), 1<<1 | 1<<3},
136         {2048, 524288, 9+(sizeof(long)/8), 1<<2 | 1<<3}
137 };
138
139
140 /*
141  * Get the current Aperture size from the x86-64.
142  * Note, that there may be multiple x86-64's, but we just return
143  * the value from the first one we find. The set_size functions
144  * keep the rest coherent anyway. Or at least should do.
145  */
146 static int amd64_fetch_size(void)
147 {
148         struct pci_dev *dev;
149         int i;
150         u32 temp;
151         struct aper_size_info_32 *values;
152
153         dev = hammers[0];
154         if (dev==NULL)
155                 return 0;
156
157         pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &temp);
158         temp = (temp & 0xe);
159         values = A_SIZE_32(amd64_aperture_sizes);
160
161         for (i = 0; i < agp_bridge->driver->num_aperture_sizes; i++) {
162                 if (temp == values[i].size_value) {
163                         agp_bridge->previous_size =
164                             agp_bridge->current_size = (void *) (values + i);
165
166                         agp_bridge->aperture_size_idx = i;
167                         return values[i].size;
168                 }
169         }
170         return 0;
171 }
172
173 /*
174  * In a multiprocessor x86-64 system, this function gets
175  * called once for each CPU.
176  */
177 static u64 amd64_configure (struct pci_dev *hammer, u64 gatt_table)
178 {
179         u64 aperturebase;
180         u32 tmp;
181         u64 addr, aper_base;
182
183         /* Address to map to */
184         pci_read_config_dword (hammer, AMD64_GARTAPERTUREBASE, &tmp);
185         aperturebase = tmp << 25;
186         aper_base = (aperturebase & PCI_BASE_ADDRESS_MEM_MASK);
187
188         /* address of the mappings table */
189         addr = (u64) gatt_table;
190         addr >>= 12;
191         tmp = (u32) addr<<4;
192         tmp &= ~0xf;
193         pci_write_config_dword (hammer, AMD64_GARTTABLEBASE, tmp);
194
195         /* Enable GART translation for this hammer. */
196         pci_read_config_dword(hammer, AMD64_GARTAPERTURECTL, &tmp);
197         tmp |= GARTEN;
198         tmp &= ~(DISGARTCPU | DISGARTIO);
199         pci_write_config_dword(hammer, AMD64_GARTAPERTURECTL, tmp);
200
201         /* keep CPU's coherent. */
202         flush_amd64_tlb (hammer);
203
204         return aper_base;
205 }
206
207
208 static struct aper_size_info_32 amd_8151_sizes[7] =
209 {
210         {2048, 524288, 9, 0x00000000 }, /* 0 0 0 0 0 0 */
211         {1024, 262144, 8, 0x00000400 }, /* 1 0 0 0 0 0 */
212         {512,  131072, 7, 0x00000600 }, /* 1 1 0 0 0 0 */
213         {256,  65536,  6, 0x00000700 }, /* 1 1 1 0 0 0 */
214         {128,  32768,  5, 0x00000720 }, /* 1 1 1 1 0 0 */
215         {64,   16384,  4, 0x00000730 }, /* 1 1 1 1 1 0 */
216         {32,   8192,   3, 0x00000738 }  /* 1 1 1 1 1 1 */
217 };
218
219 static int amd_8151_configure(void)
220 {
221         unsigned long gatt_bus = virt_to_phys(agp_bridge->gatt_table_real);
222
223         /* Configure AGP regs in each x86-64 host bridge. */
224         for_each_nb() {
225                 agp_bridge->gart_bus_addr =
226                                 amd64_configure(hammers[gart_iterator],gatt_bus);
227         }
228         return 0;
229 }
230
231
232 static void amd64_cleanup(void)
233 {
234         u32 tmp;
235
236         for_each_nb() {
237                 /* disable gart translation */
238                 pci_read_config_dword (hammers[gart_iterator], AMD64_GARTAPERTURECTL, &tmp);
239                 tmp &= ~AMD64_GARTEN;
240                 pci_write_config_dword (hammers[gart_iterator], AMD64_GARTAPERTURECTL, tmp);
241         }
242 }
243
244
245 struct agp_bridge_driver amd_8151_driver = {
246         .owner                  = THIS_MODULE,
247         .aperture_sizes         = amd_8151_sizes,
248         .size_type              = U32_APER_SIZE,
249         .num_aperture_sizes     = 7,
250         .configure              = amd_8151_configure,
251         .fetch_size             = amd64_fetch_size,
252         .cleanup                = amd64_cleanup,
253         .tlb_flush              = amd64_tlbflush,
254         .mask_memory            = agp_generic_mask_memory,
255         .masks                  = NULL,
256         .agp_enable             = agp_generic_enable,
257         .cache_flush            = global_cache_flush,
258         .create_gatt_table      = agp_generic_create_gatt_table,
259         .free_gatt_table        = agp_generic_free_gatt_table,
260         .insert_memory          = amd64_insert_memory,
261         .remove_memory          = agp_generic_remove_memory,
262         .alloc_by_type          = agp_generic_alloc_by_type,
263         .free_by_type           = agp_generic_free_by_type,
264         .agp_alloc_page         = agp_generic_alloc_page,
265         .agp_destroy_page       = agp_generic_destroy_page,
266 };
267
268 /* Some basic sanity checks for the aperture. */
269 static int __devinit aperture_valid(u64 aper, u32 size)
270 {
271         u32 pfn, c;
272         if (aper == 0) {
273                 printk(KERN_ERR PFX "No aperture\n");
274                 return 0;
275         }
276         if (size < 32*1024*1024) {
277                 printk(KERN_ERR PFX "Aperture too small (%d MB)\n", size>>20);
278                 return 0;
279         }
280         if (aper + size > 0xffffffff) {
281                 printk(KERN_ERR PFX "Aperture out of bounds\n");
282                 return 0;
283         }
284         pfn = aper >> PAGE_SHIFT;
285         for (c = 0; c < size/PAGE_SIZE; c++) {
286                 if (!pfn_valid(pfn + c))
287                         break;
288                 if (!PageReserved(pfn_to_page(pfn + c))) {
289                         printk(KERN_ERR PFX "Aperture pointing to RAM\n");
290                         return 0;
291                 }
292         }
293
294         /* Request the Aperture. This catches cases when someone else
295            already put a mapping in there - happens with some very broken BIOS
296
297            Maybe better to use pci_assign_resource/pci_enable_device instead
298            trusting the bridges? */
299         if (!aperture_resource &&
300             !(aperture_resource = request_mem_region(aper, size, "aperture"))) {
301                 printk(KERN_ERR PFX "Aperture conflicts with PCI mapping.\n");
302                 return 0;
303         }
304         return 1;
305 }
306
307 /*
308  * W*s centric BIOS sometimes only set up the aperture in the AGP
309  * bridge, not the northbridge. On AMD64 this is handled early
310  * in aperture.c, but when GART_IOMMU is not enabled or we run
311  * on a 32bit kernel this needs to be redone.
312  * Unfortunately it is impossible to fix the aperture here because it's too late
313  * to allocate that much memory. But at least error out cleanly instead of
314  * crashing.
315  */
316 static __devinit int fix_northbridge(struct pci_dev *nb, struct pci_dev *agp,
317                                                                  u16 cap)
318 {
319         u32 aper_low, aper_hi;
320         u64 aper, nb_aper;
321         int order = 0;
322         u32 nb_order, nb_base;
323         u16 apsize;
324
325         pci_read_config_dword(nb, 0x90, &nb_order);
326         nb_order = (nb_order >> 1) & 7;
327         pci_read_config_dword(nb, 0x94, &nb_base);
328         nb_aper = nb_base << 25;
329         if (aperture_valid(nb_aper, (32*1024*1024)<<nb_order)) {
330                 return 0;
331         }
332
333         /* Northbridge seems to contain crap. Try the AGP bridge. */
334
335         pci_read_config_word(agp, cap+0x14, &apsize);
336         if (apsize == 0xffff)
337                 return -1;
338
339         apsize &= 0xfff;
340         /* Some BIOS use weird encodings not in the AGPv3 table. */
341         if (apsize & 0xff)
342                 apsize |= 0xf00;
343         order = 7 - hweight16(apsize);
344
345         pci_read_config_dword(agp, 0x10, &aper_low);
346         pci_read_config_dword(agp, 0x14, &aper_hi);
347         aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
348         printk(KERN_INFO PFX "Aperture from AGP @ %Lx size %u MB\n", aper, 32 << order);
349         if (order < 0 || !aperture_valid(aper, (32*1024*1024)<<order))
350                 return -1;
351
352         pci_write_config_dword(nb, 0x90, order << 1);
353         pci_write_config_dword(nb, 0x94, aper >> 25);
354
355         return 0;
356 }
357
358 static __devinit int cache_nbs (struct pci_dev *pdev, u32 cap_ptr)
359 {
360         struct pci_dev *loop_dev = NULL;
361         int i = 0;
362
363         /* cache pci_devs of northbridges. */
364         while ((loop_dev = pci_get_device(PCI_VENDOR_ID_AMD, 0x1103, loop_dev))
365                         != NULL) {
366                 if (i == MAX_HAMMER_GARTS) {
367                         printk(KERN_ERR PFX "Too many northbridges for AGP\n");
368                         return -1;
369                 }
370                 if (fix_northbridge(loop_dev, pdev, cap_ptr) < 0) {
371                         printk(KERN_ERR PFX "No usable aperture found.\n");
372 #ifdef __x86_64__
373                         /* should port this to i386 */
374                         printk(KERN_ERR PFX "Consider rebooting with iommu=memaper=2 to get a good aperture.\n");
375 #endif
376                         return -1;
377                 }
378                 hammers[i++] = loop_dev;
379         }
380                 nr_garts = i;
381         return i == 0 ? -1 : 0;
382 }
383
384 /* Handle AMD 8151 quirks */
385 static void __devinit amd8151_init(struct pci_dev *pdev, struct agp_bridge_data *bridge)
386 {
387         char *revstring;
388         u8 rev_id;
389
390         pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
391         switch (rev_id) {
392         case 0x01: revstring="A0"; break;
393         case 0x02: revstring="A1"; break;
394         case 0x11: revstring="B0"; break;
395         case 0x12: revstring="B1"; break;
396         case 0x13: revstring="B2"; break;
397         case 0x14: revstring="B3"; break;
398         default:   revstring="??"; break;
399         }
400
401         printk (KERN_INFO PFX "Detected AMD 8151 AGP Bridge rev %s\n", revstring);
402
403         /*
404          * Work around errata.
405          * Chips before B2 stepping incorrectly reporting v3.5
406          */
407         if (rev_id < 0x13) {
408                 printk (KERN_INFO PFX "Correcting AGP revision (reports 3.5, is really 3.0)\n");
409                 bridge->major_version = 3;
410                 bridge->minor_version = 0;
411         }
412 }
413
414
415 static struct aper_size_info_32 uli_sizes[7] =
416 {
417         {256, 65536, 6, 10},
418         {128, 32768, 5, 9},
419         {64, 16384, 4, 8},
420         {32, 8192, 3, 7},
421         {16, 4096, 2, 6},
422         {8, 2048, 1, 4},
423         {4, 1024, 0, 3}
424 };
425 static int __devinit uli_agp_init(struct pci_dev *pdev)
426 {
427         u32 httfea,baseaddr,enuscr;
428         struct pci_dev *dev1;
429         int i;
430         unsigned size = amd64_fetch_size();
431         printk(KERN_INFO "Setting up ULi AGP. \n");
432         dev1 = pci_find_slot ((unsigned int)pdev->bus->number,PCI_DEVFN(0,0));
433         if (dev1 == NULL) {
434                 printk(KERN_INFO PFX "Detected a ULi chipset, "
435                         "but could not fine the secondary device.\n");
436                 return -ENODEV;
437         }
438
439         for (i = 0; i < ARRAY_SIZE(uli_sizes); i++)
440                 if (uli_sizes[i].size == size)
441                         break;
442
443         if (i == ARRAY_SIZE(uli_sizes)) {
444                 printk(KERN_INFO PFX "No ULi size found for %d\n", size);
445                 return -ENODEV;
446         }
447
448         /* shadow x86-64 registers into ULi registers */
449         pci_read_config_dword (hammers[0], AMD64_GARTAPERTUREBASE, &httfea);
450
451         /* if x86-64 aperture base is beyond 4G, exit here */
452         if ((httfea & 0x7fff) >> (32 - 25))
453                 return -ENODEV;
454
455         httfea = (httfea& 0x7fff) << 25;
456
457         pci_read_config_dword(pdev, ULI_X86_64_BASE_ADDR, &baseaddr);
458         baseaddr&= ~PCI_BASE_ADDRESS_MEM_MASK;
459         baseaddr|= httfea;
460         pci_write_config_dword(pdev, ULI_X86_64_BASE_ADDR, baseaddr);
461
462         enuscr= httfea+ (size * 1024 * 1024) - 1;
463         pci_write_config_dword(dev1, ULI_X86_64_HTT_FEA_REG, httfea);
464         pci_write_config_dword(dev1, ULI_X86_64_ENU_SCR_REG, enuscr);
465         return 0;
466 }
467
468
469 static struct aper_size_info_32 nforce3_sizes[5] =
470 {
471         {512,  131072, 7, 0x00000000 },
472         {256,  65536,  6, 0x00000008 },
473         {128,  32768,  5, 0x0000000C },
474         {64,   16384,  4, 0x0000000E },
475         {32,   8192,   3, 0x0000000F }
476 };
477
478 /* Handle shadow device of the Nvidia NForce3 */
479 /* CHECK-ME original 2.4 version set up some IORRs. Check if that is needed. */
480 static int __devinit nforce3_agp_init(struct pci_dev *pdev)
481 {
482         u32 tmp, apbase, apbar, aplimit;
483         struct pci_dev *dev1;
484         int i;
485         unsigned size = amd64_fetch_size();
486
487         printk(KERN_INFO PFX "Setting up Nforce3 AGP.\n");
488
489         dev1 = pci_find_slot((unsigned int)pdev->bus->number, PCI_DEVFN(11, 0));
490         if (dev1 == NULL) {
491                 printk(KERN_INFO PFX "agpgart: Detected an NVIDIA "
492                         "nForce3 chipset, but could not find "
493                         "the secondary device.\n");
494                 return -ENODEV;
495         }
496
497         for (i = 0; i < ARRAY_SIZE(nforce3_sizes); i++)
498                 if (nforce3_sizes[i].size == size)
499                         break;
500
501         if (i == ARRAY_SIZE(nforce3_sizes)) {
502                 printk(KERN_INFO PFX "No NForce3 size found for %d\n", size);
503                 return -ENODEV;
504         }
505
506         pci_read_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, &tmp);
507         tmp &= ~(0xf);
508         tmp |= nforce3_sizes[i].size_value;
509         pci_write_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, tmp);
510
511         /* shadow x86-64 registers into NVIDIA registers */
512         pci_read_config_dword (hammers[0], AMD64_GARTAPERTUREBASE, &apbase);
513
514         /* if x86-64 aperture base is beyond 4G, exit here */
515         if ( (apbase & 0x7fff) >> (32 - 25) )
516                  return -ENODEV;
517
518         apbase = (apbase & 0x7fff) << 25;
519
520         pci_read_config_dword(pdev, NVIDIA_X86_64_0_APBASE, &apbar);
521         apbar &= ~PCI_BASE_ADDRESS_MEM_MASK;
522         apbar |= apbase;
523         pci_write_config_dword(pdev, NVIDIA_X86_64_0_APBASE, apbar);
524
525         aplimit = apbase + (size * 1024 * 1024) - 1;
526         pci_write_config_dword(dev1, NVIDIA_X86_64_1_APBASE1, apbase);
527         pci_write_config_dword(dev1, NVIDIA_X86_64_1_APLIMIT1, aplimit);
528         pci_write_config_dword(dev1, NVIDIA_X86_64_1_APBASE2, apbase);
529         pci_write_config_dword(dev1, NVIDIA_X86_64_1_APLIMIT2, aplimit);
530
531         return 0;
532 }
533
534 static int __devinit agp_amd64_probe(struct pci_dev *pdev,
535                                      const struct pci_device_id *ent)
536 {
537         struct agp_bridge_data *bridge;
538         u8 cap_ptr;
539
540         cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
541         if (!cap_ptr)
542                 return -ENODEV;
543
544         /* Could check for AGPv3 here */
545
546         bridge = agp_alloc_bridge();
547         if (!bridge)
548                 return -ENOMEM;
549
550         if (pdev->vendor == PCI_VENDOR_ID_AMD &&
551             pdev->device == PCI_DEVICE_ID_AMD_8151_0) {
552                 amd8151_init(pdev, bridge);
553         } else {
554                 printk(KERN_INFO PFX "Detected AGP bridge %x\n", pdev->devfn);
555         }
556
557         bridge->driver = &amd_8151_driver;
558         bridge->dev = pdev;
559         bridge->capndx = cap_ptr;
560
561         /* Fill in the mode register */
562         pci_read_config_dword(pdev, bridge->capndx+PCI_AGP_STATUS, &bridge->mode);
563
564         if (cache_nbs(pdev, cap_ptr) == -1) {
565                 agp_put_bridge(bridge);
566                 return -ENODEV;
567         }
568
569         if (pdev->vendor == PCI_VENDOR_ID_NVIDIA) {
570                 int ret = nforce3_agp_init(pdev);
571                 if (ret) {
572                         agp_put_bridge(bridge);
573                         return ret;
574                 }
575         }
576
577         if (pdev->vendor == PCI_VENDOR_ID_AL) {
578                 int ret = uli_agp_init(pdev);
579                 if (ret) {
580                         agp_put_bridge(bridge);
581                         return ret;
582                 }
583         }
584
585         pci_set_drvdata(pdev, bridge);
586         return agp_add_bridge(bridge);
587 }
588
589 static void __devexit agp_amd64_remove(struct pci_dev *pdev)
590 {
591         struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
592
593         release_mem_region(virt_to_phys(bridge->gatt_table_real),
594                            amd64_aperture_sizes[bridge->aperture_size_idx].size);
595         agp_remove_bridge(bridge);
596         agp_put_bridge(bridge);
597 }
598
599 static struct pci_device_id agp_amd64_pci_table[] = {
600         {
601         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
602         .class_mask     = ~0,
603         .vendor         = PCI_VENDOR_ID_AMD,
604         .device         = PCI_DEVICE_ID_AMD_8151_0,
605         .subvendor      = PCI_ANY_ID,
606         .subdevice      = PCI_ANY_ID,
607         },
608         /* ULi M1689 */
609         {
610         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
611         .class_mask     = ~0,
612         .vendor         = PCI_VENDOR_ID_AL,
613         .device         = PCI_DEVICE_ID_AL_M1689,
614         .subvendor      = PCI_ANY_ID,
615         .subdevice      = PCI_ANY_ID,
616         },
617         /* VIA K8T800Pro */
618         {
619         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
620         .class_mask     = ~0,
621         .vendor         = PCI_VENDOR_ID_VIA,
622         .device         = PCI_DEVICE_ID_VIA_K8T800PRO_0,
623         .subvendor      = PCI_ANY_ID,
624         .subdevice      = PCI_ANY_ID,
625         },
626         /* VIA K8T800 */
627         {
628         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
629         .class_mask     = ~0,
630         .vendor         = PCI_VENDOR_ID_VIA,
631         .device         = PCI_DEVICE_ID_VIA_8385_0,
632         .subvendor      = PCI_ANY_ID,
633         .subdevice      = PCI_ANY_ID,
634         },
635         /* VIA K8M800 / K8N800 */
636         {
637         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
638         .class_mask     = ~0,
639         .vendor         = PCI_VENDOR_ID_VIA,
640         .device         = PCI_DEVICE_ID_VIA_8380_0,
641         .subvendor      = PCI_ANY_ID,
642         .subdevice      = PCI_ANY_ID,
643         },
644         /* VIA K8T890 */
645         {
646         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
647         .class_mask     = ~0,
648         .vendor         = PCI_VENDOR_ID_VIA,
649         .device         = PCI_DEVICE_ID_VIA_3238_0,
650         .subvendor      = PCI_ANY_ID,
651         .subdevice      = PCI_ANY_ID,
652         },
653         /* VIA K8T800/K8M800/K8N800 */
654         {
655         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
656         .class_mask     = ~0,
657         .vendor         = PCI_VENDOR_ID_VIA,
658         .device         = PCI_DEVICE_ID_VIA_838X_1,
659         .subvendor      = PCI_ANY_ID,
660         .subdevice      = PCI_ANY_ID,
661         },
662         /* NForce3 */
663         {
664         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
665         .class_mask     = ~0,
666         .vendor         = PCI_VENDOR_ID_NVIDIA,
667         .device         = PCI_DEVICE_ID_NVIDIA_NFORCE3,
668         .subvendor      = PCI_ANY_ID,
669         .subdevice      = PCI_ANY_ID,
670         },
671         {
672         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
673         .class_mask     = ~0,
674         .vendor         = PCI_VENDOR_ID_NVIDIA,
675         .device         = PCI_DEVICE_ID_NVIDIA_NFORCE3S,
676         .subvendor      = PCI_ANY_ID,
677         .subdevice      = PCI_ANY_ID,
678         },
679         /* SIS 755 */
680         {
681         .class          = (PCI_CLASS_BRIDGE_HOST << 8),
682         .class_mask     = ~0,
683         .vendor         = PCI_VENDOR_ID_SI,
684         .device         = PCI_DEVICE_ID_SI_755,
685         .subvendor      = PCI_ANY_ID,
686         .subdevice      = PCI_ANY_ID,
687         },
688         { }
689 };
690
691 MODULE_DEVICE_TABLE(pci, agp_amd64_pci_table);
692
693 static struct pci_driver agp_amd64_pci_driver = {
694         .name           = "agpgart-amd64",
695         .id_table       = agp_amd64_pci_table,
696         .probe          = agp_amd64_probe,
697         .remove         = agp_amd64_remove,
698 };
699
700
701 /* Not static due to IOMMU code calling it early. */
702 int __init agp_amd64_init(void)
703 {
704         int err = 0;
705         static struct pci_device_id amd64nb[] = {
706                 { PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x1103) },
707                 { },
708         };
709
710         if (agp_off)
711                 return -EINVAL;
712         if (pci_module_init(&agp_amd64_pci_driver) > 0) {
713                 struct pci_dev *dev;
714                 if (!agp_try_unsupported && !agp_try_unsupported_boot) {
715                         printk(KERN_INFO PFX "No supported AGP bridge found.\n");
716 #ifdef MODULE
717                         printk(KERN_INFO PFX "You can try agp_try_unsupported=1\n");
718 #else
719                         printk(KERN_INFO PFX "You can boot with agp=try_unsupported\n");
720 #endif
721                         return -ENODEV;
722                 }
723
724                 /* First check that we have at least one AMD64 NB */
725                 if (!pci_dev_present(amd64nb))
726                         return -ENODEV;
727
728                 /* Look for any AGP bridge */
729                 dev = NULL;
730                 err = -ENODEV;
731                 for_each_pci_dev(dev) {
732                         if (!pci_find_capability(dev, PCI_CAP_ID_AGP))
733                                 continue;
734                         /* Only one bridge supported right now */
735                         if (agp_amd64_probe(dev, NULL) == 0) {
736                                 err = 0;
737                                 break;
738                         }
739                 }
740         }
741         return err;
742 }
743
744 static void __exit agp_amd64_cleanup(void)
745 {
746         if (aperture_resource)
747                 release_resource(aperture_resource);
748         pci_unregister_driver(&agp_amd64_pci_driver);
749 }
750
751 /* On AMD64 the PCI driver needs to initialize this driver early
752    for the IOMMU, so it has to be called via a backdoor. */
753 #ifndef CONFIG_GART_IOMMU
754 module_init(agp_amd64_init);
755 module_exit(agp_amd64_cleanup);
756 #endif
757
758 MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>, Andi Kleen");
759 module_param(agp_try_unsupported, bool, 0);
760 MODULE_LICENSE("GPL");