pci_32.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/sections.h>
  23. #include <asm/pci-bridge.h>
  24. #include <asm/ppc-pci.h>
  25. #include <asm/byteorder.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/machdep.h>
  28. #undef DEBUG
  29. unsigned long isa_io_base = 0;
  30. unsigned long pci_dram_offset = 0;
  31. int pcibios_assign_bus_offset = 1;
  32. EXPORT_SYMBOL(isa_io_base);
  33. EXPORT_SYMBOL(pci_dram_offset);
  34. static void fixup_cpc710_pci64(struct pci_dev* dev);
  35. /* By default, we don't re-assign bus numbers. We do this only on
  36. * some pmacs
  37. */
  38. static int pci_assign_all_buses;
  39. /* This will remain NULL for now, until isa-bridge.c is made common
  40. * to both 32-bit and 64-bit.
  41. */
  42. struct pci_dev *isa_bridge_pcidev;
  43. EXPORT_SYMBOL_GPL(isa_bridge_pcidev);
  44. static void
  45. fixup_cpc710_pci64(struct pci_dev* dev)
  46. {
  47. /* Hide the PCI64 BARs from the kernel as their content doesn't
  48. * fit well in the resource management
  49. */
  50. dev->resource[0].start = dev->resource[0].end = 0;
  51. dev->resource[0].flags = 0;
  52. dev->resource[1].start = dev->resource[1].end = 0;
  53. dev->resource[1].flags = 0;
  54. }
  55. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CPC710_PCI64, fixup_cpc710_pci64);
  56. #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP)
  57. static u8* pci_to_OF_bus_map;
  58. static int pci_bus_count;
  59. /*
  60. * Functions below are used on OpenFirmware machines.
  61. */
  62. static void
  63. make_one_node_map(struct device_node* node, u8 pci_bus)
  64. {
  65. const int *bus_range;
  66. int len;
  67. if (pci_bus >= pci_bus_count)
  68. return;
  69. bus_range = of_get_property(node, "bus-range", &len);
  70. if (bus_range == NULL || len < 2 * sizeof(int)) {
  71. printk(KERN_WARNING "Can't get bus-range for %pOF, "
  72. "assuming it starts at 0\n", node);
  73. pci_to_OF_bus_map[pci_bus] = 0;
  74. } else
  75. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  76. for_each_child_of_node(node, node) {
  77. struct pci_dev* dev;
  78. const unsigned int *class_code, *reg;
  79. class_code = of_get_property(node, "class-code", NULL);
  80. if (!class_code || ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  81. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  82. continue;
  83. reg = of_get_property(node, "reg", NULL);
  84. if (!reg)
  85. continue;
  86. dev = pci_get_domain_bus_and_slot(0, pci_bus,
  87. ((reg[0] >> 8) & 0xff));
  88. if (!dev || !dev->subordinate) {
  89. pci_dev_put(dev);
  90. continue;
  91. }
  92. make_one_node_map(node, dev->subordinate->number);
  93. pci_dev_put(dev);
  94. }
  95. }
  96. static void __init
  97. pcibios_make_OF_bus_map(void)
  98. {
  99. int i;
  100. struct pci_controller *hose, *tmp;
  101. struct property *map_prop;
  102. struct device_node *dn;
  103. pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
  104. if (!pci_to_OF_bus_map) {
  105. printk(KERN_ERR "Can't allocate OF bus map !\n");
  106. return;
  107. }
  108. /* We fill the bus map with invalid values, that helps
  109. * debugging.
  110. */
  111. for (i=0; i<pci_bus_count; i++)
  112. pci_to_OF_bus_map[i] = 0xff;
  113. /* For each hose, we begin searching bridges */
  114. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  115. struct device_node* node = hose->dn;
  116. if (!node)
  117. continue;
  118. make_one_node_map(node, hose->first_busno);
  119. }
  120. dn = of_find_node_by_path("/");
  121. map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
  122. if (map_prop) {
  123. BUG_ON(pci_bus_count > map_prop->length);
  124. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  125. }
  126. of_node_put(dn);
  127. #ifdef DEBUG
  128. printk("PCI->OF bus map:\n");
  129. for (i=0; i<pci_bus_count; i++) {
  130. if (pci_to_OF_bus_map[i] == 0xff)
  131. continue;
  132. printk("%d -> %d\n", i, pci_to_OF_bus_map[i]);
  133. }
  134. #endif
  135. }
  136. #ifdef CONFIG_PPC_PMAC
  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. #endif
  171. #ifdef CONFIG_PPC_CHRP
  172. /* We create the "pci-OF-bus-map" property now so it appears in the
  173. * /proc device tree
  174. */
  175. void __init
  176. pci_create_OF_bus_map(void)
  177. {
  178. struct property* of_prop;
  179. struct device_node *dn;
  180. of_prop = memblock_alloc(sizeof(struct property) + 256,
  181. SMP_CACHE_BYTES);
  182. if (!of_prop)
  183. panic("%s: Failed to allocate %zu bytes\n", __func__,
  184. sizeof(struct property) + 256);
  185. dn = of_find_node_by_path("/");
  186. if (dn) {
  187. memset(of_prop, -1, sizeof(struct property) + 256);
  188. of_prop->name = "pci-OF-bus-map";
  189. of_prop->length = 256;
  190. of_prop->value = &of_prop[1];
  191. of_add_property(dn, of_prop);
  192. of_node_put(dn);
  193. }
  194. }
  195. #endif
  196. #endif /* defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP) */
  197. void pcibios_setup_phb_io_space(struct pci_controller *hose)
  198. {
  199. unsigned long io_offset;
  200. struct resource *res = &hose->io_resource;
  201. /* Fixup IO space offset */
  202. io_offset = pcibios_io_space_offset(hose);
  203. res->start += io_offset;
  204. res->end += io_offset;
  205. }
  206. static int __init pcibios_init(void)
  207. {
  208. struct pci_controller *hose, *tmp;
  209. #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
  210. int next_busno = 0;
  211. #endif
  212. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  213. #ifdef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
  214. /*
  215. * Enable PCI domains in /proc when PCI bus numbers are not unique
  216. * across all PCI domains to prevent conflicts. And keep PCI domain 0
  217. * backward compatible in /proc for video cards.
  218. */
  219. pci_add_flags(PCI_ENABLE_PROC_DOMAINS | PCI_COMPAT_DOMAIN_0);
  220. #endif
  221. if (pci_has_flag(PCI_REASSIGN_ALL_BUS))
  222. pci_assign_all_buses = 1;
  223. /* Scan all of the recorded PCI controllers. */
  224. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  225. #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
  226. if (pci_assign_all_buses)
  227. hose->first_busno = next_busno;
  228. #endif
  229. hose->last_busno = 0xff;
  230. pcibios_scan_phb(hose);
  231. pci_bus_add_devices(hose->bus);
  232. #ifndef CONFIG_PPC_PCI_BUS_NUM_DOMAIN_DEPENDENT
  233. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  234. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  235. #endif
  236. }
  237. #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_CHRP)
  238. pci_bus_count = next_busno;
  239. /* OpenFirmware based machines need a map of OF bus
  240. * numbers vs. kernel bus numbers since we may have to
  241. * remap them.
  242. */
  243. if (pci_assign_all_buses)
  244. pcibios_make_OF_bus_map();
  245. #endif
  246. /* Call common code to handle resource allocation */
  247. pcibios_resource_survey();
  248. /* Call machine dependent fixup */
  249. if (ppc_md.pcibios_fixup)
  250. ppc_md.pcibios_fixup();
  251. /* Call machine dependent post-init code */
  252. if (ppc_md.pcibios_after_init)
  253. ppc_md.pcibios_after_init();
  254. return 0;
  255. }
  256. subsys_initcall(pcibios_init);
  257. static struct pci_controller*
  258. pci_bus_to_hose(int bus)
  259. {
  260. struct pci_controller *hose, *tmp;
  261. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  262. if (bus >= hose->first_busno && bus <= hose->last_busno)
  263. return hose;
  264. return NULL;
  265. }
  266. /* Provide information on locations of various I/O regions in physical
  267. * memory. Do this on a per-card basis so that we choose the right
  268. * root bridge.
  269. * Note that the returned IO or memory base is a physical address
  270. */
  271. SYSCALL_DEFINE3(pciconfig_iobase, long, which,
  272. unsigned long, bus, unsigned long, devfn)
  273. {
  274. struct pci_controller* hose;
  275. long result = -EOPNOTSUPP;
  276. hose = pci_bus_to_hose(bus);
  277. if (!hose)
  278. return -ENODEV;
  279. switch (which) {
  280. case IOBASE_BRIDGE_NUMBER:
  281. return (long)hose->first_busno;
  282. case IOBASE_MEMORY:
  283. return (long)hose->mem_offset[0];
  284. case IOBASE_IO:
  285. return (long)hose->io_base_phys;
  286. case IOBASE_ISA_IO:
  287. return (long)isa_io_base;
  288. case IOBASE_ISA_MEM:
  289. return (long)isa_mem_base;
  290. }
  291. return result;
  292. }