1 /*
2 * apei-base.c - ACPI Platform Error Interface (APEI) supporting
3 * infrastructure
4 *
5 * APEI allows to report errors (for example from the chipset) to the
6 * the operating system. This improves NMI handling especially. In
7 * addition it supports error serialization and error injection.
8 *
9 * For more information about APEI, please refer to ACPI Specification
10 * version 4.0, chapter 17.
11 *
12 * This file has Common functions used by more than one APEI table,
13 * including framework of interpreter for ERST and EINJ; resource
14 * management for APEI registers.
15 *
16 * This feature is ported from linux acpi tree
17 * Copyright (C) 2009, Intel Corp.
18 * Author: Huang Ying <ying.huang@intel.com>
19 * Ported by: Liu, Jinsong <jinsong.liu@intel.com>
20 *
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License version
23 * 2 as published by the Free Software Foundation.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; If not, see <http://www.gnu.org/licenses/>.
32 */
33 #include <xen/kernel.h>
34 #include <xen/errno.h>
35 #include <xen/delay.h>
36 #include <xen/string.h>
37 #include <xen/types.h>
38 #include <xen/spinlock.h>
39 #include <xen/init.h>
40 #include <xen/cper.h>
41 #include <asm/io.h>
42 #include <acpi/acpi.h>
43 #include <acpi/apei.h>
44
45 #include "apei-internal.h"
46
47 /*
48 * APEI ERST (Error Record Serialization Table) and EINJ (Error
49 * INJection) interpreter framework.
50 */
51
52 #define APEI_EXEC_PRESERVE_REGISTER 0x1
53
apei_exec_ctx_init(struct apei_exec_context * ctx,struct apei_exec_ins_type * ins_table,u32 instructions,struct acpi_whea_header * action_table,u32 entries)54 int apei_exec_ctx_init(struct apei_exec_context *ctx,
55 struct apei_exec_ins_type *ins_table,
56 u32 instructions,
57 struct acpi_whea_header *action_table,
58 u32 entries)
59 {
60 if (!ctx)
61 return -EINVAL;
62
63 ctx->ins_table = ins_table;
64 ctx->instructions = instructions;
65 ctx->action_table = action_table;
66 ctx->entries = entries;
67 return 0;
68 }
69
__apei_exec_read_register(struct acpi_whea_header * entry,u64 * val)70 int __apei_exec_read_register(struct acpi_whea_header *entry, u64 *val)
71 {
72 int rc;
73
74 rc = apei_read(val, &entry->register_region);
75 if (rc)
76 return rc;
77 *val >>= entry->register_region.bit_offset;
78 *val &= entry->mask;
79
80 return 0;
81 }
82
apei_exec_read_register(struct apei_exec_context * ctx,struct acpi_whea_header * entry)83 int apei_exec_read_register(struct apei_exec_context *ctx,
84 struct acpi_whea_header *entry)
85 {
86 int rc;
87 u64 val = 0;
88
89 rc = __apei_exec_read_register(entry, &val);
90 if (rc)
91 return rc;
92 ctx->value = val;
93
94 return 0;
95 }
96
apei_exec_read_register_value(struct apei_exec_context * ctx,struct acpi_whea_header * entry)97 int apei_exec_read_register_value(struct apei_exec_context *ctx,
98 struct acpi_whea_header *entry)
99 {
100 int rc;
101
102 rc = apei_exec_read_register(ctx, entry);
103 if (rc)
104 return rc;
105 ctx->value = (ctx->value == entry->value);
106
107 return 0;
108 }
109
__apei_exec_write_register(struct acpi_whea_header * entry,u64 val)110 int __apei_exec_write_register(struct acpi_whea_header *entry, u64 val)
111 {
112 int rc;
113
114 val &= entry->mask;
115 val <<= entry->register_region.bit_offset;
116 if (entry->flags & APEI_EXEC_PRESERVE_REGISTER) {
117 u64 valr = 0;
118 rc = apei_read(&valr, &entry->register_region);
119 if (rc)
120 return rc;
121 valr &= ~(entry->mask << entry->register_region.bit_offset);
122 val |= valr;
123 }
124 rc = apei_write(val, &entry->register_region);
125
126 return rc;
127 }
128
apei_exec_write_register(struct apei_exec_context * ctx,struct acpi_whea_header * entry)129 int apei_exec_write_register(struct apei_exec_context *ctx,
130 struct acpi_whea_header *entry)
131 {
132 return __apei_exec_write_register(entry, ctx->value);
133 }
134
apei_exec_write_register_value(struct apei_exec_context * ctx,struct acpi_whea_header * entry)135 int apei_exec_write_register_value(struct apei_exec_context *ctx,
136 struct acpi_whea_header *entry)
137 {
138 int rc;
139
140 ctx->value = entry->value;
141 rc = apei_exec_write_register(ctx, entry);
142
143 return rc;
144 }
145
apei_exec_noop(struct apei_exec_context * ctx,struct acpi_whea_header * entry)146 int apei_exec_noop(struct apei_exec_context *ctx,
147 struct acpi_whea_header *entry)
148 {
149 return 0;
150 }
151
152 /*
153 * Interpret the specified action. Go through whole action table,
154 * execute all instructions belong to the action.
155 */
__apei_exec_run(struct apei_exec_context * ctx,u8 action,bool_t optional)156 int __apei_exec_run(struct apei_exec_context *ctx, u8 action,
157 bool_t optional)
158 {
159 int rc = -ENOENT;
160 u32 i, ip;
161 struct acpi_whea_header *entry;
162 apei_exec_ins_func_t run;
163
164 ctx->ip = 0;
165
166 /*
167 * "ip" is the instruction pointer of current instruction,
168 * "ctx->ip" specifies the next instruction to executed,
169 * instruction "run" function may change the "ctx->ip" to
170 * implement "goto" semantics.
171 */
172 rewind:
173 ip = 0;
174 for (i = 0; i < ctx->entries; i++) {
175 entry = &ctx->action_table[i];
176 if (entry->action != action)
177 continue;
178 if (ip == ctx->ip) {
179 if (entry->instruction >= ctx->instructions ||
180 !ctx->ins_table[entry->instruction].run) {
181 printk(KERN_WARNING
182 "Invalid action table, unknown instruction "
183 "type: %d\n", entry->instruction);
184 return -EINVAL;
185 }
186 run = ctx->ins_table[entry->instruction].run;
187 rc = run(ctx, entry);
188 if (rc < 0)
189 return rc;
190 else if (rc != APEI_EXEC_SET_IP)
191 ctx->ip++;
192 }
193 ip++;
194 if (ctx->ip < ip)
195 goto rewind;
196 }
197
198 return !optional && rc < 0 ? rc : 0;
199 }
200
201 typedef int (*apei_exec_entry_func_t)(struct apei_exec_context *ctx,
202 struct acpi_whea_header *entry,
203 void *data);
204
apei_exec_for_each_entry(struct apei_exec_context * ctx,apei_exec_entry_func_t func,void * data,int * end)205 static int __init apei_exec_for_each_entry(struct apei_exec_context *ctx,
206 apei_exec_entry_func_t func,
207 void *data,
208 int *end)
209 {
210 u8 ins;
211 int i, rc;
212 struct acpi_whea_header *entry;
213 struct apei_exec_ins_type *ins_table = ctx->ins_table;
214
215 for (i = 0; i < ctx->entries; i++) {
216 entry = ctx->action_table + i;
217 ins = entry->instruction;
218 if (end)
219 *end = i;
220 if (ins >= ctx->instructions || !ins_table[ins].run) {
221 printk(KERN_WARNING "Invalid action table, "
222 "unknown instruction type: %d\n", ins);
223 return -EINVAL;
224 }
225 rc = func(ctx, entry, data);
226 if (rc)
227 return rc;
228 }
229
230 return 0;
231 }
232
pre_map_gar_callback(struct apei_exec_context * ctx,struct acpi_whea_header * entry,void * data)233 static int __init pre_map_gar_callback(struct apei_exec_context *ctx,
234 struct acpi_whea_header *entry,
235 void *data)
236 {
237 u8 ins = entry->instruction;
238
239 if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
240 return apei_pre_map_gar(&entry->register_region);
241
242 return 0;
243 }
244
245 /* Pre-map all GARs in action table. */
apei_exec_pre_map_gars(struct apei_exec_context * ctx)246 int __init apei_exec_pre_map_gars(struct apei_exec_context *ctx)
247 {
248 int rc, end;
249
250 rc = apei_exec_for_each_entry(ctx, pre_map_gar_callback,
251 NULL, &end);
252 if (rc) {
253 struct apei_exec_context ctx_unmap;
254 memcpy(&ctx_unmap, ctx, sizeof(*ctx));
255 ctx_unmap.entries = end;
256 apei_exec_post_unmap_gars(&ctx_unmap);
257 }
258
259 return rc;
260 }
261
post_unmap_gar_callback(struct apei_exec_context * ctx,struct acpi_whea_header * entry,void * data)262 static int __init post_unmap_gar_callback(struct apei_exec_context *ctx,
263 struct acpi_whea_header *entry,
264 void *data)
265 {
266 u8 ins = entry->instruction;
267
268 if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
269 apei_post_unmap_gar(&entry->register_region);
270
271 return 0;
272 }
273
274 /* Post-unmap all GAR in action table. */
apei_exec_post_unmap_gars(struct apei_exec_context * ctx)275 int __init apei_exec_post_unmap_gars(struct apei_exec_context *ctx)
276 {
277 return apei_exec_for_each_entry(ctx, post_unmap_gar_callback,
278 NULL, NULL);
279 }
280