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, ¶m_ops_debug_layer, &acpi_dbg_layer, 0644); 153 module_param_cb(debug_level, ¶m_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, ¶m_ops_trace_method, &trace_method_name, 0644); 216 module_param_cb(trace_debug_layer, ¶m_ops_trace_attrib, &acpi_gbl_trace_dbg_layer, 0644); 217 module_param_cb(trace_debug_level, ¶m_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 u64 end; 424 425 data_attr = container_of(bin_attr, struct acpi_data_attr, attr); 426 size = data_attr->attr.size; 427 428 if (offset < 0) 429 return -EINVAL; 430 431 if (offset >= size) 432 return 0; 433 434 if (count > size - offset) 435 count = size - offset; 436 437 /* acpi_os_map_iomem() can't handle a range that wraps around */ 438 if (check_add_overflow(data_attr->addr, size, &end)) 439 return -EINVAL; 440 441 base = acpi_os_map_iomem(data_attr->addr + offset, count); 442 if (!base) 443 return -ENOMEM; 444 445 memcpy_fromio(buf, base, count); 446 447 acpi_os_unmap_iomem(base, count); 448 449 return count; 450 } 451 452 static int acpi_bert_data_init(void *th, struct acpi_data_attr *data_attr) 453 { 454 struct acpi_table_bert *bert = th; 455 456 if (bert->header.length < sizeof(struct acpi_table_bert) || 457 bert->region_length < sizeof(struct acpi_bert_region)) { 458 kfree(data_attr); 459 return -EINVAL; 460 } 461 data_attr->addr = bert->address; 462 data_attr->attr.size = bert->region_length; 463 464 return sysfs_create_bin_file(tables_data_kobj, &data_attr->attr); 465 } 466 467 static int acpi_ccel_data_init(void *th, struct acpi_data_attr *data_attr) 468 { 469 struct acpi_table_ccel *ccel = th; 470 471 if (ccel->header.length < sizeof(struct acpi_table_ccel) || 472 !ccel->log_area_start_address || !ccel->log_area_minimum_length) { 473 kfree(data_attr); 474 return -EINVAL; 475 } 476 data_attr->addr = ccel->log_area_start_address; 477 data_attr->attr.size = ccel->log_area_minimum_length; 478 479 return sysfs_create_bin_file(tables_data_kobj, &data_attr->attr); 480 } 481 482 static struct acpi_data_obj { 483 char *name; 484 int (*fn)(void *, struct acpi_data_attr *); 485 } acpi_data_objs[] = { 486 { ACPI_SIG_BERT, acpi_bert_data_init }, 487 { ACPI_SIG_CCEL, acpi_ccel_data_init }, 488 }; 489 490 #define NUM_ACPI_DATA_OBJS ARRAY_SIZE(acpi_data_objs) 491 492 static int acpi_table_data_init(struct acpi_table_header *th, struct acpi_table_attr *table_attr) 493 { 494 struct acpi_data_attr *data_attr; 495 int i; 496 497 for (i = 0; i < NUM_ACPI_DATA_OBJS; i++) { 498 if (ACPI_COMPARE_NAMESEG(th->signature, acpi_data_objs[i].name)) { 499 data_attr = kzalloc_obj(*data_attr); 500 if (!data_attr) 501 return -ENOMEM; 502 sysfs_attr_init(&data_attr->attr.attr); 503 data_attr->attr.read = acpi_data_show; 504 data_attr->attr.attr.mode = 0400; 505 strscpy(data_attr->filename, table_attr->filename); 506 data_attr->attr.attr.name = data_attr->filename; 507 return acpi_data_objs[i].fn(th, data_attr); 508 } 509 } 510 return 0; 511 } 512 513 static int acpi_tables_sysfs_init(void) 514 { 515 struct acpi_table_attr *table_attr; 516 struct acpi_table_header *table_header = NULL; 517 int table_index; 518 acpi_status status; 519 520 tables_kobj = kobject_create_and_add("tables", acpi_kobj); 521 if (!tables_kobj) 522 goto err; 523 524 tables_data_kobj = kobject_create_and_add("data", tables_kobj); 525 if (!tables_data_kobj) 526 goto err_tables_data; 527 528 dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj); 529 if (!dynamic_tables_kobj) 530 goto err_dynamic_tables; 531 532 for (table_index = 0;; table_index++) { 533 status = acpi_get_table_by_index(table_index, &table_header); 534 535 if (status == AE_BAD_PARAMETER) 536 break; 537 538 if (ACPI_FAILURE(status)) 539 continue; 540 541 table_attr = kzalloc_obj(*table_attr); 542 if (!table_attr) 543 break; 544 545 if (acpi_table_attr_init(tables_kobj, table_attr, table_header)) { 546 kfree(table_attr); 547 break; 548 } 549 list_add_tail(&table_attr->node, &acpi_table_attr_list); 550 acpi_table_data_init(table_header, table_attr); 551 } 552 553 kobject_uevent(tables_kobj, KOBJ_ADD); 554 kobject_uevent(tables_data_kobj, KOBJ_ADD); 555 kobject_uevent(dynamic_tables_kobj, KOBJ_ADD); 556 557 return 0; 558 err_dynamic_tables: 559 kobject_put(tables_data_kobj); 560 err_tables_data: 561 kobject_put(tables_kobj); 562 err: 563 return -ENOMEM; 564 } 565 566 /* 567 * Detailed ACPI IRQ counters: 568 * /sys/firmware/acpi/interrupts/ 569 */ 570 571 u32 acpi_irq_handled; 572 u32 acpi_irq_not_handled; 573 574 #define COUNT_GPE 0 575 #define COUNT_SCI 1 /* acpi_irq_handled */ 576 #define COUNT_SCI_NOT 2 /* acpi_irq_not_handled */ 577 #define COUNT_ERROR 3 /* other */ 578 #define NUM_COUNTERS_EXTRA 4 579 580 struct event_counter { 581 u32 count; 582 u32 flags; 583 }; 584 585 static struct event_counter *all_counters; 586 static u32 num_gpes; 587 static u32 num_counters; 588 static struct attribute **all_attrs; 589 static u32 acpi_gpe_count; 590 591 static struct attribute_group interrupt_stats_attr_group = { 592 .name = "interrupts", 593 }; 594 595 static struct kobj_attribute *counter_attrs; 596 597 static void delete_gpe_attr_array(void) 598 { 599 struct event_counter *tmp = all_counters; 600 601 all_counters = NULL; 602 kfree(tmp); 603 604 if (counter_attrs) { 605 int i; 606 607 for (i = 0; i < num_gpes; i++) 608 kfree(counter_attrs[i].attr.name); 609 610 kfree(counter_attrs); 611 } 612 kfree(all_attrs); 613 } 614 615 static void gpe_count(u32 gpe_number) 616 { 617 acpi_gpe_count++; 618 619 if (!all_counters) 620 return; 621 622 if (gpe_number < num_gpes) 623 all_counters[gpe_number].count++; 624 else 625 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + 626 COUNT_ERROR].count++; 627 } 628 629 static void fixed_event_count(u32 event_number) 630 { 631 if (!all_counters) 632 return; 633 634 if (event_number < ACPI_NUM_FIXED_EVENTS) 635 all_counters[num_gpes + event_number].count++; 636 else 637 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + 638 COUNT_ERROR].count++; 639 } 640 641 static void acpi_global_event_handler(u32 event_type, acpi_handle device, 642 u32 event_number, void *context) 643 { 644 if (event_type == ACPI_EVENT_TYPE_GPE) { 645 gpe_count(event_number); 646 pr_debug("GPE event 0x%02x\n", event_number); 647 } else if (event_type == ACPI_EVENT_TYPE_FIXED) { 648 fixed_event_count(event_number); 649 pr_debug("Fixed event 0x%02x\n", event_number); 650 } else { 651 pr_debug("Other event 0x%02x\n", event_number); 652 } 653 } 654 655 static int get_status(u32 index, acpi_event_status *ret, 656 acpi_handle *handle) 657 { 658 acpi_status status; 659 660 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS) 661 return -EINVAL; 662 663 if (index < num_gpes) { 664 status = acpi_get_gpe_device(index, handle); 665 if (ACPI_FAILURE(status)) { 666 pr_warn("Invalid GPE 0x%x", index); 667 return -ENXIO; 668 } 669 status = acpi_get_gpe_status(*handle, index, ret); 670 } else { 671 status = acpi_get_event_status(index - num_gpes, ret); 672 } 673 if (ACPI_FAILURE(status)) 674 return -EIO; 675 676 return 0; 677 } 678 679 static ssize_t counter_show(struct kobject *kobj, 680 struct kobj_attribute *attr, char *buf) 681 { 682 int index = attr - counter_attrs; 683 int size; 684 acpi_handle handle; 685 acpi_event_status status; 686 int result = 0; 687 688 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count = 689 acpi_irq_handled; 690 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count = 691 acpi_irq_not_handled; 692 all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count = 693 acpi_gpe_count; 694 size = sysfs_emit(buf, "%8u", all_counters[index].count); 695 696 /* "gpe_all" or "sci" */ 697 if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS) 698 goto end; 699 700 result = get_status(index, &status, &handle); 701 if (result) 702 goto end; 703 704 if (status & ACPI_EVENT_FLAG_ENABLE_SET) 705 size += sysfs_emit_at(buf, size, " EN"); 706 else 707 size += sysfs_emit_at(buf, size, " "); 708 if (status & ACPI_EVENT_FLAG_STATUS_SET) 709 size += sysfs_emit_at(buf, size, " STS"); 710 else 711 size += sysfs_emit_at(buf, size, " "); 712 713 if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER)) 714 size += sysfs_emit_at(buf, size, " invalid "); 715 else if (status & ACPI_EVENT_FLAG_ENABLED) 716 size += sysfs_emit_at(buf, size, " enabled "); 717 else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED) 718 size += sysfs_emit_at(buf, size, " wake_enabled"); 719 else 720 size += sysfs_emit_at(buf, size, " disabled "); 721 if (status & ACPI_EVENT_FLAG_MASKED) 722 size += sysfs_emit_at(buf, size, " masked "); 723 else 724 size += sysfs_emit_at(buf, size, " unmasked"); 725 726 end: 727 size += sysfs_emit_at(buf, size, "\n"); 728 return result ? result : size; 729 } 730 731 /* 732 * counter_set() sets the specified counter. 733 * setting the total "sci" file to any value clears all counters. 734 * enable/disable/clear a gpe/fixed event in user space. 735 */ 736 static ssize_t counter_set(struct kobject *kobj, 737 struct kobj_attribute *attr, const char *buf, 738 size_t size) 739 { 740 int index = attr - counter_attrs; 741 acpi_event_status status; 742 acpi_handle handle; 743 int result = 0; 744 unsigned long tmp; 745 746 if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) { 747 int i; 748 for (i = 0; i < num_counters; ++i) 749 all_counters[i].count = 0; 750 acpi_gpe_count = 0; 751 acpi_irq_handled = 0; 752 acpi_irq_not_handled = 0; 753 goto end; 754 } 755 756 /* show the event status for both GPEs and Fixed Events */ 757 result = get_status(index, &status, &handle); 758 if (result) 759 goto end; 760 761 if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER)) { 762 pr_warn("Can not change Invalid GPE/Fixed Event status\n"); 763 return -EINVAL; 764 } 765 766 if (index < num_gpes) { 767 if (!strcmp(buf, "disable\n") && 768 (status & ACPI_EVENT_FLAG_ENABLED)) 769 result = acpi_disable_gpe(handle, index); 770 else if (!strcmp(buf, "enable\n") && 771 !(status & ACPI_EVENT_FLAG_ENABLED)) 772 result = acpi_enable_gpe(handle, index); 773 else if (!strcmp(buf, "clear\n") && 774 (status & ACPI_EVENT_FLAG_STATUS_SET)) 775 result = acpi_clear_gpe(handle, index); 776 else if (!strcmp(buf, "mask\n")) 777 result = acpi_mask_gpe(handle, index, TRUE); 778 else if (!strcmp(buf, "unmask\n")) 779 result = acpi_mask_gpe(handle, index, FALSE); 780 else if (!kstrtoul(buf, 0, &tmp)) 781 all_counters[index].count = tmp; 782 else 783 result = -EINVAL; 784 } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) { 785 int event = index - num_gpes; 786 if (!strcmp(buf, "disable\n") && 787 (status & ACPI_EVENT_FLAG_ENABLE_SET)) 788 result = acpi_disable_event(event, ACPI_NOT_ISR); 789 else if (!strcmp(buf, "enable\n") && 790 !(status & ACPI_EVENT_FLAG_ENABLE_SET)) 791 result = acpi_enable_event(event, ACPI_NOT_ISR); 792 else if (!strcmp(buf, "clear\n") && 793 (status & ACPI_EVENT_FLAG_STATUS_SET)) 794 result = acpi_clear_event(event); 795 else if (!kstrtoul(buf, 0, &tmp)) 796 all_counters[index].count = tmp; 797 else 798 result = -EINVAL; 799 } else 800 all_counters[index].count = strtoul(buf, NULL, 0); 801 802 if (ACPI_FAILURE(result)) 803 result = -EINVAL; 804 end: 805 return result ? result : size; 806 } 807 808 /* 809 * A Quirk Mechanism for GPE Flooding Prevention: 810 * 811 * Quirks may be needed to prevent GPE flooding on a specific GPE. The 812 * flooding typically cannot be detected and automatically prevented by 813 * ACPI_GPE_DISPATCH_NONE check because there is a _Lxx/_Exx prepared in 814 * the AML tables. This normally indicates a feature gap in Linux, thus 815 * instead of providing endless quirk tables, we provide a boot parameter 816 * for those who want this quirk. For example, if the users want to prevent 817 * the GPE flooding for GPE 00, they need to specify the following boot 818 * parameter: 819 * acpi_mask_gpe=0x00 820 * Note, the parameter can be a list (see bitmap_parselist() for the details). 821 * The masking status can be modified by the following runtime controlling 822 * interface: 823 * echo unmask > /sys/firmware/acpi/interrupts/gpe00 824 */ 825 #define ACPI_MASKABLE_GPE_MAX 0x100 826 static DECLARE_BITMAP(acpi_masked_gpes_map, ACPI_MASKABLE_GPE_MAX) __initdata; 827 828 static int __init acpi_gpe_set_masked_gpes(char *val) 829 { 830 int ret; 831 u8 gpe; 832 833 ret = kstrtou8(val, 0, &gpe); 834 if (ret) { 835 ret = bitmap_parselist(val, acpi_masked_gpes_map, ACPI_MASKABLE_GPE_MAX); 836 if (ret) 837 return ret; 838 } else 839 set_bit(gpe, acpi_masked_gpes_map); 840 841 return 1; 842 } 843 __setup("acpi_mask_gpe=", acpi_gpe_set_masked_gpes); 844 845 void __init acpi_gpe_apply_masked_gpes(void) 846 { 847 acpi_handle handle; 848 acpi_status status; 849 u16 gpe; 850 851 for_each_set_bit(gpe, acpi_masked_gpes_map, ACPI_MASKABLE_GPE_MAX) { 852 status = acpi_get_gpe_device(gpe, &handle); 853 if (ACPI_SUCCESS(status)) { 854 pr_info("Masking GPE 0x%x.\n", gpe); 855 (void)acpi_mask_gpe(handle, gpe, TRUE); 856 } 857 } 858 } 859 860 void acpi_irq_stats_init(void) 861 { 862 acpi_status status; 863 int i; 864 865 if (all_counters) 866 return; 867 868 num_gpes = acpi_current_gpe_count; 869 num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA; 870 871 all_attrs = kzalloc_objs(*all_attrs, num_counters + 1); 872 if (all_attrs == NULL) 873 return; 874 875 all_counters = kzalloc_objs(*all_counters, num_counters); 876 if (all_counters == NULL) 877 goto fail; 878 879 status = acpi_install_global_event_handler(acpi_global_event_handler, NULL); 880 if (ACPI_FAILURE(status)) 881 goto fail; 882 883 counter_attrs = kzalloc_objs(*counter_attrs, num_counters); 884 if (counter_attrs == NULL) 885 goto fail; 886 887 for (i = 0; i < num_counters; ++i) { 888 char buffer[12]; 889 char *name; 890 891 if (i < num_gpes) 892 sprintf(buffer, "gpe%02X", i); 893 else if (i == num_gpes + ACPI_EVENT_PMTIMER) 894 sprintf(buffer, "ff_pmtimer"); 895 else if (i == num_gpes + ACPI_EVENT_GLOBAL) 896 sprintf(buffer, "ff_gbl_lock"); 897 else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON) 898 sprintf(buffer, "ff_pwr_btn"); 899 else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON) 900 sprintf(buffer, "ff_slp_btn"); 901 else if (i == num_gpes + ACPI_EVENT_RTC) 902 sprintf(buffer, "ff_rt_clk"); 903 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE) 904 sprintf(buffer, "gpe_all"); 905 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) 906 sprintf(buffer, "sci"); 907 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT) 908 sprintf(buffer, "sci_not"); 909 else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR) 910 sprintf(buffer, "error"); 911 else 912 sprintf(buffer, "bug%02X", i); 913 914 name = kstrdup(buffer, GFP_KERNEL); 915 if (name == NULL) 916 goto fail; 917 918 sysfs_attr_init(&counter_attrs[i].attr); 919 counter_attrs[i].attr.name = name; 920 counter_attrs[i].attr.mode = 0644; 921 counter_attrs[i].show = counter_show; 922 counter_attrs[i].store = counter_set; 923 924 all_attrs[i] = &counter_attrs[i].attr; 925 } 926 927 interrupt_stats_attr_group.attrs = all_attrs; 928 if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group)) 929 return; 930 931 fail: 932 delete_gpe_attr_array(); 933 } 934 935 static void __exit interrupt_stats_exit(void) 936 { 937 sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group); 938 939 delete_gpe_attr_array(); 940 } 941 942 static ssize_t pm_profile_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 943 { 944 return sysfs_emit(buf, "%d\n", acpi_gbl_FADT.preferred_profile); 945 } 946 947 static const struct kobj_attribute pm_profile_attr = __ATTR_RO(pm_profile); 948 949 static ssize_t enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 950 { 951 struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj); 952 953 return sysfs_emit(buf, "%d\n", hotplug->enabled); 954 } 955 956 static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, 957 const char *buf, size_t size) 958 { 959 struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj); 960 unsigned int val; 961 962 if (kstrtouint(buf, 10, &val) || val > 1) 963 return -EINVAL; 964 965 acpi_scan_hotplug_enabled(hotplug, val); 966 return size; 967 } 968 969 static struct kobj_attribute hotplug_enabled_attr = __ATTR_RW(enabled); 970 971 static struct attribute *hotplug_profile_attrs[] = { 972 &hotplug_enabled_attr.attr, 973 NULL 974 }; 975 ATTRIBUTE_GROUPS(hotplug_profile); 976 977 static const struct kobj_type acpi_hotplug_profile_ktype = { 978 .sysfs_ops = &kobj_sysfs_ops, 979 .default_groups = hotplug_profile_groups, 980 }; 981 982 void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug, 983 const char *name) 984 { 985 int error; 986 987 if (!hotplug_kobj) 988 goto err_out; 989 990 error = kobject_init_and_add(&hotplug->kobj, 991 &acpi_hotplug_profile_ktype, hotplug_kobj, "%s", name); 992 if (error) { 993 kobject_put(&hotplug->kobj); 994 goto err_out; 995 } 996 997 kobject_uevent(&hotplug->kobj, KOBJ_ADD); 998 return; 999 1000 err_out: 1001 pr_err("Unable to add hotplug profile '%s'\n", name); 1002 } 1003 1004 static ssize_t force_remove_show(struct kobject *kobj, 1005 struct kobj_attribute *attr, char *buf) 1006 { 1007 return sysfs_emit(buf, "%d\n", 0); 1008 } 1009 1010 static ssize_t force_remove_store(struct kobject *kobj, 1011 struct kobj_attribute *attr, 1012 const char *buf, size_t size) 1013 { 1014 bool val; 1015 int ret; 1016 1017 ret = kstrtobool(buf, &val); 1018 if (ret < 0) 1019 return ret; 1020 1021 if (val) { 1022 pr_err("Enabling force_remove is not supported anymore. Please report to linux-acpi@vger.kernel.org if you depend on this functionality\n"); 1023 return -EINVAL; 1024 } 1025 return size; 1026 } 1027 1028 static const struct kobj_attribute force_remove_attr = __ATTR_RW(force_remove); 1029 1030 int __init acpi_sysfs_init(void) 1031 { 1032 int result; 1033 1034 result = acpi_tables_sysfs_init(); 1035 if (result) 1036 return result; 1037 1038 hotplug_kobj = kobject_create_and_add("hotplug", acpi_kobj); 1039 if (!hotplug_kobj) 1040 return -ENOMEM; 1041 1042 result = sysfs_create_file(hotplug_kobj, &force_remove_attr.attr); 1043 if (result) 1044 return result; 1045 1046 result = sysfs_create_file(acpi_kobj, &pm_profile_attr.attr); 1047 return result; 1048 } 1049