include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / arch / powerpc / sysdev / fsl_pci.c
index b20171d..a14760f 100644 (file)
@@ -23,6 +23,9 @@
 #include <linux/string.h>
 #include <linux/init.h>
 #include <linux/bootmem.h>
+#include <linux/lmb.h>
+#include <linux/log2.h>
+#include <linux/slab.h>
 
 #include <asm/io.h>
 #include <asm/prom.h>
@@ -54,7 +57,7 @@ static int __init fsl_pcie_check_link(struct pci_controller *hose)
        return 0;
 }
 
-#if defined(CONFIG_PPC_85xx) || defined(CONFIG_PPC_86xx)
+#if defined(CONFIG_FSL_SOC_BOOKE) || defined(CONFIG_PPC_86xx)
 static int __init setup_one_atmu(struct ccsr_pci __iomem *pci,
        unsigned int index, const struct resource *res,
        resource_size_t offset)
@@ -96,7 +99,13 @@ static void __init setup_pci_atmu(struct pci_controller *hose,
                                  struct resource *rsrc)
 {
        struct ccsr_pci __iomem *pci;
-       int i, j, n;
+       int i, j, n, mem_log, win_idx = 2;
+       u64 mem, sz, paddr_hi = 0;
+       u64 paddr_lo = ULLONG_MAX;
+       u32 pcicsrbar = 0, pcicsrbar_sz;
+       u32 piwar = PIWAR_EN | PIWAR_PF | PIWAR_TGI_LOCAL |
+                       PIWAR_READ_SNOOP | PIWAR_WRITE_SNOOP;
+       char *name = hose->dn->full_name;
 
        pr_debug("PCI memory map start 0x%016llx, size 0x%016llx\n",
                    (u64)rsrc->start, (u64)rsrc->end - (u64)rsrc->start + 1);
@@ -117,6 +126,9 @@ static void __init setup_pci_atmu(struct pci_controller *hose,
                if (!(hose->mem_resources[i].flags & IORESOURCE_MEM))
                        continue;
 
+               paddr_lo = min(paddr_lo, (u64)hose->mem_resources[i].start);
+               paddr_hi = max(paddr_hi, (u64)hose->mem_resources[i].end);
+
                n = setup_one_atmu(pci, j, &hose->mem_resources[i],
                                   hose->pci_mem_offset);
 
@@ -147,14 +159,105 @@ static void __init setup_pci_atmu(struct pci_controller *hose,
                }
        }
 
-       /* Setup 2G inbound Memory Window @ 1 */
-       out_be32(&pci->piw[2].pitar, 0x00000000);
-       out_be32(&pci->piw[2].piwbar,0x00000000);
-       out_be32(&pci->piw[2].piwar, PIWAR_2G);
+       /* convert to pci address space */
+       paddr_hi -= hose->pci_mem_offset;
+       paddr_lo -= hose->pci_mem_offset;
+
+       if (paddr_hi == paddr_lo) {
+               pr_err("%s: No outbound window space\n", name);
+               return ;
+       }
+
+       if (paddr_lo == 0) {
+               pr_err("%s: No space for inbound window\n", name);
+               return ;
+       }
+
+       /* setup PCSRBAR/PEXCSRBAR */
+       early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, 0xffffffff);
+       early_read_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, &pcicsrbar_sz);
+       pcicsrbar_sz = ~pcicsrbar_sz + 1;
+
+       if (paddr_hi < (0x100000000ull - pcicsrbar_sz) ||
+               (paddr_lo > 0x100000000ull))
+               pcicsrbar = 0x100000000ull - pcicsrbar_sz;
+       else
+               pcicsrbar = (paddr_lo - pcicsrbar_sz) & -pcicsrbar_sz;
+       early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, pcicsrbar);
+
+       paddr_lo = min(paddr_lo, (u64)pcicsrbar);
+
+       pr_info("%s: PCICSRBAR @ 0x%x\n", name, pcicsrbar);
+
+       /* Setup inbound mem window */
+       mem = lmb_end_of_DRAM();
+       sz = min(mem, paddr_lo);
+       mem_log = __ilog2_u64(sz);
+
+       /* PCIe can overmap inbound & outbound since RX & TX are separated */
+       if (early_find_capability(hose, 0, 0, PCI_CAP_ID_EXP)) {
+               /* Size window to exact size if power-of-two or one size up */
+               if ((1ull << mem_log) != mem) {
+                       if ((1ull << mem_log) > mem)
+                               pr_info("%s: Setting PCI inbound window "
+                                       "greater than memory size\n", name);
+                       mem_log++;
+               }
+
+               piwar |= (mem_log - 1);
+
+               /* Setup inbound memory window */
+               out_be32(&pci->piw[win_idx].pitar,  0x00000000);
+               out_be32(&pci->piw[win_idx].piwbar, 0x00000000);
+               out_be32(&pci->piw[win_idx].piwar,  piwar);
+               win_idx--;
+
+               hose->dma_window_base_cur = 0x00000000;
+               hose->dma_window_size = (resource_size_t)sz;
+       } else {
+               u64 paddr = 0;
+
+               /* Setup inbound memory window */
+               out_be32(&pci->piw[win_idx].pitar,  paddr >> 12);
+               out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
+               out_be32(&pci->piw[win_idx].piwar,  (piwar | (mem_log - 1)));
+               win_idx--;
+
+               paddr += 1ull << mem_log;
+               sz -= 1ull << mem_log;
 
-       /* Save the base address and size covered by inbound window mappings */
-       hose->dma_window_base_cur = 0x00000000;
-       hose->dma_window_size = 0x80000000;
+               if (sz) {
+                       mem_log = __ilog2_u64(sz);
+                       piwar |= (mem_log - 1);
+
+                       out_be32(&pci->piw[win_idx].pitar,  paddr >> 12);
+                       out_be32(&pci->piw[win_idx].piwbar, paddr >> 12);
+                       out_be32(&pci->piw[win_idx].piwar,  piwar);
+                       win_idx--;
+
+                       paddr += 1ull << mem_log;
+               }
+
+               hose->dma_window_base_cur = 0x00000000;
+               hose->dma_window_size = (resource_size_t)paddr;
+       }
+
+       if (hose->dma_window_size < mem) {
+#ifndef CONFIG_SWIOTLB
+               pr_err("%s: ERROR: Memory size exceeds PCI ATMU ability to "
+                       "map - enable CONFIG_SWIOTLB to avoid dma errors.\n",
+                        name);
+#endif
+               /* adjusting outbound windows could reclaim space in mem map */
+               if (paddr_hi < 0xffffffffull)
+                       pr_warning("%s: WARNING: Outbound window cfg leaves "
+                               "gaps in memory map. Adjusting the memory map "
+                               "could reduce unnecessary bounce buffering.\n",
+                               name);
+
+               pr_info("%s: DMA window size is 0x%llx\n", name,
+                       (u64)hose->dma_window_size);
+       }
 
        iounmap(pci);
 }
