pci_32.c 15 KB

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