Sync ACPICA with Intel's version 20180508 (from previously 20170831).
[dragonfly.git] / sys / contrib / dev / acpica / source / os_specific / service_layers / osbsdtbl.c
blob18a07c9f854d141ec1a459919c27f41c1bdf75eb
1 /******************************************************************************
3 * Module Name: osbsdtbl - BSD OSL for obtaining ACPI tables
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
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.
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
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;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
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.
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.
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.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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.
80 * 4. Disclaimer and Export Compliance
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.
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.
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.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
121 * are met:
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include "acpidump.h"
154 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__)
155 #include <kenv.h>
156 #endif
157 #include <unistd.h>
158 #include <sys/param.h>
159 #include <sys/sysctl.h>
162 #define _COMPONENT ACPI_OS_SERVICES
163 ACPI_MODULE_NAME ("osbsdtbl")
166 /* Local prototypes */
168 static ACPI_STATUS
169 OslTableInitialize (
170 void);
172 static ACPI_STATUS
173 OslMapTable (
174 ACPI_SIZE Address,
175 char *Signature,
176 ACPI_TABLE_HEADER **Table);
178 static ACPI_STATUS
179 OslAddTablesToList (
180 void);
182 static ACPI_STATUS
183 OslGetTableViaRoot (
184 char *Signature,
185 UINT32 Instance,
186 ACPI_TABLE_HEADER **Table,
187 ACPI_PHYSICAL_ADDRESS *Address);
190 /* Hints for RSDP */
191 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
192 #define SYSTEM_KENV "hint.acpi.0.rsdp"
193 #define SYSTEM_SYSCTL "machdep.acpi_root"
194 #elif defined(__NetBSD__)
195 #define SYSTEM_SYSCTL "hw.acpi.root"
196 #endif
198 /* Initialization flags */
200 UINT8 Gbl_TableListInitialized = FALSE;
201 UINT8 Gbl_MainTableObtained = FALSE;
203 /* Local copies of main ACPI tables */
205 ACPI_TABLE_RSDP Gbl_Rsdp;
206 ACPI_TABLE_FADT *Gbl_Fadt;
207 ACPI_TABLE_RSDT *Gbl_Rsdt;
208 ACPI_TABLE_XSDT *Gbl_Xsdt;
210 /* Fadt address */
212 ACPI_PHYSICAL_ADDRESS Gbl_FadtAddress;
214 /* Revision of RSD PTR */
216 UINT8 Gbl_Revision;
218 /* List of information about obtained ACPI tables */
220 typedef struct table_info
222 struct table_info *Next;
223 char Signature[4];
224 UINT32 Instance;
225 ACPI_PHYSICAL_ADDRESS Address;
227 } OSL_TABLE_INFO;
229 OSL_TABLE_INFO *Gbl_TableListHead = NULL;
232 /******************************************************************************
234 * FUNCTION: AcpiOsGetTableByAddress
236 * PARAMETERS: Address - Physical address of the ACPI table
237 * Table - Where a pointer to the table is returned
239 * RETURN: Status; Table buffer is returned if AE_OK.
240 * AE_NOT_FOUND: A valid table was not found at the address
242 * DESCRIPTION: Get an ACPI table via a physical memory address.
244 *****************************************************************************/
246 ACPI_STATUS
247 AcpiOsGetTableByAddress (
248 ACPI_PHYSICAL_ADDRESS Address,
249 ACPI_TABLE_HEADER **Table)
251 ACPI_TABLE_HEADER *MappedTable;
252 ACPI_TABLE_HEADER *LocalTable;
253 ACPI_STATUS Status;
256 /* Validate the input physical address to avoid program crash */
258 if (Address < ACPI_HI_RSDP_WINDOW_BASE)
260 fprintf (stderr, "Invalid table address: 0x%8.8X%8.8X\n",
261 ACPI_FORMAT_UINT64 (Address));
262 return (AE_BAD_ADDRESS);
265 /* Map the table and validate it */
267 Status = OslMapTable (Address, NULL, &MappedTable);
268 if (ACPI_FAILURE (Status))
270 return (Status);
273 /* Copy table to local buffer and return it */
275 LocalTable = calloc (1, MappedTable->Length);
276 if (!LocalTable)
278 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
279 return (AE_NO_MEMORY);
282 memcpy (LocalTable, MappedTable, MappedTable->Length);
283 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
285 *Table = LocalTable;
286 return (AE_OK);
290 /******************************************************************************
292 * FUNCTION: AcpiOsGetTableByName
294 * PARAMETERS: Signature - ACPI Signature for desired table. Must be
295 * a null terminated 4-character string.
296 * Instance - Multiple table support for SSDT/UEFI (0...n)
297 * Must be 0 for other tables.
298 * Table - Where a pointer to the table is returned
299 * Address - Where the table physical address is returned
301 * RETURN: Status; Table buffer and physical address returned if AE_OK.
302 * AE_LIMIT: Instance is beyond valid limit
303 * AE_NOT_FOUND: A table with the signature was not found
305 * NOTE: Assumes the input signature is uppercase.
307 *****************************************************************************/
309 ACPI_STATUS
310 AcpiOsGetTableByName (
311 char *Signature,
312 UINT32 Instance,
313 ACPI_TABLE_HEADER **Table,
314 ACPI_PHYSICAL_ADDRESS *Address)
316 ACPI_STATUS Status;
319 /* Instance is only valid for SSDT/UEFI tables */
321 if (Instance &&
322 !ACPI_COMPARE_NAME (Signature, ACPI_SIG_SSDT) &&
323 !ACPI_COMPARE_NAME (Signature, ACPI_SIG_UEFI))
325 return (AE_LIMIT);
328 /* Initialize main tables */
330 Status = OslTableInitialize ();
331 if (ACPI_FAILURE (Status))
333 return (Status);
337 * If one of the main ACPI tables was requested (RSDT/XSDT/FADT),
338 * simply return it immediately.
340 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_XSDT))
342 if (!Gbl_Revision)
344 return (AE_NOT_FOUND);
347 *Address = Gbl_Rsdp.XsdtPhysicalAddress;
348 *Table = (ACPI_TABLE_HEADER *) Gbl_Xsdt;
349 return (AE_OK);
352 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_RSDT))
354 if (!Gbl_Rsdp.RsdtPhysicalAddress)
356 return (AE_NOT_FOUND);
359 *Address = Gbl_Rsdp.RsdtPhysicalAddress;
360 *Table = (ACPI_TABLE_HEADER *) Gbl_Rsdt;
361 return (AE_OK);
364 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FADT))
366 *Address = Gbl_FadtAddress;
367 *Table = (ACPI_TABLE_HEADER *) Gbl_Fadt;
368 return (AE_OK);
371 /* Not a main ACPI table, attempt to extract it from the RSDT/XSDT */
373 Status = OslGetTableViaRoot (Signature, Instance, Table, Address);
374 if (ACPI_FAILURE (Status))
376 return (Status);
379 return (AE_OK);
383 /******************************************************************************
385 * FUNCTION: AcpiOsGetTableByIndex
387 * PARAMETERS: Index - Which table to get
388 * Table - Where a pointer to the table is returned
389 * Instance - Where a pointer to the table instance no. is
390 * returned
391 * Address - Where the table physical address is returned
393 * RETURN: Status; Table buffer and physical address returned if AE_OK.
394 * AE_LIMIT: Index is beyond valid limit
396 * DESCRIPTION: Get an ACPI table via an index value (0 through n). Returns
397 * AE_LIMIT when an invalid index is reached. Index is not
398 * necessarily an index into the RSDT/XSDT.
400 *****************************************************************************/
402 ACPI_STATUS
403 AcpiOsGetTableByIndex (
404 UINT32 Index,
405 ACPI_TABLE_HEADER **Table,
406 UINT32 *Instance,
407 ACPI_PHYSICAL_ADDRESS *Address)
409 OSL_TABLE_INFO *Info;
410 ACPI_STATUS Status;
411 UINT32 i;
414 /* Initialize main tables */
416 Status = OslTableInitialize ();
417 if (ACPI_FAILURE (Status))
419 return (Status);
422 /* Add all tables to list */
424 Status = OslAddTablesToList ();
425 if (ACPI_FAILURE (Status))
427 return (Status);
430 /* Validate Index */
432 if (Index >= Gbl_TableListHead->Instance)
434 return (AE_LIMIT);
437 /* Point to the table list entry specified by the Index argument */
439 Info = Gbl_TableListHead;
440 for (i = 0; i <= Index; i++)
442 Info = Info->Next;
445 /* Now we can just get the table via the address or name */
447 if (Info->Address)
449 Status = AcpiOsGetTableByAddress (Info->Address, Table);
450 if (ACPI_SUCCESS (Status))
452 *Address = Info->Address;
455 else
457 Status = AcpiOsGetTableByName (Info->Signature, Info->Instance,
458 Table, Address);
461 if (ACPI_SUCCESS (Status))
463 *Instance = Info->Instance;
465 return (Status);
469 /******************************************************************************
471 * FUNCTION: OslTableInitialize
473 * PARAMETERS: None
475 * RETURN: Status
477 * DESCRIPTION: Initialize ACPI table data. Get and store main ACPI tables to
478 * local variables. Main ACPI tables include RSDP, FADT, RSDT,
479 * and/or XSDT.
481 *****************************************************************************/
483 static ACPI_STATUS
484 OslTableInitialize (
485 void)
487 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
488 char Buffer[32];
489 #endif
490 ACPI_TABLE_HEADER *MappedTable;
491 UINT8 *TableAddress;
492 UINT8 *RsdpAddress;
493 ACPI_PHYSICAL_ADDRESS RsdpBase;
494 ACPI_SIZE RsdpSize;
495 ACPI_STATUS Status;
496 u_long Address = 0;
497 #if defined(SYSTEM_SYSCTL)
498 size_t Length = sizeof (Address);
499 #endif
502 /* Get main ACPI tables from memory on first invocation of this function */
504 if (Gbl_MainTableObtained)
506 return (AE_OK);
509 /* Attempt to use kenv or sysctl to find RSD PTR record. */
511 if (Gbl_RsdpBase)
513 Address = Gbl_RsdpBase;
515 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined (__DragonFly__)
516 else if (kenv (KENV_GET, SYSTEM_KENV, Buffer, sizeof (Buffer)) > 0)
518 Address = strtoul (Buffer, NULL, 0);
520 #endif
521 #if defined(SYSTEM_SYSCTL)
522 if (!Address)
524 if (sysctlbyname (SYSTEM_SYSCTL, &Address, &Length, NULL, 0) != 0)
526 Address = 0;
529 #endif
530 if (Address)
532 RsdpBase = Address;
533 RsdpSize = sizeof (Gbl_Rsdp);
535 else
537 RsdpBase = ACPI_HI_RSDP_WINDOW_BASE;
538 RsdpSize = ACPI_HI_RSDP_WINDOW_SIZE;
541 /* Get RSDP from memory */
543 RsdpAddress = AcpiOsMapMemory (RsdpBase, RsdpSize);
544 if (!RsdpAddress)
546 return (AE_BAD_ADDRESS);
549 /* Search low memory for the RSDP */
551 TableAddress = AcpiTbScanMemoryForRsdp (RsdpAddress, RsdpSize);
552 if (!TableAddress)
554 AcpiOsUnmapMemory (RsdpAddress, RsdpSize);
555 return (AE_ERROR);
558 memcpy (&Gbl_Rsdp, TableAddress, sizeof (Gbl_Rsdp));
559 AcpiOsUnmapMemory (RsdpAddress, RsdpSize);
561 /* Get XSDT from memory */
563 if (Gbl_Rsdp.Revision)
565 Status = OslMapTable (Gbl_Rsdp.XsdtPhysicalAddress,
566 ACPI_SIG_XSDT, &MappedTable);
567 if (ACPI_FAILURE (Status))
569 return (Status);
572 Gbl_Revision = 2;
573 Gbl_Xsdt = calloc (1, MappedTable->Length);
574 if (!Gbl_Xsdt)
576 fprintf (stderr,
577 "XSDT: Could not allocate buffer for table of length %X\n",
578 MappedTable->Length);
579 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
580 return (AE_NO_MEMORY);
583 memcpy (Gbl_Xsdt, MappedTable, MappedTable->Length);
584 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
587 /* Get RSDT from memory */
589 if (Gbl_Rsdp.RsdtPhysicalAddress)
591 Status = OslMapTable (Gbl_Rsdp.RsdtPhysicalAddress,
592 ACPI_SIG_RSDT, &MappedTable);
593 if (ACPI_FAILURE (Status))
595 return (Status);
598 Gbl_Rsdt = calloc (1, MappedTable->Length);
599 if (!Gbl_Rsdt)
601 fprintf (stderr,
602 "RSDT: Could not allocate buffer for table of length %X\n",
603 MappedTable->Length);
604 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
605 return (AE_NO_MEMORY);
608 memcpy (Gbl_Rsdt, MappedTable, MappedTable->Length);
609 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
612 /* Get FADT from memory */
614 if (Gbl_Revision)
616 Gbl_FadtAddress = Gbl_Xsdt->TableOffsetEntry[0];
618 else
620 Gbl_FadtAddress = Gbl_Rsdt->TableOffsetEntry[0];
623 if (!Gbl_FadtAddress)
625 fprintf(stderr, "FADT: Table could not be found\n");
626 return (AE_ERROR);
629 Status = OslMapTable (Gbl_FadtAddress, ACPI_SIG_FADT, &MappedTable);
630 if (ACPI_FAILURE (Status))
632 return (Status);
635 Gbl_Fadt = calloc (1, MappedTable->Length);
636 if (!Gbl_Fadt)
638 fprintf (stderr,
639 "FADT: Could not allocate buffer for table of length %X\n",
640 MappedTable->Length);
641 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
642 return (AE_NO_MEMORY);
645 memcpy (Gbl_Fadt, MappedTable, MappedTable->Length);
646 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
647 Gbl_MainTableObtained = TRUE;
648 return (AE_OK);
652 /******************************************************************************
654 * FUNCTION: OslGetTableViaRoot
656 * PARAMETERS: Signature - ACPI Signature for common table. Must be
657 * a null terminated 4-character string.
658 * Instance - Multiple table support for SSDT/UEFI (0...n)
659 * Must be 0 for other tables.
660 * Table - Where a pointer to the table is returned
661 * Address - Where the table physical address is returned
663 * RETURN: Status; Table buffer and physical address returned if AE_OK.
664 * AE_LIMIT: Instance is beyond valid limit
665 * AE_NOT_FOUND: A table with the signature was not found
667 * DESCRIPTION: Get an ACPI table via the root table (RSDT/XSDT)
669 * NOTE: Assumes the input signature is uppercase.
671 *****************************************************************************/
673 static ACPI_STATUS
674 OslGetTableViaRoot (
675 char *Signature,
676 UINT32 Instance,
677 ACPI_TABLE_HEADER **Table,
678 ACPI_PHYSICAL_ADDRESS *Address)
680 ACPI_TABLE_HEADER *LocalTable = NULL;
681 ACPI_TABLE_HEADER *MappedTable = NULL;
682 UINT8 NumberOfTables;
683 UINT32 CurrentInstance = 0;
684 ACPI_PHYSICAL_ADDRESS TableAddress = 0;
685 ACPI_STATUS Status;
686 UINT32 i;
689 /* DSDT and FACS address must be extracted from the FADT */
691 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_DSDT) ||
692 ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
695 * Get the appropriate address, either 32-bit or 64-bit. Be very
696 * careful about the FADT length and validate table addresses.
697 * Note: The 64-bit addresses have priority.
699 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_DSDT))
701 if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_XDSDT) &&
702 Gbl_Fadt->XDsdt)
704 TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->XDsdt;
706 else if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_DSDT) &&
707 Gbl_Fadt->Dsdt)
709 TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->Dsdt;
712 else /* FACS */
714 if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_XFACS) &&
715 Gbl_Fadt->XFacs)
717 TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->XFacs;
719 else if ((Gbl_Fadt->Header.Length >= MIN_FADT_FOR_FACS) &&
720 Gbl_Fadt->Facs)
722 TableAddress = (ACPI_PHYSICAL_ADDRESS) Gbl_Fadt->Facs;
726 else /* Case for a normal ACPI table */
728 if (Gbl_Revision)
730 NumberOfTables =
731 (Gbl_Xsdt->Header.Length - sizeof (Gbl_Xsdt->Header))
732 / sizeof (Gbl_Xsdt->TableOffsetEntry[0]);
734 else /* Use RSDT if XSDT is not available */
736 NumberOfTables =
737 (Gbl_Rsdt->Header.Length - sizeof (Gbl_Rsdt->Header))
738 / sizeof (Gbl_Rsdt->TableOffsetEntry[0]);
741 /* Search RSDT/XSDT for the requested table */
743 for (i = 0; i < NumberOfTables; i++)
745 if (Gbl_Revision)
747 TableAddress = Gbl_Xsdt->TableOffsetEntry[i];
749 else
751 TableAddress = Gbl_Rsdt->TableOffsetEntry[i];
754 MappedTable = AcpiOsMapMemory (TableAddress, sizeof (*MappedTable));
755 if (!MappedTable)
757 return (AE_BAD_ADDRESS);
760 /* Does this table match the requested signature? */
762 if (ACPI_COMPARE_NAME (MappedTable->Signature, Signature))
765 /* Match table instance (for SSDT/UEFI tables) */
767 if (CurrentInstance == Instance)
769 AcpiOsUnmapMemory (MappedTable, sizeof (*MappedTable));
770 break;
773 CurrentInstance++;
776 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
777 TableAddress = 0;
781 if (!TableAddress)
783 if (CurrentInstance)
785 return (AE_LIMIT);
787 return (AE_NOT_FOUND);
790 /* Now we can get the requested table */
792 Status = OslMapTable (TableAddress, Signature, &MappedTable);
793 if (ACPI_FAILURE (Status))
795 return (Status);
798 /* Copy table to local buffer and return it */
800 LocalTable = calloc (1, MappedTable->Length);
801 if (!LocalTable)
803 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
804 return (AE_NO_MEMORY);
807 memcpy (LocalTable, MappedTable, MappedTable->Length);
808 AcpiOsUnmapMemory (MappedTable, MappedTable->Length);
809 *Table = LocalTable;
810 *Address = TableAddress;
811 return (AE_OK);
815 /******************************************************************************
817 * FUNCTION: OslAddTablesToList
819 * PARAMETERS: None
821 * RETURN: Status; Table list is initialized if AE_OK.
823 * DESCRIPTION: Add ACPI tables to the table list.
825 *****************************************************************************/
827 static ACPI_STATUS
828 OslAddTablesToList(
829 void)
831 ACPI_PHYSICAL_ADDRESS TableAddress;
832 OSL_TABLE_INFO *Info = NULL;
833 OSL_TABLE_INFO *NewInfo;
834 ACPI_TABLE_HEADER *Table;
835 UINT8 Instance;
836 UINT8 NumberOfTables;
837 int i;
840 /* Initialize the table list on first invocation */
842 if (Gbl_TableListInitialized)
844 return (AE_OK);
847 /* Add mandatory tables to global table list first */
849 for (i = 0; i < 4; i++)
851 NewInfo = calloc (1, sizeof (*NewInfo));
852 if (!NewInfo)
854 return (AE_NO_MEMORY);
857 switch (i) {
858 case 0:
860 Gbl_TableListHead = Info = NewInfo;
861 continue;
863 case 1:
865 ACPI_MOVE_NAME (NewInfo->Signature,
866 Gbl_Revision ? ACPI_SIG_XSDT : ACPI_SIG_RSDT);
867 break;
869 case 2:
871 ACPI_MOVE_NAME (NewInfo->Signature, ACPI_SIG_FACS);
872 break;
874 default:
876 ACPI_MOVE_NAME (NewInfo->Signature, ACPI_SIG_DSDT);
880 Info->Next = NewInfo;
881 Info = NewInfo;
882 Gbl_TableListHead->Instance++;
885 /* Add normal tables from RSDT/XSDT to global list */
887 if (Gbl_Revision)
889 NumberOfTables =
890 (Gbl_Xsdt->Header.Length - sizeof (Gbl_Xsdt->Header))
891 / sizeof (Gbl_Xsdt->TableOffsetEntry[0]);
893 else
895 NumberOfTables =
896 (Gbl_Rsdt->Header.Length - sizeof (Gbl_Rsdt->Header))
897 / sizeof (Gbl_Rsdt->TableOffsetEntry[0]);
900 for (i = 0; i < NumberOfTables; i++)
902 if (Gbl_Revision)
904 TableAddress = Gbl_Xsdt->TableOffsetEntry[i];
906 else
908 TableAddress = Gbl_Rsdt->TableOffsetEntry[i];
911 Table = AcpiOsMapMemory (TableAddress, sizeof (*Table));
912 if (!Table)
914 return (AE_BAD_ADDRESS);
917 Instance = 0;
918 NewInfo = Gbl_TableListHead;
919 while (NewInfo->Next != NULL)
921 NewInfo = NewInfo->Next;
922 if (ACPI_COMPARE_NAME (Table->Signature, NewInfo->Signature))
924 Instance++;
928 NewInfo = calloc (1, sizeof (*NewInfo));
929 if (!NewInfo)
931 AcpiOsUnmapMemory (Table, sizeof (*Table));
932 return (AE_NO_MEMORY);
935 ACPI_MOVE_NAME (NewInfo->Signature, Table->Signature);
937 AcpiOsUnmapMemory (Table, sizeof (*Table));
939 NewInfo->Instance = Instance;
940 NewInfo->Address = TableAddress;
941 Info->Next = NewInfo;
942 Info = NewInfo;
943 Gbl_TableListHead->Instance++;
946 Gbl_TableListInitialized = TRUE;
947 return (AE_OK);
951 /******************************************************************************
953 * FUNCTION: OslMapTable
955 * PARAMETERS: Address - Address of the table in memory
956 * Signature - Optional ACPI Signature for desired table.
957 * Null terminated 4-character string.
958 * Table - Where a pointer to the mapped table is
959 * returned
961 * RETURN: Status; Mapped table is returned if AE_OK.
963 * DESCRIPTION: Map entire ACPI table into caller's address space. Also
964 * validates the table and checksum.
966 *****************************************************************************/
968 static ACPI_STATUS
969 OslMapTable (
970 ACPI_SIZE Address,
971 char *Signature,
972 ACPI_TABLE_HEADER **Table)
974 ACPI_TABLE_HEADER *MappedTable;
975 UINT32 Length;
978 /* Map the header so we can get the table length */
980 MappedTable = AcpiOsMapMemory (Address, sizeof (*MappedTable));
981 if (!MappedTable)
983 return (AE_BAD_ADDRESS);
986 /* Check if table is valid */
988 if (!ApIsValidHeader (MappedTable))
990 AcpiOsUnmapMemory (MappedTable, sizeof (*MappedTable));
991 return (AE_BAD_HEADER);
994 /* If specified, signature must match */
996 if (Signature &&
997 !ACPI_COMPARE_NAME (Signature, MappedTable->Signature))
999 AcpiOsUnmapMemory (MappedTable, sizeof (*MappedTable));
1000 return (AE_NOT_EXIST);
1003 /* Map the entire table */
1005 Length = MappedTable->Length;
1006 AcpiOsUnmapMemory (MappedTable, sizeof (*MappedTable));
1008 MappedTable = AcpiOsMapMemory (Address, Length);
1009 if (!MappedTable)
1011 return (AE_BAD_ADDRESS);
1014 (void) ApIsValidChecksum (MappedTable);
1016 *Table = MappedTable;
1018 return (AE_OK);