ACPI / SRAT: fix SRAT parsing order with both LAPIC and X2APIC present
[linux-2.6-block.git] / drivers / acpi / acpica / tbutils.c
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
49 ACPI_MODULE_NAME("tbutils")
50
51 /* Local prototypes */
52 static acpi_physical_address
53 acpi_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
69 acpi_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
114 void 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
154 struct 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
199 static acpi_physical_address
200 acpi_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
255 acpi_status __init acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
256 {
257         struct acpi_table_rsdp *rsdp;
258         u32 table_entry_size;
259         u32 i;
260         u32 table_count;
261         struct acpi_table_header *table;
262         acpi_physical_address address;
263         u32 length;
264         u8 *table_entry;
265         acpi_status status;
266         u32 table_index;
267
268         ACPI_FUNCTION_TRACE(tb_parse_root_table);
269
270         /* Map the entire RSDP and extract the address of the RSDT or XSDT */
271
272         rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
273         if (!rsdp) {
274                 return_ACPI_STATUS(AE_NO_MEMORY);
275         }
276
277         acpi_tb_print_table_header(rsdp_address,
278                                    ACPI_CAST_PTR(struct acpi_table_header,
279                                                  rsdp));
280
281         /* Use XSDT if present and not overridden. Otherwise, use RSDT */
282
283         if ((rsdp->revision > 1) &&
284             rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
285                 /*
286                  * RSDP contains an XSDT (64-bit physical addresses). We must use
287                  * the XSDT if the revision is > 1 and the XSDT pointer is present,
288                  * as per the ACPI specification.
289                  */
290                 address = (acpi_physical_address) rsdp->xsdt_physical_address;
291                 table_entry_size = ACPI_XSDT_ENTRY_SIZE;
292         } else {
293                 /* Root table is an RSDT (32-bit physical addresses) */
294
295                 address = (acpi_physical_address) rsdp->rsdt_physical_address;
296                 table_entry_size = ACPI_RSDT_ENTRY_SIZE;
297         }
298
299         /*
300          * It is not possible to map more than one entry in some environments,
301          * so unmap the RSDP here before mapping other tables
302          */
303         acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
304
305         /* Map the RSDT/XSDT table header to get the full table length */
306
307         table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
308         if (!table) {
309                 return_ACPI_STATUS(AE_NO_MEMORY);
310         }
311
312         acpi_tb_print_table_header(address, table);
313
314         /*
315          * Validate length of the table, and map entire table.
316          * Minimum length table must contain at least one entry.
317          */
318         length = table->length;
319         acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
320
321         if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
322                 ACPI_BIOS_ERROR((AE_INFO,
323                                  "Invalid table length 0x%X in RSDT/XSDT",
324                                  length));
325                 return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
326         }
327
328         table = acpi_os_map_memory(address, length);
329         if (!table) {
330                 return_ACPI_STATUS(AE_NO_MEMORY);
331         }
332
333         /* Validate the root table checksum */
334
335         status = acpi_tb_verify_checksum(table, length);
336         if (ACPI_FAILURE(status)) {
337                 acpi_os_unmap_memory(table, length);
338                 return_ACPI_STATUS(status);
339         }
340
341         /* Get the number of entries and pointer to first entry */
342
343         table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
344                             table_entry_size);
345         table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
346
347         /* Initialize the root table array from the RSDT/XSDT */
348
349         for (i = 0; i < table_count; i++) {
350
351                 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
352
353                 address =
354                     acpi_tb_get_root_table_entry(table_entry, table_entry_size);
355
356                 /* Skip NULL entries in RSDT/XSDT */
357
358                 if (!address) {
359                         goto next_table;
360                 }
361
362                 status = acpi_tb_install_standard_table(address,
363                                                         ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL,
364                                                         FALSE, TRUE,
365                                                         &table_index);
366
367                 if (ACPI_SUCCESS(status) &&
368                     ACPI_COMPARE_NAME(&acpi_gbl_root_table_list.
369                                       tables[table_index].signature,
370                                       ACPI_SIG_FADT)) {
371                         acpi_gbl_fadt_index = table_index;
372                         acpi_tb_parse_fadt();
373                 }
374
375 next_table:
376
377                 table_entry += table_entry_size;
378         }
379
380         acpi_os_unmap_memory(table, length);
381         return_ACPI_STATUS(AE_OK);
382 }
383
384 /*******************************************************************************
385  *
386  * FUNCTION:    acpi_is_valid_signature
387  *
388  * PARAMETERS:  signature           - Sig string to be validated
389  *
390  * RETURN:      TRUE if signature is has 4 valid ACPI characters
391  *
392  * DESCRIPTION: Validate an ACPI table signature.
393  *
394  ******************************************************************************/
395
396 u8 acpi_is_valid_signature(char *signature)
397 {
398         u32 i;
399
400         /* Validate each character in the signature */
401
402         for (i = 0; i < ACPI_NAME_SIZE; i++) {
403                 if (!acpi_ut_valid_acpi_char(signature[i], i)) {
404                         return (FALSE);
405                 }
406         }
407
408         return (TRUE);
409 }