xref: /OK3568_Linux_fs/kernel/drivers/sfi/sfi_acpi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* sfi_acpi.c Simple Firmware Interface - ACPI extensions */
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/kernel.h>
63*4882a593Smuzhiyun #include <linux/sfi_acpi.h>
64*4882a593Smuzhiyun #include "sfi_core.h"
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun  * SFI can access ACPI-defined tables via an optional ACPI XSDT.
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * This allows re-use, and avoids re-definition, of standard tables.
70*4882a593Smuzhiyun  * For example, the "MCFG" table is defined by PCI, reserved by ACPI,
71*4882a593Smuzhiyun  * and is expected to be present many SFI-only systems.
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun static struct acpi_table_xsdt *xsdt_va __read_mostly;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define XSDT_GET_NUM_ENTRIES(ptable, entry_type) \
77*4882a593Smuzhiyun 	((ptable->header.length - sizeof(struct acpi_table_header)) / \
78*4882a593Smuzhiyun 	(sizeof(entry_type)))
79*4882a593Smuzhiyun 
acpi_to_sfi_th(struct acpi_table_header * th)80*4882a593Smuzhiyun static inline struct sfi_table_header *acpi_to_sfi_th(
81*4882a593Smuzhiyun 				struct acpi_table_header *th)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	return (struct sfi_table_header *)th;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
sfi_to_acpi_th(struct sfi_table_header * th)86*4882a593Smuzhiyun static inline struct acpi_table_header *sfi_to_acpi_th(
87*4882a593Smuzhiyun 				struct sfi_table_header *th)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	return (struct acpi_table_header *)th;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun  * sfi_acpi_parse_xsdt()
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * Parse the ACPI XSDT for later access by sfi_acpi_table_parse().
96*4882a593Smuzhiyun  */
sfi_acpi_parse_xsdt(struct sfi_table_header * th)97*4882a593Smuzhiyun static int __init sfi_acpi_parse_xsdt(struct sfi_table_header *th)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct sfi_table_key key = SFI_ANY_KEY;
100*4882a593Smuzhiyun 	int tbl_cnt, i;
101*4882a593Smuzhiyun 	void *ret;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	xsdt_va = (struct acpi_table_xsdt *)th;
104*4882a593Smuzhiyun 	tbl_cnt = XSDT_GET_NUM_ENTRIES(xsdt_va, u64);
105*4882a593Smuzhiyun 	for (i = 0; i < tbl_cnt; i++) {
106*4882a593Smuzhiyun 		ret = sfi_check_table(xsdt_va->table_offset_entry[i], &key);
107*4882a593Smuzhiyun 		if (IS_ERR(ret)) {
108*4882a593Smuzhiyun 			disable_sfi();
109*4882a593Smuzhiyun 			return -1;
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	return 0;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
sfi_acpi_init(void)116*4882a593Smuzhiyun int __init sfi_acpi_init(void)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	struct sfi_table_key xsdt_key = { .sig = SFI_SIG_XSDT };
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	sfi_table_parse(SFI_SIG_XSDT, NULL, NULL, sfi_acpi_parse_xsdt);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	/* Only call the get_table to keep the table mapped */
123*4882a593Smuzhiyun 	xsdt_va = (struct acpi_table_xsdt *)sfi_get_table(&xsdt_key);
124*4882a593Smuzhiyun 	return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
sfi_acpi_get_table(struct sfi_table_key * key)127*4882a593Smuzhiyun static struct acpi_table_header *sfi_acpi_get_table(struct sfi_table_key *key)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	u32 tbl_cnt, i;
130*4882a593Smuzhiyun 	void *ret;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	tbl_cnt = XSDT_GET_NUM_ENTRIES(xsdt_va, u64);
133*4882a593Smuzhiyun 	for (i = 0; i < tbl_cnt; i++) {
134*4882a593Smuzhiyun 		ret = sfi_check_table(xsdt_va->table_offset_entry[i], key);
135*4882a593Smuzhiyun 		if (!IS_ERR(ret) && ret)
136*4882a593Smuzhiyun 			return sfi_to_acpi_th(ret);
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return NULL;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
sfi_acpi_put_table(struct acpi_table_header * table)142*4882a593Smuzhiyun static void sfi_acpi_put_table(struct acpi_table_header *table)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	sfi_put_table(acpi_to_sfi_th(table));
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun  * sfi_acpi_table_parse()
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * Find specified table in XSDT, run handler on it and return its return value
151*4882a593Smuzhiyun  */
sfi_acpi_table_parse(char * signature,char * oem_id,char * oem_table_id,int (* handler)(struct acpi_table_header *))152*4882a593Smuzhiyun int sfi_acpi_table_parse(char *signature, char *oem_id, char *oem_table_id,
153*4882a593Smuzhiyun 			int(*handler)(struct acpi_table_header *))
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	struct acpi_table_header *table = NULL;
156*4882a593Smuzhiyun 	struct sfi_table_key key;
157*4882a593Smuzhiyun 	int ret = 0;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	if (sfi_disabled)
160*4882a593Smuzhiyun 		return -1;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	key.sig = signature;
163*4882a593Smuzhiyun 	key.oem_id = oem_id;
164*4882a593Smuzhiyun 	key.oem_table_id = oem_table_id;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	table = sfi_acpi_get_table(&key);
167*4882a593Smuzhiyun 	if (!table)
168*4882a593Smuzhiyun 		return -EINVAL;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	ret = handler(table);
171*4882a593Smuzhiyun 	sfi_acpi_put_table(table);
172*4882a593Smuzhiyun 	return ret;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
sfi_acpi_table_show(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t offset,size_t count)175*4882a593Smuzhiyun static ssize_t sfi_acpi_table_show(struct file *filp, struct kobject *kobj,
176*4882a593Smuzhiyun 			       struct bin_attribute *bin_attr, char *buf,
177*4882a593Smuzhiyun 			       loff_t offset, size_t count)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	struct sfi_table_attr *tbl_attr =
180*4882a593Smuzhiyun 	    container_of(bin_attr, struct sfi_table_attr, attr);
181*4882a593Smuzhiyun 	struct acpi_table_header *th = NULL;
182*4882a593Smuzhiyun 	struct sfi_table_key key;
183*4882a593Smuzhiyun 	ssize_t cnt;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	key.sig = tbl_attr->name;
186*4882a593Smuzhiyun 	key.oem_id = NULL;
187*4882a593Smuzhiyun 	key.oem_table_id = NULL;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	th = sfi_acpi_get_table(&key);
190*4882a593Smuzhiyun 	if (!th)
191*4882a593Smuzhiyun 		return 0;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	cnt =  memory_read_from_buffer(buf, count, &offset,
194*4882a593Smuzhiyun 					th, th->length);
195*4882a593Smuzhiyun 	sfi_acpi_put_table(th);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return cnt;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 
sfi_acpi_sysfs_init(void)201*4882a593Smuzhiyun void __init sfi_acpi_sysfs_init(void)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	u32 tbl_cnt, i;
204*4882a593Smuzhiyun 	struct sfi_table_attr *tbl_attr;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	tbl_cnt = XSDT_GET_NUM_ENTRIES(xsdt_va, u64);
207*4882a593Smuzhiyun 	for (i = 0; i < tbl_cnt; i++) {
208*4882a593Smuzhiyun 		tbl_attr =
209*4882a593Smuzhiyun 			sfi_sysfs_install_table(xsdt_va->table_offset_entry[i]);
210*4882a593Smuzhiyun 		tbl_attr->attr.read = sfi_acpi_table_show;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	return;
214*4882a593Smuzhiyun }
215