1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019-2021 Linaro Ltd.
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>
19 #define SCMI_OPTEE_MAX_MSG_SIZE 128
21 enum scmi_optee_pta_cmd {
23 * PTA_SCMI_CMD_CAPABILITIES - Get channel capabilities
25 * [out] value[0].a: Capability bit mask (enum pta_scmi_caps)
26 * [out] value[0].b: Extended capabilities or 0
28 PTA_SCMI_CMD_CAPABILITIES = 0,
31 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL - Process SCMI message in SMT buffer
33 * [in] value[0].a: Channel handle
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).
41 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,
44 * PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE - Process SMT/SCMI message
46 * [in] value[0].a: Channel handle
47 * [in/out] memref[1]: Message/response buffer (SMT and SCMI payload)
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).
55 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,
58 * PTA_SCMI_CMD_GET_CHANNEL - Get channel handle
60 * SCMI shm information are 0 if agent expects to use OP-TEE regular SHM
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)
66 PTA_SCMI_CMD_GET_CHANNEL = 3,
69 * PTA_SCMI_CMD_PROCESS_MSG_CHANNEL - Process SCMI message in a MSG
70 * buffer pointed by memref parameters
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)
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
82 PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4,
86 * OP-TEE SCMI service capabilities bit flags (32bit)
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.
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
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)
104 * struct scmi_optee_channel - Description of an OP-TEE SCMI channel
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 * @rx_len: Response size
110 * @mu: Mutex protection on channel access
111 * @cinfo: SCMI channel information
112 * @req: union for SCMI interface
113 * @req.shmem: Virtual base address of the shared memory
114 * @req.msg: Shared memory protocol handle for SCMI request and
115 * synchronous response
116 * @tee_shm: TEE shared memory handle @req or NULL if using IOMEM shmem
117 * @link: Reference in agent's channel list
119 struct scmi_optee_channel {
125 struct scmi_chan_info *cinfo;
127 struct scmi_shared_mem __iomem *shmem;
128 struct scmi_msg_payld *msg;
130 struct tee_shm *tee_shm;
131 struct list_head link;
135 * struct scmi_optee_agent - OP-TEE transport private data
137 * @dev: Device used for communication with TEE
138 * @tee_ctx: TEE context used for communication
139 * @caps: Supported channel capabilities
140 * @mu: Mutex for protection of @channel_list
141 * @channel_list: List of all created channels for the agent
143 struct scmi_optee_agent {
145 struct tee_context *tee_ctx;
148 struct list_head channel_list;
151 /* There can be only 1 SCMI service in OP-TEE we connect to */
152 static struct scmi_optee_agent *scmi_optee_private;
154 /* Forward reference to scmi_optee transport initialization */
155 static int scmi_optee_init(void);
157 /* Open a session toward SCMI OP-TEE service with REE_KERNEL identity */
158 static int open_session(struct scmi_optee_agent *agent, u32 *tee_session)
160 struct device *dev = agent->dev;
161 struct tee_client_device *scmi_pta = to_tee_client_device(dev);
162 struct tee_ioctl_open_session_arg arg = { };
165 memcpy(arg.uuid, scmi_pta->id.uuid.b, TEE_IOCTL_UUID_LEN);
166 arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
168 ret = tee_client_open_session(agent->tee_ctx, &arg, NULL);
169 if (ret < 0 || arg.ret) {
170 dev_err(dev, "Can't open tee session: %d / %#x\n", ret, arg.ret);
174 *tee_session = arg.session;
179 static void close_session(struct scmi_optee_agent *agent, u32 tee_session)
181 tee_client_close_session(agent->tee_ctx, tee_session);
184 static int get_capabilities(struct scmi_optee_agent *agent)
186 struct tee_ioctl_invoke_arg arg = { };
187 struct tee_param param[1] = { };
192 ret = open_session(agent, &tee_session);
196 arg.func = PTA_SCMI_CMD_CAPABILITIES;
197 arg.session = tee_session;
200 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
202 ret = tee_client_invoke_func(agent->tee_ctx, &arg, param);
204 close_session(agent, tee_session);
206 if (ret < 0 || arg.ret) {
207 dev_err(agent->dev, "Can't get capabilities: %d / %#x\n", ret, arg.ret);
211 caps = param[0].u.value.a;
213 if (!(caps & (PTA_SCMI_CAPS_SMT_HEADER | PTA_SCMI_CAPS_MSG_HEADER))) {
214 dev_err(agent->dev, "OP-TEE SCMI PTA doesn't support SMT and MSG\n");
223 static int get_channel(struct scmi_optee_channel *channel)
225 struct device *dev = scmi_optee_private->dev;
226 struct tee_ioctl_invoke_arg arg = { };
227 struct tee_param param[1] = { };
228 unsigned int caps = 0;
231 if (channel->tee_shm)
232 caps = PTA_SCMI_CAPS_MSG_HEADER;
234 caps = PTA_SCMI_CAPS_SMT_HEADER;
236 arg.func = PTA_SCMI_CMD_GET_CHANNEL;
237 arg.session = channel->tee_session;
240 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
241 param[0].u.value.a = channel->channel_id;
242 param[0].u.value.b = caps;
244 ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
246 if (ret || arg.ret) {
247 dev_err(dev, "Can't get channel with caps %#x: %d / %#x\n", caps, ret, arg.ret);
251 /* From now on use channel identifer provided by OP-TEE SCMI service */
252 channel->channel_id = param[0].u.value.a;
253 channel->caps = caps;
258 static int invoke_process_smt_channel(struct scmi_optee_channel *channel)
260 struct tee_ioctl_invoke_arg arg = {
261 .func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL,
262 .session = channel->tee_session,
265 struct tee_param param[1] = { };
268 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
269 param[0].u.value.a = channel->channel_id;
271 ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
272 if (ret < 0 || arg.ret) {
273 dev_err(scmi_optee_private->dev, "Can't invoke channel %u: %d / %#x\n",
274 channel->channel_id, ret, arg.ret);
281 static int invoke_process_msg_channel(struct scmi_optee_channel *channel, size_t msg_size)
283 struct tee_ioctl_invoke_arg arg = {
284 .func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL,
285 .session = channel->tee_session,
288 struct tee_param param[3] = { };
291 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
292 param[0].u.value.a = channel->channel_id;
294 param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
295 param[1].u.memref.shm = channel->tee_shm;
296 param[1].u.memref.size = msg_size;
298 param[2].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
299 param[2].u.memref.shm = channel->tee_shm;
300 param[2].u.memref.size = SCMI_OPTEE_MAX_MSG_SIZE;
302 ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
303 if (ret < 0 || arg.ret) {
304 dev_err(scmi_optee_private->dev, "Can't invoke channel %u: %d / %#x\n",
305 channel->channel_id, ret, arg.ret);
309 /* Save response size */
310 channel->rx_len = param[2].u.memref.size;
315 static int scmi_optee_link_supplier(struct device *dev)
317 if (!scmi_optee_private) {
318 if (scmi_optee_init())
319 dev_dbg(dev, "Optee bus not yet ready\n");
321 /* Wait for optee bus */
322 return -EPROBE_DEFER;
325 if (!device_link_add(dev, scmi_optee_private->dev, DL_FLAG_AUTOREMOVE_CONSUMER)) {
326 dev_err(dev, "Adding link to supplier optee device failed\n");
333 static bool scmi_optee_chan_available(struct device_node *of_node, int idx)
337 return !of_property_read_u32_index(of_node, "linaro,optee-channel-id",
341 static void scmi_optee_clear_channel(struct scmi_chan_info *cinfo)
343 struct scmi_optee_channel *channel = cinfo->transport_info;
345 if (!channel->tee_shm)
346 shmem_clear_channel(channel->req.shmem);
349 static int setup_dynamic_shmem(struct device *dev, struct scmi_optee_channel *channel)
351 const size_t msg_size = SCMI_OPTEE_MAX_MSG_SIZE;
354 channel->tee_shm = tee_shm_alloc_kernel_buf(scmi_optee_private->tee_ctx, msg_size);
355 if (IS_ERR(channel->tee_shm)) {
356 dev_err(channel->cinfo->dev, "shmem allocation failed\n");
360 shbuf = tee_shm_get_va(channel->tee_shm, 0);
361 memset(shbuf, 0, msg_size);
362 channel->req.msg = shbuf;
363 channel->rx_len = msg_size;
368 static int setup_static_shmem(struct device *dev, struct scmi_chan_info *cinfo,
369 struct scmi_optee_channel *channel)
371 struct device_node *np;
372 resource_size_t size;
376 np = of_parse_phandle(cinfo->dev->of_node, "shmem", 0);
377 if (!of_device_is_compatible(np, "arm,scmi-shmem")) {
382 ret = of_address_to_resource(np, 0, &res);
384 dev_err(dev, "Failed to get SCMI Tx shared memory\n");
388 size = resource_size(&res);
390 channel->req.shmem = devm_ioremap(dev, res.start, size);
391 if (!channel->req.shmem) {
392 dev_err(dev, "Failed to ioremap SCMI Tx shared memory\n");
393 ret = -EADDRNOTAVAIL;
405 static int setup_shmem(struct device *dev, struct scmi_chan_info *cinfo,
406 struct scmi_optee_channel *channel)
408 if (of_property_present(cinfo->dev->of_node, "shmem"))
409 return setup_static_shmem(dev, cinfo, channel);
411 return setup_dynamic_shmem(dev, channel);
414 static int scmi_optee_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, bool tx)
416 struct scmi_optee_channel *channel;
423 channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
427 ret = of_property_read_u32_index(cinfo->dev->of_node, "linaro,optee-channel-id",
432 cinfo->transport_info = channel;
433 channel->cinfo = cinfo;
434 channel->channel_id = channel_id;
435 mutex_init(&channel->mu);
437 ret = setup_shmem(dev, cinfo, channel);
441 ret = open_session(scmi_optee_private, &channel->tee_session);
445 ret = tee_client_system_session(scmi_optee_private->tee_ctx, channel->tee_session);
447 dev_warn(dev, "Could not switch to system session, do best effort\n");
449 ret = get_channel(channel);
454 cinfo->no_completion_irq = true;
456 mutex_lock(&scmi_optee_private->mu);
457 list_add(&channel->link, &scmi_optee_private->channel_list);
458 mutex_unlock(&scmi_optee_private->mu);
463 close_session(scmi_optee_private, channel->tee_session);
465 if (channel->tee_shm)
466 tee_shm_free(channel->tee_shm);
471 static int scmi_optee_chan_free(int id, void *p, void *data)
473 struct scmi_chan_info *cinfo = p;
474 struct scmi_optee_channel *channel = cinfo->transport_info;
476 mutex_lock(&scmi_optee_private->mu);
477 list_del(&channel->link);
478 mutex_unlock(&scmi_optee_private->mu);
480 close_session(scmi_optee_private, channel->tee_session);
482 if (channel->tee_shm) {
483 tee_shm_free(channel->tee_shm);
484 channel->tee_shm = NULL;
487 cinfo->transport_info = NULL;
488 channel->cinfo = NULL;
493 static int scmi_optee_send_message(struct scmi_chan_info *cinfo,
494 struct scmi_xfer *xfer)
496 struct scmi_optee_channel *channel = cinfo->transport_info;
499 mutex_lock(&channel->mu);
501 if (channel->tee_shm) {
502 msg_tx_prepare(channel->req.msg, xfer);
503 ret = invoke_process_msg_channel(channel, msg_command_size(xfer));
505 shmem_tx_prepare(channel->req.shmem, xfer, cinfo);
506 ret = invoke_process_smt_channel(channel);
510 mutex_unlock(&channel->mu);
515 static void scmi_optee_fetch_response(struct scmi_chan_info *cinfo,
516 struct scmi_xfer *xfer)
518 struct scmi_optee_channel *channel = cinfo->transport_info;
520 if (channel->tee_shm)
521 msg_fetch_response(channel->req.msg, channel->rx_len, xfer);
523 shmem_fetch_response(channel->req.shmem, xfer);
526 static void scmi_optee_mark_txdone(struct scmi_chan_info *cinfo, int ret,
527 struct scmi_xfer *__unused)
529 struct scmi_optee_channel *channel = cinfo->transport_info;
531 mutex_unlock(&channel->mu);
534 static struct scmi_transport_ops scmi_optee_ops = {
535 .link_supplier = scmi_optee_link_supplier,
536 .chan_available = scmi_optee_chan_available,
537 .chan_setup = scmi_optee_chan_setup,
538 .chan_free = scmi_optee_chan_free,
539 .send_message = scmi_optee_send_message,
540 .mark_txdone = scmi_optee_mark_txdone,
541 .fetch_response = scmi_optee_fetch_response,
542 .clear_channel = scmi_optee_clear_channel,
545 static int scmi_optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
547 return ver->impl_id == TEE_IMPL_ID_OPTEE;
550 static int scmi_optee_service_probe(struct device *dev)
552 struct scmi_optee_agent *agent;
553 struct tee_context *tee_ctx;
556 /* Only one SCMI OP-TEE device allowed */
557 if (scmi_optee_private) {
558 dev_err(dev, "An SCMI OP-TEE device was already initialized: only one allowed\n");
562 tee_ctx = tee_client_open_context(NULL, scmi_optee_ctx_match, NULL, NULL);
566 agent = devm_kzalloc(dev, sizeof(*agent), GFP_KERNEL);
573 agent->tee_ctx = tee_ctx;
574 INIT_LIST_HEAD(&agent->channel_list);
575 mutex_init(&agent->mu);
577 ret = get_capabilities(agent);
581 /* Ensure agent resources are all visible before scmi_optee_private is */
583 scmi_optee_private = agent;
588 tee_client_close_context(tee_ctx);
593 static int scmi_optee_service_remove(struct device *dev)
595 struct scmi_optee_agent *agent = scmi_optee_private;
597 if (!scmi_optee_private)
600 if (!list_empty(&scmi_optee_private->channel_list))
603 /* Ensure cleared reference is visible before resources are released */
604 smp_store_mb(scmi_optee_private, NULL);
606 tee_client_close_context(agent->tee_ctx);
611 static const struct tee_client_device_id scmi_optee_service_id[] = {
613 UUID_INIT(0xa8cfe406, 0xd4f5, 0x4a2e,
614 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99)
619 MODULE_DEVICE_TABLE(tee, scmi_optee_service_id);
621 static struct tee_client_driver scmi_optee_driver = {
622 .id_table = scmi_optee_service_id,
624 .name = "scmi-optee",
625 .bus = &tee_bus_type,
626 .probe = scmi_optee_service_probe,
627 .remove = scmi_optee_service_remove,
631 static int scmi_optee_init(void)
633 return driver_register(&scmi_optee_driver.driver);
636 static void scmi_optee_exit(void)
638 if (scmi_optee_private)
639 driver_unregister(&scmi_optee_driver.driver);
642 const struct scmi_desc scmi_optee_desc = {
643 .transport_exit = scmi_optee_exit,
644 .ops = &scmi_optee_ops,
645 .max_rx_timeout_ms = 30,
647 .max_msg_size = SCMI_OPTEE_MAX_MSG_SIZE,
648 .sync_cmds_completed_on_ret = true,