device.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/string.h>
  3. #include <linux/kernel.h>
  4. #include <linux/of.h>
  5. #include <linux/of_device.h>
  6. #include <linux/of_address.h>
  7. #include <linux/of_iommu.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/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/slab.h>
  14. #include <linux/platform_device.h>
  15. #include <asm/errno.h>
  16. #include "of_private.h"
  17. /**
  18. * of_match_device - Tell if a struct device matches an of_device_id list
  19. * @matches: array of of device match structures to search in
  20. * @dev: the of device structure to match against
  21. *
  22. * Used by a driver to check whether an platform_device present in the
  23. * system is in its list of supported devices.
  24. */
  25. const struct of_device_id *of_match_device(const struct of_device_id *matches,
  26. const struct device *dev)
  27. {
  28. if ((!matches) || (!dev->of_node))
  29. return NULL;
  30. return of_match_node(matches, dev->of_node);
  31. }
  32. EXPORT_SYMBOL(of_match_device);
  33. struct platform_device *of_dev_get(struct platform_device *dev)
  34. {
  35. struct device *tmp;
  36. if (!dev)
  37. return NULL;
  38. tmp = get_device(&dev->dev);
  39. if (tmp)
  40. return to_platform_device(tmp);
  41. else
  42. return NULL;
  43. }
  44. EXPORT_SYMBOL(of_dev_get);
  45. void of_dev_put(struct platform_device *dev)
  46. {
  47. if (dev)
  48. put_device(&dev->dev);
  49. }
  50. EXPORT_SYMBOL(of_dev_put);
  51. int of_device_add(struct platform_device *ofdev)
  52. {
  53. BUG_ON(ofdev->dev.of_node == NULL);
  54. /* name and id have to be set so that the platform bus doesn't get
  55. * confused on matching */
  56. ofdev->name = dev_name(&ofdev->dev);
  57. ofdev->id = PLATFORM_DEVID_NONE;
  58. /*
  59. * If this device has not binding numa node in devicetree, that is
  60. * of_node_to_nid returns NUMA_NO_NODE. device_add will assume that this
  61. * device is on the same node as the parent.
  62. */
  63. set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
  64. return device_add(&ofdev->dev);
  65. }
  66. /**
  67. * of_dma_configure - Setup DMA configuration
  68. * @dev: Device to apply DMA configuration
  69. * @np: Pointer to OF node having DMA configuration
  70. * @force_dma: Whether device is to be set up by of_dma_configure() even if
  71. * DMA capability is not explicitly described by firmware.
  72. * @id: Optional const pointer value input id
  73. *
  74. * Try to get devices's DMA configuration from DT and update it
  75. * accordingly.
  76. *
  77. * If platform code needs to use its own special DMA configuration, it
  78. * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
  79. * to fix up DMA configuration.
  80. */
  81. int of_dma_configure_id(struct device *dev, struct device_node *np,
  82. bool force_dma, const u32 *id)
  83. {
  84. const struct iommu_ops *iommu;
  85. const struct bus_dma_region *map = NULL;
  86. u64 dma_start = 0;
  87. u64 mask, end, size = 0;
  88. bool coherent;
  89. int ret;
  90. ret = of_dma_get_range(np, &map);
  91. if (ret < 0) {
  92. /*
  93. * For legacy reasons, we have to assume some devices need
  94. * DMA configuration regardless of whether "dma-ranges" is
  95. * correctly specified or not.
  96. */
  97. if (!force_dma)
  98. return ret == -ENODEV ? 0 : ret;
  99. } else {
  100. const struct bus_dma_region *r = map;
  101. u64 dma_end = 0;
  102. /* Determine the overall bounds of all DMA regions */
  103. for (dma_start = ~0; r->size; r++) {
  104. /* Take lower and upper limits */
  105. if (r->dma_start < dma_start)
  106. dma_start = r->dma_start;
  107. if (r->dma_start + r->size > dma_end)
  108. dma_end = r->dma_start + r->size;
  109. }
  110. size = dma_end - dma_start;
  111. /*
  112. * Add a work around to treat the size as mask + 1 in case
  113. * it is defined in DT as a mask.
  114. */
  115. if (size & 1) {
  116. dev_warn(dev, "Invalid size 0x%llx for dma-range(s)\n",
  117. size);
  118. size = size + 1;
  119. }
  120. if (!size) {
  121. dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
  122. kfree(map);
  123. return -EINVAL;
  124. }
  125. }
  126. /*
  127. * If @dev is expected to be DMA-capable then the bus code that created
  128. * it should have initialised its dma_mask pointer by this point. For
  129. * now, we'll continue the legacy behaviour of coercing it to the
  130. * coherent mask if not, but we'll no longer do so quietly.
  131. */
  132. if (!dev->dma_mask) {
  133. dev_warn(dev, "DMA mask not set\n");
  134. dev->dma_mask = &dev->coherent_dma_mask;
  135. }
  136. if (!size && dev->coherent_dma_mask)
  137. size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
  138. else if (!size)
  139. size = 1ULL << 32;
  140. /*
  141. * Limit coherent and dma mask based on size and default mask
  142. * set by the driver.
  143. */
  144. end = dma_start + size - 1;
  145. mask = DMA_BIT_MASK(ilog2(end) + 1);
  146. dev->coherent_dma_mask &= mask;
  147. *dev->dma_mask &= mask;
  148. /* ...but only set bus limit and range map if we found valid dma-ranges earlier */
  149. if (!ret) {
  150. dev->bus_dma_limit = end;
  151. dev->dma_range_map = map;
  152. }
  153. coherent = of_dma_is_coherent(np);
  154. dev_dbg(dev, "device is%sdma coherent\n",
  155. coherent ? " " : " not ");
  156. iommu = of_iommu_configure(dev, np, id);
  157. if (PTR_ERR(iommu) == -EPROBE_DEFER) {
  158. /* Don't touch range map if it wasn't set from a valid dma-ranges */
  159. if (!ret)
  160. dev->dma_range_map = NULL;
  161. kfree(map);
  162. return -EPROBE_DEFER;
  163. }
  164. dev_dbg(dev, "device is%sbehind an iommu\n",
  165. iommu ? " " : " not ");
  166. arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(of_dma_configure_id);
  170. int of_device_register(struct platform_device *pdev)
  171. {
  172. device_initialize(&pdev->dev);
  173. return of_device_add(pdev);
  174. }
  175. EXPORT_SYMBOL(of_device_register);
  176. void of_device_unregister(struct platform_device *ofdev)
  177. {
  178. device_unregister(&ofdev->dev);
  179. }
  180. EXPORT_SYMBOL(of_device_unregister);
  181. const void *of_device_get_match_data(const struct device *dev)
  182. {
  183. const struct of_device_id *match;
  184. match = of_match_device(dev->driver->of_match_table, dev);
  185. if (!match)
  186. return NULL;
  187. return match->data;
  188. }
  189. EXPORT_SYMBOL(of_device_get_match_data);
  190. static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  191. {
  192. const char *compat;
  193. char *c;
  194. struct property *p;
  195. ssize_t csize;
  196. ssize_t tsize;
  197. if ((!dev) || (!dev->of_node))
  198. return -ENODEV;
  199. /* Name & Type */
  200. /* %p eats all alphanum characters, so %c must be used here */
  201. csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
  202. of_node_get_device_type(dev->of_node));
  203. tsize = csize;
  204. len -= csize;
  205. if (str)
  206. str += csize;
  207. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  208. csize = strlen(compat) + 1;
  209. tsize += csize;
  210. if (csize > len)
  211. continue;
  212. csize = snprintf(str, len, "C%s", compat);
  213. for (c = str; c; ) {
  214. c = strchr(c, ' ');
  215. if (c)
  216. *c++ = '_';
  217. }
  218. len -= csize;
  219. str += csize;
  220. }
  221. return tsize;
  222. }
  223. int of_device_request_module(struct device *dev)
  224. {
  225. char *str;
  226. ssize_t size;
  227. int ret;
  228. size = of_device_get_modalias(dev, NULL, 0);
  229. if (size < 0)
  230. return size;
  231. str = kmalloc(size + 1, GFP_KERNEL);
  232. if (!str)
  233. return -ENOMEM;
  234. of_device_get_modalias(dev, str, size);
  235. str[size] = '\0';
  236. ret = request_module(str);
  237. kfree(str);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL_GPL(of_device_request_module);
  241. /**
  242. * of_device_modalias - Fill buffer with newline terminated modalias string
  243. */
  244. ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
  245. {
  246. ssize_t sl = of_device_get_modalias(dev, str, len - 2);
  247. if (sl < 0)
  248. return sl;
  249. if (sl > len - 2)
  250. return -ENOMEM;
  251. str[sl++] = '\n';
  252. str[sl] = 0;
  253. return sl;
  254. }
  255. EXPORT_SYMBOL_GPL(of_device_modalias);
  256. /**
  257. * of_device_uevent - Display OF related uevent information
  258. */
  259. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  260. {
  261. const char *compat, *type;
  262. struct alias_prop *app;
  263. struct property *p;
  264. int seen = 0;
  265. if ((!dev) || (!dev->of_node))
  266. return;
  267. add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
  268. add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
  269. type = of_node_get_device_type(dev->of_node);
  270. if (type)
  271. add_uevent_var(env, "OF_TYPE=%s", type);
  272. /* Since the compatible field can contain pretty much anything
  273. * it's not really legal to split it out with commas. We split it
  274. * up using a number of environment variables instead. */
  275. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  276. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  277. seen++;
  278. }
  279. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  280. seen = 0;
  281. mutex_lock(&of_mutex);
  282. list_for_each_entry(app, &aliases_lookup, link) {
  283. if (dev->of_node == app->np) {
  284. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  285. app->alias);
  286. seen++;
  287. }
  288. }
  289. mutex_unlock(&of_mutex);
  290. }
  291. int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  292. {
  293. int sl;
  294. if ((!dev) || (!dev->of_node))
  295. return -ENODEV;
  296. /* Devicetree modalias is tricky, we add it in 2 steps */
  297. if (add_uevent_var(env, "MODALIAS="))
  298. return -ENOMEM;
  299. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  300. sizeof(env->buf) - env->buflen);
  301. if (sl >= (sizeof(env->buf) - env->buflen))
  302. return -ENOMEM;
  303. env->buflen += sl;
  304. return 0;
  305. }
  306. EXPORT_SYMBOL_GPL(of_device_uevent_modalias);