system.c 15 KB

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