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