xref: /freebsd/sys/contrib/dev/acpica/compiler/asloffset.c (revision 725a9f47324d42037db93c27ceb40d4956872f3e)
1 /******************************************************************************
2  *
3  * Module Name: asloffset - Generate a C "offset table" for BIOS use.
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2023, 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/compiler/aslcompiler.h>
153 #include "aslcompiler.y.h"
154 #include <contrib/dev/acpica/include/amlcode.h>
155 #include <contrib/dev/acpica/include/acnamesp.h>
156 
157 
158 #define _COMPONENT          ACPI_COMPILER
159         ACPI_MODULE_NAME    ("asloffset")
160 
161 
162 /* Local prototypes */
163 
164 static void
165 LsEmitOffsetTableEntry (
166     UINT32                  FileId,
167     ACPI_NAMESPACE_NODE     *Node,
168     UINT32                  NamepathOffset,
169     UINT32                  Offset,
170     char                    *OpName,
171     UINT64                  Value,
172     UINT8                   AmlOpcode,
173     UINT16                  ParentOpcode);
174 
175 
176 /*******************************************************************************
177  *
178  * FUNCTION:    LsAmlOffsetWalk
179  *
180  * PARAMETERS:  ASL_WALK_CALLBACK
181  *
182  * RETURN:      Status
183  *
184  * DESCRIPTION: Process one node during a offset table file generation.
185  *
186  * Three types of objects are currently emitted to the offset table:
187  *   1) Tagged (named) resource descriptors
188  *   2) Named integer objects with constant integer values
189  *   3) Named package objects
190  *   4) Operation Regions that have constant Offset (address) parameters
191  *   5) Control methods
192  *
193  * The offset table allows the BIOS to dynamically update the values of these
194  * objects at boot time.
195  *
196  ******************************************************************************/
197 
198 ACPI_STATUS
199 LsAmlOffsetWalk (
200     ACPI_PARSE_OBJECT       *Op,
201     UINT32                  Level,
202     void                    *Context)
203 {
204     UINT32                  FileId = (UINT32) ACPI_TO_INTEGER (Context);
205     ACPI_NAMESPACE_NODE     *Node;
206     UINT32                  Length;
207     UINT32                  NamepathOffset;
208     UINT32                  DataOffset;
209     ACPI_PARSE_OBJECT       *NextOp;
210 
211 
212     /* Ignore actual data blocks for resource descriptors */
213 
214     if (Op->Asl.CompileFlags & OP_IS_RESOURCE_DATA)
215     {
216         return (AE_OK); /* Do NOT update the global AML offset */
217     }
218 
219     /* We are only interested in named objects (have a namespace node) */
220 
221     Node = Op->Asl.Node;
222     if (!Node)
223     {
224         AslGbl_CurrentAmlOffset += Op->Asl.FinalAmlLength;
225         return (AE_OK);
226     }
227 
228     /* Named resource descriptor (has a descriptor tag) */
229 
230     if ((Node->Type == ACPI_TYPE_LOCAL_RESOURCE) &&
231         (Op->Asl.CompileFlags & OP_IS_RESOURCE_DESC))
232     {
233         LsEmitOffsetTableEntry (FileId, Node, 0, AslGbl_CurrentAmlOffset,
234             Op->Asl.ParseOpName, 0, Op->Asl.Extra, AML_BUFFER_OP);
235 
236         AslGbl_CurrentAmlOffset += Op->Asl.FinalAmlLength;
237         return (AE_OK);
238     }
239 
240     switch (Op->Asl.AmlOpcode)
241     {
242     case AML_NAME_OP:
243 
244         /* Named object -- Name (NameString, DataRefObject) */
245 
246         if (!Op->Asl.Child)
247         {
248             FlPrintFile (FileId, "%s NO CHILD!\n", AslGbl_MsgBuffer);
249             return (AE_OK);
250         }
251 
252         Length = Op->Asl.FinalAmlLength;
253 
254         /* Get to the NameSeg/NamePath Op (and length of the name) */
255 
256         Op = Op->Asl.Child;
257 
258         /* Get offset of last nameseg and the actual data */
259 
260         NamepathOffset = AslGbl_CurrentAmlOffset + Length +
261             (Op->Asl.FinalAmlLength - ACPI_NAMESEG_SIZE);
262 
263         DataOffset = AslGbl_CurrentAmlOffset + Length +
264             Op->Asl.FinalAmlLength;
265 
266         /* Get actual value associated with the name */
267 
268         Op = Op->Asl.Next;
269         switch (Op->Asl.AmlOpcode)
270         {
271         case AML_BYTE_OP:
272         case AML_WORD_OP:
273         case AML_DWORD_OP:
274         case AML_QWORD_OP:
275 
276             /* The +1 is to handle the integer size prefix (opcode) */
277 
278             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, (DataOffset + 1),
279                 Op->Asl.ParseOpName, Op->Asl.Value.Integer,
280                 (UINT8) Op->Asl.AmlOpcode, AML_NAME_OP);
281             break;
282 
283         case AML_ONE_OP:
284         case AML_ONES_OP:
285         case AML_ZERO_OP:
286 
287             /* For these, offset will point to the opcode */
288 
289             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
290                 Op->Asl.ParseOpName, Op->Asl.Value.Integer,
291                 (UINT8) Op->Asl.AmlOpcode, AML_NAME_OP);
292             break;
293 
294         case AML_PACKAGE_OP:
295         case AML_VARIABLE_PACKAGE_OP:
296 
297             /* Get the package element count */
298 
299             NextOp = Op->Asl.Child;
300 
301             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
302                 Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
303                 (UINT8) Op->Asl.AmlOpcode, AML_NAME_OP);
304             break;
305 
306          default:
307              break;
308         }
309 
310         AslGbl_CurrentAmlOffset += Length;
311         return (AE_OK);
312 
313     case AML_REGION_OP:
314 
315         /* OperationRegion (NameString, RegionSpace, RegionOffset, RegionLength) */
316 
317         Length = Op->Asl.FinalAmlLength;
318 
319         /* Get the name/namepath node */
320 
321         NextOp = Op->Asl.Child;
322 
323         /* Get offset of last nameseg and the actual data */
324 
325         NamepathOffset = AslGbl_CurrentAmlOffset + Length +
326             (NextOp->Asl.FinalAmlLength - ACPI_NAMESEG_SIZE);
327 
328         DataOffset = AslGbl_CurrentAmlOffset + Length +
329             (NextOp->Asl.FinalAmlLength + 1);
330 
331         /* Get the SpaceId node, then the Offset (address) node */
332 
333         NextOp = NextOp->Asl.Next;
334         NextOp = NextOp->Asl.Next;
335 
336         switch (NextOp->Asl.AmlOpcode)
337         {
338         /*
339          * We are only interested in integer constants that can be changed
340          * at boot time. Note, the One/Ones/Zero opcodes are considered
341          * non-changeable, so we ignore them here.
342          */
343         case AML_BYTE_OP:
344         case AML_WORD_OP:
345         case AML_DWORD_OP:
346         case AML_QWORD_OP:
347 
348             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, (DataOffset + 1),
349                 Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
350                 (UINT8) NextOp->Asl.AmlOpcode, AML_REGION_OP);
351 
352             AslGbl_CurrentAmlOffset += Length;
353             return (AE_OK);
354 
355         default:
356             break;
357         }
358         break;
359 
360     case AML_METHOD_OP:
361 
362         /* Method (Namepath, ...) */
363 
364         Length = Op->Asl.FinalAmlLength;
365 
366         /* Get the NameSeg/NamePath Op */
367 
368         NextOp = Op->Asl.Child;
369 
370         /* Get offset of last nameseg and the actual data (flags byte) */
371 
372         NamepathOffset = AslGbl_CurrentAmlOffset + Length +
373             (NextOp->Asl.FinalAmlLength - ACPI_NAMESEG_SIZE);
374 
375         DataOffset = AslGbl_CurrentAmlOffset + Length +
376             NextOp->Asl.FinalAmlLength;
377 
378         /* Get the flags byte Op */
379 
380         NextOp = NextOp->Asl.Next;
381 
382         LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
383             Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
384             (UINT8) Op->Asl.AmlOpcode, AML_METHOD_OP);
385         break;
386 
387     case AML_PROCESSOR_OP:
388 
389         /* Processor (Namepath, ProcessorId, Address, Length) */
390 
391         Length = Op->Asl.FinalAmlLength;
392         NextOp = Op->Asl.Child;     /* Get Namepath */
393 
394         /* Get offset of last nameseg and the actual data (PBlock address) */
395 
396         NamepathOffset = AslGbl_CurrentAmlOffset + Length +
397             (NextOp->Asl.FinalAmlLength - ACPI_NAMESEG_SIZE);
398 
399         DataOffset = AslGbl_CurrentAmlOffset + Length +
400             (NextOp->Asl.FinalAmlLength + 1);
401 
402         NextOp = NextOp->Asl.Next;  /* Get ProcessorID (BYTE) */
403         NextOp = NextOp->Asl.Next;  /* Get Address (DWORD) */
404 
405         LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
406             Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
407             (UINT8) AML_DWORD_OP, AML_PROCESSOR_OP);
408         break;
409 
410     case AML_DEVICE_OP:
411     case AML_SCOPE_OP:
412     case AML_THERMAL_ZONE_OP:
413 
414         /* Device/Scope/ThermalZone (Namepath) */
415 
416         Length = Op->Asl.FinalAmlLength;
417         NextOp = Op->Asl.Child;     /* Get Namepath */
418 
419         /* Get offset of last nameseg */
420 
421         NamepathOffset = AslGbl_CurrentAmlOffset + Length +
422             (NextOp->Asl.FinalAmlLength - ACPI_NAMESEG_SIZE);
423 
424         LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, 0,
425             Op->Asl.ParseOpName, 0, (UINT8) 0, Op->Asl.AmlOpcode);
426         break;
427 
428     default:
429         break;
430     }
431 
432     AslGbl_CurrentAmlOffset += Op->Asl.FinalAmlLength;
433     return (AE_OK);
434 }
435 
436 
437 /*******************************************************************************
438  *
439  * FUNCTION:    LsEmitOffsetTableEntry
440  *
441  * PARAMETERS:  FileId          - ID of current listing file
442  *              Node            - Namespace node associated with the name
443  *              Offset          - Offset of the value within the AML table
444  *              OpName          - Name of the AML opcode
445  *              Value           - Current value of the AML field
446  *              AmlOpcode       - Opcode associated with the field
447  *              ObjectType      - ACPI object type
448  *
449  * RETURN:      None
450  *
451  * DESCRIPTION: Emit a line of the offset table (-so option)
452  *
453  ******************************************************************************/
454 
455 static void
456 LsEmitOffsetTableEntry (
457     UINT32                  FileId,
458     ACPI_NAMESPACE_NODE     *Node,
459     UINT32                  NamepathOffset,
460     UINT32                  Offset,
461     char                    *OpName,
462     UINT64                  Value,
463     UINT8                   AmlOpcode,
464     UINT16                  ParentOpcode)
465 {
466     ACPI_BUFFER             TargetPath;
467     ACPI_STATUS             Status;
468 
469 
470     /* Get the full pathname to the namespace node */
471 
472     TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
473     Status = AcpiNsHandleToPathname (Node, &TargetPath, FALSE);
474     if (ACPI_FAILURE (Status))
475     {
476         return;
477     }
478 
479     /* [1] - Skip the opening backslash for the path */
480 
481     strcpy (AslGbl_MsgBuffer, "\"");
482     strcat (AslGbl_MsgBuffer, &((char *) TargetPath.Pointer)[1]);
483     strcat (AslGbl_MsgBuffer, "\",");
484     ACPI_FREE (TargetPath.Pointer);
485 
486     /*
487      * Max offset is 4G, constrained by 32-bit ACPI table length.
488      * Max Length for Integers is 8 bytes.
489      */
490     FlPrintFile (FileId,
491         "    {%-29s 0x%4.4X, 0x%8.8X, 0x%2.2X, 0x%8.8X, 0x%8.8X%8.8X}, /* %s */\n",
492         AslGbl_MsgBuffer, ParentOpcode, NamepathOffset, AmlOpcode,
493         Offset, ACPI_FORMAT_UINT64 (Value), OpName);
494 }
495 
496 
497 /*******************************************************************************
498  *
499  * FUNCTION:    LsDoOffsetTableHeader, LsDoOffsetTableFooter
500  *
501  * PARAMETERS:  FileId          - ID of current listing file
502  *
503  * RETURN:      None
504  *
505  * DESCRIPTION: Header and footer for the offset table file.
506  *
507  ******************************************************************************/
508 
509 void
510 LsDoOffsetTableHeader (
511     UINT32                  FileId)
512 {
513 
514     FlPrintFile (FileId,
515         "#ifndef __AML_OFFSET_TABLE_H\n"
516         "#define __AML_OFFSET_TABLE_H\n\n");
517 
518     FlPrintFile (FileId, "typedef struct {\n"
519         "    char                   *Pathname;      /* Full pathname (from root) to the object */\n"
520         "    unsigned short         ParentOpcode;   /* AML opcode for the parent object */\n"
521         "    unsigned long          NamesegOffset;  /* Offset of last nameseg in the parent namepath */\n"
522         "    unsigned char          Opcode;         /* AML opcode for the data */\n"
523         "    unsigned long          Offset;         /* Offset for the data */\n"
524         "    unsigned long long     Value;          /* Original value of the data (as applicable) */\n"
525         "} AML_OFFSET_TABLE_ENTRY;\n\n");
526 
527     FlPrintFile (FileId,
528         "#endif /* __AML_OFFSET_TABLE_H */\n\n");
529 
530     FlPrintFile (FileId,
531         "/*\n"
532         " * Information specific to the supported object types:\n"
533         " *\n"
534         " * Integers:\n"
535         " *    Opcode is the integer prefix, indicates length of the data\n"
536         " *        (One of: BYTE, WORD, DWORD, QWORD, ZERO, ONE, ONES)\n"
537         " *    Offset points to the actual integer data\n"
538         " *    Value is the existing value in the AML\n"
539         " *\n"
540         " * Packages:\n"
541         " *    Opcode is the package or var_package opcode\n"
542         " *    Offset points to the package opcode\n"
543         " *    Value is the package element count\n"
544         " *\n"
545         " * Operation Regions:\n"
546         " *    Opcode is the address integer prefix, indicates length of the data\n"
547         " *    Offset points to the region address\n"
548         " *    Value is the existing address value in the AML\n"
549         " *\n"
550         " * Control Methods:\n"
551         " *    Offset points to the method flags byte\n"
552         " *    Value is the existing flags value in the AML\n"
553         " *\n"
554         " * Processors:\n"
555         " *    Offset points to the first byte of the PBlock Address\n"
556         " *\n"
557         " * Resource Descriptors:\n"
558         " *    Opcode is the descriptor type\n"
559         " *    Offset points to the start of the descriptor\n"
560         " *\n"
561         " * Scopes/Devices/ThermalZones:\n"
562         " *    Nameseg offset only\n"
563         " */\n");
564 
565     FlPrintFile (FileId,
566         "AML_OFFSET_TABLE_ENTRY   %s_%s_OffsetTable[] =\n{\n",
567         AslGbl_TableSignature, AslGbl_TableId);
568 }
569 
570 
571 void
572 LsDoOffsetTableFooter (
573     UINT32                  FileId)
574 {
575 
576     FlPrintFile (FileId,
577         "    {NULL,0,0,0,0,0} /* Table terminator */\n};\n\n");
578     AslGbl_CurrentAmlOffset = 0;
579 }
580