of_pci.c 8.1 KB

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