xref: /freebsd/sys/contrib/dev/acpica/compiler/aslload.c (revision 8657387683946d0c03e09fe77029edfe309eeb20)
1 /******************************************************************************
2  *
3  * Module Name: dswload - Dispatcher namespace load callbacks
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 <contrib/dev/acpica/include/amlcode.h>
154 #include <contrib/dev/acpica/include/acdispat.h>
155 #include <contrib/dev/acpica/include/acnamesp.h>
156 
157 #include "aslcompiler.y.h"
158 
159 #define _COMPONENT          ACPI_COMPILER
160         ACPI_MODULE_NAME    ("aslload")
161 
162 /* Local prototypes */
163 
164 static ACPI_STATUS
165 LdLoadFieldElements (
166     ACPI_PARSE_OBJECT       *Op,
167     ACPI_WALK_STATE         *WalkState);
168 
169 static ACPI_STATUS
170 LdLoadResourceElements (
171     ACPI_PARSE_OBJECT       *Op,
172     ACPI_WALK_STATE         *WalkState);
173 
174 static ACPI_STATUS
175 LdNamespace1Begin (
176     ACPI_PARSE_OBJECT       *Op,
177     UINT32                  Level,
178     void                    *Context);
179 
180 static ACPI_STATUS
181 LdNamespace2Begin (
182     ACPI_PARSE_OBJECT       *Op,
183     UINT32                  Level,
184     void                    *Context);
185 
186 static ACPI_STATUS
187 LdCommonNamespaceEnd (
188     ACPI_PARSE_OBJECT       *Op,
189     UINT32                  Level,
190     void                    *Context);
191 
192 
193 /*******************************************************************************
194  *
195  * FUNCTION:    LdLoadNamespace
196  *
197  * PARAMETERS:  RootOp      - Root of the parse tree
198  *
199  * RETURN:      Status
200  *
201  * DESCRIPTION: Perform a walk of the parse tree that in turn loads all of the
202  *              named ASL/AML objects into the namespace. The namespace is
203  *              constructed in order to resolve named references and references
204  *              to named fields within resource templates/descriptors.
205  *
206  ******************************************************************************/
207 
208 ACPI_STATUS
209 LdLoadNamespace (
210     ACPI_PARSE_OBJECT       *RootOp)
211 {
212     ACPI_WALK_STATE         *WalkState;
213 
214 
215     /* Create a new walk state */
216 
217     WalkState = AcpiDsCreateWalkState (0, NULL, NULL, NULL);
218     if (!WalkState)
219     {
220         return (AE_NO_MEMORY);
221     }
222 
223     /* Walk the entire parse tree, first pass */
224 
225     TrWalkParseTree (RootOp, ASL_WALK_VISIT_TWICE, LdNamespace1Begin,
226         LdCommonNamespaceEnd, WalkState);
227 
228     /* Second pass to handle forward references */
229 
230     TrWalkParseTree (RootOp, ASL_WALK_VISIT_TWICE, LdNamespace2Begin,
231         LdCommonNamespaceEnd, WalkState);
232 
233     /* Dump the namespace if debug is enabled */
234 
235     AcpiNsDumpTables (ACPI_NS_ALL, ACPI_UINT32_MAX);
236     ACPI_FREE (WalkState);
237     return (AE_OK);
238 }
239 
240 
241 /*******************************************************************************
242  *
243  * FUNCTION:    LdLoadFieldElements
244  *
245  * PARAMETERS:  Op              - Parent node (Field)
246  *              WalkState       - Current walk state
247  *
248  * RETURN:      Status
249  *
250  * DESCRIPTION: Enter the named elements of the field (children of the parent)
251  *              into the namespace.
252  *
253  ******************************************************************************/
254 
255 static ACPI_STATUS
256 LdLoadFieldElements (
257     ACPI_PARSE_OBJECT       *Op,
258     ACPI_WALK_STATE         *WalkState)
259 {
260     ACPI_PARSE_OBJECT       *Child = NULL;
261     ACPI_NAMESPACE_NODE     *Node;
262     ACPI_STATUS             Status;
263 
264 
265     /* Get the first named field element */
266 
267     switch (Op->Asl.AmlOpcode)
268     {
269     case AML_BANK_FIELD_OP:
270 
271         Child = UtGetArg (Op, 6);
272         break;
273 
274     case AML_INDEX_FIELD_OP:
275 
276         Child = UtGetArg (Op, 5);
277         break;
278 
279     case AML_FIELD_OP:
280 
281         Child = UtGetArg (Op, 4);
282         break;
283 
284     default:
285 
286         /* No other opcodes should arrive here */
287 
288         return (AE_BAD_PARAMETER);
289     }
290 
291     /* Enter all elements into the namespace */
292 
293     while (Child)
294     {
295         switch (Child->Asl.AmlOpcode)
296         {
297         case AML_INT_RESERVEDFIELD_OP:
298         case AML_INT_ACCESSFIELD_OP:
299         case AML_INT_CONNECTION_OP:
300             break;
301 
302         default:
303 
304             Status = AcpiNsLookup (WalkState->ScopeInfo,
305                 Child->Asl.Value.String,
306                 ACPI_TYPE_LOCAL_REGION_FIELD,
307                 ACPI_IMODE_LOAD_PASS1,
308                 ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE |
309                     ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
310             if (ACPI_FAILURE (Status))
311             {
312                 if (Status != AE_ALREADY_EXISTS)
313                 {
314                     AslError (ASL_ERROR, ASL_MSG_CORE_EXCEPTION, Child,
315                         Child->Asl.Value.String);
316                     return (Status);
317                 }
318                 else if (Status == AE_ALREADY_EXISTS &&
319                     (Node->Flags & ANOBJ_IS_EXTERNAL) &&
320                     Node->OwnerId != WalkState->OwnerId)
321                 {
322                     Node->Type = (UINT8) ACPI_TYPE_LOCAL_REGION_FIELD;
323                 }
324                 else
325                 {
326                     /*
327                      * The name already exists in this scope
328                      * But continue processing the elements
329                      */
330                     AslError (ASL_ERROR, ASL_MSG_NAME_EXISTS, Child,
331                         Child->Asl.Value.String);
332                 }
333             }
334             else
335             {
336                 Child->Asl.Node = Node;
337                 Node->Op = Child;
338             }
339             break;
340         }
341 
342         Child = Child->Asl.Next;
343     }
344 
345     return (AE_OK);
346 }
347 
348 
349 /*******************************************************************************
350  *
351  * FUNCTION:    LdLoadResourceElements
352  *
353  * PARAMETERS:  Op              - Parent node (Resource Descriptor)
354  *              WalkState       - Current walk state
355  *
356  * RETURN:      Status
357  *
358  * DESCRIPTION: Enter the named elements of the resource descriptor (children
359  *              of the parent) into the namespace.
360  *
361  * NOTE: In the real AML namespace, these named elements never exist. But
362  *       we simply use the namespace here as a symbol table so we can look
363  *       them up as they are referenced.
364  *
365  ******************************************************************************/
366 
367 static ACPI_STATUS
368 LdLoadResourceElements (
369     ACPI_PARSE_OBJECT       *Op,
370     ACPI_WALK_STATE         *WalkState)
371 {
372     ACPI_PARSE_OBJECT       *InitializerOp = NULL;
373     ACPI_NAMESPACE_NODE     *Node;
374     ACPI_STATUS             Status;
375 
376 
377     /*
378      * Enter the resource name into the namespace. Name must not already exist.
379      * This opens a scope, so later field names are guaranteed to be new/unique.
380      */
381     Status = AcpiNsLookup (WalkState->ScopeInfo, Op->Asl.Namepath,
382         ACPI_TYPE_LOCAL_RESOURCE, ACPI_IMODE_LOAD_PASS1,
383         ACPI_NS_NO_UPSEARCH | ACPI_NS_ERROR_IF_FOUND,
384         WalkState, &Node);
385     if (ACPI_FAILURE (Status))
386     {
387         if (Status == AE_ALREADY_EXISTS)
388         {
389             /* Actual node causing the error was saved in ParentMethod */
390 
391             AslError (ASL_ERROR, ASL_MSG_NAME_EXISTS,
392                 (ACPI_PARSE_OBJECT *) Op->Asl.ParentMethod, Op->Asl.Namepath);
393             return (AE_OK);
394         }
395         return (Status);
396     }
397 
398     Node->Value = (UINT32) Op->Asl.Value.Integer;
399     Node->Op = Op;
400     Op->Asl.Node = Node;
401 
402     /*
403      * Now enter the predefined fields, for easy lookup when referenced
404      * by the source ASL
405      */
406     InitializerOp = ASL_GET_CHILD_NODE (Op);
407     while (InitializerOp)
408     {
409         if (InitializerOp->Asl.ExternalName)
410         {
411             Status = AcpiNsLookup (WalkState->ScopeInfo,
412                 InitializerOp->Asl.ExternalName,
413                 ACPI_TYPE_LOCAL_RESOURCE_FIELD,
414                 ACPI_IMODE_LOAD_PASS1,
415                 ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE,
416                 NULL, &Node);
417             if (ACPI_FAILURE (Status))
418             {
419                 return (Status);
420             }
421 
422             /*
423              * Store the field offset and length in the namespace node
424              * so it can be used when the field is referenced
425              */
426             Node->Value = InitializerOp->Asl.Value.Tag.BitOffset;
427             Node->Length = InitializerOp->Asl.Value.Tag.BitLength;
428             InitializerOp->Asl.Node = Node;
429             Node->Op = InitializerOp;
430         }
431 
432         InitializerOp = ASL_GET_PEER_NODE (InitializerOp);
433     }
434 
435     return (AE_OK);
436 }
437 
438 
439 /*******************************************************************************
440  *
441  * FUNCTION:    LdNamespace1Begin
442  *
443  * PARAMETERS:  ASL_WALK_CALLBACK
444  *
445  * RETURN:      Status
446  *
447  * DESCRIPTION: Descending callback used during the parse tree walk. If this
448  *              is a named AML opcode, enter into the namespace
449  *
450  ******************************************************************************/
451 
452 static ACPI_STATUS
453 LdNamespace1Begin (
454     ACPI_PARSE_OBJECT       *Op,
455     UINT32                  Level,
456     void                    *Context)
457 {
458     ACPI_WALK_STATE         *WalkState = (ACPI_WALK_STATE *) Context;
459     ACPI_NAMESPACE_NODE     *Node;
460     ACPI_PARSE_OBJECT       *MethodOp;
461     ACPI_STATUS             Status;
462     ACPI_OBJECT_TYPE        ObjectType;
463     ACPI_OBJECT_TYPE        ActualObjectType = ACPI_TYPE_ANY;
464     char                    *Path;
465     UINT32                  Flags = ACPI_NS_NO_UPSEARCH;
466     ACPI_PARSE_OBJECT       *Arg;
467     UINT32                  i;
468     BOOLEAN                 ForceNewScope = FALSE;
469     ACPI_OWNER_ID           OwnerId = 0;
470 
471 
472     ACPI_FUNCTION_NAME (LdNamespace1Begin);
473     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op %p [%s]\n",
474         Op, Op->Asl.ParseOpName));
475 
476     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
477     {
478         /*
479          * Allocate an OwnerId for this block. This helps identify the owners
480          * of each namespace node. This is used in determining whether if
481          * certain external declarations cause redefinition errors.
482          */
483         Status = AcpiUtAllocateOwnerId (&OwnerId);
484         WalkState->OwnerId = OwnerId;
485         if (ACPI_FAILURE (Status))
486         {
487             AslCoreSubsystemError (Op, Status,
488                 "Failure to allocate owner ID to this definition block.", FALSE);
489             return_ACPI_STATUS (Status);
490         }
491     }
492 
493     /*
494      * We are only interested in opcodes that have an associated name
495      * (or multiple names)
496      */
497     switch (Op->Asl.AmlOpcode)
498     {
499     case AML_BANK_FIELD_OP:
500     case AML_INDEX_FIELD_OP:
501     case AML_FIELD_OP:
502 
503         Status = LdLoadFieldElements (Op, WalkState);
504         return (Status);
505 
506     case AML_INT_CONNECTION_OP:
507 
508 
509         if (Op->Asl.Child->Asl.AmlOpcode != AML_INT_NAMEPATH_OP)
510         {
511             break;
512         }
513         Arg = Op->Asl.Child;
514 
515         Status = AcpiNsLookup (WalkState->ScopeInfo, Arg->Asl.ExternalName,
516             ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
517             WalkState, &Node);
518         if (ACPI_FAILURE (Status))
519         {
520             break;
521         }
522 
523         if (Node->Type == ACPI_TYPE_BUFFER)
524         {
525             Arg->Asl.Node = Node;
526 
527             Arg = Node->Op->Asl.Child;  /* Get namepath */
528             Arg = Arg->Asl.Next;        /* Get actual buffer */
529             Arg = Arg->Asl.Child;       /* Buffer length */
530             Arg = Arg->Asl.Next;        /* RAW_DATA buffer */
531         }
532         break;
533 
534     default:
535 
536         /* All other opcodes go below */
537 
538         break;
539     }
540 
541     /* Check if this object has already been installed in the namespace */
542 
543     if (Op->Asl.Node)
544     {
545         return (AE_OK);
546     }
547 
548     Path = Op->Asl.Namepath;
549     if (!Path)
550     {
551         return (AE_OK);
552     }
553 
554     /* Map the raw opcode into an internal object type */
555 
556     switch (Op->Asl.ParseOpcode)
557     {
558     case PARSEOP_NAME:
559 
560         Arg = Op->Asl.Child;  /* Get the NameSeg/NameString node */
561         Arg = Arg->Asl.Next;  /* First peer is the object to be associated with the name */
562 
563         /*
564          * If this name refers to a ResourceTemplate, we will need to open
565          * a new scope so that the resource subfield names can be entered into
566          * the namespace underneath this name
567          */
568         if (Op->Asl.CompileFlags & OP_IS_RESOURCE_DESC)
569         {
570             ForceNewScope = TRUE;
571         }
572 
573         /* Get the data type associated with the named object, not the name itself */
574 
575         /* Log2 loop to convert from Btype (binary) to Etype (encoded) */
576 
577         ObjectType = 1;
578         for (i = 1; i < Arg->Asl.AcpiBtype; i *= 2)
579         {
580             ObjectType++;
581         }
582         break;
583 
584 
585     case PARSEOP_EXTERNAL:
586         /*
587          * "External" simply enters a name and type into the namespace.
588          * We must be careful to not open a new scope, however, no matter
589          * what type the external name refers to (e.g., a method)
590          *
591          * first child is name, next child is ObjectType
592          */
593         ActualObjectType = (UINT8) Op->Asl.Child->Asl.Next->Asl.Value.Integer;
594         ObjectType = ACPI_TYPE_ANY;
595 
596         /*
597          * We will mark every new node along the path as "External". This
598          * allows some or all of the nodes to be created later in the ASL
599          * code. Handles cases like this:
600          *
601          *   External (\_SB_.PCI0.ABCD, IntObj)
602          *   Scope (_SB_)
603          *   {
604          *       Device (PCI0)
605          *       {
606          *       }
607          *   }
608          *   Method (X)
609          *   {
610          *       Store (\_SB_.PCI0.ABCD, Local0)
611          *   }
612          */
613         Flags |= ACPI_NS_EXTERNAL;
614         break;
615 
616     case PARSEOP_DEFAULT_ARG:
617 
618         if (Op->Asl.CompileFlags == OP_IS_RESOURCE_DESC)
619         {
620             Status = LdLoadResourceElements (Op, WalkState);
621             return_ACPI_STATUS (Status);
622         }
623 
624         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
625         break;
626 
627     case PARSEOP_SCOPE:
628         /*
629          * The name referenced by Scope(Name) must already exist at this point.
630          * In other words, forward references for Scope() are not supported.
631          * The only real reason for this is that the MS interpreter cannot
632          * handle this case. Perhaps someday this case can go away.
633          */
634         Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ACPI_TYPE_ANY,
635             ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
636             WalkState, &(Node));
637         if (ACPI_FAILURE (Status))
638         {
639             if (Status == AE_NOT_FOUND)
640             {
641                 /* The name was not found, go ahead and create it */
642 
643                 Status = AcpiNsLookup (WalkState->ScopeInfo, Path,
644                     ACPI_TYPE_LOCAL_SCOPE,
645                     ACPI_IMODE_LOAD_PASS1, Flags,
646                     WalkState, &(Node));
647                 if (ACPI_FAILURE (Status))
648                 {
649                     return_ACPI_STATUS (Status);
650                 }
651 
652                 /*
653                  * However, this is an error -- primarily because the MS
654                  * interpreter can't handle a forward reference from the
655                  * Scope() operator.
656                  */
657                 AslError (ASL_ERROR, ASL_MSG_NOT_FOUND, Op,
658                     Op->Asl.ExternalName);
659                 AslError (ASL_ERROR, ASL_MSG_SCOPE_FWD_REF, Op,
660                     Op->Asl.ExternalName);
661                 goto FinishNode;
662             }
663 
664             AslCoreSubsystemError (Op, Status,
665                 "Failure from namespace lookup", FALSE);
666 
667             return_ACPI_STATUS (Status);
668         }
669         else /* Status AE_OK */
670         {
671             /*
672              * Do not allow references to external scopes from the DSDT.
673              * This is because the DSDT is always loaded first, and the
674              * external reference cannot be resolved -- causing a runtime
675              * error because Scope() must be resolved immediately.
676              * 10/2015.
677              */
678             if ((Node->Flags & ANOBJ_IS_EXTERNAL) &&
679                 (ACPI_COMPARE_NAME (Gbl_TableSignature, "DSDT")))
680             {
681                 /* However, allowed if the reference is within a method */
682 
683                 MethodOp = Op->Asl.Parent;
684                 while (MethodOp &&
685                       (MethodOp->Asl.ParseOpcode != PARSEOP_METHOD))
686                 {
687                     MethodOp = MethodOp->Asl.Parent;
688                 }
689 
690                 if (!MethodOp)
691                 {
692                     /* Not in a control method, error */
693 
694                     AslError (ASL_ERROR, ASL_MSG_CROSS_TABLE_SCOPE, Op, NULL);
695                 }
696             }
697         }
698 
699         /* We found a node with this name, now check the type */
700 
701         switch (Node->Type)
702         {
703         case ACPI_TYPE_LOCAL_SCOPE:
704         case ACPI_TYPE_DEVICE:
705         case ACPI_TYPE_POWER:
706         case ACPI_TYPE_PROCESSOR:
707         case ACPI_TYPE_THERMAL:
708 
709             /* These are acceptable types - they all open a new scope */
710             break;
711 
712         case ACPI_TYPE_INTEGER:
713         case ACPI_TYPE_STRING:
714         case ACPI_TYPE_BUFFER:
715             /*
716              * These types we will allow, but we will change the type.
717              * This enables some existing code of the form:
718              *
719              *  Name (DEB, 0)
720              *  Scope (DEB) { ... }
721              *
722              * Which is used to workaround the fact that the MS interpreter
723              * does not allow Scope() forward references.
724              */
725             sprintf (MsgBuffer, "%s [%s], changing type to [Scope]",
726                 Op->Asl.ExternalName, AcpiUtGetTypeName (Node->Type));
727             AslError (ASL_REMARK, ASL_MSG_SCOPE_TYPE, Op, MsgBuffer);
728 
729             /* Switch the type to scope, open the new scope */
730 
731             Node->Type = ACPI_TYPE_LOCAL_SCOPE;
732             Status = AcpiDsScopeStackPush (Node, ACPI_TYPE_LOCAL_SCOPE,
733                 WalkState);
734             if (ACPI_FAILURE (Status))
735             {
736                 return_ACPI_STATUS (Status);
737             }
738             break;
739 
740         default:
741 
742             /* All other types are an error */
743 
744             sprintf (MsgBuffer, "%s [%s]", Op->Asl.ExternalName,
745                 AcpiUtGetTypeName (Node->Type));
746             AslError (ASL_ERROR, ASL_MSG_SCOPE_TYPE, Op, MsgBuffer);
747 
748             /*
749              * However, switch the type to be an actual scope so
750              * that compilation can continue without generating a whole
751              * cascade of additional errors. Open the new scope.
752              */
753             Node->Type = ACPI_TYPE_LOCAL_SCOPE;
754             Status = AcpiDsScopeStackPush (Node, ACPI_TYPE_LOCAL_SCOPE,
755                 WalkState);
756             if (ACPI_FAILURE (Status))
757             {
758                 return_ACPI_STATUS (Status);
759             }
760             break;
761         }
762 
763         Status = AE_OK;
764         goto FinishNode;
765 
766 
767     default:
768 
769         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
770         break;
771     }
772 
773 
774     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Loading name: %s, (%s)\n",
775         Op->Asl.ExternalName, AcpiUtGetTypeName (ObjectType)));
776 
777     /* The name must not already exist */
778 
779     Flags |= ACPI_NS_ERROR_IF_FOUND;
780 
781     /*
782      * Enter the named type into the internal namespace. We enter the name
783      * as we go downward in the parse tree. Any necessary subobjects that
784      * involve arguments to the opcode must be created as we go back up the
785      * parse tree later.
786      */
787     Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
788         ACPI_IMODE_LOAD_PASS1, Flags, WalkState, &Node);
789     if (ACPI_FAILURE (Status))
790     {
791         if (Status == AE_ALREADY_EXISTS)
792         {
793             /* The name already exists in this scope */
794 
795             if (Node->Type == ACPI_TYPE_LOCAL_SCOPE)
796             {
797                 /* Allow multiple references to the same scope */
798 
799                 Node->Type = (UINT8) ObjectType;
800                 Status = AE_OK;
801             }
802             else if ((Node->Flags & ANOBJ_IS_EXTERNAL) &&
803                      (Op->Asl.ParseOpcode != PARSEOP_EXTERNAL))
804             {
805                 /*
806                  * Allow one create on an object or segment that was
807                  * previously declared External only if WalkState->OwnerId and
808                  * Node->OwnerId are found in different tables (meaning that
809                  * they have differnt OwnerIds).
810                  */
811                 Node->Flags &= ~ANOBJ_IS_EXTERNAL;
812                 Node->Type = (UINT8) ObjectType;
813 
814                 /* Just retyped a node, probably will need to open a scope */
815 
816                 if (AcpiNsOpensScope (ObjectType))
817                 {
818                     Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
819                     if (ACPI_FAILURE (Status))
820                     {
821                         return_ACPI_STATUS (Status);
822                     }
823                 }
824 
825                 Status = AE_OK;
826 
827                 if (Node->OwnerId == WalkState->OwnerId &&
828                     !(Node->Flags & IMPLICIT_EXTERNAL))
829                 {
830                     AslError (ASL_ERROR, ASL_MSG_NAME_EXISTS, Op,
831                         Op->Asl.ExternalName);
832                 }
833                 if (Node->Flags & IMPLICIT_EXTERNAL)
834                 {
835                     Node->Flags &= ~IMPLICIT_EXTERNAL;
836                 }
837             }
838             else if (!(Node->Flags & ANOBJ_IS_EXTERNAL) &&
839                      (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL))
840             {
841                 /*
842                  * Allow externals in same scope as the definition of the
843                  * actual object. Similar to C. Allows multiple definition
844                  * blocks that refer to each other in the same file. However,
845                  * do not allow name declaration and an external declaration
846                  * within the same table. This is considered a re-declaration.
847                  */
848                 Status = AE_OK;
849 
850                 if (Node->OwnerId == WalkState->OwnerId)
851                 {
852                     AslError (ASL_ERROR, ASL_MSG_NAME_EXISTS, Op,
853                         Op->Asl.ExternalName);
854                 }
855             }
856             else if ((Node->Flags & ANOBJ_IS_EXTERNAL) &&
857                      (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) &&
858                      (ObjectType == ACPI_TYPE_ANY))
859             {
860                 /*
861                  * Allow update of externals of unknown type.
862                  * In the case that multiple definition blocks are being
863                  * parsed, updating the OwnerId allows enables subsequent calls
864                  * of this method to understand which table the most recent
865                  * external declaration was seen. Without this OwnerId update,
866                  * code like the following is allowed to compile:
867                  *
868                  * DefinitionBlock("externtest.aml", "DSDT", 0x02, "Intel", "Many", 0x00000001)
869                  * {
870                  *     External(ERRS,methodobj)
871                  *     Method (MAIN)
872                  *     {
873                  *         Name(NUM2, 0)
874                  *         ERRS(1,2,3)
875                  *     }
876                  * }
877                  *
878                  * DefinitionBlock("externtest.aml", "SSDT", 0x02, "Intel", "Many", 0x00000001)
879                  * {
880                  *     if (0)
881                  *     {
882                  *         External(ERRS,methodobj)
883                  *     }
884                  *     Method (ERRS,3)
885                  *     {}
886                  *
887                  * }
888                  */
889                 Node->OwnerId = WalkState->OwnerId;
890 
891                 if (AcpiNsOpensScope (ActualObjectType))
892                 {
893                     Node->Type = (UINT8) ActualObjectType;
894                     Status = AE_OK;
895                 }
896                 else
897                 {
898                     sprintf (MsgBuffer, "%s [%s]", Op->Asl.ExternalName,
899                         AcpiUtGetTypeName (Node->Type));
900                     AslError (ASL_ERROR, ASL_MSG_SCOPE_TYPE, Op, MsgBuffer);
901                     return_ACPI_STATUS (AE_OK);
902                 }
903             }
904             else
905             {
906                 /* Valid error, object already exists */
907 
908                 AslError (ASL_ERROR, ASL_MSG_NAME_EXISTS, Op,
909                     Op->Asl.ExternalName);
910                 return_ACPI_STATUS (AE_OK);
911             }
912         }
913         else
914         {
915             AslCoreSubsystemError (Op, Status,
916                 "Failure from namespace lookup", FALSE);
917             return_ACPI_STATUS (Status);
918         }
919     }
920 
921     if (ForceNewScope)
922     {
923         Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
924         if (ACPI_FAILURE (Status))
925         {
926             return_ACPI_STATUS (Status);
927         }
928     }
929 
930 FinishNode:
931     /*
932      * Point the parse node to the new namespace node, and point
933      * the Node back to the original Parse node
934      */
935     Op->Asl.Node = Node;
936     Node->Op = Op;
937 
938     /* Set the actual data type if appropriate (EXTERNAL term only) */
939 
940     if (ActualObjectType != ACPI_TYPE_ANY)
941     {
942         Node->Type = (UINT8) ActualObjectType;
943         Node->Value = ASL_EXTERNAL_METHOD;
944     }
945 
946     if (Op->Asl.ParseOpcode == PARSEOP_METHOD)
947     {
948         /*
949          * Get the method argument count from "Extra" and save
950          * it in the namespace node
951          */
952         Node->Value = (UINT32) Op->Asl.Extra;
953     }
954 
955     return_ACPI_STATUS (Status);
956 }
957 
958 
959 /*******************************************************************************
960  *
961  * FUNCTION:    LdNamespace2Begin
962  *
963  * PARAMETERS:  ASL_WALK_CALLBACK
964  *
965  * RETURN:      Status
966  *
967  * DESCRIPTION: Descending callback used during the pass 2 parse tree walk.
968  *              Second pass resolves some forward references.
969  *
970  * Notes:
971  * Currently only needs to handle the Alias operator.
972  * Could be used to allow forward references from the Scope() operator, but
973  * the MS interpreter does not allow this, so this compiler does not either.
974  *
975  ******************************************************************************/
976 
977 static ACPI_STATUS
978 LdNamespace2Begin (
979     ACPI_PARSE_OBJECT       *Op,
980     UINT32                  Level,
981     void                    *Context)
982 {
983     ACPI_WALK_STATE         *WalkState = (ACPI_WALK_STATE *) Context;
984     ACPI_STATUS             Status;
985     ACPI_NAMESPACE_NODE     *Node;
986     ACPI_OBJECT_TYPE        ObjectType;
987     BOOLEAN                 ForceNewScope = FALSE;
988     ACPI_PARSE_OBJECT       *Arg;
989     char                    *Path;
990     ACPI_NAMESPACE_NODE     *TargetNode;
991 
992 
993     ACPI_FUNCTION_NAME (LdNamespace2Begin);
994     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op %p [%s]\n",
995         Op, Op->Asl.ParseOpName));
996 
997 
998     /* Ignore Ops with no namespace node */
999 
1000     Node = Op->Asl.Node;
1001     if (!Node)
1002     {
1003         return (AE_OK);
1004     }
1005 
1006     /* Get the type to determine if we should push the scope */
1007 
1008     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) &&
1009         (Op->Asl.CompileFlags == OP_IS_RESOURCE_DESC))
1010     {
1011         ObjectType = ACPI_TYPE_LOCAL_RESOURCE;
1012     }
1013     else
1014     {
1015         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
1016     }
1017 
1018     /* Push scope for Resource Templates */
1019 
1020     if (Op->Asl.ParseOpcode == PARSEOP_NAME)
1021     {
1022         if (Op->Asl.CompileFlags & OP_IS_RESOURCE_DESC)
1023         {
1024             ForceNewScope = TRUE;
1025         }
1026     }
1027 
1028     /* Push the scope stack */
1029 
1030     if (ForceNewScope || AcpiNsOpensScope (ObjectType))
1031     {
1032         Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
1033         if (ACPI_FAILURE (Status))
1034         {
1035             return_ACPI_STATUS (Status);
1036         }
1037     }
1038 
1039     if (Op->Asl.ParseOpcode == PARSEOP_ALIAS)
1040     {
1041         /* Complete the alias node by getting and saving the target node */
1042 
1043         /* First child is the alias target */
1044 
1045         Arg = Op->Asl.Child;
1046 
1047         /* Get the target pathname */
1048 
1049         Path = Arg->Asl.Namepath;
1050         if (!Path)
1051         {
1052             Status = UtInternalizeName (Arg->Asl.ExternalName, &Path);
1053             if (ACPI_FAILURE (Status))
1054             {
1055                 return (Status);
1056             }
1057         }
1058 
1059         /* Get the NS node associated with the target. It must exist. */
1060 
1061         Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ACPI_TYPE_ANY,
1062             ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
1063             WalkState, &TargetNode);
1064         if (ACPI_FAILURE (Status))
1065         {
1066             if (Status == AE_NOT_FOUND)
1067             {
1068                 AslError (ASL_ERROR, ASL_MSG_NOT_FOUND, Op,
1069                     Op->Asl.ExternalName);
1070 
1071                 /*
1072                  * The name was not found, go ahead and create it.
1073                  * This prevents more errors later.
1074                  */
1075                 Status = AcpiNsLookup (WalkState->ScopeInfo, Path,
1076                     ACPI_TYPE_ANY,
1077                     ACPI_IMODE_LOAD_PASS1, ACPI_NS_NO_UPSEARCH,
1078                     WalkState, &(Node));
1079                 return (AE_OK);
1080             }
1081 
1082             AslCoreSubsystemError (Op, Status,
1083                 "Failure from namespace lookup", FALSE);
1084             return (AE_OK);
1085         }
1086 
1087         /* Save the target node within the alias node */
1088 
1089         Node->Object = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, TargetNode);
1090     }
1091 
1092     return (AE_OK);
1093 }
1094 
1095 
1096 /*******************************************************************************
1097  *
1098  * FUNCTION:    LdCommonNamespaceEnd
1099  *
1100  * PARAMETERS:  ASL_WALK_CALLBACK
1101  *
1102  * RETURN:      Status
1103  *
1104  * DESCRIPTION: Ascending callback used during the loading of the namespace,
1105  *              We only need to worry about managing the scope stack here.
1106  *
1107  ******************************************************************************/
1108 
1109 static ACPI_STATUS
1110 LdCommonNamespaceEnd (
1111     ACPI_PARSE_OBJECT       *Op,
1112     UINT32                  Level,
1113     void                    *Context)
1114 {
1115     ACPI_WALK_STATE         *WalkState = (ACPI_WALK_STATE *) Context;
1116     ACPI_OBJECT_TYPE        ObjectType;
1117     BOOLEAN                 ForceNewScope = FALSE;
1118 
1119 
1120     ACPI_FUNCTION_NAME (LdCommonNamespaceEnd);
1121 
1122 
1123     /* We are only interested in opcodes that have an associated name */
1124 
1125     if (!Op->Asl.Namepath)
1126     {
1127         return (AE_OK);
1128     }
1129 
1130     /* Get the type to determine if we should pop the scope */
1131 
1132     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) &&
1133         (Op->Asl.CompileFlags == OP_IS_RESOURCE_DESC))
1134     {
1135         /* TBD: Merge into AcpiDsMapNamedOpcodeToDataType */
1136 
1137         ObjectType = ACPI_TYPE_LOCAL_RESOURCE;
1138     }
1139     else
1140     {
1141         ObjectType = AslMapNamedOpcodeToDataType (Op->Asl.AmlOpcode);
1142     }
1143 
1144     /* Pop scope that was pushed for Resource Templates */
1145 
1146     if (Op->Asl.ParseOpcode == PARSEOP_NAME)
1147     {
1148         if (Op->Asl.CompileFlags & OP_IS_RESOURCE_DESC)
1149         {
1150             ForceNewScope = TRUE;
1151         }
1152     }
1153 
1154     /* Pop the scope stack */
1155 
1156     if (ForceNewScope || AcpiNsOpensScope (ObjectType))
1157     {
1158         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
1159             "(%s): Popping scope for Op [%s] %p\n",
1160             AcpiUtGetTypeName (ObjectType), Op->Asl.ParseOpName, Op));
1161 
1162         (void) AcpiDsScopeStackPop (WalkState);
1163     }
1164 
1165     return (AE_OK);
1166 }
1167