Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
apei-base.c
Go to the documentation of this file.
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  * Copyright (C) 2009, Intel Corp.
17  * Author: Huang Ying <[email protected]>
18  *
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.
22  *
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.
27  *
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
31  */
32 
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/acpi.h>
37 #include <linux/acpi_io.h>
38 #include <linux/slab.h>
39 #include <linux/io.h>
40 #include <linux/kref.h>
41 #include <linux/rculist.h>
42 #include <linux/interrupt.h>
43 #include <linux/debugfs.h>
44 
45 #include "apei-internal.h"
46 
47 #define APEI_PFX "APEI: "
48 
49 /*
50  * APEI ERST (Error Record Serialization Table) and EINJ (Error
51  * INJection) interpreter framework.
52  */
53 
54 #define APEI_EXEC_PRESERVE_REGISTER 0x1
55 
57  struct apei_exec_ins_type *ins_table,
58  u32 instructions,
59  struct acpi_whea_header *action_table,
60  u32 entries)
61 {
62  ctx->ins_table = ins_table;
63  ctx->instructions = instructions;
64  ctx->action_table = action_table;
65  ctx->entries = entries;
66 }
68 
70 {
71  int rc;
72 
73  rc = apei_read(val, &entry->register_region);
74  if (rc)
75  return rc;
76  *val >>= entry->register_region.bit_offset;
77  *val &= entry->mask;
78 
79  return 0;
80 }
81 
83  struct acpi_whea_header *entry)
84 {
85  int rc;
86  u64 val = 0;
87 
88  rc = __apei_exec_read_register(entry, &val);
89  if (rc)
90  return rc;
91  ctx->value = val;
92 
93  return 0;
94 }
96 
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 }
110 
112 {
113  int rc;
114 
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 = apei_read(&valr, &entry->register_region);
120  if (rc)
121  return rc;
122  valr &= ~(entry->mask << entry->register_region.bit_offset);
123  val |= valr;
124  }
125  rc = apei_write(val, &entry->register_region);
126 
127  return rc;
128 }
129 
131  struct acpi_whea_header *entry)
132 {
133  return __apei_exec_write_register(entry, ctx->value);
134 }
136 
138  struct acpi_whea_header *entry)
139 {
140  int rc;
141 
142  ctx->value = entry->value;
143  rc = apei_exec_write_register(ctx, entry);
144 
145  return rc;
146 }
148 
150  struct acpi_whea_header *entry)
151 {
152  return 0;
153 }
155 
156 /*
157  * Interpret the specified action. Go through whole action table,
158  * execute all instructions belong to the action.
159  */
161  bool optional)
162 {
163  int rc = -ENOENT;
164  u32 i, ip;
165  struct acpi_whea_header *entry;
167 
168  ctx->ip = 0;
169 
170  /*
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.
175  */
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) {
186  "Invalid action table, unknown instruction type: %d\n",
187  entry->instruction);
188  return -EINVAL;
189  }
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++;
196  }
197  ip++;
198  if (ctx->ip < ip)
199  goto rewind;
200  }
201 
202  return !optional && rc < 0 ? rc : 0;
203 }
205 
207  struct acpi_whea_header *entry,
208  void *data);
209 
210 static int apei_exec_for_each_entry(struct apei_exec_context *ctx,
212  void *data,
213  int *end)
214 {
215  u8 ins;
216  int i, rc;
217  struct acpi_whea_header *entry;
218  struct apei_exec_ins_type *ins_table = ctx->ins_table;
219 
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) {
227  "Invalid action table, unknown instruction type: %d\n",
228  ins);
229  return -EINVAL;
230  }
231  rc = func(ctx, entry, data);
232  if (rc)
233  return rc;
234  }
235 
236  return 0;
237 }
238 
239 static int pre_map_gar_callback(struct apei_exec_context *ctx,
240  struct acpi_whea_header *entry,
241  void *data)
242 {
243  u8 ins = entry->instruction;
244 
245  if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
247 
248  return 0;
249 }
250 
251 /*
252  * Pre-map all GARs in action table to make it possible to access them
253  * in NMI handler.
254  */
256 {
257  int rc, end;
258 
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);
266  }
267 
268  return rc;
269 }
271 
272 static int post_unmap_gar_callback(struct apei_exec_context *ctx,
273  struct acpi_whea_header *entry,
274  void *data)
275 {
276  u8 ins = entry->instruction;
277 
278  if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
279  apei_unmap_generic_address(&entry->register_region);
280 
281  return 0;
282 }
283 
284 /* Post-unmap all GAR in action table. */
286 {
287  return apei_exec_for_each_entry(ctx, post_unmap_gar_callback,
288  NULL, NULL);
289 }
291 
292 /*
293  * Resource management for GARs in APEI
294  */
295 struct apei_res {
296  struct list_head list;
297  unsigned long start;
298  unsigned long end;
299 };
300 
301 /* Collect all resources requested, to avoid conflict */
303  .iomem = LIST_HEAD_INIT(apei_resources_all.iomem),
304  .ioport = LIST_HEAD_INIT(apei_resources_all.ioport),
305 };
306 
307 static int apei_res_add(struct list_head *res_list,
308  unsigned long start, unsigned long size)
309 {
310  struct apei_res *res, *resn, *res_ins = NULL;
311  unsigned long end = start + size;
312 
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;
322  }
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;
329  }
330 
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);
340  }
341 
342  return 0;
343 }
344 
345 static int apei_res_sub(struct list_head *res_list1,
346  struct list_head *res_list2)
347 {
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;
376  }
377  }
378  res1 = resn1;
379  resn1 = list_entry(resn1->list.next, struct apei_res, list);
380  }
381 
382  return 0;
383 }
384 
385 static void apei_res_clean(struct list_head *res_list)
386 {
387  struct apei_res *res, *resn;
388 
389  list_for_each_entry_safe(res, resn, res_list, list) {
390  list_del(&res->list);
391  kfree(res);
392  }
393 }
394 
396 {
397  apei_res_clean(&resources->iomem);
398  apei_res_clean(&resources->ioport);
399 }
401 
402 static int apei_resources_merge(struct apei_resources *resources1,
403  struct apei_resources *resources2)
404 {
405  int rc;
406  struct apei_res *res;
407 
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;
413  }
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;
419  }
420 
421  return 0;
422 }
423 
425  unsigned long start, unsigned long size,
426  bool iomem)
427 {
428  if (iomem)
429  return apei_res_add(&resources->iomem, start, size);
430  else
431  return apei_res_add(&resources->ioport, start, size);
432 }
434 
435 /*
436  * EINJ has two groups of GARs (EINJ table entry and trigger table
437  * entry), so common resources are subtracted from the trigger table
438  * resources before the second requesting.
439  */
440 int apei_resources_sub(struct apei_resources *resources1,
441  struct apei_resources *resources2)
442 {
443  int rc;
444 
445  rc = apei_res_sub(&resources1->iomem, &resources2->iomem);
446  if (rc)
447  return rc;
448  return apei_res_sub(&resources1->ioport, &resources2->ioport);
449 }
451 
452 static int apei_get_nvs_callback(__u64 start, __u64 size, void *data)
453 {
454  struct apei_resources *resources = data;
455  return apei_res_add(&resources->iomem, start, size);
456 }
457 
458 static int apei_get_nvs_resources(struct apei_resources *resources)
459 {
460  return acpi_nvs_for_each_region(apei_get_nvs_callback, resources);
461 }
462 
463 /*
464  * IO memory/port resource management mechanism is used to check
465  * whether memory/port area used by GARs conflicts with normal memory
466  * or IO memory/port of devices.
467  */
469  const char *desc)
470 {
471  struct apei_res *res, *res_bak = NULL;
472  struct resource *r;
473  struct apei_resources nvs_resources;
474  int rc;
475 
476  rc = apei_resources_sub(resources, &apei_resources_all);
477  if (rc)
478  return rc;
479 
480  /*
481  * Some firmware uses ACPI NVS region, that has been marked as
482  * busy, so exclude it from APEI resources to avoid false
483  * conflict.
484  */
485  apei_resources_init(&nvs_resources);
486  rc = apei_get_nvs_resources(&nvs_resources);
487  if (rc)
488  goto res_fini;
489  rc = apei_resources_sub(resources, &nvs_resources);
490  if (rc)
491  goto res_fini;
492 
493  rc = -EINVAL;
494  list_for_each_entry(res, &resources->iomem, list) {
495  r = request_mem_region(res->start, res->end - res->start,
496  desc);
497  if (!r) {
499  "Can not request [mem %#010llx-%#010llx] for %s registers\n",
500  (unsigned long long)res->start,
501  (unsigned long long)res->end - 1, desc);
502  res_bak = res;
503  goto err_unmap_iomem;
504  }
505  }
506 
507  list_for_each_entry(res, &resources->ioport, list) {
508  r = request_region(res->start, res->end - res->start, desc);
509  if (!r) {
511  "Can not request [io %#06llx-%#06llx] for %s registers\n",
512  (unsigned long long)res->start,
513  (unsigned long long)res->end - 1, desc);
514  res_bak = res;
515  goto err_unmap_ioport;
516  }
517  }
518 
519  rc = apei_resources_merge(&apei_resources_all, resources);
520  if (rc) {
521  pr_err(APEI_PFX "Fail to merge resources!\n");
522  goto err_unmap_ioport;
523  }
524 
525  return 0;
526 err_unmap_ioport:
527  list_for_each_entry(res, &resources->ioport, list) {
528  if (res == res_bak)
529  break;
530  release_region(res->start, res->end - res->start);
531  }
532  res_bak = NULL;
533 err_unmap_iomem:
534  list_for_each_entry(res, &resources->iomem, list) {
535  if (res == res_bak)
536  break;
537  release_mem_region(res->start, res->end - res->start);
538  }
539 res_fini:
540  apei_resources_fini(&nvs_resources);
541  return rc;
542 }
544 
546 {
547  int rc;
548  struct apei_res *res;
549 
550  list_for_each_entry(res, &resources->iomem, list)
551  release_mem_region(res->start, res->end - res->start);
552  list_for_each_entry(res, &resources->ioport, list)
553  release_region(res->start, res->end - res->start);
554 
555  rc = apei_resources_sub(&apei_resources_all, resources);
556  if (rc)
557  pr_err(APEI_PFX "Fail to sub resources!\n");
558 }
560 
561 static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr,
562  u32 *access_bit_width)
563 {
564  u32 bit_width, bit_offset, access_size_code, space_id;
565 
566  bit_width = reg->bit_width;
567  bit_offset = reg->bit_offset;
568  access_size_code = reg->access_width;
569  space_id = reg->space_id;
570  /* Handle possible alignment issues */
571  memcpy(paddr, &reg->address, sizeof(*paddr));
572  if (!*paddr) {
574  "Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
575  *paddr, bit_width, bit_offset, access_size_code,
576  space_id);
577  return -EINVAL;
578  }
579 
580  if (access_size_code < 1 || access_size_code > 4) {
582  "Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
583  *paddr, bit_width, bit_offset, access_size_code,
584  space_id);
585  return -EINVAL;
586  }
587  *access_bit_width = 1UL << (access_size_code + 2);
588 
589  /* Fixup common BIOS bug */
590  if (bit_width == 32 && bit_offset == 0 && (*paddr & 0x03) == 0 &&
591  *access_bit_width < 32)
592  *access_bit_width = 32;
593 
594  if ((bit_width + bit_offset) > *access_bit_width) {
596  "Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
597  *paddr, bit_width, bit_offset, access_size_code,
598  space_id);
599  return -EINVAL;
600  }
601 
602  if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
603  space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
605  "Invalid address space type in GAR [0x%llx/%u/%u/%u/%u]\n",
606  *paddr, bit_width, bit_offset, access_size_code,
607  space_id);
608  return -EINVAL;
609  }
610 
611  return 0;
612 }
613 
615 {
616  int rc;
617  u32 access_bit_width;
618  u64 address;
619 
620  rc = apei_check_gar(reg, &address, &access_bit_width);
621  if (rc)
622  return rc;
623  return acpi_os_map_generic_address(reg);
624 }
626 
627 /* read GAR in interrupt (including NMI) or process context */
629 {
630  int rc;
631  u32 access_bit_width;
632  u64 address;
634 
635  rc = apei_check_gar(reg, &address, &access_bit_width);
636  if (rc)
637  return rc;
638 
639  *val = 0;
640  switch(reg->space_id) {
642  status = acpi_os_read_memory((acpi_physical_address) address,
643  val, access_bit_width);
644  if (ACPI_FAILURE(status))
645  return -EIO;
646  break;
648  status = acpi_os_read_port(address, (u32 *)val,
649  access_bit_width);
650  if (ACPI_FAILURE(status))
651  return -EIO;
652  break;
653  default:
654  return -EINVAL;
655  }
656 
657  return 0;
658 }
660 
661 /* write GAR in interrupt (including NMI) or process context */
663 {
664  int rc;
665  u32 access_bit_width;
666  u64 address;
668 
669  rc = apei_check_gar(reg, &address, &access_bit_width);
670  if (rc)
671  return rc;
672 
673  switch (reg->space_id) {
675  status = acpi_os_write_memory((acpi_physical_address) address,
676  val, access_bit_width);
677  if (ACPI_FAILURE(status))
678  return -EIO;
679  break;
681  status = acpi_os_write_port(address, val, access_bit_width);
682  if (ACPI_FAILURE(status))
683  return -EIO;
684  break;
685  default:
686  return -EINVAL;
687  }
688 
689  return 0;
690 }
692 
693 static int collect_res_callback(struct apei_exec_context *ctx,
694  struct acpi_whea_header *entry,
695  void *data)
696 {
697  struct apei_resources *resources = data;
698  struct acpi_generic_address *reg = &entry->register_region;
699  u8 ins = entry->instruction;
700  u32 access_bit_width;
701  u64 paddr;
702  int rc;
703 
704  if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
705  return 0;
706 
707  rc = apei_check_gar(reg, &paddr, &access_bit_width);
708  if (rc)
709  return rc;
710 
711  switch (reg->space_id) {
713  return apei_res_add(&resources->iomem, paddr,
714  access_bit_width / 8);
716  return apei_res_add(&resources->ioport, paddr,
717  access_bit_width / 8);
718  default:
719  return -EINVAL;
720  }
721 }
722 
723 /*
724  * Same register may be used by multiple instructions in GARs, so
725  * resources are collected before requesting.
726  */
728  struct apei_resources *resources)
729 {
730  return apei_exec_for_each_entry(ctx, collect_res_callback,
731  resources, NULL);
732 }
734 
736 {
737  static struct dentry *dapei;
738 
739  if (!dapei)
740  dapei = debugfs_create_dir("apei", NULL);
741 
742  return dapei;
743 }
745 
746 int apei_osc_setup(void)
747 {
748  static u8 whea_uuid_str[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
750  u32 capbuf[3];
751  struct acpi_osc_context context = {
752  .uuid_str = whea_uuid_str,
753  .rev = 1,
754  .cap.length = sizeof(capbuf),
755  .cap.pointer = capbuf,
756  };
757 
758  capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
759  capbuf[OSC_SUPPORT_TYPE] = 1;
760  capbuf[OSC_CONTROL_TYPE] = 0;
761 
762  if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))
763  || ACPI_FAILURE(acpi_run_osc(handle, &context)))
764  return -EIO;
765  else {
766  kfree(context.ret.pointer);
767  return 0;
768  }
769 }