Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 1 | /* |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 2 | * Support PCI IO workaround |
| 3 | * |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 4 | * Copyright (C) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org> |
| 5 | * IBM, Corp. |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 6 | * (C) Copyright 2007-2008 TOSHIBA CORPORATION |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 7 | * |
| 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. |
| 11 | */ |
| 12 | #undef DEBUG |
| 13 | |
| 14 | #include <linux/kernel.h> |
Paul Gortmaker | 333a151 | 2011-05-27 13:39:01 -0400 | [diff] [blame] | 15 | #include <linux/sched.h> /* for init_mm */ |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 16 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/machdep.h> |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 19 | #include <asm/pgtable.h> |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 20 | #include <asm/ppc-pci.h> |
Michael Ellerman | 3cc30d0 | 2011-04-11 21:25:01 +0000 | [diff] [blame] | 21 | #include <asm/io-workarounds.h> |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 22 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 23 | #define IOWA_MAX_BUS 8 |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 24 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 25 | static struct iowa_bus iowa_busses[IOWA_MAX_BUS]; |
| 26 | static unsigned int iowa_bus_count; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 27 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 28 | static struct iowa_bus *iowa_pci_find(unsigned long vaddr, unsigned long paddr) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 29 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 30 | int i, j; |
| 31 | struct resource *res; |
| 32 | unsigned long vstart, vend; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 33 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 34 | for (i = 0; i < iowa_bus_count; i++) { |
| 35 | struct iowa_bus *bus = &iowa_busses[i]; |
| 36 | struct pci_controller *phb = bus->phb; |
| 37 | |
| 38 | if (vaddr) { |
| 39 | vstart = (unsigned long)phb->io_base_virt; |
| 40 | vend = vstart + phb->pci_io_size - 1; |
| 41 | if ((vaddr >= vstart) && (vaddr <= vend)) |
| 42 | return bus; |
| 43 | } |
| 44 | |
| 45 | if (paddr) |
| 46 | for (j = 0; j < 3; j++) { |
| 47 | res = &phb->mem_resources[j]; |
| 48 | if (paddr >= res->start && paddr <= res->end) |
| 49 | return bus; |
| 50 | } |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 51 | } |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 52 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 53 | return NULL; |
| 54 | } |
| 55 | |
Benjamin Herrenschmidt | ecd73cc | 2013-07-15 13:03:08 +1000 | [diff] [blame] | 56 | #ifdef CONFIG_PPC_INDIRECT_MMIO |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 57 | struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 58 | { |
Aneesh Kumar K.V | 12bc9f6 | 2013-06-20 14:30:18 +0530 | [diff] [blame] | 59 | unsigned hugepage_shift; |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 60 | struct iowa_bus *bus; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 61 | int token; |
| 62 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 63 | token = PCI_GET_ADDR_TOKEN(addr); |
| 64 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 65 | if (token && token <= iowa_bus_count) |
| 66 | bus = &iowa_busses[token - 1]; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 67 | else { |
| 68 | unsigned long vaddr, paddr; |
| 69 | pte_t *ptep; |
| 70 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 71 | vaddr = (unsigned long)PCI_FIX_ADDR(addr); |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 72 | if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END) |
| 73 | return NULL; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 74 | |
Aneesh Kumar K.V | 12bc9f6 | 2013-06-20 14:30:18 +0530 | [diff] [blame] | 75 | ptep = find_linux_pte_or_hugepte(init_mm.pgd, vaddr, |
| 76 | &hugepage_shift); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 77 | if (ptep == NULL) |
| 78 | paddr = 0; |
Aneesh Kumar K.V | 12bc9f6 | 2013-06-20 14:30:18 +0530 | [diff] [blame] | 79 | else { |
| 80 | /* |
| 81 | * we don't have hugepages backing iomem |
| 82 | */ |
| 83 | WARN_ON(hugepage_shift); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 84 | paddr = pte_pfn(*ptep) << PAGE_SHIFT; |
Aneesh Kumar K.V | 12bc9f6 | 2013-06-20 14:30:18 +0530 | [diff] [blame] | 85 | } |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 86 | bus = iowa_pci_find(vaddr, paddr); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 87 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 88 | if (bus == NULL) |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 89 | return NULL; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 90 | } |
| 91 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 92 | return bus; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 93 | } |
Benjamin Herrenschmidt | ecd73cc | 2013-07-15 13:03:08 +1000 | [diff] [blame] | 94 | #else /* CONFIG_PPC_INDIRECT_MMIO */ |
| 95 | struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr) |
| 96 | { |
| 97 | return NULL; |
| 98 | } |
| 99 | #endif /* !CONFIG_PPC_INDIRECT_MMIO */ |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 100 | |
Benjamin Herrenschmidt | ecd73cc | 2013-07-15 13:03:08 +1000 | [diff] [blame] | 101 | #ifdef CONFIG_PPC_INDIRECT_PIO |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 102 | struct iowa_bus *iowa_pio_find_bus(unsigned long port) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 103 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 104 | unsigned long vaddr = (unsigned long)pci_io_base + port; |
| 105 | return iowa_pci_find(vaddr, 0); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 106 | } |
Benjamin Herrenschmidt | ecd73cc | 2013-07-15 13:03:08 +1000 | [diff] [blame] | 107 | #else |
| 108 | struct iowa_bus *iowa_pio_find_bus(unsigned long port) |
| 109 | { |
| 110 | return NULL; |
| 111 | } |
| 112 | #endif |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 113 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 114 | #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \ |
| 115 | static ret iowa_##name at \ |
| 116 | { \ |
| 117 | struct iowa_bus *bus; \ |
| 118 | bus = iowa_##space##_find_bus(aa); \ |
| 119 | if (bus && bus->ops && bus->ops->name) \ |
| 120 | return bus->ops->name al; \ |
| 121 | return __do_##name al; \ |
| 122 | } |
| 123 | |
| 124 | #define DEF_PCI_AC_NORET(name, at, al, space, aa) \ |
| 125 | static void iowa_##name at \ |
| 126 | { \ |
| 127 | struct iowa_bus *bus; \ |
| 128 | bus = iowa_##space##_find_bus(aa); \ |
| 129 | if (bus && bus->ops && bus->ops->name) { \ |
| 130 | bus->ops->name al; \ |
| 131 | return; \ |
| 132 | } \ |
| 133 | __do_##name al; \ |
| 134 | } |
| 135 | |
| 136 | #include <asm/io-defs.h> |
| 137 | |
| 138 | #undef DEF_PCI_AC_RET |
| 139 | #undef DEF_PCI_AC_NORET |
| 140 | |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 141 | static const struct ppc_pci_io iowa_pci_io = { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 142 | |
| 143 | #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name, |
| 144 | #define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name, |
| 145 | |
| 146 | #include <asm/io-defs.h> |
| 147 | |
| 148 | #undef DEF_PCI_AC_RET |
| 149 | #undef DEF_PCI_AC_NORET |
| 150 | |
| 151 | }; |
| 152 | |
Benjamin Herrenschmidt | ecd73cc | 2013-07-15 13:03:08 +1000 | [diff] [blame] | 153 | #ifdef CONFIG_PPC_INDIRECT_MMIO |
Ingo Molnar | b36ac9e | 2009-01-06 14:03:44 +0000 | [diff] [blame] | 154 | static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size, |
Benjamin Herrenschmidt | 1cdab55 | 2009-02-22 16:19:14 +0000 | [diff] [blame] | 155 | unsigned long flags, void *caller) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 156 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 157 | struct iowa_bus *bus; |
Benjamin Herrenschmidt | 1cdab55 | 2009-02-22 16:19:14 +0000 | [diff] [blame] | 158 | void __iomem *res = __ioremap_caller(addr, size, flags, caller); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 159 | int busno; |
| 160 | |
Ingo Molnar | b36ac9e | 2009-01-06 14:03:44 +0000 | [diff] [blame] | 161 | bus = iowa_pci_find(0, (unsigned long)addr); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 162 | if (bus != NULL) { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 163 | busno = bus - iowa_busses; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 164 | PCI_SET_ADDR_TOKEN(res, busno + 1); |
| 165 | } |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 166 | return res; |
| 167 | } |
Benjamin Herrenschmidt | ecd73cc | 2013-07-15 13:03:08 +1000 | [diff] [blame] | 168 | #else /* CONFIG_PPC_INDIRECT_MMIO */ |
| 169 | #define iowa_ioremap NULL |
| 170 | #endif /* !CONFIG_PPC_INDIRECT_MMIO */ |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 171 | |
Michael Ellerman | d1109b7 | 2011-04-11 21:25:02 +0000 | [diff] [blame] | 172 | /* Enable IO workaround */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 173 | static void io_workaround_init(void) |
Michael Ellerman | d1109b7 | 2011-04-11 21:25:02 +0000 | [diff] [blame] | 174 | { |
| 175 | static int io_workaround_inited; |
| 176 | |
| 177 | if (io_workaround_inited) |
| 178 | return; |
| 179 | ppc_pci_io = iowa_pci_io; |
| 180 | ppc_md.ioremap = iowa_ioremap; |
| 181 | io_workaround_inited = 1; |
| 182 | } |
| 183 | |
| 184 | /* Register new bus to support workaround */ |
Greg Kroah-Hartman | cad5cef | 2012-12-21 14:04:10 -0800 | [diff] [blame] | 185 | void iowa_register_bus(struct pci_controller *phb, struct ppc_pci_io *ops, |
| 186 | int (*initfunc)(struct iowa_bus *, void *), void *data) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 187 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 188 | struct iowa_bus *bus; |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 189 | struct device_node *np = phb->dn; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 190 | |
Michael Ellerman | d1109b7 | 2011-04-11 21:25:02 +0000 | [diff] [blame] | 191 | io_workaround_init(); |
| 192 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 193 | if (iowa_bus_count >= IOWA_MAX_BUS) { |
| 194 | pr_err("IOWA:Too many pci bridges, " |
| 195 | "workarounds disabled for %s\n", np->full_name); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 199 | bus = &iowa_busses[iowa_bus_count]; |
| 200 | bus->phb = phb; |
| 201 | bus->ops = ops; |
Michael Ellerman | 69b1236 | 2011-04-11 21:25:02 +0000 | [diff] [blame] | 202 | bus->private = data; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 203 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 204 | if (initfunc) |
| 205 | if ((*initfunc)(bus, data)) |
| 206 | return; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 207 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 208 | iowa_bus_count++; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 209 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 210 | pr_debug("IOWA:[%d]Add bus, %s.\n", iowa_bus_count-1, np->full_name); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 211 | } |
| 212 | |