Linux Audio

Check our new training course

Loading...
v4.10.11
  1/******************************************************************************
  2 *
  3 * Module Name: tbutils - ACPI Table utilities
  4 *
  5 *****************************************************************************/
  6
  7/*
  8 * Copyright (C) 2000 - 2016, 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
 49ACPI_MODULE_NAME("tbutils")
 50
 51/* Local prototypes */
 
 
 
 
 
 
 52static acpi_physical_address
 53acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
 54
 55#if (!ACPI_REDUCED_HARDWARE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56/*******************************************************************************
 57 *
 58 * FUNCTION:    acpi_tb_initialize_facs
 59 *
 60 * PARAMETERS:  None
 61 *
 62 * RETURN:      Status
 63 *
 64 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
 65 *              for accessing the Global Lock and Firmware Waking Vector
 66 *
 67 ******************************************************************************/
 68
 69acpi_status acpi_tb_initialize_facs(void)
 70{
 71	struct acpi_table_facs *facs;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 72
 73	/* If Hardware Reduced flag is set, there is no FACS */
 
 
 
 
 
 
 
 
 
 74
 75	if (acpi_gbl_reduced_hardware) {
 76		acpi_gbl_FACS = NULL;
 77		return (AE_OK);
 78	} else if (acpi_gbl_FADT.Xfacs &&
 79		   (!acpi_gbl_FADT.facs
 80		    || !acpi_gbl_use32_bit_facs_addresses)) {
 81		(void)acpi_get_table_by_index(acpi_gbl_xfacs_index,
 82					      ACPI_CAST_INDIRECT_PTR(struct
 83								     acpi_table_header,
 84								     &facs));
 85		acpi_gbl_FACS = facs;
 86	} else if (acpi_gbl_FADT.facs) {
 87		(void)acpi_get_table_by_index(acpi_gbl_facs_index,
 88					      ACPI_CAST_INDIRECT_PTR(struct
 89								     acpi_table_header,
 90								     &facs));
 91		acpi_gbl_FACS = facs;
 92	}
 
 93
 94	/* If there is no FACS, just continue. There was already an error msg */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 95
 96	return (AE_OK);
 97}
 98#endif				/* !ACPI_REDUCED_HARDWARE */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 99
