1 /******************************************************************************
3 * Module Name: evregion - Operation Region support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
45 #define __EVREGION_C__
53 #define _COMPONENT ACPI_EVENTS
54 ACPI_MODULE_NAME ("evregion")
57 extern UINT8 AcpiGbl_DefaultAddressSpaces[];
59 /* Local prototypes */
62 AcpiEvOrphanEcRegMethod (
63 ACPI_NAMESPACE_NODE *EcDeviceNode);
67 ACPI_HANDLE ObjHandle,
73 /*******************************************************************************
75 * FUNCTION: AcpiEvInitializeOpRegions
81 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
82 * an installed default region handler.
84 ******************************************************************************/
87 AcpiEvInitializeOpRegions (
94 ACPI_FUNCTION_TRACE (EvInitializeOpRegions);
97 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
98 if (ACPI_FAILURE (Status))
100 return_ACPI_STATUS (Status);
103 /* Run the _REG methods for OpRegions in each default address space */
105 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
108 * Make sure the installed handler is the DEFAULT handler. If not the
109 * default, the _REG methods will have already been run (when the
110 * handler was installed)
112 if (AcpiEvHasDefaultHandler (AcpiGbl_RootNode,
113 AcpiGbl_DefaultAddressSpaces[i]))
115 Status = AcpiEvExecuteRegMethods (AcpiGbl_RootNode,
116 AcpiGbl_DefaultAddressSpaces[i]);
120 AcpiGbl_RegMethodsExecuted = TRUE;
122 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
123 return_ACPI_STATUS (Status);
127 /*******************************************************************************
129 * FUNCTION: AcpiEvAddressSpaceDispatch
131 * PARAMETERS: RegionObj - Internal region object
132 * FieldObj - Corresponding field. Can be NULL.
133 * Function - Read or Write operation
134 * RegionOffset - Where in the region to read or write
135 * BitWidth - Field width in bits (8, 16, 32, or 64)
136 * Value - Pointer to in or out value, must be
137 * a full 64-bit integer
141 * DESCRIPTION: Dispatch an address space or operation region access to
142 * a previously installed handler.
144 ******************************************************************************/
147 AcpiEvAddressSpaceDispatch (
148 ACPI_OPERAND_OBJECT *RegionObj,
149 ACPI_OPERAND_OBJECT *FieldObj,
156 ACPI_ADR_SPACE_HANDLER Handler;
157 ACPI_ADR_SPACE_SETUP RegionSetup;
158 ACPI_OPERAND_OBJECT *HandlerDesc;
159 ACPI_OPERAND_OBJECT *RegionObj2;
160 void *RegionContext = NULL;
161 ACPI_CONNECTION_INFO *Context;
164 ACPI_FUNCTION_TRACE (EvAddressSpaceDispatch);
167 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
170 return_ACPI_STATUS (AE_NOT_EXIST);
173 /* Ensure that there is a handler associated with this region */
175 HandlerDesc = RegionObj->Region.Handler;
178 ACPI_ERROR ((AE_INFO,
179 "No handler for Region [%4.4s] (%p) [%s]",
180 AcpiUtGetNodeName (RegionObj->Region.Node),
181 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
183 return_ACPI_STATUS (AE_NOT_EXIST);
186 Context = HandlerDesc->AddressSpace.Context;
189 * It may be the case that the region has never been initialized.
190 * Some types of regions require special init code
192 if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
194 /* This region has not been initialized yet, do it */
196 RegionSetup = HandlerDesc->AddressSpace.Setup;
199 /* No initialization routine, exit with error */
201 ACPI_ERROR ((AE_INFO,
202 "No init routine for region(%p) [%s]",
203 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
204 return_ACPI_STATUS (AE_NOT_EXIST);
208 * We must exit the interpreter because the region setup will
209 * potentially execute control methods (for example, the _REG method
212 AcpiExExitInterpreter ();
214 Status = RegionSetup (RegionObj, ACPI_REGION_ACTIVATE,
215 Context, &RegionContext);
217 /* Re-enter the interpreter */
219 AcpiExEnterInterpreter ();
221 /* Check for failure of the Region Setup */
223 if (ACPI_FAILURE (Status))
225 ACPI_EXCEPTION ((AE_INFO, Status,
226 "During region initialization: [%s]",
227 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
228 return_ACPI_STATUS (Status);
231 /* Region initialization may have been completed by RegionSetup */
233 if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
235 RegionObj->Region.Flags |= AOPOBJ_SETUP_COMPLETE;
238 * Save the returned context for use in all accesses to
239 * the handler for this particular region
241 if (!(RegionObj2->Extra.RegionContext))
243 RegionObj2->Extra.RegionContext = RegionContext;
248 /* We have everything we need, we can invoke the address space handler */
250 Handler = HandlerDesc->AddressSpace.Handler;
252 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
253 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
254 &RegionObj->Region.Handler->AddressSpace, Handler,
255 ACPI_FORMAT_NATIVE_UINT (RegionObj->Region.Address + RegionOffset),
256 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
259 * Special handling for GenericSerialBus and GeneralPurposeIo:
260 * There are three extra parameters that must be passed to the
261 * handler via the context:
262 * 1) Connection buffer, a resource template from Connection() op.
263 * 2) Length of the above buffer.
264 * 3) Actual access length from the AccessAs() op.
266 if (((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS) ||
267 (RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO)) &&
271 /* Get the Connection (ResourceTemplate) buffer */
273 Context->Connection = FieldObj->Field.ResourceBuffer;
274 Context->Length = FieldObj->Field.ResourceLength;
275 Context->AccessLength = FieldObj->Field.AccessLength;
278 if (!(HandlerDesc->AddressSpace.HandlerFlags &
279 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
282 * For handlers other than the default (supplied) handlers, we must
283 * exit the interpreter because the handler *might* block -- we don't
284 * know what it will do, so we can't hold the lock on the intepreter.
286 AcpiExExitInterpreter();
289 /* Call the handler */
291 Status = Handler (Function,
292 (RegionObj->Region.Address + RegionOffset), BitWidth, Value,
293 Context, RegionObj2->Extra.RegionContext);
295 if (ACPI_FAILURE (Status))
297 ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
298 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
301 if (!(HandlerDesc->AddressSpace.HandlerFlags &
302 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
305 * We just returned from a non-default handler, we must re-enter the
308 AcpiExEnterInterpreter ();
311 return_ACPI_STATUS (Status);
315 /*******************************************************************************
317 * FUNCTION: AcpiEvDetachRegion
319 * PARAMETERS: RegionObj - Region Object
320 * AcpiNsIsLocked - Namespace Region Already Locked?
324 * DESCRIPTION: Break the association between the handler and the region
325 * this is a two way association.
327 ******************************************************************************/
331 ACPI_OPERAND_OBJECT *RegionObj,
332 BOOLEAN AcpiNsIsLocked)
334 ACPI_OPERAND_OBJECT *HandlerObj;
335 ACPI_OPERAND_OBJECT *ObjDesc;
336 ACPI_OPERAND_OBJECT *StartDesc;
337 ACPI_OPERAND_OBJECT **LastObjPtr;
338 ACPI_ADR_SPACE_SETUP RegionSetup;
339 void **RegionContext;
340 ACPI_OPERAND_OBJECT *RegionObj2;
344 ACPI_FUNCTION_TRACE (EvDetachRegion);
347 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
352 RegionContext = &RegionObj2->Extra.RegionContext;
354 /* Get the address handler from the region object */
356 HandlerObj = RegionObj->Region.Handler;
359 /* This region has no handler, all done */
364 /* Find this region in the handler's list */
366 ObjDesc = HandlerObj->AddressSpace.RegionList;
368 LastObjPtr = &HandlerObj->AddressSpace.RegionList;
372 /* Is this the correct Region? */
374 if (ObjDesc == RegionObj)
376 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
377 "Removing Region %p from address handler %p\n",
378 RegionObj, HandlerObj));
380 /* This is it, remove it from the handler's list */
382 *LastObjPtr = ObjDesc->Region.Next;
383 ObjDesc->Region.Next = NULL; /* Must clear field */
387 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
388 if (ACPI_FAILURE (Status))
394 /* Now stop region accesses by executing the _REG method */
396 Status = AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_DISCONNECT);
397 if (ACPI_FAILURE (Status))
399 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
400 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
405 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
406 if (ACPI_FAILURE (Status))
413 * If the region has been activated, call the setup handler with
414 * the deactivate notification
416 if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
418 RegionSetup = HandlerObj->AddressSpace.Setup;
419 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
420 HandlerObj->AddressSpace.Context, RegionContext);
423 * RegionContext should have been released by the deactivate
424 * operation. We don't need access to it anymore here.
428 *RegionContext = NULL;
431 /* Init routine may fail, Just ignore errors */
433 if (ACPI_FAILURE (Status))
435 ACPI_EXCEPTION ((AE_INFO, Status,
436 "from region handler - deactivate, [%s]",
437 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
440 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
444 * Remove handler reference in the region
446 * NOTE: this doesn't mean that the region goes away, the region
447 * is just inaccessible as indicated to the _REG method
449 * If the region is on the handler's list, this must be the
452 RegionObj->Region.Handler = NULL;
453 AcpiUtRemoveReference (HandlerObj);
458 /* Walk the linked list of handlers */
460 LastObjPtr = &ObjDesc->Region.Next;
461 ObjDesc = ObjDesc->Region.Next;
463 /* Prevent infinite loop if list is corrupted */
465 if (ObjDesc == StartDesc)
467 ACPI_ERROR ((AE_INFO,
468 "Circular handler list in region object %p",
474 /* If we get here, the region was not in the handler's region list */
476 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
477 "Cannot remove region %p from address handler %p\n",
478 RegionObj, HandlerObj));
484 /*******************************************************************************
486 * FUNCTION: AcpiEvAttachRegion
488 * PARAMETERS: HandlerObj - Handler Object
489 * RegionObj - Region Object
490 * AcpiNsIsLocked - Namespace Region Already Locked?
494 * DESCRIPTION: Create the association between the handler and the region
495 * this is a two way association.
497 ******************************************************************************/
501 ACPI_OPERAND_OBJECT *HandlerObj,
502 ACPI_OPERAND_OBJECT *RegionObj,
503 BOOLEAN AcpiNsIsLocked)
506 ACPI_FUNCTION_TRACE (EvAttachRegion);
509 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
510 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
511 AcpiUtGetNodeName (RegionObj->Region.Node),
512 RegionObj, HandlerObj,
513 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
515 /* Link this region to the front of the handler's list */
517 RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
518 HandlerObj->AddressSpace.RegionList = RegionObj;
520 /* Install the region's handler */
522 if (RegionObj->Region.Handler)
524 return_ACPI_STATUS (AE_ALREADY_EXISTS);
527 RegionObj->Region.Handler = HandlerObj;
528 AcpiUtAddReference (HandlerObj);
530 return_ACPI_STATUS (AE_OK);
534 /*******************************************************************************
536 * FUNCTION: AcpiEvExecuteRegMethod
538 * PARAMETERS: RegionObj - Region object
539 * Function - Passed to _REG: On (1) or Off (0)
543 * DESCRIPTION: Execute _REG method for a region
545 ******************************************************************************/
548 AcpiEvExecuteRegMethod (
549 ACPI_OPERAND_OBJECT *RegionObj,
552 ACPI_EVALUATE_INFO *Info;
553 ACPI_OPERAND_OBJECT *Args[3];
554 ACPI_OPERAND_OBJECT *RegionObj2;
558 ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
561 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
564 return_ACPI_STATUS (AE_NOT_EXIST);
567 if (RegionObj2->Extra.Method_REG == NULL)
569 return_ACPI_STATUS (AE_OK);
572 /* Allocate and initialize the evaluation information block */
574 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
577 return_ACPI_STATUS (AE_NO_MEMORY);
580 Info->PrefixNode = RegionObj2->Extra.Method_REG;
581 Info->RelativePathname = NULL;
582 Info->Parameters = Args;
583 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
586 * The _REG method has two arguments:
589 * Operation region space ID Same value as RegionObj->Region.SpaceId
592 * connection status 1 for connecting the handler, 0 for disconnecting
593 * the handler (Passed as a parameter)
595 Args[0] = AcpiUtCreateIntegerObject ((UINT64) RegionObj->Region.SpaceId);
598 Status = AE_NO_MEMORY;
602 Args[1] = AcpiUtCreateIntegerObject ((UINT64) Function);
605 Status = AE_NO_MEMORY;
609 Args[2] = NULL; /* Terminate list */
611 /* Execute the method, no return value */
614 AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
616 Status = AcpiNsEvaluate (Info);
617 AcpiUtRemoveReference (Args[1]);
620 AcpiUtRemoveReference (Args[0]);
624 return_ACPI_STATUS (Status);
628 /*******************************************************************************
630 * FUNCTION: AcpiEvExecuteRegMethods
632 * PARAMETERS: Node - Namespace node for the device
633 * SpaceId - The address space ID
637 * DESCRIPTION: Run all _REG methods for the input Space ID;
638 * Note: assumes namespace is locked, or system init time.
640 ******************************************************************************/
643 AcpiEvExecuteRegMethods (
644 ACPI_NAMESPACE_NODE *Node,
645 ACPI_ADR_SPACE_TYPE SpaceId)
650 ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
654 * Run all _REG methods for all Operation Regions for this space ID. This
655 * is a separate walk in order to handle any interdependencies between
656 * regions and _REG methods. (i.e. handlers must be installed for all
657 * regions of this Space ID before we can run any _REG methods)
659 Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
660 ACPI_NS_WALK_UNLOCK, AcpiEvRegRun, NULL,
663 /* Special case for EC: handle "orphan" _REG methods with no region */
665 if (SpaceId == ACPI_ADR_SPACE_EC)
667 AcpiEvOrphanEcRegMethod (Node);
670 return_ACPI_STATUS (Status);
674 /*******************************************************************************
676 * FUNCTION: AcpiEvRegRun
678 * PARAMETERS: WalkNamespace callback
680 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
682 ******************************************************************************/
686 ACPI_HANDLE ObjHandle,
691 ACPI_OPERAND_OBJECT *ObjDesc;
692 ACPI_NAMESPACE_NODE *Node;
693 ACPI_ADR_SPACE_TYPE SpaceId;
697 SpaceId = *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE, Context);
699 /* Convert and validate the device handle */
701 Node = AcpiNsValidateHandle (ObjHandle);
704 return (AE_BAD_PARAMETER);
708 * We only care about regions.and objects that are allowed to have address
711 if ((Node->Type != ACPI_TYPE_REGION) &&
712 (Node != AcpiGbl_RootNode))
717 /* Check for an existing internal object */
719 ObjDesc = AcpiNsGetAttachedObject (Node);
722 /* No object, just exit */
727 /* Object is a Region */
729 if (ObjDesc->Region.SpaceId != SpaceId)
731 /* This region is for a different address space, just ignore it */
736 Status = AcpiEvExecuteRegMethod (ObjDesc, ACPI_REG_CONNECT);
741 /*******************************************************************************
743 * FUNCTION: AcpiEvOrphanEcRegMethod
745 * PARAMETERS: EcDeviceNode - Namespace node for an EC device
749 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
750 * device. This is a _REG method that has no corresponding region
751 * within the EC device scope. The orphan _REG method appears to
752 * have been enabled by the description of the ECDT in the ACPI
753 * specification: "The availability of the region space can be
754 * detected by providing a _REG method object underneath the
755 * Embedded Controller device."
757 * To quickly access the EC device, we use the EcDeviceNode used
758 * during EC handler installation. Otherwise, we would need to
759 * perform a time consuming namespace walk, executing _HID
760 * methods to find the EC device.
762 * MUTEX: Assumes the namespace is locked
764 ******************************************************************************/
767 AcpiEvOrphanEcRegMethod (
768 ACPI_NAMESPACE_NODE *EcDeviceNode)
770 ACPI_HANDLE RegMethod;
771 ACPI_NAMESPACE_NODE *NextNode;
773 ACPI_OBJECT_LIST Args;
774 ACPI_OBJECT Objects[2];
777 ACPI_FUNCTION_TRACE (EvOrphanEcRegMethod);
785 /* Namespace is currently locked, must release */
787 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
789 /* Get a handle to a _REG method immediately under the EC device */
791 Status = AcpiGetHandle (EcDeviceNode, METHOD_NAME__REG, &RegMethod);
792 if (ACPI_FAILURE (Status))
794 goto Exit; /* There is no _REG method present */
798 * Execute the _REG method only if there is no Operation Region in
799 * this scope with the Embedded Controller space ID. Otherwise, it
800 * will already have been executed. Note, this allows for Regions
801 * with other space IDs to be present; but the code below will then
802 * execute the _REG method with the EmbeddedControl SpaceID argument.
804 NextNode = AcpiNsGetNextNode (EcDeviceNode, NULL);
807 if ((NextNode->Type == ACPI_TYPE_REGION) &&
808 (NextNode->Object) &&
809 (NextNode->Object->Region.SpaceId == ACPI_ADR_SPACE_EC))
811 goto Exit; /* Do not execute the _REG */
814 NextNode = AcpiNsGetNextNode (EcDeviceNode, NextNode);
817 /* Evaluate the _REG(EmbeddedControl,Connect) method */
820 Args.Pointer = Objects;
821 Objects[0].Type = ACPI_TYPE_INTEGER;
822 Objects[0].Integer.Value = ACPI_ADR_SPACE_EC;
823 Objects[1].Type = ACPI_TYPE_INTEGER;
824 Objects[1].Integer.Value = ACPI_REG_CONNECT;
826 Status = AcpiEvaluateObject (RegMethod, NULL, &Args, NULL);
829 /* We ignore all errors from above, don't care */
831 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);