system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. if (table_header->signature[0] != '\0')
  87. memcpy(table_attr->name, table_header->signature,
  88. ACPI_NAME_SIZE);
  89. else
  90. memcpy(table_attr->name, "NULL", 4);
  91. list_for_each_entry(attr, &acpi_table_attr_list, node) {
  92. if (!memcmp(table_attr->name, attr->name, ACPI_NAME_SIZE))
  93. if (table_attr->instance < attr->instance)
  94. table_attr->instance = attr->instance;
  95. }
  96. table_attr->instance++;
  97. if (table_attr->instance > 1 || (table_attr->instance == 1 &&
  98. !acpi_get_table
  99. (table_header->signature, 2, &header)))
  100. sprintf(table_attr->name + ACPI_NAME_SIZE, "%d",
  101. table_attr->instance);
  102. table_attr->attr.size = 0;
  103. table_attr->attr.read = acpi_table_show;
  104. table_attr->attr.attr.name = table_attr->name;
  105. table_attr->attr.attr.mode = 0400;
  106. return;
  107. }
  108. static acpi_status
  109. acpi_sysfs_table_handler(u32 event, void *table, void *context)
  110. {
  111. struct acpi_table_attr *table_attr;
  112. switch (event) {
  113. case ACPI_TABLE_EVENT_LOAD:
  114. table_attr =
  115. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  116. if (!table_attr)
  117. return AE_NO_MEMORY;
  118. acpi_table_attr_init(table_attr, table);
  119. if (sysfs_create_bin_file(dynamic_tables_kobj,
  120. &table_attr->attr)) {
  121. kfree(table_attr);
  122. return AE_ERROR;
  123. } else
  124. list_add_tail(&table_attr->node,
  125. &acpi_table_attr_list);
  126. break;
  127. case ACPI_TABLE_EVENT_UNLOAD:
  128. /*
  129. * we do not need to do anything right now
  130. * because the table is not deleted from the
  131. * global table list when unloading it.
  132. */
  133. break;
  134. default:
  135. return AE_BAD_PARAMETER;
  136. }
  137. return AE_OK;
  138. }
  139. static int acpi_system_sysfs_init(void)
  140. {
  141. struct acpi_table_attr *table_attr;
  142. struct acpi_table_header *table_header = NULL;
  143. int table_index = 0;
  144. int result;
  145. tables_kobj = kobject_create_and_add("tables", acpi_kobj);
  146. if (!tables_kobj)
  147. goto err;
  148. dynamic_tables_kobj = kobject_create_and_add("dynamic", tables_kobj);
  149. if (!dynamic_tables_kobj)
  150. goto err_dynamic_tables;
  151. do {
  152. result = acpi_get_table_by_index(table_index, &table_header);
  153. if (!result) {
  154. table_index++;
  155. table_attr = NULL;
  156. table_attr =
  157. kzalloc(sizeof(struct acpi_table_attr), GFP_KERNEL);
  158. if (!table_attr)
  159. return -ENOMEM;
  160. acpi_table_attr_init(table_attr, table_header);
  161. result =
  162. sysfs_create_bin_file(tables_kobj,
  163. &table_attr->attr);
  164. if (result) {
  165. kfree(table_attr);
  166. return result;
  167. } else
  168. list_add_tail(&table_attr->node,
  169. &acpi_table_attr_list);
  170. }
  171. } while (!result);
  172. kobject_uevent(tables_kobj, KOBJ_ADD);
  173. kobject_uevent(dynamic_tables_kobj, KOBJ_ADD);
  174. result = acpi_install_table_handler(acpi_sysfs_table_handler, NULL);
  175. return result == AE_OK ? 0 : -EINVAL;
  176. err_dynamic_tables:
  177. kobject_put(tables_kobj);
  178. err:
  179. return -ENOMEM;
  180. }
  181. /*
  182. * Detailed ACPI IRQ counters in /sys/firmware/acpi/interrupts/
  183. * See Documentation/ABI/testing/sysfs-firmware-acpi
  184. */
  185. #define COUNT_GPE 0
  186. #define COUNT_SCI 1 /* acpi_irq_handled */
  187. #define COUNT_SCI_NOT 2 /* acpi_irq_not_handled */
  188. #define COUNT_ERROR 3 /* other */
  189. #define NUM_COUNTERS_EXTRA 4
  190. struct event_counter {
  191. u32 count;
  192. u32 flags;
  193. };
  194. static struct event_counter *all_counters;
  195. static u32 num_gpes;
  196. static u32 num_counters;
  197. static struct attribute **all_attrs;
  198. static u32 acpi_gpe_count;
  199. static struct attribute_group interrupt_stats_attr_group = {
  200. .name = "interrupts",
  201. };
  202. static struct kobj_attribute *counter_attrs;
  203. static void delete_gpe_attr_array(void)
  204. {
  205. struct event_counter *tmp = all_counters;
  206. all_counters = NULL;
  207. kfree(tmp);
  208. if (counter_attrs) {
  209. int i;
  210. for (i = 0; i < num_gpes; i++)
  211. kfree(counter_attrs[i].attr.name);
  212. kfree(counter_attrs);
  213. }
  214. kfree(all_attrs);
  215. return;
  216. }
  217. void acpi_os_gpe_count(u32 gpe_number)
  218. {
  219. acpi_gpe_count++;
  220. if (!all_counters)
  221. return;
  222. if (gpe_number < num_gpes)
  223. all_counters[gpe_number].count++;
  224. else
  225. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
  226. count++;
  227. return;
  228. }
  229. void acpi_os_fixed_event_count(u32 event_number)
  230. {
  231. if (!all_counters)
  232. return;
  233. if (event_number < ACPI_NUM_FIXED_EVENTS)
  234. all_counters[num_gpes + event_number].count++;
  235. else
  236. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR].
  237. count++;
  238. return;
  239. }
  240. static int get_status(u32 index, acpi_event_status *status, acpi_handle *handle)
  241. {
  242. int result = 0;
  243. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  244. goto end;
  245. if (index < num_gpes) {
  246. result = acpi_get_gpe_device(index, handle);
  247. if (result) {
  248. ACPI_EXCEPTION((AE_INFO, AE_NOT_FOUND,
  249. "Invalid GPE 0x%x\n", index));
  250. goto end;
  251. }
  252. result = acpi_get_gpe_status(*handle, index,
  253. ACPI_NOT_ISR, status);
  254. } else if (index < (num_gpes + ACPI_NUM_FIXED_EVENTS))
  255. result = acpi_get_event_status(index - num_gpes, status);
  256. end:
  257. return result;
  258. }
  259. static ssize_t counter_show(struct kobject *kobj,
  260. struct kobj_attribute *attr, char *buf)
  261. {
  262. int index = attr - counter_attrs;
  263. int size;
  264. acpi_handle handle;
  265. acpi_event_status status;
  266. int result = 0;
  267. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI].count =
  268. acpi_irq_handled;
  269. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT].count =
  270. acpi_irq_not_handled;
  271. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE].count =
  272. acpi_gpe_count;
  273. size = sprintf(buf, "%8d", all_counters[index].count);
  274. /* "gpe_all" or "sci" */
  275. if (index >= num_gpes + ACPI_NUM_FIXED_EVENTS)
  276. goto end;
  277. result = get_status(index, &status, &handle);
  278. if (result)
  279. goto end;
  280. if (!(status & ACPI_EVENT_FLAG_HANDLE))
  281. size += sprintf(buf + size, " invalid");
  282. else if (status & ACPI_EVENT_FLAG_ENABLED)
  283. size += sprintf(buf + size, " enabled");
  284. else if (status & ACPI_EVENT_FLAG_WAKE_ENABLED)
  285. size += sprintf(buf + size, " wake_enabled");
  286. else
  287. size += sprintf(buf + size, " disabled");
  288. end:
  289. size += sprintf(buf + size, "\n");
  290. return result ? result : size;
  291. }
  292. /*
  293. * counter_set() sets the specified counter.
  294. * setting the total "sci" file to any value clears all counters.
  295. * enable/disable/clear a gpe/fixed event in user space.
  296. */
  297. static ssize_t counter_set(struct kobject *kobj,
  298. struct kobj_attribute *attr, const char *buf, size_t size)
  299. {
  300. int index = attr - counter_attrs;
  301. acpi_event_status status;
  302. acpi_handle handle;
  303. int result = 0;
  304. if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
  305. int i;
  306. for (i = 0; i < num_counters; ++i)
  307. all_counters[i].count = 0;
  308. acpi_gpe_count = 0;
  309. acpi_irq_handled = 0;
  310. acpi_irq_not_handled = 0;
  311. goto end;
  312. }
  313. /* show the event status for both GPEs and Fixed Events */
  314. result = get_status(index, &status, &handle);
  315. if (result)
  316. goto end;
  317. if (!(status & ACPI_EVENT_FLAG_HANDLE)) {
  318. printk(KERN_WARNING PREFIX
  319. "Can not change Invalid GPE/Fixed Event status\n");
  320. return -EINVAL;
  321. }
  322. if (index < num_gpes) {
  323. if (!strcmp(buf, "disable\n") &&
  324. (status & ACPI_EVENT_FLAG_ENABLED))
  325. result = acpi_disable_gpe(handle, index);
  326. else if (!strcmp(buf, "enable\n") &&
  327. !(status & ACPI_EVENT_FLAG_ENABLED))
  328. result = acpi_enable_gpe(handle, index);
  329. else if (!strcmp(buf, "clear\n") &&
  330. (status & ACPI_EVENT_FLAG_SET))
  331. result = acpi_clear_gpe(handle, index, ACPI_NOT_ISR);
  332. else
  333. all_counters[index].count = strtoul(buf, NULL, 0);
  334. } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
  335. int event = index - num_gpes;
  336. if (!strcmp(buf, "disable\n") &&
  337. (status & ACPI_EVENT_FLAG_ENABLED))
  338. result = acpi_disable_event(event, ACPI_NOT_ISR);
  339. else if (!strcmp(buf, "enable\n") &&
  340. !(status & ACPI_EVENT_FLAG_ENABLED))
  341. result = acpi_enable_event(event, ACPI_NOT_ISR);
  342. else if (!strcmp(buf, "clear\n") &&
  343. (status & ACPI_EVENT_FLAG_SET))
  344. result = acpi_clear_event(event);
  345. else
  346. all_counters[index].count = strtoul(buf, NULL, 0);
  347. } else
  348. all_counters[index].count = strtoul(buf, NULL, 0);
  349. if (ACPI_FAILURE(result))
  350. result = -EINVAL;
  351. end:
  352. return result ? result : size;
  353. }
  354. void acpi_irq_stats_init(void)
  355. {
  356. int i;
  357. if (all_counters)
  358. return;
  359. num_gpes = acpi_current_gpe_count;
  360. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  361. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  362. GFP_KERNEL);
  363. if (all_attrs == NULL)
  364. return;
  365. all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
  366. GFP_KERNEL);
  367. if (all_counters == NULL)
  368. goto fail;
  369. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  370. GFP_KERNEL);
  371. if (counter_attrs == NULL)
  372. goto fail;
  373. for (i = 0; i < num_counters; ++i) {
  374. char buffer[12];
  375. char *name;
  376. if (i < num_gpes)
  377. sprintf(buffer, "gpe%02X", i);
  378. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  379. sprintf(buffer, "ff_pmtimer");
  380. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  381. sprintf(buffer, "ff_gbl_lock");
  382. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  383. sprintf(buffer, "ff_pwr_btn");
  384. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  385. sprintf(buffer, "ff_slp_btn");
  386. else if (i == num_gpes + ACPI_EVENT_RTC)
  387. sprintf(buffer, "ff_rt_clk");
  388. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  389. sprintf(buffer, "gpe_all");
  390. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  391. sprintf(buffer, "sci");
  392. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI_NOT)
  393. sprintf(buffer, "sci_not");
  394. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  395. sprintf(buffer, "error");
  396. else
  397. sprintf(buffer, "bug%02X", i);
  398. name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
  399. if (name == NULL)
  400. goto fail;
  401. strncpy(name, buffer, strlen(buffer) + 1);
  402. counter_attrs[i].attr.name = name;
  403. counter_attrs[i].attr.mode = 0644;
  404. counter_attrs[i].show = counter_show;
  405. counter_attrs[i].store = counter_set;
  406. all_attrs[i] = &counter_attrs[i].attr;
  407. }
  408. interrupt_stats_attr_group.attrs = all_attrs;
  409. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  410. return;
  411. fail:
  412. delete_gpe_attr_array();
  413. return;
  414. }
  415. static void __exit interrupt_stats_exit(void)
  416. {
  417. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  418. delete_gpe_attr_array();
  419. return;
  420. }
  421. /* --------------------------------------------------------------------------
  422. FS Interface (/proc)
  423. -------------------------------------------------------------------------- */
  424. #ifdef CONFIG_ACPI_PROCFS
  425. #define ACPI_SYSTEM_FILE_INFO "info"
  426. #define ACPI_SYSTEM_FILE_EVENT "event"
  427. #define ACPI_SYSTEM_FILE_DSDT "dsdt"
  428. #define ACPI_SYSTEM_FILE_FADT "fadt"
  429. static int acpi_system_read_info(struct seq_file *seq, void *offset)
  430. {
  431. seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
  432. return 0;
  433. }
  434. static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
  435. {
  436. return single_open(file, acpi_system_read_info, PDE(inode)->data);
  437. }
  438. static const struct file_operations acpi_system_info_ops = {
  439. .owner = THIS_MODULE,
  440. .open = acpi_system_info_open_fs,
  441. .read = seq_read,
  442. .llseek = seq_lseek,
  443. .release = single_release,
  444. };
  445. static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
  446. loff_t *);
  447. static const struct file_operations acpi_system_dsdt_ops = {
  448. .owner = THIS_MODULE,
  449. .read = acpi_system_read_dsdt,
  450. };
  451. static ssize_t
  452. acpi_system_read_dsdt(struct file *file,
  453. char __user * buffer, size_t count, loff_t * ppos)
  454. {
  455. acpi_status status = AE_OK;
  456. struct acpi_table_header *dsdt = NULL;
  457. ssize_t res;
  458. status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
  459. if (ACPI_FAILURE(status))
  460. return -ENODEV;
  461. res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
  462. return res;
  463. }
  464. static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
  465. loff_t *);
  466. static const struct file_operations acpi_system_fadt_ops = {
  467. .owner = THIS_MODULE,
  468. .read = acpi_system_read_fadt,
  469. };
  470. static ssize_t
  471. acpi_system_read_fadt(struct file *file,
  472. char __user * buffer, size_t count, loff_t * ppos)
  473. {
  474. acpi_status status = AE_OK;
  475. struct acpi_table_header *fadt = NULL;
  476. ssize_t res;
  477. status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
  478. if (ACPI_FAILURE(status))
  479. return -ENODEV;
  480. res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
  481. return res;
  482. }
  483. static int acpi_system_procfs_init(void)
  484. {
  485. struct proc_dir_entry *entry;
  486. int error = 0;
  487. /* 'info' [R] */
  488. entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
  489. &acpi_system_info_ops);
  490. if (!entry)
  491. goto Error;
  492. /* 'dsdt' [R] */
  493. entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
  494. &acpi_system_dsdt_ops);
  495. if (!entry)
  496. goto Error;
  497. /* 'fadt' [R] */
  498. entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
  499. &acpi_system_fadt_ops);
  500. if (!entry)
  501. goto Error;
  502. Done:
  503. return error;
  504. Error:
  505. remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
  506. remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
  507. remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
  508. error = -EFAULT;
  509. goto Done;
  510. }
  511. #else
  512. static int acpi_system_procfs_init(void)
  513. {
  514. return 0;
  515. }
  516. #endif
  517. int __init acpi_system_init(void)
  518. {
  519. int result;
  520. result = acpi_system_procfs_init();
  521. if (result)
  522. return result;
  523. result = acpi_system_sysfs_init();
  524. return result;
  525. }