0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008
0009 #include <linux/crash_dump.h>
0010 #include <linux/errno.h>
0011 #include <linux/io.h>
0012 #include <linux/mm.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 #include <linux/string.h>
0016 #include <linux/tee_drv.h>
0017 #include <linux/types.h>
0018 #include <linux/workqueue.h>
0019 #include "optee_private.h"
0020
0021 int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
0022 size_t size, size_t align,
0023 int (*shm_register)(struct tee_context *ctx,
0024 struct tee_shm *shm,
0025 struct page **pages,
0026 size_t num_pages,
0027 unsigned long start))
0028 {
0029 unsigned int order = get_order(size);
0030 struct page *page;
0031 int rc = 0;
0032
0033
0034
0035
0036
0037 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
0038 if (!page)
0039 return -ENOMEM;
0040
0041 shm->kaddr = page_address(page);
0042 shm->paddr = page_to_phys(page);
0043 shm->size = PAGE_SIZE << order;
0044
0045 if (shm_register) {
0046 unsigned int nr_pages = 1 << order, i;
0047 struct page **pages;
0048
0049 pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
0050 if (!pages) {
0051 rc = -ENOMEM;
0052 goto err;
0053 }
0054
0055 for (i = 0; i < nr_pages; i++)
0056 pages[i] = page + i;
0057
0058 rc = shm_register(shm->ctx, shm, pages, nr_pages,
0059 (unsigned long)shm->kaddr);
0060 kfree(pages);
0061 if (rc)
0062 goto err;
0063 }
0064
0065 return 0;
0066
0067 err:
0068 free_pages((unsigned long)shm->kaddr, order);
0069 return rc;
0070 }
0071
0072 void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
0073 int (*shm_unregister)(struct tee_context *ctx,
0074 struct tee_shm *shm))
0075 {
0076 if (shm_unregister)
0077 shm_unregister(shm->ctx, shm);
0078 free_pages((unsigned long)shm->kaddr, get_order(shm->size));
0079 shm->kaddr = NULL;
0080 }
0081
0082 static void optee_bus_scan(struct work_struct *work)
0083 {
0084 WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
0085 }
0086
0087 int optee_open(struct tee_context *ctx, bool cap_memref_null)
0088 {
0089 struct optee_context_data *ctxdata;
0090 struct tee_device *teedev = ctx->teedev;
0091 struct optee *optee = tee_get_drvdata(teedev);
0092
0093 ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
0094 if (!ctxdata)
0095 return -ENOMEM;
0096
0097 if (teedev == optee->supp_teedev) {
0098 bool busy = true;
0099
0100 mutex_lock(&optee->supp.mutex);
0101 if (!optee->supp.ctx) {
0102 busy = false;
0103 optee->supp.ctx = ctx;
0104 }
0105 mutex_unlock(&optee->supp.mutex);
0106 if (busy) {
0107 kfree(ctxdata);
0108 return -EBUSY;
0109 }
0110
0111 if (!optee->scan_bus_done) {
0112 INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
0113 optee->scan_bus_wq = create_workqueue("optee_bus_scan");
0114 if (!optee->scan_bus_wq) {
0115 kfree(ctxdata);
0116 return -ECHILD;
0117 }
0118 queue_work(optee->scan_bus_wq, &optee->scan_bus_work);
0119 optee->scan_bus_done = true;
0120 }
0121 }
0122 mutex_init(&ctxdata->mutex);
0123 INIT_LIST_HEAD(&ctxdata->sess_list);
0124
0125 ctx->cap_memref_null = cap_memref_null;
0126 ctx->data = ctxdata;
0127 return 0;
0128 }
0129
0130 static void optee_release_helper(struct tee_context *ctx,
0131 int (*close_session)(struct tee_context *ctx,
0132 u32 session))
0133 {
0134 struct optee_context_data *ctxdata = ctx->data;
0135 struct optee_session *sess;
0136 struct optee_session *sess_tmp;
0137
0138 if (!ctxdata)
0139 return;
0140
0141 list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
0142 list_node) {
0143 list_del(&sess->list_node);
0144 close_session(ctx, sess->session_id);
0145 kfree(sess);
0146 }
0147 kfree(ctxdata);
0148 ctx->data = NULL;
0149 }
0150
0151 void optee_release(struct tee_context *ctx)
0152 {
0153 optee_release_helper(ctx, optee_close_session_helper);
0154 }
0155
0156 void optee_release_supp(struct tee_context *ctx)
0157 {
0158 struct optee *optee = tee_get_drvdata(ctx->teedev);
0159
0160 optee_release_helper(ctx, optee_close_session_helper);
0161 if (optee->scan_bus_wq) {
0162 destroy_workqueue(optee->scan_bus_wq);
0163 optee->scan_bus_wq = NULL;
0164 }
0165 optee_supp_release(&optee->supp);
0166 }
0167
0168 void optee_remove_common(struct optee *optee)
0169 {
0170
0171 optee_unregister_devices();
0172
0173 optee_notif_uninit(optee);
0174 optee_shm_arg_cache_uninit(optee);
0175 teedev_close_context(optee->ctx);
0176
0177
0178
0179
0180 tee_device_unregister(optee->supp_teedev);
0181 tee_device_unregister(optee->teedev);
0182
0183 tee_shm_pool_free(optee->pool);
0184 optee_supp_uninit(&optee->supp);
0185 mutex_destroy(&optee->call_queue.mutex);
0186 }
0187
0188 static int smc_abi_rc;
0189 static int ffa_abi_rc;
0190
0191 static int optee_core_init(void)
0192 {
0193
0194
0195
0196
0197
0198
0199
0200 if (is_kdump_kernel())
0201 return -ENODEV;
0202
0203 smc_abi_rc = optee_smc_abi_register();
0204 ffa_abi_rc = optee_ffa_abi_register();
0205
0206
0207 if (smc_abi_rc && ffa_abi_rc)
0208 return smc_abi_rc;
0209 return 0;
0210 }
0211 module_init(optee_core_init);
0212
0213 static void optee_core_exit(void)
0214 {
0215 if (!smc_abi_rc)
0216 optee_smc_abi_unregister();
0217 if (!ffa_abi_rc)
0218 optee_ffa_abi_unregister();
0219 }
0220 module_exit(optee_core_exit);
0221
0222 MODULE_AUTHOR("Linaro");
0223 MODULE_DESCRIPTION("OP-TEE driver");
0224 MODULE_VERSION("1.0");
0225 MODULE_LICENSE("GPL v2");
0226 MODULE_ALIAS("platform:optee");