xref: /linux/drivers/acpi/sysfs.c (revision 68a385dbe05fd8da69ec8a7753591fd653f97d89)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sysfs.c - ACPI sysfs interface to userspace.
4  */
5 
6 #define pr_fmt(fmt) "ACPI: " fmt
7 
8 #include <linux/acpi.h>
9 #include <linux/bitmap.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/kstrtox.h>
13 #include <linux/moduleparam.h>
14 #include <linux/string.h>
15 
16 #include "internal.h"
17 
18 #ifdef CONFIG_ACPI_DEBUG
19 /*
20  * ACPI debug sysfs I/F, including:
21  * /sys/module/acpi/parameters/debug_layer
22  * /sys/module/acpi/parameters/debug_level
23  * /sys/module/acpi/parameters/trace_method_name
24  * /sys/module/acpi/parameters/trace_state
25  * /sys/module/acpi/parameters/trace_debug_layer
26  * /sys/module/acpi/parameters/trace_debug_level
27  */
28 
29 struct acpi_dlayer {
30 	const char *name;
31 	unsigned long value;
32 };
33 struct acpi_dlevel {
34 	const char *name;
35 	unsigned long value;
36 };
37 #define ACPI_DEBUG_INIT(v)	{ .name = #v, .value = v }
38 
39 static const struct acpi_dlayer acpi_debug_layers[] = {
40 	ACPI_DEBUG_INIT(ACPI_UTILITIES),
41 	ACPI_DEBUG_INIT(ACPI_HARDWARE),
42 	ACPI_DEBUG_INIT(ACPI_EVENTS),
43 	ACPI_DEBUG_INIT(ACPI_TABLES),
44 	ACPI_DEBUG_INIT(ACPI_NAMESPACE),
45 	ACPI_DEBUG_INIT(ACPI_PARSER),
46 	ACPI_DEBUG_INIT(ACPI_DISPATCHER),
47 	ACPI_DEBUG_INIT(ACPI_EXECUTER),
48 	ACPI_DEBUG_INIT(ACPI_RESOURCES),
49 	ACPI_DEBUG_INIT(ACPI_CA_DEBUGGER),
50 	ACPI_DEBUG_INIT(ACPI_OS_SERVICES),
51 	ACPI_DEBUG_INIT(ACPI_CA_DISASSEMBLER),
52 	ACPI_DEBUG_INIT(ACPI_COMPILER),
53 	ACPI_DEBUG_INIT(ACPI_TOOLS),
54 };
55 
56 static const struct acpi_dlevel acpi_debug_levels[] = {
57 	ACPI_DEBUG_INIT(ACPI_LV_INIT),
58 	ACPI_DEBUG_INIT(ACPI_LV_DEBUG_OBJECT),
59 	ACPI_DEBUG_INIT(ACPI_LV_INFO),
60 	ACPI_DEBUG_INIT(ACPI_LV_REPAIR),
61 	ACPI_DEBUG_INIT(ACPI_LV_TRACE_POINT),
62 
63 	ACPI_DEBUG_INIT(ACPI_LV_INIT_NAMES),
64 	ACPI_DEBUG_INIT(ACPI_LV_PARSE),
65 	ACPI_DEBUG_INIT(ACPI_LV_LOAD),
66 	ACPI_DEBUG_INIT(ACPI_LV_DISPATCH),
67 	ACPI_DEBUG_INIT(ACPI_LV_EXEC),
68 	ACPI_DEBUG_INIT(ACPI_LV_NAMES),
69 	ACPI_DEBUG_INIT(ACPI_LV_OPREGION),
70 	ACPI_DEBUG_INIT(ACPI_LV_BFIELD),
71 	ACPI_DEBUG_INIT(ACPI_LV_TABLES),
72 	ACPI_DEBUG_INIT(ACPI_LV_VALUES),
73 	ACPI_DEBUG_INIT(ACPI_LV_OBJECTS),
74 	ACPI_DEBUG_INIT(ACPI_LV_RESOURCES),
75 	ACPI_DEBUG_INIT(ACPI_LV_USER_REQUESTS),
76 	ACPI_DEBUG_INIT(ACPI_LV_PACKAGE),
77 
78 	ACPI_DEBUG_INIT(ACPI_LV_ALLOCATIONS),
79 	ACPI_DEBUG_INIT(ACPI_LV_FUNCTIONS),
80 	ACPI_DEBUG_INIT(ACPI_LV_OPTIMIZATIONS),
81 
82 	ACPI_DEBUG_INIT(ACPI_LV_MUTEX),
83 	ACPI_DEBUG_INIT(ACPI_LV_THREADS),
84 	ACPI_DEBUG_INIT(ACPI_LV_IO),
85 	ACPI_DEBUG_INIT(ACPI_LV_INTERRUPTS),
86 
87 	ACPI_DEBUG_INIT(ACPI_LV_AML_DISASSEMBLE),
88 	ACPI_DEBUG_INIT(ACPI_LV_VERBOSE_INFO),
89 	ACPI_DEBUG_INIT(ACPI_LV_FULL_TABLES),
90 	ACPI_DEBUG_INIT(ACPI_LV_EVENTS),
91 };
92 
93 static int param_get_debug_layer(char *buffer, const struct kernel_param *kp)
94 {
95 	int result = 0;
96 	int i;
97 
98 	result = sprintf(buffer, "%-25s\tHex        SET\n", "Description");
99 
100 	for (i = 0; i < ARRAY_SIZE(acpi_debug_layers); i++) {
101 		result += sprintf(buffer + result, "%-25s\t0x%08lX [%c]\n",
102 				  acpi_debug_layers[i].name,
103 				  acpi_debug_layers[i].value,
104 				  (acpi_dbg_layer & acpi_debug_layers[i].value)
105 				  ? '*' : ' ');
106 	}
107 	result +=
108 	    sprintf(buffer + result, "%-25s\t0x%08X [%c]\n", "ACPI_ALL_DRIVERS",
109 		    ACPI_ALL_DRIVERS,
110 		    (acpi_dbg_layer & ACPI_ALL_DRIVERS) ==
111 		    ACPI_ALL_DRIVERS ? '*' : (acpi_dbg_layer & ACPI_ALL_DRIVERS)
112 		    == 0 ? ' ' : '-');
113 	result +=
114 	    sprintf(buffer + result,
115 		    "--\ndebug_layer = 0x%08X ( * = enabled)\n",
116 		    acpi_dbg_layer);
117 
118 	return result;
119 }
120 
121 static int param_get_debug_level(char *buffer, const struct kernel_param *kp)
122 {
123 	int result = 0;
124 	int i;
125 
126 	result = sprintf(buffer, "%-25s\tHex        SET\n", "Description");
127 
128 	for (i = 0; i < ARRAY_SIZE(acpi_debug_levels); i++) {
129 		result += sprintf(buffer + result, "%-25s\t0x%08lX [%c]\n",
130 				  acpi_debug_levels[i].name,
131 				  acpi_debug_levels[i].value,
132 				  (acpi_dbg_level & acpi_debug_levels[i].value)
133 				  ? '*' : ' ');
134 	}
135 	result +=
136 	    sprintf(buffer + result, "--\ndebug_level = 0x%08X (* = enabled)\n",
137 		    acpi_dbg_level);
138 
139 	return result;
140 }
141 
142 static const struct kernel_param_ops param_ops_debug_layer = {
143 	.set = param_set_uint,
144 	.get = param_get_debug_layer,
145 };
146 
147 static const struct kernel_param_ops param_ops_debug_level = {
148 	.set = param_set_uint,
149 	.get = param_get_debug_level,
150 };
151 
152 module_param_cb(debug_layer, &param_ops_debug_layer, &acpi_dbg_layer, 0644);
153 module_param_cb(debug_level, &param_ops_debug_level, &acpi_dbg_level, 0644);
154 
155 static char trace_method_name[1024];
156 
157 static int param_set_trace_method_name(const char *val,
158 				       const struct kernel_param *kp)
159 {
160 	u32 saved_flags = 0;
161 	bool is_abs_path = true;
162 
163 	if (*val != '\\')
164 		is_abs_path = false;
165 
166 	if ((is_abs_path && strlen(val) > 1023) ||
167 	    (!is_abs_path && strlen(val) > 1022)) {
168 		pr_err("%s: string parameter too long\n", kp->name);
169 		return -ENOSPC;
170 	}
171 
172 	/*
173 	 * It's not safe to update acpi_gbl_trace_method_name without
174 	 * having the tracer stopped, so we save the original tracer
175 	 * state and disable it.
176 	 */
177 	saved_flags = acpi_gbl_trace_flags;
178 	(void)acpi_debug_trace(NULL,
179 			       acpi_gbl_trace_dbg_level,
180 			       acpi_gbl_trace_dbg_layer,
181 			       0);
182 
183 	/* This is a hack.  We can't kmalloc in early boot. */
184 	if (is_abs_path)
185 		strscpy(trace_method_name, val, sizeof(trace_method_name));
186 	else {
187 		trace_method_name[0] = '\\';
188 		strscpy(trace_method_name + 1, val, sizeof(trace_method_name) - 1);
189 	}
190 
191 	/* Restore the original tracer state */
192 	(void)acpi_debug_trace(trace_method_name,
193 			       acpi_gbl_trace_dbg_level,
194 			       acpi_gbl_trace_dbg_layer,
195 			       saved_flags);
196 
197 	return 0;
198 }
199 
200 static int param_get_trace_method_name(char *buffer, const struct kernel_param *kp)
201 {
202 	return sysfs_emit(buffer, "%s\n", acpi_gbl_trace_method_name);
203 }
204 
205 static const struct kernel_param_ops param_ops_trace_method = {
206 	.set = param_set_trace_method_name,
207 	.get = param_get_trace_method_name,
208 };
209 
210 static const struct kernel_param_ops param_ops_trace_attrib = {
211 	.set = param_set_uint,
212 	.get = param_get_uint,
213 };
214 
215 module_param_cb(trace_method_name, &param_ops_trace_method, &trace_method_name, 0644);
216 module_param_cb(trace_debug_layer, &param_ops_trace_attrib, &acpi_gbl_trace_dbg_layer, 0644);
217 module_param_cb(trace_debug_level, &param_ops_trace_attrib, &acpi_gbl_trace_dbg_level, 0644);
218 
219 static int param_set_trace_state(const char *val,
220 				 const struct kernel_param *kp)
221 {
222 	acpi_status status;
223 	const char *method = trace_method_name;
224 	u32 flags = 0;
225 
226 /* So "xxx-once" comparison should go prior than "xxx" comparison */
227 #define acpi_compare_param(val, key)	\
228 	strncmp((val), (key), sizeof(key) - 1)
229 
230 	if (!acpi_compare_param(val, "enable")) {
231 		method = NULL;
232 		flags = ACPI_TRACE_ENABLED;
233 	} else if (!acpi_compare_param(val, "disable"))
234 		method = NULL;
235 	else if (!acpi_compare_param(val, "method-once"))
236 		flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT;
237 	else if (!acpi_compare_param(val, "method"))
238 		flags = ACPI_TRACE_ENABLED;
239 	else if (!acpi_compare_param(val, "opcode-once"))
240 		flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT | ACPI_TRACE_OPCODE;
241 	else if (!acpi_compare_param(val, "opcode"))
242 		flags = ACPI_TRACE_ENABLED | ACPI_TRACE_OPCODE;
243 	else
244 		return -EINVAL;
245 
246 	status = acpi_debug_trace(method,
247 				  acpi_gbl_trace_dbg_level,
248 				  acpi_gbl_trace_dbg_layer,
249 				  flags);
250 	if (ACPI_FAILURE(status))
251 		return -EBUSY;
252 
253 	return 0;
254 }
255 
256 static int param_get_trace_state(char *buffer, const struct kernel_param *kp)
257 {
258 	if (!(acpi_gbl_trace_flags & ACPI_TRACE_ENABLED))
259 		return sprintf(buffer, "disable\n");
260 	if (!acpi_gbl_trace_method_name)
261 		return sprintf(buffer, "enable\n");
262 	if (acpi_gbl_trace_flags & ACPI_TRACE_ONESHOT)
263 		return sprintf(buffer, "method-once\n");
264 	else
265 		return sprintf(buffer, "method\n");
266 }
267 
268 module_param_call(trace_state, param_set_trace_state, param_get_trace_state,
269 		  NULL, 0644);
270 #endif /* CONFIG_ACPI_DEBUG */
271 
272 
273 /* /sys/module/acpi/parameters/aml_debug_output */
274 
275 module_param_named(aml_debug_output, acpi_gbl_enable_aml_debug_object,
276 		   byte, 0644);
277 MODULE_PARM_DESC(aml_debug_output,
278 		 "To enable/disable the ACPI Debug Object output.");
279 
280 /* /sys/module/acpi/parameters/acpica_version */
281 static int param_get_acpica_version(char *buffer,
282 				    const struct kernel_param *kp)
283 {
284 	int result;
285 
286 	result = sprintf(buffer, "%x\n", ACPI_CA_VERSION);
287 
288 	return result;
289 }
290 
291 module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
292 
293 /*
294  * ACPI table sysfs I/F:
295  * /sys/firmware/acpi/tables/
296  * /sys/firmware/acpi/tables/data/
297  * /sys/firmware/acpi/tables/dynamic/
298  */
299 
300 static LIST_HEAD(acpi_table_attr_list);
301 static struct kobject *tables_kobj;
302 static struct kobject *tables_data_kobj;
303 static struct kobject *dynamic_tables_kobj;
304 static struct kobject *hotplug_kobj;
305 
306 #define ACPI_MAX_TABLE_INSTANCES	999
307 #define ACPI_INST_SIZE			4 /* including trailing 0 */
308 
309 struct acpi_table_attr {
310 	struct bin_attribute attr;
311 	char name[ACPI_NAMESEG_SIZE];
312 	int instance;
313 	char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE];
314 	struct list_head node;
315 };
316 
317 struct acpi_data_attr {
318 	struct bin_attribute attr;
319 	u64	addr;
320 	char filename[ACPI_NAMESEG_SIZE+ACPI_INST_SIZE];
321 };
322 
323 static ssize_t acpi_table_show(struct file *filp, struct kobject *kobj,
324 			       const struct bin_attribute *bin_attr, char *buf,
325 			       loff_t offset, size_t count)
326 {
327 	struct acpi_table_attr *table_attr =
328 	    container_of(bin_attr, struct acpi_table_attr, attr);
329 	struct acpi_table_header *table_header = NULL;
330 	acpi_status status;
331 	ssize_t rc;
332 
333 	status = acpi_get_table(table_attr->name, table_attr->instance,
334 				&table_header);
335 	if (ACPI_FAILURE(status))
336 		return -ENODEV;
337 
338 	rc = memory_read_from_buffer(buf, count, &offset, table_header,
339 			table_header->length);
340 	acpi_put_table(table_header);
341 	return rc;
342 }
343 
344 static int acpi_table_attr_init(struct kobject *tables_obj,
345 				struct acpi_table_attr *table_attr,
346 				struct acpi_table_header *table_header)
347 {
348 	struct acpi_table_header *header = NULL;
349 	struct acpi_table_attr *attr = NULL;
350 	char instance_str[ACPI_INST_SIZE];
351 
352 	sysfs_attr_init(&table_attr->attr.attr);
353 	ACPI_COPY_NAMESEG(table_attr->name, table_header->signature);
354 
355 	list_for_each_entry(attr, &acpi_table_attr_list, node) {
356 		if (ACPI_COMPARE_NAMESEG(table_attr->name, attr->name))
357 			if (table_attr->instance < attr->instance)
358 				table_attr->instance = attr->instance;
359 	}
360 	table_attr->instance++;
361 	if (table_attr->instance > ACPI_MAX_TABLE_INSTANCES) {
362 		pr_warn("%4.4s: too many table instances\n", table_attr->name);
363 		return -ERANGE;
364 	}
365 
366 	ACPI_COPY_NAMESEG(table_attr->filename, table_header->signature);
367 	table_attr->filename[ACPI_NAMESEG_SIZE] = '\0';
368 	if (table_attr->instance > 1 || (table_attr->instance == 1 &&
369 					 !acpi_get_table
370 					 (table_header->signature, 2, &header))) {
371 		snprintf(instance_str, sizeof(instance_str), "%u",
372 			 table_attr->instance);
373 		strcat(table_attr->filename, instance_str);
374 	}
375 
376 	table_attr->attr.size = table_header->length;
377 	table_attr->attr.read = acpi_table_show;
378 	table_attr->attr.attr.name = table_attr->filename;
379 	table_attr->attr.attr.mode = 0400;
380 
381 	return sysfs_create_bin_file(tables_obj, &table_attr->attr);
382 }
383 
384 acpi_status acpi_sysfs_table_handler(u32 event, void *table, void *context)
385 {
386 	struct acpi_table_attr *table_attr;
387 
388 	switch (event) {
389 	case ACPI_TABLE_EVENT_INSTALL:
390 		table_attr = kzalloc_obj(*table_attr);
391 		if (!table_attr)
392 			return AE_NO_MEMORY;
393 
394 		if (acpi_table_attr_init(dynamic_tables_kobj,
395 					 table_attr, table)) {
396 			kfree(table_attr);
397 			return AE_ERROR;
398 		}
399 		list_add_tail(&table_attr->node, &acpi_table_attr_list);
400 		break;
401 	case ACPI_TABLE_EVENT_LOAD:
402 	case ACPI_TABLE_EVENT_UNLOAD:
403 	case ACPI_TABLE_EVENT_UNINSTALL:
404 		/*
405 		 * we do not need to do anything right now
406 		 * because the table is not deleted from the
407 		 * global table list when unloading it.
408 		 */
409 		break;
410 	default:
411 		return AE_BAD_PARAMETER;
412 	}
413 	return AE_OK;
414 }
415 
416 static ssize_t acpi_data_show(struct file *filp, struct kobject *kobj,
417 			      const struct bin_attribute *bin_attr, char *buf,
418 			      loff_t offset, size_t count)
419 {
420 	struct acpi_data_attr *data_attr;
421 	void __iomem *base;
422 	ssize_t size;
423 
424 	data_attr = container_of(bin_attr, struct acpi_data_attr, attr);
425 	size = data_attr->attr.size;
426 
427 	if (offset < 0)
428 		return -EINVAL;
429 
430 	if (offset >= size)
431 		return 0;
432 
433 	if (count > size - offset)
434 		count = size - offset;
435 
436 	base = acpi_os_map_iomem(data_attr->addr, size);
437 	if (!base)
438 		return -ENOMEM;
439 
440 	memcpy_fromio(buf, base + offset, count);
441 
442 	acpi_os_unmap_iomem(base, size);
443 
444 	return count;
445 }
446 
447 static int acpi_bert_data_init(void *th, struct acpi_data_attr *data_attr)
448 {
449 	struct acpi_table_bert *bert = th;
450 
451 	if (bert->header.length < sizeof(struct acpi_table_bert) ||
452 	    bert->region_length < sizeof(struct acpi_bert_region)) {
453 		kfree(data_attr);
454 		return -EINVAL;
455 	}
456 	data_attr->addr = bert->address;
457 	data_attr->attr.size = bert->region_length;
458 
459 	return sysfs_create_bin_file(tables_data_kobj, &data_attr->attr);
460 }
461 
462 static int acpi_ccel_data_init(void *th, struct acpi_data_attr *data_attr)
463 {
464 	struct acpi_table_ccel *ccel = th;
465 
466 	if (ccel->header.length < sizeof(struct acpi_table_ccel) ||
467 	    !ccel->log_area_start_address || !ccel->log_area_minimum_length) {
468 		kfree(data_attr);
469 		return -EINVAL;
470 	}
471 	data_attr->addr = ccel->log_area_start_address;
472 	data_attr->attr.size = ccel->log_area_minimum_length;
473 
474 	return sysfs_create_bin_file(tables_data_kobj, &data_attr->attr);
475 }
476 
477 static struct acpi_data_obj {
478 	char *name;
479 	int (*fn)(void *, struct acpi_data_attr *);
480 } acpi_data_objs[] = {
481 	{ ACPI_SIG_BERT, acpi_bert_data_init },
482 	{ ACPI_SIG_CCEL, acpi_ccel_data_init },
483 };
484 
485 #define NUM_ACPI_DATA_OBJS ARRAY_SIZE(acpi_data_objs)
486 
487 static int acpi_table_data_init(struct acpi_table_header *th, struct acpi_table_attr *table_attr)
488 {
489 	struct acpi_data_attr *data_attr;
490 	int i;
491 
492 	for (i = 0; i < NUM_ACPI_DATA_OBJS; i++) {
493 		if (ACPI_COMPARE_NAMESEG(th->signature, acpi_data_objs[i].name)) {
494 			data_attr = kzalloc_obj(*data_attr);
495 			if (!data_attr)
496 				return -ENOMEM;
497 			sysfs_attr_init(&data_attr->attr.attr);
498 			data_attr->attr.read = acpi_data_show;
499 			data_attr->attr.attr.mode = 0400;
500 			strscpy(data_attr->filename, table_attr->filename);
501 			data_attr->attr.attr.name = data_attr->filename;
502 			return acpi_data_objs[i].fn(th, data_attr);
503 		}
504 	}
505 	return 0;
506 }
507 
508 static int acpi_tables_sysfs_init(void)
509 {
510 	struct acpi_table_attr *table_attr;
511 	struct acpi_table_header *table_header = NULL;
512 	int table_index;
513 	acpi_status status;
514 	int ret;
515 
516 	tables_kobj = kobject_create_and_add("tables", acpi_kobj);
517 	if (!tables_kobj)
518 		goto err;
519 
520 	tables_data_kobj = kobject_create_and_add("data", tables_kobj);
521 	if (!tables_data_kobj)
522 		goto err_tables_data;
523 
524 	dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
525 	if (!dynamic_tables_kobj)
526 		goto err_dynamic_tables;
527 
528 	for (table_index = 0;; table_index++) {
529 		status = acpi_get_table_by_index(table_index, &table_header);
530 
531 		if (status == AE_BAD_PARAMETER)
532 			break;
533 
534 		if (ACPI_FAILURE(status))
535 			continue;
536 
537 		table_attr = kzalloc_obj(*table_attr);
538 		if (!table_attr)
539 			return -ENOMEM;
540 
541 		ret = acpi_table_attr_init(tables_kobj,
542 					   table_attr, table_header);
543 		if (ret) {
544 			kfree(table_attr);
545 			return ret;
546 		}
547 		list_add_tail(&table_attr->node, &acpi_table_attr_list);
548 		acpi_table_data_init(table_header, table_attr);
549 	}
550 
551 	kobject_uevent(tables_kobj, KOBJ_ADD);
552 	kobject_uevent(tables_data_kobj, KOBJ_ADD);
553 	kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
554 
555 	return 0;
556 err_dynamic_tables:
557 	kobject_put(tables_data_kobj);
558 err_tables_data:
559 	kobject_put(tables_kobj);
560 err:
561 	return -ENOMEM;
562 }
563 
564 /*
565  * Detailed ACPI IRQ counters:
566  * /sys/firmware/acpi/interrupts/
567  */
568 
569 u32 acpi_irq_handled;
570 u32 acpi_irq_not_handled;
571 
572 #define COUNT_GPE 0
573 #define COUNT_SCI 1		/* acpi_irq_handled */
574 #define COUNT_SCI_NOT 2		/* acpi_irq_not_handled */
575 #define COUNT_ERROR 3		/* other */
576 #define NUM_COUNTERS_EXTRA 4
577 
578 struct event_counter {
579 	u32 count;
580 	u32 flags;
581 };
582 
583 static struct event_counter *all_counters;
584 static u32 num_gpes;
585 static u32 num_counters;
586 static struct attribute **all_attrs;
587 static u32 acpi_gpe_count;
588 
589 static struct attribute_group interrupt_stats_attr_group = {
590 	.name = "interrupts",
591 };
592 
593 static struct kobj_attribute *counter_attrs;
594 
595 static void delete_gpe_attr_array(void)
596 {
597 	struct event_counter *tmp = all_counters;
598 
599 	all_counters = NULL;
600 	kfree(tmp);
601 
602 	if (counter_attrs) {
603 		int i;
604 
605 		for (i = 0; i < num_gpes; i++)
606 			kfree(counter_attrs[i].attr.name);
607 
608 		kfree(counter_attrs);
609 	}
610 	kfree(all_attrs);
611 }
612 
613 static void gpe_count(u32 gpe_number)
614 {
615 	acpi_gpe_count++;
616 
617 	if (!all_counters)
618 		return;
619 
620 	if (gpe_number < num_gpes)
621 		all_counters[gpe_number].count++;
622 	else
623 		all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
624 			     COUNT_ERROR].count++;
625 }
626 
627 static void fixed_event_count(u32 event_number)
628 {
629 	if (!all_counters)
630 		return;
631 
632 	if (event_number < ACPI_NUM_FIXED_EVENTS)
633 		all_counters[num_gpes + event_number].count++;
634 	else
635 		all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
636 			     COUNT_ERROR].count++;
637 }
638 
639 static void acpi_global_event_handler(u32 event_type, acpi_handle device,
640 	u32 event_number, void *context)
641 {
642 	if (event_type == ACPI_EVENT_TYPE_GPE) {
643 		gpe_count(event_number);
644 		pr_debug("GPE event 0x%02x\n", event_number);
645 	} else if (event_type == ACPI_EVENT_TYPE_FIXED) {
646 		fixed_event_count(event_number);
647 		pr_debug("Fixed event 0x%02x\n", event_number);
648 	} else {
649 		pr_debug("Other event 0x%02x\n", event_number);
650 	}
651 }
652 
653 static int get_status(u32 index, acpi_event_status *ret,
654 		      acpi_handle *handle)
655 {
656 	acpi_status status;
657 
658 	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
659 		return -EINVAL;
660 
661 	if (index < num_gpes) {
662 		status = acpi_get_gpe_device(index, handle);
663 		if (ACPI_FAILURE(status)) {
664 			pr_warn("Invalid GPE 0x%x", index);
665 			return -ENXIO;
666 		}
667 		status = acpi_get_gpe_status(*handle, index, ret);
668 	} else {
669 		status = acpi_get_event_status(index - num_gpes, ret);
670 	}
671 	if (ACPI_FAILURE(status))
672 		return -EIO;
673 
674 	return 0;
675 }
676 
677 static ssize_t counter_show(struct kobject *kobj,
678 			    struct kobj_attribute *attr, char *buf)
679 {
680 	int index = attr - counter_attrs;
681 	int size;
682 	acpi_handle handle;
683 	acpi_event_status status;
684 	int result = 0;
685 
686 	all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
687 	    acpi_irq_handled;
688 	all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count =
689 	    acpi_irq_not_handled;
690 	all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
691 	    acpi_gpe_count;
692 	size = sysfs_emit(buf, "%8u", all_counters[index].count);
693 
694 	/* "gpe_all" or "sci" */
695 	if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
696 		goto end;
697 
698 	result = get_status(index, &status, &handle);
699 	if (result)
700 		goto end;
701 
702 	if (status & ACPI_EVENT_FLAG_ENABLE_SET)
703 		size += sysfs_emit_at(buf, size, "  EN");
704 	else
705 		size += sysfs_emit_at(buf, size, "    ");
706 	if (status & ACPI_EVENT_FLAG_STATUS_SET)
707 		size += sysfs_emit_at(buf, size, " STS");
708 	else
709 		size += sysfs_emit_at(buf, size, "    ");
710 
711 	if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER))
712 		size += sysfs_emit_at(buf, size, " invalid     ");
713 	else if (status & ACPI_EVENT_FLAG_ENABLED)
714 		size += sysfs_emit_at(buf, size, " enabled     ");
715 	else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
716 		size += sysfs_emit_at(buf, size, " wake_enabled");
717 	else
718 		size += sysfs_emit_at(buf, size, " disabled    ");
719 	if (status & ACPI_EVENT_FLAG_MASKED)
720 		size += sysfs_emit_at(buf, size, " masked  ");
721 	else
722 		size += sysfs_emit_at(buf, size, " unmasked");
723 
724 end:
725 	size += sysfs_emit_at(buf, size, "\n");
726 	return result ? result : size;
727 }
728 
729 /*
730  * counter_set() sets the specified counter.
731  * setting the total "sci" file to any value clears all counters.
732  * enable/disable/clear a gpe/fixed event in user space.
733  */
734 static ssize_t counter_set(struct kobject *kobj,
735 			   struct kobj_attribute *attr, const char *buf,
736 			   size_t size)
737 {
738 	int index = attr - counter_attrs;
739 	acpi_event_status status;
740 	acpi_handle handle;
741 	int result = 0;
742 	unsigned long tmp;
743 
744 	if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
745 		int i;
746 		for (i = 0; i < num_counters; ++i)
747 			all_counters[i].count = 0;
748 		acpi_gpe_count = 0;
749 		acpi_irq_handled = 0;
750 		acpi_irq_not_handled = 0;
751 		goto end;
752 	}
753 
754 	/* show the event status for both GPEs and Fixed Events */
755 	result = get_status(index, &status, &handle);
756 	if (result)
757 		goto end;
758 
759 	if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER)) {
760 		pr_warn("Can not change Invalid GPE/Fixed Event status\n");
761 		return -EINVAL;
762 	}
763 
764 	if (index < num_gpes) {
765 		if (!strcmp(buf, "disable\n") &&
766 		    (status & ACPI_EVENT_FLAG_ENABLED))
767 			result = acpi_disable_gpe(handle, index);
768 		else if (!strcmp(buf, "enable\n") &&
769 			 !(status & ACPI_EVENT_FLAG_ENABLED))
770 			result = acpi_enable_gpe(handle, index);
771 		else if (!strcmp(buf, "clear\n") &&
772 			 (status & ACPI_EVENT_FLAG_STATUS_SET))
773 			result = acpi_clear_gpe(handle, index);
774 		else if (!strcmp(buf, "mask\n"))
775 			result = acpi_mask_gpe(handle, index, TRUE);
776 		else if (!strcmp(buf, "unmask\n"))
777 			result = acpi_mask_gpe(handle, index, FALSE);
778 		else if (!kstrtoul(buf, 0, &tmp))
779 			all_counters[index].count = tmp;
780 		else
781 			result = -EINVAL;
782 	} else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
783 		int event = index - num_gpes;
784 		if (!strcmp(buf, "disable\n") &&
785 		    (status & ACPI_EVENT_FLAG_ENABLE_SET))
786 			result = acpi_disable_event(event, ACPI_NOT_ISR);
787 		else if (!strcmp(buf, "enable\n") &&
788 			 !(status & ACPI_EVENT_FLAG_ENABLE_SET))
789 			result = acpi_enable_event(event, ACPI_NOT_ISR);
790 		else if (!strcmp(buf, "clear\n") &&
791 			 (status & ACPI_EVENT_FLAG_STATUS_SET))
792 			result = acpi_clear_event(event);
793 		else if (!kstrtoul(buf, 0, &tmp))
794 			all_counters[index].count = tmp;
795 		else
796 			result = -EINVAL;
797 	} else
798 		all_counters[index].count = strtoul(buf, NULL, 0);
799 
800 	if (ACPI_FAILURE(result))
801 		result = -EINVAL;
802 end:
803 	return result ? result : size;
804 }
805 
806 /*
807  * A Quirk Mechanism for GPE Flooding Prevention:
808  *
809  * Quirks may be needed to prevent GPE flooding on a specific GPE. The
810  * flooding typically cannot be detected and automatically prevented by
811  * ACPI_GPE_DISPATCH_NONE check because there is a _Lxx/_Exx prepared in
812  * the AML tables. This normally indicates a feature gap in Linux, thus
813  * instead of providing endless quirk tables, we provide a boot parameter
814  * for those who want this quirk. For example, if the users want to prevent
815  * the GPE flooding for GPE 00, they need to specify the following boot
816  * parameter:
817  *   acpi_mask_gpe=0x00
818  * Note, the parameter can be a list (see bitmap_parselist() for the details).
819  * The masking status can be modified by the following runtime controlling
820  * interface:
821  *   echo unmask > /sys/firmware/acpi/interrupts/gpe00
822  */
823 #define ACPI_MASKABLE_GPE_MAX	0x100
824 static DECLARE_BITMAP(acpi_masked_gpes_map, ACPI_MASKABLE_GPE_MAX) __initdata;
825 
826 static int __init acpi_gpe_set_masked_gpes(char *val)
827 {
828 	int ret;
829 	u8 gpe;
830 
831 	ret = kstrtou8(val, 0, &gpe);
832 	if (ret) {
833 		ret = bitmap_parselist(val, acpi_masked_gpes_map, ACPI_MASKABLE_GPE_MAX);
834 		if (ret)
835 			return ret;
836 	} else
837 		set_bit(gpe, acpi_masked_gpes_map);
838 
839 	return 1;
840 }
841 __setup("acpi_mask_gpe=", acpi_gpe_set_masked_gpes);
842 
843 void __init acpi_gpe_apply_masked_gpes(void)
844 {
845 	acpi_handle handle;
846 	acpi_status status;
847 	u16 gpe;
848 
849 	for_each_set_bit(gpe, acpi_masked_gpes_map, ACPI_MASKABLE_GPE_MAX) {
850 		status = acpi_get_gpe_device(gpe, &handle);
851 		if (ACPI_SUCCESS(status)) {
852 			pr_info("Masking GPE 0x%x.\n", gpe);
853 			(void)acpi_mask_gpe(handle, gpe, TRUE);
854 		}
855 	}
856 }
857 
858 void acpi_irq_stats_init(void)
859 {
860 	acpi_status status;
861 	int i;
862 
863 	if (all_counters)
864 		return;
865 
866 	num_gpes = acpi_current_gpe_count;
867 	num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
868 
869 	all_attrs = kzalloc_objs(*all_attrs, num_counters + 1);
870 	if (all_attrs == NULL)
871 		return;
872 
873 	all_counters = kzalloc_objs(*all_counters, num_counters);
874 	if (all_counters == NULL)
875 		goto fail;
876 
877 	status = acpi_install_global_event_handler(acpi_global_event_handler, NULL);
878 	if (ACPI_FAILURE(status))
879 		goto fail;
880 
881 	counter_attrs = kzalloc_objs(*counter_attrs, num_counters);
882 	if (counter_attrs == NULL)
883 		goto fail;
884 
885 	for (i = 0; i < num_counters; ++i) {
886 		char buffer[12];
887 		char *name;
888 
889 		if (i < num_gpes)
890 			sprintf(buffer, "gpe%02X", i);
891 		else if (i == num_gpes + ACPI_EVENT_PMTIMER)
892 			sprintf(buffer, "ff_pmtimer");
893 		else if (i == num_gpes + ACPI_EVENT_GLOBAL)
894 			sprintf(buffer, "ff_gbl_lock");
895 		else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
896 			sprintf(buffer, "ff_pwr_btn");
897 		else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
898 			sprintf(buffer, "ff_slp_btn");
899 		else if (i == num_gpes + ACPI_EVENT_RTC)
900 			sprintf(buffer, "ff_rt_clk");
901 		else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
902 			sprintf(buffer, "gpe_all");
903 		else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
904 			sprintf(buffer, "sci");
905 		else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
906 			sprintf(buffer, "sci_not");
907 		else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
908 			sprintf(buffer, "error");
909 		else
910 			sprintf(buffer, "bug%02X", i);
911 
912 		name = kstrdup(buffer, GFP_KERNEL);
913 		if (name == NULL)
914 			goto fail;
915 
916 		sysfs_attr_init(&counter_attrs[i].attr);
917 		counter_attrs[i].attr.name = name;
918 		counter_attrs[i].attr.mode = 0644;
919 		counter_attrs[i].show = counter_show;
920 		counter_attrs[i].store = counter_set;
921 
922 		all_attrs[i] = &counter_attrs[i].attr;
923 	}
924 
925 	interrupt_stats_attr_group.attrs = all_attrs;
926 	if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
927 		return;
928 
929 fail:
930 	delete_gpe_attr_array();
931 }
932 
933 static void __exit interrupt_stats_exit(void)
934 {
935 	sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
936 
937 	delete_gpe_attr_array();
938 }
939 
940 static ssize_t pm_profile_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
941 {
942 	return sysfs_emit(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
943 }
944 
945 static const struct kobj_attribute pm_profile_attr = __ATTR_RO(pm_profile);
946 
947 static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
948 {
949 	struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
950 
951 	return sysfs_emit(buf, "%d\n", hotplug->enabled);
952 }
953 
954 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
955 			     const char *buf, size_t size)
956 {
957 	struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
958 	unsigned int val;
959 
960 	if (kstrtouint(buf, 10, &val) || val > 1)
961 		return -EINVAL;
962 
963 	acpi_scan_hotplug_enabled(hotplug, val);
964 	return size;
965 }
966 
967 static struct kobj_attribute hotplug_enabled_attr = __ATTR_RW(enabled);
968 
969 static struct attribute *hotplug_profile_attrs[] = {
970 	&hotplug_enabled_attr.attr,
971 	NULL
972 };
973 ATTRIBUTE_GROUPS(hotplug_profile);
974 
975 static const struct kobj_type acpi_hotplug_profile_ktype = {
976 	.sysfs_ops = &kobj_sysfs_ops,
977 	.default_groups = hotplug_profile_groups,
978 };
979 
980 void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
981 				    const char *name)
982 {
983 	int error;
984 
985 	if (!hotplug_kobj)
986 		goto err_out;
987 
988 	error = kobject_init_and_add(&hotplug->kobj,
989 		&acpi_hotplug_profile_ktype, hotplug_kobj, "%s", name);
990 	if (error) {
991 		kobject_put(&hotplug->kobj);
992 		goto err_out;
993 	}
994 
995 	kobject_uevent(&hotplug->kobj, KOBJ_ADD);
996 	return;
997 
998  err_out:
999 	pr_err("Unable to add hotplug profile '%s'\n", name);
1000 }
1001 
1002 static ssize_t force_remove_show(struct kobject *kobj,
1003 				 struct kobj_attribute *attr, char *buf)
1004 {
1005 	return sysfs_emit(buf, "%d\n", 0);
1006 }
1007 
1008 static ssize_t force_remove_store(struct kobject *kobj,
1009 				  struct kobj_attribute *attr,
1010 				  const char *buf, size_t size)
1011 {
1012 	bool val;
1013 	int ret;
1014 
1015 	ret = kstrtobool(buf, &val);
1016 	if (ret < 0)
1017 		return ret;
1018 
1019 	if (val) {
1020 		pr_err("Enabling force_remove is not supported anymore. Please report to linux-acpi@vger.kernel.org if you depend on this functionality\n");
1021 		return -EINVAL;
1022 	}
1023 	return size;
1024 }
1025 
1026 static const struct kobj_attribute force_remove_attr = __ATTR_RW(force_remove);
1027 
1028 int __init acpi_sysfs_init(void)
1029 {
1030 	int result;
1031 
1032 	result = acpi_tables_sysfs_init();
1033 	if (result)
1034 		return result;
1035 
1036 	hotplug_kobj = kobject_create_and_add("hotplug", acpi_kobj);
1037 	if (!hotplug_kobj)
1038 		return -ENOMEM;
1039 
1040 	result = sysfs_create_file(hotplug_kobj, &force_remove_attr.attr);
1041 	if (result)
1042 		return result;
1043 
1044 	result = sysfs_create_file(acpi_kobj, &pm_profile_attr.attr);
1045 	return result;
1046 }
1047