system.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 <asm/uaccess.h>
  29. #include <acpi/acpi_drivers.h>
  30. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  31. ACPI_MODULE_NAME("system");
  32. #ifdef MODULE_PARAM_PREFIX
  33. #undef MODULE_PARAM_PREFIX
  34. #endif
  35. #define MODULE_PARAM_PREFIX "acpi."
  36. #define ACPI_SYSTEM_CLASS "system"
  37. #define ACPI_SYSTEM_DEVICE_NAME "System"
  38. u32 acpi_irq_handled;
  39. /*
  40. * Make ACPICA version work as module param
  41. */
  42. static int param_get_acpica_version(char *buffer, struct kernel_param *kp)
  43. {
  44. int result;
  45. result = sprintf(buffer, "%x", ACPI_CA_VERSION);
  46. return result;
  47. }
  48. module_param_call(acpica_version, NULL, param_get_acpica_version, NULL, 0444);
  49. /* --------------------------------------------------------------------------
  50. FS Interface (/sys)
  51. -------------------------------------------------------------------------- */
  52. static LIST_HEAD(acpi_table_attr_list);
  53. static struct kobject *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. ssize_t ret_count = count;
  69. status =
  70. acpi_get_table(table_attr->name, table_attr->instance,
  71. &table_header);
  72. if (ACPI_FAILURE(status))
  73. return -ENODEV;
  74. if (offset >= table_header->length) {
  75. ret_count = 0;
  76. goto end;
  77. }
  78. if (offset + ret_count > table_header->length)
  79. ret_count = table_header->length - offset;
  80. memcpy(buf, ((char *)table_header) + offset, ret_count);
  81. end:
  82. return ret_count;
  83. }
  84. static void acpi_table_attr_init(struct acpi_table_attr *table_attr,
  85. struct acpi_table_header *table_header)
  86. {
  87. struct acpi_table_header *header = NULL;
  88. struct acpi_table_attr *attr = NULL;
  89. memcpy(table_attr->name, table_header->signature, ACPI_NAME_SIZE);
  90. list_for_each_entry(attr, &acpi_table_attr_list, node) {
  91. if (!memcmp(table_header->signature, attr->name,
  92. 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(table_header->
  99. signature, 2,
  100. &header)))
  101. sprintf(table_attr->name + 4, "%d", 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 = 0444;
  106. table_attr->attr.attr.owner = THIS_MODULE;
  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. static u32 *all_counters;
  151. static u32 num_gpes;
  152. static u32 num_counters;
  153. static struct attribute **all_attrs;
  154. static u32 acpi_gpe_count;
  155. static struct attribute_group interrupt_stats_attr_group = {
  156. .name = "interrupts",
  157. };
  158. static struct kobj_attribute *counter_attrs;
  159. static int count_num_gpes(void)
  160. {
  161. int count = 0;
  162. struct acpi_gpe_xrupt_info *gpe_xrupt_info;
  163. struct acpi_gpe_block_info *gpe_block;
  164. acpi_cpu_flags flags;
  165. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  166. gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
  167. while (gpe_xrupt_info) {
  168. gpe_block = gpe_xrupt_info->gpe_block_list_head;
  169. while (gpe_block) {
  170. count += gpe_block->register_count *
  171. ACPI_GPE_REGISTER_WIDTH;
  172. gpe_block = gpe_block->next;
  173. }
  174. gpe_xrupt_info = gpe_xrupt_info->next;
  175. }
  176. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  177. return count;
  178. }
  179. static void delete_gpe_attr_array(void)
  180. {
  181. u32 *tmp = all_counters;
  182. all_counters = NULL;
  183. kfree(tmp);
  184. if (counter_attrs) {
  185. int i;
  186. for (i = 0; i < num_gpes; i++)
  187. kfree(counter_attrs[i].attr.name);
  188. kfree(counter_attrs);
  189. }
  190. kfree(all_attrs);
  191. return;
  192. }
  193. void acpi_os_gpe_count(u32 gpe_number)
  194. {
  195. acpi_gpe_count++;
  196. if (!all_counters)
  197. return;
  198. if (gpe_number < num_gpes)
  199. all_counters[gpe_number]++;
  200. else
  201. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR]++;
  202. return;
  203. }
  204. void acpi_os_fixed_event_count(u32 event_number)
  205. {
  206. if (!all_counters)
  207. return;
  208. if (event_number < ACPI_NUM_FIXED_EVENTS)
  209. all_counters[num_gpes + event_number]++;
  210. else
  211. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR]++;
  212. return;
  213. }
  214. static ssize_t counter_show(struct kobject *kobj,
  215. struct kobj_attribute *attr, char *buf)
  216. {
  217. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI] =
  218. acpi_irq_handled;
  219. all_counters[num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE] =
  220. acpi_gpe_count;
  221. return sprintf(buf, "%d\n", all_counters[attr - counter_attrs]);
  222. }
  223. /*
  224. * counter_set() sets the specified counter.
  225. * setting the total "sci" file to any value clears all counters.
  226. */
  227. static ssize_t counter_set(struct kobject *kobj,
  228. struct kobj_attribute *attr, const char *buf, size_t size)
  229. {
  230. int index = attr - counter_attrs;
  231. if (index == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI) {
  232. int i;
  233. for (i = 0; i < num_counters; ++i)
  234. all_counters[i] = 0;
  235. acpi_gpe_count = 0;
  236. acpi_irq_handled = 0;
  237. } else
  238. all_counters[index] = strtoul(buf, NULL, 0);
  239. return size;
  240. }
  241. void acpi_irq_stats_init(void)
  242. {
  243. int i;
  244. if (all_counters)
  245. return;
  246. num_gpes = count_num_gpes();
  247. num_counters = num_gpes + ACPI_NUM_FIXED_EVENTS + NUM_COUNTERS_EXTRA;
  248. all_attrs = kzalloc(sizeof(struct attribute *) * (num_counters + 1),
  249. GFP_KERNEL);
  250. if (all_attrs == NULL)
  251. return;
  252. all_counters = kzalloc(sizeof(u32) * (num_counters), GFP_KERNEL);
  253. if (all_counters == NULL)
  254. goto fail;
  255. counter_attrs = kzalloc(sizeof(struct kobj_attribute) * (num_counters),
  256. GFP_KERNEL);
  257. if (counter_attrs == NULL)
  258. goto fail;
  259. for (i = 0; i < num_counters; ++i) {
  260. char buffer[12];
  261. char *name;
  262. if (i < num_gpes)
  263. sprintf(buffer, "gpe%02X", i);
  264. else if (i == num_gpes + ACPI_EVENT_PMTIMER)
  265. sprintf(buffer, "ff_pmtimer");
  266. else if (i == num_gpes + ACPI_EVENT_GLOBAL)
  267. sprintf(buffer, "ff_gbl_lock");
  268. else if (i == num_gpes + ACPI_EVENT_POWER_BUTTON)
  269. sprintf(buffer, "ff_pwr_btn");
  270. else if (i == num_gpes + ACPI_EVENT_SLEEP_BUTTON)
  271. sprintf(buffer, "ff_slp_btn");
  272. else if (i == num_gpes + ACPI_EVENT_RTC)
  273. sprintf(buffer, "ff_rt_clk");
  274. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_GPE)
  275. sprintf(buffer, "gpe_all");
  276. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_SCI)
  277. sprintf(buffer, "sci");
  278. else if (i == num_gpes + ACPI_NUM_FIXED_EVENTS + COUNT_ERROR)
  279. sprintf(buffer, "error");
  280. else
  281. sprintf(buffer, "bug%02X", i);
  282. name = kzalloc(strlen(buffer) + 1, GFP_KERNEL);
  283. if (name == NULL)
  284. goto fail;
  285. strncpy(name, buffer, strlen(buffer) + 1);
  286. counter_attrs[i].attr.name = name;
  287. counter_attrs[i].attr.mode = 0644;
  288. counter_attrs[i].show = counter_show;
  289. counter_attrs[i].store = counter_set;
  290. all_attrs[i] = &counter_attrs[i].attr;
  291. }
  292. interrupt_stats_attr_group.attrs = all_attrs;
  293. if (!sysfs_create_group(acpi_kobj, &interrupt_stats_attr_group))
  294. return;
  295. fail:
  296. delete_gpe_attr_array();
  297. return;
  298. }
  299. static void __exit interrupt_stats_exit(void)
  300. {
  301. sysfs_remove_group(acpi_kobj, &interrupt_stats_attr_group);
  302. delete_gpe_attr_array();
  303. return;
  304. }
  305. /* --------------------------------------------------------------------------
  306. FS Interface (/proc)
  307. -------------------------------------------------------------------------- */
  308. #ifdef CONFIG_ACPI_PROCFS
  309. #define ACPI_SYSTEM_FILE_INFO "info"
  310. #define ACPI_SYSTEM_FILE_EVENT "event"
  311. #define ACPI_SYSTEM_FILE_DSDT "dsdt"
  312. #define ACPI_SYSTEM_FILE_FADT "fadt"
  313. static int acpi_system_read_info(struct seq_file *seq, void *offset)
  314. {
  315. seq_printf(seq, "version: %x\n", ACPI_CA_VERSION);
  316. return 0;
  317. }
  318. static int acpi_system_info_open_fs(struct inode *inode, struct file *file)
  319. {
  320. return single_open(file, acpi_system_read_info, PDE(inode)->data);
  321. }
  322. static const struct file_operations acpi_system_info_ops = {
  323. .owner = THIS_MODULE,
  324. .open = acpi_system_info_open_fs,
  325. .read = seq_read,
  326. .llseek = seq_lseek,
  327. .release = single_release,
  328. };
  329. static ssize_t acpi_system_read_dsdt(struct file *, char __user *, size_t,
  330. loff_t *);
  331. static const struct file_operations acpi_system_dsdt_ops = {
  332. .owner = THIS_MODULE,
  333. .read = acpi_system_read_dsdt,
  334. };
  335. static ssize_t
  336. acpi_system_read_dsdt(struct file *file,
  337. char __user * buffer, size_t count, loff_t * ppos)
  338. {
  339. acpi_status status = AE_OK;
  340. struct acpi_table_header *dsdt = NULL;
  341. ssize_t res;
  342. status = acpi_get_table(ACPI_SIG_DSDT, 1, &dsdt);
  343. if (ACPI_FAILURE(status))
  344. return -ENODEV;
  345. res = simple_read_from_buffer(buffer, count, ppos, dsdt, dsdt->length);
  346. return res;
  347. }
  348. static ssize_t acpi_system_read_fadt(struct file *, char __user *, size_t,
  349. loff_t *);
  350. static const struct file_operations acpi_system_fadt_ops = {
  351. .owner = THIS_MODULE,
  352. .read = acpi_system_read_fadt,
  353. };
  354. static ssize_t
  355. acpi_system_read_fadt(struct file *file,
  356. char __user * buffer, size_t count, loff_t * ppos)
  357. {
  358. acpi_status status = AE_OK;
  359. struct acpi_table_header *fadt = NULL;
  360. ssize_t res;
  361. status = acpi_get_table(ACPI_SIG_FADT, 1, &fadt);
  362. if (ACPI_FAILURE(status))
  363. return -ENODEV;
  364. res = simple_read_from_buffer(buffer, count, ppos, fadt, fadt->length);
  365. return res;
  366. }
  367. static int acpi_system_procfs_init(void)
  368. {
  369. struct proc_dir_entry *entry;
  370. int error = 0;
  371. /* 'info' [R] */
  372. entry = proc_create(ACPI_SYSTEM_FILE_INFO, S_IRUGO, acpi_root_dir,
  373. &acpi_system_info_ops);
  374. if (!entry)
  375. goto Error;
  376. /* 'dsdt' [R] */
  377. entry = proc_create(ACPI_SYSTEM_FILE_DSDT, S_IRUSR, acpi_root_dir,
  378. &acpi_system_dsdt_ops);
  379. if (!entry)
  380. goto Error;
  381. /* 'fadt' [R] */
  382. entry = proc_create(ACPI_SYSTEM_FILE_FADT, S_IRUSR, acpi_root_dir,
  383. &acpi_system_fadt_ops);
  384. if (!entry)
  385. goto Error;
  386. Done:
  387. return error;
  388. Error:
  389. remove_proc_entry(ACPI_SYSTEM_FILE_FADT, acpi_root_dir);
  390. remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_root_dir);
  391. remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_root_dir);
  392. error = -EFAULT;
  393. goto Done;
  394. }
  395. #else
  396. static int acpi_system_procfs_init(void)
  397. {
  398. return 0;
  399. }
  400. #endif
  401. static int __init acpi_system_init(void)
  402. {
  403. int result = 0;
  404. if (acpi_disabled)
  405. return 0;
  406. result = acpi_system_procfs_init();
  407. if (result)
  408. return result;
  409. result = acpi_system_sysfs_init();
  410. return result;
  411. }
  412. subsys_initcall(acpi_system_init);