device.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. *
  70. * Try to get devices's DMA configuration from DT and update it
  71. * accordingly.
  72. *
  73. * If platform code needs to use its own special DMA configuration, it
  74. * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events
  75. * to fix up DMA configuration.
  76. */
  77. int of_dma_configure(struct device *dev, struct device_node *np)
  78. {
  79. u64 dma_addr, paddr, size = 0;
  80. int ret;
  81. bool coherent;
  82. unsigned long offset;
  83. const struct iommu_ops *iommu;
  84. u64 mask;
  85. ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
  86. if (ret < 0) {
  87. /*
  88. * For legacy reasons, we have to assume some devices need
  89. * DMA configuration regardless of whether "dma-ranges" is
  90. * correctly specified or not.
  91. */
  92. if (!dev->bus->force_dma)
  93. return ret == -ENODEV ? 0 : ret;
  94. dma_addr = offset = 0;
  95. } else {
  96. offset = PFN_DOWN(paddr - dma_addr);
  97. /*
  98. * Add a work around to treat the size as mask + 1 in case
  99. * it is defined in DT as a mask.
  100. */
  101. if (size & 1) {
  102. dev_warn(dev, "Invalid size 0x%llx for dma-range\n",
  103. size);
  104. size = size + 1;
  105. }
  106. if (!size) {
  107. dev_err(dev, "Adjusted size 0x%llx invalid\n", size);
  108. return -EINVAL;
  109. }
  110. dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
  111. }
  112. /*
  113. * Set default coherent_dma_mask to 32 bit. Drivers are expected to
  114. * setup the correct supported mask.
  115. */
  116. if (!dev->coherent_dma_mask)
  117. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  118. /*
  119. * Set it to coherent_dma_mask by default if the architecture
  120. * code has not set it.
  121. */
  122. if (!dev->dma_mask)
  123. dev->dma_mask = &dev->coherent_dma_mask;
  124. if (!size)
  125. size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
  126. dev->dma_pfn_offset = offset;
  127. /*
  128. * Limit coherent and dma mask based on size and default mask
  129. * set by the driver.
  130. */
  131. mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1);
  132. dev->coherent_dma_mask &= mask;
  133. *dev->dma_mask &= mask;
  134. coherent = of_dma_is_coherent(np);
  135. dev_dbg(dev, "device is%sdma coherent\n",
  136. coherent ? " " : " not ");
  137. iommu = of_iommu_configure(dev, np);
  138. if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
  139. return -EPROBE_DEFER;
  140. dev_dbg(dev, "device is%sbehind an iommu\n",
  141. iommu ? " " : " not ");
  142. arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
  143. return 0;
  144. }
  145. EXPORT_SYMBOL_GPL(of_dma_configure);
  146. /**
  147. * of_dma_deconfigure - Clean up DMA configuration
  148. * @dev: Device for which to clean up DMA configuration
  149. *
  150. * Clean up all configuration performed by of_dma_configure_ops() and free all
  151. * resources that have been allocated.
  152. */
  153. void of_dma_deconfigure(struct device *dev)
  154. {
  155. arch_teardown_dma_ops(dev);
  156. }
  157. int of_device_register(struct platform_device *pdev)
  158. {
  159. device_initialize(&pdev->dev);
  160. return of_device_add(pdev);
  161. }
  162. EXPORT_SYMBOL(of_device_register);
  163. void of_device_unregister(struct platform_device *ofdev)
  164. {
  165. device_unregister(&ofdev->dev);
  166. }
  167. EXPORT_SYMBOL(of_device_unregister);
  168. const void *of_device_get_match_data(const struct device *dev)
  169. {
  170. const struct of_device_id *match;
  171. match = of_match_device(dev->driver->of_match_table, dev);
  172. if (!match)
  173. return NULL;
  174. return match->data;
  175. }
  176. EXPORT_SYMBOL(of_device_get_match_data);
  177. static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  178. {
  179. const char *compat;
  180. char *c;
  181. struct property *p;
  182. ssize_t csize;
  183. ssize_t tsize;
  184. if ((!dev) || (!dev->of_node))
  185. return -ENODEV;
  186. /* Name & Type */
  187. csize = snprintf(str, len, "of:N%sT%s", dev->of_node->name,
  188. dev->of_node->type);
  189. tsize = csize;
  190. len -= csize;
  191. if (str)
  192. str += csize;
  193. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  194. csize = strlen(compat) + 1;
  195. tsize += csize;
  196. if (csize > len)
  197. continue;
  198. csize = snprintf(str, len, "C%s", compat);
  199. for (c = str; c; ) {
  200. c = strchr(c, ' ');
  201. if (c)
  202. *c++ = '_';
  203. }
  204. len -= csize;
  205. str += csize;
  206. }
  207. return tsize;
  208. }
  209. int of_device_request_module(struct device *dev)
  210. {
  211. char *str;
  212. ssize_t size;
  213. int ret;
  214. size = of_device_get_modalias(dev, NULL, 0);
  215. if (size < 0)
  216. return size;
  217. str = kmalloc(size + 1, GFP_KERNEL);
  218. if (!str)
  219. return -ENOMEM;
  220. of_device_get_modalias(dev, str, size);
  221. str[size] = '\0';
  222. ret = request_module(str);
  223. kfree(str);
  224. return ret;
  225. }
  226. EXPORT_SYMBOL_GPL(of_device_request_module);
  227. /**
  228. * of_device_modalias - Fill buffer with newline terminated modalias string
  229. */
  230. ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
  231. {
  232. ssize_t sl = of_device_get_modalias(dev, str, len - 2);
  233. if (sl < 0)
  234. return sl;
  235. if (sl > len - 2)
  236. return -ENOMEM;
  237. str[sl++] = '\n';
  238. str[sl] = 0;
  239. return sl;
  240. }
  241. EXPORT_SYMBOL_GPL(of_device_modalias);
  242. /**
  243. * of_device_uevent - Display OF related uevent information
  244. */
  245. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  246. {
  247. const char *compat;
  248. struct alias_prop *app;
  249. struct property *p;
  250. int seen = 0;
  251. if ((!dev) || (!dev->of_node))
  252. return;
  253. add_uevent_var(env, "OF_NAME=%s", dev->of_node->name);
  254. add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
  255. if (dev->of_node->type && strcmp("<NULL>", dev->of_node->type) != 0)
  256. add_uevent_var(env, "OF_TYPE=%s", dev->of_node->type);
  257. /* Since the compatible field can contain pretty much anything
  258. * it's not really legal to split it out with commas. We split it
  259. * up using a number of environment variables instead. */
  260. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  261. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  262. seen++;
  263. }
  264. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  265. seen = 0;
  266. mutex_lock(&of_mutex);
  267. list_for_each_entry(app, &aliases_lookup, link) {
  268. if (dev->of_node == app->np) {
  269. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  270. app->alias);
  271. seen++;
  272. }
  273. }
  274. mutex_unlock(&of_mutex);
  275. }
  276. int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  277. {
  278. int sl;
  279. if ((!dev) || (!dev->of_node))
  280. return -ENODEV;
  281. /* Devicetree modalias is tricky, we add it in 2 steps */
  282. if (add_uevent_var(env, "MODALIAS="))
  283. return -ENOMEM;
  284. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  285. sizeof(env->buf) - env->buflen);
  286. if (sl >= (sizeof(env->buf) - env->buflen))
  287. return -ENOMEM;
  288. env->buflen += sl;
  289. return 0;
  290. }
  291. EXPORT_SYMBOL_GPL(of_device_uevent_modalias);