Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / acpi / acpica / tbutils.c
blobdfe1ac3ae34a899307a549f8ae2d5ca00511b75f
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: tbutils - ACPI Table utilities
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "actables.h"
14 #define _COMPONENT ACPI_TABLES
15 ACPI_MODULE_NAME("tbutils")
17 /* Local prototypes */
18 static acpi_physical_address
19 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
21 #if (!ACPI_REDUCED_HARDWARE)
22 /*******************************************************************************
24 * FUNCTION: acpi_tb_initialize_facs
26 * PARAMETERS: None
28 * RETURN: Status
30 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
31 * for accessing the Global Lock and Firmware Waking Vector
33 ******************************************************************************/
35 acpi_status acpi_tb_initialize_facs(void)
37 struct acpi_table_facs *facs;
39 /* If Hardware Reduced flag is set, there is no FACS */
41 if (acpi_gbl_reduced_hardware) {
42 acpi_gbl_FACS = NULL;
43 return (AE_OK);
44 } else if (acpi_gbl_FADT.Xfacs &&
45 (!acpi_gbl_FADT.facs
46 || !acpi_gbl_use32_bit_facs_addresses)) {
47 (void)acpi_get_table_by_index(acpi_gbl_xfacs_index,
48 ACPI_CAST_INDIRECT_PTR(struct
49 acpi_table_header,
50 &facs));
51 acpi_gbl_FACS = facs;
52 } else if (acpi_gbl_FADT.facs) {
53 (void)acpi_get_table_by_index(acpi_gbl_facs_index,
54 ACPI_CAST_INDIRECT_PTR(struct
55 acpi_table_header,
56 &facs));
57 acpi_gbl_FACS = facs;
60 /* If there is no FACS, just continue. There was already an error msg */
62 return (AE_OK);
64 #endif /* !ACPI_REDUCED_HARDWARE */
66 /*******************************************************************************
68 * FUNCTION: acpi_tb_check_dsdt_header
70 * PARAMETERS: None
72 * RETURN: None
74 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
75 * if the DSDT has been replaced from outside the OS and/or if
76 * the DSDT header has been corrupted.
78 ******************************************************************************/
80 void acpi_tb_check_dsdt_header(void)
83 /* Compare original length and checksum to current values */
85 if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
86 acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
87 ACPI_BIOS_ERROR((AE_INFO,
88 "The DSDT has been corrupted or replaced - "
89 "old, new headers below"));
91 acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
92 acpi_tb_print_table_header(0, acpi_gbl_DSDT);
94 ACPI_ERROR((AE_INFO,
95 "Please send DMI info to linux-acpi@vger.kernel.org\n"
96 "If system does not work as expected, please boot with acpi=copy_dsdt"));
98 /* Disable further error messages */
100 acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
101 acpi_gbl_original_dsdt_header.checksum =
102 acpi_gbl_DSDT->checksum;
106 /*******************************************************************************
108 * FUNCTION: acpi_tb_copy_dsdt
110 * PARAMETERS: table_index - Index of installed table to copy
112 * RETURN: The copied DSDT
114 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
115 * Some very bad BIOSs are known to either corrupt the DSDT or
116 * install a new, bad DSDT. This copy works around the problem.
118 ******************************************************************************/
120 struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
122 struct acpi_table_header *new_table;
123 struct acpi_table_desc *table_desc;
125 table_desc = &acpi_gbl_root_table_list.tables[table_index];
127 new_table = ACPI_ALLOCATE(table_desc->length);
128 if (!new_table) {
129 ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
130 table_desc->length));
131 return (NULL);
134 memcpy(new_table, table_desc->pointer, table_desc->length);
135 acpi_tb_uninstall_table(table_desc);
137 acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
138 tables[acpi_gbl_dsdt_index],
139 ACPI_PTR_TO_PHYSADDR(new_table),
140 ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
141 new_table);
143 ACPI_INFO(("Forced DSDT copy: length 0x%05X copied locally, original unmapped", new_table->length));
145 return (new_table);
148 /*******************************************************************************
150 * FUNCTION: acpi_tb_get_root_table_entry
152 * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
153 * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
155 * RETURN: Physical address extracted from the root table
157 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
158 * both 32-bit and 64-bit platforms
160 * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
161 * 64-bit platforms.
163 ******************************************************************************/
165 static acpi_physical_address
166 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
168 u64 address64;
171 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
172 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
174 if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
176 * 32-bit platform, RSDT: Return 32-bit table entry
177 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
179 return ((acpi_physical_address)
180 (*ACPI_CAST_PTR(u32, table_entry)));
181 } else {
183 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
184 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
185 * return 64-bit
187 ACPI_MOVE_64_TO_64(&address64, table_entry);
189 #if ACPI_MACHINE_WIDTH == 32
190 if (address64 > ACPI_UINT32_MAX) {
192 /* Will truncate 64-bit address to 32 bits, issue warning */
194 ACPI_BIOS_WARNING((AE_INFO,
195 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
196 " truncating",
197 ACPI_FORMAT_UINT64(address64)));
199 #endif
200 return ((acpi_physical_address)(address64));
204 /*******************************************************************************
206 * FUNCTION: acpi_tb_parse_root_table
208 * PARAMETERS: rsdp_address - Pointer to the RSDP
210 * RETURN: Status
212 * DESCRIPTION: This function is called to parse the Root System Description
213 * Table (RSDT or XSDT)
215 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
216 * be mapped and cannot be copied because it contains the actual
217 * memory location of the ACPI Global Lock.
219 ******************************************************************************/
221 acpi_status ACPI_INIT_FUNCTION
222 acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
224 struct acpi_table_rsdp *rsdp;
225 u32 table_entry_size;
226 u32 i;
227 u32 table_count;
228 struct acpi_table_header *table;
229 acpi_physical_address address;
230 u32 length;
231 u8 *table_entry;
232 acpi_status status;
233 u32 table_index;
235 ACPI_FUNCTION_TRACE(tb_parse_root_table);
237 /* Map the entire RSDP and extract the address of the RSDT or XSDT */
239 rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
240 if (!rsdp) {
241 return_ACPI_STATUS(AE_NO_MEMORY);
244 acpi_tb_print_table_header(rsdp_address,
245 ACPI_CAST_PTR(struct acpi_table_header,
246 rsdp));
248 /* Use XSDT if present and not overridden. Otherwise, use RSDT */
250 if ((rsdp->revision > 1) &&
251 rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
253 * RSDP contains an XSDT (64-bit physical addresses). We must use
254 * the XSDT if the revision is > 1 and the XSDT pointer is present,
255 * as per the ACPI specification.
257 address = (acpi_physical_address)rsdp->xsdt_physical_address;
258 table_entry_size = ACPI_XSDT_ENTRY_SIZE;
259 } else {
260 /* Root table is an RSDT (32-bit physical addresses) */
262 address = (acpi_physical_address)rsdp->rsdt_physical_address;
263 table_entry_size = ACPI_RSDT_ENTRY_SIZE;
267 * It is not possible to map more than one entry in some environments,
268 * so unmap the RSDP here before mapping other tables
270 acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
272 /* Map the RSDT/XSDT table header to get the full table length */
274 table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
275 if (!table) {
276 return_ACPI_STATUS(AE_NO_MEMORY);
279 acpi_tb_print_table_header(address, table);
282 * Validate length of the table, and map entire table.
283 * Minimum length table must contain at least one entry.
285 length = table->length;
286 acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
288 if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
289 ACPI_BIOS_ERROR((AE_INFO,
290 "Invalid table length 0x%X in RSDT/XSDT",
291 length));
292 return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
295 table = acpi_os_map_memory(address, length);
296 if (!table) {
297 return_ACPI_STATUS(AE_NO_MEMORY);
300 /* Validate the root table checksum */
302 status = acpi_tb_verify_checksum(table, length);
303 if (ACPI_FAILURE(status)) {
304 acpi_os_unmap_memory(table, length);
305 return_ACPI_STATUS(status);
308 /* Get the number of entries and pointer to first entry */
310 table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
311 table_entry_size);
312 table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
314 /* Initialize the root table array from the RSDT/XSDT */
316 for (i = 0; i < table_count; i++) {
318 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
320 address =
321 acpi_tb_get_root_table_entry(table_entry, table_entry_size);
323 /* Skip NULL entries in RSDT/XSDT */
325 if (!address) {
326 goto next_table;
329 status = acpi_tb_install_standard_table(address,
330 ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
331 FALSE, TRUE,
332 &table_index);
334 if (ACPI_SUCCESS(status) &&
335 ACPI_COMPARE_NAMESEG(&acpi_gbl_root_table_list.
336 tables[table_index].signature,
337 ACPI_SIG_FADT)) {
338 acpi_gbl_fadt_index = table_index;
339 acpi_tb_parse_fadt();
342 next_table:
344 table_entry += table_entry_size;
347 acpi_os_unmap_memory(table, length);
348 return_ACPI_STATUS(AE_OK);
351 /*******************************************************************************
353 * FUNCTION: acpi_tb_get_table
355 * PARAMETERS: table_desc - Table descriptor
356 * out_table - Where the pointer to the table is returned
358 * RETURN: Status and pointer to the requested table
360 * DESCRIPTION: Increase a reference to a table descriptor and return the
361 * validated table pointer.
362 * If the table descriptor is an entry of the root table list,
363 * this API must be invoked with ACPI_MTX_TABLES acquired.
365 ******************************************************************************/
367 acpi_status
368 acpi_tb_get_table(struct acpi_table_desc *table_desc,
369 struct acpi_table_header **out_table)
371 acpi_status status;
373 ACPI_FUNCTION_TRACE(acpi_tb_get_table);
375 if (table_desc->validation_count == 0) {
377 /* Table need to be "VALIDATED" */
379 status = acpi_tb_validate_table(table_desc);
380 if (ACPI_FAILURE(status)) {
381 return_ACPI_STATUS(status);
385 if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
386 table_desc->validation_count++;
389 * Detect validation_count overflows to ensure that the warning
390 * message will only be printed once.
392 if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
393 ACPI_WARNING((AE_INFO,
394 "Table %p, Validation count overflows\n",
395 table_desc));
399 *out_table = table_desc->pointer;
400 return_ACPI_STATUS(AE_OK);
403 /*******************************************************************************
405 * FUNCTION: acpi_tb_put_table
407 * PARAMETERS: table_desc - Table descriptor
409 * RETURN: None
411 * DESCRIPTION: Decrease a reference to a table descriptor and release the
412 * validated table pointer if no references.
413 * If the table descriptor is an entry of the root table list,
414 * this API must be invoked with ACPI_MTX_TABLES acquired.
416 ******************************************************************************/
418 void acpi_tb_put_table(struct acpi_table_desc *table_desc)
421 ACPI_FUNCTION_TRACE(acpi_tb_put_table);
423 if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
424 table_desc->validation_count--;
427 * Detect validation_count underflows to ensure that the warning
428 * message will only be printed once.
430 if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
431 ACPI_WARNING((AE_INFO,
432 "Table %p, Validation count underflows\n",
433 table_desc));
434 return_VOID;
438 if (table_desc->validation_count == 0) {
440 /* Table need to be "INVALIDATED" */
442 acpi_tb_invalidate_table(table_desc);
445 return_VOID;