1 // SPDX-License-Identifier: GPL-2.0
8 #include <asm/bootparam.h>
10 #include <linux/numa.h>
13 * Longest parameter of 'acpi=' is 'copy_dsdt', plus an extra '\0'
16 #define MAX_ACPI_ARG_LENGTH 10
19 * Immovable memory regions representation. Max amount of memory regions is
22 struct mem_vector immovable_mem[MAX_NUMNODES*2];
24 static acpi_physical_address
25 __efi_get_rsdp_addr(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len)
28 unsigned long rsdp_addr;
32 * Search EFI system tables for RSDP. Preferred is ACPI_20_TABLE_GUID to
33 * ACPI_TABLE_GUID because it has more features.
35 rsdp_addr = efi_find_vendor_table(boot_params_ptr, cfg_tbl_pa, cfg_tbl_len,
38 return (acpi_physical_address)rsdp_addr;
40 /* No ACPI_20_TABLE_GUID found, fallback to ACPI_TABLE_GUID. */
41 rsdp_addr = efi_find_vendor_table(boot_params_ptr, cfg_tbl_pa, cfg_tbl_len,
44 return (acpi_physical_address)rsdp_addr;
46 debug_putstr("Error getting RSDP address.\n");
51 static acpi_physical_address efi_get_rsdp_addr(void)
54 unsigned long cfg_tbl_pa = 0;
55 unsigned int cfg_tbl_len;
56 unsigned long systab_pa;
57 unsigned int nr_tables;
61 et = efi_get_type(boot_params_ptr);
62 if (et == EFI_TYPE_NONE)
65 systab_pa = efi_get_system_table(boot_params_ptr);
67 error("EFI support advertised, but unable to locate system table.");
69 ret = efi_get_conf_table(boot_params_ptr, &cfg_tbl_pa, &cfg_tbl_len);
70 if (ret || !cfg_tbl_pa)
71 error("EFI config table not found.");
73 return __efi_get_rsdp_addr(cfg_tbl_pa, cfg_tbl_len);
79 static u8 compute_checksum(u8 *buffer, u32 length)
81 u8 *end = buffer + length;
90 /* Search a block of memory for the RSDP signature. */
91 static u8 *scan_mem_for_rsdp(u8 *start, u32 length)
93 struct acpi_table_rsdp *rsdp;
98 /* Search from given start address for the requested length */
99 for (address = start; address < end; address += ACPI_RSDP_SCAN_STEP) {
101 * Both RSDP signature and checksum must be correct.
102 * Note: Sometimes there exists more than one RSDP in memory;
103 * the valid RSDP has a valid checksum, all others have an
106 rsdp = (struct acpi_table_rsdp *)address;
109 if (!ACPI_VALIDATE_RSDP_SIG(rsdp->signature))
112 /* Check the standard checksum */
113 if (compute_checksum((u8 *)rsdp, ACPI_RSDP_CHECKSUM_LENGTH))
116 /* Check extended checksum if table version >= 2 */
117 if ((rsdp->revision >= 2) &&
118 (compute_checksum((u8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)))
121 /* Signature and checksum valid, we have found a real RSDP */
127 /* Search RSDP address in EBDA. */
128 static acpi_physical_address bios_get_rsdp_addr(void)
130 unsigned long address;
133 /* Get the location of the Extended BIOS Data Area (EBDA) */
134 address = *(u16 *)ACPI_EBDA_PTR_LOCATION;
138 * Search EBDA paragraphs (EBDA is required to be a minimum of
141 if (address > 0x400) {
142 rsdp = scan_mem_for_rsdp((u8 *)address, ACPI_EBDA_WINDOW_SIZE);
144 return (acpi_physical_address)(unsigned long)rsdp;
147 /* Search upper memory: 16-byte boundaries in E0000h-FFFFFh */
148 rsdp = scan_mem_for_rsdp((u8 *) ACPI_HI_RSDP_WINDOW_BASE,
149 ACPI_HI_RSDP_WINDOW_SIZE);
151 return (acpi_physical_address)(unsigned long)rsdp;
156 /* Return RSDP address on success, otherwise 0. */
157 acpi_physical_address get_rsdp_addr(void)
159 acpi_physical_address pa;
161 pa = boot_params_ptr->acpi_rsdp_addr;
164 pa = efi_get_rsdp_addr();
167 pa = bios_get_rsdp_addr();
172 #if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_MEMORY_HOTREMOVE)
174 * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex
175 * digits, and '\0' for termination.
177 #define MAX_ADDR_LEN 19
179 static unsigned long get_cmdline_acpi_rsdp(void)
181 unsigned long addr = 0;
183 #ifdef CONFIG_KEXEC_CORE
184 char val[MAX_ADDR_LEN] = { };
187 ret = cmdline_find_option("acpi_rsdp", val, MAX_ADDR_LEN);
191 if (boot_kstrtoul(val, 16, &addr))
197 /* Compute SRAT address from RSDP. */
198 static unsigned long get_acpi_srat_table(void)
200 unsigned long root_table, acpi_table;
201 struct acpi_table_header *header;
202 struct acpi_table_rsdp *rsdp;
203 u32 num_entries, size, len;
208 * Check whether we were given an RSDP on the command line. We don't
209 * stash this in boot params because the kernel itself may have
210 * different ideas about whether to trust a command-line parameter.
212 rsdp = (struct acpi_table_rsdp *)get_cmdline_acpi_rsdp();
214 rsdp = (struct acpi_table_rsdp *)(long)
215 boot_params_ptr->acpi_rsdp_addr;
220 /* Get ACPI root table from RSDP.*/
221 if (!(cmdline_find_option("acpi", arg, sizeof(arg)) == 4 &&
222 !strncmp(arg, "rsdt", 4)) &&
223 rsdp->xsdt_physical_address &&
224 rsdp->revision > 1) {
225 root_table = rsdp->xsdt_physical_address;
226 size = ACPI_XSDT_ENTRY_SIZE;
228 root_table = rsdp->rsdt_physical_address;
229 size = ACPI_RSDT_ENTRY_SIZE;
235 header = (struct acpi_table_header *)root_table;
236 len = header->length;
237 if (len < sizeof(struct acpi_table_header) + size)
240 num_entries = (len - sizeof(struct acpi_table_header)) / size;
241 entry = (u8 *)(root_table + sizeof(struct acpi_table_header));
243 while (num_entries--) {
244 if (size == ACPI_RSDT_ENTRY_SIZE)
245 acpi_table = *(u32 *)entry;
247 acpi_table = *(u64 *)entry;
250 header = (struct acpi_table_header *)acpi_table;
252 if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT))
261 * count_immovable_mem_regions - Parse SRAT and cache the immovable
262 * memory regions into the immovable_mem array.
264 * Return the number of immovable memory regions on success, 0 on failure:
266 * - Too many immovable memory regions
267 * - ACPI off or no SRAT found
268 * - No immovable memory region found.
270 int count_immovable_mem_regions(void)
272 unsigned long table_addr, table_end, table;
273 struct acpi_subtable_header *sub_table;
274 struct acpi_table_header *table_header;
275 char arg[MAX_ACPI_ARG_LENGTH];
278 if (cmdline_find_option("acpi", arg, sizeof(arg)) == 3 &&
279 !strncmp(arg, "off", 3))
282 table_addr = get_acpi_srat_table();
286 table_header = (struct acpi_table_header *)table_addr;
287 table_end = table_addr + table_header->length;
288 table = table_addr + sizeof(struct acpi_table_srat);
290 while (table + sizeof(struct acpi_subtable_header) < table_end) {
292 sub_table = (struct acpi_subtable_header *)table;
293 if (!sub_table->length) {
294 debug_putstr("Invalid zero length SRAT subtable.\n");
298 if (sub_table->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) {
299 struct acpi_srat_mem_affinity *ma;
301 ma = (struct acpi_srat_mem_affinity *)sub_table;
302 if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && ma->length) {
303 immovable_mem[num].start = ma->base_address;
304 immovable_mem[num].size = ma->length;
308 if (num >= MAX_NUMNODES*2) {
309 debug_putstr("Too many immovable memory regions, aborting.\n");
313 table += sub_table->length;
317 #endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */