xref: /minix3/minix/drivers/power/acpi/executer/exoparg3.c (revision 29492bb71c7148a089a5afafa0c99409161218df)
1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * Module Name: exoparg3 - AML execution - opcodes with 3 arguments
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  *****************************************************************************/
6433d6423SLionel Sambuc 
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek  * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc  * All rights reserved.
10433d6423SLionel Sambuc  *
11*29492bb7SDavid van Moolenbroek  * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek  * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek  * are met:
14*29492bb7SDavid van Moolenbroek  * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek  *    notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek  *    without modification.
17*29492bb7SDavid van Moolenbroek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek  *    substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek  *    ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek  *    including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek  *    binary redistribution.
22*29492bb7SDavid van Moolenbroek  * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek  *    of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek  *    from this software without specific prior written permission.
25433d6423SLionel Sambuc  *
26*29492bb7SDavid van Moolenbroek  * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek  * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek  * Software Foundation.
29433d6423SLionel Sambuc  *
30*29492bb7SDavid van Moolenbroek  * NO WARRANTY
31*29492bb7SDavid van Moolenbroek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek  * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek  */
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "acinterp.h"
47433d6423SLionel Sambuc #include "acparser.h"
48433d6423SLionel Sambuc #include "amlcode.h"
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc 
51433d6423SLionel Sambuc #define _COMPONENT          ACPI_EXECUTER
52433d6423SLionel Sambuc         ACPI_MODULE_NAME    ("exoparg3")
53433d6423SLionel Sambuc 
54433d6423SLionel Sambuc 
55433d6423SLionel Sambuc /*!
56433d6423SLionel Sambuc  * Naming convention for AML interpreter execution routines.
57433d6423SLionel Sambuc  *
58433d6423SLionel Sambuc  * The routines that begin execution of AML opcodes are named with a common
59433d6423SLionel Sambuc  * convention based upon the number of arguments, the number of target operands,
60433d6423SLionel Sambuc  * and whether or not a value is returned:
61433d6423SLionel Sambuc  *
62433d6423SLionel Sambuc  *      AcpiExOpcode_xA_yT_zR
63433d6423SLionel Sambuc  *
64433d6423SLionel Sambuc  * Where:
65433d6423SLionel Sambuc  *
66433d6423SLionel Sambuc  * xA - ARGUMENTS:    The number of arguments (input operands) that are
67433d6423SLionel Sambuc  *                    required for this opcode type (1 through 6 args).
68433d6423SLionel Sambuc  * yT - TARGETS:      The number of targets (output operands) that are required
69433d6423SLionel Sambuc  *                    for this opcode type (0, 1, or 2 targets).
70433d6423SLionel Sambuc  * zR - RETURN VALUE: Indicates whether this opcode type returns a value
71433d6423SLionel Sambuc  *                    as the function return (0 or 1).
72433d6423SLionel Sambuc  *
73433d6423SLionel Sambuc  * The AcpiExOpcode* functions are called via the Dispatcher component with
74433d6423SLionel Sambuc  * fully resolved operands.
75433d6423SLionel Sambuc !*/
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc 
78433d6423SLionel Sambuc /*******************************************************************************
79433d6423SLionel Sambuc  *
80433d6423SLionel Sambuc  * FUNCTION:    AcpiExOpcode_3A_0T_0R
81433d6423SLionel Sambuc  *
82433d6423SLionel Sambuc  * PARAMETERS:  WalkState           - Current walk state
83433d6423SLionel Sambuc  *
84433d6423SLionel Sambuc  * RETURN:      Status
85433d6423SLionel Sambuc  *
86433d6423SLionel Sambuc  * DESCRIPTION: Execute Triadic operator (3 operands)
87433d6423SLionel Sambuc  *
88433d6423SLionel Sambuc  ******************************************************************************/
89433d6423SLionel Sambuc 
90433d6423SLionel Sambuc ACPI_STATUS
AcpiExOpcode_3A_0T_0R(ACPI_WALK_STATE * WalkState)91433d6423SLionel Sambuc AcpiExOpcode_3A_0T_0R (
92433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState)
93433d6423SLionel Sambuc {
94433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
95433d6423SLionel Sambuc     ACPI_SIGNAL_FATAL_INFO  *Fatal;
96433d6423SLionel Sambuc     ACPI_STATUS             Status = AE_OK;
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc 
99433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_0T_0R,
100433d6423SLionel Sambuc         AcpiPsGetOpcodeName (WalkState->Opcode));
101433d6423SLionel Sambuc 
102433d6423SLionel Sambuc 
103433d6423SLionel Sambuc     switch (WalkState->Opcode)
104433d6423SLionel Sambuc     {
105433d6423SLionel Sambuc     case AML_FATAL_OP:          /* Fatal (FatalType  FatalCode  FatalArg) */
106433d6423SLionel Sambuc 
107433d6423SLionel Sambuc         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
108433d6423SLionel Sambuc             "FatalOp: Type %X Code %X Arg %X <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
109433d6423SLionel Sambuc             (UINT32) Operand[0]->Integer.Value,
110433d6423SLionel Sambuc             (UINT32) Operand[1]->Integer.Value,
111433d6423SLionel Sambuc             (UINT32) Operand[2]->Integer.Value));
112433d6423SLionel Sambuc 
113433d6423SLionel Sambuc         Fatal = ACPI_ALLOCATE (sizeof (ACPI_SIGNAL_FATAL_INFO));
114433d6423SLionel Sambuc         if (Fatal)
115433d6423SLionel Sambuc         {
116433d6423SLionel Sambuc             Fatal->Type     = (UINT32) Operand[0]->Integer.Value;
117433d6423SLionel Sambuc             Fatal->Code     = (UINT32) Operand[1]->Integer.Value;
118433d6423SLionel Sambuc             Fatal->Argument = (UINT32) Operand[2]->Integer.Value;
119433d6423SLionel Sambuc         }
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc         /* Always signal the OS! */
122433d6423SLionel Sambuc 
123433d6423SLionel Sambuc         Status = AcpiOsSignal (ACPI_SIGNAL_FATAL, Fatal);
124433d6423SLionel Sambuc 
125433d6423SLionel Sambuc         /* Might return while OS is shutting down, just continue */
126433d6423SLionel Sambuc 
127433d6423SLionel Sambuc         ACPI_FREE (Fatal);
128433d6423SLionel Sambuc         break;
129433d6423SLionel Sambuc 
130433d6423SLionel Sambuc     default:
131433d6423SLionel Sambuc 
132433d6423SLionel Sambuc         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
133433d6423SLionel Sambuc             WalkState->Opcode));
134433d6423SLionel Sambuc         Status = AE_AML_BAD_OPCODE;
135433d6423SLionel Sambuc         goto Cleanup;
136433d6423SLionel Sambuc     }
137433d6423SLionel Sambuc 
138433d6423SLionel Sambuc 
139433d6423SLionel Sambuc Cleanup:
140433d6423SLionel Sambuc 
141433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
142433d6423SLionel Sambuc }
143433d6423SLionel Sambuc 
144433d6423SLionel Sambuc 
145433d6423SLionel Sambuc /*******************************************************************************
146433d6423SLionel Sambuc  *
147433d6423SLionel Sambuc  * FUNCTION:    AcpiExOpcode_3A_1T_1R
148433d6423SLionel Sambuc  *
149433d6423SLionel Sambuc  * PARAMETERS:  WalkState           - Current walk state
150433d6423SLionel Sambuc  *
151433d6423SLionel Sambuc  * RETURN:      Status
152433d6423SLionel Sambuc  *
153433d6423SLionel Sambuc  * DESCRIPTION: Execute Triadic operator (3 operands)
154433d6423SLionel Sambuc  *
155433d6423SLionel Sambuc  ******************************************************************************/
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc ACPI_STATUS
AcpiExOpcode_3A_1T_1R(ACPI_WALK_STATE * WalkState)158433d6423SLionel Sambuc AcpiExOpcode_3A_1T_1R (
159433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState)
160433d6423SLionel Sambuc {
161433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
162433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *ReturnDesc = NULL;
163433d6423SLionel Sambuc     char                    *Buffer = NULL;
164433d6423SLionel Sambuc     ACPI_STATUS             Status = AE_OK;
165433d6423SLionel Sambuc     UINT64                  Index;
166433d6423SLionel Sambuc     ACPI_SIZE               Length;
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc 
169433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_STR (ExOpcode_3A_1T_1R,
170433d6423SLionel Sambuc         AcpiPsGetOpcodeName (WalkState->Opcode));
171433d6423SLionel Sambuc 
172433d6423SLionel Sambuc 
173433d6423SLionel Sambuc     switch (WalkState->Opcode)
174433d6423SLionel Sambuc     {
175433d6423SLionel Sambuc     case AML_MID_OP:    /* Mid (Source[0], Index[1], Length[2], Result[3]) */
176433d6423SLionel Sambuc         /*
177433d6423SLionel Sambuc          * Create the return object. The Source operand is guaranteed to be
178433d6423SLionel Sambuc          * either a String or a Buffer, so just use its type.
179433d6423SLionel Sambuc          */
180433d6423SLionel Sambuc         ReturnDesc = AcpiUtCreateInternalObject (
181433d6423SLionel Sambuc                         (Operand[0])->Common.Type);
182433d6423SLionel Sambuc         if (!ReturnDesc)
183433d6423SLionel Sambuc         {
184433d6423SLionel Sambuc             Status = AE_NO_MEMORY;
185433d6423SLionel Sambuc             goto Cleanup;
186433d6423SLionel Sambuc         }
187433d6423SLionel Sambuc 
188433d6423SLionel Sambuc         /* Get the Integer values from the objects */
189433d6423SLionel Sambuc 
190433d6423SLionel Sambuc         Index = Operand[1]->Integer.Value;
191433d6423SLionel Sambuc         Length = (ACPI_SIZE) Operand[2]->Integer.Value;
192433d6423SLionel Sambuc 
193433d6423SLionel Sambuc         /*
194433d6423SLionel Sambuc          * If the index is beyond the length of the String/Buffer, or if the
195433d6423SLionel Sambuc          * requested length is zero, return a zero-length String/Buffer
196433d6423SLionel Sambuc          */
197433d6423SLionel Sambuc         if (Index >= Operand[0]->String.Length)
198433d6423SLionel Sambuc         {
199433d6423SLionel Sambuc             Length = 0;
200433d6423SLionel Sambuc         }
201433d6423SLionel Sambuc 
202433d6423SLionel Sambuc         /* Truncate request if larger than the actual String/Buffer */
203433d6423SLionel Sambuc 
204433d6423SLionel Sambuc         else if ((Index + Length) > Operand[0]->String.Length)
205433d6423SLionel Sambuc         {
206433d6423SLionel Sambuc             Length = (ACPI_SIZE) Operand[0]->String.Length -
207433d6423SLionel Sambuc                         (ACPI_SIZE) Index;
208433d6423SLionel Sambuc         }
209433d6423SLionel Sambuc 
210433d6423SLionel Sambuc         /* Strings always have a sub-pointer, not so for buffers */
211433d6423SLionel Sambuc 
212433d6423SLionel Sambuc         switch ((Operand[0])->Common.Type)
213433d6423SLionel Sambuc         {
214433d6423SLionel Sambuc         case ACPI_TYPE_STRING:
215433d6423SLionel Sambuc 
216433d6423SLionel Sambuc             /* Always allocate a new buffer for the String */
217433d6423SLionel Sambuc 
218433d6423SLionel Sambuc             Buffer = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE) Length + 1);
219433d6423SLionel Sambuc             if (!Buffer)
220433d6423SLionel Sambuc             {
221433d6423SLionel Sambuc                 Status = AE_NO_MEMORY;
222433d6423SLionel Sambuc                 goto Cleanup;
223433d6423SLionel Sambuc             }
224433d6423SLionel Sambuc             break;
225433d6423SLionel Sambuc 
226433d6423SLionel Sambuc         case ACPI_TYPE_BUFFER:
227433d6423SLionel Sambuc 
228433d6423SLionel Sambuc             /* If the requested length is zero, don't allocate a buffer */
229433d6423SLionel Sambuc 
230433d6423SLionel Sambuc             if (Length > 0)
231433d6423SLionel Sambuc             {
232433d6423SLionel Sambuc                 /* Allocate a new buffer for the Buffer */
233433d6423SLionel Sambuc 
234433d6423SLionel Sambuc                 Buffer = ACPI_ALLOCATE_ZEROED (Length);
235433d6423SLionel Sambuc                 if (!Buffer)
236433d6423SLionel Sambuc                 {
237433d6423SLionel Sambuc                     Status = AE_NO_MEMORY;
238433d6423SLionel Sambuc                     goto Cleanup;
239433d6423SLionel Sambuc                 }
240433d6423SLionel Sambuc             }
241433d6423SLionel Sambuc             break;
242433d6423SLionel Sambuc 
243433d6423SLionel Sambuc         default:                        /* Should not happen */
244433d6423SLionel Sambuc 
245433d6423SLionel Sambuc             Status = AE_AML_OPERAND_TYPE;
246433d6423SLionel Sambuc             goto Cleanup;
247433d6423SLionel Sambuc         }
248433d6423SLionel Sambuc 
249433d6423SLionel Sambuc         if (Buffer)
250433d6423SLionel Sambuc         {
251433d6423SLionel Sambuc             /* We have a buffer, copy the portion requested */
252433d6423SLionel Sambuc 
253433d6423SLionel Sambuc             ACPI_MEMCPY (Buffer, Operand[0]->String.Pointer + Index,
254433d6423SLionel Sambuc                          Length);
255433d6423SLionel Sambuc         }
256433d6423SLionel Sambuc 
257433d6423SLionel Sambuc         /* Set the length of the new String/Buffer */
258433d6423SLionel Sambuc 
259433d6423SLionel Sambuc         ReturnDesc->String.Pointer = Buffer;
260433d6423SLionel Sambuc         ReturnDesc->String.Length = (UINT32) Length;
261433d6423SLionel Sambuc 
262433d6423SLionel Sambuc         /* Mark buffer initialized */
263433d6423SLionel Sambuc 
264433d6423SLionel Sambuc         ReturnDesc->Buffer.Flags |= AOPOBJ_DATA_VALID;
265433d6423SLionel Sambuc         break;
266433d6423SLionel Sambuc 
267433d6423SLionel Sambuc     default:
268433d6423SLionel Sambuc 
269433d6423SLionel Sambuc         ACPI_ERROR ((AE_INFO, "Unknown AML opcode 0x%X",
270433d6423SLionel Sambuc             WalkState->Opcode));
271433d6423SLionel Sambuc         Status = AE_AML_BAD_OPCODE;
272433d6423SLionel Sambuc         goto Cleanup;
273433d6423SLionel Sambuc     }
274433d6423SLionel Sambuc 
275433d6423SLionel Sambuc     /* Store the result in the target */
276433d6423SLionel Sambuc 
277433d6423SLionel Sambuc     Status = AcpiExStore (ReturnDesc, Operand[3], WalkState);
278433d6423SLionel Sambuc 
279433d6423SLionel Sambuc Cleanup:
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc     /* Delete return object on error */
282433d6423SLionel Sambuc 
283433d6423SLionel Sambuc     if (ACPI_FAILURE (Status) || WalkState->ResultObj)
284433d6423SLionel Sambuc     {
285433d6423SLionel Sambuc         AcpiUtRemoveReference (ReturnDesc);
286433d6423SLionel Sambuc         WalkState->ResultObj = NULL;
287433d6423SLionel Sambuc     }
288433d6423SLionel Sambuc 
289433d6423SLionel Sambuc     /* Set the return object and exit */
290433d6423SLionel Sambuc 
291433d6423SLionel Sambuc     else
292433d6423SLionel Sambuc     {
293433d6423SLionel Sambuc         WalkState->ResultObj = ReturnDesc;
294433d6423SLionel Sambuc     }
295433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
296433d6423SLionel Sambuc }
297