1 /******************************************************************************
3 * Module Name: evregion - Operation Region support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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
**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
;
366 LastObjPtr
= &HandlerObj
->AddressSpace
.RegionList
;
370 /* Is this the correct Region? */
372 if (ObjDesc
== RegionObj
)
374 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION
,
375 "Removing Region %p from address handler %p\n",
376 RegionObj
, HandlerObj
));
378 /* This is it, remove it from the handler's list */
380 *LastObjPtr
= ObjDesc
->Region
.Next
;
381 ObjDesc
->Region
.Next
= NULL
; /* Must clear field */
385 Status
= AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
386 if (ACPI_FAILURE (Status
))
392 /* Now stop region accesses by executing the _REG method */
394 Status
= AcpiEvExecuteRegMethod (RegionObj
, ACPI_REG_DISCONNECT
);
395 if (ACPI_FAILURE (Status
))
397 ACPI_EXCEPTION ((AE_INFO
, Status
, "from region _REG, [%s]",
398 AcpiUtGetRegionName (RegionObj
->Region
.SpaceId
)));
403 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);
404 if (ACPI_FAILURE (Status
))
411 * If the region has been activated, call the setup handler with
412 * the deactivate notification
414 if (RegionObj
->Region
.Flags
& AOPOBJ_SETUP_COMPLETE
)
416 RegionSetup
= HandlerObj
->AddressSpace
.Setup
;
417 Status
= RegionSetup (RegionObj
, ACPI_REGION_DEACTIVATE
,
418 HandlerObj
->AddressSpace
.Context
, RegionContext
);
421 * RegionContext should have been released by the deactivate
422 * operation. We don't need access to it anymore here.
426 *RegionContext
= NULL
;
429 /* Init routine may fail, Just ignore errors */
431 if (ACPI_FAILURE (Status
))
433 ACPI_EXCEPTION ((AE_INFO
, Status
,
434 "from region handler - deactivate, [%s]",
435 AcpiUtGetRegionName (RegionObj
->Region
.SpaceId
)));
438 RegionObj
->Region
.Flags
&= ~(AOPOBJ_SETUP_COMPLETE
);
442 * Remove handler reference in the region
444 * NOTE: this doesn't mean that the region goes away, the region
445 * is just inaccessible as indicated to the _REG method
447 * If the region is on the handler's list, this must be the
450 RegionObj
->Region
.Handler
= NULL
;
451 AcpiUtRemoveReference (HandlerObj
);
456 /* Walk the linked list of handlers */
458 LastObjPtr
= &ObjDesc
->Region
.Next
;
459 ObjDesc
= ObjDesc
->Region
.Next
;
462 /* If we get here, the region was not in the handler's region list */
464 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION
,
465 "Cannot remove region %p from address handler %p\n",
466 RegionObj
, HandlerObj
));
472 /*******************************************************************************
474 * FUNCTION: AcpiEvAttachRegion
476 * PARAMETERS: HandlerObj - Handler Object
477 * RegionObj - Region Object
478 * AcpiNsIsLocked - Namespace Region Already Locked?
482 * DESCRIPTION: Create the association between the handler and the region
483 * this is a two way association.
485 ******************************************************************************/
489 ACPI_OPERAND_OBJECT
*HandlerObj
,
490 ACPI_OPERAND_OBJECT
*RegionObj
,
491 BOOLEAN AcpiNsIsLocked
)
494 ACPI_FUNCTION_TRACE (EvAttachRegion
);
497 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION
,
498 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
499 AcpiUtGetNodeName (RegionObj
->Region
.Node
),
500 RegionObj
, HandlerObj
,
501 AcpiUtGetRegionName (RegionObj
->Region
.SpaceId
)));
503 /* Link this region to the front of the handler's list */
505 RegionObj
->Region
.Next
= HandlerObj
->AddressSpace
.RegionList
;
506 HandlerObj
->AddressSpace
.RegionList
= RegionObj
;
508 /* Install the region's handler */
510 if (RegionObj
->Region
.Handler
)
512 return_ACPI_STATUS (AE_ALREADY_EXISTS
);
515 RegionObj
->Region
.Handler
= HandlerObj
;
516 AcpiUtAddReference (HandlerObj
);
518 return_ACPI_STATUS (AE_OK
);
522 /*******************************************************************************
524 * FUNCTION: AcpiEvExecuteRegMethod
526 * PARAMETERS: RegionObj - Region object
527 * Function - Passed to _REG: On (1) or Off (0)
531 * DESCRIPTION: Execute _REG method for a region
533 ******************************************************************************/
536 AcpiEvExecuteRegMethod (
537 ACPI_OPERAND_OBJECT
*RegionObj
,
540 ACPI_EVALUATE_INFO
*Info
;
541 ACPI_OPERAND_OBJECT
*Args
[3];
542 ACPI_OPERAND_OBJECT
*RegionObj2
;
546 ACPI_FUNCTION_TRACE (EvExecuteRegMethod
);
549 RegionObj2
= AcpiNsGetSecondaryObject (RegionObj
);
552 return_ACPI_STATUS (AE_NOT_EXIST
);
555 if (RegionObj2
->Extra
.Method_REG
== NULL
)
557 return_ACPI_STATUS (AE_OK
);
560 /* Allocate and initialize the evaluation information block */
562 Info
= ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO
));
565 return_ACPI_STATUS (AE_NO_MEMORY
);
568 Info
->PrefixNode
= RegionObj2
->Extra
.Method_REG
;
569 Info
->RelativePathname
= NULL
;
570 Info
->Parameters
= Args
;
571 Info
->Flags
= ACPI_IGNORE_RETURN_VALUE
;
574 * The _REG method has two arguments:
577 * Operation region space ID Same value as RegionObj->Region.SpaceId
580 * connection status 1 for connecting the handler, 0 for disconnecting
581 * the handler (Passed as a parameter)
583 Args
[0] = AcpiUtCreateIntegerObject ((UINT64
) RegionObj
->Region
.SpaceId
);
586 Status
= AE_NO_MEMORY
;
590 Args
[1] = AcpiUtCreateIntegerObject ((UINT64
) Function
);
593 Status
= AE_NO_MEMORY
;
597 Args
[2] = NULL
; /* Terminate list */
599 /* Execute the method, no return value */
602 AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD
, Info
->PrefixNode
, NULL
));
604 Status
= AcpiNsEvaluate (Info
);
605 AcpiUtRemoveReference (Args
[1]);
608 AcpiUtRemoveReference (Args
[0]);
612 return_ACPI_STATUS (Status
);
616 /*******************************************************************************
618 * FUNCTION: AcpiEvExecuteRegMethods
620 * PARAMETERS: Node - Namespace node for the device
621 * SpaceId - The address space ID
625 * DESCRIPTION: Run all _REG methods for the input Space ID;
626 * Note: assumes namespace is locked, or system init time.
628 ******************************************************************************/
631 AcpiEvExecuteRegMethods (
632 ACPI_NAMESPACE_NODE
*Node
,
633 ACPI_ADR_SPACE_TYPE SpaceId
)
638 ACPI_FUNCTION_TRACE (EvExecuteRegMethods
);
642 * Run all _REG methods for all Operation Regions for this space ID. This
643 * is a separate walk in order to handle any interdependencies between
644 * regions and _REG methods. (i.e. handlers must be installed for all
645 * regions of this Space ID before we can run any _REG methods)
647 Status
= AcpiNsWalkNamespace (ACPI_TYPE_ANY
, Node
, ACPI_UINT32_MAX
,
648 ACPI_NS_WALK_UNLOCK
, AcpiEvRegRun
, NULL
,
651 /* Special case for EC: handle "orphan" _REG methods with no region */
653 if (SpaceId
== ACPI_ADR_SPACE_EC
)
655 AcpiEvOrphanEcRegMethod (Node
);
658 return_ACPI_STATUS (Status
);
662 /*******************************************************************************
664 * FUNCTION: AcpiEvRegRun
666 * PARAMETERS: WalkNamespace callback
668 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
670 ******************************************************************************/
674 ACPI_HANDLE ObjHandle
,
679 ACPI_OPERAND_OBJECT
*ObjDesc
;
680 ACPI_NAMESPACE_NODE
*Node
;
681 ACPI_ADR_SPACE_TYPE SpaceId
;
685 SpaceId
= *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE
, Context
);
687 /* Convert and validate the device handle */
689 Node
= AcpiNsValidateHandle (ObjHandle
);
692 return (AE_BAD_PARAMETER
);
696 * We only care about regions.and objects that are allowed to have address
699 if ((Node
->Type
!= ACPI_TYPE_REGION
) &&
700 (Node
!= AcpiGbl_RootNode
))
705 /* Check for an existing internal object */
707 ObjDesc
= AcpiNsGetAttachedObject (Node
);
710 /* No object, just exit */
715 /* Object is a Region */
717 if (ObjDesc
->Region
.SpaceId
!= SpaceId
)
719 /* This region is for a different address space, just ignore it */
724 Status
= AcpiEvExecuteRegMethod (ObjDesc
, ACPI_REG_CONNECT
);
729 /*******************************************************************************
731 * FUNCTION: AcpiEvOrphanEcRegMethod
733 * PARAMETERS: EcDeviceNode - Namespace node for an EC device
737 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
738 * device. This is a _REG method that has no corresponding region
739 * within the EC device scope. The orphan _REG method appears to
740 * have been enabled by the description of the ECDT in the ACPI
741 * specification: "The availability of the region space can be
742 * detected by providing a _REG method object underneath the
743 * Embedded Controller device."
745 * To quickly access the EC device, we use the EcDeviceNode used
746 * during EC handler installation. Otherwise, we would need to
747 * perform a time consuming namespace walk, executing _HID
748 * methods to find the EC device.
750 * MUTEX: Assumes the namespace is locked
752 ******************************************************************************/
755 AcpiEvOrphanEcRegMethod (
756 ACPI_NAMESPACE_NODE
*EcDeviceNode
)
758 ACPI_HANDLE RegMethod
;
759 ACPI_NAMESPACE_NODE
*NextNode
;
761 ACPI_OBJECT_LIST Args
;
762 ACPI_OBJECT Objects
[2];
765 ACPI_FUNCTION_TRACE (EvOrphanEcRegMethod
);
773 /* Namespace is currently locked, must release */
775 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE
);
777 /* Get a handle to a _REG method immediately under the EC device */
779 Status
= AcpiGetHandle (EcDeviceNode
, METHOD_NAME__REG
, &RegMethod
);
780 if (ACPI_FAILURE (Status
))
782 goto Exit
; /* There is no _REG method present */
786 * Execute the _REG method only if there is no Operation Region in
787 * this scope with the Embedded Controller space ID. Otherwise, it
788 * will already have been executed. Note, this allows for Regions
789 * with other space IDs to be present; but the code below will then
790 * execute the _REG method with the EmbeddedControl SpaceID argument.
792 NextNode
= AcpiNsGetNextNode (EcDeviceNode
, NULL
);
795 if ((NextNode
->Type
== ACPI_TYPE_REGION
) &&
796 (NextNode
->Object
) &&
797 (NextNode
->Object
->Region
.SpaceId
== ACPI_ADR_SPACE_EC
))
799 goto Exit
; /* Do not execute the _REG */
802 NextNode
= AcpiNsGetNextNode (EcDeviceNode
, NextNode
);
805 /* Evaluate the _REG(EmbeddedControl,Connect) method */
808 Args
.Pointer
= Objects
;
809 Objects
[0].Type
= ACPI_TYPE_INTEGER
;
810 Objects
[0].Integer
.Value
= ACPI_ADR_SPACE_EC
;
811 Objects
[1].Type
= ACPI_TYPE_INTEGER
;
812 Objects
[1].Integer
.Value
= ACPI_REG_CONNECT
;
814 Status
= AcpiEvaluateObject (RegMethod
, NULL
, &Args
, NULL
);
817 /* We ignore all errors from above, don't care */
819 Status
= AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE
);