Merge remote-tracking branches 'spi/fix/qup' and 'spi/fix/topcliff-pch' into spi...
[cascardo/linux.git] / drivers / acpi / acpica / tbutils.c
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_delete_table(table_desc);
182         table_desc->pointer = new_table;
183         table_desc->flags = ACPI_TABLE_ORIGIN_ALLOCATED;
184
185         ACPI_INFO((AE_INFO,
186                    "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
187                    new_table->length));
188
189         return (new_table);
190 }
191
192 /*******************************************************************************
193  *
194  * FUNCTION:    acpi_tb_install_table
195  *
196  * PARAMETERS:  address                 - Physical address of DSDT or FACS
197  *              signature               - Table signature, NULL if no need to
198  *                                        match
199  *              table_index             - Index into root table array
200  *
201  * RETURN:      None
202  *
203  * DESCRIPTION: Install an ACPI table into the global data structure. The
204  *              table override mechanism is called to allow the host
205  *              OS to replace any table before it is installed in the root
206  *              table array.
207  *
208  ******************************************************************************/
209
210 void
211 acpi_tb_install_table(acpi_physical_address address,
212                       char *signature, u32 table_index)
213 {
214         struct acpi_table_header *table;
215         struct acpi_table_header *final_table;
216         struct acpi_table_desc *table_desc;
217
218         if (!address) {
219                 ACPI_ERROR((AE_INFO,
220                             "Null physical address for ACPI table [%s]",
221                             signature));
222                 return;
223         }
224
225         /* Map just the table header */
226
227         table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
228         if (!table) {
229                 ACPI_ERROR((AE_INFO,
230                             "Could not map memory for table [%s] at %p",
231                             signature, ACPI_CAST_PTR(void, address)));
232                 return;
233         }
234
235         /* If a particular signature is expected (DSDT/FACS), it must match */
236
237         if (signature && !ACPI_COMPARE_NAME(table->signature, signature)) {
238                 ACPI_BIOS_ERROR((AE_INFO,
239                                  "Invalid signature 0x%X for ACPI table, expected [%s]",
240                                  *ACPI_CAST_PTR(u32, table->signature),
241                                  signature));
242                 goto unmap_and_exit;
243         }
244
245         /*
246          * Initialize the table entry. Set the pointer to NULL, since the
247          * table is not fully mapped at this time.
248          */
249         table_desc = &acpi_gbl_root_table_list.tables[table_index];
250
251         table_desc->address = address;
252         table_desc->pointer = NULL;
253         table_desc->length = table->length;
254         table_desc->flags = ACPI_TABLE_ORIGIN_MAPPED;
255         ACPI_MOVE_32_TO_32(table_desc->signature.ascii, table->signature);
256
257         /*
258          * ACPI Table Override:
259          *
260          * Before we install the table, let the host OS override it with a new
261          * one if desired. Any table within the RSDT/XSDT can be replaced,
262          * including the DSDT which is pointed to by the FADT.
263          *
264          * NOTE: If the table is overridden, then final_table will contain a
265          * mapped pointer to the full new table. If the table is not overridden,
266          * or if there has been a physical override, then the table will be
267          * fully mapped later (in verify table). In any case, we must
268          * unmap the header that was mapped above.
269          */
270         final_table = acpi_tb_table_override(table, table_desc);
271         if (!final_table) {
272                 final_table = table;    /* There was no override */
273         }
274
275         acpi_tb_print_table_header(table_desc->address, final_table);
276
277         /* Set the global integer width (based upon revision of the DSDT) */
278
279         if (table_index == ACPI_TABLE_INDEX_DSDT) {
280                 acpi_ut_set_integer_width(final_table->revision);
281         }
282
283         /*
284          * If we have a physical override during this early loading of the ACPI
285          * tables, unmap the table for now. It will be mapped again later when
286          * it is actually used. This supports very early loading of ACPI tables,
287          * before virtual memory is fully initialized and running within the
288          * host OS. Note: A logical override has the ACPI_TABLE_ORIGIN_OVERRIDE
289          * flag set and will not be deleted below.
290          */
291         if (final_table != table) {
292                 acpi_tb_delete_table(table_desc);
293         }
294
295 unmap_and_exit:
296
297         /* Always unmap the table header that we mapped above */
298
299         acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
300 }
301
302 /*******************************************************************************
303  *
304  * FUNCTION:    acpi_tb_get_root_table_entry
305  *
306  * PARAMETERS:  table_entry         - Pointer to the RSDT/XSDT table entry
307  *              table_entry_size    - sizeof 32 or 64 (RSDT or XSDT)
308  *
309  * RETURN:      Physical address extracted from the root table
310  *
311  * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
312  *              both 32-bit and 64-bit platforms
313  *
314  * NOTE:        acpi_physical_address is 32-bit on 32-bit platforms, 64-bit on
315  *              64-bit platforms.
316  *
317  ******************************************************************************/
318
319 static acpi_physical_address
320 acpi_tb_get_root_table_entry(u8 *table_entry, u32 table_entry_size)
321 {
322         u64 address64;
323
324         /*
325          * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
326          * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
327          */
328         if (table_entry_size == ACPI_RSDT_ENTRY_SIZE) {
329                 /*
330                  * 32-bit platform, RSDT: Return 32-bit table entry
331                  * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
332                  */
333                 return ((acpi_physical_address)
334                         (*ACPI_CAST_PTR(u32, table_entry)));
335         } else {
336                 /*
337                  * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
338                  * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
339                  *  return 64-bit
340                  */
341                 ACPI_MOVE_64_TO_64(&address64, table_entry);
342
343 #if ACPI_MACHINE_WIDTH == 32
344                 if (address64 > ACPI_UINT32_MAX) {
345
346                         /* Will truncate 64-bit address to 32 bits, issue warning */
347
348                         ACPI_BIOS_WARNING((AE_INFO,
349                                            "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
350                                            " truncating",
351                                            ACPI_FORMAT_UINT64(address64)));
352                 }
353 #endif
354                 return ((acpi_physical_address) (address64));
355         }
356 }
357
358 /*******************************************************************************
359  *
360  * FUNCTION:    acpi_tb_validate_xsdt
361  *
362  * PARAMETERS:  address             - Physical address of the XSDT (from RSDP)
363  *
364  * RETURN:      Status. AE_OK if the table appears to be valid.
365  *
366  * DESCRIPTION: Validate an XSDT to ensure that it is of minimum size and does
367  *              not contain any NULL entries. A problem that is seen in the
368  *              field is that the XSDT exists, but is actually useless because
369  *              of one or more (or all) NULL entries.
370  *
371  ******************************************************************************/
372
373 static acpi_status acpi_tb_validate_xsdt(acpi_physical_address xsdt_address)
374 {
375         struct acpi_table_header *table;
376         u8 *next_entry;
377         acpi_physical_address address;
378         u32 length;
379         u32 entry_count;
380         acpi_status status;
381         u32 i;
382
383         /* Get the XSDT length */
384
385         table =
386             acpi_os_map_memory(xsdt_address, sizeof(struct acpi_table_header));
387         if (!table) {
388                 return (AE_NO_MEMORY);
389         }
390
391         length = table->length;
392         acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
393
394         /*
395          * Minimum XSDT length is the size of the standard ACPI header
396          * plus one physical address entry
397          */
398         if (length < (sizeof(struct acpi_table_header) + ACPI_XSDT_ENTRY_SIZE)) {
399                 return (AE_INVALID_TABLE_LENGTH);
400         }
401
402         /* Map the entire XSDT */
403
404         table = acpi_os_map_memory(xsdt_address, length);
405         if (!table) {
406                 return (AE_NO_MEMORY);
407         }
408
409         /* Get the number of entries and pointer to first entry */
410
411         status = AE_OK;
412         next_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
413         entry_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
414                             ACPI_XSDT_ENTRY_SIZE);
415
416         /* Validate each entry (physical address) within the XSDT */
417
418         for (i = 0; i < entry_count; i++) {
419                 address =
420                     acpi_tb_get_root_table_entry(next_entry,
421                                                  ACPI_XSDT_ENTRY_SIZE);
422                 if (!address) {
423
424                         /* Detected a NULL entry, XSDT is invalid */
425
426                         status = AE_NULL_ENTRY;
427                         break;
428                 }
429
430                 next_entry += ACPI_XSDT_ENTRY_SIZE;
431         }
432
433         /* Unmap table */
434
435         acpi_os_unmap_memory(table, length);
436         return (status);
437 }
438
439 /*******************************************************************************
440  *
441  * FUNCTION:    acpi_tb_parse_root_table
442  *
443  * PARAMETERS:  rsdp                    - Pointer to the RSDP
444  *
445  * RETURN:      Status
446  *
447  * DESCRIPTION: This function is called to parse the Root System Description
448  *              Table (RSDT or XSDT)
449  *
450  * NOTE:        Tables are mapped (not copied) for efficiency. The FACS must
451  *              be mapped and cannot be copied because it contains the actual
452  *              memory location of the ACPI Global Lock.
453  *
454  ******************************************************************************/
455
456 acpi_status __init acpi_tb_parse_root_table(acpi_physical_address rsdp_address)
457 {
458         struct acpi_table_rsdp *rsdp;
459         u32 table_entry_size;
460         u32 i;
461         u32 table_count;
462         struct acpi_table_header *table;
463         acpi_physical_address address;
464         acpi_physical_address rsdt_address;
465         u32 length;
466         u8 *table_entry;
467         acpi_status status;
468
469         ACPI_FUNCTION_TRACE(tb_parse_root_table);
470
471         /* Map the entire RSDP and extract the address of the RSDT or XSDT */
472
473         rsdp = acpi_os_map_memory(rsdp_address, sizeof(struct acpi_table_rsdp));
474         if (!rsdp) {
475                 return_ACPI_STATUS(AE_NO_MEMORY);
476         }
477
478         acpi_tb_print_table_header(rsdp_address,
479                                    ACPI_CAST_PTR(struct acpi_table_header,
480                                                  rsdp));
481
482         /* Use XSDT if present and not overridden. Otherwise, use RSDT */
483
484         if ((rsdp->revision > 1) &&
485             rsdp->xsdt_physical_address && !acpi_gbl_do_not_use_xsdt) {
486                 /*
487                  * RSDP contains an XSDT (64-bit physical addresses). We must use
488                  * the XSDT if the revision is > 1 and the XSDT pointer is present,
489                  * as per the ACPI specification.
490                  */
491                 address = (acpi_physical_address) rsdp->xsdt_physical_address;
492                 rsdt_address =
493                     (acpi_physical_address) rsdp->rsdt_physical_address;
494                 table_entry_size = ACPI_XSDT_ENTRY_SIZE;
495         } else {
496                 /* Root table is an RSDT (32-bit physical addresses) */
497
498                 address = (acpi_physical_address) rsdp->rsdt_physical_address;
499                 rsdt_address = address;
500                 table_entry_size = ACPI_RSDT_ENTRY_SIZE;
501         }
502
503         /*
504          * It is not possible to map more than one entry in some environments,
505          * so unmap the RSDP here before mapping other tables
506          */
507         acpi_os_unmap_memory(rsdp, sizeof(struct acpi_table_rsdp));
508
509         /*
510          * If it is present and used, validate the XSDT for access/size
511          * and ensure that all table entries are at least non-NULL
512          */
513         if (table_entry_size == ACPI_XSDT_ENTRY_SIZE) {
514                 status = acpi_tb_validate_xsdt(address);
515                 if (ACPI_FAILURE(status)) {
516                         ACPI_BIOS_WARNING((AE_INFO,
517                                            "XSDT is invalid (%s), using RSDT",
518                                            acpi_format_exception(status)));
519
520                         /* Fall back to the RSDT */
521
522                         address = rsdt_address;
523                         table_entry_size = ACPI_RSDT_ENTRY_SIZE;
524                 }
525         }
526
527         /* Map the RSDT/XSDT table header to get the full table length */
528
529         table = acpi_os_map_memory(address, sizeof(struct acpi_table_header));
530         if (!table) {
531                 return_ACPI_STATUS(AE_NO_MEMORY);
532         }
533
534         acpi_tb_print_table_header(address, table);
535
536         /*
537          * Validate length of the table, and map entire table.
538          * Minimum length table must contain at least one entry.
539          */
540         length = table->length;
541         acpi_os_unmap_memory(table, sizeof(struct acpi_table_header));
542
543         if (length < (sizeof(struct acpi_table_header) + table_entry_size)) {
544                 ACPI_BIOS_ERROR((AE_INFO,
545                                  "Invalid table length 0x%X in RSDT/XSDT",
546                                  length));
547                 return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
548         }
549
550         table = acpi_os_map_memory(address, length);
551         if (!table) {
552                 return_ACPI_STATUS(AE_NO_MEMORY);
553         }
554
555         /* Validate the root table checksum */
556
557         status = acpi_tb_verify_checksum(table, length);
558         if (ACPI_FAILURE(status)) {
559                 acpi_os_unmap_memory(table, length);
560                 return_ACPI_STATUS(status);
561         }
562
563         /* Get the number of entries and pointer to first entry */
564
565         table_count = (u32)((table->length - sizeof(struct acpi_table_header)) /
566                             table_entry_size);
567         table_entry = ACPI_ADD_PTR(u8, table, sizeof(struct acpi_table_header));
568
569         /*
570          * First two entries in the table array are reserved for the DSDT
571          * and FACS, which are not actually present in the RSDT/XSDT - they
572          * come from the FADT
573          */
574         acpi_gbl_root_table_list.current_table_count = 2;
575
576         /* Initialize the root table array from the RSDT/XSDT */
577
578         for (i = 0; i < table_count; i++) {
579                 if (acpi_gbl_root_table_list.current_table_count >=
580                     acpi_gbl_root_table_list.max_table_count) {
581
582                         /* There is no more room in the root table array, attempt resize */
583
584                         status = acpi_tb_resize_root_table_list();
585                         if (ACPI_FAILURE(status)) {
586                                 ACPI_WARNING((AE_INFO,
587                                               "Truncating %u table entries!",
588                                               (unsigned) (table_count -
589                                                (acpi_gbl_root_table_list.
590                                                           current_table_count -
591                                                           2))));
592                                 break;
593                         }
594                 }
595
596                 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
597
598                 acpi_gbl_root_table_list.tables[acpi_gbl_root_table_list.
599                                                 current_table_count].address =
600                     acpi_tb_get_root_table_entry(table_entry, table_entry_size);
601
602                 table_entry += table_entry_size;
603                 acpi_gbl_root_table_list.current_table_count++;
604         }
605
606         /*
607          * It is not possible to map more than one entry in some environments,
608          * so unmap the root table here before mapping other tables
609          */
610         acpi_os_unmap_memory(table, length);
611
612         /*
613          * Complete the initialization of the root table array by examining
614          * the header of each table
615          */
616         for (i = 2; i < acpi_gbl_root_table_list.current_table_count; i++) {
617                 acpi_tb_install_table(acpi_gbl_root_table_list.tables[i].
618                                       address, NULL, i);
619
620                 /* Special case for FADT - validate it then get the DSDT and FACS */
621
622                 if (ACPI_COMPARE_NAME
623                     (&acpi_gbl_root_table_list.tables[i].signature,
624                      ACPI_SIG_FADT)) {
625                         acpi_tb_parse_fadt(i);
626                 }
627         }
628
629         return_ACPI_STATUS(AE_OK);
630 }