tee_shm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2015-2016, Linaro Limited
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/device.h>
  15. #include <linux/dma-buf.h>
  16. #include <linux/fdtable.h>
  17. #include <linux/idr.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/tee_drv.h>
  21. #include "tee_private.h"
  22. /* extra references appended to shm object for registered shared memory */
  23. struct tee_shm_dmabuf_ref {
  24. struct tee_shm shm;
  25. struct dma_buf *dmabuf;
  26. struct dma_buf_attachment *attach;
  27. struct sg_table *sgt;
  28. };
  29. static void tee_shm_release(struct tee_shm *shm)
  30. {
  31. struct tee_device *teedev = shm->teedev;
  32. mutex_lock(&teedev->mutex);
  33. idr_remove(&teedev->idr, shm->id);
  34. if (shm->ctx)
  35. list_del(&shm->link);
  36. mutex_unlock(&teedev->mutex);
  37. if (shm->flags & TEE_SHM_EXT_DMA_BUF) {
  38. struct tee_shm_dmabuf_ref *ref;
  39. ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
  40. dma_buf_unmap_attachment(ref->attach, ref->sgt,
  41. DMA_BIDIRECTIONAL);
  42. dma_buf_detach(shm->dmabuf, ref->attach);
  43. dma_buf_put(ref->dmabuf);
  44. } else {
  45. struct tee_shm_pool_mgr *poolm;
  46. if (shm->flags & TEE_SHM_DMA_BUF)
  47. poolm = &teedev->pool->dma_buf_mgr;
  48. else
  49. poolm = &teedev->pool->private_mgr;
  50. poolm->ops->free(poolm, shm);
  51. }
  52. kfree(shm);
  53. tee_device_put(teedev);
  54. }
  55. static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
  56. *attach, enum dma_data_direction dir)
  57. {
  58. return NULL;
  59. }
  60. static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
  61. struct sg_table *table,
  62. enum dma_data_direction dir)
  63. {
  64. }
  65. static void tee_shm_op_release(struct dma_buf *dmabuf)
  66. {
  67. struct tee_shm *shm = dmabuf->priv;
  68. tee_shm_release(shm);
  69. }
  70. static void *tee_shm_op_kmap_atomic(struct dma_buf *dmabuf, unsigned long pgnum)
  71. {
  72. return NULL;
  73. }
  74. static void *tee_shm_op_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
  75. {
  76. return NULL;
  77. }
  78. static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
  79. {
  80. struct tee_shm *shm = dmabuf->priv;
  81. size_t size = vma->vm_end - vma->vm_start;
  82. return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
  83. size, vma->vm_page_prot);
  84. }
  85. static struct dma_buf_ops tee_shm_dma_buf_ops = {
  86. .map_dma_buf = tee_shm_op_map_dma_buf,
  87. .unmap_dma_buf = tee_shm_op_unmap_dma_buf,
  88. .release = tee_shm_op_release,
  89. .kmap_atomic = tee_shm_op_kmap_atomic,
  90. .kmap = tee_shm_op_kmap,
  91. .mmap = tee_shm_op_mmap,
  92. };
  93. /**
  94. * tee_shm_alloc() - Allocate shared memory
  95. * @ctx: Context that allocates the shared memory
  96. * @size: Requested size of shared memory
  97. * @flags: Flags setting properties for the requested shared memory.
  98. *
  99. * Memory allocated as global shared memory is automatically freed when the
  100. * TEE file pointer is closed. The @flags field uses the bits defined by
  101. * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
  102. * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
  103. * associated with a dma-buf handle, else driver private memory.
  104. */
  105. struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
  106. {
  107. struct tee_device *teedev = ctx->teedev;
  108. struct tee_shm_pool_mgr *poolm = NULL;
  109. struct tee_shm *shm;
  110. void *ret;
  111. int rc;
  112. if (!(flags & TEE_SHM_MAPPED)) {
  113. dev_err(teedev->dev.parent,
  114. "only mapped allocations supported\n");
  115. return ERR_PTR(-EINVAL);
  116. }
  117. if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
  118. dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
  119. return ERR_PTR(-EINVAL);
  120. }
  121. if (!tee_device_get(teedev))
  122. return ERR_PTR(-EINVAL);
  123. if (!teedev->pool) {
  124. /* teedev has been detached from driver */
  125. ret = ERR_PTR(-EINVAL);
  126. goto err_dev_put;
  127. }
  128. shm = kzalloc(sizeof(*shm), GFP_KERNEL);
  129. if (!shm) {
  130. ret = ERR_PTR(-ENOMEM);
  131. goto err_dev_put;
  132. }
  133. shm->flags = flags;
  134. shm->teedev = teedev;
  135. shm->ctx = ctx;
  136. if (flags & TEE_SHM_DMA_BUF)
  137. poolm = &teedev->pool->dma_buf_mgr;
  138. else
  139. poolm = &teedev->pool->private_mgr;
  140. rc = poolm->ops->alloc(poolm, shm, size);
  141. if (rc) {
  142. ret = ERR_PTR(rc);
  143. goto err_kfree;
  144. }
  145. mutex_lock(&teedev->mutex);
  146. shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
  147. mutex_unlock(&teedev->mutex);
  148. if (shm->id < 0) {
  149. ret = ERR_PTR(shm->id);
  150. goto err_pool_free;
  151. }
  152. if (flags & TEE_SHM_DMA_BUF) {
  153. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  154. exp_info.ops = &tee_shm_dma_buf_ops;
  155. exp_info.size = shm->size;
  156. exp_info.flags = O_RDWR;
  157. exp_info.priv = shm;
  158. shm->dmabuf = dma_buf_export(&exp_info);
  159. if (IS_ERR(shm->dmabuf)) {
  160. ret = ERR_CAST(shm->dmabuf);
  161. goto err_rem;
  162. }
  163. }
  164. mutex_lock(&teedev->mutex);
  165. list_add_tail(&shm->link, &ctx->list_shm);
  166. mutex_unlock(&teedev->mutex);
  167. return shm;
  168. err_rem:
  169. mutex_lock(&teedev->mutex);
  170. idr_remove(&teedev->idr, shm->id);
  171. mutex_unlock(&teedev->mutex);
  172. err_pool_free:
  173. poolm->ops->free(poolm, shm);
  174. err_kfree:
  175. kfree(shm);
  176. err_dev_put:
  177. tee_device_put(teedev);
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(tee_shm_alloc);
  181. struct tee_shm *tee_shm_register_fd(struct tee_context *ctx, int fd)
  182. {
  183. struct tee_shm_dmabuf_ref *ref;
  184. void *rc;
  185. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  186. if (!tee_device_get(ctx->teedev))
  187. return ERR_PTR(-EINVAL);
  188. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  189. if (!ref) {
  190. rc = ERR_PTR(-ENOMEM);
  191. goto err;
  192. }
  193. ref->shm.ctx = ctx;
  194. ref->shm.teedev = ctx->teedev;
  195. ref->shm.id = -1;
  196. ref->dmabuf = dma_buf_get(fd);
  197. if (!ref->dmabuf) {
  198. rc = ERR_PTR(-EINVAL);
  199. goto err;
  200. }
  201. ref->attach = dma_buf_attach(ref->dmabuf, &ref->shm.teedev->dev);
  202. if (IS_ERR_OR_NULL(ref->attach)) {
  203. rc = ERR_PTR(-EINVAL);
  204. goto err;
  205. }
  206. ref->sgt = dma_buf_map_attachment(ref->attach, DMA_BIDIRECTIONAL);
  207. if (IS_ERR_OR_NULL(ref->sgt)) {
  208. rc = ERR_PTR(-EINVAL);
  209. goto err;
  210. }
  211. if (sg_nents(ref->sgt->sgl) != 1) {
  212. rc = ERR_PTR(-EINVAL);
  213. goto err;
  214. }
  215. ref->shm.paddr = sg_dma_address(ref->sgt->sgl);
  216. ref->shm.size = sg_dma_len(ref->sgt->sgl);
  217. ref->shm.flags = TEE_SHM_DMA_BUF | TEE_SHM_EXT_DMA_BUF;
  218. mutex_lock(&ref->shm.teedev->mutex);
  219. ref->shm.id = idr_alloc(&ref->shm.teedev->idr, &ref->shm,
  220. 1, 0, GFP_KERNEL);
  221. mutex_unlock(&ref->shm.teedev->mutex);
  222. if (ref->shm.id < 0) {
  223. rc = ERR_PTR(ref->shm.id);
  224. goto err;
  225. }
  226. /* export a dmabuf to later get a userland ref */
  227. exp_info.ops = &tee_shm_dma_buf_ops;
  228. exp_info.size = ref->shm.size;
  229. exp_info.flags = O_RDWR;
  230. exp_info.priv = &ref->shm;
  231. ref->shm.dmabuf = dma_buf_export(&exp_info);
  232. if (IS_ERR(ref->shm.dmabuf)) {
  233. rc = ERR_PTR(-EINVAL);
  234. goto err;
  235. }
  236. mutex_lock(&ref->shm.teedev->mutex);
  237. list_add_tail(&ref->shm.link, &ctx->list_shm);
  238. mutex_unlock(&ref->shm.teedev->mutex);
  239. return &ref->shm;
  240. err:
  241. if (ref) {
  242. if (ref->shm.id >= 0) {
  243. mutex_lock(&ctx->teedev->mutex);
  244. idr_remove(&ctx->teedev->idr, ref->shm.id);
  245. mutex_unlock(&ctx->teedev->mutex);
  246. }
  247. if (ref->sgt)
  248. dma_buf_unmap_attachment(ref->attach, ref->sgt,
  249. DMA_BIDIRECTIONAL);
  250. if (ref->attach)
  251. dma_buf_detach(ref->dmabuf, ref->attach);
  252. if (ref->dmabuf)
  253. dma_buf_put(ref->dmabuf);
  254. }
  255. kfree(ref);
  256. tee_device_put(ctx->teedev);
  257. return rc;
  258. }
  259. EXPORT_SYMBOL_GPL(tee_shm_register_fd);
  260. /**
  261. * tee_shm_get_fd() - Increase reference count and return file descriptor
  262. * @shm: Shared memory handle
  263. * @returns user space file descriptor to shared memory
  264. */
  265. int tee_shm_get_fd(struct tee_shm *shm)
  266. {
  267. int fd;
  268. if (!(shm->flags & TEE_SHM_DMA_BUF))
  269. return -EINVAL;
  270. fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
  271. if (fd >= 0)
  272. get_dma_buf(shm->dmabuf);
  273. return fd;
  274. }
  275. /**
  276. * tee_shm_free() - Free shared memory
  277. * @shm: Handle to shared memory to free
  278. */
  279. void tee_shm_free(struct tee_shm *shm)
  280. {
  281. /*
  282. * dma_buf_put() decreases the dmabuf reference counter and will
  283. * call tee_shm_release() when the last reference is gone.
  284. *
  285. * In the case of driver private memory we call tee_shm_release
  286. * directly instead as it doesn't have a reference counter.
  287. */
  288. if (shm->flags & TEE_SHM_DMA_BUF)
  289. dma_buf_put(shm->dmabuf);
  290. else
  291. tee_shm_release(shm);
  292. }
  293. EXPORT_SYMBOL_GPL(tee_shm_free);
  294. /**
  295. * tee_shm_va2pa() - Get physical address of a virtual address
  296. * @shm: Shared memory handle
  297. * @va: Virtual address to tranlsate
  298. * @pa: Returned physical address
  299. * @returns 0 on success and < 0 on failure
  300. */
  301. int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
  302. {
  303. if (!(shm->flags & TEE_SHM_MAPPED))
  304. return -EINVAL;
  305. /* Check that we're in the range of the shm */
  306. if ((char *)va < (char *)shm->kaddr)
  307. return -EINVAL;
  308. if ((char *)va >= ((char *)shm->kaddr + shm->size))
  309. return -EINVAL;
  310. return tee_shm_get_pa(
  311. shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
  312. }
  313. EXPORT_SYMBOL_GPL(tee_shm_va2pa);
  314. /**
  315. * tee_shm_pa2va() - Get virtual address of a physical address
  316. * @shm: Shared memory handle
  317. * @pa: Physical address to tranlsate
  318. * @va: Returned virtual address
  319. * @returns 0 on success and < 0 on failure
  320. */
  321. int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
  322. {
  323. if (!(shm->flags & TEE_SHM_MAPPED))
  324. return -EINVAL;
  325. /* Check that we're in the range of the shm */
  326. if (pa < shm->paddr)
  327. return -EINVAL;
  328. if (pa >= (shm->paddr + shm->size))
  329. return -EINVAL;
  330. if (va) {
  331. void *v = tee_shm_get_va(shm, pa - shm->paddr);
  332. if (IS_ERR(v))
  333. return PTR_ERR(v);
  334. *va = v;
  335. }
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(tee_shm_pa2va);
  339. /**
  340. * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
  341. * @shm: Shared memory handle
  342. * @offs: Offset from start of this shared memory
  343. * @returns virtual address of the shared memory + offs if offs is within
  344. * the bounds of this shared memory, else an ERR_PTR
  345. */
  346. void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
  347. {
  348. if (!(shm->flags & TEE_SHM_MAPPED))
  349. return ERR_PTR(-EINVAL);
  350. if (offs >= shm->size)
  351. return ERR_PTR(-EINVAL);
  352. return (char *)shm->kaddr + offs;
  353. }
  354. EXPORT_SYMBOL_GPL(tee_shm_get_va);
  355. /**
  356. * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
  357. * @shm: Shared memory handle
  358. * @offs: Offset from start of this shared memory
  359. * @pa: Physical address to return
  360. * @returns 0 if offs is within the bounds of this shared memory, else an
  361. * error code.
  362. */
  363. int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
  364. {
  365. if (offs >= shm->size)
  366. return -EINVAL;
  367. if (pa)
  368. *pa = shm->paddr + offs;
  369. return 0;
  370. }
  371. EXPORT_SYMBOL_GPL(tee_shm_get_pa);
  372. /**
  373. * tee_shm_get_from_id() - Find shared memory object and increase reference
  374. * count
  375. * @ctx: Context owning the shared memory
  376. * @id: Id of shared memory object
  377. * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
  378. */
  379. struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
  380. {
  381. struct tee_device *teedev;
  382. struct tee_shm *shm;
  383. if (!ctx)
  384. return ERR_PTR(-EINVAL);
  385. teedev = ctx->teedev;
  386. mutex_lock(&teedev->mutex);
  387. shm = idr_find(&teedev->idr, id);
  388. if (!shm || shm->ctx != ctx)
  389. shm = ERR_PTR(-EINVAL);
  390. else if (shm->flags & TEE_SHM_DMA_BUF)
  391. get_dma_buf(shm->dmabuf);
  392. mutex_unlock(&teedev->mutex);
  393. return shm;
  394. }
  395. EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
  396. /**
  397. * tee_shm_get_id() - Get id of a shared memory object
  398. * @shm: Shared memory handle
  399. * @returns id
  400. */
  401. int tee_shm_get_id(struct tee_shm *shm)
  402. {
  403. return shm->id;
  404. }
  405. EXPORT_SYMBOL_GPL(tee_shm_get_id);
  406. /**
  407. * tee_shm_put() - Decrease reference count on a shared memory handle
  408. * @shm: Shared memory handle
  409. */
  410. void tee_shm_put(struct tee_shm *shm)
  411. {
  412. if (shm->flags & TEE_SHM_DMA_BUF)
  413. dma_buf_put(shm->dmabuf);
  414. }
  415. EXPORT_SYMBOL_GPL(tee_shm_put);