dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / uts / intel / io / acpica / namespace / nsxfname.c
blobcfd6937f497fbfb5498dd1095fbcbac061163ab3
1 /******************************************************************************
3 * Module Name: nsxfname - Public interfaces to the ACPI subsystem
4 * ACPI Namespace oriented interfaces
6 *****************************************************************************/
8 /*
9 * Copyright (C) 2000 - 2016, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #define EXPORT_ACPI_INTERFACES
47 #include "acpi.h"
48 #include "accommon.h"
49 #include "acnamesp.h"
50 #include "acparser.h"
51 #include "amlcode.h"
54 #define _COMPONENT ACPI_NAMESPACE
55 ACPI_MODULE_NAME ("nsxfname")
57 /* Local prototypes */
59 static char *
60 AcpiNsCopyDeviceId (
61 ACPI_PNP_DEVICE_ID *Dest,
62 ACPI_PNP_DEVICE_ID *Source,
63 char *StringArea);
66 /******************************************************************************
68 * FUNCTION: AcpiGetHandle
70 * PARAMETERS: Parent - Object to search under (search scope).
71 * Pathname - Pointer to an asciiz string containing the
72 * name
73 * RetHandle - Where the return handle is returned
75 * RETURN: Status
77 * DESCRIPTION: This routine will search for a caller specified name in the
78 * name space. The caller can restrict the search region by
79 * specifying a non NULL parent. The parent value is itself a
80 * namespace handle.
82 ******************************************************************************/
84 ACPI_STATUS
85 AcpiGetHandle (
86 ACPI_HANDLE Parent,
87 ACPI_STRING Pathname,
88 ACPI_HANDLE *RetHandle)
90 ACPI_STATUS Status;
91 ACPI_NAMESPACE_NODE *Node = NULL;
92 ACPI_NAMESPACE_NODE *PrefixNode = NULL;
95 ACPI_FUNCTION_ENTRY ();
98 /* Parameter Validation */
100 if (!RetHandle || !Pathname)
102 return (AE_BAD_PARAMETER);
105 /* Convert a parent handle to a prefix node */
107 if (Parent)
109 PrefixNode = AcpiNsValidateHandle (Parent);
110 if (!PrefixNode)
112 return (AE_BAD_PARAMETER);
117 * Valid cases are:
118 * 1) Fully qualified pathname
119 * 2) Parent + Relative pathname
121 * Error for <null Parent + relative path>
123 if (ACPI_IS_ROOT_PREFIX (Pathname[0]))
125 /* Pathname is fully qualified (starts with '\') */
127 /* Special case for root-only, since we can't search for it */
129 if (!strcmp (Pathname, ACPI_NS_ROOT_PATH))
131 *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, AcpiGbl_RootNode);
132 return (AE_OK);
135 else if (!PrefixNode)
137 /* Relative path with null prefix is disallowed */
139 return (AE_BAD_PARAMETER);
142 /* Find the Node and convert to a handle */
144 Status = AcpiNsGetNode (PrefixNode, Pathname, ACPI_NS_NO_UPSEARCH, &Node);
145 if (ACPI_SUCCESS (Status))
147 *RetHandle = ACPI_CAST_PTR (ACPI_HANDLE, Node);
150 return (Status);
153 ACPI_EXPORT_SYMBOL (AcpiGetHandle)
156 /******************************************************************************
158 * FUNCTION: AcpiGetName
160 * PARAMETERS: Handle - Handle to be converted to a pathname
161 * NameType - Full pathname or single segment
162 * Buffer - Buffer for returned path
164 * RETURN: Pointer to a string containing the fully qualified Name.
166 * DESCRIPTION: This routine returns the fully qualified name associated with
167 * the Handle parameter. This and the AcpiPathnameToHandle are
168 * complementary functions.
170 ******************************************************************************/
172 ACPI_STATUS
173 AcpiGetName (
174 ACPI_HANDLE Handle,
175 UINT32 NameType,
176 ACPI_BUFFER *Buffer)
178 ACPI_STATUS Status;
179 ACPI_NAMESPACE_NODE *Node;
180 const char *NodeName;
183 /* Parameter validation */
185 if (NameType > ACPI_NAME_TYPE_MAX)
187 return (AE_BAD_PARAMETER);
190 Status = AcpiUtValidateBuffer (Buffer);
191 if (ACPI_FAILURE (Status))
193 return (Status);
196 if (NameType == ACPI_FULL_PATHNAME ||
197 NameType == ACPI_FULL_PATHNAME_NO_TRAILING)
199 /* Get the full pathname (From the namespace root) */
201 Status = AcpiNsHandleToPathname (Handle, Buffer,
202 NameType == ACPI_FULL_PATHNAME ? FALSE : TRUE);
203 return (Status);
207 * Wants the single segment ACPI name.
208 * Validate handle and convert to a namespace Node
210 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
211 if (ACPI_FAILURE (Status))
213 return (Status);
216 Node = AcpiNsValidateHandle (Handle);
217 if (!Node)
219 Status = AE_BAD_PARAMETER;
220 goto UnlockAndExit;
223 /* Validate/Allocate/Clear caller buffer */
225 Status = AcpiUtInitializeBuffer (Buffer, ACPI_PATH_SEGMENT_LENGTH);
226 if (ACPI_FAILURE (Status))
228 goto UnlockAndExit;
231 /* Just copy the ACPI name from the Node and zero terminate it */
233 NodeName = AcpiUtGetNodeName (Node);
234 ACPI_MOVE_NAME (Buffer->Pointer, NodeName);
235 ((char *) Buffer->Pointer) [ACPI_NAME_SIZE] = 0;
236 Status = AE_OK;
239 UnlockAndExit:
241 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
242 return (Status);
245 ACPI_EXPORT_SYMBOL (AcpiGetName)
248 /******************************************************************************
250 * FUNCTION: AcpiNsCopyDeviceId
252 * PARAMETERS: Dest - Pointer to the destination PNP_DEVICE_ID
253 * Source - Pointer to the source PNP_DEVICE_ID
254 * StringArea - Pointer to where to copy the dest string
256 * RETURN: Pointer to the next string area
258 * DESCRIPTION: Copy a single PNP_DEVICE_ID, including the string data.
260 ******************************************************************************/
262 static char *
263 AcpiNsCopyDeviceId (
264 ACPI_PNP_DEVICE_ID *Dest,
265 ACPI_PNP_DEVICE_ID *Source,
266 char *StringArea)
268 /* Create the destination PNP_DEVICE_ID */
270 Dest->String = StringArea;
271 Dest->Length = Source->Length;
273 /* Copy actual string and return a pointer to the next string area */
275 memcpy (StringArea, Source->String, Source->Length);
276 return (StringArea + Source->Length);
280 /******************************************************************************
282 * FUNCTION: AcpiGetObjectInfo
284 * PARAMETERS: Handle - Object Handle
285 * ReturnBuffer - Where the info is returned
287 * RETURN: Status
289 * DESCRIPTION: Returns information about an object as gleaned from the
290 * namespace node and possibly by running several standard
291 * control methods (Such as in the case of a device.)
293 * For Device and Processor objects, run the Device _HID, _UID, _CID, _STA,
294 * _CLS, _ADR, _SxW, and _SxD methods.
296 * Note: Allocates the return buffer, must be freed by the caller.
298 * Note: This interface is intended to be used during the initial device
299 * discovery namespace traversal. Therefore, no complex methods can be
300 * executed, especially those that access operation regions. Therefore, do
301 * not add any additional methods that could cause problems in this area.
302 * this was the fate of the _SUB method which was found to cause such
303 * problems and was removed (11/2015).
305 ******************************************************************************/
307 ACPI_STATUS
308 AcpiGetObjectInfo (
309 ACPI_HANDLE Handle,
310 ACPI_DEVICE_INFO **ReturnBuffer)
312 ACPI_NAMESPACE_NODE *Node;
313 ACPI_DEVICE_INFO *Info;
314 ACPI_PNP_DEVICE_ID_LIST *CidList = NULL;
315 ACPI_PNP_DEVICE_ID *Hid = NULL;
316 ACPI_PNP_DEVICE_ID *Uid = NULL;
317 ACPI_PNP_DEVICE_ID *Cls = NULL;
318 char *NextIdString;
319 ACPI_OBJECT_TYPE Type;
320 ACPI_NAME Name;
321 UINT8 ParamCount= 0;
322 UINT16 Valid = 0;
323 UINT32 InfoSize;
324 UINT32 i;
325 ACPI_STATUS Status;
328 /* Parameter validation */
330 if (!Handle || !ReturnBuffer)
332 return (AE_BAD_PARAMETER);
335 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
336 if (ACPI_FAILURE (Status))
338 return (Status);
341 Node = AcpiNsValidateHandle (Handle);
342 if (!Node)
344 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
345 return (AE_BAD_PARAMETER);
348 /* Get the namespace node data while the namespace is locked */
350 InfoSize = sizeof (ACPI_DEVICE_INFO);
351 Type = Node->Type;
352 Name = Node->Name.Integer;
354 if (Node->Type == ACPI_TYPE_METHOD)
356 ParamCount = Node->Object->Method.ParamCount;
359 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
360 if (ACPI_FAILURE (Status))
362 return (Status);
365 if ((Type == ACPI_TYPE_DEVICE) ||
366 (Type == ACPI_TYPE_PROCESSOR))
369 * Get extra info for ACPI Device/Processor objects only:
370 * Run the Device _HID, _UID, _CLS, and _CID methods.
372 * Note: none of these methods are required, so they may or may
373 * not be present for this device. The Info->Valid bitfield is used
374 * to indicate which methods were found and run successfully.
377 /* Execute the Device._HID method */
379 Status = AcpiUtExecute_HID (Node, &Hid);
380 if (ACPI_SUCCESS (Status))
382 InfoSize += Hid->Length;
383 Valid |= ACPI_VALID_HID;
386 /* Execute the Device._UID method */
388 Status = AcpiUtExecute_UID (Node, &Uid);
389 if (ACPI_SUCCESS (Status))
391 InfoSize += Uid->Length;
392 Valid |= ACPI_VALID_UID;
395 /* Execute the Device._CID method */
397 Status = AcpiUtExecute_CID (Node, &CidList);
398 if (ACPI_SUCCESS (Status))
400 /* Add size of CID strings and CID pointer array */
402 InfoSize += (CidList->ListSize - sizeof (ACPI_PNP_DEVICE_ID_LIST));
403 Valid |= ACPI_VALID_CID;
406 /* Execute the Device._CLS method */
408 Status = AcpiUtExecute_CLS (Node, &Cls);
409 if (ACPI_SUCCESS (Status))
411 InfoSize += Cls->Length;
412 Valid |= ACPI_VALID_CLS;
417 * Now that we have the variable-length data, we can allocate the
418 * return buffer
420 Info = ACPI_ALLOCATE_ZEROED (InfoSize);
421 if (!Info)
423 Status = AE_NO_MEMORY;
424 goto Cleanup;
427 /* Get the fixed-length data */
429 if ((Type == ACPI_TYPE_DEVICE) ||
430 (Type == ACPI_TYPE_PROCESSOR))
433 * Get extra info for ACPI Device/Processor objects only:
434 * Run the _STA, _ADR and, SxW, and _SxD methods.
436 * Notes: none of these methods are required, so they may or may
437 * not be present for this device. The Info->Valid bitfield is used
438 * to indicate which methods were found and run successfully.
440 * For _STA, if the method does not exist, then (as per the ACPI
441 * specification), the returned CurrentStatus flags will indicate
442 * that the device is present/functional/enabled. Otherwise, the
443 * CurrentStatus flags reflect the value returned from _STA.
446 /* Execute the Device._STA method */
448 Status = AcpiUtExecute_STA (Node, &Info->CurrentStatus);
449 if (ACPI_SUCCESS (Status))
451 Valid |= ACPI_VALID_STA;
454 /* Execute the Device._ADR method */
456 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR, Node,
457 &Info->Address);
458 if (ACPI_SUCCESS (Status))
460 Valid |= ACPI_VALID_ADR;
463 /* Execute the Device._SxW methods */
465 Status = AcpiUtExecutePowerMethods (Node,
466 AcpiGbl_LowestDstateNames, ACPI_NUM_SxW_METHODS,
467 Info->LowestDstates);
468 if (ACPI_SUCCESS (Status))
470 Valid |= ACPI_VALID_SXWS;
473 /* Execute the Device._SxD methods */
475 Status = AcpiUtExecutePowerMethods (Node,
476 AcpiGbl_HighestDstateNames, ACPI_NUM_SxD_METHODS,
477 Info->HighestDstates);
478 if (ACPI_SUCCESS (Status))
480 Valid |= ACPI_VALID_SXDS;
485 * Create a pointer to the string area of the return buffer.
486 * Point to the end of the base ACPI_DEVICE_INFO structure.
488 NextIdString = ACPI_CAST_PTR (char, Info->CompatibleIdList.Ids);
489 if (CidList)
491 /* Point past the CID PNP_DEVICE_ID array */
493 NextIdString += ((ACPI_SIZE) CidList->Count * sizeof (ACPI_PNP_DEVICE_ID));
497 * Copy the HID, UID, and CIDs to the return buffer. The variable-length
498 * strings are copied to the reserved area at the end of the buffer.
500 * For HID and CID, check if the ID is a PCI Root Bridge.
502 if (Hid)
504 NextIdString = AcpiNsCopyDeviceId (&Info->HardwareId,
505 Hid, NextIdString);
507 if (AcpiUtIsPciRootBridge (Hid->String))
509 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
513 if (Uid)
515 NextIdString = AcpiNsCopyDeviceId (&Info->UniqueId,
516 Uid, NextIdString);
519 if (CidList)
521 Info->CompatibleIdList.Count = CidList->Count;
522 Info->CompatibleIdList.ListSize = CidList->ListSize;
524 /* Copy each CID */
526 for (i = 0; i < CidList->Count; i++)
528 NextIdString = AcpiNsCopyDeviceId (&Info->CompatibleIdList.Ids[i],
529 &CidList->Ids[i], NextIdString);
531 if (AcpiUtIsPciRootBridge (CidList->Ids[i].String))
533 Info->Flags |= ACPI_PCI_ROOT_BRIDGE;
538 if (Cls)
540 NextIdString = AcpiNsCopyDeviceId (&Info->ClassCode,
541 Cls, NextIdString);
544 /* Copy the fixed-length data */
546 Info->InfoSize = InfoSize;
547 Info->Type = Type;
548 Info->Name = Name;
549 Info->ParamCount = ParamCount;
550 Info->Valid = Valid;
552 *ReturnBuffer = Info;
553 Status = AE_OK;
556 Cleanup:
557 if (Hid)
559 ACPI_FREE (Hid);
561 if (Uid)
563 ACPI_FREE (Uid);
565 if (CidList)
567 ACPI_FREE (CidList);
569 if (Cls)
571 ACPI_FREE (Cls);
573 return (Status);
576 ACPI_EXPORT_SYMBOL (AcpiGetObjectInfo)
579 /******************************************************************************
581 * FUNCTION: AcpiInstallMethod
583 * PARAMETERS: Buffer - An ACPI table containing one control method
585 * RETURN: Status
587 * DESCRIPTION: Install a control method into the namespace. If the method
588 * name already exists in the namespace, it is overwritten. The
589 * input buffer must contain a valid DSDT or SSDT containing a
590 * single control method.
592 ******************************************************************************/
594 ACPI_STATUS
595 AcpiInstallMethod (
596 UINT8 *Buffer)
598 ACPI_TABLE_HEADER *Table = ACPI_CAST_PTR (ACPI_TABLE_HEADER, Buffer);
599 UINT8 *AmlBuffer;
600 UINT8 *AmlStart;
601 char *Path;
602 ACPI_NAMESPACE_NODE *Node;
603 ACPI_OPERAND_OBJECT *MethodObj;
604 ACPI_PARSE_STATE ParserState;
605 UINT32 AmlLength;
606 UINT16 Opcode;
607 UINT8 MethodFlags;
608 ACPI_STATUS Status;
611 /* Parameter validation */
613 if (!Buffer)
615 return (AE_BAD_PARAMETER);
618 /* Table must be a DSDT or SSDT */
620 if (!ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) &&
621 !ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
623 return (AE_BAD_HEADER);
626 /* First AML opcode in the table must be a control method */
628 ParserState.Aml = Buffer + sizeof (ACPI_TABLE_HEADER);
629 Opcode = AcpiPsPeekOpcode (&ParserState);
630 if (Opcode != AML_METHOD_OP)
632 return (AE_BAD_PARAMETER);
635 /* Extract method information from the raw AML */
637 ParserState.Aml += AcpiPsGetOpcodeSize (Opcode);
638 ParserState.PkgEnd = AcpiPsGetNextPackageEnd (&ParserState);
639 Path = AcpiPsGetNextNamestring (&ParserState);
641 MethodFlags = *ParserState.Aml++;
642 AmlStart = ParserState.Aml;
643 AmlLength = ACPI_PTR_DIFF (ParserState.PkgEnd, AmlStart);
646 * Allocate resources up-front. We don't want to have to delete a new
647 * node from the namespace if we cannot allocate memory.
649 AmlBuffer = ACPI_ALLOCATE (AmlLength);
650 if (!AmlBuffer)
652 return (AE_NO_MEMORY);
655 MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
656 if (!MethodObj)
658 ACPI_FREE (AmlBuffer);
659 return (AE_NO_MEMORY);
662 /* Lock namespace for AcpiNsLookup, we may be creating a new node */
664 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
665 if (ACPI_FAILURE (Status))
667 goto ErrorExit;
670 /* The lookup either returns an existing node or creates a new one */
672 Status = AcpiNsLookup (NULL, Path, ACPI_TYPE_METHOD, ACPI_IMODE_LOAD_PASS1,
673 ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_ERROR_IF_FOUND, NULL, &Node);
675 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
677 if (ACPI_FAILURE (Status)) /* NsLookup */
679 if (Status != AE_ALREADY_EXISTS)
681 goto ErrorExit;
684 /* Node existed previously, make sure it is a method node */
686 if (Node->Type != ACPI_TYPE_METHOD)
688 Status = AE_TYPE;
689 goto ErrorExit;
693 /* Copy the method AML to the local buffer */
695 memcpy (AmlBuffer, AmlStart, AmlLength);
697 /* Initialize the method object with the new method's information */
699 MethodObj->Method.AmlStart = AmlBuffer;
700 MethodObj->Method.AmlLength = AmlLength;
702 MethodObj->Method.ParamCount = (UINT8)
703 (MethodFlags & AML_METHOD_ARG_COUNT);
705 if (MethodFlags & AML_METHOD_SERIALIZED)
707 MethodObj->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
709 MethodObj->Method.SyncLevel = (UINT8)
710 ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
714 * Now that it is complete, we can attach the new method object to
715 * the method Node (detaches/deletes any existing object)
717 Status = AcpiNsAttachObject (Node, MethodObj, ACPI_TYPE_METHOD);
720 * Flag indicates AML buffer is dynamic, must be deleted later.
721 * Must be set only after attach above.
723 Node->Flags |= ANOBJ_ALLOCATED_BUFFER;
725 /* Remove local reference to the method object */
727 AcpiUtRemoveReference (MethodObj);
728 return (Status);
731 ErrorExit:
733 ACPI_FREE (AmlBuffer);
734 ACPI_FREE (MethodObj);
735 return (Status);
738 ACPI_EXPORT_SYMBOL (AcpiInstallMethod)