xref: /freebsd/sys/contrib/dev/acpica/compiler/asloffset.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
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 - 2017, 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 & NODE_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         Gbl_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 & NODE_IS_RESOURCE_DESC))
232     {
233         LsEmitOffsetTableEntry (FileId, Node, 0, Gbl_CurrentAmlOffset,
234             Op->Asl.ParseOpName, 0, Op->Asl.Extra, AML_BUFFER_OP);
235 
236         Gbl_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", MsgBuffer);
249             return (AE_OK);
250         }
251 
252         Length = Op->Asl.FinalAmlLength;
253         NamepathOffset = Gbl_CurrentAmlOffset + Length;
254 
255         /* Get to the NameSeg/NamePath Op (and length of the name) */
256 
257         Op = Op->Asl.Child;
258 
259         /* Get offset of last nameseg and the actual data */
260 
261         NamepathOffset = Gbl_CurrentAmlOffset + Length +
262             (Op->Asl.FinalAmlLength - ACPI_NAME_SIZE);
263 
264         DataOffset = Gbl_CurrentAmlOffset + Length +
265             Op->Asl.FinalAmlLength;
266 
267         /* Get actual value associated with the name */
268 
269         Op = Op->Asl.Next;
270         switch (Op->Asl.AmlOpcode)
271         {
272         case AML_BYTE_OP:
273         case AML_WORD_OP:
274         case AML_DWORD_OP:
275         case AML_QWORD_OP:
276 
277             /* The +1 is to handle the integer size prefix (opcode) */
278 
279             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, (DataOffset + 1),
280                 Op->Asl.ParseOpName, Op->Asl.Value.Integer,
281                 (UINT8) Op->Asl.AmlOpcode, AML_NAME_OP);
282             break;
283 
284         case AML_ONE_OP:
285         case AML_ONES_OP:
286         case AML_ZERO_OP:
287 
288             /* For these, offset will point to the opcode */
289 
290             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
291                 Op->Asl.ParseOpName, Op->Asl.Value.Integer,
292                 (UINT8) Op->Asl.AmlOpcode, AML_NAME_OP);
293             break;
294 
295         case AML_PACKAGE_OP:
296         case AML_VARIABLE_PACKAGE_OP:
297 
298             /* Get the package element count */
299 
300             NextOp = Op->Asl.Child;
301 
302             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
303                 Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
304                 (UINT8) Op->Asl.AmlOpcode, AML_NAME_OP);
305             break;
306 
307          default:
308              break;
309         }
310 
311         Gbl_CurrentAmlOffset += Length;
312         return (AE_OK);
313 
314     case AML_REGION_OP:
315 
316         /* OperationRegion (NameString, RegionSpace, RegionOffset, RegionLength) */
317 
318         Length = Op->Asl.FinalAmlLength;
319 
320         /* Get the name/namepath node */
321 
322         NextOp = Op->Asl.Child;
323 
324         /* Get offset of last nameseg and the actual data */
325 
326         NamepathOffset = Gbl_CurrentAmlOffset + Length +
327             (NextOp->Asl.FinalAmlLength - ACPI_NAME_SIZE);
328 
329         DataOffset = Gbl_CurrentAmlOffset + Length +
330             (NextOp->Asl.FinalAmlLength + 1);
331 
332         /* Get the SpaceId node, then the Offset (address) node */
333 
334         NextOp = NextOp->Asl.Next;
335         NextOp = NextOp->Asl.Next;
336 
337         switch (NextOp->Asl.AmlOpcode)
338         {
339         /*
340          * We are only interested in integer constants that can be changed
341          * at boot time. Note, the One/Ones/Zero opcodes are considered
342          * non-changeable, so we ignore them here.
343          */
344         case AML_BYTE_OP:
345         case AML_WORD_OP:
346         case AML_DWORD_OP:
347         case AML_QWORD_OP:
348 
349             LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, (DataOffset + 1),
350                 Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
351                 (UINT8) NextOp->Asl.AmlOpcode, AML_REGION_OP);
352 
353             Gbl_CurrentAmlOffset += Length;
354             return (AE_OK);
355 
356         default:
357             break;
358         }
359         break;
360 
361     case AML_METHOD_OP:
362 
363         /* Method (Namepath, ...) */
364 
365         Length = Op->Asl.FinalAmlLength;
366 
367         /* Get the NameSeg/NamePath Op */
368 
369         NextOp = Op->Asl.Child;
370 
371         /* Get offset of last nameseg and the actual data (flags byte) */
372 
373         NamepathOffset = Gbl_CurrentAmlOffset + Length +
374             (NextOp->Asl.FinalAmlLength - ACPI_NAME_SIZE);
375 
376         DataOffset = Gbl_CurrentAmlOffset + Length +
377             NextOp->Asl.FinalAmlLength;
378 
379         /* Get the flags byte Op */
380 
381         NextOp = NextOp->Asl.Next;
382 
383         LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
384             Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
385             (UINT8) Op->Asl.AmlOpcode, AML_METHOD_OP);
386         break;
387 
388     case AML_PROCESSOR_OP:
389 
390         /* Processor (Namepath, ProcessorId, Address, Length) */
391 
392         Length = Op->Asl.FinalAmlLength;
393         NextOp = Op->Asl.Child;     /* Get Namepath */
394 
395         /* Get offset of last nameseg and the actual data (PBlock address) */
396 
397         NamepathOffset = Gbl_CurrentAmlOffset + Length +
398             (NextOp->Asl.FinalAmlLength - ACPI_NAME_SIZE);
399 
400         DataOffset = Gbl_CurrentAmlOffset + Length +
401             (NextOp->Asl.FinalAmlLength + 1);
402 
403         NextOp = NextOp->Asl.Next;  /* Get ProcessorID (BYTE) */
404         NextOp = NextOp->Asl.Next;  /* Get Address (DWORD) */
405 
406         LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, DataOffset,
407             Op->Asl.ParseOpName, NextOp->Asl.Value.Integer,
408             (UINT8) AML_DWORD_OP, AML_PROCESSOR_OP);
409         break;
410 
411     case AML_DEVICE_OP:
412     case AML_SCOPE_OP:
413     case AML_THERMAL_ZONE_OP:
414 
415         /* Device/Scope/ThermalZone (Namepath) */
416 
417         Length = Op->Asl.FinalAmlLength;
418         NextOp = Op->Asl.Child;     /* Get Namepath */
419 
420         /* Get offset of last nameseg */
421 
422         NamepathOffset = Gbl_CurrentAmlOffset + Length +
423             (NextOp->Asl.FinalAmlLength - ACPI_NAME_SIZE);
424 
425         LsEmitOffsetTableEntry (FileId, Node, NamepathOffset, 0,
426             Op->Asl.ParseOpName, 0, (UINT8) 0, Op->Asl.AmlOpcode);
427         break;
428 
429     default:
430         break;
431     }
432 
433     Gbl_CurrentAmlOffset += Op->Asl.FinalAmlLength;
434     return (AE_OK);
435 }
436 
437 
438 /*******************************************************************************
439  *
440  * FUNCTION:    LsEmitOffsetTableEntry
441  *
442  * PARAMETERS:  FileId          - ID of current listing file
443  *              Node            - Namespace node associated with the name
444  *              Offset          - Offset of the value within the AML table
445  *              OpName          - Name of the AML opcode
446  *              Value           - Current value of the AML field
447  *              AmlOpcode       - Opcode associated with the field
448  *              ObjectType      - ACPI object type
449  *
450  * RETURN:      None
451  *
452  * DESCRIPTION: Emit a line of the offset table (-so option)
453  *
454  ******************************************************************************/
455 
456 static void
457 LsEmitOffsetTableEntry (
458     UINT32                  FileId,
459     ACPI_NAMESPACE_NODE     *Node,
460     UINT32                  NamepathOffset,
461     UINT32                  Offset,
462     char                    *OpName,
463     UINT64                  Value,
464     UINT8                   AmlOpcode,
465     UINT16                  ParentOpcode)
466 {
467     ACPI_BUFFER             TargetPath;
468     ACPI_STATUS             Status;
469 
470 
471     /* Get the full pathname to the namespace node */
472 
473     TargetPath.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
474     Status = AcpiNsHandleToPathname (Node, &TargetPath, FALSE);
475     if (ACPI_FAILURE (Status))
476     {
477         return;
478     }
479 
480     /* [1] - Skip the opening backslash for the path */
481 
482     strcpy (MsgBuffer, "\"");
483     strcat (MsgBuffer, &((char *) TargetPath.Pointer)[1]);
484     strcat (MsgBuffer, "\",");
485     ACPI_FREE (TargetPath.Pointer);
486 
487     /*
488      * Max offset is 4G, constrained by 32-bit ACPI table length.
489      * Max Length for Integers is 8 bytes.
490      */
491     FlPrintFile (FileId,
492         "    {%-29s 0x%4.4X, 0x%8.8X, 0x%2.2X, 0x%8.8X, 0x%8.8X%8.8X}, /* %s */\n",
493         MsgBuffer, ParentOpcode, NamepathOffset, AmlOpcode,
494         Offset, ACPI_FORMAT_UINT64 (Value), OpName);
495 }
496 
497 
498 /*******************************************************************************
499  *
500  * FUNCTION:    LsDoOffsetTableHeader, LsDoOffsetTableFooter
501  *
502  * PARAMETERS:  FileId          - ID of current listing file
503  *
504  * RETURN:      None
505  *
506  * DESCRIPTION: Header and footer for the offset table file.
507  *
508  ******************************************************************************/
509 
510 void
511 LsDoOffsetTableHeader (
512     UINT32                  FileId)
513 {
514 
515     FlPrintFile (FileId,
516         "#ifndef __AML_OFFSET_TABLE_H\n"
517         "#define __AML_OFFSET_TABLE_H\n\n");
518 
519     FlPrintFile (FileId, "typedef struct {\n"
520         "    char                   *Pathname;      /* Full pathname (from root) to the object */\n"
521         "    unsigned short         ParentOpcode;   /* AML opcode for the parent object */\n"
522         "    unsigned long          NamesegOffset;  /* Offset of last nameseg in the parent namepath */\n"
523         "    unsigned char          Opcode;         /* AML opcode for the data */\n"
524         "    unsigned long          Offset;         /* Offset for the data */\n"
525         "    unsigned long long     Value;          /* Original value of the data (as applicable) */\n"
526         "} AML_OFFSET_TABLE_ENTRY;\n\n");
527 
528     FlPrintFile (FileId,
529         "#endif /* __AML_OFFSET_TABLE_H */\n\n");
530 
531     FlPrintFile (FileId,
532         "/*\n"
533         " * Information specific to the supported object types:\n"
534         " *\n"
535         " * Integers:\n"
536         " *    Opcode is the integer prefix, indicates length of the data\n"
537         " *        (One of: BYTE, WORD, DWORD, QWORD, ZERO, ONE, ONES)\n"
538         " *    Offset points to the actual integer data\n"
539         " *    Value is the existing value in the AML\n"
540         " *\n"
541         " * Packages:\n"
542         " *    Opcode is the package or var_package opcode\n"
543         " *    Offset points to the package opcode\n"
544         " *    Value is the package element count\n"
545         " *\n"
546         " * Operation Regions:\n"
547         " *    Opcode is the address integer prefix, indicates length of the data\n"
548         " *    Offset points to the region address\n"
549         " *    Value is the existing address value in the AML\n"
550         " *\n"
551         " * Control Methods:\n"
552         " *    Offset points to the method flags byte\n"
553         " *    Value is the existing flags value in the AML\n"
554         " *\n"
555         " * Processors:\n"
556         " *    Offset points to the first byte of the PBlock Address\n"
557         " *\n"
558         " * Resource Descriptors:\n"
559         " *    Opcode is the descriptor type\n"
560         " *    Offset points to the start of the descriptor\n"
561         " *\n"
562         " * Scopes/Devices/ThermalZones:\n"
563         " *    Nameseg offset only\n"
564         " */\n");
565 
566     FlPrintFile (FileId,
567         "AML_OFFSET_TABLE_ENTRY   %s_%s_OffsetTable[] =\n{\n",
568         Gbl_TableSignature, Gbl_TableId);
569 }
570 
571 
572 void
573 LsDoOffsetTableFooter (
574     UINT32                  FileId)
575 {
576 
577     FlPrintFile (FileId,
578         "    {NULL,0,0,0,0,0} /* Table terminator */\n};\n\n");
579     Gbl_CurrentAmlOffset = 0;
580 }
581