shm_pool.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright 2019 Advanced Micro Devices, Inc.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/tee_drv.h>
  7. #include <linux/psp-sev.h>
  8. #include "amdtee_private.h"
  9. static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
  10. size_t size, size_t align)
  11. {
  12. unsigned int order = get_order(size);
  13. unsigned long va;
  14. int rc;
  15. /*
  16. * Ignore alignment since this is already going to be page aligned
  17. * and there's no need for any larger alignment.
  18. */
  19. va = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
  20. if (!va)
  21. return -ENOMEM;
  22. shm->kaddr = (void *)va;
  23. shm->paddr = __psp_pa((void *)va);
  24. shm->size = PAGE_SIZE << order;
  25. /* Map the allocated memory in to TEE */
  26. rc = amdtee_map_shmem(shm);
  27. if (rc) {
  28. free_pages(va, order);
  29. shm->kaddr = NULL;
  30. return rc;
  31. }
  32. return 0;
  33. }
  34. static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
  35. {
  36. /* Unmap the shared memory from TEE */
  37. amdtee_unmap_shmem(shm);
  38. free_pages((unsigned long)shm->kaddr, get_order(shm->size));
  39. shm->kaddr = NULL;
  40. }
  41. static void pool_op_destroy_pool(struct tee_shm_pool *pool)
  42. {
  43. kfree(pool);
  44. }
  45. static const struct tee_shm_pool_ops pool_ops = {
  46. .alloc = pool_op_alloc,
  47. .free = pool_op_free,
  48. .destroy_pool = pool_op_destroy_pool,
  49. };
  50. struct tee_shm_pool *amdtee_config_shm(void)
  51. {
  52. struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  53. if (!pool)
  54. return ERR_PTR(-ENOMEM);
  55. pool->ops = &pool_ops;
  56. return pool;
  57. }