73cf76ecc5f77a9cf5686f9f03dfcfde1a68e52b
[reactos.git] / reactos / drivers / bus / acpi / acpica / tables / tbutils.c
1 /******************************************************************************
2 *
3 * Module Name: tbutils - ACPI Table utilities
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2014, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116 #define __TBUTILS_C__
117
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "actables.h"
121
122 #define _COMPONENT ACPI_TABLES
123 ACPI_MODULE_NAME ("tbutils")
124
125
126 /* Local prototypes */
127
128 static ACPI_STATUS
129 AcpiTbValidateXsdt (
130 ACPI_PHYSICAL_ADDRESS Address);
131
132 static ACPI_PHYSICAL_ADDRESS
133 AcpiTbGetRootTableEntry (
134 UINT8 *TableEntry,
135 UINT32 TableEntrySize);
136
137
138 #if (!ACPI_REDUCED_HARDWARE)
139 /*******************************************************************************
140 *
141 * FUNCTION: AcpiTbInitializeFacs
142 *
143 * PARAMETERS: None
144 *
145 * RETURN: Status
146 *
147 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
148 * for accessing the Global Lock and Firmware Waking Vector
149 *
150 ******************************************************************************/
151
152 ACPI_STATUS
153 AcpiTbInitializeFacs (
154 void)
155 {
156 ACPI_STATUS Status;
157
158
159 /* If Hardware Reduced flag is set, there is no FACS */
160
161 if (AcpiGbl_ReducedHardware)
162 {
163 AcpiGbl_FACS = NULL;
164 return (AE_OK);
165 }
166
167 Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS,
168 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &AcpiGbl_FACS));
169 return (Status);
170 }
171 #endif /* !ACPI_REDUCED_HARDWARE */
172
173
174 /*******************************************************************************
175 *
176 * FUNCTION: AcpiTbTablesLoaded
177 *
178 * PARAMETERS: None
179 *
180 * RETURN: TRUE if required ACPI tables are loaded
181 *
182 * DESCRIPTION: Determine if the minimum required ACPI tables are present
183 * (FADT, FACS, DSDT)
184 *
185 ******************************************************************************/
186
187 BOOLEAN
188 AcpiTbTablesLoaded (
189 void)
190 {
191
192 if (AcpiGbl_RootTableList.CurrentTableCount >= 3)
193 {
194 return (TRUE);
195 }
196
197 return (FALSE);
198 }
199
200
201 /*******************************************************************************
202 *
203 * FUNCTION: AcpiTbCheckDsdtHeader
204 *
205 * PARAMETERS: None
206 *
207 * RETURN: None
208 *
209 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
210 * if the DSDT has been replaced from outside the OS and/or if
211 * the DSDT header has been corrupted.
212 *
213 ******************************************************************************/
214
215 void
216 AcpiTbCheckDsdtHeader (
217 void)
218 {
219
220 /* Compare original length and checksum to current values */
221
222 if (AcpiGbl_OriginalDsdtHeader.Length != AcpiGbl_DSDT->Length ||
223 AcpiGbl_OriginalDsdtHeader.Checksum != AcpiGbl_DSDT->Checksum)
224 {
225 ACPI_BIOS_ERROR ((AE_INFO,
226 "The DSDT has been corrupted or replaced - "
227 "old, new headers below"));
228 AcpiTbPrintTableHeader (0, &AcpiGbl_OriginalDsdtHeader);
229 AcpiTbPrintTableHeader (0, AcpiGbl_DSDT);
230
231 /* Disable further error messages */
232
233 AcpiGbl_OriginalDsdtHeader.Length = AcpiGbl_DSDT->Length;
234 AcpiGbl_OriginalDsdtHeader.Checksum = AcpiGbl_DSDT->Checksum;
235 }
236 }
237
238
239 /*******************************************************************************
240 *
241 * FUNCTION: AcpiTbCopyDsdt
242 *
243 * PARAMETERS: TableDesc - Installed table to copy
244 *
245 * RETURN: None
246 *
247 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
248 * Some very bad BIOSs are known to either corrupt the DSDT or
249 * install a new, bad DSDT. This copy works around the problem.
250 *
251 ******************************************************************************/
252
253 ACPI_TABLE_HEADER *
254 AcpiTbCopyDsdt (
255 UINT32 TableIndex)
256 {
257 ACPI_TABLE_HEADER *NewTable;
258 ACPI_TABLE_DESC *TableDesc;
259
260
261 TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex];
262
263 NewTable = ACPI_ALLOCATE (TableDesc->Length);
264 if (!NewTable)
265 {
266 ACPI_ERROR ((AE_INFO, "Could not copy DSDT of length 0x%X",
267 TableDesc->Length));
268 return (NULL);
269 }
270
271 ACPI_MEMCPY (NewTable, TableDesc->Pointer, TableDesc->Length);
272 AcpiTbUninstallTable (TableDesc);
273
274 AcpiTbInitTableDescriptor (
275 &AcpiGbl_RootTableList.Tables[ACPI_TABLE_INDEX_DSDT],
276 ACPI_PTR_TO_PHYSADDR (NewTable), ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
277 NewTable);
278
279 ACPI_INFO ((AE_INFO,
280 "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
281 NewTable->Length));
282
283 return (NewTable);
284 }
285
286
287 /*******************************************************************************
288 *
289 * FUNCTION: AcpiTbGetRootTableEntry
290 *
291 * PARAMETERS: TableEntry - Pointer to the RSDT/XSDT table entry
292 * TableEntrySize - sizeof 32 or 64 (RSDT or XSDT)
293 *
294 * RETURN: Physical address extracted from the root table
295 *
296 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
297 * both 32-bit and 64-bit platforms
298 *
299 * NOTE: ACPI_PHYSICAL_ADDRESS is 32-bit on 32-bit platforms, 64-bit on
300 * 64-bit platforms.
301 *
302 ******************************************************************************/
303
304 static ACPI_PHYSICAL_ADDRESS
305 AcpiTbGetRootTableEntry (
306 UINT8 *TableEntry,
307 UINT32 TableEntrySize)
308 {
309 UINT64 Address64;
310
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 (TableEntrySize == ACPI_RSDT_ENTRY_SIZE)
317 {
318 /*
319 * 32-bit platform, RSDT: Return 32-bit table entry
320 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
321 */
322 return ((ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST_PTR (UINT32, TableEntry)));
323 }
324 else
325 {
326 /*
327 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
328 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
329 * return 64-bit
330 */
331 ACPI_MOVE_64_TO_64 (&Address64, TableEntry);
332
333 #if ACPI_MACHINE_WIDTH == 32
334 if (Address64 > ACPI_UINT32_MAX)
335 {
336 /* Will truncate 64-bit address to 32 bits, issue warning */
337
338 ACPI_BIOS_WARNING ((AE_INFO,
339 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
340 " truncating",
341 ACPI_FORMAT_UINT64 (Address64)));
342 }
343 #endif
344 return ((ACPI_PHYSICAL_ADDRESS) (Address64));
345 }
346 }
347
348
349 /*******************************************************************************
350 *
351 * FUNCTION: AcpiTbValidateXsdt
352 *
353 * PARAMETERS: Address - Physical address of the XSDT (from RSDP)
354 *
355 * RETURN: Status. AE_OK if the table appears to be valid.
356 *
357 * DESCRIPTION: Validate an XSDT to ensure that it is of minimum size and does
358 * not contain any NULL entries. A problem that is seen in the
359 * field is that the XSDT exists, but is actually useless because
360 * of one or more (or all) NULL entries.
361 *
362 ******************************************************************************/
363
364 static ACPI_STATUS
365 AcpiTbValidateXsdt (
366 ACPI_PHYSICAL_ADDRESS XsdtAddress)
367 {
368 ACPI_TABLE_HEADER *Table;
369 UINT8 *NextEntry;
370 ACPI_PHYSICAL_ADDRESS Address;
371 UINT32 Length;
372 UINT32 EntryCount;
373 ACPI_STATUS Status;
374 UINT32 i;
375
376
377 /* Get the XSDT length */
378
379 Table = AcpiOsMapMemory (XsdtAddress, sizeof (ACPI_TABLE_HEADER));
380 if (!Table)
381 {
382 return (AE_NO_MEMORY);
383 }
384
385 Length = Table->Length;
386 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));
387
388 /*
389 * Minimum XSDT length is the size of the standard ACPI header
390 * plus one physical address entry
391 */
392 if (Length < (sizeof (ACPI_TABLE_HEADER) + ACPI_XSDT_ENTRY_SIZE))
393 {
394 return (AE_INVALID_TABLE_LENGTH);
395 }
396
397 /* Map the entire XSDT */
398
399 Table = AcpiOsMapMemory (XsdtAddress, Length);
400 if (!Table)
401 {
402 return (AE_NO_MEMORY);
403 }
404
405 /* Get the number of entries and pointer to first entry */
406
407 Status = AE_OK;
408 NextEntry = ACPI_ADD_PTR (UINT8, Table, sizeof (ACPI_TABLE_HEADER));
409 EntryCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) /
410 ACPI_XSDT_ENTRY_SIZE);
411
412 /* Validate each entry (physical address) within the XSDT */
413
414 for (i = 0; i < EntryCount; i++)
415 {
416 Address = AcpiTbGetRootTableEntry (NextEntry, ACPI_XSDT_ENTRY_SIZE);
417 if (!Address)
418 {
419 /* Detected a NULL entry, XSDT is invalid */
420
421 Status = AE_NULL_ENTRY;
422 break;
423 }
424
425 NextEntry += ACPI_XSDT_ENTRY_SIZE;
426 }
427
428 /* Unmap table */
429
430 AcpiOsUnmapMemory (Table, Length);
431 return (Status);
432 }
433
434
435 /*******************************************************************************
436 *
437 * FUNCTION: AcpiTbParseRootTable
438 *
439 * PARAMETERS: Rsdp - Pointer to the RSDP
440 *
441 * RETURN: Status
442 *
443 * DESCRIPTION: This function is called to parse the Root System Description
444 * Table (RSDT or XSDT)
445 *
446 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
447 * be mapped and cannot be copied because it contains the actual
448 * memory location of the ACPI Global Lock.
449 *
450 ******************************************************************************/
451
452 ACPI_STATUS
453 AcpiTbParseRootTable (
454 ACPI_PHYSICAL_ADDRESS RsdpAddress)
455 {
456 ACPI_TABLE_RSDP *Rsdp;
457 UINT32 TableEntrySize;
458 UINT32 i;
459 UINT32 TableCount;
460 ACPI_TABLE_HEADER *Table;
461 ACPI_PHYSICAL_ADDRESS Address;
462 UINT32 Length;
463 UINT8 *TableEntry;
464 ACPI_STATUS Status;
465 UINT32 TableIndex;
466
467
468 ACPI_FUNCTION_TRACE (TbParseRootTable);
469
470
471 /* Map the entire RSDP and extract the address of the RSDT or XSDT */
472
473 Rsdp = AcpiOsMapMemory (RsdpAddress, sizeof (ACPI_TABLE_RSDP));
474 if (!Rsdp)
475 {
476 return_ACPI_STATUS (AE_NO_MEMORY);
477 }
478
479 AcpiTbPrintTableHeader (RsdpAddress,
480 ACPI_CAST_PTR (ACPI_TABLE_HEADER, Rsdp));
481
482 /* Use XSDT if present and not overridden. Otherwise, use RSDT */
483
484 if ((Rsdp->Revision > 1) &&
485 Rsdp->XsdtPhysicalAddress &&
486 !AcpiGbl_DoNotUseXsdt)
487 {
488 /*
489 * RSDP contains an XSDT (64-bit physical addresses). We must use
490 * the XSDT if the revision is > 1 and the XSDT pointer is present,
491 * as per the ACPI specification.
492 */
493 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->XsdtPhysicalAddress;
494 TableEntrySize = ACPI_XSDT_ENTRY_SIZE;
495 }
496 else
497 {
498 /* Root table is an RSDT (32-bit physical addresses) */
499
500 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress;
501 TableEntrySize = ACPI_RSDT_ENTRY_SIZE;
502 }
503
504 /*
505 * It is not possible to map more than one entry in some environments,
506 * so unmap the RSDP here before mapping other tables
507 */
508 AcpiOsUnmapMemory (Rsdp, sizeof (ACPI_TABLE_RSDP));
509
510 /*
511 * If it is present and used, validate the XSDT for access/size
512 * and ensure that all table entries are at least non-NULL
513 */
514 if (TableEntrySize == ACPI_XSDT_ENTRY_SIZE)
515 {
516 Status = AcpiTbValidateXsdt (Address);
517 if (ACPI_FAILURE (Status))
518 {
519 ACPI_BIOS_WARNING ((AE_INFO, "XSDT is invalid (%s), using RSDT",
520 AcpiFormatException (Status)));
521
522 /* Fall back to the RSDT */
523
524 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress;
525 TableEntrySize = ACPI_RSDT_ENTRY_SIZE;
526 }
527 }
528
529 /* Map the RSDT/XSDT table header to get the full table length */
530
531 Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
532 if (!Table)
533 {
534 return_ACPI_STATUS (AE_NO_MEMORY);
535 }
536
537 AcpiTbPrintTableHeader (Address, Table);
538
539 /*
540 * Validate length of the table, and map entire table.
541 * Minimum length table must contain at least one entry.
542 */
543 Length = Table->Length;
544 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));
545
546 if (Length < (sizeof (ACPI_TABLE_HEADER) + TableEntrySize))
547 {
548 ACPI_BIOS_ERROR ((AE_INFO,
549 "Invalid table length 0x%X in RSDT/XSDT", Length));
550 return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
551 }
552
553 Table = AcpiOsMapMemory (Address, Length);
554 if (!Table)
555 {
556 return_ACPI_STATUS (AE_NO_MEMORY);
557 }
558
559 /* Validate the root table checksum */
560
561 Status = AcpiTbVerifyChecksum (Table, Length);
562 if (ACPI_FAILURE (Status))
563 {
564 AcpiOsUnmapMemory (Table, Length);
565 return_ACPI_STATUS (Status);
566 }
567
568 /* Get the number of entries and pointer to first entry */
569
570 TableCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) /
571 TableEntrySize);
572 TableEntry = ACPI_ADD_PTR (UINT8, Table, sizeof (ACPI_TABLE_HEADER));
573
574 /*
575 * First two entries in the table array are reserved for the DSDT
576 * and FACS, which are not actually present in the RSDT/XSDT - they
577 * come from the FADT
578 */
579 AcpiGbl_RootTableList.CurrentTableCount = 2;
580
581 /* Initialize the root table array from the RSDT/XSDT */
582
583 for (i = 0; i < TableCount; i++)
584 {
585 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
586
587 Status = AcpiTbInstallStandardTable (
588 AcpiTbGetRootTableEntry (TableEntry, TableEntrySize),
589 ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, FALSE, TRUE, &TableIndex);
590
591 if (ACPI_SUCCESS (Status) &&
592 ACPI_COMPARE_NAME (&AcpiGbl_RootTableList.Tables[TableIndex].Signature,
593 ACPI_SIG_FADT))
594 {
595 AcpiTbParseFadt (TableIndex);
596 }
597
598 TableEntry += TableEntrySize;
599 }
600
601 /*
602 * It is not possible to map more than one entry in some environments,
603 * so unmap the root table here before mapping other tables
604 */
605 AcpiOsUnmapMemory (Table, Length);
606
607 return_ACPI_STATUS (AE_OK);
608 }