tee_shm.c 12 KB

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