18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2019 Linaro Ltd. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/slab.h> 108c2ecf20Sopenharmony_ci#include <linux/tee_drv.h> 118c2ecf20Sopenharmony_ci#include <linux/uuid.h> 128c2ecf20Sopenharmony_ci#include "optee_private.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_cistatic int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data) 158c2ecf20Sopenharmony_ci{ 168c2ecf20Sopenharmony_ci if (ver->impl_id == TEE_IMPL_ID_OPTEE) 178c2ecf20Sopenharmony_ci return 1; 188c2ecf20Sopenharmony_ci else 198c2ecf20Sopenharmony_ci return 0; 208c2ecf20Sopenharmony_ci} 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic int get_devices(struct tee_context *ctx, u32 session, 238c2ecf20Sopenharmony_ci struct tee_shm *device_shm, u32 *shm_size, 248c2ecf20Sopenharmony_ci u32 func) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci int ret = 0; 278c2ecf20Sopenharmony_ci struct tee_ioctl_invoke_arg inv_arg; 288c2ecf20Sopenharmony_ci struct tee_param param[4]; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci memset(&inv_arg, 0, sizeof(inv_arg)); 318c2ecf20Sopenharmony_ci memset(¶m, 0, sizeof(param)); 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci inv_arg.func = func; 348c2ecf20Sopenharmony_ci inv_arg.session = session; 358c2ecf20Sopenharmony_ci inv_arg.num_params = 4; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci /* Fill invoke cmd params */ 388c2ecf20Sopenharmony_ci param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT; 398c2ecf20Sopenharmony_ci param[0].u.memref.shm = device_shm; 408c2ecf20Sopenharmony_ci param[0].u.memref.size = *shm_size; 418c2ecf20Sopenharmony_ci param[0].u.memref.shm_offs = 0; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci ret = tee_client_invoke_func(ctx, &inv_arg, param); 448c2ecf20Sopenharmony_ci if ((ret < 0) || ((inv_arg.ret != TEEC_SUCCESS) && 458c2ecf20Sopenharmony_ci (inv_arg.ret != TEEC_ERROR_SHORT_BUFFER))) { 468c2ecf20Sopenharmony_ci pr_err("PTA_CMD_GET_DEVICES invoke function err: %x\n", 478c2ecf20Sopenharmony_ci inv_arg.ret); 488c2ecf20Sopenharmony_ci return -EINVAL; 498c2ecf20Sopenharmony_ci } 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci *shm_size = param[0].u.memref.size; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci return 0; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic void optee_release_device(struct device *dev) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct tee_client_device *optee_device = to_tee_client_device(dev); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci kfree(optee_device); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic ssize_t need_supplicant_show(struct device *dev, 648c2ecf20Sopenharmony_ci struct device_attribute *attr, 658c2ecf20Sopenharmony_ci char *buf) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci return 0; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(need_supplicant); 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int optee_register_device(const uuid_t *device_uuid, u32 func) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci struct tee_client_device *optee_device = NULL; 758c2ecf20Sopenharmony_ci int rc; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci optee_device = kzalloc(sizeof(*optee_device), GFP_KERNEL); 788c2ecf20Sopenharmony_ci if (!optee_device) 798c2ecf20Sopenharmony_ci return -ENOMEM; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci optee_device->dev.bus = &tee_bus_type; 828c2ecf20Sopenharmony_ci optee_device->dev.release = optee_release_device; 838c2ecf20Sopenharmony_ci if (dev_set_name(&optee_device->dev, "optee-ta-%pUb", device_uuid)) { 848c2ecf20Sopenharmony_ci kfree(optee_device); 858c2ecf20Sopenharmony_ci return -ENOMEM; 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci uuid_copy(&optee_device->id.uuid, device_uuid); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci rc = device_register(&optee_device->dev); 908c2ecf20Sopenharmony_ci if (rc) { 918c2ecf20Sopenharmony_ci pr_err("device registration failed, err: %d\n", rc); 928c2ecf20Sopenharmony_ci put_device(&optee_device->dev); 938c2ecf20Sopenharmony_ci return rc; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci if (func == PTA_CMD_GET_DEVICES_SUPP) 978c2ecf20Sopenharmony_ci device_create_file(&optee_device->dev, 988c2ecf20Sopenharmony_ci &dev_attr_need_supplicant); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci return 0; 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic int __optee_enumerate_devices(u32 func) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci const uuid_t pta_uuid = 1068c2ecf20Sopenharmony_ci UUID_INIT(0x7011a688, 0xddde, 0x4053, 1078c2ecf20Sopenharmony_ci 0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8); 1088c2ecf20Sopenharmony_ci struct tee_ioctl_open_session_arg sess_arg; 1098c2ecf20Sopenharmony_ci struct tee_shm *device_shm = NULL; 1108c2ecf20Sopenharmony_ci const uuid_t *device_uuid = NULL; 1118c2ecf20Sopenharmony_ci struct tee_context *ctx = NULL; 1128c2ecf20Sopenharmony_ci u32 shm_size = 0, idx, num_devices = 0; 1138c2ecf20Sopenharmony_ci int rc; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci memset(&sess_arg, 0, sizeof(sess_arg)); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* Open context with OP-TEE driver */ 1188c2ecf20Sopenharmony_ci ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL); 1198c2ecf20Sopenharmony_ci if (IS_ERR(ctx)) 1208c2ecf20Sopenharmony_ci return -ENODEV; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* Open session with device enumeration pseudo TA */ 1238c2ecf20Sopenharmony_ci memcpy(sess_arg.uuid, pta_uuid.b, TEE_IOCTL_UUID_LEN); 1248c2ecf20Sopenharmony_ci sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC; 1258c2ecf20Sopenharmony_ci sess_arg.num_params = 0; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci rc = tee_client_open_session(ctx, &sess_arg, NULL); 1288c2ecf20Sopenharmony_ci if ((rc < 0) || (sess_arg.ret != TEEC_SUCCESS)) { 1298c2ecf20Sopenharmony_ci /* Device enumeration pseudo TA not found */ 1308c2ecf20Sopenharmony_ci rc = 0; 1318c2ecf20Sopenharmony_ci goto out_ctx; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci rc = get_devices(ctx, sess_arg.session, NULL, &shm_size, func); 1358c2ecf20Sopenharmony_ci if (rc < 0 || !shm_size) 1368c2ecf20Sopenharmony_ci goto out_sess; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci device_shm = tee_shm_alloc(ctx, shm_size, 1398c2ecf20Sopenharmony_ci TEE_SHM_MAPPED | TEE_SHM_DMA_BUF); 1408c2ecf20Sopenharmony_ci if (IS_ERR(device_shm)) { 1418c2ecf20Sopenharmony_ci pr_err("tee_shm_alloc failed\n"); 1428c2ecf20Sopenharmony_ci rc = PTR_ERR(device_shm); 1438c2ecf20Sopenharmony_ci goto out_sess; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci rc = get_devices(ctx, sess_arg.session, device_shm, &shm_size, func); 1478c2ecf20Sopenharmony_ci if (rc < 0) 1488c2ecf20Sopenharmony_ci goto out_shm; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci device_uuid = tee_shm_get_va(device_shm, 0); 1518c2ecf20Sopenharmony_ci if (IS_ERR(device_uuid)) { 1528c2ecf20Sopenharmony_ci pr_err("tee_shm_get_va failed\n"); 1538c2ecf20Sopenharmony_ci rc = PTR_ERR(device_uuid); 1548c2ecf20Sopenharmony_ci goto out_shm; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci num_devices = shm_size / sizeof(uuid_t); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci for (idx = 0; idx < num_devices; idx++) { 1608c2ecf20Sopenharmony_ci rc = optee_register_device(&device_uuid[idx], func); 1618c2ecf20Sopenharmony_ci if (rc) 1628c2ecf20Sopenharmony_ci goto out_shm; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ciout_shm: 1668c2ecf20Sopenharmony_ci tee_shm_free(device_shm); 1678c2ecf20Sopenharmony_ciout_sess: 1688c2ecf20Sopenharmony_ci tee_client_close_session(ctx, sess_arg.session); 1698c2ecf20Sopenharmony_ciout_ctx: 1708c2ecf20Sopenharmony_ci tee_client_close_context(ctx); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci return rc; 1738c2ecf20Sopenharmony_ci} 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ciint optee_enumerate_devices(u32 func) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci return __optee_enumerate_devices(func); 1788c2ecf20Sopenharmony_ci} 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic int __optee_unregister_device(struct device *dev, void *data) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci if (!strncmp(dev_name(dev), "optee-ta", strlen("optee-ta"))) 1838c2ecf20Sopenharmony_ci device_unregister(dev); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci return 0; 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_civoid optee_unregister_devices(void) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci bus_for_each_dev(&tee_bus_type, NULL, NULL, 1918c2ecf20Sopenharmony_ci __optee_unregister_device); 1928c2ecf20Sopenharmony_ci} 193