apei-base.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. bool optional)
  138. {
  139. int rc = -ENOENT;
  140. u32 i, ip;
  141. struct acpi_whea_header *entry;
  142. apei_exec_ins_func_t run;
  143. ctx->ip = 0;
  144. /*
  145. * "ip" is the instruction pointer of current instruction,
  146. * "ctx->ip" specifies the next instruction to executed,
  147. * instruction "run" function may change the "ctx->ip" to
  148. * implement "goto" semantics.
  149. */
  150. rewind:
  151. ip = 0;
  152. for (i = 0; i < ctx->entries; i++) {
  153. entry = &ctx->action_table[i];
  154. if (entry->action != action)
  155. continue;
  156. if (ip == ctx->ip) {
  157. if (entry->instruction >= ctx->instructions ||
  158. !ctx->ins_table[entry->instruction].run) {
  159. pr_warning(FW_WARN APEI_PFX
  160. "Invalid action table, unknown instruction type: %d\n",
  161. entry->instruction);
  162. return -EINVAL;
  163. }
  164. run = ctx->ins_table[entry->instruction].run;
  165. rc = run(ctx, entry);
  166. if (rc < 0)
  167. return rc;
  168. else if (rc != APEI_EXEC_SET_IP)
  169. ctx->ip++;
  170. }
  171. ip++;
  172. if (ctx->ip < ip)
  173. goto rewind;
  174. }
  175. return !optional && rc < 0 ? rc : 0;
  176. }
  177. EXPORT_SYMBOL_GPL(__apei_exec_run);
  178. typedef int (*apei_exec_entry_func_t)(struct apei_exec_context *ctx,
  179. struct acpi_whea_header *entry,
  180. void *data);
  181. static int apei_exec_for_each_entry(struct apei_exec_context *ctx,
  182. apei_exec_entry_func_t func,
  183. void *data,
  184. int *end)
  185. {
  186. u8 ins;
  187. int i, rc;
  188. struct acpi_whea_header *entry;
  189. struct apei_exec_ins_type *ins_table = ctx->ins_table;
  190. for (i = 0; i < ctx->entries; i++) {
  191. entry = ctx->action_table + i;
  192. ins = entry->instruction;
  193. if (end)
  194. *end = i;
  195. if (ins >= ctx->instructions || !ins_table[ins].run) {
  196. pr_warning(FW_WARN APEI_PFX
  197. "Invalid action table, unknown instruction type: %d\n",
  198. ins);
  199. return -EINVAL;
  200. }
  201. rc = func(ctx, entry, data);
  202. if (rc)
  203. return rc;
  204. }
  205. return 0;
  206. }
  207. static int pre_map_gar_callback(struct apei_exec_context *ctx,
  208. struct acpi_whea_header *entry,
  209. void *data)
  210. {
  211. u8 ins = entry->instruction;
  212. if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
  213. return acpi_pre_map_gar(&entry->register_region);
  214. return 0;
  215. }
  216. /*
  217. * Pre-map all GARs in action table to make it possible to access them
  218. * in NMI handler.
  219. */
  220. int apei_exec_pre_map_gars(struct apei_exec_context *ctx)
  221. {
  222. int rc, end;
  223. rc = apei_exec_for_each_entry(ctx, pre_map_gar_callback,
  224. NULL, &end);
  225. if (rc) {
  226. struct apei_exec_context ctx_unmap;
  227. memcpy(&ctx_unmap, ctx, sizeof(*ctx));
  228. ctx_unmap.entries = end;
  229. apei_exec_post_unmap_gars(&ctx_unmap);
  230. }
  231. return rc;
  232. }
  233. EXPORT_SYMBOL_GPL(apei_exec_pre_map_gars);
  234. static int post_unmap_gar_callback(struct apei_exec_context *ctx,
  235. struct acpi_whea_header *entry,
  236. void *data)
  237. {
  238. u8 ins = entry->instruction;
  239. if (ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER)
  240. acpi_post_unmap_gar(&entry->register_region);
  241. return 0;
  242. }
  243. /* Post-unmap all GAR in action table. */
  244. int apei_exec_post_unmap_gars(struct apei_exec_context *ctx)
  245. {
  246. return apei_exec_for_each_entry(ctx, post_unmap_gar_callback,
  247. NULL, NULL);
  248. }
  249. EXPORT_SYMBOL_GPL(apei_exec_post_unmap_gars);
  250. /*
  251. * Resource management for GARs in APEI
  252. */
  253. struct apei_res {
  254. struct list_head list;
  255. unsigned long start;
  256. unsigned long end;
  257. };
  258. /* Collect all resources requested, to avoid conflict */
  259. struct apei_resources apei_resources_all = {
  260. .iomem = LIST_HEAD_INIT(apei_resources_all.iomem),
  261. .ioport = LIST_HEAD_INIT(apei_resources_all.ioport),
  262. };
  263. static int apei_res_add(struct list_head *res_list,
  264. unsigned long start, unsigned long size)
  265. {
  266. struct apei_res *res, *resn, *res_ins = NULL;
  267. unsigned long end = start + size;
  268. if (end <= start)
  269. return 0;
  270. repeat:
  271. list_for_each_entry_safe(res, resn, res_list, list) {
  272. if (res->start > end || res->end < start)
  273. continue;
  274. else if (end <= res->end && start >= res->start) {
  275. kfree(res_ins);
  276. return 0;
  277. }
  278. list_del(&res->list);
  279. res->start = start = min(res->start, start);
  280. res->end = end = max(res->end, end);
  281. kfree(res_ins);
  282. res_ins = res;
  283. goto repeat;
  284. }
  285. if (res_ins)
  286. list_add(&res_ins->list, res_list);
  287. else {
  288. res_ins = kmalloc(sizeof(*res), GFP_KERNEL);
  289. if (!res_ins)
  290. return -ENOMEM;
  291. res_ins->start = start;
  292. res_ins->end = end;
  293. list_add(&res_ins->list, res_list);
  294. }
  295. return 0;
  296. }
  297. static int apei_res_sub(struct list_head *res_list1,
  298. struct list_head *res_list2)
  299. {
  300. struct apei_res *res1, *resn1, *res2, *res;
  301. res1 = list_entry(res_list1->next, struct apei_res, list);
  302. resn1 = list_entry(res1->list.next, struct apei_res, list);
  303. while (&res1->list != res_list1) {
  304. list_for_each_entry(res2, res_list2, list) {
  305. if (res1->start >= res2->end ||
  306. res1->end <= res2->start)
  307. continue;
  308. else if (res1->end <= res2->end &&
  309. res1->start >= res2->start) {
  310. list_del(&res1->list);
  311. kfree(res1);
  312. break;
  313. } else if (res1->end > res2->end &&
  314. res1->start < res2->start) {
  315. res = kmalloc(sizeof(*res), GFP_KERNEL);
  316. if (!res)
  317. return -ENOMEM;
  318. res->start = res2->end;
  319. res->end = res1->end;
  320. res1->end = res2->start;
  321. list_add(&res->list, &res1->list);
  322. resn1 = res;
  323. } else {
  324. if (res1->start < res2->start)
  325. res1->end = res2->start;
  326. else
  327. res1->start = res2->end;
  328. }
  329. }
  330. res1 = resn1;
  331. resn1 = list_entry(resn1->list.next, struct apei_res, list);
  332. }
  333. return 0;
  334. }
  335. static void apei_res_clean(struct list_head *res_list)
  336. {
  337. struct apei_res *res, *resn;
  338. list_for_each_entry_safe(res, resn, res_list, list) {
  339. list_del(&res->list);
  340. kfree(res);
  341. }
  342. }
  343. void apei_resources_fini(struct apei_resources *resources)
  344. {
  345. apei_res_clean(&resources->iomem);
  346. apei_res_clean(&resources->ioport);
  347. }
  348. EXPORT_SYMBOL_GPL(apei_resources_fini);
  349. static int apei_resources_merge(struct apei_resources *resources1,
  350. struct apei_resources *resources2)
  351. {
  352. int rc;
  353. struct apei_res *res;
  354. list_for_each_entry(res, &resources2->iomem, list) {
  355. rc = apei_res_add(&resources1->iomem, res->start,
  356. res->end - res->start);
  357. if (rc)
  358. return rc;
  359. }
  360. list_for_each_entry(res, &resources2->ioport, list) {
  361. rc = apei_res_add(&resources1->ioport, res->start,
  362. res->end - res->start);
  363. if (rc)
  364. return rc;
  365. }
  366. return 0;
  367. }
  368. /*
  369. * EINJ has two groups of GARs (EINJ table entry and trigger table
  370. * entry), so common resources are subtracted from the trigger table
  371. * resources before the second requesting.
  372. */
  373. int apei_resources_sub(struct apei_resources *resources1,
  374. struct apei_resources *resources2)
  375. {
  376. int rc;
  377. rc = apei_res_sub(&resources1->iomem, &resources2->iomem);
  378. if (rc)
  379. return rc;
  380. return apei_res_sub(&resources1->ioport, &resources2->ioport);
  381. }
  382. EXPORT_SYMBOL_GPL(apei_resources_sub);
  383. /*
  384. * IO memory/port rersource management mechanism is used to check
  385. * whether memory/port area used by GARs conflicts with normal memory
  386. * or IO memory/port of devices.
  387. */
  388. int apei_resources_request(struct apei_resources *resources,
  389. const char *desc)
  390. {
  391. struct apei_res *res, *res_bak = NULL;
  392. struct resource *r;
  393. int rc;
  394. rc = apei_resources_sub(resources, &apei_resources_all);
  395. if (rc)
  396. return rc;
  397. rc = -EINVAL;
  398. list_for_each_entry(res, &resources->iomem, list) {
  399. r = request_mem_region(res->start, res->end - res->start,
  400. desc);
  401. if (!r) {
  402. pr_err(APEI_PFX
  403. "Can not request iomem region <%016llx-%016llx> for GARs.\n",
  404. (unsigned long long)res->start,
  405. (unsigned long long)res->end);
  406. res_bak = res;
  407. goto err_unmap_iomem;
  408. }
  409. }
  410. list_for_each_entry(res, &resources->ioport, list) {
  411. r = request_region(res->start, res->end - res->start, desc);
  412. if (!r) {
  413. pr_err(APEI_PFX
  414. "Can not request ioport region <%016llx-%016llx> for GARs.\n",
  415. (unsigned long long)res->start,
  416. (unsigned long long)res->end);
  417. res_bak = res;
  418. goto err_unmap_ioport;
  419. }
  420. }
  421. rc = apei_resources_merge(&apei_resources_all, resources);
  422. if (rc) {
  423. pr_err(APEI_PFX "Fail to merge resources!\n");
  424. goto err_unmap_ioport;
  425. }
  426. return 0;
  427. err_unmap_ioport:
  428. list_for_each_entry(res, &resources->ioport, list) {
  429. if (res == res_bak)
  430. break;
  431. release_region(res->start, res->end - res->start);
  432. }
  433. res_bak = NULL;
  434. err_unmap_iomem:
  435. list_for_each_entry(res, &resources->iomem, list) {
  436. if (res == res_bak)
  437. break;
  438. release_mem_region(res->start, res->end - res->start);
  439. }
  440. return rc;
  441. }
  442. EXPORT_SYMBOL_GPL(apei_resources_request);
  443. void apei_resources_release(struct apei_resources *resources)
  444. {
  445. int rc;
  446. struct apei_res *res;
  447. list_for_each_entry(res, &resources->iomem, list)
  448. release_mem_region(res->start, res->end - res->start);
  449. list_for_each_entry(res, &resources->ioport, list)
  450. release_region(res->start, res->end - res->start);
  451. rc = apei_resources_sub(&apei_resources_all, resources);
  452. if (rc)
  453. pr_err(APEI_PFX "Fail to sub resources!\n");
  454. }
  455. EXPORT_SYMBOL_GPL(apei_resources_release);
  456. static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr)
  457. {
  458. u32 width, space_id;
  459. width = reg->bit_width;
  460. space_id = reg->space_id;
  461. /* Handle possible alignment issues */
  462. memcpy(paddr, &reg->address, sizeof(*paddr));
  463. if (!*paddr) {
  464. pr_warning(FW_BUG APEI_PFX
  465. "Invalid physical address in GAR [0x%llx/%u/%u]\n",
  466. *paddr, width, space_id);
  467. return -EINVAL;
  468. }
  469. if ((width != 8) && (width != 16) && (width != 32) && (width != 64)) {
  470. pr_warning(FW_BUG APEI_PFX
  471. "Invalid bit width in GAR [0x%llx/%u/%u]\n",
  472. *paddr, width, space_id);
  473. return -EINVAL;
  474. }
  475. if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
  476. space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
  477. pr_warning(FW_BUG APEI_PFX
  478. "Invalid address space type in GAR [0x%llx/%u/%u]\n",
  479. *paddr, width, space_id);
  480. return -EINVAL;
  481. }
  482. return 0;
  483. }
  484. static int collect_res_callback(struct apei_exec_context *ctx,
  485. struct acpi_whea_header *entry,
  486. void *data)
  487. {
  488. struct apei_resources *resources = data;
  489. struct acpi_generic_address *reg = &entry->register_region;
  490. u8 ins = entry->instruction;
  491. u64 paddr;
  492. int rc;
  493. if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
  494. return 0;
  495. rc = apei_check_gar(reg, &paddr);
  496. if (rc)
  497. return rc;
  498. switch (reg->space_id) {
  499. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  500. return apei_res_add(&resources->iomem, paddr,
  501. reg->bit_width / 8);
  502. case ACPI_ADR_SPACE_SYSTEM_IO:
  503. return apei_res_add(&resources->ioport, paddr,
  504. reg->bit_width / 8);
  505. default:
  506. return -EINVAL;
  507. }
  508. }
  509. /*
  510. * Same register may be used by multiple instructions in GARs, so
  511. * resources are collected before requesting.
  512. */
  513. int apei_exec_collect_resources(struct apei_exec_context *ctx,
  514. struct apei_resources *resources)
  515. {
  516. return apei_exec_for_each_entry(ctx, collect_res_callback,
  517. resources, NULL);
  518. }
  519. EXPORT_SYMBOL_GPL(apei_exec_collect_resources);
  520. struct dentry *apei_get_debugfs_dir(void)
  521. {
  522. static struct dentry *dapei;
  523. if (!dapei)
  524. dapei = debugfs_create_dir("apei", NULL);
  525. return dapei;
  526. }
  527. EXPORT_SYMBOL_GPL(apei_get_debugfs_dir);
  528. int apei_osc_setup(void)
  529. {
  530. static u8 whea_uuid_str[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
  531. acpi_handle handle;
  532. u32 capbuf[3];
  533. struct acpi_osc_context context = {
  534. .uuid_str = whea_uuid_str,
  535. .rev = 1,
  536. .cap.length = sizeof(capbuf),
  537. .cap.pointer = capbuf,
  538. };
  539. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  540. capbuf[OSC_SUPPORT_TYPE] = 1;
  541. capbuf[OSC_CONTROL_TYPE] = 0;
  542. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))
  543. || ACPI_FAILURE(acpi_run_osc(handle, &context)))
  544. return -EIO;
  545. else {
  546. kfree(context.ret.pointer);
  547. return 0;
  548. }
  549. }
  550. EXPORT_SYMBOL_GPL(apei_osc_setup);