xref: /freebsd-src/sys/contrib/dev/acpica/components/tables/tbxfroot.c (revision a98ff317388a00b992f1bf8404dee596f9383f5e)
1 /******************************************************************************
2  *
3  * Module Name: tbxfroot - Find the root ACPI table (RSDT)
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, 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 #define __TBXFROOT_C__
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/actables.h>
49 
50 
51 #define _COMPONENT          ACPI_TABLES
52         ACPI_MODULE_NAME    ("tbxfroot")
53 
54 
55 /*******************************************************************************
56  *
57  * FUNCTION:    AcpiTbValidateRsdp
58  *
59  * PARAMETERS:  Rsdp                - Pointer to unvalidated RSDP
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Validate the RSDP (ptr)
64  *
65  ******************************************************************************/
66 
67 ACPI_STATUS
68 AcpiTbValidateRsdp (
69     ACPI_TABLE_RSDP         *Rsdp)
70 {
71 
72     /*
73      * The signature and checksum must both be correct
74      *
75      * Note: Sometimes there exists more than one RSDP in memory; the valid
76      * RSDP has a valid checksum, all others have an invalid checksum.
77      */
78     if (ACPI_STRNCMP ((char *) Rsdp->Signature, ACPI_SIG_RSDP,
79             sizeof (ACPI_SIG_RSDP)-1) != 0)
80     {
81         /* Nope, BAD Signature */
82 
83         return (AE_BAD_SIGNATURE);
84     }
85 
86     /* Check the standard checksum */
87 
88     if (AcpiTbChecksum ((UINT8 *) Rsdp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
89     {
90         return (AE_BAD_CHECKSUM);
91     }
92 
93     /* Check extended checksum if table version >= 2 */
94 
95     if ((Rsdp->Revision >= 2) &&
96         (AcpiTbChecksum ((UINT8 *) Rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0))
97     {
98         return (AE_BAD_CHECKSUM);
99     }
100 
101     return (AE_OK);
102 }
103 
104 
105 /*******************************************************************************
106  *
107  * FUNCTION:    AcpiFindRootPointer
108  *
109  * PARAMETERS:  TableAddress            - Where the table pointer is returned
110  *
111  * RETURN:      Status, RSDP physical address
112  *
113  * DESCRIPTION: Search lower 1Mbyte of memory for the root system descriptor
114  *              pointer structure. If it is found, set *RSDP to point to it.
115  *
116  * NOTE1:       The RSDP must be either in the first 1K of the Extended
117  *              BIOS Data Area or between E0000 and FFFFF (From ACPI Spec.)
118  *              Only a 32-bit physical address is necessary.
119  *
120  * NOTE2:       This function is always available, regardless of the
121  *              initialization state of the rest of ACPI.
122  *
123  ******************************************************************************/
124 
125 ACPI_STATUS
126 AcpiFindRootPointer (
127     ACPI_SIZE               *TableAddress)
128 {
129     UINT8                   *TablePtr;
130     UINT8                   *MemRover;
131     UINT32                  PhysicalAddress;
132 
133 
134     ACPI_FUNCTION_TRACE (AcpiFindRootPointer);
135 
136 
137     /* 1a) Get the location of the Extended BIOS Data Area (EBDA) */
138 
139     TablePtr = AcpiOsMapMemory (
140                 (ACPI_PHYSICAL_ADDRESS) ACPI_EBDA_PTR_LOCATION,
141                 ACPI_EBDA_PTR_LENGTH);
142     if (!TablePtr)
143     {
144         ACPI_ERROR ((AE_INFO,
145             "Could not map memory at 0x%8.8X for length %u",
146             ACPI_EBDA_PTR_LOCATION, ACPI_EBDA_PTR_LENGTH));
147 
148         return_ACPI_STATUS (AE_NO_MEMORY);
149     }
150 
151     ACPI_MOVE_16_TO_32 (&PhysicalAddress, TablePtr);
152 
153     /* Convert segment part to physical address */
154 
155     PhysicalAddress <<= 4;
156     AcpiOsUnmapMemory (TablePtr, ACPI_EBDA_PTR_LENGTH);
157 
158     /* EBDA present? */
159 
160     if (PhysicalAddress > 0x400)
161     {
162         /*
163          * 1b) Search EBDA paragraphs (EBDA is required to be a
164          *     minimum of 1K length)
165          */
166         TablePtr = AcpiOsMapMemory (
167                     (ACPI_PHYSICAL_ADDRESS) PhysicalAddress,
168                     ACPI_EBDA_WINDOW_SIZE);
169         if (!TablePtr)
170         {
171             ACPI_ERROR ((AE_INFO,
172                 "Could not map memory at 0x%8.8X for length %u",
173                 PhysicalAddress, ACPI_EBDA_WINDOW_SIZE));
174 
175             return_ACPI_STATUS (AE_NO_MEMORY);
176         }
177 
178         MemRover = AcpiTbScanMemoryForRsdp (TablePtr, ACPI_EBDA_WINDOW_SIZE);
179         AcpiOsUnmapMemory (TablePtr, ACPI_EBDA_WINDOW_SIZE);
180 
181         if (MemRover)
182         {
183             /* Return the physical address */
184 
185             PhysicalAddress += (UINT32) ACPI_PTR_DIFF (MemRover, TablePtr);
186 
187             *TableAddress = PhysicalAddress;
188             return_ACPI_STATUS (AE_OK);
189         }
190     }
191 
192     /*
193      * 2) Search upper memory: 16-byte boundaries in E0000h-FFFFFh
194      */
195     TablePtr = AcpiOsMapMemory (
196                 (ACPI_PHYSICAL_ADDRESS) ACPI_HI_RSDP_WINDOW_BASE,
197                 ACPI_HI_RSDP_WINDOW_SIZE);
198 
199     if (!TablePtr)
200     {
201         ACPI_ERROR ((AE_INFO,
202             "Could not map memory at 0x%8.8X for length %u",
203             ACPI_HI_RSDP_WINDOW_BASE, ACPI_HI_RSDP_WINDOW_SIZE));
204 
205         return_ACPI_STATUS (AE_NO_MEMORY);
206     }
207 
208     MemRover = AcpiTbScanMemoryForRsdp (TablePtr, ACPI_HI_RSDP_WINDOW_SIZE);
209     AcpiOsUnmapMemory (TablePtr, ACPI_HI_RSDP_WINDOW_SIZE);
210 
211     if (MemRover)
212     {
213         /* Return the physical address */
214 
215         PhysicalAddress = (UINT32)
216             (ACPI_HI_RSDP_WINDOW_BASE + ACPI_PTR_DIFF (MemRover, TablePtr));
217 
218         *TableAddress = PhysicalAddress;
219         return_ACPI_STATUS (AE_OK);
220     }
221 
222     /* A valid RSDP was not found */
223 
224     ACPI_BIOS_ERROR ((AE_INFO, "A valid RSDP was not found"));
225     return_ACPI_STATUS (AE_NOT_FOUND);
226 }
227 
228 ACPI_EXPORT_SYMBOL (AcpiFindRootPointer)
229 
230 
231 /*******************************************************************************
232  *
233  * FUNCTION:    AcpiTbScanMemoryForRsdp
234  *
235  * PARAMETERS:  StartAddress        - Starting pointer for search
236  *              Length              - Maximum length to search
237  *
238  * RETURN:      Pointer to the RSDP if found, otherwise NULL.
239  *
240  * DESCRIPTION: Search a block of memory for the RSDP signature
241  *
242  ******************************************************************************/
243 
244 UINT8 *
245 AcpiTbScanMemoryForRsdp (
246     UINT8                   *StartAddress,
247     UINT32                  Length)
248 {
249     ACPI_STATUS             Status;
250     UINT8                   *MemRover;
251     UINT8                   *EndAddress;
252 
253 
254     ACPI_FUNCTION_TRACE (TbScanMemoryForRsdp);
255 
256 
257     EndAddress = StartAddress + Length;
258 
259     /* Search from given start address for the requested length */
260 
261     for (MemRover = StartAddress; MemRover < EndAddress;
262          MemRover += ACPI_RSDP_SCAN_STEP)
263     {
264         /* The RSDP signature and checksum must both be correct */
265 
266         Status = AcpiTbValidateRsdp (ACPI_CAST_PTR (ACPI_TABLE_RSDP, MemRover));
267         if (ACPI_SUCCESS (Status))
268         {
269             /* Sig and checksum valid, we have found a real RSDP */
270 
271             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
272                 "RSDP located at physical address %p\n", MemRover));
273             return_PTR (MemRover);
274         }
275 
276         /* No sig match or bad checksum, keep searching */
277     }
278 
279     /* Searched entire block, no RSDP was found */
280 
281     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
282         "Searched entire block from %p, valid RSDP was not found\n",
283         StartAddress));
284     return_PTR (NULL);
285 }
286