system.c 16 KB

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