1 // SPDX-License-Identifier: GPL-2.0-only
3 * ACPI configfs support
5 * Copyright (c) 2016 Intel Corporation
8 #define pr_fmt(fmt) "ACPI configfs: " fmt
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/configfs.h>
13 #include <linux/acpi.h>
14 #include <linux/security.h>
16 static struct config_group *acpi_table_group;
19 struct config_item cfg;
20 struct acpi_table_header *header;
24 static ssize_t acpi_table_aml_write(struct config_item *cfg,
25 const void *data, size_t size)
27 const struct acpi_table_header *header = data;
28 struct acpi_table *table;
29 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
34 table = container_of(cfg, struct acpi_table, cfg);
37 pr_err("table already loaded\n");
41 if (header->length != size) {
42 pr_err("invalid table length\n");
46 if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
47 pr_err("invalid table signature\n");
51 table = container_of(cfg, struct acpi_table, cfg);
53 table->header = kmemdup(header, header->length, GFP_KERNEL);
57 ret = acpi_load_table(table->header, &table->index);
66 static inline struct acpi_table_header *get_header(struct config_item *cfg)
68 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
71 pr_err("table not loaded\n");
73 return table->header ?: ERR_PTR(-EINVAL);
76 static ssize_t acpi_table_aml_read(struct config_item *cfg,
77 void *data, size_t size)
79 struct acpi_table_header *h = get_header(cfg);
85 memcpy(data, h, h->length);
90 #define MAX_ACPI_TABLE_SIZE (128 * 1024)
92 CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);
94 static struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
99 static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
101 struct acpi_table_header *h = get_header(cfg);
106 return sysfs_emit(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature);
109 static ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
111 struct acpi_table_header *h = get_header(cfg);
116 return sysfs_emit(str, "%d\n", h->length);
119 static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
121 struct acpi_table_header *h = get_header(cfg);
126 return sysfs_emit(str, "%d\n", h->revision);
129 static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
131 struct acpi_table_header *h = get_header(cfg);
136 return sysfs_emit(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
139 static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
141 struct acpi_table_header *h = get_header(cfg);
146 return sysfs_emit(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
149 static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
151 struct acpi_table_header *h = get_header(cfg);
156 return sysfs_emit(str, "%d\n", h->oem_revision);
159 static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg,
162 struct acpi_table_header *h = get_header(cfg);
167 return sysfs_emit(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id);
170 static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
173 struct acpi_table_header *h = get_header(cfg);
178 return sysfs_emit(str, "%d\n", h->asl_compiler_revision);
181 CONFIGFS_ATTR_RO(acpi_table_, signature);
182 CONFIGFS_ATTR_RO(acpi_table_, length);
183 CONFIGFS_ATTR_RO(acpi_table_, revision);
184 CONFIGFS_ATTR_RO(acpi_table_, oem_id);
185 CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
186 CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
187 CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
188 CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);
190 static struct configfs_attribute *acpi_table_attrs[] = {
191 &acpi_table_attr_signature,
192 &acpi_table_attr_length,
193 &acpi_table_attr_revision,
194 &acpi_table_attr_oem_id,
195 &acpi_table_attr_oem_table_id,
196 &acpi_table_attr_oem_revision,
197 &acpi_table_attr_asl_compiler_id,
198 &acpi_table_attr_asl_compiler_revision,
202 static const struct config_item_type acpi_table_type = {
203 .ct_owner = THIS_MODULE,
204 .ct_bin_attrs = acpi_table_bin_attrs,
205 .ct_attrs = acpi_table_attrs,
208 static struct config_item *acpi_table_make_item(struct config_group *group,
211 struct acpi_table *table;
213 table = kzalloc(sizeof(*table), GFP_KERNEL);
215 return ERR_PTR(-ENOMEM);
217 config_item_init_type_name(&table->cfg, name, &acpi_table_type);
221 static void acpi_table_drop_item(struct config_group *group,
222 struct config_item *cfg)
224 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
226 pr_debug("Host-directed Dynamic ACPI Table Unload\n");
227 acpi_unload_table(table->index);
228 config_item_put(cfg);
231 static struct configfs_group_operations acpi_table_group_ops = {
232 .make_item = acpi_table_make_item,
233 .drop_item = acpi_table_drop_item,
236 static const struct config_item_type acpi_tables_type = {
237 .ct_owner = THIS_MODULE,
238 .ct_group_ops = &acpi_table_group_ops,
241 static const struct config_item_type acpi_root_group_type = {
242 .ct_owner = THIS_MODULE,
245 static struct configfs_subsystem acpi_configfs = {
248 .ci_namebuf = "acpi",
249 .ci_type = &acpi_root_group_type,
252 .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
255 static int __init acpi_configfs_init(void)
258 struct config_group *root = &acpi_configfs.su_group;
260 config_group_init(root);
262 ret = configfs_register_subsystem(&acpi_configfs);
266 acpi_table_group = configfs_register_default_group(root, "table",
268 if (IS_ERR(acpi_table_group)) {
269 configfs_unregister_subsystem(&acpi_configfs);
270 return PTR_ERR(acpi_table_group);
275 module_init(acpi_configfs_init);
277 static void __exit acpi_configfs_exit(void)
279 configfs_unregister_default_group(acpi_table_group);
280 configfs_unregister_subsystem(&acpi_configfs);
282 module_exit(acpi_configfs_exit);
284 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
285 MODULE_DESCRIPTION("ACPI configfs support");
286 MODULE_LICENSE("GPL v2");