acpi_configfs.c 6.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7
/*
 * ACPI configfs support
 *
 * Copyright (c) 2016 Intel Corporation
 */

8 9
#define pr_fmt(fmt) "ACPI configfs: " fmt

10 11 12 13 14
#include <linux/init.h>
#include <linux/module.h>
#include <linux/configfs.h>
#include <linux/acpi.h>

15 16 17
#include "acpica/accommon.h"
#include "acpica/actables.h"

18 19 20 21 22
static struct config_group *acpi_table_group;

struct acpi_table {
	struct config_item cfg;
	struct acpi_table_header *header;
23
	u32 index;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
};

static ssize_t acpi_table_aml_write(struct config_item *cfg,
				    const void *data, size_t size)
{
	const struct acpi_table_header *header = data;
	struct acpi_table *table;
	int ret;

	table = container_of(cfg, struct acpi_table, cfg);

	if (table->header) {
		pr_err("table already loaded\n");
		return -EBUSY;
	}

	if (header->length != size) {
		pr_err("invalid table length\n");
		return -EINVAL;
	}

	if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
		pr_err("invalid table signature\n");
		return -EINVAL;
	}

	table = container_of(cfg, struct acpi_table, cfg);

	table->header = kmemdup(header, header->length, GFP_KERNEL);
	if (!table->header)
		return -ENOMEM;

56
	ret = acpi_load_table(table->header);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	if (ret) {
		kfree(table->header);
		table->header = NULL;
	}

	return ret;
}

static inline struct acpi_table_header *get_header(struct config_item *cfg)
{
	struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);

	if (!table->header)
		pr_err("table not loaded\n");

	return table->header;
}

static ssize_t acpi_table_aml_read(struct config_item *cfg,
				   void *data, size_t size)
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

	if (data)
		memcpy(data, h, h->length);

	return h->length;
}

#define MAX_ACPI_TABLE_SIZE (128 * 1024)

CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);

93
static struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
94 95 96 97
	&acpi_table_attr_aml,
	NULL,
};

98
static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
99 100 101 102 103 104
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

105
	return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature);
106 107
}

108
static ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
109 110 111 112 113 114 115 116 117
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

	return sprintf(str, "%d\n", h->length);
}

118
static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
119 120 121 122 123 124 125 126 127
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

	return sprintf(str, "%d\n", h->revision);
}

128
static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
129 130 131 132 133 134 135 136 137
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

	return sprintf(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
}

138
static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
139 140 141 142 143 144 145 146 147
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

	return sprintf(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
}

148
static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
149 150 151 152 153 154 155 156 157
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

	return sprintf(str, "%d\n", h->oem_revision);
}

158 159
static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg,
					       char *str)
160 161 162 163 164 165
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

166
	return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id);
167 168
}

169 170
static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
						     char *str)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
	struct acpi_table_header *h = get_header(cfg);

	if (!h)
		return -EINVAL;

	return sprintf(str, "%d\n", h->asl_compiler_revision);
}

CONFIGFS_ATTR_RO(acpi_table_, signature);
CONFIGFS_ATTR_RO(acpi_table_, length);
CONFIGFS_ATTR_RO(acpi_table_, revision);
CONFIGFS_ATTR_RO(acpi_table_, oem_id);
CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);

189
static struct configfs_attribute *acpi_table_attrs[] = {
190 191 192 193 194 195 196 197 198 199 200
	&acpi_table_attr_signature,
	&acpi_table_attr_length,
	&acpi_table_attr_revision,
	&acpi_table_attr_oem_id,
	&acpi_table_attr_oem_table_id,
	&acpi_table_attr_oem_revision,
	&acpi_table_attr_asl_compiler_id,
	&acpi_table_attr_asl_compiler_revision,
	NULL,
};

201
static const struct config_item_type acpi_table_type = {
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	.ct_owner = THIS_MODULE,
	.ct_bin_attrs = acpi_table_bin_attrs,
	.ct_attrs = acpi_table_attrs,
};

static struct config_item *acpi_table_make_item(struct config_group *group,
						const char *name)
{
	struct acpi_table *table;

	table = kzalloc(sizeof(*table), GFP_KERNEL);
	if (!table)
		return ERR_PTR(-ENOMEM);

	config_item_init_type_name(&table->cfg, name, &acpi_table_type);
	return &table->cfg;
}

220 221 222 223 224 225 226 227 228
static void acpi_table_drop_item(struct config_group *group,
				 struct config_item *cfg)
{
	struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);

	ACPI_INFO(("Host-directed Dynamic ACPI Table Unload"));
	acpi_tb_unload_table(table->index);
}

229
static struct configfs_group_operations acpi_table_group_ops = {
230
	.make_item = acpi_table_make_item,
231
	.drop_item = acpi_table_drop_item,
232 233
};

234
static const struct config_item_type acpi_tables_type = {
235 236 237 238
	.ct_owner = THIS_MODULE,
	.ct_group_ops = &acpi_table_group_ops,
};

239
static const struct config_item_type acpi_root_group_type = {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	.ct_owner = THIS_MODULE,
};

static struct configfs_subsystem acpi_configfs = {
	.su_group = {
		.cg_item = {
			.ci_namebuf = "acpi",
			.ci_type = &acpi_root_group_type,
		},
	},
	.su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
};

static int __init acpi_configfs_init(void)
{
	int ret;
	struct config_group *root = &acpi_configfs.su_group;

	config_group_init(root);

	ret = configfs_register_subsystem(&acpi_configfs);
	if (ret)
		return ret;

264 265 266
	acpi_table_group = configfs_register_default_group(root, "table",
							   &acpi_tables_type);
	return PTR_ERR_OR_ZERO(acpi_table_group);
267 268 269 270 271
}
module_init(acpi_configfs_init);

static void __exit acpi_configfs_exit(void)
{
272
	configfs_unregister_default_group(acpi_table_group);
273 274 275 276 277 278 279
	configfs_unregister_subsystem(&acpi_configfs);
}
module_exit(acpi_configfs_exit);

MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
MODULE_DESCRIPTION("ACPI configfs support");
MODULE_LICENSE("GPL v2");