| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Copyright (c) 2015-2021, Linaro Limited |
| 4 | * Copyright (c) 2016, EPAM Systems |
| 5 | */ |
| 6 | |
| 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 8 | |
| 9 | #include <linux/crash_dump.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/rpmb.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/tee_core.h> |
| 17 | #include <linux/types.h> |
| 18 | #include "optee_private.h" |
| 19 | |
| 20 | struct blocking_notifier_head optee_rpmb_intf_added = |
| 21 | BLOCKING_NOTIFIER_INIT(optee_rpmb_intf_added); |
| 22 | |
| 23 | static int rpmb_add_dev(struct device *dev) |
| 24 | { |
| 25 | blocking_notifier_call_chain(nh: &optee_rpmb_intf_added, val: 0, |
| 26 | to_rpmb_dev(dev)); |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static struct class_interface rpmb_class_intf = { |
| 32 | .add_dev = rpmb_add_dev, |
| 33 | }; |
| 34 | |
| 35 | void optee_bus_scan_rpmb(struct work_struct *work) |
| 36 | { |
| 37 | struct optee *optee = container_of(work, struct optee, |
| 38 | rpmb_scan_bus_work); |
| 39 | int ret; |
| 40 | |
| 41 | if (!optee->rpmb_scan_bus_done) { |
| 42 | ret = optee_enumerate_devices(PTA_CMD_GET_DEVICES_RPMB); |
| 43 | optee->rpmb_scan_bus_done = !ret; |
| 44 | if (ret && ret != -ENODEV) |
| 45 | pr_info("Scanning for RPMB device: ret %d\n" , ret); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int optee_rpmb_intf_rdev(struct notifier_block *intf, unsigned long action, |
| 50 | void *data) |
| 51 | { |
| 52 | struct optee *optee = container_of(intf, struct optee, rpmb_intf); |
| 53 | |
| 54 | schedule_work(work: &optee->rpmb_scan_bus_work); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int optee_set_dma_mask(struct optee *optee, u_int pa_width) |
| 60 | { |
| 61 | u64 mask = DMA_BIT_MASK(min(64, pa_width)); |
| 62 | |
| 63 | return dma_coerce_mask_and_coherent(dev: &optee->teedev->dev, mask); |
| 64 | } |
| 65 | |
| 66 | static void optee_bus_scan(struct work_struct *work) |
| 67 | { |
| 68 | WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP)); |
| 69 | } |
| 70 | |
| 71 | static ssize_t rpmb_routing_model_show(struct device *dev, |
| 72 | struct device_attribute *attr, char *buf) |
| 73 | { |
| 74 | struct optee *optee = dev_get_drvdata(dev); |
| 75 | const char *s; |
| 76 | |
| 77 | if (optee->in_kernel_rpmb_routing) |
| 78 | s = "kernel" ; |
| 79 | else |
| 80 | s = "user" ; |
| 81 | |
| 82 | return sysfs_emit(buf, fmt: "%s\n" , s); |
| 83 | } |
| 84 | static DEVICE_ATTR_RO(rpmb_routing_model); |
| 85 | |
| 86 | static struct attribute *optee_dev_attrs[] = { |
| 87 | &dev_attr_rpmb_routing_model.attr, |
| 88 | NULL |
| 89 | }; |
| 90 | |
| 91 | ATTRIBUTE_GROUPS(optee_dev); |
| 92 | |
| 93 | void optee_set_dev_group(struct optee *optee) |
| 94 | { |
| 95 | tee_device_set_dev_groups(teedev: optee->teedev, dev_groups: optee_dev_groups); |
| 96 | tee_device_set_dev_groups(teedev: optee->supp_teedev, dev_groups: optee_dev_groups); |
| 97 | } |
| 98 | |
| 99 | int optee_open(struct tee_context *ctx, bool cap_memref_null) |
| 100 | { |
| 101 | struct optee_context_data *ctxdata; |
| 102 | struct tee_device *teedev = ctx->teedev; |
| 103 | struct optee *optee = tee_get_drvdata(teedev); |
| 104 | |
| 105 | ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL); |
| 106 | if (!ctxdata) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | if (teedev == optee->supp_teedev) { |
| 110 | bool busy = true; |
| 111 | |
| 112 | mutex_lock(&optee->supp.mutex); |
| 113 | if (!optee->supp.ctx) { |
| 114 | busy = false; |
| 115 | optee->supp.ctx = ctx; |
| 116 | } |
| 117 | mutex_unlock(lock: &optee->supp.mutex); |
| 118 | if (busy) { |
| 119 | kfree(objp: ctxdata); |
| 120 | return -EBUSY; |
| 121 | } |
| 122 | |
| 123 | if (!optee->scan_bus_done) { |
| 124 | INIT_WORK(&optee->scan_bus_work, optee_bus_scan); |
| 125 | schedule_work(work: &optee->scan_bus_work); |
| 126 | optee->scan_bus_done = true; |
| 127 | } |
| 128 | } |
| 129 | mutex_init(&ctxdata->mutex); |
| 130 | INIT_LIST_HEAD(list: &ctxdata->sess_list); |
| 131 | |
| 132 | ctx->cap_memref_null = cap_memref_null; |
| 133 | ctx->data = ctxdata; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static void optee_release_helper(struct tee_context *ctx, |
| 138 | int (*close_session)(struct tee_context *ctx, |
| 139 | u32 session, |
| 140 | bool system_thread)) |
| 141 | { |
| 142 | struct optee_context_data *ctxdata = ctx->data; |
| 143 | struct optee_session *sess; |
| 144 | struct optee_session *sess_tmp; |
| 145 | |
| 146 | if (!ctxdata) |
| 147 | return; |
| 148 | |
| 149 | list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list, |
| 150 | list_node) { |
| 151 | list_del(entry: &sess->list_node); |
| 152 | close_session(ctx, sess->session_id, sess->use_sys_thread); |
| 153 | kfree(objp: sess); |
| 154 | } |
| 155 | kfree(objp: ctxdata); |
| 156 | ctx->data = NULL; |
| 157 | } |
| 158 | |
| 159 | void optee_release(struct tee_context *ctx) |
| 160 | { |
| 161 | optee_release_helper(ctx, close_session: optee_close_session_helper); |
| 162 | } |
| 163 | |
| 164 | void optee_release_supp(struct tee_context *ctx) |
| 165 | { |
| 166 | struct optee *optee = tee_get_drvdata(teedev: ctx->teedev); |
| 167 | |
| 168 | optee_release_helper(ctx, close_session: optee_close_session_helper); |
| 169 | |
| 170 | optee_supp_release(supp: &optee->supp); |
| 171 | } |
| 172 | |
| 173 | void optee_remove_common(struct optee *optee) |
| 174 | { |
| 175 | blocking_notifier_chain_unregister(nh: &optee_rpmb_intf_added, |
| 176 | nb: &optee->rpmb_intf); |
| 177 | cancel_work_sync(work: &optee->rpmb_scan_bus_work); |
| 178 | /* Unregister OP-TEE specific client devices on TEE bus */ |
| 179 | optee_unregister_devices(); |
| 180 | |
| 181 | optee_notif_uninit(optee); |
| 182 | optee_shm_arg_cache_uninit(optee); |
| 183 | teedev_close_context(ctx: optee->ctx); |
| 184 | /* |
| 185 | * The two devices have to be unregistered before we can free the |
| 186 | * other resources. |
| 187 | */ |
| 188 | tee_device_unregister(teedev: optee->supp_teedev); |
| 189 | tee_device_unregister(teedev: optee->teedev); |
| 190 | |
| 191 | tee_shm_pool_free(pool: optee->pool); |
| 192 | optee_supp_uninit(supp: &optee->supp); |
| 193 | mutex_destroy(lock: &optee->call_queue.mutex); |
| 194 | rpmb_dev_put(rdev: optee->rpmb_dev); |
| 195 | mutex_destroy(lock: &optee->rpmb_dev_mutex); |
| 196 | } |
| 197 | |
| 198 | static int smc_abi_rc; |
| 199 | static int ffa_abi_rc; |
| 200 | static bool intf_is_regged; |
| 201 | |
| 202 | static int __init optee_core_init(void) |
| 203 | { |
| 204 | int rc; |
| 205 | |
| 206 | /* |
| 207 | * The kernel may have crashed at the same time that all available |
| 208 | * secure world threads were suspended and we cannot reschedule the |
| 209 | * suspended threads without access to the crashed kernel's wait_queue. |
| 210 | * Therefore, we cannot reliably initialize the OP-TEE driver in the |
| 211 | * kdump kernel. |
| 212 | */ |
| 213 | if (is_kdump_kernel()) |
| 214 | return -ENODEV; |
| 215 | |
| 216 | if (IS_REACHABLE(CONFIG_RPMB)) { |
| 217 | rc = rpmb_interface_register(intf: &rpmb_class_intf); |
| 218 | if (rc) |
| 219 | return rc; |
| 220 | intf_is_regged = true; |
| 221 | } |
| 222 | |
| 223 | smc_abi_rc = optee_smc_abi_register(); |
| 224 | ffa_abi_rc = optee_ffa_abi_register(); |
| 225 | |
| 226 | /* If both failed there's no point with this module */ |
| 227 | if (smc_abi_rc && ffa_abi_rc) { |
| 228 | if (IS_REACHABLE(CONFIG_RPMB)) { |
| 229 | rpmb_interface_unregister(intf: &rpmb_class_intf); |
| 230 | intf_is_regged = false; |
| 231 | } |
| 232 | return smc_abi_rc; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | module_init(optee_core_init); |
| 238 | |
| 239 | static void __exit optee_core_exit(void) |
| 240 | { |
| 241 | if (IS_REACHABLE(CONFIG_RPMB) && intf_is_regged) { |
| 242 | rpmb_interface_unregister(intf: &rpmb_class_intf); |
| 243 | intf_is_regged = false; |
| 244 | } |
| 245 | |
| 246 | if (!smc_abi_rc) |
| 247 | optee_smc_abi_unregister(); |
| 248 | if (!ffa_abi_rc) |
| 249 | optee_ffa_abi_unregister(); |
| 250 | } |
| 251 | module_exit(optee_core_exit); |
| 252 | |
| 253 | MODULE_AUTHOR("Linaro" ); |
| 254 | MODULE_DESCRIPTION("OP-TEE driver" ); |
| 255 | MODULE_VERSION("1.0" ); |
| 256 | MODULE_LICENSE("GPL v2" ); |
| 257 | MODULE_ALIAS("platform:optee" ); |
| 258 | |