sysfs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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, struct kernel_param *kp)
  195. {
  196. acpi_status status;
  197. const char *method = trace_method_name;
  198. u32 flags = 0;
  199. /* So "xxx-once" comparison should go prior than "xxx" comparison */
  200. #define acpi_compare_param(val, key) \
  201. strncmp((val), (key), sizeof(key) - 1)
  202. if (!acpi_compare_param(val, "enable")) {
  203. method = NULL;
  204. flags = ACPI_TRACE_ENABLED;
  205. } else if (!acpi_compare_param(val, "disable"))
  206. method = NULL;
  207. else if (!acpi_compare_param(val, "method-once"))
  208. flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT;
  209. else if (!acpi_compare_param(val, "method"))
  210. flags = ACPI_TRACE_ENABLED;
  211. else if (!acpi_compare_param(val, "opcode-once"))
  212. flags = ACPI_TRACE_ENABLED | ACPI_TRACE_ONESHOT | ACPI_TRACE_OPCODE;
  213. else if (!acpi_compare_param(val, "opcode"))
  214. flags = ACPI_TRACE_ENABLED | ACPI_TRACE_OPCODE;
  215. else
  216. return -EINVAL;
  217. status = acpi_debug_trace(method,
  218. acpi_gbl_trace_dbg_level,
  219. acpi_gbl_trace_dbg_layer,
  220. flags);
  221. if (ACPI_FAILURE(status))
  222. return -EBUSY;
  223. return 0;
  224. }
  225. static int param_get_trace_state(char *buffer, struct kernel_param *kp)
  226. {
  227. if (!(acpi_gbl_trace_flags & ACPI_TRACE_ENABLED))
  228. return sprintf(buffer, "disable");
  229. else {
  230. if (acpi_gbl_trace_method_name) {
  231. if (acpi_gbl_trace_flags & ACPI_TRACE_ONESHOT)
  232. return sprintf(buffer, "method-once");
  233. else
  234. return sprintf(buffer, "method");
  235. } else
  236. return sprintf(buffer, "enable");
  237. }
  238. return 0;
  239. }
  240. module_param_call(trace_state, param_set_trace_state, param_get_trace_state,
  241. NULL, 0644);
  242. #endif /* CONFIG_ACPI_DEBUG */
  243. /* /sys/modules/acpi/parameters/aml_debug_output */
  244. module_param_named(aml_debug_output, acpi_gbl_enable_aml_debug_object,
  245. byte, 0644);
  246. MODULE_PARM_DESC(aml_debug_output,
  247. "To enable/disable the ACPI Debug Object output.");
  248. /* /sys/module/acpi/parameters/acpica_version */
  249. static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
  250. {
  251. int result;
  252. result = sprintf(buffer, "%x", ACPI_CA_VERSION);
  253. return result;
  254. }
  255. module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
  256. /*
  257. * ACPI table sysfs I/F:
  258. * /sys/firmware/acpi/tables/
  259. * /sys/firmware/acpi/tables/dynamic/
  260. */
  261. static LIST_HEAD(acpi_table_attr_list);
  262. static struct kobject *tables_kobj;
  263. static struct kobject *dynamic_tables_kobj;
  264. static struct kobject *hotplug_kobj;
  265. #define ACPI_MAX_TABLE_INSTANCES 999
  266. #define ACPI_INST_SIZE 4 /* including trailing 0 */
  267. struct acpi_table_attr {
  268. struct bin_attribute attr;
  269. char name[ACPI_NAME_SIZE];
  270. int instance;
  271. char filename[ACPI_NAME_SIZE+ACPI_INST_SIZE];
  272. struct list_head node;
  273. };
  274. static ssize_t acpi_table_show(struct file *filp, struct kobject *kobj,
  275. struct bin_attribute *bin_attr, char *buf,
  276. loff_t offset, size_t count)
  277. {
  278. struct acpi_table_attr *table_attr =
  279. container_of(bin_attr, struct acpi_table_attr, attr);
  280. struct acpi_table_header *table_header = NULL;
  281. acpi_status status;
  282. status = acpi_get_table(table_attr->name, table_attr->instance,
  283. &table_header);
  284. if (ACPI_FAILURE(status))
  285. return -ENODEV;
  286. return memory_read_from_buffer(buf, count, &offset,
  287. table_header, table_header->length);
  288. }
  289. static int acpi_table_attr_init(struct kobject *tables_obj,
  290. struct acpi_table_attr *table_attr,
  291. struct acpi_table_header *table_header)
  292. {
  293. struct acpi_table_header *header = NULL;
  294. struct acpi_table_attr *attr = NULL;
  295. char instance_str[ACPI_INST_SIZE];
  296. sysfs_attr_init(&table_attr->attr.attr);
  297. ACPI_MOVE_NAME(table_attr->name, table_header->signature);
  298. list_for_each_entry(attr, &acpi_table_attr_list, node) {
  299. if (ACPI_COMPARE_NAME(table_attr->name, attr->name))
  300. if (table_attr->instance < attr->instance)
  301. table_attr->instance = attr->instance;
  302. }
  303. table_attr->instance++;
  304. if (table_attr->instance > ACPI_MAX_TABLE_INSTANCES) {
  305. pr_warn("%4.4s: too many table instances\n",
  306. table_attr->name);
  307. return -ERANGE;
  308. }
  309. ACPI_MOVE_NAME(table_attr->filename, table_header->signature);
  310. table_attr->filename[ACPI_NAME_SIZE] = '\0';
  311. if (table_attr->instance > 1 || (table_attr->instance == 1 &&
  312. !acpi_get_table
  313. (table_header->signature, 2, &header))) {
  314. snprintf(instance_str, sizeof(instance_str), "%u",
  315. table_attr->instance);
  316. strcat(table_attr->filename, instance_str);
  317. }
  318. table_attr->attr.size = table_header->length;
  319. table_attr->attr.read = acpi_table_show;
  320. table_attr->attr.attr.name = table_attr->filename;
  321. table_attr->attr.attr.mode = 0400;
  322. return sysfs_create_bin_file(tables_obj, &table_attr->attr);
  323. }
  324. acpi_status acpi_sysfs_table_handler(u32 event, void *table, void *context)
  325. {
  326. struct acpi_table_attr *table_attr;
  327. switch (event) {
  328. case ACPI_TABLE_EVENT_INSTALL:
  329. table_attr =
  330. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  331. if (!table_attr)
  332. return AE_NO_MEMORY;
  333. if (acpi_table_attr_init(dynamic_tables_kobj,
  334. table_attr, table)) {
  335. kfree(table_attr);
  336. return AE_ERROR;
  337. }
  338. list_add_tail(&table_attr->node, &acpi_table_attr_list);
  339. break;
  340. case ACPI_TABLE_EVENT_LOAD:
  341. case ACPI_TABLE_EVENT_UNLOAD:
  342. case ACPI_TABLE_EVENT_UNINSTALL:
  343. /*
  344. * we do not need to do anything right now
  345. * because the table is not deleted from the
  346. * global table list when unloading it.
  347. */
  348. break;
  349. default:
  350. return AE_BAD_PARAMETER;
  351. }
  352. return AE_OK;
  353. }
  354. static int acpi_tables_sysfs_init(void)
  355. {
  356. struct acpi_table_attr *table_attr;
  357. struct acpi_table_header *table_header = NULL;
  358. int table_index;
  359. acpi_status status;
  360. int ret;
  361. tables_kobj = kobject_create_and_add("tables", acpi_kobj);
  362. if (!tables_kobj)
  363. goto err;
  364. dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
  365. if (!dynamic_tables_kobj)
  366. goto err_dynamic_tables;
  367. for (table_index = 0;; table_index++) {
  368. status = acpi_get_table_by_index(table_index, &table_header);
  369. if (status == AE_BAD_PARAMETER)
  370. break;
  371. if (ACPI_FAILURE(status))
  372. continue;
  373. table_attr = kzalloc(sizeof(*table_attr), GFP_KERNEL);
  374. if (!table_attr)
  375. return -ENOMEM;
  376. ret = acpi_table_attr_init(tables_kobj,
  377. table_attr, table_header);
  378. if (ret) {
  379. kfree(table_attr);
  380. return ret;
  381. }
  382. list_add_tail(&table_attr->node, &acpi_table_attr_list);
  383. }
  384. kobject_uevent(tables_kobj, KOBJ_ADD);
  385. kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
  386. return 0;
  387. err_dynamic_tables:
  388. kobject_put(tables_kobj);
  389. err:
  390. return -ENOMEM;
  391. }
  392. /*
  393. * Detailed ACPI IRQ counters:
  394. * /sys/firmware/acpi/interrupts/
  395. */
  396. u32 acpi_irq_handled;
  397. u32 acpi_irq_not_handled;
  398. #define COUNT_GPE 0
  399. #define COUNT_SCI 1 /* acpi_irq_handled */
  400. #define COUNT_SCI_NOT 2 /* acpi_irq_not_handled */
  401. #define COUNT_ERROR 3 /* other */
  402. #define NUM_COUNTERS_EXTRA 4
  403. struct event_counter {
  404. u32 count;
  405. u32 flags;
  406. };
  407. static struct event_counter *all_counters;
  408. static u32 num_gpes;
  409. static u32 num_counters;
  410. static struct attribute **all_attrs;
  411. static u32 acpi_gpe_count;
  412. static struct attribute_group interrupt_stats_attr_group = {
  413. .name = "interrupts",
  414. };
  415. static struct kobj_attribute *counter_attrs;
  416. static void delete_gpe_attr_array(void)
  417. {
  418. struct event_counter *tmp = all_counters;
  419. all_counters = NULL;
  420. kfree(tmp);
  421. if (counter_attrs) {
  422. int i;
  423. for (i = 0; i < num_gpes; i++)
  424. kfree(counter_attrs[i].attr.name);
  425. kfree(counter_attrs);
  426. }
  427. kfree(all_attrs);
  428. return;
  429. }
  430. static void gpe_count(u32 gpe_number)
  431. {
  432. acpi_gpe_count++;
  433. if (!all_counters)
  434. return;
  435. if (gpe_number < num_gpes)
  436. all_counters[gpe_number].count++;
  437. else
  438. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
  439. COUNT_ERROR].count++;
  440. return;
  441. }
  442. static void fixed_event_count(u32 event_number)
  443. {
  444. if (!all_counters)
  445. return;
  446. if (event_number < ACPI_NUM_FIXED_EVENTS)
  447. all_counters[num_gpes + event_number].count++;
  448. else
  449. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS +
  450. COUNT_ERROR].count++;
  451. return;
  452. }
  453. static void acpi_global_event_handler(u32 event_type, acpi_handle device,
  454. u32 event_number, void *context)
  455. {
  456. if (event_type == ACPI_EVENT_TYPE_GPE)
  457. gpe_count(event_number);
  458. if (event_type == ACPI_EVENT_TYPE_FIXED)
  459. fixed_event_count(event_number);
  460. }
  461. static int get_status(u32 index, acpi_event_status *status,
  462. acpi_handle *handle)
  463. {
  464. int result;
  465. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  466. return -EINVAL;
  467. if (index < num_gpes) {
  468. result = acpi_get_gpe_device(index, handle);
  469. if (result) {
  470. ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
  471. "Invalid GPE 0x%x", index));
  472. return result;
  473. }
  474. result = acpi_get_gpe_status(*handle, index, status);
  475. } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
  476. result = acpi_get_event_status(index - num_gpes, status);
  477. return result;
  478. }
  479. static ssize_t counter_show(struct kobject *kobj,
  480. struct kobj_attribute *attr, char *buf)
  481. {
  482. int index = attr - counter_attrs;
  483. int size;
  484. acpi_handle handle;
  485. acpi_event_status status;
  486. int result = 0;
  487. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
  488. acpi_irq_handled;
  489. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count =
  490. acpi_irq_not_handled;
  491. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
  492. acpi_gpe_count;
  493. size = sprintf(buf, "%8u", all_counters[index].count);
  494. /* "gpe_all" or "sci" */
  495. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  496. goto end;
  497. result = get_status(index, &status, &handle);
  498. if (result)
  499. goto end;
  500. if (status & ACPI_EVENT_FLAG_ENABLE_SET)
  501. size += sprintf(buf + size, " EN");
  502. else
  503. size += sprintf(buf + size, " ");
  504. if (status & ACPI_EVENT_FLAG_STATUS_SET)
  505. size += sprintf(buf + size, " STS");
  506. else
  507. size += sprintf(buf + size, " ");
  508. if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER))
  509. size += sprintf(buf + size, " invalid ");
  510. else if (status & ACPI_EVENT_FLAG_ENABLED)
  511. size += sprintf(buf + size, " enabled ");
  512. else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
  513. size += sprintf(buf + size, " wake_enabled");
  514. else
  515. size += sprintf(buf + size, " disabled ");
  516. if (status & ACPI_EVENT_FLAG_MASKED)
  517. size += sprintf(buf + size, " masked ");
  518. else
  519. size += sprintf(buf + size, " unmasked");
  520. end:
  521. size += sprintf(buf + size, "\n");
  522. return result ? result : size;
  523. }
  524. /*
  525. * counter_set() sets the specified counter.
  526. * setting the total "sci" file to any value clears all counters.
  527. * enable/disable/clear a gpe/fixed event in user space.
  528. */
  529. static ssize_t counter_set(struct kobject *kobj,
  530. struct kobj_attribute *attr, const char *buf,
  531. size_t size)
  532. {
  533. int index = attr - counter_attrs;
  534. acpi_event_status status;
  535. acpi_handle handle;
  536. int result = 0;
  537. unsigned long tmp;
  538. if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
  539. int i;
  540. for (i = 0; i < num_counters; ++i)
  541. all_counters[i].count = 0;
  542. acpi_gpe_count = 0;
  543. acpi_irq_handled = 0;
  544. acpi_irq_not_handled = 0;
  545. goto end;
  546. }
  547. /* show the event status for both GPEs and Fixed Events */
  548. result = get_status(index, &status, &handle);
  549. if (result)
  550. goto end;
  551. if (!(status & ACPI_EVENT_FLAG_HAS_HANDLER)) {
  552. printk(KERN_WARNING PREFIX
  553. "Can not change Invalid GPE/Fixed Event status\n");
  554. return -EINVAL;
  555. }
  556. if (index < num_gpes) {
  557. if (!strcmp(buf, "disable\n") &&
  558. (status & ACPI_EVENT_FLAG_ENABLED))
  559. result = acpi_disable_gpe(handle, index);
  560. else if (!strcmp(buf, "enable\n") &&
  561. !(status & ACPI_EVENT_FLAG_ENABLED))
  562. result = acpi_enable_gpe(handle, index);
  563. else if (!strcmp(buf, "clear\n") &&
  564. (status & ACPI_EVENT_FLAG_STATUS_SET))
  565. result = acpi_clear_gpe(handle, index);
  566. else if (!strcmp(buf, "mask\n"))
  567. result = acpi_mask_gpe(handle, index, TRUE);
  568. else if (!strcmp(buf, "unmask\n"))
  569. result = acpi_mask_gpe(handle, index, FALSE);
  570. else if (!kstrtoul(buf, 0, &tmp))
  571. all_counters[index].count = tmp;
  572. else
  573. result = -EINVAL;
  574. } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
  575. int event = index - num_gpes;
  576. if (!strcmp(buf, "disable\n") &&
  577. (status & ACPI_EVENT_FLAG_ENABLE_SET))
  578. result = acpi_disable_event(event, ACPI_NOT_ISR);
  579. else if (!strcmp(buf, "enable\n") &&
  580. !(status & ACPI_EVENT_FLAG_ENABLE_SET))
  581. result = acpi_enable_event(event, ACPI_NOT_ISR);
  582. else if (!strcmp(buf, "clear\n") &&
  583. (status & ACPI_EVENT_FLAG_STATUS_SET))
  584. result = acpi_clear_event(event);
  585. else if (!kstrtoul(buf, 0, &tmp))
  586. all_counters[index].count = tmp;
  587. else
  588. result = -EINVAL;
  589. } else
  590. all_counters[index].count = strtoul(buf, NULL, 0);
  591. if (ACPI_FAILURE(result))
  592. result = -EINVAL;
  593. end:
  594. return result ? result : size;
  595. }
  596. /*
  597. * A Quirk Mechanism for GPE Flooding Prevention:
  598. *
  599. * Quirks may be needed to prevent GPE flooding on a specific GPE. The
  600. * flooding typically cannot be detected and automatically prevented by
  601. * ACPI_GPE_DISPATCH_NONE check because there is a _Lxx/_Exx prepared in
  602. * the AML tables. This normally indicates a feature gap in Linux, thus
  603. * instead of providing endless quirk tables, we provide a boot parameter
  604. * for those who want this quirk. For example, if the users want to prevent
  605. * the GPE flooding for GPE 00, they need to specify the following boot
  606. * parameter:
  607. * acpi_mask_gpe=0x00
  608. * The masking status can be modified by the following runtime controlling
  609. * interface:
  610. * echo unmask > /sys/firmware/acpi/interrupts/gpe00
  611. */
  612. /*
  613. * Currently, the GPE flooding prevention only supports to mask the GPEs
  614. * numbered from 00 to 7f.
  615. */
  616. #define ACPI_MASKABLE_GPE_MAX 0x80
  617. static u64 __initdata acpi_masked_gpes;
  618. static int __init acpi_gpe_set_masked_gpes(char *val)
  619. {
  620. u8 gpe;
  621. if (kstrtou8(val, 0, &gpe) || gpe > ACPI_MASKABLE_GPE_MAX)
  622. return -EINVAL;
  623. acpi_masked_gpes |= ((u64)1<<gpe);
  624. return 1;
  625. }
  626. __setup("acpi_mask_gpe=", acpi_gpe_set_masked_gpes);
  627. void __init acpi_gpe_apply_masked_gpes(void)
  628. {
  629. acpi_handle handle;
  630. acpi_status status;
  631. u8 gpe;
  632. for (gpe = 0;
  633. gpe < min_t(u8, ACPI_MASKABLE_GPE_MAX, acpi_current_gpe_count);
  634. gpe++) {
  635. if (acpi_masked_gpes & ((u64)1<<gpe)) {
  636. status = acpi_get_gpe_device(gpe, &handle);
  637. if (ACPI_SUCCESS(status)) {
  638. pr_info("Masking GPE 0x%x.\n", gpe);
  639. (void)acpi_mask_gpe(handle, gpe, TRUE);
  640. }
  641. }
  642. }
  643. }
  644. void acpi_irq_stats_init(void)
  645. {
  646. acpi_status status;
  647. int i;
  648. if (all_counters)
  649. return;
  650. num_gpes = acpi_current_gpe_count;
  651. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  652. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  653. GFP_KERNEL);
  654. if (all_attrs == NULL)
  655. return;
  656. all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
  657. GFP_KERNEL);
  658. if (all_counters == NULL)
  659. goto fail;
  660. status = acpi_install_global_event_handler(acpi_global_event_handler, NULL);
  661. if (ACPI_FAILURE(status))
  662. goto fail;
  663. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  664. GFP_KERNEL);
  665. if (counter_attrs == NULL)
  666. goto fail;
  667. for (i = 0; i < num_counters; ++i) {
  668. char buffer[12];
  669. char *name;
  670. if (i < num_gpes)
  671. sprintf(buffer, "gpe%02X", i);
  672. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  673. sprintf(buffer, "ff_pmtimer");
  674. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  675. sprintf(buffer, "ff_gbl_lock");
  676. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  677. sprintf(buffer, "ff_pwr_btn");
  678. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  679. sprintf(buffer, "ff_slp_btn");
  680. else if (i == num_gpes + ACPI_EVENT_RTC)
  681. sprintf(buffer, "ff_rt_clk");
  682. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  683. sprintf(buffer, "gpe_all");
  684. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  685. sprintf(buffer, "sci");
  686. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
  687. sprintf(buffer, "sci_not");
  688. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  689. sprintf(buffer, "error");
  690. else
  691. sprintf(buffer, "bug%02X", i);
  692. name = kstrdup(buffer, GFP_KERNEL);
  693. if (name == NULL)
  694. goto fail;
  695. sysfs_attr_init(&counter_attrs[i].attr);
  696. counter_attrs[i].attr.name = name;
  697. counter_attrs[i].attr.mode = 0644;
  698. counter_attrs[i].show = counter_show;
  699. counter_attrs[i].store = counter_set;
  700. all_attrs[i] = &counter_attrs[i].attr;
  701. }
  702. interrupt_stats_attr_group.attrs = all_attrs;
  703. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  704. return;
  705. fail:
  706. delete_gpe_attr_array();
  707. return;
  708. }
  709. static void __exit interrupt_stats_exit(void)
  710. {
  711. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  712. delete_gpe_attr_array();
  713. return;
  714. }
  715. static ssize_t
  716. acpi_show_profile(struct device *dev, struct device_attribute *attr,
  717. char *buf)
  718. {
  719. return sprintf(buf, "%d\n", acpi_gbl_FADT.preferred_profile);
  720. }
  721. static const struct device_attribute pm_profile_attr =
  722. __ATTR(pm_profile, S_IRUGO, acpi_show_profile, NULL);
  723. static ssize_t hotplug_enabled_show(struct kobject *kobj,
  724. struct kobj_attribute *attr, char *buf)
  725. {
  726. struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
  727. return sprintf(buf, "%d\n", hotplug->enabled);
  728. }
  729. static ssize_t hotplug_enabled_store(struct kobject *kobj,
  730. struct kobj_attribute *attr,
  731. const char *buf, size_t size)
  732. {
  733. struct acpi_hotplug_profile *hotplug = to_acpi_hotplug_profile(kobj);
  734. unsigned int val;
  735. if (kstrtouint(buf, 10, &val) || val > 1)
  736. return -EINVAL;
  737. acpi_scan_hotplug_enabled(hotplug, val);
  738. return size;
  739. }
  740. static struct kobj_attribute hotplug_enabled_attr =
  741. __ATTR(enabled, S_IRUGO | S_IWUSR, hotplug_enabled_show,
  742. hotplug_enabled_store);
  743. static struct attribute *hotplug_profile_attrs[] = {
  744. &hotplug_enabled_attr.attr,
  745. NULL
  746. };
  747. static struct kobj_type acpi_hotplug_profile_ktype = {
  748. .sysfs_ops = &kobj_sysfs_ops,
  749. .default_attrs = hotplug_profile_attrs,
  750. };
  751. void acpi_sysfs_add_hotplug_profile(struct acpi_hotplug_profile *hotplug,
  752. const char *name)
  753. {
  754. int error;
  755. if (!hotplug_kobj)
  756. goto err_out;
  757. error = kobject_init_and_add(&hotplug->kobj,
  758. &acpi_hotplug_profile_ktype, hotplug_kobj, "%s", name);
  759. if (error)
  760. goto err_out;
  761. kobject_uevent(&hotplug->kobj, KOBJ_ADD);
  762. return;
  763. err_out:
  764. pr_err(PREFIX "Unable to add hotplug profile '%s'\n", name);
  765. }
  766. static ssize_t force_remove_show(struct kobject *kobj,
  767. struct kobj_attribute *attr, char *buf)
  768. {
  769. return sprintf(buf, "%d\n", !!acpi_force_hot_remove);
  770. }
  771. static ssize_t force_remove_store(struct kobject *kobj,
  772. struct kobj_attribute *attr,
  773. const char *buf, size_t size)
  774. {
  775. bool val;
  776. int ret;
  777. ret = strtobool(buf, &val);
  778. if (ret < 0)
  779. return ret;
  780. lock_device_hotplug();
  781. acpi_force_hot_remove = val;
  782. unlock_device_hotplug();
  783. return size;
  784. }
  785. static const struct kobj_attribute force_remove_attr =
  786. __ATTR(force_remove, S_IRUGO | S_IWUSR, force_remove_show,
  787. force_remove_store);
  788. int __init acpi_sysfs_init(void)
  789. {
  790. int result;
  791. result = acpi_tables_sysfs_init();
  792. if (result)
  793. return result;
  794. hotplug_kobj = kobject_create_and_add("hotplug", acpi_kobj);
  795. if (!hotplug_kobj)
  796. return -ENOMEM;
  797. result = sysfs_create_file(hotplug_kobj, &force_remove_attr.attr);
  798. if (result)
  799. return result;
  800. result = sysfs_create_file(acpi_kobj, &pm_profile_attr.attr);
  801. return result;
  802. }