system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * acpi_system.c - ACPI System Driver ($Revision: 63 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/proc_fs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/string.h>
  30. #include <asm/uaccess.h>
  31. #include <acpi/acpi_drivers.h>
  32. #define PREFIX "ACPI: "
  33. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  34. ACPI_MODULE_NAME("system");
  35. #define ACPI_SYSTEM_CLASS "system"
  36. #define ACPI_SYSTEM_DEVICE_NAME "System"
  37. u32 acpi_irq_handled;
  38. u32 acpi_irq_not_handled;
  39. /*
  40. * Make ACPICA version work as module param
  41. */
  42. static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
  43. {
  44. int result;
  45. result = sprintf(buffer, "%x", ACPI_CA_VERSION);
  46. return result;
  47. }
  48. module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
  49. /* --------------------------------------------------------------------------
  50. FS Interface (/sys)
  51. -------------------------------------------------------------------------- */
  52. static LIST_HEAD(acpi_table_attr_list);
  53. static struct kobject *tables_kobj;
  54. static struct kobject *dynamic_tables_kobj;
  55. struct acpi_table_attr {
  56. struct bin_attribute attr;
  57. char name[8];
  58. int instance;
  59. struct list_head node;
  60. };
  61. static ssize_t acpi_table_show(struct kobject *kobj,
  62. struct bin_attribute *bin_attr, char *buf,
  63. loff_t offset, size_t count)
  64. {
  65. struct acpi_table_attr *table_attr =
  66. container_of(bin_attr, struct acpi_table_attr, attr);
  67. struct acpi_table_header *table_header = NULL;
  68. acpi_status status;
  69. char name[ACPI_NAME_SIZE];
  70. if (strncmp(table_attr->name, "NULL", 4))
  71. memcpy(name, table_attr->name, ACPI_NAME_SIZE);
  72. else
  73. memcpy(name, "\0\0\0\0", 4);
  74. status =
  75. acpi_get_table(name, table_attr->instance,
  76. &table_header);
  77. if (ACPI_FAILURE(status))
  78. return -ENODEV;
  79. return memory_read_from_buffer(buf, count, &offset,
  80. table_header, table_header->length);
  81. }
  82. static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
  83. struct acpi_table_header *table_header)
  84. {
  85. struct acpi_table_header *header = NULL;
  86. struct acpi_table_attr *attr = NULL;
  87. sysfs_attr_init(&table_attr->attr.attr);
  88. if (table_header->signature[0] != '\0')
  89. memcpy(table_attr->name, table_header->signature,
  90. ACPI_NAME_SIZE);
  91. else
  92. memcpy(table_attr->name, "NULL", 4);
  93. list_for_each_entry(attr, &acpi_table_attr_list, node) {
  94. if (!memcmp(table_attr->name, attr->name, ACPI_NAME_SIZE))
  95. if (table_attr->instance < attr->instance)
  96. table_attr->instance = attr->instance;
  97. }
  98. table_attr->instance++;
  99. if (table_attr->instance > 1 || (table_attr->instance == 1 &&
  100. !acpi_get_table
  101. (table_header->signature, 2, &header)))
  102. sprintf(table_attr->name + ACPI_NAME_SIZE, "%d",
  103. table_attr->instance);
  104. table_attr->attr.size = 0;
  105. table_attr->attr.read = acpi_table_show;
  106. table_attr->attr.attr.name = table_attr->name;
  107. table_attr->attr.attr.mode = 0400;
  108. return;
  109. }
  110. static acpi_status
  111. acpi_sysfs_table_handler(u32 event, void *table, void *context)
  112. {
  113. struct acpi_table_attr *table_attr;
  114. switch (event) {
  115. case ACPI_TABLE_EVENT_LOAD:
  116. table_attr =
  117. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  118. if (!table_attr)
  119. return AE_NO_MEMORY;
  120. acpi_table_attr_init(table_attr, table);
  121. if (sysfs_create_bin_file(dynamic_tables_kobj,
  122. &table_attr->attr)) {
  123. kfree(table_attr);
  124. return AE_ERROR;
  125. } else
  126. list_add_tail(&table_attr->node,
  127. &acpi_table_attr_list);
  128. break;
  129. case ACPI_TABLE_EVENT_UNLOAD:
  130. /*
  131. * we do not need to do anything right now
  132. * because the table is not deleted from the
  133. * global table list when unloading it.
  134. */
  135. break;
  136. default:
  137. return AE_BAD_PARAMETER;
  138. }
  139. return AE_OK;
  140. }
  141. static int acpi_system_sysfs_init(void)
  142. {
  143. struct acpi_table_attr *table_attr;
  144. struct acpi_table_header *table_header = NULL;
  145. int table_index = 0;
  146. int result;
  147. tables_kobj = kobject_create_and_add("tables", acpi_kobj);
  148. if (!tables_kobj)
  149. goto err;
  150. dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
  151. if (!dynamic_tables_kobj)
  152. goto err_dynamic_tables;
  153. do {
  154. result = acpi_get_table_by_index(table_index, &table_header);
  155. if (!result) {
  156. table_index++;
  157. table_attr = NULL;
  158. table_attr =
  159. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  160. if (!table_attr)
  161. return -ENOMEM;
  162. acpi_table_attr_init(table_attr, table_header);
  163. result =
  164. sysfs_create_bin_file(tables_kobj,
  165. &table_attr->attr);
  166. if (result) {
  167. kfree(table_attr);
  168. return result;
  169. } else
  170. list_add_tail(&table_attr->node,
  171. &acpi_table_attr_list);
  172. }
  173. } while (!result);
  174. kobject_uevent(tables_kobj, KOBJ_ADD);
  175. kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
  176. result = acpi_install_table_handler(acpi_sysfs_table_handler, NULL);
  177. return result == AE_OK ? 0 : -EINVAL;
  178. err_dynamic_tables:
  179. kobject_put(tables_kobj);
  180. err:
  181. return -ENOMEM;
  182. }
  183. /*
  184. * Detailed ACPI IRQ counters in /sys/firmware/acpi/interrupts/
  185. * See Documentation/ABI/testing/sysfs-firmware-acpi
  186. */
  187. #define COUNT_GPE 0
  188. #define COUNT_SCI 1 /* acpi_irq_handled */
  189. #define COUNT_SCI_NOT 2 /* acpi_irq_not_handled */
  190. #define COUNT_ERROR 3 /* other */
  191. #define NUM_COUNTERS_EXTRA 4
  192. struct event_counter {
  193. u32 count;
  194. u32 flags;
  195. };
  196. static struct event_counter *all_counters;
  197. static u32 num_gpes;
  198. static u32 num_counters;
  199. static struct attribute **all_attrs;
  200. static u32 acpi_gpe_count;
  201. static struct attribute_group interrupt_stats_attr_group = {
  202. .name = "interrupts",
  203. };
  204. static struct kobj_attribute *counter_attrs;
  205. static void delete_gpe_attr_array(void)
  206. {
  207. struct event_counter *tmp = all_counters;
  208. all_counters = NULL;
  209. kfree(tmp);
  210. if (counter_attrs) {
  211. int i;
  212. for (i = 0; i < num_gpes; i++)
  213. kfree(counter_attrs[i].attr.name);
  214. kfree(counter_attrs);
  215. }
  216. kfree(all_attrs);
  217. return;
  218. }
  219. void acpi_os_gpe_count(u32 gpe_number)
  220. {
  221. acpi_gpe_count++;
  222. if (!all_counters)
  223. return;
  224. if (gpe_number < num_gpes)
  225. all_counters[gpe_number].count++;
  226. else
  227. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
  228. count++;
  229. return;
  230. }
  231. void acpi_os_fixed_event_count(u32 event_number)
  232. {
  233. if (!all_counters)
  234. return;
  235. if (event_number < ACPI_NUM_FIXED_EVENTS)
  236. all_counters[num_gpes + event_number].count++;
  237. else
  238. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
  239. count++;
  240. return;
  241. }
  242. static int get_status(u32 index, acpi_event_status *status, acpi_handle *handle)
  243. {
  244. int result = 0;
  245. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  246. goto end;
  247. if (index < num_gpes) {
  248. result = acpi_get_gpe_device(index, handle);
  249. if (result) {
  250. ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
  251. "Invalid GPE 0x%x\n", index));
  252. goto end;
  253. }
  254. result = acpi_get_gpe_status(*handle, index,
  255. ACPI_NOT_ISR, status);
  256. } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
  257. result = acpi_get_event_status(index - num_gpes, status);
  258. end:
  259. return result;
  260. }
  261. static ssize_t counter_show(struct kobject *kobj,
  262. struct kobj_attribute *attr, char *buf)
  263. {
  264. int index = attr - counter_attrs;
  265. int size;
  266. acpi_handle handle;
  267. acpi_event_status status;
  268. int result = 0;
  269. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
  270. acpi_irq_handled;
  271. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count =
  272. acpi_irq_not_handled;
  273. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
  274. acpi_gpe_count;
  275. size = sprintf(buf, "%8d", all_counters[index].count);
  276. /* "gpe_all" or "sci" */
  277. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  278. goto end;
  279. result = get_status(index, &status, &handle);
  280. if (result)
  281. goto end;
  282. if (!(status & ACPI_EVENT_FLAG_HANDLE))
  283. size += sprintf(buf + size, " invalid");
  284. else if (status & ACPI_EVENT_FLAG_ENABLED)
  285. size += sprintf(buf + size, " enabled");
  286. else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
  287. size += sprintf(buf + size, " wake_enabled");
  288. else
  289. size += sprintf(buf + size, " disabled");
  290. end:
  291. size += sprintf(buf + size, "\n");
  292. return result ? result : size;
  293. }
  294. /*
  295. * counter_set() sets the specified counter.
  296. * setting the total "sci" file to any value clears all counters.
  297. * enable/disable/clear a gpe/fixed event in user space.
  298. */
  299. static ssize_t counter_set(struct kobject *kobj,
  300. struct kobj_attribute *attr, const char *buf, size_t size)
  301. {
  302. int index = attr - counter_attrs;
  303. acpi_event_status status;
  304. acpi_handle handle;
  305. int result = 0;
  306. if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
  307. int i;
  308. for (i = 0; i < num_counters; ++i)
  309. all_counters[i].count = 0;
  310. acpi_gpe_count = 0;
  311. acpi_irq_handled = 0;
  312. acpi_irq_not_handled = 0;
  313. goto end;
  314. }
  315. /* show the event status for both GPEs and Fixed Events */
  316. result = get_status(index, &status, &handle);
  317. if (result)
  318. goto end;
  319. if (!(status & ACPI_EVENT_FLAG_HANDLE)) {
  320. printk(KERN_WARNING PREFIX
  321. "Can not change Invalid GPE/Fixed Event status\n");
  322. return -EINVAL;
  323. }
  324. if (index < num_gpes) {
  325. if (!strcmp(buf, "disable\n") &&
  326. (status & ACPI_EVENT_FLAG_ENABLED))
  327. result = acpi_set_gpe(handle, index, ACPI_GPE_DISABLE);
  328. else if (!strcmp(buf, "enable\n") &&
  329. !(status & ACPI_EVENT_FLAG_ENABLED))
  330. result = acpi_set_gpe(handle, index, ACPI_GPE_ENABLE);
  331. else if (!strcmp(buf, "clear\n") &&
  332. (status & ACPI_EVENT_FLAG_SET))
  333. result = acpi_clear_gpe(handle, index, ACPI_NOT_ISR);
  334. else
  335. all_counters[index].count = strtoul(buf, NULL, 0);
  336. } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
  337. int event = index - num_gpes;
  338. if (!strcmp(buf, "disable\n") &&
  339. (status & ACPI_EVENT_FLAG_ENABLED))
  340. result = acpi_disable_event(event, ACPI_NOT_ISR);
  341. else if (!strcmp(buf, "enable\n") &&
  342. !(status & ACPI_EVENT_FLAG_ENABLED))
  343. result = acpi_enable_event(event, ACPI_NOT_ISR);
  344. else if (!strcmp(buf, "clear\n") &&
  345. (status & ACPI_EVENT_FLAG_SET))
  346. result = acpi_clear_event(event);
  347. else
  348. all_counters[index].count = strtoul(buf, NULL, 0);
  349. } else
  350. all_counters[index].count = strtoul(buf, NULL, 0);
  351. if (ACPI_FAILURE(result))
  352. result = -EINVAL;
  353. end:
  354. return result ? result : size;
  355. }
  356. void acpi_irq_stats_init(void)
  357. {
  358. int i;
  359. if (all_counters)
  360. return;
  361. num_gpes = acpi_current_gpe_count;
  362. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  363. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  364. GFP_KERNEL);
  365. if (all_attrs == NULL)
  366. return;
  367. all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
  368. GFP_KERNEL);
  369. if (all_counters == NULL)
  370. goto fail;
  371. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  372. GFP_KERNEL);
  373. if (counter_attrs == NULL)
  374. goto fail;
  375. for (i = 0; i < num_counters; ++i) {
  376. char buffer[12];
  377. char *name;
  378. if (i < num_gpes)
  379. sprintf(buffer, "gpe%02X", i);
  380. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  381. sprintf(buffer, "ff_pmtimer");
  382. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  383. sprintf(buffer, "ff_gbl_lock");
  384. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  385. sprintf(buffer, "ff_pwr_btn");
  386. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  387. sprintf(buffer, "ff_slp_btn");
  388. else if (i == num_gpes + ACPI_EVENT_RTC)
  389. sprintf(buffer, "ff_rt_clk");
  390. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  391. sprintf(buffer, "gpe_all");
  392. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  393. sprintf(buffer, "sci");
  394. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
  395. sprintf(buffer, "sci_not");
  396. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  397. sprintf(buffer, "error");
  398. else
  399. sprintf(buffer, "bug%02X", i);
  400. name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
  401. if (name == NULL)
  402. goto fail;
  403. strncpy(name, buffer, strlen(buffer) + 1);
  404. sysfs_attr_init(&counter_attrs[i].attr);
  405. counter_attrs[i].attr.name = name;
  406. counter_attrs[i].attr.mode = 0644;
  407. counter_attrs[i].show = counter_show;
  408. counter_attrs[i].store = counter_set;
  409. all_attrs[i] = &counter_attrs[i].attr;
  410. }
  411. interrupt_stats_attr_group.attrs = all_attrs;
  412. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  413. return;
  414. fail:
  415. delete_gpe_attr_array();
  416. return;
  417. }
  418. static void __exit interrupt_stats_exit(void)
  419. {
  420. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  421. delete_gpe_attr_array();
  422. return;
  423. }
  424. /* --------------------------------------------------------------------------
  425. FS Interface (/proc)
  426. -------------------------------------------------------------------------- */
  427. #ifdef CONFIG_ACPI_PROCFS
  428. #define ACPI_SYSTEM_FILE_INFO "info"
  429. #define ACPI_SYSTEM_FILE_EVENT "event"
  430. #define ACPI_SYSTEM_FILE_DSDT "dsdt"
  431. #define ACPI_SYSTEM_FILE_FADT "fadt"
  432. static int acpi_system_read_info(struct seq_file *seq, void *offset)
  433. {
  434. seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
  435. return 0;
  436. }
  437. static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
  438. {
  439. return single_open(file, acpi_system_read_info, PDE(inode)->data);
  440. }
  441. static const struct file_operations acpi_system_info_ops = {
  442. .owner = THIS_MODULE,
  443. .open = acpi_system_info_open_fs,
  444. .read = seq_read,
  445. .llseek = seq_lseek,
  446. .release = single_release,
  447. };
  448. static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
  449. loff_t *);
  450. static const struct file_operations acpi_system_dsdt_ops = {
  451. .owner = THIS_MODULE,
  452. .read = acpi_system_read_dsdt,
  453. };
  454. static ssize_t
  455. acpi_system_read_dsdt(struct file *file,
  456. char __user * buffer, size_t count, loff_t * ppos)
  457. {
  458. acpi_status status = AE_OK;
  459. struct acpi_table_header *dsdt = NULL;
  460. ssize_t res;
  461. status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
  462. if (ACPI_FAILURE(status))
  463. return -ENODEV;
  464. res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
  465. return res;
  466. }
  467. static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
  468. loff_t *);
  469. static const struct file_operations acpi_system_fadt_ops = {
  470. .owner = THIS_MODULE,
  471. .read = acpi_system_read_fadt,
  472. };
  473. static ssize_t
  474. acpi_system_read_fadt(struct file *file,
  475. char __user * buffer, size_t count, loff_t * ppos)
  476. {
  477. acpi_status status = AE_OK;
  478. struct acpi_table_header *fadt = NULL;
  479. ssize_t res;
  480. status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
  481. if (ACPI_FAILURE(status))
  482. return -ENODEV;
  483. res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
  484. return res;
  485. }
  486. static int acpi_system_procfs_init(void)
  487. {
  488. struct proc_dir_entry *entry;
  489. int error = 0;
  490. /* 'info' [R] */
  491. entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
  492. &acpi_system_info_ops);
  493. if (!entry)
  494. goto Error;
  495. /* 'dsdt' [R] */
  496. entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
  497. &acpi_system_dsdt_ops);
  498. if (!entry)
  499. goto Error;
  500. /* 'fadt' [R] */
  501. entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
  502. &acpi_system_fadt_ops);
  503. if (!entry)
  504. goto Error;
  505. Done:
  506. return error;
  507. Error:
  508. remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
  509. remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
  510. remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
  511. error = -EFAULT;
  512. goto Done;
  513. }
  514. #else
  515. static int acpi_system_procfs_init(void)
  516. {
  517. return 0;
  518. }
  519. #endif
  520. int __init acpi_system_init(void)
  521. {
  522. int result;
  523. result = acpi_system_procfs_init();
  524. if (result)
  525. return result;
  526. result = acpi_system_sysfs_init();
  527. return result;
  528. }