apei-base.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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 <ying.huang@intel.com>
  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. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/acpi.h>
  36. #include <linux/io.h>
  37. #include <linux/kref.h>
  38. #include <linux/rculist.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/debugfs.h>
  41. #include <acpi/atomicio.h>
  42. #include "apei-internal.h"
  43. #define APEI_PFX "APEI: "
  44. /*
  45. * APEI ERST (Error Record Serialization Table) and EINJ (Error
  46. * INJection) interpreter framework.
  47. */
  48. #define APEI_EXEC_PRESERVE_REGISTER 0x1
  49. void apei_exec_ctx_init(struct apei_exec_context *ctx,
  50. struct apei_exec_ins_type *ins_table,
  51. u32 instructions,
  52. struct acpi_whea_header *action_table,
  53. u32 entries)
  54. {
  55. ctx->ins_table = ins_table;
  56. ctx->instructions = instructions;
  57. ctx->action_table = action_table;
  58. ctx->entries = entries;
  59. }
  60. EXPORT_SYMBOL_GPL(apei_exec_ctx_init);
  61. int __apei_exec_read_register(struct acpi_whea_header *entry, u64 *val)
  62. {
  63. int rc;
  64. rc = acpi_atomic_read(val, &entry->register_region);
  65. if (rc)
  66. return rc;
  67. *val >>= entry->register_region.bit_offset;
  68. *val &= entry->mask;
  69. return 0;
  70. }
  71. int apei_exec_read_register(struct apei_exec_context *ctx,
  72. struct acpi_whea_header *entry)
  73. {
  74. int rc;
  75. u64 val = 0;
  76. rc = __apei_exec_read_register(entry, &val);
  77. if (rc)
  78. return rc;
  79. ctx->value = val;
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(apei_exec_read_register);
  83. int apei_exec_read_register_value(struct apei_exec_context *ctx,
  84. struct acpi_whea_header *entry)
  85. {
  86. int rc;
  87. rc = apei_exec_read_register(ctx, entry);
  88. if (rc)
  89. return rc;
  90. ctx->value = (ctx->value == entry->value);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL_GPL(apei_exec_read_register_value);
  94. int __apei_exec_write_register(struct acpi_whea_header *entry, u64 val)
  95. {
  96. int rc;
  97. val &= entry->mask;
  98. val <<= entry->register_region.bit_offset;
  99. if (entry->flags & APEI_EXEC_PRESERVE_REGISTER) {
  100. u64 valr = 0;
  101. rc = acpi_atomic_read(&valr, &entry->register_region);
  102. if (rc)
  103. return rc;
  104. valr &= ~(entry->mask << entry->register_region.bit_offset);
  105. val |= valr;
  106. }
  107. rc = acpi_atomic_write(val, &entry->register_region);
  108. return rc;
  109. }
  110. int apei_exec_write_register(struct apei_exec_context *ctx,
  111. struct acpi_whea_header *entry)
  112. {
  113. return __apei_exec_write_register(entry, ctx->value);
  114. }
  115. EXPORT_SYMBOL_GPL(apei_exec_write_register);
  116. int apei_exec_write_register_value(struct apei_exec_context *ctx,
  117. struct acpi_whea_header *entry)
  118. {
  119. int rc;
  120. ctx->value = entry->value;
  121. rc = apei_exec_write_register(ctx, entry);
  122. return rc;
  123. }
  124. EXPORT_SYMBOL_GPL(apei_exec_write_register_value);
  125. int apei_exec_noop(struct apei_exec_context *ctx,
  126. struct acpi_whea_header *entry)
  127. {
  128. return 0;
  129. }
  130. EXPORT_SYMBOL_GPL(apei_exec_noop);
  131. /*
  132. * Interpret the specified action. Go through whole action table,
  133. * execute all instructions belong to the action.
  134. */
  135. int apei_exec_run(struct apei_exec_context *ctx, u8 action)
  136. {
  137. int rc;
  138. u32 i, ip;
  139. struct acpi_whea_header *entry;
  140. apei_exec_ins_func_t run;
  141. ctx->ip = 0;
  142. /*
  143. * "ip" is the instruction pointer of current instruction,
  144. * "ctx->ip" specifies the next instruction to executed,
  145. * instruction "run" function may change the "ctx->ip" to
  146. * implement "goto" semantics.
  147. */
  148. rewind:
  149. ip = 0;
  150. for (i = 0; i < ctx->entries; i++) {
  151. entry = &ctx->action_table[i];
  152. if (entry->action != action)
  153. continue;
  154. if (ip == ctx->ip) {
  155. if (entry->instruction >= ctx->instructions ||
  156. !ctx->ins_table[entry->instruction].run) {
  157. pr_warning(FW_WARN APEI_PFX
  158. "Invalid action table, unknown instruction type: %d\n",
  159. entry->instruction);
  160. return -EINVAL;
  161. }
  162. run = ctx->ins_table[entry->instruction].run;
  163. rc = run(ctx, entry);
  164. if (rc < 0)
  165. return rc;
  166. else if (rc != APEI_EXEC_SET_IP)
  167. ctx->ip++;
  168. }
  169. ip++;
  170. if (ctx->ip < ip)
  171. goto rewind;
  172. }
  173. return 0;
  174. }
  175. EXPORT_SYMBOL_GPL(apei_exec_run);
  176. typedef int (*apei_exec_entry_func_t)(struct apei_exec_context *ctx,
  177. struct acpi_whea_header *entry,
  178. void *data);
  179. static int apei_exec_for_each_entry(struct apei_exec_context *ctx,
  180. apei_exec_entry_func_t func,
  181. void *data,
  182. int *end)
  183. {
  184. u8 ins;
  185. int i, rc;
  186. struct acpi_whea_header *entry;
  187. struct apei_exec_ins_type *ins_table = ctx->ins_table;
  188. for (i = 0; i < ctx->entries; i++) {
  189. entry = ctx->action_table + i;
  190. ins = entry->instruction;
  191. if (end)
  192. *end = i;
  193. if (ins >= ctx->instructions || !ins_table[ins].run) {
  194. pr_warning(FW_WARN APEI_PFX
  195. "Invalid action table, unknown instruction type: %d\n",
  196. ins);
  197. return -EINVAL;
  198. }
  199. rc = func(ctx, entry, data);
  200. if (rc)
  201. return rc;
  202. }
  203. return 0;
  204. }
  205. static int pre_map_gar_callback(struct apei_exec_context *ctx,
  206. struct acpi_whea_header *entry,
  207. void *data)
  208. {
  209. u8 ins = entry->instruction;
  210. if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
  211. return acpi_pre_map_gar(&entry->register_region);
  212. return 0;
  213. }
  214. /*
  215. * Pre-map all GARs in action table to make it possible to access them
  216. * in NMI handler.
  217. */
  218. int apei_exec_pre_map_gars(struct apei_exec_context *ctx)
  219. {
  220. int rc, end;
  221. rc = apei_exec_for_each_entry(ctx, pre_map_gar_callback,
  222. NULL, &end);
  223. if (rc) {
  224. struct apei_exec_context ctx_unmap;
  225. memcpy(&ctx_unmap, ctx, sizeof(*ctx));
  226. ctx_unmap.entries = end;
  227. apei_exec_post_unmap_gars(&ctx_unmap);
  228. }
  229. return rc;
  230. }
  231. EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars);
  232. static int post_unmap_gar_callback(struct apei_exec_context *ctx,
  233. struct acpi_whea_header *entry,
  234. void *data)
  235. {
  236. u8 ins = entry->instruction;
  237. if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
  238. acpi_post_unmap_gar(&entry->register_region);
  239. return 0;
  240. }
  241. /* Post-unmap all GAR in action table. */
  242. int apei_exec_post_unmap_gars(struct apei_exec_context *ctx)
  243. {
  244. return apei_exec_for_each_entry(ctx, post_unmap_gar_callback,
  245. NULL, NULL);
  246. }
  247. EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars);
  248. /*
  249. * Resource management for GARs in APEI
  250. */
  251. struct apei_res {
  252. struct list_head list;
  253. unsigned long start;
  254. unsigned long end;
  255. };
  256. /* Collect all resources requested, to avoid conflict */
  257. struct apei_resources apei_resources_all = {
  258. .iomem = LIST_HEAD_INIT(apei_resources_all.iomem),
  259. .ioport = LIST_HEAD_INIT(apei_resources_all.ioport),
  260. };
  261. static int apei_res_add(struct list_head *res_list,
  262. unsigned long start, unsigned long size)
  263. {
  264. struct apei_res *res, *resn, *res_ins = NULL;
  265. unsigned long end = start + size;
  266. if (end <= start)
  267. return 0;
  268. repeat:
  269. list_for_each_entry_safe(res, resn, res_list, list) {
  270. if (res->start > end || res->end < start)
  271. continue;
  272. else if (end <= res->end && start >= res->start) {
  273. kfree(res_ins);
  274. return 0;
  275. }
  276. list_del(&res->list);
  277. res->start = start = min(res->start, start);
  278. res->end = end = max(res->end, end);
  279. kfree(res_ins);
  280. res_ins = res;
  281. goto repeat;
  282. }
  283. if (res_ins)
  284. list_add(&res_ins->list, res_list);
  285. else {
  286. res_ins = kmalloc(sizeof(*res), GFP_KERNEL);
  287. if (!res_ins)
  288. return -ENOMEM;
  289. res_ins->start = start;
  290. res_ins->end = end;
  291. list_add(&res_ins->list, res_list);
  292. }
  293. return 0;
  294. }
  295. static int apei_res_sub(struct list_head *res_list1,
  296. struct list_head *res_list2)
  297. {
  298. struct apei_res *res1, *resn1, *res2, *res;
  299. res1 = list_entry(res_list1->next, struct apei_res, list);
  300. resn1 = list_entry(res1->list.next, struct apei_res, list);
  301. while (&res1->list != res_list1) {
  302. list_for_each_entry(res2, res_list2, list) {
  303. if (res1->start >= res2->end ||
  304. res1->end <= res2->start)
  305. continue;
  306. else if (res1->end <= res2->end &&
  307. res1->start >= res2->start) {
  308. list_del(&res1->list);
  309. kfree(res1);
  310. break;
  311. } else if (res1->end > res2->end &&
  312. res1->start < res2->start) {
  313. res = kmalloc(sizeof(*res), GFP_KERNEL);
  314. if (!res)
  315. return -ENOMEM;
  316. res->start = res2->end;
  317. res->end = res1->end;
  318. res1->end = res2->start;
  319. list_add(&res->list, &res1->list);
  320. resn1 = res;
  321. } else {
  322. if (res1->start < res2->start)
  323. res1->end = res2->start;
  324. else
  325. res1->start = res2->end;
  326. }
  327. }
  328. res1 = resn1;
  329. resn1 = list_entry(resn1->list.next, struct apei_res, list);
  330. }
  331. return 0;
  332. }
  333. static void apei_res_clean(struct list_head *res_list)
  334. {
  335. struct apei_res *res, *resn;
  336. list_for_each_entry_safe(res, resn, res_list, list) {
  337. list_del(&res->list);
  338. kfree(res);
  339. }
  340. }
  341. void apei_resources_fini(struct apei_resources *resources)
  342. {
  343. apei_res_clean(&resources->iomem);
  344. apei_res_clean(&resources->ioport);
  345. }
  346. EXPORT_SYMBOL_GPL(apei_resources_fini);
  347. static int apei_resources_merge(struct apei_resources *resources1,
  348. struct apei_resources *resources2)
  349. {
  350. int rc;
  351. struct apei_res *res;
  352. list_for_each_entry(res, &resources2->iomem, list) {
  353. rc = apei_res_add(&resources1->iomem, res->start,
  354. res->end - res->start);
  355. if (rc)
  356. return rc;
  357. }
  358. list_for_each_entry(res, &resources2->ioport, list) {
  359. rc = apei_res_add(&resources1->ioport, res->start,
  360. res->end - res->start);
  361. if (rc)
  362. return rc;
  363. }
  364. return 0;
  365. }
  366. /*
  367. * EINJ has two groups of GARs (EINJ table entry and trigger table
  368. * entry), so common resources are subtracted from the trigger table
  369. * resources before the second requesting.
  370. */
  371. int apei_resources_sub(struct apei_resources *resources1,
  372. struct apei_resources *resources2)
  373. {
  374. int rc;
  375. rc = apei_res_sub(&resources1->iomem, &resources2->iomem);
  376. if (rc)
  377. return rc;
  378. return apei_res_sub(&resources1->ioport, &resources2->ioport);
  379. }
  380. EXPORT_SYMBOL_GPL(apei_resources_sub);
  381. /*
  382. * IO memory/port rersource management mechanism is used to check
  383. * whether memory/port area used by GARs conflicts with normal memory
  384. * or IO memory/port of devices.
  385. */
  386. int apei_resources_request(struct apei_resources *resources,
  387. const char *desc)
  388. {
  389. struct apei_res *res, *res_bak;
  390. struct resource *r;
  391. apei_resources_sub(resources, &apei_resources_all);
  392. list_for_each_entry(res, &resources->iomem, list) {
  393. r = request_mem_region(res->start, res->end - res->start,
  394. desc);
  395. if (!r) {
  396. pr_err(APEI_PFX
  397. "Can not request iomem region <%016llx-%016llx> for GARs.\n",
  398. (unsigned long long)res->start,
  399. (unsigned long long)res->end);
  400. res_bak = res;
  401. goto err_unmap_iomem;
  402. }
  403. }
  404. list_for_each_entry(res, &resources->ioport, list) {
  405. r = request_region(res->start, res->end - res->start, desc);
  406. if (!r) {
  407. pr_err(APEI_PFX
  408. "Can not request ioport region <%016llx-%016llx> for GARs.\n",
  409. (unsigned long long)res->start,
  410. (unsigned long long)res->end);
  411. res_bak = res;
  412. goto err_unmap_ioport;
  413. }
  414. }
  415. apei_resources_merge(&apei_resources_all, resources);
  416. return 0;
  417. err_unmap_ioport:
  418. list_for_each_entry(res, &resources->ioport, list) {
  419. if (res == res_bak)
  420. break;
  421. release_mem_region(res->start, res->end - res->start);
  422. }
  423. res_bak = NULL;
  424. err_unmap_iomem:
  425. list_for_each_entry(res, &resources->iomem, list) {
  426. if (res == res_bak)
  427. break;
  428. release_region(res->start, res->end - res->start);
  429. }
  430. return -EINVAL;
  431. }
  432. EXPORT_SYMBOL_GPL(apei_resources_request);
  433. void apei_resources_release(struct apei_resources *resources)
  434. {
  435. struct apei_res *res;
  436. list_for_each_entry(res, &resources->iomem, list)
  437. release_mem_region(res->start, res->end - res->start);
  438. list_for_each_entry(res, &resources->ioport, list)
  439. release_region(res->start, res->end - res->start);
  440. apei_resources_sub(&apei_resources_all, resources);
  441. }
  442. EXPORT_SYMBOL_GPL(apei_resources_release);
  443. static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr)
  444. {
  445. u32 width, space_id;
  446. width = reg->bit_width;
  447. space_id = reg->space_id;
  448. /* Handle possible alignment issues */
  449. memcpy(paddr, &reg->address, sizeof(*paddr));
  450. if (!*paddr) {
  451. pr_warning(FW_BUG APEI_PFX
  452. "Invalid physical address in GAR [0x%llx/%u/%u]\n",
  453. *paddr, width, space_id);
  454. return -EINVAL;
  455. }
  456. if ((width != 8) && (width != 16) && (width != 32) && (width != 64)) {
  457. pr_warning(FW_BUG APEI_PFX
  458. "Invalid bit width in GAR [0x%llx/%u/%u]\n",
  459. *paddr, width, space_id);
  460. return -EINVAL;
  461. }
  462. if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
  463. space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
  464. pr_warning(FW_BUG APEI_PFX
  465. "Invalid address space type in GAR [0x%llx/%u/%u]\n",
  466. *paddr, width, space_id);
  467. return -EINVAL;
  468. }
  469. return 0;
  470. }
  471. static int collect_res_callback(struct apei_exec_context *ctx,
  472. struct acpi_whea_header *entry,
  473. void *data)
  474. {
  475. struct apei_resources *resources = data;
  476. struct acpi_generic_address *reg = &entry->register_region;
  477. u8 ins = entry->instruction;
  478. u64 paddr;
  479. int rc;
  480. if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
  481. return 0;
  482. rc = apei_check_gar(reg, &paddr);
  483. if (rc)
  484. return rc;
  485. switch (reg->space_id) {
  486. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  487. return apei_res_add(&resources->iomem, paddr,
  488. reg->bit_width / 8);
  489. case ACPI_ADR_SPACE_SYSTEM_IO:
  490. return apei_res_add(&resources->ioport, paddr,
  491. reg->bit_width / 8);
  492. default:
  493. return -EINVAL;
  494. }
  495. }
  496. /*
  497. * Same register may be used by multiple instructions in GARs, so
  498. * resources are collected before requesting.
  499. */
  500. int apei_exec_collect_resources(struct apei_exec_context *ctx,
  501. struct apei_resources *resources)
  502. {
  503. return apei_exec_for_each_entry(ctx, collect_res_callback,
  504. resources, NULL);
  505. }
  506. EXPORT_SYMBOL_GPL(apei_exec_collect_resources);
  507. struct dentry *apei_get_debugfs_dir(void)
  508. {
  509. static struct dentry *dapei;
  510. if (!dapei)
  511. dapei = debugfs_create_dir("apei", NULL);
  512. return dapei;
  513. }
  514. EXPORT_SYMBOL_GPL(apei_get_debugfs_dir);