be2net: fix promiscuous and multicast promiscuous modes being enabled always
[linux-2.6/mini2440.git] / drivers / sfi / sfi_core.c
blobd3b496800477b065b42b1b92a0a0b201e5503b78
1 /* sfi_core.c Simple Firmware Interface - core internals */
3 /*
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.
8 GPL LICENSE SUMMARY
10 Copyright(c) 2009 Intel Corporation. All rights reserved.
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.
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.
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.
27 BSD LICENSE
29 Copyright(c) 2009 Intel Corporation. All rights reserved.
31 Redistribution and use in source and binary forms, with or without
32 modification, are permitted provided that the following conditions
33 are met:
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.
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.
59 #define KMSG_COMPONENT "SFI"
60 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
62 #include <linux/bootmem.h>
63 #include <linux/kernel.h>
64 #include <linux/module.h>
65 #include <linux/errno.h>
66 #include <linux/types.h>
67 #include <linux/acpi.h>
68 #include <linux/init.h>
69 #include <linux/sfi.h>
71 #include "sfi_core.h"
73 #define ON_SAME_PAGE(addr1, addr2) \
74 (((unsigned long)(addr1) & PAGE_MASK) == \
75 ((unsigned long)(addr2) & PAGE_MASK))
76 #define TABLE_ON_PAGE(page, table, size) (ON_SAME_PAGE(page, table) && \
77 ON_SAME_PAGE(page, table + size))
79 int sfi_disabled __read_mostly;
80 EXPORT_SYMBOL(sfi_disabled);
82 static u64 syst_pa __read_mostly;
83 static struct sfi_table_simple *syst_va __read_mostly;
86 * FW creates and saves the SFI tables in memory. When these tables get
87 * used, they may need to be mapped to virtual address space, and the mapping
88 * can happen before or after the ioremap() is ready, so a flag is needed
89 * to indicating this
91 static u32 sfi_use_ioremap __read_mostly;
93 static void __iomem *sfi_map_memory(u64 phys, u32 size)
95 if (!phys || !size)
96 return NULL;
98 if (sfi_use_ioremap)
99 return ioremap(phys, size);
100 else
101 return early_ioremap(phys, size);
104 static void sfi_unmap_memory(void __iomem *virt, u32 size)
106 if (!virt || !size)
107 return;
109 if (sfi_use_ioremap)
110 iounmap(virt);
111 else
112 early_iounmap(virt, size);
115 static void sfi_print_table_header(unsigned long long pa,
116 struct sfi_table_header *header)
118 pr_info("%4.4s %llX, %04X (v%d %6.6s %8.8s)\n",
119 header->sig, pa,
120 header->len, header->rev, header->oem_id,
121 header->oem_table_id);
125 * sfi_verify_table()
126 * Sanity check table lengh, calculate checksum
128 static __init int sfi_verify_table(struct sfi_table_header *table)
131 u8 checksum = 0;
132 u8 *puchar = (u8 *)table;
133 u32 length = table->len;
135 /* Sanity check table length against arbitrary 1MB limit */
136 if (length > 0x100000) {
137 pr_err("Invalid table length 0x%x\n", length);
138 return -1;
141 while (length--)
142 checksum += *puchar++;
144 if (checksum) {
145 pr_err("Checksum %2.2X should be %2.2X\n",
146 table->csum, table->csum - checksum);
147 return -1;
149 return 0;
153 * sfi_map_table()
155 * Return address of mapped table
156 * Check for common case that we can re-use mapping to SYST,
157 * which requires syst_pa, syst_va to be initialized.
159 struct sfi_table_header *sfi_map_table(u64 pa)
161 struct sfi_table_header *th;
162 u32 length;
164 if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
165 th = sfi_map_memory(pa, sizeof(struct sfi_table_header));
166 else
167 th = (void *)syst_va + (pa - syst_pa);
169 /* If table fits on same page as its header, we are done */
170 if (TABLE_ON_PAGE(th, th, th->len))
171 return th;
173 /* Entire table does not fit on same page as SYST */
174 length = th->len;
175 if (!TABLE_ON_PAGE(syst_pa, pa, sizeof(struct sfi_table_header)))
176 sfi_unmap_memory(th, sizeof(struct sfi_table_header));
178 return sfi_map_memory(pa, length);
182 * sfi_unmap_table()
184 * Undoes effect of sfi_map_table() by unmapping table
185 * if it did not completely fit on same page as SYST.
187 void sfi_unmap_table(struct sfi_table_header *th)
189 if (!TABLE_ON_PAGE(syst_va, th, th->len))
190 sfi_unmap_memory(th, TABLE_ON_PAGE(th, th, th->len) ?
191 sizeof(*th) : th->len);
194 static int sfi_table_check_key(struct sfi_table_header *th,
195 struct sfi_table_key *key)
198 if (strncmp(th->sig, key->sig, SFI_SIGNATURE_SIZE)
199 || (key->oem_id && strncmp(th->oem_id,
200 key->oem_id, SFI_OEM_ID_SIZE))
201 || (key->oem_table_id && strncmp(th->oem_table_id,
202 key->oem_table_id, SFI_OEM_TABLE_ID_SIZE)))
203 return -1;
205 return 0;
209 * This function will be used in 2 cases:
210 * 1. used to enumerate and verify the tables addressed by SYST/XSDT,
211 * thus no signature will be given (in kernel boot phase)
212 * 2. used to parse one specific table, signature must exist, and
213 * the mapped virt address will be returned, and the virt space
214 * will be released by call sfi_put_table() later
216 * Return value:
217 * NULL: when can't find a table matching the key
218 * ERR_PTR(error): error value
219 * virt table address: when a matched table is found
221 struct sfi_table_header *sfi_check_table(u64 pa, struct sfi_table_key *key)
223 struct sfi_table_header *th;
224 void *ret = NULL;
226 th = sfi_map_table(pa);
227 if (!th)
228 return ERR_PTR(-ENOMEM);
230 if (!key->sig) {
231 sfi_print_table_header(pa, th);
232 if (sfi_verify_table(th))
233 ret = ERR_PTR(-EINVAL);
234 } else {
235 if (!sfi_table_check_key(th, key))
236 return th; /* Success */
239 sfi_unmap_table(th);
240 return ret;
244 * sfi_get_table()
246 * Search SYST for the specified table with the signature in
247 * the key, and return the mapped table
249 struct sfi_table_header *sfi_get_table(struct sfi_table_key *key)
251 struct sfi_table_header *th;
252 u32 tbl_cnt, i;
254 tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
255 for (i = 0; i < tbl_cnt; i++) {
256 th = sfi_check_table(syst_va->pentry[i], key);
257 if (!IS_ERR(th) && th)
258 return th;
261 return NULL;
264 void sfi_put_table(struct sfi_table_header *th)
266 sfi_unmap_table(th);
269 /* Find table with signature, run handler on it */
270 int sfi_table_parse(char *signature, char *oem_id, char *oem_table_id,
271 sfi_table_handler handler)
273 struct sfi_table_header *table = NULL;
274 struct sfi_table_key key;
275 int ret = -EINVAL;
277 if (sfi_disabled || !handler || !signature)
278 goto exit;
280 key.sig = signature;
281 key.oem_id = oem_id;
282 key.oem_table_id = oem_table_id;
284 table = sfi_get_table(&key);
285 if (!table)
286 goto exit;
288 ret = handler(table);
289 sfi_put_table(table);
290 exit:
291 return ret;
293 EXPORT_SYMBOL_GPL(sfi_table_parse);
296 * sfi_parse_syst()
297 * Checksum all the tables in SYST and print their headers
299 * success: set syst_va, return 0
301 static int __init sfi_parse_syst(void)
303 struct sfi_table_key key = SFI_ANY_KEY;
304 int tbl_cnt, i;
305 void *ret;
307 syst_va = sfi_map_memory(syst_pa, sizeof(struct sfi_table_simple));
308 if (!syst_va)
309 return -ENOMEM;
311 tbl_cnt = SFI_GET_NUM_ENTRIES(syst_va, u64);
312 for (i = 0; i < tbl_cnt; i++) {
313 ret = sfi_check_table(syst_va->pentry[i], &key);
314 if (IS_ERR(ret))
315 return PTR_ERR(ret);
318 return 0;
322 * The OS finds the System Table by searching 16-byte boundaries between
323 * physical address 0x000E0000 and 0x000FFFFF. The OS shall search this region
324 * starting at the low address and shall stop searching when the 1st valid SFI
325 * System Table is found.
327 * success: set syst_pa, return 0
328 * fail: return -1
330 static __init int sfi_find_syst(void)
332 unsigned long offset, len;
333 void *start;
335 len = SFI_SYST_SEARCH_END - SFI_SYST_SEARCH_BEGIN;
336 start = sfi_map_memory(SFI_SYST_SEARCH_BEGIN, len);
337 if (!start)
338 return -1;
340 for (offset = 0; offset < len; offset += 16) {
341 struct sfi_table_header *syst_hdr;
343 syst_hdr = start + offset;
344 if (strncmp(syst_hdr->sig, SFI_SIG_SYST,
345 SFI_SIGNATURE_SIZE))
346 continue;
348 if (syst_hdr->len > PAGE_SIZE)
349 continue;
351 sfi_print_table_header(SFI_SYST_SEARCH_BEGIN + offset,
352 syst_hdr);
354 if (sfi_verify_table(syst_hdr))
355 continue;
358 * Enforce SFI spec mandate that SYST reside within a page.
360 if (!ON_SAME_PAGE(syst_pa, syst_pa + syst_hdr->len)) {
361 pr_info("SYST 0x%llx + 0x%x crosses page\n",
362 syst_pa, syst_hdr->len);
363 continue;
366 /* Success */
367 syst_pa = SFI_SYST_SEARCH_BEGIN + offset;
368 sfi_unmap_memory(start, len);
369 return 0;
372 sfi_unmap_memory(start, len);
373 return -1;
376 void __init sfi_init(void)
378 if (!acpi_disabled)
379 disable_sfi();
381 if (sfi_disabled)
382 return;
384 pr_info("Simple Firmware Interface v0.7 http://simplefirmware.org\n");
386 if (sfi_find_syst() || sfi_parse_syst() || sfi_platform_init())
387 disable_sfi();
389 return;
392 void __init sfi_init_late(void)
394 int length;
396 if (sfi_disabled)
397 return;
399 length = syst_va->header.len;
400 sfi_unmap_memory(syst_va, sizeof(struct sfi_table_simple));
402 /* Use ioremap now after it is ready */
403 sfi_use_ioremap = 1;
404 syst_va = sfi_map_memory(syst_pa, length);
406 sfi_acpi_init();