pci_32.c 14 KB

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