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__
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acevents.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/acinterp.h>
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;
161 ACPI_PHYSICAL_ADDRESS Address;
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;
251 Address = (RegionObj->Region.Address + RegionOffset);
254 * Special handling for GenericSerialBus and GeneralPurposeIo:
255 * There are three extra parameters that must be passed to the
256 * handler via the context:
257 * 1) Connection buffer, a resource template from Connection() op
258 * 2) Length of the above buffer
259 * 3) Actual access length from the AccessAs() op
261 * In addition, for GeneralPurposeIo, the Address and BitWidth fields
262 * are defined as follows:
263 * 1) Address is the pin number index of the field (bit offset from
264 * the previous Connection)
265 * 2) BitWidth is the actual bit length of the field (number of pins)
267 if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS) &&
271 /* Get the Connection (ResourceTemplate) buffer */
273 Context->Connection = FieldObj->Field.ResourceBuffer;
274 Context->Length = FieldObj->Field.ResourceLength;
275 Context->AccessLength = FieldObj->Field.AccessLength;
277 if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO) &&
281 /* Get the Connection (ResourceTemplate) buffer */
283 Context->Connection = FieldObj->Field.ResourceBuffer;
284 Context->Length = FieldObj->Field.ResourceLength;
285 Context->AccessLength = FieldObj->Field.AccessLength;
286 Address = FieldObj->Field.PinNumberIndex;
287 BitWidth = FieldObj->Field.BitLength;
290 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
291 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
292 &RegionObj->Region.Handler->AddressSpace, Handler,
293 ACPI_FORMAT_NATIVE_UINT (Address),
294 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
296 if (!(HandlerDesc->AddressSpace.HandlerFlags &
297 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
300 * For handlers other than the default (supplied) handlers, we must
301 * exit the interpreter because the handler *might* block -- we don't
302 * know what it will do, so we can't hold the lock on the intepreter.
304 AcpiExExitInterpreter();
307 /* Call the handler */
309 Status = Handler (Function, Address, BitWidth, Value, Context,
310 RegionObj2->Extra.RegionContext);
312 if (ACPI_FAILURE (Status))
314 ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
315 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
318 if (!(HandlerDesc->AddressSpace.HandlerFlags &
319 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
322 * We just returned from a non-default handler, we must re-enter the
325 AcpiExEnterInterpreter ();
328 return_ACPI_STATUS (Status);
332 /*******************************************************************************
334 * FUNCTION: AcpiEvDetachRegion
336 * PARAMETERS: RegionObj - Region Object
337 * AcpiNsIsLocked - Namespace Region Already Locked?
341 * DESCRIPTION: Break the association between the handler and the region
342 * this is a two way association.
344 ******************************************************************************/
348 ACPI_OPERAND_OBJECT *RegionObj,
349 BOOLEAN AcpiNsIsLocked)
351 ACPI_OPERAND_OBJECT *HandlerObj;
352 ACPI_OPERAND_OBJECT *ObjDesc;
353 ACPI_OPERAND_OBJECT *StartDesc;
354 ACPI_OPERAND_OBJECT **LastObjPtr;
355 ACPI_ADR_SPACE_SETUP RegionSetup;
356 void **RegionContext;
357 ACPI_OPERAND_OBJECT *RegionObj2;
361 ACPI_FUNCTION_TRACE (EvDetachRegion);
364 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
369 RegionContext = &RegionObj2->Extra.RegionContext;
371 /* Get the address handler from the region object */
373 HandlerObj = RegionObj->Region.Handler;
376 /* This region has no handler, all done */
381 /* Find this region in the handler's list */
383 ObjDesc = HandlerObj->AddressSpace.RegionList;
385 LastObjPtr = &HandlerObj->AddressSpace.RegionList;
389 /* Is this the correct Region? */
391 if (ObjDesc == RegionObj)
393 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
394 "Removing Region %p from address handler %p\n",
395 RegionObj, HandlerObj));
397 /* This is it, remove it from the handler's list */
399 *LastObjPtr = ObjDesc->Region.Next;
400 ObjDesc->Region.Next = NULL; /* Must clear field */
404 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
405 if (ACPI_FAILURE (Status))
411 /* Now stop region accesses by executing the _REG method */
413 Status = AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_DISCONNECT);
414 if (ACPI_FAILURE (Status))
416 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
417 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
422 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
423 if (ACPI_FAILURE (Status))
430 * If the region has been activated, call the setup handler with
431 * the deactivate notification
433 if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
435 RegionSetup = HandlerObj->AddressSpace.Setup;
436 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
437 HandlerObj->AddressSpace.Context, RegionContext);
440 * RegionContext should have been released by the deactivate
441 * operation. We don't need access to it anymore here.
445 *RegionContext = NULL;
448 /* Init routine may fail, Just ignore errors */
450 if (ACPI_FAILURE (Status))
452 ACPI_EXCEPTION ((AE_INFO, Status,
453 "from region handler - deactivate, [%s]",
454 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
457 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
461 * Remove handler reference in the region
463 * NOTE: this doesn't mean that the region goes away, the region
464 * is just inaccessible as indicated to the _REG method
466 * If the region is on the handler's list, this must be the
469 RegionObj->Region.Handler = NULL;
470 AcpiUtRemoveReference (HandlerObj);
475 /* Walk the linked list of handlers */
477 LastObjPtr = &ObjDesc->Region.Next;
478 ObjDesc = ObjDesc->Region.Next;
480 /* Prevent infinite loop if list is corrupted */
482 if (ObjDesc == StartDesc)
484 ACPI_ERROR ((AE_INFO,
485 "Circular handler list in region object %p",
491 /* If we get here, the region was not in the handler's region list */
493 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
494 "Cannot remove region %p from address handler %p\n",
495 RegionObj, HandlerObj));
501 /*******************************************************************************
503 * FUNCTION: AcpiEvAttachRegion
505 * PARAMETERS: HandlerObj - Handler Object
506 * RegionObj - Region Object
507 * AcpiNsIsLocked - Namespace Region Already Locked?
511 * DESCRIPTION: Create the association between the handler and the region
512 * this is a two way association.
514 ******************************************************************************/
518 ACPI_OPERAND_OBJECT *HandlerObj,
519 ACPI_OPERAND_OBJECT *RegionObj,
520 BOOLEAN AcpiNsIsLocked)
523 ACPI_FUNCTION_TRACE (EvAttachRegion);
526 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
527 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
528 AcpiUtGetNodeName (RegionObj->Region.Node),
529 RegionObj, HandlerObj,
530 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
532 /* Link this region to the front of the handler's list */
534 RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
535 HandlerObj->AddressSpace.RegionList = RegionObj;
537 /* Install the region's handler */
539 if (RegionObj->Region.Handler)
541 return_ACPI_STATUS (AE_ALREADY_EXISTS);
544 RegionObj->Region.Handler = HandlerObj;
545 AcpiUtAddReference (HandlerObj);
547 return_ACPI_STATUS (AE_OK);
551 /*******************************************************************************
553 * FUNCTION: AcpiEvExecuteRegMethod
555 * PARAMETERS: RegionObj - Region object
556 * Function - Passed to _REG: On (1) or Off (0)
560 * DESCRIPTION: Execute _REG method for a region
562 ******************************************************************************/
565 AcpiEvExecuteRegMethod (
566 ACPI_OPERAND_OBJECT *RegionObj,
569 ACPI_EVALUATE_INFO *Info;
570 ACPI_OPERAND_OBJECT *Args[3];
571 ACPI_OPERAND_OBJECT *RegionObj2;
575 ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
578 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
581 return_ACPI_STATUS (AE_NOT_EXIST);
584 if (RegionObj2->Extra.Method_REG == NULL)
586 return_ACPI_STATUS (AE_OK);
589 /* Allocate and initialize the evaluation information block */
591 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
594 return_ACPI_STATUS (AE_NO_MEMORY);
597 Info->PrefixNode = RegionObj2->Extra.Method_REG;
598 Info->RelativePathname = NULL;
599 Info->Parameters = Args;
600 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
603 * The _REG method has two arguments:
606 * Operation region space ID Same value as RegionObj->Region.SpaceId
609 * connection status 1 for connecting the handler, 0 for disconnecting
610 * the handler (Passed as a parameter)
612 Args[0] = AcpiUtCreateIntegerObject ((UINT64) RegionObj->Region.SpaceId);
615 Status = AE_NO_MEMORY;
619 Args[1] = AcpiUtCreateIntegerObject ((UINT64) Function);
622 Status = AE_NO_MEMORY;
626 Args[2] = NULL; /* Terminate list */
628 /* Execute the method, no return value */
631 AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
633 Status = AcpiNsEvaluate (Info);
634 AcpiUtRemoveReference (Args[1]);
637 AcpiUtRemoveReference (Args[0]);
641 return_ACPI_STATUS (Status);
645 /*******************************************************************************
647 * FUNCTION: AcpiEvExecuteRegMethods
649 * PARAMETERS: Node - Namespace node for the device
650 * SpaceId - The address space ID
654 * DESCRIPTION: Run all _REG methods for the input Space ID;
655 * Note: assumes namespace is locked, or system init time.
657 ******************************************************************************/
660 AcpiEvExecuteRegMethods (
661 ACPI_NAMESPACE_NODE *Node,
662 ACPI_ADR_SPACE_TYPE SpaceId)
667 ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
671 * Run all _REG methods for all Operation Regions for this space ID. This
672 * is a separate walk in order to handle any interdependencies between
673 * regions and _REG methods. (i.e. handlers must be installed for all
674 * regions of this Space ID before we can run any _REG methods)
676 Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
677 ACPI_NS_WALK_UNLOCK, AcpiEvRegRun, NULL,
680 /* Special case for EC: handle "orphan" _REG methods with no region */
682 if (SpaceId == ACPI_ADR_SPACE_EC)
684 AcpiEvOrphanEcRegMethod (Node);
687 return_ACPI_STATUS (Status);
691 /*******************************************************************************
693 * FUNCTION: AcpiEvRegRun
695 * PARAMETERS: WalkNamespace callback
697 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
699 ******************************************************************************/
703 ACPI_HANDLE ObjHandle,
708 ACPI_OPERAND_OBJECT *ObjDesc;
709 ACPI_NAMESPACE_NODE *Node;
710 ACPI_ADR_SPACE_TYPE SpaceId;
714 SpaceId = *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE, Context);
716 /* Convert and validate the device handle */
718 Node = AcpiNsValidateHandle (ObjHandle);
721 return (AE_BAD_PARAMETER);
725 * We only care about regions.and objects that are allowed to have address
728 if ((Node->Type != ACPI_TYPE_REGION) &&
729 (Node != AcpiGbl_RootNode))
734 /* Check for an existing internal object */
736 ObjDesc = AcpiNsGetAttachedObject (Node);
739 /* No object, just exit */
744 /* Object is a Region */
746 if (ObjDesc->Region.SpaceId != SpaceId)
748 /* This region is for a different address space, just ignore it */
753 Status = AcpiEvExecuteRegMethod (ObjDesc, ACPI_REG_CONNECT);
758 /*******************************************************************************
760 * FUNCTION: AcpiEvOrphanEcRegMethod
762 * PARAMETERS: EcDeviceNode - Namespace node for an EC device
766 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
767 * device. This is a _REG method that has no corresponding region
768 * within the EC device scope. The orphan _REG method appears to
769 * have been enabled by the description of the ECDT in the ACPI
770 * specification: "The availability of the region space can be
771 * detected by providing a _REG method object underneath the
772 * Embedded Controller device."
774 * To quickly access the EC device, we use the EcDeviceNode used
775 * during EC handler installation. Otherwise, we would need to
776 * perform a time consuming namespace walk, executing _HID
777 * methods to find the EC device.
779 * MUTEX: Assumes the namespace is locked
781 ******************************************************************************/
784 AcpiEvOrphanEcRegMethod (
785 ACPI_NAMESPACE_NODE *EcDeviceNode)
787 ACPI_HANDLE RegMethod;
788 ACPI_NAMESPACE_NODE *NextNode;
790 ACPI_OBJECT_LIST Args;
791 ACPI_OBJECT Objects[2];
794 ACPI_FUNCTION_TRACE (EvOrphanEcRegMethod);
802 /* Namespace is currently locked, must release */
804 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
806 /* Get a handle to a _REG method immediately under the EC device */
808 Status = AcpiGetHandle (EcDeviceNode, METHOD_NAME__REG, &RegMethod);
809 if (ACPI_FAILURE (Status))
811 goto Exit; /* There is no _REG method present */
815 * Execute the _REG method only if there is no Operation Region in
816 * this scope with the Embedded Controller space ID. Otherwise, it
817 * will already have been executed. Note, this allows for Regions
818 * with other space IDs to be present; but the code below will then
819 * execute the _REG method with the EmbeddedControl SpaceID argument.
821 NextNode = AcpiNsGetNextNode (EcDeviceNode, NULL);
824 if ((NextNode->Type == ACPI_TYPE_REGION) &&
825 (NextNode->Object) &&
826 (NextNode->Object->Region.SpaceId == ACPI_ADR_SPACE_EC))
828 goto Exit; /* Do not execute the _REG */
831 NextNode = AcpiNsGetNextNode (EcDeviceNode, NextNode);
834 /* Evaluate the _REG(EmbeddedControl,Connect) method */
837 Args.Pointer = Objects;
838 Objects[0].Type = ACPI_TYPE_INTEGER;
839 Objects[0].Integer.Value = ACPI_ADR_SPACE_EC;
840 Objects[1].Type = ACPI_TYPE_INTEGER;
841 Objects[1].Integer.Value = ACPI_REG_CONNECT;
843 Status = AcpiEvaluateObject (RegMethod, NULL, &Args, NULL);
846 /* We ignore all errors from above, don't care */
848 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);