sysfs.c 25 KB

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