1 /******************************************************************************
2  *
3  * Module Name: evxfregn - External Interfaces, ACPI Operation Regions and
4  *                         Address Spaces.
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2013, 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 #define EXPORT_ACPI_INTERFACES
47 
48 #include "acpi.h"
49 #include "accommon.h"
50 #include "acnamesp.h"
51 #include "acevents.h"
52 
53 #define _COMPONENT          ACPI_EVENTS
54         ACPI_MODULE_NAME    ("evxfregn")
55 
56 
57 /*******************************************************************************
58  *
59  * FUNCTION:    AcpiInstallAddressSpaceHandler
60  *
61  * PARAMETERS:  Device          - Handle for the device
62  *              SpaceId         - The address space ID
63  *              Handler         - Address of the handler
64  *              Setup           - Address of the setup function
65  *              Context         - Value passed to the handler on each access
66  *
67  * RETURN:      Status
68  *
69  * DESCRIPTION: Install a handler for all OpRegions of a given SpaceId.
70  *
71  * NOTE: This function should only be called after AcpiEnableSubsystem has
72  * been called. This is because any _REG methods associated with the Space ID
73  * are executed here, and these methods can only be safely executed after
74  * the default handlers have been installed and the hardware has been
75  * initialized (via AcpiEnableSubsystem.)
76  *
77  ******************************************************************************/
78 
79 ACPI_STATUS
80 AcpiInstallAddressSpaceHandler (
81     ACPI_HANDLE             Device,
82     ACPI_ADR_SPACE_TYPE     SpaceId,
83     ACPI_ADR_SPACE_HANDLER  Handler,
84     ACPI_ADR_SPACE_SETUP    Setup,
85     void                    *Context)
86 {
87     ACPI_NAMESPACE_NODE     *Node;
88     ACPI_STATUS             Status;
89 
90 
91     ACPI_FUNCTION_TRACE (AcpiInstallAddressSpaceHandler);
92 
93 
94     /* Parameter validation */
95 
96     if (!Device)
97     {
98         return_ACPI_STATUS (AE_BAD_PARAMETER);
99     }
100 
101     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
102     if (ACPI_FAILURE (Status))
103     {
104         return_ACPI_STATUS (Status);
105     }
106 
107     /* Convert and validate the device handle */
108 
109     Node = AcpiNsValidateHandle (Device);
110     if (!Node)
111     {
112         Status = AE_BAD_PARAMETER;
113         goto UnlockAndExit;
114     }
115 
116     /* Install the handler for all Regions for this Space ID */
117 
118     Status = AcpiEvInstallSpaceHandler (Node, SpaceId, Handler, Setup, Context);
119     if (ACPI_FAILURE (Status))
120     {
121         goto UnlockAndExit;
122     }
123 
124     /*
125      * For the default SpaceIDs, (the IDs for which there are default region handlers
126      * installed) Only execute the _REG methods if the global initialization _REG
127      * methods have already been run (via AcpiInitializeObjects). In other words,
128      * we will defer the execution of the _REG methods for these SpaceIDs until
129      * execution of AcpiInitializeObjects. This is done because we need the handlers
130      * for the default spaces (mem/io/pci/table) to be installed before we can run
131      * any control methods (or _REG methods). There is known BIOS code that depends
132      * on this.
133      *
134      * For all other SpaceIDs, we can safely execute the _REG methods immediately.
135      * This means that for IDs like EmbeddedController, this function should be called
136      * only after AcpiEnableSubsystem has been called.
137      */
138     switch (SpaceId)
139     {
140     case ACPI_ADR_SPACE_SYSTEM_MEMORY:
141     case ACPI_ADR_SPACE_SYSTEM_IO:
142     case ACPI_ADR_SPACE_PCI_CONFIG:
143     case ACPI_ADR_SPACE_DATA_TABLE:
144 
145         if (!AcpiGbl_RegMethodsExecuted)
146         {
147             /* We will defer execution of the _REG methods for this space */
148             goto UnlockAndExit;
149         }
150         break;
151 
152     default:
153 
154         break;
155     }
156 
157     /* Run all _REG methods for this address space */
158 
159     Status = AcpiEvExecuteRegMethods (Node, SpaceId);
160 
161 
162 UnlockAndExit:
163     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
164     return_ACPI_STATUS (Status);
165 }
166 
167 ACPI_EXPORT_SYMBOL (AcpiInstallAddressSpaceHandler)
168 
169 
170 /*******************************************************************************
171  *
172  * FUNCTION:    AcpiRemoveAddressSpaceHandler
173  *
174  * PARAMETERS:  Device          - Handle for the device
175  *              SpaceId         - The address space ID
176  *              Handler         - Address of the handler
177  *
178  * RETURN:      Status
179  *
180  * DESCRIPTION: Remove a previously installed handler.
181  *
182  ******************************************************************************/
183 
184 ACPI_STATUS
185 AcpiRemoveAddressSpaceHandler (
186     ACPI_HANDLE             Device,
187     ACPI_ADR_SPACE_TYPE     SpaceId,
188     ACPI_ADR_SPACE_HANDLER  Handler)
189 {
190     ACPI_OPERAND_OBJECT     *ObjDesc;
191     ACPI_OPERAND_OBJECT     *HandlerObj;
192     ACPI_OPERAND_OBJECT     *RegionObj;
193     ACPI_OPERAND_OBJECT     **LastObjPtr;
194     ACPI_NAMESPACE_NODE     *Node;
195     ACPI_STATUS             Status;
196 
197 
198     ACPI_FUNCTION_TRACE (AcpiRemoveAddressSpaceHandler);
199 
200 
201     /* Parameter validation */
202 
203     if (!Device)
204     {
205         return_ACPI_STATUS (AE_BAD_PARAMETER);
206     }
207 
208     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
209     if (ACPI_FAILURE (Status))
210     {
211         return_ACPI_STATUS (Status);
212     }
213 
214     /* Convert and validate the device handle */
215 
216     Node = AcpiNsValidateHandle (Device);
217     if (!Node ||
218         ((Node->Type != ACPI_TYPE_DEVICE)    &&
219          (Node->Type != ACPI_TYPE_PROCESSOR) &&
220          (Node->Type != ACPI_TYPE_THERMAL)   &&
221          (Node != AcpiGbl_RootNode)))
222     {
223         Status = AE_BAD_PARAMETER;
224         goto UnlockAndExit;
225     }
226 
227     /* Make sure the internal object exists */
228 
229     ObjDesc = AcpiNsGetAttachedObject (Node);
230     if (!ObjDesc)
231     {
232         Status = AE_NOT_EXIST;
233         goto UnlockAndExit;
234     }
235 
236     /* Find the address handler the user requested */
237 
238     HandlerObj = ObjDesc->Device.Handler;
239     LastObjPtr = &ObjDesc->Device.Handler;
240     while (HandlerObj)
241     {
242         /* We have a handler, see if user requested this one */
243 
244         if (HandlerObj->AddressSpace.SpaceId == SpaceId)
245         {
246             /* Handler must be the same as the installed handler */
247 
248             if (HandlerObj->AddressSpace.Handler != Handler)
249             {
250                 Status = AE_BAD_PARAMETER;
251                 goto UnlockAndExit;
252             }
253 
254             /* Matched SpaceId, first dereference this in the Regions */
255 
256             ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
257                 "Removing address handler %p(%p) for region %s "
258                 "on Device %p(%p)\n",
259                 HandlerObj, Handler, AcpiUtGetRegionName (SpaceId),
260                 Node, ObjDesc));
261 
262             RegionObj = HandlerObj->AddressSpace.RegionList;
263 
264             /* Walk the handler's region list */
265 
266             while (RegionObj)
267             {
268                 /*
269                  * First disassociate the handler from the region.
270                  *
271                  * NOTE: this doesn't mean that the region goes away
272                  * The region is just inaccessible as indicated to
273                  * the _REG method
274                  */
275                 AcpiEvDetachRegion (RegionObj, TRUE);
276 
277                 /*
278                  * Walk the list: Just grab the head because the
279                  * DetachRegion removed the previous head.
280                  */
281                 RegionObj = HandlerObj->AddressSpace.RegionList;
282 
283             }
284 
285             /* Remove this Handler object from the list */
286 
287             *LastObjPtr = HandlerObj->AddressSpace.Next;
288 
289             /* Now we can delete the handler object */
290 
291             AcpiUtRemoveReference (HandlerObj);
292             goto UnlockAndExit;
293         }
294 
295         /* Walk the linked list of handlers */
296 
297         LastObjPtr = &HandlerObj->AddressSpace.Next;
298         HandlerObj = HandlerObj->AddressSpace.Next;
299     }
300 
301     /* The handler does not exist */
302 
303     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
304         "Unable to remove address handler %p for %s(%X), DevNode %p, obj %p\n",
305         Handler, AcpiUtGetRegionName (SpaceId), SpaceId, Node, ObjDesc));
306 
307     Status = AE_NOT_EXIST;
308 
309 UnlockAndExit:
310     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
311     return_ACPI_STATUS (Status);
312 }
313 
314 ACPI_EXPORT_SYMBOL (AcpiRemoveAddressSpaceHandler)
315