of_pci.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. u32 reg[5];
  54. int error;
  55. error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
  56. if (error)
  57. return error;
  58. return (reg[0] >> 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. u32 bus_range[2];
  71. int error;
  72. error = of_property_read_u32_array(node, "bus-range", bus_range,
  73. ARRAY_SIZE(bus_range));
  74. if (error)
  75. return error;
  76. res->name = node->name;
  77. res->start = bus_range[0];
  78. res->end = bus_range[1];
  79. res->flags = IORESOURCE_BUS;
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
  83. /**
  84. * This function will try to obtain the host bridge domain number by
  85. * finding a property called "linux,pci-domain" of the given device node.
  86. *
  87. * @node: device tree node with the domain information
  88. *
  89. * Returns the associated domain number from DT in the range [0-0xffff], or
  90. * a negative value if the required property is not found.
  91. */
  92. int of_get_pci_domain_nr(struct device_node *node)
  93. {
  94. u32 domain;
  95. int error;
  96. error = of_property_read_u32(node, "linux,pci-domain", &domain);
  97. if (error)
  98. return error;
  99. return (u16)domain;
  100. }
  101. EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
  102. /**
  103. * This function will try to find the limitation of link speed by finding
  104. * a property called "max-link-speed" of the given device node.
  105. *
  106. * @node: device tree node with the max link speed information
  107. *
  108. * Returns the associated max link speed from DT, or a negative value if the
  109. * required property is not found or is invalid.
  110. */
  111. int of_pci_get_max_link_speed(struct device_node *node)
  112. {
  113. u32 max_link_speed;
  114. if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
  115. max_link_speed > 4)
  116. return -EINVAL;
  117. return max_link_speed;
  118. }
  119. EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
  120. /**
  121. * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
  122. * is present and valid
  123. */
  124. void of_pci_check_probe_only(void)
  125. {
  126. u32 val;
  127. int ret;
  128. ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
  129. if (ret) {
  130. if (ret == -ENODATA || ret == -EOVERFLOW)
  131. pr_warn("linux,pci-probe-only without valid value, ignoring\n");
  132. return;
  133. }
  134. if (val)
  135. pci_add_flags(PCI_PROBE_ONLY);
  136. else
  137. pci_clear_flags(PCI_PROBE_ONLY);
  138. pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis");
  139. }
  140. EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
  141. #if defined(CONFIG_OF_ADDRESS)
  142. /**
  143. * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
  144. * @dev: device node of the host bridge having the range property
  145. * @busno: bus number associated with the bridge root bus
  146. * @bus_max: maximum number of buses for this bridge
  147. * @resources: list where the range of resources will be added after DT parsing
  148. * @io_base: pointer to a variable that will contain on return the physical
  149. * address for the start of the I/O range. Can be NULL if the caller doesn't
  150. * expect IO ranges to be present in the device tree.
  151. *
  152. * It is the caller's job to free the @resources list.
  153. *
  154. * This function will parse the "ranges" property of a PCI host bridge device
  155. * node and setup the resource mapping based on its content. It is expected
  156. * that the property conforms with the Power ePAPR document.
  157. *
  158. * It returns zero if the range parsing has been successful or a standard error
  159. * value if it failed.
  160. */
  161. int of_pci_get_host_bridge_resources(struct device_node *dev,
  162. unsigned char busno, unsigned char bus_max,
  163. struct list_head *resources, resource_size_t *io_base)
  164. {
  165. struct resource_entry *window;
  166. struct resource *res;
  167. struct resource *bus_range;
  168. struct of_pci_range range;
  169. struct of_pci_range_parser parser;
  170. char range_type[4];
  171. int err;
  172. if (io_base)
  173. *io_base = (resource_size_t)OF_BAD_ADDR;
  174. bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL);
  175. if (!bus_range)
  176. return -ENOMEM;
  177. pr_info("host bridge %pOF ranges:\n", dev);
  178. err = of_pci_parse_bus_range(dev, bus_range);
  179. if (err) {
  180. bus_range->start = busno;
  181. bus_range->end = bus_max;
  182. bus_range->flags = IORESOURCE_BUS;
  183. pr_info(" No bus range found for %pOF, using %pR\n",
  184. dev, bus_range);
  185. } else {
  186. if (bus_range->end > bus_range->start + bus_max)
  187. bus_range->end = bus_range->start + bus_max;
  188. }
  189. pci_add_resource(resources, bus_range);
  190. /* Check for ranges property */
  191. err = of_pci_range_parser_init(&parser, dev);
  192. if (err)
  193. goto parse_failed;
  194. pr_debug("Parsing ranges property...\n");
  195. for_each_of_pci_range(&parser, &range) {
  196. /* Read next ranges element */
  197. if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
  198. snprintf(range_type, 4, " IO");
  199. else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
  200. snprintf(range_type, 4, "MEM");
  201. else
  202. snprintf(range_type, 4, "err");
  203. pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type,
  204. range.cpu_addr, range.cpu_addr + range.size - 1,
  205. range.pci_addr);
  206. /*
  207. * If we failed translation or got a zero-sized region
  208. * then skip this range
  209. */
  210. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  211. continue;
  212. res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  213. if (!res) {
  214. err = -ENOMEM;
  215. goto parse_failed;
  216. }
  217. err = of_pci_range_to_resource(&range, dev, res);
  218. if (err) {
  219. kfree(res);
  220. continue;
  221. }
  222. if (resource_type(res) == IORESOURCE_IO) {
  223. if (!io_base) {
  224. pr_err("I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
  225. dev);
  226. err = -EINVAL;
  227. goto conversion_failed;
  228. }
  229. if (*io_base != (resource_size_t)OF_BAD_ADDR)
  230. pr_warn("More than one I/O resource converted for %pOF. CPU base address for old range lost!\n",
  231. dev);
  232. *io_base = range.cpu_addr;
  233. }
  234. pci_add_resource_offset(resources, res, res->start - range.pci_addr);
  235. }
  236. return 0;
  237. conversion_failed:
  238. kfree(res);
  239. parse_failed:
  240. resource_list_for_each_entry(window, resources)
  241. kfree(window->res);
  242. pci_free_resource_list(resources);
  243. return err;
  244. }
  245. EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
  246. #endif /* CONFIG_OF_ADDRESS */
  247. /**
  248. * of_pci_map_rid - Translate a requester ID through a downstream mapping.
  249. * @np: root complex device node.
  250. * @rid: PCI requester ID to map.
  251. * @map_name: property name of the map to use.
  252. * @map_mask_name: optional property name of the mask to use.
  253. * @target: optional pointer to a target device node.
  254. * @id_out: optional pointer to receive the translated ID.
  255. *
  256. * Given a PCI requester ID, look up the appropriate implementation-defined
  257. * platform ID and/or the target device which receives transactions on that
  258. * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
  259. * @id_out may be NULL if only the other is required. If @target points to
  260. * a non-NULL device node pointer, only entries targeting that node will be
  261. * matched; if it points to a NULL value, it will receive the device node of
  262. * the first matching target phandle, with a reference held.
  263. *
  264. * Return: 0 on success or a standard error code on failure.
  265. */
  266. int of_pci_map_rid(struct device_node *np, u32 rid,
  267. const char *map_name, const char *map_mask_name,
  268. struct device_node **target, u32 *id_out)
  269. {
  270. u32 map_mask, masked_rid;
  271. int map_len;
  272. const __be32 *map = NULL;
  273. if (!np || !map_name || (!target && !id_out))
  274. return -EINVAL;
  275. map = of_get_property(np, map_name, &map_len);
  276. if (!map) {
  277. if (target)
  278. return -ENODEV;
  279. /* Otherwise, no map implies no translation */
  280. *id_out = rid;
  281. return 0;
  282. }
  283. if (!map_len || map_len % (4 * sizeof(*map))) {
  284. pr_err("%pOF: Error: Bad %s length: %d\n", np,
  285. map_name, map_len);
  286. return -EINVAL;
  287. }
  288. /* The default is to select all bits. */
  289. map_mask = 0xffffffff;
  290. /*
  291. * Can be overridden by "{iommu,msi}-map-mask" property.
  292. * If of_property_read_u32() fails, the default is used.
  293. */
  294. if (map_mask_name)
  295. of_property_read_u32(np, map_mask_name, &map_mask);
  296. masked_rid = map_mask & rid;
  297. for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
  298. struct device_node *phandle_node;
  299. u32 rid_base = be32_to_cpup(map + 0);
  300. u32 phandle = be32_to_cpup(map + 1);
  301. u32 out_base = be32_to_cpup(map + 2);
  302. u32 rid_len = be32_to_cpup(map + 3);
  303. if (rid_base & ~map_mask) {
  304. pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
  305. np, map_name, map_name,
  306. map_mask, rid_base);
  307. return -EFAULT;
  308. }
  309. if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
  310. continue;
  311. phandle_node = of_find_node_by_phandle(phandle);
  312. if (!phandle_node)
  313. return -ENODEV;
  314. if (target) {
  315. if (*target)
  316. of_node_put(phandle_node);
  317. else
  318. *target = phandle_node;
  319. if (*target != phandle_node)
  320. continue;
  321. }
  322. if (id_out)
  323. *id_out = masked_rid - rid_base + out_base;
  324. pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
  325. np, map_name, map_mask, rid_base, out_base,
  326. rid_len, rid, *id_out);
  327. return 0;
  328. }
  329. pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
  330. np, map_name, rid, target && *target ? *target : NULL);
  331. return -EFAULT;
  332. }