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