0001
0002
0003
0004
0005
0006 #include <linux/io.h>
0007 #include <linux/of.h>
0008 #include <linux/of_address.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/mutex.h>
0012 #include <linux/slab.h>
0013 #include <linux/tee_drv.h>
0014 #include <linux/uuid.h>
0015 #include <uapi/linux/tee.h>
0016
0017 #include "common.h"
0018
0019 #define SCMI_OPTEE_MAX_MSG_SIZE 128
0020
0021 enum scmi_optee_pta_cmd {
0022
0023
0024
0025
0026
0027
0028 PTA_SCMI_CMD_CAPABILITIES = 0,
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL = 1,
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 PTA_SCMI_CMD_PROCESS_SMT_CHANNEL_MESSAGE = 2,
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 PTA_SCMI_CMD_GET_CHANNEL = 3,
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 PTA_SCMI_CMD_PROCESS_MSG_CHANNEL = 4,
0083 };
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 #define PTA_SCMI_CAPS_NONE 0
0098 #define PTA_SCMI_CAPS_SMT_HEADER BIT(0)
0099 #define PTA_SCMI_CAPS_MSG_HEADER BIT(1)
0100 #define PTA_SCMI_CAPS_MASK (PTA_SCMI_CAPS_SMT_HEADER | \
0101 PTA_SCMI_CAPS_MSG_HEADER)
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 struct scmi_optee_channel {
0118 u32 channel_id;
0119 u32 tee_session;
0120 u32 caps;
0121 u32 rx_len;
0122 struct mutex mu;
0123 struct scmi_chan_info *cinfo;
0124 union {
0125 struct scmi_shared_mem __iomem *shmem;
0126 struct scmi_msg_payld *msg;
0127 } req;
0128 struct tee_shm *tee_shm;
0129 struct list_head link;
0130 };
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 struct scmi_optee_agent {
0142 struct device *dev;
0143 struct tee_context *tee_ctx;
0144 u32 caps;
0145 struct mutex mu;
0146 struct list_head channel_list;
0147 };
0148
0149
0150 static struct scmi_optee_agent *scmi_optee_private;
0151
0152
0153 static int scmi_optee_init(void);
0154
0155
0156 static int open_session(struct scmi_optee_agent *agent, u32 *tee_session)
0157 {
0158 struct device *dev = agent->dev;
0159 struct tee_client_device *scmi_pta = to_tee_client_device(dev);
0160 struct tee_ioctl_open_session_arg arg = { };
0161 int ret;
0162
0163 memcpy(arg.uuid, scmi_pta->id.uuid.b, TEE_IOCTL_UUID_LEN);
0164 arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
0165
0166 ret = tee_client_open_session(agent->tee_ctx, &arg, NULL);
0167 if (ret < 0 || arg.ret) {
0168 dev_err(dev, "Can't open tee session: %d / %#x\n", ret, arg.ret);
0169 return -EOPNOTSUPP;
0170 }
0171
0172 *tee_session = arg.session;
0173
0174 return 0;
0175 }
0176
0177 static void close_session(struct scmi_optee_agent *agent, u32 tee_session)
0178 {
0179 tee_client_close_session(agent->tee_ctx, tee_session);
0180 }
0181
0182 static int get_capabilities(struct scmi_optee_agent *agent)
0183 {
0184 struct tee_ioctl_invoke_arg arg = { };
0185 struct tee_param param[1] = { };
0186 u32 caps;
0187 u32 tee_session;
0188 int ret;
0189
0190 ret = open_session(agent, &tee_session);
0191 if (ret)
0192 return ret;
0193
0194 arg.func = PTA_SCMI_CMD_CAPABILITIES;
0195 arg.session = tee_session;
0196 arg.num_params = 1;
0197
0198 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
0199
0200 ret = tee_client_invoke_func(agent->tee_ctx, &arg, param);
0201
0202 close_session(agent, tee_session);
0203
0204 if (ret < 0 || arg.ret) {
0205 dev_err(agent->dev, "Can't get capabilities: %d / %#x\n", ret, arg.ret);
0206 return -EOPNOTSUPP;
0207 }
0208
0209 caps = param[0].u.value.a;
0210
0211 if (!(caps & (PTA_SCMI_CAPS_SMT_HEADER | PTA_SCMI_CAPS_MSG_HEADER))) {
0212 dev_err(agent->dev, "OP-TEE SCMI PTA doesn't support SMT and MSG\n");
0213 return -EOPNOTSUPP;
0214 }
0215
0216 agent->caps = caps;
0217
0218 return 0;
0219 }
0220
0221 static int get_channel(struct scmi_optee_channel *channel)
0222 {
0223 struct device *dev = scmi_optee_private->dev;
0224 struct tee_ioctl_invoke_arg arg = { };
0225 struct tee_param param[1] = { };
0226 unsigned int caps = 0;
0227 int ret;
0228
0229 if (channel->tee_shm)
0230 caps = PTA_SCMI_CAPS_MSG_HEADER;
0231 else
0232 caps = PTA_SCMI_CAPS_SMT_HEADER;
0233
0234 arg.func = PTA_SCMI_CMD_GET_CHANNEL;
0235 arg.session = channel->tee_session;
0236 arg.num_params = 1;
0237
0238 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT;
0239 param[0].u.value.a = channel->channel_id;
0240 param[0].u.value.b = caps;
0241
0242 ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
0243
0244 if (ret || arg.ret) {
0245 dev_err(dev, "Can't get channel with caps %#x: %d / %#x\n", caps, ret, arg.ret);
0246 return -EOPNOTSUPP;
0247 }
0248
0249
0250 channel->channel_id = param[0].u.value.a;
0251 channel->caps = caps;
0252
0253 return 0;
0254 }
0255
0256 static int invoke_process_smt_channel(struct scmi_optee_channel *channel)
0257 {
0258 struct tee_ioctl_invoke_arg arg = {
0259 .func = PTA_SCMI_CMD_PROCESS_SMT_CHANNEL,
0260 .session = channel->tee_session,
0261 .num_params = 1,
0262 };
0263 struct tee_param param[1] = { };
0264 int ret;
0265
0266 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
0267 param[0].u.value.a = channel->channel_id;
0268
0269 ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
0270 if (ret < 0 || arg.ret) {
0271 dev_err(scmi_optee_private->dev, "Can't invoke channel %u: %d / %#x\n",
0272 channel->channel_id, ret, arg.ret);
0273 return -EIO;
0274 }
0275
0276 return 0;
0277 }
0278
0279 static int invoke_process_msg_channel(struct scmi_optee_channel *channel, size_t msg_size)
0280 {
0281 struct tee_ioctl_invoke_arg arg = {
0282 .func = PTA_SCMI_CMD_PROCESS_MSG_CHANNEL,
0283 .session = channel->tee_session,
0284 .num_params = 3,
0285 };
0286 struct tee_param param[3] = { };
0287 int ret;
0288
0289 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
0290 param[0].u.value.a = channel->channel_id;
0291
0292 param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
0293 param[1].u.memref.shm = channel->tee_shm;
0294 param[1].u.memref.size = msg_size;
0295
0296 param[2].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
0297 param[2].u.memref.shm = channel->tee_shm;
0298 param[2].u.memref.size = SCMI_OPTEE_MAX_MSG_SIZE;
0299
0300 ret = tee_client_invoke_func(scmi_optee_private->tee_ctx, &arg, param);
0301 if (ret < 0 || arg.ret) {
0302 dev_err(scmi_optee_private->dev, "Can't invoke channel %u: %d / %#x\n",
0303 channel->channel_id, ret, arg.ret);
0304 return -EIO;
0305 }
0306
0307
0308 channel->rx_len = param[2].u.memref.size;
0309
0310 return 0;
0311 }
0312
0313 static int scmi_optee_link_supplier(struct device *dev)
0314 {
0315 if (!scmi_optee_private) {
0316 if (scmi_optee_init())
0317 dev_dbg(dev, "Optee bus not yet ready\n");
0318
0319
0320 return -EPROBE_DEFER;
0321 }
0322
0323 if (!device_link_add(dev, scmi_optee_private->dev, DL_FLAG_AUTOREMOVE_CONSUMER)) {
0324 dev_err(dev, "Adding link to supplier optee device failed\n");
0325 return -ECANCELED;
0326 }
0327
0328 return 0;
0329 }
0330
0331 static bool scmi_optee_chan_available(struct device *dev, int idx)
0332 {
0333 u32 channel_id;
0334
0335 return !of_property_read_u32_index(dev->of_node, "linaro,optee-channel-id",
0336 idx, &channel_id);
0337 }
0338
0339 static void scmi_optee_clear_channel(struct scmi_chan_info *cinfo)
0340 {
0341 struct scmi_optee_channel *channel = cinfo->transport_info;
0342
0343 if (!channel->tee_shm)
0344 shmem_clear_channel(channel->req.shmem);
0345 }
0346
0347 static int setup_dynamic_shmem(struct device *dev, struct scmi_optee_channel *channel)
0348 {
0349 const size_t msg_size = SCMI_OPTEE_MAX_MSG_SIZE;
0350 void *shbuf;
0351
0352 channel->tee_shm = tee_shm_alloc_kernel_buf(scmi_optee_private->tee_ctx, msg_size);
0353 if (IS_ERR(channel->tee_shm)) {
0354 dev_err(channel->cinfo->dev, "shmem allocation failed\n");
0355 return -ENOMEM;
0356 }
0357
0358 shbuf = tee_shm_get_va(channel->tee_shm, 0);
0359 memset(shbuf, 0, msg_size);
0360 channel->req.msg = shbuf;
0361 channel->rx_len = msg_size;
0362
0363 return 0;
0364 }
0365
0366 static int setup_static_shmem(struct device *dev, struct scmi_chan_info *cinfo,
0367 struct scmi_optee_channel *channel)
0368 {
0369 struct device_node *np;
0370 resource_size_t size;
0371 struct resource res;
0372 int ret;
0373
0374 np = of_parse_phandle(cinfo->dev->of_node, "shmem", 0);
0375 if (!of_device_is_compatible(np, "arm,scmi-shmem")) {
0376 ret = -ENXIO;
0377 goto out;
0378 }
0379
0380 ret = of_address_to_resource(np, 0, &res);
0381 if (ret) {
0382 dev_err(dev, "Failed to get SCMI Tx shared memory\n");
0383 goto out;
0384 }
0385
0386 size = resource_size(&res);
0387
0388 channel->req.shmem = devm_ioremap(dev, res.start, size);
0389 if (!channel->req.shmem) {
0390 dev_err(dev, "Failed to ioremap SCMI Tx shared memory\n");
0391 ret = -EADDRNOTAVAIL;
0392 goto out;
0393 }
0394
0395 ret = 0;
0396
0397 out:
0398 of_node_put(np);
0399
0400 return ret;
0401 }
0402
0403 static int setup_shmem(struct device *dev, struct scmi_chan_info *cinfo,
0404 struct scmi_optee_channel *channel)
0405 {
0406 if (of_find_property(cinfo->dev->of_node, "shmem", NULL))
0407 return setup_static_shmem(dev, cinfo, channel);
0408 else
0409 return setup_dynamic_shmem(dev, channel);
0410 }
0411
0412 static int scmi_optee_chan_setup(struct scmi_chan_info *cinfo, struct device *dev, bool tx)
0413 {
0414 struct scmi_optee_channel *channel;
0415 uint32_t channel_id;
0416 int ret;
0417
0418 if (!tx)
0419 return -ENODEV;
0420
0421 channel = devm_kzalloc(dev, sizeof(*channel), GFP_KERNEL);
0422 if (!channel)
0423 return -ENOMEM;
0424
0425 ret = of_property_read_u32_index(cinfo->dev->of_node, "linaro,optee-channel-id",
0426 0, &channel_id);
0427 if (ret)
0428 return ret;
0429
0430 cinfo->transport_info = channel;
0431 channel->cinfo = cinfo;
0432 channel->channel_id = channel_id;
0433 mutex_init(&channel->mu);
0434
0435 ret = setup_shmem(dev, cinfo, channel);
0436 if (ret)
0437 return ret;
0438
0439 ret = open_session(scmi_optee_private, &channel->tee_session);
0440 if (ret)
0441 goto err_free_shm;
0442
0443 ret = get_channel(channel);
0444 if (ret)
0445 goto err_close_sess;
0446
0447
0448 cinfo->no_completion_irq = true;
0449
0450 mutex_lock(&scmi_optee_private->mu);
0451 list_add(&channel->link, &scmi_optee_private->channel_list);
0452 mutex_unlock(&scmi_optee_private->mu);
0453
0454 return 0;
0455
0456 err_close_sess:
0457 close_session(scmi_optee_private, channel->tee_session);
0458 err_free_shm:
0459 if (channel->tee_shm)
0460 tee_shm_free(channel->tee_shm);
0461
0462 return ret;
0463 }
0464
0465 static int scmi_optee_chan_free(int id, void *p, void *data)
0466 {
0467 struct scmi_chan_info *cinfo = p;
0468 struct scmi_optee_channel *channel = cinfo->transport_info;
0469
0470 mutex_lock(&scmi_optee_private->mu);
0471 list_del(&channel->link);
0472 mutex_unlock(&scmi_optee_private->mu);
0473
0474 close_session(scmi_optee_private, channel->tee_session);
0475
0476 if (channel->tee_shm) {
0477 tee_shm_free(channel->tee_shm);
0478 channel->tee_shm = NULL;
0479 }
0480
0481 cinfo->transport_info = NULL;
0482 channel->cinfo = NULL;
0483
0484 scmi_free_channel(cinfo, data, id);
0485
0486 return 0;
0487 }
0488
0489 static int scmi_optee_send_message(struct scmi_chan_info *cinfo,
0490 struct scmi_xfer *xfer)
0491 {
0492 struct scmi_optee_channel *channel = cinfo->transport_info;
0493 int ret;
0494
0495 mutex_lock(&channel->mu);
0496
0497 if (channel->tee_shm) {
0498 msg_tx_prepare(channel->req.msg, xfer);
0499 ret = invoke_process_msg_channel(channel, msg_command_size(xfer));
0500 } else {
0501 shmem_tx_prepare(channel->req.shmem, xfer);
0502 ret = invoke_process_smt_channel(channel);
0503 }
0504
0505 if (ret)
0506 mutex_unlock(&channel->mu);
0507
0508 return ret;
0509 }
0510
0511 static void scmi_optee_fetch_response(struct scmi_chan_info *cinfo,
0512 struct scmi_xfer *xfer)
0513 {
0514 struct scmi_optee_channel *channel = cinfo->transport_info;
0515
0516 if (channel->tee_shm)
0517 msg_fetch_response(channel->req.msg, channel->rx_len, xfer);
0518 else
0519 shmem_fetch_response(channel->req.shmem, xfer);
0520 }
0521
0522 static void scmi_optee_mark_txdone(struct scmi_chan_info *cinfo, int ret,
0523 struct scmi_xfer *__unused)
0524 {
0525 struct scmi_optee_channel *channel = cinfo->transport_info;
0526
0527 mutex_unlock(&channel->mu);
0528 }
0529
0530 static struct scmi_transport_ops scmi_optee_ops = {
0531 .link_supplier = scmi_optee_link_supplier,
0532 .chan_available = scmi_optee_chan_available,
0533 .chan_setup = scmi_optee_chan_setup,
0534 .chan_free = scmi_optee_chan_free,
0535 .send_message = scmi_optee_send_message,
0536 .mark_txdone = scmi_optee_mark_txdone,
0537 .fetch_response = scmi_optee_fetch_response,
0538 .clear_channel = scmi_optee_clear_channel,
0539 };
0540
0541 static int scmi_optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
0542 {
0543 return ver->impl_id == TEE_IMPL_ID_OPTEE;
0544 }
0545
0546 static int scmi_optee_service_probe(struct device *dev)
0547 {
0548 struct scmi_optee_agent *agent;
0549 struct tee_context *tee_ctx;
0550 int ret;
0551
0552
0553 if (scmi_optee_private) {
0554 dev_err(dev, "An SCMI OP-TEE device was already initialized: only one allowed\n");
0555 return -EBUSY;
0556 }
0557
0558 tee_ctx = tee_client_open_context(NULL, scmi_optee_ctx_match, NULL, NULL);
0559 if (IS_ERR(tee_ctx))
0560 return -ENODEV;
0561
0562 agent = devm_kzalloc(dev, sizeof(*agent), GFP_KERNEL);
0563 if (!agent) {
0564 ret = -ENOMEM;
0565 goto err;
0566 }
0567
0568 agent->dev = dev;
0569 agent->tee_ctx = tee_ctx;
0570 INIT_LIST_HEAD(&agent->channel_list);
0571 mutex_init(&agent->mu);
0572
0573 ret = get_capabilities(agent);
0574 if (ret)
0575 goto err;
0576
0577
0578 smp_mb();
0579 scmi_optee_private = agent;
0580
0581 return 0;
0582
0583 err:
0584 tee_client_close_context(tee_ctx);
0585
0586 return ret;
0587 }
0588
0589 static int scmi_optee_service_remove(struct device *dev)
0590 {
0591 struct scmi_optee_agent *agent = scmi_optee_private;
0592
0593 if (!scmi_optee_private)
0594 return -EINVAL;
0595
0596 if (!list_empty(&scmi_optee_private->channel_list))
0597 return -EBUSY;
0598
0599
0600 smp_store_mb(scmi_optee_private, NULL);
0601
0602 tee_client_close_context(agent->tee_ctx);
0603
0604 return 0;
0605 }
0606
0607 static const struct tee_client_device_id scmi_optee_service_id[] = {
0608 {
0609 UUID_INIT(0xa8cfe406, 0xd4f5, 0x4a2e,
0610 0x9f, 0x8d, 0xa2, 0x5d, 0xc7, 0x54, 0xc0, 0x99)
0611 },
0612 { }
0613 };
0614
0615 MODULE_DEVICE_TABLE(tee, scmi_optee_service_id);
0616
0617 static struct tee_client_driver scmi_optee_driver = {
0618 .id_table = scmi_optee_service_id,
0619 .driver = {
0620 .name = "scmi-optee",
0621 .bus = &tee_bus_type,
0622 .probe = scmi_optee_service_probe,
0623 .remove = scmi_optee_service_remove,
0624 },
0625 };
0626
0627 static int scmi_optee_init(void)
0628 {
0629 return driver_register(&scmi_optee_driver.driver);
0630 }
0631
0632 static void scmi_optee_exit(void)
0633 {
0634 if (scmi_optee_private)
0635 driver_unregister(&scmi_optee_driver.driver);
0636 }
0637
0638 const struct scmi_desc scmi_optee_desc = {
0639 .transport_exit = scmi_optee_exit,
0640 .ops = &scmi_optee_ops,
0641 .max_rx_timeout_ms = 30,
0642 .max_msg = 20,
0643 .max_msg_size = SCMI_OPTEE_MAX_MSG_SIZE,
0644 .sync_cmds_completed_on_ret = true,
0645 };