sfi_acpi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* sfi_acpi.c Simple Firmware Interface - ACPI extensions */
  2. /*
  3. This file is provided under a dual BSD/GPLv2 license. When using or
  4. redistributing this file, you may do so under either license.
  5. GPL LICENSE SUMMARY
  6. Copyright(c) 2009 Intel Corporation. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of version 2 of the GNU General Public License as
  9. published by the Free Software Foundation.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. The full GNU General Public License is included in this distribution
  18. in the file called LICENSE.GPL.
  19. BSD LICENSE
  20. Copyright(c) 2009 Intel Corporation. All rights reserved.
  21. Redistribution and use in source and binary forms, with or without
  22. modification, are permitted provided that the following conditions
  23. are met:
  24. * Redistributions of source code must retain the above copyright
  25. notice, this list of conditions and the following disclaimer.
  26. * Redistributions in binary form must reproduce the above copyright
  27. notice, this list of conditions and the following disclaimer in
  28. the documentation and/or other materials provided with the
  29. distribution.
  30. * Neither the name of Intel Corporation nor the names of its
  31. contributors may be used to endorse or promote products derived
  32. from this software without specific prior written permission.
  33. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  37. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  39. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  40. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  41. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  43. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. #define KMSG_COMPONENT "SFI"
  46. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  47. #include <linux/kernel.h>
  48. #include <linux/sfi_acpi.h>
  49. #include "sfi_core.h"
  50. /*
  51. * SFI can access ACPI-defined tables via an optional ACPI XSDT.
  52. *
  53. * This allows re-use, and avoids re-definition, of standard tables.
  54. * For example, the "MCFG" table is defined by PCI, reserved by ACPI,
  55. * and is expected to be present many SFI-only systems.
  56. */
  57. static struct acpi_table_xsdt *xsdt_va __read_mostly;
  58. #define XSDT_GET_NUM_ENTRIES(ptable, entry_type) \
  59. ((ptable->header.length - sizeof(struct acpi_table_header)) / \
  60. (sizeof(entry_type)))
  61. static inline struct sfi_table_header *acpi_to_sfi_th(
  62. struct acpi_table_header *th)
  63. {
  64. return (struct sfi_table_header *)th;
  65. }
  66. static inline struct acpi_table_header *sfi_to_acpi_th(
  67. struct sfi_table_header *th)
  68. {
  69. return (struct acpi_table_header *)th;
  70. }
  71. /*
  72. * sfi_acpi_parse_xsdt()
  73. *
  74. * Parse the ACPI XSDT for later access by sfi_acpi_table_parse().
  75. */
  76. static int __init sfi_acpi_parse_xsdt(struct sfi_table_header *th)
  77. {
  78. struct sfi_table_key key = SFI_ANY_KEY;
  79. int tbl_cnt, i;
  80. void *ret;
  81. xsdt_va = (struct acpi_table_xsdt *)th;
  82. tbl_cnt = XSDT_GET_NUM_ENTRIES(xsdt_va, u64);
  83. for (i = 0; i < tbl_cnt; i++) {
  84. ret = sfi_check_table(xsdt_va->table_offset_entry[i], &key);
  85. if (IS_ERR(ret)) {
  86. disable_sfi();
  87. return -1;
  88. }
  89. }
  90. return 0;
  91. }
  92. int __init sfi_acpi_init(void)
  93. {
  94. struct sfi_table_key xsdt_key = { .sig = SFI_SIG_XSDT };
  95. sfi_table_parse(SFI_SIG_XSDT, NULL, NULL, sfi_acpi_parse_xsdt);
  96. /* Only call the get_table to keep the table mapped */
  97. xsdt_va = (struct acpi_table_xsdt *)sfi_get_table(&xsdt_key);
  98. return 0;
  99. }
  100. static struct acpi_table_header *sfi_acpi_get_table(struct sfi_table_key *key)
  101. {
  102. u32 tbl_cnt, i;
  103. void *ret;
  104. tbl_cnt = XSDT_GET_NUM_ENTRIES(xsdt_va, u64);
  105. for (i = 0; i < tbl_cnt; i++) {
  106. ret = sfi_check_table(xsdt_va->table_offset_entry[i], key);
  107. if (!IS_ERR(ret) && ret)
  108. return sfi_to_acpi_th(ret);
  109. }
  110. return NULL;
  111. }
  112. static void sfi_acpi_put_table(struct acpi_table_header *table)
  113. {
  114. sfi_put_table(acpi_to_sfi_th(table));
  115. }
  116. /*
  117. * sfi_acpi_table_parse()
  118. *
  119. * Find specified table in XSDT, run handler on it and return its return value
  120. */
  121. int sfi_acpi_table_parse(char *signature, char *oem_id, char *oem_table_id,
  122. int(*handler)(struct acpi_table_header *))
  123. {
  124. struct acpi_table_header *table = NULL;
  125. struct sfi_table_key key;
  126. int ret = 0;
  127. if (sfi_disabled)
  128. return -1;
  129. key.sig = signature;
  130. key.oem_id = oem_id;
  131. key.oem_table_id = oem_table_id;
  132. table = sfi_acpi_get_table(&key);
  133. if (!table)
  134. return -EINVAL;
  135. ret = handler(table);
  136. sfi_acpi_put_table(table);
  137. return ret;
  138. }
  139. static ssize_t sfi_acpi_table_show(struct file *filp, struct kobject *kobj,
  140. struct bin_attribute *bin_attr, char *buf,
  141. loff_t offset, size_t count)
  142. {
  143. struct sfi_table_attr *tbl_attr =
  144. container_of(bin_attr, struct sfi_table_attr, attr);
  145. struct acpi_table_header *th = NULL;
  146. struct sfi_table_key key;
  147. ssize_t cnt;
  148. key.sig = tbl_attr->name;
  149. key.oem_id = NULL;
  150. key.oem_table_id = NULL;
  151. th = sfi_acpi_get_table(&key);
  152. if (!th)
  153. return 0;
  154. cnt = memory_read_from_buffer(buf, count, &offset,
  155. th, th->length);
  156. sfi_acpi_put_table(th);
  157. return cnt;
  158. }
  159. void __init sfi_acpi_sysfs_init(void)
  160. {
  161. u32 tbl_cnt, i;
  162. struct sfi_table_attr *tbl_attr;
  163. tbl_cnt = XSDT_GET_NUM_ENTRIES(xsdt_va, u64);
  164. for (i = 0; i < tbl_cnt; i++) {
  165. tbl_attr =
  166. sfi_sysfs_install_table(xsdt_va->table_offset_entry[i]);
  167. tbl_attr->attr.read = sfi_acpi_table_show;
  168. }
  169. return;
  170. }