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