device.c 8.8 KB

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