tee-uclass.c 5.1 KB

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