1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020-2022 Linaro Limited.
4  */
5 
6 #define LOG_CATEGORY UCLASS_SCMI_AGENT
7 
8 #include <dm.h>
9 #include <errno.h>
10 #include <scmi_agent.h>
11 #include <scmi_agent-uclass.h>
12 #include <string.h>
13 #include <tee.h>
14 #include <asm/types.h>
15 #include <dm/device_compat.h>
16 #include <dm/devres.h>
17 #include <linux/arm-smccc.h>
18 #include <linux/bug.h>
19 #include <linux/compat.h>
20 
21 #include "smt.h"
22 
23 #define SCMI_SHM_SIZE		128
24 
25 /**
26  * struct scmi_optee_channel - Description of an SCMI OP-TEE transport
27  * @channel_id:		Channel identifier
28  * @smt:		Shared memory buffer with synchronisation protocol
29  * @dyn_shm:		True if using dynamically allocated shared memory
30  */
31 struct scmi_optee_channel {
32 	unsigned int channel_id;
33 	struct scmi_smt smt;
34 	bool dyn_shm;
35 };
36 
37 /**
38  * struct scmi_channel - Channel instance referenced in SCMI drivers
39  * @ref: Reference to local channel instance
40  **/
41 struct scmi_channel {
42 	struct scmi_optee_channel ref;
43 };
44 
45 /**
46  * struct channel_session - Aggreates SCMI service session context references
47  * @tee:		OP-TEE device to invoke
48  * @tee_session:	OP-TEE session identifier
49  * @tee_shm:		Dynamically allocated OP-TEE shared memory, or NULL
50  * @channel_hdl:	Channel handle provided by OP-TEE SCMI service
51  */
52 struct channel_session {
53 	struct udevice *tee;
54 	u32 tee_session;
55 	struct tee_shm *tee_shm;
56 	u32 channel_hdl;
57 };
58 
59 #define TA_SCMI_UUID { 0xa8cfe406, 0xd4f5, 0x4a2e, \
60 		      { 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99 } }
61 
62 enum optee_smci_pta_cmd {
63 	/*
64 	 * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities
65 	 *
66 	 * [out]    value[0].a: Capability bit mask (enum pta_scmi_caps)
67 	 * [out]    value[0].b: Extended capabilities or 0
68 	 */
69 	PTA_SCMI_CMD_CAPABILITIES = 0,
70 
71 	/*
72 	 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer
73 	 *
74 	 * [in]     value[0].a: Channel handle
75 	 *
76 	 * Shared memory used for SCMI message/response exhange is expected
77 	 * already identified and bound to channel handle in both SCMI agent
78 	 * and SCMI server (OP-TEE) parts.
79 	 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
80 	 * protocol message ID).
81 	 */
82 	PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,
83 
84 	/*
85 	 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message
86 	 *
87 	 * [in]     value[0].a: Channel handle
88 	 * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload)
89 	 *
90 	 * Shared memory used for SCMI message/response is a SMT buffer
91 	 * referenced by param[1]. It shall be 128 bytes large to fit response
92 	 * payload whatever message playload size.
93 	 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
94 	 * protocol message ID).
95 	 */
96 	PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,
97 
98 	/*
99 	 * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle
100 	 *
101 	 * [in]     value[0].a: Channel identifier
102 	 * [out]    value[0].a: Returned channel handle
103 	 * [in]     value[0].b: Requested capabilities mask (enum pta_scmi_caps)
104 	 */
105 	PTA_SCMI_CMD_GET_CHANNEL = 3,
106 
107 	/*
108 	 * PTA_SCMI_CMD_PROCESS_MSG_CHANNEL - Process SCMI message in MSG
109 	 * buffers pointed by memref parameters
110 	 *
111 	 * [in]     value[0].a: Channel handle
112 	 * [in]     memref[1]: Message buffer (MSG header and SCMI payload)
113 	 * [out]    memref[2]: Response buffer (MSG header and SCMI payload)
114 	 *
115 	 * Shared memories used for SCMI message/response are MSG buffers
116 	 * referenced by param[1] and param[2]. MSG transport protocol
117 	 * uses a 32bit header to carry SCMI meta-data (protocol ID and
118 	 * protocol message ID) followed by the effective SCMI message
119 	 * payload.
120 	 */
121 	PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4,
122 };
123 
124 /*
125  * OP-TEE SCMI service capabilities bit flags (32bit)
126  *
127  * PTA_SCMI_CAPS_SMT_HEADER
128  * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in
129  * shared memory buffers to carry SCMI protocol synchronisation information.
130  *
131  * PTA_SCMI_CAPS_MSG_HEADER
132  * When set, OP-TEE supports command using MSG header protocol in an OP-TEE
133  * shared memory to carry SCMI protocol synchronisation information and SCMI
134  * message payload.
135  */
136 #define PTA_SCMI_CAPS_NONE		0
137 #define PTA_SCMI_CAPS_SMT_HEADER	BIT(0)
138 #define PTA_SCMI_CAPS_MSG_HEADER	BIT(1)
139 #define PTA_SCMI_CAPS_MASK		(PTA_SCMI_CAPS_SMT_HEADER | \
140 					 PTA_SCMI_CAPS_MSG_HEADER)
141 
open_channel(struct udevice * dev,struct scmi_optee_channel * chan,struct channel_session * sess)142 static int open_channel(struct udevice *dev, struct scmi_optee_channel *chan,
143 			struct channel_session *sess)
144 {
145 	const struct tee_optee_ta_uuid uuid = TA_SCMI_UUID;
146 	struct tee_open_session_arg sess_arg = { };
147 	struct tee_invoke_arg cmd_arg = { };
148 	struct tee_param param[1] = { };
149 	int ret;
150 
151 	memset(sess, 0, sizeof(*sess));
152 
153 	sess->tee = tee_find_device(NULL, NULL, NULL, NULL);
154 	if (!sess->tee)
155 		return -ENODEV;
156 
157 	sess_arg.clnt_login = TEE_LOGIN_REE_KERNEL;
158 	tee_optee_ta_uuid_to_octets(sess_arg.uuid, &uuid);
159 
160 	ret = tee_open_session(sess->tee, &sess_arg, 0, NULL);
161 	if (ret) {
162 		dev_err(dev, "can't open session: %d\n", ret);
163 		return ret;
164 	}
165 
166 	cmd_arg.func = PTA_SCMI_CMD_GET_CHANNEL;
167 	cmd_arg.session = sess_arg.session;
168 
169 	param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INOUT;
170 	param[0].u.value.a = chan->channel_id;
171 	if (chan->dyn_shm)
172 		param[0].u.value.b = PTA_SCMI_CAPS_MSG_HEADER;
173 	else
174 		param[0].u.value.b = PTA_SCMI_CAPS_SMT_HEADER;
175 
176 	ret = tee_invoke_func(sess->tee, &cmd_arg, ARRAY_SIZE(param), param);
177 	if (ret || cmd_arg.ret) {
178 		dev_err(dev, "Invoke failed: %d, 0x%x\n", ret, cmd_arg.ret);
179 		if (!ret)
180 			ret = -EPROTO;
181 
182 		tee_close_session(sess->tee, sess_arg.session);
183 		return ret;
184 	}
185 
186 	sess->tee_session = sess_arg.session;
187 	sess->channel_hdl = param[0].u.value.a;
188 
189 	return 0;
190 }
191 
close_channel(struct channel_session * sess)192 static void close_channel(struct channel_session *sess)
193 {
194 	tee_close_session(sess->tee, sess->tee_session);
195 }
196 
invoke_cmd(struct udevice * dev,struct scmi_optee_channel * chan,struct channel_session * sess,struct scmi_msg * msg)197 static int invoke_cmd(struct udevice *dev, struct scmi_optee_channel *chan,
198 		      struct channel_session *sess, struct scmi_msg *msg)
199 {
200 	struct tee_invoke_arg arg = { };
201 	struct tee_param param[3] = { };
202 	int ret;
203 
204 	arg.session = sess->tee_session;
205 	param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT;
206 	param[0].u.value.a = sess->channel_hdl;
207 
208 	if (sess->tee_shm) {
209 		size_t in_size;
210 
211 		ret = scmi_msg_to_smt_msg(dev, &chan->smt, msg, &in_size);
212 		if (ret < 0)
213 			return ret;
214 
215 		arg.func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL;
216 		param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT;
217 		param[1].u.memref.shm = sess->tee_shm;
218 		param[1].u.memref.size = in_size;
219 		param[2].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
220 		param[2].u.memref.shm = sess->tee_shm;
221 		param[2].u.memref.size = sess->tee_shm->size;
222 	} else {
223 		arg.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL;
224 		scmi_write_msg_to_smt(dev, &chan->smt, msg);
225 	}
226 
227 	ret = tee_invoke_func(sess->tee, &arg, ARRAY_SIZE(param), param);
228 	if (ret || arg.ret) {
229 		if (!ret)
230 			ret = -EPROTO;
231 
232 		return ret;
233 	}
234 
235 	if (sess->tee_shm) {
236 		ret = scmi_msg_from_smt_msg(dev, &chan->smt, msg,
237 					    param[2].u.memref.size);
238 	} else {
239 		ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
240 		scmi_clear_smt_channel(&chan->smt);
241 	}
242 
243 	return ret;
244 }
245 
prepare_shm(struct udevice * dev,struct scmi_optee_channel * chan,struct channel_session * sess)246 static int prepare_shm(struct udevice *dev, struct scmi_optee_channel *chan,
247 		       struct channel_session *sess)
248 {
249 	int ret;
250 
251 	/* Static shm is already prepared by the firmware: nothing to do */
252 	if (!chan->dyn_shm)
253 		return 0;
254 
255 	chan->smt.size = SCMI_SHM_SIZE;
256 
257 	ret = tee_shm_alloc(sess->tee, chan->smt.size, 0, &sess->tee_shm);
258 	if (ret) {
259 		dev_err(dev, "Failed to allocated shmem: %d\n", ret);
260 		return ret;
261 	}
262 
263 	chan->smt.buf = sess->tee_shm->addr;
264 
265 	return 0;
266 }
267 
release_shm(struct udevice * dev,struct channel_session * sess)268 static void release_shm(struct udevice *dev, struct channel_session *sess)
269 {
270 	struct scmi_optee_channel *chan = dev_get_plat(dev);
271 
272 	if (chan->dyn_shm)
273 		tee_shm_free(sess->tee_shm);
274 }
275 
scmi_optee_process_msg(struct udevice * dev,struct scmi_channel * channel,struct scmi_msg * msg)276 static int scmi_optee_process_msg(struct udevice *dev,
277 				  struct scmi_channel *channel,
278 				  struct scmi_msg *msg)
279 {
280 	struct scmi_optee_channel *chan = &channel->ref;
281 	struct channel_session sess = { };
282 	int ret;
283 
284 	ret = open_channel(dev, chan, &sess);
285 	if (ret)
286 		return ret;
287 
288 	ret = prepare_shm(dev, chan, &sess);
289 	if (ret)
290 		goto out;
291 
292 	ret = invoke_cmd(dev, chan, &sess, msg);
293 
294 	release_shm(dev, &sess);
295 
296 out:
297 	close_channel(&sess);
298 
299 	return ret;
300 }
301 
setup_channel(struct udevice * dev,struct scmi_optee_channel * chan)302 static int setup_channel(struct udevice *dev, struct scmi_optee_channel *chan)
303 {
304 	int ret;
305 
306 	if (dev_read_u32(dev, "linaro,optee-channel-id", &chan->channel_id)) {
307 		dev_err(dev, "Missing property linaro,optee-channel-id\n");
308 		return -EINVAL;
309 	}
310 
311 	if (dev_read_prop(dev, "shmem", NULL)) {
312 		ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
313 		if (ret) {
314 			dev_err(dev, "Failed to get smt resources: %d\n", ret);
315 			return ret;
316 		}
317 		chan->dyn_shm = false;
318 	} else {
319 		chan->dyn_shm = true;
320 	}
321 
322 	return 0;
323 }
324 
scmi_optee_get_channel(struct udevice * dev,struct udevice * protocol,struct scmi_channel ** channel)325 static int scmi_optee_get_channel(struct udevice *dev,
326 				  struct udevice *protocol,
327 				  struct scmi_channel **channel)
328 {
329 	struct scmi_optee_channel *base_chan = dev_get_plat(dev);
330 	struct scmi_optee_channel *chan;
331 	u32 channel_id;
332 	int ret;
333 
334 	if (dev_read_u32(protocol, "linaro,optee-channel-id", &channel_id)) {
335 		/* Uses agent base channel */
336 		*channel = container_of(base_chan, struct scmi_channel, ref);
337 
338 		return 0;
339 	}
340 
341 	/* Setup a dedicated channel */
342 	chan = calloc(1, sizeof(*chan));
343 	if (!chan)
344 		return -ENOMEM;
345 
346 	ret = setup_channel(protocol, chan);
347 	if (ret) {
348 		free(chan);
349 		return ret;
350 	}
351 
352 	*channel = container_of(chan, struct scmi_channel, ref);
353 
354 	return 0;
355 }
356 
scmi_optee_of_to_plat(struct udevice * dev)357 static int scmi_optee_of_to_plat(struct udevice *dev)
358 {
359 	struct scmi_optee_channel *chan = dev_get_plat(dev);
360 
361 	return setup_channel(dev, chan);
362 }
363 
scmi_optee_probe(struct udevice * dev)364 static int scmi_optee_probe(struct udevice *dev)
365 {
366 	struct scmi_optee_channel *chan = dev_get_plat(dev);
367 	struct channel_session sess;
368 	int ret;
369 
370 	/* Check OP-TEE service acknowledges the SCMI channel */
371 	ret = open_channel(dev, chan, &sess);
372 	if (!ret)
373 		close_channel(&sess);
374 
375 	return ret;
376 }
377 
378 static const struct udevice_id scmi_optee_ids[] = {
379 	{ .compatible = "linaro,scmi-optee" },
380 	{ }
381 };
382 
383 static const struct scmi_agent_ops scmi_optee_ops = {
384 	.of_get_channel = scmi_optee_get_channel,
385 	.process_msg = scmi_optee_process_msg,
386 };
387 
388 U_BOOT_DRIVER(scmi_optee) = {
389 	.name		= "scmi-over-optee",
390 	.id		= UCLASS_SCMI_AGENT,
391 	.of_match	= scmi_optee_ids,
392 	.plat_auto	= sizeof(struct scmi_optee_channel),
393 	.of_to_plat	= scmi_optee_of_to_plat,
394 	.probe		= scmi_optee_probe,
395 	.flags		= DM_FLAG_OS_PREPARE,
396 	.ops		= &scmi_optee_ops,
397 };
398