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 #ifdef CONFIG_AMD_PMF_DEBUG 338 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) 339 { 340 print_hex_dump_debug("(pb): ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf, 341 dev->policy_sz, false); 342 } 343 344 static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf, 345 size_t length, loff_t *pos) 346 { 347 struct amd_pmf_dev *dev = filp->private_data; 348 unsigned char *new_policy_buf; 349 int ret; 350 351 /* Policy binary size cannot exceed POLICY_BUF_MAX_SZ */ 352 if (length > POLICY_BUF_MAX_SZ || length == 0) 353 return -EINVAL; 354 355 /* re-alloc to the new buffer length of the policy binary */ 356 new_policy_buf = memdup_user(buf, length); 357 if (IS_ERR(new_policy_buf)) 358 return PTR_ERR(new_policy_buf); 359 360 kfree(dev->policy_buf); 361 dev->policy_buf = new_policy_buf; 362 dev->policy_sz = length; 363 364 amd_pmf_hex_dump_pb(dev); 365 ret = amd_pmf_start_policy_engine(dev); 366 if (ret < 0) 367 return ret; 368 369 return length; 370 } 371 372 static const struct file_operations pb_fops = { 373 .write = amd_pmf_get_pb_data, 374 .open = simple_open, 375 }; 376 377 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) 378 { 379 dev->esbin = debugfs_create_dir("pb", debugfs_root); 380 debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops); 381 } 382 383 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) 384 { 385 debugfs_remove_recursive(dev->esbin); 386 } 387 #else 388 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) {} 389 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {} 390 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {} 391 #endif 392 393 static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data) 394 { 395 return ver->impl_id == TEE_IMPL_ID_AMDTEE; 396 } 397 398 static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id, const uuid_t *uuid) 399 { 400 struct tee_ioctl_open_session_arg sess_arg = {}; 401 int rc; 402 403 export_uuid(sess_arg.uuid, uuid); 404 sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC; 405 sess_arg.num_params = 0; 406 407 rc = tee_client_open_session(ctx, &sess_arg, NULL); 408 if (rc < 0 || sess_arg.ret != 0) { 409 pr_err("Failed to open TEE session err:%#x, rc:%d\n", sess_arg.ret, rc); 410 return rc; 411 } 412 413 *id = sess_arg.session; 414 415 return rc; 416 } 417 418 static int amd_pmf_register_input_device(struct amd_pmf_dev *dev) 419 { 420 int err; 421 422 dev->pmf_idev = devm_input_allocate_device(dev->dev); 423 if (!dev->pmf_idev) 424 return -ENOMEM; 425 426 dev->pmf_idev->name = "PMF-TA output events"; 427 dev->pmf_idev->phys = "amd-pmf/input0"; 428 429 input_set_capability(dev->pmf_idev, EV_KEY, KEY_SLEEP); 430 input_set_capability(dev->pmf_idev, EV_KEY, KEY_SCREENLOCK); 431 input_set_capability(dev->pmf_idev, EV_KEY, KEY_SUSPEND); 432 433 err = input_register_device(dev->pmf_idev); 434 if (err) { 435 dev_err(dev->dev, "Failed to register input device: %d\n", err); 436 return err; 437 } 438 439 return 0; 440 } 441 442 static int amd_pmf_tee_init(struct amd_pmf_dev *dev, const uuid_t *uuid) 443 { 444 u32 size; 445 int ret; 446 447 dev->tee_ctx = tee_client_open_context(NULL, amd_pmf_amdtee_ta_match, NULL, NULL); 448 if (IS_ERR(dev->tee_ctx)) { 449 dev_err(dev->dev, "Failed to open TEE context\n"); 450 return PTR_ERR(dev->tee_ctx); 451 } 452 453 ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id, uuid); 454 if (ret) { 455 dev_err(dev->dev, "Failed to open TA session (%d)\n", ret); 456 ret = -EINVAL; 457 goto out_ctx; 458 } 459 460 size = sizeof(struct ta_pmf_shared_memory) + dev->policy_sz; 461 dev->fw_shm_pool = tee_shm_alloc_kernel_buf(dev->tee_ctx, size); 462 if (IS_ERR(dev->fw_shm_pool)) { 463 dev_err(dev->dev, "Failed to alloc TEE shared memory\n"); 464 ret = PTR_ERR(dev->fw_shm_pool); 465 goto out_sess; 466 } 467 468 dev->shbuf = tee_shm_get_va(dev->fw_shm_pool, 0); 469 if (IS_ERR(dev->shbuf)) { 470 dev_err(dev->dev, "Failed to get TEE virtual address\n"); 471 ret = PTR_ERR(dev->shbuf); 472 goto out_shm; 473 } 474 dev_dbg(dev->dev, "TEE init done\n"); 475 476 return 0; 477 478 out_shm: 479 tee_shm_free(dev->fw_shm_pool); 480 out_sess: 481 tee_client_close_session(dev->tee_ctx, dev->session_id); 482 out_ctx: 483 tee_client_close_context(dev->tee_ctx); 484 485 return ret; 486 } 487 488 static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev) 489 { 490 tee_shm_free(dev->fw_shm_pool); 491 tee_client_close_session(dev->tee_ctx, dev->session_id); 492 tee_client_close_context(dev->tee_ctx); 493 } 494 495 int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev) 496 { 497 bool status; 498 int ret, i; 499 500 ret = apmf_check_smart_pc(dev); 501 if (ret) { 502 /* 503 * Lets not return from here if Smart PC bit is not advertised in 504 * the BIOS. This way, there will be some amount of power savings 505 * to the user with static slider (if enabled). 506 */ 507 dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret); 508 return -ENODEV; 509 } 510 511 INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd); 512 513 ret = amd_pmf_set_dram_addr(dev, true); 514 if (ret) 515 goto err_cancel_work; 516 517 dev->policy_base = devm_ioremap_resource(dev->dev, dev->res); 518 if (IS_ERR(dev->policy_base)) { 519 ret = PTR_ERR(dev->policy_base); 520 goto err_free_dram_buf; 521 } 522 523 dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL); 524 if (!dev->policy_buf) { 525 ret = -ENOMEM; 526 goto err_free_dram_buf; 527 } 528 529 memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz); 530 531 amd_pmf_hex_dump_pb(dev); 532 533 dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL); 534 if (!dev->prev_data) { 535 ret = -ENOMEM; 536 goto err_free_policy; 537 } 538 539 for (i = 0; i < ARRAY_SIZE(amd_pmf_ta_uuid); i++) { 540 ret = amd_pmf_tee_init(dev, &amd_pmf_ta_uuid[i]); 541 if (ret) 542 goto err_free_prev_data; 543 544 ret = amd_pmf_start_policy_engine(dev); 545 switch (ret) { 546 case TA_PMF_TYPE_SUCCESS: 547 status = true; 548 break; 549 case TA_ERROR_CRYPTO_INVALID_PARAM: 550 case TA_ERROR_CRYPTO_BIN_TOO_LARGE: 551 amd_pmf_tee_deinit(dev); 552 status = false; 553 break; 554 default: 555 ret = -EINVAL; 556 amd_pmf_tee_deinit(dev); 557 goto err_free_prev_data; 558 } 559 560 if (status) 561 break; 562 } 563 564 if (!status && !pb_side_load) { 565 ret = -EINVAL; 566 goto err_free_prev_data; 567 } 568 569 if (pb_side_load) 570 amd_pmf_open_pb(dev, dev->dbgfs_dir); 571 572 ret = amd_pmf_register_input_device(dev); 573 if (ret) 574 goto err_pmf_remove_pb; 575 576 return 0; 577 578 err_pmf_remove_pb: 579 if (pb_side_load && dev->esbin) 580 amd_pmf_remove_pb(dev); 581 amd_pmf_tee_deinit(dev); 582 err_free_prev_data: 583 kfree(dev->prev_data); 584 err_free_policy: 585 kfree(dev->policy_buf); 586 err_free_dram_buf: 587 kfree(dev->buf); 588 err_cancel_work: 589 cancel_delayed_work_sync(&dev->pb_work); 590 591 return ret; 592 } 593 594 void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev) 595 { 596 if (dev->pmf_idev) 597 input_unregister_device(dev->pmf_idev); 598 599 if (pb_side_load && dev->esbin) 600 amd_pmf_remove_pb(dev); 601 602 cancel_delayed_work_sync(&dev->pb_work); 603 kfree(dev->prev_data); 604 dev->prev_data = NULL; 605 kfree(dev->policy_buf); 606 dev->policy_buf = NULL; 607 kfree(dev->buf); 608 dev->buf = NULL; 609 amd_pmf_tee_deinit(dev); 610 } 611