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) */
19static int pb_actions_ms = MSEC_PER_SEC;
20/* Sideload policy binaries to debug policy failures */
21static bool pb_side_load;
22
23#ifdef CONFIG_AMD_PMF_DEBUG
24module_param(pb_actions_ms, int, 0644);
25MODULE_PARM_DESC(pb_actions_ms, "Policy binary actions sampling frequency (default = 1000ms)");
26module_param(pb_side_load, bool, 0444);
27MODULE_PARM_DESC(pb_side_load, "Sideload policy binaries debug policy failures");
28#endif
29
30static const uuid_t amd_pmf_ta_uuid = UUID_INIT(0x6fd93b77, 0x3fb8, 0x524d,
31						0xb1, 0x2d, 0xc5, 0x29, 0xb1, 0x3d, 0x85, 0x43);
32
33static 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
47static 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
65static 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
79static 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
160static 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
200static 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
239static 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
247static int amd_pmf_start_policy_engine(struct amd_pmf_dev *dev)
248{
249	struct cookie_header *header;
250	int res;
251
252	if (dev->policy_sz < POLICY_COOKIE_OFFSET + sizeof(*header))
253		return -EINVAL;
254
255	header = (struct cookie_header *)(dev->policy_buf + POLICY_COOKIE_OFFSET);
256
257	if (header->sign != POLICY_SIGN_COOKIE || !header->length) {
258		dev_dbg(dev->dev, "cookie doesn't match\n");
259		return -EINVAL;
260	}
261
262	if (dev->policy_sz < header->length + 512)
263		return -EINVAL;
264
265	/* Update the actual length */
266	dev->policy_sz = header->length + 512;
267	res = amd_pmf_invoke_cmd_init(dev);
268	if (res == TA_PMF_TYPE_SUCCESS) {
269		/* Now its safe to announce that smart pc is enabled */
270		dev->smart_pc_enabled = true;
271		/*
272		 * Start collecting the data from TA FW after a small delay
273		 * or else, we might end up getting stale values.
274		 */
275		schedule_delayed_work(&dev->pb_work, msecs_to_jiffies(pb_actions_ms * 3));
276	} else {
277		dev_err(dev->dev, "ta invoke cmd init failed err: %x\n", res);
278		dev->smart_pc_enabled = false;
279		return -EIO;
280	}
281
282	return 0;
283}
284
285#ifdef CONFIG_AMD_PMF_DEBUG
286static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev)
287{
288	print_hex_dump_debug("(pb):  ", DUMP_PREFIX_OFFSET, 16, 1, dev->policy_buf,
289			     dev->policy_sz, false);
290}
291
292static ssize_t amd_pmf_get_pb_data(struct file *filp, const char __user *buf,
293				   size_t length, loff_t *pos)
294{
295	struct amd_pmf_dev *dev = filp->private_data;
296	unsigned char *new_policy_buf;
297	int ret;
298
299	/* Policy binary size cannot exceed POLICY_BUF_MAX_SZ */
300	if (length > POLICY_BUF_MAX_SZ || length == 0)
301		return -EINVAL;
302
303	/* re-alloc to the new buffer length of the policy binary */
304	new_policy_buf = kzalloc(length, GFP_KERNEL);
305	if (!new_policy_buf)
306		return -ENOMEM;
307
308	if (copy_from_user(new_policy_buf, buf, length)) {
309		kfree(new_policy_buf);
310		return -EFAULT;
311	}
312
313	kfree(dev->policy_buf);
314	dev->policy_buf = new_policy_buf;
315	dev->policy_sz = length;
316
317	amd_pmf_hex_dump_pb(dev);
318	ret = amd_pmf_start_policy_engine(dev);
319	if (ret < 0)
320		return ret;
321
322	return length;
323}
324
325static const struct file_operations pb_fops = {
326	.write = amd_pmf_get_pb_data,
327	.open = simple_open,
328};
329
330static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root)
331{
332	dev->esbin = debugfs_create_dir("pb", debugfs_root);
333	debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops);
334}
335
336static void amd_pmf_remove_pb(struct amd_pmf_dev *dev)
337{
338	debugfs_remove_recursive(dev->esbin);
339}
340#else
341static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) {}
342static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {}
343static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {}
344#endif
345
346static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data)
347{
348	return ver->impl_id == TEE_IMPL_ID_AMDTEE;
349}
350
351static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id)
352{
353	struct tee_ioctl_open_session_arg sess_arg = {};
354	int rc;
355
356	export_uuid(sess_arg.uuid, &amd_pmf_ta_uuid);
357	sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
358	sess_arg.num_params = 0;
359
360	rc = tee_client_open_session(ctx, &sess_arg, NULL);
361	if (rc < 0 || sess_arg.ret != 0) {
362		pr_err("Failed to open TEE session err:%#x, rc:%d\n", sess_arg.ret, rc);
363		return rc;
364	}
365
366	*id = sess_arg.session;
367
368	return rc;
369}
370
371static int amd_pmf_tee_init(struct amd_pmf_dev *dev)
372{
373	u32 size;
374	int ret;
375
376	dev->tee_ctx = tee_client_open_context(NULL, amd_pmf_amdtee_ta_match, NULL, NULL);
377	if (IS_ERR(dev->tee_ctx)) {
378		dev_err(dev->dev, "Failed to open TEE context\n");
379		return PTR_ERR(dev->tee_ctx);
380	}
381
382	ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id);
383	if (ret) {
384		dev_err(dev->dev, "Failed to open TA session (%d)\n", ret);
385		ret = -EINVAL;
386		goto out_ctx;
387	}
388
389	size = sizeof(struct ta_pmf_shared_memory) + dev->policy_sz;
390	dev->fw_shm_pool = tee_shm_alloc_kernel_buf(dev->tee_ctx, size);
391	if (IS_ERR(dev->fw_shm_pool)) {
392		dev_err(dev->dev, "Failed to alloc TEE shared memory\n");
393		ret = PTR_ERR(dev->fw_shm_pool);
394		goto out_sess;
395	}
396
397	dev->shbuf = tee_shm_get_va(dev->fw_shm_pool, 0);
398	if (IS_ERR(dev->shbuf)) {
399		dev_err(dev->dev, "Failed to get TEE virtual address\n");
400		ret = PTR_ERR(dev->shbuf);
401		goto out_shm;
402	}
403	dev_dbg(dev->dev, "TEE init done\n");
404
405	return 0;
406
407out_shm:
408	tee_shm_free(dev->fw_shm_pool);
409out_sess:
410	tee_client_close_session(dev->tee_ctx, dev->session_id);
411out_ctx:
412	tee_client_close_context(dev->tee_ctx);
413
414	return ret;
415}
416
417static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev)
418{
419	tee_shm_free(dev->fw_shm_pool);
420	tee_client_close_session(dev->tee_ctx, dev->session_id);
421	tee_client_close_context(dev->tee_ctx);
422}
423
424int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
425{
426	int ret;
427
428	ret = apmf_check_smart_pc(dev);
429	if (ret) {
430		/*
431		 * Lets not return from here if Smart PC bit is not advertised in
432		 * the BIOS. This way, there will be some amount of power savings
433		 * to the user with static slider (if enabled).
434		 */
435		dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret);
436		return -ENODEV;
437	}
438
439	ret = amd_pmf_tee_init(dev);
440	if (ret)
441		return ret;
442
443	INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd);
444
445	ret = amd_pmf_set_dram_addr(dev, true);
446	if (ret)
447		goto error;
448
449	dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz);
450	if (!dev->policy_base) {
451		ret = -ENOMEM;
452		goto error;
453	}
454
455	dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL);
456	if (!dev->policy_buf) {
457		ret = -ENOMEM;
458		goto error;
459	}
460
461	memcpy_fromio(dev->policy_buf, dev->policy_base, dev->policy_sz);
462
463	amd_pmf_hex_dump_pb(dev);
464
465	dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL);
466	if (!dev->prev_data) {
467		ret = -ENOMEM;
468		goto error;
469	}
470
471	ret = amd_pmf_start_policy_engine(dev);
472	if (ret)
473		goto error;
474
475	if (pb_side_load)
476		amd_pmf_open_pb(dev, dev->dbgfs_dir);
477
478	return 0;
479
480error:
481	amd_pmf_deinit_smart_pc(dev);
482
483	return ret;
484}
485
486void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
487{
488	if (pb_side_load && dev->esbin)
489		amd_pmf_remove_pb(dev);
490
491	cancel_delayed_work_sync(&dev->pb_work);
492	kfree(dev->prev_data);
493	dev->prev_data = NULL;
494	kfree(dev->policy_buf);
495	dev->policy_buf = NULL;
496	kfree(dev->buf);
497	dev->buf = NULL;
498	amd_pmf_tee_deinit(dev);
499}
500