of_pci.c 8.0 KB

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