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/mm.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>
22 #include <asm/pte-walk.h>
25 #define IOWA_MAX_BUS 8
27 static struct iowa_bus iowa_busses[IOWA_MAX_BUS];
28 static unsigned int iowa_bus_count;
30 static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr)
34 unsigned long vstart, vend;
36 for (i = 0; i < iowa_bus_count; i++) {
37 struct iowa_bus *bus = &iowa_busses[i];
38 struct pci_controller *phb = bus->phb;
41 vstart = (unsigned long)phb->io_base_virt;
42 vend = vstart + phb->pci_io_size - 1;
43 if ((vaddr >= vstart) && (vaddr <= vend))
48 for (j = 0; j < 3; j++) {
49 res = &phb->mem_resources[j];
50 if (paddr >= res->start && paddr <= res->end)
58 #ifdef CONFIG_PPC_INDIRECT_MMIO
59 struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
61 unsigned hugepage_shift;
65 token = PCI_GET_ADDR_TOKEN(addr);
67 if (token && token <= iowa_bus_count)
68 bus = &iowa_busses[token - 1];
70 unsigned long vaddr, paddr;
73 vaddr = (unsigned long)PCI_FIX_ADDR(addr);
74 if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END)
77 * We won't find huge pages here (iomem). Also can't hit
78 * a page table free due to init_mm
80 ptep = find_init_mm_pte(vaddr, &hugepage_shift);
84 WARN_ON(hugepage_shift);
85 paddr = pte_pfn(*ptep) << PAGE_SHIFT;
87 bus = iowa_pci_find(vaddr, paddr);
95 #else /* CONFIG_PPC_INDIRECT_MMIO */
96 struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr)
100 #endif /* !CONFIG_PPC_INDIRECT_MMIO */
102 #ifdef CONFIG_PPC_INDIRECT_PIO
103 struct iowa_bus *iowa_pio_find_bus(unsigned long port)
105 unsigned long vaddr = (unsigned long)pci_io_base + port;
106 return iowa_pci_find(vaddr, 0);
109 struct iowa_bus *iowa_pio_find_bus(unsigned long port)
115 #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \
116 static ret iowa_##name at \
118 struct iowa_bus *bus; \
119 bus = iowa_##space##_find_bus(aa); \
120 if (bus && bus->ops && bus->ops->name) \
121 return bus->ops->name al; \
122 return __do_##name al; \
125 #define DEF_PCI_AC_NORET(name, at, al, space, aa) \
126 static void iowa_##name at \
128 struct iowa_bus *bus; \
129 bus = iowa_##space##_find_bus(aa); \
130 if (bus && bus->ops && bus->ops->name) { \
137 #include <asm/io-defs.h>
139 #undef DEF_PCI_AC_RET
140 #undef DEF_PCI_AC_NORET
142 static const struct ppc_pci_io iowa_pci_io = {
144 #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name,
145 #define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name,
147 #include <asm/io-defs.h>
149 #undef DEF_PCI_AC_RET
150 #undef DEF_PCI_AC_NORET
154 #ifdef CONFIG_PPC_INDIRECT_MMIO
155 static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size,
156 unsigned long flags, void *caller)
158 struct iowa_bus *bus;
159 void __iomem *res = __ioremap_caller(addr, size, flags, caller);
162 bus = iowa_pci_find(0, (unsigned long)addr);
164 busno = bus - iowa_busses;
165 PCI_SET_ADDR_TOKEN(res, busno + 1);
169 #else /* CONFIG_PPC_INDIRECT_MMIO */
170 #define iowa_ioremap NULL
171 #endif /* !CONFIG_PPC_INDIRECT_MMIO */
173 /* Enable IO workaround */
174 static void io_workaround_init(void)
176 static int io_workaround_inited;
178 if (io_workaround_inited)
180 ppc_pci_io = iowa_pci_io;
181 ppc_md.ioremap = iowa_ioremap;
182 io_workaround_inited = 1;
185 /* Register new bus to support workaround */
186 void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops,
187 int (*initfunc)(struct iowa_bus *, void *), void *data)
189 struct iowa_bus *bus;
190 struct device_node *np = phb->dn;
192 io_workaround_init();
194 if (iowa_bus_count >= IOWA_MAX_BUS) {
195 pr_err("IOWA:Too many pci bridges, "
196 "workarounds disabled for %pOF\n", np);
200 bus = &iowa_busses[iowa_bus_count];
206 if ((*initfunc)(bus, data))
211 pr_debug("IOWA:[%d]Add bus, %pOF.\n", iowa_bus_count-1, np);