pci_32.c 11 KB

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