device.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. * @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))
  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. int of_device_register(struct platform_device *pdev)
  152. {
  153. device_initialize(&pdev->dev);
  154. return of_device_add(pdev);
  155. }
  156. EXPORT_SYMBOL(of_device_register);
  157. void of_device_unregister(struct platform_device *ofdev)
  158. {
  159. device_unregister(&ofdev->dev);
  160. }
  161. EXPORT_SYMBOL(of_device_unregister);
  162. const void *of_device_get_match_data(const struct device *dev)
  163. {
  164. const struct of_device_id *match;
  165. match = of_match_device(dev->driver->of_match_table, dev);
  166. if (!match)
  167. return NULL;
  168. return match->data;
  169. }
  170. EXPORT_SYMBOL(of_device_get_match_data);
  171. static ssize_t of_device_get_modalias(struct device *dev, char *str, ssize_t len)
  172. {
  173. const char *compat;
  174. char *c;
  175. struct property *p;
  176. ssize_t csize;
  177. ssize_t tsize;
  178. if ((!dev) || (!dev->of_node))
  179. return -ENODEV;
  180. /* Name & Type */
  181. /* %p eats all alphanum characters, so %c must be used here */
  182. csize = snprintf(str, len, "of:N%pOFn%c%s", dev->of_node, 'T',
  183. of_node_get_device_type(dev->of_node));
  184. tsize = csize;
  185. len -= csize;
  186. if (str)
  187. str += csize;
  188. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  189. csize = strlen(compat) + 1;
  190. tsize += csize;
  191. if (csize > len)
  192. continue;
  193. csize = snprintf(str, len, "C%s", compat);
  194. for (c = str; c; ) {
  195. c = strchr(c, ' ');
  196. if (c)
  197. *c++ = '_';
  198. }
  199. len -= csize;
  200. str += csize;
  201. }
  202. return tsize;
  203. }
  204. int of_device_request_module(struct device *dev)
  205. {
  206. char *str;
  207. ssize_t size;
  208. int ret;
  209. size = of_device_get_modalias(dev, NULL, 0);
  210. if (size < 0)
  211. return size;
  212. str = kmalloc(size + 1, GFP_KERNEL);
  213. if (!str)
  214. return -ENOMEM;
  215. of_device_get_modalias(dev, str, size);
  216. str[size] = '\0';
  217. ret = request_module(str);
  218. kfree(str);
  219. return ret;
  220. }
  221. EXPORT_SYMBOL_GPL(of_device_request_module);
  222. /**
  223. * of_device_modalias - Fill buffer with newline terminated modalias string
  224. */
  225. ssize_t of_device_modalias(struct device *dev, char *str, ssize_t len)
  226. {
  227. ssize_t sl = of_device_get_modalias(dev, str, len - 2);
  228. if (sl < 0)
  229. return sl;
  230. if (sl > len - 2)
  231. return -ENOMEM;
  232. str[sl++] = '\n';
  233. str[sl] = 0;
  234. return sl;
  235. }
  236. EXPORT_SYMBOL_GPL(of_device_modalias);
  237. /**
  238. * of_device_uevent - Display OF related uevent information
  239. */
  240. void of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
  241. {
  242. const char *compat, *type;
  243. struct alias_prop *app;
  244. struct property *p;
  245. int seen = 0;
  246. if ((!dev) || (!dev->of_node))
  247. return;
  248. add_uevent_var(env, "OF_NAME=%pOFn", dev->of_node);
  249. add_uevent_var(env, "OF_FULLNAME=%pOF", dev->of_node);
  250. type = of_node_get_device_type(dev->of_node);
  251. if (type)
  252. add_uevent_var(env, "OF_TYPE=%s", type);
  253. /* Since the compatible field can contain pretty much anything
  254. * it's not really legal to split it out with commas. We split it
  255. * up using a number of environment variables instead. */
  256. of_property_for_each_string(dev->of_node, "compatible", p, compat) {
  257. add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat);
  258. seen++;
  259. }
  260. add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen);
  261. seen = 0;
  262. mutex_lock(&of_mutex);
  263. list_for_each_entry(app, &aliases_lookup, link) {
  264. if (dev->of_node == app->np) {
  265. add_uevent_var(env, "OF_ALIAS_%d=%s", seen,
  266. app->alias);
  267. seen++;
  268. }
  269. }
  270. mutex_unlock(&of_mutex);
  271. }
  272. int of_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
  273. {
  274. int sl;
  275. if ((!dev) || (!dev->of_node))
  276. return -ENODEV;
  277. /* Devicetree modalias is tricky, we add it in 2 steps */
  278. if (add_uevent_var(env, "MODALIAS="))
  279. return -ENOMEM;
  280. sl = of_device_get_modalias(dev, &env->buf[env->buflen-1],
  281. sizeof(env->buf) - env->buflen);
  282. if (sl >= (sizeof(env->buf) - env->buflen))
  283. return -ENOMEM;
  284. env->buflen += sl;
  285. return 0;
  286. }
  287. EXPORT_SYMBOL_GPL(of_device_uevent_modalias);