of_pci.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_check_probe_only - Setup probe only mode if linux,pci-probe-only
  104. * is present and valid
  105. */
  106. void of_pci_check_probe_only(void)
  107. {
  108. u32 val;
  109. int ret;
  110. ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
  111. if (ret) {
  112. if (ret == -ENODATA || ret == -EOVERFLOW)
  113. pr_warn("linux,pci-probe-only without valid value, ignoring\n");
  114. return;
  115. }
  116. if (val)
  117. pci_add_flags(PCI_PROBE_ONLY);
  118. else
  119. pci_clear_flags(PCI_PROBE_ONLY);
  120. pr_info("PCI: PROBE_ONLY %sabled\n", val ? "en" : "dis");
  121. }
  122. EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
  123. #if defined(CONFIG_OF_ADDRESS)
  124. /**
  125. * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
  126. * @dev: device node of the host bridge having the range property
  127. * @busno: bus number associated with the bridge root bus
  128. * @bus_max: maximum number of buses for this bridge
  129. * @resources: list where the range of resources will be added after DT parsing
  130. * @io_base: pointer to a variable that will contain on return the physical
  131. * address for the start of the I/O range. Can be NULL if the caller doesn't
  132. * expect IO ranges to be present in the device tree.
  133. *
  134. * It is the caller's job to free the @resources list.
  135. *
  136. * This function will parse the "ranges" property of a PCI host bridge device
  137. * node and setup the resource mapping based on its content. It is expected
  138. * that the property conforms with the Power ePAPR document.
  139. *
  140. * It returns zero if the range parsing has been successful or a standard error
  141. * value if it failed.
  142. */
  143. int of_pci_get_host_bridge_resources(struct device_node *dev,
  144. unsigned char busno, unsigned char bus_max,
  145. struct list_head *resources, resource_size_t *io_base)
  146. {
  147. struct resource_entry *window;
  148. struct resource *res;
  149. struct resource *bus_range;
  150. struct of_pci_range range;
  151. struct of_pci_range_parser parser;
  152. char range_type[4];
  153. int err;
  154. if (io_base)
  155. *io_base = (resource_size_t)OF_BAD_ADDR;
  156. bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL);
  157. if (!bus_range)
  158. return -ENOMEM;
  159. pr_info("PCI host bridge %s ranges:\n", dev->full_name);
  160. err = of_pci_parse_bus_range(dev, bus_range);
  161. if (err) {
  162. bus_range->start = busno;
  163. bus_range->end = bus_max;
  164. bus_range->flags = IORESOURCE_BUS;
  165. pr_info(" No bus range found for %s, using %pR\n",
  166. dev->full_name, bus_range);
  167. } else {
  168. if (bus_range->end > bus_range->start + bus_max)
  169. bus_range->end = bus_range->start + bus_max;
  170. }
  171. pci_add_resource(resources, bus_range);
  172. /* Check for ranges property */
  173. err = of_pci_range_parser_init(&parser, dev);
  174. if (err)
  175. goto parse_failed;
  176. pr_debug("Parsing ranges property...\n");
  177. for_each_of_pci_range(&parser, &range) {
  178. /* Read next ranges element */
  179. if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
  180. snprintf(range_type, 4, " IO");
  181. else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
  182. snprintf(range_type, 4, "MEM");
  183. else
  184. snprintf(range_type, 4, "err");
  185. pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type,
  186. range.cpu_addr, range.cpu_addr + range.size - 1,
  187. range.pci_addr);
  188. /*
  189. * If we failed translation or got a zero-sized region
  190. * then skip this range
  191. */
  192. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  193. continue;
  194. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  195. if (!res) {
  196. err = -ENOMEM;
  197. goto parse_failed;
  198. }
  199. err = of_pci_range_to_resource(&range, dev, res);
  200. if (err) {
  201. kfree(res);
  202. continue;
  203. }
  204. if (resource_type(res) == IORESOURCE_IO) {
  205. if (!io_base) {
  206. pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n",
  207. dev->full_name);
  208. err = -EINVAL;
  209. goto conversion_failed;
  210. }
  211. if (*io_base != (resource_size_t)OF_BAD_ADDR)
  212. pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n",
  213. dev->full_name);
  214. *io_base = range.cpu_addr;
  215. }
  216. pci_add_resource_offset(resources, res, res->start - range.pci_addr);
  217. }
  218. return 0;
  219. conversion_failed:
  220. kfree(res);
  221. parse_failed:
  222. resource_list_for_each_entry(window, resources)
  223. kfree(window->res);
  224. pci_free_resource_list(resources);
  225. return err;
  226. }
  227. EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
  228. #endif /* CONFIG_OF_ADDRESS */
  229. #ifdef CONFIG_PCI_MSI
  230. static LIST_HEAD(of_pci_msi_chip_list);
  231. static DEFINE_MUTEX(of_pci_msi_chip_mutex);
  232. int of_pci_msi_chip_add(struct msi_controller *chip)
  233. {
  234. if (!of_property_read_bool(chip->of_node, "msi-controller"))
  235. return -EINVAL;
  236. mutex_lock(&of_pci_msi_chip_mutex);
  237. list_add(&chip->list, &of_pci_msi_chip_list);
  238. mutex_unlock(&of_pci_msi_chip_mutex);
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(of_pci_msi_chip_add);
  242. void of_pci_msi_chip_remove(struct msi_controller *chip)
  243. {
  244. mutex_lock(&of_pci_msi_chip_mutex);
  245. list_del(&chip->list);
  246. mutex_unlock(&of_pci_msi_chip_mutex);
  247. }
  248. EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove);
  249. struct msi_controller *of_pci_find_msi_chip_by_node(struct device_node *of_node)
  250. {
  251. struct msi_controller *c;
  252. mutex_lock(&of_pci_msi_chip_mutex);
  253. list_for_each_entry(c, &of_pci_msi_chip_list, list) {
  254. if (c->of_node == of_node) {
  255. mutex_unlock(&of_pci_msi_chip_mutex);
  256. return c;
  257. }
  258. }
  259. mutex_unlock(&of_pci_msi_chip_mutex);
  260. return NULL;
  261. }
  262. EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node);
  263. #endif /* CONFIG_PCI_MSI */