tee-uclass.c 5.1 KB

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