pci_32.c 11 KB

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