2 * Support PCI IO workaround
4 * Copyright (C) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>
6 * (C) Copyright 2007-2008 TOSHIBA CORPORATION
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/sched.h> /* for init_mm */
18 #include <asm/machdep.h>
19 #include <asm/pgtable.h>
20 #include <asm/ppc-pci.h>
21 #include <asm/io-workarounds.h>
23 #define IOWA_MAX_BUS 8
25 static struct iowa_bus iowa_busses[IOWA_MAX_BUS];
26 static unsigned int iowa_bus_count;
28 static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr)
32 unsigned long vstart, vend;
34 for (i = 0; i < iowa_bus_count; i++) {
35 struct iowa_bus *bus = &iowa_busses[i];
36 struct pci_controller *phb = bus->phb;
39 vstart = (unsigned long)phb->io_base_virt;
40 vend = vstart + phb->pci_io_size - 1;
41 if ((vaddr >= vstart) && (vaddr <= vend))
46 for (j = 0; j < 3; j++) {
47 res = &phb->mem_resources[j];
48 if (paddr >= res->start && paddr <= res->end)
56 struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
58 unsigned hugepage_shift;
62 token = PCI_GET_ADDR_TOKEN(addr);
64 if (token && token <= iowa_bus_count)
65 bus = &iowa_busses[token - 1];
67 unsigned long vaddr, paddr;
70 vaddr = (unsigned long)PCI_FIX_ADDR(addr);
71 if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
74 ptep = find_linux_pte_or_hugepte(init_mm.pgd, vaddr,
80 * we don't have hugepages backing iomem
82 WARN_ON(hugepage_shift);
83 paddr = pte_pfn(*ptep) << PAGE_SHIFT;
85 bus = iowa_pci_find(vaddr, paddr);
94 struct iowa_bus *iowa_pio_find_bus(unsigned long port)
96 unsigned long vaddr = (unsigned long)pci_io_base + port;
97 return iowa_pci_find(vaddr, 0);
101 #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \
102 static ret iowa_##name at \
104 struct iowa_bus *bus; \
105 bus = iowa_##space##_find_bus(aa); \
106 if (bus && bus->ops && bus->ops->name) \
107 return bus->ops->name al; \
108 return __do_##name al; \
111 #define DEF_PCI_AC_NORET(name, at, al, space, aa) \
112 static void iowa_##name at \
114 struct iowa_bus *bus; \
115 bus = iowa_##space##_find_bus(aa); \
116 if (bus && bus->ops && bus->ops->name) { \
123 #include <asm/io-defs.h>
125 #undef DEF_PCI_AC_RET
126 #undef DEF_PCI_AC_NORET
128 static const struct ppc_pci_io iowa_pci_io = {
130 #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name,
131 #define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name,
133 #include <asm/io-defs.h>
135 #undef DEF_PCI_AC_RET
136 #undef DEF_PCI_AC_NORET
140 static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
141 unsigned long flags, void *caller)
143 struct iowa_bus *bus;
144 void __iomem *res = __ioremap_caller(addr, size, flags, caller);
147 bus = iowa_pci_find(0, (unsigned long)addr);
149 busno = bus - iowa_busses;
150 PCI_SET_ADDR_TOKEN(res, busno + 1);
155 /* Enable IO workaround */
156 static void io_workaround_init(void)
158 static int io_workaround_inited;
160 if (io_workaround_inited)
162 ppc_pci_io = iowa_pci_io;
163 ppc_md.ioremap = iowa_ioremap;
164 io_workaround_inited = 1;
167 /* Register new bus to support workaround */
168 void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops,
169 int (*initfunc)(struct iowa_bus *, void *), void *data)
171 struct iowa_bus *bus;
172 struct device_node *np = phb->dn;
174 io_workaround_init();
176 if (iowa_bus_count >= IOWA_MAX_BUS) {
177 pr_err("IOWA:Too many pci bridges, "
178 "workarounds disabled for %s\n", np->full_name);
182 bus = &iowa_busses[iowa_bus_count];
188 if ((*initfunc)(bus, data))
193 pr_debug("IOWA:[%d]Add bus, %s.\n", iowa_bus_count-1, np->full_name);