device.c 10 KB

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