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