of_pci_irq.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <linux/kernel.h>
  2. #include <linux/of_pci.h>
  3. #include <linux/of_irq.h>
  4. #include <linux/export.h>
  5. #include <asm/prom.h>
  6. /**
  7. * of_irq_map_pci - Resolve the interrupt for a PCI device
  8. * @pdev: the device whose interrupt is to be resolved
  9. * @out_irq: structure of_irq filled by this function
  10. *
  11. * This function resolves the PCI interrupt for a given PCI device. If a
  12. * device-node exists for a given pci_dev, it will use normal OF tree
  13. * walking. If not, it will implement standard swizzling and walk up the
  14. * PCI tree until an device-node is found, at which point it will finish
  15. * resolving using the OF tree walking.
  16. */
  17. int of_irq_map_pci(struct pci_dev *pdev, struct of_irq *out_irq)
  18. {
  19. struct device_node *dn, *ppnode;
  20. struct pci_dev *ppdev;
  21. u32 lspec;
  22. __be32 lspec_be;
  23. __be32 laddr[3];
  24. u8 pin;
  25. int rc;
  26. /* Check if we have a device node, if yes, fallback to standard
  27. * device tree parsing
  28. */
  29. dn = pci_device_to_OF_node(pdev);
  30. if (dn) {
  31. rc = of_irq_map_one(dn, 0, out_irq);
  32. if (!rc)
  33. return rc;
  34. }
  35. /* Ok, we don't, time to have fun. Let's start by building up an
  36. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  37. * for PCI. If you do different, then don't use that routine.
  38. */
  39. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  40. if (rc != 0)
  41. return rc;
  42. /* No pin, exit */
  43. if (pin == 0)
  44. return -ENODEV;
  45. /* Now we walk up the PCI tree */
  46. lspec = pin;
  47. for (;;) {
  48. /* Get the pci_dev of our parent */
  49. ppdev = pdev->bus->self;
  50. /* Ouch, it's a host bridge... */
  51. if (ppdev == NULL) {
  52. ppnode = pci_bus_to_OF_node(pdev->bus);
  53. /* No node for host bridge ? give up */
  54. if (ppnode == NULL)
  55. return -EINVAL;
  56. } else {
  57. /* We found a P2P bridge, check if it has a node */
  58. ppnode = pci_device_to_OF_node(ppdev);
  59. }
  60. /* Ok, we have found a parent with a device-node, hand over to
  61. * the OF parsing code.
  62. * We build a unit address from the linux device to be used for
  63. * resolution. Note that we use the linux bus number which may
  64. * not match your firmware bus numbering.
  65. * Fortunately, in most cases, interrupt-map-mask doesn't
  66. * include the bus number as part of the matching.
  67. * You should still be careful about that though if you intend
  68. * to rely on this function (you ship a firmware that doesn't
  69. * create device nodes for all PCI devices).
  70. */
  71. if (ppnode)
  72. break;
  73. /* We can only get here if we hit a P2P bridge with no node,
  74. * let's do standard swizzling and try again
  75. */
  76. lspec = pci_swizzle_interrupt_pin(pdev, lspec);
  77. pdev = ppdev;
  78. }
  79. lspec_be = cpu_to_be32(lspec);
  80. laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
  81. laddr[1] = laddr[2] = cpu_to_be32(0);
  82. return of_irq_map_raw(ppnode, &lspec_be, 1, laddr, out_irq);
  83. }
  84. EXPORT_SYMBOL_GPL(of_irq_map_pci);