pci_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/byteorder.h>
  23. #include <asm/uaccess.h>
  24. #undef DEBUG
  25. unsigned long isa_io_base;
  26. unsigned long pci_dram_offset;
  27. int pcibios_assign_bus_offset = 1;
  28. static u8 *pci_to_OF_bus_map;
  29. /* By default, we don't re-assign bus numbers. We do this only on
  30. * some pmacs
  31. */
  32. static int pci_assign_all_buses;
  33. static int pci_bus_count;
  34. /*
  35. * Functions below are used on OpenFirmware machines.
  36. */
  37. static void
  38. make_one_node_map(struct device_node *node, u8 pci_bus)
  39. {
  40. const int *bus_range;
  41. int len;
  42. if (pci_bus >= pci_bus_count)
  43. return;
  44. bus_range = of_get_property(node, "bus-range", &len);
  45. if (bus_range == NULL || len < 2 * sizeof(int)) {
  46. printk(KERN_WARNING "Can't get bus-range for %s, "
  47. "assuming it starts at 0\n", node->full_name);
  48. pci_to_OF_bus_map[pci_bus] = 0;
  49. } else
  50. pci_to_OF_bus_map[pci_bus] = bus_range[0];
  51. for_each_child_of_node(node, node) {
  52. struct pci_dev *dev;
  53. const unsigned int *class_code, *reg;
  54. class_code = of_get_property(node, "class-code", NULL);
  55. if (!class_code ||
  56. ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  57. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS))
  58. continue;
  59. reg = of_get_property(node, "reg", NULL);
  60. if (!reg)
  61. continue;
  62. dev = pci_get_bus_and_slot(pci_bus, ((reg[0] >> 8) & 0xff));
  63. if (!dev || !dev->subordinate) {
  64. pci_dev_put(dev);
  65. continue;
  66. }
  67. make_one_node_map(node, dev->subordinate->number);
  68. pci_dev_put(dev);
  69. }
  70. }
  71. void
  72. pcibios_make_OF_bus_map(void)
  73. {
  74. int i;
  75. struct pci_controller *hose, *tmp;
  76. struct property *map_prop;
  77. struct device_node *dn;
  78. pci_to_OF_bus_map = kmalloc(pci_bus_count, GFP_KERNEL);
  79. if (!pci_to_OF_bus_map) {
  80. printk(KERN_ERR "Can't allocate OF bus map !\n");
  81. return;
  82. }
  83. /* We fill the bus map with invalid values, that helps
  84. * debugging.
  85. */
  86. for (i = 0; i < pci_bus_count; i++)
  87. pci_to_OF_bus_map[i] = 0xff;
  88. /* For each hose, we begin searching bridges */
  89. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  90. struct device_node *node = hose->dn;
  91. if (!node)
  92. continue;
  93. make_one_node_map(node, hose->first_busno);
  94. }
  95. dn = of_find_node_by_path("/");
  96. map_prop = of_find_property(dn, "pci-OF-bus-map", NULL);
  97. if (map_prop) {
  98. BUG_ON(pci_bus_count > map_prop->length);
  99. memcpy(map_prop->value, pci_to_OF_bus_map, pci_bus_count);
  100. }
  101. of_node_put(dn);
  102. #ifdef DEBUG
  103. printk(KERN_INFO "PCI->OF bus map:\n");
  104. for (i = 0; i < pci_bus_count; i++) {
  105. if (pci_to_OF_bus_map[i] == 0xff)
  106. continue;
  107. printk(KERN_INFO "%d -> %d\n", i, pci_to_OF_bus_map[i]);
  108. }
  109. #endif
  110. }
  111. typedef int (*pci_OF_scan_iterator)(struct device_node *node, void *data);
  112. static struct device_node *scan_OF_pci_childs(struct device_node *parent,
  113. pci_OF_scan_iterator filter, void *data)
  114. {
  115. struct device_node *node;
  116. struct device_node *sub_node;
  117. for_each_child_of_node(parent, node) {
  118. const unsigned int *class_code;
  119. if (filter(node, data)) {
  120. of_node_put(node);
  121. return node;
  122. }
  123. /* For PCI<->PCI bridges or CardBus bridges, we go down
  124. * Note: some OFs create a parent node "multifunc-device" as
  125. * a fake root for all functions of a multi-function device,
  126. * we go down them as well.
  127. */
  128. class_code = of_get_property(node, "class-code", NULL);
  129. if ((!class_code ||
  130. ((*class_code >> 8) != PCI_CLASS_BRIDGE_PCI &&
  131. (*class_code >> 8) != PCI_CLASS_BRIDGE_CARDBUS)) &&
  132. strcmp(node->name, "multifunc-device"))
  133. continue;
  134. sub_node = scan_OF_pci_childs(node, filter, data);
  135. if (sub_node) {
  136. of_node_put(node);
  137. return sub_node;
  138. }
  139. }
  140. return NULL;
  141. }
  142. static struct device_node *scan_OF_for_pci_dev(struct device_node *parent,
  143. unsigned int devfn)
  144. {
  145. struct device_node *np, *cnp;
  146. const u32 *reg;
  147. unsigned int psize;
  148. for_each_child_of_node(parent, np) {
  149. reg = of_get_property(np, "reg", &psize);
  150. if (reg && psize >= 4 && ((reg[0] >> 8) & 0xff) == devfn)
  151. return np;
  152. /* Note: some OFs create a parent node "multifunc-device" as
  153. * a fake root for all functions of a multi-function device,
  154. * we go down them as well. */
  155. if (!strcmp(np->name, "multifunc-device")) {
  156. cnp = scan_OF_for_pci_dev(np, devfn);
  157. if (cnp)
  158. return cnp;
  159. }
  160. }
  161. return NULL;
  162. }
  163. static struct device_node *scan_OF_for_pci_bus(struct pci_bus *bus)
  164. {
  165. struct device_node *parent, *np;
  166. /* Are we a root bus ? */
  167. if (bus->self == NULL || bus->parent == NULL) {
  168. struct pci_controller *hose = pci_bus_to_host(bus);
  169. if (hose == NULL)
  170. return NULL;
  171. return of_node_get(hose->dn);
  172. }
  173. /* not a root bus, we need to get our parent */
  174. parent = scan_OF_for_pci_bus(bus->parent);
  175. if (parent == NULL)
  176. return NULL;
  177. /* now iterate for children for a match */
  178. np = scan_OF_for_pci_dev(parent, bus->self->devfn);
  179. of_node_put(parent);
  180. return np;
  181. }
  182. /*
  183. * Scans the OF tree for a device node matching a PCI device
  184. */
  185. struct device_node *
  186. pci_busdev_to_OF_node(struct pci_bus *bus, int devfn)
  187. {
  188. struct device_node *parent, *np;
  189. pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
  190. parent = scan_OF_for_pci_bus(bus);
  191. if (parent == NULL)
  192. return NULL;
  193. pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
  194. np = scan_OF_for_pci_dev(parent, devfn);
  195. of_node_put(parent);
  196. pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
  197. /* XXX most callers don't release the returned node
  198. * mostly because ppc64 doesn't increase the refcount,
  199. * we need to fix that.
  200. */
  201. return np;
  202. }
  203. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  204. struct device_node*
  205. pci_device_to_OF_node(struct pci_dev *dev)
  206. {
  207. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  208. }
  209. EXPORT_SYMBOL(pci_device_to_OF_node);
  210. static int
  211. find_OF_pci_device_filter(struct device_node *node, void *data)
  212. {
  213. return ((void *)node == data);
  214. }
  215. /*
  216. * Returns the PCI device matching a given OF node
  217. */
  218. int
  219. pci_device_from_OF_node(struct device_node *node, u8 *bus, u8 *devfn)
  220. {
  221. const unsigned int *reg;
  222. struct pci_controller *hose;
  223. struct pci_dev *dev = NULL;
  224. /* Make sure it's really a PCI device */
  225. hose = pci_find_hose_for_OF_device(node);
  226. if (!hose || !hose->dn)
  227. return -ENODEV;
  228. if (!scan_OF_pci_childs(hose->dn,
  229. find_OF_pci_device_filter, (void *)node))
  230. return -ENODEV;
  231. reg = of_get_property(node, "reg", NULL);
  232. if (!reg)
  233. return -ENODEV;
  234. *bus = (reg[0] >> 16) & 0xff;
  235. *devfn = ((reg[0] >> 8) & 0xff);
  236. /* Ok, here we need some tweak. If we have already renumbered
  237. * all busses, we can't rely on the OF bus number any more.
  238. * the pci_to_OF_bus_map is not enough as several PCI busses
  239. * may match the same OF bus number.
  240. */
  241. if (!pci_to_OF_bus_map)
  242. return 0;
  243. for_each_pci_dev(dev)
  244. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  245. dev->devfn == *devfn) {
  246. *bus = dev->bus->number;
  247. pci_dev_put(dev);
  248. return 0;
  249. }
  250. return -ENODEV;
  251. }
  252. EXPORT_SYMBOL(pci_device_from_OF_node);
  253. /* We create the "pci-OF-bus-map" property now so it appears in the
  254. * /proc device tree
  255. */
  256. void __init
  257. pci_create_OF_bus_map(void)
  258. {
  259. struct property *of_prop;
  260. struct device_node *dn;
  261. of_prop = (struct property *) alloc_bootmem(sizeof(struct property) + \
  262. 256);
  263. if (!of_prop)
  264. return;
  265. dn = of_find_node_by_path("/");
  266. if (dn) {
  267. memset(of_prop, -1, sizeof(struct property) + 256);
  268. of_prop->name = "pci-OF-bus-map";
  269. of_prop->length = 256;
  270. of_prop->value = &of_prop[1];
  271. prom_add_property(dn, of_prop);
  272. of_node_put(dn);
  273. }
  274. }
  275. static void __devinit pcibios_scan_phb(struct pci_controller *hose)
  276. {
  277. struct pci_bus *bus;
  278. struct device_node *node = hose->dn;
  279. unsigned long io_offset;
  280. struct resource *res = &hose->io_resource;
  281. pr_debug("PCI: Scanning PHB %s\n",
  282. node ? node->full_name : "<NO NAME>");
  283. /* Create an empty bus for the toplevel */
  284. bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
  285. if (bus == NULL) {
  286. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  287. hose->global_number);
  288. return;
  289. }
  290. bus.dev->of_node = of_node_get(node);
  291. bus->secondary = hose->first_busno;
  292. hose->bus = bus;
  293. /* Fixup IO space offset */
  294. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  295. res->start = (res->start + io_offset) & 0xffffffffu;
  296. res->end = (res->end + io_offset) & 0xffffffffu;
  297. /* Wire up PHB bus resources */
  298. pcibios_setup_phb_resources(hose);
  299. /* Scan children */
  300. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  301. }
  302. static int __init pcibios_init(void)
  303. {
  304. struct pci_controller *hose, *tmp;
  305. int next_busno = 0;
  306. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  307. if (pci_flags & PCI_REASSIGN_ALL_BUS) {
  308. printk(KERN_INFO "setting pci_asign_all_busses\n");
  309. pci_assign_all_buses = 1;
  310. }
  311. /* Scan all of the recorded PCI controllers. */
  312. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  313. if (pci_assign_all_buses)
  314. hose->first_busno = next_busno;
  315. hose->last_busno = 0xff;
  316. pcibios_scan_phb(hose);
  317. printk(KERN_INFO "calling pci_bus_add_devices()\n");
  318. pci_bus_add_devices(hose->bus);
  319. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  320. next_busno = hose->last_busno + \
  321. pcibios_assign_bus_offset;
  322. }
  323. pci_bus_count = next_busno;
  324. /* OpenFirmware based machines need a map of OF bus
  325. * numbers vs. kernel bus numbers since we may have to
  326. * remap them.
  327. */
  328. if (pci_assign_all_buses)
  329. pcibios_make_OF_bus_map();
  330. /* Call common code to handle resource allocation */
  331. pcibios_resource_survey();
  332. return 0;
  333. }
  334. subsys_initcall(pcibios_init);
  335. static struct pci_controller*
  336. pci_bus_to_hose(int bus)
  337. {
  338. struct pci_controller *hose, *tmp;
  339. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  340. if (bus >= hose->first_busno && bus <= hose->last_busno)
  341. return hose;
  342. return NULL;
  343. }
  344. /* Provide information on locations of various I/O regions in physical
  345. * memory. Do this on a per-card basis so that we choose the right
  346. * root bridge.
  347. * Note that the returned IO or memory base is a physical address
  348. */
  349. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  350. {
  351. struct pci_controller *hose;
  352. long result = -EOPNOTSUPP;
  353. hose = pci_bus_to_hose(bus);
  354. if (!hose)
  355. return -ENODEV;
  356. switch (which) {
  357. case IOBASE_BRIDGE_NUMBER:
  358. return (long)hose->first_busno;
  359. case IOBASE_MEMORY:
  360. return (long)hose->pci_mem_offset;
  361. case IOBASE_IO:
  362. return (long)hose->io_base_phys;
  363. case IOBASE_ISA_IO:
  364. return (long)isa_io_base;
  365. case IOBASE_ISA_MEM:
  366. return (long)isa_mem_base;
  367. }
  368. return result;
  369. }