sfi_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* sfi_core.c Simple Firmware Interface - core internals */
  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/bootmem.h>
  48. #include <linux/kernel.h>
  49. #include <linux/module.h>
  50. #include <linux/errno.h>
  51. #include <linux/types.h>
  52. #include <linux/acpi.h>
  53. #include <linux/init.h>
  54. #include <linux/sfi.h>
  55. #include "sfi_core.h"
  56. #define ON_SAME_PAGE(addr1, addr2) \
  57. (((unsigned long)(addr1) & PAGE_MASK) == \
  58. ((unsigned long)(addr2) & PAGE_MASK))
  59. #define TABLE_ON_PAGE(page, table, size) (ON_SAME_PAGE(page, table) && \
  60. ON_SAME_PAGE(page, table + size))
  61. int sfi_disabled __read_mostly;
  62. EXPORT_SYMBOL(sfi_disabled);
  63. static u64 syst_pa __read_mostly;
  64. static struct sfi_table_simple *syst_va __read_mostly;
  65. /*
  66. * FW creates and saves the SFI tables in memory. When these tables get
  67. * used, they may need to be mapped to virtual address space, and the mapping
  68. * can happen before or after the ioremap() is ready, so a flag is needed
  69. * to indicating this
  70. */
  71. static u32 sfi_use_ioremap __read_mostly;
  72. static void __iomem *sfi_map_memory(u64 phys, u32 size)
  73. {
  74. if (!phys || !size)
  75. return NULL;
  76. if (sfi_use_ioremap)
  77. return ioremap(phys, size);
  78. else
  79. return early_ioremap(phys, size);
  80. }
  81. static void sfi_unmap_memory(void __iomem *virt, u32 size)
  82. {
  83. if (!virt || !size)
  84. return;
  85. if (sfi_use_ioremap)
  86. iounmap(virt);
  87. else
  88. early_iounmap(virt, size);
  89. }
  90. static void sfi_print_table_header(unsigned long long pa,
  91. struct sfi_table_header *header)
  92. {
  93. pr_info("%4.4s %llX, %04X (v%d %6.6s %8.8s)\n",
  94. header->sig, pa,
  95. header->len, header->rev, header->oem_id,
  96. header->oem_table_id);
  97. }
  98. /*
  99. * sfi_verify_table()
  100. * Sanity check table lengh, calculate checksum
  101. */
  102. static __init int sfi_verify_table(struct sfi_table_header *table)
  103. {
  104. u8 checksum = 0;
  105. u8 *puchar = (u8 *)table;
  106. u32 length = table->len;
  107. /* Sanity check table length against arbitrary 1MB limit */
  108. if (length > 0x100000) {
  109. pr_err("Invalid table length 0x%x\n", length);
  110. return -1;
  111. }
  112. while (length--)
  113. checksum += *puchar++;
  114. if (checksum) {
  115. pr_err("Checksum %2.2X should be %2.2X\n",
  116. table->csum, table->csum - checksum);
  117. return -1;
  118. }
  119. return 0;
  120. }
  121. /*
  122. * sfi_map_table()
  123. *
  124. * Return address of mapped table
  125. * Check for common case that we can re-use mapping to SYST,
  126. * which requires syst_pa, syst_va to be initialized.
  127. */
  128. struct sfi_table_header *sfi_map_table(u64 pa)
  129. {
  130. struct sfi_table_header *th;
  131. u32 length;
  132. if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
  133. th = sfi_map_memory(pa, sizeof(struct sfi_table_header));
  134. else
  135. th = (void *)syst_va + (pa - syst_pa);
  136. /* If table fits on same page as its header, we are done */
  137. if (TABLE_ON_PAGE(th, th, th->len))
  138. return th;
  139. /* Entire table does not fit on same page as SYST */
  140. length = th->len;
  141. if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
  142. sfi_unmap_memory(th, sizeof(struct sfi_table_header));
  143. return sfi_map_memory(pa, length);
  144. }
  145. /*
  146. * sfi_unmap_table()
  147. *
  148. * Undoes effect of sfi_map_table() by unmapping table
  149. * if it did not completely fit on same page as SYST.
  150. */
  151. void sfi_unmap_table(struct sfi_table_header *th)
  152. {
  153. if (!TABLE_ON_PAGE(syst_va, th, th->len))
  154. sfi_unmap_memory(th, TABLE_ON_PAGE(th, th, th->len) ?
  155. sizeof(*th) : th->len);
  156. }
  157. static int sfi_table_check_key(struct sfi_table_header *th,
  158. struct sfi_table_key *key)
  159. {
  160. if (strncmp(th->sig, key->sig, SFI_SIGNATURE_SIZE)
  161. || (key->oem_id && strncmp(th->oem_id,
  162. key->oem_id, SFI_OEM_ID_SIZE))
  163. || (key->oem_table_id && strncmp(th->oem_table_id,
  164. key->oem_table_id, SFI_OEM_TABLE_ID_SIZE)))
  165. return -1;
  166. return 0;
  167. }
  168. /*
  169. * This function will be used in 2 cases:
  170. * 1. used to enumerate and verify the tables addressed by SYST/XSDT,
  171. * thus no signature will be given (in kernel boot phase)
  172. * 2. used to parse one specific table, signature must exist, and
  173. * the mapped virt address will be returned, and the virt space
  174. * will be released by call sfi_put_table() later
  175. *
  176. * Return value:
  177. * NULL: when can't find a table matching the key
  178. * ERR_PTR(error): error value
  179. * virt table address: when a matched table is found
  180. */
  181. struct sfi_table_header *sfi_check_table(u64 pa, struct sfi_table_key *key)
  182. {
  183. struct sfi_table_header *th;
  184. void *ret = NULL;
  185. th = sfi_map_table(pa);
  186. if (!th)
  187. return ERR_PTR(-ENOMEM);
  188. if (!key->sig) {
  189. sfi_print_table_header(pa, th);
  190. if (sfi_verify_table(th))
  191. ret = ERR_PTR(-EINVAL);
  192. } else {
  193. if (!sfi_table_check_key(th, key))
  194. return th; /* Success */
  195. }
  196. sfi_unmap_table(th);
  197. return ret;
  198. }
  199. /*
  200. * sfi_get_table()
  201. *
  202. * Search SYST for the specified table with the signature in
  203. * the key, and return the mapped table
  204. */
  205. struct sfi_table_header *sfi_get_table(struct sfi_table_key *key)
  206. {
  207. struct sfi_table_header *th;
  208. u32 tbl_cnt, i;
  209. tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
  210. for (i = 0; i < tbl_cnt; i++) {
  211. th = sfi_check_table(syst_va->pentry[i], key);
  212. if (!IS_ERR(th) && th)
  213. return th;
  214. }
  215. return NULL;
  216. }
  217. void sfi_put_table(struct sfi_table_header *th)
  218. {
  219. sfi_unmap_table(th);
  220. }
  221. /* Find table with signature, run handler on it */
  222. int sfi_table_parse(char *signature, char *oem_id, char *oem_table_id,
  223. sfi_table_handler handler)
  224. {
  225. struct sfi_table_header *table = NULL;
  226. struct sfi_table_key key;
  227. int ret = -EINVAL;
  228. if (sfi_disabled || !handler || !signature)
  229. goto exit;
  230. key.sig = signature;
  231. key.oem_id = oem_id;
  232. key.oem_table_id = oem_table_id;
  233. table = sfi_get_table(&key);
  234. if (!table)
  235. goto exit;
  236. ret = handler(table);
  237. sfi_put_table(table);
  238. exit:
  239. return ret;
  240. }
  241. EXPORT_SYMBOL_GPL(sfi_table_parse);
  242. /*
  243. * sfi_parse_syst()
  244. * Checksum all the tables in SYST and print their headers
  245. *
  246. * success: set syst_va, return 0
  247. */
  248. static int __init sfi_parse_syst(void)
  249. {
  250. struct sfi_table_key key = SFI_ANY_KEY;
  251. int tbl_cnt, i;
  252. void *ret;
  253. syst_va = sfi_map_memory(syst_pa, sizeof(struct sfi_table_simple));
  254. if (!syst_va)
  255. return -ENOMEM;
  256. tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
  257. for (i = 0; i < tbl_cnt; i++) {
  258. ret = sfi_check_table(syst_va->pentry[i], &key);
  259. if (IS_ERR(ret))
  260. return PTR_ERR(ret);
  261. }
  262. return 0;
  263. }
  264. /*
  265. * The OS finds the System Table by searching 16-byte boundaries between
  266. * physical address 0x000E0000 and 0x000FFFFF. The OS shall search this region
  267. * starting at the low address and shall stop searching when the 1st valid SFI
  268. * System Table is found.
  269. *
  270. * success: set syst_pa, return 0
  271. * fail: return -1
  272. */
  273. static __init int sfi_find_syst(void)
  274. {
  275. unsigned long offset, len;
  276. void *start;
  277. len = SFI_SYST_SEARCH_END - SFI_SYST_SEARCH_BEGIN;
  278. start = sfi_map_memory(SFI_SYST_SEARCH_BEGIN, len);
  279. if (!start)
  280. return -1;
  281. for (offset = 0; offset < len; offset += 16) {
  282. struct sfi_table_header *syst_hdr;
  283. syst_hdr = start + offset;
  284. if (strncmp(syst_hdr->sig, SFI_SIG_SYST,
  285. SFI_SIGNATURE_SIZE))
  286. continue;
  287. if (syst_hdr->len > PAGE_SIZE)
  288. continue;
  289. sfi_print_table_header(SFI_SYST_SEARCH_BEGIN + offset,
  290. syst_hdr);
  291. if (sfi_verify_table(syst_hdr))
  292. continue;
  293. /*
  294. * Enforce SFI spec mandate that SYST reside within a page.
  295. */
  296. if (!ON_SAME_PAGE(syst_pa, syst_pa + syst_hdr->len)) {
  297. pr_info("SYST 0x%llx + 0x%x crosses page\n",
  298. syst_pa, syst_hdr->len);
  299. continue;
  300. }
  301. /* Success */
  302. syst_pa = SFI_SYST_SEARCH_BEGIN + offset;
  303. sfi_unmap_memory(start, len);
  304. return 0;
  305. }
  306. sfi_unmap_memory(start, len);
  307. return -1;
  308. }
  309. void __init sfi_init(void)
  310. {
  311. if (!acpi_disabled)
  312. disable_sfi();
  313. if (sfi_disabled)
  314. return;
  315. pr_info("Simple Firmware Interface v0.7 http://simplefirmware.org\n");
  316. if (sfi_find_syst() || sfi_parse_syst() || sfi_platform_init())
  317. disable_sfi();
  318. return;
  319. }
  320. void __init sfi_init_late(void)
  321. {
  322. int length;
  323. if (sfi_disabled)
  324. return;
  325. length = syst_va->header.len;
  326. sfi_unmap_memory(syst_va, sizeof(struct sfi_table_simple));
  327. /* Use ioremap now after it is ready */
  328. sfi_use_ioremap = 1;
  329. syst_va = sfi_map_memory(syst_pa, length);
  330. sfi_acpi_init();
  331. }