Sync ACPICA with Intel's version 20150619.
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / asllookup.c
1 /******************************************************************************
2  *
3  * Module Name: asllookup- Namespace lookup functions
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2015, 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 "aslcompiler.h"
45 #include "aslcompiler.y.h"
46 #include "acparser.h"
47 #include "amlcode.h"
48 #include "acnamesp.h"
49 #include "acdispat.h"
50
51
52 #define _COMPONENT          ACPI_COMPILER
53         ACPI_MODULE_NAME    ("asllookup")
54
55 /* Local prototypes */
56
57 static ACPI_STATUS
58 LkIsObjectUsed (
59     ACPI_HANDLE             ObjHandle,
60     UINT32                  Level,
61     void                    *Context,
62     void                    **ReturnValue);
63
64 static ACPI_PARSE_OBJECT *
65 LkGetNameOp (
66     ACPI_PARSE_OBJECT       *Op);
67
68
69 /*******************************************************************************
70  *
71  * FUNCTION:    LkFindUnreferencedObjects
72  *
73  * PARAMETERS:  None
74  *
75  * RETURN:      None
76  *
77  * DESCRIPTION: Namespace walk to find objects that are not referenced in any
78  *              way. Must be called after the namespace has been cross
79  *              referenced.
80  *
81  ******************************************************************************/
82
83 void
84 LkFindUnreferencedObjects (
85     void)
86 {
87
88     /* Walk entire namespace from the supplied root */
89
90     (void) AcpiNsWalkNamespace (ACPI_TYPE_ANY, ACPI_ROOT_OBJECT,
91                 ACPI_UINT32_MAX, FALSE, LkIsObjectUsed, NULL,
92                 NULL, NULL);
93 }
94
95
96 /*******************************************************************************
97  *
98  * FUNCTION:    LkIsObjectUsed
99  *
100  * PARAMETERS:  ACPI_WALK_CALLBACK
101  *
102  * RETURN:      Status
103  *
104  * DESCRIPTION: Check for an unreferenced namespace object and emit a warning.
105  *              We have to be careful, because some types and names are
106  *              typically or always unreferenced, we don't want to issue
107  *              excessive warnings. Note: Names that are declared within a
108  *              control method are temporary, so we always issue a remark
109  *              if they are not referenced.
110  *
111  ******************************************************************************/
112 typedef struct asl_method_local
113 {
114     UINT32                  Flags;
115     ACPI_PARSE_OBJECT       *Op;
116
117 } ASL_METHOD_LOCAL;
118
119
120 static ACPI_STATUS
121 LkIsObjectUsed (
122     ACPI_HANDLE             ObjHandle,
123     UINT32                  Level,
124     void                    *Context,
125     void                    **ReturnValue)
126 {
127     ACPI_NAMESPACE_NODE     *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, ObjHandle);
128     ACPI_NAMESPACE_NODE     *Next;
129     ASL_METHOD_LOCAL        *MethodLocals;
130     UINT32                  i;
131
132
133     /* Referenced flag is set during the namespace xref */
134
135     if (Node->Flags & ANOBJ_IS_REFERENCED)
136     {
137         return (AE_OK);
138     }
139
140     if (!Node->Op)
141     {
142         return (AE_OK);
143     }
144
145     /* These types are typically never directly referenced, ignore them */
146
147     switch (Node->Type)
148     {
149     case ACPI_TYPE_DEVICE:
150     case ACPI_TYPE_PROCESSOR:
151     case ACPI_TYPE_POWER:
152     case ACPI_TYPE_THERMAL:
153     case ACPI_TYPE_LOCAL_RESOURCE:
154
155         return (AE_OK);
156
157     case ACPI_TYPE_METHOD:
158
159         /* Check for Locals that are set but never used */
160
161         MethodLocals = (ASL_METHOD_LOCAL *) Node->Object;
162         for (i = 0; i < ACPI_METHOD_NUM_LOCALS; i++)
163         {
164             if (MethodLocals[i].Flags & ANOBJ_IS_REFERENCED)
165             {
166                 sprintf (MsgBuffer, "Local%u",
167                     i);
168
169                 AslError (ASL_REMARK, ASL_MSG_LOCAL_NOT_USED,
170                     MethodLocals[i].Op, MsgBuffer);
171             }
172         }
173         break;
174
175     default:
176
177         break;
178     }
179
180     /* Determine if the name is within a control method */
181
182     Next = Node->Parent;
183     while (Next)
184     {
185         if (Next->Type == ACPI_TYPE_METHOD)
186         {
187             /*
188              * Name is within a method, therefore it is temporary.
189              * Issue a remark even if it is a reserved name (starts
190              * with an underscore).
191              */
192             sprintf (MsgBuffer, "Name is within method [%4.4s]",
193                 Next->Name.Ascii);
194             AslError (ASL_REMARK, ASL_MSG_NOT_REFERENCED,
195                 LkGetNameOp (Node->Op), MsgBuffer);
196             return (AE_OK);
197         }
198
199         Next = Next->Parent;
200     }
201
202     /* The name is not within a control method */
203
204     /*
205      * Ignore names that start with an underscore. These are the reserved
206      * ACPI names and are typically not referenced since they are meant
207      * to be called by the host OS.
208      */
209     if (Node->Name.Ascii[0] == '_')
210     {
211         return (AE_OK);
212     }
213
214     /*
215      * What remains is an unresolved user name that is not within a method.
216      * However, the object could be referenced via another table, so issue
217      * the warning at level 2.
218      */
219     AslError (ASL_WARNING2, ASL_MSG_NOT_REFERENCED,
220         LkGetNameOp (Node->Op), NULL);
221     return (AE_OK);
222 }
223
224
225 /*******************************************************************************
226  *
227  * FUNCTION:    LkGetNameOp
228  *
229  * PARAMETERS:  Op              - Current Op
230  *
231  * RETURN:      NameOp associated with the input op
232  *
233  * DESCRIPTION: Find the name declaration op associated with the operator
234  *
235  ******************************************************************************/
236
237 static ACPI_PARSE_OBJECT *
238 LkGetNameOp (
239     ACPI_PARSE_OBJECT       *Op)
240 {
241     const ACPI_OPCODE_INFO  *OpInfo;
242     ACPI_PARSE_OBJECT       *NameOp = Op;
243
244
245     OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
246
247
248     /* Get the NamePath from the appropriate place */
249
250     if (OpInfo->Flags & AML_NAMED)
251     {
252         /* For nearly all NAMED operators, the name reference is the first child */
253
254         NameOp = Op->Asl.Child;
255         if (Op->Asl.AmlOpcode == AML_ALIAS_OP)
256         {
257             /*
258              * ALIAS is the only oddball opcode, the name declaration
259              * (alias name) is the second operand
260              */
261             NameOp = Op->Asl.Child->Asl.Next;
262         }
263     }
264     else if (OpInfo->Flags & AML_CREATE)
265     {
266         /* Name must appear as the last parameter */
267
268         NameOp = Op->Asl.Child;
269         while (!(NameOp->Asl.CompileFlags & NODE_IS_NAME_DECLARATION))
270         {
271             NameOp = NameOp->Asl.Next;
272         }
273     }
274
275     return (NameOp);
276 }