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