xref: /linux/drivers/acpi/acpi_fpdt.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
4  * FPDT support for exporting boot and suspend/resume performance data
5  *
6  * Copyright (C) 2021 Intel Corporation. All rights reserved.
7  */
8 
9 #define pr_fmt(fmt) "ACPI FPDT: " fmt
10 
11 #include <linux/acpi.h>
12 
13 /*
14  * FPDT contains ACPI table header and a number of fpdt_subtable_entries.
15  * Each fpdt_subtable_entry points to a subtable: FBPT or S3PT.
16  * Each FPDT subtable (FBPT/S3PT) is composed of a fpdt_subtable_header
17  * and a number of fpdt performance records.
18  * Each FPDT performance record is composed of a fpdt_record_header and
19  * performance data fields, for boot or suspend or resume phase.
20  */
21 enum fpdt_subtable_type {
22 	SUBTABLE_FBPT,
23 	SUBTABLE_S3PT,
24 };
25 
26 struct fpdt_subtable_entry {
27 	u16 type;		/* refer to enum fpdt_subtable_type */
28 	u8 length;
29 	u8 revision;
30 	u32 reserved;
31 	u64 address;		/* physical address of the S3PT/FBPT table */
32 };
33 
34 struct fpdt_subtable_header {
35 	u32 signature;
36 	u32 length;
37 };
38 
39 enum fpdt_record_type {
40 	RECORD_S3_RESUME,
41 	RECORD_S3_SUSPEND,
42 	RECORD_BOOT,
43 };
44 
45 struct fpdt_record_header {
46 	u16 type;		/* refer to enum fpdt_record_type */
47 	u8 length;
48 	u8 revision;
49 };
50 
51 struct resume_performance_record {
52 	struct fpdt_record_header header;
53 	u32 resume_count;
54 	u64 resume_prev;
55 	u64 resume_avg;
56 } __attribute__((packed));
57 
58 struct boot_performance_record {
59 	struct fpdt_record_header header;
60 	u32 reserved;
61 	u64 firmware_start;
62 	u64 bootloader_load;
63 	u64 bootloader_launch;
64 	u64 exitbootservice_start;
65 	u64 exitbootservice_end;
66 } __attribute__((packed));
67 
68 struct suspend_performance_record {
69 	struct fpdt_record_header header;
70 	u64 suspend_start;
71 	u64 suspend_end;
72 } __attribute__((packed));
73 
74 
75 static struct resume_performance_record *record_resume;
76 static struct suspend_performance_record *record_suspend;
77 static struct boot_performance_record *record_boot;
78 
79 #define FPDT_ATTR(phase, name)	\
80 static ssize_t name##_show(struct kobject *kobj,	\
81 		 struct kobj_attribute *attr, char *buf)	\
82 {	\
83 	return sprintf(buf, "%llu\n", record_##phase->name);	\
84 }	\
85 static struct kobj_attribute name##_attr =	\
86 __ATTR(name##_ns, 0444, name##_show, NULL)
87 
88 FPDT_ATTR(resume, resume_prev);
89 FPDT_ATTR(resume, resume_avg);
90 FPDT_ATTR(suspend, suspend_start);
91 FPDT_ATTR(suspend, suspend_end);
92 FPDT_ATTR(boot, firmware_start);
93 FPDT_ATTR(boot, bootloader_load);
94 FPDT_ATTR(boot, bootloader_launch);
95 FPDT_ATTR(boot, exitbootservice_start);
96 FPDT_ATTR(boot, exitbootservice_end);
97 
98 static ssize_t resume_count_show(struct kobject *kobj,
99 				 struct kobj_attribute *attr, char *buf)
100 {
101 	return sprintf(buf, "%u\n", record_resume->resume_count);
102 }
103 
104 static struct kobj_attribute resume_count_attr =
105 __ATTR_RO(resume_count);
106 
107 static struct attribute *resume_attrs[] = {
108 	&resume_count_attr.attr,
109 	&resume_prev_attr.attr,
110 	&resume_avg_attr.attr,
111 	NULL
112 };
113 
114 static const struct attribute_group resume_attr_group = {
115 	.attrs = resume_attrs,
116 	.name = "resume",
117 };
118 
119 static struct attribute *suspend_attrs[] = {
120 	&suspend_start_attr.attr,
121 	&suspend_end_attr.attr,
122 	NULL
123 };
124 
125 static const struct attribute_group suspend_attr_group = {
126 	.attrs = suspend_attrs,
127 	.name = "suspend",
128 };
129 
130 static struct attribute *boot_attrs[] = {
131 	&firmware_start_attr.attr,
132 	&bootloader_load_attr.attr,
133 	&bootloader_launch_attr.attr,
134 	&exitbootservice_start_attr.attr,
135 	&exitbootservice_end_attr.attr,
136 	NULL
137 };
138 
139 static const struct attribute_group boot_attr_group = {
140 	.attrs = boot_attrs,
141 	.name = "boot",
142 };
143 
144 static BIN_ATTR(FBPT, 0400, sysfs_bin_attr_simple_read, NULL, 0);
145 static BIN_ATTR(S3PT, 0400, sysfs_bin_attr_simple_read, NULL, 0);
146 
147 static struct kobject *fpdt_kobj;
148 
149 #if defined CONFIG_X86 && defined CONFIG_PHYS_ADDR_T_64BIT
150 #include <linux/processor.h>
151 static bool fpdt_address_valid(u64 address)
152 {
153 	/*
154 	 * On some systems the table contains invalid addresses
155 	 * with unsuppored high address bits set, check for this.
156 	 */
157 	return !(address >> boot_cpu_data.x86_phys_bits);
158 }
159 #else
160 static bool fpdt_address_valid(u64 address)
161 {
162 	return true;
163 }
164 #endif
165 
166 static int fpdt_process_subtable(u64 address, u32 subtable_type)
167 {
168 	struct fpdt_subtable_header *subtable_header;
169 	struct fpdt_record_header *record_header;
170 	char *signature = (subtable_type == SUBTABLE_FBPT ? "FBPT" : "S3PT");
171 	u32 length, offset, remaining;
172 	int result;
173 
174 	if (!fpdt_address_valid(address)) {
175 		pr_info(FW_BUG "invalid physical address: 0x%llx!\n", address);
176 		return -EINVAL;
177 	}
178 
179 	subtable_header = acpi_os_map_memory(address, sizeof(*subtable_header));
180 	if (!subtable_header)
181 		return -ENOMEM;
182 
183 	if (strncmp((char *)&subtable_header->signature, signature, 4)) {
184 		pr_info(FW_BUG "subtable signature and type mismatch!\n");
185 		acpi_os_unmap_memory(subtable_header, sizeof(*subtable_header));
186 		return -EINVAL;
187 	}
188 
189 	length = subtable_header->length;
190 	if (length < sizeof(*subtable_header)) {
191 		pr_err(FW_BUG "Invalid FPDT subtable length %u.\n", length);
192 		acpi_os_unmap_memory(subtable_header, sizeof(*subtable_header));
193 		return -EINVAL;
194 	}
195 
196 	acpi_os_unmap_memory(subtable_header, sizeof(*subtable_header));
197 
198 	subtable_header = acpi_os_map_memory(address, length);
199 	if (!subtable_header)
200 		return -ENOMEM;
201 
202 	offset = sizeof(*subtable_header);
203 	while (offset < length) {
204 		remaining = length - offset;
205 		if (remaining < sizeof(*record_header)) {
206 			pr_err(FW_BUG "Truncated FPDT record header.\n");
207 			result = -EINVAL;
208 			goto err;
209 		}
210 
211 		record_header = (void *)subtable_header + offset;
212 		if (record_header->length < sizeof(*record_header) ||
213 		    record_header->length > remaining) {
214 			pr_err(FW_BUG "Invalid FPDT record length %u.\n",
215 			       record_header->length);
216 			result = -EINVAL;
217 			goto err;
218 		}
219 		offset += record_header->length;
220 
221 		switch (record_header->type) {
222 		case RECORD_S3_RESUME:
223 			if (record_header->length < sizeof(*record_resume)) {
224 				result = -EINVAL;
225 				goto err;
226 			}
227 			if (subtable_type != SUBTABLE_S3PT) {
228 				pr_err(FW_BUG "Invalid record %d for subtable %s\n",
229 				     record_header->type, signature);
230 				result = -EINVAL;
231 				goto err;
232 			}
233 			if (record_resume) {
234 				pr_err("Duplicate resume performance record found.\n");
235 				continue;
236 			}
237 			record_resume = (struct resume_performance_record *)record_header;
238 			result = sysfs_create_group(fpdt_kobj, &resume_attr_group);
239 			if (result)
240 				goto err;
241 			break;
242 		case RECORD_S3_SUSPEND:
243 			if (record_header->length < sizeof(*record_suspend)) {
244 				result = -EINVAL;
245 				goto err;
246 			}
247 			if (subtable_type != SUBTABLE_S3PT) {
248 				pr_err(FW_BUG "Invalid %d for subtable %s\n",
249 				     record_header->type, signature);
250 				continue;
251 			}
252 			if (record_suspend) {
253 				pr_err("Duplicate suspend performance record found.\n");
254 				continue;
255 			}
256 			record_suspend = (struct suspend_performance_record *)record_header;
257 			result = sysfs_create_group(fpdt_kobj, &suspend_attr_group);
258 			if (result)
259 				goto err;
260 			break;
261 		case RECORD_BOOT:
262 			if (record_header->length < sizeof(*record_boot)) {
263 				result = -EINVAL;
264 				goto err;
265 			}
266 			if (subtable_type != SUBTABLE_FBPT) {
267 				pr_err(FW_BUG "Invalid %d for subtable %s\n",
268 				     record_header->type, signature);
269 				result = -EINVAL;
270 				goto err;
271 			}
272 			if (record_boot) {
273 				pr_err("Duplicate boot performance record found.\n");
274 				continue;
275 			}
276 			record_boot = (struct boot_performance_record *)record_header;
277 			result = sysfs_create_group(fpdt_kobj, &boot_attr_group);
278 			if (result)
279 				goto err;
280 			break;
281 
282 		default:
283 			/* Other types are reserved in ACPI 6.4 spec. */
284 			break;
285 		}
286 	}
287 
288 	if (subtable_type == SUBTABLE_FBPT) {
289 		bin_attr_FBPT.private = subtable_header;
290 		bin_attr_FBPT.size = length;
291 		result = sysfs_create_bin_file(fpdt_kobj, &bin_attr_FBPT);
292 		if (result)
293 			pr_warn("Failed to create FBPT sysfs attribute.\n");
294 	} else if (subtable_type == SUBTABLE_S3PT) {
295 		bin_attr_S3PT.private = subtable_header;
296 		bin_attr_S3PT.size = length;
297 		result = sysfs_create_bin_file(fpdt_kobj, &bin_attr_S3PT);
298 		if (result)
299 			pr_warn("Failed to create S3PT sysfs attribute.\n");
300 	}
301 
302 	return 0;
303 
304 err:
305 	if (bin_attr_FBPT.private) {
306 		sysfs_remove_bin_file(fpdt_kobj, &bin_attr_FBPT);
307 		bin_attr_FBPT.private = NULL;
308 	}
309 
310 	if (bin_attr_S3PT.private) {
311 		sysfs_remove_bin_file(fpdt_kobj, &bin_attr_S3PT);
312 		bin_attr_S3PT.private = NULL;
313 	}
314 
315 	if (record_boot)
316 		sysfs_remove_group(fpdt_kobj, &boot_attr_group);
317 
318 	if (record_suspend)
319 		sysfs_remove_group(fpdt_kobj, &suspend_attr_group);
320 
321 	if (record_resume)
322 		sysfs_remove_group(fpdt_kobj, &resume_attr_group);
323 
324 	return result;
325 }
326 
327 static int __init acpi_init_fpdt(void)
328 {
329 	acpi_status status;
330 	struct acpi_table_header *header;
331 	struct fpdt_subtable_entry *subtable;
332 	u32 offset = sizeof(*header);
333 	int result;
334 
335 	status = acpi_get_table(ACPI_SIG_FPDT, 0, &header);
336 
337 	if (ACPI_FAILURE(status))
338 		return 0;
339 
340 	fpdt_kobj = kobject_create_and_add("fpdt", acpi_kobj);
341 	if (!fpdt_kobj) {
342 		result = -ENOMEM;
343 		goto err_nomem;
344 	}
345 
346 	while (offset < header->length) {
347 		if (header->length - offset < sizeof(*subtable)) {
348 			pr_err(FW_BUG "Truncated FPDT subtable entry.\n");
349 			result = -EINVAL;
350 			goto err_subtable;
351 		}
352 
353 		subtable = (void *)header + offset;
354 		if (subtable->length < sizeof(*subtable) ||
355 		    subtable->length > header->length - offset) {
356 			pr_err(FW_BUG "Invalid FPDT subtable entry length %u.\n",
357 			       subtable->length);
358 			result = -EINVAL;
359 			goto err_subtable;
360 		}
361 
362 		switch (subtable->type) {
363 		case SUBTABLE_FBPT:
364 		case SUBTABLE_S3PT:
365 			result = fpdt_process_subtable(subtable->address,
366 					      subtable->type);
367 			if (result)
368 				goto err_subtable;
369 			break;
370 		default:
371 			/* Other types are reserved in ACPI 6.4 spec. */
372 			break;
373 		}
374 		offset += subtable->length;
375 	}
376 	return 0;
377 err_subtable:
378 	kobject_put(fpdt_kobj);
379 
380 err_nomem:
381 	acpi_put_table(header);
382 	return result;
383 }
384 
385 fs_initcall(acpi_init_fpdt);
386