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.
44 #define __EVREGION_C__
52 #define _COMPONENT ACPI_EVENTS
53 ACPI_MODULE_NAME ("evregion")
56 extern UINT8 AcpiGbl_DefaultAddressSpaces[];
58 /* Local prototypes */
61 AcpiEvOrphanEcRegMethod (
62 ACPI_NAMESPACE_NODE *EcDeviceNode);
66 ACPI_HANDLE ObjHandle,
72 /*******************************************************************************
74 * FUNCTION: AcpiEvInitializeOpRegions
80 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
81 * an installed default region handler.
83 ******************************************************************************/
86 AcpiEvInitializeOpRegions (
93 ACPI_FUNCTION_TRACE (EvInitializeOpRegions);
96 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
97 if (ACPI_FAILURE (Status))
99 return_ACPI_STATUS (Status);
102 /* Run the _REG methods for OpRegions in each default address space */
104 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
107 * Make sure the installed handler is the DEFAULT handler. If not the
108 * default, the _REG methods will have already been run (when the
109 * handler was installed)
111 if (AcpiEvHasDefaultHandler (AcpiGbl_RootNode,
112 AcpiGbl_DefaultAddressSpaces[i]))
114 Status = AcpiEvExecuteRegMethods (AcpiGbl_RootNode,
115 AcpiGbl_DefaultAddressSpaces[i]);
119 AcpiGbl_RegMethodsExecuted = TRUE;
121 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
122 return_ACPI_STATUS (Status);
126 /*******************************************************************************
128 * FUNCTION: AcpiEvAddressSpaceDispatch
130 * PARAMETERS: RegionObj - Internal region object
131 * FieldObj - Corresponding field. Can be NULL.
132 * Function - Read or Write operation
133 * RegionOffset - Where in the region to read or write
134 * BitWidth - Field width in bits (8, 16, 32, or 64)
135 * Value - Pointer to in or out value, must be
136 * a full 64-bit integer
140 * DESCRIPTION: Dispatch an address space or operation region access to
141 * a previously installed handler.
143 ******************************************************************************/
146 AcpiEvAddressSpaceDispatch (
147 ACPI_OPERAND_OBJECT *RegionObj,
148 ACPI_OPERAND_OBJECT *FieldObj,
155 ACPI_ADR_SPACE_HANDLER Handler;
156 ACPI_ADR_SPACE_SETUP RegionSetup;
157 ACPI_OPERAND_OBJECT *HandlerDesc;
158 ACPI_OPERAND_OBJECT *RegionObj2;
159 void *RegionContext = NULL;
160 ACPI_CONNECTION_INFO *Context;
163 ACPI_FUNCTION_TRACE (EvAddressSpaceDispatch);
166 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
169 return_ACPI_STATUS (AE_NOT_EXIST);
172 /* Ensure that there is a handler associated with this region */
174 HandlerDesc = RegionObj->Region.Handler;
177 ACPI_ERROR ((AE_INFO,
178 "No handler for Region [%4.4s] (%p) [%s]",
179 AcpiUtGetNodeName (RegionObj->Region.Node),
180 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
182 return_ACPI_STATUS (AE_NOT_EXIST);
185 Context = HandlerDesc->AddressSpace.Context;
188 * It may be the case that the region has never been initialized.
189 * Some types of regions require special init code
191 if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
193 /* This region has not been initialized yet, do it */
195 RegionSetup = HandlerDesc->AddressSpace.Setup;
198 /* No initialization routine, exit with error */
200 ACPI_ERROR ((AE_INFO,
201 "No init routine for region(%p) [%s]",
202 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
203 return_ACPI_STATUS (AE_NOT_EXIST);
207 * We must exit the interpreter because the region setup will
208 * potentially execute control methods (for example, the _REG method
211 AcpiExExitInterpreter ();
213 Status = RegionSetup (RegionObj, ACPI_REGION_ACTIVATE,
214 Context, &RegionContext);
216 /* Re-enter the interpreter */
218 AcpiExEnterInterpreter ();
220 /* Check for failure of the Region Setup */
222 if (ACPI_FAILURE (Status))
224 ACPI_EXCEPTION ((AE_INFO, Status,
225 "During region initialization: [%s]",
226 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
227 return_ACPI_STATUS (Status);
230 /* Region initialization may have been completed by RegionSetup */
232 if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
234 RegionObj->Region.Flags |= AOPOBJ_SETUP_COMPLETE;
237 * Save the returned context for use in all accesses to
238 * the handler for this particular region
240 if (!(RegionObj2->Extra.RegionContext))
242 RegionObj2->Extra.RegionContext = RegionContext;
247 /* We have everything we need, we can invoke the address space handler */
249 Handler = HandlerDesc->AddressSpace.Handler;
251 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
252 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
253 &RegionObj->Region.Handler->AddressSpace, Handler,
254 ACPI_FORMAT_NATIVE_UINT (RegionObj->Region.Address + RegionOffset),
255 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
258 * Special handling for GenericSerialBus and GeneralPurposeIo:
259 * There are three extra parameters that must be passed to the
260 * handler via the context:
261 * 1) Connection buffer, a resource template from Connection() op.
262 * 2) Length of the above buffer.
263 * 3) Actual access length from the AccessAs() op.
265 if (((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS) ||
266 (RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO)) &&
270 /* Get the Connection (ResourceTemplate) buffer */
272 Context->Connection = FieldObj->Field.ResourceBuffer;
273 Context->Length = FieldObj->Field.ResourceLength;
274 Context->AccessLength = FieldObj->Field.AccessLength;
277 if (!(HandlerDesc->AddressSpace.HandlerFlags &
278 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
281 * For handlers other than the default (supplied) handlers, we must
282 * exit the interpreter because the handler *might* block -- we don't
283 * know what it will do, so we can't hold the lock on the intepreter.
285 AcpiExExitInterpreter();
288 /* Call the handler */
290 Status = Handler (Function,
291 (RegionObj->Region.Address + RegionOffset), BitWidth, Value,
292 Context, RegionObj2->Extra.RegionContext);
294 if (ACPI_FAILURE (Status))
296 ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
297 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
300 if (!(HandlerDesc->AddressSpace.HandlerFlags &
301 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
304 * We just returned from a non-default handler, we must re-enter the
307 AcpiExEnterInterpreter ();
310 return_ACPI_STATUS (Status);
314 /*******************************************************************************
316 * FUNCTION: AcpiEvDetachRegion
318 * PARAMETERS: RegionObj - Region Object
319 * AcpiNsIsLocked - Namespace Region Already Locked?
323 * DESCRIPTION: Break the association between the handler and the region
324 * this is a two way association.
326 ******************************************************************************/
330 ACPI_OPERAND_OBJECT *RegionObj,
331 BOOLEAN AcpiNsIsLocked)
333 ACPI_OPERAND_OBJECT *HandlerObj;
334 ACPI_OPERAND_OBJECT *ObjDesc;
335 ACPI_OPERAND_OBJECT *StartDesc;
336 ACPI_OPERAND_OBJECT **LastObjPtr;
337 ACPI_ADR_SPACE_SETUP RegionSetup;
338 void **RegionContext;
339 ACPI_OPERAND_OBJECT *RegionObj2;
343 ACPI_FUNCTION_TRACE (EvDetachRegion);
346 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
351 RegionContext = &RegionObj2->Extra.RegionContext;
353 /* Get the address handler from the region object */
355 HandlerObj = RegionObj->Region.Handler;
358 /* This region has no handler, all done */
363 /* Find this region in the handler's list */
365 ObjDesc = HandlerObj->AddressSpace.RegionList;
367 LastObjPtr = &HandlerObj->AddressSpace.RegionList;
371 /* Is this the correct Region? */
373 if (ObjDesc == RegionObj)
375 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
376 "Removing Region %p from address handler %p\n",
377 RegionObj, HandlerObj));
379 /* This is it, remove it from the handler's list */
381 *LastObjPtr = ObjDesc->Region.Next;
382 ObjDesc->Region.Next = NULL; /* Must clear field */
386 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
387 if (ACPI_FAILURE (Status))
393 /* Now stop region accesses by executing the _REG method */
395 Status = AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_DISCONNECT);
396 if (ACPI_FAILURE (Status))
398 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
399 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
404 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
405 if (ACPI_FAILURE (Status))
412 * If the region has been activated, call the setup handler with
413 * the deactivate notification
415 if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
417 RegionSetup = HandlerObj->AddressSpace.Setup;
418 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
419 HandlerObj->AddressSpace.Context, RegionContext);
422 * RegionContext should have been released by the deactivate
423 * operation. We don't need access to it anymore here.
427 *RegionContext = NULL;
430 /* Init routine may fail, Just ignore errors */
432 if (ACPI_FAILURE (Status))
434 ACPI_EXCEPTION ((AE_INFO, Status,
435 "from region handler - deactivate, [%s]",
436 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
439 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
443 * Remove handler reference in the region
445 * NOTE: this doesn't mean that the region goes away, the region
446 * is just inaccessible as indicated to the _REG method
448 * If the region is on the handler's list, this must be the
451 RegionObj->Region.Handler = NULL;
452 AcpiUtRemoveReference (HandlerObj);
457 /* Walk the linked list of handlers */
459 LastObjPtr = &ObjDesc->Region.Next;
460 ObjDesc = ObjDesc->Region.Next;
462 /* Prevent infinite loop if list is corrupted */
464 if (ObjDesc == StartDesc)
466 ACPI_ERROR ((AE_INFO,
467 "Circular handler list in region object %p",
473 /* If we get here, the region was not in the handler's region list */
475 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
476 "Cannot remove region %p from address handler %p\n",
477 RegionObj, HandlerObj));
483 /*******************************************************************************
485 * FUNCTION: AcpiEvAttachRegion
487 * PARAMETERS: HandlerObj - Handler Object
488 * RegionObj - Region Object
489 * AcpiNsIsLocked - Namespace Region Already Locked?
493 * DESCRIPTION: Create the association between the handler and the region
494 * this is a two way association.
496 ******************************************************************************/
500 ACPI_OPERAND_OBJECT *HandlerObj,
501 ACPI_OPERAND_OBJECT *RegionObj,
502 BOOLEAN AcpiNsIsLocked)
505 ACPI_FUNCTION_TRACE (EvAttachRegion);
508 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
509 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
510 AcpiUtGetNodeName (RegionObj->Region.Node),
511 RegionObj, HandlerObj,
512 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
514 /* Link this region to the front of the handler's list */
516 RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
517 HandlerObj->AddressSpace.RegionList = RegionObj;
519 /* Install the region's handler */
521 if (RegionObj->Region.Handler)
523 return_ACPI_STATUS (AE_ALREADY_EXISTS);
526 RegionObj->Region.Handler = HandlerObj;
527 AcpiUtAddReference (HandlerObj);
529 return_ACPI_STATUS (AE_OK);
533 /*******************************************************************************
535 * FUNCTION: AcpiEvExecuteRegMethod
537 * PARAMETERS: RegionObj - Region object
538 * Function - Passed to _REG: On (1) or Off (0)
542 * DESCRIPTION: Execute _REG method for a region
544 ******************************************************************************/
547 AcpiEvExecuteRegMethod (
548 ACPI_OPERAND_OBJECT *RegionObj,
551 ACPI_EVALUATE_INFO *Info;
552 ACPI_OPERAND_OBJECT *Args[3];
553 ACPI_OPERAND_OBJECT *RegionObj2;
557 ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
560 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
563 return_ACPI_STATUS (AE_NOT_EXIST);
566 if (RegionObj2->Extra.Method_REG == NULL)
568 return_ACPI_STATUS (AE_OK);
571 /* Allocate and initialize the evaluation information block */
573 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
576 return_ACPI_STATUS (AE_NO_MEMORY);
579 Info->PrefixNode = RegionObj2->Extra.Method_REG;
580 Info->RelativePathname = NULL;
581 Info->Parameters = Args;
582 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
585 * The _REG method has two arguments:
588 * Operation region space ID Same value as RegionObj->Region.SpaceId
591 * connection status 1 for connecting the handler, 0 for disconnecting
592 * the handler (Passed as a parameter)
594 Args[0] = AcpiUtCreateIntegerObject ((UINT64) RegionObj->Region.SpaceId);
597 Status = AE_NO_MEMORY;
601 Args[1] = AcpiUtCreateIntegerObject ((UINT64) Function);
604 Status = AE_NO_MEMORY;
608 Args[2] = NULL; /* Terminate list */
610 /* Execute the method, no return value */
613 AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
615 Status = AcpiNsEvaluate (Info);
616 AcpiUtRemoveReference (Args[1]);
619 AcpiUtRemoveReference (Args[0]);
623 return_ACPI_STATUS (Status);
627 /*******************************************************************************
629 * FUNCTION: AcpiEvExecuteRegMethods
631 * PARAMETERS: Node - Namespace node for the device
632 * SpaceId - The address space ID
636 * DESCRIPTION: Run all _REG methods for the input Space ID;
637 * Note: assumes namespace is locked, or system init time.
639 ******************************************************************************/
642 AcpiEvExecuteRegMethods (
643 ACPI_NAMESPACE_NODE *Node,
644 ACPI_ADR_SPACE_TYPE SpaceId)
649 ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
653 * Run all _REG methods for all Operation Regions for this space ID. This
654 * is a separate walk in order to handle any interdependencies between
655 * regions and _REG methods. (i.e. handlers must be installed for all
656 * regions of this Space ID before we can run any _REG methods)
658 Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
659 ACPI_NS_WALK_UNLOCK, AcpiEvRegRun, NULL,
662 /* Special case for EC: handle "orphan" _REG methods with no region */
664 if (SpaceId == ACPI_ADR_SPACE_EC)
666 AcpiEvOrphanEcRegMethod (Node);
669 return_ACPI_STATUS (Status);
673 /*******************************************************************************
675 * FUNCTION: AcpiEvRegRun
677 * PARAMETERS: WalkNamespace callback
679 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
681 ******************************************************************************/
685 ACPI_HANDLE ObjHandle,
690 ACPI_OPERAND_OBJECT *ObjDesc;
691 ACPI_NAMESPACE_NODE *Node;
692 ACPI_ADR_SPACE_TYPE SpaceId;
696 SpaceId = *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE, Context);
698 /* Convert and validate the device handle */
700 Node = AcpiNsValidateHandle (ObjHandle);
703 return (AE_BAD_PARAMETER);
707 * We only care about regions.and objects that are allowed to have address
710 if ((Node->Type != ACPI_TYPE_REGION) &&
711 (Node != AcpiGbl_RootNode))
716 /* Check for an existing internal object */
718 ObjDesc = AcpiNsGetAttachedObject (Node);
721 /* No object, just exit */
726 /* Object is a Region */
728 if (ObjDesc->Region.SpaceId != SpaceId)
730 /* This region is for a different address space, just ignore it */
735 Status = AcpiEvExecuteRegMethod (ObjDesc, ACPI_REG_CONNECT);
740 /*******************************************************************************
742 * FUNCTION: AcpiEvOrphanEcRegMethod
744 * PARAMETERS: EcDeviceNode - Namespace node for an EC device
748 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
749 * device. This is a _REG method that has no corresponding region
750 * within the EC device scope. The orphan _REG method appears to
751 * have been enabled by the description of the ECDT in the ACPI
752 * specification: "The availability of the region space can be
753 * detected by providing a _REG method object underneath the
754 * Embedded Controller device."
756 * To quickly access the EC device, we use the EcDeviceNode used
757 * during EC handler installation. Otherwise, we would need to
758 * perform a time consuming namespace walk, executing _HID
759 * methods to find the EC device.
761 * MUTEX: Assumes the namespace is locked
763 ******************************************************************************/
766 AcpiEvOrphanEcRegMethod (
767 ACPI_NAMESPACE_NODE *EcDeviceNode)
769 ACPI_HANDLE RegMethod;
770 ACPI_NAMESPACE_NODE *NextNode;
772 ACPI_OBJECT_LIST Args;
773 ACPI_OBJECT Objects[2];
776 ACPI_FUNCTION_TRACE (EvOrphanEcRegMethod);
784 /* Namespace is currently locked, must release */
786 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
788 /* Get a handle to a _REG method immediately under the EC device */
790 Status = AcpiGetHandle (EcDeviceNode, METHOD_NAME__REG, &RegMethod);
791 if (ACPI_FAILURE (Status))
793 goto Exit; /* There is no _REG method present */
797 * Execute the _REG method only if there is no Operation Region in
798 * this scope with the Embedded Controller space ID. Otherwise, it
799 * will already have been executed. Note, this allows for Regions
800 * with other space IDs to be present; but the code below will then
801 * execute the _REG method with the EmbeddedControl SpaceID argument.
803 NextNode = AcpiNsGetNextNode (EcDeviceNode, NULL);
806 if ((NextNode->Type == ACPI_TYPE_REGION) &&
807 (NextNode->Object) &&
808 (NextNode->Object->Region.SpaceId == ACPI_ADR_SPACE_EC))
810 goto Exit; /* Do not execute the _REG */
813 NextNode = AcpiNsGetNextNode (EcDeviceNode, NextNode);
816 /* Evaluate the _REG(EmbeddedControl,Connect) method */
819 Args.Pointer = Objects;
820 Objects[0].Type = ACPI_TYPE_INTEGER;
821 Objects[0].Integer.Value = ACPI_ADR_SPACE_EC;
822 Objects[1].Type = ACPI_TYPE_INTEGER;
823 Objects[1].Integer.Value = ACPI_REG_CONNECT;
825 Status = AcpiEvaluateObject (RegMethod, NULL, &Args, NULL);
828 /* We ignore all errors from above, don't care */
830 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);