1 /******************************************************************************
3 * Module Name: evregion - Operation Region support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acevents.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
48 #include <contrib/dev/acpica/include/acinterp.h>
50 #define _COMPONENT ACPI_EVENTS
51 ACPI_MODULE_NAME ("evregion")
54 extern UINT8 AcpiGbl_DefaultAddressSpaces[];
56 /* Local prototypes */
59 AcpiEvOrphanEcRegMethod (
60 ACPI_NAMESPACE_NODE *EcDeviceNode);
64 ACPI_HANDLE ObjHandle,
70 /*******************************************************************************
72 * FUNCTION: AcpiEvInitializeOpRegions
78 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
79 * an installed default region handler.
81 ******************************************************************************/
84 AcpiEvInitializeOpRegions (
91 ACPI_FUNCTION_TRACE (EvInitializeOpRegions);
94 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
95 if (ACPI_FAILURE (Status))
97 return_ACPI_STATUS (Status);
100 /* Run the _REG methods for OpRegions in each default address space */
102 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
105 * Make sure the installed handler is the DEFAULT handler. If not the
106 * default, the _REG methods will have already been run (when the
107 * handler was installed)
109 if (AcpiEvHasDefaultHandler (AcpiGbl_RootNode,
110 AcpiGbl_DefaultAddressSpaces[i]))
112 Status = AcpiEvExecuteRegMethods (AcpiGbl_RootNode,
113 AcpiGbl_DefaultAddressSpaces[i]);
117 AcpiGbl_RegMethodsExecuted = TRUE;
119 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
120 return_ACPI_STATUS (Status);
124 /*******************************************************************************
126 * FUNCTION: AcpiEvAddressSpaceDispatch
128 * PARAMETERS: RegionObj - Internal region object
129 * FieldObj - Corresponding field. Can be NULL.
130 * Function - Read or Write operation
131 * RegionOffset - Where in the region to read or write
132 * BitWidth - Field width in bits (8, 16, 32, or 64)
133 * Value - Pointer to in or out value, must be
134 * a full 64-bit integer
138 * DESCRIPTION: Dispatch an address space or operation region access to
139 * a previously installed handler.
141 ******************************************************************************/
144 AcpiEvAddressSpaceDispatch (
145 ACPI_OPERAND_OBJECT *RegionObj,
146 ACPI_OPERAND_OBJECT *FieldObj,
153 ACPI_ADR_SPACE_HANDLER Handler;
154 ACPI_ADR_SPACE_SETUP RegionSetup;
155 ACPI_OPERAND_OBJECT *HandlerDesc;
156 ACPI_OPERAND_OBJECT *RegionObj2;
157 void *RegionContext = NULL;
158 ACPI_CONNECTION_INFO *Context;
159 ACPI_PHYSICAL_ADDRESS Address;
162 ACPI_FUNCTION_TRACE (EvAddressSpaceDispatch);
165 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
168 return_ACPI_STATUS (AE_NOT_EXIST);
171 /* Ensure that there is a handler associated with this region */
173 HandlerDesc = RegionObj->Region.Handler;
176 ACPI_ERROR ((AE_INFO,
177 "No handler for Region [%4.4s] (%p) [%s]",
178 AcpiUtGetNodeName (RegionObj->Region.Node),
179 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
181 return_ACPI_STATUS (AE_NOT_EXIST);
184 Context = HandlerDesc->AddressSpace.Context;
187 * It may be the case that the region has never been initialized.
188 * Some types of regions require special init code
190 if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
192 /* This region has not been initialized yet, do it */
194 RegionSetup = HandlerDesc->AddressSpace.Setup;
197 /* No initialization routine, exit with error */
199 ACPI_ERROR ((AE_INFO,
200 "No init routine for region(%p) [%s]",
201 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
202 return_ACPI_STATUS (AE_NOT_EXIST);
206 * We must exit the interpreter because the region setup will
207 * potentially execute control methods (for example, the _REG method
210 AcpiExExitInterpreter ();
212 Status = RegionSetup (RegionObj, ACPI_REGION_ACTIVATE,
213 Context, &RegionContext);
215 /* Re-enter the interpreter */
217 AcpiExEnterInterpreter ();
219 /* Check for failure of the Region Setup */
221 if (ACPI_FAILURE (Status))
223 ACPI_EXCEPTION ((AE_INFO, Status,
224 "During region initialization: [%s]",
225 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
226 return_ACPI_STATUS (Status);
229 /* Region initialization may have been completed by RegionSetup */
231 if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
233 RegionObj->Region.Flags |= AOPOBJ_SETUP_COMPLETE;
236 * Save the returned context for use in all accesses to
237 * the handler for this particular region
239 if (!(RegionObj2->Extra.RegionContext))
241 RegionObj2->Extra.RegionContext = RegionContext;
246 /* We have everything we need, we can invoke the address space handler */
248 Handler = HandlerDesc->AddressSpace.Handler;
249 Address = (RegionObj->Region.Address + RegionOffset);
252 * Special handling for GenericSerialBus and GeneralPurposeIo:
253 * There are three extra parameters that must be passed to the
254 * handler via the context:
255 * 1) Connection buffer, a resource template from Connection() op
256 * 2) Length of the above buffer
257 * 3) Actual access length from the AccessAs() op
259 * In addition, for GeneralPurposeIo, the Address and BitWidth fields
260 * are defined as follows:
261 * 1) Address is the pin number index of the field (bit offset from
262 * the previous Connection)
263 * 2) BitWidth is the actual bit length of the field (number of pins)
265 if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS) &&
269 /* Get the Connection (ResourceTemplate) buffer */
271 Context->Connection = FieldObj->Field.ResourceBuffer;
272 Context->Length = FieldObj->Field.ResourceLength;
273 Context->AccessLength = FieldObj->Field.AccessLength;
275 if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO) &&
279 /* Get the Connection (ResourceTemplate) buffer */
281 Context->Connection = FieldObj->Field.ResourceBuffer;
282 Context->Length = FieldObj->Field.ResourceLength;
283 Context->AccessLength = FieldObj->Field.AccessLength;
284 Address = FieldObj->Field.PinNumberIndex;
285 BitWidth = FieldObj->Field.BitLength;
288 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
289 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
290 &RegionObj->Region.Handler->AddressSpace, Handler,
291 ACPI_FORMAT_UINT64 (Address),
292 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
294 if (!(HandlerDesc->AddressSpace.HandlerFlags &
295 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
298 * For handlers other than the default (supplied) handlers, we must
299 * exit the interpreter because the handler *might* block -- we don't
300 * know what it will do, so we can't hold the lock on the intepreter.
302 AcpiExExitInterpreter();
305 /* Call the handler */
307 Status = Handler (Function, Address, BitWidth, Value, Context,
308 RegionObj2->Extra.RegionContext);
310 if (ACPI_FAILURE (Status))
312 ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
313 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
316 if (!(HandlerDesc->AddressSpace.HandlerFlags &
317 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
320 * We just returned from a non-default handler, we must re-enter the
323 AcpiExEnterInterpreter ();
326 return_ACPI_STATUS (Status);
330 /*******************************************************************************
332 * FUNCTION: AcpiEvDetachRegion
334 * PARAMETERS: RegionObj - Region Object
335 * AcpiNsIsLocked - Namespace Region Already Locked?
339 * DESCRIPTION: Break the association between the handler and the region
340 * this is a two way association.
342 ******************************************************************************/
346 ACPI_OPERAND_OBJECT *RegionObj,
347 BOOLEAN AcpiNsIsLocked)
349 ACPI_OPERAND_OBJECT *HandlerObj;
350 ACPI_OPERAND_OBJECT *ObjDesc;
351 ACPI_OPERAND_OBJECT *StartDesc;
352 ACPI_OPERAND_OBJECT **LastObjPtr;
353 ACPI_ADR_SPACE_SETUP RegionSetup;
354 void **RegionContext;
355 ACPI_OPERAND_OBJECT *RegionObj2;
359 ACPI_FUNCTION_TRACE (EvDetachRegion);
362 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
367 RegionContext = &RegionObj2->Extra.RegionContext;
369 /* Get the address handler from the region object */
371 HandlerObj = RegionObj->Region.Handler;
374 /* This region has no handler, all done */
379 /* Find this region in the handler's list */
381 ObjDesc = HandlerObj->AddressSpace.RegionList;
383 LastObjPtr = &HandlerObj->AddressSpace.RegionList;
387 /* Is this the correct Region? */
389 if (ObjDesc == RegionObj)
391 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
392 "Removing Region %p from address handler %p\n",
393 RegionObj, HandlerObj));
395 /* This is it, remove it from the handler's list */
397 *LastObjPtr = ObjDesc->Region.Next;
398 ObjDesc->Region.Next = NULL; /* Must clear field */
402 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
403 if (ACPI_FAILURE (Status))
409 /* Now stop region accesses by executing the _REG method */
411 Status = AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_DISCONNECT);
412 if (ACPI_FAILURE (Status))
414 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
415 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
420 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
421 if (ACPI_FAILURE (Status))
428 * If the region has been activated, call the setup handler with
429 * the deactivate notification
431 if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
433 RegionSetup = HandlerObj->AddressSpace.Setup;
434 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
435 HandlerObj->AddressSpace.Context, RegionContext);
438 * RegionContext should have been released by the deactivate
439 * operation. We don't need access to it anymore here.
443 *RegionContext = NULL;
446 /* Init routine may fail, Just ignore errors */
448 if (ACPI_FAILURE (Status))
450 ACPI_EXCEPTION ((AE_INFO, Status,
451 "from region handler - deactivate, [%s]",
452 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
455 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
459 * Remove handler reference in the region
461 * NOTE: this doesn't mean that the region goes away, the region
462 * is just inaccessible as indicated to the _REG method
464 * If the region is on the handler's list, this must be the
467 RegionObj->Region.Handler = NULL;
468 AcpiUtRemoveReference (HandlerObj);
473 /* Walk the linked list of handlers */
475 LastObjPtr = &ObjDesc->Region.Next;
476 ObjDesc = ObjDesc->Region.Next;
478 /* Prevent infinite loop if list is corrupted */
480 if (ObjDesc == StartDesc)
482 ACPI_ERROR ((AE_INFO,
483 "Circular handler list in region object %p",
489 /* If we get here, the region was not in the handler's region list */
491 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
492 "Cannot remove region %p from address handler %p\n",
493 RegionObj, HandlerObj));
499 /*******************************************************************************
501 * FUNCTION: AcpiEvAttachRegion
503 * PARAMETERS: HandlerObj - Handler Object
504 * RegionObj - Region Object
505 * AcpiNsIsLocked - Namespace Region Already Locked?
509 * DESCRIPTION: Create the association between the handler and the region
510 * this is a two way association.
512 ******************************************************************************/
516 ACPI_OPERAND_OBJECT *HandlerObj,
517 ACPI_OPERAND_OBJECT *RegionObj,
518 BOOLEAN AcpiNsIsLocked)
521 ACPI_FUNCTION_TRACE (EvAttachRegion);
524 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
525 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
526 AcpiUtGetNodeName (RegionObj->Region.Node),
527 RegionObj, HandlerObj,
528 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
530 /* Link this region to the front of the handler's list */
532 RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
533 HandlerObj->AddressSpace.RegionList = RegionObj;
535 /* Install the region's handler */
537 if (RegionObj->Region.Handler)
539 return_ACPI_STATUS (AE_ALREADY_EXISTS);
542 RegionObj->Region.Handler = HandlerObj;
543 AcpiUtAddReference (HandlerObj);
545 return_ACPI_STATUS (AE_OK);
549 /*******************************************************************************
551 * FUNCTION: AcpiEvExecuteRegMethod
553 * PARAMETERS: RegionObj - Region object
554 * Function - Passed to _REG: On (1) or Off (0)
558 * DESCRIPTION: Execute _REG method for a region
560 ******************************************************************************/
563 AcpiEvExecuteRegMethod (
564 ACPI_OPERAND_OBJECT *RegionObj,
567 ACPI_EVALUATE_INFO *Info;
568 ACPI_OPERAND_OBJECT *Args[3];
569 ACPI_OPERAND_OBJECT *RegionObj2;
573 ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
576 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
579 return_ACPI_STATUS (AE_NOT_EXIST);
582 if (RegionObj2->Extra.Method_REG == NULL)
584 return_ACPI_STATUS (AE_OK);
587 /* Allocate and initialize the evaluation information block */
589 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
592 return_ACPI_STATUS (AE_NO_MEMORY);
595 Info->PrefixNode = RegionObj2->Extra.Method_REG;
596 Info->RelativePathname = NULL;
597 Info->Parameters = Args;
598 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
601 * The _REG method has two arguments:
604 * Operation region space ID Same value as RegionObj->Region.SpaceId
607 * connection status 1 for connecting the handler, 0 for disconnecting
608 * the handler (Passed as a parameter)
610 Args[0] = AcpiUtCreateIntegerObject ((UINT64) RegionObj->Region.SpaceId);
613 Status = AE_NO_MEMORY;
617 Args[1] = AcpiUtCreateIntegerObject ((UINT64) Function);
620 Status = AE_NO_MEMORY;
624 Args[2] = NULL; /* Terminate list */
626 /* Execute the method, no return value */
629 AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
631 Status = AcpiNsEvaluate (Info);
632 AcpiUtRemoveReference (Args[1]);
635 AcpiUtRemoveReference (Args[0]);
639 return_ACPI_STATUS (Status);
643 /*******************************************************************************
645 * FUNCTION: AcpiEvExecuteRegMethods
647 * PARAMETERS: Node - Namespace node for the device
648 * SpaceId - The address space ID
652 * DESCRIPTION: Run all _REG methods for the input Space ID;
653 * Note: assumes namespace is locked, or system init time.
655 ******************************************************************************/
658 AcpiEvExecuteRegMethods (
659 ACPI_NAMESPACE_NODE *Node,
660 ACPI_ADR_SPACE_TYPE SpaceId)
665 ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
669 * Run all _REG methods for all Operation Regions for this space ID. This
670 * is a separate walk in order to handle any interdependencies between
671 * regions and _REG methods. (i.e. handlers must be installed for all
672 * regions of this Space ID before we can run any _REG methods)
674 Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
675 ACPI_NS_WALK_UNLOCK, AcpiEvRegRun, NULL,
678 /* Special case for EC: handle "orphan" _REG methods with no region */
680 if (SpaceId == ACPI_ADR_SPACE_EC)
682 AcpiEvOrphanEcRegMethod (Node);
685 return_ACPI_STATUS (Status);
689 /*******************************************************************************
691 * FUNCTION: AcpiEvRegRun
693 * PARAMETERS: WalkNamespace callback
695 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
697 ******************************************************************************/
701 ACPI_HANDLE ObjHandle,
706 ACPI_OPERAND_OBJECT *ObjDesc;
707 ACPI_NAMESPACE_NODE *Node;
708 ACPI_ADR_SPACE_TYPE SpaceId;
712 SpaceId = *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE, Context);
714 /* Convert and validate the device handle */
716 Node = AcpiNsValidateHandle (ObjHandle);
719 return (AE_BAD_PARAMETER);
723 * We only care about regions.and objects that are allowed to have address
726 if ((Node->Type != ACPI_TYPE_REGION) &&
727 (Node != AcpiGbl_RootNode))
732 /* Check for an existing internal object */
734 ObjDesc = AcpiNsGetAttachedObject (Node);
737 /* No object, just exit */
742 /* Object is a Region */
744 if (ObjDesc->Region.SpaceId != SpaceId)
746 /* This region is for a different address space, just ignore it */
751 Status = AcpiEvExecuteRegMethod (ObjDesc, ACPI_REG_CONNECT);
756 /*******************************************************************************
758 * FUNCTION: AcpiEvOrphanEcRegMethod
760 * PARAMETERS: EcDeviceNode - Namespace node for an EC device
764 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
765 * device. This is a _REG method that has no corresponding region
766 * within the EC device scope. The orphan _REG method appears to
767 * have been enabled by the description of the ECDT in the ACPI
768 * specification: "The availability of the region space can be
769 * detected by providing a _REG method object underneath the
770 * Embedded Controller device."
772 * To quickly access the EC device, we use the EcDeviceNode used
773 * during EC handler installation. Otherwise, we would need to
774 * perform a time consuming namespace walk, executing _HID
775 * methods to find the EC device.
777 * MUTEX: Assumes the namespace is locked
779 ******************************************************************************/
782 AcpiEvOrphanEcRegMethod (
783 ACPI_NAMESPACE_NODE *EcDeviceNode)
785 ACPI_HANDLE RegMethod;
786 ACPI_NAMESPACE_NODE *NextNode;
788 ACPI_OBJECT_LIST Args;
789 ACPI_OBJECT Objects[2];
792 ACPI_FUNCTION_TRACE (EvOrphanEcRegMethod);
800 /* Namespace is currently locked, must release */
802 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
804 /* Get a handle to a _REG method immediately under the EC device */
806 Status = AcpiGetHandle (EcDeviceNode, METHOD_NAME__REG, &RegMethod);
807 if (ACPI_FAILURE (Status))
809 goto Exit; /* There is no _REG method present */
813 * Execute the _REG method only if there is no Operation Region in
814 * this scope with the Embedded Controller space ID. Otherwise, it
815 * will already have been executed. Note, this allows for Regions
816 * with other space IDs to be present; but the code below will then
817 * execute the _REG method with the EmbeddedControl SpaceID argument.
819 NextNode = AcpiNsGetNextNode (EcDeviceNode, NULL);
822 if ((NextNode->Type == ACPI_TYPE_REGION) &&
823 (NextNode->Object) &&
824 (NextNode->Object->Region.SpaceId == ACPI_ADR_SPACE_EC))
826 goto Exit; /* Do not execute the _REG */
829 NextNode = AcpiNsGetNextNode (EcDeviceNode, NextNode);
832 /* Evaluate the _REG(EmbeddedControl,Connect) method */
835 Args.Pointer = Objects;
836 Objects[0].Type = ACPI_TYPE_INTEGER;
837 Objects[0].Integer.Value = ACPI_ADR_SPACE_EC;
838 Objects[1].Type = ACPI_TYPE_INTEGER;
839 Objects[1].Integer.Value = ACPI_REG_CONNECT;
841 Status = AcpiEvaluateObject (RegMethod, NULL, &Args, NULL);
844 /* We ignore all errors from above, don't care */
846 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);