xref: /freebsd/sys/contrib/dev/acpica/components/tables/tbfadt.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /******************************************************************************
2  *
3  * Module Name: tbfadt   - FADT table utilities
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2024, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
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.
133  *
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.
145  *
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.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/actables.h>
155 
156 #define _COMPONENT          ACPI_TABLES
157         ACPI_MODULE_NAME    ("tbfadt")
158 
159 /* Local prototypes */
160 
161 static void
162 AcpiTbInitGenericAddress (
163     ACPI_GENERIC_ADDRESS    *GenericAddress,
164     UINT8                   SpaceId,
165     UINT8                   ByteWidth,
166     UINT64                  Address,
167     const char              *RegisterName,
168     UINT8                   Flags);
169 
170 static void
171 AcpiTbConvertFadt (
172     void);
173 
174 static void
175 AcpiTbSetupFadtRegisters (
176     void);
177 
178 static UINT64
179 AcpiTbSelectAddress (
180     char                    *RegisterName,
181     UINT32                  Address32,
182     UINT64                  Address64);
183 
184 
185 /* Table for conversion of FADT to common internal format and FADT validation */
186 
187 typedef struct acpi_fadt_info
188 {
189     const char              *Name;
190     UINT16                  Address64;
191     UINT16                  Address32;
192     UINT16                  Length;
193     UINT8                   DefaultLength;
194     UINT8                   Flags;
195 
196 } ACPI_FADT_INFO;
197 
198 #define ACPI_FADT_OPTIONAL          0
199 #define ACPI_FADT_REQUIRED          1
200 #define ACPI_FADT_SEPARATE_LENGTH   2
201 #define ACPI_FADT_GPE_REGISTER      4
202 
203 static ACPI_FADT_INFO     FadtInfoTable[] =
204 {
205     {"Pm1aEventBlock",
206         ACPI_FADT_OFFSET (XPm1aEventBlock),
207         ACPI_FADT_OFFSET (Pm1aEventBlock),
208         ACPI_FADT_OFFSET (Pm1EventLength),
209         ACPI_PM1_REGISTER_WIDTH * 2,        /* Enable + Status register */
210         ACPI_FADT_REQUIRED},
211 
212     {"Pm1bEventBlock",
213         ACPI_FADT_OFFSET (XPm1bEventBlock),
214         ACPI_FADT_OFFSET (Pm1bEventBlock),
215         ACPI_FADT_OFFSET (Pm1EventLength),
216         ACPI_PM1_REGISTER_WIDTH * 2,        /* Enable + Status register */
217         ACPI_FADT_OPTIONAL},
218 
219     {"Pm1aControlBlock",
220         ACPI_FADT_OFFSET (XPm1aControlBlock),
221         ACPI_FADT_OFFSET (Pm1aControlBlock),
222         ACPI_FADT_OFFSET (Pm1ControlLength),
223         ACPI_PM1_REGISTER_WIDTH,
224         ACPI_FADT_REQUIRED},
225 
226     {"Pm1bControlBlock",
227         ACPI_FADT_OFFSET (XPm1bControlBlock),
228         ACPI_FADT_OFFSET (Pm1bControlBlock),
229         ACPI_FADT_OFFSET (Pm1ControlLength),
230         ACPI_PM1_REGISTER_WIDTH,
231         ACPI_FADT_OPTIONAL},
232 
233     {"Pm2ControlBlock",
234         ACPI_FADT_OFFSET (XPm2ControlBlock),
235         ACPI_FADT_OFFSET (Pm2ControlBlock),
236         ACPI_FADT_OFFSET (Pm2ControlLength),
237         ACPI_PM2_REGISTER_WIDTH,
238         ACPI_FADT_SEPARATE_LENGTH},
239 
240     {"PmTimerBlock",
241         ACPI_FADT_OFFSET (XPmTimerBlock),
242         ACPI_FADT_OFFSET (PmTimerBlock),
243         ACPI_FADT_OFFSET (PmTimerLength),
244         ACPI_PM_TIMER_WIDTH,
245         ACPI_FADT_SEPARATE_LENGTH},         /* ACPI 5.0A: Timer is optional */
246 
247     {"Gpe0Block",
248         ACPI_FADT_OFFSET (XGpe0Block),
249         ACPI_FADT_OFFSET (Gpe0Block),
250         ACPI_FADT_OFFSET (Gpe0BlockLength),
251         0,
252         ACPI_FADT_SEPARATE_LENGTH | ACPI_FADT_GPE_REGISTER},
253 
254     {"Gpe1Block",
255         ACPI_FADT_OFFSET (XGpe1Block),
256         ACPI_FADT_OFFSET (Gpe1Block),
257         ACPI_FADT_OFFSET (Gpe1BlockLength),
258         0,
259         ACPI_FADT_SEPARATE_LENGTH | ACPI_FADT_GPE_REGISTER}
260 };
261 
262 #define ACPI_FADT_INFO_ENTRIES \
263             (sizeof (FadtInfoTable) / sizeof (ACPI_FADT_INFO))
264 
265 
266 /* Table used to split Event Blocks into separate status/enable registers */
267 
268 typedef struct acpi_fadt_pm_info
269 {
270     ACPI_GENERIC_ADDRESS    *Target;
271     UINT16                  Source;
272     UINT8                   RegisterNum;
273 
274 } ACPI_FADT_PM_INFO;
275 
276 static ACPI_FADT_PM_INFO    FadtPmInfoTable[] =
277 {
278     {&AcpiGbl_XPm1aStatus,
279         ACPI_FADT_OFFSET (XPm1aEventBlock),
280         0},
281 
282     {&AcpiGbl_XPm1aEnable,
283         ACPI_FADT_OFFSET (XPm1aEventBlock),
284         1},
285 
286     {&AcpiGbl_XPm1bStatus,
287         ACPI_FADT_OFFSET (XPm1bEventBlock),
288         0},
289 
290     {&AcpiGbl_XPm1bEnable,
291         ACPI_FADT_OFFSET (XPm1bEventBlock),
292         1}
293 };
294 
295 #define ACPI_FADT_PM_INFO_ENTRIES \
296             (sizeof (FadtPmInfoTable) / sizeof (ACPI_FADT_PM_INFO))
297 
298 
299 /*******************************************************************************
300  *
301  * FUNCTION:    AcpiTbInitGenericAddress
302  *
303  * PARAMETERS:  GenericAddress      - GAS struct to be initialized
304  *              SpaceId             - ACPI Space ID for this register
305  *              ByteWidth           - Width of this register
306  *              Address             - Address of the register
307  *              RegisterName        - ASCII name of the ACPI register
308  *
309  * RETURN:      None
310  *
311  * DESCRIPTION: Initialize a Generic Address Structure (GAS)
312  *              See the ACPI specification for a full description and
313  *              definition of this structure.
314  *
315  ******************************************************************************/
316 
317 static void
318 AcpiTbInitGenericAddress (
319     ACPI_GENERIC_ADDRESS    *GenericAddress,
320     UINT8                   SpaceId,
321     UINT8                   ByteWidth,
322     UINT64                  Address,
323     const char              *RegisterName,
324     UINT8                   Flags)
325 {
326     UINT8                   BitWidth;
327 
328 
329     /*
330      * Bit width field in the GAS is only one byte long, 255 max.
331      * Check for BitWidth overflow in GAS.
332      */
333     BitWidth = (UINT8) (ByteWidth * 8);
334     if (ByteWidth > 31)     /* (31*8)=248, (32*8)=256 */
335     {
336         /*
337          * No error for GPE blocks, because we do not use the BitWidth
338          * for GPEs, the legacy length (ByteWidth) is used instead to
339          * allow for a large number of GPEs.
340          */
341         if (!(Flags & ACPI_FADT_GPE_REGISTER))
342         {
343             ACPI_ERROR ((AE_INFO,
344                 "%s - 32-bit FADT register is too long (%u bytes, %u bits) "
345                 "to convert to GAS struct - 255 bits max, truncating",
346                 RegisterName, ByteWidth, (ByteWidth * 8)));
347         }
348 
349         BitWidth = 255;
350     }
351 
352     /*
353      * The 64-bit Address field is non-aligned in the byte packed
354      * GAS struct.
355      */
356     ACPI_MOVE_64_TO_64 (&GenericAddress->Address, &Address);
357 
358     /* All other fields are byte-wide */
359 
360     GenericAddress->SpaceId = SpaceId;
361     GenericAddress->BitWidth = BitWidth;
362     GenericAddress->BitOffset = 0;
363     GenericAddress->AccessWidth = 0; /* Access width ANY */
364 }
365 
366 
367 /*******************************************************************************
368  *
369  * FUNCTION:    AcpiTbSelectAddress
370  *
371  * PARAMETERS:  RegisterName        - ASCII name of the ACPI register
372  *              Address32           - 32-bit address of the register
373  *              Address64           - 64-bit address of the register
374  *
375  * RETURN:      The resolved 64-bit address
376  *
377  * DESCRIPTION: Select between 32-bit and 64-bit versions of addresses within
378  *              the FADT. Used for the FACS and DSDT addresses.
379  *
380  * NOTES:
381  *
382  * Check for FACS and DSDT address mismatches. An address mismatch between
383  * the 32-bit and 64-bit address fields (FIRMWARE_CTRL/X_FIRMWARE_CTRL and
384  * DSDT/X_DSDT) could be a corrupted address field or it might indicate
385  * the presence of two FACS or two DSDT tables.
386  *
387  * November 2013:
388  * By default, as per the ACPICA specification, a valid 64-bit address is
389  * used regardless of the value of the 32-bit address. However, this
390  * behavior can be overridden via the AcpiGbl_Use32BitFadtAddresses flag.
391  *
392  ******************************************************************************/
393 
394 static UINT64
395 AcpiTbSelectAddress (
396     char                    *RegisterName,
397     UINT32                  Address32,
398     UINT64                  Address64)
399 {
400 
401     if (!Address64)
402     {
403         /* 64-bit address is zero, use 32-bit address */
404 
405         return ((UINT64) Address32);
406     }
407 
408     if (Address32 &&
409        (Address64 != (UINT64) Address32))
410     {
411         /* Address mismatch between 32-bit and 64-bit versions */
412 
413         ACPI_BIOS_WARNING ((AE_INFO,
414             "32/64X %s address mismatch in FADT: "
415             "0x%8.8X/0x%8.8X%8.8X, using %u-bit address",
416             RegisterName, Address32, ACPI_FORMAT_UINT64 (Address64),
417             AcpiGbl_Use32BitFadtAddresses ? 32 : 64));
418 
419         /* 32-bit address override */
420 
421         if (AcpiGbl_Use32BitFadtAddresses)
422         {
423             return ((UINT64) Address32);
424         }
425     }
426 
427     /* Default is to use the 64-bit address */
428 
429     return (Address64);
430 }
431 
432 
433 /*******************************************************************************
434  *
435  * FUNCTION:    AcpiTbParseFadt
436  *
437  * PARAMETERS:  None
438  *
439  * RETURN:      None
440  *
441  * DESCRIPTION: Initialize the FADT, DSDT and FACS tables
442  *              (FADT contains the addresses of the DSDT and FACS)
443  *
444  ******************************************************************************/
445 
446 void
447 AcpiTbParseFadt (
448     void)
449 {
450     UINT32                  Length;
451     ACPI_TABLE_HEADER       *Table;
452     ACPI_TABLE_DESC         *FadtDesc;
453     ACPI_STATUS             Status;
454 
455 
456     /*
457      * The FADT has multiple versions with different lengths,
458      * and it contains pointers to both the DSDT and FACS tables.
459      *
460      * Get a local copy of the FADT and convert it to a common format
461      * Map entire FADT, assumed to be smaller than one page.
462      */
463     FadtDesc = &AcpiGbl_RootTableList.Tables[AcpiGbl_FadtIndex];
464     Status = AcpiTbGetTable (FadtDesc, &Table);
465     if (ACPI_FAILURE (Status))
466     {
467         return;
468     }
469     Length = FadtDesc->Length;
470 
471     /*
472      * Validate the FADT checksum before we copy the table. Ignore
473      * checksum error as we want to try to get the DSDT and FACS.
474      */
475     (void) AcpiUtVerifyChecksum (Table, Length);
476 
477     /* Create a local copy of the FADT in common ACPI 2.0+ format */
478 
479     AcpiTbCreateLocalFadt (Table, Length);
480 
481     /* All done with the real FADT, unmap it */
482 
483     AcpiTbPutTable (FadtDesc);
484 
485     /* Obtain the DSDT and FACS tables via their addresses within the FADT */
486 
487     AcpiTbInstallStandardTable (
488         (ACPI_PHYSICAL_ADDRESS) AcpiGbl_FADT.XDsdt,
489         ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, NULL, FALSE, TRUE,
490         &AcpiGbl_DsdtIndex);
491 
492     if (AcpiGbl_FADT.Facs)
493     {
494         AcpiTbInstallStandardTable (
495             (ACPI_PHYSICAL_ADDRESS) AcpiGbl_FADT.Facs,
496             ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, NULL, FALSE, TRUE,
497             &AcpiGbl_FacsIndex);
498     }
499     if (AcpiGbl_FADT.XFacs)
500     {
501         AcpiTbInstallStandardTable (
502             (ACPI_PHYSICAL_ADDRESS) AcpiGbl_FADT.XFacs,
503             ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, NULL, FALSE, TRUE,
504             &AcpiGbl_XFacsIndex);
505     }
506 }
507 
508 
509 /*******************************************************************************
510  *
511  * FUNCTION:    AcpiTbCreateLocalFadt
512  *
513  * PARAMETERS:  Table               - Pointer to BIOS FADT
514  *              Length              - Length of the table
515  *
516  * RETURN:      None
517  *
518  * DESCRIPTION: Get a local copy of the FADT and convert it to a common format.
519  *              Performs validation on some important FADT fields.
520  *
521  * NOTE:        We create a local copy of the FADT regardless of the version.
522  *
523  ******************************************************************************/
524 
525 void
526 AcpiTbCreateLocalFadt (
527     ACPI_TABLE_HEADER       *Table,
528     UINT32                  Length)
529 {
530 
531     /*
532      * Check if the FADT is larger than the largest table that we expect
533      * (typically the current ACPI specification version). If so, truncate
534      * the table, and issue a warning.
535      */
536     if (Length > sizeof (ACPI_TABLE_FADT))
537     {
538         ACPI_BIOS_WARNING ((AE_INFO,
539             "FADT (revision %u) is longer than %s length, "
540             "truncating length %u to %u",
541             Table->Revision, ACPI_FADT_CONFORMANCE, Length,
542             (UINT32) sizeof (ACPI_TABLE_FADT)));
543     }
544 
545     /* Clear the entire local FADT */
546 
547     memset (&AcpiGbl_FADT, 0, sizeof (ACPI_TABLE_FADT));
548 
549     /* Copy the original FADT, up to sizeof (ACPI_TABLE_FADT) */
550 
551     memcpy (&AcpiGbl_FADT, Table,
552         ACPI_MIN (Length, sizeof (ACPI_TABLE_FADT)));
553 
554     /* Take a copy of the Hardware Reduced flag */
555 
556     AcpiGbl_ReducedHardware = FALSE;
557     if (AcpiGbl_FADT.Flags & ACPI_FADT_HW_REDUCED)
558     {
559         AcpiGbl_ReducedHardware = TRUE;
560     }
561 
562     /* Convert the local copy of the FADT to the common internal format */
563 
564     AcpiTbConvertFadt ();
565 
566     /* Initialize the global ACPI register structures */
567 
568     AcpiTbSetupFadtRegisters ();
569 }
570 
571 
572 /*******************************************************************************
573  *
574  * FUNCTION:    AcpiTbConvertFadt
575  *
576  * PARAMETERS:  None - AcpiGbl_FADT is used.
577  *
578  * RETURN:      None
579  *
580  * DESCRIPTION: Converts all versions of the FADT to a common internal format.
581  *              Expand 32-bit addresses to 64-bit as necessary. Also validate
582  *              important fields within the FADT.
583  *
584  * NOTE:        AcpiGbl_FADT must be of size (ACPI_TABLE_FADT), and must
585  *              contain a copy of the actual BIOS-provided FADT.
586  *
587  * Notes on 64-bit register addresses:
588  *
589  * After this FADT conversion, later ACPICA code will only use the 64-bit "X"
590  * fields of the FADT for all ACPI register addresses.
591  *
592  * The 64-bit X fields are optional extensions to the original 32-bit FADT
593  * V1.0 fields. Even if they are present in the FADT, they are optional and
594  * are unused if the BIOS sets them to zero. Therefore, we must copy/expand
595  * 32-bit V1.0 fields to the 64-bit X fields if the 64-bit X field is originally
596  * zero.
597  *
598  * For ACPI 1.0 FADTs (that contain no 64-bit addresses), all 32-bit address
599  * fields are expanded to the corresponding 64-bit X fields in the internal
600  * common FADT.
601  *
602  * For ACPI 2.0+ FADTs, all valid (non-zero) 32-bit address fields are expanded
603  * to the corresponding 64-bit X fields, if the 64-bit field is originally
604  * zero. Adhering to the ACPI specification, we completely ignore the 32-bit
605  * field if the 64-bit field is valid, regardless of whether the host OS is
606  * 32-bit or 64-bit.
607  *
608  * Possible additional checks:
609  *  (AcpiGbl_FADT.Pm1EventLength >= 4)
610  *  (AcpiGbl_FADT.Pm1ControlLength >= 2)
611  *  (AcpiGbl_FADT.PmTimerLength >= 4)
612  *  Gpe block lengths must be multiple of 2
613  *
614  ******************************************************************************/
615 
616 static void
617 AcpiTbConvertFadt (
618     void)
619 {
620     const char              *Name;
621     ACPI_GENERIC_ADDRESS    *Address64;
622     UINT32                  Address32;
623     UINT8                   Length;
624     UINT8                   Flags;
625     UINT32                  i;
626 
627 
628     /*
629      * For ACPI 1.0 FADTs (revision 1 or 2), ensure that reserved fields which
630      * should be zero are indeed zero. This will workaround BIOSs that
631      * inadvertently place values in these fields.
632      *
633      * The ACPI 1.0 reserved fields that will be zeroed are the bytes located
634      * at offset 45, 55, 95, and the word located at offset 109, 110.
635      *
636      * Note: The FADT revision value is unreliable. Only the length can be
637      * trusted.
638      */
639     if (AcpiGbl_FADT.Header.Length <= ACPI_FADT_V2_SIZE)
640     {
641         AcpiGbl_FADT.PreferredProfile = 0;
642         AcpiGbl_FADT.PstateControl = 0;
643         AcpiGbl_FADT.CstControl = 0;
644         AcpiGbl_FADT.BootFlags = 0;
645     }
646 
647     /*
648      * Now we can update the local FADT length to the length of the
649      * current FADT version as defined by the ACPI specification.
650      * Thus, we will have a common FADT internally.
651      */
652     AcpiGbl_FADT.Header.Length = sizeof (ACPI_TABLE_FADT);
653 
654     /*
655      * Expand the 32-bit DSDT addresses to 64-bit as necessary.
656      * Later ACPICA code will always use the X 64-bit field.
657      */
658     AcpiGbl_FADT.XDsdt = AcpiTbSelectAddress ("DSDT",
659         AcpiGbl_FADT.Dsdt, AcpiGbl_FADT.XDsdt);
660 
661     /* If Hardware Reduced flag is set, we are all done */
662 
663     if (AcpiGbl_ReducedHardware)
664     {
665         return;
666     }
667 
668     /* Examine all of the 64-bit extended address fields (X fields) */
669 
670     for (i = 0; i < ACPI_FADT_INFO_ENTRIES; i++)
671     {
672         /*
673          * Get the 32-bit and 64-bit addresses, as well as the register
674          * length and register name.
675          */
676         Address32 = *ACPI_ADD_PTR (UINT32,
677             &AcpiGbl_FADT, FadtInfoTable[i].Address32);
678 
679         Address64 = ACPI_ADD_PTR (ACPI_GENERIC_ADDRESS,
680             &AcpiGbl_FADT, FadtInfoTable[i].Address64);
681 
682         Length = *ACPI_ADD_PTR (UINT8,
683             &AcpiGbl_FADT, FadtInfoTable[i].Length);
684 
685         Name = FadtInfoTable[i].Name;
686         Flags = FadtInfoTable[i].Flags;
687 
688         /*
689          * Expand the ACPI 1.0 32-bit addresses to the ACPI 2.0 64-bit "X"
690          * generic address structures as necessary. Later code will always use
691          * the 64-bit address structures.
692          *
693          * November 2013:
694          * Now always use the 64-bit address if it is valid (non-zero), in
695          * accordance with the ACPI specification which states that a 64-bit
696          * address supersedes the 32-bit version. This behavior can be
697          * overridden by the AcpiGbl_Use32BitFadtAddresses flag.
698          *
699          * During 64-bit address construction and verification,
700          * these cases are handled:
701          *
702          * Address32 zero, Address64 [don't care]   - Use Address64
703          *
704          * No override: if AcpiGbl_Use32BitFadtAddresses is FALSE, and:
705          * Address32 non-zero, Address64 zero       - Copy/use Address32
706          * Address32 non-zero == Address64 non-zero - Use Address64
707          * Address32 non-zero != Address64 non-zero - Warning, use Address64
708          *
709          * Override: if AcpiGbl_Use32BitFadtAddresses is TRUE, and:
710          * Address32 non-zero, Address64 zero       - Copy/use Address32
711          * Address32 non-zero == Address64 non-zero - Copy/use Address32
712          * Address32 non-zero != Address64 non-zero - Warning, copy/use Address32
713          *
714          * Note: SpaceId is always I/O for 32-bit legacy address fields
715          */
716         if (Address32)
717         {
718             if (Address64->Address)
719             {
720                 if (Address64->Address != (UINT64) Address32)
721                 {
722                     /* Address mismatch */
723 
724                     ACPI_BIOS_WARNING ((AE_INFO,
725                         "32/64X address mismatch in FADT/%s: "
726                         "0x%8.8X/0x%8.8X%8.8X, using %u-bit address",
727                         Name, Address32,
728                         ACPI_FORMAT_UINT64 (Address64->Address),
729                         AcpiGbl_Use32BitFadtAddresses ? 32 : 64));
730                 }
731 
732                 /*
733                  * For each extended field, check for length mismatch
734                  * between the legacy length field and the corresponding
735                  * 64-bit X length field.
736                  * Note: If the legacy length field is > 0xFF bits, ignore
737                  * this check. (GPE registers can be larger than the
738                  * 64-bit GAS structure can accommodate, 0xFF bits).
739                  */
740                 if ((ACPI_MUL_8 (Length) <= ACPI_UINT8_MAX) &&
741                     (Address64->BitWidth != ACPI_MUL_8 (Length)))
742                 {
743                     ACPI_BIOS_WARNING ((AE_INFO,
744                         "32/64X length mismatch in FADT/%s: %u/%u",
745                         Name, ACPI_MUL_8 (Length), Address64->BitWidth));
746                 }
747             }
748 
749             /*
750              * Hardware register access code always uses the 64-bit fields.
751              * So if the 64-bit field is zero or is to be overridden,
752              * initialize it with the 32-bit fields.
753              * Note that when the 32-bit address favor is specified, the
754              * 64-bit fields are always re-initialized so that
755              * AccessSize/BitWidth/BitOffset fields can be correctly
756              * configured to the values to trigger a 32-bit compatible
757              * access mode in the hardware register access code.
758              */
759             if (!Address64->Address || AcpiGbl_Use32BitFadtAddresses)
760             {
761                 AcpiTbInitGenericAddress (Address64,
762                     ACPI_ADR_SPACE_SYSTEM_IO, Length,
763                     (UINT64) Address32, Name, Flags);
764             }
765         }
766 
767         if (FadtInfoTable[i].Flags & ACPI_FADT_REQUIRED)
768         {
769             /*
770              * Field is required (PM1aEvent, PM1aControl).
771              * Both the address and length must be non-zero.
772              */
773             if (!Address64->Address || !Length)
774             {
775                 ACPI_BIOS_ERROR ((AE_INFO,
776                     "Required FADT field %s has zero address and/or length: "
777                     "0x%8.8X%8.8X/0x%X",
778                     Name, ACPI_FORMAT_UINT64 (Address64->Address), Length));
779             }
780         }
781         else if (FadtInfoTable[i].Flags & ACPI_FADT_SEPARATE_LENGTH)
782         {
783             /*
784              * Field is optional (PM2Control, GPE0, GPE1) AND has its own
785              * length field. If present, both the address and length must
786              * be valid.
787              */
788             if ((Address64->Address && !Length) ||
789                 (!Address64->Address && Length))
790             {
791                 ACPI_BIOS_WARNING ((AE_INFO,
792                     "Optional FADT field %s has valid %s but zero %s: "
793                     "0x%8.8X%8.8X/0x%X", Name,
794                     (Length ? "Length" : "Address"),
795                     (Length ? "Address": "Length"),
796                     ACPI_FORMAT_UINT64 (Address64->Address), Length));
797             }
798         }
799     }
800 }
801 
802 
803 /*******************************************************************************
804  *
805  * FUNCTION:    AcpiTbSetupFadtRegisters
806  *
807  * PARAMETERS:  None, uses AcpiGbl_FADT.
808  *
809  * RETURN:      None
810  *
811  * DESCRIPTION: Initialize global ACPI PM1 register definitions. Optionally,
812  *              force FADT register definitions to their default lengths.
813  *
814  ******************************************************************************/
815 
816 static void
817 AcpiTbSetupFadtRegisters (
818     void)
819 {
820     ACPI_GENERIC_ADDRESS    *Target64;
821     ACPI_GENERIC_ADDRESS    *Source64;
822     UINT8                   Pm1RegisterByteWidth;
823     UINT32                  i;
824 
825 
826     /*
827      * Optionally check all register lengths against the default values and
828      * update them if they are incorrect.
829      */
830     if (AcpiGbl_UseDefaultRegisterWidths)
831     {
832         for (i = 0; i < ACPI_FADT_INFO_ENTRIES; i++)
833         {
834             Target64 = ACPI_ADD_PTR (ACPI_GENERIC_ADDRESS, &AcpiGbl_FADT,
835                 FadtInfoTable[i].Address64);
836 
837             /*
838              * If a valid register (Address != 0) and the (DefaultLength > 0)
839              * (Not a GPE register), then check the width against the default.
840              */
841             if ((Target64->Address) &&
842                 (FadtInfoTable[i].DefaultLength > 0) &&
843                 (FadtInfoTable[i].DefaultLength != Target64->BitWidth))
844             {
845                 ACPI_BIOS_WARNING ((AE_INFO,
846                     "Invalid length for FADT/%s: %u, using default %u",
847                     FadtInfoTable[i].Name, Target64->BitWidth,
848                     FadtInfoTable[i].DefaultLength));
849 
850                 /* Incorrect size, set width to the default */
851 
852                 Target64->BitWidth = FadtInfoTable[i].DefaultLength;
853             }
854         }
855     }
856 
857     /*
858      * Get the length of the individual PM1 registers (enable and status).
859      * Each register is defined to be (event block length / 2). Extra divide
860      * by 8 converts bits to bytes.
861      */
862     Pm1RegisterByteWidth = (UINT8)
863         ACPI_DIV_16 (AcpiGbl_FADT.XPm1aEventBlock.BitWidth);
864 
865     /*
866      * Calculate separate GAS structs for the PM1x (A/B) Status and Enable
867      * registers. These addresses do not appear (directly) in the FADT, so it
868      * is useful to pre-calculate them from the PM1 Event Block definitions.
869      *
870      * The PM event blocks are split into two register blocks, first is the
871      * PM Status Register block, followed immediately by the PM Enable
872      * Register block. Each is of length (Pm1EventLength/2)
873      *
874      * Note: The PM1A event block is required by the ACPI specification.
875      * However, the PM1B event block is optional and is rarely, if ever,
876      * used.
877      */
878 
879     for (i = 0; i < ACPI_FADT_PM_INFO_ENTRIES; i++)
880     {
881         Source64 = ACPI_ADD_PTR (ACPI_GENERIC_ADDRESS, &AcpiGbl_FADT,
882             FadtPmInfoTable[i].Source);
883 
884         if (Source64->Address)
885         {
886             AcpiTbInitGenericAddress (FadtPmInfoTable[i].Target,
887                 Source64->SpaceId, Pm1RegisterByteWidth,
888                 Source64->Address +
889                     (FadtPmInfoTable[i].RegisterNum * Pm1RegisterByteWidth),
890                 "PmRegisters", 0);
891         }
892     }
893 }
894