system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 file *filp, 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, status);
  255. } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
  256. result = acpi_get_event_status(index - num_gpes, status);
  257. end:
  258. return result;
  259. }
  260. static ssize_t counter_show(struct kobject *kobj,
  261. struct kobj_attribute *attr, char *buf)
  262. {
  263. int index = attr - counter_attrs;
  264. int size;
  265. acpi_handle handle;
  266. acpi_event_status status;
  267. int result = 0;
  268. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
  269. acpi_irq_handled;
  270. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count =
  271. acpi_irq_not_handled;
  272. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
  273. acpi_gpe_count;
  274. size = sprintf(buf, "%8d", all_counters[index].count);
  275. /* "gpe_all" or "sci" */
  276. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  277. goto end;
  278. result = get_status(index, &status, &handle);
  279. if (result)
  280. goto end;
  281. if (!(status & ACPI_EVENT_FLAG_HANDLE))
  282. size += sprintf(buf + size, " invalid");
  283. else if (status & ACPI_EVENT_FLAG_ENABLED)
  284. size += sprintf(buf + size, " enabled");
  285. else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
  286. size += sprintf(buf + size, " wake_enabled");
  287. else
  288. size += sprintf(buf + size, " disabled");
  289. end:
  290. size += sprintf(buf + size, "\n");
  291. return result ? result : size;
  292. }
  293. /*
  294. * counter_set() sets the specified counter.
  295. * setting the total "sci" file to any value clears all counters.
  296. * enable/disable/clear a gpe/fixed event in user space.
  297. */
  298. static ssize_t counter_set(struct kobject *kobj,
  299. struct kobj_attribute *attr, const char *buf, size_t size)
  300. {
  301. int index = attr - counter_attrs;
  302. acpi_event_status status;
  303. acpi_handle handle;
  304. int result = 0;
  305. if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
  306. int i;
  307. for (i = 0; i < num_counters; ++i)
  308. all_counters[i].count = 0;
  309. acpi_gpe_count = 0;
  310. acpi_irq_handled = 0;
  311. acpi_irq_not_handled = 0;
  312. goto end;
  313. }
  314. /* show the event status for both GPEs and Fixed Events */
  315. result = get_status(index, &status, &handle);
  316. if (result)
  317. goto end;
  318. if (!(status & ACPI_EVENT_FLAG_HANDLE)) {
  319. printk(KERN_WARNING PREFIX
  320. "Can not change Invalid GPE/Fixed Event status\n");
  321. return -EINVAL;
  322. }
  323. if (index < num_gpes) {
  324. if (!strcmp(buf, "disable\n") &&
  325. (status & ACPI_EVENT_FLAG_ENABLED))
  326. result = acpi_disable_gpe(handle, index,
  327. ACPI_GPE_TYPE_RUNTIME);
  328. else if (!strcmp(buf, "enable\n") &&
  329. !(status & ACPI_EVENT_FLAG_ENABLED))
  330. result = acpi_enable_gpe(handle, index,
  331. ACPI_GPE_TYPE_RUNTIME);
  332. else if (!strcmp(buf, "clear\n") &&
  333. (status & ACPI_EVENT_FLAG_SET))
  334. result = acpi_clear_gpe(handle, index);
  335. else
  336. all_counters[index].count = strtoul(buf, NULL, 0);
  337. } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
  338. int event = index - num_gpes;
  339. if (!strcmp(buf, "disable\n") &&
  340. (status & ACPI_EVENT_FLAG_ENABLED))
  341. result = acpi_disable_event(event, ACPI_NOT_ISR);
  342. else if (!strcmp(buf, "enable\n") &&
  343. !(status & ACPI_EVENT_FLAG_ENABLED))
  344. result = acpi_enable_event(event, ACPI_NOT_ISR);
  345. else if (!strcmp(buf, "clear\n") &&
  346. (status & ACPI_EVENT_FLAG_SET))
  347. result = acpi_clear_event(event);
  348. else
  349. all_counters[index].count = strtoul(buf, NULL, 0);
  350. } else
  351. all_counters[index].count = strtoul(buf, NULL, 0);
  352. if (ACPI_FAILURE(result))
  353. result = -EINVAL;
  354. end:
  355. return result ? result : size;
  356. }
  357. void acpi_irq_stats_init(void)
  358. {
  359. int i;
  360. if (all_counters)
  361. return;
  362. num_gpes = acpi_current_gpe_count;
  363. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  364. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  365. GFP_KERNEL);
  366. if (all_attrs == NULL)
  367. return;
  368. all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
  369. GFP_KERNEL);
  370. if (all_counters == NULL)
  371. goto fail;
  372. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  373. GFP_KERNEL);
  374. if (counter_attrs == NULL)
  375. goto fail;
  376. for (i = 0; i < num_counters; ++i) {
  377. char buffer[12];
  378. char *name;
  379. if (i < num_gpes)
  380. sprintf(buffer, "gpe%02X", i);
  381. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  382. sprintf(buffer, "ff_pmtimer");
  383. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  384. sprintf(buffer, "ff_gbl_lock");
  385. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  386. sprintf(buffer, "ff_pwr_btn");
  387. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  388. sprintf(buffer, "ff_slp_btn");
  389. else if (i == num_gpes + ACPI_EVENT_RTC)
  390. sprintf(buffer, "ff_rt_clk");
  391. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  392. sprintf(buffer, "gpe_all");
  393. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  394. sprintf(buffer, "sci");
  395. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
  396. sprintf(buffer, "sci_not");
  397. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  398. sprintf(buffer, "error");
  399. else
  400. sprintf(buffer, "bug%02X", i);
  401. name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
  402. if (name == NULL)
  403. goto fail;
  404. strncpy(name, buffer, strlen(buffer) + 1);
  405. sysfs_attr_init(&counter_attrs[i].attr);
  406. counter_attrs[i].attr.name = name;
  407. counter_attrs[i].attr.mode = 0644;
  408. counter_attrs[i].show = counter_show;
  409. counter_attrs[i].store = counter_set;
  410. all_attrs[i] = &counter_attrs[i].attr;
  411. }
  412. interrupt_stats_attr_group.attrs = all_attrs;
  413. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  414. return;
  415. fail:
  416. delete_gpe_attr_array();
  417. return;
  418. }
  419. static void __exit interrupt_stats_exit(void)
  420. {
  421. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  422. delete_gpe_attr_array();
  423. return;
  424. }
  425. /* --------------------------------------------------------------------------
  426. FS Interface (/proc)
  427. -------------------------------------------------------------------------- */
  428. #ifdef CONFIG_ACPI_PROCFS
  429. #define ACPI_SYSTEM_FILE_INFO "info"
  430. #define ACPI_SYSTEM_FILE_EVENT "event"
  431. #define ACPI_SYSTEM_FILE_DSDT "dsdt"
  432. #define ACPI_SYSTEM_FILE_FADT "fadt"
  433. static int acpi_system_read_info(struct seq_file *seq, void *offset)
  434. {
  435. seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
  436. return 0;
  437. }
  438. static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
  439. {
  440. return single_open(file, acpi_system_read_info, PDE(inode)->data);
  441. }
  442. static const struct file_operations acpi_system_info_ops = {
  443. .owner = THIS_MODULE,
  444. .open = acpi_system_info_open_fs,
  445. .read = seq_read,
  446. .llseek = seq_lseek,
  447. .release = single_release,
  448. };
  449. static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
  450. loff_t *);
  451. static const struct file_operations acpi_system_dsdt_ops = {
  452. .owner = THIS_MODULE,
  453. .read = acpi_system_read_dsdt,
  454. };
  455. static ssize_t
  456. acpi_system_read_dsdt(struct file *file,
  457. char __user * buffer, size_t count, loff_t * ppos)
  458. {
  459. acpi_status status = AE_OK;
  460. struct acpi_table_header *dsdt = NULL;
  461. ssize_t res;
  462. status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
  463. if (ACPI_FAILURE(status))
  464. return -ENODEV;
  465. res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
  466. return res;
  467. }
  468. static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
  469. loff_t *);
  470. static const struct file_operations acpi_system_fadt_ops = {
  471. .owner = THIS_MODULE,
  472. .read = acpi_system_read_fadt,
  473. };
  474. static ssize_t
  475. acpi_system_read_fadt(struct file *file,
  476. char __user * buffer, size_t count, loff_t * ppos)
  477. {
  478. acpi_status status = AE_OK;
  479. struct acpi_table_header *fadt = NULL;
  480. ssize_t res;
  481. status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
  482. if (ACPI_FAILURE(status))
  483. return -ENODEV;
  484. res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
  485. return res;
  486. }
  487. static int acpi_system_procfs_init(void)
  488. {
  489. struct proc_dir_entry *entry;
  490. int error = 0;
  491. /* 'info' [R] */
  492. entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
  493. &acpi_system_info_ops);
  494. if (!entry)
  495. goto Error;
  496. /* 'dsdt' [R] */
  497. entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
  498. &acpi_system_dsdt_ops);
  499. if (!entry)
  500. goto Error;
  501. /* 'fadt' [R] */
  502. entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
  503. &acpi_system_fadt_ops);
  504. if (!entry)
  505. goto Error;
  506. Done:
  507. return error;
  508. Error:
  509. remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
  510. remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
  511. remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
  512. error = -EFAULT;
  513. goto Done;
  514. }
  515. #else
  516. static int acpi_system_procfs_init(void)
  517. {
  518. return 0;
  519. }
  520. #endif
  521. int __init acpi_system_init(void)
  522. {
  523. int result;
  524. result = acpi_system_procfs_init();
  525. if (result)
  526. return result;
  527. result = acpi_system_sysfs_init();
  528. return result;
  529. }