12800cd8eSLawrence Tang /**
22800cd8eSLawrence Tang  * Describes functions for converting ARM CPER sections from binary and JSON format
32800cd8eSLawrence Tang  * into an intermediate format.
42800cd8eSLawrence Tang  *
52800cd8eSLawrence Tang  * Author: Lawrence.Tang@arm.com
62800cd8eSLawrence Tang  **/
72800cd8eSLawrence Tang 
82800cd8eSLawrence Tang #include <stdio.h>
92800cd8eSLawrence Tang #include "json.h"
102800cd8eSLawrence Tang #include "../edk/Cper.h"
112800cd8eSLawrence Tang #include "../cper-utils.h"
122800cd8eSLawrence Tang #include "cper-section-arm.h"
132800cd8eSLawrence Tang 
143d0e4f24SLawrence Tang //Private pre-definitions.
153d0e4f24SLawrence Tang json_object* cper_arm_error_info_to_ir(EFI_ARM_PROCESSOR_ERROR_INFORMATION_ENTRY* error_info, void** cur_pos);
163d0e4f24SLawrence Tang json_object* cper_arm_cache_error_to_ir(EFI_ARM_PROCESSOR_CACHE_ERROR_STRUCTURE* cache_error);
173d0e4f24SLawrence Tang json_object* cper_arm_tlb_error_to_ir(EFI_ARM_PROCESSOR_TLB_ERROR_STRUCTURE* tlb_error);
183d0e4f24SLawrence Tang json_object* cper_arm_bus_error_to_ir(EFI_ARM_PROCESSOR_BUS_ERROR_STRUCTURE* bus_error);
193d0e4f24SLawrence Tang 
202800cd8eSLawrence Tang //Converts the given processor-generic CPER section into JSON IR.
212800cd8eSLawrence Tang json_object* cper_section_arm_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
222800cd8eSLawrence Tang {
232800cd8eSLawrence Tang     EFI_ARM_PROCESSOR_ERROR_RECORD* record = (EFI_ARM_PROCESSOR_ERROR_RECORD*)section;
242800cd8eSLawrence Tang     json_object* section_ir = json_object_new_object();
252800cd8eSLawrence Tang 
262800cd8eSLawrence Tang     //Validation bits.
272800cd8eSLawrence Tang     json_object* validation = bitfield_to_ir(record->ValidFields, 4, ARM_PROCESSOR_ERROR_VALID_BITFIELD_NAMES);
282800cd8eSLawrence Tang     json_object_object_add(section_ir, "validationBits", validation);
292800cd8eSLawrence Tang 
302800cd8eSLawrence Tang     //Number of error info and context info structures, and length.
312800cd8eSLawrence Tang     json_object_object_add(section_ir, "errorInfoNum", json_object_new_int(record->ErrInfoNum));
322800cd8eSLawrence Tang     json_object_object_add(section_ir, "contextInfoNum", json_object_new_int(record->ContextInfoNum));
332800cd8eSLawrence Tang     json_object_object_add(section_ir, "sectionLength", json_object_new_int(record->SectionLength));
342800cd8eSLawrence Tang 
352800cd8eSLawrence Tang     //Error affinity.
362800cd8eSLawrence Tang     json_object* error_affinity = json_object_new_object();
372800cd8eSLawrence Tang     json_object_object_add(error_affinity, "value", json_object_new_int(record->ErrorAffinityLevel));
382800cd8eSLawrence Tang     json_object_object_add(error_affinity, "type",
392800cd8eSLawrence Tang         json_object_new_string(record->ErrorAffinityLevel < 4 ? "Vendor Defined" : "Reserved"));
402800cd8eSLawrence Tang     json_object_object_add(section_ir, "errorAffinity", error_affinity);
412800cd8eSLawrence Tang 
422800cd8eSLawrence Tang     //Processor ID (MPIDR_EL1) and chip ID (MIDR_EL1).
432800cd8eSLawrence Tang     json_object_object_add(section_ir, "mpidrEl1", json_object_new_uint64(record->MPIDR_EL1));
442800cd8eSLawrence Tang     json_object_object_add(section_ir, "midrEl1", json_object_new_uint64(record->MIDR_EL1));
452800cd8eSLawrence Tang 
462800cd8eSLawrence Tang     //Whether the processor is running, and the state of it if so.
472800cd8eSLawrence Tang     json_object_object_add(section_ir, "running", json_object_new_boolean(record->RunningState));
483d0e4f24SLawrence Tang     if (record->RunningState >> 31)
492800cd8eSLawrence Tang     {
503d0e4f24SLawrence Tang         //Bit 32 of running state is on, so PSCI state information is included.
513d0e4f24SLawrence Tang         //todo: Look at how to make this human readable from the ARM PSCI document.
523d0e4f24SLawrence Tang         json_object_object_add(section_ir, "psciState", json_object_new_int(record->PsciState));
532800cd8eSLawrence Tang     }
542800cd8eSLawrence Tang 
553d0e4f24SLawrence Tang     //Processor error structures.
563d0e4f24SLawrence Tang     json_object* error_info_array = json_object_new_array();
573d0e4f24SLawrence Tang     EFI_ARM_PROCESSOR_ERROR_INFORMATION_ENTRY* cur_error = (EFI_ARM_PROCESSOR_ERROR_INFORMATION_ENTRY*)(record + 1);
583d0e4f24SLawrence Tang     for (int i=0; i<record->ErrInfoNum; i++)
593d0e4f24SLawrence Tang     {
603d0e4f24SLawrence Tang         json_object_array_add(error_info_array, cper_arm_error_info_to_ir(cur_error, (void*)&cur_error));
613d0e4f24SLawrence Tang         //Dynamically sized structure, so pointer is controlled within the above function.
623d0e4f24SLawrence Tang     }
632800cd8eSLawrence Tang     return section_ir;
642800cd8eSLawrence Tang }
653d0e4f24SLawrence Tang 
663d0e4f24SLawrence Tang //Converts a single ARM Process Error Information structure into JSON IR.
673d0e4f24SLawrence Tang json_object* cper_arm_error_info_to_ir(EFI_ARM_PROCESSOR_ERROR_INFORMATION_ENTRY* error_info, void** cur_pos)
683d0e4f24SLawrence Tang {
693d0e4f24SLawrence Tang     json_object* error_info_ir = json_object_new_object();
703d0e4f24SLawrence Tang 
713d0e4f24SLawrence Tang     //Version, length.
723d0e4f24SLawrence Tang     json_object_object_add(error_info_ir, "version", json_object_new_int(error_info->Version));
733d0e4f24SLawrence Tang     json_object_object_add(error_info_ir, "version", json_object_new_int(error_info->Length));
743d0e4f24SLawrence Tang 
753d0e4f24SLawrence Tang     //Validation bitfield.
763d0e4f24SLawrence Tang     json_object* validation = bitfield_to_ir(error_info->ValidationBits, 5, ARM_PROCESSOR_ERROR_INFO_ENTRY_VALID_BITFIELD_NAMES);
773d0e4f24SLawrence Tang     json_object_object_add(error_info_ir, "validationBits", validation);
783d0e4f24SLawrence Tang 
793d0e4f24SLawrence Tang     //The type of error information in this log.
803d0e4f24SLawrence Tang     //todo: The UEFI spec is ambiguous, what are the values for these??
813d0e4f24SLawrence Tang     json_object* error_type = integer_to_readable_pair(error_info->Type, 4,
823d0e4f24SLawrence Tang         ARM_PROCESSOR_ERROR_INFO_ENTRY_INFO_TYPES_KEYS,
833d0e4f24SLawrence Tang         ARM_PROCESSOR_ERROR_INFO_ENTRY_INFO_TYPES_VALUES,
843d0e4f24SLawrence Tang         "Unknown (Reserved)");
853d0e4f24SLawrence Tang     json_object_object_add(error_info_ir, "errorType", error_type);
863d0e4f24SLawrence Tang 
873d0e4f24SLawrence Tang     //Multiple error count.
88*22a467ceSLawrence Tang     json_object* multiple_error = json_object_new_object();
893d0e4f24SLawrence Tang     json_object_object_add(multiple_error, "value", json_object_new_int(error_info->MultipleError));
903d0e4f24SLawrence Tang     json_object_object_add(multiple_error, "type",
913d0e4f24SLawrence Tang         json_object_new_string(error_info->MultipleError < 1 ? "Single Error" : "Multiple Errors"));
923d0e4f24SLawrence Tang     json_object_object_add(error_info_ir, "multipleError", multiple_error);
933d0e4f24SLawrence Tang 
943d0e4f24SLawrence Tang     //Flags.
953d0e4f24SLawrence Tang     json_object* flags = bitfield_to_ir(error_info->Flags, 4, ARM_PROCESSOR_ERROR_INFO_ENTRY_FLAGS_NAMES);
963d0e4f24SLawrence Tang     json_object_object_add(error_info_ir, "flags", flags);
973d0e4f24SLawrence Tang 
983d0e4f24SLawrence Tang     //Error information, split by type.
993d0e4f24SLawrence Tang     json_object* error_subinfo = NULL;
1003d0e4f24SLawrence Tang     switch (error_info->Type)
1013d0e4f24SLawrence Tang     {
1023d0e4f24SLawrence Tang         case 0: //Cache
103*22a467ceSLawrence Tang             error_subinfo = cper_arm_cache_error_to_ir((EFI_ARM_PROCESSOR_CACHE_ERROR_STRUCTURE*)&error_info->ErrorInformation);
1043d0e4f24SLawrence Tang             break;
1053d0e4f24SLawrence Tang         case 1: //TLB
106*22a467ceSLawrence Tang             error_subinfo = cper_arm_tlb_error_to_ir((EFI_ARM_PROCESSOR_TLB_ERROR_STRUCTURE*)&error_info->ErrorInformation);
1073d0e4f24SLawrence Tang             break;
1083d0e4f24SLawrence Tang         case 2: //Bus
109*22a467ceSLawrence Tang             error_subinfo = cper_arm_bus_error_to_ir((EFI_ARM_PROCESSOR_BUS_ERROR_STRUCTURE*)&error_info->ErrorInformation);
1103d0e4f24SLawrence Tang             break;
1113d0e4f24SLawrence Tang     }
1123d0e4f24SLawrence Tang     json_object_object_add(error_info_ir, "errorInformation", error_subinfo);
1133d0e4f24SLawrence Tang 
1143d0e4f24SLawrence Tang     return error_info_ir;
1153d0e4f24SLawrence Tang }
1163d0e4f24SLawrence Tang 
1173d0e4f24SLawrence Tang //Converts a single ARM cache error information structure into JSON IR format.
1183d0e4f24SLawrence Tang json_object* cper_arm_cache_error_to_ir(EFI_ARM_PROCESSOR_CACHE_ERROR_STRUCTURE* cache_error)
1193d0e4f24SLawrence Tang {
1203d0e4f24SLawrence Tang     //todo
1213d0e4f24SLawrence Tang }
1223d0e4f24SLawrence Tang 
1233d0e4f24SLawrence Tang //Converts a single ARM TLB error information structure into JSON IR format.
1243d0e4f24SLawrence Tang json_object* cper_arm_tlb_error_to_ir(EFI_ARM_PROCESSOR_TLB_ERROR_STRUCTURE* tlb_error)
1253d0e4f24SLawrence Tang {
1263d0e4f24SLawrence Tang     //todo
1273d0e4f24SLawrence Tang }
1283d0e4f24SLawrence Tang 
1293d0e4f24SLawrence Tang //Converts a single ARM bus error information structure into JSON IR format.
1303d0e4f24SLawrence Tang json_object* cper_arm_bus_error_to_ir(EFI_ARM_PROCESSOR_BUS_ERROR_STRUCTURE* bus_error)
1313d0e4f24SLawrence Tang {
1323d0e4f24SLawrence Tang     //todo
1333d0e4f24SLawrence Tang }