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