of_pci.c 12 KB

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