Merge branch 'for-linus' of git://git390.marist.edu/pub/scm/linux-2.6
[safe/jmp/linux-2.6] / drivers / char / agp / amd64-agp.c
index d8200ac..70312da 100644 (file)
 #include <asm/page.h>          /* PAGE_SIZE */
 #include <asm/e820.h>
 #include <asm/k8.h>
+#include <asm/gart.h>
 #include "agp.h"
 
-/* PTE bits. */
-#define GPTE_VALID     1
-#define GPTE_COHERENT  2
-
-/* Aperture control register bits. */
-#define GARTEN         (1<<0)
-#define DISGARTCPU     (1<<4)
-#define DISGARTIO      (1<<5)
-
-/* GART cache control register bits. */
-#define INVGART                (1<<0)
-#define GARTPTEERR     (1<<1)
-
-/* K8 On-cpu GART registers */
-#define AMD64_GARTAPERTURECTL  0x90
-#define AMD64_GARTAPERTUREBASE 0x94
-#define AMD64_GARTTABLEBASE    0x98
-#define AMD64_GARTCACHECTL     0x9c
-#define AMD64_GARTEN           (1<<0)
-
 /* NVIDIA K8 registers */
 #define NVIDIA_X86_64_0_APBASE         0x10
 #define NVIDIA_X86_64_1_APBASE1                0x50
@@ -53,6 +34,7 @@
 
 static struct resource *aperture_resource;
 static int __initdata agp_try_unsupported = 1;
+static int agp_bridges_found;
 
 static void amd64_tlbflush(struct agp_memory *temp)
 {
@@ -90,14 +72,15 @@ static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
                j++;
        }
 
-       if (mem->is_flushed == FALSE) {
+       if (!mem->is_flushed) {
                global_cache_flush();
-               mem->is_flushed = TRUE;
+               mem->is_flushed = true;
        }
 
        for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
                tmp = agp_bridge->driver->mask_memory(agp_bridge,
-                       mem->memory[i], mask_type);
+                                                     page_to_phys(mem->pages[i]),
+                                                     mask_type);
 
                BUG_ON(tmp & 0xffffff0000000ffcULL);
                pte = (tmp & 0x000000ff00000000ULL) >> 28;
@@ -165,29 +148,18 @@ static int amd64_fetch_size(void)
  * In a multiprocessor x86-64 system, this function gets
  * called once for each CPU.
  */
-static u64 amd64_configure (struct pci_dev *hammer, u64 gatt_table)
+static u64 amd64_configure(struct pci_dev *hammer, u64 gatt_table)
 {
        u64 aperturebase;
        u32 tmp;
-       u64 addr, aper_base;
+       u64 aper_base;
 
        /* Address to map to */
-       pci_read_config_dword (hammer, AMD64_GARTAPERTUREBASE, &tmp);
+       pci_read_config_dword(hammer, AMD64_GARTAPERTUREBASE, &tmp);
        aperturebase = tmp << 25;
        aper_base = (aperturebase & PCI_BASE_ADDRESS_MEM_MASK);
 
-       /* address of the mappings table */
-       addr = (u64) gatt_table;
-       addr >>= 12;
-       tmp = (u32) addr<<4;
-       tmp &= ~0xf;
-       pci_write_config_dword (hammer, AMD64_GARTTABLEBASE, tmp);
-
-       /* Enable GART translation for this hammer. */
-       pci_read_config_dword(hammer, AMD64_GARTAPERTURECTL, &tmp);
-       tmp |= GARTEN;
-       tmp &= ~(DISGARTCPU | DISGARTIO);
-       pci_write_config_dword(hammer, AMD64_GARTAPERTURECTL, tmp);
+       enable_gart_translation(hammer, gatt_table);
 
        return aper_base;
 }
