Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / contrib / dev / acpica-unix / events / evregion.c
1 /******************************************************************************
2  *
3  * Module Name: evregion - ACPI AddressSpace (OpRegion) handler dispatch
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights.  You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code.  No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision.  In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change.  Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee.  Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution.  In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government.  In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115
116
117 #define __EVREGION_C__
118
119 #include "acpi.h"
120 #include "accommon.h"
121 #include "acevents.h"
122 #include "acnamesp.h"
123 #include "acinterp.h"
124
125 #define _COMPONENT          ACPI_EVENTS
126         ACPI_MODULE_NAME    ("evregion")
127
128
129 /* Local prototypes */
130
131 static BOOLEAN
132 AcpiEvHasDefaultHandler (
133     ACPI_NAMESPACE_NODE     *Node,
134     ACPI_ADR_SPACE_TYPE     SpaceId);
135
136 static ACPI_STATUS
137 AcpiEvRegRun (
138     ACPI_HANDLE             ObjHandle,
139     UINT32                  Level,
140     void                    *Context,
141     void                    **ReturnValue);
142
143 static ACPI_STATUS
144 AcpiEvInstallHandler (
145     ACPI_HANDLE             ObjHandle,
146     UINT32                  Level,
147     void                    *Context,
148     void                    **ReturnValue);
149
150 /* These are the address spaces that will get default handlers */
151
152 #define ACPI_NUM_DEFAULT_SPACES     4
153
154 static UINT8        AcpiGbl_DefaultAddressSpaces[ACPI_NUM_DEFAULT_SPACES] =
155 {
156     ACPI_ADR_SPACE_SYSTEM_MEMORY,
157     ACPI_ADR_SPACE_SYSTEM_IO,
158     ACPI_ADR_SPACE_PCI_CONFIG,
159     ACPI_ADR_SPACE_DATA_TABLE
160 };
161
162
163 /*******************************************************************************
164  *
165  * FUNCTION:    AcpiEvInstallRegionHandlers
166  *
167  * PARAMETERS:  None
168  *
169  * RETURN:      Status
170  *
171  * DESCRIPTION: Installs the core subsystem default address space handlers.
172  *
173  ******************************************************************************/
174
175 ACPI_STATUS
176 AcpiEvInstallRegionHandlers (
177     void)
178 {
179     ACPI_STATUS             Status;
180     UINT32                  i;
181
182
183     ACPI_FUNCTION_TRACE (EvInstallRegionHandlers);
184
185
186     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
187     if (ACPI_FAILURE (Status))
188     {
189         return_ACPI_STATUS (Status);
190     }
191
192     /*
193      * All address spaces (PCI Config, EC, SMBus) are scope dependent and
194      * registration must occur for a specific device.
195      *
196      * In the case of the system memory and IO address spaces there is
197      * currently no device associated with the address space. For these we
198      * use the root.
199      *
200      * We install the default PCI config space handler at the root so that
201      * this space is immediately available even though the we have not
202      * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
203      * specification which states that the PCI config space must be always
204      * available -- even though we are nowhere near ready to find the PCI root
205      * buses at this point.
206      *
207      * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
208      * has already been installed (via AcpiInstallAddressSpaceHandler).
209      * Similar for AE_SAME_HANDLER.
210      */
211     for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
212     {
213         Status = AcpiEvInstallSpaceHandler (AcpiGbl_RootNode,
214                     AcpiGbl_DefaultAddressSpaces[i],
215                     ACPI_DEFAULT_HANDLER, NULL, NULL);
216         switch (Status)
217         {
218         case AE_OK:
219         case AE_SAME_HANDLER:
220         case AE_ALREADY_EXISTS:
221
222             /* These exceptions are all OK */
223
224             Status = AE_OK;
225             break;
226
227         default:
228
229             goto UnlockAndExit;
230         }
231     }
232
233 UnlockAndExit:
234     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
235     return_ACPI_STATUS (Status);
236 }
237
238
239 /*******************************************************************************
240  *
241  * FUNCTION:    AcpiEvHasDefaultHandler
242  *
243  * PARAMETERS:  Node                - Namespace node for the device
244  *              SpaceId             - The address space ID
245  *
246  * RETURN:      TRUE if default handler is installed, FALSE otherwise
247  *
248  * DESCRIPTION: Check if the default handler is installed for the requested
249  *              space ID.
250  *
251  ******************************************************************************/
252
253 static BOOLEAN
254 AcpiEvHasDefaultHandler (
255     ACPI_NAMESPACE_NODE     *Node,
256     ACPI_ADR_SPACE_TYPE     SpaceId)
257 {
258     ACPI_OPERAND_OBJECT     *ObjDesc;
259     ACPI_OPERAND_OBJECT     *HandlerObj;
260
261
262     /* Must have an existing internal object */
263
264     ObjDesc = AcpiNsGetAttachedObject (Node);
265     if (ObjDesc)
266     {
267         HandlerObj = ObjDesc->Device.Handler;
268
269         /* Walk the linked list of handlers for this object */
270
271         while (HandlerObj)
272         {
273             if (HandlerObj->AddressSpace.SpaceId == SpaceId)
274             {
275                 if (HandlerObj->AddressSpace.HandlerFlags &
276                         ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
277                 {
278                     return (TRUE);
279                 }
280             }
281
282             HandlerObj = HandlerObj->AddressSpace.Next;
283         }
284     }
285
286     return (FALSE);
287 }
288
289
290 /*******************************************************************************
291  *
292  * FUNCTION:    AcpiEvInitializeOpRegions
293  *
294  * PARAMETERS:  None
295  *
296  * RETURN:      Status
297  *
298  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
299  *              an installed default region handler.
300  *
301  ******************************************************************************/
302
303 ACPI_STATUS
304 AcpiEvInitializeOpRegions (
305     void)
306 {
307     ACPI_STATUS             Status;
308     UINT32                  i;
309
310
311     ACPI_FUNCTION_TRACE (EvInitializeOpRegions);
312
313
314     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
315     if (ACPI_FAILURE (Status))
316     {
317         return_ACPI_STATUS (Status);
318     }
319
320     /* Run the _REG methods for OpRegions in each default address space */
321
322     for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
323     {
324         /*
325          * Make sure the installed handler is the DEFAULT handler. If not the
326          * default, the _REG methods will have already been run (when the
327          * handler was installed)
328          */
329         if (AcpiEvHasDefaultHandler (AcpiGbl_RootNode,
330                AcpiGbl_DefaultAddressSpaces[i]))
331         {
332             Status = AcpiEvExecuteRegMethods (AcpiGbl_RootNode,
333                         AcpiGbl_DefaultAddressSpaces[i]);
334         }
335     }
336
337     AcpiGbl_RegMethodsExecuted = TRUE;
338
339     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
340     return_ACPI_STATUS (Status);
341 }
342
343
344 /*******************************************************************************
345  *
346  * FUNCTION:    AcpiEvExecuteRegMethod
347  *
348  * PARAMETERS:  RegionObj           - Region object
349  *              Function            - Passed to _REG: On (1) or Off (0)
350  *
351  * RETURN:      Status
352  *
353  * DESCRIPTION: Execute _REG method for a region
354  *
355  ******************************************************************************/
356
357 ACPI_STATUS
358 AcpiEvExecuteRegMethod (
359     ACPI_OPERAND_OBJECT     *RegionObj,
360     UINT32                  Function)
361 {
362     ACPI_EVALUATE_INFO      *Info;
363     ACPI_OPERAND_OBJECT     *Args[3];
364     ACPI_OPERAND_OBJECT     *RegionObj2;
365     ACPI_STATUS             Status;
366
367
368     ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
369
370
371     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
372     if (!RegionObj2)
373     {
374         return_ACPI_STATUS (AE_NOT_EXIST);
375     }
376
377     if (RegionObj2->Extra.Method_REG == NULL)
378     {
379         return_ACPI_STATUS (AE_OK);
380     }
381
382     /* Allocate and initialize the evaluation information block */
383
384     Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
385     if (!Info)
386     {
387         return_ACPI_STATUS (AE_NO_MEMORY);
388     }
389
390     Info->PrefixNode = RegionObj2->Extra.Method_REG;
391     Info->Pathname = NULL;
392     Info->Parameters = Args;
393     Info->Flags = ACPI_IGNORE_RETURN_VALUE;
394
395     /*
396      * The _REG method has two arguments:
397      *
398      * Arg0 - Integer:
399      *  Operation region space ID Same value as RegionObj->Region.SpaceId
400      *
401      * Arg1 - Integer:
402      *  connection status 1 for connecting the handler, 0 for disconnecting
403      *  the handler (Passed as a parameter)
404      */
405     Args[0] = AcpiUtCreateIntegerObject ((UINT64) RegionObj->Region.SpaceId);
406     if (!Args[0])
407     {
408         Status = AE_NO_MEMORY;
409         goto Cleanup1;
410     }
411
412     Args[1] = AcpiUtCreateIntegerObject ((UINT64) Function);
413     if (!Args[1])
414     {
415         Status = AE_NO_MEMORY;
416         goto Cleanup2;
417     }
418
419     Args[2] = NULL; /* Terminate list */
420
421     /* Execute the method, no return value */
422
423     ACPI_DEBUG_EXEC (
424         AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
425
426     Status = AcpiNsEvaluate (Info);
427     AcpiUtRemoveReference (Args[1]);
428
429 Cleanup2:
430     AcpiUtRemoveReference (Args[0]);
431
432 Cleanup1:
433     ACPI_FREE (Info);
434     return_ACPI_STATUS (Status);
435 }
436
437
438 /*******************************************************************************
439  *
440  * FUNCTION:    AcpiEvAddressSpaceDispatch
441  *
442  * PARAMETERS:  RegionObj           - Internal region object
443  *              Function            - Read or Write operation
444  *              RegionOffset        - Where in the region to read or write
445  *              BitWidth            - Field width in bits (8, 16, 32, or 64)
446  *              Value               - Pointer to in or out value, must be
447  *                                    a full 64-bit integer
448  *
449  * RETURN:      Status
450  *
451  * DESCRIPTION: Dispatch an address space or operation region access to
452  *              a previously installed handler.
453  *
454  ******************************************************************************/
455
456 ACPI_STATUS
457 AcpiEvAddressSpaceDispatch (
458     ACPI_OPERAND_OBJECT     *RegionObj,
459     UINT32                  Function,
460     UINT32                  RegionOffset,
461     UINT32                  BitWidth,
462     UINT64                  *Value)
463 {
464     ACPI_STATUS             Status;
465     ACPI_ADR_SPACE_HANDLER  Handler;
466     ACPI_ADR_SPACE_SETUP    RegionSetup;
467     ACPI_OPERAND_OBJECT     *HandlerDesc;
468     ACPI_OPERAND_OBJECT     *RegionObj2;
469     void                    *RegionContext = NULL;
470
471
472     ACPI_FUNCTION_TRACE (EvAddressSpaceDispatch);
473
474
475     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
476     if (!RegionObj2)
477     {
478         return_ACPI_STATUS (AE_NOT_EXIST);
479     }
480
481     /* Ensure that there is a handler associated with this region */
482
483     HandlerDesc = RegionObj->Region.Handler;
484     if (!HandlerDesc)
485     {
486         ACPI_ERROR ((AE_INFO,
487             "No handler for Region [%4.4s] (%p) [%s]",
488             AcpiUtGetNodeName (RegionObj->Region.Node),
489             RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
490
491         return_ACPI_STATUS (AE_NOT_EXIST);
492     }
493
494     /*
495      * It may be the case that the region has never been initialized.
496      * Some types of regions require special init code
497      */
498     if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
499     {
500         /* This region has not been initialized yet, do it */
501
502         RegionSetup = HandlerDesc->AddressSpace.Setup;
503         if (!RegionSetup)
504         {
505             /* No initialization routine, exit with error */
506
507             ACPI_ERROR ((AE_INFO,
508                 "No init routine for region(%p) [%s]",
509                 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
510             return_ACPI_STATUS (AE_NOT_EXIST);
511         }
512
513         /*
514          * We must exit the interpreter because the region setup will
515          * potentially execute control methods (for example, the _REG method
516          * for this region)
517          */
518         AcpiExExitInterpreter ();
519
520         Status = RegionSetup (RegionObj, ACPI_REGION_ACTIVATE,
521                     HandlerDesc->AddressSpace.Context, &RegionContext);
522
523         /* Re-enter the interpreter */
524
525         AcpiExEnterInterpreter ();
526
527         /* Check for failure of the Region Setup */
528
529         if (ACPI_FAILURE (Status))
530         {
531             ACPI_EXCEPTION ((AE_INFO, Status,
532                 "During region initialization: [%s]",
533                 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
534             return_ACPI_STATUS (Status);
535         }
536
537         /* Region initialization may have been completed by RegionSetup */
538
539         if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
540         {
541             RegionObj->Region.Flags |= AOPOBJ_SETUP_COMPLETE;
542
543             if (RegionObj2->Extra.RegionContext)
544             {
545                 /* The handler for this region was already installed */
546
547                 ACPI_FREE (RegionContext);
548             }
549             else
550             {
551                 /*
552                  * Save the returned context for use in all accesses to
553                  * this particular region
554                  */
555                 RegionObj2->Extra.RegionContext = RegionContext;
556             }
557         }
558     }
559
560     /* We have everything we need, we can invoke the address space handler */
561
562     Handler = HandlerDesc->AddressSpace.Handler;
563
564     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
565         "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
566         &RegionObj->Region.Handler->AddressSpace, Handler,
567         ACPI_FORMAT_NATIVE_UINT (RegionObj->Region.Address + RegionOffset),
568         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
569
570     if (!(HandlerDesc->AddressSpace.HandlerFlags &
571             ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
572     {
573         /*
574          * For handlers other than the default (supplied) handlers, we must
575          * exit the interpreter because the handler *might* block -- we don't
576          * know what it will do, so we can't hold the lock on the intepreter.
577          */
578         AcpiExExitInterpreter();
579     }
580
581     /* Call the handler */
582
583     Status = Handler (Function,
584         (RegionObj->Region.Address + RegionOffset), BitWidth, Value,
585         HandlerDesc->AddressSpace.Context, RegionObj2->Extra.RegionContext);
586
587     if (ACPI_FAILURE (Status))
588     {
589         ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
590             AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
591     }
592
593     if (!(HandlerDesc->AddressSpace.HandlerFlags &
594             ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
595     {
596         /*
597          * We just returned from a non-default handler, we must re-enter the
598          * interpreter
599          */
600        AcpiExEnterInterpreter ();
601     }
602
603     return_ACPI_STATUS (Status);
604 }
605
606
607 /*******************************************************************************
608  *
609  * FUNCTION:    AcpiEvDetachRegion
610  *
611  * PARAMETERS:  RegionObj           - Region Object
612  *              AcpiNsIsLocked      - Namespace Region Already Locked?
613  *
614  * RETURN:      None
615  *
616  * DESCRIPTION: Break the association between the handler and the region
617  *              this is a two way association.
618  *
619  ******************************************************************************/
620
621 void
622 AcpiEvDetachRegion(
623     ACPI_OPERAND_OBJECT     *RegionObj,
624     BOOLEAN                 AcpiNsIsLocked)
625 {
626     ACPI_OPERAND_OBJECT     *HandlerObj;
627     ACPI_OPERAND_OBJECT     *ObjDesc;
628     ACPI_OPERAND_OBJECT     **LastObjPtr;
629     ACPI_ADR_SPACE_SETUP    RegionSetup;
630     void                    **RegionContext;
631     ACPI_OPERAND_OBJECT     *RegionObj2;
632     ACPI_STATUS             Status;
633
634
635     ACPI_FUNCTION_TRACE (EvDetachRegion);
636
637
638     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
639     if (!RegionObj2)
640     {
641         return_VOID;
642     }
643     RegionContext = &RegionObj2->Extra.RegionContext;
644
645     /* Get the address handler from the region object */
646
647     HandlerObj = RegionObj->Region.Handler;
648     if (!HandlerObj)
649     {
650         /* This region has no handler, all done */
651
652         return_VOID;
653     }
654
655     /* Find this region in the handler's list */
656
657     ObjDesc = HandlerObj->AddressSpace.RegionList;
658     LastObjPtr = &HandlerObj->AddressSpace.RegionList;
659
660     while (ObjDesc)
661     {
662         /* Is this the correct Region? */
663
664         if (ObjDesc == RegionObj)
665         {
666             ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
667                 "Removing Region %p from address handler %p\n",
668                 RegionObj, HandlerObj));
669
670             /* This is it, remove it from the handler's list */
671
672             *LastObjPtr = ObjDesc->Region.Next;
673             ObjDesc->Region.Next = NULL;        /* Must clear field */
674
675             if (AcpiNsIsLocked)
676             {
677                 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
678                 if (ACPI_FAILURE (Status))
679                 {
680                     return_VOID;
681                 }
682             }
683
684             /* Now stop region accesses by executing the _REG method */
685
686             Status = AcpiEvExecuteRegMethod (RegionObj, 0);
687             if (ACPI_FAILURE (Status))
688             {
689                 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
690                     AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
691             }
692
693             if (AcpiNsIsLocked)
694             {
695                 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
696                 if (ACPI_FAILURE (Status))
697                 {
698                     return_VOID;
699                 }
700             }
701
702             /*
703              * If the region has been activated, call the setup handler with
704              * the deactivate notification
705              */
706             if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
707             {
708                 RegionSetup = HandlerObj->AddressSpace.Setup;
709                 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
710                     HandlerObj->AddressSpace.Context, RegionContext);
711
712                 /* Init routine may fail, Just ignore errors */
713
714                 if (ACPI_FAILURE (Status))
715                 {
716                     ACPI_EXCEPTION ((AE_INFO, Status,
717                         "from region handler - deactivate, [%s]",
718                         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
719                 }
720
721                 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
722             }
723
724             /*
725              * Remove handler reference in the region
726              *
727              * NOTE: this doesn't mean that the region goes away, the region
728              * is just inaccessible as indicated to the _REG method
729              *
730              * If the region is on the handler's list, this must be the
731              * region's handler
732              */
733             RegionObj->Region.Handler = NULL;
734             AcpiUtRemoveReference (HandlerObj);
735
736             return_VOID;
737         }
738
739         /* Walk the linked list of handlers */
740
741         LastObjPtr = &ObjDesc->Region.Next;
742         ObjDesc = ObjDesc->Region.Next;
743     }
744
745     /* If we get here, the region was not in the handler's region list */
746
747     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
748         "Cannot remove region %p from address handler %p\n",
749         RegionObj, HandlerObj));
750
751     return_VOID;
752 }
753
754
755 /*******************************************************************************
756  *
757  * FUNCTION:    AcpiEvAttachRegion
758  *
759  * PARAMETERS:  HandlerObj          - Handler Object
760  *              RegionObj           - Region Object
761  *              AcpiNsIsLocked      - Namespace Region Already Locked?
762  *
763  * RETURN:      None
764  *
765  * DESCRIPTION: Create the association between the handler and the region
766  *              this is a two way association.
767  *
768  ******************************************************************************/
769
770 ACPI_STATUS
771 AcpiEvAttachRegion (
772     ACPI_OPERAND_OBJECT     *HandlerObj,
773     ACPI_OPERAND_OBJECT     *RegionObj,
774     BOOLEAN                 AcpiNsIsLocked)
775 {
776
777     ACPI_FUNCTION_TRACE (EvAttachRegion);
778
779
780     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
781         "Adding Region [%4.4s] %p to address handler %p [%s]\n",
782         AcpiUtGetNodeName (RegionObj->Region.Node),
783         RegionObj, HandlerObj,
784         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
785
786     /* Link this region to the front of the handler's list */
787
788     RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
789     HandlerObj->AddressSpace.RegionList = RegionObj;
790
791     /* Install the region's handler */
792
793     if (RegionObj->Region.Handler)
794     {
795         return_ACPI_STATUS (AE_ALREADY_EXISTS);
796     }
797
798     RegionObj->Region.Handler = HandlerObj;
799     AcpiUtAddReference (HandlerObj);
800
801     return_ACPI_STATUS (AE_OK);
802 }
803
804
805 /*******************************************************************************
806  *
807  * FUNCTION:    AcpiEvInstallHandler
808  *
809  * PARAMETERS:  WalkNamespace callback
810  *
811  * DESCRIPTION: This routine installs an address handler into objects that are
812  *              of type Region or Device.
813  *
814  *              If the Object is a Device, and the device has a handler of
815  *              the same type then the search is terminated in that branch.
816  *
817  *              This is because the existing handler is closer in proximity
818  *              to any more regions than the one we are trying to install.
819  *
820  ******************************************************************************/
821
822 static ACPI_STATUS
823 AcpiEvInstallHandler (
824     ACPI_HANDLE             ObjHandle,
825     UINT32                  Level,
826     void                    *Context,
827     void                    **ReturnValue)
828 {
829     ACPI_OPERAND_OBJECT     *HandlerObj;
830     ACPI_OPERAND_OBJECT     *NextHandlerObj;
831     ACPI_OPERAND_OBJECT     *ObjDesc;
832     ACPI_NAMESPACE_NODE     *Node;
833     ACPI_STATUS             Status;
834
835
836     ACPI_FUNCTION_NAME (EvInstallHandler);
837
838
839     HandlerObj = (ACPI_OPERAND_OBJECT  *) Context;
840
841     /* Parameter validation */
842
843     if (!HandlerObj)
844     {
845         return (AE_OK);
846     }
847
848     /* Convert and validate the device handle */
849
850     Node = AcpiNsValidateHandle (ObjHandle);
851     if (!Node)
852     {
853         return (AE_BAD_PARAMETER);
854     }
855
856     /*
857      * We only care about regions and objects that are allowed to have
858      * address space handlers
859      */
860     if ((Node->Type != ACPI_TYPE_DEVICE) &&
861         (Node->Type != ACPI_TYPE_REGION) &&
862         (Node != AcpiGbl_RootNode))
863     {
864         return (AE_OK);
865     }
866
867     /* Check for an existing internal object */
868
869     ObjDesc = AcpiNsGetAttachedObject (Node);
870     if (!ObjDesc)
871     {
872         /* No object, just exit */
873
874         return (AE_OK);
875     }
876
877     /* Devices are handled different than regions */
878
879     if (ObjDesc->Common.Type == ACPI_TYPE_DEVICE)
880     {
881         /* Check if this Device already has a handler for this address space */
882
883         NextHandlerObj = ObjDesc->Device.Handler;
884         while (NextHandlerObj)
885         {
886             /* Found a handler, is it for the same address space? */
887
888             if (NextHandlerObj->AddressSpace.SpaceId ==
889                     HandlerObj->AddressSpace.SpaceId)
890             {
891                 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
892                     "Found handler for region [%s] in device %p(%p) "
893                     "handler %p\n",
894                     AcpiUtGetRegionName (HandlerObj->AddressSpace.SpaceId),
895                     ObjDesc, NextHandlerObj, HandlerObj));
896
897                 /*
898                  * Since the object we found it on was a device, then it
899                  * means that someone has already installed a handler for
900                  * the branch of the namespace from this device on. Just
901                  * bail out telling the walk routine to not traverse this
902                  * branch. This preserves the scoping rule for handlers.
903                  */
904                 return (AE_CTRL_DEPTH);
905             }
906
907             /* Walk the linked list of handlers attached to this device */
908
909             NextHandlerObj = NextHandlerObj->AddressSpace.Next;
910         }
911
912         /*
913          * As long as the device didn't have a handler for this space we
914          * don't care about it. We just ignore it and proceed.
915          */
916         return (AE_OK);
917     }
918
919     /* Object is a Region */
920
921     if (ObjDesc->Region.SpaceId != HandlerObj->AddressSpace.SpaceId)
922     {
923         /* This region is for a different address space, just ignore it */
924
925         return (AE_OK);
926     }
927
928     /*
929      * Now we have a region and it is for the handler's address space type.
930      *
931      * First disconnect region for any previous handler (if any)
932      */
933     AcpiEvDetachRegion (ObjDesc, FALSE);
934
935     /* Connect the region to the new handler */
936
937     Status = AcpiEvAttachRegion (HandlerObj, ObjDesc, FALSE);
938     return (Status);
939 }
940
941
942 /*******************************************************************************
943  *
944  * FUNCTION:    AcpiEvInstallSpaceHandler
945  *
946  * PARAMETERS:  Node            - Namespace node for the device
947  *              SpaceId         - The address space ID
948  *              Handler         - Address of the handler
949  *              Setup           - Address of the setup function
950  *              Context         - Value passed to the handler on each access
951  *
952  * RETURN:      Status
953  *
954  * DESCRIPTION: Install a handler for all OpRegions of a given SpaceId.
955  *              Assumes namespace is locked
956  *
957  ******************************************************************************/
958
959 ACPI_STATUS
960 AcpiEvInstallSpaceHandler (
961     ACPI_NAMESPACE_NODE     *Node,
962     ACPI_ADR_SPACE_TYPE     SpaceId,
963     ACPI_ADR_SPACE_HANDLER  Handler,
964     ACPI_ADR_SPACE_SETUP    Setup,
965     void                    *Context)
966 {
967     ACPI_OPERAND_OBJECT     *ObjDesc;
968     ACPI_OPERAND_OBJECT     *HandlerObj;
969     ACPI_STATUS             Status;
970     ACPI_OBJECT_TYPE        Type;
971     UINT8                  Flags = 0;
972
973
974     ACPI_FUNCTION_TRACE (EvInstallSpaceHandler);
975
976
977     /*
978      * This registration is valid for only the types below and the root. This
979      * is where the default handlers get placed.
980      */
981     if ((Node->Type != ACPI_TYPE_DEVICE)     &&
982         (Node->Type != ACPI_TYPE_PROCESSOR)  &&
983         (Node->Type != ACPI_TYPE_THERMAL)    &&
984         (Node != AcpiGbl_RootNode))
985     {
986         Status = AE_BAD_PARAMETER;
987         goto UnlockAndExit;
988     }
989
990     if (Handler == ACPI_DEFAULT_HANDLER)
991     {
992         Flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
993
994         switch (SpaceId)
995         {
996         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
997             Handler = AcpiExSystemMemorySpaceHandler;
998             Setup   = AcpiEvSystemMemoryRegionSetup;
999             break;
1000
1001         case ACPI_ADR_SPACE_SYSTEM_IO:
1002             Handler = AcpiExSystemIoSpaceHandler;
1003             Setup   = AcpiEvIoSpaceRegionSetup;
1004             break;
1005
1006         case ACPI_ADR_SPACE_PCI_CONFIG:
1007             Handler = AcpiExPciConfigSpaceHandler;
1008             Setup   = AcpiEvPciConfigRegionSetup;
1009             break;
1010
1011         case ACPI_ADR_SPACE_CMOS:
1012             Handler = AcpiExCmosSpaceHandler;
1013             Setup   = AcpiEvCmosRegionSetup;
1014             break;
1015
1016         case ACPI_ADR_SPACE_PCI_BAR_TARGET:
1017             Handler = AcpiExPciBarSpaceHandler;
1018             Setup   = AcpiEvPciBarRegionSetup;
1019             break;
1020
1021         case ACPI_ADR_SPACE_DATA_TABLE:
1022             Handler = AcpiExDataTableSpaceHandler;
1023             Setup   = NULL;
1024             break;
1025
1026         default:
1027             Status = AE_BAD_PARAMETER;
1028             goto UnlockAndExit;
1029         }
1030     }
1031
1032     /* If the caller hasn't specified a setup routine, use the default */
1033
1034     if (!Setup)
1035     {
1036         Setup = AcpiEvDefaultRegionSetup;
1037     }
1038
1039     /* Check for an existing internal object */
1040
1041     ObjDesc = AcpiNsGetAttachedObject (Node);
1042     if (ObjDesc)
1043     {
1044         /*
1045          * The attached device object already exists. Make sure the handler
1046          * is not already installed.
1047          */
1048         HandlerObj = ObjDesc->Device.Handler;
1049
1050         /* Walk the handler list for this device */
1051
1052         while (HandlerObj)
1053         {
1054             /* Same SpaceId indicates a handler already installed */
1055
1056             if (HandlerObj->AddressSpace.SpaceId == SpaceId)
1057             {
1058                 if (HandlerObj->AddressSpace.Handler == Handler)
1059                 {
1060                     /*
1061                      * It is (relatively) OK to attempt to install the SAME
1062                      * handler twice. This can easily happen with the
1063                      * PCI_Config space.
1064                      */
1065                     Status = AE_SAME_HANDLER;
1066                     goto UnlockAndExit;
1067                 }
1068                 else
1069                 {
1070                     /* A handler is already installed */
1071
1072                     Status = AE_ALREADY_EXISTS;
1073                 }
1074                 goto UnlockAndExit;
1075             }
1076
1077             /* Walk the linked list of handlers */
1078
1079             HandlerObj = HandlerObj->AddressSpace.Next;
1080         }
1081     }
1082     else
1083     {
1084         ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
1085             "Creating object on Device %p while installing handler\n", Node));
1086
1087         /* ObjDesc does not exist, create one */
1088
1089         if (Node->Type == ACPI_TYPE_ANY)
1090         {
1091             Type = ACPI_TYPE_DEVICE;
1092         }
1093         else
1094         {
1095             Type = Node->Type;
1096         }
1097
1098         ObjDesc = AcpiUtCreateInternalObject (Type);
1099         if (!ObjDesc)
1100         {
1101             Status = AE_NO_MEMORY;
1102             goto UnlockAndExit;
1103         }
1104
1105         /* Init new descriptor */
1106
1107         ObjDesc->Common.Type = (UINT8) Type;
1108
1109         /* Attach the new object to the Node */
1110
1111         Status = AcpiNsAttachObject (Node, ObjDesc, Type);
1112
1113         /* Remove local reference to the object */
1114
1115         AcpiUtRemoveReference (ObjDesc);
1116
1117         if (ACPI_FAILURE (Status))
1118         {
1119             goto UnlockAndExit;
1120         }
1121     }
1122
1123     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
1124         "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
1125         AcpiUtGetRegionName (SpaceId), SpaceId,
1126         AcpiUtGetNodeName (Node), Node, ObjDesc));
1127
1128     /*
1129      * Install the handler
1130      *
1131      * At this point there is no existing handler. Just allocate the object
1132      * for the handler and link it into the list.
1133      */
1134     HandlerObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
1135     if (!HandlerObj)
1136     {
1137         Status = AE_NO_MEMORY;
1138         goto UnlockAndExit;
1139     }
1140
1141     /* Init handler obj */
1142
1143     HandlerObj->AddressSpace.SpaceId = (UINT8) SpaceId;
1144     HandlerObj->AddressSpace.HandlerFlags = Flags;
1145     HandlerObj->AddressSpace.RegionList = NULL;
1146     HandlerObj->AddressSpace.Node = Node;
1147     HandlerObj->AddressSpace.Handler = Handler;
1148     HandlerObj->AddressSpace.Context = Context;
1149     HandlerObj->AddressSpace.Setup  = Setup;
1150
1151     /* Install at head of Device.AddressSpace list */
1152
1153     HandlerObj->AddressSpace.Next = ObjDesc->Device.Handler;
1154
1155     /*
1156      * The Device object is the first reference on the HandlerObj.
1157      * Each region that uses the handler adds a reference.
1158      */
1159     ObjDesc->Device.Handler = HandlerObj;
1160
1161     /*
1162      * Walk the namespace finding all of the regions this
1163      * handler will manage.
1164      *
1165      * Start at the device and search the branch toward
1166      * the leaf nodes until either the leaf is encountered or
1167      * a device is detected that has an address handler of the
1168      * same type.
1169      *
1170      * In either case, back up and search down the remainder
1171      * of the branch
1172      */
1173     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
1174                 ACPI_NS_WALK_UNLOCK, AcpiEvInstallHandler, NULL,
1175                 HandlerObj, NULL);
1176
1177 UnlockAndExit:
1178     return_ACPI_STATUS (Status);
1179 }
1180
1181
1182 /*******************************************************************************
1183  *
1184  * FUNCTION:    AcpiEvExecuteRegMethods
1185  *
1186  * PARAMETERS:  Node            - Namespace node for the device
1187  *              SpaceId         - The address space ID
1188  *
1189  * RETURN:      Status
1190  *
1191  * DESCRIPTION: Run all _REG methods for the input Space ID;
1192  *              Note: assumes namespace is locked, or system init time.
1193  *
1194  ******************************************************************************/
1195
1196 ACPI_STATUS
1197 AcpiEvExecuteRegMethods (
1198     ACPI_NAMESPACE_NODE     *Node,
1199     ACPI_ADR_SPACE_TYPE     SpaceId)
1200 {
1201     ACPI_STATUS             Status;
1202
1203
1204     ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
1205
1206
1207     /*
1208      * Run all _REG methods for all Operation Regions for this space ID. This
1209      * is a separate walk in order to handle any interdependencies between
1210      * regions and _REG methods. (i.e. handlers must be installed for all
1211      * regions of this Space ID before we can run any _REG methods)
1212      */
1213     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
1214                 ACPI_NS_WALK_UNLOCK, AcpiEvRegRun, NULL,
1215                 &SpaceId, NULL);
1216
1217     return_ACPI_STATUS (Status);
1218 }
1219
1220
1221 /*******************************************************************************
1222  *
1223  * FUNCTION:    AcpiEvRegRun
1224  *
1225  * PARAMETERS:  WalkNamespace callback
1226  *
1227  * DESCRIPTION: Run _REG method for region objects of the requested spaceID
1228  *
1229  ******************************************************************************/
1230
1231 static ACPI_STATUS
1232 AcpiEvRegRun (
1233     ACPI_HANDLE             ObjHandle,
1234     UINT32                  Level,
1235     void                    *Context,
1236     void                    **ReturnValue)
1237 {
1238     ACPI_OPERAND_OBJECT     *ObjDesc;
1239     ACPI_NAMESPACE_NODE     *Node;
1240     ACPI_ADR_SPACE_TYPE     SpaceId;
1241     ACPI_STATUS             Status;
1242
1243
1244     SpaceId = *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE, Context);
1245
1246     /* Convert and validate the device handle */
1247
1248     Node = AcpiNsValidateHandle (ObjHandle);
1249     if (!Node)
1250     {
1251         return (AE_BAD_PARAMETER);
1252     }
1253
1254     /*
1255      * We only care about regions.and objects that are allowed to have address
1256      * space handlers
1257      */
1258     if ((Node->Type != ACPI_TYPE_REGION) &&
1259         (Node != AcpiGbl_RootNode))
1260     {
1261         return (AE_OK);
1262     }
1263
1264     /* Check for an existing internal object */
1265
1266     ObjDesc = AcpiNsGetAttachedObject (Node);
1267     if (!ObjDesc)
1268     {
1269         /* No object, just exit */
1270
1271         return (AE_OK);
1272     }
1273
1274     /* Object is a Region */
1275
1276     if (ObjDesc->Region.SpaceId != SpaceId)
1277     {
1278         /* This region is for a different address space, just ignore it */
1279
1280         return (AE_OK);
1281     }
1282
1283     Status = AcpiEvExecuteRegMethod (ObjDesc, 1);
1284     return (Status);
1285 }
1286