apei-base.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 <asm/unaligned.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 = apei_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 = apei_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 = apei_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 apei_map_generic_address(&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. apei_unmap_generic_address(&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. int apei_resources_add(struct apei_resources *resources,
  369. unsigned long start, unsigned long size,
  370. bool iomem)
  371. {
  372. if (iomem)
  373. return apei_res_add(&resources->iomem, start, size);
  374. else
  375. return apei_res_add(&resources->ioport, start, size);
  376. }
  377. EXPORT_SYMBOL_GPL(apei_resources_add);
  378. /*
  379. * EINJ has two groups of GARs (EINJ table entry and trigger table
  380. * entry), so common resources are subtracted from the trigger table
  381. * resources before the second requesting.
  382. */
  383. int apei_resources_sub(struct apei_resources *resources1,
  384. struct apei_resources *resources2)
  385. {
  386. int rc;
  387. rc = apei_res_sub(&resources1->iomem, &resources2->iomem);
  388. if (rc)
  389. return rc;
  390. return apei_res_sub(&resources1->ioport, &resources2->ioport);
  391. }
  392. EXPORT_SYMBOL_GPL(apei_resources_sub);
  393. static int apei_get_res_callback(__u64 start, __u64 size, void *data)
  394. {
  395. struct apei_resources *resources = data;
  396. return apei_res_add(&resources->iomem, start, size);
  397. }
  398. static int apei_get_nvs_resources(struct apei_resources *resources)
  399. {
  400. return acpi_nvs_for_each_region(apei_get_res_callback, resources);
  401. }
  402. int (*arch_apei_filter_addr)(int (*func)(__u64 start, __u64 size,
  403. void *data), void *data);
  404. static int apei_get_arch_resources(struct apei_resources *resources)
  405. {
  406. return arch_apei_filter_addr(apei_get_res_callback, resources);
  407. }
  408. /*
  409. * IO memory/port resource management mechanism is used to check
  410. * whether memory/port area used by GARs conflicts with normal memory
  411. * or IO memory/port of devices.
  412. */
  413. int apei_resources_request(struct apei_resources *resources,
  414. const char *desc)
  415. {
  416. struct apei_res *res, *res_bak = NULL;
  417. struct resource *r;
  418. struct apei_resources nvs_resources, arch_res;
  419. int rc;
  420. rc = apei_resources_sub(resources, &apei_resources_all);
  421. if (rc)
  422. return rc;
  423. /*
  424. * Some firmware uses ACPI NVS region, that has been marked as
  425. * busy, so exclude it from APEI resources to avoid false
  426. * conflict.
  427. */
  428. apei_resources_init(&nvs_resources);
  429. rc = apei_get_nvs_resources(&nvs_resources);
  430. if (rc)
  431. goto nvs_res_fini;
  432. rc = apei_resources_sub(resources, &nvs_resources);
  433. if (rc)
  434. goto nvs_res_fini;
  435. if (arch_apei_filter_addr) {
  436. apei_resources_init(&arch_res);
  437. rc = apei_get_arch_resources(&arch_res);
  438. if (rc)
  439. goto arch_res_fini;
  440. rc = apei_resources_sub(resources, &arch_res);
  441. if (rc)
  442. goto arch_res_fini;
  443. }
  444. rc = -EINVAL;
  445. list_for_each_entry(res, &resources->iomem, list) {
  446. r = request_mem_region(res->start, res->end - res->start,
  447. desc);
  448. if (!r) {
  449. pr_err(APEI_PFX
  450. "Can not request [mem %#010llx-%#010llx] for %s registers\n",
  451. (unsigned long long)res->start,
  452. (unsigned long long)res->end - 1, desc);
  453. res_bak = res;
  454. goto err_unmap_iomem;
  455. }
  456. }
  457. list_for_each_entry(res, &resources->ioport, list) {
  458. r = request_region(res->start, res->end - res->start, desc);
  459. if (!r) {
  460. pr_err(APEI_PFX
  461. "Can not request [io %#06llx-%#06llx] for %s registers\n",
  462. (unsigned long long)res->start,
  463. (unsigned long long)res->end - 1, desc);
  464. res_bak = res;
  465. goto err_unmap_ioport;
  466. }
  467. }
  468. rc = apei_resources_merge(&apei_resources_all, resources);
  469. if (rc) {
  470. pr_err(APEI_PFX "Fail to merge resources!\n");
  471. goto err_unmap_ioport;
  472. }
  473. return 0;
  474. err_unmap_ioport:
  475. list_for_each_entry(res, &resources->ioport, list) {
  476. if (res == res_bak)
  477. break;
  478. release_region(res->start, res->end - res->start);
  479. }
  480. res_bak = NULL;
  481. err_unmap_iomem:
  482. list_for_each_entry(res, &resources->iomem, list) {
  483. if (res == res_bak)
  484. break;
  485. release_mem_region(res->start, res->end - res->start);
  486. }
  487. arch_res_fini:
  488. apei_resources_fini(&arch_res);
  489. nvs_res_fini:
  490. apei_resources_fini(&nvs_resources);
  491. return rc;
  492. }
  493. EXPORT_SYMBOL_GPL(apei_resources_request);
  494. void apei_resources_release(struct apei_resources *resources)
  495. {
  496. int rc;
  497. struct apei_res *res;
  498. list_for_each_entry(res, &resources->iomem, list)
  499. release_mem_region(res->start, res->end - res->start);
  500. list_for_each_entry(res, &resources->ioport, list)
  501. release_region(res->start, res->end - res->start);
  502. rc = apei_resources_sub(&apei_resources_all, resources);
  503. if (rc)
  504. pr_err(APEI_PFX "Fail to sub resources!\n");
  505. }
  506. EXPORT_SYMBOL_GPL(apei_resources_release);
  507. static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr,
  508. u32 *access_bit_width)
  509. {
  510. u32 bit_width, bit_offset, access_size_code, space_id;
  511. bit_width = reg->bit_width;
  512. bit_offset = reg->bit_offset;
  513. access_size_code = reg->access_width;
  514. space_id = reg->space_id;
  515. *paddr = get_unaligned(&reg->address);
  516. if (!*paddr) {
  517. pr_warning(FW_BUG APEI_PFX
  518. "Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
  519. *paddr, bit_width, bit_offset, access_size_code,
  520. space_id);
  521. return -EINVAL;
  522. }
  523. if (access_size_code < 1 || access_size_code > 4) {
  524. pr_warning(FW_BUG APEI_PFX
  525. "Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
  526. *paddr, bit_width, bit_offset, access_size_code,
  527. space_id);
  528. return -EINVAL;
  529. }
  530. *access_bit_width = 1UL << (access_size_code + 2);
  531. /* Fixup common BIOS bug */
  532. if (bit_width == 32 && bit_offset == 0 && (*paddr & 0x03) == 0 &&
  533. *access_bit_width < 32)
  534. *access_bit_width = 32;
  535. else if (bit_width == 64 && bit_offset == 0 && (*paddr & 0x07) == 0 &&
  536. *access_bit_width < 64)
  537. *access_bit_width = 64;
  538. if ((bit_width + bit_offset) > *access_bit_width) {
  539. pr_warning(FW_BUG APEI_PFX
  540. "Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
  541. *paddr, bit_width, bit_offset, access_size_code,
  542. space_id);
  543. return -EINVAL;
  544. }
  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/%u/%u]\n",
  549. *paddr, bit_width, bit_offset, access_size_code,
  550. space_id);
  551. return -EINVAL;
  552. }
  553. return 0;
  554. }
  555. int apei_map_generic_address(struct acpi_generic_address *reg)
  556. {
  557. int rc;
  558. u32 access_bit_width;
  559. u64 address;
  560. rc = apei_check_gar(reg, &address, &access_bit_width);
  561. if (rc)
  562. return rc;
  563. return acpi_os_map_generic_address(reg);
  564. }
  565. EXPORT_SYMBOL_GPL(apei_map_generic_address);
  566. /* read GAR in interrupt (including NMI) or process context */
  567. int apei_read(u64 *val, struct acpi_generic_address *reg)
  568. {
  569. int rc;
  570. u32 access_bit_width;
  571. u64 address;
  572. acpi_status status;
  573. rc = apei_check_gar(reg, &address, &access_bit_width);
  574. if (rc)
  575. return rc;
  576. *val = 0;
  577. switch(reg->space_id) {
  578. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  579. status = acpi_os_read_memory((acpi_physical_address) address,
  580. val, access_bit_width);
  581. if (ACPI_FAILURE(status))
  582. return -EIO;
  583. break;
  584. case ACPI_ADR_SPACE_SYSTEM_IO:
  585. status = acpi_os_read_port(address, (u32 *)val,
  586. access_bit_width);
  587. if (ACPI_FAILURE(status))
  588. return -EIO;
  589. break;
  590. default:
  591. return -EINVAL;
  592. }
  593. return 0;
  594. }
  595. EXPORT_SYMBOL_GPL(apei_read);
  596. /* write GAR in interrupt (including NMI) or process context */
  597. int apei_write(u64 val, struct acpi_generic_address *reg)
  598. {
  599. int rc;
  600. u32 access_bit_width;
  601. u64 address;
  602. acpi_status status;
  603. rc = apei_check_gar(reg, &address, &access_bit_width);
  604. if (rc)
  605. return rc;
  606. switch (reg->space_id) {
  607. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  608. status = acpi_os_write_memory((acpi_physical_address) address,
  609. val, access_bit_width);
  610. if (ACPI_FAILURE(status))
  611. return -EIO;
  612. break;
  613. case ACPI_ADR_SPACE_SYSTEM_IO:
  614. status = acpi_os_write_port(address, val, access_bit_width);
  615. if (ACPI_FAILURE(status))
  616. return -EIO;
  617. break;
  618. default:
  619. return -EINVAL;
  620. }
  621. return 0;
  622. }
  623. EXPORT_SYMBOL_GPL(apei_write);
  624. static int collect_res_callback(struct apei_exec_context *ctx,
  625. struct acpi_whea_header *entry,
  626. void *data)
  627. {
  628. struct apei_resources *resources = data;
  629. struct acpi_generic_address *reg = &entry->register_region;
  630. u8 ins = entry->instruction;
  631. u32 access_bit_width;
  632. u64 paddr;
  633. int rc;
  634. if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
  635. return 0;
  636. rc = apei_check_gar(reg, &paddr, &access_bit_width);
  637. if (rc)
  638. return rc;
  639. switch (reg->space_id) {
  640. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  641. return apei_res_add(&resources->iomem, paddr,
  642. access_bit_width / 8);
  643. case ACPI_ADR_SPACE_SYSTEM_IO:
  644. return apei_res_add(&resources->ioport, paddr,
  645. access_bit_width / 8);
  646. default:
  647. return -EINVAL;
  648. }
  649. }
  650. /*
  651. * Same register may be used by multiple instructions in GARs, so
  652. * resources are collected before requesting.
  653. */
  654. int apei_exec_collect_resources(struct apei_exec_context *ctx,
  655. struct apei_resources *resources)
  656. {
  657. return apei_exec_for_each_entry(ctx, collect_res_callback,
  658. resources, NULL);
  659. }
  660. EXPORT_SYMBOL_GPL(apei_exec_collect_resources);
  661. struct dentry *apei_get_debugfs_dir(void)
  662. {
  663. static struct dentry *dapei;
  664. if (!dapei)
  665. dapei = debugfs_create_dir("apei", NULL);
  666. return dapei;
  667. }
  668. EXPORT_SYMBOL_GPL(apei_get_debugfs_dir);
  669. int __weak arch_apei_enable_cmcff(struct acpi_hest_header *hest_hdr,
  670. void *data)
  671. {
  672. return 1;
  673. }
  674. EXPORT_SYMBOL_GPL(arch_apei_enable_cmcff);
  675. void __weak arch_apei_report_mem_error(int sev,
  676. struct cper_sec_mem_err *mem_err)
  677. {
  678. }
  679. EXPORT_SYMBOL_GPL(arch_apei_report_mem_error);
  680. int apei_osc_setup(void)
  681. {
  682. static u8 whea_uuid_str[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
  683. acpi_handle handle;
  684. u32 capbuf[3];
  685. struct acpi_osc_context context = {
  686. .uuid_str = whea_uuid_str,
  687. .rev = 1,
  688. .cap.length = sizeof(capbuf),
  689. .cap.pointer = capbuf,
  690. };
  691. capbuf[OSC_QUERY_DWORD] = OSC_QUERY_ENABLE;
  692. capbuf[OSC_SUPPORT_DWORD] = 1;
  693. capbuf[OSC_CONTROL_DWORD] = 0;
  694. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))
  695. || ACPI_FAILURE(acpi_run_osc(handle, &context)))
  696. return -EIO;
  697. else {
  698. kfree(context.ret.pointer);
  699. return 0;
  700. }
  701. }
  702. EXPORT_SYMBOL_GPL(apei_osc_setup);