of_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #define pr_fmt(fmt) "OF: PCI: " fmt
  2. #include <linux/kernel.h>
  3. #include <linux/export.h>
  4. #include <linux/of.h>
  5. #include <linux/of_address.h>
  6. #include <linux/of_device.h>
  7. #include <linux/of_pci.h>
  8. #include <linux/slab.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("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("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 */
  265. /**
  266. * of_pci_map_rid - Translate a requester ID through a downstream mapping.
  267. * @np: root complex device node.
  268. * @rid: PCI requester ID to map.
  269. * @map_name: property name of the map to use.
  270. * @map_mask_name: optional property name of the mask to use.
  271. * @target: optional pointer to a target device node.
  272. * @id_out: optional pointer to receive the translated ID.
  273. *
  274. * Given a PCI requester ID, look up the appropriate implementation-defined
  275. * platform ID and/or the target device which receives transactions on that
  276. * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
  277. * @id_out may be NULL if only the other is required. If @target points to
  278. * a non-NULL device node pointer, only entries targeting that node will be
  279. * matched; if it points to a NULL value, it will receive the device node of
  280. * the first matching target phandle, with a reference held.
  281. *
  282. * Return: 0 on success or a standard error code on failure.
  283. */
  284. int of_pci_map_rid(struct device_node *np, u32 rid,
  285. const char *map_name, const char *map_mask_name,
  286. struct device_node **target, u32 *id_out)
  287. {
  288. u32 map_mask, masked_rid;
  289. int map_len;
  290. const __be32 *map = NULL;
  291. if (!np || !map_name || (!target && !id_out))
  292. return -EINVAL;
  293. map = of_get_property(np, map_name, &map_len);
  294. if (!map) {
  295. if (target)
  296. return -ENODEV;
  297. /* Otherwise, no map implies no translation */
  298. *id_out = rid;
  299. return 0;
  300. }
  301. if (!map_len || map_len % (4 * sizeof(*map))) {
  302. pr_err("%s: Error: Bad %s length: %d\n", np->full_name,
  303. map_name, map_len);
  304. return -EINVAL;
  305. }
  306. /* The default is to select all bits. */
  307. map_mask = 0xffffffff;
  308. /*
  309. * Can be overridden by "{iommu,msi}-map-mask" property.
  310. * If of_property_read_u32() fails, the default is used.
  311. */
  312. if (map_mask_name)
  313. of_property_read_u32(np, map_mask_name, &map_mask);
  314. masked_rid = map_mask & rid;
  315. for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
  316. struct device_node *phandle_node;
  317. u32 rid_base = be32_to_cpup(map + 0);
  318. u32 phandle = be32_to_cpup(map + 1);
  319. u32 out_base = be32_to_cpup(map + 2);
  320. u32 rid_len = be32_to_cpup(map + 3);
  321. if (rid_base & ~map_mask) {
  322. pr_err("%s: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
  323. np->full_name, map_name, map_name,
  324. map_mask, rid_base);
  325. return -EFAULT;
  326. }
  327. if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
  328. continue;
  329. phandle_node = of_find_node_by_phandle(phandle);
  330. if (!phandle_node)
  331. return -ENODEV;
  332. if (target) {
  333. if (*target)
  334. of_node_put(phandle_node);
  335. else
  336. *target = phandle_node;
  337. if (*target != phandle_node)
  338. continue;
  339. }
  340. if (id_out)
  341. *id_out = masked_rid - rid_base + out_base;
  342. pr_debug("%s: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
  343. np->full_name, map_name, map_mask, rid_base, out_base,
  344. rid_len, rid, *id_out);
  345. return 0;
  346. }
  347. pr_err("%s: Invalid %s translation - no match for rid 0x%x on %s\n",
  348. np->full_name, map_name, rid,
  349. target && *target ? (*target)->full_name : "any target");
  350. return -EFAULT;
  351. }