1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019-2021 Linaro Ltd.
4  */
5 
6 #include <linux/io.h>
7 #include <linux/of.h>
8 #include <linux/of_address.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/tee_drv.h>
14 #include <linux/uuid.h>
15 #include <uapi/linux/tee.h>
16 
17 #include "common.h"
18 
19 #define SCMI_OPTEE_MAX_MSG_SIZE		128
20 
21 enum scmi_optee_pta_cmd {
22 	/*
23 	 * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities
24 	 *
25 	 * [out]    value[0].a: Capability bit mask (enum pta_scmi_caps)
26 	 * [out]    value[0].b: Extended capabilities or 0
27 	 */
28 	PTA_SCMI_CMD_CAPABILITIES = 0,
29 
30 	/*
31 	 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer
32 	 *
33 	 * [in]     value[0].a: Channel handle
34 	 *
35 	 * Shared memory used for SCMI message/response exhange is expected
36 	 * already identified and bound to channel handle in both SCMI agent
37 	 * and SCMI server (OP-TEE) parts.
38 	 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
39 	 * protocol message ID).
40 	 */
41 	PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,
42 
43 	/*
44 	 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message
45 	 *
46 	 * [in]     value[0].a: Channel handle
47 	 * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload)
48 	 *
49 	 * Shared memory used for SCMI message/response is a SMT buffer
50 	 * referenced by param[1]. It shall be 128 bytes large to fit response
51 	 * payload whatever message playload size.
52 	 * The memory uses SMT header to carry SCMI meta-data (protocol ID and
53 	 * protocol message ID).
54 	 */
55 	PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,
56 
57 	/*
58 	 * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle
59 	 *
60 	 * SCMI shm information are 0 if agent expects to use OP-TEE regular SHM
61 	 *
62 	 * [in]     value[0].a: Channel identifier
63 	 * [out]    value[0].a: Returned channel handle
64 	 * [in]     value[0].b: Requested capabilities mask (enum pta_scmi_caps)
65 	 */
66 	PTA_SCMI_CMD_GET_CHANNEL = 3,
67 
68 	/*
69 	 * PTA_SCMI_CMD_PROCESS_MSG_CHANNEL - Process SCMI message in a MSG
70 	 * buffer pointed by memref parameters
71 	 *
72 	 * [in]     value[0].a: Channel handle
73 	 * [in]     memref[1]: Message buffer (MSG and SCMI payload)
74 	 * [out]    memref[2]: Response buffer (MSG and SCMI payload)
75 	 *
76 	 * Shared memories used for SCMI message/response are MSG buffers
77 	 * referenced by param[1] and param[2]. MSG transport protocol
78 	 * uses a 32bit header to carry SCMI meta-data (protocol ID and
79 	 * protocol message ID) followed by the effective SCMI message
80 	 * payload.
81 	 */
82 	PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4,
83 };
84 
85 /*
86  * OP-TEE SCMI service capabilities bit flags (32bit)
87  *
88  * PTA_SCMI_CAPS_SMT_HEADER
89  * When set, OP-TEE supports command using SMT header protocol (SCMI shmem) in
90  * shared memory buffers to carry SCMI protocol synchronisation information.
91  *
92  * PTA_SCMI_CAPS_MSG_HEADER
93  * When set, OP-TEE supports command using MSG header protocol in an OP-TEE
94  * shared memory to carry SCMI protocol synchronisation information and SCMI
95  * message payload.
96  */
97 #define PTA_SCMI_CAPS_NONE		0
98 #define PTA_SCMI_CAPS_SMT_HEADER	BIT(0)
99 #define PTA_SCMI_CAPS_MSG_HEADER	BIT(1)
100 #define PTA_SCMI_CAPS_MASK		(PTA_SCMI_CAPS_SMT_HEADER | \
101 					 PTA_SCMI_CAPS_MSG_HEADER)
102 
103 /**
104  * struct scmi_optee_channel - Description of an OP-TEE SCMI channel
105  *
106  * @channel_id: OP-TEE channel ID used for this transport
107  * @tee_session: TEE session identifier
108  * @caps: OP-TEE SCMI channel capabilities
109  * @mu: Mutex protection on channel access
110  * @cinfo: SCMI channel information
111  * @shmem: Virtual base address of the shared memory
112  * @req: Shared memory protocol handle for SCMI request and synchronous response
113  * @tee_shm: TEE shared memory handle @req or NULL if using IOMEM shmem
114  * @link: Reference in agent's channel list
115  */
116 struct scmi_optee_channel {
117 	u32 channel_id;
118 	u32 tee_session;
119 	u32 caps;
120 	struct mutex mu;
121 	struct scmi_chan_info *cinfo;
122 	union {
123 		struct scmi_shared_mem __iomem *shmem;
124 		struct scmi_msg_payld *msg;
125 	} req;
126 	struct tee_shm *tee_shm;
127 	struct list_head link;
128 };
129 
130 /**
131  * struct scmi_optee_agent - OP-TEE transport private data
132  *
133  * @dev: Device used for communication with TEE
134  * @tee_ctx: TEE context used for communication
135  * @caps: Supported channel capabilities
136  * @mu: Mutex for protection of @channel_list
137  * @channel_list: List of all created channels for the agent
138  */
139 struct scmi_optee_agent {
140 	struct device *dev;
141 	struct tee_context *tee_ctx;
142 	u32 caps;
143 	struct mutex mu;
144 	struct list_head channel_list;
145 };
146 
147 /* There can be only 1 SCMI service in OP-TEE we connect to */
148 static struct scmi_optee_agent *scmi_optee_private;
149 
150 /* Forward reference to scmi_optee transport initialization */
151 static int scmi_optee_init(void);
152 
153 /* Open a session toward SCMI OP-TEE service with REE_KERNEL identity */
154 static int open_session(struct scmi_optee_agent *agent, u32 *tee_session)
155 {
156 	struct device *dev = agent->dev;
157 	struct tee_client_device *scmi_pta = to_tee_client_device(dev);
158 	struct tee_ioctl_open_session_arg arg = { };
159 	int ret;
160 
161 	memcpy(arg.uuid, scmi_pta->id.uuid.b, TEE_IOCTL_UUID_LEN);
162 	arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
163 
164 	ret = tee_client_open_session(agent->tee_ctx, &arg, NULL);
165 	if (ret < 0 || arg.ret) {
166 		dev_err(dev, "Can't open tee session: %d / %#x\n", ret, arg.ret);
167 		return -EOPNOTSUPP;
168 	}
169 
170 	*tee_session = arg.session;
171 
172 	return 0;
173 }
174 
175 static void close_session(struct scmi_optee_agent *agent, u32 tee_session)
176 {
177 	tee_client_close_session(agent->tee_ctx, tee_session);
178 }
179 
180 static int get_capabilities(struct scmi_optee_agent *agent)
181 {
182 	struct tee_ioctl_invoke_arg arg = { };
183 	struct tee_param param[1] = { };
184 	u32 caps;
185 	u32 tee_session;
186 	int ret;
187 
188 	ret = open_session(agent, &tee_session);
189 	if (ret)
190 		return ret;
191 
192 	arg.func = PTA_SCMI_CMD_CAPABILITIES;
193 	arg.session = tee_session;
194 	arg.num_params = 1;
195 
196 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
197 
198 	ret = tee_client_invoke_func(agent->tee_ctx, &arg, param);
199 
200 	close_session(agent, tee_session);
201 
202 	if (ret < 0 || arg.ret) {
203 		dev_err(agent->dev, "Can't get capabilities: %d / %#x\n", ret, arg.ret);
204 		return -EOPNOTSUPP;
205 	}
206 
207 	caps = param[0].u.value.a;
208 
209 	if (!(caps & (PTA_SCMI_CAPS_SMT_HEADER | PTA_SCMI_CAPS_MSG_HEADER))) {
210 		dev_err(agent->dev, "OP-TEE SCMI PTA doesn't support SMT and MSG\n");
211 		return -EOPNOTSUPP;
212 	}
213 
214 	agent->caps = caps;
215 
216 	return 0;
217 }
218 
219 static int get_channel(struct scmi_optee_channel *channel)
220 {
221 	struct device *dev = scmi_optee_private->dev;
222 	struct tee_ioctl_invoke_arg arg = { };
223 	struct tee_param param[1] = { };
224 	unsigned int caps = 0;
225 	int ret;
226 
227 	if (channel->tee_shm)
228 		caps = PTA_SCMI_CAPS_MSG_HEADER;
229 	else
230 		caps = PTA_SCMI_CAPS_SMT_HEADER;
231 
232 	arg.func = PTA_SCMI_CMD_GET_CHANNEL;
233 	arg.session = channel->tee_session;
234 	arg.num_params = 1;
235 
236 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
237 	param[0].u.value.a = channel->channel_id;
238 	param[0].u.value.b = caps;
239 
240 	ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
241 
242 	if (ret || arg.ret) {
243 		dev_err(dev, "Can't get channel with caps %#x: %d / %#x\n", caps, ret, arg.ret);
244 		return -EOPNOTSUPP;
245 	}
246 
247 	/* From now on use channel identifer provided by OP-TEE SCMI service */
248 	channel->channel_id = param[0].u.value.a;
249 	channel->caps = caps;
250 
251 	return 0;
252 }
253 
254 static int invoke_process_smt_channel(struct scmi_optee_channel *channel)
255 {
256 	struct tee_ioctl_invoke_arg arg = {
257 		.func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL,
258 		.session = channel->tee_session,
259 		.num_params = 1,
260 	};
261 	struct tee_param param[1] = { };
262 	int ret;
263 
264 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
265 	param[0].u.value.a = channel->channel_id;
266 
267 	ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
268 	if (ret < 0 || arg.ret) {
269 		dev_err(scmi_optee_private->dev, "Can't invoke channel %u: %d / %#x\n",
270 			channel->channel_id, ret, arg.ret);
271 		return -EIO;
272 	}
273 
274 	return 0;
275 }
276 
277 static int invoke_process_msg_channel(struct scmi_optee_channel *channel, size_t msg_size)
278 {
279 	struct tee_ioctl_invoke_arg arg = {
280 		.func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL,
281 		.session = channel->tee_session,
282 		.num_params = 3,
283 	};
284 	struct tee_param param[3] = { };
285 	int ret;
286 
287 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
288 	param[0].u.value.a = channel->channel_id;
289 
290 	param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
291 	param[1].u.memref.shm = channel->tee_shm;
292 	param[1].u.memref.size = msg_size;
293 
294 	param[2].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
295 	param[2].u.memref.shm = channel->tee_shm;
296 	param[2].u.memref.size = SCMI_OPTEE_MAX_MSG_SIZE;
297 
298 	ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
299 	if (ret < 0 || arg.ret) {
300 		dev_err(scmi_optee_private->dev, "Can't invoke channel %u: %d / %#x\n",
301 			channel->channel_id, ret, arg.ret);
302 		return -EIO;
303 	}
304 
305 	return 0;
306 }
307 
308 static int scmi_optee_link_supplier(struct device *dev)
309 {
310 	if (!scmi_optee_private) {
311 		if (scmi_optee_init())
312 			dev_dbg(dev, "Optee bus not yet ready\n");
313 
314 		/* Wait for optee bus */
315 		return -EPROBE_DEFER;
316 	}
317 
318 	if (!device_link_add(dev, scmi_optee_private->dev, DL_FLAG_AUTOREMOVE_CONSUMER)) {
319 		dev_err(dev, "Adding link to supplier optee device failed\n");
320 		return -ECANCELED;
321 	}
322 
323 	return 0;
324 }
325 
326 static bool scmi_optee_chan_available(struct device *dev, int idx)
327 {
328 	u32 channel_id;
329 
330 	return !of_property_read_u32_index(dev->of_node, "linaro,optee-channel-id",
331 					   idx, &channel_id);
332 }
333 
334 static void scmi_optee_clear_channel(struct scmi_chan_info *cinfo)
335 {
336 	struct scmi_optee_channel *channel = cinfo->transport_info;
337 
338 	if (!channel->tee_shm)
339 		shmem_clear_channel(channel->req.shmem);
340 }
341 
342 static int setup_dynamic_shmem(struct device *dev, struct scmi_optee_channel *channel)
343 {
344 	const size_t msg_size = SCMI_OPTEE_MAX_MSG_SIZE;
345 	void *shbuf;
346 
347 	channel->tee_shm = tee_shm_alloc_kernel_buf(scmi_optee_private->tee_ctx, msg_size);
348 	if (IS_ERR(channel->tee_shm)) {
349 		dev_err(channel->cinfo->dev, "shmem allocation failed\n");
350 		return -ENOMEM;
351 	}
352 
353 	shbuf = tee_shm_get_va(channel->tee_shm, 0);
354 	memset(shbuf, 0, msg_size);
355 	channel->req.msg = shbuf;
356 
357 	return 0;
358 }
359 
360 static int setup_static_shmem(struct device *dev, struct scmi_chan_info *cinfo,
361 			      struct scmi_optee_channel *channel)
362 {
363 	struct device_node *np;
364 	resource_size_t size;
365 	struct resource res;
366 	int ret;
367 
368 	np = of_parse_phandle(cinfo->dev->of_node, "shmem", 0);
369 	if (!of_device_is_compatible(np, "arm,scmi-shmem")) {
370 		ret = -ENXIO;
371 		goto out;
372 	}
373 
374 	ret = of_address_to_resource(np, 0, &res);
375 	if (ret) {
376 		dev_err(dev, "Failed to get SCMI Tx shared memory\n");
377 		goto out;
378 	}
379 
380 	size = resource_size(&res);
381 
382 	channel->req.shmem = devm_ioremap(dev, res.start, size);
383 	if (!channel->req.shmem) {
384 		dev_err(dev, "Failed to ioremap SCMI Tx shared memory\n");
385 		ret = -EADDRNOTAVAIL;
386 		goto out;
387 	}
388 
389 	ret = 0;
390 
391 out:
392 	of_node_put(np);
393 
394 	return ret;
395 }
396 
397 static int setup_shmem(struct device *dev, struct scmi_chan_info *cinfo,
398 		       struct scmi_optee_channel *channel)
399 {
400 	if (of_find_property(cinfo->dev->of_node, "shmem", NULL))
401 		return setup_static_shmem(dev, cinfo, channel);
402 	else
403 		return setup_dynamic_shmem(dev, channel);
404 }
405 
406 static int scmi_optee_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, bool tx)
407 {
408 	struct scmi_optee_channel *channel;
409 	uint32_t channel_id;
410 	int ret;
411 
412 	if (!tx)
413 		return -ENODEV;
414 
415 	channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
416 	if (!channel)
417 		return -ENOMEM;
418 
419 	ret = of_property_read_u32_index(cinfo->dev->of_node, "linaro,optee-channel-id",
420 					 0, &channel_id);
421 	if (ret)
422 		return ret;
423 
424 	cinfo->transport_info = channel;
425 	channel->cinfo = cinfo;
426 	channel->channel_id = channel_id;
427 	mutex_init(&channel->mu);
428 
429 	ret = setup_shmem(dev, cinfo, channel);
430 	if (ret)
431 		return ret;
432 
433 	ret = open_session(scmi_optee_private, &channel->tee_session);
434 	if (ret)
435 		goto err_free_shm;
436 
437 	ret = get_channel(channel);
438 	if (ret)
439 		goto err_close_sess;
440 
441 	/* Enable polling */
442 	cinfo->no_completion_irq = true;
443 
444 	mutex_lock(&scmi_optee_private->mu);
445 	list_add(&channel->link, &scmi_optee_private->channel_list);
446 	mutex_unlock(&scmi_optee_private->mu);
447 
448 	return 0;
449 
450 err_close_sess:
451 	close_session(scmi_optee_private, channel->tee_session);
452 err_free_shm:
453 	if (channel->tee_shm)
454 		tee_shm_free(channel->tee_shm);
455 
456 	return ret;
457 }
458 
459 static int scmi_optee_chan_free(int id, void *p, void *data)
460 {
461 	struct scmi_chan_info *cinfo = p;
462 	struct scmi_optee_channel *channel = cinfo->transport_info;
463 
464 	mutex_lock(&scmi_optee_private->mu);
465 	list_del(&channel->link);
466 	mutex_unlock(&scmi_optee_private->mu);
467 
468 	close_session(scmi_optee_private, channel->tee_session);
469 
470 	if (channel->tee_shm) {
471 		tee_shm_free(channel->tee_shm);
472 		channel->tee_shm = NULL;
473 	}
474 
475 	cinfo->transport_info = NULL;
476 	channel->cinfo = NULL;
477 
478 	scmi_free_channel(cinfo, data, id);
479 
480 	return 0;
481 }
482 
483 static int scmi_optee_send_message(struct scmi_chan_info *cinfo,
484 				   struct scmi_xfer *xfer)
485 {
486 	struct scmi_optee_channel *channel = cinfo->transport_info;
487 	int ret;
488 
489 	mutex_lock(&channel->mu);
490 
491 	if (channel->tee_shm) {
492 		msg_tx_prepare(channel->req.msg, xfer);
493 		ret = invoke_process_msg_channel(channel, msg_command_size(xfer));
494 	} else {
495 		shmem_tx_prepare(channel->req.shmem, xfer);
496 		ret = invoke_process_smt_channel(channel);
497 	}
498 
499 	if (ret)
500 		mutex_unlock(&channel->mu);
501 
502 	return ret;
503 }
504 
505 static void scmi_optee_fetch_response(struct scmi_chan_info *cinfo,
506 				      struct scmi_xfer *xfer)
507 {
508 	struct scmi_optee_channel *channel = cinfo->transport_info;
509 
510 	if (channel->tee_shm)
511 		msg_fetch_response(channel->req.msg, SCMI_OPTEE_MAX_MSG_SIZE, xfer);
512 	else
513 		shmem_fetch_response(channel->req.shmem, xfer);
514 }
515 
516 static void scmi_optee_mark_txdone(struct scmi_chan_info *cinfo, int ret,
517 				   struct scmi_xfer *__unused)
518 {
519 	struct scmi_optee_channel *channel = cinfo->transport_info;
520 
521 	mutex_unlock(&channel->mu);
522 }
523 
524 static struct scmi_transport_ops scmi_optee_ops = {
525 	.link_supplier = scmi_optee_link_supplier,
526 	.chan_available = scmi_optee_chan_available,
527 	.chan_setup = scmi_optee_chan_setup,
528 	.chan_free = scmi_optee_chan_free,
529 	.send_message = scmi_optee_send_message,
530 	.mark_txdone = scmi_optee_mark_txdone,
531 	.fetch_response = scmi_optee_fetch_response,
532 	.clear_channel = scmi_optee_clear_channel,
533 };
534 
535 static int scmi_optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
536 {
537 	return ver->impl_id == TEE_IMPL_ID_OPTEE;
538 }
539 
540 static int scmi_optee_service_probe(struct device *dev)
541 {
542 	struct scmi_optee_agent *agent;
543 	struct tee_context *tee_ctx;
544 	int ret;
545 
546 	/* Only one SCMI OP-TEE device allowed */
547 	if (scmi_optee_private) {
548 		dev_err(dev, "An SCMI OP-TEE device was already initialized: only one allowed\n");
549 		return -EBUSY;
550 	}
551 
552 	tee_ctx = tee_client_open_context(NULL, scmi_optee_ctx_match, NULL, NULL);
553 	if (IS_ERR(tee_ctx))
554 		return -ENODEV;
555 
556 	agent = devm_kzalloc(dev, sizeof(*agent), GFP_KERNEL);
557 	if (!agent) {
558 		ret = -ENOMEM;
559 		goto err;
560 	}
561 
562 	agent->dev = dev;
563 	agent->tee_ctx = tee_ctx;
564 	INIT_LIST_HEAD(&agent->channel_list);
565 	mutex_init(&agent->mu);
566 
567 	ret = get_capabilities(agent);
568 	if (ret)
569 		goto err;
570 
571 	/* Ensure agent resources are all visible before scmi_optee_private is */
572 	smp_mb();
573 	scmi_optee_private = agent;
574 
575 	return 0;
576 
577 err:
578 	tee_client_close_context(tee_ctx);
579 
580 	return ret;
581 }
582 
583 static int scmi_optee_service_remove(struct device *dev)
584 {
585 	struct scmi_optee_agent *agent = scmi_optee_private;
586 
587 	if (!scmi_optee_private)
588 		return -EINVAL;
589 
590 	if (!list_empty(&scmi_optee_private->channel_list))
591 		return -EBUSY;
592 
593 	/* Ensure cleared reference is visible before resources are released */
594 	smp_store_mb(scmi_optee_private, NULL);
595 
596 	tee_client_close_context(agent->tee_ctx);
597 
598 	return 0;
599 }
600 
601 static const struct tee_client_device_id scmi_optee_service_id[] = {
602 	{
603 		UUID_INIT(0xa8cfe406, 0xd4f5, 0x4a2e,
604 			  0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99)
605 	},
606 	{ }
607 };
608 
609 MODULE_DEVICE_TABLE(tee, scmi_optee_service_id);
610 
611 static struct tee_client_driver scmi_optee_driver = {
612 	.id_table	= scmi_optee_service_id,
613 	.driver		= {
614 		.name = "scmi-optee",
615 		.bus = &tee_bus_type,
616 		.probe = scmi_optee_service_probe,
617 		.remove = scmi_optee_service_remove,
618 	},
619 };
620 
621 static int scmi_optee_init(void)
622 {
623 	return driver_register(&scmi_optee_driver.driver);
624 }
625 
626 static void scmi_optee_exit(void)
627 {
628 	if (scmi_optee_private)
629 		driver_unregister(&scmi_optee_driver.driver);
630 }
631 
632 const struct scmi_desc scmi_optee_desc = {
633 	.transport_exit = scmi_optee_exit,
634 	.ops = &scmi_optee_ops,
635 	.max_rx_timeout_ms = 30,
636 	.max_msg = 20,
637 	.max_msg_size = SCMI_OPTEE_MAX_MSG_SIZE,
638 	.sync_cmds_completed_on_ret = true,
639 };
640