xref: /minix3/minix/drivers/power/acpi/executer/exconfig.c (revision 29492bb71c7148a089a5afafa0c99409161218df)
1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * Module Name: exconfig - Namespace reconfiguration (Load/Unload opcodes)
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 "acnamesp.h"
48433d6423SLionel Sambuc #include "actables.h"
49433d6423SLionel Sambuc #include "acdispat.h"
50433d6423SLionel Sambuc #include "acevents.h"
51*29492bb7SDavid van Moolenbroek #include "amlcode.h"
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc 
54433d6423SLionel Sambuc #define _COMPONENT          ACPI_EXECUTER
55433d6423SLionel Sambuc         ACPI_MODULE_NAME    ("exconfig")
56433d6423SLionel Sambuc 
57433d6423SLionel Sambuc /* Local prototypes */
58433d6423SLionel Sambuc 
59433d6423SLionel Sambuc static ACPI_STATUS
60433d6423SLionel Sambuc AcpiExAddTable (
61433d6423SLionel Sambuc     UINT32                  TableIndex,
62433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *ParentNode,
63433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     **DdbHandle);
64433d6423SLionel Sambuc 
65433d6423SLionel Sambuc static ACPI_STATUS
66433d6423SLionel Sambuc AcpiExRegionRead (
67433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *ObjDesc,
68433d6423SLionel Sambuc     UINT32                  Length,
69433d6423SLionel Sambuc     UINT8                   *Buffer);
70433d6423SLionel Sambuc 
71433d6423SLionel Sambuc 
72433d6423SLionel Sambuc /*******************************************************************************
73433d6423SLionel Sambuc  *
74433d6423SLionel Sambuc  * FUNCTION:    AcpiExAddTable
75433d6423SLionel Sambuc  *
76433d6423SLionel Sambuc  * PARAMETERS:  Table               - Pointer to raw table
77433d6423SLionel Sambuc  *              ParentNode          - Where to load the table (scope)
78433d6423SLionel Sambuc  *              DdbHandle           - Where to return the table handle.
79433d6423SLionel Sambuc  *
80433d6423SLionel Sambuc  * RETURN:      Status
81433d6423SLionel Sambuc  *
82433d6423SLionel Sambuc  * DESCRIPTION: Common function to Install and Load an ACPI table with a
83433d6423SLionel Sambuc  *              returned table handle.
84433d6423SLionel Sambuc  *
85433d6423SLionel Sambuc  ******************************************************************************/
86433d6423SLionel Sambuc 
87433d6423SLionel Sambuc static ACPI_STATUS
AcpiExAddTable(UINT32 TableIndex,ACPI_NAMESPACE_NODE * ParentNode,ACPI_OPERAND_OBJECT ** DdbHandle)88433d6423SLionel Sambuc AcpiExAddTable (
89433d6423SLionel Sambuc     UINT32                  TableIndex,
90433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *ParentNode,
91433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     **DdbHandle)
92433d6423SLionel Sambuc {
93433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *ObjDesc;
94433d6423SLionel Sambuc     ACPI_STATUS             Status;
95433d6423SLionel Sambuc     ACPI_OWNER_ID           OwnerId;
96433d6423SLionel Sambuc 
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (ExAddTable);
99433d6423SLionel Sambuc 
100433d6423SLionel Sambuc 
101433d6423SLionel Sambuc     /* Create an object to be the table handle */
102433d6423SLionel Sambuc 
103433d6423SLionel Sambuc     ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
104433d6423SLionel Sambuc     if (!ObjDesc)
105433d6423SLionel Sambuc     {
106433d6423SLionel Sambuc         return_ACPI_STATUS (AE_NO_MEMORY);
107433d6423SLionel Sambuc     }
108433d6423SLionel Sambuc 
109433d6423SLionel Sambuc     /* Init the table handle */
110433d6423SLionel Sambuc 
111433d6423SLionel Sambuc     ObjDesc->Common.Flags |= AOPOBJ_DATA_VALID;
112433d6423SLionel Sambuc     ObjDesc->Reference.Class = ACPI_REFCLASS_TABLE;
113433d6423SLionel Sambuc     *DdbHandle = ObjDesc;
114433d6423SLionel Sambuc 
115433d6423SLionel Sambuc     /* Install the new table into the local data structures */
116433d6423SLionel Sambuc 
117433d6423SLionel Sambuc     ObjDesc->Reference.Value = TableIndex;
118433d6423SLionel Sambuc 
119433d6423SLionel Sambuc     /* Add the table to the namespace */
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc     Status = AcpiNsLoadTable (TableIndex, ParentNode);
122433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
123433d6423SLionel Sambuc     {
124433d6423SLionel Sambuc         AcpiUtRemoveReference (ObjDesc);
125433d6423SLionel Sambuc         *DdbHandle = NULL;
126433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
127433d6423SLionel Sambuc     }
128433d6423SLionel Sambuc 
129433d6423SLionel Sambuc     /* Execute any module-level code that was found in the table */
130433d6423SLionel Sambuc 
131433d6423SLionel Sambuc     AcpiExExitInterpreter ();
132433d6423SLionel Sambuc     AcpiNsExecModuleCodeList ();
133433d6423SLionel Sambuc     AcpiExEnterInterpreter ();
134433d6423SLionel Sambuc 
135*29492bb7SDavid van Moolenbroek     /*
136*29492bb7SDavid van Moolenbroek      * Update GPEs for any new _Lxx/_Exx methods. Ignore errors. The host is
137*29492bb7SDavid van Moolenbroek      * responsible for discovering any new wake GPEs by running _PRW methods
138*29492bb7SDavid van Moolenbroek      * that may have been loaded by this table.
139*29492bb7SDavid van Moolenbroek      */
140433d6423SLionel Sambuc     Status = AcpiTbGetOwnerId (TableIndex, &OwnerId);
141433d6423SLionel Sambuc     if (ACPI_SUCCESS (Status))
142433d6423SLionel Sambuc     {
143433d6423SLionel Sambuc         AcpiEvUpdateGpes (OwnerId);
144433d6423SLionel Sambuc     }
145433d6423SLionel Sambuc 
146433d6423SLionel Sambuc     return_ACPI_STATUS (AE_OK);
147433d6423SLionel Sambuc }
148433d6423SLionel Sambuc 
149433d6423SLionel Sambuc 
150433d6423SLionel Sambuc /*******************************************************************************
151433d6423SLionel Sambuc  *
152433d6423SLionel Sambuc  * FUNCTION:    AcpiExLoadTableOp
153433d6423SLionel Sambuc  *
154433d6423SLionel Sambuc  * PARAMETERS:  WalkState           - Current state with operands
155433d6423SLionel Sambuc  *              ReturnDesc          - Where to store the return object
156433d6423SLionel Sambuc  *
157433d6423SLionel Sambuc  * RETURN:      Status
158433d6423SLionel Sambuc  *
159433d6423SLionel Sambuc  * DESCRIPTION: Load an ACPI table from the RSDT/XSDT
160433d6423SLionel Sambuc  *
161433d6423SLionel Sambuc  ******************************************************************************/
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc ACPI_STATUS
AcpiExLoadTableOp(ACPI_WALK_STATE * WalkState,ACPI_OPERAND_OBJECT ** ReturnDesc)164433d6423SLionel Sambuc AcpiExLoadTableOp (
165433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState,
166433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     **ReturnDesc)
167433d6423SLionel Sambuc {
168433d6423SLionel Sambuc     ACPI_STATUS             Status;
169433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     **Operand = &WalkState->Operands[0];
170433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *ParentNode;
171433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *StartNode;
172433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *ParameterNode = NULL;
173433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *DdbHandle;
174433d6423SLionel Sambuc     ACPI_TABLE_HEADER       *Table;
175433d6423SLionel Sambuc     UINT32                  TableIndex;
176433d6423SLionel Sambuc 
177433d6423SLionel Sambuc 
178433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (ExLoadTableOp);
179433d6423SLionel Sambuc 
180433d6423SLionel Sambuc 
181*29492bb7SDavid van Moolenbroek     /* Validate lengths for the Signature, OemId, and OemTableId strings */
182433d6423SLionel Sambuc 
183433d6423SLionel Sambuc     if ((Operand[0]->String.Length > ACPI_NAME_SIZE) ||
184433d6423SLionel Sambuc         (Operand[1]->String.Length > ACPI_OEM_ID_SIZE) ||
185433d6423SLionel Sambuc         (Operand[2]->String.Length > ACPI_OEM_TABLE_ID_SIZE))
186433d6423SLionel Sambuc     {
187*29492bb7SDavid van Moolenbroek         return_ACPI_STATUS (AE_AML_STRING_LIMIT);
188433d6423SLionel Sambuc     }
189433d6423SLionel Sambuc 
190433d6423SLionel Sambuc     /* Find the ACPI table in the RSDT/XSDT */
191433d6423SLionel Sambuc 
192*29492bb7SDavid van Moolenbroek     Status = AcpiTbFindTable (
193*29492bb7SDavid van Moolenbroek                 Operand[0]->String.Pointer,
194433d6423SLionel Sambuc                 Operand[1]->String.Pointer,
195433d6423SLionel Sambuc                 Operand[2]->String.Pointer, &TableIndex);
196433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
197433d6423SLionel Sambuc     {
198433d6423SLionel Sambuc         if (Status != AE_NOT_FOUND)
199433d6423SLionel Sambuc         {
200433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
201433d6423SLionel Sambuc         }
202433d6423SLionel Sambuc 
203433d6423SLionel Sambuc         /* Table not found, return an Integer=0 and AE_OK */
204433d6423SLionel Sambuc 
205433d6423SLionel Sambuc         DdbHandle = AcpiUtCreateIntegerObject ((UINT64) 0);
206433d6423SLionel Sambuc         if (!DdbHandle)
207433d6423SLionel Sambuc         {
208433d6423SLionel Sambuc             return_ACPI_STATUS (AE_NO_MEMORY);
209433d6423SLionel Sambuc         }
210433d6423SLionel Sambuc 
211433d6423SLionel Sambuc         *ReturnDesc = DdbHandle;
212433d6423SLionel Sambuc         return_ACPI_STATUS (AE_OK);
213433d6423SLionel Sambuc     }
214433d6423SLionel Sambuc 
215433d6423SLionel Sambuc     /* Default nodes */
216433d6423SLionel Sambuc 
217433d6423SLionel Sambuc     StartNode = WalkState->ScopeInfo->Scope.Node;
218433d6423SLionel Sambuc     ParentNode = AcpiGbl_RootNode;
219433d6423SLionel Sambuc 
220433d6423SLionel Sambuc     /* RootPath (optional parameter) */
221433d6423SLionel Sambuc 
222433d6423SLionel Sambuc     if (Operand[3]->String.Length > 0)
223433d6423SLionel Sambuc     {
224433d6423SLionel Sambuc         /*
225433d6423SLionel Sambuc          * Find the node referenced by the RootPathString. This is the
226433d6423SLionel Sambuc          * location within the namespace where the table will be loaded.
227433d6423SLionel Sambuc          */
228433d6423SLionel Sambuc         Status = AcpiNsGetNode (StartNode, Operand[3]->String.Pointer,
229433d6423SLionel Sambuc                     ACPI_NS_SEARCH_PARENT, &ParentNode);
230433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
231433d6423SLionel Sambuc         {
232433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
233433d6423SLionel Sambuc         }
234433d6423SLionel Sambuc     }
235433d6423SLionel Sambuc 
236433d6423SLionel Sambuc     /* ParameterPath (optional parameter) */
237433d6423SLionel Sambuc 
238433d6423SLionel Sambuc     if (Operand[4]->String.Length > 0)
239433d6423SLionel Sambuc     {
240*29492bb7SDavid van Moolenbroek         if ((Operand[4]->String.Pointer[0] != AML_ROOT_PREFIX) &&
241*29492bb7SDavid van Moolenbroek             (Operand[4]->String.Pointer[0] != AML_PARENT_PREFIX))
242433d6423SLionel Sambuc         {
243433d6423SLionel Sambuc             /*
244433d6423SLionel Sambuc              * Path is not absolute, so it will be relative to the node
245433d6423SLionel Sambuc              * referenced by the RootPathString (or the NS root if omitted)
246433d6423SLionel Sambuc              */
247433d6423SLionel Sambuc             StartNode = ParentNode;
248433d6423SLionel Sambuc         }
249433d6423SLionel Sambuc 
250433d6423SLionel Sambuc         /* Find the node referenced by the ParameterPathString */
251433d6423SLionel Sambuc 
252433d6423SLionel Sambuc         Status = AcpiNsGetNode (StartNode, Operand[4]->String.Pointer,
253433d6423SLionel Sambuc                     ACPI_NS_SEARCH_PARENT, &ParameterNode);
254433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
255433d6423SLionel Sambuc         {
256433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
257433d6423SLionel Sambuc         }
258433d6423SLionel Sambuc     }
259433d6423SLionel Sambuc 
260433d6423SLionel Sambuc     /* Load the table into the namespace */
261433d6423SLionel Sambuc 
262433d6423SLionel Sambuc     Status = AcpiExAddTable (TableIndex, ParentNode, &DdbHandle);
263433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
264433d6423SLionel Sambuc     {
265433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
266433d6423SLionel Sambuc     }
267433d6423SLionel Sambuc 
268433d6423SLionel Sambuc     /* Parameter Data (optional) */
269433d6423SLionel Sambuc 
270433d6423SLionel Sambuc     if (ParameterNode)
271433d6423SLionel Sambuc     {
272433d6423SLionel Sambuc         /* Store the parameter data into the optional parameter object */
273433d6423SLionel Sambuc 
274433d6423SLionel Sambuc         Status = AcpiExStore (Operand[5],
275433d6423SLionel Sambuc                     ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, ParameterNode),
276433d6423SLionel Sambuc                     WalkState);
277433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
278433d6423SLionel Sambuc         {
279433d6423SLionel Sambuc             (void) AcpiExUnloadTable (DdbHandle);
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc             AcpiUtRemoveReference (DdbHandle);
282433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
283433d6423SLionel Sambuc         }
284433d6423SLionel Sambuc     }
285433d6423SLionel Sambuc 
286433d6423SLionel Sambuc     Status = AcpiGetTableByIndex (TableIndex, &Table);
287433d6423SLionel Sambuc     if (ACPI_SUCCESS (Status))
288433d6423SLionel Sambuc     {
289433d6423SLionel Sambuc         ACPI_INFO ((AE_INFO, "Dynamic OEM Table Load:"));
290433d6423SLionel Sambuc         AcpiTbPrintTableHeader (0, Table);
291433d6423SLionel Sambuc     }
292433d6423SLionel Sambuc 
293433d6423SLionel Sambuc     /* Invoke table handler if present */
294433d6423SLionel Sambuc 
295433d6423SLionel Sambuc     if (AcpiGbl_TableHandler)
296433d6423SLionel Sambuc     {
297433d6423SLionel Sambuc         (void) AcpiGbl_TableHandler (ACPI_TABLE_EVENT_LOAD, Table,
298433d6423SLionel Sambuc                     AcpiGbl_TableHandlerContext);
299433d6423SLionel Sambuc     }
300433d6423SLionel Sambuc 
301433d6423SLionel Sambuc     *ReturnDesc = DdbHandle;
302433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
303433d6423SLionel Sambuc }
304433d6423SLionel Sambuc 
305433d6423SLionel Sambuc 
306433d6423SLionel Sambuc /*******************************************************************************
307433d6423SLionel Sambuc  *
308433d6423SLionel Sambuc  * FUNCTION:    AcpiExRegionRead
309433d6423SLionel Sambuc  *
310433d6423SLionel Sambuc  * PARAMETERS:  ObjDesc         - Region descriptor
311433d6423SLionel Sambuc  *              Length          - Number of bytes to read
312433d6423SLionel Sambuc  *              Buffer          - Pointer to where to put the data
313433d6423SLionel Sambuc  *
314433d6423SLionel Sambuc  * RETURN:      Status
315433d6423SLionel Sambuc  *
316433d6423SLionel Sambuc  * DESCRIPTION: Read data from an operation region. The read starts from the
317433d6423SLionel Sambuc  *              beginning of the region.
318433d6423SLionel Sambuc  *
319433d6423SLionel Sambuc  ******************************************************************************/
320433d6423SLionel Sambuc 
321433d6423SLionel Sambuc static ACPI_STATUS
AcpiExRegionRead(ACPI_OPERAND_OBJECT * ObjDesc,UINT32 Length,UINT8 * Buffer)322433d6423SLionel Sambuc AcpiExRegionRead (
323433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *ObjDesc,
324433d6423SLionel Sambuc     UINT32                  Length,
325433d6423SLionel Sambuc     UINT8                   *Buffer)
326433d6423SLionel Sambuc {
327433d6423SLionel Sambuc     ACPI_STATUS             Status;
328433d6423SLionel Sambuc     UINT64                  Value;
329433d6423SLionel Sambuc     UINT32                  RegionOffset = 0;
330433d6423SLionel Sambuc     UINT32                  i;
331433d6423SLionel Sambuc 
332433d6423SLionel Sambuc 
333433d6423SLionel Sambuc     /* Bytewise reads */
334433d6423SLionel Sambuc 
335433d6423SLionel Sambuc     for (i = 0; i < Length; i++)
336433d6423SLionel Sambuc     {
337*29492bb7SDavid van Moolenbroek         Status = AcpiEvAddressSpaceDispatch (ObjDesc, NULL, ACPI_READ,
338433d6423SLionel Sambuc                     RegionOffset, 8, &Value);
339433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
340433d6423SLionel Sambuc         {
341433d6423SLionel Sambuc             return (Status);
342433d6423SLionel Sambuc         }
343433d6423SLionel Sambuc 
344433d6423SLionel Sambuc         *Buffer = (UINT8) Value;
345433d6423SLionel Sambuc         Buffer++;
346433d6423SLionel Sambuc         RegionOffset++;
347433d6423SLionel Sambuc     }
348433d6423SLionel Sambuc 
349433d6423SLionel Sambuc     return (AE_OK);
350433d6423SLionel Sambuc }
351433d6423SLionel Sambuc 
352433d6423SLionel Sambuc 
353433d6423SLionel Sambuc /*******************************************************************************
354433d6423SLionel Sambuc  *
355433d6423SLionel Sambuc  * FUNCTION:    AcpiExLoadOp
356433d6423SLionel Sambuc  *
357433d6423SLionel Sambuc  * PARAMETERS:  ObjDesc         - Region or Buffer/Field where the table will be
358433d6423SLionel Sambuc  *                                obtained
359433d6423SLionel Sambuc  *              Target          - Where a handle to the table will be stored
360433d6423SLionel Sambuc  *              WalkState       - Current state
361433d6423SLionel Sambuc  *
362433d6423SLionel Sambuc  * RETURN:      Status
363433d6423SLionel Sambuc  *
364433d6423SLionel Sambuc  * DESCRIPTION: Load an ACPI table from a field or operation region
365433d6423SLionel Sambuc  *
366433d6423SLionel Sambuc  * NOTE: Region Fields (Field, BankField, IndexFields) are resolved to buffer
367433d6423SLionel Sambuc  *       objects before this code is reached.
368433d6423SLionel Sambuc  *
369433d6423SLionel Sambuc  *       If source is an operation region, it must refer to SystemMemory, as
370433d6423SLionel Sambuc  *       per the ACPI specification.
371433d6423SLionel Sambuc  *
372433d6423SLionel Sambuc  ******************************************************************************/
373433d6423SLionel Sambuc 
374433d6423SLionel Sambuc ACPI_STATUS
AcpiExLoadOp(ACPI_OPERAND_OBJECT * ObjDesc,ACPI_OPERAND_OBJECT * Target,ACPI_WALK_STATE * WalkState)375433d6423SLionel Sambuc AcpiExLoadOp (
376433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *ObjDesc,
377433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *Target,
378433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState)
379433d6423SLionel Sambuc {
380433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *DdbHandle;
381*29492bb7SDavid van Moolenbroek     ACPI_TABLE_HEADER       *TableHeader;
382433d6423SLionel Sambuc     ACPI_TABLE_HEADER       *Table;
383433d6423SLionel Sambuc     UINT32                  TableIndex;
384433d6423SLionel Sambuc     ACPI_STATUS             Status;
385433d6423SLionel Sambuc     UINT32                  Length;
386433d6423SLionel Sambuc 
387433d6423SLionel Sambuc 
388433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (ExLoadOp);
389433d6423SLionel Sambuc 
390433d6423SLionel Sambuc 
391433d6423SLionel Sambuc     /* Source Object can be either an OpRegion or a Buffer/Field */
392433d6423SLionel Sambuc 
393433d6423SLionel Sambuc     switch (ObjDesc->Common.Type)
394433d6423SLionel Sambuc     {
395433d6423SLionel Sambuc     case ACPI_TYPE_REGION:
396433d6423SLionel Sambuc 
397433d6423SLionel Sambuc         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
398433d6423SLionel Sambuc             "Load table from Region %p\n", ObjDesc));
399433d6423SLionel Sambuc 
400433d6423SLionel Sambuc         /* Region must be SystemMemory (from ACPI spec) */
401433d6423SLionel Sambuc 
402433d6423SLionel Sambuc         if (ObjDesc->Region.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
403433d6423SLionel Sambuc         {
404433d6423SLionel Sambuc             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
405433d6423SLionel Sambuc         }
406433d6423SLionel Sambuc 
407433d6423SLionel Sambuc         /*
408433d6423SLionel Sambuc          * If the Region Address and Length have not been previously evaluated,
409433d6423SLionel Sambuc          * evaluate them now and save the results.
410433d6423SLionel Sambuc          */
411433d6423SLionel Sambuc         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
412433d6423SLionel Sambuc         {
413433d6423SLionel Sambuc             Status = AcpiDsGetRegionArguments (ObjDesc);
414433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
415433d6423SLionel Sambuc             {
416433d6423SLionel Sambuc                 return_ACPI_STATUS (Status);
417433d6423SLionel Sambuc             }
418433d6423SLionel Sambuc         }
419433d6423SLionel Sambuc 
420433d6423SLionel Sambuc         /* Get the table header first so we can get the table length */
421433d6423SLionel Sambuc 
422*29492bb7SDavid van Moolenbroek         TableHeader = ACPI_ALLOCATE (sizeof (ACPI_TABLE_HEADER));
423*29492bb7SDavid van Moolenbroek         if (!TableHeader)
424433d6423SLionel Sambuc         {
425433d6423SLionel Sambuc             return_ACPI_STATUS (AE_NO_MEMORY);
426433d6423SLionel Sambuc         }
427433d6423SLionel Sambuc 
428433d6423SLionel Sambuc         Status = AcpiExRegionRead (ObjDesc, sizeof (ACPI_TABLE_HEADER),
429*29492bb7SDavid van Moolenbroek                     ACPI_CAST_PTR (UINT8, TableHeader));
430*29492bb7SDavid van Moolenbroek         Length = TableHeader->Length;
431*29492bb7SDavid van Moolenbroek         ACPI_FREE (TableHeader);
432433d6423SLionel Sambuc 
433433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
434433d6423SLionel Sambuc         {
435433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
436433d6423SLionel Sambuc         }
437433d6423SLionel Sambuc 
438433d6423SLionel Sambuc         /* Must have at least an ACPI table header */
439433d6423SLionel Sambuc 
440433d6423SLionel Sambuc         if (Length < sizeof (ACPI_TABLE_HEADER))
441433d6423SLionel Sambuc         {
442433d6423SLionel Sambuc             return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
443433d6423SLionel Sambuc         }
444433d6423SLionel Sambuc 
445433d6423SLionel Sambuc         /*
446433d6423SLionel Sambuc          * The original implementation simply mapped the table, with no copy.
447433d6423SLionel Sambuc          * However, the memory region is not guaranteed to remain stable and
448433d6423SLionel Sambuc          * we must copy the table to a local buffer. For example, the memory
449433d6423SLionel Sambuc          * region is corrupted after suspend on some machines. Dynamically
450433d6423SLionel Sambuc          * loaded tables are usually small, so this overhead is minimal.
451433d6423SLionel Sambuc          *
452433d6423SLionel Sambuc          * The latest implementation (5/2009) does not use a mapping at all.
453433d6423SLionel Sambuc          * We use the low-level operation region interface to read the table
454433d6423SLionel Sambuc          * instead of the obvious optimization of using a direct mapping.
455433d6423SLionel Sambuc          * This maintains a consistent use of operation regions across the
456433d6423SLionel Sambuc          * entire subsystem. This is important if additional processing must
457433d6423SLionel Sambuc          * be performed in the (possibly user-installed) operation region
458433d6423SLionel Sambuc          * handler. For example, AcpiExec and ASLTS depend on this.
459433d6423SLionel Sambuc          */
460433d6423SLionel Sambuc 
461433d6423SLionel Sambuc         /* Allocate a buffer for the table */
462433d6423SLionel Sambuc 
463*29492bb7SDavid van Moolenbroek         Table = ACPI_ALLOCATE (Length);
464*29492bb7SDavid van Moolenbroek         if (!Table)
465433d6423SLionel Sambuc         {
466433d6423SLionel Sambuc             return_ACPI_STATUS (AE_NO_MEMORY);
467433d6423SLionel Sambuc         }
468433d6423SLionel Sambuc 
469433d6423SLionel Sambuc         /* Read the entire table */
470433d6423SLionel Sambuc 
471433d6423SLionel Sambuc         Status = AcpiExRegionRead (ObjDesc, Length,
472*29492bb7SDavid van Moolenbroek                     ACPI_CAST_PTR (UINT8, Table));
473433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
474433d6423SLionel Sambuc         {
475*29492bb7SDavid van Moolenbroek             ACPI_FREE (Table);
476433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
477433d6423SLionel Sambuc         }
478433d6423SLionel Sambuc         break;
479433d6423SLionel Sambuc 
480433d6423SLionel Sambuc     case ACPI_TYPE_BUFFER: /* Buffer or resolved RegionField */
481433d6423SLionel Sambuc 
482433d6423SLionel Sambuc         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
483433d6423SLionel Sambuc             "Load table from Buffer or Field %p\n", ObjDesc));
484433d6423SLionel Sambuc 
485433d6423SLionel Sambuc         /* Must have at least an ACPI table header */
486433d6423SLionel Sambuc 
487433d6423SLionel Sambuc         if (ObjDesc->Buffer.Length < sizeof (ACPI_TABLE_HEADER))
488433d6423SLionel Sambuc         {
489433d6423SLionel Sambuc             return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
490433d6423SLionel Sambuc         }
491433d6423SLionel Sambuc 
492433d6423SLionel Sambuc         /* Get the actual table length from the table header */
493433d6423SLionel Sambuc 
494*29492bb7SDavid van Moolenbroek         TableHeader = ACPI_CAST_PTR (ACPI_TABLE_HEADER, ObjDesc->Buffer.Pointer);
495*29492bb7SDavid van Moolenbroek         Length = TableHeader->Length;
496433d6423SLionel Sambuc 
497433d6423SLionel Sambuc         /* Table cannot extend beyond the buffer */
498433d6423SLionel Sambuc 
499433d6423SLionel Sambuc         if (Length > ObjDesc->Buffer.Length)
500433d6423SLionel Sambuc         {
501433d6423SLionel Sambuc             return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
502433d6423SLionel Sambuc         }
503433d6423SLionel Sambuc         if (Length < sizeof (ACPI_TABLE_HEADER))
504433d6423SLionel Sambuc         {
505433d6423SLionel Sambuc             return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);
506433d6423SLionel Sambuc         }
507433d6423SLionel Sambuc 
508433d6423SLionel Sambuc         /*
509433d6423SLionel Sambuc          * Copy the table from the buffer because the buffer could be modified
510433d6423SLionel Sambuc          * or even deleted in the future
511433d6423SLionel Sambuc          */
512*29492bb7SDavid van Moolenbroek         Table = ACPI_ALLOCATE (Length);
513*29492bb7SDavid van Moolenbroek         if (!Table)
514433d6423SLionel Sambuc         {
515433d6423SLionel Sambuc             return_ACPI_STATUS (AE_NO_MEMORY);
516433d6423SLionel Sambuc         }
517433d6423SLionel Sambuc 
518*29492bb7SDavid van Moolenbroek         ACPI_MEMCPY (Table, TableHeader, Length);
519433d6423SLionel Sambuc         break;
520433d6423SLionel Sambuc 
521433d6423SLionel Sambuc     default:
522*29492bb7SDavid van Moolenbroek 
523433d6423SLionel Sambuc         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
524433d6423SLionel Sambuc     }
525433d6423SLionel Sambuc 
526433d6423SLionel Sambuc     /* Install the new table into the local data structures */
527433d6423SLionel Sambuc 
528*29492bb7SDavid van Moolenbroek     ACPI_INFO ((AE_INFO, "Dynamic OEM Table Load:"));
529*29492bb7SDavid van Moolenbroek     (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
530*29492bb7SDavid van Moolenbroek 
531*29492bb7SDavid van Moolenbroek     Status = AcpiTbInstallStandardTable (ACPI_PTR_TO_PHYSADDR (Table),
532*29492bb7SDavid van Moolenbroek                 ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, TRUE, TRUE,
533*29492bb7SDavid van Moolenbroek                 &TableIndex);
534*29492bb7SDavid van Moolenbroek 
535*29492bb7SDavid van Moolenbroek     (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
536433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
537433d6423SLionel Sambuc     {
538433d6423SLionel Sambuc         /* Delete allocated table buffer */
539433d6423SLionel Sambuc 
540*29492bb7SDavid van Moolenbroek         ACPI_FREE (Table);
541*29492bb7SDavid van Moolenbroek         return_ACPI_STATUS (Status);
542*29492bb7SDavid van Moolenbroek     }
543*29492bb7SDavid van Moolenbroek 
544*29492bb7SDavid van Moolenbroek     /*
545*29492bb7SDavid van Moolenbroek      * Note: Now table is "INSTALLED", it must be validated before
546*29492bb7SDavid van Moolenbroek      * loading.
547*29492bb7SDavid van Moolenbroek      */
548*29492bb7SDavid van Moolenbroek     Status = AcpiTbValidateTable (&AcpiGbl_RootTableList.Tables[TableIndex]);
549*29492bb7SDavid van Moolenbroek     if (ACPI_FAILURE (Status))
550*29492bb7SDavid van Moolenbroek     {
551433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
552433d6423SLionel Sambuc     }
553433d6423SLionel Sambuc 
554433d6423SLionel Sambuc     /*
555433d6423SLionel Sambuc      * Add the table to the namespace.
556433d6423SLionel Sambuc      *
557433d6423SLionel Sambuc      * Note: Load the table objects relative to the root of the namespace.
558433d6423SLionel Sambuc      * This appears to go against the ACPI specification, but we do it for
559433d6423SLionel Sambuc      * compatibility with other ACPI implementations.
560433d6423SLionel Sambuc      */
561433d6423SLionel Sambuc     Status = AcpiExAddTable (TableIndex, AcpiGbl_RootNode, &DdbHandle);
562433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
563433d6423SLionel Sambuc     {
564433d6423SLionel Sambuc         /* On error, TablePtr was deallocated above */
565433d6423SLionel Sambuc 
566433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
567433d6423SLionel Sambuc     }
568433d6423SLionel Sambuc 
569433d6423SLionel Sambuc     /* Store the DdbHandle into the Target operand */
570433d6423SLionel Sambuc 
571433d6423SLionel Sambuc     Status = AcpiExStore (DdbHandle, Target, WalkState);
572433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
573433d6423SLionel Sambuc     {
574433d6423SLionel Sambuc         (void) AcpiExUnloadTable (DdbHandle);
575433d6423SLionel Sambuc 
576433d6423SLionel Sambuc         /* TablePtr was deallocated above */
577433d6423SLionel Sambuc 
578433d6423SLionel Sambuc         AcpiUtRemoveReference (DdbHandle);
579433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
580433d6423SLionel Sambuc     }
581433d6423SLionel Sambuc 
582433d6423SLionel Sambuc     /* Remove the reference by added by AcpiExStore above */
583433d6423SLionel Sambuc 
584433d6423SLionel Sambuc     AcpiUtRemoveReference (DdbHandle);
585433d6423SLionel Sambuc 
586433d6423SLionel Sambuc     /* Invoke table handler if present */
587433d6423SLionel Sambuc 
588433d6423SLionel Sambuc     if (AcpiGbl_TableHandler)
589433d6423SLionel Sambuc     {
590*29492bb7SDavid van Moolenbroek         (void) AcpiGbl_TableHandler (ACPI_TABLE_EVENT_LOAD, Table,
591433d6423SLionel Sambuc                     AcpiGbl_TableHandlerContext);
592433d6423SLionel Sambuc     }
593433d6423SLionel Sambuc 
594433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
595433d6423SLionel Sambuc }
596433d6423SLionel Sambuc 
597433d6423SLionel Sambuc 
598433d6423SLionel Sambuc /*******************************************************************************
599433d6423SLionel Sambuc  *
600433d6423SLionel Sambuc  * FUNCTION:    AcpiExUnloadTable
601433d6423SLionel Sambuc  *
602433d6423SLionel Sambuc  * PARAMETERS:  DdbHandle           - Handle to a previously loaded table
603433d6423SLionel Sambuc  *
604433d6423SLionel Sambuc  * RETURN:      Status
605433d6423SLionel Sambuc  *
606433d6423SLionel Sambuc  * DESCRIPTION: Unload an ACPI table
607433d6423SLionel Sambuc  *
608433d6423SLionel Sambuc  ******************************************************************************/
609433d6423SLionel Sambuc 
610433d6423SLionel Sambuc ACPI_STATUS
AcpiExUnloadTable(ACPI_OPERAND_OBJECT * DdbHandle)611433d6423SLionel Sambuc AcpiExUnloadTable (
612433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *DdbHandle)
613433d6423SLionel Sambuc {
614433d6423SLionel Sambuc     ACPI_STATUS             Status = AE_OK;
615433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *TableDesc = DdbHandle;
616433d6423SLionel Sambuc     UINT32                  TableIndex;
617433d6423SLionel Sambuc     ACPI_TABLE_HEADER       *Table;
618433d6423SLionel Sambuc 
619433d6423SLionel Sambuc 
620433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE (ExUnloadTable);
621433d6423SLionel Sambuc 
622433d6423SLionel Sambuc 
623433d6423SLionel Sambuc     /*
624*29492bb7SDavid van Moolenbroek      * Temporarily emit a warning so that the ASL for the machine can be
625*29492bb7SDavid van Moolenbroek      * hopefully obtained. This is to say that the Unload() operator is
626*29492bb7SDavid van Moolenbroek      * extremely rare if not completely unused.
627*29492bb7SDavid van Moolenbroek      */
628*29492bb7SDavid van Moolenbroek     ACPI_WARNING ((AE_INFO,
629*29492bb7SDavid van Moolenbroek         "Received request to unload an ACPI table"));
630*29492bb7SDavid van Moolenbroek 
631*29492bb7SDavid van Moolenbroek     /*
632433d6423SLionel Sambuc      * Validate the handle
633433d6423SLionel Sambuc      * Although the handle is partially validated in AcpiExReconfiguration()
634433d6423SLionel Sambuc      * when it calls AcpiExResolveOperands(), the handle is more completely
635433d6423SLionel Sambuc      * validated here.
636433d6423SLionel Sambuc      *
637433d6423SLionel Sambuc      * Handle must be a valid operand object of type reference. Also, the
638433d6423SLionel Sambuc      * DdbHandle must still be marked valid (table has not been previously
639433d6423SLionel Sambuc      * unloaded)
640433d6423SLionel Sambuc      */
641433d6423SLionel Sambuc     if ((!DdbHandle) ||
642433d6423SLionel Sambuc         (ACPI_GET_DESCRIPTOR_TYPE (DdbHandle) != ACPI_DESC_TYPE_OPERAND) ||
643433d6423SLionel Sambuc         (DdbHandle->Common.Type != ACPI_TYPE_LOCAL_REFERENCE) ||
644433d6423SLionel Sambuc         (!(DdbHandle->Common.Flags & AOPOBJ_DATA_VALID)))
645433d6423SLionel Sambuc     {
646*29492bb7SDavid van Moolenbroek         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
647433d6423SLionel Sambuc     }
648433d6423SLionel Sambuc 
649433d6423SLionel Sambuc     /* Get the table index from the DdbHandle */
650433d6423SLionel Sambuc 
651433d6423SLionel Sambuc     TableIndex = TableDesc->Reference.Value;
652433d6423SLionel Sambuc 
653433d6423SLionel Sambuc     /* Ensure the table is still loaded */
654433d6423SLionel Sambuc 
655433d6423SLionel Sambuc     if (!AcpiTbIsTableLoaded (TableIndex))
656433d6423SLionel Sambuc     {
657433d6423SLionel Sambuc         return_ACPI_STATUS (AE_NOT_EXIST);
658433d6423SLionel Sambuc     }
659433d6423SLionel Sambuc 
660433d6423SLionel Sambuc     /* Invoke table handler if present */
661433d6423SLionel Sambuc 
662433d6423SLionel Sambuc     if (AcpiGbl_TableHandler)
663433d6423SLionel Sambuc     {
664433d6423SLionel Sambuc         Status = AcpiGetTableByIndex (TableIndex, &Table);
665433d6423SLionel Sambuc         if (ACPI_SUCCESS (Status))
666433d6423SLionel Sambuc         {
667433d6423SLionel Sambuc             (void) AcpiGbl_TableHandler (ACPI_TABLE_EVENT_UNLOAD, Table,
668433d6423SLionel Sambuc                         AcpiGbl_TableHandlerContext);
669433d6423SLionel Sambuc         }
670433d6423SLionel Sambuc     }
671433d6423SLionel Sambuc 
672433d6423SLionel Sambuc     /* Delete the portion of the namespace owned by this table */
673433d6423SLionel Sambuc 
674433d6423SLionel Sambuc     Status = AcpiTbDeleteNamespaceByOwner (TableIndex);
675433d6423SLionel Sambuc     if (ACPI_FAILURE (Status))
676433d6423SLionel Sambuc     {
677433d6423SLionel Sambuc         return_ACPI_STATUS (Status);
678433d6423SLionel Sambuc     }
679433d6423SLionel Sambuc 
680433d6423SLionel Sambuc     (void) AcpiTbReleaseOwnerId (TableIndex);
681433d6423SLionel Sambuc     AcpiTbSetTableLoadedFlag (TableIndex, FALSE);
682433d6423SLionel Sambuc 
683433d6423SLionel Sambuc     /*
684433d6423SLionel Sambuc      * Invalidate the handle. We do this because the handle may be stored
685433d6423SLionel Sambuc      * in a named object and may not be actually deleted until much later.
686433d6423SLionel Sambuc      */
687433d6423SLionel Sambuc     DdbHandle->Common.Flags &= ~AOPOBJ_DATA_VALID;
688433d6423SLionel Sambuc     return_ACPI_STATUS (AE_OK);
689433d6423SLionel Sambuc }
690