blob: 57d9d574d4dde6e448e83d12f3f66265fbc13398 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Octavian Purdila0bf54fc2016-07-08 19:13:13 +03002/*
3 * ACPI configfs support
4 *
5 * Copyright (c) 2016 Intel Corporation
Octavian Purdila0bf54fc2016-07-08 19:13:13 +03006 */
7
Octavian Purdila612bd012016-07-08 19:13:14 +03008#define pr_fmt(fmt) "ACPI configfs: " fmt
9
Octavian Purdila0bf54fc2016-07-08 19:13:13 +030010#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/configfs.h>
13#include <linux/acpi.h>
14
Jan Kiszka772bf1e2017-06-09 20:36:31 +020015#include "acpica/accommon.h"
16#include "acpica/actables.h"
17
Octavian Purdila612bd012016-07-08 19:13:14 +030018static struct config_group *acpi_table_group;
19
20struct acpi_table {
21 struct config_item cfg;
22 struct acpi_table_header *header;
Jan Kiszka772bf1e2017-06-09 20:36:31 +020023 u32 index;
Octavian Purdila612bd012016-07-08 19:13:14 +030024};
25
26static ssize_t acpi_table_aml_write(struct config_item *cfg,
27 const void *data, size_t size)
28{
29 const struct acpi_table_header *header = data;
30 struct acpi_table *table;
31 int ret;
32
33 table = container_of(cfg, struct acpi_table, cfg);
34
35 if (table->header) {
36 pr_err("table already loaded\n");
37 return -EBUSY;
38 }
39
40 if (header->length != size) {
41 pr_err("invalid table length\n");
42 return -EINVAL;
43 }
44
45 if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
46 pr_err("invalid table signature\n");
47 return -EINVAL;
48 }
49
50 table = container_of(cfg, struct acpi_table, cfg);
51
52 table->header = kmemdup(header, header->length, GFP_KERNEL);
53 if (!table->header)
54 return -ENOMEM;
55
Nikolaus Vossd06c47e2019-06-12 10:36:06 +020056 ret = acpi_load_table(table->header);
Octavian Purdila612bd012016-07-08 19:13:14 +030057 if (ret) {
58 kfree(table->header);
59 table->header = NULL;
60 }
61
62 return ret;
63}
64
65static inline struct acpi_table_header *get_header(struct config_item *cfg)
66{
67 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
68
69 if (!table->header)
70 pr_err("table not loaded\n");
71
72 return table->header;
73}
74
75static ssize_t acpi_table_aml_read(struct config_item *cfg,
76 void *data, size_t size)
77{
78 struct acpi_table_header *h = get_header(cfg);
79
80 if (!h)
81 return -EINVAL;
82
83 if (data)
84 memcpy(data, h, h->length);
85
86 return h->length;
87}
88
89#define MAX_ACPI_TABLE_SIZE (128 * 1024)
90
91CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);
92
Andy Shevchenkobf567dd2019-03-11 16:04:30 +020093static struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
Octavian Purdila612bd012016-07-08 19:13:14 +030094 &acpi_table_attr_aml,
95 NULL,
96};
97
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +020098static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +030099{
100 struct acpi_table_header *h = get_header(cfg);
101
102 if (!h)
103 return -EINVAL;
104
Bob Moore32786752019-04-08 13:42:25 -0700105 return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature);
Octavian Purdila612bd012016-07-08 19:13:14 +0300106}
107
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +0200108static ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +0300109{
110 struct acpi_table_header *h = get_header(cfg);
111
112 if (!h)
113 return -EINVAL;
114
115 return sprintf(str, "%d\n", h->length);
116}
117
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +0200118static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +0300119{
120 struct acpi_table_header *h = get_header(cfg);
121
122 if (!h)
123 return -EINVAL;
124
125 return sprintf(str, "%d\n", h->revision);
126}
127
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +0200128static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +0300129{
130 struct acpi_table_header *h = get_header(cfg);
131
132 if (!h)
133 return -EINVAL;
134
135 return sprintf(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
136}
137
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +0200138static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +0300139{
140 struct acpi_table_header *h = get_header(cfg);
141
142 if (!h)
143 return -EINVAL;
144
145 return sprintf(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
146}
147
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +0200148static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +0300149{
150 struct acpi_table_header *h = get_header(cfg);
151
152 if (!h)
153 return -EINVAL;
154
155 return sprintf(str, "%d\n", h->oem_revision);
156}
157
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +0200158static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg,
159 char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +0300160{
161 struct acpi_table_header *h = get_header(cfg);
162
163 if (!h)
164 return -EINVAL;
165
Bob Moore32786752019-04-08 13:42:25 -0700166 return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id);
Octavian Purdila612bd012016-07-08 19:13:14 +0300167}
168
Andy Shevchenkoc62c15a2019-03-11 16:04:29 +0200169static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
170 char *str)
Octavian Purdila612bd012016-07-08 19:13:14 +0300171{
172 struct acpi_table_header *h = get_header(cfg);
173
174 if (!h)
175 return -EINVAL;
176
177 return sprintf(str, "%d\n", h->asl_compiler_revision);
178}
179
180CONFIGFS_ATTR_RO(acpi_table_, signature);
181CONFIGFS_ATTR_RO(acpi_table_, length);
182CONFIGFS_ATTR_RO(acpi_table_, revision);
183CONFIGFS_ATTR_RO(acpi_table_, oem_id);
184CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
185CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
186CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
187CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);
188
Andy Shevchenkobf567dd2019-03-11 16:04:30 +0200189static struct configfs_attribute *acpi_table_attrs[] = {
Octavian Purdila612bd012016-07-08 19:13:14 +0300190 &acpi_table_attr_signature,
191 &acpi_table_attr_length,
192 &acpi_table_attr_revision,
193 &acpi_table_attr_oem_id,
194 &acpi_table_attr_oem_table_id,
195 &acpi_table_attr_oem_revision,
196 &acpi_table_attr_asl_compiler_id,
197 &acpi_table_attr_asl_compiler_revision,
198 NULL,
199};
200
Bhumika Goyalc1e95082017-10-16 17:18:48 +0200201static const struct config_item_type acpi_table_type = {
Octavian Purdila612bd012016-07-08 19:13:14 +0300202 .ct_owner = THIS_MODULE,
203 .ct_bin_attrs = acpi_table_bin_attrs,
204 .ct_attrs = acpi_table_attrs,
205};
206
207static struct config_item *acpi_table_make_item(struct config_group *group,
208 const char *name)
209{
210 struct acpi_table *table;
211
212 table = kzalloc(sizeof(*table), GFP_KERNEL);
213 if (!table)
214 return ERR_PTR(-ENOMEM);
215
216 config_item_init_type_name(&table->cfg, name, &acpi_table_type);
217 return &table->cfg;
218}
219
Jan Kiszka772bf1e2017-06-09 20:36:31 +0200220static void acpi_table_drop_item(struct config_group *group,
221 struct config_item *cfg)
222{
223 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
224
225 ACPI_INFO(("Host-directed Dynamic ACPI Table Unload"));
226 acpi_tb_unload_table(table->index);
227}
228
Andy Shevchenkobf567dd2019-03-11 16:04:30 +0200229static struct configfs_group_operations acpi_table_group_ops = {
Octavian Purdila612bd012016-07-08 19:13:14 +0300230 .make_item = acpi_table_make_item,
Jan Kiszka772bf1e2017-06-09 20:36:31 +0200231 .drop_item = acpi_table_drop_item,
Octavian Purdila612bd012016-07-08 19:13:14 +0300232};
233
Bhumika Goyalc1e95082017-10-16 17:18:48 +0200234static const struct config_item_type acpi_tables_type = {
Octavian Purdila612bd012016-07-08 19:13:14 +0300235 .ct_owner = THIS_MODULE,
236 .ct_group_ops = &acpi_table_group_ops,
237};
238
Bhumika Goyalc1e95082017-10-16 17:18:48 +0200239static const struct config_item_type acpi_root_group_type = {
Octavian Purdila0bf54fc2016-07-08 19:13:13 +0300240 .ct_owner = THIS_MODULE,
241};
242
243static struct configfs_subsystem acpi_configfs = {
244 .su_group = {
245 .cg_item = {
246 .ci_namebuf = "acpi",
247 .ci_type = &acpi_root_group_type,
248 },
249 },
250 .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
251};
252
253static int __init acpi_configfs_init(void)
254{
255 int ret;
256 struct config_group *root = &acpi_configfs.su_group;
257
258 config_group_init(root);
259
260 ret = configfs_register_subsystem(&acpi_configfs);
261 if (ret)
262 return ret;
263
Octavian Purdila612bd012016-07-08 19:13:14 +0300264 acpi_table_group = configfs_register_default_group(root, "table",
265 &acpi_tables_type);
266 return PTR_ERR_OR_ZERO(acpi_table_group);
Octavian Purdila0bf54fc2016-07-08 19:13:13 +0300267}
268module_init(acpi_configfs_init);
269
270static void __exit acpi_configfs_exit(void)
271{
Octavian Purdila612bd012016-07-08 19:13:14 +0300272 configfs_unregister_default_group(acpi_table_group);
Octavian Purdila0bf54fc2016-07-08 19:13:13 +0300273 configfs_unregister_subsystem(&acpi_configfs);
274}
275module_exit(acpi_configfs_exit);
276
277MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
278MODULE_DESCRIPTION("ACPI configfs support");
279MODULE_LICENSE("GPL v2");