1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018-2020 Linaro Limited
6 #define LOG_CATEGORY UCLASS_TEE
14 #include <asm/cache.h>
15 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
19 * struct tee_uclass_priv - information of a TEE, stored by the uclass
21 * @list_shm: list of structe tee_shm representing memory blocks shared
24 struct tee_uclass_priv {
25 struct list_head list_shm;
28 static const struct tee_driver_ops *tee_get_ops(struct udevice *dev)
30 return device_get_ops(dev);
33 void tee_get_version(struct udevice *dev, struct tee_version_data *vers)
35 tee_get_ops(dev)->get_version(dev, vers);
38 int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
39 uint num_param, struct tee_param *param)
41 return tee_get_ops(dev)->open_session(dev, arg, num_param, param);
44 int tee_close_session(struct udevice *dev, u32 session)
46 return tee_get_ops(dev)->close_session(dev, session);
49 int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
50 uint num_param, struct tee_param *param)
52 return tee_get_ops(dev)->invoke_func(dev, arg, num_param, param);
55 int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
56 u32 flags, struct tee_shm **shmp)
62 if (flags & TEE_SHM_ALLOC) {
64 p = memalign(align, size);
71 shm = calloc(1, sizeof(*shm));
82 if (flags & TEE_SHM_SEC_REGISTER) {
83 rc = tee_get_ops(dev)->shm_register(dev, shm);
88 if (flags & TEE_SHM_REGISTER) {
89 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
91 list_add(&shm->link, &priv->list_shm);
99 if (flags & TEE_SHM_ALLOC)
105 int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
106 struct tee_shm **shmp)
110 f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER | TEE_SHM_ALLOC;
112 return __tee_shm_add(dev, 0, NULL, size, f, shmp);
115 int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
116 struct tee_shm **shmp)
118 u32 f = flags & ~TEE_SHM_ALLOC;
120 f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER;
122 return __tee_shm_add(dev, 0, addr, size, f, shmp);
125 void tee_shm_free(struct tee_shm *shm)
130 if (shm->flags & TEE_SHM_SEC_REGISTER)
131 tee_get_ops(shm->dev)->shm_unregister(shm->dev, shm);
133 if (shm->flags & TEE_SHM_REGISTER)
134 list_del(&shm->link);
136 if (shm->flags & TEE_SHM_ALLOC)
142 bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev)
144 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
147 list_for_each_entry(s, &priv->list_shm, link)
154 struct udevice *tee_find_device(struct udevice *start,
155 int (*match)(struct tee_version_data *vers,
158 struct tee_version_data *vers)
160 struct udevice *dev = start;
161 struct tee_version_data lv;
162 struct tee_version_data *v = vers ? vers : &lv;
165 uclass_find_first_device(UCLASS_TEE, &dev);
167 uclass_find_next_device(&dev);
169 for (; dev; uclass_find_next_device(&dev)) {
170 if (device_probe(dev))
172 tee_get_ops(dev)->get_version(dev, v);
173 if (!match || match(v, data))
180 static int tee_pre_probe(struct udevice *dev)
182 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
184 INIT_LIST_HEAD(&priv->list_shm);
189 static int tee_pre_remove(struct udevice *dev)
191 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
195 * Any remaining shared memory must be unregistered now as U-Boot
196 * is about to hand over to the next stage and that memory will be
199 while (!list_empty(&priv->list_shm)) {
200 shm = list_first_entry(&priv->list_shm, struct tee_shm, link);
201 debug("%s: freeing leftover shm %p (size %lu, flags %#x)\n",
202 __func__, (void *)shm, shm->size, shm->flags);
209 UCLASS_DRIVER(tee) = {
212 .per_device_auto = sizeof(struct tee_uclass_priv),
213 .pre_probe = tee_pre_probe,
214 .pre_remove = tee_pre_remove,
217 void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d,
218 const u8 s[TEE_UUID_LEN])
220 d->time_low = ((u32)s[0] << 24) | ((u32)s[1] << 16) |
221 ((u32)s[2] << 8) | s[3],
222 d->time_mid = ((u32)s[4] << 8) | s[5];
223 d->time_hi_and_version = ((u32)s[6] << 8) | s[7];
224 memcpy(d->clock_seq_and_node, s + 8, sizeof(d->clock_seq_and_node));
227 void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN],
228 const struct tee_optee_ta_uuid *s)
230 d[0] = s->time_low >> 24;
231 d[1] = s->time_low >> 16;
232 d[2] = s->time_low >> 8;
234 d[4] = s->time_mid >> 8;
236 d[6] = s->time_hi_and_version >> 8;
237 d[7] = s->time_hi_and_version;
238 memcpy(d + 8, s->clock_seq_and_node, sizeof(s->clock_seq_and_node));
241 void tee_flush_all_shm_dcache(struct udevice *dev)
243 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
246 list_for_each_entry(s, &priv->list_shm, link) {
247 ulong start = rounddown((ulong)s->addr,
248 CONFIG_SYS_CACHELINE_SIZE);
249 ulong end = roundup((ulong)s->addr + s->size,
250 CONFIG_SYS_CACHELINE_SIZE);
252 flush_dcache_range(start, end);