1 // SPDX-License-Identifier: GPL-2.0
3 * AMD Platform Management Framework Driver - TEE Interface
5 * Copyright (c) 2023, Advanced Micro Devices, Inc.
11 #include <linux/debugfs.h>
12 #include <linux/tee_drv.h>
13 #include <linux/uuid.h>
16 #define MAX_TEE_PARAM 4
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;
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");
30 static const uuid_t amd_pmf_ta_uuid = UUID_INIT(0x6fd93b77, 0x3fb8, 0x524d,
31 0xb1, 0x2d, 0xc5, 0x29, 0xb1, 0x3d, 0x85, 0x43);
33 static const char *amd_pmf_uevent_as_str(unsigned int state)
36 case SYSTEM_STATE_S0i3:
40 case SYSTEM_STATE_SCREEN_LOCK:
43 return "Unknown Smart PC event";
47 static void amd_pmf_prepare_args(struct amd_pmf_dev *dev, int cmd,
48 struct tee_ioctl_invoke_arg *arg,
49 struct tee_param *param)
51 memset(arg, 0, sizeof(*arg));
52 memset(param, 0, MAX_TEE_PARAM * sizeof(*param));
55 arg->session = dev->session_id;
56 arg->num_params = MAX_TEE_PARAM;
58 /* Fill invoke cmd params */
59 param[0].u.memref.size = sizeof(struct ta_pmf_shared_memory);
60 param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
61 param[0].u.memref.shm = dev->fw_shm_pool;
62 param[0].u.memref.shm_offs = 0;
65 static void amd_pmf_update_uevents(struct amd_pmf_dev *dev, u16 event)
67 input_report_key(dev->pmf_idev, event, 1); /* key press */
68 input_sync(dev->pmf_idev);
69 input_report_key(dev->pmf_idev, event, 0); /* key release */
70 input_sync(dev->pmf_idev);
73 static void amd_pmf_apply_policies(struct amd_pmf_dev *dev, struct ta_pmf_enact_result *out)
78 for (idx = 0; idx < out->actions_count; idx++) {
79 val = out->actions_list[idx].value;
80 switch (out->actions_list[idx].action_index) {
82 if (dev->prev_data->spl != val) {
83 amd_pmf_send_cmd(dev, SET_SPL, false, val, NULL);
84 dev_dbg(dev->dev, "update SPL: %u\n", val);
85 dev->prev_data->spl = val;
90 if (dev->prev_data->sppt != val) {
91 amd_pmf_send_cmd(dev, SET_SPPT, false, val, NULL);
92 dev_dbg(dev->dev, "update SPPT: %u\n", val);
93 dev->prev_data->sppt = val;
98 if (dev->prev_data->fppt != val) {
99 amd_pmf_send_cmd(dev, SET_FPPT, false, val, NULL);
100 dev_dbg(dev->dev, "update FPPT: %u\n", val);
101 dev->prev_data->fppt = val;
105 case PMF_POLICY_SPPT_APU_ONLY:
106 if (dev->prev_data->sppt_apuonly != val) {
107 amd_pmf_send_cmd(dev, SET_SPPT_APU_ONLY, false, val, NULL);
108 dev_dbg(dev->dev, "update SPPT_APU_ONLY: %u\n", val);
109 dev->prev_data->sppt_apuonly = val;
113 case PMF_POLICY_STT_MIN:
114 if (dev->prev_data->stt_minlimit != val) {
115 amd_pmf_send_cmd(dev, SET_STT_MIN_LIMIT, false, val, NULL);
116 dev_dbg(dev->dev, "update STT_MIN: %u\n", val);
117 dev->prev_data->stt_minlimit = val;
121 case PMF_POLICY_STT_SKINTEMP_APU:
122 if (dev->prev_data->stt_skintemp_apu != val) {
123 amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false, val, NULL);
124 dev_dbg(dev->dev, "update STT_SKINTEMP_APU: %u\n", val);
125 dev->prev_data->stt_skintemp_apu = val;
129 case PMF_POLICY_STT_SKINTEMP_HS2:
130 if (dev->prev_data->stt_skintemp_hs2 != val) {
131 amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false, val, NULL);
132 dev_dbg(dev->dev, "update STT_SKINTEMP_HS2: %u\n", val);
133 dev->prev_data->stt_skintemp_hs2 = val;
138 if (dev->prev_data->p3t_limit != val) {
139 amd_pmf_send_cmd(dev, SET_P3T, false, val, NULL);
140 dev_dbg(dev->dev, "update P3T: %u\n", val);
141 dev->prev_data->p3t_limit = val;
145 case PMF_POLICY_SYSTEM_STATE:
148 amd_pmf_update_uevents(dev, KEY_SLEEP);
151 amd_pmf_update_uevents(dev, KEY_SUSPEND);
154 amd_pmf_update_uevents(dev, KEY_SCREENLOCK);
157 dev_err(dev->dev, "Invalid PMF policy system state: %d\n", val);
160 dev_dbg(dev->dev, "update SYSTEM_STATE: %s\n",
161 amd_pmf_uevent_as_str(val));
167 static int amd_pmf_invoke_cmd_enact(struct amd_pmf_dev *dev)
169 struct ta_pmf_shared_memory *ta_sm = NULL;
170 struct ta_pmf_enact_result *out = NULL;
171 struct ta_pmf_enact_table *in = NULL;
172 struct tee_param param[MAX_TEE_PARAM];
173 struct tee_ioctl_invoke_arg arg;
179 memset(dev->shbuf, 0, dev->policy_sz);
181 out = &ta_sm->pmf_output.policy_apply_table;
182 in = &ta_sm->pmf_input.enact_table;
184 memset(ta_sm, 0, sizeof(*ta_sm));
185 ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES;
186 ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR;
188 amd_pmf_populate_ta_inputs(dev, in);
189 amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES, &arg, param);
191 ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
192 if (ret < 0 || arg.ret != 0) {
193 dev_err(dev->dev, "TEE enact cmd failed. err: %x, ret:%d\n", arg.ret, ret);
197 if (ta_sm->pmf_result == TA_PMF_TYPE_SUCCESS && out->actions_count) {
198 amd_pmf_dump_ta_inputs(dev, in);
199 dev_dbg(dev->dev, "action count:%u result:%x\n", out->actions_count,
201 amd_pmf_apply_policies(dev, out);
207 static int amd_pmf_invoke_cmd_init(struct amd_pmf_dev *dev)
209 struct ta_pmf_shared_memory *ta_sm = NULL;
210 struct tee_param param[MAX_TEE_PARAM];
211 struct ta_pmf_init_table *in = NULL;
212 struct tee_ioctl_invoke_arg arg;
216 dev_err(dev->dev, "Failed to get TEE context\n");
220 dev_dbg(dev->dev, "Policy Binary size: %u bytes\n", dev->policy_sz);
221 memset(dev->shbuf, 0, dev->policy_sz);
223 in = &ta_sm->pmf_input.init_table;
225 ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE;
226 ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR;
228 in->metadata_macrocheck = false;
229 in->sku_check = false;
231 in->frequency = pb_actions_ms;
232 in->policies_table.table_size = dev->policy_sz;
234 memcpy(in->policies_table.table, dev->policy_buf, dev->policy_sz);
235 amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE, &arg, param);
237 ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
238 if (ret < 0 || arg.ret != 0) {
239 dev_err(dev->dev, "Failed to invoke TEE init cmd. err: %x, ret:%d\n", arg.ret, ret);
243 return ta_sm->pmf_result;
246 static void amd_pmf_invoke_cmd(struct work_struct *work)
248 struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, pb_work.work);
250 amd_pmf_invoke_cmd_enact(dev);
251 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms));
254 static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)
256 struct cookie_header *header;
259 if (dev->policy_sz < POLICY_COOKIE_OFFSET + sizeof(*header))
262 header = (struct cookie_header *)(dev->policy_buf + POLICY_COOKIE_OFFSET);
264 if (header->sign != POLICY_SIGN_COOKIE || !header->length) {
265 dev_dbg(dev->dev, "cookie doesn't match\n");
269 if (dev->policy_sz < header->length + 512)
272 /* Update the actual length */
273 dev->policy_sz = header->length + 512;
274 res = amd_pmf_invoke_cmd_init(dev);
275 if (res == TA_PMF_TYPE_SUCCESS) {
276 /* Now its safe to announce that smart pc is enabled */
277 dev->smart_pc_enabled = true;
279 * Start collecting the data from TA FW after a small delay
280 * or else, we might end up getting stale values.
282 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms * 3));
284 dev_err(dev->dev, "ta invoke cmd init failed err: %x\n", res);
285 dev->smart_pc_enabled = false;
292 #ifdef CONFIG_AMD_PMF_DEBUG
293 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev)
295 print_hex_dump_debug("(pb): ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf,
296 dev->policy_sz, false);
299 static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf,
300 size_t length, loff_t *pos)
302 struct amd_pmf_dev *dev = filp->private_data;
303 unsigned char *new_policy_buf;
306 /* Policy binary size cannot exceed POLICY_BUF_MAX_SZ */
307 if (length > POLICY_BUF_MAX_SZ || length == 0)
310 /* re-alloc to the new buffer length of the policy binary */
311 new_policy_buf = memdup_user(buf, length);
312 if (IS_ERR(new_policy_buf))
313 return PTR_ERR(new_policy_buf);
315 kfree(dev->policy_buf);
316 dev->policy_buf = new_policy_buf;
317 dev->policy_sz = length;
319 amd_pmf_hex_dump_pb(dev);
320 ret = amd_pmf_start_policy_engine(dev);
327 static const struct file_operations pb_fops = {
328 .write = amd_pmf_get_pb_data,
332 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root)
334 dev->esbin = debugfs_create_dir("pb", debugfs_root);
335 debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops);
338 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev)
340 debugfs_remove_recursive(dev->esbin);
343 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) {}
344 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {}
345 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {}
348 static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data)
350 return ver->impl_id == TEE_IMPL_ID_AMDTEE;
353 static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id)
355 struct tee_ioctl_open_session_arg sess_arg = {};
358 export_uuid(sess_arg.uuid, &amd_pmf_ta_uuid);
359 sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
360 sess_arg.num_params = 0;
362 rc = tee_client_open_session(ctx, &sess_arg, NULL);
363 if (rc < 0 || sess_arg.ret != 0) {
364 pr_err("Failed to open TEE session err:%#x, rc:%d\n", sess_arg.ret, rc);
368 *id = sess_arg.session;
373 static int amd_pmf_register_input_device(struct amd_pmf_dev *dev)
377 dev->pmf_idev = devm_input_allocate_device(dev->dev);
381 dev->pmf_idev->name = "PMF-TA output events";
382 dev->pmf_idev->phys = "amd-pmf/input0";
384 input_set_capability(dev->pmf_idev, EV_KEY, KEY_SLEEP);
385 input_set_capability(dev->pmf_idev, EV_KEY, KEY_SCREENLOCK);
386 input_set_capability(dev->pmf_idev, EV_KEY, KEY_SUSPEND);
388 err = input_register_device(dev->pmf_idev);
390 dev_err(dev->dev, "Failed to register input device: %d\n", err);
397 static int amd_pmf_tee_init(struct amd_pmf_dev *dev)
402 dev->tee_ctx = tee_client_open_context(NULL, amd_pmf_amdtee_ta_match, NULL, NULL);
403 if (IS_ERR(dev->tee_ctx)) {
404 dev_err(dev->dev, "Failed to open TEE context\n");
405 return PTR_ERR(dev->tee_ctx);
408 ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id);
410 dev_err(dev->dev, "Failed to open TA session (%d)\n", ret);
415 size = sizeof(struct ta_pmf_shared_memory) + dev->policy_sz;
416 dev->fw_shm_pool = tee_shm_alloc_kernel_buf(dev->tee_ctx, size);
417 if (IS_ERR(dev->fw_shm_pool)) {
418 dev_err(dev->dev, "Failed to alloc TEE shared memory\n");
419 ret = PTR_ERR(dev->fw_shm_pool);
423 dev->shbuf = tee_shm_get_va(dev->fw_shm_pool, 0);
424 if (IS_ERR(dev->shbuf)) {
425 dev_err(dev->dev, "Failed to get TEE virtual address\n");
426 ret = PTR_ERR(dev->shbuf);
429 dev_dbg(dev->dev, "TEE init done\n");
434 tee_shm_free(dev->fw_shm_pool);
436 tee_client_close_session(dev->tee_ctx, dev->session_id);
438 tee_client_close_context(dev->tee_ctx);
443 static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev)
445 tee_shm_free(dev->fw_shm_pool);
446 tee_client_close_session(dev->tee_ctx, dev->session_id);
447 tee_client_close_context(dev->tee_ctx);
450 int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
454 ret = apmf_check_smart_pc(dev);
457 * Lets not return from here if Smart PC bit is not advertised in
458 * the BIOS. This way, there will be some amount of power savings
459 * to the user with static slider (if enabled).
461 dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret);
465 ret = amd_pmf_tee_init(dev);
469 INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd);
471 ret = amd_pmf_set_dram_addr(dev, true);
475 dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz);
476 if (!dev->policy_base) {
481 dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL);
482 if (!dev->policy_buf) {
487 memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz);
489 amd_pmf_hex_dump_pb(dev);
491 dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL);
492 if (!dev->prev_data) {
497 ret = amd_pmf_start_policy_engine(dev);
502 amd_pmf_open_pb(dev, dev->dbgfs_dir);
504 ret = amd_pmf_register_input_device(dev);
511 amd_pmf_deinit_smart_pc(dev);
516 void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
519 input_unregister_device(dev->pmf_idev);
521 if (pb_side_load && dev->esbin)
522 amd_pmf_remove_pb(dev);
524 cancel_delayed_work_sync(&dev->pb_work);
525 kfree(dev->prev_data);
526 dev->prev_data = NULL;
527 kfree(dev->policy_buf);
528 dev->policy_buf = NULL;
531 amd_pmf_tee_deinit(dev);