1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Routines for tracking a legacy ISA bridge 4 * 5 * Copyrigh 2007 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp. 6 * 7 * Some bits and pieces moved over from pci_64.c 8 * 9 * Copyrigh 2003 Anton Blanchard <anton@au.ibm.com>, IBM Corp. 10 */ 11 12 #define DEBUG 13 14 #include <linux/kernel.h> 15 #include <linux/pci.h> 16 #include <linux/string.h> 17 #include <linux/export.h> 18 #include <linux/init.h> 19 #include <linux/mm.h> 20 #include <linux/notifier.h> 21 #include <linux/of_address.h> 22 #include <linux/vmalloc.h> 23 24 #include <asm/processor.h> 25 #include <asm/io.h> 26 #include <asm/pci-bridge.h> 27 #include <asm/machdep.h> 28 #include <asm/ppc-pci.h> 29 #include <asm/isa-bridge.h> 30 31 unsigned long isa_io_base; /* NULL if no ISA bus */ 32 EXPORT_SYMBOL(isa_io_base); 33 34 /* Cached ISA bridge dev. */ 35 static struct device_node *isa_bridge_devnode; 36 struct pci_dev *isa_bridge_pcidev; 37 EXPORT_SYMBOL_GPL(isa_bridge_pcidev); 38 39 #define ISA_SPACE_MASK 0x1 40 #define ISA_SPACE_IO 0x1 41 42 static void remap_isa_base(phys_addr_t pa, unsigned long size) 43 { 44 WARN_ON_ONCE(ISA_IO_BASE & ~PAGE_MASK); 45 WARN_ON_ONCE(pa & ~PAGE_MASK); 46 WARN_ON_ONCE(size & ~PAGE_MASK); 47 48 if (slab_is_available()) { 49 if (ioremap_page_range(ISA_IO_BASE, ISA_IO_BASE + size, pa, 50 pgprot_noncached(PAGE_KERNEL))) 51 vunmap_range(ISA_IO_BASE, ISA_IO_BASE + size); 52 } else { 53 early_ioremap_range(ISA_IO_BASE, pa, size, 54 pgprot_noncached(PAGE_KERNEL)); 55 } 56 } 57 58 static int process_ISA_OF_ranges(struct device_node *isa_node, 59 unsigned long phb_io_base_phys) 60 { 61 unsigned int size; 62 struct of_range_parser parser; 63 struct of_range range; 64 65 if (of_range_parser_init(&parser, isa_node)) 66 goto inval_range; 67 68 for_each_of_range(&parser, &range) { 69 if ((range.flags & ISA_SPACE_MASK) != ISA_SPACE_IO) 70 continue; 71 72 if (range.cpu_addr == OF_BAD_ADDR) { 73 pr_err("ISA: Bad CPU mapping: %s\n", __func__); 74 return -EINVAL; 75 } 76 77 /* We need page alignment */ 78 if ((range.bus_addr & ~PAGE_MASK) || (range.cpu_addr & ~PAGE_MASK)) { 79 pr_warn("ISA: bridge %pOF has non aligned IO range\n", isa_node); 80 return -EINVAL; 81 } 82 83 /* Align size and make sure it's cropped to 64K */ 84 size = PAGE_ALIGN(range.size); 85 if (size > 0x10000) 86 size = 0x10000; 87 88 if (!phb_io_base_phys) 89 phb_io_base_phys = range.cpu_addr; 90 91 remap_isa_base(phb_io_base_phys, size); 92 return 0; 93 } 94 95 inval_range: 96 if (!phb_io_base_phys) { 97 pr_err("no ISA IO ranges or unexpected isa range, mapping 64k\n"); 98 remap_isa_base(phb_io_base_phys, 0x10000); 99 } 100 return 0; 101 } 102 103 104 /** 105 * isa_bridge_find_early - Find and map the ISA IO space early before 106 * main PCI discovery. This is optionally called by 107 * the arch code when adding PCI PHBs to get early 108 * access to ISA IO ports 109 */ 110 void __init isa_bridge_find_early(struct pci_controller *hose) 111 { 112 struct device_node *np, *parent = NULL, *tmp; 113 114 /* If we already have an ISA bridge, bail off */ 115 if (isa_bridge_devnode != NULL) 116 return; 117 118 /* For each "isa" node in the system. Note : we do a search by 119 * type and not by name. It might be better to do by name but that's 120 * what the code used to do and I don't want to break too much at 121 * once. We can look into changing that separately 122 */ 123 for_each_node_by_type(np, "isa") { 124 /* Look for our hose being a parent */ 125 for (parent = of_get_parent(np); parent;) { 126 if (parent == hose->dn) { 127 of_node_put(parent); 128 break; 129 } 130 tmp = parent; 131 parent = of_get_parent(parent); 132 of_node_put(tmp); 133 } 134 if (parent != NULL) 135 break; 136 } 137 if (np == NULL) 138 return; 139 isa_bridge_devnode = np; 140 141 /* Now parse the "ranges" property and setup the ISA mapping */ 142 process_ISA_OF_ranges(np, hose->io_base_phys); 143 144 /* Set the global ISA io base to indicate we have an ISA bridge */ 145 isa_io_base = ISA_IO_BASE; 146 147 pr_debug("ISA bridge (early) is %pOF\n", np); 148 } 149 150 /** 151 * isa_bridge_find_early - Find and map the ISA IO space early before 152 * main PCI discovery. This is optionally called by 153 * the arch code when adding PCI PHBs to get early 154 * access to ISA IO ports 155 */ 156 void __init isa_bridge_init_non_pci(struct device_node *np) 157 { 158 int ret; 159 160 /* If we already have an ISA bridge, bail off */ 161 if (isa_bridge_devnode != NULL) 162 return; 163 164 ret = process_ISA_OF_ranges(np, 0); 165 if (ret) 166 return; 167 168 /* Got it */ 169 isa_bridge_devnode = np; 170 171 /* Set the global ISA io base to indicate we have an ISA bridge 172 * and map it 173 */ 174 isa_io_base = ISA_IO_BASE; 175 176 pr_debug("ISA: Non-PCI bridge is %pOF\n", np); 177 } 178 179 /** 180 * isa_bridge_find_late - Find and map the ISA IO space upon discovery of 181 * a new ISA bridge 182 */ 183 static void isa_bridge_find_late(struct pci_dev *pdev, 184 struct device_node *devnode) 185 { 186 struct pci_controller *hose = pci_bus_to_host(pdev->bus); 187 188 /* Store ISA device node and PCI device */ 189 isa_bridge_devnode = of_node_get(devnode); 190 isa_bridge_pcidev = pdev; 191 192 /* Now parse the "ranges" property and setup the ISA mapping */ 193 process_ISA_OF_ranges(devnode, hose->io_base_phys); 194 195 /* Set the global ISA io base to indicate we have an ISA bridge */ 196 isa_io_base = ISA_IO_BASE; 197 198 pr_debug("ISA bridge (late) is %pOF on %s\n", 199 devnode, pci_name(pdev)); 200 } 201 202 /** 203 * isa_bridge_remove - Remove/unmap an ISA bridge 204 */ 205 static void isa_bridge_remove(void) 206 { 207 pr_debug("ISA bridge removed !\n"); 208 209 /* Clear the global ISA io base to indicate that we have no more 210 * ISA bridge. Note that drivers don't quite handle that, though 211 * we should probably do something about it. But do we ever really 212 * have ISA bridges being removed on machines using legacy devices ? 213 */ 214 isa_io_base = ISA_IO_BASE; 215 216 /* Clear references to the bridge */ 217 of_node_put(isa_bridge_devnode); 218 isa_bridge_devnode = NULL; 219 isa_bridge_pcidev = NULL; 220 221 /* Unmap the ISA area */ 222 vunmap_range(ISA_IO_BASE, ISA_IO_BASE + 0x10000); 223 } 224 225 /** 226 * isa_bridge_notify - Get notified of PCI devices addition/removal 227 */ 228 static int isa_bridge_notify(struct notifier_block *nb, unsigned long action, 229 void *data) 230 { 231 struct device *dev = data; 232 struct pci_dev *pdev = to_pci_dev(dev); 233 struct device_node *devnode = pci_device_to_OF_node(pdev); 234 235 switch(action) { 236 case BUS_NOTIFY_ADD_DEVICE: 237 /* Check if we have an early ISA device, without PCI dev */ 238 if (isa_bridge_devnode && isa_bridge_devnode == devnode && 239 !isa_bridge_pcidev) { 240 pr_debug("ISA bridge PCI attached: %s\n", 241 pci_name(pdev)); 242 isa_bridge_pcidev = pdev; 243 } 244 245 /* Check if we have no ISA device, and this happens to be one, 246 * register it as such if it has an OF device 247 */ 248 if (!isa_bridge_devnode && of_node_is_type(devnode, "isa")) 249 isa_bridge_find_late(pdev, devnode); 250 251 return 0; 252 case BUS_NOTIFY_DEL_DEVICE: 253 /* Check if this our existing ISA device */ 254 if (pdev == isa_bridge_pcidev || 255 (devnode && devnode == isa_bridge_devnode)) 256 isa_bridge_remove(); 257 return 0; 258 } 259 return 0; 260 } 261 262 static struct notifier_block isa_bridge_notifier = { 263 .notifier_call = isa_bridge_notify 264 }; 265 266 /** 267 * isa_bridge_init - register to be notified of ISA bridge addition/removal 268 * 269 */ 270 static int __init isa_bridge_init(void) 271 { 272 bus_register_notifier(&pci_bus_type, &isa_bridge_notifier); 273 return 0; 274 } 275 arch_initcall(isa_bridge_init); 276