Sync ACPICA with Intel's version 20170224.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / dispatcher / dswload2.c
blob4985f2078cfe556514c2a519021f169a6391b6b6
1 /******************************************************************************
3 * Module Name: dswload2 - Dispatcher second pass namespace load callbacks
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2017, Intel Corp.
9 * All rights reserved.
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.
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.
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.
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 #include "acevents.h"
53 #define _COMPONENT ACPI_DISPATCHER
54 ACPI_MODULE_NAME ("dswload2")
57 /*******************************************************************************
59 * FUNCTION: AcpiDsLoad2BeginOp
61 * PARAMETERS: WalkState - Current state of the parse tree walk
62 * OutOp - Wher to return op if a new one is created
64 * RETURN: Status
66 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
68 ******************************************************************************/
70 ACPI_STATUS
71 AcpiDsLoad2BeginOp (
72 ACPI_WALK_STATE *WalkState,
73 ACPI_PARSE_OBJECT **OutOp)
75 ACPI_PARSE_OBJECT *Op;
76 ACPI_NAMESPACE_NODE *Node;
77 ACPI_STATUS Status;
78 ACPI_OBJECT_TYPE ObjectType;
79 char *BufferPtr;
80 UINT32 Flags;
83 ACPI_FUNCTION_TRACE (DsLoad2BeginOp);
86 Op = WalkState->Op;
87 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Op=%p State=%p\n", Op, WalkState));
89 if (Op)
91 if ((WalkState->ControlState) &&
92 (WalkState->ControlState->Common.State ==
93 ACPI_CONTROL_CONDITIONAL_EXECUTING))
95 /* We are executing a while loop outside of a method */
97 Status = AcpiDsExecBeginOp (WalkState, OutOp);
98 return_ACPI_STATUS (Status);
101 /* We only care about Namespace opcodes here */
103 if ((!(WalkState->OpInfo->Flags & AML_NSOPCODE) &&
104 (WalkState->Opcode != AML_INT_NAMEPATH_OP)) ||
105 (!(WalkState->OpInfo->Flags & AML_NAMED)))
107 return_ACPI_STATUS (AE_OK);
110 /* Get the name we are going to enter or lookup in the namespace */
112 if (WalkState->Opcode == AML_INT_NAMEPATH_OP)
114 /* For Namepath op, get the path string */
116 BufferPtr = Op->Common.Value.String;
117 if (!BufferPtr)
119 /* No name, just exit */
121 return_ACPI_STATUS (AE_OK);
124 else
126 /* Get name from the op */
128 BufferPtr = ACPI_CAST_PTR (char, &Op->Named.Name);
131 else
133 /* Get the namestring from the raw AML */
135 BufferPtr = AcpiPsGetNextNamestring (&WalkState->ParserState);
138 /* Map the opcode into an internal object type */
140 ObjectType = WalkState->OpInfo->ObjectType;
142 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
143 "State=%p Op=%p Type=%X\n", WalkState, Op, ObjectType));
145 switch (WalkState->Opcode)
147 case AML_FIELD_OP:
148 case AML_BANK_FIELD_OP:
149 case AML_INDEX_FIELD_OP:
151 Node = NULL;
152 Status = AE_OK;
153 break;
155 case AML_INT_NAMEPATH_OP:
157 * The NamePath is an object reference to an existing object.
158 * Don't enter the name into the namespace, but look it up
159 * for use later.
161 Status = AcpiNsLookup (WalkState->ScopeInfo, BufferPtr, ObjectType,
162 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
163 WalkState, &(Node));
164 break;
166 case AML_SCOPE_OP:
168 /* Special case for Scope(\) -> refers to the Root node */
170 if (Op && (Op->Named.Node == AcpiGbl_RootNode))
172 Node = Op->Named.Node;
174 Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
175 if (ACPI_FAILURE (Status))
177 return_ACPI_STATUS (Status);
180 else
183 * The Path is an object reference to an existing object.
184 * Don't enter the name into the namespace, but look it up
185 * for use later.
187 Status = AcpiNsLookup (WalkState->ScopeInfo, BufferPtr, ObjectType,
188 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
189 WalkState, &(Node));
190 if (ACPI_FAILURE (Status))
192 #ifdef ACPI_ASL_COMPILER
193 if (Status == AE_NOT_FOUND)
195 Status = AE_OK;
197 else
199 ACPI_ERROR_NAMESPACE (BufferPtr, Status);
201 #else
202 ACPI_ERROR_NAMESPACE (BufferPtr, Status);
203 #endif
204 return_ACPI_STATUS (Status);
209 * We must check to make sure that the target is
210 * one of the opcodes that actually opens a scope
212 switch (Node->Type)
214 case ACPI_TYPE_ANY:
215 case ACPI_TYPE_LOCAL_SCOPE: /* Scope */
216 case ACPI_TYPE_DEVICE:
217 case ACPI_TYPE_POWER:
218 case ACPI_TYPE_PROCESSOR:
219 case ACPI_TYPE_THERMAL:
221 /* These are acceptable types */
222 break;
224 case ACPI_TYPE_INTEGER:
225 case ACPI_TYPE_STRING:
226 case ACPI_TYPE_BUFFER:
229 * These types we will allow, but we will change the type.
230 * This enables some existing code of the form:
232 * Name (DEB, 0)
233 * Scope (DEB) { ... }
235 ACPI_WARNING ((AE_INFO,
236 "Type override - [%4.4s] had invalid type (%s) "
237 "for Scope operator, changed to type ANY",
238 AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type)));
240 Node->Type = ACPI_TYPE_ANY;
241 WalkState->ScopeInfo->Common.Value = ACPI_TYPE_ANY;
242 break;
244 case ACPI_TYPE_METHOD:
247 * Allow scope change to root during execution of module-level
248 * code. Root is typed METHOD during this time.
250 if ((Node == AcpiGbl_RootNode) &&
251 (WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL))
253 break;
256 /*lint -fallthrough */
258 default:
260 /* All other types are an error */
262 ACPI_ERROR ((AE_INFO,
263 "Invalid type (%s) for target of "
264 "Scope operator [%4.4s] (Cannot override)",
265 AcpiUtGetTypeName (Node->Type), AcpiUtGetNodeName (Node)));
267 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
269 break;
271 default:
273 /* All other opcodes */
275 if (Op && Op->Common.Node)
277 /* This op/node was previously entered into the namespace */
279 Node = Op->Common.Node;
281 if (AcpiNsOpensScope (ObjectType))
283 Status = AcpiDsScopeStackPush (Node, ObjectType, WalkState);
284 if (ACPI_FAILURE (Status))
286 return_ACPI_STATUS (Status);
290 return_ACPI_STATUS (AE_OK);
294 * Enter the named type into the internal namespace. We enter the name
295 * as we go downward in the parse tree. Any necessary subobjects that
296 * involve arguments to the opcode must be created as we go back up the
297 * parse tree later.
299 * Note: Name may already exist if we are executing a deferred opcode.
301 if (WalkState->DeferredNode)
303 /* This name is already in the namespace, get the node */
305 Node = WalkState->DeferredNode;
306 Status = AE_OK;
307 break;
310 Flags = ACPI_NS_NO_UPSEARCH;
311 if (WalkState->PassNumber == ACPI_IMODE_EXECUTE)
313 /* Execution mode, node cannot already exist, node is temporary */
315 Flags |= ACPI_NS_ERROR_IF_FOUND;
317 if (!(WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL))
319 Flags |= ACPI_NS_TEMPORARY;
323 /* Add new entry or lookup existing entry */
325 Status = AcpiNsLookup (WalkState->ScopeInfo, BufferPtr, ObjectType,
326 ACPI_IMODE_LOAD_PASS2, Flags, WalkState, &Node);
328 if (ACPI_SUCCESS (Status) && (Flags & ACPI_NS_TEMPORARY))
330 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
331 "***New Node [%4.4s] %p is temporary\n",
332 AcpiUtGetNodeName (Node), Node));
334 break;
337 if (ACPI_FAILURE (Status))
339 ACPI_ERROR_NAMESPACE (BufferPtr, Status);
340 return_ACPI_STATUS (Status);
343 if (!Op)
345 /* Create a new op */
347 Op = AcpiPsAllocOp (WalkState->Opcode, WalkState->Aml);
348 if (!Op)
350 return_ACPI_STATUS (AE_NO_MEMORY);
353 /* Initialize the new op */
355 if (Node)
357 Op->Named.Name = Node->Name.Integer;
359 *OutOp = Op;
363 * Put the Node in the "op" object that the parser uses, so we
364 * can get it again quickly when this scope is closed
366 Op->Common.Node = Node;
367 return_ACPI_STATUS (Status);
371 /*******************************************************************************
373 * FUNCTION: AcpiDsLoad2EndOp
375 * PARAMETERS: WalkState - Current state of the parse tree walk
377 * RETURN: Status
379 * DESCRIPTION: Ascending callback used during the loading of the namespace,
380 * both control methods and everything else.
382 ******************************************************************************/
384 ACPI_STATUS
385 AcpiDsLoad2EndOp (
386 ACPI_WALK_STATE *WalkState)
388 ACPI_PARSE_OBJECT *Op;
389 ACPI_STATUS Status = AE_OK;
390 ACPI_OBJECT_TYPE ObjectType;
391 ACPI_NAMESPACE_NODE *Node;
392 ACPI_PARSE_OBJECT *Arg;
393 ACPI_NAMESPACE_NODE *NewNode;
394 #ifndef ACPI_NO_METHOD_EXECUTION
395 UINT32 i;
396 UINT8 RegionSpace;
397 #endif
400 ACPI_FUNCTION_TRACE (DsLoad2EndOp);
402 Op = WalkState->Op;
403 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Opcode [%s] Op %p State %p\n",
404 WalkState->OpInfo->Name, Op, WalkState));
406 /* Check if opcode had an associated namespace object */
408 if (!(WalkState->OpInfo->Flags & AML_NSOBJECT))
410 return_ACPI_STATUS (AE_OK);
413 if (Op->Common.AmlOpcode == AML_SCOPE_OP)
415 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
416 "Ending scope Op=%p State=%p\n", Op, WalkState));
419 ObjectType = WalkState->OpInfo->ObjectType;
422 * Get the Node/name from the earlier lookup
423 * (It was saved in the *op structure)
425 Node = Op->Common.Node;
428 * Put the Node on the object stack (Contains the ACPI Name of
429 * this object)
431 WalkState->Operands[0] = (void *) Node;
432 WalkState->NumOperands = 1;
434 /* Pop the scope stack */
436 if (AcpiNsOpensScope (ObjectType) &&
437 (Op->Common.AmlOpcode != AML_INT_METHODCALL_OP))
439 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "(%s) Popping scope for Op %p\n",
440 AcpiUtGetTypeName (ObjectType), Op));
442 Status = AcpiDsScopeStackPop (WalkState);
443 if (ACPI_FAILURE (Status))
445 goto Cleanup;
450 * Named operations are as follows:
452 * AML_ALIAS
453 * AML_BANKFIELD
454 * AML_CREATEBITFIELD
455 * AML_CREATEBYTEFIELD
456 * AML_CREATEDWORDFIELD
457 * AML_CREATEFIELD
458 * AML_CREATEQWORDFIELD
459 * AML_CREATEWORDFIELD
460 * AML_DATA_REGION
461 * AML_DEVICE
462 * AML_EVENT
463 * AML_FIELD
464 * AML_INDEXFIELD
465 * AML_METHOD
466 * AML_METHODCALL
467 * AML_MUTEX
468 * AML_NAME
469 * AML_NAMEDFIELD
470 * AML_OPREGION
471 * AML_POWERRES
472 * AML_PROCESSOR
473 * AML_SCOPE
474 * AML_THERMALZONE
477 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
478 "Create-Load [%s] State=%p Op=%p NamedObj=%p\n",
479 AcpiPsGetOpcodeName (Op->Common.AmlOpcode), WalkState, Op, Node));
481 /* Decode the opcode */
483 Arg = Op->Common.Value.Arg;
485 switch (WalkState->OpInfo->Type)
487 #ifndef ACPI_NO_METHOD_EXECUTION
489 case AML_TYPE_CREATE_FIELD:
491 * Create the field object, but the field buffer and index must
492 * be evaluated later during the execution phase
494 Status = AcpiDsCreateBufferField (Op, WalkState);
495 break;
497 case AML_TYPE_NAMED_FIELD:
499 * If we are executing a method, initialize the field
501 if (WalkState->MethodNode)
503 Status = AcpiDsInitFieldObjects (Op, WalkState);
506 switch (Op->Common.AmlOpcode)
508 case AML_INDEX_FIELD_OP:
510 Status = AcpiDsCreateIndexField (
511 Op, (ACPI_HANDLE) Arg->Common.Node, WalkState);
512 break;
514 case AML_BANK_FIELD_OP:
516 Status = AcpiDsCreateBankField (Op, Arg->Common.Node, WalkState);
517 break;
519 case AML_FIELD_OP:
521 Status = AcpiDsCreateField (Op, Arg->Common.Node, WalkState);
522 break;
524 default:
526 /* All NAMED_FIELD opcodes must be handled above */
527 break;
529 break;
531 case AML_TYPE_NAMED_SIMPLE:
533 Status = AcpiDsCreateOperands (WalkState, Arg);
534 if (ACPI_FAILURE (Status))
536 goto Cleanup;
539 switch (Op->Common.AmlOpcode)
541 case AML_PROCESSOR_OP:
543 Status = AcpiExCreateProcessor (WalkState);
544 break;
546 case AML_POWER_RESOURCE_OP:
548 Status = AcpiExCreatePowerResource (WalkState);
549 break;
551 case AML_MUTEX_OP:
553 Status = AcpiExCreateMutex (WalkState);
554 break;
556 case AML_EVENT_OP:
558 Status = AcpiExCreateEvent (WalkState);
559 break;
561 case AML_ALIAS_OP:
563 Status = AcpiExCreateAlias (WalkState);
564 break;
566 default:
568 /* Unknown opcode */
570 Status = AE_OK;
571 goto Cleanup;
574 /* Delete operands */
576 for (i = 1; i < WalkState->NumOperands; i++)
578 AcpiUtRemoveReference (WalkState->Operands[i]);
579 WalkState->Operands[i] = NULL;
582 break;
583 #endif /* ACPI_NO_METHOD_EXECUTION */
585 case AML_TYPE_NAMED_COMPLEX:
587 switch (Op->Common.AmlOpcode)
589 #ifndef ACPI_NO_METHOD_EXECUTION
590 case AML_REGION_OP:
591 case AML_DATA_REGION_OP:
593 if (Op->Common.AmlOpcode == AML_REGION_OP)
595 RegionSpace = (ACPI_ADR_SPACE_TYPE)
596 ((Op->Common.Value.Arg)->Common.Value.Integer);
598 else
600 RegionSpace = ACPI_ADR_SPACE_DATA_TABLE;
604 * The OpRegion is not fully parsed at this time. The only valid
605 * argument is the SpaceId. (We must save the address of the
606 * AML of the address and length operands)
608 * If we have a valid region, initialize it. The namespace is
609 * unlocked at this point.
611 * Need to unlock interpreter if it is locked (if we are running
612 * a control method), in order to allow _REG methods to be run
613 * during AcpiEvInitializeRegion.
615 if (WalkState->MethodNode)
618 * Executing a method: initialize the region and unlock
619 * the interpreter
621 Status = AcpiExCreateRegion (Op->Named.Data,
622 Op->Named.Length, RegionSpace, WalkState);
623 if (ACPI_FAILURE (Status))
625 return_ACPI_STATUS (Status);
629 Status = AcpiEvInitializeRegion (
630 AcpiNsGetAttachedObject (Node));
631 break;
633 case AML_NAME_OP:
635 Status = AcpiDsCreateNode (WalkState, Node, Op);
636 break;
638 case AML_METHOD_OP:
640 * MethodOp PkgLength NameString MethodFlags TermList
642 * Note: We must create the method node/object pair as soon as we
643 * see the method declaration. This allows later pass1 parsing
644 * of invocations of the method (need to know the number of
645 * arguments.)
647 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
648 "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
649 WalkState, Op, Op->Named.Node));
651 if (!AcpiNsGetAttachedObject (Op->Named.Node))
653 WalkState->Operands[0] = ACPI_CAST_PTR (void, Op->Named.Node);
654 WalkState->NumOperands = 1;
656 Status = AcpiDsCreateOperands (
657 WalkState, Op->Common.Value.Arg);
658 if (ACPI_SUCCESS (Status))
660 Status = AcpiExCreateMethod (
661 Op->Named.Data, Op->Named.Length, WalkState);
664 WalkState->Operands[0] = NULL;
665 WalkState->NumOperands = 0;
667 if (ACPI_FAILURE (Status))
669 return_ACPI_STATUS (Status);
672 break;
674 #endif /* ACPI_NO_METHOD_EXECUTION */
676 default:
678 /* All NAMED_COMPLEX opcodes must be handled above */
679 break;
681 break;
683 case AML_CLASS_INTERNAL:
685 /* case AML_INT_NAMEPATH_OP: */
686 break;
688 case AML_CLASS_METHOD_CALL:
690 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
691 "RESOLVING-MethodCall: State=%p Op=%p NamedObj=%p\n",
692 WalkState, Op, Node));
695 * Lookup the method name and save the Node
697 Status = AcpiNsLookup (WalkState->ScopeInfo, Arg->Common.Value.String,
698 ACPI_TYPE_ANY, ACPI_IMODE_LOAD_PASS2,
699 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
700 WalkState, &(NewNode));
701 if (ACPI_SUCCESS (Status))
704 * Make sure that what we found is indeed a method
705 * We didn't search for a method on purpose, to see if the name
706 * would resolve
708 if (NewNode->Type != ACPI_TYPE_METHOD)
710 Status = AE_AML_OPERAND_TYPE;
713 /* We could put the returned object (Node) on the object stack for
714 * later, but for now, we will put it in the "op" object that the
715 * parser uses, so we can get it again at the end of this scope
717 Op->Common.Node = NewNode;
719 else
721 ACPI_ERROR_NAMESPACE (Arg->Common.Value.String, Status);
723 break;
726 default:
728 break;
731 Cleanup:
733 /* Remove the Node pushed at the very beginning */
735 WalkState->Operands[0] = NULL;
736 WalkState->NumOperands = 0;
737 return_ACPI_STATUS (Status);