1db2bae30SDana Myers /******************************************************************************
2db2bae30SDana Myers *
3db2bae30SDana Myers * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
4db2bae30SDana Myers *
5db2bae30SDana Myers *****************************************************************************/
6db2bae30SDana Myers
726f3cdf0SGordon Ross /*
8*385cc6b4SJerry Jelinek * Copyright (C) 2000 - 2016, Intel Corp.
9db2bae30SDana Myers * All rights reserved.
10db2bae30SDana Myers *
1126f3cdf0SGordon Ross * Redistribution and use in source and binary forms, with or without
1226f3cdf0SGordon Ross * modification, are permitted provided that the following conditions
1326f3cdf0SGordon Ross * are met:
1426f3cdf0SGordon Ross * 1. Redistributions of source code must retain the above copyright
1526f3cdf0SGordon Ross * notice, this list of conditions, and the following disclaimer,
1626f3cdf0SGordon Ross * without modification.
1726f3cdf0SGordon Ross * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1826f3cdf0SGordon Ross * substantially similar to the "NO WARRANTY" disclaimer below
1926f3cdf0SGordon Ross * ("Disclaimer") and any redistribution must be conditioned upon
2026f3cdf0SGordon Ross * including a substantially similar Disclaimer requirement for further
2126f3cdf0SGordon Ross * binary redistribution.
2226f3cdf0SGordon Ross * 3. Neither the names of the above-listed copyright holders nor the names
2326f3cdf0SGordon Ross * of any contributors may be used to endorse or promote products derived
2426f3cdf0SGordon Ross * from this software without specific prior written permission.
25db2bae30SDana Myers *
2626f3cdf0SGordon Ross * Alternatively, this software may be distributed under the terms of the
2726f3cdf0SGordon Ross * GNU General Public License ("GPL") version 2 as published by the Free
2826f3cdf0SGordon Ross * Software Foundation.
29db2bae30SDana Myers *
3026f3cdf0SGordon Ross * NO WARRANTY
3126f3cdf0SGordon Ross * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3226f3cdf0SGordon Ross * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3326f3cdf0SGordon Ross * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3426f3cdf0SGordon Ross * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3526f3cdf0SGordon Ross * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3626f3cdf0SGordon Ross * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3726f3cdf0SGordon Ross * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3826f3cdf0SGordon Ross * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3926f3cdf0SGordon Ross * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4026f3cdf0SGordon Ross * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4126f3cdf0SGordon Ross * POSSIBILITY OF SUCH DAMAGES.
4226f3cdf0SGordon Ross */
43db2bae30SDana Myers
44db2bae30SDana Myers #include "acpi.h"
45aa2aa9a6SDana Myers #include "accommon.h"
46db2bae30SDana Myers #include "acdispat.h"
47db2bae30SDana Myers #include "acinterp.h"
48db2bae30SDana Myers #include "acnamesp.h"
49*385cc6b4SJerry Jelinek #include "acparser.h"
50*385cc6b4SJerry Jelinek #include "amlcode.h"
51*385cc6b4SJerry Jelinek #include "acdebug.h"
52db2bae30SDana Myers
53db2bae30SDana Myers
54db2bae30SDana Myers #define _COMPONENT ACPI_DISPATCHER
55db2bae30SDana Myers ACPI_MODULE_NAME ("dsmethod")
56db2bae30SDana Myers
57db2bae30SDana Myers /* Local prototypes */
58db2bae30SDana Myers
59db2bae30SDana Myers static ACPI_STATUS
60*385cc6b4SJerry Jelinek AcpiDsDetectNamedOpcodes (
61*385cc6b4SJerry Jelinek ACPI_WALK_STATE *WalkState,
62*385cc6b4SJerry Jelinek ACPI_PARSE_OBJECT **OutOp);
63*385cc6b4SJerry Jelinek
64*385cc6b4SJerry Jelinek static ACPI_STATUS
65db2bae30SDana Myers AcpiDsCreateMethodMutex (
66db2bae30SDana Myers ACPI_OPERAND_OBJECT *MethodDesc);
67db2bae30SDana Myers
68db2bae30SDana Myers
69db2bae30SDana Myers /*******************************************************************************
70db2bae30SDana Myers *
71*385cc6b4SJerry Jelinek * FUNCTION: AcpiDsAutoSerializeMethod
72*385cc6b4SJerry Jelinek *
73*385cc6b4SJerry Jelinek * PARAMETERS: Node - Namespace Node of the method
74*385cc6b4SJerry Jelinek * ObjDesc - Method object attached to node
75*385cc6b4SJerry Jelinek *
76*385cc6b4SJerry Jelinek * RETURN: Status
77*385cc6b4SJerry Jelinek *
78*385cc6b4SJerry Jelinek * DESCRIPTION: Parse a control method AML to scan for control methods that
79*385cc6b4SJerry Jelinek * need serialization due to the creation of named objects.
80*385cc6b4SJerry Jelinek *
81*385cc6b4SJerry Jelinek * NOTE: It is a bit of overkill to mark all such methods serialized, since
82*385cc6b4SJerry Jelinek * there is only a problem if the method actually blocks during execution.
83*385cc6b4SJerry Jelinek * A blocking operation is, for example, a Sleep() operation, or any access
84*385cc6b4SJerry Jelinek * to an operation region. However, it is probably not possible to easily
85*385cc6b4SJerry Jelinek * detect whether a method will block or not, so we simply mark all suspicious
86*385cc6b4SJerry Jelinek * methods as serialized.
87*385cc6b4SJerry Jelinek *
88*385cc6b4SJerry Jelinek * NOTE2: This code is essentially a generic routine for parsing a single
89*385cc6b4SJerry Jelinek * control method.
90*385cc6b4SJerry Jelinek *
91*385cc6b4SJerry Jelinek ******************************************************************************/
92*385cc6b4SJerry Jelinek
93*385cc6b4SJerry Jelinek ACPI_STATUS
AcpiDsAutoSerializeMethod(ACPI_NAMESPACE_NODE * Node,ACPI_OPERAND_OBJECT * ObjDesc)94*385cc6b4SJerry Jelinek AcpiDsAutoSerializeMethod (
95*385cc6b4SJerry Jelinek ACPI_NAMESPACE_NODE *Node,
96*385cc6b4SJerry Jelinek ACPI_OPERAND_OBJECT *ObjDesc)
97*385cc6b4SJerry Jelinek {
98*385cc6b4SJerry Jelinek ACPI_STATUS Status;
99*385cc6b4SJerry Jelinek ACPI_PARSE_OBJECT *Op = NULL;
100*385cc6b4SJerry Jelinek ACPI_WALK_STATE *WalkState;
101*385cc6b4SJerry Jelinek
102*385cc6b4SJerry Jelinek
103*385cc6b4SJerry Jelinek ACPI_FUNCTION_TRACE_PTR (DsAutoSerializeMethod, Node);
104*385cc6b4SJerry Jelinek
105*385cc6b4SJerry Jelinek
106*385cc6b4SJerry Jelinek ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
107*385cc6b4SJerry Jelinek "Method auto-serialization parse [%4.4s] %p\n",
108*385cc6b4SJerry Jelinek AcpiUtGetNodeName (Node), Node));
109*385cc6b4SJerry Jelinek
110*385cc6b4SJerry Jelinek /* Create/Init a root op for the method parse tree */
111*385cc6b4SJerry Jelinek
112*385cc6b4SJerry Jelinek Op = AcpiPsAllocOp (AML_METHOD_OP, ObjDesc->Method.AmlStart);
113*385cc6b4SJerry Jelinek if (!Op)
114*385cc6b4SJerry Jelinek {
115*385cc6b4SJerry Jelinek return_ACPI_STATUS (AE_NO_MEMORY);
116*385cc6b4SJerry Jelinek }
117*385cc6b4SJerry Jelinek
118*385cc6b4SJerry Jelinek AcpiPsSetName (Op, Node->Name.Integer);
119*385cc6b4SJerry Jelinek Op->Common.Node = Node;
120*385cc6b4SJerry Jelinek
121*385cc6b4SJerry Jelinek /* Create and initialize a new walk state */
122*385cc6b4SJerry Jelinek
123*385cc6b4SJerry Jelinek WalkState = AcpiDsCreateWalkState (Node->OwnerId, NULL, NULL, NULL);
124*385cc6b4SJerry Jelinek if (!WalkState)
125*385cc6b4SJerry Jelinek {
126*385cc6b4SJerry Jelinek AcpiPsFreeOp (Op);
127*385cc6b4SJerry Jelinek return_ACPI_STATUS (AE_NO_MEMORY);
128*385cc6b4SJerry Jelinek }
129*385cc6b4SJerry Jelinek
130*385cc6b4SJerry Jelinek Status = AcpiDsInitAmlWalk (WalkState, Op, Node,
131*385cc6b4SJerry Jelinek ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength, NULL, 0);
132*385cc6b4SJerry Jelinek if (ACPI_FAILURE (Status))
133*385cc6b4SJerry Jelinek {
134*385cc6b4SJerry Jelinek AcpiDsDeleteWalkState (WalkState);
135*385cc6b4SJerry Jelinek AcpiPsFreeOp (Op);
136*385cc6b4SJerry Jelinek return_ACPI_STATUS (Status);
137*385cc6b4SJerry Jelinek }
138*385cc6b4SJerry Jelinek
139*385cc6b4SJerry Jelinek WalkState->DescendingCallback = AcpiDsDetectNamedOpcodes;
140*385cc6b4SJerry Jelinek
141*385cc6b4SJerry Jelinek /* Parse the method, scan for creation of named objects */
142*385cc6b4SJerry Jelinek
143*385cc6b4SJerry Jelinek Status = AcpiPsParseAml (WalkState);
144*385cc6b4SJerry Jelinek
145*385cc6b4SJerry Jelinek AcpiPsDeleteParseTree (Op);
146*385cc6b4SJerry Jelinek return_ACPI_STATUS (Status);
147*385cc6b4SJerry Jelinek }
148*385cc6b4SJerry Jelinek
149*385cc6b4SJerry Jelinek
150*385cc6b4SJerry Jelinek /*******************************************************************************
151*385cc6b4SJerry Jelinek *
152*385cc6b4SJerry Jelinek * FUNCTION: AcpiDsDetectNamedOpcodes
153*385cc6b4SJerry Jelinek *
154*385cc6b4SJerry Jelinek * PARAMETERS: WalkState - Current state of the parse tree walk
155*385cc6b4SJerry Jelinek * OutOp - Unused, required for parser interface
156*385cc6b4SJerry Jelinek *
157*385cc6b4SJerry Jelinek * RETURN: Status
158*385cc6b4SJerry Jelinek *
159*385cc6b4SJerry Jelinek * DESCRIPTION: Descending callback used during the loading of ACPI tables.
160*385cc6b4SJerry Jelinek * Currently used to detect methods that must be marked serialized
161*385cc6b4SJerry Jelinek * in order to avoid problems with the creation of named objects.
162*385cc6b4SJerry Jelinek *
163*385cc6b4SJerry Jelinek ******************************************************************************/
164*385cc6b4SJerry Jelinek
165*385cc6b4SJerry Jelinek static ACPI_STATUS
AcpiDsDetectNamedOpcodes(ACPI_WALK_STATE * WalkState,ACPI_PARSE_OBJECT ** OutOp)166*385cc6b4SJerry Jelinek AcpiDsDetectNamedOpcodes (
167*385cc6b4SJerry Jelinek ACPI_WALK_STATE *WalkState,
168*385cc6b4SJerry Jelinek ACPI_PARSE_OBJECT **OutOp)
169*385cc6b4SJerry Jelinek {
170*385cc6b4SJerry Jelinek
171*385cc6b4SJerry Jelinek ACPI_FUNCTION_NAME (AcpiDsDetectNamedOpcodes);
172*385cc6b4SJerry Jelinek
173*385cc6b4SJerry Jelinek
174*385cc6b4SJerry Jelinek /* We are only interested in opcodes that create a new name */
175*385cc6b4SJerry Jelinek
176*385cc6b4SJerry Jelinek if (!(WalkState->OpInfo->Flags & (AML_NAMED | AML_CREATE | AML_FIELD)))
177*385cc6b4SJerry Jelinek {
178*385cc6b4SJerry Jelinek return (AE_OK);
179*385cc6b4SJerry Jelinek }
180*385cc6b4SJerry Jelinek
181*385cc6b4SJerry Jelinek /*
182*385cc6b4SJerry Jelinek * At this point, we know we have a Named object opcode.
183*385cc6b4SJerry Jelinek * Mark the method as serialized. Later code will create a mutex for
184*385cc6b4SJerry Jelinek * this method to enforce serialization.
185*385cc6b4SJerry Jelinek *
186*385cc6b4SJerry Jelinek * Note, ACPI_METHOD_IGNORE_SYNC_LEVEL flag means that we will ignore the
187*385cc6b4SJerry Jelinek * Sync Level mechanism for this method, even though it is now serialized.
188*385cc6b4SJerry Jelinek * Otherwise, there can be conflicts with existing ASL code that actually
189*385cc6b4SJerry Jelinek * uses sync levels.
190*385cc6b4SJerry Jelinek */
191*385cc6b4SJerry Jelinek WalkState->MethodDesc->Method.SyncLevel = 0;
192*385cc6b4SJerry Jelinek WalkState->MethodDesc->Method.InfoFlags |=
193*385cc6b4SJerry Jelinek (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
194*385cc6b4SJerry Jelinek
195*385cc6b4SJerry Jelinek ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
196*385cc6b4SJerry Jelinek "Method serialized [%4.4s] %p - [%s] (%4.4X)\n",
197*385cc6b4SJerry Jelinek WalkState->MethodNode->Name.Ascii, WalkState->MethodNode,
198*385cc6b4SJerry Jelinek WalkState->OpInfo->Name, WalkState->Opcode));
199*385cc6b4SJerry Jelinek
200*385cc6b4SJerry Jelinek /* Abort the parse, no need to examine this method any further */
201*385cc6b4SJerry Jelinek
202*385cc6b4SJerry Jelinek return (AE_CTRL_TERMINATE);
203*385cc6b4SJerry Jelinek }
204*385cc6b4SJerry Jelinek
205*385cc6b4SJerry Jelinek
206*385cc6b4SJerry Jelinek /*******************************************************************************
207*385cc6b4SJerry Jelinek *
208db2bae30SDana Myers * FUNCTION: AcpiDsMethodError
209db2bae30SDana Myers *
210db2bae30SDana Myers * PARAMETERS: Status - Execution status
211db2bae30SDana Myers * WalkState - Current state
212db2bae30SDana Myers *
213db2bae30SDana Myers * RETURN: Status
214db2bae30SDana Myers *
215db2bae30SDana Myers * DESCRIPTION: Called on method error. Invoke the global exception handler if
216*385cc6b4SJerry Jelinek * present, dump the method data if the debugger is configured
217db2bae30SDana Myers *
218db2bae30SDana Myers * Note: Allows the exception handler to change the status code
219db2bae30SDana Myers *
220db2bae30SDana Myers ******************************************************************************/
221db2bae30SDana Myers
222db2bae30SDana Myers ACPI_STATUS
AcpiDsMethodError(ACPI_STATUS Status,ACPI_WALK_STATE * WalkState)223db2bae30SDana Myers AcpiDsMethodError (
224db2bae30SDana Myers ACPI_STATUS Status,
225db2bae30SDana Myers ACPI_WALK_STATE *WalkState)
226db2bae30SDana Myers {
227*385cc6b4SJerry Jelinek UINT32 AmlOffset;
228*385cc6b4SJerry Jelinek
229*385cc6b4SJerry Jelinek
230db2bae30SDana Myers ACPI_FUNCTION_ENTRY ();
231db2bae30SDana Myers
232db2bae30SDana Myers
233db2bae30SDana Myers /* Ignore AE_OK and control exception codes */
234db2bae30SDana Myers
235db2bae30SDana Myers if (ACPI_SUCCESS (Status) ||
236db2bae30SDana Myers (Status & AE_CODE_CONTROL))
237db2bae30SDana Myers {
238db2bae30SDana Myers return (Status);
239db2bae30SDana Myers }
240db2bae30SDana Myers
241db2bae30SDana Myers /* Invoke the global exception handler */
242db2bae30SDana Myers
243db2bae30SDana Myers if (AcpiGbl_ExceptionHandler)
244db2bae30SDana Myers {
245db2bae30SDana Myers /* Exit the interpreter, allow handler to execute methods */
246db2bae30SDana Myers
247db2bae30SDana Myers AcpiExExitInterpreter ();
248db2bae30SDana Myers
249db2bae30SDana Myers /*
250db2bae30SDana Myers * Handler can map the exception code to anything it wants, including
251db2bae30SDana Myers * AE_OK, in which case the executing method will not be aborted.
252db2bae30SDana Myers */
253*385cc6b4SJerry Jelinek AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->Aml,
254*385cc6b4SJerry Jelinek WalkState->ParserState.AmlStart);
255*385cc6b4SJerry Jelinek
256db2bae30SDana Myers Status = AcpiGbl_ExceptionHandler (Status,
257db2bae30SDana Myers WalkState->MethodNode ?
258db2bae30SDana Myers WalkState->MethodNode->Name.Integer : 0,
259*385cc6b4SJerry Jelinek WalkState->Opcode, AmlOffset, NULL);
260db2bae30SDana Myers AcpiExEnterInterpreter ();
261db2bae30SDana Myers }
262db2bae30SDana Myers
263db2bae30SDana Myers AcpiDsClearImplicitReturn (WalkState);
264db2bae30SDana Myers
265db2bae30SDana Myers if (ACPI_FAILURE (Status))
266db2bae30SDana Myers {
267*385cc6b4SJerry Jelinek AcpiDsDumpMethodStack (Status, WalkState, WalkState->Op);
268db2bae30SDana Myers
269*385cc6b4SJerry Jelinek /* Display method locals/args if debugger is present */
270*385cc6b4SJerry Jelinek
271*385cc6b4SJerry Jelinek #ifdef ACPI_DEBUGGER
272*385cc6b4SJerry Jelinek AcpiDbDumpMethodInfo (Status, WalkState);
273db2bae30SDana Myers #endif
274*385cc6b4SJerry Jelinek }
275db2bae30SDana Myers
276db2bae30SDana Myers return (Status);
277db2bae30SDana Myers }
278db2bae30SDana Myers
279db2bae30SDana Myers
280db2bae30SDana Myers /*******************************************************************************
281db2bae30SDana Myers *
282db2bae30SDana Myers * FUNCTION: AcpiDsCreateMethodMutex
283db2bae30SDana Myers *
284db2bae30SDana Myers * PARAMETERS: ObjDesc - The method object
285db2bae30SDana Myers *
286db2bae30SDana Myers * RETURN: Status
287db2bae30SDana Myers *
288db2bae30SDana Myers * DESCRIPTION: Create a mutex object for a serialized control method
289db2bae30SDana Myers *
290db2bae30SDana Myers ******************************************************************************/
291db2bae30SDana Myers
292db2bae30SDana Myers static ACPI_STATUS
AcpiDsCreateMethodMutex(ACPI_OPERAND_OBJECT * MethodDesc)293db2bae30SDana Myers AcpiDsCreateMethodMutex (
294db2bae30SDana Myers ACPI_OPERAND_OBJECT *MethodDesc)
295db2bae30SDana Myers {
296db2bae30SDana Myers ACPI_OPERAND_OBJECT *MutexDesc;
297db2bae30SDana Myers ACPI_STATUS Status;
298db2bae30SDana Myers
299db2bae30SDana Myers
300db2bae30SDana Myers ACPI_FUNCTION_TRACE (DsCreateMethodMutex);
301db2bae30SDana Myers
302db2bae30SDana Myers
303db2bae30SDana Myers /* Create the new mutex object */
304db2bae30SDana Myers
305db2bae30SDana Myers MutexDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
306db2bae30SDana Myers if (!MutexDesc)
307db2bae30SDana Myers {
308db2bae30SDana Myers return_ACPI_STATUS (AE_NO_MEMORY);
309db2bae30SDana Myers }
310db2bae30SDana Myers
311db2bae30SDana Myers /* Create the actual OS Mutex */
312db2bae30SDana Myers
313db2bae30SDana Myers Status = AcpiOsCreateMutex (&MutexDesc->Mutex.OsMutex);
314db2bae30SDana Myers if (ACPI_FAILURE (Status))
315db2bae30SDana Myers {
316*385cc6b4SJerry Jelinek AcpiUtDeleteObjectDesc (MutexDesc);
317db2bae30SDana Myers return_ACPI_STATUS (Status);
318db2bae30SDana Myers }
319db2bae30SDana Myers
320db2bae30SDana Myers MutexDesc->Mutex.SyncLevel = MethodDesc->Method.SyncLevel;
321db2bae30SDana Myers MethodDesc->Method.Mutex = MutexDesc;
322db2bae30SDana Myers return_ACPI_STATUS (AE_OK);
323db2bae30SDana Myers }
324db2bae30SDana Myers
325db2bae30SDana Myers
326db2bae30SDana Myers /*******************************************************************************
327db2bae30SDana Myers *
328db2bae30SDana Myers * FUNCTION: AcpiDsBeginMethodExecution
329db2bae30SDana Myers *
330db2bae30SDana Myers * PARAMETERS: MethodNode - Node of the method
331db2bae30SDana Myers * ObjDesc - The method object
332db2bae30SDana Myers * WalkState - current state, NULL if not yet executing
333db2bae30SDana Myers * a method.
334db2bae30SDana Myers *
335db2bae30SDana Myers * RETURN: Status
336db2bae30SDana Myers *
337db2bae30SDana Myers * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
338db2bae30SDana Myers * increments the thread count, and waits at the method semaphore
339db2bae30SDana Myers * for clearance to execute.
340db2bae30SDana Myers *
341db2bae30SDana Myers ******************************************************************************/
342db2bae30SDana Myers
343db2bae30SDana Myers ACPI_STATUS
AcpiDsBeginMethodExecution(ACPI_NAMESPACE_NODE * MethodNode,ACPI_OPERAND_OBJECT * ObjDesc,ACPI_WALK_STATE * WalkState)344db2bae30SDana Myers AcpiDsBeginMethodExecution (
345db2bae30SDana Myers ACPI_NAMESPACE_NODE *MethodNode,
346db2bae30SDana Myers ACPI_OPERAND_OBJECT *ObjDesc,
347db2bae30SDana Myers ACPI_WALK_STATE *WalkState)
348db2bae30SDana Myers {
349db2bae30SDana Myers ACPI_STATUS Status = AE_OK;
350db2bae30SDana Myers
351db2bae30SDana Myers
352db2bae30SDana Myers ACPI_FUNCTION_TRACE_PTR (DsBeginMethodExecution, MethodNode);
353db2bae30SDana Myers
354db2bae30SDana Myers
355db2bae30SDana Myers if (!MethodNode)
356db2bae30SDana Myers {
357db2bae30SDana Myers return_ACPI_STATUS (AE_NULL_ENTRY);
358db2bae30SDana Myers }
359db2bae30SDana Myers
360*385cc6b4SJerry Jelinek AcpiExStartTraceMethod (MethodNode, ObjDesc, WalkState);
361*385cc6b4SJerry Jelinek
362db2bae30SDana Myers /* Prevent wraparound of thread count */
363db2bae30SDana Myers
364db2bae30SDana Myers if (ObjDesc->Method.ThreadCount == ACPI_UINT8_MAX)
365db2bae30SDana Myers {
366db2bae30SDana Myers ACPI_ERROR ((AE_INFO,
367db2bae30SDana Myers "Method reached maximum reentrancy limit (255)"));
368db2bae30SDana Myers return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
369db2bae30SDana Myers }
370db2bae30SDana Myers
371db2bae30SDana Myers /*
372db2bae30SDana Myers * If this method is serialized, we need to acquire the method mutex.
373db2bae30SDana Myers */
37426f3cdf0SGordon Ross if (ObjDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED)
375db2bae30SDana Myers {
376db2bae30SDana Myers /*
377db2bae30SDana Myers * Create a mutex for the method if it is defined to be Serialized
378db2bae30SDana Myers * and a mutex has not already been created. We defer the mutex creation
379db2bae30SDana Myers * until a method is actually executed, to minimize the object count
380db2bae30SDana Myers */
381db2bae30SDana Myers if (!ObjDesc->Method.Mutex)
382db2bae30SDana Myers {
383db2bae30SDana Myers Status = AcpiDsCreateMethodMutex (ObjDesc);
384db2bae30SDana Myers if (ACPI_FAILURE (Status))
385db2bae30SDana Myers {
386db2bae30SDana Myers return_ACPI_STATUS (Status);
387db2bae30SDana Myers }
388db2bae30SDana Myers }
389db2bae30SDana Myers
390db2bae30SDana Myers /*
391db2bae30SDana Myers * The CurrentSyncLevel (per-thread) must be less than or equal to
392db2bae30SDana Myers * the sync level of the method. This mechanism provides some
393*385cc6b4SJerry Jelinek * deadlock prevention.
394*385cc6b4SJerry Jelinek *
395*385cc6b4SJerry Jelinek * If the method was auto-serialized, we just ignore the sync level
396*385cc6b4SJerry Jelinek * mechanism, because auto-serialization of methods can interfere
397*385cc6b4SJerry Jelinek * with ASL code that actually uses sync levels.
398db2bae30SDana Myers *
399db2bae30SDana Myers * Top-level method invocation has no walk state at this point
400db2bae30SDana Myers */
401db2bae30SDana Myers if (WalkState &&
402*385cc6b4SJerry Jelinek (!(ObjDesc->Method.InfoFlags & ACPI_METHOD_IGNORE_SYNC_LEVEL)) &&
403*385cc6b4SJerry Jelinek (WalkState->Thread->CurrentSyncLevel >
404*385cc6b4SJerry Jelinek ObjDesc->Method.Mutex->Mutex.SyncLevel))
405db2bae30SDana Myers {
406db2bae30SDana Myers ACPI_ERROR ((AE_INFO,
407*385cc6b4SJerry Jelinek "Cannot acquire Mutex for method [%4.4s]"
408*385cc6b4SJerry Jelinek ", current SyncLevel is too large (%u)",
409db2bae30SDana Myers AcpiUtGetNodeName (MethodNode),
410db2bae30SDana Myers WalkState->Thread->CurrentSyncLevel));
411db2bae30SDana Myers
412db2bae30SDana Myers return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
413db2bae30SDana Myers }
414db2bae30SDana Myers
415db2bae30SDana Myers /*
416db2bae30SDana Myers * Obtain the method mutex if necessary. Do not acquire mutex for a
417db2bae30SDana Myers * recursive call.
418db2bae30SDana Myers */
419db2bae30SDana Myers if (!WalkState ||
420db2bae30SDana Myers !ObjDesc->Method.Mutex->Mutex.ThreadId ||
421*385cc6b4SJerry Jelinek (WalkState->Thread->ThreadId !=
422*385cc6b4SJerry Jelinek ObjDesc->Method.Mutex->Mutex.ThreadId))
423db2bae30SDana Myers {
424db2bae30SDana Myers /*
425db2bae30SDana Myers * Acquire the method mutex. This releases the interpreter if we
426db2bae30SDana Myers * block (and reacquires it before it returns)
427db2bae30SDana Myers */
428*385cc6b4SJerry Jelinek Status = AcpiExSystemWaitMutex (
429*385cc6b4SJerry Jelinek ObjDesc->Method.Mutex->Mutex.OsMutex, ACPI_WAIT_FOREVER);
430db2bae30SDana Myers if (ACPI_FAILURE (Status))
431db2bae30SDana Myers {
432db2bae30SDana Myers return_ACPI_STATUS (Status);
433db2bae30SDana Myers }
434db2bae30SDana Myers
435db2bae30SDana Myers /* Update the mutex and walk info and save the original SyncLevel */
436db2bae30SDana Myers
437db2bae30SDana Myers if (WalkState)
438db2bae30SDana Myers {
439db2bae30SDana Myers ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
440db2bae30SDana Myers WalkState->Thread->CurrentSyncLevel;
441db2bae30SDana Myers
442*385cc6b4SJerry Jelinek ObjDesc->Method.Mutex->Mutex.ThreadId =
443*385cc6b4SJerry Jelinek WalkState->Thread->ThreadId;
444*385cc6b4SJerry Jelinek
445*385cc6b4SJerry Jelinek /*
446*385cc6b4SJerry Jelinek * Update the current SyncLevel only if this is not an auto-
447*385cc6b4SJerry Jelinek * serialized method. In the auto case, we have to ignore
448*385cc6b4SJerry Jelinek * the sync level for the method mutex (created for the
449*385cc6b4SJerry Jelinek * auto-serialization) because we have no idea of what the
450*385cc6b4SJerry Jelinek * sync level should be. Therefore, just ignore it.
451*385cc6b4SJerry Jelinek */
452*385cc6b4SJerry Jelinek if (!(ObjDesc->Method.InfoFlags &
453*385cc6b4SJerry Jelinek ACPI_METHOD_IGNORE_SYNC_LEVEL))
454*385cc6b4SJerry Jelinek {
455*385cc6b4SJerry Jelinek WalkState->Thread->CurrentSyncLevel =
456*385cc6b4SJerry Jelinek ObjDesc->Method.SyncLevel;
457*385cc6b4SJerry Jelinek }
458db2bae30SDana Myers }
459db2bae30SDana Myers else
460db2bae30SDana Myers {
461db2bae30SDana Myers ObjDesc->Method.Mutex->Mutex.OriginalSyncLevel =
462db2bae30SDana Myers ObjDesc->Method.Mutex->Mutex.SyncLevel;
463*385cc6b4SJerry Jelinek
464*385cc6b4SJerry Jelinek ObjDesc->Method.Mutex->Mutex.ThreadId =
465*385cc6b4SJerry Jelinek AcpiOsGetThreadId ();
466db2bae30SDana Myers }
467db2bae30SDana Myers }
468db2bae30SDana Myers
469db2bae30SDana Myers /* Always increase acquisition depth */
470db2bae30SDana Myers
471db2bae30SDana Myers ObjDesc->Method.Mutex->Mutex.AcquisitionDepth++;
472db2bae30SDana Myers }
473db2bae30SDana Myers
474db2bae30SDana Myers /*
475db2bae30SDana Myers * Allocate an Owner ID for this method, only if this is the first thread
476db2bae30SDana Myers * to begin concurrent execution. We only need one OwnerId, even if the
477db2bae30SDana Myers * method is invoked recursively.
478db2bae30SDana Myers */
479db2bae30SDana Myers if (!ObjDesc->Method.OwnerId)
480db2bae30SDana Myers {
481db2bae30SDana Myers Status = AcpiUtAllocateOwnerId (&ObjDesc->Method.OwnerId);
482db2bae30SDana Myers if (ACPI_FAILURE (Status))
483db2bae30SDana Myers {
484db2bae30SDana Myers goto Cleanup;
485db2bae30SDana Myers }
486db2bae30SDana Myers }
487db2bae30SDana Myers
488db2bae30SDana Myers /*
489db2bae30SDana Myers * Increment the method parse tree thread count since it has been
490db2bae30SDana Myers * reentered one more time (even if it is the same thread)
491db2bae30SDana Myers */
492db2bae30SDana Myers ObjDesc->Method.ThreadCount++;
493db2bae30SDana Myers AcpiMethodCount++;
494db2bae30SDana Myers return_ACPI_STATUS (Status);
495db2bae30SDana Myers
496db2bae30SDana Myers
497db2bae30SDana Myers Cleanup:
498db2bae30SDana Myers /* On error, must release the method mutex (if present) */
499db2bae30SDana Myers
500db2bae30SDana Myers if (ObjDesc->Method.Mutex)
501db2bae30SDana Myers {
502db2bae30SDana Myers AcpiOsReleaseMutex (ObjDesc->Method.Mutex->Mutex.OsMutex);
503db2bae30SDana Myers }
504db2bae30SDana Myers return_ACPI_STATUS (Status);
505db2bae30SDana Myers }
506db2bae30SDana Myers
507db2bae30SDana Myers
508db2bae30SDana Myers /*******************************************************************************
509db2bae30SDana Myers *
510db2bae30SDana Myers * FUNCTION: AcpiDsCallControlMethod
511db2bae30SDana Myers *
512db2bae30SDana Myers * PARAMETERS: Thread - Info for this thread
513db2bae30SDana Myers * ThisWalkState - Current walk state
514db2bae30SDana Myers * Op - Current Op to be walked
515db2bae30SDana Myers *
516db2bae30SDana Myers * RETURN: Status
517db2bae30SDana Myers *
518db2bae30SDana Myers * DESCRIPTION: Transfer execution to a called control method
519db2bae30SDana Myers *
520db2bae30SDana Myers ******************************************************************************/
521db2bae30SDana Myers
522db2bae30SDana Myers ACPI_STATUS
AcpiDsCallControlMethod(ACPI_THREAD_STATE * Thread,ACPI_WALK_STATE * ThisWalkState,ACPI_PARSE_OBJECT * Op)523db2bae30SDana Myers AcpiDsCallControlMethod (
524db2bae30SDana Myers ACPI_THREAD_STATE *Thread,
525db2bae30SDana Myers ACPI_WALK_STATE *ThisWalkState,
526db2bae30SDana Myers ACPI_PARSE_OBJECT *Op)
527db2bae30SDana Myers {
528db2bae30SDana Myers ACPI_STATUS Status;
529db2bae30SDana Myers ACPI_NAMESPACE_NODE *MethodNode;
530db2bae30SDana Myers ACPI_WALK_STATE *NextWalkState = NULL;
531db2bae30SDana Myers ACPI_OPERAND_OBJECT *ObjDesc;
532db2bae30SDana Myers ACPI_EVALUATE_INFO *Info;
533db2bae30SDana Myers UINT32 i;
534db2bae30SDana Myers
535db2bae30SDana Myers
536db2bae30SDana Myers ACPI_FUNCTION_TRACE_PTR (DsCallControlMethod, ThisWalkState);
537db2bae30SDana Myers
538*385cc6b4SJerry Jelinek ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
539*385cc6b4SJerry Jelinek "Calling method %p, currentstate=%p\n",
540db2bae30SDana Myers ThisWalkState->PrevOp, ThisWalkState));
541db2bae30SDana Myers
542db2bae30SDana Myers /*
543db2bae30SDana Myers * Get the namespace entry for the control method we are about to call
544db2bae30SDana Myers */
545db2bae30SDana Myers MethodNode = ThisWalkState->MethodCallNode;
546db2bae30SDana Myers if (!MethodNode)
547db2bae30SDana Myers {
548db2bae30SDana Myers return_ACPI_STATUS (AE_NULL_ENTRY);
549db2bae30SDana Myers }
550db2bae30SDana Myers
551db2bae30SDana Myers ObjDesc = AcpiNsGetAttachedObject (MethodNode);
552db2bae30SDana Myers if (!ObjDesc)
553db2bae30SDana Myers {
554db2bae30SDana Myers return_ACPI_STATUS (AE_NULL_OBJECT);
555db2bae30SDana Myers }
556db2bae30SDana Myers
557db2bae30SDana Myers /* Init for new method, possibly wait on method mutex */
558db2bae30SDana Myers
559*385cc6b4SJerry Jelinek Status = AcpiDsBeginMethodExecution (
560*385cc6b4SJerry Jelinek MethodNode, ObjDesc, ThisWalkState);
561db2bae30SDana Myers if (ACPI_FAILURE (Status))
562db2bae30SDana Myers {
563db2bae30SDana Myers return_ACPI_STATUS (Status);
564db2bae30SDana Myers }
565db2bae30SDana Myers
566db2bae30SDana Myers /* Begin method parse/execution. Create a new walk state */
567db2bae30SDana Myers
568*385cc6b4SJerry Jelinek NextWalkState = AcpiDsCreateWalkState (
569*385cc6b4SJerry Jelinek ObjDesc->Method.OwnerId, NULL, ObjDesc, Thread);
570db2bae30SDana Myers if (!NextWalkState)
571db2bae30SDana Myers {
572db2bae30SDana Myers Status = AE_NO_MEMORY;
573db2bae30SDana Myers goto Cleanup;
574db2bae30SDana Myers }
575db2bae30SDana Myers
576db2bae30SDana Myers /*
577db2bae30SDana Myers * The resolved arguments were put on the previous walk state's operand
578db2bae30SDana Myers * stack. Operands on the previous walk state stack always
579db2bae30SDana Myers * start at index 0. Also, null terminate the list of arguments
580db2bae30SDana Myers */
581db2bae30SDana Myers ThisWalkState->Operands [ThisWalkState->NumOperands] = NULL;
582db2bae30SDana Myers
583db2bae30SDana Myers /*
584db2bae30SDana Myers * Allocate and initialize the evaluation information block
585db2bae30SDana Myers * TBD: this is somewhat inefficient, should change interface to
586db2bae30SDana Myers * DsInitAmlWalk. For now, keeps this struct off the CPU stack
587db2bae30SDana Myers */
588db2bae30SDana Myers Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
589db2bae30SDana Myers if (!Info)
590db2bae30SDana Myers {
591*385cc6b4SJerry Jelinek Status = AE_NO_MEMORY;
592*385cc6b4SJerry Jelinek goto Cleanup;
593db2bae30SDana Myers }
594db2bae30SDana Myers
595db2bae30SDana Myers Info->Parameters = &ThisWalkState->Operands[0];
596db2bae30SDana Myers
597db2bae30SDana Myers Status = AcpiDsInitAmlWalk (NextWalkState, NULL, MethodNode,
598db2bae30SDana Myers ObjDesc->Method.AmlStart, ObjDesc->Method.AmlLength,
599db2bae30SDana Myers Info, ACPI_IMODE_EXECUTE);
600db2bae30SDana Myers
601db2bae30SDana Myers ACPI_FREE (Info);
602db2bae30SDana Myers if (ACPI_FAILURE (Status))
603db2bae30SDana Myers {
604db2bae30SDana Myers goto Cleanup;
605db2bae30SDana Myers }
606db2bae30SDana Myers
607db2bae30SDana Myers /*
608db2bae30SDana Myers * Delete the operands on the previous walkstate operand stack
609db2bae30SDana Myers * (they were copied to new objects)
610db2bae30SDana Myers */
611db2bae30SDana Myers for (i = 0; i < ObjDesc->Method.ParamCount; i++)
612db2bae30SDana Myers {
613db2bae30SDana Myers AcpiUtRemoveReference (ThisWalkState->Operands [i]);
614db2bae30SDana Myers ThisWalkState->Operands [i] = NULL;
615db2bae30SDana Myers }
616db2bae30SDana Myers
617db2bae30SDana Myers /* Clear the operand stack */
618db2bae30SDana Myers
619db2bae30SDana Myers ThisWalkState->NumOperands = 0;
620db2bae30SDana Myers
621db2bae30SDana Myers ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
622db2bae30SDana Myers "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
623db2bae30SDana Myers MethodNode->Name.Ascii, NextWalkState));
624db2bae30SDana Myers
625db2bae30SDana Myers /* Invoke an internal method if necessary */
626db2bae30SDana Myers
62726f3cdf0SGordon Ross if (ObjDesc->Method.InfoFlags & ACPI_METHOD_INTERNAL_ONLY)
628db2bae30SDana Myers {
62926f3cdf0SGordon Ross Status = ObjDesc->Method.Dispatch.Implementation (NextWalkState);
630aa2aa9a6SDana Myers if (Status == AE_OK)
631aa2aa9a6SDana Myers {
632aa2aa9a6SDana Myers Status = AE_CTRL_TERMINATE;
633aa2aa9a6SDana Myers }
634db2bae30SDana Myers }
635db2bae30SDana Myers
636db2bae30SDana Myers return_ACPI_STATUS (Status);
637db2bae30SDana Myers
638db2bae30SDana Myers
639db2bae30SDana Myers Cleanup:
640db2bae30SDana Myers
641db2bae30SDana Myers /* On error, we must terminate the method properly */
642db2bae30SDana Myers
643db2bae30SDana Myers AcpiDsTerminateControlMethod (ObjDesc, NextWalkState);
644db2bae30SDana Myers AcpiDsDeleteWalkState (NextWalkState);
645db2bae30SDana Myers
646db2bae30SDana Myers return_ACPI_STATUS (Status);
647db2bae30SDana Myers }
648db2bae30SDana Myers
649db2bae30SDana Myers
650db2bae30SDana Myers /*******************************************************************************
651db2bae30SDana Myers *
652db2bae30SDana Myers * FUNCTION: AcpiDsRestartControlMethod
653db2bae30SDana Myers *
654db2bae30SDana Myers * PARAMETERS: WalkState - State for preempted method (caller)
655db2bae30SDana Myers * ReturnDesc - Return value from the called method
656db2bae30SDana Myers *
657db2bae30SDana Myers * RETURN: Status
658db2bae30SDana Myers *
659db2bae30SDana Myers * DESCRIPTION: Restart a method that was preempted by another (nested) method
660db2bae30SDana Myers * invocation. Handle the return value (if any) from the callee.
661db2bae30SDana Myers *
662db2bae30SDana Myers ******************************************************************************/
663db2bae30SDana Myers
664db2bae30SDana Myers ACPI_STATUS
AcpiDsRestartControlMethod(ACPI_WALK_STATE * WalkState,ACPI_OPERAND_OBJECT * ReturnDesc)665db2bae30SDana Myers AcpiDsRestartControlMethod (
666db2bae30SDana Myers ACPI_WALK_STATE *WalkState,
667db2bae30SDana Myers ACPI_OPERAND_OBJECT *ReturnDesc)
668db2bae30SDana Myers {
669db2bae30SDana Myers ACPI_STATUS Status;
670db2bae30SDana Myers int SameAsImplicitReturn;
671db2bae30SDana Myers
672db2bae30SDana Myers
673db2bae30SDana Myers ACPI_FUNCTION_TRACE_PTR (DsRestartControlMethod, WalkState);
674db2bae30SDana Myers
675db2bae30SDana Myers
676db2bae30SDana Myers ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
677db2bae30SDana Myers "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
678db2bae30SDana Myers AcpiUtGetNodeName (WalkState->MethodNode),
679db2bae30SDana Myers WalkState->MethodCallOp, ReturnDesc));
680db2bae30SDana Myers
681db2bae30SDana Myers ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
682db2bae30SDana Myers " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
683db2bae30SDana Myers WalkState->ReturnUsed,
684db2bae30SDana Myers WalkState->Results, WalkState));
685db2bae30SDana Myers
686db2bae30SDana Myers /* Did the called method return a value? */
687db2bae30SDana Myers
688db2bae30SDana Myers if (ReturnDesc)
689db2bae30SDana Myers {
690db2bae30SDana Myers /* Is the implicit return object the same as the return desc? */
691db2bae30SDana Myers
692db2bae30SDana Myers SameAsImplicitReturn = (WalkState->ImplicitReturnObj == ReturnDesc);
693db2bae30SDana Myers
694db2bae30SDana Myers /* Are we actually going to use the return value? */
695db2bae30SDana Myers
696db2bae30SDana Myers if (WalkState->ReturnUsed)
697db2bae30SDana Myers {
698db2bae30SDana Myers /* Save the return value from the previous method */
699db2bae30SDana Myers
700db2bae30SDana Myers Status = AcpiDsResultPush (ReturnDesc, WalkState);
701db2bae30SDana Myers if (ACPI_FAILURE (Status))
702db2bae30SDana Myers {
703db2bae30SDana Myers AcpiUtRemoveReference (ReturnDesc);
704db2bae30SDana Myers return_ACPI_STATUS (Status);
705db2bae30SDana Myers }
706db2bae30SDana Myers
707db2bae30SDana Myers /*
708db2bae30SDana Myers * Save as THIS method's return value in case it is returned
709db2bae30SDana Myers * immediately to yet another method
710db2bae30SDana Myers */
711db2bae30SDana Myers WalkState->ReturnDesc = ReturnDesc;
712db2bae30SDana Myers }
713db2bae30SDana Myers
714db2bae30SDana Myers /*
715db2bae30SDana Myers * The following code is the optional support for the so-called
716db2bae30SDana Myers * "implicit return". Some AML code assumes that the last value of the
717db2bae30SDana Myers * method is "implicitly" returned to the caller, in the absence of an
718db2bae30SDana Myers * explicit return value.
719db2bae30SDana Myers *
720db2bae30SDana Myers * Just save the last result of the method as the return value.
721db2bae30SDana Myers *
722db2bae30SDana Myers * NOTE: this is optional because the ASL language does not actually
723db2bae30SDana Myers * support this behavior.
724db2bae30SDana Myers */
725db2bae30SDana Myers else if (!AcpiDsDoImplicitReturn (ReturnDesc, WalkState, FALSE) ||
726db2bae30SDana Myers SameAsImplicitReturn)
727db2bae30SDana Myers {
728db2bae30SDana Myers /*
729db2bae30SDana Myers * Delete the return value if it will not be used by the
730db2bae30SDana Myers * calling method or remove one reference if the explicit return
731db2bae30SDana Myers * is the same as the implicit return value.
732db2bae30SDana Myers */
733db2bae30SDana Myers AcpiUtRemoveReference (ReturnDesc);
734db2bae30SDana Myers }
735db2bae30SDana Myers }
736db2bae30SDana Myers
737db2bae30SDana Myers return_ACPI_STATUS (AE_OK);
738db2bae30SDana Myers }
739db2bae30SDana Myers
740db2bae30SDana Myers
741db2bae30SDana Myers /*******************************************************************************
742db2bae30SDana Myers *
743db2bae30SDana Myers * FUNCTION: AcpiDsTerminateControlMethod
744db2bae30SDana Myers *
745db2bae30SDana Myers * PARAMETERS: MethodDesc - Method object
746db2bae30SDana Myers * WalkState - State associated with the method
747db2bae30SDana Myers *
748db2bae30SDana Myers * RETURN: None
749db2bae30SDana Myers *
750db2bae30SDana Myers * DESCRIPTION: Terminate a control method. Delete everything that the method
751db2bae30SDana Myers * created, delete all locals and arguments, and delete the parse
752db2bae30SDana Myers * tree if requested.
753db2bae30SDana Myers *
754db2bae30SDana Myers * MUTEX: Interpreter is locked
755db2bae30SDana Myers *
756db2bae30SDana Myers ******************************************************************************/
757db2bae30SDana Myers
758db2bae30SDana Myers void
AcpiDsTerminateControlMethod(ACPI_OPERAND_OBJECT * MethodDesc,ACPI_WALK_STATE * WalkState)759db2bae30SDana Myers AcpiDsTerminateControlMethod (
760db2bae30SDana Myers ACPI_OPERAND_OBJECT *MethodDesc,
761db2bae30SDana Myers ACPI_WALK_STATE *WalkState)
762db2bae30SDana Myers {
763db2bae30SDana Myers
764db2bae30SDana Myers ACPI_FUNCTION_TRACE_PTR (DsTerminateControlMethod, WalkState);
765db2bae30SDana Myers
766db2bae30SDana Myers
767db2bae30SDana Myers /* MethodDesc is required, WalkState is optional */
768db2bae30SDana Myers
769db2bae30SDana Myers if (!MethodDesc)
770db2bae30SDana Myers {
771db2bae30SDana Myers return_VOID;
772db2bae30SDana Myers }
773db2bae30SDana Myers
774db2bae30SDana Myers if (WalkState)
775db2bae30SDana Myers {
776db2bae30SDana Myers /* Delete all arguments and locals */
777db2bae30SDana Myers
778db2bae30SDana Myers AcpiDsMethodDataDeleteAll (WalkState);
779db2bae30SDana Myers
780db2bae30SDana Myers /*
781db2bae30SDana Myers * If method is serialized, release the mutex and restore the
782db2bae30SDana Myers * current sync level for this thread
783db2bae30SDana Myers */
784db2bae30SDana Myers if (MethodDesc->Method.Mutex)
785db2bae30SDana Myers {
786db2bae30SDana Myers /* Acquisition Depth handles recursive calls */
787db2bae30SDana Myers
788db2bae30SDana Myers MethodDesc->Method.Mutex->Mutex.AcquisitionDepth--;
789db2bae30SDana Myers if (!MethodDesc->Method.Mutex->Mutex.AcquisitionDepth)
790db2bae30SDana Myers {
791db2bae30SDana Myers WalkState->Thread->CurrentSyncLevel =
792db2bae30SDana Myers MethodDesc->Method.Mutex->Mutex.OriginalSyncLevel;
793db2bae30SDana Myers
794*385cc6b4SJerry Jelinek AcpiOsReleaseMutex (
795*385cc6b4SJerry Jelinek MethodDesc->Method.Mutex->Mutex.OsMutex);
796db2bae30SDana Myers MethodDesc->Method.Mutex->Mutex.ThreadId = 0;
797db2bae30SDana Myers }
798db2bae30SDana Myers }
799db2bae30SDana Myers
800db2bae30SDana Myers /*
80157190917SDana Myers * Delete any namespace objects created anywhere within the
80226f3cdf0SGordon Ross * namespace by the execution of this method. Unless:
80326f3cdf0SGordon Ross * 1) This method is a module-level executable code method, in which
80426f3cdf0SGordon Ross * case we want make the objects permanent.
80526f3cdf0SGordon Ross * 2) There are other threads executing the method, in which case we
80626f3cdf0SGordon Ross * will wait until the last thread has completed.
807db2bae30SDana Myers */
80826f3cdf0SGordon Ross if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL) &&
80926f3cdf0SGordon Ross (MethodDesc->Method.ThreadCount == 1))
81026f3cdf0SGordon Ross {
81126f3cdf0SGordon Ross /* Delete any direct children of (created by) this method */
81226f3cdf0SGordon Ross
81326f3cdf0SGordon Ross AcpiNsDeleteNamespaceSubtree (WalkState->MethodNode);
81426f3cdf0SGordon Ross
81526f3cdf0SGordon Ross /*
81626f3cdf0SGordon Ross * Delete any objects that were created by this method
81726f3cdf0SGordon Ross * elsewhere in the namespace (if any were created).
81826f3cdf0SGordon Ross * Use of the ACPI_METHOD_MODIFIED_NAMESPACE optimizes the
81926f3cdf0SGordon Ross * deletion such that we don't have to perform an entire
82026f3cdf0SGordon Ross * namespace walk for every control method execution.
82126f3cdf0SGordon Ross */
82226f3cdf0SGordon Ross if (MethodDesc->Method.InfoFlags & ACPI_METHOD_MODIFIED_NAMESPACE)
82357190917SDana Myers {
824db2bae30SDana Myers AcpiNsDeleteNamespaceByOwner (MethodDesc->Method.OwnerId);
825*385cc6b4SJerry Jelinek MethodDesc->Method.InfoFlags &=
826*385cc6b4SJerry Jelinek ~ACPI_METHOD_MODIFIED_NAMESPACE;
82726f3cdf0SGordon Ross }
828db2bae30SDana Myers }
82957190917SDana Myers }
830db2bae30SDana Myers
831db2bae30SDana Myers /* Decrement the thread count on the method */
832db2bae30SDana Myers
833db2bae30SDana Myers if (MethodDesc->Method.ThreadCount)
834db2bae30SDana Myers {
835db2bae30SDana Myers MethodDesc->Method.ThreadCount--;
836db2bae30SDana Myers }
837db2bae30SDana Myers else
838db2bae30SDana Myers {
839db2bae30SDana Myers ACPI_ERROR ((AE_INFO,
840db2bae30SDana Myers "Invalid zero thread count in method"));
841db2bae30SDana Myers }
842db2bae30SDana Myers
843db2bae30SDana Myers /* Are there any other threads currently executing this method? */
844db2bae30SDana Myers
845db2bae30SDana Myers if (MethodDesc->Method.ThreadCount)
846db2bae30SDana Myers {
847db2bae30SDana Myers /*
848db2bae30SDana Myers * Additional threads. Do not release the OwnerId in this case,
849db2bae30SDana Myers * we immediately reuse it for the next thread executing this method
850db2bae30SDana Myers */
851db2bae30SDana Myers ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
85226f3cdf0SGordon Ross "*** Completed execution of one thread, %u threads remaining\n",
853db2bae30SDana Myers MethodDesc->Method.ThreadCount));
854db2bae30SDana Myers }
855db2bae30SDana Myers else
856db2bae30SDana Myers {
857db2bae30SDana Myers /* This is the only executing thread for this method */
858db2bae30SDana Myers
859db2bae30SDana Myers /*
860db2bae30SDana Myers * Support to dynamically change a method from NotSerialized to
861db2bae30SDana Myers * Serialized if it appears that the method is incorrectly written and
862db2bae30SDana Myers * does not support multiple thread execution. The best example of this
863db2bae30SDana Myers * is if such a method creates namespace objects and blocks. A second
86426f3cdf0SGordon Ross * thread will fail with an AE_ALREADY_EXISTS exception.
865db2bae30SDana Myers *
866db2bae30SDana Myers * This code is here because we must wait until the last thread exits
86726f3cdf0SGordon Ross * before marking the method as serialized.
868db2bae30SDana Myers */
86926f3cdf0SGordon Ross if (MethodDesc->Method.InfoFlags & ACPI_METHOD_SERIALIZED_PENDING)
870db2bae30SDana Myers {
87126f3cdf0SGordon Ross if (WalkState)
87226f3cdf0SGordon Ross {
873*385cc6b4SJerry Jelinek ACPI_INFO ((
874*385cc6b4SJerry Jelinek "Marking method %4.4s as Serialized "
875*385cc6b4SJerry Jelinek "because of AE_ALREADY_EXISTS error",
87626f3cdf0SGordon Ross WalkState->MethodNode->Name.Ascii));
87726f3cdf0SGordon Ross }
87826f3cdf0SGordon Ross
87926f3cdf0SGordon Ross /*
88026f3cdf0SGordon Ross * Method tried to create an object twice and was marked as
88126f3cdf0SGordon Ross * "pending serialized". The probable cause is that the method
88226f3cdf0SGordon Ross * cannot handle reentrancy.
88326f3cdf0SGordon Ross *
88426f3cdf0SGordon Ross * The method was created as NotSerialized, but it tried to create
88526f3cdf0SGordon Ross * a named object and then blocked, causing the second thread
88626f3cdf0SGordon Ross * entrance to begin and then fail. Workaround this problem by
88726f3cdf0SGordon Ross * marking the method permanently as Serialized when the last
88826f3cdf0SGordon Ross * thread exits here.
88926f3cdf0SGordon Ross */
890*385cc6b4SJerry Jelinek MethodDesc->Method.InfoFlags &=
891*385cc6b4SJerry Jelinek ~ACPI_METHOD_SERIALIZED_PENDING;
892*385cc6b4SJerry Jelinek
893*385cc6b4SJerry Jelinek MethodDesc->Method.InfoFlags |=
894*385cc6b4SJerry Jelinek (ACPI_METHOD_SERIALIZED | ACPI_METHOD_IGNORE_SYNC_LEVEL);
89526f3cdf0SGordon Ross MethodDesc->Method.SyncLevel = 0;
896db2bae30SDana Myers }
897db2bae30SDana Myers
898db2bae30SDana Myers /* No more threads, we can free the OwnerId */
899db2bae30SDana Myers
90026f3cdf0SGordon Ross if (!(MethodDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL))
90157190917SDana Myers {
902db2bae30SDana Myers AcpiUtReleaseOwnerId (&MethodDesc->Method.OwnerId);
903db2bae30SDana Myers }
90457190917SDana Myers }
905db2bae30SDana Myers
906*385cc6b4SJerry Jelinek AcpiExStopTraceMethod ((ACPI_NAMESPACE_NODE *) MethodDesc->Method.Node,
907*385cc6b4SJerry Jelinek MethodDesc, WalkState);
908*385cc6b4SJerry Jelinek
909db2bae30SDana Myers return_VOID;
910db2bae30SDana Myers }
911