sysfs.c 27 KB

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