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