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