core.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. static void optee_bus_scan(struct work_struct *work)
  48. {
  49. WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
  50. }
  51. static ssize_t rpmb_routing_model_show(struct device *dev,
  52. struct device_attribute *attr, char *buf)
  53. {
  54. struct optee *optee = dev_get_drvdata(dev);
  55. const char *s;
  56. if (optee->in_kernel_rpmb_routing)
  57. s = "kernel";
  58. else
  59. s = "user";
  60. return scnprintf(buf, PAGE_SIZE, "%s\n", s);
  61. }
  62. static DEVICE_ATTR_RO(rpmb_routing_model);
  63. static struct attribute *optee_dev_attrs[] = {
  64. &dev_attr_rpmb_routing_model.attr,
  65. NULL
  66. };
  67. ATTRIBUTE_GROUPS(optee_dev);
  68. void optee_set_dev_group(struct optee *optee)
  69. {
  70. tee_device_set_dev_groups(optee->teedev, optee_dev_groups);
  71. tee_device_set_dev_groups(optee->supp_teedev, optee_dev_groups);
  72. }
  73. int optee_open(struct tee_context *ctx, bool cap_memref_null)
  74. {
  75. struct optee_context_data *ctxdata;
  76. struct tee_device *teedev = ctx->teedev;
  77. struct optee *optee = tee_get_drvdata(teedev);
  78. ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
  79. if (!ctxdata)
  80. return -ENOMEM;
  81. if (teedev == optee->supp_teedev) {
  82. bool busy = true;
  83. mutex_lock(&optee->supp.mutex);
  84. if (!optee->supp.ctx) {
  85. busy = false;
  86. optee->supp.ctx = ctx;
  87. }
  88. mutex_unlock(&optee->supp.mutex);
  89. if (busy) {
  90. kfree(ctxdata);
  91. return -EBUSY;
  92. }
  93. if (!optee->scan_bus_done) {
  94. INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
  95. schedule_work(&optee->scan_bus_work);
  96. optee->scan_bus_done = true;
  97. }
  98. }
  99. mutex_init(&ctxdata->mutex);
  100. INIT_LIST_HEAD(&ctxdata->sess_list);
  101. ctx->cap_memref_null = cap_memref_null;
  102. ctx->data = ctxdata;
  103. return 0;
  104. }
  105. static void optee_release_helper(struct tee_context *ctx,
  106. int (*close_session)(struct tee_context *ctx,
  107. u32 session,
  108. bool system_thread))
  109. {
  110. struct optee_context_data *ctxdata = ctx->data;
  111. struct optee_session *sess;
  112. struct optee_session *sess_tmp;
  113. if (!ctxdata)
  114. return;
  115. list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
  116. list_node) {
  117. list_del(&sess->list_node);
  118. close_session(ctx, sess->session_id, sess->use_sys_thread);
  119. kfree(sess);
  120. }
  121. kfree(ctxdata);
  122. ctx->data = NULL;
  123. }
  124. void optee_release(struct tee_context *ctx)
  125. {
  126. optee_release_helper(ctx, optee_close_session_helper);
  127. }
  128. void optee_release_supp(struct tee_context *ctx)
  129. {
  130. struct optee *optee = tee_get_drvdata(ctx->teedev);
  131. optee_release_helper(ctx, optee_close_session_helper);
  132. optee_supp_release(&optee->supp);
  133. }
  134. void optee_remove_common(struct optee *optee)
  135. {
  136. blocking_notifier_chain_unregister(&optee_rpmb_intf_added,
  137. &optee->rpmb_intf);
  138. cancel_work_sync(&optee->rpmb_scan_bus_work);
  139. /* Unregister OP-TEE specific client devices on TEE bus */
  140. optee_unregister_devices();
  141. optee_notif_uninit(optee);
  142. optee_shm_arg_cache_uninit(optee);
  143. teedev_close_context(optee->ctx);
  144. /*
  145. * The two devices have to be unregistered before we can free the
  146. * other resources.
  147. */
  148. tee_device_unregister(optee->supp_teedev);
  149. tee_device_unregister(optee->teedev);
  150. tee_shm_pool_free(optee->pool);
  151. optee_supp_uninit(&optee->supp);
  152. mutex_destroy(&optee->call_queue.mutex);
  153. rpmb_dev_put(optee->rpmb_dev);
  154. mutex_destroy(&optee->rpmb_dev_mutex);
  155. }
  156. static int smc_abi_rc;
  157. static int ffa_abi_rc;
  158. static bool intf_is_regged;
  159. static int __init optee_core_init(void)
  160. {
  161. int rc;
  162. /*
  163. * The kernel may have crashed at the same time that all available
  164. * secure world threads were suspended and we cannot reschedule the
  165. * suspended threads without access to the crashed kernel's wait_queue.
  166. * Therefore, we cannot reliably initialize the OP-TEE driver in the
  167. * kdump kernel.
  168. */
  169. if (is_kdump_kernel())
  170. return -ENODEV;
  171. if (IS_REACHABLE(CONFIG_RPMB)) {
  172. rc = rpmb_interface_register(&rpmb_class_intf);
  173. if (rc)
  174. return rc;
  175. intf_is_regged = true;
  176. }
  177. smc_abi_rc = optee_smc_abi_register();
  178. ffa_abi_rc = optee_ffa_abi_register();
  179. /* If both failed there's no point with this module */
  180. if (smc_abi_rc && ffa_abi_rc) {
  181. if (IS_REACHABLE(CONFIG_RPMB)) {
  182. rpmb_interface_unregister(&rpmb_class_intf);
  183. intf_is_regged = false;
  184. }
  185. return smc_abi_rc;
  186. }
  187. return 0;
  188. }
  189. module_init(optee_core_init);
  190. static void __exit optee_core_exit(void)
  191. {
  192. if (IS_REACHABLE(CONFIG_RPMB) && intf_is_regged) {
  193. rpmb_interface_unregister(&rpmb_class_intf);
  194. intf_is_regged = false;
  195. }
  196. if (!smc_abi_rc)
  197. optee_smc_abi_unregister();
  198. if (!ffa_abi_rc)
  199. optee_ffa_abi_unregister();
  200. }
  201. module_exit(optee_core_exit);
  202. MODULE_AUTHOR("Linaro");
  203. MODULE_DESCRIPTION("OP-TEE driver");
  204. MODULE_VERSION("1.0");
  205. MODULE_LICENSE("GPL v2");
  206. MODULE_ALIAS("platform:optee");