of_pci.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include <linux/kernel.h>
  2. #include <linux/export.h>
  3. #include <linux/of.h>
  4. #include <linux/of_address.h>
  5. #include <linux/of_pci.h>
  6. #include <linux/slab.h>
  7. static inline int __of_pci_pci_compare(struct device_node *node,
  8. unsigned int data)
  9. {
  10. int devfn;
  11. devfn = of_pci_get_devfn(node);
  12. if (devfn < 0)
  13. return 0;
  14. return devfn == data;
  15. }
  16. struct device_node *of_pci_find_child_device(struct device_node *parent,
  17. unsigned int devfn)
  18. {
  19. struct device_node *node, *node2;
  20. for_each_child_of_node(parent, node) {
  21. if (__of_pci_pci_compare(node, devfn))
  22. return node;
  23. /*
  24. * Some OFs create a parent node "multifunc-device" as
  25. * a fake root for all functions of a multi-function
  26. * device we go down them as well.
  27. */
  28. if (!strcmp(node->name, "multifunc-device")) {
  29. for_each_child_of_node(node, node2) {
  30. if (__of_pci_pci_compare(node2, devfn)) {
  31. of_node_put(node);
  32. return node2;
  33. }
  34. }
  35. }
  36. }
  37. return NULL;
  38. }
  39. EXPORT_SYMBOL_GPL(of_pci_find_child_device);
  40. /**
  41. * of_pci_get_devfn() - Get device and function numbers for a device node
  42. * @np: device node
  43. *
  44. * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
  45. * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
  46. * and function numbers respectively. On error a negative error code is
  47. * returned.
  48. */
  49. int of_pci_get_devfn(struct device_node *np)
  50. {
  51. unsigned int size;
  52. const __be32 *reg;
  53. reg = of_get_property(np, "reg", &size);
  54. if (!reg || size < 5 * sizeof(__be32))
  55. return -EINVAL;
  56. return (be32_to_cpup(reg) >> 8) & 0xff;
  57. }
  58. EXPORT_SYMBOL_GPL(of_pci_get_devfn);
  59. /**
  60. * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
  61. * @node: device node
  62. * @res: address to a struct resource to return the bus-range
  63. *
  64. * Returns 0 on success or a negative error-code on failure.
  65. */
  66. int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
  67. {
  68. const __be32 *values;
  69. int len;
  70. values = of_get_property(node, "bus-range", &len);
  71. if (!values || len < sizeof(*values) * 2)
  72. return -EINVAL;
  73. res->name = node->name;
  74. res->start = be32_to_cpup(values++);
  75. res->end = be32_to_cpup(values);
  76. res->flags = IORESOURCE_BUS;
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
  80. /**
  81. * This function will try to obtain the host bridge domain number by
  82. * finding a property called "linux,pci-domain" of the given device node.
  83. *
  84. * @node: device tree node with the domain information
  85. *
  86. * Returns the associated domain number from DT in the range [0-0xffff], or
  87. * a negative value if the required property is not found.
  88. */
  89. int of_get_pci_domain_nr(struct device_node *node)
  90. {
  91. const __be32 *value;
  92. int len;
  93. u16 domain;
  94. value = of_get_property(node, "linux,pci-domain", &len);
  95. if (!value || len < sizeof(*value))
  96. return -EINVAL;
  97. domain = (u16)be32_to_cpup(value);
  98. return domain;
  99. }
  100. EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
  101. #if defined(CONFIG_OF_ADDRESS)
  102. /**
  103. * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
  104. * @dev: device node of the host bridge having the range property
  105. * @busno: bus number associated with the bridge root bus
  106. * @bus_max: maximum number of buses for this bridge
  107. * @resources: list where the range of resources will be added after DT parsing
  108. * @io_base: pointer to a variable that will contain on return the physical
  109. * address for the start of the I/O range. Can be NULL if the caller doesn't
  110. * expect IO ranges to be present in the device tree.
  111. *
  112. * It is the caller's job to free the @resources list.
  113. *
  114. * This function will parse the "ranges" property of a PCI host bridge device
  115. * node and setup the resource mapping based on its content. It is expected
  116. * that the property conforms with the Power ePAPR document.
  117. *
  118. * It returns zero if the range parsing has been successful or a standard error
  119. * value if it failed.
  120. */
  121. int of_pci_get_host_bridge_resources(struct device_node *dev,
  122. unsigned char busno, unsigned char bus_max,
  123. struct list_head *resources, resource_size_t *io_base)
  124. {
  125. struct resource *res;
  126. struct resource *bus_range;
  127. struct of_pci_range range;
  128. struct of_pci_range_parser parser;
  129. char range_type[4];
  130. int err;
  131. if (io_base)
  132. *io_base = (resource_size_t)OF_BAD_ADDR;
  133. bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL);
  134. if (!bus_range)
  135. return -ENOMEM;
  136. pr_info("PCI host bridge %s ranges:\n", dev->full_name);
  137. err = of_pci_parse_bus_range(dev, bus_range);
  138. if (err) {
  139. bus_range->start = busno;
  140. bus_range->end = bus_max;
  141. bus_range->flags = IORESOURCE_BUS;
  142. pr_info(" No bus range found for %s, using %pR\n",
  143. dev->full_name, bus_range);
  144. } else {
  145. if (bus_range->end > bus_range->start + bus_max)
  146. bus_range->end = bus_range->start + bus_max;
  147. }
  148. pci_add_resource(resources, bus_range);
  149. /* Check for ranges property */
  150. err = of_pci_range_parser_init(&parser, dev);
  151. if (err)
  152. goto parse_failed;
  153. pr_debug("Parsing ranges property...\n");
  154. for_each_of_pci_range(&parser, &range) {
  155. /* Read next ranges element */
  156. if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
  157. snprintf(range_type, 4, " IO");
  158. else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
  159. snprintf(range_type, 4, "MEM");
  160. else
  161. snprintf(range_type, 4, "err");
  162. pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type,
  163. range.cpu_addr, range.cpu_addr + range.size - 1,
  164. range.pci_addr);
  165. /*
  166. * If we failed translation or got a zero-sized region
  167. * then skip this range
  168. */
  169. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  170. continue;
  171. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  172. if (!res) {
  173. err = -ENOMEM;
  174. goto parse_failed;
  175. }
  176. err = of_pci_range_to_resource(&range, dev, res);
  177. if (err)
  178. goto conversion_failed;
  179. if (resource_type(res) == IORESOURCE_IO) {
  180. if (!io_base) {
  181. pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n",
  182. dev->full_name);
  183. err = -EINVAL;
  184. goto conversion_failed;
  185. }
  186. if (*io_base != (resource_size_t)OF_BAD_ADDR)
  187. pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n",
  188. dev->full_name);
  189. *io_base = range.cpu_addr;
  190. }
  191. pci_add_resource_offset(resources, res, res->start - range.pci_addr);
  192. }
  193. return 0;
  194. conversion_failed:
  195. kfree(res);
  196. parse_failed:
  197. pci_free_resource_list(resources);
  198. return err;
  199. }
  200. EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
  201. #endif /* CONFIG_OF_ADDRESS */
  202. #ifdef CONFIG_PCI_MSI
  203. static LIST_HEAD(of_pci_msi_chip_list);
  204. static DEFINE_MUTEX(of_pci_msi_chip_mutex);
  205. int of_pci_msi_chip_add(struct msi_chip *chip)
  206. {
  207. if (!of_property_read_bool(chip->of_node, "msi-controller"))
  208. return -EINVAL;
  209. mutex_lock(&of_pci_msi_chip_mutex);
  210. list_add(&chip->list, &of_pci_msi_chip_list);
  211. mutex_unlock(&of_pci_msi_chip_mutex);
  212. return 0;
  213. }
  214. EXPORT_SYMBOL_GPL(of_pci_msi_chip_add);
  215. void of_pci_msi_chip_remove(struct msi_chip *chip)
  216. {
  217. mutex_lock(&of_pci_msi_chip_mutex);
  218. list_del(&chip->list);
  219. mutex_unlock(&of_pci_msi_chip_mutex);
  220. }
  221. EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove);
  222. struct msi_chip *of_pci_find_msi_chip_by_node(struct device_node *of_node)
  223. {
  224. struct msi_chip *c;
  225. mutex_lock(&of_pci_msi_chip_mutex);
  226. list_for_each_entry(c, &of_pci_msi_chip_list, list) {
  227. if (c->of_node == of_node) {
  228. mutex_unlock(&of_pci_msi_chip_mutex);
  229. return c;
  230. }
  231. }
  232. mutex_unlock(&of_pci_msi_chip_mutex);
  233. return NULL;
  234. }
  235. EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node);
  236. #endif /* CONFIG_PCI_MSI */