100/*******************************************************************************
101 *
102 * FUNCTION:    acpi_tb_check_dsdt_header
103 *
104 * PARAMETERS:  None
105 *
106 * RETURN:      None
107 *
108 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
109 *              if the DSDT has been replaced from outside the OS and/or if
110 *              the DSDT header has been corrupted.
111 *
112 ******************************************************************************/
113
114void acpi_tb_check_dsdt_header(void)
115{
116
117	/* Compare original length and checksum to current values */
118
119	if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
120	    acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
121		ACPI_BIOS_ERROR((AE_INFO,
122				 "The DSDT has been corrupted or replaced - "
123				 "old, new headers below"));
124
125		acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
126		acpi_tb_print_table_header(0, acpi_gbl_DSDT);
127
128		ACPI_ERROR((AE_INFO,
129			    "Please send DMI info to linux-acpi@vger.kernel.org\n"
130			    "If system does not work as expected, please boot with acpi=copy_dsdt"));
131
132		/* Disable further error messages */
133
134		acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
135		acpi_gbl_original_dsdt_header.checksum =
136		    acpi_gbl_DSDT->checksum;
137	}
138}
139
140/*******************************************************************************
141 *
142 * FUNCTION:    acpi_tb_copy_dsdt
143 *
144 * PARAMETERS:  table_desc          - Installed table to copy
145 *
146 * RETURN:      None
147 *
148 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
149 *              Some very bad BIOSs are known to either corrupt the DSDT or
150 *              install a new, bad DSDT. This copy works around the problem.
151 *
152 ******************************************************************************/
153
154struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
155{
156	struct acpi_table_header *new_table;
157	struct acpi_table_desc *table_desc;
158
159	table_desc = &acpi_gbl_root_table_list.tables[table_index];
160
161	new_table = ACPI_ALLOCATE(table_desc->length);
162	if (!new_table) {
163		ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
164			    table_desc->length));
165		return (NULL);
166	}
167
168	memcpy(new_table, table_desc->pointer, table_desc->length);
169	acpi_tb_uninstall_table(table_desc);
 
 
 
 
 
 
170
171	acpi_tb_init_table_descriptor(&acpi_gbl_root_table_list.
172				      tables[acpi_gbl_dsdt_index],
173				      ACPI_PTR_TO_PHYSADDR(new_table),
174				      ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
175				      new_table);
176
177	ACPI_INFO(("Forced DSDT copy: length 0x%05X copied locally, original unmapped", new_table->length));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
179	return (new_table);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180}
181
182/*******************************************************************************
183 *
184 * FUNCTION:    acpi_tb_get_root_table_entry
185 *
186 * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
187 *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
188 *
189 * RETURN:      Physical address extracted from the root table
190 *
191 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
192 *              both 32-bit and 64-bit platforms
193 *
194 * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
195 *              64-bit platforms.
196 *
197 ******************************************************************************/
198
199static acpi_physical_address
200acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
201{
202	u64 address64;
203
204	/*
205	 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
206	 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
207	 */
208	if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
209		/*
210		 * 32-bit platform, RSDT: Return 32-bit table entry
211		 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
212		 */
213		return ((acpi_physical_address)
214			(*ACPI_CAST_PTR(u32, table_entry)));
215	} else {
216		/*
217		 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
218		 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
219		 *  return 64-bit
220		 */
221		ACPI_MOVE_64_TO_64(&address64, table_entry);
222
223#if ACPI_MACHINE_WIDTH == 32
224		if (address64 > ACPI_UINT32_MAX) {
225
226			/* Will truncate 64-bit address to 32 bits, issue warning */
227
228			ACPI_BIOS_WARNING((AE_INFO,
229					   "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
230					   " truncating",
231					   ACPI_FORMAT_UINT64(address64)));
232		}
233#endif
234		return ((acpi_physical_address)(address64));
235	}
236}
237
238/*******************************************************************************
239 *
240 * FUNCTION:    acpi_tb_parse_root_table
241 *
242 * PARAMETERS:  rsdp                    - Pointer to the RSDP
243 *
244 * RETURN:      Status
245 *
246 * DESCRIPTION: This function is called to parse the Root System Description
247 *              Table (RSDT or XSDT)
248 *
249 * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
250 *              be mapped and cannot be copied because it contains the actual
251 *              memory location of the ACPI Global Lock.
252 *
253 ******************************************************************************/
254
255acpi_status ACPI_INIT_FUNCTION
256acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
257{
258	struct acpi_table_rsdp *rsdp;
259	u32 table_entry_size;
260	u32 i;
261	u32 table_count;
262	struct acpi_table_header *table;
263	acpi_physical_address address;
 
264	u32 length;
265	u8 *table_entry;
266	acpi_status status;
267	u32 table_index;
268
269	ACPI_FUNCTION_TRACE(tb_parse_root_table);
270
271	/* Map the entire RSDP and extract the address of the RSDT or XSDT */
272
 
273	rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
274	if (!rsdp) {
275		return_ACPI_STATUS(AE_NO_MEMORY);
276	}
277
278	acpi_tb_print_table_header(rsdp_address,
279				   ACPI_CAST_PTR(struct acpi_table_header,
280						 rsdp));
281
282	/* Use XSDT if present and not overridden. Otherwise, use RSDT */
283
284	if ((rsdp->revision > 1) &&
285	    rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
286		/*
287		 * RSDP contains an XSDT (64-bit physical addresses). We must use
288		 * the XSDT if the revision is > 1 and the XSDT pointer is present,
289		 * as per the ACPI specification.
290		 */
291		address = (acpi_physical_address)rsdp->xsdt_physical_address;
292		table_entry_size = ACPI_XSDT_ENTRY_SIZE;
 
 
293	} else {
294		/* Root table is an RSDT (32-bit physical addresses) */
295
296		address = (acpi_physical_address)rsdp->rsdt_physical_address;
297		table_entry_size = ACPI_RSDT_ENTRY_SIZE;
298	}
299
300	/*
301	 * It is not possible to map more than one entry in some environments,
302	 * so unmap the RSDP here before mapping other tables
303	 */
304	acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
305
 
 
 
 
 
 
 
 
 
306	/* Map the RSDT/XSDT table header to get the full table length */
307
308	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
309	if (!table) {
310		return_ACPI_STATUS(AE_NO_MEMORY);
311	}
312
313	acpi_tb_print_table_header(address, table);
314
315	/*
316	 * Validate length of the table, and map entire table.
317	 * Minimum length table must contain at least one entry.
318	 */
319	length = table->length;
320	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
321
322	if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
323		ACPI_BIOS_ERROR((AE_INFO,
324				 "Invalid table length 0x%X in RSDT/XSDT",
325				 length));
326		return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
327	}
328
329	table = acpi_os_map_memory(address, length);
330	if (!table) {
331		return_ACPI_STATUS(AE_NO_MEMORY);
332	}
333
334	/* Validate the root table checksum */
335
336	status = acpi_tb_verify_checksum(table, length);
337	if (ACPI_FAILURE(status)) {
338		acpi_os_unmap_memory(table, length);
339		return_ACPI_STATUS(status);
340	}
341
342	/* Get the number of entries and pointer to first entry */
343
344	table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
345			    table_entry_size);
346	table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
347
348	/* Initialize the root table array from the RSDT/XSDT */
 
 
 
 
 
