call.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/slab.h>
  7. #include <linux/tee.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/xarray.h>
  10. #include "qcomtee.h"
  11. static int find_qtee_object(struct qcomtee_object **object, unsigned long id,
  12. struct qcomtee_context_data *ctxdata)
  13. {
  14. int err = 0;
  15. guard(rcu)();
  16. /* Object release is RCU protected. */
  17. *object = idr_find(&ctxdata->qtee_objects_idr, id);
  18. if (!qcomtee_object_get(*object))
  19. err = -EINVAL;
  20. return err;
  21. }
  22. static void del_qtee_object(unsigned long id,
  23. struct qcomtee_context_data *ctxdata)
  24. {
  25. struct qcomtee_object *object;
  26. scoped_guard(mutex, &ctxdata->qtee_lock)
  27. object = idr_remove(&ctxdata->qtee_objects_idr, id);
  28. qcomtee_object_put(object);
  29. }
  30. /**
  31. * qcomtee_context_add_qtee_object() - Add a QTEE object to the context.
  32. * @param: TEE parameter representing @object.
  33. * @object: QTEE object.
  34. * @ctx: context to add the object.
  35. *
  36. * It assumes @object is %QCOMTEE_OBJECT_TYPE_TEE and the caller has already
  37. * issued qcomtee_object_get() for @object.
  38. *
  39. * Return: On success, returns 0; on failure, returns < 0.
  40. */
  41. int qcomtee_context_add_qtee_object(struct tee_param *param,
  42. struct qcomtee_object *object,
  43. struct tee_context *ctx)
  44. {
  45. int ret;
  46. struct qcomtee_context_data *ctxdata = ctx->data;
  47. scoped_guard(mutex, &ctxdata->qtee_lock)
  48. ret = idr_alloc(&ctxdata->qtee_objects_idr, object, 0, 0,
  49. GFP_KERNEL);
  50. if (ret < 0)
  51. return ret;
  52. param->u.objref.id = ret;
  53. /* QTEE Object: QCOMTEE_OBJREF_FLAG_TEE set. */
  54. param->u.objref.flags = QCOMTEE_OBJREF_FLAG_TEE;
  55. return 0;
  56. }
  57. /* Retrieve the QTEE object added with qcomtee_context_add_qtee_object(). */
  58. int qcomtee_context_find_qtee_object(struct qcomtee_object **object,
  59. struct tee_param *param,
  60. struct tee_context *ctx)
  61. {
  62. struct qcomtee_context_data *ctxdata = ctx->data;
  63. return find_qtee_object(object, param->u.objref.id, ctxdata);
  64. }
  65. /**
  66. * qcomtee_context_del_qtee_object() - Delete a QTEE object from the context.
  67. * @param: TEE parameter representing @object.
  68. * @ctx: context for deleting the object.
  69. *
  70. * The @param has been initialized by qcomtee_context_add_qtee_object().
  71. */
  72. void qcomtee_context_del_qtee_object(struct tee_param *param,
  73. struct tee_context *ctx)
  74. {
  75. struct qcomtee_context_data *ctxdata = ctx->data;
  76. /* 'qtee_objects_idr' stores QTEE objects only. */
  77. if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_TEE)
  78. del_qtee_object(param->u.objref.id, ctxdata);
  79. }
  80. /**
  81. * qcomtee_objref_to_arg() - Convert OBJREF parameter to QTEE argument.
  82. * @arg: QTEE argument.
  83. * @param: TEE parameter.
  84. * @ctx: context in which the conversion should happen.
  85. *
  86. * It assumes @param is an OBJREF.
  87. * It does not set @arg.type; the caller should initialize it to a correct
  88. * &enum qcomtee_arg_type value. It gets the object's refcount in @arg;
  89. * the caller should manage to put it afterward.
  90. *
  91. * Return: On success, returns 0; on failure, returns < 0.
  92. */
  93. int qcomtee_objref_to_arg(struct qcomtee_arg *arg, struct tee_param *param,
  94. struct tee_context *ctx)
  95. {
  96. int err = -EINVAL;
  97. arg->o = NULL_QCOMTEE_OBJECT;
  98. /* param is a NULL object: */
  99. if (param->u.objref.id == TEE_OBJREF_NULL)
  100. return 0;
  101. /* param is a callback object: */
  102. if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_USER)
  103. err = qcomtee_user_param_to_object(&arg->o, param, ctx);
  104. /* param is a QTEE object: */
  105. else if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_TEE)
  106. err = qcomtee_context_find_qtee_object(&arg->o, param, ctx);
  107. /* param is a memory object: */
  108. else if (param->u.objref.flags & QCOMTEE_OBJREF_FLAG_MEM)
  109. err = qcomtee_memobj_param_to_object(&arg->o, param, ctx);
  110. /*
  111. * For callback objects, call qcomtee_object_get() to keep a temporary
  112. * copy for the driver, as these objects are released asynchronously
  113. * and may disappear even before returning from QTEE.
  114. *
  115. * - For direct object invocations, the matching put is called in
  116. * qcomtee_object_invoke() when parsing the QTEE response.
  117. * - For callback responses, put is called in qcomtee_user_object_notify()
  118. * after QTEE has received its copies.
  119. */
  120. if (!err && (typeof_qcomtee_object(arg->o) == QCOMTEE_OBJECT_TYPE_CB))
  121. qcomtee_object_get(arg->o);
  122. return err;
  123. }
  124. /**
  125. * qcomtee_objref_from_arg() - Convert QTEE argument to OBJREF param.
  126. * @param: TEE parameter.
  127. * @arg: QTEE argument.
  128. * @ctx: context in which the conversion should happen.
  129. *
  130. * It assumes @arg is of %QCOMTEE_ARG_TYPE_IO or %QCOMTEE_ARG_TYPE_OO.
  131. * It does not set @param.attr; the caller should initialize it to a
  132. * correct type.
  133. *
  134. * Return: On success, returns 0; on failure, returns < 0.
  135. */
  136. int qcomtee_objref_from_arg(struct tee_param *param, struct qcomtee_arg *arg,
  137. struct tee_context *ctx)
  138. {
  139. struct qcomtee_object *object = arg->o;
  140. switch (typeof_qcomtee_object(object)) {
  141. case QCOMTEE_OBJECT_TYPE_NULL:
  142. param->u.objref.id = TEE_OBJREF_NULL;
  143. return 0;
  144. case QCOMTEE_OBJECT_TYPE_CB:
  145. /* object is a callback object: */
  146. if (is_qcomtee_user_object(object))
  147. return qcomtee_user_param_from_object(param, object,
  148. ctx);
  149. /* object is a memory object: */
  150. else if (is_qcomtee_memobj_object(object))
  151. return qcomtee_memobj_param_from_object(param, object,
  152. ctx);
  153. break;
  154. case QCOMTEE_OBJECT_TYPE_TEE:
  155. return qcomtee_context_add_qtee_object(param, object, ctx);
  156. case QCOMTEE_OBJECT_TYPE_ROOT:
  157. default:
  158. break;
  159. }
  160. return -EINVAL;
  161. }
  162. /**
  163. * qcomtee_params_to_args() - Convert TEE parameters to QTEE arguments.
  164. * @u: QTEE arguments.
  165. * @params: TEE parameters.
  166. * @num_params: number of elements in the parameter array.
  167. * @ctx: context in which the conversion should happen.
  168. *
  169. * It assumes @u has at least @num_params + 1 entries and has been initialized
  170. * with %QCOMTEE_ARG_TYPE_INV as &struct qcomtee_arg.type.
  171. *
  172. * Return: On success, returns 0; on failure, returns < 0.
  173. */
  174. static int qcomtee_params_to_args(struct qcomtee_arg *u,
  175. struct tee_param *params, int num_params,
  176. struct tee_context *ctx)
  177. {
  178. int i;
  179. for (i = 0; i < num_params; i++) {
  180. switch (params[i].attr) {
  181. case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT:
  182. case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT:
  183. u[i].flags = QCOMTEE_ARG_FLAGS_UADDR;
  184. u[i].b.uaddr = params[i].u.ubuf.uaddr;
  185. u[i].b.size = params[i].u.ubuf.size;
  186. if (params[i].attr ==
  187. TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT)
  188. u[i].type = QCOMTEE_ARG_TYPE_IB;
  189. else /* TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT */
  190. u[i].type = QCOMTEE_ARG_TYPE_OB;
  191. break;
  192. case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT:
  193. u[i].type = QCOMTEE_ARG_TYPE_IO;
  194. if (qcomtee_objref_to_arg(&u[i], &params[i], ctx))
  195. goto out_failed;
  196. break;
  197. case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT:
  198. u[i].type = QCOMTEE_ARG_TYPE_OO;
  199. u[i].o = NULL_QCOMTEE_OBJECT;
  200. break;
  201. default:
  202. goto out_failed;
  203. }
  204. }
  205. return 0;
  206. out_failed:
  207. /* Undo qcomtee_objref_to_arg(). */
  208. for (i--; i >= 0; i--) {
  209. if (u[i].type != QCOMTEE_ARG_TYPE_IO)
  210. continue;
  211. qcomtee_user_object_set_notify(u[i].o, false);
  212. /* See docs for qcomtee_objref_to_arg() for double put. */
  213. if (typeof_qcomtee_object(u[i].o) == QCOMTEE_OBJECT_TYPE_CB)
  214. qcomtee_object_put(u[i].o);
  215. qcomtee_object_put(u[i].o);
  216. }
  217. return -EINVAL;
  218. }
  219. /**
  220. * qcomtee_params_from_args() - Convert QTEE arguments to TEE parameters.
  221. * @params: TEE parameters.
  222. * @u: QTEE arguments.
  223. * @num_params: number of elements in the parameter array.
  224. * @ctx: context in which the conversion should happen.
  225. *
  226. * @u should have already been initialized by qcomtee_params_to_args().
  227. * This also represents the end of a QTEE invocation that started with
  228. * qcomtee_params_to_args() by releasing %QCOMTEE_ARG_TYPE_IO objects.
  229. *
  230. * Return: On success, returns 0; on failure, returns < 0.
  231. */
  232. static int qcomtee_params_from_args(struct tee_param *params,
  233. struct qcomtee_arg *u, int num_params,
  234. struct tee_context *ctx)
  235. {
  236. int i, np;
  237. qcomtee_arg_for_each(np, u) {
  238. switch (u[np].type) {
  239. case QCOMTEE_ARG_TYPE_OB:
  240. /* TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT */
  241. params[np].u.ubuf.size = u[np].b.size;
  242. break;
  243. case QCOMTEE_ARG_TYPE_IO:
  244. /* IEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT */
  245. qcomtee_object_put(u[np].o);
  246. break;
  247. case QCOMTEE_ARG_TYPE_OO:
  248. /* TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT */
  249. if (qcomtee_objref_from_arg(&params[np], &u[np], ctx))
  250. goto out_failed;
  251. break;
  252. case QCOMTEE_ARG_TYPE_IB:
  253. default:
  254. break;
  255. }
  256. }
  257. return 0;
  258. out_failed:
  259. /* Undo qcomtee_objref_from_arg(). */
  260. for (i = 0; i < np; i++) {
  261. if (params[i].attr == TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT)
  262. qcomtee_context_del_qtee_object(&params[i], ctx);
  263. }
  264. /* Release any IO and OO objects not processed. */
  265. for (; i < num_params && u[i].type; i++) {
  266. if (u[i].type == QCOMTEE_ARG_TYPE_OO ||
  267. u[i].type == QCOMTEE_ARG_TYPE_IO)
  268. qcomtee_object_put(u[i].o);
  269. }
  270. return -EINVAL;
  271. }
  272. /* TEE Device Ops. */
  273. static int qcomtee_params_check(struct tee_param *params, int num_params)
  274. {
  275. int io = 0, oo = 0, ib = 0, ob = 0;
  276. int i;
  277. /* QTEE can accept 64 arguments. */
  278. if (num_params > QCOMTEE_ARGS_MAX)
  279. return -EINVAL;
  280. /* Supported parameter types. */
  281. for (i = 0; i < num_params; i++) {
  282. switch (params[i].attr) {
  283. case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT:
  284. ib++;
  285. break;
  286. case TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_OUTPUT:
  287. ob++;
  288. break;
  289. case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT:
  290. io++;
  291. break;
  292. case TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT:
  293. oo++;
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. }
  299. /* QTEE can accept 16 arguments of each supported types. */
  300. if (io > QCOMTEE_ARGS_PER_TYPE || oo > QCOMTEE_ARGS_PER_TYPE ||
  301. ib > QCOMTEE_ARGS_PER_TYPE || ob > QCOMTEE_ARGS_PER_TYPE)
  302. return -EINVAL;
  303. return 0;
  304. }
  305. /* Check if an operation on ROOT_QCOMTEE_OBJECT from userspace is permitted. */
  306. static int qcomtee_root_object_check(u32 op, struct tee_param *params,
  307. int num_params)
  308. {
  309. /* Some privileged operations recognized by QTEE. */
  310. if (op == QCOMTEE_ROOT_OP_NOTIFY_DOMAIN_CHANGE ||
  311. op == QCOMTEE_ROOT_OP_ADCI_ACCEPT ||
  312. op == QCOMTEE_ROOT_OP_ADCI_SHUTDOWN)
  313. return -EINVAL;
  314. /*
  315. * QCOMTEE_ROOT_OP_REG_WITH_CREDENTIALS is to register with QTEE
  316. * by passing a credential object as input OBJREF. TEE_OBJREF_NULL as a
  317. * credential object represents a privileged client for QTEE and
  318. * is used by the kernel only.
  319. */
  320. if (op == QCOMTEE_ROOT_OP_REG_WITH_CREDENTIALS && num_params == 2) {
  321. if (params[0].attr == TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_INPUT &&
  322. params[1].attr == TEE_IOCTL_PARAM_ATTR_TYPE_OBJREF_OUTPUT) {
  323. if (params[0].u.objref.id == TEE_OBJREF_NULL)
  324. return -EINVAL;
  325. }
  326. }
  327. return 0;
  328. }
  329. /**
  330. * qcomtee_object_invoke() - Invoke a QTEE object.
  331. * @ctx: TEE context.
  332. * @arg: ioctl arguments.
  333. * @params: parameters for the object.
  334. *
  335. * Return: On success, returns 0; on failure, returns < 0.
  336. */
  337. static int qcomtee_object_invoke(struct tee_context *ctx,
  338. struct tee_ioctl_object_invoke_arg *arg,
  339. struct tee_param *params)
  340. {
  341. struct qcomtee_context_data *ctxdata = ctx->data;
  342. struct qcomtee_object *object;
  343. int i, ret, result;
  344. if (qcomtee_params_check(params, arg->num_params))
  345. return -EINVAL;
  346. /* First, handle reserved operations: */
  347. if (arg->op == QCOMTEE_MSG_OBJECT_OP_RELEASE) {
  348. del_qtee_object(arg->id, ctxdata);
  349. return 0;
  350. }
  351. /* Otherwise, invoke a QTEE object: */
  352. struct qcomtee_object_invoke_ctx *oic __free(kfree) =
  353. qcomtee_object_invoke_ctx_alloc(ctx);
  354. if (!oic)
  355. return -ENOMEM;
  356. /* +1 for ending QCOMTEE_ARG_TYPE_INV. */
  357. struct qcomtee_arg *u __free(kfree) = kzalloc_objs(*u,
  358. arg->num_params + 1);
  359. if (!u)
  360. return -ENOMEM;
  361. /* Get an object to invoke. */
  362. if (arg->id == TEE_OBJREF_NULL) {
  363. /* Use ROOT if TEE_OBJREF_NULL is invoked. */
  364. if (qcomtee_root_object_check(arg->op, params, arg->num_params))
  365. return -EINVAL;
  366. object = ROOT_QCOMTEE_OBJECT;
  367. } else if (find_qtee_object(&object, arg->id, ctxdata)) {
  368. return -EINVAL;
  369. }
  370. ret = qcomtee_params_to_args(u, params, arg->num_params, ctx);
  371. if (ret)
  372. goto out;
  373. ret = qcomtee_object_do_invoke(oic, object, arg->op, u, &result);
  374. if (ret) {
  375. qcomtee_arg_for_each_input_object(i, u) {
  376. qcomtee_user_object_set_notify(u[i].o, false);
  377. qcomtee_object_put(u[i].o);
  378. }
  379. goto out;
  380. }
  381. /* Prase QTEE response and put driver's object copies: */
  382. if (!result) {
  383. /* Assume service is UNAVAIL if unable to process the result. */
  384. if (qcomtee_params_from_args(params, u, arg->num_params, ctx))
  385. result = QCOMTEE_MSG_ERROR_UNAVAIL;
  386. } else {
  387. /*
  388. * qcomtee_params_to_args() gets a copy of IO for the driver to
  389. * make sure they do not get released while in the middle of
  390. * invocation. On success (!result), qcomtee_params_from_args()
  391. * puts them; Otherwise, put them here.
  392. */
  393. qcomtee_arg_for_each_input_object(i, u)
  394. qcomtee_object_put(u[i].o);
  395. }
  396. arg->ret = result;
  397. out:
  398. qcomtee_object_put(object);
  399. return ret;
  400. }
  401. /**
  402. * qcomtee_supp_recv() - Wait for a request for the supplicant.
  403. * @ctx: TEE context.
  404. * @op: requested operation on the object.
  405. * @num_params: number of elements in the parameter array.
  406. * @params: parameters for @op.
  407. *
  408. * The first parameter is a meta %TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT.
  409. * On input, it provides a user buffer. This buffer is used for parameters of
  410. * type %TEE_IOCTL_PARAM_ATTR_TYPE_UBUF_INPUT in qcomtee_cb_params_from_args().
  411. * On output, the object ID and request ID are stored in the meta parameter.
  412. *
  413. * @num_params is updated to the number of parameters that actually exist
  414. * in @params on return.
  415. *
  416. * Return: On success, returns 0; on failure, returns < 0.
  417. */
  418. static int qcomtee_supp_recv(struct tee_context *ctx, u32 *op, u32 *num_params,
  419. struct tee_param *params)
  420. {
  421. struct qcomtee_user_object_request_data data;
  422. void __user *uaddr;
  423. size_t ubuf_size;
  424. int i, ret;
  425. if (!*num_params)
  426. return -EINVAL;
  427. /* First parameter should be an INOUT + meta parameter. */
  428. if (params->attr !=
  429. (TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT | TEE_IOCTL_PARAM_ATTR_META))
  430. return -EINVAL;
  431. /* Other parameters are none. */
  432. for (i = 1; i < *num_params; i++)
  433. if (params[i].attr)
  434. return -EINVAL;
  435. if (!IS_ALIGNED(params->u.value.a, 8))
  436. return -EINVAL;
  437. /* User buffer and size from meta parameter. */
  438. uaddr = u64_to_user_ptr(params->u.value.a);
  439. ubuf_size = params->u.value.b;
  440. /* Process TEE parameters. +/-1 to ignore the meta parameter. */
  441. ret = qcomtee_user_object_select(ctx, params + 1, *num_params - 1,
  442. uaddr, ubuf_size, &data);
  443. if (ret)
  444. return ret;
  445. params->u.value.a = data.object_id;
  446. params->u.value.b = data.id;
  447. params->u.value.c = 0;
  448. *op = data.op;
  449. *num_params = data.np + 1;
  450. return 0;
  451. }
  452. /**
  453. * qcomtee_supp_send() - Submit a response for a request.
  454. * @ctx: TEE context.
  455. * @errno: return value for the request.
  456. * @num_params: number of elements in the parameter array.
  457. * @params: returned parameters.
  458. *
  459. * The first parameter is a meta %TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT.
  460. * It specifies the request ID this response belongs to.
  461. *
  462. * Return: On success, returns 0; on failure, returns < 0.
  463. */
  464. static int qcomtee_supp_send(struct tee_context *ctx, u32 errno, u32 num_params,
  465. struct tee_param *params)
  466. {
  467. int req_id;
  468. if (!num_params)
  469. return -EINVAL;
  470. /* First parameter should be an OUTPUT + meta parameter. */
  471. if (params->attr != (TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT |
  472. TEE_IOCTL_PARAM_ATTR_META))
  473. return -EINVAL;
  474. req_id = params->u.value.a;
  475. /* Process TEE parameters. +/-1 to ignore the meta parameter. */
  476. return qcomtee_user_object_submit(ctx, params + 1, num_params - 1,
  477. req_id, errno);
  478. }
  479. static int qcomtee_open(struct tee_context *ctx)
  480. {
  481. struct qcomtee_context_data *ctxdata __free(kfree) = kzalloc_obj(*ctxdata);
  482. if (!ctxdata)
  483. return -ENOMEM;
  484. /*
  485. * In the QTEE driver, the same context is used to refcount resources
  486. * shared by QTEE. For example, teedev_ctx_get() is called for any
  487. * instance of callback objects (see qcomtee_user_param_to_object()).
  488. *
  489. * Maintain a copy of teedev for QTEE as it serves as a direct user of
  490. * this context. The teedev will be released in the context's release().
  491. *
  492. * tee_device_unregister() will remain blocked until all contexts
  493. * are released. This includes contexts owned by the user, which are
  494. * closed by teedev_close_context(), as well as those owned by QTEE
  495. * closed by teedev_ctx_put() in object's release().
  496. */
  497. if (!tee_device_get(ctx->teedev))
  498. return -EINVAL;
  499. idr_init(&ctxdata->qtee_objects_idr);
  500. mutex_init(&ctxdata->qtee_lock);
  501. idr_init(&ctxdata->reqs_idr);
  502. INIT_LIST_HEAD(&ctxdata->reqs_list);
  503. mutex_init(&ctxdata->reqs_lock);
  504. init_completion(&ctxdata->req_c);
  505. ctx->data = no_free_ptr(ctxdata);
  506. return 0;
  507. }
  508. /* Gets called when the user closes the device */
  509. static void qcomtee_close_context(struct tee_context *ctx)
  510. {
  511. struct qcomtee_context_data *ctxdata = ctx->data;
  512. struct qcomtee_object *object;
  513. int id;
  514. /* Process QUEUED or PROCESSING requests. */
  515. qcomtee_requests_destroy(ctxdata);
  516. /* Release QTEE objects. */
  517. idr_for_each_entry(&ctxdata->qtee_objects_idr, object, id)
  518. qcomtee_object_put(object);
  519. }
  520. /* Gets called when the final reference to the context goes away. */
  521. static void qcomtee_release(struct tee_context *ctx)
  522. {
  523. struct qcomtee_context_data *ctxdata = ctx->data;
  524. idr_destroy(&ctxdata->qtee_objects_idr);
  525. idr_destroy(&ctxdata->reqs_idr);
  526. kfree(ctxdata);
  527. /* There is nothing shared in this context with QTEE. */
  528. tee_device_put(ctx->teedev);
  529. }
  530. static void qcomtee_get_version(struct tee_device *teedev,
  531. struct tee_ioctl_version_data *vers)
  532. {
  533. struct tee_ioctl_version_data v = {
  534. .impl_id = TEE_IMPL_ID_QTEE,
  535. .gen_caps = TEE_GEN_CAP_OBJREF,
  536. };
  537. *vers = v;
  538. }
  539. /**
  540. * qcomtee_get_qtee_feature_list() - Query QTEE features versions.
  541. * @ctx: TEE context.
  542. * @id: ID of the feature to query.
  543. * @version: version of the feature.
  544. *
  545. * Used to query the verion of features supported by QTEE.
  546. */
  547. static void qcomtee_get_qtee_feature_list(struct tee_context *ctx, u32 id,
  548. u32 *version)
  549. {
  550. struct qcomtee_object *client_env, *service;
  551. struct qcomtee_arg u[3] = { 0 };
  552. int result;
  553. struct qcomtee_object_invoke_ctx *oic __free(kfree) =
  554. qcomtee_object_invoke_ctx_alloc(ctx);
  555. if (!oic)
  556. return;
  557. client_env = qcomtee_object_get_client_env(oic);
  558. if (client_env == NULL_QCOMTEE_OBJECT)
  559. return;
  560. /* Get ''FeatureVersions Service'' object. */
  561. service = qcomtee_object_get_service(oic, client_env,
  562. QCOMTEE_FEATURE_VER_UID);
  563. if (service == NULL_QCOMTEE_OBJECT)
  564. goto out_failed;
  565. /* IB: Feature to query. */
  566. u[0].b.addr = &id;
  567. u[0].b.size = sizeof(id);
  568. u[0].type = QCOMTEE_ARG_TYPE_IB;
  569. /* OB: Version returned. */
  570. u[1].b.addr = version;
  571. u[1].b.size = sizeof(*version);
  572. u[1].type = QCOMTEE_ARG_TYPE_OB;
  573. qcomtee_object_do_invoke(oic, service, QCOMTEE_FEATURE_VER_OP_GET, u,
  574. &result);
  575. out_failed:
  576. qcomtee_object_put(service);
  577. qcomtee_object_put(client_env);
  578. }
  579. static const struct tee_driver_ops qcomtee_ops = {
  580. .get_version = qcomtee_get_version,
  581. .open = qcomtee_open,
  582. .close_context = qcomtee_close_context,
  583. .release = qcomtee_release,
  584. .object_invoke_func = qcomtee_object_invoke,
  585. .supp_recv = qcomtee_supp_recv,
  586. .supp_send = qcomtee_supp_send,
  587. };
  588. static const struct tee_desc qcomtee_desc = {
  589. .name = "qcomtee",
  590. .ops = &qcomtee_ops,
  591. .owner = THIS_MODULE,
  592. };
  593. static int qcomtee_probe(struct platform_device *pdev)
  594. {
  595. struct workqueue_struct *async_wq;
  596. struct tee_device *teedev;
  597. struct tee_shm_pool *pool;
  598. struct tee_context *ctx;
  599. struct qcomtee *qcomtee;
  600. int err;
  601. qcomtee = kzalloc_obj(*qcomtee);
  602. if (!qcomtee)
  603. return -ENOMEM;
  604. pool = qcomtee_shm_pool_alloc();
  605. if (IS_ERR(pool)) {
  606. err = PTR_ERR(pool);
  607. goto err_free_qcomtee;
  608. }
  609. teedev = tee_device_alloc(&qcomtee_desc, NULL, pool, qcomtee);
  610. if (IS_ERR(teedev)) {
  611. err = PTR_ERR(teedev);
  612. goto err_pool_destroy;
  613. }
  614. qcomtee->teedev = teedev;
  615. qcomtee->pool = pool;
  616. err = tee_device_register(qcomtee->teedev);
  617. if (err)
  618. goto err_unreg_teedev;
  619. platform_set_drvdata(pdev, qcomtee);
  620. /* Start async wq. */
  621. async_wq = alloc_ordered_workqueue("qcomtee_wq", 0);
  622. if (!async_wq) {
  623. err = -ENOMEM;
  624. goto err_unreg_teedev;
  625. }
  626. qcomtee->wq = async_wq;
  627. /* Driver context used for async operations of teedev. */
  628. ctx = teedev_open(qcomtee->teedev);
  629. if (IS_ERR(ctx)) {
  630. err = PTR_ERR(ctx);
  631. goto err_dest_wq;
  632. }
  633. qcomtee->ctx = ctx;
  634. /* Init Object table. */
  635. qcomtee->xa_last_id = 0;
  636. xa_init_flags(&qcomtee->xa_local_objects, XA_FLAGS_ALLOC);
  637. /* Get QTEE verion. */
  638. qcomtee_get_qtee_feature_list(qcomtee->ctx,
  639. QCOMTEE_FEATURE_VER_OP_GET_QTEE_ID,
  640. &qcomtee->qtee_version);
  641. pr_info("QTEE version %u.%u.%u\n",
  642. QTEE_VERSION_GET_MAJOR(qcomtee->qtee_version),
  643. QTEE_VERSION_GET_MINOR(qcomtee->qtee_version),
  644. QTEE_VERSION_GET_PATCH(qcomtee->qtee_version));
  645. return 0;
  646. err_dest_wq:
  647. destroy_workqueue(qcomtee->wq);
  648. err_unreg_teedev:
  649. tee_device_unregister(qcomtee->teedev);
  650. err_pool_destroy:
  651. tee_shm_pool_free(pool);
  652. err_free_qcomtee:
  653. kfree(qcomtee);
  654. return err;
  655. }
  656. /**
  657. * qcomtee_remove() - Device Removal Routine.
  658. * @pdev: platform device information struct.
  659. *
  660. * It is called by the platform subsystem to alert the driver that it should
  661. * release the device.
  662. *
  663. * QTEE does not provide an API to inform it about a callback object going away.
  664. * However, when releasing QTEE objects, any callback object sent to QTEE
  665. * previously would be released by QTEE as part of the object release.
  666. */
  667. static void qcomtee_remove(struct platform_device *pdev)
  668. {
  669. struct qcomtee *qcomtee = platform_get_drvdata(pdev);
  670. teedev_close_context(qcomtee->ctx);
  671. /* Wait for RELEASE operations to be processed for QTEE objects. */
  672. tee_device_unregister(qcomtee->teedev);
  673. destroy_workqueue(qcomtee->wq);
  674. tee_shm_pool_free(qcomtee->pool);
  675. kfree(qcomtee);
  676. }
  677. static const struct platform_device_id qcomtee_ids[] = { { "qcomtee", 0 }, {} };
  678. MODULE_DEVICE_TABLE(platform, qcomtee_ids);
  679. static struct platform_driver qcomtee_platform_driver = {
  680. .probe = qcomtee_probe,
  681. .remove = qcomtee_remove,
  682. .driver = {
  683. .name = "qcomtee",
  684. },
  685. .id_table = qcomtee_ids,
  686. };
  687. module_platform_driver(qcomtee_platform_driver);
  688. MODULE_AUTHOR("Qualcomm");
  689. MODULE_DESCRIPTION("QTEE driver");
  690. MODULE_VERSION("1.0");
  691. MODULE_LICENSE("GPL");