apei-base.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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/acpi_io.h>
  37. #include <linux/slab.h>
  38. #include <linux/io.h>
  39. #include <linux/kref.h>
  40. #include <linux/rculist.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/debugfs.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_nvs_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_nvs_callback, resources);
  401. }
  402. /*
  403. * IO memory/port resource management mechanism is used to check
  404. * whether memory/port area used by GARs conflicts with normal memory
  405. * or IO memory/port of devices.
  406. */
  407. int apei_resources_request(struct apei_resources *resources,
  408. const char *desc)
  409. {
  410. struct apei_res *res, *res_bak = NULL;
  411. struct resource *r;
  412. struct apei_resources nvs_resources;
  413. int rc;
  414. rc = apei_resources_sub(resources, &apei_resources_all);
  415. if (rc)
  416. return rc;
  417. /*
  418. * Some firmware uses ACPI NVS region, that has been marked as
  419. * busy, so exclude it from APEI resources to avoid false
  420. * conflict.
  421. */
  422. apei_resources_init(&nvs_resources);
  423. rc = apei_get_nvs_resources(&nvs_resources);
  424. if (rc)
  425. goto res_fini;
  426. rc = apei_resources_sub(resources, &nvs_resources);
  427. if (rc)
  428. goto res_fini;
  429. rc = -EINVAL;
  430. list_for_each_entry(res, &resources->iomem, list) {
  431. r = request_mem_region(res->start, res->end - res->start,
  432. desc);
  433. if (!r) {
  434. pr_err(APEI_PFX
  435. "Can not request [mem %#010llx-%#010llx] for %s registers\n",
  436. (unsigned long long)res->start,
  437. (unsigned long long)res->end - 1, desc);
  438. res_bak = res;
  439. goto err_unmap_iomem;
  440. }
  441. }
  442. list_for_each_entry(res, &resources->ioport, list) {
  443. r = request_region(res->start, res->end - res->start, desc);
  444. if (!r) {
  445. pr_err(APEI_PFX
  446. "Can not request [io %#06llx-%#06llx] for %s registers\n",
  447. (unsigned long long)res->start,
  448. (unsigned long long)res->end - 1, desc);
  449. res_bak = res;
  450. goto err_unmap_ioport;
  451. }
  452. }
  453. rc = apei_resources_merge(&apei_resources_all, resources);
  454. if (rc) {
  455. pr_err(APEI_PFX "Fail to merge resources!\n");
  456. goto err_unmap_ioport;
  457. }
  458. return 0;
  459. err_unmap_ioport:
  460. list_for_each_entry(res, &resources->ioport, list) {
  461. if (res == res_bak)
  462. break;
  463. release_region(res->start, res->end - res->start);
  464. }
  465. res_bak = NULL;
  466. err_unmap_iomem:
  467. list_for_each_entry(res, &resources->iomem, list) {
  468. if (res == res_bak)
  469. break;
  470. release_mem_region(res->start, res->end - res->start);
  471. }
  472. res_fini:
  473. apei_resources_fini(&nvs_resources);
  474. return rc;
  475. }
  476. EXPORT_SYMBOL_GPL(apei_resources_request);
  477. void apei_resources_release(struct apei_resources *resources)
  478. {
  479. int rc;
  480. struct apei_res *res;
  481. list_for_each_entry(res, &resources->iomem, list)
  482. release_mem_region(res->start, res->end - res->start);
  483. list_for_each_entry(res, &resources->ioport, list)
  484. release_region(res->start, res->end - res->start);
  485. rc = apei_resources_sub(&apei_resources_all, resources);
  486. if (rc)
  487. pr_err(APEI_PFX "Fail to sub resources!\n");
  488. }
  489. EXPORT_SYMBOL_GPL(apei_resources_release);
  490. static int apei_check_gar(struct acpi_generic_address *reg, u64 *paddr,
  491. u32 *access_bit_width)
  492. {
  493. u32 bit_width, bit_offset, access_size_code, space_id;
  494. bit_width = reg->bit_width;
  495. bit_offset = reg->bit_offset;
  496. access_size_code = reg->access_width;
  497. space_id = reg->space_id;
  498. /* Handle possible alignment issues */
  499. memcpy(paddr, &reg->address, sizeof(*paddr));
  500. if (!*paddr) {
  501. pr_warning(FW_BUG APEI_PFX
  502. "Invalid physical address in GAR [0x%llx/%u/%u/%u/%u]\n",
  503. *paddr, bit_width, bit_offset, access_size_code,
  504. space_id);
  505. return -EINVAL;
  506. }
  507. if (access_size_code < 1 || access_size_code > 4) {
  508. pr_warning(FW_BUG APEI_PFX
  509. "Invalid access size code in GAR [0x%llx/%u/%u/%u/%u]\n",
  510. *paddr, bit_width, bit_offset, access_size_code,
  511. space_id);
  512. return -EINVAL;
  513. }
  514. *access_bit_width = 1UL << (access_size_code + 2);
  515. /* Fixup common BIOS bug */
  516. if (bit_width == 32 && bit_offset == 0 && (*paddr & 0x03) == 0 &&
  517. *access_bit_width < 32)
  518. *access_bit_width = 32;
  519. if ((bit_width + bit_offset) > *access_bit_width) {
  520. pr_warning(FW_BUG APEI_PFX
  521. "Invalid bit width + offset in GAR [0x%llx/%u/%u/%u/%u]\n",
  522. *paddr, bit_width, bit_offset, access_size_code,
  523. space_id);
  524. return -EINVAL;
  525. }
  526. if (space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY &&
  527. space_id != ACPI_ADR_SPACE_SYSTEM_IO) {
  528. pr_warning(FW_BUG APEI_PFX
  529. "Invalid address space type in GAR [0x%llx/%u/%u/%u/%u]\n",
  530. *paddr, bit_width, bit_offset, access_size_code,
  531. space_id);
  532. return -EINVAL;
  533. }
  534. return 0;
  535. }
  536. int apei_map_generic_address(struct acpi_generic_address *reg)
  537. {
  538. int rc;
  539. u32 access_bit_width;
  540. u64 address;
  541. rc = apei_check_gar(reg, &address, &access_bit_width);
  542. if (rc)
  543. return rc;
  544. return acpi_os_map_generic_address(reg);
  545. }
  546. EXPORT_SYMBOL_GPL(apei_map_generic_address);
  547. /* read GAR in interrupt (including NMI) or process context */
  548. int apei_read(u64 *val, struct acpi_generic_address *reg)
  549. {
  550. int rc;
  551. u32 access_bit_width;
  552. u64 address;
  553. acpi_status status;
  554. rc = apei_check_gar(reg, &address, &access_bit_width);
  555. if (rc)
  556. return rc;
  557. *val = 0;
  558. switch(reg->space_id) {
  559. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  560. status = acpi_os_read_memory((acpi_physical_address) address,
  561. val, access_bit_width);
  562. if (ACPI_FAILURE(status))
  563. return -EIO;
  564. break;
  565. case ACPI_ADR_SPACE_SYSTEM_IO:
  566. status = acpi_os_read_port(address, (u32 *)val,
  567. access_bit_width);
  568. if (ACPI_FAILURE(status))
  569. return -EIO;
  570. break;
  571. default:
  572. return -EINVAL;
  573. }
  574. return 0;
  575. }
  576. EXPORT_SYMBOL_GPL(apei_read);
  577. /* write GAR in interrupt (including NMI) or process context */
  578. int apei_write(u64 val, struct acpi_generic_address *reg)
  579. {
  580. int rc;
  581. u32 access_bit_width;
  582. u64 address;
  583. acpi_status status;
  584. rc = apei_check_gar(reg, &address, &access_bit_width);
  585. if (rc)
  586. return rc;
  587. switch (reg->space_id) {
  588. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  589. status = acpi_os_write_memory((acpi_physical_address) address,
  590. val, access_bit_width);
  591. if (ACPI_FAILURE(status))
  592. return -EIO;
  593. break;
  594. case ACPI_ADR_SPACE_SYSTEM_IO:
  595. status = acpi_os_write_port(address, val, access_bit_width);
  596. if (ACPI_FAILURE(status))
  597. return -EIO;
  598. break;
  599. default:
  600. return -EINVAL;
  601. }
  602. return 0;
  603. }
  604. EXPORT_SYMBOL_GPL(apei_write);
  605. static int collect_res_callback(struct apei_exec_context *ctx,
  606. struct acpi_whea_header *entry,
  607. void *data)
  608. {
  609. struct apei_resources *resources = data;
  610. struct acpi_generic_address *reg = &entry->register_region;
  611. u8 ins = entry->instruction;
  612. u32 access_bit_width;
  613. u64 paddr;
  614. int rc;
  615. if (!(ctx->ins_table[ins].flags & APEI_EXEC_INS_ACCESS_REGISTER))
  616. return 0;
  617. rc = apei_check_gar(reg, &paddr, &access_bit_width);
  618. if (rc)
  619. return rc;
  620. switch (reg->space_id) {
  621. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  622. return apei_res_add(&resources->iomem, paddr,
  623. access_bit_width / 8);
  624. case ACPI_ADR_SPACE_SYSTEM_IO:
  625. return apei_res_add(&resources->ioport, paddr,
  626. access_bit_width / 8);
  627. default:
  628. return -EINVAL;
  629. }
  630. }
  631. /*
  632. * Same register may be used by multiple instructions in GARs, so
  633. * resources are collected before requesting.
  634. */
  635. int apei_exec_collect_resources(struct apei_exec_context *ctx,
  636. struct apei_resources *resources)
  637. {
  638. return apei_exec_for_each_entry(ctx, collect_res_callback,
  639. resources, NULL);
  640. }
  641. EXPORT_SYMBOL_GPL(apei_exec_collect_resources);
  642. struct dentry *apei_get_debugfs_dir(void)
  643. {
  644. static struct dentry *dapei;
  645. if (!dapei)
  646. dapei = debugfs_create_dir("apei", NULL);
  647. return dapei;
  648. }
  649. EXPORT_SYMBOL_GPL(apei_get_debugfs_dir);
  650. int apei_osc_setup(void)
  651. {
  652. static u8 whea_uuid_str[] = "ed855e0c-6c90-47bf-a62a-26de0fc5ad5c";
  653. acpi_handle handle;
  654. u32 capbuf[3];
  655. struct acpi_osc_context context = {
  656. .uuid_str = whea_uuid_str,
  657. .rev = 1,
  658. .cap.length = sizeof(capbuf),
  659. .cap.pointer = capbuf,
  660. };
  661. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  662. capbuf[OSC_SUPPORT_TYPE] = 1;
  663. capbuf[OSC_CONTROL_TYPE] = 0;
  664. if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle))
  665. || ACPI_FAILURE(acpi_run_osc(handle, &context)))
  666. return -EIO;
  667. else {
  668. kfree(context.ret.pointer);
  669. return 0;
  670. }
  671. }
  672. EXPORT_SYMBOL_GPL(apei_osc_setup);