xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpica/tbutils.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Module Name: tbutils - ACPI Table utilities
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *****************************************************************************/
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "actables.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #define _COMPONENT          ACPI_TABLES
15*4882a593Smuzhiyun ACPI_MODULE_NAME("tbutils")
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* Local prototypes */
18*4882a593Smuzhiyun static acpi_physical_address
19*4882a593Smuzhiyun acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #if (!ACPI_REDUCED_HARDWARE)
22*4882a593Smuzhiyun /*******************************************************************************
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * FUNCTION:    acpi_tb_initialize_facs
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * PARAMETERS:  None
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * RETURN:      Status
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
31*4882a593Smuzhiyun  *              for accessing the Global Lock and Firmware Waking Vector
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  ******************************************************************************/
34*4882a593Smuzhiyun 
acpi_tb_initialize_facs(void)35*4882a593Smuzhiyun acpi_status acpi_tb_initialize_facs(void)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct acpi_table_facs *facs;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	/* If Hardware Reduced flag is set, there is no FACS */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	if (acpi_gbl_reduced_hardware) {
42*4882a593Smuzhiyun 		acpi_gbl_FACS = NULL;
43*4882a593Smuzhiyun 		return (AE_OK);
44*4882a593Smuzhiyun 	} else if (acpi_gbl_FADT.Xfacs &&
45*4882a593Smuzhiyun 		   (!acpi_gbl_FADT.facs
46*4882a593Smuzhiyun 		    || !acpi_gbl_use32_bit_facs_addresses)) {
47*4882a593Smuzhiyun 		(void)acpi_get_table_by_index(acpi_gbl_xfacs_index,
48*4882a593Smuzhiyun 					      ACPI_CAST_INDIRECT_PTR(struct
49*4882a593Smuzhiyun 								     acpi_table_header,
50*4882a593Smuzhiyun 								     &facs));
51*4882a593Smuzhiyun 		acpi_gbl_FACS = facs;
52*4882a593Smuzhiyun 	} else if (acpi_gbl_FADT.facs) {
53*4882a593Smuzhiyun 		(void)acpi_get_table_by_index(acpi_gbl_facs_index,
54*4882a593Smuzhiyun 					      ACPI_CAST_INDIRECT_PTR(struct
55*4882a593Smuzhiyun 								     acpi_table_header,
56*4882a593Smuzhiyun 								     &facs));
57*4882a593Smuzhiyun 		acpi_gbl_FACS = facs;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* If there is no FACS, just continue. There was already an error msg */
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	return (AE_OK);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun #endif				/* !ACPI_REDUCED_HARDWARE */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /*******************************************************************************
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * FUNCTION:    acpi_tb_check_dsdt_header
69*4882a593Smuzhiyun  *
70*4882a593Smuzhiyun  * PARAMETERS:  None
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * RETURN:      None
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
75*4882a593Smuzhiyun  *              if the DSDT has been replaced from outside the OS and/or if
76*4882a593Smuzhiyun  *              the DSDT header has been corrupted.
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  ******************************************************************************/
79*4882a593Smuzhiyun 
acpi_tb_check_dsdt_header(void)80*4882a593Smuzhiyun void acpi_tb_check_dsdt_header(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* Compare original length and checksum to current values */
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
86*4882a593Smuzhiyun 	    acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
87*4882a593Smuzhiyun 		ACPI_BIOS_ERROR((AE_INFO,
88*4882a593Smuzhiyun 				 "The DSDT has been corrupted or replaced - "
89*4882a593Smuzhiyun 				 "old, new headers below"));
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 		acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
92*4882a593Smuzhiyun 		acpi_tb_print_table_header(0, acpi_gbl_DSDT);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 		ACPI_ERROR((AE_INFO,
95*4882a593Smuzhiyun 			    "Please send DMI info to linux-acpi@vger.kernel.org\n"
96*4882a593Smuzhiyun 			    "If system does not work as expected, please boot with acpi=copy_dsdt"));
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		/* Disable further error messages */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
101*4882a593Smuzhiyun 		acpi_gbl_original_dsdt_header.checksum =
102*4882a593Smuzhiyun 		    acpi_gbl_DSDT->checksum;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /*******************************************************************************
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * FUNCTION:    acpi_tb_copy_dsdt
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  * PARAMETERS:  table_index         - Index of installed table to copy
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * RETURN:      The copied DSDT
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
115*4882a593Smuzhiyun  *              Some very bad BIOSs are known to either corrupt the DSDT or
116*4882a593Smuzhiyun  *              install a new, bad DSDT. This copy works around the problem.
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  ******************************************************************************/
119*4882a593Smuzhiyun 
acpi_tb_copy_dsdt(u32 table_index)120*4882a593Smuzhiyun struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	struct acpi_table_header *new_table;
123*4882a593Smuzhiyun 	struct acpi_table_desc *table_desc;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	table_desc = &acpi_gbl_root_table_list.tables[table_index];
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	new_table = ACPI_ALLOCATE(table_desc->length);
128*4882a593Smuzhiyun 	if (!new_table) {
129*4882a593Smuzhiyun 		ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
130*4882a593Smuzhiyun 			    table_desc->length));
131*4882a593Smuzhiyun 		return (NULL);
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	memcpy(new_table, table_desc->pointer, table_desc->length);
135*4882a593Smuzhiyun 	acpi_tb_uninstall_table(table_desc);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
138*4882a593Smuzhiyun 				      tables[acpi_gbl_dsdt_index],
139*4882a593Smuzhiyun 				      ACPI_PTR_TO_PHYSADDR(new_table),
140*4882a593Smuzhiyun 				      ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
141*4882a593Smuzhiyun 				      new_table);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	ACPI_INFO(("Forced DSDT copy: length 0x%05X copied locally, original unmapped", new_table->length));
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	return (new_table);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /*******************************************************************************
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * FUNCTION:    acpi_tb_get_root_table_entry
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
153*4882a593Smuzhiyun  *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  * RETURN:      Physical address extracted from the root table
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
158*4882a593Smuzhiyun  *              both 32-bit and 64-bit platforms
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun  * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
161*4882a593Smuzhiyun  *              64-bit platforms.
162*4882a593Smuzhiyun  *
163*4882a593Smuzhiyun  ******************************************************************************/
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun static acpi_physical_address
acpi_tb_get_root_table_entry(u8 * table_entry,u32 table_entry_size)166*4882a593Smuzhiyun acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	u64 address64;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	/*
171*4882a593Smuzhiyun 	 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
172*4882a593Smuzhiyun 	 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
173*4882a593Smuzhiyun 	 */
174*4882a593Smuzhiyun 	if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
175*4882a593Smuzhiyun 		/*
176*4882a593Smuzhiyun 		 * 32-bit platform, RSDT: Return 32-bit table entry
177*4882a593Smuzhiyun 		 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
178*4882a593Smuzhiyun 		 */
179*4882a593Smuzhiyun 		return ((acpi_physical_address)
180*4882a593Smuzhiyun 			(*ACPI_CAST_PTR(u32, table_entry)));
181*4882a593Smuzhiyun 	} else {
182*4882a593Smuzhiyun 		/*
183*4882a593Smuzhiyun 		 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
184*4882a593Smuzhiyun 		 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
185*4882a593Smuzhiyun 		 *  return 64-bit
186*4882a593Smuzhiyun 		 */
187*4882a593Smuzhiyun 		ACPI_MOVE_64_TO_64(&address64, table_entry);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun #if ACPI_MACHINE_WIDTH == 32
190*4882a593Smuzhiyun 		if (address64 > ACPI_UINT32_MAX) {
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 			/* Will truncate 64-bit address to 32 bits, issue warning */
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 			ACPI_BIOS_WARNING((AE_INFO,
195*4882a593Smuzhiyun 					   "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
196*4882a593Smuzhiyun 					   " truncating",
197*4882a593Smuzhiyun 					   ACPI_FORMAT_UINT64(address64)));
198*4882a593Smuzhiyun 		}
199*4882a593Smuzhiyun #endif
200*4882a593Smuzhiyun 		return ((acpi_physical_address)(address64));
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun /*******************************************************************************
205*4882a593Smuzhiyun  *
206*4882a593Smuzhiyun  * FUNCTION:    acpi_tb_parse_root_table
207*4882a593Smuzhiyun  *
208*4882a593Smuzhiyun  * PARAMETERS:  rsdp_address        - Pointer to the RSDP
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * RETURN:      Status
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * DESCRIPTION: This function is called to parse the Root System Description
213*4882a593Smuzhiyun  *              Table (RSDT or XSDT)
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
216*4882a593Smuzhiyun  *              be mapped and cannot be copied because it contains the actual
217*4882a593Smuzhiyun  *              memory location of the ACPI Global Lock.
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  ******************************************************************************/
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun acpi_status ACPI_INIT_FUNCTION
acpi_tb_parse_root_table(acpi_physical_address rsdp_address)222*4882a593Smuzhiyun acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	struct acpi_table_rsdp *rsdp;
225*4882a593Smuzhiyun 	u32 table_entry_size;
226*4882a593Smuzhiyun 	u32 i;
227*4882a593Smuzhiyun 	u32 table_count;
228*4882a593Smuzhiyun 	struct acpi_table_header *table;
229*4882a593Smuzhiyun 	acpi_physical_address address;
230*4882a593Smuzhiyun 	u32 length;
231*4882a593Smuzhiyun 	u8 *table_entry;
232*4882a593Smuzhiyun 	acpi_status status;
233*4882a593Smuzhiyun 	u32 table_index;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(tb_parse_root_table);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	/* Map the entire RSDP and extract the address of the RSDT or XSDT */
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
240*4882a593Smuzhiyun 	if (!rsdp) {
241*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_NO_MEMORY);
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	acpi_tb_print_table_header(rsdp_address,
245*4882a593Smuzhiyun 				   ACPI_CAST_PTR(struct acpi_table_header,
246*4882a593Smuzhiyun 						 rsdp));
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	/* Use XSDT if present and not overridden. Otherwise, use RSDT */
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	if ((rsdp->revision > 1) &&
251*4882a593Smuzhiyun 	    rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
252*4882a593Smuzhiyun 		/*
253*4882a593Smuzhiyun 		 * RSDP contains an XSDT (64-bit physical addresses). We must use
254*4882a593Smuzhiyun 		 * the XSDT if the revision is > 1 and the XSDT pointer is present,
255*4882a593Smuzhiyun 		 * as per the ACPI specification.
256*4882a593Smuzhiyun 		 */
257*4882a593Smuzhiyun 		address = (acpi_physical_address)rsdp->xsdt_physical_address;
258*4882a593Smuzhiyun 		table_entry_size = ACPI_XSDT_ENTRY_SIZE;
259*4882a593Smuzhiyun 	} else {
260*4882a593Smuzhiyun 		/* Root table is an RSDT (32-bit physical addresses) */
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 		address = (acpi_physical_address)rsdp->rsdt_physical_address;
263*4882a593Smuzhiyun 		table_entry_size = ACPI_RSDT_ENTRY_SIZE;
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	/*
267*4882a593Smuzhiyun 	 * It is not possible to map more than one entry in some environments,
268*4882a593Smuzhiyun 	 * so unmap the RSDP here before mapping other tables
269*4882a593Smuzhiyun 	 */
270*4882a593Smuzhiyun 	acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	/* Map the RSDT/XSDT table header to get the full table length */
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
275*4882a593Smuzhiyun 	if (!table) {
276*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_NO_MEMORY);
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	acpi_tb_print_table_header(address, table);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/*
282*4882a593Smuzhiyun 	 * Validate length of the table, and map entire table.
283*4882a593Smuzhiyun 	 * Minimum length table must contain at least one entry.
284*4882a593Smuzhiyun 	 */
285*4882a593Smuzhiyun 	length = table->length;
286*4882a593Smuzhiyun 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
289*4882a593Smuzhiyun 		ACPI_BIOS_ERROR((AE_INFO,
290*4882a593Smuzhiyun 				 "Invalid table length 0x%X in RSDT/XSDT",
291*4882a593Smuzhiyun 				 length));
292*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
293*4882a593Smuzhiyun 	}
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	table = acpi_os_map_memory(address, length);
296*4882a593Smuzhiyun 	if (!table) {
297*4882a593Smuzhiyun 		return_ACPI_STATUS(AE_NO_MEMORY);
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/* Validate the root table checksum */
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	status = acpi_tb_verify_checksum(table, length);
303*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
304*4882a593Smuzhiyun 		acpi_os_unmap_memory(table, length);
305*4882a593Smuzhiyun 		return_ACPI_STATUS(status);
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/* Get the number of entries and pointer to first entry */
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
311*4882a593Smuzhiyun 			    table_entry_size);
312*4882a593Smuzhiyun 	table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* Initialize the root table array from the RSDT/XSDT */
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	for (i = 0; i < table_count; i++) {
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 		/* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		address =
321*4882a593Smuzhiyun 		    acpi_tb_get_root_table_entry(table_entry, table_entry_size);
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 		/* Skip NULL entries in RSDT/XSDT */
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 		if (!address) {
326*4882a593Smuzhiyun 			goto next_table;
327*4882a593Smuzhiyun 		}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 		status = acpi_tb_install_standard_table(address,
330*4882a593Smuzhiyun 							ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
331*4882a593Smuzhiyun 							FALSE, TRUE,
332*4882a593Smuzhiyun 							&table_index);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		if (ACPI_SUCCESS(status) &&
335*4882a593Smuzhiyun 		    ACPI_COMPARE_NAMESEG(&acpi_gbl_root_table_list.
336*4882a593Smuzhiyun 					 tables[table_index].signature,
337*4882a593Smuzhiyun 					 ACPI_SIG_FADT)) {
338*4882a593Smuzhiyun 			acpi_gbl_fadt_index = table_index;
339*4882a593Smuzhiyun 			acpi_tb_parse_fadt();
340*4882a593Smuzhiyun 		}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun next_table:
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 		table_entry += table_entry_size;
345*4882a593Smuzhiyun 	}
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	acpi_os_unmap_memory(table, length);
348*4882a593Smuzhiyun 	return_ACPI_STATUS(AE_OK);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /*******************************************************************************
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * FUNCTION:    acpi_tb_get_table
354*4882a593Smuzhiyun  *
355*4882a593Smuzhiyun  * PARAMETERS:  table_desc          - Table descriptor
356*4882a593Smuzhiyun  *              out_table           - Where the pointer to the table is returned
357*4882a593Smuzhiyun  *
358*4882a593Smuzhiyun  * RETURN:      Status and pointer to the requested table
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * DESCRIPTION: Increase a reference to a table descriptor and return the
361*4882a593Smuzhiyun  *              validated table pointer.
362*4882a593Smuzhiyun  *              If the table descriptor is an entry of the root table list,
363*4882a593Smuzhiyun  *              this API must be invoked with ACPI_MTX_TABLES acquired.
364*4882a593Smuzhiyun  *
365*4882a593Smuzhiyun  ******************************************************************************/
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun acpi_status
acpi_tb_get_table(struct acpi_table_desc * table_desc,struct acpi_table_header ** out_table)368*4882a593Smuzhiyun acpi_tb_get_table(struct acpi_table_desc *table_desc,
369*4882a593Smuzhiyun 		  struct acpi_table_header **out_table)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	acpi_status status;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(acpi_tb_get_table);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	if (table_desc->validation_count == 0) {
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		/* Table need to be "VALIDATED" */
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		status = acpi_tb_validate_table(table_desc);
380*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
381*4882a593Smuzhiyun 			return_ACPI_STATUS(status);
382*4882a593Smuzhiyun 		}
383*4882a593Smuzhiyun 	}
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
386*4882a593Smuzhiyun 		table_desc->validation_count++;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 		/*
389*4882a593Smuzhiyun 		 * Detect validation_count overflows to ensure that the warning
390*4882a593Smuzhiyun 		 * message will only be printed once.
391*4882a593Smuzhiyun 		 */
392*4882a593Smuzhiyun 		if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
393*4882a593Smuzhiyun 			ACPI_WARNING((AE_INFO,
394*4882a593Smuzhiyun 				      "Table %p, Validation count overflows\n",
395*4882a593Smuzhiyun 				      table_desc));
396*4882a593Smuzhiyun 		}
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	*out_table = table_desc->pointer;
400*4882a593Smuzhiyun 	return_ACPI_STATUS(AE_OK);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun /*******************************************************************************
404*4882a593Smuzhiyun  *
405*4882a593Smuzhiyun  * FUNCTION:    acpi_tb_put_table
406*4882a593Smuzhiyun  *
407*4882a593Smuzhiyun  * PARAMETERS:  table_desc          - Table descriptor
408*4882a593Smuzhiyun  *
409*4882a593Smuzhiyun  * RETURN:      None
410*4882a593Smuzhiyun  *
411*4882a593Smuzhiyun  * DESCRIPTION: Decrease a reference to a table descriptor and release the
412*4882a593Smuzhiyun  *              validated table pointer if no references.
413*4882a593Smuzhiyun  *              If the table descriptor is an entry of the root table list,
414*4882a593Smuzhiyun  *              this API must be invoked with ACPI_MTX_TABLES acquired.
415*4882a593Smuzhiyun  *
416*4882a593Smuzhiyun  ******************************************************************************/
417*4882a593Smuzhiyun 
acpi_tb_put_table(struct acpi_table_desc * table_desc)418*4882a593Smuzhiyun void acpi_tb_put_table(struct acpi_table_desc *table_desc)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE(acpi_tb_put_table);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (table_desc->validation_count < ACPI_MAX_TABLE_VALIDATIONS) {
424*4882a593Smuzhiyun 		table_desc->validation_count--;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 		/*
427*4882a593Smuzhiyun 		 * Detect validation_count underflows to ensure that the warning
428*4882a593Smuzhiyun 		 * message will only be printed once.
429*4882a593Smuzhiyun 		 */
430*4882a593Smuzhiyun 		if (table_desc->validation_count >= ACPI_MAX_TABLE_VALIDATIONS) {
431*4882a593Smuzhiyun 			ACPI_WARNING((AE_INFO,
432*4882a593Smuzhiyun 				      "Table %p, Validation count underflows\n",
433*4882a593Smuzhiyun 				      table_desc));
434*4882a593Smuzhiyun 			return_VOID;
435*4882a593Smuzhiyun 		}
436*4882a593Smuzhiyun 	}
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	if (table_desc->validation_count == 0) {
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 		/* Table need to be "INVALIDATED" */
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 		acpi_tb_invalidate_table(table_desc);
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	return_VOID;
446*4882a593Smuzhiyun }
447