xref: /linux/drivers/platform/x86/amd/pmf/tee-if.c (revision 6093a688a07da07808f0122f9aa2a3eed250d853)
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 
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 
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 
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 
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,
127 						 fixp_q88_fromint(val), NULL);
128 				dev_dbg(dev->dev, "update STT_SKINTEMP_APU: %u\n", val);
129 				dev->prev_data->stt_skintemp_apu = val;
130 			}
131 			break;
132 
133 		case PMF_POLICY_STT_SKINTEMP_HS2:
134 			if (dev->prev_data->stt_skintemp_hs2 != val) {
135 				amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false,
136 						 fixp_q88_fromint(val), NULL);
137 				dev_dbg(dev->dev, "update STT_SKINTEMP_HS2: %u\n", val);
138 				dev->prev_data->stt_skintemp_hs2 = val;
139 			}
140 			break;
141 
142 		case PMF_POLICY_P3T:
143 			if (dev->prev_data->p3t_limit != val) {
144 				amd_pmf_send_cmd(dev, SET_P3T, false, val, NULL);
145 				dev_dbg(dev->dev, "update P3T: %u\n", val);
146 				dev->prev_data->p3t_limit = val;
147 			}
148 			break;
149 
150 		case PMF_POLICY_PMF_PPT:
151 			if (dev->prev_data->pmf_ppt != val) {
152 				amd_pmf_send_cmd(dev, SET_PMF_PPT, false, val, NULL);
153 				dev_dbg(dev->dev, "update PMF PPT: %u\n", val);
154 				dev->prev_data->pmf_ppt = val;
155 			}
156 			break;
157 
158 		case PMF_POLICY_PMF_PPT_APU_ONLY:
159 			if (dev->prev_data->pmf_ppt_apu_only != val) {
160 				amd_pmf_send_cmd(dev, SET_PMF_PPT_APU_ONLY, false, val, NULL);
161 				dev_dbg(dev->dev, "update PMF PPT APU ONLY: %u\n", val);
162 				dev->prev_data->pmf_ppt_apu_only = val;
163 			}
164 			break;
165 
166 		case PMF_POLICY_SYSTEM_STATE:
167 			switch (val) {
168 			case 0:
169 				amd_pmf_update_uevents(dev, KEY_SLEEP);
170 				break;
171 			case 1:
172 				amd_pmf_update_uevents(dev, KEY_SUSPEND);
173 				break;
174 			case 2:
175 				amd_pmf_update_uevents(dev, KEY_SCREENLOCK);
176 				break;
177 			default:
178 				dev_err(dev->dev, "Invalid PMF policy system state: %d\n", val);
179 			}
180 
181 			dev_dbg(dev->dev, "update SYSTEM_STATE: %s\n",
182 				amd_pmf_uevent_as_str(val));
183 			break;
184 
185 		case PMF_POLICY_BIOS_OUTPUT_1:
186 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(0), 0);
187 			break;
188 
189 		case PMF_POLICY_BIOS_OUTPUT_2:
190 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(1), 1);
191 			break;
192 
193 		case PMF_POLICY_BIOS_OUTPUT_3:
194 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(2), 2);
195 			break;
196 
197 		case PMF_POLICY_BIOS_OUTPUT_4:
198 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(3), 3);
199 			break;
200 
201 		case PMF_POLICY_BIOS_OUTPUT_5:
202 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(4), 4);
203 			break;
204 
205 		case PMF_POLICY_BIOS_OUTPUT_6:
206 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(5), 5);
207 			break;
208 
209 		case PMF_POLICY_BIOS_OUTPUT_7:
210 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(6), 6);
211 			break;
212 
213 		case PMF_POLICY_BIOS_OUTPUT_8:
214 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(7), 7);
215 			break;
216 
217 		case PMF_POLICY_BIOS_OUTPUT_9:
218 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(8), 8);
219 			break;
220 
221 		case PMF_POLICY_BIOS_OUTPUT_10:
222 			amd_pmf_smartpc_apply_bios_output(dev, val, BIT(9), 9);
223 			break;
224 		}
225 	}
226 }
227 
228 int amd_pmf_invoke_cmd_enact(struct amd_pmf_dev *dev)
229 {
230 	struct ta_pmf_shared_memory *ta_sm = NULL;
231 	struct ta_pmf_enact_result *out = NULL;
232 	struct ta_pmf_enact_table *in = NULL;
233 	struct tee_param param[MAX_TEE_PARAM];
234 	struct tee_ioctl_invoke_arg arg;
235 	int ret = 0;
236 
237 	if (!dev->tee_ctx)
238 		return -ENODEV;
239 
240 	memset(dev->shbuf, 0, dev->policy_sz);
241 	ta_sm = dev->shbuf;
242 	out = &ta_sm->pmf_output.policy_apply_table;
243 	in = &ta_sm->pmf_input.enact_table;
244 
245 	memset(ta_sm, 0, sizeof(*ta_sm));
246 	ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES;
247 	ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR;
248 
249 	amd_pmf_populate_ta_inputs(dev, in);
250 	amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES, &arg, param);
251 
252 	ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
253 	if (ret < 0 || arg.ret != 0) {
254 		dev_err(dev->dev, "TEE enact cmd failed. err: %x, ret:%d\n", arg.ret, ret);
255 		return ret;
256 	}
257 
258 	if (ta_sm->pmf_result == TA_PMF_TYPE_SUCCESS && out->actions_count) {
259 		amd_pmf_dump_ta_inputs(dev, in);
260 		dev_dbg(dev->dev, "action count:%u result:%x\n", out->actions_count,
261 			ta_sm->pmf_result);
262 		amd_pmf_apply_policies(dev, out);
263 	}
264 
265 	return 0;
266 }
267 
268 static int amd_pmf_invoke_cmd_init(struct amd_pmf_dev *dev)
269 {
270 	struct ta_pmf_shared_memory *ta_sm = NULL;
271 	struct tee_param param[MAX_TEE_PARAM];
272 	struct ta_pmf_init_table *in = NULL;
273 	struct tee_ioctl_invoke_arg arg;
274 	int ret = 0;
275 
276 	if (!dev->tee_ctx) {
277 		dev_err(dev->dev, "Failed to get TEE context\n");
278 		return -ENODEV;
279 	}
280 
281 	dev_dbg(dev->dev, "Policy Binary size: %llu bytes\n", (unsigned long long)dev->policy_sz);
282 	memset(dev->shbuf, 0, dev->policy_sz);
283 	ta_sm = dev->shbuf;
284 	in = &ta_sm->pmf_input.init_table;
285 
286 	ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE;
287 	ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR;
288 
289 	in->metadata_macrocheck = false;
290 	in->sku_check = false;
291 	in->validate = true;
292 	in->frequency = pb_actions_ms;
293 	in->policies_table.table_size = dev->policy_sz;
294 
295 	memcpy(in->policies_table.table, dev->policy_buf, dev->policy_sz);
296 	amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE, &arg, param);
297 
298 	ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
299 	if (ret < 0 || arg.ret != 0) {
300 		dev_err(dev->dev, "Failed to invoke TEE init cmd. err: %x, ret:%d\n", arg.ret, ret);
301 		return ret;
302 	}
303 
304 	return ta_sm->pmf_result;
305 }
306 
307 static void amd_pmf_invoke_cmd(struct work_struct *work)
308 {
309 	struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, pb_work.work);
310 
311 	amd_pmf_invoke_cmd_enact(dev);
312 	schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms));
313 }
314 
315 static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)
316 {
317 	struct cookie_header *header;
318 	int res;
319 
320 	if (dev->policy_sz < POLICY_COOKIE_OFFSET + sizeof(*header))
321 		return -EINVAL;
322 
323 	header = (struct cookie_header *)(dev->policy_buf + POLICY_COOKIE_OFFSET);
324 
325 	if (header->sign != POLICY_SIGN_COOKIE || !header->length) {
326 		dev_dbg(dev->dev, "cookie doesn't match\n");
327 		return -EINVAL;
328 	}
329 
330 	if (dev->policy_sz < header->length + 512)
331 		return -EINVAL;
332 
333 	/* Update the actual length */
334 	dev->policy_sz = header->length + 512;
335 	res = amd_pmf_invoke_cmd_init(dev);
336 	if (res == TA_PMF_TYPE_SUCCESS) {
337 		/* Now its safe to announce that smart pc is enabled */
338 		dev->smart_pc_enabled = true;
339 		/*
340 		 * Start collecting the data from TA FW after a small delay
341 		 * or else, we might end up getting stale values.
342 		 */
343 		schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms * 3));
344 	} else {
345 		dev_dbg(dev->dev, "ta invoke cmd init failed err: %x\n", res);
346 		dev->smart_pc_enabled = false;
347 		return res;
348 	}
349 
350 	return 0;
351 }
352 
353 static inline bool amd_pmf_pb_valid(struct amd_pmf_dev *dev)
354 {
355 	return memchr_inv(dev->policy_buf, 0xff, dev->policy_sz);
356 }
357 
358 #ifdef CONFIG_AMD_PMF_DEBUG
359 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev)
360 {
361 	print_hex_dump_debug("(pb):  ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf,
362 			     dev->policy_sz, false);
363 }
364 
365 static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf,
366 				   size_t length, loff_t *pos)
367 {
368 	struct amd_pmf_dev *dev = filp->private_data;
369 	unsigned char *new_policy_buf;
370 	int ret;
371 
372 	/* Policy binary size cannot exceed POLICY_BUF_MAX_SZ */
373 	if (length > POLICY_BUF_MAX_SZ || length == 0)
374 		return -EINVAL;
375 
376 	/* re-alloc to the new buffer length of the policy binary */
377 	new_policy_buf = devm_kzalloc(dev->dev, length, GFP_KERNEL);
378 	if (!new_policy_buf)
379 		return -ENOMEM;
380 
381 	if (copy_from_user(new_policy_buf, buf, length)) {
382 		devm_kfree(dev->dev, new_policy_buf);
383 		return -EFAULT;
384 	}
385 
386 	devm_kfree(dev->dev, dev->policy_buf);
387 	dev->policy_buf = new_policy_buf;
388 	dev->policy_sz = length;
389 
390 	if (!amd_pmf_pb_valid(dev))
391 		return -EINVAL;
392 
393 	amd_pmf_hex_dump_pb(dev);
394 	ret = amd_pmf_start_policy_engine(dev);
395 	if (ret < 0)
396 		return ret;
397 
398 	return length;
399 }
400 
401 static const struct file_operations pb_fops = {
402 	.write = amd_pmf_get_pb_data,
403 	.open = simple_open,
404 };
405 
406 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root)
407 {
408 	dev->esbin = debugfs_create_dir("pb", debugfs_root);
409 	debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops);
410 }
411 
412 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev)
413 {
414 	debugfs_remove_recursive(dev->esbin);
415 }
416 #else
417 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) {}
418 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {}
419 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {}
420 #endif
421 
422 static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data)
423 {
424 	return ver->impl_id == TEE_IMPL_ID_AMDTEE;
425 }
426 
427 static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id, const uuid_t *uuid)
428 {
429 	struct tee_ioctl_open_session_arg sess_arg = {};
430 	int rc;
431 
432 	export_uuid(sess_arg.uuid, uuid);
433 	sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
434 	sess_arg.num_params = 0;
435 
436 	rc = tee_client_open_session(ctx, &sess_arg, NULL);
437 	if (rc < 0 || sess_arg.ret != 0) {
438 		pr_err("Failed to open TEE session err:%#x, rc:%d\n", sess_arg.ret, rc);
439 		return rc ?: -EINVAL;
440 	}
441 
442 	*id = sess_arg.session;
443 
444 	return 0;
445 }
446 
447 static int amd_pmf_register_input_device(struct amd_pmf_dev *dev)
448 {
449 	int err;
450 
451 	dev->pmf_idev = devm_input_allocate_device(dev->dev);
452 	if (!dev->pmf_idev)
453 		return -ENOMEM;
454 
455 	dev->pmf_idev->name = "PMF-TA output events";
456 	dev->pmf_idev->phys = "amd-pmf/input0";
457 
458 	input_set_capability(dev->pmf_idev, EV_KEY, KEY_SLEEP);
459 	input_set_capability(dev->pmf_idev, EV_KEY, KEY_SCREENLOCK);
460 	input_set_capability(dev->pmf_idev, EV_KEY, KEY_SUSPEND);
461 
462 	err = input_register_device(dev->pmf_idev);
463 	if (err) {
464 		dev_err(dev->dev, "Failed to register input device: %d\n", err);
465 		return err;
466 	}
467 
468 	return 0;
469 }
470 
471 static int amd_pmf_tee_init(struct amd_pmf_dev *dev, const uuid_t *uuid)
472 {
473 	u32 size;
474 	int ret;
475 
476 	dev->tee_ctx = tee_client_open_context(NULL, amd_pmf_amdtee_ta_match, NULL, NULL);
477 	if (IS_ERR(dev->tee_ctx)) {
478 		dev_err(dev->dev, "Failed to open TEE context\n");
479 		ret = PTR_ERR(dev->tee_ctx);
480 		dev->tee_ctx = NULL;
481 		return ret;
482 	}
483 
484 	ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id, uuid);
485 	if (ret) {
486 		dev_err(dev->dev, "Failed to open TA session (%d)\n", ret);
487 		ret = -EINVAL;
488 		goto out_ctx;
489 	}
490 
491 	size = sizeof(struct ta_pmf_shared_memory) + dev->policy_sz;
492 	dev->fw_shm_pool = tee_shm_alloc_kernel_buf(dev->tee_ctx, size);
493 	if (IS_ERR(dev->fw_shm_pool)) {
494 		dev_err(dev->dev, "Failed to alloc TEE shared memory\n");
495 		ret = PTR_ERR(dev->fw_shm_pool);
496 		goto out_sess;
497 	}
498 
499 	dev->shbuf = tee_shm_get_va(dev->fw_shm_pool, 0);
500 	if (IS_ERR(dev->shbuf)) {
501 		dev_err(dev->dev, "Failed to get TEE virtual address\n");
502 		ret = PTR_ERR(dev->shbuf);
503 		goto out_shm;
504 	}
505 	dev_dbg(dev->dev, "TEE init done\n");
506 
507 	return 0;
508 
509 out_shm:
510 	tee_shm_free(dev->fw_shm_pool);
511 out_sess:
512 	tee_client_close_session(dev->tee_ctx, dev->session_id);
513 out_ctx:
514 	tee_client_close_context(dev->tee_ctx);
515 
516 	return ret;
517 }
518 
519 static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev)
520 {
521 	if (!dev->tee_ctx)
522 		return;
523 	tee_shm_free(dev->fw_shm_pool);
524 	tee_client_close_session(dev->tee_ctx, dev->session_id);
525 	tee_client_close_context(dev->tee_ctx);
526 	dev->tee_ctx = NULL;
527 }
528 
529 int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
530 {
531 	bool status;
532 	int ret, i;
533 
534 	ret = apmf_check_smart_pc(dev);
535 	if (ret) {
536 		/*
537 		 * Lets not return from here if Smart PC bit is not advertised in
538 		 * the BIOS. This way, there will be some amount of power savings
539 		 * to the user with static slider (if enabled).
540 		 */
541 		dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret);
542 		return -ENODEV;
543 	}
544 
545 	INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd);
546 
547 	ret = amd_pmf_set_dram_addr(dev, true);
548 	if (ret)
549 		return ret;
550 
551 	dev->policy_base = devm_ioremap_resource(dev->dev, dev->res);
552 	if (IS_ERR(dev->policy_base))
553 		return PTR_ERR(dev->policy_base);
554 
555 	dev->policy_buf = devm_kzalloc(dev->dev, dev->policy_sz, GFP_KERNEL);
556 	if (!dev->policy_buf)
557 		return -ENOMEM;
558 
559 	memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz);
560 
561 	if (!amd_pmf_pb_valid(dev)) {
562 		dev_info(dev->dev, "No Smart PC policy present\n");
563 		return -EINVAL;
564 	}
565 
566 	amd_pmf_hex_dump_pb(dev);
567 
568 	dev->prev_data = devm_kzalloc(dev->dev, sizeof(*dev->prev_data), GFP_KERNEL);
569 	if (!dev->prev_data)
570 		return -ENOMEM;
571 
572 	for (i = 0; i < ARRAY_SIZE(amd_pmf_ta_uuid); i++) {
573 		ret = amd_pmf_tee_init(dev, &amd_pmf_ta_uuid[i]);
574 		if (ret)
575 			return ret;
576 
577 		ret = amd_pmf_start_policy_engine(dev);
578 		dev_dbg(dev->dev, "start policy engine ret: %d\n", ret);
579 		status = ret == TA_PMF_TYPE_SUCCESS;
580 		if (status) {
581 			dev->cb_flag = true;
582 			break;
583 		}
584 		amd_pmf_tee_deinit(dev);
585 	}
586 
587 	if (!status && !pb_side_load) {
588 		ret = -EINVAL;
589 		goto err;
590 	}
591 
592 	if (pb_side_load)
593 		amd_pmf_open_pb(dev, dev->dbgfs_dir);
594 
595 	ret = amd_pmf_register_input_device(dev);
596 	if (ret)
597 		goto err;
598 
599 	return 0;
600 
601 err:
602 	amd_pmf_deinit_smart_pc(dev);
603 
604 	return ret;
605 }
606 
607 void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
608 {
609 	if (dev->pmf_idev)
610 		input_unregister_device(dev->pmf_idev);
611 
612 	if (pb_side_load && dev->esbin)
613 		amd_pmf_remove_pb(dev);
614 
615 	cancel_delayed_work_sync(&dev->pb_work);
616 	amd_pmf_tee_deinit(dev);
617 }
618