ghes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Support for generating APEI tables and recording CPER for Guests
  3. *
  4. * Copyright (c) 2020 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * Author: Dongjiu Geng <gengdongjiu@huawei.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "hw/acpi/ghes.h"
  22. #include "hw/acpi/aml-build.h"
  23. #include "qemu/error-report.h"
  24. #include "hw/acpi/generic_event_device.h"
  25. #include "hw/nvram/fw_cfg.h"
  26. #include "qemu/uuid.h"
  27. #define ACPI_GHES_ERRORS_FW_CFG_FILE "etc/hardware_errors"
  28. #define ACPI_GHES_DATA_ADDR_FW_CFG_FILE "etc/hardware_errors_addr"
  29. /* The max size in bytes for one error block */
  30. #define ACPI_GHES_MAX_RAW_DATA_LENGTH (1 * KiB)
  31. /* Now only support ARMv8 SEA notification type error source */
  32. #define ACPI_GHES_ERROR_SOURCE_COUNT 1
  33. /* Generic Hardware Error Source version 2 */
  34. #define ACPI_GHES_SOURCE_GENERIC_ERROR_V2 10
  35. /* Address offset in Generic Address Structure(GAS) */
  36. #define GAS_ADDR_OFFSET 4
  37. /*
  38. * The total size of Generic Error Data Entry
  39. * ACPI 6.1/6.2: 18.3.2.7.1 Generic Error Data,
  40. * Table 18-343 Generic Error Data Entry
  41. */
  42. #define ACPI_GHES_DATA_LENGTH 72
  43. /* The memory section CPER size, UEFI 2.6: N.2.5 Memory Error Section */
  44. #define ACPI_GHES_MEM_CPER_LENGTH 80
  45. /* Masks for block_status flags */
  46. #define ACPI_GEBS_UNCORRECTABLE 1
  47. /*
  48. * Total size for Generic Error Status Block except Generic Error Data Entries
  49. * ACPI 6.2: 18.3.2.7.1 Generic Error Data,
  50. * Table 18-380 Generic Error Status Block
  51. */
  52. #define ACPI_GHES_GESB_SIZE 20
  53. /*
  54. * Values for error_severity field
  55. */
  56. enum AcpiGenericErrorSeverity {
  57. ACPI_CPER_SEV_RECOVERABLE = 0,
  58. ACPI_CPER_SEV_FATAL = 1,
  59. ACPI_CPER_SEV_CORRECTED = 2,
  60. ACPI_CPER_SEV_NONE = 3,
  61. };
  62. /*
  63. * Hardware Error Notification
  64. * ACPI 4.0: 17.3.2.7 Hardware Error Notification
  65. * Composes dummy Hardware Error Notification descriptor of specified type
  66. */
  67. static void build_ghes_hw_error_notification(GArray *table, const uint8_t type)
  68. {
  69. /* Type */
  70. build_append_int_noprefix(table, type, 1);
  71. /*
  72. * Length:
  73. * Total length of the structure in bytes
  74. */
  75. build_append_int_noprefix(table, 28, 1);
  76. /* Configuration Write Enable */
  77. build_append_int_noprefix(table, 0, 2);
  78. /* Poll Interval */
  79. build_append_int_noprefix(table, 0, 4);
  80. /* Vector */
  81. build_append_int_noprefix(table, 0, 4);
  82. /* Switch To Polling Threshold Value */
  83. build_append_int_noprefix(table, 0, 4);
  84. /* Switch To Polling Threshold Window */
  85. build_append_int_noprefix(table, 0, 4);
  86. /* Error Threshold Value */
  87. build_append_int_noprefix(table, 0, 4);
  88. /* Error Threshold Window */
  89. build_append_int_noprefix(table, 0, 4);
  90. }
  91. /*
  92. * Generic Error Data Entry
  93. * ACPI 6.1: 18.3.2.7.1 Generic Error Data
  94. */
  95. static void acpi_ghes_generic_error_data(GArray *table,
  96. const uint8_t *section_type, uint32_t error_severity,
  97. uint8_t validation_bits, uint8_t flags,
  98. uint32_t error_data_length, QemuUUID fru_id,
  99. uint64_t time_stamp)
  100. {
  101. const uint8_t fru_text[20] = {0};
  102. /* Section Type */
  103. g_array_append_vals(table, section_type, 16);
  104. /* Error Severity */
  105. build_append_int_noprefix(table, error_severity, 4);
  106. /* Revision */
  107. build_append_int_noprefix(table, 0x300, 2);
  108. /* Validation Bits */
  109. build_append_int_noprefix(table, validation_bits, 1);
  110. /* Flags */
  111. build_append_int_noprefix(table, flags, 1);
  112. /* Error Data Length */
  113. build_append_int_noprefix(table, error_data_length, 4);
  114. /* FRU Id */
  115. g_array_append_vals(table, fru_id.data, ARRAY_SIZE(fru_id.data));
  116. /* FRU Text */
  117. g_array_append_vals(table, fru_text, sizeof(fru_text));
  118. /* Timestamp */
  119. build_append_int_noprefix(table, time_stamp, 8);
  120. }
  121. /*
  122. * Generic Error Status Block
  123. * ACPI 6.1: 18.3.2.7.1 Generic Error Data
  124. */
  125. static void acpi_ghes_generic_error_status(GArray *table, uint32_t block_status,
  126. uint32_t raw_data_offset, uint32_t raw_data_length,
  127. uint32_t data_length, uint32_t error_severity)
  128. {
  129. /* Block Status */
  130. build_append_int_noprefix(table, block_status, 4);
  131. /* Raw Data Offset */
  132. build_append_int_noprefix(table, raw_data_offset, 4);
  133. /* Raw Data Length */
  134. build_append_int_noprefix(table, raw_data_length, 4);
  135. /* Data Length */
  136. build_append_int_noprefix(table, data_length, 4);
  137. /* Error Severity */
  138. build_append_int_noprefix(table, error_severity, 4);
  139. }
  140. /* UEFI 2.6: N.2.5 Memory Error Section */
  141. static void acpi_ghes_build_append_mem_cper(GArray *table,
  142. uint64_t error_physical_addr)
  143. {
  144. /*
  145. * Memory Error Record
  146. */
  147. /* Validation Bits */
  148. build_append_int_noprefix(table,
  149. (1ULL << 14) | /* Type Valid */
  150. (1ULL << 1) /* Physical Address Valid */,
  151. 8);
  152. /* Error Status */
  153. build_append_int_noprefix(table, 0, 8);
  154. /* Physical Address */
  155. build_append_int_noprefix(table, error_physical_addr, 8);
  156. /* Skip all the detailed information normally found in such a record */
  157. build_append_int_noprefix(table, 0, 48);
  158. /* Memory Error Type */
  159. build_append_int_noprefix(table, 0 /* Unknown error */, 1);
  160. /* Skip all the detailed information normally found in such a record */
  161. build_append_int_noprefix(table, 0, 7);
  162. }
  163. static int acpi_ghes_record_mem_error(uint64_t error_block_address,
  164. uint64_t error_physical_addr)
  165. {
  166. GArray *block;
  167. /* Memory Error Section Type */
  168. const uint8_t uefi_cper_mem_sec[] =
  169. UUID_LE(0xA5BC1114, 0x6F64, 0x4EDE, 0xB8, 0x63, 0x3E, 0x83, \
  170. 0xED, 0x7C, 0x83, 0xB1);
  171. /* invalid fru id: ACPI 4.0: 17.3.2.6.1 Generic Error Data,
  172. * Table 17-13 Generic Error Data Entry
  173. */
  174. QemuUUID fru_id = {};
  175. uint32_t data_length;
  176. block = g_array_new(false, true /* clear */, 1);
  177. /* This is the length if adding a new generic error data entry*/
  178. data_length = ACPI_GHES_DATA_LENGTH + ACPI_GHES_MEM_CPER_LENGTH;
  179. /*
  180. * It should not run out of the preallocated memory if adding a new generic
  181. * error data entry
  182. */
  183. assert((data_length + ACPI_GHES_GESB_SIZE) <=
  184. ACPI_GHES_MAX_RAW_DATA_LENGTH);
  185. /* Build the new generic error status block header */
  186. acpi_ghes_generic_error_status(block, ACPI_GEBS_UNCORRECTABLE,
  187. 0, 0, data_length, ACPI_CPER_SEV_RECOVERABLE);
  188. /* Build this new generic error data entry header */
  189. acpi_ghes_generic_error_data(block, uefi_cper_mem_sec,
  190. ACPI_CPER_SEV_RECOVERABLE, 0, 0,
  191. ACPI_GHES_MEM_CPER_LENGTH, fru_id, 0);
  192. /* Build the memory section CPER for above new generic error data entry */
  193. acpi_ghes_build_append_mem_cper(block, error_physical_addr);
  194. /* Write the generic error data entry into guest memory */
  195. cpu_physical_memory_write(error_block_address, block->data, block->len);
  196. g_array_free(block, true);
  197. return 0;
  198. }
  199. /*
  200. * Build table for the hardware error fw_cfg blob.
  201. * Initialize "etc/hardware_errors" and "etc/hardware_errors_addr" fw_cfg blobs.
  202. * See docs/specs/acpi_hest_ghes.rst for blobs format.
  203. */
  204. void build_ghes_error_table(GArray *hardware_errors, BIOSLinker *linker)
  205. {
  206. int i, error_status_block_offset;
  207. /* Build error_block_address */
  208. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  209. build_append_int_noprefix(hardware_errors, 0, sizeof(uint64_t));
  210. }
  211. /* Build read_ack_register */
  212. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  213. /*
  214. * Initialize the value of read_ack_register to 1, so GHES can be
  215. * writeable after (re)boot.
  216. * ACPI 6.2: 18.3.2.8 Generic Hardware Error Source version 2
  217. * (GHESv2 - Type 10)
  218. */
  219. build_append_int_noprefix(hardware_errors, 1, sizeof(uint64_t));
  220. }
  221. /* Generic Error Status Block offset in the hardware error fw_cfg blob */
  222. error_status_block_offset = hardware_errors->len;
  223. /* Reserve space for Error Status Data Block */
  224. acpi_data_push(hardware_errors,
  225. ACPI_GHES_MAX_RAW_DATA_LENGTH * ACPI_GHES_ERROR_SOURCE_COUNT);
  226. /* Tell guest firmware to place hardware_errors blob into RAM */
  227. bios_linker_loader_alloc(linker, ACPI_GHES_ERRORS_FW_CFG_FILE,
  228. hardware_errors, sizeof(uint64_t), false);
  229. for (i = 0; i < ACPI_GHES_ERROR_SOURCE_COUNT; i++) {
  230. /*
  231. * Tell firmware to patch error_block_address entries to point to
  232. * corresponding "Generic Error Status Block"
  233. */
  234. bios_linker_loader_add_pointer(linker,
  235. ACPI_GHES_ERRORS_FW_CFG_FILE, sizeof(uint64_t) * i,
  236. sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
  237. error_status_block_offset + i * ACPI_GHES_MAX_RAW_DATA_LENGTH);
  238. }
  239. /*
  240. * tell firmware to write hardware_errors GPA into
  241. * hardware_errors_addr fw_cfg, once the former has been initialized.
  242. */
  243. bios_linker_loader_write_pointer(linker, ACPI_GHES_DATA_ADDR_FW_CFG_FILE,
  244. 0, sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE, 0);
  245. }
  246. /* Build Generic Hardware Error Source version 2 (GHESv2) */
  247. static void build_ghes_v2(GArray *table_data, int source_id, BIOSLinker *linker)
  248. {
  249. uint64_t address_offset;
  250. /*
  251. * Type:
  252. * Generic Hardware Error Source version 2(GHESv2 - Type 10)
  253. */
  254. build_append_int_noprefix(table_data, ACPI_GHES_SOURCE_GENERIC_ERROR_V2, 2);
  255. /* Source Id */
  256. build_append_int_noprefix(table_data, source_id, 2);
  257. /* Related Source Id */
  258. build_append_int_noprefix(table_data, 0xffff, 2);
  259. /* Flags */
  260. build_append_int_noprefix(table_data, 0, 1);
  261. /* Enabled */
  262. build_append_int_noprefix(table_data, 1, 1);
  263. /* Number of Records To Pre-allocate */
  264. build_append_int_noprefix(table_data, 1, 4);
  265. /* Max Sections Per Record */
  266. build_append_int_noprefix(table_data, 1, 4);
  267. /* Max Raw Data Length */
  268. build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
  269. address_offset = table_data->len;
  270. /* Error Status Address */
  271. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
  272. 4 /* QWord access */, 0);
  273. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  274. address_offset + GAS_ADDR_OFFSET, sizeof(uint64_t),
  275. ACPI_GHES_ERRORS_FW_CFG_FILE, source_id * sizeof(uint64_t));
  276. switch (source_id) {
  277. case ACPI_HEST_SRC_ID_SEA:
  278. /*
  279. * Notification Structure
  280. * Now only enable ARMv8 SEA notification type
  281. */
  282. build_ghes_hw_error_notification(table_data, ACPI_GHES_NOTIFY_SEA);
  283. break;
  284. default:
  285. error_report("Not support this error source");
  286. abort();
  287. }
  288. /* Error Status Block Length */
  289. build_append_int_noprefix(table_data, ACPI_GHES_MAX_RAW_DATA_LENGTH, 4);
  290. /*
  291. * Read Ack Register
  292. * ACPI 6.1: 18.3.2.8 Generic Hardware Error Source
  293. * version 2 (GHESv2 - Type 10)
  294. */
  295. address_offset = table_data->len;
  296. build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0x40, 0,
  297. 4 /* QWord access */, 0);
  298. bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
  299. address_offset + GAS_ADDR_OFFSET,
  300. sizeof(uint64_t), ACPI_GHES_ERRORS_FW_CFG_FILE,
  301. (ACPI_GHES_ERROR_SOURCE_COUNT + source_id) * sizeof(uint64_t));
  302. /*
  303. * Read Ack Preserve field
  304. * We only provide the first bit in Read Ack Register to OSPM to write
  305. * while the other bits are preserved.
  306. */
  307. build_append_int_noprefix(table_data, ~0x1ULL, 8);
  308. /* Read Ack Write */
  309. build_append_int_noprefix(table_data, 0x1, 8);
  310. }
  311. /* Build Hardware Error Source Table */
  312. void acpi_build_hest(GArray *table_data, BIOSLinker *linker)
  313. {
  314. uint64_t hest_start = table_data->len;
  315. /* Hardware Error Source Table header*/
  316. acpi_data_push(table_data, sizeof(AcpiTableHeader));
  317. /* Error Source Count */
  318. build_append_int_noprefix(table_data, ACPI_GHES_ERROR_SOURCE_COUNT, 4);
  319. build_ghes_v2(table_data, ACPI_HEST_SRC_ID_SEA, linker);
  320. build_header(linker, table_data, (void *)(table_data->data + hest_start),
  321. "HEST", table_data->len - hest_start, 1, NULL, NULL);
  322. }
  323. void acpi_ghes_add_fw_cfg(AcpiGhesState *ags, FWCfgState *s,
  324. GArray *hardware_error)
  325. {
  326. /* Create a read-only fw_cfg file for GHES */
  327. fw_cfg_add_file(s, ACPI_GHES_ERRORS_FW_CFG_FILE, hardware_error->data,
  328. hardware_error->len);
  329. /* Create a read-write fw_cfg file for Address */
  330. fw_cfg_add_file_callback(s, ACPI_GHES_DATA_ADDR_FW_CFG_FILE, NULL, NULL,
  331. NULL, &(ags->ghes_addr_le), sizeof(ags->ghes_addr_le), false);
  332. }
  333. int acpi_ghes_record_errors(uint8_t source_id, uint64_t physical_address)
  334. {
  335. uint64_t error_block_addr, read_ack_register_addr, read_ack_register = 0;
  336. uint64_t start_addr;
  337. bool ret = -1;
  338. AcpiGedState *acpi_ged_state;
  339. AcpiGhesState *ags;
  340. assert(source_id < ACPI_HEST_SRC_ID_RESERVED);
  341. acpi_ged_state = ACPI_GED(object_resolve_path_type("", TYPE_ACPI_GED,
  342. NULL));
  343. g_assert(acpi_ged_state);
  344. ags = &acpi_ged_state->ghes_state;
  345. start_addr = le64_to_cpu(ags->ghes_addr_le);
  346. if (physical_address) {
  347. if (source_id < ACPI_HEST_SRC_ID_RESERVED) {
  348. start_addr += source_id * sizeof(uint64_t);
  349. }
  350. cpu_physical_memory_read(start_addr, &error_block_addr,
  351. sizeof(error_block_addr));
  352. error_block_addr = le64_to_cpu(error_block_addr);
  353. read_ack_register_addr = start_addr +
  354. ACPI_GHES_ERROR_SOURCE_COUNT * sizeof(uint64_t);
  355. cpu_physical_memory_read(read_ack_register_addr,
  356. &read_ack_register, sizeof(read_ack_register));
  357. /* zero means OSPM does not acknowledge the error */
  358. if (!read_ack_register) {
  359. error_report("OSPM does not acknowledge previous error,"
  360. " so can not record CPER for current error anymore");
  361. } else if (error_block_addr) {
  362. read_ack_register = cpu_to_le64(0);
  363. /*
  364. * Clear the Read Ack Register, OSPM will write it to 1 when
  365. * it acknowledges this error.
  366. */
  367. cpu_physical_memory_write(read_ack_register_addr,
  368. &read_ack_register, sizeof(uint64_t));
  369. ret = acpi_ghes_record_mem_error(error_block_addr,
  370. physical_address);
  371. } else
  372. error_report("can not find Generic Error Status Block");
  373. }
  374. return ret;
  375. }