system.c 16 KB

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