[ACPICA]
[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 #include "acpi.h"
117 #include "accommon.h"
118 #include "actables.h"
119
120 #define _COMPONENT ACPI_TABLES
121 ACPI_MODULE_NAME ("tbutils")
122
123
124 /* Local prototypes */
125
126 static ACPI_PHYSICAL_ADDRESS
127 AcpiTbGetRootTableEntry (
128 UINT8 *TableEntry,
129 UINT32 TableEntrySize);
130
131
132 #if (!ACPI_REDUCED_HARDWARE)
133 /*******************************************************************************
134 *
135 * FUNCTION: AcpiTbInitializeFacs
136 *
137 * PARAMETERS: None
138 *
139 * RETURN: Status
140 *
141 * DESCRIPTION: Create a permanent mapping for the FADT and save it in a global
142 * for accessing the Global Lock and Firmware Waking Vector
143 *
144 ******************************************************************************/
145
146 ACPI_STATUS
147 AcpiTbInitializeFacs (
148 void)
149 {
150 ACPI_STATUS Status;
151
152
153 /* If Hardware Reduced flag is set, there is no FACS */
154
155 if (AcpiGbl_ReducedHardware)
156 {
157 AcpiGbl_FACS = NULL;
158 return (AE_OK);
159 }
160
161 Status = AcpiGetTableByIndex (ACPI_TABLE_INDEX_FACS,
162 ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &AcpiGbl_FACS));
163 return (Status);
164 }
165 #endif /* !ACPI_REDUCED_HARDWARE */
166
167
168 /*******************************************************************************
169 *
170 * FUNCTION: AcpiTbTablesLoaded
171 *
172 * PARAMETERS: None
173 *
174 * RETURN: TRUE if required ACPI tables are loaded
175 *
176 * DESCRIPTION: Determine if the minimum required ACPI tables are present
177 * (FADT, FACS, DSDT)
178 *
179 ******************************************************************************/
180
181 BOOLEAN
182 AcpiTbTablesLoaded (
183 void)
184 {
185
186 if (AcpiGbl_RootTableList.CurrentTableCount >= 3)
187 {
188 return (TRUE);
189 }
190
191 return (FALSE);
192 }
193
194
195 /*******************************************************************************
196 *
197 * FUNCTION: AcpiTbCheckDsdtHeader
198 *
199 * PARAMETERS: None
200 *
201 * RETURN: None
202 *
203 * DESCRIPTION: Quick compare to check validity of the DSDT. This will detect
204 * if the DSDT has been replaced from outside the OS and/or if
205 * the DSDT header has been corrupted.
206 *
207 ******************************************************************************/
208
209 void
210 AcpiTbCheckDsdtHeader (
211 void)
212 {
213
214 /* Compare original length and checksum to current values */
215
216 if (AcpiGbl_OriginalDsdtHeader.Length != AcpiGbl_DSDT->Length ||
217 AcpiGbl_OriginalDsdtHeader.Checksum != AcpiGbl_DSDT->Checksum)
218 {
219 ACPI_BIOS_ERROR ((AE_INFO,
220 "The DSDT has been corrupted or replaced - "
221 "old, new headers below"));
222 AcpiTbPrintTableHeader (0, &AcpiGbl_OriginalDsdtHeader);
223 AcpiTbPrintTableHeader (0, AcpiGbl_DSDT);
224
225 /* Disable further error messages */
226
227 AcpiGbl_OriginalDsdtHeader.Length = AcpiGbl_DSDT->Length;
228 AcpiGbl_OriginalDsdtHeader.Checksum = AcpiGbl_DSDT->Checksum;
229 }
230 }
231
232
233 /*******************************************************************************
234 *
235 * FUNCTION: AcpiTbCopyDsdt
236 *
237 * PARAMETERS: TableDesc - Installed table to copy
238 *
239 * RETURN: None
240 *
241 * DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.
242 * Some very bad BIOSs are known to either corrupt the DSDT or
243 * install a new, bad DSDT. This copy works around the problem.
244 *
245 ******************************************************************************/
246
247 ACPI_TABLE_HEADER *
248 AcpiTbCopyDsdt (
249 UINT32 TableIndex)
250 {
251 ACPI_TABLE_HEADER *NewTable;
252 ACPI_TABLE_DESC *TableDesc;
253
254
255 TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex];
256
257 NewTable = ACPI_ALLOCATE (TableDesc->Length);
258 if (!NewTable)
259 {
260 ACPI_ERROR ((AE_INFO, "Could not copy DSDT of length 0x%X",
261 TableDesc->Length));
262 return (NULL);
263 }
264
265 ACPI_MEMCPY (NewTable, TableDesc->Pointer, TableDesc->Length);
266 AcpiTbUninstallTable (TableDesc);
267
268 AcpiTbInitTableDescriptor (
269 &AcpiGbl_RootTableList.Tables[ACPI_TABLE_INDEX_DSDT],
270 ACPI_PTR_TO_PHYSADDR (NewTable), ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
271 NewTable);
272
273 ACPI_INFO ((AE_INFO,
274 "Forced DSDT copy: length 0x%05X copied locally, original unmapped",
275 NewTable->Length));
276
277 return (NewTable);
278 }
279
280
281 /*******************************************************************************
282 *
283 * FUNCTION: AcpiTbGetRootTableEntry
284 *
285 * PARAMETERS: TableEntry - Pointer to the RSDT/XSDT table entry
286 * TableEntrySize - sizeof 32 or 64 (RSDT or XSDT)
287 *
288 * RETURN: Physical address extracted from the root table
289 *
290 * DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on
291 * both 32-bit and 64-bit platforms
292 *
293 * NOTE: ACPI_PHYSICAL_ADDRESS is 32-bit on 32-bit platforms, 64-bit on
294 * 64-bit platforms.
295 *
296 ******************************************************************************/
297
298 static ACPI_PHYSICAL_ADDRESS
299 AcpiTbGetRootTableEntry (
300 UINT8 *TableEntry,
301 UINT32 TableEntrySize)
302 {
303 UINT64 Address64;
304
305
306 /*
307 * Get the table physical address (32-bit for RSDT, 64-bit for XSDT):
308 * Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT
309 */
310 if (TableEntrySize == ACPI_RSDT_ENTRY_SIZE)
311 {
312 /*
313 * 32-bit platform, RSDT: Return 32-bit table entry
314 * 64-bit platform, RSDT: Expand 32-bit to 64-bit and return
315 */
316 return ((ACPI_PHYSICAL_ADDRESS) (*ACPI_CAST_PTR (UINT32, TableEntry)));
317 }
318 else
319 {
320 /*
321 * 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return
322 * 64-bit platform, XSDT: Move (unaligned) 64-bit to local,
323 * return 64-bit
324 */
325 ACPI_MOVE_64_TO_64 (&Address64, TableEntry);
326
327 #if ACPI_MACHINE_WIDTH == 32
328 if (Address64 > ACPI_UINT32_MAX)
329 {
330 /* Will truncate 64-bit address to 32 bits, issue warning */
331
332 ACPI_BIOS_WARNING ((AE_INFO,
333 "64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"
334 " truncating",
335 ACPI_FORMAT_UINT64 (Address64)));
336 }
337 #endif
338 return ((ACPI_PHYSICAL_ADDRESS) (Address64));
339 }
340 }
341
342
343 /*******************************************************************************
344 *
345 * FUNCTION: AcpiTbParseRootTable
346 *
347 * PARAMETERS: Rsdp - Pointer to the RSDP
348 *
349 * RETURN: Status
350 *
351 * DESCRIPTION: This function is called to parse the Root System Description
352 * Table (RSDT or XSDT)
353 *
354 * NOTE: Tables are mapped (not copied) for efficiency. The FACS must
355 * be mapped and cannot be copied because it contains the actual
356 * memory location of the ACPI Global Lock.
357 *
358 ******************************************************************************/
359
360 ACPI_STATUS
361 AcpiTbParseRootTable (
362 ACPI_PHYSICAL_ADDRESS RsdpAddress)
363 {
364 ACPI_TABLE_RSDP *Rsdp;
365 UINT32 TableEntrySize;
366 UINT32 i;
367 UINT32 TableCount;
368 ACPI_TABLE_HEADER *Table;
369 ACPI_PHYSICAL_ADDRESS Address;
370 UINT32 Length;
371 UINT8 *TableEntry;
372 ACPI_STATUS Status;
373 UINT32 TableIndex;
374
375
376 ACPI_FUNCTION_TRACE (TbParseRootTable);
377
378
379 /* Map the entire RSDP and extract the address of the RSDT or XSDT */
380
381 Rsdp = AcpiOsMapMemory (RsdpAddress, sizeof (ACPI_TABLE_RSDP));
382 if (!Rsdp)
383 {
384 return_ACPI_STATUS (AE_NO_MEMORY);
385 }
386
387 AcpiTbPrintTableHeader (RsdpAddress,
388 ACPI_CAST_PTR (ACPI_TABLE_HEADER, Rsdp));
389
390 /* Use XSDT if present and not overridden. Otherwise, use RSDT */
391
392 if ((Rsdp->Revision > 1) &&
393 Rsdp->XsdtPhysicalAddress &&
394 !AcpiGbl_DoNotUseXsdt)
395 {
396 /*
397 * RSDP contains an XSDT (64-bit physical addresses). We must use
398 * the XSDT if the revision is > 1 and the XSDT pointer is present,
399 * as per the ACPI specification.
400 */
401 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->XsdtPhysicalAddress;
402 TableEntrySize = ACPI_XSDT_ENTRY_SIZE;
403 }
404 else
405 {
406 /* Root table is an RSDT (32-bit physical addresses) */
407
408 Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress;
409 TableEntrySize = ACPI_RSDT_ENTRY_SIZE;
410 }
411
412 /*
413 * It is not possible to map more than one entry in some environments,
414 * so unmap the RSDP here before mapping other tables
415 */
416 AcpiOsUnmapMemory (Rsdp, sizeof (ACPI_TABLE_RSDP));
417
418 /* Map the RSDT/XSDT table header to get the full table length */
419
420 Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));
421 if (!Table)
422 {
423 return_ACPI_STATUS (AE_NO_MEMORY);
424 }
425
426 AcpiTbPrintTableHeader (Address, Table);
427
428 /*
429 * Validate length of the table, and map entire table.
430 * Minimum length table must contain at least one entry.
431 */
432 Length = Table->Length;
433 AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));
434
435 if (Length < (sizeof (ACPI_TABLE_HEADER) + TableEntrySize))
436 {
437 ACPI_BIOS_ERROR ((AE_INFO,
438 "Invalid table length 0x%X in RSDT/XSDT", Length));
439 return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
440 }
441
442 Table = AcpiOsMapMemory (Address, Length);
443 if (!Table)
444 {
445 return_ACPI_STATUS (AE_NO_MEMORY);
446 }
447
448 /* Validate the root table checksum */
449
450 Status = AcpiTbVerifyChecksum (Table, Length);
451 if (ACPI_FAILURE (Status))
452 {
453 AcpiOsUnmapMemory (Table, Length);
454 return_ACPI_STATUS (Status);
455 }
456
457 /* Get the number of entries and pointer to first entry */
458
459 TableCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) /
460 TableEntrySize);
461 TableEntry = ACPI_ADD_PTR (UINT8, Table, sizeof (ACPI_TABLE_HEADER));
462
463 /*
464 * First two entries in the table array are reserved for the DSDT
465 * and FACS, which are not actually present in the RSDT/XSDT - they
466 * come from the FADT
467 */
468 AcpiGbl_RootTableList.CurrentTableCount = 2;
469
470 /* Initialize the root table array from the RSDT/XSDT */
471
472 for (i = 0; i < TableCount; i++)
473 {
474 /* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */
475
476 Address = AcpiTbGetRootTableEntry (TableEntry, TableEntrySize);
477
478 /* Skip NULL entries in RSDT/XSDT */
479
480 if (!Address)
481 {
482 goto NextTable;
483 }
484
485 Status = AcpiTbInstallStandardTable (Address,
486 ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, FALSE, TRUE, &TableIndex);
487
488 if (ACPI_SUCCESS (Status) &&
489 ACPI_COMPARE_NAME (&AcpiGbl_RootTableList.Tables[TableIndex].Signature,
490 ACPI_SIG_FADT))
491 {
492 AcpiTbParseFadt (TableIndex);
493 }
494
495 NextTable:
496
497 TableEntry += TableEntrySize;
498 }
499
500 AcpiOsUnmapMemory (Table, Length);
501
502 return_ACPI_STATUS (AE_OK);
503 }