Lines Matching refs:dev
50 static void amd_pmf_prepare_args(struct amd_pmf_dev *dev, int cmd,
58 arg->session = dev->session_id;
64 param[0].u.memref.shm = dev->fw_shm_pool;
68 static void amd_pmf_update_uevents(struct amd_pmf_dev *dev, u16 event)
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);
76 static void amd_pmf_apply_policies(struct amd_pmf_dev *dev, struct ta_pmf_enact_result *out)
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;
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;
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;
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;
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;
125 if (dev->prev_data->stt_skintemp_apu != val) {
126 amd_pmf_send_cmd(dev, SET_STT_LIMIT_APU, false,
128 dev_dbg(dev->dev, "update STT_SKINTEMP_APU: %u\n", val);
129 dev->prev_data->stt_skintemp_apu = val;
134 if (dev->prev_data->stt_skintemp_hs2 != val) {
135 amd_pmf_send_cmd(dev, SET_STT_LIMIT_HS2, false,
137 dev_dbg(dev->dev, "update STT_SKINTEMP_HS2: %u\n", val);
138 dev->prev_data->stt_skintemp_hs2 = val;
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;
153 amd_pmf_update_uevents(dev, KEY_SLEEP);
156 amd_pmf_update_uevents(dev, KEY_SUSPEND);
159 amd_pmf_update_uevents(dev, KEY_SCREENLOCK);
162 dev_err(dev->dev, "Invalid PMF policy system state: %d\n", val);
165 dev_dbg(dev->dev, "update SYSTEM_STATE: %s\n",
170 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(0), 0);
174 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(1), 1);
178 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(2), 2);
182 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(3), 3);
186 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(4), 4);
190 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(5), 5);
194 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(6), 6);
198 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(7), 7);
202 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(8), 8);
206 amd_pmf_smartpc_apply_bios_output(dev, val, BIT(9), 9);
212 static int amd_pmf_invoke_cmd_enact(struct amd_pmf_dev *dev)
221 if (!dev->tee_ctx)
224 memset(dev->shbuf, 0, dev->policy_sz);
225 ta_sm = dev->shbuf;
233 amd_pmf_populate_ta_inputs(dev, in);
234 amd_pmf_prepare_args(dev, TA_PMF_COMMAND_POLICY_BUILDER_ENACT_POLICIES, &arg, param);
236 ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
238 dev_err(dev->dev, "TEE enact cmd failed. err: %x, ret:%d\n", arg.ret, ret);
243 amd_pmf_dump_ta_inputs(dev, in);
244 dev_dbg(dev->dev, "action count:%u result:%x\n", out->actions_count,
246 amd_pmf_apply_policies(dev, out);
252 static int amd_pmf_invoke_cmd_init(struct amd_pmf_dev *dev)
260 if (!dev->tee_ctx) {
261 dev_err(dev->dev, "Failed to get TEE context\n");
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;
277 in->policies_table.table_size = dev->policy_sz;
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);
282 ret = tee_client_invoke_func(dev->tee_ctx, &arg, param);
284 dev_err(dev->dev, "Failed to invoke TEE init cmd. err: %x, ret:%d\n", arg.ret, ret);
293 struct amd_pmf_dev *dev = container_of(work, struct amd_pmf_dev, pb_work.work);
295 amd_pmf_invoke_cmd_enact(dev);
296 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms));
299 static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)
304 if (dev->policy_sz < POLICY_COOKIE_OFFSET + sizeof(*header))
307 header = (struct cookie_header *)(dev->policy_buf + POLICY_COOKIE_OFFSET);
310 dev_dbg(dev->dev, "cookie doesn't match\n");
314 if (dev->policy_sz < header->length + 512)
318 dev->policy_sz = header->length + 512;
319 res = amd_pmf_invoke_cmd_init(dev);
322 dev->smart_pc_enabled = true;
327 schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms * 3));
329 dev_dbg(dev->dev, "ta invoke cmd init failed err: %x\n", res);
330 dev->smart_pc_enabled = false;
337 static inline bool amd_pmf_pb_valid(struct amd_pmf_dev *dev)
339 return memchr_inv(dev->policy_buf, 0xff, dev->policy_sz);
343 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev)
345 print_hex_dump_debug("(pb): ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf,
346 dev->policy_sz, false);
352 struct amd_pmf_dev *dev = filp->private_data;
361 new_policy_buf = devm_kzalloc(dev->dev, length, GFP_KERNEL);
366 devm_kfree(dev->dev, new_policy_buf);
370 devm_kfree(dev->dev, dev->policy_buf);
371 dev->policy_buf = new_policy_buf;
372 dev->policy_sz = length;
374 if (!amd_pmf_pb_valid(dev))
377 amd_pmf_hex_dump_pb(dev);
378 ret = amd_pmf_start_policy_engine(dev);
390 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root)
392 dev->esbin = debugfs_create_dir("pb", debugfs_root);
393 debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops);
396 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev)
398 debugfs_remove_recursive(dev->esbin);
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) {}
431 static int amd_pmf_register_input_device(struct amd_pmf_dev *dev)
435 dev->pmf_idev = devm_input_allocate_device(dev->dev);
436 if (!dev->pmf_idev)
439 dev->pmf_idev->name = "PMF-TA output events";
440 dev->pmf_idev->phys = "amd-pmf/input0";
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);
446 err = input_register_device(dev->pmf_idev);
448 dev_err(dev->dev, "Failed to register input device: %d\n", err);
455 static int amd_pmf_tee_init(struct amd_pmf_dev *dev, const uuid_t *uuid)
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;
468 ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id, uuid);
470 dev_err(dev->dev, "Failed to open TA session (%d)\n", ret);
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);
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);
489 dev_dbg(dev->dev, "TEE init done\n");
494 tee_shm_free(dev->fw_shm_pool);
496 tee_client_close_session(dev->tee_ctx, dev->session_id);
498 tee_client_close_context(dev->tee_ctx);
503 static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev)
505 if (!dev->tee_ctx)
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;
513 int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
518 ret = apmf_check_smart_pc(dev);
525 dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret);
529 INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd);
531 ret = amd_pmf_set_dram_addr(dev, true);
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);
539 dev->policy_buf = devm_kzalloc(dev->dev, dev->policy_sz, GFP_KERNEL);
540 if (!dev->policy_buf)
543 memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz);
545 if (!amd_pmf_pb_valid(dev)) {
546 dev_info(dev->dev, "No Smart PC policy present\n");
550 amd_pmf_hex_dump_pb(dev);
552 dev->prev_data = devm_kzalloc(dev->dev, sizeof(*dev->prev_data), GFP_KERNEL);
553 if (!dev->prev_data)
557 ret = amd_pmf_tee_init(dev, &amd_pmf_ta_uuid[i]);
561 ret = amd_pmf_start_policy_engine(dev);
562 dev_dbg(dev->dev, "start policy engine ret: %d\n", ret);
566 amd_pmf_tee_deinit(dev);
575 amd_pmf_open_pb(dev, dev->dbgfs_dir);
577 ret = amd_pmf_register_input_device(dev);
584 amd_pmf_deinit_smart_pc(dev);
589 void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
591 if (dev->pmf_idev)
592 input_unregister_device(dev->pmf_idev);
594 if (pb_side_load && dev->esbin)
595 amd_pmf_remove_pb(dev);
597 cancel_delayed_work_sync(&dev->pb_work);
598 amd_pmf_tee_deinit(dev);