18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#define BOOT_CTYPE_H
38c2ecf20Sopenharmony_ci#include "misc.h"
48c2ecf20Sopenharmony_ci#include "error.h"
58c2ecf20Sopenharmony_ci#include "../string.h"
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/numa.h>
88c2ecf20Sopenharmony_ci#include <linux/efi.h>
98c2ecf20Sopenharmony_ci#include <asm/efi.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * Longest parameter of 'acpi=' is 'copy_dsdt', plus an extra '\0'
138c2ecf20Sopenharmony_ci * for termination.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci#define MAX_ACPI_ARG_LENGTH 10
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ci * Immovable memory regions representation. Max amount of memory regions is
198c2ecf20Sopenharmony_ci * MAX_NUMNODES*2.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_cistruct mem_vector immovable_mem[MAX_NUMNODES*2];
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * Search EFI system tables for RSDP.  If both ACPI_20_TABLE_GUID and
258c2ecf20Sopenharmony_ci * ACPI_TABLE_GUID are found, take the former, which has more features.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistatic acpi_physical_address
288c2ecf20Sopenharmony_ci__efi_get_rsdp_addr(unsigned long config_tables, unsigned int nr_tables,
298c2ecf20Sopenharmony_ci		    bool efi_64)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	acpi_physical_address rsdp_addr = 0;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#ifdef CONFIG_EFI
348c2ecf20Sopenharmony_ci	int i;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	/* Get EFI tables from systab. */
378c2ecf20Sopenharmony_ci	for (i = 0; i < nr_tables; i++) {
388c2ecf20Sopenharmony_ci		acpi_physical_address table;
398c2ecf20Sopenharmony_ci		efi_guid_t guid;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci		if (efi_64) {
428c2ecf20Sopenharmony_ci			efi_config_table_64_t *tbl = (efi_config_table_64_t *)config_tables + i;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci			guid  = tbl->guid;
458c2ecf20Sopenharmony_ci			table = tbl->table;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci			if (!IS_ENABLED(CONFIG_X86_64) && table >> 32) {
488c2ecf20Sopenharmony_ci				debug_putstr("Error getting RSDP address: EFI config table located above 4GB.\n");
498c2ecf20Sopenharmony_ci				return 0;
508c2ecf20Sopenharmony_ci			}
518c2ecf20Sopenharmony_ci		} else {
528c2ecf20Sopenharmony_ci			efi_config_table_32_t *tbl = (efi_config_table_32_t *)config_tables + i;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci			guid  = tbl->guid;
558c2ecf20Sopenharmony_ci			table = tbl->table;
568c2ecf20Sopenharmony_ci		}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci		if (!(efi_guidcmp(guid, ACPI_TABLE_GUID)))
598c2ecf20Sopenharmony_ci			rsdp_addr = table;
608c2ecf20Sopenharmony_ci		else if (!(efi_guidcmp(guid, ACPI_20_TABLE_GUID)))
618c2ecf20Sopenharmony_ci			return table;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci#endif
648c2ecf20Sopenharmony_ci	return rsdp_addr;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* EFI/kexec support is 64-bit only. */
688c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
698c2ecf20Sopenharmony_cistatic struct efi_setup_data *get_kexec_setup_data_addr(void)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	struct setup_data *data;
728c2ecf20Sopenharmony_ci	u64 pa_data;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	pa_data = boot_params->hdr.setup_data;
758c2ecf20Sopenharmony_ci	while (pa_data) {
768c2ecf20Sopenharmony_ci		data = (struct setup_data *)pa_data;
778c2ecf20Sopenharmony_ci		if (data->type == SETUP_EFI)
788c2ecf20Sopenharmony_ci			return (struct efi_setup_data *)(pa_data + sizeof(struct setup_data));
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		pa_data = data->next;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci	return NULL;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic acpi_physical_address kexec_get_rsdp_addr(void)
868c2ecf20Sopenharmony_ci{
878c2ecf20Sopenharmony_ci	efi_system_table_64_t *systab;
888c2ecf20Sopenharmony_ci	struct efi_setup_data *esd;
898c2ecf20Sopenharmony_ci	struct efi_info *ei;
908c2ecf20Sopenharmony_ci	char *sig;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	esd = (struct efi_setup_data *)get_kexec_setup_data_addr();
938c2ecf20Sopenharmony_ci	if (!esd)
948c2ecf20Sopenharmony_ci		return 0;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (!esd->tables) {
978c2ecf20Sopenharmony_ci		debug_putstr("Wrong kexec SETUP_EFI data.\n");
988c2ecf20Sopenharmony_ci		return 0;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	ei = &boot_params->efi_info;
1028c2ecf20Sopenharmony_ci	sig = (char *)&ei->efi_loader_signature;
1038c2ecf20Sopenharmony_ci	if (strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
1048c2ecf20Sopenharmony_ci		debug_putstr("Wrong kexec EFI loader signature.\n");
1058c2ecf20Sopenharmony_ci		return 0;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* Get systab from boot params. */
1098c2ecf20Sopenharmony_ci	systab = (efi_system_table_64_t *) (ei->efi_systab | ((__u64)ei->efi_systab_hi << 32));
1108c2ecf20Sopenharmony_ci	if (!systab)
1118c2ecf20Sopenharmony_ci		error("EFI system table not found in kexec boot_params.");
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return __efi_get_rsdp_addr((unsigned long)esd->tables, systab->nr_tables, true);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci#else
1168c2ecf20Sopenharmony_cistatic acpi_physical_address kexec_get_rsdp_addr(void) { return 0; }
1178c2ecf20Sopenharmony_ci#endif /* CONFIG_X86_64 */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic acpi_physical_address efi_get_rsdp_addr(void)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci#ifdef CONFIG_EFI
1228c2ecf20Sopenharmony_ci	unsigned long systab, config_tables;
1238c2ecf20Sopenharmony_ci	unsigned int nr_tables;
1248c2ecf20Sopenharmony_ci	struct efi_info *ei;
1258c2ecf20Sopenharmony_ci	bool efi_64;
1268c2ecf20Sopenharmony_ci	char *sig;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	ei = &boot_params->efi_info;
1298c2ecf20Sopenharmony_ci	sig = (char *)&ei->efi_loader_signature;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
1328c2ecf20Sopenharmony_ci		efi_64 = true;
1338c2ecf20Sopenharmony_ci	} else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) {
1348c2ecf20Sopenharmony_ci		efi_64 = false;
1358c2ecf20Sopenharmony_ci	} else {
1368c2ecf20Sopenharmony_ci		debug_putstr("Wrong EFI loader signature.\n");
1378c2ecf20Sopenharmony_ci		return 0;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* Get systab from boot params. */
1418c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64
1428c2ecf20Sopenharmony_ci	systab = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32);
1438c2ecf20Sopenharmony_ci#else
1448c2ecf20Sopenharmony_ci	if (ei->efi_systab_hi || ei->efi_memmap_hi) {
1458c2ecf20Sopenharmony_ci		debug_putstr("Error getting RSDP address: EFI system table located above 4GB.\n");
1468c2ecf20Sopenharmony_ci		return 0;
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci	systab = ei->efi_systab;
1498c2ecf20Sopenharmony_ci#endif
1508c2ecf20Sopenharmony_ci	if (!systab)
1518c2ecf20Sopenharmony_ci		error("EFI system table not found.");
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	/* Handle EFI bitness properly */
1548c2ecf20Sopenharmony_ci	if (efi_64) {
1558c2ecf20Sopenharmony_ci		efi_system_table_64_t *stbl = (efi_system_table_64_t *)systab;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		config_tables	= stbl->tables;
1588c2ecf20Sopenharmony_ci		nr_tables	= stbl->nr_tables;
1598c2ecf20Sopenharmony_ci	} else {
1608c2ecf20Sopenharmony_ci		efi_system_table_32_t *stbl = (efi_system_table_32_t *)systab;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		config_tables	= stbl->tables;
1638c2ecf20Sopenharmony_ci		nr_tables	= stbl->nr_tables;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (!config_tables)
1678c2ecf20Sopenharmony_ci		error("EFI config tables not found.");
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return __efi_get_rsdp_addr(config_tables, nr_tables, efi_64);
1708c2ecf20Sopenharmony_ci#else
1718c2ecf20Sopenharmony_ci	return 0;
1728c2ecf20Sopenharmony_ci#endif
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic u8 compute_checksum(u8 *buffer, u32 length)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	u8 *end = buffer + length;
1788c2ecf20Sopenharmony_ci	u8 sum = 0;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	while (buffer < end)
1818c2ecf20Sopenharmony_ci		sum += *(buffer++);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return sum;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/* Search a block of memory for the RSDP signature. */
1878c2ecf20Sopenharmony_cistatic u8 *scan_mem_for_rsdp(u8 *start, u32 length)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct acpi_table_rsdp *rsdp;
1908c2ecf20Sopenharmony_ci	u8 *address, *end;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	end = start + length;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* Search from given start address for the requested length */
1958c2ecf20Sopenharmony_ci	for (address = start; address < end; address += ACPI_RSDP_SCAN_STEP) {
1968c2ecf20Sopenharmony_ci		/*
1978c2ecf20Sopenharmony_ci		 * Both RSDP signature and checksum must be correct.
1988c2ecf20Sopenharmony_ci		 * Note: Sometimes there exists more than one RSDP in memory;
1998c2ecf20Sopenharmony_ci		 * the valid RSDP has a valid checksum, all others have an
2008c2ecf20Sopenharmony_ci		 * invalid checksum.
2018c2ecf20Sopenharmony_ci		 */
2028c2ecf20Sopenharmony_ci		rsdp = (struct acpi_table_rsdp *)address;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		/* BAD Signature */
2058c2ecf20Sopenharmony_ci		if (!ACPI_VALIDATE_RSDP_SIG(rsdp->signature))
2068c2ecf20Sopenharmony_ci			continue;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		/* Check the standard checksum */
2098c2ecf20Sopenharmony_ci		if (compute_checksum((u8 *)rsdp, ACPI_RSDP_CHECKSUM_LENGTH))
2108c2ecf20Sopenharmony_ci			continue;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		/* Check extended checksum if table version >= 2 */
2138c2ecf20Sopenharmony_ci		if ((rsdp->revision >= 2) &&
2148c2ecf20Sopenharmony_ci		    (compute_checksum((u8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)))
2158c2ecf20Sopenharmony_ci			continue;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci		/* Signature and checksum valid, we have found a real RSDP */
2188c2ecf20Sopenharmony_ci		return address;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci	return NULL;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/* Search RSDP address in EBDA. */
2248c2ecf20Sopenharmony_cistatic acpi_physical_address bios_get_rsdp_addr(void)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	unsigned long address;
2278c2ecf20Sopenharmony_ci	u8 *rsdp;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Get the location of the Extended BIOS Data Area (EBDA) */
2308c2ecf20Sopenharmony_ci	address = *(u16 *)ACPI_EBDA_PTR_LOCATION;
2318c2ecf20Sopenharmony_ci	address <<= 4;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	/*
2348c2ecf20Sopenharmony_ci	 * Search EBDA paragraphs (EBDA is required to be a minimum of
2358c2ecf20Sopenharmony_ci	 * 1K length)
2368c2ecf20Sopenharmony_ci	 */
2378c2ecf20Sopenharmony_ci	if (address > 0x400) {
2388c2ecf20Sopenharmony_ci		rsdp = scan_mem_for_rsdp((u8 *)address, ACPI_EBDA_WINDOW_SIZE);
2398c2ecf20Sopenharmony_ci		if (rsdp)
2408c2ecf20Sopenharmony_ci			return (acpi_physical_address)(unsigned long)rsdp;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	/* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */
2448c2ecf20Sopenharmony_ci	rsdp = scan_mem_for_rsdp((u8 *) ACPI_HI_RSDP_WINDOW_BASE,
2458c2ecf20Sopenharmony_ci					ACPI_HI_RSDP_WINDOW_SIZE);
2468c2ecf20Sopenharmony_ci	if (rsdp)
2478c2ecf20Sopenharmony_ci		return (acpi_physical_address)(unsigned long)rsdp;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	return 0;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/* Return RSDP address on success, otherwise 0. */
2538c2ecf20Sopenharmony_ciacpi_physical_address get_rsdp_addr(void)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	acpi_physical_address pa;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	pa = boot_params->acpi_rsdp_addr;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	/*
2608c2ecf20Sopenharmony_ci	 * Try to get EFI data from setup_data. This can happen when we're a
2618c2ecf20Sopenharmony_ci	 * kexec'ed kernel and kexec(1) has passed all the required EFI info to
2628c2ecf20Sopenharmony_ci	 * us.
2638c2ecf20Sopenharmony_ci	 */
2648c2ecf20Sopenharmony_ci	if (!pa)
2658c2ecf20Sopenharmony_ci		pa = kexec_get_rsdp_addr();
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	if (!pa)
2688c2ecf20Sopenharmony_ci		pa = efi_get_rsdp_addr();
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	if (!pa)
2718c2ecf20Sopenharmony_ci		pa = bios_get_rsdp_addr();
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return pa;
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci#if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_MEMORY_HOTREMOVE)
2778c2ecf20Sopenharmony_ci/*
2788c2ecf20Sopenharmony_ci * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex
2798c2ecf20Sopenharmony_ci * digits, and '\0' for termination.
2808c2ecf20Sopenharmony_ci */
2818c2ecf20Sopenharmony_ci#define MAX_ADDR_LEN 19
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic unsigned long get_cmdline_acpi_rsdp(void)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	unsigned long addr = 0;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci#ifdef CONFIG_KEXEC
2888c2ecf20Sopenharmony_ci	char val[MAX_ADDR_LEN] = { };
2898c2ecf20Sopenharmony_ci	int ret;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	ret = cmdline_find_option("acpi_rsdp", val, MAX_ADDR_LEN);
2928c2ecf20Sopenharmony_ci	if (ret < 0)
2938c2ecf20Sopenharmony_ci		return 0;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (boot_kstrtoul(val, 16, &addr))
2968c2ecf20Sopenharmony_ci		return 0;
2978c2ecf20Sopenharmony_ci#endif
2988c2ecf20Sopenharmony_ci	return addr;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci/* Compute SRAT address from RSDP. */
3028c2ecf20Sopenharmony_cistatic unsigned long get_acpi_srat_table(void)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	unsigned long root_table, acpi_table;
3058c2ecf20Sopenharmony_ci	struct acpi_table_header *header;
3068c2ecf20Sopenharmony_ci	struct acpi_table_rsdp *rsdp;
3078c2ecf20Sopenharmony_ci	u32 num_entries, size, len;
3088c2ecf20Sopenharmony_ci	char arg[10];
3098c2ecf20Sopenharmony_ci	u8 *entry;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/*
3128c2ecf20Sopenharmony_ci	 * Check whether we were given an RSDP on the command line. We don't
3138c2ecf20Sopenharmony_ci	 * stash this in boot params because the kernel itself may have
3148c2ecf20Sopenharmony_ci	 * different ideas about whether to trust a command-line parameter.
3158c2ecf20Sopenharmony_ci	 */
3168c2ecf20Sopenharmony_ci	rsdp = (struct acpi_table_rsdp *)get_cmdline_acpi_rsdp();
3178c2ecf20Sopenharmony_ci	if (!rsdp)
3188c2ecf20Sopenharmony_ci		rsdp = (struct acpi_table_rsdp *)(long)
3198c2ecf20Sopenharmony_ci			boot_params->acpi_rsdp_addr;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	if (!rsdp)
3228c2ecf20Sopenharmony_ci		return 0;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* Get ACPI root table from RSDP.*/
3258c2ecf20Sopenharmony_ci	if (!(cmdline_find_option("acpi", arg, sizeof(arg)) == 4 &&
3268c2ecf20Sopenharmony_ci	    !strncmp(arg, "rsdt", 4)) &&
3278c2ecf20Sopenharmony_ci	    rsdp->xsdt_physical_address &&
3288c2ecf20Sopenharmony_ci	    rsdp->revision > 1) {
3298c2ecf20Sopenharmony_ci		root_table = rsdp->xsdt_physical_address;
3308c2ecf20Sopenharmony_ci		size = ACPI_XSDT_ENTRY_SIZE;
3318c2ecf20Sopenharmony_ci	} else {
3328c2ecf20Sopenharmony_ci		root_table = rsdp->rsdt_physical_address;
3338c2ecf20Sopenharmony_ci		size = ACPI_RSDT_ENTRY_SIZE;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (!root_table)
3378c2ecf20Sopenharmony_ci		return 0;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	header = (struct acpi_table_header *)root_table;
3408c2ecf20Sopenharmony_ci	len = header->length;
3418c2ecf20Sopenharmony_ci	if (len < sizeof(struct acpi_table_header) + size)
3428c2ecf20Sopenharmony_ci		return 0;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	num_entries = (len - sizeof(struct acpi_table_header)) / size;
3458c2ecf20Sopenharmony_ci	entry = (u8 *)(root_table + sizeof(struct acpi_table_header));
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	while (num_entries--) {
3488c2ecf20Sopenharmony_ci		if (size == ACPI_RSDT_ENTRY_SIZE)
3498c2ecf20Sopenharmony_ci			acpi_table = *(u32 *)entry;
3508c2ecf20Sopenharmony_ci		else
3518c2ecf20Sopenharmony_ci			acpi_table = *(u64 *)entry;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		if (acpi_table) {
3548c2ecf20Sopenharmony_ci			header = (struct acpi_table_header *)acpi_table;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci			if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT))
3578c2ecf20Sopenharmony_ci				return acpi_table;
3588c2ecf20Sopenharmony_ci		}
3598c2ecf20Sopenharmony_ci		entry += size;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci	return 0;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci/**
3658c2ecf20Sopenharmony_ci * count_immovable_mem_regions - Parse SRAT and cache the immovable
3668c2ecf20Sopenharmony_ci * memory regions into the immovable_mem array.
3678c2ecf20Sopenharmony_ci *
3688c2ecf20Sopenharmony_ci * Return the number of immovable memory regions on success, 0 on failure:
3698c2ecf20Sopenharmony_ci *
3708c2ecf20Sopenharmony_ci * - Too many immovable memory regions
3718c2ecf20Sopenharmony_ci * - ACPI off or no SRAT found
3728c2ecf20Sopenharmony_ci * - No immovable memory region found.
3738c2ecf20Sopenharmony_ci */
3748c2ecf20Sopenharmony_ciint count_immovable_mem_regions(void)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	unsigned long table_addr, table_end, table;
3778c2ecf20Sopenharmony_ci	struct acpi_subtable_header *sub_table;
3788c2ecf20Sopenharmony_ci	struct acpi_table_header *table_header;
3798c2ecf20Sopenharmony_ci	char arg[MAX_ACPI_ARG_LENGTH];
3808c2ecf20Sopenharmony_ci	int num = 0;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	if (cmdline_find_option("acpi", arg, sizeof(arg)) == 3 &&
3838c2ecf20Sopenharmony_ci	    !strncmp(arg, "off", 3))
3848c2ecf20Sopenharmony_ci		return 0;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	table_addr = get_acpi_srat_table();
3878c2ecf20Sopenharmony_ci	if (!table_addr)
3888c2ecf20Sopenharmony_ci		return 0;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	table_header = (struct acpi_table_header *)table_addr;
3918c2ecf20Sopenharmony_ci	table_end = table_addr + table_header->length;
3928c2ecf20Sopenharmony_ci	table = table_addr + sizeof(struct acpi_table_srat);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	while (table + sizeof(struct acpi_subtable_header) < table_end) {
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci		sub_table = (struct acpi_subtable_header *)table;
3978c2ecf20Sopenharmony_ci		if (!sub_table->length) {
3988c2ecf20Sopenharmony_ci			debug_putstr("Invalid zero length SRAT subtable.\n");
3998c2ecf20Sopenharmony_ci			return 0;
4008c2ecf20Sopenharmony_ci		}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci		if (sub_table->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) {
4038c2ecf20Sopenharmony_ci			struct acpi_srat_mem_affinity *ma;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci			ma = (struct acpi_srat_mem_affinity *)sub_table;
4068c2ecf20Sopenharmony_ci			if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && ma->length) {
4078c2ecf20Sopenharmony_ci				immovable_mem[num].start = ma->base_address;
4088c2ecf20Sopenharmony_ci				immovable_mem[num].size = ma->length;
4098c2ecf20Sopenharmony_ci				num++;
4108c2ecf20Sopenharmony_ci			}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci			if (num >= MAX_NUMNODES*2) {
4138c2ecf20Sopenharmony_ci				debug_putstr("Too many immovable memory regions, aborting.\n");
4148c2ecf20Sopenharmony_ci				return 0;
4158c2ecf20Sopenharmony_ci			}
4168c2ecf20Sopenharmony_ci		}
4178c2ecf20Sopenharmony_ci		table += sub_table->length;
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci	return num;
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci#endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */
422