1 /******************************************************************************
3 * Module Name: evrgnini- ACPI AddressSpace (OpRegion) init
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 __EVRGNINI_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>
51 #define _COMPONENT ACPI_EVENTS
52 ACPI_MODULE_NAME ("evrgnini")
54 /* Local prototypes */
57 AcpiEvIsPciRootBridge (
58 ACPI_NAMESPACE_NODE *Node);
61 /*******************************************************************************
63 * FUNCTION: AcpiEvSystemMemoryRegionSetup
65 * PARAMETERS: Handle - Region we are interested in
66 * Function - Start or stop
67 * HandlerContext - Address space handler context
68 * RegionContext - Region specific context
72 * DESCRIPTION: Setup a SystemMemory operation region
74 ******************************************************************************/
77 AcpiEvSystemMemoryRegionSetup (
83 ACPI_OPERAND_OBJECT *RegionDesc = (ACPI_OPERAND_OBJECT *) Handle;
84 ACPI_MEM_SPACE_CONTEXT *LocalRegionContext;
87 ACPI_FUNCTION_TRACE (EvSystemMemoryRegionSetup);
90 if (Function == ACPI_REGION_DEACTIVATE)
94 LocalRegionContext = (ACPI_MEM_SPACE_CONTEXT *) *RegionContext;
96 /* Delete a cached mapping if present */
98 if (LocalRegionContext->MappedLength)
100 AcpiOsUnmapMemory (LocalRegionContext->MappedLogicalAddress,
101 LocalRegionContext->MappedLength);
103 ACPI_FREE (LocalRegionContext);
104 *RegionContext = NULL;
106 return_ACPI_STATUS (AE_OK);
109 /* Create a new context */
111 LocalRegionContext = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_MEM_SPACE_CONTEXT));
112 if (!(LocalRegionContext))
114 return_ACPI_STATUS (AE_NO_MEMORY);
117 /* Save the region length and address for use in the handler */
119 LocalRegionContext->Length = RegionDesc->Region.Length;
120 LocalRegionContext->Address = RegionDesc->Region.Address;
122 *RegionContext = LocalRegionContext;
123 return_ACPI_STATUS (AE_OK);
127 /*******************************************************************************
129 * FUNCTION: AcpiEvIoSpaceRegionSetup
131 * PARAMETERS: Handle - Region we are interested in
132 * Function - Start or stop
133 * HandlerContext - Address space handler context
134 * RegionContext - Region specific context
138 * DESCRIPTION: Setup a IO operation region
140 ******************************************************************************/
143 AcpiEvIoSpaceRegionSetup (
146 void *HandlerContext,
147 void **RegionContext)
149 ACPI_FUNCTION_TRACE (EvIoSpaceRegionSetup);
152 if (Function == ACPI_REGION_DEACTIVATE)
154 *RegionContext = NULL;
158 *RegionContext = HandlerContext;
161 return_ACPI_STATUS (AE_OK);
165 /*******************************************************************************
167 * FUNCTION: AcpiEvPciConfigRegionSetup
169 * PARAMETERS: Handle - Region we are interested in
170 * Function - Start or stop
171 * HandlerContext - Address space handler context
172 * RegionContext - Region specific context
176 * DESCRIPTION: Setup a PCI_Config operation region
178 * MUTEX: Assumes namespace is not locked
180 ******************************************************************************/
183 AcpiEvPciConfigRegionSetup (
186 void *HandlerContext,
187 void **RegionContext)
189 ACPI_STATUS Status = AE_OK;
191 ACPI_PCI_ID *PciId = *RegionContext;
192 ACPI_OPERAND_OBJECT *HandlerObj;
193 ACPI_NAMESPACE_NODE *ParentNode;
194 ACPI_NAMESPACE_NODE *PciRootNode;
195 ACPI_NAMESPACE_NODE *PciDeviceNode;
196 ACPI_OPERAND_OBJECT *RegionObj = (ACPI_OPERAND_OBJECT *) Handle;
199 ACPI_FUNCTION_TRACE (EvPciConfigRegionSetup);
202 HandlerObj = RegionObj->Region.Handler;
206 * No installed handler. This shouldn't happen because the dispatch
207 * routine checks before we get here, but we check again just in case.
209 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
210 "Attempting to init a region %p, with no handler\n", RegionObj));
211 return_ACPI_STATUS (AE_NOT_EXIST);
214 *RegionContext = NULL;
215 if (Function == ACPI_REGION_DEACTIVATE)
221 return_ACPI_STATUS (Status);
224 ParentNode = RegionObj->Region.Node->Parent;
227 * Get the _SEG and _BBN values from the device upon which the handler
230 * We need to get the _SEG and _BBN objects relative to the PCI BUS device.
231 * This is the device the handler has been registered to handle.
235 * If the AddressSpace.Node is still pointing to the root, we need
236 * to scan upward for a PCI Root bridge and re-associate the OpRegion
237 * handlers with that device.
239 if (HandlerObj->AddressSpace.Node == AcpiGbl_RootNode)
241 /* Start search from the parent object */
243 PciRootNode = ParentNode;
244 while (PciRootNode != AcpiGbl_RootNode)
246 /* Get the _HID/_CID in order to detect a RootBridge */
248 if (AcpiEvIsPciRootBridge (PciRootNode))
250 /* Install a handler for this PCI root bridge */
252 Status = AcpiInstallAddressSpaceHandler (
253 (ACPI_HANDLE) PciRootNode,
254 ACPI_ADR_SPACE_PCI_CONFIG,
255 ACPI_DEFAULT_HANDLER, NULL, NULL);
256 if (ACPI_FAILURE (Status))
258 if (Status == AE_SAME_HANDLER)
261 * It is OK if the handler is already installed on the
262 * root bridge. Still need to return a context object
263 * for the new PCI_Config operation region, however.
269 ACPI_EXCEPTION ((AE_INFO, Status,
270 "Could not install PciConfig handler "
271 "for Root Bridge %4.4s",
272 AcpiUtGetNodeName (PciRootNode)));
278 PciRootNode = PciRootNode->Parent;
281 /* PCI root bridge not found, use namespace root node */
285 PciRootNode = HandlerObj->AddressSpace.Node;
289 * If this region is now initialized, we are done.
290 * (InstallAddressSpaceHandler could have initialized it)
292 if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
294 return_ACPI_STATUS (AE_OK);
297 /* Region is still not initialized. Create a new context */
299 PciId = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PCI_ID));
302 return_ACPI_STATUS (AE_NO_MEMORY);
306 * For PCI_Config space access, we need the segment, bus, device and
307 * function numbers. Acquire them here.
309 * Find the parent device object. (This allows the operation region to be
310 * within a subscope under the device, such as a control method.)
312 PciDeviceNode = RegionObj->Region.Node;
313 while (PciDeviceNode && (PciDeviceNode->Type != ACPI_TYPE_DEVICE))
315 PciDeviceNode = PciDeviceNode->Parent;
321 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
325 * Get the PCI device and function numbers from the _ADR object
326 * contained in the parent's scope.
328 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__ADR,
329 PciDeviceNode, &PciValue);
332 * The default is zero, and since the allocation above zeroed the data,
333 * just do nothing on failure.
335 if (ACPI_SUCCESS (Status))
337 PciId->Device = ACPI_HIWORD (ACPI_LODWORD (PciValue));
338 PciId->Function = ACPI_LOWORD (ACPI_LODWORD (PciValue));
341 /* The PCI segment number comes from the _SEG method */
343 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__SEG,
344 PciRootNode, &PciValue);
345 if (ACPI_SUCCESS (Status))
347 PciId->Segment = ACPI_LOWORD (PciValue);
350 /* The PCI bus number comes from the _BBN method */
352 Status = AcpiUtEvaluateNumericObject (METHOD_NAME__BBN,
353 PciRootNode, &PciValue);
354 if (ACPI_SUCCESS (Status))
356 PciId->Bus = ACPI_LOWORD (PciValue);
359 /* Complete/update the PCI ID for this device */
361 Status = AcpiHwDerivePciId (PciId, PciRootNode, RegionObj->Region.Node);
362 if (ACPI_FAILURE (Status))
365 return_ACPI_STATUS (Status);
368 *RegionContext = PciId;
369 return_ACPI_STATUS (AE_OK);
373 /*******************************************************************************
375 * FUNCTION: AcpiEvIsPciRootBridge
377 * PARAMETERS: Node - Device node being examined
379 * RETURN: TRUE if device is a PCI/PCI-Express Root Bridge
381 * DESCRIPTION: Determine if the input device represents a PCI Root Bridge by
382 * examining the _HID and _CID for the device.
384 ******************************************************************************/
387 AcpiEvIsPciRootBridge (
388 ACPI_NAMESPACE_NODE *Node)
391 ACPI_PNP_DEVICE_ID *Hid;
392 ACPI_PNP_DEVICE_ID_LIST *Cid;
397 /* Get the _HID and check for a PCI Root Bridge */
399 Status = AcpiUtExecute_HID (Node, &Hid);
400 if (ACPI_FAILURE (Status))
405 Match = AcpiUtIsPciRootBridge (Hid->String);
413 /* The _HID did not match. Get the _CID and check for a PCI Root Bridge */
415 Status = AcpiUtExecute_CID (Node, &Cid);
416 if (ACPI_FAILURE (Status))
421 /* Check all _CIDs in the returned list */
423 for (i = 0; i < Cid->Count; i++)
425 if (AcpiUtIsPciRootBridge (Cid->Ids[i].String))
437 /*******************************************************************************
439 * FUNCTION: AcpiEvPciBarRegionSetup
441 * PARAMETERS: Handle - Region we are interested in
442 * Function - Start or stop
443 * HandlerContext - Address space handler context
444 * RegionContext - Region specific context
448 * DESCRIPTION: Setup a PciBAR operation region
450 * MUTEX: Assumes namespace is not locked
452 ******************************************************************************/
455 AcpiEvPciBarRegionSetup (
458 void *HandlerContext,
459 void **RegionContext)
461 ACPI_FUNCTION_TRACE (EvPciBarRegionSetup);
464 return_ACPI_STATUS (AE_OK);
468 /*******************************************************************************
470 * FUNCTION: AcpiEvCmosRegionSetup
472 * PARAMETERS: Handle - Region we are interested in
473 * Function - Start or stop
474 * HandlerContext - Address space handler context
475 * RegionContext - Region specific context
479 * DESCRIPTION: Setup a CMOS operation region
481 * MUTEX: Assumes namespace is not locked
483 ******************************************************************************/
486 AcpiEvCmosRegionSetup (
489 void *HandlerContext,
490 void **RegionContext)
492 ACPI_FUNCTION_TRACE (EvCmosRegionSetup);
495 return_ACPI_STATUS (AE_OK);
499 /*******************************************************************************
501 * FUNCTION: AcpiEvDefaultRegionSetup
503 * PARAMETERS: Handle - Region we are interested in
504 * Function - Start or stop
505 * HandlerContext - Address space handler context
506 * RegionContext - Region specific context
510 * DESCRIPTION: Default region initialization
512 ******************************************************************************/
515 AcpiEvDefaultRegionSetup (
518 void *HandlerContext,
519 void **RegionContext)
521 ACPI_FUNCTION_TRACE (EvDefaultRegionSetup);
524 if (Function == ACPI_REGION_DEACTIVATE)
526 *RegionContext = NULL;
530 *RegionContext = HandlerContext;
533 return_ACPI_STATUS (AE_OK);
537 /*******************************************************************************
539 * FUNCTION: AcpiEvInitializeRegion
541 * PARAMETERS: RegionObj - Region we are initializing
542 * AcpiNsLocked - Is namespace locked?
546 * DESCRIPTION: Initializes the region, finds any _REG methods and saves them
547 * for execution at a later time
549 * Get the appropriate address space handler for a newly
552 * This also performs address space specific initialization. For
553 * example, PCI regions must have an _ADR object that contains
554 * a PCI address in the scope of the definition. This address is
555 * required to perform an access to PCI config space.
557 * MUTEX: Interpreter should be unlocked, because we may run the _REG
558 * method for this region.
560 ******************************************************************************/
563 AcpiEvInitializeRegion (
564 ACPI_OPERAND_OBJECT *RegionObj,
565 BOOLEAN AcpiNsLocked)
567 ACPI_OPERAND_OBJECT *HandlerObj;
568 ACPI_OPERAND_OBJECT *ObjDesc;
569 ACPI_ADR_SPACE_TYPE SpaceId;
570 ACPI_NAMESPACE_NODE *Node;
572 ACPI_NAMESPACE_NODE *MethodNode;
573 ACPI_NAME *RegNamePtr = (ACPI_NAME *) METHOD_NAME__REG;
574 ACPI_OPERAND_OBJECT *RegionObj2;
577 ACPI_FUNCTION_TRACE_U32 (EvInitializeRegion, AcpiNsLocked);
582 return_ACPI_STATUS (AE_BAD_PARAMETER);
585 if (RegionObj->Common.Flags & AOPOBJ_OBJECT_INITIALIZED)
587 return_ACPI_STATUS (AE_OK);
590 RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
593 return_ACPI_STATUS (AE_NOT_EXIST);
596 Node = RegionObj->Region.Node->Parent;
597 SpaceId = RegionObj->Region.SpaceId;
601 RegionObj->Region.Handler = NULL;
602 RegionObj2->Extra.Method_REG = NULL;
603 RegionObj->Common.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
604 RegionObj->Common.Flags |= AOPOBJ_OBJECT_INITIALIZED;
606 /* Find any "_REG" method associated with this region definition */
608 Status = AcpiNsSearchOneScope (
609 *RegNamePtr, Node, ACPI_TYPE_METHOD, &MethodNode);
610 if (ACPI_SUCCESS (Status))
613 * The _REG method is optional and there can be only one per region
614 * definition. This will be executed when the handler is attached
617 RegionObj2->Extra.Method_REG = MethodNode;
621 * The following loop depends upon the root Node having no parent
622 * ie: AcpiGbl_RootNode->ParentEntry being set to NULL
626 /* Check to see if a handler exists */
629 ObjDesc = AcpiNsGetAttachedObject (Node);
632 /* Can only be a handler if the object exists */
636 case ACPI_TYPE_DEVICE:
638 HandlerObj = ObjDesc->Device.Handler;
641 case ACPI_TYPE_PROCESSOR:
643 HandlerObj = ObjDesc->Processor.Handler;
646 case ACPI_TYPE_THERMAL:
648 HandlerObj = ObjDesc->ThermalZone.Handler;
651 case ACPI_TYPE_METHOD:
653 * If we are executing module level code, the original
654 * Node's object was replaced by this Method object and we
655 * saved the handler in the method object.
657 * See AcpiNsExecModuleCode
659 if (ObjDesc->Method.InfoFlags & ACPI_METHOD_MODULE_LEVEL)
661 HandlerObj = ObjDesc->Method.Dispatch.Handler;
667 /* Ignore other objects */
674 /* Is this handler of the correct type? */
676 if (HandlerObj->AddressSpace.SpaceId == SpaceId)
678 /* Found correct handler */
680 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
681 "Found handler %p for region %p in obj %p\n",
682 HandlerObj, RegionObj, ObjDesc));
684 Status = AcpiEvAttachRegion (HandlerObj, RegionObj,
688 * Tell all users that this region is usable by
689 * running the _REG method
693 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
694 if (ACPI_FAILURE (Status))
696 return_ACPI_STATUS (Status);
700 Status = AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_CONNECT);
704 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
705 if (ACPI_FAILURE (Status))
707 return_ACPI_STATUS (Status);
711 return_ACPI_STATUS (AE_OK);
714 /* Try next handler in the list */
716 HandlerObj = HandlerObj->AddressSpace.Next;
720 /* This node does not have the handler we need; Pop up one level */
725 /* If we get here, there is no handler for this region */
727 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
728 "No handler for RegionType %s(%X) (RegionObj %p)\n",
729 AcpiUtGetRegionName (SpaceId), SpaceId, RegionObj));
731 return_ACPI_STATUS (AE_NOT_EXIST);