sysfs.c 27 KB

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