msm_dma_iommu_mapping.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, 2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/dma-map-ops.h>
  6. #include <linux/kernel.h>
  7. #include <linux/kref.h>
  8. #include <linux/slab.h>
  9. #include <linux/rbtree.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/err.h>
  13. #include <asm/barrier.h>
  14. #include <linux/msm_dma_iommu_mapping.h>
  15. #include <linux/qcom-dma-mapping.h>
  16. /**
  17. * struct msm_iommu_map - represents a mapping of an ion buffer to an iommu
  18. * @lnode - list node to exist in the buffer's list of iommu mappings
  19. * @dev - Device this is mapped to. Used as key
  20. * @sgl - The scatterlist for this mapping
  21. * @nents - Number of entries in sgl
  22. * @dir - The direction for the map.
  23. * @meta - Backpointer to the meta this guy belongs to.
  24. * @ref - for reference counting this mapping
  25. * @attrs - dma mapping attributes
  26. * @buf_start_addr - address of start of buffer
  27. *
  28. * Represents a mapping of one dma_buf buffer to a particular device
  29. * and address range. There may exist other mappings of this buffer in
  30. * different devices. All mappings will have the same cacheability and security.
  31. */
  32. struct msm_iommu_map {
  33. struct list_head lnode;
  34. struct rb_node node;
  35. struct device *dev;
  36. struct scatterlist *sgl;
  37. unsigned int nents;
  38. enum dma_data_direction dir;
  39. struct msm_iommu_meta *meta;
  40. struct kref ref;
  41. unsigned long attrs;
  42. dma_addr_t buf_start_addr;
  43. };
  44. struct msm_iommu_meta {
  45. struct rb_node node;
  46. struct list_head iommu_maps;
  47. struct kref ref;
  48. struct mutex lock;
  49. void *buffer;
  50. };
  51. static struct rb_root iommu_root;
  52. static DEFINE_MUTEX(msm_iommu_map_mutex);
  53. static void msm_iommu_meta_add(struct msm_iommu_meta *meta)
  54. {
  55. struct rb_root *root = &iommu_root;
  56. struct rb_node **p = &root->rb_node;
  57. struct rb_node *parent = NULL;
  58. struct msm_iommu_meta *entry;
  59. while (*p) {
  60. parent = *p;
  61. entry = rb_entry(parent, struct msm_iommu_meta, node);
  62. if (meta->buffer < entry->buffer)
  63. p = &(*p)->rb_left;
  64. else if (meta->buffer > entry->buffer)
  65. p = &(*p)->rb_right;
  66. else
  67. pr_err("%s: dma_buf %pK already exists\n", __func__,
  68. entry->buffer);
  69. }
  70. rb_link_node(&meta->node, parent, p);
  71. rb_insert_color(&meta->node, root);
  72. }
  73. static struct msm_iommu_meta *msm_iommu_meta_lookup(void *buffer)
  74. {
  75. struct rb_root *root = &iommu_root;
  76. struct rb_node **p = &root->rb_node;
  77. struct rb_node *parent = NULL;
  78. struct msm_iommu_meta *entry = NULL;
  79. while (*p) {
  80. parent = *p;
  81. entry = rb_entry(parent, struct msm_iommu_meta, node);
  82. if (buffer < entry->buffer)
  83. p = &(*p)->rb_left;
  84. else if (buffer > entry->buffer)
  85. p = &(*p)->rb_right;
  86. else
  87. return entry;
  88. }
  89. return NULL;
  90. }
  91. static void msm_iommu_add(struct msm_iommu_meta *meta,
  92. struct msm_iommu_map *iommu)
  93. {
  94. INIT_LIST_HEAD(&iommu->lnode);
  95. list_add(&iommu->lnode, &meta->iommu_maps);
  96. }
  97. static struct msm_iommu_map *msm_iommu_lookup(struct msm_iommu_meta *meta,
  98. struct device *dev)
  99. {
  100. struct msm_iommu_map *entry;
  101. list_for_each_entry(entry, &meta->iommu_maps, lnode) {
  102. if (entry->dev == dev)
  103. return entry;
  104. }
  105. return NULL;
  106. }
  107. static struct msm_iommu_meta *msm_iommu_meta_create(struct dma_buf *dma_buf)
  108. {
  109. struct msm_iommu_meta *meta;
  110. meta = kzalloc(sizeof(*meta), GFP_KERNEL);
  111. if (!meta)
  112. return ERR_PTR(-ENOMEM);
  113. INIT_LIST_HEAD(&meta->iommu_maps);
  114. meta->buffer = dma_buf->priv;
  115. kref_init(&meta->ref);
  116. mutex_init(&meta->lock);
  117. msm_iommu_meta_add(meta);
  118. return meta;
  119. }
  120. static void msm_iommu_meta_put(struct msm_iommu_meta *meta);
  121. static struct scatterlist *clone_sgl(struct scatterlist *sg, int nents)
  122. {
  123. struct scatterlist *next, *s;
  124. int i;
  125. struct sg_table table;
  126. if (sg_alloc_table(&table, nents, GFP_KERNEL))
  127. return NULL;
  128. next = table.sgl;
  129. for_each_sg(sg, s, nents, i) {
  130. *next = *s;
  131. next = sg_next(next);
  132. }
  133. return table.sgl;
  134. }
  135. static inline int __msm_dma_map_sg(struct device *dev, struct scatterlist *sg,
  136. int nents, enum dma_data_direction dir,
  137. struct dma_buf *dma_buf,
  138. unsigned long attrs)
  139. {
  140. struct msm_iommu_map *iommu_map;
  141. struct msm_iommu_meta *iommu_meta = NULL;
  142. int ret = 0;
  143. bool extra_meta_ref_taken = false;
  144. int late_unmap = !(attrs & DMA_ATTR_NO_DELAYED_UNMAP);
  145. mutex_lock(&msm_iommu_map_mutex);
  146. iommu_meta = msm_iommu_meta_lookup(dma_buf->priv);
  147. if (!iommu_meta) {
  148. iommu_meta = msm_iommu_meta_create(dma_buf);
  149. if (IS_ERR(iommu_meta)) {
  150. mutex_unlock(&msm_iommu_map_mutex);
  151. ret = PTR_ERR(iommu_meta);
  152. goto out;
  153. }
  154. if (late_unmap) {
  155. kref_get(&iommu_meta->ref);
  156. extra_meta_ref_taken = true;
  157. }
  158. } else {
  159. kref_get(&iommu_meta->ref);
  160. }
  161. mutex_unlock(&msm_iommu_map_mutex);
  162. mutex_lock(&iommu_meta->lock);
  163. iommu_map = msm_iommu_lookup(iommu_meta, dev);
  164. if (!iommu_map) {
  165. iommu_map = kmalloc(sizeof(*iommu_map), GFP_KERNEL);
  166. if (!iommu_map) {
  167. ret = -ENOMEM;
  168. goto out_unlock;
  169. }
  170. ret = dma_map_sg_attrs(dev, sg, nents, dir, attrs);
  171. if (!ret) {
  172. kfree(iommu_map);
  173. goto out_unlock;
  174. }
  175. iommu_map->sgl = clone_sgl(sg, nents);
  176. if (!iommu_map->sgl) {
  177. kfree(iommu_map);
  178. ret = -ENOMEM;
  179. goto out_unlock;
  180. }
  181. iommu_map->nents = nents;
  182. iommu_map->dev = dev;
  183. iommu_map->dir = dir;
  184. iommu_map->attrs = attrs;
  185. iommu_map->buf_start_addr = sg_phys(sg);
  186. kref_init(&iommu_map->ref);
  187. if (late_unmap)
  188. kref_get(&iommu_map->ref);
  189. iommu_map->meta = iommu_meta;
  190. msm_iommu_add(iommu_meta, iommu_map);
  191. } else {
  192. if (nents == iommu_map->nents &&
  193. dir == iommu_map->dir &&
  194. (attrs & ~DMA_ATTR_SKIP_CPU_SYNC) ==
  195. (iommu_map->attrs & ~DMA_ATTR_SKIP_CPU_SYNC) &&
  196. sg_phys(sg) == iommu_map->buf_start_addr) {
  197. struct scatterlist *sg_tmp = sg;
  198. struct scatterlist *map_sg;
  199. int i;
  200. for_each_sg(iommu_map->sgl, map_sg, nents, i) {
  201. sg_dma_address(sg_tmp) = sg_dma_address(map_sg);
  202. sg_dma_len(sg_tmp) = sg_dma_len(map_sg);
  203. if (sg_dma_len(map_sg) == 0)
  204. break;
  205. sg_tmp = sg_next(sg_tmp);
  206. if (sg_tmp == NULL)
  207. break;
  208. }
  209. kref_get(&iommu_map->ref);
  210. if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
  211. dma_sync_sg_for_device(dev, iommu_map->sgl,
  212. iommu_map->nents, iommu_map->dir);
  213. if (dev_is_dma_coherent(dev))
  214. /*
  215. * Ensure all outstanding changes for coherent
  216. * buffers are applied to the cache before any
  217. * DMA occurs.
  218. */
  219. dmb(ish);
  220. ret = nents;
  221. } else {
  222. bool start_diff = (sg_phys(sg) !=
  223. iommu_map->buf_start_addr);
  224. dev_err(dev, "lazy map request differs:\n"
  225. "req dir:%d, original dir:%d\n"
  226. "req nents:%d, original nents:%d\n"
  227. "req map attrs:%lu, original map attrs:%lu\n"
  228. "req buffer start address differs:%d\n",
  229. dir, iommu_map->dir, nents,
  230. iommu_map->nents, attrs, iommu_map->attrs,
  231. start_diff);
  232. ret = -EINVAL;
  233. }
  234. }
  235. mutex_unlock(&iommu_meta->lock);
  236. return ret;
  237. out_unlock:
  238. mutex_unlock(&iommu_meta->lock);
  239. out:
  240. if (!IS_ERR(iommu_meta)) {
  241. if (extra_meta_ref_taken)
  242. msm_iommu_meta_put(iommu_meta);
  243. msm_iommu_meta_put(iommu_meta);
  244. }
  245. return ret;
  246. }
  247. /*
  248. * We are not taking a reference to the dma_buf here. It is expected that
  249. * clients hold reference to the dma_buf until they are done with mapping and
  250. * unmapping.
  251. */
  252. int msm_dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
  253. enum dma_data_direction dir, struct dma_buf *dma_buf,
  254. unsigned long attrs)
  255. {
  256. int ret;
  257. if (IS_ERR_OR_NULL(dev)) {
  258. pr_err("%s: dev pointer is invalid\n", __func__);
  259. return -EINVAL;
  260. }
  261. if (IS_ERR_OR_NULL(sg)) {
  262. pr_err("%s: sg table pointer is invalid\n", __func__);
  263. return -EINVAL;
  264. }
  265. if (IS_ERR_OR_NULL(dma_buf)) {
  266. pr_err("%s: dma_buf pointer is invalid\n", __func__);
  267. return -EINVAL;
  268. }
  269. ret = __msm_dma_map_sg(dev, sg, nents, dir, dma_buf, attrs);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL(msm_dma_map_sg_attrs);
  273. static void msm_iommu_meta_destroy(struct kref *kref)
  274. {
  275. struct msm_iommu_meta *meta = container_of(kref, struct msm_iommu_meta,
  276. ref);
  277. if (!list_empty(&meta->iommu_maps)) {
  278. WARN(1, "%s: DMA Buffer %pK being destroyed with outstanding iommu mappings!\n",
  279. __func__, meta->buffer);
  280. }
  281. rb_erase(&meta->node, &iommu_root);
  282. kfree(meta);
  283. }
  284. static void msm_iommu_meta_put(struct msm_iommu_meta *meta)
  285. {
  286. /*
  287. * Need to lock here to prevent race against map/unmap
  288. */
  289. mutex_lock(&msm_iommu_map_mutex);
  290. kref_put(&meta->ref, msm_iommu_meta_destroy);
  291. mutex_unlock(&msm_iommu_map_mutex);
  292. }
  293. static void msm_iommu_map_release(struct kref *kref)
  294. {
  295. struct msm_iommu_map *map = container_of(kref, struct msm_iommu_map,
  296. ref);
  297. struct sg_table table;
  298. table.nents = table.orig_nents = map->nents;
  299. table.sgl = map->sgl;
  300. list_del(&map->lnode);
  301. /* Skip an additional cache maintenance on the dma unmap path */
  302. if (!(map->attrs & DMA_ATTR_SKIP_CPU_SYNC))
  303. map->attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  304. dma_unmap_sg_attrs(map->dev, map->sgl, map->nents, map->dir,
  305. map->attrs);
  306. sg_free_table(&table);
  307. kfree(map);
  308. }
  309. void msm_dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sgl,
  310. int nents, enum dma_data_direction dir,
  311. struct dma_buf *dma_buf, unsigned long attrs)
  312. {
  313. struct msm_iommu_map *iommu_map;
  314. struct msm_iommu_meta *meta;
  315. mutex_lock(&msm_iommu_map_mutex);
  316. meta = msm_iommu_meta_lookup(dma_buf->priv);
  317. if (!meta) {
  318. WARN(1, "%s: (%pK) was never mapped\n", __func__, dma_buf);
  319. mutex_unlock(&msm_iommu_map_mutex);
  320. goto out;
  321. }
  322. mutex_unlock(&msm_iommu_map_mutex);
  323. mutex_lock(&meta->lock);
  324. iommu_map = msm_iommu_lookup(meta, dev);
  325. if (!iommu_map) {
  326. WARN(1, "%s: (%pK) was never mapped for device %p\n", __func__,
  327. dma_buf, dev);
  328. mutex_unlock(&meta->lock);
  329. goto out;
  330. }
  331. if (dir != iommu_map->dir)
  332. WARN(1, "%s: (%pK) dir:%d differs from original dir:%d\n",
  333. __func__, dma_buf, dir, iommu_map->dir);
  334. if (attrs && ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0))
  335. dma_sync_sg_for_cpu(dev, iommu_map->sgl, iommu_map->nents, dir);
  336. iommu_map->attrs = attrs;
  337. kref_put(&iommu_map->ref, msm_iommu_map_release);
  338. mutex_unlock(&meta->lock);
  339. msm_iommu_meta_put(meta);
  340. out:
  341. return;
  342. }
  343. EXPORT_SYMBOL(msm_dma_unmap_sg_attrs);
  344. int msm_dma_unmap_all_for_dev(struct device *dev)
  345. {
  346. int ret = 0;
  347. struct msm_iommu_meta *meta;
  348. struct rb_root *root;
  349. struct rb_node *meta_node;
  350. mutex_lock(&msm_iommu_map_mutex);
  351. root = &iommu_root;
  352. meta_node = rb_first(root);
  353. while (meta_node) {
  354. struct msm_iommu_map *iommu_map;
  355. struct msm_iommu_map *iommu_map_next;
  356. meta = rb_entry(meta_node, struct msm_iommu_meta, node);
  357. mutex_lock(&meta->lock);
  358. list_for_each_entry_safe(iommu_map, iommu_map_next,
  359. &meta->iommu_maps, lnode)
  360. if (iommu_map->dev == dev)
  361. if (!kref_put(&iommu_map->ref,
  362. msm_iommu_map_release))
  363. ret = -EINVAL;
  364. mutex_unlock(&meta->lock);
  365. meta_node = rb_next(meta_node);
  366. }
  367. mutex_unlock(&msm_iommu_map_mutex);
  368. return ret;
  369. }
  370. EXPORT_SYMBOL(msm_dma_unmap_all_for_dev);
  371. /*
  372. * Only to be called by ION code when a buffer is freed
  373. */
  374. void msm_dma_buf_freed(void *buffer)
  375. {
  376. struct msm_iommu_map *iommu_map;
  377. struct msm_iommu_map *iommu_map_next;
  378. struct msm_iommu_meta *meta;
  379. mutex_lock(&msm_iommu_map_mutex);
  380. meta = msm_iommu_meta_lookup(buffer);
  381. if (!meta) {
  382. /* Already unmapped (assuming no late unmapping) */
  383. mutex_unlock(&msm_iommu_map_mutex);
  384. return;
  385. }
  386. mutex_unlock(&msm_iommu_map_mutex);
  387. mutex_lock(&meta->lock);
  388. list_for_each_entry_safe(iommu_map, iommu_map_next, &meta->iommu_maps,
  389. lnode)
  390. kref_put(&iommu_map->ref, msm_iommu_map_release);
  391. if (!list_empty(&meta->iommu_maps)) {
  392. WARN(1, "%s: DMA buffer %pK destroyed with outstanding iommu mappings\n",
  393. __func__, meta->buffer);
  394. }
  395. INIT_LIST_HEAD(&meta->iommu_maps);
  396. mutex_unlock(&meta->lock);
  397. msm_iommu_meta_put(meta);
  398. }
  399. EXPORT_SYMBOL(msm_dma_buf_freed);
  400. MODULE_LICENSE("GPL v2");