349
 
 
 
350	for (i = 0; i < table_count; i++) {
 
 
351
352		/* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
353
354		address =
355		    acpi_tb_get_root_table_entry(table_entry, table_entry_size);
356
357		/* Skip NULL entries in RSDT/XSDT */
358
359		if (!address) {
360			goto next_table;
 
 
 
 
 
 
361		}
362
363		status = acpi_tb_install_standard_table(address,
364							ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
365							FALSE, TRUE,
366							&table_index);
367
368		if (ACPI_SUCCESS(status) &&
369		    ACPI_COMPARE_NAME(&acpi_gbl_root_table_list.
370				      tables[table_index].signature,
371				      ACPI_SIG_FADT)) {
372			acpi_gbl_fadt_index = table_index;
373			acpi_tb_parse_fadt();
374		}
375
376next_table:
 
 
377
378		table_entry += table_entry_size;
 
379	}
380
 
 
 
 
381	acpi_os_unmap_memory(table, length);
382	return_ACPI_STATUS(AE_OK);
383}
384
385/*******************************************************************************
386 *
387 * FUNCTION:    acpi_tb_get_table
388 *
389 * PARAMETERS:  table_desc          - Table descriptor
390 *              out_table           - Where the pointer to the table is returned
391 *
392 * RETURN:      Status and pointer to the requested table
393 *
394 * DESCRIPTION: Increase a reference to a table descriptor and return the
395 *              validated table pointer.
396 *              If the table descriptor is an entry of the root table list,
397 *              this API must be invoked with ACPI_MTX_TABLES acquired.
398 *
399 ******************************************************************************/
400
401acpi_status
402acpi_tb_get_table(struct acpi_table_desc *table_desc,
403		  struct acpi_table_header **out_table)
404{
405	acpi_status status;
406
407	ACPI_FUNCTION_TRACE(acpi_tb_get_table);
408
409	if (table_desc->validation_count == 0) {
410
411		/* Table need to be "VALIDATED" */
412
413		status = acpi_tb_validate_table(table_desc);
414		if (ACPI_FAILURE(status)) {
415			return_ACPI_STATUS(status);
 
 
 
 
 
 
 
416		}
417	}
418
419	table_desc->validation_count++;
420	if (table_desc->validation_count == 0) {
421		ACPI_ERROR((AE_INFO,
422			    "Table %p, Validation count is zero after increment\n",
423			    table_desc));
424		table_desc->validation_count--;
425		return_ACPI_STATUS(AE_LIMIT);
426	}
427
428	*out_table = table_desc->pointer;
429	return_ACPI_STATUS(AE_OK);
430}
431
432/*******************************************************************************
433 *
434 * FUNCTION:    acpi_tb_put_table
435 *
436 * PARAMETERS:  table_desc          - Table descriptor
437 *
438 * RETURN:      None
439 *
440 * DESCRIPTION: Decrease a reference to a table descriptor and release the
441 *              validated table pointer if no references.
442 *              If the table descriptor is an entry of the root table list,
443 *              this API must be invoked with ACPI_MTX_TABLES acquired.
444 *
445 ******************************************************************************/
446
447void acpi_tb_put_table(struct acpi_table_desc *table_desc)
448{
449
450	ACPI_FUNCTION_TRACE(acpi_tb_put_table);
451
452	if (table_desc->validation_count == 0) {
453		ACPI_WARNING((AE_INFO,
454			      "Table %p, Validation count is zero before decrement\n",
455			      table_desc));
456		return_VOID;
457	}
458	table_desc->validation_count--;
459
460	if (table_desc->validation_count == 0) {
461
462		/* Table need to be "INVALIDATED" */
463
464		acpi_tb_invalidate_table(table_desc);
465	}
466
467	return_VOID;
468}
v3.1
  1/******************************************************************************
  2 *
  3 * Module Name: tbutils   - table utilities
  4 *
  5 *****************************************************************************/
  6
  7/*
  8 * Copyright (C) 2000 - 2011, 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
 49ACPI_MODULE_NAME("tbutils")
 50
 51/* Local prototypes */
 52static void acpi_tb_fix_string(char *string, acpi_size length);
 53
 54static void
 55acpi_tb_cleanup_table_header(struct acpi_table_header *out_header,
 56			     struct acpi_table_header *header);
 57
 58static acpi_physical_address
 59acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size);
 60
 61/*******************************************************************************
 62 *
 63 * FUNCTION:    acpi_tb_check_xsdt
 64 *
 65 * PARAMETERS:  address                    - Pointer to the XSDT
 66 *
 67 * RETURN:      status
 68 *		AE_OK - XSDT is okay
 69 *		AE_NO_MEMORY - can't map XSDT
 70 *		AE_INVALID_TABLE_LENGTH - invalid table length
 71 *		AE_NULL_ENTRY - XSDT has NULL entry
 72 *
 73 * DESCRIPTION: validate XSDT
 74******************************************************************************/
 75
 76static acpi_status
 77acpi_tb_check_xsdt(acpi_physical_address address)
 78{
 79	struct acpi_table_header *table;
 80	u32 length;
 81	u64 xsdt_entry_address;
 82	u8 *table_entry;
 83	u32 table_count;
 84	int i;
 85
 86	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
 87	if (!table)
 88		return AE_NO_MEMORY;
 89
 90	length = table->length;
 91	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
 92	if (length < sizeof(struct acpi_table_header))
 93		return AE_INVALID_TABLE_LENGTH;
 94
 95	table = acpi_os_map_memory(address, length);
 96	if (!table)
 97		return AE_NO_MEMORY;
 98
 99	/* Calculate the number of tables described in XSDT */
100	table_count =
101		(u32) ((table->length -
102		sizeof(struct acpi_table_header)) / sizeof(u64));
103	table_entry =
104		ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
105	for (i = 0; i < table_count; i++) {
106		ACPI_MOVE_64_TO_64(&xsdt_entry_address, table_entry);
107		if (!xsdt_entry_address) {
108			/* XSDT has NULL entry */
109			break;
110		}
111		table_entry += sizeof(u64);
112	}
113	acpi_os_unmap_memory(table, length);
114
115	if (i < table_count)
116		return AE_NULL_ENTRY;
117	else
118		return AE_OK;
119}
120
121/*******************************************************************************
122 *
123 * FUNCTION:    acpi_tb_initialize_facs
124 *
125 * PARAMETERS:  None
126 *
127 * RETURN:      Status
128 *
129 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
130 *              for accessing the Global Lock and Firmware Waking Vector
131 *
132 ******************************************************************************/
133
134acpi_status acpi_tb_initialize_facs(void)
135{
136	acpi_status status;
137
138	status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
139					 ACPI_CAST_INDIRECT_PTR(struct
140								acpi_table_header,
141								&acpi_gbl_FACS));
142	return status;
143}
144
145/*******************************************************************************
146 *
147 * FUNCTION:    acpi_tb_tables_loaded
148 *
149 * PARAMETERS:  None
150 *
151 * RETURN:      TRUE if required ACPI tables are loaded
152 *
153 * DESCRIPTION: Determine if the minimum required ACPI tables are present
154 *              (FADT, FACS, DSDT)
155 *
156 ******************************************************************************/
157
158u8 acpi_tb_tables_loaded(void)
159{
160
161	if (acpi_gbl_root_table_list.current_table_count >= 3) {
162		return (TRUE);
163	}
164
165	return (FALSE);
166}
167
168/*******************************************************************************
169 *
170 * FUNCTION:    acpi_tb_fix_string
171 *
172 * PARAMETERS:  String              - String to be repaired
173 *              Length              - Maximum length
174 *
175 * RETURN:      None
176 *
177 * DESCRIPTION: Replace every non-printable or non-ascii byte in the string
178 *              with a question mark '?'.
179 *
180 ******************************************************************************/
181
182static void acpi_tb_fix_string(char *string, acpi_size length)
183{
184
185	while (length && *string) {
186		if (!ACPI_IS_PRINT(*string)) {
187			*string = '?';
188		}
189		string++;
190		length--;
191	}
192}
193
194/*******************************************************************************
195 *
196 * FUNCTION:    acpi_tb_cleanup_table_header
197 *
198 * PARAMETERS:  out_header          - Where the cleaned header is returned
199 *              Header              - Input ACPI table header
200 *
201 * RETURN:      Returns the cleaned header in out_header
202 *
203 * DESCRIPTION: Copy the table header and ensure that all "string" fields in
204 *              the header consist of printable characters.
205 *
206 ******************************************************************************/
207
208static void
209acpi_tb_cleanup_table_header(struct acpi_table_header *out_header,
210			     struct acpi_table_header *header)
211{
212
213	ACPI_MEMCPY(out_header, header, sizeof(struct acpi_table_header));
214
215	acpi_tb_fix_string(out_header->signature, ACPI_NAME_SIZE);
216	acpi_tb_fix_string(out_header->oem_id, ACPI_OEM_ID_SIZE);
217	acpi_tb_fix_string(out_header->oem_table_id, ACPI_OEM_TABLE_ID_SIZE);
218	acpi_tb_fix_string(out_header->asl_compiler_id, ACPI_NAME_SIZE);
219}
220
221/*******************************************************************************
222 *
223 * FUNCTION:    acpi_tb_print_table_header
224 *
225 * PARAMETERS:  Address             - Table physical address
226 *              Header              - Table header
227 *
228 * RETURN:      None
229 *
230 * DESCRIPTION: Print an ACPI table header. Special cases for FACS and RSDP.
231 *
232 ******************************************************************************/
233
234void
235acpi_tb_print_table_header(acpi_physical_address address,
236			   struct acpi_table_header *header)
237{
238	struct acpi_table_header local_header;
239
240	/*
241	 * The reason that the Address is cast to a void pointer is so that we
242	 * can use %p which will work properly on both 32-bit and 64-bit hosts.
243	 */
244	if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_FACS)) {
245
246		/* FACS only has signature and length fields */
247
248		ACPI_INFO((AE_INFO, "%4.4s %p %05X",
249			   header->signature, ACPI_CAST_PTR(void, address),
250			   header->length));
251	} else if (ACPI_COMPARE_NAME(header->signature, ACPI_SIG_RSDP)) {
252
253		/* RSDP has no common fields */
254
255		ACPI_MEMCPY(local_header.oem_id,
256			    ACPI_CAST_PTR(struct acpi_table_rsdp,
257					  header)->oem_id, ACPI_OEM_ID_SIZE);
258		acpi_tb_fix_string(local_header.oem_id, ACPI_OEM_ID_SIZE);
259
260		ACPI_INFO((AE_INFO, "RSDP %p %05X (v%.2d %6.6s)",
261			   ACPI_CAST_PTR (void, address),
262			   (ACPI_CAST_PTR(struct acpi_table_rsdp, header)->
263			    revision >
264			    0) ? ACPI_CAST_PTR(struct acpi_table_rsdp,
265					       header)->length : 20,
266			   ACPI_CAST_PTR(struct acpi_table_rsdp,
267					 header)->revision,
268			   local_header.oem_id));
269	} else {
270		/* Standard ACPI table with full common header */
271
272		acpi_tb_cleanup_table_header(&local_header, header);
273
274		ACPI_INFO((AE_INFO,
275			   "%4.4s %p %05X (v%.2d %6.6s %8.8s %08X %4.4s %08X)",
276			   local_header.signature, ACPI_CAST_PTR(void, address),
277			   local_header.length, local_header.revision,
278			   local_header.oem_id, local_header.oem_table_id,
279			   local_header.oem_revision,
280			   local_header.asl_compiler_id,
281			   local_header.asl_compiler_revision));
282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283	}
284}
285
286/*******************************************************************************
287 *
288 * FUNCTION:    acpi_tb_validate_checksum
289 *
290 * PARAMETERS:  Table               - ACPI table to verify
291 *              Length              - Length of entire table
292 *
293 * RETURN:      Status
294 *
295 * DESCRIPTION: Verifies that the table checksums to zero. Optionally returns
296 *              exception on bad checksum.
297 *
298 ******************************************************************************/
299
300acpi_status acpi_tb_verify_checksum(struct acpi_table_header *table, u32 length)
301{
302	u8 checksum;
303
304	/* Compute the checksum on the table */
305
306	checksum = acpi_tb_checksum(ACPI_CAST_PTR(u8, table), length);
307
308	/* Checksum ok? (should be zero) */
309
310	if (checksum) {
311		ACPI_WARNING((AE_INFO,
312			      "Incorrect checksum in table [%4.4s] - 0x%2.2X, should be 0x%2.2X",
313			      table->signature, table->checksum,
314			      (u8) (table->checksum - checksum)));
315
316#if (ACPI_CHECKSUM_ABORT)
317
318		return (AE_BAD_CHECKSUM);
319#endif
320	}
321
322	return (AE_OK);
323}
324
325/*******************************************************************************
326 *
327 * FUNCTION:    acpi_tb_checksum
328 *
329 * PARAMETERS:  Buffer          - Pointer to memory region to be checked
330 *              Length          - Length of this memory region
331 *
332 * RETURN:      Checksum (u8)
333 *
334 * DESCRIPTION: Calculates circular checksum of memory region.
335 *
336 ******************************************************************************/
337
338u8 acpi_tb_checksum(u8 *buffer, u32 length)
339{
340	u8 sum = 0;
341	u8 *end = buffer + length;
342
343	while (buffer < end) {
344		sum = (u8) (sum + *(buffer++));
345	}
346
347	return sum;
348}
349
350/*******************************************************************************
351 *
352 * FUNCTION:    acpi_tb_check_dsdt_header
353 *
354 * PARAMETERS:  None
355 *
356 * RETURN:      None
357 *
358 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
359 *              if the DSDT has been replaced from outside the OS and/or if
360 *              the DSDT header has been corrupted.
361 *
362 ******************************************************************************/
363
364void acpi_tb_check_dsdt_header(void)
365{
366
367	/* Compare original length and checksum to current values */
368
369	if (acpi_gbl_original_dsdt_header.length != acpi_gbl_DSDT->length ||
370	    acpi_gbl_original_dsdt_header.checksum != acpi_gbl_DSDT->checksum) {
371		ACPI_ERROR((AE_INFO,
372			    "The DSDT has been corrupted or replaced - old, new headers below"));
 
 
373		acpi_tb_print_table_header(0, &acpi_gbl_original_dsdt_header);
374		acpi_tb_print_table_header(0, acpi_gbl_DSDT);
375
376		ACPI_ERROR((AE_INFO,
377			    "Please send DMI info to linux-acpi@vger.kernel.org\n"
378			    "If system does not work as expected, please boot with acpi=copy_dsdt"));
379
380		/* Disable further error messages */
381
382		acpi_gbl_original_dsdt_header.length = acpi_gbl_DSDT->length;
383		acpi_gbl_original_dsdt_header.checksum =
384		    acpi_gbl_DSDT->checksum;
385	}
386}
387
388/*******************************************************************************
389 *
390 * FUNCTION:    acpi_tb_copy_dsdt
391 *
392 * PARAMETERS:  table_desc          - Installed table to copy
393 *
394 * RETURN:      None
395 *
396 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
397 *              Some very bad BIOSs are known to either corrupt the DSDT or
398 *              install a new, bad DSDT. This copy works around the problem.
399 *
400 ******************************************************************************/
401
402struct acpi_table_header *acpi_tb_copy_dsdt(u32 table_index)
403{
404	struct acpi_table_header *new_table;
405	struct acpi_table_desc *table_desc;
406
407	table_desc = &acpi_gbl_root_table_list.tables[table_index];
408
409	new_table = ACPI_ALLOCATE(table_desc->length);
410	if (!new_table) {
411		ACPI_ERROR((AE_INFO, "Could not copy DSDT of length 0x%X",
412			    table_desc->length));
413		return (NULL);
414	}
415
416	ACPI_MEMCPY(new_table, table_desc->pointer, table_desc->length);
417	acpi_tb_delete_table(table_desc);
418	table_desc->pointer = new_table;
419	table_desc->flags = ACPI_TABLE_ORIGIN_ALLOCATED;
420
421	ACPI_INFO((AE_INFO,
422		   "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
423		   new_table->length));
424
425	return (new_table);
426}
 
 
 
