Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / acpi / apei / apei-base.c
blob61540360d5ce815f67e67250874ad357f3e328e6
1 /*
2 * apei-base.c - ACPI Platform Error Interface (APEI) supporting
3 * infrastructure
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.
9 * For more information about APEI, please refer to ACPI Specification
10 * version 4.0, chapter 17.
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.
16 * Copyright (C) 2009, Intel Corp.
17 * Author: Huang Ying <ying.huang@intel.com>
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License version
21 * 2 as published by the Free Software Foundation.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/acpi.h>
37 #include <linux/slab.h>
38 #include <linux/io.h>
39 #include <linux/kref.h>
40 #include <linux/rculist.h>
41 #include <linux/interrupt.h>
42 #include <linux/debugfs.h>
43 #include <acpi/atomicio.h>
45 #include "apei-internal.h"
47 #define APEI_PFX "APEI: "
50 * APEI ERST (Error Record Serialization Table) and EINJ (Error
51 * INJection) interpreter framework.
54 #define APEI_EXEC_PRESERVE_REGISTER 0x1
56 void apei_exec_ctx_init(struct apei_exec_context *ctx,
57 struct apei_exec_ins_type *ins_table,
58 u32 instructions,
59 struct acpi_whea_header *action_table,
60 u32 entries)
62 ctx->ins_table = ins_table;
63 ctx->instructions = instructions;
64 ctx->action_table = action_table;
65 ctx->entries = entries;
67 EXPORT_SYMBOL_GPL(apei_exec_ctx_init);
69 int __apei_exec_read_register(struct acpi_whea_header *entry, u64 *val)
71 int rc;
73 rc = acpi_atomic_read(val, &entry->register_region);
74 if (rc)
75 return rc;
76 *val >>= entry->register_region.bit_offset;
77 *val &= entry->mask;
79 return 0;
82 int apei_exec_read_register(struct apei_exec_context *ctx,
83 struct acpi_whea_header *entry)
85 int rc;
86 u64 val = 0;
88 rc = __apei_exec_read_register(entry, &val);
89 if (rc)
90 return rc;
91 ctx->value = val;
93 return 0;
95 EXPORT_SYMBOL_GPL(apei_exec_read_register);
97 int apei_exec_read_register_value(struct apei_exec_context *ctx,
98 struct acpi_whea_header *entry)
100 int rc;
102 rc = apei_exec_read_register(ctx, entry);
103 if (rc)
104 return rc;
105 ctx->value = (ctx->value == entry->value);
107 return 0;
109 EXPORT_SYMBOL_GPL(apei_exec_read_register_value);
111 int __apei_exec_write_register(struct acpi_whea_header *entry, u64 val)
113 int rc;
115 val &= entry->mask;
116 val <<= entry->register_region.bit_offset;
117 if (entry->flags & APEI_EXEC_PRESERVE_REGISTER) {
118 u64 valr = 0;
119 rc = acpi_atomic_read(&valr, &entry->register_region);
120 if (rc)
121 return rc;
122 valr &= ~(entry->mask << entry->register_region.bit_offset);
123 val |= valr;
125 rc = acpi_atomic_write(val, &entry->register_region);
127 return rc;
130 int apei_exec_write_register(struct apei_exec_context *ctx,
131 struct acpi_whea_header *entry)
133 return __apei_exec_write_register(entry, ctx->value);
135 EXPORT_SYMBOL_GPL(apei_exec_write_register);
137 int apei_exec_write_register_value(struct apei_exec_context *ctx,
138 struct acpi_whea_header *entry)
140 int rc;
142 ctx->value = entry->value;
143 rc = apei_exec_write_register(ctx, entry);
145 return rc;
147 EXPORT_SYMBOL_GPL(apei_exec_write_register_value);
149 int apei_exec_noop(struct apei_exec_context *ctx,
150 struct acpi_whea_header *entry)
152 return 0;
154 EXPORT_SYMBOL_GPL(apei_exec_noop);
157 * Interpret the specified action. Go through whole action table,
158 * execute all instructions belong to the action.
160 int __apei_exec_run(struct apei_exec_context *ctx, u8 action,
161 bool optional)
163 int rc = -ENOENT;
164 u32 i, ip;
165 struct acpi_whea_header *entry;
166 apei_exec_ins_func_t run;
168 ctx->ip = 0;
171 * "ip" is the instruction pointer of current instruction,
172 * "ctx->ip" specifies the next instruction to executed,
173 * instruction "run" function may change the "ctx->ip" to
174 * implement "goto" semantics.
176 rewind:
177 ip = 0;
178 for (i = 0; i < ctx->entries; i++) {
179 entry = &ctx->action_table[i];
180 if (entry->action != action)
181 continue;
182 if (ip == ctx->ip) {
183 if (entry->instruction >= ctx->instructions ||
184 !ctx->ins_table[entry->instruction].run) {
185 pr_warning(FW_WARN APEI_PFX
186 "Invalid action table, unknown instruction type: %d\n",
187 entry->instruction);
188 return -EINVAL;
190 run = ctx->ins_table[entry->instruction].run;
191 rc = run(ctx, entry);
192 if (rc < 0)
193 return rc;
194 else if (rc != APEI_EXEC_SET_IP)
195 ctx->ip++;
197 ip++;
198 if (ctx->ip < ip)
199 goto rewind;
202 return !optional && rc < 0 ? rc : 0;
204 EXPORT_SYMBOL_GPL(__apei_exec_run);
206 typedef int (*apei_exec_entry_func_t)(struct apei_exec_context *ctx,
207 struct acpi_whea_header *entry,
208 void *data);
210 static int apei_exec_for_each_entry(struct apei_exec_context *ctx,
211 apei_exec_entry_func_t func,
212 void *data,
213 int *end)
215 u8 ins;
216 int i, rc;
217 struct acpi_whea_header *entry;
218 struct apei_exec_ins_type *ins_table = ctx->ins_table;
220 for (i = 0; i < ctx->entries; i++) {
221 entry = ctx->action_table + i;
222 ins = entry->instruction;
223 if (end)
224 *end = i;
225 if (ins >= ctx->instructions || !ins_table[ins].run) {
226 pr_warning(FW_WARN APEI_PFX
227 "Invalid action table, unknown instruction type: %d\n",
228 ins);
229 return -EINVAL;
231 rc = func(ctx, entry, data);
232 if (rc)
233 return rc;
236 return 0;
239 static int pre_map_gar_callback(struct apei_exec_context *ctx,
240 struct acpi_whea_header *entry,
241 void *data)
243 u8 ins = entry->instruction;
245 if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
246 return acpi_pre_map_gar(&entry->register_region);
248 return 0;
252 * Pre-map all GARs in action table to make it possible to access them
253 * in NMI handler.
255 int apei_exec_pre_map_gars(struct apei_exec_context *ctx)
257 int rc, end;
259 rc = apei_exec_for_each_entry(ctx, pre_map_gar_callback,
260 NULL, &end);
261 if (rc) {
262 struct apei_exec_context ctx_unmap;
263 memcpy(&ctx_unmap, ctx, sizeof(*ctx));
264 ctx_unmap.entries = end;
265 apei_exec_post_unmap_gars(&ctx_unmap);
268 return rc;
270 EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars);
272 static int post_unmap_gar_callback(struct apei_exec_context *ctx,
273 struct acpi_whea_header *entry,
274 void *data)
276 u8 ins = entry->instruction;
278 if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
279 acpi_post_unmap_gar(&entry->register_region);
281 return 0;
284 /* Post-unmap all GAR in action table. */
285 int apei_exec_post_unmap_gars(struct apei_exec_context *ctx)
287 return apei_exec_for_each_entry(ctx, post_unmap_gar_callback,
288 NULL, NULL);
290 EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars);
293 * Resource management for GARs in APEI
295 struct apei_res {
296 struct list_head list;
297 unsigned long start;
298 unsigned long end;
301 /* Collect all resources requested, to avoid conflict */
302 struct apei_resources apei_resources_all = {
303 .iomem = LIST_HEAD_INIT(apei_resources_all.iomem),
304 .ioport = LIST_HEAD_INIT(apei_resources_all.ioport),
307 static int apei_res_add(struct list_head *res_list,
308 unsigned long start, unsigned long size)
310 struct apei_res *res, *resn, *res_ins = NULL;
311 unsigned long end = start + size;
313 if (end <= start)
314 return 0;
315 repeat:
316 list_for_each_entry_safe(res, resn, res_list, list) {
317 if (res->start > end || res->end < start)
318 continue;
319 else if (end <= res->end && start >= res->start) {
320 kfree(res_ins);
321 return 0;
323 list_del(&res->list);
324 res->start = start = min(res->start, start);
325 res->end = end = max(res->end, end);
326 kfree(res_ins);
327 res_ins = res;
328 goto repeat;
331 if (res_ins)
332 list_add(&res_ins->list, res_list);
333 else {
334 res_ins = kmalloc(sizeof(*res), GFP_KERNEL);
335 if (!res_ins)
336 return -ENOMEM;
337 res_ins->start = start;
338 res_ins->end = end;
339 list_add(&res_ins->list, res_list);
342 return 0;
345 static int apei_res_sub(struct list_head *res_list1,
346 struct list_head *res_list2)
348 struct apei_res *res1, *resn1, *res2, *res;
349 res1 = list_entry(res_list1->next, struct apei_res, list);
350 resn1 = list_entry(res1->list.next, struct apei_res, list);
351 while (&res1->list != res_list1) {
352 list_for_each_entry(res2, res_list2, list) {
353 if (res1->start >= res2->end ||
354 res1->end <= res2->start)
355 continue;
356 else if (res1->end <= res2->end &&
357 res1->start >= res2->start) {
358 list_del(&res1->list);
359 kfree(res1);
360 break;
361 } else if (res1->end > res2->end &&
362 res1->start < res2->start) {
363 res = kmalloc(sizeof(*res), GFP_KERNEL);
364 if (!res)
365 return -ENOMEM;
366 res->start = res2->end;
367 res->end = res1->end;
368 res1->end = res2->start;
369 list_add(&res->list, &res1->list);
370 resn1 = res;
371 } else {
372 if (res1->start < res2->start)
373 res1->end = res2->start;
374 else
375 res1->start = res2->end;
378 res1 = resn1;
379 resn1 = list_entry(resn1->list.next, struct apei_res, list);
382 return 0;
385 static void apei_res_clean(struct list_head *res_list)
387 struct apei_res *res, *resn;
389 list_for_each_entry_safe(res, resn, res_list, list) {
390 list_del(&res->list);
391 kfree(res);
395 void apei_resources_fini(struct apei_resources *resources)
397 apei_res_clean(&resources->iomem);
398 apei_res_clean(&resources->ioport);
400 EXPORT_SYMBOL_GPL(apei_resources_fini);
402 static int apei_resources_merge(struct apei_resources *resources1,
403 struct apei_resources *resources2)
405 int rc;
406 struct apei_res *res;
408 list_for_each_entry(res, &resources2->iomem, list) {
409 rc = apei_res_add(&resources1->iomem, res->start,
410 res->end - res->start);
411 if (rc)
412 return rc;
414 list_for_each_entry(res, &resources2->ioport, list) {
415 rc = apei_res_add(&resources1->ioport, res->start,
416 res->end - res->start);
417 if (rc)
418 return rc;
421 return 0;
425 * EINJ has two groups of GARs (EINJ table entry and trigger table
426 * entry), so common resources are subtracted from the trigger table
427 * resources before the second requesting.
429 int apei_resources_sub(struct apei_resources *resources1,
430 struct apei_resources *resources2)
432 int rc;
434 rc = apei_res_sub(&resources1->iomem, &resources2->iomem);
435 if (rc)
436 return rc;
437 return apei_res_sub(&resources1->ioport, &resources2->ioport);
439 EXPORT_SYMBOL_GPL(apei_resources_sub);
442 * IO memory/port rersource management mechanism is used to check
443 * whether memory/port area used by GARs conflicts with normal memory
444 * or IO memory/port of devices.
446 int apei_resources_request(struct apei_resources *resources,
447 const char *desc)
449 struct apei_res *res, *res_bak = NULL;
450 struct resource *r;
451 int rc;
453 rc = apei_resources_sub(resources, &apei_resources_all);
454 if (rc)
455 return rc;
457 rc = -EINVAL;
458 list_for_each_entry(res, &resources->iomem, list) {
459 r = request_mem_region(res->start, res->end - res->start,
460 desc);
461 if (!r) {
462 pr_err(APEI_PFX
463 "Can not request iomem region <%016llx-%016llx> for GARs.\n",
464 (unsigned long long)res->start,
465 (unsigned long long)res->end);
466 res_bak = res;
467 goto err_unmap_iomem;
471 list_for_each_entry(res, &resources->ioport, list) {
472 r = request_region(res->start, res->end - res->start, desc);
473 if (!r) {
474 pr_err(APEI_PFX
475 "Can not request ioport region <%016llx-%016llx> for GARs.\n",
476 (unsigned long long)res->start,
477 (unsigned long long)res->end);
478 res_bak = res;
479 goto err_unmap_ioport;
483 rc = apei_resources_merge(&apei_resources_all, resources);
484 if (rc) {
485 pr_err(APEI_PFX "Fail to merge resources!\n");
486 goto err_unmap_ioport;
489 return 0;
490 err_unmap_ioport:
491 list_for_each_entry(res, &resources->ioport, list) {
492 if (res == res_bak)
493 break;
494 release_region(res->start, res->end - res->start);
496 res_bak = NULL;
497 err_unmap_iomem:
498 list_for_each_entry(res, &resources->iomem, list) {
499 if (res == res_bak)
500 break;
501 release_mem_region(res->start, res->end - res->start);
503 return rc;
505 EXPORT_SYMBOL_GPL(apei_resources_request);
507 void apei_resources_release(struct apei_resources *resources)
509 int rc;
510 struct apei_res *res;
512 list_for_each_entry(res, &resources->iomem, list)
513 release_mem_region(res->start, res->end - res->start);
514 list_for_each_entry(res, &resources->ioport, list)
515 release_region(res->start, res->end - res->start);
517 rc = apei_resources_sub(&apei_resources_all, resources);
518 if (rc)
519 pr_err(APEI_PFX "Fail to sub resources!\n");
521 EXPORT_SYMBOL_GPL(apei_resources_release);
523 static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr)
525 u32 width, space_id;
527 width = reg->bit_width;
528 space_id = reg->space_id;
529 /* Handle possible alignment issues */
530 memcpy(paddr, &reg->address, sizeof(*paddr));
531 if (!*paddr) {
532 pr_warning(FW_BUG APEI_PFX
533 "Invalid physical address in GAR [0x%llx/%u/%u]\n",
534 *paddr, width, space_id);
535 return -EINVAL;
538 if ((width != 8) && (width != 16) && (width != 32) && (width != 64)) {
539 pr_warning(FW_BUG APEI_PFX
540 "Invalid bit width in GAR [0x%llx/%u/%u]\n",
541 *paddr, width, space_id);
542 return -EINVAL;
545 if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
546 space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
547 pr_warning(FW_BUG APEI_PFX
548 "Invalid address space type in GAR [0x%llx/%u/%u]\n",
549 *paddr, width, space_id);
550 return -EINVAL;
553 return 0;
556 static int collect_res_callback(struct apei_exec_context *ctx,
557 struct acpi_whea_header *entry,
558 void *data)
560 struct apei_resources *resources = data;
561 struct acpi_generic_address *reg = &entry->register_region;
562 u8 ins = entry->instruction;
563 u64 paddr;
564 int rc;
566 if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
567 return 0;
569 rc = apei_check_gar(reg, &paddr);
570 if (rc)
571 return rc;
573 switch (reg->space_id) {
574 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
575 return apei_res_add(&resources->iomem, paddr,
576 reg->bit_width / 8);
577 case ACPI_ADR_SPACE_SYSTEM_IO:
578 return apei_res_add(&resources->ioport, paddr,
579 reg->bit_width / 8);
580 default:
581 return -EINVAL;
586 * Same register may be used by multiple instructions in GARs, so
587 * resources are collected before requesting.
589 int apei_exec_collect_resources(struct apei_exec_context *ctx,
590 struct apei_resources *resources)
592 return apei_exec_for_each_entry(ctx, collect_res_callback,
593 resources, NULL);
595 EXPORT_SYMBOL_GPL(apei_exec_collect_resources);
597 struct dentry *apei_get_debugfs_dir(void)
599 static struct dentry *dapei;
601 if (!dapei)
602 dapei = debugfs_create_dir("apei", NULL);
604 return dapei;
606 EXPORT_SYMBOL_GPL(apei_get_debugfs_dir);
608 int apei_osc_setup(void)
610 static u8 whea_uuid_str[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
611 acpi_handle handle;
612 u32 capbuf[3];
613 struct acpi_osc_context context = {
614 .uuid_str = whea_uuid_str,
615 .rev = 1,
616 .cap.length = sizeof(capbuf),
617 .cap.pointer = capbuf,
620 capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
621 capbuf[OSC_SUPPORT_TYPE] = 1;
622 capbuf[OSC_CONTROL_TYPE] = 0;
624 if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))
625 || ACPI_FAILURE(acpi_run_osc(handle, &context)))
626 return -EIO;
627 else {
628 kfree(context.ret.pointer);
629 return 0;
632 EXPORT_SYMBOL_GPL(apei_osc_setup);