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