of.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * PCI <-> OF mapping helpers
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/irqdomain.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <linux/of.h>
  15. #include <linux/of_pci.h>
  16. #include "pci.h"
  17. void pci_set_of_node(struct pci_dev *dev)
  18. {
  19. if (!dev->bus->dev.of_node)
  20. return;
  21. dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
  22. dev->devfn);
  23. }
  24. void pci_release_of_node(struct pci_dev *dev)
  25. {
  26. of_node_put(dev->dev.of_node);
  27. dev->dev.of_node = NULL;
  28. }
  29. void pci_set_bus_of_node(struct pci_bus *bus)
  30. {
  31. if (bus->self == NULL)
  32. bus->dev.of_node = pcibios_get_phb_of_node(bus);
  33. else
  34. bus->dev.of_node = of_node_get(bus->self->dev.of_node);
  35. }
  36. void pci_release_bus_of_node(struct pci_bus *bus)
  37. {
  38. of_node_put(bus->dev.of_node);
  39. bus->dev.of_node = NULL;
  40. }
  41. struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
  42. {
  43. /* This should only be called for PHBs */
  44. if (WARN_ON(bus->self || bus->parent))
  45. return NULL;
  46. /* Look for a node pointer in either the intermediary device we
  47. * create above the root bus or it's own parent. Normally only
  48. * the later is populated.
  49. */
  50. if (bus->bridge->of_node)
  51. return of_node_get(bus->bridge->of_node);
  52. if (bus->bridge->parent && bus->bridge->parent->of_node)
  53. return of_node_get(bus->bridge->parent->of_node);
  54. return NULL;
  55. }
  56. struct irq_domain *pci_host_bridge_of_msi_domain(struct pci_bus *bus)
  57. {
  58. #ifdef CONFIG_IRQ_DOMAIN
  59. struct device_node *np;
  60. struct irq_domain *d;
  61. if (!bus->dev.of_node)
  62. return NULL;
  63. /* Start looking for a phandle to an MSI controller. */
  64. np = of_parse_phandle(bus->dev.of_node, "msi-parent", 0);
  65. /*
  66. * If we don't have an msi-parent property, look for a domain
  67. * directly attached to the host bridge.
  68. */
  69. if (!np)
  70. np = bus->dev.of_node;
  71. d = irq_find_matching_host(np, DOMAIN_BUS_PCI_MSI);
  72. if (d)
  73. return d;
  74. return irq_find_host(np);
  75. #else
  76. return NULL;
  77. #endif
  78. }