427
428/*******************************************************************************
429 *
430 * FUNCTION:    acpi_tb_install_table
431 *
432 * PARAMETERS:  Address                 - Physical address of DSDT or FACS
433 *              Signature               - Table signature, NULL if no need to
434 *                                        match
435 *              table_index             - Index into root table array
436 *
437 * RETURN:      None
438 *
439 * DESCRIPTION: Install an ACPI table into the global data structure. The
440 *              table override mechanism is implemented here to allow the host
441 *              OS to replace any table before it is installed in the root
442 *              table array.
443 *
444 ******************************************************************************/
445
446void
447acpi_tb_install_table(acpi_physical_address address,
448		      char *signature, u32 table_index)
449{
450	u8 flags;
451	acpi_status status;
452	struct acpi_table_header *table_to_install;
453	struct acpi_table_header *mapped_table;
454	struct acpi_table_header *override_table = NULL;
455
456	if (!address) {
457		ACPI_ERROR((AE_INFO,
458			    "Null physical address for ACPI table [%s]",
459			    signature));
460		return;
461	}
462
463	/* Map just the table header */
464
465	mapped_table =
466	    acpi_os_map_memory(address, sizeof(struct acpi_table_header));
467	if (!mapped_table) {
468		return;
469	}
470
471	/* If a particular signature is expected (DSDT/FACS), it must match */
472
473	if (signature && !ACPI_COMPARE_NAME(mapped_table->signature, signature)) {
474		ACPI_ERROR((AE_INFO,
475			    "Invalid signature 0x%X for ACPI table, expected [%s]",
476			    *ACPI_CAST_PTR(u32, mapped_table->signature),
477			    signature));
478		goto unmap_and_exit;
479	}
480
481	/*
482	 * ACPI Table Override:
483	 *
484	 * Before we install the table, let the host OS override it with a new
485	 * one if desired. Any table within the RSDT/XSDT can be replaced,
486	 * including the DSDT which is pointed to by the FADT.
487	 */
488	status = acpi_os_table_override(mapped_table, &override_table);
489	if (ACPI_SUCCESS(status) && override_table) {
490		ACPI_INFO((AE_INFO,
491			   "%4.4s @ 0x%p Table override, replaced with:",
492			   mapped_table->signature, ACPI_CAST_PTR(void,
493								  address)));
494
495		acpi_gbl_root_table_list.tables[table_index].pointer =
496		    override_table;
497		address = ACPI_PTR_TO_PHYSADDR(override_table);
498
499		table_to_install = override_table;
500		flags = ACPI_TABLE_ORIGIN_OVERRIDE;
501	} else {
502		table_to_install = mapped_table;
503		flags = ACPI_TABLE_ORIGIN_MAPPED;
504	}
505
506	/* Initialize the table entry */
507
508	acpi_gbl_root_table_list.tables[table_index].address = address;
509	acpi_gbl_root_table_list.tables[table_index].length =
510	    table_to_install->length;
511	acpi_gbl_root_table_list.tables[table_index].flags = flags;
512
513	ACPI_MOVE_32_TO_32(&
514			   (acpi_gbl_root_table_list.tables[table_index].
515			    signature), table_to_install->signature);
516
517	acpi_tb_print_table_header(address, table_to_install);
518
519	if (table_index == ACPI_TABLE_INDEX_DSDT) {
520
521		/* Global integer width is based upon revision of the DSDT */
522
523		acpi_ut_set_integer_width(table_to_install->revision);
524	}
525
526      unmap_and_exit:
527	acpi_os_unmap_memory(mapped_table, sizeof(struct acpi_table_header));
528}
529
530/*******************************************************************************
531 *
532 * FUNCTION:    acpi_tb_get_root_table_entry
533 *
534 * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
535 *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
536 *
537 * RETURN:      Physical address extracted from the root table
538 *
539 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
540 *              both 32-bit and 64-bit platforms
541 *
542 * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
543 *              64-bit platforms.
544 *
545 ******************************************************************************/
546
547static acpi_physical_address
548acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
549{
550	u64 address64;
551
552	/*
553	 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
554	 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
555	 */
556	if (table_entry_size == sizeof(u32)) {
557		/*
558		 * 32-bit platform, RSDT: Return 32-bit table entry
559		 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
560		 */
561		return ((acpi_physical_address)
562			(*ACPI_CAST_PTR(u32, table_entry)));
563	} else {
564		/*
565		 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
566		 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
567		 *  return 64-bit
568		 */
569		ACPI_MOVE_64_TO_64(&address64, table_entry);
570
571#if ACPI_MACHINE_WIDTH == 32
572		if (address64 > ACPI_UINT32_MAX) {
573
574			/* Will truncate 64-bit address to 32 bits, issue warning */
575
576			ACPI_WARNING((AE_INFO,
577				      "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
578				      " truncating",
579				      ACPI_FORMAT_UINT64(address64)));
580		}
581#endif
582		return ((acpi_physical_address) (address64));
583	}
584}
585
586/*******************************************************************************
587 *
588 * FUNCTION:    acpi_tb_parse_root_table
589 *
590 * PARAMETERS:  Rsdp                    - Pointer to the RSDP
591 *
592 * RETURN:      Status
593 *
594 * DESCRIPTION: This function is called to parse the Root System Description
595 *              Table (RSDT or XSDT)
596 *
597 * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
598 *              be mapped and cannot be copied because it contains the actual
599 *              memory location of the ACPI Global Lock.
600 *
601 ******************************************************************************/
602
603acpi_status __init
604acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
605{
606	struct acpi_table_rsdp *rsdp;
607	u32 table_entry_size;
608	u32 i;
609	u32 table_count;
610	struct acpi_table_header *table;
611	acpi_physical_address address;
612	acpi_physical_address uninitialized_var(rsdt_address);
613	u32 length;
614	u8 *table_entry;
615	acpi_status status;
 
616
617	ACPI_FUNCTION_TRACE(tb_parse_root_table);
618
619	/*
620	 * Map the entire RSDP and extract the address of the RSDT or XSDT
621	 */
622	rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
623	if (!rsdp) {
624		return_ACPI_STATUS(AE_NO_MEMORY);
625	}
626
627	acpi_tb_print_table_header(rsdp_address,
628				   ACPI_CAST_PTR(struct acpi_table_header,
629						 rsdp));
630
631	/* Differentiate between RSDT and XSDT root tables */
632
633	if (rsdp->revision > 1 && rsdp->xsdt_physical_address
634			&& !acpi_rsdt_forced) {
635		/*
636		 * Root table is an XSDT (64-bit physical addresses). We must use the
637		 * XSDT if the revision is > 1 and the XSDT pointer is present, as per
638		 * the ACPI specification.
639		 */
640		address = (acpi_physical_address) rsdp->xsdt_physical_address;
641		table_entry_size = sizeof(u64);
642		rsdt_address = (acpi_physical_address)
643					rsdp->rsdt_physical_address;
644	} else {
645		/* Root table is an RSDT (32-bit physical addresses) */
646
647		address = (acpi_physical_address) rsdp->rsdt_physical_address;
648		table_entry_size = sizeof(u32);
649	}
650
651	/*
652	 * It is not possible to map more than one entry in some environments,
653	 * so unmap the RSDP here before mapping other tables
654	 */
655	acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
656
657	if (table_entry_size == sizeof(u64)) {
658		if (acpi_tb_check_xsdt(address) == AE_NULL_ENTRY) {
659			/* XSDT has NULL entry, RSDT is used */
660			address = rsdt_address;
661			table_entry_size = sizeof(u32);
662			ACPI_WARNING((AE_INFO, "BIOS XSDT has NULL entry, "
663					"using RSDT"));
664		}
665	}
666	/* Map the RSDT/XSDT table header to get the full table length */
667
668	table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
669	if (!table) {
670		return_ACPI_STATUS(AE_NO_MEMORY);
671	}
672
673	acpi_tb_print_table_header(address, table);
674
675	/* Get the length of the full table, verify length and map entire table */
676
 
 
677	length = table->length;
678	acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
679
680	if (length < sizeof(struct acpi_table_header)) {
681		ACPI_ERROR((AE_INFO, "Invalid length 0x%X in RSDT/XSDT",
682			    length));
 
683		return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
684	}
685
686	table = acpi_os_map_memory(address, length);
687	if (!table) {
688		return_ACPI_STATUS(AE_NO_MEMORY);
689	}
690
691	/* Validate the root table checksum */
692
693	status = acpi_tb_verify_checksum(table, length);
694	if (ACPI_FAILURE(status)) {
695		acpi_os_unmap_memory(table, length);
696		return_ACPI_STATUS(status);
697	}
698
699	/* Calculate the number of tables described in the root table */
700
701	table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
702			    table_entry_size);
703	/*
704	 * First two entries in the table array are reserved for the DSDT
705	 * and FACS, which are not actually present in the RSDT/XSDT - they
706	 * come from the FADT
707	 */
708	table_entry =
709	    ACPI_CAST_PTR(u8, table) + sizeof(struct acpi_table_header);
710	acpi_gbl_root_table_list.current_table_count = 2;
711
712	/*
713	 * Initialize the root table array from the RSDT/XSDT
714	 */
715	for (i = 0; i < table_count; i++) {
716		if (acpi_gbl_root_table_list.current_table_count >=
717		    acpi_gbl_root_table_list.max_table_count) {
718
719			/* There is no more room in the root table array, attempt resize */
 
 
 
720
721			status = acpi_tb_resize_root_table_list();
722			if (ACPI_FAILURE(status)) {
723				ACPI_WARNING((AE_INFO,
724					      "Truncating %u table entries!",
725					      (unsigned) (table_count -
726					       (acpi_gbl_root_table_list.
727							  current_table_count -
728							  2))));
729				break;
730			}
731		}
732
733		/* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
 
 
 
 
 
 
 
 
 
 
 
734
735		acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.
736						current_table_count].address =
737		    acpi_tb_get_root_table_entry(table_entry, table_entry_size);
738
739		table_entry += table_entry_size;
740		acpi_gbl_root_table_list.current_table_count++;
741	}
742
743	/*
744	 * It is not possible to map more than one entry in some environments,
745	 * so unmap the root table here before mapping other tables
746	 */
747	acpi_os_unmap_memory(table, length);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
748
749	/*
750	 * Complete the initialization of the root table array by examining
751	 * the header of each table
752	 */
753	for (i = 2; i < acpi_gbl_root_table_list.current_table_count; i++) {
754		acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
755				      address, NULL, i);
756
757		/* Special case for FADT - get the DSDT and FACS */
758
759		if (ACPI_COMPARE_NAME
760		    (&acpi_gbl_root_table_list.tables[i].signature,
761		     ACPI_SIG_FADT)) {
762			acpi_tb_parse_fadt(i);
763		}
764	}
765
 
 
 
 
 
 
 
 
 
 
766	return_ACPI_STATUS(AE_OK);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
767}