xref: /openbmc/linux/drivers/acpi/acpica/tbutils.c (revision 6c870213d6f3a25981c10728f46294a3bed1703f)
1 /******************************************************************************
2  *
3  * Module Name: tbutils - ACPI Table utilities
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "actables.h"
47 
48 #define _COMPONENT          ACPI_TABLES
49 ACPI_MODULE_NAME("tbutils")
50 
51 /* Local prototypes */
52 static acpi_status acpi_tb_validate_xsdt(acpi_physical_address address);
53 
54 static acpi_physical_address
55 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
56 
57 #if (!ACPI_REDUCED_HARDWARE)
58 /*******************************************************************************
59  *
60  * FUNCTION:    acpi_tb_initialize_facs
61  *
62  * PARAMETERS:  None
63  *
64  * RETURN:      Status
65  *
66  * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
67  *              for accessing the Global Lock and Firmware Waking Vector
68  *
69  ******************************************************************************/
70 
71 acpi_status acpi_tb_initialize_facs(void)
72 {
73 	acpi_status status;
74 
75 	/* If Hardware Reduced flag is set, there is no FACS */
76 
77 	if (acpi_gbl_reduced_hardware) {
78 		acpi_gbl_FACS = NULL;
79 		return (AE_OK);
80 	}
81 
82 	status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
83 					 ACPI_CAST_INDIRECT_PTR(struct
84 								acpi_table_header,
85 								&acpi_gbl_FACS));
86 	return (status);
87 }
88 #endif				/* !ACPI_REDUCED_HARDWARE */
89 
90 /*******************************************************************************
91  *
92  * FUNCTION:    acpi_tb_tables_loaded
93  *
94  * PARAMETERS:  None
95  *
96  * RETURN:      TRUE if required ACPI tables are loaded
97  *
98  * DESCRIPTION: Determine if the minimum required ACPI tables are present
99  *              (FADT, FACS, DSDT)
100  *
101  ******************************************************************************/
102 
103 u8 acpi_tb_tables_loaded(void)
104 {
105 
106 	if (acpi_gbl_root_table_list.current_table_count >= 3) {
107 		return (TRUE);
108 	}
109 
110 	return (FALSE);
111 }
112 
113 /*******************************************************************************
114  *
115  * FUNCTION:    acpi_tb_check_dsdt_header
116  *
117  * PARAMETERS:  None
118  *
119  * RETURN:      None
120  *
121  * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
122  *              if the DSDT has been replaced from outside the OS and/or if
123  *              the DSDT header has been corrupted.
124  *
125  ******************************************************************************/
126 
127 void acpi_tb_check_dsdt_header(void)
128 {
129 
130 	/* Compare original length and checksum to current values */
131 
132 	if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
133 	    acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
134 		ACPI_BIOS_ERROR((AE_INFO,
135 				 "The DSDT has been corrupted or replaced - "
136 				 "old, new headers below"));
137 		acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
138 		acpi_tb_print_table_header(0, acpi_gbl_DSDT);
139 
140 		ACPI_ERROR((AE_INFO,
141 			    "Please send DMI info to linux-acpi@vger.kernel.org\n"
142 			    "If system does not work as expected, please boot with acpi=copy_dsdt"));
143 
144 		/* Disable further error messages */
145 
146 		acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
147 		acpi_gbl_original_dsdt_header.checksum =
148 		    acpi_gbl_DSDT->checksum;
149 	}
150 }
151 
152 /*******************************************************************************
153  *
154  * FUNCTION:    acpi_tb_copy_dsdt
155  *
156  * PARAMETERS:  table_desc          - Installed table to copy
157  *
158  * RETURN:      None
159  *
160  * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
161  *              Some very bad BIOSs are known to either corrupt the DSDT or
162  *              install a new, bad DSDT. This copy works around the problem.
163  *
164  ******************************************************************************/
165 
166 struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
167 {
168 	struct acpi_table_header *new_table;
169 	struct acpi_table_desc *table_desc;
170 
171 	table_desc = &acpi_gbl_root_table_list.tables[table_index];
172 
173 	new_table = ACPI_ALLOCATE(table_desc->length);
174 	if (!new_table) {
175 		ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
176 			    table_desc->length));
177 		return (NULL);
178 	}
179 
180 	ACPI_MEMCPY(new_table, table_desc->pointer, table_desc->length);
181 	acpi_tb_uninstall_table(table_desc);
182 
183 	acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
184 				      tables[ACPI_TABLE_INDEX_DSDT],
185 				      ACPI_PTR_TO_PHYSADDR(new_table),
186 				      ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
187 				      new_table);
188 
189 	ACPI_INFO((AE_INFO,
190 		   "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
191 		   new_table->length));
192 
193 	return (new_table);
194 }
195 
196 /*******************************************************************************
197  *
198  * FUNCTION:    acpi_tb_get_root_table_entry
199  *
200  * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
201  *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
202  *
203  * RETURN:      Physical address extracted from the root table
204  *
205  * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
206  *              both 32-bit and 64-bit platforms
207  *
208  * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
209  *              64-bit platforms.
210  *
211  ******************************************************************************/
212 
213 static acpi_physical_address
214 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
215 {
216 	u64 address64;
217 
218 	/*
219 	 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
220 	 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
221 	 */
222 	if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
223 		/*
224 		 * 32-bit platform, RSDT: Return 32-bit table entry
225 		 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
226 		 */
227 		return ((acpi_physical_address)
228 			(*ACPI_CAST_PTR(u32, table_entry)));
229 	} else {
230 		/*
231 		 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
232 		 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
233 		 *  return 64-bit
234 		 */
235 		ACPI_MOVE_64_TO_64(&address64, table_entry);
236 
237 #if ACPI_MACHINE_WIDTH == 32
238 		if (address64 > ACPI_UINT32_MAX) {
239 
240 			/* Will truncate 64-bit address to 32 bits, issue warning */
241 
242 			ACPI_BIOS_WARNING((AE_INFO,
243 					   "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
244 					   " truncating",
245 					   ACPI_FORMAT_UINT64(address64)));
246 		}
247 #endif
248 		return ((acpi_physical_address) (address64));
249 	}
250 }
251 
252 /*******************************************************************************
253  *
254  * FUNCTION:    acpi_tb_validate_xsdt
255  *
256  * PARAMETERS:  address             - Physical address of the XSDT (from RSDP)
257  *
258  * RETURN:      Status. AE_OK if the table appears to be valid.
259  *
260  * DESCRIPTION: Validate an XSDT to ensure that it is of minimum size and does
261  *              not contain any NULL entries. A problem that is seen in the
262  *              field is that the XSDT exists, but is actually useless because
263  *              of one or more (or all) NULL entries.
264  *
265  ******************************************************************************/
266 
267 static acpi_status acpi_tb_validate_xsdt(acpi_physical_address xsdt_address)
268 {
269 	struct acpi_table_header *table;
270 	u8 *next_entry;
271 	acpi_physical_address address;
272 	u32 length;
273 	u32 entry_count;
274 	acpi_status status;
275 	u32 i;
276 
277 	/* Get the XSDT length */
278 
279 	table =
280 	    acpi_os_map_memory(xsdt_address, sizeof(struct acpi_table_header));
281 	if (!table) {
282 		return (AE_NO_MEMORY);
283 	}
284 
285 	length = table->length;
286 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
287 
288 	/*
289 	 * Minimum XSDT length is the size of the standard ACPI header
290 	 * plus one physical address entry
291 	 */
292 	if (length < (sizeof(struct acpi_table_header) + ACPI_XSDT_ENTRY_SIZE)) {
293 		return (AE_INVALID_TABLE_LENGTH);
294 	}
295 
296 	/* Map the entire XSDT */
297 
298 	table = acpi_os_map_memory(xsdt_address, length);
299 	if (!table) {
300 		return (AE_NO_MEMORY);
301 	}
302 
303 	/* Get the number of entries and pointer to first entry */
304 
305 	status = AE_OK;
306 	next_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
307 	entry_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
308 			    ACPI_XSDT_ENTRY_SIZE);
309 
310 	/* Validate each entry (physical address) within the XSDT */
311 
312 	for (i = 0; i < entry_count; i++) {
313 		address =
314 		    acpi_tb_get_root_table_entry(next_entry,
315 						 ACPI_XSDT_ENTRY_SIZE);
316 		if (!address) {
317 
318 			/* Detected a NULL entry, XSDT is invalid */
319 
320 			status = AE_NULL_ENTRY;
321 			break;
322 		}
323 
324 		next_entry += ACPI_XSDT_ENTRY_SIZE;
325 	}
326 
327 	/* Unmap table */
328 
329 	acpi_os_unmap_memory(table, length);
330 	return (status);
331 }
332 
333 /*******************************************************************************
334  *
335  * FUNCTION:    acpi_tb_parse_root_table
336  *
337  * PARAMETERS:  rsdp                    - Pointer to the RSDP
338  *
339  * RETURN:      Status
340  *
341  * DESCRIPTION: This function is called to parse the Root System Description
342  *              Table (RSDT or XSDT)
343  *
344  * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
345  *              be mapped and cannot be copied because it contains the actual
346  *              memory location of the ACPI Global Lock.
347  *
348  ******************************************************************************/
349 
350 acpi_status __init acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
351 {
352 	struct acpi_table_rsdp *rsdp;
353 	u32 table_entry_size;
354 	u32 i;
355 	u32 table_count;
356 	struct acpi_table_header *table;
357 	acpi_physical_address address;
358 	u32 length;
359 	u8 *table_entry;
360 	acpi_status status;
361 	u32 table_index;
362 
363 	ACPI_FUNCTION_TRACE(tb_parse_root_table);
364 
365 	/* Map the entire RSDP and extract the address of the RSDT or XSDT */
366 
367 	rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
368 	if (!rsdp) {
369 		return_ACPI_STATUS(AE_NO_MEMORY);
370 	}
371 
372 	acpi_tb_print_table_header(rsdp_address,
373 				   ACPI_CAST_PTR(struct acpi_table_header,
374 						 rsdp));
375 
376 	/* Use XSDT if present and not overridden. Otherwise, use RSDT */
377 
378 	if ((rsdp->revision > 1) &&
379 	    rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
380 		/*
381 		 * RSDP contains an XSDT (64-bit physical addresses). We must use
382 		 * the XSDT if the revision is > 1 and the XSDT pointer is present,
383 		 * as per the ACPI specification.
384 		 */
385 		address = (acpi_physical_address) rsdp->xsdt_physical_address;
386 		table_entry_size = ACPI_XSDT_ENTRY_SIZE;
387 	} else {
388 		/* Root table is an RSDT (32-bit physical addresses) */
389 
390 		address = (acpi_physical_address) rsdp->rsdt_physical_address;
391 		table_entry_size = ACPI_RSDT_ENTRY_SIZE;
392 	}
393 
394 	/*
395 	 * It is not possible to map more than one entry in some environments,
396 	 * so unmap the RSDP here before mapping other tables
397 	 */
398 	acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
399 
400 	/*
401 	 * If it is present and used, validate the XSDT for access/size
402 	 * and ensure that all table entries are at least non-NULL
403 	 */
404 	if (table_entry_size == ACPI_XSDT_ENTRY_SIZE) {
405 		status = acpi_tb_validate_xsdt(address);
406 		if (ACPI_FAILURE(status)) {
407 			ACPI_BIOS_WARNING((AE_INFO,
408 					   "XSDT is invalid (%s), using RSDT",
409 					   acpi_format_exception(status)));
410 
411 			/* Fall back to the RSDT */
412 
413 			address =
414 			    (acpi_physical_address) rsdp->rsdt_physical_address;
415 			table_entry_size = ACPI_RSDT_ENTRY_SIZE;
416 		}
417 	}
418 
419 	/* Map the RSDT/XSDT table header to get the full table length */
420 
421 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
422 	if (!table) {
423 		return_ACPI_STATUS(AE_NO_MEMORY);
424 	}
425 
426 	acpi_tb_print_table_header(address, table);
427 
428 	/*
429 	 * Validate length of the table, and map entire table.
430 	 * Minimum length table must contain at least one entry.
431 	 */
432 	length = table->length;
433 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
434 
435 	if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
436 		ACPI_BIOS_ERROR((AE_INFO,
437 				 "Invalid table length 0x%X in RSDT/XSDT",
438 				 length));
439 		return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
440 	}
441 
442 	table = acpi_os_map_memory(address, length);
443 	if (!table) {
444 		return_ACPI_STATUS(AE_NO_MEMORY);
445 	}
446 
447 	/* Validate the root table checksum */
448 
449 	status = acpi_tb_verify_checksum(table, length);
450 	if (ACPI_FAILURE(status)) {
451 		acpi_os_unmap_memory(table, length);
452 		return_ACPI_STATUS(status);
453 	}
454 
455 	/* Get the number of entries and pointer to first entry */
456 
457 	table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
458 			    table_entry_size);
459 	table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
460 
461 	/*
462 	 * First two entries in the table array are reserved for the DSDT
463 	 * and FACS, which are not actually present in the RSDT/XSDT - they
464 	 * come from the FADT
465 	 */
466 	acpi_gbl_root_table_list.current_table_count = 2;
467 
468 	/* Initialize the root table array from the RSDT/XSDT */
469 
470 	for (i = 0; i < table_count; i++) {
471 
472 		/* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
473 
474 		status =
475 		    acpi_tb_install_standard_table(acpi_tb_get_root_table_entry
476 						   (table_entry,
477 						    table_entry_size),
478 						   ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
479 						   FALSE, TRUE, &table_index);
480 
481 		if (ACPI_SUCCESS(status) &&
482 		    ACPI_COMPARE_NAME(&acpi_gbl_root_table_list.
483 				      tables[table_index].signature,
484 				      ACPI_SIG_FADT)) {
485 			acpi_tb_parse_fadt(table_index);
486 		}
487 
488 		table_entry += table_entry_size;
489 	}
490 
491 	/*
492 	 * It is not possible to map more than one entry in some environments,
493 	 * so unmap the root table here before mapping other tables
494 	 */
495 	acpi_os_unmap_memory(table, length);
496 
497 	return_ACPI_STATUS(AE_OK);
498 }
499