system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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/init.h>
  28. #include <linux/string.h>
  29. #include <asm/uaccess.h>
  30. #include <acpi/acpi_drivers.h>
  31. #define PREFIX "ACPI: "
  32. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  33. ACPI_MODULE_NAME("system");
  34. #define ACPI_SYSTEM_CLASS "system"
  35. #define ACPI_SYSTEM_DEVICE_NAME "System"
  36. u32 acpi_irq_handled;
  37. u32 acpi_irq_not_handled;
  38. /*
  39. * Make ACPICA version work as module param
  40. */
  41. static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
  42. {
  43. int result;
  44. result = sprintf(buffer, "%x", ACPI_CA_VERSION);
  45. return result;
  46. }
  47. module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
  48. /* --------------------------------------------------------------------------
  49. FS Interface (/sys)
  50. -------------------------------------------------------------------------- */
  51. static LIST_HEAD(acpi_table_attr_list);
  52. static struct kobject *tables_kobj;
  53. static struct kobject *dynamic_tables_kobj;
  54. struct acpi_table_attr {
  55. struct bin_attribute attr;
  56. char name[8];
  57. int instance;
  58. struct list_head node;
  59. };
  60. static ssize_t acpi_table_show(struct kobject *kobj,
  61. struct bin_attribute *bin_attr, char *buf,
  62. loff_t offset, size_t count)
  63. {
  64. struct acpi_table_attr *table_attr =
  65. container_of(bin_attr, struct acpi_table_attr, attr);
  66. struct acpi_table_header *table_header = NULL;
  67. acpi_status status;
  68. char name[ACPI_NAME_SIZE];
  69. if (strncmp(table_attr->name, "NULL", 4))
  70. memcpy(name, table_attr->name, ACPI_NAME_SIZE);
  71. else
  72. memcpy(name, "\0\0\0\0", 4);
  73. status =
  74. acpi_get_table(name, table_attr->instance,
  75. &table_header);
  76. if (ACPI_FAILURE(status))
  77. return -ENODEV;
  78. return memory_read_from_buffer(buf, count, &offset,
  79. table_header, table_header->length);
  80. }
  81. static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
  82. struct acpi_table_header *table_header)
  83. {
  84. struct acpi_table_header *header = NULL;
  85. struct acpi_table_attr *attr = NULL;
  86. sysfs_attr_init(&table_attr->attr.attr);
  87. if (table_header->signature[0] != '\0')
  88. memcpy(table_attr->name, table_header->signature,
  89. ACPI_NAME_SIZE);
  90. else
  91. memcpy(table_attr->name, "NULL", 4);
  92. list_for_each_entry(attr, &acpi_table_attr_list, node) {
  93. if (!memcmp(table_attr->name, attr->name, ACPI_NAME_SIZE))
  94. if (table_attr->instance < attr->instance)
  95. table_attr->instance = attr->instance;
  96. }
  97. table_attr->instance++;
  98. if (table_attr->instance > 1 || (table_attr->instance == 1 &&
  99. !acpi_get_table
  100. (table_header->signature, 2, &header)))
  101. sprintf(table_attr->name + ACPI_NAME_SIZE, "%d",
  102. table_attr->instance);
  103. table_attr->attr.size = 0;
  104. table_attr->attr.read = acpi_table_show;
  105. table_attr->attr.attr.name = table_attr->name;
  106. table_attr->attr.attr.mode = 0400;
  107. return;
  108. }
  109. static acpi_status
  110. acpi_sysfs_table_handler(u32 event, void *table, void *context)
  111. {
  112. struct acpi_table_attr *table_attr;
  113. switch (event) {
  114. case ACPI_TABLE_EVENT_LOAD:
  115. table_attr =
  116. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  117. if (!table_attr)
  118. return AE_NO_MEMORY;
  119. acpi_table_attr_init(table_attr, table);
  120. if (sysfs_create_bin_file(dynamic_tables_kobj,
  121. &table_attr->attr)) {
  122. kfree(table_attr);
  123. return AE_ERROR;
  124. } else
  125. list_add_tail(&table_attr->node,
  126. &acpi_table_attr_list);
  127. break;
  128. case ACPI_TABLE_EVENT_UNLOAD:
  129. /*
  130. * we do not need to do anything right now
  131. * because the table is not deleted from the
  132. * global table list when unloading it.
  133. */
  134. break;
  135. default:
  136. return AE_BAD_PARAMETER;
  137. }
  138. return AE_OK;
  139. }
  140. static int acpi_system_sysfs_init(void)
  141. {
  142. struct acpi_table_attr *table_attr;
  143. struct acpi_table_header *table_header = NULL;
  144. int table_index = 0;
  145. int result;
  146. tables_kobj = kobject_create_and_add("tables", acpi_kobj);
  147. if (!tables_kobj)
  148. goto err;
  149. dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
  150. if (!dynamic_tables_kobj)
  151. goto err_dynamic_tables;
  152. do {
  153. result = acpi_get_table_by_index(table_index, &table_header);
  154. if (!result) {
  155. table_index++;
  156. table_attr = NULL;
  157. table_attr =
  158. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  159. if (!table_attr)
  160. return -ENOMEM;
  161. acpi_table_attr_init(table_attr, table_header);
  162. result =
  163. sysfs_create_bin_file(tables_kobj,
  164. &table_attr->attr);
  165. if (result) {
  166. kfree(table_attr);
  167. return result;
  168. } else
  169. list_add_tail(&table_attr->node,
  170. &acpi_table_attr_list);
  171. }
  172. } while (!result);
  173. kobject_uevent(tables_kobj, KOBJ_ADD);
  174. kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
  175. result = acpi_install_table_handler(acpi_sysfs_table_handler, NULL);
  176. return result == AE_OK ? 0 : -EINVAL;
  177. err_dynamic_tables:
  178. kobject_put(tables_kobj);
  179. err:
  180. return -ENOMEM;
  181. }
  182. /*
  183. * Detailed ACPI IRQ counters in /sys/firmware/acpi/interrupts/
  184. * See Documentation/ABI/testing/sysfs-firmware-acpi
  185. */
  186. #define COUNT_GPE 0
  187. #define COUNT_SCI 1 /* acpi_irq_handled */
  188. #define COUNT_SCI_NOT 2 /* acpi_irq_not_handled */
  189. #define COUNT_ERROR 3 /* other */
  190. #define NUM_COUNTERS_EXTRA 4
  191. struct event_counter {
  192. u32 count;
  193. u32 flags;
  194. };
  195. static struct event_counter *all_counters;
  196. static u32 num_gpes;
  197. static u32 num_counters;
  198. static struct attribute **all_attrs;
  199. static u32 acpi_gpe_count;
  200. static struct attribute_group interrupt_stats_attr_group = {
  201. .name = "interrupts",
  202. };
  203. static struct kobj_attribute *counter_attrs;
  204. static void delete_gpe_attr_array(void)
  205. {
  206. struct event_counter *tmp = all_counters;
  207. all_counters = NULL;
  208. kfree(tmp);
  209. if (counter_attrs) {
  210. int i;
  211. for (i = 0; i < num_gpes; i++)
  212. kfree(counter_attrs[i].attr.name);
  213. kfree(counter_attrs);
  214. }
  215. kfree(all_attrs);
  216. return;
  217. }
  218. void acpi_os_gpe_count(u32 gpe_number)
  219. {
  220. acpi_gpe_count++;
  221. if (!all_counters)
  222. return;
  223. if (gpe_number < num_gpes)
  224. all_counters[gpe_number].count++;
  225. else
  226. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
  227. count++;
  228. return;
  229. }
  230. void acpi_os_fixed_event_count(u32 event_number)
  231. {
  232. if (!all_counters)
  233. return;
  234. if (event_number < ACPI_NUM_FIXED_EVENTS)
  235. all_counters[num_gpes + event_number].count++;
  236. else
  237. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
  238. count++;
  239. return;
  240. }
  241. static int get_status(u32 index, acpi_event_status *status, acpi_handle *handle)
  242. {
  243. int result = 0;
  244. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  245. goto end;
  246. if (index < num_gpes) {
  247. result = acpi_get_gpe_device(index, handle);
  248. if (result) {
  249. ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
  250. "Invalid GPE 0x%x\n", index));
  251. goto end;
  252. }
  253. result = acpi_get_gpe_status(*handle, index,
  254. ACPI_NOT_ISR, 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_set_gpe(handle, index, ACPI_GPE_DISABLE);
  327. else if (!strcmp(buf, "enable\n") &&
  328. !(status & ACPI_EVENT_FLAG_ENABLED))
  329. result = acpi_set_gpe(handle, index, ACPI_GPE_ENABLE);
  330. else if (!strcmp(buf, "clear\n") &&
  331. (status & ACPI_EVENT_FLAG_SET))
  332. result = acpi_clear_gpe(handle, index, ACPI_NOT_ISR);
  333. else
  334. all_counters[index].count = strtoul(buf, NULL, 0);
  335. } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
  336. int event = index - num_gpes;
  337. if (!strcmp(buf, "disable\n") &&
  338. (status & ACPI_EVENT_FLAG_ENABLED))
  339. result = acpi_disable_event(event, ACPI_NOT_ISR);
  340. else if (!strcmp(buf, "enable\n") &&
  341. !(status & ACPI_EVENT_FLAG_ENABLED))
  342. result = acpi_enable_event(event, ACPI_NOT_ISR);
  343. else if (!strcmp(buf, "clear\n") &&
  344. (status & ACPI_EVENT_FLAG_SET))
  345. result = acpi_clear_event(event);
  346. else
  347. all_counters[index].count = strtoul(buf, NULL, 0);
  348. } else
  349. all_counters[index].count = strtoul(buf, NULL, 0);
  350. if (ACPI_FAILURE(result))
  351. result = -EINVAL;
  352. end:
  353. return result ? result : size;
  354. }
  355. void acpi_irq_stats_init(void)
  356. {
  357. int i;
  358. if (all_counters)
  359. return;
  360. num_gpes = acpi_current_gpe_count;
  361. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  362. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  363. GFP_KERNEL);
  364. if (all_attrs == NULL)
  365. return;
  366. all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
  367. GFP_KERNEL);
  368. if (all_counters == NULL)
  369. goto fail;
  370. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  371. GFP_KERNEL);
  372. if (counter_attrs == NULL)
  373. goto fail;
  374. for (i = 0; i < num_counters; ++i) {
  375. char buffer[12];
  376. char *name;
  377. if (i < num_gpes)
  378. sprintf(buffer, "gpe%02X", i);
  379. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  380. sprintf(buffer, "ff_pmtimer");
  381. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  382. sprintf(buffer, "ff_gbl_lock");
  383. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  384. sprintf(buffer, "ff_pwr_btn");
  385. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  386. sprintf(buffer, "ff_slp_btn");
  387. else if (i == num_gpes + ACPI_EVENT_RTC)
  388. sprintf(buffer, "ff_rt_clk");
  389. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  390. sprintf(buffer, "gpe_all");
  391. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  392. sprintf(buffer, "sci");
  393. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
  394. sprintf(buffer, "sci_not");
  395. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  396. sprintf(buffer, "error");
  397. else
  398. sprintf(buffer, "bug%02X", i);
  399. name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
  400. if (name == NULL)
  401. goto fail;
  402. strncpy(name, buffer, strlen(buffer) + 1);
  403. sysfs_attr_init(&counter_attrs[i].attr);
  404. counter_attrs[i].attr.name = name;
  405. counter_attrs[i].attr.mode = 0644;
  406. counter_attrs[i].show = counter_show;
  407. counter_attrs[i].store = counter_set;
  408. all_attrs[i] = &counter_attrs[i].attr;
  409. }
  410. interrupt_stats_attr_group.attrs = all_attrs;
  411. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  412. return;
  413. fail:
  414. delete_gpe_attr_array();
  415. return;
  416. }
  417. static void __exit interrupt_stats_exit(void)
  418. {
  419. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  420. delete_gpe_attr_array();
  421. return;
  422. }
  423. /* --------------------------------------------------------------------------
  424. FS Interface (/proc)
  425. -------------------------------------------------------------------------- */
  426. #ifdef CONFIG_ACPI_PROCFS
  427. #define ACPI_SYSTEM_FILE_INFO "info"
  428. #define ACPI_SYSTEM_FILE_EVENT "event"
  429. #define ACPI_SYSTEM_FILE_DSDT "dsdt"
  430. #define ACPI_SYSTEM_FILE_FADT "fadt"
  431. static int acpi_system_read_info(struct seq_file *seq, void *offset)
  432. {
  433. seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
  434. return 0;
  435. }
  436. static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
  437. {
  438. return single_open(file, acpi_system_read_info, PDE(inode)->data);
  439. }
  440. static const struct file_operations acpi_system_info_ops = {
  441. .owner = THIS_MODULE,
  442. .open = acpi_system_info_open_fs,
  443. .read = seq_read,
  444. .llseek = seq_lseek,
  445. .release = single_release,
  446. };
  447. static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
  448. loff_t *);
  449. static const struct file_operations acpi_system_dsdt_ops = {
  450. .owner = THIS_MODULE,
  451. .read = acpi_system_read_dsdt,
  452. };
  453. static ssize_t
  454. acpi_system_read_dsdt(struct file *file,
  455. char __user * buffer, size_t count, loff_t * ppos)
  456. {
  457. acpi_status status = AE_OK;
  458. struct acpi_table_header *dsdt = NULL;
  459. ssize_t res;
  460. status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
  461. if (ACPI_FAILURE(status))
  462. return -ENODEV;
  463. res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
  464. return res;
  465. }
  466. static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
  467. loff_t *);
  468. static const struct file_operations acpi_system_fadt_ops = {
  469. .owner = THIS_MODULE,
  470. .read = acpi_system_read_fadt,
  471. };
  472. static ssize_t
  473. acpi_system_read_fadt(struct file *file,
  474. char __user * buffer, size_t count, loff_t * ppos)
  475. {
  476. acpi_status status = AE_OK;
  477. struct acpi_table_header *fadt = NULL;
  478. ssize_t res;
  479. status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
  480. if (ACPI_FAILURE(status))
  481. return -ENODEV;
  482. res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
  483. return res;
  484. }
  485. static int acpi_system_procfs_init(void)
  486. {
  487. struct proc_dir_entry *entry;
  488. int error = 0;
  489. /* 'info' [R] */
  490. entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
  491. &acpi_system_info_ops);
  492. if (!entry)
  493. goto Error;
  494. /* 'dsdt' [R] */
  495. entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
  496. &acpi_system_dsdt_ops);
  497. if (!entry)
  498. goto Error;
  499. /* 'fadt' [R] */
  500. entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
  501. &acpi_system_fadt_ops);
  502. if (!entry)
  503. goto Error;
  504. Done:
  505. return error;
  506. Error:
  507. remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
  508. remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
  509. remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
  510. error = -EFAULT;
  511. goto Done;
  512. }
  513. #else
  514. static int acpi_system_procfs_init(void)
  515. {
  516. return 0;
  517. }
  518. #endif
  519. int __init acpi_system_init(void)
  520. {
  521. int result;
  522. result = acpi_system_procfs_init();
  523. if (result)
  524. return result;
  525. result = acpi_system_sysfs_init();
  526. return result;
  527. }