@@ -180,19 +283,9 @@ static void __init setup_pci_cmd(struct pci_controller *hose)
        }
 }
 
-static void __init setup_pci_pcsrbar(struct pci_controller *hose)
-{
-#ifdef CONFIG_PCI_MSI
-       phys_addr_t immr_base;
-
-       immr_base = get_immrbase();
-       early_write_config_dword(hose, 0, 0, PCI_BASE_ADDRESS_0, immr_base);
-#endif
-}
-
 void fsl_pcibios_fixup_bus(struct pci_bus *bus)
 {
-       struct pci_controller *hose = (struct pci_controller *) bus->sysdata;
+       struct pci_controller *hose = pci_bus_to_host(bus);
        int i;
 
        if ((bus->parent == hose->bus) &&
@@ -273,8 +366,6 @@ int __init fsl_add_bridge(struct device_node *dev, int is_primary)
        /* Setup PEX window registers */
        setup_pci_atmu(hose, &rsrc);
 
-       /* Setup PEXCSRBAR */
-       setup_pci_pcsrbar(hose);
        return 0;
 }
 
@@ -302,9 +393,23 @@ DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8536, quirk_fsl_pcie_header);
 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641, quirk_fsl_pcie_header);
 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8641D, quirk_fsl_pcie_header);
 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8610, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1011E, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1011, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1013E, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1013, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1020E, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1020, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1022E, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P1022, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2010E, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2010, quirk_fsl_pcie_header);
 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020E, quirk_fsl_pcie_header);
 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P2020, quirk_fsl_pcie_header);
-#endif /* CONFIG_PPC_85xx || CONFIG_PPC_86xx */
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4040E, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4040, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4080E, quirk_fsl_pcie_header);
+DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_P4080, quirk_fsl_pcie_header);
+#endif /* CONFIG_FSL_SOC_BOOKE || CONFIG_PPC_86xx */
 
 #if defined(CONFIG_PPC_83xx) || defined(CONFIG_PPC_MPC512x)
 DECLARE_PCI_FIXUP_HEADER(0x1957, PCI_DEVICE_ID_MPC8314E, quirk_fsl_pcie_header);
@@ -332,7 +437,7 @@ struct mpc83xx_pcie_priv {
 
 static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
 {
-       struct pci_controller *hose = bus->sysdata;
+       struct pci_controller *hose = pci_bus_to_host(bus);
 
        if (hose->indirect_type & PPC_INDIRECT_TYPE_NO_PCIE_LINK)
                return PCIBIOS_DEVICE_NOT_FOUND;
@@ -358,10 +463,9 @@ static int mpc83xx_pcie_exclude_device(struct pci_bus *bus, unsigned int devfn)
 static void __iomem *mpc83xx_pcie_remap_cfg(struct pci_bus *bus,
                                            unsigned int devfn, int offset)
 {
-       struct pci_controller *hose = bus->sysdata;
+       struct pci_controller *hose = pci_bus_to_host(bus);
        struct mpc83xx_pcie_priv *pcie = hose->dn->data;
-       u8 bus_no = bus->number - hose->first_busno;
-       u32 dev_base = bus_no << 24 | devfn << 16;
+       u32 dev_base = bus->number << 24 | devfn << 16;
        int ret;
 
        ret = mpc83xx_pcie_exclude_device(bus, devfn);
@@ -411,12 +515,17 @@ static int mpc83xx_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
 static int mpc83xx_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
                                     int offset, int len, u32 val)
 {
+       struct pci_controller *hose = pci_bus_to_host(bus);
        void __iomem *cfg_addr;
 
        cfg_addr = mpc83xx_pcie_remap_cfg(bus, devfn, offset);
        if (!cfg_addr)
                return PCIBIOS_DEVICE_NOT_FOUND;
 
+       /* PPC_INDIRECT_TYPE_SURPRESS_PRIMARY_BUS */
+       if (offset == PCI_PRIMARY_BUS && bus->number == hose->first_busno)
+               val &= 0xffffff00;
+
        switch (len) {
        case 1:
                out_8(cfg_addr, val);