core.c 6.5 KB

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