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