Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ACPI configfs support
4 *
5 * Copyright (c) 2016 Intel Corporation
6 */
7
8#define pr_fmt(fmt) "ACPI configfs: " fmt
9
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>
15
16#include "acpica/accommon.h"
17#include "acpica/actables.h"
18
19static struct config_group *acpi_table_group;
20
21struct acpi_table {
22 struct config_item cfg;
23 struct acpi_table_header *header;
24 u32 index;
25};
26
27static ssize_t acpi_table_aml_write(struct config_item *cfg,
28 const void *data, size_t size)
29{
30 const struct acpi_table_header *header = data;
31 struct acpi_table *table;
32 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
33
34 if (ret)
35 return ret;
36
37 table = container_of(cfg, struct acpi_table, cfg);
38
39 if (table->header) {
40 pr_err("table already loaded\n");
41 return -EBUSY;
42 }
43
44 if (header->length != size) {
45 pr_err("invalid table length\n");
46 return -EINVAL;
47 }
48
49 if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
50 pr_err("invalid table signature\n");
51 return -EINVAL;
52 }
53
54 table = container_of(cfg, struct acpi_table, cfg);
55
56 table->header = kmemdup(header, header->length, GFP_KERNEL);
57 if (!table->header)
58 return -ENOMEM;
59
60 ret = acpi_load_table(table->header, &table->index);
61 if (ret) {
62 kfree(table->header);
63 table->header = NULL;
64 }
65
66 return ret;
67}
68
69static inline struct acpi_table_header *get_header(struct config_item *cfg)
70{
71 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
72
73 if (!table->header)
74 pr_err("table not loaded\n");
75
76 return table->header;
77}
78
79static ssize_t acpi_table_aml_read(struct config_item *cfg,
80 void *data, size_t size)
81{
82 struct acpi_table_header *h = get_header(cfg);
83
84 if (!h)
85 return -EINVAL;
86
87 if (data)
88 memcpy(data, h, h->length);
89
90 return h->length;
91}
92
93#define MAX_ACPI_TABLE_SIZE (128 * 1024)
94
95CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);
96
97static struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
98 &acpi_table_attr_aml,
99 NULL,
100};
101
102static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
103{
104 struct acpi_table_header *h = get_header(cfg);
105
106 if (!h)
107 return -EINVAL;
108
109 return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature);
110}
111
112static ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
113{
114 struct acpi_table_header *h = get_header(cfg);
115
116 if (!h)
117 return -EINVAL;
118
119 return sprintf(str, "%d\n", h->length);
120}
121
122static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
123{
124 struct acpi_table_header *h = get_header(cfg);
125
126 if (!h)
127 return -EINVAL;
128
129 return sprintf(str, "%d\n", h->revision);
130}
131
132static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
133{
134 struct acpi_table_header *h = get_header(cfg);
135
136 if (!h)
137 return -EINVAL;
138
139 return sprintf(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
140}
141
142static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
143{
144 struct acpi_table_header *h = get_header(cfg);
145
146 if (!h)
147 return -EINVAL;
148
149 return sprintf(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
150}
151
152static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
153{
154 struct acpi_table_header *h = get_header(cfg);
155
156 if (!h)
157 return -EINVAL;
158
159 return sprintf(str, "%d\n", h->oem_revision);
160}
161
162static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg,
163 char *str)
164{
165 struct acpi_table_header *h = get_header(cfg);
166
167 if (!h)
168 return -EINVAL;
169
170 return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id);
171}
172
173static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
174 char *str)
175{
176 struct acpi_table_header *h = get_header(cfg);
177
178 if (!h)
179 return -EINVAL;
180
181 return sprintf(str, "%d\n", h->asl_compiler_revision);
182}
183
184CONFIGFS_ATTR_RO(acpi_table_, signature);
185CONFIGFS_ATTR_RO(acpi_table_, length);
186CONFIGFS_ATTR_RO(acpi_table_, revision);
187CONFIGFS_ATTR_RO(acpi_table_, oem_id);
188CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
189CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
190CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
191CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);
192
193static struct configfs_attribute *acpi_table_attrs[] = {
194 &acpi_table_attr_signature,
195 &acpi_table_attr_length,
196 &acpi_table_attr_revision,
197 &acpi_table_attr_oem_id,
198 &acpi_table_attr_oem_table_id,
199 &acpi_table_attr_oem_revision,
200 &acpi_table_attr_asl_compiler_id,
201 &acpi_table_attr_asl_compiler_revision,
202 NULL,
203};
204
205static const struct config_item_type acpi_table_type = {
206 .ct_owner = THIS_MODULE,
207 .ct_bin_attrs = acpi_table_bin_attrs,
208 .ct_attrs = acpi_table_attrs,
209};
210
211static struct config_item *acpi_table_make_item(struct config_group *group,
212 const char *name)
213{
214 struct acpi_table *table;
215
216 table = kzalloc(sizeof(*table), GFP_KERNEL);
217 if (!table)
218 return ERR_PTR(-ENOMEM);
219
220 config_item_init_type_name(&table->cfg, name, &acpi_table_type);
221 return &table->cfg;
222}
223
224static void acpi_table_drop_item(struct config_group *group,
225 struct config_item *cfg)
226{
227 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
228
229 ACPI_INFO(("Host-directed Dynamic ACPI Table Unload"));
230 acpi_unload_table(table->index);
231}
232
233static struct configfs_group_operations acpi_table_group_ops = {
234 .make_item = acpi_table_make_item,
235 .drop_item = acpi_table_drop_item,
236};
237
238static const struct config_item_type acpi_tables_type = {
239 .ct_owner = THIS_MODULE,
240 .ct_group_ops = &acpi_table_group_ops,
241};
242
243static const struct config_item_type acpi_root_group_type = {
244 .ct_owner = THIS_MODULE,
245};
246
247static struct configfs_subsystem acpi_configfs = {
248 .su_group = {
249 .cg_item = {
250 .ci_namebuf = "acpi",
251 .ci_type = &acpi_root_group_type,
252 },
253 },
254 .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
255};
256
257static int __init acpi_configfs_init(void)
258{
259 int ret;
260 struct config_group *root = &acpi_configfs.su_group;
261
262 config_group_init(root);
263
264 ret = configfs_register_subsystem(&acpi_configfs);
265 if (ret)
266 return ret;
267
268 acpi_table_group = configfs_register_default_group(root, "table",
269 &acpi_tables_type);
270 return PTR_ERR_OR_ZERO(acpi_table_group);
271}
272module_init(acpi_configfs_init);
273
274static void __exit acpi_configfs_exit(void)
275{
276 configfs_unregister_default_group(acpi_table_group);
277 configfs_unregister_subsystem(&acpi_configfs);
278}
279module_exit(acpi_configfs_exit);
280
281MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
282MODULE_DESCRIPTION("ACPI configfs support");
283MODULE_LICENSE("GPL v2");
1/*
2 * ACPI configfs support
3 *
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10
11#define pr_fmt(fmt) "ACPI configfs: " fmt
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/configfs.h>
16#include <linux/acpi.h>
17
18#include "acpica/accommon.h"
19#include "acpica/actables.h"
20
21static struct config_group *acpi_table_group;
22
23struct acpi_table {
24 struct config_item cfg;
25 struct acpi_table_header *header;
26 u32 index;
27};
28
29static ssize_t acpi_table_aml_write(struct config_item *cfg,
30 const void *data, size_t size)
31{
32 const struct acpi_table_header *header = data;
33 struct acpi_table *table;
34 int ret;
35
36 table = container_of(cfg, struct acpi_table, cfg);
37
38 if (table->header) {
39 pr_err("table already loaded\n");
40 return -EBUSY;
41 }
42
43 if (header->length != size) {
44 pr_err("invalid table length\n");
45 return -EINVAL;
46 }
47
48 if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
49 pr_err("invalid table signature\n");
50 return -EINVAL;
51 }
52
53 table = container_of(cfg, struct acpi_table, cfg);
54
55 table->header = kmemdup(header, header->length, GFP_KERNEL);
56 if (!table->header)
57 return -ENOMEM;
58
59 ACPI_INFO(("Host-directed Dynamic ACPI Table Load:"));
60 ret = acpi_tb_install_and_load_table(
61 ACPI_PTR_TO_PHYSADDR(table->header),
62 ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL, FALSE,
63 &table->index);
64 if (ret) {
65 kfree(table->header);
66 table->header = NULL;
67 }
68
69 return ret;
70}
71
72static inline struct acpi_table_header *get_header(struct config_item *cfg)
73{
74 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
75
76 if (!table->header)
77 pr_err("table not loaded\n");
78
79 return table->header;
80}
81
82static ssize_t acpi_table_aml_read(struct config_item *cfg,
83 void *data, size_t size)
84{
85 struct acpi_table_header *h = get_header(cfg);
86
87 if (!h)
88 return -EINVAL;
89
90 if (data)
91 memcpy(data, h, h->length);
92
93 return h->length;
94}
95
96#define MAX_ACPI_TABLE_SIZE (128 * 1024)
97
98CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);
99
100struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
101 &acpi_table_attr_aml,
102 NULL,
103};
104
105ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
106{
107 struct acpi_table_header *h = get_header(cfg);
108
109 if (!h)
110 return -EINVAL;
111
112 return sprintf(str, "%.*s\n", ACPI_NAME_SIZE, h->signature);
113}
114
115ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
116{
117 struct acpi_table_header *h = get_header(cfg);
118
119 if (!h)
120 return -EINVAL;
121
122 return sprintf(str, "%d\n", h->length);
123}
124
125ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
126{
127 struct acpi_table_header *h = get_header(cfg);
128
129 if (!h)
130 return -EINVAL;
131
132 return sprintf(str, "%d\n", h->revision);
133}
134
135ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
136{
137 struct acpi_table_header *h = get_header(cfg);
138
139 if (!h)
140 return -EINVAL;
141
142 return sprintf(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
143}
144
145ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
146{
147 struct acpi_table_header *h = get_header(cfg);
148
149 if (!h)
150 return -EINVAL;
151
152 return sprintf(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
153}
154
155ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
156{
157 struct acpi_table_header *h = get_header(cfg);
158
159 if (!h)
160 return -EINVAL;
161
162 return sprintf(str, "%d\n", h->oem_revision);
163}
164
165ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg, char *str)
166{
167 struct acpi_table_header *h = get_header(cfg);
168
169 if (!h)
170 return -EINVAL;
171
172 return sprintf(str, "%.*s\n", ACPI_NAME_SIZE, h->asl_compiler_id);
173}
174
175ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
176 char *str)
177{
178 struct acpi_table_header *h = get_header(cfg);
179
180 if (!h)
181 return -EINVAL;
182
183 return sprintf(str, "%d\n", h->asl_compiler_revision);
184}
185
186CONFIGFS_ATTR_RO(acpi_table_, signature);
187CONFIGFS_ATTR_RO(acpi_table_, length);
188CONFIGFS_ATTR_RO(acpi_table_, revision);
189CONFIGFS_ATTR_RO(acpi_table_, oem_id);
190CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
191CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
192CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
193CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);
194
195struct configfs_attribute *acpi_table_attrs[] = {
196 &acpi_table_attr_signature,
197 &acpi_table_attr_length,
198 &acpi_table_attr_revision,
199 &acpi_table_attr_oem_id,
200 &acpi_table_attr_oem_table_id,
201 &acpi_table_attr_oem_revision,
202 &acpi_table_attr_asl_compiler_id,
203 &acpi_table_attr_asl_compiler_revision,
204 NULL,
205};
206
207static const struct config_item_type acpi_table_type = {
208 .ct_owner = THIS_MODULE,
209 .ct_bin_attrs = acpi_table_bin_attrs,
210 .ct_attrs = acpi_table_attrs,
211};
212
213static struct config_item *acpi_table_make_item(struct config_group *group,
214 const char *name)
215{
216 struct acpi_table *table;
217
218 table = kzalloc(sizeof(*table), GFP_KERNEL);
219 if (!table)
220 return ERR_PTR(-ENOMEM);
221
222 config_item_init_type_name(&table->cfg, name, &acpi_table_type);
223 return &table->cfg;
224}
225
226static void acpi_table_drop_item(struct config_group *group,
227 struct config_item *cfg)
228{
229 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
230
231 ACPI_INFO(("Host-directed Dynamic ACPI Table Unload"));
232 acpi_tb_unload_table(table->index);
233}
234
235struct configfs_group_operations acpi_table_group_ops = {
236 .make_item = acpi_table_make_item,
237 .drop_item = acpi_table_drop_item,
238};
239
240static const struct config_item_type acpi_tables_type = {
241 .ct_owner = THIS_MODULE,
242 .ct_group_ops = &acpi_table_group_ops,
243};
244
245static const struct config_item_type acpi_root_group_type = {
246 .ct_owner = THIS_MODULE,
247};
248
249static struct configfs_subsystem acpi_configfs = {
250 .su_group = {
251 .cg_item = {
252 .ci_namebuf = "acpi",
253 .ci_type = &acpi_root_group_type,
254 },
255 },
256 .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
257};
258
259static int __init acpi_configfs_init(void)
260{
261 int ret;
262 struct config_group *root = &acpi_configfs.su_group;
263
264 config_group_init(root);
265
266 ret = configfs_register_subsystem(&acpi_configfs);
267 if (ret)
268 return ret;
269
270 acpi_table_group = configfs_register_default_group(root, "table",
271 &acpi_tables_type);
272 return PTR_ERR_OR_ZERO(acpi_table_group);
273}
274module_init(acpi_configfs_init);
275
276static void __exit acpi_configfs_exit(void)
277{
278 configfs_unregister_default_group(acpi_table_group);
279 configfs_unregister_subsystem(&acpi_configfs);
280}
281module_exit(acpi_configfs_exit);
282
283MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
284MODULE_DESCRIPTION("ACPI configfs support");
285MODULE_LICENSE("GPL v2");