pci_32.c 15 KB

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