tee_shm.c 12 KB

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