pci_32.c 7.5 KB

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