sfi_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 <linux/slab.h>
  56. #include "sfi_core.h"
  57. #define ON_SAME_PAGE(addr1, addr2) \
  58. (((unsigned long)(addr1) & PAGE_MASK) == \
  59. ((unsigned long)(addr2) & PAGE_MASK))
  60. #define TABLE_ON_PAGE(page, table, size) (ON_SAME_PAGE(page, table) && \
  61. ON_SAME_PAGE(page, table + size))
  62. int sfi_disabled __read_mostly;
  63. EXPORT_SYMBOL(sfi_disabled);
  64. static u64 syst_pa __read_mostly;
  65. static struct sfi_table_simple *syst_va __read_mostly;
  66. /*
  67. * FW creates and saves the SFI tables in memory. When these tables get
  68. * used, they may need to be mapped to virtual address space, and the mapping
  69. * can happen before or after the ioremap() is ready, so a flag is needed
  70. * to indicating this
  71. */
  72. static u32 sfi_use_ioremap __read_mostly;
  73. /*
  74. * sfi_un/map_memory calls early_ioremap/iounmap which is a __init function
  75. * and introduces section mismatch. So use __ref to make it calm.
  76. */
  77. static void __iomem * __ref sfi_map_memory(u64 phys, u32 size)
  78. {
  79. if (!phys || !size)
  80. return NULL;
  81. if (sfi_use_ioremap)
  82. return ioremap_cache(phys, size);
  83. else
  84. return early_ioremap(phys, size);
  85. }
  86. static void __ref sfi_unmap_memory(void __iomem *virt, u32 size)
  87. {
  88. if (!virt || !size)
  89. return;
  90. if (sfi_use_ioremap)
  91. iounmap(virt);
  92. else
  93. early_iounmap(virt, size);
  94. }
  95. static void sfi_print_table_header(unsigned long long pa,
  96. struct sfi_table_header *header)
  97. {
  98. pr_info("%4.4s %llX, %04X (v%d %6.6s %8.8s)\n",
  99. header->sig, pa,
  100. header->len, header->rev, header->oem_id,
  101. header->oem_table_id);
  102. }
  103. /*
  104. * sfi_verify_table()
  105. * Sanity check table lengh, calculate checksum
  106. */
  107. static int sfi_verify_table(struct sfi_table_header *table)
  108. {
  109. u8 checksum = 0;
  110. u8 *puchar = (u8 *)table;
  111. u32 length = table->len;
  112. /* Sanity check table length against arbitrary 1MB limit */
  113. if (length > 0x100000) {
  114. pr_err("Invalid table length 0x%x\n", length);
  115. return -1;
  116. }
  117. while (length--)
  118. checksum += *puchar++;
  119. if (checksum) {
  120. pr_err("Checksum %2.2X should be %2.2X\n",
  121. table->csum, table->csum - checksum);
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * sfi_map_table()
  128. *
  129. * Return address of mapped table
  130. * Check for common case that we can re-use mapping to SYST,
  131. * which requires syst_pa, syst_va to be initialized.
  132. */
  133. static struct sfi_table_header *sfi_map_table(u64 pa)
  134. {
  135. struct sfi_table_header *th;
  136. u32 length;
  137. if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
  138. th = sfi_map_memory(pa, sizeof(struct sfi_table_header));
  139. else
  140. th = (void *)syst_va + (pa - syst_pa);
  141. /* If table fits on same page as its header, we are done */
  142. if (TABLE_ON_PAGE(th, th, th->len))
  143. return th;
  144. /* Entire table does not fit on same page as SYST */
  145. length = th->len;
  146. if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
  147. sfi_unmap_memory(th, sizeof(struct sfi_table_header));
  148. return sfi_map_memory(pa, length);
  149. }
  150. /*
  151. * sfi_unmap_table()
  152. *
  153. * Undoes effect of sfi_map_table() by unmapping table
  154. * if it did not completely fit on same page as SYST.
  155. */
  156. static void sfi_unmap_table(struct sfi_table_header *th)
  157. {
  158. if (!TABLE_ON_PAGE(syst_va, th, th->len))
  159. sfi_unmap_memory(th, TABLE_ON_PAGE(th, th, th->len) ?
  160. sizeof(*th) : th->len);
  161. }
  162. static int sfi_table_check_key(struct sfi_table_header *th,
  163. struct sfi_table_key *key)
  164. {
  165. if (strncmp(th->sig, key->sig, SFI_SIGNATURE_SIZE)
  166. || (key->oem_id && strncmp(th->oem_id,
  167. key->oem_id, SFI_OEM_ID_SIZE))
  168. || (key->oem_table_id && strncmp(th->oem_table_id,
  169. key->oem_table_id, SFI_OEM_TABLE_ID_SIZE)))
  170. return -1;
  171. return 0;
  172. }
  173. /*
  174. * This function will be used in 2 cases:
  175. * 1. used to enumerate and verify the tables addressed by SYST/XSDT,
  176. * thus no signature will be given (in kernel boot phase)
  177. * 2. used to parse one specific table, signature must exist, and
  178. * the mapped virt address will be returned, and the virt space
  179. * will be released by call sfi_put_table() later
  180. *
  181. * This two cases are from two different functions with two different
  182. * sections and causes section mismatch warning. So use __ref to tell
  183. * modpost not to make any noise.
  184. *
  185. * Return value:
  186. * NULL: when can't find a table matching the key
  187. * ERR_PTR(error): error value
  188. * virt table address: when a matched table is found
  189. */
  190. struct sfi_table_header *
  191. __ref sfi_check_table(u64 pa, struct sfi_table_key *key)
  192. {
  193. struct sfi_table_header *th;
  194. void *ret = NULL;
  195. th = sfi_map_table(pa);
  196. if (!th)
  197. return ERR_PTR(-ENOMEM);
  198. if (!key->sig) {
  199. sfi_print_table_header(pa, th);
  200. if (sfi_verify_table(th))
  201. ret = ERR_PTR(-EINVAL);
  202. } else {
  203. if (!sfi_table_check_key(th, key))
  204. return th; /* Success */
  205. }
  206. sfi_unmap_table(th);
  207. return ret;
  208. }
  209. /*
  210. * sfi_get_table()
  211. *
  212. * Search SYST for the specified table with the signature in
  213. * the key, and return the mapped table
  214. */
  215. struct sfi_table_header *sfi_get_table(struct sfi_table_key *key)
  216. {
  217. struct sfi_table_header *th;
  218. u32 tbl_cnt, i;
  219. tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
  220. for (i = 0; i < tbl_cnt; i++) {
  221. th = sfi_check_table(syst_va->pentry[i], key);
  222. if (!IS_ERR(th) && th)
  223. return th;
  224. }
  225. return NULL;
  226. }
  227. void sfi_put_table(struct sfi_table_header *th)
  228. {
  229. sfi_unmap_table(th);
  230. }
  231. /* Find table with signature, run handler on it */
  232. int sfi_table_parse(char *signature, char *oem_id, char *oem_table_id,
  233. sfi_table_handler handler)
  234. {
  235. struct sfi_table_header *table = NULL;
  236. struct sfi_table_key key;
  237. int ret = -EINVAL;
  238. if (sfi_disabled || !handler || !signature)
  239. goto exit;
  240. key.sig = signature;
  241. key.oem_id = oem_id;
  242. key.oem_table_id = oem_table_id;
  243. table = sfi_get_table(&key);
  244. if (!table)
  245. goto exit;
  246. ret = handler(table);
  247. sfi_put_table(table);
  248. exit:
  249. return ret;
  250. }
  251. EXPORT_SYMBOL_GPL(sfi_table_parse);
  252. /*
  253. * sfi_parse_syst()
  254. * Checksum all the tables in SYST and print their headers
  255. *
  256. * success: set syst_va, return 0
  257. */
  258. static int __init sfi_parse_syst(void)
  259. {
  260. struct sfi_table_key key = SFI_ANY_KEY;
  261. int tbl_cnt, i;
  262. void *ret;
  263. syst_va = sfi_map_memory(syst_pa, sizeof(struct sfi_table_simple));
  264. if (!syst_va)
  265. return -ENOMEM;
  266. tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
  267. for (i = 0; i < tbl_cnt; i++) {
  268. ret = sfi_check_table(syst_va->pentry[i], &key);
  269. if (IS_ERR(ret))
  270. return PTR_ERR(ret);
  271. }
  272. return 0;
  273. }
  274. /*
  275. * The OS finds the System Table by searching 16-byte boundaries between
  276. * physical address 0x000E0000 and 0x000FFFFF. The OS shall search this region
  277. * starting at the low address and shall stop searching when the 1st valid SFI
  278. * System Table is found.
  279. *
  280. * success: set syst_pa, return 0
  281. * fail: return -1
  282. */
  283. static __init int sfi_find_syst(void)
  284. {
  285. unsigned long offset, len;
  286. void *start;
  287. len = SFI_SYST_SEARCH_END - SFI_SYST_SEARCH_BEGIN;
  288. start = sfi_map_memory(SFI_SYST_SEARCH_BEGIN, len);
  289. if (!start)
  290. return -1;
  291. for (offset = 0; offset < len; offset += 16) {
  292. struct sfi_table_header *syst_hdr;
  293. syst_hdr = start + offset;
  294. if (strncmp(syst_hdr->sig, SFI_SIG_SYST,
  295. SFI_SIGNATURE_SIZE))
  296. continue;
  297. if (syst_hdr->len > PAGE_SIZE)
  298. continue;
  299. sfi_print_table_header(SFI_SYST_SEARCH_BEGIN + offset,
  300. syst_hdr);
  301. if (sfi_verify_table(syst_hdr))
  302. continue;
  303. /*
  304. * Enforce SFI spec mandate that SYST reside within a page.
  305. */
  306. if (!ON_SAME_PAGE(syst_pa, syst_pa + syst_hdr->len)) {
  307. pr_info("SYST 0x%llx + 0x%x crosses page\n",
  308. syst_pa, syst_hdr->len);
  309. continue;
  310. }
  311. /* Success */
  312. syst_pa = SFI_SYST_SEARCH_BEGIN + offset;
  313. sfi_unmap_memory(start, len);
  314. return 0;
  315. }
  316. sfi_unmap_memory(start, len);
  317. return -1;
  318. }
  319. static struct kobject *sfi_kobj;
  320. static struct kobject *tables_kobj;
  321. static ssize_t sfi_table_show(struct file *filp, struct kobject *kobj,
  322. struct bin_attribute *bin_attr, char *buf,
  323. loff_t offset, size_t count)
  324. {
  325. struct sfi_table_attr *tbl_attr =
  326. container_of(bin_attr, struct sfi_table_attr, attr);
  327. struct sfi_table_header *th = NULL;
  328. struct sfi_table_key key;
  329. ssize_t cnt;
  330. key.sig = tbl_attr->name;
  331. key.oem_id = NULL;
  332. key.oem_table_id = NULL;
  333. if (strncmp(SFI_SIG_SYST, tbl_attr->name, SFI_SIGNATURE_SIZE)) {
  334. th = sfi_get_table(&key);
  335. if (!th)
  336. return 0;
  337. cnt = memory_read_from_buffer(buf, count, &offset,
  338. th, th->len);
  339. sfi_put_table(th);
  340. } else
  341. cnt = memory_read_from_buffer(buf, count, &offset,
  342. syst_va, syst_va->header.len);
  343. return cnt;
  344. }
  345. struct sfi_table_attr __init *sfi_sysfs_install_table(u64 pa)
  346. {
  347. struct sfi_table_attr *tbl_attr;
  348. struct sfi_table_header *th;
  349. int ret;
  350. tbl_attr = kzalloc(sizeof(struct sfi_table_attr), GFP_KERNEL);
  351. if (!tbl_attr)
  352. return NULL;
  353. th = sfi_map_table(pa);
  354. if (!th || !th->sig[0]) {
  355. kfree(tbl_attr);
  356. return NULL;
  357. }
  358. sysfs_attr_init(&tbl_attr->attr.attr);
  359. memcpy(tbl_attr->name, th->sig, SFI_SIGNATURE_SIZE);
  360. tbl_attr->attr.size = 0;
  361. tbl_attr->attr.read = sfi_table_show;
  362. tbl_attr->attr.attr.name = tbl_attr->name;
  363. tbl_attr->attr.attr.mode = 0400;
  364. ret = sysfs_create_bin_file(tables_kobj,
  365. &tbl_attr->attr);
  366. if (ret) {
  367. kfree(tbl_attr);
  368. tbl_attr = NULL;
  369. }
  370. sfi_unmap_table(th);
  371. return tbl_attr;
  372. }
  373. static int __init sfi_sysfs_init(void)
  374. {
  375. int tbl_cnt, i;
  376. if (sfi_disabled)
  377. return 0;
  378. sfi_kobj = kobject_create_and_add("sfi", firmware_kobj);
  379. if (!sfi_kobj)
  380. return 0;
  381. tables_kobj = kobject_create_and_add("tables", sfi_kobj);
  382. if (!tables_kobj) {
  383. kobject_put(sfi_kobj);
  384. return 0;
  385. }
  386. sfi_sysfs_install_table(syst_pa);
  387. tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
  388. for (i = 0; i < tbl_cnt; i++)
  389. sfi_sysfs_install_table(syst_va->pentry[i]);
  390. sfi_acpi_sysfs_init();
  391. kobject_uevent(sfi_kobj, KOBJ_ADD);
  392. kobject_uevent(tables_kobj, KOBJ_ADD);
  393. pr_info("SFI sysfs interfaces init success\n");
  394. return 0;
  395. }
  396. void __init sfi_init(void)
  397. {
  398. if (!acpi_disabled)
  399. disable_sfi();
  400. if (sfi_disabled)
  401. return;
  402. pr_info("Simple Firmware Interface v0.81 http://simplefirmware.org\n");
  403. if (sfi_find_syst() || sfi_parse_syst() || sfi_platform_init())
  404. disable_sfi();
  405. return;
  406. }
  407. void __init sfi_init_late(void)
  408. {
  409. int length;
  410. if (sfi_disabled)
  411. return;
  412. length = syst_va->header.len;
  413. sfi_unmap_memory(syst_va, sizeof(struct sfi_table_simple));
  414. /* Use ioremap now after it is ready */
  415. sfi_use_ioremap = 1;
  416. syst_va = sfi_map_memory(syst_pa, length);
  417. sfi_acpi_init();
  418. }
  419. /*
  420. * The reason we put it here because we need wait till the /sys/firmware
  421. * is setup, then our interface can be registered in /sys/firmware/sfi
  422. */
  423. core_initcall(sfi_sysfs_init);