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> |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 15 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | #include <asm/machdep.h> |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 18 | #include <asm/pgtable.h> |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 19 | #include <asm/ppc-pci.h> |
| 20 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 21 | #include "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 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 56 | struct iowa_bus *iowa_mem_find_bus(const PCI_IO_ADDR addr) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 57 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 58 | struct iowa_bus *bus; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 59 | int token; |
| 60 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 61 | token = PCI_GET_ADDR_TOKEN(addr); |
| 62 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 63 | if (token && token <= iowa_bus_count) |
| 64 | bus = &iowa_busses[token - 1]; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 65 | else { |
| 66 | unsigned long vaddr, paddr; |
| 67 | pte_t *ptep; |
| 68 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 69 | vaddr = (unsigned long)PCI_FIX_ADDR(addr); |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 70 | if (vaddr < PHB_IO_BASE || vaddr >= PHB_IO_END) |
| 71 | return NULL; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 72 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 73 | ptep = find_linux_pte(init_mm.pgd, vaddr); |
| 74 | if (ptep == NULL) |
| 75 | paddr = 0; |
| 76 | else |
| 77 | paddr = pte_pfn(*ptep) << PAGE_SHIFT; |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 78 | bus = iowa_pci_find(vaddr, paddr); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 79 | |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 80 | if (bus == NULL) |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 81 | return NULL; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 82 | } |
| 83 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 84 | return bus; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 85 | } |
| 86 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 87 | struct iowa_bus *iowa_pio_find_bus(unsigned long port) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 88 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 89 | unsigned long vaddr = (unsigned long)pci_io_base + port; |
| 90 | return iowa_pci_find(vaddr, 0); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 94 | #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \ |
| 95 | static ret iowa_##name at \ |
| 96 | { \ |
| 97 | struct iowa_bus *bus; \ |
| 98 | bus = iowa_##space##_find_bus(aa); \ |
| 99 | if (bus && bus->ops && bus->ops->name) \ |
| 100 | return bus->ops->name al; \ |
| 101 | return __do_##name al; \ |
| 102 | } |
| 103 | |
| 104 | #define DEF_PCI_AC_NORET(name, at, al, space, aa) \ |
| 105 | static void iowa_##name at \ |
| 106 | { \ |
| 107 | struct iowa_bus *bus; \ |
| 108 | bus = iowa_##space##_find_bus(aa); \ |
| 109 | if (bus && bus->ops && bus->ops->name) { \ |
| 110 | bus->ops->name al; \ |
| 111 | return; \ |
| 112 | } \ |
| 113 | __do_##name al; \ |
| 114 | } |
| 115 | |
| 116 | #include <asm/io-defs.h> |
| 117 | |
| 118 | #undef DEF_PCI_AC_RET |
| 119 | #undef DEF_PCI_AC_NORET |
| 120 | |
Ishizaki Kou | 7012255 | 2008-05-14 19:05:19 +1000 | [diff] [blame] | 121 | static const struct ppc_pci_io __devinitconst iowa_pci_io = { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 122 | |
| 123 | #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) .name = iowa_##name, |
| 124 | #define DEF_PCI_AC_NORET(name, at, al, space, aa) .name = iowa_##name, |
| 125 | |
| 126 | #include <asm/io-defs.h> |
| 127 | |
| 128 | #undef DEF_PCI_AC_RET |
| 129 | #undef DEF_PCI_AC_NORET |
| 130 | |
| 131 | }; |
| 132 | |
Ingo Molnar | b36ac9e | 2009-01-06 14:03:44 +0000 | [diff] [blame] | 133 | static void __iomem *iowa_ioremap(phys_addr_t addr, unsigned long size, |
Benjamin Herrenschmidt | 1cdab55 | 2009-02-22 16:19:14 +0000 | [diff] [blame] | 134 | unsigned long flags, void *caller) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 135 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 136 | struct iowa_bus *bus; |
Benjamin Herrenschmidt | 1cdab55 | 2009-02-22 16:19:14 +0000 | [diff] [blame] | 137 | void __iomem *res = __ioremap_caller(addr, size, flags, caller); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 138 | int busno; |
| 139 | |
Ingo Molnar | b36ac9e | 2009-01-06 14:03:44 +0000 | [diff] [blame] | 140 | bus = iowa_pci_find(0, (unsigned long)addr); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 141 | if (bus != NULL) { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 142 | busno = bus - iowa_busses; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 143 | PCI_SET_ADDR_TOKEN(res, busno + 1); |
| 144 | } |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 145 | return res; |
| 146 | } |
| 147 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 148 | /* Regist new bus to support workaround */ |
Ishizaki Kou | 7012255 | 2008-05-14 19:05:19 +1000 | [diff] [blame] | 149 | void __devinit iowa_register_bus(struct pci_controller *phb, |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 150 | struct ppc_pci_io *ops, |
| 151 | int (*initfunc)(struct iowa_bus *, void *), void *data) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 152 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 153 | struct iowa_bus *bus; |
Stephen Rothwell | 44ef339 | 2007-12-10 14:33:21 +1100 | [diff] [blame] | 154 | struct device_node *np = phb->dn; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 155 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 156 | if (iowa_bus_count >= IOWA_MAX_BUS) { |
| 157 | pr_err("IOWA:Too many pci bridges, " |
| 158 | "workarounds disabled for %s\n", np->full_name); |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 162 | bus = &iowa_busses[iowa_bus_count]; |
| 163 | bus->phb = phb; |
| 164 | bus->ops = ops; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 165 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 166 | if (initfunc) |
| 167 | if ((*initfunc)(bus, data)) |
| 168 | return; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 169 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 170 | iowa_bus_count++; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 171 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 172 | 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] | 173 | } |
| 174 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 175 | /* enable IO workaround */ |
Ishizaki Kou | 7012255 | 2008-05-14 19:05:19 +1000 | [diff] [blame] | 176 | void __devinit io_workaround_init(void) |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 177 | { |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 178 | static int io_workaround_inited; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 179 | |
Ishizaki Kou | 7cfb62a | 2008-04-24 19:21:10 +1000 | [diff] [blame] | 180 | if (io_workaround_inited) |
| 181 | return; |
| 182 | ppc_pci_io = iowa_pci_io; |
| 183 | ppc_md.ioremap = iowa_ioremap; |
| 184 | io_workaround_inited = 1; |
Benjamin Herrenschmidt | 014da7f | 2006-11-11 17:25:12 +1100 | [diff] [blame] | 185 | } |