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