nsload.c revision 91116
1258945Sroberto/******************************************************************************
2258945Sroberto *
3258945Sroberto * Module Name: nsload - namespace loading/expanding/contracting procedures
4258945Sroberto *              $Revision: 53 $
5258945Sroberto *
6258945Sroberto *****************************************************************************/
7280849Scy
8280849Scy/******************************************************************************
9258945Sroberto *
10258945Sroberto * 1. Copyright Notice
11280849Scy *
12258945Sroberto * Some or all of this work - Copyright (c) 1999 - 2002, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights.  You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
21 *
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
28 *
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code.  No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision.  In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change.  Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee.  Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
54 *
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution.  In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
66 *
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government.  In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117#define __NSLOAD_C__
118
119#include "acpi.h"
120#include "acinterp.h"
121#include "acnamesp.h"
122#include "amlcode.h"
123#include "acparser.h"
124#include "acdispat.h"
125#include "acdebug.h"
126
127
128#define _COMPONENT          ACPI_NAMESPACE
129        ACPI_MODULE_NAME    ("nsload")
130
131
132/*******************************************************************************
133 *
134 * FUNCTION:    AcpiLoadNamespace
135 *
136 * PARAMETERS:  None
137 *
138 * RETURN:      Status
139 *
140 * DESCRIPTION: Load the name space from what ever is pointed to by DSDT.
141 *              (DSDT points to either the BIOS or a buffer.)
142 *
143 ******************************************************************************/
144
145ACPI_STATUS
146AcpiNsLoadNamespace (
147    void)
148{
149    ACPI_STATUS             Status;
150
151
152    ACPI_FUNCTION_TRACE ("AcpiLoadNameSpace");
153
154
155    /* There must be at least a DSDT installed */
156
157    if (AcpiGbl_DSDT == NULL)
158    {
159        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "DSDT is not in memory\n"));
160        return_ACPI_STATUS (AE_NO_ACPI_TABLES);
161    }
162
163    /*
164     * Load the namespace.  The DSDT is required,
165     * but the SSDT and PSDT tables are optional.
166     */
167    Status = AcpiNsLoadTableByType (ACPI_TABLE_DSDT);
168    if (ACPI_FAILURE (Status))
169    {
170        return_ACPI_STATUS (Status);
171    }
172
173    /* Ignore exceptions from these */
174
175    AcpiNsLoadTableByType (ACPI_TABLE_SSDT);
176    AcpiNsLoadTableByType (ACPI_TABLE_PSDT);
177
178    ACPI_DEBUG_PRINT_RAW ((ACPI_DB_OK,
179        "ACPI Namespace successfully loaded at root %p\n",
180        AcpiGbl_RootNode));
181
182    return_ACPI_STATUS (Status);
183}
184
185
186/*******************************************************************************
187 *
188 * FUNCTION:    AcpiNsOneParsePass
189 *
190 * PARAMETERS:  PassNumber              - 1 or 2
191 *              TableDesc               - The table to be parsed.
192 *
193 * RETURN:      Status
194 *
195 * DESCRIPTION: Perform one complete parse of an ACPI/AML table.
196 *
197 ******************************************************************************/
198
199ACPI_STATUS
200AcpiNsOneCompleteParse (
201    UINT32                  PassNumber,
202    ACPI_TABLE_DESC         *TableDesc)
203{
204    ACPI_PARSE_OBJECT       *ParseRoot;
205    ACPI_STATUS             Status;
206    ACPI_WALK_STATE         *WalkState;
207
208
209    ACPI_FUNCTION_TRACE ("NsOneCompleteParse");
210
211
212    /* Create and init a Root Node */
213
214    ParseRoot = AcpiPsAllocOp (AML_SCOPE_OP);
215    if (!ParseRoot)
216    {
217        return_ACPI_STATUS (AE_NO_MEMORY);
218    }
219
220    ((ACPI_PARSE2_OBJECT *) ParseRoot)->Name = ACPI_ROOT_NAME;
221
222    /* Create and initialize a new walk state */
223
224    WalkState = AcpiDsCreateWalkState (TABLE_ID_DSDT,
225                                    NULL, NULL, NULL);
226    if (!WalkState)
227    {
228        AcpiPsFreeOp (ParseRoot);
229        return_ACPI_STATUS (AE_NO_MEMORY);
230    }
231
232    Status = AcpiDsInitAmlWalk (WalkState, ParseRoot, NULL, TableDesc->AmlStart,
233                    TableDesc->AmlLength, NULL, NULL, PassNumber);
234    if (ACPI_FAILURE (Status))
235    {
236        AcpiDsDeleteWalkState (WalkState);
237        return_ACPI_STATUS (Status);
238    }
239
240    /* Parse the AML */
241
242    ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "*PARSE* pass %d parse\n", PassNumber));
243    Status = AcpiPsParseAml (WalkState);
244
245    AcpiPsDeleteParseTree (ParseRoot);
246    return_ACPI_STATUS (Status);
247}
248
249
250/*******************************************************************************
251 *
252 * FUNCTION:    AcpiNsParseTable
253 *
254 * PARAMETERS:  TableDesc       - An ACPI table descriptor for table to parse
255 *              StartNode       - Where to enter the table into the namespace
256 *
257 * RETURN:      Status
258 *
259 * DESCRIPTION: Parse AML within an ACPI table and return a tree of ops
260 *
261 ******************************************************************************/
262
263ACPI_STATUS
264AcpiNsParseTable (
265    ACPI_TABLE_DESC         *TableDesc,
266    ACPI_NAMESPACE_NODE     *StartNode)
267{
268    ACPI_STATUS             Status;
269
270
271    ACPI_FUNCTION_TRACE ("NsParseTable");
272
273
274    /*
275     * AML Parse, pass 1
276     *
277     * In this pass, we load most of the namespace.  Control methods
278     * are not parsed until later.  A parse tree is not created.  Instead,
279     * each Parser Op subtree is deleted when it is finished.  This saves
280     * a great deal of memory, and allows a small cache of parse objects
281     * to service the entire parse.  The second pass of the parse then
282     * performs another complete parse of the AML..
283     */
284    Status = AcpiNsOneCompleteParse (1, TableDesc);
285    if (ACPI_FAILURE (Status))
286    {
287        return_ACPI_STATUS (Status);
288    }
289
290    /*
291     * AML Parse, pass 2
292     *
293     * In this pass, we resolve forward references and other things
294     * that could not be completed during the first pass.
295     * Another complete parse of the AML is performed, but the
296     * overhead of this is compensated for by the fact that the
297     * parse objects are all cached.
298     */
299    Status = AcpiNsOneCompleteParse (2, TableDesc);
300    if (ACPI_FAILURE (Status))
301    {
302        return_ACPI_STATUS (Status);
303    }
304
305    return_ACPI_STATUS (Status);
306}
307
308
309/*******************************************************************************
310 *
311 * FUNCTION:    AcpiNsLoadTable
312 *
313 * PARAMETERS:  TableDesc       - Descriptor for table to be loaded
314 *              Node            - Owning NS node
315 *
316 * RETURN:      Status
317 *
318 * DESCRIPTION: Load one ACPI table into the namespace
319 *
320 ******************************************************************************/
321
322ACPI_STATUS
323AcpiNsLoadTable (
324    ACPI_TABLE_DESC         *TableDesc,
325    ACPI_NAMESPACE_NODE     *Node)
326{
327    ACPI_STATUS             Status;
328
329
330    ACPI_FUNCTION_TRACE ("NsLoadTable");
331
332
333    /* Check if table contains valid AML (must be DSDT, PSDT, SSDT, etc.) */
334
335    if (!(AcpiGbl_AcpiTableData[TableDesc->Type].Flags & ACPI_TABLE_EXECUTABLE))
336    {
337        /* Just ignore this table */
338
339        return_ACPI_STATUS (AE_OK);
340    }
341
342    /* Check validity of the AML start and length */
343
344    if (!TableDesc->AmlStart)
345    {
346        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null AML pointer\n"));
347        return_ACPI_STATUS (AE_BAD_PARAMETER);
348    }
349
350    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AML block at %p\n", TableDesc->AmlStart));
351
352    if (!TableDesc->AmlLength)
353    {
354        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Zero-length AML block\n"));
355        return_ACPI_STATUS (AE_BAD_PARAMETER);
356    }
357
358    /*
359     * Parse the table and load the namespace with all named
360     * objects found within.  Control methods are NOT parsed
361     * at this time.  In fact, the control methods cannot be
362     * parsed until the entire namespace is loaded, because
363     * if a control method makes a forward reference (call)
364     * to another control method, we can't continue parsing
365     * because we don't know how many arguments to parse next!
366     */
367    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** Loading table into namespace ****\n"));
368
369    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
370    if (ACPI_FAILURE (Status))
371    {
372        return_ACPI_STATUS (Status);
373    }
374
375    Status = AcpiNsParseTable (TableDesc, Node->Child);
376    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
377
378    if (ACPI_FAILURE (Status))
379    {
380        return_ACPI_STATUS (Status);
381    }
382
383    /*
384     * Now we can parse the control methods.  We always parse
385     * them here for a sanity check, and if configured for
386     * just-in-time parsing, we delete the control method
387     * parse trees.
388     */
389    ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
390        "**** Begin Table Method Parsing and Object Initialization ****\n"));
391
392    Status = AcpiDsInitializeObjects (TableDesc, Node);
393
394    ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
395        "**** Completed Table Method Parsing and Object Initialization ****\n"));
396
397    return_ACPI_STATUS (Status);
398}
399
400
401/*******************************************************************************
402 *
403 * FUNCTION:    AcpiNsLoadTableByType
404 *
405 * PARAMETERS:  TableType           - Id of the table type to load
406 *
407 * RETURN:      Status
408 *
409 * DESCRIPTION: Load an ACPI table or tables into the namespace.  All tables
410 *              of the given type are loaded.  The mechanism allows this
411 *              routine to be called repeatedly.
412 *
413 ******************************************************************************/
414
415ACPI_STATUS
416AcpiNsLoadTableByType (
417    ACPI_TABLE_TYPE         TableType)
418{
419    UINT32                  i;
420    ACPI_STATUS             Status;
421    ACPI_TABLE_DESC         *TableDesc;
422
423
424    ACPI_FUNCTION_TRACE ("NsLoadTableByType");
425
426
427    Status = AcpiUtAcquireMutex (ACPI_MTX_TABLES);
428    if (ACPI_FAILURE (Status))
429    {
430        return_ACPI_STATUS (Status);
431    }
432
433    /*
434     * Table types supported are:
435     * DSDT (one), SSDT/PSDT (multiple)
436     */
437    switch (TableType)
438    {
439    case ACPI_TABLE_DSDT:
440
441        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Loading DSDT\n"));
442
443        TableDesc = &AcpiGbl_AcpiTables[ACPI_TABLE_DSDT];
444
445        /* If table already loaded into namespace, just return */
446
447        if (TableDesc->LoadedIntoNamespace)
448        {
449            goto UnlockAndExit;
450        }
451
452        TableDesc->TableId = TABLE_ID_DSDT;
453
454        /* Now load the single DSDT */
455
456        Status = AcpiNsLoadTable (TableDesc, AcpiGbl_RootNode);
457        if (ACPI_SUCCESS (Status))
458        {
459            TableDesc->LoadedIntoNamespace = TRUE;
460        }
461
462        break;
463
464
465    case ACPI_TABLE_SSDT:
466
467        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Loading %d SSDTs\n",
468            AcpiGbl_AcpiTables[ACPI_TABLE_SSDT].Count));
469
470        /*
471         * Traverse list of SSDT tables
472         */
473        TableDesc = &AcpiGbl_AcpiTables[ACPI_TABLE_SSDT];
474        for (i = 0; i < AcpiGbl_AcpiTables[ACPI_TABLE_SSDT].Count; i++)
475        {
476            /*
477             * Only attempt to load table if it is not
478             * already loaded!
479             */
480            if (!TableDesc->LoadedIntoNamespace)
481            {
482                Status = AcpiNsLoadTable (TableDesc, AcpiGbl_RootNode);
483                if (ACPI_FAILURE (Status))
484                {
485                    break;
486                }
487
488                TableDesc->LoadedIntoNamespace = TRUE;
489            }
490
491            TableDesc = TableDesc->Next;
492        }
493        break;
494
495
496    case ACPI_TABLE_PSDT:
497
498        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Loading %d PSDTs\n",
499            AcpiGbl_AcpiTables[ACPI_TABLE_PSDT].Count));
500
501        /*
502         * Traverse list of PSDT tables
503         */
504        TableDesc = &AcpiGbl_AcpiTables[ACPI_TABLE_PSDT];
505
506        for (i = 0; i < AcpiGbl_AcpiTables[ACPI_TABLE_PSDT].Count; i++)
507        {
508            /* Only attempt to load table if it is not already loaded! */
509
510            if (!TableDesc->LoadedIntoNamespace)
511            {
512                Status = AcpiNsLoadTable (TableDesc, AcpiGbl_RootNode);
513                if (ACPI_FAILURE (Status))
514                {
515                    break;
516                }
517
518                TableDesc->LoadedIntoNamespace = TRUE;
519            }
520
521            TableDesc = TableDesc->Next;
522        }
523
524        break;
525
526
527    default:
528        Status = AE_SUPPORT;
529        break;
530    }
531
532
533UnlockAndExit:
534    (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
535    return_ACPI_STATUS (Status);
536}
537
538
539/*******************************************************************************
540 *
541 * FUNCTION:    AcpiNsDeleteSubtree
542 *
543 * PARAMETERS:  StartHandle         - Handle in namespace where search begins
544 *
545 * RETURNS      Status
546 *
547 * DESCRIPTION: Walks the namespace starting at the given handle and deletes
548 *              all objects, entries, and scopes in the entire subtree.
549 *
550 *              Namespace/Interpreter should be locked or the subsystem should
551 *              be in shutdown before this routine is called.
552 *
553 ******************************************************************************/
554
555ACPI_STATUS
556AcpiNsDeleteSubtree (
557    ACPI_HANDLE             StartHandle)
558{
559    ACPI_STATUS             Status;
560    ACPI_HANDLE             ChildHandle;
561    ACPI_HANDLE             ParentHandle;
562    ACPI_HANDLE             NextChildHandle;
563    ACPI_HANDLE             Dummy;
564    UINT32                  Level;
565
566
567    ACPI_FUNCTION_TRACE ("NsDeleteSubtree");
568
569
570    ParentHandle = StartHandle;
571    ChildHandle  = 0;
572    Level        = 1;
573
574    /*
575     * Traverse the tree of objects until we bubble back up
576     * to where we started.
577     */
578    while (Level > 0)
579    {
580        /* Attempt to get the next object in this scope */
581
582        Status = AcpiGetNextObject (ACPI_TYPE_ANY, ParentHandle,
583                                    ChildHandle, &NextChildHandle);
584
585        ChildHandle = NextChildHandle;
586
587        /* Did we get a new object? */
588
589        if (ACPI_SUCCESS (Status))
590        {
591            /* Check if this object has any children */
592
593            if (ACPI_SUCCESS (AcpiGetNextObject (ACPI_TYPE_ANY, ChildHandle,
594                                    0, &Dummy)))
595            {
596                /*
597                 * There is at least one child of this object,
598                 * visit the object
599                 */
600                Level++;
601                ParentHandle = ChildHandle;
602                ChildHandle  = 0;
603            }
604        }
605        else
606        {
607            /*
608             * No more children in this object, go back up to
609             * the object's parent
610             */
611            Level--;
612
613            /* Delete all children now */
614
615            AcpiNsDeleteChildren (ChildHandle);
616
617            ChildHandle = ParentHandle;
618            AcpiGetParent (ParentHandle, &ParentHandle);
619        }
620    }
621
622    /* Now delete the starting object, and we are done */
623
624    AcpiNsDeleteNode (ChildHandle);
625
626    return_ACPI_STATUS (AE_OK);
627}
628
629
630/*******************************************************************************
631 *
632 *  FUNCTION:       AcpiNsUnloadNameSpace
633 *
634 *  PARAMETERS:     Handle          - Root of namespace subtree to be deleted
635 *
636 *  RETURN:         Status
637 *
638 *  DESCRIPTION:    Shrinks the namespace, typically in response to an undocking
639 *                  event.  Deletes an entire subtree starting from (and
640 *                  including) the given handle.
641 *
642 ******************************************************************************/
643
644ACPI_STATUS
645AcpiNsUnloadNamespace (
646    ACPI_HANDLE             Handle)
647{
648    ACPI_STATUS             Status;
649
650
651    ACPI_FUNCTION_TRACE ("NsUnloadNameSpace");
652
653
654    /* Parameter validation */
655
656    if (!AcpiGbl_RootNode)
657    {
658        return_ACPI_STATUS (AE_NO_NAMESPACE);
659    }
660
661    if (!Handle)
662    {
663        return_ACPI_STATUS (AE_BAD_PARAMETER);
664    }
665
666    /* This function does the real work */
667
668    Status = AcpiNsDeleteSubtree (Handle);
669
670    return_ACPI_STATUS (Status);
671}
672
673
674