Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Octavian Purdila | 0bf54fc | 2016-07-08 19:13:13 +0300 | [diff] [blame] | 2 | /* |
| 3 | * ACPI configfs support |
| 4 | * |
| 5 | * Copyright (c) 2016 Intel Corporation |
Octavian Purdila | 0bf54fc | 2016-07-08 19:13:13 +0300 | [diff] [blame] | 6 | */ |
| 7 | |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 8 | #define pr_fmt(fmt) "ACPI configfs: " fmt |
| 9 | |
Octavian Purdila | 0bf54fc | 2016-07-08 19:13:13 +0300 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/configfs.h> |
| 13 | #include <linux/acpi.h> |
Jason A. Donenfeld | 75b0cea | 2020-06-15 04:43:32 -0600 | [diff] [blame] | 14 | #include <linux/security.h> |
Octavian Purdila | 0bf54fc | 2016-07-08 19:13:13 +0300 | [diff] [blame] | 15 | |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 16 | static struct config_group *acpi_table_group; |
| 17 | |
| 18 | struct acpi_table { |
| 19 | struct config_item cfg; |
| 20 | struct acpi_table_header *header; |
Jan Kiszka | 772bf1e | 2017-06-09 20:36:31 +0200 | [diff] [blame] | 21 | u32 index; |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static ssize_t acpi_table_aml_write(struct config_item *cfg, |
| 25 | const void *data, size_t size) |
| 26 | { |
| 27 | const struct acpi_table_header *header = data; |
| 28 | struct acpi_table *table; |
Jason A. Donenfeld | 75b0cea | 2020-06-15 04:43:32 -0600 | [diff] [blame] | 29 | int ret = security_locked_down(LOCKDOWN_ACPI_TABLES); |
| 30 | |
| 31 | if (ret) |
| 32 | return ret; |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 33 | |
| 34 | table = container_of(cfg, struct acpi_table, cfg); |
| 35 | |
| 36 | if (table->header) { |
| 37 | pr_err("table already loaded\n"); |
| 38 | return -EBUSY; |
| 39 | } |
| 40 | |
| 41 | if (header->length != size) { |
| 42 | pr_err("invalid table length\n"); |
| 43 | return -EINVAL; |
| 44 | } |
| 45 | |
| 46 | if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) { |
| 47 | pr_err("invalid table signature\n"); |
| 48 | return -EINVAL; |
| 49 | } |
| 50 | |
| 51 | table = container_of(cfg, struct acpi_table, cfg); |
| 52 | |
| 53 | table->header = kmemdup(header, header->length, GFP_KERNEL); |
| 54 | if (!table->header) |
| 55 | return -ENOMEM; |
| 56 | |
Nikolaus Voss | 1770093 | 2019-10-25 14:36:53 -0700 | [diff] [blame] | 57 | ret = acpi_load_table(table->header, &table->index); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 58 | if (ret) { |
| 59 | kfree(table->header); |
| 60 | table->header = NULL; |
| 61 | } |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static inline struct acpi_table_header *get_header(struct config_item *cfg) |
| 67 | { |
| 68 | struct acpi_table *table = container_of(cfg, struct acpi_table, cfg); |
| 69 | |
| 70 | if (!table->header) |
| 71 | pr_err("table not loaded\n"); |
| 72 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 73 | return table->header ?: ERR_PTR(-EINVAL); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static ssize_t acpi_table_aml_read(struct config_item *cfg, |
| 77 | void *data, size_t size) |
| 78 | { |
| 79 | struct acpi_table_header *h = get_header(cfg); |
| 80 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 81 | if (IS_ERR(h)) |
| 82 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 83 | |
| 84 | if (data) |
| 85 | memcpy(data, h, h->length); |
| 86 | |
| 87 | return h->length; |
| 88 | } |
| 89 | |
| 90 | #define MAX_ACPI_TABLE_SIZE (128 * 1024) |
| 91 | |
| 92 | CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE); |
| 93 | |
Andy Shevchenko | bf567dd | 2019-03-11 16:04:30 +0200 | [diff] [blame] | 94 | static struct configfs_bin_attribute *acpi_table_bin_attrs[] = { |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 95 | &acpi_table_attr_aml, |
| 96 | NULL, |
| 97 | }; |
| 98 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 99 | static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 100 | { |
| 101 | struct acpi_table_header *h = get_header(cfg); |
| 102 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 103 | if (IS_ERR(h)) |
| 104 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 105 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 106 | return sysfs_emit(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 107 | } |
| 108 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 109 | static ssize_t acpi_table_length_show(struct config_item *cfg, char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 110 | { |
| 111 | struct acpi_table_header *h = get_header(cfg); |
| 112 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 113 | if (IS_ERR(h)) |
| 114 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 115 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 116 | return sysfs_emit(str, "%d\n", h->length); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 117 | } |
| 118 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 119 | static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 120 | { |
| 121 | struct acpi_table_header *h = get_header(cfg); |
| 122 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 123 | if (IS_ERR(h)) |
| 124 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 125 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 126 | return sysfs_emit(str, "%d\n", h->revision); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 127 | } |
| 128 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 129 | static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 130 | { |
| 131 | struct acpi_table_header *h = get_header(cfg); |
| 132 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 133 | if (IS_ERR(h)) |
| 134 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 135 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 136 | return sysfs_emit(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 137 | } |
| 138 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 139 | static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 140 | { |
| 141 | struct acpi_table_header *h = get_header(cfg); |
| 142 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 143 | if (IS_ERR(h)) |
| 144 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 145 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 146 | return sysfs_emit(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 147 | } |
| 148 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 149 | static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 150 | { |
| 151 | struct acpi_table_header *h = get_header(cfg); |
| 152 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 153 | if (IS_ERR(h)) |
| 154 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 155 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 156 | return sysfs_emit(str, "%d\n", h->oem_revision); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 157 | } |
| 158 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 159 | static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg, |
| 160 | char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 161 | { |
| 162 | struct acpi_table_header *h = get_header(cfg); |
| 163 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 164 | if (IS_ERR(h)) |
| 165 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 166 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 167 | return sysfs_emit(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 168 | } |
| 169 | |
Andy Shevchenko | c62c15a | 2019-03-11 16:04:29 +0200 | [diff] [blame] | 170 | static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg, |
| 171 | char *str) |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 172 | { |
| 173 | struct acpi_table_header *h = get_header(cfg); |
| 174 | |
Andy Shevchenko | 45c16fe | 2021-07-15 17:16:51 +0300 | [diff] [blame] | 175 | if (IS_ERR(h)) |
| 176 | return PTR_ERR(h); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 177 | |
Andy Shevchenko | ae57338 | 2021-07-15 17:16:50 +0300 | [diff] [blame] | 178 | return sysfs_emit(str, "%d\n", h->asl_compiler_revision); |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 179 | } |
| 180 | |
| 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); |
| 189 | |
Andy Shevchenko | bf567dd | 2019-03-11 16:04:30 +0200 | [diff] [blame] | 190 | static struct configfs_attribute *acpi_table_attrs[] = { |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 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, |
| 199 | NULL, |
| 200 | }; |
| 201 | |
Bhumika Goyal | c1e9508 | 2017-10-16 17:18:48 +0200 | [diff] [blame] | 202 | static const struct config_item_type acpi_table_type = { |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 203 | .ct_owner = THIS_MODULE, |
| 204 | .ct_bin_attrs = acpi_table_bin_attrs, |
| 205 | .ct_attrs = acpi_table_attrs, |
| 206 | }; |
| 207 | |
| 208 | static struct config_item *acpi_table_make_item(struct config_group *group, |
| 209 | const char *name) |
| 210 | { |
| 211 | struct acpi_table *table; |
| 212 | |
| 213 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
| 214 | if (!table) |
| 215 | return ERR_PTR(-ENOMEM); |
| 216 | |
| 217 | config_item_init_type_name(&table->cfg, name, &acpi_table_type); |
| 218 | return &table->cfg; |
| 219 | } |
| 220 | |
Jan Kiszka | 772bf1e | 2017-06-09 20:36:31 +0200 | [diff] [blame] | 221 | static void acpi_table_drop_item(struct config_group *group, |
| 222 | struct config_item *cfg) |
| 223 | { |
| 224 | struct acpi_table *table = container_of(cfg, struct acpi_table, cfg); |
| 225 | |
Hanjun Guo | be7ae56 | 2021-05-24 17:41:05 +0800 | [diff] [blame] | 226 | pr_debug("Host-directed Dynamic ACPI Table Unload\n"); |
Nikolaus Voss | 1770093 | 2019-10-25 14:36:53 -0700 | [diff] [blame] | 227 | acpi_unload_table(table->index); |
Hanjun Guo | 9a2e849 | 2020-09-18 17:13:28 +0800 | [diff] [blame] | 228 | config_item_put(cfg); |
Jan Kiszka | 772bf1e | 2017-06-09 20:36:31 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Andy Shevchenko | bf567dd | 2019-03-11 16:04:30 +0200 | [diff] [blame] | 231 | static struct configfs_group_operations acpi_table_group_ops = { |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 232 | .make_item = acpi_table_make_item, |
Jan Kiszka | 772bf1e | 2017-06-09 20:36:31 +0200 | [diff] [blame] | 233 | .drop_item = acpi_table_drop_item, |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 234 | }; |
| 235 | |
Bhumika Goyal | c1e9508 | 2017-10-16 17:18:48 +0200 | [diff] [blame] | 236 | static const struct config_item_type acpi_tables_type = { |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 237 | .ct_owner = THIS_MODULE, |
| 238 | .ct_group_ops = &acpi_table_group_ops, |
| 239 | }; |
| 240 | |
Bhumika Goyal | c1e9508 | 2017-10-16 17:18:48 +0200 | [diff] [blame] | 241 | static const struct config_item_type acpi_root_group_type = { |
Octavian Purdila | 0bf54fc | 2016-07-08 19:13:13 +0300 | [diff] [blame] | 242 | .ct_owner = THIS_MODULE, |
| 243 | }; |
| 244 | |
| 245 | static struct configfs_subsystem acpi_configfs = { |
| 246 | .su_group = { |
| 247 | .cg_item = { |
| 248 | .ci_namebuf = "acpi", |
| 249 | .ci_type = &acpi_root_group_type, |
| 250 | }, |
| 251 | }, |
| 252 | .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex), |
| 253 | }; |
| 254 | |
| 255 | static int __init acpi_configfs_init(void) |
| 256 | { |
| 257 | int ret; |
| 258 | struct config_group *root = &acpi_configfs.su_group; |
| 259 | |
| 260 | config_group_init(root); |
| 261 | |
| 262 | ret = configfs_register_subsystem(&acpi_configfs); |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 266 | acpi_table_group = configfs_register_default_group(root, "table", |
| 267 | &acpi_tables_type); |
Qinglang Miao | 67e4005 | 2021-01-15 10:22:50 +0800 | [diff] [blame] | 268 | if (IS_ERR(acpi_table_group)) { |
| 269 | configfs_unregister_subsystem(&acpi_configfs); |
| 270 | return PTR_ERR(acpi_table_group); |
| 271 | } |
| 272 | |
| 273 | return 0; |
Octavian Purdila | 0bf54fc | 2016-07-08 19:13:13 +0300 | [diff] [blame] | 274 | } |
| 275 | module_init(acpi_configfs_init); |
| 276 | |
| 277 | static void __exit acpi_configfs_exit(void) |
| 278 | { |
Octavian Purdila | 612bd01 | 2016-07-08 19:13:14 +0300 | [diff] [blame] | 279 | configfs_unregister_default_group(acpi_table_group); |
Octavian Purdila | 0bf54fc | 2016-07-08 19:13:13 +0300 | [diff] [blame] | 280 | configfs_unregister_subsystem(&acpi_configfs); |
| 281 | } |
| 282 | module_exit(acpi_configfs_exit); |
| 283 | |
| 284 | MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>"); |
| 285 | MODULE_DESCRIPTION("ACPI configfs support"); |
| 286 | MODULE_LICENSE("GPL v2"); |