pci_32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. if (!have_of)
  232. return NULL;
  233. pr_debug("pci_busdev_to_OF_node(%d,0x%x)\n", bus->number, devfn);
  234. parent = scan_OF_for_pci_bus(bus);
  235. if (parent == NULL)
  236. return NULL;
  237. pr_debug(" parent is %s\n", parent ? parent->full_name : "<NULL>");
  238. np = scan_OF_for_pci_dev(parent, devfn);
  239. of_node_put(parent);
  240. pr_debug(" result is %s\n", np ? np->full_name : "<NULL>");
  241. /* XXX most callers don't release the returned node
  242. * mostly because ppc64 doesn't increase the refcount,
  243. * we need to fix that.
  244. */
  245. return np;
  246. }
  247. EXPORT_SYMBOL(pci_busdev_to_OF_node);
  248. struct device_node*
  249. pci_device_to_OF_node(struct pci_dev *dev)
  250. {
  251. return pci_busdev_to_OF_node(dev->bus, dev->devfn);
  252. }
  253. EXPORT_SYMBOL(pci_device_to_OF_node);
  254. static int
  255. find_OF_pci_device_filter(struct device_node* node, void* data)
  256. {
  257. return ((void *)node == data);
  258. }
  259. /*
  260. * Returns the PCI device matching a given OF node
  261. */
  262. int
  263. pci_device_from_OF_node(struct device_node* node, u8* bus, u8* devfn)
  264. {
  265. const unsigned int *reg;
  266. struct pci_controller* hose;
  267. struct pci_dev* dev = NULL;
  268. if (!have_of)
  269. return -ENODEV;
  270. /* Make sure it's really a PCI device */
  271. hose = pci_find_hose_for_OF_device(node);
  272. if (!hose || !hose->dn)
  273. return -ENODEV;
  274. if (!scan_OF_pci_childs(hose->dn,
  275. find_OF_pci_device_filter, (void *)node))
  276. return -ENODEV;
  277. reg = of_get_property(node, "reg", NULL);
  278. if (!reg)
  279. return -ENODEV;
  280. *bus = (reg[0] >> 16) & 0xff;
  281. *devfn = ((reg[0] >> 8) & 0xff);
  282. /* Ok, here we need some tweak. If we have already renumbered
  283. * all busses, we can't rely on the OF bus number any more.
  284. * the pci_to_OF_bus_map is not enough as several PCI busses
  285. * may match the same OF bus number.
  286. */
  287. if (!pci_to_OF_bus_map)
  288. return 0;
  289. for_each_pci_dev(dev)
  290. if (pci_to_OF_bus_map[dev->bus->number] == *bus &&
  291. dev->devfn == *devfn) {
  292. *bus = dev->bus->number;
  293. pci_dev_put(dev);
  294. return 0;
  295. }
  296. return -ENODEV;
  297. }
  298. EXPORT_SYMBOL(pci_device_from_OF_node);
  299. /* We create the "pci-OF-bus-map" property now so it appears in the
  300. * /proc device tree
  301. */
  302. void __init
  303. pci_create_OF_bus_map(void)
  304. {
  305. struct property* of_prop;
  306. struct device_node *dn;
  307. of_prop = (struct property*) alloc_bootmem(sizeof(struct property) + 256);
  308. if (!of_prop)
  309. return;
  310. dn = of_find_node_by_path("/");
  311. if (dn) {
  312. memset(of_prop, -1, sizeof(struct property) + 256);
  313. of_prop->name = "pci-OF-bus-map";
  314. of_prop->length = 256;
  315. of_prop->value = &of_prop[1];
  316. prom_add_property(dn, of_prop);
  317. of_node_put(dn);
  318. }
  319. }
  320. #else /* CONFIG_PPC_OF */
  321. void pcibios_make_OF_bus_map(void)
  322. {
  323. }
  324. #endif /* CONFIG_PPC_OF */
  325. static void __devinit pcibios_scan_phb(struct pci_controller *hose)
  326. {
  327. struct pci_bus *bus;
  328. struct device_node *node = hose->dn;
  329. unsigned long io_offset;
  330. struct resource *res = &hose->io_resource;
  331. pr_debug("PCI: Scanning PHB %s\n",
  332. node ? node->full_name : "<NO NAME>");
  333. /* Create an empty bus for the toplevel */
  334. bus = pci_create_bus(hose->parent, hose->first_busno, hose->ops, hose);
  335. if (bus == NULL) {
  336. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  337. hose->global_number);
  338. return;
  339. }
  340. bus->secondary = hose->first_busno;
  341. hose->bus = bus;
  342. /* Fixup IO space offset */
  343. io_offset = (unsigned long)hose->io_base_virt - isa_io_base;
  344. res->start = (res->start + io_offset) & 0xffffffffu;
  345. res->end = (res->end + io_offset) & 0xffffffffu;
  346. /* Wire up PHB bus resources */
  347. pcibios_setup_phb_resources(hose);
  348. /* Scan children */
  349. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  350. }
  351. static int __init pcibios_init(void)
  352. {
  353. struct pci_controller *hose, *tmp;
  354. int next_busno = 0;
  355. printk(KERN_INFO "PCI: Probing PCI hardware\n");
  356. if (ppc_pci_flags & PPC_PCI_REASSIGN_ALL_BUS)
  357. pci_assign_all_buses = 1;
  358. /* Scan all of the recorded PCI controllers. */
  359. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  360. if (pci_assign_all_buses)
  361. hose->first_busno = next_busno;
  362. hose->last_busno = 0xff;
  363. pcibios_scan_phb(hose);
  364. pci_bus_add_devices(hose->bus);
  365. if (pci_assign_all_buses || next_busno <= hose->last_busno)
  366. next_busno = hose->last_busno + pcibios_assign_bus_offset;
  367. }
  368. pci_bus_count = next_busno;
  369. /* OpenFirmware based machines need a map of OF bus
  370. * numbers vs. kernel bus numbers since we may have to
  371. * remap them.
  372. */
  373. if (pci_assign_all_buses && have_of)
  374. pcibios_make_OF_bus_map();
  375. /* Call common code to handle resource allocation */
  376. pcibios_resource_survey();
  377. /* Call machine dependent post-init code */
  378. if (ppc_md.pcibios_after_init)
  379. ppc_md.pcibios_after_init();
  380. return 0;
  381. }
  382. subsys_initcall(pcibios_init);
  383. /* the next one is stolen from the alpha port... */
  384. void __init
  385. pcibios_update_irq(struct pci_dev *dev, int irq)
  386. {
  387. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  388. /* XXX FIXME - update OF device tree node interrupt property */
  389. }
  390. static struct pci_controller*
  391. pci_bus_to_hose(int bus)
  392. {
  393. struct pci_controller *hose, *tmp;
  394. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  395. if (bus >= hose->first_busno && bus <= hose->last_busno)
  396. return hose;
  397. return NULL;
  398. }
  399. /* Provide information on locations of various I/O regions in physical
  400. * memory. Do this on a per-card basis so that we choose the right
  401. * root bridge.
  402. * Note that the returned IO or memory base is a physical address
  403. */
  404. long sys_pciconfig_iobase(long which, unsigned long bus, unsigned long devfn)
  405. {
  406. struct pci_controller* hose;
  407. long result = -EOPNOTSUPP;
  408. hose = pci_bus_to_hose(bus);
  409. if (!hose)
  410. return -ENODEV;
  411. switch (which) {
  412. case IOBASE_BRIDGE_NUMBER:
  413. return (long)hose->first_busno;
  414. case IOBASE_MEMORY:
  415. return (long)hose->pci_mem_offset;
  416. case IOBASE_IO:
  417. return (long)hose->io_base_phys;
  418. case IOBASE_ISA_IO:
  419. return (long)isa_io_base;
  420. case IOBASE_ISA_MEM:
  421. return (long)isa_mem_base;
  422. }
  423. return result;
  424. }
  425. unsigned long pci_address_to_pio(phys_addr_t address)
  426. {
  427. struct pci_controller *hose, *tmp;
  428. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  429. unsigned int size = hose->io_resource.end -
  430. hose->io_resource.start + 1;
  431. if (address >= hose->io_base_phys &&
  432. address < (hose->io_base_phys + size)) {
  433. unsigned long base =
  434. (unsigned long)hose->io_base_virt - _IO_BASE;
  435. return base + (address - hose->io_base_phys);
  436. }
  437. }
  438. return (unsigned int)-1;
  439. }
  440. EXPORT_SYMBOL(pci_address_to_pio);
  441. /*
  442. * Null PCI config access functions, for the case when we can't
  443. * find a hose.
  444. */
  445. #define NULL_PCI_OP(rw, size, type) \
  446. static int \
  447. null_##rw##_config_##size(struct pci_dev *dev, int offset, type val) \
  448. { \
  449. return PCIBIOS_DEVICE_NOT_FOUND; \
  450. }
  451. static int
  452. null_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  453. int len, u32 *val)
  454. {
  455. return PCIBIOS_DEVICE_NOT_FOUND;
  456. }
  457. static int
  458. null_write_config(struct pci_bus *bus, unsigned int devfn, int offset,
  459. int len, u32 val)
  460. {
  461. return PCIBIOS_DEVICE_NOT_FOUND;
  462. }
  463. static struct pci_ops null_pci_ops =
  464. {
  465. .read = null_read_config,
  466. .write = null_write_config,
  467. };
  468. /*
  469. * These functions are used early on before PCI scanning is done
  470. * and all of the pci_dev and pci_bus structures have been created.
  471. */
  472. static struct pci_bus *
  473. fake_pci_bus(struct pci_controller *hose, int busnr)
  474. {
  475. static struct pci_bus bus;
  476. if (hose == 0) {
  477. hose = pci_bus_to_hose(busnr);
  478. if (hose == 0)
  479. printk(KERN_ERR "Can't find hose for PCI bus %d!\n", busnr);
  480. }
  481. bus.number = busnr;
  482. bus.sysdata = hose;
  483. bus.ops = hose? hose->ops: &null_pci_ops;
  484. return &bus;
  485. }
  486. #define EARLY_PCI_OP(rw, size, type) \
  487. int early_##rw##_config_##size(struct pci_controller *hose, int bus, \
  488. int devfn, int offset, type value) \
  489. { \
  490. return pci_bus_##rw##_config_##size(fake_pci_bus(hose, bus), \
  491. devfn, offset, value); \
  492. }
  493. EARLY_PCI_OP(read, byte, u8 *)
  494. EARLY_PCI_OP(read, word, u16 *)
  495. EARLY_PCI_OP(read, dword, u32 *)
  496. EARLY_PCI_OP(write, byte, u8)
  497. EARLY_PCI_OP(write, word, u16)
  498. EARLY_PCI_OP(write, dword, u32)
  499. extern int pci_bus_find_capability (struct pci_bus *bus, unsigned int devfn, int cap);
  500. int early_find_capability(struct pci_controller *hose, int bus, int devfn,
  501. int cap)
  502. {
  503. return pci_bus_find_capability(fake_pci_bus(hose, bus), devfn, cap);
  504. }