drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / acpi / acpica / tbutils.c
blobdad7425fce3f103c05d04803e5545dcb1f79a223
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: tbutils - ACPI Table utilities
6 * Copyright (C) 2000 - 2023, 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 /*******************************************************************************
23 * FUNCTION: acpi_tb_initialize_facs
25 * PARAMETERS: None
27 * RETURN: Status
29 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
30 * for accessing the Global Lock and Firmware Waking Vector
32 ******************************************************************************/
34 acpi_status acpi_tb_initialize_facs(void)
36 struct acpi_table_facs *facs;
38 if (acpi_gbl_FADT.Xfacs &&
39 (!acpi_gbl_FADT.facs
40 || !acpi_gbl_use32_bit_facs_addresses)) {
41 (void)acpi_get_table_by_index(acpi_gbl_xfacs_index,
42 ACPI_CAST_INDIRECT_PTR(struct
43 acpi_table_header,
44 &facs));
45 acpi_gbl_FACS = facs;
46 } else if (acpi_gbl_FADT.facs) {
47 (void)acpi_get_table_by_index(acpi_gbl_facs_index,
48 ACPI_CAST_INDIRECT_PTR(struct
49 acpi_table_header,
50 &facs));
51 acpi_gbl_FACS = facs;
54 /* If there is no FACS, just continue. There was already an error msg */
56 return (AE_OK);
59 /*******************************************************************************
61 * FUNCTION: acpi_tb_check_dsdt_header
63 * PARAMETERS: None
65 * RETURN: None
67 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
68 * if the DSDT has been replaced from outside the OS and/or if
69 * the DSDT header has been corrupted.
71 ******************************************************************************/
73 void acpi_tb_check_dsdt_header(void)
76 /* Compare original length and checksum to current values */
78 if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
79 acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
80 ACPI_BIOS_ERROR((AE_INFO,
81 "The DSDT has been corrupted or replaced - "
82 "old, new headers below"));
84 acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
85 acpi_tb_print_table_header(0, acpi_gbl_DSDT);
87 ACPI_ERROR((AE_INFO,
88 "Please send DMI info to linux-acpi@vger.kernel.org\n"
89 "If system does not work as expected, please boot with acpi=copy_dsdt"));
91 /* Disable further error messages */
93 acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
94 acpi_gbl_original_dsdt_header.checksum =
95 acpi_gbl_DSDT->checksum;
99 /*******************************************************************************
101 * FUNCTION: acpi_tb_copy_dsdt
103 * PARAMETERS: table_index - Index of installed table to copy
105 * RETURN: The copied DSDT
107 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
108 * Some very bad BIOSs are known to either corrupt the DSDT or
109 * install a new, bad DSDT. This copy works around the problem.
111 ******************************************************************************/
113 struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
115 struct acpi_table_header *new_table;
116 struct acpi_table_desc *table_desc;
118 table_desc = &acpi_gbl_root_table_list.tables[table_index];
120 new_table = ACPI_ALLOCATE(table_desc->length);
121 if (!new_table) {
122 ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
123 table_desc->length));
124 return (NULL);
127 memcpy(new_table, table_desc->pointer, table_desc->length);
128 acpi_tb_uninstall_table(table_desc);
130 acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
131 tables[acpi_gbl_dsdt_index],
132 ACPI_PTR_TO_PHYSADDR(new_table),
133 ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
134 new_table);
136 ACPI_INFO(("Forced DSDT copy: length 0x%05X copied locally, original unmapped", new_table->length));
138 return (new_table);
141 /*******************************************************************************
143 * FUNCTION: acpi_tb_get_root_table_entry
145 * PARAMETERS: table_entry - Pointer to the RSDT/XSDT table entry
146 * table_entry_size - sizeof 32 or 64 (RSDT or XSDT)
148 * RETURN: Physical address extracted from the root table
150 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
151 * both 32-bit and 64-bit platforms
153 * NOTE: acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
154 * 64-bit platforms.
156 ******************************************************************************/
158 static acpi_physical_address
159 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
161 u32 address32;
162 u64 address64;
165 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
166 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
168 if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
170 * 32-bit platform, RSDT: Return 32-bit table entry
171 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
173 ACPI_MOVE_32_TO_32(&address32, table_entry);
174 return address32;
175 } else {
177 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
178 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
179 * return 64-bit
181 ACPI_MOVE_64_TO_64(&address64, table_entry);
183 #if ACPI_MACHINE_WIDTH == 32
184 if (address64 > ACPI_UINT32_MAX) {
186 /* Will truncate 64-bit address to 32 bits, issue warning */
188 ACPI_BIOS_WARNING((AE_INFO,
189 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
190 " truncating",
191 ACPI_FORMAT_UINT64(address64)));
193 #endif
194 return ((acpi_physical_address)(address64));
198 /*******************************************************************************
200 * FUNCTION: acpi_tb_parse_root_table
202 * PARAMETERS: rsdp_address - Pointer to the RSDP
204 * RETURN: Status
206 * DESCRIPTION: This function is called to parse the Root System Description
207 * Table (RSDT or XSDT)
209 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
210 * be mapped and cannot be copied because it contains the actual
211 * memory location of the ACPI Global Lock.
213 ******************************************************************************/
215 acpi_status ACPI_INIT_FUNCTION
216 acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
218 struct acpi_table_rsdp *rsdp;
219 u32 table_entry_size;
220 u32 i;
221 u32 table_count;
222 struct acpi_table_header *table;
223 acpi_physical_address address;
224 u32 length;
225 u8 *table_entry;
226 acpi_status status;
227 u32 table_index;
229 ACPI_FUNCTION_TRACE(tb_parse_root_table);
231 /* Map the entire RSDP and extract the address of the RSDT or XSDT */
233 rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
234 if (!rsdp) {
235 return_ACPI_STATUS(AE_NO_MEMORY);
238 acpi_tb_print_table_header(rsdp_address,
239 ACPI_CAST_PTR(struct acpi_table_header,
240 rsdp));
242 /* Use XSDT if present and not overridden. Otherwise, use RSDT */
244 if ((rsdp->revision > 1) &&
245 rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
247 * RSDP contains an XSDT (64-bit physical addresses). We must use
248 * the XSDT if the revision is > 1 and the XSDT pointer is present,
249 * as per the ACPI specification.
251 address = (acpi_physical_address)rsdp->xsdt_physical_address;
252 table_entry_size = ACPI_XSDT_ENTRY_SIZE;
253 } else {
254 /* Root table is an RSDT (32-bit physical addresses) */
256 address = (acpi_physical_address)rsdp->rsdt_physical_address;
257 table_entry_size = ACPI_RSDT_ENTRY_SIZE;
261 * It is not possible to map more than one entry in some environments,
262 * so unmap the RSDP here before mapping other tables
264 acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
266 /* Map the RSDT/XSDT table header to get the full table length */
268 table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
269 if (!table) {
270 return_ACPI_STATUS(AE_NO_MEMORY);
273 acpi_tb_print_table_header(address, table);
276 * Validate length of the table, and map entire table.
277 * Minimum length table must contain at least one entry.
279 length = table->length;
280 acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
282 if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
283 ACPI_BIOS_ERROR((AE_INFO,
284 "Invalid table length 0x%X in RSDT/XSDT",
285 length));
286 return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
289 table = acpi_os_map_memory(address, length);
290 if (!table) {
291 return_ACPI_STATUS(AE_NO_MEMORY);
294 /* Validate the root table checksum */
296 status = acpi_ut_verify_checksum(table, length);
297 if (ACPI_FAILURE(status)) {
298 acpi_os_unmap_memory(table, length);
299 return_ACPI_STATUS(status);
302 /* Get the number of entries and pointer to first entry */
304 table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
305 table_entry_size);
306 table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
308 /* Initialize the root table array from the RSDT/XSDT */
310 for (i = 0; i < table_count; i++) {
312 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
314 address =
315 acpi_tb_get_root_table_entry(table_entry, table_entry_size);
317 /* Skip NULL entries in RSDT/XSDT */
319 if (!address) {
320 goto next_table;
323 status = acpi_tb_install_standard_table(address,
324 ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
325 NULL, FALSE, TRUE,
326 &table_index);
328 if (ACPI_SUCCESS(status) &&
329 ACPI_COMPARE_NAMESEG(&acpi_gbl_root_table_list.
330 tables[table_index].signature,
331 ACPI_SIG_FADT)) {
332 acpi_gbl_fadt_index = table_index;
333 acpi_tb_parse_fadt();
336 next_table:
338 table_entry += table_entry_size;
341 acpi_os_unmap_memory(table, length);
342 return_ACPI_STATUS(AE_OK);
345 /*******************************************************************************
347 * FUNCTION: acpi_tb_get_table
349 * PARAMETERS: table_desc - Table descriptor
350 * out_table - Where the pointer to the table is returned
352 * RETURN: Status and pointer to the requested table
354 * DESCRIPTION: Increase a reference to a table descriptor and return the
355 * validated table pointer.
356 * If the table descriptor is an entry of the root table list,
357 * this API must be invoked with ACPI_MTX_TABLES acquired.
359 ******************************************************************************/
361 acpi_status
362 acpi_tb_get_table(struct acpi_table_desc *table_desc,
363 struct acpi_table_header **out_table)
365 acpi_status status;
367 ACPI_FUNCTION_TRACE(acpi_tb_get_table);
369 if (table_desc->validation_count == 0) {
371 /* Table need to be "VALIDATED" */
373 status = acpi_tb_validate_table(table_desc);
374 if (ACPI_FAILURE(status)) {
375 return_ACPI_STATUS(status);
379 if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
380 table_desc->validation_count++;
383 * Detect validation_count overflows to ensure that the warning
384 * message will only be printed once.
386 if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
387 ACPI_WARNING((AE_INFO,
388 "Table %p, Validation count overflows\n",
389 table_desc));
393 *out_table = table_desc->pointer;
394 return_ACPI_STATUS(AE_OK);
397 /*******************************************************************************
399 * FUNCTION: acpi_tb_put_table
401 * PARAMETERS: table_desc - Table descriptor
403 * RETURN: None
405 * DESCRIPTION: Decrease a reference to a table descriptor and release the
406 * validated table pointer if no references.
407 * If the table descriptor is an entry of the root table list,
408 * this API must be invoked with ACPI_MTX_TABLES acquired.
410 ******************************************************************************/
412 void acpi_tb_put_table(struct acpi_table_desc *table_desc)
415 ACPI_FUNCTION_TRACE(acpi_tb_put_table);
417 if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
418 table_desc->validation_count--;
421 * Detect validation_count underflows to ensure that the warning
422 * message will only be printed once.
424 if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
425 ACPI_WARNING((AE_INFO,
426 "Table %p, Validation count underflows\n",
427 table_desc));
428 return_VOID;
432 if (table_desc->validation_count == 0) {
434 /* Table need to be "INVALIDATED" */
436 acpi_tb_invalidate_table(table_desc);
439 return_VOID;