xref: /linux/drivers/platform/x86/amd/pmf/tee-if.c (revision d8310914848223de7ec04d55bd15f013f0dad803)
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 		kfree(new_policy_buf);
303 		return -EFAULT;
304 	}
305 
306 	kfree(dev->policy_buf);
307 	dev->policy_buf = new_policy_buf;
308 	dev->policy_sz = length;
309 
310 	amd_pmf_hex_dump_pb(dev);
311 	ret = amd_pmf_start_policy_engine(dev);
312 	if (ret)
313 		return -EINVAL;
314 
315 	return length;
316 }
317 
318 static const struct file_operations pb_fops = {
319 	.write = amd_pmf_get_pb_data,
320 	.open = simple_open,
321 };
322 
323 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root)
324 {
325 	dev->esbin = debugfs_create_dir("pb", debugfs_root);
326 	debugfs_create_file("update_policy", 0644, dev->esbin, dev, &pb_fops);
327 }
328 
329 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev)
330 {
331 	debugfs_remove_recursive(dev->esbin);
332 }
333 #else
334 static void amd_pmf_open_pb(struct amd_pmf_dev *dev, struct dentry *debugfs_root) {}
335 static void amd_pmf_remove_pb(struct amd_pmf_dev *dev) {}
336 static void amd_pmf_hex_dump_pb(struct amd_pmf_dev *dev) {}
337 #endif
338 
339 static int amd_pmf_get_bios_buffer(struct amd_pmf_dev *dev)
340 {
341 	dev->policy_buf = kzalloc(dev->policy_sz, GFP_KERNEL);
342 	if (!dev->policy_buf)
343 		return -ENOMEM;
344 
345 	dev->policy_base = devm_ioremap(dev->dev, dev->policy_addr, dev->policy_sz);
346 	if (!dev->policy_base)
347 		return -ENOMEM;
348 
349 	memcpy(dev->policy_buf, dev->policy_base, dev->policy_sz);
350 
351 	amd_pmf_hex_dump_pb(dev);
352 	if (pb_side_load)
353 		amd_pmf_open_pb(dev, dev->dbgfs_dir);
354 
355 	return amd_pmf_start_policy_engine(dev);
356 }
357 
358 static int amd_pmf_amdtee_ta_match(struct tee_ioctl_version_data *ver, const void *data)
359 {
360 	return ver->impl_id == TEE_IMPL_ID_AMDTEE;
361 }
362 
363 static int amd_pmf_ta_open_session(struct tee_context *ctx, u32 *id)
364 {
365 	struct tee_ioctl_open_session_arg sess_arg = {};
366 	int rc;
367 
368 	export_uuid(sess_arg.uuid, &amd_pmf_ta_uuid);
369 	sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
370 	sess_arg.num_params = 0;
371 
372 	rc = tee_client_open_session(ctx, &sess_arg, NULL);
373 	if (rc < 0 || sess_arg.ret != 0) {
374 		pr_err("Failed to open TEE session err:%#x, rc:%d\n", sess_arg.ret, rc);
375 		return rc;
376 	}
377 
378 	*id = sess_arg.session;
379 
380 	return rc;
381 }
382 
383 static int amd_pmf_tee_init(struct amd_pmf_dev *dev)
384 {
385 	u32 size;
386 	int ret;
387 
388 	dev->tee_ctx = tee_client_open_context(NULL, amd_pmf_amdtee_ta_match, NULL, NULL);
389 	if (IS_ERR(dev->tee_ctx)) {
390 		dev_err(dev->dev, "Failed to open TEE context\n");
391 		return PTR_ERR(dev->tee_ctx);
392 	}
393 
394 	ret = amd_pmf_ta_open_session(dev->tee_ctx, &dev->session_id);
395 	if (ret) {
396 		dev_err(dev->dev, "Failed to open TA session (%d)\n", ret);
397 		ret = -EINVAL;
398 		goto out_ctx;
399 	}
400 
401 	size = sizeof(struct ta_pmf_shared_memory) + dev->policy_sz;
402 	dev->fw_shm_pool = tee_shm_alloc_kernel_buf(dev->tee_ctx, size);
403 	if (IS_ERR(dev->fw_shm_pool)) {
404 		dev_err(dev->dev, "Failed to alloc TEE shared memory\n");
405 		ret = PTR_ERR(dev->fw_shm_pool);
406 		goto out_sess;
407 	}
408 
409 	dev->shbuf = tee_shm_get_va(dev->fw_shm_pool, 0);
410 	if (IS_ERR(dev->shbuf)) {
411 		dev_err(dev->dev, "Failed to get TEE virtual address\n");
412 		ret = PTR_ERR(dev->shbuf);
413 		goto out_shm;
414 	}
415 	dev_dbg(dev->dev, "TEE init done\n");
416 
417 	return 0;
418 
419 out_shm:
420 	tee_shm_free(dev->fw_shm_pool);
421 out_sess:
422 	tee_client_close_session(dev->tee_ctx, dev->session_id);
423 out_ctx:
424 	tee_client_close_context(dev->tee_ctx);
425 
426 	return ret;
427 }
428 
429 static void amd_pmf_tee_deinit(struct amd_pmf_dev *dev)
430 {
431 	tee_shm_free(dev->fw_shm_pool);
432 	tee_client_close_session(dev->tee_ctx, dev->session_id);
433 	tee_client_close_context(dev->tee_ctx);
434 }
435 
436 int amd_pmf_init_smart_pc(struct amd_pmf_dev *dev)
437 {
438 	int ret;
439 
440 	ret = apmf_check_smart_pc(dev);
441 	if (ret) {
442 		/*
443 		 * Lets not return from here if Smart PC bit is not advertised in
444 		 * the BIOS. This way, there will be some amount of power savings
445 		 * to the user with static slider (if enabled).
446 		 */
447 		dev_info(dev->dev, "PMF Smart PC not advertised in BIOS!:%d\n", ret);
448 		return -ENODEV;
449 	}
450 
451 	ret = amd_pmf_tee_init(dev);
452 	if (ret)
453 		return ret;
454 
455 	INIT_DELAYED_WORK(&dev->pb_work, amd_pmf_invoke_cmd);
456 	amd_pmf_set_dram_addr(dev, true);
457 	amd_pmf_get_bios_buffer(dev);
458 	dev->prev_data = kzalloc(sizeof(*dev->prev_data), GFP_KERNEL);
459 	if (!dev->prev_data)
460 		return -ENOMEM;
461 
462 	return dev->smart_pc_enabled;
463 }
464 
465 void amd_pmf_deinit_smart_pc(struct amd_pmf_dev *dev)
466 {
467 	if (pb_side_load)
468 		amd_pmf_remove_pb(dev);
469 
470 	kfree(dev->prev_data);
471 	kfree(dev->policy_buf);
472 	cancel_delayed_work_sync(&dev->pb_work);
473 	amd_pmf_tee_deinit(dev);
474 }
475