evxfregn.c revision 217365
1/******************************************************************************
2 *
3 * Module Name: evxfregn - External Interfaces, ACPI Operation Regions and
4 *                         Address Spaces.
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions, and the following disclaimer,
17 *    without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 *    substantially similar to the "NO WARRANTY" disclaimer below
20 *    ("Disclaimer") and any redistribution must be conditioned upon
21 *    including a substantially similar Disclaimer requirement for further
22 *    binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 *    of any contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#define __EVXFREGN_C__
46
47#include <contrib/dev/acpica/include/acpi.h>
48#include <contrib/dev/acpica/include/accommon.h>
49#include <contrib/dev/acpica/include/acnamesp.h>
50#include <contrib/dev/acpica/include/acevents.h>
51
52#define _COMPONENT          ACPI_EVENTS
53        ACPI_MODULE_NAME    ("evxfregn")
54
55
56/*******************************************************************************
57 *
58 * FUNCTION:    AcpiInstallAddressSpaceHandler
59 *
60 * PARAMETERS:  Device          - Handle for the device
61 *              SpaceId         - The address space ID
62 *              Handler         - Address of the handler
63 *              Setup           - Address of the setup function
64 *              Context         - Value passed to the handler on each access
65 *
66 * RETURN:      Status
67 *
68 * DESCRIPTION: Install a handler for all OpRegions of a given SpaceId.
69 *
70 * NOTE: This function should only be called after AcpiEnableSubsystem has
71 * been called. This is because any _REG methods associated with the Space ID
72 * are executed here, and these methods can only be safely executed after
73 * the default handlers have been installed and the hardware has been
74 * initialized (via AcpiEnableSubsystem.)
75 *
76 ******************************************************************************/
77
78ACPI_STATUS
79AcpiInstallAddressSpaceHandler (
80    ACPI_HANDLE             Device,
81    ACPI_ADR_SPACE_TYPE     SpaceId,
82    ACPI_ADR_SPACE_HANDLER  Handler,
83    ACPI_ADR_SPACE_SETUP    Setup,
84    void                    *Context)
85{
86    ACPI_NAMESPACE_NODE     *Node;
87    ACPI_STATUS             Status;
88
89
90    ACPI_FUNCTION_TRACE (AcpiInstallAddressSpaceHandler);
91
92
93    /* Parameter validation */
94
95    if (!Device)
96    {
97        return_ACPI_STATUS (AE_BAD_PARAMETER);
98    }
99
100    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
101    if (ACPI_FAILURE (Status))
102    {
103        return_ACPI_STATUS (Status);
104    }
105
106    /* Convert and validate the device handle */
107
108    Node = AcpiNsValidateHandle (Device);
109    if (!Node)
110    {
111        Status = AE_BAD_PARAMETER;
112        goto UnlockAndExit;
113    }
114
115    /* Install the handler for all Regions for this Space ID */
116
117    Status = AcpiEvInstallSpaceHandler (Node, SpaceId, Handler, Setup, Context);
118    if (ACPI_FAILURE (Status))
119    {
120        goto UnlockAndExit;
121    }
122
123    /* Run all _REG methods for this address space */
124
125    Status = AcpiEvExecuteRegMethods (Node, SpaceId);
126
127UnlockAndExit:
128    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
129    return_ACPI_STATUS (Status);
130}
131
132ACPI_EXPORT_SYMBOL (AcpiInstallAddressSpaceHandler)
133
134
135/*******************************************************************************
136 *
137 * FUNCTION:    AcpiRemoveAddressSpaceHandler
138 *
139 * PARAMETERS:  Device          - Handle for the device
140 *              SpaceId         - The address space ID
141 *              Handler         - Address of the handler
142 *
143 * RETURN:      Status
144 *
145 * DESCRIPTION: Remove a previously installed handler.
146 *
147 ******************************************************************************/
148
149ACPI_STATUS
150AcpiRemoveAddressSpaceHandler (
151    ACPI_HANDLE             Device,
152    ACPI_ADR_SPACE_TYPE     SpaceId,
153    ACPI_ADR_SPACE_HANDLER  Handler)
154{
155    ACPI_OPERAND_OBJECT     *ObjDesc;
156    ACPI_OPERAND_OBJECT     *HandlerObj;
157    ACPI_OPERAND_OBJECT     *RegionObj;
158    ACPI_OPERAND_OBJECT     **LastObjPtr;
159    ACPI_NAMESPACE_NODE     *Node;
160    ACPI_STATUS             Status;
161
162
163    ACPI_FUNCTION_TRACE (AcpiRemoveAddressSpaceHandler);
164
165
166    /* Parameter validation */
167
168    if (!Device)
169    {
170        return_ACPI_STATUS (AE_BAD_PARAMETER);
171    }
172
173    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
174    if (ACPI_FAILURE (Status))
175    {
176        return_ACPI_STATUS (Status);
177    }
178
179    /* Convert and validate the device handle */
180
181    Node = AcpiNsValidateHandle (Device);
182    if (!Node ||
183        ((Node->Type != ACPI_TYPE_DEVICE)    &&
184         (Node->Type != ACPI_TYPE_PROCESSOR) &&
185         (Node->Type != ACPI_TYPE_THERMAL)   &&
186         (Node != AcpiGbl_RootNode)))
187    {
188        Status = AE_BAD_PARAMETER;
189        goto UnlockAndExit;
190    }
191
192    /* Make sure the internal object exists */
193
194    ObjDesc = AcpiNsGetAttachedObject (Node);
195    if (!ObjDesc)
196    {
197        Status = AE_NOT_EXIST;
198        goto UnlockAndExit;
199    }
200
201    /* Find the address handler the user requested */
202
203    HandlerObj = ObjDesc->Device.Handler;
204    LastObjPtr = &ObjDesc->Device.Handler;
205    while (HandlerObj)
206    {
207        /* We have a handler, see if user requested this one */
208
209        if (HandlerObj->AddressSpace.SpaceId == SpaceId)
210        {
211            /* Handler must be the same as the installed handler */
212
213            if (HandlerObj->AddressSpace.Handler != Handler)
214            {
215                Status = AE_BAD_PARAMETER;
216                goto UnlockAndExit;
217            }
218
219            /* Matched SpaceId, first dereference this in the Regions */
220
221            ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
222                "Removing address handler %p(%p) for region %s "
223                "on Device %p(%p)\n",
224                HandlerObj, Handler, AcpiUtGetRegionName (SpaceId),
225                Node, ObjDesc));
226
227            RegionObj = HandlerObj->AddressSpace.RegionList;
228
229            /* Walk the handler's region list */
230
231            while (RegionObj)
232            {
233                /*
234                 * First disassociate the handler from the region.
235                 *
236                 * NOTE: this doesn't mean that the region goes away
237                 * The region is just inaccessible as indicated to
238                 * the _REG method
239                 */
240                AcpiEvDetachRegion (RegionObj, TRUE);
241
242                /*
243                 * Walk the list: Just grab the head because the
244                 * DetachRegion removed the previous head.
245                 */
246                RegionObj = HandlerObj->AddressSpace.RegionList;
247
248            }
249
250            /* Remove this Handler object from the list */
251
252            *LastObjPtr = HandlerObj->AddressSpace.Next;
253
254            /* Now we can delete the handler object */
255
256            AcpiUtRemoveReference (HandlerObj);
257            goto UnlockAndExit;
258        }
259
260        /* Walk the linked list of handlers */
261
262        LastObjPtr = &HandlerObj->AddressSpace.Next;
263        HandlerObj = HandlerObj->AddressSpace.Next;
264    }
265
266    /* The handler does not exist */
267
268    ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
269        "Unable to remove address handler %p for %s(%X), DevNode %p, obj %p\n",
270        Handler, AcpiUtGetRegionName (SpaceId), SpaceId, Node, ObjDesc));
271
272    Status = AE_NOT_EXIST;
273
274UnlockAndExit:
275    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
276    return_ACPI_STATUS (Status);
277}
278
279ACPI_EXPORT_SYMBOL (AcpiRemoveAddressSpaceHandler)
280
281