pci_32.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common pmac/prep/chrp pci routines. -- Cort
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/pci.h>
  7. #include <linux/delay.h>
  8. #include <linux/string.h>
  9. #include <linux/init.h>
  10. #include <linux/capability.h>
  11. #include <linux/sched.h>
  12. #include <linux/errno.h>
  13. #include <linux/memblock.h>
  14. #include <linux/syscalls.h>
  15. #include <linux/irq.h>
  16. #include <linux/list.h>
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. #include <linux/export.h>
  20. #include <asm/processor.h>
  21. #include <asm/io.h>
  22. #include <asm/prom.h>
  23. #include <asm/sections.h>
  24. #include <asm/pci-bridge.h>
  25. #include <asm/ppc-pci.h>
  26. #include <asm/byteorder.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/machdep.h>
  29. #undef DEBUG
  30. unsigned long isa_io_base = 0;
  31. unsigned long pci_dram_offset = 0;
  32. int pcibios_assign_bus_offset = 1;
  33. EXPORT_SYMBOL(isa_io_base);
  34. EXPORT_SYMBOL(pci_dram_offset);
  35. void pcibios_make_OF_bus_map(void);
  36. static void fixup_cpc710_pci64(struct pci_dev* dev);
  37. static u8* pci_to_OF_bus_map;
  38. /* By default, we don't re-assign bus numbers. We do this only on
  39. * some pmacs
  40. */
  41. static int pci_assign_all_buses;
  42. static int pci_bus_count;
  43. /* This will remain NULL for now, until isa-bridge.c is made common
  44. * to both 32-bit and 64-bit.
  45. */
  46. struct pci_dev *isa_bridge_pcidev;
  47. EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
  48. static void
  49. fixup_cpc710_pci64(struct pci_dev* dev)
  50. {
  51. /* Hide the PCI64 BARs from the kernel as their content doesn't
  52. * fit well in the resource management
  53. */
  54. dev->resource[0].start = dev->resource[0].end = 0;
  55. dev->resource[0].flags = 0;
  56. dev->resource[1].start = dev->resource[1].end = 0;
  57. dev->resource[1].flags = 0;
  58. }
  59. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  60. /*
  61. * Functions below are used on OpenFirmware machines.
  62. */
  63. static void
  64. make_one_node_map(struct device_node* node, u8 pci_bus)
  65. {
  66. const int *bus_range;
  67. int len;
  68. if (pci_bus >= pci_bus_count)
  69. return;
  70. bus_range = of_get_property(node, "bus-range", &len);
  71. if (bus_range == NULL || len < 2 * sizeof(int)) {
  72. printk(KERN_WARNING "Can't get bus-range for %pOF, "
  73. "assuming it starts at 0\n", node);
  74. pci_to_OF_bus_map[pci_bus] = 0;
  75. } else
  76. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  77. for_each_child_of_node(node, node) {
  78. struct pci_dev* dev;
  79. const unsigned int *class_code, *reg;
  80. class_code = of_get_property(node, "class-code", NULL);
  81. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  82. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  83. continue;
  84. reg = of_get_property(node, "reg", NULL);
  85. if (!reg)
  86. continue;
  87. dev = pci_get_domain_bus_and_slot(0, pci_bus,
  88. ((reg[0] >> 8) & 0xff));
  89. if (!dev || !dev->subordinate) {
  90. pci_dev_put(dev);
  91. continue;
  92. }
  93. make_one_node_map(node, dev->subordinate->number);
  94. pci_dev_put(dev);
  95. }
  96. }
  97. void
  98. pcibios_make_OF_bus_map(void)
  99. {
  100. int i;
  101. struct pci_controller *hose, *tmp;
  102. struct property *map_prop;
  103. struct device_node *dn;
  104. pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
  105. if (!pci_to_OF_bus_map) {
  106. printk(KERN_ERR "Can't allocate OF bus map !\n");
  107. return;
  108. }
  109. /* We fill the bus map with invalid values, that helps
  110. * debugging.
  111. */
  112. for (i=0; i<pci_bus_count; i++)
  113. pci_to_OF_bus_map[i] = 0xff;
  114. /* For each hose, we begin searching bridges */
  115. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  116. struct device_node* node = hose->dn;
  117. if (!node)
  118. continue;
  119. make_one_node_map(node, hose->first_busno);
  120. }
  121. dn = of_find_node_by_path("/");
  122. map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
  123. if (map_prop) {
  124. BUG_ON(pci_bus_count > map_prop->length);
  125. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  126. }
  127. of_node_put(dn);
  128. #ifdef DEBUG
  129. printk("PCI->OF bus map:\n");
  130. for (i=0; i<pci_bus_count; i++) {
  131. if (pci_to_OF_bus_map[i] == 0xff)
  132. continue;
  133. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  134. }
  135. #endif
  136. }
  137. /*
  138. * Returns the PCI device matching a given OF node
  139. */
  140. int pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
  141. {
  142. struct pci_dev *dev = NULL;
  143. const __be32 *reg;
  144. int size;
  145. /* Check if it might have a chance to be a PCI device */
  146. if (!pci_find_hose_for_OF_device(node))
  147. return -ENODEV;
  148. reg = of_get_property(node, "reg", &size);
  149. if (!reg || size < 5 * sizeof(u32))
  150. return -ENODEV;
  151. *bus = (be32_to_cpup(&reg[0]) >> 16) & 0xff;
  152. *devfn = (be32_to_cpup(&reg[0]) >> 8) & 0xff;
  153. /* Ok, here we need some tweak. If we have already renumbered
  154. * all busses, we can't rely on the OF bus number any more.
  155. * the pci_to_OF_bus_map is not enough as several PCI busses
  156. * may match the same OF bus number.
  157. */
  158. if (!pci_to_OF_bus_map)
  159. return 0;
  160. for_each_pci_dev(dev)
  161. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  162. dev->devfn == *devfn) {
  163. *bus = dev->bus->number;
  164. pci_dev_put(dev);
  165. return 0;
  166. }
  167. return -ENODEV;
  168. }
  169. EXPORT_SYMBOL(pci_device_from_OF_node);
  170. /* We create the "pci-OF-bus-map" property now so it appears in the
  171. * /proc device tree
  172. */
  173. void __init
  174. pci_create_OF_bus_map(void)
  175. {
  176. struct property* of_prop;
  177. struct device_node *dn;
  178. of_prop = memblock_alloc(sizeof(struct property) + 256,
  179. SMP_CACHE_BYTES);
  180. if (!of_prop)
  181. panic("%s: Failed to allocate %zu bytes\n", __func__,
  182. sizeof(struct property) + 256);
  183. dn = of_find_node_by_path("/");
  184. if (dn) {
  185. memset(of_prop, -1, sizeof(struct property) + 256);
  186. of_prop->name = "pci-OF-bus-map";
  187. of_prop->length = 256;
  188. of_prop->value = &of_prop[1];
  189. of_add_property(dn, of_prop);
  190. of_node_put(dn);
  191. }
  192. }
  193. void pcibios_setup_phb_io_space(struct pci_controller *hose)
  194. {
  195. unsigned long io_offset;
  196. struct resource *res = &hose->io_resource;
  197. /* Fixup IO space offset */
  198. io_offset = pcibios_io_space_offset(hose);
  199. res->start += io_offset;
  200. res->end += io_offset;
  201. }
  202. static int __init pcibios_init(void)
  203. {
  204. struct pci_controller *hose, *tmp;
  205. int next_busno = 0;
  206. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  207. if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
  208. pci_assign_all_buses = 1;
  209. /* Scan all of the recorded PCI controllers. */
  210. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  211. if (pci_assign_all_buses)
  212. hose->first_busno = next_busno;
  213. hose->last_busno = 0xff;
  214. pcibios_scan_phb(hose);
  215. pci_bus_add_devices(hose->bus);
  216. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  217. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  218. }
  219. pci_bus_count = next_busno;
  220. /* OpenFirmware based machines need a map of OF bus
  221. * numbers vs. kernel bus numbers since we may have to
  222. * remap them.
  223. */
  224. if (pci_assign_all_buses)
  225. pcibios_make_OF_bus_map();
  226. /* Call common code to handle resource allocation */
  227. pcibios_resource_survey();
  228. /* Call machine dependent fixup */
  229. if (ppc_md.pcibios_fixup)
  230. ppc_md.pcibios_fixup();
  231. /* Call machine dependent post-init code */
  232. if (ppc_md.pcibios_after_init)
  233. ppc_md.pcibios_after_init();
  234. return 0;
  235. }
  236. subsys_initcall(pcibios_init);
  237. static struct pci_controller*
  238. pci_bus_to_hose(int bus)
  239. {
  240. struct pci_controller *hose, *tmp;
  241. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  242. if (bus >= hose->first_busno && bus <= hose->last_busno)
  243. return hose;
  244. return NULL;
  245. }
  246. /* Provide information on locations of various I/O regions in physical
  247. * memory. Do this on a per-card basis so that we choose the right
  248. * root bridge.
  249. * Note that the returned IO or memory base is a physical address
  250. */
  251. SYSCALL_DEFINE3(pciconfig_iobase, long, which,
  252. unsigned long, bus, unsigned long, devfn)
  253. {
  254. struct pci_controller* hose;
  255. long result = -EOPNOTSUPP;
  256. hose = pci_bus_to_hose(bus);
  257. if (!hose)
  258. return -ENODEV;
  259. switch (which) {
  260. case IOBASE_BRIDGE_NUMBER:
  261. return (long)hose->first_busno;
  262. case IOBASE_MEMORY:
  263. return (long)hose->mem_offset[0];
  264. case IOBASE_IO:
  265. return (long)hose->io_base_phys;
  266. case IOBASE_ISA_IO:
  267. return (long)isa_io_base;
  268. case IOBASE_ISA_MEM:
  269. return (long)isa_mem_base;
  270. }
  271. return result;
  272. }