9729cd61e17ad91993be2ec109dd7c3ee9e2a79c
[dragonfly.git] / sys / contrib / dev / acpica / source / components / namespace / nsaccess.c
1 /*******************************************************************************
2  *
3  * Module Name: nsaccess - Top-level functions for accessing ACPI namespace
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 "acpi.h"
45 #include "accommon.h"
46 #include "amlcode.h"
47 #include "acnamesp.h"
48 #include "acdispat.h"
49
50
51 #define _COMPONENT          ACPI_NAMESPACE
52         ACPI_MODULE_NAME    ("nsaccess")
53
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    AcpiNsRootInitialize
58  *
59  * PARAMETERS:  None
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Allocate and initialize the default root named objects
64  *
65  * MUTEX:       Locks namespace for entire execution
66  *
67  ******************************************************************************/
68
69 ACPI_STATUS
70 AcpiNsRootInitialize (
71     void)
72 {
73     ACPI_STATUS                 Status;
74     const ACPI_PREDEFINED_NAMES *InitVal = NULL;
75     ACPI_NAMESPACE_NODE         *NewNode;
76     ACPI_OPERAND_OBJECT         *ObjDesc;
77     ACPI_STRING                 Val = NULL;
78
79
80     ACPI_FUNCTION_TRACE (NsRootInitialize);
81
82
83     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
84     if (ACPI_FAILURE (Status))
85     {
86         return_ACPI_STATUS (Status);
87     }
88
89     /*
90      * The global root ptr is initially NULL, so a non-NULL value indicates
91      * that AcpiNsRootInitialize() has already been called; just return.
92      */
93     if (AcpiGbl_RootNode)
94     {
95         Status = AE_OK;
96         goto UnlockAndExit;
97     }
98
99     /*
100      * Tell the rest of the subsystem that the root is initialized
101      * (This is OK because the namespace is locked)
102      */
103     AcpiGbl_RootNode = &AcpiGbl_RootNodeStruct;
104
105     /* Enter the pre-defined names in the name table */
106
107     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
108         "Entering predefined entries into namespace\n"));
109
110     for (InitVal = AcpiGbl_PreDefinedNames; InitVal->Name; InitVal++)
111     {
112         /* _OSI is optional for now, will be permanent later */
113
114         if (!ACPI_STRCMP (InitVal->Name, "_OSI") && !AcpiGbl_CreateOsiMethod)
115         {
116             continue;
117         }
118
119         Status = AcpiNsLookup (NULL, InitVal->Name, InitVal->Type,
120                         ACPI_IMODE_LOAD_PASS2, ACPI_NS_NO_UPSEARCH,
121                         NULL, &NewNode);
122         if (ACPI_FAILURE (Status))
123         {
124             ACPI_EXCEPTION ((AE_INFO, Status,
125                 "Could not create predefined name %s",
126                 InitVal->Name));
127             continue;
128         }
129
130         /*
131          * Name entered successfully. If entry in PreDefinedNames[] specifies
132          * an initial value, create the initial value.
133          */
134         if (InitVal->Val)
135         {
136             Status = AcpiOsPredefinedOverride (InitVal, &Val);
137             if (ACPI_FAILURE (Status))
138             {
139                 ACPI_ERROR ((AE_INFO,
140                     "Could not override predefined %s",
141                     InitVal->Name));
142             }
143
144             if (!Val)
145             {
146                 Val = InitVal->Val;
147             }
148
149             /*
150              * Entry requests an initial value, allocate a
151              * descriptor for it.
152              */
153             ObjDesc = AcpiUtCreateInternalObject (InitVal->Type);
154             if (!ObjDesc)
155             {
156                 Status = AE_NO_MEMORY;
157                 goto UnlockAndExit;
158             }
159
160             /*
161              * Convert value string from table entry to
162              * internal representation. Only types actually
163              * used for initial values are implemented here.
164              */
165             switch (InitVal->Type)
166             {
167             case ACPI_TYPE_METHOD:
168
169                 ObjDesc->Method.ParamCount = (UINT8) ACPI_TO_INTEGER (Val);
170                 ObjDesc->Common.Flags |= AOPOBJ_DATA_VALID;
171
172 #if defined (ACPI_ASL_COMPILER)
173
174                 /* Save the parameter count for the iASL compiler */
175
176                 NewNode->Value = ObjDesc->Method.ParamCount;
177 #else
178                 /* Mark this as a very SPECIAL method */
179
180                 ObjDesc->Method.InfoFlags = ACPI_METHOD_INTERNAL_ONLY;
181                 ObjDesc->Method.Dispatch.Implementation = AcpiUtOsiImplementation;
182 #endif
183                 break;
184
185             case ACPI_TYPE_INTEGER:
186
187                 ObjDesc->Integer.Value = ACPI_TO_INTEGER (Val);
188                 break;
189
190             case ACPI_TYPE_STRING:
191
192                 /* Build an object around the static string */
193
194                 ObjDesc->String.Length = (UINT32) ACPI_STRLEN (Val);
195                 ObjDesc->String.Pointer = Val;
196                 ObjDesc->Common.Flags |= AOPOBJ_STATIC_POINTER;
197                 break;
198
199             case ACPI_TYPE_MUTEX:
200
201                 ObjDesc->Mutex.Node = NewNode;
202                 ObjDesc->Mutex.SyncLevel = (UINT8) (ACPI_TO_INTEGER (Val) - 1);
203
204                 /* Create a mutex */
205
206                 Status = AcpiOsCreateMutex (&ObjDesc->Mutex.OsMutex);
207                 if (ACPI_FAILURE (Status))
208                 {
209                     AcpiUtRemoveReference (ObjDesc);
210                     goto UnlockAndExit;
211                 }
212
213                 /* Special case for ACPI Global Lock */
214
215                 if (ACPI_STRCMP (InitVal->Name, "_GL_") == 0)
216                 {
217                     AcpiGbl_GlobalLockMutex = ObjDesc;
218
219                     /* Create additional counting semaphore for global lock */
220
221                     Status = AcpiOsCreateSemaphore (
222                                 1, 0, &AcpiGbl_GlobalLockSemaphore);
223                     if (ACPI_FAILURE (Status))
224                     {
225                         AcpiUtRemoveReference (ObjDesc);
226                         goto UnlockAndExit;
227                     }
228                 }
229                 break;
230
231             default:
232
233                 ACPI_ERROR ((AE_INFO, "Unsupported initial type value 0x%X",
234                     InitVal->Type));
235                 AcpiUtRemoveReference (ObjDesc);
236                 ObjDesc = NULL;
237                 continue;
238             }
239
240             /* Store pointer to value descriptor in the Node */
241
242             Status = AcpiNsAttachObject (NewNode, ObjDesc,
243                         ObjDesc->Common.Type);
244
245             /* Remove local reference to the object */
246
247             AcpiUtRemoveReference (ObjDesc);
248         }
249     }
250
251
252 UnlockAndExit:
253     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
254
255     /* Save a handle to "_GPE", it is always present */
256
257     if (ACPI_SUCCESS (Status))
258     {
259         Status = AcpiNsGetNode (NULL, "\\_GPE", ACPI_NS_NO_UPSEARCH,
260                     &AcpiGbl_FadtGpeDevice);
261     }
262
263     return_ACPI_STATUS (Status);
264 }
265
266
267 /*******************************************************************************
268  *
269  * FUNCTION:    AcpiNsLookup
270  *
271  * PARAMETERS:  ScopeInfo       - Current scope info block
272  *              Pathname        - Search pathname, in internal format
273  *                                (as represented in the AML stream)
274  *              Type            - Type associated with name
275  *              InterpreterMode - IMODE_LOAD_PASS2 => add name if not found
276  *              Flags           - Flags describing the search restrictions
277  *              WalkState       - Current state of the walk
278  *              ReturnNode      - Where the Node is placed (if found
279  *                                or created successfully)
280  *
281  * RETURN:      Status
282  *
283  * DESCRIPTION: Find or enter the passed name in the name space.
284  *              Log an error if name not found in Exec mode.
285  *
286  * MUTEX:       Assumes namespace is locked.
287  *
288  ******************************************************************************/
289
290 ACPI_STATUS
291 AcpiNsLookup (
292     ACPI_GENERIC_STATE      *ScopeInfo,
293     char                    *Pathname,
294     ACPI_OBJECT_TYPE        Type,
295     ACPI_INTERPRETER_MODE   InterpreterMode,
296     UINT32                  Flags,
297     ACPI_WALK_STATE         *WalkState,
298     ACPI_NAMESPACE_NODE     **ReturnNode)
299 {
300     ACPI_STATUS             Status;
301     char                    *Path = Pathname;
302     ACPI_NAMESPACE_NODE     *PrefixNode;
303     ACPI_NAMESPACE_NODE     *CurrentNode = NULL;
304     ACPI_NAMESPACE_NODE     *ThisNode = NULL;
305     UINT32                  NumSegments;
306     UINT32                  NumCarats;
307     ACPI_NAME               SimpleName;
308     ACPI_OBJECT_TYPE        TypeToCheckFor;
309     ACPI_OBJECT_TYPE        ThisSearchType;
310     UINT32                  SearchParentFlag = ACPI_NS_SEARCH_PARENT;
311     UINT32                  LocalFlags;
312
313
314     ACPI_FUNCTION_TRACE (NsLookup);
315
316
317     if (!ReturnNode)
318     {
319         return_ACPI_STATUS (AE_BAD_PARAMETER);
320     }
321
322     LocalFlags = Flags & ~(ACPI_NS_ERROR_IF_FOUND | ACPI_NS_SEARCH_PARENT);
323     *ReturnNode = ACPI_ENTRY_NOT_FOUND;
324     AcpiGbl_NsLookupCount++;
325
326     if (!AcpiGbl_RootNode)
327     {
328         return_ACPI_STATUS (AE_NO_NAMESPACE);
329     }
330
331     /* Get the prefix scope. A null scope means use the root scope */
332
333     if ((!ScopeInfo) ||
334         (!ScopeInfo->Scope.Node))
335     {
336         ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
337             "Null scope prefix, using root node (%p)\n",
338             AcpiGbl_RootNode));
339
340         PrefixNode = AcpiGbl_RootNode;
341     }
342     else
343     {
344         PrefixNode = ScopeInfo->Scope.Node;
345         if (ACPI_GET_DESCRIPTOR_TYPE (PrefixNode) != ACPI_DESC_TYPE_NAMED)
346         {
347             ACPI_ERROR ((AE_INFO, "%p is not a namespace node [%s]",
348                 PrefixNode, AcpiUtGetDescriptorName (PrefixNode)));
349             return_ACPI_STATUS (AE_AML_INTERNAL);
350         }
351
352         if (!(Flags & ACPI_NS_PREFIX_IS_SCOPE))
353         {
354             /*
355              * This node might not be a actual "scope" node (such as a
356              * Device/Method, etc.)  It could be a Package or other object
357              * node. Backup up the tree to find the containing scope node.
358              */
359             while (!AcpiNsOpensScope (PrefixNode->Type) &&
360                     PrefixNode->Type != ACPI_TYPE_ANY)
361             {
362                 PrefixNode = PrefixNode->Parent;
363             }
364         }
365     }
366
367     /* Save type. TBD: may be no longer necessary */
368
369     TypeToCheckFor = Type;
370
371     /*
372      * Begin examination of the actual pathname
373      */
374     if (!Pathname)
375     {
376         /* A Null NamePath is allowed and refers to the root */
377
378         NumSegments = 0;
379         ThisNode = AcpiGbl_RootNode;
380         Path = "";
381
382         ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
383             "Null Pathname (Zero segments), Flags=%X\n", Flags));
384     }
385     else
386     {
387         /*
388          * Name pointer is valid (and must be in internal name format)
389          *
390          * Check for scope prefixes:
391          *
392          * As represented in the AML stream, a namepath consists of an
393          * optional scope prefix followed by a name segment part.
394          *
395          * If present, the scope prefix is either a Root Prefix (in
396          * which case the name is fully qualified), or one or more
397          * Parent Prefixes (in which case the name's scope is relative
398          * to the current scope).
399          */
400         if (*Path == (UINT8) AML_ROOT_PREFIX)
401         {
402             /* Pathname is fully qualified, start from the root */
403
404             ThisNode = AcpiGbl_RootNode;
405             SearchParentFlag = ACPI_NS_NO_UPSEARCH;
406
407             /* Point to name segment part */
408
409             Path++;
410
411             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
412                 "Path is absolute from root [%p]\n", ThisNode));
413         }
414         else
415         {
416             /* Pathname is relative to current scope, start there */
417
418             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
419                 "Searching relative to prefix scope [%4.4s] (%p)\n",
420                 AcpiUtGetNodeName (PrefixNode), PrefixNode));
421
422             /*
423              * Handle multiple Parent Prefixes (carat) by just getting
424              * the parent node for each prefix instance.
425              */
426             ThisNode = PrefixNode;
427             NumCarats = 0;
428             while (*Path == (UINT8) AML_PARENT_PREFIX)
429             {
430                 /* Name is fully qualified, no search rules apply */
431
432                 SearchParentFlag = ACPI_NS_NO_UPSEARCH;
433
434                 /*
435                  * Point past this prefix to the name segment
436                  * part or the next Parent Prefix
437                  */
438                 Path++;
439
440                 /* Backup to the parent node */
441
442                 NumCarats++;
443                 ThisNode = ThisNode->Parent;
444                 if (!ThisNode)
445                 {
446                     /* Current scope has no parent scope */
447
448                     ACPI_ERROR ((AE_INFO,
449                         "%s: Path has too many parent prefixes (^) "
450                         "- reached beyond root node", Pathname));
451                     return_ACPI_STATUS (AE_NOT_FOUND);
452                 }
453             }
454
455             if (SearchParentFlag == ACPI_NS_NO_UPSEARCH)
456             {
457                 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
458                     "Search scope is [%4.4s], path has %u carat(s)\n",
459                     AcpiUtGetNodeName (ThisNode), NumCarats));
460             }
461         }
462
463         /*
464          * Determine the number of ACPI name segments in this pathname.
465          *
466          * The segment part consists of either:
467          *  - A Null name segment (0)
468          *  - A DualNamePrefix followed by two 4-byte name segments
469          *  - A MultiNamePrefix followed by a byte indicating the
470          *      number of segments and the segments themselves.
471          *  - A single 4-byte name segment
472          *
473          * Examine the name prefix opcode, if any, to determine the number of
474          * segments.
475          */
476         switch (*Path)
477         {
478         case 0:
479             /*
480              * Null name after a root or parent prefixes. We already
481              * have the correct target node and there are no name segments.
482              */
483             NumSegments  = 0;
484             Type = ThisNode->Type;
485
486             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
487                 "Prefix-only Pathname (Zero name segments), Flags=%X\n",
488                 Flags));
489             break;
490
491         case AML_DUAL_NAME_PREFIX:
492
493             /* More than one NameSeg, search rules do not apply */
494
495             SearchParentFlag = ACPI_NS_NO_UPSEARCH;
496
497             /* Two segments, point to first name segment */
498
499             NumSegments = 2;
500             Path++;
501
502             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
503                 "Dual Pathname (2 segments, Flags=%X)\n", Flags));
504             break;
505
506         case AML_MULTI_NAME_PREFIX_OP:
507
508             /* More than one NameSeg, search rules do not apply */
509
510             SearchParentFlag = ACPI_NS_NO_UPSEARCH;
511
512             /* Extract segment count, point to first name segment */
513
514             Path++;
515             NumSegments = (UINT32) (UINT8) *Path;
516             Path++;
517
518             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
519                 "Multi Pathname (%u Segments, Flags=%X)\n",
520                 NumSegments, Flags));
521             break;
522
523         default:
524             /*
525              * Not a Null name, no Dual or Multi prefix, hence there is
526              * only one name segment and Pathname is already pointing to it.
527              */
528             NumSegments = 1;
529
530             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
531                 "Simple Pathname (1 segment, Flags=%X)\n", Flags));
532             break;
533         }
534
535         ACPI_DEBUG_EXEC (AcpiNsPrintPathname (NumSegments, Path));
536     }
537
538
539     /*
540      * Search namespace for each segment of the name. Loop through and
541      * verify (or add to the namespace) each name segment.
542      *
543      * The object type is significant only at the last name
544      * segment. (We don't care about the types along the path, only
545      * the type of the final target object.)
546      */
547     ThisSearchType = ACPI_TYPE_ANY;
548     CurrentNode = ThisNode;
549     while (NumSegments && CurrentNode)
550     {
551         NumSegments--;
552         if (!NumSegments)
553         {
554             /* This is the last segment, enable typechecking */
555
556             ThisSearchType = Type;
557
558             /*
559              * Only allow automatic parent search (search rules) if the caller
560              * requested it AND we have a single, non-fully-qualified NameSeg
561              */
562             if ((SearchParentFlag != ACPI_NS_NO_UPSEARCH) &&
563                 (Flags & ACPI_NS_SEARCH_PARENT))
564             {
565                 LocalFlags |= ACPI_NS_SEARCH_PARENT;
566             }
567
568             /* Set error flag according to caller */
569
570             if (Flags & ACPI_NS_ERROR_IF_FOUND)
571             {
572                 LocalFlags |= ACPI_NS_ERROR_IF_FOUND;
573             }
574         }
575
576         /* Extract one ACPI name from the front of the pathname */
577
578         ACPI_MOVE_32_TO_32 (&SimpleName, Path);
579
580         /* Try to find the single (4 character) ACPI name */
581
582         Status = AcpiNsSearchAndEnter (SimpleName, WalkState, CurrentNode,
583                     InterpreterMode, ThisSearchType, LocalFlags, &ThisNode);
584         if (ACPI_FAILURE (Status))
585         {
586             if (Status == AE_NOT_FOUND)
587             {
588                 /* Name not found in ACPI namespace */
589
590                 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
591                     "Name [%4.4s] not found in scope [%4.4s] %p\n",
592                     (char *) &SimpleName, (char *) &CurrentNode->Name,
593                     CurrentNode));
594             }
595
596             *ReturnNode = ThisNode;
597             return_ACPI_STATUS (Status);
598         }
599
600         /* More segments to follow? */
601
602         if (NumSegments > 0)
603         {
604             /*
605              * If we have an alias to an object that opens a scope (such as a
606              * device or processor), we need to dereference the alias here so
607              * that we can access any children of the original node (via the
608              * remaining segments).
609              */
610             if (ThisNode->Type == ACPI_TYPE_LOCAL_ALIAS)
611             {
612                 if (!ThisNode->Object)
613                 {
614                     return_ACPI_STATUS (AE_NOT_EXIST);
615                 }
616
617                 if (AcpiNsOpensScope (((ACPI_NAMESPACE_NODE *)
618                         ThisNode->Object)->Type))
619                 {
620                     ThisNode = (ACPI_NAMESPACE_NODE *) ThisNode->Object;
621                 }
622             }
623         }
624
625         /* Special handling for the last segment (NumSegments == 0) */
626
627         else
628         {
629             /*
630              * Sanity typecheck of the target object:
631              *
632              * If 1) This is the last segment (NumSegments == 0)
633              *    2) And we are looking for a specific type
634              *       (Not checking for TYPE_ANY)
635              *    3) Which is not an alias
636              *    4) Which is not a local type (TYPE_SCOPE)
637              *    5) And the type of target object is known (not TYPE_ANY)
638              *    6) And target object does not match what we are looking for
639              *
640              * Then we have a type mismatch. Just warn and ignore it.
641              */
642             if ((TypeToCheckFor != ACPI_TYPE_ANY)                   &&
643                 (TypeToCheckFor != ACPI_TYPE_LOCAL_ALIAS)           &&
644                 (TypeToCheckFor != ACPI_TYPE_LOCAL_METHOD_ALIAS)    &&
645                 (TypeToCheckFor != ACPI_TYPE_LOCAL_SCOPE)           &&
646                 (ThisNode->Type != ACPI_TYPE_ANY)                   &&
647                 (ThisNode->Type != TypeToCheckFor))
648             {
649                 /* Complain about a type mismatch */
650
651                 ACPI_WARNING ((AE_INFO,
652                     "NsLookup: Type mismatch on %4.4s (%s), searching for (%s)",
653                     ACPI_CAST_PTR (char, &SimpleName),
654                     AcpiUtGetTypeName (ThisNode->Type),
655                     AcpiUtGetTypeName (TypeToCheckFor)));
656             }
657
658             /*
659              * If this is the last name segment and we are not looking for a
660              * specific type, but the type of found object is known, use that
661              * type to (later) see if it opens a scope.
662              */
663             if (Type == ACPI_TYPE_ANY)
664             {
665                 Type = ThisNode->Type;
666             }
667         }
668
669         /* Point to next name segment and make this node current */
670
671         Path += ACPI_NAME_SIZE;
672         CurrentNode = ThisNode;
673     }
674
675     /* Always check if we need to open a new scope */
676
677     if (!(Flags & ACPI_NS_DONT_OPEN_SCOPE) && (WalkState))
678     {
679         /*
680          * If entry is a type which opens a scope, push the new scope on the
681          * scope stack.
682          */
683         if (AcpiNsOpensScope (Type))
684         {
685             Status = AcpiDsScopeStackPush (ThisNode, Type, WalkState);
686             if (ACPI_FAILURE (Status))
687             {
688                 return_ACPI_STATUS (Status);
689             }
690         }
691     }
692
693     *ReturnNode = ThisNode;
694     return_ACPI_STATUS (AE_OK);
695 }