sysfs.c 27 KB

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