blob: 7bd6b18fdd2731f27a8eccf7eb9ead6198b0ef19 [file] [log] [blame]
Feng Tang6ae69962009-08-14 15:13:46 -04001/* sfi_core.c Simple Firmware Interface - core internals */
2
3/*
4
5 This file is provided under a dual BSD/GPLv2 license. When using or
6 redistributing this file, you may do so under either license.
7
8 GPL LICENSE SUMMARY
9
10 Copyright(c) 2009 Intel Corporation. All rights reserved.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of version 2 of the GNU General Public License as
14 published by the Free Software Foundation.
15
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24 The full GNU General Public License is included in this distribution
25 in the file called LICENSE.GPL.
26
27 BSD LICENSE
28
29 Copyright(c) 2009 Intel Corporation. All rights reserved.
30
31 Redistribution and use in source and binary forms, with or without
32 modification, are permitted provided that the following conditions
33 are met:
34
35 * Redistributions of source code must retain the above copyright
36 notice, this list of conditions and the following disclaimer.
37 * Redistributions in binary form must reproduce the above copyright
38 notice, this list of conditions and the following disclaimer in
39 the documentation and/or other materials provided with the
40 distribution.
41 * Neither the name of Intel Corporation nor the names of its
42 contributors may be used to endorse or promote products derived
43 from this software without specific prior written permission.
44
45 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
49 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56
57*/
58
59#define KMSG_COMPONENT "SFI"
60#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
61
62#include <linux/bootmem.h>
63#include <linux/kernel.h>
64#include <linux/module.h>
65#include <linux/string.h>
66#include <linux/errno.h>
67#include <linux/types.h>
68#include <linux/acpi.h>
69#include <linux/init.h>
70#include <linux/irq.h>
71#include <linux/sfi.h>
72#include <linux/smp.h>
73
74#include <asm/pgtable.h>
75
76#include "sfi_core.h"
77
78#define ON_SAME_PAGE(addr1, addr2) \
79 (((unsigned long)(addr1) & PAGE_MASK) == \
80 ((unsigned long)(addr2) & PAGE_MASK))
81#define TABLE_ON_PAGE(page, table, size) (ON_SAME_PAGE(page, table) && \
82 ON_SAME_PAGE(page, table + size))
83
84int sfi_disabled __read_mostly;
85EXPORT_SYMBOL(sfi_disabled);
86
87static u64 syst_pa __read_mostly;
88static struct sfi_table_simple *syst_va __read_mostly;
89
90/*
91 * FW creates and saves the SFI tables in memory. When these tables get
92 * used, they may need to be mapped to virtual address space, and the mapping
93 * can happen before or after the ioremap() is ready, so a flag is needed
94 * to indicating this
95 */
96static u32 sfi_use_ioremap __read_mostly;
97
98static void __iomem *sfi_map_memory(u64 phys, u32 size)
99{
100 if (!phys || !size)
101 return NULL;
102
103 if (sfi_use_ioremap)
104 return ioremap(phys, size);
105 else
106 return early_ioremap(phys, size);
107}
108
109static void sfi_unmap_memory(void __iomem *virt, u32 size)
110{
111 if (!virt || !size)
112 return;
113
114 if (sfi_use_ioremap)
115 iounmap(virt);
116 else
117 early_iounmap(virt, size);
118}
119
120static void sfi_print_table_header(unsigned long long pa,
121 struct sfi_table_header *header)
122{
123 pr_info("%4.4s %llX, %04X (v%d %6.6s %8.8s)\n",
124 header->sig, pa,
125 header->len, header->rev, header->oem_id,
126 header->oem_table_id);
127}
128
129/*
130 * sfi_verify_table()
131 * Sanity check table lengh, calculate checksum
132 */
133static __init int sfi_verify_table(struct sfi_table_header *table)
134{
135
136 u8 checksum = 0;
137 u8 *puchar = (u8 *)table;
138 u32 length = table->len;
139
140 /* Sanity check table length against arbitrary 1MB limit */
141 if (length > 0x100000) {
142 pr_err("Invalid table length 0x%x\n", length);
143 return -1;
144 }
145
146 while (length--)
147 checksum += *puchar++;
148
149 if (checksum) {
150 pr_err("Checksum %2.2X should be %2.2X\n",
151 table->csum, table->csum - checksum);
152 return -1;
153 }
154 return 0;
155}
156
157/*
158 * sfi_map_table()
159 *
160 * Return address of mapped table
161 * Check for common case that we can re-use mapping to SYST,
162 * which requires syst_pa, syst_va to be initialized.
163 */
164struct sfi_table_header *sfi_map_table(u64 pa)
165{
166 struct sfi_table_header *th;
167 u32 length;
168
169 if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
170 th = sfi_map_memory(pa, sizeof(struct sfi_table_header));
171 else
172 th = (void *)syst_va + (pa - syst_pa);
173
174 /* If table fits on same page as its header, we are done */
175 if (TABLE_ON_PAGE(th, th, th->len))
176 return th;
177
178 /* Entire table does not fit on same page as SYST */
179 length = th->len;
180 if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
181 sfi_unmap_memory(th, sizeof(struct sfi_table_header));
182
183 return sfi_map_memory(pa, length);
184}
185
186/*
187 * sfi_unmap_table()
188 *
189 * Undoes effect of sfi_map_table() by unmapping table
190 * if it did not completely fit on same page as SYST.
191 */
192void sfi_unmap_table(struct sfi_table_header *th)
193{
194 if (!TABLE_ON_PAGE(syst_va, th, th->len))
195 sfi_unmap_memory(th, TABLE_ON_PAGE(th, th, th->len) ?
196 sizeof(*th) : th->len);
197}
198
199static int sfi_table_check_key(struct sfi_table_header *th,
200 struct sfi_table_key *key)
201{
202
203 if (strncmp(th->sig, key->sig, SFI_SIGNATURE_SIZE)
204 || (key->oem_id && strncmp(th->oem_id,
205 key->oem_id, SFI_OEM_ID_SIZE))
206 || (key->oem_table_id && strncmp(th->oem_table_id,
207 key->oem_table_id, SFI_OEM_TABLE_ID_SIZE)))
208 return -1;
209
210 return 0;
211}
212
213/*
214 * This function will be used in 2 cases:
215 * 1. used to enumerate and verify the tables addressed by SYST/XSDT,
216 * thus no signature will be given (in kernel boot phase)
217 * 2. used to parse one specific table, signature must exist, and
218 * the mapped virt address will be returned, and the virt space
219 * will be released by call sfi_put_table() later
220 *
221 * Return value:
222 * NULL: when can't find a table matching the key
223 * ERR_PTR(error): error value
224 * virt table address: when a matched table is found
225 */
226struct sfi_table_header *sfi_check_table(u64 pa, struct sfi_table_key *key)
227{
228 struct sfi_table_header *th;
229 void *ret = NULL;
230
231 th = sfi_map_table(pa);
232 if (!th)
233 return ERR_PTR(-ENOMEM);
234
235 if (!key->sig) {
236 sfi_print_table_header(pa, th);
237 if (sfi_verify_table(th))
238 ret = ERR_PTR(-EINVAL);
239 } else {
240 if (!sfi_table_check_key(th, key))
241 return th; /* Success */
242 }
243
244 sfi_unmap_table(th);
245 return ret;
246}
247
248/*
249 * sfi_get_table()
250 *
251 * Search SYST for the specified table with the signature in
252 * the key, and return the mapped table
253 */
254struct sfi_table_header *sfi_get_table(struct sfi_table_key *key)
255{
256 struct sfi_table_header *th;
257 u32 tbl_cnt, i;
258
259 tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
260 for (i = 0; i < tbl_cnt; i++) {
261 th = sfi_check_table(syst_va->pentry[i], key);
262 if (!IS_ERR(th) && th)
263 return th;
264 }
265
266 return NULL;
267}
268
269void sfi_put_table(struct sfi_table_header *th)
270{
271 sfi_unmap_table(th);
272}
273
274/* Find table with signature, run handler on it */
275int sfi_table_parse(char *signature, char *oem_id, char *oem_table_id,
276 sfi_table_handler handler)
277{
278 struct sfi_table_header *table = NULL;
279 struct sfi_table_key key;
280 int ret = -EINVAL;
281
282 if (sfi_disabled || !handler || !signature)
283 goto exit;
284
285 key.sig = signature;
286 key.oem_id = oem_id;
287 key.oem_table_id = oem_table_id;
288
289 table = sfi_get_table(&key);
290 if (!table)
291 goto exit;
292
293 ret = handler(table);
294 sfi_put_table(table);
295exit:
296 return ret;
297}
298EXPORT_SYMBOL_GPL(sfi_table_parse);
299
300/*
301 * sfi_parse_syst()
302 * Checksum all the tables in SYST and print their headers
303 *
304 * success: set syst_va, return 0
305 */
306static int __init sfi_parse_syst(void)
307{
308 struct sfi_table_key key = SFI_ANY_KEY;
309 int tbl_cnt, i;
310 void *ret;
311
312 syst_va = sfi_map_memory(syst_pa, sizeof(struct sfi_table_simple));
313 if (!syst_va)
314 return -ENOMEM;
315
316 tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
317 for (i = 0; i < tbl_cnt; i++) {
318 ret = sfi_check_table(syst_va->pentry[i], &key);
319 if (IS_ERR(ret))
320 return PTR_ERR(ret);
321 }
322
323 return 0;
324}
325
326/*
327 * The OS finds the System Table by searching 16-byte boundaries between
328 * physical address 0x000E0000 and 0x000FFFFF. The OS shall search this region
329 * starting at the low address and shall stop searching when the 1st valid SFI
330 * System Table is found.
331 *
332 * success: set syst_pa, return 0
333 * fail: return -1
334 */
335static __init int sfi_find_syst(void)
336{
337 unsigned long offset, len;
338 void *start;
339
340 len = SFI_SYST_SEARCH_END - SFI_SYST_SEARCH_BEGIN;
341 start = sfi_map_memory(SFI_SYST_SEARCH_BEGIN, len);
342 if (!start)
343 return -1;
344
345 for (offset = 0; offset < len; offset += 16) {
346 struct sfi_table_header *syst_hdr;
347
348 syst_hdr = start + offset;
349 if (strncmp(syst_hdr->sig, SFI_SIG_SYST,
350 SFI_SIGNATURE_SIZE))
351 continue;
352
353 if (syst_hdr->len > PAGE_SIZE)
354 continue;
355
356 sfi_print_table_header(SFI_SYST_SEARCH_BEGIN + offset,
357 syst_hdr);
358
359 if (sfi_verify_table(syst_hdr))
360 continue;
361
362 /*
363 * Enforce SFI spec mandate that SYST reside within a page.
364 */
365 if (!ON_SAME_PAGE(syst_pa, syst_pa + syst_hdr->len)) {
366 pr_info("SYST 0x%llx + 0x%x crosses page\n",
367 syst_pa, syst_hdr->len);
368 continue;
369 }
370
371 /* Success */
372 syst_pa = SFI_SYST_SEARCH_BEGIN + offset;
373 sfi_unmap_memory(start, len);
374 return 0;
375 }
376
377 sfi_unmap_memory(start, len);
378 return -1;
379}
380
381void __init sfi_init(void)
382{
383 if (!acpi_disabled)
384 disable_sfi();
385
386 if (sfi_disabled)
387 return;
388
389 pr_info("Simple Firmware Interface v0.7 http://simplefirmware.org\n");
390
391 if (sfi_find_syst() || sfi_parse_syst() || sfi_platform_init())
392 disable_sfi();
393
394 return;
395}
396
397void __init sfi_init_late(void)
398{
399 int length;
400
401 if (sfi_disabled)
402 return;
403
404 length = syst_va->header.len;
405 sfi_unmap_memory(syst_va, sizeof(struct sfi_table_simple));
406
407 /* Use ioremap now after it is ready */
408 sfi_use_ioremap = 1;
409 syst_va = sfi_map_memory(syst_pa, length);
410
411 sfi_acpi_init();
412}