Sync ACPICA with Intel's version 20190108.
[dragonfly.git] / sys / contrib / dev / acpica / source / components / namespace / nsxfname.c
1 /******************************************************************************
2  *
3  * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4  *                         ACPI Namespace oriented interfaces
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2019, 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  * Alternatively, you may choose to be licensed under the terms of the
118  * following license:
119  *
120  * Redistribution and use in source and binary forms, with or without
121  * modification, are permitted provided that the following conditions
122  * are met:
123  * 1. Redistributions of source code must retain the above copyright
124  *    notice, this list of conditions, and the following disclaimer,
125  *    without modification.
126  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127  *    substantially similar to the "NO WARRANTY" disclaimer below
128  *    ("Disclaimer") and any redistribution must be conditioned upon
129  *    including a substantially similar Disclaimer requirement for further
130  *    binary redistribution.
131  * 3. Neither the names of the above-listed copyright holders nor the names
132  *    of any contributors may be used to endorse or promote products derived
133  *    from this software without specific prior written permission.
134  *
135  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
138  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
139  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
140  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
141  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
145  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146  *
147  * Alternatively, you may choose to be licensed under the terms of the
148  * GNU General Public License ("GPL") version 2 as published by the Free
149  * Software Foundation.
150  *
151  *****************************************************************************/
152
153 #define EXPORT_ACPI_INTERFACES
154
155 #include "acpi.h"
156 #include "accommon.h"
157 #include "acnamesp.h"
158 #include "acparser.h"
159 #include "amlcode.h"
160
161
162 #define _COMPONENT          ACPI_NAMESPACE
163         ACPI_MODULE_NAME    ("nsxfname")
164
165 /* Local prototypes */
166
167 static char *
168 AcpiNsCopyDeviceId (
169     ACPI_PNP_DEVICE_ID      *Dest,
170     ACPI_PNP_DEVICE_ID      *Source,
171     char                    *StringArea);
172
173
174 /******************************************************************************
175  *
176  * FUNCTION:    AcpiGetHandle
177  *
178  * PARAMETERS:  Parent          - Object to search under (search scope).
179  *              Pathname        - Pointer to an asciiz string containing the
180  *                                name
181  *              RetHandle       - Where the return handle is returned
182  *
183  * RETURN:      Status
184  *
185  * DESCRIPTION: This routine will search for a caller specified name in the
186  *              name space. The caller can restrict the search region by
187  *              specifying a non NULL parent. The parent value is itself a
188  *              namespace handle.
189  *
190  ******************************************************************************/
191
192 ACPI_STATUS
193 AcpiGetHandle (
194     ACPI_HANDLE             Parent,
195     ACPI_STRING             Pathname,
196     ACPI_HANDLE             *RetHandle)
197 {
198     ACPI_STATUS             Status;
199     ACPI_NAMESPACE_NODE     *Node = NULL;
200     ACPI_NAMESPACE_NODE     *PrefixNode = NULL;
201
202
203     ACPI_FUNCTION_ENTRY ();
204
205
206     /* Parameter Validation */
207
208     if (!RetHandle || !Pathname)
209     {
210         return (AE_BAD_PARAMETER);
211     }
212
213     /* Convert a parent handle to a prefix node */
214
215     if (Parent)
216     {
217         PrefixNode = AcpiNsValidateHandle (Parent);
218         if (!PrefixNode)
219         {
220             return (AE_BAD_PARAMETER);
221         }
222     }
223
224     /*
225      * Valid cases are:
226      * 1) Fully qualified pathname
227      * 2) Parent + Relative pathname
228      *
229      * Error for <null Parent + relative path>
230      */
231     if (ACPI_IS_ROOT_PREFIX (Pathname[0]))
232     {
233         /* Pathname is fully qualified (starts with '\') */
234
235         /* Special case for root-only, since we can't search for it */
236
237         if (!strcmp (Pathname, ACPI_NS_ROOT_PATH))
238         {
239             *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, AcpiGbl_RootNode);
240             return (AE_OK);
241         }
242     }
243     else if (!PrefixNode)
244     {
245         /* Relative path with null prefix is disallowed */
246
247         return (AE_BAD_PARAMETER);
248     }
249
250     /* Find the Node and convert to a handle */
251
252     Status = AcpiNsGetNode (PrefixNode, Pathname, ACPI_NS_NO_UPSEARCH, &Node);
253     if (ACPI_SUCCESS (Status))
254     {
255         *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, Node);
256     }
257
258     return (Status);
259 }
260
261 ACPI_EXPORT_SYMBOL (AcpiGetHandle)
262
263
264 /******************************************************************************
265  *
266  * FUNCTION:    AcpiGetName
267  *
268  * PARAMETERS:  Handle          - Handle to be converted to a pathname
269  *              NameType        - Full pathname or single segment
270  *              Buffer          - Buffer for returned path
271  *
272  * RETURN:      Pointer to a string containing the fully qualified Name.
273  *
274  * DESCRIPTION: This routine returns the fully qualified name associated with
275  *              the Handle parameter. This and the AcpiPathnameToHandle are
276  *              complementary functions.
277  *
278  ******************************************************************************/
279
280 ACPI_STATUS
281 AcpiGetName (
282     ACPI_HANDLE             Handle,
283     UINT32                  NameType,
284     ACPI_BUFFER             *Buffer)
285 {
286     ACPI_STATUS             Status;
287
288
289     /* Parameter validation */
290
291     if (NameType > ACPI_NAME_TYPE_MAX)
292     {
293         return (AE_BAD_PARAMETER);
294     }
295
296     Status = AcpiUtValidateBuffer (Buffer);
297     if (ACPI_FAILURE (Status))
298     {
299         return (Status);
300     }
301
302     /*
303      * Wants the single segment ACPI name.
304      * Validate handle and convert to a namespace Node
305      */
306     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
307     if (ACPI_FAILURE (Status))
308     {
309         return (Status);
310     }
311
312     if (NameType == ACPI_FULL_PATHNAME ||
313         NameType == ACPI_FULL_PATHNAME_NO_TRAILING)
314     {
315         /* Get the full pathname (From the namespace root) */
316
317         Status = AcpiNsHandleToPathname (Handle, Buffer,
318             NameType == ACPI_FULL_PATHNAME ? FALSE : TRUE);
319     }
320     else
321     {
322         /* Get the single name */
323
324         Status = AcpiNsHandleToName (Handle, Buffer);
325     }
326
327     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
328     return (Status);
329 }
330
331 ACPI_EXPORT_SYMBOL (AcpiGetName)
332
333
334 /******************************************************************************
335  *
336  * FUNCTION:    AcpiNsCopyDeviceId
337  *
338  * PARAMETERS:  Dest                - Pointer to the destination PNP_DEVICE_ID
339  *              Source              - Pointer to the source PNP_DEVICE_ID
340  *              StringArea          - Pointer to where to copy the dest string
341  *
342  * RETURN:      Pointer to the next string area
343  *
344  * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
345  *
346  ******************************************************************************/
347
348 static char *
349 AcpiNsCopyDeviceId (
350     ACPI_PNP_DEVICE_ID      *Dest,
351     ACPI_PNP_DEVICE_ID      *Source,
352     char                    *StringArea)
353 {
354     /* Create the destination PNP_DEVICE_ID */
355
356     Dest->String = StringArea;
357     Dest->Length = Source->Length;
358
359     /* Copy actual string and return a pointer to the next string area */
360
361     memcpy (StringArea, Source->String, Source->Length);
362     return (StringArea + Source->Length);
363 }
364
365
366 /******************************************************************************
367  *
368  * FUNCTION:    AcpiGetObjectInfo
369  *
370  * PARAMETERS:  Handle              - Object Handle
371  *              ReturnBuffer        - Where the info is returned
372  *
373  * RETURN:      Status
374  *
375  * DESCRIPTION: Returns information about an object as gleaned from the
376  *              namespace node and possibly by running several standard
377  *              control methods (Such as in the case of a device.)
378  *
379  * For Device and Processor objects, run the Device _HID, _UID, _CID,
380  * _CLS, _ADR, _SxW, and _SxD methods.
381  *
382  * Note: Allocates the return buffer, must be freed by the caller.
383  *
384  * Note: This interface is intended to be used during the initial device
385  * discovery namespace traversal. Therefore, no complex methods can be
386  * executed, especially those that access operation regions. Therefore, do
387  * not add any additional methods that could cause problems in this area.
388  * Because of this reason support for the following methods has been removed:
389  * 1) _SUB method was removed (11/2015)
390  * 2) _STA method was removed (02/2018)
391  *
392  ******************************************************************************/
393
394 ACPI_STATUS
395 AcpiGetObjectInfo (
396     ACPI_HANDLE             Handle,
397     ACPI_DEVICE_INFO        **ReturnBuffer)
398 {
399     ACPI_NAMESPACE_NODE     *Node;
400     ACPI_DEVICE_INFO        *Info;
401     ACPI_PNP_DEVICE_ID_LIST *CidList = NULL;
402     ACPI_PNP_DEVICE_ID      *Hid = NULL;
403     ACPI_PNP_DEVICE_ID      *Uid = NULL;
404     ACPI_PNP_DEVICE_ID      *Cls = NULL;
405     char                    *NextIdString;
406     ACPI_OBJECT_TYPE        Type;
407     ACPI_NAME               Name;
408     UINT8                   ParamCount= 0;
409     UINT16                  Valid = 0;
410     UINT32                  InfoSize;
411     UINT32                  i;
412     ACPI_STATUS             Status;
413
414
415     /* Parameter validation */
416
417     if (!Handle || !ReturnBuffer)
418     {
419         return (AE_BAD_PARAMETER);
420     }
421
422     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
423     if (ACPI_FAILURE (Status))
424     {
425         return (Status);
426     }
427
428     Node = AcpiNsValidateHandle (Handle);
429     if (!Node)
430     {
431         (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
432         return (AE_BAD_PARAMETER);
433     }
434
435     /* Get the namespace node data while the namespace is locked */
436
437     InfoSize = sizeof (ACPI_DEVICE_INFO);
438     Type = Node->Type;
439     Name = Node->Name.Integer;
440
441     if (Node->Type == ACPI_TYPE_METHOD)
442     {
443         ParamCount = Node->Object->Method.ParamCount;
444     }
445
446     Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
447     if (ACPI_FAILURE (Status))
448     {
449         return (Status);
450     }
451
452     if ((Type == ACPI_TYPE_DEVICE) ||
453         (Type == ACPI_TYPE_PROCESSOR))
454     {
455         /*
456          * Get extra info for ACPI Device/Processor objects only:
457          * Run the Device _HID, _UID, _CLS, and _CID methods.
458          *
459          * Note: none of these methods are required, so they may or may
460          * not be present for this device. The Info->Valid bitfield is used
461          * to indicate which methods were found and run successfully.
462          */
463
464         /* Execute the Device._HID method */
465
466         Status = AcpiUtExecute_HID (Node, &Hid);
467         if (ACPI_SUCCESS (Status))
468         {
469             InfoSize += Hid->Length;
470             Valid |= ACPI_VALID_HID;
471         }
472
473         /* Execute the Device._UID method */
474
475         Status = AcpiUtExecute_UID (Node, &Uid);
476         if (ACPI_SUCCESS (Status))
477         {
478             InfoSize += Uid->Length;
479             Valid |= ACPI_VALID_UID;
480         }
481
482         /* Execute the Device._CID method */
483
484         Status = AcpiUtExecute_CID (Node, &CidList);
485         if (ACPI_SUCCESS (Status))
486         {
487             /* Add size of CID strings and CID pointer array */
488
489             InfoSize += (CidList->ListSize - sizeof (ACPI_PNP_DEVICE_ID_LIST));
490             Valid |= ACPI_VALID_CID;
491         }
492
493         /* Execute the Device._CLS method */
494
495         Status = AcpiUtExecute_CLS (Node, &Cls);
496         if (ACPI_SUCCESS (Status))
497         {
498             InfoSize += Cls->Length;
499             Valid |= ACPI_VALID_CLS;
500         }
501     }
502
503     /*
504      * Now that we have the variable-length data, we can allocate the
505      * return buffer
506      */
507     Info = ACPI_ALLOCATE_ZEROED (InfoSize);
508     if (!Info)
509     {
510         Status = AE_NO_MEMORY;
511         goto Cleanup;
512     }
513
514     /* Get the fixed-length data */
515
516     if ((Type == ACPI_TYPE_DEVICE) ||
517         (Type == ACPI_TYPE_PROCESSOR))
518     {
519         /*
520          * Get extra info for ACPI Device/Processor objects only:
521          * Run the _ADR and, SxW, and _SxD methods.
522          *
523          * Notes: none of these methods are required, so they may or may
524          * not be present for this device. The Info->Valid bitfield is used
525          * to indicate which methods were found and run successfully.
526          */
527
528         /* Execute the Device._ADR method */
529
530         Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR, Node,
531             &Info->Address);
532         if (ACPI_SUCCESS (Status))
533         {
534             Valid |= ACPI_VALID_ADR;
535         }
536
537         /* Execute the Device._SxW methods */
538
539         Status = AcpiUtExecutePowerMethods (Node,
540             AcpiGbl_LowestDstateNames, ACPI_NUM_SxW_METHODS,
541             Info->LowestDstates);
542         if (ACPI_SUCCESS (Status))
543         {
544             Valid |= ACPI_VALID_SXWS;
545         }
546
547         /* Execute the Device._SxD methods */
548
549         Status = AcpiUtExecutePowerMethods (Node,
550             AcpiGbl_HighestDstateNames, ACPI_NUM_SxD_METHODS,
551             Info->HighestDstates);
552         if (ACPI_SUCCESS (Status))
553         {
554             Valid |= ACPI_VALID_SXDS;
555         }
556     }
557
558     /*
559      * Create a pointer to the string area of the return buffer.
560      * Point to the end of the base ACPI_DEVICE_INFO structure.
561      */
562     NextIdString = ACPI_CAST_PTR (char, Info->CompatibleIdList.Ids);
563     if (CidList)
564     {
565         /* Point past the CID PNP_DEVICE_ID array */
566
567         NextIdString += ((ACPI_SIZE) CidList->Count * sizeof (ACPI_PNP_DEVICE_ID));
568     }
569
570     /*
571      * Copy the HID, UID, and CIDs to the return buffer. The variable-length
572      * strings are copied to the reserved area at the end of the buffer.
573      *
574      * For HID and CID, check if the ID is a PCI Root Bridge.
575      */
576     if (Hid)
577     {
578         NextIdString = AcpiNsCopyDeviceId (&Info->HardwareId,
579             Hid, NextIdString);
580
581         if (AcpiUtIsPciRootBridge (Hid->String))
582         {
583             Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
584         }
585     }
586
587     if (Uid)
588     {
589         NextIdString = AcpiNsCopyDeviceId (&Info->UniqueId,
590             Uid, NextIdString);
591     }
592
593     if (CidList)
594     {
595         Info->CompatibleIdList.Count = CidList->Count;
596         Info->CompatibleIdList.ListSize = CidList->ListSize;
597
598         /* Copy each CID */
599
600         for (i = 0; i < CidList->Count; i++)
601         {
602             NextIdString = AcpiNsCopyDeviceId (&Info->CompatibleIdList.Ids[i],
603                 &CidList->Ids[i], NextIdString);
604
605             if (AcpiUtIsPciRootBridge (CidList->Ids[i].String))
606             {
607                 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
608             }
609         }
610     }
611
612     if (Cls)
613     {
614         NextIdString = AcpiNsCopyDeviceId (&Info->ClassCode,
615             Cls, NextIdString);
616     }
617
618     /* Copy the fixed-length data */
619
620     Info->InfoSize = InfoSize;
621     Info->Type = Type;
622     Info->Name = Name;
623     Info->ParamCount = ParamCount;
624     Info->Valid = Valid;
625
626     *ReturnBuffer = Info;
627     Status = AE_OK;
628
629
630 Cleanup:
631     if (Hid)
632     {
633         ACPI_FREE (Hid);
634     }
635     if (Uid)
636     {
637         ACPI_FREE (Uid);
638     }
639     if (CidList)
640     {
641         ACPI_FREE (CidList);
642     }
643     if (Cls)
644     {
645         ACPI_FREE (Cls);
646     }
647     return (Status);
648 }
649
650 ACPI_EXPORT_SYMBOL (AcpiGetObjectInfo)
651
652
653 /******************************************************************************
654  *
655  * FUNCTION:    AcpiInstallMethod
656  *
657  * PARAMETERS:  Buffer         - An ACPI table containing one control method
658  *
659  * RETURN:      Status
660  *
661  * DESCRIPTION: Install a control method into the namespace. If the method
662  *              name already exists in the namespace, it is overwritten. The
663  *              input buffer must contain a valid DSDT or SSDT containing a
664  *              single control method.
665  *
666  ******************************************************************************/
667
668 ACPI_STATUS
669 AcpiInstallMethod (
670     UINT8                   *Buffer)
671 {
672     ACPI_TABLE_HEADER       *Table = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Buffer);
673     UINT8                   *AmlBuffer;
674     UINT8                   *AmlStart;
675     char                    *Path;
676     ACPI_NAMESPACE_NODE     *Node;
677     ACPI_OPERAND_OBJECT     *MethodObj;
678     ACPI_PARSE_STATE        ParserState;
679     UINT32                  AmlLength;
680     UINT16                  Opcode;
681     UINT8                   MethodFlags;
682     ACPI_STATUS             Status;
683
684
685     /* Parameter validation */
686
687     if (!Buffer)
688     {
689         return (AE_BAD_PARAMETER);
690     }
691
692     /* Table must be a DSDT or SSDT */
693
694     if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) &&
695         !ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
696     {
697         return (AE_BAD_HEADER);
698     }
699
700     /* First AML opcode in the table must be a control method */
701
702     ParserState.Aml = Buffer + sizeof (ACPI_TABLE_HEADER);
703     Opcode = AcpiPsPeekOpcode (&ParserState);
704     if (Opcode != AML_METHOD_OP)
705     {
706         return (AE_BAD_PARAMETER);
707     }
708
709     /* Extract method information from the raw AML */
710
711     ParserState.Aml += AcpiPsGetOpcodeSize (Opcode);
712     ParserState.PkgEnd = AcpiPsGetNextPackageEnd (&ParserState);
713     Path = AcpiPsGetNextNamestring (&ParserState);
714
715     MethodFlags = *ParserState.Aml++;
716     AmlStart = ParserState.Aml;
717     AmlLength = ACPI_PTR_DIFF (ParserState.PkgEnd, AmlStart);
718
719     /*
720      * Allocate resources up-front. We don't want to have to delete a new
721      * node from the namespace if we cannot allocate memory.
722      */
723     AmlBuffer = ACPI_ALLOCATE (AmlLength);
724     if (!AmlBuffer)
725     {
726         return (AE_NO_MEMORY);
727     }
728
729     MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
730     if (!MethodObj)
731     {
732         ACPI_FREE (AmlBuffer);
733         return (AE_NO_MEMORY);
734     }
735
736     /* Lock namespace for AcpiNsLookup, we may be creating a new node */
737
738     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
739     if (ACPI_FAILURE (Status))
740     {
741         goto ErrorExit;
742     }
743
744     /* The lookup either returns an existing node or creates a new one */
745
746     Status = AcpiNsLookup (NULL, Path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
747         ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
748
749     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
750
751     if (ACPI_FAILURE (Status)) /* NsLookup */
752     {
753         if (Status != AE_ALREADY_EXISTS)
754         {
755             goto ErrorExit;
756         }
757
758         /* Node existed previously, make sure it is a method node */
759
760         if (Node->Type != ACPI_TYPE_METHOD)
761         {
762             Status = AE_TYPE;
763             goto ErrorExit;
764         }
765     }
766
767     /* Copy the method AML to the local buffer */
768
769     memcpy (AmlBuffer, AmlStart, AmlLength);
770
771     /* Initialize the method object with the new method's information */
772
773     MethodObj->Method.AmlStart = AmlBuffer;
774     MethodObj->Method.AmlLength = AmlLength;
775
776     MethodObj->Method.ParamCount = (UINT8)
777         (MethodFlags & AML_METHOD_ARG_COUNT);
778
779     if (MethodFlags & AML_METHOD_SERIALIZED)
780     {
781         MethodObj->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
782
783         MethodObj->Method.SyncLevel = (UINT8)
784             ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
785     }
786
787     /*
788      * Now that it is complete, we can attach the new method object to
789      * the method Node (detaches/deletes any existing object)
790      */
791     Status = AcpiNsAttachObject (Node, MethodObj, ACPI_TYPE_METHOD);
792
793     /*
794      * Flag indicates AML buffer is dynamic, must be deleted later.
795      * Must be set only after attach above.
796      */
797     Node->Flags |= ANOBJ_ALLOCATED_BUFFER;
798
799     /* Remove local reference to the method object */
800
801     AcpiUtRemoveReference (MethodObj);
802     return (Status);
803
804
805 ErrorExit:
806
807     ACPI_FREE (AmlBuffer);
808     ACPI_FREE (MethodObj);
809     return (Status);
810 }
811
812 ACPI_EXPORT_SYMBOL (AcpiInstallMethod)