pci_32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. static void __devinit pcibios_scan_phb(struct pci_controller *hose)
  310. {
  311. struct pci_bus *bus;
  312. struct device_node *node = hose->dn;
  313. unsigned long io_offset;
  314. struct resource *res = &hose->io_resource;
  315. pr_debug("PCI: Scanning PHB %s\n",
  316. node ? node->full_name : "<NO NAME>");
  317. /* Create an empty bus for the toplevel */
  318. bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
  319. if (bus == NULL) {
  320. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  321. hose->global_number);
  322. return;
  323. }
  324. bus->secondary = hose->first_busno;
  325. hose->bus = bus;
  326. /* Fixup IO space offset */
  327. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  328. res->start = (res->start + io_offset) & 0xffffffffu;
  329. res->end = (res->end + io_offset) & 0xffffffffu;
  330. /* Wire up PHB bus resources */
  331. pcibios_setup_phb_resources(hose);
  332. /* Scan children */
  333. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  334. }
  335. static int __init pcibios_init(void)
  336. {
  337. struct pci_controller *hose, *tmp;
  338. int next_busno = 0;
  339. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  340. if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
  341. pci_assign_all_buses = 1;
  342. /* Scan all of the recorded PCI controllers. */
  343. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  344. if (pci_assign_all_buses)
  345. hose->first_busno = next_busno;
  346. hose->last_busno = 0xff;
  347. pcibios_scan_phb(hose);
  348. pci_bus_add_devices(hose->bus);
  349. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  350. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  351. }
  352. pci_bus_count = next_busno;
  353. /* OpenFirmware based machines need a map of OF bus
  354. * numbers vs. kernel bus numbers since we may have to
  355. * remap them.
  356. */
  357. if (pci_assign_all_buses)
  358. pcibios_make_OF_bus_map();
  359. /* Call common code to handle resource allocation */
  360. pcibios_resource_survey();
  361. /* Call machine dependent post-init code */
  362. if (ppc_md.pcibios_after_init)
  363. ppc_md.pcibios_after_init();
  364. return 0;
  365. }
  366. subsys_initcall(pcibios_init);
  367. static struct pci_controller*
  368. pci_bus_to_hose(int bus)
  369. {
  370. struct pci_controller *hose, *tmp;
  371. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  372. if (bus >= hose->first_busno && bus <= hose->last_busno)
  373. return hose;
  374. return NULL;
  375. }
  376. /* Provide information on locations of various I/O regions in physical
  377. * memory. Do this on a per-card basis so that we choose the right
  378. * root bridge.
  379. * Note that the returned IO or memory base is a physical address
  380. */
  381. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  382. {
  383. struct pci_controller* hose;
  384. long result = -EOPNOTSUPP;
  385. hose = pci_bus_to_hose(bus);
  386. if (!hose)
  387. return -ENODEV;
  388. switch (which) {
  389. case IOBASE_BRIDGE_NUMBER:
  390. return (long)hose->first_busno;
  391. case IOBASE_MEMORY:
  392. return (long)hose->pci_mem_offset;
  393. case IOBASE_IO:
  394. return (long)hose->io_base_phys;
  395. case IOBASE_ISA_IO:
  396. return (long)isa_io_base;
  397. case IOBASE_ISA_MEM:
  398. return (long)isa_mem_base;
  399. }
  400. return result;
  401. }
  402. /*
  403. * Null PCI config access functions, for the case when we can't
  404. * find a hose.
  405. */
  406. #define NULL_PCI_OP(rw, size, type) \
  407. static int \
  408. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  409. { \
  410. return PCIBIOS_DEVICE_NOT_FOUND; \
  411. }
  412. static int
  413. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  414. int len, u32 *val)
  415. {
  416. return PCIBIOS_DEVICE_NOT_FOUND;
  417. }
  418. static int
  419. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  420. int len, u32 val)
  421. {
  422. return PCIBIOS_DEVICE_NOT_FOUND;
  423. }
  424. static struct pci_ops null_pci_ops =
  425. {
  426. .read = null_read_config,
  427. .write = null_write_config,
  428. };
  429. /*
  430. * These functions are used early on before PCI scanning is done
  431. * and all of the pci_dev and pci_bus structures have been created.
  432. */
  433. static struct pci_bus *
  434. fake_pci_bus(struct pci_controller *hose, int busnr)
  435. {
  436. static struct pci_bus bus;
  437. if (hose == 0) {
  438. hose = pci_bus_to_hose(busnr);
  439. if (hose == 0)
  440. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  441. }
  442. bus.number = busnr;
  443. bus.sysdata = hose;
  444. bus.ops = hose? hose->ops: &null_pci_ops;
  445. return &bus;
  446. }
  447. #define EARLY_PCI_OP(rw, size, type) \
  448. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  449. int devfn, int offset, type value) \
  450. { \
  451. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  452. devfn, offset, value); \
  453. }
  454. EARLY_PCI_OP(read, byte, u8 *)
  455. EARLY_PCI_OP(read, word, u16 *)
  456. EARLY_PCI_OP(read, dword, u32 *)
  457. EARLY_PCI_OP(write, byte, u8)
  458. EARLY_PCI_OP(write, word, u16)
  459. EARLY_PCI_OP(write, dword, u32)
  460. extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
  461. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  462. int cap)
  463. {
  464. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  465. }