system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #ifdef MODULE_PARAM_PREFIX
  34. #undef MODULE_PARAM_PREFIX
  35. #endif
  36. #define MODULE_PARAM_PREFIX "acpi."
  37. #define ACPI_SYSTEM_CLASS "system"
  38. #define ACPI_SYSTEM_DEVICE_NAME "System"
  39. u32 acpi_irq_handled;
  40. /*
  41. * Make ACPICA version work as module param
  42. */
  43. static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
  44. {
  45. int result;
  46. result = sprintf(buffer, "%x", ACPI_CA_VERSION);
  47. return result;
  48. }
  49. module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
  50. /* --------------------------------------------------------------------------
  51. FS Interface (/sys)
  52. -------------------------------------------------------------------------- */
  53. static LIST_HEAD(acpi_table_attr_list);
  54. static struct kobject *tables_kobj;
  55. static struct kobject *dynamic_tables_kobj;
  56. struct acpi_table_attr {
  57. struct bin_attribute attr;
  58. char name[8];
  59. int instance;
  60. struct list_head node;
  61. };
  62. static ssize_t acpi_table_show(struct kobject *kobj,
  63. struct bin_attribute *bin_attr, char *buf,
  64. loff_t offset, size_t count)
  65. {
  66. struct acpi_table_attr *table_attr =
  67. container_of(bin_attr, struct acpi_table_attr, attr);
  68. struct acpi_table_header *table_header = NULL;
  69. acpi_status status;
  70. char name[ACPI_NAME_SIZE];
  71. if (strncmp(table_attr->name, "NULL", 4))
  72. memcpy(name, table_attr->name, ACPI_NAME_SIZE);
  73. else
  74. memcpy(name, "\0\0\0\0", 4);
  75. status =
  76. acpi_get_table(name, table_attr->instance,
  77. &table_header);
  78. if (ACPI_FAILURE(status))
  79. return -ENODEV;
  80. return memory_read_from_buffer(buf, count, &offset,
  81. table_header, table_header->length);
  82. }
  83. static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
  84. struct acpi_table_header *table_header)
  85. {
  86. struct acpi_table_header *header = NULL;
  87. struct acpi_table_attr *attr = NULL;
  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 = 0444;
  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_ERROR 2 /* other */
  190. #define NUM_COUNTERS_EXTRA 3
  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_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. goto end;
  310. }
  311. /* show the event status for both GPEs and Fixed Events */
  312. result = get_status(index, &status, &handle);
  313. if (result)
  314. goto end;
  315. if (!(status & ACPI_EVENT_FLAG_HANDLE)) {
  316. printk(KERN_WARNING PREFIX
  317. "Can not change Invalid GPE/Fixed Event status\n");
  318. return -EINVAL;
  319. }
  320. if (index < num_gpes) {
  321. if (!strcmp(buf, "disable\n") &&
  322. (status & ACPI_EVENT_FLAG_ENABLED))
  323. result = acpi_disable_gpe(handle, index);
  324. else if (!strcmp(buf, "enable\n") &&
  325. !(status & ACPI_EVENT_FLAG_ENABLED))
  326. result = acpi_enable_gpe(handle, index);
  327. else if (!strcmp(buf, "clear\n") &&
  328. (status & ACPI_EVENT_FLAG_SET))
  329. result = acpi_clear_gpe(handle, index, ACPI_NOT_ISR);
  330. else
  331. all_counters[index].count = strtoul(buf, NULL, 0);
  332. } else if (index < num_gpes + ACPI_NUM_FIXED_EVENTS) {
  333. int event = index - num_gpes;
  334. if (!strcmp(buf, "disable\n") &&
  335. (status & ACPI_EVENT_FLAG_ENABLED))
  336. result = acpi_disable_event(event, ACPI_NOT_ISR);
  337. else if (!strcmp(buf, "enable\n") &&
  338. !(status & ACPI_EVENT_FLAG_ENABLED))
  339. result = acpi_enable_event(event, ACPI_NOT_ISR);
  340. else if (!strcmp(buf, "clear\n") &&
  341. (status & ACPI_EVENT_FLAG_SET))
  342. result = acpi_clear_event(event);
  343. else
  344. all_counters[index].count = strtoul(buf, NULL, 0);
  345. } else
  346. all_counters[index].count = strtoul(buf, NULL, 0);
  347. if (ACPI_FAILURE(result))
  348. result = -EINVAL;
  349. end:
  350. return result ? result : size;
  351. }
  352. void acpi_irq_stats_init(void)
  353. {
  354. int i;
  355. if (all_counters)
  356. return;
  357. num_gpes = acpi_current_gpe_count;
  358. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  359. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  360. GFP_KERNEL);
  361. if (all_attrs == NULL)
  362. return;
  363. all_counters = kzalloc(sizeof(struct event_counter) * (num_counters),
  364. GFP_KERNEL);
  365. if (all_counters == NULL)
  366. goto fail;
  367. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  368. GFP_KERNEL);
  369. if (counter_attrs == NULL)
  370. goto fail;
  371. for (i = 0; i < num_counters; ++i) {
  372. char buffer[12];
  373. char *name;
  374. if (i < num_gpes)
  375. sprintf(buffer, "gpe%02X", i);
  376. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  377. sprintf(buffer, "ff_pmtimer");
  378. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  379. sprintf(buffer, "ff_gbl_lock");
  380. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  381. sprintf(buffer, "ff_pwr_btn");
  382. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  383. sprintf(buffer, "ff_slp_btn");
  384. else if (i == num_gpes + ACPI_EVENT_RTC)
  385. sprintf(buffer, "ff_rt_clk");
  386. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  387. sprintf(buffer, "gpe_all");
  388. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  389. sprintf(buffer, "sci");
  390. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  391. sprintf(buffer, "error");
  392. else
  393. sprintf(buffer, "bug%02X", i);
  394. name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
  395. if (name == NULL)
  396. goto fail;
  397. strncpy(name, buffer, strlen(buffer) + 1);
  398. counter_attrs[i].attr.name = name;
  399. counter_attrs[i].attr.mode = 0644;
  400. counter_attrs[i].show = counter_show;
  401. counter_attrs[i].store = counter_set;
  402. all_attrs[i] = &counter_attrs[i].attr;
  403. }
  404. interrupt_stats_attr_group.attrs = all_attrs;
  405. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  406. return;
  407. fail:
  408. delete_gpe_attr_array();
  409. return;
  410. }
  411. static void __exit interrupt_stats_exit(void)
  412. {
  413. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  414. delete_gpe_attr_array();
  415. return;
  416. }
  417. /* --------------------------------------------------------------------------
  418. FS Interface (/proc)
  419. -------------------------------------------------------------------------- */
  420. #ifdef CONFIG_ACPI_PROCFS
  421. #define ACPI_SYSTEM_FILE_INFO "info"
  422. #define ACPI_SYSTEM_FILE_EVENT "event"
  423. #define ACPI_SYSTEM_FILE_DSDT "dsdt"
  424. #define ACPI_SYSTEM_FILE_FADT "fadt"
  425. static int acpi_system_read_info(struct seq_file *seq, void *offset)
  426. {
  427. seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
  428. return 0;
  429. }
  430. static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
  431. {
  432. return single_open(file, acpi_system_read_info, PDE(inode)->data);
  433. }
  434. static const struct file_operations acpi_system_info_ops = {
  435. .owner = THIS_MODULE,
  436. .open = acpi_system_info_open_fs,
  437. .read = seq_read,
  438. .llseek = seq_lseek,
  439. .release = single_release,
  440. };
  441. static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
  442. loff_t *);
  443. static const struct file_operations acpi_system_dsdt_ops = {
  444. .owner = THIS_MODULE,
  445. .read = acpi_system_read_dsdt,
  446. };
  447. static ssize_t
  448. acpi_system_read_dsdt(struct file *file,
  449. char __user * buffer, size_t count, loff_t * ppos)
  450. {
  451. acpi_status status = AE_OK;
  452. struct acpi_table_header *dsdt = NULL;
  453. ssize_t res;
  454. status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
  455. if (ACPI_FAILURE(status))
  456. return -ENODEV;
  457. res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
  458. return res;
  459. }
  460. static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
  461. loff_t *);
  462. static const struct file_operations acpi_system_fadt_ops = {
  463. .owner = THIS_MODULE,
  464. .read = acpi_system_read_fadt,
  465. };
  466. static ssize_t
  467. acpi_system_read_fadt(struct file *file,
  468. char __user * buffer, size_t count, loff_t * ppos)
  469. {
  470. acpi_status status = AE_OK;
  471. struct acpi_table_header *fadt = NULL;
  472. ssize_t res;
  473. status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
  474. if (ACPI_FAILURE(status))
  475. return -ENODEV;
  476. res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
  477. return res;
  478. }
  479. static int acpi_system_procfs_init(void)
  480. {
  481. struct proc_dir_entry *entry;
  482. int error = 0;
  483. /* 'info' [R] */
  484. entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
  485. &acpi_system_info_ops);
  486. if (!entry)
  487. goto Error;
  488. /* 'dsdt' [R] */
  489. entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
  490. &acpi_system_dsdt_ops);
  491. if (!entry)
  492. goto Error;
  493. /* 'fadt' [R] */
  494. entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
  495. &acpi_system_fadt_ops);
  496. if (!entry)
  497. goto Error;
  498. Done:
  499. return error;
  500. Error:
  501. remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
  502. remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
  503. remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
  504. error = -EFAULT;
  505. goto Done;
  506. }
  507. #else
  508. static int acpi_system_procfs_init(void)
  509. {
  510. return 0;
  511. }
  512. #endif
  513. int __init acpi_system_init(void)
  514. {
  515. int result;
  516. result = acpi_system_procfs_init();
  517. if (result)
  518. return result;
  519. result = acpi_system_sysfs_init();
  520. return result;
  521. }