tee-uclass.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018-2020 Linaro Limited
  4. */
  5. #define LOG_CATEGORY UCLASS_TEE
  6. #include <common.h>
  7. #include <cpu_func.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <tee.h>
  12. #include <asm/cache.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/uclass-internal.h>
  15. /**
  16. * struct tee_uclass_priv - information of a TEE, stored by the uclass
  17. *
  18. * @list_shm: list of structe tee_shm representing memory blocks shared
  19. * with the TEE.
  20. */
  21. struct tee_uclass_priv {
  22. struct list_head list_shm;
  23. };
  24. static const struct tee_driver_ops *tee_get_ops(struct udevice *dev)
  25. {
  26. return device_get_ops(dev);
  27. }
  28. void tee_get_version(struct udevice *dev, struct tee_version_data *vers)
  29. {
  30. tee_get_ops(dev)->get_version(dev, vers);
  31. }
  32. int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
  33. uint num_param, struct tee_param *param)
  34. {
  35. return tee_get_ops(dev)->open_session(dev, arg, num_param, param);
  36. }
  37. int tee_close_session(struct udevice *dev, u32 session)
  38. {
  39. return tee_get_ops(dev)->close_session(dev, session);
  40. }
  41. int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
  42. uint num_param, struct tee_param *param)
  43. {
  44. return tee_get_ops(dev)->invoke_func(dev, arg, num_param, param);
  45. }
  46. int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
  47. u32 flags, struct tee_shm **shmp)
  48. {
  49. struct tee_shm *shm;
  50. void *p = addr;
  51. int rc;
  52. if (flags & TEE_SHM_ALLOC) {
  53. if (align)
  54. p = memalign(align, size);
  55. else
  56. p = malloc(size);
  57. }
  58. if (!p)
  59. return -ENOMEM;
  60. shm = calloc(1, sizeof(*shm));
  61. if (!shm) {
  62. rc = -ENOMEM;
  63. goto err;
  64. }
  65. shm->dev = dev;
  66. shm->addr = p;
  67. shm->size = size;
  68. shm->flags = flags;
  69. if (flags & TEE_SHM_SEC_REGISTER) {
  70. rc = tee_get_ops(dev)->shm_register(dev, shm);
  71. if (rc)
  72. goto err;
  73. }
  74. if (flags & TEE_SHM_REGISTER) {
  75. struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
  76. list_add(&shm->link, &priv->list_shm);
  77. }
  78. *shmp = shm;
  79. return 0;
  80. err:
  81. free(shm);
  82. if (flags & TEE_SHM_ALLOC)
  83. free(p);
  84. return rc;
  85. }
  86. int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
  87. struct tee_shm **shmp)
  88. {
  89. u32 f = flags;
  90. f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER | TEE_SHM_ALLOC;
  91. return __tee_shm_add(dev, 0, NULL, size, f, shmp);
  92. }
  93. int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
  94. struct tee_shm **shmp)
  95. {
  96. u32 f = flags & ~TEE_SHM_ALLOC;
  97. f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER;
  98. return __tee_shm_add(dev, 0, addr, size, f, shmp);
  99. }
  100. void tee_shm_free(struct tee_shm *shm)
  101. {
  102. if (!shm)
  103. return;
  104. if (shm->flags & TEE_SHM_SEC_REGISTER)
  105. tee_get_ops(shm->dev)->shm_unregister(shm->dev, shm);
  106. if (shm->flags & TEE_SHM_REGISTER)
  107. list_del(&shm->link);
  108. if (shm->flags & TEE_SHM_ALLOC)
  109. free(shm->addr);
  110. free(shm);
  111. }
  112. bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev)
  113. {
  114. struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
  115. struct tee_shm *s;
  116. list_for_each_entry(s, &priv->list_shm, link)
  117. if (s == shm)
  118. return true;
  119. return false;
  120. }
  121. struct udevice *tee_find_device(struct udevice *start,
  122. int (*match)(struct tee_version_data *vers,
  123. const void *data),
  124. const void *data,
  125. struct tee_version_data *vers)
  126. {
  127. struct udevice *dev = start;
  128. struct tee_version_data lv;
  129. struct tee_version_data *v = vers ? vers : &lv;
  130. if (!dev)
  131. uclass_find_first_device(UCLASS_TEE, &dev);
  132. else
  133. uclass_find_next_device(&dev);
  134. for (; dev; uclass_find_next_device(&dev)) {
  135. if (device_probe(dev))
  136. continue;
  137. tee_get_ops(dev)->get_version(dev, v);
  138. if (!match || match(v, data))
  139. return dev;
  140. }
  141. return NULL;
  142. }
  143. static int tee_pre_probe(struct udevice *dev)
  144. {
  145. struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
  146. INIT_LIST_HEAD(&priv->list_shm);
  147. return 0;
  148. }
  149. static int tee_pre_remove(struct udevice *dev)
  150. {
  151. struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
  152. struct tee_shm *shm;
  153. /*
  154. * Any remaining shared memory must be unregistered now as U-Boot
  155. * is about to hand over to the next stage and that memory will be
  156. * reused.
  157. */
  158. while (!list_empty(&priv->list_shm)) {
  159. shm = list_first_entry(&priv->list_shm, struct tee_shm, link);
  160. debug("%s: freeing leftover shm %p (size %lu, flags %#x)\n",
  161. __func__, (void *)shm, shm->size, shm->flags);
  162. tee_shm_free(shm);
  163. }
  164. return 0;
  165. }
  166. UCLASS_DRIVER(tee) = {
  167. .id = UCLASS_TEE,
  168. .name = "tee",
  169. .per_device_auto = sizeof(struct tee_uclass_priv),
  170. .pre_probe = tee_pre_probe,
  171. .pre_remove = tee_pre_remove,
  172. };
  173. void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d,
  174. const u8 s[TEE_UUID_LEN])
  175. {
  176. d->time_low = ((u32)s[0] << 24) | ((u32)s[1] << 16) |
  177. ((u32)s[2] << 8) | s[3],
  178. d->time_mid = ((u32)s[4] << 8) | s[5];
  179. d->time_hi_and_version = ((u32)s[6] << 8) | s[7];
  180. memcpy(d->clock_seq_and_node, s + 8, sizeof(d->clock_seq_and_node));
  181. }
  182. void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN],
  183. const struct tee_optee_ta_uuid *s)
  184. {
  185. d[0] = s->time_low >> 24;
  186. d[1] = s->time_low >> 16;
  187. d[2] = s->time_low >> 8;
  188. d[3] = s->time_low;
  189. d[4] = s->time_mid >> 8;
  190. d[5] = s->time_mid;
  191. d[6] = s->time_hi_and_version >> 8;
  192. d[7] = s->time_hi_and_version;
  193. memcpy(d + 8, s->clock_seq_and_node, sizeof(s->clock_seq_and_node));
  194. }
  195. void tee_flush_all_shm_dcache(struct udevice *dev)
  196. {
  197. struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
  198. struct tee_shm *s;
  199. list_for_each_entry(s, &priv->list_shm, link) {
  200. ulong start = rounddown((ulong)s->addr,
  201. CONFIG_SYS_CACHELINE_SIZE);
  202. ulong end = roundup((ulong)s->addr + s->size,
  203. CONFIG_SYS_CACHELINE_SIZE);
  204. flush_dcache_range(start, end);
  205. }
  206. }