shm_pool.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2015, Linaro Limited
  3. * Copyright (c) 2017, EPAM Systems
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/device.h>
  16. #include <linux/dma-buf.h>
  17. #include <linux/genalloc.h>
  18. #include <linux/slab.h>
  19. #include <linux/tee_drv.h>
  20. #include "optee_private.h"
  21. #include "optee_smc.h"
  22. #include "shm_pool.h"
  23. static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
  24. struct tee_shm *shm, size_t size)
  25. {
  26. unsigned int order = get_order(size);
  27. struct page *page;
  28. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
  29. if (!page)
  30. return -ENOMEM;
  31. shm->kaddr = page_address(page);
  32. shm->paddr = page_to_phys(page);
  33. shm->size = PAGE_SIZE << order;
  34. return 0;
  35. }
  36. static void pool_op_free(struct tee_shm_pool_mgr *poolm,
  37. struct tee_shm *shm)
  38. {
  39. free_pages((unsigned long)shm->kaddr, get_order(shm->size));
  40. shm->kaddr = NULL;
  41. }
  42. static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
  43. {
  44. kfree(poolm);
  45. }
  46. static const struct tee_shm_pool_mgr_ops pool_ops = {
  47. .alloc = pool_op_alloc,
  48. .free = pool_op_free,
  49. .destroy_poolmgr = pool_op_destroy_poolmgr,
  50. };
  51. /**
  52. * optee_shm_pool_alloc_pages() - create page-based allocator pool
  53. *
  54. * This pool is used when OP-TEE supports dymanic SHM. In this case
  55. * command buffers and such are allocated from kernel's own memory.
  56. */
  57. struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void)
  58. {
  59. struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  60. if (!mgr)
  61. return ERR_PTR(-ENOMEM);
  62. mgr->ops = &pool_ops;
  63. return mgr;
  64. }