Linux Audio

Check our new training course

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