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(0x6fd93b77, 0x3fb8, 0x524d, 31 0xb1, 0x2d, 0xc5, 0x29, 0xb1, 0x3d, 0x85, 0x43); 32 33 static const char *amd_pmf_uevent_as_str(unsigned int state) 34 { 35 switch (state) { 36 case SYSTEM_STATE_S0i3: 37 return "S0i3"; 38 case SYSTEM_STATE_S4: 39 return "S4"; 40 case SYSTEM_STATE_SCREEN_LOCK: 41 return "SCREEN_LOCK"; 42 default: 43 return "Unknown Smart PC event"; 44 } 45 } 46 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) 50 { 51 memset(arg, 0, sizeof(*arg)); 52 memset(param, 0, MAX_TEE_PARAM * sizeof(*param)); 53 54 arg->func = cmd; 55 arg->session = dev->session_id; 56 arg->num_params = MAX_TEE_PARAM; 57 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; 63 } 64 65 static void amd_pmf_update_uevents(struct amd_pmf_dev *dev, u16 event) 66 { 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); 71 } 72 73 static void amd_pmf_apply_policies(struct amd_pmf_dev *dev, struct ta_pmf_enact_result *out) 74 { 75 u32 val; 76 int idx; 77 78 for (idx = 0; idx < out->actions_count; idx++) { 79 val = out->actions_list[idx].value; 80 switch (out->actions_list[idx].action_index) { 81 case PMF_POLICY_SPL: 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; 86 } 87 break; 88 89 case PMF_POLICY_SPPT: 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; 94 } 95 break; 96 97 case PMF_POLICY_FPPT: 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; 102 } 103 break; 104 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; 110 } 111 break; 112 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; 118 } 119 break; 120 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; 126 } 127 break; 128 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; 134 } 135 break; 136 137 case PMF_POLICY_P3T: 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; 142 } 143 break; 144 145 case PMF_POLICY_SYSTEM_STATE: 146 switch (val) { 147 case 0: 148 amd_pmf_update_uevents(dev, KEY_SLEEP); 149 break; 150 case 1: 151 amd_pmf_update_uevents(dev, KEY_SUSPEND); 152 break; 153 case 2: 154 amd_pmf_update_uevents(dev, KEY_SCREENLOCK); 155 break; 156 default: 157 dev_err(dev->dev, "Invalid PMF policy system state: %d\n", val); 158 } 159 160 dev_dbg(dev->dev, "update SYSTEM_STATE: %s\n", 161 amd_pmf_uevent_as_str(val)); 162 break; 163 } 164 } 165 } 166 167 static int amd_pmf_invoke_cmd_enact(struct amd_pmf_dev *dev) 168 { 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; 174 int ret = 0; 175 176 if (!dev->tee_ctx) 177 return -ENODEV; 178 179 memset(dev->shbuf, 0, dev->policy_sz); 180 ta_sm = dev->shbuf; 181 out = &ta_sm->pmf_output.policy_apply_table; 182 in = &ta_sm->pmf_input.enact_table; 183 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; 187 188 amd_pmf_populate_ta_inputs(dev, in); 189 amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES, &arg, param); 190 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); 194 return ret; 195 } 196 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, 200 ta_sm->pmf_result); 201 amd_pmf_apply_policies(dev, out); 202 } 203 204 return 0; 205 } 206 207 static int amd_pmf_invoke_cmd_init(struct amd_pmf_dev *dev) 208 { 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; 213 int ret = 0; 214 215 if (!dev->tee_ctx) { 216 dev_err(dev->dev, "Failed to get TEE context\n"); 217 return -ENODEV; 218 } 219 220 dev_dbg(dev->dev, "Policy Binary size: %u bytes\n", dev->policy_sz); 221 memset(dev->shbuf, 0, dev->policy_sz); 222 ta_sm = dev->shbuf; 223 in = &ta_sm->pmf_input.init_table; 224 225 ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE; 226 ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR; 227 228 in->metadata_macrocheck = false; 229 in->sku_check = false; 230 in->validate = true; 231 in->frequency = pb_actions_ms; 232 in->policies_table.table_size = dev->policy_sz; 233 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); 236 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); 240 return ret; 241 } 242 243 return ta_sm->pmf_result; 244 } 245 246 static void amd_pmf_invoke_cmd(struct work_struct *work) 247 { 248 struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, pb_work.work); 249 250 amd_pmf_invoke_cmd_enact(dev); 251 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms)); 252 } 253 254 static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) 255 { 256 struct cookie_header *header; 257 int res; 258 259 if (dev->policy_sz < POLICY_COOKIE_OFFSET + sizeof(*header)) 260 return -EINVAL; 261 262 header = (struct cookie_header *)(dev->policy_buf + POLICY_COOKIE_OFFSET); 263 264 if (header->sign != POLICY_SIGN_COOKIE || !header->length) { 265 dev_dbg(dev->dev, "cookie doesn't match\n"); 266 return -EINVAL; 267 } 268 269 if (dev->policy_sz < header->length + 512) 270 return -EINVAL; 271 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; 278 /* 279 * Start collecting the data from TA FW after a small delay 280 * or else, we might end up getting stale values. 281 */ 282 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms * 3)); 283 } else { 284 dev_err(dev->dev, "ta invoke cmd init failed err: %x\n", res); 285 dev->smart_pc_enabled = false; 286 return -EIO; 287 } 288 289 return 0; 290 } 291 292 #ifdef CONFIG_AMD_PMF_DEBUG 293 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) 294 { 295 print_hex_dump_debug("(pb): ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf, 296 dev->policy_sz, false); 297 } 298 299 static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf, 300 size_t length, loff_t *pos) 301 { 302 struct amd_pmf_dev *dev = filp->private_data; 303 unsigned char *new_policy_buf; 304 int ret; 305 306 /* Policy binary size cannot exceed POLICY_BUF_MAX_SZ */ 307 if (length > POLICY_BUF_MAX_SZ || length == 0) 308 return -EINVAL; 309 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); 314 315 kfree(dev->policy_buf); 316 dev->policy_buf = new_policy_buf; 317 dev->policy_sz = length; 318 319 amd_pmf_hex_dump_pb(dev); 320 ret = amd_pmf_start_policy_engine(dev); 321 if (ret < 0) 322 return ret; 323 324 return length; 325 } 326 327 static const struct file_operations pb_fops = { 328 .write = amd_pmf_get_pb_data, 329 .open = simple_open, 330 }; 331 332 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) 333 { 334 dev->esbin = debugfs_create_dir("pb", debugfs_root); 335 debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops); 336 } 337 338 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) 339 { 340 debugfs_remove_recursive(dev->esbin); 341 } 342 #else 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) {} 346 #endif 347 348 static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data) 349 { 350 return ver->impl_id == TEE_IMPL_ID_AMDTEE; 351 } 352 353 static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id) 354 { 355 struct tee_ioctl_open_session_arg sess_arg = {}; 356 int rc; 357 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; 361 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); 365 return rc; 366 } 367 368 *id = sess_arg.session; 369 370 return rc; 371 } 372 373 static int amd_pmf_register_input_device(struct amd_pmf_dev *dev) 374 { 375 int err; 376 377 dev->pmf_idev = devm_input_allocate_device(dev->dev); 378 if (!dev->pmf_idev) 379 return -ENOMEM; 380 381 dev->pmf_idev->name = "PMF-TA output events"; 382 dev->pmf_idev->phys = "amd-pmf/input0"; 383 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); 387 388 err = input_register_device(dev->pmf_idev); 389 if (err) { 390 dev_err(dev->dev, "Failed to register input device: %d\n", err); 391 return err; 392 } 393 394 return 0; 395 } 396 397 static int amd_pmf_tee_init(struct amd_pmf_dev *dev) 398 { 399 u32 size; 400 int ret; 401 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); 406 } 407 408 ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id); 409 if (ret) { 410 dev_err(dev->dev, "Failed to open TA session (%d)\n", ret); 411 ret = -EINVAL; 412 goto out_ctx; 413 } 414 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); 420 goto out_sess; 421 } 422 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); 427 goto out_shm; 428 } 429 dev_dbg(dev->dev, "TEE init done\n"); 430 431 return 0; 432 433 out_shm: 434 tee_shm_free(dev->fw_shm_pool); 435 out_sess: 436 tee_client_close_session(dev->tee_ctx, dev->session_id); 437 out_ctx: 438 tee_client_close_context(dev->tee_ctx); 439 440 return ret; 441 } 442 443 static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev) 444 { 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); 448 } 449 450 int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev) 451 { 452 int ret; 453 454 ret = apmf_check_smart_pc(dev); 455 if (ret) { 456 /* 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). 460 */ 461 dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret); 462 return -ENODEV; 463 } 464 465 ret = amd_pmf_tee_init(dev); 466 if (ret) 467 return ret; 468 469 INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd); 470 471 ret = amd_pmf_set_dram_addr(dev, true); 472 if (ret) 473 goto error; 474 475 dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz); 476 if (!dev->policy_base) { 477 ret = -ENOMEM; 478 goto error; 479 } 480 481 dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL); 482 if (!dev->policy_buf) { 483 ret = -ENOMEM; 484 goto error; 485 } 486 487 memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz); 488 489 amd_pmf_hex_dump_pb(dev); 490 491 dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL); 492 if (!dev->prev_data) { 493 ret = -ENOMEM; 494 goto error; 495 } 496 497 ret = amd_pmf_start_policy_engine(dev); 498 if (ret) 499 goto error; 500 501 if (pb_side_load) 502 amd_pmf_open_pb(dev, dev->dbgfs_dir); 503 504 ret = amd_pmf_register_input_device(dev); 505 if (ret) 506 goto error; 507 508 return 0; 509 510 error: 511 amd_pmf_deinit_smart_pc(dev); 512 513 return ret; 514 } 515 516 void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev) 517 { 518 if (dev->pmf_idev) 519 input_unregister_device(dev->pmf_idev); 520 521 if (pb_side_load && dev->esbin) 522 amd_pmf_remove_pb(dev); 523 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; 529 kfree(dev->buf); 530 dev->buf = NULL; 531 amd_pmf_tee_deinit(dev); 532 } 533