1 /******************************************************************************
2  *
3  * Module Name: tbutils   - table utilities
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2008, 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 <xen/init.h>
45 #include <acpi/acpi.h>
46 #include <acpi/actables.h>
47 
48 #define _COMPONENT          ACPI_TABLES
49 ACPI_MODULE_NAME("tbutils")
50 
51 /*******************************************************************************
52  *
53  * FUNCTION:    acpi_tb_check_xsdt
54  *
55  * PARAMETERS:  address                    - Pointer to the XSDT
56  *
57  * RETURN:      status
58  *		AE_OK - XSDT is okay
59  *		AE_NO_MEMORY - can't map XSDT
60  *		AE_INVALID_TABLE_LENGTH - invalid table length
61  *		AE_NULL_ENTRY - XSDT has NULL entry
62  *
63  * DESCRIPTION: validate XSDT
64 ******************************************************************************/
65 
66 static acpi_status __init
acpi_tb_check_xsdt(acpi_physical_address address)67 acpi_tb_check_xsdt(acpi_physical_address address)
68 {
69 	struct acpi_table_header *table;
70 	u32 length;
71 	u64 xsdt_entry_address;
72 	u8 *table_entry;
73 	u32 table_count;
74 	int i;
75 
76 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
77 	if (!table)
78 		return AE_NO_MEMORY;
79 
80 	length = table->length;
81 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
82 	if (length < sizeof(struct acpi_table_header))
83 		return AE_INVALID_TABLE_LENGTH;
84 
85 	table = acpi_os_map_memory(address, length);
86 	if (!table)
87 		return AE_NO_MEMORY;
88 
89 	/* Calculate the number of tables described in XSDT */
90 	table_count =
91 		(u32) ((table->length -
92 		sizeof(struct acpi_table_header)) / sizeof(u64));
93 	table_entry =
94 		ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
95 	for (i = 0; i < table_count; i++) {
96 		ACPI_MOVE_64_TO_64(&xsdt_entry_address, table_entry);
97 		if (!xsdt_entry_address) {
98 			/* XSDT has NULL entry */
99 			break;
100 		}
101 		table_entry += sizeof(u64);
102 	}
103 	acpi_os_unmap_memory(table, length);
104 
105 	if (i < table_count)
106 		return AE_NULL_ENTRY;
107 	else
108 		return AE_OK;
109 }
110 
111 /*******************************************************************************
112  *
113  * FUNCTION:    acpi_tb_print_table_header
114  *
115  * PARAMETERS:  Address             - Table physical address
116  *              Header              - Table header
117  *
118  * RETURN:      None
119  *
120  * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
121  *
122  ******************************************************************************/
123 
124 void __init
acpi_tb_print_table_header(acpi_physical_address address,struct acpi_table_header * header)125 acpi_tb_print_table_header(acpi_physical_address address,
126 			   struct acpi_table_header *header)
127 {
128 
129 	if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
130 
131 		/* FACS only has signature and length fields of common table header */
132 
133 		ACPI_INFO((AE_INFO, "%4.4s %08lX, %04X",
134 			   header->signature, (unsigned long)address,
135 			   header->length));
136 	} else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
137 
138 		/* RSDP has no common fields */
139 
140 		ACPI_INFO((AE_INFO, "RSDP %08lX, %04X (r%d %6.6s)",
141 			   (unsigned long)address,
142 			   (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
143 			    revision >
144 			    0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
145 					       header)->length : 20,
146 			   ACPI_CAST_PTR(struct acpi_table_rsdp,
147 					 header)->revision,
148 			   ACPI_CAST_PTR(struct acpi_table_rsdp,
149 					 header)->oem_id));
150 	} else {
151 		/* Standard ACPI table with full common header */
152 
153 		ACPI_INFO((AE_INFO,
154 			   "%4.4s %08lX, %04X (r%d %6.6s %8.8s %8X %4.4s %8X)",
155 			   header->signature, (unsigned long)address,
156 			   header->length, header->revision, header->oem_id,
157 			   header->oem_table_id, header->oem_revision,
158 			   header->asl_compiler_id,
159 			   header->asl_compiler_revision));
160 	}
161 }
162 
163 /*******************************************************************************
164  *
165  * FUNCTION:    acpi_tb_validate_checksum
166  *
167  * PARAMETERS:  Table               - ACPI table to verify
168  *              Length              - Length of entire table
169  *
170  * RETURN:      Status
171  *
172  * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
173  *              exception on bad checksum.
174  *
175  ******************************************************************************/
176 
177 acpi_status __init
acpi_tb_verify_checksum(struct acpi_table_header * table,u32 length)178 acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
179 {
180 	u8 checksum;
181 
182 	/* Compute the checksum on the table */
183 
184 	checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
185 
186 	/* Checksum ok? (should be zero) */
187 
188 	if (checksum) {
189 		ACPI_WARNING((AE_INFO,
190 			      "Incorrect checksum in table [%4.4s] - %2.2X, should be %2.2X",
191 			      table->signature, table->checksum,
192 			      (u8) (table->checksum - checksum)));
193 
194 #if (ACPI_CHECKSUM_ABORT)
195 
196 		return (AE_BAD_CHECKSUM);
197 #endif
198 	}
199 
200 	return (AE_OK);
201 }
202 
203 /*******************************************************************************
204  *
205  * FUNCTION:    acpi_tb_checksum
206  *
207  * PARAMETERS:  Buffer          - Pointer to memory region to be checked
208  *              Length          - Length of this memory region
209  *
210  * RETURN:      Checksum (u8)
211  *
212  * DESCRIPTION: Calculates circular checksum of memory region.
213  *
214  ******************************************************************************/
215 
acpi_tb_checksum(u8 * buffer,acpi_native_uint length)216 u8 acpi_tb_checksum(u8 * buffer, acpi_native_uint length)
217 {
218 	u8 sum = 0;
219 	u8 *end = buffer + length;
220 
221 	while (buffer < end) {
222 		sum = (u8) (sum + *(buffer++));
223 	}
224 
225 	return sum;
226 }
227 
228 /*******************************************************************************
229  *
230  * FUNCTION:    acpi_tb_install_table
231  *
232  * PARAMETERS:  Address                 - Physical address of DSDT or FACS
233  *              Flags                   - Flags
234  *              Signature               - Table signature, NULL if no need to
235  *                                        match
236  *              table_index             - Index into root table array
237  *
238  * RETURN:      None
239  *
240  * DESCRIPTION: Install an ACPI table into the global data structure.
241  *
242  ******************************************************************************/
243 
244 void __init
acpi_tb_install_table(acpi_physical_address address,u8 flags,char * signature,acpi_native_uint table_index)245 acpi_tb_install_table(acpi_physical_address address,
246 		      u8 flags, char *signature, acpi_native_uint table_index)
247 {
248 	struct acpi_table_header *table;
249 
250 	if (!address) {
251 		ACPI_ERROR((AE_INFO,
252 			    "Null physical address for ACPI table [%s]",
253 			    signature));
254 		return;
255 	}
256 
257 	/* Map just the table header */
258 
259 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
260 	if (!table) {
261 		return;
262 	}
263 
264 	/* If a particular signature is expected, signature must match */
265 
266 	if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
267 		ACPI_ERROR((AE_INFO,
268 			    "Invalid signature 0x%X for ACPI table [%s]",
269 			    *ACPI_CAST_PTR(u32, table->signature), signature));
270 		goto unmap_and_exit;
271 	}
272 
273 	/* Initialize the table entry */
274 
275 	acpi_gbl_root_table_list.tables[table_index].address = address;
276 	acpi_gbl_root_table_list.tables[table_index].length = table->length;
277 	acpi_gbl_root_table_list.tables[table_index].flags = flags;
278 
279 	ACPI_MOVE_32_TO_32(&
280 			   (acpi_gbl_root_table_list.tables[table_index].
281 			    signature), table->signature);
282 
283 	acpi_tb_print_table_header(address, table);
284 
285       unmap_and_exit:
286 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
287 }
288 
289 /*******************************************************************************
290  *
291  * FUNCTION:    acpi_tb_get_root_table_entry
292  *
293  * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
294  *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
295  *
296  * RETURN:      Physical address extracted from the root table
297  *
298  * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
299  *              both 32-bit and 64-bit platforms
300  *
301  * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
302  *              64-bit platforms.
303  *
304  ******************************************************************************/
305 
306 static acpi_physical_address __init
acpi_tb_get_root_table_entry(u8 * table_entry,acpi_native_uint table_entry_size)307 acpi_tb_get_root_table_entry(u8 * table_entry,
308 			     acpi_native_uint table_entry_size)
309 {
310 	u64 address64;
311 
312 	/*
313 	 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
314 	 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
315 	 */
316 	if (table_entry_size == sizeof(u32)) {
317 		/*
318 		 * 32-bit platform, RSDT: Return 32-bit table entry
319 		 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
320 		 */
321 		return ((acpi_physical_address)
322 			(*ACPI_CAST_PTR(u32, table_entry)));
323 	} else {
324 		/*
325 		 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
326 		 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local, return 64-bit
327 		 */
328 		ACPI_MOVE_64_TO_64(&address64, table_entry);
329 
330 #if ACPI_MACHINE_WIDTH == 32
331 		if (address64 > ACPI_UINT32_MAX) {
332 
333 			/* Will truncate 64-bit address to 32 bits, issue warning */
334 
335 			ACPI_WARNING((AE_INFO,
336 				      "64-bit Physical Address in XSDT is too large (%8.8X%8.8X), truncating",
337 				      ACPI_FORMAT_UINT64(address64)));
338 		}
339 #endif
340 		return ((acpi_physical_address) (address64));
341 	}
342 }
343 
344 /*******************************************************************************
345  *
346  * FUNCTION:    acpi_tb_parse_root_table
347  *
348  * PARAMETERS:  Rsdp                    - Pointer to the RSDP
349  *              Flags                   - Flags
350  *
351  * RETURN:      Status
352  *
353  * DESCRIPTION: This function is called to parse the Root System Description
354  *              Table (RSDT or XSDT)
355  *
356  * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
357  *              be mapped and cannot be copied because it contains the actual
358  *              memory location of the ACPI Global Lock.
359  *
360  ******************************************************************************/
361 
362 acpi_status __init
acpi_tb_parse_root_table(acpi_physical_address rsdp_address,u8 flags)363 acpi_tb_parse_root_table(acpi_physical_address rsdp_address, u8 flags)
364 {
365 	struct acpi_table_rsdp *rsdp;
366 	acpi_native_uint table_entry_size;
367 	acpi_native_uint i;
368 	u32 table_count;
369 	struct acpi_table_header *table;
370 	acpi_physical_address address;
371 	acpi_physical_address rsdt_address = 0;
372 	u32 length;
373 	u8 *table_entry;
374 	acpi_status status;
375 
376 	ACPI_FUNCTION_TRACE(tb_parse_root_table);
377 
378 	/*
379 	 * Map the entire RSDP and extract the address of the RSDT or XSDT
380 	 */
381 	rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
382 	if (!rsdp) {
383 		return_ACPI_STATUS(AE_NO_MEMORY);
384 	}
385 
386 	acpi_tb_print_table_header(rsdp_address,
387 				   ACPI_CAST_PTR(struct acpi_table_header,
388 						 rsdp));
389 
390 	/* Differentiate between RSDT and XSDT root tables */
391 
392 	if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
393 		/*
394 		 * Root table is an XSDT (64-bit physical addresses). We must use the
395 		 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
396 		 * the ACPI specification.
397 		 */
398 		address = (acpi_physical_address) rsdp->xsdt_physical_address;
399 		table_entry_size = sizeof(u64);
400 		rsdt_address = (acpi_physical_address)
401 					rsdp->rsdt_physical_address;
402 	} else {
403 		/* Root table is an RSDT (32-bit physical addresses) */
404 
405 		address = (acpi_physical_address) rsdp->rsdt_physical_address;
406 		table_entry_size = sizeof(u32);
407 	}
408 
409 	/*
410 	 * It is not possible to map more than one entry in some environments,
411 	 * so unmap the RSDP here before mapping other tables
412 	 */
413 	acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
414 
415 	if (table_entry_size == sizeof(u64)) {
416 		if (acpi_tb_check_xsdt(address) == AE_NULL_ENTRY) {
417 			/* XSDT has NULL entry, RSDT is used */
418 			address = rsdt_address;
419 			table_entry_size = sizeof(u32);
420 			ACPI_WARNING((AE_INFO, "BIOS XSDT has NULL entry, "
421 					"using RSDT"));
422 		}
423 	}
424 	/* Map the RSDT/XSDT table header to get the full table length */
425 
426 	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
427 	if (!table) {
428 		return_ACPI_STATUS(AE_NO_MEMORY);
429 	}
430 
431 	acpi_tb_print_table_header(address, table);
432 
433 	/* Get the length of the full table, verify length and map entire table */
434 
435 	length = table->length;
436 	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
437 
438 	if (length < sizeof(struct acpi_table_header)) {
439 		ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
440 			    length));
441 		return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
442 	}
443 
444 	table = acpi_os_map_memory(address, length);
445 	if (!table) {
446 		return_ACPI_STATUS(AE_NO_MEMORY);
447 	}
448 
449 	/* Validate the root table checksum */
450 
451 	status = acpi_tb_verify_checksum(table, length);
452 	if (ACPI_FAILURE(status)) {
453 		acpi_os_unmap_memory(table, length);
454 		return_ACPI_STATUS(status);
455 	}
456 
457 	/* Calculate the number of tables described in the root table */
458 
459 	table_count =
460 	    (u32) ((table->length -
461 		    sizeof(struct acpi_table_header)) / table_entry_size);
462 
463 	/*
464 	 * First two entries in the table array are reserved for the DSDT and FACS,
465 	 * which are not actually present in the RSDT/XSDT - they come from the FADT
466 	 */
467 	table_entry =
468 	    ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
469 	acpi_gbl_root_table_list.count = 2;
470 
471 	/*
472 	 * Initialize the root table array from the RSDT/XSDT
473 	 */
474 	for (i = 0; i < table_count; i++) {
475 		if (acpi_gbl_root_table_list.count >=
476 		    acpi_gbl_root_table_list.size) {
477 
478 			/* There is no more room in the root table array, attempt resize */
479 
480 			status = acpi_tb_resize_root_table_list();
481 			if (ACPI_FAILURE(status)) {
482 				ACPI_WARNING((AE_INFO,
483 					      "Truncating %u table entries!",
484 					      (unsigned)
485 					      (acpi_gbl_root_table_list.size -
486 					       acpi_gbl_root_table_list.
487 					       count)));
488 				break;
489 			}
490 		}
491 
492 		/* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
493 
494 		acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.count].
495 		    address =
496 		    acpi_tb_get_root_table_entry(table_entry, table_entry_size);
497 
498 		table_entry += table_entry_size;
499 		acpi_gbl_root_table_list.count++;
500 	}
501 
502 	/*
503 	 * It is not possible to map more than one entry in some environments,
504 	 * so unmap the root table here before mapping other tables
505 	 */
506 	acpi_os_unmap_memory(table, length);
507 
508 	/*
509 	 * Complete the initialization of the root table array by examining
510 	 * the header of each table
511 	 */
512 	for (i = 2; i < acpi_gbl_root_table_list.count; i++) {
513 		acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
514 				      address, flags, NULL, i);
515 
516 		/* Special case for FADT - get the DSDT and FACS */
517 
518 		if (ACPI_COMPARE_NAME
519 		    (&acpi_gbl_root_table_list.tables[i].signature,
520 		     ACPI_SIG_FADT)) {
521 			acpi_tb_parse_fadt(i, flags);
522 		}
523 	}
524 
525 	return_ACPI_STATUS(AE_OK);
526 }
527