core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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/firmware/qcom/qcom_scm.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/xarray.h>
  12. #include "qcomtee.h"
  13. /* QTEE root object. */
  14. struct qcomtee_object qcomtee_object_root = {
  15. .name = "root",
  16. .object_type = QCOMTEE_OBJECT_TYPE_ROOT,
  17. .info.qtee_id = QCOMTEE_MSG_OBJECT_ROOT,
  18. };
  19. /* Next argument of type @type after index @i. */
  20. int qcomtee_next_arg_type(struct qcomtee_arg *u, int i,
  21. enum qcomtee_arg_type type)
  22. {
  23. while (u[i].type != QCOMTEE_ARG_TYPE_INV && u[i].type != type)
  24. i++;
  25. return i;
  26. }
  27. /*
  28. * QTEE expects IDs with QCOMTEE_MSG_OBJECT_NS_BIT set for objects of
  29. * QCOMTEE_OBJECT_TYPE_CB type. The first ID with QCOMTEE_MSG_OBJECT_NS_BIT
  30. * set is reserved for the primordial object.
  31. */
  32. #define QCOMTEE_OBJECT_PRIMORDIAL (QCOMTEE_MSG_OBJECT_NS_BIT)
  33. #define QCOMTEE_OBJECT_ID_START (QCOMTEE_OBJECT_PRIMORDIAL + 1)
  34. #define QCOMTEE_OBJECT_ID_END (U32_MAX)
  35. #define QCOMTEE_OBJECT_SET(p, type, ...) \
  36. __QCOMTEE_OBJECT_SET(p, type, ##__VA_ARGS__, 0UL)
  37. #define __QCOMTEE_OBJECT_SET(p, type, optr, ...) \
  38. do { \
  39. (p)->object_type = (type); \
  40. (p)->info.qtee_id = (unsigned long)(optr); \
  41. } while (0)
  42. static struct qcomtee_object *
  43. qcomtee_qtee_object_alloc(struct qcomtee_object_invoke_ctx *oic,
  44. unsigned int object_id)
  45. {
  46. struct qcomtee *qcomtee = tee_get_drvdata(oic->ctx->teedev);
  47. struct qcomtee_object *object;
  48. object = kzalloc_obj(*object);
  49. if (!object)
  50. return NULL_QCOMTEE_OBJECT;
  51. /* If failed, "no-name". */
  52. object->name = kasprintf(GFP_KERNEL, "qcomtee-%u", object_id);
  53. QCOMTEE_OBJECT_SET(object, QCOMTEE_OBJECT_TYPE_TEE, object_id);
  54. kref_init(&object->refcount);
  55. /* A QTEE object requires a context for async operations. */
  56. object->info.qcomtee_async_ctx = qcomtee->ctx;
  57. teedev_ctx_get(object->info.qcomtee_async_ctx);
  58. return object;
  59. }
  60. static void qcomtee_qtee_object_free(struct qcomtee_object *object)
  61. {
  62. /* See qcomtee_qtee_object_alloc(). */
  63. teedev_ctx_put(object->info.qcomtee_async_ctx);
  64. kfree(object->name);
  65. kfree(object);
  66. }
  67. static void qcomtee_do_release_qtee_object(struct work_struct *work)
  68. {
  69. struct qcomtee_object *object;
  70. struct qcomtee *qcomtee;
  71. int ret, result = 0;
  72. /* RELEASE does not require any argument. */
  73. struct qcomtee_arg args[] = { { .type = QCOMTEE_ARG_TYPE_INV } };
  74. object = container_of(work, struct qcomtee_object, work);
  75. qcomtee = tee_get_drvdata(object->info.qcomtee_async_ctx->teedev);
  76. /* Get the TEE context used for asynchronous operations. */
  77. qcomtee->oic.ctx = object->info.qcomtee_async_ctx;
  78. ret = qcomtee_object_do_invoke_internal(&qcomtee->oic, object,
  79. QCOMTEE_MSG_OBJECT_OP_RELEASE,
  80. args, &result);
  81. /* Is it safe to retry the release? */
  82. if (ret && ret != -ENODEV) {
  83. queue_work(qcomtee->wq, &object->work);
  84. } else {
  85. if (ret || result)
  86. pr_err("%s release failed, ret = %d (%x)\n",
  87. qcomtee_object_name(object), ret, result);
  88. qcomtee_qtee_object_free(object);
  89. }
  90. }
  91. static void qcomtee_release_qtee_object(struct qcomtee_object *object)
  92. {
  93. struct qcomtee *qcomtee =
  94. tee_get_drvdata(object->info.qcomtee_async_ctx->teedev);
  95. INIT_WORK(&object->work, qcomtee_do_release_qtee_object);
  96. queue_work(qcomtee->wq, &object->work);
  97. }
  98. static void qcomtee_object_release(struct kref *refcount)
  99. {
  100. struct qcomtee_object *object;
  101. const char *name;
  102. object = container_of(refcount, struct qcomtee_object, refcount);
  103. /*
  104. * qcomtee_object_get() is called in a RCU read lock. synchronize_rcu()
  105. * to avoid releasing the object while it is being accessed in
  106. * qcomtee_object_get().
  107. */
  108. synchronize_rcu();
  109. switch (typeof_qcomtee_object(object)) {
  110. case QCOMTEE_OBJECT_TYPE_TEE:
  111. qcomtee_release_qtee_object(object);
  112. break;
  113. case QCOMTEE_OBJECT_TYPE_CB:
  114. name = object->name;
  115. if (object->ops->release)
  116. object->ops->release(object);
  117. kfree_const(name);
  118. break;
  119. case QCOMTEE_OBJECT_TYPE_ROOT:
  120. case QCOMTEE_OBJECT_TYPE_NULL:
  121. default:
  122. break;
  123. }
  124. }
  125. /**
  126. * qcomtee_object_get() - Increase the object's reference count.
  127. * @object: object to increase the reference count.
  128. *
  129. * Context: The caller should hold RCU read lock.
  130. */
  131. int qcomtee_object_get(struct qcomtee_object *object)
  132. {
  133. if (object != &qcomtee_primordial_object &&
  134. object != NULL_QCOMTEE_OBJECT &&
  135. object != ROOT_QCOMTEE_OBJECT)
  136. return kref_get_unless_zero(&object->refcount);
  137. return 0;
  138. }
  139. /**
  140. * qcomtee_object_put() - Decrease the object's reference count.
  141. * @object: object to decrease the reference count.
  142. */
  143. void qcomtee_object_put(struct qcomtee_object *object)
  144. {
  145. if (object != &qcomtee_primordial_object &&
  146. object != NULL_QCOMTEE_OBJECT &&
  147. object != ROOT_QCOMTEE_OBJECT)
  148. kref_put(&object->refcount, qcomtee_object_release);
  149. }
  150. static int qcomtee_idx_alloc(struct qcomtee_object_invoke_ctx *oic, u32 *idx,
  151. struct qcomtee_object *object)
  152. {
  153. struct qcomtee *qcomtee = tee_get_drvdata(oic->ctx->teedev);
  154. /* Every ID allocated here has QCOMTEE_MSG_OBJECT_NS_BIT set. */
  155. return xa_alloc_cyclic(&qcomtee->xa_local_objects, idx, object,
  156. XA_LIMIT(QCOMTEE_OBJECT_ID_START,
  157. QCOMTEE_OBJECT_ID_END),
  158. &qcomtee->xa_last_id, GFP_KERNEL);
  159. }
  160. struct qcomtee_object *qcomtee_idx_erase(struct qcomtee_object_invoke_ctx *oic,
  161. u32 idx)
  162. {
  163. struct qcomtee *qcomtee = tee_get_drvdata(oic->ctx->teedev);
  164. if (idx < QCOMTEE_OBJECT_ID_START || idx > QCOMTEE_OBJECT_ID_END)
  165. return NULL_QCOMTEE_OBJECT;
  166. return xa_erase(&qcomtee->xa_local_objects, idx);
  167. }
  168. /**
  169. * qcomtee_object_id_get() - Get an ID for an object to send to QTEE.
  170. * @oic: context to use for the invocation.
  171. * @object: object to assign an ID.
  172. * @object_id: object ID.
  173. *
  174. * Called on the path to QTEE to construct the message; see
  175. * qcomtee_prepare_msg() and qcomtee_update_msg().
  176. *
  177. * Return: On success, returns 0; on failure, returns < 0.
  178. */
  179. static int qcomtee_object_id_get(struct qcomtee_object_invoke_ctx *oic,
  180. struct qcomtee_object *object,
  181. unsigned int *object_id)
  182. {
  183. u32 idx;
  184. switch (typeof_qcomtee_object(object)) {
  185. case QCOMTEE_OBJECT_TYPE_CB:
  186. if (qcomtee_idx_alloc(oic, &idx, object) < 0)
  187. return -ENOSPC;
  188. *object_id = idx;
  189. break;
  190. case QCOMTEE_OBJECT_TYPE_ROOT:
  191. case QCOMTEE_OBJECT_TYPE_TEE:
  192. *object_id = object->info.qtee_id;
  193. break;
  194. case QCOMTEE_OBJECT_TYPE_NULL:
  195. *object_id = QCOMTEE_MSG_OBJECT_NULL;
  196. break;
  197. }
  198. return 0;
  199. }
  200. /* Release object ID assigned in qcomtee_object_id_get. */
  201. static void qcomtee_object_id_put(struct qcomtee_object_invoke_ctx *oic,
  202. unsigned int object_id)
  203. {
  204. qcomtee_idx_erase(oic, object_id);
  205. }
  206. /**
  207. * qcomtee_local_object_get() - Get the object referenced by the ID.
  208. * @oic: context to use for the invocation.
  209. * @object_id: object ID.
  210. *
  211. * It is called on the path from QTEE.
  212. * It is called on behalf of QTEE to obtain an instance of an object
  213. * for a given ID. It increases the object's reference count on success.
  214. *
  215. * Return: On error, returns %NULL_QCOMTEE_OBJECT.
  216. * On success, returns the object.
  217. */
  218. static struct qcomtee_object *
  219. qcomtee_local_object_get(struct qcomtee_object_invoke_ctx *oic,
  220. unsigned int object_id)
  221. {
  222. struct qcomtee *qcomtee = tee_get_drvdata(oic->ctx->teedev);
  223. struct qcomtee_object *object;
  224. if (object_id == QCOMTEE_OBJECT_PRIMORDIAL)
  225. return &qcomtee_primordial_object;
  226. guard(rcu)();
  227. object = xa_load(&qcomtee->xa_local_objects, object_id);
  228. /* It already checks for %NULL_QCOMTEE_OBJECT. */
  229. qcomtee_object_get(object);
  230. return object;
  231. }
  232. /**
  233. * qcomtee_object_user_init() - Initialize an object for the user.
  234. * @object: object to initialize.
  235. * @ot: type of object as &enum qcomtee_object_type.
  236. * @ops: instance of callbacks.
  237. * @fmt: name assigned to the object.
  238. *
  239. * Return: On success, returns 0; on failure, returns < 0.
  240. */
  241. int qcomtee_object_user_init(struct qcomtee_object *object,
  242. enum qcomtee_object_type ot,
  243. struct qcomtee_object_operations *ops,
  244. const char *fmt, ...)
  245. {
  246. va_list ap;
  247. int ret;
  248. kref_init(&object->refcount);
  249. QCOMTEE_OBJECT_SET(object, QCOMTEE_OBJECT_TYPE_NULL);
  250. va_start(ap, fmt);
  251. switch (ot) {
  252. case QCOMTEE_OBJECT_TYPE_NULL:
  253. ret = 0;
  254. break;
  255. case QCOMTEE_OBJECT_TYPE_CB:
  256. object->ops = ops;
  257. if (!object->ops->dispatch)
  258. return -EINVAL;
  259. /* If failed, "no-name". */
  260. object->name = kvasprintf_const(GFP_KERNEL, fmt, ap);
  261. QCOMTEE_OBJECT_SET(object, QCOMTEE_OBJECT_TYPE_CB);
  262. ret = 0;
  263. break;
  264. case QCOMTEE_OBJECT_TYPE_ROOT:
  265. case QCOMTEE_OBJECT_TYPE_TEE:
  266. default:
  267. ret = -EINVAL;
  268. }
  269. va_end(ap);
  270. return ret;
  271. }
  272. /**
  273. * qcomtee_object_type() - Returns the type of object represented by an ID.
  274. * @object_id: object ID for the object.
  275. *
  276. * Similar to typeof_qcomtee_object(), but instead of receiving an object as
  277. * an argument, it receives an object ID. It is used internally on the return
  278. * path from QTEE.
  279. *
  280. * Return: Returns the type of object referenced by @object_id.
  281. */
  282. static enum qcomtee_object_type qcomtee_object_type(unsigned int object_id)
  283. {
  284. if (object_id == QCOMTEE_MSG_OBJECT_NULL)
  285. return QCOMTEE_OBJECT_TYPE_NULL;
  286. if (object_id & QCOMTEE_MSG_OBJECT_NS_BIT)
  287. return QCOMTEE_OBJECT_TYPE_CB;
  288. return QCOMTEE_OBJECT_TYPE_TEE;
  289. }
  290. /**
  291. * qcomtee_object_qtee_init() - Initialize an object for QTEE.
  292. * @oic: context to use for the invocation.
  293. * @object: object returned.
  294. * @object_id: object ID received from QTEE.
  295. *
  296. * Return: On failure, returns < 0 and sets @object to %NULL_QCOMTEE_OBJECT.
  297. * On success, returns 0
  298. */
  299. static int qcomtee_object_qtee_init(struct qcomtee_object_invoke_ctx *oic,
  300. struct qcomtee_object **object,
  301. unsigned int object_id)
  302. {
  303. int ret = 0;
  304. switch (qcomtee_object_type(object_id)) {
  305. case QCOMTEE_OBJECT_TYPE_NULL:
  306. *object = NULL_QCOMTEE_OBJECT;
  307. break;
  308. case QCOMTEE_OBJECT_TYPE_CB:
  309. *object = qcomtee_local_object_get(oic, object_id);
  310. if (*object == NULL_QCOMTEE_OBJECT)
  311. ret = -EINVAL;
  312. break;
  313. default: /* QCOMTEE_OBJECT_TYPE_TEE */
  314. *object = qcomtee_qtee_object_alloc(oic, object_id);
  315. if (*object == NULL_QCOMTEE_OBJECT)
  316. ret = -ENOMEM;
  317. break;
  318. }
  319. return ret;
  320. }
  321. /*
  322. * ''Marshaling API''
  323. * qcomtee_prepare_msg - Prepare the inbound buffer for sending to QTEE
  324. * qcomtee_update_args - Parse the QTEE response in the inbound buffer
  325. * qcomtee_prepare_args - Parse the QTEE request from the outbound buffer
  326. * qcomtee_update_msg - Update the outbound buffer with the response for QTEE
  327. */
  328. static int qcomtee_prepare_msg(struct qcomtee_object_invoke_ctx *oic,
  329. struct qcomtee_object *object, u32 op,
  330. struct qcomtee_arg *u)
  331. {
  332. struct qcomtee_msg_object_invoke *msg;
  333. unsigned int object_id;
  334. int i, ib, ob, io, oo;
  335. size_t offset;
  336. /* Use the input message buffer in 'oic'. */
  337. msg = oic->in_msg.addr;
  338. /* Start offset in a message for buffer arguments. */
  339. offset = qcomtee_msg_buffer_args(struct qcomtee_msg_object_invoke,
  340. qcomtee_args_len(u));
  341. /* Get the ID of the object being invoked. */
  342. if (qcomtee_object_id_get(oic, object, &object_id))
  343. return -ENOSPC;
  344. ib = 0;
  345. qcomtee_arg_for_each_input_buffer(i, u) {
  346. void *msgptr; /* Address of buffer payload: */
  347. /* Overflow already checked in qcomtee_msg_buffers_alloc(). */
  348. msg->args[ib].b.offset = offset;
  349. msg->args[ib].b.size = u[i].b.size;
  350. msgptr = qcomtee_msg_offset_to_ptr(msg, offset);
  351. /* Userspace client or kernel client!? */
  352. if (!(u[i].flags & QCOMTEE_ARG_FLAGS_UADDR))
  353. memcpy(msgptr, u[i].b.addr, u[i].b.size);
  354. else if (copy_from_user(msgptr, u[i].b.uaddr, u[i].b.size))
  355. return -EFAULT;
  356. offset += qcomtee_msg_offset_align(u[i].b.size);
  357. ib++;
  358. }
  359. ob = ib;
  360. qcomtee_arg_for_each_output_buffer(i, u) {
  361. /* Overflow already checked in qcomtee_msg_buffers_alloc(). */
  362. msg->args[ob].b.offset = offset;
  363. msg->args[ob].b.size = u[i].b.size;
  364. offset += qcomtee_msg_offset_align(u[i].b.size);
  365. ob++;
  366. }
  367. io = ob;
  368. qcomtee_arg_for_each_input_object(i, u) {
  369. if (qcomtee_object_id_get(oic, u[i].o, &msg->args[io].o)) {
  370. qcomtee_object_id_put(oic, object_id);
  371. for (io--; io >= ob; io--)
  372. qcomtee_object_id_put(oic, msg->args[io].o);
  373. return -ENOSPC;
  374. }
  375. io++;
  376. }
  377. oo = io;
  378. qcomtee_arg_for_each_output_object(i, u)
  379. oo++;
  380. /* Set object, operation, and argument counts. */
  381. qcomtee_msg_init(msg, object_id, op, ib, ob, io, oo);
  382. return 0;
  383. }
  384. /**
  385. * qcomtee_update_args() - Parse the QTEE response in the inbound buffer.
  386. * @u: array of arguments for the invocation.
  387. * @oic: context to use for the invocation.
  388. *
  389. * @u must be the same as the one used in qcomtee_prepare_msg() when
  390. * initializing the inbound buffer.
  391. *
  392. * On failure, it continues processing the QTEE message. The caller should
  393. * do the necessary cleanup, including calling qcomtee_object_put()
  394. * on the output objects.
  395. *
  396. * Return: On success, returns 0; on failure, returns < 0.
  397. */
  398. static int qcomtee_update_args(struct qcomtee_arg *u,
  399. struct qcomtee_object_invoke_ctx *oic)
  400. {
  401. struct qcomtee_msg_object_invoke *msg;
  402. int i, ib, ob, io, oo;
  403. int ret = 0;
  404. /* Use the input message buffer in 'oic'. */
  405. msg = oic->in_msg.addr;
  406. ib = 0;
  407. qcomtee_arg_for_each_input_buffer(i, u)
  408. ib++;
  409. ob = ib;
  410. qcomtee_arg_for_each_output_buffer(i, u) {
  411. void *msgptr; /* Address of buffer payload: */
  412. /* QTEE can override the size to a smaller value. */
  413. u[i].b.size = msg->args[ob].b.size;
  414. msgptr = qcomtee_msg_offset_to_ptr(msg, msg->args[ob].b.offset);
  415. /* Userspace client or kernel client!? */
  416. if (!(u[i].flags & QCOMTEE_ARG_FLAGS_UADDR))
  417. memcpy(u[i].b.addr, msgptr, u[i].b.size);
  418. else if (copy_to_user(u[i].b.uaddr, msgptr, u[i].b.size))
  419. ret = -EINVAL;
  420. ob++;
  421. }
  422. io = ob;
  423. qcomtee_arg_for_each_input_object(i, u)
  424. io++;
  425. oo = io;
  426. qcomtee_arg_for_each_output_object(i, u) {
  427. if (qcomtee_object_qtee_init(oic, &u[i].o, msg->args[oo].o))
  428. ret = -EINVAL;
  429. oo++;
  430. }
  431. return ret;
  432. }
  433. /**
  434. * qcomtee_prepare_args() - Parse the QTEE request from the outbound buffer.
  435. * @oic: context to use for the invocation.
  436. *
  437. * It initializes &qcomtee_object_invoke_ctx->u based on the QTEE request in
  438. * the outbound buffer. It sets %QCOMTEE_ARG_TYPE_INV at the end of the array.
  439. *
  440. * On failure, it continues processing the QTEE message. The caller should
  441. * do the necessary cleanup, including calling qcomtee_object_put()
  442. * on the input objects.
  443. *
  444. * Return: On success, returns 0; on failure, returns < 0.
  445. */
  446. static int qcomtee_prepare_args(struct qcomtee_object_invoke_ctx *oic)
  447. {
  448. struct qcomtee_msg_callback *msg;
  449. int i, ret = 0;
  450. /* Use the output message buffer in 'oic'. */
  451. msg = oic->out_msg.addr;
  452. qcomtee_msg_for_each_input_buffer(i, msg) {
  453. oic->u[i].b.addr =
  454. qcomtee_msg_offset_to_ptr(msg, msg->args[i].b.offset);
  455. oic->u[i].b.size = msg->args[i].b.size;
  456. oic->u[i].type = QCOMTEE_ARG_TYPE_IB;
  457. }
  458. qcomtee_msg_for_each_output_buffer(i, msg) {
  459. oic->u[i].b.addr =
  460. qcomtee_msg_offset_to_ptr(msg, msg->args[i].b.offset);
  461. oic->u[i].b.size = msg->args[i].b.size;
  462. oic->u[i].type = QCOMTEE_ARG_TYPE_OB;
  463. }
  464. qcomtee_msg_for_each_input_object(i, msg) {
  465. if (qcomtee_object_qtee_init(oic, &oic->u[i].o, msg->args[i].o))
  466. ret = -EINVAL;
  467. oic->u[i].type = QCOMTEE_ARG_TYPE_IO;
  468. }
  469. qcomtee_msg_for_each_output_object(i, msg)
  470. oic->u[i].type = QCOMTEE_ARG_TYPE_OO;
  471. /* End of Arguments. */
  472. oic->u[i].type = QCOMTEE_ARG_TYPE_INV;
  473. return ret;
  474. }
  475. static int qcomtee_update_msg(struct qcomtee_object_invoke_ctx *oic)
  476. {
  477. struct qcomtee_msg_callback *msg;
  478. int i, ib, ob, io, oo;
  479. /* Use the output message buffer in 'oic'. */
  480. msg = oic->out_msg.addr;
  481. ib = 0;
  482. qcomtee_arg_for_each_input_buffer(i, oic->u)
  483. ib++;
  484. ob = ib;
  485. qcomtee_arg_for_each_output_buffer(i, oic->u) {
  486. /* Only reduce size; never increase it. */
  487. if (msg->args[ob].b.size < oic->u[i].b.size)
  488. return -EINVAL;
  489. msg->args[ob].b.size = oic->u[i].b.size;
  490. ob++;
  491. }
  492. io = ob;
  493. qcomtee_arg_for_each_input_object(i, oic->u)
  494. io++;
  495. oo = io;
  496. qcomtee_arg_for_each_output_object(i, oic->u) {
  497. if (qcomtee_object_id_get(oic, oic->u[i].o, &msg->args[oo].o)) {
  498. for (oo--; oo >= io; oo--)
  499. qcomtee_object_id_put(oic, msg->args[oo].o);
  500. return -ENOSPC;
  501. }
  502. oo++;
  503. }
  504. return 0;
  505. }
  506. /* Invoke a callback object. */
  507. static void qcomtee_cb_object_invoke(struct qcomtee_object_invoke_ctx *oic,
  508. struct qcomtee_msg_callback *msg)
  509. {
  510. int i, errno;
  511. u32 op;
  512. /* Get the object being invoked. */
  513. unsigned int object_id = msg->cxt;
  514. struct qcomtee_object *object;
  515. /* QTEE cannot invoke a NULL object or objects it hosts. */
  516. if (qcomtee_object_type(object_id) == QCOMTEE_OBJECT_TYPE_NULL ||
  517. qcomtee_object_type(object_id) == QCOMTEE_OBJECT_TYPE_TEE) {
  518. errno = -EINVAL;
  519. goto out;
  520. }
  521. object = qcomtee_local_object_get(oic, object_id);
  522. if (object == NULL_QCOMTEE_OBJECT) {
  523. errno = -EINVAL;
  524. goto out;
  525. }
  526. oic->object = object;
  527. /* Filter bits used by transport. */
  528. op = msg->op & QCOMTEE_MSG_OBJECT_OP_MASK;
  529. switch (op) {
  530. case QCOMTEE_MSG_OBJECT_OP_RELEASE:
  531. qcomtee_object_id_put(oic, object_id);
  532. qcomtee_object_put(object);
  533. errno = 0;
  534. break;
  535. case QCOMTEE_MSG_OBJECT_OP_RETAIN:
  536. qcomtee_object_get(object);
  537. errno = 0;
  538. break;
  539. default:
  540. errno = qcomtee_prepare_args(oic);
  541. if (errno) {
  542. /* Release any object that arrived as input. */
  543. qcomtee_arg_for_each_input_buffer(i, oic->u)
  544. qcomtee_object_put(oic->u[i].o);
  545. break;
  546. }
  547. errno = object->ops->dispatch(oic, object, op, oic->u);
  548. if (!errno) {
  549. /* On success, notify at the appropriate time. */
  550. oic->flags |= QCOMTEE_OIC_FLAG_NOTIFY;
  551. }
  552. }
  553. out:
  554. oic->errno = errno;
  555. }
  556. static int
  557. qcomtee_object_invoke_ctx_invoke(struct qcomtee_object_invoke_ctx *oic,
  558. int *result, u64 *res_type)
  559. {
  560. phys_addr_t out_msg_paddr;
  561. phys_addr_t in_msg_paddr;
  562. int ret;
  563. u64 res;
  564. tee_shm_get_pa(oic->out_shm, 0, &out_msg_paddr);
  565. tee_shm_get_pa(oic->in_shm, 0, &in_msg_paddr);
  566. if (!(oic->flags & QCOMTEE_OIC_FLAG_BUSY))
  567. ret = qcom_scm_qtee_invoke_smc(in_msg_paddr, oic->in_msg.size,
  568. out_msg_paddr, oic->out_msg.size,
  569. &res, res_type);
  570. else
  571. ret = qcom_scm_qtee_callback_response(out_msg_paddr,
  572. oic->out_msg.size,
  573. &res, res_type);
  574. if (ret)
  575. pr_err("QTEE returned with %d.\n", ret);
  576. else
  577. *result = (int)res;
  578. return ret;
  579. }
  580. /**
  581. * qcomtee_qtee_objects_put() - Put the callback objects in the argument array.
  582. * @u: array of arguments.
  583. *
  584. * When qcomtee_object_do_invoke_internal() is successfully invoked,
  585. * QTEE takes ownership of the callback objects. If the invocation fails,
  586. * qcomtee_object_do_invoke_internal() calls qcomtee_qtee_objects_put()
  587. * to mimic the release of callback objects by QTEE.
  588. */
  589. static void qcomtee_qtee_objects_put(struct qcomtee_arg *u)
  590. {
  591. int i;
  592. qcomtee_arg_for_each_input_object(i, u) {
  593. if (typeof_qcomtee_object(u[i].o) == QCOMTEE_OBJECT_TYPE_CB)
  594. qcomtee_object_put(u[i].o);
  595. }
  596. }
  597. /**
  598. * qcomtee_object_do_invoke_internal() - Submit an invocation for an object.
  599. * @oic: context to use for the current invocation.
  600. * @object: object being invoked.
  601. * @op: requested operation on the object.
  602. * @u: array of arguments for the current invocation.
  603. * @result: result returned from QTEE.
  604. *
  605. * The caller is responsible for keeping track of the refcount for each
  606. * object, including @object. On return, the caller loses ownership of all
  607. * input objects of type %QCOMTEE_OBJECT_TYPE_CB.
  608. *
  609. * Return: On success, returns 0; on failure, returns < 0.
  610. */
  611. int qcomtee_object_do_invoke_internal(struct qcomtee_object_invoke_ctx *oic,
  612. struct qcomtee_object *object, u32 op,
  613. struct qcomtee_arg *u, int *result)
  614. {
  615. struct qcomtee_msg_callback *cb_msg;
  616. struct qcomtee_object *qto;
  617. int i, ret, errno;
  618. u64 res_type;
  619. /* Allocate inbound and outbound buffers. */
  620. ret = qcomtee_msg_buffers_alloc(oic, u);
  621. if (ret) {
  622. qcomtee_qtee_objects_put(u);
  623. return ret;
  624. }
  625. ret = qcomtee_prepare_msg(oic, object, op, u);
  626. if (ret) {
  627. qcomtee_qtee_objects_put(u);
  628. goto out;
  629. }
  630. /* Use input message buffer in 'oic'. */
  631. cb_msg = oic->out_msg.addr;
  632. while (1) {
  633. if (oic->flags & QCOMTEE_OIC_FLAG_BUSY) {
  634. errno = oic->errno;
  635. if (!errno)
  636. errno = qcomtee_update_msg(oic);
  637. qcomtee_msg_set_result(cb_msg, errno);
  638. }
  639. /* Invoke the remote object. */
  640. ret = qcomtee_object_invoke_ctx_invoke(oic, result, &res_type);
  641. /* Return form callback objects result submission: */
  642. if (oic->flags & QCOMTEE_OIC_FLAG_BUSY) {
  643. qto = oic->object;
  644. if (qto) {
  645. if (oic->flags & QCOMTEE_OIC_FLAG_NOTIFY) {
  646. if (qto->ops->notify)
  647. qto->ops->notify(oic, qto,
  648. errno || ret);
  649. }
  650. /* Get is in qcomtee_cb_object_invoke(). */
  651. qcomtee_object_put(qto);
  652. }
  653. oic->object = NULL_QCOMTEE_OBJECT;
  654. oic->flags &= ~(QCOMTEE_OIC_FLAG_BUSY |
  655. QCOMTEE_OIC_FLAG_NOTIFY);
  656. }
  657. if (ret) {
  658. /*
  659. * Unable to finished the invocation.
  660. * If QCOMTEE_OIC_FLAG_SHARED is not set, put
  661. * QCOMTEE_OBJECT_TYPE_CB input objects.
  662. */
  663. if (!(oic->flags & QCOMTEE_OIC_FLAG_SHARED))
  664. qcomtee_qtee_objects_put(u);
  665. else
  666. ret = -ENODEV;
  667. goto out;
  668. } else {
  669. /*
  670. * QTEE obtained ownership of QCOMTEE_OBJECT_TYPE_CB
  671. * input objects in 'u'. On further failure, QTEE is
  672. * responsible for releasing them.
  673. */
  674. oic->flags |= QCOMTEE_OIC_FLAG_SHARED;
  675. }
  676. /* Is it a callback request? */
  677. if (res_type != QCOMTEE_RESULT_INBOUND_REQ_NEEDED) {
  678. /*
  679. * Parse results. If failed, assume the service
  680. * was unavailable (i.e. QCOMTEE_MSG_ERROR_UNAVAIL)
  681. * and put output objects to initiate cleanup.
  682. */
  683. if (!*result && qcomtee_update_args(u, oic)) {
  684. *result = QCOMTEE_MSG_ERROR_UNAVAIL;
  685. qcomtee_arg_for_each_output_object(i, u)
  686. qcomtee_object_put(u[i].o);
  687. }
  688. break;
  689. } else {
  690. oic->flags |= QCOMTEE_OIC_FLAG_BUSY;
  691. qcomtee_fetch_async_reqs(oic);
  692. qcomtee_cb_object_invoke(oic, cb_msg);
  693. }
  694. }
  695. qcomtee_fetch_async_reqs(oic);
  696. out:
  697. qcomtee_msg_buffers_free(oic);
  698. return ret;
  699. }
  700. int qcomtee_object_do_invoke(struct qcomtee_object_invoke_ctx *oic,
  701. struct qcomtee_object *object, u32 op,
  702. struct qcomtee_arg *u, int *result)
  703. {
  704. /* User can not set bits used by transport. */
  705. if (op & ~QCOMTEE_MSG_OBJECT_OP_MASK)
  706. return -EINVAL;
  707. /* User can only invoke QTEE hosted objects. */
  708. if (typeof_qcomtee_object(object) != QCOMTEE_OBJECT_TYPE_TEE &&
  709. typeof_qcomtee_object(object) != QCOMTEE_OBJECT_TYPE_ROOT)
  710. return -EINVAL;
  711. /* User cannot directly issue these operations to QTEE. */
  712. if (op == QCOMTEE_MSG_OBJECT_OP_RELEASE ||
  713. op == QCOMTEE_MSG_OBJECT_OP_RETAIN)
  714. return -EINVAL;
  715. return qcomtee_object_do_invoke_internal(oic, object, op, u, result);
  716. }
  717. /**
  718. * qcomtee_object_get_client_env() - Get a privileged client env. object.
  719. * @oic: context to use for the current invocation.
  720. *
  721. * The caller should call qcomtee_object_put() on the returned object
  722. * to release it.
  723. *
  724. * Return: On error, returns %NULL_QCOMTEE_OBJECT.
  725. * On success, returns the object.
  726. */
  727. struct qcomtee_object *
  728. qcomtee_object_get_client_env(struct qcomtee_object_invoke_ctx *oic)
  729. {
  730. struct qcomtee_arg u[3] = { 0 };
  731. int ret, result;
  732. u[0].o = NULL_QCOMTEE_OBJECT;
  733. u[0].type = QCOMTEE_ARG_TYPE_IO;
  734. u[1].type = QCOMTEE_ARG_TYPE_OO;
  735. ret = qcomtee_object_do_invoke(oic, ROOT_QCOMTEE_OBJECT,
  736. QCOMTEE_ROOT_OP_REG_WITH_CREDENTIALS, u,
  737. &result);
  738. if (ret || result)
  739. return NULL_QCOMTEE_OBJECT;
  740. return u[1].o;
  741. }
  742. struct qcomtee_object *
  743. qcomtee_object_get_service(struct qcomtee_object_invoke_ctx *oic,
  744. struct qcomtee_object *client_env, u32 uid)
  745. {
  746. struct qcomtee_arg u[3] = { 0 };
  747. int ret, result;
  748. u[0].b.addr = &uid;
  749. u[0].b.size = sizeof(uid);
  750. u[0].type = QCOMTEE_ARG_TYPE_IB;
  751. u[1].type = QCOMTEE_ARG_TYPE_OO;
  752. ret = qcomtee_object_do_invoke(oic, client_env, QCOMTEE_CLIENT_ENV_OPEN,
  753. u, &result);
  754. if (ret || result)
  755. return NULL_QCOMTEE_OBJECT;
  756. return u[1].o;
  757. }