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