1 /*******************************************************************************
2  *
3  * Module Name: utmisc - common utility procedures
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acnamesp.h"
47 
48 
49 #define _COMPONENT          ACPI_UTILITIES
50         ACPI_MODULE_NAME    ("utmisc")
51 
52 
53 /*******************************************************************************
54  *
55  * FUNCTION:    AcpiUtIsPciRootBridge
56  *
57  * PARAMETERS:  Id              - The HID/CID in string format
58  *
59  * RETURN:      TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
60  *
61  * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
62  *
63  ******************************************************************************/
64 
65 BOOLEAN
66 AcpiUtIsPciRootBridge (
67     char                    *Id)
68 {
69 
70     /*
71      * Check if this is a PCI root bridge.
72      * ACPI 3.0+: check for a PCI Express root also.
73      */
74     if (!(ACPI_STRCMP (Id,
75             PCI_ROOT_HID_STRING)) ||
76 
77         !(ACPI_STRCMP (Id,
78             PCI_EXPRESS_ROOT_HID_STRING)))
79     {
80         return (TRUE);
81     }
82 
83     return (FALSE);
84 }
85 
86 
87 /*******************************************************************************
88  *
89  * FUNCTION:    AcpiUtIsAmlTable
90  *
91  * PARAMETERS:  Table               - An ACPI table
92  *
93  * RETURN:      TRUE if table contains executable AML; FALSE otherwise
94  *
95  * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
96  *              Currently, these are DSDT,SSDT,PSDT. All other table types are
97  *              data tables that do not contain AML code.
98  *
99  ******************************************************************************/
100 
101 BOOLEAN
102 AcpiUtIsAmlTable (
103     ACPI_TABLE_HEADER       *Table)
104 {
105 
106     /* These are the only tables that contain executable AML */
107 
108     if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) ||
109         ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_PSDT) ||
110         ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
111     {
112         return (TRUE);
113     }
114 
115     return (FALSE);
116 }
117 
118 
119 /*******************************************************************************
120  *
121  * FUNCTION:    AcpiUtDwordByteSwap
122  *
123  * PARAMETERS:  Value           - Value to be converted
124  *
125  * RETURN:      UINT32 integer with bytes swapped
126  *
127  * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
128  *
129  ******************************************************************************/
130 
131 UINT32
132 AcpiUtDwordByteSwap (
133     UINT32                  Value)
134 {
135     union
136     {
137         UINT32              Value;
138         UINT8               Bytes[4];
139     } Out;
140     union
141     {
142         UINT32              Value;
143         UINT8               Bytes[4];
144     } In;
145 
146 
147     ACPI_FUNCTION_ENTRY ();
148 
149 
150     In.Value = Value;
151 
152     Out.Bytes[0] = In.Bytes[3];
153     Out.Bytes[1] = In.Bytes[2];
154     Out.Bytes[2] = In.Bytes[1];
155     Out.Bytes[3] = In.Bytes[0];
156 
157     return (Out.Value);
158 }
159 
160 
161 /*******************************************************************************
162  *
163  * FUNCTION:    AcpiUtSetIntegerWidth
164  *
165  * PARAMETERS:  Revision            From DSDT header
166  *
167  * RETURN:      None
168  *
169  * DESCRIPTION: Set the global integer bit width based upon the revision
170  *              of the DSDT. For Revision 1 and 0, Integers are 32 bits.
171  *              For Revision 2 and above, Integers are 64 bits. Yes, this
172  *              makes a difference.
173  *
174  ******************************************************************************/
175 
176 void
177 AcpiUtSetIntegerWidth (
178     UINT8                   Revision)
179 {
180 
181     if (Revision < 2)
182     {
183         /* 32-bit case */
184 
185         AcpiGbl_IntegerBitWidth    = 32;
186         AcpiGbl_IntegerNybbleWidth = 8;
187         AcpiGbl_IntegerByteWidth   = 4;
188     }
189     else
190     {
191         /* 64-bit case (ACPI 2.0+) */
192 
193         AcpiGbl_IntegerBitWidth    = 64;
194         AcpiGbl_IntegerNybbleWidth = 16;
195         AcpiGbl_IntegerByteWidth   = 8;
196     }
197 }
198 
199 
200 /*******************************************************************************
201  *
202  * FUNCTION:    AcpiUtCreateUpdateStateAndPush
203  *
204  * PARAMETERS:  Object          - Object to be added to the new state
205  *              Action          - Increment/Decrement
206  *              StateList       - List the state will be added to
207  *
208  * RETURN:      Status
209  *
210  * DESCRIPTION: Create a new state and push it
211  *
212  ******************************************************************************/
213 
214 ACPI_STATUS
215 AcpiUtCreateUpdateStateAndPush (
216     ACPI_OPERAND_OBJECT     *Object,
217     UINT16                  Action,
218     ACPI_GENERIC_STATE      **StateList)
219 {
220     ACPI_GENERIC_STATE       *State;
221 
222 
223     ACPI_FUNCTION_ENTRY ();
224 
225 
226     /* Ignore null objects; these are expected */
227 
228     if (!Object)
229     {
230         return (AE_OK);
231     }
232 
233     State = AcpiUtCreateUpdateState (Object, Action);
234     if (!State)
235     {
236         return (AE_NO_MEMORY);
237     }
238 
239     AcpiUtPushGenericState (StateList, State);
240     return (AE_OK);
241 }
242 
243 
244 /*******************************************************************************
245  *
246  * FUNCTION:    AcpiUtWalkPackageTree
247  *
248  * PARAMETERS:  SourceObject        - The package to walk
249  *              TargetObject        - Target object (if package is being copied)
250  *              WalkCallback        - Called once for each package element
251  *              Context             - Passed to the callback function
252  *
253  * RETURN:      Status
254  *
255  * DESCRIPTION: Walk through a package
256  *
257  ******************************************************************************/
258 
259 ACPI_STATUS
260 AcpiUtWalkPackageTree (
261     ACPI_OPERAND_OBJECT     *SourceObject,
262     void                    *TargetObject,
263     ACPI_PKG_CALLBACK       WalkCallback,
264     void                    *Context)
265 {
266     ACPI_STATUS             Status = AE_OK;
267     ACPI_GENERIC_STATE      *StateList = NULL;
268     ACPI_GENERIC_STATE      *State;
269     UINT32                  ThisIndex;
270     ACPI_OPERAND_OBJECT     *ThisSourceObj;
271 
272 
273     ACPI_FUNCTION_TRACE (UtWalkPackageTree);
274 
275 
276     State = AcpiUtCreatePkgState (SourceObject, TargetObject, 0);
277     if (!State)
278     {
279         return_ACPI_STATUS (AE_NO_MEMORY);
280     }
281 
282     while (State)
283     {
284         /* Get one element of the package */
285 
286         ThisIndex     = State->Pkg.Index;
287         ThisSourceObj = (ACPI_OPERAND_OBJECT *)
288                         State->Pkg.SourceObject->Package.Elements[ThisIndex];
289 
290         /*
291          * Check for:
292          * 1) An uninitialized package element. It is completely
293          *    legal to declare a package and leave it uninitialized
294          * 2) Not an internal object - can be a namespace node instead
295          * 3) Any type other than a package. Packages are handled in else
296          *    case below.
297          */
298         if ((!ThisSourceObj) ||
299             (ACPI_GET_DESCRIPTOR_TYPE (ThisSourceObj) != ACPI_DESC_TYPE_OPERAND) ||
300             (ThisSourceObj->Common.Type != ACPI_TYPE_PACKAGE))
301         {
302             Status = WalkCallback (ACPI_COPY_TYPE_SIMPLE, ThisSourceObj,
303                                     State, Context);
304             if (ACPI_FAILURE (Status))
305             {
306                 return_ACPI_STATUS (Status);
307             }
308 
309             State->Pkg.Index++;
310             while (State->Pkg.Index >= State->Pkg.SourceObject->Package.Count)
311             {
312                 /*
313                  * We've handled all of the objects at this level,  This means
314                  * that we have just completed a package. That package may
315                  * have contained one or more packages itself.
316                  *
317                  * Delete this state and pop the previous state (package).
318                  */
319                 AcpiUtDeleteGenericState (State);
320                 State = AcpiUtPopGenericState (&StateList);
321 
322                 /* Finished when there are no more states */
323 
324                 if (!State)
325                 {
326                     /*
327                      * We have handled all of the objects in the top level
328                      * package just add the length of the package objects
329                      * and exit
330                      */
331                     return_ACPI_STATUS (AE_OK);
332                 }
333 
334                 /*
335                  * Go back up a level and move the index past the just
336                  * completed package object.
337                  */
338                 State->Pkg.Index++;
339             }
340         }
341         else
342         {
343             /* This is a subobject of type package */
344 
345             Status = WalkCallback (ACPI_COPY_TYPE_PACKAGE, ThisSourceObj,
346                                         State, Context);
347             if (ACPI_FAILURE (Status))
348             {
349                 return_ACPI_STATUS (Status);
350             }
351 
352             /*
353              * Push the current state and create a new one
354              * The callback above returned a new target package object.
355              */
356             AcpiUtPushGenericState (&StateList, State);
357             State = AcpiUtCreatePkgState (ThisSourceObj,
358                                             State->Pkg.ThisTargetObj, 0);
359             if (!State)
360             {
361                 /* Free any stacked Update State objects */
362 
363                 while (StateList)
364                 {
365                     State = AcpiUtPopGenericState (&StateList);
366                     AcpiUtDeleteGenericState (State);
367                 }
368                 return_ACPI_STATUS (AE_NO_MEMORY);
369             }
370         }
371     }
372 
373     /* We should never get here */
374 
375     return_ACPI_STATUS (AE_AML_INTERNAL);
376 }
377 
378 
379 #ifdef ACPI_DEBUG_OUTPUT
380 /*******************************************************************************
381  *
382  * FUNCTION:    AcpiUtDisplayInitPathname
383  *
384  * PARAMETERS:  Type                - Object type of the node
385  *              ObjHandle           - Handle whose pathname will be displayed
386  *              Path                - Additional path string to be appended.
387  *                                      (NULL if no extra path)
388  *
389  * RETURN:      ACPI_STATUS
390  *
391  * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
392  *
393  ******************************************************************************/
394 
395 void
396 AcpiUtDisplayInitPathname (
397     UINT8                   Type,
398     ACPI_NAMESPACE_NODE     *ObjHandle,
399     char                    *Path)
400 {
401     ACPI_STATUS             Status;
402     ACPI_BUFFER             Buffer;
403 
404 
405     ACPI_FUNCTION_ENTRY ();
406 
407 
408     /* Only print the path if the appropriate debug level is enabled */
409 
410     if (!(AcpiDbgLevel & ACPI_LV_INIT_NAMES))
411     {
412         return;
413     }
414 
415     /* Get the full pathname to the node */
416 
417     Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
418     Status = AcpiNsHandleToPathname (ObjHandle, &Buffer);
419     if (ACPI_FAILURE (Status))
420     {
421         return;
422     }
423 
424     /* Print what we're doing */
425 
426     switch (Type)
427     {
428     case ACPI_TYPE_METHOD:
429 
430         AcpiOsPrintf ("Executing    ");
431         break;
432 
433     default:
434 
435         AcpiOsPrintf ("Initializing ");
436         break;
437     }
438 
439     /* Print the object type and pathname */
440 
441     AcpiOsPrintf ("%-12s  %s",
442         AcpiUtGetTypeName (Type), (char *) Buffer.Pointer);
443 
444     /* Extra path is used to append names like _STA, _INI, etc. */
445 
446     if (Path)
447     {
448         AcpiOsPrintf (".%s", Path);
449     }
450     AcpiOsPrintf ("\n");
451 
452     ACPI_FREE (Buffer.Pointer);
453 }
454 #endif
455