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