isa-bridge.c 7.1 KB

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