xref: /freebsd/sys/contrib/dev/acpica/components/tables/tbfadt.c (revision 29fc4075e69fd27de0cded313ac6000165d99f8b)
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 - 2022, 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) AcpiTbVerifyChecksum (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 Hardware Reduced flag is set, there is no FACS */
493 
494     if (!AcpiGbl_ReducedHardware)
495     {
496         if (AcpiGbl_FADT.Facs)
497         {
498             AcpiTbInstallStandardTable (
499                 (ACPI_PHYSICAL_ADDRESS) AcpiGbl_FADT.Facs,
500                 ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, NULL, FALSE, TRUE,
501                 &AcpiGbl_FacsIndex);
502         }
503         if (AcpiGbl_FADT.XFacs)
504         {
505             AcpiTbInstallStandardTable (
506                 (ACPI_PHYSICAL_ADDRESS) AcpiGbl_FADT.XFacs,
507                 ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, NULL, FALSE, TRUE,
508                 &AcpiGbl_XFacsIndex);
509         }
510     }
511 }
512 
513 
514 /*******************************************************************************
515  *
516  * FUNCTION:    AcpiTbCreateLocalFadt
517  *
518  * PARAMETERS:  Table               - Pointer to BIOS FADT
519  *              Length              - Length of the table
520  *
521  * RETURN:      None
522  *
523  * DESCRIPTION: Get a local copy of the FADT and convert it to a common format.
524  *              Performs validation on some important FADT fields.
525  *
526  * NOTE:        We create a local copy of the FADT regardless of the version.
527  *
528  ******************************************************************************/
529 
530 void
531 AcpiTbCreateLocalFadt (
532     ACPI_TABLE_HEADER       *Table,
533     UINT32                  Length)
534 {
535 
536     /*
537      * Check if the FADT is larger than the largest table that we expect
538      * (typically the current ACPI specification version). If so, truncate
539      * the table, and issue a warning.
540      */
541     if (Length > sizeof (ACPI_TABLE_FADT))
542     {
543         ACPI_BIOS_WARNING ((AE_INFO,
544             "FADT (revision %u) is longer than %s length, "
545             "truncating length %u to %u",
546             Table->Revision, ACPI_FADT_CONFORMANCE, Length,
547             (UINT32) sizeof (ACPI_TABLE_FADT)));
548     }
549 
550     /* Clear the entire local FADT */
551 
552     memset (&AcpiGbl_FADT, 0, sizeof (ACPI_TABLE_FADT));
553 
554     /* Copy the original FADT, up to sizeof (ACPI_TABLE_FADT) */
555 
556     memcpy (&AcpiGbl_FADT, Table,
557         ACPI_MIN (Length, sizeof (ACPI_TABLE_FADT)));
558 
559     /* Take a copy of the Hardware Reduced flag */
560 
561     AcpiGbl_ReducedHardware = FALSE;
562     if (AcpiGbl_FADT.Flags & ACPI_FADT_HW_REDUCED)
563     {
564         AcpiGbl_ReducedHardware = TRUE;
565     }
566 
567     /* Convert the local copy of the FADT to the common internal format */
568 
569     AcpiTbConvertFadt ();
570 
571     /* Initialize the global ACPI register structures */
572 
573     AcpiTbSetupFadtRegisters ();
574 }
575 
576 
577 /*******************************************************************************
578  *
579  * FUNCTION:    AcpiTbConvertFadt
580  *
581  * PARAMETERS:  None - AcpiGbl_FADT is used.
582  *
583  * RETURN:      None
584  *
585  * DESCRIPTION: Converts all versions of the FADT to a common internal format.
586  *              Expand 32-bit addresses to 64-bit as necessary. Also validate
587  *              important fields within the FADT.
588  *
589  * NOTE:        AcpiGbl_FADT must be of size (ACPI_TABLE_FADT), and must
590  *              contain a copy of the actual BIOS-provided FADT.
591  *
592  * Notes on 64-bit register addresses:
593  *
594  * After this FADT conversion, later ACPICA code will only use the 64-bit "X"
595  * fields of the FADT for all ACPI register addresses.
596  *
597  * The 64-bit X fields are optional extensions to the original 32-bit FADT
598  * V1.0 fields. Even if they are present in the FADT, they are optional and
599  * are unused if the BIOS sets them to zero. Therefore, we must copy/expand
600  * 32-bit V1.0 fields to the 64-bit X fields if the 64-bit X field is originally
601  * zero.
602  *
603  * For ACPI 1.0 FADTs (that contain no 64-bit addresses), all 32-bit address
604  * fields are expanded to the corresponding 64-bit X fields in the internal
605  * common FADT.
606  *
607  * For ACPI 2.0+ FADTs, all valid (non-zero) 32-bit address fields are expanded
608  * to the corresponding 64-bit X fields, if the 64-bit field is originally
609  * zero. Adhering to the ACPI specification, we completely ignore the 32-bit
610  * field if the 64-bit field is valid, regardless of whether the host OS is
611  * 32-bit or 64-bit.
612  *
613  * Possible additional checks:
614  *  (AcpiGbl_FADT.Pm1EventLength >= 4)
615  *  (AcpiGbl_FADT.Pm1ControlLength >= 2)
616  *  (AcpiGbl_FADT.PmTimerLength >= 4)
617  *  Gpe block lengths must be multiple of 2
618  *
619  ******************************************************************************/
620 
621 static void
622 AcpiTbConvertFadt (
623     void)
624 {
625     const char              *Name;
626     ACPI_GENERIC_ADDRESS    *Address64;
627     UINT32                  Address32;
628     UINT8                   Length;
629     UINT8                   Flags;
630     UINT32                  i;
631 
632 
633     /*
634      * For ACPI 1.0 FADTs (revision 1 or 2), ensure that reserved fields which
635      * should be zero are indeed zero. This will workaround BIOSs that
636      * inadvertently place values in these fields.
637      *
638      * The ACPI 1.0 reserved fields that will be zeroed are the bytes located
639      * at offset 45, 55, 95, and the word located at offset 109, 110.
640      *
641      * Note: The FADT revision value is unreliable. Only the length can be
642      * trusted.
643      */
644     if (AcpiGbl_FADT.Header.Length <= ACPI_FADT_V2_SIZE)
645     {
646         AcpiGbl_FADT.PreferredProfile = 0;
647         AcpiGbl_FADT.PstateControl = 0;
648         AcpiGbl_FADT.CstControl = 0;
649         AcpiGbl_FADT.BootFlags = 0;
650     }
651 
652     /*
653      * Now we can update the local FADT length to the length of the
654      * current FADT version as defined by the ACPI specification.
655      * Thus, we will have a common FADT internally.
656      */
657     AcpiGbl_FADT.Header.Length = sizeof (ACPI_TABLE_FADT);
658 
659     /*
660      * Expand the 32-bit DSDT addresses to 64-bit as necessary.
661      * Later ACPICA code will always use the X 64-bit field.
662      */
663     AcpiGbl_FADT.XDsdt = AcpiTbSelectAddress ("DSDT",
664         AcpiGbl_FADT.Dsdt, AcpiGbl_FADT.XDsdt);
665 
666     /* If Hardware Reduced flag is set, we are all done */
667 
668     if (AcpiGbl_ReducedHardware)
669     {
670         return;
671     }
672 
673     /* Examine all of the 64-bit extended address fields (X fields) */
674 
675     for (i = 0; i < ACPI_FADT_INFO_ENTRIES; i++)
676     {
677         /*
678          * Get the 32-bit and 64-bit addresses, as well as the register
679          * length and register name.
680          */
681         Address32 = *ACPI_ADD_PTR (UINT32,
682             &AcpiGbl_FADT, FadtInfoTable[i].Address32);
683 
684         Address64 = ACPI_ADD_PTR (ACPI_GENERIC_ADDRESS,
685             &AcpiGbl_FADT, FadtInfoTable[i].Address64);
686 
687         Length = *ACPI_ADD_PTR (UINT8,
688             &AcpiGbl_FADT, FadtInfoTable[i].Length);
689 
690         Name = FadtInfoTable[i].Name;
691         Flags = FadtInfoTable[i].Flags;
692 
693         /*
694          * Expand the ACPI 1.0 32-bit addresses to the ACPI 2.0 64-bit "X"
695          * generic address structures as necessary. Later code will always use
696          * the 64-bit address structures.
697          *
698          * November 2013:
699          * Now always use the 64-bit address if it is valid (non-zero), in
700          * accordance with the ACPI specification which states that a 64-bit
701          * address supersedes the 32-bit version. This behavior can be
702          * overridden by the AcpiGbl_Use32BitFadtAddresses flag.
703          *
704          * During 64-bit address construction and verification,
705          * these cases are handled:
706          *
707          * Address32 zero, Address64 [don't care]   - Use Address64
708          *
709          * No override: if AcpiGbl_Use32BitFadtAddresses is FALSE, and:
710          * Address32 non-zero, Address64 zero       - Copy/use Address32
711          * Address32 non-zero == Address64 non-zero - Use Address64
712          * Address32 non-zero != Address64 non-zero - Warning, use Address64
713          *
714          * Override: if AcpiGbl_Use32BitFadtAddresses is TRUE, and:
715          * Address32 non-zero, Address64 zero       - Copy/use Address32
716          * Address32 non-zero == Address64 non-zero - Copy/use Address32
717          * Address32 non-zero != Address64 non-zero - Warning, copy/use Address32
718          *
719          * Note: SpaceId is always I/O for 32-bit legacy address fields
720          */
721         if (Address32)
722         {
723             if (Address64->Address)
724             {
725                 if (Address64->Address != (UINT64) Address32)
726                 {
727                     /* Address mismatch */
728 
729                     ACPI_BIOS_WARNING ((AE_INFO,
730                         "32/64X address mismatch in FADT/%s: "
731                         "0x%8.8X/0x%8.8X%8.8X, using %u-bit address",
732                         Name, Address32,
733                         ACPI_FORMAT_UINT64 (Address64->Address),
734                         AcpiGbl_Use32BitFadtAddresses ? 32 : 64));
735                 }
736 
737                 /*
738                  * For each extended field, check for length mismatch
739                  * between the legacy length field and the corresponding
740                  * 64-bit X length field.
741                  * Note: If the legacy length field is > 0xFF bits, ignore
742                  * this check. (GPE registers can be larger than the
743                  * 64-bit GAS structure can accommodate, 0xFF bits).
744                  */
745                 if ((ACPI_MUL_8 (Length) <= ACPI_UINT8_MAX) &&
746                     (Address64->BitWidth != ACPI_MUL_8 (Length)))
747                 {
748                     ACPI_BIOS_WARNING ((AE_INFO,
749                         "32/64X length mismatch in FADT/%s: %u/%u",
750                         Name, ACPI_MUL_8 (Length), Address64->BitWidth));
751                 }
752             }
753 
754             /*
755              * Hardware register access code always uses the 64-bit fields.
756              * So if the 64-bit field is zero or is to be overridden,
757              * initialize it with the 32-bit fields.
758              * Note that when the 32-bit address favor is specified, the
759              * 64-bit fields are always re-initialized so that
760              * AccessSize/BitWidth/BitOffset fields can be correctly
761              * configured to the values to trigger a 32-bit compatible
762              * access mode in the hardware register access code.
763              */
764             if (!Address64->Address || AcpiGbl_Use32BitFadtAddresses)
765             {
766                 AcpiTbInitGenericAddress (Address64,
767                     ACPI_ADR_SPACE_SYSTEM_IO, Length,
768                     (UINT64) Address32, Name, Flags);
769             }
770         }
771 
772         if (FadtInfoTable[i].Flags & ACPI_FADT_REQUIRED)
773         {
774             /*
775              * Field is required (PM1aEvent, PM1aControl).
776              * Both the address and length must be non-zero.
777              */
778             if (!Address64->Address || !Length)
779             {
780                 ACPI_BIOS_ERROR ((AE_INFO,
781                     "Required FADT field %s has zero address and/or length: "
782                     "0x%8.8X%8.8X/0x%X",
783                     Name, ACPI_FORMAT_UINT64 (Address64->Address), Length));
784             }
785         }
786         else if (FadtInfoTable[i].Flags & ACPI_FADT_SEPARATE_LENGTH)
787         {
788             /*
789              * Field is optional (PM2Control, GPE0, GPE1) AND has its own
790              * length field. If present, both the address and length must
791              * be valid.
792              */
793             if ((Address64->Address && !Length) ||
794                 (!Address64->Address && Length))
795             {
796                 ACPI_BIOS_WARNING ((AE_INFO,
797                     "Optional FADT field %s has valid %s but zero %s: "
798                     "0x%8.8X%8.8X/0x%X", Name,
799                     (Length ? "Length" : "Address"),
800                     (Length ? "Address": "Length"),
801                     ACPI_FORMAT_UINT64 (Address64->Address), Length));
802             }
803         }
804     }
805 }
806 
807 
808 /*******************************************************************************
809  *
810  * FUNCTION:    AcpiTbSetupFadtRegisters
811  *
812  * PARAMETERS:  None, uses AcpiGbl_FADT.
813  *
814  * RETURN:      None
815  *
816  * DESCRIPTION: Initialize global ACPI PM1 register definitions. Optionally,
817  *              force FADT register definitions to their default lengths.
818  *
819  ******************************************************************************/
820 
821 static void
822 AcpiTbSetupFadtRegisters (
823     void)
824 {
825     ACPI_GENERIC_ADDRESS    *Target64;
826     ACPI_GENERIC_ADDRESS    *Source64;
827     UINT8                   Pm1RegisterByteWidth;
828     UINT32                  i;
829 
830 
831     /*
832      * Optionally check all register lengths against the default values and
833      * update them if they are incorrect.
834      */
835     if (AcpiGbl_UseDefaultRegisterWidths)
836     {
837         for (i = 0; i < ACPI_FADT_INFO_ENTRIES; i++)
838         {
839             Target64 = ACPI_ADD_PTR (ACPI_GENERIC_ADDRESS, &AcpiGbl_FADT,
840                 FadtInfoTable[i].Address64);
841 
842             /*
843              * If a valid register (Address != 0) and the (DefaultLength > 0)
844              * (Not a GPE register), then check the width against the default.
845              */
846             if ((Target64->Address) &&
847                 (FadtInfoTable[i].DefaultLength > 0) &&
848                 (FadtInfoTable[i].DefaultLength != Target64->BitWidth))
849             {
850                 ACPI_BIOS_WARNING ((AE_INFO,
851                     "Invalid length for FADT/%s: %u, using default %u",
852                     FadtInfoTable[i].Name, Target64->BitWidth,
853                     FadtInfoTable[i].DefaultLength));
854 
855                 /* Incorrect size, set width to the default */
856 
857                 Target64->BitWidth = FadtInfoTable[i].DefaultLength;
858             }
859         }
860     }
861 
862     /*
863      * Get the length of the individual PM1 registers (enable and status).
864      * Each register is defined to be (event block length / 2). Extra divide
865      * by 8 converts bits to bytes.
866      */
867     Pm1RegisterByteWidth = (UINT8)
868         ACPI_DIV_16 (AcpiGbl_FADT.XPm1aEventBlock.BitWidth);
869 
870     /*
871      * Calculate separate GAS structs for the PM1x (A/B) Status and Enable
872      * registers. These addresses do not appear (directly) in the FADT, so it
873      * is useful to pre-calculate them from the PM1 Event Block definitions.
874      *
875      * The PM event blocks are split into two register blocks, first is the
876      * PM Status Register block, followed immediately by the PM Enable
877      * Register block. Each is of length (Pm1EventLength/2)
878      *
879      * Note: The PM1A event block is required by the ACPI specification.
880      * However, the PM1B event block is optional and is rarely, if ever,
881      * used.
882      */
883 
884     for (i = 0; i < ACPI_FADT_PM_INFO_ENTRIES; i++)
885     {
886         Source64 = ACPI_ADD_PTR (ACPI_GENERIC_ADDRESS, &AcpiGbl_FADT,
887             FadtPmInfoTable[i].Source);
888 
889         if (Source64->Address)
890         {
891             AcpiTbInitGenericAddress (FadtPmInfoTable[i].Target,
892                 Source64->SpaceId, Pm1RegisterByteWidth,
893                 Source64->Address +
894                     (FadtPmInfoTable[i].RegisterNum * Pm1RegisterByteWidth),
895                 "PmRegisters", 0);
896         }
897     }
898 }
899