Merge branch 'vendor/LIBEDIT'
[dragonfly.git] / sys / contrib / dev / acpica / source / components / dispatcher / dswload.c
1 /******************************************************************************
2  *
3  * Module Name: dswload - Dispatcher first pass namespace load callbacks
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #define __DSWLOAD_C__
45
46 #include "acpi.h"
47 #include "accommon.h"
48 #include "acparser.h"
49 #include "amlcode.h"
50 #include "acdispat.h"
51 #include "acinterp.h"
52 #include "acnamesp.h"
53
54 #ifdef ACPI_ASL_COMPILER
55 #include "acdisasm.h"
56 #endif
57
58 #define _COMPONENT          ACPI_DISPATCHER
59         ACPI_MODULE_NAME    ("dswload")
60
61
62 /*******************************************************************************
63  *
64  * FUNCTION:    AcpiDsInitCallbacks
65  *
66  * PARAMETERS:  WalkState       - Current state of the parse tree walk
67  *              PassNumber      - 1, 2, or 3
68  *
69  * RETURN:      Status
70  *
71  * DESCRIPTION: Init walk state callbacks
72  *
73  ******************************************************************************/
74
75 ACPI_STATUS
76 AcpiDsInitCallbacks (
77     ACPI_WALK_STATE         *WalkState,
78     UINT32                  PassNumber)
79 {
80
81     switch (PassNumber)
82     {
83     case 0:
84
85         /* Parse only - caller will setup callbacks */
86
87         WalkState->ParseFlags         = ACPI_PARSE_LOAD_PASS1 |
88                                         ACPI_PARSE_DELETE_TREE |
89                                         ACPI_PARSE_DISASSEMBLE;
90         WalkState->DescendingCallback = NULL;
91         WalkState->AscendingCallback  = NULL;
92         break;
93
94     case 1:
95
96         /* Load pass 1 */
97
98         WalkState->ParseFlags         = ACPI_PARSE_LOAD_PASS1 |
99                                         ACPI_PARSE_DELETE_TREE;
100         WalkState->DescendingCallback = AcpiDsLoad1BeginOp;
101         WalkState->AscendingCallback  = AcpiDsLoad1EndOp;
102         break;
103
104     case 2:
105
106         /* Load pass 2 */
107
108         WalkState->ParseFlags         = ACPI_PARSE_LOAD_PASS1 |
109                                         ACPI_PARSE_DELETE_TREE;
110         WalkState->DescendingCallback = AcpiDsLoad2BeginOp;
111         WalkState->AscendingCallback  = AcpiDsLoad2EndOp;
112         break;
113
114     case 3:
115
116         /* Execution pass */
117
118 #ifndef ACPI_NO_METHOD_EXECUTION
119         WalkState->ParseFlags        |= ACPI_PARSE_EXECUTE  |
120                                         ACPI_PARSE_DELETE_TREE;
121         WalkState->DescendingCallback = AcpiDsExecBeginOp;
122         WalkState->AscendingCallback  = AcpiDsExecEndOp;
123 #endif
124         break;
125
126     default:
127
128         return (AE_BAD_PARAMETER);
129     }
130
131     return (AE_OK);
132 }
133
134
135 /*******************************************************************************
136  *
137  * FUNCTION:    AcpiDsLoad1BeginOp
138  *
139  * PARAMETERS:  WalkState       - Current state of the parse tree walk
140  *              OutOp           - Where to return op if a new one is created
141  *
142  * RETURN:      Status
143  *
144  * DESCRIPTION: Descending callback used during the loading of ACPI tables.
145  *
146  ******************************************************************************/
147
148 ACPI_STATUS
149 AcpiDsLoad1BeginOp (
150     ACPI_WALK_STATE         *WalkState,
151     ACPI_PARSE_OBJECT       **OutOp)
152 {
153     ACPI_PARSE_OBJECT       *Op;
154     ACPI_NAMESPACE_NODE     *Node;
155     ACPI_STATUS             Status;
156     ACPI_OBJECT_TYPE        ObjectType;
157     char                    *Path;
158     UINT32                  Flags;
159
160
161     ACPI_FUNCTION_TRACE (DsLoad1BeginOp);
162
163
164     Op = WalkState->Op;
165     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p State=%p\n", Op, WalkState));
166
167     /* We are only interested in opcodes that have an associated name */
168
169     if (Op)
170     {
171         if (!(WalkState->OpInfo->Flags & AML_NAMED))
172         {
173             *OutOp = Op;
174             return_ACPI_STATUS (AE_OK);
175         }
176
177         /* Check if this object has already been installed in the namespace */
178
179         if (Op->Common.Node)
180         {
181             *OutOp = Op;
182             return_ACPI_STATUS (AE_OK);
183         }
184     }
185
186     Path = AcpiPsGetNextNamestring (&WalkState->ParserState);
187
188     /* Map the raw opcode into an internal object type */
189
190     ObjectType = WalkState->OpInfo->ObjectType;
191
192     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
193         "State=%p Op=%p [%s]\n", WalkState, Op, AcpiUtGetTypeName (ObjectType)));
194
195     switch (WalkState->Opcode)
196     {
197     case AML_SCOPE_OP:
198         /*
199          * The target name of the Scope() operator must exist at this point so
200          * that we can actually open the scope to enter new names underneath it.
201          * Allow search-to-root for single namesegs.
202          */
203         Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
204                         ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT, WalkState, &(Node));
205 #ifdef ACPI_ASL_COMPILER
206         if (Status == AE_NOT_FOUND)
207         {
208             /*
209              * Table disassembly:
210              * Target of Scope() not found. Generate an External for it, and
211              * insert the name into the namespace.
212              */
213             AcpiDmAddOpToExternalList (Op, Path, ACPI_TYPE_DEVICE, 0, 0);
214             Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
215                        ACPI_IMODE_LOAD_PASS1, ACPI_NS_SEARCH_PARENT,
216                        WalkState, &Node);
217         }
218 #endif
219         if (ACPI_FAILURE (Status))
220         {
221             ACPI_ERROR_NAMESPACE (Path, Status);
222             return_ACPI_STATUS (Status);
223         }
224
225         /*
226          * Check to make sure that the target is
227          * one of the opcodes that actually opens a scope
228          */
229         switch (Node->Type)
230         {
231         case ACPI_TYPE_ANY:
232         case ACPI_TYPE_LOCAL_SCOPE:         /* Scope  */
233         case ACPI_TYPE_DEVICE:
234         case ACPI_TYPE_POWER:
235         case ACPI_TYPE_PROCESSOR:
236         case ACPI_TYPE_THERMAL:
237
238             /* These are acceptable types */
239             break;
240
241         case ACPI_TYPE_INTEGER:
242         case ACPI_TYPE_STRING:
243         case ACPI_TYPE_BUFFER:
244             /*
245              * These types we will allow, but we will change the type.
246              * This enables some existing code of the form:
247              *
248              *  Name (DEB, 0)
249              *  Scope (DEB) { ... }
250              *
251              * Note: silently change the type here. On the second pass,
252              * we will report a warning
253              */
254             ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
255                 "Type override - [%4.4s] had invalid type (%s) "
256                 "for Scope operator, changed to type ANY\n",
257                 AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type)));
258
259             Node->Type = ACPI_TYPE_ANY;
260             WalkState->ScopeInfo->Common.Value = ACPI_TYPE_ANY;
261             break;
262
263         case ACPI_TYPE_METHOD:
264             /*
265              * Allow scope change to root during execution of module-level
266              * code. Root is typed METHOD during this time.
267              */
268             if ((Node == AcpiGbl_RootNode) &&
269                 (WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL))
270             {
271                 break;
272             }
273
274             /*lint -fallthrough */
275
276         default:
277
278             /* All other types are an error */
279
280             ACPI_ERROR ((AE_INFO,
281                 "Invalid type (%s) for target of "
282                 "Scope operator [%4.4s] (Cannot override)",
283                 AcpiUtGetTypeName (Node->Type), AcpiUtGetNodeName (Node)));
284
285             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
286         }
287         break;
288
289     default:
290         /*
291          * For all other named opcodes, we will enter the name into
292          * the namespace.
293          *
294          * Setup the search flags.
295          * Since we are entering a name into the namespace, we do not want to
296          * enable the search-to-root upsearch.
297          *
298          * There are only two conditions where it is acceptable that the name
299          * already exists:
300          *    1) the Scope() operator can reopen a scoping object that was
301          *       previously defined (Scope, Method, Device, etc.)
302          *    2) Whenever we are parsing a deferred opcode (OpRegion, Buffer,
303          *       BufferField, or Package), the name of the object is already
304          *       in the namespace.
305          */
306         if (WalkState->DeferredNode)
307         {
308             /* This name is already in the namespace, get the node */
309
310             Node = WalkState->DeferredNode;
311             Status = AE_OK;
312             break;
313         }
314
315         /*
316          * If we are executing a method, do not create any namespace objects
317          * during the load phase, only during execution.
318          */
319         if (WalkState->MethodNode)
320         {
321             Node = NULL;
322             Status = AE_OK;
323             break;
324         }
325
326         Flags = ACPI_NS_NO_UPSEARCH;
327         if ((WalkState->Opcode != AML_SCOPE_OP) &&
328             (!(WalkState->ParseFlags & ACPI_PARSE_DEFERRED_OP)))
329         {
330             Flags |= ACPI_NS_ERROR_IF_FOUND;
331             ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "[%s] Cannot already exist\n",
332                     AcpiUtGetTypeName (ObjectType)));
333         }
334         else
335         {
336             ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
337                 "[%s] Both Find or Create allowed\n",
338                     AcpiUtGetTypeName (ObjectType)));
339         }
340
341         /*
342          * Enter the named type into the internal namespace. We enter the name
343          * as we go downward in the parse tree. Any necessary subobjects that
344          * involve arguments to the opcode must be created as we go back up the
345          * parse tree later.
346          */
347         Status = AcpiNsLookup (WalkState->ScopeInfo, Path, ObjectType,
348                         ACPI_IMODE_LOAD_PASS1, Flags, WalkState, &Node);
349         if (ACPI_FAILURE (Status))
350         {
351             if (Status == AE_ALREADY_EXISTS)
352             {
353                 /* The name already exists in this scope */
354
355                 if (Node->Flags & ANOBJ_IS_EXTERNAL)
356                 {
357                     /*
358                      * Allow one create on an object or segment that was
359                      * previously declared External
360                      */
361                     Node->Flags &= ~ANOBJ_IS_EXTERNAL;
362                     Node->Type = (UINT8) ObjectType;
363
364                     /* Just retyped a node, probably will need to open a scope */
365
366                     if (AcpiNsOpensScope (ObjectType))
367                     {
368                         Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
369                         if (ACPI_FAILURE (Status))
370                         {
371                             return_ACPI_STATUS (Status);
372                         }
373                     }
374
375                     Status = AE_OK;
376                 }
377             }
378
379             if (ACPI_FAILURE (Status))
380             {
381                 ACPI_ERROR_NAMESPACE (Path, Status);
382                 return_ACPI_STATUS (Status);
383             }
384         }
385         break;
386     }
387
388     /* Common exit */
389
390     if (!Op)
391     {
392         /* Create a new op */
393
394         Op = AcpiPsAllocOp (WalkState->Opcode);
395         if (!Op)
396         {
397             return_ACPI_STATUS (AE_NO_MEMORY);
398         }
399     }
400
401     /* Initialize the op */
402
403 #if (defined (ACPI_NO_METHOD_EXECUTION) || defined (ACPI_CONSTANT_EVAL_ONLY))
404     Op->Named.Path = ACPI_CAST_PTR (UINT8, Path);
405 #endif
406
407     if (Node)
408     {
409         /*
410          * Put the Node in the "op" object that the parser uses, so we
411          * can get it again quickly when this scope is closed
412          */
413         Op->Common.Node = Node;
414         Op->Named.Name = Node->Name.Integer;
415     }
416
417     AcpiPsAppendArg (AcpiPsGetParentScope (&WalkState->ParserState), Op);
418     *OutOp = Op;
419     return_ACPI_STATUS (Status);
420 }
421
422
423 /*******************************************************************************
424  *
425  * FUNCTION:    AcpiDsLoad1EndOp
426  *
427  * PARAMETERS:  WalkState       - Current state of the parse tree walk
428  *
429  * RETURN:      Status
430  *
431  * DESCRIPTION: Ascending callback used during the loading of the namespace,
432  *              both control methods and everything else.
433  *
434  ******************************************************************************/
435
436 ACPI_STATUS
437 AcpiDsLoad1EndOp (
438     ACPI_WALK_STATE         *WalkState)
439 {
440     ACPI_PARSE_OBJECT       *Op;
441     ACPI_OBJECT_TYPE        ObjectType;
442     ACPI_STATUS             Status = AE_OK;
443
444
445     ACPI_FUNCTION_TRACE (DsLoad1EndOp);
446
447
448     Op = WalkState->Op;
449     ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p State=%p\n", Op, WalkState));
450
451     /* We are only interested in opcodes that have an associated name */
452
453     if (!(WalkState->OpInfo->Flags & (AML_NAMED | AML_FIELD)))
454     {
455         return_ACPI_STATUS (AE_OK);
456     }
457
458     /* Get the object type to determine if we should pop the scope */
459
460     ObjectType = WalkState->OpInfo->ObjectType;
461
462 #ifndef ACPI_NO_METHOD_EXECUTION
463     if (WalkState->OpInfo->Flags & AML_FIELD)
464     {
465         /*
466          * If we are executing a method, do not create any namespace objects
467          * during the load phase, only during execution.
468          */
469         if (!WalkState->MethodNode)
470         {
471             if (WalkState->Opcode == AML_FIELD_OP          ||
472                 WalkState->Opcode == AML_BANK_FIELD_OP     ||
473                 WalkState->Opcode == AML_INDEX_FIELD_OP)
474             {
475                 Status = AcpiDsInitFieldObjects (Op, WalkState);
476             }
477         }
478         return_ACPI_STATUS (Status);
479     }
480
481     /*
482      * If we are executing a method, do not create any namespace objects
483      * during the load phase, only during execution.
484      */
485     if (!WalkState->MethodNode)
486     {
487         if (Op->Common.AmlOpcode == AML_REGION_OP)
488         {
489             Status = AcpiExCreateRegion (Op->Named.Data, Op->Named.Length,
490                         (ACPI_ADR_SPACE_TYPE) ((Op->Common.Value.Arg)->Common.Value.Integer),
491                         WalkState);
492             if (ACPI_FAILURE (Status))
493             {
494                 return_ACPI_STATUS (Status);
495             }
496         }
497         else if (Op->Common.AmlOpcode == AML_DATA_REGION_OP)
498         {
499             Status = AcpiExCreateRegion (Op->Named.Data, Op->Named.Length,
500                         ACPI_ADR_SPACE_DATA_TABLE, WalkState);
501             if (ACPI_FAILURE (Status))
502             {
503                 return_ACPI_STATUS (Status);
504             }
505         }
506     }
507 #endif
508
509     if (Op->Common.AmlOpcode == AML_NAME_OP)
510     {
511         /* For Name opcode, get the object type from the argument */
512
513         if (Op->Common.Value.Arg)
514         {
515             ObjectType = (AcpiPsGetOpcodeInfo (
516                 (Op->Common.Value.Arg)->Common.AmlOpcode))->ObjectType;
517
518             /* Set node type if we have a namespace node */
519
520             if (Op->Common.Node)
521             {
522                 Op->Common.Node->Type = (UINT8) ObjectType;
523             }
524         }
525     }
526
527     /*
528      * If we are executing a method, do not create any namespace objects
529      * during the load phase, only during execution.
530      */
531     if (!WalkState->MethodNode)
532     {
533         if (Op->Common.AmlOpcode == AML_METHOD_OP)
534         {
535             /*
536              * MethodOp PkgLength NameString MethodFlags TermList
537              *
538              * Note: We must create the method node/object pair as soon as we
539              * see the method declaration. This allows later pass1 parsing
540              * of invocations of the method (need to know the number of
541              * arguments.)
542              */
543             ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
544                 "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
545                 WalkState, Op, Op->Named.Node));
546
547             if (!AcpiNsGetAttachedObject (Op->Named.Node))
548             {
549                 WalkState->Operands[0] = ACPI_CAST_PTR (void, Op->Named.Node);
550                 WalkState->NumOperands = 1;
551
552                 Status = AcpiDsCreateOperands (WalkState, Op->Common.Value.Arg);
553                 if (ACPI_SUCCESS (Status))
554                 {
555                     Status = AcpiExCreateMethod (Op->Named.Data,
556                                         Op->Named.Length, WalkState);
557                 }
558
559                 WalkState->Operands[0] = NULL;
560                 WalkState->NumOperands = 0;
561
562                 if (ACPI_FAILURE (Status))
563                 {
564                     return_ACPI_STATUS (Status);
565                 }
566             }
567         }
568     }
569
570     /* Pop the scope stack (only if loading a table) */
571
572     if (!WalkState->MethodNode &&
573         AcpiNsOpensScope (ObjectType))
574     {
575         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "(%s): Popping scope for Op %p\n",
576             AcpiUtGetTypeName (ObjectType), Op));
577
578         Status = AcpiDsScopeStackPop (WalkState);
579     }
580
581     return_ACPI_STATUS (Status);
582 }