apei-base.c 15 KB

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