blob: 52412a4098e4cef6fa737bdfc19020305f137146 [file] [log] [blame]
Jens Wiklander14290442018-09-25 16:40:09 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
Etienne Carriere1ba96782021-05-19 16:27:41 +02003 * Copyright (c) 2018-2020 Linaro Limited
Jens Wiklander14290442018-09-25 16:40:09 +02004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_TEE
7
Jens Wiklander14290442018-09-25 16:40:09 +02008#include <common.h>
Etienne Carriere1ba96782021-05-19 16:27:41 +02009#include <cpu_func.h>
Jens Wiklander14290442018-09-25 16:40:09 +020010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
13#include <tee.h>
Etienne Carriere1ba96782021-05-19 16:27:41 +020014#include <asm/cache.h>
Jens Wiklander14290442018-09-25 16:40:09 +020015#include <dm/device-internal.h>
16#include <dm/uclass-internal.h>
Jens Wiklander14290442018-09-25 16:40:09 +020017
18/**
19 * struct tee_uclass_priv - information of a TEE, stored by the uclass
20 *
21 * @list_shm: list of structe tee_shm representing memory blocks shared
22 * with the TEE.
23 */
24struct tee_uclass_priv {
25 struct list_head list_shm;
26};
27
28static const struct tee_driver_ops *tee_get_ops(struct udevice *dev)
29{
30 return device_get_ops(dev);
31}
32
33void tee_get_version(struct udevice *dev, struct tee_version_data *vers)
34{
35 tee_get_ops(dev)->get_version(dev, vers);
36}
37
38int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
39 uint num_param, struct tee_param *param)
40{
41 return tee_get_ops(dev)->open_session(dev, arg, num_param, param);
42}
43
44int tee_close_session(struct udevice *dev, u32 session)
45{
46 return tee_get_ops(dev)->close_session(dev, session);
47}
48
49int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
50 uint num_param, struct tee_param *param)
51{
52 return tee_get_ops(dev)->invoke_func(dev, arg, num_param, param);
53}
54
55int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
56 u32 flags, struct tee_shm **shmp)
57{
58 struct tee_shm *shm;
59 void *p = addr;
60 int rc;
61
62 if (flags & TEE_SHM_ALLOC) {
63 if (align)
64 p = memalign(align, size);
65 else
66 p = malloc(size);
67 }
68 if (!p)
69 return -ENOMEM;
70
71 shm = calloc(1, sizeof(*shm));
72 if (!shm) {
73 rc = -ENOMEM;
74 goto err;
75 }
76
77 shm->dev = dev;
78 shm->addr = p;
79 shm->size = size;
80 shm->flags = flags;
81
82 if (flags & TEE_SHM_SEC_REGISTER) {
83 rc = tee_get_ops(dev)->shm_register(dev, shm);
84 if (rc)
85 goto err;
86 }
87
88 if (flags & TEE_SHM_REGISTER) {
89 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
90
91 list_add(&shm->link, &priv->list_shm);
92 }
93
94 *shmp = shm;
95
96 return 0;
97err:
98 free(shm);
99 if (flags & TEE_SHM_ALLOC)
100 free(p);
101
102 return rc;
103}
104
105int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
106 struct tee_shm **shmp)
107{
108 u32 f = flags;
109
110 f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER | TEE_SHM_ALLOC;
111
112 return __tee_shm_add(dev, 0, NULL, size, f, shmp);
113}
114
115int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
116 struct tee_shm **shmp)
117{
118 u32 f = flags & ~TEE_SHM_ALLOC;
119
120 f |= TEE_SHM_SEC_REGISTER | TEE_SHM_REGISTER;
121
122 return __tee_shm_add(dev, 0, addr, size, f, shmp);
123}
124
125void tee_shm_free(struct tee_shm *shm)
126{
127 if (!shm)
128 return;
129
130 if (shm->flags & TEE_SHM_SEC_REGISTER)
131 tee_get_ops(shm->dev)->shm_unregister(shm->dev, shm);
132
133 if (shm->flags & TEE_SHM_REGISTER)
134 list_del(&shm->link);
135
136 if (shm->flags & TEE_SHM_ALLOC)
137 free(shm->addr);
138
139 free(shm);
140}
141
142bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev)
143{
144 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
145 struct tee_shm *s;
146
147 list_for_each_entry(s, &priv->list_shm, link)
148 if (s == shm)
149 return true;
150
151 return false;
152}
153
154struct udevice *tee_find_device(struct udevice *start,
155 int (*match)(struct tee_version_data *vers,
156 const void *data),
157 const void *data,
158 struct tee_version_data *vers)
159{
160 struct udevice *dev = start;
161 struct tee_version_data lv;
162 struct tee_version_data *v = vers ? vers : &lv;
163
164 if (!dev)
165 uclass_find_first_device(UCLASS_TEE, &dev);
166 else
167 uclass_find_next_device(&dev);
168
169 for (; dev; uclass_find_next_device(&dev)) {
170 if (device_probe(dev))
171 continue;
172 tee_get_ops(dev)->get_version(dev, v);
173 if (!match || match(v, data))
174 return dev;
175 }
176
177 return NULL;
178}
179
180static int tee_pre_probe(struct udevice *dev)
181{
182 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
183
184 INIT_LIST_HEAD(&priv->list_shm);
185
186 return 0;
187}
188
189static int tee_pre_remove(struct udevice *dev)
190{
191 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
192 struct tee_shm *shm;
193
194 /*
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
197 * reused.
198 */
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);
203 tee_shm_free(shm);
204 }
205
206 return 0;
207}
208
209UCLASS_DRIVER(tee) = {
210 .id = UCLASS_TEE,
211 .name = "tee",
Simon Glass8a2b47f2020-12-03 16:55:17 -0700212 .per_device_auto = sizeof(struct tee_uclass_priv),
Jens Wiklander14290442018-09-25 16:40:09 +0200213 .pre_probe = tee_pre_probe,
214 .pre_remove = tee_pre_remove,
215};
Jens Wiklander8ebf55f2018-09-25 16:40:15 +0200216
217void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d,
218 const u8 s[TEE_UUID_LEN])
219{
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));
225}
226
227void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN],
228 const struct tee_optee_ta_uuid *s)
229{
230 d[0] = s->time_low >> 24;
231 d[1] = s->time_low >> 16;
232 d[2] = s->time_low >> 8;
233 d[3] = s->time_low;
234 d[4] = s->time_mid >> 8;
235 d[5] = s->time_mid;
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));
239}
Etienne Carriere1ba96782021-05-19 16:27:41 +0200240
241void tee_flush_all_shm_dcache(struct udevice *dev)
242{
243 struct tee_uclass_priv *priv = dev_get_uclass_priv(dev);
244 struct tee_shm *s;
245
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);
251
252 flush_dcache_range(start, end);
253 }
254}