core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright 2019 Advanced Micro Devices, Inc.
  4. */
  5. #include <linux/errno.h>
  6. #include <linux/io.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/string.h>
  10. #include <linux/device.h>
  11. #include <linux/tee_core.h>
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/firmware.h>
  16. #include "amdtee_private.h"
  17. #include <linux/psp-tee.h>
  18. static struct amdtee_driver_data *drv_data;
  19. static DEFINE_MUTEX(session_list_mutex);
  20. static void amdtee_get_version(struct tee_device *teedev,
  21. struct tee_ioctl_version_data *vers)
  22. {
  23. struct tee_ioctl_version_data v = {
  24. .impl_id = TEE_IMPL_ID_AMDTEE,
  25. .impl_caps = 0,
  26. .gen_caps = TEE_GEN_CAP_GP,
  27. };
  28. *vers = v;
  29. }
  30. static int amdtee_open(struct tee_context *ctx)
  31. {
  32. struct amdtee_context_data *ctxdata;
  33. ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
  34. if (!ctxdata)
  35. return -ENOMEM;
  36. INIT_LIST_HEAD(&ctxdata->sess_list);
  37. INIT_LIST_HEAD(&ctxdata->shm_list);
  38. mutex_init(&ctxdata->shm_mutex);
  39. ctx->data = ctxdata;
  40. return 0;
  41. }
  42. static void release_session(struct amdtee_session *sess)
  43. {
  44. int i;
  45. /* Close any open session */
  46. for (i = 0; i < TEE_NUM_SESSIONS; ++i) {
  47. /* Check if session entry 'i' is valid */
  48. if (!test_bit(i, sess->sess_mask))
  49. continue;
  50. handle_close_session(sess->ta_handle, sess->session_info[i]);
  51. handle_unload_ta(sess->ta_handle);
  52. }
  53. kfree(sess);
  54. }
  55. static void amdtee_release(struct tee_context *ctx)
  56. {
  57. struct amdtee_context_data *ctxdata = ctx->data;
  58. if (!ctxdata)
  59. return;
  60. while (true) {
  61. struct amdtee_session *sess;
  62. sess = list_first_entry_or_null(&ctxdata->sess_list,
  63. struct amdtee_session,
  64. list_node);
  65. if (!sess)
  66. break;
  67. list_del(&sess->list_node);
  68. release_session(sess);
  69. }
  70. mutex_destroy(&ctxdata->shm_mutex);
  71. kfree(ctxdata);
  72. ctx->data = NULL;
  73. }
  74. /**
  75. * alloc_session() - Allocate a session structure
  76. * @ctxdata: TEE Context data structure
  77. * @session: Session ID for which 'struct amdtee_session' structure is to be
  78. * allocated.
  79. *
  80. * Scans the TEE context's session list to check if TA is already loaded in to
  81. * TEE. If yes, returns the 'session' structure for that TA. Else allocates,
  82. * initializes a new 'session' structure and adds it to context's session list.
  83. *
  84. * The caller must hold a mutex.
  85. *
  86. * Returns:
  87. * 'struct amdtee_session *' on success and NULL on failure.
  88. */
  89. static struct amdtee_session *alloc_session(struct amdtee_context_data *ctxdata,
  90. u32 session)
  91. {
  92. struct amdtee_session *sess;
  93. u32 ta_handle = get_ta_handle(session);
  94. /* Scan session list to check if TA is already loaded in to TEE */
  95. list_for_each_entry(sess, &ctxdata->sess_list, list_node)
  96. if (sess->ta_handle == ta_handle) {
  97. kref_get(&sess->refcount);
  98. return sess;
  99. }
  100. /* Allocate a new session and add to list */
  101. sess = kzalloc(sizeof(*sess), GFP_KERNEL);
  102. if (sess) {
  103. sess->ta_handle = ta_handle;
  104. kref_init(&sess->refcount);
  105. spin_lock_init(&sess->lock);
  106. list_add(&sess->list_node, &ctxdata->sess_list);
  107. }
  108. return sess;
  109. }
  110. /* Requires mutex to be held */
  111. static struct amdtee_session *find_session(struct amdtee_context_data *ctxdata,
  112. u32 session)
  113. {
  114. u32 ta_handle = get_ta_handle(session);
  115. u32 index = get_session_index(session);
  116. struct amdtee_session *sess;
  117. if (index >= TEE_NUM_SESSIONS)
  118. return NULL;
  119. list_for_each_entry(sess, &ctxdata->sess_list, list_node)
  120. if (ta_handle == sess->ta_handle &&
  121. test_bit(index, sess->sess_mask))
  122. return sess;
  123. return NULL;
  124. }
  125. u32 get_buffer_id(struct tee_shm *shm)
  126. {
  127. struct amdtee_context_data *ctxdata = shm->ctx->data;
  128. struct amdtee_shm_data *shmdata;
  129. u32 buf_id = 0;
  130. mutex_lock(&ctxdata->shm_mutex);
  131. list_for_each_entry(shmdata, &ctxdata->shm_list, shm_node)
  132. if (shmdata->kaddr == shm->kaddr) {
  133. buf_id = shmdata->buf_id;
  134. break;
  135. }
  136. mutex_unlock(&ctxdata->shm_mutex);
  137. return buf_id;
  138. }
  139. static DEFINE_MUTEX(drv_mutex);
  140. static int copy_ta_binary(struct tee_context *ctx, void *ptr, void **ta,
  141. size_t *ta_size)
  142. {
  143. const struct firmware *fw;
  144. char fw_name[TA_PATH_MAX];
  145. struct {
  146. u32 lo;
  147. u16 mid;
  148. u16 hi_ver;
  149. u8 seq_n[8];
  150. } *uuid = ptr;
  151. int n, rc = 0;
  152. n = snprintf(fw_name, TA_PATH_MAX,
  153. "%s/%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x.bin",
  154. TA_LOAD_PATH, uuid->lo, uuid->mid, uuid->hi_ver,
  155. uuid->seq_n[0], uuid->seq_n[1],
  156. uuid->seq_n[2], uuid->seq_n[3],
  157. uuid->seq_n[4], uuid->seq_n[5],
  158. uuid->seq_n[6], uuid->seq_n[7]);
  159. if (n < 0 || n >= TA_PATH_MAX) {
  160. pr_err("failed to get firmware name\n");
  161. return -EINVAL;
  162. }
  163. mutex_lock(&drv_mutex);
  164. n = request_firmware(&fw, fw_name, &ctx->teedev->dev);
  165. if (n) {
  166. pr_err("failed to load firmware %s\n", fw_name);
  167. rc = -ENOMEM;
  168. goto unlock;
  169. }
  170. *ta_size = roundup(fw->size, PAGE_SIZE);
  171. *ta = (void *)__get_free_pages(GFP_KERNEL, get_order(*ta_size));
  172. if (!*ta) {
  173. pr_err("%s: get_free_pages failed\n", __func__);
  174. rc = -ENOMEM;
  175. goto rel_fw;
  176. }
  177. memcpy(*ta, fw->data, fw->size);
  178. rel_fw:
  179. release_firmware(fw);
  180. unlock:
  181. mutex_unlock(&drv_mutex);
  182. return rc;
  183. }
  184. /* mutex must be held by caller */
  185. static void destroy_session(struct kref *ref)
  186. {
  187. struct amdtee_session *sess = container_of(ref, struct amdtee_session,
  188. refcount);
  189. list_del(&sess->list_node);
  190. mutex_unlock(&session_list_mutex);
  191. kfree(sess);
  192. }
  193. int amdtee_open_session(struct tee_context *ctx,
  194. struct tee_ioctl_open_session_arg *arg,
  195. struct tee_param *param)
  196. {
  197. struct amdtee_context_data *ctxdata = ctx->data;
  198. struct amdtee_session *sess = NULL;
  199. u32 session_info, ta_handle;
  200. size_t ta_size;
  201. int rc, i;
  202. void *ta;
  203. if (arg->clnt_login != TEE_IOCTL_LOGIN_PUBLIC) {
  204. pr_err("unsupported client login method\n");
  205. return -EINVAL;
  206. }
  207. rc = copy_ta_binary(ctx, &arg->uuid[0], &ta, &ta_size);
  208. if (rc) {
  209. pr_err("failed to copy TA binary\n");
  210. return rc;
  211. }
  212. /* Load the TA binary into TEE environment */
  213. handle_load_ta(ta, ta_size, arg);
  214. if (arg->ret != TEEC_SUCCESS)
  215. goto out;
  216. ta_handle = get_ta_handle(arg->session);
  217. mutex_lock(&session_list_mutex);
  218. sess = alloc_session(ctxdata, arg->session);
  219. mutex_unlock(&session_list_mutex);
  220. if (!sess) {
  221. handle_unload_ta(ta_handle);
  222. rc = -ENOMEM;
  223. goto out;
  224. }
  225. /* Open session with loaded TA */
  226. handle_open_session(arg, &session_info, param);
  227. if (arg->ret != TEEC_SUCCESS) {
  228. pr_err("open_session failed %d\n", arg->ret);
  229. handle_unload_ta(ta_handle);
  230. kref_put_mutex(&sess->refcount, destroy_session,
  231. &session_list_mutex);
  232. goto out;
  233. }
  234. /* Find an empty session index for the given TA */
  235. spin_lock(&sess->lock);
  236. i = find_first_zero_bit(sess->sess_mask, TEE_NUM_SESSIONS);
  237. if (i < TEE_NUM_SESSIONS) {
  238. sess->session_info[i] = session_info;
  239. set_session_id(ta_handle, i, &arg->session);
  240. set_bit(i, sess->sess_mask);
  241. }
  242. spin_unlock(&sess->lock);
  243. if (i >= TEE_NUM_SESSIONS) {
  244. pr_err("reached maximum session count %d\n", TEE_NUM_SESSIONS);
  245. handle_close_session(ta_handle, session_info);
  246. handle_unload_ta(ta_handle);
  247. kref_put_mutex(&sess->refcount, destroy_session,
  248. &session_list_mutex);
  249. rc = -ENOMEM;
  250. goto out;
  251. }
  252. out:
  253. free_pages((u64)ta, get_order(ta_size));
  254. return rc;
  255. }
  256. int amdtee_close_session(struct tee_context *ctx, u32 session)
  257. {
  258. struct amdtee_context_data *ctxdata = ctx->data;
  259. u32 i, ta_handle, session_info;
  260. struct amdtee_session *sess;
  261. pr_debug("%s: sid = 0x%x\n", __func__, session);
  262. /*
  263. * Check that the session is valid and clear the session
  264. * usage bit
  265. */
  266. mutex_lock(&session_list_mutex);
  267. sess = find_session(ctxdata, session);
  268. if (sess) {
  269. ta_handle = get_ta_handle(session);
  270. i = get_session_index(session);
  271. session_info = sess->session_info[i];
  272. spin_lock(&sess->lock);
  273. clear_bit(i, sess->sess_mask);
  274. spin_unlock(&sess->lock);
  275. }
  276. mutex_unlock(&session_list_mutex);
  277. if (!sess)
  278. return -EINVAL;
  279. /* Close the session */
  280. handle_close_session(ta_handle, session_info);
  281. handle_unload_ta(ta_handle);
  282. kref_put_mutex(&sess->refcount, destroy_session, &session_list_mutex);
  283. return 0;
  284. }
  285. int amdtee_map_shmem(struct tee_shm *shm)
  286. {
  287. struct amdtee_context_data *ctxdata;
  288. struct amdtee_shm_data *shmnode;
  289. struct shmem_desc shmem;
  290. int rc, count;
  291. u32 buf_id;
  292. if (!shm)
  293. return -EINVAL;
  294. shmnode = kmalloc(sizeof(*shmnode), GFP_KERNEL);
  295. if (!shmnode)
  296. return -ENOMEM;
  297. count = 1;
  298. shmem.kaddr = shm->kaddr;
  299. shmem.size = shm->size;
  300. /*
  301. * Send a MAP command to TEE and get the corresponding
  302. * buffer Id
  303. */
  304. rc = handle_map_shmem(count, &shmem, &buf_id);
  305. if (rc) {
  306. pr_err("map_shmem failed: ret = %d\n", rc);
  307. kfree(shmnode);
  308. return rc;
  309. }
  310. shmnode->kaddr = shm->kaddr;
  311. shmnode->buf_id = buf_id;
  312. ctxdata = shm->ctx->data;
  313. mutex_lock(&ctxdata->shm_mutex);
  314. list_add(&shmnode->shm_node, &ctxdata->shm_list);
  315. mutex_unlock(&ctxdata->shm_mutex);
  316. pr_debug("buf_id :[%x] kaddr[%p]\n", shmnode->buf_id, shmnode->kaddr);
  317. return 0;
  318. }
  319. void amdtee_unmap_shmem(struct tee_shm *shm)
  320. {
  321. struct amdtee_context_data *ctxdata;
  322. struct amdtee_shm_data *shmnode;
  323. u32 buf_id;
  324. if (!shm)
  325. return;
  326. buf_id = get_buffer_id(shm);
  327. /* Unmap the shared memory from TEE */
  328. handle_unmap_shmem(buf_id);
  329. ctxdata = shm->ctx->data;
  330. mutex_lock(&ctxdata->shm_mutex);
  331. list_for_each_entry(shmnode, &ctxdata->shm_list, shm_node)
  332. if (buf_id == shmnode->buf_id) {
  333. list_del(&shmnode->shm_node);
  334. kfree(shmnode);
  335. break;
  336. }
  337. mutex_unlock(&ctxdata->shm_mutex);
  338. }
  339. int amdtee_invoke_func(struct tee_context *ctx,
  340. struct tee_ioctl_invoke_arg *arg,
  341. struct tee_param *param)
  342. {
  343. struct amdtee_context_data *ctxdata = ctx->data;
  344. struct amdtee_session *sess;
  345. u32 i, session_info;
  346. /* Check that the session is valid */
  347. mutex_lock(&session_list_mutex);
  348. sess = find_session(ctxdata, arg->session);
  349. if (sess) {
  350. i = get_session_index(arg->session);
  351. session_info = sess->session_info[i];
  352. }
  353. mutex_unlock(&session_list_mutex);
  354. if (!sess)
  355. return -EINVAL;
  356. handle_invoke_cmd(arg, session_info, param);
  357. return 0;
  358. }
  359. int amdtee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
  360. {
  361. return -EINVAL;
  362. }
  363. static const struct tee_driver_ops amdtee_ops = {
  364. .get_version = amdtee_get_version,
  365. .open = amdtee_open,
  366. .release = amdtee_release,
  367. .open_session = amdtee_open_session,
  368. .close_session = amdtee_close_session,
  369. .invoke_func = amdtee_invoke_func,
  370. .cancel_req = amdtee_cancel_req,
  371. };
  372. static const struct tee_desc amdtee_desc = {
  373. .name = DRIVER_NAME "-clnt",
  374. .ops = &amdtee_ops,
  375. .owner = THIS_MODULE,
  376. };
  377. static int __init amdtee_driver_init(void)
  378. {
  379. struct tee_device *teedev;
  380. struct tee_shm_pool *pool;
  381. struct amdtee *amdtee;
  382. int rc;
  383. rc = psp_check_tee_status();
  384. if (rc) {
  385. pr_err("amd-tee driver: tee not present\n");
  386. return rc;
  387. }
  388. drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
  389. if (!drv_data)
  390. return -ENOMEM;
  391. amdtee = kzalloc(sizeof(*amdtee), GFP_KERNEL);
  392. if (!amdtee) {
  393. rc = -ENOMEM;
  394. goto err_kfree_drv_data;
  395. }
  396. pool = amdtee_config_shm();
  397. if (IS_ERR(pool)) {
  398. pr_err("shared pool configuration error\n");
  399. rc = PTR_ERR(pool);
  400. goto err_kfree_amdtee;
  401. }
  402. teedev = tee_device_alloc(&amdtee_desc, NULL, pool, amdtee);
  403. if (IS_ERR(teedev)) {
  404. rc = PTR_ERR(teedev);
  405. goto err_free_pool;
  406. }
  407. amdtee->teedev = teedev;
  408. rc = tee_device_register(amdtee->teedev);
  409. if (rc)
  410. goto err_device_unregister;
  411. amdtee->pool = pool;
  412. drv_data->amdtee = amdtee;
  413. pr_info("amd-tee driver initialization successful\n");
  414. return 0;
  415. err_device_unregister:
  416. tee_device_unregister(amdtee->teedev);
  417. err_free_pool:
  418. tee_shm_pool_free(pool);
  419. err_kfree_amdtee:
  420. kfree(amdtee);
  421. err_kfree_drv_data:
  422. kfree(drv_data);
  423. drv_data = NULL;
  424. pr_err("amd-tee driver initialization failed\n");
  425. return rc;
  426. }
  427. module_init(amdtee_driver_init);
  428. static void __exit amdtee_driver_exit(void)
  429. {
  430. struct amdtee *amdtee;
  431. if (!drv_data || !drv_data->amdtee)
  432. return;
  433. amdtee = drv_data->amdtee;
  434. tee_device_unregister(amdtee->teedev);
  435. tee_shm_pool_free(amdtee->pool);
  436. }
  437. module_exit(amdtee_driver_exit);
  438. MODULE_AUTHOR(DRIVER_AUTHOR);
  439. MODULE_DESCRIPTION("AMD-TEE driver");
  440. MODULE_VERSION("1.0");
  441. MODULE_LICENSE("Dual MIT/GPL");