1 // SPDX-License-Identifier: GPL-2.0 2 #define BOOT_CTYPE_H 3 #include "misc.h" 4 #include "error.h" 5 #include "../string.h" 6 #include "efi.h" 7 8 #include <asm/bootparam.h> 9 10 #include <linux/numa.h> 11 12 /* 13 * Longest parameter of 'acpi=' is 'copy_dsdt', plus an extra '\0' 14 * for termination. 15 */ 16 #define MAX_ACPI_ARG_LENGTH 10 17 18 /* 19 * Immovable memory regions representation. Max amount of memory regions is 20 * MAX_NUMNODES*2. 21 */ 22 struct mem_vector immovable_mem[MAX_NUMNODES*2]; 23 24 static acpi_physical_address 25 __efi_get_rsdp_addr(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len) 26 { 27 #ifdef CONFIG_EFI 28 unsigned long rsdp_addr; 29 int ret; 30 31 /* 32 * Search EFI system tables for RSDP. Preferred is ACPI_20_TABLE_GUID to 33 * ACPI_TABLE_GUID because it has more features. 34 */ 35 rsdp_addr = efi_find_vendor_table(boot_params_ptr, cfg_tbl_pa, cfg_tbl_len, 36 ACPI_20_TABLE_GUID); 37 if (rsdp_addr) 38 return (acpi_physical_address)rsdp_addr; 39 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, 42 ACPI_TABLE_GUID); 43 if (rsdp_addr) 44 return (acpi_physical_address)rsdp_addr; 45 46 debug_putstr("Error getting RSDP address.\n"); 47 #endif 48 return 0; 49 } 50 51 static acpi_physical_address efi_get_rsdp_addr(void) 52 { 53 #ifdef CONFIG_EFI 54 unsigned long cfg_tbl_pa = 0; 55 unsigned int cfg_tbl_len; 56 unsigned long systab_pa; 57 unsigned int nr_tables; 58 enum efi_type et; 59 int ret; 60 61 et = efi_get_type(boot_params_ptr); 62 if (et == EFI_TYPE_NONE) 63 return 0; 64 65 systab_pa = efi_get_system_table(boot_params_ptr); 66 if (!systab_pa) 67 error("EFI support advertised, but unable to locate system table."); 68 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."); 72 73 return __efi_get_rsdp_addr(cfg_tbl_pa, cfg_tbl_len); 74 #else 75 return 0; 76 #endif 77 } 78 79 static u8 compute_checksum(u8 *buffer, u32 length) 80 { 81 u8 *end = buffer + length; 82 u8 sum = 0; 83 84 while (buffer < end) 85 sum += *(buffer++); 86 87 return sum; 88 } 89 90 /* Search a block of memory for the RSDP signature. */ 91 static u8 *scan_mem_for_rsdp(u8 *start, u32 length) 92 { 93 struct acpi_table_rsdp *rsdp; 94 u8 *address, *end; 95 96 end = start + length; 97 98 /* Search from given start address for the requested length */ 99 for (address = start; address < end; address += ACPI_RSDP_SCAN_STEP) { 100 /* 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 104 * invalid checksum. 105 */ 106 rsdp = (struct acpi_table_rsdp *)address; 107 108 /* BAD Signature */ 109 if (!ACPI_VALIDATE_RSDP_SIG(rsdp->signature)) 110 continue; 111 112 /* Check the standard checksum */ 113 if (compute_checksum((u8 *)rsdp, ACPI_RSDP_CHECKSUM_LENGTH)) 114 continue; 115 116 /* Check extended checksum if table version >= 2 */ 117 if ((rsdp->revision >= 2) && 118 (compute_checksum((u8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH))) 119 continue; 120 121 /* Signature and checksum valid, we have found a real RSDP */ 122 return address; 123 } 124 return NULL; 125 } 126 127 /* Search RSDP address in EBDA. */ 128 static acpi_physical_address bios_get_rsdp_addr(void) 129 { 130 unsigned long address; 131 u8 *rsdp; 132 133 /* Get the location of the Extended BIOS Data Area (EBDA) */ 134 address = *(u16 *)ACPI_EBDA_PTR_LOCATION; 135 address <<= 4; 136 137 /* 138 * Search EBDA paragraphs (EBDA is required to be a minimum of 139 * 1K length) 140 */ 141 if (address > 0x400) { 142 rsdp = scan_mem_for_rsdp((u8 *)address, ACPI_EBDA_WINDOW_SIZE); 143 if (rsdp) 144 return (acpi_physical_address)(unsigned long)rsdp; 145 } 146 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); 150 if (rsdp) 151 return (acpi_physical_address)(unsigned long)rsdp; 152 153 return 0; 154 } 155 156 /* Return RSDP address on success, otherwise 0. */ 157 acpi_physical_address get_rsdp_addr(void) 158 { 159 acpi_physical_address pa; 160 161 pa = boot_params_ptr->acpi_rsdp_addr; 162 163 if (!pa) 164 pa = efi_get_rsdp_addr(); 165 166 if (!pa) 167 pa = bios_get_rsdp_addr(); 168 169 return pa; 170 } 171 172 #if defined(CONFIG_RANDOMIZE_BASE) && defined(CONFIG_MEMORY_HOTREMOVE) 173 /* 174 * Max length of 64-bit hex address string is 19, prefix "0x" + 16 hex 175 * digits, and '\0' for termination. 176 */ 177 #define MAX_ADDR_LEN 19 178 179 static unsigned long get_cmdline_acpi_rsdp(void) 180 { 181 unsigned long addr = 0; 182 183 #ifdef CONFIG_KEXEC_CORE 184 char val[MAX_ADDR_LEN] = { }; 185 int ret; 186 187 ret = cmdline_find_option("acpi_rsdp", val, sizeof(val)); 188 if (ret < 0) 189 return 0; 190 191 if (ret >= sizeof(val)) { 192 warn("acpi_rsdp= value too long; ignoring"); 193 return 0; 194 } 195 196 if (boot_kstrtoul(val, 16, &addr)) 197 return 0; 198 #endif 199 return addr; 200 } 201 202 /* Compute SRAT address from RSDP. */ 203 static unsigned long get_acpi_srat_table(void) 204 { 205 unsigned long root_table, acpi_table; 206 struct acpi_table_header *header; 207 struct acpi_table_rsdp *rsdp; 208 u32 num_entries, size, len; 209 char arg[10]; 210 u8 *entry; 211 212 /* 213 * Check whether we were given an RSDP on the command line. We don't 214 * stash this in boot params because the kernel itself may have 215 * different ideas about whether to trust a command-line parameter. 216 */ 217 rsdp = (struct acpi_table_rsdp *)get_cmdline_acpi_rsdp(); 218 if (!rsdp) 219 rsdp = (struct acpi_table_rsdp *)(long) 220 boot_params_ptr->acpi_rsdp_addr; 221 222 if (!rsdp) 223 return 0; 224 225 /* Get ACPI root table from RSDP.*/ 226 if (!(cmdline_find_option("acpi", arg, sizeof(arg)) == 4 && 227 !strncmp(arg, "rsdt", 4)) && 228 rsdp->xsdt_physical_address && 229 rsdp->revision > 1) { 230 root_table = rsdp->xsdt_physical_address; 231 size = ACPI_XSDT_ENTRY_SIZE; 232 } else { 233 root_table = rsdp->rsdt_physical_address; 234 size = ACPI_RSDT_ENTRY_SIZE; 235 } 236 237 if (!root_table) 238 return 0; 239 240 header = (struct acpi_table_header *)root_table; 241 len = header->length; 242 if (len < sizeof(struct acpi_table_header) + size) 243 return 0; 244 245 num_entries = (len - sizeof(struct acpi_table_header)) / size; 246 entry = (u8 *)(root_table + sizeof(struct acpi_table_header)); 247 248 while (num_entries--) { 249 if (size == ACPI_RSDT_ENTRY_SIZE) 250 acpi_table = *(u32 *)entry; 251 else 252 acpi_table = *(u64 *)entry; 253 254 if (acpi_table) { 255 header = (struct acpi_table_header *)acpi_table; 256 257 if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT)) 258 return acpi_table; 259 } 260 entry += size; 261 } 262 return 0; 263 } 264 265 /** 266 * count_immovable_mem_regions - Parse SRAT and cache the immovable 267 * memory regions into the immovable_mem array. 268 * 269 * Return the number of immovable memory regions on success, 0 on failure: 270 * 271 * - Too many immovable memory regions 272 * - ACPI off or no SRAT found 273 * - No immovable memory region found. 274 */ 275 int count_immovable_mem_regions(void) 276 { 277 unsigned long table_addr, table_end, table; 278 struct acpi_subtable_header *sub_table; 279 struct acpi_table_header *table_header; 280 char arg[MAX_ACPI_ARG_LENGTH]; 281 int num = 0; 282 283 if (cmdline_find_option("acpi", arg, sizeof(arg)) == 3 && 284 !strncmp(arg, "off", 3)) 285 return 0; 286 287 table_addr = get_acpi_srat_table(); 288 if (!table_addr) 289 return 0; 290 291 table_header = (struct acpi_table_header *)table_addr; 292 table_end = table_addr + table_header->length; 293 table = table_addr + sizeof(struct acpi_table_srat); 294 295 while (table + sizeof(struct acpi_subtable_header) < table_end) { 296 297 sub_table = (struct acpi_subtable_header *)table; 298 if (!sub_table->length) { 299 debug_putstr("Invalid zero length SRAT subtable.\n"); 300 return 0; 301 } 302 303 if (sub_table->type == ACPI_SRAT_TYPE_MEMORY_AFFINITY) { 304 struct acpi_srat_mem_affinity *ma; 305 306 ma = (struct acpi_srat_mem_affinity *)sub_table; 307 if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) && ma->length) { 308 immovable_mem[num].start = ma->base_address; 309 immovable_mem[num].size = ma->length; 310 num++; 311 } 312 313 if (num >= MAX_NUMNODES*2) { 314 debug_putstr("Too many immovable memory regions, aborting.\n"); 315 return 0; 316 } 317 } 318 table += sub_table->length; 319 } 320 return num; 321 } 322 #endif /* CONFIG_RANDOMIZE_BASE && CONFIG_MEMORY_HOTREMOVE */ 323