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