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 int amd_pmf_update_uevents(struct amd_pmf_dev *dev, u16 event) 66 { 67 char *envp[2] = {}; 68 69 envp[0] = kasprintf(GFP_KERNEL, "EVENT_ID=%d", event); 70 if (!envp[0]) 71 return -EINVAL; 72 73 kobject_uevent_env(&dev->dev->kobj, KOBJ_CHANGE, envp); 74 75 kfree(envp[0]); 76 return 0; 77 } 78 79 static void amd_pmf_apply_policies(struct amd_pmf_dev *dev, struct ta_pmf_enact_result *out) 80 { 81 u32 val; 82 int idx; 83 84 for (idx = 0; idx < out->actions_count; idx++) { 85 val = out->actions_list[idx].value; 86 switch (out->actions_list[idx].action_index) { 87 case PMF_POLICY_SPL: 88 if (dev->prev_data->spl != val) { 89 amd_pmf_send_cmd(dev, SET_SPL, false, val, NULL); 90 dev_dbg(dev->dev, "update SPL: %u\n", val); 91 dev->prev_data->spl = val; 92 } 93 break; 94 95 case PMF_POLICY_SPPT: 96 if (dev->prev_data->sppt != val) { 97 amd_pmf_send_cmd(dev, SET_SPPT, false, val, NULL); 98 dev_dbg(dev->dev, "update SPPT: %u\n", val); 99 dev->prev_data->sppt = val; 100 } 101 break; 102 103 case PMF_POLICY_FPPT: 104 if (dev->prev_data->fppt != val) { 105 amd_pmf_send_cmd(dev, SET_FPPT, false, val, NULL); 106 dev_dbg(dev->dev, "update FPPT: %u\n", val); 107 dev->prev_data->fppt = val; 108 } 109 break; 110 111 case PMF_POLICY_SPPT_APU_ONLY: 112 if (dev->prev_data->sppt_apuonly != val) { 113 amd_pmf_send_cmd(dev, SET_SPPT_APU_ONLY, false, val, NULL); 114 dev_dbg(dev->dev, "update SPPT_APU_ONLY: %u\n", val); 115 dev->prev_data->sppt_apuonly = val; 116 } 117 break; 118 119 case PMF_POLICY_STT_MIN: 120 if (dev->prev_data->stt_minlimit != val) { 121 amd_pmf_send_cmd(dev, SET_STT_MIN_LIMIT, false, val, NULL); 122 dev_dbg(dev->dev, "update STT_MIN: %u\n", val); 123 dev->prev_data->stt_minlimit = val; 124 } 125 break; 126 127 case PMF_POLICY_STT_SKINTEMP_APU: 128 if (dev->prev_data->stt_skintemp_apu != val) { 129 amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false, val, NULL); 130 dev_dbg(dev->dev, "update STT_SKINTEMP_APU: %u\n", val); 131 dev->prev_data->stt_skintemp_apu = val; 132 } 133 break; 134 135 case PMF_POLICY_STT_SKINTEMP_HS2: 136 if (dev->prev_data->stt_skintemp_hs2 != val) { 137 amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false, val, NULL); 138 dev_dbg(dev->dev, "update STT_SKINTEMP_HS2: %u\n", val); 139 dev->prev_data->stt_skintemp_hs2 = val; 140 } 141 break; 142 143 case PMF_POLICY_P3T: 144 if (dev->prev_data->p3t_limit != val) { 145 amd_pmf_send_cmd(dev, SET_P3T, false, val, NULL); 146 dev_dbg(dev->dev, "update P3T: %u\n", val); 147 dev->prev_data->p3t_limit = val; 148 } 149 break; 150 151 case PMF_POLICY_SYSTEM_STATE: 152 amd_pmf_update_uevents(dev, val); 153 dev_dbg(dev->dev, "update SYSTEM_STATE: %s\n", 154 amd_pmf_uevent_as_str(val)); 155 break; 156 } 157 } 158 } 159 160 static int amd_pmf_invoke_cmd_enact(struct amd_pmf_dev *dev) 161 { 162 struct ta_pmf_shared_memory *ta_sm = NULL; 163 struct ta_pmf_enact_result *out = NULL; 164 struct ta_pmf_enact_table *in = NULL; 165 struct tee_param param[MAX_TEE_PARAM]; 166 struct tee_ioctl_invoke_arg arg; 167 int ret = 0; 168 169 if (!dev->tee_ctx) 170 return -ENODEV; 171 172 memset(dev->shbuf, 0, dev->policy_sz); 173 ta_sm = dev->shbuf; 174 out = &ta_sm->pmf_output.policy_apply_table; 175 in = &ta_sm->pmf_input.enact_table; 176 177 memset(ta_sm, 0, sizeof(*ta_sm)); 178 ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES; 179 ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR; 180 181 amd_pmf_populate_ta_inputs(dev, in); 182 amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES, &arg, param); 183 184 ret = tee_client_invoke_func(dev->tee_ctx, &arg, param); 185 if (ret < 0 || arg.ret != 0) { 186 dev_err(dev->dev, "TEE enact cmd failed. err: %x, ret:%d\n", arg.ret, ret); 187 return ret; 188 } 189 190 if (ta_sm->pmf_result == TA_PMF_TYPE_SUCCESS && out->actions_count) { 191 amd_pmf_dump_ta_inputs(dev, in); 192 dev_dbg(dev->dev, "action count:%u result:%x\n", out->actions_count, 193 ta_sm->pmf_result); 194 amd_pmf_apply_policies(dev, out); 195 } 196 197 return 0; 198 } 199 200 static int amd_pmf_invoke_cmd_init(struct amd_pmf_dev *dev) 201 { 202 struct ta_pmf_shared_memory *ta_sm = NULL; 203 struct tee_param param[MAX_TEE_PARAM]; 204 struct ta_pmf_init_table *in = NULL; 205 struct tee_ioctl_invoke_arg arg; 206 int ret = 0; 207 208 if (!dev->tee_ctx) { 209 dev_err(dev->dev, "Failed to get TEE context\n"); 210 return -ENODEV; 211 } 212 213 dev_dbg(dev->dev, "Policy Binary size: %u bytes\n", dev->policy_sz); 214 memset(dev->shbuf, 0, dev->policy_sz); 215 ta_sm = dev->shbuf; 216 in = &ta_sm->pmf_input.init_table; 217 218 ta_sm->command_id = TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE; 219 ta_sm->if_version = PMF_TA_IF_VERSION_MAJOR; 220 221 in->metadata_macrocheck = false; 222 in->sku_check = false; 223 in->validate = true; 224 in->frequency = pb_actions_ms; 225 in->policies_table.table_size = dev->policy_sz; 226 227 memcpy(in->policies_table.table, dev->policy_buf, dev->policy_sz); 228 amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_INITIALIZE, &arg, param); 229 230 ret = tee_client_invoke_func(dev->tee_ctx, &arg, param); 231 if (ret < 0 || arg.ret != 0) { 232 dev_err(dev->dev, "Failed to invoke TEE init cmd. err: %x, ret:%d\n", arg.ret, ret); 233 return ret; 234 } 235 236 return ta_sm->pmf_result; 237 } 238 239 static void amd_pmf_invoke_cmd(struct work_struct *work) 240 { 241 struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, pb_work.work); 242 243 amd_pmf_invoke_cmd_enact(dev); 244 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms)); 245 } 246 247 static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev) 248 { 249 u32 cookie, length; 250 int res; 251 252 cookie = readl(dev->policy_buf + POLICY_COOKIE_OFFSET); 253 length = readl(dev->policy_buf + POLICY_COOKIE_LEN); 254 255 if (cookie != POLICY_SIGN_COOKIE || !length) 256 return -EINVAL; 257 258 /* Update the actual length */ 259 dev->policy_sz = length + 512; 260 res = amd_pmf_invoke_cmd_init(dev); 261 if (res == TA_PMF_TYPE_SUCCESS) { 262 /* Now its safe to announce that smart pc is enabled */ 263 dev->smart_pc_enabled = PMF_SMART_PC_ENABLED; 264 /* 265 * Start collecting the data from TA FW after a small delay 266 * or else, we might end up getting stale values. 267 */ 268 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms * 3)); 269 } else { 270 dev_err(dev->dev, "ta invoke cmd init failed err: %x\n", res); 271 dev->smart_pc_enabled = PMF_SMART_PC_DISABLED; 272 return res; 273 } 274 275 return 0; 276 } 277 278 #ifdef CONFIG_AMD_PMF_DEBUG 279 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) 280 { 281 print_hex_dump_debug("(pb): ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf, 282 dev->policy_sz, false); 283 } 284 285 static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf, 286 size_t length, loff_t *pos) 287 { 288 struct amd_pmf_dev *dev = filp->private_data; 289 unsigned char *new_policy_buf; 290 int ret; 291 292 /* Policy binary size cannot exceed POLICY_BUF_MAX_SZ */ 293 if (length > POLICY_BUF_MAX_SZ || length == 0) 294 return -EINVAL; 295 296 /* re-alloc to the new buffer length of the policy binary */ 297 new_policy_buf = kzalloc(length, GFP_KERNEL); 298 if (!new_policy_buf) 299 return -ENOMEM; 300 301 if (copy_from_user(new_policy_buf, buf, length)) 302 return -EFAULT; 303 304 kfree(dev->policy_buf); 305 dev->policy_buf = new_policy_buf; 306 dev->policy_sz = length; 307 308 amd_pmf_hex_dump_pb(dev); 309 ret = amd_pmf_start_policy_engine(dev); 310 if (ret) 311 return -EINVAL; 312 313 return length; 314 } 315 316 static const struct file_operations pb_fops = { 317 .write = amd_pmf_get_pb_data, 318 .open = simple_open, 319 }; 320 321 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) 322 { 323 dev->esbin = debugfs_create_dir("pb", debugfs_root); 324 debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops); 325 } 326 327 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) 328 { 329 debugfs_remove_recursive(dev->esbin); 330 } 331 #else 332 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) {} 333 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {} 334 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {} 335 #endif 336 337 static int amd_pmf_get_bios_buffer(struct amd_pmf_dev *dev) 338 { 339 dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL); 340 if (!dev->policy_buf) 341 return -ENOMEM; 342 343 dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz); 344 if (!dev->policy_base) 345 return -ENOMEM; 346 347 memcpy(dev->policy_buf, dev->policy_base, dev->policy_sz); 348 349 amd_pmf_hex_dump_pb(dev); 350 if (pb_side_load) 351 amd_pmf_open_pb(dev, dev->dbgfs_dir); 352 353 return amd_pmf_start_policy_engine(dev); 354 } 355 356 static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data) 357 { 358 return ver->impl_id == TEE_IMPL_ID_AMDTEE; 359 } 360 361 static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id) 362 { 363 struct tee_ioctl_open_session_arg sess_arg = {}; 364 int rc; 365 366 export_uuid(sess_arg.uuid, &amd_pmf_ta_uuid); 367 sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC; 368 sess_arg.num_params = 0; 369 370 rc = tee_client_open_session(ctx, &sess_arg, NULL); 371 if (rc < 0 || sess_arg.ret != 0) { 372 pr_err("Failed to open TEE session err:%#x, rc:%d\n", sess_arg.ret, rc); 373 return rc; 374 } 375 376 *id = sess_arg.session; 377 378 return rc; 379 } 380 381 static int amd_pmf_tee_init(struct amd_pmf_dev *dev) 382 { 383 u32 size; 384 int ret; 385 386 dev->tee_ctx = tee_client_open_context(NULL, amd_pmf_amdtee_ta_match, NULL, NULL); 387 if (IS_ERR(dev->tee_ctx)) { 388 dev_err(dev->dev, "Failed to open TEE context\n"); 389 return PTR_ERR(dev->tee_ctx); 390 } 391 392 ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id); 393 if (ret) { 394 dev_err(dev->dev, "Failed to open TA session (%d)\n", ret); 395 ret = -EINVAL; 396 goto out_ctx; 397 } 398 399 size = sizeof(struct ta_pmf_shared_memory) + dev->policy_sz; 400 dev->fw_shm_pool = tee_shm_alloc_kernel_buf(dev->tee_ctx, size); 401 if (IS_ERR(dev->fw_shm_pool)) { 402 dev_err(dev->dev, "Failed to alloc TEE shared memory\n"); 403 ret = PTR_ERR(dev->fw_shm_pool); 404 goto out_sess; 405 } 406 407 dev->shbuf = tee_shm_get_va(dev->fw_shm_pool, 0); 408 if (IS_ERR(dev->shbuf)) { 409 dev_err(dev->dev, "Failed to get TEE virtual address\n"); 410 ret = PTR_ERR(dev->shbuf); 411 goto out_shm; 412 } 413 dev_dbg(dev->dev, "TEE init done\n"); 414 415 return 0; 416 417 out_shm: 418 tee_shm_free(dev->fw_shm_pool); 419 out_sess: 420 tee_client_close_session(dev->tee_ctx, dev->session_id); 421 out_ctx: 422 tee_client_close_context(dev->tee_ctx); 423 424 return ret; 425 } 426 427 static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev) 428 { 429 tee_shm_free(dev->fw_shm_pool); 430 tee_client_close_session(dev->tee_ctx, dev->session_id); 431 tee_client_close_context(dev->tee_ctx); 432 } 433 434 int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev) 435 { 436 int ret; 437 438 ret = apmf_check_smart_pc(dev); 439 if (ret) { 440 /* 441 * Lets not return from here if Smart PC bit is not advertised in 442 * the BIOS. This way, there will be some amount of power savings 443 * to the user with static slider (if enabled). 444 */ 445 dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret); 446 return -ENODEV; 447 } 448 449 ret = amd_pmf_tee_init(dev); 450 if (ret) 451 return ret; 452 453 INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd); 454 amd_pmf_set_dram_addr(dev, true); 455 amd_pmf_get_bios_buffer(dev); 456 dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL); 457 if (!dev->prev_data) 458 return -ENOMEM; 459 460 return dev->smart_pc_enabled; 461 } 462 463 void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev) 464 { 465 if (pb_side_load) 466 amd_pmf_remove_pb(dev); 467 468 kfree(dev->prev_data); 469 kfree(dev->policy_buf); 470 cancel_delayed_work_sync(&dev->pb_work); 471 amd_pmf_tee_deinit(dev); 472 } 473