^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2019 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/tee_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "optee_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (ver->impl_id == TEE_IMPL_ID_OPTEE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int get_devices(struct tee_context *ctx, u32 session,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct tee_shm *device_shm, u32 *shm_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u32 func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct tee_ioctl_invoke_arg inv_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct tee_param param[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) memset(&inv_arg, 0, sizeof(inv_arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) memset(¶m, 0, sizeof(param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) inv_arg.func = func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) inv_arg.session = session;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) inv_arg.num_params = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* Fill invoke cmd params */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) param[0].u.memref.shm = device_shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) param[0].u.memref.size = *shm_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) param[0].u.memref.shm_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ret = tee_client_invoke_func(ctx, &inv_arg, param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if ((ret < 0) || ((inv_arg.ret != TEEC_SUCCESS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) (inv_arg.ret != TEEC_ERROR_SHORT_BUFFER))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pr_err("PTA_CMD_GET_DEVICES invoke function err: %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) inv_arg.ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *shm_size = param[0].u.memref.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static void optee_release_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct tee_client_device *optee_device = to_tee_client_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) kfree(optee_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int optee_register_device(const uuid_t *device_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct tee_client_device *optee_device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) optee_device = kzalloc(sizeof(*optee_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!optee_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) optee_device->dev.bus = &tee_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) optee_device->dev.release = optee_release_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (dev_set_name(&optee_device->dev, "optee-ta-%pUb", device_uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) kfree(optee_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) uuid_copy(&optee_device->id.uuid, device_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) rc = device_register(&optee_device->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) pr_err("device registration failed, err: %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) kfree(optee_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int __optee_enumerate_devices(u32 func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) const uuid_t pta_uuid =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) UUID_INIT(0x7011a688, 0xddde, 0x4053,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) 0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct tee_ioctl_open_session_arg sess_arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct tee_shm *device_shm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) const uuid_t *device_uuid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct tee_context *ctx = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) u32 shm_size = 0, idx, num_devices = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) memset(&sess_arg, 0, sizeof(sess_arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /* Open context with OP-TEE driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (IS_ERR(ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* Open session with device enumeration pseudo TA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) memcpy(sess_arg.uuid, pta_uuid.b, TEE_IOCTL_UUID_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) sess_arg.num_params = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rc = tee_client_open_session(ctx, &sess_arg, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if ((rc < 0) || (sess_arg.ret != TEEC_SUCCESS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Device enumeration pseudo TA not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto out_ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) rc = get_devices(ctx, sess_arg.session, NULL, &shm_size, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (rc < 0 || !shm_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto out_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) device_shm = tee_shm_alloc(ctx, shm_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) TEE_SHM_MAPPED | TEE_SHM_DMA_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (IS_ERR(device_shm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pr_err("tee_shm_alloc failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) rc = PTR_ERR(device_shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto out_sess;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) rc = get_devices(ctx, sess_arg.session, device_shm, &shm_size, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto out_shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) device_uuid = tee_shm_get_va(device_shm, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (IS_ERR(device_uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pr_err("tee_shm_get_va failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) rc = PTR_ERR(device_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) goto out_shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) num_devices = shm_size / sizeof(uuid_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) for (idx = 0; idx < num_devices; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) rc = optee_register_device(&device_uuid[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto out_shm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) out_shm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) tee_shm_free(device_shm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) out_sess:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) tee_client_close_session(ctx, sess_arg.session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) out_ctx:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) tee_client_close_context(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int optee_enumerate_devices(u32 func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return __optee_enumerate_devices(func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int __optee_unregister_device(struct device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (!strncmp(dev_name(dev), "optee-ta", strlen("optee-ta")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) device_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) void optee_unregister_devices(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) bus_for_each_dev(&tee_bus_type, NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) __optee_unregister_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }