feat(lib): introduce Hob creation library

According to Platform Initialization (PI) Specification [1] and
discussion on edk2 mailing list [2],
StandaloneMm shouldn't create Hob but it should be passed from TF-A.
IOW, TF-A should pass boot information via HOB list to initialise
StandaloneMm properly.

And this HOB lists could be delivered via
    - SPM_MM: Transfer List according to the firmware handoff spec[3]

    - FF-A v1.1 >= : FF-A boot protocol.

This patch introduces a TF-A HOB creation library and
some of definitions which StandaloneMm requires to boot.

Link: https://uefi.org/sites/default/files/resources/PI_Spec_1_6.pdf [1]
Link: https://edk2.groups.io/g/devel/topic/103675962#114283 [2]
Link: https://github.com/FirmwareHandoff/firmware_handoff [3]
Signed-off-by: Levi Yun <yeoreum.yun@arm.com>
Change-Id: I5e0838adce487110206998a8b79bc3adca922cec
diff --git a/include/lib/hob/efi_types.h b/include/lib/hob/efi_types.h
new file mode 100644
index 0000000..071d012
--- /dev/null
+++ b/include/lib/hob/efi_types.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef EFI_TYPES_H
+#define EFI_TYPES_H
+
+#include <stdint.h>
+#include <tools_share/uuid.h>
+
+typedef uint64_t efi_physical_address_t;
+
+/*****************************************************************************
+ *                            EFI_BOOT_MODE                                  *
+ *****************************************************************************/
+
+typedef uint32_t efi_boot_mode_t;
+/**
+ * EFI boot mode.
+ */
+#define EFI_BOOT_WITH_FULL_CONFIGURATION                   U(0x00)
+#define EFI_BOOT_WITH_MINIMAL_CONFIGURATION                U(0x01)
+#define EFI_BOOT_ASSUMING_NO_CONFIGURATION_CHANGES         U(0x02)
+#define EFI_BOOT_WITH_FULL_CONFIGURATION_PLUS_DIAGNOSTICS  U(0x03)
+#define EFI_BOOT_WITH_DEFAULT_SETTINGS                     U(0x04)
+#define EFI_BOOT_ON_S4_RESUME                              U(0x05)
+#define EFI_BOOT_ON_S5_RESUME                              U(0x06)
+#define EFI_BOOT_WITH_MFG_MODE_SETTINGS                    U(0x07)
+#define EFI_BOOT_ON_S2_RESUME                              U(0x10)
+#define EFI_BOOT_ON_S3_RESUME                              U(0x11)
+#define EFI_BOOT_ON_FLASH_UPDATE                           U(0x12)
+#define EFI_BOOT_IN_RECOVERY_MODE                          U(0x20)
+
+/*****************************************************************************
+ *                            EFI_RESOURCE_TYPE                              *
+ *****************************************************************************/
+
+typedef uint32_t efi_resource_type_t;
+
+/**
+ * Value of EFI_RESOURCE_TYPE used in EFI_HOB_RESOURCE_DESCRIPTOR.
+ */
+#define EFI_RESOURCE_SYSTEM_MEMORY          U(0x00000000)
+#define EFI_RESOURCE_MEMORY_MAPPED_IO       U(0x00000001)
+#define EFI_RESOURCE_IO                     U(0x00000002)
+#define EFI_RESOURCE_FIRMWARE_DEVICE        U(0x00000003)
+#define EFI_RESOURCE_MEMORY_MAPPED_IO_PORT  U(0x00000004)
+#define EFI_RESOURCE_MEMORY_RESERVED        U(0x00000005)
+#define EFI_RESOURCE_IO_RESERVED            U(0x00000006)
+
+/*****************************************************************************
+ *                       EFI_RESOURCE_ATTRIBUTE_TYPE                         *
+ *****************************************************************************/
+
+typedef uint32_t efi_resource_attribute_type_t;
+
+#define EFI_RESOURCE_ATTRIBUTE_PRESENT                  U(0x00000001)
+#define EFI_RESOURCE_ATTRIBUTE_INITIALIZED              U(0x00000002)
+#define EFI_RESOURCE_ATTRIBUTE_TESTED                   U(0x00000004)
+#define EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED           U(0x00000080)
+#define EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED          U(0x00000100)
+#define EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED      U(0x00000200)
+#define EFI_RESOURCE_ATTRIBUTE_PERSISTENT               U(0x00800000)
+#define EFI_RESOURCE_ATTRIBUTE_SINGLE_BIT_ECC           U(0x00000008)
+#define EFI_RESOURCE_ATTRIBUTE_MULTIPLE_BIT_ECC         U(0x00000010)
+#define EFI_RESOURCE_ATTRIBUTE_ECC_RESERVED_1           U(0x00000020)
+#define EFI_RESOURCE_ATTRIBUTE_ECC_RESERVED_2           U(0x00000040)
+#define EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE              U(0x00000400)
+#define EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE        U(0x00000800)
+#define EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE  U(0x00001000)
+#define EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE     U(0x00002000)
+#define EFI_RESOURCE_ATTRIBUTE_16_BIT_IO                U(0x00004000)
+#define EFI_RESOURCE_ATTRIBUTE_32_BIT_IO                U(0x00008000)
+#define EFI_RESOURCE_ATTRIBUTE_64_BIT_IO                U(0x00010000)
+#define EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED        U(0x00020000)
+#define EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE         U(0x00100000)
+#define EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE        U(0x00200000)
+#define EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE    U(0x00400000)
+#define EFI_RESOURCE_ATTRIBUTE_PERSISTABLE              U(0x01000000)
+#define EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED      U(0x00040000)
+#define EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE    U(0x00080000)
+#define EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE            U(0x02000000)
+
+#endif  /* EFI_TYPES_H */
diff --git a/include/lib/hob/hob.h b/include/lib/hob/hob.h
new file mode 100644
index 0000000..120f5da
--- /dev/null
+++ b/include/lib/hob/hob.h
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef HOB_H
+#define HOB_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <lib/hob/efi_types.h>
+#include <lib/utils_def.h>
+#include <tools_share/uuid.h>
+
+/*****************************************************************************
+ *                            Hob Generic Header                             *
+ *****************************************************************************/
+
+/**
+ * HobType values of EFI_HOB_GENERIC_HEADER.
+ */
+#define EFI_HOB_TYPE_HANDOFF              U(0x0001)
+#define EFI_HOB_TYPE_MEMORY_ALLOCATION    U(0x0002)
+#define EFI_HOB_TYPE_RESOURCE_DESCRIPTOR  U(0x0003)
+#define EFI_HOB_TYPE_GUID_EXTENSION       U(0x0004)
+#define EFI_HOB_TYPE_FV                   U(0x0005)
+#define EFI_HOB_TYPE_CPU                  U(0x0006)
+#define EFI_HOB_TYPE_MEMORY_POOL          U(0x0007)
+#define EFI_HOB_TYPE_FV2                  U(0x0009)
+#define EFI_HOB_TYPE_LOAD_PEIM_UNUSED     U(0x000A)
+#define EFI_HOB_TYPE_UEFI_CAPSULE         U(0x000B)
+#define EFI_HOB_TYPE_FV3                  U(0x000C)
+#define EFI_HOB_TYPE_UNUSED               U(0xFFFE)
+#define EFI_HOB_TYPE_END_OF_HOB_LIST      U(0xFFFF)
+
+struct efi_hob_generic_header {
+	uint16_t hob_type;
+	uint16_t hob_length;
+	uint32_t reserved;
+};
+
+/*****************************************************************************
+ *                               PHIT Hob.                                   *
+ *****************************************************************************/
+
+#define EFI_HOB_HANDOFF_TABLE_VERSION     U(0x000a)
+
+struct efi_hob_handoff_info_table {
+	struct efi_hob_generic_header header;
+	uint32_t version;
+	efi_boot_mode_t  boot_mode;
+	efi_physical_address_t efi_memory_top;
+	efi_physical_address_t efi_memory_bottom;
+	efi_physical_address_t efi_free_memory_top;
+	efi_physical_address_t efi_free_memory_bottom;
+	efi_physical_address_t efi_end_of_hob_list;
+};
+
+/*****************************************************************************
+ *                       Resource Descriptor Hob.                            *
+ *****************************************************************************/
+
+struct efi_hob_resource_descriptor {
+	struct efi_hob_generic_header header;
+	struct efi_guid owner;
+	efi_resource_type_t resource_type;
+	efi_resource_attribute_type_t resource_attribute;
+	efi_physical_address_t physical_start;
+	uint64_t resource_length;
+};
+
+/*****************************************************************************
+ *                           Guid Extension Hob.                             *
+ *****************************************************************************/
+struct efi_hob_guid_type {
+	struct efi_hob_generic_header header;
+	struct efi_guid name;
+	/**
+	 * Guid specific data goes here.
+	 */
+};
+
+/*****************************************************************************
+ *                           Firmware Volume Hob.                            *
+ *****************************************************************************/
+struct efi_hob_firmware_volume {
+	struct efi_hob_generic_header header;
+	efi_physical_address_t base_address;
+	uint64_t length;
+	/**
+	 * Guid specific data goes here.
+	 */
+};
+
+/*****************************************************************************
+ *                              Interfaces.                                  *
+ *****************************************************************************/
+
+struct efi_hob_handoff_info_table *
+create_hob_list(
+		efi_physical_address_t efi_memory_begin, size_t efi_memory_length,
+		efi_physical_address_t efi_free_memory_bottom, size_t efi_free_memory_length);
+
+int create_resource_descriptor_hob(
+		struct efi_hob_handoff_info_table *hob_table,
+		efi_resource_type_t resource_type,
+		efi_resource_attribute_type_t resource_attribute,
+		efi_physical_address_t phy_addr_start,
+		uint64_t resource_length);
+
+int create_guid_hob(struct efi_hob_handoff_info_table *hob_table,
+		struct efi_guid *guid, uint16_t data_length, void **data);
+
+int create_fv_hob(struct efi_hob_handoff_info_table *hob_table,
+		efi_physical_address_t base_addr, uint64_t size);
+
+#endif /* HOB_H */
diff --git a/include/lib/hob/hob_guid.h b/include/lib/hob/hob_guid.h
new file mode 100644
index 0000000..65d3dbf
--- /dev/null
+++ b/include/lib/hob/hob_guid.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef HOB_GUID_H
+#define HOB_GUID_H
+
+#include <lib/hob/efi_types.h>
+
+/**
+ * Guid used for creating StandaloneMm related information.
+ */
+
+#define MM_PEI_MMRAM_MEMORY_RESERVE_GUID                                        \
+{                                                                               \
+	0x0703f912, 0xbf8d, 0x4e2a, {0xbe, 0x07, 0xab, 0x27, 0x25, 0x25, 0xc5, 0x92 } \
+}
+
+#define MM_NS_BUFFER_GUID                                                       \
+{                                                                               \
+	0xf00497e3, 0xbfa2, 0x41a1, {0x9d, 0x29, 0x54, 0xc2, 0xe9, 0x37, 0x21, 0xc5 } \
+}
+
+#define MM_MP_INFORMATION_GUID                                                  \
+{                                                                               \
+	0xba33f15d, 0x4000, 0x45c1, {0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3}  \
+}
+
+#endif /* HOB_GUID_H */
diff --git a/include/lib/hob/mmram.h b/include/lib/hob/mmram.h
new file mode 100644
index 0000000..b269c64
--- /dev/null
+++ b/include/lib/hob/mmram.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#ifndef MMRAM_H
+#define MMRAM_H
+
+#include <lib/hob/efi_types.h>
+
+/**
+ * MMRAM states and capabilities
+ * See UEFI Platform Initialization Specification Version 1.8, IV-5.3.5
+ */
+#define EFI_MMRAM_OPEN                U(0x00000001)
+#define EFI_MMRAM_CLOSED              U(0x00000002)
+#define EFI_MMRAM_LOCKED              U(0x00000004)
+#define EFI_CACHEABLE                 U(0x00000008)
+#define EFI_ALLOCATED                 U(0x00000010)
+#define EFI_NEEDS_TESTING             U(0x00000020)
+#define EFI_NEEDS_ECC_INITIALIZATION  U(0x00000040)
+
+#define EFI_SMRAM_OPEN    EFI_MMRAM_OPEN
+#define EFI_SMRAM_CLOSED  EFI_MMRAM_CLOSED
+#define EFI_SMRAM_LOCKED  EFI_MMRAM_LOCKED
+
+struct efi_mmram_descriptor {
+	efi_physical_address_t physical_start;
+	efi_physical_address_t cpu_start;
+	uint64_t physical_size;
+	uint64_t region_state;
+};
+
+/**
+ * MMRAM block descriptor
+ * This definition comes from
+ *     https://github.com/tianocore/edk2/blob/master/StandaloneMmPkg/Include/Guid/MmramMemoryReserve.h
+ */
+struct efi_mmram_hob_descriptor_block {
+	uint32_t number_of_mm_reserved_regions;
+	struct efi_mmram_descriptor descriptor[];
+};
+
+#endif /* MMRAM_H */
diff --git a/include/lib/hob/mpinfo.h b/include/lib/hob/mpinfo.h
new file mode 100644
index 0000000..b80d8f1
--- /dev/null
+++ b/include/lib/hob/mpinfo.h
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#ifndef MPINFO_H
+#define MPINFO_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <lib/utils_def.h>
+
+/*
+ * Value used in the NumberProcessors parameter of the GetProcessorInfo function
+ */
+#define CPU_V2_EXTENDED_TOPOLOGY  UL(1 << 24)
+
+/*
+ * This bit is used in the StatusFlag field of EFI_PROCESSOR_INFORMATION and
+ * indicates whether the processor is playing the role of BSP. If the bit is 1,
+ * then the processor is BSP. Otherwise, it is AP.
+ */
+#define PROCESSOR_AS_BSP_BIT  UL(1 << 0)
+
+/*
+ * This bit is used in the StatusFlag field of EFI_PROCESSOR_INFORMATION and
+ * indicates whether the processor is enabled. If the bit is 1, then the
+ * processor is enabled. Otherwise, it is disabled.
+ */
+#define PROCESSOR_ENABLED_BIT  UL(1 << 1)
+
+/*
+ * This bit is used in the StatusFlag field of EFI_PROCESSOR_INFORMATION and
+ * indicates whether the processor is healthy. If the bit is 1, then the
+ * processor is healthy. Otherwise, some fault has been detected for the processor.
+ */
+#define PROCESSOR_HEALTH_STATUS_BIT  UL(1 << 2)
+
+/*
+ * Structure that describes the physical location of a logical CPU.
+ */
+struct efi_cpu_physical_location {
+	uint32_t package;
+	uint32_t core;
+	uint32_t thread;
+};
+
+/*
+ * Structure that defines the 6-level physical location of the processor
+ */
+struct efi_cpu_physical_location2 {
+	uint32_t package;
+	uint32_t module;
+	uint32_t tile;
+	uint32_t die;
+	uint32_t core;
+	uint32_t thread;
+};
+
+union extended_processor_information {
+	/*
+	 * The 6-level physical location of the processor, including the
+	 * physical package number that identifies the cartridge, the physical
+	 * module number within package, the physical tile number within the module,
+	 * the physical die number within the tile, the physical core number within
+	 * package, and logical thread number within core.
+	 */
+	struct efi_cpu_physical_location2 location2;
+};
+
+/*
+ * Structure that describes information about a logical CPU.
+ */
+struct efi_processor_information {
+	/*
+	 * The unique processor ID determined by system hardware.
+	 */
+	uint64_t processor_id;
+
+	/*
+	 * Flags indicating if the processor is BSP or AP, if the processor is enabled
+	 * or disabled, and if the processor is healthy. Bits 3..31 are reserved and
+	 * must be 0.
+	 *
+	 * <pre>
+	 * BSP  ENABLED  HEALTH  Description
+	 * ===  =======  ======  ===================================================
+	 * 0      0       0     Unhealthy Disabled AP.
+	 * 0      0       1     Healthy Disabled AP.
+	 * 0      1       0     Unhealthy Enabled AP.
+	 * 0      1       1     Healthy Enabled AP.
+	 * 1      0       0     Invalid. The BSP can never be in the disabled state.
+	 * 1      0       1     Invalid. The BSP can never be in the disabled state.
+	 * 1      1       0     Unhealthy Enabled BSP.
+	 * 1      1       1     Healthy Enabled BSP.
+	 * </pre>
+	 */
+	uint32_t status_flags;
+
+	/*
+	 * The physical location of the processor, including the physical package number
+	 * that identifies the cartridge, the physical core number within package, and
+	 * logical thread number within core.
+	 */
+	struct efi_cpu_physical_location location;
+
+	/*
+	 * The extended information of the processor. This field is filled only when
+	 * CPU_V2_EXTENDED_TOPOLOGY is set in parameter ProcessorNumber.
+	 */
+	union extended_processor_information extended_information;
+};
+
+struct efi_mp_information_hob_data {
+	uint64_t number_of_processors;
+	uint64_t number_of_enabled_processors;
+	struct efi_processor_information processor_info[];
+};
+
+#endif /* MPINFO_H */