sysfs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * sysfs.c - ACPI sysfs interface to userspace.
  3. */
  4. #define pr_fmt(fmt) "ACPI: " fmt
  5. #include <linux/init.h>
  6. #include <linux/kernel.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/acpi.h>
  9. #include "internal.h"
  10. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  11. ACPI_MODULE_NAME("sysfs");
  12. #ifdef CONFIG_ACPI_DEBUG
  13. /*
  14. * ACPI debug sysfs I/F, including:
  15. * /sys/modules/acpi/parameters/debug_layer
  16. * /sys/modules/acpi/parameters/debug_level
  17. * /sys/modules/acpi/parameters/trace_method_name
  18. * /sys/modules/acpi/parameters/trace_state
  19. * /sys/modules/acpi/parameters/trace_debug_layer
  20. * /sys/modules/acpi/parameters/trace_debug_level
  21. */
  22. struct acpi_dlayer {
  23. const char *name;
  24. unsigned long value;
  25. };
  26. struct acpi_dlevel {
  27. const char *name;
  28. unsigned long value;
  29. };
  30. #define ACPI_DEBUG_INIT(v) { .name = #v, .value = v }
  31. static const struct acpi_dlayer acpi_debug_layers[] = {
  32. ACPI_DEBUG_INIT(ACPI_UTILITIES),
  33. ACPI_DEBUG_INIT(ACPI_HARDWARE),
  34. ACPI_DEBUG_INIT(ACPI_EVENTS),
  35. ACPI_DEBUG_INIT(ACPI_TABLES),
  36. ACPI_DEBUG_INIT(ACPI_NAMESPACE),
  37. ACPI_DEBUG_INIT(ACPI_PARSER),
  38. ACPI_DEBUG_INIT(ACPI_DISPATCHER),
  39. ACPI_DEBUG_INIT(ACPI_EXECUTER),
  40. ACPI_DEBUG_INIT(ACPI_RESOURCES),
  41. ACPI_DEBUG_INIT(ACPI_CA_DEBUGGER),
  42. ACPI_DEBUG_INIT(ACPI_OS_SERVICES),
  43. ACPI_DEBUG_INIT(ACPI_CA_DISASSEMBLER),
  44. ACPI_DEBUG_INIT(ACPI_COMPILER),
  45. ACPI_DEBUG_INIT(ACPI_TOOLS),
  46. ACPI_DEBUG_INIT(ACPI_BUS_COMPONENT),
  47. ACPI_DEBUG_INIT(ACPI_AC_COMPONENT),
  48. ACPI_DEBUG_INIT(ACPI_BATTERY_COMPONENT),
  49. ACPI_DEBUG_INIT(ACPI_BUTTON_COMPONENT),
  50. ACPI_DEBUG_INIT(ACPI_SBS_COMPONENT),
  51. ACPI_DEBUG_INIT(ACPI_FAN_COMPONENT),
  52. ACPI_DEBUG_INIT(ACPI_PCI_COMPONENT),
  53. ACPI_DEBUG_INIT(ACPI_POWER_COMPONENT),
  54. ACPI_DEBUG_INIT(ACPI_CONTAINER_COMPONENT),
  55. ACPI_DEBUG_INIT(ACPI_SYSTEM_COMPONENT),
  56. ACPI_DEBUG_INIT(ACPI_THERMAL_COMPONENT),
  57. ACPI_DEBUG_INIT(ACPI_MEMORY_DEVICE_COMPONENT),
  58. ACPI_DEBUG_INIT(ACPI_VIDEO_COMPONENT),
  59. ACPI_DEBUG_INIT(ACPI_PROCESSOR_COMPONENT),
  60. };
  61. static const struct acpi_dlevel acpi_debug_levels[] = {
  62. ACPI_DEBUG_INIT(ACPI_LV_INIT),
  63. ACPI_DEBUG_INIT(ACPI_LV_DEBUG_OBJECT),
  64. ACPI_DEBUG_INIT(ACPI_LV_INFO),
  65. ACPI_DEBUG_INIT(ACPI_LV_REPAIR),
  66. ACPI_DEBUG_INIT(ACPI_LV_TRACE_POINT),
  67. ACPI_DEBUG_INIT(ACPI_LV_INIT_NAMES),
  68. ACPI_DEBUG_INIT(ACPI_LV_PARSE),
  69. ACPI_DEBUG_INIT(ACPI_LV_LOAD),
  70. ACPI_DEBUG_INIT(ACPI_LV_DISPATCH),
  71. ACPI_DEBUG_INIT(ACPI_LV_EXEC),
  72. ACPI_DEBUG_INIT(ACPI_LV_NAMES),
  73. ACPI_DEBUG_INIT(ACPI_LV_OPREGION),
  74. ACPI_DEBUG_INIT(ACPI_LV_BFIELD),
  75. ACPI_DEBUG_INIT(ACPI_LV_TABLES),
  76. ACPI_DEBUG_INIT(ACPI_LV_VALUES),
  77. ACPI_DEBUG_INIT(ACPI_LV_OBJECTS),
  78. ACPI_DEBUG_INIT(ACPI_LV_RESOURCES),
  79. ACPI_DEBUG_INIT(ACPI_LV_USER_REQUESTS),
  80. ACPI_DEBUG_INIT(ACPI_LV_PACKAGE),
  81. ACPI_DEBUG_INIT(ACPI_LV_ALLOCATIONS),
  82. ACPI_DEBUG_INIT(ACPI_LV_FUNCTIONS),
  83. ACPI_DEBUG_INIT(ACPI_LV_OPTIMIZATIONS),
  84. ACPI_DEBUG_INIT(ACPI_LV_MUTEX),
  85. ACPI_DEBUG_INIT(ACPI_LV_THREADS),
  86. ACPI_DEBUG_INIT(ACPI_LV_IO),
  87. ACPI_DEBUG_INIT(ACPI_LV_INTERRUPTS),
  88. ACPI_DEBUG_INIT(ACPI_LV_AML_DISASSEMBLE),
  89. ACPI_DEBUG_INIT(ACPI_LV_VERBOSE_INFO),
  90. ACPI_DEBUG_INIT(ACPI_LV_FULL_TABLES),
  91. ACPI_DEBUG_INIT(ACPI_LV_EVENTS),
  92. };
  93. static int param_get_debug_layer(char *buffer, const struct kernel_param *kp)
  94. {
  95. int result = 0;
  96. int i;
  97. result = sprintf(buffer, "%-25s\tHex SET\n", "Description");
  98. for (i = 0; i < ARRAY_SIZE(acpi_debug_layers); i++) {
  99. result += sprintf(buffer + result, "%-25s\t0x%08lX [%c]\n",
  100. acpi_debug_layers[i].name,
  101. acpi_debug_layers[i].value,
  102. (acpi_dbg_layer & acpi_debug_layers[i].value)
  103. ? '*' : ' ');
  104. }
  105. result +=
  106. sprintf(buffer + result, "%-25s\t0x%08X [%c]\n", "ACPI_ALL_DRIVERS",
  107. ACPI_ALL_DRIVERS,
  108. (acpi_dbg_layer & ACPI_ALL_DRIVERS) ==
  109. ACPI_ALL_DRIVERS ? '*' : (acpi_dbg_layer & ACPI_ALL_DRIVERS)
  110. == 0 ? ' ' : '-');
  111. result +=
  112. sprintf(buffer + result,
  113. "--\ndebug_layer = 0x%08X ( * = enabled)\n",
  114. acpi_dbg_layer);
  115. return result;
  116. }
  117. static int param_get_debug_level(char *buffer, const struct kernel_param *kp)
  118. {
  119. int result = 0;
  120. int i;
  121. result = sprintf(buffer, "%-25s\tHex SET\n", "Description");
  122. for (i = 0; i < ARRAY_SIZE(acpi_debug_levels); i++) {
  123. result += sprintf(buffer + result, "%-25s\t0x%08lX [%c]\n",
  124. acpi_debug_levels[i].name,
  125. acpi_debug_levels[i].value,
  126. (acpi_dbg_level & acpi_debug_levels[i].value)
  127. ? '*' : ' ');
  128. }
  129. result +=
  130. sprintf(buffer + result, "--\ndebug_level = 0x%08X (* = enabled)\n",
  131. acpi_dbg_level);
  132. return result;
  133. }
  134. static const struct kernel_param_ops param_ops_debug_layer = {
  135. .set = param_set_uint,
  136. .get = param_get_debug_layer,
  137. };
  138. static const struct kernel_param_ops param_ops_debug_level = {
  139. .set = param_set_uint,
  140. .get = param_get_debug_level,
  141. };
  142. module_param_cb(debug_layer, &param_ops_debug_layer, &acpi_dbg_layer, 0644);
  143. module_param_cb(debug_level, &param_ops_debug_level, &acpi_dbg_level, 0644);
  144. static char trace_method_name[1024];
  145. int param_set_trace_method_name(const char *val, const struct kernel_param *kp)
  146. {
  147. u32 saved_flags = 0;
  148. bool is_abs_path = true;
  149. if (*val != '\\')
  150. is_abs_path = false;
  151. if ((is_abs_path && strlen(val) > 1023) ||
  152. (!is_abs_path && strlen(val) > 1022)) {
  153. pr_err("%s: string parameter too long\n", kp->name);
  154. return -ENOSPC;
  155. }
  156. /*
  157. * It's not safe to update acpi_gbl_trace_method_name without
  158. * having the tracer stopped, so we save the original tracer
  159. * state and disable it.
  160. */
  161. saved_flags = acpi_gbl_trace_flags;
  162. (void)acpi_debug_trace(NULL,
  163. acpi_gbl_trace_dbg_level,
  164. acpi_gbl_trace_dbg_layer,
  165. 0);
  166. /* This is a hack. We can't kmalloc in early boot. */
  167. if (is_abs_path)
  168. strcpy(trace_method_name, val);
  169. else {
  170. trace_method_name[0] = '\\';
  171. strcpy(trace_method_name+1, val);
  172. }
  173. /* Restore the original tracer state */
  174. (void)acpi_debug_trace(trace_method_name,
  175. acpi_gbl_trace_dbg_level,
  176. acpi_gbl_trace_dbg_layer,
  177. saved_flags);
  178. return 0;
  179. }
  180. static int param_get_trace_method_name(char *buffer, const struct kernel_param *kp)
  181. {
  182. return scnprintf(buffer, PAGE_SIZE, "%s", acpi_gbl_trace_method_name);
  183. }
  184. static const struct kernel_param_ops param_ops_trace_method = {
  185. .set = param_set_trace_method_name,
  186. .get = param_get_trace_method_name,
  187. };
  188. static const struct kernel_param_ops param_ops_trace_attrib = {
  189. .set = param_set_uint,
  190. .get = param_get_uint,
  191. };
  192. module_param_cb(trace_method_name, &param_ops_trace_method, &trace_method_name, 0644);
  193. module_param_cb(trace_debug_layer, &param_ops_trace_attrib, &acpi_gbl_trace_dbg_layer, 0644);
  194. module_param_cb(trace_debug_level, &param_ops_trace_attrib, &acpi_gbl_trace_dbg_level, 0644);
  195. static int param_set_trace_state(const char *val, struct kernel_param *kp)
  196. {
  197. acpi_status status;
  198. const char *method = trace_method_name;
  199. u32 flags = 0;
  200. /* So "xxx-once" comparison should go prior than "xxx" comparison */
  201. #define acpi_compare_param(val, key) \
  202. strncmp((val), (key), sizeof(key) - 1)
  203. if (!acpi_compare_param(val, "enable")) {
  204. method = NULL;
  205. flags = ACPI_TRACE_ENABLED;
  206. } else if (!acpi_compare_param(val, "disable"))
  207. method = NULL;
  208. else if (!acpi_compare_param(val, "method-once"))
  209. flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT;
  210. else if (!acpi_compare_param(val, "method"))
  211. flags = ACPI_TRACE_ENABLED;
  212. else if (!acpi_compare_param(val, "opcode-once"))
  213. flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT | ACPI_TRACE_OPCODE;
  214. else if (!acpi_compare_param(val, "opcode"))
  215. flags = ACPI_TRACE_ENABLED | ACPI_TRACE_OPCODE;
  216. else
  217. return -EINVAL;
  218. status = acpi_debug_trace(method,
  219. acpi_gbl_trace_dbg_level,
  220. acpi_gbl_trace_dbg_layer,
  221. flags);
  222. if (ACPI_FAILURE(status))
  223. return -EBUSY;
  224. return 0;
  225. }
  226. static int param_get_trace_state(char *buffer, struct kernel_param *kp)
  227. {
  228. if (!(acpi_gbl_trace_flags & ACPI_TRACE_ENABLED))
  229. return sprintf(buffer, "disable");
  230. else {
  231. if (acpi_gbl_trace_method_name) {
  232. if (acpi_gbl_trace_flags & ACPI_TRACE_ONESHOT)
  233. return sprintf(buffer, "method-once");
  234. else
  235. return sprintf(buffer, "method");
  236. } else
  237. return sprintf(buffer, "enable");
  238. }
  239. return 0;
  240. }
  241. module_param_call(trace_state, param_set_trace_state, param_get_trace_state,
  242. NULL, 0644);
  243. #endif /* CONFIG_ACPI_DEBUG */
  244. /* /sys/modules/acpi/parameters/aml_debug_output */
  245. module_param_named(aml_debug_output, acpi_gbl_enable_aml_debug_object,
  246. byte, 0644);
  247. MODULE_PARM_DESC(aml_debug_output,
  248. "To enable/disable the ACPI Debug Object output.");
  249. /* /sys/module/acpi/parameters/acpica_version */
  250. static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
  251. {
  252. int result;
  253. result = sprintf(buffer, "%x", ACPI_CA_VERSION);
  254. return result;
  255. }
  256. module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
  257. /*
  258. * ACPI table sysfs I/F:
  259. * /sys/firmware/acpi/tables/
  260. * /sys/firmware/acpi/tables/dynamic/
  261. */
  262. static LIST_HEAD(acpi_table_attr_list);
  263. static struct kobject *tables_kobj;
  264. static struct kobject *dynamic_tables_kobj;
  265. static struct kobject *hotplug_kobj;
  266. #define ACPI_MAX_TABLE_INSTANCES 999
  267. #define ACPI_INST_SIZE 4 /* including trailing 0 */
  268. struct acpi_table_attr {
  269. struct bin_attribute attr;
  270. char name[ACPI_NAME_SIZE];
  271. int instance;
  272. char filename[ACPI_NAME_SIZE+ACPI_INST_SIZE];
  273. struct list_head node;
  274. };
  275. static ssize_t acpi_table_show(struct file *filp, struct kobject *kobj,
  276. struct bin_attribute *bin_attr, char *buf,
  277. loff_t offset, size_t count)
  278. {
  279. struct acpi_table_attr *table_attr =
  280. container_of(bin_attr, struct acpi_table_attr, attr);
  281. struct acpi_table_header *table_header = NULL;
  282. acpi_status status;
  283. ssize_t rc;
  284. status = acpi_get_table(table_attr->name, table_attr->instance,
  285. &table_header);
  286. if (ACPI_FAILURE(status))
  287. return -ENODEV;
  288. rc = memory_read_from_buffer(buf, count, &offset, table_header,
  289. table_header->length);
  290. acpi_put_table(table_header);
  291. return rc;
  292. }
  293. static int acpi_table_attr_init(struct kobject *tables_obj,
  294. struct acpi_table_attr *table_attr,
  295. struct acpi_table_header *table_header)
  296. {
  297. struct acpi_table_header *header = NULL;
  298. struct acpi_table_attr *attr = NULL;
  299. char instance_str[ACPI_INST_SIZE];
  300. sysfs_attr_init(&table_attr->attr.attr);
  301. ACPI_MOVE_NAME(table_attr->name, table_header->signature);
  302. list_for_each_entry(attr, &acpi_table_attr_list, node) {
  303. if (ACPI_COMPARE_NAME(table_attr->name, attr->name))
  304. if (table_attr->instance < attr->instance)
  305. table_attr->instance = attr->instance;
  306. }
  307. table_attr->instance++;
  308. if (table_attr->instance > ACPI_MAX_TABLE_INSTANCES) {
  309. pr_warn("%4.4s: too many table instances\n",
  310. table_attr->name);
  311. return -ERANGE;
  312. }
  313. ACPI_MOVE_NAME(table_attr->filename, table_header->signature);
  314. table_attr->filename[ACPI_NAME_SIZE] = '\0';
  315. if (table_attr->instance > 1 || (table_attr->instance == 1 &&
  316. !acpi_get_table
  317. (table_header->signature, 2, &header))) {
  318. snprintf(instance_str, sizeof(instance_str), "%u",
  319. table_attr->instance);
  320. strcat(table_attr->filename, instance_str);
  321. }
  322. table_attr->attr.size = table_header->length;
  323. table_attr->attr.read = acpi_table_show;
  324. table_attr->attr.attr.name = table_attr->filename;
  325. table_attr->attr.attr.mode = 0400;
  326. return sysfs_create_bin_file(tables_obj, &table_attr->attr);
  327. }
  328. acpi_status acpi_sysfs_table_handler(u32 event, void *table, void *context)
  329. {
  330. struct acpi_table_attr *table_attr;
  331. switch (event) {
  332. case ACPI_TABLE_EVENT_INSTALL:
  333. table_attr =
  334. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  335. if (!table_attr)
  336. return AE_NO_MEMORY;
  337. if (acpi_table_attr_init(dynamic_tables_kobj,
  338. table_attr, table)) {
  339. kfree(table_attr);
  340. return AE_ERROR;
  341. }
  342. list_add_tail(&table_attr->node, &acpi_table_attr_list);
  343. break;
  344. case ACPI_TABLE_EVENT_LOAD:
  345. case ACPI_TABLE_EVENT_UNLOAD:
  346. case ACPI_TABLE_EVENT_UNINSTALL:
  347. /*
  348. * we do not need to do anything right now
  349. * because the table is not deleted from the
  350. * global table list when unloading it.
  351. */
  352. break;
  353. default:
  354. return AE_BAD_PARAMETER;
  355. }
  356. return AE_OK;
  357. }
  358. static int acpi_tables_sysfs_init(void)
  359. {
  360. struct acpi_table_attr *table_attr;
  361. struct acpi_table_header *table_header = NULL;
  362. int table_index;
  363. acpi_status status;
  364. int ret;
  365. tables_kobj = kobject_create_and_add("tables", acpi_kobj);
  366. if (!tables_kobj)
  367. goto err;
  368. dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
  369. if (!dynamic_tables_kobj)
  370. goto err_dynamic_tables;
  371. for (table_index = 0;; table_index++) {
  372. status = acpi_get_table_by_index(table_index, &table_header);
  373. if (status == AE_BAD_PARAMETER)
  374. break;
  375. if (ACPI_FAILURE(status))
  376. continue;
  377. table_attr = kzalloc(sizeof(*table_attr), GFP_KERNEL);
  378. if (!table_attr)
  379. return -ENOMEM;
  380. ret = acpi_table_attr_init(tables_kobj,
  381. table_attr, table_header);
  382. if (ret) {
  383. kfree(table_attr);
  384. return ret;
  385. }
  386. list_add_tail(&table_attr->node, &acpi_table_attr_list);
  387. }
  388. kobject_uevent(tables_kobj, KOBJ_ADD);
  389. kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
  390. return 0;
  391. err_dynamic_tables:
  392. kobject_put(tables_kobj);
  393. err:
  394. return -ENOMEM;
  395. }
  396. /*
  397. * Detailed ACPI IRQ counters:
  398. * /sys/firmware/acpi/interrupts/
  399. */
  400. u32 acpi_irq_handled;
  401. u32 acpi_irq_not_handled;
  402. #define COUNT_GPE 0
  403. #define COUNT_SCI 1 /* acpi_irq_handled */
  404. #define COUNT_SCI_NOT 2 /* acpi_irq_not_handled */
  405. #define COUNT_ERROR 3 /* other */
  406. #define NUM_COUNTERS_EXTRA 4
  407. struct event_counter {
  408. u32 count;
  409. u32 flags;
  410. };
  411. static struct event_counter *all_counters;
  412. static u32 num_gpes;
  413. static u32 num_counters;
  414. static struct attribute **all_attrs;
  415. static u32 acpi_gpe_count;
  416. static struct attribute_group interrupt_stats_attr_group = {
  417. .name = "interrupts",
  418. };
  419. static struct kobj_attribute *counter_attrs;
  420. static void delete_gpe_attr_array(void)
  421. {
  422. struct event_counter *tmp = all_counters;
  423. all_counters = NULL;
  424. kfree(tmp);
  425. if (counter_attrs) {
  426. int i;
  427. for (i = 0; i < num_gpes; i++)
  428. kfree(counter_attrs[i].attr.name);
  429. kfree(counter_attrs);
  430. }
  431. kfree(all_attrs);
  432. return;
  433. }
  434. static void gpe_count(u32 gpe_number)
  435. {
  436. acpi_gpe_count++;
  437. if (!all_counters)
  438. return;
  439. if (gpe_number < num_gpes)
  440. all_counters[gpe_number].count++;
  441. else
  442. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
  443. COUNT_ERROR].count++;
  444. return;
  445. }
  446. static void fixed_event_count(u32 event_number)
  447. {
  448. if (!all_counters)
  449. return;
  450. if (event_number < ACPI_NUM_FIXED_EVENTS)
  451. all_counters[num_gpes + event_number].count++;
  452. else
  453. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
  454. COUNT_ERROR].count++;
  455. return;
  456. }
  457. static void acpi_global_event_handler(u32 event_type, acpi_handle device,
  458. u32 event_number, void *context)
  459. {
  460. if (event_type == ACPI_EVENT_TYPE_GPE) {
  461. gpe_count(event_number);
  462. pr_debug("GPE event 0x%02x\n", event_number);
  463. } else if (event_type == ACPI_EVENT_TYPE_FIXED) {
  464. fixed_event_count(event_number);
  465. pr_debug("Fixed event 0x%02x\n", event_number);
  466. } else {
  467. pr_debug("Other event 0x%02x\n", event_number);
  468. }
  469. }
  470. static int get_status(u32 index, acpi_event_status *status,
  471. acpi_handle *handle)
  472. {
  473. int result;
  474. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  475. return -EINVAL;
  476. if (index < num_gpes) {
  477. result = acpi_get_gpe_device(index, handle);
  478. if (result) {
  479. ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
  480. "Invalid GPE 0x%x", index));
  481. return result;
  482. }
  483. result = acpi_get_gpe_status(*handle, index, status);
  484. } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
  485. result = acpi_get_event_status(index - num_gpes, status);
  486. return result;
  487. }
  488. static ssize_t counter_show(struct kobject *kobj,
  489. struct kobj_attribute *attr, char *buf)
  490. {
  491. int index = attr - counter_attrs;
  492. int size;
  493. acpi_handle handle;
  494. acpi_event_status status;
  495. int result = 0;
  496. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
  497. acpi_irq_handled;
  498. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count =
  499. acpi_irq_not_handled;
  500. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
  501. acpi_gpe_count;
  502. size = sprintf(buf, "%8u", all_counters[index].count);
  503. /* "gpe_all" or "sci" */
  504. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  505. goto end;
  506. result = get_status(index, &status, &handle);
  507. if (result)
  508. goto end;
  509. if (status & ACPI_EVENT_FLAG_ENABLE_SET)
  510. size += sprintf(buf + size, " EN");
  511. else
  512. size += sprintf(buf + size, " ");
  513. if (status & ACPI_EVENT_FLAG_STATUS_SET)
  514. size += sprintf(buf + size, " STS");
  515. else
  516. size += sprintf(buf + size, " ");
  517. if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER))
  518. size += sprintf(buf + size, " invalid ");
  519. else if (status & ACPI_EVENT_FLAG_ENABLED)
  520. size += sprintf(buf + size, " enabled ");
  521. else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
  522. size += sprintf(buf + size, " wake_enabled");
  523. else
  524. size += sprintf(buf + size, " disabled ");
  525. if (status & ACPI_EVENT_FLAG_MASKED)
  526. size += sprintf(buf + size, " masked ");
  527. else
  528. size += sprintf(buf + size, " unmasked");
  529. end:
  530. size += sprintf(buf + size, "\n");
  531. return result ? result : size;
  532. }
  533. /*
  534. * counter_set() sets the specified counter.
  535. * setting the total "sci" file to any value clears all counters.
  536. * enable/disable/clear a gpe/fixed event in user space.
  537. */
  538. static ssize_t counter_set(struct kobject *kobj,
  539. struct kobj_attribute *attr, const char *buf,
  540. size_t size)
  541. {
  542. int index = attr - counter_attrs;
  543. acpi_event_status status;
  544. acpi_handle handle;
  545. int result = 0;
  546. unsigned long tmp;
  547. if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
  548. int i;
  549. for (i = 0; i < num_counters; ++i)
  550. all_counters[i].count = 0;
  551. acpi_gpe_count = 0;
  552. acpi_irq_handled = 0;
  553. acpi_irq_not_handled = 0;
  554. goto end;
  555. }
  556. /* show the event status for both GPEs and Fixed Events */
  557. result = get_status(index, &status, &handle);
  558. if (result)
  559. goto end;
  560. if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER)) {
  561. printk(KERN_WARNING PREFIX
  562. "Can not change Invalid GPE/Fixed Event status\n");
  563. return -EINVAL;
  564. }
  565. if (index < num_gpes) {
  566. if (!strcmp(buf, "disable\n") &&
  567. (status & ACPI_EVENT_FLAG_ENABLED))
  568. result = acpi_disable_gpe(handle, index);
  569. else if (!strcmp(buf, "enable\n") &&
  570. !(status & ACPI_EVENT_FLAG_ENABLED))
  571. result = acpi_enable_gpe(handle, index);
  572. else if (!strcmp(buf, "clear\n") &&
  573. (status & ACPI_EVENT_FLAG_STATUS_SET))
  574. result = acpi_clear_gpe(handle, index);
  575. else if (!strcmp(buf, "mask\n"))
  576. result = acpi_mask_gpe(handle, index, TRUE);
  577. else if (!strcmp(buf, "unmask\n"))
  578. result = acpi_mask_gpe(handle, index, FALSE);
  579. else if (!kstrtoul(buf, 0, &tmp))
  580. all_counters[index].count = tmp;
  581. else
  582. result = -EINVAL;
  583. } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
  584. int event = index - num_gpes;
  585. if (!strcmp(buf, "disable\n") &&
  586. (status & ACPI_EVENT_FLAG_ENABLE_SET))
  587. result = acpi_disable_event(event, ACPI_NOT_ISR);
  588. else if (!strcmp(buf, "enable\n") &&
  589. !(status & ACPI_EVENT_FLAG_ENABLE_SET))
  590. result = acpi_enable_event(event, ACPI_NOT_ISR);
  591. else if (!strcmp(buf, "clear\n") &&
  592. (status & ACPI_EVENT_FLAG_STATUS_SET))
  593. result = acpi_clear_event(event);
  594. else if (!kstrtoul(buf, 0, &tmp))
  595. all_counters[index].count = tmp;
  596. else
  597. result = -EINVAL;
  598. } else
  599. all_counters[index].count = strtoul(buf, NULL, 0);
  600. if (ACPI_FAILURE(result))
  601. result = -EINVAL;
  602. end:
  603. return result ? result : size;
  604. }
  605. /*
  606. * A Quirk Mechanism for GPE Flooding Prevention:
  607. *
  608. * Quirks may be needed to prevent GPE flooding on a specific GPE. The
  609. * flooding typically cannot be detected and automatically prevented by
  610. * ACPI_GPE_DISPATCH_NONE check because there is a _Lxx/_Exx prepared in
  611. * the AML tables. This normally indicates a feature gap in Linux, thus
  612. * instead of providing endless quirk tables, we provide a boot parameter
  613. * for those who want this quirk. For example, if the users want to prevent
  614. * the GPE flooding for GPE 00, they need to specify the following boot
  615. * parameter:
  616. * acpi_mask_gpe=0x00
  617. * The masking status can be modified by the following runtime controlling
  618. * interface:
  619. * echo unmask > /sys/firmware/acpi/interrupts/gpe00
  620. */
  621. /*
  622. * Currently, the GPE flooding prevention only supports to mask the GPEs
  623. * numbered from 00 to 7f.
  624. */
  625. #define ACPI_MASKABLE_GPE_MAX 0x80
  626. static u64 __initdata acpi_masked_gpes;
  627. static int __init acpi_gpe_set_masked_gpes(char *val)
  628. {
  629. u8 gpe;
  630. if (kstrtou8(val, 0, &gpe) || gpe > ACPI_MASKABLE_GPE_MAX)
  631. return -EINVAL;
  632. acpi_masked_gpes |= ((u64)1<<gpe);
  633. return 1;
  634. }
  635. __setup("acpi_mask_gpe=", acpi_gpe_set_masked_gpes);
  636. void __init acpi_gpe_apply_masked_gpes(void)
  637. {
  638. acpi_handle handle;
  639. acpi_status status;
  640. u8 gpe;
  641. for (gpe = 0;
  642. gpe < min_t(u8, ACPI_MASKABLE_GPE_MAX, acpi_current_gpe_count);
  643. gpe++) {
  644. if (acpi_masked_gpes & ((u64)1<<gpe)) {
  645. status = acpi_get_gpe_device(gpe, &handle);
  646. if (ACPI_SUCCESS(status)) {
  647. pr_info("Masking GPE 0x%x.\n", gpe);
  648. (void)acpi_mask_gpe(handle, gpe, TRUE);
  649. }
  650. }
  651. }
  652. }
  653. void acpi_irq_stats_init(void)
  654. {
  655. acpi_status status;
  656. int i;
  657. if (all_counters)
  658. return;
  659. num_gpes = acpi_current_gpe_count;
  660. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  661. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  662. GFP_KERNEL);
  663. if (all_attrs == NULL)
  664. return;
  665. all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
  666. GFP_KERNEL);
  667. if (all_counters == NULL)
  668. goto fail;
  669. status = acpi_install_global_event_handler(acpi_global_event_handler, NULL);
  670. if (ACPI_FAILURE(status))
  671. goto fail;
  672. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  673. GFP_KERNEL);
  674. if (counter_attrs == NULL)
  675. goto fail;
  676. for (i = 0; i < num_counters; ++i) {
  677. char buffer[12];
  678. char *name;
  679. if (i < num_gpes)
  680. sprintf(buffer, "gpe%02X", i);
  681. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  682. sprintf(buffer, "ff_pmtimer");
  683. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  684. sprintf(buffer, "ff_gbl_lock");
  685. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  686. sprintf(buffer, "ff_pwr_btn");
  687. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  688. sprintf(buffer, "ff_slp_btn");
  689. else if (i == num_gpes + ACPI_EVENT_RTC)
  690. sprintf(buffer, "ff_rt_clk");
  691. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  692. sprintf(buffer, "gpe_all");
  693. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  694. sprintf(buffer, "sci");
  695. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
  696. sprintf(buffer, "sci_not");
  697. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  698. sprintf(buffer, "error");
  699. else
  700. sprintf(buffer, "bug%02X", i);
  701. name = kstrdup(buffer, GFP_KERNEL);
  702. if (name == NULL)
  703. goto fail;
  704. sysfs_attr_init(&counter_attrs[i].attr);
  705. counter_attrs[i].attr.name = name;
  706. counter_attrs[i].attr.mode = 0644;
  707. counter_attrs[i].show = counter_show;
  708. counter_attrs[i].store = counter_set;
  709. all_attrs[i] = &counter_attrs[i].attr;
  710. }
  711. interrupt_stats_attr_group.attrs = all_attrs;
  712. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  713. return;
  714. fail:
  715. delete_gpe_attr_array();
  716. return;
  717. }
  718. static void __exit interrupt_stats_exit(void)
  719. {
  720. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  721. delete_gpe_attr_array();
  722. return;
  723. }
  724. static ssize_t
  725. acpi_show_profile(struct device *dev, struct device_attribute *attr,
  726. char *buf)
  727. {
  728. return sprintf(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
  729. }
  730. static const struct device_attribute pm_profile_attr =
  731. __ATTR(pm_profile, S_IRUGO, acpi_show_profile, NULL);
  732. static ssize_t hotplug_enabled_show(struct kobject *kobj,
  733. struct kobj_attribute *attr, char *buf)
  734. {
  735. struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
  736. return sprintf(buf, "%d\n", hotplug->enabled);
  737. }
  738. static ssize_t hotplug_enabled_store(struct kobject *kobj,
  739. struct kobj_attribute *attr,
  740. const char *buf, size_t size)
  741. {
  742. struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
  743. unsigned int val;
  744. if (kstrtouint(buf, 10, &val) || val > 1)
  745. return -EINVAL;
  746. acpi_scan_hotplug_enabled(hotplug, val);
  747. return size;
  748. }
  749. static struct kobj_attribute hotplug_enabled_attr =
  750. __ATTR(enabled, S_IRUGO | S_IWUSR, hotplug_enabled_show,
  751. hotplug_enabled_store);
  752. static struct attribute *hotplug_profile_attrs[] = {
  753. &hotplug_enabled_attr.attr,
  754. NULL
  755. };
  756. static struct kobj_type acpi_hotplug_profile_ktype = {
  757. .sysfs_ops = &kobj_sysfs_ops,
  758. .default_attrs = hotplug_profile_attrs,
  759. };
  760. void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
  761. const char *name)
  762. {
  763. int error;
  764. if (!hotplug_kobj)
  765. goto err_out;
  766. error = kobject_init_and_add(&hotplug->kobj,
  767. &acpi_hotplug_profile_ktype, hotplug_kobj, "%s", name);
  768. if (error)
  769. goto err_out;
  770. kobject_uevent(&hotplug->kobj, KOBJ_ADD);
  771. return;
  772. err_out:
  773. pr_err(PREFIX "Unable to add hotplug profile '%s'\n", name);
  774. }
  775. static ssize_t force_remove_show(struct kobject *kobj,
  776. struct kobj_attribute *attr, char *buf)
  777. {
  778. return sprintf(buf, "%d\n", 0);
  779. }
  780. static ssize_t force_remove_store(struct kobject *kobj,
  781. struct kobj_attribute *attr,
  782. const char *buf, size_t size)
  783. {
  784. bool val;
  785. int ret;
  786. ret = strtobool(buf, &val);
  787. if (ret < 0)
  788. return ret;
  789. if (val) {
  790. pr_err("Enabling force_remove is not supported anymore. Please report to linux-acpi@vger.kernel.org if you depend on this functionality\n");
  791. return -EINVAL;
  792. }
  793. return size;
  794. }
  795. static const struct kobj_attribute force_remove_attr =
  796. __ATTR(force_remove, S_IRUGO | S_IWUSR, force_remove_show,
  797. force_remove_store);
  798. int __init acpi_sysfs_init(void)
  799. {
  800. int result;
  801. result = acpi_tables_sysfs_init();
  802. if (result)
  803. return result;
  804. hotplug_kobj = kobject_create_and_add("hotplug", acpi_kobj);
  805. if (!hotplug_kobj)
  806. return -ENOMEM;
  807. result = sysfs_create_file(hotplug_kobj, &force_remove_attr.attr);
  808. if (result)
  809. return result;
  810. result = sysfs_create_file(acpi_kobj, &pm_profile_attr.attr);
  811. return result;
  812. }