erst.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * ACPI Error Record Serialization Table, ERST, Implementation
  3. *
  4. * ACPI ERST introduced in ACPI 4.0, June 16, 2009.
  5. * ACPI Platform Error Interfaces : Error Serialization
  6. *
  7. * Copyright (c) 2021 Oracle and/or its affiliates.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0-or-later
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qapi/error.h"
  13. #include "hw/qdev-core.h"
  14. #include "exec/memory.h"
  15. #include "qom/object.h"
  16. #include "hw/pci/pci_device.h"
  17. #include "qom/object_interfaces.h"
  18. #include "qemu/error-report.h"
  19. #include "migration/vmstate.h"
  20. #include "hw/qdev-properties.h"
  21. #include "hw/acpi/acpi.h"
  22. #include "hw/acpi/acpi-defs.h"
  23. #include "hw/acpi/aml-build.h"
  24. #include "hw/acpi/bios-linker-loader.h"
  25. #include "exec/address-spaces.h"
  26. #include "sysemu/hostmem.h"
  27. #include "hw/acpi/erst.h"
  28. #include "trace.h"
  29. /* ACPI 4.0: Table 17-16 Serialization Actions */
  30. #define ACTION_BEGIN_WRITE_OPERATION 0x0
  31. #define ACTION_BEGIN_READ_OPERATION 0x1
  32. #define ACTION_BEGIN_CLEAR_OPERATION 0x2
  33. #define ACTION_END_OPERATION 0x3
  34. #define ACTION_SET_RECORD_OFFSET 0x4
  35. #define ACTION_EXECUTE_OPERATION 0x5
  36. #define ACTION_CHECK_BUSY_STATUS 0x6
  37. #define ACTION_GET_COMMAND_STATUS 0x7
  38. #define ACTION_GET_RECORD_IDENTIFIER 0x8
  39. #define ACTION_SET_RECORD_IDENTIFIER 0x9
  40. #define ACTION_GET_RECORD_COUNT 0xA
  41. #define ACTION_BEGIN_DUMMY_WRITE_OPERATION 0xB
  42. #define ACTION_RESERVED 0xC
  43. #define ACTION_GET_ERROR_LOG_ADDRESS_RANGE 0xD
  44. #define ACTION_GET_ERROR_LOG_ADDRESS_LENGTH 0xE
  45. #define ACTION_GET_ERROR_LOG_ADDRESS_RANGE_ATTRIBUTES 0xF
  46. #define ACTION_GET_EXECUTE_OPERATION_TIMINGS 0x10 /* ACPI 6.3 */
  47. /* ACPI 4.0: Table 17-17 Command Status Definitions */
  48. #define STATUS_SUCCESS 0x00
  49. #define STATUS_NOT_ENOUGH_SPACE 0x01
  50. #define STATUS_HARDWARE_NOT_AVAILABLE 0x02
  51. #define STATUS_FAILED 0x03
  52. #define STATUS_RECORD_STORE_EMPTY 0x04
  53. #define STATUS_RECORD_NOT_FOUND 0x05
  54. /* ACPI 4.0: Table 17-19 Serialization Instructions */
  55. #define INST_READ_REGISTER 0x00
  56. #define INST_READ_REGISTER_VALUE 0x01
  57. #define INST_WRITE_REGISTER 0x02
  58. #define INST_WRITE_REGISTER_VALUE 0x03
  59. #define INST_NOOP 0x04
  60. #define INST_LOAD_VAR1 0x05
  61. #define INST_LOAD_VAR2 0x06
  62. #define INST_STORE_VAR1 0x07
  63. #define INST_ADD 0x08
  64. #define INST_SUBTRACT 0x09
  65. #define INST_ADD_VALUE 0x0A
  66. #define INST_SUBTRACT_VALUE 0x0B
  67. #define INST_STALL 0x0C
  68. #define INST_STALL_WHILE_TRUE 0x0D
  69. #define INST_SKIP_NEXT_INSTRUCTION_IF_TRUE 0x0E
  70. #define INST_GOTO 0x0F
  71. #define INST_SET_SRC_ADDRESS_BASE 0x10
  72. #define INST_SET_DST_ADDRESS_BASE 0x11
  73. #define INST_MOVE_DATA 0x12
  74. /* UEFI 2.1: Appendix N Common Platform Error Record */
  75. #define UEFI_CPER_RECORD_MIN_SIZE 128U
  76. #define UEFI_CPER_RECORD_LENGTH_OFFSET 20U
  77. #define UEFI_CPER_RECORD_ID_OFFSET 96U
  78. /*
  79. * NOTE that when accessing CPER fields within a record, memcpy()
  80. * is utilized to avoid a possible misaligned access on the host.
  81. */
  82. /*
  83. * This implementation is an ACTION (cmd) and VALUE (data)
  84. * interface consisting of just two 64-bit registers.
  85. */
  86. #define ERST_REG_SIZE (16UL)
  87. #define ERST_ACTION_OFFSET (0UL) /* action (cmd) */
  88. #define ERST_VALUE_OFFSET (8UL) /* argument/value (data) */
  89. /*
  90. * ERST_RECORD_SIZE is the buffer size for exchanging ERST
  91. * record contents. Thus, it defines the maximum record size.
  92. * As this is mapped through a PCI BAR, it must be a power of
  93. * two and larger than UEFI_CPER_RECORD_MIN_SIZE.
  94. * The backing storage is divided into fixed size "slots",
  95. * each ERST_RECORD_SIZE in length, and each "slot"
  96. * storing a single record. No attempt at optimizing storage
  97. * through compression, compaction, etc is attempted.
  98. * NOTE that slot 0 is reserved for the backing storage header.
  99. * Depending upon the size of the backing storage, additional
  100. * slots will be part of the slot 0 header in order to account
  101. * for a record_id for each available remaining slot.
  102. */
  103. /* 8KiB records, not too small, not too big */
  104. #define ERST_RECORD_SIZE (8192UL)
  105. #define ACPI_ERST_MEMDEV_PROP "memdev"
  106. #define ACPI_ERST_RECORD_SIZE_PROP "record_size"
  107. /*
  108. * From the ACPI ERST spec sections:
  109. * A record id of all 0s is used to indicate 'unspecified' record id.
  110. * A record id of all 1s is used to indicate empty or end.
  111. */
  112. #define ERST_UNSPECIFIED_RECORD_ID (0UL)
  113. #define ERST_EMPTY_END_RECORD_ID (~0UL)
  114. #define ERST_IS_VALID_RECORD_ID(rid) \
  115. ((rid != ERST_UNSPECIFIED_RECORD_ID) && \
  116. (rid != ERST_EMPTY_END_RECORD_ID))
  117. /*
  118. * Implementation-specific definitions and types.
  119. * Values are arbitrary and chosen for this implementation.
  120. * See erst.rst documentation for details.
  121. */
  122. #define ERST_EXECUTE_OPERATION_MAGIC 0x9CUL
  123. #define ERST_STORE_MAGIC 0x524F545354535245UL /* ERSTSTOR */
  124. typedef struct {
  125. uint64_t magic;
  126. uint32_t record_size;
  127. uint32_t storage_offset; /* offset to record storage beyond header */
  128. uint16_t version;
  129. uint16_t reserved;
  130. uint32_t record_count;
  131. uint64_t map[]; /* contains record_ids, and position indicates index */
  132. } __attribute__((packed)) ERSTStorageHeader;
  133. /*
  134. * Object cast macro
  135. */
  136. #define ACPIERST(obj) \
  137. OBJECT_CHECK(ERSTDeviceState, (obj), TYPE_ACPI_ERST)
  138. /*
  139. * Main ERST device state structure
  140. */
  141. typedef struct {
  142. PCIDevice parent_obj;
  143. /* Backend storage */
  144. HostMemoryBackend *hostmem;
  145. MemoryRegion *hostmem_mr;
  146. uint32_t storage_size;
  147. uint32_t default_record_size;
  148. /* Programming registers */
  149. MemoryRegion iomem_mr;
  150. /* Exchange buffer */
  151. MemoryRegion exchange_mr;
  152. /* Interface state */
  153. uint8_t operation;
  154. uint8_t busy_status;
  155. uint8_t command_status;
  156. uint32_t record_offset;
  157. uint64_t reg_action;
  158. uint64_t reg_value;
  159. uint64_t record_identifier;
  160. ERSTStorageHeader *header;
  161. unsigned first_record_index;
  162. unsigned last_record_index;
  163. unsigned next_record_index;
  164. } ERSTDeviceState;
  165. /*******************************************************************/
  166. /*******************************************************************/
  167. typedef struct {
  168. GArray *table_data;
  169. pcibus_t bar;
  170. uint8_t instruction;
  171. uint8_t flags;
  172. uint8_t register_bit_width;
  173. pcibus_t register_offset;
  174. } BuildSerializationInstructionEntry;
  175. /* ACPI 4.0: 17.4.1.2 Serialization Instruction Entries */
  176. static void build_serialization_instruction(
  177. BuildSerializationInstructionEntry *e,
  178. uint8_t serialization_action,
  179. uint64_t value)
  180. {
  181. /* ACPI 4.0: Table 17-18 Serialization Instruction Entry */
  182. struct AcpiGenericAddress gas;
  183. uint64_t mask;
  184. /* Serialization Action */
  185. build_append_int_noprefix(e->table_data, serialization_action, 1);
  186. /* Instruction */
  187. build_append_int_noprefix(e->table_data, e->instruction, 1);
  188. /* Flags */
  189. build_append_int_noprefix(e->table_data, e->flags, 1);
  190. /* Reserved */
  191. build_append_int_noprefix(e->table_data, 0, 1);
  192. /* Register Region */
  193. gas.space_id = AML_SYSTEM_MEMORY;
  194. gas.bit_width = e->register_bit_width;
  195. gas.bit_offset = 0;
  196. gas.access_width = (uint8_t)ctz32(e->register_bit_width) - 2;
  197. gas.address = (uint64_t)(e->bar + e->register_offset);
  198. build_append_gas_from_struct(e->table_data, &gas);
  199. /* Value */
  200. build_append_int_noprefix(e->table_data, value, 8);
  201. /* Mask */
  202. mask = (1ULL << (e->register_bit_width - 1) << 1) - 1;
  203. build_append_int_noprefix(e->table_data, mask, 8);
  204. }
  205. /* ACPI 4.0: 17.4.1 Serialization Action Table */
  206. void build_erst(GArray *table_data, BIOSLinker *linker, Object *erst_dev,
  207. const char *oem_id, const char *oem_table_id)
  208. {
  209. /*
  210. * Serialization Action Table
  211. * The serialization action table must be generated first
  212. * so that its size can be known in order to populate the
  213. * Instruction Entry Count field.
  214. */
  215. unsigned action;
  216. GArray *table_instruction_data = g_array_new(FALSE, FALSE, sizeof(char));
  217. pcibus_t bar0 = pci_get_bar_addr(PCI_DEVICE(erst_dev), 0);
  218. AcpiTable table = { .sig = "ERST", .rev = 1, .oem_id = oem_id,
  219. .oem_table_id = oem_table_id };
  220. /* Contexts for the different ways ACTION and VALUE are accessed */
  221. BuildSerializationInstructionEntry rd_value_32_val = {
  222. .table_data = table_instruction_data, .bar = bar0, .flags = 0,
  223. .instruction = INST_READ_REGISTER_VALUE,
  224. .register_bit_width = 32,
  225. .register_offset = ERST_VALUE_OFFSET,
  226. };
  227. BuildSerializationInstructionEntry rd_value_32 = {
  228. .table_data = table_instruction_data, .bar = bar0, .flags = 0,
  229. .instruction = INST_READ_REGISTER,
  230. .register_bit_width = 32,
  231. .register_offset = ERST_VALUE_OFFSET,
  232. };
  233. BuildSerializationInstructionEntry rd_value_64 = {
  234. .table_data = table_instruction_data, .bar = bar0, .flags = 0,
  235. .instruction = INST_READ_REGISTER,
  236. .register_bit_width = 64,
  237. .register_offset = ERST_VALUE_OFFSET,
  238. };
  239. BuildSerializationInstructionEntry wr_value_32_val = {
  240. .table_data = table_instruction_data, .bar = bar0, .flags = 0,
  241. .instruction = INST_WRITE_REGISTER_VALUE,
  242. .register_bit_width = 32,
  243. .register_offset = ERST_VALUE_OFFSET,
  244. };
  245. BuildSerializationInstructionEntry wr_value_32 = {
  246. .table_data = table_instruction_data, .bar = bar0, .flags = 0,
  247. .instruction = INST_WRITE_REGISTER,
  248. .register_bit_width = 32,
  249. .register_offset = ERST_VALUE_OFFSET,
  250. };
  251. BuildSerializationInstructionEntry wr_value_64 = {
  252. .table_data = table_instruction_data, .bar = bar0, .flags = 0,
  253. .instruction = INST_WRITE_REGISTER,
  254. .register_bit_width = 64,
  255. .register_offset = ERST_VALUE_OFFSET,
  256. };
  257. BuildSerializationInstructionEntry wr_action = {
  258. .table_data = table_instruction_data, .bar = bar0, .flags = 0,
  259. .instruction = INST_WRITE_REGISTER_VALUE,
  260. .register_bit_width = 32,
  261. .register_offset = ERST_ACTION_OFFSET,
  262. };
  263. trace_acpi_erst_pci_bar_0(bar0);
  264. /* Serialization Instruction Entries */
  265. action = ACTION_BEGIN_WRITE_OPERATION;
  266. build_serialization_instruction(&wr_action, action, action);
  267. action = ACTION_BEGIN_READ_OPERATION;
  268. build_serialization_instruction(&wr_action, action, action);
  269. action = ACTION_BEGIN_CLEAR_OPERATION;
  270. build_serialization_instruction(&wr_action, action, action);
  271. action = ACTION_END_OPERATION;
  272. build_serialization_instruction(&wr_action, action, action);
  273. action = ACTION_SET_RECORD_OFFSET;
  274. build_serialization_instruction(&wr_value_32, action, 0);
  275. build_serialization_instruction(&wr_action, action, action);
  276. action = ACTION_EXECUTE_OPERATION;
  277. build_serialization_instruction(&wr_value_32_val, action,
  278. ERST_EXECUTE_OPERATION_MAGIC);
  279. build_serialization_instruction(&wr_action, action, action);
  280. action = ACTION_CHECK_BUSY_STATUS;
  281. build_serialization_instruction(&wr_action, action, action);
  282. build_serialization_instruction(&rd_value_32_val, action, 0x01);
  283. action = ACTION_GET_COMMAND_STATUS;
  284. build_serialization_instruction(&wr_action, action, action);
  285. build_serialization_instruction(&rd_value_32, action, 0);
  286. action = ACTION_GET_RECORD_IDENTIFIER;
  287. build_serialization_instruction(&wr_action, action, action);
  288. build_serialization_instruction(&rd_value_64, action, 0);
  289. action = ACTION_SET_RECORD_IDENTIFIER;
  290. build_serialization_instruction(&wr_value_64, action, 0);
  291. build_serialization_instruction(&wr_action, action, action);
  292. action = ACTION_GET_RECORD_COUNT;
  293. build_serialization_instruction(&wr_action, action, action);
  294. build_serialization_instruction(&rd_value_32, action, 0);
  295. action = ACTION_BEGIN_DUMMY_WRITE_OPERATION;
  296. build_serialization_instruction(&wr_action, action, action);
  297. action = ACTION_GET_ERROR_LOG_ADDRESS_RANGE;
  298. build_serialization_instruction(&wr_action, action, action);
  299. build_serialization_instruction(&rd_value_64, action, 0);
  300. action = ACTION_GET_ERROR_LOG_ADDRESS_LENGTH;
  301. build_serialization_instruction(&wr_action, action, action);
  302. build_serialization_instruction(&rd_value_64, action, 0);
  303. action = ACTION_GET_ERROR_LOG_ADDRESS_RANGE_ATTRIBUTES;
  304. build_serialization_instruction(&wr_action, action, action);
  305. build_serialization_instruction(&rd_value_32, action, 0);
  306. action = ACTION_GET_EXECUTE_OPERATION_TIMINGS;
  307. build_serialization_instruction(&wr_action, action, action);
  308. build_serialization_instruction(&rd_value_64, action, 0);
  309. /* Serialization Header */
  310. acpi_table_begin(&table, table_data);
  311. /* Serialization Header Size */
  312. build_append_int_noprefix(table_data, 48, 4);
  313. /* Reserved */
  314. build_append_int_noprefix(table_data, 0, 4);
  315. /*
  316. * Instruction Entry Count
  317. * Each instruction entry is 32 bytes
  318. */
  319. g_assert((table_instruction_data->len) % 32 == 0);
  320. build_append_int_noprefix(table_data,
  321. (table_instruction_data->len / 32), 4);
  322. /* Serialization Instruction Entries */
  323. g_array_append_vals(table_data, table_instruction_data->data,
  324. table_instruction_data->len);
  325. g_array_free(table_instruction_data, TRUE);
  326. acpi_table_end(linker, &table);
  327. }
  328. /*******************************************************************/
  329. /*******************************************************************/
  330. static uint8_t *get_nvram_ptr_by_index(ERSTDeviceState *s, unsigned index)
  331. {
  332. uint8_t *rc = NULL;
  333. off_t offset = (index * le32_to_cpu(s->header->record_size));
  334. g_assert(offset < s->storage_size);
  335. rc = memory_region_get_ram_ptr(s->hostmem_mr);
  336. rc += offset;
  337. return rc;
  338. }
  339. static void make_erst_storage_header(ERSTDeviceState *s)
  340. {
  341. ERSTStorageHeader *header = s->header;
  342. unsigned mapsz, headersz;
  343. header->magic = cpu_to_le64(ERST_STORE_MAGIC);
  344. header->record_size = cpu_to_le32(s->default_record_size);
  345. header->version = cpu_to_le16(0x0100);
  346. header->reserved = cpu_to_le16(0x0000);
  347. /* Compute mapsize */
  348. mapsz = s->storage_size / s->default_record_size;
  349. mapsz *= sizeof(uint64_t);
  350. /* Compute header+map size */
  351. headersz = sizeof(ERSTStorageHeader) + mapsz;
  352. /* Round up to nearest integer multiple of ERST_RECORD_SIZE */
  353. headersz = QEMU_ALIGN_UP(headersz, s->default_record_size);
  354. header->storage_offset = cpu_to_le32(headersz);
  355. /*
  356. * The HostMemoryBackend initializes contents to zero,
  357. * so all record_ids stashed in the map are zero'd.
  358. * As well the record_count is zero. Properly initialized.
  359. */
  360. }
  361. static void check_erst_backend_storage(ERSTDeviceState *s, Error **errp)
  362. {
  363. ERSTStorageHeader *header;
  364. uint32_t record_size;
  365. header = memory_region_get_ram_ptr(s->hostmem_mr);
  366. s->header = header;
  367. /* Ensure pointer to header is 64-bit aligned */
  368. g_assert(QEMU_PTR_IS_ALIGNED(header, sizeof(uint64_t)));
  369. /*
  370. * Check if header is uninitialized; HostMemoryBackend inits to 0
  371. */
  372. if (le64_to_cpu(header->magic) == 0UL) {
  373. make_erst_storage_header(s);
  374. }
  375. /* Validity check record_size */
  376. record_size = le32_to_cpu(header->record_size);
  377. if (!(
  378. (record_size) && /* non zero */
  379. (record_size >= UEFI_CPER_RECORD_MIN_SIZE) &&
  380. (((record_size - 1) & record_size) == 0) && /* is power of 2 */
  381. (record_size >= 4096) /* PAGE_SIZE */
  382. )) {
  383. error_setg(errp, "ERST record_size %u is invalid", record_size);
  384. return;
  385. }
  386. /* Validity check header */
  387. if (!(
  388. (le64_to_cpu(header->magic) == ERST_STORE_MAGIC) &&
  389. ((le32_to_cpu(header->storage_offset) % record_size) == 0) &&
  390. (le16_to_cpu(header->version) == 0x0100) &&
  391. (le16_to_cpu(header->reserved) == 0)
  392. )) {
  393. error_setg(errp, "ERST backend storage header is invalid");
  394. return;
  395. }
  396. /* Check storage_size against record_size */
  397. if (((s->storage_size % record_size) != 0) ||
  398. (record_size > s->storage_size)) {
  399. error_setg(errp, "ACPI ERST requires storage size be multiple of "
  400. "record size (%uKiB)", record_size);
  401. return;
  402. }
  403. /* Compute offset of first and last record storage slot */
  404. s->first_record_index = le32_to_cpu(header->storage_offset)
  405. / record_size;
  406. s->last_record_index = (s->storage_size / record_size);
  407. }
  408. static void update_map_entry(ERSTDeviceState *s, unsigned index,
  409. uint64_t record_id)
  410. {
  411. if (index < s->last_record_index) {
  412. s->header->map[index] = cpu_to_le64(record_id);
  413. }
  414. }
  415. static unsigned find_next_empty_record_index(ERSTDeviceState *s)
  416. {
  417. unsigned rc = 0; /* 0 not a valid index */
  418. unsigned index = s->first_record_index;
  419. for (; index < s->last_record_index; ++index) {
  420. if (le64_to_cpu(s->header->map[index]) == ERST_UNSPECIFIED_RECORD_ID) {
  421. rc = index;
  422. break;
  423. }
  424. }
  425. return rc;
  426. }
  427. static unsigned lookup_erst_record(ERSTDeviceState *s,
  428. uint64_t record_identifier)
  429. {
  430. unsigned rc = 0; /* 0 not a valid index */
  431. /* Find the record_identifier in the map */
  432. if (record_identifier != ERST_UNSPECIFIED_RECORD_ID) {
  433. /*
  434. * Count number of valid records encountered, and
  435. * short-circuit the loop if identifier not found
  436. */
  437. uint32_t record_count = le32_to_cpu(s->header->record_count);
  438. unsigned count = 0;
  439. unsigned index;
  440. for (index = s->first_record_index; index < s->last_record_index &&
  441. count < record_count; ++index) {
  442. if (le64_to_cpu(s->header->map[index]) == record_identifier) {
  443. rc = index;
  444. break;
  445. }
  446. if (le64_to_cpu(s->header->map[index]) !=
  447. ERST_UNSPECIFIED_RECORD_ID) {
  448. ++count;
  449. }
  450. }
  451. }
  452. return rc;
  453. }
  454. /*
  455. * ACPI 4.0: 17.4.1.1 Serialization Actions, also see
  456. * ACPI 4.0: 17.4.2.2 Operations - Reading 6.c and 2.c
  457. */
  458. static unsigned get_next_record_identifier(ERSTDeviceState *s,
  459. uint64_t *record_identifier, bool first)
  460. {
  461. unsigned found = 0;
  462. unsigned index;
  463. /* For operations needing to return 'first' record identifier */
  464. if (first) {
  465. /* Reset initial index to beginning */
  466. s->next_record_index = s->first_record_index;
  467. }
  468. index = s->next_record_index;
  469. *record_identifier = ERST_EMPTY_END_RECORD_ID;
  470. if (le32_to_cpu(s->header->record_count)) {
  471. for (; index < s->last_record_index; ++index) {
  472. if (le64_to_cpu(s->header->map[index]) !=
  473. ERST_UNSPECIFIED_RECORD_ID) {
  474. /* where to start next time */
  475. s->next_record_index = index + 1;
  476. *record_identifier = le64_to_cpu(s->header->map[index]);
  477. found = 1;
  478. break;
  479. }
  480. }
  481. }
  482. if (!found) {
  483. /* at end (ie scan complete), reset */
  484. s->next_record_index = s->first_record_index;
  485. }
  486. return STATUS_SUCCESS;
  487. }
  488. /* ACPI 4.0: 17.4.2.3 Operations - Clearing */
  489. static unsigned clear_erst_record(ERSTDeviceState *s)
  490. {
  491. unsigned rc = STATUS_RECORD_NOT_FOUND;
  492. unsigned index;
  493. /* Check for valid record identifier */
  494. if (!ERST_IS_VALID_RECORD_ID(s->record_identifier)) {
  495. return STATUS_FAILED;
  496. }
  497. index = lookup_erst_record(s, s->record_identifier);
  498. if (index) {
  499. /* No need to wipe record, just invalidate its map entry */
  500. uint32_t record_count;
  501. update_map_entry(s, index, ERST_UNSPECIFIED_RECORD_ID);
  502. record_count = le32_to_cpu(s->header->record_count);
  503. record_count -= 1;
  504. s->header->record_count = cpu_to_le32(record_count);
  505. rc = STATUS_SUCCESS;
  506. }
  507. return rc;
  508. }
  509. /* ACPI 4.0: 17.4.2.2 Operations - Reading */
  510. static unsigned read_erst_record(ERSTDeviceState *s)
  511. {
  512. unsigned rc = STATUS_RECORD_NOT_FOUND;
  513. unsigned exchange_length;
  514. unsigned index;
  515. /* Check if backend storage is empty */
  516. if (le32_to_cpu(s->header->record_count) == 0) {
  517. return STATUS_RECORD_STORE_EMPTY;
  518. }
  519. exchange_length = memory_region_size(&s->exchange_mr);
  520. /* Check for record identifier of all 0s */
  521. if (s->record_identifier == ERST_UNSPECIFIED_RECORD_ID) {
  522. /* Set to 'first' record in storage */
  523. get_next_record_identifier(s, &s->record_identifier, true);
  524. /* record_identifier is now a valid id, or all 1s */
  525. }
  526. /* Check for record identifier of all 1s */
  527. if (s->record_identifier == ERST_EMPTY_END_RECORD_ID) {
  528. return STATUS_FAILED;
  529. }
  530. /* Validate record_offset */
  531. if (s->record_offset > (exchange_length - UEFI_CPER_RECORD_MIN_SIZE)) {
  532. return STATUS_FAILED;
  533. }
  534. index = lookup_erst_record(s, s->record_identifier);
  535. if (index) {
  536. uint8_t *nvram;
  537. uint8_t *exchange;
  538. uint32_t record_length;
  539. /* Obtain pointer to the exchange buffer */
  540. exchange = memory_region_get_ram_ptr(&s->exchange_mr);
  541. exchange += s->record_offset;
  542. /* Obtain pointer to slot in storage */
  543. nvram = get_nvram_ptr_by_index(s, index);
  544. /* Validate CPER record_length */
  545. memcpy((uint8_t *)&record_length,
  546. &nvram[UEFI_CPER_RECORD_LENGTH_OFFSET],
  547. sizeof(uint32_t));
  548. record_length = le32_to_cpu(record_length);
  549. if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
  550. rc = STATUS_FAILED;
  551. }
  552. if (record_length > exchange_length - s->record_offset) {
  553. rc = STATUS_FAILED;
  554. }
  555. /* If all is ok, copy the record to the exchange buffer */
  556. if (rc != STATUS_FAILED) {
  557. memcpy(exchange, nvram, record_length);
  558. rc = STATUS_SUCCESS;
  559. }
  560. } else {
  561. /*
  562. * See "Reading : 'The steps performed by the platform ...' 2.c"
  563. * Set to 'first' record in storage
  564. */
  565. get_next_record_identifier(s, &s->record_identifier, true);
  566. }
  567. return rc;
  568. }
  569. /* ACPI 4.0: 17.4.2.1 Operations - Writing */
  570. static unsigned write_erst_record(ERSTDeviceState *s)
  571. {
  572. unsigned rc = STATUS_FAILED;
  573. unsigned exchange_length;
  574. unsigned index;
  575. uint64_t record_identifier;
  576. uint32_t record_length;
  577. uint8_t *exchange;
  578. uint8_t *nvram = NULL;
  579. bool record_found = false;
  580. exchange_length = memory_region_size(&s->exchange_mr);
  581. /* Validate record_offset */
  582. if (s->record_offset > (exchange_length - UEFI_CPER_RECORD_MIN_SIZE)) {
  583. return STATUS_FAILED;
  584. }
  585. /* Obtain pointer to record in the exchange buffer */
  586. exchange = memory_region_get_ram_ptr(&s->exchange_mr);
  587. exchange += s->record_offset;
  588. /* Validate CPER record_length */
  589. memcpy((uint8_t *)&record_length, &exchange[UEFI_CPER_RECORD_LENGTH_OFFSET],
  590. sizeof(uint32_t));
  591. record_length = le32_to_cpu(record_length);
  592. if (record_length < UEFI_CPER_RECORD_MIN_SIZE) {
  593. return STATUS_FAILED;
  594. }
  595. if (record_length > exchange_length - s->record_offset) {
  596. return STATUS_FAILED;
  597. }
  598. /* Extract record identifier */
  599. memcpy((uint8_t *)&record_identifier, &exchange[UEFI_CPER_RECORD_ID_OFFSET],
  600. sizeof(uint64_t));
  601. record_identifier = le64_to_cpu(record_identifier);
  602. /* Check for valid record identifier */
  603. if (!ERST_IS_VALID_RECORD_ID(record_identifier)) {
  604. return STATUS_FAILED;
  605. }
  606. index = lookup_erst_record(s, record_identifier);
  607. if (index) {
  608. /* Record found, overwrite existing record */
  609. nvram = get_nvram_ptr_by_index(s, index);
  610. record_found = true;
  611. } else {
  612. /* Record not found, not an overwrite, allocate for write */
  613. index = find_next_empty_record_index(s);
  614. if (index) {
  615. nvram = get_nvram_ptr_by_index(s, index);
  616. } else {
  617. /* All slots are occupied */
  618. rc = STATUS_NOT_ENOUGH_SPACE;
  619. }
  620. }
  621. if (nvram) {
  622. /* Write the record into the slot */
  623. memcpy(nvram, exchange, record_length);
  624. memset(nvram + record_length, 0xFF, exchange_length - record_length);
  625. /* If a new record, increment the record_count */
  626. if (!record_found) {
  627. uint32_t record_count;
  628. record_count = le32_to_cpu(s->header->record_count);
  629. record_count += 1; /* writing new record */
  630. s->header->record_count = cpu_to_le32(record_count);
  631. }
  632. update_map_entry(s, index, record_identifier);
  633. rc = STATUS_SUCCESS;
  634. }
  635. return rc;
  636. }
  637. /*******************************************************************/
  638. static uint64_t erst_rd_reg64(hwaddr addr,
  639. uint64_t reg, unsigned size)
  640. {
  641. uint64_t rdval;
  642. uint64_t mask;
  643. unsigned shift;
  644. if (size == sizeof(uint64_t)) {
  645. /* 64b access */
  646. mask = 0xFFFFFFFFFFFFFFFFUL;
  647. shift = 0;
  648. } else {
  649. /* 32b access */
  650. mask = 0x00000000FFFFFFFFUL;
  651. shift = ((addr & 0x4) == 0x4) ? 32 : 0;
  652. }
  653. rdval = reg;
  654. rdval >>= shift;
  655. rdval &= mask;
  656. return rdval;
  657. }
  658. static uint64_t erst_wr_reg64(hwaddr addr,
  659. uint64_t reg, uint64_t val, unsigned size)
  660. {
  661. uint64_t wrval;
  662. uint64_t mask;
  663. unsigned shift;
  664. if (size == sizeof(uint64_t)) {
  665. /* 64b access */
  666. mask = 0xFFFFFFFFFFFFFFFFUL;
  667. shift = 0;
  668. } else {
  669. /* 32b access */
  670. mask = 0x00000000FFFFFFFFUL;
  671. shift = ((addr & 0x4) == 0x4) ? 32 : 0;
  672. }
  673. val &= mask;
  674. val <<= shift;
  675. mask <<= shift;
  676. wrval = reg;
  677. wrval &= ~mask;
  678. wrval |= val;
  679. return wrval;
  680. }
  681. static void erst_reg_write(void *opaque, hwaddr addr,
  682. uint64_t val, unsigned size)
  683. {
  684. ERSTDeviceState *s = (ERSTDeviceState *)opaque;
  685. /*
  686. * NOTE: All actions/operations/side effects happen on the WRITE,
  687. * by this implementation's design. The READs simply return the
  688. * reg_value contents.
  689. */
  690. trace_acpi_erst_reg_write(addr, val, size);
  691. switch (addr) {
  692. case ERST_VALUE_OFFSET + 0:
  693. case ERST_VALUE_OFFSET + 4:
  694. s->reg_value = erst_wr_reg64(addr, s->reg_value, val, size);
  695. break;
  696. case ERST_ACTION_OFFSET + 0:
  697. /*
  698. * NOTE: all valid values written to this register are of the
  699. * ACTION_* variety. Thus there is no need to make this a 64-bit
  700. * register, 32-bits is appropriate. As such ERST_ACTION_OFFSET+4
  701. * is not needed.
  702. */
  703. switch (val) {
  704. case ACTION_BEGIN_WRITE_OPERATION:
  705. case ACTION_BEGIN_READ_OPERATION:
  706. case ACTION_BEGIN_CLEAR_OPERATION:
  707. case ACTION_BEGIN_DUMMY_WRITE_OPERATION:
  708. case ACTION_END_OPERATION:
  709. s->operation = val;
  710. break;
  711. case ACTION_SET_RECORD_OFFSET:
  712. s->record_offset = s->reg_value;
  713. break;
  714. case ACTION_EXECUTE_OPERATION:
  715. if ((uint8_t)s->reg_value == ERST_EXECUTE_OPERATION_MAGIC) {
  716. s->busy_status = 1;
  717. switch (s->operation) {
  718. case ACTION_BEGIN_WRITE_OPERATION:
  719. s->command_status = write_erst_record(s);
  720. break;
  721. case ACTION_BEGIN_READ_OPERATION:
  722. s->command_status = read_erst_record(s);
  723. break;
  724. case ACTION_BEGIN_CLEAR_OPERATION:
  725. s->command_status = clear_erst_record(s);
  726. break;
  727. case ACTION_BEGIN_DUMMY_WRITE_OPERATION:
  728. s->command_status = STATUS_SUCCESS;
  729. break;
  730. case ACTION_END_OPERATION:
  731. s->command_status = STATUS_SUCCESS;
  732. break;
  733. default:
  734. s->command_status = STATUS_FAILED;
  735. break;
  736. }
  737. s->busy_status = 0;
  738. }
  739. break;
  740. case ACTION_CHECK_BUSY_STATUS:
  741. s->reg_value = s->busy_status;
  742. break;
  743. case ACTION_GET_COMMAND_STATUS:
  744. s->reg_value = s->command_status;
  745. break;
  746. case ACTION_GET_RECORD_IDENTIFIER:
  747. s->command_status = get_next_record_identifier(s,
  748. &s->reg_value, false);
  749. break;
  750. case ACTION_SET_RECORD_IDENTIFIER:
  751. s->record_identifier = s->reg_value;
  752. break;
  753. case ACTION_GET_RECORD_COUNT:
  754. s->reg_value = le32_to_cpu(s->header->record_count);
  755. break;
  756. case ACTION_GET_ERROR_LOG_ADDRESS_RANGE:
  757. s->reg_value = (hwaddr)pci_get_bar_addr(PCI_DEVICE(s), 1);
  758. break;
  759. case ACTION_GET_ERROR_LOG_ADDRESS_LENGTH:
  760. s->reg_value = le32_to_cpu(s->header->record_size);
  761. break;
  762. case ACTION_GET_ERROR_LOG_ADDRESS_RANGE_ATTRIBUTES:
  763. s->reg_value = 0x0; /* intentional, not NVRAM mode */
  764. break;
  765. case ACTION_GET_EXECUTE_OPERATION_TIMINGS:
  766. s->reg_value =
  767. (100ULL << 32) | /* 100us max time */
  768. (10ULL << 0) ; /* 10us min time */
  769. break;
  770. default:
  771. /* Unknown action/command, NOP */
  772. break;
  773. }
  774. break;
  775. default:
  776. /* This should not happen, but if it does, NOP */
  777. break;
  778. }
  779. }
  780. static uint64_t erst_reg_read(void *opaque, hwaddr addr,
  781. unsigned size)
  782. {
  783. ERSTDeviceState *s = (ERSTDeviceState *)opaque;
  784. uint64_t val = 0;
  785. switch (addr) {
  786. case ERST_ACTION_OFFSET + 0:
  787. case ERST_ACTION_OFFSET + 4:
  788. val = erst_rd_reg64(addr, s->reg_action, size);
  789. break;
  790. case ERST_VALUE_OFFSET + 0:
  791. case ERST_VALUE_OFFSET + 4:
  792. val = erst_rd_reg64(addr, s->reg_value, size);
  793. break;
  794. default:
  795. break;
  796. }
  797. trace_acpi_erst_reg_read(addr, val, size);
  798. return val;
  799. }
  800. static const MemoryRegionOps erst_reg_ops = {
  801. .read = erst_reg_read,
  802. .write = erst_reg_write,
  803. .endianness = DEVICE_NATIVE_ENDIAN,
  804. };
  805. /*******************************************************************/
  806. /*******************************************************************/
  807. static int erst_post_load(void *opaque, int version_id)
  808. {
  809. ERSTDeviceState *s = opaque;
  810. /* Recompute pointer to header */
  811. s->header = (ERSTStorageHeader *)get_nvram_ptr_by_index(s, 0);
  812. trace_acpi_erst_post_load(s->header, le32_to_cpu(s->header->record_size));
  813. return 0;
  814. }
  815. static const VMStateDescription erst_vmstate = {
  816. .name = "acpi-erst",
  817. .version_id = 1,
  818. .minimum_version_id = 1,
  819. .post_load = erst_post_load,
  820. .fields = (VMStateField[]) {
  821. VMSTATE_UINT8(operation, ERSTDeviceState),
  822. VMSTATE_UINT8(busy_status, ERSTDeviceState),
  823. VMSTATE_UINT8(command_status, ERSTDeviceState),
  824. VMSTATE_UINT32(record_offset, ERSTDeviceState),
  825. VMSTATE_UINT64(reg_action, ERSTDeviceState),
  826. VMSTATE_UINT64(reg_value, ERSTDeviceState),
  827. VMSTATE_UINT64(record_identifier, ERSTDeviceState),
  828. VMSTATE_UINT32(next_record_index, ERSTDeviceState),
  829. VMSTATE_END_OF_LIST()
  830. }
  831. };
  832. static void erst_realizefn(PCIDevice *pci_dev, Error **errp)
  833. {
  834. ERSTDeviceState *s = ACPIERST(pci_dev);
  835. trace_acpi_erst_realizefn_in();
  836. if (!s->hostmem) {
  837. error_setg(errp, "'" ACPI_ERST_MEMDEV_PROP "' property is not set");
  838. return;
  839. } else if (host_memory_backend_is_mapped(s->hostmem)) {
  840. error_setg(errp, "can't use already busy memdev: %s",
  841. object_get_canonical_path_component(OBJECT(s->hostmem)));
  842. return;
  843. }
  844. s->hostmem_mr = host_memory_backend_get_memory(s->hostmem);
  845. /* HostMemoryBackend size will be multiple of PAGE_SIZE */
  846. s->storage_size = object_property_get_int(OBJECT(s->hostmem), "size", errp);
  847. /* Initialize backend storage and record_count */
  848. check_erst_backend_storage(s, errp);
  849. /* BAR 0: Programming registers */
  850. memory_region_init_io(&s->iomem_mr, OBJECT(pci_dev), &erst_reg_ops, s,
  851. TYPE_ACPI_ERST, ERST_REG_SIZE);
  852. pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->iomem_mr);
  853. /* BAR 1: Exchange buffer memory */
  854. memory_region_init_ram(&s->exchange_mr, OBJECT(pci_dev),
  855. "erst.exchange",
  856. le32_to_cpu(s->header->record_size), errp);
  857. pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
  858. &s->exchange_mr);
  859. /* Include the backend storage in the migration stream */
  860. vmstate_register_ram_global(s->hostmem_mr);
  861. trace_acpi_erst_realizefn_out(s->storage_size);
  862. }
  863. static void erst_reset(DeviceState *dev)
  864. {
  865. ERSTDeviceState *s = ACPIERST(dev);
  866. trace_acpi_erst_reset_in(le32_to_cpu(s->header->record_count));
  867. s->operation = 0;
  868. s->busy_status = 0;
  869. s->command_status = STATUS_SUCCESS;
  870. s->record_identifier = ERST_UNSPECIFIED_RECORD_ID;
  871. s->record_offset = 0;
  872. s->next_record_index = s->first_record_index;
  873. /* NOTE: first/last_record_index are computed only once */
  874. trace_acpi_erst_reset_out(le32_to_cpu(s->header->record_count));
  875. }
  876. static Property erst_properties[] = {
  877. DEFINE_PROP_LINK(ACPI_ERST_MEMDEV_PROP, ERSTDeviceState, hostmem,
  878. TYPE_MEMORY_BACKEND, HostMemoryBackend *),
  879. DEFINE_PROP_UINT32(ACPI_ERST_RECORD_SIZE_PROP, ERSTDeviceState,
  880. default_record_size, ERST_RECORD_SIZE),
  881. DEFINE_PROP_END_OF_LIST(),
  882. };
  883. static void erst_class_init(ObjectClass *klass, void *data)
  884. {
  885. DeviceClass *dc = DEVICE_CLASS(klass);
  886. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  887. trace_acpi_erst_class_init_in();
  888. k->realize = erst_realizefn;
  889. k->vendor_id = PCI_VENDOR_ID_REDHAT;
  890. k->device_id = PCI_DEVICE_ID_REDHAT_ACPI_ERST;
  891. k->revision = 0x00;
  892. k->class_id = PCI_CLASS_OTHERS;
  893. dc->reset = erst_reset;
  894. dc->vmsd = &erst_vmstate;
  895. dc->user_creatable = true;
  896. dc->hotpluggable = false;
  897. device_class_set_props(dc, erst_properties);
  898. dc->desc = "ACPI Error Record Serialization Table (ERST) device";
  899. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  900. trace_acpi_erst_class_init_out();
  901. }
  902. static const TypeInfo erst_type_info = {
  903. .name = TYPE_ACPI_ERST,
  904. .parent = TYPE_PCI_DEVICE,
  905. .class_init = erst_class_init,
  906. .instance_size = sizeof(ERSTDeviceState),
  907. .interfaces = (InterfaceInfo[]) {
  908. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  909. { }
  910. }
  911. };
  912. static void erst_register_types(void)
  913. {
  914. type_register_static(&erst_type_info);
  915. }
  916. type_init(erst_register_types)