device.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/of.h>
  4. #include <linux/of_device.h>
  5. #include <linux/of_address.h>
  6. #include <linux/of_iommu.h>
  7. #include <linux/of_reserved_mem.h>
  8. #include <linux/dma-direct.h> /* for bus_dma_region */
  9. #include <linux/dma-map-ops.h>
  10. #include <linux/init.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/slab.h>
  13. #include <linux/platform_device.h>
  14. #include <asm/errno.h>
  15. #include "of_private.h"
  16. /**
  17. * of_match_device - Tell if a struct device matches an of_device_id list
  18. * @matches: array of of_device_id match structures to search in
  19. * @dev: the OF device structure to match against
  20. *
  21. * Used by a driver to check whether an platform_device present in the
  22. * system is in its list of supported devices.
  23. */
  24. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  25. const struct device *dev)
  26. {
  27. if (!matches || !dev->of_node || dev->of_node_reused)
  28. return NULL;
  29. return of_match_node(matches, dev->of_node);
  30. }
  31. EXPORT_SYMBOL(of_match_device);
  32. static void
  33. of_dma_set_restricted_buffer(struct device *dev, struct device_node *np)
  34. {
  35. struct device_node *of_node = dev->of_node;
  36. struct of_phandle_iterator it;
  37. int rc, i = 0;
  38. if (!IS_ENABLED(CONFIG_DMA_RESTRICTED_POOL))
  39. return;
  40. /*
  41. * If dev->of_node doesn't exist or doesn't contain memory-region, try
  42. * the OF node having DMA configuration.
  43. */
  44. if (!of_property_present(of_node, "memory-region"))
  45. of_node = np;
  46. of_for_each_phandle(&it, rc, of_node, "memory-region", NULL, 0) {
  47. /*
  48. * There might be multiple memory regions, but only one
  49. * restricted-dma-pool region is allowed.
  50. */
  51. if (of_device_is_compatible(it.node, "restricted-dma-pool") &&
  52. of_device_is_available(it.node)) {
  53. if (of_reserved_mem_device_init_by_idx(dev, of_node, i))
  54. dev_warn(dev, "failed to initialise \"restricted-dma-pool\" memory node\n");
  55. of_node_put(it.node);
  56. break;
  57. }
  58. i++;
  59. }
  60. }
  61. /**
  62. * of_dma_configure_id - Setup DMA configuration
  63. * @dev: Device to apply DMA configuration
  64. * @np: Pointer to OF node having DMA configuration
  65. * @force_dma: Whether device is to be set up by of_dma_configure() even if
  66. * DMA capability is not explicitly described by firmware.
  67. * @id: Optional const pointer value input id
  68. *
  69. * Try to get devices's DMA configuration from DT and update it
  70. * accordingly.
  71. *
  72. * If platform code needs to use its own special DMA configuration, it
  73. * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
  74. * to fix up DMA configuration.
  75. */
  76. int of_dma_configure_id(struct device *dev, struct device_node *np,
  77. bool force_dma, const u32 *id)
  78. {
  79. const struct bus_dma_region *map = NULL;
  80. struct device_node *bus_np;
  81. u64 mask, end = 0;
  82. bool coherent, set_map = false;
  83. int ret;
  84. if (dev->dma_range_map) {
  85. dev_dbg(dev, "dma_range_map already set\n");
  86. goto skip_map;
  87. }
  88. if (np == dev->of_node)
  89. bus_np = __of_get_dma_parent(np);
  90. else
  91. bus_np = of_node_get(np);
  92. ret = of_dma_get_range(bus_np, &map);
  93. of_node_put(bus_np);
  94. if (ret < 0) {
  95. /*
  96. * For legacy reasons, we have to assume some devices need
  97. * DMA configuration regardless of whether "dma-ranges" is
  98. * correctly specified or not.
  99. */
  100. if (!force_dma)
  101. return ret == -ENODEV ? 0 : ret;
  102. } else {
  103. /* Determine the overall bounds of all DMA regions */
  104. end = dma_range_map_max(map);
  105. set_map = true;
  106. }
  107. skip_map:
  108. /*
  109. * If @dev is expected to be DMA-capable then the bus code that created
  110. * it should have initialised its dma_mask pointer by this point. For
  111. * now, we'll continue the legacy behaviour of coercing it to the
  112. * coherent mask if not, but we'll no longer do so quietly.
  113. */
  114. if (!dev->dma_mask) {
  115. dev_warn(dev, "DMA mask not set\n");
  116. dev->dma_mask = &dev->coherent_dma_mask;
  117. }
  118. if (!end && dev->coherent_dma_mask)
  119. end = dev->coherent_dma_mask;
  120. else if (!end)
  121. end = (1ULL << 32) - 1;
  122. /*
  123. * Limit coherent and dma mask based on size and default mask
  124. * set by the driver.
  125. */
  126. mask = DMA_BIT_MASK(ilog2(end) + 1);
  127. dev->coherent_dma_mask &= mask;
  128. *dev->dma_mask &= mask;
  129. /* ...but only set bus limit and range map if we found valid dma-ranges earlier */
  130. if (set_map) {
  131. dev->bus_dma_limit = end;
  132. dev->dma_range_map = map;
  133. }
  134. coherent = of_dma_is_coherent(np);
  135. dev_dbg(dev, "device is%sdma coherent\n",
  136. coherent ? " " : " not ");
  137. ret = of_iommu_configure(dev, np, id);
  138. if (ret == -EPROBE_DEFER) {
  139. /* Don't touch range map if it wasn't set from a valid dma-ranges */
  140. if (set_map)
  141. dev->dma_range_map = NULL;
  142. kfree(map);
  143. return -EPROBE_DEFER;
  144. }
  145. /* Take all other IOMMU errors to mean we'll just carry on without it */
  146. dev_dbg(dev, "device is%sbehind an iommu\n",
  147. !ret ? " " : " not ");
  148. arch_setup_dma_ops(dev, coherent);
  149. if (ret)
  150. of_dma_set_restricted_buffer(dev, np);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(of_dma_configure_id);
  154. const void *of_device_get_match_data(const struct device *dev)
  155. {
  156. const struct of_device_id *match;
  157. match = of_match_device(dev->driver->of_match_table, dev);
  158. if (!match)
  159. return NULL;
  160. return match->data;
  161. }
  162. EXPORT_SYMBOL(of_device_get_match_data);
  163. /**
  164. * of_device_modalias - Fill buffer with newline terminated modalias string
  165. * @dev: Calling device
  166. * @str: Modalias string
  167. * @len: Size of @str
  168. */
  169. ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
  170. {
  171. ssize_t sl;
  172. if (!dev || !dev->of_node || dev->of_node_reused)
  173. return -ENODEV;
  174. sl = of_modalias(dev->of_node, str, len - 2);
  175. if (sl < 0)
  176. return sl;
  177. if (sl > len - 2)
  178. return -ENOMEM;
  179. str[sl++] = '\n';
  180. str[sl] = 0;
  181. return sl;
  182. }
  183. EXPORT_SYMBOL_GPL(of_device_modalias);
  184. /**
  185. * of_device_uevent - Display OF related uevent information
  186. * @dev: Device to display the uevent information for
  187. * @env: Kernel object's userspace event reference to fill up
  188. */
  189. void of_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
  190. {
  191. const char *compat, *type;
  192. struct alias_prop *app;
  193. struct property *p;
  194. int seen = 0;
  195. if ((!dev) || (!dev->of_node))
  196. return;
  197. add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
  198. add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
  199. type = of_node_get_device_type(dev->of_node);
  200. if (type)
  201. add_uevent_var(env, "OF_TYPE=%s", type);
  202. /* Since the compatible field can contain pretty much anything
  203. * it's not really legal to split it out with commas. We split it
  204. * up using a number of environment variables instead. */
  205. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  206. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  207. seen++;
  208. }
  209. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  210. seen = 0;
  211. mutex_lock(&of_mutex);
  212. list_for_each_entry(app, &aliases_lookup, link) {
  213. if (dev->of_node == app->np) {
  214. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  215. app->alias);
  216. seen++;
  217. }
  218. }
  219. mutex_unlock(&of_mutex);
  220. }
  221. EXPORT_SYMBOL_GPL(of_device_uevent);
  222. int of_device_uevent_modalias(const struct device *dev, struct kobj_uevent_env *env)
  223. {
  224. int sl;
  225. if ((!dev) || (!dev->of_node) || dev->of_node_reused)
  226. return -ENODEV;
  227. /* Devicetree modalias is tricky, we add it in 2 steps */
  228. if (add_uevent_var(env, "MODALIAS="))
  229. return -ENOMEM;
  230. sl = of_modalias(dev->of_node, &env->buf[env->buflen-1],
  231. sizeof(env->buf) - env->buflen);
  232. if (sl < 0)
  233. return sl;
  234. if (sl >= (sizeof(env->buf) - env->buflen))
  235. return -ENOMEM;
  236. env->buflen += sl;
  237. return 0;
  238. }
  239. EXPORT_SYMBOL_GPL(of_device_uevent_modalias);
  240. /**
  241. * of_device_make_bus_id - Use the device node data to assign a unique name
  242. * @dev: pointer to device structure that is linked to a device tree node
  243. *
  244. * This routine will first try using the translated bus address to
  245. * derive a unique name. If it cannot, then it will prepend names from
  246. * parent nodes until a unique name can be derived.
  247. */
  248. void of_device_make_bus_id(struct device *dev)
  249. {
  250. struct device_node *node = dev->of_node;
  251. const __be32 *reg;
  252. u64 addr;
  253. u32 mask;
  254. /* Construct the name, using parent nodes if necessary to ensure uniqueness */
  255. while (node->parent) {
  256. /*
  257. * If the address can be translated, then that is as much
  258. * uniqueness as we need. Make it the first component and return
  259. */
  260. reg = of_get_property(node, "reg", NULL);
  261. if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
  262. if (!of_property_read_u32(node, "mask", &mask))
  263. dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
  264. addr, ffs(mask) - 1, node, dev_name(dev));
  265. else
  266. dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
  267. addr, node, dev_name(dev));
  268. return;
  269. }
  270. /* format arguments only used if dev_name() resolves to NULL */
  271. dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
  272. kbasename(node->full_name), dev_name(dev));
  273. node = node->parent;
  274. }
  275. }
  276. EXPORT_SYMBOL_GPL(of_device_make_bus_id);