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