tee-if.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AMD Platform Management Framework Driver - TEE Interface
  4. *
  5. * Copyright (c) 2023, Advanced Micro Devices, Inc.
  6. * All Rights Reserved.
  7. *
  8. * Author: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
  9. */
  10. #include <linux/debugfs.h>
  11. #include <linux/tee_drv.h>
  12. #include <linux/uuid.h>
  13. #include "pmf.h"
  14. #define MAX_TEE_PARAM 4
  15. /* Policy binary actions sampling frequency (in ms) */
  16. static int pb_actions_ms = MSEC_PER_SEC;
  17. /* Sideload policy binaries to debug policy failures */
  18. static bool pb_side_load;
  19. #ifdef CONFIG_AMD_PMF_DEBUG
  20. module_param(pb_actions_ms, int, 0644);
  21. MODULE_PARM_DESC(pb_actions_ms, "Policy binary actions sampling frequency (default = 1000ms)");
  22. module_param(pb_side_load, bool, 0444);
  23. MODULE_PARM_DESC(pb_side_load, "Sideload policy binaries debug policy failures");
  24. #endif
  25. static const uuid_t amd_pmf_ta_uuid[] = { UUID_INIT(0xd9b39bf2, 0x66bd, 0x4154, 0xaf, 0xb8, 0x8a,
  26. 0xcc, 0x2b, 0x2b, 0x60, 0xd6),
  27. UUID_INIT(0x6fd93b77, 0x3fb8, 0x524d, 0xb1, 0x2d, 0xc5,
  28. 0x29, 0xb1, 0x3d, 0x85, 0x43),
  29. };
  30. static const char *amd_pmf_uevent_as_str(unsigned int state)
  31. {
  32. switch (state) {
  33. case SYSTEM_STATE_S0i3:
  34. return "S0i3";
  35. case SYSTEM_STATE_S4:
  36. return "S4";
  37. case SYSTEM_STATE_SCREEN_LOCK:
  38. return "SCREEN_LOCK";
  39. default:
  40. return "Unknown Smart PC event";
  41. }
  42. }
  43. static void amd_pmf_prepare_args(struct amd_pmf_dev *dev, int cmd,
  44. struct tee_ioctl_invoke_arg *arg,
  45. struct tee_param *param)
  46. {
  47. memset(arg, 0, sizeof(*arg));
  48. memset(param, 0, MAX_TEE_PARAM * sizeof(*param));
  49. arg->func = cmd;
  50. arg->session = dev->session_id;
  51. arg->num_params = MAX_TEE_PARAM;
  52. /* Fill invoke cmd params */
  53. param[0].u.memref.size = sizeof(struct ta_pmf_shared_memory);
  54. param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
  55. param[0].u.memref.shm = dev->fw_shm_pool;
  56. param[0].u.memref.shm_offs = 0;
  57. }
  58. static void amd_pmf_update_uevents(struct amd_pmf_dev *dev, u16 event)
  59. {
  60. input_report_key(dev->pmf_idev, event, 1); /* key press */
  61. input_sync(dev->pmf_idev);
  62. input_report_key(dev->pmf_idev, event, 0); /* key release */
  63. input_sync(dev->pmf_idev);
  64. }
  65. static void amd_pmf_apply_policies(struct amd_pmf_dev *dev, struct ta_pmf_enact_result *out)
  66. {
  67. u32 val;
  68. int idx;
  69. for (idx = 0; idx < out->actions_count; idx++) {
  70. val = out->actions_list[idx].value;
  71. switch (out->actions_list[idx].action_index) {
  72. case PMF_POLICY_SPL:
  73. if (dev->prev_data->spl != val) {
  74. amd_pmf_send_cmd(dev, SET_SPL, false, val, NULL);
  75. dev_dbg(dev->dev, "update SPL: %u\n", val);
  76. dev->prev_data->spl = val;
  77. }
  78. break;
  79. case PMF_POLICY_SPPT:
  80. if (dev->prev_data->sppt != val) {
  81. amd_pmf_send_cmd(dev, SET_SPPT, false, val, NULL);
  82. dev_dbg(dev->dev, "update SPPT: %u\n", val);
  83. dev->prev_data->sppt = val;
  84. }
  85. break;
  86. case PMF_POLICY_FPPT:
  87. if (dev->prev_data->fppt != val) {
  88. amd_pmf_send_cmd(dev, SET_FPPT, false, val, NULL);
  89. dev_dbg(dev->dev, "update FPPT: %u\n", val);
  90. dev->prev_data->fppt = val;
  91. }
  92. break;
  93. case PMF_POLICY_SPPT_APU_ONLY:
  94. if (dev->prev_data->sppt_apuonly != val) {
  95. amd_pmf_send_cmd(dev, SET_SPPT_APU_ONLY, false, val, NULL);
  96. dev_dbg(dev->dev, "update SPPT_APU_ONLY: %u\n", val);
  97. dev->prev_data->sppt_apuonly = val;
  98. }
  99. break;
  100. case PMF_POLICY_STT_MIN:
  101. if (dev->prev_data->stt_minlimit != val) {
  102. amd_pmf_send_cmd(dev, SET_STT_MIN_LIMIT, false, val, NULL);
  103. dev_dbg(dev->dev, "update STT_MIN: %u\n", val);
  104. dev->prev_data->stt_minlimit = val;
  105. }
  106. break;
  107. case PMF_POLICY_STT_SKINTEMP_APU:
  108. if (dev->prev_data->stt_skintemp_apu != val) {
  109. amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false,
  110. fixp_q88_fromint(val), NULL);
  111. dev_dbg(dev->dev, "update STT_SKINTEMP_APU: %u\n", val);
  112. dev->prev_data->stt_skintemp_apu = val;
  113. }
  114. break;
  115. case PMF_POLICY_STT_SKINTEMP_HS2:
  116. if (dev->prev_data->stt_skintemp_hs2 != val) {
  117. amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false,
  118. fixp_q88_fromint(val), NULL);
  119. dev_dbg(dev->dev, "update STT_SKINTEMP_HS2: %u\n", val);
  120. dev->prev_data->stt_skintemp_hs2 = val;
  121. }
  122. break;
  123. case PMF_POLICY_P3T:
  124. if (dev->prev_data->p3t_limit != val) {
  125. amd_pmf_send_cmd(dev, SET_P3T, false, val, NULL);
  126. dev_dbg(dev->dev, "update P3T: %u\n", val);
  127. dev->prev_data->p3t_limit = val;
  128. }
  129. break;
  130. case PMF_POLICY_SYSTEM_STATE:
  131. switch (val) {
  132. case 0:
  133. amd_pmf_update_uevents(dev, KEY_SLEEP);
  134. break;
  135. case 1:
  136. amd_pmf_update_uevents(dev, KEY_SUSPEND);
  137. break;
  138. case 2:
  139. amd_pmf_update_uevents(dev, KEY_SCREENLOCK);
  140. break;
  141. default:
  142. dev_err(dev->dev, "Invalid PMF policy system state: %d\n", val);
  143. }
  144. dev_dbg(dev->dev, "update SYSTEM_STATE: %s\n",
  145. amd_pmf_uevent_as_str(val));
  146. break;
  147. case PMF_POLICY_BIOS_OUTPUT_1:
  148. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(0), 0);
  149. break;
  150. case PMF_POLICY_BIOS_OUTPUT_2:
  151. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(1), 1);
  152. break;
  153. case PMF_POLICY_BIOS_OUTPUT_3:
  154. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(2), 2);
  155. break;
  156. case PMF_POLICY_BIOS_OUTPUT_4:
  157. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(3), 3);
  158. break;
  159. case PMF_POLICY_BIOS_OUTPUT_5:
  160. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(4), 4);
  161. break;
  162. case PMF_POLICY_BIOS_OUTPUT_6:
  163. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(5), 5);
  164. break;
  165. case PMF_POLICY_BIOS_OUTPUT_7:
  166. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(6), 6);
  167. break;
  168. case PMF_POLICY_BIOS_OUTPUT_8:
  169. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(7), 7);
  170. break;
  171. case PMF_POLICY_BIOS_OUTPUT_9:
  172. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(8), 8);
  173. break;
  174. case PMF_POLICY_BIOS_OUTPUT_10:
  175. amd_pmf_smartpc_apply_bios_output(dev, val, BIT(9), 9);
  176. break;
  177. }
  178. }
  179. }
  180. static int amd_pmf_invoke_cmd_enact(struct amd_pmf_dev *dev)
  181. {
  182. struct ta_pmf_shared_memory *ta_sm = NULL;
  183. struct ta_pmf_enact_result *out = NULL;
  184. struct ta_pmf_enact_table *in = NULL;
  185. struct tee_param param[MAX_TEE_PARAM];
  186. struct tee_ioctl_invoke_arg arg;
  187. int ret = 0;
  188. if (!dev->tee_ctx)
  189. return -ENODEV;
  190. memset(dev->shbuf, 0, dev->policy_sz);
  191. ta_sm = dev->shbuf;
  192. out = &ta_sm->pmf_output.policy_apply_table;
  193. in = &ta_sm->pmf_input.enact_table;
  194. memset(ta_sm, 0, sizeof(*ta_sm));
  195. ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES;
  196. ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR;
  197. amd_pmf_populate_ta_inputs(dev, in);
  198. amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES, &arg, param);
  199. ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
  200. if (ret < 0 || arg.ret != 0) {
  201. dev_err(dev->dev, "TEE enact cmd failed. err: %x, ret:%d\n", arg.ret, ret);
  202. return ret;
  203. }
  204. if (ta_sm->pmf_result == TA_PMF_TYPE_SUCCESS && out->actions_count) {
  205. amd_pmf_dump_ta_inputs(dev, in);
  206. dev_dbg(dev->dev, "action count:%u result:%x\n", out->actions_count,
  207. ta_sm->pmf_result);
  208. amd_pmf_apply_policies(dev, out);
  209. }
  210. return 0;
  211. }
  212. static int amd_pmf_invoke_cmd_init(struct amd_pmf_dev *dev)
  213. {
  214. struct ta_pmf_shared_memory *ta_sm = NULL;
  215. struct tee_param param[MAX_TEE_PARAM];
  216. struct ta_pmf_init_table *in = NULL;
  217. struct tee_ioctl_invoke_arg arg;
  218. int ret = 0;
  219. if (!dev->tee_ctx) {
  220. dev_err(dev->dev, "Failed to get TEE context\n");
  221. return -ENODEV;
  222. }
  223. dev_dbg(dev->dev, "Policy Binary size: %u bytes\n", dev->policy_sz);
  224. memset(dev->shbuf, 0, dev->policy_sz);
  225. ta_sm = dev->shbuf;
  226. in = &ta_sm->pmf_input.init_table;
  227. ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE;
  228. ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR;
  229. in->metadata_macrocheck = false;
  230. in->sku_check = false;
  231. in->validate = true;
  232. in->frequency = pb_actions_ms;
  233. in->policies_table.table_size = dev->policy_sz;
  234. memcpy(in->policies_table.table, dev->policy_buf, dev->policy_sz);
  235. amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE, &arg, param);
  236. ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
  237. if (ret < 0 || arg.ret != 0) {
  238. dev_err(dev->dev, "Failed to invoke TEE init cmd. err: %x, ret:%d\n", arg.ret, ret);
  239. return ret;
  240. }
  241. return ta_sm->pmf_result;
  242. }
  243. static void amd_pmf_invoke_cmd(struct work_struct *work)
  244. {
  245. struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, pb_work.work);
  246. amd_pmf_invoke_cmd_enact(dev);
  247. schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms));
  248. }
  249. static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)
  250. {
  251. struct cookie_header *header;
  252. int res;
  253. if (dev->policy_sz < POLICY_COOKIE_OFFSET + sizeof(*header))
  254. return -EINVAL;
  255. header = (struct cookie_header *)(dev->policy_buf + POLICY_COOKIE_OFFSET);
  256. if (header->sign != POLICY_SIGN_COOKIE || !header->length) {
  257. dev_dbg(dev->dev, "cookie doesn't match\n");
  258. return -EINVAL;
  259. }
  260. if (dev->policy_sz < header->length + 512)
  261. return -EINVAL;
  262. /* Update the actual length */
  263. dev->policy_sz = header->length + 512;
  264. res = amd_pmf_invoke_cmd_init(dev);
  265. if (res == TA_PMF_TYPE_SUCCESS) {
  266. /* Now its safe to announce that smart pc is enabled */
  267. dev->smart_pc_enabled = true;
  268. /*
  269. * Start collecting the data from TA FW after a small delay
  270. * or else, we might end up getting stale values.
  271. */
  272. schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms * 3));
  273. } else {
  274. dev_dbg(dev->dev, "ta invoke cmd init failed err: %x\n", res);
  275. dev->smart_pc_enabled = false;
  276. return res;
  277. }
  278. return 0;
  279. }
  280. static inline bool amd_pmf_pb_valid(struct amd_pmf_dev *dev)
  281. {
  282. return memchr_inv(dev->policy_buf, 0xff, dev->policy_sz);
  283. }
  284. #ifdef CONFIG_AMD_PMF_DEBUG
  285. static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev)
  286. {
  287. print_hex_dump_debug("(pb): ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf,
  288. dev->policy_sz, false);
  289. }
  290. static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf,
  291. size_t length, loff_t *pos)
  292. {
  293. struct amd_pmf_dev *dev = filp->private_data;
  294. unsigned char *new_policy_buf;
  295. int ret;
  296. /* Policy binary size cannot exceed POLICY_BUF_MAX_SZ */
  297. if (length > POLICY_BUF_MAX_SZ || length == 0)
  298. return -EINVAL;
  299. /* re-alloc to the new buffer length of the policy binary */
  300. new_policy_buf = memdup_user(buf, length);
  301. if (IS_ERR(new_policy_buf))
  302. return PTR_ERR(new_policy_buf);
  303. kfree(dev->policy_buf);
  304. dev->policy_buf = new_policy_buf;
  305. dev->policy_sz = length;
  306. if (!amd_pmf_pb_valid(dev)) {
  307. ret = -EINVAL;
  308. goto cleanup;
  309. }
  310. amd_pmf_hex_dump_pb(dev);
  311. ret = amd_pmf_start_policy_engine(dev);
  312. if (ret < 0)
  313. goto cleanup;
  314. return length;
  315. cleanup:
  316. kfree(dev->policy_buf);
  317. dev->policy_buf = NULL;
  318. return ret;
  319. }
  320. static const struct file_operations pb_fops = {
  321. .write = amd_pmf_get_pb_data,
  322. .open = simple_open,
  323. };
  324. static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root)
  325. {
  326. dev->esbin = debugfs_create_dir("pb", debugfs_root);
  327. debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops);
  328. }
  329. static void amd_pmf_remove_pb(struct amd_pmf_dev *dev)
  330. {
  331. debugfs_remove_recursive(dev->esbin);
  332. }
  333. #else
  334. static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) {}
  335. static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {}
  336. static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {}
  337. #endif
  338. static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data)
  339. {
  340. return ver->impl_id == TEE_IMPL_ID_AMDTEE;
  341. }
  342. static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id, const uuid_t *uuid)
  343. {
  344. struct tee_ioctl_open_session_arg sess_arg = {};
  345. int rc;
  346. export_uuid(sess_arg.uuid, uuid);
  347. sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
  348. sess_arg.num_params = 0;
  349. rc = tee_client_open_session(ctx, &sess_arg, NULL);
  350. if (rc < 0 || sess_arg.ret != 0) {
  351. pr_err("Failed to open TEE session err:%#x, rc:%d\n", sess_arg.ret, rc);
  352. return rc ?: -EINVAL;
  353. }
  354. *id = sess_arg.session;
  355. return 0;
  356. }
  357. static int amd_pmf_register_input_device(struct amd_pmf_dev *dev)
  358. {
  359. int err;
  360. dev->pmf_idev = devm_input_allocate_device(dev->dev);
  361. if (!dev->pmf_idev)
  362. return -ENOMEM;
  363. dev->pmf_idev->name = "PMF-TA output events";
  364. dev->pmf_idev->phys = "amd-pmf/input0";
  365. input_set_capability(dev->pmf_idev, EV_KEY, KEY_SLEEP);
  366. input_set_capability(dev->pmf_idev, EV_KEY, KEY_SCREENLOCK);
  367. input_set_capability(dev->pmf_idev, EV_KEY, KEY_SUSPEND);
  368. err = input_register_device(dev->pmf_idev);
  369. if (err) {
  370. dev_err(dev->dev, "Failed to register input device: %d\n", err);
  371. return err;
  372. }
  373. return 0;
  374. }
  375. static int amd_pmf_tee_init(struct amd_pmf_dev *dev, const uuid_t *uuid)
  376. {
  377. u32 size;
  378. int ret;
  379. dev->tee_ctx = tee_client_open_context(NULL, amd_pmf_amdtee_ta_match, NULL, NULL);
  380. if (IS_ERR(dev->tee_ctx)) {
  381. dev_err(dev->dev, "Failed to open TEE context\n");
  382. ret = PTR_ERR(dev->tee_ctx);
  383. dev->tee_ctx = NULL;
  384. return ret;
  385. }
  386. ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id, uuid);
  387. if (ret) {
  388. dev_err(dev->dev, "Failed to open TA session (%d)\n", ret);
  389. ret = -EINVAL;
  390. goto out_ctx;
  391. }
  392. size = sizeof(struct ta_pmf_shared_memory) + dev->policy_sz;
  393. dev->fw_shm_pool = tee_shm_alloc_kernel_buf(dev->tee_ctx, size);
  394. if (IS_ERR(dev->fw_shm_pool)) {
  395. dev_err(dev->dev, "Failed to alloc TEE shared memory\n");
  396. ret = PTR_ERR(dev->fw_shm_pool);
  397. goto out_sess;
  398. }
  399. dev->shbuf = tee_shm_get_va(dev->fw_shm_pool, 0);
  400. if (IS_ERR(dev->shbuf)) {
  401. dev_err(dev->dev, "Failed to get TEE virtual address\n");
  402. ret = PTR_ERR(dev->shbuf);
  403. goto out_shm;
  404. }
  405. dev_dbg(dev->dev, "TEE init done\n");
  406. return 0;
  407. out_shm:
  408. tee_shm_free(dev->fw_shm_pool);
  409. out_sess:
  410. tee_client_close_session(dev->tee_ctx, dev->session_id);
  411. out_ctx:
  412. tee_client_close_context(dev->tee_ctx);
  413. return ret;
  414. }
  415. static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev)
  416. {
  417. if (!dev->tee_ctx)
  418. return;
  419. tee_shm_free(dev->fw_shm_pool);
  420. tee_client_close_session(dev->tee_ctx, dev->session_id);
  421. tee_client_close_context(dev->tee_ctx);
  422. dev->tee_ctx = NULL;
  423. }
  424. int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
  425. {
  426. bool status;
  427. int ret, i;
  428. ret = apmf_check_smart_pc(dev);
  429. if (ret) {
  430. /*
  431. * Lets not return from here if Smart PC bit is not advertised in
  432. * the BIOS. This way, there will be some amount of power savings
  433. * to the user with static slider (if enabled).
  434. */
  435. dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret);
  436. return -ENODEV;
  437. }
  438. INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd);
  439. ret = amd_pmf_set_dram_addr(dev, true);
  440. if (ret)
  441. goto err_cancel_work;
  442. dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz);
  443. if (!dev->policy_base) {
  444. ret = -ENOMEM;
  445. goto err_free_dram_buf;
  446. }
  447. dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL);
  448. if (!dev->policy_buf) {
  449. ret = -ENOMEM;
  450. goto err_free_dram_buf;
  451. }
  452. memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz);
  453. if (!amd_pmf_pb_valid(dev)) {
  454. dev_info(dev->dev, "No Smart PC policy present\n");
  455. ret = -EINVAL;
  456. goto err_free_policy;
  457. }
  458. amd_pmf_hex_dump_pb(dev);
  459. dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL);
  460. if (!dev->prev_data) {
  461. ret = -ENOMEM;
  462. goto err_free_policy;
  463. }
  464. for (i = 0; i < ARRAY_SIZE(amd_pmf_ta_uuid); i++) {
  465. ret = amd_pmf_tee_init(dev, &amd_pmf_ta_uuid[i]);
  466. if (ret)
  467. goto err_free_prev_data;
  468. ret = amd_pmf_start_policy_engine(dev);
  469. switch (ret) {
  470. case TA_PMF_TYPE_SUCCESS:
  471. status = true;
  472. break;
  473. case TA_ERROR_CRYPTO_INVALID_PARAM:
  474. case TA_ERROR_CRYPTO_BIN_TOO_LARGE:
  475. amd_pmf_tee_deinit(dev);
  476. status = false;
  477. break;
  478. default:
  479. ret = -EINVAL;
  480. amd_pmf_tee_deinit(dev);
  481. goto err_free_prev_data;
  482. }
  483. if (status)
  484. break;
  485. }
  486. if (!status && !pb_side_load) {
  487. ret = -EINVAL;
  488. goto err_free_prev_data;
  489. }
  490. if (pb_side_load)
  491. amd_pmf_open_pb(dev, dev->dbgfs_dir);
  492. ret = amd_pmf_register_input_device(dev);
  493. if (ret)
  494. goto err_pmf_remove_pb;
  495. return 0;
  496. err_pmf_remove_pb:
  497. if (pb_side_load && dev->esbin)
  498. amd_pmf_remove_pb(dev);
  499. amd_pmf_tee_deinit(dev);
  500. err_free_prev_data:
  501. kfree(dev->prev_data);
  502. err_free_policy:
  503. kfree(dev->policy_buf);
  504. err_free_dram_buf:
  505. kfree(dev->buf);
  506. err_cancel_work:
  507. cancel_delayed_work_sync(&dev->pb_work);
  508. return ret;
  509. }
  510. void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
  511. {
  512. if (dev->pmf_idev)
  513. input_unregister_device(dev->pmf_idev);
  514. if (pb_side_load && dev->esbin)
  515. amd_pmf_remove_pb(dev);
  516. cancel_delayed_work_sync(&dev->pb_work);
  517. kfree(dev->prev_data);
  518. dev->prev_data = NULL;
  519. kfree(dev->policy_buf);
  520. dev->policy_buf = NULL;
  521. kfree(dev->buf);
  522. dev->buf = NULL;
  523. amd_pmf_tee_deinit(dev);
  524. }