device.c 8.5 KB

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