@@ -206,7 +178,7 @@ static const struct aper_size_info_32 amd_8151_sizes[7] =
 
 static int amd_8151_configure(void)
 {
-       unsigned long gatt_bus = virt_to_gart(agp_bridge->gatt_table_real);
+       unsigned long gatt_bus = virt_to_phys(agp_bridge->gatt_table_real);
        int i;
 
        /* Configure AGP regs in each x86-64 host bridge. */
@@ -226,9 +198,9 @@ static void amd64_cleanup(void)
         for (i = 0; i < num_k8_northbridges; i++) {
                struct pci_dev *dev = k8_northbridges[i];
                /* disable gart translation */
-               pci_read_config_dword (dev, AMD64_GARTAPERTURECTL, &tmp);
+               pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &tmp);
                tmp &= ~AMD64_GARTEN;
-               pci_write_config_dword (dev, AMD64_GARTAPERTURECTL, tmp);
+               pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, tmp);
        }
 }
 
@@ -238,6 +210,7 @@ static const struct agp_bridge_driver amd_8151_driver = {
        .aperture_sizes         = amd_8151_sizes,
        .size_type              = U32_APER_SIZE,
        .num_aperture_sizes     = 7,
+       .needs_scratch_page     = true,
        .configure              = amd_8151_configure,
        .fetch_size             = amd64_fetch_size,
        .cleanup                = amd64_cleanup,
@@ -253,29 +226,17 @@ static const struct agp_bridge_driver amd_8151_driver = {
        .alloc_by_type          = agp_generic_alloc_by_type,
        .free_by_type           = agp_generic_free_by_type,
        .agp_alloc_page         = agp_generic_alloc_page,
+       .agp_alloc_pages        = agp_generic_alloc_pages,
        .agp_destroy_page       = agp_generic_destroy_page,
+       .agp_destroy_pages      = agp_generic_destroy_pages,
        .agp_type_to_mask_type  = agp_generic_type_to_mask_type,
 };
 
 /* Some basic sanity checks for the aperture. */
-static int __devinit aperture_valid(u64 aper, u32 size)
+static int __devinit agp_aperture_valid(u64 aper, u32 size)
 {
-       if (aper == 0) {
-               printk(KERN_ERR PFX "No aperture\n");
-               return 0;
-       }
-       if (size < 32*1024*1024) {
-               printk(KERN_ERR PFX "Aperture too small (%d MB)\n", size>>20);
+       if (!aperture_valid(aper, size, 32*1024*1024))
                return 0;
-       }
-       if ((u64)aper + size > 0x100000000ULL) {
-               printk(KERN_ERR PFX "Aperture out of bounds\n");
-               return 0;
-       }
-       if (e820_any_mapped(aper, aper + size, E820_RAM)) {
-               printk(KERN_ERR PFX "Aperture pointing to RAM\n");
-               return 0;
-       }
 
        /* Request the Aperture. This catches cases when someone else
           already put a mapping in there - happens with some very broken BIOS
@@ -308,19 +269,19 @@ static __devinit int fix_northbridge(struct pci_dev *nb, struct pci_dev *agp,
        u32 nb_order, nb_base;
        u16 apsize;
 
-       pci_read_config_dword(nb, 0x90, &nb_order);
+       pci_read_config_dword(nb, AMD64_GARTAPERTURECTL, &nb_order);
        nb_order = (nb_order >> 1) & 7;
-       pci_read_config_dword(nb, 0x94, &nb_base);
+       pci_read_config_dword(nb, AMD64_GARTAPERTUREBASE, &nb_base);
        nb_aper = nb_base << 25;
-       if (aperture_valid(nb_aper, (32*1024*1024)<<nb_order)) {
-               return 0;
-       }
 
        /* Northbridge seems to contain crap. Try the AGP bridge. */
 
        pci_read_config_word(agp, cap+0x14, &apsize);
-       if (apsize == 0xffff)
+       if (apsize == 0xffff) {
+               if (agp_aperture_valid(nb_aper, (32*1024*1024)<<nb_order))
+                       return 0;
                return -1;
+       }
 
        apsize &= 0xfff;
        /* Some BIOS use weird encodings not in the AGPv3 table. */
@@ -331,12 +292,29 @@ static __devinit int fix_northbridge(struct pci_dev *nb, struct pci_dev *agp,
        pci_read_config_dword(agp, 0x10, &aper_low);
        pci_read_config_dword(agp, 0x14, &aper_hi);
        aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
-       printk(KERN_INFO PFX "Aperture from AGP @ %Lx size %u MB\n", aper, 32 << order);
-       if (order < 0 || !aperture_valid(aper, (32*1024*1024)<<order))
+
+       /*
+        * On some sick chips APSIZE is 0. This means it wants 4G
+        * so let double check that order, and lets trust the AMD NB settings
+        */
+       if (order >=0 && aper + (32ULL<<(20 + order)) > 0x100000000ULL) {
+               dev_info(&agp->dev, "aperture size %u MB is not right, using settings from NB\n",
+                        32 << order);
+               order = nb_order;
+       }
+
+       if (nb_order >= order) {
+               if (agp_aperture_valid(nb_aper, (32*1024*1024)<<nb_order))
+                       return 0;
+       }
+
+       dev_info(&agp->dev, "aperture from AGP @ %Lx size %u MB\n",
+                aper, 32 << order);
+       if (order < 0 || !agp_aperture_valid(aper, (32*1024*1024)<<order))
                return -1;
 
-       pci_write_config_dword(nb, 0x90, order << 1);
-       pci_write_config_dword(nb, 0x94, aper >> 25);
+       pci_write_config_dword(nb, AMD64_GARTAPERTURECTL, order << 1);
+       pci_write_config_dword(nb, AMD64_GARTAPERTUREBASE, aper >> 25);
 
        return 0;
 }
@@ -352,10 +330,10 @@ static __devinit int cache_nbs (struct pci_dev *pdev, u32 cap_ptr)
        for (i = 0; i < num_k8_northbridges; i++) {
                struct pci_dev *dev = k8_northbridges[i];
                if (fix_northbridge(dev, pdev, cap_ptr) < 0) {
-                       printk(KERN_ERR PFX "No usable aperture found.\n");
+                       dev_err(&dev->dev, "no usable aperture found\n");
 #ifdef __x86_64__
                        /* should port this to i386 */
-                       printk(KERN_ERR PFX "Consider rebooting with iommu=memaper=2 to get a good aperture.\n");
+                       dev_err(&dev->dev, "consider rebooting with iommu=memaper=2 to get a good aperture\n");
 #endif
                        return -1;
                }
@@ -378,14 +356,14 @@ static void __devinit amd8151_init(struct pci_dev *pdev, struct agp_bridge_data
        default:   revstring="??"; break;
        }
 
-       printk (KERN_INFO PFX "Detected AMD 8151 AGP Bridge rev %s\n", revstring);
+       dev_info(&pdev->dev, "AMD 8151 AGP Bridge rev %s\n", revstring);
 
        /*
         * Work around errata.
         * Chips before B2 stepping incorrectly reporting v3.5
         */
        if (pdev->revision < 0x13) {
-               printk (KERN_INFO PFX "Correcting AGP revision (reports 3.5, is really 3.0)\n");
+               dev_info(&pdev->dev, "correcting AGP revision (reports 3.5, is really 3.0)\n");
                bridge->major_version = 3;
                bridge->minor_version = 0;
        }
@@ -406,13 +384,13 @@ static int __devinit uli_agp_init(struct pci_dev *pdev)
 {
        u32 httfea,baseaddr,enuscr;
        struct pci_dev *dev1;
-       int i;
+       int i, ret;
        unsigned size = amd64_fetch_size();
-       printk(KERN_INFO "Setting up ULi AGP.\n");
+
+       dev_info(&pdev->dev, "setting up ULi AGP\n");
        dev1 = pci_get_slot (pdev->bus,PCI_DEVFN(0,0));
        if (dev1 == NULL) {
-               printk(KERN_INFO PFX "Detected a ULi chipset, "
-                       "but could not fine the secondary device.\n");
+               dev_info(&pdev->dev, "can't find ULi secondary device\n");
                return -ENODEV;
        }
 
@@ -421,16 +399,19 @@ static int __devinit uli_agp_init(struct pci_dev *pdev)
                        break;
 
        if (i == ARRAY_SIZE(uli_sizes)) {
-               printk(KERN_INFO PFX "No ULi size found for %d\n", size);
-               return -ENODEV;
+               dev_info(&pdev->dev, "no ULi size found for %d\n", size);
+               ret = -ENODEV;
+               goto put;
        }
 
        /* shadow x86-64 registers into ULi registers */
        pci_read_config_dword (k8_northbridges[0], AMD64_GARTAPERTUREBASE, &httfea);
 
        /* if x86-64 aperture base is beyond 4G, exit here */
-       if ((httfea & 0x7fff) >> (32 - 25))
-               return -ENODEV;
+       if ((httfea & 0x7fff) >> (32 - 25)) {
+               ret = -ENODEV;
+               goto put;
+       }
 
        httfea = (httfea& 0x7fff) << 25;
 
@@ -442,9 +423,10 @@ static int __devinit uli_agp_init(struct pci_dev *pdev)
        enuscr= httfea+ (size * 1024 * 1024) - 1;
        pci_write_config_dword(dev1, ULI_X86_64_HTT_FEA_REG, httfea);
        pci_write_config_dword(dev1, ULI_X86_64_ENU_SCR_REG, enuscr);
-
+       ret = 0;
+put:
        pci_dev_put(dev1);
-       return 0;
+       return ret;
 }
 
 
@@ -463,16 +445,14 @@ static int nforce3_agp_init(struct pci_dev *pdev)
 {
        u32 tmp, apbase, apbar, aplimit;
        struct pci_dev *dev1;
-       int i;
+       int i, ret;
        unsigned size = amd64_fetch_size();
 
-       printk(KERN_INFO PFX "Setting up Nforce3 AGP.\n");
+       dev_info(&pdev->dev, "setting up Nforce3 AGP\n");
 
        dev1 = pci_get_slot(pdev->bus, PCI_DEVFN(11, 0));
        if (dev1 == NULL) {
-               printk(KERN_INFO PFX "agpgart: Detected an NVIDIA "
-                       "nForce3 chipset, but could not find "
-                       "the secondary device.\n");
+               dev_info(&pdev->dev, "can't find Nforce3 secondary device\n");
                return -ENODEV;
        }
 
@@ -481,8 +461,9 @@ static int nforce3_agp_init(struct pci_dev *pdev)
                        break;
 
        if (i == ARRAY_SIZE(nforce3_sizes)) {
-               printk(KERN_INFO PFX "No NForce3 size found for %d\n", size);
-               return -ENODEV;
+               dev_info(&pdev->dev, "no NForce3 size found for %d\n", size);
+               ret = -ENODEV;
+               goto put;
        }
 
        pci_read_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, &tmp);
@@ -495,8 +476,9 @@ static int nforce3_agp_init(struct pci_dev *pdev)
 
        /* if x86-64 aperture base is beyond 4G, exit here */
        if ( (apbase & 0x7fff) >> (32 - 25) ) {
-               printk(KERN_INFO PFX "aperture base > 4G\n");
-               return -ENODEV;
+               dev_info(&pdev->dev, "aperture base > 4G\n");
+               ret = -ENODEV;
+               goto put;
        }
 
        apbase = (apbase & 0x7fff) << 25;
@@ -512,9 +494,11 @@ static int nforce3_agp_init(struct pci_dev *pdev)
        pci_write_config_dword(dev1, NVIDIA_X86_64_1_APBASE2, apbase);
        pci_write_config_dword(dev1, NVIDIA_X86_64_1_APLIMIT2, aplimit);
 
+       ret = 0;
+put:
        pci_dev_put(dev1);
 
-       return 0;
+       return ret;
 }
 
 static int __devinit agp_amd64_probe(struct pci_dev *pdev,
@@ -522,6 +506,11 @@ static int __devinit agp_amd64_probe(struct pci_dev *pdev,
 {
        struct agp_bridge_data *bridge;
        u8 cap_ptr;
+       int err;
+
+       /* The Highlander principle */
+       if (agp_bridges_found)
+               return -ENODEV;
 
        cap_ptr = pci_find_capability(pdev, PCI_CAP_ID_AGP);
        if (!cap_ptr)
@@ -537,7 +526,8 @@ static int __devinit agp_amd64_probe(struct pci_dev *pdev,
            pdev->device == PCI_DEVICE_ID_AMD_8151_0) {
                amd8151_init(pdev, bridge);
        } else {
-               printk(KERN_INFO PFX "Detected AGP bridge %x\n", pdev->devfn);
+               dev_info(&pdev->dev, "AGP bridge [%04x/%04x]\n",
+                        pdev->vendor, pdev->device);
        }
 
        bridge->driver = &amd_8151_driver;
@@ -569,17 +559,24 @@ static int __devinit agp_amd64_probe(struct pci_dev *pdev,
        }
 
        pci_set_drvdata(pdev, bridge);
-       return agp_add_bridge(bridge);
+       err = agp_add_bridge(bridge);
+       if (err < 0)
+               return err;
+
+       agp_bridges_found++;
+       return 0;
 }
 
 static void __devexit agp_amd64_remove(struct pci_dev *pdev)
 {
        struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
 
-       release_mem_region(virt_to_gart(bridge->gatt_table_real),
+       release_mem_region(virt_to_phys(bridge->gatt_table_real),
                           amd64_aperture_sizes[bridge->aperture_size_idx].size);
        agp_remove_bridge(bridge);
        agp_put_bridge(bridge);
+
+       agp_bridges_found--;
 }
 
 #ifdef CONFIG_PM
@@ -727,6 +724,11 @@ static struct pci_device_id agp_amd64_pci_table[] = {
 
 MODULE_DEVICE_TABLE(pci, agp_amd64_pci_table);
 
+static DEFINE_PCI_DEVICE_TABLE(agp_amd64_pci_promisc_table) = {
+       { PCI_DEVICE_CLASS(0, 0) },
+       { }
+};
+
 static struct pci_driver agp_amd64_pci_driver = {
        .name           = "agpgart-amd64",
        .id_table       = agp_amd64_pci_table,
@@ -746,8 +748,12 @@ int __init agp_amd64_init(void)
 
        if (agp_off)
                return -EINVAL;
-       if (pci_register_driver(&agp_amd64_pci_driver) < 0) {
-               struct pci_dev *dev;
+
+       err = pci_register_driver(&agp_amd64_pci_driver);
+       if (err < 0)
+               return err;
+
+       if (agp_bridges_found == 0) {
                if (!agp_try_unsupported && !agp_try_unsupported_boot) {
                        printk(KERN_INFO PFX "No supported AGP bridge found.\n");
 #ifdef MODULE
@@ -763,35 +769,37 @@ int __init agp_amd64_init(void)
                        return -ENODEV;
 
                /* Look for any AGP bridge */
-               dev = NULL;
-               err = -ENODEV;
-               for_each_pci_dev(dev) {
-                       if (!pci_find_capability(dev, PCI_CAP_ID_AGP))
-                               continue;
-                       /* Only one bridge supported right now */
-                       if (agp_amd64_probe(dev, NULL) == 0) {
-                               err = 0;
-                               break;
-                       }
-               }
+               agp_amd64_pci_driver.id_table = agp_amd64_pci_promisc_table;
+               err = driver_attach(&agp_amd64_pci_driver.driver);
+               if (err == 0 && agp_bridges_found == 0)
+                       err = -ENODEV;
        }
        return err;
 }
 
+static int __init agp_amd64_mod_init(void)
+{
+#ifndef MODULE
+       if (gart_iommu_aperture)
+               return agp_bridges_found ? 0 : -ENODEV;
+#endif
+       return agp_amd64_init();
+}
+
 static void __exit agp_amd64_cleanup(void)
 {
+#ifndef MODULE
+       if (gart_iommu_aperture)
+               return;
+#endif
        if (aperture_resource)
                release_resource(aperture_resource);
        pci_unregister_driver(&agp_amd64_pci_driver);
 }
 
-/* On AMD64 the PCI driver needs to initialize this driver early
-   for the IOMMU, so it has to be called via a backdoor. */
-#ifndef CONFIG_GART_IOMMU
-module_init(agp_amd64_init);
+module_init(agp_amd64_mod_init);
 module_exit(agp_amd64_cleanup);
-#endif
 
-MODULE_AUTHOR("Dave Jones <davej@codemonkey.org.uk>, Andi Kleen");
+MODULE_AUTHOR("Dave Jones <davej@redhat.com>, Andi Kleen");
 module_param(agp_try_unsupported, bool, 0);
 MODULE_LICENSE("GPL");