Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, Linaro Limited |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
| 17 | #include <linux/arm-smccc.h> |
| 18 | #include <linux/errno.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/tee_drv.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include "optee_private.h" |
| 30 | #include "optee_smc.h" |
| 31 | |
| 32 | #define DRIVER_NAME "optee" |
| 33 | |
| 34 | #define OPTEE_SHM_NUM_PRIV_PAGES 1 |
| 35 | |
| 36 | /** |
| 37 | * optee_from_msg_param() - convert from OPTEE_MSG parameters to |
| 38 | * struct tee_param |
| 39 | * @params: subsystem internal parameter representation |
| 40 | * @num_params: number of elements in the parameter arrays |
| 41 | * @msg_params: OPTEE_MSG parameters |
| 42 | * Returns 0 on success or <0 on failure |
| 43 | */ |
| 44 | int optee_from_msg_param(struct tee_param *params, size_t num_params, |
| 45 | const struct optee_msg_param *msg_params) |
| 46 | { |
| 47 | int rc; |
| 48 | size_t n; |
| 49 | struct tee_shm *shm; |
| 50 | phys_addr_t pa; |
| 51 | |
| 52 | for (n = 0; n < num_params; n++) { |
| 53 | struct tee_param *p = params + n; |
| 54 | const struct optee_msg_param *mp = msg_params + n; |
| 55 | u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK; |
| 56 | |
| 57 | switch (attr) { |
| 58 | case OPTEE_MSG_ATTR_TYPE_NONE: |
| 59 | p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE; |
| 60 | memset(&p->u, 0, sizeof(p->u)); |
| 61 | break; |
| 62 | case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT: |
| 63 | case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT: |
| 64 | case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT: |
| 65 | p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + |
| 66 | attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; |
| 67 | p->u.value.a = mp->u.value.a; |
| 68 | p->u.value.b = mp->u.value.b; |
| 69 | p->u.value.c = mp->u.value.c; |
| 70 | break; |
| 71 | case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT: |
| 72 | case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT: |
| 73 | case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT: |
| 74 | p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT + |
| 75 | attr - OPTEE_MSG_ATTR_TYPE_TMEM_INPUT; |
| 76 | p->u.memref.size = mp->u.tmem.size; |
| 77 | shm = (struct tee_shm *)(unsigned long) |
| 78 | mp->u.tmem.shm_ref; |
| 79 | if (!shm) { |
| 80 | p->u.memref.shm_offs = 0; |
| 81 | p->u.memref.shm = NULL; |
| 82 | break; |
| 83 | } |
| 84 | rc = tee_shm_get_pa(shm, 0, &pa); |
| 85 | if (rc) |
| 86 | return rc; |
| 87 | p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa; |
| 88 | p->u.memref.shm = shm; |
| 89 | |
| 90 | /* Check that the memref is covered by the shm object */ |
| 91 | if (p->u.memref.size) { |
| 92 | size_t o = p->u.memref.shm_offs + |
| 93 | p->u.memref.size - 1; |
| 94 | |
| 95 | rc = tee_shm_get_pa(shm, o, NULL); |
| 96 | if (rc) |
| 97 | return rc; |
| 98 | } |
| 99 | break; |
| 100 | default: |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters |
| 109 | * @msg_params: OPTEE_MSG parameters |
| 110 | * @num_params: number of elements in the parameter arrays |
| 111 | * @params: subsystem itnernal parameter representation |
| 112 | * Returns 0 on success or <0 on failure |
| 113 | */ |
| 114 | int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params, |
| 115 | const struct tee_param *params) |
| 116 | { |
| 117 | int rc; |
| 118 | size_t n; |
| 119 | phys_addr_t pa; |
| 120 | |
| 121 | for (n = 0; n < num_params; n++) { |
| 122 | const struct tee_param *p = params + n; |
| 123 | struct optee_msg_param *mp = msg_params + n; |
| 124 | |
| 125 | switch (p->attr) { |
| 126 | case TEE_IOCTL_PARAM_ATTR_TYPE_NONE: |
| 127 | mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE; |
| 128 | memset(&mp->u, 0, sizeof(mp->u)); |
| 129 | break; |
| 130 | case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT: |
| 131 | case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT: |
| 132 | case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT: |
| 133 | mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - |
| 134 | TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 135 | mp->u.value.a = p->u.value.a; |
| 136 | mp->u.value.b = p->u.value.b; |
| 137 | mp->u.value.c = p->u.value.c; |
| 138 | break; |
| 139 | case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT: |
| 140 | case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT: |
| 141 | case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT: |
| 142 | mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + |
| 143 | p->attr - |
| 144 | TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT; |
| 145 | mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm; |
| 146 | mp->u.tmem.size = p->u.memref.size; |
| 147 | if (!p->u.memref.shm) { |
| 148 | mp->u.tmem.buf_ptr = 0; |
| 149 | break; |
| 150 | } |
| 151 | rc = tee_shm_get_pa(p->u.memref.shm, |
| 152 | p->u.memref.shm_offs, &pa); |
| 153 | if (rc) |
| 154 | return rc; |
| 155 | mp->u.tmem.buf_ptr = pa; |
| 156 | mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED << |
| 157 | OPTEE_MSG_ATTR_CACHE_SHIFT; |
| 158 | break; |
| 159 | default: |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static void optee_get_version(struct tee_device *teedev, |
| 167 | struct tee_ioctl_version_data *vers) |
| 168 | { |
| 169 | struct tee_ioctl_version_data v = { |
| 170 | .impl_id = TEE_IMPL_ID_OPTEE, |
| 171 | .impl_caps = TEE_OPTEE_CAP_TZ, |
| 172 | .gen_caps = TEE_GEN_CAP_GP, |
| 173 | }; |
| 174 | *vers = v; |
| 175 | } |
| 176 | |
| 177 | static int optee_open(struct tee_context *ctx) |
| 178 | { |
| 179 | struct optee_context_data *ctxdata; |
| 180 | struct tee_device *teedev = ctx->teedev; |
| 181 | struct optee *optee = tee_get_drvdata(teedev); |
| 182 | |
| 183 | ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL); |
| 184 | if (!ctxdata) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | if (teedev == optee->supp_teedev) { |
| 188 | bool busy = true; |
| 189 | |
Jens Wiklander | ad675fa | 2016-12-23 13:13:39 +0100 | [diff] [blame^] | 190 | mutex_lock(&optee->supp.mutex); |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 191 | if (!optee->supp.ctx) { |
| 192 | busy = false; |
| 193 | optee->supp.ctx = ctx; |
| 194 | } |
Jens Wiklander | ad675fa | 2016-12-23 13:13:39 +0100 | [diff] [blame^] | 195 | mutex_unlock(&optee->supp.mutex); |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 196 | if (busy) { |
| 197 | kfree(ctxdata); |
| 198 | return -EBUSY; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | mutex_init(&ctxdata->mutex); |
| 203 | INIT_LIST_HEAD(&ctxdata->sess_list); |
| 204 | |
| 205 | ctx->data = ctxdata; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void optee_release(struct tee_context *ctx) |
| 210 | { |
| 211 | struct optee_context_data *ctxdata = ctx->data; |
| 212 | struct tee_device *teedev = ctx->teedev; |
| 213 | struct optee *optee = tee_get_drvdata(teedev); |
| 214 | struct tee_shm *shm; |
| 215 | struct optee_msg_arg *arg = NULL; |
| 216 | phys_addr_t parg; |
| 217 | struct optee_session *sess; |
| 218 | struct optee_session *sess_tmp; |
| 219 | |
| 220 | if (!ctxdata) |
| 221 | return; |
| 222 | |
| 223 | shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED); |
| 224 | if (!IS_ERR(shm)) { |
| 225 | arg = tee_shm_get_va(shm, 0); |
| 226 | /* |
Jens Wiklander | 14edc41 | 2017-05-15 11:09:28 +0200 | [diff] [blame] | 227 | * If va2pa fails for some reason, we can't call into |
| 228 | * secure world, only free the memory. Secure OS will leak |
| 229 | * sessions and finally refuse more sessions, but we will |
| 230 | * at least let normal world reclaim its memory. |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 231 | */ |
| 232 | if (!IS_ERR(arg)) |
Jens Wiklander | 14edc41 | 2017-05-15 11:09:28 +0200 | [diff] [blame] | 233 | if (tee_shm_va2pa(shm, arg, &parg)) |
| 234 | arg = NULL; /* prevent usage of parg below */ |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list, |
| 238 | list_node) { |
| 239 | list_del(&sess->list_node); |
| 240 | if (!IS_ERR_OR_NULL(arg)) { |
| 241 | memset(arg, 0, sizeof(*arg)); |
| 242 | arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION; |
| 243 | arg->session = sess->session_id; |
| 244 | optee_do_call_with_arg(ctx, parg); |
| 245 | } |
| 246 | kfree(sess); |
| 247 | } |
| 248 | kfree(ctxdata); |
| 249 | |
| 250 | if (!IS_ERR(shm)) |
| 251 | tee_shm_free(shm); |
| 252 | |
| 253 | ctx->data = NULL; |
| 254 | |
Jens Wiklander | ad675fa | 2016-12-23 13:13:39 +0100 | [diff] [blame^] | 255 | if (teedev == optee->supp_teedev) |
| 256 | optee_supp_release(&optee->supp); |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Bhumika Goyal | 7b22a54 | 2017-06-29 15:05:04 +0530 | [diff] [blame] | 259 | static const struct tee_driver_ops optee_ops = { |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 260 | .get_version = optee_get_version, |
| 261 | .open = optee_open, |
| 262 | .release = optee_release, |
| 263 | .open_session = optee_open_session, |
| 264 | .close_session = optee_close_session, |
| 265 | .invoke_func = optee_invoke_func, |
| 266 | .cancel_req = optee_cancel_req, |
| 267 | }; |
| 268 | |
Bhumika Goyal | 7b22a54 | 2017-06-29 15:05:04 +0530 | [diff] [blame] | 269 | static const struct tee_desc optee_desc = { |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 270 | .name = DRIVER_NAME "-clnt", |
| 271 | .ops = &optee_ops, |
| 272 | .owner = THIS_MODULE, |
| 273 | }; |
| 274 | |
Bhumika Goyal | 7b22a54 | 2017-06-29 15:05:04 +0530 | [diff] [blame] | 275 | static const struct tee_driver_ops optee_supp_ops = { |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 276 | .get_version = optee_get_version, |
| 277 | .open = optee_open, |
| 278 | .release = optee_release, |
| 279 | .supp_recv = optee_supp_recv, |
| 280 | .supp_send = optee_supp_send, |
| 281 | }; |
| 282 | |
Bhumika Goyal | 7b22a54 | 2017-06-29 15:05:04 +0530 | [diff] [blame] | 283 | static const struct tee_desc optee_supp_desc = { |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 284 | .name = DRIVER_NAME "-supp", |
| 285 | .ops = &optee_supp_ops, |
| 286 | .owner = THIS_MODULE, |
| 287 | .flags = TEE_DESC_PRIVILEGED, |
| 288 | }; |
| 289 | |
| 290 | static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn) |
| 291 | { |
| 292 | struct arm_smccc_res res; |
| 293 | |
| 294 | invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res); |
| 295 | |
| 296 | if (res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 && |
| 297 | res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3) |
| 298 | return true; |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn) |
| 303 | { |
| 304 | union { |
| 305 | struct arm_smccc_res smccc; |
| 306 | struct optee_smc_calls_revision_result result; |
| 307 | } res; |
| 308 | |
| 309 | invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc); |
| 310 | |
| 311 | if (res.result.major == OPTEE_MSG_REVISION_MAJOR && |
| 312 | (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR) |
| 313 | return true; |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn, |
| 318 | u32 *sec_caps) |
| 319 | { |
| 320 | union { |
| 321 | struct arm_smccc_res smccc; |
| 322 | struct optee_smc_exchange_capabilities_result result; |
| 323 | } res; |
| 324 | u32 a1 = 0; |
| 325 | |
| 326 | /* |
| 327 | * TODO This isn't enough to tell if it's UP system (from kernel |
| 328 | * point of view) or not, is_smp() returns the the information |
| 329 | * needed, but can't be called directly from here. |
| 330 | */ |
| 331 | if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1) |
| 332 | a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR; |
| 333 | |
| 334 | invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0, |
| 335 | &res.smccc); |
| 336 | |
| 337 | if (res.result.status != OPTEE_SMC_RETURN_OK) |
| 338 | return false; |
| 339 | |
| 340 | *sec_caps = res.result.capabilities; |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | static struct tee_shm_pool * |
| 345 | optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm) |
| 346 | { |
| 347 | union { |
| 348 | struct arm_smccc_res smccc; |
| 349 | struct optee_smc_get_shm_config_result result; |
| 350 | } res; |
| 351 | struct tee_shm_pool *pool; |
| 352 | unsigned long vaddr; |
| 353 | phys_addr_t paddr; |
| 354 | size_t size; |
| 355 | phys_addr_t begin; |
| 356 | phys_addr_t end; |
| 357 | void *va; |
| 358 | struct tee_shm_pool_mem_info priv_info; |
| 359 | struct tee_shm_pool_mem_info dmabuf_info; |
| 360 | |
| 361 | invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc); |
| 362 | if (res.result.status != OPTEE_SMC_RETURN_OK) { |
| 363 | pr_info("shm service not available\n"); |
| 364 | return ERR_PTR(-ENOENT); |
| 365 | } |
| 366 | |
| 367 | if (res.result.settings != OPTEE_SMC_SHM_CACHED) { |
| 368 | pr_err("only normal cached shared memory supported\n"); |
| 369 | return ERR_PTR(-EINVAL); |
| 370 | } |
| 371 | |
| 372 | begin = roundup(res.result.start, PAGE_SIZE); |
| 373 | end = rounddown(res.result.start + res.result.size, PAGE_SIZE); |
| 374 | paddr = begin; |
| 375 | size = end - begin; |
| 376 | |
| 377 | if (size < 2 * OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE) { |
| 378 | pr_err("too small shared memory area\n"); |
| 379 | return ERR_PTR(-EINVAL); |
| 380 | } |
| 381 | |
| 382 | va = memremap(paddr, size, MEMREMAP_WB); |
| 383 | if (!va) { |
| 384 | pr_err("shared memory ioremap failed\n"); |
| 385 | return ERR_PTR(-EINVAL); |
| 386 | } |
| 387 | vaddr = (unsigned long)va; |
| 388 | |
| 389 | priv_info.vaddr = vaddr; |
| 390 | priv_info.paddr = paddr; |
| 391 | priv_info.size = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE; |
| 392 | dmabuf_info.vaddr = vaddr + OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE; |
| 393 | dmabuf_info.paddr = paddr + OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE; |
| 394 | dmabuf_info.size = size - OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE; |
| 395 | |
| 396 | pool = tee_shm_pool_alloc_res_mem(&priv_info, &dmabuf_info); |
| 397 | if (IS_ERR(pool)) { |
| 398 | memunmap(va); |
| 399 | goto out; |
| 400 | } |
| 401 | |
| 402 | *memremaped_shm = va; |
| 403 | out: |
| 404 | return pool; |
| 405 | } |
| 406 | |
| 407 | /* Simple wrapper functions to be able to use a function pointer */ |
| 408 | static void optee_smccc_smc(unsigned long a0, unsigned long a1, |
| 409 | unsigned long a2, unsigned long a3, |
| 410 | unsigned long a4, unsigned long a5, |
| 411 | unsigned long a6, unsigned long a7, |
| 412 | struct arm_smccc_res *res) |
| 413 | { |
| 414 | arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res); |
| 415 | } |
| 416 | |
| 417 | static void optee_smccc_hvc(unsigned long a0, unsigned long a1, |
| 418 | unsigned long a2, unsigned long a3, |
| 419 | unsigned long a4, unsigned long a5, |
| 420 | unsigned long a6, unsigned long a7, |
| 421 | struct arm_smccc_res *res) |
| 422 | { |
| 423 | arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res); |
| 424 | } |
| 425 | |
| 426 | static optee_invoke_fn *get_invoke_func(struct device_node *np) |
| 427 | { |
| 428 | const char *method; |
| 429 | |
| 430 | pr_info("probing for conduit method from DT.\n"); |
| 431 | |
| 432 | if (of_property_read_string(np, "method", &method)) { |
| 433 | pr_warn("missing \"method\" property\n"); |
| 434 | return ERR_PTR(-ENXIO); |
| 435 | } |
| 436 | |
| 437 | if (!strcmp("hvc", method)) |
| 438 | return optee_smccc_hvc; |
| 439 | else if (!strcmp("smc", method)) |
| 440 | return optee_smccc_smc; |
| 441 | |
| 442 | pr_warn("invalid \"method\" property: %s\n", method); |
| 443 | return ERR_PTR(-EINVAL); |
| 444 | } |
| 445 | |
| 446 | static struct optee *optee_probe(struct device_node *np) |
| 447 | { |
| 448 | optee_invoke_fn *invoke_fn; |
| 449 | struct tee_shm_pool *pool; |
| 450 | struct optee *optee = NULL; |
| 451 | void *memremaped_shm = NULL; |
| 452 | struct tee_device *teedev; |
| 453 | u32 sec_caps; |
| 454 | int rc; |
| 455 | |
| 456 | invoke_fn = get_invoke_func(np); |
| 457 | if (IS_ERR(invoke_fn)) |
| 458 | return (void *)invoke_fn; |
| 459 | |
| 460 | if (!optee_msg_api_uid_is_optee_api(invoke_fn)) { |
| 461 | pr_warn("api uid mismatch\n"); |
| 462 | return ERR_PTR(-EINVAL); |
| 463 | } |
| 464 | |
| 465 | if (!optee_msg_api_revision_is_compatible(invoke_fn)) { |
| 466 | pr_warn("api revision mismatch\n"); |
| 467 | return ERR_PTR(-EINVAL); |
| 468 | } |
| 469 | |
| 470 | if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) { |
| 471 | pr_warn("capabilities mismatch\n"); |
| 472 | return ERR_PTR(-EINVAL); |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * We have no other option for shared memory, if secure world |
| 477 | * doesn't have any reserved memory we can use we can't continue. |
| 478 | */ |
| 479 | if (!(sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM)) |
| 480 | return ERR_PTR(-EINVAL); |
| 481 | |
| 482 | pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm); |
| 483 | if (IS_ERR(pool)) |
| 484 | return (void *)pool; |
| 485 | |
| 486 | optee = kzalloc(sizeof(*optee), GFP_KERNEL); |
| 487 | if (!optee) { |
| 488 | rc = -ENOMEM; |
| 489 | goto err; |
| 490 | } |
| 491 | |
| 492 | optee->invoke_fn = invoke_fn; |
| 493 | |
| 494 | teedev = tee_device_alloc(&optee_desc, NULL, pool, optee); |
| 495 | if (IS_ERR(teedev)) { |
| 496 | rc = PTR_ERR(teedev); |
| 497 | goto err; |
| 498 | } |
| 499 | optee->teedev = teedev; |
| 500 | |
| 501 | teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee); |
| 502 | if (IS_ERR(teedev)) { |
| 503 | rc = PTR_ERR(teedev); |
| 504 | goto err; |
| 505 | } |
| 506 | optee->supp_teedev = teedev; |
| 507 | |
| 508 | rc = tee_device_register(optee->teedev); |
| 509 | if (rc) |
| 510 | goto err; |
| 511 | |
| 512 | rc = tee_device_register(optee->supp_teedev); |
| 513 | if (rc) |
| 514 | goto err; |
| 515 | |
| 516 | mutex_init(&optee->call_queue.mutex); |
| 517 | INIT_LIST_HEAD(&optee->call_queue.waiters); |
| 518 | optee_wait_queue_init(&optee->wait_queue); |
| 519 | optee_supp_init(&optee->supp); |
| 520 | optee->memremaped_shm = memremaped_shm; |
| 521 | optee->pool = pool; |
| 522 | |
| 523 | optee_enable_shm_cache(optee); |
| 524 | |
| 525 | pr_info("initialized driver\n"); |
| 526 | return optee; |
| 527 | err: |
| 528 | if (optee) { |
| 529 | /* |
| 530 | * tee_device_unregister() is safe to call even if the |
| 531 | * devices hasn't been registered with |
| 532 | * tee_device_register() yet. |
| 533 | */ |
| 534 | tee_device_unregister(optee->supp_teedev); |
| 535 | tee_device_unregister(optee->teedev); |
| 536 | kfree(optee); |
| 537 | } |
| 538 | if (pool) |
| 539 | tee_shm_pool_free(pool); |
| 540 | if (memremaped_shm) |
| 541 | memunmap(memremaped_shm); |
| 542 | return ERR_PTR(rc); |
| 543 | } |
| 544 | |
| 545 | static void optee_remove(struct optee *optee) |
| 546 | { |
| 547 | /* |
| 548 | * Ask OP-TEE to free all cached shared memory objects to decrease |
| 549 | * reference counters and also avoid wild pointers in secure world |
| 550 | * into the old shared memory range. |
| 551 | */ |
| 552 | optee_disable_shm_cache(optee); |
| 553 | |
| 554 | /* |
| 555 | * The two devices has to be unregistered before we can free the |
| 556 | * other resources. |
| 557 | */ |
| 558 | tee_device_unregister(optee->supp_teedev); |
| 559 | tee_device_unregister(optee->teedev); |
| 560 | |
| 561 | tee_shm_pool_free(optee->pool); |
| 562 | if (optee->memremaped_shm) |
| 563 | memunmap(optee->memremaped_shm); |
| 564 | optee_wait_queue_exit(&optee->wait_queue); |
| 565 | optee_supp_uninit(&optee->supp); |
| 566 | mutex_destroy(&optee->call_queue.mutex); |
| 567 | |
| 568 | kfree(optee); |
| 569 | } |
| 570 | |
| 571 | static const struct of_device_id optee_match[] = { |
| 572 | { .compatible = "linaro,optee-tz" }, |
| 573 | {}, |
| 574 | }; |
| 575 | |
| 576 | static struct optee *optee_svc; |
| 577 | |
| 578 | static int __init optee_driver_init(void) |
| 579 | { |
| 580 | struct device_node *fw_np; |
| 581 | struct device_node *np; |
| 582 | struct optee *optee; |
| 583 | |
| 584 | /* Node is supposed to be below /firmware */ |
| 585 | fw_np = of_find_node_by_name(NULL, "firmware"); |
| 586 | if (!fw_np) |
| 587 | return -ENODEV; |
| 588 | |
| 589 | np = of_find_matching_node(fw_np, optee_match); |
Jens Wiklander | 18ebb2f | 2015-04-14 14:33:20 +0200 | [diff] [blame] | 590 | if (!np) |
| 591 | return -ENODEV; |
| 592 | |
| 593 | optee = optee_probe(np); |
| 594 | of_node_put(np); |
| 595 | |
| 596 | if (IS_ERR(optee)) |
| 597 | return PTR_ERR(optee); |
| 598 | |
| 599 | optee_svc = optee; |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | module_init(optee_driver_init); |
| 604 | |
| 605 | static void __exit optee_driver_exit(void) |
| 606 | { |
| 607 | struct optee *optee = optee_svc; |
| 608 | |
| 609 | optee_svc = NULL; |
| 610 | if (optee) |
| 611 | optee_remove(optee); |
| 612 | } |
| 613 | module_exit(optee_driver_exit); |
| 614 | |
| 615 | MODULE_AUTHOR("Linaro"); |
| 616 | MODULE_DESCRIPTION("OP-TEE driver"); |
| 617 | MODULE_SUPPORTED_DEVICE(""); |
| 618 | MODULE_VERSION("1.0"); |
| 619 | MODULE_LICENSE("GPL v2"); |